objc

mqtt.h
Go to the documentation of this file.
1 
14 #pragma once
15 #include <stdbool.h>
16 #include <stddef.h>
17 #include <stdint.h>
18 #include <string.h>
19 
21 // TYPES
22 
31 typedef union net_mqtt_t {
32  uintptr_t _align; // ensure suitable alignment for internal state
33  uint8_t opaque[128];
34 } net_mqtt_t;
35 
40 typedef enum {
42  (1 << 0),
44  (1 << 1),
48  (1 << 3),
51  (1
52  << 4),
54  (1 << 5),
57  (1 << 6),
59 
73  net_mqtt_status_t status,
74  void *user_data);
75 
77 // LIFECYCLE
78 
91 net_mqtt_t net_mqtt_init(net_mqtt_connect_callback_t connect, void *user_data);
92 
101 bool net_mqtt_valid(net_mqtt_t *mqtt);
102 
113 void net_mqtt_finalize(net_mqtt_t *mqtt);
114 
140 bool net_mqtt_connect(net_mqtt_t *mqtt, const char *hostname, uint16_t port,
141  const char *client_id, const char *user,
142  const char *password, uint16_t keepalive_secs,
143  const char *will_topic, const char *will_message);
144 
155 bool net_mqtt_disconnect(net_mqtt_t *mqtt);
156 
158 // METHODS
159 
176 bool net_mqtt_publish(net_mqtt_t *mqtt, const char *topic, const void *data,
177  size_t size, uint8_t qos);
178 
195 static inline bool net_mqtt_publish_str(net_mqtt_t *mqtt, const char *topic,
196  const char *str, uint8_t qos) {
197  return net_mqtt_publish(mqtt, topic, str, strlen(str), qos);
198 }
199 
219 bool net_mqtt_subscribe(net_mqtt_t *mqtt, const char *topic, uint8_t qos);
220 
221 /*
222 // Future API (publish/subscribe):
223 // The following functions are planned and will follow the same asynchronous
224 // completion model. They are left commented-out to document the intended
225 // surface while implementation stabilizes.
226 
227 
228 bool net_mqtt_unsubscribe(net_mqtt_t *mqtt, const char *topic);
229 */
bool net_mqtt_valid(net_mqtt_t *mqtt)
Check whether an MQTT client is valid and connected.
static bool net_mqtt_publish_str(net_mqtt_t *mqtt, const char *topic, const char *str, uint8_t qos)
Publish a NULL-terminated string to a topic.
Definition: mqtt.h:195
bool net_mqtt_connect(net_mqtt_t *mqtt, const char *hostname, uint16_t port, const char *client_id, const char *user, const char *password, uint16_t keepalive_secs, const char *will_topic, const char *will_message)
Begin an asynchronous connection to an MQTT broker.
net_mqtt_status_t
Connection/event status flags for MQTT operations.
Definition: mqtt.h:40
Protocol-level error (malformed packet, unsupported feature, etc).
Definition: mqtt.h:47
DNS resolution failed.
Definition: mqtt.h:46
net_mqtt_t net_mqtt_init(net_mqtt_connect_callback_t connect, void *user_data)
Create and initialize an MQTT handle.
Authentication failure (bad client ID/username/password).
Definition: mqtt.h:50
bool net_mqtt_publish(net_mqtt_t *mqtt, const char *topic, const void *data, size_t size, uint8_t qos)
Publish a message to a topic.
Disconnected from the broker (remote close or local request).
Definition: mqtt.h:43
bool net_mqtt_subscribe(net_mqtt_t *mqtt, const char *topic, uint8_t qos)
Subscribe to a topic.
union net_mqtt_t net_mqtt_t
Opaque MQTT handle.
void net_mqtt_finalize(net_mqtt_t *mqtt)
Finalize the MQTT handle and free associated resources.
Operation timed out (connect or in-flight request exceeded deadline).
Definition: mqtt.h:53
void(* net_mqtt_connect_callback_t)(net_mqtt_t *mqtt, net_mqtt_status_t status, void *user_data)
Callback invoked on connection state changes and errors.
Definition: mqtt.h:72
Opaque MQTT handle.
Definition: mqtt.h:31
bool net_mqtt_disconnect(net_mqtt_t *mqtt)
Begin an asynchronous disconnect from the MQTT broker.
Connected to the broker and session is active.
Definition: mqtt.h:41
Generic error occurred (e.g., connection failed).
Definition: mqtt.h:56