Skip to content

Commit

Permalink
⚡ (ble): Allow to refresh data in Advertising
Browse files Browse the repository at this point in the history
  • Loading branch information
YannLocatelli committed Apr 20, 2022
1 parent e3b6954 commit c33e300
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 0 deletions.
5 changes: 5 additions & 0 deletions libs/BLEKit/include/BLEKit.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,17 @@ class BLEKit

void init();

void setAdvertisingData(const AdvertisingData &advertising_data);
[[nodiscard]] auto getAdvertisingData() const -> AdvertisingData;

private:
// ? mbed::BLE specific function
void processEvents(BLE::OnEventsToProcessCallbackContext *context);

CoreEventQueue _event_queue {};

AdvertisingData _advertising_data {};

BLE &_ble = BLE::Instance();
CoreGap _core_gap {_ble.gap()};
CoreGattServer _core_gatt_server {_ble.gattServer()};
Expand Down
1 change: 1 addition & 0 deletions libs/BLEKit/include/CoreGap.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class CoreGap
// void onInit(std::function<void()> cb) { _post_init = cb; }

void startAdvertising();
void setAdvertising(AdvertisingData advertising_data);

private:
ble::advertising_handle_t _advertising_handle {ble::LEGACY_ADVERTISING_HANDLE};
Expand Down
12 changes: 12 additions & 0 deletions libs/BLEKit/source/BLEKit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,15 @@ void BLEKit::processEvents(BLE::OnEventsToProcessCallbackContext *context)
{
_event_queue.callMbedCallback(mbed::callback(&context->ble, &BLE::processEvents));
}

void BLEKit::setAdvertisingData(const AdvertisingData &advertising_data)
{
_advertising_data = advertising_data;

_core_gap.setAdvertising(_advertising_data);
}

auto BLEKit::getAdvertisingData() const -> AdvertisingData
{
return _advertising_data;
}
10 changes: 10 additions & 0 deletions libs/BLEKit/source/CoreGap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,13 @@ void CoreGap::startAdvertising()

_gap.startAdvertising(_advertising_handle, adv_duration_t(millisecond_t(4000)));
}

void CoreGap::setAdvertising(AdvertisingData advertising_data)
{
_advertising_data_builder.setName(advertising_data.name);
_advertising_data_builder.setServiceData(
leka::service::commands::uuid, // TODO: commands::uuid only for compatibility with LekaApp
{advertising_data.data(), advertising_data.size()});

_gap.setAdvertisingPayload(_advertising_handle, _advertising_data_builder.getAdvertisingData());
}
10 changes: 10 additions & 0 deletions libs/BLEKit/tests/BLEKit_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,13 @@ TEST_F(BLEKitTest, callOnEventsToProcess)

EXPECT_TRUE(spy_CoreEventQueue_did_call_function);
}

TEST_F(BLEKitTest, getAdvertisingDataThenSetAdvertisingData)
{
auto advertising_data = ble.getAdvertisingData();
advertising_data.name = "NewLeka";

EXPECT_CALL(mbed_mock_gap, setAdvertisingPayload).Times(1);

ble.setAdvertisingData(advertising_data);
}
16 changes: 16 additions & 0 deletions libs/BLEKit/tests/CoreGap_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,19 @@ TEST_F(CoreGapTest, onInitializationComplete)

// coregap.onInitializationComplete(&context); // Alternative
}

TEST_F(CoreGapTest, setAdvertising)
{
std::array<uint8_t, 64> buffer {};
auto data_builder = AdvertisingDataBuilder {{buffer.begin(), buffer.end()}};
auto new_advertising_data = AdvertisingData {.name = "NewLeka", .battery = 0x42, .is_charging = 0x01};

data_builder.setName(new_advertising_data.name);
data_builder.setServiceData(service::commands::uuid, {new_advertising_data.data(), new_advertising_data.size()});

EXPECT_CALL(mbed_mock_gap,
setAdvertisingPayload(LEGACY_ADVERTISING_HANDLE, compareAdvertisingPayload(data_builder)))
.Times(1);

coregap.setAdvertising(new_advertising_data);
}
6 changes: 6 additions & 0 deletions spikes/lk_ble/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,5 +106,11 @@ auto main() -> int
auto version = service_update.getVersion();

log_info("Requested version: %d.%d.%d", version.major, version.minor, version.revision);

auto advertising_data = blekit.getAdvertisingData();
advertising_data.name = "NewLeka";
advertising_data.battery = level;
advertising_data.is_charging = charging_status;
blekit.setAdvertisingData(advertising_data);
}
}

0 comments on commit c33e300

Please sign in to comment.