From 81f3b3a08eeb640426d700f8e4935982c7a871ce Mon Sep 17 00:00:00 2001 From: Lucas Wennerholm Date: Mon, 22 Jan 2024 14:20:28 +0100 Subject: [PATCH] Notify when there is data in socket rx buffer --- libraries/SocketWrapper/src/MbedClient.cpp | 8 ++++++++ libraries/SocketWrapper/src/MbedClient.h | 5 +++++ 2 files changed, 13 insertions(+) diff --git a/libraries/SocketWrapper/src/MbedClient.cpp b/libraries/SocketWrapper/src/MbedClient.cpp index cce392b12..f9dcdad52 100644 --- a/libraries/SocketWrapper/src/MbedClient.cpp +++ b/libraries/SocketWrapper/src/MbedClient.cpp @@ -20,6 +20,10 @@ void arduino::MbedClient::readSocket() { int ret = NSAPI_ERROR_WOULD_BLOCK; do { if (rxBuffer.availableForStore() == 0) { + // Notify that the buffer is full and data needs to be processed + if (_data_available_cb) { + _data_available_cb(); + } yield(); continue; } @@ -42,6 +46,10 @@ void arduino::MbedClient::readSocket() { mutex->unlock(); _status = true; } while (ret == NSAPI_ERROR_WOULD_BLOCK || ret > 0); + // Notify about data in RX Buffer + if (_data_available_cb) { + _data_available_cb(); + } } cleanup: _status = false; diff --git a/libraries/SocketWrapper/src/MbedClient.h b/libraries/SocketWrapper/src/MbedClient.h index cd9929839..662e10473 100644 --- a/libraries/SocketWrapper/src/MbedClient.h +++ b/libraries/SocketWrapper/src/MbedClient.h @@ -89,6 +89,10 @@ class MbedClient : public arduino::Client { return sock != nullptr; } + void registerDataAvailableCb(mbed::Callback cb) { + _data_available_cb = cb; + } + void setSocket(Socket* _sock); Socket* getSocket() { return sock; }; @@ -114,6 +118,7 @@ class MbedClient : public arduino::Client { } private: + mbed::Callback _data_available_cb; RingBufferN rxBuffer; bool _status = false; bool borrowed_socket = false;