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

fix using mixed APIs in Gattserver example #225

Merged
merged 2 commits into from
Mar 25, 2019
Merged
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
78 changes: 47 additions & 31 deletions BLE_GattServer/source/BLEProcess.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,20 +128,22 @@ class BLEProcess : private mbed::NonCopyable<BLEProcess> {
printf("Ble instance initialized\r\n");

Gap &gap = _ble_interface.gap();
ble_error_t error = gap.setAdvertisingPayload(make_advertising_data());
if (error) {
printf("Error %u during gap.setAdvertisingPayload\r\n", error);
gap.onConnection(this, &BLEProcess::when_connection);
gap.onDisconnection(this, &BLEProcess::when_disconnection);

if (!set_advertising_parameters()) {
return;
}

gap.setAdvertisingParams(make_advertising_params());

gap.onConnection(this, &BLEProcess::when_connection);
gap.onDisconnection(this, &BLEProcess::when_disconnection);
if (!set_advertising_data()) {
return;
}

start_advertising();
if (!start_advertising()) {
return;
}

if (_post_init_cb) {
if (_post_init_cb) {
_post_init_cb(_ble_interface, _event_queue);
}
}
Expand All @@ -157,45 +159,59 @@ class BLEProcess : private mbed::NonCopyable<BLEProcess> {
start_advertising();
}

void start_advertising(void)
bool start_advertising(void)
{
ble_error_t error = _ble_interface.gap().startAdvertising();
Gap &gap = _ble_interface.gap();

/* Start advertising the set */
ble_error_t error = gap.startAdvertising(ble::LEGACY_ADVERTISING_HANDLE);

if (error) {
printf("Error %u during gap.startAdvertising.\r\n", error);
return;
return false;
} else {
printf("Advertising started.\r\n");
return true;
}
}

static GapAdvertisingData make_advertising_data(void)
bool set_advertising_parameters()
{
static const uint8_t device_name[] = "GattServer";
GapAdvertisingData advertising_data;
Gap &gap = _ble_interface.gap();

// add advertising flags
advertising_data.addFlags(
GapAdvertisingData::LE_GENERAL_DISCOVERABLE |
GapAdvertisingData::BREDR_NOT_SUPPORTED
ble_error_t error = gap.setAdvertisingParameters(
ble::LEGACY_ADVERTISING_HANDLE,
ble::AdvertisingParameters()
);

// add device name
advertising_data.addData(
GapAdvertisingData::COMPLETE_LOCAL_NAME,
device_name,
sizeof(device_name)
);
if (error) {
printf("Gap::setAdvertisingParameters() failed with error %d", error);
return false;
}

return advertising_data;
return true;
}

static GapAdvertisingParams make_advertising_params(void)
bool set_advertising_data()
{
return GapAdvertisingParams(
/* type */ GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED,
/* interval */ GapAdvertisingParams::MSEC_TO_ADVERTISEMENT_DURATION_UNITS(500),
/* timeout */ 0
Gap &gap = _ble_interface.gap();

/* Use the simple builder to construct the payload; it fails at runtime
* if there is not enough space left in the buffer */
ble_error_t error = gap.setAdvertisingPayload(
ble::LEGACY_ADVERTISING_HANDLE,
ble::AdvertisingDataSimpleBuilder<ble::LEGACY_ADVERTISING_MAX_SIZE>()
.setFlags()
.setName("GattServer")
.getAdvertisingData()
);

if (error) {
printf("Gap::setAdvertisingPayload() failed with error %d", error);
return false;
}

return true;
}

events::EventQueue &_event_queue;
Expand Down