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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers GSwifi_sock.cpp Source File

GSwifi_sock.cpp

Go to the documentation of this file.
00001 /* Copyright (C) 2013 gsfan, MIT License
00002  *
00003  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
00004  * and associated documentation files (the "Software"), to deal in the Software without restriction,
00005  * including without limitation the rights to use, copy, modify, merge, publish, distribute,
00006  * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
00007  * furnished to do so, subject to the following conditions:
00008  *
00009  * The above copyright notice and this permission notice shall be included in all copies or
00010  * substantial portions of the Software.
00011  *
00012  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
00013  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00014  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00015  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00016  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00017  */
00018 /** @file
00019  * @brief Gainspan wi-fi module library for mbed
00020  * GS1011MIC, GS1011MIP, GainSpan WiFi Breakout, etc.
00021  */
00022 
00023 #include "dbg.h"
00024 #include "mbed.h"
00025 #include "GSwifi.h"
00026 
00027 
00028 void GSwifi::newSock (int cid, GSTYPE type, GSPROTOCOL pro) {
00029     DBG("newSock %d\r\n", cid);
00030     _gs_sock[cid].type = type;
00031     _gs_sock[cid].protocol = pro;
00032     _gs_sock[cid].connect = true;
00033     if (_gs_sock[cid].data == NULL) {
00034         _gs_sock[cid].data = new CircBuffer<char>(GS_DATA_SIZE);
00035     } else {
00036         _gs_sock[cid].data->flush();
00037     }
00038     _gs_sock[cid].lcid = 0;
00039     _gs_sock[cid].received = false;
00040     _gs_sock[cid].onGsReceive.detach();
00041 }
00042 
00043 int GSwifi::open (Host &host, GSPROTOCOL pro, int port) {
00044     char cmd[GS_CMD_SIZE];
00045 
00046     if (! _connect || _status != GSSTAT_READY) return -1;
00047     if (host.getIp().isNull()) {
00048         if (getHostByName(host)) {
00049             if (getHostByName(host)) return -1;
00050         }
00051     }
00052     if (host.getPort() == 0) {
00053         return -1;
00054     }
00055 
00056     if (pro == GSPROT_UDP) {
00057         sprintf(cmd, "AT+NCUDP=%d.%d.%d.%d,%d", host.getIp()[0], host.getIp()[1], host.getIp()[2], host.getIp()[3], host.getPort());
00058         if (port) {
00059             sprintf(&cmd[strlen(cmd)], ",%d", port);
00060         }
00061     } else {
00062         sprintf(cmd, "AT+NCTCP=%d.%d.%d.%d,%d", host.getIp()[0], host.getIp()[1], host.getIp()[2], host.getIp()[3], host.getPort());
00063     }
00064     if (command(cmd, GSRES_CONNECT)) return -1;
00065 
00066     newSock(_cid, GSTYPE_CLIENT, pro);
00067     return _cid;
00068 }
00069 
00070 int GSwifi::listen (int port, GSPROTOCOL pro) {
00071     char cmd[GS_CMD_SIZE];
00072 
00073     if (! _connect || _status != GSSTAT_READY) return -1;
00074     if (port == 0) {
00075         return -1;
00076     }
00077 
00078     if (pro == GSPROT_UDP) {
00079         sprintf(cmd, "AT+NSUDP=%d", port);
00080     } else {
00081         sprintf(cmd, "AT+NSTCP=%d", port);
00082     }
00083     if (command(cmd, GSRES_CONNECT)) return -1;
00084 
00085     newSock(_cid, GSTYPE_SERVER, pro);
00086     return _cid;
00087 }
00088 
00089 int GSwifi::close (int cid) {
00090     char cmd[GS_CMD_SIZE];
00091 
00092     if (! _gs_sock[cid].connect) return -1;
00093 
00094     _gs_sock[cid].connect = false;
00095 //    delete _gs_sock[cid].data;
00096 //    _gs_sock[cid].data = NULL;
00097     sprintf(cmd, "AT+NCLOSE=%X", cid);
00098     return command(cmd, GSRES_NORMAL);    
00099 }
00100 
00101 int GSwifi::send (int cid, const char *buf, int len) {
00102     int i;
00103     char cmd[GS_CMD_SIZE];
00104 
00105     if ((! _gs_sock[cid].connect) || acquireUart()) return -1;
00106 
00107     if ((_gs_sock[cid].protocol == GSPROT_TCP) ||
00108       (_gs_sock[cid].protocol == GSPROT_UDP && _gs_sock[cid].type == GSTYPE_CLIENT) ||
00109       (_gs_sock[cid].protocol == GSPROT_HTTPD)) {
00110         // TCP Client, TCP Server, UDP Client
00111         resetResponse(GSRES_NONE);
00112 #ifdef GS_BULK
00113         sprintf(cmd, "\x1bZ%X%04d", cid, len);
00114         _gs_puts(cmd);
00115         for (i = 0; i < len; i ++) {
00116             _gs_putc(buf[i]);
00117 #ifdef DEBUG_VIEW
00118             DBG("%c", buf[i]);
00119 #endif
00120         }
00121         releaseUart();
00122 #else // GS_BULK
00123         sprintf(cmd, "\x1bS%X", cid);
00124         _gs_puts(cmd);
00125         for (i = 0; i < len; i ++) {
00126             if (buf[i] >= 0x20 && buf[i] < 0x7f) {
00127                 _gs_putc(buf[i]);
00128 #ifdef DEBUG_VIEW
00129                 DBG("%c", buf[i]);
00130 #endif
00131             }
00132         }
00133         _gs_putc(0x1b);
00134         _gs_putc('E');
00135         releaseUart();
00136 #endif
00137     } else {
00138         releaseUart();
00139         return -1;
00140     }
00141     
00142     return waitResponse(GS_TIMEOUT);
00143 }
00144 
00145 int GSwifi::send (int cid, const char *buf, int len, Host &host) {
00146     char cmd[GS_CMD_SIZE];
00147     int i;
00148 
00149     if ((! _gs_sock[cid].connect) || acquireUart()) return -1;
00150 
00151     if ((_gs_sock[cid].protocol == GSPROT_UDP && _gs_sock[cid].type == GSTYPE_SERVER)) {
00152         // UDP Server
00153         resetResponse(GSRES_NONE);
00154 #ifdef GS_BULK
00155         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);
00156         _gs_puts(cmd);
00157         for (i = 0; i < len; i ++) {
00158             _gs_putc(buf[i]);
00159 #ifdef DEBUG_VIEW
00160             DBG("%c", buf[i]);
00161 #endif
00162         }
00163         releaseUart();
00164 #else // GS_BULK
00165         sprintf(cmd, "\x1bU%X%d.%d.%d.%d:%d:", cid, host.getIp()[0], host.getIp()[1], host.getIp()[2], host.getIp()[3], host.getPort());
00166         _gs_puts(cmd);
00167         for (i = 0; i < len; i ++) {
00168             if (buf[i] >= 0x20 && buf[i] < 0x7f) {
00169                 _gs_putc(buf[i]);
00170 #ifdef DEBUG_VIEW
00171                 DBG("%c", buf[i]);
00172 #endif
00173             }
00174         }
00175         _gs_putc(0x1b);
00176         _gs_putc('E');
00177         releaseUart();
00178 #endif
00179     } else {
00180         releaseUart();
00181         return -1;
00182     }
00183 
00184     return waitResponse(GS_TIMEOUT);
00185 }
00186 
00187 int GSwifi::recv (int cid, char *buf, int len) {
00188     int i;
00189     Timer timeout;
00190     
00191     if (_gs_sock[cid].data == NULL) return 0;
00192 
00193     timeout.start();
00194     while (_gs_sock[cid].data->isEmpty()) {
00195         if (timeout.read_ms() > GS_TIMEOUT) return 0;
00196     }
00197     timeout.stop();
00198 
00199     for (i = 0; i < len; i ++) {
00200         if (_gs_sock[cid].data->dequeue(&buf[i]) == false) break;
00201     }
00202     return i;
00203 }
00204 
00205 int GSwifi::recv (int cid, char *buf, int len, Host &host) {
00206     int i;
00207     Timer timeout;
00208     
00209     if (_gs_sock[cid].data == NULL) return 0;
00210 
00211     timeout.start();
00212     while (_gs_sock[cid].data->isEmpty()) {
00213         if (timeout.read_ms() > GS_TIMEOUT) return 0;
00214     }
00215     timeout.stop();
00216 
00217     for (i = 0; i < len; i ++) {
00218         if (_gs_sock[cid].data->dequeue(&buf[i]) == false) break;
00219     }
00220     host = _from;
00221     return i;
00222 }
00223 
00224 bool GSwifi::isConnected (int cid) {
00225     return _gs_sock[cid].connect;
00226 }