Zigbee Protocol Controller 1.6.0
mqtt_client.hpp
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#ifndef MQTT_CLIENT_HPP
14#define MQTT_CLIENT_HPP
15
16// Standard-library
17#include <functional>
18#include <string>
19#include <queue>
20#include <map>
21#include <set>
22#include <vector>
23#include <chrono>
24
25// Unify components
26#include "sl_status.h"
27#include "uic_version.h"
28
29// This component
30#include "mqtt_client.h"
31#include "mqtt_client_fsm.hpp"
32
33// Some type-definitions to help with internal consistency
34typedef struct {
35 std::string topic;
36 std::string message;
38 bool retain;
40
42 public:
45 void *user;
46
47 inline bool operator==(const callback_info &rhs) const
48 {
49 return ((callback == rhs.callback) && (user == rhs.user));
50 }
51 };
52
54 const std::string &hostname,
55 const uint32_t port,
56 const std::string &client_id,
57 const std::string &cafile,
58 const std::string &certfile,
59 const std::string &keyfile,
60 std::function<int(const int, void *)> event_sender,
61 std::function<void(const int, const unsigned long)> delayed_event_sender,
62 std::function<int(const int, void *)> event_counter);
63
64 // Process (lifetime) management functions: Initialization, tear-down, and
65 // event-functionality.
69 void poll();
70 void disconnect(); // Needs to be public so graceful disconnect can take
71 // place, even after the event-queue has be torn down.
72 void event(const int incoming_event);
73 std::function<int(const int, void *)>
74 event_sender; // Contiki: process_post(...)
75 std::function<void(const int, const unsigned long)> delayed_event_sender;
76 std::function<int(const int, void *)>
77 event_counter; // Contiki: process_count_events(...)
78
79 // Publish and subscribe functions. This is what the uic_mqtt_* functions call.
80 void publish(const std::string &topic,
81 const std::string &message,
82 size_t message_length,
83 bool retain);
84
91 void unretain(const std::string &prefix_pattern);
92
99 void unretain_by_regex(const std::string &regex);
100
109 int count_topics(const std::string &prefix_pattern);
110
111 void subscribe(const std::string &topic, const callback_info &callback);
113 void unsubscribe(const std::string &topic, const callback_info &callback);
114
115 // Callbacks to the outside world
120
121 // The MQTT callbacks. Those are called (through wrappers) from
122 // the Mosquitto library.
123 void on_connect(int return_code);
124 void on_disconnect(int return_code);
125 void on_message(const std::string &topic,
126 const std::string &message,
127 size_t message_length,
128 int message_qos,
129 bool message_retain);
130
131 // Execute after connect and before disconnect callbacks
134
135 const char *get_client_id() const;
136
138
139 private:
140 const std::string hostname;
143 std::chrono::milliseconds next_reconnect_backoff;
144 std::chrono::milliseconds max_reconnect_backoff;
145 std::set<mqtt_connection_callback_t> on_connect_callbacks;
146 std::set<mqtt_connection_callback_t> on_disconnect_callbacks;
147 std::set<mqtt_simple_callback_t> before_disconnect_callbacks;
148 std::set<mqtt_simple_callback_t> after_connect_callbacks;
149
150 // Message-queueing and routing: Containers and arbitrators. The arbitrators are
151 // what allows for an asynchronous implementation of the client.
152 std::deque<message_queue_element_t> publishing_queue;
153 std::queue<std::string> subscription_queue;
154 std::queue<std::string> unsubscription_queue;
155 std::map<std::string, std::vector<callback_info>> subscription_callbacks;
156 std::set<std::string> retained_topics;
157
159 sl_status_t publish_to_broker(bool flushing = false);
164
165 // MQTT-client-library fields. Those need to be client-library agnostic but
166 // were, at the time of writing, based on the Mosquitto architecture.
168 std::string mqtt_client_id;
169 std::string mqtt_cafile;
170 std::string mqtt_certfile;
171 std::string mqtt_keyfile;
173
174 // FSM functionality
179 void send_event(const int outgoing_event, void *procdata);
180 void send_delayed_event(const int outgoing_event,
181 std::chrono::milliseconds delay_time);
182 void transition(mqtt_client_fsm &new_state);
184};
185
186#endif // MQTT_CLIENT_HPP
Definition: mqtt_client_fsm.hpp:59
Definition: mqtt_client_fsm.hpp:44
Definition: mqtt_client_fsm.hpp:29
Definition: mqtt_client_fsm.hpp:20
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_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_simple_callback_t)()
A void function type (void function()) to invoke when some events occurred.
Definition: mqtt_client.h:134
SL Status Codes.
Definition: mqtt_client.hpp:34
std::string message
Definition: mqtt_client.hpp:36
bool retain
Definition: mqtt_client.hpp:38
size_t message_length
Definition: mqtt_client.hpp:37
std::string topic
Definition: mqtt_client.hpp:35
Definition: mqtt_client.hpp:43
bool operator==(const callback_info &rhs) const
Definition: mqtt_client.hpp:47
void * user
Definition: mqtt_client.hpp:45
message_callback_ex_t callback
Definition: mqtt_client.hpp:44
Definition: mqtt_client.hpp:41
sl_status_t connect()
const char * get_client_id() const
void unretain_by_regex(const std::string &regex)
Un-retain previously retained messages.
void before_disconnect_callback_call()
std::set< std::string > retained_topics
Definition: mqtt_client.hpp:156
void event(const int incoming_event)
std::string mqtt_cafile
Definition: mqtt_client.hpp:169
void * mqtt_client_library_instance
Definition: mqtt_client.hpp:167
bool connected_to_broker
Definition: mqtt_client.hpp:172
sl_status_t teardown()
std::map< std::string, std::vector< callback_info > > subscription_callbacks
Definition: mqtt_client.hpp:155
void subscribe(const std::string &topic, const callback_info &callback)
int socket_file_descriptor
Definition: mqtt_client.hpp:142
void after_connect_callback_set(mqtt_simple_callback_t)
void publish(const std::string &topic, const std::string &message, size_t message_length, bool retain)
std::string mqtt_client_id
Definition: mqtt_client.hpp:168
std::set< mqtt_connection_callback_t > on_connect_callbacks
Definition: mqtt_client.hpp:145
std::string mqtt_keyfile
Definition: mqtt_client.hpp:171
void unsubscribe(const std::string &topic, const callback_info &callback)
sl_status_t setup()
std::function< int(const int, void *)> event_counter
Definition: mqtt_client.hpp:77
void on_connect_callback_set(mqtt_connection_callback_t)
int file_descriptor()
std::deque< message_queue_element_t > publishing_queue
Definition: mqtt_client.hpp:152
int count_topics(const std::string &prefix_pattern)
Count the number of topics published.
mqtt_client(const std::string &hostname, const uint32_t port, const std::string &client_id, const std::string &cafile, const std::string &certfile, const std::string &keyfile, std::function< int(const int, void *)> event_sender, std::function< void(const int, const unsigned long)> delayed_event_sender, std::function< int(const int, void *)> event_counter)
bool unsubscription_messages_waiting()
std::function< void(const int, const unsigned long)> delayed_event_sender
Definition: mqtt_client.hpp:75
void before_disconnect_callback_set(mqtt_simple_callback_t)
std::chrono::milliseconds next_reconnect_backoff
Definition: mqtt_client.hpp:143
std::string mqtt_certfile
Definition: mqtt_client.hpp:170
void transition(mqtt_client_fsm &new_state)
void on_message(const std::string &topic, const std::string &message, size_t message_length, int message_qos, bool message_retain)
void send_delayed_event(const int outgoing_event, std::chrono::milliseconds delay_time)
sl_status_t publish_to_broker(bool flushing=false)
const uint32_t port
Definition: mqtt_client.hpp:141
bool subscription_messages_waiting()
void unretain(const std::string &prefix_pattern)
Un-retain previously retained messages.
std::function< int(const int, void *)> event_sender
Definition: mqtt_client.hpp:74
std::set< mqtt_simple_callback_t > before_disconnect_callbacks
Definition: mqtt_client.hpp:147
std::set< mqtt_simple_callback_t > after_connect_callbacks
Definition: mqtt_client.hpp:148
void resubscribe()
void disconnect()
void after_connect_callback_call()
void on_connect(int return_code)
void on_disconnect(int return_code)
void on_disconnect_callback_set(mqtt_connection_callback_t)
std::set< mqtt_connection_callback_t > on_disconnect_callbacks
Definition: mqtt_client.hpp:146
std::chrono::milliseconds max_reconnect_backoff
Definition: mqtt_client.hpp:144
void send_event(const int outgoing_event, void *procdata)
sl_status_t unsubscribe_from_topic()
sl_status_t subscribe_to_topic()
std::queue< std::string > unsubscription_queue
Definition: mqtt_client.hpp:154
std::queue< std::string > subscription_queue
Definition: mqtt_client.hpp:153
mqtt_client_fsm * current_state
Definition: mqtt_client.hpp:178
bool publish_messages_waiting()
bool is_connected_to_broker() const
const std::string hostname
Definition: mqtt_client.hpp:140