Skip to content

Commit 28b90c1

Browse files
author
Scott Powell
committed
Merge branch 'transportcodes' into dev
2 parents a9d245f + 963290e commit 28b90c1

File tree

20 files changed

+821
-22
lines changed

20 files changed

+821
-22
lines changed

examples/companion_radio/MyMesh.cpp

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@
5050
#define CMD_SEND_BINARY_REQ 50
5151
#define CMD_FACTORY_RESET 51
5252
#define CMD_SEND_PATH_DISCOVERY_REQ 52
53+
#define CMD_SET_FLOOD_SCOPE 54 // v8+
54+
#define CMD_SEND_CONTROL_DATA 55 // v8+
5355

5456
#define RESP_CODE_OK 0
5557
#define RESP_CODE_ERR 1
@@ -99,6 +101,7 @@
99101
#define PUSH_CODE_TELEMETRY_RESPONSE 0x8B
100102
#define PUSH_CODE_BINARY_RESPONSE 0x8C
101103
#define PUSH_CODE_PATH_DISCOVERY_RESPONSE 0x8D
104+
#define PUSH_CODE_CONTROL_DATA 0x8E // v8+
102105

103106
#define ERR_CODE_UNSUPPORTED_CMD 1
104107
#define ERR_CODE_NOT_FOUND 2
@@ -378,6 +381,35 @@ void MyMesh::queueMessage(const ContactInfo &from, uint8_t txt_type, mesh::Packe
378381
#endif
379382
}
380383

