see http://mbed.org/users/okini3939/notebook/wattmeter-shield-on-mbed/

Dependencies:   mbed

Fork of GSwifi_xively by gs fan

Committer:
okini3939
Date:
Wed Nov 27 08:18:45 2013 +0000
Revision:
4:9a2415f2ab07
update GSwifiInterface library

Who changed what in which revision?

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