1
6
7
8
9
10
16
17
18
19
20
21
22
23
24
25
28
29
30
31
32
40
41
42
43
45
47
48
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
90
91
92
95
108
109
112
118
119
125
126
127
135
136
139
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
168
170
172
174
175
176
177
178
180
181
182
183
184
185
188
189
190
191
192
193
195
197
198
200
201
202
203
205
206
207
208
209
210
211
212
213
214
215
216
217
224
225
228
229
235
242
248
249
251
253
254
255
259
261
262
267
268
269
272
273
274
277
278
279
280
281
282
283
284
285
286
287
288
289
291
292
294
295
297
298
299
301
302
303
306
307
310
317
318
322
325
326
327
328
331
332
334
336
337
339
340
341
342
345
349
354
359
360
363
366
367
368
371
375
376
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
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
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
/* ... */
#ifndef _MQTT_CLIENT_H_
#define _MQTT_CLIENT_H_
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include "esp_err.h"
#include "esp_event.h"
#include "esp_transport.h"6 includes
#ifdef CONFIG_MQTT_PROTOCOL_5
#include "mqtt5_client.h"
#endif
#ifdef __cplusplus
extern "C" {
#endif
#ifndef ESP_EVENT_DECLARE_BASE
typedef void *esp_event_loop_handle_t;
typedef void *esp_event_handler_t;/* ... */
#endif
typedef struct esp_mqtt_client *esp_mqtt_client_handle_t;
/* ... */
typedef enum esp_mqtt_event_id_t {
MQTT_EVENT_ANY = -1,
MQTT_EVENT_ERROR =
0,
/* ... */
MQTT_EVENT_CONNECTED,
/* ... */
MQTT_EVENT_DISCONNECTED,
MQTT_EVENT_SUBSCRIBED,
/* ... */
MQTT_EVENT_UNSUBSCRIBED,
MQTT_EVENT_PUBLISHED,
MQTT_EVENT_DATA,
/* ... */
MQTT_EVENT_BEFORE_CONNECT,
MQTT_EVENT_DELETED,
/* ... */
MQTT_USER_EVENT,
/* ... */
}{ ... } esp_mqtt_event_id_t;
/* ... */
typedef enum esp_mqtt_connect_return_code_t {
MQTT_CONNECTION_ACCEPTED = 0,
MQTT_CONNECTION_REFUSE_PROTOCOL,
/* ... */
MQTT_CONNECTION_REFUSE_ID_REJECTED,
/* ... */
MQTT_CONNECTION_REFUSE_SERVER_UNAVAILABLE,
/* ... */
MQTT_CONNECTION_REFUSE_BAD_USERNAME,
/* ... */
MQTT_CONNECTION_REFUSE_NOT_AUTHORIZED
/* ... */
}{ ... } esp_mqtt_connect_return_code_t;
/* ... */
typedef enum esp_mqtt_error_type_t {
MQTT_ERROR_TYPE_NONE = 0,
MQTT_ERROR_TYPE_TCP_TRANSPORT,
MQTT_ERROR_TYPE_CONNECTION_REFUSED,
MQTT_ERROR_TYPE_SUBSCRIBE_FAILED
}{ ... } esp_mqtt_error_type_t;
/* ... */
#define MQTT_ERROR_TYPE_ESP_TLS MQTT_ERROR_TYPE_TCP_TRANSPORT
typedef enum esp_mqtt_transport_t {
MQTT_TRANSPORT_UNKNOWN = 0x0,
MQTT_TRANSPORT_OVER_TCP,
MQTT_TRANSPORT_OVER_SSL,
MQTT_TRANSPORT_OVER_WS,
MQTT_TRANSPORT_OVER_WSS
/* ... */
}{ ... } esp_mqtt_transport_t;
/* ... */
typedef enum esp_mqtt_protocol_ver_t {
MQTT_PROTOCOL_UNDEFINED = 0,
MQTT_PROTOCOL_V_3_1,
MQTT_PROTOCOL_V_3_1_1,
MQTT_PROTOCOL_V_5,
}{ ... } esp_mqtt_protocol_ver_t;
/* ... */
typedef struct esp_mqtt_error_codes {
/* ... */
esp_err_t esp_tls_last_esp_err;
/* ... */
int esp_tls_stack_err;
/* ... */
int esp_tls_cert_verify_flags;
/* ... */
esp_mqtt_error_type_t
error_type;
esp_mqtt_connect_return_code_t
connect_return_code;
/* ... */
int esp_transport_sock_errno;
}{ ... } esp_mqtt_error_codes_t;
/* ... */
typedef struct esp_mqtt_event_t {
esp_mqtt_event_id_t event_id;
esp_mqtt_client_handle_t client;
char *data;
int data_len;
int total_data_len;
/* ... */
int current_data_offset;
/* ... */
char *topic;
int topic_len;
/* ... */
int msg_id;
int session_present;
esp_mqtt_error_codes_t
*error_handle;
/* ... */
bool retain;
int qos;
bool dup;
esp_mqtt_protocol_ver_t protocol_ver;
#ifdef CONFIG_MQTT_PROTOCOL_5
esp_mqtt5_event_property_t *property;
#endif
}{ ... } esp_mqtt_event_t;
typedef esp_mqtt_event_t *esp_mqtt_event_handle_t;
/* ... */
typedef struct esp_mqtt_client_config_t {
/* ... */
struct broker_t {
/* ... */
struct address_t {
const char *uri;
const char *hostname;
esp_mqtt_transport_t transport;
const char *path;
uint32_t port;
}{ ... } address;
/* ... */
struct verification_t {
bool use_global_ca_store;
/* ... */
esp_err_t (*crt_bundle_attach)(void *conf);
/* ... */
const char *certificate;
size_t certificate_len;
const struct psk_key_hint *psk_hint_key;
/* ... */
bool skip_cert_common_name_check;
/* ... */
const char **alpn_protos;
const char *common_name;
/* ... */
}{ ... } verification;
}{ ... } broker;
/* ... */
struct credentials_t {
const char *username;
const char *client_id;
/* ... */
bool set_null_client_id;
/* ... */
struct authentication_t {
const char *password;
const char *certificate;
/* ... */
size_t certificate_len;
const char *key;
/* ... */
size_t key_len;
const char *key_password;
/* ... */
int key_password_len;
bool use_secure_element;
void *ds_data;
/* ... */
}{ ... } authentication;
}{ ... } credentials;
/* ... */
struct session_t {
/* ... */
struct last_will_t {
const char *topic;
const char *msg;
int msg_len;
int qos;
int retain;
}{ ... } last_will;
bool disable_clean_session;
int keepalive;
/* ... */
bool disable_keepalive;
/* ... */
esp_mqtt_protocol_ver_t protocol_ver;
int message_retransmit_timeout;
}{ ... } session;
/* ... */
struct network_t {
int reconnect_timeout_ms;
/* ... */
int timeout_ms;
/* ... */
int refresh_connection_after_ms;
bool disable_auto_reconnect;
/* ... */
esp_transport_handle_t transport;
struct ifreq * if_name;
}{ ... } network;
/* ... */
struct task_t {
int priority;
int stack_size;
}{ ... } task;
/* ... */
struct buffer_t {
int size;
int out_size;
/* ... */
}{ ... } buffer;
/* ... */
struct outbox_config_t {
uint64_t limit;
}{ ... } outbox;
}{ ... } esp_mqtt_client_config_t;
/* ... */
typedef struct topic_t {
const char *filter;
int qos;
}{ ... } esp_mqtt_topic_t;
/* ... */
esp_mqtt_client_handle_t
esp_mqtt_client_init(const esp_mqtt_client_config_t *config);
/* ... */
esp_err_t esp_mqtt_client_set_uri(esp_mqtt_client_handle_t client,
const char *uri);
/* ... */
esp_err_t esp_mqtt_client_start(esp_mqtt_client_handle_t client);
/* ... */
esp_err_t esp_mqtt_client_reconnect(esp_mqtt_client_handle_t client);
/* ... */
esp_err_t esp_mqtt_client_disconnect(esp_mqtt_client_handle_t client);
/* ... */
esp_err_t esp_mqtt_client_stop(esp_mqtt_client_handle_t client);
#ifdef __cplusplus
#define esp_mqtt_client_subscribe esp_mqtt_client_subscribe_single
/* ... */
#else
/* ... */
#define esp_mqtt_client_subscribe(client_handle, topic_type, qos_or_size) _Generic((topic_type), \
char *: esp_mqtt_client_subscribe_single, \
const char *: esp_mqtt_client_subscribe_single, \
esp_mqtt_topic_t*: esp_mqtt_client_subscribe_multiple \
)(client_handle, topic_type, qos_or_size)...
/* ... */
#endif
/* ... */
int esp_mqtt_client_subscribe_single(esp_mqtt_client_handle_t client,
const char *topic, int qos);
/* ... */
int esp_mqtt_client_subscribe_multiple(esp_mqtt_client_handle_t client,
const esp_mqtt_topic_t *topic_list, int size);
/* ... */
int esp_mqtt_client_unsubscribe(esp_mqtt_client_handle_t client,
const char *topic);
/* ... */
int esp_mqtt_client_publish(esp_mqtt_client_handle_t client, const char *topic,
const char *data, int len, int qos, int retain);
/* ... */
int esp_mqtt_client_enqueue(esp_mqtt_client_handle_t client, const char *topic,
const char *data, int len, int qos, int retain,
bool store);
/* ... */
esp_err_t esp_mqtt_client_destroy(esp_mqtt_client_handle_t client);
/* ... */
esp_err_t esp_mqtt_set_config(esp_mqtt_client_handle_t client,
const esp_mqtt_client_config_t *config);
/* ... */
esp_err_t esp_mqtt_client_register_event(esp_mqtt_client_handle_t client,
esp_mqtt_event_id_t event,
esp_event_handler_t event_handler,
void *event_handler_arg);
/* ... */
esp_err_t esp_mqtt_client_unregister_event(esp_mqtt_client_handle_t client, esp_mqtt_event_id_t event, esp_event_handler_t event_handler);
/* ... */
int esp_mqtt_client_get_outbox_size(esp_mqtt_client_handle_t client);
/* ... */
esp_err_t esp_mqtt_dispatch_custom_event(esp_mqtt_client_handle_t client, esp_mqtt_event_t *event);
#ifdef __cplusplus
}{...}
#endif
/* ... */
#endif