Skip to content

Commit

Permalink
Normalized Wifi to WiFI.
Browse files Browse the repository at this point in the history
Fixed button spacing on console clear and log reset buttons
Renamed cgiReboot method to cgiReset and called the proper reset method
Normalized whitespace in cgiflash.c
  • Loading branch information
brunnels committed Dec 8, 2015
1 parent ebda951 commit 3eccd26
Show file tree
Hide file tree
Showing 10 changed files with 139 additions and 147 deletions.
2 changes: 1 addition & 1 deletion esp-link/cgi.c
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ int ICACHE_FLASH_ATTR cgiMenu(HttpdConnData *connData) {
"{ "
"\"menu\": [ "
"\"Home\", \"/home.html\", "
"\"Wifi\", \"/wifi/wifi.html\", "
"\"WiFI\", \"/wifi/wifi.html\", "
"\"µC Console\", \"/console.html\", "
"\"Services\", \"/services.html\", "
#ifdef MQTT
Expand Down
223 changes: 111 additions & 112 deletions esp-link/cgiflash.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@ Some flash handling cgi routines. Used for reading the existing flash and updati

// Check that the header of the firmware blob looks like actual firmware...
static char* ICACHE_FLASH_ATTR check_header(void *buf) {
uint8_t *cd = (uint8_t *)buf;
uint8_t *cd = (uint8_t *)buf;
#ifdef CGIFLASH_DBG
uint32_t *buf32 = buf;
os_printf("%p: %08lX %08lX %08lX %08lX\n", buf, buf32[0], buf32[1], buf32[2], buf32[3]);
uint32_t *buf32 = buf;
os_printf("%p: %08lX %08lX %08lX %08lX\n", buf, buf32[0], buf32[1], buf32[2], buf32[3]);
#endif
if (cd[0] != 0xEA) return "IROM magic missing";
if (cd[1] != 4 || cd[2] > 3 || (cd[3]>>4) > 6) return "bad flash header";
if (((uint16_t *)buf)[3] != 0x4010) return "Invalid entry addr";
if (((uint32_t *)buf)[2] != 0) return "Invalid start offset";
return NULL;
if (cd[0] != 0xEA) return "IROM magic missing";
if (cd[1] != 4 || cd[2] > 3 || (cd[3]>>4) > 6) return "bad flash header";
if (((uint16_t *)buf)[3] != 0x4010) return "Invalid entry addr";
if (((uint32_t *)buf)[2] != 0) return "Invalid start offset";
return NULL;
}

// check whether the flash map/size we have allows for OTA upgrade
Expand All @@ -49,153 +49,152 @@ static char *flash_too_small = "Flash too small for OTA update";

//===== Cgi to query which firmware needs to be uploaded next
int ICACHE_FLASH_ATTR cgiGetFirmwareNext(HttpdConnData *connData) {
if (connData->conn==NULL) return HTTPD_CGI_DONE; // Connection aborted. Clean up.
if (connData->conn==NULL) return HTTPD_CGI_DONE; // Connection aborted. Clean up.

if (!canOTA()) {
errorResponse(connData, 400, flash_too_small);
return HTTPD_CGI_DONE;
}

uint8 id = system_upgrade_userbin_check();
httpdStartResponse(connData, 200);
httpdHeader(connData, "Content-Type", "text/plain");
httpdHeader(connData, "Content-Length", "9");
httpdEndHeaders(connData);
char *next = id == 1 ? "user1.bin" : "user2.bin";
httpdSend(connData, next, -1);
uint8 id = system_upgrade_userbin_check();
httpdStartResponse(connData, 200);
httpdHeader(connData, "Content-Type", "text/plain");
httpdHeader(connData, "Content-Length", "9");
httpdEndHeaders(connData);
char *next = id == 1 ? "user1.bin" : "user2.bin";
httpdSend(connData, next, -1);
DBG("Next firmware: %s (got %d)\n", next, id);
return HTTPD_CGI_DONE;
return HTTPD_CGI_DONE;
}

//===== Cgi that allows the firmware to be replaced via http POST
int ICACHE_FLASH_ATTR cgiUploadFirmware(HttpdConnData *connData) {
if (connData->conn==NULL) return HTTPD_CGI_DONE; // Connection aborted. Clean up.
if (connData->conn==NULL) return HTTPD_CGI_DONE; // Connection aborted. Clean up.

if (!canOTA()) {
errorResponse(connData, 400, flash_too_small);
return HTTPD_CGI_DONE;
}

int offset = connData->post->received - connData->post->buffLen;
if (offset == 0) {
connData->cgiPrivData = NULL;
} else if (connData->cgiPrivData != NULL) {
// we have an error condition, do nothing
return HTTPD_CGI_DONE;
}

// assume no error yet...
char *err = NULL;
int code = 400;

// check overall size
//os_printf("FW: %d (max %d)\n", connData->post->len, FIRMWARE_SIZE);
if (connData->post->len > FIRMWARE_SIZE) err = "Firmware image too large";
if (connData->post->buff == NULL || connData->requestType != HTTPD_METHOD_POST ||
connData->post->len < 1024) err = "Invalid request";

// check that data starts with an appropriate header
if (err == NULL && offset == 0) err = check_header(connData->post->buff);

// make sure we're buffering in 1024 byte chunks
if (err == NULL && offset % 1024 != 0) {
err = "Buffering problem";
code = 500;
}

// return an error if there is one
if (err != NULL) {
int offset = connData->post->received - connData->post->buffLen;
if (offset == 0) {
connData->cgiPrivData = NULL;
} else if (connData->cgiPrivData != NULL) {
// we have an error condition, do nothing
return HTTPD_CGI_DONE;
}

// assume no error yet...
char *err = NULL;
int code = 400;

// check overall size
//os_printf("FW: %d (max %d)\n", connData->post->len, FIRMWARE_SIZE);
if (connData->post->len > FIRMWARE_SIZE) err = "Firmware image too large";
if (connData->post->buff == NULL || connData->requestType != HTTPD_METHOD_POST ||
connData->post->len < 1024) err = "Invalid request";

// check that data starts with an appropriate header
if (err == NULL && offset == 0) err = check_header(connData->post->buff);

// make sure we're buffering in 1024 byte chunks
if (err == NULL && offset % 1024 != 0) {
err = "Buffering problem";
code = 500;
}

// return an error if there is one
if (err != NULL) {
DBG("Error %d: %s\n", code, err);
httpdStartResponse(connData, code);
httpdHeader(connData, "Content-Type", "text/plain");
//httpdHeader(connData, "Content-Length", strlen(err)+2);
httpdEndHeaders(connData);
httpdSend(connData, err, -1);
httpdSend(connData, "\r\n", -1);
connData->cgiPrivData = (void *)1;
return HTTPD_CGI_DONE;
}

// let's see which partition we need to flash and what flash address that puts us at
uint8 id = system_upgrade_userbin_check();
int address = id == 1 ? 4*1024 // either start after 4KB boot partition
: 4*1024 + FIRMWARE_SIZE + 16*1024 + 4*1024; // 4KB boot, fw1, 16KB user param, 4KB reserved
address += offset;

// erase next flash block if necessary
if (address % SPI_FLASH_SEC_SIZE == 0){
httpdStartResponse(connData, code);
httpdHeader(connData, "Content-Type", "text/plain");
//httpdHeader(connData, "Content-Length", strlen(err)+2);
httpdEndHeaders(connData);
httpdSend(connData, err, -1);
httpdSend(connData, "\r\n", -1);
connData->cgiPrivData = (void *)1;
return HTTPD_CGI_DONE;
}

// let's see which partition we need to flash and what flash address that puts us at
uint8 id = system_upgrade_userbin_check();
int address = id == 1 ? 4*1024 // either start after 4KB boot partition
: 4*1024 + FIRMWARE_SIZE + 16*1024 + 4*1024; // 4KB boot, fw1, 16KB user param, 4KB reserved
address += offset;

// erase next flash block if necessary
if (address % SPI_FLASH_SEC_SIZE == 0){
DBG("Flashing 0x%05x (id=%d)\n", address, 2 - id);
spi_flash_erase_sector(address/SPI_FLASH_SEC_SIZE);
}

// Write the data
//DBG("Writing %d bytes at 0x%05x (%d of %d)\n", connData->post->buffSize, address,
// connData->post->received, connData->post->len);
spi_flash_write(address, (uint32 *)connData->post->buff, connData->post->buffLen);

if (connData->post->received == connData->post->len){
httpdStartResponse(connData, 200);
httpdEndHeaders(connData);
return HTTPD_CGI_DONE;
} else {
return HTTPD_CGI_MORE;
}
spi_flash_erase_sector(address/SPI_FLASH_SEC_SIZE);
}

// Write the data
//DBG("Writing %d bytes at 0x%05x (%d of %d)\n", connData->post->buffSize, address,
// connData->post->received, connData->post->len);
spi_flash_write(address, (uint32 *)connData->post->buff, connData->post->buffLen);

if (connData->post->received == connData->post->len){
httpdStartResponse(connData, 200);
httpdEndHeaders(connData);
return HTTPD_CGI_DONE;
} else {
return HTTPD_CGI_MORE;
}
}

static ETSTimer flash_reboot_timer;

// Handle request to reboot into the new firmware
int ICACHE_FLASH_ATTR cgiRebootFirmware(HttpdConnData *connData) {
if (connData->conn==NULL) return HTTPD_CGI_DONE; // Connection aborted. Clean up.
if (connData->conn==NULL) return HTTPD_CGI_DONE; // Connection aborted. Clean up.

if (!canOTA()) {
errorResponse(connData, 400, flash_too_small);
return HTTPD_CGI_DONE;
}

// sanity-check that the 'next' partition actually contains something that looks like
// valid firmware
uint8 id = system_upgrade_userbin_check();
int address = id == 1 ? 4*1024 // either start after 4KB boot partition
: 4*1024 + FIRMWARE_SIZE + 16*1024 + 4*1024; // 4KB boot, fw1, 16KB user param, 4KB reserved
uint32 buf[8];
// sanity-check that the 'next' partition actually contains something that looks like
// valid firmware
uint8 id = system_upgrade_userbin_check();
int address = id == 1 ? 4*1024 // either start after 4KB boot partition
: 4*1024 + FIRMWARE_SIZE + 16*1024 + 4*1024; // 4KB boot, fw1, 16KB user param, 4KB reserved
uint32 buf[8];
DBG("Checking %p\n", (void *)address);
spi_flash_read(address, buf, sizeof(buf));
char *err = check_header(buf);
if (err != NULL) {
spi_flash_read(address, buf, sizeof(buf));
char *err = check_header(buf);
if (err != NULL) {
DBG("Error %d: %s\n", 400, err);
httpdStartResponse(connData, 400);
httpdHeader(connData, "Content-Type", "text/plain");
//httpdHeader(connData, "Content-Length", strlen(err)+2);
httpdEndHeaders(connData);
httpdSend(connData, err, -1);
httpdSend(connData, "\r\n", -1);
return HTTPD_CGI_DONE;
}

httpdStartResponse(connData, 200);
httpdHeader(connData, "Content-Length", "0");
httpdEndHeaders(connData);

// Schedule a reboot
system_upgrade_flag_set(UPGRADE_FLAG_FINISH);
os_timer_disarm(&flash_reboot_timer);
os_timer_setfn(&flash_reboot_timer, (os_timer_func_t *)system_upgrade_reboot, NULL);
os_timer_arm(&flash_reboot_timer, 2000, 1);
return HTTPD_CGI_DONE;
httpdStartResponse(connData, 400);
httpdHeader(connData, "Content-Type", "text/plain");
//httpdHeader(connData, "Content-Length", strlen(err)+2);
httpdEndHeaders(connData);
httpdSend(connData, err, -1);
httpdSend(connData, "\r\n", -1);
return HTTPD_CGI_DONE;
}

httpdStartResponse(connData, 200);
httpdHeader(connData, "Content-Length", "0");
httpdEndHeaders(connData);

// Schedule a reboot
system_upgrade_flag_set(UPGRADE_FLAG_FINISH);
os_timer_disarm(&flash_reboot_timer);
os_timer_setfn(&flash_reboot_timer, (os_timer_func_t *)system_upgrade_reboot, NULL);
os_timer_arm(&flash_reboot_timer, 2000, 1);
return HTTPD_CGI_DONE;
}

int ICACHE_FLASH_ATTR cgiReboot(HttpdConnData *connData) {
int ICACHE_FLASH_ATTR cgiReset(HttpdConnData *connData) {
if (connData->conn == NULL) return HTTPD_CGI_DONE; // Connection aborted. Clean up.

httpdStartResponse(connData, 200);
httpdHeader(connData, "Content-Length", "0");
httpdEndHeaders(connData);

// Schedule a reboot
system_upgrade_flag_set(UPGRADE_FLAG_FINISH);
os_timer_disarm(&flash_reboot_timer);
os_timer_setfn(&flash_reboot_timer, (os_timer_func_t *)system_upgrade_reboot, NULL);
os_timer_setfn(&flash_reboot_timer, (os_timer_func_t *)system_restart, NULL);
os_timer_arm(&flash_reboot_timer, 2000, 1);
return HTTPD_CGI_DONE;
}
2 changes: 1 addition & 1 deletion esp-link/cgiflash.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ int cgiReadFlash(HttpdConnData *connData);
int cgiGetFirmwareNext(HttpdConnData *connData);
int cgiUploadFirmware(HttpdConnData *connData);
int cgiRebootFirmware(HttpdConnData *connData);
int cgiReboot(HttpdConnData *connData);
int cgiReset(HttpdConnData *connData);

#endif
2 changes: 1 addition & 1 deletion esp-link/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ HttpdBuiltInUrl builtInUrls[] = {
{ "/pgm/upload", cgiOptibootData, NULL },
{ "/log/text", ajaxLog, NULL },
{ "/log/dbg", ajaxLogDbg, NULL },
{ "/log/reboot", cgiReboot, NULL },
{ "/log/reset", cgiReset, NULL },
{ "/console/reset", ajaxConsoleReset, NULL },
{ "/console/baud", ajaxConsoleBaud, NULL },
{ "/console/text", ajaxConsole, NULL },
Expand Down
6 changes: 3 additions & 3 deletions html/console.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ <h1>Microcontroller Console</h1>
<div class="content flex-fill flex-vbox">
<p>
<a id="reset-button" class="pure-button button-primary" href="#">Reset &#xb5;C</a>
<a id="clear-button" class="pure-button button-primary" href="#">Clear Log</a>
&nbsp;&nbsp;Baud:
&nbsp; <a id="clear-button" class="pure-button button-primary" href="#">Clear Log</a>
&nbsp; Baud:
<select id="baud-sel" class="pure-button" href="#">
<option value="460800">460800</option>
<option value="250000">250000</option>
Expand All @@ -18,7 +18,7 @@ <h1>Microcontroller Console</h1>
<option value="19200">19200</option>
<option value="9600">9600</option>
</select>
&nbsp;&nbsp;Fmt: 8N1
&nbsp; Fmt: 8N1
</p>
<div class="pure-g">
<div class="pure-u-1-4"><legend><b>Console</b></legend></div>
Expand Down
10 changes: 5 additions & 5 deletions html/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ <h1>System overview</h1>
</div>
</td></tr>
<tr><td>Network SSID</td><td id="wifi-ssid"></td></tr>
<tr><td>Wifi status</td><td id="wifi-status"></td></tr>
<tr><td>Wifi address</td><td id="wifi-ip"></td></tr>
<tr><td>WiFI status</td><td id="wifi-status"></td></tr>
<tr><td>WiFI address</td><td id="wifi-ip"></td></tr>
<tr><td>SLIP status</td><td class="system-slip"></td></tr>
<tr><td>MQTT status</td><td class="system-mqtt"></td></tr>
<tr><td>Serial baud</td><td class="system-baud"></td></tr>
Expand All @@ -32,7 +32,7 @@ <h1>System overview</h1>
<div class="card">
<h1>Info</h1>
<p style="margin-bottom:0;">The JeeLabs esp-link firmware bridges the ESP8266
serial port to Wifi and can
serial port to WiFI and can
program microcontrollers over the serial port, in particular Arduinos, AVRs, and
NXP's LPC800 and other ARM processors. Typical avrdude command line to
program an Arduino:</p>
Expand Down Expand Up @@ -109,8 +109,8 @@ <h1>Pin assignment</h1>
<h1>System details</h1>
<div id="system-spinner" class="spinner spinner-small"></div>
<table id="system-table" class="pure-table pure-table-horizontal" hidden><tbody>
<tr><td>WiFi mode</td><td id="wifi-mode"></td></tr>
<tr><td>Wifi channel</td><td id="wifi-chan"></td></tr>
<tr><td>WiFI mode</td><td id="wifi-mode"></td></tr>
<tr><td>WiFI channel</td><td id="wifi-chan"></td></tr>
<tr><td>Flash chip ID</td><td>
<div>
<span class="system-id"></span>
Expand Down
Loading

0 comments on commit 3eccd26

Please sign in to comment.