Skip to content

Commit 71a556b

Browse files
committed
fix(zigbee): Fix errors and warnings, swap parameters order
1 parent a83d7a2 commit 71a556b

File tree

3 files changed

+100
-99
lines changed

3 files changed

+100
-99
lines changed

Diff for: libraries/Zigbee/src/ZigbeeEP.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -376,8 +376,7 @@ void ZigbeeEP::zbReadTimeCluster(const esp_zb_zcl_attribute_t *attribute) {
376376
// uint8_t max_data_size; /*!< The maximum size of OTA data */
377377
// } esp_zb_zcl_ota_upgrade_client_variable_t;
378378

379-
void ZigbeeEP::addOTAClient(uint32_t file_version, uint16_t manufacturer, uint16_t image_type, uint32_t downloaded_file_ver,
380-
uint16_t hw_version, uint8_t max_data_size) {
379+
void ZigbeeEP::addOTAClient(uint32_t file_version, uint32_t downloaded_file_ver, uint16_t hw_version, uint16_t manufacturer, uint16_t image_type, uint8_t max_data_size) {
381380

382381
esp_zb_ota_cluster_cfg_t ota_cluster_cfg = {};
383382
ota_cluster_cfg.ota_upgrade_file_version = file_version;//OTA_UPGRADE_RUNNING_FILE_VERSION;

Diff for: libraries/Zigbee/src/ZigbeeEP.h

+2-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
/* Useful defines */
1111
#define ZB_CMD_TIMEOUT 10000 // 10 seconds
12-
#define OTA_UPGRADE_QUERY_INTERVAL (1 * 60) // 1 minutes
12+
#define OTA_UPGRADE_QUERY_INTERVAL (1 * 60) // 1 hour = 60 minutes
1313

1414
#define ZB_ARRAY_LENTH(arr) (sizeof(arr) / sizeof(arr[0]))
1515
#define XYZ_TO_RGB(X, Y, Z, r, g, b) \
@@ -108,8 +108,7 @@ class ZigbeeEP {
108108
return _allow_multiple_binding;
109109
}
110110

111-
void addOTAClient(uint32_t file_version = 0x01010100, uint16_t manufacturer = 0x1001, uint16_t image_type = 0x1011,
112-
uint32_t downloaded_file_ver = 0x01010101, uint16_t hw_version = 0x0101, uint8_t max_data_size = 223);
111+
void addOTAClient(uint32_t file_version, uint32_t downloaded_file_ver, uint16_t hw_version, uint16_t manufacturer, uint16_t image_type, uint8_t max_data_size = 223);
113112
void requestOTAUpdate();
114113

115114
// findEndpoind may be implemented by EPs to find and bind devices

Diff for: libraries/Zigbee/src/ZigbeeHandlers.cpp

+97-94
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,24 @@
55
#if SOC_IEEE802154_SUPPORTED && CONFIG_ZB_ENABLED
66

77
#include "esp_ota_ops.h"
8+
#if CONFIG_ZB_DELTA_OTA // Delta OTA, code is prepared for this feature but not enabled by default
9+
#include "esp_delta_ota_ops.h"
10+
#endif
11+
12+
//OTA Upgrade defines and variables
13+
#define OTA_ELEMENT_HEADER_LEN 6 /* OTA element format header size include tag identifier and length field */
14+
15+
/**
16+
* @name Enumeration for the tag identifier denotes the type and format of the data within the element
17+
* @anchor esp_ota_element_tag_id_t
18+
*/
19+
typedef enum esp_ota_element_tag_id_e {
20+
UPGRADE_IMAGE = 0x0000, /*!< Upgrade image */
21+
} esp_ota_element_tag_id_t;
22+
23+
static const esp_partition_t *s_ota_partition = NULL;
24+
static esp_ota_handle_t s_ota_handle = 0;
25+
static bool s_tagid_received = false;
826

927
// forward declaration of all implemented handlers
1028
static esp_err_t zb_attribute_set_handler(const esp_zb_zcl_set_attr_value_message_t *message);
@@ -219,21 +237,6 @@ static esp_err_t zb_window_covering_movement_resp_handler(const esp_zb_zcl_windo
219237
return ESP_OK;
220238
}
221239

222-
//OTA Upgrade
223-
#define OTA_ELEMENT_HEADER_LEN 6 /* OTA element format header size include tag identifier and length field */
224-
225-
/**
226-
* @name Enumeration for the tag identifier denotes the type and format of the data within the element
227-
* @anchor esp_ota_element_tag_id_t
228-
*/
229-
typedef enum esp_ota_element_tag_id_e {
230-
UPGRADE_IMAGE = 0x0000, /*!< Upgrade image */
231-
} esp_ota_element_tag_id_t;
232-
233-
static const esp_partition_t *s_ota_partition = NULL;
234-
static esp_ota_handle_t s_ota_handle = 0;
235-
static bool s_tagid_received = false;
236-
237240
static esp_err_t esp_element_ota_data(uint32_t total_size, const void *payload, uint16_t payload_size, void **outbuf, uint16_t *outlen)
238241
{
239242
static uint16_t tagid = 0;
@@ -247,16 +250,17 @@ static esp_err_t esp_element_ota_data(uint32_t total_size, const void *payload,
247250
return ESP_ERR_INVALID_ARG;
248251
}
249252

250-
tagid = *(const uint16_t *)payload;
251-
length = *(const uint32_t *)(payload + sizeof(tagid));
253+
const uint8_t *payload_ptr = (const uint8_t *)payload;
254+
tagid = *(const uint16_t *)payload_ptr;
255+
length = *(const uint32_t *)(payload_ptr + sizeof(tagid));
252256
if ((length + OTA_ELEMENT_HEADER_LEN) != total_size) {
253257
log_e("Invalid element length [%ld/%ld]", length, total_size);
254258
return ESP_ERR_INVALID_ARG;
255259
}
256260

257261
s_tagid_received = true;
258262

259-
data_buf = (void *)(payload + OTA_ELEMENT_HEADER_LEN);
263+
data_buf = (void *)(payload_ptr + OTA_ELEMENT_HEADER_LEN);
260264
data_len = payload_size - OTA_ELEMENT_HEADER_LEN;
261265
} else {
262266
data_buf = (void *)payload;
@@ -279,98 +283,97 @@ static esp_err_t esp_element_ota_data(uint32_t total_size, const void *payload,
279283

280284
static esp_err_t zb_ota_upgrade_status_handler(const esp_zb_zcl_ota_upgrade_value_message_t *message)
281285
{
282-
static uint32_t total_size = 0;
283-
static uint32_t offset = 0;
284-
static int64_t start_time = 0;
285-
esp_err_t ret = ESP_OK;
286-
287-
if (message->info.status == ESP_ZB_ZCL_STATUS_SUCCESS) {
288-
switch (message->upgrade_status) {
289-
case ESP_ZB_ZCL_OTA_UPGRADE_STATUS_START:
290-
log_i("Zigbee - OTA upgrade start");
291-
start_time = esp_timer_get_time();
292-
s_ota_partition = esp_ota_get_next_update_partition(NULL);
293-
assert(s_ota_partition);
286+
static uint32_t total_size = 0;
287+
static uint32_t offset = 0;
288+
[[maybe_unused]]
289+
static int64_t start_time = 0;
290+
esp_err_t ret = ESP_OK;
291+
292+
if (message->info.status == ESP_ZB_ZCL_STATUS_SUCCESS) {
293+
switch (message->upgrade_status) {
294+
case ESP_ZB_ZCL_OTA_UPGRADE_STATUS_START:
295+
log_i("Zigbee - OTA upgrade start");
296+
start_time = esp_timer_get_time();
297+
s_ota_partition = esp_ota_get_next_update_partition(NULL);
298+
assert(s_ota_partition);
294299
#if CONFIG_ZB_DELTA_OTA
295-
ret = esp_delta_ota_begin(s_ota_partition, 0, &s_ota_handle);
300+
ret = esp_delta_ota_begin(s_ota_partition, 0, &s_ota_handle);
296301
#else
297-
ret = esp_ota_begin(s_ota_partition, 0, &s_ota_handle);
302+
ret = esp_ota_begin(s_ota_partition, 0, &s_ota_handle);
298303
#endif
299-
if(ret != ESP_OK) {
300-
log_e("Zigbee - Failed to begin OTA partition, status: %s", esp_err_to_name(ret));
301-
return ret;
302-
}
303-
break;
304-
case ESP_ZB_ZCL_OTA_UPGRADE_STATUS_RECEIVE:
305-
total_size = message->ota_header.image_size;
306-
offset += message->payload_size;
307-
log_i("Zigbee - OTA Client receives data: progress [%ld/%ld]", offset, total_size);
308-
if (message->payload_size && message->payload) {
309-
uint16_t payload_size = 0;
310-
void *payload = NULL;
311-
ret = esp_element_ota_data(total_size, message->payload, message->payload_size, &payload, &payload_size);
312-
if(ret != ESP_OK) {
313-
log_e("Zigbee - Failed to element OTA data, status: %s", esp_err_to_name(ret));
314-
return ret;
315-
}
304+
if(ret != ESP_OK) {
305+
log_e("Zigbee - Failed to begin OTA partition, status: %s", esp_err_to_name(ret));
306+
return ret;
307+
}
308+
break;
309+
case ESP_ZB_ZCL_OTA_UPGRADE_STATUS_RECEIVE:
310+
total_size = message->ota_header.image_size;
311+
offset += message->payload_size;
312+
log_i("Zigbee - OTA Client receives data: progress [%ld/%ld]", offset, total_size);
313+
if (message->payload_size && message->payload) {
314+
uint16_t payload_size = 0;
315+
void *payload = NULL;
316+
ret = esp_element_ota_data(total_size, message->payload, message->payload_size, &payload, &payload_size);
317+
if(ret != ESP_OK) {
318+
log_e("Zigbee - Failed to element OTA data, status: %s", esp_err_to_name(ret));
319+
return ret;
320+
}
316321
#if CONFIG_ZB_DELTA_OTA
317-
ret = esp_delta_ota_write(s_ota_handle, payload, payload_size);
322+
ret = esp_delta_ota_write(s_ota_handle, payload, payload_size);
318323
#else
319-
ret = esp_ota_write(s_ota_handle, (const void *)payload, payload_size);
324+
ret = esp_ota_write(s_ota_handle, (const void *)payload, payload_size);
320325
#endif
321-
if(ret != ESP_OK) {
322-
log_e("Zigbee - Failed to write OTA data to partition, status: %s", esp_err_to_name(ret));
323-
return ret;
324-
}
325-
}
326-
break;
327-
case ESP_ZB_ZCL_OTA_UPGRADE_STATUS_APPLY:
328-
log_i("Zigbee - OTA upgrade apply");
329-
break;
330-
case ESP_ZB_ZCL_OTA_UPGRADE_STATUS_CHECK:
331-
ret = offset == total_size ? ESP_OK : ESP_FAIL;
332-
offset = 0;
333-
total_size = 0;
334-
s_tagid_received = false;
335-
log_i("Zigbee - OTA upgrade check status: %s", esp_err_to_name(ret));
336-
break;
337-
case ESP_ZB_ZCL_OTA_UPGRADE_STATUS_FINISH:
338-
log_i("Zigbee - OTA Finish");
339-
log_i("Zigbee - OTA Information: version: 0x%lx, manufacturer code: 0x%x, image type: 0x%x, total size: %ld bytes, cost time: %lld ms,",
340-
message->ota_header.file_version, message->ota_header.manufacturer_code, message->ota_header.image_type,
341-
message->ota_header.image_size, (esp_timer_get_time() - start_time) / 1000);
326+
if(ret != ESP_OK) {
327+
log_e("Zigbee - Failed to write OTA data to partition, status: %s", esp_err_to_name(ret));
328+
return ret;
329+
}
330+
}
331+
break;
332+
case ESP_ZB_ZCL_OTA_UPGRADE_STATUS_APPLY:
333+
log_i("Zigbee - OTA upgrade apply");
334+
break;
335+
case ESP_ZB_ZCL_OTA_UPGRADE_STATUS_CHECK:
336+
ret = offset == total_size ? ESP_OK : ESP_FAIL;
337+
offset = 0;
338+
total_size = 0;
339+
s_tagid_received = false;
340+
log_i("Zigbee - OTA upgrade check status: %s", esp_err_to_name(ret));
341+
break;
342+
case ESP_ZB_ZCL_OTA_UPGRADE_STATUS_FINISH:
343+
log_i("Zigbee - OTA Finish");
344+
log_i("Zigbee - OTA Information: version: 0x%lx, manufacturer code: 0x%x, image type: 0x%x, total size: %ld bytes, cost time: %lld ms,",
345+
message->ota_header.file_version, message->ota_header.manufacturer_code, message->ota_header.image_type,
346+
message->ota_header.image_size, (esp_timer_get_time() - start_time) / 1000);
342347
#if CONFIG_ZB_DELTA_OTA
343-
ret = esp_delta_ota_end(s_ota_handle);
348+
ret = esp_delta_ota_end(s_ota_handle);
344349
#else
345-
ret = esp_ota_end(s_ota_handle);
350+
ret = esp_ota_end(s_ota_handle);
346351
#endif
347-
if(ret != ESP_OK) {
348-
log_e("Zigbee - Failed to end OTA partition, status: %s", esp_err_to_name(ret));
349-
return ret;
350-
}
351-
ret = esp_ota_set_boot_partition(s_ota_partition);
352-
if(ret != ESP_OK) {
353-
log_e("Zigbee - Failed to set OTA boot partition, status: %s", esp_err_to_name(ret));
354-
return ret;
355-
}
356-
log_w("Zigbee - Prepare to restart system");
357-
esp_restart();
358-
break;
359-
default:
360-
log_i("Zigbee - OTA status: %d", message->upgrade_status);
361-
break;
362-
}
352+
if(ret != ESP_OK) {
353+
log_e("Zigbee - Failed to end OTA partition, status: %s", esp_err_to_name(ret));
354+
return ret;
355+
}
356+
ret = esp_ota_set_boot_partition(s_ota_partition);
357+
if(ret != ESP_OK) {
358+
log_e("Zigbee - Failed to set OTA boot partition, status: %s", esp_err_to_name(ret));
359+
return ret;
360+
}
361+
log_w("Zigbee - Prepare to restart system");
362+
esp_restart();
363+
break;
364+
default:
365+
log_i("Zigbee - OTA status: %d", message->upgrade_status);
366+
break;
363367
}
364-
return ret;
368+
}
369+
return ret;
365370
}
366371

367372
static esp_err_t zb_ota_upgrade_query_image_resp_handler(const esp_zb_zcl_ota_upgrade_query_image_resp_message_t *message)
368373
{
369374
if (message->info.status == ESP_ZB_ZCL_STATUS_SUCCESS) {
370375
log_i("Zigbee - Queried OTA image from address: 0x%04hx, endpoint: %d", message->server_addr.u.short_addr, message->server_endpoint);
371-
log_i("Zigbee - Image version: 0x%lx, manufacturer code: 0x%x, image size: %ld", message->file_version, message->manufacturer_code,
372-
message->image_size);
373-
//TODO: add more contidions to reject OTA image upgrade
376+
log_i("Zigbee - Image version: 0x%lx, manufacturer code: 0x%x, image size: %ld", message->file_version, message->manufacturer_code, message->image_size);
374377
if(message->image_size == 0) {
375378
log_i("Zigbee - Rejecting OTA image upgrade, image size is 0");
376379
return ESP_FAIL;

0 commit comments

Comments
 (0)