@@ -19,8 +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
+ _read_model[ 0 ] = ' \0 ' ;
23
+ _read_manufacturer[ 0 ] = ' \0 ' ;
24
24
_time_status = 0 ;
25
25
if (!lock) {
26
26
lock = xSemaphoreCreateBinary ();
@@ -38,7 +38,7 @@ bool ZigbeeEP::setManufacturerAndModel(const char *name, const char *model) {
38
38
// Convert manufacturer to ZCL string
39
39
size_t name_length = strlen (name);
40
40
size_t model_length = strlen (model);
41
- if (name_length > 32 || model_length > 32 ) {
41
+ if (name_length > ZB_MAX_NAME_LENGTH || model_length > ZB_MAX_NAME_LENGTH ) {
42
42
log_e (" Manufacturer or model name is too long" );
43
43
return false ;
44
44
}
@@ -49,15 +49,8 @@ bool ZigbeeEP::setManufacturerAndModel(const char *name, const char *model) {
49
49
return false ;
50
50
}
51
51
// Allocate a new array of size length + 2 (1 for the length, 1 for null terminator)
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
- }
52
+ char zb_name[ZB_MAX_NAME_LENGTH + 2 ];
53
+ char zb_model[ZB_MAX_NAME_LENGTH + 2 ];
61
54
// Store the length as the first element
62
55
zb_name[0 ] = static_cast <char >(name_length); // Cast size_t to char
63
56
zb_model[0 ] = static_cast <char >(model_length);
@@ -76,8 +69,6 @@ bool ZigbeeEP::setManufacturerAndModel(const char *name, const char *model) {
76
69
if (ret_model != ESP_OK) {
77
70
log_e (" Failed to set model: 0x%x: %s" , ret_model, esp_err_to_name (ret_model));
78
71
}
79
- free (zb_model);
80
- free (zb_name);
81
72
return ret_name == ESP_OK && ret_model == ESP_OK;
82
73
}
83
74
@@ -176,8 +167,7 @@ char *ZigbeeEP::readManufacturer(uint8_t endpoint, uint16_t short_addr, esp_zb_i
176
167
read_req.attr_number = ZB_ARRAY_LENTH (attributes);
177
168
read_req.attr_field = attributes;
178
169
179
- free (_read_manufacturer);
180
- _read_manufacturer = nullptr ;
170
+ _read_manufacturer[0 ] = ' \0 ' ;
181
171
182
172
esp_zb_lock_acquire (portMAX_DELAY);
183
173
esp_zb_zcl_read_attr_cmd_req (&read_req);
@@ -212,8 +202,7 @@ char *ZigbeeEP::readModel(uint8_t endpoint, uint16_t short_addr, esp_zb_ieee_add
212
202
read_req.attr_number = ZB_ARRAY_LENTH (attributes);
213
203
read_req.attr_field = attributes;
214
204
215
- free (_read_model);
216
- _read_model = nullptr ;
205
+ _read_model[0 ] = ' \0 ' ;
217
206
218
207
esp_zb_lock_acquire (portMAX_DELAY);
219
208
esp_zb_zcl_read_attr_cmd_req (&read_req);
@@ -254,30 +243,16 @@ void ZigbeeEP::zbReadBasicCluster(const esp_zb_zcl_attribute_t *attribute) {
254
243
/* Basic cluster attributes */
255
244
if (attribute->id == ESP_ZB_ZCL_ATTR_BASIC_MANUFACTURER_NAME_ID && attribute->data .type == ESP_ZB_ZCL_ATTR_TYPE_CHAR_STRING && attribute->data .value ) {
256
245
zbstring_t *zbstr = (zbstring_t *)attribute->data .value ;
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 );
264
- zb_manufacturer[zbstr->len ] = ' \0 ' ;
246
+ memcpy (_read_manufacturer, zbstr->data , zbstr->len );
247
+ _read_manufacturer[zbstr->len ] = ' \0 ' ;
265
248
log_i (" Peer Manufacturer is \" %s\" " , zb_manufacturer);
266
- _read_manufacturer = zb_manufacturer;
267
249
xSemaphoreGive (lock);
268
250
}
269
251
if (attribute->id == ESP_ZB_ZCL_ATTR_BASIC_MODEL_IDENTIFIER_ID && attribute->data .type == ESP_ZB_ZCL_ATTR_TYPE_CHAR_STRING && attribute->data .value ) {
270
252
zbstring_t *zbstr = (zbstring_t *)attribute->data .value ;
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 );
278
- zb_model[zbstr->len ] = ' \0 ' ;
253
+ memcpy (_read_model, zbstr->data , zbstr->len );
254
+ _read_model[zbstr->len ] = ' \0 ' ;
279
255
log_i (" Peer Model is \" %s\" " , zb_model);
280
- _read_model = zb_model;
281
256
xSemaphoreGive (lock);
282
257
}
283
258
}
0 commit comments