From ce128543b779eba0cdf59583404b42ce7e3eaa3b Mon Sep 17 00:00:00 2001 From: Avamander Date: Fri, 9 Feb 2018 17:46:12 +0200 Subject: [PATCH] Added packet peek functionality (#106) --- RF24Network.cpp | 34 +++++++++++++++++++++++++++------- RF24Network.h | 13 +++++++++++++ 2 files changed, 40 insertions(+), 7 deletions(-) diff --git a/RF24Network.cpp b/RF24Network.cpp index a8dd94bd..a01ada7b 100644 --- a/RF24Network.cpp +++ b/RF24Network.cpp @@ -604,14 +604,10 @@ uint16_t RF24Network::parent() const } /******************************************************************/ -/*uint8_t RF24Network::peekData(){ - - return frame_queue[0]; -}*/ uint16_t RF24Network::peek(RF24NetworkHeader& header) { - if ( available() ) + if (available()) { #if defined (RF24_LINUX) RF24NetworkFrame frame = frame_queue.front(); @@ -630,12 +626,36 @@ uint16_t RF24Network::peek(RF24NetworkHeader& header) /******************************************************************/ +void RF24Network::peek(RF24NetworkHeader& header, void* message, uint16_t maxlen) +{ +#if defined (RF24_LINUX) + if (available()) + { // TODO: Untested + RF24NetworkFrame frame = frame_queue.front(); + memcpy(&header, &(frame.header), sizeof(RF24NetworkHeader)); + memcpy(message, frame.message_buffer, maxlen); + } +#else + if(available()) + { + memcpy(&header, frame_queue, 8); //Copy the header + if(maxlen > 0) + { + memcpy(message, frame_queue + 10, maxlen); //Copy the message + } + } +#endif +} + +/******************************************************************/ + uint16_t RF24Network::read(RF24NetworkHeader& header,void* message, uint16_t maxlen) { uint16_t bufsize = 0; #if defined (RF24_LINUX) - if ( available() ) { + if (available()) + { RF24NetworkFrame frame = frame_queue.front(); // How much buffer size should we actually copy? @@ -651,7 +671,7 @@ uint16_t RF24Network::read(RF24NetworkHeader& header,void* message, uint16_t max frame_queue.pop(); } #else - if ( available() ) + if (available()) { memcpy(&header,frame_queue,8); diff --git a/RF24Network.h b/RF24Network.h index af0cbe93..6a9d6259 100644 --- a/RF24Network.h +++ b/RF24Network.h @@ -431,6 +431,19 @@ class RF24Network */ uint16_t peek(RF24NetworkHeader& header); + /** + * Read the next available payload + * + * Reads the next available payload without advancing to the next + * incoming message. Useful for doing a transparent packet + * manipulation layer on top of RF24Network. + * + * @param[out] header The header (envelope) of this message + * @param[out] message Pointer to memory where the message should be placed + * @param maxlen Amount of bytes to copy to message. + */ + void peek(RF24NetworkHeader& header, void* message, uint16_t maxlen); + /** * Read a message *