WIP: Minor refactoring of examples.

This commit is contained in:
Chad Attermann committed 2025-08-24 20:52:38 -06:00
1 parent 0c902054db
commit f155e39cf6
28 files changed
+326 -334

No files matched your search

+116
View File
@@ -0,0 +1,116 @@
cmake_minimum_required(VERSION 3.14)
project(Reticulum LANGUAGES CXX)
# Set C++ standard (can be overridden via command line)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
# Option to fetch external libraries
option(FETCH_EXTERNAL_LIBS "Download and include external dependencies" ON)
include(FetchContent)
# Fetch external libraries
if(FETCH_EXTERNAL_LIBS)
message(STATUS "Fetching external dependencies...")
# ArduinoJson
FetchContent_Declare(
arduinojson
GIT_REPOSITORY https://github.com/bblanchon/ArduinoJson.git
GIT_TAG v7.4.2
)
FetchContent_MakeAvailable(arduinojson)
# MsgPack (non-CMake library)
FetchContent_Declare(
msgpack
GIT_REPOSITORY https://github.com/hideakitai/MsgPack.git
GIT_TAG v0.4.2
)
FetchContent_MakeAvailable(msgpack)
# Crypto (non-CMake library)
FetchContent_Declare(
crypto
GIT_REPOSITORY https://github.com/attermann/Crypto.git
GIT_TAG master
)
FetchContent_MakeAvailable(crypto)
endif()
# Include MsgPack (non-CMake library) library header files
set(MSGPACK_INCLUDE_DIR ${msgpack_SOURCE_DIR})
# Include Crypto (non-CMake library) library header files
set(CRYPTO_INCLUDE_DIR ${crypto_SOURCE_DIR})
# Include Crypto (non-CMake library) library source files
file(GLOB CRYPTO_SOURCES ${crypto_SOURCE_DIR}/*.cpp)
add_library(CryptoLib STATIC ${CRYPTO_SOURCES})
target_include_directories(CryptoLib PUBLIC ${CRYPTO_INCLUDE_DIR})
# Include directories
include_directories(src include)
# Gather source files
file(GLOB_RECURSE SOURCES CONFIGURE_DEPENDS src/*.cpp)
# Create shared library
add_library(ReticulumShared SHARED ${SOURCES})
set_target_properties(ReticulumShared PROPERTIES OUTPUT_NAME "Reticulum")
# Create static library
add_library(ReticulumStatic STATIC ${SOURCES})
set_target_properties(ReticulumStatic PROPERTIES OUTPUT_NAME "Reticulum")
# Common compiler flags and defines
foreach(target ReticulumShared ReticulumStatic)
target_compile_options(${target} PRIVATE
-Wall
-Wno-missing-field-initializers
-Wno-format
-Wno-unused-parameter
)
target_compile_definitions(${target} PRIVATE
LIBRARY_TEST
RNS_USE_FS
RNS_PERSIST_PATHS
NATIVE
)
target_include_directories(${target} PUBLIC
${PROJECT_SOURCE_DIR}/src
${PROJECT_SOURCE_DIR}/include
)
# Include MsgPack (non-CMake library) headers
target_include_directories(${target} PUBLIC
${MSGPACK_INCLUDE_DIR}
)
# Include Crypto (non-CMake library) headers
target_include_directories(${target} PUBLIC
${CRYPTO_INCLUDE_DIR}
)
# Link external libraries if available
if(FETCH_EXTERNAL_LIBS)
target_link_libraries(${target} PRIVATE
ArduinoJson
msgpackc-cxx
crypto
CryptoLib
MsgPack
)
endif()
endforeach()
# Optional test executable
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/main.cpp")
add_executable(ReticulumTest main.cpp)
target_link_libraries(ReticulumTest PRIVATE ReticulumStatic)
endif()
+2
View File
@@ -40,6 +40,8 @@ Instructions for command line builds and building of individual example applicat
- [x] Successful testing of Transport of both Packets and Links
- [x] Implement memory management appropriate for memory constricted MCUs
- [x] Implement Link
- [x] Implement AES256
- [ ] Implement Ratchets
- [ ] Implement Resource
- [ ] Implement Channel and Buffer
- [x] Add example applications utilizing the C++ API
@@ -0,0 +1,10 @@
name=lora_interface
version=0.0.1
author=Chad Attermann <attermann@gmail.com>
maintainer=Chad Attermann <attermann@gmail.com>
sentence=LoRaInterface implementation common to all examples
paragraph=LoRaInterface implementation common to all examples
category=Communication
url=
architectures=*
depends=
File renamed without changes.
@@ -1,9 +1,9 @@
name=common
name=udp_interface
version=0.0.1
author=Chad Attermann <attermann@gmail.com>
maintainer=Chad Attermann <attermann@gmail.com>
sentence=Boilerplate and implementations common to all examples
paragraph=Boilerplate and implementations common to all examples
sentence=UDPInterface implementation common to all examples
paragraph=UDPInterface implementation common to all examples
category=Communication
url=
architectures=*
@@ -246,7 +246,7 @@ void UniversalFileSystem::listDir(const char* dir) {
//File file = SPIFFS.open(file_path, mode);
//if (!file) {
File* file = new File(SPIFFS.open(file_path, mode));
if (file == null || !(*file)) {
if (file == nullptr || !(*file)) {
ERRORF("open_file: failed to open output file %s", file_path);
return {RNS::Type::NONE};
}
@@ -0,0 +1,10 @@
name=universal_filesystem
version=0.0.1
author=Chad Attermann <attermann@gmail.com>
maintainer=Chad Attermann <attermann@gmail.com>
sentence=UniversalFilesystem implementation common to all examples
paragraph=UniversalFilesystem implementation common to all examples
category=Communication
url=
architectures=*
depends=
+77 -162
View File
@@ -42,6 +42,10 @@
// them all within the app namespace "example_utilities"
#define APP_NAME "example_utilities"
RNS::Reticulum reticulum;
RNS::FileSystem universal_filesystem(RNS::Type::NONE);
RNS::Interface udp_interface(RNS::Type::NONE);
/*
##########################################################
@@ -96,10 +100,16 @@ void server_loop(RNS::Destination& destination) {
// destination on the network, which will let clients
// know how to create messages directed towards it.
while (true) {
int c;
while ((c = getchar()) != '\n' && c != EOF);
destination.announce();
RNS::log("Sent announce from "+destination.hash().toHex());
reticulum.loop();
udp_interface.loop();
// Non-blocking input
char ch;
while (read(STDIN_FILENO, &ch, 1) > 0) {
if (ch == '\n') {
destination.announce();
RNS::log("Sent announce from "+destination.hash().toHex());
}
}
}
}
@@ -107,21 +117,6 @@ void server_loop(RNS::Destination& destination) {
// to run as a server
void server() {
// Initialize and register filesystem
RNS::FileSystem universal_filesystem = new UniversalFileSystem();
universal_filesystem.init();
RNS::Utilities::OS::register_filesystem(universal_filesystem);
// Initialize and register interface
RNS::Interface udp_interface = new UDPInterface();
udp_interface.mode(RNS::Type::Interface::MODE_GATEWAY);
RNS::Transport::register_interface(udp_interface);
udp_interface.start();
// Initialize and start Reticulum
RNS::Reticulum reticulum = RNS::Reticulum();
reticulum.start();
// Randomly create a new identity for our link example
RNS::Identity server_identity = RNS::Identity();
@@ -142,33 +137,7 @@ void server() {
// Everything's ready!
// Let's Wait for client requests or user input
//server_loop(server_destination);
// Let the user know that everything is ready
RNS::log(
"Link example "+
server_destination.hash().toHex()+
" running, waiting for a connection."
);
RNS::log("Hit enter to manually send an announce (Ctrl-C to quit)");
// We enter a loop that runs until the users exits.
// If the user hits enter, we will announce our server
// destination on the network, which will let clients
// know how to create messages directed towards it.
while (true) {
reticulum.loop();
udp_interface.loop();
// Non-blocking input
char ch;
while (read(STDIN_FILENO, &ch, 1) > 0) {
if (ch == '\n') {
server_destination.announce();
RNS::log("Sent announce from "+server_destination.hash().toHex());
}
}
}
server_loop(server_destination);
}
@@ -221,54 +190,62 @@ void client_packet_received(const RNS::Bytes& message, const RNS::Packet& packet
}
void client_loop() {
// Wait for the link to become active
// Wait for the link to become active
RNS::log("Waiting for link to become active...");
while (!server_link) {
reticulum.loop();
udp_interface.loop();
RNS::Utilities::OS::sleep(0.1);
}
bool should_quit = false;
std::string text;
printf("> ");
fflush(STDIN_FILENO);
bool should_quit = false;
while (!should_quit) {
try {
printf("> ");
char *line = NULL;
size_t size;
if (getline(&line, &size, stdin) == -1) {
//printf("No line\n");
continue;
}
std::string text(line);
reticulum.loop();
udp_interface.loop();
// Check if we should quit the example
if (text == "quit" || text == "q" || text == "exit") {
should_quit = true;
server_link.teardown();
}
// Non-blocking input
char ch;
while (read(STDIN_FILENO, &ch, 1) > 0) {
if (ch == '\n') {
// If not, send the entered text over the link
if (text != "") {
RNS::Bytes data(text);
if (data.size() <= RNS::Type::Link::MDU) {
// CBA TODO: Add Packet constructor that accepts Link but doesn't require Destination
//RNS::Packet(RNS::Type::NONE, server_link, data).send();
RNS::Packet(server_link, data).send();
// Check if we should quit the example
if (text == "quit" || text == "q" || text == "exit") {
should_quit = true;
server_link.teardown();
}
else {
RNS::log(
"Cannot send this packet, the data size of "+
std::to_string(data.size())+" bytes exceeds the link packet MDU of "+
std::to_string(RNS::Type::Link::MDU)+" bytes",
RNS::LOG_ERROR
);
// If not, send the entered text over the link
if (text != "") {
RNS::Bytes data(text);
if (data.size() <= RNS::Type::Link::MDU) {
printf("(sending data: %s)\n", text.c_str());
// CBA TODO: Add Packet constructor that accepts Link but doesn't require Destination
//RNS::Packet(RNS::Type::NONE, server_link, data).send();
RNS::Packet(server_link, data).send();
}
else {
RNS::log(
"Cannot send this packet, the data size of "+
std::to_string(data.size())+" bytes exceeds the link packet MDU of "+
std::to_string(RNS::Type::Link::MDU)+" bytes",
RNS::LOG_ERROR
);
}
}
text.clear();
printf("> ");
fflush(STDIN_FILENO);
} else {
text += ch;
}
}
catch (std::exception& e) {
//RNS::log("Error while sending data over the link: "+e.what());
RNS::log("Error while sending data over the link: ");
should_quit = true;
server_link.teardown();
}
RNS::Utilities::OS::sleep(0.1);
}
}
@@ -291,21 +268,6 @@ void client(const char* destination_hexhash) {
return;
}
// Initialize and register filesystem
RNS::FileSystem universal_filesystem = new UniversalFileSystem();
universal_filesystem.init();
RNS::Utilities::OS::register_filesystem(universal_filesystem);
// Initialize and register interface
RNS::Interface udp_interface = new UDPInterface();
udp_interface.mode(RNS::Type::Interface::MODE_GATEWAY);
RNS::Transport::register_interface(udp_interface);
udp_interface.start();
// Initialize and start Reticulum
RNS::Reticulum reticulum = RNS::Reticulum();
reticulum.start();
// Check if we know a path to the destination
if (!RNS::Transport::has_path(destination_hash)) {
RNS::log("Destination is not yet known. Requesting path and waiting for announce to arrive...");
@@ -348,68 +310,7 @@ void client(const char* destination_hexhash) {
// Everything is set up, so let's enter a loop
// for the user to interact with the example
//client_loop();
// Wait for the link to become active
RNS::log("Waiting for link to become active...");
while (!server_link) {
reticulum.loop();
udp_interface.loop();
RNS::Utilities::OS::sleep(0.1);
}
RNS::log("Link is active");
std::string text;
printf("> ");
fflush(STDIN_FILENO);
bool should_quit = false;
while (!should_quit) {
//printf(".");
reticulum.loop();
udp_interface.loop();
// Non-blocking input
char ch;
while (read(STDIN_FILENO, &ch, 1) > 0) {
if (ch == '\n') {
//printf("Text: %s", text.c_str());
// Check if we should quit the example
if (text == "quit" || text == "q" || text == "exit") {
should_quit = true;
server_link.teardown();
}
// If not, send the entered text over the link
if (text != "") {
RNS::Bytes data(text);
if (data.size() <= RNS::Type::Link::MDU) {
printf("(sending data: %s)\n", text.c_str());
// CBA TODO: Add Packet constructor that accepts Link but doesn't require Destination
//RNS::Packet(RNS::Type::NONE, server_link, data).send();
RNS::Packet(server_link, data).send();
}
else {
RNS::log(
"Cannot send this packet, the data size of "+
std::to_string(data.size())+" bytes exceeds the link packet MDU of "+
std::to_string(RNS::Type::Link::MDU)+" bytes",
RNS::LOG_ERROR
);
}
}
text.clear();
printf("> ");
fflush(STDIN_FILENO);
} else {
text += ch;
}
}
RNS::Utilities::OS::sleep(0.1);
}
client_loop();
}
@@ -437,8 +338,8 @@ int main(int argc, char *argv[]) {
#if defined(MEM_LOG)
RNS::loglevel(RNS::LOG_MEM);
#else
//RNS::loglevel(RNS::LOG_NOTICE);
RNS::loglevel(RNS::LOG_TRACE);
RNS::loglevel(RNS::LOG_NOTICE);
//RNS::loglevel(RNS::LOG_TRACE);
#endif
// Register the signal handler for SIGINT
@@ -448,6 +349,20 @@ int main(int argc, char *argv[]) {
int flags = fcntl(STDIN_FILENO, F_GETFL, 0);
fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK);
// Initialize and register filesystem
universal_filesystem = new UniversalFileSystem();
universal_filesystem.init();
RNS::Utilities::OS::register_filesystem(universal_filesystem);
// Initialize and register interface
udp_interface = new UDPInterface();
udp_interface.mode(RNS::Type::Interface::MODE_GATEWAY);
RNS::Transport::register_interface(udp_interface);
udp_interface.start();
// Initialize and start Reticulum
reticulum.start();
if (argc <= 1) {
printf("\nMust specify a destination for client mode, or \"-s\" or \"--server\" for server mode.\n\n");
return -1;
+3 -4
View File
@@ -9,9 +9,7 @@
; https://docs.platformio.org/page/projectconf.html
[platformio]
;default_envs = native
;include_dir = ../../src
;src_dir = ../../src
default_envs = native
include_dir = .
src_dir = .
@@ -28,7 +26,8 @@ lib_deps =
MsgPack@^0.4.2
https://github.com/attermann/Crypto.git
microReticulum=symlink://../..
common=symlink://../common
universal_filesystem=symlink://../common/universal_filesystem
udp_interface=symlink://../common/udp_interface
[env:native]
platform = native
+3 -62
View File
@@ -9,9 +9,7 @@
; https://docs.platformio.org/page/projectconf.html
[platformio]
;default_envs = native
;include_dir = ../../src
;src_dir = ../../src
default_envs = native
include_dir = .
src_dir = .
@@ -31,7 +29,8 @@ lib_deps =
https://github.com/attermann/Crypto.git
https://github.com/hideakitai/MsgPack.git
microReticulum=symlink://../..
impl=symlink://../impl
universal_filesystem=symlink://../common/universal_filesystem
lora_interface=symlink://../common/lora_interface
[env:native]
platform = native
@@ -76,44 +75,6 @@ lib_deps =
lib_extra_dirs = ../
lib_compat_mode = off
[env:cubecell-gps]
framework = arduino
platform = heltec-cubecell
board = cubecell_gps
;build_flags = -Wl,-Map,output.map
build_flags =
${env.build_flags}
-std=c++11
-Wall
-Wextra
-Wno-missing-field-initializers
-Wno-format
-I.
-DCUBECELL
lib_deps =
${env.lib_deps}
; CBA NOTE must undef min and max from Arduino.h
[env:heltec-tracker]
framework = arduino
platform = espressif32
board = heltec_wireless_tracker
upload_protocol = esp-builtin
;board_build.partitions = no_ota.csv
;board_build.partitions = huge_app.csv
build_flags =
${env.build_flags}
-Wall
-Wextra
-Wno-missing-field-initializers
-Wno-format
-I variants/heltec_wireless_tracker
-I.
-DBOARD_ESP32
lib_deps =
${env.lib_deps}
lovyan03/LovyanGFX@^1.1.8
[env:ttgo-lora32-v21]
framework = arduino
platform = espressif32
@@ -169,23 +130,3 @@ build_flags =
build_unflags = -fno-exceptions
lib_deps =
${env.lib_deps}
[env:nrf52840_dk_adafruit]
framework = arduino
platform = nordicnrf52
board = nrf52840_dk_adafruit
monitor_speed = 115200
build_flags =
${env.build_flags}
lib_deps =
${env.lib_deps}
[env:nrf52_dk]
framework = arduino
;framework = zephyr
platform = nordicnrf52
board = nrf52_dk
build_flags =
${env.build_flags}
lib_deps =
${env.lib_deps}
+33 -72
View File
@@ -9,9 +9,7 @@
; https://docs.platformio.org/page/projectconf.html
[platformio]
;default_envs = native
;include_dir = ../../src
;src_dir = ../../src
default_envs = native
include_dir = .
src_dir = .
@@ -28,7 +26,8 @@ lib_deps =
https://github.com/attermann/Crypto.git
https://github.com/hideakitai/MsgPack.git
microReticulum=symlink://../..
common=symlink://../common
universal_filesystem=symlink://../common/universal_filesystem
udp_interface=symlink://../common/udp_interface
[env:native]
platform = native
@@ -73,74 +72,6 @@ lib_deps =
lib_extra_dirs = ../
lib_compat_mode = off
[env:luckfox]
;platform = linux_arm
;board = raspberrypi_2b
;framework = arduino
;platform = https://github.com/geeksville/platform-native.git
platform = https://github.com/meshtastic/platform-native.git
;platform = https://github.com/meshtastic/platform-native/archive/6cb7a455b440dd0738e8ed74a18136ed5cf7ea63.zip
;board = cross_platform
board = linux_hardware
monitor_port = none
build_flags =
${env.build_flags}
-std=gnu17
-std=c++17
-g3
-ggdb
-Wall
-Wextra
-Wno-missing-field-initializers
-Wno-format
-Wno-unused-parameter
-O0
;-lgpiod
-I.
-DNATIVE
lib_deps =
${env.lib_deps}
lib_extra_dirs = ../
lib_compat_mode = off
[env:cubecell-gps]
framework = arduino
platform = heltec-cubecell
board = cubecell_gps
;build_flags = -Wl,-Map,output.map
build_flags =
${env.build_flags}
-std=c++11
-Wall
-Wextra
-Wno-missing-field-initializers
-Wno-format
-I.
-DCUBECELL
lib_deps =
${env.lib_deps}
; CBA NOTE must undef min and max from Arduino.h
[env:heltec-tracker]
framework = arduino
platform = espressif32
board = heltec_wireless_tracker
upload_protocol = esp-builtin
;board_build.partitions = no_ota.csv
;board_build.partitions = huge_app.csv
build_flags =
${env.build_flags}
-Wall
-Wextra
-Wno-missing-field-initializers
-Wno-format
-I variants/heltec_wireless_tracker
-I.
-DBOARD_ESP32
lib_deps =
${env.lib_deps}
lovyan03/LovyanGFX@^1.1.8
[env:ttgo-lora32-v21]
framework = arduino
platform = espressif32
@@ -216,3 +147,33 @@ build_flags =
${env.build_flags}
lib_deps =
${env.lib_deps}
[env:luckfox]
;platform = linux_arm
;board = raspberrypi_2b
;framework = arduino
;platform = https://github.com/geeksville/platform-native.git
platform = https://github.com/meshtastic/platform-native.git
;platform = https://github.com/meshtastic/platform-native/archive/6cb7a455b440dd0738e8ed74a18136ed5cf7ea63.zip
;board = cross_platform
board = linux_hardware
monitor_port = none
build_flags =
${env.build_flags}
-std=gnu17
-std=c++17
-g3
-ggdb
-Wall
-Wextra
-Wno-missing-field-initializers
-Wno-format
-Wno-unused-parameter
-O0
;-lgpiod
-I.
-DNATIVE
lib_deps =
${env.lib_deps}
lib_extra_dirs = ../
lib_compat_mode = off
+1 -1
View File
@@ -246,7 +246,7 @@ void FileSystem::listDir(const char* dir) {
//File file = SPIFFS.open(file_path, mode);
//if (!file) {
File* file = new File(SPIFFS.open(file_path, mode));
if (file == null || !(*file)) {
if (file == nullptr || !(*file)) {
ERRORF("open_file: failed to open output file %s", file_path);
return {RNS::Type::NONE};
}
+4 -13
View File
@@ -9,7 +9,7 @@
; https://docs.platformio.org/page/projectconf.html
[platformio]
;default_envs = native
default_envs = native
[env]
build_flags =
@@ -60,18 +60,6 @@ lib_deps =
${env.lib_deps}
lib_compat_mode = off
[env:cubecell-gps]
platform = heltec-cubecell
board = cubecell_gps
framework = arduino
monitor_speed = 115200
;build_flags = -Wl,-Map,output.map
build_flags =
${env.build_flags}
lib_deps =
${env.lib_deps}
; CBA NOTE must undef min and max from Arduino.h
[env:ttgo-lora32-v21]
platform = espressif32
board = ttgo-lora32-v21
@@ -80,6 +68,7 @@ framework = arduino
monitor_speed = 115200
build_flags =
${env.build_flags}
-DMSGPACK_USE_BOOST=OFF
lib_deps =
${env.lib_deps}
@@ -92,6 +81,7 @@ framework = arduino
monitor_speed = 115200
build_flags =
${env.build_flags}
-DMSGPACK_USE_BOOST=OFF
lib_deps =
${env.lib_deps}
@@ -106,5 +96,6 @@ build_flags =
-fexceptions
-DRNS_USE_ALLOCATOR=1
-DRNS_USE_TLSF=1
-DMSGPACK_USE_BOOST=OFF
lib_deps =
${env.lib_deps}
+1 -1
View File
@@ -55,7 +55,7 @@ namespace RNS {
public:
FileStream(Type::NoneConstructor none) {
MEMF("FileStream object NONE created, this: 0x%X, impl: 0x%X", this, _impl.get());
MEMF("FileStream NONE object created, this: 0x%X, impl: 0x%X", this, _impl.get());
}
FileStream(const FileStream& obj) : _impl(obj._impl), _crc(obj._crc) {
MEMF("FileStream object copy created, this: 0x%X, impl: 0x%X", this, _impl.get());
+1 -1
View File
@@ -41,7 +41,7 @@ namespace RNS {
public:
FileSystem(Type::NoneConstructor none) {
MEMF("FileSystem object NONE created, this: 0x%X, impl: 0x%X", this, _impl.get());
MEMF("FileSystem NONE object created, this: 0x%X, impl: 0x%X", this, _impl.get());
}
FileSystem(const FileSystem& obj) : _impl(obj._impl) {
MEMF("FileSystem object copy created, this: 0x%X, impl: 0x%X", this, _impl.get());
+1 -1
View File
@@ -44,13 +44,13 @@ namespace RNS {
static uint16_t _known_destinations_maxsize;
public:
Identity(bool create_keys = true);
Identity(Type::NoneConstructor none) {
MEM("Identity NONE object created, this: " + std::to_string((uintptr_t)this) + ", data: " + std::to_string((uintptr_t)_object.get()));
}
Identity(const Identity& identity) : _object(identity._object) {
MEM("Identity object copy created, this: " + std::to_string((uintptr_t)this) + ", data: " + std::to_string((uintptr_t)_object.get()));
}
Identity(bool create_keys = true);
virtual ~Identity() {
MEM("Identity object destroyed, this: " + std::to_string((uintptr_t)this) + ", data: " + std::to_string((uintptr_t)_object.get()));
}
+1 -1
View File
@@ -98,7 +98,7 @@ namespace RNS {
public:
Interface(Type::NoneConstructor none) {
MEMF("Interface object NONE created, this: 0x%X, impl: 0x%X", this, _impl.get());
MEMF("Interface NONE object created, this: 0x%X, impl: 0x%X", this, _impl.get());
}
Interface(const Interface& obj) : _impl(obj._impl) {
MEMF("Interface object copy created, this: 0x%X, impl: 0x%X", this, _impl.get());
+5 -3
View File
@@ -11,6 +11,7 @@
#include "Cryptography/HKDF.h"
#include "Cryptography/Token.h"
#include "Cryptography/Random.h"
#include "Utilities/OS.h"
#define MSGPACK_DEBUGLOG_ENABLE 0
#include <MsgPack.h>
@@ -119,7 +120,7 @@ Link::Link(const Destination& destination /*= {Type::NONE}*/, Callbacks::establi
if (Link::ENABLED_MODES.find(mode) == Link::ENABLED_MODES.end()) throw std::runtime_error("Requested link mode "+std::to_string(mode)+" not enabled");
//p signalling_value = (mtu & Link.MTU_BYTEMASK)+(((mode<<5) & Link.MODE_BYTEMASK)<<16)
//p return struct.pack(">I", signalling_value)[1:]
uint32_t signalling_value = htonl((mtu & MTU_BYTEMASK)+(((mode<<5) & MODE_BYTEMASK)<<16));
uint32_t signalling_value = OS::portable_htonl((mtu & MTU_BYTEMASK)+(((mode<<5) & MODE_BYTEMASK)<<16));
Bytes data(((uint8_t*)&signalling_value)+1, 3);
return data;
}
@@ -1576,8 +1577,9 @@ RequestReceipt::RequestReceipt(const Link& link, const PacketReceipt& packet_rec
}
else if (_object->_resource) {
_object->_hash = resource.request_id();
//const_cast<Resource&>(resource).set_concluded_callback(request_resource_concluded);
const_cast<Resource&>(resource).set_concluded_callback(std::bind(&RequestReceipt::request_resource_concluded, this, std::placeholders::_1));
// CBA TODO Need to find cross-platform way of passing class method as callback
//z const_cast<Resource&>(resource).set_concluded_callback(request_resource_concluded);
//z const_cast<Resource&>(resource).set_concluded_callback(std::bind(&RequestReceipt::request_resource_concluded, this, std::placeholders::_1));
}
_object->_link = link;
_object->_request_id = _object->_hash;
+1 -1
View File
@@ -48,9 +48,9 @@ namespace RNS {
};
public:
PacketReceipt() : _object(new Object()) {}
PacketReceipt(Type::NoneConstructor none) {}
PacketReceipt(const PacketReceipt& packet_receipt) : _object(packet_receipt._object) {}
PacketReceipt() : _object(new Object()) {}
PacketReceipt(const Packet& packet);
inline PacketReceipt& operator = (const PacketReceipt& packet_receipt) {
+3 -2
View File
@@ -19,8 +19,9 @@ namespace RNS {
public:
class Callbacks {
public:
//using concluded = void(*)(const Resource& resource);
typedef std::function<void(const Resource& resource)> concluded;
// CBA std::function apparently not implemented in NRF52 framework
//typedef std::function<void(const Resource& resource)> concluded;
using concluded = void(*)(const Resource& resource);
using progress = void(*)(const Resource& resource);
public:
concluded _concluded = nullptr;
+4 -3
View File
@@ -69,9 +69,6 @@ void Reticulum::sigterm_handler(signal, frame):
Reticulum::Reticulum() : _object(new Object()) {
MEM("Reticulum default object creating..., this: " + std::to_string((uintptr_t)this) + ", data: " + std::to_string((uintptr_t)_object.get()));
INFO("Total memory: " + std::to_string(OS::heap_size()));
INFO("Total flash: " + std::to_string(OS::storage_size()));
// Initialize random number generator
TRACE("Initializing RNG...");
RNG.begin("Reticulum");
@@ -198,6 +195,10 @@ Reticulum::Reticulum() : _object(new Object()) {
}
void Reticulum::start() {
INFO("Total memory: " + std::to_string(OS::heap_size()));
INFO("Total flash: " + std::to_string(OS::storage_size()));
INFO("Starting Transport...");
Transport::start(*this);
}
+2 -2
View File
@@ -47,13 +47,13 @@ namespace RNS {
inline static const Reticulum& get_instance() { return _instance; }
public:
Reticulum();
Reticulum(Type::NoneConstructor none) {
MEM("Reticulum NONE object created, this: " + std::to_string((uintptr_t)this) + ", data: " + std::to_string((uintptr_t)_object.get()));
MEM("Reticulum empty object created, this: " + std::to_string((uintptr_t)this) + ", data: " + std::to_string((uintptr_t)_object.get()));
}
Reticulum(const Reticulum& reticulum) : _object(reticulum._object) {
MEM("Reticulum object copy created, this: " + std::to_string((uintptr_t)this) + ", data: " + std::to_string((uintptr_t)_object.get()));
}
Reticulum();
virtual ~Reticulum() {
MEM("Reticulum object destroyed, this: " + std::to_string((uintptr_t)this) + ", data: " + std::to_string((uintptr_t)_object.get()));
}
+36
View File
@@ -78,6 +78,42 @@ namespace RNS { namespace Utilities {
return result;
}
// Detect endianness at runtime
static int is_big_endian(void) {
uint16_t test = 0x0102;
return ((uint8_t*)&test)[0] == 0x01;
}
// Byte swap functions
static uint16_t swap16(uint16_t val) {
return (val << 8) | (val >> 8);
}
static uint32_t swap32(uint32_t val) {
return ((val << 24) & 0xFF000000) |
((val << 8) & 0x00FF0000) |
((val >> 8) & 0x0000FF00) |
((val >> 24) & 0x000000FF);
}
// Platform-independent replacements
static uint16_t portable_htons(uint16_t val) {
return is_big_endian() ? val : swap16(val);
}
static uint32_t portable_htonl(uint32_t val) {
return is_big_endian() ? val : swap32(val);
}
static uint16_t portable_ntohs(uint16_t val) {
return is_big_endian() ? val : swap16(val);
}
static uint32_t portable_ntohl(uint32_t val) {
return is_big_endian() ? val : swap32(val);
}
#if defined(RNS_USE_ALLOCATOR)
static void dump_allocator_stats();
#endif
+8 -1
View File
@@ -1,6 +1,13 @@
#include <stdio.h>
#ifdef NATIVE
#ifdef ARDUINO
void setup() {
}
void loop() {
}
#else
int main(void) {
printf("\nSilly rabbit, microReticulum is a library!\n\nSee the examples directory for example programs that make use of this library.\n\n");