From fbfe0a39e920c06325b024bf4574800edc6a6bf3 Mon Sep 17 00:00:00 2001 From: David Betz Date: Thu, 17 Mar 2016 09:19:19 -0400 Subject: [PATCH 1/2] Add changes we want to upstream. --- Makefile | 6 +++--- cmd/cmd.c | 18 ++++++++++++++++++ cmd/cmd.h | 2 ++ espfs/espfs.c | 11 +++++++++++ espfs/espfs.h | 3 ++- httpd/httpd.c | 32 +++++++++++++++++++------------- httpd/httpd.h | 2 ++ 7 files changed, 57 insertions(+), 17 deletions(-) diff --git a/Makefile b/Makefile index 4e5fa72..40da424 100644 --- a/Makefile +++ b/Makefile @@ -19,8 +19,8 @@ # The Wifi station configuration can be hard-coded here, which makes esp-link come up in STA+AP # mode trying to connect to the specified AP *only* if the flash wireless settings are empty! # This happens on a full serial flash and avoids having to hunt for the AP... -# STA_SSID ?= -# STA_PASS ?= +STA_SSID ?= $(ESP_LINK_SSID) +STA_PASS ?= $(ESP_LINK_PASSWD) # The SOFTAP configuration can be hard-coded here, the minimum parameters to set are AP_SSID && AP_PASS # The AP SSID has to be at least 8 characters long, same for AP PASSWORD @@ -56,7 +56,7 @@ XTENSA_TOOLS_ROOT ?= $(abspath ../esp-open-sdk/xtensa-lx106-elf/bin)/ # Base directory of the ESP8266 SDK package, absolute # Typically you'll download from Espressif's BBS, http://bbs.espressif.com/viewforum.php?f=5 -SDK_BASE ?= $(abspath ../esp_iot_sdk_v1.5.1) +SDK_BASE ?= $(abspath ../esp_iot_sdk_v1.5.2) # Esptool.py path and port, only used for 1-time serial flashing # Typically you'll use https://github.com/themadinventor/esptool diff --git a/cmd/cmd.c b/cmd/cmd.c index 6ebb8cb..35801b7 100644 --- a/cmd/cmd.c +++ b/cmd/cmd.c @@ -171,6 +171,24 @@ cmdPopArg(CmdRequest *req, void *data, uint16_t len) { return 0; } +// Copy the next argument from a command structure into the data pointer, returns 0 on success +// -1 on error +int32_t ICACHE_FLASH_ATTR +cmdPopArgPtr(CmdRequest *req, void **pPtr, uint16_t *pLen) { + + if (req->arg_num >= req->cmd->argc) + return -1; + + *pLen = *(uint16_t*)req->arg_ptr; + + req->arg_ptr += 2; + *pPtr = req->arg_ptr; + req->arg_ptr += (*pLen+3)&~3; // round up to multiple of 4 + + req->arg_num ++; + return 0; +} + // Skip the next argument void ICACHE_FLASH_ATTR cmdSkipArg(CmdRequest *req) { diff --git a/cmd/cmd.h b/cmd/cmd.h index 6b4b018..d680b26 100644 --- a/cmd/cmd.h +++ b/cmd/cmd.h @@ -95,6 +95,8 @@ uint32_t cmdGetArgc(CmdRequest *req); uint16_t cmdArgLen(CmdRequest *req); // Copy next arg from request into the data pointer, returns 0 on success, -1 on error int32_t cmdPopArg(CmdRequest *req, void *data, uint16_t len); +// Copy a pointer to the next arg from request into *pPtr and the length into *pLen, returns 0 on success, -1 on error +int32_t cmdPopArgPtr(CmdRequest *req, void **pPtr, uint16_t *pLen); // Skip next arg void cmdSkipArg(CmdRequest *req); diff --git a/espfs/espfs.c b/espfs/espfs.c index f9942f3..48e425a 100644 --- a/espfs/espfs.c +++ b/espfs/espfs.c @@ -182,6 +182,17 @@ EspFsFile ICACHE_FLASH_ATTR *espFsOpen(char *fileName) { } } +//Get file size. +int ICACHE_FLASH_ATTR espFsSize(EspFsFile *fh) { + int32_t len; + if (fh==NULL) return 0; + if (fh->decompressor==COMPRESS_NONE) + memcpyAligned((char*)&len, (char*)&fh->header->fileLenComp, sizeof(int32_t)); + else + memcpyAligned((char*)&len, (char*)&fh->header->fileLenDecomp, sizeof(int32_t)); + return len; +} + //Read len bytes from the given file into buff. Returns the actual amount of bytes read. int ICACHE_FLASH_ATTR espFsRead(EspFsFile *fh, char *buff, int len) { int flen, fdlen; diff --git a/espfs/espfs.h b/espfs/espfs.h index c8e13e7..2f45d47 100644 --- a/espfs/espfs.h +++ b/espfs/espfs.h @@ -12,8 +12,9 @@ typedef struct EspFsFile EspFsFile; EspFsInitResult espFsInit(void *flashAddress); EspFsFile *espFsOpen(char *fileName); int espFsFlags(EspFsFile *fh); +int espFsSize(EspFsFile *fh); int espFsRead(EspFsFile *fh, char *buff, int len); void espFsClose(EspFsFile *fh); -#endif \ No newline at end of file +#endif diff --git a/httpd/httpd.c b/httpd/httpd.c index 5590809..f9f8a58 100644 --- a/httpd/httpd.c +++ b/httpd/httpd.c @@ -23,7 +23,6 @@ Esp8266 http server - core routines #define DBG(format, ...) do { } while(0) #endif - //Max length of request head #define MAX_HEAD_LEN 1024 //Max amount of connections @@ -44,6 +43,7 @@ struct HttpdPriv { char *sendBuff; // output buffer short headPos; // offset into header short sendBuffLen; // offset into output buffer + short sendBuffMax; // size of output buffer short code; // http response code (only for logging) }; @@ -226,6 +226,14 @@ int ICACHE_FLASH_ATTR httpdGetHeader(HttpdConnData *conn, char *header, char *re return 0; } +//Setup an output buffer +void ICACHE_FLASH_ATTR httpdSetOutputBuffer(HttpdConnData *conn, char *buff, short max) +{ + conn->priv->sendBuff = buff; + conn->priv->sendBuffLen = 0; + conn->priv->sendBuffMax = max; +} + //Start the response headers. void ICACHE_FLASH_ATTR httpdStartResponse(HttpdConnData *conn, int code) { char buff[128]; @@ -277,9 +285,9 @@ int ICACHE_FLASH_ATTR cgiRedirect(HttpdConnData *connData) { //Returns 1 for success, 0 for out-of-memory. int ICACHE_FLASH_ATTR httpdSend(HttpdConnData *conn, const char *data, int len) { if (len<0) len = strlen(data); - if (conn->priv->sendBuffLen + len>MAX_SENDBUFF_LEN) { + if (conn->priv->sendBuffLen + len>conn->priv->sendBuffMax) { DBG("%sERROR! httpdSend full (%d of %d)\n", - connStr, conn->priv->sendBuffLen, MAX_SENDBUFF_LEN); + connStr, conn->priv->sendBuffLen, conn->priv->sendBuffMax); return 0; } os_memcpy(conn->priv->sendBuff + conn->priv->sendBuffLen, data, len); @@ -288,7 +296,7 @@ int ICACHE_FLASH_ATTR httpdSend(HttpdConnData *conn, const char *data, int len) } //Helper function to send any data in conn->priv->sendBuff -static void ICACHE_FLASH_ATTR xmitSendBuff(HttpdConnData *conn) { +void ICACHE_FLASH_ATTR httpdFlush(HttpdConnData *conn) { if (conn->priv->sendBuffLen != 0) { sint8 status = espconn_sent(conn->conn, (uint8_t*)conn->priv->sendBuff, conn->priv->sendBuffLen); if (status != 0) { @@ -307,13 +315,12 @@ static void ICACHE_FLASH_ATTR httpdSentCb(void *arg) { if (conn == NULL) return; // aborted connection char sendBuff[MAX_SENDBUFF_LEN]; - conn->priv->sendBuff = sendBuff; - conn->priv->sendBuffLen = 0; + httpdSetOutputBuffer(conn, sendBuff, sizeof(sendBuff)); if (conn->cgi == NULL) { //Marked for destruction? //os_printf("Closing 0x%p/0x%p->0x%p\n", arg, conn->conn, conn); espconn_disconnect(conn->conn); // we will get a disconnect callback - return; //No need to call xmitSendBuff. + return; //No need to call httpdFlush. } int r = conn->cgi(conn); //Execute cgi fn. @@ -324,7 +331,7 @@ static void ICACHE_FLASH_ATTR httpdSentCb(void *arg) { DBG("%sERROR! Bad CGI code %d\n", connStr, r); conn->cgi = NULL; //mark for destruction. } - xmitSendBuff(conn); + httpdFlush(conn); } static const char *httpNotFoundHeader = "HTTP/1.0 404 Not Found\r\nConnection: close\r\n" @@ -366,7 +373,7 @@ static void ICACHE_FLASH_ATTR httpdProcessRequest(HttpdConnData *conn) { //generate a built-in 404 to handle this. DBG("%s%s not found. 404!\n", connStr, conn->url); httpdSend(conn, httpNotFoundHeader, -1); - xmitSendBuff(conn); + httpdFlush(conn); conn->cgi = NULL; //mark for destruction. if (conn->post) conn->post->len = 0; // skip any remaining receives return; @@ -378,12 +385,12 @@ static void ICACHE_FLASH_ATTR httpdProcessRequest(HttpdConnData *conn) { r = conn->cgi(conn); if (r == HTTPD_CGI_MORE) { //Yep, it's happy to do so and has more data to send. - xmitSendBuff(conn); + httpdFlush(conn); return; } else if (r == HTTPD_CGI_DONE) { //Yep, it's happy to do so and already is done sending data. - xmitSendBuff(conn); + httpdFlush(conn); conn->cgi = NULL; //mark for destruction. if (conn->post) conn->post->len = 0; // skip any remaining receives return; @@ -485,8 +492,7 @@ static void ICACHE_FLASH_ATTR httpdRecvCb(void *arg, char *data, unsigned short if (conn == NULL) return; // aborted connection char sendBuff[MAX_SENDBUFF_LEN]; - conn->priv->sendBuff = sendBuff; - conn->priv->sendBuffLen = 0; + httpdSetOutputBuffer(conn, sendBuff, sizeof(sendBuff)); //This is slightly evil/dirty: we abuse conn->post->len as a state variable for where in the http communications we are: //<0 (-1): Post len unknown because we're still receiving headers diff --git a/httpd/httpd.h b/httpd/httpd.h index cca6f32..33a0700 100644 --- a/httpd/httpd.h +++ b/httpd/httpd.h @@ -59,10 +59,12 @@ int httpdUrlDecode(char *val, int valLen, char *ret, int retLen); int ICACHE_FLASH_ATTR httpdFindArg(char *line, char *arg, char *buff, int buffLen); void ICACHE_FLASH_ATTR httpdInit(HttpdBuiltInUrl *fixedUrls, int port); const char *httpdGetMimetype(char *url); +void ICACHE_FLASH_ATTR httpdSetOutputBuffer(HttpdConnData *conn, char *buff, short max); void ICACHE_FLASH_ATTR httpdStartResponse(HttpdConnData *conn, int code); void ICACHE_FLASH_ATTR httpdHeader(HttpdConnData *conn, const char *field, const char *val); void ICACHE_FLASH_ATTR httpdEndHeaders(HttpdConnData *conn); int ICACHE_FLASH_ATTR httpdGetHeader(HttpdConnData *conn, char *header, char *ret, int retLen); int ICACHE_FLASH_ATTR httpdSend(HttpdConnData *conn, const char *data, int len); +void ICACHE_FLASH_ATTR httpdFlush(HttpdConnData *conn); #endif From 9aefc9ee6c142cfb6fa0f531e060e14510fcbe7f Mon Sep 17 00:00:00 2001 From: David Betz Date: Thu, 17 Mar 2016 09:38:33 -0400 Subject: [PATCH 2/2] Get rid of unnecessary environment variables ESP_LINK_SSID and ESP_LINK_PASSWD. --- Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 40da424..5cd42d8 100644 --- a/Makefile +++ b/Makefile @@ -19,8 +19,8 @@ # The Wifi station configuration can be hard-coded here, which makes esp-link come up in STA+AP # mode trying to connect to the specified AP *only* if the flash wireless settings are empty! # This happens on a full serial flash and avoids having to hunt for the AP... -STA_SSID ?= $(ESP_LINK_SSID) -STA_PASS ?= $(ESP_LINK_PASSWD) +STA_SSID ?= +STA_PASS ?= # The SOFTAP configuration can be hard-coded here, the minimum parameters to set are AP_SSID && AP_PASS # The AP SSID has to be at least 8 characters long, same for AP PASSWORD