void rBootHttpUpdate::onTimer() { if (TcpClient::isProcessing()) return; // Will wait if (TcpClient::getConnectionState() == eTCS_Successful) { if (!isSuccessful()) { updateFailed(); return; } currentItem++; if (currentItem >= items.count()) { debugf("\r\nFirmware download finished!"); for (int i = 0; i < items.count(); i++) { debugf(" - item: %d, addr: %X, len: %d bytes", i, items[i].targetOffset, items[i].size); } applyUpdate(); return; } } else if (TcpClient::getConnectionState() == eTCS_Failed) { updateFailed(); return; } rBootHttpUpdateItem &it = items[currentItem]; debugf("Download file:\r\n (%d) %s -> %X", currentItem, it.url.c_str(), it.targetOffset); rBootWriteStatus = rboot_write_init(items[currentItem].targetOffset); startDownload(URL(it.url), eHCM_UserDefined, NULL); }
// start the ota process, with user supplied options bool ICACHE_FLASH_ATTR rboot_ota_start(ota_callback callback) { uint8 slot; rboot_config bootconf; err_t result; // check not already updating if (system_upgrade_flag_check() == UPGRADE_FLAG_START) { return false; } // create upgrade status structure upgrade = (upgrade_status*)os_zalloc(sizeof(upgrade_status)); if (!upgrade) { CHATFABRIC_DEBUG(_GLOBAL_DEBUG, "No ram!\r\n"); return false; } // store the callback upgrade->callback = callback; // get details of rom slot to update bootconf = rboot_get_config(); slot = bootconf.current_rom; if (slot == 0) slot = 1; else slot = 0; upgrade->rom_slot = slot; // flash to rom slot upgrade->write_status = rboot_write_init(bootconf.roms[upgrade->rom_slot]); // to flash a file (e.g. containing a filesystem) to an arbitrary location // (e.g. 0x40000 bytes after the start of the rom) use code this like instead: // Note: address must be start of a sector (multiple of 4k)! //upgrade->write_status = rboot_write_init(bootconf.roms[upgrade->rom_slot] + 0x40000); //upgrade->rom_slot = FLASH_BY_ADDR; // create connection upgrade->conn = (struct espconn *)os_zalloc(sizeof(struct espconn)); if (!upgrade->conn) { CHATFABRIC_DEBUG(_GLOBAL_DEBUG, "No ram!\r\n"); os_free(upgrade); return false; } upgrade->conn->proto.tcp = (esp_tcp *)os_zalloc(sizeof(esp_tcp)); if (!upgrade->conn->proto.tcp) { CHATFABRIC_DEBUG(_GLOBAL_DEBUG, "No ram!\r\n"); os_free(upgrade->conn); os_free(upgrade); return false; } // set update flag system_upgrade_flag_set(UPGRADE_FLAG_START); // dns lookup result = espconn_gethostbyname(upgrade->conn, OTA_HOST, &upgrade->ip, upgrade_resolved); if (result == ESPCONN_OK) { // hostname is already cached or is actually a dotted decimal ip address upgrade_resolved(0, &upgrade->ip, upgrade->conn); } else if (result == ESPCONN_INPROGRESS) { // lookup taking place, will call upgrade_resolved on completion } else { CHATFABRIC_DEBUG(_GLOBAL_DEBUG, "DNS error!\r\n"); os_free(upgrade->conn->proto.tcp); os_free(upgrade->conn); os_free(upgrade); return false; } return true; }