Skip to content

Commit

Permalink
Fix memory issue, code clean up, testing changes
Browse files Browse the repository at this point in the history
Fix: Default calculation for total frags was incorrect for certain
lengths
- Move multicast address setup into address setup function to cleanup
and reduce code size
- Do not use fast-frag transfer for multicast payloads
- Cleanup pipe_address function
  • Loading branch information
TMRh20 committed Jan 20, 2015
1 parent a3b27d9 commit c90027f
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 45 deletions.
118 changes: 74 additions & 44 deletions RF24Network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,7 @@ void RF24Network::begin(uint8_t _channel, uint16_t _node_address )
while (i--){
radio.openReadingPipe(i,pipe_address(_node_address,i));
}
#if defined (RF24NetworkMulticast)
uint8_t count = 0; uint16_t addy = _node_address;
while(addy) {
addy/=8;
count++;
}
multicast_level = count;
#endif

radio.startListening();

}
Expand Down Expand Up @@ -163,7 +156,7 @@ uint8_t RF24Network::update(void)
#endif

uint8_t returnVal = header->type;

//printf("got\n");
// Is this for us?
if ( header->to_node == node_address ){

Expand Down Expand Up @@ -233,12 +226,17 @@ uint8_t RF24Network::update(void)
return EXTERNAL_DATA_TYPE;
}
#endif
}else{
}else{
//printf("route\n");
//isRouted=0;
write(header->to_node,1); //Send it on, indicate it is a routed payload
//isRouted=0;
}
#else
//isRouted=0;
//if(radio.available()){printf("------FLUSHED DATA --------------");}
write(header->to_node,1); //Send it on, indicate it is a routed payload
//isRouted=0;
#endif
}

Expand Down Expand Up @@ -678,15 +676,18 @@ bool RF24Network::write(RF24NetworkHeader& header,const void* message, size_t le
}*/

//Divide the message payload into chunks of max_frame_payload_size
uint8_t fragment_id = 1 + ((len - 1) / max_frame_payload_size); //the number of fragments to send = ceil(len/max_frame_payload_size)
uint8_t fragment_id = (len % max_frame_payload_size != 0) + ((len ) / max_frame_payload_size); //the number of fragments to send = ceil(len/max_frame_payload_size)

uint8_t msgCount = 0;

IF_SERIAL_DEBUG_FRAGMENTATION(printf("%lu: FRG Total message fragments %d\n\r",millis(),fragment_id););

fastFragTransfer = 1;
if(header.to_node != 0100){
fastFragTransfer = 1;
}
if(fastFragTransfer){
radio.stopListening();
}
}
uint8_t retriesPerFrag = 0;


