diff --git a/README.md b/README.md index dbc05f6..53fab5a 100644 --- a/README.md +++ b/README.md @@ -2,13 +2,16 @@ Port of Reticulum Network Stack to C++ specifically but not exclusively targeting 32-bit and better MCUs. +Note that this is a library implementing the Reticulum Network Stack and not a useful application on its own. For an actual application, see [microReticulum_Firmware](https://github.com/attermann/microReticulum_Firmware) which is a version of RNode firmware that implements a Reticulm Transport Node. + ## Dependencies Build environment is configured for use in [VSCode](https://code.visualstudio.com/) and [PlatformIO](https://platformio.org/). This API is dependent on the following external libraries: -- [ArduinoJson](https://github.com/bblanchon/ArduinoJson) - [rweather/Crypto](https://github.com/rweather/arduinolibs) +- [ArduinoJson](https://github.com/bblanchon/ArduinoJson) +- [MsgPack](https://github.com/hideakitai/MsgPack) ## Build Options diff --git a/examples/common/lora_interface/LoRaInterface.cpp b/examples/common/lora_interface/LoRaInterface.cpp index 79a0a28..207ad6c 100644 --- a/examples/common/lora_interface/LoRaInterface.cpp +++ b/examples/common/lora_interface/LoRaInterface.cpp @@ -40,9 +40,16 @@ bool LoRaInterface::start() { INFO("LoRa initializing..."); #ifdef ARDUINO + +#ifdef BOARD_NRF52 + SPI.setPins(RADIO_MISO_PIN, RADIO_SCLK_PIN, RADIO_MOSI_PIN); + SPI.begin(); +#else //SPI.begin(RADIO_SCLK_PIN, RADIO_MISO_PIN, RADIO_MOSI_PIN); SPI.begin(); - delay(1500); +#endif + +delay(1500); LoRa.setPins(RADIO_CS_PIN, RADIO_RST_PIN, RADIO_DIO0_PIN); diff --git a/library.json b/library.json index b9997cb..d4da5f1 100644 --- a/library.json +++ b/library.json @@ -1,8 +1,8 @@ { "name": "microReticulum", - "version": "0.2.2", + "version": "0.2.3", "description": "Port of Reticulum Network Stack to C++ specifically but not exclusively targeting 32-bit and better MCUs", - "keywords": "reticulum, rns, mcu, esp32, nrf52", + "keywords": "reticulum, rns, mesh, embedded, mcu, esp32, nrf52, native", "repository": { "type": "git", diff --git a/library.properties b/library.properties index 9af61af..d791489 100644 --- a/library.properties +++ b/library.properties @@ -1,10 +1,10 @@ name=microReticulum -version=0.2.2 +version=0.2.3 author=Chad Attermann maintainer=Chad Attermann sentence=Port of Reticulum Network Stack to C++ -paragraph=Port of Reticulum Network Stack to C++ specifically but not exclusively targeting 32-bit and better MCUs +paragraph=Specifically but not exclusively targeting 32-bit and better MCUs category=Communication url=https://github.com/attermann/microReticulum.git architectures=* -depends=ArduinoJson +depends=ArduinoJson,MsgPack diff --git a/src/Interface.h b/src/Interface.h index 480d018..7ec2de2 100644 --- a/src/Interface.h +++ b/src/Interface.h @@ -53,7 +53,7 @@ namespace RNS { // CBA Internal method to handle housekeeping for data going out on interface void handle_outgoing(const Bytes& data); // CBA Internal method to handle data coming in on interface and pass on to transport - void handle_incoming(const Bytes& data); + virtual void handle_incoming(const Bytes& data); virtual const Bytes get_hash() const { return Identity::full_hash({toString()}); diff --git a/src/Packet.cpp b/src/Packet.cpp index 34b0b62..0e68a49 100644 --- a/src/Packet.cpp +++ b/src/Packet.cpp @@ -18,7 +18,7 @@ ProofDestination::ProofDestination(const Packet& packet) : Destination({Type::NO { } -Packet::Packet(const Destination& destination, const Interface& attached_interface, const Bytes& data, types packet_type /*= DATA*/, context_types context /*= CONTEXT_NONE*/, Type::Transport::types transport_type /*= Type::Transport::BROADCAST*/, header_types header_type /*= HEADER_1*/, const Bytes& transport_id /*= {Bytes::NONE}*/, bool create_receipt /*= true*/, context_flag context_flag /*= FLAG_UNSET*/) : +Packet::Packet(const Destination& destination, const Interface& attached_interface, const Bytes& data, types packet_type /*= DATA*/, context_types context /*= CONTEXT_NONE*/, Type::Transport::types transport_type /*= Type::Transport::BROADCAST*/, header_types header_type /*= HEADER_1*/, const Bytes& transport_id /*= {Bytes::NONE}*/, bool create_receipt /*= true*/, context_flags context_flag /*= FLAG_UNSET*/) : _object(new Object(destination, attached_interface)) { @@ -59,7 +59,7 @@ Packet::Packet(const Destination& destination, const Interface& attached_interfa } // CBA LINK -Packet::Packet(const Link& link, const Bytes& data, Type::Packet::types packet_type /*= Type::Packet::DATA*/, Type::Packet::context_types context /*= Type::Packet::CONTEXT_NONE*/, context_flag context_flag /*= FLAG_UNSET*/) : +Packet::Packet(const Link& link, const Bytes& data, Type::Packet::types packet_type /*= Type::Packet::DATA*/, Type::Packet::context_types context /*= Type::Packet::CONTEXT_NONE*/, context_flags context_flag /*= FLAG_UNSET*/) : //_object(new Object(link)) //Packet(link.destination(), data, packet_type, context, Type::Transport::BROADCAST, Type::Packet::HEADER_1, {Bytes::NONE}, true, context_flag) // CBA Must use a destination that targets the Link itself instead of the original destination used to create the link @@ -79,14 +79,14 @@ uint8_t Packet::get_packed_flags() { uint8_t packed_flags = 0; if (_object->_context == LRPROOF) { TRACE("***** Packing with LINK type"); - packed_flags = (_object->_header_type << 6) | (_object->_transport_type << 4) | (Type::Destination::LINK << 2) | _object->_packet_type; + packed_flags = (_object->_header_type << 6) | (_object->_context_flag << 5) | (_object->_transport_type << 4) | (Type::Destination::LINK << 2) | _object->_packet_type; } else { if (!_object->_destination) { throw std::logic_error("Packet destination is required"); } TRACEF("***** Packing with %d type", _object->_destination.type()); - packed_flags = (_object->_header_type << 6) | (_object->_transport_type << 4) | (_object->_destination.type() << 2) | _object->_packet_type; + packed_flags = (_object->_header_type << 6) | (_object->_context_flag << 5) | (_object->_transport_type << 4) | (_object->_destination.type() << 2) | _object->_packet_type; } return packed_flags; } @@ -94,7 +94,8 @@ TRACEF("***** Packing with %d type", _object->_destination.type()); void Packet::unpack_flags(uint8_t flags) { assert(_object); _object->_header_type = static_cast((flags & 0b01000000) >> 6); - _object->_transport_type = static_cast((flags & 0b00110000) >> 4); + _object->_context_flag = static_cast((flags & 0b00100000) >> 5); + _object->_transport_type = static_cast((flags & 0b00010000) >> 4); _object->_destination_type = static_cast((flags & 0b00001100) >> 2); _object->_packet_type = static_cast(flags & 0b00000011); } diff --git a/src/Packet.h b/src/Packet.h index cd99280..c6898ef 100644 --- a/src/Packet.h +++ b/src/Packet.h @@ -160,7 +160,7 @@ namespace RNS { Type::Packet::header_types header_type = Type::Packet::HEADER_1, const Bytes& transport_id = {Bytes::NONE}, bool create_receipt = true, - Type::Packet::context_flag context_flag = Type::Packet::FLAG_UNSET + Type::Packet::context_flags context_flag = Type::Packet::FLAG_UNSET ); Packet( const Destination& destination, @@ -171,7 +171,7 @@ namespace RNS { Type::Packet::header_types header_type = Type::Packet::HEADER_1, const Bytes& transport_id = {Bytes::NONE}, bool create_receipt = true, - Type::Packet::context_flag context_flag = Type::Packet::FLAG_UNSET + Type::Packet::context_flags context_flag = Type::Packet::FLAG_UNSET ) : Packet(destination, {Type::NONE}, data, packet_type, context, transport_type, header_type, transport_id, create_receipt, context_flag) {} // CBA LINK Packet( @@ -179,7 +179,7 @@ namespace RNS { const Bytes& data, Type::Packet::types packet_type = Type::Packet::DATA, Type::Packet::context_types context = Type::Packet::CONTEXT_NONE, - Type::Packet::context_flag context_flag = Type::Packet::FLAG_UNSET + Type::Packet::context_flags context_flag = Type::Packet::FLAG_UNSET ); virtual ~Packet() { MEM("Packet object destroyed, this: " + std::to_string((uintptr_t)this) + ", data: " + std::to_string((uintptr_t)_object.get())); @@ -229,6 +229,7 @@ namespace RNS { inline const Interface& attached_interface() const { assert(_object); return _object->_attached_interface; } inline const Interface& receiving_interface() const { assert(_object); return _object->_receiving_interface; } inline Type::Packet::header_types header_type() const { assert(_object); return _object->_header_type; } + inline Type::Packet::context_flags context_flag() const { assert(_object); return _object->_context_flag; } inline Type::Transport::types transport_type() const { assert(_object); return _object->_transport_type; } inline Type::Destination::types destination_type() const { assert(_object); return _object->_destination_type; } inline Type::Packet::types packet_type() const { assert(_object); return _object->_packet_type; } @@ -293,7 +294,7 @@ namespace RNS { Type::Destination::types _destination_type = Type::Destination::SINGLE; Type::Packet::types _packet_type = Type::Packet::DATA; Type::Packet::context_types _context = Type::Packet::CONTEXT_NONE; - uint8_t _context_flag = Type::Packet::FLAG_UNSET; + Type::Packet::context_flags _context_flag = Type::Packet::FLAG_UNSET; uint8_t _flags = 0; uint8_t _hops = 0; diff --git a/src/Transport.cpp b/src/Transport.cpp index bf4b70c..26d1815 100644 --- a/src/Transport.cpp +++ b/src/Transport.cpp @@ -374,7 +374,9 @@ using namespace RNS::Utilities; announce_context, Type::Transport::TRANSPORT, Type::Packet::HEADER_2, - Transport::_identity.hash() + Transport::_identity.hash(), + true, + announce_entry._packet.context_flag() ); new_packet.hops(announce_entry._hops); @@ -1151,7 +1153,7 @@ using namespace RNS::Utilities; } /*static*/ void Transport::inbound(const Bytes& raw, const Interface& interface /*= {Type::NONE}*/) { - TRACE("Transport::inbound()"); + TRACEF("Transport::inbound: received %d bytes", raw.size()); ++_packets_received; // CBA if (_callbacks._receive_packet) { @@ -1891,7 +1893,9 @@ using namespace RNS::Utilities; announce_context, Type::Transport::TRANSPORT, Type::Packet::HEADER_2, - _identity.hash() + _identity.hash(), + true, + packet.context_flag() ); new_announce.hops(packet.hops()); @@ -1910,7 +1914,9 @@ using namespace RNS::Utilities; announce_context, Type::Transport::TRANSPORT, Type::Packet::HEADER_2, - _identity.hash() + _identity.hash(), + true, + packet.context_flag() ); new_announce.hops(packet.hops()); @@ -1945,7 +1951,9 @@ using namespace RNS::Utilities; Type::Packet::PATH_RESPONSE, Type::Transport::TRANSPORT, Type::Packet::HEADER_2, - _identity.hash() + _identity.hash(), + true, + packet.context_flag() ); new_announce.hops(packet.hops()); diff --git a/src/Type.h b/src/Type.h index 37a43c2..4fbf066 100644 --- a/src/Type.h +++ b/src/Type.h @@ -345,7 +345,7 @@ namespace RNS { namespace Type { }; // Context flag values - enum context_flag { + enum context_flags { FLAG_SET = 0x01, FLAG_UNSET = 0x00, }; diff --git a/src/Utilities/Persistence.h b/src/Utilities/Persistence.h index 8e77ae5..cdf0c89 100644 --- a/src/Utilities/Persistence.h +++ b/src/Utilities/Persistence.h @@ -469,7 +469,7 @@ namespace RNS { namespace Persistence { static Bytes _buffer(Type::Persistence::BUFFER_MAXSIZE); template size_t crc(const T& obj) { - TRACE("Persistence::crc"); + //TRACE("Persistence::crc"); _document.set(obj); size_t size = _buffer.capacity(); #ifdef USE_MSGPACK @@ -485,7 +485,7 @@ namespace RNS { namespace Persistence { } template size_t serialize(const T& obj, const char* file_path) { - TRACE("Persistence::serialize"); + //TRACE("Persistence::serialize"); _document.set(obj); size_t size = _buffer.capacity(); #ifdef USE_MSGPACK @@ -516,7 +516,7 @@ namespace RNS { namespace Persistence { } template size_t deserialize(T& obj, const char* file_path) { - TRACE("Persistence::deserialize"); + //TRACE("Persistence::deserialize"); size_t read = RNS::Utilities::OS::read_file(file_path, _buffer); if (read > 0) { TRACEF("Persistence::deserialize: read: %d bytes", _buffer.size()); @@ -547,7 +547,7 @@ namespace RNS { namespace Persistence { #if 1 template size_t crc(std::map& map) { - TRACE("Persistence::crc>"); + //TRACE("Persistence::crc>"); uint32_t crc = 0; crc = Utilities::Crc::crc32(crc, '{'); @@ -583,7 +583,7 @@ namespace RNS { namespace Persistence { } template size_t serialize(std::map& map, const char* file_path, uint32_t& crc) { - TRACE("Persistence::serialize>"); + //TRACE("Persistence::serialize>"); // CBA TODO: Use stream here instead to avoid having to buffer entire structure RNS::FileStream stream = RNS::Utilities::OS::open_file(file_path, RNS::FileStream::MODE_WRITE); @@ -626,7 +626,7 @@ namespace RNS { namespace Persistence { } template size_t deserialize(std::map& map, const char* file_path, uint32_t& crc) { - TRACE("Persistence::deserialize>"); + //TRACE("Persistence::deserialize>"); // CBA TODO: Use stream here instead to avoid having to buffer entire structure RNS::FileStream stream = RNS::Utilities::OS::open_file(file_path, RNS::FileStream::MODE_READ);