diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..144437d --- /dev/null +++ b/CMakeLists.txt @@ -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() + diff --git a/README.md b/README.md index f89c3ce..ec371de 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/examples/common/LoRaInterface.cpp b/examples/common/lora_interface/LoRaInterface.cpp similarity index 100% rename from examples/common/LoRaInterface.cpp rename to examples/common/lora_interface/LoRaInterface.cpp diff --git a/examples/common/LoRaInterface.h b/examples/common/lora_interface/LoRaInterface.h similarity index 100% rename from examples/common/LoRaInterface.h rename to examples/common/lora_interface/LoRaInterface.h diff --git a/examples/common/lora_interface/library.properties b/examples/common/lora_interface/library.properties new file mode 100644 index 0000000..34ca327 --- /dev/null +++ b/examples/common/lora_interface/library.properties @@ -0,0 +1,10 @@ +name=lora_interface +version=0.0.1 +author=Chad Attermann +maintainer=Chad Attermann +sentence=LoRaInterface implementation common to all examples +paragraph=LoRaInterface implementation common to all examples +category=Communication +url= +architectures=* +depends= diff --git a/examples/common/UDPInterface.cpp b/examples/common/udp_interface/UDPInterface.cpp similarity index 100% rename from examples/common/UDPInterface.cpp rename to examples/common/udp_interface/UDPInterface.cpp diff --git a/examples/common/UDPInterface.h b/examples/common/udp_interface/UDPInterface.h similarity index 100% rename from examples/common/UDPInterface.h rename to examples/common/udp_interface/UDPInterface.h diff --git a/examples/common/library.properties b/examples/common/udp_interface/library.properties similarity index 53% rename from examples/common/library.properties rename to examples/common/udp_interface/library.properties index 79232c1..226d9d7 100644 --- a/examples/common/library.properties +++ b/examples/common/udp_interface/library.properties @@ -1,9 +1,9 @@ -name=common +name=udp_interface version=0.0.1 author=Chad Attermann maintainer=Chad Attermann -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=* diff --git a/examples/common/UniversalFileSystem.cpp b/examples/common/universal_filesystem/UniversalFileSystem.cpp similarity index 99% rename from examples/common/UniversalFileSystem.cpp rename to examples/common/universal_filesystem/UniversalFileSystem.cpp index 545d91d..50ec26c 100644 --- a/examples/common/UniversalFileSystem.cpp +++ b/examples/common/universal_filesystem/UniversalFileSystem.cpp @@ -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}; } diff --git a/examples/common/UniversalFileSystem.h b/examples/common/universal_filesystem/UniversalFileSystem.h similarity index 100% rename from examples/common/UniversalFileSystem.h rename to examples/common/universal_filesystem/UniversalFileSystem.h diff --git a/examples/common/universal_filesystem/library.properties b/examples/common/universal_filesystem/library.properties new file mode 100644 index 0000000..3dbee3d --- /dev/null +++ b/examples/common/universal_filesystem/library.properties @@ -0,0 +1,10 @@ +name=universal_filesystem +version=0.0.1 +author=Chad Attermann +maintainer=Chad Attermann +sentence=UniversalFilesystem implementation common to all examples +paragraph=UniversalFilesystem implementation common to all examples +category=Communication +url= +architectures=* +depends= diff --git a/examples/link/main.cpp b/examples/link/main.cpp index c2cc7a8..281e523 100644 --- a/examples/link/main.cpp +++ b/examples/link/main.cpp @@ -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; diff --git a/examples/link/platformio.ini b/examples/link/platformio.ini index 726b3c8..c956b6b 100644 --- a/examples/link/platformio.ini +++ b/examples/link/platformio.ini @@ -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 diff --git a/examples/lora_announce/platformio.ini b/examples/lora_announce/platformio.ini index bb7d854..11f1f7e 100644 --- a/examples/lora_announce/platformio.ini +++ b/examples/lora_announce/platformio.ini @@ -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} diff --git a/examples/udp_announce/platformio.ini b/examples/udp_announce/platformio.ini index 16e34e7..3d019e9 100644 --- a/examples/udp_announce/platformio.ini +++ b/examples/udp_announce/platformio.ini @@ -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 diff --git a/examples/unit_tests/FileSystem.cpp b/examples/unit_tests/FileSystem.cpp index ecc741d..33a1590 100644 --- a/examples/unit_tests/FileSystem.cpp +++ b/examples/unit_tests/FileSystem.cpp @@ -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}; } diff --git a/platformio.ini b/platformio.ini index 8b02deb..0506b1c 100644 --- a/platformio.ini +++ b/platformio.ini @@ -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} diff --git a/src/FileStream.h b/src/FileStream.h index 8b12c30..6b69d65 100644 --- a/src/FileStream.h +++ b/src/FileStream.h @@ -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()); diff --git a/src/FileSystem.h b/src/FileSystem.h index 6be4f77..41812c4 100644 --- a/src/FileSystem.h +++ b/src/FileSystem.h @@ -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()); diff --git a/src/Identity.h b/src/Identity.h index f5033df..0459aa1 100644 --- a/src/Identity.h +++ b/src/Identity.h @@ -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())); } diff --git a/src/Interface.h b/src/Interface.h index 6ce2828..480d018 100644 --- a/src/Interface.h +++ b/src/Interface.h @@ -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()); diff --git a/src/Link.cpp b/src/Link.cpp index 81d4ecf..903151e 100644 --- a/src/Link.cpp +++ b/src/Link.cpp @@ -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 @@ -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).set_concluded_callback(request_resource_concluded); - const_cast(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).set_concluded_callback(request_resource_concluded); + //z const_cast(resource).set_concluded_callback(std::bind(&RequestReceipt::request_resource_concluded, this, std::placeholders::_1)); } _object->_link = link; _object->_request_id = _object->_hash; diff --git a/src/Packet.h b/src/Packet.h index ab58b69..cd99280 100644 --- a/src/Packet.h +++ b/src/Packet.h @@ -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) { diff --git a/src/Resource.h b/src/Resource.h index e66597c..e1cd909 100644 --- a/src/Resource.h +++ b/src/Resource.h @@ -19,8 +19,9 @@ namespace RNS { public: class Callbacks { public: - //using concluded = void(*)(const Resource& resource); - typedef std::function concluded; + // CBA std::function apparently not implemented in NRF52 framework + //typedef std::function concluded; + using concluded = void(*)(const Resource& resource); using progress = void(*)(const Resource& resource); public: concluded _concluded = nullptr; diff --git a/src/Reticulum.cpp b/src/Reticulum.cpp index 5dca412..88c678b 100644 --- a/src/Reticulum.cpp +++ b/src/Reticulum.cpp @@ -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); } diff --git a/src/Reticulum.h b/src/Reticulum.h index 33beb1c..6cd12af 100644 --- a/src/Reticulum.h +++ b/src/Reticulum.h @@ -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())); } diff --git a/src/Utilities/OS.h b/src/Utilities/OS.h index 4255f21..aac40db 100644 --- a/src/Utilities/OS.h +++ b/src/Utilities/OS.h @@ -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 diff --git a/src/main.cpp b/src/main.cpp index eebde65..ed4a21a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,6 +1,13 @@ #include -#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");