diff --git a/README.md b/README.md index c5429bc..493127d 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ A library faciliating communication with RbController Android app: https://play. #include "rbprotocol.h" #include "rbwebserver.h" -void onPktReceived(rb::Protocol &protocol, void *cookie, const std::string& command, rbjson::Object *pkt) { +void onPktReceived(const std::string& command, rbjson::Object *pkt) { if(command == "joy") { printf("Joy: "); rbjson::Array *data = pkt->getArray("data"); diff --git a/examples/basic/main.cpp b/examples/basic/main.cpp index d669d6e..92fdc12 100644 --- a/examples/basic/main.cpp +++ b/examples/basic/main.cpp @@ -8,7 +8,7 @@ #include "rbprotocol.h" #include "rbwebserver.h" -void onPktReceived(rb::Protocol &protocol, void *cookie, const std::string& command, rbjson::Object *pkt) { +void onPktReceived(const std::string& command, rbjson::Object *pkt) { if(command == "joy") { printf("Joy: "); rbjson::Array *data = pkt->getArray("data"); diff --git a/src/rbprotocol.cpp b/src/rbprotocol.cpp index 8bce2e3..0c76f83 100644 --- a/src/rbprotocol.cpp +++ b/src/rbprotocol.cpp @@ -43,12 +43,11 @@ class SemaphoreHolder { namespace rb { Protocol::Protocol(const char *owner, const char *name, const char *description, - ProtocolCallback onPacketReceivedCallback, void *callback_cookie) { + std::function callback) { m_owner = owner; m_name = name; m_desc = description; - m_callback = onPacketReceivedCallback; - m_callback_cookie = callback_cookie; + m_callback = callback; m_socket = -1; m_mutex = xSemaphoreCreateMutex(); @@ -387,7 +386,7 @@ void Protocol::handle_msg(struct sockaddr_in *addr, char *buf, ssize_t size) { ESP_LOGI(TAG, "We are possessed!"); send_log("The device %s has been possessed!\n", m_name); } else if(m_callback != NULL) { - m_callback(*this, m_callback_cookie, cmd, pkt.get()); + m_callback(cmd, pkt.get()); } } diff --git a/src/rbprotocol.h b/src/rbprotocol.h index b264989..11083e9 100644 --- a/src/rbprotocol.h +++ b/src/rbprotocol.h @@ -5,6 +5,7 @@ #include #include #include +#include #include "rbjson.h" @@ -17,11 +18,6 @@ namespace rb { class Protocol; -/** - * \brief This is the type for onPacketReceived callback - */ -typedef void (*ProtocolCallback)(Protocol& protocol, void *cookie, const std::string& command, rbjson::Object *pkt); - /** * \brief Class that manages the RBProtocol communication */ @@ -32,7 +28,7 @@ class Protocol { * It runs on a separate task, only single packet is processed at a time. */ Protocol(const char *owner, const char *name, const char *description, - ProtocolCallback onPacketReceivedCallback = NULL, void *callback_cookie = NULL); + std::function callback = nullptr); ~Protocol(); void start(int port = RBPROTOCOL_PORT); //!< Start listening for UDP packets on port @@ -79,8 +75,7 @@ class Protocol { const char *m_name; const char *m_desc; - ProtocolCallback m_callback; - void *m_callback_cookie; + std::function m_callback; int m_socket; int m_read_counter;