Enhance Bytes, fix for Lins, test/example updates

- Added convenience methods to Bytes class
- Fix for logic error in Link class compliments of @cobraPA
- Added unit tests
- Updated example apps
This commit is contained in:
Chad Attermann committed 2026-01-21 12:38:13 -07:00
1 parent b8be484624
commit 818782382a
19 files changed
+275 -156

No files matched your search

File renamed without changes.
+91
View File
@@ -0,0 +1,91 @@
{
"cmake.configureOnOpen": false,
"files.associations": {
"system_error": "cpp",
"array": "cpp",
"atomic": "cpp",
"*.tcc": "cpp",
"cctype": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"deque": "cpp",
"unordered_map": "cpp",
"unordered_set": "cpp",
"vector": "cpp",
"exception": "cpp",
"algorithm": "cpp",
"functional": "cpp",
"iterator": "cpp",
"map": "cpp",
"memory": "cpp",
"memory_resource": "cpp",
"numeric": "cpp",
"optional": "cpp",
"random": "cpp",
"string": "cpp",
"string_view": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"utility": "cpp",
"fstream": "cpp",
"initializer_list": "cpp",
"iomanip": "cpp",
"iosfwd": "cpp",
"istream": "cpp",
"limits": "cpp",
"new": "cpp",
"ostream": "cpp",
"sstream": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"cinttypes": "cpp",
"typeinfo": "cpp",
"set": "cpp",
"__functional_base": "cpp",
"__bit_reference": "cpp",
"__config": "cpp",
"__debug": "cpp",
"__errc": "cpp",
"__hash_table": "cpp",
"__locale": "cpp",
"__mutex_base": "cpp",
"__node_handle": "cpp",
"__nullptr": "cpp",
"__split_buffer": "cpp",
"__string": "cpp",
"__threading_support": "cpp",
"__tree": "cpp",
"__tuple": "cpp",
"bit": "cpp",
"bitset": "cpp",
"chrono": "cpp",
"complex": "cpp",
"ios": "cpp",
"iostream": "cpp",
"list": "cpp",
"locale": "cpp",
"mutex": "cpp",
"ratio": "cpp",
"*.ipp": "cpp",
"cfenv": "cpp",
"strstream": "cpp",
"typeindex": "cpp",
"variant": "cpp",
"__verbose_abort": "cpp",
"charconv": "cpp",
"shared_mutex": "cpp",
"source_location": "cpp",
"execution": "cpp",
"forward_list": "cpp",
"queue": "cpp"
}
}
@@ -40,7 +40,8 @@ bool LoRaInterface::start() {
INFO("LoRa initializing...");
#ifdef ARDUINO
SPI.begin(RADIO_SCLK_PIN, RADIO_MISO_PIN, RADIO_MOSI_PIN);
//SPI.begin(RADIO_SCLK_PIN, RADIO_MISO_PIN, RADIO_MOSI_PIN);
SPI.begin();
delay(1500);
LoRa.setPins(RADIO_CS_PIN, RADIO_RST_PIN, RADIO_DIO0_PIN);
@@ -58,7 +59,7 @@ bool LoRaInterface::start() {
LoRa.setTxPower(power);
INFO("LoRa init succeeded.");
TRACE("LoRa bandwidth is " + std::to_string(Utilities::OS::round(bitrate()/1000.0, 2)) + " Kbps");
TRACE("LoRa bandwidth is " + std::to_string(Utilities::OS::round(_bitrate/1000.0, 2)) + " Kbps");
#endif
_online = true;
File renamed without changes.
+10
View File
@@ -0,0 +1,10 @@
{
// See http://go.microsoft.com/fwlink/?LinkId=827846
// for the documentation about the extensions.json format
"recommendations": [
"platformio.platformio-ide"
],
"unwantedRecommendations": [
"ms-vscode.cpptools-extension-pack"
]
}
File renamed without changes.
@@ -22,7 +22,6 @@
#ifdef ARDUINO
#include <Arduino.h>
#include <SPIFFS.h>
#else
#include <termios.h>
#include <fcntl.h>
@@ -72,58 +72,22 @@ lib_deps =
lib_extra_dirs = ../
lib_compat_mode = off
[env:ttgo-lora32-v21]
framework = arduino
platform = espressif32
board = ttgo-lora32-v21
upload_speed = 460800
board_build.partitions = no_ota.csv
build_flags =
[env:native20]
platform = native
build_unflags = -std=gnu++11
build_flags =
${env.build_flags}
-std=c++20
-g3
-ggdb
-Wall
-Wextra
-Wno-missing-field-initializers
-Wno-format
-Wno-unused-parameter
-I.
-DBOARD_ESP32
lib_deps =
${env.lib_deps}
monitor_filters = esp32_exception_decoder
[env:ttgo-t-beam]
framework = arduino
platform = espressif32
board = ttgo-t-beam
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.
-DBOARD_ESP32
lib_deps =
${env.lib_deps}
monitor_filters = esp32_exception_decoder
[env:wiscore_rak4631]
framework = arduino
platform = nordicnrf52
board = wiscore_rak4631
board_build.partitions = no_ota.csv
board_build.FileSystem = littlefs
build_flags =
${env.build_flags}
-fexceptions
-Wall
;-Wextra
-Wno-missing-field-initializers
-Wno-format
-I.
-DBOARD_NRF52
-DRNS_USE_TLSF=1
build_unflags = -fno-exceptions
lib_deps =
-DNATIVE
lib_deps =
${env.lib_deps}
lib_extra_dirs = ../
lib_compat_mode = off
+3 -1
View File
@@ -16,7 +16,7 @@
#ifdef ARDUINO
#include <Arduino.h>
#include <SPIFFS.h>
//#include <SPIFFS.h>
#else
#include <termios.h>
#include <fcntl.h>
@@ -265,6 +265,7 @@ void setup() {
pinMode(BUTTON_PIN, INPUT);
attachInterrupt(BUTTON_PIN, userKey, FALLING);
/*
// Setup filesystem
if (!SPIFFS.begin(true, "")){
ERROR("SPIFFS filesystem mount failed");
@@ -272,6 +273,7 @@ void setup() {
else {
DEBUG("SPIFFS filesystem is ready");
}
*/
#endif
RNS::loglevel(RNS::LOG_TRACE);
+35 -58
View File
@@ -9,7 +9,6 @@
; https://docs.platformio.org/page/projectconf.html
[platformio]
default_envs = native
include_dir = .
src_dir = .
@@ -18,63 +17,24 @@ monitor_speed = 115200
upload_speed = 921600
build_type = debug
build_flags =
-Wall
-Wno-missing-field-initializers
-Wno-format
-I.
;-DMEM_LOG=1
-DRNS_USE_ALLOCATOR=1
-DRNS_USE_ALLOCATOR=1
-DRNS_USE_TLSF=1
;-DTEST_OBJECT_DATA=1
-DTEST_OBJECT_IMPL=1
lib_deps =
ArduinoJson@^6.21.3
sandeepmistry/LoRa @ ^0.8.0
https://github.com/attermann/Crypto.git
https://github.com/hideakitai/MsgPack.git
microReticulum=symlink://../..
universal_filesystem=symlink://../common/universal_filesystem
lora_interface=symlink://../common/lora_interface
[env:native]
platform = native
monitor_port = none
build_flags =
${env.build_flags}
-std=c++11
-g3
-ggdb
-Wall
-Wextra
-Wno-missing-field-initializers
-Wno-format
-Wno-unused-parameter
-I.
-DNATIVE
lib_deps =
${env.lib_deps}
lib_extra_dirs = ../
lib_compat_mode = off
;debug_init_break =
;debug_init_break = tbreak
;debug_tool =
[env:native17]
platform = native
build_unflags = -std=gnu++11
build_flags =
${env.build_flags}
-std=c++17
-g3
-ggdb
-Wall
-Wextra
-Wno-missing-field-initializers
-Wno-format
-Wno-unused-parameter
-I.
-DNATIVE
lib_deps =
${env.lib_deps}
lib_extra_dirs = ../
lib_compat_mode = off
[env:ttgo-lora32-v21]
framework = arduino
platform = espressif32
@@ -83,11 +43,7 @@ upload_speed = 460800
board_build.partitions = no_ota.csv
build_flags =
${env.build_flags}
-Wall
-Wextra
-Wno-missing-field-initializers
-Wno-format
-I.
-DBOARD_ESP32
lib_deps =
${env.lib_deps}
@@ -101,11 +57,7 @@ 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.
-DBOARD_ESP32
lib_deps =
${env.lib_deps}
@@ -119,14 +71,39 @@ board_build.partitions = no_ota.csv
board_build.FileSystem = littlefs
build_flags =
${env.build_flags}
-fexceptions
-Wall
;-Wextra
-Wno-missing-field-initializers
-Wno-format
-I.
-fexceptions
-DBOARD_NRF52
-DRNS_USE_TLSF=1
build_unflags = -fno-exceptions
lib_deps =
${env.lib_deps}
adafruit/Adafruit TinyUSB Library
[env:nrf52840_dk_adafruit]
framework = arduino
platform = nordicnrf52
board = nrf52840_dk_adafruit
monitor_speed = 115200
build_flags =
${env.build_flags}
;-Wextra
-fexceptions
-DBOARD_NRF52
-DRNS_USE_TLSF=1
lib_deps =
${env.lib_deps}
[env:nrf52_dk]
framework = arduino
;framework = zephyr
platform = nordicnrf52
board = nrf52_dk
build_flags =
${env.build_flags}
;-Wextra
-fexceptions
-DBOARD_NRF52
-DRNS_USE_TLSF=1
lib_deps =
${env.lib_deps}
+3 -1
View File
@@ -16,7 +16,7 @@
#ifdef ARDUINO
#include <Arduino.h>
#include <SPIFFS.h>
//#include <SPIFFS.h>
#else
#include <termios.h>
#include <fcntl.h>
@@ -258,6 +258,7 @@ void setup() {
pinMode(BUTTON_PIN, INPUT);
attachInterrupt(BUTTON_PIN, userKey, FALLING);
/*
// Setup filesystem
if (!SPIFFS.begin(true, "")){
ERROR("SPIFFS filesystem mount failed");
@@ -265,6 +266,7 @@ void setup() {
else {
DEBUG("SPIFFS filesystem is ready");
}
*/
#endif
#if defined(MEM_LOG)
+20 -40
View File
@@ -72,6 +72,26 @@ lib_deps =
lib_extra_dirs = ../
lib_compat_mode = off
[env:native20]
platform = native
build_unflags = -std=gnu++11
build_flags =
${env.build_flags}
-std=c++20
-g3
-ggdb
-Wall
-Wextra
-Wno-missing-field-initializers
-Wno-format
-Wno-unused-parameter
-I.
-DNATIVE
lib_deps =
${env.lib_deps}
lib_extra_dirs = ../
lib_compat_mode = off
[env:ttgo-lora32-v21]
framework = arduino
platform = espressif32
@@ -108,46 +128,6 @@ lib_deps =
${env.lib_deps}
monitor_filters = esp32_exception_decoder
[env:wiscore_rak4631]
framework = arduino
platform = nordicnrf52
board = wiscore_rak4631
board_build.partitions = no_ota.csv
board_build.FileSystem = littlefs
build_flags =
${env.build_flags}
-fexceptions
-Wall
;-Wextra
-Wno-missing-field-initializers
-Wno-format
-I.
-DBOARD_NRF52
-DRNS_USE_TLSF=1
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}
[env:luckfox]
;platform = linux_arm
;board = raspberrypi_2b
+2 -1
View File
@@ -13,7 +13,6 @@ default_envs = native
[env]
build_flags =
-std=c++11
-Wall
;-Wextra
-Wno-missing-field-initializers
@@ -36,6 +35,7 @@ test_build_src = true
platform = native
build_flags =
${env.build_flags}
-std=c++11
-DNATIVE
lib_deps =
${env.lib_deps}
@@ -102,6 +102,7 @@ build_flags =
-DMSGPACK_USE_BOOST=OFF
lib_deps =
${env.lib_deps}
adafruit/Adafruit TinyUSB Library
; Overrides for specific tests (not working!)
[test_env:test_objects]
+25
View File
@@ -78,6 +78,10 @@ MEM("Creating from data-move...");
assign(chunk, size);
MEMF("Bytes object created from chunk \"%s\", this: %lu, data: %lu", toString().c_str(), this, _data.get());
}
Bytes(const void* chunk, size_t size) {
assign(chunk, size);
MEMF("Bytes object created from chunk \"%s\", this: %lu, data: %lu", toString().c_str(), this, _data.get());
}
Bytes(const char* string) {
assign(string);
MEMF("Bytes object created from string \"%s\", this: %lu, data: %lu", toString().c_str(), this, _data.get());
@@ -161,6 +165,19 @@ MEM("Creating from data-move...");
return Data();
return *_data.get();
}
// CBA NOTE: Following cast operators can cause issues with ambiguity from other libraries
/*
inline operator const uint8_t*() const {
if (!_data)
return nullptr;
return _data->data();
}
inline operator const void*() const {
if (!_data)
return nullptr;
return _data->data();
}
*/
private:
inline SharedData shareData() const {
@@ -226,6 +243,9 @@ MEM("Creating from data-move...");
//_data->insert(_data->begin(), chunk, chunk + chunk_size);
_data->assign(chunk, chunk + chunk_size);
}
inline void assign(const void* chunk, size_t chunk_size) {
assign((uint8_t*)chunk, chunk_size);
}
inline void assign(const char* string) {
// if assignment is empty then clear data and don't bother creating new
if (string == nullptr || string[0] == 0) {
@@ -267,6 +287,9 @@ MEM("Creating from data-move...");
exclusiveData(true, size() + chunk_size);
_data->insert(_data->end(), chunk, chunk + chunk_size);
}
inline void append(const void* chunk, size_t chunk_size) {
append((uint8_t*)chunk, chunk_size);
}
inline void append(const char* string) {
// if append is empty then do nothing
if (string == nullptr || string[0] == 0) {
@@ -410,9 +433,11 @@ inline RNS::Bytes& operator << (RNS::Bytes& lhbytes, const char* rhstr) {
}
namespace ArduinoJson {
// Serialize
inline bool convertToJson(const RNS::Bytes& src, JsonVariant dst) {
return dst.set(src.toHex());
}
// Deserialize
inline void convertFromJson(JsonVariantConst src, RNS::Bytes& dst) {
dst.assignHex(src.as<const char*>());
}
+1 -1
View File
@@ -1034,7 +1034,7 @@ void Link::receive(const Packet& packet) {
{
const Bytes plaintext = decrypt(packet.data());
if (plaintext) {
if (_object->_initiator && plaintext.size() == Type::Identity::KEYSIZE/8 + Type::Identity::SIGLENGTH/8) {
if (!(_object->_initiator) && plaintext.size() == Type::Identity::KEYSIZE/8 + Type::Identity::SIGLENGTH/8) {
const Bytes public_key = plaintext.left(Type::Identity::KEYSIZE/8);
const Bytes signed_data = _object->_link_id + public_key;
const Bytes signature = plaintext.mid(Type::Identity::KEYSIZE/8, Type::Identity::SIGLENGTH/8);
+3
View File
@@ -3,8 +3,11 @@
#include <stdio.h>
#ifdef ARDUINO
#include <Arduino.h>
void setup() {
Serial.begin(115200);
Serial.print("\nSilly rabbit, microReticulum is a library!\n\nSee the examples directory for example programs that make use of this library.\n\n");
}
void loop() {
}
+57
View File
@@ -3,6 +3,9 @@
#define MSGPACK_DEBUGLOG_ENABLE 0
#define DEBUGLOG_DEFAULT_LOG_LEVEL_TRACE
#include <MsgPack.h>
#include <ArduinoJson.h>
#include "Bytes.h"
#include <string.h>
@@ -176,6 +179,7 @@ void test_pack_bytes_array_python() {
const RNS::Bytes one("first\xab");
const RNS::Bytes two("second\xcd");
MsgPack::Packer packer;
// CBA NOTE: MsgPack appears to use Bytes cast operator to std::vector<uint8_t> for serializing Bytes as binary
packer.to_array(time, one, two);
RNS::Bytes data(packer.data(), packer.size());
TEST_ASSERT_EQUAL_size_t(27, packer.size());
@@ -232,6 +236,57 @@ void test_unpack_float_python() {
}
void test_json_pack_bytes_array_python() {
static const unsigned char binary[] = {
0x93, 0xcb, 0x40, 0x5e, 0xdd, 0x2f, 0x1a, 0x9f,
0xbe, 0x77, 0xc4, 0x06, 0x66, 0x69, 0x72, 0x73,
0x74, 0xab, 0xc4, 0x07, 0x73, 0x65, 0x63, 0x6f,
0x6e, 0x64, 0xcd,
};
double time = 123.456;
// Test that array containing bytes matches python equivalent
const RNS::Bytes one("first\xab");
const RNS::Bytes two("second\xcd");
// CBA NOTE: ArduinJson AJ MsgPack appears to also use Bytes cast operator to std::vector<uint8_t> for serializing Bytes
// but serializes it as an array instead of as binary, so can't currently serialize Bytes directly yet.
// CBA NOTE: Actually AJ MsgPack probably uses the same Json (de)serializers defined in Bytes as AJ Json uses, so need a way
// to distinguish MsgPack from JSON in (de)serializers to know whether to store as binary or as string represenation!!!
MsgPackBinary bin_one(one.data(), one.size());
MsgPackBinary bin_two(two.data(), two.size());
JsonDocument doc;
doc.add(time);
doc.add(bin_one);
doc.add(bin_two);
RNS::Bytes packed;
size_t len = serializeMsgPack(doc, packed.writable(256), 256);
packed.resize(len);
TEST_ASSERT_EQUAL_size_t(27, packed.size());
TEST_ASSERT_EQUAL_MEMORY(binary, packed.data(), packed.size());
}
void test_json_unpack_bytes_array_python() {
static const unsigned char binary[] = {
0x93, 0xcb, 0x40, 0x5e, 0xdd, 0x2f, 0x1a, 0x9f,
0xbe, 0x77, 0xc4, 0x06, 0x66, 0x69, 0x72, 0x73,
0x74, 0xab, 0xc4, 0x07, 0x73, 0x65, 0x63, 0x6f,
0x6e, 0x64, 0xcd,
};
JsonDocument doc;
deserializeMsgPack(doc, binary, sizeof(binary));
double time = doc[0];
// CBA NOTE: Can't currently deserialize directly into Bytes
//RNS::Bytes one;
//RNS::Bytes two;
MsgPackBinary bin_one = doc[1];
MsgPackBinary bin_two = doc[2];
RNS::Bytes one(bin_one.data(), bin_one.size());
RNS::Bytes two(bin_two.data(), bin_two.size());
TEST_ASSERT_EQUAL_FLOAT(123.456, time);
TEST_ASSERT_EQUAL_MEMORY("first\xab", one.data(), one.size());
TEST_ASSERT_EQUAL_MEMORY("second\xcd", two.data(), two.size());
}
void setUp(void) {
// set stuff up here before each test
}
@@ -249,6 +304,8 @@ int runUnityTests(void) {
RUN_TEST(test_unpack_bytes_array_python);
RUN_TEST(test_pack_float_python);
RUN_TEST(test_unpack_float_python);
RUN_TEST(test_json_pack_bytes_array_python);
RUN_TEST(test_json_unpack_bytes_array_python);
return UNITY_END();
}
+2 -1
View File
@@ -1176,8 +1176,9 @@ int runUnityTests(void) {
RNS::Utilities::OS::remove_file(test_map_path);
RNS::Utilities::OS::remove_file(test_vector_path);
RNS::Utilities::OS::remove_file(test_set_path);
RNS::Utilities::OS::remove_file(test_array_path);
RNS::Utilities::OS::remove_file(test_destination_table_path);
RNS::Utilities::OS::remove_file(test_empty_destination_table_path);
RNS::Utilities::OS::remove_file(test_array_path);
RNS::Utilities::OS::remove_file(test_series_path);
RNS::Utilities::OS::deregister_filesystem();
+8 -2
View File
@@ -6,7 +6,11 @@
#include <Transport.h>
#include <Log.h>
RNS::FileSystem reticulum_filesystem(RNS::Type::NONE);
#ifdef ARDUINO
const char destination_table_path[] = "/destination_table";
#else
const char destination_table_path[] = "destination_table";
#endif
class InInterface : public RNS::InterfaceImpl {
public:
@@ -100,7 +104,7 @@ void testReticulum() {
HEAD("Running testReticulum...", RNS::LOG_TRACE);
HEAD("Registering Filesystem with OS...", RNS::LOG_TRACE);
reticulum_filesystem = new FileSystem();
RNS::FileSystem reticulum_filesystem = new FileSystem();
((FileSystem*)reticulum_filesystem.get())->init();
RNS::Utilities::OS::register_filesystem(reticulum_filesystem);
@@ -168,6 +172,8 @@ void testReticulum() {
HEAD("Destroying Reticulum instance...", RNS::LOG_TRACE);
reticulum = {RNS::Type::NONE};
RNS::Utilities::OS::remove_file(destination_table_path);
}