Skip to content

Commit

Permalink
use pointer for events but keep single allocation
Browse files Browse the repository at this point in the history
  • Loading branch information
256dpi committed Nov 9, 2021
1 parent cb095f2 commit 55bf4af
Showing 1 changed file with 20 additions and 20 deletions.
40 changes: 20 additions & 20 deletions esp_mqtt.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ void esp_mqtt_init(esp_mqtt_status_callback_t scb, esp_mqtt_message_callback_t m
esp_mqtt_select_mutex = xSemaphoreCreateMutex();

// create queue
esp_mqtt_event_queue = xQueueCreate(CONFIG_ESP_MQTT_EVENT_QUEUE_SIZE, sizeof(esp_mqtt_event_t));
esp_mqtt_event_queue = xQueueCreate(CONFIG_ESP_MQTT_EVENT_QUEUE_SIZE, sizeof(esp_mqtt_event_t *));
}

#if defined(CONFIG_ESP_MQTT_TLS_ENABLE)
Expand Down Expand Up @@ -136,44 +136,44 @@ bool esp_mqtt_tls(bool enable, bool verify, const uint8_t *ca_buf, size_t ca_len
#endif

static void esp_mqtt_message_handler(lwmqtt_client_t *client, void *ref, lwmqtt_string_t topic, lwmqtt_message_t msg) {
// create message
esp_mqtt_event_t evt = {0};

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

// prepare message
esp_mqtt_event_t *evt = buffer;

// copy topic with additional null termination
evt.topic.len = topic.len;
evt.topic.data = evt.buffer;
memcpy(evt.topic.data, topic.data, (size_t)topic.len);
evt.topic.data[topic.len] = 0;
evt->topic.len = topic.len;
evt->topic.data = buffer + sizeof(esp_mqtt_event_t);
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 = evt.buffer + topic.len + 1;
memcpy(evt.message.payload, msg.payload, (size_t)msg.payload_len);
evt.message.payload[msg.payload_len] = 0;
evt->message.retained = msg.retained;
evt->message.qos = msg.qos;
evt->message.payload_len = msg.payload_len;
evt->message.payload = buffer + sizeof(esp_mqtt_event_t) + (size_t)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.buffer);
free(evt);
}
}

static void esp_mqtt_dispatch_events() {
// receive next event
esp_mqtt_event_t evt = {0};
esp_mqtt_event_t *evt = 0;
while (xQueueReceive(esp_mqtt_event_queue, &evt, 0) == pdTRUE) {
// call callback if existing
if (esp_mqtt_message_callback) {
esp_mqtt_message_callback(evt.topic.data, evt.message.payload, evt.message.payload_len);
esp_mqtt_message_callback(evt->topic.data, evt->message.payload, evt->message.payload_len);
}

// free buffer
free(evt.buffer);
// free event
free(evt);
}
}

Expand Down

0 comments on commit 55bf4af

Please sign in to comment.