server_ota.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  1. #include "server_ota.h"
  2. #include <string>
  3. #include "string.h"
  4. #include <esp_int_wdt.h>
  5. #include <esp_task_wdt.h>
  6. #include <string.h>
  7. #include "freertos/FreeRTOS.h"
  8. #include "freertos/task.h"
  9. #include "esp_system.h"
  10. #include "esp_event.h"
  11. #include "esp_event.h"
  12. #include "esp_log.h"
  13. #include <esp_ota_ops.h>
  14. #include "esp_http_client.h"
  15. #include "esp_flash_partitions.h"
  16. #include "esp_partition.h"
  17. #include <nvs.h>
  18. #include "nvs_flash.h"
  19. #include "driver/gpio.h"
  20. // #include "protocol_examples_common.h"
  21. #include "errno.h"
  22. #include <sys/stat.h>
  23. #include "server_tflite.h"
  24. #include "server_file.h"
  25. #include "server_GPIO.h"
  26. #ifdef ENABLE_MQTT
  27. #include "interface_mqtt.h"
  28. #endif //ENABLE_MQTT
  29. #include "ClassLogFile.h"
  30. #include "Helper.h"
  31. #include "../../include/defines.h"
  32. /*an ota data write buffer ready to write to the flash*/
  33. static char ota_write_data[SERVER_OTA_SCRATCH_BUFSIZE + 1] = { 0 };
  34. static const char *TAG = "OTA";
  35. esp_err_t handler_reboot(httpd_req_t *req);
  36. static bool ota_update_task(std::string fn);
  37. std::string _file_name_update;
  38. bool initial_setup = false;
  39. static void infinite_loop(void)
  40. {
  41. int i = 0;
  42. ESP_LOGI(TAG, "When a new firmware is available on the server, press the reset button to download it");
  43. while(1) {
  44. ESP_LOGI(TAG, "Waiting for a new firmware... %d", ++i);
  45. vTaskDelay(1000 / portTICK_PERIOD_MS);
  46. }
  47. }
  48. void task_do_Update_ZIP(void *pvParameter)
  49. {
  50. std::string filetype = toUpper(getFileType(_file_name_update));
  51. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "File: " + _file_name_update + " Filetype: " + filetype);
  52. if (filetype == "ZIP")
  53. {
  54. std::string in, out, outbin, zw, retfirmware;
  55. out = "/sdcard/html";
  56. outbin = "/sdcard/firmware";
  57. retfirmware = unzip_new(_file_name_update, out+"/", outbin+"/", "/sdcard/", initial_setup);
  58. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Files unzipped.");
  59. if (retfirmware.length() > 0)
  60. {
  61. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Found firmware.bin");
  62. ota_update_task(retfirmware);
  63. }
  64. if (initial_setup)
  65. {
  66. }
  67. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Trigger reboot due to firmware update.");
  68. doReboot();
  69. }
  70. else
  71. {
  72. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Only ZIP-Files support for update during startup!");
  73. }
  74. }
  75. void CheckUpdate()
  76. {
  77. FILE *pfile;
  78. if ((pfile = fopen("/sdcard/update.txt", "r")) == NULL)
  79. {
  80. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "No update triggered.");
  81. return;
  82. }
  83. char zw[1024] = "";
  84. fgets(zw, 1024, pfile);
  85. _file_name_update = std::string(zw);
  86. if (fgets(zw, 1024, pfile))
  87. {
  88. std::string _szw = std::string(zw);
  89. if (_szw == "init")
  90. {
  91. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Inital Setup triggered.");
  92. initial_setup = true; }
  93. }
  94. fclose(pfile);
  95. DeleteFile("/sdcard/update.txt"); // Prevent Boot Loop!!!
  96. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Update during boot triggered - Update File: " + _file_name_update);
  97. BaseType_t xReturned;
  98. int _i = configMINIMAL_STACK_SIZE;
  99. xReturned = xTaskCreate(&task_do_Update_ZIP, "task_do_Update_ZIP", configMINIMAL_STACK_SIZE * 35, NULL, tskIDLE_PRIORITY+1, NULL);
  100. while(1) { // wait until reboot within task_do_Update_ZIP
  101. vTaskDelay(1000 / portTICK_PERIOD_MS);
  102. }
  103. }
  104. static bool ota_update_task(std::string fn)
  105. {
  106. esp_err_t err;
  107. /* update handle : set by esp_ota_begin(), must be freed via esp_ota_end() */
  108. esp_ota_handle_t update_handle = 0 ;
  109. const esp_partition_t *update_partition = NULL;
  110. ESP_LOGI(TAG, "Starting OTA update");
  111. const esp_partition_t *configured = esp_ota_get_boot_partition();
  112. const esp_partition_t *running = esp_ota_get_running_partition();
  113. if (configured != running) {
  114. ESP_LOGW(TAG, "Configured OTA boot partition at offset 0x%08x, but running from offset 0x%08x",
  115. configured->address, running->address);
  116. ESP_LOGW(TAG, "(This can happen if either the OTA boot data or preferred boot image become somehow corrupted.)");
  117. }
  118. ESP_LOGI(TAG, "Running partition type %d subtype %d (offset 0x%08x)",
  119. running->type, running->subtype, running->address);
  120. update_partition = esp_ota_get_next_update_partition(NULL);
  121. ESP_LOGI(TAG, "Writing to partition subtype %d at offset 0x%x",
  122. update_partition->subtype, update_partition->address);
  123. // assert(update_partition != NULL);
  124. int binary_file_length = 0;
  125. // deal with all receive packet
  126. bool image_header_was_checked = false;
  127. int data_read;
  128. FILE* f = fopen(fn.c_str(), "rb"); // previously only "r
  129. if (f == NULL) { // File does not exist
  130. return false;
  131. }
  132. data_read = fread(ota_write_data, 1, SERVER_OTA_SCRATCH_BUFSIZE, f);
  133. while (data_read > 0) {
  134. if (data_read < 0) {
  135. ESP_LOGE(TAG, "Error: SSL data read error");
  136. return false;
  137. } else if (data_read > 0) {
  138. if (image_header_was_checked == false) {
  139. esp_app_desc_t new_app_info;
  140. if (data_read > sizeof(esp_image_header_t) + sizeof(esp_image_segment_header_t) + sizeof(esp_app_desc_t)) {
  141. // check current version with downloading
  142. memcpy(&new_app_info, &ota_write_data[sizeof(esp_image_header_t) + sizeof(esp_image_segment_header_t)], sizeof(esp_app_desc_t));
  143. ESP_LOGI(TAG, "New firmware version: %s", new_app_info.version);
  144. esp_app_desc_t running_app_info;
  145. if (esp_ota_get_partition_description(running, &running_app_info) == ESP_OK) {
  146. ESP_LOGI(TAG, "Running firmware version: %s", running_app_info.version);
  147. }
  148. const esp_partition_t* last_invalid_app = esp_ota_get_last_invalid_partition();
  149. esp_app_desc_t invalid_app_info;
  150. if (esp_ota_get_partition_description(last_invalid_app, &invalid_app_info) == ESP_OK) {
  151. ESP_LOGI(TAG, "Last invalid firmware version: %s", invalid_app_info.version);
  152. }
  153. // check current version with last invalid partition
  154. if (last_invalid_app != NULL) {
  155. if (memcmp(invalid_app_info.version, new_app_info.version, sizeof(new_app_info.version)) == 0) {
  156. ESP_LOGW(TAG, "New version is the same as invalid version.");
  157. ESP_LOGW(TAG, "Previously, there was an attempt to launch the firmware with %s version, but it failed.", invalid_app_info.version);
  158. ESP_LOGW(TAG, "The firmware has been rolled back to the previous version.");
  159. infinite_loop();
  160. }
  161. }
  162. /*
  163. if (memcmp(new_app_info.version, running_app_info.version, sizeof(new_app_info.version)) == 0) {
  164. ESP_LOGW(TAG, "Current running version is the same as a new. We will not continue the update.");
  165. infinite_loop();
  166. }
  167. */
  168. image_header_was_checked = true;
  169. err = esp_ota_begin(update_partition, OTA_SIZE_UNKNOWN, &update_handle);
  170. if (err != ESP_OK) {
  171. ESP_LOGE(TAG, "esp_ota_begin failed (%s)", esp_err_to_name(err));
  172. return false;
  173. }
  174. ESP_LOGI(TAG, "esp_ota_begin succeeded");
  175. } else {
  176. ESP_LOGE(TAG, "received package is not fit len");
  177. return false;
  178. }
  179. }
  180. err = esp_ota_write( update_handle, (const void *)ota_write_data, data_read);
  181. if (err != ESP_OK) {
  182. return false;
  183. }
  184. binary_file_length += data_read;
  185. ESP_LOGD(TAG, "Written image length %d", binary_file_length);
  186. } else if (data_read == 0) {
  187. //
  188. // * As esp_http_client_read never returns negative error code, we rely on
  189. // * `errno` to check for underlying transport connectivity closure if any
  190. //
  191. if (errno == ECONNRESET || errno == ENOTCONN) {
  192. ESP_LOGE(TAG, "Connection closed, errno = %d", errno);
  193. break;
  194. }
  195. }
  196. data_read = fread(ota_write_data, 1, SERVER_OTA_SCRATCH_BUFSIZE, f);
  197. }
  198. fclose(f);
  199. ESP_LOGI(TAG, "Total Write binary data length: %d", binary_file_length);
  200. err = esp_ota_end(update_handle);
  201. if (err != ESP_OK) {
  202. if (err == ESP_ERR_OTA_VALIDATE_FAILED) {
  203. ESP_LOGE(TAG, "Image validation failed, image is corrupted");
  204. }
  205. ESP_LOGE(TAG, "esp_ota_end failed (%s)!", esp_err_to_name(err));
  206. return false;
  207. }
  208. err = esp_ota_set_boot_partition(update_partition);
  209. if (err != ESP_OK) {
  210. ESP_LOGE(TAG, "esp_ota_set_boot_partition failed (%s)!", esp_err_to_name(err));
  211. }
  212. // ESP_LOGI(TAG, "Prepare to restart system!");
  213. // esp_restart();
  214. return true ;
  215. }
  216. static void print_sha256 (const uint8_t *image_hash, const char *label)
  217. {
  218. char hash_print[HASH_LEN * 2 + 1];
  219. hash_print[HASH_LEN * 2] = 0;
  220. for (int i = 0; i < HASH_LEN; ++i) {
  221. sprintf(&hash_print[i * 2], "%02x", image_hash[i]);
  222. }
  223. ESP_LOGI(TAG, "%s: %s", label, hash_print);
  224. }
  225. static bool diagnostic(void)
  226. {
  227. return true;
  228. }
  229. void CheckOTAUpdate(void)
  230. {
  231. ESP_LOGI(TAG, "Start CheckOTAUpdateCheck...");
  232. uint8_t sha_256[HASH_LEN] = { 0 };
  233. esp_partition_t partition;
  234. // get sha256 digest for the partition table
  235. partition.address = ESP_PARTITION_TABLE_OFFSET;
  236. partition.size = ESP_PARTITION_TABLE_MAX_LEN;
  237. partition.type = ESP_PARTITION_TYPE_DATA;
  238. esp_partition_get_sha256(&partition, sha_256);
  239. print_sha256(sha_256, "SHA-256 for the partition table: ");
  240. // get sha256 digest for bootloader
  241. partition.address = ESP_BOOTLOADER_OFFSET;
  242. partition.size = ESP_PARTITION_TABLE_OFFSET;
  243. partition.type = ESP_PARTITION_TYPE_APP;
  244. esp_partition_get_sha256(&partition, sha_256);
  245. print_sha256(sha_256, "SHA-256 for bootloader: ");
  246. // get sha256 digest for running partition
  247. esp_partition_get_sha256(esp_ota_get_running_partition(), sha_256);
  248. print_sha256(sha_256, "SHA-256 for current firmware: ");
  249. const esp_partition_t *running = esp_ota_get_running_partition();
  250. esp_ota_img_states_t ota_state;
  251. esp_err_t res_stat_partition = esp_ota_get_state_partition(running, &ota_state);
  252. switch (res_stat_partition)
  253. {
  254. case ESP_OK:
  255. ESP_LOGD(TAG, "CheckOTAUpdate Partition: ESP_OK");
  256. if (esp_ota_get_state_partition(running, &ota_state) == ESP_OK) {
  257. if (ota_state == ESP_OTA_IMG_PENDING_VERIFY) {
  258. // run diagnostic function ...
  259. bool diagnostic_is_ok = diagnostic();
  260. if (diagnostic_is_ok) {
  261. ESP_LOGI(TAG, "Diagnostics completed successfully! Continuing execution...");
  262. esp_ota_mark_app_valid_cancel_rollback();
  263. } else {
  264. ESP_LOGE(TAG, "Diagnostics failed! Start rollback to the previous version...");
  265. esp_ota_mark_app_invalid_rollback_and_reboot();
  266. }
  267. }
  268. }
  269. break;
  270. case ESP_ERR_INVALID_ARG:
  271. ESP_LOGD(TAG, "CheckOTAUpdate Partition: ESP_ERR_INVALID_ARG");
  272. break;
  273. case ESP_ERR_NOT_SUPPORTED:
  274. ESP_LOGD(TAG, "CheckOTAUpdate Partition: ESP_ERR_NOT_SUPPORTED");
  275. break;
  276. case ESP_ERR_NOT_FOUND:
  277. ESP_LOGD(TAG, "CheckOTAUpdate Partition: ESP_ERR_NOT_FOUND");
  278. break;
  279. }
  280. if (esp_ota_get_state_partition(running, &ota_state) == ESP_OK) {
  281. if (ota_state == ESP_OTA_IMG_PENDING_VERIFY) {
  282. // run diagnostic function ...
  283. bool diagnostic_is_ok = diagnostic();
  284. if (diagnostic_is_ok) {
  285. ESP_LOGI(TAG, "Diagnostics completed successfully! Continuing execution...");
  286. esp_ota_mark_app_valid_cancel_rollback();
  287. } else {
  288. ESP_LOGE(TAG, "Diagnostics failed! Start rollback to the previous version...");
  289. esp_ota_mark_app_invalid_rollback_and_reboot();
  290. }
  291. }
  292. }
  293. }
  294. esp_err_t handler_ota_update(httpd_req_t *req)
  295. {
  296. #ifdef DEBUG_DETAIL_ON
  297. LogFile.WriteHeapInfo("handler_ota_update - Start");
  298. #endif
  299. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "handler_ota_update");
  300. char _query[200];
  301. char _filename[100];
  302. char _valuechar[30];
  303. std::string fn = "/sdcard/firmware/";
  304. bool _file_del = false;
  305. std::string _task = "";
  306. if (httpd_req_get_url_query_str(req, _query, 200) == ESP_OK)
  307. {
  308. ESP_LOGD(TAG, "Query: %s", _query);
  309. if (httpd_query_key_value(_query, "task", _valuechar, 30) == ESP_OK)
  310. {
  311. ESP_LOGD(TAG, "task is found: %s", _valuechar);
  312. _task = std::string(_valuechar);
  313. }
  314. if (httpd_query_key_value(_query, "file", _filename, 100) == ESP_OK)
  315. {
  316. fn.append(_filename);
  317. ESP_LOGD(TAG, "File: %s", fn.c_str());
  318. }
  319. if (httpd_query_key_value(_query, "delete", _filename, 100) == ESP_OK)
  320. {
  321. fn.append(_filename);
  322. _file_del = true;
  323. ESP_LOGD(TAG, "Delete Default File: %s", fn.c_str());
  324. }
  325. };
  326. if (_task.compare("emptyfirmwaredir") == 0)
  327. {
  328. ESP_LOGD(TAG, "Start empty directory /firmware");
  329. delete_all_in_directory("/sdcard/firmware");
  330. std::string zw = "firmware directory deleted - v2\n";
  331. ESP_LOGD(TAG, "%s", zw.c_str());
  332. printf("Ausgabe: %s\n", zw.c_str());
  333. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  334. httpd_resp_send(req, zw.c_str(), strlen(zw.c_str()));
  335. /* Respond with an empty chunk to signal HTTP response completion */
  336. httpd_resp_send_chunk(req, NULL, 0);
  337. ESP_LOGD(TAG, "Done empty directory /firmware");
  338. return ESP_OK;
  339. }
  340. if (_task.compare("update") == 0)
  341. {
  342. std::string filetype = toUpper(getFileType(fn));
  343. if (filetype.length() == 0)
  344. {
  345. std::string zw = "Update failed - no file specified (zip, bin, tfl, tlite)";
  346. httpd_resp_sendstr_chunk(req, zw.c_str());
  347. httpd_resp_sendstr_chunk(req, NULL);
  348. return ESP_OK;
  349. }
  350. if ((filetype == "TFLITE") || (filetype == "TFL"))
  351. {
  352. std::string out = "/sdcard/config/" + getFileFullFileName(fn);
  353. DeleteFile(out);
  354. CopyFile(fn, out);
  355. DeleteFile(fn);
  356. const char* resp_str = "Neural Network File copied.";
  357. httpd_resp_sendstr_chunk(req, resp_str);
  358. httpd_resp_sendstr_chunk(req, NULL);
  359. return ESP_OK;
  360. }
  361. if (filetype == "ZIP")
  362. {
  363. FILE *pfile;
  364. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Update for reboot.");
  365. pfile = fopen("/sdcard/update.txt", "w");
  366. fwrite(fn.c_str(), fn.length(), 1, pfile);
  367. fclose(pfile);
  368. std::string zw = "reboot\n";
  369. httpd_resp_sendstr_chunk(req, zw.c_str());
  370. httpd_resp_sendstr_chunk(req, NULL);
  371. ESP_LOGD(TAG, "Send reboot");
  372. return ESP_OK;
  373. /*
  374. std::string in, out, outbin, zw, retfirmware;
  375. out = "/sdcard/html";
  376. outbin = "/sdcard/firmware";
  377. retfirmware = unzip_new(fn, out+"/", outbin+"/");
  378. if (retfirmware.length() > 0)
  379. {
  380. filetype = "BIN";
  381. fn = retfirmware;
  382. }
  383. else
  384. {
  385. zw = "Web Interface Update Successfull!\nNo reboot necessary.\n";
  386. httpd_resp_sendstr_chunk(req, zw.c_str());
  387. httpd_resp_sendstr_chunk(req, NULL);
  388. return ESP_OK;
  389. }
  390. */
  391. }
  392. if (filetype == "BIN")
  393. {
  394. const char* resp_str;
  395. KillTFliteTasks();
  396. gpio_handler_deinit();
  397. if (ota_update_task(fn))
  398. {
  399. std::string zw = "reboot\n";
  400. httpd_resp_sendstr_chunk(req, zw.c_str());
  401. httpd_resp_sendstr_chunk(req, NULL);
  402. ESP_LOGD(TAG, "Send reboot");
  403. return ESP_OK;
  404. }
  405. resp_str = "Error during Firmware Update!!!\nPlease check output of console.";
  406. httpd_resp_send(req, resp_str, strlen(resp_str));
  407. #ifdef DEBUG_DETAIL_ON
  408. LogFile.WriteHeapInfo("handler_ota_update - Done");
  409. #endif
  410. return ESP_OK;
  411. }
  412. std::string zw = "Update failed - no valid file specified (zip, bin, tfl, tlite)!";
  413. httpd_resp_sendstr_chunk(req, zw.c_str());
  414. httpd_resp_sendstr_chunk(req, NULL);
  415. return ESP_OK;
  416. }
  417. if (_task.compare("unziphtml") == 0)
  418. {
  419. ESP_LOGD(TAG, "Task unziphtml");
  420. std::string in, out, zw;
  421. in = "/sdcard/firmware/html.zip";
  422. out = "/sdcard/html";
  423. delete_all_in_directory(out);
  424. unzip(in, out+"/");
  425. zw = "Web Interface Update Successfull!\nNo reboot necessary";
  426. httpd_resp_send(req, zw.c_str(), strlen(zw.c_str()));
  427. httpd_resp_sendstr_chunk(req, NULL);
  428. return ESP_OK;
  429. }
  430. if (_file_del)
  431. {
  432. ESP_LOGD(TAG, "Delete !! _file_del: %s", fn.c_str());
  433. struct stat file_stat;
  434. int _result = stat(fn.c_str(), &file_stat);
  435. ESP_LOGD(TAG, "Ergebnis %d\n", _result);
  436. if (_result == 0) {
  437. ESP_LOGD(TAG, "Deleting file: %s", fn.c_str());
  438. /* Delete file */
  439. unlink(fn.c_str());
  440. }
  441. else
  442. {
  443. ESP_LOGD(TAG, "File does not exist: %s", fn.c_str());
  444. }
  445. /* Respond with an empty chunk to signal HTTP response completion */
  446. std::string zw = "file deleted\n";
  447. ESP_LOGD(TAG, "%s", zw.c_str());
  448. httpd_resp_send(req, zw.c_str(), strlen(zw.c_str()));
  449. httpd_resp_send_chunk(req, NULL, 0);
  450. return ESP_OK;
  451. }
  452. string zw = "ota without parameter - should not be the case!";
  453. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  454. httpd_resp_send(req, zw.c_str(), strlen(zw.c_str()));
  455. httpd_resp_send_chunk(req, NULL, 0);
  456. ESP_LOGE(TAG, "ota without parameter - should not be the case!");
  457. /*
  458. const char* resp_str;
  459. KillTFliteTasks();
  460. gpio_handler_deinit();
  461. if (ota_update_task(fn))
  462. {
  463. resp_str = "Firmware Update Successfull! You can restart now.";
  464. }
  465. else
  466. {
  467. resp_str = "Error during Firmware Update!!! Please check console output.";
  468. }
  469. httpd_resp_send(req, resp_str, strlen(resp_str));
  470. #ifdef DEBUG_DETAIL_ON
  471. LogFile.WriteHeapInfo("handler_ota_update - Done");
  472. #endif
  473. */
  474. return ESP_OK;
  475. };
  476. void hard_restart() {
  477. esp_task_wdt_init(1,true);
  478. esp_task_wdt_add(NULL);
  479. while(true);
  480. }
  481. void task_reboot(void *pvParameter)
  482. {
  483. while(1)
  484. {
  485. vTaskDelay(5000 / portTICK_PERIOD_MS);
  486. esp_restart();
  487. hard_restart();
  488. }
  489. vTaskDelete(NULL); //Delete this task if it exits from the loop above
  490. }
  491. void doReboot(){
  492. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Reboot triggered by Software (5s).");
  493. LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Reboot in 5sec");
  494. xTaskCreate(&task_reboot, "reboot", configMINIMAL_STACK_SIZE * 64, NULL, 10, NULL);
  495. // KillTFliteTasks(); // kills itself
  496. gpio_handler_destroy();
  497. #ifdef ENABLE_MQTT
  498. MQTTdestroy_client();
  499. #endif //ENABLE_MQTT
  500. vTaskDelay(5000 / portTICK_PERIOD_MS);
  501. esp_restart();
  502. hard_restart();
  503. }
  504. esp_err_t handler_reboot(httpd_req_t *req)
  505. {
  506. #ifdef DEBUG_DETAIL_ON
  507. LogFile.WriteHeapInfo("handler_reboot - Start");
  508. #endif
  509. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "handler_reboot");
  510. ESP_LOGI(TAG, "!!! System will restart within 5 sec!!!");
  511. const char* resp_str = "<body style='font-family: arial'> <h3 id=t></h3></body><script>var h='Rebooting!<br>The page will automatically reload in around 25..60s<br>(in case of a firmware update it can take up to 180s).<br>'; document.getElementById('t').innerHTML=h; setInterval(function (){h +='.'; document.getElementById('t').innerHTML=h; fetch(window.location.hostname,{mode: 'no-cors'}).then(r=>{parent.location.href=('/index.html');})}, 1000);</script>";
  512. httpd_resp_send(req, resp_str, strlen(resp_str));
  513. doReboot();
  514. #ifdef DEBUG_DETAIL_ON
  515. LogFile.WriteHeapInfo("handler_reboot - Done");
  516. #endif
  517. return ESP_OK;
  518. }
  519. void register_server_ota_sdcard_uri(httpd_handle_t server)
  520. {
  521. ESP_LOGI(TAG, "Registering URI handlers");
  522. httpd_uri_t camuri = { };
  523. camuri.method = HTTP_GET;
  524. camuri.uri = "/ota";
  525. camuri.handler = handler_ota_update;
  526. camuri.user_ctx = (void*) "Do OTA";
  527. httpd_register_uri_handler(server, &camuri);
  528. camuri.method = HTTP_GET;
  529. camuri.uri = "/reboot";
  530. camuri.handler = handler_reboot;
  531. camuri.user_ctx = (void*) "Reboot";
  532. httpd_register_uri_handler(server, &camuri);
  533. }