mirror of
https://github.com/ratspeak/microReticulum
synced 2026-11-22 23:10:38 +00:00
Compare commits
1
Commits
base64
...
tcp_announce
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
01cf1afdae |
No files matched your search
@@ -0,0 +1,360 @@
|
||||
#include "TCPInterface.h"
|
||||
|
||||
#include "../src/Log.h"
|
||||
#include <Transport.h>
|
||||
|
||||
#ifndef ARDUINO
|
||||
#include <arpa/inet.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <netdb.h>
|
||||
#include <netinet/in.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/socket.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
using namespace RNS;
|
||||
|
||||
/*
|
||||
@staticmethod
|
||||
def get_address_for_if(name):
|
||||
import RNS.vendor.ifaddr.niwrapper as netinfo
|
||||
ifaddr = netinfo.ifaddresses(name)
|
||||
return ifaddr[netinfo.AF_INET][0]["addr"]
|
||||
|
||||
@staticmethod
|
||||
def get_broadcast_for_if(name):
|
||||
import RNS.vendor.ifaddr.niwrapper as netinfo
|
||||
ifaddr = netinfo.ifaddresses(name)
|
||||
return ifaddr[netinfo.AF_INET][0]["broadcast"]
|
||||
*/
|
||||
|
||||
// p def __init__(self, owner, name, device=None, bindip=None, bindport=None,
|
||||
// forwardip=None, forwardport=None):
|
||||
TCPInterface::TCPInterface(const char *name /*= "TCPInterface"*/)
|
||||
: RNS::InterfaceImpl(name) {
|
||||
|
||||
_IN = true;
|
||||
_OUT = true;
|
||||
_bitrate = BITRATE_GUESS;
|
||||
_HW_MTU = 1064;
|
||||
}
|
||||
|
||||
/*virtual*/ TCPInterface::~TCPInterface() { stop(); }
|
||||
|
||||
// bool TCPInterface::start(const char* wifi_ssid, const char* wifi_password,
|
||||
// int port /*= DEFAULT_TCP_PORT*/, const char* local_host /*= nullptr*/) {
|
||||
/*virtual*/ bool TCPInterface::start() {
|
||||
const char *wifi_ssid = "wifi_ssid";
|
||||
const char *wifi_password = "wifi_password";
|
||||
int port = DEFAULT_TCP_LISTEN_PORT;
|
||||
const char *listen_host = nullptr;
|
||||
|
||||
_online = false;
|
||||
|
||||
if (wifi_ssid != nullptr) {
|
||||
_wifi_ssid = wifi_ssid;
|
||||
}
|
||||
if (wifi_password != nullptr) {
|
||||
_wifi_password = wifi_password;
|
||||
}
|
||||
if (listen_host != nullptr) {
|
||||
_listen_host = listen_host;
|
||||
}
|
||||
_listen_port = port;
|
||||
TRACE("TCPInterface: listen host: " + _listen_host);
|
||||
TRACE("TCPInterface: listen port: " + std::to_string(_listen_port));
|
||||
|
||||
#ifdef ARDUINO
|
||||
// TODO: ALL THIS STUFF
|
||||
TRACE("TCPInterface: wifi ssid: " + _wifi_ssid);
|
||||
TRACE("TCPInterface: wifi password: " + _wifi_password);
|
||||
|
||||
// Connect to the WiFi network
|
||||
WiFi.begin(_wifi_ssid.c_str(), _wifi_password.c_str());
|
||||
Serial.println("");
|
||||
|
||||
// Wait for WiFi network connection to complete
|
||||
Serial.print("Connecting to ");
|
||||
Serial.print(_wifi_ssid.c_str());
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(500);
|
||||
Serial.print(".");
|
||||
}
|
||||
Serial.println("");
|
||||
Serial.print("Connected to ");
|
||||
Serial.println(_wifi_ssid.c_str());
|
||||
Serial.print("IP address: ");
|
||||
Serial.println(WiFi.localIP());
|
||||
|
||||
// This initializes tcp server and transfer buffer
|
||||
server.begin(_listen_port);
|
||||
#else
|
||||
|
||||
struct in_addr local_addr;
|
||||
if (inet_aton(_listen_host.c_str(), &local_addr) == 0) {
|
||||
struct hostent *host_ent = gethostbyname(_listen_host.c_str());
|
||||
if (host_ent == nullptr || host_ent->h_addr_list[0] == nullptr) {
|
||||
ERROR("Unable to resolve local host " + std::string(listen_host));
|
||||
return false;
|
||||
}
|
||||
_local_address = *((in_addr_t *)(host_ent->h_addr_list[0]));
|
||||
} else {
|
||||
_local_address = local_addr.s_addr;
|
||||
}
|
||||
|
||||
TRACE("Opening TCP socket");
|
||||
_socket = socket(PF_INET, SOCK_STREAM, 0);
|
||||
if (_socket < 0) {
|
||||
ERROR("Unable to create socket with error " + std::to_string(errno));
|
||||
return false;
|
||||
}
|
||||
|
||||
int reuse = 1;
|
||||
setsockopt(_socket, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse));
|
||||
#ifdef SO_REUSEPORT
|
||||
setsockopt(_socket, SOL_SOCKET, SO_REUSEPORT, &reuse, sizeof(reuse));
|
||||
#endif
|
||||
|
||||
INFO("Binding TCP socket " + std::to_string(_socket) + " to " + _listen_host +
|
||||
":" + std::to_string(_listen_port));
|
||||
sockaddr_in bind_addr;
|
||||
bind_addr.sin_family = AF_INET;
|
||||
bind_addr.sin_addr.s_addr = _local_address;
|
||||
bind_addr.sin_port = htons(_listen_port);
|
||||
if (bind(_socket, (struct sockaddr *)&bind_addr, sizeof(bind_addr)) == -1) {
|
||||
close(_socket);
|
||||
_socket = -1;
|
||||
ERROR("Unable to bind socket with error " + std::to_string(errno));
|
||||
return false;
|
||||
}
|
||||
|
||||
if (listen(_socket, 5) == -1) {
|
||||
close(_socket);
|
||||
_socket = -1;
|
||||
ERROR("Unable to listen on socket with error " + std::to_string(errno));
|
||||
return false;
|
||||
}
|
||||
INFO("TCP socket listening on " + _listen_host + ":" +
|
||||
std::to_string(_listen_port));
|
||||
|
||||
int flags = fcntl(_socket, F_GETFL, 0);
|
||||
fcntl(_socket, F_SETFL, flags | O_NONBLOCK);
|
||||
#endif
|
||||
|
||||
_online = true;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/*virtual*/ void TCPInterface::stop() {
|
||||
#ifdef ARDUINO
|
||||
#else
|
||||
// Close all client connections
|
||||
for (int client_socket : _client_sockets) {
|
||||
close(client_socket);
|
||||
}
|
||||
_client_sockets.clear();
|
||||
_client_states.clear();
|
||||
|
||||
// Close server socket
|
||||
if (_socket > -1) {
|
||||
close(_socket);
|
||||
_socket = -1;
|
||||
}
|
||||
#endif
|
||||
|
||||
_online = false;
|
||||
}
|
||||
|
||||
/*virtual*/ void TCPInterface::loop() {
|
||||
|
||||
if (_online) {
|
||||
#ifdef ARDUINO
|
||||
// Check for incoming clients
|
||||
WiFiClient client = server.available();
|
||||
|
||||
if (client) {
|
||||
Serial.println("New client connected!");
|
||||
_clients.push_back(client);
|
||||
// Loop while the client is connected
|
||||
while (client.connected()) {
|
||||
// Check if there is data available to read
|
||||
if (client.available()) {
|
||||
String data = client.readStringUntil('\n'); // Read until newline character
|
||||
Serial.print("Received from client: ");
|
||||
Serial.println(data);
|
||||
|
||||
// Send a response back to the client
|
||||
client.println("Message received!");
|
||||
}
|
||||
}
|
||||
// Close the connection when the client disconnects
|
||||
client.stop();
|
||||
Serial.println("Client disconnected.");
|
||||
}
|
||||
#else
|
||||
sockaddr_in client_addr;
|
||||
socklen_t client_len = sizeof(client_addr);
|
||||
int new_client =
|
||||
accept(_socket, (struct sockaddr *)&client_addr, &client_len);
|
||||
|
||||
if (new_client > 0) {
|
||||
int flags = fcntl(new_client, F_GETFL, 0);
|
||||
fcntl(new_client, F_SETFL, flags | O_NONBLOCK);
|
||||
|
||||
_client_sockets.push_back(new_client);
|
||||
_client_states[new_client] = ClientState();
|
||||
INFO("New client connected: socket " + std::to_string(new_client) +
|
||||
" from " + std::string(inet_ntoa(client_addr.sin_addr)) + ":" +
|
||||
std::to_string(ntohs(client_addr.sin_port)));
|
||||
}
|
||||
|
||||
for (auto it = _client_sockets.begin(); it != _client_sockets.end();) {
|
||||
int client_socket = *it;
|
||||
size_t available = 0;
|
||||
|
||||
if (ioctl(client_socket, FIONREAD, &available) == 0 && available > 0) {
|
||||
RNS::Bytes temp_buffer;
|
||||
ssize_t len =
|
||||
read(client_socket, temp_buffer.writable(available), available);
|
||||
if (len > 0) {
|
||||
temp_buffer.resize(len);
|
||||
_client_states[client_socket].buffer.append(temp_buffer);
|
||||
process_frames(client_socket);
|
||||
++it;
|
||||
} else if (len == 0) {
|
||||
INFO("Client disconnected: socket " + std::to_string(client_socket));
|
||||
close(client_socket);
|
||||
_client_states.erase(client_socket);
|
||||
it = _client_sockets.erase(it);
|
||||
} else {
|
||||
ERROR("Read error on client socket " + std::to_string(client_socket) +
|
||||
": " + std::to_string(errno));
|
||||
close(client_socket);
|
||||
_client_states.erase(client_socket);
|
||||
it = _client_sockets.erase(it);
|
||||
}
|
||||
} else {
|
||||
++it;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
/*virtual*/ void TCPInterface::send_outgoing(const Bytes &data) {
|
||||
DEBUG(toString() + ".on_outgoing: data: " + data.toHex());
|
||||
try {
|
||||
if (_online) {
|
||||
#ifdef ARDUINO
|
||||
// TODO: Arduino TCP implementation
|
||||
#else
|
||||
RNS::Bytes hdlc_frame = hdlc_encode(data);
|
||||
|
||||
for (auto it = _client_sockets.begin(); it != _client_sockets.end();) {
|
||||
int client_socket = *it;
|
||||
ssize_t sent = send(client_socket, hdlc_frame.data(), hdlc_frame.size(),
|
||||
MSG_NOSIGNAL);
|
||||
|
||||
if (sent > 0) {
|
||||
TRACE("Sent " + std::to_string(sent) + " bytes to client socket " +
|
||||
std::to_string(client_socket));
|
||||
++it;
|
||||
} else if (sent == 0 || (sent < 0 && (errno == EPIPE || errno == ECONNRESET))) {
|
||||
INFO("Client disconnected during send: socket " +
|
||||
std::to_string(client_socket));
|
||||
close(client_socket);
|
||||
_client_states.erase(client_socket);
|
||||
it = _client_sockets.erase(it);
|
||||
} else {
|
||||
ERROR("Send error on client socket " + std::to_string(client_socket) +
|
||||
": " + std::to_string(errno));
|
||||
++it;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
InterfaceImpl::handle_outgoing(data);
|
||||
} catch (std::exception &e) {
|
||||
ERROR("Could not transmit on " + toString() +
|
||||
". The contained exception was: " + e.what());
|
||||
}
|
||||
}
|
||||
|
||||
void TCPInterface::process_frames(int client_socket) {
|
||||
ClientState &state = _client_states[client_socket];
|
||||
hdlc_decode_stream(state.buffer, client_socket);
|
||||
}
|
||||
|
||||
RNS::Bytes TCPInterface::hdlc_encode(const RNS::Bytes &data) {
|
||||
DEBUG("HDLC encode input: " + data.toHex());
|
||||
|
||||
std::vector<uint8_t> buffer;
|
||||
buffer.reserve(data.size() * 2 + 2);
|
||||
|
||||
buffer.push_back(0x7E);
|
||||
|
||||
for (size_t i = 0; i < data.size(); i++) {
|
||||
uint8_t byte = data[i];
|
||||
if (byte == 0x7E || byte == 0x7D) {
|
||||
uint8_t escaped_byte = byte ^ 0x20;
|
||||
DEBUG("HDLC escape: " + RNS::Bytes(&byte, 1).toHex() + " -> 7d" +
|
||||
RNS::Bytes(&escaped_byte, 1).toHex());
|
||||
buffer.push_back(0x7D); // Escape byte
|
||||
buffer.push_back(byte ^ 0x20); // Escaped data
|
||||
} else {
|
||||
buffer.push_back(byte); // Normal data
|
||||
}
|
||||
}
|
||||
|
||||
buffer.push_back(0x7E);
|
||||
|
||||
RNS::Bytes encoded;
|
||||
encoded.resize(buffer.size());
|
||||
memcpy(encoded.writable(buffer.size()), buffer.data(), buffer.size());
|
||||
|
||||
DEBUG("HDLC encode output: " + encoded.toHex());
|
||||
return encoded;
|
||||
}
|
||||
|
||||
void TCPInterface::hdlc_decode_stream(RNS::Bytes &buffer, int client_socket) {
|
||||
ClientState &state = _client_states[client_socket];
|
||||
|
||||
DEBUG("HDLC decode stream input: " + buffer.toHex());
|
||||
|
||||
for (size_t i = 0; i < buffer.size(); i++) {
|
||||
uint8_t byte = buffer[i];
|
||||
|
||||
if (byte == HDLC_FLAG) {
|
||||
if (state.in_frame && state.frame_data.size() > 0) {
|
||||
DEBUG("HDLC decode output: " + state.frame_data.toHex());
|
||||
on_incoming(state.frame_data);
|
||||
state.frame_data.clear();
|
||||
}
|
||||
state.in_frame = true;
|
||||
state.escape_next = false;
|
||||
} else if (state.in_frame) {
|
||||
if (state.escape_next) {
|
||||
uint8_t decoded_byte = byte ^ HDLC_ESC_MASK;
|
||||
DEBUG("HDLC unescape: " + RNS::Bytes(&byte, 1).toHex() + " -> " + RNS::Bytes(&decoded_byte, 1).toHex());
|
||||
state.frame_data.append(decoded_byte);
|
||||
state.escape_next = false;
|
||||
} else if (byte == HDLC_ESC) {
|
||||
DEBUG("HDLC escape found, next byte will be unescaped");
|
||||
state.escape_next = true;
|
||||
} else {
|
||||
state.frame_data.append(byte);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
buffer.clear();
|
||||
}
|
||||
|
||||
void TCPInterface::on_incoming(const Bytes &data) {
|
||||
DEBUG(toString() + ".on_incoming: data: " + data.toHex());
|
||||
InterfaceImpl::handle_incoming(data);
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
#pragma once
|
||||
|
||||
#include "../src/Bytes.h"
|
||||
#include "../src/Interface.h"
|
||||
|
||||
#ifdef ARDUINO
|
||||
#include <WiFi.h>
|
||||
#include <WiFiServer.h>
|
||||
#include <WiFiClient.h>
|
||||
#else
|
||||
#include <netinet/in.h>
|
||||
#endif
|
||||
|
||||
#include <map>
|
||||
#include <stdint.h>
|
||||
#include <vector>
|
||||
|
||||
#define DEFAULT_TCP_LISTEN_PORT 4242
|
||||
#define DEFAULT_TCP_LISTEN_IP "0.0.0.0"
|
||||
|
||||
class TCPInterface : public RNS::InterfaceImpl {
|
||||
|
||||
public:
|
||||
static const uint32_t BITRATE_GUESS = 10 * 1000 * 1000;
|
||||
|
||||
// HDLC constants
|
||||
static const uint8_t HDLC_FLAG = 0x7E;
|
||||
static const uint8_t HDLC_ESC = 0x7D;
|
||||
static const uint8_t HDLC_ESC_MASK = 0x20;
|
||||
|
||||
public:
|
||||
TCPInterface(const char *name = "TCPInterface");
|
||||
virtual ~TCPInterface();
|
||||
|
||||
virtual bool start();
|
||||
virtual void stop();
|
||||
virtual void loop();
|
||||
|
||||
virtual inline std::string toString() const {
|
||||
return "TCPInterface[" + _name + "/" + _listen_host + ":" +
|
||||
std::to_string(_listen_port) + "]";
|
||||
}
|
||||
|
||||
protected:
|
||||
virtual void send_outgoing(const RNS::Bytes &data);
|
||||
void on_incoming(const RNS::Bytes &data);
|
||||
void process_frames(int client_socket);
|
||||
|
||||
// HDLC framing methods
|
||||
RNS::Bytes hdlc_encode(const RNS::Bytes &data);
|
||||
void hdlc_decode_stream(RNS::Bytes &buffer, int client_socket);
|
||||
|
||||
private:
|
||||
// uint8_t buffer[Type::Reticulum::MTU] = {0};
|
||||
RNS::Bytes _buffer;
|
||||
|
||||
// TODO: REMOVE?
|
||||
std::string _wifi_ssid;
|
||||
std::string _wifi_password;
|
||||
|
||||
std::string _listen_host = DEFAULT_TCP_LISTEN_IP;
|
||||
int _listen_port = DEFAULT_TCP_LISTEN_PORT;
|
||||
|
||||
#ifdef ARDUINO
|
||||
WiFiServer server;
|
||||
std::vector<WiFiClient> _clients;
|
||||
#else
|
||||
int _socket = -1;
|
||||
in_addr_t _local_address = INADDR_ANY;
|
||||
std::vector<int> _client_sockets;
|
||||
#endif
|
||||
struct ClientState {
|
||||
RNS::Bytes buffer;
|
||||
RNS::Bytes frame_data;
|
||||
bool in_frame = false;
|
||||
bool escape_next = false;
|
||||
};
|
||||
std::map<int, ClientState> _client_states;
|
||||
};
|
||||
@@ -0,0 +1,10 @@
|
||||
name=tcp_interface
|
||||
version=0.0.1
|
||||
author=Chad Attermann <attermann@gmail.com>
|
||||
maintainer=Chad Attermann <attermann@gmail.com>
|
||||
sentence=TCPInterface implementation common to all examples
|
||||
paragraph=TCPInterface implementation common to all examples
|
||||
category=Communication
|
||||
url=
|
||||
architectures=*
|
||||
depends=
|
||||
@@ -0,0 +1,5 @@
|
||||
.pio
|
||||
.vscode/.browse.c_cpp.db*
|
||||
.vscode/c_cpp_properties.json
|
||||
.vscode/launch.json
|
||||
.vscode/ipch
|
||||
+10
@@ -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"
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,249 @@
|
||||
// #define NDEBUG
|
||||
|
||||
#include <TCPInterface.h>
|
||||
#include <UniversalFileSystem.h>
|
||||
|
||||
#include <Bytes.h>
|
||||
#include <Destination.h>
|
||||
#include <Identity.h>
|
||||
#include <Interface.h>
|
||||
#include <Log.h>
|
||||
#include <Packet.h>
|
||||
#include <Reticulum.h>
|
||||
#include <Transport.h>
|
||||
#include <Type.h>
|
||||
#include <Utilities/OS.h>
|
||||
|
||||
#ifdef ARDUINO
|
||||
#include <Arduino.h>
|
||||
#include <SPIFFS.h>
|
||||
#else
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <termios.h>
|
||||
#endif
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
// #include <sstream>
|
||||
|
||||
#define BUTTON_PIN 38
|
||||
const char *APP_NAME = "example_utilities";
|
||||
|
||||
// We initialise two lists of strings to use as app_data
|
||||
const char *fruits[] = {"Peach", "Quince", "Date", "Tangerine",
|
||||
"Pomelo", "Carambola", "Grape"};
|
||||
const char *noble_gases[] = {"Helium", "Neon", "Argon", "Krypton",
|
||||
"Xenon", "Radon", "Oganesson"};
|
||||
|
||||
double last_announce = 0.0;
|
||||
bool send_announce = false;
|
||||
|
||||
class ExampleAnnounceHandler : public RNS::AnnounceHandler {
|
||||
public:
|
||||
ExampleAnnounceHandler(const char *aspect_filter = nullptr)
|
||||
: AnnounceHandler(aspect_filter) {}
|
||||
virtual ~ExampleAnnounceHandler() {}
|
||||
virtual void received_announce(const RNS::Bytes &destination_hash,
|
||||
const RNS::Identity &announced_identity,
|
||||
const RNS::Bytes &app_data) {
|
||||
INFO("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
|
||||
INFO("ExampleAnnounceHandler: destination hash: " +
|
||||
destination_hash.toHex());
|
||||
if (announced_identity) {
|
||||
INFO("ExampleAnnounceHandler: announced identity hash: " +
|
||||
announced_identity.hash().toHex());
|
||||
INFO("ExampleAnnounceHandler: announced identity app data: " +
|
||||
announced_identity.app_data().toHex());
|
||||
}
|
||||
if (app_data) {
|
||||
INFO("ExampleAnnounceHandler: app data text: \"" + app_data.toString() +
|
||||
"\"");
|
||||
}
|
||||
INFO("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
|
||||
"!!!!!");
|
||||
}
|
||||
};
|
||||
|
||||
void onPacket(const RNS::Bytes &data, const RNS::Packet &packet) {
|
||||
INFO("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
|
||||
"!!!");
|
||||
INFO("onPacket: data: " + data.toHex());
|
||||
INFO("onPacket: text: \"" + data.toString() + "\"");
|
||||
// TRACE("onPacket: " + packet.debugString());
|
||||
INFO("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
|
||||
}
|
||||
|
||||
void onPingPacket(const RNS::Bytes &data, const RNS::Packet &packet) {
|
||||
INFO("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
|
||||
INFO("onPingPacket: data: " + data.toHex());
|
||||
INFO("onPingPacket: text: \"" + data.toString() + "\"");
|
||||
// TRACE("onPingPacket: " + packet.debugString());
|
||||
INFO("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
|
||||
}
|
||||
|
||||
RNS::Reticulum reticulum({RNS::Type::NONE});
|
||||
RNS::Interface tcp_interface(RNS::Type::NONE);
|
||||
RNS::FileSystem universal_filesystem(RNS::Type::NONE);
|
||||
RNS::Identity identity({RNS::Type::NONE});
|
||||
RNS::Destination destination({RNS::Type::NONE});
|
||||
|
||||
RNS::HAnnounceHandler announce_handler(new ExampleAnnounceHandler());
|
||||
|
||||
void reticulum_announce() {
|
||||
if (destination) {
|
||||
HEAD("Announcing destination...", RNS::LOG_TRACE);
|
||||
// destination.announce(RNS::bytesFromString(fruits[RNS::Cryptography::randomnum()
|
||||
// % 7]));
|
||||
// test path
|
||||
// destination.announce(RNS::bytesFromString(fruits[RNS::Cryptography::randomnum()
|
||||
// % 7]), true, nullptr, RNS::bytesFromString("test_tag"));
|
||||
// test packet send
|
||||
destination.announce(
|
||||
RNS::bytesFromString(fruits[RNS::Cryptography::randomnum() % 7]));
|
||||
}
|
||||
}
|
||||
void reticulum_setup() {
|
||||
INFO("Setting up Reticulum...");
|
||||
|
||||
try {
|
||||
|
||||
HEAD("Registering FileSystem with OS...", RNS::LOG_TRACE);
|
||||
universal_filesystem = new UniversalFileSystem();
|
||||
universal_filesystem.init();
|
||||
RNS::Utilities::OS::register_filesystem(universal_filesystem);
|
||||
|
||||
HEAD("Registering TCPInterface instance with Transport...", RNS::LOG_TRACE);
|
||||
tcp_interface = new TCPInterface("TCPInterface_Main");
|
||||
tcp_interface.mode(RNS::Type::Interface::MODE_GATEWAY);
|
||||
RNS::Transport::register_interface(tcp_interface);
|
||||
tcp_interface.start();
|
||||
|
||||
HEAD("Creating Reticulum instance...", RNS::LOG_TRACE);
|
||||
reticulum = RNS::Reticulum();
|
||||
reticulum.transport_enabled(true);
|
||||
reticulum.start();
|
||||
|
||||
HEAD("Creating Identity instance...", RNS::LOG_TRACE);
|
||||
identity = RNS::Identity(false);
|
||||
RNS::Bytes prv_bytes;
|
||||
#ifdef ARDUINO
|
||||
prv_bytes.assignHex(
|
||||
"78E7D93E28D55871608FF13329A226CABC3903A357388A035B360162FF6321570B092E0583772AB80BC425F99791DF5CA2CA0A985FF0415DAB419BBC64DDFAE8");
|
||||
#else
|
||||
prv_bytes.assignHex(
|
||||
"E0D43398EDC974EBA9F4A83463691A08F4D306D4E56BA6B275B8690A2FBD9852E9EBE7C03BC45CAEC9EF8E78C830037210BFB9986F6CA2DEE2B5C28D7B4DE6B0");
|
||||
#endif
|
||||
identity.load_private_key(prv_bytes);
|
||||
|
||||
HEAD("Creating Destination instance...", RNS::LOG_TRACE);
|
||||
destination =
|
||||
RNS::Destination(identity, RNS::Type::Destination::IN,
|
||||
RNS::Type::Destination::SINGLE, "app", "aspects");
|
||||
|
||||
HEAD("Registering packet callback with Destination...", RNS::LOG_TRACE);
|
||||
destination.set_packet_callback(onPacket);
|
||||
destination.set_proof_strategy(RNS::Type::Destination::PROVE_ALL);
|
||||
|
||||
{
|
||||
HEAD("Creating PING Destination instance...", RNS::LOG_TRACE);
|
||||
RNS::Destination ping_destination(identity, RNS::Type::Destination::IN,
|
||||
RNS::Type::Destination::SINGLE,
|
||||
"example_utilities", "echo.request");
|
||||
|
||||
HEAD("Registering packet callback with PING Destination...",
|
||||
RNS::LOG_TRACE);
|
||||
ping_destination.set_packet_callback(onPingPacket);
|
||||
ping_destination.set_proof_strategy(RNS::Type::Destination::PROVE_ALL);
|
||||
}
|
||||
|
||||
HEAD("Registering announce handler with Transport...", RNS::LOG_TRACE);
|
||||
RNS::Transport::register_announce_handler(announce_handler);
|
||||
|
||||
HEAD("Ready!", RNS::LOG_TRACE);
|
||||
} catch (std::exception &e) {
|
||||
ERRORF("!!! Exception in reticulum_setup: %s", e.what());
|
||||
}
|
||||
}
|
||||
void reticulum_teardown() {
|
||||
INFO("Tearing down Reticulum...");
|
||||
|
||||
RNS::Transport::persist_data();
|
||||
|
||||
try {
|
||||
|
||||
HEAD("Deregistering announce handler with Transport...", RNS::LOG_TRACE);
|
||||
RNS::Transport::deregister_announce_handler(announce_handler);
|
||||
|
||||
HEAD("Deregistering Interface instances with Transport...", RNS::LOG_TRACE);
|
||||
RNS::Transport::deregister_interface(tcp_interface);
|
||||
|
||||
} catch (std::exception &e) {
|
||||
ERRORF("!!! Exception in reticulum_teardown: %s", e.what());
|
||||
}
|
||||
}
|
||||
|
||||
void setup() {
|
||||
RNS::loglevel(RNS::LOG_TRACE);
|
||||
reticulum_setup();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
reticulum.loop();
|
||||
tcp_interface.loop();
|
||||
|
||||
#ifdef ARDUINO
|
||||
/*
|
||||
if ((RNS::Utilities::OS::time() - last_announce) > 10) {
|
||||
reticulum_announce();
|
||||
last_announce = RNS::Utilities::OS::time();
|
||||
}
|
||||
*/
|
||||
#endif
|
||||
if (send_announce) {
|
||||
reticulum_announce();
|
||||
send_announce = false;
|
||||
}
|
||||
}
|
||||
|
||||
#ifndef ARDUINO
|
||||
int getch() {
|
||||
termios oldt;
|
||||
termios newt;
|
||||
tcgetattr(STDIN_FILENO, &oldt);
|
||||
newt = oldt;
|
||||
newt.c_lflag &= ~(ICANON | ECHO);
|
||||
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
|
||||
fcntl(STDIN_FILENO, F_SETFL, fcntl(STDIN_FILENO, F_GETFL) | O_NONBLOCK);
|
||||
int ch = getchar();
|
||||
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
|
||||
return ch;
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
printf("Hello from Native on PlatformIO!\n");
|
||||
|
||||
setup();
|
||||
|
||||
bool run = true;
|
||||
while (run) {
|
||||
loop();
|
||||
int ch = getch();
|
||||
if (ch > 0) {
|
||||
switch (ch) {
|
||||
case 'a':
|
||||
reticulum_announce();
|
||||
break;
|
||||
case 'q':
|
||||
run = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
RNS::Utilities::OS::sleep(0.01);
|
||||
}
|
||||
|
||||
reticulum_teardown();
|
||||
|
||||
printf("Goodbye from Native on PlatformIO!\n");
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,178 @@
|
||||
; 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_dir = .
|
||||
|
||||
[env]
|
||||
monitor_speed = 115200
|
||||
upload_speed = 460800
|
||||
build_type = debug
|
||||
build_flags =
|
||||
;-DRNS_MEM_LOG
|
||||
lib_deps =
|
||||
ArduinoJson@^7.4.2
|
||||
MsgPack@^0.4.2
|
||||
https://github.com/attermann/Crypto.git
|
||||
microReticulum=symlink://../..
|
||||
universal_filesystem=symlink://../common/universal_filesystem
|
||||
tcp_interface=symlink://../common/tcp_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: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
|
||||
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
|
||||
-DMSGPACK_USE_BOOST=OFF
|
||||
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
|
||||
-DMSGPACK_USE_BOOST=OFF
|
||||
lib_deps =
|
||||
${env.lib_deps}
|
||||
monitor_filters = esp32_exception_decoder
|
||||
|
||||
[env:lilygo_tbeam_supreme]
|
||||
framework = arduino
|
||||
platform = espressif32
|
||||
board = t-beams3-supreme
|
||||
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
|
||||
-DMSGPACK_USE_BOOST=OFF
|
||||
lib_deps =
|
||||
${env.lib_deps}
|
||||
monitor_filters = esp32_exception_decoder
|
||||
|
||||
[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
|
||||
Reference in new issue
Block a user