mirror of
https://github.com/ratspeak/microReticulum
synced 2026-11-27 13:52:14 +00:00
WIP: Fixes, optimizations, and more Transport
Changed all time references to use decimal seconds to match Python reference implementation. Fixed memory issues caused by storing references to objects instead of copies. More implementation of Transport logic. This is the first build that fully implements end-to-end path finding and routing.
This commit is contained in:
1 parent
ea7b4603ed
commit
642486ac7e
28 files changed
+958
-261
No files matched your search
+138
-25
@@ -12,6 +12,7 @@
|
||||
#include "Bytes.h"
|
||||
#include "Type.h"
|
||||
#include "Interfaces/UDPInterface.h"
|
||||
#include "Interfaces/LoRaInterface.h"
|
||||
#include "Utilities/OS.h"
|
||||
|
||||
#ifdef ARDUINO
|
||||
@@ -30,9 +31,14 @@
|
||||
#ifndef NDEBUG
|
||||
//#define RUN_TESTS
|
||||
#endif
|
||||
|
||||
#define RUN_RETICULUM
|
||||
#define UDP_INTERFACE
|
||||
#define LORA_INTERFACE
|
||||
//#define RETICULUM_PACKET_TEST
|
||||
|
||||
#define USER_BUTTON_PIN 38
|
||||
|
||||
// Let's define an app name. We'll use this for all
|
||||
// destinations we create. Since this basic example
|
||||
// is part of a range of example utilities, we'll put
|
||||
@@ -43,6 +49,9 @@ const char* APP_NAME = "example_utilities";
|
||||
const char* fruits[] = {"Peach", "Quince", "Date", "Tangerine", "Pomelo", "Carambola", "Grape"};
|
||||
const char* noble_gases[] = {"Helium", "Neon", "Argon", "Krypton", "Xenon", "Radon", "Oganesson"};
|
||||
|
||||
double last_announce = 0.0;
|
||||
bool send_announce = false;
|
||||
|
||||
class TestInterface : public RNS::Interface {
|
||||
public:
|
||||
TestInterface() : RNS::Interface("TestInterface") {
|
||||
@@ -137,7 +146,7 @@ public:
|
||||
ExampleAnnounceHandler(const char* aspect_filter = nullptr) : AnnounceHandler(aspect_filter) {}
|
||||
virtual ~ExampleAnnounceHandler() {}
|
||||
virtual void received_announce(const RNS::Bytes& destination_hash, const RNS::Identity& announced_identity, const RNS::Bytes& app_data) {
|
||||
RNS::info("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
|
||||
RNS::info("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
|
||||
RNS::info("ExampleAnnounceHandler: destination hash: " + destination_hash.toHex());
|
||||
if (announced_identity) {
|
||||
RNS::info("ExampleAnnounceHandler: announced identity hash: " + announced_identity.hash().toHex());
|
||||
@@ -146,28 +155,44 @@ public:
|
||||
if (app_data) {
|
||||
RNS::info("ExampleAnnounceHandler: app data text: \"" + app_data.toString() + "\"");
|
||||
}
|
||||
RNS::info("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
|
||||
RNS::info("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
|
||||
}
|
||||
};
|
||||
|
||||
// Test packet receive callback
|
||||
void onPacket(const RNS::Bytes& data, const RNS::Packet& packet) {
|
||||
RNS::info("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
|
||||
RNS::head("onPacket: data: " + data.toHex(), RNS::LOG_INFO);
|
||||
RNS::head("onPacket: data string: \"" + data.toString() + "\"", RNS::LOG_INFO);
|
||||
//RNS::head("onPacket: " + packet.debugString(), RNS::LOG_EXTREME);
|
||||
RNS::info("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
|
||||
RNS::info("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
|
||||
RNS::info("onPacket: data: " + data.toHex());
|
||||
RNS::info("onPacket: text: \"" + data.toString() + "\"");
|
||||
//RNS::extreme("onPacket: " + packet.debugString());
|
||||
RNS::info("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
|
||||
}
|
||||
|
||||
// Ping packet receive callback
|
||||
void onPingPacket(const RNS::Bytes& data, const RNS::Packet& packet) {
|
||||
RNS::info("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
|
||||
RNS::info("onPingPacket: data: " + data.toHex());
|
||||
RNS::info("onPingPacket: text: \"" + data.toString() + "\"");
|
||||
//RNS::extreme("onPingPacket: " + packet.debugString());
|
||||
RNS::info("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
|
||||
}
|
||||
|
||||
|
||||
#if defined(RUN_RETICULUM)
|
||||
RNS::Reticulum reticulum({RNS::Type::NONE});
|
||||
RNS::Identity identity({RNS::Type::NONE});
|
||||
RNS::Destination destination({RNS::Type::NONE});
|
||||
|
||||
//TestInterface interface;
|
||||
TestOutInterface outinterface;
|
||||
TestInInterface ininterface;
|
||||
TestLoopbackInterface loopinterface(ininterface);
|
||||
RNS::Interfaces::UDPInterface udpinterface;
|
||||
#ifdef UDP_INTERFACE
|
||||
RNS::Interfaces::UDPInterface udp_interface;
|
||||
#endif
|
||||
#ifdef LORA_INTERFACE
|
||||
RNS::Interfaces::LoRaInterface lora_interface;
|
||||
#endif
|
||||
|
||||
//ExampleAnnounceHandler announce_handler((const char*)"example_utilities.announcesample.fruits");
|
||||
//RNS::HAnnounceHandler announce_handler(new ExampleAnnounceHandler("example_utilities.announcesample.fruits"));
|
||||
@@ -197,6 +222,17 @@ void run_tests() {
|
||||
}
|
||||
#endif
|
||||
|
||||
void reticulum_announce() {
|
||||
if (destination) {
|
||||
RNS::head("Announcing destination...", RNS::LOG_EXTREME);
|
||||
//destination.announce(RNS::bytesFromString(fruits[RNS::Cryptography::randomnum() % 7]));
|
||||
// test path
|
||||
//destination.announce(RNS::bytesFromString(fruits[RNS::Cryptography::randomnum() % 7]), true, nullptr, RNS::bytesFromString("test_tag"));
|
||||
// test packet send
|
||||
destination.announce(RNS::bytesFromString(fruits[RNS::Cryptography::randomnum() % 7]));
|
||||
}
|
||||
}
|
||||
|
||||
#if defined(RUN_RETICULUM)
|
||||
void setup_reticulum() {
|
||||
RNS::info("Setting up Reticulum...");
|
||||
@@ -222,32 +258,71 @@ void setup_reticulum() {
|
||||
RNS::Transport::register_interface(outinterface);
|
||||
RNS::Transport::register_interface(ininterface);
|
||||
RNS::Transport::register_interface(loopinterface);
|
||||
RNS::Transport::register_interface(udpinterface);
|
||||
#ifdef UDP_INTERFACE
|
||||
RNS::Transport::register_interface(udp_interface);
|
||||
#endif
|
||||
#ifdef LORA_INTERFACE
|
||||
RNS::Transport::register_interface(lora_interface);
|
||||
#endif
|
||||
|
||||
#ifdef UDP_INTERFACE
|
||||
RNS::head("Starting UDPInterface...", RNS::LOG_EXTREME);
|
||||
udpinterface.start();
|
||||
udp_interface.start();
|
||||
#endif
|
||||
|
||||
#ifdef LORA_INTERFACE
|
||||
RNS::head("Starting LoRaInterface...", RNS::LOG_EXTREME);
|
||||
lora_interface.start();
|
||||
#endif
|
||||
|
||||
RNS::head("Creating Identity instance...", RNS::LOG_EXTREME);
|
||||
RNS::Identity identity;
|
||||
// new identity
|
||||
//RNS::Identity identity;
|
||||
//identity = RNS::Identity();
|
||||
// predefined identity
|
||||
//RNS::Identity identity(false);
|
||||
identity = RNS::Identity(false);
|
||||
RNS::Bytes prv_bytes;
|
||||
#ifdef ARDUINO
|
||||
prv_bytes.assignHex("78E7D93E28D55871608FF13329A226CABC3903A357388A035B360162FF6321570B092E0583772AB80BC425F99791DF5CA2CA0A985FF0415DAB419BBC64DDFAE8");
|
||||
#else
|
||||
prv_bytes.assignHex("E0D43398EDC974EBA9F4A83463691A08F4D306D4E56BA6B275B8690A2FBD9852E9EBE7C03BC45CAEC9EF8E78C830037210BFB9986F6CA2DEE2B5C28D7B4DE6B0");
|
||||
#endif
|
||||
identity.load_private_key(prv_bytes);
|
||||
// 22.6% (+0.7%)
|
||||
|
||||
RNS::head("Creating Destination instance...", RNS::LOG_EXTREME);
|
||||
RNS::Destination destination(identity, RNS::Type::Destination::IN, RNS::Type::Destination::SINGLE, "app", "aspects");
|
||||
//RNS::Destination destination(identity, RNS::Type::Destination::IN, RNS::Type::Destination::SINGLE, "app", "aspects");
|
||||
destination = RNS::Destination(identity, RNS::Type::Destination::IN, RNS::Type::Destination::SINGLE, "app", "aspects");
|
||||
// 23.0% (+0.4%)
|
||||
|
||||
|
||||
// test data receive packet
|
||||
RNS::head("Registering packet callback with Destination...", RNS::LOG_EXTREME);
|
||||
destination.set_packet_callback(onPacket);
|
||||
destination.set_proof_strategy(RNS::Type::Destination::PROVE_ALL);
|
||||
|
||||
{
|
||||
RNS::head("Creating PING Destination instance...", RNS::LOG_EXTREME);
|
||||
RNS::Destination ping_destination(identity, RNS::Type::Destination::IN, RNS::Type::Destination::SINGLE, "example_utilities", "echo.request");
|
||||
|
||||
RNS::head("Registering packet callback with PING Destination...", RNS::LOG_EXTREME);
|
||||
ping_destination.set_packet_callback(onPingPacket);
|
||||
ping_destination.set_proof_strategy(RNS::Type::Destination::PROVE_ALL);
|
||||
}
|
||||
|
||||
RNS::head("Registering announce handler with Transport...", RNS::LOG_EXTREME);
|
||||
RNS::Transport::register_announce_handler(announce_handler);
|
||||
|
||||
/*
|
||||
RNS::head("Announcing destination...", RNS::LOG_EXTREME);
|
||||
//destination.announce(RNS::bytesFromString(fruits[rand() % 7]));
|
||||
//destination.announce(RNS::bytesFromString(fruits[RNS::Cryptography::randomnum() % 7]));
|
||||
// test path
|
||||
//destination.announce(RNS::bytesFromString(fruits[rand() % 7]), true, nullptr, RNS::bytesFromString("test_tag"));
|
||||
//destination.announce(RNS::bytesFromString(fruits[RNS::Cryptography::randomnum() % 7]), true, nullptr, RNS::bytesFromString("test_tag"));
|
||||
// test packet send
|
||||
destination.announce(RNS::bytesFromString(fruits[rand() % 7]));
|
||||
destination.announce(RNS::bytesFromString(fruits[RNS::Cryptography::randomnum() % 7]));
|
||||
// 23.9% (+0.8%)
|
||||
*/
|
||||
reticulum_announce();
|
||||
|
||||
#if defined (RETICULUM_PACKET_TEST)
|
||||
// test data send packet
|
||||
@@ -258,10 +333,6 @@ void setup_reticulum() {
|
||||
send_packet.pack();
|
||||
RNS::extreme("Test send_packet: " + send_packet.debugString());
|
||||
|
||||
// test data receive packet
|
||||
RNS::head("Registering packet callback with Destination...", RNS::LOG_EXTREME);
|
||||
destination.set_packet_callback(onPacket);
|
||||
|
||||
RNS::head("Creating recv packet...", RNS::LOG_EXTREME);
|
||||
RNS::Packet recv_packet({RNS::Type::NONE}, send_packet.raw());
|
||||
recv_packet.unpack();
|
||||
@@ -286,7 +357,12 @@ void teardown_reticulum() {
|
||||
RNS::Transport::deregister_announce_handler(announce_handler);
|
||||
|
||||
RNS::head("Deregistering Interface instances with Transport...", RNS::LOG_EXTREME);
|
||||
RNS::Transport::deregister_interface(udpinterface);
|
||||
#ifdef UDP_INTERFACE
|
||||
RNS::Transport::deregister_interface(udp_interface);
|
||||
#endif
|
||||
#ifdef LORA_INTERFACE
|
||||
RNS::Transport::deregister_interface(lora_interface);
|
||||
#endif
|
||||
RNS::Transport::deregister_interface(loopinterface);
|
||||
RNS::Transport::deregister_interface(ininterface);
|
||||
RNS::Transport::deregister_interface(outinterface);
|
||||
@@ -299,14 +375,30 @@ void teardown_reticulum() {
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef ARDUINO
|
||||
void userKey(void)
|
||||
{
|
||||
//delay(10);
|
||||
if (digitalRead(USER_BUTTON_PIN) == LOW) {
|
||||
//Serial.print("T-Beam USER button press\n");
|
||||
send_announce = true;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
void setup() {
|
||||
|
||||
#ifdef ARDUINO
|
||||
Serial.begin(115200);
|
||||
Serial.print("Hello from T-Beam on PlatformIO!\n");
|
||||
|
||||
// Setup user button
|
||||
pinMode(USER_BUTTON_PIN, INPUT);
|
||||
attachInterrupt(USER_BUTTON_PIN, userKey, FALLING);
|
||||
#endif
|
||||
|
||||
RNS::loglevel(RNS::LOG_EXTREME);
|
||||
//RNS::loglevel(RNS::LOG_MEM);
|
||||
|
||||
/*
|
||||
{
|
||||
@@ -331,8 +423,28 @@ void setup() {
|
||||
void loop() {
|
||||
|
||||
#if defined(RUN_RETICULUM)
|
||||
|
||||
reticulum.loop();
|
||||
udpinterface.loop();
|
||||
#ifdef UDP_INTERFACE
|
||||
udp_interface.loop();
|
||||
#endif
|
||||
#ifdef LORA_INTERFACE
|
||||
lora_interface.loop();
|
||||
#endif
|
||||
|
||||
#ifdef ARDUINO
|
||||
/*
|
||||
if ((RNS::Utilities::OS::time() - last_announce) > 10) {
|
||||
reticulum_announce();
|
||||
last_announce = RNS::Utilities::OS::time();
|
||||
}
|
||||
*/
|
||||
#endif
|
||||
if (send_announce) {
|
||||
reticulum_announce();
|
||||
send_announce = false;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
}
|
||||
@@ -342,9 +454,10 @@ int main(void) {
|
||||
|
||||
setup();
|
||||
|
||||
//while (true) {
|
||||
// loop();
|
||||
//}
|
||||
while (true) {
|
||||
loop();
|
||||
RNS::Utilities::OS::sleep(0.01);
|
||||
}
|
||||
|
||||
printf("Goodbye from Native on PlatformIO!\n");
|
||||
}
|
||||
Reference in new issue
Block a user