Added comments

v0.9.0
Jeroen Domburg 10 years ago
parent 6010c30fa6
commit a8b0da04c5
  1. 4
      user/cgi.c
  2. 23
      user/cgiwifi.c
  3. 2
      user/espfs.c
  4. 32
      user/httpd.c
  5. 2
      user/httpd.h
  6. 2
      user/httpdespfs.c

@ -1,5 +1,6 @@
/* /*
Some random cgi routines. Some random cgi routines. Used in the LED example and the page that returns the entire
flash as a binary. Also handles the hit counter on the main page.
*/ */
/* /*
@ -94,6 +95,7 @@ int ICACHE_FLASH_ATTR cgiReadFlash(HttpdConnData *connData) {
*pos=0x40200000; *pos=0x40200000;
return HTTPD_CGI_MORE; return HTTPD_CGI_MORE;
} }
//Send 1K of flash per call. We will get called again if we haven't sent 512K yet.
espconn_sent(connData->conn, (uint8 *)(*pos), 1024); espconn_sent(connData->conn, (uint8 *)(*pos), 1024);
*pos+=1024; *pos+=1024;
if (*pos>=0x40200000+(512*1024)) return HTTPD_CGI_DONE; else return HTTPD_CGI_MORE; if (*pos>=0x40200000+(512*1024)) return HTTPD_CGI_DONE; else return HTTPD_CGI_MORE;

@ -71,9 +71,12 @@ void ICACHE_FLASH_ATTR wifiScanDoneCb(void *arg, STATUS status) {
bss_link = (struct bss_info *)arg; bss_link = (struct bss_info *)arg;
while (bss_link != NULL) { while (bss_link != NULL) {
if (n>=cgiWifiAps.noAps) { 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.
os_printf("Huh? I have more than the allocated %d aps!\n", cgiWifiAps.noAps); os_printf("Huh? I have more than the allocated %d aps!\n", cgiWifiAps.noAps);
break; break;
} }
//Save the ap data.
cgiWifiAps.apData[n]=(ApData *)os_malloc(sizeof(ApData)); cgiWifiAps.apData[n]=(ApData *)os_malloc(sizeof(ApData));
cgiWifiAps.apData[n]->rssi=bss_link->rssi; cgiWifiAps.apData[n]->rssi=bss_link->rssi;
cgiWifiAps.apData[n]->enc=bss_link->authmode; cgiWifiAps.apData[n]->enc=bss_link->authmode;
@ -92,14 +95,15 @@ static void ICACHE_FLASH_ATTR wifiStartScan() {
// int x; // int x;
if (cgiWifiAps.scanInProgress) return; if (cgiWifiAps.scanInProgress) return;
cgiWifiAps.scanInProgress=1; cgiWifiAps.scanInProgress=1;
/* #if 0
//Not sure if this is still needed.
x=wifi_station_get_connect_status(); x=wifi_station_get_connect_status();
if (x!=STATION_GOT_IP) { if (x!=STATION_GOT_IP) {
//Unit probably is trying to connect to a bogus AP. This messes up scanning. Stop that. //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); os_printf("STA status = %d. Disconnecting STA...\n", x);
wifi_station_disconnect(); wifi_station_disconnect();
} }
*/ #endif
wifi_station_scan(NULL, wifiScanDoneCb); wifi_station_scan(NULL, wifiScanDoneCb);
} }
@ -115,13 +119,16 @@ int ICACHE_FLASH_ATTR cgiWiFiScan(HttpdConnData *connData) {
httpdEndHeaders(connData); httpdEndHeaders(connData);
if (cgiWifiAps.scanInProgress==1) { if (cgiWifiAps.scanInProgress==1) {
//We're still scanning. Tell Javascript code that.
len=os_sprintf(buff, "{\n \"result\": { \n\"inProgress\": \"1\"\n }\n}\n"); len=os_sprintf(buff, "{\n \"result\": { \n\"inProgress\": \"1\"\n }\n}\n");
espconn_sent(connData->conn, (uint8 *)buff, len); espconn_sent(connData->conn, (uint8 *)buff, len);
} else { } else {
//We have a scan result. Pass it on.
len=os_sprintf(buff, "{\n \"result\": { \n\"inProgress\": \"0\", \n\"APs\": [\n"); len=os_sprintf(buff, "{\n \"result\": { \n\"inProgress\": \"0\", \n\"APs\": [\n");
espconn_sent(connData->conn, (uint8 *)buff, len); espconn_sent(connData->conn, (uint8 *)buff, len);
if (cgiWifiAps.apData==NULL) cgiWifiAps.noAps=0; if (cgiWifiAps.apData==NULL) cgiWifiAps.noAps=0;
for (i=0; i<cgiWifiAps.noAps; i++) { for (i=0; i<cgiWifiAps.noAps; i++) {
//Fill in json code for an access point
len=os_sprintf(buff, "{\"essid\": \"%s\", \"rssi\": \"%d\", \"enc\": \"%d\"}%s\n", len=os_sprintf(buff, "{\"essid\": \"%s\", \"rssi\": \"%d\", \"enc\": \"%d\"}%s\n",
cgiWifiAps.apData[i]->ssid, cgiWifiAps.apData[i]->rssi, cgiWifiAps.apData[i]->ssid, cgiWifiAps.apData[i]->rssi,
cgiWifiAps.apData[i]->enc, (i==cgiWifiAps.noAps-1)?"":","); cgiWifiAps.apData[i]->enc, (i==cgiWifiAps.noAps-1)?"":",");
@ -129,6 +136,7 @@ int ICACHE_FLASH_ATTR cgiWiFiScan(HttpdConnData *connData) {
} }
len=os_sprintf(buff, "]\n}\n}\n"); len=os_sprintf(buff, "]\n}\n}\n");
espconn_sent(connData->conn, (uint8 *)buff, len); espconn_sent(connData->conn, (uint8 *)buff, len);
//Also start a new scan.
wifiStartScan(); wifiStartScan();
} }
return HTTPD_CGI_DONE; return HTTPD_CGI_DONE;
@ -137,6 +145,14 @@ int ICACHE_FLASH_ATTR cgiWiFiScan(HttpdConnData *connData) {
//Temp store for new ap info. //Temp store for new ap info.
static struct station_config stconf; static struct station_config stconf;
/*
ToDo:
- Thoroughly test this code
- Simplify if possible. The cascaded delayed routines are probably not
needed anymore. I hope.
*/
//This routine is ran some time after a connection attempt to an access point. If //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. //the connect succeeds, this gets the module in STA-only mode.
static void ICACHE_FLASH_ATTR resetTimerCb(void *arg) { static void ICACHE_FLASH_ATTR resetTimerCb(void *arg) {
@ -148,6 +164,7 @@ static void ICACHE_FLASH_ATTR resetTimerCb(void *arg) {
system_restart(); system_restart();
} else { } else {
os_printf("Connect fail. Not going into STA-only mode.\n"); os_printf("Connect fail. Not going into STA-only mode.\n");
//Maybe also pass this through on the webpage?
} }
} }
@ -193,7 +210,7 @@ int ICACHE_FLASH_ATTR cgiWiFiConnect(HttpdConnData *connData) {
//Schedule disconnect/connect //Schedule disconnect/connect
os_timer_disarm(&reassTimer); os_timer_disarm(&reassTimer);
os_timer_setfn(&reassTimer, reassTimerCb, NULL); os_timer_setfn(&reassTimer, reassTimerCb, NULL);
#if 1 #if 1 //Set to 0 if you want to disable the actual reconnecting bit
os_timer_arm(&reassTimer, 1000, 0); os_timer_arm(&reassTimer, 1000, 0);
httpdRedirect(connData, "connecting.html"); httpdRedirect(connData, "connecting.html");

@ -81,6 +81,8 @@ a memory exception, crashing the program.
//Copies len bytes over from dst to src, but does it using *only* //Copies len bytes over from dst to src, but does it using *only*
//aligned 32-bit reads. Yes, it's no too optimized but it's short and sweet and it works. //aligned 32-bit reads. Yes, it's no too optimized but it's short and sweet and it works.
//ToDo: perhaps os_memcpy also does unaligned accesses?
void ICACHE_FLASH_ATTR memcpyAligned(char *dst, char *src, int len) { void ICACHE_FLASH_ATTR memcpyAligned(char *dst, char *src, int len) {
int x; int x;
int w, b; int w, b;

@ -1,4 +1,6 @@
//Esp8266 http server - core routines /*
Esp8266 http server - core routines
*/
/* /*
* ---------------------------------------------------------------------------- * ----------------------------------------------------------------------------
@ -32,7 +34,7 @@
//This gets set at init time. //This gets set at init time.
static HttpdBuiltInUrl *builtInUrls; static HttpdBuiltInUrl *builtInUrls;
//Private data for httpd thing //Private data for http connection
struct HttpdPriv { struct HttpdPriv {
char head[MAX_HEAD_LEN]; char head[MAX_HEAD_LEN];
int headPos; int headPos;
@ -43,10 +45,11 @@ struct HttpdPriv {
static HttpdPriv connPrivData[MAX_CONN]; static HttpdPriv connPrivData[MAX_CONN];
static HttpdConnData connData[MAX_CONN]; static HttpdConnData connData[MAX_CONN];
//Listening connection data
static struct espconn httpdConn; static struct espconn httpdConn;
static esp_tcp httpdTcp; static esp_tcp httpdTcp;
//Struct to keep extension->mime data in
typedef struct { typedef struct {
const char *ext; const char *ext;
const char *mimetype; const char *mimetype;
@ -73,6 +76,7 @@ const char ICACHE_FLASH_ATTR *httpdGetMimetype(char *url) {
while (ext!=url && *ext!='.') ext--; while (ext!=url && *ext!='.') ext--;
if (*ext=='.') ext++; if (*ext=='.') ext++;
//ToDo: os_strcmp is case sensitive; we may want to do case-intensive matching here...
while (mimeTypes[i].ext!=NULL && os_strcmp(ext, mimeTypes[i].ext)!=0) i++; while (mimeTypes[i].ext!=NULL && os_strcmp(ext, mimeTypes[i].ext)!=0) i++;
return mimeTypes[i].mimetype; return mimeTypes[i].mimetype;
} }
@ -134,7 +138,8 @@ int httpdUrlDecode(char *val, int valLen, char *ret, int retLen) {
//Find a specific arg in a string of get- or post-data. //Find a specific arg in a string of get- or post-data.
//Line is the string of post/get-data, arg is the name of the value to find. The //Line is the string of post/get-data, arg is the name of the value to find. The
//zero-terminated result is written in buff, with at most buffLen bytes used. The //zero-terminated result is written in buff, with at most buffLen bytes used. The
//function returns the length of the result, or -1 if the value wasn't found. //function returns the length of the result, or -1 if the value wasn't found. The
//returned string will be urldecoded already.
int ICACHE_FLASH_ATTR httpdFindArg(char *line, char *arg, char *buff, int buffLen) { int ICACHE_FLASH_ATTR httpdFindArg(char *line, char *arg, char *buff, int buffLen) {
char *p, *e; char *p, *e;
if (line==NULL) return 0; if (line==NULL) return 0;
@ -160,7 +165,7 @@ int ICACHE_FLASH_ATTR httpdFindArg(char *line, char *arg, char *buff, int buffLe
void ICACHE_FLASH_ATTR httpdStartResponse(HttpdConnData *conn, int code) { void ICACHE_FLASH_ATTR httpdStartResponse(HttpdConnData *conn, int code) {
char buff[128]; char buff[128];
int l; int l;
l=os_sprintf(buff, "HTTP/1.0 %d OK\r\nServer: esp8266-httpd/0.1\r\n", code); l=os_sprintf(buff, "HTTP/1.0 %d OK\r\nServer: esp8266-httpd/"HTTPDVER"\r\n", code);
espconn_sent(conn->conn, (uint8 *)buff, l); espconn_sent(conn->conn, (uint8 *)buff, l);
} }
@ -177,7 +182,8 @@ void ICACHE_FLASH_ATTR httpdEndHeaders(HttpdConnData *conn) {
espconn_sent(conn->conn, (uint8 *)"\r\n", 2); espconn_sent(conn->conn, (uint8 *)"\r\n", 2);
} }
//ToDo: sprintf->snprintf everywhere //ToDo: sprintf->snprintf everywhere... esp doesn't have snprintf tho' :/
//Redirect to the given URL.
void ICACHE_FLASH_ATTR httpdRedirect(HttpdConnData *conn, char *newUrl) { void ICACHE_FLASH_ATTR httpdRedirect(HttpdConnData *conn, char *newUrl) {
char buff[1024]; char buff[1024];
int l; int l;
@ -185,6 +191,7 @@ void ICACHE_FLASH_ATTR httpdRedirect(HttpdConnData *conn, char *newUrl) {
espconn_sent(conn->conn, (uint8 *)buff, l); espconn_sent(conn->conn, (uint8 *)buff, l);
} }
//Use this as a cgi function to redirect one url to another.
int ICACHE_FLASH_ATTR cgiRedirect(HttpdConnData *connData) { int ICACHE_FLASH_ATTR cgiRedirect(HttpdConnData *connData) {
if (connData->conn==NULL) { if (connData->conn==NULL) {
//Connection aborted. Clean up. //Connection aborted. Clean up.
@ -195,7 +202,8 @@ int ICACHE_FLASH_ATTR cgiRedirect(HttpdConnData *connData) {
return HTTPD_CGI_DONE; return HTTPD_CGI_DONE;
} }
//Callback called when the data on a socket has been successfully
//sent.
static void ICACHE_FLASH_ATTR httpdSentCb(void *arg) { static void ICACHE_FLASH_ATTR httpdSentCb(void *arg) {
int r; int r;
HttpdConnData *conn=httpdFindConnData(arg); HttpdConnData *conn=httpdFindConnData(arg);
@ -216,6 +224,8 @@ static void ICACHE_FLASH_ATTR httpdSentCb(void *arg) {
static const char *httpNotFoundHeader="HTTP/1.0 404 Not Found\r\nServer: esp8266-httpd/0.1\r\nContent-Type: text/plain\r\n\r\nNot Found.\r\n"; static const char *httpNotFoundHeader="HTTP/1.0 404 Not Found\r\nServer: esp8266-httpd/0.1\r\nContent-Type: text/plain\r\n\r\nNot Found.\r\n";
//This is called when the headers have been received and the connection is ready to send
//the result headers and data.
static void ICACHE_FLASH_ATTR httpdSendResp(HttpdConnData *conn) { static void ICACHE_FLASH_ATTR httpdSendResp(HttpdConnData *conn) {
int i=0; int i=0;
int r; int r;
@ -241,6 +251,7 @@ static void ICACHE_FLASH_ATTR httpdSendResp(HttpdConnData *conn) {
conn->cgi=NULL; //mark for destruction conn->cgi=NULL; //mark for destruction
} }
//Parse a line of header data and modify the connection data accordingly.
static void ICACHE_FLASH_ATTR httpdParseHeader(char *h, HttpdConnData *conn) { static void ICACHE_FLASH_ATTR httpdParseHeader(char *h, HttpdConnData *conn) {
int i; int i;
// os_printf("Got header %s\n", h); // os_printf("Got header %s\n", h);
@ -258,6 +269,7 @@ static void ICACHE_FLASH_ATTR httpdParseHeader(char *h, HttpdConnData *conn) {
*e=0; //terminate url part *e=0; //terminate url part
os_printf("URL = %s\n", conn->url); os_printf("URL = %s\n", conn->url);
//Parse out the URL part before the GET parameters.
conn->getArgs=(char*)os_strstr(conn->url, "?"); conn->getArgs=(char*)os_strstr(conn->url, "?");
if (conn->getArgs!=0) { if (conn->getArgs!=0) {
*conn->getArgs=0; *conn->getArgs=0;
@ -268,15 +280,20 @@ static void ICACHE_FLASH_ATTR httpdParseHeader(char *h, HttpdConnData *conn) {
} }
} else if (os_strncmp(h, "Content-Length: ", 16)==0) { } else if (os_strncmp(h, "Content-Length: ", 16)==0) {
i=0; i=0;
//Skip trailing spaces
while (h[i]!=' ') i++; while (h[i]!=' ') i++;
//Get POST data length
conn->postLen=atoi(h+i+1); conn->postLen=atoi(h+i+1);
//Clamp if too big. Hmm, maybe we should error out instead?
if (conn->postLen>MAX_POST) conn->postLen=MAX_POST; if (conn->postLen>MAX_POST) conn->postLen=MAX_POST;
os_printf("Mallocced buffer for %d bytes of post data.\n", conn->postLen); os_printf("Mallocced buffer for %d bytes of post data.\n", conn->postLen);
//Alloc the memory.
conn->postBuff=(char*)os_malloc(conn->postLen+1); conn->postBuff=(char*)os_malloc(conn->postLen+1);
conn->priv->postPos=0; conn->priv->postPos=0;
} }
} }
//Callback called when there's data available on a socket.
static void ICACHE_FLASH_ATTR httpdRecvCb(void *arg, char *data, unsigned short len) { static void ICACHE_FLASH_ATTR httpdRecvCb(void *arg, char *data, unsigned short len) {
int x; int x;
char *p, *e; char *p, *e;
@ -285,7 +302,6 @@ static void ICACHE_FLASH_ATTR httpdRecvCb(void *arg, char *data, unsigned short
for (x=0; x<len; x++) { for (x=0; x<len; x++) {
if (conn->priv->headPos!=-1) { if (conn->priv->headPos!=-1) {
//This byte is a header byte. //This byte is a header byte.
if (conn->priv->headPos!=MAX_HEAD_LEN) conn->priv->head[conn->priv->headPos++]=data[x]; if (conn->priv->headPos!=MAX_HEAD_LEN) conn->priv->head[conn->priv->headPos++]=data[x];

@ -4,6 +4,8 @@
#include "lwip/ip_addr.h" #include "lwip/ip_addr.h"
#include <espconn.h> #include <espconn.h>
#define HTTPDVER "0.2"
#define HTTPD_CGI_MORE 0 #define HTTPD_CGI_MORE 0
#define HTTPD_CGI_DONE 1 #define HTTPD_CGI_DONE 1
#define HTTPD_CGI_NOTFOUND 2 #define HTTPD_CGI_NOTFOUND 2

@ -1,5 +1,5 @@
/* /*
Connector to let httpd use the espfs filesystem to serve the files in that. Connector to let httpd use the espfs filesystem to serve the files in it.
*/ */
/* /*

Loading…
Cancel
Save