mirror of https://github.com/jeelabs/esp-link.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
998 lines
33 KiB
998 lines
33 KiB
9 years ago
|
/*
|
||
|
Cgi/template routines for the /wifi url.
|
||
|
*/
|
||
|
|
||
10 years ago
|
/*
|
||
|
* ----------------------------------------------------------------------------
|
||
|
* "THE BEER-WARE LICENSE" (Revision 42):
|
||
10 years ago
|
* Jeroen Domburg <jeroen@spritesmods.com> wrote this file. As long as you retain
|
||
|
* this notice you can do whatever you want with this stuff. If we meet some day,
|
||
|
* and you think this stuff is worth it, you can buy me a beer in return.
|
||
10 years ago
|
* ----------------------------------------------------------------------------
|
||
10 years ago
|
* Heavily modified and enhanced by Thorsten von Eicken in 2015
|
||
|
* ----------------------------------------------------------------------------
|
||
10 years ago
|
*/
|
||
|
|
||
8 years ago
|
|
||
10 years ago
|
#include <esp8266.h>
|
||
|
#include "cgiwifi.h"
|
||
10 years ago
|
#include "cgi.h"
|
||
10 years ago
|
#include "status.h"
|
||
10 years ago
|
#include "config.h"
|
||
10 years ago
|
#include "log.h"
|
||
10 years ago
|
|
||
9 years ago
|
#ifdef CGIWIFI_DBG
|
||
9 years ago
|
#define DBG(format, ...) do { os_printf(format, ## __VA_ARGS__); } while(0)
|
||
9 years ago
|
#else
|
||
|
#define DBG(format, ...) do { } while(0)
|
||
|
#endif
|
||
10 years ago
|
|
||
9 years ago
|
# define VERS_STR_STR(V) #V
|
||
|
# define VERS_STR(V) VERS_STR_STR(V)
|
||
|
|
||
9 years ago
|
bool mdns_started = false;
|
||
9 years ago
|
|
||
9 years ago
|
// ===== wifi status change callbacks
|
||
|
static WifiStateChangeCb wifi_state_change_cb[4];
|
||
10 years ago
|
|
||
8 years ago
|
// Temp store for new station config
|
||
9 years ago
|
struct station_config stconf;
|
||
9 years ago
|
|
||
|
// Temp store for new ap config
|
||
9 years ago
|
struct softap_config apconf;
|
||
9 years ago
|
|
||
10 years ago
|
uint8_t wifiState = wifiIsDisconnected;
|
||
|
// reasons for which a connection failed
|
||
|
uint8_t wifiReason = 0;
|
||
|
static char *wifiReasons[] = {
|
||
9 years ago
|
"", "unspecified", "auth_expire", "auth_leave", "assoc_expire", "assoc_toomany", "not_authed",
|
||
|
"not_assoced", "assoc_leave", "assoc_not_authed", "disassoc_pwrcap_bad", "disassoc_supchan_bad",
|
||
|
"ie_invalid", "mic_failure", "4way_handshake_timeout", "group_key_update_timeout",
|
||
|
"ie_in_4way_differs", "group_cipher_invalid", "pairwise_cipher_invalid", "akmp_invalid",
|
||
|
"unsupp_rsn_ie_version", "invalid_rsn_ie_cap", "802_1x_auth_failed", "cipher_suite_rejected",
|
||
|
"beacon_timeout", "no_ap_found" };
|
||
10 years ago
|
|
||
10 years ago
|
static char *wifiMode[] = { 0, "STA", "AP", "AP+STA" };
|
||
10 years ago
|
static char *wifiPhy[] = { 0, "11b", "11g", "11n" };
|
||
10 years ago
|
|
||
9 years ago
|
void (*wifiStatusCb)(uint8_t); // callback when wifi status changes
|
||
|
|
||
10 years ago
|
static char* ICACHE_FLASH_ATTR wifiGetReason(void) {
|
||
9 years ago
|
if (wifiReason <= 24) return wifiReasons[wifiReason];
|
||
|
if (wifiReason >= 200 && wifiReason <= 201) return wifiReasons[wifiReason-200+24];
|
||
|
return wifiReasons[1];
|
||
10 years ago
|
}
|
||
|
|
||
|
// handler for wifi status change callback coming in from espressif library
|
||
|
static void ICACHE_FLASH_ATTR wifiHandleEventCb(System_Event_t *evt) {
|
||
9 years ago
|
switch (evt->event) {
|
||
|
case EVENT_STAMODE_CONNECTED:
|
||
|
wifiState = wifiIsConnected;
|
||
|
wifiReason = 0;
|
||
9 years ago
|
DBG("Wifi connected to ssid %s, ch %d\n", evt->event_info.connected.ssid,
|
||
9 years ago
|
evt->event_info.connected.channel);
|
||
|
statusWifiUpdate(wifiState);
|
||
|
break;
|
||
|
case EVENT_STAMODE_DISCONNECTED:
|
||
|
wifiState = wifiIsDisconnected;
|
||
|
wifiReason = evt->event_info.disconnected.reason;
|
||
9 years ago
|
DBG("Wifi disconnected from ssid %s, reason %s (%d)\n",
|
||
9 years ago
|
evt->event_info.disconnected.ssid, wifiGetReason(), evt->event_info.disconnected.reason);
|
||
|
statusWifiUpdate(wifiState);
|
||
|
break;
|
||
|
case EVENT_STAMODE_AUTHMODE_CHANGE:
|
||
9 years ago
|
DBG("Wifi auth mode: %d -> %d\n",
|
||
9 years ago
|
evt->event_info.auth_change.old_mode, evt->event_info.auth_change.new_mode);
|
||
|
break;
|
||
|
case EVENT_STAMODE_GOT_IP:
|
||
|
wifiState = wifiGotIP;
|
||
|
wifiReason = 0;
|
||
9 years ago
|
DBG("Wifi got ip:" IPSTR ",mask:" IPSTR ",gw:" IPSTR "\n",
|
||
9 years ago
|
IP2STR(&evt->event_info.got_ip.ip), IP2STR(&evt->event_info.got_ip.mask),
|
||
|
IP2STR(&evt->event_info.got_ip.gw));
|
||
|
statusWifiUpdate(wifiState);
|
||
|
if (!mdns_started)
|
||
|
wifiStartMDNS(evt->event_info.got_ip.ip);
|
||
|
break;
|
||
|
case EVENT_SOFTAPMODE_STACONNECTED:
|
||
9 years ago
|
DBG("Wifi AP: station " MACSTR " joined, AID = %d\n",
|
||
9 years ago
|
MAC2STR(evt->event_info.sta_connected.mac), evt->event_info.sta_connected.aid);
|
||
|
break;
|
||
|
case EVENT_SOFTAPMODE_STADISCONNECTED:
|
||
9 years ago
|
DBG("Wifi AP: station " MACSTR " left, AID = %d\n",
|
||
9 years ago
|
MAC2STR(evt->event_info.sta_disconnected.mac), evt->event_info.sta_disconnected.aid);
|
||
|
break;
|
||
|
default:
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
for (int i = 0; i < 4; i++) {
|
||
|
if (wifi_state_change_cb[i] != NULL) (wifi_state_change_cb[i])(wifiState);
|
||
|
}
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
void ICACHE_FLASH_ATTR wifiAddStateChangeCb(WifiStateChangeCb cb) {
|
||
9 years ago
|
for (int i = 0; i < 4; i++) {
|
||
|
if (wifi_state_change_cb[i] == cb) return;
|
||
|
if (wifi_state_change_cb[i] == NULL) {
|
||
|
wifi_state_change_cb[i] = cb;
|
||
|
return;
|
||
|
}
|
||
|
}
|
||
9 years ago
|
DBG("WIFI: max state change cb count exceeded\n");
|
||
10 years ago
|
}
|
||
|
|
||
8 years ago
|
static struct mdns_info *mdns_info;
|
||
8 years ago
|
// See https://github.com/arduino/Arduino/blob/master/arduino-core/src/cc/arduino/packages/discoverers/NetworkDiscovery.java#L155-L168
|
||
8 years ago
|
static char* mdns_txt = "ssh_upload=no";
|
||
8 years ago
|
|
||
9 years ago
|
void ICACHE_FLASH_ATTR wifiStartMDNS(struct ip_addr ip) {
|
||
9 years ago
|
if (flashConfig.mdns_enable) {
|
||
8 years ago
|
if (mdns_info == NULL)
|
||
|
mdns_info = (struct mdns_info *)os_zalloc(sizeof(struct mdns_info));
|
||
|
|
||
9 years ago
|
mdns_info->host_name = flashConfig.hostname;
|
||
|
mdns_info->server_name = flashConfig.mdns_servername;
|
||
|
mdns_info->server_port = 80;
|
||
|
mdns_info->ipAddr = ip.addr;
|
||
8 years ago
|
mdns_info->txt_data[0] = mdns_txt;
|
||
9 years ago
|
espconn_mdns_init(mdns_info);
|
||
9 years ago
|
}
|
||
9 years ago
|
else {
|
||
9 years ago
|
espconn_mdns_server_unregister();
|
||
|
espconn_mdns_close();
|
||
8 years ago
|
if (mdns_info != NULL) {
|
||
|
os_free(mdns_info);
|
||
|
mdns_info = NULL;
|
||
|
}
|
||
9 years ago
|
}
|
||
|
mdns_started = true;
|
||
9 years ago
|
}
|
||
|
|
||
10 years ago
|
// ===== wifi scanning
|
||
|
|
||
10 years ago
|
//WiFi access point data
|
||
|
typedef struct {
|
||
9 years ago
|
char ssid[32];
|
||
|
sint8 rssi;
|
||
|
char enc;
|
||
10 years ago
|
} ApData;
|
||
|
|
||
10 years ago
|
//Scan result
|
||
10 years ago
|
typedef struct {
|
||
9 years ago
|
char scanInProgress; //if 1, don't access the underlying stuff from the webpage.
|
||
|
ApData **apData;
|
||
|
int noAps;
|
||
10 years ago
|
} ScanResultData;
|
||
|
|
||
|
//Static scan status storage.
|
||
10 years ago
|
static ScanResultData cgiWifiAps;
|
||
|
|
||
10 years ago
|
//Callback the code calls when a wlan ap scan is done. Basically stores the result in
|
||
|
//the cgiWifiAps struct.
|
||
|
void ICACHE_FLASH_ATTR wifiScanDoneCb(void *arg, STATUS status) {
|
||
9 years ago
|
int n;
|
||
|
struct bss_info *bss_link = (struct bss_info *)arg;
|
||
|
|
||
|
if (status!=OK) {
|
||
9 years ago
|
DBG("wifiScanDoneCb status=%d\n", status);
|
||
9 years ago
|
cgiWifiAps.scanInProgress=0;
|
||
9 years ago
|
return;
|
||
|
}
|
||
|
|
||
|
//Clear prev ap data if needed.
|
||
|
if (cgiWifiAps.apData!=NULL) {
|
||
|
for (n=0; n<cgiWifiAps.noAps; n++) os_free(cgiWifiAps.apData[n]);
|
||
|
os_free(cgiWifiAps.apData);
|
||
|
}
|
||
|
|
||
|
//Count amount of access points found.
|
||
|
n=0;
|
||
|
while (bss_link != NULL) {
|
||
|
bss_link = bss_link->next.stqe_next;
|
||
|
n++;
|
||
|
}
|
||
|
//Allocate memory for access point data
|
||
|
cgiWifiAps.apData=(ApData **)os_malloc(sizeof(ApData *)*n);
|
||
|
cgiWifiAps.noAps=n;
|
||
9 years ago
|
DBG("Scan done: found %d APs\n", n);
|
||
9 years ago
|
|
||
|
//Copy access point data to the static struct
|
||
|
n=0;
|
||
|
bss_link = (struct bss_info *)arg;
|
||
|
while (bss_link != NULL) {
|
||
|
if (n>=cgiWifiAps.noAps) {
|
||
|
//This means the bss_link changed under our nose. Shouldn't happen!
|
||
|
//Break because otherwise we will write in unallocated memory.
|
||
9 years ago
|
DBG("Huh? I have more than the allocated %d aps!\n", cgiWifiAps.noAps);
|
||
9 years ago
|
break;
|
||
|
}
|
||
|
//Save the ap data.
|
||
|
cgiWifiAps.apData[n]=(ApData *)os_malloc(sizeof(ApData));
|
||
|
cgiWifiAps.apData[n]->rssi=bss_link->rssi;
|
||
|
cgiWifiAps.apData[n]->enc=bss_link->authmode;
|
||
|
strncpy(cgiWifiAps.apData[n]->ssid, (char*)bss_link->ssid, 32);
|
||
9 years ago
|
DBG("bss%d: %s (%d)\n", n+1, (char*)bss_link->ssid, bss_link->rssi);
|
||
9 years ago
|
|
||
|
bss_link = bss_link->next.stqe_next;
|
||
|
n++;
|
||
|
}
|
||
|
//We're done.
|
||
|
cgiWifiAps.scanInProgress=0;
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
static ETSTimer scanTimer;
|
||
|
static void ICACHE_FLASH_ATTR scanStartCb(void *arg) {
|
||
9 years ago
|
DBG("Starting a scan\n");
|
||
9 years ago
|
wifi_station_scan(NULL, wifiScanDoneCb);
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
static int ICACHE_FLASH_ATTR cgiWiFiGetScan(HttpdConnData *connData) {
|
||
9 years ago
|
if (connData->conn==NULL) return HTTPD_CGI_DONE; // Connection aborted. Clean up.
|
||
|
char buff[1460];
|
||
|
const int chunk = 1460/64; // ssid is up to 32 chars
|
||
|
int len = 0;
|
||
|
|
||
8 years ago
|
DBG("GET scan: cgiData=%d noAps=%d\n", (int)connData->cgiData, cgiWifiAps.noAps);
|
||
9 years ago
|
|
||
|
// handle continuation call, connData->cgiData-1 is the position in the scan results where we
|
||
|
// we need to continue sending from (using -1 'cause 0 means it's the first call)
|
||
|
if (connData->cgiData) {
|
||
|
int next = (int)connData->cgiData-1;
|
||
|
int pos = next;
|
||
|
while (pos < cgiWifiAps.noAps && pos < next+chunk) {
|
||
|
len += os_sprintf(buff+len, "{\"essid\": \"%s\", \"rssi\": %d, \"enc\": \"%d\"}%c\n",
|
||
|
cgiWifiAps.apData[pos]->ssid, cgiWifiAps.apData[pos]->rssi, cgiWifiAps.apData[pos]->enc,
|
||
|
(pos+1 == cgiWifiAps.noAps) ? ' ' : ',');
|
||
|
pos++;
|
||
|
}
|
||
|
// done or more?
|
||
|
if (pos == cgiWifiAps.noAps) {
|
||
|
len += os_sprintf(buff+len, "]}}\n");
|
||
|
httpdSend(connData, buff, len);
|
||
|
return HTTPD_CGI_DONE;
|
||
|
} else {
|
||
|
connData->cgiData = (void*)(pos+1);
|
||
|
httpdSend(connData, buff, len);
|
||
|
return HTTPD_CGI_MORE;
|
||
9 years ago
|
}
|
||
9 years ago
|
}
|
||
|
|
||
|
jsonHeader(connData, 200);
|
||
|
|
||
|
if (cgiWifiAps.scanInProgress==1) {
|
||
|
//We're still scanning. Tell Javascript code that.
|
||
|
len = os_sprintf(buff, "{\n \"result\": { \n\"inProgress\": \"1\"\n }\n}\n");
|
||
9 years ago
|
httpdSend(connData, buff, len);
|
||
9 years ago
|
return HTTPD_CGI_DONE;
|
||
|
}
|
||
|
|
||
|
len = os_sprintf(buff, "{\"result\": {\"inProgress\": \"0\", \"APs\": [\n");
|
||
|
connData->cgiData = (void *)1; // start with first result next time we're called
|
||
|
httpdSend(connData, buff, len);
|
||
|
return HTTPD_CGI_MORE;
|
||
10 years ago
|
}
|
||
|
|
||
8 years ago
|
// Start scanning, without parameters
|
||
|
void ICACHE_FLASH_ATTR wifiStartScan() {
|
||
|
if (!cgiWifiAps.scanInProgress) {
|
||
|
cgiWifiAps.scanInProgress = 1;
|
||
|
os_timer_disarm(&scanTimer);
|
||
|
os_timer_setfn(&scanTimer, scanStartCb, NULL);
|
||
7 years ago
|
os_timer_arm(&scanTimer, 200, 0);
|
||
8 years ago
|
}
|
||
|
}
|
||
|
|
||
|
// Start scanning, web interface
|
||
|
int ICACHE_FLASH_ATTR cgiWiFiStartScan(HttpdConnData *connData) {
|
||
|
if (connData->conn==NULL) return HTTPD_CGI_DONE; // Connection aborted. Clean up.
|
||
|
jsonHeader(connData, 200);
|
||
|
|
||
|
// Don't duplicate code, reuse the function above.
|
||
|
wifiStartScan();
|
||
|
|
||
|
return HTTPD_CGI_DONE;
|
||
|
}
|
||
|
|
||
10 years ago
|
int ICACHE_FLASH_ATTR cgiWiFiScan(HttpdConnData *connData) {
|
||
9 years ago
|
if (connData->requestType == HTTPD_METHOD_GET) {
|
||
|
return cgiWiFiGetScan(connData);
|
||
9 years ago
|
}else if(connData->requestType == HTTPD_METHOD_POST) {
|
||
9 years ago
|
// DO NOT start APs scan in AP mode
|
||
|
int mode = wifi_get_opmode();
|
||
|
if(mode==2){
|
||
|
jsonHeader(connData, 400);
|
||
|
return HTTPD_CGI_DONE;
|
||
|
}else{
|
||
|
return cgiWiFiStartScan(connData);
|
||
|
}
|
||
|
}else{
|
||
9 years ago
|
jsonHeader(connData, 404);
|
||
|
return HTTPD_CGI_DONE;
|
||
|
}
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
// ===== timers to change state and rescue from failed associations
|
||
|
|
||
|
// reset timer changes back to STA+AP if we can't associate
|
||
|
#define RESET_TIMEOUT (15000) // 15 seconds
|
||
|
static ETSTimer resetTimer;
|
||
10 years ago
|
|
||
9 years ago
|
// This routine is ran some time after a connection attempt to an access point. If
|
||
|
// the connect succeeds, this gets the module in STA-only mode. If it fails, it ensures
|
||
|
// that the module is in STA+AP mode so the user has a chance to recover.
|
||
10 years ago
|
static void ICACHE_FLASH_ATTR resetTimerCb(void *arg) {
|
||
9 years ago
|
int x = wifi_station_get_connect_status();
|
||
|
int m = wifi_get_opmode() & 0x3;
|
||
9 years ago
|
DBG("Wifi check: mode=%s status=%d\n", wifiMode[m], x);
|
||
9 years ago
|
|
||
9 years ago
|
if (m == 2) return; // 2=AP, in AP-only mode we don't do any auto-switching
|
||
|
|
||
|
if ( x == STATION_GOT_IP ) {
|
||
|
// if we got an IP we could switch to STA-only...
|
||
|
if (m != 1) { // 1=STA
|
||
9 years ago
|
#ifdef CHANGE_TO_STA
|
||
9 years ago
|
// We're happily connected, go to STA mode
|
||
9 years ago
|
DBG("Wifi got IP. Going into STA mode..\n");
|
||
9 years ago
|
wifi_set_opmode(1);
|
||
7 years ago
|
os_timer_arm(&resetTimer, RESET_TIMEOUT, 0); // check one more time after switching to STA-only
|
||
9 years ago
|
#endif
|
||
9 years ago
|
}
|
||
9 years ago
|
log_uart(false);
|
||
|
// no more resetTimer at this point, gotta use physical reset to recover if in trouble
|
||
9 years ago
|
} else {
|
||
|
// we don't have an IP address
|
||
|
if (m != 3) {
|
||
|
DBG("Wifi connect failed. Going into STA+AP mode..\n");
|
||
|
wifi_set_opmode(3);
|
||
|
wifi_softap_set_config(&apconf);
|
||
9 years ago
|
}
|
||
|
log_uart(true);
|
||
9 years ago
|
DBG("Enabling/continuing uart log\n");
|
||
7 years ago
|
os_timer_arm(&resetTimer, RESET_TIMEOUT, 0);
|
||
9 years ago
|
}
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
// Reassociate timer to delay change of association so the original request can finish
|
||
|
static ETSTimer reassTimer;
|
||
10 years ago
|
|
||
10 years ago
|
// Callback actually doing reassociation
|
||
10 years ago
|
static void ICACHE_FLASH_ATTR reassTimerCb(void *arg) {
|
||
9 years ago
|
DBG("Wifi changing association\n");
|
||
9 years ago
|
wifi_station_disconnect();
|
||
|
stconf.bssid_set = 0;
|
||
|
wifi_station_set_config(&stconf);
|
||
|
wifi_station_connect();
|
||
|
// Schedule check, we give some extra time (4x) 'cause the reassociation can cause the AP
|
||
|
// to have to change channel, and then the client needs to follow before it can see the
|
||
|
// IP address
|
||
|
os_timer_disarm(&resetTimer);
|
||
|
os_timer_setfn(&resetTimer, resetTimerCb, NULL);
|
||
7 years ago
|
os_timer_arm(&resetTimer, 4*RESET_TIMEOUT, 0);
|
||
10 years ago
|
}
|
||
|
|
||
8 years ago
|
// Kick off connection to some network
|
||
|
void ICACHE_FLASH_ATTR connectToNetwork(char *ssid, char *pass) {
|
||
|
os_strncpy((char*)stconf.ssid, ssid, 32);
|
||
|
os_strncpy((char*)stconf.password, pass, 64);
|
||
|
DBG("Wifi try to connect to AP %s pw %s\n", ssid, pass);
|
||
|
|
||
|
// Schedule disconnect/connect
|
||
|
os_timer_disarm(&reassTimer);
|
||
|
os_timer_setfn(&reassTimer, reassTimerCb, NULL);
|
||
7 years ago
|
os_timer_arm(&reassTimer, 1000, 0); // 1 second for the response of this request to make it
|
||
8 years ago
|
}
|
||
|
|
||
10 years ago
|
// This cgi uses the routines above to connect to a specific access point with the
|
||
|
// given ESSID using the given password.
|
||
10 years ago
|
int ICACHE_FLASH_ATTR cgiWiFiConnect(HttpdConnData *connData) {
|
||
9 years ago
|
int mode = wifi_get_opmode();
|
||
|
if(mode == 2){
|
||
9 years ago
|
jsonHeader(connData, 400);
|
||
9 years ago
|
httpdSend(connData, "Can't associate to an AP en SoftAP mode", -1);
|
||
|
return HTTPD_CGI_DONE;
|
||
9 years ago
|
}
|
||
9 years ago
|
char essid[128];
|
||
|
char passwd[128];
|
||
|
|
||
|
if (connData->conn==NULL) return HTTPD_CGI_DONE;
|
||
|
|
||
|
int el = httpdFindArg(connData->getArgs, "essid", essid, sizeof(essid));
|
||
|
int pl = httpdFindArg(connData->getArgs, "passwd", passwd, sizeof(passwd));
|
||
|
|
||
|
if (el > 0 && pl >= 0) {
|
||
|
//Set to 0 if you want to disable the actual reconnecting bit
|
||
8 years ago
|
|
||
|
connectToNetwork(essid, passwd);
|
||
9 years ago
|
jsonHeader(connData, 200);
|
||
|
} else {
|
||
9 years ago
|
jsonHeader(connData, 400);
|
||
9 years ago
|
httpdSend(connData, "Cannot parse ssid or password", -1);
|
||
|
}
|
||
|
return HTTPD_CGI_DONE;
|
||
10 years ago
|
}
|
||
|
|
||
9 years ago
|
static bool ICACHE_FLASH_ATTR parse_ip(char *buff, ip_addr_t *ip_ptr) {
|
||
9 years ago
|
char *next = buff; // where to start parsing next integer
|
||
|
int found = 0; // number of integers parsed
|
||
|
uint32_t ip = 0; // the ip addres parsed
|
||
|
for (int i=0; i<32; i++) { // 32 is just a safety limit
|
||
|
char c = buff[i];
|
||
|
if (c == '.' || c == 0) {
|
||
|
// parse the preceding integer and accumulate into IP address
|
||
|
bool last = c == 0;
|
||
|
buff[i] = 0;
|
||
|
uint32_t v = atoi(next);
|
||
|
ip = ip | ((v&0xff)<<(found*8));
|
||
|
next = buff+i+1; // next integer starts after the '.'
|
||
|
found++;
|
||
|
if (last) { // if at end of string we better got 4 integers
|
||
|
ip_ptr->addr = ip;
|
||
|
return found == 4;
|
||
|
}
|
||
|
continue;
|
||
|
}
|
||
|
if (c < '0' || c > '9') return false;
|
||
|
}
|
||
|
return false;
|
||
10 years ago
|
}
|
||
|
|
||
|
#ifdef DEBUGIP
|
||
|
static void ICACHE_FLASH_ATTR debugIP() {
|
||
9 years ago
|
struct ip_info info;
|
||
|
if (wifi_get_ip_info(0, &info)) {
|
||
8 years ago
|
DBG("\"ip\": \"%d.%d.%d.%d\"\n", IP2STR(&info.ip.addr));
|
||
|
DBG("\"netmask\": \"%d.%d.%d.%d\"\n", IP2STR(&info.netmask.addr));
|
||
|
DBG("\"gateway\": \"%d.%d.%d.%d\"\n", IP2STR(&info.gw.addr));
|
||
|
DBG("\"hostname\": \"%s\"\n", wifi_station_get_hostname());
|
||
9 years ago
|
} else {
|
||
8 years ago
|
DBG("\"ip\": \"-none-\"\n");
|
||
9 years ago
|
}
|
||
10 years ago
|
}
|
||
|
#endif
|
||
|
|
||
10 years ago
|
// configure Wifi, specifically DHCP vs static IP address based on flash config
|
||
9 years ago
|
void ICACHE_FLASH_ATTR configWifiIP() {
|
||
9 years ago
|
if (flashConfig.staticip == 0) {
|
||
|
// let's DHCP!
|
||
|
wifi_station_set_hostname(flashConfig.hostname);
|
||
|
if (wifi_station_dhcpc_status() == DHCP_STARTED)
|
||
|
wifi_station_dhcpc_stop();
|
||
|
wifi_station_dhcpc_start();
|
||
9 years ago
|
DBG("Wifi uses DHCP, hostname=%s\n", flashConfig.hostname);
|
||
9 years ago
|
} else {
|
||
|
// no DHCP, we got static network config!
|
||
|
wifi_station_dhcpc_stop();
|
||
|
struct ip_info ipi;
|
||
|
ipi.ip.addr = flashConfig.staticip;
|
||
|
ipi.netmask.addr = flashConfig.netmask;
|
||
|
ipi.gw.addr = flashConfig.gateway;
|
||
|
wifi_set_ip_info(0, &ipi);
|
||
9 years ago
|
DBG("Wifi uses static IP %d.%d.%d.%d\n", IP2STR(&ipi.ip.addr));
|
||
9 years ago
|
}
|
||
10 years ago
|
#ifdef DEBUGIP
|
||
9 years ago
|
debugIP();
|
||
10 years ago
|
#endif
|
||
|
}
|
||
|
|
||
10 years ago
|
// Change special settings
|
||
|
int ICACHE_FLASH_ATTR cgiWiFiSpecial(HttpdConnData *connData) {
|
||
9 years ago
|
char dhcp[8];
|
||
|
char staticip[20];
|
||
|
char netmask[20];
|
||
|
char gateway[20];
|
||
|
|
||
|
if (connData->conn==NULL) return HTTPD_CGI_DONE;
|
||
|
|
||
|
// get args and their string lengths
|
||
|
int dl = httpdFindArg(connData->getArgs, "dhcp", dhcp, sizeof(dhcp));
|
||
|
int sl = httpdFindArg(connData->getArgs, "staticip", staticip, sizeof(staticip));
|
||
|
int nl = httpdFindArg(connData->getArgs, "netmask", netmask, sizeof(netmask));
|
||
|
int gl = httpdFindArg(connData->getArgs, "gateway", gateway, sizeof(gateway));
|
||
|
|
||
|
if (!(dl > 0 && sl >= 0 && nl >= 0 && gl >= 0)) {
|
||
|
jsonHeader(connData, 400);
|
||
|
httpdSend(connData, "Request is missing fields", -1);
|
||
9 years ago
|
return HTTPD_CGI_DONE;
|
||
9 years ago
|
}
|
||
|
|
||
|
char url[64]; // redirect URL
|
||
|
if (os_strcmp(dhcp, "off") == 0) {
|
||
|
// parse static IP params
|
||
|
struct ip_info ipi;
|
||
|
bool ok = parse_ip(staticip, &ipi.ip);
|
||
|
if (nl > 0) ok = ok && parse_ip(netmask, &ipi.netmask);
|
||
|
else IP4_ADDR(&ipi.netmask, 255, 255, 255, 0);
|
||
|
if (gl > 0) ok = ok && parse_ip(gateway, &ipi.gw);
|
||
|
else ipi.gw.addr = 0;
|
||
|
if (!ok) {
|
||
|
jsonHeader(connData, 400);
|
||
|
httpdSend(connData, "Cannot parse static IP config", -1);
|
||
|
return HTTPD_CGI_DONE;
|
||
|
}
|
||
|
// save the params in flash
|
||
|
flashConfig.staticip = ipi.ip.addr;
|
||
|
flashConfig.netmask = ipi.netmask.addr;
|
||
|
flashConfig.gateway = ipi.gw.addr;
|
||
|
// construct redirect URL
|
||
|
os_sprintf(url, "{\"url\": \"http://%d.%d.%d.%d\"}", IP2STR(&ipi.ip));
|
||
|
|
||
|
} else {
|
||
|
// dynamic IP
|
||
|
flashConfig.staticip = 0;
|
||
|
os_sprintf(url, "{\"url\": \"http://%s\"}", flashConfig.hostname);
|
||
|
}
|
||
|
|
||
|
configSave(); // ignore error...
|
||
|
// schedule change-over
|
||
|
os_timer_disarm(&reassTimer);
|
||
|
os_timer_setfn(&reassTimer, configWifiIP, NULL);
|
||
7 years ago
|
os_timer_arm(&reassTimer, 1000, 0); // 1 second for the response of this request to make it
|
||
9 years ago
|
// return redirect info
|
||
|
jsonHeader(connData, 200);
|
||
|
httpdSend(connData, url, -1);
|
||
|
return HTTPD_CGI_DONE;
|
||
9 years ago
|
}
|
||
|
|
||
|
// ==== Soft-AP related functions
|
||
|
|
||
|
// Change Soft-AP main settings
|
||
|
int ICACHE_FLASH_ATTR cgiApSettingsChange(HttpdConnData *connData) {
|
||
9 years ago
|
|
||
9 years ago
|
if (connData->conn==NULL) return HTTPD_CGI_DONE; // Connection aborted. Clean up.
|
||
9 years ago
|
|
||
9 years ago
|
// No changes for Soft-AP in STA mode
|
||
|
int mode = wifi_get_opmode();
|
||
|
if ( mode == 1 ){
|
||
9 years ago
|
jsonHeader(connData, 400);
|
||
9 years ago
|
httpdSend(connData, "No changes allowed in STA mode", -1);
|
||
9 years ago
|
return HTTPD_CGI_DONE;
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
9 years ago
|
char buff[96];
|
||
|
int len;
|
||
9 years ago
|
|
||
9 years ago
|
// Check extra security measure, this must be 1
|
||
9 years ago
|
len=httpdFindArg(connData->getArgs, "100", buff, sizeof(buff));
|
||
|
if(len>0){
|
||
|
if(atoi(buff)!=1){
|
||
9 years ago
|
jsonHeader(connData, 400);
|
||
|
return HTTPD_CGI_DONE;
|
||
|
}
|
||
9 years ago
|
}
|
||
9 years ago
|
// Set new SSID
|
||
9 years ago
|
len=httpdFindArg(connData->getArgs, "ap_ssid", buff, sizeof(buff));
|
||
9 years ago
|
if(checkString(buff) && len>0 && len<=32){
|
||
9 years ago
|
// STRING PREPROCESSING DONE IN CLIENT SIDE
|
||
9 years ago
|
os_memset(apconf.ssid, 0, 32);
|
||
9 years ago
|
os_memcpy(apconf.ssid, buff, len);
|
||
9 years ago
|
apconf.ssid_len = len;
|
||
9 years ago
|
}else{
|
||
|
jsonHeader(connData, 400);
|
||
9 years ago
|
httpdSend(connData, "SSID not valid or out of range", -1);
|
||
9 years ago
|
return HTTPD_CGI_DONE;
|
||
|
}
|
||
|
// Set new PASSWORD
|
||
9 years ago
|
len=httpdFindArg(connData->getArgs, "ap_password", buff, sizeof(buff));
|
||
9 years ago
|
os_memset(apconf.password, 0, 64);
|
||
|
if (checkString(buff) && len>7 && len<=64) {
|
||
9 years ago
|
// String preprocessing done in client side, wifiap.js line 31
|
||
|
os_memcpy(apconf.password, buff, len);
|
||
8 years ago
|
DBG("Setting AP password len=%d\n", len);
|
||
9 years ago
|
} else if (len != 0) {
|
||
9 years ago
|
jsonHeader(connData, 400);
|
||
|
httpdSend(connData, "PASSWORD not valid or out of range", -1);
|
||
|
return HTTPD_CGI_DONE;
|
||
9 years ago
|
}
|
||
|
// Set auth mode
|
||
9 years ago
|
if (len != 0) {
|
||
9 years ago
|
// Set authentication mode, before password to check open settings
|
||
|
len=httpdFindArg(connData->getArgs, "ap_authmode", buff, sizeof(buff));
|
||
9 years ago
|
if (len > 0) {
|
||
9 years ago
|
int value = atoi(buff);
|
||
9 years ago
|
if (value > 0 && value <= 4) {
|
||
9 years ago
|
apconf.authmode = value;
|
||
9 years ago
|
} else {
|
||
9 years ago
|
// If out of range set by default
|
||
8 years ago
|
DBG("Forcing AP authmode to WPA_WPA2_PSK\n");
|
||
9 years ago
|
apconf.authmode = 4;
|
||
|
}
|
||
9 years ago
|
} else {
|
||
9 years ago
|
// Valid password but wrong auth mode, default 4
|
||
8 years ago
|
DBG("Forcing AP authmode to WPA_WPA2_PSK\n");
|
||
9 years ago
|
apconf.authmode = 4;
|
||
9 years ago
|
}
|
||
9 years ago
|
} else {
|
||
9 years ago
|
apconf.authmode = 0;
|
||
9 years ago
|
}
|
||
8 years ago
|
DBG("Setting AP authmode=%d\n", apconf.authmode);
|
||
9 years ago
|
// Set max connection number
|
||
|
len=httpdFindArg(connData->getArgs, "ap_maxconn", buff, sizeof(buff));
|
||
|
if(len>0){
|
||
9 years ago
|
|
||
9 years ago
|
int value = atoi(buff);
|
||
|
if(value > 0 && value <= 4){
|
||
|
apconf.max_connection = value;
|
||
|
}else{
|
||
9 years ago
|
// If out of range, set by default
|
||
9 years ago
|
apconf.max_connection = 4;
|
||
|
}
|
||
9 years ago
|
}
|
||
9 years ago
|
// Set beacon interval value
|
||
|
len=httpdFindArg(connData->getArgs, "ap_beacon", buff, sizeof(buff));
|
||
|
if(len>0){
|
||
|
int value = atoi(buff);
|
||
|
if(value >= 100 && value <= 60000){
|
||
|
apconf.beacon_interval = value;
|
||
|
}else{
|
||
9 years ago
|
// If out of range, set by default
|
||
9 years ago
|
apconf.beacon_interval = 100;
|
||
|
}
|
||
|
}
|
||
|
// Set ssid to be hidden or not
|
||
|
len=httpdFindArg(connData->getArgs, "ap_hidden", buff, sizeof(buff));
|
||
|
if(len>0){
|
||
|
int value = atoi(buff);
|
||
|
if(value == 0 || value == 1){
|
||
|
apconf.ssid_hidden = value;
|
||
|
}else{
|
||
9 years ago
|
// If out of range, set by default
|
||
9 years ago
|
apconf.ssid_hidden = 0;
|
||
|
}
|
||
|
}
|
||
|
// Store new configuration
|
||
|
wifi_softap_set_config(&apconf);
|
||
9 years ago
|
|
||
9 years ago
|
jsonHeader(connData, 200);
|
||
|
return HTTPD_CGI_DONE;
|
||
|
}
|
||
|
|
||
|
// Get current Soft-AP settings
|
||
|
int ICACHE_FLASH_ATTR cgiApSettingsInfo(HttpdConnData *connData) {
|
||
9 years ago
|
|
||
9 years ago
|
char buff[1024];
|
||
|
if (connData->conn == NULL) return HTTPD_CGI_DONE; // Connection aborted. Clean up.
|
||
|
os_sprintf(buff,
|
||
|
"{ "
|
||
|
"\"ap_ssid\": \"%s\", "
|
||
|
"\"ap_password\": \"%s\", "
|
||
|
"\"ap_authmode\": %d, "
|
||
|
"\"ap_maxconn\": %d, "
|
||
|
"\"ap_beacon\": %d, "
|
||
|
"\"ap_hidden\": \"%s\" "
|
||
|
" }",
|
||
|
apconf.ssid,
|
||
|
apconf.password,
|
||
|
apconf.authmode,
|
||
|
apconf.max_connection,
|
||
|
apconf.beacon_interval,
|
||
|
apconf.ssid_hidden ? "enabled" : "disabled"
|
||
|
);
|
||
9 years ago
|
|
||
9 years ago
|
jsonHeader(connData, 200);
|
||
9 years ago
|
httpdSend(connData, buff, -1);
|
||
9 years ago
|
return HTTPD_CGI_DONE;
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
//This cgi changes the operating mode: STA / AP / STA+AP
|
||
10 years ago
|
int ICACHE_FLASH_ATTR cgiWiFiSetMode(HttpdConnData *connData) {
|
||
9 years ago
|
int len;
|
||
|
char buff[1024];
|
||
|
int previous_mode = wifi_get_opmode();
|
||
|
if (connData->conn==NULL) return HTTPD_CGI_DONE; // Connection aborted. Clean up.
|
||
|
|
||
|
len=httpdFindArg(connData->getArgs, "mode", buff, sizeof(buff));
|
||
9 years ago
|
int next_mode = atoi(buff);
|
||
9 years ago
|
|
||
9 years ago
|
if (len!=0) {
|
||
9 years ago
|
if (next_mode == 2){
|
||
|
// moving to AP mode, so disconnect before leave STA mode
|
||
|
wifi_station_disconnect();
|
||
|
}
|
||
9 years ago
|
|
||
9 years ago
|
DBG("Wifi switching to mode %d\n", next_mode);
|
||
|
wifi_set_opmode(next_mode&3);
|
||
9 years ago
|
|
||
9 years ago
|
if (previous_mode == 2) {
|
||
9 years ago
|
// moving to STA or STA+AP mode from AP, try to connect and set timer
|
||
9 years ago
|
stconf.bssid_set = 0;
|
||
|
wifi_station_set_config(&stconf);
|
||
|
wifi_station_connect();
|
||
9 years ago
|
os_timer_disarm(&resetTimer);
|
||
|
os_timer_setfn(&resetTimer, resetTimerCb, NULL);
|
||
7 years ago
|
os_timer_arm(&resetTimer, RESET_TIMEOUT, 0);
|
||
9 years ago
|
}
|
||
9 years ago
|
if(previous_mode == 1){
|
||
9 years ago
|
// moving to AP or STA+AP from STA, so softap config call needed
|
||
9 years ago
|
wifi_softap_set_config(&apconf);
|
||
|
}
|
||
9 years ago
|
jsonHeader(connData, 200);
|
||
|
} else {
|
||
|
jsonHeader(connData, 400);
|
||
9 years ago
|
}
|
||
9 years ago
|
return HTTPD_CGI_DONE;
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
static char *connStatuses[] = { "idle", "connecting", "wrong password", "AP not found",
|
||
9 years ago
|
"failed", "got IP address" };
|
||
10 years ago
|
|
||
10 years ago
|
static char *wifiWarn[] = { 0,
|
||
9 years ago
|
"Switch to <a href=\\\"#\\\" onclick=\\\"changeWifiMode(3)\\\">STA+AP mode</a>",
|
||
9 years ago
|
"Switch to <a href=\\\"#\\\" onclick=\\\"changeWifiMode(3)\\\">STA+AP mode</a>",
|
||
9 years ago
|
"Switch to <a href=\\\"#\\\" onclick=\\\"changeWifiMode(1)\\\">STA mode</a>",
|
||
9 years ago
|
"Switch to <a href=\\\"#\\\" onclick=\\\"changeWifiMode(2)\\\">AP mode</a>",
|
||
|
};
|
||
|
|
||
|
static char *apAuthMode[] = { "OPEN",
|
||
|
"WEP",
|
||
|
"WPA_PSK",
|
||
|
"WPA2_PSK",
|
||
|
"WPA_WPA2_PSK",
|
||
|
};
|
||
|
|
||
9 years ago
|
#ifdef CHANGE_TO_STA
|
||
|
#define MODECHANGE "yes"
|
||
|
#else
|
||
|
#define MODECHANGE "no"
|
||
|
#endif
|
||
|
|
||
10 years ago
|
// print various Wifi information into json buffer
|
||
|
int ICACHE_FLASH_ATTR printWifiInfo(char *buff) {
|
||
9 years ago
|
int len;
|
||
9 years ago
|
//struct station_config stconf;
|
||
|
wifi_station_get_config(&stconf);
|
||
|
//struct softap_config apconf;
|
||
|
wifi_softap_get_config(&apconf);
|
||
9 years ago
|
|
||
9 years ago
|
uint8_t op = wifi_get_opmode() & 0x3;
|
||
|
char *mode = wifiMode[op];
|
||
|
char *status = "unknown";
|
||
|
int st = wifi_station_get_connect_status();
|
||
|
if (st >= 0 && st < sizeof(connStatuses)) status = connStatuses[st];
|
||
|
int p = wifi_get_phy_mode();
|
||
|
char *phy = wifiPhy[p&3];
|
||
|
char *warn = wifiWarn[op];
|
||
9 years ago
|
if (op == 3) op = 4; // Done to let user switch to AP only mode from Soft-AP settings page, using only one set of warnings
|
||
9 years ago
|
char *apwarn = wifiWarn[op];
|
||
9 years ago
|
char *apauth = apAuthMode[apconf.authmode];
|
||
9 years ago
|
sint8 rssi = wifi_station_get_rssi();
|
||
|
if (rssi > 0) rssi = 0;
|
||
|
uint8 mac_addr[6];
|
||
|
uint8 apmac_addr[6];
|
||
|
wifi_get_macaddr(0, mac_addr);
|
||
9 years ago
|
wifi_get_macaddr(1, apmac_addr);
|
||
9 years ago
|
uint8_t chan = wifi_get_channel();
|
||
9 years ago
|
|
||
9 years ago
|
len = os_sprintf(buff,
|
||
9 years ago
|
"\"mode\": \"%s\", \"modechange\": \"%s\", \"ssid\": \"%s\", \"status\": \"%s\", "
|
||
|
"\"phy\": \"%s\", \"rssi\": \"%ddB\", \"warn\": \"%s\", \"apwarn\": \"%s\", "
|
||
|
"\"mac\":\"%02x:%02x:%02x:%02x:%02x:%02x\", \"chan\":\"%d\", \"apssid\": \"%s\", "
|
||
|
"\"appass\": \"%s\", \"apchan\": \"%d\", \"apmaxc\": \"%d\", \"aphidd\": \"%s\", "
|
||
|
"\"apbeac\": \"%d\", \"apauth\": \"%s\",\"apmac\":\"%02x:%02x:%02x:%02x:%02x:%02x\"",
|
||
|
mode, MODECHANGE, (char*)stconf.ssid, status, phy, rssi, warn, apwarn,
|
||
|
mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5],
|
||
|
chan, (char*)apconf.ssid, (char*)apconf.password, apconf.channel, apconf.max_connection,
|
||
|
apconf.ssid_hidden?"enabled":"disabled", apconf.beacon_interval,
|
||
|
apauth,apmac_addr[0], apmac_addr[1], apmac_addr[2], apmac_addr[3], apmac_addr[4],
|
||
|
apmac_addr[5]);
|
||
9 years ago
|
|
||
9 years ago
|
struct ip_info info;
|
||
|
if (wifi_get_ip_info(0, &info)) {
|
||
|
len += os_sprintf(buff+len, ", \"ip\": \"%d.%d.%d.%d\"", IP2STR(&info.ip.addr));
|
||
|
len += os_sprintf(buff+len, ", \"netmask\": \"%d.%d.%d.%d\"", IP2STR(&info.netmask.addr));
|
||
|
len += os_sprintf(buff+len, ", \"gateway\": \"%d.%d.%d.%d\"", IP2STR(&info.gw.addr));
|
||
|
len += os_sprintf(buff+len, ", \"hostname\": \"%s\"", flashConfig.hostname);
|
||
|
} else {
|
||
|
len += os_sprintf(buff+len, ", \"ip\": \"-none-\"");
|
||
|
}
|
||
|
len += os_sprintf(buff+len, ", \"staticip\": \"%d.%d.%d.%d\"", IP2STR(&flashConfig.staticip));
|
||
|
len += os_sprintf(buff+len, ", \"dhcp\": \"%s\"", flashConfig.staticip > 0 ? "off" : "on");
|
||
9 years ago
|
|
||
9 years ago
|
return len;
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
int ICACHE_FLASH_ATTR cgiWiFiConnStatus(HttpdConnData *connData) {
|
||
9 years ago
|
char buff[1024];
|
||
|
int len;
|
||
|
|
||
|
if (connData->conn==NULL) return HTTPD_CGI_DONE; // Connection aborted. Clean up.
|
||
|
jsonHeader(connData, 200);
|
||
|
|
||
|
len = os_sprintf(buff, "{");
|
||
|
len += printWifiInfo(buff+len);
|
||
|
len += os_sprintf(buff+len, ", ");
|
||
|
|
||
|
if (wifiReason != 0) {
|
||
|
len += os_sprintf(buff+len, "\"reason\": \"%s\", ", wifiGetReason());
|
||
|
}
|
||
|
|
||
10 years ago
|
#if 0
|
||
9 years ago
|
// commented out 'cause often the client that requested the change can't get a request in to
|
||
|
// find out that it succeeded. Better to just wait the std 15 seconds...
|
||
|
int st=wifi_station_get_connect_status();
|
||
|
if (st == STATION_GOT_IP) {
|
||
|
if (wifi_get_opmode() != 1) {
|
||
|
// Reset into AP-only mode sooner.
|
||
|
os_timer_disarm(&resetTimer);
|
||
|
os_timer_setfn(&resetTimer, resetTimerCb, NULL);
|
||
7 years ago
|
os_timer_arm(&resetTimer, 1000, 0);
|
||
9 years ago
|
}
|
||
|
}
|
||
10 years ago
|
#endif
|
||
9 years ago
|
|
||
|
len += os_sprintf(buff+len, "\"x\":0}\n");
|
||
|
//DBG(" -> %s\n", buff);
|
||
|
httpdSend(connData, buff, len);
|
||
|
return HTTPD_CGI_DONE;
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
// Cgi to return various Wifi information
|
||
|
int ICACHE_FLASH_ATTR cgiWifiInfo(HttpdConnData *connData) {
|
||
9 years ago
|
char buff[1024];
|
||
|
|
||
|
if (connData->conn==NULL) return HTTPD_CGI_DONE; // Connection aborted. Clean up.
|
||
|
|
||
|
os_strcpy(buff, "{");
|
||
|
printWifiInfo(buff+1);
|
||
|
os_strcat(buff, "}");
|
||
|
|
||
|
jsonHeader(connData, 200);
|
||
|
httpdSend(connData, buff, -1);
|
||
|
return HTTPD_CGI_DONE;
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
9 years ago
|
// Check string againt invalid characters
|
||
|
int ICACHE_FLASH_ATTR checkString(char *str){
|
||
9 years ago
|
for(int i=0; i<os_strlen(str); i++) {
|
||
|
// We allow any printable character
|
||
|
if (str[i] < '!' || str[i] > '~') {
|
||
9 years ago
|
DBG("Error: String has non alphanumeric chars\n");
|
||
9 years ago
|
return 0;
|
||
9 years ago
|
}
|
||
|
}
|
||
9 years ago
|
return 1;
|
||
10 years ago
|
}
|
||
|
|
||
9 years ago
|
/* Init the wireless
|
||
|
*
|
||
|
* Call both Soft-AP and Station default config
|
||
|
* Change values according to Makefile hard-coded variables
|
||
|
* Anyway set wifi opmode to STA+AP, it will change to STA if CHANGE_TO_STA is set to yes in Makefile
|
||
|
* Call a timer to check the STA connection
|
||
|
*/
|
||
10 years ago
|
void ICACHE_FLASH_ATTR wifiInit() {
|
||
9 years ago
|
|
||
9 years ago
|
// Check the wifi opmode
|
||
9 years ago
|
int x = wifi_get_opmode() & 0x3;
|
||
9 years ago
|
|
||
9 years ago
|
// If STA is enabled switch to STA+AP to allow for recovery, it will then switch to STA-only
|
||
|
// once it gets an IP address
|
||
|
if (x == 1) wifi_set_opmode(3);
|
||
9 years ago
|
|
||
9 years ago
|
// Call both STATION and SOFTAP default config
|
||
|
wifi_station_get_config_default(&stconf);
|
||
|
wifi_softap_get_config_default(&apconf);
|
||
9 years ago
|
|
||
9 years ago
|
DBG("Wifi init, mode=%s\n",wifiMode[x]);
|
||
9 years ago
|
|
||
9 years ago
|
// Change STATION parameters, if defined in the Makefile
|
||
9 years ago
|
#if defined(STA_SSID) && defined(STA_PASS)
|
||
9 years ago
|
// Set parameters
|
||
|
if (os_strlen((char*)stconf.ssid) == 0 && os_strlen((char*)stconf.password) == 0) {
|
||
|
os_strncpy((char*)stconf.ssid, VERS_STR(STA_SSID), 32);
|
||
|
os_strncpy((char*)stconf.password, VERS_STR(STA_PASS), 64);
|
||
9 years ago
|
|
||
9 years ago
|
wifi_set_opmode(3);
|
||
|
|
||
9 years ago
|
DBG("Wifi pre-config trying to connect to AP %s pw %s\n",(char*)stconf.ssid, (char*)stconf.password);
|
||
9 years ago
|
|
||
9 years ago
|
// wifi_set_phy_mode(2); // limit to 802.11b/g 'cause n is flaky
|
||
|
stconf.bssid_set = 0;
|
||
|
wifi_station_set_config(&stconf);
|
||
|
}
|
||
9 years ago
|
#endif
|
||
9 years ago
|
|
||
9 years ago
|
// Change SOFT_AP settings, if defined in Makefile
|
||
9 years ago
|
#if defined(AP_SSID)
|
||
|
// Check if ssid and pass are alphanumeric values
|
||
|
int ssidlen = os_strlen(VERS_STR(AP_SSID));
|
||
|
if(checkString(VERS_STR(AP_SSID)) && ssidlen > 7 && ssidlen < 32){
|
||
9 years ago
|
// Clean memory and set the value of SSID
|
||
9 years ago
|
os_memset(apconf.ssid, 0, 32);
|
||
9 years ago
|
os_memcpy(apconf.ssid, VERS_STR(AP_SSID), os_strlen(VERS_STR(AP_SSID)));
|
||
9 years ago
|
// Specify the length of ssid
|
||
9 years ago
|
apconf.ssid_len= ssidlen;
|
||
|
#if defined(AP_PASS)
|
||
9 years ago
|
// If pass is at least 8 and less than 64
|
||
|
int passlen = os_strlen(VERS_STR(AP_PASS));
|
||
9 years ago
|
if( checkString(VERS_STR(AP_PASS)) && passlen > 7 && passlen < 64 ){
|
||
9 years ago
|
// Clean memory and set the value of PASS
|
||
9 years ago
|
os_memset(apconf.password, 0, 64);
|
||
9 years ago
|
os_memcpy(apconf.password, VERS_STR(AP_PASS), passlen);
|
||
|
// Can't choose auth mode without a valid ssid and password
|
||
|
#ifdef AP_AUTH_MODE
|
||
|
// If set, use specified auth mode
|
||
|
if(AP_AUTH_MODE >= 0 && AP_AUTH_MODE <=4)
|
||
|
apconf.authmode = AP_AUTH_MODE;
|
||
|
#else
|
||
9 years ago
|
// If not, use WPA2
|
||
|
apconf.authmode = AUTH_WPA_WPA2_PSK;
|
||
9 years ago
|
#endif
|
||
|
}else if ( passlen == 0){
|
||
9 years ago
|
// If ssid is ok and no pass, set auth open
|
||
9 years ago
|
apconf.authmode = AUTH_OPEN;
|
||
|
// Remove stored password
|
||
9 years ago
|
os_memset(apconf.password, 0, 64);
|
||
9 years ago
|
}
|
||
9 years ago
|
#endif
|
||
9 years ago
|
}// end of ssid and pass check
|
||
|
#ifdef AP_SSID_HIDDEN
|
||
|
// If set, use specified ssid hidden parameter
|
||
|
if(AP_SSID_HIDDEN == 0 || AP_SSID_HIDDEN ==1)
|
||
9 years ago
|
apconf.ssid_hidden = AP_SSID_HIDDEN;
|
||
9 years ago
|
#endif
|
||
|
#ifdef AP_MAX_CONN
|
||
|
// If set, use specified max conn number
|
||
|
if(AP_MAX_CONN > 0 && AP_MAX_CONN <5)
|
||
9 years ago
|
apconf.max_connection = AP_MAX_CONN;
|
||
9 years ago
|
#endif
|
||
|
#ifdef AP_BEACON_INTERVAL
|
||
|
// If set use specified beacon interval
|
||
|
if(AP_BEACON_INTERVAL >= 100 && AP_BEACON_INTERVAL <= 60000)
|
||
9 years ago
|
apconf.beacon_interval = AP_BEACON_INTERVAL;
|
||
9 years ago
|
#endif
|
||
9 years ago
|
// Check softap config
|
||
9 years ago
|
bool softap_set_conf = wifi_softap_set_config(&apconf);
|
||
|
// Debug info
|
||
9 years ago
|
|
||
9 years ago
|
DBG("Wifi Soft-AP parameters change: %s\n",softap_set_conf? "success":"fail");
|
||
9 years ago
|
#endif // if defined(AP_SSID)
|
||
9 years ago
|
|
||
9 years ago
|
configWifiIP();
|
||
9 years ago
|
|
||
9 years ago
|
// The default sleep mode should be modem_sleep, but we set it here explicitly for good
|
||
|
// measure. We can't use light_sleep because that powers off everthing and we would loose
|
||
|
// all connections.
|
||
|
wifi_set_sleep_type(MODEM_SLEEP_T);
|
||
9 years ago
|
|
||
9 years ago
|
wifi_set_event_handler_cb(wifiHandleEventCb);
|
||
|
// check on the wifi in a few seconds to see whether we need to switch mode
|
||
|
os_timer_disarm(&resetTimer);
|
||
|
os_timer_setfn(&resetTimer, resetTimerCb, NULL);
|
||
7 years ago
|
os_timer_arm(&resetTimer, RESET_TIMEOUT, 0);
|
||
9 years ago
|
}
|
||
8 years ago
|
|
||
|
// Access functions for cgiWifiAps : query the number of entries in the table
|
||
|
int ICACHE_FLASH_ATTR wifiGetApCount() {
|
||
|
if (cgiWifiAps.scanInProgress)
|
||
|
return 0;
|
||
|
return cgiWifiAps.noAps;
|
||
|
}
|
||
|
|
||
|
// Access functions for cgiWifiAps : returns the name of a network, i is the index into the array, return stored in memory pointed to by ptr.
|
||
|
ICACHE_FLASH_ATTR void wifiGetApName(int i, char *ptr) {
|
||
|
if (i < 0)
|
||
|
return;
|
||
|
if (i >= cgiWifiAps.noAps)
|
||
|
return;
|
||
|
|
||
|
if (ptr != 0)
|
||
|
strncpy(ptr, cgiWifiAps.apData[i]->ssid, 32);
|
||
|
|
||
|
DBG("AP %s\n", cgiWifiAps.apData[i]->ssid);
|
||
|
}
|
||
|
|
||
|
// Access functions for cgiWifiAps : returns the signal strength of network (i is index into array). Return current network strength for negative i.
|
||
8 years ago
|
ICACHE_FLASH_ATTR int wifiSignalStrength(int i) {
|
||
8 years ago
|
sint8 rssi;
|
||
|
|
||
|
if (i < 0 || i == 255)
|
||
|
rssi = wifi_station_get_rssi(); // Current network's signal strength
|
||
|
else if (i >= cgiWifiAps.noAps)
|
||
|
rssi = 0; // FIX ME
|
||
|
else
|
||
|
rssi = cgiWifiAps.apData[i]->rssi; // Signal strength of any known network
|
||
|
|
||
|
return rssi;
|
||
|
}
|