init web server */
| 89 | init web server |
| 90 | */ |
| 91 | void WebInterface::init(void) |
| 92 | { |
| 93 | Serial.printf("WAP start %s %s\n", g.wifi_ssid, g.wifi_password); |
| 94 | IPAddress myIP = WiFi.softAPIP(); |
| 95 | |
| 96 | server.addHandler( &AJAX_Handler ); |
| 97 | server.addHandler( &ROMFS_Handler ); |
| 98 | |
| 99 | /*handling uploading firmware file */ |
| 100 | server.on("/update", HTTP_POST, []() { |
| 101 | if (Update.hasError()) { |
| 102 | server.sendHeader("Connection", "close"); |
| 103 | server.send(500, "text/plain","FAIL"); |
| 104 | Serial.printf("Update Failed: Update function has errors\n"); |
| 105 | delay(5000); |
| 106 | } else { |
| 107 | server.sendHeader("Connection", "close"); |
| 108 | server.send(200, "text/plain","OK"); |
| 109 | Serial.printf("Update Success: \nRebooting...\n"); |
| 110 | delay(1000); |
| 111 | ESP.restart(); |
| 112 | } |
| 113 | }, [this]() { |
| 114 | HTTPUpload& upload = server.upload(); |
| 115 | static const esp_partition_t* partition_new_firmware = esp_ota_get_next_update_partition(NULL); //get OTA partion to which we will write new firmware file; |
| 116 | if (upload.status == UPLOAD_FILE_START) { |
| 117 | Serial.printf("Update: %s\n", upload.filename.c_str()); |
| 118 | lead_len = 0; |
| 119 | |
| 120 | if (!Update.begin(UPDATE_SIZE_UNKNOWN)) { //start with max available size |
| 121 | Update.printError(Serial); |
| 122 | } |
| 123 | } else if (upload.status == UPLOAD_FILE_WRITE) { |
| 124 | /* flashing firmware to ESP*/ |
| 125 | if (lead_len < sizeof(lead_bytes)) { |
| 126 | uint32_t n = sizeof(lead_bytes)-lead_len; |
| 127 | if (n > upload.currentSize) { |
| 128 | n = upload.currentSize; |
| 129 | } |
| 130 | memcpy(&lead_bytes[lead_len], upload.buf, n); |
| 131 | lead_len += n; |
| 132 | } |
| 133 | if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) { |
| 134 | Update.printError(Serial); |
| 135 | } |
| 136 | } else if (upload.status == UPLOAD_FILE_END) { |
| 137 | // write extra bytes to force flush of the buffer before we check signature |
| 138 | uint32_t extra = SPI_FLASH_SEC_SIZE+1; |
| 139 | while (extra--) { |
| 140 | uint8_t ff = 0xff; |
| 141 | Update.write(&ff, 1); |
| 142 | } |
| 143 | if (!CheckFirmware::check_OTA_next(partition_new_firmware, lead_bytes, lead_len)) { |
| 144 | Serial.printf("Update Failed: firmware checks have errors\n"); |
| 145 | server.sendHeader("Connection", "close"); |
| 146 | server.send(500, "text/plain","FAIL"); |
| 147 | delay(5000); |
| 148 | } else if (Update.end(true)) { |