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

GainSpan Wi-Fi library

The GS1011/GS2100 is an ultra low power 802.11b wireless module from GainSpan.

mbed RTOS supported.

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

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

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

mbed RTOS に対応しています。(mbed2.0)

Revision:
11:71d67fea5ace
Parent:
8:64184a968e3b
Child:
12:057089026a20
--- a/GSwifi/GSwifi_sock.cpp	Thu Oct 31 08:34:45 2013 +0000
+++ b/GSwifi/GSwifi_sock.cpp	Fri Nov 15 04:20:14 2013 +0000
@@ -22,6 +22,8 @@
 {
     int i, flg = 0;
 
+    if (!isConnected() && _state.status == STAT_READY) return -1;
+
     for (i = 0; i < strlen(host); i ++) {
         if ((host[i] < '0' || host[i] > '9') && host[i] != '.') {
             flg = 1;
@@ -43,35 +45,45 @@
 }
 
 int GSwifi::open (Protocol proto, const char *ip, int port, int src) {
+    int cid;
+
+    if (!isConnected() && _state.status == STAT_READY) return -1;
+
     _state.cid = -1;
-    _state.acid = -1;
     if (proto == PROTO_TCP) {
         if (cmdNCTCP(ip, port)) return -1;
     } else {
         if (cmdNCUDP(ip, port, src)) return -1;
     }
     if (_state.cid < 0) return -1;
-    _con[_state.cid].protocol = proto;
-    _con[_state.cid].type = TYPE_CLIENT;
-    return _state.cid;
+    cid = _state.cid;
+    _con[cid].protocol = proto;
+    _con[cid].type = TYPE_CLIENT;
+    return cid;
 }
 
 int GSwifi::listen (Protocol proto, int port) {
+    int cid;
+
+    if (!isConnected() && _state.status == STAT_READY) return -1;
+
     _state.cid = -1;
-    _state.acid = -1;
     if (proto == PROTO_TCP) {
         if (cmdNSTCP(port)) return -1;
     } else {
         if (cmdNSUDP(port)) return -1;
     }
     if (_state.cid < 0) return -1;
-    _con[_state.cid].protocol = proto;
-    _con[_state.cid].type = TYPE_SERVER;
-    return _state.cid;
+    cid = _state.cid;
+    _con[cid].protocol = proto;
+    _con[cid].type = TYPE_SERVER;
+    return cid;
 }
 
 int GSwifi::close (int cid) {
-    if (cid < 0 || cid >= 16) return -1;
+
+    if (!isConnected(cid)) return -1;
+
     _con[cid].connected = false;
     return cmdNCLOSE(cid);
 }
@@ -79,15 +91,14 @@
 int GSwifi::send (int cid, const char *buf, int len) {
     char cmd[CFG_CMD_SIZE];
 
-    if (cid < 0 || cid >= 16) return -1;
-    if (!_con[cid].connected) return -1;
+    if (!isConnected(cid)) return -1;
 
     if ((_con[cid].protocol == PROTO_TCP) ||
       (_con[cid].protocol == PROTO_UDP && _con[cid].type == TYPE_CLIENT) ||
       (_con[cid].protocol == PROTO_HTTPD)) {
         if (len > CFG_DATA_SIZE) len = CFG_DATA_SIZE;
         sprintf(cmd, "\x1bZ%X%04d", cid, len);
-        return sendData(buf, len, DEFAULT_WAIT_RESP_TIMEOUT, cmd);
+        return sendData(buf, len, CFG_TIMEOUT, cmd);
     } else {
         return -1;
     }
@@ -96,13 +107,12 @@
 int GSwifi::sendto (int cid, const char *buf, int len, const char *ip, int port) {
     char cmd[CFG_CMD_SIZE];
 
-    if (cid < 0 || cid >= 16) return -1;
-    if (!_con[cid].connected) return -1;
+    if (!isConnected(cid)) return -1;
 
     if ((_con[cid].protocol == PROTO_UDP && _con[cid].type == TYPE_SERVER)) {
         if (len > CFG_DATA_SIZE) len = CFG_DATA_SIZE;
         sprintf(cmd, "\x1bY%X%s:%d:%04d", cid, ip, port, len);
-        return sendData(buf, len, DEFAULT_WAIT_RESP_TIMEOUT, cmd);
+        return sendData(buf, len, CFG_TIMEOUT, cmd);
     } else {
         return -1;
     }
@@ -111,8 +121,7 @@
 int GSwifi::recv (int cid, char *buf, int len) {
     int i;
 
-    if (cid < 0 || cid >= 16) return -1;
-    if (!_con[cid].connected) return -1;
+    if (!isConnected(cid)) return -1;
 
     if (_con[cid].buf == NULL) return 0;
     while (!_con[cid].received && _state.mode != MODE_COMMAND);
@@ -127,8 +136,7 @@
 int GSwifi::recvfrom (int cid, char *buf, int len, char *ip, int *port) {
     int i;
 
-    if (cid < 0 || cid >= 16) return -1;
-    if (!_con[cid].connected) return -1;
+    if (!isConnected(cid)) return -1;
 
     if (_con[cid].buf == NULL) return 0;
     while (!_con[cid].received && _state.mode != MODE_COMMAND);
@@ -143,20 +151,23 @@
 }
 
 int GSwifi::readable (int cid) {
-    if (cid < 0 || cid >= 16) return -1;
-    if (!_con[cid].connected) return -1;
+    if (!isConnected(cid)) return -1;
+
     if (_con[cid].buf == NULL) return -1;
     return _con[cid].buf->available();
 }
 
 bool GSwifi::isConnected (int cid) {
     if (cid < 0 || cid >= 16) return false;
+
     return _con[cid].connected;
 }
 
 int GSwifi::accept (int cid) {
     int i;
-    if (cid < 0 || cid >= 16) return -1;
+
+    if (!isConnected(cid)) return -1;
+
     for (i = 0; i < 16; i ++) {
         if (_con[i].connected && _con[i].accept && _con[i].parent == cid) {
             _con[i].accept = false;
@@ -167,7 +178,9 @@
 }
 
 int GSwifi::getRemote(int cid, char **ip, int *port) {
-    if (cid < 0 || cid >= 16) return -1;
+
+    if (!isConnected(cid)) return -1;
+
     *ip = _con[cid].ip;
     *port = _con[cid].port;
     return 0;