From 52b2769ab76b79688fa74cec2f5e1bbaa659c29b Mon Sep 17 00:00:00 2001 From: Yann Locatelli Date: Mon, 22 Jan 2024 11:34:12 +0100 Subject: [PATCH] :sparkles: (BLEKit): Add temperature characteristic --- libs/BLEKit/include/BLEServiceMonitoring.h | 19 ++++++++++++++++--- .../internal/ServicesCharacteristics.h | 9 +++++---- .../tests/BLEServiceMonitoring_test.cpp | 18 ++++++++++++++++++ 3 files changed, 39 insertions(+), 7 deletions(-) diff --git a/libs/BLEKit/include/BLEServiceMonitoring.h b/libs/BLEKit/include/BLEServiceMonitoring.h index 7415f3aa46..ccdf96254f 100644 --- a/libs/BLEKit/include/BLEServiceMonitoring.h +++ b/libs/BLEKit/include/BLEServiceMonitoring.h @@ -29,6 +29,14 @@ class BLEServiceMonitoring : public interface::BLEService sendData(data); } + void setTemperature(float value) + { + std::memcpy(temperature.data(), &value, 4); + + auto data = std::make_tuple(_temperature_characteristic.getValueHandle(), temperature); + sendData(data); + } + auto isScreensaverEnable() const -> bool { return screensaver_enable; } void onDataReceived(const data_received_handle_t ¶ms) final @@ -63,6 +71,11 @@ class BLEServiceMonitoring : public interface::BLEService service::monitoring::characteristic::charging_status, &charging_status, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY}; + std::array temperature {}; + ReadOnlyArrayGattCharacteristic _temperature_characteristic { + service::monitoring::characteristic::temperature, temperature.begin(), + GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY}; + bool screensaver_enable {true}; WriteOnlyGattCharacteristic screensaver_enable_characteristic { service::monitoring::characteristic::screensaver_enable, &screensaver_enable}; @@ -76,9 +89,9 @@ class BLEServiceMonitoring : public interface::BLEService WriteOnlyGattCharacteristic hard_reboot_characteristic {service::monitoring::characteristic::hard_reboot, &hard_reboot}; - std::array _characteristic_table { - &_charging_status_characteristic, &screensaver_enable_characteristic, &soft_reboot_characteristic, - &hard_reboot_characteristic}; + std::array _characteristic_table { + &_charging_status_characteristic, &_temperature_characteristic, &screensaver_enable_characteristic, + &soft_reboot_characteristic, &hard_reboot_characteristic}; }; } // namespace leka diff --git a/libs/BLEKit/include/internal/ServicesCharacteristics.h b/libs/BLEKit/include/internal/ServicesCharacteristics.h index dedab2fc6b..1fe741aa04 100644 --- a/libs/BLEKit/include/internal/ServicesCharacteristics.h +++ b/libs/BLEKit/include/internal/ServicesCharacteristics.h @@ -37,10 +37,11 @@ namespace monitoring { inline constexpr uint16_t uuid = 0x7779; namespace characteristic { - inline constexpr uint16_t charging_status = 0x6783; - inline constexpr uint16_t screensaver_enable = 0x8369; - inline constexpr uint16_t soft_reboot = 0x8382; - inline constexpr uint16_t hard_reboot = 0x7282; + inline constexpr uint16_t charging_status = 0x6783; + inline constexpr UUID::LongUUIDBytes_t temperature = {"Temperature"}; + inline constexpr uint16_t screensaver_enable = 0x8369; + inline constexpr uint16_t soft_reboot = 0x8382; + inline constexpr uint16_t hard_reboot = 0x7282; } // namespace characteristic } // namespace monitoring diff --git a/libs/BLEKit/tests/BLEServiceMonitoring_test.cpp b/libs/BLEKit/tests/BLEServiceMonitoring_test.cpp index bdc03452e2..9284a4c53d 100644 --- a/libs/BLEKit/tests/BLEServiceMonitoring_test.cpp +++ b/libs/BLEKit/tests/BLEServiceMonitoring_test.cpp @@ -54,6 +54,24 @@ TEST_F(BLEServiceMonitoringTest, setChargingStatus) EXPECT_FALSE(actual_charging_status); } +TEST_F(BLEServiceMonitoringTest, setTemperature) +{ + std::array actual_temperature {}; + std::array expected_temperature {}; + + auto spy_callback = [&actual_temperature](const BLEServiceMonitoring::data_to_send_handle_t &handle) { + for (auto i = 0; i < std::size(actual_temperature); i++) { + actual_temperature.at(i) = std::get<1>(handle)[i]; + } + }; + + service_monitoring.onDataReadyToSend(spy_callback); + + service_monitoring.setTemperature(3.14159); + expected_temperature = {0xD0, 0x0F, 0x49, 0x40}; // 3.14159, little-endian as in Swift + EXPECT_EQ(actual_temperature, expected_temperature); +} + TEST_F(BLEServiceMonitoringTest, isScreensaverEnableDefault) { auto actual_is_screensaver_enable = service_monitoring.isScreensaverEnable();