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:
Mon Jun 24 07:36:48 2013 +0000
Revision:
36:a70b11e1560f
Parent:
34:f5f40c92af00
Child:
37:e61ea8267415
fix _gs_puts

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 36:a70b11e1560f 105 if ((! _gs_sock[cid].connect) || waitCts()) 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 25:f6e5622d2930 121 #else
gsfan 36:a70b11e1560f 122 sprintf(cmd, "\x1bS%X", cid);
gsfan 36:a70b11e1560f 123 _gs_puts(cmd);
gsfan 25:f6e5622d2930 124 for (i = 0; i < len; i ++) {
gsfan 25:f6e5622d2930 125 if (buf[i] >= 0x20 && buf[i] < 0x7f) {
gsfan 25:f6e5622d2930 126 _gs_putc(buf[i]);
gsfan 25:f6e5622d2930 127 #ifdef DEBUG_VIEW
gsfan 25:f6e5622d2930 128 DBG("%c", buf[i]);
gsfan 25:f6e5622d2930 129 #endif
gsfan 25:f6e5622d2930 130 }
gsfan 25:f6e5622d2930 131 }
gsfan 25:f6e5622d2930 132 _gs_putc(0x1b);
gsfan 25:f6e5622d2930 133 _gs_putc('E');
gsfan 25:f6e5622d2930 134 #endif
gsfan 25:f6e5622d2930 135 } else {
gsfan 25:f6e5622d2930 136 return -1;
gsfan 25:f6e5622d2930 137 }
gsfan 26:b347ee3a1087 138
gsfan 26:b347ee3a1087 139 return waitResponse(GS_TIMEOUT);
gsfan 25:f6e5622d2930 140 }
gsfan 25:f6e5622d2930 141
gsfan 25:f6e5622d2930 142 int GSwifi::send (int cid, const char *buf, int len, Host &host) {
gsfan 36:a70b11e1560f 143 char cmd[GS_CMD_SIZE];
gsfan 25:f6e5622d2930 144 int i;
gsfan 25:f6e5622d2930 145
gsfan 36:a70b11e1560f 146 if ((! _gs_sock[cid].connect) || waitCts()) return -1;
gsfan 25:f6e5622d2930 147
gsfan 25:f6e5622d2930 148 if ((_gs_sock[cid].protocol == GSPROT_UDP && _gs_sock[cid].type == GSTYPE_SERVER)) {
gsfan 25:f6e5622d2930 149 // UDP Server
gsfan 26:b347ee3a1087 150 resetResponse(GSRES_NONE);
gsfan 25:f6e5622d2930 151 #ifdef GS_BULK
gsfan 36:a70b11e1560f 152 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 153 _gs_puts(cmd);
gsfan 25:f6e5622d2930 154 for (i = 0; i < len; i ++) {
gsfan 25:f6e5622d2930 155 _gs_putc(buf[i]);
gsfan 25:f6e5622d2930 156 #ifdef DEBUG_VIEW
gsfan 25:f6e5622d2930 157 DBG("%c", buf[i]);
gsfan 25:f6e5622d2930 158 #endif
gsfan 25:f6e5622d2930 159 }
gsfan 25:f6e5622d2930 160 #else
gsfan 36:a70b11e1560f 161 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 162 _gs_puts(cmd);
gsfan 25:f6e5622d2930 163 for (i = 0; i < len; i ++) {
gsfan 25:f6e5622d2930 164 if (buf[i] >= 0x20 && buf[i] < 0x7f) {
gsfan 25:f6e5622d2930 165 _gs_putc(buf[i]);
gsfan 25:f6e5622d2930 166 #ifdef DEBUG_VIEW
gsfan 25:f6e5622d2930 167 DBG("%c", buf[i]);
gsfan 25:f6e5622d2930 168 #endif
gsfan 25:f6e5622d2930 169 }
gsfan 25:f6e5622d2930 170 }
gsfan 25:f6e5622d2930 171 _gs_putc(0x1b);
gsfan 25:f6e5622d2930 172 _gs_putc('E');
gsfan 25:f6e5622d2930 173 #endif
gsfan 25:f6e5622d2930 174 } else {
gsfan 25:f6e5622d2930 175 return -1;
gsfan 25:f6e5622d2930 176 }
gsfan 26:b347ee3a1087 177
gsfan 26:b347ee3a1087 178 return waitResponse(GS_TIMEOUT);
gsfan 25:f6e5622d2930 179 }
gsfan 25:f6e5622d2930 180
gsfan 25:f6e5622d2930 181 int GSwifi::recv (int cid, char *buf, int len) {
gsfan 25:f6e5622d2930 182 int i;
gsfan 25:f6e5622d2930 183 Timer timeout;
gsfan 25:f6e5622d2930 184
gsfan 25:f6e5622d2930 185 if (_gs_sock[cid].data == NULL) return 0;
gsfan 25:f6e5622d2930 186
gsfan 25:f6e5622d2930 187 timeout.start();
gsfan 26:b347ee3a1087 188 while (_gs_sock[cid].data->isEmpty()) {
gsfan 25:f6e5622d2930 189 if (timeout.read_ms() > GS_TIMEOUT) return 0;
gsfan 25:f6e5622d2930 190 }
gsfan 25:f6e5622d2930 191 timeout.stop();
gsfan 25:f6e5622d2930 192
gsfan 25:f6e5622d2930 193 for (i = 0; i < len; i ++) {
gsfan 25:f6e5622d2930 194 if (_gs_sock[cid].data->dequeue(&buf[i]) == false) break;
gsfan 25:f6e5622d2930 195 }
gsfan 25:f6e5622d2930 196 return i;
gsfan 25:f6e5622d2930 197 }
gsfan 25:f6e5622d2930 198
gsfan 25:f6e5622d2930 199 int GSwifi::recv (int cid, char *buf, int len, Host &host) {
gsfan 25:f6e5622d2930 200 int i;
gsfan 25:f6e5622d2930 201 Timer timeout;
gsfan 25:f6e5622d2930 202
gsfan 25:f6e5622d2930 203 if (_gs_sock[cid].data == NULL) return 0;
gsfan 25:f6e5622d2930 204
gsfan 25:f6e5622d2930 205 timeout.start();
gsfan 26:b347ee3a1087 206 while (_gs_sock[cid].data->isEmpty()) {
gsfan 25:f6e5622d2930 207 if (timeout.read_ms() > GS_TIMEOUT) return 0;
gsfan 25:f6e5622d2930 208 }
gsfan 25:f6e5622d2930 209 timeout.stop();
gsfan 25:f6e5622d2930 210
gsfan 25:f6e5622d2930 211 for (i = 0; i < len; i ++) {
gsfan 25:f6e5622d2930 212 if (_gs_sock[cid].data->dequeue(&buf[i]) == false) break;
gsfan 25:f6e5622d2930 213 }
gsfan 25:f6e5622d2930 214 host = _from;
gsfan 25:f6e5622d2930 215 return i;
gsfan 25:f6e5622d2930 216 }
gsfan 25:f6e5622d2930 217
gsfan 25:f6e5622d2930 218 bool GSwifi::isConnected (int cid) {
gsfan 25:f6e5622d2930 219 return _gs_sock[cid].connect;
gsfan 25:f6e5622d2930 220 }