server_ota.cpp 21 KB

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