Skip to content

Commit

Permalink
Added checking for valid address
Browse files Browse the repository at this point in the history
  • Loading branch information
maniacbug committed Jun 15, 2011
1 parent 3684a91 commit 82576d0
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions RF24Network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
uint16_t RF24NetworkHeader::next_id = 1;

uint64_t pipe_address( uint16_t node, uint8_t pipe );
bool is_valid_address( uint16_t node );

/******************************************************************/

Expand All @@ -38,6 +39,9 @@ RF24Network::RF24Network( RF24& _radio ): radio(_radio), next_frame(frame_queue)

void RF24Network::begin(uint8_t _channel, uint16_t _node_address )
{
if (! is_valid_address(_node_address) )
return;

node_address = _node_address;

// Set up the radio the way we want it to look
Expand Down Expand Up @@ -79,6 +83,10 @@ void RF24Network::update(void)
IF_SERIAL_DEBUG(printf_P(PSTR("%lu: MAC Received on %u %s\n\r"),millis(),pipe_num,header.toString()));
IF_SERIAL_DEBUG(const uint16_t* i = reinterpret_cast<const uint16_t*>(frame_buffer + sizeof(RF24NetworkHeader));printf_P(PSTR("%lu: NET message %04x\n\r"),millis(),*i));

// Throw it away if it's not a valid address
if ( !is_valid_address(header.to_node) )
continue;

// Is this for us?
if ( header.to_node == node_address )
// Add it to the buffer of frames for us
Expand Down Expand Up @@ -204,6 +212,10 @@ bool RF24Network::write(uint16_t to_node)
{
bool ok = false;

// Throw it away if it's not a valid address
if ( !is_valid_address(to_node) )
return false;

// First, stop listening so we can talk.
radio.stopListening();

Expand Down Expand Up @@ -377,6 +389,27 @@ uint8_t RF24Network::pipe_to_descendant( uint16_t node )

/******************************************************************/

bool is_valid_address( uint16_t node )
{
bool result = true;

while(node)
{
uint8_t digit = node & 0B111;
if (digit < 1 || digit > 5)
{
result = false;
printf_P(PSTR("*** WARNING *** Invalid address 0%o\n\r"),node);
break;
}
node >>= 3;
}

return result;
}

/******************************************************************/

uint64_t pipe_address( uint16_t node, uint8_t pipe )
{
static uint8_t pipe_segment[] = { 0x3c, 0x5a, 0x69, 0x96, 0xa5, 0xc3 };
Expand Down

0 comments on commit 82576d0

Please sign in to comment.