@@ -19,6 +19,8 @@ ZigbeeEP::ZigbeeEP(uint8_t endpoint) {
19
19
_ep_config.endpoint = 0 ;
20
20
_cluster_list = nullptr ;
21
21
_on_identify = nullptr ;
22
+ _read_model = NULL ;
23
+ _read_manufacturer = NULL ;
22
24
_time_status = 0 ;
23
25
if (!lock) {
24
26
lock = xSemaphoreCreateBinary ();
@@ -33,16 +35,23 @@ void ZigbeeEP::setVersion(uint8_t version) {
33
35
}
34
36
35
37
bool ZigbeeEP::setManufacturerAndModel (const char *name, const char *model) {
38
+ // Allocate a new array of size length + 2 (1 for the length, 1 for null terminator)
39
+ char zb_name[ZB_MAX_NAME_LENGTH + 2 ];
40
+ char zb_model[ZB_MAX_NAME_LENGTH + 2 ];
41
+
36
42
// Convert manufacturer to ZCL string
37
43
size_t name_length = strlen (name);
38
44
size_t model_length = strlen (model);
39
- if (name_length > 32 || model_length > 32 ) {
45
+ if (name_length > ZB_MAX_NAME_LENGTH || model_length > ZB_MAX_NAME_LENGTH ) {
40
46
log_e (" Manufacturer or model name is too long" );
41
47
return false ;
42
48
}
43
- // Allocate a new array of size length + 2 (1 for the length, 1 for null terminator)
44
- char *zb_name = new char [name_length + 2 ];
45
- char *zb_model = new char [model_length + 2 ];
49
+ // Get and check the basic cluster
50
+ 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);
51
+ if (basic_cluster == nullptr ) {
52
+ log_e (" Failed to get basic cluster" );
53
+ return false ;
54
+ }
46
55
// Store the length as the first element
47
56
zb_name[0 ] = static_cast <char >(name_length); // Cast size_t to char
48
57
zb_model[0 ] = static_cast <char >(model_length);
@@ -52,9 +61,7 @@ bool ZigbeeEP::setManufacturerAndModel(const char *name, const char *model) {
52
61
// Null-terminate the array
53
62
zb_name[name_length + 1 ] = ' \0 ' ;
54
63
zb_model[model_length + 1 ] = ' \0 ' ;
55
-
56
- // Get the basic cluster and update the manufacturer and model attributes
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);
64
+ // Update the manufacturer and model attributes
58
65
esp_err_t ret_name = esp_zb_basic_cluster_add_attr (basic_cluster, ESP_ZB_ZCL_ATTR_BASIC_MANUFACTURER_NAME_ID, (void *)zb_name);
59
66
if (ret_name != ESP_OK) {
60
67
log_e (" Failed to set manufacturer: 0x%x: %s" , ret_name, esp_err_to_name (ret_name));
@@ -63,8 +70,6 @@ bool ZigbeeEP::setManufacturerAndModel(const char *name, const char *model) {
63
70
if (ret_model != ESP_OK) {
64
71
log_e (" Failed to set model: 0x%x: %s" , ret_model, esp_err_to_name (ret_model));
65
72
}
66
- delete[] zb_name;
67
- delete[] zb_model;
68
73
return ret_name == ESP_OK && ret_model == ESP_OK;
69
74
}
70
75
@@ -163,10 +168,10 @@ char *ZigbeeEP::readManufacturer(uint8_t endpoint, uint16_t short_addr, esp_zb_i
163
168
read_req.attr_number = ZB_ARRAY_LENTH (attributes);
164
169
read_req.attr_field = attributes;
165
170
166
- if (_read_manufacturer != nullptr ) {
171
+ if (_read_manufacturer != NULL ) {
167
172
free (_read_manufacturer);
168
173
}
169
- _read_manufacturer = nullptr ;
174
+ _read_manufacturer = NULL ;
170
175
171
176
esp_zb_lock_acquire (portMAX_DELAY);
172
177
esp_zb_zcl_read_attr_cmd_req (&read_req);
@@ -201,10 +206,10 @@ char *ZigbeeEP::readModel(uint8_t endpoint, uint16_t short_addr, esp_zb_ieee_add
201
206
read_req.attr_number = ZB_ARRAY_LENTH (attributes);
202
207
read_req.attr_field = attributes;
203
208
204
- if (_read_model != nullptr ) {
209
+ if (_read_model != NULL ) {
205
210
free (_read_model);
206
211
}
207
- _read_model = nullptr ;
212
+ _read_model = NULL ;
208
213
209
214
esp_zb_lock_acquire (portMAX_DELAY);
210
215
esp_zb_zcl_read_attr_cmd_req (&read_req);
@@ -245,20 +250,28 @@ void ZigbeeEP::zbReadBasicCluster(const esp_zb_zcl_attribute_t *attribute) {
245
250
/* Basic cluster attributes */
246
251
if (attribute->id == ESP_ZB_ZCL_ATTR_BASIC_MANUFACTURER_NAME_ID && attribute->data .type == ESP_ZB_ZCL_ATTR_TYPE_CHAR_STRING && attribute->data .value ) {
247
252
zbstring_t *zbstr = (zbstring_t *)attribute->data .value ;
248
- char *string = (char *)malloc (zbstr->len + 1 );
249
- memcpy (string, zbstr->data , zbstr->len );
250
- string[zbstr->len ] = ' \0 ' ;
251
- log_i (" Peer Manufacturer is \" %s\" " , string);
252
- _read_manufacturer = string;
253
+ _read_manufacturer = (char *)malloc (zbstr->len + 1 );
254
+ if (_read_manufacturer == NULL ) {
255
+ log_e (" Failed to allocate memory for manufacturer data" );
256
+ xSemaphoreGive (lock);
257
+ return ;
258
+ }
259
+ memcpy (_read_manufacturer, zbstr->data , zbstr->len );
260
+ _read_manufacturer[zbstr->len ] = ' \0 ' ;
261
+ log_i (" Peer Manufacturer is \" %s\" " , _read_manufacturer);
253
262
xSemaphoreGive (lock);
254
263
}
255
264
if (attribute->id == ESP_ZB_ZCL_ATTR_BASIC_MODEL_IDENTIFIER_ID && attribute->data .type == ESP_ZB_ZCL_ATTR_TYPE_CHAR_STRING && attribute->data .value ) {
256
265
zbstring_t *zbstr = (zbstring_t *)attribute->data .value ;
257
- char *string = (char *)malloc (zbstr->len + 1 );
258
- memcpy (string, zbstr->data , zbstr->len );
259
- string[zbstr->len ] = ' \0 ' ;
260
- log_i (" Peer Model is \" %s\" " , string);
261
- _read_model = string;
266
+ _read_model = (char *)malloc (zbstr->len + 1 );
267
+ if (_read_model == NULL ) {
268
+ log_e (" Failed to allocate memory for model data" );
269
+ xSemaphoreGive (lock);
270
+ return ;
271
+ }
272
+ memcpy (_read_model, zbstr->data , zbstr->len );
273
+ _read_model[zbstr->len ] = ' \0 ' ;
274
+ log_i (" Peer Model is \" %s\" " , _read_model);
262
275
xSemaphoreGive (lock);
263
276
}
264
277
}
0 commit comments