private fork
Fork of GSwifiInterface by
GSwifi/GSwifi.cpp@11:71d67fea5ace, 2013-11-15 (annotated)
- Committer:
- gsfan
- Date:
- Fri Nov 15 04:20:14 2013 +0000
- Revision:
- 11:71d67fea5ace
- Parent:
- 8:64184a968e3b
- Child:
- 12:057089026a20
fix api function, miss spell
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 | 8:64184a968e3b | 66 | sendCommand(NULL, RES_NULL, 0); |
gsfan | 8:64184a968e3b | 67 | if (cmdE(false)) return -1; |
gsfan | 8:64184a968e3b | 68 | if (_flow) { |
gsfan | 8:64184a968e3b | 69 | cmdR(true); |
gsfan | 8:64184a968e3b | 70 | } |
gsfan | 8:64184a968e3b | 71 | cmdBDATA(true); |
gsfan | 8:64184a968e3b | 72 | if (cmdNMAC()) return -1; |
gsfan | 8:64184a968e3b | 73 | cmdWM(0); // infrastructure |
gsfan | 5:78943b3945b5 | 74 | wait_ms(100); |
gsfan | 5:78943b3945b5 | 75 | |
gsfan | 11:71d67fea5ace | 76 | switch (_state.sec) { |
gsfan | 5:78943b3945b5 | 77 | case SEC_NONE: |
gsfan | 5:78943b3945b5 | 78 | case SEC_OPEN: |
gsfan | 5:78943b3945b5 | 79 | case SEC_WEP: |
gsfan | 8:64184a968e3b | 80 | cmdNDHCP(_state.dhcp, _state.name, 0); |
gsfan | 8:64184a968e3b | 81 | cmdWAUTH(_state.sec); |
gsfan | 11:71d67fea5ace | 82 | if (_state.sec != SEC_NONE) { |
gsfan | 8:64184a968e3b | 83 | cmdWWEP(1, _state.pass); |
gsfan | 5:78943b3945b5 | 84 | wait_ms(100); |
gsfan | 5:78943b3945b5 | 85 | } |
gsfan | 11:71d67fea5ace | 86 | for (i = 0; i < CFG_TRYJOIN; i ++) { |
gsfan | 11:71d67fea5ace | 87 | r = cmdWA(_state.ssid); |
gsfan | 11:71d67fea5ace | 88 | if (!r) break; |
gsfan | 8:64184a968e3b | 89 | DBG("retry\r\n"); |
gsfan | 8:64184a968e3b | 90 | wait_ms(1000); |
gsfan | 5:78943b3945b5 | 91 | } |
gsfan | 5:78943b3945b5 | 92 | break; |
gsfan | 5:78943b3945b5 | 93 | case SEC_WPA_PSK: |
gsfan | 5:78943b3945b5 | 94 | case SEC_WPA2_PSK: |
gsfan | 8:64184a968e3b | 95 | cmdNDHCP(_state.dhcp, _state.name, 0); |
gsfan | 8:64184a968e3b | 96 | cmdWAUTH(0); |
gsfan | 8:64184a968e3b | 97 | cmdWPAPSK(_state.ssid, _state.pass); |
gsfan | 5:78943b3945b5 | 98 | wait_ms(100); |
gsfan | 11:71d67fea5ace | 99 | for (i = 0; i < CFG_TRYJOIN; i ++) { |
gsfan | 11:71d67fea5ace | 100 | r = cmdWA(_state.ssid); |
gsfan | 11:71d67fea5ace | 101 | if (!r) break; |
gsfan | 8:64184a968e3b | 102 | DBG("retry\r\n"); |
gsfan | 8:64184a968e3b | 103 | wait_ms(1000); |
gsfan | 5:78943b3945b5 | 104 | } |
gsfan | 5:78943b3945b5 | 105 | break; |
gsfan | 8:64184a968e3b | 106 | case SEC_WPA_ENT: |
gsfan | 8:64184a968e3b | 107 | case SEC_WPA2_ENT: |
gsfan | 8:64184a968e3b | 108 | cmdWAUTH(0); |
gsfan | 8:64184a968e3b | 109 | DBG("Can't support security\r\n"); |
gsfan | 8:64184a968e3b | 110 | break; |
gsfan | 5:78943b3945b5 | 111 | case SEC_WPS_BUTTON: |
gsfan | 8:64184a968e3b | 112 | cmdNDHCP(false); |
gsfan | 8:64184a968e3b | 113 | cmdWAUTH(0); |
gsfan | 8:64184a968e3b | 114 | r = cmdWWPS(true); |
gsfan | 8:64184a968e3b | 115 | if (r) break; |
gsfan | 8:64184a968e3b | 116 | if (_state.dhcp) { |
gsfan | 8:64184a968e3b | 117 | r = cmdNDHCP(_state.dhcp, _state.name); |
gsfan | 5:78943b3945b5 | 118 | } |
gsfan | 8:64184a968e3b | 119 | cmdWSTATUS(); |
gsfan | 8:64184a968e3b | 120 | cmdWPAPSK(_state.ssid, _state.pass); |
gsfan | 8:64184a968e3b | 121 | break; |
gsfan | 8:64184a968e3b | 122 | case SEC_WPS_PIN: |
gsfan | 8:64184a968e3b | 123 | cmdNDHCP(false); |
gsfan | 8:64184a968e3b | 124 | cmdWAUTH(0); |
gsfan | 8:64184a968e3b | 125 | r = cmdWWPS(true, _state.pass); |
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 | 5:78943b3945b5 | 132 | break; |
gsfan | 5:78943b3945b5 | 133 | default: |
gsfan | 5:78943b3945b5 | 134 | DBG("Can't use security\r\n"); |
gsfan | 5:78943b3945b5 | 135 | break; |
gsfan | 5:78943b3945b5 | 136 | } |
gsfan | 5:78943b3945b5 | 137 | |
gsfan | 8:64184a968e3b | 138 | if (!r) { |
gsfan | 11:71d67fea5ace | 139 | // connected |
gsfan | 8:64184a968e3b | 140 | if (! _state.dhcp) { |
gsfan | 8:64184a968e3b | 141 | cmdDNSSET(_state.nameserver); |
gsfan | 5:78943b3945b5 | 142 | } |
gsfan | 5:78943b3945b5 | 143 | _state.associated = true; |
gsfan | 11:71d67fea5ace | 144 | #ifdef CFG_ENABLE_RTOS |
gsfan | 8:64184a968e3b | 145 | _threadPoll = new Thread(&threadPoll); |
gsfan | 8:64184a968e3b | 146 | // _threadPoll = new Thread(&threadPoll, NULL, osPriorityLow); |
gsfan | 8:64184a968e3b | 147 | #endif |
gsfan | 5:78943b3945b5 | 148 | } |
gsfan | 5:78943b3945b5 | 149 | return r; |
gsfan | 5:78943b3945b5 | 150 | } |
gsfan | 5:78943b3945b5 | 151 | |
gsfan | 11:71d67fea5ace | 152 | int GSwifi::adhock () |
gsfan | 5:78943b3945b5 | 153 | { |
gsfan | 8:64184a968e3b | 154 | bool r = -1; |
gsfan | 5:78943b3945b5 | 155 | |
gsfan | 8:64184a968e3b | 156 | _state.wm = WM_ADHOCK; |
gsfan | 11:71d67fea5ace | 157 | _state.initialized = true; |
gsfan | 8:64184a968e3b | 158 | clearFlags(); |
gsfan | 8:64184a968e3b | 159 | sendCommand(NULL, RES_NULL, 0); |
gsfan | 8:64184a968e3b | 160 | if (cmdE(false)) return -1; |
gsfan | 8:64184a968e3b | 161 | if (_flow) { |
gsfan | 8:64184a968e3b | 162 | cmdR(true); |
gsfan | 5:78943b3945b5 | 163 | } |
gsfan | 8:64184a968e3b | 164 | if (cmdNMAC()) return -1; |
gsfan | 8:64184a968e3b | 165 | cmdBDATA(true); |
gsfan | 8:64184a968e3b | 166 | cmdWM(1); // adhock |
gsfan | 8:64184a968e3b | 167 | wait_ms(100); |
gsfan | 8:64184a968e3b | 168 | |
gsfan | 8:64184a968e3b | 169 | cmdNDHCP(false); |
gsfan | 8:64184a968e3b | 170 | if (! _state.dhcp) { |
gsfan | 8:64184a968e3b | 171 | cmdNSET(_state.ip, _state.netmask, _state.ip); |
gsfan | 5:78943b3945b5 | 172 | } |
gsfan | 5:78943b3945b5 | 173 | |
gsfan | 11:71d67fea5ace | 174 | switch (_state.sec) { |
gsfan | 8:64184a968e3b | 175 | case SEC_NONE: |
gsfan | 8:64184a968e3b | 176 | case SEC_OPEN: |
gsfan | 8:64184a968e3b | 177 | case SEC_WEP: |
gsfan | 8:64184a968e3b | 178 | cmdWAUTH(_state.sec); |
gsfan | 11:71d67fea5ace | 179 | if (_state.sec != SEC_NONE) { |
gsfan | 8:64184a968e3b | 180 | cmdWWEP(1, _state.pass); |
gsfan | 8:64184a968e3b | 181 | wait_ms(100); |
gsfan | 8:64184a968e3b | 182 | } |
gsfan | 8:64184a968e3b | 183 | r = cmdWA(_state.ssid); |
gsfan | 8:64184a968e3b | 184 | break; |
gsfan | 8:64184a968e3b | 185 | default: |
gsfan | 8:64184a968e3b | 186 | DBG("Can't use security\r\n"); |
gsfan | 8:64184a968e3b | 187 | break; |
gsfan | 5:78943b3945b5 | 188 | } |
gsfan | 5:78943b3945b5 | 189 | |
gsfan | 8:64184a968e3b | 190 | if (!r) { |
gsfan | 11:71d67fea5ace | 191 | // connected |
gsfan | 8:64184a968e3b | 192 | _state.associated = true; |
gsfan | 8:64184a968e3b | 193 | } |
gsfan | 8:64184a968e3b | 194 | return r; |
gsfan | 5:78943b3945b5 | 195 | } |
gsfan | 5:78943b3945b5 | 196 | |
gsfan | 11:71d67fea5ace | 197 | int GSwifi::limitedap () |
gsfan | 5:78943b3945b5 | 198 | { |
gsfan | 8:64184a968e3b | 199 | bool r = -1; |
gsfan | 8:64184a968e3b | 200 | |
gsfan | 8:64184a968e3b | 201 | _state.wm = WM_LIMITEDAP; |
gsfan | 11:71d67fea5ace | 202 | _state.initialized = true; |
gsfan | 8:64184a968e3b | 203 | if (!strlen(_state.name)) { |
gsfan | 8:64184a968e3b | 204 | strncpy(_state.name, CFG_DNSNAME, sizeof(_state.name)); |
gsfan | 8:64184a968e3b | 205 | } |
gsfan | 8:64184a968e3b | 206 | clearFlags(); |
gsfan | 8:64184a968e3b | 207 | sendCommand(NULL, RES_NULL, 0); |
gsfan | 8:64184a968e3b | 208 | if (cmdE(false)) return -1; |
gsfan | 8:64184a968e3b | 209 | if (_flow) { |
gsfan | 8:64184a968e3b | 210 | cmdR(true); |
gsfan | 8:64184a968e3b | 211 | } |
gsfan | 8:64184a968e3b | 212 | if (cmdNMAC()) return -1; |
gsfan | 8:64184a968e3b | 213 | cmdBDATA(true); |
gsfan | 8:64184a968e3b | 214 | cmdWM(2); // limitedap |
gsfan | 8:64184a968e3b | 215 | wait_ms(100); |
gsfan | 8:64184a968e3b | 216 | |
gsfan | 8:64184a968e3b | 217 | cmdNDHCP(false); |
gsfan | 8:64184a968e3b | 218 | if (! _state.dhcp) { |
gsfan | 8:64184a968e3b | 219 | cmdNSET(_state.ip, _state.netmask, _state.ip); |
gsfan | 8:64184a968e3b | 220 | cmdDNSSET(_state.ip); |
gsfan | 5:78943b3945b5 | 221 | } |
gsfan | 8:64184a968e3b | 222 | cmdDHCPSRVR(true); |
gsfan | 8:64184a968e3b | 223 | cmdDNS(true, _state.name); |
gsfan | 8:64184a968e3b | 224 | |
gsfan | 11:71d67fea5ace | 225 | switch (_state.sec) { |
gsfan | 8:64184a968e3b | 226 | case SEC_NONE: |
gsfan | 8:64184a968e3b | 227 | case SEC_OPEN: |
gsfan | 8:64184a968e3b | 228 | case SEC_WEP: |
gsfan | 8:64184a968e3b | 229 | cmdWAUTH(_state.sec); |
gsfan | 11:71d67fea5ace | 230 | if (_state.sec != SEC_NONE) { |
gsfan | 8:64184a968e3b | 231 | cmdWWEP(1, _state.pass); |
gsfan | 8:64184a968e3b | 232 | wait_ms(100); |
gsfan | 8:64184a968e3b | 233 | } |
gsfan | 8:64184a968e3b | 234 | r = cmdWA(_state.ssid); |
gsfan | 8:64184a968e3b | 235 | break; |
gsfan | 8:64184a968e3b | 236 | default: |
gsfan | 8:64184a968e3b | 237 | DBG("Can't use security\r\n"); |
gsfan | 8:64184a968e3b | 238 | break; |
gsfan | 8:64184a968e3b | 239 | } |
gsfan | 8:64184a968e3b | 240 | |
gsfan | 8:64184a968e3b | 241 | if (!r) { |
gsfan | 11:71d67fea5ace | 242 | // connected |
gsfan | 8:64184a968e3b | 243 | _state.associated = true; |
gsfan | 8:64184a968e3b | 244 | } |
gsfan | 8:64184a968e3b | 245 | return r; |
gsfan | 5:78943b3945b5 | 246 | } |
gsfan | 5:78943b3945b5 | 247 | |
gsfan | 8:64184a968e3b | 248 | int GSwifi::disconnect() |
gsfan | 5:78943b3945b5 | 249 | { |
gsfan | 8:64184a968e3b | 250 | int i; |
gsfan | 5:78943b3945b5 | 251 | |
gsfan | 5:78943b3945b5 | 252 | // if already disconnected, return |
gsfan | 5:78943b3945b5 | 253 | if (!_state.associated) |
gsfan | 8:64184a968e3b | 254 | return 0; |
gsfan | 5:78943b3945b5 | 255 | |
gsfan | 11:71d67fea5ace | 256 | #ifdef CFG_ENABLE_RTOS |
gsfan | 8:64184a968e3b | 257 | if (_threadPoll) { |
gsfan | 8:64184a968e3b | 258 | _threadPoll->terminate(); |
gsfan | 8:64184a968e3b | 259 | delete _threadPoll; |
gsfan | 8:64184a968e3b | 260 | } |
gsfan | 8:64184a968e3b | 261 | #endif |
gsfan | 8:64184a968e3b | 262 | for (i = 0; i < 16; i ++) { |
gsfan | 8:64184a968e3b | 263 | if (_con[i].buf) { |
gsfan | 5:78943b3945b5 | 264 | _con[i].buf->flush(); |
gsfan | 8:64184a968e3b | 265 | } |
gsfan | 5:78943b3945b5 | 266 | } |
gsfan | 8:64184a968e3b | 267 | cmdNCLOSEALL(); |
gsfan | 8:64184a968e3b | 268 | cmdWD(); |
gsfan | 8:64184a968e3b | 269 | cmdNDHCP(false); |
gsfan | 5:78943b3945b5 | 270 | wait_ms(100); |
gsfan | 5:78943b3945b5 | 271 | |
gsfan | 5:78943b3945b5 | 272 | _state.associated = false; |
gsfan | 8:64184a968e3b | 273 | return 0; |
gsfan | 5:78943b3945b5 | 274 | |
gsfan | 5:78943b3945b5 | 275 | } |
gsfan | 5:78943b3945b5 | 276 | |
gsfan | 8:64184a968e3b | 277 | bool GSwifi::isConnected() |
gsfan | 5:78943b3945b5 | 278 | { |
gsfan | 8:64184a968e3b | 279 | return _state.associated; |
gsfan | 5:78943b3945b5 | 280 | } |
gsfan | 5:78943b3945b5 | 281 | |
gsfan | 8:64184a968e3b | 282 | void GSwifi::poll () { |
gsfan | 11:71d67fea5ace | 283 | #ifndef CFG_ENABLE_RTOS |
gsfan | 11:71d67fea5ace | 284 | static int t = 0; |
gsfan | 11:71d67fea5ace | 285 | |
gsfan | 11:71d67fea5ace | 286 | if (_state.wm == WM_INFRASTRUCTURE && (! isConnected()) && _state.ssid) { |
gsfan | 11:71d67fea5ace | 287 | if (t && time(NULL) > t) { |
gsfan | 11:71d67fea5ace | 288 | // Wi-Fi re-associate |
gsfan | 11:71d67fea5ace | 289 | if (_state.initialized) { |
gsfan | 11:71d67fea5ace | 290 | if (!cmdWA(_state.ssid)) { |
gsfan | 11:71d67fea5ace | 291 | _state.associated = true; |
gsfan | 11:71d67fea5ace | 292 | t = 0; |
gsfan | 11:71d67fea5ace | 293 | } else { |
gsfan | 11:71d67fea5ace | 294 | t = time(NULL) + CFG_RECONNECT; |
gsfan | 11:71d67fea5ace | 295 | } |
gsfan | 11:71d67fea5ace | 296 | } else { |
gsfan | 11:71d67fea5ace | 297 | if (join()) { |
gsfan | 11:71d67fea5ace | 298 | t = 0; |
gsfan | 11:71d67fea5ace | 299 | } else { |
gsfan | 11:71d67fea5ace | 300 | t = time(NULL) + CFG_RECONNECT; |
gsfan | 11:71d67fea5ace | 301 | } |
gsfan | 11:71d67fea5ace | 302 | } |
gsfan | 11:71d67fea5ace | 303 | } |
gsfan | 11:71d67fea5ace | 304 | } else { |
gsfan | 11:71d67fea5ace | 305 | if (t == 0) |
gsfan | 11:71d67fea5ace | 306 | t = time(NULL) + CFG_RECONNECT; |
gsfan | 11:71d67fea5ace | 307 | } |
gsfan | 11:71d67fea5ace | 308 | #else |
gsfan | 8:64184a968e3b | 309 | if (_state.wm == WM_INFRASTRUCTURE && (! isConnected()) && _state.ssid) { |
gsfan | 8:64184a968e3b | 310 | // Wi-Fi re-associate |
gsfan | 11:71d67fea5ace | 311 | if (_state.initialized) { |
gsfan | 11:71d67fea5ace | 312 | if (!cmdWA(_state.ssid)) { |
gsfan | 11:71d67fea5ace | 313 | _state.associated = true; |
gsfan | 11:71d67fea5ace | 314 | } |
gsfan | 11:71d67fea5ace | 315 | } else { |
gsfan | 11:71d67fea5ace | 316 | join(); |
gsfan | 5:78943b3945b5 | 317 | } |
gsfan | 5:78943b3945b5 | 318 | } |
gsfan | 11:71d67fea5ace | 319 | #endif |
gsfan | 5:78943b3945b5 | 320 | } |
gsfan | 5:78943b3945b5 | 321 | |
gsfan | 11:71d67fea5ace | 322 | #ifdef CFG_ENABLE_RTOS |
gsfan | 8:64184a968e3b | 323 | void GSwifi::threadPoll (void const *args) { |
gsfan | 8:64184a968e3b | 324 | GSwifi * _wifi = GSwifi::getInstance(); |
gsfan | 8:64184a968e3b | 325 | if (_wifi == NULL) |
gsfan | 8:64184a968e3b | 326 | error("Socket constructor error: no wifly instance available!\r\n"); |
gsfan | 5:78943b3945b5 | 327 | |
gsfan | 8:64184a968e3b | 328 | INFO("threadPoll"); |
gsfan | 8:64184a968e3b | 329 | for (;;) { |
gsfan | 8:64184a968e3b | 330 | Thread::signal_wait(1); |
gsfan | 8:64184a968e3b | 331 | Thread::wait(1000); |
gsfan | 8:64184a968e3b | 332 | INFO("disassociated"); |
gsfan | 8:64184a968e3b | 333 | while (!_wifi->isConnected()){ |
gsfan | 8:64184a968e3b | 334 | _wifi->poll(); |
gsfan | 8:64184a968e3b | 335 | Thread::wait(CFG_TIMEOUT); |
gsfan | 5:78943b3945b5 | 336 | } |
gsfan | 5:78943b3945b5 | 337 | } |
gsfan | 5:78943b3945b5 | 338 | } |
gsfan | 8:64184a968e3b | 339 | #endif |
gsfan | 5:78943b3945b5 | 340 | |
gsfan | 11:71d67fea5ace | 341 | int GSwifi::setAddress (const char *name) { |
gsfan | 11:71d67fea5ace | 342 | _state.dhcp = true; |
gsfan | 11:71d67fea5ace | 343 | strncpy(_state.name, name, sizeof(_state.name)); |
gsfan | 11:71d67fea5ace | 344 | return 0; |
gsfan | 11:71d67fea5ace | 345 | } |
gsfan | 11:71d67fea5ace | 346 | |
gsfan | 11:71d67fea5ace | 347 | int GSwifi::setAddress (const char *ip, const char *netmask, const char *gateway, const char *name) { |
gsfan | 11:71d67fea5ace | 348 | _state.dhcp = false; |
gsfan | 11:71d67fea5ace | 349 | strncpy(_state.ip, ip, sizeof(_state.ip)); |
gsfan | 11:71d67fea5ace | 350 | strncpy(_state.netmask, netmask, sizeof(_state.netmask)); |
gsfan | 11:71d67fea5ace | 351 | strncpy(_state.gateway, gateway, sizeof(_state.gateway)); |
gsfan | 11:71d67fea5ace | 352 | strncpy(_state.name, name, sizeof(_state.name)); |
gsfan | 11:71d67fea5ace | 353 | return 0; |
gsfan | 11:71d67fea5ace | 354 | } |
gsfan | 11:71d67fea5ace | 355 | |
gsfan | 11:71d67fea5ace | 356 | int GSwifi::getAddress (char *ip, char *netmask, char *gateway) { |
gsfan | 11:71d67fea5ace | 357 | strcpy(ip, _state.ip); |
gsfan | 11:71d67fea5ace | 358 | strcpy(netmask, _state.netmask); |
gsfan | 11:71d67fea5ace | 359 | strcpy(gateway, _state.gateway); |
gsfan | 11:71d67fea5ace | 360 | return 0; |
gsfan | 11:71d67fea5ace | 361 | } |
gsfan | 11:71d67fea5ace | 362 | |
gsfan | 8:64184a968e3b | 363 | int GSwifi::setSsid (Security sec, const char *ssid, const char *phrase) { |
gsfan | 8:64184a968e3b | 364 | int i; |
gsfan | 5:78943b3945b5 | 365 | |
gsfan | 8:64184a968e3b | 366 | _state.sec = sec; |
gsfan | 11:71d67fea5ace | 367 | |
gsfan | 8:64184a968e3b | 368 | // change all ' ' in '$' in the ssid and the passphrase |
gsfan | 8:64184a968e3b | 369 | strncpy(_state.ssid, ssid, sizeof(_state.ssid)); |
gsfan | 8:64184a968e3b | 370 | for (i = 0; i < strlen(_state.ssid); i++) { |
gsfan | 8:64184a968e3b | 371 | if (_state.ssid[i] == ' ') |
gsfan | 8:64184a968e3b | 372 | _state.ssid[i] = '$'; |
gsfan | 5:78943b3945b5 | 373 | } |
gsfan | 11:71d67fea5ace | 374 | |
gsfan | 11:71d67fea5ace | 375 | if (sec == SEC_WEP) { |
gsfan | 11:71d67fea5ace | 376 | if (strlen(phrase) == 5 || strlen(phrase) == 13) { |
gsfan | 11:71d67fea5ace | 377 | for (i = 0; i < strlen(phrase); i++) { |
gsfan | 11:71d67fea5ace | 378 | _state.pass[i * 2] = '0' + ((phrase[i] >> 4) & 0x0f); |
gsfan | 11:71d67fea5ace | 379 | _state.pass[i * 2 + 1] = '0' + (phrase[i + 1] & 0x0f); |
gsfan | 11:71d67fea5ace | 380 | } |
gsfan | 11:71d67fea5ace | 381 | } else { |
gsfan | 11:71d67fea5ace | 382 | strncpy(_state.pass, phrase, strlen(phrase)); |
gsfan | 11:71d67fea5ace | 383 | } |
gsfan | 11:71d67fea5ace | 384 | } else { |
gsfan | 11:71d67fea5ace | 385 | strncpy(_state.pass, phrase, sizeof(_state.pass)); |
gsfan | 11:71d67fea5ace | 386 | } |
gsfan | 8:64184a968e3b | 387 | for (i = 0; i < strlen(_state.pass); i++) { |
gsfan | 8:64184a968e3b | 388 | if (_state.pass[i] == ' ') |
gsfan | 8:64184a968e3b | 389 | _state.pass[i] = '$'; |
gsfan | 5:78943b3945b5 | 390 | } |
gsfan | 5:78943b3945b5 | 391 | return 0; |
gsfan | 5:78943b3945b5 | 392 | } |
gsfan | 5:78943b3945b5 | 393 |