// called when connection receives data (hopefully the rom) static void ICACHE_FLASH_ATTR upgrade_recvcb(void *arg, char *pusrdata, unsigned short length) { char *ptrData, *ptrLen, *ptr; // disarm the timer os_timer_disarm(&ota_timer); // first reply? if (upgrade->content_len == 0) { // valid http response? if ((ptrLen = (char*)os_strstr(pusrdata, "Content-Length: ")) && (ptrData = (char*)os_strstr(ptrLen, "\r\n\r\n")) && (os_strncmp(pusrdata + 9, "200", 3) == 0)) { // end of header/start of data ptrData += 4; // length of data after header in this chunk length -= (ptrData - pusrdata); // running total of download length upgrade->total_len += length; // process current chunk if (!rboot_write_flash(&upgrade->write_status, (uint8*)ptrData, length)) { // write error rboot_ota_deinit(); return; } // work out total download size ptrLen += 16; ptr = (char *)os_strstr(ptrLen, "\r\n"); *ptr = '\0'; // destructive upgrade->content_len = atoi(ptrLen); } else { // fail, not a valid http header/non-200 response/etc. rboot_ota_deinit(); return; } } else { // not the first chunk, process it upgrade->total_len += length; if (!rboot_write_flash(&upgrade->write_status, (uint8*)pusrdata, length)) { // write error rboot_ota_deinit(); return; } } // check if we are finished if (upgrade->total_len == upgrade->content_len) { system_upgrade_flag_set(UPGRADE_FLAG_FINISH); // clean up and call user callback rboot_ota_deinit(); } else if (upgrade->conn->state != ESPCONN_READ) { // fail, but how do we get here? premature end of stream? rboot_ota_deinit(); } else { // timer for next recv os_timer_setfn(&ota_timer, (os_timer_func_t *)rboot_ota_deinit, 0); os_timer_arm(&ota_timer, OTA_NETWORK_TIMEOUT, 0); } }
void rBootHttpUpdate::writeRawData(pbuf* buf, int startPos) { pbuf *cur = buf; while (cur != NULL && cur->len > 0 && !writeError) { uint8* ptr = (uint8*) cur->payload + startPos; int len = cur->len - startPos; writeError = !rboot_write_flash(&rBootWriteStatus, ptr, len); if (writeError) { debugf("Write Error!"); } items[currentItem].size += len; cur = cur->next; startPos = 0; } }