Skip to content

Commit

Permalink
Workaround for printf issue (#207)
Browse files Browse the repository at this point in the history
* Workaround for printf issue

Workaround for printf issue identified in issue nRF24/RF24Gateway#25

* Change all prints to print millis() separate

* Only update Arduino printfs

* More printf fun

- Removed all the millis() printfs
- Removed header.toString calls

* Delete commented debugs

Delete commented millis() debug statements
  • Loading branch information
TMRh20 authored May 27, 2023
1 parent 8c90bbe commit 8e444a2
Showing 1 changed file with 27 additions and 28 deletions.
55 changes: 27 additions & 28 deletions RF24Network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,17 +147,16 @@ uint8_t RF24Network::update(void)
if (frame_size < sizeof(RF24NetworkHeader) || !is_valid_address(header->to_node) || !is_valid_address(header->from_node)) {
continue;
}

IF_SERIAL_DEBUG(printf_P(PSTR("%u: MAC Received " PRIPSTR
"\n\r"),
millis(), header->toString()));
//IF_SERIAL_DEBUG(printf_P(PSTR("MAC Received " PRIPSTR
// "\n\r"),
// header->toString()));
#if defined(RF24_LINUX)
if (frame_size) {
IF_SERIAL_DEBUG_FRAGMENTATION_L2(printf_P(PSTR("%u: FRG Rcv frame size %i\n"), millis(), frame_size););
IF_SERIAL_DEBUG_FRAGMENTATION_L2(printf_P(PSTR("%u: FRG Rcv frame "), millis()); const char* charPtr = reinterpret_cast<const char*>(frame_buffer); for (uint16_t i = 0; i < frame_size; i++) { printf_P(PSTR("%02X "), charPtr[i]); }; printf_P(PSTR("\n\r")));
}
#else
IF_SERIAL_DEBUG(const uint16_t* i = reinterpret_cast<const uint16_t*>(frame_buffer + sizeof(RF24NetworkHeader)); printf_P(PSTR("%u: NET message %04x\n\r"), millis(), *i));
IF_SERIAL_DEBUG(const uint16_t* i = reinterpret_cast<const uint16_t*>(frame_buffer + sizeof(RF24NetworkHeader)); printf_P(PSTR("NET message %04x\n\r"), *i));
#endif

returnVal = header->type;
Expand All @@ -181,7 +180,7 @@ uint8_t RF24Network::update(void)
continue;
}
if ((returnSysMsgs && header->type > MAX_USER_DEFINED_HEADER_TYPE) || header->type == NETWORK_ACK) {
IF_SERIAL_DEBUG_ROUTING(printf_P(PSTR("%u: MAC System payload rcvd %d\n"), millis(), returnVal););
IF_SERIAL_DEBUG_ROUTING(printf_P(PSTR("MAC System payload rcvd %d\n"), returnVal););
if (header->type != NETWORK_FIRST_FRAGMENT && header->type != NETWORK_MORE_FRAGMENTS && header->type != EXTERNAL_DATA_TYPE && header->type != NETWORK_LAST_FRAGMENT) {
return returnVal;
}
Expand Down Expand Up @@ -214,7 +213,7 @@ uint8_t RF24Network::update(void)
uint8_t val = enqueue(header);

if (multicastRelay) {
IF_SERIAL_DEBUG_ROUTING(printf_P(PSTR("%u: MAC FWD multicast frame from 0%o to level %u\n"), millis(), header->from_node, _multicast_level + 1););
IF_SERIAL_DEBUG_ROUTING(printf_P(PSTR("MAC FWD multicast frame from 0%o to level %u\n"), header->from_node, _multicast_level + 1););
if ((node_address >> 3) != 0) {
// for all but the first level of nodes, those not directly connected to the master, we add the total delay per level
delayMicroseconds(600 * 4);
Expand Down Expand Up @@ -411,7 +410,7 @@ uint8_t RF24Network::enqueue(RF24NetworkHeader* header)
bool result = false;
uint16_t message_size = frame_size - sizeof(RF24NetworkHeader);

IF_SERIAL_DEBUG(printf_P(PSTR("%u: NET Enqueue @%x\n"), millis(), next_frame - frame_queue));
IF_SERIAL_DEBUG(printf_P(PSTR("NET Enqueue @%x\n"), next_frame - frame_queue));

#if !defined(DISABLE_FRAGMENTATION)

Expand Down Expand Up @@ -630,9 +629,9 @@ uint16_t RF24Network::read(RF24NetworkHeader& header, void* message, uint16_t ma
if (maxlen > 0) {
maxlen = rf24_min(maxlen, bufsize);
memcpy(message, frame_queue + 10, maxlen);
IF_SERIAL_DEBUG(printf_P(PSTR("%u: NET message size %d\n"), millis(), bufsize););
IF_SERIAL_DEBUG(printf_P(PSTR("NET message size %d\n"), bufsize););

IF_SERIAL_DEBUG(uint16_t len = maxlen; printf_P(PSTR("%u: NET message "), millis()); const uint8_t* charPtr = reinterpret_cast<const uint8_t*>(message); while (len--) { printf_P(PSTR("%02x "), charPtr[len]); } printf_P(PSTR("\n\r")));
IF_SERIAL_DEBUG(uint16_t len = maxlen; printf_P(PSTR("NET message ")); const uint8_t* charPtr = reinterpret_cast<const uint8_t*>(message); while (len--) { printf_P(PSTR("%02x "), charPtr[len]); } printf_P(PSTR("\n\r")));
}
next_frame -= bufsize + 10;
uint8_t padding = 0;
Expand Down Expand Up @@ -683,8 +682,9 @@ bool RF24Network::write(RF24NetworkHeader& header, const void* message, uint16_t
return _write(header, message, len, writeDirect);
}
//Check payload size

if (len > MAX_PAYLOAD_SIZE) {
IF_SERIAL_DEBUG(printf_P(PSTR("%u: NET write message failed. Given 'len' %d is bigger than the MAX Payload size %i\n\r"), millis(), len, MAX_PAYLOAD_SIZE););
IF_SERIAL_DEBUG(printf_P(PSTR("NET write message failed. Given 'len' %d is bigger than the MAX Payload size %i\n\r"), len, MAX_PAYLOAD_SIZE););
return false;
}

Expand All @@ -693,7 +693,7 @@ bool RF24Network::write(RF24NetworkHeader& header, const void* message, uint16_t

uint8_t msgCount = 0;

IF_SERIAL_DEBUG_FRAGMENTATION(printf_P(PSTR("%u: FRG Total message fragments %d\n\r"), millis(), fragment_id););
IF_SERIAL_DEBUG_FRAGMENTATION(printf_P(PSTR("FRG Total message fragments %d\n\r"), fragment_id););

if (header.to_node != NETWORK_MULTICAST_ADDRESS) {
networkFlags |= FLAG_FAST_FRAG;
Expand Down Expand Up @@ -741,12 +741,12 @@ bool RF24Network::write(RF24NetworkHeader& header, const void* message, uint16_t
//if(writeDirect != NETWORK_AUTO_ROUTING){ delay(2); } //Delay 2ms between sending multicast payloads

if (!ok && retriesPerFrag >= 3) {
IF_SERIAL_DEBUG_FRAGMENTATION(printf_P(PSTR("%u: FRG TX with fragmentID '%d' failed after %d fragments. Abort.\n\r"), millis(), fragment_id, msgCount));
IF_SERIAL_DEBUG_FRAGMENTATION(printf_P(PSTR("FRG TX with fragmentID '%d' failed after %d fragments. Abort.\n\r"), fragment_id, msgCount));
break;
}

//Message was successful sent
IF_SERIAL_DEBUG_FRAGMENTATION_L2(printf_P(PSTR("%u: FRG message transmission with fragmentID '%d' successful.\n\r"), millis(), fragment_id));
// Message was successful sent
IF_SERIAL_DEBUG_FRAGMENTATION_L2(printf_P(PSTR("FRG message transmission with fragmentID '%d' successful.\n\r"), fragment_id));
}
header.type = type;
if (networkFlags & FLAG_FAST_FRAG) {
Expand All @@ -756,9 +756,9 @@ bool RF24Network::write(RF24NetworkHeader& header, const void* message, uint16_t
}
networkFlags &= ~FLAG_FAST_FRAG;

//Return true if all the chunks where sent successfully
// Return true if all the chunks where sent successfully
IF_SERIAL_DEBUG_FRAGMENTATION(printf_P(PSTR("FRG total message fragments sent %i.\r\n"), msgCount););

IF_SERIAL_DEBUG_FRAGMENTATION(printf_P(PSTR("%u: FRG total message fragments sent %i.\r\n"), millis(), msgCount););
if (!ok || fragment_id > 0) {
return false;
}
Expand All @@ -777,9 +777,9 @@ bool RF24Network::_write(RF24NetworkHeader& header, const void* message, uint16_
// Build the full frame to send
memcpy(frame_buffer, &header, sizeof(RF24NetworkHeader));

IF_SERIAL_DEBUG(printf_P(PSTR("%u: NET Sending " PRIPSTR
"\n\r"),
millis(), header.toString()));
//IF_SERIAL_DEBUG(printf_P(PSTR("NET Sending " PRIPSTR
// "\n\r"),
// header.toString()));
if (len) {
#if defined(RF24_LINUX)
memcpy(frame_buffer + sizeof(RF24NetworkHeader), message, rf24_min(frame_size - sizeof(RF24NetworkHeader), len));
Expand All @@ -789,7 +789,7 @@ bool RF24Network::_write(RF24NetworkHeader& header, const void* message, uint16_

memcpy(frame_buffer + sizeof(RF24NetworkHeader), message, len);

IF_SERIAL_DEBUG(uint16_t tmpLen = len; printf_P(PSTR("%u: NET message "), millis()); const uint8_t* charPtr = reinterpret_cast<const uint8_t*>(message); while (tmpLen--) { printf_P(PSTR("%02x "), charPtr[tmpLen]); } printf_P(PSTR("\n\r")));
IF_SERIAL_DEBUG(uint16_t tmpLen = len; printf_P(PSTR("NET message ")); const uint8_t* charPtr = reinterpret_cast<const uint8_t*>(message); while (tmpLen--) { printf_P(PSTR("%02x "), charPtr[tmpLen]); } printf_P(PSTR("\n\r")));
#endif
}

Expand Down Expand Up @@ -839,15 +839,15 @@ bool RF24Network::write(uint16_t to_node, uint8_t sendType)
logicalToPhysicalStruct conversion = {to_node, sendType, 0};
logicalToPhysicalAddress(&conversion);

IF_SERIAL_DEBUG(printf_P(PSTR("%u: MAC Sending to 0%o via 0%o on pipe %x\n\r"), millis(), to_node, conversion.send_node, conversion.send_pipe));
IF_SERIAL_DEBUG(printf_P(PSTR("MAC Sending to 0%o via 0%o on pipe %x\n\r"), to_node, conversion.send_node, conversion.send_pipe));
/**Write it*/
if (sendType == TX_ROUTED && conversion.send_node == to_node && isAckType) {
delay(2);
}
ok = write_to_pipe(conversion.send_node, conversion.send_pipe, conversion.multicast);

if (!ok) {
IF_SERIAL_DEBUG_ROUTING(printf_P(PSTR("%u: MAC Send fail to 0%o via 0%o on pipe %x\n\r"), millis(), to_node, conversion.send_node, conversion.send_pipe););
IF_SERIAL_DEBUG_ROUTING(printf_P(PSTR("MAC Send fail to 0%o via 0%o on pipe %x\n\r"), to_node, conversion.send_node, conversion.send_pipe););
}

if (sendType == TX_ROUTED && ok && conversion.send_node == to_node && isAckType) {
Expand All @@ -868,8 +868,8 @@ bool RF24Network::write(uint16_t to_node, uint8_t sendType)
frame_size = sizeof(RF24NetworkHeader);
write_to_pipe(conversion.send_node, conversion.send_pipe, conversion.multicast);

//dynLen=0;
IF_SERIAL_DEBUG_ROUTING(printf_P(PSTR("%u: MAC Route OK to 0%o ACK sent to 0%o\n"), millis(), to_node, header->from_node););
// dynLen=0;
IF_SERIAL_DEBUG_ROUTING(printf_P(PSTR("MAC Route OK to 0%o ACK sent to 0%o\n"), to_node, header->from_node););
}

if (ok && conversion.send_node != to_node && (sendType == TX_NORMAL || sendType == USER_TX_TO_LOGICAL_ADDRESS) && isAckType) {
Expand All @@ -887,7 +887,7 @@ bool RF24Network::write(uint16_t to_node, uint8_t sendType)
delayMicroseconds(900);
#endif
if (millis() - reply_time > routeTimeout) {
IF_SERIAL_DEBUG_ROUTING(printf_P(PSTR("%u: MAC Network ACK fail from 0%o via 0%o on pipe %x\n\r"), millis(), to_node, conversion.send_node, conversion.send_pipe););
IF_SERIAL_DEBUG_ROUTING(printf_P(PSTR("MAC Network ACK fail from 0%o via 0%o on pipe %x\n\r"), to_node, conversion.send_node, conversion.send_pipe););
ok = false;
break;
}
Expand Down Expand Up @@ -1176,8 +1176,7 @@ uint64_t RF24Network::pipe_address(uint16_t node, uint8_t pipe)
else
out[1] = address_translation[count - 1];
#endif

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));
IF_SERIAL_DEBUG(uint32_t* top = reinterpret_cast<uint32_t*>(out + 1); printf_P(PSTR("NET Pipe %i on node 0%o has address %x%x\n\r"), pipe, node, *top, *out));

return result;
}
Expand Down

0 comments on commit 8e444a2

Please sign in to comment.