From 4a05874dba4392264fb2a5ce4a8e299dc7c29b55 Mon Sep 17 00:00:00 2001 From: Ben Meadors Date: Sun, 19 May 2024 07:24:10 -0500 Subject: [PATCH 1/4] Try-fix: Remove logging of actual payload strings (and compressed) for TAK packets (#3922) * Remove logging of actual payload strings (and compressed) for TAK packets * Don't assert / reboot. Log error and skip decode --- src/mesh/Router.cpp | 5 ++++- src/modules/AtakPluginModule.cpp | 31 ++++++++++--------------------- 2 files changed, 14 insertions(+), 22 deletions(-) diff --git a/src/mesh/Router.cpp b/src/mesh/Router.cpp index 4189bca66..3141d986b 100644 --- a/src/mesh/Router.cpp +++ b/src/mesh/Router.cpp @@ -311,7 +311,10 @@ bool perhapsDecode(meshtastic_MeshPacket *p) if (channels.decryptForHash(chIndex, p->channel)) { // Try to decrypt the packet if we can size_t rawSize = p->encrypted.size; - assert(rawSize <= sizeof(bytes)); + if (rawSize > sizeof(bytes)) { + LOG_ERROR("Packet too large to attempt decription! (rawSize=%d > 256)\n", rawSize); + return false; + } memcpy(bytes, p->encrypted.bytes, rawSize); // we have to copy into a scratch buffer, because these bytes are a union with the decoded protobuf crypto->decrypt(p->from, p->id, rawSize, bytes); diff --git a/src/modules/AtakPluginModule.cpp b/src/modules/AtakPluginModule.cpp index 64a85e2bf..b460602ef 100644 --- a/src/modules/AtakPluginModule.cpp +++ b/src/modules/AtakPluginModule.cpp @@ -64,40 +64,33 @@ void AtakPluginModule::alterReceivedProtobuf(meshtastic_MeshPacket &mp, meshtast { // From Phone (EUD) if (mp.from == 0) { - LOG_DEBUG("Received uncompressed TAK payload from phone with %d bytes\n", mp.decoded.payload.size); + LOG_DEBUG("Received uncompressed TAK payload from phone: %d bytes\n", mp.decoded.payload.size); // Compress for LoRA transport auto compressed = cloneTAKPacketData(t); compressed.is_compressed = true; if (t->has_contact) { auto length = unishox2_compress_simple(t->contact.callsign, strlen(t->contact.callsign), compressed.contact.callsign); - LOG_DEBUG("Uncompressed callsign '%s' - %d bytes\n", t->contact.callsign, strlen(t->contact.callsign)); - LOG_DEBUG("Compressed callsign '%s' - %d bytes\n", t->contact.callsign, length); + LOG_DEBUG("Compressed callsign: %d bytes\n", length); length = unishox2_compress_simple(t->contact.device_callsign, strlen(t->contact.device_callsign), compressed.contact.device_callsign); - LOG_DEBUG("Uncompressed device_callsign '%s' - %d bytes\n", t->contact.device_callsign, - strlen(t->contact.device_callsign)); - LOG_DEBUG("Compressed device_callsign '%s' - %d bytes\n", compressed.contact.device_callsign, length); + LOG_DEBUG("Compressed device_callsign: %d bytes\n", length); } if (t->which_payload_variant == meshtastic_TAKPacket_chat_tag) { auto length = unishox2_compress_simple(t->payload_variant.chat.message, strlen(t->payload_variant.chat.message), compressed.payload_variant.chat.message); - LOG_DEBUG("Uncompressed chat message '%s' - %d bytes\n", t->payload_variant.chat.message, - strlen(t->payload_variant.chat.message)); - LOG_DEBUG("Compressed chat message '%s' - %d bytes\n", compressed.payload_variant.chat.message, length); + LOG_DEBUG("Compressed chat message: %d bytes\n", length); if (t->payload_variant.chat.has_to) { compressed.payload_variant.chat.has_to = true; length = unishox2_compress_simple(t->payload_variant.chat.to, strlen(t->payload_variant.chat.to), compressed.payload_variant.chat.to); - LOG_DEBUG("Uncompressed chat to '%s' - %d bytes\n", t->payload_variant.chat.to, - strlen(t->payload_variant.chat.to)); - LOG_DEBUG("Compressed chat to '%s' - %d bytes\n", compressed.payload_variant.chat.to, length); + LOG_DEBUG("Compressed chat to: %d bytes\n", length); } } mp.decoded.payload.size = pb_encode_to_bytes(mp.decoded.payload.bytes, sizeof(mp.decoded.payload.bytes), meshtastic_TAKPacket_fields, &compressed); - LOG_DEBUG("Final payload size of %d bytes\n", mp.decoded.payload.size); + LOG_DEBUG("Final payload: %d bytes\n", mp.decoded.payload.size); } else { if (!t->is_compressed) { // Not compressed. Something is wrong @@ -113,27 +106,23 @@ void AtakPluginModule::alterReceivedProtobuf(meshtastic_MeshPacket &mp, meshtast auto length = unishox2_decompress_simple(t->contact.callsign, strlen(t->contact.callsign), uncompressed.contact.callsign); - LOG_DEBUG("Compressed callsign: %d bytes\n", strlen(t->contact.callsign)); - LOG_DEBUG("Decompressed callsign: '%s' @ %d bytes\n", uncompressed.contact.callsign, length); + LOG_DEBUG("Decompressed callsign: %d bytes\n", length); length = unishox2_decompress_simple(t->contact.device_callsign, strlen(t->contact.device_callsign), uncompressed.contact.device_callsign); - LOG_DEBUG("Compressed device_callsign: %d bytes\n", strlen(t->contact.device_callsign)); - LOG_DEBUG("Decompressed device_callsign: '%s' @ %d bytes\n", uncompressed.contact.device_callsign, length); + LOG_DEBUG("Decompressed device_callsign: %d bytes\n", length); } if (uncompressed.which_payload_variant == meshtastic_TAKPacket_chat_tag) { auto length = unishox2_decompress_simple(t->payload_variant.chat.message, strlen(t->payload_variant.chat.message), uncompressed.payload_variant.chat.message); - LOG_DEBUG("Compressed chat message: %d bytes\n", strlen(t->payload_variant.chat.message)); - LOG_DEBUG("Decompressed chat message: '%s' @ %d bytes\n", uncompressed.payload_variant.chat.message, length); + LOG_DEBUG("Decompressed chat message: %d bytes\n", length); if (t->payload_variant.chat.has_to) { uncompressed.payload_variant.chat.has_to = true; length = unishox2_decompress_simple(t->payload_variant.chat.to, strlen(t->payload_variant.chat.to), uncompressed.payload_variant.chat.to); - LOG_DEBUG("Compressed chat to: %d bytes\n", strlen(t->payload_variant.chat.to)); - LOG_DEBUG("Decompressed chat to: '%s' @ %d bytes\n", uncompressed.payload_variant.chat.to, length); + LOG_DEBUG("Decompressed chat to: %d bytes\n", length); } } decompressedCopy->decoded.payload.size = From 3719ddac038947bdb3a6c46ef4b3563510ffe684 Mon Sep 17 00:00:00 2001 From: Jorropo Date: Sun, 19 May 2024 14:25:05 +0200 Subject: [PATCH 2/4] automatically propose to setup virtualenv if platformio is having issues in native build (#3923) --- bin/build-native.sh | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/bin/build-native.sh b/bin/build-native.sh index 9d31d091a..e8ed61bcf 100755 --- a/bin/build-native.sh +++ b/bin/build-native.sh @@ -2,6 +2,17 @@ set -e +platformioFailed() { + [[ $VIRTUAL_ENV != "" ]] && exit 1 # don't hint at virtualenv if it's already in use + echo -e "\nThere were issues running platformio and you are not using a virtual environment." \ + "\nYou may try setting up virtualenv and downloading the latest platformio from pip:" \ + "\n\tvirtualenv venv" \ + "\n\tsource venv/bin/activate" \ + "\n\tpip install platformio" \ + "\n\t./bin/build-native.sh # retry building" + exit 1 +} + VERSION=$(bin/buildinfo.py long) SHORT_VERSION=$(bin/buildinfo.py short) @@ -13,8 +24,8 @@ mkdir -p $OUTDIR/ rm -r $OUTDIR/* || true # Important to pull latest version of libs into all device flavors, otherwise some devices might be stale -platformio pkg update --environment native -pio run --environment native +platformio pkg update --environment native || platformioFailed +pio run --environment native || platformioFailed cp .pio/build/native/program "$OUTDIR/meshtasticd_linux_$(uname -m)" cp bin/device-install.* $OUTDIR cp bin/device-update.* $OUTDIR From 3a628047ef40f34060e8c8f7e5132d24be45bcad Mon Sep 17 00:00:00 2001 From: HarukiToreda <116696711+HarukiToreda@users.noreply.github.com> Date: Sat, 18 May 2024 22:14:22 -0400 Subject: [PATCH 3/4] Added fix for ESP32 --- src/detect/ScanI2C.cpp | 28 ++++++++++++++++++++++++++++ src/detect/ScanI2C.h | 1 + src/main.cpp | 3 +++ src/main.h | 3 ++- 4 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/detect/ScanI2C.cpp b/src/detect/ScanI2C.cpp index 149bb95f0..941fdf3e8 100644 --- a/src/detect/ScanI2C.cpp +++ b/src/detect/ScanI2C.cpp @@ -1,4 +1,6 @@ #include "ScanI2C.h" +#include "main.h" +#include const ScanI2C::DeviceAddress ScanI2C::ADDRESS_NONE = ScanI2C::DeviceAddress(); const ScanI2C::FoundDevice ScanI2C::DEVICE_NONE = ScanI2C::FoundDevice(ScanI2C::DeviceType::NONE, ADDRESS_NONE); @@ -27,7 +29,33 @@ ScanI2C::FoundDevice ScanI2C::firstRTC() const ScanI2C::DeviceType types[] = {RTC_RV3028, RTC_PCF8563}; return firstOfOrNONE(2, types); } +bool performScanForCardKB() { + // Example I2C scan code for CardKB (adjust as needed) + Wire.beginTransmission(CARDKB_I2C_ADDRESS); + if (Wire.endTransmission() == 0) { + return true; // CardKB detected + } + return false; // CardKB not detected +} +void scanForCardKB() { + const int maxRetries = 10; // Maximum number of retries + const int retryDelay = 100; // Delay between retries in milliseconds + for (int i = 0; i < maxRetries; ++i) { + // Perform the scan (example scan code, adjust as needed) + cardKBDetected = performScanForCardKB(); + + if (cardKBDetected) { + Serial.println("CardKB Keyboard detected."); + break; + } + + delay(retryDelay); // Wait before the next retry + } + if (!cardKBDetected) { + Serial.println("CardKB Keyboard not detected. Canned Message Module Disabled."); + } +} ScanI2C::FoundDevice ScanI2C::firstKeyboard() const { ScanI2C::DeviceType types[] = {CARDKB, TDECKKB, BBQ10KB, RAK14004}; diff --git a/src/detect/ScanI2C.h b/src/detect/ScanI2C.h index 6c01b9100..15668aecb 100644 --- a/src/detect/ScanI2C.h +++ b/src/detect/ScanI2C.h @@ -2,6 +2,7 @@ #include #include +bool performScanForCardKB(); class ScanI2C { diff --git a/src/main.cpp b/src/main.cpp index 4a9fef5d0..a3e9258ba 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -35,6 +35,8 @@ #include // #include +bool cardKBDetected = false; +void scanForCardKB(); #ifdef ARCH_ESP32 #if !MESHTASTIC_EXCLUDE_WEBSERVER #include "mesh/http/WebServer.h" @@ -374,6 +376,7 @@ void setup() // otherwise keyboard and touch screen will not work delay(800); #endif +scanForCardKB(); // Initial scan for CardKB // Currently only the tbeam has a PMU // PMU initialization needs to be placed before i2c scanning diff --git a/src/main.h b/src/main.h index db05a4734..737d424a7 100644 --- a/src/main.h +++ b/src/main.h @@ -21,7 +21,7 @@ extern NimbleBluetooth *nimbleBluetooth; #include "NRF52Bluetooth.h" extern NRF52Bluetooth *nrf52Bluetooth; #endif - +extern bool cardKBDetected; #if ARCH_PORTDUINO extern HardwareSPI *DisplaySPI; extern HardwareSPI *LoraSPI; @@ -39,6 +39,7 @@ extern bool pmu_found; extern bool isCharging; extern bool isUSBPowered; +#define CARDKB_I2C_ADDRESS 0x5F // Replace 0x5F with the actual address if different #if !defined(ARCH_PORTDUINO) && !defined(ARCH_STM32WL) extern ATECCX08A atecc; #endif From a37f309c0379dfa161f2dae41f319acdc0b3d0b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20G=C3=B6ttgens?= Date: Sun, 19 May 2024 19:32:08 +0200 Subject: [PATCH 4/4] change the main scan class so they scan only for wanted bits - UNTESTED --- src/detect/ScanI2C.cpp | 29 +-------------------------- src/detect/ScanI2C.h | 2 +- src/detect/ScanI2CTwoWire.cpp | 13 ++++++++++-- src/detect/ScanI2CTwoWire.h | 4 +++- src/input/kbI2cBase.cpp | 37 +++++++++++++++++++++++++++++++++-- src/main.cpp | 3 --- src/main.h | 3 +-- 7 files changed, 52 insertions(+), 39 deletions(-) diff --git a/src/detect/ScanI2C.cpp b/src/detect/ScanI2C.cpp index 941fdf3e8..7d0a83653 100644 --- a/src/detect/ScanI2C.cpp +++ b/src/detect/ScanI2C.cpp @@ -1,6 +1,4 @@ #include "ScanI2C.h" -#include "main.h" -#include const ScanI2C::DeviceAddress ScanI2C::ADDRESS_NONE = ScanI2C::DeviceAddress(); const ScanI2C::FoundDevice ScanI2C::DEVICE_NONE = ScanI2C::FoundDevice(ScanI2C::DeviceType::NONE, ADDRESS_NONE); @@ -8,6 +6,7 @@ const ScanI2C::FoundDevice ScanI2C::DEVICE_NONE = ScanI2C::FoundDevice(ScanI2C:: ScanI2C::ScanI2C() = default; void ScanI2C::scanPort(ScanI2C::I2CPort port) {} +void ScanI2C::scanPort(ScanI2C::I2CPort port, int *address) {} void ScanI2C::setSuppressScreen() { @@ -29,33 +28,7 @@ ScanI2C::FoundDevice ScanI2C::firstRTC() const ScanI2C::DeviceType types[] = {RTC_RV3028, RTC_PCF8563}; return firstOfOrNONE(2, types); } -bool performScanForCardKB() { - // Example I2C scan code for CardKB (adjust as needed) - Wire.beginTransmission(CARDKB_I2C_ADDRESS); - if (Wire.endTransmission() == 0) { - return true; // CardKB detected - } - return false; // CardKB not detected -} -void scanForCardKB() { - const int maxRetries = 10; // Maximum number of retries - const int retryDelay = 100; // Delay between retries in milliseconds - for (int i = 0; i < maxRetries; ++i) { - // Perform the scan (example scan code, adjust as needed) - cardKBDetected = performScanForCardKB(); - - if (cardKBDetected) { - Serial.println("CardKB Keyboard detected."); - break; - } - - delay(retryDelay); // Wait before the next retry - } - if (!cardKBDetected) { - Serial.println("CardKB Keyboard not detected. Canned Message Module Disabled."); - } -} ScanI2C::FoundDevice ScanI2C::firstKeyboard() const { ScanI2C::DeviceType types[] = {CARDKB, TDECKKB, BBQ10KB, RAK14004}; diff --git a/src/detect/ScanI2C.h b/src/detect/ScanI2C.h index 15668aecb..4aa4549cc 100644 --- a/src/detect/ScanI2C.h +++ b/src/detect/ScanI2C.h @@ -2,7 +2,6 @@ #include #include -bool performScanForCardKB(); class ScanI2C { @@ -82,6 +81,7 @@ class ScanI2C ScanI2C(); virtual void scanPort(ScanI2C::I2CPort); + virtual void scanPort(ScanI2C::I2CPort, int *); /* * A bit of a hack, this tells the scanner not to tell later systems there is a screen to avoid enabling it. diff --git a/src/detect/ScanI2CTwoWire.cpp b/src/detect/ScanI2CTwoWire.cpp index 7828dfb58..f7068ee02 100644 --- a/src/detect/ScanI2CTwoWire.cpp +++ b/src/detect/ScanI2CTwoWire.cpp @@ -135,7 +135,7 @@ uint16_t ScanI2CTwoWire::getRegisterValue(const ScanI2CTwoWire::RegisterLocation type = T; \ break; -void ScanI2CTwoWire::scanPort(I2CPort port) +void ScanI2CTwoWire::scanPort(I2CPort port, int *address) { concurrency::LockGuard guard((concurrency::Lock *)&lock); @@ -163,6 +163,10 @@ void ScanI2CTwoWire::scanPort(I2CPort port) #endif for (addr.address = 1; addr.address < 127; addr.address++) { + // Skip the address if it is not requested oon a partial scan + if (sizeof(address) > 0 && addr.address != *address) { + continue; + } i2cBus->beginTransmission(addr.address); #ifdef ARCH_PORTDUINO if (i2cBus->read() != -1) @@ -353,6 +357,11 @@ void ScanI2CTwoWire::scanPort(I2CPort port) } } +void ScanI2CTwoWire::scanPort(I2CPort port) +{ + scanPort(port, nullptr); +} + TwoWire *ScanI2CTwoWire::fetchI2CBus(ScanI2C::DeviceAddress address) const { if (address.port == ScanI2C::I2CPort::WIRE) { @@ -369,4 +378,4 @@ TwoWire *ScanI2CTwoWire::fetchI2CBus(ScanI2C::DeviceAddress address) const size_t ScanI2CTwoWire::countDevices() const { return foundDevices.size(); -} +} \ No newline at end of file diff --git a/src/detect/ScanI2CTwoWire.h b/src/detect/ScanI2CTwoWire.h index 9acd736d2..a7c19c779 100644 --- a/src/detect/ScanI2CTwoWire.h +++ b/src/detect/ScanI2CTwoWire.h @@ -16,6 +16,8 @@ class ScanI2CTwoWire : public ScanI2C public: void scanPort(ScanI2C::I2CPort) override; + void scanPort(ScanI2C::I2CPort, int *) override; + ScanI2C::FoundDevice find(ScanI2C::DeviceType) const override; TwoWire *fetchI2CBus(ScanI2C::DeviceAddress) const; @@ -53,4 +55,4 @@ class ScanI2CTwoWire : public ScanI2C uint16_t getRegisterValue(const RegisterLocation &, ResponseWidth) const; DeviceType probeOLED(ScanI2C::DeviceAddress) const; -}; +}; \ No newline at end of file diff --git a/src/input/kbI2cBase.cpp b/src/input/kbI2cBase.cpp index af7c96b20..55f435fda 100644 --- a/src/input/kbI2cBase.cpp +++ b/src/input/kbI2cBase.cpp @@ -1,6 +1,7 @@ #include "kbI2cBase.h" #include "configuration.h" #include "detect/ScanI2C.h" +#include "detect/ScanI2CTwoWire.h" extern ScanI2C::DeviceAddress cardkb_found; extern uint8_t kb_model; @@ -30,8 +31,40 @@ uint8_t read_from_14004(TwoWire *i2cBus, uint8_t reg, uint8_t *data, uint8_t len int32_t KbI2cBase::runOnce() { if (cardkb_found.address == 0x00) { - // Input device is not detected. - return INT32_MAX; + // Input device is not detected. Rescan now. + auto i2cScanner = std::unique_ptr(new ScanI2CTwoWire()); + int i2caddr_scan[] = {CARDKB_ADDR, TDECK_KB_ADDR, BBQ10_KB_ADDR}; +#if defined(I2C_SDA1) + i2cScanner->scanPort(ScanI2C::I2CPort::WIRE1, i2caddr_scan); +#endif + i2cScanner->scanPort(ScanI2C::I2CPort::WIRE, i2caddr_scan); + auto kb_info = i2cScanner->firstKeyboard(); + + if (kb_info.type != ScanI2C::DeviceType::NONE) { + cardkb_found = kb_info.address; + switch (kb_info.type) { + case ScanI2C::DeviceType::RAK14004: + kb_model = 0x02; + break; + case ScanI2C::DeviceType::CARDKB: + kb_model = 0x00; + break; + case ScanI2C::DeviceType::TDECKKB: + // assign an arbitrary value to distinguish from other models + kb_model = 0x10; + break; + case ScanI2C::DeviceType::BBQ10KB: + // assign an arbitrary value to distinguish from other models + kb_model = 0x11; + break; + default: + // use this as default since it's also just zero + LOG_WARN("kb_info.type is unknown(0x%02x), setting kb_model=0x00\n", kb_info.type); + kb_model = 0x00; + } + } + if (cardkb_found.address == 0x00) + return INT32_MAX; } if (!i2cBus) { diff --git a/src/main.cpp b/src/main.cpp index a3e9258ba..4a9fef5d0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -35,8 +35,6 @@ #include // #include -bool cardKBDetected = false; -void scanForCardKB(); #ifdef ARCH_ESP32 #if !MESHTASTIC_EXCLUDE_WEBSERVER #include "mesh/http/WebServer.h" @@ -376,7 +374,6 @@ void setup() // otherwise keyboard and touch screen will not work delay(800); #endif -scanForCardKB(); // Initial scan for CardKB // Currently only the tbeam has a PMU // PMU initialization needs to be placed before i2c scanning diff --git a/src/main.h b/src/main.h index 737d424a7..db05a4734 100644 --- a/src/main.h +++ b/src/main.h @@ -21,7 +21,7 @@ extern NimbleBluetooth *nimbleBluetooth; #include "NRF52Bluetooth.h" extern NRF52Bluetooth *nrf52Bluetooth; #endif -extern bool cardKBDetected; + #if ARCH_PORTDUINO extern HardwareSPI *DisplaySPI; extern HardwareSPI *LoraSPI; @@ -39,7 +39,6 @@ extern bool pmu_found; extern bool isCharging; extern bool isUSBPowered; -#define CARDKB_I2C_ADDRESS 0x5F // Replace 0x5F with the actual address if different #if !defined(ARCH_PORTDUINO) && !defined(ARCH_STM32WL) extern ATECCX08A atecc; #endif