|
| 1 | +//! @file |
| 2 | +//! |
| 3 | +//! Copyright (c) Memfault, Inc. |
| 4 | +//! See License.txt for details |
| 5 | + |
| 6 | +#include <stddef.h> |
| 7 | +#include <stdint.h> |
| 8 | + |
| 9 | +#include "app_memfault_transport.h" |
| 10 | +#include "esp_log.h" |
| 11 | +#include "memfault/components.h" |
| 12 | +#include "mqtt_client.h" |
| 13 | + |
| 14 | +// TODO: Fill in with device's Memfault project |
| 15 | +#define MEMFAULT_PROJECT "my_project" |
| 16 | + |
| 17 | +static const char *TAG = "app_memfault_transport_mqtt"; |
| 18 | + |
| 19 | +// TODO: Fill in with broker connection configuration |
| 20 | +static esp_mqtt_client_config_t s_mqtt_config = { |
| 21 | + .broker.address.uri = "mqtt://192.168.50.88", |
| 22 | + .credentials.username = "test", |
| 23 | + .credentials.authentication.password = "test1234", |
| 24 | + .session.protocol_ver = MQTT_PROTOCOL_V_5, |
| 25 | +}; |
| 26 | +static SemaphoreHandle_t s_mqtt_connected = NULL; |
| 27 | + |
| 28 | +static esp_mqtt_client_handle_t s_mqtt_client = NULL; |
| 29 | +static esp_mqtt5_publish_property_config_t s_publish_property = { |
| 30 | + .topic_alias = 1, |
| 31 | +}; |
| 32 | +static char s_topic_string[128] = {0}; |
| 33 | + |
| 34 | +static uint8_t s_chunk_data[1024] = {0}; |
| 35 | + |
| 36 | +static void mqtt_event_handler(MEMFAULT_UNUSED void *handler_args, |
| 37 | + MEMFAULT_UNUSED esp_event_base_t base, |
| 38 | + MEMFAULT_UNUSED int32_t event_id, void *event_data) { |
| 39 | + esp_mqtt_event_handle_t event = (esp_mqtt_event_handle_t)event_data; |
| 40 | + |
| 41 | + switch (event->event_id) { |
| 42 | + case MQTT_EVENT_CONNECTED: |
| 43 | + ESP_LOGI(TAG, "Connected to MQTT broker"); |
| 44 | + xSemaphoreGive(s_mqtt_connected); |
| 45 | + break; |
| 46 | + default: |
| 47 | + ESP_LOGE(TAG, "Unknown MQTT event received[%d]", event->event_id); |
| 48 | + break; |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +static void prv_close_client(void) { |
| 53 | + int rv = esp_mqtt_client_disconnect(s_mqtt_client); |
| 54 | + if (rv) { |
| 55 | + ESP_LOGW(TAG, "Failed to disconnect[%d]", rv); |
| 56 | + } |
| 57 | + |
| 58 | + rv = esp_mqtt_client_destroy(s_mqtt_client); |
| 59 | + if (rv) { |
| 60 | + ESP_LOGW(TAG, "Failed to destroy client[%d]", rv); |
| 61 | + } |
| 62 | + memfault_metrics_heartbeat_timer_stop(MEMFAULT_METRICS_KEY(mqtt_conn_uptime)); |
| 63 | + |
| 64 | + s_mqtt_client = NULL; |
| 65 | +} |
| 66 | + |
| 67 | +static int prv_create_client(void) { |
| 68 | + if (s_mqtt_client) { |
| 69 | + return 0; |
| 70 | + } |
| 71 | + |
| 72 | + s_mqtt_client = esp_mqtt_client_init(&s_mqtt_config); |
| 73 | + if (s_mqtt_client == NULL) { |
| 74 | + ESP_LOGE(TAG, "MQTT client failed to initialize"); |
| 75 | + return -1; |
| 76 | + } |
| 77 | + |
| 78 | + int rv = |
| 79 | + esp_mqtt_client_register_event(s_mqtt_client, MQTT_EVENT_CONNECTED, mqtt_event_handler, NULL); |
| 80 | + if (rv) { |
| 81 | + ESP_LOGE(TAG, "MQTT event handler registration failed[%d]", rv); |
| 82 | + } |
| 83 | + |
| 84 | + rv = esp_mqtt_client_start(s_mqtt_client); |
| 85 | + if (rv) { |
| 86 | + ESP_LOGE(TAG, "MQTT client start failed[%d]", rv); |
| 87 | + return -1; |
| 88 | + } |
| 89 | + |
| 90 | + // Wait for successful connection |
| 91 | + rv = xSemaphoreTake(s_mqtt_connected, (1000 * 10) / portTICK_PERIOD_MS); |
| 92 | + if (rv != pdTRUE) { |
| 93 | + ESP_LOGE(TAG, "MQTT client failed to connect[%d]", rv); |
| 94 | + memfault_metrics_heartbeat_timer_start(MEMFAULT_METRICS_KEY(mqtt_conn_downtime)); |
| 95 | + prv_close_client(); |
| 96 | + return -1; |
| 97 | + } |
| 98 | + |
| 99 | + // Update connection metrics when connected |
| 100 | + memfault_metrics_heartbeat_timer_stop(MEMFAULT_METRICS_KEY(mqtt_conn_downtime)); |
| 101 | + memfault_metrics_heartbeat_timer_start(MEMFAULT_METRICS_KEY(mqtt_conn_uptime)); |
| 102 | + |
| 103 | + // Set topic alias before publishing |
| 104 | + rv = esp_mqtt5_client_set_publish_property(s_mqtt_client, &s_publish_property); |
| 105 | + if (rv != 0) { |
| 106 | + ESP_LOGW(TAG, "MQTT client could not set publish property [%d]", rv); |
| 107 | + } |
| 108 | + return 0; |
| 109 | +} |
| 110 | + |
| 111 | +static const char *prv_get_device_serial(void) { |
| 112 | + sMemfaultDeviceInfo info = {0}; |
| 113 | + memfault_platform_get_device_info(&info); |
| 114 | + return info.device_serial; |
| 115 | +} |
| 116 | + |
| 117 | +void prv_build_topic_string(void) { |
| 118 | + // String already built |
| 119 | + if (strlen(s_topic_string) > 0) { |
| 120 | + return; |
| 121 | + } |
| 122 | + |
| 123 | + const char *device_serial = prv_get_device_serial(); |
| 124 | + snprintf(s_topic_string, MEMFAULT_ARRAY_SIZE(s_topic_string), |
| 125 | + "memfault/" MEMFAULT_PROJECT "/%s/chunks", device_serial); |
| 126 | +} |
| 127 | + |
| 128 | +void app_memfault_transport_init(void) { |
| 129 | +#if MEMFAULT_FREERTOS_PORT_USE_STATIC_ALLOCATION != 0 |
| 130 | + static StaticSemaphore_t s_mqtt_connected; |
| 131 | + s_mqtt_connected = xSemaphoreCreateBinaryStatic(&s_mqtt_connected); |
| 132 | +#else |
| 133 | + s_mqtt_connected = xSemaphoreCreateBinary(); |
| 134 | +#endif |
| 135 | +} |
| 136 | + |
| 137 | +int app_memfault_transport_send_chunks(void) { |
| 138 | + int rv = prv_create_client(); |
| 139 | + |
| 140 | + if (rv) { |
| 141 | + return rv; |
| 142 | + } |
| 143 | + |
| 144 | + prv_build_topic_string(); |
| 145 | + |
| 146 | + ESP_LOGD(TAG, "Checking for packetizer data"); |
| 147 | + while (memfault_packetizer_data_available()) { |
| 148 | + size_t chunk_size = MEMFAULT_ARRAY_SIZE(s_chunk_data); |
| 149 | + bool chunk_filled = memfault_packetizer_get_chunk(s_chunk_data, &chunk_size); |
| 150 | + |
| 151 | + if (!chunk_filled) { |
| 152 | + ESP_LOGW(TAG, "No chunk data produced"); |
| 153 | + break; |
| 154 | + } |
| 155 | + |
| 156 | + rv = esp_mqtt_client_publish(s_mqtt_client, s_topic_string, (char *)s_chunk_data, chunk_size, 1, |
| 157 | + 0); |
| 158 | + if (rv < 0) { |
| 159 | + ESP_LOGE(TAG, "MQTT failed to publish[%d]", rv); |
| 160 | + memfault_packetizer_abort(); |
| 161 | + break; |
| 162 | + } |
| 163 | + |
| 164 | + memfault_metrics_heartbeat_add(MEMFAULT_METRICS_KEY(mqtt_publish_bytes), chunk_size); |
| 165 | + memfault_metrics_heartbeat_add(MEMFAULT_METRICS_KEY(mqtt_publish_count), 1); |
| 166 | + ESP_LOGD(TAG, "chunk[%d], len[%zu] published to %s", rv, chunk_size, s_topic_string); |
| 167 | + } |
| 168 | + |
| 169 | + prv_close_client(); |
| 170 | + return rv; |
| 171 | +} |
0 commit comments