Cleaned up code and moved POST vars to their own struct

pull/30/head
prozac 9 years ago
parent c2a02f28ea
commit 532741ebee
  1. 30
      user/cgi.c

@ -38,7 +38,7 @@ int ICACHE_FLASH_ATTR cgiLed(HttpdConnData *connData) {
return HTTPD_CGI_DONE; return HTTPD_CGI_DONE;
} }
len=httpdFindArg(connData->postBuff, "led", buff, sizeof(buff)); len=httpdFindArg(connData->post->buff, "led", buff, sizeof(buff));
if (len!=0) { if (len!=0) {
currLedState=atoi(buff); currLedState=atoi(buff);
ioLed(currLedState); ioLed(currLedState);
@ -105,8 +105,6 @@ int ICACHE_FLASH_ATTR cgiReadFlash(HttpdConnData *connData) {
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;
} }
uint32_t postCounter = 0;
int ICACHE_FLASH_ATTR cgiUploadEspfs(HttpdConnData *connData) { int ICACHE_FLASH_ATTR cgiUploadEspfs(HttpdConnData *connData) {
if (connData->conn==NULL) { if (connData->conn==NULL) {
//Connection aborted. Clean up. //Connection aborted. Clean up.
@ -114,11 +112,11 @@ int ICACHE_FLASH_ATTR cgiUploadEspfs(HttpdConnData *connData) {
} }
SpiFlashOpResult ret; SpiFlashOpResult ret;
int x; int x;
uint32_t flashOff = ESPFS_POS; int flashOff = ESPFS_POS;
uint32_t flashSize = ESPFS_SIZE; int flashSize = ESPFS_SIZE;
//If this is the first time, erase the flash sector //If this is the first time, erase the flash sector
if (postCounter == 0){ if (connData->post->received == 0){
os_printf("Erasing flash at 0x%x...\n", flashOff); os_printf("Erasing flash at 0x%x...\n", flashOff);
// Which segment are we flashing? // Which segment are we flashing?
for (x=0; x<flashSize; x+=4096){ for (x=0; x<flashSize; x+=4096){
@ -127,26 +125,16 @@ int ICACHE_FLASH_ATTR cgiUploadEspfs(HttpdConnData *connData) {
os_printf("Done erasing.\n"); os_printf("Done erasing.\n");
} }
// Because we get sent 1k chunks until the end, if data is less than 1k, we pad it as it must be the end...right??? // The source should be 4byte aligned, so go ahead an flash whatever we have
if (connData->postBuffSize==1024){ ret=spi_flash_write((flashOff + connData->post->received), (uint32 *)connData->post->buff, connData->post->buffLen);
ret=spi_flash_write((flashOff + postCounter), (uint32 *)connData->postBuff, 1024);
os_printf("Flash return %d\n", ret);
} else {
// Think we can probably use postReceived to check if it's the last chunk and then pad the original postBuff to avoid allocating another 1k of memory
char *postBuff = (char*)os_zalloc(1024);
os_printf("Mallocced buffer of 1024 bytes of last chunk.\n");
os_memcpy(postBuff, connData->postBuff, connData->postBuffSize);
ret=spi_flash_write((flashOff + postCounter), (uint32 *)postBuff, 1024);
os_printf("Flash return %d\n", ret); os_printf("Flash return %d\n", ret);
}
// Count bytes for data // Count bytes for data
postCounter = postCounter + connData->postBuffSize;//connData->postBuff); connData->post->received += connData->post->buffSize;//connData->postBuff);
os_printf("Wrote %d bytes (%dB of %d)\n", connData->postBuffSize, postCounter, connData->postLen);//&connData->postBuff)); os_printf("Wrote %d bytes (%dB of %d)\n", connData->post->buffSize, connData->post->received, connData->post->len);//&connData->postBuff));
if (postCounter == connData->postLen){ if (connData->post->received == connData->post->len){
httpdSend(connData, "Finished uploading", -1); httpdSend(connData, "Finished uploading", -1);
postCounter=0;
return HTTPD_CGI_DONE; return HTTPD_CGI_DONE;
} else { } else {
return HTTPD_CGI_MORE; return HTTPD_CGI_MORE;

Loading…
Cancel
Save