Skip to content

Commit

Permalink
Merge pull request #84 from sorru94/improve_ota_update
Browse files Browse the repository at this point in the history
OTA update interfaces update
  • Loading branch information
harlem88 authored May 30, 2023
2 parents 4c509e8 + 7b8828a commit 53fdeaa
Show file tree
Hide file tree
Showing 9 changed files with 500 additions and 269 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
strategy:
matrix:
idf-version:
- "4.3"
- "4.4.4"
- "5.0.1"
build-system:
- idf
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [0.7.0] - Unreleased
### Added
- Add support to ESP-IDF v5.0.
- Add support for `io.edgehog.devicemanager.OTAEvent` interface.
- Add support for update/cancel operation in `io.edgehog.devicemanager.OTARequest` interface.

### Removed
- Remove support for `io.edgehog.devicemanager.OTAResponse` interface.

### Fixed
- Fix GPIO still active issue after execution of LedBehavior routine.
Expand Down
34 changes: 33 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Create a new `idf_component.yml` in the project root directory, as follows:
## IDF Component Manager Manifest File
dependencies:
idf:
version: ">=4.1.0"
version: ">=4.4.4"
edgehog-esp32-device:
version: "*" # this is the latest commit on the main branch
git: https://github.com/edgehog-device-manager/edgehog-esp32-device.git
Expand All @@ -56,6 +56,38 @@ dependencies:
idf.py build
```

## Component Free RTOS APIs usage

The Edgehog ESP32 Device interacts internally with the Free RTOS APIs. As such some factors
should be taken into account when integrating this component in a larger system.

The following **tasks** are spawned directly by the Edgehog ESP32 Device:
- `BLINK TASK`: Provides functionality for visual feedback using an on board LED.
It is only spawned if `CONFIG_INDICATOR_GPIO_ENABLE` is set in the Edgehog ESP32 device
configuration, will use `2048` words from the stack, and can be triggered by a publish from the
Astarte cluster to the dedicated LED interface. This task has a fixed duration and will be deleted
at timeout.
- `OTA UPDATE TASK`: Provides functionality for OTA updates.
It will use `4096` words from the stack, and can be triggered by a publish from the
Astarte cluster to the dedicated OTA update interface. This task does not have a fixed duration, it
will run untill a successful OTA update has been downloaded and flashed or the procedure failed.
Note that the OTA update task could restart the device.

All of the tasks are spawned with the lowest priority and rely on the time slicing functionality
of freertos to run concurrently with the main task.

Other than tasks, a certain number of **software timers** are also created to be used for telemetry.
The actual number of timers that this component is going to use will depend on the
telemetry configuration of your project. Each telemetry type will create a separate timer.
Software timers run all in a single task instantiated by freertos. The stack size and priority for
such task should be configured in the project using this component.
This module has been tested using `2048` words for the stack size and a priority of `1` for the
timer task.

In addition to what stated above, this component requires an Astarte ESP32 Device to be externally
instantiated and provided in its configuration struct. The Astarte ESP32 Device interacts internally
with the Free RTOS APIs and its resource usage should be evaluated separately.

## Resources

* [ESP32 Component Documentation](https://edgehog-device-manager.github.io/docs/snapshot/device-sdks/esp32/)
Expand Down
14 changes: 8 additions & 6 deletions include/edgehog.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,14 @@ typedef enum
EDGEHOG_ERR = 1, /**< A generic error occurred. This is usually an internal error in the SDK */
EDGEHOG_ERR_NETWORK = 2, /**< A generic network error occurred. */
EDGEHOG_ERR_NVS = 3, /**< A generic error occurred when dealing with NVS */
EDGEHOG_ERR_OTA_ALREADY_IN_PROGRESS = 4, /**< Attempted to perform OTA operation while there is another one already active*/
EDGEHOG_ERR_OTA_FAILED = 5, /**< An error occurred during OTA procedure */
EDGEHOG_ERR_OTA_DEPLOY = 6, /**< An error occurred during OTA Deploy procedure */
EDGEHOG_ERR_OTA_WRONG_PARTITION = 7, /**< The OTA procedure boot on the wrong partition */
EDGEHOG_ERR_TASK_CREATE = 8, /**< xTaskCreate was unable to spawn a new task */
EDGEHOG_ERR_DEVICE_NOT_READY = 9, /**< Tried to perform an operation on a Device in a non-ready or initialized state */
EDGEHOG_ERR_OTA_INVALID_REQUEST = 4, /**< Invalid OTA update request received */
EDGEHOG_ERR_OTA_ALREADY_IN_PROGRESS = 5, /**< Attempted to perform OTA operation while there is another one already active*/
EDGEHOG_ERR_OTA_INVALID_IMAGE = 6, /**< Invalid OTA image received */
EDGEHOG_ERR_OTA_SYSTEM_ROLLBACK = 7, /**< The OTA procedure boot on the wrong partition */
EDGEHOG_ERR_OTA_CANCELED = 8, /**< OTA update aborted by Edgehog half way during the procedure */
EDGEHOG_ERR_OTA_INTERNAL = 9, /**< An error occurred during OTA procedure */
EDGEHOG_ERR_TASK_CREATE = 10, /**< xTaskCreate was unable to spawn a new task */
EDGEHOG_ERR_DEVICE_NOT_READY = 11, /**< Tried to perform an operation on a Device in a non-ready or initialized state */
}edgehog_err_t;

// clang-format on
Expand Down
17 changes: 7 additions & 10 deletions private/edgehog_ota.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,34 +28,31 @@ extern "C" {
#include "edgehog_device.h"

extern const astarte_interface_t ota_request_interface;
extern const astarte_interface_t ota_response_interface;
extern const astarte_interface_t ota_event_interface;

/**
* @brief initialize Edgehog device OTA.
*
* @details This function initializes the OTA procedure and
* if there is any pending OTA it completes it.
*
* @param edgehog_device A valid Edgehog device handle.
* @param edgehog_dev A valid Edgehog device handle.
*/

void edgehog_ota_init(edgehog_device_handle_t edgehog_device);
void edgehog_ota_init(edgehog_device_handle_t edgehog_dev);

/**
* @brief receive Edgehog device OTA.
*
* @details This function receives an OTA event request from Astarte,
* internally call do_ota function which is blocking, the calling
* task will be blocked until the OTA procedure completes.
* @details This function receives an OTA event request from Astarte. This function may spawn a
* new task to preform the OTA update.
*
* @param edgehog_device A valid Edgehog device handle.
* @param edgehog_dev A valid Edgehog device handle.
* @param event_request A valid Astarte device data event.
*
* @return EDGEHOG_OK if the OTA event is handled successfully, an edgehog_err_t otherwise.
*/

edgehog_err_t edgehog_ota_event(
edgehog_device_handle_t edgehog_device, astarte_device_data_event_t *event_request);
edgehog_device_handle_t edgehog_dev, astarte_device_data_event_t *event_request);

#ifdef __cplusplus
}
Expand Down
12 changes: 3 additions & 9 deletions src/edgehog_device.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@
#include <esp_timer.h>
#include <esp_wifi.h>
#include <esp_wifi_types.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <string.h>
#include <uuid.h>

Expand Down Expand Up @@ -118,13 +116,9 @@ void edgehog_device_astarte_event_handler(
}

if (strcmp(event->interface_name, ota_request_interface.name) == 0) {
// Beware this function blocks the caller until OTA is completed.
edgehog_err_t ota_result = edgehog_ota_event(edgehog_device, event);
if (ota_result == EDGEHOG_OK) {
ESP_LOGI(TAG, "OTA Deploy end successfully, device restart in 5 seconds");
vTaskDelay(pdMS_TO_TICKS(5000));
ESP_LOGI(TAG, "Device restart");
esp_restart();
if (ota_result != EDGEHOG_OK) {
ESP_LOGE(TAG, "Unable to handle OTA update request");
}
} else if (strcmp(event->interface_name, commands_interface.name) == 0) {
if (edgehog_command_event(event) != EDGEHOG_OK) {
Expand Down Expand Up @@ -233,7 +227,7 @@ esp_err_t add_interfaces(astarte_device_handle_t device)
&wifi_scan_result_interface,
&system_info_interface,
&ota_request_interface,
&ota_response_interface,
&ota_event_interface,
&storage_usage_interface,
&battery_status_interface,
&commands_interface,
Expand Down
4 changes: 2 additions & 2 deletions src/edgehog_led.c
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ static edgehog_err_t set_led_behavior(
led_manager->current_config->default_behavior = led_manager->default_behavior;
led_manager->current_config->terminated = false;

BaseType_t ret = xTaskCreate(blinkTaskCode, "NAME", STACK_SIZE, led_manager->current_config,
tskIDLE_PRIORITY, &led_manager->task_handle);
BaseType_t ret = xTaskCreate(blinkTaskCode, "BLINK TASK", STACK_SIZE,
led_manager->current_config, tskIDLE_PRIORITY, &led_manager->task_handle);

if (ret != pdPASS) {
free(led_manager->current_config);
Expand Down
Loading

0 comments on commit 53fdeaa

Please sign in to comment.