Expand All @@ -710,6 +711,7 @@ bool RF24Network::write(RF24NetworkHeader& header,const void* message, size_t le
size_t offset = msgCount*max_frame_payload_size;
size_t fragmentLen = rf24_min(len-offset,max_frame_payload_size);

//delay(3);
//Try to send the payload chunk with the copied header
frame_size = sizeof(RF24NetworkHeader)+fragmentLen;
bool ok = _write(fragmentHeader,((char *)message)+offset,fragmentLen,writeDirect);
Expand All @@ -722,9 +724,10 @@ bool RF24Network::write(RF24NetworkHeader& header,const void* message, size_t le
retriesPerFrag = 0;
fragment_id--;
msgCount++;
//delayMicroseconds(130);
}

if(writeDirect != 070){ delay(3); } //Delay 5ms between sending multicast payloads
if(writeDirect != 070){ delay(2); } //Delay 5ms between sending multicast payloads

if (!ok && retriesPerFrag >= 3) {
IF_SERIAL_DEBUG_FRAGMENTATION(printf("%lu: FRG TX with fragmentID '%d' failed after %d fragments. Abort.\n\r",millis(),fragment_id,msgCount););
Expand All @@ -740,7 +743,7 @@ bool RF24Network::write(RF24NetworkHeader& header,const void* message, size_t le

//Check and modify counters
if((msgCount%3) == 0){
//delay(2);
//delay(3);
}
}
fastFragTransfer = 0;
Expand Down Expand Up @@ -817,6 +820,10 @@ bool RF24Network::write(uint16_t to_node, uint8_t directTo) // Direct To: 0 = F
bool isAckType;
if(frame_buffer[6] > 64 && frame_buffer[6] < 192 ){ ++isAckType; }

if( ( (frame_buffer[7] % 2) && frame_buffer[6] == NETWORK_MORE_FRAGMENTS) ){
isAckType = 0;
}

// Throw it away if it's not a valid address
if ( !is_valid_address(to_node) )
return false;
Expand Down Expand Up @@ -956,21 +963,33 @@ bool RF24Network::logicalToPhysicalAddress(logicalToPhysicalStruct *conversionIn
bool RF24Network::write_to_pipe( uint16_t node, uint8_t pipe, bool multicast )
{
bool ok = false;

uint64_t out_pipe = pipe_address( node, pipe );

#if !defined (DUAL_HEAD_RADIO)
// Open the correct pipe for writing.
#if !defined (DUAL_HEAD_RADIO)
// Open the correct pipe for writing.
// First, stop listening so we can talk


if(!fastFragTransfer){
radio.stopListening();
}
radio.openWritingPipe(out_pipe);
ok = radio.writeFast(frame_buffer, frame_size,multicast);
radio.openWritingPipe(out_pipe);

if(!fastFragTransfer){
// if( (frame_buffer[7] % 2) && frame_buffer[6] == NETWORK_MORE_FRAGMENTS){
// ok = true;
// radio.startFastWrite(frame_buffer, frame_size,multicast);
//}else{
ok = radio.writeFast(frame_buffer, frame_size,multicast);
//}

//if(!fastFragTransfer){
ok = radio.txStandBy(txTimeout);
}
//radio.startListening();
//}
// if(multicast){

// }

#else
radio1.openWritingPipe(out_pipe);
radio1.writeFast(frame_buffer, frame_size);
Expand Down Expand Up @@ -1037,10 +1056,21 @@ bool RF24Network::is_descendant( uint16_t node )
void RF24Network::setup_address(void)
{
// First, establish the node_mask
uint16_t node_mask_check = 0xFFFF;
while ( node_address & node_mask_check )
uint16_t node_mask_check = 0xFFFF;
#if defined (RF24NetworkMulticast)
uint8_t count = 0;
#endif

while ( node_address & node_mask_check ){
node_mask_check <<= 3;

#if defined (RF24NetworkMulticast)
count++;
}
multicast_level = count;
#else
}
#endif

node_mask = ~ node_mask_check;

// parent mask is the next level down
Expand All @@ -1059,7 +1089,6 @@ void RF24Network::setup_address(void)
}
parent_pipe = i;


IF_SERIAL_DEBUG( printf_P(PSTR("setup_address node=0%o mask=0%o parent=0%o pipe=0%o\n\r"),node_address,node_mask,parent_node,parent_pipe););

}
Expand Down Expand Up @@ -1162,26 +1191,27 @@ uint64_t pipe_address( uint16_t node, uint8_t pipe )

// Translate the address to use our optimally chosen radio address bytes
uint8_t count = 1; uint16_t dec = node;
#if defined (RF24NetworkMulticast)
if(pipe != 0 || !node){
#endif
while(dec){

while(dec){
#if defined (RF24NetworkMulticast)
if(pipe != 0 || !node)
#endif
out[count]=address_translation[(dec % 8)]; // Convert our decimal values to octal, translate them to address bytes, and set our address
dec /= 8;
count++;

dec /= 8;
count++;
}

#if defined (RF24NetworkMulticast)
if(pipe != 0 || !node)
#endif
out[0] = address_translation[pipe];
#if defined (RF24NetworkMulticast)
else
out[1] = address_translation[count-1];
#endif


out[0] = address_translation[pipe]; // Set last byte by pipe number
#if defined (RF24NetworkMulticast)
}else{
while(dec){
dec/=8;
count++;
}
out[1] = address_translation[count-1];
}

#endif

#if defined (RF24_LINUX)
IF_SERIAL_DEBUG(uint32_t* top = reinterpret_cast<uint32_t*>(out+1);printf_P(PSTR("%u: NET Pipe %i on node 0%o has address %x%x\n\r"),millis(),pipe,node,*top,*out));
Expand Down
2 changes: 1 addition & 1 deletion RF24Network.h
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ class RF24Network
bool is_valid_address( uint16_t node );

bool fastFragTransfer;
bool fastTransferStarted;
//bool isRouted;

private:

Expand Down

1 comment on commit c90027f

@wilmsn
Copy link
Contributor

@wilmsn wilmsn commented on c90027f Jan 20, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sync with TMRh20 sources

Please sign in to comment.