Zigbee Protocol Controller 1.6.0
mqtt_client.h
Go to the documentation of this file.
1/******************************************************************************
2 * # License
3 * <b>Copyright 2021 Silicon Laboratories Inc. www.silabs.com</b>
4 ******************************************************************************
5 * The licensor of this software is Silicon Laboratories Inc. Your use of this
6 * software is governed by the terms of Silicon Labs Master Software License
7 * Agreement (MSLA) available at
8 * www.silabs.com/about-us/legal/master-software-license-agreement. This
9 * software is distributed to you in Source Code format and is governed by the
10 * sections of the MSLA applicable to Source Code.
11 *
12 *****************************************************************************/
13
26#ifndef MQTT_CLIENT_H
27#define MQTT_CLIENT_H
28
29// Standard library
30#include <stdbool.h>
31#include <unistd.h>
32#include <inttypes.h>
33
34// Unify components
35#include "sl_status.h"
36
37#ifndef MQTT_CLIENT_POLL_TIMER_MILLISECONDS
38#define MQTT_CLIENT_POLL_TIMER_MILLISECONDS 2000
39#endif
40
41#ifndef MQTT_CLIENT_KEEP_ALIVE_INTERVAL_MILLISECONDS
42#define MQTT_CLIENT_KEEP_ALIVE_INTERVAL_MILLISECONDS 60000
43#endif
44
45#ifndef MQTT_CLIENT_MINIMUM_RECONNECT_BACKOFF_MILLISECONDS
46#define MQTT_CLIENT_MINIMUM_RECONNECT_BACKOFF_MILLISECONDS 1000
47#endif
48
49#ifndef MQTT_CLIENT_MAXIMUM_RECONNECT_BACKOFF_MILLISECONDS
50#define MQTT_CLIENT_MAXIMUM_RECONNECT_BACKOFF_MILLISECONDS 60000
51#endif
52
53#ifndef MQTT_CLIENT_QoS
54#define MQTT_CLIENT_QoS 0
55#endif
56
57#ifdef __cplusplus
58extern "C" {
59#endif // __cplusplus
60
71typedef enum {
81// This is really hackish, but I couldn't figure out a way to do this with
82// preprocessor-magic but still be able to properly Doxygen-document the
83// members of the enum.
84__attribute__((unused)) static const char *event_names[]
85 = {"MQTT_EVENT_CONNECT",
86 "MQTT_EVENT_DISCONNECT",
87 "MQTT_EVENT_PUBLISH",
88 "MQTT_EVENT_SUBSCRIBE",
89 "MQTT_EVENT_UNSUBSCRIBE",
90 "MQTT_TRANSITION_DISCONNECTED",
91 "MQTT_TRANSITION_CONNECTING",
92 "MQTT_TRANSITION_CONNECTED"};
93
94struct mqtt_client;
95
96typedef struct mqtt_client
98
106typedef void (*message_callback_t)(const char *topic,
107 const char *message,
108 const size_t message_length);
109
118typedef void (*message_callback_ex_t)(const char *topic,
119 const char *message,
120 const size_t message_length,
121 void *user);
129 const int file_descriptor);
130
134typedef void (*mqtt_simple_callback_t)();
135
136// C-wrappers. All of those functions have a C++ counterpart within mqtt_client
137// or a C++ specific functionality that needs to be bridged to C-land (e.g. new).
138
146
155
163
174
181
191void mqtt_client_event(mqtt_client_t instance, const int incoming_event);
192
207 const char *topic,
208 const char *message,
209 const size_t message_length,
210 bool retain);
211
225void mqtt_client_unretain(mqtt_client_t instance, const char *prefix_pattern);
226
237 const char *prefix_pattern);
238
247void mqtt_client_unretain_by_regex(mqtt_client_t instance, const char *regex);
248
262 const char *topic,
263 message_callback_t callback);
264
279 const char *topic,
280 message_callback_ex_t callback,
281 void *user);
282
297 const char *topic,
298 message_callback_t callback);
299
315 const char *topic,
316 message_callback_ex_t callback,
317 void *user);
318
339 const char *hostname,
340 const uint32_t port,
341 const char *client_id,
342 const char *cafile,
343 const char *certfile,
344 const char *keyfile,
345 int (*event_sender)(const int event, void *procdata),
346 void (*delayed_event_sender)(const int event,
347 const unsigned long milliseconds),
348 int (*event_counter)(const int event, void *procdata));
349
356
365
374
383 mqtt_client_t instance, mqtt_simple_callback_t before_disconnect);
384
393 mqtt_client_t instance, mqtt_simple_callback_t after_connect);
394
402
410
411#ifdef __cplusplus
412} // extern "C"
413#endif // __cplusplus
414
415#endif // MQTT_CLIENT_H
416
__attribute__((unused)) static const char *event_names[]
void mqtt_client_publish(mqtt_client_t instance, const char *topic, const char *message, const size_t message_length, bool retain)
Publish a message to the MQTT-broker.
void mqtt_client_subscribe_ex(mqtt_client_t instance, const char *topic, message_callback_ex_t callback, void *user)
Subscribe to an MQTT-topic.
void mqtt_client_unretain_by_regex(mqtt_client_t instance, const char *regex)
Un-retain a previously retained message.
void(* message_callback_ex_t)(const char *topic, const char *message, const size_t message_length, void *user)
A callback type for pushing incoming messages.
Definition: mqtt_client.h:118
void mqtt_client_unsubscribe(mqtt_client_t instance, const char *topic, message_callback_t callback)
Unsubscribe from an MQTT-topic.
int mqtt_client_count_topics(mqtt_client_t instance, const char *prefix_pattern)
Count the number of topics published.
struct mqtt_client * mqtt_client_t
Type for internal consistency and to save typing.
Definition: mqtt_client.h:96
void(* mqtt_connection_callback_t)(mqtt_client_t instance, const int file_descriptor)
A callback type for MQTT connection/disconnection events.
Definition: mqtt_client.h:128
void mqtt_client_on_connect_callback_set(mqtt_client_t instance, mqtt_connection_callback_t on_connect)
Sets the callback to be invoked after an MQTT connection is established.
void mqtt_client_after_connect_callback_set(mqtt_client_t instance, mqtt_simple_callback_t after_connect)
Deletes/frees the mqtt_client instance.
sl_status_t mqtt_client_setup(mqtt_client_t instance)
Initialize whatever resources the client needs.
int mqtt_client_file_descriptor(mqtt_client_t instance)
Get the file-descriptor for the underlying MQTT TCP socket.
void mqtt_client_before_disconnect_callback_set(mqtt_client_t instance, mqtt_simple_callback_t before_disconnect)
Sets a callback for calling before calling disconnect.
void mqtt_client_poll(mqtt_client_t instance)
Process any data that needs processed.
mqtt_client_t mqtt_client_new(const char *hostname, const uint32_t port, const char *client_id, const char *cafile, const char *certfile, const char *keyfile, int(*event_sender)(const int event, void *procdata), void(*delayed_event_sender)(const int event, const unsigned long milliseconds), int(*event_counter)(const int event, void *procdata))
Allocates a new mqtt_client instance.
void mqtt_client_unsubscribe_ex(mqtt_client_t instance, const char *topic, message_callback_ex_t callback, void *user)
Unsubscribe from an MQTT-topic.
void mqtt_client_on_disconnect_callback_set(mqtt_client_t instance, mqtt_connection_callback_t on_disconnect)
Sets the callback to be invoked after an MQTT connection is disconnected.
mqtt_event_t
Definition: mqtt_client.h:71
sl_status_t mqtt_client_teardown(mqtt_client_t instance)
Close open connections and free up resources acquired by the client.
void(* message_callback_t)(const char *topic, const char *message, const size_t message_length)
A callback type for pushing incoming messages.
Definition: mqtt_client.h:106
bool mqtt_client_is_connected_to_broker(const mqtt_client_t instance)
Checks if we are connected to the MQTT Broker.
void mqtt_client_subscribe(mqtt_client_t instance, const char *topic, message_callback_t callback)
Subscribe to an MQTT-topic.
void mqtt_client_disconnect(mqtt_client_t instance)
Do a synchronous disconnect from the MQTT-broker. This can be used when a system is being shut down a...
const char * mqtt_client_get_client_id(const mqtt_client_t client_instance)
Returns the MQTT Client ID in use by the Client instance.
void mqtt_client_delete(mqtt_client_t instance)
Deletes/frees the mqtt_client instance.
void(* mqtt_simple_callback_t)()
A void function type (void function()) to invoke when some events occurred.
Definition: mqtt_client.h:134
void mqtt_client_event(mqtt_client_t instance, const int incoming_event)
Push events to the mqtt_client.
void mqtt_client_unretain(mqtt_client_t instance, const char *prefix_pattern)
Un-retain a previously retained message.
@ MQTT_EVENT_PUBLISH
Publish one queued up message.
Definition: mqtt_client.h:74
@ MQTT_TRANSITION_CONNECTED
Request FSM to transition to the connected state.
Definition: mqtt_client.h:79
@ MQTT_TRANSITION_DISCONNECTED
Request FSM to transition to the disconnected state.
Definition: mqtt_client.h:77
@ MQTT_EVENT_SUBSCRIBE
Subscribe to one queued up topic.
Definition: mqtt_client.h:75
@ MQTT_TRANSITION_CONNECTING
Request FSM to transition to the connecting state.
Definition: mqtt_client.h:78
@ MQTT_EVENT_DISCONNECT
Disconnect (from broker) request.
Definition: mqtt_client.h:73
@ MQTT_EVENT_CONNECT
Request the client to connect to its broker.
Definition: mqtt_client.h:72
@ MQTT_EVENT_UNSUBSCRIBE
Unsubscribe from one queued up topic.
Definition: mqtt_client.h:76
uint32_t sl_status_t
Definition: sl_status.h:139
SL Status Codes.
Definition: mqtt_client.hpp:41
void event(const int incoming_event)
std::function< int(const int, void *)> event_counter
Definition: mqtt_client.hpp:77
int file_descriptor()
std::function< void(const int, const unsigned long)> delayed_event_sender
Definition: mqtt_client.hpp:75
const uint32_t port
Definition: mqtt_client.hpp:141
std::function< int(const int, void *)> event_sender
Definition: mqtt_client.hpp:74
void on_connect(int return_code)
void on_disconnect(int return_code)
const std::string hostname
Definition: mqtt_client.hpp:140
mqtt_client_t client_instance
Definition: uic_mqtt.c:39