GainSpan Wi-Fi library see: http://mbed.org/users/gsfan/notebook/gainspan_wifi/
Dependents: GSwifi_httpd GSwifi_websocket GSwifi_tcpclient GSwifi_tcpserver ... more
Fork of GSwifi by
GainSpan Wi-Fi library
The GS1011 is an ultra low power 802.11b wireless module from GainSpan.
see: http://mbed.org/users/gsfan/notebook/gainspan_wifi/
ゲインスパン Wi-Fi モジュール ライブラリ
ゲインスパン社の低電力 Wi-Fiモジュール(無線LAN) GS1011 シリーズ用のライブラリです。
解説: http://mbed.org/users/gsfan/notebook/gainspan_wifi/
Diff: GSwifi_sock.cpp
- Revision:
- 25:f6e5622d2930
- Child:
- 26:b347ee3a1087
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/GSwifi_sock.cpp Mon Feb 11 06:01:46 2013 +0000 @@ -0,0 +1,214 @@ +/* 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. + */ +/** @file + * @brief Gainspan wi-fi module library for mbed + * GS1011MIC, GS1011MIP, GainSpan WiFi Breakout, etc. + */ + +#include "dbg.h" +#include "mbed.h" +#include "GSwifi.h" + + +void GSwifi::newSock (int cid, GSTYPE type, GSPROTOCOL pro, onGsReceiveFunc ponGsReceive) { + _gs_sock[cid].type = type; + _gs_sock[cid].protocol = pro; + _gs_sock[cid].connect = true; + if (_gs_sock[cid].data == NULL) { + _gs_sock[cid].data = new CircBuffer<char>(GS_DATA_SIZE); + } else { + _gs_sock[cid].data->flush(); + } + _gs_sock[cid].lcid = 0; + _gs_sock[cid].received = 0; + _gs_sock[cid].onGsReceive = ponGsReceive; +} + +int GSwifi::open (Host &host, GSPROTOCOL pro, onGsReceiveFunc ponGsReceive) { + char cmd[GS_CMD_SIZE]; + + if (! _connect || _status != GSSTAT_READY) return -1; + if (host.getIp().isNull() || host.getPort() == 0) { + return -1; + } + + if (pro == GSPROT_UDP) { + sprintf(cmd, "AT+NCUDP=%d.%d.%d.%d,%d", host.getIp()[0], host.getIp()[1], host.getIp()[2], host.getIp()[3], host.getPort()); + } else { + sprintf(cmd, "AT+NCTCP=%d.%d.%d.%d,%d", host.getIp()[0], host.getIp()[1], host.getIp()[2], host.getIp()[3], host.getPort()); + } + if (command(cmd, GSRES_CONNECT)) return -1; + + newSock(_cid, GSTYPE_CLIENT, pro, ponGsReceive); + return _cid; +} + +int GSwifi::listen (int port, GSPROTOCOL pro, onGsReceiveFunc ponGsReceive) { + char cmd[GS_CMD_SIZE]; + + if (! _connect || _status != GSSTAT_READY) return -1; + if (port == 0) { + return -1; + } + + if (pro == GSPROT_UDP) { + sprintf(cmd, "AT+NSUDP=%d", port); + } else { + sprintf(cmd, "AT+NSTCP=%d", port); + } + if (command(cmd, GSRES_CONNECT)) return -1; + + newSock(_cid, GSTYPE_SERVER, pro, ponGsReceive); + return _cid; +} + +int GSwifi::close (int cid) { + char cmd[GS_CMD_SIZE]; + + if (! _gs_sock[cid].connect) return -1; + + _gs_sock[cid].connect = false; +// delete _gs_sock[cid].data; +// _gs_sock[cid].data = NULL; + sprintf(cmd, "AT+NCLOSE=%X", cid); + return command(cmd, GSRES_NORMAL); +} + +int GSwifi::send (int cid, const char *buf, int len) { + int i; + Timer timeout; + + if (! _gs_sock[cid].connect) return -1; + + if ((_gs_sock[cid].protocol == GSPROT_TCP) || + (_gs_sock[cid].protocol == GSPROT_UDP && _gs_sock[cid].type == GSTYPE_CLIENT) || + (_gs_sock[cid].protocol == GSPROT_HTTPD)) { + // TCP Client, TCP Server, UDP Client + _gs_ok = 0; + _gs_failure = 0; +#ifdef GS_BULK + _gs.printf("\x1bZ%X%04d", cid, len); + for (i = 0; i < len; i ++) { + _gs_putc(buf[i]); +#ifdef DEBUG_VIEW + DBG("%c", buf[i]); +#endif + } +#else + _gs.printf("\x1bS%X", cid); + for (i = 0; i < len; i ++) { + if (buf[i] >= 0x20 && buf[i] < 0x7f) { + _gs_putc(buf[i]); +#ifdef DEBUG_VIEW + DBG("%c", buf[i]); +#endif + } + } + _gs_putc(0x1b); + _gs_putc('E'); +#endif + } else { + return -1; + } + timeout.start(); + while (!_gs_ok && !_gs_failure && timeout.read_ms() < GS_TIMEOUT); + return _gs_ok == 1 ? 0 : -1; +} + +int GSwifi::send (int cid, const char *buf, int len, Host &host) { + int i; + Timer timeout; + + if (! _gs_sock[cid].connect) return -1; + + if ((_gs_sock[cid].protocol == GSPROT_UDP && _gs_sock[cid].type == GSTYPE_SERVER)) { + // UDP Server + _gs_ok = 0; + _gs_failure = 0; +#ifdef GS_BULK + _gs.printf("\x1bY%X", cid); + _gs.printf("%d.%d.%d.%d:%d:", host.getIp()[0], host.getIp()[1], host.getIp()[2], host.getIp()[3], host.getPort()); + _gs.printf("%04d", len); + for (i = 0; i < len; i ++) { + _gs_putc(buf[i]); +#ifdef DEBUG_VIEW + DBG("%c", buf[i]); +#endif + } +#else + _gs.printf("\x1bU%X", cid); + _gs.printf("%d.%d.%d.%d:%d:", host.getIp()[0], host.getIp()[1], host.getIp()[2], host.getIp()[3], host.getPort()); + for (i = 0; i < len; i ++) { + if (buf[i] >= 0x20 && buf[i] < 0x7f) { + _gs_putc(buf[i]); +#ifdef DEBUG_VIEW + DBG("%c", buf[i]); +#endif + } + } + _gs_putc(0x1b); + _gs_putc('E'); +#endif + } else { + return -1; + } + timeout.start(); + while (!_gs_ok && !_gs_failure && timeout.read_ms() < GS_TIMEOUT); + return _gs_ok == 1 ? 0 : -1; +} + +int GSwifi::recv (int cid, char *buf, int len) { + int i; + Timer timeout; + + if (_gs_sock[cid].data == NULL) return 0; + + timeout.start(); + while (_gs_sock[cid].data->use() == 0) { + if (timeout.read_ms() > GS_TIMEOUT) return 0; + } + timeout.stop(); + + for (i = 0; i < len; i ++) { + if (_gs_sock[cid].data->dequeue(&buf[i]) == false) break; + } + return i; +} + +int GSwifi::recv (int cid, char *buf, int len, Host &host) { + int i; + Timer timeout; + + if (_gs_sock[cid].data == NULL) return 0; + + timeout.start(); + while (_gs_sock[cid].data->use() == 0) { + if (timeout.read_ms() > GS_TIMEOUT) return 0; + } + timeout.stop(); + + for (i = 0; i < len; i ++) { + if (_gs_sock[cid].data->dequeue(&buf[i]) == false) break; + } + host = _from; + return i; +} + +bool GSwifi::isConnected (int cid) { + return _gs_sock[cid].connect; +}