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
GainSpan Wi-Fi library
The GS1011/GS2100 is an ultra low power 802.11b wireless module from GainSpan.
mbed RTOS supported.
- about this library: http://mbed.org/users/gsfan/notebook/GSwifiInterface/
- about Wi-Fi module: http://mbed.org/users/gsfan/notebook/gainspan_wifi/
ゲインスパン Wi-Fi モジュール ライブラリ
ゲインスパン社の低電力 Wi-Fiモジュール(無線LAN) GS1011/GS2100 シリーズ用のライブラリです。
mbed RTOS に対応しています。(mbed2.0)
- このライブラリについて: http://mbed.org/users/gsfan/notebook/gainspan_wifi/
- Wi-FIモジュールについて: http://mbed.org/users/gsfan/notebook/gainspan_wifi/
- UARTコマンド、SPIデータインターフェースに対応しました。(2019/09)
GSwifi/GSwifi.cpp@13:189e04ac70bd, 2013-11-26 (annotated)
- Committer:
- gsfan
- Date:
- Tue Nov 26 09:06:35 2013 +0000
- Revision:
- 13:189e04ac70bd
- Parent:
- 12:057089026a20
- Child:
- 15:086d1a33a197
fix WREGDOMAIN, NDHCP, reconnect
Who changed what in which revision?
User | Revision | Line number | New 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 | 8:64184a968e3b | 37 | for (int i = 0; i < 16; 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 | 8:64184a968e3b | 83 | cmdWAUTH(_state.sec); |
gsfan | 11:71d67fea5ace | 84 | if (_state.sec != SEC_NONE) { |
gsfan | 8:64184a968e3b | 85 | cmdWWEP(1, _state.pass); |
gsfan | 5:78943b3945b5 | 86 | wait_ms(100); |
gsfan | 5:78943b3945b5 | 87 | } |
gsfan | 11:71d67fea5ace | 88 | for (i = 0; i < CFG_TRYJOIN; i ++) { |
gsfan | 11:71d67fea5ace | 89 | r = cmdWA(_state.ssid); |
gsfan | 11:71d67fea5ace | 90 | if (!r) break; |
gsfan | 8:64184a968e3b | 91 | DBG("retry\r\n"); |
gsfan | 8:64184a968e3b | 92 | wait_ms(1000); |
gsfan | 5:78943b3945b5 | 93 | } |
gsfan | 5:78943b3945b5 | 94 | break; |
gsfan | 5:78943b3945b5 | 95 | case SEC_WPA_PSK: |
gsfan | 5:78943b3945b5 | 96 | case SEC_WPA2_PSK: |
gsfan | 13:189e04ac70bd | 97 | cmdNDHCP(_state.dhcp, _state.name, DEFAULT_WAIT_RESP_TIMEOUT); |
gsfan | 8:64184a968e3b | 98 | cmdWAUTH(0); |
gsfan | 8:64184a968e3b | 99 | cmdWPAPSK(_state.ssid, _state.pass); |
gsfan | 5:78943b3945b5 | 100 | wait_ms(100); |
gsfan | 11:71d67fea5ace | 101 | for (i = 0; i < CFG_TRYJOIN; i ++) { |
gsfan | 11:71d67fea5ace | 102 | r = cmdWA(_state.ssid); |
gsfan | 11:71d67fea5ace | 103 | if (!r) break; |
gsfan | 8:64184a968e3b | 104 | DBG("retry\r\n"); |
gsfan | 8:64184a968e3b | 105 | wait_ms(1000); |
gsfan | 5:78943b3945b5 | 106 | } |
gsfan | 5:78943b3945b5 | 107 | break; |
gsfan | 8:64184a968e3b | 108 | case SEC_WPA_ENT: |
gsfan | 8:64184a968e3b | 109 | case SEC_WPA2_ENT: |
gsfan | 8:64184a968e3b | 110 | cmdWAUTH(0); |
gsfan | 8:64184a968e3b | 111 | DBG("Can't support security\r\n"); |
gsfan | 8:64184a968e3b | 112 | break; |
gsfan | 5:78943b3945b5 | 113 | case SEC_WPS_BUTTON: |
gsfan | 8:64184a968e3b | 114 | cmdNDHCP(false); |
gsfan | 8:64184a968e3b | 115 | cmdWAUTH(0); |
gsfan | 8:64184a968e3b | 116 | r = cmdWWPS(true); |
gsfan | 8:64184a968e3b | 117 | if (r) break; |
gsfan | 8:64184a968e3b | 118 | if (_state.dhcp) { |
gsfan | 8:64184a968e3b | 119 | r = cmdNDHCP(_state.dhcp, _state.name); |
gsfan | 5:78943b3945b5 | 120 | } |
gsfan | 8:64184a968e3b | 121 | cmdWSTATUS(); |
gsfan | 8:64184a968e3b | 122 | cmdWPAPSK(_state.ssid, _state.pass); |
gsfan | 8:64184a968e3b | 123 | break; |
gsfan | 8:64184a968e3b | 124 | case SEC_WPS_PIN: |
gsfan | 8:64184a968e3b | 125 | cmdNDHCP(false); |
gsfan | 8:64184a968e3b | 126 | cmdWAUTH(0); |
gsfan | 8:64184a968e3b | 127 | r = cmdWWPS(true, _state.pass); |
gsfan | 8:64184a968e3b | 128 | if (r) break; |
gsfan | 8:64184a968e3b | 129 | if (_state.dhcp) { |
gsfan | 8:64184a968e3b | 130 | r = cmdNDHCP(_state.dhcp, _state.name); |
gsfan | 5:78943b3945b5 | 131 | } |
gsfan | 8:64184a968e3b | 132 | cmdWSTATUS(); |
gsfan | 8:64184a968e3b | 133 | cmdWPAPSK(_state.ssid, _state.pass); |
gsfan | 5:78943b3945b5 | 134 | break; |
gsfan | 5:78943b3945b5 | 135 | default: |
gsfan | 5:78943b3945b5 | 136 | DBG("Can't use security\r\n"); |
gsfan | 5:78943b3945b5 | 137 | break; |
gsfan | 5:78943b3945b5 | 138 | } |
gsfan | 5:78943b3945b5 | 139 | |
gsfan | 8:64184a968e3b | 140 | if (!r) { |
gsfan | 11:71d67fea5ace | 141 | // connected |
gsfan | 8:64184a968e3b | 142 | if (! _state.dhcp) { |
gsfan | 8:64184a968e3b | 143 | cmdDNSSET(_state.nameserver); |
gsfan | 5:78943b3945b5 | 144 | } |
gsfan | 5:78943b3945b5 | 145 | _state.associated = true; |
gsfan | 11:71d67fea5ace | 146 | #ifdef CFG_ENABLE_RTOS |
gsfan | 8:64184a968e3b | 147 | _threadPoll = new Thread(&threadPoll); |
gsfan | 8:64184a968e3b | 148 | // _threadPoll = new Thread(&threadPoll, NULL, osPriorityLow); |
gsfan | 8:64184a968e3b | 149 | #endif |
gsfan | 5:78943b3945b5 | 150 | } |
gsfan | 5:78943b3945b5 | 151 | return r; |
gsfan | 5:78943b3945b5 | 152 | } |
gsfan | 5:78943b3945b5 | 153 | |
gsfan | 11:71d67fea5ace | 154 | int GSwifi::adhock () |
gsfan | 5:78943b3945b5 | 155 | { |
gsfan | 8:64184a968e3b | 156 | bool r = -1; |
gsfan | 5:78943b3945b5 | 157 | |
gsfan | 8:64184a968e3b | 158 | _state.wm = WM_ADHOCK; |
gsfan | 11:71d67fea5ace | 159 | _state.initialized = true; |
gsfan | 8:64184a968e3b | 160 | clearFlags(); |
gsfan | 12:057089026a20 | 161 | _state.mode = MODE_COMMAND; |
gsfan | 8:64184a968e3b | 162 | sendCommand(NULL, RES_NULL, 0); |
gsfan | 8:64184a968e3b | 163 | if (cmdE(false)) return -1; |
gsfan | 8:64184a968e3b | 164 | if (_flow) { |
gsfan | 8:64184a968e3b | 165 | cmdR(true); |
gsfan | 5:78943b3945b5 | 166 | } |
gsfan | 8:64184a968e3b | 167 | if (cmdNMAC()) return -1; |
gsfan | 8:64184a968e3b | 168 | cmdBDATA(true); |
gsfan | 13:189e04ac70bd | 169 | cmdWREGDOMAIN(CFG_WREGDOMAIN); |
gsfan | 8:64184a968e3b | 170 | cmdWM(1); // adhock |
gsfan | 8:64184a968e3b | 171 | wait_ms(100); |
gsfan | 8:64184a968e3b | 172 | |
gsfan | 8:64184a968e3b | 173 | cmdNDHCP(false); |
gsfan | 8:64184a968e3b | 174 | if (! _state.dhcp) { |
gsfan | 8:64184a968e3b | 175 | cmdNSET(_state.ip, _state.netmask, _state.ip); |
gsfan | 5:78943b3945b5 | 176 | } |
gsfan | 5:78943b3945b5 | 177 | |
gsfan | 11:71d67fea5ace | 178 | switch (_state.sec) { |
gsfan | 8:64184a968e3b | 179 | case SEC_NONE: |
gsfan | 8:64184a968e3b | 180 | case SEC_OPEN: |
gsfan | 8:64184a968e3b | 181 | case SEC_WEP: |
gsfan | 8:64184a968e3b | 182 | cmdWAUTH(_state.sec); |
gsfan | 11:71d67fea5ace | 183 | if (_state.sec != SEC_NONE) { |
gsfan | 8:64184a968e3b | 184 | cmdWWEP(1, _state.pass); |
gsfan | 8:64184a968e3b | 185 | wait_ms(100); |
gsfan | 8:64184a968e3b | 186 | } |
gsfan | 8:64184a968e3b | 187 | r = cmdWA(_state.ssid); |
gsfan | 8:64184a968e3b | 188 | break; |
gsfan | 8:64184a968e3b | 189 | default: |
gsfan | 8:64184a968e3b | 190 | DBG("Can't use security\r\n"); |
gsfan | 8:64184a968e3b | 191 | break; |
gsfan | 5:78943b3945b5 | 192 | } |
gsfan | 5:78943b3945b5 | 193 | |
gsfan | 8:64184a968e3b | 194 | if (!r) { |
gsfan | 11:71d67fea5ace | 195 | // connected |
gsfan | 8:64184a968e3b | 196 | _state.associated = true; |
gsfan | 8:64184a968e3b | 197 | } |
gsfan | 8:64184a968e3b | 198 | return r; |
gsfan | 5:78943b3945b5 | 199 | } |
gsfan | 5:78943b3945b5 | 200 | |
gsfan | 11:71d67fea5ace | 201 | int GSwifi::limitedap () |
gsfan | 5:78943b3945b5 | 202 | { |
gsfan | 8:64184a968e3b | 203 | bool r = -1; |
gsfan | 8:64184a968e3b | 204 | |
gsfan | 8:64184a968e3b | 205 | _state.wm = WM_LIMITEDAP; |
gsfan | 11:71d67fea5ace | 206 | _state.initialized = true; |
gsfan | 8:64184a968e3b | 207 | if (!strlen(_state.name)) { |
gsfan | 8:64184a968e3b | 208 | strncpy(_state.name, CFG_DNSNAME, sizeof(_state.name)); |
gsfan | 8:64184a968e3b | 209 | } |
gsfan | 8:64184a968e3b | 210 | clearFlags(); |
gsfan | 12:057089026a20 | 211 | _state.mode = MODE_COMMAND; |
gsfan | 8:64184a968e3b | 212 | sendCommand(NULL, RES_NULL, 0); |
gsfan | 8:64184a968e3b | 213 | if (cmdE(false)) return -1; |
gsfan | 8:64184a968e3b | 214 | if (_flow) { |
gsfan | 8:64184a968e3b | 215 | cmdR(true); |
gsfan | 8:64184a968e3b | 216 | } |
gsfan | 8:64184a968e3b | 217 | if (cmdNMAC()) return -1; |
gsfan | 8:64184a968e3b | 218 | cmdBDATA(true); |
gsfan | 13:189e04ac70bd | 219 | cmdWREGDOMAIN(CFG_WREGDOMAIN); |
gsfan | 8:64184a968e3b | 220 | cmdWM(2); // limitedap |
gsfan | 8:64184a968e3b | 221 | wait_ms(100); |
gsfan | 8:64184a968e3b | 222 | |
gsfan | 8:64184a968e3b | 223 | cmdNDHCP(false); |
gsfan | 8:64184a968e3b | 224 | if (! _state.dhcp) { |
gsfan | 8:64184a968e3b | 225 | cmdNSET(_state.ip, _state.netmask, _state.ip); |
gsfan | 8:64184a968e3b | 226 | cmdDNSSET(_state.ip); |
gsfan | 5:78943b3945b5 | 227 | } |
gsfan | 8:64184a968e3b | 228 | cmdDHCPSRVR(true); |
gsfan | 8:64184a968e3b | 229 | cmdDNS(true, _state.name); |
gsfan | 8:64184a968e3b | 230 | |
gsfan | 11:71d67fea5ace | 231 | switch (_state.sec) { |
gsfan | 8:64184a968e3b | 232 | case SEC_NONE: |
gsfan | 8:64184a968e3b | 233 | case SEC_OPEN: |
gsfan | 8:64184a968e3b | 234 | case SEC_WEP: |
gsfan | 8:64184a968e3b | 235 | cmdWAUTH(_state.sec); |
gsfan | 11:71d67fea5ace | 236 | if (_state.sec != SEC_NONE) { |
gsfan | 8:64184a968e3b | 237 | cmdWWEP(1, _state.pass); |
gsfan | 8:64184a968e3b | 238 | wait_ms(100); |
gsfan | 8:64184a968e3b | 239 | } |
gsfan | 8:64184a968e3b | 240 | r = cmdWA(_state.ssid); |
gsfan | 8:64184a968e3b | 241 | break; |
gsfan | 8:64184a968e3b | 242 | default: |
gsfan | 8:64184a968e3b | 243 | DBG("Can't use security\r\n"); |
gsfan | 8:64184a968e3b | 244 | break; |
gsfan | 8:64184a968e3b | 245 | } |
gsfan | 8:64184a968e3b | 246 | |
gsfan | 8:64184a968e3b | 247 | if (!r) { |
gsfan | 11:71d67fea5ace | 248 | // connected |
gsfan | 8:64184a968e3b | 249 | _state.associated = true; |
gsfan | 8:64184a968e3b | 250 | } |
gsfan | 8:64184a968e3b | 251 | return r; |
gsfan | 5:78943b3945b5 | 252 | } |
gsfan | 5:78943b3945b5 | 253 | |
gsfan | 12:057089026a20 | 254 | int GSwifi::dissociate() |
gsfan | 5:78943b3945b5 | 255 | { |
gsfan | 8:64184a968e3b | 256 | int i; |
gsfan | 5:78943b3945b5 | 257 | |
gsfan | 5:78943b3945b5 | 258 | // if already disconnected, return |
gsfan | 5:78943b3945b5 | 259 | if (!_state.associated) |
gsfan | 8:64184a968e3b | 260 | return 0; |
gsfan | 5:78943b3945b5 | 261 | |
gsfan | 11:71d67fea5ace | 262 | #ifdef CFG_ENABLE_RTOS |
gsfan | 8:64184a968e3b | 263 | if (_threadPoll) { |
gsfan | 8:64184a968e3b | 264 | _threadPoll->terminate(); |
gsfan | 8:64184a968e3b | 265 | delete _threadPoll; |
gsfan | 8:64184a968e3b | 266 | } |
gsfan | 8:64184a968e3b | 267 | #endif |
gsfan | 8:64184a968e3b | 268 | for (i = 0; i < 16; i ++) { |
gsfan | 8:64184a968e3b | 269 | if (_con[i].buf) { |
gsfan | 5:78943b3945b5 | 270 | _con[i].buf->flush(); |
gsfan | 8:64184a968e3b | 271 | } |
gsfan | 5:78943b3945b5 | 272 | } |
gsfan | 8:64184a968e3b | 273 | cmdNCLOSEALL(); |
gsfan | 8:64184a968e3b | 274 | cmdWD(); |
gsfan | 8:64184a968e3b | 275 | cmdNDHCP(false); |
gsfan | 5:78943b3945b5 | 276 | wait_ms(100); |
gsfan | 5:78943b3945b5 | 277 | |
gsfan | 5:78943b3945b5 | 278 | _state.associated = false; |
gsfan | 8:64184a968e3b | 279 | return 0; |
gsfan | 5:78943b3945b5 | 280 | |
gsfan | 5:78943b3945b5 | 281 | } |
gsfan | 5:78943b3945b5 | 282 | |
gsfan | 12:057089026a20 | 283 | bool GSwifi::isAssociated() |
gsfan | 5:78943b3945b5 | 284 | { |
gsfan | 8:64184a968e3b | 285 | return _state.associated; |
gsfan | 5:78943b3945b5 | 286 | } |
gsfan | 5:78943b3945b5 | 287 | |
gsfan | 8:64184a968e3b | 288 | void GSwifi::poll () { |
gsfan | 11:71d67fea5ace | 289 | #ifndef CFG_ENABLE_RTOS |
gsfan | 11:71d67fea5ace | 290 | static int t = 0; |
gsfan | 13:189e04ac70bd | 291 | static Timer timer; |
gsfan | 11:71d67fea5ace | 292 | |
gsfan | 12:057089026a20 | 293 | if (_state.wm == WM_INFRASTRUCTURE && (! isAssociated()) && _state.ssid) { |
gsfan | 13:189e04ac70bd | 294 | if (t == 0 || timer.read() > CFG_RECONNECT) { |
gsfan | 11:71d67fea5ace | 295 | // Wi-Fi re-associate |
gsfan | 11:71d67fea5ace | 296 | if (_state.initialized) { |
gsfan | 13:189e04ac70bd | 297 | if (cmdWA(_state.ssid)) { |
gsfan | 13:189e04ac70bd | 298 | timer.reset(); |
gsfan | 13:189e04ac70bd | 299 | timer.start(); |
gsfan | 13:189e04ac70bd | 300 | t = 1; |
gsfan | 13:189e04ac70bd | 301 | } else { |
gsfan | 11:71d67fea5ace | 302 | _state.associated = true; |
gsfan | 13:189e04ac70bd | 303 | timer.stop(); |
gsfan | 11:71d67fea5ace | 304 | t = 0; |
gsfan | 11:71d67fea5ace | 305 | } |
gsfan | 11:71d67fea5ace | 306 | } else { |
gsfan | 11:71d67fea5ace | 307 | if (join()) { |
gsfan | 13:189e04ac70bd | 308 | timer.reset(); |
gsfan | 13:189e04ac70bd | 309 | timer.start(); |
gsfan | 13:189e04ac70bd | 310 | t = 1; |
gsfan | 11:71d67fea5ace | 311 | } else { |
gsfan | 13:189e04ac70bd | 312 | timer.stop(); |
gsfan | 13:189e04ac70bd | 313 | t = 0; |
gsfan | 11:71d67fea5ace | 314 | } |
gsfan | 11:71d67fea5ace | 315 | } |
gsfan | 11:71d67fea5ace | 316 | } |
gsfan | 11:71d67fea5ace | 317 | } |
gsfan | 11:71d67fea5ace | 318 | #else |
gsfan | 12:057089026a20 | 319 | if (_state.wm == WM_INFRASTRUCTURE && (! isAssociated()) && _state.ssid) { |
gsfan | 8:64184a968e3b | 320 | // Wi-Fi re-associate |
gsfan | 11:71d67fea5ace | 321 | if (_state.initialized) { |
gsfan | 11:71d67fea5ace | 322 | if (!cmdWA(_state.ssid)) { |
gsfan | 11:71d67fea5ace | 323 | _state.associated = true; |
gsfan | 11:71d67fea5ace | 324 | } |
gsfan | 11:71d67fea5ace | 325 | } else { |
gsfan | 11:71d67fea5ace | 326 | join(); |
gsfan | 5:78943b3945b5 | 327 | } |
gsfan | 5:78943b3945b5 | 328 | } |
gsfan | 11:71d67fea5ace | 329 | #endif |
gsfan | 12:057089026a20 | 330 | |
gsfan | 12:057089026a20 | 331 | for (int i = 0; i < 16; i ++) { |
gsfan | 12:057089026a20 | 332 | if (isConnected(i) && _con[i].received && _con[i].buf) |
gsfan | 12:057089026a20 | 333 | if (_con[i].func != NULL && !_con[i].buf->isEmpty()) { |
gsfan | 12:057089026a20 | 334 | _con[i].func(i); |
gsfan | 12:057089026a20 | 335 | if (_con[i].buf->isEmpty()) { |
gsfan | 12:057089026a20 | 336 | _con[i].received = false; |
gsfan | 12:057089026a20 | 337 | } |
gsfan | 12:057089026a20 | 338 | } |
gsfan | 12:057089026a20 | 339 | } |
gsfan | 12:057089026a20 | 340 | |
gsfan | 12:057089026a20 | 341 | #ifdef CFG_ENABLE_HTTPD |
gsfan | 12:057089026a20 | 342 | httpdPoll(); |
gsfan | 12:057089026a20 | 343 | #endif |
gsfan | 5:78943b3945b5 | 344 | } |
gsfan | 5:78943b3945b5 | 345 | |
gsfan | 11:71d67fea5ace | 346 | #ifdef CFG_ENABLE_RTOS |
gsfan | 8:64184a968e3b | 347 | void GSwifi::threadPoll (void const *args) { |
gsfan | 8:64184a968e3b | 348 | GSwifi * _wifi = GSwifi::getInstance(); |
gsfan | 8:64184a968e3b | 349 | if (_wifi == NULL) |
gsfan | 8:64184a968e3b | 350 | error("Socket constructor error: no wifly instance available!\r\n"); |
gsfan | 5:78943b3945b5 | 351 | |
gsfan | 8:64184a968e3b | 352 | INFO("threadPoll"); |
gsfan | 8:64184a968e3b | 353 | for (;;) { |
gsfan | 8:64184a968e3b | 354 | Thread::signal_wait(1); |
gsfan | 8:64184a968e3b | 355 | Thread::wait(1000); |
gsfan | 8:64184a968e3b | 356 | INFO("disassociated"); |
gsfan | 12:057089026a20 | 357 | while (!_wifi->isAssociated()){ |
gsfan | 8:64184a968e3b | 358 | _wifi->poll(); |
gsfan | 13:189e04ac70bd | 359 | Thread::wait(CFG_RECONNECT * 1000); |
gsfan | 5:78943b3945b5 | 360 | } |
gsfan | 5:78943b3945b5 | 361 | } |
gsfan | 5:78943b3945b5 | 362 | } |
gsfan | 8:64184a968e3b | 363 | #endif |
gsfan | 5:78943b3945b5 | 364 | |
gsfan | 12:057089026a20 | 365 | GSwifi::Status GSwifi::getStatus () { |
gsfan | 12:057089026a20 | 366 | return _state.status; |
gsfan | 12:057089026a20 | 367 | } |
gsfan | 12:057089026a20 | 368 | |
gsfan | 12:057089026a20 | 369 | int GSwifi::setMacAddress (const char *mac) { |
gsfan | 12:057089026a20 | 370 | if (cmdNMAC(mac)) return -1; |
gsfan | 12:057089026a20 | 371 | strncpy(_state.mac, mac, sizeof(_state.mac)); |
gsfan | 12:057089026a20 | 372 | return 0; |
gsfan | 12:057089026a20 | 373 | } |
gsfan | 12:057089026a20 | 374 | |
gsfan | 12:057089026a20 | 375 | int GSwifi::getMacAddress (char *mac) { |
gsfan | 12:057089026a20 | 376 | if (cmdNMAC()) return -1; |
gsfan | 12:057089026a20 | 377 | strcpy(mac, _state.mac); |
gsfan | 12:057089026a20 | 378 | return 0; |
gsfan | 12:057089026a20 | 379 | } |
gsfan | 12:057089026a20 | 380 | |
gsfan | 11:71d67fea5ace | 381 | int GSwifi::setAddress (const char *name) { |
gsfan | 11:71d67fea5ace | 382 | _state.dhcp = true; |
gsfan | 11:71d67fea5ace | 383 | strncpy(_state.name, name, sizeof(_state.name)); |
gsfan | 11:71d67fea5ace | 384 | return 0; |
gsfan | 11:71d67fea5ace | 385 | } |
gsfan | 11:71d67fea5ace | 386 | |
gsfan | 11:71d67fea5ace | 387 | int GSwifi::setAddress (const char *ip, const char *netmask, const char *gateway, const char *name) { |
gsfan | 11:71d67fea5ace | 388 | _state.dhcp = false; |
gsfan | 11:71d67fea5ace | 389 | strncpy(_state.ip, ip, sizeof(_state.ip)); |
gsfan | 11:71d67fea5ace | 390 | strncpy(_state.netmask, netmask, sizeof(_state.netmask)); |
gsfan | 11:71d67fea5ace | 391 | strncpy(_state.gateway, gateway, sizeof(_state.gateway)); |
gsfan | 11:71d67fea5ace | 392 | strncpy(_state.name, name, sizeof(_state.name)); |
gsfan | 11:71d67fea5ace | 393 | return 0; |
gsfan | 11:71d67fea5ace | 394 | } |
gsfan | 11:71d67fea5ace | 395 | |
gsfan | 11:71d67fea5ace | 396 | int GSwifi::getAddress (char *ip, char *netmask, char *gateway) { |
gsfan | 11:71d67fea5ace | 397 | strcpy(ip, _state.ip); |
gsfan | 11:71d67fea5ace | 398 | strcpy(netmask, _state.netmask); |
gsfan | 11:71d67fea5ace | 399 | strcpy(gateway, _state.gateway); |
gsfan | 11:71d67fea5ace | 400 | return 0; |
gsfan | 11:71d67fea5ace | 401 | } |
gsfan | 11:71d67fea5ace | 402 | |
gsfan | 8:64184a968e3b | 403 | int GSwifi::setSsid (Security sec, const char *ssid, const char *phrase) { |
gsfan | 8:64184a968e3b | 404 | int i; |
gsfan | 5:78943b3945b5 | 405 | |
gsfan | 8:64184a968e3b | 406 | _state.sec = sec; |
gsfan | 11:71d67fea5ace | 407 | |
gsfan | 8:64184a968e3b | 408 | // change all ' ' in '$' in the ssid and the passphrase |
gsfan | 8:64184a968e3b | 409 | strncpy(_state.ssid, ssid, sizeof(_state.ssid)); |
gsfan | 8:64184a968e3b | 410 | for (i = 0; i < strlen(_state.ssid); i++) { |
gsfan | 8:64184a968e3b | 411 | if (_state.ssid[i] == ' ') |
gsfan | 8:64184a968e3b | 412 | _state.ssid[i] = '$'; |
gsfan | 5:78943b3945b5 | 413 | } |
gsfan | 11:71d67fea5ace | 414 | |
gsfan | 11:71d67fea5ace | 415 | if (sec == SEC_WEP) { |
gsfan | 11:71d67fea5ace | 416 | if (strlen(phrase) == 5 || strlen(phrase) == 13) { |
gsfan | 11:71d67fea5ace | 417 | for (i = 0; i < strlen(phrase); i++) { |
gsfan | 11:71d67fea5ace | 418 | _state.pass[i * 2] = '0' + ((phrase[i] >> 4) & 0x0f); |
gsfan | 11:71d67fea5ace | 419 | _state.pass[i * 2 + 1] = '0' + (phrase[i + 1] & 0x0f); |
gsfan | 11:71d67fea5ace | 420 | } |
gsfan | 11:71d67fea5ace | 421 | } else { |
gsfan | 11:71d67fea5ace | 422 | strncpy(_state.pass, phrase, strlen(phrase)); |
gsfan | 11:71d67fea5ace | 423 | } |
gsfan | 11:71d67fea5ace | 424 | } else { |
gsfan | 11:71d67fea5ace | 425 | strncpy(_state.pass, phrase, sizeof(_state.pass)); |
gsfan | 11:71d67fea5ace | 426 | } |
gsfan | 8:64184a968e3b | 427 | for (i = 0; i < strlen(_state.pass); i++) { |
gsfan | 8:64184a968e3b | 428 | if (_state.pass[i] == ' ') |
gsfan | 8:64184a968e3b | 429 | _state.pass[i] = '$'; |
gsfan | 5:78943b3945b5 | 430 | } |
gsfan | 5:78943b3945b5 | 431 | return 0; |
gsfan | 5:78943b3945b5 | 432 | } |
gsfan | 5:78943b3945b5 | 433 |