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)

Committer:
gsfan
Date:
Fri Mar 07 15:16:55 2014 +0000
Revision:
17:c30ed7b1536d
Parent:
16:105b56570533
Child:
19:d035412a7803
bug fix wsOpen

Who changed what in which revision?

UserRevisionLine numberNew contents of line
gsfan 11:71d67fea5ace 1 /* Copyright (C) 2013 gsfan, MIT License
gsfan 5:78943b3945b5 2 *
gsfan 5:78943b3945b5 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
gsfan 5:78943b3945b5 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
gsfan 5:78943b3945b5 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
gsfan 5:78943b3945b5 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
gsfan 5:78943b3945b5 7 * furnished to do so, subject to the following conditions:
gsfan 5:78943b3945b5 8 *
gsfan 5:78943b3945b5 9 * The above copyright notice and this permission notice shall be included in all copies or
gsfan 5:78943b3945b5 10 * substantial portions of the Software.
gsfan 5:78943b3945b5 11 *
gsfan 5:78943b3945b5 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
gsfan 5:78943b3945b5 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
gsfan 5:78943b3945b5 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
gsfan 5:78943b3945b5 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
gsfan 5:78943b3945b5 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
gsfan 5:78943b3945b5 17 */
gsfan 5:78943b3945b5 18
gsfan 5:78943b3945b5 19 #include "mbed.h"
gsfan 5:78943b3945b5 20 #include "GSwifi.h"
gsfan 5:78943b3945b5 21 #include <string>
gsfan 5:78943b3945b5 22 #include <algorithm>
gsfan 5:78943b3945b5 23
gsfan 5:78943b3945b5 24 GSwifi * GSwifi::_inst;
gsfan 5:78943b3945b5 25
gsfan 8:64184a968e3b 26 GSwifi::GSwifi(PinName tx, PinName rx, PinName cts, PinName rts, PinName reset, PinName alarm, int baud):
gsfan 8:64184a968e3b 27 _gs(tx, rx), _reset(reset)
gsfan 5:78943b3945b5 28 {
gsfan 8:64184a968e3b 29 _inst = this;
gsfan 5:78943b3945b5 30 memset(&_state, 0, sizeof(_state));
gsfan 5:78943b3945b5 31 memset(&_con, 0, sizeof(_con));
gsfan 11:71d67fea5ace 32 _state.initialized = false;
gsfan 11:71d67fea5ace 33 _state.status = STAT_READY;
gsfan 8:64184a968e3b 34 _state.cid = -1;
gsfan 8:64184a968e3b 35 _state.buf = new CircBuffer<char>(CFG_DATA_SIZE);
gsfan 11:71d67fea5ace 36 #ifdef CFG_ENABLE_RTOS
gsfan 16:105b56570533 37 for (int i = 0; i < CFG_MAX_SOCKETS; i ++) {
gsfan 8:64184a968e3b 38 _con[i].buf = new CircBuffer<char>(CFG_DATA_SIZE);
gsfan 11:71d67fea5ace 39 if (_con[i].buf == NULL)
gsfan 11:71d67fea5ace 40 error("CircBuffer failed");
gsfan 5:78943b3945b5 41 }
gsfan 8:64184a968e3b 42 _threadPoll = NULL;
gsfan 5:78943b3945b5 43 #endif
gsfan 8:64184a968e3b 44
gsfan 8:64184a968e3b 45 setReset(true);
gsfan 8:64184a968e3b 46 initUart(cts, rts, alarm, baud);
gsfan 8:64184a968e3b 47 setAlarm(true);
gsfan 8:64184a968e3b 48 wait_ms(10);
gsfan 8:64184a968e3b 49 setAlarm(false);
gsfan 8:64184a968e3b 50 wait_ms(100);
gsfan 8:64184a968e3b 51 setReset(false);
gsfan 8:64184a968e3b 52 wait_ms(100);
gsfan 5:78943b3945b5 53 }
gsfan 5:78943b3945b5 54
gsfan 11:71d67fea5ace 55 int GSwifi::join()
gsfan 5:78943b3945b5 56 {
gsfan 11:71d67fea5ace 57 int i;
gsfan 8:64184a968e3b 58 bool r = -1;
gsfan 5:78943b3945b5 59
gsfan 8:64184a968e3b 60 _state.wm = WM_INFRASTRUCTURE;
gsfan 11:71d67fea5ace 61 _state.initialized = true;
gsfan 8:64184a968e3b 62 if (!strlen(_state.name)) {
gsfan 8:64184a968e3b 63 strncpy(_state.name, CFG_DHCPNAME, sizeof(_state.name));
gsfan 8:64184a968e3b 64 }
gsfan 8:64184a968e3b 65 clearFlags();
gsfan 12:057089026a20 66 _state.mode = MODE_COMMAND;
gsfan 8:64184a968e3b 67 sendCommand(NULL, RES_NULL, 0);
gsfan 8:64184a968e3b 68 if (cmdE(false)) return -1;
gsfan 8:64184a968e3b 69 if (_flow) {
gsfan 8:64184a968e3b 70 cmdR(true);
gsfan 8:64184a968e3b 71 }
gsfan 8:64184a968e3b 72 cmdBDATA(true);
gsfan 8:64184a968e3b 73 if (cmdNMAC()) return -1;
gsfan 13:189e04ac70bd 74 cmdWREGDOMAIN(CFG_WREGDOMAIN);
gsfan 8:64184a968e3b 75 cmdWM(0); // infrastructure
gsfan 5:78943b3945b5 76 wait_ms(100);
gsfan 5:78943b3945b5 77
gsfan 11:71d67fea5ace 78 switch (_state.sec) {
gsfan 5:78943b3945b5 79 case SEC_NONE:
gsfan 5:78943b3945b5 80 case SEC_OPEN:
gsfan 5:78943b3945b5 81 case SEC_WEP:
gsfan 13:189e04ac70bd 82 cmdNDHCP(_state.dhcp, _state.name, DEFAULT_WAIT_RESP_TIMEOUT);
gsfan 15:086d1a33a197 83 if (! _state.dhcp) {
gsfan 15:086d1a33a197 84 cmdNSET(_state.ip, _state.netmask, _state.ip);
gsfan 15:086d1a33a197 85 }
gsfan 8:64184a968e3b 86 cmdWAUTH(_state.sec);
gsfan 11:71d67fea5ace 87 if (_state.sec != SEC_NONE) {
gsfan 8:64184a968e3b 88 cmdWWEP(1, _state.pass);
gsfan 5:78943b3945b5 89 wait_ms(100);
gsfan 5:78943b3945b5 90 }
gsfan 11:71d67fea5ace 91 for (i = 0; i < CFG_TRYJOIN; i ++) {
gsfan 11:71d67fea5ace 92 r = cmdWA(_state.ssid);
gsfan 11:71d67fea5ace 93 if (!r) break;
gsfan 8:64184a968e3b 94 DBG("retry\r\n");
gsfan 8:64184a968e3b 95 wait_ms(1000);
gsfan 5:78943b3945b5 96 }
gsfan 5:78943b3945b5 97 break;
gsfan 5:78943b3945b5 98 case SEC_WPA_PSK:
gsfan 5:78943b3945b5 99 case SEC_WPA2_PSK:
gsfan 13:189e04ac70bd 100 cmdNDHCP(_state.dhcp, _state.name, DEFAULT_WAIT_RESP_TIMEOUT);
gsfan 15:086d1a33a197 101 if (! _state.dhcp) {
gsfan 15:086d1a33a197 102 cmdNSET(_state.ip, _state.netmask, _state.ip);
gsfan 15:086d1a33a197 103 }
gsfan 8:64184a968e3b 104 cmdWAUTH(0);
gsfan 8:64184a968e3b 105 cmdWPAPSK(_state.ssid, _state.pass);
gsfan 5:78943b3945b5 106 wait_ms(100);
gsfan 11:71d67fea5ace 107 for (i = 0; i < CFG_TRYJOIN; i ++) {
gsfan 11:71d67fea5ace 108 r = cmdWA(_state.ssid);
gsfan 11:71d67fea5ace 109 if (!r) break;
gsfan 8:64184a968e3b 110 DBG("retry\r\n");
gsfan 8:64184a968e3b 111 wait_ms(1000);
gsfan 17:c30ed7b1536d 112
gsfan 17:c30ed7b1536d 113 if (_state.dhcp && i == CFG_TRYJOIN - 1) {
gsfan 17:c30ed7b1536d 114 cmdNDHCP(0, NULL, DEFAULT_WAIT_RESP_TIMEOUT);
gsfan 17:c30ed7b1536d 115 r = cmdWA(_state.ssid);
gsfan 17:c30ed7b1536d 116 if (!r && _state.dhcp) {
gsfan 17:c30ed7b1536d 117 wait_ms(1000);
gsfan 17:c30ed7b1536d 118 r = cmdNDHCP(_state.dhcp, NULL, CFG_TIMEOUT);
gsfan 17:c30ed7b1536d 119 }
gsfan 17:c30ed7b1536d 120 break;
gsfan 17:c30ed7b1536d 121 }
gsfan 5:78943b3945b5 122 }
gsfan 5:78943b3945b5 123 break;
gsfan 8:64184a968e3b 124 case SEC_WPA_ENT:
gsfan 8:64184a968e3b 125 case SEC_WPA2_ENT:
gsfan 8:64184a968e3b 126 cmdWAUTH(0);
gsfan 8:64184a968e3b 127 DBG("Can't support security\r\n");
gsfan 8:64184a968e3b 128 break;
gsfan 5:78943b3945b5 129 case SEC_WPS_BUTTON:
gsfan 8:64184a968e3b 130 cmdNDHCP(false);
gsfan 15:086d1a33a197 131 if (! _state.dhcp) {
gsfan 15:086d1a33a197 132 cmdNSET(_state.ip, _state.netmask, _state.ip);
gsfan 15:086d1a33a197 133 }
gsfan 8:64184a968e3b 134 cmdWAUTH(0);
gsfan 8:64184a968e3b 135 r = cmdWWPS(true);
gsfan 8:64184a968e3b 136 if (r) break;
gsfan 8:64184a968e3b 137 if (_state.dhcp) {
gsfan 8:64184a968e3b 138 r = cmdNDHCP(_state.dhcp, _state.name);
gsfan 5:78943b3945b5 139 }
gsfan 8:64184a968e3b 140 cmdWSTATUS();
gsfan 8:64184a968e3b 141 cmdWPAPSK(_state.ssid, _state.pass);
gsfan 8:64184a968e3b 142 break;
gsfan 8:64184a968e3b 143 case SEC_WPS_PIN:
gsfan 8:64184a968e3b 144 cmdNDHCP(false);
gsfan 15:086d1a33a197 145 if (! _state.dhcp) {
gsfan 15:086d1a33a197 146 cmdNSET(_state.ip, _state.netmask, _state.ip);
gsfan 15:086d1a33a197 147 }
gsfan 8:64184a968e3b 148 cmdWAUTH(0);
gsfan 8:64184a968e3b 149 r = cmdWWPS(true, _state.pass);
gsfan 8:64184a968e3b 150 if (r) break;
gsfan 8:64184a968e3b 151 if (_state.dhcp) {
gsfan 8:64184a968e3b 152 r = cmdNDHCP(_state.dhcp, _state.name);
gsfan 5:78943b3945b5 153 }
gsfan 8:64184a968e3b 154 cmdWSTATUS();
gsfan 8:64184a968e3b 155 cmdWPAPSK(_state.ssid, _state.pass);
gsfan 5:78943b3945b5 156 break;
gsfan 5:78943b3945b5 157 default:
gsfan 5:78943b3945b5 158 DBG("Can't use security\r\n");
gsfan 5:78943b3945b5 159 break;
gsfan 5:78943b3945b5 160 }
gsfan 5:78943b3945b5 161
gsfan 8:64184a968e3b 162 if (!r) {
gsfan 11:71d67fea5ace 163 // connected
gsfan 8:64184a968e3b 164 if (! _state.dhcp) {
gsfan 8:64184a968e3b 165 cmdDNSSET(_state.nameserver);
gsfan 5:78943b3945b5 166 }
gsfan 5:78943b3945b5 167 _state.associated = true;
gsfan 11:71d67fea5ace 168 #ifdef CFG_ENABLE_RTOS
gsfan 8:64184a968e3b 169 _threadPoll = new Thread(&threadPoll);
gsfan 8:64184a968e3b 170 // _threadPoll = new Thread(&threadPoll, NULL, osPriorityLow);
gsfan 8:64184a968e3b 171 #endif
gsfan 5:78943b3945b5 172 }
gsfan 5:78943b3945b5 173 return r;
gsfan 5:78943b3945b5 174 }
gsfan 5:78943b3945b5 175
gsfan 11:71d67fea5ace 176 int GSwifi::adhock ()
gsfan 5:78943b3945b5 177 {
gsfan 8:64184a968e3b 178 bool r = -1;
gsfan 5:78943b3945b5 179
gsfan 8:64184a968e3b 180 _state.wm = WM_ADHOCK;
gsfan 11:71d67fea5ace 181 _state.initialized = true;
gsfan 8:64184a968e3b 182 clearFlags();
gsfan 12:057089026a20 183 _state.mode = MODE_COMMAND;
gsfan 8:64184a968e3b 184 sendCommand(NULL, RES_NULL, 0);
gsfan 8:64184a968e3b 185 if (cmdE(false)) return -1;
gsfan 8:64184a968e3b 186 if (_flow) {
gsfan 8:64184a968e3b 187 cmdR(true);
gsfan 5:78943b3945b5 188 }
gsfan 8:64184a968e3b 189 if (cmdNMAC()) return -1;
gsfan 8:64184a968e3b 190 cmdBDATA(true);
gsfan 13:189e04ac70bd 191 cmdWREGDOMAIN(CFG_WREGDOMAIN);
gsfan 8:64184a968e3b 192 cmdWM(1); // adhock
gsfan 8:64184a968e3b 193 wait_ms(100);
gsfan 8:64184a968e3b 194
gsfan 8:64184a968e3b 195 cmdNDHCP(false);
gsfan 8:64184a968e3b 196 if (! _state.dhcp) {
gsfan 8:64184a968e3b 197 cmdNSET(_state.ip, _state.netmask, _state.ip);
gsfan 5:78943b3945b5 198 }
gsfan 5:78943b3945b5 199
gsfan 11:71d67fea5ace 200 switch (_state.sec) {
gsfan 8:64184a968e3b 201 case SEC_NONE:
gsfan 8:64184a968e3b 202 case SEC_OPEN:
gsfan 8:64184a968e3b 203 case SEC_WEP:
gsfan 8:64184a968e3b 204 cmdWAUTH(_state.sec);
gsfan 11:71d67fea5ace 205 if (_state.sec != SEC_NONE) {
gsfan 8:64184a968e3b 206 cmdWWEP(1, _state.pass);
gsfan 8:64184a968e3b 207 wait_ms(100);
gsfan 8:64184a968e3b 208 }
gsfan 8:64184a968e3b 209 r = cmdWA(_state.ssid);
gsfan 8:64184a968e3b 210 break;
gsfan 8:64184a968e3b 211 default:
gsfan 8:64184a968e3b 212 DBG("Can't use security\r\n");
gsfan 8:64184a968e3b 213 break;
gsfan 5:78943b3945b5 214 }
gsfan 5:78943b3945b5 215
gsfan 8:64184a968e3b 216 if (!r) {
gsfan 11:71d67fea5ace 217 // connected
gsfan 8:64184a968e3b 218 _state.associated = true;
gsfan 8:64184a968e3b 219 }
gsfan 8:64184a968e3b 220 return r;
gsfan 5:78943b3945b5 221 }
gsfan 5:78943b3945b5 222
gsfan 11:71d67fea5ace 223 int GSwifi::limitedap ()
gsfan 5:78943b3945b5 224 {
gsfan 8:64184a968e3b 225 bool r = -1;
gsfan 8:64184a968e3b 226
gsfan 8:64184a968e3b 227 _state.wm = WM_LIMITEDAP;
gsfan 11:71d67fea5ace 228 _state.initialized = true;
gsfan 8:64184a968e3b 229 if (!strlen(_state.name)) {
gsfan 8:64184a968e3b 230 strncpy(_state.name, CFG_DNSNAME, sizeof(_state.name));
gsfan 8:64184a968e3b 231 }
gsfan 8:64184a968e3b 232 clearFlags();
gsfan 12:057089026a20 233 _state.mode = MODE_COMMAND;
gsfan 8:64184a968e3b 234 sendCommand(NULL, RES_NULL, 0);
gsfan 8:64184a968e3b 235 if (cmdE(false)) return -1;
gsfan 8:64184a968e3b 236 if (_flow) {
gsfan 8:64184a968e3b 237 cmdR(true);
gsfan 8:64184a968e3b 238 }
gsfan 8:64184a968e3b 239 if (cmdNMAC()) return -1;
gsfan 8:64184a968e3b 240 cmdBDATA(true);
gsfan 13:189e04ac70bd 241 cmdWREGDOMAIN(CFG_WREGDOMAIN);
gsfan 8:64184a968e3b 242 cmdWM(2); // limitedap
gsfan 8:64184a968e3b 243 wait_ms(100);
gsfan 8:64184a968e3b 244
gsfan 8:64184a968e3b 245 cmdNDHCP(false);
gsfan 8:64184a968e3b 246 if (! _state.dhcp) {
gsfan 8:64184a968e3b 247 cmdNSET(_state.ip, _state.netmask, _state.ip);
gsfan 8:64184a968e3b 248 cmdDNSSET(_state.ip);
gsfan 5:78943b3945b5 249 }
gsfan 8:64184a968e3b 250 cmdDHCPSRVR(true);
gsfan 8:64184a968e3b 251 cmdDNS(true, _state.name);
gsfan 8:64184a968e3b 252
gsfan 11:71d67fea5ace 253 switch (_state.sec) {
gsfan 8:64184a968e3b 254 case SEC_NONE:
gsfan 8:64184a968e3b 255 case SEC_OPEN:
gsfan 8:64184a968e3b 256 case SEC_WEP:
gsfan 8:64184a968e3b 257 cmdWAUTH(_state.sec);
gsfan 11:71d67fea5ace 258 if (_state.sec != SEC_NONE) {
gsfan 8:64184a968e3b 259 cmdWWEP(1, _state.pass);
gsfan 8:64184a968e3b 260 wait_ms(100);
gsfan 8:64184a968e3b 261 }
gsfan 8:64184a968e3b 262 r = cmdWA(_state.ssid);
gsfan 8:64184a968e3b 263 break;
gsfan 8:64184a968e3b 264 default:
gsfan 8:64184a968e3b 265 DBG("Can't use security\r\n");
gsfan 8:64184a968e3b 266 break;
gsfan 8:64184a968e3b 267 }
gsfan 8:64184a968e3b 268
gsfan 8:64184a968e3b 269 if (!r) {
gsfan 11:71d67fea5ace 270 // connected
gsfan 8:64184a968e3b 271 _state.associated = true;
gsfan 8:64184a968e3b 272 }
gsfan 8:64184a968e3b 273 return r;
gsfan 5:78943b3945b5 274 }
gsfan 5:78943b3945b5 275
gsfan 12:057089026a20 276 int GSwifi::dissociate()
gsfan 5:78943b3945b5 277 {
gsfan 8:64184a968e3b 278 int i;
gsfan 5:78943b3945b5 279
gsfan 5:78943b3945b5 280 // if already disconnected, return
gsfan 5:78943b3945b5 281 if (!_state.associated)
gsfan 8:64184a968e3b 282 return 0;
gsfan 5:78943b3945b5 283
gsfan 11:71d67fea5ace 284 #ifdef CFG_ENABLE_RTOS
gsfan 8:64184a968e3b 285 if (_threadPoll) {
gsfan 8:64184a968e3b 286 _threadPoll->terminate();
gsfan 8:64184a968e3b 287 delete _threadPoll;
gsfan 8:64184a968e3b 288 }
gsfan 8:64184a968e3b 289 #endif
gsfan 8:64184a968e3b 290 for (i = 0; i < 16; i ++) {
gsfan 8:64184a968e3b 291 if (_con[i].buf) {
gsfan 5:78943b3945b5 292 _con[i].buf->flush();
gsfan 8:64184a968e3b 293 }
gsfan 5:78943b3945b5 294 }
gsfan 8:64184a968e3b 295 cmdNCLOSEALL();
gsfan 8:64184a968e3b 296 cmdWD();
gsfan 8:64184a968e3b 297 cmdNDHCP(false);
gsfan 5:78943b3945b5 298 wait_ms(100);
gsfan 5:78943b3945b5 299
gsfan 5:78943b3945b5 300 _state.associated = false;
gsfan 8:64184a968e3b 301 return 0;
gsfan 5:78943b3945b5 302
gsfan 5:78943b3945b5 303 }
gsfan 5:78943b3945b5 304
gsfan 12:057089026a20 305 bool GSwifi::isAssociated()
gsfan 5:78943b3945b5 306 {
gsfan 8:64184a968e3b 307 return _state.associated;
gsfan 5:78943b3945b5 308 }
gsfan 5:78943b3945b5 309
gsfan 8:64184a968e3b 310 void GSwifi::poll () {
gsfan 11:71d67fea5ace 311 #ifndef CFG_ENABLE_RTOS
gsfan 11:71d67fea5ace 312 static int t = 0;
gsfan 13:189e04ac70bd 313 static Timer timer;
gsfan 11:71d67fea5ace 314
gsfan 12:057089026a20 315 if (_state.wm == WM_INFRASTRUCTURE && (! isAssociated()) && _state.ssid) {
gsfan 13:189e04ac70bd 316 if (t == 0 || timer.read() > CFG_RECONNECT) {
gsfan 11:71d67fea5ace 317 // Wi-Fi re-associate
gsfan 11:71d67fea5ace 318 if (_state.initialized) {
gsfan 13:189e04ac70bd 319 if (cmdWA(_state.ssid)) {
gsfan 13:189e04ac70bd 320 timer.reset();
gsfan 13:189e04ac70bd 321 timer.start();
gsfan 13:189e04ac70bd 322 t = 1;
gsfan 13:189e04ac70bd 323 } else {
gsfan 11:71d67fea5ace 324 _state.associated = true;
gsfan 13:189e04ac70bd 325 timer.stop();
gsfan 11:71d67fea5ace 326 t = 0;
gsfan 11:71d67fea5ace 327 }
gsfan 11:71d67fea5ace 328 } else {
gsfan 11:71d67fea5ace 329 if (join()) {
gsfan 13:189e04ac70bd 330 timer.reset();
gsfan 13:189e04ac70bd 331 timer.start();
gsfan 13:189e04ac70bd 332 t = 1;
gsfan 11:71d67fea5ace 333 } else {
gsfan 13:189e04ac70bd 334 timer.stop();
gsfan 13:189e04ac70bd 335 t = 0;
gsfan 11:71d67fea5ace 336 }
gsfan 11:71d67fea5ace 337 }
gsfan 11:71d67fea5ace 338 }
gsfan 11:71d67fea5ace 339 }
gsfan 11:71d67fea5ace 340 #else
gsfan 12:057089026a20 341 if (_state.wm == WM_INFRASTRUCTURE && (! isAssociated()) && _state.ssid) {
gsfan 8:64184a968e3b 342 // Wi-Fi re-associate
gsfan 11:71d67fea5ace 343 if (_state.initialized) {
gsfan 11:71d67fea5ace 344 if (!cmdWA(_state.ssid)) {
gsfan 11:71d67fea5ace 345 _state.associated = true;
gsfan 11:71d67fea5ace 346 }
gsfan 11:71d67fea5ace 347 } else {
gsfan 11:71d67fea5ace 348 join();
gsfan 5:78943b3945b5 349 }
gsfan 5:78943b3945b5 350 }
gsfan 11:71d67fea5ace 351 #endif
gsfan 12:057089026a20 352
gsfan 12:057089026a20 353 for (int i = 0; i < 16; i ++) {
gsfan 16:105b56570533 354 if (isConnected(i) && _con[i].received && _con[i].buf != NULL)
gsfan 12:057089026a20 355 if (_con[i].func != NULL && !_con[i].buf->isEmpty()) {
gsfan 12:057089026a20 356 _con[i].func(i);
gsfan 12:057089026a20 357 if (_con[i].buf->isEmpty()) {
gsfan 12:057089026a20 358 _con[i].received = false;
gsfan 12:057089026a20 359 }
gsfan 12:057089026a20 360 }
gsfan 12:057089026a20 361 }
gsfan 12:057089026a20 362
gsfan 12:057089026a20 363 #ifdef CFG_ENABLE_HTTPD
gsfan 12:057089026a20 364 httpdPoll();
gsfan 12:057089026a20 365 #endif
gsfan 5:78943b3945b5 366 }
gsfan 5:78943b3945b5 367
gsfan 11:71d67fea5ace 368 #ifdef CFG_ENABLE_RTOS
gsfan 8:64184a968e3b 369 void GSwifi::threadPoll (void const *args) {
gsfan 8:64184a968e3b 370 GSwifi * _wifi = GSwifi::getInstance();
gsfan 8:64184a968e3b 371 if (_wifi == NULL)
gsfan 8:64184a968e3b 372 error("Socket constructor error: no wifly instance available!\r\n");
gsfan 5:78943b3945b5 373
gsfan 8:64184a968e3b 374 INFO("threadPoll");
gsfan 8:64184a968e3b 375 for (;;) {
gsfan 8:64184a968e3b 376 Thread::signal_wait(1);
gsfan 8:64184a968e3b 377 Thread::wait(1000);
gsfan 8:64184a968e3b 378 INFO("disassociated");
gsfan 12:057089026a20 379 while (!_wifi->isAssociated()){
gsfan 8:64184a968e3b 380 _wifi->poll();
gsfan 13:189e04ac70bd 381 Thread::wait(CFG_RECONNECT * 1000);
gsfan 5:78943b3945b5 382 }
gsfan 5:78943b3945b5 383 }
gsfan 5:78943b3945b5 384 }
gsfan 8:64184a968e3b 385 #endif
gsfan 5:78943b3945b5 386
gsfan 12:057089026a20 387 GSwifi::Status GSwifi::getStatus () {
gsfan 12:057089026a20 388 return _state.status;
gsfan 12:057089026a20 389 }
gsfan 12:057089026a20 390
gsfan 12:057089026a20 391 int GSwifi::setMacAddress (const char *mac) {
gsfan 12:057089026a20 392 if (cmdNMAC(mac)) return -1;
gsfan 12:057089026a20 393 strncpy(_state.mac, mac, sizeof(_state.mac));
gsfan 12:057089026a20 394 return 0;
gsfan 12:057089026a20 395 }
gsfan 12:057089026a20 396
gsfan 12:057089026a20 397 int GSwifi::getMacAddress (char *mac) {
gsfan 12:057089026a20 398 if (cmdNMAC()) return -1;
gsfan 12:057089026a20 399 strcpy(mac, _state.mac);
gsfan 12:057089026a20 400 return 0;
gsfan 12:057089026a20 401 }
gsfan 12:057089026a20 402
gsfan 11:71d67fea5ace 403 int GSwifi::setAddress (const char *name) {
gsfan 11:71d67fea5ace 404 _state.dhcp = true;
gsfan 11:71d67fea5ace 405 strncpy(_state.name, name, sizeof(_state.name));
gsfan 11:71d67fea5ace 406 return 0;
gsfan 11:71d67fea5ace 407 }
gsfan 11:71d67fea5ace 408
gsfan 15:086d1a33a197 409 int GSwifi::setAddress (const char *ip, const char *netmask, const char *gateway, const char *dns, const char *name) {
gsfan 11:71d67fea5ace 410 _state.dhcp = false;
gsfan 11:71d67fea5ace 411 strncpy(_state.ip, ip, sizeof(_state.ip));
gsfan 11:71d67fea5ace 412 strncpy(_state.netmask, netmask, sizeof(_state.netmask));
gsfan 11:71d67fea5ace 413 strncpy(_state.gateway, gateway, sizeof(_state.gateway));
gsfan 15:086d1a33a197 414 strncpy(_state.nameserver, dns, sizeof(_state.nameserver));
gsfan 11:71d67fea5ace 415 strncpy(_state.name, name, sizeof(_state.name));
gsfan 11:71d67fea5ace 416 return 0;
gsfan 11:71d67fea5ace 417 }
gsfan 11:71d67fea5ace 418
gsfan 11:71d67fea5ace 419 int GSwifi::getAddress (char *ip, char *netmask, char *gateway) {
gsfan 11:71d67fea5ace 420 strcpy(ip, _state.ip);
gsfan 11:71d67fea5ace 421 strcpy(netmask, _state.netmask);
gsfan 11:71d67fea5ace 422 strcpy(gateway, _state.gateway);
gsfan 11:71d67fea5ace 423 return 0;
gsfan 11:71d67fea5ace 424 }
gsfan 11:71d67fea5ace 425
gsfan 8:64184a968e3b 426 int GSwifi::setSsid (Security sec, const char *ssid, const char *phrase) {
gsfan 8:64184a968e3b 427 int i;
gsfan 5:78943b3945b5 428
gsfan 8:64184a968e3b 429 _state.sec = sec;
gsfan 11:71d67fea5ace 430
gsfan 8:64184a968e3b 431 // change all ' ' in '$' in the ssid and the passphrase
gsfan 8:64184a968e3b 432 strncpy(_state.ssid, ssid, sizeof(_state.ssid));
gsfan 8:64184a968e3b 433 for (i = 0; i < strlen(_state.ssid); i++) {
gsfan 8:64184a968e3b 434 if (_state.ssid[i] == ' ')
gsfan 8:64184a968e3b 435 _state.ssid[i] = '$';
gsfan 5:78943b3945b5 436 }
gsfan 11:71d67fea5ace 437
gsfan 11:71d67fea5ace 438 if (sec == SEC_WEP) {
gsfan 11:71d67fea5ace 439 if (strlen(phrase) == 5 || strlen(phrase) == 13) {
gsfan 11:71d67fea5ace 440 for (i = 0; i < strlen(phrase); i++) {
gsfan 11:71d67fea5ace 441 _state.pass[i * 2] = '0' + ((phrase[i] >> 4) & 0x0f);
gsfan 11:71d67fea5ace 442 _state.pass[i * 2 + 1] = '0' + (phrase[i + 1] & 0x0f);
gsfan 11:71d67fea5ace 443 }
gsfan 11:71d67fea5ace 444 } else {
gsfan 11:71d67fea5ace 445 strncpy(_state.pass, phrase, strlen(phrase));
gsfan 11:71d67fea5ace 446 }
gsfan 11:71d67fea5ace 447 } else {
gsfan 11:71d67fea5ace 448 strncpy(_state.pass, phrase, sizeof(_state.pass));
gsfan 11:71d67fea5ace 449 }
gsfan 8:64184a968e3b 450 for (i = 0; i < strlen(_state.pass); i++) {
gsfan 8:64184a968e3b 451 if (_state.pass[i] == ' ')
gsfan 8:64184a968e3b 452 _state.pass[i] = '$';
gsfan 5:78943b3945b5 453 }
gsfan 5:78943b3945b5 454 return 0;
gsfan 5:78943b3945b5 455 }
gsfan 5:78943b3945b5 456