@@ -34,41 +34,36 @@ void ZigbeeEP::setVersion(uint8_t version) {
34
34
35
35
bool ZigbeeEP::setManufacturerAndModel (const char *name, const char *model) {
36
36
// Convert manufacturer to ZCL string
37
- size_t length = strlen (name);
38
- if (length > 32 ) {
39
- log_e (" Manufacturer name is too long" );
37
+ size_t name_length = strlen (name);
38
+ size_t model_length = strlen (model);
39
+ if (name_length > 32 || model_length > 32 ) {
40
+ log_e (" Manufacturer or model name is too long" );
40
41
return false ;
41
42
}
42
43
// Allocate a new array of size length + 2 (1 for the length, 1 for null terminator)
43
- char *zb_name = new char [length + 2 ];
44
+ char *zb_name = new char [name_length + 2 ];
45
+ char *zb_model = new char [model_length + 2 ];
44
46
// Store the length as the first element
45
- zb_name[0 ] = static_cast <char >(length); // Cast size_t to char
47
+ zb_name[0 ] = static_cast <char >(name_length); // Cast size_t to char
48
+ zb_model[0 ] = static_cast <char >(model_length);
46
49
// Use memcpy to copy the characters to the result array
47
- memcpy (zb_name + 1 , name, length);
50
+ memcpy (zb_name + 1 , name, name_length);
51
+ memcpy (zb_model + 1 , model, model_length);
48
52
// Null-terminate the array
49
- zb_name[length + 1 ] = ' \0 ' ;
50
-
51
- // Convert model to ZCL string
52
- length = strlen (model);
53
- if (length > 32 ) {
54
- log_e (" Model name is too long" );
55
- delete[] zb_name;
56
- return false ;
57
- }
58
- char *zb_model = new char [length + 2 ];
59
- zb_model[0 ] = static_cast <char >(length);
60
- memcpy (zb_model + 1 , model, length);
61
- zb_model[length + 1 ] = ' \0 ' ;
53
+ zb_name[name_length + 1 ] = ' \0 ' ;
54
+ zb_model[model_length + 1 ] = ' \0 ' ;
62
55
63
56
// Get the basic cluster and update the manufacturer and model attributes
64
57
esp_zb_attribute_list_t *basic_cluster = esp_zb_cluster_list_get_cluster (_cluster_list, ESP_ZB_ZCL_CLUSTER_ID_BASIC, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE);
65
58
esp_err_t ret_manufacturer = esp_zb_basic_cluster_add_attr (basic_cluster, ESP_ZB_ZCL_ATTR_BASIC_MANUFACTURER_NAME_ID, (void *)zb_name);
66
59
esp_err_t ret_model = esp_zb_basic_cluster_add_attr (basic_cluster, ESP_ZB_ZCL_ATTR_BASIC_MODEL_IDENTIFIER_ID, (void *)zb_model);
67
60
if (ret_manufacturer != ESP_OK || ret_model != ESP_OK) {
68
61
log_e (" Failed to set manufacturer (0x%x) or model (0x%x)" , ret_manufacturer, ret_model);
69
- return false ;
70
62
}
71
- return true ;
63
+
64
+ delete[] zb_name;
65
+ delete[] zb_model;
66
+ return ret_manufacturer == ESP_OK && ret_model == ESP_OK;
72
67
}
73
68
74
69
void ZigbeeEP::setPowerSource (zb_power_source_t power_source, uint8_t battery_percentage) {
@@ -103,7 +98,7 @@ bool ZigbeeEP::setBatteryPercentage(uint8_t percentage) {
103
98
);
104
99
esp_zb_lock_release ();
105
100
if (ret != ESP_ZB_ZCL_STATUS_SUCCESS) {
106
- log_e (" Failed to set battery percentage: 0x%x" , ret);
101
+ log_e (" Failed to set battery percentage: 0x%x: %s " , ret, esp_zb_zcl_status_to_name (ret) );
107
102
return false ;
108
103
}
109
104
log_v (" Battery percentage updated" );
@@ -153,7 +148,9 @@ char *ZigbeeEP::readManufacturer(uint8_t endpoint, uint16_t short_addr, esp_zb_i
153
148
read_req.attr_number = ZB_ARRAY_LENTH (attributes);
154
149
read_req.attr_field = attributes;
155
150
156
- // clear read manufacturer
151
+ if (_read_manufacturer != nullptr ) {
152
+ free (_read_manufacturer);
153
+ }
157
154
_read_manufacturer = nullptr ;
158
155
159
156
esp_zb_lock_acquire (portMAX_DELAY);
@@ -189,7 +186,9 @@ char *ZigbeeEP::readModel(uint8_t endpoint, uint16_t short_addr, esp_zb_ieee_add
189
186
read_req.attr_number = ZB_ARRAY_LENTH (attributes);
190
187
read_req.attr_field = attributes;
191
188
192
- // clear read model
189
+ if (_read_model != nullptr ) {
190
+ free (_read_model);
191
+ }
193
192
_read_model = nullptr ;
194
193
195
194
esp_zb_lock_acquire (portMAX_DELAY);
@@ -287,7 +286,7 @@ bool ZigbeeEP::setTime(tm time) {
287
286
ret = esp_zb_zcl_set_attribute_val (_endpoint, ESP_ZB_ZCL_CLUSTER_ID_TIME, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE, ESP_ZB_ZCL_ATTR_TIME_TIME_ID, &utc_time, false );
288
287
esp_zb_lock_release ();
289
288
if (ret != ESP_ZB_ZCL_STATUS_SUCCESS) {
290
- log_e (" Failed to set time: 0x%x" , ret);
289
+ log_e (" Failed to set time: 0x%x: %s " , ret, esp_zb_zcl_status_to_name (ret) );
291
290
return false ;
292
291
}
293
292
return true ;
@@ -300,7 +299,7 @@ bool ZigbeeEP::setTimezone(int32_t gmt_offset) {
300
299
ret = esp_zb_zcl_set_attribute_val (_endpoint, ESP_ZB_ZCL_CLUSTER_ID_TIME, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE, ESP_ZB_ZCL_ATTR_TIME_TIME_ZONE_ID, &gmt_offset, false );
301
300
esp_zb_lock_release ();
302
301
if (ret != ESP_ZB_ZCL_STATUS_SUCCESS) {
303
- log_e (" Failed to set timezone: 0x%x" , ret);
302
+ log_e (" Failed to set timezone: 0x%x: %s " , ret, esp_zb_zcl_status_to_name (ret) );
304
303
return false ;
305
304
}
306
305
return true ;
@@ -422,7 +421,7 @@ void ZigbeeEP::zbReadTimeCluster(const esp_zb_zcl_attribute_t *attribute) {
422
421
// uint8_t max_data_size; /*!< The maximum size of OTA data */
423
422
// } esp_zb_zcl_ota_upgrade_client_variable_t;
424
423
425
- void ZigbeeEP::addOTAClient (
424
+ bool ZigbeeEP::addOTAClient (
426
425
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
427
426
) {
428
427
@@ -442,11 +441,23 @@ void ZigbeeEP::addOTAClient(
442
441
uint16_t ota_upgrade_server_addr = 0xffff ;
443
442
uint8_t ota_upgrade_server_ep = 0xff ;
444
443
445
- esp_zb_ota_cluster_add_attr (ota_cluster, ESP_ZB_ZCL_ATTR_OTA_UPGRADE_CLIENT_DATA_ID, (void *)&variable_config);
446
- esp_zb_ota_cluster_add_attr (ota_cluster, ESP_ZB_ZCL_ATTR_OTA_UPGRADE_SERVER_ADDR_ID, (void *)&ota_upgrade_server_addr);
447
- esp_zb_ota_cluster_add_attr (ota_cluster, ESP_ZB_ZCL_ATTR_OTA_UPGRADE_SERVER_ENDPOINT_ID, (void *)&ota_upgrade_server_ep);
448
-
444
+ esp_err_t ret = esp_zb_ota_cluster_add_attr (ota_cluster, ESP_ZB_ZCL_ATTR_OTA_UPGRADE_CLIENT_DATA_ID, (void *)&variable_config);
445
+ if (ret != ESP_OK) {
446
+ log_e (" Failed to add OTA client data: 0x%x: %s" , ret, esp_err_to_name (ret));
447
+ return false ;
448
+ }
449
+ ret = esp_zb_ota_cluster_add_attr (ota_cluster, ESP_ZB_ZCL_ATTR_OTA_UPGRADE_SERVER_ADDR_ID, (void *)&ota_upgrade_server_addr);
450
+ if (ret != ESP_OK) {
451
+ log_e (" Failed to add OTA server address: 0x%x: %s" , ret, esp_err_to_name (ret));
452
+ return false ;
453
+ }
454
+ ret = esp_zb_ota_cluster_add_attr (ota_cluster, ESP_ZB_ZCL_ATTR_OTA_UPGRADE_SERVER_ENDPOINT_ID, (void *)&ota_upgrade_server_ep);
455
+ if (ret != ESP_OK) {
456
+ log_e (" Failed to add OTA server endpoint: 0x%x: %s" , ret, esp_err_to_name (ret));
457
+ return false ;
458
+ }
449
459
esp_zb_cluster_list_add_ota_cluster (_cluster_list, ota_cluster, ESP_ZB_ZCL_CLUSTER_CLIENT_ROLE);
460
+ return true ;
450
461
}
451
462
452
463
static void findOTAServer (esp_zb_zdp_status_t zdo_status, uint16_t addr, uint8_t endpoint, void *user_ctx) {
@@ -477,4 +488,76 @@ void ZigbeeEP::requestOTAUpdate() {
477
488
esp_zb_lock_release ();
478
489
}
479
490
491
+ const char * ZigbeeEP::esp_zb_zcl_status_to_name (esp_zb_zcl_status_t status) {
492
+ switch (status) {
493
+ case ESP_ZB_ZCL_STATUS_SUCCESS:
494
+ return " Success" ;
495
+ case ESP_ZB_ZCL_STATUS_FAIL:
496
+ return " Fail" ;
497
+ case ESP_ZB_ZCL_STATUS_NOT_AUTHORIZED:
498
+ return " Not authorized" ;
499
+ case ESP_ZB_ZCL_STATUS_MALFORMED_CMD:
500
+ return " Malformed command" ;
501
+ case ESP_ZB_ZCL_STATUS_UNSUP_CLUST_CMD:
502
+ return " Unsupported cluster command" ;
503
+ case ESP_ZB_ZCL_STATUS_UNSUP_GEN_CMD:
504
+ return " Unsupported general command" ;
505
+ case ESP_ZB_ZCL_STATUS_UNSUP_MANUF_CLUST_CMD:
506
+ return " Unsupported manufacturer cluster command" ;
507
+ case ESP_ZB_ZCL_STATUS_UNSUP_MANUF_GEN_CMD:
508
+ return " Unsupported manufacturer general command" ;
509
+ case ESP_ZB_ZCL_STATUS_INVALID_FIELD:
510
+ return " Invalid field" ;
511
+ case ESP_ZB_ZCL_STATUS_UNSUP_ATTRIB:
512
+ return " Unsupported attribute" ;
513
+ case ESP_ZB_ZCL_STATUS_INVALID_VALUE:
514
+ return " Invalid value" ;
515
+ case ESP_ZB_ZCL_STATUS_READ_ONLY:
516
+ return " Read only" ;
517
+ case ESP_ZB_ZCL_STATUS_INSUFF_SPACE:
518
+ return " Insufficient space" ;
519
+ case ESP_ZB_ZCL_STATUS_DUPE_EXISTS:
520
+ return " Duplicate exists" ;
521
+ case ESP_ZB_ZCL_STATUS_NOT_FOUND:
522
+ return " Not found" ;
523
+ case ESP_ZB_ZCL_STATUS_UNREPORTABLE_ATTRIB:
524
+ return " Unreportable attribute" ;
525
+ case ESP_ZB_ZCL_STATUS_INVALID_TYPE:
526
+ return " Invalid type" ;
527
+ case ESP_ZB_ZCL_STATUS_WRITE_ONLY:
528
+ return " Write only" ;
529
+ case ESP_ZB_ZCL_STATUS_INCONSISTENT:
530
+ return " Inconsistent" ;
531
+ case ESP_ZB_ZCL_STATUS_ACTION_DENIED:
532
+ return " Action denied" ;
533
+ case ESP_ZB_ZCL_STATUS_TIMEOUT:
534
+ return " Timeout" ;
535
+ case ESP_ZB_ZCL_STATUS_ABORT:
536
+ return " Abort" ;
537
+ case ESP_ZB_ZCL_STATUS_INVALID_IMAGE:
538
+ return " Invalid OTA upgrade image" ;
539
+ case ESP_ZB_ZCL_STATUS_WAIT_FOR_DATA:
540
+ return " Server does not have data block available yet" ;
541
+ case ESP_ZB_ZCL_STATUS_NO_IMAGE_AVAILABLE:
542
+ return " No image available" ;
543
+ case ESP_ZB_ZCL_STATUS_REQUIRE_MORE_IMAGE:
544
+ return " Require more image" ;
545
+ case ESP_ZB_ZCL_STATUS_NOTIFICATION_PENDING:
546
+ return " Notification pending" ;
547
+ case ESP_ZB_ZCL_STATUS_HW_FAIL:
548
+ return " Hardware failure" ;
549
+ case ESP_ZB_ZCL_STATUS_SW_FAIL:
550
+ return " Software failure" ;
551
+ case ESP_ZB_ZCL_STATUS_CALIB_ERR:
552
+ return " Calibration error" ;
553
+ case ESP_ZB_ZCL_STATUS_UNSUP_CLUST:
554
+ return " Cluster is not found on the target endpoint" ;
555
+ case ESP_ZB_ZCL_STATUS_LIMIT_REACHED:
556
+ return " Limit reached" ;
557
+ default :
558
+ return " Unknown status" ;
559
+ }
560
+ }
561
+
562
+
480
563
#endif // CONFIG_ZB_ENABLED
0 commit comments