|
|
@@ -0,0 +1,774 @@
|
|
|
+#include "SensorMesh.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 "sensor"
|
|
|
+#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
|
|
|
+
|
|
|
+#ifndef SENSOR_READ_INTERVAL_SECS
|
|
|
+ #define SENSOR_READ_INTERVAL_SECS 60
|
|
|
+#endif
|
|
|
+
|
|
|
+/* ------------------------------ Code -------------------------------- */
|
|
|
+
|
|
|
+#define REQ_TYPE_LOGIN 0x00
|
|
|
+#define REQ_TYPE_GET_STATUS 0x01
|
|
|
+#define REQ_TYPE_KEEP_ALIVE 0x02
|
|
|
+#define REQ_TYPE_GET_TELEMETRY_DATA 0x03
|
|
|
+#define REQ_TYPE_GET_AVG_MIN_MAX 0x04
|
|
|
+
|
|
|
+#define RESP_SERVER_LOGIN_OK 0 // response to ANON_REQ
|
|
|
+
|
|
|
+#define CLI_REPLY_DELAY_MILLIS 1000
|
|
|
+
|
|
|
+#define LAZY_CONTACTS_WRITE_DELAY 5000
|
|
|
+
|
|
|
+static File openAppend(FILESYSTEM* _fs, 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
|
|
|
+}
|
|
|
+
|
|
|
+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 SensorMesh::loadContacts() {
|
|
|
+ num_contacts = 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) {
|
|
|
+ ContactInfo c;
|
|
|
+ uint8_t pub_key[32];
|
|
|
+ uint8_t unused[5];
|
|
|
+
|
|
|
+ bool success = (file.read(pub_key, 32) == 32);
|
|
|
+ success = success && (file.read((uint8_t *) &c.permissions, 2) == 2);
|
|
|
+ success = success && (file.read(unused, 5) == 5);
|
|
|
+ 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_contacts < MAX_CONTACTS) {
|
|
|
+ contacts[num_contacts++] = c;
|
|
|
+ } else {
|
|
|
+ full = true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ file.close();
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+void SensorMesh::saveContacts() {
|
|
|
+ File file = openWrite(_fs, "/s_contacts");
|
|
|
+ if (file) {
|
|
|
+ uint8_t unused[5];
|
|
|
+ memset(unused, 0, sizeof(unused));
|
|
|
+
|
|
|
+ for (int i = 0; i < num_contacts; i++) {
|
|
|
+ auto c = &contacts[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, 2) == 2);
|
|
|
+ success = success && (file.write(unused, 5) == 5);
|
|
|
+ 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();
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+static uint8_t getDataSize(uint8_t type) {
|
|
|
+ switch (type) {
|
|
|
+ case LPP_GPS:
|
|
|
+ return 9;
|
|
|
+ case LPP_POLYLINE:
|
|
|
+ return 8; // TODO: this is MINIMIUM
|
|
|
+ case LPP_GYROMETER:
|
|
|
+ case LPP_ACCELEROMETER:
|
|
|
+ return 6;
|
|
|
+ case LPP_GENERIC_SENSOR:
|
|
|
+ case LPP_FREQUENCY:
|
|
|
+ case LPP_DISTANCE:
|
|
|
+ case LPP_ENERGY:
|
|
|
+ case LPP_UNIXTIME:
|
|
|
+ return 4;
|
|
|
+ case LPP_COLOUR:
|
|
|
+ return 3;
|
|
|
+ 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:
|
|
|
+ return 2;
|
|
|
+ }
|
|
|
+ return 1;
|
|
|
+}
|
|
|
+
|
|
|
+static uint32_t getMultiplier(uint8_t type) {
|
|
|
+ switch (type) {
|
|
|
+ case LPP_CURRENT:
|
|
|
+ case LPP_DISTANCE:
|
|
|
+ case LPP_ENERGY:
|
|
|
+ return 1000;
|
|
|
+ case LPP_VOLTAGE:
|
|
|
+ case LPP_ANALOG_INPUT:
|
|
|
+ case LPP_ANALOG_OUTPUT:
|
|
|
+ return 100;
|
|
|
+ case LPP_TEMPERATURE:
|
|
|
+ case LPP_BAROMETRIC_PRESSURE:
|
|
|
+ return 10;
|
|
|
+ }
|
|
|
+ return 1;
|
|
|
+}
|
|
|
+
|
|
|
+static bool isSigned(uint8_t type) {
|
|
|
+ return type == LPP_ALTITUDE || type == LPP_TEMPERATURE || type == LPP_GYROMETER ||
|
|
|
+ type == LPP_ANALOG_INPUT || type == LPP_ANALOG_OUTPUT || type == LPP_GPS || type == LPP_ACCELEROMETER;
|
|
|
+}
|
|
|
+
|
|
|
+static 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);
|
|
|
+}
|
|
|
+
|
|
|
+static uint8_t putFloat(uint8_t * dest, float value, uint8_t size, uint32_t multiplier, bool is_signed) {
|
|
|
+ // check sign
|
|
|
+ bool sign = value < 0;
|
|
|
+ if (sign) value = -value;
|
|
|
+
|
|
|
+ // get value to store
|
|
|
+ uint32_t v = value * multiplier;
|
|
|
+
|
|
|
+ // format an uint32_t as if it was an int32_t
|
|
|
+ if (is_signed & sign) {
|
|
|
+ uint32_t mask = (1 << (size * 8)) - 1;
|
|
|
+ v = v & mask;
|
|
|
+ if (sign) v = mask - v + 1;
|
|
|
+ }
|
|
|
+
|
|
|
+ // add bytes (MSB first)
|
|
|
+ for (uint8_t i=1; i<=size; i++) {
|
|
|
+ dest[size - i] = (v & 0xFF);
|
|
|
+ v >>= 8;
|
|
|
+ }
|
|
|
+ return size;
|
|
|
+}
|
|
|
+
|
|
|
+uint8_t SensorMesh::handleRequest(uint16_t perms, uint32_t sender_timestamp, uint8_t req_type, uint8_t* payload, size_t payload_len) {
|
|
|
+ memcpy(reply_data, &sender_timestamp, 4); // reflect sender_timestamp back in response packet (kind of like a 'tag')
|
|
|
+
|
|
|
+ if (req_type == REQ_TYPE_GET_TELEMETRY_DATA && (perms & PERM_GET_TELEMETRY) != 0) {
|
|
|
+ telemetry.reset();
|
|
|
+ telemetry.addVoltage(TELEM_CHANNEL_SELF, (float)board.getBattMilliVolts() / 1000.0f);
|
|
|
+ // query other sensors -- target specific
|
|
|
+ sensors.querySensors(0xFF, telemetry); // allow all telemetry permissions for admin or guest
|
|
|
+
|
|
|
+ uint8_t tlen = telemetry.getSize();
|
|
|
+ memcpy(&reply_data[4], telemetry.getBuffer(), tlen);
|
|
|
+ return 4 + tlen; // reply_len
|
|
|
+ }
|
|
|
+ if (req_type == REQ_TYPE_GET_AVG_MIN_MAX && (perms & PERM_GET_MIN_MAX_AVG) != 0) {
|
|
|
+ uint32_t start_secs_ago, end_secs_ago;
|
|
|
+ memcpy(&start_secs_ago, &payload[0], 4);
|
|
|
+ memcpy(&end_secs_ago, &payload[4], 4);
|
|
|
+ uint8_t res1 = payload[8]; // reserved for future (extra query params)
|
|
|
+ uint8_t res2 = payload[8];
|
|
|
+
|
|
|
+ MinMaxAvg data[8];
|
|
|
+ int n;
|
|
|
+ if (res1 == 0 && res2 == 0) {
|
|
|
+ n = querySeriesData(start_secs_ago, end_secs_ago, data, 8);
|
|
|
+ } else {
|
|
|
+ n = 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ uint8_t ofs = 4;
|
|
|
+ {
|
|
|
+ uint32_t now = getRTCClock()->getCurrentTime();
|
|
|
+ memcpy(&reply_data[ofs], &now, 4); ofs += 4;
|
|
|
+ }
|
|
|
+
|
|
|
+ for (int i = 0; i < n; i++) {
|
|
|
+ auto d = &data[i];
|
|
|
+ reply_data[ofs++] = d->_channel;
|
|
|
+ reply_data[ofs++] = d->_lpp_type;
|
|
|
+ uint8_t sz = getDataSize(d->_lpp_type);
|
|
|
+ uint32_t mult = getMultiplier(d->_lpp_type);
|
|
|
+ bool is_signed = isSigned(d->_lpp_type);
|
|
|
+ ofs += putFloat(&reply_data[ofs], d->_min, sz, mult, is_signed);
|
|
|
+ ofs += putFloat(&reply_data[ofs], d->_max, sz, mult, is_signed);
|
|
|
+ ofs += putFloat(&reply_data[ofs], d->_avg, sz, mult, is_signed);
|
|
|
+ }
|
|
|
+ return ofs;
|
|
|
+ }
|
|
|
+ return 0; // unknown command
|
|
|
+}
|
|
|
+
|
|
|
+mesh::Packet* SensorMesh::createSelfAdvert() {
|
|
|
+ uint8_t app_data[MAX_ADVERT_DATA_SIZE];
|
|
|
+ uint8_t app_data_len;
|
|
|
+ {
|
|
|
+ AdvertDataBuilder builder(ADV_TYPE_SENSOR, _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);
|
|
|
+}
|
|
|
+
|
|
|
+ContactInfo* SensorMesh::putContact(const mesh::Identity& id) {
|
|
|
+ uint32_t min_time = 0xFFFFFFFF;
|
|
|
+ ContactInfo* oldest = &contacts[MAX_CONTACTS - 1];
|
|
|
+ for (int i = 0; i < num_contacts; i++) {
|
|
|
+ if (id.matches(contacts[i].id)) return &contacts[i]; // already known
|
|
|
+ if (!contacts[i].isAdmin() && contacts[i].last_activity < min_time) {
|
|
|
+ oldest = &contacts[i];
|
|
|
+ min_time = oldest->last_activity;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ ContactInfo* c;
|
|
|
+ if (num_contacts < MAX_CONTACTS) {
|
|
|
+ c = &contacts[num_contacts++];
|
|
|
+ } else {
|
|
|
+ c = oldest; // evict least active contact
|
|
|
+ }
|
|
|
+ memset(c, 0, sizeof(*c));
|
|
|
+ c->id = id;
|
|
|
+ c->out_path_len = -1; // initially out_path is unknown
|
|
|
+ return c;
|
|
|
+}
|
|
|
+
|
|
|
+void SensorMesh::applyContactPermissions(const uint8_t* pubkey, uint16_t perms) {
|
|
|
+ mesh::Identity id(pubkey);
|
|
|
+ auto c = putContact(id);
|
|
|
+
|
|
|
+ if (perms == 0) { // no permissions, remove from contacts
|
|
|
+ memset(c, 0, sizeof(*c));
|
|
|
+ } else {
|
|
|
+ c->permissions = perms; // update their permissions
|
|
|
+ self_id.calcSharedSecret(c->shared_secret, pubkey);
|
|
|
+ }
|
|
|
+ dirty_contacts_expiry = futureMillis(LAZY_CONTACTS_WRITE_DELAY); // trigger saveContacts()
|
|
|
+}
|
|
|
+
|
|
|
+void SensorMesh::sendAlert(const char* text) {
|
|
|
+ int text_len = strlen(text);
|
|
|
+
|
|
|
+ // send text message to all contacts with RECV_ALERT permission
|
|
|
+ for (int i = 0; i < num_contacts; i++) {
|
|
|
+ auto c = &contacts[i];
|
|
|
+ if ((c->permissions & PERM_RECV_ALERTS) == 0) continue; // contact does NOT want alerts
|
|
|
+
|
|
|
+ uint8_t data[MAX_PACKET_PAYLOAD];
|
|
|
+ uint32_t now = getRTCClock()->getCurrentTimeUnique(); // need different timestamp per packet
|
|
|
+ memcpy(data, &now, 4);
|
|
|
+ data[4] = (TXT_TYPE_PLAIN << 2); // attempt and flags
|
|
|
+ memcpy(&data[5], text, text_len);
|
|
|
+ // calc expected ACK reply
|
|
|
+ // uint32_t expected_ack;
|
|
|
+ // mesh::Utils::sha256((uint8_t *)&expected_ack, 4, data, 5 + text_len, self_id.pub_key, PUB_KEY_SIZE);
|
|
|
+
|
|
|
+ auto pkt = createDatagram(PAYLOAD_TYPE_TXT_MSG, c->id, c->shared_secret, data, 5 + text_len);
|
|
|
+ if (pkt) {
|
|
|
+ if (c->out_path_len >= 0) { // we have an out_path, so send DIRECT
|
|
|
+ sendDirect(pkt, c->out_path, c->out_path_len);
|
|
|
+ } else {
|
|
|
+ sendFlood(pkt);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+void SensorMesh::alertIf(bool condition, Trigger& t, const char* text) {
|
|
|
+ if (condition) {
|
|
|
+ if (!t.triggered) {
|
|
|
+ t.triggered = true;
|
|
|
+ t.time = getRTCClock()->getCurrentTime();
|
|
|
+ sendAlert(text);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if (t.triggered) {
|
|
|
+ t.triggered = false;
|
|
|
+ // TODO: apply debounce logic
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+float SensorMesh::getAirtimeBudgetFactor() const {
|
|
|
+ return _prefs.airtime_factor;
|
|
|
+}
|
|
|
+
|
|
|
+bool SensorMesh::allowPacketForward(const mesh::Packet* packet) {
|
|
|
+ if (_prefs.disable_fwd) return false;
|
|
|
+ if (packet->isRouteFlood() && packet->path_len >= _prefs.flood_max) return false;
|
|
|
+ return true;
|
|
|
+}
|
|
|
+
|
|
|
+int SensorMesh::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 SensorMesh::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 SensorMesh::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;
|
|
|
+}
|
|
|
+int SensorMesh::getInterferenceThreshold() const {
|
|
|
+ return _prefs.interference_threshold;
|
|
|
+}
|
|
|
+int SensorMesh::getAGCResetInterval() const {
|
|
|
+ return ((int)_prefs.agc_reset_interval) * 4000; // milliseconds
|
|
|
+}
|
|
|
+
|
|
|
+uint8_t SensorMesh::handleLoginReq(const mesh::Identity& sender, const uint8_t* secret, uint32_t sender_timestamp, const uint8_t* data) {
|
|
|
+ if (strcmp((char *) data, _prefs.password) != 0) { // check for valid password
|
|
|
+ #if MESH_DEBUG
|
|
|
+ MESH_DEBUG_PRINTLN("Invalid password: %s", &data[4]);
|
|
|
+ #endif
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ auto client = putContact(sender); // add to contacts (if not already known)
|
|
|
+ if (sender_timestamp <= client->last_timestamp) {
|
|
|
+ MESH_DEBUG_PRINTLN("Possible login replay attack!");
|
|
|
+ return 0; // FATAL: client table is full -OR- replay attack
|
|
|
+ }
|
|
|
+
|
|
|
+ MESH_DEBUG_PRINTLN("Login success!");
|
|
|
+ client->last_timestamp = sender_timestamp;
|
|
|
+ client->last_activity = getRTCClock()->getCurrentTime();
|
|
|
+ client->permissions = PERM_IS_ADMIN | PERM_RECV_ALERTS;
|
|
|
+ memcpy(client->shared_secret, secret, PUB_KEY_SIZE);
|
|
|
+
|
|
|
+ dirty_contacts_expiry = futureMillis(LAZY_CONTACTS_WRITE_DELAY);
|
|
|
+
|
|
|
+ uint32_t now = getRTCClock()->getCurrentTimeUnique();
|
|
|
+ memcpy(reply_data, &now, 4); // response packets always prefixed with timestamp
|
|
|
+ reply_data[4] = RESP_SERVER_LOGIN_OK;
|
|
|
+ reply_data[5] = 0; // NEW: recommended keep-alive interval (secs / 16)
|
|
|
+ reply_data[6] = 1; // 1 = is admin
|
|
|
+ reply_data[7] = 0; // FUTURE: reserved
|
|
|
+ getRNG()->random(&reply_data[8], 4); // random blob to help packet-hash uniqueness
|
|
|
+
|
|
|
+ return 12; // reply length
|
|
|
+}
|
|
|
+
|
|
|
+void SensorMesh::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;
|
|
|
+ }
|
|
|
+
|
|
|
+ // handle sensor-specific CLI commands
|
|
|
+ if (memcmp(command, "setperm ", 8) == 0) { // format: setperm {pubkey-hex} {permissions-int16}
|
|
|
+ char* hex = &command[8];
|
|
|
+ char* sp = strchr(hex, ' '); // look for separator char
|
|
|
+ if (sp == NULL || sp - hex != PUB_KEY_SIZE*2) {
|
|
|
+ strcpy(reply, "Err - bad pubkey len");
|
|
|
+ } else {
|
|
|
+ *sp++ = 0; // replace space with null terminator
|
|
|
+
|
|
|
+ uint8_t pubkey[PUB_KEY_SIZE];
|
|
|
+ if (mesh::Utils::fromHex(pubkey, PUB_KEY_SIZE, hex)) {
|
|
|
+ uint16_t perms = atoi(sp);
|
|
|
+ applyContactPermissions(pubkey, perms);
|
|
|
+ strcpy(reply, "OK");
|
|
|
+ } else {
|
|
|
+ strcpy(reply, "Err - bad pubkey");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else if (sender_timestamp == 0 && strcmp(command, "getperm") == 0) {
|
|
|
+ Serial.println("Permissions:");
|
|
|
+ for (int i = 0; i < num_contacts; i++) {
|
|
|
+ auto c = &contacts[i];
|
|
|
+
|
|
|
+ mesh::Utils::printHex(Serial, c->id.pub_key, PUB_KEY_SIZE);
|
|
|
+ Serial.printf(" %04X\n", c->permissions);
|
|
|
+ }
|
|
|
+ reply[0] = 0;
|
|
|
+ } else {
|
|
|
+ _cli.handleCommand(sender_timestamp, command, reply); // common CLI commands
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+void SensorMesh::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);
|
|
|
+
|
|
|
+ data[len] = 0; // ensure null terminator
|
|
|
+ uint8_t reply_len = handleLoginReq(sender, secret, timestamp, &data[4]);
|
|
|
+
|
|
|
+ if (reply_len == 0) return; // invalid request
|
|
|
+
|
|
|
+ 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, 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, sender, secret, reply_data, reply_len);
|
|
|
+ if (reply) sendFlood(reply, SERVER_RESPONSE_DELAY);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+int SensorMesh::searchPeersByHash(const uint8_t* hash) {
|
|
|
+ int n = 0;
|
|
|
+ for (int i = 0; i < num_contacts && n < MAX_SEARCH_RESULTS; i++) {
|
|
|
+ if (contacts[i].id.isHashMatch(hash)) {
|
|
|
+ matching_peer_indexes[n++] = i; // store the INDEXES of matching contacts (for subsequent 'peer' methods)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return n;
|
|
|
+}
|
|
|
+
|
|
|
+void SensorMesh::getPeerSharedSecret(uint8_t* dest_secret, int peer_idx) {
|
|
|
+ int i = matching_peer_indexes[peer_idx];
|
|
|
+ if (i >= 0 && i < num_contacts) {
|
|
|
+ // lookup pre-calculated shared_secret
|
|
|
+ memcpy(dest_secret, contacts[i].shared_secret, PUB_KEY_SIZE);
|
|
|
+ } else {
|
|
|
+ MESH_DEBUG_PRINTLN("getPeerSharedSecret: Invalid peer idx: %d", i);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+void SensorMesh::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_contacts) {
|
|
|
+ MESH_DEBUG_PRINTLN("onPeerDataRecv: Invalid sender idx: %d", i);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ ContactInfo& from = contacts[i];
|
|
|
+
|
|
|
+ if (type == PAYLOAD_TYPE_REQ) { // request (from a known contact)
|
|
|
+ uint32_t timestamp;
|
|
|
+ memcpy(×tamp, data, 4);
|
|
|
+
|
|
|
+ if (timestamp > from.last_timestamp) { // prevent replay attacks
|
|
|
+ uint8_t reply_len = handleRequest(from.isAdmin() ? 0xFFFF : from.permissions, timestamp, data[4], &data[5], len - 5);
|
|
|
+ if (reply_len == 0) return; // invalid command
|
|
|
+
|
|
|
+ from.last_timestamp = timestamp;
|
|
|
+ from.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(from.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, from.id, secret, reply_data, reply_len);
|
|
|
+ if (reply) {
|
|
|
+ if (from.out_path_len >= 0) { // we have an out_path, so send DIRECT
|
|
|
+ sendDirect(reply, from.out_path, from.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 && from.isAdmin()) { // 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_CLI_DATA)) {
|
|
|
+ MESH_DEBUG_PRINTLN("onPeerDataRecv: unsupported text type received: flags=%02x", (uint32_t)flags);
|
|
|
+ } else if (sender_timestamp > from.last_timestamp) { // prevent replay attacks
|
|
|
+ from.last_timestamp = sender_timestamp;
|
|
|
+ from.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
|
|
|
+
|
|
|
+ uint8_t temp[166];
|
|
|
+ char *command = (char *) &data[5];
|
|
|
+ char *reply = (char *) &temp[5];
|
|
|
+ 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);
|
|
|
+
|
|
|
+ auto reply = createDatagram(PAYLOAD_TYPE_TXT_MSG, from.id, secret, temp, 5 + text_len);
|
|
|
+ if (reply) {
|
|
|
+ if (from.out_path_len < 0) {
|
|
|
+ sendFlood(reply, CLI_REPLY_DELAY_MILLIS);
|
|
|
+ } else {
|
|
|
+ sendDirect(reply, from.out_path, from.out_path_len, CLI_REPLY_DELAY_MILLIS);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ MESH_DEBUG_PRINTLN("onPeerDataRecv: possible replay attack detected");
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+bool SensorMesh::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) {
|
|
|
+ int i = matching_peer_indexes[sender_idx];
|
|
|
+ if (i < 0 || i >= num_contacts) {
|
|
|
+ MESH_DEBUG_PRINTLN("onPeerPathRecv: Invalid sender idx: %d", i);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ ContactInfo& from = contacts[i];
|
|
|
+
|
|
|
+ MESH_DEBUG_PRINTLN("PATH to contact, path_len=%d", (uint32_t) path_len);
|
|
|
+ // NOTE: for this impl, we just replace the current 'out_path' regardless, whenever sender sends us a new out_path.
|
|
|
+ // FUTURE: could store multiple out_paths per contact, and try to find which is the 'best'(?)
|
|
|
+ memcpy(from.out_path, path, from.out_path_len = path_len); // store a copy of path, for sendDirect()
|
|
|
+ from.last_activity = getRTCClock()->getCurrentTime();
|
|
|
+
|
|
|
+ // REVISIT: maybe make ALL out_paths non-persisted to minimise flash writes??
|
|
|
+ if (from.isAdmin()) {
|
|
|
+ // only do saveContacts() (of this out_path change) if this is an admin
|
|
|
+ dirty_contacts_expiry = futureMillis(LAZY_CONTACTS_WRITE_DELAY);
|
|
|
+ }
|
|
|
+
|
|
|
+ // NOTE: no reciprocal path send!!
|
|
|
+ return false;
|
|
|
+}
|
|
|
+
|
|
|
+SensorMesh::SensorMesh(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)
|
|
|
+{
|
|
|
+ num_contacts = 0;
|
|
|
+ next_local_advert = next_flood_advert = 0;
|
|
|
+ dirty_contacts_expiry = 0;
|
|
|
+ last_read_time = 0;
|
|
|
+
|
|
|
+ // 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 = 0; // disabled
|
|
|
+ _prefs.disable_fwd = true;
|
|
|
+ _prefs.flood_max = 64;
|
|
|
+ _prefs.interference_threshold = 0; // disabled
|
|
|
+}
|
|
|
+
|
|
|
+void SensorMesh::begin(FILESYSTEM* fs) {
|
|
|
+ mesh::Mesh::begin();
|
|
|
+ _fs = fs;
|
|
|
+ // load persisted prefs
|
|
|
+ _cli.loadPrefs(_fs);
|
|
|
+
|
|
|
+ loadContacts();
|
|
|
+
|
|
|
+ radio_set_params(_prefs.freq, _prefs.bw, _prefs.sf, _prefs.cr);
|
|
|
+ radio_set_tx_power(_prefs.tx_power_dbm);
|
|
|
+
|
|
|
+ updateAdvertTimer();
|
|
|
+ updateFloodAdvertTimer();
|
|
|
+}
|
|
|
+
|
|
|
+bool SensorMesh::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 SensorMesh::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 SensorMesh::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 SensorMesh::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 SensorMesh::setTxPower(uint8_t power_dbm) {
|
|
|
+ radio_set_tx_power(power_dbm);
|
|
|
+}
|
|
|
+
|
|
|
+float SensorMesh::getTelemValue(uint8_t channel, uint8_t type) {
|
|
|
+ auto buf = telemetry.getBuffer();
|
|
|
+ uint8_t size = telemetry.getSize();
|
|
|
+ uint8_t i = 0;
|
|
|
+
|
|
|
+ while (i + 2 < size) {
|
|
|
+ // Get channel #
|
|
|
+ uint8_t ch = buf[i++];
|
|
|
+ // Get data type
|
|
|
+ uint8_t t = buf[i++];
|
|
|
+ uint8_t sz = getDataSize(t);
|
|
|
+
|
|
|
+ if (ch == channel && t == type) {
|
|
|
+ return getFloat(&buf[i], sz, getMultiplier(t), isSigned(t));
|
|
|
+ }
|
|
|
+ i += sz; // skip
|
|
|
+ }
|
|
|
+ return 0.0f; // not found
|
|
|
+}
|
|
|
+
|
|
|
+bool SensorMesh::getGPS(uint8_t channel, float& lat, float& lon, float& alt) {
|
|
|
+ return false; // TODO
|
|
|
+}
|
|
|
+
|
|
|
+void SensorMesh::loop() {
|
|
|
+ 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
|
|
|
+ }
|
|
|
+
|
|
|
+ uint32_t curr = getRTCClock()->getCurrentTime();
|
|
|
+ if (curr >= last_read_time + SENSOR_READ_INTERVAL_SECS) {
|
|
|
+ telemetry.reset();
|
|
|
+ telemetry.addVoltage(TELEM_CHANNEL_SELF, (float)board.getBattMilliVolts() / 1000.0f);
|
|
|
+ // query other sensors -- target specific
|
|
|
+ sensors.querySensors(0xFF, telemetry); // allow all telemetry permissions
|
|
|
+
|
|
|
+ onSensorDataRead();
|
|
|
+
|
|
|
+ last_read_time = curr;
|
|
|
+ }
|
|
|
+
|
|
|
+ // is there are pending dirty contacts write needed?
|
|
|
+ if (dirty_contacts_expiry && millisHasNowPassed(dirty_contacts_expiry)) {
|
|
|
+ saveContacts();
|
|
|
+ dirty_contacts_expiry = 0;
|
|
|
+ }
|
|
|
+}
|