diff --git a/driver/uart.c b/driver/uart.c index b111dd0..27558a6 100644 --- a/driver/uart.c +++ b/driver/uart.c @@ -9,6 +9,7 @@ * Modification history: * 2014/3/12, v1.0 create this file. *******************************************************************************/ +#include "espmissingincludes.h" #include "ets_sys.h" #include "osapi.h" #include "driver/uart.h" diff --git a/html/cat.jpeg b/html/cat.jpeg deleted file mode 100644 index c807409..0000000 Binary files a/html/cat.jpeg and /dev/null differ diff --git a/include/driver/uart.h b/include/driver/uart.h index 957621b..6cdc3f5 100644 --- a/include/driver/uart.h +++ b/include/driver/uart.h @@ -97,5 +97,7 @@ typedef struct { void uart_init(UartBautRate uart0_br, UartBautRate uart1_br); void uart0_sendStr(const char *str); + + #endif diff --git a/user/cgi.c b/user/cgi.c index 6aa3e4c..cd75a55 100644 --- a/user/cgi.c +++ b/user/cgi.c @@ -4,9 +4,12 @@ Some random cgi routines. #include #include +#include "user_interface.h" +#include "mem.h" #include "httpd.h" #include "cgi.h" #include "io.h" +#include "espmissingincludes.h" int ICACHE_FLASH_ATTR cgiLed(HttpdConnData *connData) { int len; @@ -18,33 +21,215 @@ int ICACHE_FLASH_ATTR cgiLed(HttpdConnData *connData) { } len=httpdFindArg(connData->postBuff, "led", buff, sizeof(buff)); - ioLed(atoi(buff)); + if (len!=0) ioLed(atoi(buff)); httpdRedirect(connData, "led.html"); return HTTPD_CGI_DONE; } -int ICACHE_FLASH_ATTR cgiTest(HttpdConnData *connData) { + +//WiFi access point data +typedef struct { + char ssid[32]; + char rssi; + char enc; +} ApData; + +//Scan resolt +typedef struct { + char scanInProgress; + ApData **apData; + int noAps; +} ScanResultData; + +//Static scan status storage. +ScanResultData cgiWifiAps; + +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; + wifi_station_disconnect(); //test HACK + return; + } + + //Clear prev ap data if needed. + if (cgiWifiAps.apData!=NULL) { + for (n=0; nnext.stqe_next; + n++; + } + //Allocate memory for access point data + cgiWifiAps.apData=(ApData **)os_malloc(sizeof(ApData *)*n); + cgiWifiAps.noAps=n; + + //Copy access point data to the static struct + n=0; + bss_link = (struct bss_info *)arg; + while (bss_link != NULL) { + 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++; + } + os_printf("Scan done: found %d APs\n", n); + //We're done. + cgiWifiAps.scanInProgress=0; +} + +static void ICACHE_FLASH_ATTR wifiStartScan() { + int x; + cgiWifiAps.scanInProgress=1; + x=wifi_station_get_connect_status(); + if (x!=STATION_GOT_IP) { + //Unit probably is trying to connect to a bogus AP. This messes up scanning. Stop that. + os_printf("STA status = %d. Disconnecting STA...\n", x); + wifi_station_disconnect(); + } + wifi_station_scan(NULL, wifiScanDoneCb); +} + + +int ICACHE_FLASH_ATTR cgiWiFiScan(HttpdConnData *connData) { int len; - char val1[128]; - char val2[128]; + int i; char buff[1024]; + httpdStartResponse(connData, 200); + httpdHeader(connData, "Content-Type", "text/json"); + httpdEndHeaders(connData); + + if (cgiWifiAps.scanInProgress==1) { + len=os_sprintf(buff, "{\n \"result\": { \n\"inProgress\": \"1\"\n }\n}\n"); + espconn_sent(connData->conn, (uint8 *)buff, len); + } else { + len=os_sprintf(buff, "{\n \"result\": { \n\"inProgress\": \"0\", \n\"APs\": [\n"); + espconn_sent(connData->conn, (uint8 *)buff, len); + if (cgiWifiAps.apData==NULL) cgiWifiAps.noAps=0; + for (i=0; issid, cgiWifiAps.apData[i]->rssi, + cgiWifiAps.apData[i]->enc, (i==cgiWifiAps.noAps-1)?"":","); + espconn_sent(connData->conn, (uint8 *)buff, len); + } + len=os_sprintf(buff, "]\n}\n}\n"); + espconn_sent(connData->conn, (uint8 *)buff, len); + wifiStartScan(); + } + return HTTPD_CGI_DONE; +} + +static struct station_config stconf; + + +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. + wifi_set_opmode(1); + system_restart(); + } else { + os_printf("Connect fail. Not going into STA-only mode.\n"); + } +} + + +static void ICACHE_FLASH_ATTR reassTimerCb(void *arg) { + int x; + static ETSTimer resetTimer; + wifi_station_disconnect(); + wifi_station_set_config(&stconf); + wifi_station_connect(); + x=wifi_get_opmode(); + if (x!=1) { + //Schedule disconnect/connect + os_timer_disarm(&resetTimer); + os_timer_setfn(&resetTimer, resetTimerCb, NULL); + os_timer_arm(&resetTimer, 4000, 0); + } +} + +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; } + + httpdFindArg(connData->postBuff, "essid", essid, sizeof(essid)); + httpdFindArg(connData->postBuff, "passwd", passwd, sizeof(passwd)); - httpdStartResponse(connData, 200); - httpdHeader(connData, "Content-Type", "text/plain"); - httpdEndHeaders(connData); + os_strncpy((char*)stconf.ssid, essid, 32); + os_strncpy((char*)stconf.password, passwd, 64); - - httpdFindArg(connData->postBuff, "Test1", val1, sizeof(val1)); - httpdFindArg(connData->postBuff, "Test2", val2, sizeof(val2)); - len=os_sprintf(buff, "Field 1: %s\nField 2: %s\n", val1, val2); - espconn_sent(connData->conn, (uint8 *)buff, len); + //Schedule disconnect/connect + os_timer_disarm(&reassTimer); + os_timer_setfn(&reassTimer, reassTimerCb, NULL); + os_timer_arm(&reassTimer, 1000, 0); + + httpdRedirect(connData, "connecting.html"); return HTTPD_CGI_DONE; } +//Cgi that reads the SPI flash. Assumes 512KByte flash. +int ICACHE_FLASH_ATTR cgiReadFlash(HttpdConnData *connData) { + int *pos=(int *)&connData->cgiData; + if (connData->conn==NULL) { + //Connection aborted. Clean up. + return HTTPD_CGI_DONE; + } + + if (*pos==0) { + os_printf("Start flash download.\n"); + httpdStartResponse(connData, 200); + httpdHeader(connData, "Content-Type", "application/bin"); + httpdEndHeaders(connData); + *pos=0x40200000; + return HTTPD_CGI_MORE; + } + espconn_sent(connData->conn, (uint8 *)(*pos), 1024); + *pos+=1024; + if (*pos>=0x40200000+(512*1024)) return HTTPD_CGI_DONE; else return HTTPD_CGI_MORE; +} + +int ICACHE_FLASH_ATTR cgiTest(HttpdConnData *connData) { + return HTTPD_CGI_DONE; +} + + +void ICACHE_FLASH_ATTR tplWlan(HttpdConnData *connData, char *token, void **arg) { + char buff[1024]; + int x; + static struct station_config stconf; + if (token==NULL) return; + 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); + } + espconn_sent(connData->conn, (uint8 *)buff, os_strlen(buff)); +} + + diff --git a/user/cgi.h b/user/cgi.h index 5fd6826..0edb3a8 100644 --- a/user/cgi.h +++ b/user/cgi.h @@ -4,6 +4,11 @@ #include "httpd.h" int cgiLed(HttpdConnData *connData); +int cgiReadFlash(HttpdConnData *connData); int cgiTest(HttpdConnData *connData); +int cgiWiFiScan(HttpdConnData *connData); +void ICACHE_FLASH_ATTR tplWlan(HttpdConnData *connData, char *token, void **arg); +int ICACHE_FLASH_ATTR cgiWiFi(HttpdConnData *connData); +int ICACHE_FLASH_ATTR cgiWiFiConnect(HttpdConnData *connData); #endif \ No newline at end of file diff --git a/user/espfs.c b/user/espfs.c index 45f5189..8def7d6 100644 --- a/user/espfs.c +++ b/user/espfs.c @@ -12,6 +12,7 @@ It's written for use with httpd, but doesn't need to be used as such. #include "espconn.h" #include "mem.h" #include "osapi.h" +#include "espmissingincludes.h" #else //Test build #include @@ -109,7 +110,8 @@ EspFsFile ICACHE_FLASH_ATTR *espFsOpen(char *fileName) { } p+=sizeof(EspFsHeader); os_memcpy(namebuf, p, sizeof(namebuf)); - os_printf("Found file '%s'. Namelen=%x fileLenComp=%x, compr=%d flags=%d\n", namebuf, h.nameLen,h.fileLenComp,h.compression,h.flags); + os_printf("Found file '%s'. Namelen=%x fileLenComp=%x, compr=%d flags=%d\n", + namebuf, (unsigned int)h.nameLen, (unsigned int)h.fileLenComp, h.compression, h.flags); if (os_strcmp(namebuf, fileName)==0) { p+=h.nameLen; r=(EspFsFile *)os_malloc(sizeof(EspFsFile)); @@ -155,7 +157,7 @@ int ICACHE_FLASH_ATTR espFsRead(EspFsFile *fh, char *buff, int len) { int toRead; toRead=flen-(fh->posComp-fh->posStart); if (len>toRead) len=toRead; - os_printf("Reading %d bytes from %x\n", len, fh->posComp); + os_printf("Reading %d bytes from %x\n", len, (unsigned int)fh->posComp); memcpyAligned(buff, fh->posComp, len); fh->posDecomp+=len; fh->posComp+=len; @@ -164,23 +166,24 @@ int ICACHE_FLASH_ATTR espFsRead(EspFsFile *fh, char *buff, int len) { #ifdef EFS_HEATSHRINK } else if (fh->decompressor==COMPRESS_HEATSHRINK) { int decoded=0; - int elen, rlen, r; + unsigned int elen, rlen; char ebuff[16]; heatshrink_decoder *dec=(heatshrink_decoder *)fh->decompData; while(decodedposComp - fh->posStart); if (elen==0) return decoded; //file is read if (elen>0) { memcpyAligned(ebuff, fh->posComp, 16); - r=heatshrink_decoder_sink(dec, ebuff, (elen>16)?16:elen, &rlen); + heatshrink_decoder_sink(dec, (uint8_t *)ebuff, (elen>16)?16:elen, &rlen); fh->posComp+=rlen; if (rlen==elen) { heatshrink_decoder_finish(dec); } } //Grab decompressed data and put into buff - r=heatshrink_decoder_poll(dec, buff, len-decoded, &rlen); + heatshrink_decoder_poll(dec, (uint8_t *)buff, len-decoded, &rlen); fh->posDecomp+=rlen; buff+=rlen; decoded+=rlen; diff --git a/user/heatshrink_decoder.c b/user/heatshrink_decoder.c index 0e6a239..763a318 100644 --- a/user/heatshrink_decoder.c +++ b/user/heatshrink_decoder.c @@ -8,6 +8,7 @@ #define _STDDEF_H #define _STDINT_H +#include "espmissingincludes.h" #include "c_types.h" #include "mem.h" #include "osapi.h" diff --git a/user/httpd.c b/user/httpd.c index 69198b5..ee2df7e 100644 --- a/user/httpd.c +++ b/user/httpd.c @@ -1,3 +1,4 @@ +#include "espmissingincludes.h" #include "driver/uart.h" #include "c_types.h" #include "user_interface.h" @@ -43,6 +44,7 @@ typedef struct { static const MimeMap mimeTypes[]={ {"htm", "text/htm"}, {"html", "text/html"}, + {"js", "text/javascript"}, {"txt", "text/plain"}, {"jpg", "image/jpeg"}, {"jpeg", "image/jpeg"}, @@ -83,6 +85,7 @@ static int httpdHexVal(char c) { if (c>='0' && c<='9') return c-'0'; if (c>='A' && c<='F') return c-'A'+10; if (c>='a' && c<='f') return c-'a'+10; + return 0; } //Decode a percent-encoded value @@ -107,13 +110,13 @@ int httpdUrlDecode(char *val, int valLen, char *ret, int retLen) { s++; } if (dconn, buff, l); + espconn_sent(conn->conn, (uint8 *)buff, l); } @@ -146,11 +149,11 @@ void ICACHE_FLASH_ATTR httpdHeader(HttpdConnData *conn, const char *field, const char buff[256]; int l; l=os_sprintf(buff, "%s: %s\r\n", field, val); - espconn_sent(conn->conn, buff, l); + espconn_sent(conn->conn, (uint8 *)buff, l); } void ICACHE_FLASH_ATTR httpdEndHeaders(HttpdConnData *conn) { - espconn_sent(conn->conn, "\r\n", 2); + espconn_sent(conn->conn, (uint8 *)"\r\n", 2); } //ToDo: sprintf->snprintf everywhere @@ -158,13 +161,10 @@ void ICACHE_FLASH_ATTR httpdRedirect(HttpdConnData *conn, char *newUrl) { char buff[1024]; int l; l=os_sprintf(buff, "HTTP/1.1 302 Found\r\nLocation: %s\r\n\r\nMoved to %s\r\n", newUrl, newUrl); - espconn_sent(conn->conn, buff, l); + espconn_sent(conn->conn, (uint8 *)buff, l); } int ICACHE_FLASH_ATTR cgiRedirect(HttpdConnData *connData) { - int len; - char buff[1024]; - if (connData->conn==NULL) { //Connection aborted. Clean up. return HTTPD_CGI_DONE; @@ -198,7 +198,7 @@ static void ICACHE_FLASH_ATTR httpdSendResp(HttpdConnData *conn) { int r; //See if the url is somewhere in our internal url table. while (builtInUrls[i].url!=NULL && conn->url!=NULL) { - os_printf("%s == %s?\n", builtInUrls[i].url, conn->url); +// os_printf("%s == %s?\n", builtInUrls[i].url, conn->url); if (os_strcmp(builtInUrls[i].url, conn->url)==0 || builtInUrls[i].url[0]=='*') { os_printf("Is url index %d\n", i); conn->cgiData=NULL; @@ -237,7 +237,6 @@ static void ICACHE_FLASH_ATTR httpdParseHeader(char *h, HttpdConnData *conn) { os_printf("URL = %s\n", conn->url); conn->getArgs=(char*)os_strstr(conn->url, "?"); if (conn->getArgs!=0) { - int x,l; *conn->getArgs=0; conn->getArgs++; os_printf("GET args = %s\n", conn->getArgs); @@ -256,7 +255,7 @@ static void ICACHE_FLASH_ATTR httpdParseHeader(char *h, HttpdConnData *conn) { } static void ICACHE_FLASH_ATTR httpdRecvCb(void *arg, char *data, unsigned short len) { - int x, l; + int x; char *p, *e; HttpdConnData *conn=httpdFindConnData(arg); if (conn==NULL) return; @@ -336,7 +335,6 @@ static void ICACHE_FLASH_ATTR httpdDisconCb(void *arg) { static void ICACHE_FLASH_ATTR httpdConnectCb(void *arg) { struct espconn *conn=arg; - HttpdConnData *conndata; int i; //Find empty conndata in pool for (i=0; i #include #include "c_types.h" @@ -58,14 +59,13 @@ typedef struct { int tokenPos; } TplData; -typedef int (* TplCallback)(HttpdConnData *connData, char *token, void **arg); +typedef void (* TplCallback)(HttpdConnData *connData, char *token, void **arg); int ICACHE_FLASH_ATTR cgiEspFsTemplate(HttpdConnData *connData) { TplData *tpd=connData->cgiData; int len; - int x, sp; - char *p, *e; - int sentTo; + int x, sp=0; + char *e=NULL; char buff[1025]; if (connData->conn==NULL) { diff --git a/user/io.c b/user/io.c index e89689f..b7862ea 100644 --- a/user/io.c +++ b/user/io.c @@ -1,3 +1,4 @@ +#include "espmissingincludes.h" #include "c_types.h" #include "user_interface.h" #include "espconn.h" @@ -20,12 +21,13 @@ void ICACHE_FLASH_ATTR ioLed(int ena) { } } -static void ICACHE_FLASH_ATTR resetBtnTimerCb(void) { +static void ICACHE_FLASH_ATTR resetBtnTimerCb(void *arg) { static int resetCnt=0; if (!GPIO_INPUT_GET(BTNGPIO)) { resetCnt++; } else { if (resetCnt>=6) { //3 sec pressed + wifi_station_disconnect(); wifi_set_opmode(0x3); //reset to AP+STA mode os_printf("Reset to AP mode. Restarting system...\n"); system_restart(); diff --git a/user/user_main.c b/user/user_main.c index 4983daa..0bc9cd1 100644 --- a/user/user_main.c +++ b/user/user_main.c @@ -1,3 +1,4 @@ +#include "espmissingincludes.h" #include "ets_sys.h" #include "driver/uart.h" #include "osapi.h" @@ -7,9 +8,16 @@ #include "cgi.h" HttpdBuiltInUrl builtInUrls[]={ -// {"/", cgiLiteral, "Lalala etc"}, + {"/", cgiRedirect, "/test2.html"}, + {"/flash.bin", cgiReadFlash, NULL}, {"/led.cgi", cgiLed, NULL}, - {"/test.cgi", cgiTest, NULL}, + + {"/wifi", cgiRedirect, "/wifi/wifi.tpl"}, + {"/wifi/", cgiRedirect, "/wifi/wifi.tpl"}, + {"/wifi/wifiscan.cgi", cgiWiFiScan, NULL}, + {"/wifi/wifi.tpl", cgiEspFsTemplate, tplWlan}, + {"/wifi/connect.cgi", cgiWiFiConnect}, + {"*", cgiEspFsHook, NULL}, {NULL, NULL, NULL} };