Zephyr API Documentation  3.5.0
A Scalable Open Source RTOS
3.5.0
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
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
19#ifndef ZEPHYR_INCLUDE_NET_MQTT_H_
20#define ZEPHYR_INCLUDE_NET_MQTT_H_
21
22#include <stddef.h>
23
24#include <zephyr/kernel.h>
25#include <zephyr/types.h>
27#include <zephyr/net/net_ip.h>
28#include <zephyr/sys/mutex.h>
30
31#ifdef __cplusplus
32extern "C" {
33#endif
34
44
49
58
61
64
67
70
73
76
79};
80
85};
86
93
98
105
110
115
120
125
128
132
137
140
143
147
149struct mqtt_utf8 {
150 const uint8_t *utf8;
152};
153
163#define MQTT_UTF8_LITERAL(literal) \
164 ((struct mqtt_utf8) {literal, sizeof(literal) - 1})
165
170};
171
178
183};
184
189};
190
198
203};
204
209};
210
215};
216
221};
222
227};
228
237};
238
243};
244
251
254
259
264};
265
270
273
276};
277
285
293
296
299
302
305
308
311};
312
314struct mqtt_evt {
317
320
325};
326
327struct mqtt_client;
328
337typedef void (*mqtt_evt_cb_t)(struct mqtt_client *client,
338 const struct mqtt_evt *evt);
339
344
347
351 const int *cipher_list;
352
355
358
362 const char *hostname;
363
366};
367
372
373#if defined(CONFIG_MQTT_LIB_TLS)
375 MQTT_TRANSPORT_SECURE,
376#endif /* CONFIG_MQTT_LIB_TLS */
377
378#if defined(CONFIG_MQTT_LIB_WEBSOCKET)
380 MQTT_TRANSPORT_NON_SECURE_WEBSOCKET,
381#if defined(CONFIG_MQTT_LIB_TLS)
383 MQTT_TRANSPORT_SECURE_WEBSOCKET,
384#endif
385#endif /* CONFIG_MQTT_LIB_WEBSOCKET */
386#if defined(CONFIG_MQTT_LIB_CUSTOM_TRANSPORT)
388 MQTT_TRANSPORT_CUSTOM,
389#endif /* CONFIG_MQTT_LIB_CUSTOM_TRANSPORT */
390
396
404
405 union {
406 /* TCP socket transport for MQTT */
407 struct {
409 int sock;
411
412#if defined(CONFIG_MQTT_LIB_TLS)
413 /* TLS socket transport for MQTT */
414 struct {
416 int sock;
417
421 struct mqtt_sec_config config;
422 } tls;
423#endif /* CONFIG_MQTT_LIB_TLS */
424 };
425
426#if defined(CONFIG_MQTT_LIB_WEBSOCKET)
428 struct {
430 struct websocket_request config;
431
433 int sock;
434
436 int32_t timeout;
437 } websocket;
438#endif
439
440#if defined(CONFIG_MQTT_LIB_CUSTOM_TRANSPORT)
442 void *custom_transport_data;
443#endif /* CONFIG_MQTT_LIB_CUSTOM_TRANSPORT */
444
445#if defined(CONFIG_SOCKS)
446 struct {
447 struct sockaddr addr;
448 socklen_t addrlen;
449 } proxy;
450#endif
451};
452
457
462
465
468
471};
472
480
483
486
490 const void *broker;
491
496
502
505
510
514
517
520
523
526
531
534
537
541
546};
547
557void mqtt_client_init(struct mqtt_client *client);
558
559#if defined(CONFIG_SOCKS)
560/*
561 * @brief Set proxy server details
562 *
563 * @param[in] client Client instance for which the procedure is requested,
564 * Shall not be NULL.
565 * @param[in] proxy_addr Proxy server address.
566 * @param[in] addrlen Proxy server address length.
567 *
568 * @return 0 or a negative error code (errno.h) indicating reason of failure.
569 *
570 * @note Must be called before calling mqtt_connect().
571 */
572int mqtt_client_set_proxy(struct mqtt_client *client,
573 struct sockaddr *proxy_addr,
574 socklen_t addrlen);
575#endif
576
595int mqtt_connect(struct mqtt_client *client);
596
607int mqtt_publish(struct mqtt_client *client,
608 const struct mqtt_publish_param *param);
609
622 const struct mqtt_puback_param *param);
623
636 const struct mqtt_pubrec_param *param);
637
649 const struct mqtt_pubrel_param *param);
650
663 const struct mqtt_pubcomp_param *param);
664
674int mqtt_subscribe(struct mqtt_client *client,
675 const struct mqtt_subscription_list *param);
676
689int mqtt_unsubscribe(struct mqtt_client *client,
690 const struct mqtt_subscription_list *param);
691
701int mqtt_ping(struct mqtt_client *client);
702
711int mqtt_disconnect(struct mqtt_client *client);
712
723int mqtt_abort(struct mqtt_client *client);
724
739int mqtt_live(struct mqtt_client *client);
740
751int mqtt_keepalive_time_left(const struct mqtt_client *client);
752
768int mqtt_input(struct mqtt_client *client);
769
785int mqtt_read_publish_payload(struct mqtt_client *client, void *buffer,
786 size_t length);
787
799int mqtt_read_publish_payload_blocking(struct mqtt_client *client, void *buffer,
800 size_t length);
801
815 size_t length);
816
817#ifdef __cplusplus
818}
819#endif
820
821#endif /* ZEPHYR_INCLUDE_NET_MQTT_H_ */
822
size_t socklen_t
Length of a socket address.
Definition: net_ip.h:168
mqtt_evt_type
MQTT Asynchronous Events notified to the application from the module through the callback registered ...
Definition: mqtt.h:39
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.
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.
mqtt_qos
MQTT Quality of Service types.
Definition: mqtt.h:88
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.
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:82
int mqtt_keepalive_time_left(const struct mqtt_client *client)
Helper function to determine when next keep alive message should be sent.
mqtt_conn_return_code
MQTT CONNACK return codes.
Definition: mqtt.h:107
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:337
mqtt_suback_return_code
MQTT SUBACK return codes.
Definition: mqtt.h:134
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.
void mqtt_client_init(struct mqtt_client *client)
Initializes the client instance.
int mqtt_ping(struct mqtt_client *client)
API to send MQTT ping.
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.
int mqtt_abort(struct mqtt_client *client)
API to abort MQTT connection.
mqtt_transport_type
MQTT transport type.
Definition: mqtt.h:369
@ MQTT_EVT_PUBREC
Reception confirmation for published message with QoS 2.
Definition: mqtt.h:63
@ MQTT_EVT_PUBACK
Acknowledgment for published message with QoS 1.
Definition: mqtt.h:60
@ MQTT_EVT_PINGRESP
Ping Response from server.
Definition: mqtt.h:78
@ MQTT_EVT_SUBACK
Acknowledgment to a subscribe request.
Definition: mqtt.h:72
@ MQTT_EVT_DISCONNECT
Disconnection Event.
Definition: mqtt.h:48
@ MQTT_EVT_UNSUBACK
Acknowledgment to a unsubscribe request.
Definition: mqtt.h:75
@ MQTT_EVT_PUBLISH
Publish event received when message is published on a topic client is subscribed to.
Definition: mqtt.h:57
@ MQTT_EVT_PUBREL
Release of published message with QoS 2.
Definition: mqtt.h:66
@ MQTT_EVT_CONNACK
Acknowledgment of connection request.
Definition: mqtt.h:43
@ MQTT_EVT_PUBCOMP
Confirmation to a publish release message with QoS 2.
Definition: mqtt.h:69
@ MQTT_QOS_0_AT_MOST_ONCE
Lowest Quality of Service, no acknowledgment needed for published message.
Definition: mqtt.h:92
@ MQTT_QOS_1_AT_LEAST_ONCE
Medium Quality of Service, if acknowledgment expected for published message, duplicate messages permi...
Definition: mqtt.h:97
@ MQTT_QOS_2_EXACTLY_ONCE
Highest Quality of Service, acknowledgment expected and message shall be published only once.
Definition: mqtt.h:103
@ MQTT_VERSION_3_1_0
Protocol level for 3.1.0.
Definition: mqtt.h:83
@ MQTT_VERSION_3_1_1
Protocol level for 3.1.1.
Definition: mqtt.h:84
@ MQTT_CONNECTION_ACCEPTED
Connection accepted.
Definition: mqtt.h:109
@ MQTT_NOT_AUTHORIZED
The Client is not authorized to connect.
Definition: mqtt.h:130
@ MQTT_UNACCEPTABLE_PROTOCOL_VERSION
The Server does not support the level of the MQTT protocol requested by the Client.
Definition: mqtt.h:114
@ MQTT_BAD_USER_NAME_OR_PASSWORD
The data in the user name or password is malformed.
Definition: mqtt.h:127
@ MQTT_IDENTIFIER_REJECTED
The Client identifier is correct UTF-8 but not allowed by the Server.
Definition: mqtt.h:119
@ MQTT_SERVER_UNAVAILABLE
The Network Connection has been made but the MQTT service is unavailable.
Definition: mqtt.h:124
@ MQTT_SUBACK_FAILURE
Subscription for a topic failed.
Definition: mqtt.h:145
@ MQTT_SUBACK_SUCCESS_QoS_0
Subscription with QoS 0 succeeded.
Definition: mqtt.h:136
@ MQTT_SUBACK_SUCCESS_QoS_2
Subscription with QoS 2 succeeded.
Definition: mqtt.h:142
@ MQTT_SUBACK_SUCCESS_QoS_1
Subscription with QoS 1 succeeded.
Definition: mqtt.h:139
@ MQTT_TRANSPORT_NUM
Shall not be used as a transport type.
Definition: mqtt.h:394
@ MQTT_TRANSPORT_NON_SECURE
Use non secure TCP transport for MQTT connection.
Definition: mqtt.h:371
int sec_tag_t
Secure tag, a reference to TLS credential.
Definition: tls_credentials.h:70
Public kernel APIs.
IPv6 and IPv4 definitions.
__UINT32_TYPE__ uint32_t
Definition: stdint.h:90
__INT32_TYPE__ int32_t
Definition: stdint.h:74
__UINT8_TYPE__ uint8_t
Definition: stdint.h:88
__UINT16_TYPE__ uint16_t
Definition: stdint.h:89
__INT8_TYPE__ int8_t
Definition: stdint.h:72
Abstracts binary strings.
Definition: mqtt.h:167
uint8_t * data
Pointer to binary stream.
Definition: mqtt.h:168
uint32_t len
Length of binary stream.
Definition: mqtt.h:169
MQTT Client definition to maintain information relevant to the client.
Definition: mqtt.h:477
struct mqtt_internal internal
MQTT client internal state.
Definition: mqtt.h:479
mqtt_evt_cb_t evt_cb
Application callback registered with the module to get MQTT events.
Definition: mqtt.h:513
struct mqtt_topic * will_topic
Will topic and QoS.
Definition: mqtt.h:504
uint32_t tx_buf_size
Size of transmit buffer.
Definition: mqtt.h:525
uint8_t protocol_version
MQTT protocol version.
Definition: mqtt.h:533
uint32_t rx_buf_size
Size of receive buffer.
Definition: mqtt.h:519
int8_t unacked_ping
Unanswered PINGREQ count on this connection.
Definition: mqtt.h:536
const void * broker
Broker details, for example, address, port.
Definition: mqtt.h:490
struct mqtt_utf8 * will_message
Will message.
Definition: mqtt.h:509
uint8_t * rx_buf
Receive buffer used for MQTT packet reception in RX path.
Definition: mqtt.h:516
uint16_t keepalive
Keepalive interval for this client in seconds.
Definition: mqtt.h:530
struct mqtt_utf8 client_id
Unique client identification to be used for the connection.
Definition: mqtt.h:485
uint8_t clean_session
Clean session flag indicating a fresh (1) or a retained session (0).
Definition: mqtt.h:545
struct mqtt_utf8 * user_name
User name (if any) to be used for the connection.
Definition: mqtt.h:495
struct mqtt_transport transport
MQTT transport configuration and data.
Definition: mqtt.h:482
uint8_t will_retain
Will retain flag, 1 if will message shall be retained persistently.
Definition: mqtt.h:540
uint8_t * tx_buf
Transmit buffer used for creating MQTT packet in TX path.
Definition: mqtt.h:522
struct mqtt_utf8 * password
Password (if any) to be used for the connection.
Definition: mqtt.h:501
Parameters for a connection acknowledgment (CONNACK).
Definition: mqtt.h:192
enum mqtt_conn_return_code return_code
The appropriate non-zero Connect return code indicates if the Server is unable to process a connectio...
Definition: mqtt.h:202
uint8_t session_present_flag
The Session Present flag enables a Client to establish whether the Client and Server have a consisten...
Definition: mqtt.h:197
Defines MQTT asynchronous event notified to the application.
Definition: mqtt.h:314
enum mqtt_evt_type type
Identifies the event.
Definition: mqtt.h:316
int result
Event result.
Definition: mqtt.h:324
union mqtt_evt_param param
Contains parameters (if any) accompanying the event.
Definition: mqtt.h:319
MQTT internal state.
Definition: mqtt.h:454
uint32_t rx_buf_datalen
Internal.
Definition: mqtt.h:467
uint32_t last_activity
Internal.
Definition: mqtt.h:461
uint32_t remaining_payload
Internal.
Definition: mqtt.h:470
uint32_t state
Internal.
Definition: mqtt.h:464
struct sys_mutex mutex
Internal.
Definition: mqtt.h:456
Parameters for MQTT publish acknowledgment (PUBACK).
Definition: mqtt.h:206
uint16_t message_id
Message id of the PUBLISH message being acknowledged.
Definition: mqtt.h:208
Parameters for MQTT publish complete (PUBCOMP).
Definition: mqtt.h:224
uint16_t message_id
Message id of the PUBREL message being acknowledged.
Definition: mqtt.h:226
Parameters for a publish message.
Definition: mqtt.h:186
struct mqtt_binstr payload
Payload on the topic published.
Definition: mqtt.h:188
struct mqtt_topic topic
Topic on which data was published.
Definition: mqtt.h:187
Parameters for a publish message (PUBLISH).
Definition: mqtt.h:246
uint8_t dup_flag
Duplicate flag.
Definition: mqtt.h:258
struct mqtt_publish_message message
Messages including topic, QoS and its payload (if any) to be published.
Definition: mqtt.h:250
uint8_t retain_flag
Retain flag.
Definition: mqtt.h:263
uint16_t message_id
Message id used for the publish message.
Definition: mqtt.h:253
Parameters for MQTT publish receive (PUBREC).
Definition: mqtt.h:212
uint16_t message_id
Message id of the PUBLISH message being acknowledged.
Definition: mqtt.h:214
Parameters for MQTT publish release (PUBREL).
Definition: mqtt.h:218
uint16_t message_id
Message id of the PUBREC message being acknowledged.
Definition: mqtt.h:220
TLS configuration for secure MQTT transports.
Definition: mqtt.h:341
int peer_verify
Indicates the preference for peer verification.
Definition: mqtt.h:343
const int * cipher_list
Indicates the list of ciphers to be used for the session.
Definition: mqtt.h:351
uint32_t cipher_count
Indicates the number of entries in the cipher list.
Definition: mqtt.h:346
int cert_nocopy
Indicates the preference for copying certificates to the heap.
Definition: mqtt.h:365
uint32_t sec_tag_count
Indicates the number of entries in the sec tag list.
Definition: mqtt.h:354
const sec_tag_t * sec_tag_list
Indicates the list of security tags to be used for the session.
Definition: mqtt.h:357
const char * hostname
Peer hostname for ceritificate verification.
Definition: mqtt.h:362
Parameters for MQTT subscription acknowledgment (SUBACK).
Definition: mqtt.h:230
struct mqtt_binstr return_codes
Return codes indicating maximum QoS level granted for each topic in the subscription list.
Definition: mqtt.h:236
uint16_t message_id
Message id of the SUBSCRIBE message being acknowledged.
Definition: mqtt.h:232
List of topics in a subscription request.
Definition: mqtt.h:267
uint16_t message_id
Message id used to identify subscription request.
Definition: mqtt.h:275
uint16_t list_count
Number of topics in the subscription list.
Definition: mqtt.h:272
struct mqtt_topic * list
Array containing topics along with QoS for each.
Definition: mqtt.h:269
Abstracts MQTT UTF-8 encoded topic that can be subscribed to or published.
Definition: mqtt.h:175
uint8_t qos
Quality of service requested for the subscription.
Definition: mqtt.h:182
struct mqtt_utf8 topic
Topic on to be published or subscribed to.
Definition: mqtt.h:177
MQTT transport specific data.
Definition: mqtt.h:398
int sock
Socket descriptor.
Definition: mqtt.h:409
enum mqtt_transport_type type
Transport type selection for client instance.
Definition: mqtt.h:403
struct mqtt_transport::@292::@294 tcp
Parameters for MQTT unsubscribe acknowledgment (UNSUBACK).
Definition: mqtt.h:240
uint16_t message_id
Message id of the UNSUBSCRIBE message being acknowledged.
Definition: mqtt.h:242
Abstracts UTF-8 encoded strings.
Definition: mqtt.h:149
uint32_t size
Size of UTF string, in bytes.
Definition: mqtt.h:151
const uint8_t * utf8
Pointer to UTF-8 string.
Definition: mqtt.h:150
Generic sockaddr struct.
Definition: net_ip.h:347
Definition: mutex.h:28
Websocket client connection request.
Definition: websocket.h:67
TLS credentials management.
Defines event parameters notified along with asynchronous events to the application.
Definition: mqtt.h:282
struct mqtt_pubrel_param pubrel
Parameters accompanying MQTT_EVT_PUBREL event.
Definition: mqtt.h:301
struct mqtt_puback_param puback
Parameters accompanying MQTT_EVT_PUBACK event.
Definition: mqtt.h:295
struct mqtt_pubrec_param pubrec
Parameters accompanying MQTT_EVT_PUBREC event.
Definition: mqtt.h:298
struct mqtt_publish_param publish
Parameters accompanying MQTT_EVT_PUBLISH event.
Definition: mqtt.h:292
struct mqtt_connack_param connack
Parameters accompanying MQTT_EVT_CONNACK event.
Definition: mqtt.h:284
struct mqtt_pubcomp_param pubcomp
Parameters accompanying MQTT_EVT_PUBCOMP event.
Definition: mqtt.h:304
struct mqtt_unsuback_param unsuback
Parameters accompanying MQTT_EVT_UNSUBACK event.
Definition: mqtt.h:310
struct mqtt_suback_param suback
Parameters accompanying MQTT_EVT_SUBACK event.
Definition: mqtt.h:307
Websocket API.