@@ -10,7 +10,7 @@ Because this application is based on _switch_example_ . you could find more deta
1010
1111Change 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
2727Import 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.
3030You 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
3939Register 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
5452Send 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
7063Lookup 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
9396Run 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
0 commit comments