Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Yann/feature/ble/service monitoring/temperature humidity #1357

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 41 additions & 3 deletions libs/BLEKit/include/BLEServiceMonitoring.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,29 @@ 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);
}

void setHumidity(float value)
{
std::memcpy(humidity.data(), &value, 4);

auto data = std::make_tuple(_humidity_characteristic.getValueHandle(), humidity);
sendData(data);
}

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

void onTemperatureHumidityRequested(const std::function<void()> &callback)
{
_on_temperature_humidity_requested_callback = callback;
}

void onDataReceived(const data_received_handle_t &params) final
{
if (params.handle == _screensaver_enable_characteristic.getValueHandle()) {
Expand All @@ -68,7 +89,11 @@ class BLEServiceMonitoring : public interface::BLEService

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

private:
Expand All @@ -79,6 +104,18 @@ class BLEServiceMonitoring : public interface::BLEService
GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY,
};

std::array<uint8_t, 4> temperature {};
ReadOnlyArrayGattCharacteristic<uint8_t, 4> _temperature_characteristic {
service::monitoring::characteristic::temperature, temperature.begin(),
GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY};

std::array<uint8_t, 4> humidity {};
ReadOnlyArrayGattCharacteristic<uint8_t, 4> _humidity_characteristic {
service::monitoring::characteristic::humidity, humidity.begin(),
GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY};

std::function<void()> _on_temperature_humidity_requested_callback {};

bool _screensaver_enable {true};
WriteOnlyGattCharacteristic<bool> _screensaver_enable_characteristic {
service::monitoring::characteristic::screensaver_enable,
Expand Down Expand Up @@ -106,9 +143,10 @@ class BLEServiceMonitoring : public interface::BLEService
GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY,
};

std::array<GattCharacteristic *, 5> _characteristic_table {
std::array<GattCharacteristic *, 7> _characteristic_table {
&_charging_status_characteristic, &_screensaver_enable_characteristic, &_soft_reboot_characteristic,
&_hard_reboot_characteristic, &_negotiated_mtu_characteristic,
&_hard_reboot_characteristic, &_negotiated_mtu_characteristic, &_temperature_characteristic,
&_humidity_characteristic,
};
};

Expand Down
2 changes: 2 additions & 0 deletions libs/BLEKit/include/internal/ServicesCharacteristics.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ namespace monitoring {
inline constexpr uint16_t soft_reboot = 0x8382;
inline constexpr uint16_t hard_reboot = 0x7282;
inline constexpr UUID::LongUUIDBytes_t negotiated_mtu = {"NEGOTIATED_MTU"};
inline constexpr UUID::LongUUIDBytes_t temperature = {"TEMPERATURE"};
inline constexpr UUID::LongUUIDBytes_t humidity = {"HUMIDITY"};
} // namespace characteristic

} // namespace monitoring
Expand Down
65 changes: 65 additions & 0 deletions libs/BLEKit/tests/BLEServiceMonitoring_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,71 @@ TEST_F(BLEServiceMonitoringTest, setChargingStatus)
EXPECT_FALSE(actual_charging_status);
}

TEST_F(BLEServiceMonitoringTest, setTemperature)
{
std::array<uint8_t, 4> actual_temperature {};
std::array<uint8_t, 4> 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(31.4159);
expected_temperature = {0xC3, 0x53, 0xFB, 0x41}; // 31.4159, little-endian as in Swift
EXPECT_EQ(actual_temperature, expected_temperature);
}

TEST_F(BLEServiceMonitoringTest, setHumidity)
{
std::array<uint8_t, 4> actual_humidity {};
std::array<uint8_t, 4> expected_humidity {};

auto spy_callback = [&actual_humidity](const BLEServiceMonitoring::data_to_send_handle_t &handle) {
for (auto i = 0; i < std::size(actual_humidity); i++) {
actual_humidity.at(i) = std::get<1>(handle)[i];
}
};

service_monitoring.onDataReadyToSend(spy_callback);

service_monitoring.setHumidity(51.24);
expected_humidity = {0xC3, 0xF5, 0x4C, 0x42}; // 51.24 little-endian as in Swift
EXPECT_EQ(actual_humidity, expected_humidity);
}

TEST_F(BLEServiceMonitoringTest, onTemperatureHumidityRequested)
{
testing::MockFunction<void()> mock_callback {};
service_monitoring.onTemperatureHumidityRequested(mock_callback.AsStdFunction());

EXPECT_CALL(mock_callback, Call).Times(1);

service_monitoring.onDataRequested(data_requested_handle);
}

TEST_F(BLEServiceMonitoringTest, onTemperatureHumidityRequestedNotSameHandle)
{
testing::MockFunction<void()> mock_callback {};
service_monitoring.onTemperatureHumidityRequested(mock_callback.AsStdFunction());

data_requested_handle.handle = 0xFFFF;

EXPECT_CALL(mock_callback, Call).Times(0);

service_monitoring.onDataRequested(data_requested_handle);
}

TEST_F(BLEServiceMonitoringTest, onTemperatureHumidityRequestedtUnset)
{
service_monitoring.onTemperatureHumidityRequested(nullptr);

service_monitoring.onDataRequested(data_requested_handle);
}

TEST_F(BLEServiceMonitoringTest, isScreensaverEnableDefault)
{
auto actual_is_screensaver_enable = service_monitoring.isScreensaverEnable();
Expand Down