Contiki-NG
|
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) |
Connects to a MQTT broker. More... | |
void | mqtt_disconnect (struct mqtt_connection *conn) |
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) |
Subscribes to a MQTT topic. More... | |
mqtt_status_t | mqtt_unsubscribe (struct mqtt_connection *conn, uint16_t *mid, char *topic) |
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) |
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) |
Set the last will topic and message for a MQTT client. More... | |
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:
A mechanism, Last Will, to notify interested parties when an abnormal disconnection occurs.
The protocol specification and other useful information can be found here: http://mqtt.org
typedef void(* mqtt_event_callback_t) (struct mqtt_connection *m, mqtt_event_t event, void *data) |
MQTT event callback function.
m | A pointer to a MQTT connection |
event | The event number |
data | A 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.
mqtt_status_t mqtt_connect | ( | struct mqtt_connection * | conn, |
char * | host, | ||
uint16_t | port, | ||
uint16_t | keep_alive, | ||
uint8_t | clean_session | ||
) |
Connects to a MQTT broker.
conn | A pointer to the MQTT connection. |
host | IP address of the broker to connect to. |
port | Port of the broker to connect to, default is MQTT port is 1883. |
keep_alive | Keep 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_session | Request a new session and discard pending messages with QoS > 0, as well as client subscriptions |
This function connects to a MQTT broker.
void mqtt_disconnect | ( | struct mqtt_connection * | conn | ) |
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 | ||
) |
Publish to a MQTT topic.
conn | A pointer to the MQTT connection. |
mid | A pointer to message ID. |
topic | A pointer to the topic to subscribe to. |
payload | A pointer to the topic payload. |
payload_size | Payload size. |
qos_level | Quality Of Service level to use. Currently supports 0, 1. |
retain | If 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 |
This function publishes to a topic on 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.
conn | A pointer to the MQTT connection. |
app_process | A pointer to the application process handling the MQTT connection. |
client_id | A pointer to the MQTT client ID. |
event_callback | Callback function responsible for handling the callback from MQTT engine. |
max_segment_size | The TCP segment size to use for this MQTT/TCP connection. |
This function initializes the MQTT engine and shall be called before any other MQTT function.
void mqtt_set_last_will | ( | struct mqtt_connection * | conn, |
char * | topic, | ||
char * | message, | ||
mqtt_qos_level_t | qos | ||
) |
Set the last will topic and message for a MQTT client.
conn | A pointer to the MQTT connection. |
topic | A pointer to the Last Will topic. |
message | A pointer to the Last Will message (payload). |
qos | The desired QoS level. |
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.
void mqtt_set_username_password | ( | struct mqtt_connection * | conn, |
char * | username, | ||
char * | password | ||
) |
Set the user name and password for a MQTT client.
conn | A pointer to the MQTT connection. |
username | A pointer to the user name. |
password | A pointer to the password. |
This function sets clients user name and password to use when connecting to a MQTT broker.
mqtt_status_t mqtt_subscribe | ( | struct mqtt_connection * | conn, |
uint16_t * | mid, | ||
char * | topic, | ||
mqtt_qos_level_t | qos_level | ||
) |
Subscribes to a MQTT topic.
conn | A pointer to the MQTT connection. |
mid | A pointer to message ID. |
topic | A pointer to the topic to subscribe to. |
qos_level | Quality Of Service level to use. Currently supports 0, 1. |
This function subscribes to a topic on a MQTT broker.
mqtt_status_t mqtt_unsubscribe | ( | struct mqtt_connection * | conn, |
uint16_t * | mid, | ||
char * | topic | ||
) |