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:
Thu Jun 05 04:21:02 2014 +0000
Revision:
19:d035412a7803
Parent:
17:c30ed7b1536d
Child:
20:9bfb8a13010c
supported http server with rtos

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 19:d035412a7803 35 #ifdef CFG_EXTENDED_MEMORY1
gsfan 19:d035412a7803 36 int n = CFG_EXTENDED_SIZE1 / (CFG_MAX_SOCKETS + 1);
gsfan 19:d035412a7803 37 _state.buf = new CircBuffer<char>(n - 1, CFG_EXTENDED_MEMORY1);
gsfan 19:d035412a7803 38 #ifdef CFG_ENABLE_RTOS
gsfan 19:d035412a7803 39 for (int i = 0; i < CFG_MAX_SOCKETS; i ++) {
gsfan 19:d035412a7803 40 _con[i].buf = new CircBuffer<char>(n - 1, CFG_EXTENDED_MEMORY1 + (n * (i + 1)));
gsfan 19:d035412a7803 41 if (_con[i].buf == NULL)
gsfan 19:d035412a7803 42 error("CircBuffer failed");
gsfan 19:d035412a7803 43 }
gsfan 19:d035412a7803 44 _threadPoll = NULL;
gsfan 19:d035412a7803 45 #endif
gsfan 19:d035412a7803 46 #else // CFG_EXTENDED_MEMORY
gsfan 8:64184a968e3b 47 _state.buf = new CircBuffer<char>(CFG_DATA_SIZE);
gsfan 11:71d67fea5ace 48 #ifdef CFG_ENABLE_RTOS
gsfan 16:105b56570533 49 for (int i = 0; i < CFG_MAX_SOCKETS; i ++) {
gsfan 8:64184a968e3b 50 _con[i].buf = new CircBuffer<char>(CFG_DATA_SIZE);
gsfan 11:71d67fea5ace 51 if (_con[i].buf == NULL)
gsfan 11:71d67fea5ace 52 error("CircBuffer failed");
gsfan 5:78943b3945b5 53 }
gsfan 8:64184a968e3b 54 _threadPoll = NULL;
gsfan 5:78943b3945b5 55 #endif
gsfan 19:d035412a7803 56 #endif // CFG_EXTENDED_MEMORY
gsfan 8:64184a968e3b 57
gsfan 8:64184a968e3b 58 setReset(true);
gsfan 8:64184a968e3b 59 initUart(cts, rts, alarm, baud);
gsfan 8:64184a968e3b 60 setAlarm(true);
gsfan 8:64184a968e3b 61 wait_ms(10);
gsfan 8:64184a968e3b 62 setAlarm(false);
gsfan 8:64184a968e3b 63 wait_ms(100);
gsfan 8:64184a968e3b 64 setReset(false);
gsfan 8:64184a968e3b 65 wait_ms(100);
gsfan 5:78943b3945b5 66 }
gsfan 5:78943b3945b5 67
gsfan 11:71d67fea5ace 68 int GSwifi::join()
gsfan 5:78943b3945b5 69 {
gsfan 11:71d67fea5ace 70 int i;
gsfan 8:64184a968e3b 71 bool r = -1;
gsfan 5:78943b3945b5 72
gsfan 8:64184a968e3b 73 _state.wm = WM_INFRASTRUCTURE;
gsfan 11:71d67fea5ace 74 _state.initialized = true;
gsfan 8:64184a968e3b 75 if (!strlen(_state.name)) {
gsfan 8:64184a968e3b 76 strncpy(_state.name, CFG_DHCPNAME, sizeof(_state.name));
gsfan 8:64184a968e3b 77 }
gsfan 8:64184a968e3b 78 clearFlags();
gsfan 12:057089026a20 79 _state.mode = MODE_COMMAND;
gsfan 8:64184a968e3b 80 sendCommand(NULL, RES_NULL, 0);
gsfan 8:64184a968e3b 81 if (cmdE(false)) return -1;
gsfan 8:64184a968e3b 82 if (_flow) {
gsfan 8:64184a968e3b 83 cmdR(true);
gsfan 8:64184a968e3b 84 }
gsfan 8:64184a968e3b 85 cmdBDATA(true);
gsfan 8:64184a968e3b 86 if (cmdNMAC()) return -1;
gsfan 13:189e04ac70bd 87 cmdWREGDOMAIN(CFG_WREGDOMAIN);
gsfan 8:64184a968e3b 88 cmdWM(0); // infrastructure
gsfan 5:78943b3945b5 89 wait_ms(100);
gsfan 5:78943b3945b5 90
gsfan 11:71d67fea5ace 91 switch (_state.sec) {
gsfan 5:78943b3945b5 92 case SEC_NONE:
gsfan 5:78943b3945b5 93 case SEC_OPEN:
gsfan 5:78943b3945b5 94 case SEC_WEP:
gsfan 13:189e04ac70bd 95 cmdNDHCP(_state.dhcp, _state.name, DEFAULT_WAIT_RESP_TIMEOUT);
gsfan 15:086d1a33a197 96 if (! _state.dhcp) {
gsfan 15:086d1a33a197 97 cmdNSET(_state.ip, _state.netmask, _state.ip);
gsfan 15:086d1a33a197 98 }
gsfan 8:64184a968e3b 99 cmdWAUTH(_state.sec);
gsfan 11:71d67fea5ace 100 if (_state.sec != SEC_NONE) {
gsfan 8:64184a968e3b 101 cmdWWEP(1, _state.pass);
gsfan 5:78943b3945b5 102 wait_ms(100);
gsfan 5:78943b3945b5 103 }
gsfan 11:71d67fea5ace 104 for (i = 0; i < CFG_TRYJOIN; i ++) {
gsfan 11:71d67fea5ace 105 r = cmdWA(_state.ssid);
gsfan 11:71d67fea5ace 106 if (!r) break;
gsfan 8:64184a968e3b 107 DBG("retry\r\n");
gsfan 8:64184a968e3b 108 wait_ms(1000);
gsfan 5:78943b3945b5 109 }
gsfan 5:78943b3945b5 110 break;
gsfan 5:78943b3945b5 111 case SEC_WPA_PSK:
gsfan 5:78943b3945b5 112 case SEC_WPA2_PSK:
gsfan 13:189e04ac70bd 113 cmdNDHCP(_state.dhcp, _state.name, DEFAULT_WAIT_RESP_TIMEOUT);
gsfan 15:086d1a33a197 114 if (! _state.dhcp) {
gsfan 15:086d1a33a197 115 cmdNSET(_state.ip, _state.netmask, _state.ip);
gsfan 15:086d1a33a197 116 }
gsfan 8:64184a968e3b 117 cmdWAUTH(0);
gsfan 8:64184a968e3b 118 cmdWPAPSK(_state.ssid, _state.pass);
gsfan 5:78943b3945b5 119 wait_ms(100);
gsfan 11:71d67fea5ace 120 for (i = 0; i < CFG_TRYJOIN; i ++) {
gsfan 11:71d67fea5ace 121 r = cmdWA(_state.ssid);
gsfan 11:71d67fea5ace 122 if (!r) break;
gsfan 8:64184a968e3b 123 DBG("retry\r\n");
gsfan 8:64184a968e3b 124 wait_ms(1000);
gsfan 17:c30ed7b1536d 125
gsfan 17:c30ed7b1536d 126 if (_state.dhcp && i == CFG_TRYJOIN - 1) {
gsfan 17:c30ed7b1536d 127 cmdNDHCP(0, NULL, DEFAULT_WAIT_RESP_TIMEOUT);
gsfan 17:c30ed7b1536d 128 r = cmdWA(_state.ssid);
gsfan 17:c30ed7b1536d 129 if (!r && _state.dhcp) {
gsfan 17:c30ed7b1536d 130 wait_ms(1000);
gsfan 17:c30ed7b1536d 131 r = cmdNDHCP(_state.dhcp, NULL, CFG_TIMEOUT);
gsfan 17:c30ed7b1536d 132 }
gsfan 17:c30ed7b1536d 133 break;
gsfan 17:c30ed7b1536d 134 }
gsfan 5:78943b3945b5 135 }
gsfan 5:78943b3945b5 136 break;
gsfan 8:64184a968e3b 137 case SEC_WPA_ENT:
gsfan 8:64184a968e3b 138 case SEC_WPA2_ENT:
gsfan 8:64184a968e3b 139 cmdWAUTH(0);
gsfan 8:64184a968e3b 140 DBG("Can't support security\r\n");
gsfan 8:64184a968e3b 141 break;
gsfan 5:78943b3945b5 142 case SEC_WPS_BUTTON:
gsfan 8:64184a968e3b 143 cmdNDHCP(false);
gsfan 15:086d1a33a197 144 if (! _state.dhcp) {
gsfan 15:086d1a33a197 145 cmdNSET(_state.ip, _state.netmask, _state.ip);
gsfan 15:086d1a33a197 146 }
gsfan 8:64184a968e3b 147 cmdWAUTH(0);
gsfan 8:64184a968e3b 148 r = cmdWWPS(true);
gsfan 8:64184a968e3b 149 if (r) break;
gsfan 8:64184a968e3b 150 if (_state.dhcp) {
gsfan 8:64184a968e3b 151 r = cmdNDHCP(_state.dhcp, _state.name);
gsfan 5:78943b3945b5 152 }
gsfan 8:64184a968e3b 153 cmdWSTATUS();
gsfan 8:64184a968e3b 154 cmdWPAPSK(_state.ssid, _state.pass);
gsfan 8:64184a968e3b 155 break;
gsfan 8:64184a968e3b 156 case SEC_WPS_PIN:
gsfan 8:64184a968e3b 157 cmdNDHCP(false);
gsfan 15:086d1a33a197 158 if (! _state.dhcp) {
gsfan 15:086d1a33a197 159 cmdNSET(_state.ip, _state.netmask, _state.ip);
gsfan 15:086d1a33a197 160 }
gsfan 8:64184a968e3b 161 cmdWAUTH(0);
gsfan 8:64184a968e3b 162 r = cmdWWPS(true, _state.pass);
gsfan 8:64184a968e3b 163 if (r) break;
gsfan 8:64184a968e3b 164 if (_state.dhcp) {
gsfan 8:64184a968e3b 165 r = cmdNDHCP(_state.dhcp, _state.name);
gsfan 5:78943b3945b5 166 }
gsfan 8:64184a968e3b 167 cmdWSTATUS();
gsfan 8:64184a968e3b 168 cmdWPAPSK(_state.ssid, _state.pass);
gsfan 5:78943b3945b5 169 break;
gsfan 5:78943b3945b5 170 default:
gsfan 5:78943b3945b5 171 DBG("Can't use security\r\n");
gsfan 5:78943b3945b5 172 break;
gsfan 5:78943b3945b5 173 }
gsfan 5:78943b3945b5 174
gsfan 8:64184a968e3b 175 if (!r) {
gsfan 11:71d67fea5ace 176 // connected
gsfan 8:64184a968e3b 177 if (! _state.dhcp) {
gsfan 8:64184a968e3b 178 cmdDNSSET(_state.nameserver);
gsfan 5:78943b3945b5 179 }
gsfan 5:78943b3945b5 180 _state.associated = true;
gsfan 11:71d67fea5ace 181 #ifdef CFG_ENABLE_RTOS
gsfan 8:64184a968e3b 182 _threadPoll = new Thread(&threadPoll);
gsfan 8:64184a968e3b 183 // _threadPoll = new Thread(&threadPoll, NULL, osPriorityLow);
gsfan 8:64184a968e3b 184 #endif
gsfan 5:78943b3945b5 185 }
gsfan 5:78943b3945b5 186 return r;
gsfan 5:78943b3945b5 187 }
gsfan 5:78943b3945b5 188
gsfan 11:71d67fea5ace 189 int GSwifi::adhock ()
gsfan 5:78943b3945b5 190 {
gsfan 8:64184a968e3b 191 bool r = -1;
gsfan 5:78943b3945b5 192
gsfan 8:64184a968e3b 193 _state.wm = WM_ADHOCK;
gsfan 11:71d67fea5ace 194 _state.initialized = true;
gsfan 8:64184a968e3b 195 clearFlags();
gsfan 12:057089026a20 196 _state.mode = MODE_COMMAND;
gsfan 8:64184a968e3b 197 sendCommand(NULL, RES_NULL, 0);
gsfan 8:64184a968e3b 198 if (cmdE(false)) return -1;
gsfan 8:64184a968e3b 199 if (_flow) {
gsfan 8:64184a968e3b 200 cmdR(true);
gsfan 5:78943b3945b5 201 }
gsfan 8:64184a968e3b 202 if (cmdNMAC()) return -1;
gsfan 8:64184a968e3b 203 cmdBDATA(true);
gsfan 13:189e04ac70bd 204 cmdWREGDOMAIN(CFG_WREGDOMAIN);
gsfan 8:64184a968e3b 205 cmdWM(1); // adhock
gsfan 8:64184a968e3b 206 wait_ms(100);
gsfan 8:64184a968e3b 207
gsfan 8:64184a968e3b 208 cmdNDHCP(false);
gsfan 8:64184a968e3b 209 if (! _state.dhcp) {
gsfan 8:64184a968e3b 210 cmdNSET(_state.ip, _state.netmask, _state.ip);
gsfan 5:78943b3945b5 211 }
gsfan 5:78943b3945b5 212
gsfan 11:71d67fea5ace 213 switch (_state.sec) {
gsfan 8:64184a968e3b 214 case SEC_NONE:
gsfan 8:64184a968e3b 215 case SEC_OPEN:
gsfan 8:64184a968e3b 216 case SEC_WEP:
gsfan 8:64184a968e3b 217 cmdWAUTH(_state.sec);
gsfan 11:71d67fea5ace 218 if (_state.sec != SEC_NONE) {
gsfan 8:64184a968e3b 219 cmdWWEP(1, _state.pass);
gsfan 8:64184a968e3b 220 wait_ms(100);
gsfan 8:64184a968e3b 221 }
gsfan 8:64184a968e3b 222 r = cmdWA(_state.ssid);
gsfan 8:64184a968e3b 223 break;
gsfan 8:64184a968e3b 224 default:
gsfan 8:64184a968e3b 225 DBG("Can't use security\r\n");
gsfan 8:64184a968e3b 226 break;
gsfan 5:78943b3945b5 227 }
gsfan 5:78943b3945b5 228
gsfan 8:64184a968e3b 229 if (!r) {
gsfan 11:71d67fea5ace 230 // connected
gsfan 8:64184a968e3b 231 _state.associated = true;
gsfan 8:64184a968e3b 232 }
gsfan 8:64184a968e3b 233 return r;
gsfan 5:78943b3945b5 234 }
gsfan 5:78943b3945b5 235
gsfan 11:71d67fea5ace 236 int GSwifi::limitedap ()
gsfan 5:78943b3945b5 237 {
gsfan 8:64184a968e3b 238 bool r = -1;
gsfan 8:64184a968e3b 239
gsfan 8:64184a968e3b 240 _state.wm = WM_LIMITEDAP;
gsfan 11:71d67fea5ace 241 _state.initialized = true;
gsfan 8:64184a968e3b 242 if (!strlen(_state.name)) {
gsfan 8:64184a968e3b 243 strncpy(_state.name, CFG_DNSNAME, sizeof(_state.name));
gsfan 8:64184a968e3b 244 }
gsfan 8:64184a968e3b 245 clearFlags();
gsfan 12:057089026a20 246 _state.mode = MODE_COMMAND;
gsfan 8:64184a968e3b 247 sendCommand(NULL, RES_NULL, 0);
gsfan 8:64184a968e3b 248 if (cmdE(false)) return -1;
gsfan 8:64184a968e3b 249 if (_flow) {
gsfan 8:64184a968e3b 250 cmdR(true);
gsfan 8:64184a968e3b 251 }
gsfan 8:64184a968e3b 252 if (cmdNMAC()) return -1;
gsfan 8:64184a968e3b 253 cmdBDATA(true);
gsfan 13:189e04ac70bd 254 cmdWREGDOMAIN(CFG_WREGDOMAIN);
gsfan 8:64184a968e3b 255 cmdWM(2); // limitedap
gsfan 8:64184a968e3b 256 wait_ms(100);
gsfan 8:64184a968e3b 257
gsfan 8:64184a968e3b 258 cmdNDHCP(false);
gsfan 8:64184a968e3b 259 if (! _state.dhcp) {
gsfan 8:64184a968e3b 260 cmdNSET(_state.ip, _state.netmask, _state.ip);
gsfan 8:64184a968e3b 261 cmdDNSSET(_state.ip);
gsfan 5:78943b3945b5 262 }
gsfan 8:64184a968e3b 263 cmdDHCPSRVR(true);
gsfan 8:64184a968e3b 264 cmdDNS(true, _state.name);
gsfan 8:64184a968e3b 265
gsfan 11:71d67fea5ace 266 switch (_state.sec) {
gsfan 8:64184a968e3b 267 case SEC_NONE:
gsfan 8:64184a968e3b 268 case SEC_OPEN:
gsfan 8:64184a968e3b 269 case SEC_WEP:
gsfan 8:64184a968e3b 270 cmdWAUTH(_state.sec);
gsfan 11:71d67fea5ace 271 if (_state.sec != SEC_NONE) {
gsfan 8:64184a968e3b 272 cmdWWEP(1, _state.pass);
gsfan 8:64184a968e3b 273 wait_ms(100);
gsfan 8:64184a968e3b 274 }
gsfan 8:64184a968e3b 275 r = cmdWA(_state.ssid);
gsfan 8:64184a968e3b 276 break;
gsfan 8:64184a968e3b 277 default:
gsfan 8:64184a968e3b 278 DBG("Can't use security\r\n");
gsfan 8:64184a968e3b 279 break;
gsfan 8:64184a968e3b 280 }
gsfan 8:64184a968e3b 281
gsfan 8:64184a968e3b 282 if (!r) {
gsfan 11:71d67fea5ace 283 // connected
gsfan 8:64184a968e3b 284 _state.associated = true;
gsfan 8:64184a968e3b 285 }
gsfan 8:64184a968e3b 286 return r;
gsfan 5:78943b3945b5 287 }
gsfan 5:78943b3945b5 288
gsfan 12:057089026a20 289 int GSwifi::dissociate()
gsfan 5:78943b3945b5 290 {
gsfan 8:64184a968e3b 291 int i;
gsfan 5:78943b3945b5 292
gsfan 5:78943b3945b5 293 // if already disconnected, return
gsfan 5:78943b3945b5 294 if (!_state.associated)
gsfan 8:64184a968e3b 295 return 0;
gsfan 5:78943b3945b5 296
gsfan 11:71d67fea5ace 297 #ifdef CFG_ENABLE_RTOS
gsfan 8:64184a968e3b 298 if (_threadPoll) {
gsfan 8:64184a968e3b 299 _threadPoll->terminate();
gsfan 8:64184a968e3b 300 delete _threadPoll;
gsfan 8:64184a968e3b 301 }
gsfan 8:64184a968e3b 302 #endif
gsfan 8:64184a968e3b 303 for (i = 0; i < 16; i ++) {
gsfan 8:64184a968e3b 304 if (_con[i].buf) {
gsfan 5:78943b3945b5 305 _con[i].buf->flush();
gsfan 8:64184a968e3b 306 }
gsfan 5:78943b3945b5 307 }
gsfan 8:64184a968e3b 308 cmdNCLOSEALL();
gsfan 8:64184a968e3b 309 cmdWD();
gsfan 8:64184a968e3b 310 cmdNDHCP(false);
gsfan 5:78943b3945b5 311 wait_ms(100);
gsfan 5:78943b3945b5 312
gsfan 5:78943b3945b5 313 _state.associated = false;
gsfan 8:64184a968e3b 314 return 0;
gsfan 5:78943b3945b5 315
gsfan 5:78943b3945b5 316 }
gsfan 5:78943b3945b5 317
gsfan 12:057089026a20 318 bool GSwifi::isAssociated()
gsfan 5:78943b3945b5 319 {
gsfan 8:64184a968e3b 320 return _state.associated;
gsfan 5:78943b3945b5 321 }
gsfan 5:78943b3945b5 322
gsfan 8:64184a968e3b 323 void GSwifi::poll () {
gsfan 11:71d67fea5ace 324 #ifndef CFG_ENABLE_RTOS
gsfan 11:71d67fea5ace 325 static int t = 0;
gsfan 13:189e04ac70bd 326 static Timer timer;
gsfan 11:71d67fea5ace 327
gsfan 12:057089026a20 328 if (_state.wm == WM_INFRASTRUCTURE && (! isAssociated()) && _state.ssid) {
gsfan 13:189e04ac70bd 329 if (t == 0 || timer.read() > CFG_RECONNECT) {
gsfan 11:71d67fea5ace 330 // Wi-Fi re-associate
gsfan 11:71d67fea5ace 331 if (_state.initialized) {
gsfan 13:189e04ac70bd 332 if (cmdWA(_state.ssid)) {
gsfan 13:189e04ac70bd 333 timer.reset();
gsfan 13:189e04ac70bd 334 timer.start();
gsfan 13:189e04ac70bd 335 t = 1;
gsfan 13:189e04ac70bd 336 } else {
gsfan 11:71d67fea5ace 337 _state.associated = true;
gsfan 13:189e04ac70bd 338 timer.stop();
gsfan 11:71d67fea5ace 339 t = 0;
gsfan 11:71d67fea5ace 340 }
gsfan 11:71d67fea5ace 341 } else {
gsfan 11:71d67fea5ace 342 if (join()) {
gsfan 13:189e04ac70bd 343 timer.reset();
gsfan 13:189e04ac70bd 344 timer.start();
gsfan 13:189e04ac70bd 345 t = 1;
gsfan 11:71d67fea5ace 346 } else {
gsfan 13:189e04ac70bd 347 timer.stop();
gsfan 13:189e04ac70bd 348 t = 0;
gsfan 11:71d67fea5ace 349 }
gsfan 11:71d67fea5ace 350 }
gsfan 11:71d67fea5ace 351 }
gsfan 11:71d67fea5ace 352 }
gsfan 11:71d67fea5ace 353 #else
gsfan 12:057089026a20 354 if (_state.wm == WM_INFRASTRUCTURE && (! isAssociated()) && _state.ssid) {
gsfan 8:64184a968e3b 355 // Wi-Fi re-associate
gsfan 11:71d67fea5ace 356 if (_state.initialized) {
gsfan 11:71d67fea5ace 357 if (!cmdWA(_state.ssid)) {
gsfan 11:71d67fea5ace 358 _state.associated = true;
gsfan 11:71d67fea5ace 359 }
gsfan 11:71d67fea5ace 360 } else {
gsfan 11:71d67fea5ace 361 join();
gsfan 5:78943b3945b5 362 }
gsfan 5:78943b3945b5 363 }
gsfan 11:71d67fea5ace 364 #endif
gsfan 12:057089026a20 365
gsfan 12:057089026a20 366 for (int i = 0; i < 16; i ++) {
gsfan 16:105b56570533 367 if (isConnected(i) && _con[i].received && _con[i].buf != NULL)
gsfan 12:057089026a20 368 if (_con[i].func != NULL && !_con[i].buf->isEmpty()) {
gsfan 12:057089026a20 369 _con[i].func(i);
gsfan 12:057089026a20 370 if (_con[i].buf->isEmpty()) {
gsfan 12:057089026a20 371 _con[i].received = false;
gsfan 12:057089026a20 372 }
gsfan 12:057089026a20 373 }
gsfan 12:057089026a20 374 }
gsfan 12:057089026a20 375
gsfan 12:057089026a20 376 #ifdef CFG_ENABLE_HTTPD
gsfan 12:057089026a20 377 httpdPoll();
gsfan 12:057089026a20 378 #endif
gsfan 5:78943b3945b5 379 }
gsfan 5:78943b3945b5 380
gsfan 11:71d67fea5ace 381 #ifdef CFG_ENABLE_RTOS
gsfan 8:64184a968e3b 382 void GSwifi::threadPoll (void const *args) {
gsfan 8:64184a968e3b 383 GSwifi * _wifi = GSwifi::getInstance();
gsfan 8:64184a968e3b 384 if (_wifi == NULL)
gsfan 8:64184a968e3b 385 error("Socket constructor error: no wifly instance available!\r\n");
gsfan 5:78943b3945b5 386
gsfan 8:64184a968e3b 387 INFO("threadPoll");
gsfan 8:64184a968e3b 388 for (;;) {
gsfan 19:d035412a7803 389 _wifi->poll();
gsfan 19:d035412a7803 390 #ifdef CFG_ENABLE_HTTPD
gsfan 19:d035412a7803 391 if (_wifi->isAssociated()) {
gsfan 19:d035412a7803 392 Thread::wait(1);
gsfan 19:d035412a7803 393 }
gsfan 19:d035412a7803 394 #else
gsfan 8:64184a968e3b 395 Thread::signal_wait(1);
gsfan 8:64184a968e3b 396 Thread::wait(1000);
gsfan 8:64184a968e3b 397 INFO("disassociated");
gsfan 19:d035412a7803 398 #endif
gsfan 12:057089026a20 399 while (!_wifi->isAssociated()){
gsfan 8:64184a968e3b 400 _wifi->poll();
gsfan 13:189e04ac70bd 401 Thread::wait(CFG_RECONNECT * 1000);
gsfan 5:78943b3945b5 402 }
gsfan 5:78943b3945b5 403 }
gsfan 5:78943b3945b5 404 }
gsfan 8:64184a968e3b 405 #endif
gsfan 5:78943b3945b5 406
gsfan 12:057089026a20 407 GSwifi::Status GSwifi::getStatus () {
gsfan 12:057089026a20 408 return _state.status;
gsfan 12:057089026a20 409 }
gsfan 12:057089026a20 410
gsfan 12:057089026a20 411 int GSwifi::setMacAddress (const char *mac) {
gsfan 12:057089026a20 412 if (cmdNMAC(mac)) return -1;
gsfan 12:057089026a20 413 strncpy(_state.mac, mac, sizeof(_state.mac));
gsfan 12:057089026a20 414 return 0;
gsfan 12:057089026a20 415 }
gsfan 12:057089026a20 416
gsfan 12:057089026a20 417 int GSwifi::getMacAddress (char *mac) {
gsfan 12:057089026a20 418 if (cmdNMAC()) return -1;
gsfan 12:057089026a20 419 strcpy(mac, _state.mac);
gsfan 12:057089026a20 420 return 0;
gsfan 12:057089026a20 421 }
gsfan 12:057089026a20 422
gsfan 11:71d67fea5ace 423 int GSwifi::setAddress (const char *name) {
gsfan 11:71d67fea5ace 424 _state.dhcp = true;
gsfan 11:71d67fea5ace 425 strncpy(_state.name, name, sizeof(_state.name));
gsfan 11:71d67fea5ace 426 return 0;
gsfan 11:71d67fea5ace 427 }
gsfan 11:71d67fea5ace 428
gsfan 15:086d1a33a197 429 int GSwifi::setAddress (const char *ip, const char *netmask, const char *gateway, const char *dns, const char *name) {
gsfan 11:71d67fea5ace 430 _state.dhcp = false;
gsfan 11:71d67fea5ace 431 strncpy(_state.ip, ip, sizeof(_state.ip));
gsfan 11:71d67fea5ace 432 strncpy(_state.netmask, netmask, sizeof(_state.netmask));
gsfan 11:71d67fea5ace 433 strncpy(_state.gateway, gateway, sizeof(_state.gateway));
gsfan 15:086d1a33a197 434 strncpy(_state.nameserver, dns, sizeof(_state.nameserver));
gsfan 11:71d67fea5ace 435 strncpy(_state.name, name, sizeof(_state.name));
gsfan 11:71d67fea5ace 436 return 0;
gsfan 11:71d67fea5ace 437 }
gsfan 11:71d67fea5ace 438
gsfan 11:71d67fea5ace 439 int GSwifi::getAddress (char *ip, char *netmask, char *gateway) {
gsfan 11:71d67fea5ace 440 strcpy(ip, _state.ip);
gsfan 11:71d67fea5ace 441 strcpy(netmask, _state.netmask);
gsfan 11:71d67fea5ace 442 strcpy(gateway, _state.gateway);
gsfan 11:71d67fea5ace 443 return 0;
gsfan 11:71d67fea5ace 444 }
gsfan 11:71d67fea5ace 445
gsfan 8:64184a968e3b 446 int GSwifi::setSsid (Security sec, const char *ssid, const char *phrase) {
gsfan 8:64184a968e3b 447 int i;
gsfan 5:78943b3945b5 448
gsfan 8:64184a968e3b 449 _state.sec = sec;
gsfan 11:71d67fea5ace 450
gsfan 8:64184a968e3b 451 // change all ' ' in '$' in the ssid and the passphrase
gsfan 8:64184a968e3b 452 strncpy(_state.ssid, ssid, sizeof(_state.ssid));
gsfan 8:64184a968e3b 453 for (i = 0; i < strlen(_state.ssid); i++) {
gsfan 8:64184a968e3b 454 if (_state.ssid[i] == ' ')
gsfan 8:64184a968e3b 455 _state.ssid[i] = '$';
gsfan 5:78943b3945b5 456 }
gsfan 11:71d67fea5ace 457
gsfan 11:71d67fea5ace 458 if (sec == SEC_WEP) {
gsfan 11:71d67fea5ace 459 if (strlen(phrase) == 5 || strlen(phrase) == 13) {
gsfan 11:71d67fea5ace 460 for (i = 0; i < strlen(phrase); i++) {
gsfan 11:71d67fea5ace 461 _state.pass[i * 2] = '0' + ((phrase[i] >> 4) & 0x0f);
gsfan 11:71d67fea5ace 462 _state.pass[i * 2 + 1] = '0' + (phrase[i + 1] & 0x0f);
gsfan 11:71d67fea5ace 463 }
gsfan 11:71d67fea5ace 464 } else {
gsfan 11:71d67fea5ace 465 strncpy(_state.pass, phrase, strlen(phrase));
gsfan 11:71d67fea5ace 466 }
gsfan 11:71d67fea5ace 467 } else {
gsfan 11:71d67fea5ace 468 strncpy(_state.pass, phrase, sizeof(_state.pass));
gsfan 11:71d67fea5ace 469 }
gsfan 8:64184a968e3b 470 for (i = 0; i < strlen(_state.pass); i++) {
gsfan 8:64184a968e3b 471 if (_state.pass[i] == ' ')
gsfan 8:64184a968e3b 472 _state.pass[i] = '$';
gsfan 5:78943b3945b5 473 }
gsfan 5:78943b3945b5 474 return 0;
gsfan 5:78943b3945b5 475 }
gsfan 5:78943b3945b5 476