Skip to content

Commit 4eaf0f6

Browse files
rcarle3911256dpi
authored andcommitted
Added lambda feature for onMessage callback
1 parent bb51a65 commit 4eaf0f6

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

src/MQTTClient.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ static void MQTTClientHandler(lwmqtt_client_t * /*client*/, void *ref, lwmqtt_st
101101
return;
102102
}
103103

104-
// return if simple callback is not set
105-
if (cb->simple == nullptr) {
104+
// return if simple and lambda callbacks are not set
105+
if (cb->simple == nullptr && cb->lambda == nullptr) {
106106
return;
107107
}
108108

@@ -115,6 +115,12 @@ static void MQTTClientHandler(lwmqtt_client_t * /*client*/, void *ref, lwmqtt_st
115115
str_payload = String((const char *)message.payload);
116116
}
117117

118+
// call the lambda callback and return if available
119+
if (cb->lambda != nullptr) {
120+
cb->lambda(str_topic, str_payload);
121+
return;
122+
}
123+
118124
// call simple callback
119125
cb->simple(str_topic, str_payload);
120126
}
@@ -163,17 +169,27 @@ void MQTTClient::begin(const char _hostname[], int _port, Client &_client) {
163169
lwmqtt_set_callback(&this->client, (void *)&this->callback, MQTTClientHandler);
164170
}
165171

172+
void MQTTClient::onMessage(MQTTClientCallbackLambda cb) {
173+
// set callback
174+
this->callback.client = this;
175+
this->callback.simple = nullptr;
176+
this->callback.lambda = cb;
177+
this->callback.advanced = nullptr;
178+
}
179+
166180
void MQTTClient::onMessage(MQTTClientCallbackSimple cb) {
167181
// set callback
168182
this->callback.client = this;
169183
this->callback.simple = cb;
184+
this->callback.lambda = nullptr;
170185
this->callback.advanced = nullptr;
171186
}
172187

173188
void MQTTClient::onMessageAdvanced(MQTTClientCallbackAdvanced cb) {
174189
// set callback
175190
this->callback.client = this;
176191
this->callback.simple = nullptr;
192+
this->callback.lambda = nullptr;
177193
this->callback.advanced = cb;
178194
}
179195

src/MQTTClient.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <Arduino.h>
55
#include <Client.h>
66
#include <Stream.h>
7+
#include <functional>
78

89
extern "C" {
910
#include "lwmqtt/lwmqtt.h"
@@ -22,13 +23,15 @@ typedef struct {
2223

2324
class MQTTClient;
2425

26+
typedef std::function<void(String&, String&)> MQTTClientCallbackLambda;
2527
typedef void (*MQTTClientCallbackSimple)(String &topic, String &payload);
2628
typedef void (*MQTTClientCallbackAdvanced)(MQTTClient *client, char topic[], char bytes[], int length);
2729

2830
typedef struct {
2931
MQTTClient *client = nullptr;
3032
MQTTClientCallbackSimple simple = nullptr;
3133
MQTTClientCallbackAdvanced advanced = nullptr;
34+
MQTTClientCallbackLambda lambda = nullptr;
3235
} MQTTClientCallback;
3336

3437
class MQTTClient {
@@ -66,6 +69,7 @@ class MQTTClient {
6669
void begin(const char _hostname[], Client &_client) { this->begin(_hostname, 1883, _client); }
6770
void begin(const char hostname[], int port, Client &client);
6871

72+
void onMessage(MQTTClientCallbackLambda cb);
6973
void onMessage(MQTTClientCallbackSimple cb);
7074
void onMessageAdvanced(MQTTClientCallbackAdvanced cb);
7175

0 commit comments

Comments
 (0)