Contiki-NG
Files | Typedefs | Enumerations | Functions

This application is an engine for MQTT v3.1. More...

Files

file  mqtt.c
 Implementation of the Contiki MQTT engine.
 
file  mqtt.h
 Header file for the Contiki MQTT engine.
 

Typedefs

typedef void(* mqtt_event_callback_t) (struct mqtt_connection *m, mqtt_event_t event, void *data)
 MQTT event callback function. More...
 

Enumerations

enum  mqtt_event_t
 MQTT engine events.
 

Functions

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. More...
 
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. More...
 
void mqtt_disconnect (struct mqtt_connection *conn, struct mqtt_prop_list *prop_list)
 Disconnects from a MQTT broker. More...
 
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. More...
 
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. More...
 
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. More...
 
void mqtt_set_username_password (struct mqtt_connection *conn, char *username, char *password)
 Set the user name and password for a MQTT client. More...
 
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. More...
 
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). More...
 

Detailed Description

This application is an engine for MQTT v3.1.

It supports QoS Levels 0 and 1.

MQTT is a Client Server publish/subscribe messaging transport protocol. It is light weight, open, simple, and designed so as to be easy to implement. These characteristics make it ideal for use in many situations, including constrained environments such as for communication in Machine to Machine (M2M) and Internet of Things (IoT) contexts where a small code footprint is required and/or network bandwidth is at a premium.

The protocol runs over TCP/IP, more specifically tcp_socket. Its features include:

Typedef Documentation

◆ mqtt_event_callback_t

typedef void(* mqtt_event_callback_t) (struct mqtt_connection *m, mqtt_event_t event, void *data)

MQTT event callback function.

Parameters
mA pointer to a MQTT connection
eventThe event number
dataA user-defined pointer

The MQTT socket event callback function gets called whenever there is an event on a MQTT connection, such as the connection getting connected or closed.

Definition at line 499 of file mqtt.h.

Function Documentation

◆ mqtt_auth()

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).

Parameters
connA pointer to the MQTT connection.
auth_typeThe type of auth to send (continue authentication or re-authentication).
prop_listOutput properties.
Returns
MQTT_STATUS_OK or some error status

This function send an MQTT authentication message.

Definition at line 2095 of file mqtt.c.

◆ mqtt_connect()

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.

Parameters
connA pointer to the MQTT connection.
hostIP address of the broker to connect to.
portPort of the broker to connect to, default is MQTT port is 1883.
keep_aliveKeep alive timer in seconds. Used by broker to handle client disc. Defines the maximum time interval between two messages from the client. Shall be min 1.5 x report interval.
clean_sessionRequest a new session and discard pending messages with QoS > 0, as well as client subscriptions
prop_listOutput properties (MQTTv5-only).
Returns
MQTT_STATUS_OK or an error status

This function connects to a MQTT broker.

Definition at line 1816 of file mqtt.c.

◆ mqtt_disconnect()

void mqtt_disconnect ( struct mqtt_connection *  conn,
struct mqtt_prop_list *  prop_list 
)

Disconnects from a MQTT broker.

Parameters
connA pointer to the MQTT connection.
prop_listOutput properties (MQTTv5-only).

This function disconnects from a MQTT broker.

Definition at line 1868 of file mqtt.c.

◆ mqtt_publish()

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.

Parameters
connA pointer to the MQTT connection.
midA pointer to message ID.
topicA pointer to the topic to subscribe to.
payloadA pointer to the topic payload.
payload_sizePayload size.
qos_levelQuality Of Service level to use. Currently supports 0, 1.
retainIf the RETAIN flag is set to 1, in a PUBLISH Packet sent by a Client to a Server, the Server MUST store the Application Message and its QoS, so that it can be delivered to future subscribers whose subscriptions match its topic name
topic_aliasTopic alias to send (MQTTv5-only).
topic_alias_enControl whether or not to discard topic and only send topic alias s(MQTTv5-only).
prop_listOutput properties (MQTTv5-only).
Returns
MQTT_STATUS_OK or some error status

This function publishes to a topic on a MQTT broker.

Definition at line 1979 of file mqtt.c.

◆ mqtt_register()

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.

Parameters
connA pointer to the MQTT connection.
app_processA pointer to the application process handling the MQTT connection.
client_idA pointer to the MQTT client ID.
event_callbackCallback function responsible for handling the callback from MQTT engine.
max_segment_sizeThe TCP segment size to use for this MQTT/TCP connection.
Returns
MQTT_STATUS_OK or MQTT_STATUS_INVALID_ARGS_ERROR

This function initializes the MQTT engine and shall be called before any other MQTT function.

Definition at line 1777 of file mqtt.c.

◆ mqtt_set_last_will()

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.

Parameters
connA pointer to the MQTT connection.
topicA pointer to the Last Will topic.
messageA pointer to the Last Will message (payload).
qosThe desired QoS level.
will_propsWill message properties (MQTTv5-only).

This function sets clients Last Will topic and message (payload). If the Will Flag is set to 1 (using the function) this indicates that, if the Connect request is accepted, a Will Message MUST be stored on the Server and associated with the Network Connection. The Will Message MUST be published when the Network Connection is subsequently closed.

This functionality can be used to get notified that a device has disconnected from the broker.

Definition at line 2062 of file mqtt.c.

◆ mqtt_set_username_password()

void mqtt_set_username_password ( struct mqtt_connection *  conn,
char *  username,
char *  password 
)

Set the user name and password for a MQTT client.

Parameters
connA pointer to the MQTT connection.
usernameA pointer to the user name.
passwordA pointer to the password.

This function sets clients user name and password to use when connecting to a MQTT broker.

Definition at line 2041 of file mqtt.c.

◆ mqtt_subscribe()

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.

Parameters
connA pointer to the MQTT connection.
midA pointer to message ID.
topicA pointer to the topic to subscribe to.
qos_levelQuality Of Service level to use. Currently supports 0, 1.
nlNo Local (MQTTv5-only).
rapRetain As Published (MQTTv5-only).
ret_handlingRetain handling options (MQTTv5-only).
prop_listOutput properties (MQTTv5-only).
Returns
MQTT_STATUS_OK or some error status

This function subscribes to a topic on a MQTT broker.

Definition at line 1888 of file mqtt.c.

◆ mqtt_unsubscribe()

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.

Parameters
connA pointer to the MQTT connection.
midA pointer to message ID.
topicA pointer to the topic to unsubscribe from.
prop_listOutput properties (MQTTv5-only).
Returns
MQTT_STATUS_OK or some error status

This function unsubscribes from a topic on a MQTT broker.

Definition at line 1940 of file mqtt.c.