From cad926b12c08a283e0c757681a4436282dd879bf Mon Sep 17 00:00:00 2001 From: Yann Locatelli Date: Mon, 22 Jan 2024 12:54:45 +0100 Subject: [PATCH] :sparkles: (BLEKit): Add onTemperatureRequested --- libs/BLEKit/include/BLEServiceMonitoring.h | 11 ++++++- .../tests/BLEServiceMonitoring_test.cpp | 29 +++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/libs/BLEKit/include/BLEServiceMonitoring.h b/libs/BLEKit/include/BLEServiceMonitoring.h index ccdf96254f..1b29dd8880 100644 --- a/libs/BLEKit/include/BLEServiceMonitoring.h +++ b/libs/BLEKit/include/BLEServiceMonitoring.h @@ -39,6 +39,11 @@ class BLEServiceMonitoring : public interface::BLEService auto isScreensaverEnable() const -> bool { return screensaver_enable; } + void onTemperatureRequested(const std::function &callback) + { + _on_temperature_requested_callback = callback; + } + void onDataReceived(const data_received_handle_t ¶ms) final { if (params.handle == screensaver_enable_characteristic.getValueHandle()) { @@ -62,7 +67,10 @@ class BLEServiceMonitoring : public interface::BLEService void onDataRequested(const data_requested_handle_t ¶ms) final { - // do nothing + if (params.handle == _temperature_characteristic.getValueHandle() && + _on_temperature_requested_callback != nullptr) { + _on_temperature_requested_callback(); + } } private: @@ -75,6 +83,7 @@ class BLEServiceMonitoring : public interface::BLEService ReadOnlyArrayGattCharacteristic _temperature_characteristic { service::monitoring::characteristic::temperature, temperature.begin(), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY}; + std::function _on_temperature_requested_callback {}; bool screensaver_enable {true}; WriteOnlyGattCharacteristic screensaver_enable_characteristic { diff --git a/libs/BLEKit/tests/BLEServiceMonitoring_test.cpp b/libs/BLEKit/tests/BLEServiceMonitoring_test.cpp index b814870066..16ad001662 100644 --- a/libs/BLEKit/tests/BLEServiceMonitoring_test.cpp +++ b/libs/BLEKit/tests/BLEServiceMonitoring_test.cpp @@ -72,6 +72,35 @@ TEST_F(BLEServiceMonitoringTest, setTemperature) EXPECT_EQ(actual_temperature, expected_temperature); } +TEST_F(BLEServiceMonitoringTest, onTemperatureRequested) +{ + testing::MockFunction mock_callback {}; + service_monitoring.onTemperatureRequested(mock_callback.AsStdFunction()); + + EXPECT_CALL(mock_callback, Call).Times(1); + + service_monitoring.onDataRequested(data_requested_handle); +} + +TEST_F(BLEServiceMonitoringTest, onTemperatureRequestedNotSameHandle) +{ + testing::MockFunction mock_callback {}; + service_monitoring.onTemperatureRequested(mock_callback.AsStdFunction()); + + data_requested_handle.handle = 0xFFFF; + + EXPECT_CALL(mock_callback, Call).Times(0); + + service_monitoring.onDataRequested(data_requested_handle); +} + +TEST_F(BLEServiceMonitoringTest, onTemperatureRequestedtUnset) +{ + service_monitoring.onTemperatureRequested(nullptr); + + service_monitoring.onDataRequested(data_requested_handle); +} + TEST_F(BLEServiceMonitoringTest, isScreensaverEnableDefault) { auto actual_is_screensaver_enable = service_monitoring.isScreensaverEnable();