Zephyr API Documentation  2.7.0-rc2
A Scalable Open Source RTOS
mqtt.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2018 Nordic Semiconductor ASA
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
22#ifndef ZEPHYR_INCLUDE_NET_MQTT_H_
23#define ZEPHYR_INCLUDE_NET_MQTT_H_
24
25#include <stddef.h>
26
27#include <zephyr.h>
28#include <zephyr/types.h>
29#include <net/tls_credentials.h>
30#include <net/net_ip.h>
31#include <sys/mutex.h>
32#include <net/websocket.h>
33
34#ifdef __cplusplus
35extern "C" {
36#endif
37
47
52
61
64
67
70
73
76
79
82};
83
88};
89
96
101
108
113
118
123
128
131
135
140
143
146
150
152struct mqtt_utf8 {
153 const uint8_t *utf8;
155};
156
166#define MQTT_UTF8_LITERAL(literal) \
167 ((struct mqtt_utf8) {literal, sizeof(literal) - 1})
168
173};
174
181
186};
187
192};
193
201
206};
207
211};
212
216};
217
221};
222
226};
227
232};
233
237};
238
245
248
253
258};
259
264
267
270};
271
279
287
290
293
296
299
302
305};
306
308struct mqtt_evt {
311
314
319};
320
321struct mqtt_client;
322
332 const struct mqtt_evt *evt);
333
338
341
346
349
352
356 const char *hostname;
357};
358
363
364#if defined(CONFIG_MQTT_LIB_TLS)
366 MQTT_TRANSPORT_SECURE,
367#endif /* CONFIG_MQTT_LIB_TLS */
368
369#if defined(CONFIG_MQTT_LIB_WEBSOCKET)
371 MQTT_TRANSPORT_NON_SECURE_WEBSOCKET,
372#if defined(CONFIG_MQTT_LIB_TLS)
374 MQTT_TRANSPORT_SECURE_WEBSOCKET,
375#endif
376#endif /* CONFIG_MQTT_LIB_WEBSOCKET */
377#if defined(CONFIG_MQTT_LIB_CUSTOM_TRANSPORT)
379 MQTT_TRANSPORT_CUSTOM,
380#endif /* CONFIG_MQTT_LIB_CUSTOM_TRANSPORT */
381
387
395
396 union {
397 /* TCP socket transport for MQTT */
398 struct {
400 int sock;
402
403#if defined(CONFIG_MQTT_LIB_TLS)
404 /* TLS socket transport for MQTT */
405 struct {
407 int sock;
408
412 struct mqtt_sec_config config;
413 } tls;
414#endif /* CONFIG_MQTT_LIB_TLS */
415 };
416
417#if defined(CONFIG_MQTT_LIB_WEBSOCKET)
419 struct {
421 struct websocket_request config;
422
424 int sock;
425
428 } websocket;
429#endif
430
431#if defined(CONFIG_MQTT_LIB_CUSTOM_TRANSPORT)
433 void *custom_transport_data;
434#endif /* CONFIG_MQTT_LIB_CUSTOM_TRANSPORT */
435
436#if defined(CONFIG_SOCKS)
437 struct {
438 struct sockaddr addr;
439 socklen_t addrlen;
440 } proxy;
441#endif
442};
443
448
453
456
459
462};
463
471
474
477
481 const void *broker;
482
487
493
496
501
505
508
511
514
517
522
525
528
532
537};
538
549
550#if defined(CONFIG_SOCKS)
551/*
552 * @brief Set proxy server details
553 *
554 * @param[in] client Client instance for which the procedure is requested,
555 * Shall not be NULL.
556 * @param[in] proxy_addr Proxy server address.
557 * @param[in] addrlen Proxy server address length.
558 *
559 * @return 0 or a negative error code (errno.h) indicating reason of failure.
560 *
561 * @note Must be called before calling mqtt_connect().
562 */
563int mqtt_client_set_proxy(struct mqtt_client *client,
564 struct sockaddr *proxy_addr,
565 socklen_t addrlen);
566#endif
567
587
599 const struct mqtt_publish_param *param);
600
613 const struct mqtt_puback_param *param);
614
627 const struct mqtt_pubrec_param *param);
628
640 const struct mqtt_pubrel_param *param);
641
654 const struct mqtt_pubcomp_param *param);
655
666 const struct mqtt_subscription_list *param);
667
681 const struct mqtt_subscription_list *param);
682
693
703
715
731
743
760
777 size_t length);
778
791 size_t length);
792
806 size_t length);
807
808#ifdef __cplusplus
809}
810#endif
811
812#endif /* ZEPHYR_INCLUDE_NET_MQTT_H_ */
813
ZTEST_BMEM int timeout
Definition: main.c:31
void
Definition: eswifi_shell.c:15
size_t socklen_t
Definition: net_ip.h:161
static ZTEST_BMEM char buffer[8]
Test mailbox enhance capabilities.
Definition: test_mbox_api.c:566
mqtt_evt_type
MQTT Asynchronous Events notified to the application from the module through the callback registered ...
Definition: mqtt.h:42
int mqtt_read_publish_payload_blocking(struct mqtt_client *client, void *buffer, size_t length)
Blocking version of mqtt_read_publish_payload function.
int mqtt_publish_qos2_complete(struct mqtt_client *client, const struct mqtt_pubcomp_param *param)
API used by client to send acknowledgment on receiving QoS2 publish release message....
int mqtt_input(struct mqtt_client *client)
Receive an incoming MQTT packet. The registered callback will be called with the packet content.
int mqtt_readall_publish_payload(struct mqtt_client *client, uint8_t *buffer, size_t length)
Blocking version of mqtt_read_publish_payload function which runs until the required number of bytes ...
int mqtt_read_publish_payload(struct mqtt_client *client, void *buffer, size_t length)
Read the payload of the received PUBLISH message. This function should be called within the MQTT even...
mqtt_qos
MQTT Quality of Service types.
Definition: mqtt.h:91
int mqtt_subscribe(struct mqtt_client *client, const struct mqtt_subscription_list *param)
API to request subscription of one or more topics on the connection.
int mqtt_unsubscribe(struct mqtt_client *client, const struct mqtt_subscription_list *param)
API to request unsubscription of one or more topics on the connection.
int mqtt_publish(struct mqtt_client *client, const struct mqtt_publish_param *param)
API to publish messages on topics.
int mqtt_publish_qos2_release(struct mqtt_client *client, const struct mqtt_pubrel_param *param)
API used by client to request release of QoS2 publish message. Should be called on reception of MQTT_...
int mqtt_live(struct mqtt_client *client)
This API should be called periodically for the client to be able to keep the connection alive by send...
mqtt_version
MQTT version protocol level.
Definition: mqtt.h:85
int mqtt_keepalive_time_left(const struct mqtt_client *client)
Helper function to determine when next keep alive message should be sent. Can be used for instance as...
mqtt_conn_return_code
MQTT CONNACK return codes.
Definition: mqtt.h:110
void(* mqtt_evt_cb_t)(struct mqtt_client *client, const struct mqtt_evt *evt)
Asynchronous event notification callback registered by the application.
Definition: mqtt.h:331
mqtt_suback_return_code
MQTT SUBACK return codes.
Definition: mqtt.h:137
int mqtt_publish_qos2_receive(struct mqtt_client *client, const struct mqtt_pubrec_param *param)
API used by client to send acknowledgment on receiving QoS2 publish message. Should be called on rece...
void mqtt_client_init(struct mqtt_client *client)
Initializes the client instance.
int mqtt_ping(struct mqtt_client *client)
API to send MQTT ping. The use of this API is optional, as the library handles the connection keep-al...
int mqtt_disconnect(struct mqtt_client *client)
API to disconnect MQTT connection.
int mqtt_connect(struct mqtt_client *client)
API to request new MQTT client connection.
int mqtt_publish_qos1_ack(struct mqtt_client *client, const struct mqtt_puback_param *param)
API used by client to send acknowledgment on receiving QoS1 publish message. Should be called on rece...
int mqtt_abort(struct mqtt_client *client)
API to abort MQTT connection. This will close the corresponding transport without closing the connect...
mqtt_transport_type
MQTT transport type.
Definition: mqtt.h:360
@ MQTT_EVT_PUBREC
Definition: mqtt.h:66
@ MQTT_EVT_PUBACK
Definition: mqtt.h:63
@ MQTT_EVT_PINGRESP
Definition: mqtt.h:81
@ MQTT_EVT_SUBACK
Definition: mqtt.h:75
@ MQTT_EVT_DISCONNECT
Definition: mqtt.h:51
@ MQTT_EVT_UNSUBACK
Definition: mqtt.h:78
@ MQTT_EVT_PUBLISH
Definition: mqtt.h:60
@ MQTT_EVT_PUBREL
Definition: mqtt.h:69
@ MQTT_EVT_CONNACK
Definition: mqtt.h:46
@ MQTT_EVT_PUBCOMP
Definition: mqtt.h:72
@ MQTT_QOS_0_AT_MOST_ONCE
Definition: mqtt.h:95
@ MQTT_QOS_1_AT_LEAST_ONCE
Definition: mqtt.h:100
@ MQTT_QOS_2_EXACTLY_ONCE
Definition: mqtt.h:106
@ MQTT_VERSION_3_1_0
Definition: mqtt.h:86
@ MQTT_VERSION_3_1_1
Definition: mqtt.h:87
@ MQTT_CONNECTION_ACCEPTED
Definition: mqtt.h:112
@ MQTT_NOT_AUTHORIZED
Definition: mqtt.h:133
@ MQTT_UNACCEPTABLE_PROTOCOL_VERSION
Definition: mqtt.h:117
@ MQTT_BAD_USER_NAME_OR_PASSWORD
Definition: mqtt.h:130
@ MQTT_IDENTIFIER_REJECTED
Definition: mqtt.h:122
@ MQTT_SERVER_UNAVAILABLE
Definition: mqtt.h:127
@ MQTT_SUBACK_FAILURE
Definition: mqtt.h:148
@ MQTT_SUBACK_SUCCESS_QoS_0
Definition: mqtt.h:139
@ MQTT_SUBACK_SUCCESS_QoS_2
Definition: mqtt.h:145
@ MQTT_SUBACK_SUCCESS_QoS_1
Definition: mqtt.h:142
@ MQTT_TRANSPORT_NUM
Definition: mqtt.h:385
@ MQTT_TRANSPORT_NON_SECURE
Definition: mqtt.h:362
int sec_tag_t
Definition: tls_credentials.h:70
struct k_thread client
Definition: main.c:31
IPv6 and IPv4 definitions.
__UINT32_TYPE__ uint32_t
Definition: stdint.h:60
__INT32_TYPE__ int32_t
Definition: stdint.h:44
__UINT8_TYPE__ uint8_t
Definition: stdint.h:58
__UINT16_TYPE__ uint16_t
Definition: stdint.h:59
__INT8_TYPE__ int8_t
Definition: stdint.h:42
Abstracts binary strings.
Definition: mqtt.h:170
uint8_t * data
Definition: mqtt.h:171
uint32_t len
Definition: mqtt.h:172
MQTT Client definition to maintain information relevant to the client.
Definition: mqtt.h:468
struct mqtt_internal internal
Definition: mqtt.h:470
mqtt_evt_cb_t evt_cb
Definition: mqtt.h:504
struct mqtt_topic * will_topic
Definition: mqtt.h:495
uint32_t tx_buf_size
Definition: mqtt.h:516
uint8_t protocol_version
Definition: mqtt.h:524
uint32_t rx_buf_size
Definition: mqtt.h:510
int8_t unacked_ping
Definition: mqtt.h:527
const void * broker
Definition: mqtt.h:481
struct mqtt_utf8 * will_message
Definition: mqtt.h:500
uint8_t * rx_buf
Definition: mqtt.h:507
uint16_t keepalive
Definition: mqtt.h:521
struct mqtt_utf8 client_id
Definition: mqtt.h:476
uint8_t clean_session
Definition: mqtt.h:536
struct mqtt_utf8 * user_name
Definition: mqtt.h:486
struct mqtt_transport transport
Definition: mqtt.h:473
uint8_t will_retain
Definition: mqtt.h:531
uint8_t * tx_buf
Definition: mqtt.h:513
struct mqtt_utf8 * password
Definition: mqtt.h:492
Parameters for a connection acknowledgment (CONNACK).
Definition: mqtt.h:195
enum mqtt_conn_return_code return_code
Definition: mqtt.h:205
uint8_t session_present_flag
Definition: mqtt.h:200
Defines MQTT asynchronous event notified to the application.
Definition: mqtt.h:308
enum mqtt_evt_type type
Definition: mqtt.h:310
int result
Definition: mqtt.h:318
union mqtt_evt_param param
Definition: mqtt.h:313
MQTT internal state.
Definition: mqtt.h:445
uint32_t rx_buf_datalen
Definition: mqtt.h:458
uint32_t last_activity
Definition: mqtt.h:452
uint32_t remaining_payload
Definition: mqtt.h:461
uint32_t state
Definition: mqtt.h:455
struct sys_mutex mutex
Definition: mqtt.h:447
Parameters for MQTT publish acknowledgment (PUBACK).
Definition: mqtt.h:209
uint16_t message_id
Definition: mqtt.h:210
Parameters for MQTT publish complete (PUBCOMP).
Definition: mqtt.h:224
uint16_t message_id
Definition: mqtt.h:225
Parameters for a publish message.
Definition: mqtt.h:189
struct mqtt_binstr payload
Definition: mqtt.h:191
struct mqtt_topic topic
Definition: mqtt.h:190
Parameters for a publish message.
Definition: mqtt.h:240
uint8_t dup_flag
Definition: mqtt.h:252
struct mqtt_publish_message message
Definition: mqtt.h:244
uint8_t retain_flag
Definition: mqtt.h:257
uint16_t message_id
Definition: mqtt.h:247
Parameters for MQTT publish receive (PUBREC).
Definition: mqtt.h:214
uint16_t message_id
Definition: mqtt.h:215
Parameters for MQTT publish release (PUBREL).
Definition: mqtt.h:219
uint16_t message_id
Definition: mqtt.h:220
TLS configuration for secure MQTT transports.
Definition: mqtt.h:335
int * cipher_list
Definition: mqtt.h:345
int peer_verify
Definition: mqtt.h:337
uint32_t cipher_count
Definition: mqtt.h:340
uint32_t sec_tag_count
Definition: mqtt.h:348
sec_tag_t * sec_tag_list
Definition: mqtt.h:351
const char * hostname
Definition: mqtt.h:356
Parameters for MQTT subscription acknowledgment (SUBACK).
Definition: mqtt.h:229
struct mqtt_binstr return_codes
Definition: mqtt.h:231
uint16_t message_id
Definition: mqtt.h:230
List of topics in a subscription request.
Definition: mqtt.h:261
uint16_t message_id
Definition: mqtt.h:269
uint16_t list_count
Definition: mqtt.h:266
struct mqtt_topic * list
Definition: mqtt.h:263
Abstracts MQTT UTF-8 encoded topic that can be subscribed to or published.
Definition: mqtt.h:178
uint8_t qos
Definition: mqtt.h:185
struct mqtt_utf8 topic
Definition: mqtt.h:180
MQTT transport specific data.
Definition: mqtt.h:389
int sock
Definition: mqtt.h:400
enum mqtt_transport_type type
Definition: mqtt.h:394
struct mqtt_transport::@158::@160 tcp
Parameters for MQTT unsubscribe acknowledgment (UNSUBACK).
Definition: mqtt.h:235
uint16_t message_id
Definition: mqtt.h:236
Abstracts UTF-8 encoded strings.
Definition: mqtt.h:152
uint32_t size
Definition: mqtt.h:154
const uint8_t * utf8
Definition: mqtt.h:153
Definition: net_ip.h:335
Definition: mutex.h:28
Definition: websocket.h:67
TLS credentials management.
Defines event parameters notified along with asynchronous events to the application.
Definition: mqtt.h:276
struct mqtt_pubrel_param pubrel
Definition: mqtt.h:295
struct mqtt_puback_param puback
Definition: mqtt.h:289
struct mqtt_pubrec_param pubrec
Definition: mqtt.h:292
struct mqtt_publish_param publish
Definition: mqtt.h:286
struct mqtt_connack_param connack
Definition: mqtt.h:278
struct mqtt_pubcomp_param pubcomp
Definition: mqtt.h:298
struct mqtt_unsuback_param unsuback
Definition: mqtt.h:304
struct mqtt_suback_param suback
Definition: mqtt.h:301
Websocket API.