From e16d5c7e397183e63e297bba5ccf3bad1daf8df0 Mon Sep 17 00:00:00 2001 From: geeksville Date: Fri, 14 Feb 2020 04:32:08 -0800 Subject: [PATCH] never let phone override macadddr, only override fields the phone has set --- src/MeshBluetoothService.cpp | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/src/MeshBluetoothService.cpp b/src/MeshBluetoothService.cpp index 7416351c..2f4d11d2 100644 --- a/src/MeshBluetoothService.cpp +++ b/src/MeshBluetoothService.cpp @@ -36,14 +36,20 @@ public: } void onWrite(BLECharacteristic *c) + { + writeToDest(c, my_struct); + } + +protected: + /// like onWrite, but we provide an different destination to write to, for use by subclasses that + /// want to optionally ignore parts of writes. + /// returns true for success + bool writeToDest(BLECharacteristic *c, void *dest) { // dumpCharacteristic(pCharacteristic); DEBUG_MSG("Got on proto write\n"); std::string src = c->getValue(); - if (pb_decode_from_bytes((const uint8_t *)src.c_str(), src.length(), fields, my_struct)) - { - // Success, the bytes are now in our struct - do nothing else - } + return pb_decode_from_bytes((const uint8_t *)src.c_str(), src.length(), fields, dest); } }; @@ -107,8 +113,18 @@ public: void onWrite(BLECharacteristic *c) { - ProtobufCharacteristic::onWrite(c); - service.reloadOwner(); + static User o; // if the phone doesn't set ID we are careful to keep ours, we also always keep our macaddr + if (writeToDest(c, &o)) + { + if (*o.long_name) + strcpy(owner.long_name, o.long_name); + if (*o.short_name) + strcpy(owner.short_name, o.short_name); + if (*o.id) + strcpy(owner.id, o.id); + + service.reloadOwner(); + } } };