Skip to content

Commit 9cecbad

Browse files
author
Scott Powell
committed
* refactor: CommonCLI, processing of optional command prefix moved to handleCommand() call sites
* Sensor, anon_req now just for admin login (guest password now unused) * special CLI command, "setperm {pubkey-hex} {permissions-int16}" for admin(s) to manage user access (permissions 0 = remove)
1 parent ac83492 commit 9cecbad

File tree

6 files changed

+126
-90
lines changed

6 files changed

+126
-90
lines changed

examples/simple_repeater/main.cpp

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -500,12 +500,12 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks {
500500
}
501501

502502
uint8_t temp[166];
503-
const char *command = (const char *) &data[5];
503+
char *command = (char *) &data[5];
504504
char *reply = (char *) &temp[5];
505505
if (is_retry) {
506506
*reply = 0;
507507
} else {
508-
_cli.handleCommand(sender_timestamp, command, reply);
508+
handleCommand(sender_timestamp, command, reply);
509509
}
510510
int text_len = strlen(reply);
511511
if (text_len > 0) {
@@ -581,8 +581,6 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks {
581581
_prefs.interference_threshold = 0; // disabled
582582
}
583583

584-
CommonCLI* getCLI() { return &_cli; }
585-
586584
void begin(FILESYSTEM* fs) {
587585
mesh::Mesh::begin();
588586
_fs = fs;
@@ -706,6 +704,18 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks {
706704
((SimpleMeshTables *)getTables())->resetStats();
707705
}
708706

707+
void handleCommand(uint32_t sender_timestamp, char* command, char* reply) {
708+
while (*command == ' ') command++; // skip leading spaces
709+
710+
if (strlen(command) > 4 && command[2] == '|') { // optional prefix (for companion radio CLI)
711+
memcpy(reply, command, 3); // reflect the prefix back
712+
reply += 3;
713+
command += 3;
714+
}
715+
716+
_cli.handleCommand(sender_timestamp, command, reply); // common CLI commands
717+
}
718+
709719
void loop() {
710720
mesh::Mesh::loop();
711721

@@ -817,7 +827,7 @@ void loop() {
817827
if (len > 0 && command[len - 1] == '\r') { // received complete line
818828
command[len - 1] = 0; // replace newline with C string null terminator
819829
char reply[160];
820-
the_mesh.getCLI()->handleCommand(0, command, reply); // NOTE: there is no sender_timestamp via serial!
830+
the_mesh.handleCommand(0, command, reply); // NOTE: there is no sender_timestamp via serial!
821831
if (reply[0]) {
822832
Serial.print(" -> "); Serial.println(reply);
823833
}

examples/simple_room_server/main.cpp

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks {
333333
}
334334
return 0; // unknown command
335335
}
336-
336+
337337
protected:
338338
float getAirtimeBudgetFactor() const override {
339339
return _prefs.airtime_factor;
@@ -555,7 +555,7 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks {
555555
if (is_retry) {
556556
temp[5] = 0; // no reply
557557
} else {
558-
_cli.handleCommand(sender_timestamp, (const char *) &data[5], (char *) &temp[5]);
558+
handleCommand(sender_timestamp, (char *) &data[5], (char *) &temp[5]);
559559
temp[4] = (TXT_TYPE_CLI_DATA << 2); // attempt and flags, (NOTE: legacy was: TXT_TYPE_PLAIN)
560560
}
561561
send_ack = false;
@@ -743,8 +743,6 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks {
743743
_num_posted = _num_post_pushes = 0;
744744
}
745745

746-
CommonCLI* getCLI() { return &_cli; }
747-
748746
void begin(FILESYSTEM* fs) {
749747
mesh::Mesh::begin();
750748
_fs = fs;
@@ -845,6 +843,18 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks {
845843
((SimpleMeshTables *)getTables())->resetStats();
846844
}
847845

846+
void handleCommand(uint32_t sender_timestamp, char* command, char* reply) {
847+
while (*command == ' ') command++; // skip leading spaces
848+
849+
if (strlen(command) > 4 && command[2] == '|') { // optional prefix (for companion radio CLI)
850+
memcpy(reply, command, 3); // reflect the prefix back
851+
reply += 3;
852+
command += 3;
853+
}
854+
855+
_cli.handleCommand(sender_timestamp, command, reply); // common CLI commands
856+
}
857+
848858
void loop() {
849859
mesh::Mesh::loop();
850860

@@ -998,7 +1008,7 @@ void loop() {
9981008
if (len > 0 && command[len - 1] == '\r') { // received complete line
9991009
command[len - 1] = 0; // replace newline with C string null terminator
10001010
char reply[160];
1001-
the_mesh.getCLI()->handleCommand(0, command, reply); // NOTE: there is no sender_timestamp via serial!
1011+
the_mesh.handleCommand(0, command, reply); // NOTE: there is no sender_timestamp via serial!
10021012
if (reply[0]) {
10031013
Serial.print(" -> "); Serial.println(reply);
10041014
}

examples/simple_sensor/SensorMesh.cpp

Lines changed: 86 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,11 @@ void SensorMesh::loadContacts() {
9292
while (!full) {
9393
ContactInfo c;
9494
uint8_t pub_key[32];
95-
uint8_t unused;
95+
uint8_t unused[5];
9696

9797
bool success = (file.read(pub_key, 32) == 32);
98-
success = success && (file.read(&c.type, 1) == 1);
99-
success = success && (file.read(&c.flags, 1) == 1);
100-
success = success && (file.read(&unused, 1) == 1);
98+
success = success && (file.read((uint8_t *) &c.permissions, 2) == 2);
99+
success = success && (file.read(unused, 5) == 5);
101100
success = success && (file.read((uint8_t *)&c.out_path_len, 1) == 1);
102101
success = success && (file.read(c.out_path, 64) == 64);
103102
success = success && (file.read(c.shared_secret, PUB_KEY_SIZE) == PUB_KEY_SIZE);
@@ -121,16 +120,16 @@ void SensorMesh::loadContacts() {
121120
void SensorMesh::saveContacts() {
122121
File file = openWrite(_fs, "/s_contacts");
123122
if (file) {
124-
uint8_t unused = 0;
123+
uint8_t unused[5];
124+
memset(unused, 0, sizeof(unused));
125125

126126
for (int i = 0; i < num_contacts; i++) {
127127
auto c = &contacts[i];
128-
if (c->type == 0) continue; // don't persist guest contacts
128+
if (c->permissions == 0) continue; // skip deleted entries
129129

130130
bool success = (file.write(c->id.pub_key, 32) == 32);
131-
success = success && (file.write(&c->type, 1) == 1);
132-
success = success && (file.write(&c->flags, 1) == 1);
133-
success = success && (file.write(&unused, 1) == 1);
131+
success = success && (file.write((uint8_t *) &c->permissions, 2) == 2);
132+
success = success && (file.write(unused, 5) == 5);
134133
success = success && (file.write((uint8_t *)&c->out_path_len, 1) == 1);
135134
success = success && (file.write(c->out_path, 64) == 64);
136135
success = success && (file.write(c->shared_secret, PUB_KEY_SIZE) == PUB_KEY_SIZE);
@@ -141,36 +140,34 @@ void SensorMesh::saveContacts() {
141140
}
142141
}
143142

144-
uint8_t SensorMesh::handleRequest(bool is_admin, uint32_t sender_timestamp, uint8_t req_type, uint8_t* payload, size_t payload_len) {
143+
uint8_t SensorMesh::handleRequest(uint16_t perms, uint32_t sender_timestamp, uint8_t req_type, uint8_t* payload, size_t payload_len) {
145144
memcpy(reply_data, &sender_timestamp, 4); // reflect sender_timestamp back in response packet (kind of like a 'tag')
146145

147-
switch (req_type) {
148-
case REQ_TYPE_GET_TELEMETRY_DATA: {
149-
telemetry.reset();
150-
telemetry.addVoltage(TELEM_CHANNEL_SELF, (float)board.getBattMilliVolts() / 1000.0f);
151-
// query other sensors -- target specific
152-
sensors.querySensors(0xFF, telemetry); // allow all telemetry permissions for admin or guest
146+
if (req_type == REQ_TYPE_GET_TELEMETRY_DATA && (perms & PERM_GET_TELEMETRY) != 0) {
147+
telemetry.reset();
148+
telemetry.addVoltage(TELEM_CHANNEL_SELF, (float)board.getBattMilliVolts() / 1000.0f);
149+
// query other sensors -- target specific
150+
sensors.querySensors(0xFF, telemetry); // allow all telemetry permissions for admin or guest
153151

154-
uint8_t tlen = telemetry.getSize();
155-
memcpy(&reply_data[4], telemetry.getBuffer(), tlen);
156-
return 4 + tlen; // reply_len
157-
}
158-
case REQ_TYPE_GET_AVG_MIN_MAX: {
159-
uint32_t start_secs_ago, end_secs_ago;
160-
memcpy(&start_secs_ago, &payload[0], 4);
161-
memcpy(&end_secs_ago, &payload[4], 4);
162-
uint8_t res1 = payload[8]; // reserved for future (extra query params)
163-
uint8_t res2 = payload[8];
164-
165-
MinMaxAvg data[8];
166-
int n;
167-
if (res1 == 0 && res2 == 0) {
168-
n = querySeriesData(start_secs_ago, end_secs_ago, data, 8);
169-
} else {
170-
n = 0;
171-
}
172-
return 0; // TODO: encode data[0..n)
152+
uint8_t tlen = telemetry.getSize();
153+
memcpy(&reply_data[4], telemetry.getBuffer(), tlen);
154+
return 4 + tlen; // reply_len
155+
}
156+
if (req_type == REQ_TYPE_GET_AVG_MIN_MAX && (perms & PERM_GET_MIN_MAX_AVG) != 0) {
157+
uint32_t start_secs_ago, end_secs_ago;
158+
memcpy(&start_secs_ago, &payload[0], 4);
159+
memcpy(&end_secs_ago, &payload[4], 4);
160+
uint8_t res1 = payload[8]; // reserved for future (extra query params)
161+
uint8_t res2 = payload[8];
162+
163+
MinMaxAvg data[8];
164+
int n;
165+
if (res1 == 0 && res2 == 0) {
166+
n = querySeriesData(start_secs_ago, end_secs_ago, data, 8);
167+
} else {
168+
n = 0;
173169
}
170+
return 0; // TODO: encode data[0..n)
174171
}
175172
return 0; // unknown command
176173
}
@@ -209,6 +206,18 @@ ContactInfo* SensorMesh::putContact(const mesh::Identity& id) {
209206
return c;
210207
}
211208

209+
void SensorMesh::applyContactPermissions(const uint8_t* pubkey, uint16_t perms) {
210+
mesh::Identity id(pubkey);
211+
auto c = putContact(id);
212+
213+
if (perms == 0) { // no permissions, remove from contacts
214+
memset(c, 0, sizeof(*c));
215+
} else {
216+
c->permissions = perms; // update their permissions
217+
}
218+
dirty_contacts_expiry = futureMillis(LAZY_CONTACTS_WRITE_DELAY); // trigger saveContacts()
219+
}
220+
212221
void SensorMesh::sendAlert(const char* text) {
213222
int text_len = strlen(text);
214223

@@ -283,12 +292,7 @@ int SensorMesh::getAGCResetInterval() const {
283292
}
284293

285294
uint8_t SensorMesh::handleLoginReq(const mesh::Identity& sender, const uint8_t* secret, uint32_t sender_timestamp, const uint8_t* data) {
286-
bool is_admin;
287-
if (strcmp((char *) data, _prefs.password) == 0) { // check for valid password
288-
is_admin = true;
289-
} else if (strcmp((char *) data, _prefs.guest_password) == 0) { // check guest password
290-
is_admin = false;
291-
} else {
295+
if (strcmp((char *) data, _prefs.password) != 0) { // check for valid password
292296
#if MESH_DEBUG
293297
MESH_DEBUG_PRINTLN("Invalid password: %s", &data[4]);
294298
#endif
@@ -304,46 +308,61 @@ uint8_t SensorMesh::handleLoginReq(const mesh::Identity& sender, const uint8_t*
304308
MESH_DEBUG_PRINTLN("Login success!");
305309
client->last_timestamp = sender_timestamp;
306310
client->last_activity = getRTCClock()->getCurrentTime();
307-
client->type = is_admin ? 1 : 0;
311+
client->permissions = PERM_IS_ADMIN;
308312
memcpy(client->shared_secret, secret, PUB_KEY_SIZE);
309313

310-
if (is_admin) {
311-
// only need to saveContacts() if this is an admin
312-
dirty_contacts_expiry = futureMillis(LAZY_CONTACTS_WRITE_DELAY);
313-
}
314+
dirty_contacts_expiry = futureMillis(LAZY_CONTACTS_WRITE_DELAY);
314315

315316
uint32_t now = getRTCClock()->getCurrentTimeUnique();
316317
memcpy(reply_data, &now, 4); // response packets always prefixed with timestamp
317318
reply_data[4] = RESP_SERVER_LOGIN_OK;
318319
reply_data[5] = 0; // NEW: recommended keep-alive interval (secs / 16)
319-
reply_data[6] = client->type;
320+
reply_data[6] = 1; // 1 = is admin
320321
reply_data[7] = 0; // FUTURE: reserved
321322
getRNG()->random(&reply_data[8], 4); // random blob to help packet-hash uniqueness
322323

323324
return 12; // reply length
324325
}
325326

327+
void SensorMesh::handleCommand(uint32_t sender_timestamp, char* command, char* reply) {
328+
while (*command == ' ') command++; // skip leading spaces
329+
330+
if (strlen(command) > 4 && command[2] == '|') { // optional prefix (for companion radio CLI)
331+
memcpy(reply, command, 3); // reflect the prefix back
332+
reply += 3;
333+
command += 3;
334+
}
335+
336+
// handle sensor-specific CLI commands
337+
if (memcmp(command, "setperm ", 8) == 0) { // format: setperm {pubkey-hex} {permissions-int16}
338+
char* hex = &command[8];
339+
char* sp = strchr(hex, ' '); // look for separator char
340+
if (sp == NULL || sp - hex != PUB_KEY_SIZE*2) {
341+
strcpy(reply, "Err - bad pubkey len");
342+
} else {
343+
*sp++ = 0; // replace space with null terminator
344+
345+
uint8_t pubkey[PUB_KEY_SIZE];
346+
if (mesh::Utils::fromHex(pubkey, PUB_KEY_SIZE, hex)) {
347+
uint16_t perms = atoi(sp);
348+
applyContactPermissions(pubkey, perms);
349+
strcpy(reply, "OK");
350+
} else {
351+
strcpy(reply, "Err - bad pubkey");
352+
}
353+
}
354+
} else {
355+
_cli.handleCommand(sender_timestamp, command, reply); // common CLI commands
356+
}
357+
}
358+
326359
void SensorMesh::onAnonDataRecv(mesh::Packet* packet, const uint8_t* secret, const mesh::Identity& sender, uint8_t* data, size_t len) {
327360
if (packet->getPayloadType() == PAYLOAD_TYPE_ANON_REQ) { // received an initial request by a possible admin client (unknown at this stage)
328361
uint32_t timestamp;
329362
memcpy(&timestamp, data, 4);
330363

331364
data[len] = 0; // ensure null terminator
332-
333-
uint8_t req_code;
334-
uint8_t i = 4;
335-
if (data[4] < 32) { // non-print char, is a request code
336-
req_code = data[i++];
337-
} else {
338-
req_code = REQ_TYPE_LOGIN;
339-
}
340-
341-
uint8_t reply_len;
342-
if (req_code == REQ_TYPE_LOGIN) {
343-
reply_len = handleLoginReq(sender, secret, timestamp, &data[i]);
344-
} else {
345-
reply_len = handleRequest(false, timestamp, req_code, &data[i], len - i);
346-
}
365+
uint8_t reply_len = handleLoginReq(sender, secret, timestamp, &data[4]);
347366

348367
if (reply_len == 0) return; // invalid request
349368

@@ -406,7 +425,7 @@ void SensorMesh::onPeerDataRecv(mesh::Packet* packet, uint8_t type, int sender_i
406425
memcpy(&timestamp, data, 4);
407426

408427
if (timestamp > from.last_timestamp) { // prevent replay attacks
409-
uint8_t reply_len = handleRequest(from.isAdmin(), timestamp, data[4], &data[5], len - 5);
428+
uint8_t reply_len = handleRequest(from.isAdmin() ? 0xFFFF : from.permissions, timestamp, data[4], &data[5], len - 5);
410429
if (reply_len == 0) return; // invalid command
411430

412431
from.last_timestamp = timestamp;
@@ -445,9 +464,9 @@ void SensorMesh::onPeerDataRecv(mesh::Packet* packet, uint8_t type, int sender_i
445464
data[len] = 0; // need to make a C string again, with null terminator
446465

447466
uint8_t temp[166];
448-
const char *command = (const char *) &data[5];
467+
char *command = (char *) &data[5];
449468
char *reply = (char *) &temp[5];
450-
_cli.handleCommand(sender_timestamp, command, reply);
469+
handleCommand(sender_timestamp, command, reply);
451470

452471
int text_len = strlen(reply);
453472
if (text_len > 0) {
@@ -489,8 +508,9 @@ bool SensorMesh::onPeerPathRecv(mesh::Packet* packet, int sender_idx, const uint
489508
memcpy(from.out_path, path, from.out_path_len = path_len); // store a copy of path, for sendDirect()
490509
from.last_activity = getRTCClock()->getCurrentTime();
491510

511+
// REVISIT: maybe make ALL out_paths non-persisted to minimise flash writes??
492512
if (from.isAdmin()) {
493-
// only need to saveContacts() if this is an admin
513+
// only do saveContacts() (of this out_path change) if this is an admin
494514
dirty_contacts_expiry = futureMillis(LAZY_CONTACTS_WRITE_DELAY);
495515
}
496516

examples/simple_sensor/SensorMesh.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,20 @@
2323
#include <RTClib.h>
2424
#include <target.h>
2525

26+
#define PERM_IS_ADMIN 0x8000
27+
#define PERM_GET_TELEMETRY 0x0001
28+
#define PERM_GET_MIN_MAX_AVG 0x0002
29+
2630
struct ContactInfo {
2731
mesh::Identity id;
28-
uint8_t type; // 1 = admin, 0 = guest
29-
uint8_t flags;
32+
uint16_t permissions;
3033
int8_t out_path_len;
3134
uint8_t out_path[MAX_PATH_SIZE];
3235
uint8_t shared_secret[PUB_KEY_SIZE];
3336
uint32_t last_timestamp; // by THEIR clock (transient)
3437
uint32_t last_activity; // by OUR clock (transient)
3538

36-
bool isAdmin() const { return type != 0; }
39+
bool isAdmin() const { return (permissions & PERM_IS_ADMIN) != 0; }
3740
};
3841

3942
#ifndef FIRMWARE_BUILD_DATE
@@ -56,8 +59,8 @@ class SensorMesh : public mesh::Mesh, public CommonCLICallbacks {
5659
public:
5760
SensorMesh(mesh::MainBoard& board, mesh::Radio& radio, mesh::MillisecondClock& ms, mesh::RNG& rng, mesh::RTCClock& rtc, mesh::MeshTables& tables);
5861
void begin(FILESYSTEM* fs);
59-
CommonCLI* getCLI() { return &_cli; }
6062
void loop();
63+
void handleCommand(uint32_t sender_timestamp, char* command, char* reply);
6164

6265
// CommonCLI callbacks
6366
const char* getFirmwareVer() override { return FIRMWARE_VERSION; }
@@ -128,9 +131,10 @@ class SensorMesh : public mesh::Mesh, public CommonCLICallbacks {
128131
void loadContacts();
129132
void saveContacts();
130133
uint8_t handleLoginReq(const mesh::Identity& sender, const uint8_t* secret, uint32_t sender_timestamp, const uint8_t* data);
131-
uint8_t handleRequest(bool is_admin, uint32_t sender_timestamp, uint8_t req_type, uint8_t* payload, size_t payload_len);
134+
uint8_t handleRequest(uint16_t perms, uint32_t sender_timestamp, uint8_t req_type, uint8_t* payload, size_t payload_len);
132135
mesh::Packet* createSelfAdvert();
133136
ContactInfo* putContact(const mesh::Identity& id);
137+
void applyContactPermissions(const uint8_t* pubkey, uint16_t perms);
134138

135139
void sendAlert(const char* text);
136140

0 commit comments

Comments
 (0)