server_ota.cpp 21 KB

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