private fork
Fork of GSwifiInterface by
Diff: GSwifi/GSwifi.cpp
- Revision:
- 11:71d67fea5ace
- Parent:
- 8:64184a968e3b
- Child:
- 12:057089026a20
diff -r 7d8134e7df01 -r 71d67fea5ace GSwifi/GSwifi.cpp --- a/GSwifi/GSwifi.cpp Thu Oct 31 08:34:45 2013 +0000 +++ b/GSwifi/GSwifi.cpp Fri Nov 15 04:20:14 2013 +0000 @@ -1,4 +1,4 @@ -/* Copyright (C) 2012 mbed.org, MIT License +/* Copyright (C) 2013 gsfan, MIT License * * Permission is hereby granted, free of charge, to any person obtaining a copy of this software * and associated documentation files (the "Software"), to deal in the Software without restriction, @@ -15,9 +15,6 @@ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/* Copyright (C) 2013 gsfan, MIT License - * port to the GainSpan Wi-FI module GS1011 - */ #include "mbed.h" #include "GSwifi.h" @@ -32,12 +29,15 @@ _inst = this; memset(&_state, 0, sizeof(_state)); memset(&_con, 0, sizeof(_con)); + _state.initialized = false; + _state.status = STAT_READY; _state.cid = -1; - _state.acid = -1; _state.buf = new CircBuffer<char>(CFG_DATA_SIZE); -#ifdef RTOS_H +#ifdef CFG_ENABLE_RTOS for (int i = 0; i < 16; i ++) { _con[i].buf = new CircBuffer<char>(CFG_DATA_SIZE); + if (_con[i].buf == NULL) + error("CircBuffer failed"); } _threadPoll = NULL; #endif @@ -52,12 +52,13 @@ wait_ms(100); } -int GSwifi::join(Security sec, const char *ssid, const char *phrase) +int GSwifi::join() { + int i; bool r = -1; _state.wm = WM_INFRASTRUCTURE; - setSsid(sec, ssid, phrase); + _state.initialized = true; if (!strlen(_state.name)) { strncpy(_state.name, CFG_DHCPNAME, sizeof(_state.name)); } @@ -72,21 +73,21 @@ cmdWM(0); // infrastructure wait_ms(100); - switch (sec) { + switch (_state.sec) { case SEC_NONE: case SEC_OPEN: case SEC_WEP: cmdNDHCP(_state.dhcp, _state.name, 0); cmdWAUTH(_state.sec); - if (sec != SEC_NONE) { + if (_state.sec != SEC_NONE) { cmdWWEP(1, _state.pass); wait_ms(100); } - r = cmdWA(_state.ssid); - if (r) { + for (i = 0; i < CFG_TRYJOIN; i ++) { + r = cmdWA(_state.ssid); + if (!r) break; DBG("retry\r\n"); wait_ms(1000); - r = cmdWA(_state.ssid); } break; case SEC_WPA_PSK: @@ -95,11 +96,11 @@ cmdWAUTH(0); cmdWPAPSK(_state.ssid, _state.pass); wait_ms(100); - r = cmdWA(_state.ssid); - if (r) { + for (i = 0; i < CFG_TRYJOIN; i ++) { + r = cmdWA(_state.ssid); + if (!r) break; DBG("retry\r\n"); wait_ms(1000); - r = cmdWA(_state.ssid); } break; case SEC_WPA_ENT: @@ -135,11 +136,12 @@ } if (!r) { + // connected if (! _state.dhcp) { cmdDNSSET(_state.nameserver); } _state.associated = true; -#ifdef RTOS_H +#ifdef CFG_ENABLE_RTOS _threadPoll = new Thread(&threadPoll); // _threadPoll = new Thread(&threadPoll, NULL, osPriorityLow); #endif @@ -147,12 +149,12 @@ return r; } -int GSwifi::adhock (Security sec, const char *ssid, const char *phrase) +int GSwifi::adhock () { bool r = -1; _state.wm = WM_ADHOCK; - setSsid(sec, ssid, phrase); + _state.initialized = true; clearFlags(); sendCommand(NULL, RES_NULL, 0); if (cmdE(false)) return -1; @@ -169,12 +171,12 @@ cmdNSET(_state.ip, _state.netmask, _state.ip); } - switch (sec) { + switch (_state.sec) { case SEC_NONE: case SEC_OPEN: case SEC_WEP: cmdWAUTH(_state.sec); - if (sec != SEC_NONE) { + if (_state.sec != SEC_NONE) { cmdWWEP(1, _state.pass); wait_ms(100); } @@ -186,17 +188,18 @@ } if (!r) { + // connected _state.associated = true; } return r; } -int GSwifi::limitedap (Security sec, const char *ssid, const char *phrase) +int GSwifi::limitedap () { bool r = -1; _state.wm = WM_LIMITEDAP; - setSsid(sec, ssid, phrase); + _state.initialized = true; if (!strlen(_state.name)) { strncpy(_state.name, CFG_DNSNAME, sizeof(_state.name)); } @@ -219,12 +222,12 @@ cmdDHCPSRVR(true); cmdDNS(true, _state.name); - switch (sec) { + switch (_state.sec) { case SEC_NONE: case SEC_OPEN: case SEC_WEP: cmdWAUTH(_state.sec); - if (sec != SEC_NONE) { + if (_state.sec != SEC_NONE) { cmdWWEP(1, _state.pass); wait_ms(100); } @@ -236,6 +239,7 @@ } if (!r) { + // connected _state.associated = true; } return r; @@ -249,7 +253,7 @@ if (!_state.associated) return 0; -#ifdef RTOS_H +#ifdef CFG_ENABLE_RTOS if (_threadPoll) { _threadPoll->terminate(); delete _threadPoll; @@ -276,15 +280,46 @@ } void GSwifi::poll () { +#ifndef CFG_ENABLE_RTOS + static int t = 0; + + if (_state.wm == WM_INFRASTRUCTURE && (! isConnected()) && _state.ssid) { + if (t && time(NULL) > t) { + // Wi-Fi re-associate + if (_state.initialized) { + if (!cmdWA(_state.ssid)) { + _state.associated = true; + t = 0; + } else { + t = time(NULL) + CFG_RECONNECT; + } + } else { + if (join()) { + t = 0; + } else { + t = time(NULL) + CFG_RECONNECT; + } + } + } + } else { + if (t == 0) + t = time(NULL) + CFG_RECONNECT; + } +#else if (_state.wm == WM_INFRASTRUCTURE && (! isConnected()) && _state.ssid) { // Wi-Fi re-associate - if (!cmdWA(_state.ssid)) { - _state.associated = true; + if (_state.initialized) { + if (!cmdWA(_state.ssid)) { + _state.associated = true; + } + } else { + join(); } } +#endif } -#ifdef RTOS_H +#ifdef CFG_ENABLE_RTOS void GSwifi::threadPoll (void const *args) { GSwifi * _wifi = GSwifi::getInstance(); if (_wifi == NULL) @@ -303,17 +338,52 @@ } #endif +int GSwifi::setAddress (const char *name) { + _state.dhcp = true; + strncpy(_state.name, name, sizeof(_state.name)); + return 0; +} + +int GSwifi::setAddress (const char *ip, const char *netmask, const char *gateway, const char *name) { + _state.dhcp = false; + strncpy(_state.ip, ip, sizeof(_state.ip)); + strncpy(_state.netmask, netmask, sizeof(_state.netmask)); + strncpy(_state.gateway, gateway, sizeof(_state.gateway)); + strncpy(_state.name, name, sizeof(_state.name)); + return 0; +} + +int GSwifi::getAddress (char *ip, char *netmask, char *gateway) { + strcpy(ip, _state.ip); + strcpy(netmask, _state.netmask); + strcpy(gateway, _state.gateway); + return 0; +} + int GSwifi::setSsid (Security sec, const char *ssid, const char *phrase) { int i; _state.sec = sec; + // change all ' ' in '$' in the ssid and the passphrase strncpy(_state.ssid, ssid, sizeof(_state.ssid)); for (i = 0; i < strlen(_state.ssid); i++) { if (_state.ssid[i] == ' ') _state.ssid[i] = '$'; } - strncpy(_state.pass, phrase, sizeof(_state.pass)); + + if (sec == SEC_WEP) { + if (strlen(phrase) == 5 || strlen(phrase) == 13) { + for (i = 0; i < strlen(phrase); i++) { + _state.pass[i * 2] = '0' + ((phrase[i] >> 4) & 0x0f); + _state.pass[i * 2 + 1] = '0' + (phrase[i + 1] & 0x0f); + } + } else { + strncpy(_state.pass, phrase, strlen(phrase)); + } + } else { + strncpy(_state.pass, phrase, sizeof(_state.pass)); + } for (i = 0; i < strlen(_state.pass); i++) { if (_state.pass[i] == ' ') _state.pass[i] = '$'; @@ -321,60 +391,3 @@ return 0; } -int GSwifi::standby (int msec) { - switch (_state.status) { - case STAT_READY: - cmdSTORENWCONN(); - for (int i = 0; i < 16; i ++) { - _con[i].connected = false; - } - break; - case STAT_WAKEUP: - if (cmdE(false)) return -1; - if (_flow) { - cmdR(true); - } - break; - default: - return -1; - } - - _state.status = STAT_STANDBY; - return cmdPSSTBY(msec, 0); -} - -int GSwifi::deepSleep () { - if (_state.status != STAT_READY) return -1; - _state.status = STAT_DEEPSLEEP; - return cmdPSDPSLEEP(); // go deep sleep -} - -int GSwifi::wakeup () { - if (_state.status == STAT_STANDBY) { - Timer timeout; - setAlarm(true); - timeout.start(); - while (_state.status != STAT_WAKEUP && timeout.read() < CFG_TIMEOUT); - timeout.stop(); - setAlarm(false); - } - - switch (_state.status) { - case STAT_WAKEUP: - _state.status = STAT_READY; - if (cmdE(false)) return -1; - if (_flow) { - cmdR(true); - } - cmdBDATA(true); - int r = cmdRESTORENWCONN(); - wait_ms(100); -// return cmdWRXACTIVE(true); - return r; - case STAT_DEEPSLEEP: - _state.status = STAT_READY; - return cmdAT(); - } - return -1; -} -