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/refactor/ble/give characteristic instead of attribute handle #1361

Merged
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
1 change: 0 additions & 1 deletion libs/BLEKit/include/BLEKit.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#include <functional>
#include <span>

#include "BLEServiceBattery.h"
#include "ble/BLE.h"

#include "CoreEventQueue.h"
Expand Down
4 changes: 1 addition & 3 deletions 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 All @@ -41,8 +41,6 @@ class BLEServiceBattery : public interface::BLEService
}

private:
data_to_send_handle_t send_data_function {};

uint8_t _battery_level_characteristic_value {};
ReadOnlyGattCharacteristic<uint8_t> _battery_level_writable_characteristic {
service::battery::characteristic::level, &_battery_level_characteristic_value,
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
2 changes: 1 addition & 1 deletion libs/BLEKit/include/BLEServiceMagicCard.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class BLEServiceMagicCard : public interface::BLEService
raw_data[1] = utils::memory::getLowByte(id);
raw_data[2] = static_cast<uint8_t>(language);

auto data = std::make_tuple(raw_data_characteristic.getValueHandle(), raw_data);
auto data = std::make_tuple(&raw_data_characteristic, raw_data);
sendData(data);
}

Expand Down
5 changes: 2 additions & 3 deletions libs/BLEKit/include/BLEServiceMonitoring.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,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 All @@ -37,8 +37,7 @@ class BLEServiceMonitoring : public interface::BLEService
auto _high_byte = utils::memory::getHighByte(_negotiated_mtu);
auto _low_byte = utils::memory::getLowByte(_negotiated_mtu);

auto data =
std::make_tuple(_negotiated_mtu_characteristic.getValueHandle(), std::to_array({_high_byte, _low_byte}));
auto data = std::make_tuple(&_negotiated_mtu_characteristic, std::to_array({_high_byte, _low_byte}));

sendData(data);
}
Expand Down
3 changes: 1 addition & 2 deletions libs/BLEKit/include/CoreGattServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

#include <span>

#include "BLEServiceBattery.h"
#include "ble/BLE.h"
#include "ble/GattServer.h"

Expand All @@ -26,7 +25,7 @@ class CoreGattServer
void onMTUNegotiated(const std::function<void(uint16_t)> &callback);

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,9 +26,9 @@ 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)));
}

void CoreGattServer::onMTUNegotiated(const std::function<void(uint16_t)> &callback)
Expand Down
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);
}