WIP: Updated ecamples and added native main()

This commit is contained in:
Chad Attermann committed 2025-08-20 22:05:11 -06:00
1 parent b0011a10ed
commit 2deb358def
38 files changed
+1292 -99

No files matched your search

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
-76
View File
@@ -1,76 +0,0 @@
#pragma once
#include "../src/Interface.h"
#include "../src/Bytes.h"
#include "../src/Type.h"
#ifdef ARDUINO
#include <WiFi.h>
#include <WiFiUdp.h>
//#include <AsyncUDP.h>
#else
#include <netinet/in.h>
#endif
#include <stdint.h>
#define DEFAULT_UDP_PORT 4242
#define DEFAULT_UDP_LOCAL_HOST "0.0.0.0"
#define DEFAULT_UDP_REMOTE_HOST "255.255.255.255"
namespace RNS { namespace Interfaces {
class UDPInterface : public Interface {
public:
static const uint32_t BITRATE_GUESS = 10*1000*1000;
//z def get_address_for_if(name):
//z def get_broadcast_for_if(name):
public:
//p def __init__(self, owner, name, device=None, bindip=None, bindport=None, forwardip=None, forwardport=None):
UDPInterface(const char* name = "UDPInterface");
virtual ~UDPInterface();
bool start(const char* wifi_ssid, const char* wifi_password, int port = DEFAULT_UDP_PORT, const char* local_host = nullptr);
void stop();
void loop();
virtual inline std::string toString() const { return "UDPInterface[" + name() + "/" + _local_host + ":" + std::to_string(_local_port) + "]"; }
//virtual inline std::string toString() const { return "UDPInterface[" + name() + "]"; }
private:
virtual void on_incoming(const Bytes& data);
virtual void on_outgoing(const Bytes& data);
private:
const uint16_t HW_MTU = 1064;
//uint8_t buffer[Type::Reticulum::MTU] = {0};
Bytes _buffer;
// WiFi network name and password
std::string _wifi_ssid;
std::string _wifi_password;
// IP address to send UDP data to.
// it can be ip address of the server or
// broadcast
std::string _local_host = DEFAULT_UDP_LOCAL_HOST;
int _local_port = DEFAULT_UDP_PORT;
std::string _remote_host = DEFAULT_UDP_REMOTE_HOST;
int _remote_port = DEFAULT_UDP_PORT;
// create UDP instance
#ifdef ARDUINO
WiFiUDP udp;
//AsyncUDP udp;
#else
int _socket = -1;
in_addr_t _local_address = INADDR_ANY;
in_addr_t _remote_address = INADDR_NONE;
#endif
};
} }
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
@@ -14,7 +14,6 @@
#endif
using namespace RNS;
using namespace RNS::Interfaces;
/*
@staticmethod
@@ -31,11 +30,11 @@ def get_broadcast_for_if(name):
*/
//p def __init__(self, owner, name, device=None, bindip=None, bindport=None, forwardip=None, forwardport=None):
UDPInterface::UDPInterface(const char* name /*= "UDPInterface"*/) : Interface(name) {
UDPInterface::UDPInterface(const char* name /*= "UDPInterface"*/) : RNS::InterfaceImpl(name) {
IN(true);
OUT(true);
bitrate(BITRATE_GUESS);
_IN = true;
_OUT = true;
_bitrate = BITRATE_GUESS;
}
@@ -43,8 +42,14 @@ UDPInterface::UDPInterface(const char* name /*= "UDPInterface"*/) : Interface(na
stop();
}
bool UDPInterface::start(const char* wifi_ssid, const char* wifi_password, int port /*= DEFAULT_UDP_PORT*/, const char* local_host /*=nullptr*/) {
online(false);
//bool UDPInterface::start(const char* wifi_ssid, const char* wifi_password, int port /*= DEFAULT_UDP_PORT*/, const char* local_host /*= nullptr*/) {
/*virtual*/ bool UDPInterface::start() {
const char* wifi_ssid = "wifi_ssid";
const char* wifi_password = "wifi_password";
int port = DEFAULT_UDP_PORT;
const char* local_host = nullptr;
_online = false;
if (wifi_ssid != nullptr) {
_wifi_ssid = wifi_ssid;
@@ -57,14 +62,15 @@ bool UDPInterface::start(const char* wifi_ssid, const char* wifi_password, int p
}
_local_port = port;
_remote_port = port;
TRACE("UDPInterface: wifi ssid: " + _wifi_ssid);
TRACE("UDPInterface: wifi password: " + _wifi_password);
TRACE("UDPInterface: local host: " + _local_host);
TRACE("UDPInterface: local port: " + std::to_string(_local_port));
TRACE("UDPInterface: remote host: " + _remote_host);
TRACE("UDPInterface: remote port: " + std::to_string(_remote_port));
#ifdef ARDUINO
TRACE("UDPInterface: wifi ssid: " + _wifi_ssid);
TRACE("UDPInterface: wifi password: " + _wifi_password);
// Connect to the WiFi network
WiFi.begin(_wifi_ssid.c_str(), _wifi_password.c_str());
Serial.println("");
@@ -174,12 +180,12 @@ bool UDPInterface::start(const char* wifi_ssid, const char* wifi_password, int p
}
#endif
online(true);
_online = true;
return true;
}
void UDPInterface::stop() {
/*virtual*/ void UDPInterface::stop() {
#ifdef ARDUINO
#else
if (_socket > -1) {
@@ -188,12 +194,12 @@ void UDPInterface::stop() {
}
#endif
online(false);
_online = false;
}
void UDPInterface::loop() {
/*virtual*/ void UDPInterface::loop() {
if (online()) {
if (_online) {
// Check for incoming packet
#ifdef ARDUINO
udp.parsePacket();
@@ -220,13 +226,13 @@ void UDPInterface::loop() {
/*virtual*/ void UDPInterface::on_incoming(const Bytes& data) {
DEBUG(toString() + ".on_incoming: data: " + data.toHex());
Interface::on_incoming(data);
//Interface::on_incoming(data);
}
/*virtual*/ void UDPInterface::on_outgoing(const Bytes& data) {
/*virtual*/ void UDPInterface::send_outgoing(const Bytes& data) {
DEBUG(toString() + ".on_outgoing: data: " + data.toHex());
try {
if (online()) {
if (_online) {
// Send packet
#ifdef ARDUINO
udp.beginPacket(_remote_host.c_str(), _remote_port);
@@ -243,7 +249,9 @@ void UDPInterface::loop() {
#endif
}
Interface::on_outgoing(data);
//Interface::on_outgoing(data);
// CBA Call base method to handle internal housekeeping
InterfaceImpl::send_outgoing(data);
}
catch (std::exception& e) {
ERROR("Could not transmit on " + toString() + ". The contained exception was: " + e.what());
+74
View File
@@ -0,0 +1,74 @@
#pragma once
#include "../src/Interface.h"
#include "../src/Bytes.h"
#include "../src/Type.h"
#ifdef ARDUINO
#include <WiFi.h>
#include <WiFiUdp.h>
//#include <AsyncUDP.h>
#else
#include <netinet/in.h>
#endif
#include <stdint.h>
#define DEFAULT_UDP_PORT 4242
#define DEFAULT_UDP_LOCAL_HOST "0.0.0.0"
#define DEFAULT_UDP_REMOTE_HOST "255.255.255.255"
class UDPInterface : public RNS::InterfaceImpl {
public:
static const uint32_t BITRATE_GUESS = 10*1000*1000;
//z def get_address_for_if(name):
//z def get_broadcast_for_if(name):
public:
//p def __init__(self, owner, name, device=None, bindip=None, bindport=None, forwardip=None, forwardport=None):
UDPInterface(const char* name = "UDPInterface");
virtual ~UDPInterface();
//bool start(const char* wifi_ssid, const char* wifi_password, int port = DEFAULT_UDP_PORT, const char* local_host = nullptr);
virtual bool start();
virtual void stop();
virtual void loop();
virtual inline std::string toString() const { return "UDPInterface[" + _name + "/" + _local_host + ":" + std::to_string(_local_port) + "]"; }
//virtual inline std::string toString() const { return "UDPInterface[" + name() + "]"; }
protected:
virtual void on_incoming(const RNS::Bytes& data);
//virtual void on_outgoing(const RNS::Bytes& data);
virtual void send_outgoing(const RNS::Bytes& data);
private:
const uint16_t HW_MTU = 1064;
//uint8_t buffer[Type::Reticulum::MTU] = {0};
RNS::Bytes _buffer;
// WiFi network name and password
std::string _wifi_ssid;
std::string _wifi_password;
// IP address to send UDP data to.
// it can be ip address of the server or
// broadcast
std::string _local_host = DEFAULT_UDP_LOCAL_HOST;
int _local_port = DEFAULT_UDP_PORT;
std::string _remote_host = DEFAULT_UDP_REMOTE_HOST;
int _remote_port = DEFAULT_UDP_PORT;
// create UDP instance
#ifdef ARDUINO
WiFiUDP udp;
//AsyncUDP udp;
#else
int _socket = -1;
in_addr_t _local_address = INADDR_ANY;
in_addr_t _remote_address = INADDR_NONE;
#endif
};
@@ -1,6 +1,7 @@
//#define NDEBUG
#include "UDPInterface.h"
#include "FileSystem.h"
#include "Reticulum.h"
#include "Identity.h"
@@ -87,10 +88,13 @@ void onPingPacket(const RNS::Bytes& data, const RNS::Packet& packet) {
RNS::Reticulum reticulum({RNS::Type::NONE});
RNS::Interface udp_interface(RNS::Type::NONE);
RNS::FileSystem test_filesystem(RNS::Type::NONE);
RNS::Identity identity({RNS::Type::NONE});
RNS::Destination destination({RNS::Type::NONE});
RNS::Interfaces::UDPInterface udp_interface("udp");
UDPInterface* udp_interface_impl = nullptr;
FileSystem* test_filesystem_impl = nullptr;
//ExampleAnnounceHandler announce_handler((const char*)"example_utilities.announcesample.fruits");
//RNS::HAnnounceHandler announce_handler(new ExampleAnnounceHandler("example_utilities.announcesample.fruits"));
@@ -120,12 +124,19 @@ void reticulum_setup() {
// 21.8% baseline here with serial
test_filesystem_impl = new FileSystem();
test_filesystem = test_filesystem_impl;
((FileSystem*)test_filesystem.get())->init();
RNS::Utilities::OS::register_filesystem(test_filesystem);
HEAD("Registering Interface instances with Transport...", RNS::LOG_TRACE);
udp_interface_impl = new UDPInterface();
udp_interface = udp_interface_impl;
udp_interface.mode(RNS::Type::Interface::MODE_GATEWAY);
RNS::Transport::register_interface(udp_interface);
HEAD("Starting UDPInterface...", RNS::LOG_TRACE);
udp_interface.start("wifi_ssid", "wifi_password", 4242);
udp_interface_impl->start();
HEAD("Creating Reticulum instance...", RNS::LOG_TRACE);
//RNS::Reticulum reticulum;
@@ -216,7 +227,7 @@ void reticulum_setup() {
void reticulum_teardown() {
INFO("Tearing down Reticulum...");
RNS::Transport::save_path_table();
//zzzCBA RNS::Transport::save_path_table();
try {
@@ -282,7 +293,7 @@ void setup() {
void loop() {
reticulum.loop();
udp_interface.loop();
udp_interface_impl->loop();
#ifdef ARDUINO
/*
+237
View File
@@ -0,0 +1,237 @@
; PlatformIO Project Configuration File
;
; Build options: build flags, source filter
; Upload options: custom upload port, speed and extra flags
; Library options: dependencies, extra library storages
; Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html
[platformio]
;default_envs = native
;include_dir = ../../src
;src_dir = ../../src
include_dir = .
src_dir = .
[env]
monitor_speed = 115200
upload_speed = 921600
build_type = debug
build_flags =
-DRNS_USE_ALLOCATOR=1
-DRNS_USE_TLSF=1
lib_deps =
[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}
ArduinoJson@^6.21.3
https://github.com/attermann/arduinolibs.git
microReticulum=symlink://../..
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}
ArduinoJson@^6.21.3
https://github.com/attermann/arduinolibs.git
microReticulum=symlink://../..
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}
ArduinoJson@^6.21.3
https://github.com/attermann/arduinolibs.git
microReticulum=symlink://../..
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}
ArduinoJson@^6.21.3
https://github.com/attermann/arduinolibs.git
microReticulum=symlink://../..
; 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
ArduinoJson@^6.21.3
https://github.com/attermann/arduinolibs.git
microReticulum=symlink://../..
[env:ttgo-lora32-v21]
framework = arduino
platform = espressif32
board = ttgo-lora32-v21
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}
ArduinoJson@^6.21.3
https://github.com/attermann/arduinolibs.git
microReticulum=symlink://../..
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}
ArduinoJson@^6.21.3
https://github.com/attermann/arduinolibs.git
microReticulum=symlink://../..
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}
ArduinoJson@^6.21.3
https://github.com/attermann/arduinolibs.git
microReticulum=symlink://../..
[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}
+5
View File
@@ -0,0 +1,5 @@
.pio
.vscode/.browse.c_cpp.db*
.vscode/c_cpp_properties.json
.vscode/launch.json
.vscode/ipch
+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.
+525
View File
@@ -0,0 +1,525 @@
#include "FileSystem.h"
#include <Utilities/OS.h>
#include <Log.h>
bool FileSystem::init() {
TRACE("FileSystem initializing...");
#ifdef ARDUINO
#ifdef BOARD_ESP32
// Setup FileSystem
INFO("SPIFFS mounting FileSystem");
if (!SPIFFS.begin(true, "")){
ERROR("SPIFFS FileSystem mount failed");
return false;
}
uint32_t size = SPIFFS.totalBytes() / (1024 * 1024);
Serial.print("size: ");
Serial.print(size);
Serial.println(" MB");
uint32_t used = SPIFFS.usedBytes() / (1024 * 1024);
Serial.print("used: ");
Serial.print(used);
Serial.println(" MB");
// ensure FileSystem is writable and format if not
RNS::Bytes test("test");
if (write_file("/test", test) < 4) {
INFO("SPIFFS FileSystem is being formatted, please wait...");
SPIFFS.format();
}
else {
remove_file("/test");
}
DEBUG("SPIFFS FileSystem is ready");
#elif BOARD_NRF52
// Initialize Internal File System
INFO("InternalFS mounting FileSystem");
InternalFS.begin();
INFO("InternalFS FileSystem is ready");
#endif
#endif
return true;
}
#ifdef ARDUINO
void FileSystem::listDir(const char* dir) {
Serial.print("DIR: ");
Serial.println(dir);
#ifdef BOARD_ESP32
File root = SPIFFS.open(dir);
if (!root) {
Serial.println("Failed to opend directory");
return;
}
File file = root.openNextFile();
while (file) {
if (file.isDirectory()) {
Serial.print(" DIR: ");
}
else {
Serial.print(" FILE: ");
}
Serial.println(file.name());
file.close();
file = root.openNextFile();
}
root.close();
#elif BOARD_NRF52
File root = InternalFS.open(dir);
if (!root) {
Serial.println("Failed to opend directory");
return;
}
File file = root.openNextFile();
while (file) {
if (file.isDirectory()) {
Serial.print(" DIR: ");
}
else {
Serial.print(" FILE: ");
}
Serial.println(file.name());
file.close();
file = root.openNextFile();
}
root.close();
#endif
}
#else
void FileSystem::listDir(const char* dir) {
}
#endif
/*virtual*/ bool FileSystem::file_exists(const char* file_path) {
#ifdef ARDUINO
#ifdef BOARD_ESP32
File file = SPIFFS.open(file_path, FILE_READ);
if (file) {
#elif BOARD_NRF52
File file(InternalFS);
if (file.open(file_path, FILE_O_READ)) {
#else
if (false) {
#endif
#else
// Native
FILE* file = fopen(file_path, "r");
if (file != nullptr) {
#endif
//TRACE("file_exists: file exists, closing file");
#ifdef ARDUINO
#ifdef BOARD_ESP32
file.close();
#elif BOARD_NRF52
file.close();
#endif
#else
// Native
fclose(file);
#endif
return true;
}
else {
ERROR("file_exists: failed to open file " + std::string(file_path));
return false;
}
}
/*virtual*/ size_t FileSystem::read_file(const char* file_path, RNS::Bytes& data) {
size_t read = 0;
#ifdef ARDUINO
#ifdef BOARD_ESP32
File file = SPIFFS.open(file_path, FILE_READ);
if (file) {
size_t size = file.size();
read = file.readBytes((char*)data.writable(size), size);
#elif BOARD_NRF52
//File file(InternalFS);
//if (file.open(file_path, FILE_O_READ)) {
File file = InternalFS.open(file_path, FILE_O_READ);
if (file) {
size_t size = file.size();
read = file.readBytes((char*)data.writable(size), size);
#else
if (false) {
size_t size = 0;
#endif
#else
// Native
FILE* file = fopen(file_path, "r");
if (file != nullptr) {
fseek(file, 0, SEEK_END);
size_t size = ftell(file);
rewind(file);
//size_t read = fread(data.writable(size), size, 1, file);
read = fread(data.writable(size), 1, size, file);
#endif
TRACE("read_file: read " + std::to_string(read) + " bytes from file " + std::string(file_path));
if (read != size) {
ERROR("read_file: failed to read file " + std::string(file_path));
data.clear();
}
//TRACE("read_file: closing input file");
#ifdef ARDUINO
#ifdef BOARD_ESP32
file.close();
#elif BOARD_NRF52
file.close();
#endif
#else
// Native
fclose(file);
#endif
}
else {
ERROR("read_file: failed to open input file " + std::string(file_path));
}
return read;
}
/*virtual*/ size_t FileSystem::write_file(const char* file_path, const RNS::Bytes& data) {
// CBA TODO Replace remove with working truncation
remove_file(file_path);
size_t wrote = 0;
#ifdef ARDUINO
#ifdef BOARD_ESP32
File file = SPIFFS.open(file_path, FILE_WRITE);
if (file) {
wrote = file.write(data.data(), data.size());
#elif BOARD_NRF52
File file(InternalFS);
if (file.open(file_path, FILE_O_WRITE)) {
wrote = file.write(data.data(), data.size());
#else
if (false) {
#endif
#else
// Native
FILE* file = fopen(file_path, "w");
if (file != nullptr) {
//size_t wrote = fwrite(data.data(), data.size(), 1, file);
wrote = fwrite(data.data(), 1, data.size(), file);
#endif
TRACE("write_file: wrote " + std::to_string(wrote) + " bytes to file " + std::string(file_path));
if (wrote < data.size()) {
WARNING("write_file: not all data was written to file " + std::string(file_path));
}
//TRACE("write_file: closing output file");
#ifdef ARDUINO
#ifdef BOARD_ESP32
file.close();
#elif BOARD_NRF52
file.close();
#endif
#else
// Native
fclose(file);
#endif
}
else {
ERROR("write_file: failed to open output file " + std::string(file_path));
}
return wrote;
}
/*virtual*/ RNS::FileStream FileSystem::open_file(const char* file_path, RNS::FileStream::MODE file_mode) {
TRACEF("open_file: opening file %s", file_path);
#ifdef ARDUINO
#ifdef BOARD_ESP32
const char* mode;
if (file_mode == RNS::FileStream::MODE_READ) {
mode = FILE_READ;
}
else if (file_mode == RNS::FileStream::MODE_WRITE) {
mode = FILE_WRITE;
}
else if (file_mode == RNS::FileStream::MODE_APPEND) {
mode = FILE_APPEND;
}
else {
ERRORF("open_file: unsupported mode %d", file_mode);
return {RNS::Type::NONE};
}
TRACEF("open_file: opening file %s in mode %s", file_path, mode);
//// Using copy constructor to create a File* instead of local
//File file = SPIFFS.open(file_path, mode);
//if (!file) {
File* file = new File(SPIFFS.open(file_path, mode));
if (file == null || !(*file)) {
ERRORF("open_file: failed to open output file %s", file_path);
return {RNS::Type::NONE};
}
TRACEF("open_file: successfully opened file %s", file_path);
return RNS::FileStream(new FileStreamImpl(file));
#elif BOARD_NRF52
//File file = File(InternalFS);
File* file = new File(InternalFS);
int mode;
if (file_mode == RNS::FileStream::MODE_READ) {
mode = FILE_O_READ;
}
else if (file_mode == RNS::FileStream::MODE_WRITE) {
mode = FILE_O_WRITE;
// CBA TODO Replace remove with working truncation
if (InternalFS.exists(file_path)) {
InternalFS.remove(file_path);
}
}
else if (file_mode == RNS::FileStream::MODE_APPEND) {
// CBA This is the default write mode for nrf52 littlefs
mode = FILE_O_WRITE;
}
else {
ERRORF("open_file: unsupported mode %d", file_mode);
return {RNS::Type::NONE};
}
if (!file->open(file_path, mode)) {
ERRORF("open_file: failed to open output file %s", file_path);
return {RNS::Type::NONE};
}
// Seek to beginning to overwrite (this is failing on nrf52)
//if (file_mode == RNS::FileStream::MODE_WRITE) {
// file->seek(0);
// file->truncate(0);
//}
TRACEF("open_file: successfully opened file %s", file_path);
return RNS::FileStream(new FileStreamImpl(file));
#else
#warning("unsuppoprted");
return RNS::FileStream(RNS::Type::NONE);
#endif
#else // ARDUINO
// Native
const char* mode;
if (file_mode == RNS::FileStream::MODE_READ) {
mode = "r";
}
else if (file_mode == RNS::FileStream::MODE_WRITE) {
mode = "w";
}
else if (file_mode == RNS::FileStream::MODE_APPEND) {
mode = "a";
}
else {
ERRORF("open_file: unsupported mode %d", file_mode);
return {RNS::Type::NONE};
}
TRACEF("open_file: opening file %s in mode %s", file_path, mode);
FILE* file = fopen(file_path, mode);
if (file == nullptr) {
ERRORF("open_file: failed to open output file %s", file_path);
return {RNS::Type::NONE};
}
TRACEF("open_file: successfully opened file %s", file_path);
return RNS::FileStream(new FileStreamImpl(file));
#endif
}
/*virtual*/ bool FileSystem::remove_file(const char* file_path) {
#ifdef ARDUINO
#ifdef BOARD_ESP32
return SPIFFS.remove(file_path);
#elif BOARD_NRF52
return InternalFS.remove(file_path);
#else
return false;
#endif
#else
// Native
return (remove(file_path) == 0);
#endif
}
/*virtual*/ bool FileSystem::rename_file(const char* from_file_path, const char* to_file_path) {
#ifdef ARDUINO
#ifdef BOARD_ESP32
return SPIFFS.rename(from_file_path, to_file_path);
#elif BOARD_NRF52
return InternalFS.rename(from_file_path, to_file_path);
#else
return false;
#endif
#else
// Native
return (rename(from_file_path, to_file_path) == 0);
#endif
}
/*virtua*/ bool FileSystem::directory_exists(const char* directory_path) {
TRACE("directory_exists: checking for existence of directory " + std::string(directory_path));
#ifdef ARDUINO
#ifdef BOARD_ESP32
File file = SPIFFS.open(directory_path, FILE_READ);
if (file) {
bool is_directory = file.isDirectory();
file.close();
return is_directory;
}
#elif BOARD_NRF52
File file(InternalFS);
if (file.open(directory_path, FILE_O_READ)) {
bool is_directory = file.isDirectory();
file.close();
return is_directory;
}
#else
if (false) {
return false;
}
#endif
else {
return false;
}
#else
// Native
return false;
#endif
}
/*virtual*/ bool FileSystem::create_directory(const char* directory_path) {
#ifdef ARDUINO
#ifdef BOARD_ESP32
if (!SPIFFS.mkdir(directory_path)) {
ERROR("create_directory: failed to create directorty " + std::string(directory_path));
return false;
}
return true;
#elif BOARD_NRF52
if (!InternalFS.mkdir(directory_path)) {
ERROR("create_directory: failed to create directorty " + std::string(directory_path));
return false;
}
return true;
#else
return false;
#endif
#else
// Native
struct stat st = {0};
if (stat(directory_path, &st) == 0) {
return true;
}
return (mkdir(directory_path, 0700) == 0);
#endif
}
/*virtua*/ bool FileSystem::remove_directory(const char* directory_path) {
TRACE("remove_directory: removing directory " + std::string(directory_path));
#ifdef ARDUINO
#ifdef BOARD_ESP32
//if (!LittleFS.rmdir_r(directory_path)) {
if (!SPIFFS.rmdir(directory_path)) {
ERROR("remove_directory: failed to remove directorty " + std::string(directory_path));
return false;
}
return true;
#elif BOARD_NRF52
if (!InternalFS.rmdir_r(directory_path)) {
ERROR("remove_directory: failed to remove directory " + std::string(directory_path));
return false;
}
return true;
#else
return false;
#endif
#else
// Native
return false;
#endif
}
/*virtua*/ std::list<std::string> FileSystem::list_directory(const char* directory_path) {
TRACE("list_directory: listing directory " + std::string(directory_path));
std::list<std::string> files;
#ifdef ARDUINO
#ifdef BOARD_ESP32
File root = SPIFFS.open(directory_path);
#elif BOARD_NRF52
File root = InternalFS.open(directory_path);
#endif
if (!root) {
ERROR("list_directory: failed to open directory " + std::string(directory_path));
return files;
}
File file = root.openNextFile();
while (file) {
if (!file.isDirectory()) {
char* name = (char*)file.name();
files.push_back(name);
}
// CBA Following close required to avoid leaking memory
file.close();
file = root.openNextFile();
}
TRACE("list_directory: returning directory listing");
root.close();
return files;
#else
// Native
return files;
#endif
}
#ifdef ARDUINO
#ifdef BOARD_ESP32
/*virtual*/ size_t FileSystem::storage_size() {
return SPIFFS.totalBytes();
}
/*virtual*/ size_t FileSystem::storage_available() {
return (SPIFFS.totalBytes() - SPIFFS.usedBytes());
}
#elif BOARD_NRF52
static int _countLfsBlock(void *p, lfs_block_t block){
lfs_size_t *size = (lfs_size_t*) p;
*size += 1;
return 0;
}
static lfs_ssize_t getUsedBlockCount() {
lfs_size_t size = 0;
lfs_traverse(InternalFS._getFS(), _countLfsBlock, &size);
return size;
}
static int totalBytes() {
const lfs_config* config = InternalFS._getFS()->cfg;
return config->block_size * config->block_count;
}
static int usedBytes() {
const lfs_config* config = InternalFS._getFS()->cfg;
const int usedBlockCount = getUsedBlockCount();
return config->block_size * usedBlockCount;
}
/*virtual*/ size_t FileSystem::storage_size() {
//return totalBytes();
return InternalFS.totalBytes();
}
/*virtual*/ size_t FileSystem::storage_available() {
//return (totalBytes() - usedBytes());
return (InternalFS.totalBytes() - InternalFS.usedBytes());
}
#endif
#else
/*virtual*/ size_t FileSystem::storage_size() {
return 0;
}
/*virtual*/ size_t FileSystem::storage_available() {
return 0;
}
#endif
+184
View File
@@ -0,0 +1,184 @@
#pragma once
#include <FileSystem.h>
#include <FileStream.h>
#include <Bytes.h>
#ifdef ARDUINO
#ifdef BOARD_ESP32
//#include <FS.h>
#include <SPIFFS.h>
#elif BOARD_NRF52
//#include <Adafruit_LittleFS.h>
#include <InternalFileSystem.h>
using namespace Adafruit_LittleFS_Namespace;
#endif
#else
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <unistd.h>
#endif
class FileSystem : public RNS::FileSystemImpl {
public:
FileSystem() {}
bool init();
public:
static void listDir(const char* dir);
public:
virtual bool file_exists(const char* file_path);
virtual size_t read_file(const char* file_path, RNS::Bytes& data);
virtual size_t write_file(const char* file_path, const RNS::Bytes& data);
virtual RNS::FileStream open_file(const char* file_path, RNS::FileStream::MODE file_mode);
virtual bool remove_file(const char* file_path);
virtual bool rename_file(const char* from_file_path, const char* to_file_path);
virtual bool directory_exists(const char* directory_path);
virtual bool create_directory(const char* directory_path);
virtual bool remove_directory(const char* directory_path);
virtual std::list<std::string> list_directory(const char* directory_path);
virtual size_t storage_size();
virtual size_t storage_available();
protected:
#if defined(BOARD_ESP32) || defined(BOARD_NRF52)
class FileStreamImpl : public RNS::FileStreamImpl {
private:
std::unique_ptr<File> _file;
bool _closed = false;
public:
FileStreamImpl(File* file) : RNS::FileStreamImpl(), _file(file) {}
virtual ~FileStreamImpl() { if (!_closed) close(); }
public:
inline virtual const char* name() { return _file->name(); }
inline virtual size_t size() { return _file->size(); }
inline virtual void close() { _closed = true; _file->close(); }
// Print overrides
inline virtual size_t write(uint8_t byte) { return _file->write(byte); }
inline virtual size_t write(const uint8_t *buffer, size_t size) { return _file->write(buffer, size); }
// Stream overrides
inline virtual int available() { return _file->available(); }
inline virtual int read() { return _file->read(); }
inline virtual int peek() { return _file->peek(); }
inline virtual void flush() { _file->flush(); }
};
#else
class FileStreamImpl : public RNS::FileStreamImpl {
private:
FILE* _file = nullptr;
bool _closed = false;
size_t _available = 0;
char _filename[1024];
public:
FileStreamImpl(FILE* file) : RNS::FileStreamImpl(), _file(file) {
_available = size();
}
virtual ~FileStreamImpl() { if (!_closed) close(); }
public:
inline virtual const char* name() {
assert(_file);
#if 0
char proclnk[1024];
snprintf(proclnk, sizeof(proclnk), "/proc/self/fd/%d", fileno(_file));
int r = readlink(proclnk, _filename, sizeof(_filename));
if (r < 0) {
return nullptr);
}
_filename[r] = '\0';
return _filename;
#elif 0
if (fcntl(fd, F_GETPATH, _filename) < 0) {
rerturn nullptr;
}
return _filename;
#else
return nullptr;
#endif
}
inline virtual size_t size() {
assert(_file);
struct stat st;
fstat(fileno(_file), &st);
return st.st_size;
}
inline virtual void close() {
assert(_file);
_closed = true;
fclose(_file);
_file = nullptr;
}
// Print overrides
inline virtual size_t write(uint8_t byte) {
assert(_file);
int ch = fputc(byte, _file);
if (ch == EOF) {
return 0;
}
++_available;
return 1;
}
inline virtual size_t write(const uint8_t *buffer, size_t size) {
assert(_file);
size_t wrote = fwrite(buffer, sizeof(uint8_t), size, _file);
_available += wrote;
return wrote;
}
// Stream overrides
inline virtual int available() {
#if 0
assert(_file);
int size = 0;
ioctl(fileno(_file), FIONREAD, &size);
TRACEF("FileStream::available: %d", size);
return size;
#else
return _available;
#endif
}
inline virtual int read() {
if (_available <= 0) {
return EOF;
}
assert(_file);
int ch = fgetc(_file);
TRACEF("FileStream::read: %c", ch);
return ch;
}
inline virtual int peek() {
if (_available <= 0) {
return EOF;
}
assert(_file);
int ch = fgetc(_file);
ungetc(ch, _file);
TRACEF("FileStream::peek: %c", ch);
return ch;
}
inline virtual void flush() {
assert(_file);
fflush(_file);
TRACE("FileStream::flush");
}
};
#endif
};
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
+204
View File
@@ -0,0 +1,204 @@
; PlatformIO Project Configuration File
;
; Build options: build flags, source filter
; Upload options: custom upload port, speed and extra flags
; Library options: dependencies, extra library storages
; Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html
[platformio]
;default_envs = native
;include_dir = ../../src
;src_dir = ../../src
include_dir = .
src_dir = .
[env]
monitor_speed = 115200
upload_speed = 921600
build_type = debug
build_flags =
-DRNS_USE_ALLOCATOR=1
-DRNS_USE_TLSF=1
lib_deps =
[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}
ArduinoJson@^6.21.3
https://github.com/attermann/arduinolibs.git
microReticulum=symlink://../..
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}
ArduinoJson@^6.21.3
https://github.com/attermann/arduinolibs.git
microReticulum=symlink://../..
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}
ArduinoJson@^6.21.3
https://github.com/attermann/arduinolibs.git
microReticulum=symlink://../..
; 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
ArduinoJson@^6.21.3
https://github.com/attermann/arduinolibs.git
microReticulum=symlink://../..
[env:ttgo-lora32-v21]
framework = arduino
platform = espressif32
board = ttgo-lora32-v21
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}
ArduinoJson@^6.21.3
https://github.com/attermann/arduinolibs.git
microReticulum=symlink://../..
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}
ArduinoJson@^6.21.3
https://github.com/attermann/arduinolibs.git
microReticulum=symlink://../..
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}
ArduinoJson@^6.21.3
https://github.com/attermann/arduinolibs.git
microReticulum=symlink://../..
[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}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
+3 -1
View File
@@ -47,6 +47,8 @@ namespace RNS {
// CBA handle data coming in on interface
//virtual void handle_incoming(const Bytes& data);
virtual inline std::string toString() const { return "Interface[" + _name + "]"; }
protected:
Interface* _parent = nullptr;
bool _IN = false;
@@ -172,7 +174,7 @@ namespace RNS {
inline bool is_local_shared_instance() const { assert(_impl); return _impl->_is_local_shared_instance; }
inline HInterface parent_interface() const { assert(_impl); return _impl->_parent_interface; }
virtual inline std::string toString() const { if (!_impl) return ""; return "Interface[" + _impl->_name + "]"; }
virtual inline std::string toString() const { if (!_impl) return ""; return _impl->toString(); }
#ifndef NDEBUG
inline std::string debugString() const {
+9
View File
@@ -0,0 +1,9 @@
#include <stdio.h>
#ifdef NATIVE
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");
}
#endif