384+
bool MyMesh::filterRecvFloodPacket(mesh::Packet* packet) {
385+
// REVISIT: try to determine which Region (from transport_codes[1]) that Sender is indicating for replies/responses
386+
// if unknown, fallback to finding Region from transport_codes[0], the 'scope' used by Sender
387+
return false;
388+
}
389+
390+
void MyMesh::sendFloodScoped(const ContactInfo& recipient, mesh::Packet* pkt, uint32_t delay_millis) {
391+
// TODO: dynamic send_scope, depending on recipient and current 'home' Region
392+
if (send_scope.isNull()) {
393+
sendFlood(pkt, delay_millis);
394+
} else {
395+
uint16_t codes[2];
396+
codes[0] = send_scope.calcTransportCode(pkt);
397+
codes[1] = 0; // REVISIT: set to 'home' Region, for sender/return region?
398+
sendFlood(pkt, codes, delay_millis);
399+
}
400+
}
401+
void MyMesh::sendFloodScoped(const mesh::GroupChannel& channel, mesh::Packet* pkt, uint32_t delay_millis) {
402+
// TODO: have per-channel send_scope
403+
if (send_scope.isNull()) {
404+
sendFlood(pkt, delay_millis);
405+
} else {
406+
uint16_t codes[2];
407+
codes[0] = send_scope.calcTransportCode(pkt);
408+
codes[1] = 0; // REVISIT: set to 'home' Region, for sender/return region?
409+
sendFlood(pkt, codes, delay_millis);
410+
}
411+
}
412+
381413
void MyMesh::onMessageRecv(const ContactInfo &from, mesh::Packet *pkt, uint32_t sender_timestamp,
382414
const char *text) {
383415
markConnectionActive(from); // in case this is from a server, and we have a connection
@@ -596,6 +628,26 @@ bool MyMesh::onContactPathRecv(ContactInfo& contact, uint8_t* in_path, uint8_t i
596628
return BaseChatMesh::onContactPathRecv(contact, in_path, in_path_len, out_path, out_path_len, extra_type, extra, extra_len);
597629
}
598630

631+
void MyMesh::onControlDataRecv(mesh::Packet *packet) {
632+
if (packet->payload_len + 4 > sizeof(out_frame)) {
633+
MESH_DEBUG_PRINTLN("onControlDataRecv(), payload_len too long: %d", packet->payload_len);
634+
return;
635+
}
636+
int i = 0;
637+
out_frame[i++] = PUSH_CODE_CONTROL_DATA;
638+
out_frame[i++] = (int8_t)(_radio->getLastSNR() * 4);
639+
out_frame[i++] = (int8_t)(_radio->getLastRSSI());
640+
out_frame[i++] = packet->path_len;
641+
memcpy(&out_frame[i], packet->payload, packet->payload_len);
642+
i += packet->payload_len;
643+
644+
if (_serial->isConnected()) {
645+
_serial->writeFrame(out_frame, i);
646+
} else {
647+
MESH_DEBUG_PRINTLN("onControlDataRecv(), data received while app offline");
648+
}
649+
}
650+
599651
void MyMesh::onRawDataRecv(mesh::Packet *packet) {
600652
if (packet->payload_len + 4 > sizeof(out_frame)) {
601653
MESH_DEBUG_PRINTLN("onRawDataRecv(), payload_len too long: %d", packet->payload_len);
@@ -663,6 +715,7 @@ MyMesh::MyMesh(mesh::Radio &radio, mesh::RNG &rng, mesh::RTCClock &rtc, SimpleMe
663715
sign_data = NULL;
664716
dirty_contacts_expiry = 0;
665717
memset(advert_paths, 0, sizeof(advert_paths));
718+
memset(send_scope.key, 0, sizeof(send_scope.key));
666719

667720
// defaults
668721
memset(&_prefs, 0, sizeof(_prefs));
@@ -1485,6 +1538,21 @@ void MyMesh::handleCmdFrame(size_t len) {
14851538
} else {
14861539
writeErrFrame(ERR_CODE_FILE_IO_ERROR);
14871540
}
1541+
} else if (cmd_frame[0] == CMD_SET_FLOOD_SCOPE && len >= 2 && cmd_frame[1] == 0) {
1542+
if (len >= 2 + 16) {
1543+
memcpy(send_scope.key, &cmd_frame[2], sizeof(send_scope.key)); // set curr scope TransportKey
1544+
} else {
1545+
memset(send_scope.key, 0, sizeof(send_scope.key)); // set scope to null
1546+
}
1547+
writeOKFrame();
1548+
} else if (cmd_frame[0] == CMD_SEND_CONTROL_DATA && len >= 2 && (cmd_frame[1] & 0x80) != 0) {
1549+
auto resp = createControlData(&cmd_frame[1], len - 1);
1550+
if (resp) {
1551+
sendZeroHop(resp);
1552+
writeOKFrame();
1553+
} else {
1554+
writeErrFrame(ERR_CODE_TABLE_FULL);
1555+
}
14881556
} else {
14891557
writeErrFrame(ERR_CODE_UNSUPPORTED_CMD);
14901558
MESH_DEBUG_PRINTLN("ERROR: unknown command: %02X", cmd_frame[0]);

examples/companion_radio/MyMesh.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#include "AbstractUITask.h"
66

77
/*------------ Frame Protocol --------------*/
8-
#define FIRMWARE_VER_CODE 7
8+
#define FIRMWARE_VER_CODE 8
99

1010
#ifndef FIRMWARE_BUILD_DATE
1111
#define FIRMWARE_BUILD_DATE "2 Oct 2025"
@@ -68,6 +68,7 @@
6868
#endif
6969

7070
#include <helpers/BaseChatMesh.h>
71+
#include <helpers/TransportKeyStore.h>
7172

7273
/* -------------------------------------------------------------------------------------- */
7374

@@ -106,6 +107,10 @@ class MyMesh : public BaseChatMesh, public DataStoreHost {
106107
int getInterferenceThreshold() const override;
107108
int calcRxDelay(float score, uint32_t air_time) const override;
108109
uint8_t getExtraAckTransmitCount() const override;
110+
bool filterRecvFloodPacket(mesh::Packet* packet) override;
111+
112+
void sendFloodScoped(const ContactInfo& recipient, mesh::Packet* pkt, uint32_t delay_millis=0) override;
113+
void sendFloodScoped(const mesh::GroupChannel& channel, mesh::Packet* pkt, uint32_t delay_millis=0) override;
109114

110115
void logRxRaw(float snr, float rssi, const uint8_t raw[], int len) override;
111116
bool isAutoAddEnabled() const override;
@@ -128,6 +133,7 @@ class MyMesh : public BaseChatMesh, public DataStoreHost {
128133
uint8_t onContactRequest(const ContactInfo &contact, uint32_t sender_timestamp, const uint8_t *data,
129134
uint8_t len, uint8_t *reply) override;
130135
void onContactResponse(const ContactInfo &contact, const uint8_t *data, uint8_t len) override;
136+
void onControlDataRecv(mesh::Packet *packet) override;
131137
void onRawDataRecv(mesh::Packet *packet) override;
132138
void onTraceRecv(mesh::Packet *packet, uint32_t tag, uint32_t auth_code, uint8_t flags,
133139
const uint8_t *path_snrs, const uint8_t *path_hashes, uint8_t path_len) override;
@@ -191,6 +197,8 @@ class MyMesh : public BaseChatMesh, public DataStoreHost {
191197
uint32_t sign_data_len;
192198
unsigned long dirty_contacts_expiry;
193199

200+
TransportKey send_scope;
201+
194202
uint8_t cmd_frame[MAX_FRAME_SIZE + 1];
195203
uint8_t out_frame[MAX_FRAME_SIZE + 1];
196204
CayenneLPP telemetry;

0 commit comments

Comments
 (0)