@@ -63,13 +63,28 @@ enum OtaTaskEventFlag : uint32_t {
6363
6464static esp_ota_img_states_t _otaImageState;
6565static OpenShock::FirmwareBootType _bootType;
66- static TaskHandle_t _taskHandle;
66+ static TaskHandle_t _taskHandle = nullptr ;
6767static OpenShock::SemVer _requestedVersion;
6868static OpenShock::SimpleMutex _requestedVersionMutex = {};
6969
7070using namespace OpenShock ;
7171
72- static bool _tryQueueUpdateRequest (const OpenShock::SemVer& version)
72+ static bool otaum_try_notify_task (uint32_t eventFlag)
73+ {
74+ if (_taskHandle == nullptr ) {
75+ OS_LOGW (TAG, " Unable to notify OTA task, task handle is null" );
76+ return false ;
77+ }
78+
79+ if (xTaskNotify (_taskHandle, eventFlag, eSetBits) != pdPASS) {
80+ OS_LOGE (TAG, " Failed to notify OTA task (event: 0x%08x)" , eventFlag);
81+ return false ;
82+ }
83+
84+ return true ;
85+ }
86+
87+ static bool otaum_try_queue_update_request (const OpenShock::SemVer& version)
7388{
7489 if (!_requestedVersionMutex.lock (pdMS_TO_TICKS (1000 ))) {
7590 OS_LOGE (TAG, " Failed to take requested version mutex" );
@@ -80,7 +95,7 @@ static bool _tryQueueUpdateRequest(const OpenShock::SemVer& version)
8095
8196 _requestedVersionMutex.unlock ();
8297
83- xTaskNotify (_taskHandle, OTA_TASK_EVENT_UPDATE_REQUESTED, eSetBits );
98+ otaum_try_notify_task ( OTA_TASK_EVENT_UPDATE_REQUESTED);
8499
85100 return true ;
86101}
@@ -106,7 +121,7 @@ static void otaum_evh_wifidisconnected(void* event_handler_arg, esp_event_base_t
106121 (void )event_id;
107122 (void )event_data;
108123
109- xTaskNotify (_taskHandle, OTA_TASK_EVENT_WIFI_DISCONNECTED, eSetBits );
124+ otaum_try_notify_task ( OTA_TASK_EVENT_WIFI_DISCONNECTED);
110125}
111126
112127static void otaum_evh_ipevent (void * event_handler_arg, esp_event_base_t event_base, int32_t event_id, void * event_data)
@@ -118,17 +133,17 @@ static void otaum_evh_ipevent(void* event_handler_arg, esp_event_base_t event_ba
118133 switch (event_id) {
119134 case IP_EVENT_GOT_IP6:
120135 case IP_EVENT_STA_GOT_IP:
121- xTaskNotify (_taskHandle, OTA_TASK_EVENT_WIFI_CONNECTED, eSetBits );
136+ otaum_try_notify_task ( OTA_TASK_EVENT_WIFI_CONNECTED);
122137 break ;
123138 case IP_EVENT_STA_LOST_IP:
124- xTaskNotify (_taskHandle, OTA_TASK_EVENT_WIFI_DISCONNECTED, eSetBits );
139+ otaum_try_notify_task ( OTA_TASK_EVENT_WIFI_DISCONNECTED);
125140 break ;
126141 default :
127142 return ;
128143 }
129144}
130145
131- static bool _sendProgressMessage (Serialization::Types::OtaUpdateProgressTask task, float progress)
146+ static bool otaum_send_progress_msg (Serialization::Types::OtaUpdateProgressTask task, float progress)
132147{
133148 int32_t updateId;
134149 if (!Config::GetOtaUpdateId (updateId)) {
@@ -159,18 +174,18 @@ static bool _sendFailureMessage(std::string_view message, bool fatal = false)
159174 return true ;
160175}
161176
162- static bool _flashAppPartition (const esp_partition_t * partition, std::string_view remoteUrl, const uint8_t (&remoteHash)[32])
177+ static bool otaum_flash_app_partition (const esp_partition_t * partition, std::string_view remoteUrl, const uint8_t (&remoteHash)[32])
163178{
164179 OS_LOGD (TAG, " Flashing app partition" );
165180
166- if (!_sendProgressMessage (Serialization::Types::OtaUpdateProgressTask::FlashingApplication, 0 .0f )) {
181+ if (!otaum_send_progress_msg (Serialization::Types::OtaUpdateProgressTask::FlashingApplication, 0 .0f )) {
167182 return false ;
168183 }
169184
170185 auto onProgress = [](std::size_t current, std::size_t total, float progress) -> bool {
171186 OS_LOGD (TAG, " Flashing app partition: %u / %u (%.2f%%)" , current, total, progress * 100 .0f );
172187
173- _sendProgressMessage (Serialization::Types::OtaUpdateProgressTask::FlashingApplication, progress);
188+ otaum_send_progress_msg (Serialization::Types::OtaUpdateProgressTask::FlashingApplication, progress);
174189
175190 return true ;
176191 };
@@ -181,7 +196,7 @@ static bool _flashAppPartition(const esp_partition_t* partition, std::string_vie
181196 return false ;
182197 }
183198
184- if (!_sendProgressMessage (Serialization::Types::OtaUpdateProgressTask::MarkingApplicationBootable, 0 .0f )) {
199+ if (!otaum_send_progress_msg (Serialization::Types::OtaUpdateProgressTask::MarkingApplicationBootable, 0 .0f )) {
185200 return false ;
186201 }
187202
@@ -195,9 +210,9 @@ static bool _flashAppPartition(const esp_partition_t* partition, std::string_vie
195210 return true ;
196211}
197212
198- static bool _flashFilesystemPartition (const esp_partition_t * parition, std::string_view remoteUrl, const uint8_t (&remoteHash)[32])
213+ static bool otaum_flash_fs_partition (const esp_partition_t * parition, std::string_view remoteUrl, const uint8_t (&remoteHash)[32])
199214{
200- if (!_sendProgressMessage (Serialization::Types::OtaUpdateProgressTask::PreparingForUpdate, 0 .0f )) {
215+ if (!otaum_send_progress_msg (Serialization::Types::OtaUpdateProgressTask::PreparingForUpdate, 0 .0f )) {
201216 return false ;
202217 }
203218
@@ -210,14 +225,14 @@ static bool _flashFilesystemPartition(const esp_partition_t* parition, std::stri
210225
211226 OS_LOGD (TAG, " Flashing filesystem partition" );
212227
213- if (!_sendProgressMessage (Serialization::Types::OtaUpdateProgressTask::FlashingFilesystem, 0 .0f )) {
228+ if (!otaum_send_progress_msg (Serialization::Types::OtaUpdateProgressTask::FlashingFilesystem, 0 .0f )) {
214229 return false ;
215230 }
216231
217232 auto onProgress = [](std::size_t current, std::size_t total, float progress) -> bool {
218233 OS_LOGD (TAG, " Flashing filesystem partition: %u / %u (%.2f%%)" , current, total, progress * 100 .0f );
219234
220- _sendProgressMessage (Serialization::Types::OtaUpdateProgressTask::FlashingFilesystem, progress);
235+ otaum_send_progress_msg (Serialization::Types::OtaUpdateProgressTask::FlashingFilesystem, progress);
221236
222237 return true ;
223238 };
@@ -228,7 +243,7 @@ static bool _flashFilesystemPartition(const esp_partition_t* parition, std::stri
228243 return false ;
229244 }
230245
231- if (!_sendProgressMessage (Serialization::Types::OtaUpdateProgressTask::VerifyingFilesystem, 0 .0f )) {
246+ if (!otaum_send_progress_msg (Serialization::Types::OtaUpdateProgressTask::VerifyingFilesystem, 0 .0f )) {
232247 return false ;
233248 }
234249
@@ -244,6 +259,13 @@ static bool _flashFilesystemPartition(const esp_partition_t* parition, std::stri
244259 return true ;
245260}
246261
262+ static void otaum_updatetask ()
263+ {
264+ if (esp_task_wdt_init (5 , true ) != ESP_OK) {
265+ OS_LOGE (TAG, " Failed to restore task watchdog timeout" );
266+ }
267+ };
268+
247269static void otaum_updatetask (void * arg)
248270{
249271 (void )arg;
@@ -355,7 +377,7 @@ static void otaum_updatetask(void* arg)
355377 continue ;
356378 }
357379
358- if (!_sendProgressMessage (Serialization::Types::OtaUpdateProgressTask::FetchingMetadata, 0 .0f )) {
380+ if (!otaum_send_progress_msg (Serialization::Types::OtaUpdateProgressTask::FetchingMetadata, 0 .0f )) {
359381 continue ;
360382 }
361383
@@ -393,24 +415,41 @@ static void otaum_updatetask(void* arg)
393415
394416 // Increase task watchdog timeout.
395417 // Prevents panics on some ESP32s when clearing large partitions.
396- esp_task_wdt_init (15 , true );
418+ if (esp_task_wdt_init (15 , true ) != ESP_OK) {
419+ OS_LOGE (TAG, " Failed to increase task watchdog timeout" );
420+ _sendFailureMessage (" Failed to increase task watchdog timeout" sv);
421+ continue ;
422+ }
423+
424+ auto restoreTaskWatchdogTimeout = []() {
425+ if (esp_task_wdt_init (5 , true ) != ESP_OK) {
426+ OS_LOGE (TAG, " Failed to restore task watchdog timeout" );
427+ }
428+ };
397429
398430 // Flash app and filesystem partitions.
399- if (!_flashFilesystemPartition (filesystemPartition, release.filesystemBinaryUrl , release.filesystemBinaryHash )) continue ;
400- if (!_flashAppPartition (appPartition, release.appBinaryUrl , release.appBinaryHash )) continue ;
431+ if (!otaum_flash_fs_partition (filesystemPartition, release.filesystemBinaryUrl , release.filesystemBinaryHash )) {
432+ restoreTaskWatchdogTimeout ();
433+ continue ;
434+ }
435+ if (!otaum_flash_app_partition (appPartition, release.appBinaryUrl , release.appBinaryHash )) {
436+ restoreTaskWatchdogTimeout ();
437+ continue ;
438+ }
401439
402440 // Set OTA boot type in config.
403441 if (!Config::SetOtaUpdateStep (OpenShock::OtaUpdateStep::Updated)) {
404442 OS_LOGE (TAG, " Failed to set OTA update step" );
405443 _sendFailureMessage (" Failed to set OTA update step" sv);
444+ restoreTaskWatchdogTimeout ();
406445 continue ;
407446 }
408447
409448 // Set task watchdog timeout back to default.
410- esp_task_wdt_init ( 5 , true );
449+ restoreTaskWatchdogTimeout ( );
411450
412451 // Send reboot message.
413- _sendProgressMessage (Serialization::Types::OtaUpdateProgressTask::Rebooting, 0 .0f );
452+ otaum_send_progress_msg (Serialization::Types::OtaUpdateProgressTask::Rebooting, 0 .0f );
414453
415454 // Reboot into new firmware.
416455 OS_LOGI (TAG, " Restarting into new firmware..." );
@@ -505,6 +544,12 @@ bool OtaUpdateManager::Init()
505544 }
506545 }
507546
547+ // Start OTA update task.
548+ if (TaskUtils::TaskCreateExpensive (otaum_updatetask, " OTA Update" , 8192 , nullptr , 1 , &_taskHandle) != pdPASS) { // PROFILED: 6.2KB stack usage
549+ OS_LOGE (TAG, " Failed to create OTA update task" );
550+ return false ;
551+ }
552+
508553 err = esp_event_handler_register (IP_EVENT, ESP_EVENT_ANY_ID, otaum_evh_ipevent, nullptr );
509554 if (err != ESP_OK) {
510555 OS_LOGE (TAG, " Failed to register event handler for IP_EVENT: %s" , esp_err_to_name (err));
@@ -517,9 +562,6 @@ bool OtaUpdateManager::Init()
517562 return false ;
518563 }
519564
520- // Start OTA update task.
521- TaskUtils::TaskCreateExpensive (otaum_updatetask, " OTA Update" , 8192 , nullptr , 1 , &_taskHandle); // PROFILED: 6.2KB stack usage
522-
523565 return true ;
524566}
525567
@@ -671,14 +713,24 @@ bool OtaUpdateManager::TryGetFirmwareRelease(const OpenShock::SemVer& version, F
671713 }
672714 }
673715
716+ if (!foundAppHash) {
717+ OS_LOGE (TAG, " Missing hash for app.bin" );
718+ return false ;
719+ }
720+
721+ if (!foundFilesystemHash) {
722+ OS_LOGE (TAG, " Missing hash for staticfs.bin" );
723+ return false ;
724+ }
725+
674726 return true ;
675727}
676728
677729bool OtaUpdateManager::TryStartFirmwareUpdate (const OpenShock::SemVer& version)
678730{
679731 OS_LOGD (TAG, " Requesting firmware version %s" , version.toString ().c_str ()); // TODO: This is abusing the SemVer::toString() method causing alot of string copies, fix this
680732
681- return _tryQueueUpdateRequest (version);
733+ return otaum_try_queue_update_request (version);
682734}
683735
684736FirmwareBootType OtaUpdateManager::GetFirmwareBootType ()
0 commit comments