picofuse

mqtt.h
Go to the documentation of this file.
1 
40 #pragma once
41 #include <picofuse/net/types.h>
42 #include <stdbool.h>
43 #include <stddef.h>
44 
45 #ifdef __cplusplus
46 extern "C" {
47 #endif
48 
54 #define NET_MQTT_PORT 1883
55 
62 #ifndef NET_MQTT_TOPIC_CAPACITY
63 #define NET_MQTT_TOPIC_CAPACITY 8
64 #endif
65 
75 #ifndef NET_MQTT_TOPIC_FILTER_SIZE
76 #define NET_MQTT_TOPIC_FILTER_SIZE 128
77 #endif
78 
80 // TYPES
81 
90 typedef struct net_mqtt_t net_mqtt_t;
91 
99 typedef enum {
106 
120 typedef struct {
121  const char *client_id;
122  const char *username;
124  const char *password;
126  uint16_t keepalive_s;
138 
143 typedef enum {
161 
166 typedef struct {
167  const char *topic;
168  uint32_t message_id;
173 
178 typedef struct {
179  net_mqtt_qos_t granted_qos;
180  uint32_t topic_id;
191 
196 typedef struct {
197  uint32_t topic_id;
204 
209 typedef struct {
210  const char *topic;
211  const void *payload;
216  size_t payload_len;
228  bool retain;
229  uint32_t topic_id;
243 
252 typedef enum {
265 
270 typedef enum {
287 
292 typedef struct {
293  net_mqtt_error_kind_t kind;
294  net_mqtt_error_action_t action;
295  uint32_t message_id;
307 
312 typedef struct {
313  net_mqtt_event_type_t type;
314  union {
315  net_mqtt_sent_t sent;
316  net_mqtt_subscribed_t subscribed;
317  net_mqtt_unsubscribed_t unsubscribed;
318  net_mqtt_received_t received;
319  net_mqtt_error_t error;
320  } data;
322 
337 typedef void (*net_mqtt_event_callback_t)(net_mqtt_t *mqtt,
338  const net_mqtt_event_t *event,
339  void *userdata);
340 
342 // LIFECYCLE
343 
361 
377 net_mqtt_t *net_mqtt_init(const net_addr_t *addr, uint16_t port,
378  uint32_t timeout_ms, const net_mqtt_config_t *config);
379 
395  void *userdata);
396 
405 void net_mqtt_deinit(net_mqtt_t *mqtt);
406 
409 // CONNECTION
411 
428 bool net_mqtt_connect(net_mqtt_t *mqtt);
429 
447 void net_mqtt_disconnect(net_mqtt_t *mqtt);
448 
451 // PUBLISH
453 
516 uint32_t net_mqtt_publish(net_mqtt_t *mqtt, const char *topic,
517  const void *payload, size_t payload_len,
518  net_mqtt_qos_t qos, bool retain);
519 
522 // SUBSCRIBE
524 
578 uint32_t net_mqtt_subscribe(net_mqtt_t *mqtt, const char *topic,
579  net_mqtt_qos_t qos);
580 
614 bool net_mqtt_unsubscribe(net_mqtt_t *mqtt, uint32_t topic_id);
615 
634 const char *net_mqtt_topic_to_string(net_mqtt_t *mqtt, uint32_t topic_id);
635 
646 size_t net_mqtt_error_to_string(const net_mqtt_error_t *error, char *buf,
647  size_t buf_size);
648 
651 #ifdef __cplusplus
652 }
653 #endif
net_mqtt_t * net_mqtt_init(const net_addr_t *addr, uint16_t port, uint32_t timeout_ms, const net_mqtt_config_t *config)
Initialize an MQTT client instance.
A well-formed reply arrived that doesn&#39;t match anything currently pending (wrong packet id...
Definition: mqtt.h:257
const char * topic
Topic the message was published to - may be more specific than the net_mqtt_subscribe() filter that m...
Definition: mqtt.h:210
size_t net_mqtt_error_to_string(const net_mqtt_error_t *error, char *buf, size_t buf_size)
Format a net_mqtt_event_error payload as a human-readable string.
bool net_mqtt_connect(net_mqtt_t *mqtt)
Open the MQTT connection.
uint32_t net_mqtt_subscribe(net_mqtt_t *mqtt, const char *topic, net_mqtt_qos_t qos)
Stage a subscription to a topic filter, waiting for room to do so.
net_mqtt_qos_t
Delivery guarantee for a published or subscribed message.
Definition: mqtt.h:99
Payload for a net_mqtt_event_unsubscribed event.
Definition: mqtt.h:196
Not tied to a specific operation - a packet this client doesn&#39;t recognize at all arrived on the wire...
Definition: mqtt.h:271
The broker sent something this client couldn&#39;t parse as a valid packet.
Definition: mqtt.h:255
net_mqtt_event_type_t
Event payload tag for net_mqtt_event_t.
Definition: mqtt.h:143
uint32_t message_id
The id net_mqtt_publish() returned for the call this event completes - lets a caller with several pub...
Definition: mqtt.h:168
void net_mqtt_default_config(net_mqtt_config_t *config)
Fill an MQTT config struct with safe defaults.
Payload for a net_mqtt_event_received event.
Definition: mqtt.h:209
uint32_t message_id
The id net_mqtt_publish()/net_mqtt_subscribe()/ net_mqtt_unsubscribe() returned for the call this err...
Definition: mqtt.h:295
const char * topic
Topic that was published to.
Definition: mqtt.h:167
void net_mqtt_disconnect(net_mqtt_t *mqtt)
Close the MQTT connection.
bool retain
True if this is a retained message delivered because of the subscription itself rather than a live pu...
Definition: mqtt.h:228
uint32_t net_mqtt_publish(net_mqtt_t *mqtt, const char *topic, const void *payload, size_t payload_len, net_mqtt_qos_t qos, bool retain)
Stage a message to publish to a topic, waiting for room to do so.
Payload for a net_mqtt_event_sent event.
Definition: mqtt.h:166
Exactly once - slowest, a four-way handshake under the hood.
Definition: mqtt.h:103
void(* net_mqtt_event_callback_t)(net_mqtt_t *mqtt, const net_mqtt_event_t *event, void *userdata)
Called for every event on an MQTT client instance.
Definition: mqtt.h:337
Delivery of an incoming PUBLISH (see net_mqtt_event_received) - not tied to any of this client&#39;s own ...
Definition: mqtt.h:281
net_mqtt_subscribe()&#39;s own write, or its SUBACK reply.
Definition: mqtt.h:277
Payload for a net_mqtt_event_subscribed event.
Definition: mqtt.h:178
net_mqtt_publish()&#39;s own write, or its PUBACK/PUBREC/PUBREL/PUBCOMP reply chain.
Definition: mqtt.h:274
bool net_mqtt_unsubscribe(net_mqtt_t *mqtt, uint32_t topic_id)
Stage removal of a confirmed subscription, waiting for room to do so.
net_mqtt_error_kind_t kind
Coarse category - see net_mqtt_error_kind_t.
Definition: mqtt.h:293
net_mqtt_connect() succeeded - carries no payload.
Definition: mqtt.h:144
A single MQTT client event.
Definition: mqtt.h:312
void net_mqtt_deinit(net_mqtt_t *mqtt)
Release a handle from net_mqtt_init().
net_mqtt_unsubscribe()&#39;s own write, or its UNSUBACK reply.
Definition: mqtt.h:279
void net_mqtt_set_callback(net_mqtt_t *mqtt, net_mqtt_event_callback_t callback, void *userdata)
Register the callback for events on an MQTT client instance.
A net_mqtt_subscribe() call completed - see net_mqtt_subscribed_t.
Definition: mqtt.h:152
The connection closed, whether from net_mqtt_disconnect() or an unexpected drop noticed during net_po...
Definition: mqtt.h:146
uint32_t topic_id
The topic_id that was passed to net_mqtt_unsubscribe() - not that call&#39;s own (now-spent) return value...
Definition: mqtt.h:197
net_mqtt_qos_t granted_qos
The QoS the broker actually granted for.
Definition: mqtt.h:179
This client&#39;s own automatic keepalive PINGREQ/PINGRESP.
Definition: mqtt.h:284
Payload for a net_mqtt_event_error event.
Definition: mqtt.h:292
A write to the connection itself failed.
Definition: mqtt.h:253
const char * client_id
MQTT client identifier sent in CONNECT.
Definition: mqtt.h:121
At least once - acknowledged, but may be delivered more than once.
Definition: mqtt.h:101
struct net_mqtt_t net_mqtt_t
Opaque handle identifying an MQTT client instance.
Definition: mqtt.h:90
net_mqtt_error_action_t
Which operation a net_mqtt_event_error event happened during.
Definition: mqtt.h:270
A net_mqtt_publish() call completed - see net_mqtt_sent_t.
Definition: mqtt.h:150
net_mqtt_event_type_t type
Event payload tag.
Definition: mqtt.h:313
An IPv4 or IPv6 address.
Definition: types.h:48
const char * net_mqtt_topic_to_string(net_mqtt_t *mqtt, uint32_t topic_id)
Look up the topic filter behind a confirmed subscription.
At most once - fire and forget, no acknowledgment.
Definition: mqtt.h:100
The broker explicitly rejected the request (e.g.
Definition: mqtt.h:262
MQTT connection options.
Definition: mqtt.h:120
A message arrived on a subscribed topic - see net_mqtt_received_t.
Definition: mqtt.h:157
net_mqtt_error_kind_t
Coarse category for a net_mqtt_event_error event.
Definition: mqtt.h:252
Something failed - see net_mqtt_error_t.
Definition: mqtt.h:159
A net_mqtt_unsubscribe() call completed - see net_mqtt_unsubscribed_t.
Definition: mqtt.h:154
No reply arrived within timeout_ms.
Definition: mqtt.h:261
net_mqtt_error_action_t action
Which operation - see net_mqtt_error_action_t.
Definition: mqtt.h:294