Skip to content

Commit bf3e5c1

Browse files
authored
feat(zigbee_ep): changed model and manufacturer to heap
1 parent 49d983a commit bf3e5c1

File tree

1 file changed

+11
-36
lines changed

1 file changed

+11
-36
lines changed

libraries/Zigbee/src/ZigbeeEP.cpp

+11-36
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ ZigbeeEP::ZigbeeEP(uint8_t endpoint) {
1919
_ep_config.endpoint = 0;
2020
_cluster_list = nullptr;
2121
_on_identify = nullptr;
22-
_read_model = nullptr;
23-
_read_manufacturer = nullptr;
22+
_read_model[0] = '\0';
23+
_read_manufacturer[0] = '\0';
2424
_time_status = 0;
2525
if (!lock) {
2626
lock = xSemaphoreCreateBinary();
@@ -38,7 +38,7 @@ bool ZigbeeEP::setManufacturerAndModel(const char *name, const char *model) {
3838
// Convert manufacturer to ZCL string
3939
size_t name_length = strlen(name);
4040
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) {
4242
log_e("Manufacturer or model name is too long");
4343
return false;
4444
}
@@ -49,15 +49,8 @@ bool ZigbeeEP::setManufacturerAndModel(const char *name, const char *model) {
4949
return false;
5050
}
5151
// 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];
6154
// Store the length as the first element
6255
zb_name[0] = static_cast<char>(name_length); // Cast size_t to char
6356
zb_model[0] = static_cast<char>(model_length);
@@ -76,8 +69,6 @@ bool ZigbeeEP::setManufacturerAndModel(const char *name, const char *model) {
7669
if (ret_model != ESP_OK) {
7770
log_e("Failed to set model: 0x%x: %s", ret_model, esp_err_to_name(ret_model));
7871
}
79-
free(zb_model);
80-
free(zb_name);
8172
return ret_name == ESP_OK && ret_model == ESP_OK;
8273
}
8374

@@ -176,8 +167,7 @@ char *ZigbeeEP::readManufacturer(uint8_t endpoint, uint16_t short_addr, esp_zb_i
176167
read_req.attr_number = ZB_ARRAY_LENTH(attributes);
177168
read_req.attr_field = attributes;
178169

179-
free(_read_manufacturer);
180-
_read_manufacturer = nullptr;
170+
_read_manufacturer[0] = '\0';
181171

182172
esp_zb_lock_acquire(portMAX_DELAY);
183173
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
212202
read_req.attr_number = ZB_ARRAY_LENTH(attributes);
213203
read_req.attr_field = attributes;
214204

215-
free(_read_model);
216-
_read_model = nullptr;
205+
_read_model[0] = '\0';
217206

218207
esp_zb_lock_acquire(portMAX_DELAY);
219208
esp_zb_zcl_read_attr_cmd_req(&read_req);
@@ -254,30 +243,16 @@ void ZigbeeEP::zbReadBasicCluster(const esp_zb_zcl_attribute_t *attribute) {
254243
/* Basic cluster attributes */
255244
if (attribute->id == ESP_ZB_ZCL_ATTR_BASIC_MANUFACTURER_NAME_ID && attribute->data.type == ESP_ZB_ZCL_ATTR_TYPE_CHAR_STRING && attribute->data.value) {
256245
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';
265248
log_i("Peer Manufacturer is \"%s\"", zb_manufacturer);
266-
_read_manufacturer = zb_manufacturer;
267249
xSemaphoreGive(lock);
268250
}
269251
if (attribute->id == ESP_ZB_ZCL_ATTR_BASIC_MODEL_IDENTIFIER_ID && attribute->data.type == ESP_ZB_ZCL_ATTR_TYPE_CHAR_STRING && attribute->data.value) {
270252
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';
279255
log_i("Peer Model is \"%s\"", zb_model);
280-
_read_model = zb_model;
281256
xSemaphoreGive(lock);
282257
}
283258
}

0 commit comments

Comments
 (0)