Skip to content

Commit 29f433e

Browse files
hdlee27Kwang-Hui
authored andcommitted
update ota_demo app to apply new code for OTA_DEMO
(cherry-picked from commit beb8301) Signed-off-by: Hyundo Lee <54927573+hdlee27@users.noreply.github.com>
1 parent 50100f8 commit 29f433e

20 files changed

+2188
-729
lines changed

apps/esp32/ota_demo/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# The following lines of boilerplate have to be in your project's
2+
# CMakeLists in this exact order for cmake to work correctly
3+
cmake_minimum_required(VERSION 3.5)
4+
5+
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
6+
7+
project(ota_demo)
8+

apps/esp32/ota_demo/README.md

Lines changed: 52 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Because this application is based on _switch_example_ . you could find more deta
1010

1111
Change configuration
1212
-------------------
13-
These compile options will enable HTTPS and MBEDTLS feature to connect OTA server.
13+
These compile options will enable HTTPS and MBEDTLS feature to connect OTA server.
1414
`stdkconfig`
1515
```c
1616
// Enable option for HTTPS
@@ -26,7 +26,7 @@ CONFIG_STDK_IOT_CORE_NET_MBEDTLS=y
2626

2727
Import certificate
2828
-------------------
29-
This application requires _root.pem_, _public_key.pem_ which is used for https connection and firmware signature validation.
29+
This application requires _root.pem_, _public_key.pem_ which is used for https connection and firmware signature validation.
3030
You can find how to create these file from [here](../../../doc/ota_demo.md#preparing-certificate)
3131

3232
```sh
@@ -38,82 +38,82 @@ $ cp public_key.pem [st-device-sdk-c-ref path]/apps/esp32/ota_demo/main
3838

3939
Register capability callback
4040
-------------------
41-
This code init handle and add command callback for **Firmware Update** capability.
42-
`ota_demo.c`
41+
This code init handle and add command callback for **Firmware Update** capability.
42+
`main.c`
4343
```c
44-
// create handle for capability
45-
ota_cap_handle = st_cap_handle_init(ctx, "main", "firmwareUpdate", cap_current_version_init_cb, NULL);
44+
// create ota data for capability
45+
cap_ota_data = caps_ota_initialize(ctx, "main", NULL, NULL);
4646

4747
// set command callback
48-
iot_err = st_cap_cmd_set_cb(ota_cap_handle, "updateFirmware", update_firmware_cmd_cb, NULL);
49-
if (iot_err)
50-
printf("fail to set cmd_cb for updateFirmware");
48+
cap_ota_data->cmd_update_firmware_usr_cb = cap_update_cmd_cb;
5149
```
5250
***
5351

5452
Send currentVersion
5553
-------------------
56-
This code will update `currentVersion` attribute by sending event.
57-
`ota_demo.c`
54+
This code will update `currentVersion` attribute by sending event.
55+
`main.c`
5856
```c
59-
void cap_current_version_init_cb(IOT_CAP_HANDLE *handle, void *usr_data)
60-
{
61-
...
62-
init_evt = st_cap_attr_create_string("currentVersion", OTA_FIRMWARE_VERSION, NULL);
57+
char *firmware_version = get_current_firmware_version();
6358

64-
sequence_no = st_cap_attr_send(handle, evt_num, &init_evt);
65-
...
66-
}
59+
cap_ota_data->set_currentVersion(cap_ota_data, firmware_version);
6760
```
6861
***
6962
7063
Lookup available new firmware
7164
-----------------------------
72-
This code will lookup available new firmware by reading OTA server's `versioninfo.json` and send `availableVersion` event.
73-
`ota_demo.c`
65+
This code will lookup available new firmware by reading OTA server's `versioninfo.json` and send `availableVersion` event.
66+
`main.c`
7467
```c
75-
static void ota_polling_task_func(void *arg)
76-
{
77-
while (1) {
68+
69+
void ota_polling_task(void *arg)
70+
{
71+
while (1) {
7872
...
79-
esp_err_t ret = ota_https_read_version_info(&read_data, &read_data_len);
80-
if (ret == ESP_OK) {
81-
...
82-
esp_err_t err = ota_api_get_available_version(read_data, read_data_len, &available_version);
83-
...
84-
cap_available_version_set(available_version);
85-
...
86-
}
73+
ota_check_for_update((void *)arg);
8774
...
88-
}
75+
}
76+
}
77+
78+
void ota_check_for_update(void *user_data)
79+
{
80+
...
81+
ota_err_t ret = _read_version_info_from_server(&read_data, &read_data_len);
82+
if (ret == OTA_OK) {
83+
...
84+
ret = _get_available_version(read_data, read_data_len, current_version, &available_version);
85+
...
86+
if (available_version) {
87+
caps_data->set_availableVersion(caps_data, available_version);
88+
caps_data->attr_availableVersion_send(caps_data);
89+
...
90+
}
91+
}
8992
}
9093
```
9194
***
9295

