From 574fbbc93e9498f73c99997ba827c134f36ea70a Mon Sep 17 00:00:00 2001 From: Quentin Date: Fri, 25 Oct 2024 14:27:58 +0200 Subject: [PATCH] Added a fix for MyCo2 detection --- CHANGELOG.md | 1 + src/Sensirion_upt_ble_auto_detection.cpp | 22 ++++++++++++++++++---- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ee85ad6..625e1e8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Use unit64_t for address (device id) instead of std::string - Fix several clang tidy issues - Update example to call keepAlive regularly. This ensures scanning does not stop due to errors. +- Fixed detection of 'Sensirion MyCO2' gadgets. ## [0.1.0] ### Added diff --git a/src/Sensirion_upt_ble_auto_detection.cpp b/src/Sensirion_upt_ble_auto_detection.cpp index 6eb4d65..04edcef 100644 --- a/src/Sensirion_upt_ble_auto_detection.cpp +++ b/src/Sensirion_upt_ble_auto_detection.cpp @@ -58,17 +58,31 @@ uint16_t SensiScan::getDeviceId(const std::string& data) { * @brief decode chunk of Advertisement containing encoded samples * * @returns 0 on success - * 1 if the Frame is too short or if the SampleType is unknown + * 1 if the SampleType is unknown + * 2 if the received data length is too short for the sample type */ uint8_t SensiScan::decodeData(const MetaData& metaData, const std::string& data, std::vector& samples) { auto sampleType = static_cast(data[3]); DataType dataType = getDataTypeFromSampleType(sampleType); - SampleConfig sampleConfig = sampleConfigSelector[dataType]; + std::map::iterator sampleConfigIt = sampleConfigSelector.find(dataType); - if (data.length() < 6 + sampleConfig.sampleSizeBytes) { - return 1; // Frame too short or no config found + if (sampleConfigIt == sampleConfigSelector.end()) { + return 1; // No config found for data type + } + + SampleConfig sampleConfig = sampleConfigIt->second; + + if (data.length() < 6 + sampleConfig.sampleSizeBytes ) { + /* + NOTE: Here we ignore frames that are too short for T_RH_CO2_ALT data types + because the MyCO2 gadget is actually not sending the 2 reserved Bytes (ALT) + */ + bool is_a_myco2_gadget = metaData.deviceType.bleGadgetType == BLEGadgetType::MYCO2 && data.length() == 6 + sampleConfig.sampleSizeBytes - 2; + if (! is_a_myco2_gadget){ + return 2; // Frame too short + } } unsigned long timestamp = millis();