Skip to content

Commit

Permalink
🏗️ (BLEKit): Use GattCharacteristic instead of GattAttribute::Handle_t
Browse files Browse the repository at this point in the history
  • Loading branch information
YannLocatelli committed Feb 5, 2024
1 parent 3933f2e commit 0cc97da
Show file tree
Hide file tree
Showing 10 changed files with 16 additions and 18 deletions.
2 changes: 1 addition & 1 deletion libs/BLEKit/include/BLEServiceBattery.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class BLEServiceBattery : public interface::BLEService
} else {
_battery_level_characteristic_value = 100;
}
auto data = std::make_tuple(_battery_level_writable_characteristic.getValueHandle(),
auto data = std::make_tuple(&_battery_level_writable_characteristic,
std::span(&_battery_level_characteristic_value, 1));
sendData(data);
}
Expand Down
2 changes: 1 addition & 1 deletion libs/BLEKit/include/BLEServiceConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class BLEServiceConfig : public interface::BLEService
{
std::copy(std::begin(robot_name), std::begin(robot_name) + std::size(_robot_name), _robot_name.begin());

auto data = std::make_tuple(_robot_name_characteristic.getValueHandle(), _robot_name);
auto data = std::make_tuple(&_robot_name_characteristic, _robot_name);
sendData(data);
}

Expand Down
4 changes: 2 additions & 2 deletions libs/BLEKit/include/BLEServiceDeviceInformation.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class BLEServiceDeviceInformation : public interface::BLEService
{
std::copy(std::begin(value), std::begin(value) + std::size(_serial_number), _serial_number.begin());

auto data = std::make_tuple(_serial_number_characteristic.getValueHandle(), _serial_number);
auto data = std::make_tuple(&_serial_number_characteristic, std::span<const uint8_t>(_serial_number));
sendData(data);
}

Expand All @@ -36,7 +36,7 @@ class BLEServiceDeviceInformation : public interface::BLEService

std::copy(std::begin(version_cstr), std::begin(version_cstr) + std::size(_os_version), _os_version.begin());

auto data = std::make_tuple(_os_version_characteristic.getValueHandle(), _os_version);
auto data = std::make_tuple(&_os_version_characteristic, std::span<const uint8_t>(_os_version));
sendData(data);
}

Expand Down
7 changes: 3 additions & 4 deletions libs/BLEKit/include/BLEServiceFileExchange.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ class BLEServiceFileExchange : public interface::BLEService
{
set_file_exchange_state = static_cast<uint8_t>(value);

auto data = std::make_tuple(set_file_exchange_state_characteristic.getValueHandle(),
std::span(&set_file_exchange_state, 1));
auto data = std::make_tuple(&set_file_exchange_state_characteristic, std::span(&set_file_exchange_state, 1));
sendData(data);
}

Expand All @@ -35,15 +34,15 @@ class BLEServiceFileExchange : public interface::BLEService
{
clear_file = 0;

auto data = std::make_tuple(clear_file_characteristic.getValueHandle(), std::span(&clear_file, 1));
auto data = std::make_tuple(&clear_file_characteristic, std::span(&clear_file, 1));
sendData(data);
}

void setFileSHA256(std::array<uint8_t, 32> sha256)
{
std::copy(std::begin(sha256), std::begin(sha256) + std::size(sha256), file_sha256.begin());

auto data = std::make_tuple(file_sha256_characteristic.getValueHandle(), file_sha256);
auto data = std::make_tuple(&file_sha256_characteristic, file_sha256);
sendData(data);
}

Expand Down
4 changes: 2 additions & 2 deletions libs/BLEKit/include/BLEServiceMagicCard.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ class BLEServiceMagicCard : public interface::BLEService
_id[0] = utils::memory::getHighByte(id);
_id[1] = utils::memory::getLowByte(id);

auto data = std::make_tuple(_id_characteristic.getValueHandle(), _id);
auto data = std::make_tuple(&_id_characteristic, _id);
sendData(data);
}

void sendLanguage(MagicCard::Language language)
{
_language = static_cast<uint8_t>(language);

auto data = std::make_tuple(_language_characteristic.getValueHandle(), std::span(&_language, 1));
auto data = std::make_tuple(&_language_characteristic, std::span(&_language, 1));
sendData(data);
}

Expand Down
2 changes: 1 addition & 1 deletion libs/BLEKit/include/BLEServiceMonitoring.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class BLEServiceMonitoring : public interface::BLEService
{
charging_status = static_cast<uint8_t>(value);

auto data = std::make_tuple(_charging_status_characteristic.getValueHandle(), std::span(&charging_status, 1));
auto data = std::make_tuple(&_charging_status_characteristic, std::span(&charging_status, 1));
sendData(data);
}

Expand Down
2 changes: 1 addition & 1 deletion libs/BLEKit/include/CoreGattServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class CoreGattServer
void setServices(std::span<interface::BLEService *> services);

private:
void write(GattAttribute::Handle_t characteristic_updated, std::span<const uint8_t> data);
void write(const GattCharacteristic *characteristic_updated, std::span<const uint8_t> data);

ble::GattServer &_gatt_server;
leka::CoreGattServerEventHandler _gatt_server_event_handler;
Expand Down
2 changes: 1 addition & 1 deletion libs/BLEKit/include/internal/BLEService.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class BLEService : public GattService

using data_received_handle_t = GattWriteCallbackParams;
using data_requested_handle_t = GattReadCallbackParams;
using data_to_send_handle_t = std::tuple<GattAttribute::Handle_t, std::span<const uint8_t>>;
using data_to_send_handle_t = std::tuple<const GattCharacteristic *, std::span<const uint8_t>>;

virtual void onDataReceived(const data_received_handle_t &handle) = 0;
virtual void onDataRequested(const data_requested_handle_t &handle) = 0;
Expand Down
4 changes: 2 additions & 2 deletions libs/BLEKit/source/CoreGattServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ void CoreGattServer::setServices(std::span<interface::BLEService *> services)
_gatt_server_event_handler.setServices(services);
}

void CoreGattServer::write(GattAttribute::Handle_t characteristic_updated, std::span<const uint8_t> data)
void CoreGattServer::write(const GattCharacteristic *characteristic_updated, std::span<const uint8_t> data)
{
_gatt_server.write(characteristic_updated, data.data(), static_cast<uint16_t>(std::size(data)));
_gatt_server.write(characteristic_updated->getValueHandle(), data.data(), static_cast<uint16_t>(std::size(data)));
}
5 changes: 2 additions & 3 deletions libs/BLEKit/tests/CoreGattServer_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,12 @@ TEST_F(CoreGattServerTest, onDataReadyToSend)

gatt_server.setServices(services);

auto handle = GattAttribute::Handle_t {};
auto data_to_send = std::to_array<const uint8_t>({0x2A, 0x2B, 0x2C, 0x2D});

auto tuple = std::make_tuple(handle, data_to_send);
auto tuple = std::make_tuple(&characteristic, data_to_send);
const auto &[h, d] = tuple; // need for the EXPECT_CALL: addresses must be the same

EXPECT_CALL(mbed_mock_gatt, write(h, d.data(), std::size(d), _)).Times(1);
EXPECT_CALL(mbed_mock_gatt, write(h->getValueHandle(), d.data(), std::size(d), _)).Times(1);

mock_service.sendData(tuple);
}

0 comments on commit 0cc97da

Please sign in to comment.