diff --git a/examples/link_native/main.cpp b/examples/link_native/main.cpp index fd34d87..7b62a1c 100644 --- a/examples/link_native/main.cpp +++ b/examples/link_native/main.cpp @@ -26,6 +26,7 @@ #include #include #include +#include #endif #include @@ -34,6 +35,7 @@ #include #include #include +#include // Let's define an app name. We'll use this for all // destinations we create. Since this echo example @@ -328,11 +330,27 @@ void cleanup_handler(int signum) { // starts up the desired program mode. int main(int argc, char *argv[]) { + bool log_trace = false; + for (int i = 1; i < argc; ++i) { + if (argv[i] && strcasecmp(argv[i], "--log_trace") == 0) { + log_trace = true; + for (int j = i; j < argc - 1; ++j) { + argv[j] = argv[j + 1]; + } + --argc; + --i; + } + } + #if defined(RNS_MEM_LOG) RNS::loglevel(RNS::LOG_MEM); #else - RNS::loglevel(RNS::LOG_NOTICE); - //RNS::loglevel(RNS::LOG_TRACE); + if (log_trace) { + RNS::loglevel(RNS::LOG_TRACE); + } + else { + RNS::loglevel(RNS::LOG_NOTICE); + } #endif // Register the signal handler for SIGINT diff --git a/examples/link_native/platformio.ini b/examples/link_native/platformio.ini index 3cd43b2..2cd26ad 100644 --- a/examples/link_native/platformio.ini +++ b/examples/link_native/platformio.ini @@ -18,8 +18,6 @@ monitor_speed = 115200 upload_speed = 921600 build_type = debug build_flags = - -DRNS_USE_ALLOCATOR=1 - -DRNS_USE_TLSF=1 ;-DRNS_MEM_LOG lib_deps = ArduinoJson@^7.4.2 diff --git a/library.json b/library.json index d4da5f1..b7fb833 100644 --- a/library.json +++ b/library.json @@ -1,6 +1,6 @@ { "name": "microReticulum", - "version": "0.2.3", + "version": "0.2.4", "description": "Port of Reticulum Network Stack to C++ specifically but not exclusively targeting 32-bit and better MCUs", "keywords": "reticulum, rns, mesh, embedded, mcu, esp32, nrf52, native", "repository": diff --git a/library.properties b/library.properties index d791489..ace0975 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=microReticulum -version=0.2.3 +version=0.2.4 author=Chad Attermann maintainer=Chad Attermann sentence=Port of Reticulum Network Stack to C++ diff --git a/src/Link.cpp b/src/Link.cpp index 31a7ed3..d358f8e 100644 --- a/src/Link.cpp +++ b/src/Link.cpp @@ -176,14 +176,34 @@ Link::Link(const Destination& destination /*= {Type::NONE}*/, Callbacks::establi } /*static*/ Link Link::validate_request( const Destination& owner, const Bytes& data, const Packet& packet) { - if (data.size() == ECPUBSIZE) { + if (data.size() == ECPUBSIZE || data.size() == ECPUBSIZE + LINK_MTU_SIZE) { try { Link link({Type::NONE}, nullptr, nullptr, owner, data.left(ECPUBSIZE/2), data.mid(ECPUBSIZE/2, ECPUBSIZE/2)); link.set_link_id(packet); + + if (data.size() == ECPUBSIZE + LINK_MTU_SIZE) { + DEBUG("Link request includes MTU signalling"); // TODO: Remove debug + try { + uint16_t mtu = mtu_from_lr_packet(packet); + link.mtu((mtu != 0) ? mtu : Type::Reticulum::MTU); + } + catch (std::exception& e) { + ERRORF("An error ocurred while validating link request %s", link.link_id().toHex().c_str()); + link.mtu(Type::Reticulum::MTU); + } + } + + link.mode(mode_from_lr_packet(packet)); + + // TODO: Remove debug + DEBUGF("Incoming link request with mode %d", link.get_mode()); + + link.update_mdu(); link.destination(packet.destination()); link.establishment_timeout(ESTABLISHMENT_TIMEOUT_PER_HOP * std::max((uint8_t)1, packet.hops()) + KEEPALIVE); link.establishment_cost(link.establishment_cost() + packet.raw().size()); VERBOSEF("Validating link request %s", link.link_id().toHex().c_str()); + TRACEF("Link MTU configured to %d", link.mtu()); TRACEF("Establishment timeout is %f for incoming link request %s", link.establishment_timeout(), link.link_id().toHex().c_str()); link.handshake(); link.attached_interface(packet.receiving_interface()); @@ -202,7 +222,7 @@ Link::Link(const Destination& destination /*= {Type::NONE}*/, Callbacks::establi } } else { - DEBUG("Invalid link request payload size, dropping request"); + DEBUGF("Invalid link request payload size (%zu), dropping request", data.size()); return {Type::NONE}; } } @@ -1564,6 +1584,16 @@ void Link::status(Type::Link::status status) { _object->_status = status; } +void Link::mtu(uint16_t mtu) { + assert(_object); + _object->_mtu = mtu; +} + +void Link::mode(RNS::Type::Link::link_mode mode) { + assert(_object); + _object->_mode = mode; +} + //RequestReceipt::RequestReceipt(const Link& link, const PacketReceipt& packet_receipt /*= {Type::NONE}*/, const Resource& resource /*= {Type::NONE}*/, RequestReceipt::Callbacks::response response_callback /*= nullptr*/, RequestReceipt::Callbacks::failed failed_callback /*= nullptr*/, RequestReceipt::Callbacks::progress progress_callback /*= nullptr*/, double timeout /*= 0.0*/, int request_size /*= 0*/) : RequestReceipt::RequestReceipt(const Link& link, const PacketReceipt& packet_receipt, const Resource& resource, RequestReceipt::Callbacks::response response_callback /*= nullptr*/, RequestReceipt::Callbacks::failed failed_callback /*= nullptr*/, RequestReceipt::Callbacks::progress progress_callback /*= nullptr*/, double timeout /*= 0.0*/, int request_size /*= 0*/) : diff --git a/src/Link.h b/src/Link.h index 2b82415..8f02fe6 100644 --- a/src/Link.h +++ b/src/Link.h @@ -198,7 +198,7 @@ namespace RNS { void teardown_packet(const Packet& packet); void link_closed(); void start_watchdog(); - void __watchdog_job(); + void __watchdog_job(); void send_keepalive(); void handle_request(const Bytes& request_id, const ResourceRequest& unpacked_request); void handle_response(const Bytes& request_id, const Bytes& response_data, size_t response_size, size_t response_transfer_size); @@ -259,6 +259,8 @@ namespace RNS { void increment_tx(); void increment_txbytes(uint16_t bytes); void status(Type::Link::status status); + void mtu(uint16_t mtu); + void mode(RNS::Type::Link::link_mode mode); protected: std::shared_ptr _object; diff --git a/src/Transport.cpp b/src/Transport.cpp index 26d1815..1ba487f 100644 --- a/src/Transport.cpp +++ b/src/Transport.cpp @@ -334,7 +334,13 @@ using namespace RNS::Utilities; // auto& announce_entry = pair.second; //TRACE("[0] announce entry data size: " + std::to_string(announce_entry._packet.data().size())); //p announce_entry = Transport.announce_table[destination_hash] - if (announce_entry._retries > Type::Transport::PATHFINDER_R) { + if (announce_entry._retries > 0 && announce_entry._retries >= Type::Transport::LOCAL_REBROADCASTS_MAX) { + TRACE("Completed announce processing for " + destination_hash.toHex() + ", local rebroadcast limit reached"); + // CBA OK to modify collection here since we're immediately exiting iteration + _announce_table.erase(destination_hash); + break; + } + else if (announce_entry._retries > Type::Transport::PATHFINDER_R) { TRACE("Completed announce processing for " + destination_hash.toHex() + ", retry limit reached"); // CBA OK to modify collection here since we're immediately exiting iteration _announce_table.erase(destination_hash); diff --git a/src/Utilities/OS.cpp b/src/Utilities/OS.cpp index f2f318c..3b2ece6 100644 --- a/src/Utilities/OS.cpp +++ b/src/Utilities/OS.cpp @@ -71,6 +71,10 @@ void* operator new(size_t size) { _buffer_size = (size_t)(_buffer_size / 1024) * 1024; TRACEF("buffer_size: %u", _buffer_size); void* raw_buffer = malloc(_buffer_size); +#else + _buffer_size = (size_t)BUFFER_SIZE; + TRACEF("buffer_size: %u", _buffer_size); + void* raw_buffer = malloc(_buffer_size); #endif if (raw_buffer == nullptr) { ERROR("-- allocation for tlsf FAILED");