GSwifiInterface library (interface for GainSpan Wi-Fi GS1011 modules) Please see https://mbed.org/users/gsfan/notebook/GSwifiInterface/

Dependents:   GSwifiInterface_HelloWorld GSwifiInterface_HelloServo GSwifiInterface_UDPEchoServer GSwifiInterface_UDPEchoClient ... more

Fork of WiflyInterface by mbed official

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers GSwifi_sock.cpp Source File

GSwifi_sock.cpp

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 
00019 #include "GSwifi.h"
00020 
00021 int GSwifi::getHostByName(const char * host, char * ip)
00022 {
00023     int i, flg = 0;
00024 
00025     if (!isAssociated() || _state.status != STAT_READY) return -1;
00026 
00027     for (i = 0; i < strlen(host); i ++) {
00028         if ((host[i] < '0' || host[i] > '9') && host[i] != '.') {
00029             flg = 1;
00030             break;
00031         }
00032     }
00033     if (!flg) {
00034         strncpy(ip, host, 16);
00035         return 0;
00036     }
00037 
00038     if (cmdDNSLOOKUP(host)) {
00039         // retry
00040         wait_ms(1000);
00041         if (cmdDNSLOOKUP(host)) return -1;
00042     }
00043     strncpy(ip, _state.resolv, 16);
00044     return 0;
00045 }
00046 
00047 int GSwifi::open (Protocol proto, const char *ip, int port, int src, void(*func)(int)) {
00048     int cid;
00049 
00050     if (!isAssociated() || _state.status != STAT_READY) return -1;
00051 
00052     _state.cid = -1;
00053     if (proto == PROTO_TCP) {
00054         if (cmdNCTCP(ip, port)) return -1;
00055     } else {
00056         if (cmdNCUDP(ip, port, src)) return -1;
00057     }
00058     if (_state.cid < 0) return -1;
00059     cid = _state.cid;
00060     _con[cid].protocol = proto;
00061     _con[cid].type = TYPE_CLIENT;
00062     _con[cid].func = func;
00063     return cid;
00064 }
00065 
00066 int GSwifi::listen (Protocol proto, int port, void(*func)(int)) {
00067     int cid;
00068 
00069     if (!isAssociated() || _state.status != STAT_READY) return -1;
00070 
00071     _state.cid = -1;
00072     if (proto == PROTO_TCP) {
00073         if (cmdNSTCP(port)) return -1;
00074     } else {
00075         if (cmdNSUDP(port)) return -1;
00076     }
00077     if (_state.cid < 0) return -1;
00078     cid = _state.cid;
00079     _con[cid].protocol = proto;
00080     _con[cid].type = TYPE_SERVER;
00081     _con[cid].func = func;
00082     return cid;
00083 }
00084 
00085 int GSwifi::close (int cid) {
00086 
00087     if (!isConnected(cid)) return -1;
00088 
00089     _con[cid].connected = false;
00090     return cmdNCLOSE(cid);
00091 }
00092 
00093 int GSwifi::send (int cid, const char *buf, int len) {
00094     char cmd[CFG_CMD_SIZE];
00095 
00096     if (!isConnected(cid)) return -1;
00097 
00098     if ((_con[cid].protocol == PROTO_TCP) ||
00099       (_con[cid].protocol == PROTO_UDP && _con[cid].type == TYPE_CLIENT) ||
00100       (_con[cid].protocol == PROTO_HTTPD)) {
00101         if (len > CFG_DATA_SIZE) len = CFG_DATA_SIZE;
00102         sprintf(cmd, "\x1bZ%X%04d", cid, len);
00103         return sendData(buf, len, CFG_TIMEOUT, cmd);
00104     } else {
00105         return -1;
00106     }
00107 }
00108 
00109 int GSwifi::sendto (int cid, const char *buf, int len, const char *ip, int port) {
00110     char cmd[CFG_CMD_SIZE];
00111 
00112     if (!isConnected(cid)) return -1;
00113 
00114     if ((_con[cid].protocol == PROTO_UDP && _con[cid].type == TYPE_SERVER)) {
00115         if (len > CFG_DATA_SIZE) len = CFG_DATA_SIZE;
00116         sprintf(cmd, "\x1bY%X%s:%d:%04d", cid, ip, port, len);
00117         return sendData(buf, len, CFG_TIMEOUT, cmd);
00118     } else {
00119         return -1;
00120     }
00121 }
00122 
00123 int GSwifi::recv (int cid, char *buf, int len) {
00124     int i;
00125 
00126     if (!isConnected(cid)) return -1;
00127 
00128     if (_con[cid].buf == NULL) return 0;
00129     while (!_con[cid].received && _state.mode != MODE_COMMAND);
00130     _con[cid].received = false;
00131     for (i = 0; i < len; i ++) {
00132         if (_con[cid].buf->dequeue(&buf[i]) == false) break;
00133     }
00134     setRts(true);
00135     return i;
00136 }
00137 
00138 int GSwifi::recvfrom (int cid, char *buf, int len, char *ip, int *port) {
00139     int i;
00140 
00141     if (!isConnected(cid)) return -1;
00142 
00143     if (_con[cid].buf == NULL) return 0;
00144     while (!_con[cid].received && _state.mode != MODE_COMMAND);
00145     _con[cid].received = false;
00146     for (i = 0; i < len; i ++) {
00147         if (_con[cid].buf->dequeue(&buf[i]) == false) break;
00148     }
00149     strncpy(ip, _con[cid].ip, 16);
00150     *port = _con[cid].port;
00151     setRts(true);
00152     return i;
00153 }
00154 
00155 int GSwifi::readable (int cid) {
00156     if (!isConnected(cid)) return -1;
00157 
00158     if (_con[cid].buf == NULL) return -1;
00159     return _con[cid].buf->available();
00160 }
00161 
00162 bool GSwifi::isConnected (int cid) {
00163     if (cid < 0 || cid >= 16) return false;
00164 
00165     return _con[cid].connected;
00166 }
00167 
00168 int GSwifi::accept (int cid) {
00169     int i;
00170 
00171     if (!isConnected(cid)) return -1;
00172 
00173     for (i = 0; i < 16; i ++) {
00174         if (_con[i].connected && _con[i].accept && _con[i].parent == cid) {
00175             _con[i].accept = false;
00176             return i;
00177         }
00178     }
00179     return -1;
00180 }
00181 
00182 int GSwifi::getRemote(int cid, char **ip, int *port) {
00183 
00184     if (!isConnected(cid)) return -1;
00185 
00186     *ip = _con[cid].ip;
00187     *port = _con[cid].port;
00188     return 0;
00189 }
00190 
00191 void GSwifi::initCon (int cid, bool connected) {
00192     _con[cid].parent = -1;
00193     _con[cid].func = NULL;
00194     _con[cid].accept = false;
00195 #ifndef CFG_ENABLE_RTOS
00196     if (_con[cid].buf == NULL) {
00197         _con[cid].buf = new CircBuffer<char>(CFG_DATA_SIZE);
00198         if (_con[cid].buf == NULL) error("Can't allocate memory");
00199     }
00200 #endif
00201     if (_con[cid].buf != NULL) {
00202         _con[cid].buf->flush();
00203     }
00204     _con[cid].connected = connected;
00205 }