-
Notifications
You must be signed in to change notification settings - Fork 30
/
esp_mqtt.c
706 lines (568 loc) · 18.5 KB
/
esp_mqtt.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
#include <esp_log.h>
#include <freertos/FreeRTOS.h>
#include <freertos/semphr.h>
#include <freertos/task.h>
#include <lwmqtt.h>
#include <string.h>
#if defined(CONFIG_ESP_MQTT_TLS_ENABLE)
#include "esp_tls_lwmqtt.h"
#endif
#include "esp_lwmqtt.h"
#include "esp_mqtt.h"
#define ESP_MQTT_LOG_TAG "esp_mqtt"
static SemaphoreHandle_t esp_mqtt_main_mutex = NULL;
#define ESP_MQTT_LOCK_MAIN() \
do { \
} while (xSemaphoreTake(esp_mqtt_main_mutex, portMAX_DELAY) != pdPASS)
#define ESP_MQTT_UNLOCK_MAIN() xSemaphoreGive(esp_mqtt_main_mutex)
static SemaphoreHandle_t esp_mqtt_select_mutex = NULL;
#define ESP_MQTT_LOCK_SELECT() \
do { \
} while (xSemaphoreTake(esp_mqtt_select_mutex, portMAX_DELAY) != pdPASS)
#define ESP_MQTT_UNLOCK_SELECT() xSemaphoreGive(esp_mqtt_select_mutex)
static TaskHandle_t esp_mqtt_task = NULL;
static size_t esp_mqtt_buffer_size;
static uint32_t esp_mqtt_command_timeout;
static struct {
char *host;
char *port;
char *client_id;
char *username;
char *password;
} esp_mqtt_config = {0};
static struct {
char *topic;
char *payload;
int qos;
bool retained;
} esp_mqtt_lwt_config = {0};
static bool esp_mqtt_running = false;
static bool esp_mqtt_connected = false;
static bool esp_mqtt_error = false;
static esp_mqtt_status_callback_t esp_mqtt_status_callback = NULL;
static esp_mqtt_message_callback_t esp_mqtt_message_callback = NULL;
static lwmqtt_client_t esp_mqtt_client;
static esp_lwmqtt_network_t esp_mqtt_network = {0};
#if defined(CONFIG_ESP_MQTT_TLS_ENABLE)
static bool esp_mqtt_use_tls = false;
static esp_tls_lwmqtt_network_t esp_mqtt_tls_network = {0};
#endif
static esp_lwmqtt_timer_t esp_mqtt_timer1, esp_mqtt_timer2;
static void *esp_mqtt_write_buffer;
static void *esp_mqtt_read_buffer;
static int esp_mqtt_core;
static QueueHandle_t esp_mqtt_event_queue = NULL;
typedef struct {
void *buffer;
lwmqtt_string_t topic;
lwmqtt_message_t message;
} esp_mqtt_event_t;
void esp_mqtt_init(esp_mqtt_status_callback_t scb, esp_mqtt_message_callback_t mcb, size_t buffer_size,
int command_timeout, int core) {
// set callbacks
esp_mqtt_status_callback = scb;
esp_mqtt_message_callback = mcb;
// set settings
esp_mqtt_buffer_size = buffer_size;
esp_mqtt_command_timeout = (uint32_t)command_timeout;
esp_mqtt_core = core;
// allocate buffers
esp_mqtt_write_buffer = malloc((size_t)buffer_size);
esp_mqtt_read_buffer = malloc((size_t)buffer_size);
// create mutexes
esp_mqtt_main_mutex = xSemaphoreCreateMutex();
esp_mqtt_select_mutex = xSemaphoreCreateMutex();
// create queue
esp_mqtt_event_queue = xQueueCreate(CONFIG_ESP_MQTT_EVENT_QUEUE_SIZE, sizeof(esp_mqtt_event_t *));
}
#if defined(CONFIG_ESP_MQTT_TLS_ENABLE)
bool esp_mqtt_tls(bool enable, bool verify, const uint8_t *ca_buf, size_t ca_len) {
// acquire mutex
ESP_MQTT_LOCK_MAIN();
// free existing buffer
if (esp_mqtt_tls_network.ca_buf) {
free(esp_mqtt_tls_network.ca_buf);
}
// disable if requested
if (!enable) {
esp_mqtt_use_tls = false;
esp_mqtt_tls_network.verify = false;
esp_mqtt_tls_network.ca_buf = NULL;
esp_mqtt_tls_network.ca_len = 0;
ESP_MQTT_UNLOCK_MAIN();
return true;
}
// check ca certificate
if (verify && (!ca_buf || ca_len <= 0)) {
ESP_LOGE(ESP_MQTT_LOG_TAG, "esp_mqtt_tls: ca_buf must be not NULL when verify is enabled.");
ESP_MQTT_UNLOCK_MAIN();
return false;
}
// set configuration
esp_mqtt_use_tls = true;
esp_mqtt_tls_network.verify = verify;
esp_mqtt_tls_network.ca_buf = NULL;
esp_mqtt_tls_network.ca_len = 0;
// copy buffer
if (ca_buf) {
esp_mqtt_tls_network.ca_len = ca_len + 1;
esp_mqtt_tls_network.ca_buf = malloc(ca_len + 1);
memcpy(esp_mqtt_tls_network.ca_buf, ca_buf, ca_len);
esp_mqtt_tls_network.ca_buf[ca_len] = 0;
}
// release mutex
ESP_MQTT_UNLOCK_MAIN();
return true;
}
#endif
static void esp_mqtt_message_handler(lwmqtt_client_t *client, void *ref, lwmqtt_string_t topic, lwmqtt_message_t msg) {
// allocate buffer
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 = 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 = 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);
}
}
static void esp_mqtt_dispatch_events() {
// receive next event
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, (int)evt->message.qos,
evt->message.retained);
}
// free event
free(evt);
}
}
static bool esp_mqtt_process_connect() {
// initialize the client
lwmqtt_init(&esp_mqtt_client, esp_mqtt_write_buffer, esp_mqtt_buffer_size, esp_mqtt_read_buffer,
esp_mqtt_buffer_size);
#if defined(CONFIG_ESP_MQTT_TLS_ENABLE)
if (esp_mqtt_use_tls) {
lwmqtt_set_network(&esp_mqtt_client, &esp_mqtt_tls_network, esp_tls_lwmqtt_network_read,
esp_tls_lwmqtt_network_write);
} else {
lwmqtt_set_network(&esp_mqtt_client, &esp_mqtt_network, esp_lwmqtt_network_read, esp_lwmqtt_network_write);
}
#else
lwmqtt_set_network(&esp_mqtt_client, &esp_mqtt_network, esp_lwmqtt_network_read, esp_lwmqtt_network_write);
#endif
lwmqtt_set_timers(&esp_mqtt_client, &esp_mqtt_timer1, &esp_mqtt_timer2, esp_lwmqtt_timer_set, esp_lwmqtt_timer_get);
lwmqtt_set_callback(&esp_mqtt_client, NULL, esp_mqtt_message_handler);
// initiate network connection
lwmqtt_err_t err;
#if defined(CONFIG_ESP_MQTT_TLS_ENABLE)
if (esp_mqtt_use_tls) {
err = esp_tls_lwmqtt_network_connect(&esp_mqtt_tls_network, esp_mqtt_config.host, esp_mqtt_config.port);
} else {
err = esp_lwmqtt_network_connect(&esp_mqtt_network, esp_mqtt_config.host, esp_mqtt_config.port);
}
#else
err = esp_lwmqtt_network_connect(&esp_mqtt_network, esp_mqtt_config.host, esp_mqtt_config.port);
#endif
if (err != LWMQTT_SUCCESS) {
ESP_LOGE(ESP_MQTT_LOG_TAG, "esp_lwmqtt_network_connect: %d", err);
return false;
}
// release mutex
ESP_MQTT_UNLOCK_MAIN();
// acquire select mutex
ESP_MQTT_LOCK_SELECT();
// wait for connection
bool connected = false;
#if defined(CONFIG_ESP_MQTT_TLS_ENABLE)
if (esp_mqtt_use_tls) {
err = esp_tls_lwmqtt_network_wait(&esp_mqtt_tls_network, &connected, esp_mqtt_command_timeout);
} else {
err = esp_lwmqtt_network_wait(&esp_mqtt_network, &connected, esp_mqtt_command_timeout);
}
#else
err = esp_lwmqtt_network_wait(&esp_mqtt_network, &connected, esp_mqtt_command_timeout);
#endif
if (err != LWMQTT_SUCCESS) {
ESP_LOGE(ESP_MQTT_LOG_TAG, "esp_lwmqtt_network_wait: %d", err);
return false;
}
// release select mutex
ESP_MQTT_UNLOCK_SELECT();
// acquire mutex
ESP_MQTT_LOCK_MAIN();
// return if not connected
if (!connected) {
return false;
}
// setup connect data
lwmqtt_options_t options = lwmqtt_default_options;
options.keep_alive = 10;
options.client_id = lwmqtt_string(esp_mqtt_config.client_id);
options.username = lwmqtt_string(esp_mqtt_config.username);
options.password = lwmqtt_string(esp_mqtt_config.password);
// last will data
lwmqtt_will_t will;
will.topic = lwmqtt_string(esp_mqtt_lwt_config.topic);
will.qos = (lwmqtt_qos_t)esp_mqtt_lwt_config.qos;
will.retained = esp_mqtt_lwt_config.retained;
will.payload = lwmqtt_string(esp_mqtt_lwt_config.payload);
// attempt connection
lwmqtt_return_code_t return_code;
err =
lwmqtt_connect(&esp_mqtt_client, options, will.topic.len ? &will : NULL, &return_code, esp_mqtt_command_timeout);
if (err != LWMQTT_SUCCESS) {
ESP_LOGE(ESP_MQTT_LOG_TAG, "lwmqtt_connect: %d", err);
return false;
}
return true;
}
static void esp_mqtt_process() {
// connection loop
for (;;) {
// log attempt
ESP_LOGI(ESP_MQTT_LOG_TAG, "esp_mqtt_process: begin connection attempt");
// acquire mutex
ESP_MQTT_LOCK_MAIN();
// make connection attempt
if (esp_mqtt_process_connect()) {
// log success
ESP_LOGI(ESP_MQTT_LOG_TAG, "esp_mqtt_process: connection attempt successful");
// set flag
esp_mqtt_connected = true;
// release mutex
ESP_MQTT_UNLOCK_MAIN();
// exit loop
break;
}
// release mutex
ESP_MQTT_UNLOCK_MAIN();
// log fail
ESP_LOGW(ESP_MQTT_LOG_TAG, "esp_mqtt_process: connection attempt failed");
// delay loop by 1s and yield to other processes
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
// call callback if existing
if (esp_mqtt_status_callback) {
esp_mqtt_status_callback(ESP_MQTT_STATUS_CONNECTED);
}
// yield loop
for (;;) {
// check for error
if (esp_mqtt_error) {
break;
}
// acquire select mutex
ESP_MQTT_LOCK_SELECT();
// block until data is available
bool available = false;
lwmqtt_err_t err;
#if defined(CONFIG_ESP_MQTT_TLS_ENABLE)
if (esp_mqtt_use_tls) {
err = esp_tls_lwmqtt_network_select(&esp_mqtt_tls_network, &available, esp_mqtt_command_timeout);
} else {
err = esp_lwmqtt_network_select(&esp_mqtt_network, &available, esp_mqtt_command_timeout);
}
#else
err = esp_lwmqtt_network_select(&esp_mqtt_network, &available, esp_mqtt_command_timeout);
#endif
if (err != LWMQTT_SUCCESS) {
ESP_LOGE(ESP_MQTT_LOG_TAG, "esp_lwmqtt_network_select: %d", err);
ESP_MQTT_UNLOCK_SELECT();
break;
}
// release select mutex
ESP_MQTT_UNLOCK_SELECT();
// acquire mutex
ESP_MQTT_LOCK_MAIN();
// process data if available
if (available) {
// get available bytes
size_t available_bytes = 0;
#if defined(CONFIG_ESP_MQTT_TLS_ENABLE)
if (esp_mqtt_use_tls) {
err = esp_tls_lwmqtt_network_peek(&esp_mqtt_tls_network, &available_bytes, esp_mqtt_command_timeout);
} else {
err = esp_lwmqtt_network_peek(&esp_mqtt_network, &available_bytes);
}
#else
err = esp_lwmqtt_network_peek(&esp_mqtt_network, &available_bytes);
#endif
if (err != LWMQTT_SUCCESS) {
ESP_LOGE(ESP_MQTT_LOG_TAG, "esp_lwmqtt_network_peek: %d", err);
ESP_MQTT_UNLOCK_MAIN();
break;
}
// yield client only if there is still data to read since select might unblock because of incoming ack packets
// that are already handled until we get to this point
if (available_bytes > 0) {
err = lwmqtt_yield(&esp_mqtt_client, available_bytes, esp_mqtt_command_timeout);
if (err != LWMQTT_SUCCESS) {
ESP_LOGE(ESP_MQTT_LOG_TAG, "lwmqtt_yield: %d", err);
ESP_MQTT_UNLOCK_MAIN();
break;
}
}
}
// do mqtt background work
err = lwmqtt_keep_alive(&esp_mqtt_client, esp_mqtt_command_timeout);
if (err != LWMQTT_SUCCESS) {
ESP_LOGE(ESP_MQTT_LOG_TAG, "lwmqtt_keep_alive: %d", err);
ESP_MQTT_UNLOCK_MAIN();
break;
}
// release mutex
ESP_MQTT_UNLOCK_MAIN();
// dispatch queued events
esp_mqtt_dispatch_events();
}
// acquire mutex
ESP_MQTT_LOCK_MAIN();
// disconnect network
#if defined(CONFIG_ESP_MQTT_TLS_ENABLE)
if (esp_mqtt_use_tls) {
esp_tls_lwmqtt_network_disconnect(&esp_mqtt_tls_network);
} else {
esp_lwmqtt_network_disconnect(&esp_mqtt_network);
}
#else
esp_lwmqtt_network_disconnect(&esp_mqtt_network);
#endif
// set flags
esp_mqtt_connected = false;
esp_mqtt_error = false;
// release mutex
ESP_MQTT_UNLOCK_MAIN();
// log event
ESP_LOGI(ESP_MQTT_LOG_TAG, "esp_mqtt_process: lost connection");
// call callback if existing
if (esp_mqtt_status_callback) {
esp_mqtt_status_callback(ESP_MQTT_STATUS_DISCONNECTED);
}
}
static void esp_mqtt_run(void *p) {
// keep processing
for (;;) {
esp_mqtt_process();
}
}
void esp_mqtt_lwt(const char *topic, const char *payload, int qos, bool retained) {
// acquire mutex
ESP_MQTT_LOCK_MAIN();
// free topic if set
if (esp_mqtt_lwt_config.topic != NULL) {
free(esp_mqtt_lwt_config.topic);
esp_mqtt_lwt_config.topic = NULL;
}
// free payload if set
if (esp_mqtt_lwt_config.payload != NULL) {
free(esp_mqtt_lwt_config.payload);
esp_mqtt_lwt_config.payload = NULL;
}
// set topic if provided
if (topic != NULL) {
esp_mqtt_lwt_config.topic = strdup(topic);
}
// set payload if provided
if (payload != NULL) {
esp_mqtt_lwt_config.payload = strdup(payload);
}
// set qos
esp_mqtt_lwt_config.qos = qos;
// set retained
esp_mqtt_lwt_config.retained = retained;
// release mutex
ESP_MQTT_UNLOCK_MAIN();
}
bool esp_mqtt_start(const char *host, const char *port, const char *client_id, const char *username,
const char *password) {
// acquire mutex
ESP_MQTT_LOCK_MAIN();
// check if already running
if (esp_mqtt_running) {
ESP_LOGW(ESP_MQTT_LOG_TAG, "esp_mqtt_start: already running");
ESP_MQTT_UNLOCK_MAIN();
return true;
}
// free host if set
if (esp_mqtt_config.host != NULL) {
free(esp_mqtt_config.host);
esp_mqtt_config.host = NULL;
}
// free port if set
if (esp_mqtt_config.port != NULL) {
free(esp_mqtt_config.port);
esp_mqtt_config.port = NULL;
}
// free client id if set
if (esp_mqtt_config.client_id != NULL) {
free(esp_mqtt_config.client_id);
esp_mqtt_config.client_id = NULL;
}
// free username if set
if (esp_mqtt_config.username != NULL) {
free(esp_mqtt_config.username);
esp_mqtt_config.username = NULL;
}
// free password if set
if (esp_mqtt_config.password != NULL) {
free(esp_mqtt_config.password);
esp_mqtt_config.password = NULL;
}
// set host if provided
if (host != NULL) {
esp_mqtt_config.host = strdup(host);
}
// set port if provided
if (port != NULL) {
esp_mqtt_config.port = strdup(port);
}
// set client id if provided
if (client_id != NULL) {
esp_mqtt_config.client_id = strdup(client_id);
}
// set username if provided
if (username != NULL) {
esp_mqtt_config.username = strdup(username);
}
// set password if provided
if (password != NULL) {
esp_mqtt_config.password = strdup(password);
}
// create mqtt thread
ESP_LOGI(ESP_MQTT_LOG_TAG, "esp_mqtt_start: create task");
BaseType_t ret = xTaskCreatePinnedToCore(esp_mqtt_run, "esp_mqtt", CONFIG_ESP_MQTT_TASK_STACK, NULL,
CONFIG_ESP_MQTT_TASK_PRIORITY, &esp_mqtt_task, esp_mqtt_core);
if (ret != pdPASS) {
ESP_LOGW(ESP_MQTT_LOG_TAG, "esp_mqtt_start: failed to create task");
ESP_MQTT_UNLOCK_MAIN();
return false;
}
// set flag
esp_mqtt_running = true;
// release mutex
ESP_MQTT_UNLOCK_MAIN();
return true;
}
bool esp_mqtt_subscribe(const char *topic, int qos) {
// acquire mutex
ESP_MQTT_LOCK_MAIN();
// check if still connected
if (!esp_mqtt_connected) {
ESP_LOGW(ESP_MQTT_LOG_TAG, "esp_mqtt_subscribe: not connected");
ESP_MQTT_UNLOCK_MAIN();
return false;
}
// subscribe to topic
lwmqtt_err_t err =
lwmqtt_subscribe_one(&esp_mqtt_client, lwmqtt_string(topic), (lwmqtt_qos_t)qos, esp_mqtt_command_timeout);
if (err != LWMQTT_SUCCESS) {
esp_mqtt_error = true;
ESP_LOGE(ESP_MQTT_LOG_TAG, "lwmqtt_subscribe_one: %d", err);
ESP_MQTT_UNLOCK_MAIN();
return false;
}
// release mutex
ESP_MQTT_UNLOCK_MAIN();
return true;
}
bool esp_mqtt_unsubscribe(const char *topic) {
// acquire mutex
ESP_MQTT_LOCK_MAIN();
// check if still connected
if (!esp_mqtt_connected) {
ESP_LOGW(ESP_MQTT_LOG_TAG, "esp_mqtt_unsubscribe: not connected");
ESP_MQTT_UNLOCK_MAIN();
return false;
}
// unsubscribe from topic
lwmqtt_err_t err = lwmqtt_unsubscribe_one(&esp_mqtt_client, lwmqtt_string(topic), esp_mqtt_command_timeout);
if (err != LWMQTT_SUCCESS) {
esp_mqtt_error = true;
ESP_LOGE(ESP_MQTT_LOG_TAG, "lwmqtt_unsubscribe_one: %d", err);
ESP_MQTT_UNLOCK_MAIN();
return false;
}
// release mutex
ESP_MQTT_UNLOCK_MAIN();
return true;
}
bool esp_mqtt_publish(const char *topic, const uint8_t *payload, size_t len, int qos, bool retained) {
// acquire mutex
ESP_MQTT_LOCK_MAIN();
// check if still connected
if (!esp_mqtt_connected) {
ESP_LOGW(ESP_MQTT_LOG_TAG, "esp_mqtt_publish: not connected");
ESP_MQTT_UNLOCK_MAIN();
return false;
}
// prepare message
lwmqtt_message_t message;
message.qos = (lwmqtt_qos_t)qos;
message.retained = retained;
message.payload = (uint8_t*)payload;
message.payload_len = len;
// publish message
lwmqtt_err_t err = lwmqtt_publish(&esp_mqtt_client, lwmqtt_string(topic), message, esp_mqtt_command_timeout);
if (err != LWMQTT_SUCCESS) {
esp_mqtt_error = true;
ESP_LOGE(ESP_MQTT_LOG_TAG, "lwmqtt_publish: %d", err);
ESP_MQTT_UNLOCK_MAIN();
return false;
}
// release mutex
ESP_MQTT_UNLOCK_MAIN();
return true;
}
void esp_mqtt_stop() {
// acquire mutexes
ESP_MQTT_LOCK_MAIN();
ESP_MQTT_LOCK_SELECT();
// return immediately if not running anymore
if (!esp_mqtt_running) {
ESP_MQTT_UNLOCK_SELECT();
ESP_MQTT_UNLOCK_MAIN();
return;
}
// attempt to properly disconnect a connected client
if (esp_mqtt_connected) {
lwmqtt_err_t err = lwmqtt_disconnect(&esp_mqtt_client, esp_mqtt_command_timeout);
if (err != LWMQTT_SUCCESS) {
ESP_LOGE(ESP_MQTT_LOG_TAG, "lwmqtt_disconnect: %d", err);
}
// set flag
esp_mqtt_connected = false;
}
// disconnect network
#if defined(CONFIG_ESP_MQTT_TLS_ENABLE)
if (esp_mqtt_use_tls) {
esp_tls_lwmqtt_network_disconnect(&esp_mqtt_tls_network);
} else {
esp_lwmqtt_network_disconnect(&esp_mqtt_network);
}
#else
esp_lwmqtt_network_disconnect(&esp_mqtt_network);
#endif
// kill mqtt task
ESP_LOGI(ESP_MQTT_LOG_TAG, "esp_mqtt_stop: deleting task");
vTaskDelete(esp_mqtt_task);
// set flag
esp_mqtt_running = false;
// release mutexes
ESP_MQTT_UNLOCK_SELECT();
ESP_MQTT_UNLOCK_MAIN();
}