87#include "contiki-net.h"
88#include "contiki-lib.h"
89#include "lib/random.h"
96#include "tcp-socket.h"
97#include "udp-socket.h"
104#define MQTT_PROTOCOL_VERSION_3_1 3
105#define MQTT_PROTOCOL_VERSION_3_1_1 4
106#define MQTT_PROTOCOL_VERSION_5 5
108#ifdef MQTT_CONF_VERSION
109#define MQTT_PROTOCOL_VERSION MQTT_CONF_VERSION
111#define MQTT_PROTOCOL_VERSION MQTT_PROTOCOL_VERSION_3_1
114#if MQTT_PROTOCOL_VERSION == MQTT_PROTOCOL_VERSION_5
116#elif MQTT_PROTOCOL_VERSION == MQTT_PROTOCOL_VERSION_3_1_1
118#elif MQTT_PROTOCOL_VERSION == MQTT_PROTOCOL_VERSION_3_1
122#define MQTT_CLIENT_ID_MAX_LEN 23
125#define MQTT_TCP_INPUT_BUFF_SIZE 512
126#define MQTT_TCP_OUTPUT_BUFF_SIZE 512
128#define MQTT_INPUT_BUFF_SIZE 512
129#define MQTT_MAX_TOPIC_LENGTH 64
130#define MQTT_MAX_TOPICS_PER_SUBSCRIBE 1
132#define MQTT_FHDR_SIZE 1
133#define MQTT_MAX_REMAINING_LENGTH_BYTES 4
135#define MQTT_PROTOCOL_NAME "MQIsdp"
137#define MQTT_PROTOCOL_NAME "MQTT"
140#define MQTT_TOPIC_MAX_LENGTH 128
142#if MQTT_PROTOCOL_VERSION >= MQTT_PROTOCOL_VERSION_3_1_1
143#ifdef MQTT_CONF_SUPPORTS_EMPTY_CLIENT_ID
144#define MQTT_SRV_SUPPORTS_EMPTY_CLIENT_ID MQTT_CONF_SUPPORTS_EMPTY_CLIENT_ID
146#define MQTT_SRV_SUPPORTS_EMPTY_CLIENT_ID 0
149#define MQTT_SRV_SUPPORTS_EMPTY_CLIENT_ID 0
166#define MQTT_CONNECT_VHDR_SIZE 12
179#define MQTT_CONNECT_VHDR_SIZE 10
182#define MQTT_STRING_LEN_SIZE 2
183#define MQTT_MID_SIZE 2
184#define MQTT_QOS_SIZE 1
193#define DBG(...) printf(__VA_ARGS__)
198extern process_event_t mqtt_update_event;
201struct mqtt_connection;
203struct mqtt_prop_list;
211 MQTT_CLEAN_SESSION_OFF,
212 MQTT_CLEAN_SESSION_ON,
213} mqtt_clean_session_t;
219 MQTT_EVENT_CONNECTED,
220 MQTT_EVENT_DISCONNECTED,
228 MQTT_EVENT_ERROR = 0x80,
229 MQTT_EVENT_PROTOCOL_ERROR,
230 MQTT_EVENT_CONNECTION_REFUSED_ERROR,
231 MQTT_EVENT_DNS_ERROR,
232 MQTT_EVENT_NOT_IMPLEMENTED_ERROR,
241 MQTT_STATUS_OUT_QUEUE_FULL,
244 MQTT_STATUS_ERROR = 0x80,
245 MQTT_STATUS_NOT_CONNECTED_ERROR,
246 MQTT_STATUS_INVALID_ARGS_ERROR,
247 MQTT_STATUS_DNS_ERROR,
257 MQTT_QOS_STATE_NO_ACK,
258 MQTT_QOS_STATE_GOT_ACK,
275 MQTT_CONN_STATE_ERROR,
276 MQTT_CONN_STATE_DNS_ERROR,
277 MQTT_CONN_STATE_DISCONNECTING,
278 MQTT_CONN_STATE_ABORT_IMMEDIATE,
279 MQTT_CONN_STATE_NOT_CONNECTED,
280 MQTT_CONN_STATE_DNS_LOOKUP,
281 MQTT_CONN_STATE_TCP_CONNECTING,
282 MQTT_CONN_STATE_TCP_CONNECTED,
283 MQTT_CONN_STATE_CONNECTING_TO_BROKER,
284 MQTT_CONN_STATE_CONNECTED_TO_BROKER,
285 MQTT_CONN_STATE_SENDING_MQTT_DISCONNECT,
289 MQTT_FHDR_MSG_TYPE_CONNECT = 0x10,
290 MQTT_FHDR_MSG_TYPE_CONNACK = 0x20,
291 MQTT_FHDR_MSG_TYPE_PUBLISH = 0x30,
292 MQTT_FHDR_MSG_TYPE_PUBACK = 0x40,
293 MQTT_FHDR_MSG_TYPE_PUBREC = 0x50,
294 MQTT_FHDR_MSG_TYPE_PUBREL = 0x60,
295 MQTT_FHDR_MSG_TYPE_PUBCOMP = 0x70,
296 MQTT_FHDR_MSG_TYPE_SUBSCRIBE = 0x80,
297 MQTT_FHDR_MSG_TYPE_SUBACK = 0x90,
298 MQTT_FHDR_MSG_TYPE_UNSUBSCRIBE = 0xA0,
299 MQTT_FHDR_MSG_TYPE_UNSUBACK = 0xB0,
300 MQTT_FHDR_MSG_TYPE_PINGREQ = 0xC0,
301 MQTT_FHDR_MSG_TYPE_PINGRESP = 0xD0,
302 MQTT_FHDR_MSG_TYPE_DISCONNECT = 0xE0,
303 MQTT_FHDR_MSG_TYPE_AUTH = 0xF0,
308 MQTT_VHDR_PROP_ANY = 0x00,
309 MQTT_VHDR_PROP_PAYLOAD_FMT_IND = 0x01,
310 MQTT_VHDR_PROP_MSG_EXP_INT = 0x02,
311 MQTT_VHDR_PROP_CONTENT_TYPE = 0x03,
312 MQTT_VHDR_PROP_RESP_TOPIC = 0x08,
313 MQTT_VHDR_PROP_CORRELATION_DATA = 0x09,
314 MQTT_VHDR_PROP_SUB_ID = 0x0B,
315 MQTT_VHDR_PROP_SESS_EXP_INT = 0x11,
316 MQTT_VHDR_PROP_ASSIGNED_CLIENT_ID = 0x12,
317 MQTT_VHDR_PROP_SERVER_KEEP_ALIVE = 0x13,
318 MQTT_VHDR_PROP_AUTH_METHOD = 0x15,
319 MQTT_VHDR_PROP_AUTH_DATA = 0x16,
320 MQTT_VHDR_PROP_REQ_PROBLEM_INFO = 0x17,
321 MQTT_VHDR_PROP_WILL_DELAY_INT = 0x18,
322 MQTT_VHDR_PROP_REQ_RESP_INFO = 0x19,
323 MQTT_VHDR_PROP_RESP_INFO = 0x1A,
324 MQTT_VHDR_PROP_SERVER_REFERENCE = 0x1C,
325 MQTT_VHDR_PROP_REASON_STRING = 0x1F,
326 MQTT_VHDR_PROP_RECEIVE_MAX = 0x21,
327 MQTT_VHDR_PROP_TOPIC_ALIAS_MAX = 0x22,
328 MQTT_VHDR_PROP_TOPIC_ALIAS = 0x23,
329 MQTT_VHDR_PROP_MAX_QOS = 0x24,
330 MQTT_VHDR_PROP_RETAIN_AVAIL = 0x25,
331 MQTT_VHDR_PROP_USER_PROP = 0x26,
332 MQTT_VHDR_PROP_MAX_PKT_SZ = 0x27,
333 MQTT_VHDR_PROP_WILD_SUB_AVAIL = 0x28,
334 MQTT_VHDR_PROP_SUB_ID_AVAIL = 0x29,
335 MQTT_VHDR_PROP_SHARED_SUB_AVAIL = 0x2A,
340 MQTT_CAP_RETAIN_AVAIL = 0x00,
341 MQTT_CAP_WILD_SUB_AVAIL = 0x01,
342 MQTT_CAP_SUB_ID_AVAIL = 0x02,
343 MQTT_CAP_SHARED_SUB_AVAIL = 0x04,
344} mqtt_srv_capability_t;
352 MQTT_TOPIC_ALIAS_OFF,
354} mqtt_topic_alias_en_t;
357 MQTT_SUB_OPTION_QOS = 0x03,
358 MQTT_SUB_OPTION_NL = 0x04,
359 MQTT_SUB_OPTION_RAP = 0x08,
360 MQTT_SUB_OPTION_RETAIN_HANDLING = 0x30,
374 MQTT_RET_H_SEND_ALL = 0x00,
375 MQTT_RET_H_SEND_NEW = 0x01,
376 MQTT_RET_H_SEND_NONE = 0x02,
377} mqtt_retain_handling_t;
391struct mqtt_suback_event {
393 mqtt_qos_level_t qos_level;
400struct mqtt_connack_event {
401 uint8_t session_present;
412 char topic[MQTT_MAX_TOPIC_LENGTH + 1];
414 uint8_t *payload_chunk;
415 uint16_t payload_chunk_length;
418 uint16_t payload_length;
419 uint16_t payload_left;
423struct mqtt_in_packet {
425 struct mqtt_connection *next;
429 uint32_t byte_counter;
430 uint8_t packet_received;
433 uint16_t remaining_length;
437 uint8_t has_remaining_length;
442 uint16_t payload_pos;
443 uint8_t payload[MQTT_INPUT_BUFF_SIZE];
446 uint8_t *payload_start;
451 uint8_t topic_len_received;
452 uint8_t topic_received;
456 uint8_t has_reason_code;
460 uint8_t properties_enc_len;
461 uint16_t properties_len;
462 uint8_t *props_start;
463 uint8_t *curr_props_pos;
468struct mqtt_out_packet {
470 uint32_t remaining_length;
471 uint8_t remaining_length_enc[MQTT_MAX_REMAINING_LENGTH_BYTES];
472 uint8_t remaining_length_enc_bytes;
475 uint16_t topic_length;
477 uint32_t payload_size;
478 mqtt_qos_level_t qos;
479 mqtt_qos_state_t qos_state;
480 mqtt_retain_t retain;
485 uint8_t auth_reason_code;
503typedef void (*mqtt_topic_callback_t)(
struct mqtt_connection *m,
504 struct mqtt_message *msg);
507 struct mqtt_string topic;
508 struct mqtt_string message;
509 mqtt_qos_level_t qos;
515struct mqtt_credentials {
516 struct mqtt_string username;
517 struct mqtt_string password;
520struct mqtt_connection {
522 struct mqtt_connection *next;
525 struct mqtt_string client_id;
527 uint8_t connect_vhdr_flags;
528 uint8_t auto_reconnect;
531 struct ctimer keep_alive_timer;
532 uint8_t waiting_for_pingresp;
534 struct mqtt_will will;
535 struct mqtt_credentials credentials;
537 mqtt_conn_state_t state;
541 uint16_t mid_counter;
544 uint8_t out_queue_full;
545 struct process *app_process;
548 uint8_t *out_buffer_ptr;
549 uint8_t out_buffer[MQTT_TCP_OUTPUT_BUFF_SIZE];
550 uint8_t out_buffer_sent;
551 struct mqtt_out_packet out_packet;
552 struct pt out_proto_thread;
553 uint32_t out_write_pos;
554 uint16_t max_segment_size;
557 uint8_t in_buffer[MQTT_TCP_INPUT_BUFF_SIZE];
558 struct mqtt_in_packet in_packet;
559 struct mqtt_message in_publish_msg;
563 uip_ipaddr_t server_ip;
564 uint16_t server_port;
565 struct tcp_socket socket;
570 uint8_t srv_feature_en;
571 struct mqtt_prop_list *out_props;
592 struct process *app_process,
595 uint16_t max_segment_size);
617 uint8_t clean_session,
618 struct mqtt_prop_list *prop_list);
620 uint8_t clean_session);
632 struct mqtt_prop_list *prop_list);
655 mqtt_qos_level_t qos_level,
656 mqtt_nl_en_t nl, mqtt_rap_en_t rap,
657 mqtt_retain_handling_t ret_handling,
658 struct mqtt_prop_list *prop_list);
660 mqtt_qos_level_t qos_level);
677 struct mqtt_prop_list *prop_list);
706 uint32_t payload_size,
707 mqtt_qos_level_t qos_level,
709 mqtt_retain_t retain,
711 mqtt_topic_alias_en_t topic_alias_en,
712 struct mqtt_prop_list *prop_list);
714 mqtt_retain_t retain);
752 mqtt_qos_level_t qos,
753 struct mqtt_prop_list *will_props);
755 mqtt_qos_level_t qos);
758#define mqtt_connected(conn) \
759 ((conn)->state == MQTT_CONN_STATE_CONNECTED_TO_BROKER ? 1 : 0)
761#define mqtt_ready(conn) \
762 (!(conn)->out_queue_full && mqtt_connected((conn)))
764void mqtt_encode_var_byte_int(uint8_t *vbi_out,
768uint8_t mqtt_decode_var_byte_int(
const uint8_t *input_data_ptr,
771 uint32_t *pkt_byte_count,
784mqtt_status_t
mqtt_auth(
struct mqtt_connection *conn,
785 mqtt_auth_type_t auth_type,
786 struct mqtt_prop_list *prop_list);
Header file for the callback timer.
#define LIST_STRUCT(name)
Declare a linked list inside a structure declaraction.
mqtt_status_t mqtt_auth(struct mqtt_connection *conn, mqtt_auth_type_t auth_type, struct mqtt_prop_list *prop_list)
Send authentication message (MQTTv5-only).
mqtt_status_t mqtt_connect(struct mqtt_connection *conn, char *host, uint16_t port, uint16_t keep_alive, uint8_t clean_session, struct mqtt_prop_list *prop_list)
Connects to a MQTT broker.
mqtt_status_t mqtt_register(struct mqtt_connection *conn, struct process *app_process, char *client_id, mqtt_event_callback_t event_callback, uint16_t max_segment_size)
Initializes the MQTT engine.
mqtt_status_t mqtt_unsubscribe(struct mqtt_connection *conn, uint16_t *mid, char *topic, struct mqtt_prop_list *prop_list)
Unsubscribes from a MQTT topic.
void(* mqtt_event_callback_t)(struct mqtt_connection *m, mqtt_event_t event, void *data)
MQTT event callback function.
mqtt_status_t mqtt_subscribe(struct mqtt_connection *conn, uint16_t *mid, char *topic, mqtt_qos_level_t qos_level, mqtt_nl_en_t nl, mqtt_rap_en_t rap, mqtt_retain_handling_t ret_handling, struct mqtt_prop_list *prop_list)
Subscribes to a MQTT topic.
void mqtt_disconnect(struct mqtt_connection *conn, struct mqtt_prop_list *prop_list)
Disconnects from a MQTT broker.
mqtt_status_t mqtt_publish(struct mqtt_connection *conn, uint16_t *mid, char *topic, uint8_t *payload, uint32_t payload_size, mqtt_qos_level_t qos_level, mqtt_retain_t retain, uint8_t topic_alias, mqtt_topic_alias_en_t topic_alias_en, struct mqtt_prop_list *prop_list)
Publish to a MQTT topic.
mqtt_event_t
MQTT engine events.
void mqtt_set_username_password(struct mqtt_connection *conn, char *username, char *password)
Set the user name and password for a MQTT client.
void mqtt_set_last_will(struct mqtt_connection *conn, char *topic, char *message, mqtt_qos_level_t qos, struct mqtt_prop_list *will_props)
Set the last will topic and message for a MQTT client.
Header file for the LED HAL.
Header file for IPv6-related data structures.
Header file for the uIP TCP/IP stack.