4
4
5
5
#if SOC_IEEE802154_SUPPORTED && CONFIG_ZB_ENABLED
6
6
7
+ #include " esp_ota_ops.h"
8
+
7
9
// forward declaration of all implemented handlers
8
10
static esp_err_t zb_attribute_set_handler (const esp_zb_zcl_set_attr_value_message_t *message);
9
11
static esp_err_t zb_attribute_reporting_handler (const esp_zb_zcl_report_attr_message_t *message);
@@ -13,6 +15,9 @@ static esp_err_t zb_cmd_ias_zone_status_change_handler(const esp_zb_zcl_ias_zone
13
15
static esp_err_t zb_cmd_ias_zone_enroll_response_handler (const esp_zb_zcl_ias_zone_enroll_response_message_t *message);
14
16
static esp_err_t zb_cmd_default_resp_handler (const esp_zb_zcl_cmd_default_resp_message_t *message);
15
17
static esp_err_t zb_window_covering_movement_resp_handler (const esp_zb_zcl_window_covering_movement_message_t *message);
18
+ static esp_err_t zb_ota_upgrade_status_handler (const esp_zb_zcl_ota_upgrade_value_message_t *message);
19
+ static esp_err_t zb_ota_upgrade_query_image_resp_handler (const esp_zb_zcl_ota_upgrade_query_image_resp_message_t *message);
20
+
16
21
17
22
// Zigbee action handlers
18
23
[[maybe_unused]]
@@ -32,6 +37,12 @@ static esp_err_t zb_action_handler(esp_zb_core_action_callback_id_t callback_id,
32
37
case ESP_ZB_CORE_WINDOW_COVERING_MOVEMENT_CB_ID:
33
38
ret = zb_window_covering_movement_resp_handler ((esp_zb_zcl_window_covering_movement_message_t *)message);
34
39
break ;
40
+ case ESP_ZB_CORE_OTA_UPGRADE_VALUE_CB_ID:
41
+ ret = zb_ota_upgrade_status_handler ((esp_zb_zcl_ota_upgrade_value_message_t *)message);
42
+ break ;
43
+ case ESP_ZB_CORE_OTA_UPGRADE_QUERY_IMAGE_RESP_CB_ID:
44
+ ret = zb_ota_upgrade_query_image_resp_handler ((esp_zb_zcl_ota_upgrade_query_image_resp_message_t *)message);
45
+ break ;
35
46
case ESP_ZB_CORE_CMD_DEFAULT_RESP_CB_ID: ret = zb_cmd_default_resp_handler ((esp_zb_zcl_cmd_default_resp_message_t *)message); break ;
36
47
default : log_w (" Receive unhandled Zigbee action(0x%x) callback" , callback_id); break ;
37
48
}
@@ -186,22 +197,6 @@ static esp_err_t zb_cmd_ias_zone_enroll_response_handler(const esp_zb_zcl_ias_zo
186
197
return ESP_OK;
187
198
}
188
199
189
- static esp_err_t zb_cmd_default_resp_handler (const esp_zb_zcl_cmd_default_resp_message_t *message) {
190
- if (!message) {
191
- log_e (" Empty message" );
192
- return ESP_FAIL;
193
- }
194
- if (message->info .status != ESP_ZB_ZCL_STATUS_SUCCESS) {
195
- log_e (" Received message: error status(%d)" , message->info .status );
196
- return ESP_ERR_INVALID_ARG;
197
- }
198
- log_v (
199
- " Received default response: from address(0x%x), src_endpoint(%d) to dst_endpoint(%d), cluster(0x%x) with status 0x%x" ,
200
- message->info .src_address .u .short_addr , message->info .src_endpoint , message->info .dst_endpoint , message->info .cluster , message->status_code
201
- );
202
- return ESP_OK;
203
- }
204
-
205
200
static esp_err_t zb_window_covering_movement_resp_handler (const esp_zb_zcl_window_covering_movement_message_t *message) {
206
201
if (!message) {
207
202
log_e (" Empty message" );
@@ -224,4 +219,109 @@ static esp_err_t zb_window_covering_movement_resp_handler(const esp_zb_zcl_windo
224
219
return ESP_OK;
225
220
}
226
221
222
+ static esp_err_t zb_ota_upgrade_status_handler (const esp_zb_zcl_ota_upgrade_value_message_t *message)
223
+ {
224
+ static const esp_partition_t *s_ota_partition = NULL ;
225
+ static esp_ota_handle_t s_ota_handle = 0 ;
226
+
227
+ static uint32_t total_size = 0 ;
228
+ static uint32_t offset = 0 ;
229
+ static int64_t start_time = 0 ;
230
+ esp_err_t ret = ESP_OK;
231
+
232
+ if (message->info .status == ESP_ZB_ZCL_STATUS_SUCCESS) {
233
+ switch (message->upgrade_status ) {
234
+ case ESP_ZB_ZCL_OTA_UPGRADE_STATUS_START:
235
+ log_i (" Zigbee OTA - Upgrade start" );
236
+ start_time = esp_timer_get_time ();
237
+ s_ota_partition = esp_ota_get_next_update_partition (NULL );
238
+ assert (s_ota_partition);
239
+ ret = esp_ota_begin (s_ota_partition, 0 , &s_ota_handle);
240
+ if (ret == ESP_OK) {
241
+ log_i (" Zigbee OTA - OTA partition begin" );
242
+ } else {
243
+ log_e (" Zigbee OTA - Failed to begin OTA partition, status: %s" , esp_err_to_name (ret));
244
+ return ret;
245
+ }
246
+ break ;
247
+ case ESP_ZB_ZCL_OTA_UPGRADE_STATUS_RECEIVE:
248
+ total_size = message->ota_header .image_size ;
249
+ offset += message->payload_size ;
250
+ log_i (" Zigbee OTA - Client receives data: progress [%ld/%ld]" , offset, total_size);
251
+ if (message->payload_size && message->payload ) {
252
+ ret = esp_ota_write (s_ota_handle, (const void *)message->payload , message->payload_size );
253
+ if (ret == ESP_OK) {
254
+ log_i (" Zigbee OTA - Write OTA data to partition" );
255
+ } else {
256
+ log_e (" Zigbee OTA - Failed to write OTA data to partition, status: %s" , esp_err_to_name (ret));
257
+ return ret;
258
+ }
259
+ }
260
+ break ;
261
+ case ESP_ZB_ZCL_OTA_UPGRADE_STATUS_APPLY:
262
+ log_i (" Zigbee OTA - Upgrade apply" );
263
+ break ;
264
+ case ESP_ZB_ZCL_OTA_UPGRADE_STATUS_CHECK:
265
+ ret = offset == total_size ? ESP_OK : ESP_FAIL;
266
+ log_i (" Zigbee OTA - Upgrade check status: %s" , esp_err_to_name (ret));
267
+ break ;
268
+ case ESP_ZB_ZCL_OTA_UPGRADE_STATUS_FINISH:
269
+ log_i (" Zigbee OTA - Finish" );
270
+ log_i (" Zigbee OTA - Information: version: 0x%lx, manufacturer code: 0x%x, image type: 0x%x, total size: %ld bytes, cost time: %lld ms" ,
271
+ message->ota_header .file_version , message->ota_header .manufacturer_code , message->ota_header .image_type ,
272
+ message->ota_header .image_size , (esp_timer_get_time () - start_time) / 1000 );
273
+ ret = esp_ota_end (s_ota_handle);
274
+ if (ret == ESP_OK) {
275
+ log_i (" Zigbee OTA - OTA partition end" );
276
+ } else {
277
+ log_e (" Zigbee OTA - Failed to end OTA partition, status: %s" , esp_err_to_name (ret));
278
+ return ret;
279
+ }
280
+ ret = esp_ota_set_boot_partition (s_ota_partition);
281
+ if (ret == ESP_OK) {
282
+ log_i (" Zigbee OTA - Set OTA boot partition" );
283
+ } else {
284
+ log_e (" Zigbee OTA - Failed to set OTA boot partition, status: %s" , esp_err_to_name (ret));
285
+ return ret;
286
+ }
287
+ log_w (" Zigbee OTA - Prepare to restart system" );
288
+ esp_restart ();
289
+ break ;
290
+ default :
291
+ log_i (" Zigbee OTA - Status: %d" , message->upgrade_status );
292
+ break ;
293
+ }
294
+ }
295
+ return ret;
296
+ }
297
+
298
+ static esp_err_t zb_ota_upgrade_query_image_resp_handler (const esp_zb_zcl_ota_upgrade_query_image_resp_message_t *message)
299
+ {
300
+ if (message->info .status == ESP_ZB_ZCL_STATUS_SUCCESS) {
301
+ log_i (" Zigbee - Queried OTA image from address: 0x%04hx, endpoint: %d" , message->server_addr .u .short_addr , message->server_endpoint );
302
+ log_i (" Zigbee - Image version: 0x%lx, manufacturer code: 0x%x, image size: %ld" , message->file_version , message->manufacturer_code ,
303
+ message->image_size );
304
+ log_i (" Zigbee - Approving OTA image upgrade" );
305
+ } else {
306
+ log_i (" Zigbee - OTA image upgrade response status: 0x%x" , message->info .status );
307
+ }
308
+ return ESP_OK;
309
+ }
310
+
311
+ static esp_err_t zb_cmd_default_resp_handler (const esp_zb_zcl_cmd_default_resp_message_t *message) {
312
+ if (!message) {
313
+ log_e (" Empty message" );
314
+ return ESP_FAIL;
315
+ }
316
+ if (message->info .status != ESP_ZB_ZCL_STATUS_SUCCESS) {
317
+ log_e (" Received message: error status(%d)" , message->info .status );
318
+ return ESP_ERR_INVALID_ARG;
319
+ }
320
+ log_v (
321
+ " Received default response: from address(0x%x), src_endpoint(%d) to dst_endpoint(%d), cluster(0x%x) with status 0x%x" ,
322
+ message->info .src_address .u .short_addr , message->info .src_endpoint , message->info .dst_endpoint , message->info .cluster , message->status_code
323
+ );
324
+ return ESP_OK;
325
+ }
326
+
227
327
#endif // SOC_IEEE802154_SUPPORTED && CONFIG_ZB_ENABLED
0 commit comments