Skip to content

Commit

Permalink
allocate single buffer per event
Browse files Browse the repository at this point in the history
  • Loading branch information
256dpi committed Nov 4, 2021
1 parent 5c550cb commit cb095f2
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions esp_mqtt.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ static void *esp_mqtt_read_buffer;
static QueueHandle_t esp_mqtt_event_queue = NULL;

typedef struct {
void *buffer;
lwmqtt_string_t topic;
lwmqtt_message_t message;
} esp_mqtt_event_t;
Expand Down Expand Up @@ -138,25 +139,27 @@ static void esp_mqtt_message_handler(lwmqtt_client_t *client, void *ref, lwmqtt_
// create message
esp_mqtt_event_t evt = {0};

// allocate buffer
evt.buffer = malloc((size_t)topic.len + 1 + msg.payload_len + 1);

// copy topic with additional null termination
evt.topic.len = topic.len;
evt.topic.data = malloc((size_t)topic.len + 1);
evt.topic.data = evt.buffer;
memcpy(evt.topic.data, topic.data, (size_t)topic.len);
evt.topic.data[topic.len] = 0;

// copy message with additional null termination
evt.message.retained = msg.retained;
evt.message.qos = msg.qos;
evt.message.payload_len = msg.payload_len;
evt.message.payload = malloc((size_t)msg.payload_len + 1);
evt.message.payload = evt.buffer + topic.len + 1;
memcpy(evt.message.payload, msg.payload, (size_t)msg.payload_len);
evt.message.payload[msg.payload_len] = 0;

// queue event
if (xQueueSend(esp_mqtt_event_queue, &evt, 0) != pdTRUE) {
ESP_LOGE(ESP_MQTT_LOG_TAG, "xQueueSend: queue is full, dropping message");
free(evt.topic.data);
free(evt.message.payload);
free(evt.buffer);
}
}

Expand All @@ -169,9 +172,8 @@ static void esp_mqtt_dispatch_events() {
esp_mqtt_message_callback(evt.topic.data, evt.message.payload, evt.message.payload_len);
}

// free data
free(evt.topic.data);
free(evt.message.payload);
// free buffer
free(evt.buffer);
}
}

Expand Down

0 comments on commit cb095f2

Please sign in to comment.