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