123
Fork of WizFi250Interface_1 by
Revision 0:e3dc9e54a15b, committed 2016-08-30
- Comitter:
- kaizen
- Date:
- Tue Aug 30 00:05:24 2016 +0000
- Child:
- 1:046a828fe0d2
- Commit message:
- First release WizFi250Interface for NetworkInterfaceAPI
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/WizFi250/CBuffer.h Tue Aug 30 00:05:24 2016 +0000
@@ -0,0 +1,78 @@
+/* Copyright (C) 2012 mbed.org, MIT License
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute,
+ * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies or
+ * substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+ * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#ifndef CIRCBUFFER_H_
+#define CIRCBUFFER_H_
+
+template <class T>
+class CircBuffer {
+public:
+ CircBuffer(int length) {
+ write = 0;
+ read = 0;
+ size = length + 1;
+ buf = (T *)malloc(size * sizeof(T));
+ if (buf == NULL)
+ error("Can't allocate memory");
+ };
+
+ bool isFull() {
+ return (((write + 1) % size) == read);
+ };
+
+ bool isEmpty() {
+ return (read == write);
+ };
+
+ void queue(T k) {
+ if (isFull()) {
+// read++;
+// read %= size;
+ return;
+ }
+ buf[write++] = k;
+ write %= size;
+ }
+
+ void flush() {
+ read = 0;
+ write = 0;
+ }
+
+
+ uint32_t available() {
+ return (write >= read) ? write - read : size - read + write;
+ };
+
+ bool dequeue(T * c) {
+ bool empty = isEmpty();
+ if (!empty) {
+ *c = buf[read++];
+ read %= size;
+ }
+ return(!empty);
+ };
+
+private:
+ volatile uint32_t write;
+ volatile uint32_t read;
+ uint32_t size;
+ T * buf;
+};
+
+#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/WizFi250/WizFi250.cpp Tue Aug 30 00:05:24 2016 +0000
@@ -0,0 +1,174 @@
+/*
+ * Copyright (C) 2013 gsfan, MIT License
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute,
+ * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies or
+ * substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+ * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+/* Copyright (C) 2014 Wiznet, MIT License
+ * port to the Wiznet Module WizFi250
+ */
+
+#include "mbed.h"
+#include "WizFi250.h"
+
+WizFi250 * WizFi250::_inst;
+
+
+WizFi250::WizFi250(PinName tx,PinName rx,PinName cts, PinName rts,PinName reset, PinName alarm, int baud):
+ _wizfi(tx,rx), _reset(reset)
+{
+ _inst = this;
+ memset(&_state, 0, sizeof(_state));
+ memset(&_con, 0, sizeof(_con));
+ _state.initialized = false;
+ _state.status = STAT_READY;
+ _state.cid = -1;
+ _state.buf = new CircBuffer<char>(CFG_DATA_SIZE);
+
+ initUart(cts, rts, alarm, baud);
+ _reset.output();
+
+ setRts(true); // release
+ /*
+ wait_ms(500);
+ cmdAT();
+ cmdMECHO(false);
+ if(cts != NC && rts != NC)
+ cmdUSET(baud,"HW");
+ else
+ cmdUSET(baud,"N");
+
+ // WizFi250 will restart by cmdUSET command.
+ wait_ms(1000);
+ cmdAT();
+ */
+}
+
+int WizFi250::join(WiFiMode mode)
+{
+ char sec[10];
+
+ if( cmdMMAC() ) return -1;
+
+ if(mode == WM_AP)
+ _state.wm = WM_AP;
+ else
+ _state.wm = WM_STATION;
+
+ if ( cmdWNET(_state.dhcp) ) return -1;
+ if ( cmdWSET(_state.wm, _state.ssid) ) return -1;
+
+ switch (_state.sec)
+ {
+ case SEC_AUTO:
+ strcpy(sec,"");
+ break;
+ case SEC_OPEN:
+ strcpy(sec,"OPEN");
+ break;
+ case SEC_WEP:
+ strcpy(sec,"WEP");
+ break;
+ case SEC_WPA_TKIP:
+ strcpy(sec,"WPA");
+ break;
+ case SEC_WPA_AES:
+ strcpy(sec,"WPAAES");
+ break;
+ case SEC_WPA2_AES:
+ strcpy(sec,"WPA2AES");
+ break;
+ case SEC_WPA2_TKIP:
+ strcpy(sec,"WPA2TKIP");
+ break;
+ case SEC_WPA2_MIXED:
+ strcpy(sec,"WPA2");
+ break;
+ }
+ if ( cmdWSEC(_state.wm, _state.pass, sec) ) return -1;
+ if ( cmdWJOIN() ) return -1;;
+ _state.associated = true;
+
+ return 0;
+}
+
+bool WizFi250::isAssociated()
+{
+ return _state.associated;
+}
+
+int WizFi250::setMacAddress (const char *mac)
+{
+ if (cmdMMAC(mac)) return -1;
+ strncpy(_state.mac, mac, sizeof(_state.mac));
+ return 0;
+}
+
+int WizFi250::getMacAddress (char *mac)
+{
+ if (cmdMMAC()) return -1;
+ strcpy(mac, _state.mac);
+ return 0;
+}
+
+int WizFi250::setAddress (const char *name)
+{
+ _state.dhcp = true;
+ strncpy(_state.name, name, sizeof(_state.name));
+ return 0;
+}
+
+int WizFi250::setAddress (const char *ip, const char *netmask, const char *gateway, const char *dns, const char *name)
+{
+ _state.dhcp = false;
+ strncpy(_state.ip, ip, sizeof(_state.ip));
+ strncpy(_state.netmask, netmask, sizeof(_state.netmask));
+ strncpy(_state.gateway, gateway, sizeof(_state.gateway));
+ strncpy(_state.nameserver, dns, sizeof(_state.nameserver));
+ strncpy(_state.name, name, sizeof(_state.name));
+ return 0;
+}
+
+int WizFi250::getAddress (char *ip, char *netmask, char *gateway)
+{
+ strcpy(ip, _state.ip);
+ strcpy(netmask, _state.netmask);
+ strcpy(gateway, _state.gateway);
+ return 0;
+}
+
+int WizFi250::setSsid (const char *ssid)
+{
+ strncpy(_state.ssid, ssid, sizeof(_state.ssid));
+ return 0;
+}
+
+int WizFi250::setSec ( Security sec, const char *phrase )
+{
+ _state.sec = sec;
+ strncpy(_state.pass, phrase, strlen(phrase));
+ return 0;
+}
+
+const char *WizFi250::getIPAddress(void)
+{
+ return _state.ip;
+}
+
+const char *WizFi250::getMACAddress(void)
+{
+ return _state.mac;
+}
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/WizFi250/WizFi250.h Tue Aug 30 00:05:24 2016 +0000
@@ -0,0 +1,321 @@
+/* Copyright (C) 2013 gsfan, MIT License
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute,
+ * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies or
+ * substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+ * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+/* Copyright (C) 2014 Wiznet, MIT License
+ * port to the Wiznet Module WizFi250
+ */
+
+#ifndef WIZFI250_H_
+#define WIZFI250_H_
+
+#include "WizFi250_conf.h"
+
+#include "mbed.h"
+#include "RawSerial.h"
+#include "Serial.h"
+#include "CBuffer.h"
+#include <ctype.h>
+#include <stdlib.h>
+#include <string>
+
+using namespace std;
+
+#define BUF_SIZE 1600
+
+//Debug is disabled by default
+#if 1
+#define WIZ_DBG(x, ...) std::printf("[WizFi250: DBG]" x "\r\n", ##__VA_ARGS__);
+#define WIZ_WARN(x, ...) std::printf("[WizFi250: WARN]" x "\r\n", ##__VA_ARGS__);
+#define WIZ_ERR(x, ...) std::printf("[WizFi250: ERR]" x "\r\n", ##__VA_ARGS__);
+#define WIZ_INFO(x, ...) std::printf("[WizFi250: INFO]" x "\r\n", ##__VA_ARGS__);
+#else
+#define WIZ_DBG(x, ...)
+#define WIZ_WARN(x, ...)
+#define WIZ_ERR(x, ...)
+#define WIZ_INFO(x, ...)
+#endif
+
+
+class WizFi250
+{
+public:
+
+ enum AntennaMode{
+ PCB = 0,
+ UFL = 1,
+ AUTO = 3,
+ };
+
+ enum WiFiMode {
+ WM_STATION = 0,
+ WM_AP = 1,
+ };
+
+ /** Wi-Fi security
+ */
+ enum Security {
+ // kaizen need to change
+ SEC_AUTO = 0,
+ SEC_OPEN = 1,
+ SEC_WEP = 2,
+ SEC_WPA_TKIP = 3,
+ SEC_WPA_AES = 4,
+ SEC_WPA2_AES = 5,
+ SEC_WPA2_TKIP = 6,
+ SEC_WPA2_MIXED = 7,
+ };
+
+ /** TCP/IP protocol
+ */
+ enum Protocol {
+ PROTO_UDP = 0,
+ PROTO_TCP = 1,
+ };
+
+ /** Client/Server
+ */
+ enum Type {
+ TYPE_CLIENT = 0,
+ TYPE_SERVER = 1,
+ };
+
+ enum Response {
+ RES_NULL,
+ RES_MACADDRESS,
+ RES_WJOIN,
+ RES_CONNECT,
+ RES_SSEND,
+ RES_FDNS,
+ RES_SMGMT,
+ RES_WSTATUS,
+ };
+
+ enum Mode {
+ MODE_COMMAND,
+ MODE_CMDRESP,
+ MODE_DATA_RX,
+ MODE_DATA_RXUDP,
+ MODE_DATA_RXUDP_BULK,
+ };
+
+ enum Status {
+ STAT_NONE,
+ STAT_READY,
+ STAT_STANDBY,
+ STAT_WAKEUP,
+ STAT_DEEPSLEEP,
+ };
+
+
+ WizFi250 (PinName tx, PinName rx, PinName cts, PinName rts, PinName reset, PinName alarm = NC, int baud = 115200);
+
+ // --------- WizFi250_at.cpp ---------
+ void clearFlags ();
+ int sendCommand (const char * cmd, Response res = RES_NULL, int timeout = DEFAULT_WAIT_RESP_TIMEOUT, int opt = 2);
+
+ int cmdAT ();
+ int cmdMECHO (bool flg);
+ int cmdUSET (int baud, char *flow);
+ int cmdMMAC (const char *mac = NULL);
+ int cmdWSET (WiFiMode mode, const char *ssid, const char *bssid = NULL, int channel = 1);
+ int cmdWANT (AntennaMode mode);
+ int cmdWNET (bool is_dhcp);
+ int cmdWSEC (WiFiMode mode, const char *key, const char *sec = NULL);
+ int cmdWJOIN ();
+ int cmdWLEAVE ();
+ int cmdWSTATUS ();
+ int cmdSCON ( const char *openType, const char *socketType, int localPort, const char *dataMode = "0");
+ int cmdSCON ( const char *openType, const char *socketType, const char *remoteIp, int remotePort, int localPort = 0, const char *dataMode = "0");
+ int cmdSSEND ( const char *data, int cid, int sendSize, const char *remoteIp = NULL, int remotePort = 0, int Timeout = 2000 );
+ int cmdCLOSE ( int cid );
+ int cmdFDNS (const char *host);
+ int cmdSMGMT ( int cid );
+
+
+ static WizFi250 * getInstance() {
+ return _inst;
+ };
+
+
+ // --------- WizFi2550_sock.cpp ---------
+ int getHostByName (const char * host, char *ip);
+ int open (Protocol proto, const char *ip, int remotePort, int localPort = 0, void(*func)(int) = NULL);
+ int listen (Protocol proto, int port, void(*func)(int)=NULL);
+ int close (int cid);
+ void initCon (int cid, bool connected);
+ int send (int cid, const char *buf, int len);
+ int sendto (int cid, const char *buf, int len, const char *ip, int port);
+ int recv (int cid, char *buf, int len);
+ int recvfrom (int cid, char *buf, int len, char *ip, int *port);
+ int readable (int cid);
+ bool isConnected (int cid);
+ int accept (int cid);
+ int getRemote (int cid, char **ip, int *port);
+
+
+
+protected:
+ static WizFi250 * _inst;
+
+
+ // Serial _wizfi
+ RawSerial _wizfi;
+ int _baud;
+ DigitalIn *_cts;
+ DigitalOut *_rts;
+ int _flow;
+
+ DigitalInOut _reset;
+
+ struct STATE {
+ WiFiMode wm;
+ Security sec;
+ char ssid[35];
+ char pass[66];
+ char ip[16];
+ char netmask[16];
+ char gateway[16];
+ char nameserver[16];
+ char mac[18];
+ char resolv[16];
+ char name[32];
+ int rssi;
+ bool dhcp;
+ time_t time;
+
+ bool initialized;
+ bool associated;
+ volatile Mode mode;
+ volatile Status status;
+ bool escape;
+ volatile bool ok, failure;
+ volatile Response res;
+ int cid;
+ int n;
+ CircBuffer<char> *buf;
+ char dbgRespBuf[BUF_SIZE];
+ //char dummyBuf[BUF_SIZE];
+ } _state;
+
+
+public:
+ struct CONNECTION {
+ Protocol protocol;
+ Type type;
+ bool connected;
+ char ip[16];
+ int port;
+ int send_length;
+ int recv_length;
+ CircBuffer<char> *buf;
+ volatile bool received;
+ volatile int parent;
+ volatile bool accept;
+ void(*func)(int);
+ } _con[8];
+
+ // --------- WizFi250.cpp ---------
+
+ int join(WiFiMode mode);
+ bool isAssociated();
+
+ //int limitedap ();
+ //int dissociate ();
+ /*
+ int disconnect () {
+ return dissociate();
+ }
+ */
+
+ Status getStatus ();
+
+ int setMacAddress (const char *mac);
+ int getMacAddress (char *mac);
+ int setAddress (const char *name = NULL);
+ int setAddress (const char *ip, const char *netmask, const char *gateway, const char *dns = NULL, const char *name = NULL);
+ int getAddress (char *ip, char *netmask, char *gateway);
+ int setSsid (const char *ssid);
+ int setSec (Security sec, const char *phrase);
+
+ const char* getIPAddress (void);
+ const char* getMACAddress (void);
+
+ // --------- WizFi250_msg.cpp ---------
+ void recvData ( char c );
+ int parseMessage ();
+ void msgOk (const char *buf);
+ void msgError (const char *buf);
+ void msgConnect (const char *buf);
+ void msgDisconnect (const char *buf);
+ void msgListen (const char *buf);
+
+ void resMacAddress (const char *buf);
+ void resWJOIN (const char *buf);
+ void resConnect (const char *buf);
+ void resSSEND (const char *buf);
+ void resFDNS (const char *buf);
+ void resSMGMT (const char *buf);
+ void resWSTATUS (const char *buf);
+
+
+ // --------- WizFi250_hal.cpp ---------
+ void setReset (bool flg);
+ void isrUart ();
+ int getUart ();
+ void putUart (char c);
+ void setRts (bool flg);
+ int lockUart (int ms);
+ void unlockUart ();
+ void initUart (PinName cts, PinName rts, PinName alarm, int baud);
+
+
+ // --------- WizFi2550_util.cpp ---------
+ int x2i (char c);
+ int i2x (int i);
+
+ // --------- WizFi250_ifc.cpp (For NetworkSocketAPI) ---------
+ /**
+ * Startup the WizFi250
+ *
+ * @return true only if WizFi250 was setup correctly
+ */
+ bool startup(void);
+
+ /**
+ * Reset WizFi250
+ *
+ * @return true only if WizFi250 resets successfully
+ */
+ bool reset(void);
+
+ /**
+ * Disconnect WizFi250 from AP
+ *
+ * @return true only if WizFi250 is disconnected successfully
+ */
+ bool disconnect(void);
+
+ /**
+ * Check if WizFi250 is conenected
+ *
+ * @return true only if the chip has an IP address
+ */
+ bool isConnected(void);
+
+};
+
+#endif /* WIZFI250_H_ */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/WizFi250/WizFi250_at.cpp Tue Aug 30 00:05:24 2016 +0000
@@ -0,0 +1,345 @@
+/*
+ * Copyright (C) 2013 gsfan, MIT License
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute,
+ * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies or
+ * substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+ * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+/* Copyright (C) 2014 Wiznet, MIT License
+ * port to the Wiznet Module WizFi250
+ */
+
+
+#include "WizFi250.h"
+
+
+
+void WizFi250::clearFlags()
+{
+ _state.ok = false;
+ _state.failure = false;
+ _state.res = RES_NULL;
+ _state.n = 0;
+}
+
+
+int WizFi250::sendCommand(const char * cmd, Response res, int timeout, int opt)
+{
+ unsigned int i;
+ Timer t;
+
+ if (lockUart(timeout)) return -1;
+
+ clearFlags();
+ _state.res = res;
+
+ WIZ_INFO("%s", cmd)
+ for (i=0; i< strlen(cmd); i++)
+ {
+ putUart(cmd[i]);
+ }
+
+ if(opt == 1)
+ {
+ putUart('\r');
+ }
+ else if(opt == 2)
+ {
+ putUart('\r');
+ putUart('\n');
+ }
+ unlockUart();
+
+ if(timeout)
+ {
+ t.start();
+ for(;;)
+ {
+ if (_state.ok && _state.res == RES_NULL){
+ break;
+ }
+ if (_state.failure || t.read_ms() > timeout)
+ {
+ WIZ_WARN("failure of timeout[%d]ms\r\n",t.read_ms());
+ _state.res = RES_NULL;
+ t.stop();
+ return -1;
+ }
+ }
+
+ t.stop();
+ }
+
+ WIZ_INFO("[OK]\r\n");
+ _state.res = RES_NULL;
+
+ return 0;
+}
+
+int WizFi250::cmdAT()
+{
+ int resp;
+
+ resp = sendCommand("AT");
+
+ return resp;
+}
+
+int WizFi250::cmdMECHO(bool flg)
+{
+ int status;
+ char cmd[CFG_CMD_SIZE];
+
+ sprintf(cmd,"AT+MECHO=%d",flg);
+ status = sendCommand(cmd);
+
+ return status;
+}
+
+int WizFi250::cmdUSET(int baud, char *flow)
+{
+ int status;
+ char cmd[CFG_CMD_SIZE];
+
+ sprintf(cmd,"AT+USET=%d,N,8,1,%s",baud, flow);
+ status = sendCommand(cmd);
+
+ if(status == 0)
+ {
+ wait(1);
+ _state.buf->flush();
+ }
+
+ return status;
+}
+
+int WizFi250::cmdMMAC(const char *mac)
+{
+ int resp;
+ char cmd[CFG_CMD_SIZE];
+
+ if (mac)
+ {
+ sprintf(cmd, "AT+MMAC=%s",mac);
+ resp = sendCommand(cmd);
+ }
+ else
+ {
+ sprintf(cmd, "AT+MMAC=?");
+ resp = sendCommand(cmd, RES_MACADDRESS);
+ }
+
+ return resp;
+}
+
+int WizFi250::cmdWSET(WiFiMode mode, const char *ssid, const char *bssid, int channel)
+{
+ char cmd[CFG_CMD_SIZE];
+
+ if(*bssid == NULL)
+ {
+ sprintf(cmd, "AT+WSET=%d,%s",mode, ssid);
+ }
+ else
+ {
+ sprintf(cmd, "AT+WSET=%d,%s,%s,%d",mode, ssid, bssid, channel);
+ }
+
+ return sendCommand(cmd);
+}
+
+int WizFi250::cmdWANT(AntennaMode mode)
+{
+ char cmd[CFG_CMD_SIZE];
+ sprintf(cmd, "AT+WANT=%d",mode);
+
+ return sendCommand(cmd);
+}
+
+int WizFi250::cmdWNET(bool is_dhcp)
+{
+ char cmd[CFG_CMD_SIZE];
+
+ if(is_dhcp == true)
+ {
+ sprintf(cmd, "AT+WNET=1");
+ }
+ else
+ {
+ sprintf(cmd, "AT+WNET=0,%s,%s,%s",_state.ip,_state.netmask,_state.gateway);
+ }
+
+ return sendCommand(cmd);
+}
+
+int WizFi250::cmdWSEC(WiFiMode mode, const char *key, const char *sec)
+{
+ char cmd[CFG_CMD_SIZE];
+
+ if(*sec == NULL)
+ {
+ sprintf(cmd, "AT+WSEC=%d,,%s",mode, key);
+ }
+ else
+ {
+ sprintf(cmd, "AT+WSEC=%d,%s,%s",mode, sec, key);
+ }
+
+ return sendCommand(cmd);
+}
+
+int WizFi250::cmdWJOIN()
+{
+ //if( sendCommand("AT+WJOIN", RES_WJOIN, CFG_JOIN_TIMEOUT) )
+ if( sendCommand("AT+WJOIN", RES_NULL, CFG_JOIN_TIMEOUT) )
+ {
+ WIZ_ERR("cmdWJOIN");
+ return -1;
+ }
+
+ if( cmdWSTATUS() )
+ return -1;
+
+ WIZ_INFO("WizFi250 is successfully join to AP");
+
+ return 0;
+}
+
+int WizFi250::cmdWLEAVE()
+{
+ return sendCommand("AT+WLEAVE");
+}
+
+
+int WizFi250::cmdWSTATUS()
+{
+ if( sendCommand("AT+WSTATUS", RES_WSTATUS, DEFAULT_WAIT_RESP_TIMEOUT) )
+ {
+ WIZ_ERR("cmdWSTATUS");
+ return -1;
+ }
+
+ WIZ_INFO("IP : %s", _state.ip);
+ WIZ_INFO("Gateway : %s", _state.gateway);
+
+ return 0;
+}
+
+int WizFi250::cmdSCON ( const char *openType, const char *socketType, int localPort, const char *dataMode)
+{
+ char cmd[CFG_CMD_SIZE];
+
+ sprintf(cmd,"AT+SCON=%s,%s,,,%d,%s",openType, socketType, localPort, dataMode);
+ return sendCommand(cmd);
+}
+
+int WizFi250::cmdSCON ( const char *openType, const char *socketType, const char *remoteIp, int remotePort, int localPort, const char *dataMode)
+{
+ int resp;
+ char cmd[CFG_CMD_SIZE];
+
+ if(localPort == 0)
+ sprintf(cmd,"AT+SCON=%s,%s,%s,%d,%s,%s",openType, socketType, remoteIp, remotePort, "", dataMode);
+ else
+ sprintf(cmd,"AT+SCON=%s,%s,%s,%d,%d,%s",openType, socketType, remoteIp, remotePort, localPort, dataMode);
+
+ resp = sendCommand(cmd, RES_CONNECT, 30000 );
+
+ return resp;
+}
+
+int WizFi250::cmdSSEND ( const char *data, int cid, int sendSize, const char *remoteIp, int remotePort, int Timeout )
+{
+ int i, resp;
+ Timer t;
+ char cmd[CFG_CMD_SIZE];
+
+ if (lockUart(Timeout)) return -1;
+
+ clearFlags();
+ if(remoteIp == NULL)
+ {
+ sprintf(cmd,"AT+SSEND=%d,,,%d",cid, sendSize);
+ }
+ else
+ {
+ sprintf(cmd,"AT+SSEND=%d,%s,%d,%d",cid, remoteIp, remotePort, sendSize);
+ }
+
+ _con[cid].send_length = sendSize;
+
+ resp = sendCommand(cmd, RES_SSEND, 2000, 1);
+
+ unlockUart();
+ if(resp){
+ WIZ_DBG("Fail cmdSSEND")
+ return -1;
+ }
+
+ for(i=0; i<sendSize; i++)
+ {
+ putUart(data[i]);
+ }
+ unlockUart();
+
+ if(Timeout)
+ {
+ t.start();
+ for(;;)
+ {
+ if (_state.ok) break;
+ if (_state.failure || t.read_ms() > Timeout)
+ {
+ WIZ_WARN("failure or timeout\r\n");
+ return -1;
+ }
+ }
+ t.stop();
+ }
+
+ wait(0.05);
+ WIZ_INFO("%s\r\n",data);
+
+ return i;
+}
+
+
+int WizFi250::cmdCLOSE ( int cid )
+{
+ char cmd[CFG_CMD_SIZE];
+
+ sprintf(cmd,"AT+SMGMT=%d",cid);
+ return sendCommand(cmd);
+}
+
+
+int WizFi250::cmdFDNS (const char *host)
+{
+ char cmd[CFG_CMD_SIZE];
+ int resp;
+
+ sprintf(cmd,"AT+FDNS=%s,3000",host);
+ resp = sendCommand(cmd, RES_FDNS);
+
+ WIZ_DBG("%s",_state.resolv);
+ return resp;
+}
+
+int WizFi250::cmdSMGMT ( int cid )
+{
+ int resp;
+
+ resp = sendCommand("AT+SMGMT=?", RES_SMGMT);
+ return resp;
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/WizFi250/WizFi250_conf.h Tue Aug 30 00:05:24 2016 +0000 @@ -0,0 +1,38 @@ +/* Copyright (C) 2013 gsfan, MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +/* Copyright (C) 2014 Wiznet, MIT License + * port to the Wiznet Module WizFi250 + */ + +#ifndef WIZFI250_CONF_H_ +#define WIZFI250_CONF_H_ + + +#define CFG_TRYJOIN 3 + + +#define DEFAULT_WAIT_RESP_TIMEOUT 2000 // ms +#define CFG_JOIN_TIMEOUT 60000 // ms +#define CFG_CMD_SIZE 128 + +#define CFG_DATA_SIZE BUF_SIZE + +#define CFG_DEFAULT_MAC "00:08:DC:00:00:00" + + +#endif /* WIZFI250_CONF_H_ */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/WizFi250/WizFi250_hal.cpp Tue Aug 30 00:05:24 2016 +0000
@@ -0,0 +1,155 @@
+/*
+ * Copyright (C) 2013 gsfan, MIT License
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute,
+ * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies or
+ * substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+ * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+/* Copyright (C) 2014 Wiznet, MIT License
+ * port to the Wiznet Module WizFi250
+ */
+
+#include "WizFi250.h"
+
+void WizFi250::setReset(bool flg)
+{
+ if( flg )
+ {
+ // low
+ _reset.output();
+ _reset = 0;
+ }
+ else
+ {
+ // high z
+ _reset.input();
+ _reset.mode(PullNone);
+ }
+}
+
+void WizFi250::isrUart()
+{
+ char c;
+
+ c = getUart();
+
+ recvData(c);
+ //S_UartPutc(c);
+}
+
+int WizFi250::getUart()
+{
+ return _wizfi.getc();
+}
+
+void WizFi250::putUart (char c)
+{
+ _wizfi.putc(c);
+}
+
+void WizFi250::setRts (bool flg)
+{
+ if (flg)
+ {
+ if(_flow == 2)
+ {
+ if(_rts)
+ {
+ _rts->write(0); // low
+ }
+ }
+ }
+ else
+ {
+ if(_flow == 2)
+ {
+ if(_rts)
+ {
+ _rts->write(1); // high
+ }
+ }
+ }
+}
+
+int WizFi250::lockUart (int ms)
+{
+ Timer t;
+
+ if(_state.mode != MODE_COMMAND)
+ {
+ t.start();
+ while(_state.mode != MODE_COMMAND)
+ {
+ if(t.read_ms() >= ms)
+ {
+ WIZ_WARN("lock timeout (%d)\r\n", _state.mode);
+ return -1;
+ }
+ }
+ }
+
+#ifdef CFG_ENABLE_RTOS
+ if (_mutexUart.lock(ms) != osOK) return -1;
+#endif
+
+ if(_flow == 2)
+ {
+ if(_cts && _cts->read())
+ {
+ // CTS check
+ t.start();
+ while (_cts->read())
+ {
+ if(t.read_ms() >= ms)
+ {
+ WIZ_DBG("cts timeout\r\n");
+ return -1;
+ }
+ }
+ }
+ }
+
+ setRts(false); // blcok
+ return 0;
+}
+
+void WizFi250::unlockUart()
+{
+ setRts(true); // release
+#ifdef CFG_ENABLE_RTOS
+ _mutexUart.unlock();
+#endif
+}
+
+void WizFi250::initUart (PinName cts, PinName rts, PinName alarm, int baud)
+{
+ _baud = baud;
+ if (_baud) _wizfi.baud(_baud);
+
+ _wizfi.attach(this, &WizFi250::isrUart, Serial::RxIrq);
+
+ _cts = NULL;
+ _rts = NULL;
+ _flow = 0;
+
+ if(cts != NC)
+ {
+ _cts = new DigitalIn(cts);
+ }
+ if(rts != NC)
+ {
+ _rts = new DigitalOut(rts);
+ _flow = 2;
+ }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/WizFi250/WizFi250_ifc.cpp Tue Aug 30 00:05:24 2016 +0000
@@ -0,0 +1,46 @@
+/* WizFi250
+ * Copyright (c) 2015 ARM Limited
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include "WizFi250.h"
+
+bool WizFi250::startup(void)
+{
+ reset();
+
+ cmdAT();
+ if( cmdMECHO(false) == -1) return false;
+ //if( cmdMECHO(true) == -1) return false;
+ if(_cts != NULL && _rts != NULL)
+ {
+ if( cmdUSET(_baud,"HW") == -1) return false;
+ }
+ else
+ {
+ if( cmdUSET(_baud,"N") == -1) return false;
+ }
+ wait_ms(1000);
+ if( cmdAT() == -1) return false;
+
+ return true;
+}
+
+bool WizFi250::reset(void)
+{
+ _reset = 0;
+ wait_ms(500);
+ _reset = 1;
+
+ return true;
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/WizFi250/WizFi250_msg.cpp Tue Aug 30 00:05:24 2016 +0000
@@ -0,0 +1,431 @@
+/*
+/* Copyright (C) 2013 gsfan, MIT License
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute,
+ * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies or
+ * substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+ * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+/* Copyright (C) 2014 Wiznet, MIT License
+ * port to the Wiznet Module WizFi250
+ */
+
+#include "WizFi250.h"
+
+#ifdef CFG_ENABLE_RTOS
+#undef WIZ_DBG
+#define WIZ_DBG(x, ...)
+#endif
+
+// This function is operating in ISR. So you can't use debug message.
+void WizFi250::recvData ( char c )
+{
+ static int cid, sub, len, count;
+
+ switch(_state.mode)
+ {
+ case MODE_COMMAND:
+ switch(c)
+ {
+ case 0:
+ case 0x0a: // LF
+ case 0x0d: // CR
+ break;
+
+ case '{':
+ _state.buf->flush();
+ _state.mode = MODE_DATA_RX;
+ sub = 0;
+ break;
+
+ default:
+ _state.buf->flush();
+ _state.buf->queue(c);
+ _state.mode = MODE_CMDRESP;
+ break;
+ }
+ break;
+
+ case MODE_CMDRESP:
+ switch(c)
+ {
+ case 0:
+ break;
+ case 0x0a: // LF
+ break;
+ case 0x0d: // CR
+ if (_flow == 2) setRts(false); // block
+ _state.mode = MODE_COMMAND;
+ parseMessage();
+ if (_flow == 2) setRts(true); // release
+ break;
+ default:
+ _state.buf->queue(c);
+ break;
+ }
+ break;
+
+ case MODE_DATA_RX:
+ switch(sub)
+ {
+ case 0:
+ // cid
+ if( (c >= '0') && (c <= '9') )
+ {
+ cid = x2i(c);
+ }
+ else if ( c == ',' )
+ {
+ sub++;
+ count = 0;
+ len = 0;
+ }
+ else
+ {
+ _state.mode = MODE_COMMAND;
+ }
+ break;
+
+ case 1:
+ // ip
+ if ((c >= '0' && c <= '9') || c == '.')
+ {
+ _con[cid].ip[count] = c;
+ count++;
+ }
+ else if( c == ',' )
+ {
+ _con[cid].ip[count] = '\0';
+ _con[cid].port = 0;
+ sub++;
+ }
+ else
+ {
+ _state.mode = MODE_COMMAND;
+ }
+ break;
+
+ case 2:
+ // port
+ if ( c >= '0' && c <= '9' )
+ {
+ _con[cid].port = (_con[cid].port * 10) + ( c - '0' );
+ }
+ else if( c == ',')
+ {
+ sub++;
+ count = 0;
+ }
+ else
+ {
+ _state.mode = MODE_COMMAND;
+ }
+ break;
+
+ case 3:
+ // data length
+ if ( c >= '0' && c <= '9' )
+ {
+ //_con[cid].recv_length = (_con[cid].recv_length * 10) + (c - '0');
+ len = (len * 10) + (c - '0');
+ }
+ else if( c == '}' )
+ {
+ sub++;
+ count = 0;
+ _con[cid].recv_length = len;
+ }
+ else
+ {
+ _state.mode = MODE_COMMAND;
+ }
+ break;
+
+ default:
+ if(_con[cid].buf != NULL)
+ {
+ _con[cid].buf->queue(c);
+ if(_con[cid].buf->available() > CFG_DATA_SIZE - 16 )
+ {
+ setRts(false); // blcok
+ _con[cid].received = true;
+ WIZ_WARN("buf full");
+ }
+ }
+ _con[cid].recv_length--;
+ if(_con[cid].recv_length == 0)
+ {
+ //WIZ_DBG("recv cid: %d, count : %d, len : %d",cid, count, len);
+ _con[cid].received = true;
+ _state.mode = MODE_COMMAND;
+ }
+ break;
+ }
+ break;
+ }
+}
+
+
+#define MSG_TABLE_NUM 6
+#define RES_TABLE_NUM 7
+int WizFi250::parseMessage () {
+ int i;
+ char buf[128];
+
+ static const struct MSG_TABLE {
+ const char msg[24];
+ void (WizFi250::*func)(const char *);
+ } msg_table[MSG_TABLE_NUM] = {
+ {"[OK]", &WizFi250::msgOk},
+ {"[ERROR]", &WizFi250::msgError},
+ {"[ERROR:INVALIDINPUT]", &WizFi250::msgError},
+ {"[CONNECT ", &WizFi250::msgConnect},
+ {"[DISCONNECT ", &WizFi250::msgDisconnect},
+ {"[LISTEN ", &WizFi250::msgListen},
+ };
+ static const struct RES_TABLE{
+ const Response res;
+ void (WizFi250::*func)(const char *);
+ }res_table[RES_TABLE_NUM]={
+ {RES_NULL, NULL},
+ {RES_MACADDRESS, &WizFi250::resMacAddress},
+// {RES_WJOIN, &WizFi250::resWJOIN},
+ {RES_CONNECT, &WizFi250::resConnect},
+ {RES_SSEND, &WizFi250::resSSEND},
+ {RES_FDNS, &WizFi250::resFDNS},
+ {RES_SMGMT, &WizFi250::resSMGMT},
+ {RES_WSTATUS, &WizFi250::resWSTATUS},
+ };
+
+
+ for( i=0; i<sizeof(buf); i++ )
+ {
+ if( _state.buf->dequeue(&buf[i]) == false ) break;
+ }
+
+ buf[i] = '\0';
+ //strncpy(_state.dbgRespBuf, buf, sizeof(buf) );
+ //WIZ_DBG("%s\r\n",_state.dbgRespBuf);
+
+ if(_state.res != RES_NULL)
+ {
+ for( i=0; i<RES_TABLE_NUM; i++)
+ {
+ if(res_table[i].res == _state.res)
+ {
+ //WIZ_DBG("parse res %d '%s'\r\n", i, buf);
+ if(res_table[i].func != NULL)
+ {
+ (this->*(res_table[i].func))(buf);
+ }
+
+ if(res_table[i].res == RES_CONNECT && _state.n < 2)
+ return -1;
+ }
+ }
+ }
+
+ for( i=0; i<MSG_TABLE_NUM; i++)
+ {
+ if( strncmp(buf, msg_table[i].msg, strlen(msg_table[i].msg)) == 0 )
+ {
+ //WIZ_DBG("parse msg '%s'\r\n", buf);
+ if(msg_table[i].func != NULL)
+ {
+ (this->*(msg_table[i].func))(buf);
+ }
+ return 0;
+ }
+ }
+
+ return -1;
+}
+
+
+void WizFi250::msgOk (const char *buf)
+{
+ _state.ok = true;
+}
+
+void WizFi250::msgError (const char *buf)
+{
+ _state.failure = true;
+}
+
+void WizFi250::msgConnect (const char *buf)
+{
+ int cid;
+
+ if (buf[9] < '0' || buf[9] > '8' || buf[10] != ']') return;
+
+ cid = x2i(buf[9]);
+
+ initCon(cid, true);
+ _state.cid = cid;
+ _con[cid].accept = true;
+ _con[cid].parent = cid;
+}
+
+void WizFi250::msgDisconnect (const char *buf)
+{
+ int cid;
+
+ if(buf[12] < '0' || buf[12] > '8' || buf[13] != ']') return;
+
+ cid = x2i(buf[12]);
+ _con[cid].connected = false;
+}
+
+void WizFi250::msgListen (const char *buf)
+{
+ int cid;
+
+ if(buf[8] < '0' || buf[8] > '8' || buf[9] != ']') return;
+
+ cid = x2i(buf[8]);
+ _state.cid = cid;
+}
+
+void WizFi250::resMacAddress (const char *buf)
+{
+ if( buf[2] == ':' && buf[5] == ':')
+ {
+ strncpy(_state.mac, buf, sizeof(_state.mac));
+ _state.mac[17] = 0;
+ _state.res = RES_NULL;
+
+ if(strncmp(_state.mac,CFG_DEFAULT_MAC,sizeof(CFG_DEFAULT_MAC)) == 0)
+ _state.ok = false;
+ _state.ok = true;
+ }
+}
+
+void WizFi250::resConnect (const char *buf)
+{
+ int cid;
+
+ if (buf[0] == '[' && buf[1] == 'O' && buf[2] == 'K' && buf[3] == ']')
+ {
+ _state.n++;
+ }
+ else if( buf[0] == '[' && buf[1] == 'C' && buf[2] == 'O' && buf[3] == 'N' &&
+ buf[4] == 'N' && buf[5] == 'E' && buf[6] == 'C' && buf[7] == 'T')
+ {
+ cid = x2i(buf[9]);
+ _state.cid = cid;
+ _state.n++;
+ }
+
+ if(_state.n >= 2)
+ {
+ _state.res = RES_NULL;
+ _state.ok = true;
+ }
+}
+
+void WizFi250::resSSEND (const char *buf)
+{
+ if(_state.cid != -1)
+ {
+ _state.res = RES_NULL;
+ _state.ok = true;
+ }
+}
+
+void WizFi250::resFDNS (const char *buf)
+{
+ int i;
+
+ for(i=0; i<strlen(buf); i++)
+ {
+ if( (buf[i] < '0' || buf[i] > '9') && buf[i] != '.' )
+ {
+ return;
+ }
+ }
+
+ strncpy(_state.resolv, buf, sizeof(_state.resolv));
+ _state.res = RES_NULL;
+}
+
+void WizFi250::resSMGMT (const char *buf)
+{
+ int cid, i;
+ char *c;
+
+ if( (buf[0] < '0' || buf[0] > '8') ) return;
+
+ cid = x2i(buf[0]);
+ if( cid != _state.cid ) return;
+
+ // IP
+ c = (char*)(buf+6);
+ for( i=0; i<16; i++ )
+ {
+ if( *(c+i) == ':')
+ {
+ _con[cid].ip[i] = '\0';
+ i++;
+ break;
+ }
+ if( ( *(c+i) < '0' || *(c+i) > '9') && *(c+i) != '.' ) return;
+ _con[cid].ip[i] = *(c+i);
+ }
+
+ // Port
+ c = (c+i);
+ _con[cid].port = 0;
+ for( i=0; i<5; i++ )
+ {
+ if( *(c+i) == '/') break;
+ if( *(c+i) < '0' || *(c+i) > '9' ) return;
+
+ _con[cid].port = (_con[cid].port * 10) + ( *(c+i) - '0' );
+ }
+
+ _state.res = RES_NULL;
+}
+
+void WizFi250::resWSTATUS (const char *buf)
+{
+ int idx=0,sep_cnt=0;
+ int ip_idx=0,gw_idx=0;
+
+ if(_state.n == 0)
+ {
+ _state.n++;
+ }
+ else if(_state.n == 1)
+ {
+ for(idx=0;buf[idx]!='\r';idx++)
+ {
+ if(buf[idx] =='/')
+ {
+ sep_cnt++;
+ continue;
+ }
+
+ if( sep_cnt == 2) // IP Address
+ {
+ _state.ip[ip_idx++] = buf[idx];
+ }
+ else if(sep_cnt == 3)
+ {
+ _state.gateway[gw_idx++] = buf[idx];
+ }
+ }
+ _state.ip[ip_idx] = '\0';
+ _state.gateway[gw_idx] = '\0';
+ _state.res = RES_NULL;
+ }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/WizFi250/WizFi250_sock.cpp Tue Aug 30 00:05:24 2016 +0000
@@ -0,0 +1,243 @@
+/*
+/* Copyright (C) 2013 gsfan, MIT License
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute,
+ * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies or
+ * substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+ * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+/* Copyright (C) 2014 Wiznet, MIT License
+ * port to the Wiznet Module WizFi250
+ */
+
+#include "WizFi250.h"
+
+
+int WizFi250::getHostByName(const char * host, char *ip)
+{
+ int i, flg = 0;
+
+ if(!isAssociated() || _state.status != STAT_READY) return -1;
+
+ for(i=0; i<strlen(host); i++)
+ {
+ if( (host[i] < '0' || host[i] > '9') && host[i] != '.')
+ {
+ flg = 1;
+ break;
+ }
+ }
+ if (!flg)
+ {
+ strncpy(ip, host, 16);
+ return 0;
+ }
+
+ if ( cmdFDNS(host) )
+ {
+ wait_ms(1000);
+ if( cmdFDNS(host) ) return -1;
+ }
+ strncpy(ip, _state.resolv, 16);
+ return 0;
+}
+
+int WizFi250::open(Protocol proto, const char *ip, int remotePort, int localPort, void(*func)(int))
+{
+ int cid;
+
+ if (!isAssociated() || _state.status != STAT_READY) return -1;
+
+ _state.cid = -1;
+
+ if (proto == PROTO_TCP)
+ {
+ if( cmdSCON( "O","TCN",ip, remotePort, localPort, "0" ) ) return -1;
+ }
+ else if(proto == PROTO_UDP)
+ {
+ if( cmdSCON( "O","UCN",ip, remotePort, localPort, "0" ) ) return -1;
+ }
+ if(_state.cid < 0) return -1;
+
+ initCon(_state.cid, true);
+ cid = _state.cid;
+ _con[cid].protocol = proto;
+ _con[cid].type = TYPE_CLIENT;
+ _con[cid].func = func;
+ return cid;
+}
+
+int WizFi250::listen (Protocol proto, int port, void(*func)(int))
+{
+ int cid;
+
+ if(!isAssociated() || _state.status != STAT_READY) return -1;
+
+ _state.cid = -1;
+
+ if(proto == PROTO_TCP)
+ {
+ if( sendCommand("AT+MEVTMSG=1") ) return -1;
+ if( cmdSCON("O","TSN",port) ) return -1;
+ }
+ else
+ {
+ if( cmdSCON("O","USN",port) ) return -1;
+ }
+
+ if (_state.cid < 0) return -1;
+ cid = _state.cid;
+ _con[cid].protocol = proto;
+ _con[cid].type = TYPE_SERVER;
+ _con[cid].func = func;
+
+ return cid;
+}
+
+int WizFi250::close (int cid)
+{
+// if(!isConnected(cid)) return -1;
+
+ _con[cid].connected = false;
+ return cmdCLOSE(cid);
+}
+
+
+void WizFi250::initCon ( int cid, bool connected )
+{
+ _con[cid].parent = -1; // It will be delete because It is not need
+ _con[cid].func = NULL;
+ _con[cid].accept = false;
+
+//#ifndef CFG_ENABLE_RTOS
+ if ( _con[cid].buf == NULL )
+ {
+ _con[cid].buf = new CircBuffer<char>(CFG_DATA_SIZE);
+ if ( _con[cid].buf == NULL ) error("Can't allocate memory");
+ }
+//#endif
+ if ( _con[cid].buf != NULL )
+ {
+ _con[cid].buf->flush();
+ }
+ _con[cid].connected = connected;
+}
+
+int WizFi250::send(int cid, const char *buf, int len)
+{
+ if(!isConnected(cid)) return -1;
+
+ if((_con[cid].protocol == PROTO_TCP) ||
+ (_con[cid].protocol == PROTO_UDP && _con[cid].type == TYPE_CLIENT) )
+ {
+// if ( len > CFG_DATA_SIZE) len = CFG_DATA_SIZE;
+ return cmdSSEND(buf,cid,len);
+ }
+ else
+ {
+ return -1;
+ }
+}
+
+int WizFi250::sendto (int cid, const char *buf, int len, const char *ip, int port)
+{
+ if(!isConnected(cid)) return -1;
+
+ if((_con[cid].protocol == PROTO_UDP && _con[cid].type == TYPE_SERVER))
+ {
+ if ( len > CFG_DATA_SIZE ) len = CFG_DATA_SIZE;
+ return cmdSSEND(buf,cid,len,ip,port);
+ }
+ else
+ {
+ return -1;
+ }
+}
+
+int WizFi250::recv (int cid, char *buf, int len)
+{
+ int i;
+
+ if (!isConnected(cid)) return -1;
+
+ if (_con[cid].buf == NULL ) return 0;
+ while (!_con[cid].received && _state.mode != MODE_COMMAND);
+ _con[cid].received = false;
+ for(i=0; i<len; i++)
+ {
+ if(_con[cid].buf->dequeue(&buf[i]) == false) break;
+ }
+ setRts(true); // release
+ return i;
+}
+
+int WizFi250::recvfrom (int cid, char *buf, int len, char *ip, int *port)
+{
+ int i;
+
+ if (!isConnected(cid)) return -1;
+
+ if (_con[cid].buf == NULL) return 0;
+
+ while (!_con[cid].received && _state.mode != MODE_COMMAND);
+
+ _con[cid].received = false;
+ for(i=0; i<len; i++)
+ {
+ if( _con[cid].buf->dequeue(&buf[i]) == false ) break;
+ }
+ //buf[i] = '\0';
+ strncpy(ip, _con[cid].ip, 16);
+ *port = _con[cid].port;
+ setRts(true); // release
+
+ return i;
+}
+
+int WizFi250::readable (int cid)
+{
+ if (!isConnected(cid)) return -1;
+
+ if(_con[cid].buf == NULL) return -1;
+ return _con[cid].buf->available();
+}
+
+bool WizFi250::isConnected (int cid)
+{
+ if ( cid < 0 || cid >=8 ) return false;
+
+ return _con[cid].connected;
+}
+
+int WizFi250::accept (int cid)
+{
+ if(!isConnected(cid)) return -1;
+
+ if(_con[cid].connected && _con[cid].accept)
+ {
+ _con[cid].accept = false;
+ return cid;
+ }
+
+ return -1;
+}
+
+int WizFi250::getRemote(int cid, char **ip, int *port)
+{
+ if (!isConnected(cid)) return -1;
+
+ *ip = _con[cid].ip;
+ *port = _con[cid].port;
+ return 0;
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/WizFi250/WizFi250_util.cpp Tue Aug 30 00:05:24 2016 +0000
@@ -0,0 +1,55 @@
+/*
+ * Copyright (C) 2013 gsfan, MIT License
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute,
+ * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies or
+ * substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+ * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+/* Copyright (C) 2014 Wiznet, MIT License
+ * port to the Wiznet Module WizFi250
+ */
+
+#include "WizFi250.h"
+
+int WizFi250::x2i(char c)
+{
+ if ( c >= '0' && c <= '9')
+ {
+ return c - '0';
+ }
+ else if ( c >= 'A' && c <= 'F')
+ {
+ return c - 'A' + 10;
+ }
+ else if ( c >= 'a' && c <= 'f')
+ {
+ return c - 'a' + 10;
+ }
+
+ return 0;
+}
+
+int WizFi250::i2x(int i)
+{
+ if ( i >= 0 && i <= 9 )
+ {
+ return i + '0';
+ }
+ else if ( i >= 10 && i <= 15 )
+ {
+ return i - 10 + 'A';
+ }
+
+ return 0;
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/WizFi250Interface.cpp Tue Aug 30 00:05:24 2016 +0000
@@ -0,0 +1,214 @@
+/* WizFi250 implementation of NetworkInterfaceAPI
+ * Copyright (c) 2015 ARM Limited
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+#include "WizFi250Interface.h"
+
+// Various timeouts for different WizFi250 operations
+#define WizFi250_CONNECT_TIMEOUT 15000
+#define WizFi250_SEND_TIMEOUT 500
+#define WizFi250_RECV_TIMEOUT 0
+#define WizFi250_MISC_TIMEOUT 500
+
+#define WizFi250_DELAY_MS 300
+
+
+// WizFi250Interface implementation
+WizFi250Interface::WizFi250Interface(PinName tx, PinName rx, PinName cts, PinName rts, PinName reset, PinName alarm, int baud)
+ : _wizfi250(tx, rx, cts, rts, reset, alarm, baud)
+{
+ memset(_ids, 0, sizeof(_ids));
+}
+
+int WizFi250Interface::connect(
+ const char *ssid,
+ const char *pass,
+ nsapi_security_t security)
+{
+ if (!_wizfi250.startup())
+ {
+ return NSAPI_ERROR_DEVICE_ERROR;
+ }
+
+ _wizfi250.setSsid(ssid);
+ _wizfi250.setSec(WizFi250::SEC_AUTO, pass);
+ _wizfi250.setAddress("");
+
+ if( _wizfi250.join(WizFi250::WM_STATION) == -1)
+ {
+ return NSAPI_ERROR_NO_CONNECTION;
+ }
+
+ return 0;
+}
+
+int WizFi250Interface::disconnect()
+{
+ if ( _wizfi250.cmdWLEAVE() == -1 ) return NSAPI_ERROR_DEVICE_ERROR;
+
+ return 0;
+}
+
+const char *WizFi250Interface::get_ip_address()
+{
+ return _wizfi250.getIPAddress();
+}
+
+const char *WizFi250Interface::get_mac_address()
+{
+ return _wizfi250.getMACAddress();
+}
+
+struct wizfi250_socket {
+ int id;
+ nsapi_protocol_t proto;
+ bool connected;
+};
+
+int WizFi250Interface::socket_open(void **handle, nsapi_protocol_t proto)
+{
+ // Look for an unused socket
+
+ /*
+ int id = -1;
+
+ for (int i = 0; i < WIZFI250_SOCKET_COUNT; i++) {
+ if (_ids[i] == false) {
+ id = i;
+ _ids[i] = true;
+ break;
+ }
+ }
+
+ if (id == -1) {
+ return NSAPI_ERROR_NO_SOCKET;
+ }
+ */
+
+ struct wizfi250_socket *socket = new struct wizfi250_socket;
+ if (!socket) {
+ return NSAPI_ERROR_NO_SOCKET;
+ }
+
+ socket->id = -1;
+ socket->proto = proto;
+ socket->connected = false;
+ *handle = socket;
+ return 0;
+}
+
+int WizFi250Interface::socket_close(void *handle)
+{
+ struct wizfi250_socket *socket = (struct wizfi250_socket *)handle;
+ int err = 0;
+
+ if(socket->id == -1){
+ err = NSAPI_ERROR_NO_SOCKET;
+ }
+ else if (_wizfi250.close(socket->id) == -1) {
+ err = NSAPI_ERROR_DEVICE_ERROR;
+ }
+
+ _ids[socket->id] = false;
+ wait_ms(WizFi250_DELAY_MS);
+ delete socket;
+ return err;
+}
+
+int WizFi250Interface::socket_bind(void *handle, const SocketAddress &address)
+{
+ return NSAPI_ERROR_UNSUPPORTED;
+}
+
+int WizFi250Interface::socket_listen(void *handle, int backlog)
+{
+ return NSAPI_ERROR_UNSUPPORTED;
+}
+
+int WizFi250Interface::socket_connect(void *handle, const SocketAddress &addr)
+{
+ int cid=-1;
+ struct wizfi250_socket *socket = (struct wizfi250_socket *)handle;
+
+ WizFi250::Protocol proto = (socket->proto == NSAPI_UDP) ? WizFi250::PROTO_UDP : WizFi250::PROTO_TCP;
+ if((cid = _wizfi250.open(proto, addr.get_ip_address(), addr.get_port())) == -1 )
+ {
+ return NSAPI_ERROR_DEVICE_ERROR;
+ }
+
+ if(cid >= WIZFI250_SOCKET_COUNT)
+ {
+ return NSAPI_ERROR_NO_SOCKET;
+ }
+
+ _ids[cid] = true;
+ socket->id = cid;
+ socket->connected = true;
+ wait_ms(WizFi250_DELAY_MS);
+ return 0;
+}
+
+int WizFi250Interface::socket_accept(void **handle, void *server)
+{
+ return NSAPI_ERROR_UNSUPPORTED;
+}
+
+int WizFi250Interface::socket_send(void *handle, const void *data, unsigned size)
+{
+ struct wizfi250_socket *socket = (struct wizfi250_socket *)handle;
+
+ if ( _wizfi250.send(socket->id, (const char*)data, size) == -1 ) {
+ return NSAPI_ERROR_DEVICE_ERROR;
+ }
+
+ return size;
+}
+
+int WizFi250Interface::socket_recv(void *handle, void *data, unsigned size)
+{
+ struct wizfi250_socket *socket = (struct wizfi250_socket *)handle;
+
+ int32_t recv = _wizfi250.recv(socket->id, (char*)data, size);
+ if (recv < 0) {
+ return NSAPI_ERROR_WOULD_BLOCK;
+ }
+
+ return recv;
+}
+
+int WizFi250Interface::socket_sendto(void *handle, const SocketAddress &addr, const void *data, unsigned size)
+{
+ struct wizfi250_socket *socket = (struct wizfi250_socket *)handle;
+ if (!socket->connected) {
+ int err = socket_connect(socket, addr);
+ if (err < 0) {
+ return err;
+ }
+ }
+
+ return socket_send(socket, data, size);
+}
+
+int WizFi250Interface::socket_recvfrom(void *handle, SocketAddress *addr, void *data, unsigned size)
+{
+ struct wizfi250_socket *socket = (struct wizfi250_socket *)handle;
+ return socket_recv(socket, data, size);
+}
+
+void WizFi250Interface::socket_attach(void *handle, void (*callback)(void *), void *data)
+{
+}
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/WizFi250Interface.h Tue Aug 30 00:05:24 2016 +0000
@@ -0,0 +1,158 @@
+/* WizFi250 implementation of NetworkInterfaceAPI
+ * Copyright (c) 2015 ARM Limited
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef WIZFI250_INTERFACE_H
+#define WIZFI250_INTERFACE_H
+
+#include "WiFiInterface.h"
+#include "WizFi250.h"
+
+#define WIZFI250_SOCKET_COUNT 8
+
+/** WizFi250Interface class
+ * Implementation of the NetworkStack for the WizFi250
+ */
+class WizFi250Interface : public NetworkStack, public WiFiInterface
+{
+public:
+ WizFi250Interface(PinName tx, PinName rx, PinName cts, PinName rts, PinName reset, PinName alarm, int baud=115200 );
+
+ virtual int connect(
+ const char *ssid,
+ const char *pass,
+ nsapi_security_t security = NSAPI_SECURITY_NONE);
+
+ /** Stop the interface
+ * @return 0 on success, negative on failure
+ */
+ virtual int disconnect();
+
+ /** Get the internally stored IP address
+ * @return IP address of the interface or null if not yet connected
+ */
+ virtual const char *get_ip_address();
+
+ /** Get the internally stored MAC address
+ * @return MAC address of the interface
+ */
+ virtual const char *get_mac_address();
+
+protected:
+ /** Open a socket
+ * @param handle Handle in which to store new socket
+ * @param proto Type of socket to open, NSAPI_TCP or NSAPI_UDP
+ * @return 0 on success, negative on failure
+ */
+ virtual int socket_open(void **handle, nsapi_protocol_t proto);
+
+ /** Close the socket
+ * @param handle Socket handle
+ * @return 0 on success, negative on failure
+ * @note On failure, any memory associated with the socket must still
+ * be cleaned up
+ */
+ virtual int socket_close(void *handle);
+
+ /** Bind a server socket to a specific port
+ * @param handle Socket handle
+ * @param address Local address to listen for incoming connections on
+ * @return 0 on success, negative on failure.
+ */
+ virtual int socket_bind(void *handle, const SocketAddress &address);
+
+ /** Start listening for incoming connections
+ * @param handle Socket handle
+ * @param backlog Number of pending connections that can be queued up at any
+ * one time [Default: 1]
+ * @return 0 on success, negative on failure
+ */
+ virtual int socket_listen(void *handle, int backlog);
+
+ /** Connects this TCP socket to the server
+ * @param handle Socket handle
+ * @param address SocketAddress to connect to
+ * @return 0 on success, negative on failure
+ */
+ virtual int socket_connect(void *handle, const SocketAddress &address);
+
+ /** Accept a new connection.
+ * @param handle Handle in which to store new socket
+ * @param server Socket handle to server to accept from
+ * @return 0 on success, negative on failure
+ * @note This call is not-blocking, if this call would block, must
+ * immediately return NSAPI_ERROR_WOULD_WAIT
+ */
+ virtual int socket_accept(void **handle, void *server);
+
+ /** Send data to the remote host
+ * @param handle Socket handle
+ * @param data The buffer to send to the host
+ * @param size The length of the buffer to send
+ * @return Number of written bytes on success, negative on failure
+ * @note This call is not-blocking, if this call would block, must
+ * immediately return NSAPI_ERROR_WOULD_WAIT
+ */
+ virtual int socket_send(void *handle, const void *data, unsigned size);
+
+ /** Receive data from the remote host
+ * @param handle Socket handle
+ * @param data The buffer in which to store the data received from the host
+ * @param size The maximum length of the buffer
+ * @return Number of received bytes on success, negative on failure
+ * @note This call is not-blocking, if this call would block, must
+ * immediately return NSAPI_ERROR_WOULD_WAIT
+ */
+ virtual int socket_recv(void *handle, void *data, unsigned size);
+
+ /** Send a packet to a remote endpoint
+ * @param handle Socket handle
+ * @param address The remote SocketAddress
+ * @param data The packet to be sent
+ * @param size The length of the packet to be sent
+ * @return The number of written bytes on success, negative on failure
+ * @note This call is not-blocking, if this call would block, must
+ * immediately return NSAPI_ERROR_WOULD_WAIT
+ */
+ virtual int socket_sendto(void *handle, const SocketAddress &address, const void *data, unsigned size);
+
+ /** Receive a packet from a remote endpoint
+ * @param handle Socket handle
+ * @param address Destination for the remote SocketAddress or null
+ * @param buffer The buffer for storing the incoming packet data
+ * If a packet is too long to fit in the supplied buffer,
+ * excess bytes are discarded
+ * @param size The length of the buffer
+ * @return The number of received bytes on success, negative on failure
+ * @note This call is not-blocking, if this call would block, must
+ * immediately return NSAPI_ERROR_WOULD_WAIT
+ */
+ virtual int socket_recvfrom(void *handle, SocketAddress *address, void *buffer, unsigned size);
+
+ /** Register a callback on state change of the socket
+ * @param handle Socket handle
+ * @param callback Function to call on state change
+ * @param data Argument to pass to callback
+ * @note Callback may be called in an interrupt context.
+ */
+ virtual void socket_attach(void *handle, void (*callback)(void *), void *data);
+
+private:
+ WizFi250 _wizfi250;
+ bool _ids[WIZFI250_SOCKET_COUNT];
+};
+
+#endif
+
