@@ -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 = nullptr ;
23
+ _read_manufacturer = nullptr ;
22
24
_time_status = 0 ;
23
25
if (!lock) {
24
26
lock = xSemaphoreCreateBinary ();
@@ -40,9 +42,22 @@ bool ZigbeeEP::setManufacturerAndModel(const char *name, const char *model) {
40
42
log_e (" Manufacturer or model name is too long" );
41
43
return false ;
42
44
}
45
+ // Get and check the basic cluster
46
+ 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);
47
+ if (basic_cluster == nullptr ) {
48
+ log_e (" Failed to get basic cluster" );
49
+ return false ;
50
+ }
43
51
// 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 ];
52
+ char *zb_name = (char *) malloc (name_length + 2 );
53
+ char *zb_model = (char *) malloc (model_length + 2 );
54
+ if (zb_name == nullptr || zb_model == nullptr ) {
55
+ log_e (" Failed to allocate memory for name and model data" );
56
+ // make sure any allocated memory is returned to heap
57
+ free (zb_name);
58
+ free (zb_model);
59
+ return false ;
60
+ }
46
61
// Store the length as the first element
47
62
zb_name[0 ] = static_cast <char >(name_length); // Cast size_t to char
48
63
zb_model[0 ] = static_cast <char >(model_length);
@@ -52,9 +67,7 @@ bool ZigbeeEP::setManufacturerAndModel(const char *name, const char *model) {
52
67
// Null-terminate the array
53
68
zb_name[name_length + 1 ] = ' \0 ' ;
54
69
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);
70
+ // Update the manufacturer and model attributes
58
71
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
72
if (ret_name != ESP_OK) {
60
73
log_e (" Failed to set manufacturer: 0x%x: %s" , ret_name, esp_err_to_name (ret_name));
@@ -63,8 +76,8 @@ bool ZigbeeEP::setManufacturerAndModel(const char *name, const char *model) {
63
76
if (ret_model != ESP_OK) {
64
77
log_e (" Failed to set model: 0x%x: %s" , ret_model, esp_err_to_name (ret_model));
65
78
}
66
- delete[] zb_name ;
67
- delete[] zb_model ;
79
+ free (zb_model) ;
80
+ free (zb_name) ;
68
81
return ret_name == ESP_OK && ret_model == ESP_OK;
69
82
}
70
83
@@ -163,9 +176,7 @@ char *ZigbeeEP::readManufacturer(uint8_t endpoint, uint16_t short_addr, esp_zb_i
163
176
read_req.attr_number = ZB_ARRAY_LENTH (attributes);
164
177
read_req.attr_field = attributes;
165
178
166
- if (_read_manufacturer != nullptr ) {
167
- free (_read_manufacturer);
168
- }
179
+ free (_read_manufacturer);
169
180
_read_manufacturer = nullptr ;
170
181
171
182
esp_zb_lock_acquire (portMAX_DELAY);
@@ -201,9 +212,7 @@ char *ZigbeeEP::readModel(uint8_t endpoint, uint16_t short_addr, esp_zb_ieee_add
201
212
read_req.attr_number = ZB_ARRAY_LENTH (attributes);
202
213
read_req.attr_field = attributes;
203
214
204
- if (_read_model != nullptr ) {
205
- free (_read_model);
206
- }
215
+ free (_read_model);
207
216
_read_model = nullptr ;
208
217
209
218
esp_zb_lock_acquire (portMAX_DELAY);
@@ -245,20 +254,30 @@ void ZigbeeEP::zbReadBasicCluster(const esp_zb_zcl_attribute_t *attribute) {
245
254
/* Basic cluster attributes */
246
255
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
256
zbstring_t *zbstr = (zbstring_t *)attribute->data .value ;
248
- char *string = (char *)malloc (zbstr->len + 1 );
249
- memcpy (string, zbstr->data , zbstr->len );
257
+ char *zb_manufacturer = (char *)malloc (zbstr->len + 1 );
258
+ if (zb_manufacturer == nullptr ) {
259
+ log_e (" Failed to allocate memory for manufacturer data" );
260
+ xSemaphoreGive (lock);
261
+ return ;
262
+ }
263
+ memcpy (zb_manufacturer, zbstr->data , zbstr->len );
250
264
string[zbstr->len ] = ' \0 ' ;
251
- log_i (" Peer Manufacturer is \" %s\" " , string );
252
- _read_manufacturer = string ;
265
+ log_i (" Peer Manufacturer is \" %s\" " , zb_manufacturer );
266
+ _read_manufacturer = zb_manufacturer ;
253
267
xSemaphoreGive (lock);
254
268
}
255
269
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
270
zbstring_t *zbstr = (zbstring_t *)attribute->data .value ;
257
- char *string = (char *)malloc (zbstr->len + 1 );
258
- memcpy (string, zbstr->data , zbstr->len );
271
+ char *zb_model = (char *)malloc (zbstr->len + 1 );
272
+ if (zb_model == nullptr ) {
273
+ log_e (" Failed to allocate memory for model data" );
274
+ xSemaphoreGive (lock);
275
+ return ;
276
+ }
277
+ memcpy (zb_model, zbstr->data , zbstr->len );
259
278
string[zbstr->len ] = ' \0 ' ;
260
- log_i (" Peer Model is \" %s\" " , string );
261
- _read_model = string ;
279
+ log_i (" Peer Model is \" %s\" " , zb_model );
280
+ _read_model = zb_model ;
262
281
xSemaphoreGive (lock);
263
282
}
264
283
}
0 commit comments