Skip to content

Commit

Permalink
✨ (BLEKit): Add onTemperatureRequested
Browse files Browse the repository at this point in the history
  • Loading branch information
YannLocatelli committed Jan 23, 2024
1 parent 7ab044e commit cad926b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
11 changes: 10 additions & 1 deletion libs/BLEKit/include/BLEServiceMonitoring.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ class BLEServiceMonitoring : public interface::BLEService

auto isScreensaverEnable() const -> bool { return screensaver_enable; }

void onTemperatureRequested(const std::function<void()> &callback)
{
_on_temperature_requested_callback = callback;
}

void onDataReceived(const data_received_handle_t &params) final
{
if (params.handle == screensaver_enable_characteristic.getValueHandle()) {
Expand All @@ -62,7 +67,10 @@ class BLEServiceMonitoring : public interface::BLEService

void onDataRequested(const data_requested_handle_t &params) final
{
// do nothing
if (params.handle == _temperature_characteristic.getValueHandle() &&
_on_temperature_requested_callback != nullptr) {
_on_temperature_requested_callback();
}
}

private:
Expand All @@ -75,6 +83,7 @@ class BLEServiceMonitoring : public interface::BLEService
ReadOnlyArrayGattCharacteristic<uint8_t, 4> _temperature_characteristic {
service::monitoring::characteristic::temperature, temperature.begin(),
GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY};
std::function<void()> _on_temperature_requested_callback {};

bool screensaver_enable {true};
WriteOnlyGattCharacteristic<bool> screensaver_enable_characteristic {
Expand Down
29 changes: 29 additions & 0 deletions libs/BLEKit/tests/BLEServiceMonitoring_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,35 @@ TEST_F(BLEServiceMonitoringTest, setTemperature)
EXPECT_EQ(actual_temperature, expected_temperature);
}

TEST_F(BLEServiceMonitoringTest, onTemperatureRequested)
{
testing::MockFunction<void()> 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<void()> 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();
Expand Down

0 comments on commit cad926b

Please sign in to comment.