for TwitterMbed
Fork of GSwifi_old by
Diff: GSwifi.cpp
- Revision:
- 20:151b5a4fdd29
- Parent:
- 19:cad912f5a6ba
- Child:
- 22:9b077e2823ce
diff -r cad912f5a6ba -r 151b5a4fdd29 GSwifi.cpp --- a/GSwifi.cpp Tue Nov 06 08:54:19 2012 +0000 +++ b/GSwifi.cpp Thu Nov 08 01:28:45 2012 +0000 @@ -496,7 +496,7 @@ disconnect(); command("AT+WM=0", GSRES_NORMAL); // infrastructure wait_ms(100); - if (dhcp) { + if (dhcp && sec != GSSEC_WPS_BUTTON) { command("AT+NDHCP=1", GSRES_NORMAL); } else { command("AT+NDHCP=0", GSRES_NORMAL); @@ -506,7 +506,11 @@ case GSSEC_NONE: command("AT+WAUTH=0", GSRES_NORMAL); sprintf(cmd, "AT+WA=%s", ssid); - r = command(cmd, GSRES_NORMAL, GS_TIMEOUT2); + r = command(cmd, GSRES_DHCP, GS_TIMEOUT2); + if (r) { + DBG("retry\r\n"); + r = command(cmd, GSRES_DHCP, GS_TIMEOUT2); // retry + } break; case GSSEC_OPEN: case GSSEC_WEP: @@ -517,6 +521,10 @@ wait_ms(100); sprintf(cmd, "AT+WA=%s", ssid); r = command(cmd, GSRES_DHCP, GS_TIMEOUT2); + if (r) { + DBG("retry\r\n"); + r = command(cmd, GSRES_DHCP, GS_TIMEOUT2); + } break; case GSSEC_WPA_PSK: case GSSEC_WPA2_PSK: @@ -528,6 +536,10 @@ wait_ms(100); sprintf(cmd, "AT+WA=%s", ssid); r = command(cmd, GSRES_DHCP, GS_TIMEOUT2); + if (r) { + DBG("retry\r\n"); + r = command(cmd, GSRES_DHCP, GS_TIMEOUT2); + } break; case GSSEC_WPS_BUTTON: command("AT+WAUTH=0", GSRES_NORMAL); @@ -602,7 +614,7 @@ return r; } -int GSwifi::limitedap (GSSECURITY sec, const char *ssid, const char *pass, IpAddr ipaddr, IpAddr netmask) { +int GSwifi::limitedap (GSSECURITY sec, const char *ssid, const char *pass, IpAddr ipaddr, IpAddr netmask, bool prov) { int r; char cmd[GS_CMD_SIZE]; @@ -625,7 +637,7 @@ command("AT+NDHCP=0", GSRES_NORMAL); setAddress(ipaddr, netmask, ipaddr, ipaddr); if (command("AT+DHCPSRVR=1", GSRES_NORMAL)) return -1; - if (command("AT+DNS=1,setup", GSRES_NORMAL)) return -1; + if (command("AT+DNS=1,setup.local", GSRES_NORMAL)) return -1; switch (sec) { case GSSEC_NONE: @@ -649,6 +661,10 @@ break; } + if (r == 0 && prov) { + command("AT+WEBPROV=admin,password", GSRES_NORMAL); + } + if (r == 0) _connect = true; return r; } @@ -903,9 +919,11 @@ tmp = strstr(tmp, " ") + 1; _gs_sock[acid].host.setPort(atoi(tmp)); +#ifdef GS_USE_HTTPD if (_gs_sock[acid].protocol == GSPROT_HTTPD) { poll_httpd(acid, 0); } else +#endif if (_gs_sock[acid].onGsReceive != NULL) { _gs_sock[acid].onGsReceive(acid, 0); // event connected } @@ -916,9 +934,11 @@ DBG("disconnect %d\r\n", cid); _gs_sock[cid].connect = false; +#ifdef GS_USE_HTTPD if (_gs_sock[cid].protocol == GSPROT_HTTPD) { poll_httpd(cid, 0); } else +#endif if (_gs_sock[cid].onGsReceive != NULL) { _gs_sock[cid].onGsReceive(cid, 0); // event disconnected } @@ -959,12 +979,14 @@ if (_gs_sock[i].received && _gs_sock[i].data->use()) { // recv interrupt _gs_sock[i].received = 0; +#ifdef GS_USE_HTTPD if (_gs_sock[i].protocol == GSPROT_HTTPD) { for (j = 0; j < 1500 / GS_DATA_SIZE + 1; j ++) { if (! _gs_sock[i].connect || ! _gs_sock[i].data->use()) break; poll_httpd(i, _gs_sock[i].data->use()); } } else +#endif if (_gs_sock[i].onGsReceive != NULL) { for (j = 0; j < 1500 / GS_DATA_SIZE + 1; j ++) { if (! _gs_sock[i].connect || ! _gs_sock[i].data->use()) break; @@ -1055,14 +1077,14 @@ _gs.printf("\x1bZ%X%04d", cid, len); for (i = 0; i < len; i ++) { _gs_putc(buf[i]); - DBG("%c", buf[i]); +// DBG("%c", buf[i]); } #else _gs.printf("\x1bS%X", cid); for (i = 0; i < len; i ++) { if (buf[i] >= 0x20 && buf[i] < 0x7f) { _gs_putc(buf[i]); - DBG("%c", buf[i]); +// DBG("%c", buf[i]); } } _gs_putc(0x1b); @@ -1092,7 +1114,7 @@ _gs.printf("%04d", len); for (i = 0; i < len; i ++) { _gs_putc(buf[i]); - DBG("%c", buf[i]); +// DBG("%c", buf[i]); } #else _gs.printf("\x1bU%X", cid); @@ -1100,7 +1122,7 @@ for (i = 0; i < len; i ++) { if (buf[i] >= 0x20 && buf[i] < 0x7f) { _gs_putc(buf[i]); - DBG("%c", buf[i]); +// DBG("%c", buf[i]); } } _gs_putc(0x1b); @@ -1302,23 +1324,52 @@ return 0; } +int GSwifi::from_hex (int ch) { + return isdigit(ch) ? ch - '0' : tolower(ch) - 'a' + 10; +} + +int GSwifi::to_hex (int code) { + static char hex[] = "0123456789abcdef"; + return hex[code & 15]; +} + int GSwifi::urlencode (char *str, char *buf, int len) { // code from // Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) - static const char to_hex[] = "0123456789ABCDEF"; -// char *pstr = str, *buf = (char*)malloc(strlen(str) * 3 + 1), *pbuf = buf; +// char *pstr = str, *buf = (char*)malloc(strlen(str) * 3 + 1), *pbuf = buf; char *pstr = str, *pbuf = buf; if (len < (strlen(str) * 3 + 1)) return -1; while (*pstr) { - if (isalnum(*pstr) || *pstr == '-' || *pstr == '_' || *pstr == '.' || *pstr == '~') { + if (isalnum(*pstr) || *pstr == '-' || *pstr == '_' || *pstr == '.' || *pstr == '~') *pbuf++ = *pstr; - } else if (*pstr == ' ') { + else if (*pstr == ' ') *pbuf++ = '+'; - } else { - *pbuf++ = '%'; - *pbuf++ = to_hex[(*pstr >> 4) & 0x0f]; - *pbuf++ = to_hex[*pstr & 0x0f]; + else + *pbuf++ = '%', *pbuf++ = to_hex(*pstr >> 4), *pbuf++ = to_hex(*pstr & 15); + pstr++; + } + *pbuf = '\0'; + return 0; +} + +int GSwifi::urldecode (char *str, char *buf, int len) { + // code from + // Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) +// char *pstr = str, *buf = (char*)malloc(strlen(str) + 1), *pbuf = buf; + char *pstr = str, *pbuf = buf; + + if (len < (strlen(str) / 3 - 1)) return -1; + while (*pstr) { + if (*pstr == '%') { + if (pstr[1] && pstr[2]) { + *pbuf++ = from_hex(pstr[1]) << 4 | from_hex(pstr[2]); + pstr += 2; + } + } else if (*pstr == '+') { + *pbuf++ = ' '; + } else { + *pbuf++ = *pstr; } pstr++; }