9396
Run firmware update
9497
-------------------
95-
This code will run firmware update by receiving `updateFirmware` command.
96-
`ota_demo.c`
98+
This code will run firmware update by receiving `updateFirmware` command.
99+
`main.c`
97100
```c
98-
static void ota_task_func(void * pvParameter)
99-
{
100-
printf("Starting OTA...\n");
101-
102-
esp_err_t ret = ota_https_update_device();
103-
if (ret != ESP_OK) {
104-
printf("Firmware Upgrades Failed (%d) \n", ret);
105-
_task_fatal_error();
106-
}
107-
108-
printf("Restart system!\n");
109-
esp_restart();
101+
static void ota_update_task(void * pvParameter)
102+
{
103+
printf("\n Starting OTA...\n");
104+
105+
```
106+
ota_err_t ret = ota_update_device();
107+
```
108+
109+
printf("Prepare to restart system!");
110+
ota_restart_device();
110111
}
111112

112-
void update_firmware_cmd_cb(IOT_CAP_HANDLE *handle,
113-
iot_cap_cmd_data_t *cmd_data, void *usr_data)
114-
{
115-
ota_nvs_flash_init();
116-
xTaskCreate(&ota_task_func, "ota_task_func", 8096, NULL, 5, &ota_task_handle);
117-
}
113+
static void cap_update_cmd_cb(struct caps_ota_data *caps_data)
114+
{
115+
ota_nvs_flash_init();
116+
xTaskCreate(&ota_update_task, "ota_update_task", 8096, NULL, 5, &ota_task_handle);
117+
}
118118
```
119119
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
idf_component_register(SRCS "main.c"
2+
"device_control.c"
3+
"iot_cli_cmd.c"
4+
"iot_uart_cli.c"
5+
"ota_util.c"
6+
"caps_switch.c"
7+
"caps_switchLevel.c"
8+
"caps_firmwareUpdate.c"
9+
INCLUDE_DIRS "$ENV{STDK_CORE_PATH}/src/include/bsp"
10+
"$ENV{STDK_CORE_PATH}/src/include/os"
11+
"$ENV{STDK_CORE_PATH}/src/include/mqtt"
12+
"$ENV{STDK_CORE_PATH}/src/include/external"
13+
"$ENV{STDK_CORE_PATH}/src/port/net/mbedtls"
14+
EMBED_FILES "device_info.json"
15+
"onboarding_config.json"
16+
"public_key.pem"
17+
"root.pem"
18+
)
19+
20+
set(STDK_IOT_CORE_USE_DEFINED_CONFIG "y")
21+
22+
set(STDK_LINK_LIBRARY
23+
__idf_libsodium
24+
__idf_json
25+
)
26+
27+
set(STDK_INCLUDE_PATH
28+
"$ENV{IDF_PATH}/components/freertos/include/freertos"
29+
"$ENV{IDF_PATH}/components/nvs_flash/include"
30+
"$ENV{IDF_PATH}/components/spi_flash/include"
31+
"$ENV{IDF_PATH}/components/bootloader_support/include"
32+
)
33+
34+
add_subdirectory($ENV{STDK_CORE_PATH} iotcore)
35+
target_link_libraries(${COMPONENT_LIB} PUBLIC iotcore)

0 commit comments

Comments
 (0)