GCC Code Coverage Report


Directory: ./
File: mqtt.cpp
Date: 2026-12-01 12:27:28
Exec Total Coverage
Lines: 323 354 91.2%
Functions: 32 33 97.0%
Branches: 317 574 55.2%

Line Branch Exec Source
1 /*
2 * This file is part of the Falcon Player (FPP) and is Copyright (C)
3 * 2013-2022 by the Falcon Player Developers.
4 *
5 * The Falcon Player (FPP) is free software, and is covered under
6 * multiple Open Source licenses. Please see the included 'LICENSES'
7 * file for descriptions of what files are covered by each license.
8 *
9 * This source file is covered under the LGPL v2.1 as described in the
10 * included LICENSE.LGPL file.
11 */
12
13 #include "fpp-pch.h"
14
15 #include "fpp-json.h"
16 #include "fppversion.h"
17
18 #include <atomic>
19 #include <cstring>
20 #include <fcntl.h>
21 #include <chrono>
22 #include <functional>
23 #include <future>
24 #include <list>
25 #include <map>
26 #include <memory>
27 #include <mosquitto.h>
28 #include <mutex>
29 #include <stdlib.h>
30 #include <string>
31 #include <thread>
32 #include <unistd.h>
33 #include <vector>
34
35 #include "Events.h"
36 #include "Timers.h"
37 #include "Warnings.h"
38 #include "common.h" // SetThreadName() -- needed directly for NOPCH builds
39 #include "common_mini.h" // FPPstrerror(), FPPrand() -- needed directly for NOPCH builds
40 #include "log.h"
41 #include "mqtt.h"
42 #include "settings.h"
43 #include "commands/Commands.h"
44
45 #define FALCON_TOPIC "falcon/player"
46
47 MosquittoClient* mqtt = NULL;
48
49 class MQTTCommand : public Command {
50 public:
51 61 MQTTCommand() :
52
2/4
✓ Branch 0 taken 61 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 61 times.
✗ Branch 3 not taken.
122 Command("MQTT") {
53
5/10
✓ Branch 0 taken 61 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 61 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 61 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 61 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 61 times.
✗ Branch 9 not taken.
366 args.push_back(CommandArg("topic", "string", "Topic"));
54
5/10
✓ Branch 0 taken 61 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 61 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 61 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 61 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 61 times.
✗ Branch 9 not taken.
366 args.push_back(CommandArg("message", "string", "Message"));
55
6/12
✓ Branch 0 taken 61 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 61 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 61 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 61 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 61 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 61 times.
✗ Branch 11 not taken.
366 args.push_back(CommandArg("retain", "bool", "Retain").setDefaultValue("false"));
56 61 }
57
58 2 virtual std::unique_ptr<Command::Result> run(const std::vector<std::string>& args) override {
59
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (args.size() < 2) {
60 ✗ return std::make_unique<Command::ErrorResult>("MQTT Requires two arguments");
61 }
62 2 bool retain = false;
63
3/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1 times.
4 if ((args.size() >= 3) &&
64
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 ((args[2] == "true") ||
65
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 (args[2] == "1"))) {
66 1 retain = true;
67 }
68
69 2 mqtt->PublishRaw(args[0], args[1], retain);
70 2 return std::make_unique<Command::Result>("OK");
71 }
72 };
73
74 61 void mosq_disc_callback(struct mosquitto* mosq, void* userdata, int level) {
75
1/2
✓ Branch 0 taken 61 times.
✗ Branch 1 not taken.
61 if (userdata) {
76 61 ((MosquittoClient*)userdata)->HandleDisconnect();
77 }
78 /*
79 * Don't call reconnect here. Per documentation, loop_start() will handle
80 * reconnect automatically.
81 */
82 // mosquitto_reconnect(mosq);
83 61 }
84
85 // A broker that refuses the connection (bad credentials, not authorised, etc.)
86 // makes libmosquitto retry forever, invoking the connect callback with a
87 // non-zero result each attempt; count refusals so they can be logged without
88 // flooding at the retry rate.
89 static int mqttConnectRefusals = 0;
90 static long long mqttLastRefusalLogMS = 0;
91
92 // The network thread, running mosquitto_loop_forever(), and its signal that
93 // the loop has returned (see ~MosquittoClient()). Kept here rather than in
94 // MosquittoClient, whose layout plugins compile in (see mqtt.h).
95 static std::thread mqttLoopThread;
96 static std::future<void> mqttLoopStopped;
97
98 // libmosquitto opens its connection to the broker without close-on-exec, and
99 // has no hook to open it another way, so every shell and tool fppd ran
100 // inherited it. Mark it once it is open: here, on each connect, since a
101 // reconnect opens a new one, and straight after the first connect starts.
102 128 static void MarkBrokerSocketCloseOnExec(struct mosquitto* mosq) {
103 128 int fd = mosquitto_socket(mosq);
104
1/2
✓ Branch 0 taken 128 times.
✗ Branch 1 not taken.
128 if (fd >= 0) {
105 128 fcntl(fd, F_SETFD, FD_CLOEXEC);
106 }
107 128 }
108
109 67 void mosq_connect_callback(struct mosquitto* mosq, void* userdata, int rc) {
110 67 MarkBrokerSocketCloseOnExec(mosq);
111
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 63 times.
67 if (rc != 0) {
112 4 mqttConnectRefusals++;
113 4 long long now = GetMonotonicTimeMS();
114
3/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
4 if ((mqttConnectRefusals == 1) || ((now - mqttLastRefusalLogMS) >= 60000)) {
115 1 mqttLastRefusalLogMS = now;
116 1 LogErr(VB_CONTROL, "MQTT broker refused connection: %s (%d attempts so far)\n",
117 mosquitto_connack_string(rc), mqttConnectRefusals);
118 }
119 4 return;
120 }
121
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 62 times.
63 if (mqttConnectRefusals) {
122 1 LogWarn(VB_CONTROL, "MQTT broker accepted connection after %d refused attempts\n",
123 mqttConnectRefusals);
124 1 mqttConnectRefusals = 0;
125 1 mqttLastRefusalLogMS = 0;
126 }
127
1/2
✓ Branch 0 taken 63 times.
✗ Branch 1 not taken.
63 if (userdata) {
128 63 ((MosquittoClient*)userdata)->HandleConnect();
129 }
130 }
131
132 15562 void mosq_log_callback(struct mosquitto* mosq, void* userdata, int level, const char* str) {
133 15562 mqtt->LogCallback(userdata, level, str);
134 15562 }
135
136 6234 void mosq_msg_callback(struct mosquitto* mosq, void* userdata, const struct mosquitto_message* message) {
137 6234 mqtt->MessageCallback(userdata, message);
138 6232 }
139
140 /*
141 *
142 */
143 // callbackTopics is walked on libmosquitto's thread for every message, and
144 // changed on the main loop and API threads as callbacks come and go. A file
145 // static rather than a member so MosquittoClient's layout doesn't change.
146 static std::mutex s_callbackTopicsLock;
147
148 6358 static std::list<std::string> CopyTopics(const std::list<std::string>& topics) {
149
1/2
✓ Branch 0 taken 6358 times.
✗ Branch 1 not taken.
6358 std::lock_guard<std::mutex> lock(s_callbackTopicsLock);
150
1/2
✓ Branch 0 taken 6358 times.
✗ Branch 1 not taken.
12716 return topics;
151 6358 }
152
153 62 MosquittoClient::MosquittoClient(const std::string& host, const int port,
154 62 const std::string& topicPrefix) :
155 62 m_port(port),
156 62 m_keepalive(60),
157 62 m_mosq(NULL),
158
1/2
✓ Branch 0 taken 62 times.
✗ Branch 1 not taken.
62 m_host(host),
159 62 m_canProcessMessages(false),
160 62 m_isConnected(false),
161
1/2
✓ Branch 0 taken 62 times.
✗ Branch 1 not taken.
124 m_topicPrefix(topicPrefix) {
162
1/2
✓ Branch 0 taken 62 times.
✗ Branch 1 not taken.
62 LogDebug(VB_CONTROL, "MosquittoClient::MosquittoClient('%s', %d, '%s')\n",
163 host.c_str(), port, topicPrefix.c_str());
164
165
2/4
✓ Branch 0 taken 62 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 62 times.
✗ Branch 3 not taken.
184 WarningHolder::AddWarning(4, "MQTT Disconnected");
166
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 62 times.
62 if (m_topicPrefix.size()) {
167 ✗ m_topicPrefix += "/";
168 }
169
170
1/2
✓ Branch 0 taken 62 times.
✗ Branch 1 not taken.
62 m_baseTopic = m_topicPrefix;
171
1/2
✓ Branch 0 taken 62 times.
✗ Branch 1 not taken.
62 m_baseTopic += FALCON_TOPIC;
172
1/2
✓ Branch 0 taken 62 times.
✗ Branch 1 not taken.
62 m_baseTopic += "/";
173
174
1/2
✓ Branch 0 taken 62 times.
✗ Branch 1 not taken.
62 std::string hostname = getSetting("HostName");
175
2/4
✓ Branch 0 taken 62 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 62 times.
62 if (hostname == "") {
176 ✗ hostname = "FPP";
177 }
178
1/2
✓ Branch 0 taken 62 times.
✗ Branch 1 not taken.
62 m_baseTopic += hostname;
179
180 62 } // End of MosquittoClient()
181
182 /*
183 *
184 */
185 248 MosquittoClient::~MosquittoClient() {
186 // Stop the network thread by disconnecting and joining it, not by
187 // cancelling it. A cancel lands at whatever cancellation point the thread
188 // has reached, which can be inside a message callback running a command,
189 // and the catch (...) that guards those callbacks swallows the forced
190 // unwind, which aborts the process on the way out. A clean disconnect
191 // means the broker drops our will, but shutdown has already published
192 // "ready" = 0 itself (Events::PrepareForShutdown), ahead of the
193 // DISCONNECT in the same queue.
194 //
195 // The callbacks go first: the disconnect this causes would otherwise run
196 // HandleDisconnect(), whose timer captures this object, and the others
197 // reach this object through the global `mqtt`.
198 //
199 // The join is bounded. With the broker unreachable the thread can be
200 // inside a blocking connect() (about two minutes when SYNs are dropped),
201 // or a message callback can be stuck in a command, and fppd's restart and
202 // shutdown waited on it. Every path from here ends in exec or _exit, so
203 // past the bound the client is left to the thread and never freed.
204 //
205 // The thread is ours, not mosquitto_loop_start()'s, so the join does not
206 // depend on libmosquitto still seeing it running: its own thread clears
207 // mosq->threaded on the way out, and a mosquitto_loop_stop() that read
208 // that first returned MOSQ_ERR_INVAL without joining, leaving
209 // mosquitto_destroy() to free the client under the exiting thread.
210
1/2
✓ Branch 0 taken 62 times.
✗ Branch 1 not taken.
124 if (m_mosq) {
211 124 mosquitto_message_callback_set(m_mosq, nullptr);
212 124 mosquitto_log_callback_set(m_mosq, nullptr);
213 124 mosquitto_connect_callback_set(m_mosq, nullptr);
214 124 mosquitto_disconnect_callback_set(m_mosq, nullptr);
215 124 mosquitto_disconnect(m_mosq);
216
217
2/2
✓ Branch 0 taken 61 times.
✓ Branch 1 taken 1 times.
124 if (mqttLoopThread.joinable()) {
218
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 57 times.
122 if (mqttLoopStopped.wait_for(std::chrono::seconds(3)) != std::future_status::ready) {
219 8 LogWarn(VB_CONTROL, "MQTT network thread did not stop within 3 s; leaving it\n");
220 8 mqttLoopThread.detach();
221 8 mqtt = NULL;
222 8 return;
223 }
224 114 mqttLoopThread.join();
225 }
226 }
227
228 116 mosquitto_destroy(m_mosq);
229 116 mqtt = NULL;
230
231 116 mosquitto_lib_cleanup();
232
12/12
✓ Branch 0 taken 58 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 58 times.
✓ Branch 3 taken 4 times.
✓ Branch 4 taken 58 times.
✓ Branch 5 taken 4 times.
✓ Branch 6 taken 58 times.
✓ Branch 7 taken 4 times.
✓ Branch 8 taken 58 times.
✓ Branch 9 taken 4 times.
✓ Branch 10 taken 58 times.
✓ Branch 11 taken 4 times.
288 }
233
234 61 void MosquittoClient::PrepareForShutdown() {
235
3/4
✓ Branch 0 taken 61 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 610 times.
✓ Branch 3 taken 61 times.
671 for (const std::string& n : CopyTopics(callbackTopics)) {
236
1/2
✓ Branch 0 taken 610 times.
✗ Branch 1 not taken.
610 RemoveCallback(n);
237 61 }
238
5/6
✓ Branch 0 taken 61 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✓ Branch 3 taken 54 times.
✓ Branch 4 taken 7 times.
✓ Branch 5 taken 54 times.
61 if (m_canProcessMessages && m_isConnected) {
239 7 std::vector<std::string> subscribe_topics;
240
2/4
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
7 subscribe_topics.push_back(m_baseTopic + "/set/#");
241
242
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 7 times.
14 for (auto& subscribe : subscribe_topics) {
243
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 mosquitto_unsubscribe(m_mosq, NULL, subscribe.c_str());
244 }
245 7 }
246 61 m_canProcessMessages = false;
247 61 }
248
249 /*
250 *
251 */
252 62 int MosquittoClient::Init(const std::string& username, const std::string& password, const std::string& ca_file) {
253
1/2
✓ Branch 0 taken 62 times.
✗ Branch 1 not taken.
62 mosquitto_lib_init();
254 // Use supplied Client Id (If one)
255
1/2
✓ Branch 0 taken 62 times.
✗ Branch 1 not taken.
62 std::string clientId = getSetting("MQTTClientId");
256
2/4
✓ Branch 0 taken 62 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 62 times.
✗ Branch 3 not taken.
62 if (clientId == "") {
257 // If not, genereate one with random digits on end
258
1/2
✓ Branch 0 taken 62 times.
✗ Branch 1 not taken.
62 clientId = getSetting("HostName");
259
2/4
✓ Branch 0 taken 62 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 62 times.
62 if (clientId == "") {
260 ✗ clientId = "FPP";
261 }
262
1/2
✓ Branch 0 taken 62 times.
✗ Branch 1 not taken.
62 clientId.append("_");
263
2/2
✓ Branch 0 taken 248 times.
✓ Branch 1 taken 62 times.
310 for (int i = 0; i < 4; i++) {
264
1/2
✓ Branch 0 taken 248 times.
✗ Branch 1 not taken.
248 int digit = FPPrand() % 10;
265 248 char digitc = '0' + digit;
266
1/2
✓ Branch 0 taken 248 times.
✗ Branch 1 not taken.
248 clientId.append(1, digitc);
267 }
268 }
269
1/2
✓ Branch 0 taken 62 times.
✗ Branch 1 not taken.
62 LogDebug(VB_CONTROL, "Using MQTT client id of %s \n", clientId.c_str());
270
1/2
✓ Branch 0 taken 62 times.
✗ Branch 1 not taken.
62 m_mosq = mosquitto_new(clientId.c_str(), true, NULL);
271
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 62 times.
62 if (!m_mosq) {
272 ✗ LogErr(VB_CONTROL, "Error, unable to create new Mosquitto instance.\n");
273 ✗ return 0;
274 }
275
1/2
✓ Branch 0 taken 62 times.
✗ Branch 1 not taken.
62 mosquitto_user_data_set(m_mosq, this);
276 // The network loop runs on a thread of our own (below), so libmosquitto
277 // has to lock as it would for mosquitto_loop_start().
278
1/2
✓ Branch 0 taken 62 times.
✗ Branch 1 not taken.
62 mosquitto_threaded_set(m_mosq, true);
279
1/2
✓ Branch 0 taken 62 times.
✗ Branch 1 not taken.
62 mosquitto_log_callback_set(m_mosq, mosq_log_callback);
280
1/2
✓ Branch 0 taken 62 times.
✗ Branch 1 not taken.
62 mosquitto_disconnect_callback_set(m_mosq, mosq_disc_callback);
281
1/2
✓ Branch 0 taken 62 times.
✗ Branch 1 not taken.
62 mosquitto_connect_callback_set(m_mosq, mosq_connect_callback);
282
283 // Back off failed reconnects exponentially (1s doubling up to 60s) rather
284 // than libmosquitto's default fixed 1s retry, so an unreachable or
285 // refusing broker isn't hammered once per second indefinitely.
286
1/2
✓ Branch 0 taken 62 times.
✗ Branch 1 not taken.
62 mosquitto_reconnect_delay_set(m_mosq, 1, 60, true);
287
288
3/4
✓ Branch 0 taken 62 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 61 times.
62 if (username != "") {
289
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 mosquitto_username_pw_set(m_mosq, username.c_str(), password.c_str());
290 }
291
292
3/4
✓ Branch 0 taken 62 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 61 times.
62 if (ca_file != "") {
293
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 LogDebug(VB_CONTROL, "Using CA File: %s for MQTT\n", ca_file.c_str());
294
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 int rc = mosquitto_tls_set(m_mosq, ca_file.c_str(), NULL, NULL, NULL, NULL);
295
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (rc) {
296
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 LogErr(VB_CONTROL, "Error, unable to set MQTT_Ca_file. RC= %d\n", rc);
297 1 return 0;
298 }
299 } else {
300
1/2
✓ Branch 0 taken 61 times.
✗ Branch 1 not taken.
61 LogDebug(VB_CONTROL, "No CA File specified for MQTT\n");
301 }
302
303
1/2
✓ Branch 0 taken 61 times.
✗ Branch 1 not taken.
61 LogDebug(VB_CONTROL, "About to call MQTT Connect (%s, %d, %d)\n", m_host.c_str(), m_port, m_keepalive);
304
305 // Tell MQTT broker to make MQTT "ready" topic go to zero if crash or network loss
306
2/4
✓ Branch 0 taken 61 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 61 times.
✗ Branch 3 not taken.
61 static std::string last_ready_message = "0";
307
4/10
✓ Branch 0 taken 61 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 61 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 61 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 61 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
61 static std::string last_ready_topic = m_baseTopic + "/" + MQTT_READY_TOPIC_NAME;
308
1/2
✓ Branch 0 taken 61 times.
✗ Branch 1 not taken.
61 int rc = mosquitto_will_set(m_mosq, last_ready_topic.c_str(), last_ready_message.size(), last_ready_message.c_str(), 1, true);
309
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 61 times.
61 if (rc != MOSQ_ERR_SUCCESS) {
310 ✗ LogErr(VB_CONTROL, "MQTT: Unable to set last will for %s. Error code: %d\n", MQTT_READY_TOPIC_NAME, rc);
311 }
312
313
1/2
✓ Branch 0 taken 61 times.
✗ Branch 1 not taken.
61 rc = mosquitto_connect_async(m_mosq, m_host.c_str(), m_port, m_keepalive);
314
1/2
✓ Branch 0 taken 61 times.
✗ Branch 1 not taken.
61 MarkBrokerSocketCloseOnExec(m_mosq);
315
316
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 61 times.
61 if (rc) {
317 ✗ LogErr(VB_CONTROL, "Error, unable to connect to Mosquitto Broker at %s: %d\n",
318 m_host.c_str(), rc);
319 ✗ LogErr(VB_CONTROL, "MQTT Error: %s\n", FPPstrerror(rc));
320 ✗ return 0;
321 }
322
323 // What mosquitto_loop_start() would run, on a thread the destructor can
324 // join: sleep for the keepalive (or a day without one) between packets,
325 // since publish() and disconnect() wake the loop.
326 61 struct mosquitto* m = m_mosq;
327
1/2
✓ Branch 0 taken 61 times.
✗ Branch 1 not taken.
61 int timeout = m_keepalive ? m_keepalive * 1000 : 1000 * 86400;
328
1/2
✓ Branch 0 taken 61 times.
✗ Branch 1 not taken.
61 auto stopped = std::make_shared<std::promise<void>>();
329
1/2
✓ Branch 0 taken 61 times.
✗ Branch 1 not taken.
61 mqttLoopStopped = stopped->get_future();
330 try {
331 183 mqttLoopThread = std::thread([m, timeout, stopped]() {
332
2/4
✓ Branch 0 taken 61 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 61 times.
✗ Branch 3 not taken.
61 SetThreadName("FPP-MQTT");
333 61 mosquitto_loop_forever(m, timeout, 1);
334 57 stopped->set_value();
335
1/2
✓ Branch 0 taken 61 times.
✗ Branch 1 not taken.
118 });
336 ✗ } catch (const std::system_error& e) {
337 ✗ LogErr(VB_CONTROL, "Error, unable to start Mosquitto loop: %s\n", e.what());
338 ✗ return 0;
339 ✗ }
340
341
5/12
✓ Branch 0 taken 61 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 61 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 61 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 61 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 61 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
63 CommandManager::INSTANCE.addCategorizedCommand(new MQTTCommand(), "Outputs", 1);
342
343 61 return 1;
344 62 }
345
346 /*
347 * Publish to an absolute topic. Note the parameter order is (retain, qos);
348 * the Publish() overloads below must pass them through in that order.
349 * This signature is used directly by plugins (e.g. fpp-HomeAssistant) and
350 * must not change.
351 */
352 4194 int MosquittoClient::PublishRaw(const std::string& topic, const std::string& msg, const bool retain, const int qos) {
353 // Truncated: status publishes are multi-KB, and a LogDebug is always
354 // formatted into the crash ring.
355
2/4
✓ Branch 0 taken 4194 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4194 times.
✗ Branch 3 not taken.
4194 LogDebug(VB_CONTROL, "Publishing message '%s' on topic '%s'\n", TruncateForLog(msg).c_str(), topic.c_str());
356
357 int result;
358 {
359 // Scope the lock to just the mosquitto_publish() call, matching the
360 // coverage of the previous pthread_mutex_lock()/unlock() pair.
361
1/2
✓ Branch 0 taken 4194 times.
✗ Branch 1 not taken.
4194 std::lock_guard<std::mutex> lock(m_mosqLock);
362
1/2
✓ Branch 0 taken 4194 times.
✗ Branch 1 not taken.
4194 result = mosquitto_publish(m_mosq, NULL, topic.c_str(), msg.size(), msg.c_str(), qos, retain);
363 4194 }
364
365
2/2
✓ Branch 0 taken 67 times.
✓ Branch 1 taken 4127 times.
4194 if (result != 0) {
366 // MOSQ_ERR_NO_CONN (4) is expected when disconnected, don't spam logs
367
3/6
✓ Branch 0 taken 67 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 67 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 67 times.
✗ Branch 5 not taken.
67 if (result == 4 && !m_isConnected) {
368 67 LogDebug(VB_CONTROL, "Cannot publish to MQTT, not connected to broker\n");
369 } else {
370 ✗ LogErr(VB_CONTROL, "Error running mosquitto_publish: %d\n", result);
371 }
372 67 return 0;
373 }
374
375 4127 return 1;
376 }
377
378 // Two-argument overloads used by Events::Publish() and by plugins.
379 //
380 // Everything published through here is retained (QoS 1). Historically the
381 // four-argument overloads below passed retain/qos to PublishRaw() in the wrong
382 // order, which had the effect of retaining every message regardless of the
383 // flag requested. Consumers such as Home Assistant depend on that: retained
384 // state topics (status, playlist/*, fppd_status, plugin light/switch state...)
385 // are what let a subscriber recover the current state on (re)connect instead
386 // of showing "unknown" until the next change. Keep retaining state topics so
387 // fixing the argument order does not change what is stored on the broker.
388 2470 bool MosquittoClient::Publish(const std::string& topic, const std::string& data) {
389 2470 return Publish(topic, data, true, 1);
390 }
391 1472 bool MosquittoClient::Publish(const std::string& topic, const int value) {
392
6/6
✓ Branch 0 taken 122 times.
✓ Branch 1 taken 1350 times.
✓ Branch 2 taken 61 times.
✓ Branch 3 taken 61 times.
✓ Branch 4 taken 61 times.
✓ Branch 5 taken 1411 times.
1472 if (topic == MQTT_READY_TOPIC_NAME && value) {
393 61 SetReady();
394 }
395 1472 return Publish(topic, value, true, 1);
396 }
397
398 /*
399 *
400 */
401 2596 int MosquittoClient::Publish(const std::string& subTopic, const std::string& msg, const bool retain, const int qos) {
402
2/4
✓ Branch 0 taken 2596 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2596 times.
✗ Branch 3 not taken.
2596 std::string topic = m_baseTopic + "/" + subTopic;
403
404
1/2
✓ Branch 0 taken 2596 times.
✗ Branch 1 not taken.
5192 return PublishRaw(topic, msg, retain, qos);
405 2596 }
406
407 /*
408 *
409 */
410 1535 int MosquittoClient::Publish(const std::string& subTopic, const int value, const bool retain, const int qos) {
411
2/4
✓ Branch 0 taken 1535 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1535 times.
✗ Branch 3 not taken.
1535 std::string topic = m_baseTopic + "/" + subTopic;
412 1535 std::string msg = std::to_string(value);
413
414
1/2
✓ Branch 0 taken 1535 times.
✗ Branch 1 not taken.
3070 return PublishRaw(topic, msg, retain, qos);
415 1535 }
416
417 /*
418 *
419 */
420 15562 void MosquittoClient::LogCallback(void* userdata, int level, const char* str) {
421
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15562 times.
15562 switch (level) {
422 // FIXME, comment out the first 3 of these or tie to our own debug level
423 // case MOSQ_LOG_DEBUG:
424 // case MOSQ_LOG_INFO:
425 // case MOSQ_LOG_NOTICE:
426 ✗ case MOSQ_LOG_WARNING:
427 case MOSQ_LOG_ERR:
428 ✗ LogDebug(VB_CONTROL, "Mosquitto Log: %d:%s\n", level, str);
429 }
430 15562 }
431
432 10353 bool MosquittoClient::IsConnected() {
433 10353 return m_isConnected;
434 }
435
436 611 void MosquittoClient::RegisterCallback(const std::string& topic) {
437 611 LogDebug(VB_CONTROL, "MQTT: In AddCallback with %s\n", topic.c_str());
438 {
439
1/2
✓ Branch 0 taken 611 times.
✗ Branch 1 not taken.
611 std::lock_guard<std::mutex> lock(s_callbackTopicsLock);
440
1/2
✓ Branch 0 taken 611 times.
✗ Branch 1 not taken.
611 callbackTopics.push_back(topic);
441 611 }
442
2/6
✗ Branch 0 not taken.
✓ Branch 1 taken 611 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 611 times.
611 if (m_canProcessMessages && m_isConnected) {
443 ✗ if (topic.rfind("/set/", 0) != 0) {
444 // we are registered on all "/set/" already, no need to re-register
445 ✗ std::string tp = m_baseTopic + topic;
446 ✗ LogDebug(VB_CONTROL, "MQTT: Preparing to Subscribe to %s\n", tp.c_str());
447 ✗ int rc = mosquitto_subscribe(m_mosq, NULL, tp.c_str(), 0);
448 ✗ if (rc != MOSQ_ERR_SUCCESS) {
449 ✗ LogErr(VB_CONTROL, "Error, unable to subscribe to %s: %d\n", tp.c_str(), rc);
450 }
451 ✗ }
452 }
453 611 }
454 1228 void MosquittoClient::RemoveCallback(const std::string& topic) {
455
6/6
✓ Branch 0 taken 611 times.
✓ Branch 1 taken 617 times.
✓ Branch 2 taken 71 times.
✓ Branch 3 taken 540 times.
✓ Branch 4 taken 71 times.
✓ Branch 5 taken 1157 times.
1228 if (m_canProcessMessages && m_isConnected) {
456
3/4
✓ Branch 0 taken 71 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 42 times.
✓ Branch 3 taken 29 times.
71 if (topic.rfind("/set/", 0) != 0) {
457 // we are registered on all "/set/" already, no need to un-register
458
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 std::string tp = m_baseTopic + topic;
459
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 LogDebug(VB_CONTROL, "MQTT: Preparing to Unsubscribe to %s\n", tp.c_str());
460
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 int rc = mosquitto_unsubscribe(m_mosq, NULL, tp.c_str());
461
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
42 if (rc != MOSQ_ERR_SUCCESS) {
462 ✗ LogErr(VB_CONTROL, "Error, unable to Unsubscribe to %s: %d\n", tp.c_str(), rc);
463 }
464 42 }
465 }
466
1/2
✓ Branch 0 taken 1228 times.
✗ Branch 1 not taken.
1228 std::lock_guard<std::mutex> lock(s_callbackTopicsLock);
467 1228 callbackTopics.remove(topic);
468 1228 }
469 61 void MosquittoClient::SetReady() {
470 61 LogDebug(VB_CONTROL, "Mosquitto SetReady()\n");
471
1/2
✓ Branch 0 taken 61 times.
✗ Branch 1 not taken.
61 if (!m_canProcessMessages) {
472 61 m_canProcessMessages = true;
473 61 mosquitto_message_callback_set(m_mosq, mosq_msg_callback);
474 }
475
476 // Register topics which were set since we originally connected
477 // But only if already connected!
478
2/2
✓ Branch 0 taken 59 times.
✓ Branch 1 taken 2 times.
61 if (m_isConnected) {
479 59 HandleConnect();
480 }
481 61 }
482
483 122 void MosquittoClient::HandleConnect() {
484 122 m_isConnected = true;
485
1/2
✓ Branch 0 taken 122 times.
✗ Branch 1 not taken.
122 LogWarn(VB_CONTROL, "Mosquitto Connected\n");
486
2/2
✓ Branch 0 taken 59 times.
✓ Branch 1 taken 63 times.
122 if (!m_canProcessMessages) {
487
1/2
✓ Branch 0 taken 59 times.
✗ Branch 1 not taken.
59 LogWarn(VB_CONTROL, "HandleConnect() called before can process messages. Won't Register topics yet.\n");
488 59 return;
489 }
490
491
1/2
✓ Branch 0 taken 63 times.
✗ Branch 1 not taken.
63 LogDebug(VB_CONTROL, "Mosquitto Connected.... Will Subscribe to Topics\n");
492 63 std::vector<std::string> subscribe_topics;
493
2/4
✓ Branch 0 taken 63 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 63 times.
✗ Branch 3 not taken.
63 subscribe_topics.push_back(m_baseTopic + "/set/#");
494
495
1/2
✓ Branch 0 taken 63 times.
✗ Branch 1 not taken.
63 std::string add_topics = getSetting("MQTTSubscribe");
496
497
3/4
✓ Branch 0 taken 63 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 61 times.
63 if (add_topics != "") {
498 // Handle single topic with no semicolon
499 2 std::vector<std::string> subscribe_to_me;
500
5/6
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 1 times.
2 if (add_topics.find(';') == std::string::npos && !add_topics.empty()) {
501
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 subscribe_to_me.push_back(add_topics);
502 }
503
504 // Handle multiple topics separated by semicolon
505 2 size_t start = 0, end = 0;
506
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 2 times.
5 while ((end = add_topics.find(';', start)) != std::string::npos) {
507
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 std::string sub = add_topics.substr(start, end - start);
508
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 subscribe_to_me.push_back(sub);
509 3 start = end + 1;
510 3 }
511 // Add last topic if there is no trailing semicolon
512
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (start < add_topics.length()) {
513
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 subscribe_to_me.push_back(add_topics.substr(start));
514 }
515
516 // Trip topic names and add to subscribe list
517
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 2 times.
8 for (auto& s : subscribe_to_me) {
518
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 s.erase(0, s.find_first_not_of(" \t\n\r\f\v"));
519
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 s.erase(s.find_last_not_of(" \t\n\r\f\v") + 1);
520
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
6 if (!s.empty()) {
521
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 LogDebug(VB_CONTROL, "Adding extra MQTT subscribe topic: %s\n", s.c_str());
522
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 subscribe_topics.push_back(s);
523 }
524 }
525 2 }
526
527
528
3/4
✓ Branch 0 taken 63 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 631 times.
✓ Branch 3 taken 63 times.
694 for (auto& topic : CopyTopics(callbackTopics)) {
529
3/4
✓ Branch 0 taken 631 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 378 times.
✓ Branch 3 taken 253 times.
631 if (topic.rfind("/set/", 0) != 0) {
530
1/2
✓ Branch 0 taken 378 times.
✗ Branch 1 not taken.
378 std::string tp = m_baseTopic + topic;
531
1/2
✓ Branch 0 taken 378 times.
✗ Branch 1 not taken.
378 subscribe_topics.push_back(tp);
532 378 }
533 63 }
534
535
2/2
✓ Branch 0 taken 445 times.
✓ Branch 1 taken 63 times.
508 for (auto& subscribe : subscribe_topics) {
536
1/2
✓ Branch 0 taken 445 times.
✗ Branch 1 not taken.
445 LogDebug(VB_CONTROL, "MQTT: Preparing to Subscribe to %s\n", subscribe.c_str());
537
1/2
✓ Branch 0 taken 445 times.
✗ Branch 1 not taken.
445 int rc = mosquitto_subscribe(m_mosq, NULL, subscribe.c_str(), 0);
538
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 445 times.
445 if (rc != MOSQ_ERR_SUCCESS) {
539 ✗ LogErr(VB_CONTROL, "Error, unable to subscribe to %s: %d\n", subscribe.c_str(), rc);
540 }
541 }
542
543
3/6
✓ Branch 0 taken 63 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 63 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 63 times.
✗ Branch 5 not taken.
189 WarningHolder::RemoveWarning(4, "MQTT Disconnected");
544
545 // Re-publish retained status messages on reconnection
546 // This ensures the broker has the current values after a network disconnection
547 // that may have triggered the last will (ready=0)
548
1/2
✓ Branch 0 taken 63 times.
✗ Branch 1 not taken.
63 LogDebug(VB_CONTROL, "MQTT: Re-publishing retained status on reconnection\n");
549
2/4
✓ Branch 0 taken 63 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 63 times.
✗ Branch 3 not taken.
126 Publish(MQTT_READY_TOPIC_NAME, 1, true, 1);
550
4/8
✓ Branch 0 taken 63 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 63 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 63 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 63 times.
✗ Branch 7 not taken.
252 Publish("version", getFPPVersion(), true, 1);
551
4/8
✓ Branch 0 taken 63 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 63 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 63 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 63 times.
✗ Branch 7 not taken.
189 Publish("branch", getFPPBranch(), true, 1);
552
553
1/2
✓ Branch 0 taken 63 times.
✗ Branch 1 not taken.
63 LogDebug(VB_CONTROL, "MQTT HandleConnect Complete\n");
554 63 }
555
556 61 void MosquittoClient::HandleDisconnect() {
557 61 long long tm = GetTimeMS();
558
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 57 times.
61 if (!m_isConnected) {
559 // This fires after every refused reconnect attempt; the refusal is
560 // already logged (rate-limited) in mosq_connect_callback and the
561 // "MQTT Disconnected" warning is still active, so stay quiet here.
562 4 LogDebug(VB_CONTROL, "Mosquitto Disconnected (was not connected) %lld\n", tm);
563 4 return;
564 }
565 57 LogWarn(VB_CONTROL, "Mosquitto Disconnected. Will try reconnect %lld\n", tm);
566 57 m_isConnected = false;
567
2/6
✓ Branch 0 taken 57 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 57 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
170 Timers::INSTANCE.addTimer("MosquittoDisconnect", tm + 10000, [this, tm]() {
568
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (!m_isConnected) {
569
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
3 WarningHolder::AddWarning(4, "MQTT Disconnected");
570 }
571 2 });
572 }
573
574 /*
575 *
576 */
577 6234 void MosquittoClient::MessageCallback(void* obj, const struct mosquitto_message* message) {
578
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6234 times.
6234 if (!m_canProcessMessages) {
579 // FPPd is not yet in a state where incoming messages can be processed
580 210 return;
581 }
582 6234 bool match = 0;
583 6234 std::string emptyStr;
584 6234 std::string topic;
585 6234 std::string payload;
586
587
1/2
✓ Branch 0 taken 6234 times.
✗ Branch 1 not taken.
6234 LogDebug(VB_CONTROL, "Received Mosquitto message: '%s' on topic '%s'\n",
588 (char*)message->payload, message->topic);
589
590
1/2
✓ Branch 0 taken 6234 times.
✗ Branch 1 not taken.
6234 if (message->topic)
591
1/2
✓ Branch 0 taken 6234 times.
✗ Branch 1 not taken.
6234 topic = (char*)message->topic;
592
593
2/2
✓ Branch 0 taken 6203 times.
✓ Branch 1 taken 31 times.
6234 if (message->payload)
594
1/2
✓ Branch 0 taken 6203 times.
✗ Branch 1 not taken.
6203 payload = (char*)message->payload;
595
596
1/2
✓ Branch 0 taken 6234 times.
✗ Branch 1 not taken.
6234 CacheSetMessage(topic, payload);
597
598
3/4
✓ Branch 0 taken 6234 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 61677 times.
✓ Branch 3 taken 6022 times.
67699 for (auto& a : CopyTopics(callbackTopics)) {
599
1/2
✓ Branch 0 taken 61677 times.
✗ Branch 1 not taken.
61677 std::string s = m_baseTopic + a;
600
1/2
✓ Branch 0 taken 61677 times.
✗ Branch 1 not taken.
61677 LogDebug(VB_CONTROL, "Testing Callback '%s'\n", s.c_str());
601
1/2
✓ Branch 0 taken 61677 times.
✗ Branch 1 not taken.
61677 mosquitto_topic_matches_sub(s.c_str(), message->topic, &match);
602
2/2
✓ Branch 0 taken 212 times.
✓ Branch 1 taken 61465 times.
61677 if (match) {
603
1/2
✓ Branch 0 taken 212 times.
✗ Branch 1 not taken.
212 std::string tp = message->topic;
604
1/2
✓ Branch 0 taken 212 times.
✗ Branch 1 not taken.
212 tp.replace(0, m_baseTopic.size(), emptyStr);
605
1/2
✓ Branch 0 taken 210 times.
✗ Branch 1 not taken.
212 Events::InvokeCallback(a, tp, payload);
606
1/2
✓ Branch 0 taken 210 times.
✗ Branch 1 not taken.
210 LogDebug(VB_CONTROL, "Found and Completed Callback for '%s'\n", s.c_str());
607 210 return;
608 210 }
609
4/4
✓ Branch 0 taken 61465 times.
✓ Branch 1 taken 210 times.
✓ Branch 2 taken 6022 times.
✓ Branch 3 taken 210 times.
67907 }
610
611
1/2
✓ Branch 0 taken 6022 times.
✗ Branch 1 not taken.
6022 LogDebug(VB_CONTROL, "No match found for Mosquitto topic '%s'. Assuming it is an additional subscribe topic\n",
612 message->topic);
613
6/6
✓ Branch 0 taken 6022 times.
✓ Branch 1 taken 210 times.
✓ Branch 2 taken 6022 times.
✓ Branch 3 taken 210 times.
✓ Branch 4 taken 6022 times.
✓ Branch 5 taken 210 times.
6652 }
614
615 // Every message on every subscribed topic is cached, for conditions,
616 // variables and playlist branches to read. Unbounded, a broker with many
617 // topics -- or anyone who can publish under the subscribed wildcards, one
618 // topic name after another -- grew fppd until it ran out of memory. Past
619 // these, the topic updated longest ago makes room, and a payload too large
620 // to be a value anything here compares is not kept.
621 static constexpr size_t MQTT_CACHE_MAX_TOPICS = 4096;
622 static constexpr size_t MQTT_CACHE_MAX_PAYLOAD = 64 * 1024;
623
624 6234 void MosquittoClient::CacheSetMessage(std::string& topic, std::string& message) {
625
1/2
✓ Branch 0 taken 6234 times.
✗ Branch 1 not taken.
6234 std::unique_lock<std::mutex> lock(messageCacheLock);
626
627
1/2
✓ Branch 0 taken 6234 times.
✗ Branch 1 not taken.
6234 LogDebug(VB_CONTROL, "MosquittoClient::CacheSetMessage('%s', '%s')\n", topic.c_str(), message.c_str());
628
629
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6234 times.
6234 if (message.size() > MQTT_CACHE_MAX_PAYLOAD) {
630 // not an older, smaller value standing in for it either
631 ✗ messageCache.erase(topic);
632 ✗ return;
633 }
634
1/2
✓ Branch 0 taken 6234 times.
✗ Branch 1 not taken.
6234 auto it = messageCache.find(topic);
635
6/6
✓ Branch 0 taken 6051 times.
✓ Branch 1 taken 183 times.
✓ Branch 2 taken 1905 times.
✓ Branch 3 taken 4146 times.
✓ Branch 4 taken 1905 times.
✓ Branch 5 taken 4329 times.
6234 if (it == messageCache.end() && messageCache.size() >= MQTT_CACHE_MAX_TOPICS) {
636 1905 auto oldest = messageCache.begin();
637
2/2
✓ Branch 0 taken 7802880 times.
✓ Branch 1 taken 1905 times.
7804785 for (auto e = messageCache.begin(); e != messageCache.end(); ++e) {
638
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7802880 times.
7802880 if (e->second.lastUpdated < oldest->second.lastUpdated) {
639 ✗ oldest = e;
640 }
641 }
642
1/2
✓ Branch 0 taken 1905 times.
✗ Branch 1 not taken.
1905 messageCache.erase(oldest);
643 }
644 6234 messageCache[topic] = { message, time(nullptr) };
645
3/8
✓ Branch 0 taken 6234 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6234 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 6234 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
12468 }
646
647 2 bool MosquittoClient::CacheCheckMessage(std::string& topic, std::string& message) {
648
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 std::unique_lock<std::mutex> lock(messageCacheLock);
649
650
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 auto search = messageCache.find(topic);
651
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (search != messageCache.end())
652 2 return search->second.value == message;
653
654 ✗ return false;
655 2 }
656
657 518 bool MosquittoClient::GetCachedMessage(const std::string& topic, std::string& message) {
658 time_t lastUpdated;
659
1/2
✓ Branch 0 taken 518 times.
✗ Branch 1 not taken.
1036 return GetCachedMessage(topic, message, lastUpdated);
660 }
661
662 518 bool MosquittoClient::GetCachedMessage(const std::string& topic, std::string& message, time_t& lastUpdated) {
663
1/2
✓ Branch 0 taken 518 times.
✗ Branch 1 not taken.
518 std::unique_lock<std::mutex> lock(messageCacheLock);
664
665
1/2
✓ Branch 0 taken 518 times.
✗ Branch 1 not taken.
518 auto search = messageCache.find(topic);
666
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 518 times.
518 if (search == messageCache.end())
667 ✗ return false;
668
669
1/2
✓ Branch 0 taken 518 times.
✗ Branch 1 not taken.
518 message = search->second.value;
670 518 lastUpdated = search->second.lastUpdated;
671 518 return true;
672 518 }
673
674 252 void MosquittoClient::dumpMessageCache(Json::Value& result, bool includeMetadata) {
675 // NOTE: This assumes that result is an Array
676
1/2
✓ Branch 0 taken 252 times.
✗ Branch 1 not taken.
252 std::unique_lock<std::mutex> lock(messageCacheLock);
677
678
2/2
✓ Branch 0 taken 17021 times.
✓ Branch 1 taken 252 times.
17273 for (const auto& pair : messageCache) {
679
2/2
✓ Branch 0 taken 518 times.
✓ Branch 1 taken 16503 times.
17021 if (includeMetadata) {
680
3/6
✓ Branch 0 taken 518 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 518 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 518 times.
✗ Branch 5 not taken.
518 result[pair.first]["value"] = pair.second.value;
681
3/6
✓ Branch 0 taken 518 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 518 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 518 times.
✗ Branch 5 not taken.
518 result[pair.first]["lastUpdated"] = (Json::Int64)pair.second.lastUpdated;
682 } else {
683
2/4
✓ Branch 0 taken 16503 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 16503 times.
✗ Branch 3 not taken.
16503 result[pair.first] = pair.second.value;
684 }
685 }
686 252 }
687