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.
312 lines
9.3 KiB
312 lines
9.3 KiB
10 years ago
|
/*
|
||
|
Cgi/template routines for the /wifi url.
|
||
|
*/
|
||
|
|
||
|
/*
|
||
|
* ----------------------------------------------------------------------------
|
||
|
* "THE BEER-WARE LICENSE" (Revision 42):
|
||
|
* 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
|
#include <esp8266.h>
|
||
|
#include "cgiwifi.h"
|
||
10 years ago
|
|
||
10 years ago
|
//Enable this to disallow any changes in AP settings
|
||
|
//#define DEMO_MODE
|
||
|
|
||
10 years ago
|
//WiFi access point data
|
||
|
typedef struct {
|
||
|
char ssid[32];
|
||
|
char rssi;
|
||
|
char enc;
|
||
|
} ApData;
|
||
|
|
||
10 years ago
|
//Scan result
|
||
10 years ago
|
typedef struct {
|
||
10 years ago
|
char scanInProgress; //if 1, don't access the underlying stuff from the webpage.
|
||
10 years ago
|
ApData **apData;
|
||
|
int noAps;
|
||
|
} ScanResultData;
|
||
|
|
||
|
//Static scan status storage.
|
||
10 years ago
|
static ScanResultData cgiWifiAps;
|
||
|
|
||
|
#define CONNTRY_IDLE 0
|
||
|
#define CONNTRY_WORKING 1
|
||
|
#define CONNTRY_SUCCESS 2
|
||
|
#define CONNTRY_FAIL 3
|
||
|
//Connection result var
|
||
|
static int connTryStatus=CONNTRY_IDLE;
|
||
|
static ETSTimer resetTimer;
|
||
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) {
|
||
|
int n;
|
||
|
struct bss_info *bss_link = (struct bss_info *)arg;
|
||
|
os_printf("wifiScanDoneCb %d\n", status);
|
||
|
if (status!=OK) {
|
||
|
cgiWifiAps.scanInProgress=0;
|
||
|
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;
|
||
10 years ago
|
os_printf("Scan done: found %d APs\n", n);
|
||
10 years ago
|
|
||
|
//Copy access point data to the static struct
|
||
|
n=0;
|
||
|
bss_link = (struct bss_info *)arg;
|
||
|
while (bss_link != NULL) {
|
||
10 years ago
|
if (n>=cgiWifiAps.noAps) {
|
||
10 years ago
|
//This means the bss_link changed under our nose. Shouldn't happen!
|
||
|
//Break because otherwise we will write in unallocated memory.
|
||
10 years ago
|
os_printf("Huh? I have more than the allocated %d aps!\n", cgiWifiAps.noAps);
|
||
|
break;
|
||
|
}
|
||
10 years ago
|
//Save the ap data.
|
||
10 years ago
|
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);
|
||
|
|
||
|
bss_link = bss_link->next.stqe_next;
|
||
|
n++;
|
||
|
}
|
||
|
//We're done.
|
||
|
cgiWifiAps.scanInProgress=0;
|
||
|
}
|
||
|
|
||
|
|
||
|
//Routine to start a WiFi access point scan.
|
||
|
static void ICACHE_FLASH_ATTR wifiStartScan() {
|
||
10 years ago
|
// int x;
|
||
|
if (cgiWifiAps.scanInProgress) return;
|
||
10 years ago
|
cgiWifiAps.scanInProgress=1;
|
||
|
wifi_station_scan(NULL, wifiScanDoneCb);
|
||
|
}
|
||
|
|
||
|
//This CGI is called from the bit of AJAX-code in wifi.tpl. It will initiate a
|
||
|
//scan for access points and if available will return the result of an earlier scan.
|
||
|
//The result is embedded in a bit of JSON parsed by the javascript in wifi.tpl.
|
||
|
int ICACHE_FLASH_ATTR cgiWiFiScan(HttpdConnData *connData) {
|
||
10 years ago
|
int pos=(int)connData->cgiData;
|
||
10 years ago
|
int len;
|
||
|
char buff[1024];
|
||
10 years ago
|
|
||
|
if (!cgiWifiAps.scanInProgress && pos!=0) {
|
||
|
//Fill in json code for an access point
|
||
|
if (pos-1<cgiWifiAps.noAps) {
|
||
|
len=os_sprintf(buff, "{\"essid\": \"%s\", \"rssi\": \"%d\", \"enc\": \"%d\"}%s\n",
|
||
|
cgiWifiAps.apData[pos-1]->ssid, cgiWifiAps.apData[pos-1]->rssi,
|
||
|
cgiWifiAps.apData[pos-1]->enc, (pos-1==cgiWifiAps.noAps-1)?"":",");
|
||
|
httpdSend(connData, buff, len);
|
||
|
}
|
||
|
pos++;
|
||
|
if ((pos-1)>=cgiWifiAps.noAps) {
|
||
|
len=os_sprintf(buff, "]\n}\n}\n");
|
||
|
httpdSend(connData, buff, len);
|
||
|
//Also start a new scan.
|
||
|
wifiStartScan();
|
||
|
return HTTPD_CGI_DONE;
|
||
|
} else {
|
||
|
connData->cgiData=(void*)pos;
|
||
|
return HTTPD_CGI_MORE;
|
||
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
httpdStartResponse(connData, 200);
|
||
|
httpdHeader(connData, "Content-Type", "text/json");
|
||
|
httpdEndHeaders(connData);
|
||
|
|
||
|
if (cgiWifiAps.scanInProgress==1) {
|
||
10 years ago
|
//We're still scanning. Tell Javascript code that.
|
||
10 years ago
|
len=os_sprintf(buff, "{\n \"result\": { \n\"inProgress\": \"1\"\n }\n}\n");
|
||
10 years ago
|
httpdSend(connData, buff, len);
|
||
10 years ago
|
return HTTPD_CGI_DONE;
|
||
10 years ago
|
} else {
|
||
10 years ago
|
//We have a scan result. Pass it on.
|
||
10 years ago
|
len=os_sprintf(buff, "{\n \"result\": { \n\"inProgress\": \"0\", \n\"APs\": [\n");
|
||
10 years ago
|
httpdSend(connData, buff, len);
|
||
10 years ago
|
if (cgiWifiAps.apData==NULL) cgiWifiAps.noAps=0;
|
||
10 years ago
|
connData->cgiData=(void *)1;
|
||
|
return HTTPD_CGI_MORE;
|
||
10 years ago
|
}
|
||
|
}
|
||
|
|
||
|
//Temp store for new ap info.
|
||
|
static struct station_config stconf;
|
||
|
|
||
|
//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.
|
||
|
static void ICACHE_FLASH_ATTR resetTimerCb(void *arg) {
|
||
|
int x=wifi_station_get_connect_status();
|
||
|
if (x==STATION_GOT_IP) {
|
||
|
//Go to STA mode. This needs a reset, so do that.
|
||
10 years ago
|
os_printf("Got IP. Going into STA mode..\n");
|
||
10 years ago
|
wifi_set_opmode(1);
|
||
|
system_restart();
|
||
|
} else {
|
||
10 years ago
|
connTryStatus=CONNTRY_FAIL;
|
||
10 years ago
|
os_printf("Connect fail. Not going into STA-only mode.\n");
|
||
10 years ago
|
//Maybe also pass this through on the webpage?
|
||
10 years ago
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
|
||
|
|
||
10 years ago
|
//Actually connect to a station. This routine is timed because I had problems
|
||
|
//with immediate connections earlier. It probably was something else that caused it,
|
||
|
//but I can't be arsed to put the code back :P
|
||
|
static void ICACHE_FLASH_ATTR reassTimerCb(void *arg) {
|
||
|
int x;
|
||
10 years ago
|
os_printf("Try to connect to AP....\n");
|
||
10 years ago
|
wifi_station_disconnect();
|
||
|
wifi_station_set_config(&stconf);
|
||
|
wifi_station_connect();
|
||
|
x=wifi_get_opmode();
|
||
10 years ago
|
connTryStatus=CONNTRY_WORKING;
|
||
10 years ago
|
if (x!=1) {
|
||
|
//Schedule disconnect/connect
|
||
|
os_timer_disarm(&resetTimer);
|
||
|
os_timer_setfn(&resetTimer, resetTimerCb, NULL);
|
||
10 years ago
|
os_timer_arm(&resetTimer, 15000, 0); //time out after 15 secs of trying to connect
|
||
10 years ago
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
//This cgi uses the routines above to connect to a specific access point with the
|
||
|
//given ESSID using the given password.
|
||
|
int ICACHE_FLASH_ATTR cgiWiFiConnect(HttpdConnData *connData) {
|
||
|
char essid[128];
|
||
|
char passwd[128];
|
||
|
static ETSTimer reassTimer;
|
||
|
|
||
|
if (connData->conn==NULL) {
|
||
|
//Connection aborted. Clean up.
|
||
|
return HTTPD_CGI_DONE;
|
||
|
}
|
||
|
|
||
10 years ago
|
httpdFindArg(connData->post->buff, "essid", essid, sizeof(essid));
|
||
|
httpdFindArg(connData->post->buff, "passwd", passwd, sizeof(passwd));
|
||
10 years ago
|
|
||
|
os_strncpy((char*)stconf.ssid, essid, 32);
|
||
|
os_strncpy((char*)stconf.password, passwd, 64);
|
||
10 years ago
|
os_printf("Try to connect to AP %s pw %s\n", essid, passwd);
|
||
10 years ago
|
|
||
|
//Schedule disconnect/connect
|
||
|
os_timer_disarm(&reassTimer);
|
||
|
os_timer_setfn(&reassTimer, reassTimerCb, NULL);
|
||
10 years ago
|
//Set to 0 if you want to disable the actual reconnecting bit
|
||
10 years ago
|
#ifdef DEMO_MODE
|
||
|
httpdRedirect(connData, "/wifi");
|
||
|
#else
|
||
10 years ago
|
os_timer_arm(&reassTimer, 500, 0);
|
||
10 years ago
|
httpdRedirect(connData, "connecting.html");
|
||
10 years ago
|
#endif
|
||
10 years ago
|
return HTTPD_CGI_DONE;
|
||
|
}
|
||
|
|
||
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 cgiWiFiSetMode(HttpdConnData *connData) {
|
||
10 years ago
|
int len;
|
||
|
char buff[1024];
|
||
|
|
||
|
if (connData->conn==NULL) {
|
||
|
//Connection aborted. Clean up.
|
||
|
return HTTPD_CGI_DONE;
|
||
|
}
|
||
|
|
||
|
len=httpdFindArg(connData->getArgs, "mode", buff, sizeof(buff));
|
||
|
if (len!=0) {
|
||
|
os_printf("cgiWifiSetMode: %s\n", buff);
|
||
10 years ago
|
#ifndef DEMO_MODE
|
||
10 years ago
|
wifi_set_opmode(atoi(buff));
|
||
|
system_restart();
|
||
10 years ago
|
#endif
|
||
10 years ago
|
}
|
||
|
httpdRedirect(connData, "/wifi");
|
||
|
return HTTPD_CGI_DONE;
|
||
|
}
|
||
|
|
||
10 years ago
|
int ICACHE_FLASH_ATTR cgiWiFiConnStatus(HttpdConnData *connData) {
|
||
|
char buff[1024];
|
||
|
int len;
|
||
|
struct ip_info info;
|
||
|
int st=wifi_station_get_connect_status();
|
||
|
httpdStartResponse(connData, 200);
|
||
|
httpdHeader(connData, "Content-Type", "text/json");
|
||
|
httpdEndHeaders(connData);
|
||
|
if (connTryStatus==CONNTRY_IDLE) {
|
||
|
len=os_sprintf(buff, "{\n \"status\": \"idle\"\n }\n");
|
||
|
} else if (connTryStatus==CONNTRY_WORKING || connTryStatus==CONNTRY_SUCCESS) {
|
||
|
if (st==STATION_GOT_IP) {
|
||
|
wifi_get_ip_info(0, &info);
|
||
|
len=os_sprintf(buff, "{\n \"status\": \"success\",\n \"ip\": \"%d.%d.%d.%d\" }\n",
|
||
|
(info.ip.addr>>0)&0xff, (info.ip.addr>>8)&0xff,
|
||
|
(info.ip.addr>>16)&0xff, (info.ip.addr>>24)&0xff);
|
||
|
//Reset into AP-only mode sooner.
|
||
|
os_timer_disarm(&resetTimer);
|
||
|
os_timer_setfn(&resetTimer, resetTimerCb, NULL);
|
||
|
os_timer_arm(&resetTimer, 1000, 0);
|
||
|
} else {
|
||
|
len=os_sprintf(buff, "{\n \"status\": \"working\"\n }\n");
|
||
|
}
|
||
|
} else {
|
||
|
len=os_sprintf(buff, "{\n \"status\": \"fail\"\n }\n");
|
||
|
}
|
||
|
|
||
|
httpdSend(connData, buff, len);
|
||
|
return HTTPD_CGI_DONE;
|
||
|
}
|
||
|
|
||
10 years ago
|
//Template code for the WLAN page.
|
||
10 years ago
|
int ICACHE_FLASH_ATTR tplWlan(HttpdConnData *connData, char *token, void **arg) {
|
||
10 years ago
|
char buff[1024];
|
||
|
int x;
|
||
|
static struct station_config stconf;
|
||
10 years ago
|
if (token==NULL) return HTTPD_CGI_DONE;
|
||
10 years ago
|
wifi_station_get_config(&stconf);
|
||
|
|
||
|
os_strcpy(buff, "Unknown");
|
||
|
if (os_strcmp(token, "WiFiMode")==0) {
|
||
|
x=wifi_get_opmode();
|
||
|
if (x==1) os_strcpy(buff, "Client");
|
||
|
if (x==2) os_strcpy(buff, "SoftAP");
|
||
|
if (x==3) os_strcpy(buff, "STA+AP");
|
||
|
} else if (os_strcmp(token, "currSsid")==0) {
|
||
|
os_strcpy(buff, (char*)stconf.ssid);
|
||
|
} else if (os_strcmp(token, "WiFiPasswd")==0) {
|
||
|
os_strcpy(buff, (char*)stconf.password);
|
||
10 years ago
|
} else if (os_strcmp(token, "WiFiapwarn")==0) {
|
||
|
x=wifi_get_opmode();
|
||
|
if (x==2) {
|
||
|
os_strcpy(buff, "<b>Can't scan in this mode.</b> Click <a href=\"setmode.cgi?mode=3\">here</a> to go to STA+AP mode.");
|
||
|
} else {
|
||
|
os_strcpy(buff, "Click <a href=\"setmode.cgi?mode=2\">here</a> to go to standalone AP mode.");
|
||
|
}
|
||
10 years ago
|
}
|
||
10 years ago
|
httpdSend(connData, buff, -1);
|
||
10 years ago
|
return HTTPD_CGI_DONE;
|
||
10 years ago
|
}
|
||
|
|
||
|
|