mirror of https://github.com/meshcore-dev/MeshCore
82 changed files with 5209 additions and 2053 deletions
@ -0,0 +1,38 @@ |
|||||
|
/* Linker script to configure memory regions. */ |
||||
|
|
||||
|
SEARCH_DIR(.) |
||||
|
GROUP(-lgcc -lc -lnosys) |
||||
|
|
||||
|
MEMORY |
||||
|
{ |
||||
|
FLASH (rx) : ORIGIN = 0x26000, LENGTH = 0xD4000 - 0x26000 |
||||
|
|
||||
|
/* SRAM required by Softdevice depend on |
||||
|
* - Attribute Table Size (Number of Services and Characteristics) |
||||
|
* - Vendor UUID count |
||||
|
* - Max ATT MTU |
||||
|
* - Concurrent connection peripheral + central + secure links |
||||
|
* - Event Len, HVN queue, Write CMD queue |
||||
|
*/ |
||||
|
RAM (rwx) : ORIGIN = 0x20006000, LENGTH = 0x20040000 - 0x20006000 |
||||
|
} |
||||
|
|
||||
|
SECTIONS |
||||
|
{ |
||||
|
. = ALIGN(4); |
||||
|
.svc_data : |
||||
|
{ |
||||
|
PROVIDE(__start_svc_data = .); |
||||
|
KEEP(*(.svc_data)) |
||||
|
PROVIDE(__stop_svc_data = .); |
||||
|
} > RAM |
||||
|
|
||||
|
.fs_data : |
||||
|
{ |
||||
|
PROVIDE(__start_fs_data = .); |
||||
|
KEEP(*(.fs_data)) |
||||
|
PROVIDE(__stop_fs_data = .); |
||||
|
} > RAM |
||||
|
} INSERT AFTER .data; |
||||
|
|
||||
|
INCLUDE "nrf52_common.ld" |
||||
@ -0,0 +1,38 @@ |
|||||
|
/* Linker script to configure memory regions. */ |
||||
|
|
||||
|
SEARCH_DIR(.) |
||||
|
GROUP(-lgcc -lc -lnosys) |
||||
|
|
||||
|
MEMORY |
||||
|
{ |
||||
|
FLASH (rx) : ORIGIN = 0x27000, LENGTH = 0xD4000 - 0x27000 |
||||
|
|
||||
|
/* SRAM required by Softdevice depend on |
||||
|
* - Attribute Table Size (Number of Services and Characteristics) |
||||
|
* - Vendor UUID count |
||||
|
* - Max ATT MTU |
||||
|
* - Concurrent connection peripheral + central + secure links |
||||
|
* - Event Len, HVN queue, Write CMD queue |
||||
|
*/ |
||||
|
RAM (rwx) : ORIGIN = 0x20006000, LENGTH = 0x20040000 - 0x20006000 |
||||
|
} |
||||
|
|
||||
|
SECTIONS |
||||
|
{ |
||||
|
. = ALIGN(4); |
||||
|
.svc_data : |
||||
|
{ |
||||
|
PROVIDE(__start_svc_data = .); |
||||
|
KEEP(*(.svc_data)) |
||||
|
PROVIDE(__stop_svc_data = .); |
||||
|
} > RAM |
||||
|
|
||||
|
.fs_data : |
||||
|
{ |
||||
|
PROVIDE(__start_fs_data = .); |
||||
|
KEEP(*(.fs_data)) |
||||
|
PROVIDE(__stop_fs_data = .); |
||||
|
} > RAM |
||||
|
} INSERT AFTER .data; |
||||
|
|
||||
|
INCLUDE "nrf52_common.ld" |
||||
@ -0,0 +1,701 @@ |
|||||
|
#include "MyMesh.h" |
||||
|
|
||||
|
/* ------------------------------ Config -------------------------------- */ |
||||
|
|
||||
|
#ifndef LORA_FREQ |
||||
|
#define LORA_FREQ 915.0 |
||||
|
#endif |
||||
|
#ifndef LORA_BW |
||||
|
#define LORA_BW 250 |
||||
|
#endif |
||||
|
#ifndef LORA_SF |
||||
|
#define LORA_SF 10 |
||||
|
#endif |
||||
|
#ifndef LORA_CR |
||||
|
#define LORA_CR 5 |
||||
|
#endif |
||||
|
#ifndef LORA_TX_POWER |
||||
|
#define LORA_TX_POWER 20 |
||||
|
#endif |
||||
|
|
||||
|
#ifndef ADVERT_NAME |
||||
|
#define ADVERT_NAME "repeater" |
||||
|
#endif |
||||
|
#ifndef ADVERT_LAT |
||||
|
#define ADVERT_LAT 0.0 |
||||
|
#endif |
||||
|
#ifndef ADVERT_LON |
||||
|
#define ADVERT_LON 0.0 |
||||
|
#endif |
||||
|
|
||||
|
#ifndef ADMIN_PASSWORD |
||||
|
#define ADMIN_PASSWORD "password" |
||||
|
#endif |
||||
|
|
||||
|
#ifndef SERVER_RESPONSE_DELAY |
||||
|
#define SERVER_RESPONSE_DELAY 300 |
||||
|
#endif |
||||
|
|
||||
|
#ifndef TXT_ACK_DELAY |
||||
|
#define TXT_ACK_DELAY 200 |
||||
|
#endif |
||||
|
|
||||
|
#define REQ_TYPE_GET_STATUS 0x01 // same as _GET_STATS
|
||||
|
#define REQ_TYPE_KEEP_ALIVE 0x02 |
||||
|
#define REQ_TYPE_GET_TELEMETRY_DATA 0x03 |
||||
|
|
||||
|
#define RESP_SERVER_LOGIN_OK 0 // response to ANON_REQ
|
||||
|
|
||||
|
#define CLI_REPLY_DELAY_MILLIS 600 |
||||
|
|
||||
|
|
||||
|
ClientInfo *MyMesh::putClient(const mesh::Identity &id) { |
||||
|
uint32_t min_time = 0xFFFFFFFF; |
||||
|
ClientInfo *oldest = &known_clients[0]; |
||||
|
for (int i = 0; i < MAX_CLIENTS; i++) { |
||||
|
if (known_clients[i].last_activity < min_time) { |
||||
|
oldest = &known_clients[i]; |
||||
|
min_time = oldest->last_activity; |
||||
|
} |
||||
|
if (id.matches(known_clients[i].id)) return &known_clients[i]; // already known
|
||||
|
} |
||||
|
|
||||
|
oldest->id = id; |
||||
|
oldest->out_path_len = -1; // initially out_path is unknown
|
||||
|
oldest->last_timestamp = 0; |
||||
|
return oldest; |
||||
|
} |
||||
|
|
||||
|
void MyMesh::putNeighbour(const mesh::Identity &id, uint32_t timestamp, float snr) { |
||||
|
#if MAX_NEIGHBOURS // check if neighbours enabled
|
||||
|
// find existing neighbour, else use least recently updated
|
||||
|
uint32_t oldest_timestamp = 0xFFFFFFFF; |
||||
|
NeighbourInfo *neighbour = &neighbours[0]; |
||||
|
for (int i = 0; i < MAX_NEIGHBOURS; i++) { |
||||
|
// if neighbour already known, we should update it
|
||||
|
if (id.matches(neighbours[i].id)) { |
||||
|
neighbour = &neighbours[i]; |
||||
|
break; |
||||
|
} |
||||
|
|
||||
|
// otherwise we should update the least recently updated neighbour
|
||||
|
if (neighbours[i].heard_timestamp < oldest_timestamp) { |
||||
|
neighbour = &neighbours[i]; |
||||
|
oldest_timestamp = neighbour->heard_timestamp; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// update neighbour info
|
||||
|
neighbour->id = id; |
||||
|
neighbour->advert_timestamp = timestamp; |
||||
|
neighbour->heard_timestamp = getRTCClock()->getCurrentTime(); |
||||
|
neighbour->snr = (int8_t)(snr * 4); |
||||
|
#endif |
||||
|
} |
||||
|
|
||||
|
int MyMesh::handleRequest(ClientInfo *sender, uint32_t sender_timestamp, uint8_t *payload, |
||||
|
size_t payload_len) { |
||||
|
// uint32_t now = getRTCClock()->getCurrentTimeUnique();
|
||||
|
// memcpy(reply_data, &now, 4); // response packets always prefixed with timestamp
|
||||
|
memcpy(reply_data, &sender_timestamp, |
||||
|
4); // reflect sender_timestamp back in response packet (kind of like a 'tag')
|
||||
|
|
||||
|
switch (payload[0]) { |
||||
|
case REQ_TYPE_GET_STATUS: { // guests can also access this now
|
||||
|
RepeaterStats stats; |
||||
|
stats.batt_milli_volts = board.getBattMilliVolts(); |
||||
|
stats.curr_tx_queue_len = _mgr->getOutboundCount(0xFFFFFFFF); |
||||
|
stats.noise_floor = (int16_t)_radio->getNoiseFloor(); |
||||
|
stats.last_rssi = (int16_t)radio_driver.getLastRSSI(); |
||||
|
stats.n_packets_recv = radio_driver.getPacketsRecv(); |
||||
|
stats.n_packets_sent = radio_driver.getPacketsSent(); |
||||
|
stats.total_air_time_secs = getTotalAirTime() / 1000; |
||||
|
stats.total_up_time_secs = _ms->getMillis() / 1000; |
||||
|
stats.n_sent_flood = getNumSentFlood(); |
||||
|
stats.n_sent_direct = getNumSentDirect(); |
||||
|
stats.n_recv_flood = getNumRecvFlood(); |
||||
|
stats.n_recv_direct = getNumRecvDirect(); |
||||
|
stats.err_events = _err_flags; |
||||
|
stats.last_snr = (int16_t)(radio_driver.getLastSNR() * 4); |
||||
|
stats.n_direct_dups = ((SimpleMeshTables *)getTables())->getNumDirectDups(); |
||||
|
stats.n_flood_dups = ((SimpleMeshTables *)getTables())->getNumFloodDups(); |
||||
|
stats.total_rx_air_time_secs = getReceiveAirTime() / 1000; |
||||
|
|
||||
|
memcpy(&reply_data[4], &stats, sizeof(stats)); |
||||
|
|
||||
|
return 4 + sizeof(stats); // reply_len
|
||||
|
} |
||||
|
case REQ_TYPE_GET_TELEMETRY_DATA: { |
||||
|
uint8_t perm_mask = ~(payload[1]); // NEW: first reserved byte (of 4), is now inverse mask to apply to permissions
|
||||
|
|
||||
|
telemetry.reset(); |
||||
|
telemetry.addVoltage(TELEM_CHANNEL_SELF, (float)board.getBattMilliVolts() / 1000.0f); |
||||
|
// query other sensors -- target specific
|
||||
|
sensors.querySensors((sender->is_admin ? 0xFF : 0x00) & perm_mask, telemetry); |
||||
|
|
||||
|
uint8_t tlen = telemetry.getSize(); |
||||
|
memcpy(&reply_data[4], telemetry.getBuffer(), tlen); |
||||
|
return 4 + tlen; // reply_len
|
||||
|
} |
||||
|
} |
||||
|
return 0; // unknown command
|
||||
|
} |
||||
|
|
||||
|
mesh::Packet *MyMesh::createSelfAdvert() { |
||||
|
uint8_t app_data[MAX_ADVERT_DATA_SIZE]; |
||||
|
uint8_t app_data_len; |
||||
|
{ |
||||
|
AdvertDataBuilder builder(ADV_TYPE_REPEATER, _prefs.node_name, _prefs.node_lat, _prefs.node_lon); |
||||
|
app_data_len = builder.encodeTo(app_data); |
||||
|
} |
||||
|
|
||||
|
return createAdvert(self_id, app_data, app_data_len); |
||||
|
} |
||||
|
|
||||
|
File MyMesh::openAppend(const char *fname) { |
||||
|
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM) |
||||
|
return _fs->open(fname, FILE_O_WRITE); |
||||
|
#elif defined(RP2040_PLATFORM) |
||||
|
return _fs->open(fname, "a"); |
||||
|
#else |
||||
|
return _fs->open(fname, "a", true); |
||||
|
#endif |
||||
|
} |
||||
|
|
||||
|
bool MyMesh::allowPacketForward(const mesh::Packet *packet) { |
||||
|
if (_prefs.disable_fwd) return false; |
||||
|
if (packet->isRouteFlood() && packet->path_len >= _prefs.flood_max) return false; |
||||
|
return true; |
||||
|
} |
||||
|
|
||||
|
const char *MyMesh::getLogDateTime() { |
||||
|
static char tmp[32]; |
||||
|
uint32_t now = getRTCClock()->getCurrentTime(); |
||||
|
DateTime dt = DateTime(now); |
||||
|
sprintf(tmp, "%02d:%02d:%02d - %d/%d/%d U", dt.hour(), dt.minute(), dt.second(), dt.day(), dt.month(), |
||||
|
dt.year()); |
||||
|
return tmp; |
||||
|
} |
||||
|
|
||||
|
void MyMesh::logRxRaw(float snr, float rssi, const uint8_t raw[], int len) { |
||||
|
#if MESH_PACKET_LOGGING |
||||
|
Serial.print(getLogDateTime()); |
||||
|
Serial.print(" RAW: "); |
||||
|
mesh::Utils::printHex(Serial, raw, len); |
||||
|
Serial.println(); |
||||
|
#endif |
||||
|
} |
||||
|
|
||||
|
void MyMesh::logRx(mesh::Packet *pkt, int len, float score) { |
||||
|
if (_logging) { |
||||
|
File f = openAppend(PACKET_LOG_FILE); |
||||
|
if (f) { |
||||
|
f.print(getLogDateTime()); |
||||
|
f.printf(": RX, len=%d (type=%d, route=%s, payload_len=%d) SNR=%d RSSI=%d score=%d", len, |
||||
|
pkt->getPayloadType(), pkt->isRouteDirect() ? "D" : "F", pkt->payload_len, |
||||
|
(int)_radio->getLastSNR(), (int)_radio->getLastRSSI(), (int)(score * 1000)); |
||||
|
|
||||
|
if (pkt->getPayloadType() == PAYLOAD_TYPE_PATH || pkt->getPayloadType() == PAYLOAD_TYPE_REQ || |
||||
|
pkt->getPayloadType() == PAYLOAD_TYPE_RESPONSE || pkt->getPayloadType() == PAYLOAD_TYPE_TXT_MSG) { |
||||
|
f.printf(" [%02X -> %02X]\n", (uint32_t)pkt->payload[1], (uint32_t)pkt->payload[0]); |
||||
|
} else { |
||||
|
f.printf("\n"); |
||||
|
} |
||||
|
f.close(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
void MyMesh::logTx(mesh::Packet *pkt, int len) { |
||||
|
#ifdef WITH_BRIDGE |
||||
|
bridge.onPacketTransmitted(pkt); |
||||
|
#endif |
||||
|
if (_logging) { |
||||
|
File f = openAppend(PACKET_LOG_FILE); |
||||
|
if (f) { |
||||
|
f.print(getLogDateTime()); |
||||
|
f.printf(": TX, len=%d (type=%d, route=%s, payload_len=%d)", len, pkt->getPayloadType(), |
||||
|
pkt->isRouteDirect() ? "D" : "F", pkt->payload_len); |
||||
|
|
||||
|
if (pkt->getPayloadType() == PAYLOAD_TYPE_PATH || pkt->getPayloadType() == PAYLOAD_TYPE_REQ || |
||||
|
pkt->getPayloadType() == PAYLOAD_TYPE_RESPONSE || pkt->getPayloadType() == PAYLOAD_TYPE_TXT_MSG) { |
||||
|
f.printf(" [%02X -> %02X]\n", (uint32_t)pkt->payload[1], (uint32_t)pkt->payload[0]); |
||||
|
} else { |
||||
|
f.printf("\n"); |
||||
|
} |
||||
|
f.close(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
void MyMesh::logTxFail(mesh::Packet *pkt, int len) { |
||||
|
if (_logging) { |
||||
|
File f = openAppend(PACKET_LOG_FILE); |
||||
|
if (f) { |
||||
|
f.print(getLogDateTime()); |
||||
|
f.printf(": TX FAIL!, len=%d (type=%d, route=%s, payload_len=%d)\n", len, pkt->getPayloadType(), |
||||
|
pkt->isRouteDirect() ? "D" : "F", pkt->payload_len); |
||||
|
f.close(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
int MyMesh::calcRxDelay(float score, uint32_t air_time) const { |
||||
|
if (_prefs.rx_delay_base <= 0.0f) return 0; |
||||
|
return (int)((pow(_prefs.rx_delay_base, 0.85f - score) - 1.0) * air_time); |
||||
|
} |
||||
|
|
||||
|
uint32_t MyMesh::getRetransmitDelay(const mesh::Packet *packet) { |
||||
|
uint32_t t = (_radio->getEstAirtimeFor(packet->path_len + packet->payload_len + 2) * _prefs.tx_delay_factor); |
||||
|
return getRNG()->nextInt(0, 6) * t; |
||||
|
} |
||||
|
uint32_t MyMesh::getDirectRetransmitDelay(const mesh::Packet *packet) { |
||||
|
uint32_t t = (_radio->getEstAirtimeFor(packet->path_len + packet->payload_len + 2) * _prefs.direct_tx_delay_factor); |
||||
|
return getRNG()->nextInt(0, 6) * t; |
||||
|
} |
||||
|
|
||||
|
void MyMesh::onAnonDataRecv(mesh::Packet *packet, const uint8_t *secret, const mesh::Identity &sender, |
||||
|
uint8_t *data, size_t len) { |
||||
|
if (packet->getPayloadType() == PAYLOAD_TYPE_ANON_REQ) { // received an initial request by a possible admin
|
||||
|
// client (unknown at this stage)
|
||||
|
uint32_t timestamp; |
||||
|
memcpy(×tamp, data, 4); |
||||
|
|
||||
|
bool is_admin; |
||||
|
data[len] = 0; // ensure null terminator
|
||||
|
if (strcmp((char *)&data[4], _prefs.password) == 0) { // check for valid password
|
||||
|
is_admin = true; |
||||
|
} else if (strcmp((char *)&data[4], _prefs.guest_password) == 0) { // check guest password
|
||||
|
is_admin = false; |
||||
|
} else { |
||||
|
#if MESH_DEBUG |
||||
|
MESH_DEBUG_PRINTLN("Invalid password: %s", &data[4]); |
||||
|
#endif |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
auto client = putClient(sender); // add to known clients (if not already known)
|
||||
|
if (timestamp <= client->last_timestamp) { |
||||
|
MESH_DEBUG_PRINTLN("Possible login replay attack!"); |
||||
|
return; // FATAL: client table is full -OR- replay attack
|
||||
|
} |
||||
|
|
||||
|
MESH_DEBUG_PRINTLN("Login success!"); |
||||
|
client->last_timestamp = timestamp; |
||||
|
client->last_activity = getRTCClock()->getCurrentTime(); |
||||
|
client->is_admin = is_admin; |
||||
|
memcpy(client->secret, secret, PUB_KEY_SIZE); |
||||
|
|
||||
|
uint32_t now = getRTCClock()->getCurrentTimeUnique(); |
||||
|
memcpy(reply_data, &now, 4); // response packets always prefixed with timestamp
|
||||
|
#if 0 |
||||
|
memcpy(&reply_data[4], "OK", 2); // legacy response
|
||||
|
#else |
||||
|
reply_data[4] = RESP_SERVER_LOGIN_OK; |
||||
|
reply_data[5] = 0; // NEW: recommended keep-alive interval (secs / 16)
|
||||
|
reply_data[6] = is_admin ? 1 : 0; |
||||
|
reply_data[7] = 0; // FUTURE: reserved
|
||||
|
getRNG()->random(&reply_data[8], 4); // random blob to help packet-hash uniqueness
|
||||
|
#endif |
||||
|
|
||||
|
if (packet->isRouteFlood()) { |
||||
|
// let this sender know path TO here, so they can use sendDirect(), and ALSO encode the response
|
||||
|
mesh::Packet *path = createPathReturn(sender, client->secret, packet->path, packet->path_len, |
||||
|
PAYLOAD_TYPE_RESPONSE, reply_data, 12); |
||||
|
if (path) sendFlood(path, SERVER_RESPONSE_DELAY); |
||||
|
} else { |
||||
|
mesh::Packet *reply = createDatagram(PAYLOAD_TYPE_RESPONSE, sender, client->secret, reply_data, 12); |
||||
|
if (reply) { |
||||
|
if (client->out_path_len >= 0) { // we have an out_path, so send DIRECT
|
||||
|
sendDirect(reply, client->out_path, client->out_path_len, SERVER_RESPONSE_DELAY); |
||||
|
} else { |
||||
|
sendFlood(reply, SERVER_RESPONSE_DELAY); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
int MyMesh::searchPeersByHash(const uint8_t *hash) { |
||||
|
int n = 0; |
||||
|
for (int i = 0; i < MAX_CLIENTS; i++) { |
||||
|
if (known_clients[i].id.isHashMatch(hash)) { |
||||
|
matching_peer_indexes[n++] = i; // store the INDEXES of matching contacts (for subsequent 'peer' methods)
|
||||
|
} |
||||
|
} |
||||
|
return n; |
||||
|
} |
||||
|
|
||||
|
void MyMesh::getPeerSharedSecret(uint8_t *dest_secret, int peer_idx) { |
||||
|
int i = matching_peer_indexes[peer_idx]; |
||||
|
if (i >= 0 && i < MAX_CLIENTS) { |
||||
|
// lookup pre-calculated shared_secret
|
||||
|
memcpy(dest_secret, known_clients[i].secret, PUB_KEY_SIZE); |
||||
|
} else { |
||||
|
MESH_DEBUG_PRINTLN("getPeerSharedSecret: Invalid peer idx: %d", i); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
void MyMesh::onAdvertRecv(mesh::Packet *packet, const mesh::Identity &id, uint32_t timestamp, |
||||
|
const uint8_t *app_data, size_t app_data_len) { |
||||
|
mesh::Mesh::onAdvertRecv(packet, id, timestamp, app_data, app_data_len); // chain to super impl
|
||||
|
|
||||
|
// if this a zero hop advert, add it to neighbours
|
||||
|
if (packet->path_len == 0) { |
||||
|
AdvertDataParser parser(app_data, app_data_len); |
||||
|
if (parser.isValid() && parser.getType() == ADV_TYPE_REPEATER) { // just keep neigbouring Repeaters
|
||||
|
putNeighbour(id, timestamp, packet->getSNR()); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
void MyMesh::onPeerDataRecv(mesh::Packet *packet, uint8_t type, int sender_idx, const uint8_t *secret, |
||||
|
uint8_t *data, size_t len) { |
||||
|
int i = matching_peer_indexes[sender_idx]; |
||||
|
if (i < 0 || |
||||
|
i >= MAX_CLIENTS) { // get from our known_clients table (sender SHOULD already be known in this context)
|
||||
|
MESH_DEBUG_PRINTLN("onPeerDataRecv: invalid peer idx: %d", i); |
||||
|
return; |
||||
|
} |
||||
|
auto client = &known_clients[i]; |
||||
|
if (type == PAYLOAD_TYPE_REQ) { // request (from a Known admin client!)
|
||||
|
uint32_t timestamp; |
||||
|
memcpy(×tamp, data, 4); |
||||
|
|
||||
|
if (timestamp > client->last_timestamp) { // prevent replay attacks
|
||||
|
int reply_len = handleRequest(client, timestamp, &data[4], len - 4); |
||||
|
if (reply_len == 0) return; // invalid command
|
||||
|
|
||||
|
client->last_timestamp = timestamp; |
||||
|
client->last_activity = getRTCClock()->getCurrentTime(); |
||||
|
|
||||
|
if (packet->isRouteFlood()) { |
||||
|
// let this sender know path TO here, so they can use sendDirect(), and ALSO encode the response
|
||||
|
mesh::Packet *path = createPathReturn(client->id, secret, packet->path, packet->path_len, |
||||
|
PAYLOAD_TYPE_RESPONSE, reply_data, reply_len); |
||||
|
if (path) sendFlood(path, SERVER_RESPONSE_DELAY); |
||||
|
} else { |
||||
|
mesh::Packet *reply = |
||||
|
createDatagram(PAYLOAD_TYPE_RESPONSE, client->id, secret, reply_data, reply_len); |
||||
|
if (reply) { |
||||
|
if (client->out_path_len >= 0) { // we have an out_path, so send DIRECT
|
||||
|
sendDirect(reply, client->out_path, client->out_path_len, SERVER_RESPONSE_DELAY); |
||||
|
} else { |
||||
|
sendFlood(reply, SERVER_RESPONSE_DELAY); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} else { |
||||
|
MESH_DEBUG_PRINTLN("onPeerDataRecv: possible replay attack detected"); |
||||
|
} |
||||
|
} else if (type == PAYLOAD_TYPE_TXT_MSG && len > 5 && client->is_admin) { // a CLI command
|
||||
|
uint32_t sender_timestamp; |
||||
|
memcpy(&sender_timestamp, data, 4); // timestamp (by sender's RTC clock - which could be wrong)
|
||||
|
uint flags = (data[4] >> 2); // message attempt number, and other flags
|
||||
|
|
||||
|
if (!(flags == TXT_TYPE_PLAIN || flags == TXT_TYPE_CLI_DATA)) { |
||||
|
MESH_DEBUG_PRINTLN("onPeerDataRecv: unsupported text type received: flags=%02x", (uint32_t)flags); |
||||
|
} else if (sender_timestamp >= client->last_timestamp) { // prevent replay attacks
|
||||
|
bool is_retry = (sender_timestamp == client->last_timestamp); |
||||
|
client->last_timestamp = sender_timestamp; |
||||
|
client->last_activity = getRTCClock()->getCurrentTime(); |
||||
|
|
||||
|
// len can be > original length, but 'text' will be padded with zeroes
|
||||
|
data[len] = 0; // need to make a C string again, with null terminator
|
||||
|
|
||||
|
if (flags == TXT_TYPE_PLAIN) { // for legacy CLI, send Acks
|
||||
|
uint32_t ack_hash; // calc truncated hash of the message timestamp + text + sender pub_key, to prove
|
||||
|
// to sender that we got it
|
||||
|
mesh::Utils::sha256((uint8_t *)&ack_hash, 4, data, 5 + strlen((char *)&data[5]), client->id.pub_key, |
||||
|
PUB_KEY_SIZE); |
||||
|
|
||||
|
mesh::Packet *ack = createAck(ack_hash); |
||||
|
if (ack) { |
||||
|
if (client->out_path_len < 0) { |
||||
|
sendFlood(ack, TXT_ACK_DELAY); |
||||
|
} else { |
||||
|
sendDirect(ack, client->out_path, client->out_path_len, TXT_ACK_DELAY); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
uint8_t temp[166]; |
||||
|
char *command = (char *)&data[5]; |
||||
|
char *reply = (char *)&temp[5]; |
||||
|
if (is_retry) { |
||||
|
*reply = 0; |
||||
|
} else { |
||||
|
handleCommand(sender_timestamp, command, reply); |
||||
|
} |
||||
|
int text_len = strlen(reply); |
||||
|
if (text_len > 0) { |
||||
|
uint32_t timestamp = getRTCClock()->getCurrentTimeUnique(); |
||||
|
if (timestamp == sender_timestamp) { |
||||
|
// WORKAROUND: the two timestamps need to be different, in the CLI view
|
||||
|
timestamp++; |
||||
|
} |
||||
|
memcpy(temp, ×tamp, 4); // mostly an extra blob to help make packet_hash unique
|
||||
|
temp[4] = (TXT_TYPE_CLI_DATA << 2); // NOTE: legacy was: TXT_TYPE_PLAIN
|
||||
|
|
||||
|
auto reply = createDatagram(PAYLOAD_TYPE_TXT_MSG, client->id, secret, temp, 5 + text_len); |
||||
|
if (reply) { |
||||
|
if (client->out_path_len < 0) { |
||||
|
sendFlood(reply, CLI_REPLY_DELAY_MILLIS); |
||||
|
} else { |
||||
|
sendDirect(reply, client->out_path, client->out_path_len, CLI_REPLY_DELAY_MILLIS); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} else { |
||||
|
MESH_DEBUG_PRINTLN("onPeerDataRecv: possible replay attack detected"); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
bool MyMesh::onPeerPathRecv(mesh::Packet *packet, int sender_idx, const uint8_t *secret, uint8_t *path, |
||||
|
uint8_t path_len, uint8_t extra_type, uint8_t *extra, uint8_t extra_len) { |
||||
|
// TODO: prevent replay attacks
|
||||
|
int i = matching_peer_indexes[sender_idx]; |
||||
|
|
||||
|
if (i >= 0 && |
||||
|
i < MAX_CLIENTS) { // get from our known_clients table (sender SHOULD already be known in this context)
|
||||
|
MESH_DEBUG_PRINTLN("PATH to client, path_len=%d", (uint32_t)path_len); |
||||
|
auto client = &known_clients[i]; |
||||
|
memcpy(client->out_path, path, client->out_path_len = path_len); // store a copy of path, for sendDirect()
|
||||
|
} else { |
||||
|
MESH_DEBUG_PRINTLN("onPeerPathRecv: invalid peer idx: %d", i); |
||||
|
} |
||||
|
|
||||
|
// NOTE: no reciprocal path send!!
|
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
MyMesh::MyMesh(mesh::MainBoard &board, mesh::Radio &radio, mesh::MillisecondClock &ms, mesh::RNG &rng, |
||||
|
mesh::RTCClock &rtc, mesh::MeshTables &tables) |
||||
|
: mesh::Mesh(radio, ms, rng, rtc, *new StaticPoolPacketManager(32), tables), |
||||
|
_cli(board, rtc, &_prefs, this), telemetry(MAX_PACKET_PAYLOAD - 4) |
||||
|
#if defined(WITH_RS232_BRIDGE) |
||||
|
, bridge(WITH_RS232_BRIDGE, _mgr, &rtc) |
||||
|
#elif defined(WITH_ESPNOW_BRIDGE) |
||||
|
, bridge(_mgr, &rtc) |
||||
|
#endif |
||||
|
{ |
||||
|
memset(known_clients, 0, sizeof(known_clients)); |
||||
|
next_local_advert = next_flood_advert = 0; |
||||
|
set_radio_at = revert_radio_at = 0; |
||||
|
_logging = false; |
||||
|
|
||||
|
#if MAX_NEIGHBOURS |
||||
|
memset(neighbours, 0, sizeof(neighbours)); |
||||
|
#endif |
||||
|
|
||||
|
// defaults
|
||||
|
memset(&_prefs, 0, sizeof(_prefs)); |
||||
|
_prefs.airtime_factor = 1.0; // one half
|
||||
|
_prefs.rx_delay_base = 0.0f; // turn off by default, was 10.0;
|
||||
|
_prefs.tx_delay_factor = 0.5f; // was 0.25f
|
||||
|
StrHelper::strncpy(_prefs.node_name, ADVERT_NAME, sizeof(_prefs.node_name)); |
||||
|
_prefs.node_lat = ADVERT_LAT; |
||||
|
_prefs.node_lon = ADVERT_LON; |
||||
|
StrHelper::strncpy(_prefs.password, ADMIN_PASSWORD, sizeof(_prefs.password)); |
||||
|
_prefs.freq = LORA_FREQ; |
||||
|
_prefs.sf = LORA_SF; |
||||
|
_prefs.bw = LORA_BW; |
||||
|
_prefs.cr = LORA_CR; |
||||
|
_prefs.tx_power_dbm = LORA_TX_POWER; |
||||
|
_prefs.advert_interval = 1; // default to 2 minutes for NEW installs
|
||||
|
_prefs.flood_advert_interval = 12; // 12 hours
|
||||
|
_prefs.flood_max = 64; |
||||
|
_prefs.interference_threshold = 0; // disabled
|
||||
|
} |
||||
|
|
||||
|
void MyMesh::begin(FILESYSTEM *fs) { |
||||
|
mesh::Mesh::begin(); |
||||
|
_fs = fs; |
||||
|
// load persisted prefs
|
||||
|
_cli.loadPrefs(_fs); |
||||
|
|
||||
|
#ifdef WITH_BRIDGE |
||||
|
bridge.begin(); |
||||
|
#endif |
||||
|
|
||||
|
radio_set_params(_prefs.freq, _prefs.bw, _prefs.sf, _prefs.cr); |
||||
|
radio_set_tx_power(_prefs.tx_power_dbm); |
||||
|
|
||||
|
updateAdvertTimer(); |
||||
|
updateFloodAdvertTimer(); |
||||
|
} |
||||
|
|
||||
|
void MyMesh::applyTempRadioParams(float freq, float bw, uint8_t sf, uint8_t cr, int timeout_mins) { |
||||
|
set_radio_at = futureMillis(2000); // give CLI reply some time to be sent back, before applying temp radio params
|
||||
|
pending_freq = freq; |
||||
|
pending_bw = bw; |
||||
|
pending_sf = sf; |
||||
|
pending_cr = cr; |
||||
|
|
||||
|
revert_radio_at = futureMillis(2000 + timeout_mins * 60 * 1000); // schedule when to revert radio params
|
||||
|
} |
||||
|
|
||||
|
bool MyMesh::formatFileSystem() { |
||||
|
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM) |
||||
|
return InternalFS.format(); |
||||
|
#elif defined(RP2040_PLATFORM) |
||||
|
return LittleFS.format(); |
||||
|
#elif defined(ESP32) |
||||
|
return SPIFFS.format(); |
||||
|
#else |
||||
|
#error "need to implement file system erase" |
||||
|
return false; |
||||
|
#endif |
||||
|
} |
||||
|
|
||||
|
void MyMesh::sendSelfAdvertisement(int delay_millis) { |
||||
|
mesh::Packet *pkt = createSelfAdvert(); |
||||
|
if (pkt) { |
||||
|
sendFlood(pkt, delay_millis); |
||||
|
} else { |
||||
|
MESH_DEBUG_PRINTLN("ERROR: unable to create advertisement packet!"); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
void MyMesh::updateAdvertTimer() { |
||||
|
if (_prefs.advert_interval > 0) { // schedule local advert timer
|
||||
|
next_local_advert = futureMillis(((uint32_t)_prefs.advert_interval) * 2 * 60 * 1000); |
||||
|
} else { |
||||
|
next_local_advert = 0; // stop the timer
|
||||
|
} |
||||
|
} |
||||
|
|
||||
|
void MyMesh::updateFloodAdvertTimer() { |
||||
|
if (_prefs.flood_advert_interval > 0) { // schedule flood advert timer
|
||||
|
next_flood_advert = futureMillis(((uint32_t)_prefs.flood_advert_interval) * 60 * 60 * 1000); |
||||
|
} else { |
||||
|
next_flood_advert = 0; // stop the timer
|
||||
|
} |
||||
|
} |
||||
|
|
||||
|
void MyMesh::dumpLogFile() { |
||||
|
#if defined(RP2040_PLATFORM) |
||||
|
File f = _fs->open(PACKET_LOG_FILE, "r"); |
||||
|
#else |
||||
|
File f = _fs->open(PACKET_LOG_FILE); |
||||
|
#endif |
||||
|
if (f) { |
||||
|
while (f.available()) { |
||||
|
int c = f.read(); |
||||
|
if (c < 0) break; |
||||
|
Serial.print((char)c); |
||||
|
} |
||||
|
f.close(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
void MyMesh::setTxPower(uint8_t power_dbm) { |
||||
|
radio_set_tx_power(power_dbm); |
||||
|
} |
||||
|
|
||||
|
void MyMesh::formatNeighborsReply(char *reply) { |
||||
|
char *dp = reply; |
||||
|
|
||||
|
#if MAX_NEIGHBOURS |
||||
|
for (int i = 0; i < MAX_NEIGHBOURS && dp - reply < 134; i++) { |
||||
|
NeighbourInfo *neighbour = &neighbours[i]; |
||||
|
if (neighbour->heard_timestamp == 0) continue; // skip empty slots
|
||||
|
|
||||
|
// add new line if not first item
|
||||
|
if (i > 0) *dp++ = '\n'; |
||||
|
|
||||
|
char hex[10]; |
||||
|
// get 4 bytes of neighbour id as hex
|
||||
|
mesh::Utils::toHex(hex, neighbour->id.pub_key, 4); |
||||
|
|
||||
|
// add next neighbour
|
||||
|
uint32_t secs_ago = getRTCClock()->getCurrentTime() - neighbour->heard_timestamp; |
||||
|
sprintf(dp, "%s:%d:%d", hex, secs_ago, neighbour->snr); |
||||
|
while (*dp) |
||||
|
dp++; // find end of string
|
||||
|
} |
||||
|
#endif |
||||
|
if (dp == reply) { // no neighbours, need empty response
|
||||
|
strcpy(dp, "-none-"); |
||||
|
dp += 6; |
||||
|
} |
||||
|
*dp = 0; // null terminator
|
||||
|
} |
||||
|
|
||||
|
void MyMesh::removeNeighbor(const uint8_t *pubkey, int key_len) { |
||||
|
#if MAX_NEIGHBOURS |
||||
|
for (int i = 0; i < MAX_NEIGHBOURS; i++) { |
||||
|
NeighbourInfo *neighbour = &neighbours[i]; |
||||
|
if (memcmp(neighbour->id.pub_key, pubkey, key_len) == 0) { |
||||
|
neighbours[i] = NeighbourInfo(); // clear neighbour entry
|
||||
|
} |
||||
|
} |
||||
|
#endif |
||||
|
} |
||||
|
|
||||
|
void MyMesh::saveIdentity(const mesh::LocalIdentity &new_id) { |
||||
|
self_id = new_id; |
||||
|
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM) |
||||
|
IdentityStore store(*_fs, ""); |
||||
|
#elif defined(ESP32) |
||||
|
IdentityStore store(*_fs, "/identity"); |
||||
|
#elif defined(RP2040_PLATFORM) |
||||
|
IdentityStore store(*_fs, "/identity"); |
||||
|
#else |
||||
|
#error "need to define saveIdentity()" |
||||
|
#endif |
||||
|
store.save("_main", self_id); |
||||
|
} |
||||
|
|
||||
|
void MyMesh::clearStats() { |
||||
|
radio_driver.resetStats(); |
||||
|
resetStats(); |
||||
|
((SimpleMeshTables *)getTables())->resetStats(); |
||||
|
} |
||||
|
|
||||
|
void MyMesh::handleCommand(uint32_t sender_timestamp, char *command, char *reply) { |
||||
|
while (*command == ' ') |
||||
|
command++; // skip leading spaces
|
||||
|
|
||||
|
if (strlen(command) > 4 && command[2] == '|') { // optional prefix (for companion radio CLI)
|
||||
|
memcpy(reply, command, 3); // reflect the prefix back
|
||||
|
reply += 3; |
||||
|
command += 3; |
||||
|
} |
||||
|
|
||||
|
_cli.handleCommand(sender_timestamp, command, reply); // common CLI commands
|
||||
|
} |
||||
|
|
||||
|
void MyMesh::loop() { |
||||
|
#ifdef WITH_BRIDGE |
||||
|
bridge.loop(); |
||||
|
#endif |
||||
|
|
||||
|
mesh::Mesh::loop(); |
||||
|
|
||||
|
if (next_flood_advert && millisHasNowPassed(next_flood_advert)) { |
||||
|
mesh::Packet *pkt = createSelfAdvert(); |
||||
|
if (pkt) sendFlood(pkt); |
||||
|
|
||||
|
updateFloodAdvertTimer(); // schedule next flood advert
|
||||
|
updateAdvertTimer(); // also schedule local advert (so they don't overlap)
|
||||
|
} else if (next_local_advert && millisHasNowPassed(next_local_advert)) { |
||||
|
mesh::Packet *pkt = createSelfAdvert(); |
||||
|
if (pkt) sendZeroHop(pkt); |
||||
|
|
||||
|
updateAdvertTimer(); // schedule next local advert
|
||||
|
} |
||||
|
|
||||
|
if (set_radio_at && millisHasNowPassed(set_radio_at)) { // apply pending (temporary) radio params
|
||||
|
set_radio_at = 0; // clear timer
|
||||
|
radio_set_params(pending_freq, pending_bw, pending_sf, pending_cr); |
||||
|
MESH_DEBUG_PRINTLN("Temp radio params"); |
||||
|
} |
||||
|
|
||||
|
if (revert_radio_at && millisHasNowPassed(revert_radio_at)) { // revert radio params to orig
|
||||
|
revert_radio_at = 0; // clear timer
|
||||
|
radio_set_params(_prefs.freq, _prefs.bw, _prefs.sf, _prefs.cr); |
||||
|
MESH_DEBUG_PRINTLN("Radio params restored"); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,192 @@ |
|||||
|
#pragma once |
||||
|
|
||||
|
#include <Arduino.h> |
||||
|
#include <Mesh.h> |
||||
|
#include <helpers/CommonCLI.h> |
||||
|
|
||||
|
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM) |
||||
|
#include <InternalFileSystem.h> |
||||
|
#elif defined(RP2040_PLATFORM) |
||||
|
#include <LittleFS.h> |
||||
|
#elif defined(ESP32) |
||||
|
#include <SPIFFS.h> |
||||
|
#endif |
||||
|
|
||||
|
#include <helpers/ArduinoHelpers.h> |
||||
|
#include <helpers/StaticPoolPacketManager.h> |
||||
|
#include <helpers/SimpleMeshTables.h> |
||||
|
#include <helpers/IdentityStore.h> |
||||
|
#include <helpers/AdvertDataHelpers.h> |
||||
|
#include <helpers/TxtDataHelpers.h> |
||||
|
#include <RTClib.h> |
||||
|
#include <target.h> |
||||
|
|
||||
|
#ifdef WITH_RS232_BRIDGE |
||||
|
#include "helpers/bridges/RS232Bridge.h" |
||||
|
#define WITH_BRIDGE |
||||
|
#endif |
||||
|
|
||||
|
#ifdef WITH_ESPNOW_BRIDGE |
||||
|
#include "helpers/bridges/ESPNowBridge.h" |
||||
|
#define WITH_BRIDGE |
||||
|
#endif |
||||
|
|
||||
|
#ifdef WITH_BRIDGE |
||||
|
extern AbstractBridge* bridge; |
||||
|
#endif |
||||
|
|
||||
|
struct RepeaterStats { |
||||
|
uint16_t batt_milli_volts; |
||||
|
uint16_t curr_tx_queue_len; |
||||
|
int16_t noise_floor; |
||||
|
int16_t last_rssi; |
||||
|
uint32_t n_packets_recv; |
||||
|
uint32_t n_packets_sent; |
||||
|
uint32_t total_air_time_secs; |
||||
|
uint32_t total_up_time_secs; |
||||
|
uint32_t n_sent_flood, n_sent_direct; |
||||
|
uint32_t n_recv_flood, n_recv_direct; |
||||
|
uint16_t err_events; // was 'n_full_events'
|
||||
|
int16_t last_snr; // x 4
|
||||
|
uint16_t n_direct_dups, n_flood_dups; |
||||
|
uint32_t total_rx_air_time_secs; |
||||
|
}; |
||||
|
|
||||
|
struct ClientInfo { |
||||
|
mesh::Identity id; |
||||
|
uint32_t last_timestamp, last_activity; |
||||
|
uint8_t secret[PUB_KEY_SIZE]; |
||||
|
bool is_admin; |
||||
|
int8_t out_path_len; |
||||
|
uint8_t out_path[MAX_PATH_SIZE]; |
||||
|
}; |
||||
|
|
||||
|
#ifndef MAX_CLIENTS |
||||
|
#define MAX_CLIENTS 32 |
||||
|
#endif |
||||
|
|
||||
|
struct NeighbourInfo { |
||||
|
mesh::Identity id; |
||||
|
uint32_t advert_timestamp; |
||||
|
uint32_t heard_timestamp; |
||||
|
int8_t snr; // multiplied by 4, user should divide to get float value
|
||||
|
}; |
||||
|
|
||||
|
#ifndef FIRMWARE_BUILD_DATE |
||||
|
#define FIRMWARE_BUILD_DATE "1 Sep 2025" |
||||
|
#endif |
||||
|
|
||||
|
#ifndef FIRMWARE_VERSION |
||||
|
#define FIRMWARE_VERSION "v1.8.1" |
||||
|
#endif |
||||
|
|
||||
|
#define FIRMWARE_ROLE "repeater" |
||||
|
|
||||
|
#define PACKET_LOG_FILE "/packet_log" |
||||
|
|
||||
|
class MyMesh : public mesh::Mesh, public CommonCLICallbacks { |
||||
|
FILESYSTEM* _fs; |
||||
|
unsigned long next_local_advert, next_flood_advert; |
||||
|
bool _logging; |
||||
|
NodePrefs _prefs; |
||||
|
CommonCLI _cli; |
||||
|
uint8_t reply_data[MAX_PACKET_PAYLOAD]; |
||||
|
ClientInfo known_clients[MAX_CLIENTS]; |
||||
|
#if MAX_NEIGHBOURS |
||||
|
NeighbourInfo neighbours[MAX_NEIGHBOURS]; |
||||
|
#endif |
||||
|
CayenneLPP telemetry; |
||||
|
unsigned long set_radio_at, revert_radio_at; |
||||
|
float pending_freq; |
||||
|
float pending_bw; |
||||
|
uint8_t pending_sf; |
||||
|
uint8_t pending_cr; |
||||
|
int matching_peer_indexes[MAX_CLIENTS]; |
||||
|
#if defined(WITH_RS232_BRIDGE) |
||||
|
RS232Bridge bridge; |
||||
|
#elif defined(WITH_ESPNOW_BRIDGE) |
||||
|
ESPNowBridge bridge; |
||||
|
#endif |
||||
|
|
||||
|
ClientInfo* putClient(const mesh::Identity& id); |
||||
|
void putNeighbour(const mesh::Identity& id, uint32_t timestamp, float snr); |
||||
|
int handleRequest(ClientInfo* sender, uint32_t sender_timestamp, uint8_t* payload, size_t payload_len); |
||||
|
mesh::Packet* createSelfAdvert(); |
||||
|
|
||||
|
File openAppend(const char* fname); |
||||
|
|
||||
|
protected: |
||||
|
float getAirtimeBudgetFactor() const override { |
||||
|
return _prefs.airtime_factor; |
||||
|
} |
||||
|
|
||||
|
bool allowPacketForward(const mesh::Packet* packet) override; |
||||
|
const char* getLogDateTime() override; |
||||
|
void logRxRaw(float snr, float rssi, const uint8_t raw[], int len) override; |
||||
|
|
||||
|
void logRx(mesh::Packet* pkt, int len, float score) override; |
||||
|
void logTx(mesh::Packet* pkt, int len) override; |
||||
|
void logTxFail(mesh::Packet* pkt, int len) override; |
||||
|
int calcRxDelay(float score, uint32_t air_time) const override; |
||||
|
|
||||
|
uint32_t getRetransmitDelay(const mesh::Packet* packet) override; |
||||
|
uint32_t getDirectRetransmitDelay(const mesh::Packet* packet) override; |
||||
|
|
||||
|
int getInterferenceThreshold() const override { |
||||
|
return _prefs.interference_threshold; |
||||
|
} |
||||
|
int getAGCResetInterval() const override { |
||||
|
return ((int)_prefs.agc_reset_interval) * 4000; // milliseconds
|
||||
|
} |
||||
|
uint8_t getExtraAckTransmitCount() const override { |
||||
|
return _prefs.multi_acks; |
||||
|
} |
||||
|
|
||||
|
void onAnonDataRecv(mesh::Packet* packet, const uint8_t* secret, const mesh::Identity& sender, uint8_t* data, size_t len) override; |
||||
|
int searchPeersByHash(const uint8_t* hash) override; |
||||
|
void getPeerSharedSecret(uint8_t* dest_secret, int peer_idx) override; |
||||
|
void onAdvertRecv(mesh::Packet* packet, const mesh::Identity& id, uint32_t timestamp, const uint8_t* app_data, size_t app_data_len); |
||||
|
void onPeerDataRecv(mesh::Packet* packet, uint8_t type, int sender_idx, const uint8_t* secret, uint8_t* data, size_t len) override; |
||||
|
bool onPeerPathRecv(mesh::Packet* packet, int sender_idx, const uint8_t* secret, uint8_t* path, uint8_t path_len, uint8_t extra_type, uint8_t* extra, uint8_t extra_len) override; |
||||
|
|
||||
|
public: |
||||
|
MyMesh(mesh::MainBoard& board, mesh::Radio& radio, mesh::MillisecondClock& ms, mesh::RNG& rng, mesh::RTCClock& rtc, mesh::MeshTables& tables); |
||||
|
|
||||
|
void begin(FILESYSTEM* fs); |
||||
|
|
||||
|
const char* getFirmwareVer() override { return FIRMWARE_VERSION; } |
||||
|
const char* getBuildDate() override { return FIRMWARE_BUILD_DATE; } |
||||
|
const char* getRole() override { return FIRMWARE_ROLE; } |
||||
|
const char* getNodeName() { return _prefs.node_name; } |
||||
|
NodePrefs* getNodePrefs() { |
||||
|
return &_prefs; |
||||
|
} |
||||
|
|
||||
|
void savePrefs() override { |
||||
|
_cli.savePrefs(_fs); |
||||
|
} |
||||
|
|
||||
|
void applyTempRadioParams(float freq, float bw, uint8_t sf, uint8_t cr, int timeout_mins) override; |
||||
|
bool formatFileSystem() override; |
||||
|
void sendSelfAdvertisement(int delay_millis) override; |
||||
|
void updateAdvertTimer() override; |
||||
|
void updateFloodAdvertTimer() override; |
||||
|
|
||||
|
void setLoggingOn(bool enable) override { _logging = enable; } |
||||
|
|
||||
|
void eraseLogFile() override { |
||||
|
_fs->remove(PACKET_LOG_FILE); |
||||
|
} |
||||
|
|
||||
|
void dumpLogFile() override; |
||||
|
void setTxPower(uint8_t power_dbm) override; |
||||
|
void formatNeighborsReply(char *reply) override; |
||||
|
void removeNeighbor(const uint8_t* pubkey, int key_len) override; |
||||
|
|
||||
|
mesh::LocalIdentity& getSelfId() override { return self_id; } |
||||
|
|
||||
|
void saveIdentity(const mesh::LocalIdentity& new_id) override; |
||||
|
void clearStats() override; |
||||
|
void handleCommand(uint32_t sender_timestamp, char* command, char* reply); |
||||
|
void loop(); |
||||
|
}; |
||||
@ -0,0 +1,809 @@ |
|||||
|
#include "MyMesh.h" |
||||
|
|
||||
|
#define REPLY_DELAY_MILLIS 1500 |
||||
|
#define PUSH_NOTIFY_DELAY_MILLIS 2000 |
||||
|
#define SYNC_PUSH_INTERVAL 1200 |
||||
|
|
||||
|
#define PUSH_ACK_TIMEOUT_FLOOD 12000 |
||||
|
#define PUSH_TIMEOUT_BASE 4000 |
||||
|
#define PUSH_ACK_TIMEOUT_FACTOR 2000 |
||||
|
|
||||
|
#define POST_SYNC_DELAY_SECS 6 |
||||
|
|
||||
|
#define CLIENT_KEEP_ALIVE_SECS 0 // Now Disabled (was 128)
|
||||
|
|
||||
|
#define REQ_TYPE_GET_STATUS 0x01 // same as _GET_STATS
|
||||
|
#define REQ_TYPE_KEEP_ALIVE 0x02 |
||||
|
#define REQ_TYPE_GET_TELEMETRY_DATA 0x03 |
||||
|
|
||||
|
#define RESP_SERVER_LOGIN_OK 0 // response to ANON_REQ
|
||||
|
|
||||
|
struct ServerStats { |
||||
|
uint16_t batt_milli_volts; |
||||
|
uint16_t curr_tx_queue_len; |
||||
|
int16_t noise_floor; |
||||
|
int16_t last_rssi; |
||||
|
uint32_t n_packets_recv; |
||||
|
uint32_t n_packets_sent; |
||||
|
uint32_t total_air_time_secs; |
||||
|
uint32_t total_up_time_secs; |
||||
|
uint32_t n_sent_flood, n_sent_direct; |
||||
|
uint32_t n_recv_flood, n_recv_direct; |
||||
|
uint16_t err_events; // was 'n_full_events'
|
||||
|
int16_t last_snr; // x 4
|
||||
|
uint16_t n_direct_dups, n_flood_dups; |
||||
|
uint16_t n_posted, n_post_push; |
||||
|
}; |
||||
|
|
||||
|
ClientInfo *MyMesh::putClient(const mesh::Identity &id) { |
||||
|
for (int i = 0; i < num_clients; i++) { |
||||
|
if (id.matches(known_clients[i].id)) return &known_clients[i]; // already known
|
||||
|
} |
||||
|
ClientInfo *newClient; |
||||
|
if (num_clients < MAX_CLIENTS) { |
||||
|
newClient = &known_clients[num_clients++]; |
||||
|
} else { // table is currently full
|
||||
|
// evict least active client
|
||||
|
uint32_t oldest_timestamp = 0xFFFFFFFF; |
||||
|
newClient = &known_clients[0]; |
||||
|
for (int i = 0; i < num_clients; i++) { |
||||
|
auto c = &known_clients[i]; |
||||
|
if (c->last_activity < oldest_timestamp) { |
||||
|
oldest_timestamp = c->last_activity; |
||||
|
newClient = c; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
newClient->id = id; |
||||
|
newClient->out_path_len = -1; // initially out_path is unknown
|
||||
|
newClient->last_timestamp = 0; |
||||
|
return newClient; |
||||
|
} |
||||
|
|
||||
|
void MyMesh::evict(ClientInfo *client) { |
||||
|
client->last_activity = 0; // this slot will now be re-used (will be oldest)
|
||||
|
memset(client->id.pub_key, 0, sizeof(client->id.pub_key)); |
||||
|
memset(client->secret, 0, sizeof(client->secret)); |
||||
|
client->pending_ack = 0; |
||||
|
} |
||||
|
|
||||
|
void MyMesh::addPost(ClientInfo *client, const char *postData) { |
||||
|
// TODO: suggested postData format: <title>/<descrption>
|
||||
|
posts[next_post_idx].author = client->id; // add to cyclic queue
|
||||
|
StrHelper::strncpy(posts[next_post_idx].text, postData, MAX_POST_TEXT_LEN); |
||||
|
|
||||
|
posts[next_post_idx].post_timestamp = getRTCClock()->getCurrentTimeUnique(); |
||||
|
next_post_idx = (next_post_idx + 1) % MAX_UNSYNCED_POSTS; |
||||
|
|
||||
|
next_push = futureMillis(PUSH_NOTIFY_DELAY_MILLIS); |
||||
|
_num_posted++; // stats
|
||||
|
} |
||||
|
|
||||
|
void MyMesh::pushPostToClient(ClientInfo *client, PostInfo &post) { |
||||
|
int len = 0; |
||||
|
memcpy(&reply_data[len], &post.post_timestamp, 4); |
||||
|
len += 4; // this is a PAST timestamp... but should be accepted by client
|
||||
|
|
||||
|
uint8_t attempt; |
||||
|
getRNG()->random(&attempt, 1); // need this for re-tries, so packet hash (and ACK) will be different
|
||||
|
reply_data[len++] = (TXT_TYPE_SIGNED_PLAIN << 2) | (attempt & 3); // 'signed' plain text
|
||||
|
|
||||
|
// encode prefix of post.author.pub_key
|
||||
|
memcpy(&reply_data[len], post.author.pub_key, 4); |
||||
|
len += 4; // just first 4 bytes
|
||||
|
|
||||
|
int text_len = strlen(post.text); |
||||
|
memcpy(&reply_data[len], post.text, text_len); |
||||
|
len += text_len; |
||||
|
|
||||
|
// calc expected ACK reply
|
||||
|
mesh::Utils::sha256((uint8_t *)&client->pending_ack, 4, reply_data, len, client->id.pub_key, PUB_KEY_SIZE); |
||||
|
client->push_post_timestamp = post.post_timestamp; |
||||
|
|
||||
|
auto reply = createDatagram(PAYLOAD_TYPE_TXT_MSG, client->id, client->secret, reply_data, len); |
||||
|
if (reply) { |
||||
|
if (client->out_path_len < 0) { |
||||
|
sendFlood(reply); |
||||
|
client->ack_timeout = futureMillis(PUSH_ACK_TIMEOUT_FLOOD); |
||||
|
} else { |
||||
|
sendDirect(reply, client->out_path, client->out_path_len); |
||||
|
client->ack_timeout = |
||||
|
futureMillis(PUSH_TIMEOUT_BASE + PUSH_ACK_TIMEOUT_FACTOR * (client->out_path_len + 1)); |
||||
|
} |
||||
|
_num_post_pushes++; // stats
|
||||
|
} else { |
||||
|
client->pending_ack = 0; |
||||
|
MESH_DEBUG_PRINTLN("Unable to push post to client"); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
uint8_t MyMesh::getUnsyncedCount(ClientInfo *client) { |
||||
|
uint8_t count = 0; |
||||
|
for (int k = 0; k < MAX_UNSYNCED_POSTS; k++) { |
||||
|
if (posts[k].post_timestamp > client->sync_since // is new post for this Client?
|
||||
|
&& !posts[k].author.matches(client->id)) { // don't push posts to the author
|
||||
|
count++; |
||||
|
} |
||||
|
} |
||||
|
return count; |
||||
|
} |
||||
|
|
||||
|
bool MyMesh::processAck(const uint8_t *data) { |
||||
|
for (int i = 0; i < num_clients; i++) { |
||||
|
auto client = &known_clients[i]; |
||||
|
if (client->pending_ack && memcmp(data, &client->pending_ack, 4) == 0) { // got an ACK from Client!
|
||||
|
client->pending_ack = 0; // clear this, so next push can happen
|
||||
|
client->push_failures = 0; |
||||
|
client->sync_since = client->push_post_timestamp; // advance Client's SINCE timestamp, to sync next post
|
||||
|
return true; |
||||
|
} |
||||
|
} |
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
mesh::Packet *MyMesh::createSelfAdvert() { |
||||
|
uint8_t app_data[MAX_ADVERT_DATA_SIZE]; |
||||
|
uint8_t app_data_len; |
||||
|
{ |
||||
|
AdvertDataBuilder builder(ADV_TYPE_ROOM, _prefs.node_name, _prefs.node_lat, _prefs.node_lon); |
||||
|
app_data_len = builder.encodeTo(app_data); |
||||
|
} |
||||
|
|
||||
|
return createAdvert(self_id, app_data, app_data_len); |
||||
|
} |
||||
|
|
||||
|
File MyMesh::openAppend(const char *fname) { |
||||
|
#if defined(NRF52_PLATFORM) |
||||
|
return _fs->open(fname, FILE_O_WRITE); |
||||
|
#elif defined(RP2040_PLATFORM) |
||||
|
return _fs->open(fname, "a"); |
||||
|
#else |
||||
|
return _fs->open(fname, "a", true); |
||||
|
#endif |
||||
|
} |
||||
|
|
||||
|
int MyMesh::handleRequest(ClientInfo *sender, uint32_t sender_timestamp, uint8_t *payload, |
||||
|
size_t payload_len) { |
||||
|
// uint32_t now = getRTCClock()->getCurrentTimeUnique();
|
||||
|
// memcpy(reply_data, &now, 4); // response packets always prefixed with timestamp
|
||||
|
memcpy(reply_data, &sender_timestamp, 4); // reflect sender_timestamp back in response packet (kind of like a 'tag')
|
||||
|
|
||||
|
switch (payload[0]) { |
||||
|
case REQ_TYPE_GET_STATUS: { |
||||
|
ServerStats stats; |
||||
|
stats.batt_milli_volts = board.getBattMilliVolts(); |
||||
|
stats.curr_tx_queue_len = _mgr->getOutboundCount(0xFFFFFFFF); |
||||
|
stats.noise_floor = (int16_t)_radio->getNoiseFloor(); |
||||
|
stats.last_rssi = (int16_t)radio_driver.getLastRSSI(); |
||||
|
stats.n_packets_recv = radio_driver.getPacketsRecv(); |
||||
|
stats.n_packets_sent = radio_driver.getPacketsSent(); |
||||
|
stats.total_air_time_secs = getTotalAirTime() / 1000; |
||||
|
stats.total_up_time_secs = _ms->getMillis() / 1000; |
||||
|
stats.n_sent_flood = getNumSentFlood(); |
||||
|
stats.n_sent_direct = getNumSentDirect(); |
||||
|
stats.n_recv_flood = getNumRecvFlood(); |
||||
|
stats.n_recv_direct = getNumRecvDirect(); |
||||
|
stats.err_events = _err_flags; |
||||
|
stats.last_snr = (int16_t)(radio_driver.getLastSNR() * 4); |
||||
|
stats.n_direct_dups = ((SimpleMeshTables *)getTables())->getNumDirectDups(); |
||||
|
stats.n_flood_dups = ((SimpleMeshTables *)getTables())->getNumFloodDups(); |
||||
|
stats.n_posted = _num_posted; |
||||
|
stats.n_post_push = _num_post_pushes; |
||||
|
|
||||
|
memcpy(&reply_data[4], &stats, sizeof(stats)); |
||||
|
return 4 + sizeof(stats); |
||||
|
} |
||||
|
|
||||
|
case REQ_TYPE_GET_TELEMETRY_DATA: { |
||||
|
uint8_t perm_mask = ~(payload[1]); // NEW: first reserved byte (of 4), is now inverse mask to apply to permissions
|
||||
|
|
||||
|
telemetry.reset(); |
||||
|
telemetry.addVoltage(TELEM_CHANNEL_SELF, (float)board.getBattMilliVolts() / 1000.0f); |
||||
|
// query other sensors -- target specific
|
||||
|
sensors.querySensors((sender->permission == RoomPermission::ADMIN ? 0xFF : 0x00) & perm_mask, telemetry); |
||||
|
|
||||
|
uint8_t tlen = telemetry.getSize(); |
||||
|
memcpy(&reply_data[4], telemetry.getBuffer(), tlen); |
||||
|
return 4 + tlen; // reply_len
|
||||
|
} |
||||
|
} |
||||
|
return 0; // unknown command
|
||||
|
} |
||||
|
|
||||
|
void MyMesh::logRxRaw(float snr, float rssi, const uint8_t raw[], int len) { |
||||
|
#if MESH_PACKET_LOGGING |
||||
|
Serial.print(getLogDateTime()); |
||||
|
Serial.print(" RAW: "); |
||||
|
mesh::Utils::printHex(Serial, raw, len); |
||||
|
Serial.println(); |
||||
|
#endif |
||||
|
} |
||||
|
|
||||
|
void MyMesh::logRx(mesh::Packet *pkt, int len, float score) { |
||||
|
if (_logging) { |
||||
|
File f = openAppend(PACKET_LOG_FILE); |
||||
|
if (f) { |
||||
|
f.print(getLogDateTime()); |
||||
|
f.printf(": RX, len=%d (type=%d, route=%s, payload_len=%d) SNR=%d RSSI=%d score=%d", len, |
||||
|
pkt->getPayloadType(), pkt->isRouteDirect() ? "D" : "F", pkt->payload_len, |
||||
|
(int)_radio->getLastSNR(), (int)_radio->getLastRSSI(), (int)(score * 1000)); |
||||
|
|
||||
|
if (pkt->getPayloadType() == PAYLOAD_TYPE_PATH || pkt->getPayloadType() == PAYLOAD_TYPE_REQ || |
||||
|
pkt->getPayloadType() == PAYLOAD_TYPE_RESPONSE || pkt->getPayloadType() == PAYLOAD_TYPE_TXT_MSG) { |
||||
|
f.printf(" [%02X -> %02X]\n", (uint32_t)pkt->payload[1], (uint32_t)pkt->payload[0]); |
||||
|
} else { |
||||
|
f.printf("\n"); |
||||
|
} |
||||
|
f.close(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
void MyMesh::logTx(mesh::Packet *pkt, int len) { |
||||
|
if (_logging) { |
||||
|
File f = openAppend(PACKET_LOG_FILE); |
||||
|
if (f) { |
||||
|
f.print(getLogDateTime()); |
||||
|
f.printf(": TX, len=%d (type=%d, route=%s, payload_len=%d)", len, pkt->getPayloadType(), |
||||
|
pkt->isRouteDirect() ? "D" : "F", pkt->payload_len); |
||||
|
|
||||
|
if (pkt->getPayloadType() == PAYLOAD_TYPE_PATH || pkt->getPayloadType() == PAYLOAD_TYPE_REQ || |
||||
|
pkt->getPayloadType() == PAYLOAD_TYPE_RESPONSE || pkt->getPayloadType() == PAYLOAD_TYPE_TXT_MSG) { |
||||
|
f.printf(" [%02X -> %02X]\n", (uint32_t)pkt->payload[1], (uint32_t)pkt->payload[0]); |
||||
|
} else { |
||||
|
f.printf("\n"); |
||||
|
} |
||||
|
f.close(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
void MyMesh::logTxFail(mesh::Packet *pkt, int len) { |
||||
|
if (_logging) { |
||||
|
File f = openAppend(PACKET_LOG_FILE); |
||||
|
if (f) { |
||||
|
f.print(getLogDateTime()); |
||||
|
f.printf(": TX FAIL!, len=%d (type=%d, route=%s, payload_len=%d)\n", len, pkt->getPayloadType(), |
||||
|
pkt->isRouteDirect() ? "D" : "F", pkt->payload_len); |
||||
|
f.close(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
int MyMesh::calcRxDelay(float score, uint32_t air_time) const { |
||||
|
if (_prefs.rx_delay_base <= 0.0f) return 0; |
||||
|
return (int)((pow(_prefs.rx_delay_base, 0.85f - score) - 1.0) * air_time); |
||||
|
} |
||||
|
|
||||
|
const char *MyMesh::getLogDateTime() { |
||||
|
static char tmp[32]; |
||||
|
uint32_t now = getRTCClock()->getCurrentTime(); |
||||
|
DateTime dt = DateTime(now); |
||||
|
sprintf(tmp, "%02d:%02d:%02d - %d/%d/%d U", dt.hour(), dt.minute(), dt.second(), dt.day(), dt.month(), |
||||
|
dt.year()); |
||||
|
return tmp; |
||||
|
} |
||||
|
|
||||
|
uint32_t MyMesh::getRetransmitDelay(const mesh::Packet *packet) { |
||||
|
uint32_t t = (_radio->getEstAirtimeFor(packet->path_len + packet->payload_len + 2) * _prefs.tx_delay_factor); |
||||
|
return getRNG()->nextInt(0, 6) * t; |
||||
|
} |
||||
|
uint32_t MyMesh::getDirectRetransmitDelay(const mesh::Packet *packet) { |
||||
|
uint32_t t = (_radio->getEstAirtimeFor(packet->path_len + packet->payload_len + 2) * _prefs.direct_tx_delay_factor); |
||||
|
return getRNG()->nextInt(0, 6) * t; |
||||
|
} |
||||
|
|
||||
|
bool MyMesh::allowPacketForward(const mesh::Packet *packet) { |
||||
|
if (_prefs.disable_fwd) return false; |
||||
|
if (packet->isRouteFlood() && packet->path_len >= _prefs.flood_max) return false; |
||||
|
return true; |
||||
|
} |
||||
|
|
||||
|
void MyMesh::onAnonDataRecv(mesh::Packet *packet, const uint8_t *secret, const mesh::Identity &sender, |
||||
|
uint8_t *data, size_t len) { |
||||
|
if (packet->getPayloadType() == PAYLOAD_TYPE_ANON_REQ) { // received an initial request by a possible admin
|
||||
|
// client (unknown at this stage)
|
||||
|
uint32_t sender_timestamp, sender_sync_since; |
||||
|
memcpy(&sender_timestamp, data, 4); |
||||
|
memcpy(&sender_sync_since, &data[4], 4); // sender's "sync messags SINCE x" timestamp
|
||||
|
|
||||
|
RoomPermission perm; |
||||
|
data[len] = 0; // ensure null terminator
|
||||
|
if (strcmp((char *)&data[8], _prefs.password) == 0) { // check for valid admin password
|
||||
|
perm = RoomPermission::ADMIN; |
||||
|
} else { |
||||
|
if (strcmp((char *)&data[8], _prefs.guest_password) == 0) { // check the room/public password
|
||||
|
perm = RoomPermission::GUEST; |
||||
|
} else if (_prefs.allow_read_only) { |
||||
|
perm = RoomPermission::READ_ONLY; |
||||
|
} else { |
||||
|
MESH_DEBUG_PRINTLN("Incorrect room password"); |
||||
|
return; // no response. Client will timeout
|
||||
|
} |
||||
|
} |
||||
|
|
||||
|
auto client = putClient(sender); // add to known clients (if not already known)
|
||||
|
if (sender_timestamp <= client->last_timestamp) { |
||||
|
MESH_DEBUG_PRINTLN("possible replay attack!"); |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
MESH_DEBUG_PRINTLN("Login success!"); |
||||
|
client->permission = perm; |
||||
|
client->last_timestamp = sender_timestamp; |
||||
|
client->sync_since = sender_sync_since; |
||||
|
client->pending_ack = 0; |
||||
|
client->push_failures = 0; |
||||
|
memcpy(client->secret, secret, PUB_KEY_SIZE); |
||||
|
|
||||
|
uint32_t now = getRTCClock()->getCurrentTime(); |
||||
|
client->last_activity = now; |
||||
|
|
||||
|
now = getRTCClock()->getCurrentTimeUnique(); |
||||
|
memcpy(reply_data, &now, 4); // response packets always prefixed with timestamp
|
||||
|
// TODO: maybe reply with count of messages waiting to be synced for THIS client?
|
||||
|
reply_data[4] = RESP_SERVER_LOGIN_OK; |
||||
|
reply_data[5] = (CLIENT_KEEP_ALIVE_SECS >> 4); // NEW: recommended keep-alive interval (secs / 16)
|
||||
|
reply_data[6] = (perm == RoomPermission::ADMIN ? 1 : (perm == RoomPermission::GUEST ? 0 : 2)); |
||||
|
reply_data[7] = getUnsyncedCount(client); // NEW
|
||||
|
memcpy(&reply_data[8], "OK", 2); // REVISIT: not really needed
|
||||
|
|
||||
|
next_push = futureMillis(PUSH_NOTIFY_DELAY_MILLIS); // delay next push, give RESPONSE packet time to arrive first
|
||||
|
|
||||
|
if (packet->isRouteFlood()) { |
||||
|
// let this sender know path TO here, so they can use sendDirect(), and ALSO encode the response
|
||||
|
mesh::Packet *path = createPathReturn(sender, client->secret, packet->path, packet->path_len, |
||||
|
PAYLOAD_TYPE_RESPONSE, reply_data, 8 + 2); |
||||
|
if (path) sendFlood(path, SERVER_RESPONSE_DELAY); |
||||
|
} else { |
||||
|
mesh::Packet *reply = createDatagram(PAYLOAD_TYPE_RESPONSE, sender, client->secret, reply_data, 8 + 2); |
||||
|
if (reply) { |
||||
|
if (client->out_path_len >= 0) { // we have an out_path, so send DIRECT
|
||||
|
sendDirect(reply, client->out_path, client->out_path_len, SERVER_RESPONSE_DELAY); |
||||
|
} else { |
||||
|
sendFlood(reply, SERVER_RESPONSE_DELAY); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
int MyMesh::searchPeersByHash(const uint8_t *hash) { |
||||
|
int n = 0; |
||||
|
for (int i = 0; i < num_clients; i++) { |
||||
|
if (known_clients[i].id.isHashMatch(hash)) { |
||||
|
matching_peer_indexes[n++] = i; // store the INDEXES of matching contacts (for subsequent 'peer' methods)
|
||||
|
} |
||||
|
} |
||||
|
return n; |
||||
|
} |
||||
|
|
||||
|
void MyMesh::getPeerSharedSecret(uint8_t *dest_secret, int peer_idx) { |
||||
|
int i = matching_peer_indexes[peer_idx]; |
||||
|
if (i >= 0 && i < num_clients) { |
||||
|
// lookup pre-calculated shared_secret
|
||||
|
memcpy(dest_secret, known_clients[i].secret, PUB_KEY_SIZE); |
||||
|
} else { |
||||
|
MESH_DEBUG_PRINTLN("getPeerSharedSecret: Invalid peer idx: %d", i); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
void MyMesh::onPeerDataRecv(mesh::Packet *packet, uint8_t type, int sender_idx, const uint8_t *secret, |
||||
|
uint8_t *data, size_t len) { |
||||
|
int i = matching_peer_indexes[sender_idx]; |
||||
|
if (i < 0 || i >= num_clients) { // get from our known_clients table (sender SHOULD already be known in this context)
|
||||
|
MESH_DEBUG_PRINTLN("onPeerDataRecv: invalid peer idx: %d", i); |
||||
|
return; |
||||
|
} |
||||
|
auto client = &known_clients[i]; |
||||
|
if (type == PAYLOAD_TYPE_TXT_MSG && len > 5) { // a CLI command or new Post
|
||||
|
uint32_t sender_timestamp; |
||||
|
memcpy(&sender_timestamp, data, 4); // timestamp (by sender's RTC clock - which could be wrong)
|
||||
|
uint flags = (data[4] >> 2); // message attempt number, and other flags
|
||||
|
|
||||
|
if (!(flags == TXT_TYPE_PLAIN || flags == TXT_TYPE_CLI_DATA)) { |
||||
|
MESH_DEBUG_PRINTLN("onPeerDataRecv: unsupported command flags received: flags=%02x", (uint32_t)flags); |
||||
|
} else if (sender_timestamp >= client->last_timestamp) { // prevent replay attacks, but send Acks for retries
|
||||
|
bool is_retry = (sender_timestamp == client->last_timestamp); |
||||
|
client->last_timestamp = sender_timestamp; |
||||
|
|
||||
|
uint32_t now = getRTCClock()->getCurrentTimeUnique(); |
||||
|
client->last_activity = now; |
||||
|
client->push_failures = 0; // reset so push can resume (if prev failed)
|
||||
|
|
||||
|
// len can be > original length, but 'text' will be padded with zeroes
|
||||
|
data[len] = 0; // need to make a C string again, with null terminator
|
||||
|
|
||||
|
uint32_t ack_hash; // calc truncated hash of the message timestamp + text + sender pub_key, to prove to
|
||||
|
// sender that we got it
|
||||
|
mesh::Utils::sha256((uint8_t *)&ack_hash, 4, data, 5 + strlen((char *)&data[5]), client->id.pub_key, |
||||
|
PUB_KEY_SIZE); |
||||
|
|
||||
|
uint8_t temp[166]; |
||||
|
bool send_ack; |
||||
|
if (flags == TXT_TYPE_CLI_DATA) { |
||||
|
if (client->permission == RoomPermission::ADMIN) { |
||||
|
if (is_retry) { |
||||
|
temp[5] = 0; // no reply
|
||||
|
} else { |
||||
|
handleCommand(sender_timestamp, (char *)&data[5], (char *)&temp[5]); |
||||
|
temp[4] = (TXT_TYPE_CLI_DATA << 2); // attempt and flags, (NOTE: legacy was: TXT_TYPE_PLAIN)
|
||||
|
} |
||||
|
send_ack = false; |
||||
|
} else { |
||||
|
temp[5] = 0; // no reply
|
||||
|
send_ack = false; // and no ACK... user shoudn't be sending these
|
||||
|
} |
||||
|
} else { // TXT_TYPE_PLAIN
|
||||
|
if (client->permission == RoomPermission::READ_ONLY) { |
||||
|
temp[5] = 0; // no reply
|
||||
|
send_ack = false; // no ACK
|
||||
|
} else { |
||||
|
if (!is_retry) { |
||||
|
addPost(client, (const char *)&data[5]); |
||||
|
} |
||||
|
temp[5] = 0; // no reply (ACK is enough)
|
||||
|
send_ack = true; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
uint32_t delay_millis; |
||||
|
if (send_ack) { |
||||
|
if (client->out_path_len < 0) { |
||||
|
mesh::Packet *ack = createAck(ack_hash); |
||||
|
if (ack) sendFlood(ack, TXT_ACK_DELAY); |
||||
|
delay_millis = TXT_ACK_DELAY + REPLY_DELAY_MILLIS; |
||||
|
} else { |
||||
|
uint32_t d = TXT_ACK_DELAY; |
||||
|
if (getExtraAckTransmitCount() > 0) { |
||||
|
mesh::Packet *a1 = createMultiAck(ack_hash, 1); |
||||
|
if (a1) sendDirect(a1, client->out_path, client->out_path_len, d); |
||||
|
d += 300; |
||||
|
} |
||||
|
|
||||
|
mesh::Packet *a2 = createAck(ack_hash); |
||||
|
if (a2) sendDirect(a2, client->out_path, client->out_path_len, d); |
||||
|
delay_millis = d + REPLY_DELAY_MILLIS; |
||||
|
} |
||||
|
} else { |
||||
|
delay_millis = 0; |
||||
|
} |
||||
|
|
||||
|
int text_len = strlen((char *)&temp[5]); |
||||
|
if (text_len > 0) { |
||||
|
if (now == sender_timestamp) { |
||||
|
// WORKAROUND: the two timestamps need to be different, in the CLI view
|
||||
|
now++; |
||||
|
} |
||||
|
memcpy(temp, &now, 4); // mostly an extra blob to help make packet_hash unique
|
||||
|
|
||||
|
// calc expected ACK reply
|
||||
|
// mesh::Utils::sha256((uint8_t *)&expected_ack_crc, 4, temp, 5 + text_len, self_id.pub_key,
|
||||
|
// PUB_KEY_SIZE);
|
||||
|
|
||||
|
auto reply = createDatagram(PAYLOAD_TYPE_TXT_MSG, client->id, secret, temp, 5 + text_len); |
||||
|
if (reply) { |
||||
|
if (client->out_path_len < 0) { |
||||
|
sendFlood(reply, delay_millis + SERVER_RESPONSE_DELAY); |
||||
|
} else { |
||||
|
sendDirect(reply, client->out_path, client->out_path_len, delay_millis + SERVER_RESPONSE_DELAY); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} else { |
||||
|
MESH_DEBUG_PRINTLN("onPeerDataRecv: possible replay attack detected"); |
||||
|
} |
||||
|
} else if (type == PAYLOAD_TYPE_REQ && len >= 5) { |
||||
|
uint32_t sender_timestamp; |
||||
|
memcpy(&sender_timestamp, data, 4); // timestamp (by sender's RTC clock - which could be wrong)
|
||||
|
if (sender_timestamp < client->last_timestamp) { // prevent replay attacks
|
||||
|
MESH_DEBUG_PRINTLN("onPeerDataRecv: possible replay attack detected"); |
||||
|
} else { |
||||
|
client->last_timestamp = sender_timestamp; |
||||
|
|
||||
|
uint32_t now = getRTCClock()->getCurrentTime(); |
||||
|
client->last_activity = now; // <-- THIS will keep client connection alive
|
||||
|
client->push_failures = 0; // reset so push can resume (if prev failed)
|
||||
|
|
||||
|
if (data[4] == REQ_TYPE_KEEP_ALIVE && packet->isRouteDirect()) { // request type
|
||||
|
uint32_t forceSince = 0; |
||||
|
if (len >= 9) { // optional - last post_timestamp client received
|
||||
|
memcpy(&forceSince, &data[5], 4); // NOTE: this may be 0, if part of decrypted PADDING!
|
||||
|
} else { |
||||
|
memcpy(&data[5], &forceSince, 4); // make sure there are zeroes in payload (for ack_hash calc below)
|
||||
|
} |
||||
|
if (forceSince > 0) { |
||||
|
client->sync_since = forceSince; // force-update the 'sync since'
|
||||
|
} |
||||
|
|
||||
|
client->pending_ack = 0; |
||||
|
|
||||
|
// TODO: Throttle KEEP_ALIVE requests!
|
||||
|
// if client sends too quickly, evict()
|
||||
|
|
||||
|
// RULE: only send keep_alive response DIRECT!
|
||||
|
if (client->out_path_len >= 0) { |
||||
|
uint32_t ack_hash; // calc ACK to prove to sender that we got request
|
||||
|
mesh::Utils::sha256((uint8_t *)&ack_hash, 4, data, 9, client->id.pub_key, PUB_KEY_SIZE); |
||||
|
|
||||
|
auto reply = createAck(ack_hash); |
||||
|
if (reply) { |
||||
|
reply->payload[reply->payload_len++] = getUnsyncedCount(client); // NEW: add unsynced counter to end of ACK packet
|
||||
|
sendDirect(reply, client->out_path, client->out_path_len, SERVER_RESPONSE_DELAY); |
||||
|
} |
||||
|
} |
||||
|
} else { |
||||
|
int reply_len = handleRequest(client, sender_timestamp, &data[4], len - 4); |
||||
|
if (reply_len > 0) { // valid command
|
||||
|
if (packet->isRouteFlood()) { |
||||
|
// let this sender know path TO here, so they can use sendDirect(), and ALSO encode the response
|
||||
|
mesh::Packet *path = createPathReturn(client->id, secret, packet->path, packet->path_len, |
||||
|
PAYLOAD_TYPE_RESPONSE, reply_data, reply_len); |
||||
|
if (path) sendFlood(path, SERVER_RESPONSE_DELAY); |
||||
|
} else { |
||||
|
mesh::Packet *reply = createDatagram(PAYLOAD_TYPE_RESPONSE, client->id, secret, reply_data, reply_len); |
||||
|
if (reply) { |
||||
|
if (client->out_path_len >= 0) { // we have an out_path, so send DIRECT
|
||||
|
sendDirect(reply, client->out_path, client->out_path_len, SERVER_RESPONSE_DELAY); |
||||
|
} else { |
||||
|
sendFlood(reply, SERVER_RESPONSE_DELAY); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
bool MyMesh::onPeerPathRecv(mesh::Packet *packet, int sender_idx, const uint8_t *secret, uint8_t *path, |
||||
|
uint8_t path_len, uint8_t extra_type, uint8_t *extra, uint8_t extra_len) { |
||||
|
// TODO: prevent replay attacks
|
||||
|
int i = matching_peer_indexes[sender_idx]; |
||||
|
|
||||
|
if (i >= 0 && i < num_clients) { // get from our known_clients table (sender SHOULD already be known in this context)
|
||||
|
MESH_DEBUG_PRINTLN("PATH to client, path_len=%d", (uint32_t)path_len); |
||||
|
auto client = &known_clients[i]; |
||||
|
memcpy(client->out_path, path, client->out_path_len = path_len); // store a copy of path, for sendDirect()
|
||||
|
} else { |
||||
|
MESH_DEBUG_PRINTLN("onPeerPathRecv: invalid peer idx: %d", i); |
||||
|
} |
||||
|
|
||||
|
if (extra_type == PAYLOAD_TYPE_ACK && extra_len >= 4) { |
||||
|
// also got an encoded ACK!
|
||||
|
processAck(extra); |
||||
|
} |
||||
|
|
||||
|
// NOTE: no reciprocal path send!!
|
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
void MyMesh::onAckRecv(mesh::Packet *packet, uint32_t ack_crc) { |
||||
|
if (processAck((uint8_t *)&ack_crc)) { |
||||
|
packet->markDoNotRetransmit(); // ACK was for this node, so don't retransmit
|
||||
|
} |
||||
|
} |
||||
|
|
||||
|
MyMesh::MyMesh(mesh::MainBoard &board, mesh::Radio &radio, mesh::MillisecondClock &ms, mesh::RNG &rng, |
||||
|
mesh::RTCClock &rtc, mesh::MeshTables &tables) |
||||
|
: mesh::Mesh(radio, ms, rng, rtc, *new StaticPoolPacketManager(32), tables), |
||||
|
_cli(board, rtc, &_prefs, this), telemetry(MAX_PACKET_PAYLOAD - 4) { |
||||
|
next_local_advert = next_flood_advert = 0; |
||||
|
_logging = false; |
||||
|
set_radio_at = revert_radio_at = 0; |
||||
|
|
||||
|
// defaults
|
||||
|
memset(&_prefs, 0, sizeof(_prefs)); |
||||
|
_prefs.airtime_factor = 1.0; // one half
|
||||
|
_prefs.rx_delay_base = 0.0f; // off by default, was 10.0
|
||||
|
_prefs.tx_delay_factor = 0.5f; // was 0.25f;
|
||||
|
StrHelper::strncpy(_prefs.node_name, ADVERT_NAME, sizeof(_prefs.node_name)); |
||||
|
_prefs.node_lat = ADVERT_LAT; |
||||
|
_prefs.node_lon = ADVERT_LON; |
||||
|
StrHelper::strncpy(_prefs.password, ADMIN_PASSWORD, sizeof(_prefs.password)); |
||||
|
_prefs.freq = LORA_FREQ; |
||||
|
_prefs.sf = LORA_SF; |
||||
|
_prefs.bw = LORA_BW; |
||||
|
_prefs.cr = LORA_CR; |
||||
|
_prefs.tx_power_dbm = LORA_TX_POWER; |
||||
|
_prefs.disable_fwd = 1; |
||||
|
_prefs.advert_interval = 1; // default to 2 minutes for NEW installs
|
||||
|
_prefs.flood_advert_interval = 12; // 12 hours
|
||||
|
_prefs.flood_max = 64; |
||||
|
_prefs.interference_threshold = 0; // disabled
|
||||
|
#ifdef ROOM_PASSWORD |
||||
|
StrHelper::strncpy(_prefs.guest_password, ROOM_PASSWORD, sizeof(_prefs.guest_password)); |
||||
|
#endif |
||||
|
|
||||
|
num_clients = 0; |
||||
|
next_post_idx = 0; |
||||
|
next_client_idx = 0; |
||||
|
next_push = 0; |
||||
|
memset(posts, 0, sizeof(posts)); |
||||
|
_num_posted = _num_post_pushes = 0; |
||||
|
} |
||||
|
|
||||
|
void MyMesh::begin(FILESYSTEM *fs) { |
||||
|
mesh::Mesh::begin(); |
||||
|
_fs = fs; |
||||
|
// load persisted prefs
|
||||
|
_cli.loadPrefs(_fs); |
||||
|
|
||||
|
radio_set_params(_prefs.freq, _prefs.bw, _prefs.sf, _prefs.cr); |
||||
|
radio_set_tx_power(_prefs.tx_power_dbm); |
||||
|
|
||||
|
updateAdvertTimer(); |
||||
|
updateFloodAdvertTimer(); |
||||
|
} |
||||
|
|
||||
|
void MyMesh::applyTempRadioParams(float freq, float bw, uint8_t sf, uint8_t cr, int timeout_mins) { |
||||
|
set_radio_at = futureMillis(2000); // give CLI reply some time to be sent back, before applying temp radio params
|
||||
|
pending_freq = freq; |
||||
|
pending_bw = bw; |
||||
|
pending_sf = sf; |
||||
|
pending_cr = cr; |
||||
|
|
||||
|
revert_radio_at = futureMillis(2000 + timeout_mins * 60 * 1000); // schedule when to revert radio params
|
||||
|
} |
||||
|
|
||||
|
bool MyMesh::formatFileSystem() { |
||||
|
#if defined(NRF52_PLATFORM) |
||||
|
return InternalFS.format(); |
||||
|
#elif defined(RP2040_PLATFORM) |
||||
|
return LittleFS.format(); |
||||
|
#elif defined(ESP32) |
||||
|
return SPIFFS.format(); |
||||
|
#else |
||||
|
#error "need to implement file system erase" |
||||
|
return false; |
||||
|
#endif |
||||
|
} |
||||
|
|
||||
|
void MyMesh::sendSelfAdvertisement(int delay_millis) { |
||||
|
mesh::Packet *pkt = createSelfAdvert(); |
||||
|
if (pkt) { |
||||
|
sendFlood(pkt, delay_millis); |
||||
|
} else { |
||||
|
MESH_DEBUG_PRINTLN("ERROR: unable to create advertisement packet!"); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
void MyMesh::updateAdvertTimer() { |
||||
|
if (_prefs.advert_interval > 0) { // schedule local advert timer
|
||||
|
next_local_advert = futureMillis((uint32_t)_prefs.advert_interval * 2 * 60 * 1000); |
||||
|
} else { |
||||
|
next_local_advert = 0; // stop the timer
|
||||
|
} |
||||
|
} |
||||
|
void MyMesh::updateFloodAdvertTimer() { |
||||
|
if (_prefs.flood_advert_interval > 0) { // schedule flood advert timer
|
||||
|
next_flood_advert = futureMillis(((uint32_t)_prefs.flood_advert_interval) * 60 * 60 * 1000); |
||||
|
} else { |
||||
|
next_flood_advert = 0; // stop the timer
|
||||
|
} |
||||
|
} |
||||
|
|
||||
|
void MyMesh::dumpLogFile() { |
||||
|
#if defined(RP2040_PLATFORM) |
||||
|
File f = _fs->open(PACKET_LOG_FILE, "r"); |
||||
|
#else |
||||
|
File f = _fs->open(PACKET_LOG_FILE); |
||||
|
#endif |
||||
|
if (f) { |
||||
|
while (f.available()) { |
||||
|
int c = f.read(); |
||||
|
if (c < 0) break; |
||||
|
Serial.print((char)c); |
||||
|
} |
||||
|
f.close(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
void MyMesh::setTxPower(uint8_t power_dbm) { |
||||
|
radio_set_tx_power(power_dbm); |
||||
|
} |
||||
|
|
||||
|
void MyMesh::saveIdentity(const mesh::LocalIdentity &new_id) { |
||||
|
self_id = new_id; |
||||
|
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM) |
||||
|
IdentityStore store(*_fs, ""); |
||||
|
#elif defined(ESP32) |
||||
|
IdentityStore store(*_fs, "/identity"); |
||||
|
#elif defined(RP2040_PLATFORM) |
||||
|
IdentityStore store(*_fs, "/identity"); |
||||
|
#else |
||||
|
#error "need to define saveIdentity()" |
||||
|
#endif |
||||
|
store.save("_main", self_id); |
||||
|
} |
||||
|
|
||||
|
void MyMesh::clearStats() { |
||||
|
radio_driver.resetStats(); |
||||
|
resetStats(); |
||||
|
((SimpleMeshTables *)getTables())->resetStats(); |
||||
|
} |
||||
|
|
||||
|
void MyMesh::handleCommand(uint32_t sender_timestamp, char *command, char *reply) { |
||||
|
while (*command == ' ') |
||||
|
command++; // skip leading spaces
|
||||
|
|
||||
|
if (strlen(command) > 4 && command[2] == '|') { // optional prefix (for companion radio CLI)
|
||||
|
memcpy(reply, command, 3); // reflect the prefix back
|
||||
|
reply += 3; |
||||
|
command += 3; |
||||
|
} |
||||
|
|
||||
|
_cli.handleCommand(sender_timestamp, command, reply); // common CLI commands
|
||||
|
} |
||||
|
|
||||
|
void MyMesh::loop() { |
||||
|
mesh::Mesh::loop(); |
||||
|
|
||||
|
if (millisHasNowPassed(next_push) && num_clients > 0) { |
||||
|
// check for ACK timeouts
|
||||
|
for (int i = 0; i < num_clients; i++) { |
||||
|
auto c = &known_clients[i]; |
||||
|
if (c->pending_ack && millisHasNowPassed(c->ack_timeout)) { |
||||
|
c->push_failures++; |
||||
|
c->pending_ack = 0; // reset (TODO: keep prev expected_ack's in a list, incase they arrive LATER, after we retry)
|
||||
|
MESH_DEBUG_PRINTLN("pending ACK timed out: push_failures: %d", (uint32_t)c->push_failures); |
||||
|
} |
||||
|
} |
||||
|
// check next Round-Robin client, and sync next new post
|
||||
|
auto client = &known_clients[next_client_idx]; |
||||
|
bool did_push = false; |
||||
|
if (client->pending_ack == 0 && client->last_activity != 0 && |
||||
|
client->push_failures < 3) { // not already waiting for ACK, AND not evicted, AND retries not max
|
||||
|
MESH_DEBUG_PRINTLN("loop - checking for client %02X", (uint32_t)client->id.pub_key[0]); |
||||
|
uint32_t now = getRTCClock()->getCurrentTime(); |
||||
|
for (int k = 0, idx = next_post_idx; k < MAX_UNSYNCED_POSTS; k++) { |
||||
|
auto p = &posts[idx]; |
||||
|
if (now >= p->post_timestamp + POST_SYNC_DELAY_SECS && |
||||
|
p->post_timestamp > client->sync_since // is new post for this Client?
|
||||
|
&& !p->author.matches(client->id)) { // don't push posts to the author
|
||||
|
// push this post to Client, then wait for ACK
|
||||
|
pushPostToClient(client, *p); |
||||
|
did_push = true; |
||||
|
MESH_DEBUG_PRINTLN("loop - pushed to client %02X: %s", (uint32_t)client->id.pub_key[0], p->text); |
||||
|
break; |
||||
|
} |
||||
|
idx = (idx + 1) % MAX_UNSYNCED_POSTS; // wrap to start of cyclic queue
|
||||
|
} |
||||
|
} else { |
||||
|
MESH_DEBUG_PRINTLN("loop - skipping busy (or evicted) client %02X", (uint32_t)client->id.pub_key[0]); |
||||
|
} |
||||
|
next_client_idx = (next_client_idx + 1) % num_clients; // round robin polling for each client
|
||||
|
|
||||
|
if (did_push) { |
||||
|
next_push = futureMillis(SYNC_PUSH_INTERVAL); |
||||
|
} else { |
||||
|
// were no unsynced posts for curr client, so proccess next client much quicker! (in next loop())
|
||||
|
next_push = futureMillis(SYNC_PUSH_INTERVAL / 8); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
if (next_flood_advert && millisHasNowPassed(next_flood_advert)) { |
||||
|
mesh::Packet *pkt = createSelfAdvert(); |
||||
|
if (pkt) sendFlood(pkt); |
||||
|
|
||||
|
updateFloodAdvertTimer(); // schedule next flood advert
|
||||
|
updateAdvertTimer(); // also schedule local advert (so they don't overlap)
|
||||
|
} else if (next_local_advert && millisHasNowPassed(next_local_advert)) { |
||||
|
mesh::Packet *pkt = createSelfAdvert(); |
||||
|
if (pkt) sendZeroHop(pkt); |
||||
|
|
||||
|
updateAdvertTimer(); // schedule next local advert
|
||||
|
} |
||||
|
|
||||
|
if (set_radio_at && millisHasNowPassed(set_radio_at)) { // apply pending (temporary) radio params
|
||||
|
set_radio_at = 0; // clear timer
|
||||
|
radio_set_params(pending_freq, pending_bw, pending_sf, pending_cr); |
||||
|
MESH_DEBUG_PRINTLN("Temp radio params"); |
||||
|
} |
||||
|
|
||||
|
if (revert_radio_at && millisHasNowPassed(revert_radio_at)) { // revert radio params to orig
|
||||
|
revert_radio_at = 0; // clear timer
|
||||
|
radio_set_params(_prefs.freq, _prefs.bw, _prefs.sf, _prefs.cr); |
||||
|
MESH_DEBUG_PRINTLN("Radio params restored"); |
||||
|
} |
||||
|
|
||||
|
// TODO: periodically check for OLD/inactive entries in known_clients[], and evict
|
||||
|
} |
||||
@ -0,0 +1,220 @@ |
|||||
|
#pragma once |
||||
|
|
||||
|
#include <Arduino.h> // needed for PlatformIO |
||||
|
#include <Mesh.h> |
||||
|
|
||||
|
#if defined(NRF52_PLATFORM) |
||||
|
#include <InternalFileSystem.h> |
||||
|
#elif defined(RP2040_PLATFORM) |
||||
|
#include <LittleFS.h> |
||||
|
#elif defined(ESP32) |
||||
|
#include <SPIFFS.h> |
||||
|
#endif |
||||
|
|
||||
|
#include <helpers/ArduinoHelpers.h> |
||||
|
#include <helpers/StaticPoolPacketManager.h> |
||||
|
#include <helpers/SimpleMeshTables.h> |
||||
|
#include <helpers/IdentityStore.h> |
||||
|
#include <helpers/AdvertDataHelpers.h> |
||||
|
#include <helpers/TxtDataHelpers.h> |
||||
|
#include <helpers/CommonCLI.h> |
||||
|
#include <RTClib.h> |
||||
|
#include <target.h> |
||||
|
|
||||
|
/* ------------------------------ Config -------------------------------- */ |
||||
|
|
||||
|
#ifndef FIRMWARE_BUILD_DATE |
||||
|
#define FIRMWARE_BUILD_DATE "1 Sep 2025" |
||||
|
#endif |
||||
|
|
||||
|
#ifndef FIRMWARE_VERSION |
||||
|
#define FIRMWARE_VERSION "v1.8.1" |
||||
|
#endif |
||||
|
|
||||
|
#ifndef LORA_FREQ |
||||
|
#define LORA_FREQ 915.0 |
||||
|
#endif |
||||
|
#ifndef LORA_BW |
||||
|
#define LORA_BW 250 |
||||
|
#endif |
||||
|
#ifndef LORA_SF |
||||
|
#define LORA_SF 10 |
||||
|
#endif |
||||
|
#ifndef LORA_CR |
||||
|
#define LORA_CR 5 |
||||
|
#endif |
||||
|
#ifndef LORA_TX_POWER |
||||
|
#define LORA_TX_POWER 20 |
||||
|
#endif |
||||
|
|
||||
|
#ifndef ADVERT_NAME |
||||
|
#define ADVERT_NAME "Test BBS" |
||||
|
#endif |
||||
|
#ifndef ADVERT_LAT |
||||
|
#define ADVERT_LAT 0.0 |
||||
|
#endif |
||||
|
#ifndef ADVERT_LON |
||||
|
#define ADVERT_LON 0.0 |
||||
|
#endif |
||||
|
|
||||
|
#ifndef ADMIN_PASSWORD |
||||
|
#define ADMIN_PASSWORD "password" |
||||
|
#endif |
||||
|
|
||||
|
#ifndef MAX_CLIENTS |
||||
|
#define MAX_CLIENTS 32 |
||||
|
#endif |
||||
|
|
||||
|
#ifndef MAX_UNSYNCED_POSTS |
||||
|
#define MAX_UNSYNCED_POSTS 32 |
||||
|
#endif |
||||
|
|
||||
|
#ifndef SERVER_RESPONSE_DELAY |
||||
|
#define SERVER_RESPONSE_DELAY 300 |
||||
|
#endif |
||||
|
|
||||
|
#ifndef TXT_ACK_DELAY |
||||
|
#define TXT_ACK_DELAY 200 |
||||
|
#endif |
||||
|
|
||||
|
#define FIRMWARE_ROLE "room_server" |
||||
|
|
||||
|
#define PACKET_LOG_FILE "/packet_log" |
||||
|
|
||||
|
enum RoomPermission { |
||||
|
ADMIN, |
||||
|
GUEST, |
||||
|
READ_ONLY |
||||
|
}; |
||||
|
|
||||
|
struct ClientInfo { |
||||
|
mesh::Identity id; |
||||
|
uint32_t last_timestamp; // by THEIR clock
|
||||
|
uint32_t last_activity; // by OUR clock
|
||||
|
uint32_t sync_since; // sync messages SINCE this timestamp (by OUR clock)
|
||||
|
uint32_t pending_ack; |
||||
|
uint32_t push_post_timestamp; |
||||
|
unsigned long ack_timeout; |
||||
|
RoomPermission permission; |
||||
|
uint8_t push_failures; |
||||
|
uint8_t secret[PUB_KEY_SIZE]; |
||||
|
int out_path_len; |
||||
|
uint8_t out_path[MAX_PATH_SIZE]; |
||||
|
}; |
||||
|
|
||||
|
#define MAX_POST_TEXT_LEN (160-9) |
||||
|
|
||||
|
struct PostInfo { |
||||
|
mesh::Identity author; |
||||
|
uint32_t post_timestamp; // by OUR clock
|
||||
|
char text[MAX_POST_TEXT_LEN+1]; |
||||
|
}; |
||||
|
|
||||
|
class MyMesh : public mesh::Mesh, public CommonCLICallbacks { |
||||
|
FILESYSTEM* _fs; |
||||
|
unsigned long next_local_advert, next_flood_advert; |
||||
|
bool _logging; |
||||
|
NodePrefs _prefs; |
||||
|
CommonCLI _cli; |
||||
|
uint8_t reply_data[MAX_PACKET_PAYLOAD]; |
||||
|
int num_clients; |
||||
|
ClientInfo known_clients[MAX_CLIENTS]; |
||||
|
unsigned long next_push; |
||||
|
uint16_t _num_posted, _num_post_pushes; |
||||
|
int next_client_idx; // for round-robin polling
|
||||
|
int next_post_idx; |
||||
|
PostInfo posts[MAX_UNSYNCED_POSTS]; // cyclic queue
|
||||
|
CayenneLPP telemetry; |
||||
|
unsigned long set_radio_at, revert_radio_at; |
||||
|
float pending_freq; |
||||
|
float pending_bw; |
||||
|
uint8_t pending_sf; |
||||
|
uint8_t pending_cr; |
||||
|
int matching_peer_indexes[MAX_CLIENTS]; |
||||
|
|
||||
|
ClientInfo* putClient(const mesh::Identity& id); |
||||
|
void evict(ClientInfo* client); |
||||
|
void addPost(ClientInfo* client, const char* postData); |
||||
|
void pushPostToClient(ClientInfo* client, PostInfo& post); |
||||
|
uint8_t getUnsyncedCount(ClientInfo* client); |
||||
|
bool processAck(const uint8_t *data); |
||||
|
mesh::Packet* createSelfAdvert(); |
||||
|
File openAppend(const char* fname); |
||||
|
int handleRequest(ClientInfo* sender, uint32_t sender_timestamp, uint8_t* payload, size_t payload_len); |
||||
|
|
||||
|
protected: |
||||
|
float getAirtimeBudgetFactor() const override { |
||||
|
return _prefs.airtime_factor; |
||||
|
} |
||||
|
|
||||
|
void logRxRaw(float snr, float rssi, const uint8_t raw[], int len) override; |
||||
|
void logRx(mesh::Packet* pkt, int len, float score) override; |
||||
|
void logTx(mesh::Packet* pkt, int len) override; |
||||
|
void logTxFail(mesh::Packet* pkt, int len) override; |
||||
|
|
||||
|
int calcRxDelay(float score, uint32_t air_time) const override; |
||||
|
const char* getLogDateTime() override; |
||||
|
uint32_t getRetransmitDelay(const mesh::Packet* packet) override; |
||||
|
uint32_t getDirectRetransmitDelay(const mesh::Packet* packet) override; |
||||
|
|
||||
|
int getInterferenceThreshold() const override { |
||||
|
return _prefs.interference_threshold; |
||||
|
} |
||||
|
int getAGCResetInterval() const override { |
||||
|
return ((int)_prefs.agc_reset_interval) * 4000; // milliseconds
|
||||
|
} |
||||
|
uint8_t getExtraAckTransmitCount() const override { |
||||
|
return _prefs.multi_acks; |
||||
|
} |
||||
|
|
||||
|
bool allowPacketForward(const mesh::Packet* packet) override; |
||||
|
void onAnonDataRecv(mesh::Packet* packet, const uint8_t* secret, const mesh::Identity& sender, uint8_t* data, size_t len) override; |
||||
|
int searchPeersByHash(const uint8_t* hash) override ; |
||||
|
void getPeerSharedSecret(uint8_t* dest_secret, int peer_idx) override; |
||||
|
void onPeerDataRecv(mesh::Packet* packet, uint8_t type, int sender_idx, const uint8_t* secret, uint8_t* data, size_t len) override; |
||||
|
bool onPeerPathRecv(mesh::Packet* packet, int sender_idx, const uint8_t* secret, uint8_t* path, uint8_t path_len, uint8_t extra_type, uint8_t* extra, uint8_t extra_len) override; |
||||
|
void onAckRecv(mesh::Packet* packet, uint32_t ack_crc) override; |
||||
|
|
||||
|
public: |
||||
|
MyMesh(mesh::MainBoard& board, mesh::Radio& radio, mesh::MillisecondClock& ms, mesh::RNG& rng, mesh::RTCClock& rtc, mesh::MeshTables& tables); |
||||
|
|
||||
|
void begin(FILESYSTEM* fs); |
||||
|
|
||||
|
const char* getFirmwareVer() override { return FIRMWARE_VERSION; } |
||||
|
const char* getBuildDate() override { return FIRMWARE_BUILD_DATE; } |
||||
|
const char* getRole() override { return FIRMWARE_ROLE; } |
||||
|
const char* getNodeName() { return _prefs.node_name; } |
||||
|
NodePrefs* getNodePrefs() { |
||||
|
return &_prefs; |
||||
|
} |
||||
|
|
||||
|
void savePrefs() override { |
||||
|
_cli.savePrefs(_fs); |
||||
|
} |
||||
|
|
||||
|
void applyTempRadioParams(float freq, float bw, uint8_t sf, uint8_t cr, int timeout_mins) override; |
||||
|
bool formatFileSystem() override; |
||||
|
void sendSelfAdvertisement(int delay_millis) override; |
||||
|
void updateAdvertTimer() override; |
||||
|
void updateFloodAdvertTimer() override; |
||||
|
|
||||
|
void setLoggingOn(bool enable) override { _logging = enable; } |
||||
|
|
||||
|
void eraseLogFile() override { |
||||
|
_fs->remove(PACKET_LOG_FILE); |
||||
|
} |
||||
|
|
||||
|
void dumpLogFile() override; |
||||
|
void setTxPower(uint8_t power_dbm) override; |
||||
|
|
||||
|
void formatNeighborsReply(char *reply) override { |
||||
|
strcpy(reply, "not supported"); |
||||
|
} |
||||
|
|
||||
|
mesh::LocalIdentity& getSelfId() override { return self_id; } |
||||
|
|
||||
|
void saveIdentity(const mesh::LocalIdentity& new_id) override; |
||||
|
void clearStats() override; |
||||
|
void handleCommand(uint32_t sender_timestamp, char* command, char* reply); |
||||
|
void loop(); |
||||
|
}; |
||||
@ -0,0 +1,34 @@ |
|||||
|
#pragma once |
||||
|
|
||||
|
#include <Mesh.h> |
||||
|
|
||||
|
class AbstractBridge { |
||||
|
public: |
||||
|
virtual ~AbstractBridge() {} |
||||
|
|
||||
|
/**
|
||||
|
* @brief Initializes the bridge. |
||||
|
*/ |
||||
|
virtual void begin() = 0; |
||||
|
|
||||
|
/**
|
||||
|
* @brief A method to be called on every main loop iteration. |
||||
|
* Used for tasks like checking for incoming data. |
||||
|
*/ |
||||
|
virtual void loop() = 0; |
||||
|
|
||||
|
/**
|
||||
|
* @brief A callback that is triggered when the mesh transmits a packet. |
||||
|
* The bridge can use this to forward the packet. |
||||
|
* |
||||
|
* @param packet The packet that was transmitted. |
||||
|
*/ |
||||
|
virtual void onPacketTransmitted(mesh::Packet* packet) = 0; |
||||
|
|
||||
|
/**
|
||||
|
* @brief Processes a received packet from the bridge's medium. |
||||
|
* |
||||
|
* @param packet The packet that was received. |
||||
|
*/ |
||||
|
virtual void onPacketReceived(mesh::Packet* packet) = 0; |
||||
|
}; |
||||
@ -0,0 +1,128 @@ |
|||||
|
#include "ClientACL.h" |
||||
|
|
||||
|
static File openWrite(FILESYSTEM* _fs, const char* filename) { |
||||
|
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM) |
||||
|
_fs->remove(filename); |
||||
|
return _fs->open(filename, FILE_O_WRITE); |
||||
|
#elif defined(RP2040_PLATFORM) |
||||
|
return _fs->open(filename, "w"); |
||||
|
#else |
||||
|
return _fs->open(filename, "w", true); |
||||
|
#endif |
||||
|
} |
||||
|
|
||||
|
void ClientACL::load(FILESYSTEM* _fs) { |
||||
|
num_clients = 0; |
||||
|
if (_fs->exists("/s_contacts")) { |
||||
|
#if defined(RP2040_PLATFORM) |
||||
|
File file = _fs->open("/s_contacts", "r"); |
||||
|
#else |
||||
|
File file = _fs->open("/s_contacts"); |
||||
|
#endif |
||||
|
if (file) { |
||||
|
bool full = false; |
||||
|
while (!full) { |
||||
|
ClientInfo c; |
||||
|
uint8_t pub_key[32]; |
||||
|
uint8_t unused[6]; |
||||
|
|
||||
|
bool success = (file.read(pub_key, 32) == 32); |
||||
|
success = success && (file.read((uint8_t *) &c.permissions, 1) == 1); |
||||
|
success = success && (file.read(unused, 6) == 6); |
||||
|
success = success && (file.read((uint8_t *)&c.out_path_len, 1) == 1); |
||||
|
success = success && (file.read(c.out_path, 64) == 64); |
||||
|
success = success && (file.read(c.shared_secret, PUB_KEY_SIZE) == PUB_KEY_SIZE); |
||||
|
c.last_timestamp = 0; // transient
|
||||
|
c.last_activity = 0; |
||||
|
|
||||
|
if (!success) break; // EOF
|
||||
|
|
||||
|
c.id = mesh::Identity(pub_key); |
||||
|
if (num_clients < MAX_CLIENTS) { |
||||
|
clients[num_clients++] = c; |
||||
|
} else { |
||||
|
full = true; |
||||
|
} |
||||
|
} |
||||
|
file.close(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
void ClientACL::save(FILESYSTEM* _fs) { |
||||
|
File file = openWrite(_fs, "/s_contacts"); |
||||
|
if (file) { |
||||
|
uint8_t unused[6]; |
||||
|
memset(unused, 0, sizeof(unused)); |
||||
|
|
||||
|
for (int i = 0; i < num_clients; i++) { |
||||
|
auto c = &clients[i]; |
||||
|
if (c->permissions == 0) continue; // skip deleted entries
|
||||
|
|
||||
|
bool success = (file.write(c->id.pub_key, 32) == 32); |
||||
|
success = success && (file.write((uint8_t *) &c->permissions, 1) == 1); |
||||
|
success = success && (file.write(unused, 6) == 6); |
||||
|
success = success && (file.write((uint8_t *)&c->out_path_len, 1) == 1); |
||||
|
success = success && (file.write(c->out_path, 64) == 64); |
||||
|
success = success && (file.write(c->shared_secret, PUB_KEY_SIZE) == PUB_KEY_SIZE); |
||||
|
|
||||
|
if (!success) break; // write failed
|
||||
|
} |
||||
|
file.close(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
ClientInfo* ClientACL::getClient(const uint8_t* pubkey, int key_len) { |
||||
|
for (int i = 0; i < num_clients; i++) { |
||||
|
if (memcmp(pubkey, clients[i].id.pub_key, key_len) == 0) return &clients[i]; // already known
|
||||
|
} |
||||
|
return NULL; // not found
|
||||
|
} |
||||
|
|
||||
|
ClientInfo* ClientACL::putClient(const mesh::Identity& id, uint8_t init_perms) { |
||||
|
uint32_t min_time = 0xFFFFFFFF; |
||||
|
ClientInfo* oldest = &clients[MAX_CLIENTS - 1]; |
||||
|
for (int i = 0; i < num_clients; i++) { |
||||
|
if (id.matches(clients[i].id)) return &clients[i]; // already known
|
||||
|
if (!clients[i].isAdmin() && clients[i].last_activity < min_time) { |
||||
|
oldest = &clients[i]; |
||||
|
min_time = oldest->last_activity; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
ClientInfo* c; |
||||
|
if (num_clients < MAX_CLIENTS) { |
||||
|
c = &clients[num_clients++]; |
||||
|
} else { |
||||
|
c = oldest; // evict least active contact
|
||||
|
} |
||||
|
memset(c, 0, sizeof(*c)); |
||||
|
c->permissions = init_perms; |
||||
|
c->id = id; |
||||
|
c->out_path_len = -1; // initially out_path is unknown
|
||||
|
return c; |
||||
|
} |
||||
|
|
||||
|
bool ClientACL::applyPermissions(const mesh::LocalIdentity& self_id, const uint8_t* pubkey, int key_len, uint8_t perms) { |
||||
|
ClientInfo* c; |
||||
|
if ((perms & PERM_ACL_ROLE_MASK) == PERM_ACL_GUEST) { // guest role is not persisted in contacts
|
||||
|
c = getClient(pubkey, key_len); |
||||
|
if (c == NULL) return false; // partial pubkey not found
|
||||
|
|
||||
|
num_clients--; // delete from contacts[]
|
||||
|
int i = c - clients; |
||||
|
while (i < num_clients) { |
||||
|
clients[i] = clients[i + 1]; |
||||
|
i++; |
||||
|
} |
||||
|
} else { |
||||
|
if (key_len < PUB_KEY_SIZE) return false; // need complete pubkey when adding/modifying
|
||||
|
|
||||
|
mesh::Identity id(pubkey); |
||||
|
c = putClient(id, 0); |
||||
|
|
||||
|
c->permissions = perms; // update their permissions
|
||||
|
self_id.calcSharedSecret(c->shared_secret, pubkey); |
||||
|
} |
||||
|
return true; |
||||
|
} |
||||
@ -0,0 +1,47 @@ |
|||||
|
#pragma once |
||||
|
|
||||
|
#include <Arduino.h> // needed for PlatformIO |
||||
|
#include <Mesh.h> |
||||
|
#include <helpers/IdentityStore.h> |
||||
|
|
||||
|
#define PERM_ACL_ROLE_MASK 3 // lower 2 bits
|
||||
|
#define PERM_ACL_GUEST 0 |
||||
|
#define PERM_ACL_READ_ONLY 1 |
||||
|
#define PERM_ACL_READ_WRITE 2 |
||||
|
#define PERM_ACL_ADMIN 3 |
||||
|
|
||||
|
struct ClientInfo { |
||||
|
mesh::Identity id; |
||||
|
uint8_t permissions; |
||||
|
int8_t out_path_len; |
||||
|
uint8_t out_path[MAX_PATH_SIZE]; |
||||
|
uint8_t shared_secret[PUB_KEY_SIZE]; |
||||
|
uint32_t last_timestamp; // by THEIR clock (transient)
|
||||
|
uint32_t last_activity; // by OUR clock (transient)
|
||||
|
|
||||
|
bool isAdmin() const { return (permissions & PERM_ACL_ROLE_MASK) == PERM_ACL_ADMIN; } |
||||
|
}; |
||||
|
|
||||
|
#ifndef MAX_CLIENTS |
||||
|
#define MAX_CLIENTS 20 |
||||
|
#endif |
||||
|
|
||||
|
class ClientACL { |
||||
|
ClientInfo clients[MAX_CLIENTS]; |
||||
|
int num_clients; |
||||
|
|
||||
|
public: |
||||
|
ClientACL() { |
||||
|
memset(clients, 0, sizeof(clients)); |
||||
|
num_clients = 0; |
||||
|
} |
||||
|
void load(FILESYSTEM* _fs); |
||||
|
void save(FILESYSTEM* _fs); |
||||
|
|
||||
|
ClientInfo* getClient(const uint8_t* pubkey, int key_len); |
||||
|
ClientInfo* putClient(const mesh::Identity& id, uint8_t init_perms); |
||||
|
bool applyPermissions(const mesh::LocalIdentity& self_id, const uint8_t* pubkey, int key_len, uint8_t perms); |
||||
|
|
||||
|
int getNumClients() const { return num_clients; } |
||||
|
ClientInfo* getClientByIdx(int idx) { return &clients[idx]; } |
||||
|
}; |
||||
@ -0,0 +1,36 @@ |
|||||
|
#include "BridgeBase.h" |
||||
|
|
||||
|
#include <Arduino.h> |
||||
|
|
||||
|
const char *BridgeBase::getLogDateTime() { |
||||
|
static char tmp[32]; |
||||
|
uint32_t now = _rtc->getCurrentTime(); |
||||
|
DateTime dt = DateTime(now); |
||||
|
sprintf(tmp, "%02d:%02d:%02d - %d/%d/%d U", dt.hour(), dt.minute(), dt.second(), dt.day(), dt.month(), |
||||
|
dt.year()); |
||||
|
return tmp; |
||||
|
} |
||||
|
|
||||
|
uint16_t BridgeBase::fletcher16(const uint8_t *data, size_t len) { |
||||
|
uint8_t sum1 = 0, sum2 = 0; |
||||
|
|
||||
|
for (size_t i = 0; i < len; i++) { |
||||
|
sum1 = (sum1 + data[i]) % 255; |
||||
|
sum2 = (sum2 + sum1) % 255; |
||||
|
} |
||||
|
|
||||
|
return (sum2 << 8) | sum1; |
||||
|
} |
||||
|
|
||||
|
bool BridgeBase::validateChecksum(const uint8_t *data, size_t len, uint16_t received_checksum) { |
||||
|
uint16_t calculated_checksum = fletcher16(data, len); |
||||
|
return received_checksum == calculated_checksum; |
||||
|
} |
||||
|
|
||||
|
void BridgeBase::handleReceivedPacket(mesh::Packet *packet) { |
||||
|
if (!_seen_packets.hasSeen(packet)) { |
||||
|
_mgr->queueInbound(packet, millis() + BRIDGE_DELAY); |
||||
|
} else { |
||||
|
_mgr->free(packet); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,112 @@ |
|||||
|
#pragma once |
||||
|
|
||||
|
#include "helpers/AbstractBridge.h" |
||||
|
#include "helpers/SimpleMeshTables.h" |
||||
|
|
||||
|
#include <RTClib.h> |
||||
|
|
||||
|
/**
|
||||
|
* @brief Base class implementing common bridge functionality |
||||
|
* |
||||
|
* This class provides common functionality used by different bridge implementations |
||||
|
* like packet tracking, checksum calculation, timestamping, and duplicate detection. |
||||
|
* |
||||
|
* Features: |
||||
|
* - Fletcher-16 checksum calculation for data integrity |
||||
|
* - Packet duplicate detection using SimpleMeshTables |
||||
|
* - Common timestamp formatting for debug logging |
||||
|
* - Shared packet management and queuing logic |
||||
|
*/ |
||||
|
class BridgeBase : public AbstractBridge { |
||||
|
public: |
||||
|
virtual ~BridgeBase() = default; |
||||
|
|
||||
|
/**
|
||||
|
* @brief Common magic number used by all bridge implementations for packet identification |
||||
|
* |
||||
|
* This magic number is placed at the beginning of bridge packets to identify |
||||
|
* them as mesh bridge packets and provide frame synchronization. |
||||
|
*/ |
||||
|
static constexpr uint16_t BRIDGE_PACKET_MAGIC = 0xC03E; |
||||
|
|
||||
|
/**
|
||||
|
* @brief Common field sizes used by bridge implementations |
||||
|
* |
||||
|
* These constants define the size of common packet fields used across bridges. |
||||
|
* BRIDGE_MAGIC_SIZE is used by all bridges for packet identification. |
||||
|
* BRIDGE_LENGTH_SIZE is used by bridges that need explicit length fields (like RS232). |
||||
|
* BRIDGE_CHECKSUM_SIZE is used by all bridges for Fletcher-16 checksums. |
||||
|
*/ |
||||
|
static constexpr uint16_t BRIDGE_MAGIC_SIZE = sizeof(BRIDGE_PACKET_MAGIC); |
||||
|
static constexpr uint16_t BRIDGE_LENGTH_SIZE = sizeof(uint16_t); |
||||
|
static constexpr uint16_t BRIDGE_CHECKSUM_SIZE = sizeof(uint16_t); |
||||
|
|
||||
|
/**
|
||||
|
* @brief Default delay in milliseconds for scheduling inbound packet processing |
||||
|
* |
||||
|
* It provides a buffer to prevent immediate processing conflicts in the mesh network. |
||||
|
* Used in handleReceivedPacket() as: millis() + BRIDGE_DELAY |
||||
|
*/ |
||||
|
static constexpr uint16_t BRIDGE_DELAY = 500; // TODO: maybe too high ?
|
||||
|
|
||||
|
protected: |
||||
|
/** Packet manager for allocating and queuing mesh packets */ |
||||
|
mesh::PacketManager *_mgr; |
||||
|
|
||||
|
/** RTC clock for timestamping debug messages */ |
||||
|
mesh::RTCClock *_rtc; |
||||
|
|
||||
|
/** Tracks seen packets to prevent loops in broadcast communications */ |
||||
|
SimpleMeshTables _seen_packets; |
||||
|
|
||||
|
/**
|
||||
|
* @brief Constructs a BridgeBase instance |
||||
|
* |
||||
|
* @param mgr PacketManager for allocating and queuing packets |
||||
|
* @param rtc RTCClock for timestamping debug messages |
||||
|
*/ |
||||
|
BridgeBase(mesh::PacketManager *mgr, mesh::RTCClock *rtc) : _mgr(mgr), _rtc(rtc) {} |
||||
|
|
||||
|
/**
|
||||
|
* @brief Gets formatted date/time string for logging |
||||
|
* |
||||
|
* Format: "HH:MM:SS - DD/MM/YYYY U" |
||||
|
* |
||||
|
* @return Formatted date/time string |
||||
|
*/ |
||||
|
const char *getLogDateTime(); |
||||
|
|
||||
|
/**
|
||||
|
* @brief Calculate Fletcher-16 checksum |
||||
|
* |
||||
|
* Based on: https://en.wikipedia.org/wiki/Fletcher%27s_checksum
|
||||
|
* Used to verify data integrity of received packets |
||||
|
* |
||||
|
* @param data Pointer to data to calculate checksum for |
||||
|
* @param len Length of data in bytes |
||||
|
* @return Calculated Fletcher-16 checksum |
||||
|
*/ |
||||
|
static uint16_t fletcher16(const uint8_t *data, size_t len); |
||||
|
|
||||
|
/**
|
||||
|
* @brief Validate received checksum against calculated checksum |
||||
|
* |
||||
|
* @param data Pointer to data to validate |
||||
|
* @param len Length of data in bytes |
||||
|
* @param received_checksum Checksum received with data |
||||
|
* @return true if checksum is valid, false otherwise |
||||
|
*/ |
||||
|
bool validateChecksum(const uint8_t *data, size_t len, uint16_t received_checksum); |
||||
|
|
||||
|
/**
|
||||
|
* @brief Common packet handling for received packets |
||||
|
* |
||||
|
* Implements the standard pattern used by all bridges: |
||||
|
* - Check if packet was seen before using _seen_packets.hasSeen() |
||||
|
* - Queue packet for mesh processing if not seen before |
||||
|
* - Free packet if already seen to prevent duplicates |
||||
|
* |
||||
|
* @param packet The received mesh packet |
||||
|
*/ |
||||
|
void handleReceivedPacket(mesh::Packet *packet); |
||||
|
}; |
||||
@ -0,0 +1,196 @@ |
|||||
|
#include "ESPNowBridge.h" |
||||
|
|
||||
|
#include <WiFi.h> |
||||
|
#include <esp_wifi.h> |
||||
|
|
||||
|
#ifdef WITH_ESPNOW_BRIDGE |
||||
|
|
||||
|
// Static member to handle callbacks
|
||||
|
ESPNowBridge *ESPNowBridge::_instance = nullptr; |
||||
|
|
||||
|
// Static callback wrappers
|
||||
|
void ESPNowBridge::recv_cb(const uint8_t *mac, const uint8_t *data, int32_t len) { |
||||
|
if (_instance) { |
||||
|
_instance->onDataRecv(mac, data, len); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
void ESPNowBridge::send_cb(const uint8_t *mac, esp_now_send_status_t status) { |
||||
|
if (_instance) { |
||||
|
_instance->onDataSent(mac, status); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
ESPNowBridge::ESPNowBridge(mesh::PacketManager *mgr, mesh::RTCClock *rtc) |
||||
|
: BridgeBase(mgr, rtc), _rx_buffer_pos(0) { |
||||
|
_instance = this; |
||||
|
} |
||||
|
|
||||
|
void ESPNowBridge::begin() { |
||||
|
// Initialize WiFi in station mode
|
||||
|
WiFi.mode(WIFI_STA); |
||||
|
|
||||
|
// Initialize ESP-NOW
|
||||
|
if (esp_now_init() != ESP_OK) { |
||||
|
Serial.printf("%s: ESPNOW BRIDGE: Error initializing ESP-NOW\n", getLogDateTime()); |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
// Register callbacks
|
||||
|
esp_now_register_recv_cb(recv_cb); |
||||
|
esp_now_register_send_cb(send_cb); |
||||
|
|
||||
|
// Add broadcast peer
|
||||
|
esp_now_peer_info_t peerInfo = {}; |
||||
|
memset(&peerInfo, 0, sizeof(peerInfo)); |
||||
|
memset(peerInfo.peer_addr, 0xFF, ESP_NOW_ETH_ALEN); // Broadcast address
|
||||
|
peerInfo.channel = 0; |
||||
|
peerInfo.encrypt = false; |
||||
|
|
||||
|
if (esp_now_add_peer(&peerInfo) != ESP_OK) { |
||||
|
Serial.printf("%s: ESPNOW BRIDGE: Failed to add broadcast peer\n", getLogDateTime()); |
||||
|
return; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
void ESPNowBridge::loop() { |
||||
|
// Nothing to do here - ESP-NOW is callback based
|
||||
|
} |
||||
|
|
||||
|
void ESPNowBridge::xorCrypt(uint8_t *data, size_t len) { |
||||
|
size_t keyLen = strlen(_secret); |
||||
|
for (size_t i = 0; i < len; i++) { |
||||
|
data[i] ^= _secret[i % keyLen]; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
void ESPNowBridge::onDataRecv(const uint8_t *mac, const uint8_t *data, int32_t len) { |
||||
|
// Ignore packets that are too small to contain header + checksum
|
||||
|
if (len < (BRIDGE_MAGIC_SIZE + BRIDGE_CHECKSUM_SIZE)) { |
||||
|
#if MESH_PACKET_LOGGING |
||||
|
Serial.printf("%s: ESPNOW BRIDGE: RX packet too small, len=%d\n", getLogDateTime(), len); |
||||
|
#endif |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
// Validate total packet size
|
||||
|
if (len > MAX_ESPNOW_PACKET_SIZE) { |
||||
|
#if MESH_PACKET_LOGGING |
||||
|
Serial.printf("%s: ESPNOW BRIDGE: RX packet too large, len=%d\n", getLogDateTime(), len); |
||||
|
#endif |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
// Check packet header magic
|
||||
|
uint16_t received_magic = (data[0] << 8) | data[1]; |
||||
|
if (received_magic != BRIDGE_PACKET_MAGIC) { |
||||
|
#if MESH_PACKET_LOGGING |
||||
|
Serial.printf("%s: ESPNOW BRIDGE: RX invalid magic 0x%04X\n", getLogDateTime(), received_magic); |
||||
|
#endif |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
// Make a copy we can decrypt
|
||||
|
uint8_t decrypted[MAX_ESPNOW_PACKET_SIZE]; |
||||
|
const size_t encryptedDataLen = len - BRIDGE_MAGIC_SIZE; |
||||
|
memcpy(decrypted, data + BRIDGE_MAGIC_SIZE, encryptedDataLen); |
||||
|
|
||||
|
// Try to decrypt (checksum + payload)
|
||||
|
xorCrypt(decrypted, encryptedDataLen); |
||||
|
|
||||
|
// Validate checksum
|
||||
|
uint16_t received_checksum = (decrypted[0] << 8) | decrypted[1]; |
||||
|
const size_t payloadLen = encryptedDataLen - BRIDGE_CHECKSUM_SIZE; |
||||
|
|
||||
|
if (!validateChecksum(decrypted + BRIDGE_CHECKSUM_SIZE, payloadLen, received_checksum)) { |
||||
|
// Failed to decrypt - likely from a different network
|
||||
|
#if MESH_PACKET_LOGGING |
||||
|
Serial.printf("%s: ESPNOW BRIDGE: RX checksum mismatch, rcv=0x%04X\n", getLogDateTime(), |
||||
|
received_checksum); |
||||
|
#endif |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
#if MESH_PACKET_LOGGING |
||||
|
Serial.printf("%s: ESPNOW BRIDGE: RX, payload_len=%d\n", getLogDateTime(), payloadLen); |
||||
|
#endif |
||||
|
|
||||
|
// Create mesh packet
|
||||
|
mesh::Packet *pkt = _instance->_mgr->allocNew(); |
||||
|
if (!pkt) return; |
||||
|
|
||||
|
if (pkt->readFrom(decrypted + BRIDGE_CHECKSUM_SIZE, payloadLen)) { |
||||
|
_instance->onPacketReceived(pkt); |
||||
|
} else { |
||||
|
_instance->_mgr->free(pkt); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
void ESPNowBridge::onDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) { |
||||
|
// Could add transmission error handling here if needed
|
||||
|
} |
||||
|
|
||||
|
void ESPNowBridge::onPacketReceived(mesh::Packet *packet) { |
||||
|
handleReceivedPacket(packet); |
||||
|
} |
||||
|
|
||||
|
void ESPNowBridge::onPacketTransmitted(mesh::Packet *packet) { |
||||
|
// First validate the packet pointer
|
||||
|
if (!packet) { |
||||
|
#if MESH_PACKET_LOGGING |
||||
|
Serial.printf("%s: ESPNOW BRIDGE: TX invalid packet pointer\n", getLogDateTime()); |
||||
|
#endif |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
if (!_seen_packets.hasSeen(packet)) { |
||||
|
|
||||
|
// Create a temporary buffer just for size calculation and reuse for actual writing
|
||||
|
uint8_t sizingBuffer[MAX_PAYLOAD_SIZE]; |
||||
|
uint16_t meshPacketLen = packet->writeTo(sizingBuffer); |
||||
|
|
||||
|
// Check if packet fits within our maximum payload size
|
||||
|
if (meshPacketLen > MAX_PAYLOAD_SIZE) { |
||||
|
#if MESH_PACKET_LOGGING |
||||
|
Serial.printf("%s: ESPNOW BRIDGE: TX packet too large (payload=%d, max=%d)\n", getLogDateTime(), |
||||
|
meshPacketLen, MAX_PAYLOAD_SIZE); |
||||
|
#endif |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
uint8_t buffer[MAX_ESPNOW_PACKET_SIZE]; |
||||
|
|
||||
|
// Write magic header (2 bytes)
|
||||
|
buffer[0] = (BRIDGE_PACKET_MAGIC >> 8) & 0xFF; |
||||
|
buffer[1] = BRIDGE_PACKET_MAGIC & 0xFF; |
||||
|
|
||||
|
// Write packet payload starting after magic header and checksum
|
||||
|
const size_t packetOffset = BRIDGE_MAGIC_SIZE + BRIDGE_CHECKSUM_SIZE; |
||||
|
memcpy(buffer + packetOffset, sizingBuffer, meshPacketLen); |
||||
|
|
||||
|
// Calculate and add checksum (only of the payload)
|
||||
|
uint16_t checksum = fletcher16(buffer + packetOffset, meshPacketLen); |
||||
|
buffer[2] = (checksum >> 8) & 0xFF; // High byte
|
||||
|
buffer[3] = checksum & 0xFF; // Low byte
|
||||
|
|
||||
|
// Encrypt payload and checksum (not including magic header)
|
||||
|
xorCrypt(buffer + BRIDGE_MAGIC_SIZE, meshPacketLen + BRIDGE_CHECKSUM_SIZE); |
||||
|
|
||||
|
// Total packet size: magic header + checksum + payload
|
||||
|
const size_t totalPacketSize = BRIDGE_MAGIC_SIZE + BRIDGE_CHECKSUM_SIZE + meshPacketLen; |
||||
|
|
||||
|
// Broadcast using ESP-NOW
|
||||
|
uint8_t broadcastAddress[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; |
||||
|
esp_err_t result = esp_now_send(broadcastAddress, buffer, totalPacketSize); |
||||
|
|
||||
|
#if MESH_PACKET_LOGGING |
||||
|
if (result == ESP_OK) { |
||||
|
Serial.printf("%s: ESPNOW BRIDGE: TX, len=%d\n", getLogDateTime(), meshPacketLen); |
||||
|
} else { |
||||
|
Serial.printf("%s: ESPNOW BRIDGE: TX FAILED!\n", getLogDateTime()); |
||||
|
} |
||||
|
#endif |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
#endif |
||||
@ -0,0 +1,156 @@ |
|||||
|
#pragma once |
||||
|
|
||||
|
#include "MeshCore.h" |
||||
|
#include "esp_now.h" |
||||
|
#include "helpers/bridges/BridgeBase.h" |
||||
|
|
||||
|
#ifdef WITH_ESPNOW_BRIDGE |
||||
|
|
||||
|
#ifndef WITH_ESPNOW_BRIDGE_SECRET |
||||
|
#error WITH_ESPNOW_BRIDGE_SECRET must be defined to use ESPNowBridge |
||||
|
#endif |
||||
|
|
||||
|
/**
|
||||
|
* @brief Bridge implementation using ESP-NOW protocol for packet transport |
||||
|
* |
||||
|
* This bridge enables mesh packet transport over ESP-NOW, a connectionless communication |
||||
|
* protocol provided by Espressif that allows ESP32 devices to communicate directly |
||||
|
* without WiFi router infrastructure. |
||||
|
* |
||||
|
* Features: |
||||
|
* - Broadcast-based communication (all bridges receive all packets) |
||||
|
* - Network isolation using XOR encryption with shared secret |
||||
|
* - Duplicate packet detection using SimpleMeshTables tracking |
||||
|
* - Maximum packet size of 250 bytes (ESP-NOW limitation) |
||||
|
* |
||||
|
* Packet Structure: |
||||
|
* [2 bytes] Magic Header - Used to identify ESPNowBridge packets |
||||
|
* [2 bytes] Fletcher-16 checksum of encrypted payload (calculated over payload only) |
||||
|
* [246 bytes max] Encrypted payload containing the mesh packet |
||||
|
* |
||||
|
* The Fletcher-16 checksum is used to validate packet integrity and detect |
||||
|
* corrupted or tampered packets. It's calculated over the encrypted payload |
||||
|
* and provides a simple but effective way to verify packets are both |
||||
|
* uncorrupted and from the same network (since the checksum is calculated |
||||
|
* after encryption). |
||||
|
* |
||||
|
* Configuration: |
||||
|
* - Define WITH_ESPNOW_BRIDGE to enable this bridge |
||||
|
* - Define WITH_ESPNOW_BRIDGE_SECRET with a string to set the network encryption key |
||||
|
* |
||||
|
* Network Isolation: |
||||
|
* Multiple independent mesh networks can coexist by using different |
||||
|
* WITH_ESPNOW_BRIDGE_SECRET values. Packets encrypted with a different key will |
||||
|
* fail the checksum validation and be discarded. |
||||
|
*/ |
||||
|
class ESPNowBridge : public BridgeBase { |
||||
|
private: |
||||
|
static ESPNowBridge *_instance; |
||||
|
static void recv_cb(const uint8_t *mac, const uint8_t *data, int32_t len); |
||||
|
static void send_cb(const uint8_t *mac, esp_now_send_status_t status); |
||||
|
|
||||
|
/**
|
||||
|
* ESP-NOW Protocol Structure: |
||||
|
* - ESP-NOW header: 20 bytes (handled by ESP-NOW protocol) |
||||
|
* - ESP-NOW payload: 250 bytes maximum |
||||
|
* Total ESP-NOW packet: 270 bytes |
||||
|
* |
||||
|
* Our Bridge Packet Structure (must fit in ESP-NOW payload): |
||||
|
* - Magic header: 2 bytes |
||||
|
* - Checksum: 2 bytes |
||||
|
* - Available payload: 246 bytes |
||||
|
*/ |
||||
|
static const size_t MAX_ESPNOW_PACKET_SIZE = 250; |
||||
|
|
||||
|
/**
|
||||
|
* Size constants for packet parsing |
||||
|
*/ |
||||
|
static const size_t MAX_PAYLOAD_SIZE = MAX_ESPNOW_PACKET_SIZE - (BRIDGE_MAGIC_SIZE + BRIDGE_CHECKSUM_SIZE); |
||||
|
|
||||
|
/** Buffer for receiving ESP-NOW packets */ |
||||
|
uint8_t _rx_buffer[MAX_ESPNOW_PACKET_SIZE]; |
||||
|
|
||||
|
/** Current position in receive buffer */ |
||||
|
size_t _rx_buffer_pos; |
||||
|
|
||||
|
/**
|
||||
|
* Network encryption key from build define |
||||
|
* Must be defined with WITH_ESPNOW_BRIDGE_SECRET |
||||
|
* Used for XOR encryption to isolate different mesh networks |
||||
|
*/ |
||||
|
const char *_secret = WITH_ESPNOW_BRIDGE_SECRET; |
||||
|
|
||||
|
/**
|
||||
|
* Performs XOR encryption/decryption of data |
||||
|
* |
||||
|
* Uses WITH_ESPNOW_BRIDGE_SECRET as the key in a simple XOR operation. |
||||
|
* The same operation is used for both encryption and decryption. |
||||
|
* While not cryptographically secure, it provides basic network isolation. |
||||
|
* |
||||
|
* @param data Pointer to data to encrypt/decrypt |
||||
|
* @param len Length of data in bytes |
||||
|
*/ |
||||
|
void xorCrypt(uint8_t *data, size_t len); |
||||
|
|
||||
|
/**
|
||||
|
* ESP-NOW receive callback |
||||
|
* Called by ESP-NOW when a packet is received |
||||
|
* |
||||
|
* @param mac Source MAC address |
||||
|
* @param data Received data |
||||
|
* @param len Length of received data |
||||
|
*/ |
||||
|
void onDataRecv(const uint8_t *mac, const uint8_t *data, int32_t len); |
||||
|
|
||||
|
/**
|
||||
|
* ESP-NOW send callback |
||||
|
* Called by ESP-NOW after a transmission attempt |
||||
|
* |
||||
|
* @param mac_addr Destination MAC address |
||||
|
* @param status Transmission status |
||||
|
*/ |
||||
|
void onDataSent(const uint8_t *mac_addr, esp_now_send_status_t status); |
||||
|
|
||||
|
public: |
||||
|
/**
|
||||
|
* Constructs an ESPNowBridge instance |
||||
|
* |
||||
|
* @param mgr PacketManager for allocating and queuing packets |
||||
|
* @param rtc RTCClock for timestamping debug messages |
||||
|
*/ |
||||
|
ESPNowBridge(mesh::PacketManager *mgr, mesh::RTCClock *rtc); |
||||
|
|
||||
|
/**
|
||||
|
* Initializes the ESP-NOW bridge |
||||
|
* |
||||
|
* - Configures WiFi in station mode |
||||
|
* - Initializes ESP-NOW protocol |
||||
|
* - Registers callbacks |
||||
|
* - Sets up broadcast peer |
||||
|
*/ |
||||
|
void begin() override; |
||||
|
|
||||
|
/**
|
||||
|
* Main loop handler |
||||
|
* ESP-NOW is callback-based, so this is currently empty |
||||
|
*/ |
||||
|
void loop() override; |
||||
|
|
||||
|
/**
|
||||
|
* Called when a packet is received via ESP-NOW |
||||
|
* Queues the packet for mesh processing if not seen before |
||||
|
* |
||||
|
* @param packet The received mesh packet |
||||
|
*/ |
||||
|
void onPacketReceived(mesh::Packet *packet) override; |
||||
|
|
||||
|
/**
|
||||
|
* Called when a packet needs to be transmitted via ESP-NOW |
||||
|
* Encrypts and broadcasts the packet if not seen before |
||||
|
* |
||||
|
* @param packet The mesh packet to transmit |
||||
|
*/ |
||||
|
void onPacketTransmitted(mesh::Packet *packet) override; |
||||
|
}; |
||||
|
|
||||
|
#endif |
||||
@ -0,0 +1,147 @@ |
|||||
|
#include "RS232Bridge.h" |
||||
|
|
||||
|
#include <HardwareSerial.h> |
||||
|
|
||||
|
#ifdef WITH_RS232_BRIDGE |
||||
|
|
||||
|
RS232Bridge::RS232Bridge(Stream &serial, mesh::PacketManager *mgr, mesh::RTCClock *rtc) |
||||
|
: BridgeBase(mgr, rtc), _serial(&serial) {} |
||||
|
|
||||
|
void RS232Bridge::begin() { |
||||
|
#if !defined(WITH_RS232_BRIDGE_RX) || !defined(WITH_RS232_BRIDGE_TX) |
||||
|
#error "WITH_RS232_BRIDGE_RX and WITH_RS232_BRIDGE_TX must be defined" |
||||
|
#endif |
||||
|
|
||||
|
#if defined(ESP32) |
||||
|
((HardwareSerial *)_serial)->setPins(WITH_RS232_BRIDGE_RX, WITH_RS232_BRIDGE_TX); |
||||
|
#elif defined(NRF52_PLATFORM) |
||||
|
((HardwareSerial *)_serial)->setPins(WITH_RS232_BRIDGE_RX, WITH_RS232_BRIDGE_TX); |
||||
|
#elif defined(RP2040_PLATFORM) |
||||
|
((SerialUART *)_serial)->setRX(WITH_RS232_BRIDGE_RX); |
||||
|
((SerialUART *)_serial)->setTX(WITH_RS232_BRIDGE_TX); |
||||
|
#elif defined(STM32_PLATFORM) |
||||
|
((HardwareSerial *)_serial)->setRx(WITH_RS232_BRIDGE_RX); |
||||
|
((HardwareSerial *)_serial)->setTx(WITH_RS232_BRIDGE_TX); |
||||
|
#else |
||||
|
#error RS232Bridge was not tested on the current platform |
||||
|
#endif |
||||
|
((HardwareSerial *)_serial)->begin(115200); |
||||
|
} |
||||
|
|
||||
|
void RS232Bridge::onPacketTransmitted(mesh::Packet *packet) { |
||||
|
// First validate the packet pointer
|
||||
|
if (!packet) { |
||||
|
#if MESH_PACKET_LOGGING |
||||
|
Serial.printf("%s: RS232 BRIDGE: TX invalid packet pointer\n", getLogDateTime()); |
||||
|
#endif |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
if (!_seen_packets.hasSeen(packet)) { |
||||
|
|
||||
|
uint8_t buffer[MAX_SERIAL_PACKET_SIZE]; |
||||
|
uint16_t len = packet->writeTo(buffer + 4); |
||||
|
|
||||
|
// Check if packet fits within our maximum payload size
|
||||
|
if (len > (MAX_TRANS_UNIT + 1)) { |
||||
|
#if MESH_PACKET_LOGGING |
||||
|
Serial.printf("%s: RS232 BRIDGE: TX packet too large (payload=%d, max=%d)\n", getLogDateTime(), len, |
||||
|
MAX_TRANS_UNIT + 1); |
||||
|
#endif |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
// Build packet header
|
||||
|
buffer[0] = (BRIDGE_PACKET_MAGIC >> 8) & 0xFF; // Magic high byte
|
||||
|
buffer[1] = BRIDGE_PACKET_MAGIC & 0xFF; // Magic low byte
|
||||
|
buffer[2] = (len >> 8) & 0xFF; // Length high byte
|
||||
|
buffer[3] = len & 0xFF; // Length low byte
|
||||
|
|
||||
|
// Calculate checksum over the payload
|
||||
|
uint16_t checksum = fletcher16(buffer + 4, len); |
||||
|
buffer[4 + len] = (checksum >> 8) & 0xFF; // Checksum high byte
|
||||
|
buffer[5 + len] = checksum & 0xFF; // Checksum low byte
|
||||
|
|
||||
|
// Send complete packet
|
||||
|
_serial->write(buffer, len + SERIAL_OVERHEAD); |
||||
|
|
||||
|
#if MESH_PACKET_LOGGING |
||||
|
Serial.printf("%s: RS232 BRIDGE: TX, len=%d crc=0x%04x\n", getLogDateTime(), len, checksum); |
||||
|
#endif |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
void RS232Bridge::loop() { |
||||
|
while (_serial->available()) { |
||||
|
uint8_t b = _serial->read(); |
||||
|
|
||||
|
if (_rx_buffer_pos < 2) { |
||||
|
// Waiting for magic word
|
||||
|
if ((_rx_buffer_pos == 0 && b == ((BRIDGE_PACKET_MAGIC >> 8) & 0xFF)) || |
||||
|
(_rx_buffer_pos == 1 && b == (BRIDGE_PACKET_MAGIC & 0xFF))) { |
||||
|
_rx_buffer[_rx_buffer_pos++] = b; |
||||
|
} else { |
||||
|
// Invalid magic byte, reset and start over
|
||||
|
_rx_buffer_pos = 0; |
||||
|
// Check if this byte could be the start of a new magic word
|
||||
|
if (b == ((BRIDGE_PACKET_MAGIC >> 8) & 0xFF)) { |
||||
|
_rx_buffer[_rx_buffer_pos++] = b; |
||||
|
} |
||||
|
} |
||||
|
} else { |
||||
|
// Reading length, payload, and checksum
|
||||
|
_rx_buffer[_rx_buffer_pos++] = b; |
||||
|
|
||||
|
if (_rx_buffer_pos >= 4) { |
||||
|
uint16_t len = (_rx_buffer[2] << 8) | _rx_buffer[3]; |
||||
|
|
||||
|
// Validate length field
|
||||
|
if (len > (MAX_TRANS_UNIT + 1)) { |
||||
|
#if MESH_PACKET_LOGGING |
||||
|
Serial.printf("%s: RS232 BRIDGE: RX invalid length %d, resetting\n", getLogDateTime(), len); |
||||
|
#endif |
||||
|
_rx_buffer_pos = 0; // Invalid length, reset
|
||||
|
continue; |
||||
|
} |
||||
|
|
||||
|
if (_rx_buffer_pos == len + SERIAL_OVERHEAD) { // Full packet received
|
||||
|
uint16_t received_checksum = (_rx_buffer[4 + len] << 8) | _rx_buffer[5 + len]; |
||||
|
|
||||
|
if (validateChecksum(_rx_buffer + 4, len, received_checksum)) { |
||||
|
#if MESH_PACKET_LOGGING |
||||
|
Serial.printf("%s: RS232 BRIDGE: RX, len=%d crc=0x%04x\n", getLogDateTime(), len, |
||||
|
received_checksum); |
||||
|
#endif |
||||
|
mesh::Packet *pkt = _mgr->allocNew(); |
||||
|
if (pkt) { |
||||
|
if (pkt->readFrom(_rx_buffer + 4, len)) { |
||||
|
onPacketReceived(pkt); |
||||
|
} else { |
||||
|
#if MESH_PACKET_LOGGING |
||||
|
Serial.printf("%s: RS232 BRIDGE: RX failed to parse packet\n", getLogDateTime()); |
||||
|
#endif |
||||
|
_mgr->free(pkt); |
||||
|
} |
||||
|
} else { |
||||
|
#if MESH_PACKET_LOGGING |
||||
|
Serial.printf("%s: RS232 BRIDGE: RX failed to allocate packet\n", getLogDateTime()); |
||||
|
#endif |
||||
|
} |
||||
|
} else { |
||||
|
#if MESH_PACKET_LOGGING |
||||
|
Serial.printf("%s: RS232 BRIDGE: RX checksum mismatch, rcv=0x%04x\n", getLogDateTime(), |
||||
|
received_checksum); |
||||
|
#endif |
||||
|
} |
||||
|
_rx_buffer_pos = 0; // Reset for next packet
|
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
void RS232Bridge::onPacketReceived(mesh::Packet *packet) { |
||||
|
handleReceivedPacket(packet); |
||||
|
} |
||||
|
|
||||
|
#endif |
||||
@ -0,0 +1,141 @@ |
|||||
|
#pragma once |
||||
|
|
||||
|
#include "helpers/bridges/BridgeBase.h" |
||||
|
|
||||
|
#include <Stream.h> |
||||
|
|
||||
|
#ifdef WITH_RS232_BRIDGE |
||||
|
|
||||
|
/**
|
||||
|
* @brief Bridge implementation using RS232/UART protocol for packet transport |
||||
|
* |
||||
|
* This bridge enables mesh packet transport over serial/UART connections, |
||||
|
* allowing nodes to communicate over wired serial links. It implements a simple |
||||
|
* packet framing protocol with checksums for reliable transfer. |
||||
|
* |
||||
|
* Features: |
||||
|
* - Point-to-point communication over hardware UART |
||||
|
* - Fletcher-16 checksum for data integrity verification |
||||
|
* - Magic header for packet synchronization and frame alignment |
||||
|
* - Duplicate packet detection using SimpleMeshTables tracking |
||||
|
* - Configurable RX/TX pins via build defines |
||||
|
* - Fixed baud rate at 115200 for consistent timing |
||||
|
* |
||||
|
* Packet Structure: |
||||
|
* [2 bytes] Magic Header (0xC03E) - Used to identify start of RS232Bridge packets |
||||
|
* [2 bytes] Payload Length - Length of the mesh packet payload |
||||
|
* [n bytes] Mesh Packet Payload - The actual mesh packet data |
||||
|
* [2 bytes] Fletcher-16 Checksum - Calculated over the payload for integrity verification |
||||
|
* |
||||
|
* The Fletcher-16 checksum is calculated over the mesh packet payload and provides |
||||
|
* error detection capabilities suitable for serial communication where electrical |
||||
|
* noise, timing issues, or hardware problems could corrupt data. The checksum |
||||
|
* validation ensures only valid packets are forwarded to the mesh. |
||||
|
* |
||||
|
* Configuration: |
||||
|
* - Define WITH_RS232_BRIDGE to enable this bridge |
||||
|
* - Define WITH_RS232_BRIDGE_RX with the RX pin number |
||||
|
* - Define WITH_RS232_BRIDGE_TX with the TX pin number |
||||
|
* |
||||
|
* Platform Support: |
||||
|
* Different platforms require different pin configuration methods: |
||||
|
* - ESP32: Uses HardwareSerial::setPins(rx, tx) |
||||
|
* - NRF52: Uses HardwareSerial::setPins(rx, tx) |
||||
|
* - RP2040: Uses SerialUART::setRX(rx) and SerialUART::setTX(tx) |
||||
|
* - STM32: Uses HardwareSerial::setRx(rx) and HardwareSerial::setTx(tx) |
||||
|
*/ |
||||
|
class RS232Bridge : public BridgeBase { |
||||
|
public: |
||||
|
/**
|
||||
|
* @brief Constructs an RS232Bridge instance |
||||
|
* |
||||
|
* @param serial The hardware serial port to use |
||||
|
* @param mgr PacketManager for allocating and queuing packets |
||||
|
* @param rtc RTCClock for timestamping debug messages |
||||
|
*/ |
||||
|
RS232Bridge(Stream &serial, mesh::PacketManager *mgr, mesh::RTCClock *rtc); |
||||
|
|
||||
|
/**
|
||||
|
* Initializes the RS232 bridge |
||||
|
* |
||||
|
* - Validates that RX/TX pins are defined |
||||
|
* - Configures UART pins based on target platform |
||||
|
* - Sets baud rate to 115200 for consistent communication |
||||
|
* - Platform-specific pin configuration methods are used |
||||
|
*/ |
||||
|
void begin() override; |
||||
|
|
||||
|
/**
|
||||
|
* @brief Main loop handler for processing incoming serial data |
||||
|
* |
||||
|
* Implements a state machine for packet reception: |
||||
|
* 1. Searches for magic header bytes for packet synchronization |
||||
|
* 2. Reads length field to determine expected packet size |
||||
|
* 3. Validates packet length against maximum allowed size |
||||
|
* 4. Receives complete packet payload and checksum |
||||
|
* 5. Validates Fletcher-16 checksum for data integrity |
||||
|
* 6. Creates mesh packet and forwards if valid |
||||
|
*/ |
||||
|
void loop() override; |
||||
|
|
||||
|
/**
|
||||
|
* @brief Called when a packet needs to be transmitted over serial |
||||
|
* |
||||
|
* Formats the mesh packet with RS232 framing protocol: |
||||
|
* - Adds magic header for synchronization |
||||
|
* - Includes payload length field |
||||
|
* - Calculates Fletcher-16 checksum over payload |
||||
|
* - Transmits complete framed packet |
||||
|
* - Uses duplicate detection to prevent retransmission |
||||
|
* |
||||
|
* @param packet The mesh packet to transmit |
||||
|
*/ |
||||
|
void onPacketTransmitted(mesh::Packet *packet) override; |
||||
|
|
||||
|
/**
|
||||
|
* @brief Called when a complete valid packet has been received from serial |
||||
|
* |
||||
|
* Forwards the received packet to the mesh for processing. |
||||
|
* The packet has already been validated for checksum integrity |
||||
|
* and parsed successfully at this point. |
||||
|
* |
||||
|
* @param packet The received mesh packet ready for processing |
||||
|
*/ |
||||
|
void onPacketReceived(mesh::Packet *packet) override; |
||||
|
|
||||
|
private: |
||||
|
/**
|
||||
|
* RS232 Protocol Structure: |
||||
|
* - Magic header: 2 bytes (packet identification) |
||||
|
* - Length field: 2 bytes (payload length) |
||||
|
* - Payload: variable bytes (mesh packet data) |
||||
|
* - Checksum: 2 bytes (Fletcher-16 over payload) |
||||
|
* Total overhead: 6 bytes |
||||
|
*/ |
||||
|
|
||||
|
/**
|
||||
|
* @brief The total overhead of the serial protocol in bytes. |
||||
|
* Includes: MAGIC_WORD (2) + LENGTH (2) + CHECKSUM (2) = 6 bytes |
||||
|
*/ |
||||
|
static constexpr uint16_t SERIAL_OVERHEAD = BRIDGE_MAGIC_SIZE + BRIDGE_LENGTH_SIZE + BRIDGE_CHECKSUM_SIZE; |
||||
|
|
||||
|
/**
|
||||
|
* @brief The maximum size of a complete packet on the serial line. |
||||
|
* |
||||
|
* This is calculated as the sum of: |
||||
|
* - MAX_TRANS_UNIT + 1 for the maximum mesh packet size |
||||
|
* - SERIAL_OVERHEAD for the framing (magic + length + checksum) |
||||
|
*/ |
||||
|
static constexpr uint16_t MAX_SERIAL_PACKET_SIZE = (MAX_TRANS_UNIT + 1) + SERIAL_OVERHEAD; |
||||
|
|
||||
|
/** Hardware serial port interface */ |
||||
|
Stream *_serial; |
||||
|
|
||||
|
/** Buffer for building received packets */ |
||||
|
uint8_t _rx_buffer[MAX_SERIAL_PACKET_SIZE]; |
||||
|
|
||||
|
/** Current position in the receive buffer */ |
||||
|
uint16_t _rx_buffer_pos = 0; |
||||
|
}; |
||||
|
|
||||
|
#endif |
||||
@ -0,0 +1,223 @@ |
|||||
|
#pragma once |
||||
|
|
||||
|
#include <stdint.h> |
||||
|
|
||||
|
#define LPP_DIGITAL_INPUT 0 // 1 byte
|
||||
|
#define LPP_DIGITAL_OUTPUT 1 // 1 byte
|
||||
|
#define LPP_ANALOG_INPUT 2 // 2 bytes, 0.01 signed
|
||||
|
#define LPP_ANALOG_OUTPUT 3 // 2 bytes, 0.01 signed
|
||||
|
#define LPP_GENERIC_SENSOR 100 // 4 bytes, unsigned
|
||||
|
#define LPP_LUMINOSITY 101 // 2 bytes, 1 lux unsigned
|
||||
|
#define LPP_PRESENCE 102 // 1 byte, bool
|
||||
|
#define LPP_TEMPERATURE 103 // 2 bytes, 0.1°C signed
|
||||
|
#define LPP_RELATIVE_HUMIDITY 104 // 1 byte, 0.5% unsigned
|
||||
|
#define LPP_ACCELEROMETER 113 // 2 bytes per axis, 0.001G
|
||||
|
#define LPP_BAROMETRIC_PRESSURE 115 // 2 bytes 0.1hPa unsigned
|
||||
|
#define LPP_VOLTAGE 116 // 2 bytes 0.01V unsigned
|
||||
|
#define LPP_CURRENT 117 // 2 bytes 0.001A unsigned
|
||||
|
#define LPP_FREQUENCY 118 // 4 bytes 1Hz unsigned
|
||||
|
#define LPP_PERCENTAGE 120 // 1 byte 1-100% unsigned
|
||||
|
#define LPP_ALTITUDE 121 // 2 byte 1m signed
|
||||
|
#define LPP_CONCENTRATION 125 // 2 bytes, 1 ppm unsigned
|
||||
|
#define LPP_POWER 128 // 2 byte, 1W, unsigned
|
||||
|
#define LPP_DISTANCE 130 // 4 byte, 0.001m, unsigned
|
||||
|
#define LPP_ENERGY 131 // 4 byte, 0.001kWh, unsigned
|
||||
|
#define LPP_DIRECTION 132 // 2 bytes, 1deg, unsigned
|
||||
|
#define LPP_UNIXTIME 133 // 4 bytes, unsigned
|
||||
|
#define LPP_GYROMETER 134 // 2 bytes per axis, 0.01 °/s
|
||||
|
#define LPP_COLOUR 135 // 1 byte per RGB Color
|
||||
|
#define LPP_GPS 136 // 3 byte lon/lat 0.0001 °, 3 bytes alt 0.01 meter
|
||||
|
#define LPP_SWITCH 142 // 1 byte, 0/1
|
||||
|
#define LPP_POLYLINE 240 // 1 byte size, 1 byte delta factor, 3 byte lon/lat 0.0001° * factor, n (size-8) bytes deltas
|
||||
|
|
||||
|
// Multipliers
|
||||
|
#define LPP_DIGITAL_INPUT_MULT 1 |
||||
|
#define LPP_DIGITAL_OUTPUT_MULT 1 |
||||
|
#define LPP_ANALOG_INPUT_MULT 100 |
||||
|
#define LPP_ANALOG_OUTPUT_MULT 100 |
||||
|
#define LPP_GENERIC_SENSOR_MULT 1 |
||||
|
#define LPP_LUMINOSITY_MULT 1 |
||||
|
#define LPP_PRESENCE_MULT 1 |
||||
|
#define LPP_TEMPERATURE_MULT 10 |
||||
|
#define LPP_RELATIVE_HUMIDITY_MULT 2 |
||||
|
#define LPP_ACCELEROMETER_MULT 1000 |
||||
|
#define LPP_BAROMETRIC_PRESSURE_MULT 10 |
||||
|
#define LPP_VOLTAGE_MULT 100 |
||||
|
#define LPP_CURRENT_MULT 1000 |
||||
|
#define LPP_FREQUENCY_MULT 1 |
||||
|
#define LPP_PERCENTAGE_MULT 1 |
||||
|
#define LPP_ALTITUDE_MULT 1 |
||||
|
#define LPP_POWER_MULT 1 |
||||
|
#define LPP_DISTANCE_MULT 1000 |
||||
|
#define LPP_ENERGY_MULT 1000 |
||||
|
#define LPP_DIRECTION_MULT 1 |
||||
|
#define LPP_UNIXTIME_MULT 1 |
||||
|
#define LPP_GYROMETER_MULT 100 |
||||
|
#define LPP_GPS_LAT_LON_MULT 10000 |
||||
|
#define LPP_GPS_ALT_MULT 100 |
||||
|
#define LPP_SWITCH_MULT 1 |
||||
|
#define LPP_CONCENTRATION_MULT 1 |
||||
|
#define LPP_COLOUR_MULT 1 |
||||
|
|
||||
|
#define LPP_ERROR_OK 0 |
||||
|
#define LPP_ERROR_OVERFLOW 1 |
||||
|
#define LPP_ERROR_UNKOWN_TYPE 2 |
||||
|
|
||||
|
class LPPReader { |
||||
|
const uint8_t* _buf; |
||||
|
uint8_t _len; |
||||
|
uint8_t _pos; |
||||
|
|
||||
|
float getFloat(const uint8_t * buffer, uint8_t size, uint32_t multiplier, bool is_signed) { |
||||
|
uint32_t value = 0; |
||||
|
for (uint8_t i = 0; i < size; i++) { |
||||
|
value = (value << 8) + buffer[i]; |
||||
|
} |
||||
|
|
||||
|
int sign = 1; |
||||
|
if (is_signed) { |
||||
|
uint32_t bit = 1ul << ((size * 8) - 1); |
||||
|
if ((value & bit) == bit) { |
||||
|
value = (bit << 1) - value; |
||||
|
sign = -1; |
||||
|
} |
||||
|
} |
||||
|
return sign * ((float) value / multiplier); |
||||
|
} |
||||
|
|
||||
|
public: |
||||
|
LPPReader(const uint8_t buf[], uint8_t len) : _buf(buf), _len(len), _pos(0) { } |
||||
|
|
||||
|
void reset() { |
||||
|
_pos = 0; |
||||
|
} |
||||
|
|
||||
|
bool readHeader(uint8_t& channel, uint8_t& type) { |
||||
|
if (_pos + 2 < _len) { |
||||
|
channel = _buf[_pos++]; |
||||
|
type = _buf[_pos++]; |
||||
|
|
||||
|
return channel != 0; // channel 0 is End-of-data
|
||||
|
} |
||||
|
return false; // end-of-buffer
|
||||
|
} |
||||
|
|
||||
|
bool readGPS(float& lat, float& lon, float& alt) { |
||||
|
lat = getFloat(&_buf[_pos], 3, 10000, true); _pos += 3; |
||||
|
lon = getFloat(&_buf[_pos], 3, 10000, true); _pos += 3; |
||||
|
alt = getFloat(&_buf[_pos], 3, 100, true); _pos += 3; |
||||
|
return _pos <= _len; |
||||
|
} |
||||
|
bool readVoltage(float& voltage) { |
||||
|
voltage = getFloat(&_buf[_pos], 2, 100, false); _pos += 2; |
||||
|
return _pos <= _len; |
||||
|
} |
||||
|
bool readCurrent(float& amps) { |
||||
|
amps = getFloat(&_buf[_pos], 2, 1000, false); _pos += 2; |
||||
|
return _pos <= _len; |
||||
|
} |
||||
|
bool readPower(float& watts) { |
||||
|
watts = getFloat(&_buf[_pos], 2, 1, false); _pos += 2; |
||||
|
return _pos <= _len; |
||||
|
} |
||||
|
bool readTemperature(float& degrees_c) { |
||||
|
degrees_c = getFloat(&_buf[_pos], 2, 10, true); _pos += 2; |
||||
|
return _pos <= _len; |
||||
|
} |
||||
|
bool readPressure(float& pa) { |
||||
|
pa = getFloat(&_buf[_pos], 2, 10, false); _pos += 2; |
||||
|
return _pos <= _len; |
||||
|
} |
||||
|
bool readRelativeHumidity(float& pct) { |
||||
|
pct = getFloat(&_buf[_pos], 1, 2, false); _pos += 1; |
||||
|
return _pos <= _len; |
||||
|
} |
||||
|
bool readAltitude(float& m) { |
||||
|
m = getFloat(&_buf[_pos], 2, 1, true); _pos += 2; |
||||
|
return _pos <= _len; |
||||
|
} |
||||
|
|
||||
|
void skipData(uint8_t type) { |
||||
|
switch (type) { |
||||
|
case LPP_GPS: |
||||
|
_pos += 9; break; |
||||
|
case LPP_POLYLINE: |
||||
|
_pos += 8; break; // TODO: this is MINIMIUM
|
||||
|
case LPP_GYROMETER: |
||||
|
case LPP_ACCELEROMETER: |
||||
|
_pos += 6; break; |
||||
|
case LPP_GENERIC_SENSOR: |
||||
|
case LPP_FREQUENCY: |
||||
|
case LPP_DISTANCE: |
||||
|
case LPP_ENERGY: |
||||
|
case LPP_UNIXTIME: |
||||
|
_pos += 4; break; |
||||
|
case LPP_COLOUR: |
||||
|
_pos += 3; break; |
||||
|
case LPP_ANALOG_INPUT: |
||||
|
case LPP_ANALOG_OUTPUT: |
||||
|
case LPP_LUMINOSITY: |
||||
|
case LPP_TEMPERATURE: |
||||
|
case LPP_CONCENTRATION: |
||||
|
case LPP_BAROMETRIC_PRESSURE: |
||||
|
case LPP_ALTITUDE: |
||||
|
case LPP_VOLTAGE: |
||||
|
case LPP_CURRENT: |
||||
|
case LPP_DIRECTION: |
||||
|
case LPP_POWER: |
||||
|
_pos += 2; break; |
||||
|
default: |
||||
|
_pos++; |
||||
|
} |
||||
|
} |
||||
|
}; |
||||
|
|
||||
|
class LPPWriter { |
||||
|
uint8_t* _buf; |
||||
|
uint8_t _max_len; |
||||
|
uint8_t _len; |
||||
|
|
||||
|
void write(uint16_t value) { |
||||
|
_buf[_len++] = (value >> 8) & 0xFF; // MSB
|
||||
|
_buf[_len++] = value & 0xFF; // LSB
|
||||
|
} |
||||
|
|
||||
|
public: |
||||
|
LPPWriter(uint8_t buf[], uint8_t max_len): _buf(buf), _max_len(max_len), _len(0) { } |
||||
|
|
||||
|
bool writeVoltage(uint8_t channel, float voltage) { |
||||
|
if (_len + 4 <= _max_len) { |
||||
|
_buf[_len++] = channel; |
||||
|
_buf[_len++] = LPP_VOLTAGE; |
||||
|
uint16_t value = voltage * 100; |
||||
|
write(value); |
||||
|
return true; |
||||
|
} |
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
bool writeGPS(uint8_t channel, float lat, float lon, float alt) { |
||||
|
if (_len + 11 <= _max_len) { |
||||
|
_buf[_len++] = channel; |
||||
|
_buf[_len++] = LPP_GPS; |
||||
|
|
||||
|
int32_t lati = lat * 10000; // we lose some precision :-(
|
||||
|
int32_t loni = lon * 10000; |
||||
|
int32_t alti = alt * 100; |
||||
|
|
||||
|
_buf[_len++] = lati >> 16; |
||||
|
_buf[_len++] = lati >> 8; |
||||
|
_buf[_len++] = lati; |
||||
|
_buf[_len++] = loni >> 16; |
||||
|
_buf[_len++] = loni >> 8; |
||||
|
_buf[_len++] = loni; |
||||
|
_buf[_len++] = alti >> 16; |
||||
|
_buf[_len++] = alti >> 8; |
||||
|
_buf[_len++] = alti; |
||||
|
return true; |
||||
|
} |
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
uint8_t length() { return _len; } |
||||
|
}; |
||||
@ -0,0 +1,81 @@ |
|||||
|
#include <Arduino.h> |
||||
|
#include "RAKWismeshTagBoard.h" |
||||
|
|
||||
|
#include <bluefruit.h> |
||||
|
#include <Wire.h> |
||||
|
|
||||
|
static BLEDfu bledfu; |
||||
|
|
||||
|
static void connect_callback(uint16_t conn_handle) { |
||||
|
(void)conn_handle; |
||||
|
MESH_DEBUG_PRINTLN("BLE client connected"); |
||||
|
} |
||||
|
|
||||
|
static void disconnect_callback(uint16_t conn_handle, uint8_t reason) { |
||||
|
(void)conn_handle; |
||||
|
(void)reason; |
||||
|
|
||||
|
MESH_DEBUG_PRINTLN("BLE client disconnected"); |
||||
|
} |
||||
|
|
||||
|
void RAKWismeshTagBoard::begin() { |
||||
|
// for future use, sub-classes SHOULD call this from their begin()
|
||||
|
startup_reason = BD_STARTUP_NORMAL; |
||||
|
pinMode(PIN_VBAT_READ, INPUT); |
||||
|
pinMode(PIN_USER_BTN, INPUT_PULLUP); |
||||
|
|
||||
|
Wire.setPins(PIN_BOARD_SDA, PIN_BOARD_SCL); |
||||
|
Wire.begin(); |
||||
|
|
||||
|
pinMode(SX126X_POWER_EN, OUTPUT); |
||||
|
digitalWrite(SX126X_POWER_EN, HIGH); |
||||
|
delay(10); // give sx1262 some time to power up
|
||||
|
} |
||||
|
|
||||
|
bool RAKWismeshTagBoard::startOTAUpdate(const char* id, char reply[]) { |
||||
|
// Config the peripheral connection with maximum bandwidth
|
||||
|
// more SRAM required by SoftDevice
|
||||
|
// Note: All config***() function must be called before begin()
|
||||
|
Bluefruit.configPrphBandwidth(BANDWIDTH_MAX); |
||||
|
Bluefruit.configPrphConn(92, BLE_GAP_EVENT_LENGTH_MIN, 16, 16); |
||||
|
|
||||
|
Bluefruit.begin(1, 0); |
||||
|
// Set max power. Accepted values are: -40, -30, -20, -16, -12, -8, -4, 0, 4
|
||||
|
Bluefruit.setTxPower(4); |
||||
|
// Set the BLE device name
|
||||
|
Bluefruit.setName("WISMESHTAG_OTA"); |
||||
|
|
||||
|
Bluefruit.Periph.setConnectCallback(connect_callback); |
||||
|
Bluefruit.Periph.setDisconnectCallback(disconnect_callback); |
||||
|
|
||||
|
// To be consistent OTA DFU should be added first if it exists
|
||||
|
bledfu.begin(); |
||||
|
|
||||
|
// Set up and start advertising
|
||||
|
// Advertising packet
|
||||
|
Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE); |
||||
|
Bluefruit.Advertising.addTxPower(); |
||||
|
Bluefruit.Advertising.addName(); |
||||
|
|
||||
|
/* Start Advertising
|
||||
|
- Enable auto advertising if disconnected |
||||
|
- Interval: fast mode = 20 ms, slow mode = 152.5 ms |
||||
|
- Timeout for fast mode is 30 seconds |
||||
|
- Start(timeout) with timeout = 0 will advertise forever (until connected) |
||||
|
|
||||
|
For recommended advertising interval |
||||
|
https://developer.apple.com/library/content/qa/qa1931/_index.html
|
||||
|
*/ |
||||
|
Bluefruit.Advertising.restartOnDisconnect(true); |
||||
|
Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms
|
||||
|
Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode
|
||||
|
Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds
|
||||
|
|
||||
|
uint8_t mac_addr[6]; |
||||
|
memset(mac_addr, 0, sizeof(mac_addr)); |
||||
|
Bluefruit.getAddr(mac_addr); |
||||
|
sprintf(reply, "OK - mac: %02X:%02X:%02X:%02X:%02X:%02X", |
||||
|
mac_addr[5], mac_addr[4], mac_addr[3], mac_addr[2], mac_addr[1], mac_addr[0]); |
||||
|
|
||||
|
return true; |
||||
|
} |
||||
@ -0,0 +1,82 @@ |
|||||
|
#pragma once |
||||
|
|
||||
|
#include <MeshCore.h> |
||||
|
#include <Arduino.h> |
||||
|
|
||||
|
// built-ins
|
||||
|
#define PIN_VBAT_READ 5 |
||||
|
#define ADC_MULTIPLIER (3 * 1.73 * 1.187 * 1000) |
||||
|
|
||||
|
class RAKWismeshTagBoard : public mesh::MainBoard { |
||||
|
protected: |
||||
|
uint8_t startup_reason; |
||||
|
|
||||
|
public: |
||||
|
void begin(); |
||||
|
uint8_t getStartupReason() const override { return startup_reason; } |
||||
|
|
||||
|
#if defined(P_LORA_TX_LED) && defined(LED_STATE_ON) |
||||
|
void onBeforeTransmit() override { |
||||
|
digitalWrite(P_LORA_TX_LED, LED_STATE_ON); // turn TX LED on
|
||||
|
} |
||||
|
void onAfterTransmit() override { |
||||
|
digitalWrite(P_LORA_TX_LED, !LED_STATE_ON); // turn TX LED off
|
||||
|
} |
||||
|
#endif |
||||
|
|
||||
|
#define BATTERY_SAMPLES 8 |
||||
|
|
||||
|
uint16_t getBattMilliVolts() override { |
||||
|
analogReadResolution(12); |
||||
|
|
||||
|
uint32_t raw = 0; |
||||
|
for (int i = 0; i < BATTERY_SAMPLES; i++) { |
||||
|
raw += analogRead(PIN_VBAT_READ); |
||||
|
} |
||||
|
raw = raw / BATTERY_SAMPLES; |
||||
|
|
||||
|
return (ADC_MULTIPLIER * raw) / 4096; |
||||
|
} |
||||
|
|
||||
|
const char* getManufacturerName() const override { |
||||
|
return "RAK WisMesh Tag"; |
||||
|
} |
||||
|
|
||||
|
void reboot() override { |
||||
|
NVIC_SystemReset(); |
||||
|
} |
||||
|
|
||||
|
bool startOTAUpdate(const char* id, char reply[]) override; |
||||
|
|
||||
|
void powerOff() override { |
||||
|
#ifdef BUZZER_EN |
||||
|
digitalWrite(BUZZER_EN, LOW); |
||||
|
#endif |
||||
|
|
||||
|
#ifdef PIN_GPS_EN |
||||
|
digitalWrite(PIN_GPS_EN, LOW); |
||||
|
#endif |
||||
|
|
||||
|
// set led on and wait for button release before poweroff
|
||||
|
#ifdef LED_PIN |
||||
|
digitalWrite(LED_PIN, HIGH); |
||||
|
#endif |
||||
|
#ifdef BUTTON_PIN |
||||
|
// wismesh tag uses LOW to indicate button is pressed, wait until it goes HIGH to indicate it was released
|
||||
|
while(digitalRead(BUTTON_PIN) == LOW); |
||||
|
#endif |
||||
|
#ifdef LED_GREEN |
||||
|
digitalWrite(LED_GREEN, LOW); |
||||
|
#endif |
||||
|
#ifdef LED_BLUE |
||||
|
digitalWrite(LED_BLUE, LOW); |
||||
|
#endif |
||||
|
|
||||
|
#ifdef BUTTON_PIN |
||||
|
// configure button press to wake up when in powered off state
|
||||
|
nrf_gpio_cfg_sense_input(digitalPinToInterrupt(BUTTON_PIN), NRF_GPIO_PIN_PULLUP, NRF_GPIO_PIN_SENSE_LOW); |
||||
|
#endif |
||||
|
|
||||
|
sd_power_system_off(); |
||||
|
} |
||||
|
}; |
||||
@ -0,0 +1,119 @@ |
|||||
|
[rak_wismesh_tag] |
||||
|
extends = nrf52_base |
||||
|
platform = https://github.com/maxgerhardt/platform-nordicnrf52.git#rak |
||||
|
board = wiscore_rak4631 |
||||
|
board_check = true |
||||
|
build_flags = ${nrf52_base.build_flags} |
||||
|
${sensor_base.build_flags} |
||||
|
-I variants/rak_wismesh_tag |
||||
|
-I src/helpers/ui |
||||
|
-D RAK_WISMESH_TAG |
||||
|
-D RAK_BOARD |
||||
|
-D P_LORA_TX_LED=LED_GREEN |
||||
|
-D P_LORA_DIO_1=SX126X_DIO1 |
||||
|
-D P_LORA_NSS=PIN_SPI_NSS |
||||
|
-D P_LORA_RESET=SX126X_RESET |
||||
|
-D P_LORA_BUSY=SX126X_BUSY |
||||
|
-D P_LORA_SCLK=PIN_SPI_SCK |
||||
|
-D P_LORA_MISO=PIN_SPI_MISO |
||||
|
-D P_LORA_MOSI=PIN_SPI_MOSI |
||||
|
-D RADIO_CLASS=CustomSX1262 |
||||
|
-D WRAPPER_CLASS=CustomSX1262Wrapper |
||||
|
-D DISPLAY_CLASS=NullDisplayDriver |
||||
|
-D LORA_TX_POWER=22 |
||||
|
-D SX126X_CURRENT_LIMIT=140 |
||||
|
-D SX126X_RX_BOOSTED_GAIN=1 |
||||
|
-D PIN_BUZZER=21 |
||||
|
-D PIN_BOARD_SDA=PIN_WIRE_SDA |
||||
|
-D PIN_BOARD_SCL=PIN_WIRE_SCL |
||||
|
build_src_filter = ${nrf52_base.build_src_filter} |
||||
|
+<../variants/rak_wismesh_tag> |
||||
|
+<helpers/ui/buzzer.cpp> |
||||
|
+<helpers/ui/MomentaryButton.cpp> |
||||
|
+<helpers/ui/NullDisplayDriver.cpp> |
||||
|
+<helpers/sensors> |
||||
|
lib_deps = |
||||
|
${nrf52_base.lib_deps} |
||||
|
${sensor_base.lib_deps} |
||||
|
end2endzone/NonBlockingRTTTL@^1.3.0 |
||||
|
|
||||
|
[env:RAK_WisMesh_Tag_Repeater] |
||||
|
extends = rak_wismesh_tag |
||||
|
build_flags = |
||||
|
${rak_wismesh_tag.build_flags} |
||||
|
-D ADVERT_NAME='"RAK WM Repeater"' |
||||
|
-D ADVERT_LAT=0.0 |
||||
|
-D ADVERT_LON=0.0 |
||||
|
-D ADMIN_PASSWORD='"password"' |
||||
|
-D MAX_NEIGHBOURS=8 |
||||
|
; -D MESH_PACKET_LOGGING=1 |
||||
|
; -D MESH_DEBUG=1 |
||||
|
build_src_filter = ${rak_wismesh_tag.build_src_filter} |
||||
|
+<../examples/simple_repeater> |
||||
|
|
||||
|
[env:RAK_WisMesh_Tag_room_server] |
||||
|
extends = rak_wismesh_tag |
||||
|
build_flags = |
||||
|
${rak_wismesh_tag.build_flags} |
||||
|
-D ADVERT_NAME='"RAK WM Room"' |
||||
|
-D ADVERT_LAT=0.0 |
||||
|
-D ADVERT_LON=0.0 |
||||
|
-D ADMIN_PASSWORD='"password"' |
||||
|
-D ROOM_PASSWORD='"hello"' |
||||
|
; -D MESH_PACKET_LOGGING=1 |
||||
|
; -D MESH_DEBUG=1 |
||||
|
build_src_filter = ${rak_wismesh_tag.build_src_filter} |
||||
|
+<../examples/simple_room_server> |
||||
|
|
||||
|
[env:RAK_WisMesh_Tag_companion_radio_usb] |
||||
|
extends = rak_wismesh_tag |
||||
|
board_build.ldscript = boards/nrf52840_s140_v6_extrafs.ld |
||||
|
board_upload.maximum_size = 712704 |
||||
|
build_flags = |
||||
|
${rak_wismesh_tag.build_flags} |
||||
|
-I examples/companion_radio/ui-orig |
||||
|
-D MAX_CONTACTS=350 |
||||
|
-D MAX_GROUP_CHANNELS=40 |
||||
|
; NOTE: DO NOT ENABLE --> -D MESH_PACKET_LOGGING=1 |
||||
|
; NOTE: DO NOT ENABLE --> -D MESH_DEBUG=1 |
||||
|
build_src_filter = ${rak_wismesh_tag.build_src_filter} |
||||
|
+<../examples/companion_radio/*.cpp> |
||||
|
+<../examples/companion_radio/ui-orig/*.cpp> |
||||
|
lib_deps = |
||||
|
${rak_wismesh_tag.lib_deps} |
||||
|
densaugeo/base64 @ ~1.4.0 |
||||
|
|
||||
|
[env:RAK_WisMesh_Tag_companion_radio_ble] |
||||
|
extends = rak_wismesh_tag |
||||
|
board_build.ldscript = boards/nrf52840_s140_v6_extrafs.ld |
||||
|
board_upload.maximum_size = 712704 |
||||
|
build_flags = |
||||
|
${rak_wismesh_tag.build_flags} |
||||
|
-I examples/companion_radio/ui-orig |
||||
|
-D MAX_CONTACTS=350 |
||||
|
-D MAX_GROUP_CHANNELS=40 |
||||
|
-D BLE_PIN_CODE=123456 |
||||
|
-D BLE_DEBUG_LOGGING=1 |
||||
|
-D OFFLINE_QUEUE_SIZE=256 |
||||
|
; -D MESH_PACKET_LOGGING=1 |
||||
|
-D MESH_DEBUG=1 |
||||
|
build_src_filter = ${rak_wismesh_tag.build_src_filter} |
||||
|
+<helpers/nrf52/SerialBLEInterface.cpp> |
||||
|
+<../examples/companion_radio/*.cpp> |
||||
|
+<../examples/companion_radio/ui-orig/*.cpp> |
||||
|
lib_deps = |
||||
|
${rak4631.lib_deps} |
||||
|
densaugeo/base64 @ ~1.4.0 |
||||
|
|
||||
|
[env:RAK_WisMesh_Tag_sensor] |
||||
|
extends = rak4631 |
||||
|
build_flags = |
||||
|
${rak4631.build_flags} |
||||
|
-D ADVERT_NAME='"RAK WM Sensor"' |
||||
|
-D ADVERT_LAT=0.0 |
||||
|
-D ADVERT_LON=0.0 |
||||
|
-D ADMIN_PASSWORD='"password"' |
||||
|
; -D MESH_PACKET_LOGGING=1 |
||||
|
-D MESH_DEBUG=1 |
||||
|
build_src_filter = ${rak4631.build_src_filter} |
||||
|
+<../examples/simple_sensor> |
||||
@ -0,0 +1,54 @@ |
|||||
|
#include <Arduino.h> |
||||
|
#include "target.h" |
||||
|
#include <helpers/ArduinoHelpers.h> |
||||
|
|
||||
|
RAKWismeshTagBoard board; |
||||
|
|
||||
|
#ifndef PIN_USER_BTN |
||||
|
#define PIN_USER_BTN (-1) |
||||
|
#endif |
||||
|
|
||||
|
#ifdef DISPLAY_CLASS |
||||
|
DISPLAY_CLASS display; |
||||
|
MomentaryButton user_btn(PIN_USER_BTN, 1000, true, true); |
||||
|
#endif |
||||
|
|
||||
|
RADIO_CLASS radio = new Module(P_LORA_NSS, P_LORA_DIO_1, P_LORA_RESET, P_LORA_BUSY, SPI); |
||||
|
|
||||
|
WRAPPER_CLASS radio_driver(radio, board); |
||||
|
|
||||
|
VolatileRTCClock fallback_clock; |
||||
|
AutoDiscoverRTCClock rtc_clock(fallback_clock); |
||||
|
|
||||
|
#if ENV_INCLUDE_GPS |
||||
|
#include <helpers/sensors/MicroNMEALocationProvider.h> |
||||
|
MicroNMEALocationProvider nmea = MicroNMEALocationProvider(Serial1); |
||||
|
EnvironmentSensorManager sensors = EnvironmentSensorManager(nmea); |
||||
|
#else |
||||
|
EnvironmentSensorManager sensors; |
||||
|
#endif |
||||
|
|
||||
|
bool radio_init() { |
||||
|
rtc_clock.begin(Wire); |
||||
|
return radio.std_init(&SPI); |
||||
|
} |
||||
|
|
||||
|
uint32_t radio_get_rng_seed() { |
||||
|
return radio.random(0x7FFFFFFF); |
||||
|
} |
||||
|
|
||||
|
void radio_set_params(float freq, float bw, uint8_t sf, uint8_t cr) { |
||||
|
radio.setFrequency(freq); |
||||
|
radio.setSpreadingFactor(sf); |
||||
|
radio.setBandwidth(bw); |
||||
|
radio.setCodingRate(cr); |
||||
|
} |
||||
|
|
||||
|
void radio_set_tx_power(uint8_t dbm) { |
||||
|
radio.setOutputPower(dbm); |
||||
|
} |
||||
|
|
||||
|
mesh::LocalIdentity radio_new_identity() { |
||||
|
RadioNoiseListener rng(radio); |
||||
|
return mesh::LocalIdentity(&rng); // create new random identity
|
||||
|
} |
||||
@ -0,0 +1,27 @@ |
|||||
|
#pragma once |
||||
|
|
||||
|
#define RADIOLIB_STATIC_ONLY 1 |
||||
|
#include <RadioLib.h> |
||||
|
#include <helpers/radiolib/RadioLibWrappers.h> |
||||
|
#include <RAKWismeshTagBoard.h> |
||||
|
#include <helpers/radiolib/CustomSX1262Wrapper.h> |
||||
|
#include <helpers/AutoDiscoverRTCClock.h> |
||||
|
#include <helpers/sensors/EnvironmentSensorManager.h> |
||||
|
|
||||
|
#ifdef DISPLAY_CLASS |
||||
|
#include <helpers/ui/NullDisplayDriver.h> |
||||
|
extern DISPLAY_CLASS display; |
||||
|
#include <helpers/ui/MomentaryButton.h> |
||||
|
extern MomentaryButton user_btn; |
||||
|
#endif |
||||
|
|
||||
|
extern RAKWismeshTagBoard board; |
||||
|
extern WRAPPER_CLASS radio_driver; |
||||
|
extern AutoDiscoverRTCClock rtc_clock; |
||||
|
extern EnvironmentSensorManager sensors; |
||||
|
|
||||
|
bool radio_init(); |
||||
|
uint32_t radio_get_rng_seed(); |
||||
|
void radio_set_params(float freq, float bw, uint8_t sf, uint8_t cr); |
||||
|
void radio_set_tx_power(uint8_t dbm); |
||||
|
mesh::LocalIdentity radio_new_identity(); |
||||
@ -0,0 +1,21 @@ |
|||||
|
#include "variant.h" |
||||
|
#include "nrf.h" |
||||
|
#include "wiring_constants.h" |
||||
|
#include "wiring_digital.h" |
||||
|
|
||||
|
const uint32_t g_ADigitalPinMap[] = { |
||||
|
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
||||
|
32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47 |
||||
|
}; |
||||
|
|
||||
|
void initVariant() { |
||||
|
// LEDs
|
||||
|
pinMode(LED_BLUE, OUTPUT); |
||||
|
pinMode(LED_GREEN, OUTPUT); |
||||
|
digitalWrite(LED_BLUE, LOW); |
||||
|
digitalWrite(LED_GREEN, LOW); |
||||
|
|
||||
|
// GPS
|
||||
|
pinMode(PIN_GPS_EN, OUTPUT); |
||||
|
digitalWrite(PIN_GPS_EN, HIGH); |
||||
|
} |
||||
@ -0,0 +1,121 @@ |
|||||
|
/*
|
||||
|
* variant.h |
||||
|
* Copyright (C) 2023 Seeed K.K. |
||||
|
* MIT License |
||||
|
*/ |
||||
|
|
||||
|
#pragma once |
||||
|
|
||||
|
#include "WVariant.h" |
||||
|
|
||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||
|
// Low frequency clock source
|
||||
|
|
||||
|
#define USE_LFXO // 32.768 kHz crystal oscillator
|
||||
|
#define VARIANT_MCK (64000000ul) |
||||
|
|
||||
|
#define WIRE_INTERFACES_COUNT (1) |
||||
|
#define PIN_TXCO (21) |
||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||
|
// Power
|
||||
|
|
||||
|
#define PIN_PWR_EN (12) |
||||
|
|
||||
|
#define BATTERY_PIN (5) |
||||
|
#define ADC_MULTIPLIER (1.73F) |
||||
|
|
||||
|
#define ADC_RESOLUTION (14) |
||||
|
#define BATTERY_SENSE_RES (12) |
||||
|
|
||||
|
#define AREF_VOLTAGE (3.0) |
||||
|
|
||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||
|
// Number of pins
|
||||
|
|
||||
|
#define PINS_COUNT (48) |
||||
|
#define NUM_DIGITAL_PINS (48) |
||||
|
#define NUM_ANALOG_INPUTS (6) |
||||
|
#define NUM_ANALOG_OUTPUTS (0) |
||||
|
|
||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||
|
// UART pin definition
|
||||
|
|
||||
|
#define PIN_SERIAL1_RX (15) |
||||
|
#define PIN_SERIAL1_TX (16) |
||||
|
|
||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||
|
// I2C pin definition
|
||||
|
#define WIRE_INTERFACES_COUNT (1) |
||||
|
#define PIN_WIRE_SDA (25) |
||||
|
#define PIN_WIRE_SCL (24) |
||||
|
|
||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||
|
// SPI pin definition
|
||||
|
|
||||
|
#define SPI_INTERFACES_COUNT (2) |
||||
|
|
||||
|
#define PIN_SPI_MISO (45) |
||||
|
#define PIN_SPI_MOSI (44) |
||||
|
#define PIN_SPI_SCK (43) |
||||
|
#define PIN_SPI_NSS (42) |
||||
|
|
||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||
|
// Builtin LEDs
|
||||
|
|
||||
|
#define LED_RED (-1) |
||||
|
#define LED_BLUE (36) |
||||
|
#define LED_GREEN (35) |
||||
|
|
||||
|
//#define PIN_STATUS_LED LED_BLUE
|
||||
|
#define LED_BUILTIN LED_GREEN |
||||
|
#define LED_PIN LED_GREEN |
||||
|
#define LED_STATE_ON HIGH |
||||
|
|
||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||
|
// Builtin buttons
|
||||
|
|
||||
|
#define PIN_BUTTON1 (9) |
||||
|
#define BUTTON_PIN PIN_BUTTON1 |
||||
|
#define PIN_USER_BTN BUTTON_PIN |
||||
|
|
||||
|
#define PIN_BUTTON2 (12) |
||||
|
#define BUTTON_PIN2 PIN_BUTTON2 |
||||
|
|
||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||
|
// Lora
|
||||
|
|
||||
|
#define USE_SX1262 |
||||
|
#define LORA_CS (42) |
||||
|
#define SX126X_DIO1 (47) |
||||
|
#define SX126X_BUSY (46) |
||||
|
#define SX126X_RESET (38) |
||||
|
#define SX126X_POWER_EN (37) |
||||
|
#define SX126X_DIO2_AS_RF_SWITCH |
||||
|
#define SX126X_DIO3_TCXO_VOLTAGE 1.8 |
||||
|
|
||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||
|
// SPI1
|
||||
|
|
||||
|
#define PIN_SPI1_MISO (29) |
||||
|
#define PIN_SPI1_MOSI (30) |
||||
|
#define PIN_SPI1_SCK (3) |
||||
|
|
||||
|
// GxEPD2 needs that for a panel that is not even used !
|
||||
|
extern const int MISO; |
||||
|
extern const int MOSI; |
||||
|
extern const int SCK; |
||||
|
|
||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||
|
// GPS
|
||||
|
|
||||
|
#define PIN_GPS_RX (PIN_SERIAL1_TX) |
||||
|
#define PIN_GPS_TX (PIN_SERIAL1_RX) |
||||
|
#define PIN_GPS_PPS (17) |
||||
|
#define PIN_GPS_EN (34) |
||||
|
|
||||
|
///////////////////////////////////////////////////////////////////////////////
|
||||
|
// OTHER PINS
|
||||
|
#define PIN_AREF (2) |
||||
|
#define PIN_NFC1 (9) |
||||
|
#define PIN_NFC2 (10) |
||||
|
#define PIN_BUZZER (21) |
||||
Loading…
Reference in new issue