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 gs fan

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/

/media/uploads/gsfan/gs_im_002.jpg /media/uploads/gsfan/gs1011m_2.jpg

ゲインスパン Wi-Fi モジュール ライブラリ

ゲインスパン社の低電力 Wi-Fiモジュール(無線LAN) GS1011 シリーズ用のライブラリです。

解説: http://mbed.org/users/gsfan/notebook/gainspan_wifi/

Committer:
gsfan
Date:
Wed Dec 18 01:29:43 2013 +0000
Revision:
43:0b5e2727e020
Parent:
37:e61ea8267415
fix reconnect

Who changed what in which revision?

UserRevisionLine numberNew contents of line
gsfan 25:f6e5622d2930 1 /* Copyright (C) 2013 gsfan, MIT License
gsfan 25:f6e5622d2930 2 *
gsfan 25:f6e5622d2930 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
gsfan 25:f6e5622d2930 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
gsfan 25:f6e5622d2930 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
gsfan 25:f6e5622d2930 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
gsfan 25:f6e5622d2930 7 * furnished to do so, subject to the following conditions:
gsfan 25:f6e5622d2930 8 *
gsfan 25:f6e5622d2930 9 * The above copyright notice and this permission notice shall be included in all copies or
gsfan 25:f6e5622d2930 10 * substantial portions of the Software.
gsfan 25:f6e5622d2930 11 *
gsfan 25:f6e5622d2930 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
gsfan 25:f6e5622d2930 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
gsfan 25:f6e5622d2930 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
gsfan 25:f6e5622d2930 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
gsfan 25:f6e5622d2930 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
gsfan 25:f6e5622d2930 17 */
gsfan 25:f6e5622d2930 18 /** @file
gsfan 25:f6e5622d2930 19 * @brief Gainspan wi-fi module library for mbed
gsfan 25:f6e5622d2930 20 * GS1011MIC, GS1011MIP, GainSpan WiFi Breakout, etc.
gsfan 25:f6e5622d2930 21 */
gsfan 25:f6e5622d2930 22
gsfan 25:f6e5622d2930 23 #include "dbg.h"
gsfan 25:f6e5622d2930 24 #include "mbed.h"
gsfan 25:f6e5622d2930 25 #include "GSwifi.h"
gsfan 25:f6e5622d2930 26
gsfan 25:f6e5622d2930 27
gsfan 26:b347ee3a1087 28 void GSwifi::newSock (int cid, GSTYPE type, GSPROTOCOL pro) {
gsfan 31:0abdc584823d 29 DBG("newSock %d\r\n", cid);
gsfan 25:f6e5622d2930 30 _gs_sock[cid].type = type;
gsfan 25:f6e5622d2930 31 _gs_sock[cid].protocol = pro;
gsfan 25:f6e5622d2930 32 _gs_sock[cid].connect = true;
gsfan 25:f6e5622d2930 33 if (_gs_sock[cid].data == NULL) {
gsfan 25:f6e5622d2930 34 _gs_sock[cid].data = new CircBuffer<char>(GS_DATA_SIZE);
gsfan 25:f6e5622d2930 35 } else {
gsfan 25:f6e5622d2930 36 _gs_sock[cid].data->flush();
gsfan 25:f6e5622d2930 37 }
gsfan 25:f6e5622d2930 38 _gs_sock[cid].lcid = 0;
gsfan 26:b347ee3a1087 39 _gs_sock[cid].received = false;
gsfan 31:0abdc584823d 40 _gs_sock[cid].onGsReceive.detach();
gsfan 25:f6e5622d2930 41 }
gsfan 25:f6e5622d2930 42
gsfan 36:a70b11e1560f 43 int GSwifi::open (Host &host, GSPROTOCOL pro, int port) {
gsfan 25:f6e5622d2930 44 char cmd[GS_CMD_SIZE];
gsfan 25:f6e5622d2930 45
gsfan 25:f6e5622d2930 46 if (! _connect || _status != GSSTAT_READY) return -1;
gsfan 34:f5f40c92af00 47 if (host.getIp().isNull()) {
gsfan 34:f5f40c92af00 48 if (getHostByName(host)) {
gsfan 34:f5f40c92af00 49 if (getHostByName(host)) return -1;
gsfan 34:f5f40c92af00 50 }
gsfan 34:f5f40c92af00 51 }
gsfan 34:f5f40c92af00 52 if (host.getPort() == 0) {
gsfan 25:f6e5622d2930 53 return -1;
gsfan 25:f6e5622d2930 54 }
gsfan 25:f6e5622d2930 55
gsfan 25:f6e5622d2930 56 if (pro == GSPROT_UDP) {
gsfan 25:f6e5622d2930 57 sprintf(cmd, "AT+NCUDP=%d.%d.%d.%d,%d", host.getIp()[0], host.getIp()[1], host.getIp()[2], host.getIp()[3], host.getPort());
gsfan 36:a70b11e1560f 58 if (port) {
gsfan 36:a70b11e1560f 59 sprintf(&cmd[strlen(cmd)], ",%d", port);
gsfan 36:a70b11e1560f 60 }
gsfan 25:f6e5622d2930 61 } else {
gsfan 25:f6e5622d2930 62 sprintf(cmd, "AT+NCTCP=%d.%d.%d.%d,%d", host.getIp()[0], host.getIp()[1], host.getIp()[2], host.getIp()[3], host.getPort());
gsfan 25:f6e5622d2930 63 }
gsfan 25:f6e5622d2930 64 if (command(cmd, GSRES_CONNECT)) return -1;
gsfan 25:f6e5622d2930 65
gsfan 28:fbba4c58d14c 66 newSock(_cid, GSTYPE_CLIENT, pro);
gsfan 25:f6e5622d2930 67 return _cid;
gsfan 25:f6e5622d2930 68 }
gsfan 25:f6e5622d2930 69
gsfan 28:fbba4c58d14c 70 int GSwifi::listen (int port, GSPROTOCOL pro) {
gsfan 25:f6e5622d2930 71 char cmd[GS_CMD_SIZE];
gsfan 25:f6e5622d2930 72
gsfan 25:f6e5622d2930 73 if (! _connect || _status != GSSTAT_READY) return -1;
gsfan 25:f6e5622d2930 74 if (port == 0) {
gsfan 25:f6e5622d2930 75 return -1;
gsfan 25:f6e5622d2930 76 }
gsfan 25:f6e5622d2930 77
gsfan 25:f6e5622d2930 78 if (pro == GSPROT_UDP) {
gsfan 25:f6e5622d2930 79 sprintf(cmd, "AT+NSUDP=%d", port);
gsfan 25:f6e5622d2930 80 } else {
gsfan 25:f6e5622d2930 81 sprintf(cmd, "AT+NSTCP=%d", port);
gsfan 25:f6e5622d2930 82 }
gsfan 25:f6e5622d2930 83 if (command(cmd, GSRES_CONNECT)) return -1;
gsfan 25:f6e5622d2930 84
gsfan 28:fbba4c58d14c 85 newSock(_cid, GSTYPE_SERVER, pro);
gsfan 25:f6e5622d2930 86 return _cid;
gsfan 25:f6e5622d2930 87 }
gsfan 25:f6e5622d2930 88
gsfan 25:f6e5622d2930 89 int GSwifi::close (int cid) {
gsfan 25:f6e5622d2930 90 char cmd[GS_CMD_SIZE];
gsfan 25:f6e5622d2930 91
gsfan 25:f6e5622d2930 92 if (! _gs_sock[cid].connect) return -1;
gsfan 25:f6e5622d2930 93
gsfan 25:f6e5622d2930 94 _gs_sock[cid].connect = false;
gsfan 25:f6e5622d2930 95 // delete _gs_sock[cid].data;
gsfan 25:f6e5622d2930 96 // _gs_sock[cid].data = NULL;
gsfan 25:f6e5622d2930 97 sprintf(cmd, "AT+NCLOSE=%X", cid);
gsfan 25:f6e5622d2930 98 return command(cmd, GSRES_NORMAL);
gsfan 25:f6e5622d2930 99 }
gsfan 25:f6e5622d2930 100
gsfan 25:f6e5622d2930 101 int GSwifi::send (int cid, const char *buf, int len) {
gsfan 25:f6e5622d2930 102 int i;
gsfan 36:a70b11e1560f 103 char cmd[GS_CMD_SIZE];
gsfan 25:f6e5622d2930 104
gsfan 37:e61ea8267415 105 if ((! _gs_sock[cid].connect) || acquireUart()) return -1;
gsfan 25:f6e5622d2930 106
gsfan 25:f6e5622d2930 107 if ((_gs_sock[cid].protocol == GSPROT_TCP) ||
gsfan 25:f6e5622d2930 108 (_gs_sock[cid].protocol == GSPROT_UDP && _gs_sock[cid].type == GSTYPE_CLIENT) ||
gsfan 25:f6e5622d2930 109 (_gs_sock[cid].protocol == GSPROT_HTTPD)) {
gsfan 25:f6e5622d2930 110 // TCP Client, TCP Server, UDP Client
gsfan 26:b347ee3a1087 111 resetResponse(GSRES_NONE);
gsfan 25:f6e5622d2930 112 #ifdef GS_BULK
gsfan 36:a70b11e1560f 113 sprintf(cmd, "\x1bZ%X%04d", cid, len);
gsfan 36:a70b11e1560f 114 _gs_puts(cmd);
gsfan 25:f6e5622d2930 115 for (i = 0; i < len; i ++) {
gsfan 25:f6e5622d2930 116 _gs_putc(buf[i]);
gsfan 25:f6e5622d2930 117 #ifdef DEBUG_VIEW
gsfan 25:f6e5622d2930 118 DBG("%c", buf[i]);
gsfan 25:f6e5622d2930 119 #endif
gsfan 25:f6e5622d2930 120 }
gsfan 37:e61ea8267415 121 releaseUart();
gsfan 37:e61ea8267415 122 #else // GS_BULK
gsfan 36:a70b11e1560f 123 sprintf(cmd, "\x1bS%X", cid);
gsfan 36:a70b11e1560f 124 _gs_puts(cmd);
gsfan 25:f6e5622d2930 125 for (i = 0; i < len; i ++) {
gsfan 25:f6e5622d2930 126 if (buf[i] >= 0x20 && buf[i] < 0x7f) {
gsfan 25:f6e5622d2930 127 _gs_putc(buf[i]);
gsfan 25:f6e5622d2930 128 #ifdef DEBUG_VIEW
gsfan 25:f6e5622d2930 129 DBG("%c", buf[i]);
gsfan 25:f6e5622d2930 130 #endif
gsfan 25:f6e5622d2930 131 }
gsfan 25:f6e5622d2930 132 }
gsfan 25:f6e5622d2930 133 _gs_putc(0x1b);
gsfan 25:f6e5622d2930 134 _gs_putc('E');
gsfan 37:e61ea8267415 135 releaseUart();
gsfan 25:f6e5622d2930 136 #endif
gsfan 25:f6e5622d2930 137 } else {
gsfan 37:e61ea8267415 138 releaseUart();
gsfan 25:f6e5622d2930 139 return -1;
gsfan 25:f6e5622d2930 140 }
gsfan 26:b347ee3a1087 141
gsfan 26:b347ee3a1087 142 return waitResponse(GS_TIMEOUT);
gsfan 25:f6e5622d2930 143 }
gsfan 25:f6e5622d2930 144
gsfan 25:f6e5622d2930 145 int GSwifi::send (int cid, const char *buf, int len, Host &host) {
gsfan 36:a70b11e1560f 146 char cmd[GS_CMD_SIZE];
gsfan 25:f6e5622d2930 147 int i;
gsfan 25:f6e5622d2930 148
gsfan 37:e61ea8267415 149 if ((! _gs_sock[cid].connect) || acquireUart()) return -1;
gsfan 25:f6e5622d2930 150
gsfan 25:f6e5622d2930 151 if ((_gs_sock[cid].protocol == GSPROT_UDP && _gs_sock[cid].type == GSTYPE_SERVER)) {
gsfan 25:f6e5622d2930 152 // UDP Server
gsfan 26:b347ee3a1087 153 resetResponse(GSRES_NONE);
gsfan 25:f6e5622d2930 154 #ifdef GS_BULK
gsfan 36:a70b11e1560f 155 sprintf(cmd, "\x1bY%X%d.%d.%d.%d:%d:%04d", cid, host.getIp()[0], host.getIp()[1], host.getIp()[2], host.getIp()[3], host.getPort(), len);
gsfan 36:a70b11e1560f 156 _gs_puts(cmd);
gsfan 25:f6e5622d2930 157 for (i = 0; i < len; i ++) {
gsfan 25:f6e5622d2930 158 _gs_putc(buf[i]);
gsfan 25:f6e5622d2930 159 #ifdef DEBUG_VIEW
gsfan 25:f6e5622d2930 160 DBG("%c", buf[i]);
gsfan 25:f6e5622d2930 161 #endif
gsfan 25:f6e5622d2930 162 }
gsfan 37:e61ea8267415 163 releaseUart();
gsfan 37:e61ea8267415 164 #else // GS_BULK
gsfan 36:a70b11e1560f 165 sprintf(cmd, "\x1bU%X%d.%d.%d.%d:%d:", cid, host.getIp()[0], host.getIp()[1], host.getIp()[2], host.getIp()[3], host.getPort());
gsfan 36:a70b11e1560f 166 _gs_puts(cmd);
gsfan 25:f6e5622d2930 167 for (i = 0; i < len; i ++) {
gsfan 25:f6e5622d2930 168 if (buf[i] >= 0x20 && buf[i] < 0x7f) {
gsfan 25:f6e5622d2930 169 _gs_putc(buf[i]);
gsfan 25:f6e5622d2930 170 #ifdef DEBUG_VIEW
gsfan 25:f6e5622d2930 171 DBG("%c", buf[i]);
gsfan 25:f6e5622d2930 172 #endif
gsfan 25:f6e5622d2930 173 }
gsfan 25:f6e5622d2930 174 }
gsfan 25:f6e5622d2930 175 _gs_putc(0x1b);
gsfan 25:f6e5622d2930 176 _gs_putc('E');
gsfan 37:e61ea8267415 177 releaseUart();
gsfan 25:f6e5622d2930 178 #endif
gsfan 25:f6e5622d2930 179 } else {
gsfan 37:e61ea8267415 180 releaseUart();
gsfan 25:f6e5622d2930 181 return -1;
gsfan 25:f6e5622d2930 182 }
gsfan 26:b347ee3a1087 183
gsfan 26:b347ee3a1087 184 return waitResponse(GS_TIMEOUT);
gsfan 25:f6e5622d2930 185 }
gsfan 25:f6e5622d2930 186
gsfan 25:f6e5622d2930 187 int GSwifi::recv (int cid, char *buf, int len) {
gsfan 25:f6e5622d2930 188 int i;
gsfan 25:f6e5622d2930 189 Timer timeout;
gsfan 25:f6e5622d2930 190
gsfan 25:f6e5622d2930 191 if (_gs_sock[cid].data == NULL) return 0;
gsfan 25:f6e5622d2930 192
gsfan 25:f6e5622d2930 193 timeout.start();
gsfan 26:b347ee3a1087 194 while (_gs_sock[cid].data->isEmpty()) {
gsfan 25:f6e5622d2930 195 if (timeout.read_ms() > GS_TIMEOUT) return 0;
gsfan 25:f6e5622d2930 196 }
gsfan 25:f6e5622d2930 197 timeout.stop();
gsfan 25:f6e5622d2930 198
gsfan 25:f6e5622d2930 199 for (i = 0; i < len; i ++) {
gsfan 25:f6e5622d2930 200 if (_gs_sock[cid].data->dequeue(&buf[i]) == false) break;
gsfan 25:f6e5622d2930 201 }
gsfan 25:f6e5622d2930 202 return i;
gsfan 25:f6e5622d2930 203 }
gsfan 25:f6e5622d2930 204
gsfan 25:f6e5622d2930 205 int GSwifi::recv (int cid, char *buf, int len, Host &host) {
gsfan 25:f6e5622d2930 206 int i;
gsfan 25:f6e5622d2930 207 Timer timeout;
gsfan 25:f6e5622d2930 208
gsfan 25:f6e5622d2930 209 if (_gs_sock[cid].data == NULL) return 0;
gsfan 25:f6e5622d2930 210
gsfan 25:f6e5622d2930 211 timeout.start();
gsfan 26:b347ee3a1087 212 while (_gs_sock[cid].data->isEmpty()) {
gsfan 25:f6e5622d2930 213 if (timeout.read_ms() > GS_TIMEOUT) return 0;
gsfan 25:f6e5622d2930 214 }
gsfan 25:f6e5622d2930 215 timeout.stop();
gsfan 25:f6e5622d2930 216
gsfan 25:f6e5622d2930 217 for (i = 0; i < len; i ++) {
gsfan 25:f6e5622d2930 218 if (_gs_sock[cid].data->dequeue(&buf[i]) == false) break;
gsfan 25:f6e5622d2930 219 }
gsfan 25:f6e5622d2930 220 host = _from;
gsfan 25:f6e5622d2930 221 return i;
gsfan 25:f6e5622d2930 222 }
gsfan 25:f6e5622d2930 223
gsfan 25:f6e5622d2930 224 bool GSwifi::isConnected (int cid) {
gsfan 25:f6e5622d2930 225 return _gs_sock[cid].connect;
gsfan 25:f6e5622d2930 226 }