Skip to content

Commit

Permalink
Allow any size MAX_PAYLOAD_SIZE
Browse files Browse the repository at this point in the history
- Remove checking of total incoming fragments
- Instead rely on cumulative size of incoming payloads vs MAX_PAYLOAD_SIZE
- Allows users to define any size for the buffers instead of having to use multiples of 24
- Also adjust is_valid_address() to check specifically against unique multicast address instead of allowing a bunch of out of range addresses
  • Loading branch information
TMRh20 committed Aug 12, 2020
1 parent 293c55e commit 3474b15
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 28 deletions.
30 changes: 10 additions & 20 deletions RF24Network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -361,11 +361,7 @@ bool RF24Network::appendFragmentToFrame(RF24NetworkFrame frame) {
return false;
}
}
if(frame.header.reserved > (uint16_t(MAX_PAYLOAD_SIZE) / max_frame_payload_size) ){
IF_SERIAL_DEBUG_FRAGMENTATION( printf("%u FRG Too many fragments in payload %u, dropping...",millis(),frame.header.reserved); );
// If there are more fragments than we can possibly handle, return
return false;
}

frameFragmentsCache[ frame.header.from_node ] = frame;
return true;
}else
Expand All @@ -376,6 +372,12 @@ bool RF24Network::appendFragmentToFrame(RF24NetworkFrame frame) {
return false;
}
RF24NetworkFrame *f = &(frameFragmentsCache[ frame.header.from_node ]);

if( f->message_size + frame.message_size > MAX_PAYLOAD_SIZE){
IF_SERIAL_DEBUG_FRAGMENTATION( printf("%u FRG Frame of size %u plus enqueued frame of size %u exceeds max payload size \n",millis(),frame.message_size,f->message_size); );
return false;
}

if( f->header.reserved - 1 == frame.header.reserved && f->header.id == frame.header.id){
// Cache the fragment
memcpy(f->message_buffer+f->message_size, frame.message_buffer, frame.message_size);
Expand Down Expand Up @@ -443,16 +445,8 @@ uint8_t RF24Network::enqueue(RF24NetworkHeader* header)
if(isFragment){

if(header->type == NETWORK_FIRST_FRAGMENT){
// Drop frames exceeding max size and duplicates (MAX_PAYLOAD_SIZE needs to be divisible by 24)
if(header->reserved > (uint16_t(MAX_PAYLOAD_SIZE) / max_frame_payload_size) ){

#if defined (SERIAL_DEBUG_FRAGMENTATION) || defined (SERIAL_DEBUG_MINIMAL)
printf_P(PSTR("Frag frame with %d frags exceeds MAX_PAYLOAD_SIZE or out of sequence\n"),header->reserved);
#endif
frag_queue.header.reserved = 0;
return false;
}
memcpy(&frag_queue,&frame_buffer,8);
memcpy(&frag_queue,&frame_buffer,sizeof(RF24NetworkHeader));
memcpy(frag_queue.message_buffer,frame_buffer+sizeof(RF24NetworkHeader),message_size);

IF_SERIAL_DEBUG_FRAGMENTATION( Serial.print(F("queue first, total frags ")); Serial.println(header->reserved); );
Expand All @@ -467,7 +461,7 @@ uint8_t RF24Network::enqueue(RF24NetworkHeader* header)

if(frag_queue.message_size + message_size > MAX_PAYLOAD_SIZE){
#if defined (SERIAL_DEBUG_FRAGMENTATION) || defined (SERIAL_DEBUG_MINIMAL)
Serial.print(F("Drop frag ")); Serial.print(header->reserved);
Serial.print(F("Drop frag ")); Serial.print(header->reserved);
Serial.println(F(" Size exceeds max"));
#endif
frag_queue.header.reserved=0;
Expand Down Expand Up @@ -1203,15 +1197,11 @@ uint8_t RF24Network::pipe_to_descendant( uint16_t node )
bool RF24Network::is_valid_address( uint16_t node )
{
bool result = true;

if(node == 0100){ return result; }
while(node)
{
uint8_t digit = node & 0x07;
#if !defined (RF24NetworkMulticast)
if (digit < 1 || digit > 5)
#else
if (digit < 0 || digit > 5) //Allow our out of range multicast address
#endif
{
result = false;
IF_SERIAL_DEBUG_MINIMAL(printf_P(PSTR("*** WARNING *** Invalid address 0%o\n\r"),node););
Expand Down
9 changes: 1 addition & 8 deletions RF24Network_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,13 @@

/** Maximum size of fragmented network frames and fragmentation cache.
*
* @note:
* The size of fragmented data is initialy indicated by the number of packets to be sent (Max 24 bytes data per packet)
* ie: A payload of 24 bytes requires 1 packet send. MAX_PAYLOAD_SIZE of 24 is fine.
* A payload of 25 requires 2 packets, so the MAX_PAYLOAD_SIZE must be 48 or higher.
* @note: Must be a multiple of 24!!! UL is to specify Unsigned Long and prevent compiler warnings
* @note: This buffer can now be any size > 24. Previously need to be a multiple of 24.
* @note: If used with RF24Ethernet, this value is used to set the buffer sizes.
*/
#define MAX_PAYLOAD_SIZE 144

/** The size of the main buffer. This is the user-cache, where incoming data is stored.
* Data is stored using Frames: Header (8-bytes) + Frame_Size (2-bytes) + Data (?-bytes)
*
* @note The MAX_PAYLOAD_SIZE is (MAIN_BUFFER_SIZE - 10), and the result must be divisible by 24!!!
*/
#define MAIN_BUFFER_SIZE (MAX_PAYLOAD_SIZE + FRAME_HEADER_SIZE)

Expand Down

0 comments on commit 3474b15

Please sign in to comment.