Skip to content

Added lambda feature for onMessage callback. Fixes #147 #175

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions src/MQTTClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ static void MQTTClientHandler(lwmqtt_client_t * /*client*/, void *ref, lwmqtt_st
return;
}

// return if simple callback is not set
if (cb->simple == nullptr) {
// return if simple and lambda callbacks are not set
if (cb->simple == nullptr && cb->lambda == nullptr) {
return;
}

Expand All @@ -115,6 +115,14 @@ static void MQTTClientHandler(lwmqtt_client_t * /*client*/, void *ref, lwmqtt_st
str_payload = String((const char *)message.payload);
}

#if has_functional
// call the lambda callback and return if available
if (cb->lambda != nullptr) {
cb->lambda(str_topic, str_payload);
return;
}
#endif

// call simple callback
cb->simple(str_topic, str_payload);
}
Expand Down Expand Up @@ -163,17 +171,29 @@ void MQTTClient::begin(const char _hostname[], int _port, Client &_client) {
lwmqtt_set_callback(&this->client, (void *)&this->callback, MQTTClientHandler);
}

#if has_functional
void MQTTClient::onMessage(MQTTClientCallbackSimpleLambda cb) {
// set callback
this->callback.client = this;
this->callback.simple = nullptr;
this->callback.lambda = cb;
this->callback.advanced = nullptr;
}
#endif

void MQTTClient::onMessage(MQTTClientCallbackSimple cb) {
// set callback
this->callback.client = this;
this->callback.simple = cb;
this->callback.lambda = nullptr;
this->callback.advanced = nullptr;
}

void MQTTClient::onMessageAdvanced(MQTTClientCallbackAdvanced cb) {
// set callback
this->callback.client = this;
this->callback.simple = nullptr;
this->callback.lambda = nullptr;
this->callback.advanced = cb;
}

Expand Down
17 changes: 17 additions & 0 deletions src/MQTTClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
#include <Arduino.h>
#include <Client.h>
#include <Stream.h>
#if __has_include(<functional>)
#include <functional>
#define has_functional 1
#else
#define has_functional 0
#endif

extern "C" {
#include "lwmqtt/lwmqtt.h"
Expand All @@ -25,10 +31,17 @@ class MQTTClient;
typedef void (*MQTTClientCallbackSimple)(String &topic, String &payload);
typedef void (*MQTTClientCallbackAdvanced)(MQTTClient *client, char topic[], char bytes[], int length);

#if has_functional
typedef std::function<void(String&, String&)> MQTTClientCallbackSimpleLambda;
#endif

typedef struct {
MQTTClient *client = nullptr;
MQTTClientCallbackSimple simple = nullptr;
MQTTClientCallbackAdvanced advanced = nullptr;
#if has_functional
MQTTClientCallbackSimpleLambda lambda = nullptr;
#endif
} MQTTClientCallback;

class MQTTClient {
Expand Down Expand Up @@ -69,6 +82,10 @@ class MQTTClient {
void onMessage(MQTTClientCallbackSimple cb);
void onMessageAdvanced(MQTTClientCallbackAdvanced cb);

#if has_functional
void onMessage(MQTTClientCallbackSimpleLambda cb);
#endif

void setClockSource(MQTTClientClockSource cb);

void setHost(const char _hostname[]) { this->setHost(_hostname, 1883); }
Expand Down