private fork
Fork of GSwifiInterface by
GSwifi/GSwifi.cpp@16:105b56570533, 2014-03-04 (annotated)
- Committer:
- gsfan
- Date:
- Tue Mar 04 15:01:38 2014 +0000
- Revision:
- 16:105b56570533
- Parent:
- 15:086d1a33a197
- Child:
- 17:c30ed7b1536d
bug fix (cause of hard fault)
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 | 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 | 5:78943b3945b5 | 112 | } |
gsfan | 5:78943b3945b5 | 113 | break; |
gsfan | 8:64184a968e3b | 114 | case SEC_WPA_ENT: |
gsfan | 8:64184a968e3b | 115 | case SEC_WPA2_ENT: |
gsfan | 8:64184a968e3b | 116 | cmdWAUTH(0); |
gsfan | 8:64184a968e3b | 117 | DBG("Can't support security\r\n"); |
gsfan | 8:64184a968e3b | 118 | break; |
gsfan | 5:78943b3945b5 | 119 | case SEC_WPS_BUTTON: |
gsfan | 8:64184a968e3b | 120 | cmdNDHCP(false); |
gsfan | 15:086d1a33a197 | 121 | if (! _state.dhcp) { |
gsfan | 15:086d1a33a197 | 122 | cmdNSET(_state.ip, _state.netmask, _state.ip); |
gsfan | 15:086d1a33a197 | 123 | } |
gsfan | 8:64184a968e3b | 124 | cmdWAUTH(0); |
gsfan | 8:64184a968e3b | 125 | r = cmdWWPS(true); |
gsfan | 8:64184a968e3b | 126 | if (r) break; |
gsfan | 8:64184a968e3b | 127 | if (_state.dhcp) { |
gsfan | 8:64184a968e3b | 128 | r = cmdNDHCP(_state.dhcp, _state.name); |
gsfan | 5:78943b3945b5 | 129 | } |
gsfan | 8:64184a968e3b | 130 | cmdWSTATUS(); |
gsfan | 8:64184a968e3b | 131 | cmdWPAPSK(_state.ssid, _state.pass); |
gsfan | 8:64184a968e3b | 132 | break; |
gsfan | 8:64184a968e3b | 133 | case SEC_WPS_PIN: |
gsfan | 8:64184a968e3b | 134 | cmdNDHCP(false); |
gsfan | 15:086d1a33a197 | 135 | if (! _state.dhcp) { |
gsfan | 15:086d1a33a197 | 136 | cmdNSET(_state.ip, _state.netmask, _state.ip); |
gsfan | 15:086d1a33a197 | 137 | } |
gsfan | 8:64184a968e3b | 138 | cmdWAUTH(0); |
gsfan | 8:64184a968e3b | 139 | r = cmdWWPS(true, _state.pass); |
gsfan | 8:64184a968e3b | 140 | if (r) break; |
gsfan | 8:64184a968e3b | 141 | if (_state.dhcp) { |
gsfan | 8:64184a968e3b | 142 | r = cmdNDHCP(_state.dhcp, _state.name); |
gsfan | 5:78943b3945b5 | 143 | } |
gsfan | 8:64184a968e3b | 144 | cmdWSTATUS(); |
gsfan | 8:64184a968e3b | 145 | cmdWPAPSK(_state.ssid, _state.pass); |
gsfan | 5:78943b3945b5 | 146 | break; |
gsfan | 5:78943b3945b5 | 147 | default: |
gsfan | 5:78943b3945b5 | 148 | DBG("Can't use security\r\n"); |
gsfan | 5:78943b3945b5 | 149 | break; |
gsfan | 5:78943b3945b5 | 150 | } |
gsfan | 5:78943b3945b5 | 151 | |
gsfan | 8:64184a968e3b | 152 | if (!r) { |
gsfan | 11:71d67fea5ace | 153 | // connected |
gsfan | 8:64184a968e3b | 154 | if (! _state.dhcp) { |
gsfan | 8:64184a968e3b | 155 | cmdDNSSET(_state.nameserver); |
gsfan | 5:78943b3945b5 | 156 | } |
gsfan | 5:78943b3945b5 | 157 | _state.associated = true; |
gsfan | 11:71d67fea5ace | 158 | #ifdef CFG_ENABLE_RTOS |
gsfan | 8:64184a968e3b | 159 | _threadPoll = new Thread(&threadPoll); |
gsfan | 8:64184a968e3b | 160 | // _threadPoll = new Thread(&threadPoll, NULL, osPriorityLow); |
gsfan | 8:64184a968e3b | 161 | #endif |
gsfan | 5:78943b3945b5 | 162 | } |
gsfan | 5:78943b3945b5 | 163 | return r; |
gsfan | 5:78943b3945b5 | 164 | } |
gsfan | 5:78943b3945b5 | 165 | |
gsfan | 11:71d67fea5ace | 166 | int GSwifi::adhock () |
gsfan | 5:78943b3945b5 | 167 | { |
gsfan | 8:64184a968e3b | 168 | bool r = -1; |
gsfan | 5:78943b3945b5 | 169 | |
gsfan | 8:64184a968e3b | 170 | _state.wm = WM_ADHOCK; |
gsfan | 11:71d67fea5ace | 171 | _state.initialized = true; |
gsfan | 8:64184a968e3b | 172 | clearFlags(); |
gsfan | 12:057089026a20 | 173 | _state.mode = MODE_COMMAND; |
gsfan | 8:64184a968e3b | 174 | sendCommand(NULL, RES_NULL, 0); |
gsfan | 8:64184a968e3b | 175 | if (cmdE(false)) return -1; |
gsfan | 8:64184a968e3b | 176 | if (_flow) { |
gsfan | 8:64184a968e3b | 177 | cmdR(true); |
gsfan | 5:78943b3945b5 | 178 | } |
gsfan | 8:64184a968e3b | 179 | if (cmdNMAC()) return -1; |
gsfan | 8:64184a968e3b | 180 | cmdBDATA(true); |
gsfan | 13:189e04ac70bd | 181 | cmdWREGDOMAIN(CFG_WREGDOMAIN); |
gsfan | 8:64184a968e3b | 182 | cmdWM(1); // adhock |
gsfan | 8:64184a968e3b | 183 | wait_ms(100); |
gsfan | 8:64184a968e3b | 184 | |
gsfan | 8:64184a968e3b | 185 | cmdNDHCP(false); |
gsfan | 8:64184a968e3b | 186 | if (! _state.dhcp) { |
gsfan | 8:64184a968e3b | 187 | cmdNSET(_state.ip, _state.netmask, _state.ip); |
gsfan | 5:78943b3945b5 | 188 | } |
gsfan | 5:78943b3945b5 | 189 | |
gsfan | 11:71d67fea5ace | 190 | switch (_state.sec) { |
gsfan | 8:64184a968e3b | 191 | case SEC_NONE: |
gsfan | 8:64184a968e3b | 192 | case SEC_OPEN: |
gsfan | 8:64184a968e3b | 193 | case SEC_WEP: |
gsfan | 8:64184a968e3b | 194 | cmdWAUTH(_state.sec); |
gsfan | 11:71d67fea5ace | 195 | if (_state.sec != SEC_NONE) { |
gsfan | 8:64184a968e3b | 196 | cmdWWEP(1, _state.pass); |
gsfan | 8:64184a968e3b | 197 | wait_ms(100); |
gsfan | 8:64184a968e3b | 198 | } |
gsfan | 8:64184a968e3b | 199 | r = cmdWA(_state.ssid); |
gsfan | 8:64184a968e3b | 200 | break; |
gsfan | 8:64184a968e3b | 201 | default: |
gsfan | 8:64184a968e3b | 202 | DBG("Can't use security\r\n"); |
gsfan | 8:64184a968e3b | 203 | break; |
gsfan | 5:78943b3945b5 | 204 | } |
gsfan | 5:78943b3945b5 | 205 | |
gsfan | 8:64184a968e3b | 206 | if (!r) { |
gsfan | 11:71d67fea5ace | 207 | // connected |
gsfan | 8:64184a968e3b | 208 | _state.associated = true; |
gsfan | 8:64184a968e3b | 209 | } |
gsfan | 8:64184a968e3b | 210 | return r; |
gsfan | 5:78943b3945b5 | 211 | } |
gsfan | 5:78943b3945b5 | 212 | |
gsfan | 11:71d67fea5ace | 213 | int GSwifi::limitedap () |
gsfan | 5:78943b3945b5 | 214 | { |
gsfan | 8:64184a968e3b | 215 | bool r = -1; |
gsfan | 8:64184a968e3b | 216 | |
gsfan | 8:64184a968e3b | 217 | _state.wm = WM_LIMITEDAP; |
gsfan | 11:71d67fea5ace | 218 | _state.initialized = true; |
gsfan | 8:64184a968e3b | 219 | if (!strlen(_state.name)) { |
gsfan | 8:64184a968e3b | 220 | strncpy(_state.name, CFG_DNSNAME, sizeof(_state.name)); |
gsfan | 8:64184a968e3b | 221 | } |
gsfan | 8:64184a968e3b | 222 | clearFlags(); |
gsfan | 12:057089026a20 | 223 | _state.mode = MODE_COMMAND; |
gsfan | 8:64184a968e3b | 224 | sendCommand(NULL, RES_NULL, 0); |
gsfan | 8:64184a968e3b | 225 | if (cmdE(false)) return -1; |
gsfan | 8:64184a968e3b | 226 | if (_flow) { |
gsfan | 8:64184a968e3b | 227 | cmdR(true); |
gsfan | 8:64184a968e3b | 228 | } |
gsfan | 8:64184a968e3b | 229 | if (cmdNMAC()) return -1; |
gsfan | 8:64184a968e3b | 230 | cmdBDATA(true); |
gsfan | 13:189e04ac70bd | 231 | cmdWREGDOMAIN(CFG_WREGDOMAIN); |
gsfan | 8:64184a968e3b | 232 | cmdWM(2); // limitedap |
gsfan | 8:64184a968e3b | 233 | wait_ms(100); |
gsfan | 8:64184a968e3b | 234 | |
gsfan | 8:64184a968e3b | 235 | cmdNDHCP(false); |
gsfan | 8:64184a968e3b | 236 | if (! _state.dhcp) { |
gsfan | 8:64184a968e3b | 237 | cmdNSET(_state.ip, _state.netmask, _state.ip); |
gsfan | 8:64184a968e3b | 238 | cmdDNSSET(_state.ip); |
gsfan | 5:78943b3945b5 | 239 | } |
gsfan | 8:64184a968e3b | 240 | cmdDHCPSRVR(true); |
gsfan | 8:64184a968e3b | 241 | cmdDNS(true, _state.name); |
gsfan | 8:64184a968e3b | 242 | |
gsfan | 11:71d67fea5ace | 243 | switch (_state.sec) { |
gsfan | 8:64184a968e3b | 244 | case SEC_NONE: |
gsfan | 8:64184a968e3b | 245 | case SEC_OPEN: |
gsfan | 8:64184a968e3b | 246 | case SEC_WEP: |
gsfan | 8:64184a968e3b | 247 | cmdWAUTH(_state.sec); |
gsfan | 11:71d67fea5ace | 248 | if (_state.sec != SEC_NONE) { |
gsfan | 8:64184a968e3b | 249 | cmdWWEP(1, _state.pass); |
gsfan | 8:64184a968e3b | 250 | wait_ms(100); |
gsfan | 8:64184a968e3b | 251 | } |
gsfan | 8:64184a968e3b | 252 | r = cmdWA(_state.ssid); |
gsfan | 8:64184a968e3b | 253 | break; |
gsfan | 8:64184a968e3b | 254 | default: |
gsfan | 8:64184a968e3b | 255 | DBG("Can't use security\r\n"); |
gsfan | 8:64184a968e3b | 256 | break; |
gsfan | 8:64184a968e3b | 257 | } |
gsfan | 8:64184a968e3b | 258 | |
gsfan | 8:64184a968e3b | 259 | if (!r) { |
gsfan | 11:71d67fea5ace | 260 | // connected |
gsfan | 8:64184a968e3b | 261 | _state.associated = true; |
gsfan | 8:64184a968e3b | 262 | } |
gsfan | 8:64184a968e3b | 263 | return r; |
gsfan | 5:78943b3945b5 | 264 | } |
gsfan | 5:78943b3945b5 | 265 | |
gsfan | 12:057089026a20 | 266 | int GSwifi::dissociate() |
gsfan | 5:78943b3945b5 | 267 | { |
gsfan | 8:64184a968e3b | 268 | int i; |
gsfan | 5:78943b3945b5 | 269 | |
gsfan | 5:78943b3945b5 | 270 | // if already disconnected, return |
gsfan | 5:78943b3945b5 | 271 | if (!_state.associated) |
gsfan | 8:64184a968e3b | 272 | return 0; |
gsfan | 5:78943b3945b5 | 273 | |
gsfan | 11:71d67fea5ace | 274 | #ifdef CFG_ENABLE_RTOS |
gsfan | 8:64184a968e3b | 275 | if (_threadPoll) { |
gsfan | 8:64184a968e3b | 276 | _threadPoll->terminate(); |
gsfan | 8:64184a968e3b | 277 | delete _threadPoll; |
gsfan | 8:64184a968e3b | 278 | } |
gsfan | 8:64184a968e3b | 279 | #endif |
gsfan | 8:64184a968e3b | 280 | for (i = 0; i < 16; i ++) { |
gsfan | 8:64184a968e3b | 281 | if (_con[i].buf) { |
gsfan | 5:78943b3945b5 | 282 | _con[i].buf->flush(); |
gsfan | 8:64184a968e3b | 283 | } |
gsfan | 5:78943b3945b5 | 284 | } |
gsfan | 8:64184a968e3b | 285 | cmdNCLOSEALL(); |
gsfan | 8:64184a968e3b | 286 | cmdWD(); |
gsfan | 8:64184a968e3b | 287 | cmdNDHCP(false); |
gsfan | 5:78943b3945b5 | 288 | wait_ms(100); |
gsfan | 5:78943b3945b5 | 289 | |
gsfan | 5:78943b3945b5 | 290 | _state.associated = false; |
gsfan | 8:64184a968e3b | 291 | return 0; |
gsfan | 5:78943b3945b5 | 292 | |
gsfan | 5:78943b3945b5 | 293 | } |
gsfan | 5:78943b3945b5 | 294 | |
gsfan | 12:057089026a20 | 295 | bool GSwifi::isAssociated() |
gsfan | 5:78943b3945b5 | 296 | { |
gsfan | 8:64184a968e3b | 297 | return _state.associated; |
gsfan | 5:78943b3945b5 | 298 | } |
gsfan | 5:78943b3945b5 | 299 | |
gsfan | 8:64184a968e3b | 300 | void GSwifi::poll () { |
gsfan | 11:71d67fea5ace | 301 | #ifndef CFG_ENABLE_RTOS |
gsfan | 11:71d67fea5ace | 302 | static int t = 0; |
gsfan | 13:189e04ac70bd | 303 | static Timer timer; |
gsfan | 11:71d67fea5ace | 304 | |
gsfan | 12:057089026a20 | 305 | if (_state.wm == WM_INFRASTRUCTURE && (! isAssociated()) && _state.ssid) { |
gsfan | 13:189e04ac70bd | 306 | if (t == 0 || timer.read() > CFG_RECONNECT) { |
gsfan | 11:71d67fea5ace | 307 | // Wi-Fi re-associate |
gsfan | 11:71d67fea5ace | 308 | if (_state.initialized) { |
gsfan | 13:189e04ac70bd | 309 | if (cmdWA(_state.ssid)) { |
gsfan | 13:189e04ac70bd | 310 | timer.reset(); |
gsfan | 13:189e04ac70bd | 311 | timer.start(); |
gsfan | 13:189e04ac70bd | 312 | t = 1; |
gsfan | 13:189e04ac70bd | 313 | } else { |
gsfan | 11:71d67fea5ace | 314 | _state.associated = true; |
gsfan | 13:189e04ac70bd | 315 | timer.stop(); |
gsfan | 11:71d67fea5ace | 316 | t = 0; |
gsfan | 11:71d67fea5ace | 317 | } |
gsfan | 11:71d67fea5ace | 318 | } else { |
gsfan | 11:71d67fea5ace | 319 | if (join()) { |
gsfan | 13:189e04ac70bd | 320 | timer.reset(); |
gsfan | 13:189e04ac70bd | 321 | timer.start(); |
gsfan | 13:189e04ac70bd | 322 | t = 1; |
gsfan | 11:71d67fea5ace | 323 | } else { |
gsfan | 13:189e04ac70bd | 324 | timer.stop(); |
gsfan | 13:189e04ac70bd | 325 | t = 0; |
gsfan | 11:71d67fea5ace | 326 | } |
gsfan | 11:71d67fea5ace | 327 | } |
gsfan | 11:71d67fea5ace | 328 | } |
gsfan | 11:71d67fea5ace | 329 | } |
gsfan | 11:71d67fea5ace | 330 | #else |
gsfan | 12:057089026a20 | 331 | if (_state.wm == WM_INFRASTRUCTURE && (! isAssociated()) && _state.ssid) { |
gsfan | 8:64184a968e3b | 332 | // Wi-Fi re-associate |
gsfan | 11:71d67fea5ace | 333 | if (_state.initialized) { |
gsfan | 11:71d67fea5ace | 334 | if (!cmdWA(_state.ssid)) { |
gsfan | 11:71d67fea5ace | 335 | _state.associated = true; |
gsfan | 11:71d67fea5ace | 336 | } |
gsfan | 11:71d67fea5ace | 337 | } else { |
gsfan | 11:71d67fea5ace | 338 | join(); |
gsfan | 5:78943b3945b5 | 339 | } |
gsfan | 5:78943b3945b5 | 340 | } |
gsfan | 11:71d67fea5ace | 341 | #endif |
gsfan | 12:057089026a20 | 342 | |
gsfan | 12:057089026a20 | 343 | for (int i = 0; i < 16; i ++) { |
gsfan | 16:105b56570533 | 344 | if (isConnected(i) && _con[i].received && _con[i].buf != NULL) |
gsfan | 12:057089026a20 | 345 | if (_con[i].func != NULL && !_con[i].buf->isEmpty()) { |
gsfan | 12:057089026a20 | 346 | _con[i].func(i); |
gsfan | 12:057089026a20 | 347 | if (_con[i].buf->isEmpty()) { |
gsfan | 12:057089026a20 | 348 | _con[i].received = false; |
gsfan | 12:057089026a20 | 349 | } |
gsfan | 12:057089026a20 | 350 | } |
gsfan | 12:057089026a20 | 351 | } |
gsfan | 12:057089026a20 | 352 | |
gsfan | 12:057089026a20 | 353 | #ifdef CFG_ENABLE_HTTPD |
gsfan | 12:057089026a20 | 354 | httpdPoll(); |
gsfan | 12:057089026a20 | 355 | #endif |
gsfan | 5:78943b3945b5 | 356 | } |
gsfan | 5:78943b3945b5 | 357 | |
gsfan | 11:71d67fea5ace | 358 | #ifdef CFG_ENABLE_RTOS |
gsfan | 8:64184a968e3b | 359 | void GSwifi::threadPoll (void const *args) { |
gsfan | 8:64184a968e3b | 360 | GSwifi * _wifi = GSwifi::getInstance(); |
gsfan | 8:64184a968e3b | 361 | if (_wifi == NULL) |
gsfan | 8:64184a968e3b | 362 | error("Socket constructor error: no wifly instance available!\r\n"); |
gsfan | 5:78943b3945b5 | 363 | |
gsfan | 8:64184a968e3b | 364 | INFO("threadPoll"); |
gsfan | 8:64184a968e3b | 365 | for (;;) { |
gsfan | 8:64184a968e3b | 366 | Thread::signal_wait(1); |
gsfan | 8:64184a968e3b | 367 | Thread::wait(1000); |
gsfan | 8:64184a968e3b | 368 | INFO("disassociated"); |
gsfan | 12:057089026a20 | 369 | while (!_wifi->isAssociated()){ |
gsfan | 8:64184a968e3b | 370 | _wifi->poll(); |
gsfan | 13:189e04ac70bd | 371 | Thread::wait(CFG_RECONNECT * 1000); |
gsfan | 5:78943b3945b5 | 372 | } |
gsfan | 5:78943b3945b5 | 373 | } |
gsfan | 5:78943b3945b5 | 374 | } |
gsfan | 8:64184a968e3b | 375 | #endif |
gsfan | 5:78943b3945b5 | 376 | |
gsfan | 12:057089026a20 | 377 | GSwifi::Status GSwifi::getStatus () { |
gsfan | 12:057089026a20 | 378 | return _state.status; |
gsfan | 12:057089026a20 | 379 | } |
gsfan | 12:057089026a20 | 380 | |
gsfan | 12:057089026a20 | 381 | int GSwifi::setMacAddress (const char *mac) { |
gsfan | 12:057089026a20 | 382 | if (cmdNMAC(mac)) return -1; |
gsfan | 12:057089026a20 | 383 | strncpy(_state.mac, mac, sizeof(_state.mac)); |
gsfan | 12:057089026a20 | 384 | return 0; |
gsfan | 12:057089026a20 | 385 | } |
gsfan | 12:057089026a20 | 386 | |
gsfan | 12:057089026a20 | 387 | int GSwifi::getMacAddress (char *mac) { |
gsfan | 12:057089026a20 | 388 | if (cmdNMAC()) return -1; |
gsfan | 12:057089026a20 | 389 | strcpy(mac, _state.mac); |
gsfan | 12:057089026a20 | 390 | return 0; |
gsfan | 12:057089026a20 | 391 | } |
gsfan | 12:057089026a20 | 392 | |
gsfan | 11:71d67fea5ace | 393 | int GSwifi::setAddress (const char *name) { |
gsfan | 11:71d67fea5ace | 394 | _state.dhcp = true; |
gsfan | 11:71d67fea5ace | 395 | strncpy(_state.name, name, sizeof(_state.name)); |
gsfan | 11:71d67fea5ace | 396 | return 0; |
gsfan | 11:71d67fea5ace | 397 | } |
gsfan | 11:71d67fea5ace | 398 | |
gsfan | 15:086d1a33a197 | 399 | int GSwifi::setAddress (const char *ip, const char *netmask, const char *gateway, const char *dns, const char *name) { |
gsfan | 11:71d67fea5ace | 400 | _state.dhcp = false; |
gsfan | 11:71d67fea5ace | 401 | strncpy(_state.ip, ip, sizeof(_state.ip)); |
gsfan | 11:71d67fea5ace | 402 | strncpy(_state.netmask, netmask, sizeof(_state.netmask)); |
gsfan | 11:71d67fea5ace | 403 | strncpy(_state.gateway, gateway, sizeof(_state.gateway)); |
gsfan | 15:086d1a33a197 | 404 | strncpy(_state.nameserver, dns, sizeof(_state.nameserver)); |
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 | 11:71d67fea5ace | 409 | int GSwifi::getAddress (char *ip, char *netmask, char *gateway) { |
gsfan | 11:71d67fea5ace | 410 | strcpy(ip, _state.ip); |
gsfan | 11:71d67fea5ace | 411 | strcpy(netmask, _state.netmask); |
gsfan | 11:71d67fea5ace | 412 | strcpy(gateway, _state.gateway); |
gsfan | 11:71d67fea5ace | 413 | return 0; |
gsfan | 11:71d67fea5ace | 414 | } |
gsfan | 11:71d67fea5ace | 415 | |
gsfan | 8:64184a968e3b | 416 | int GSwifi::setSsid (Security sec, const char *ssid, const char *phrase) { |
gsfan | 8:64184a968e3b | 417 | int i; |
gsfan | 5:78943b3945b5 | 418 | |
gsfan | 8:64184a968e3b | 419 | _state.sec = sec; |
gsfan | 11:71d67fea5ace | 420 | |
gsfan | 8:64184a968e3b | 421 | // change all ' ' in '$' in the ssid and the passphrase |
gsfan | 8:64184a968e3b | 422 | strncpy(_state.ssid, ssid, sizeof(_state.ssid)); |
gsfan | 8:64184a968e3b | 423 | for (i = 0; i < strlen(_state.ssid); i++) { |
gsfan | 8:64184a968e3b | 424 | if (_state.ssid[i] == ' ') |
gsfan | 8:64184a968e3b | 425 | _state.ssid[i] = '$'; |
gsfan | 5:78943b3945b5 | 426 | } |
gsfan | 11:71d67fea5ace | 427 | |
gsfan | 11:71d67fea5ace | 428 | if (sec == SEC_WEP) { |
gsfan | 11:71d67fea5ace | 429 | if (strlen(phrase) == 5 || strlen(phrase) == 13) { |
gsfan | 11:71d67fea5ace | 430 | for (i = 0; i < strlen(phrase); i++) { |
gsfan | 11:71d67fea5ace | 431 | _state.pass[i * 2] = '0' + ((phrase[i] >> 4) & 0x0f); |
gsfan | 11:71d67fea5ace | 432 | _state.pass[i * 2 + 1] = '0' + (phrase[i + 1] & 0x0f); |
gsfan | 11:71d67fea5ace | 433 | } |
gsfan | 11:71d67fea5ace | 434 | } else { |
gsfan | 11:71d67fea5ace | 435 | strncpy(_state.pass, phrase, strlen(phrase)); |
gsfan | 11:71d67fea5ace | 436 | } |
gsfan | 11:71d67fea5ace | 437 | } else { |
gsfan | 11:71d67fea5ace | 438 | strncpy(_state.pass, phrase, sizeof(_state.pass)); |
gsfan | 11:71d67fea5ace | 439 | } |
gsfan | 8:64184a968e3b | 440 | for (i = 0; i < strlen(_state.pass); i++) { |
gsfan | 8:64184a968e3b | 441 | if (_state.pass[i] == ' ') |
gsfan | 8:64184a968e3b | 442 | _state.pass[i] = '$'; |
gsfan | 5:78943b3945b5 | 443 | } |
gsfan | 5:78943b3945b5 | 444 | return 0; |
gsfan | 5:78943b3945b5 | 445 | } |
gsfan | 5:78943b3945b5 | 446 |