Skip to content

Commit 23022dd

Browse files
author
Cruz Monrreal
authored
Merge pull request #8998 from pan-/ble-extended-advertising-fixes
Ble extended advertising fixes
2 parents eec536b + a545da7 commit 23022dd

File tree

21 files changed

+168
-49
lines changed

21 files changed

+168
-49
lines changed

features/FEATURE_BLE/ble/gap/Events.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ struct AdvertisingReportEvent {
6363
advertising_sid_t SID,
6464
advertising_power_t txPower,
6565
rssi_t rssi,
66-
periodic_interval_t periodicInterval,
66+
uint16_t periodicInterval,
6767
const peer_address_type_t &directAddressType,
6868
const address_t &directAddress,
6969
const mbed::Span<const uint8_t> &advertisingData
@@ -133,10 +133,15 @@ struct AdvertisingReportEvent {
133133
return rssi;
134134
}
135135

136+
/** Indicate if periodic interval is valid */
137+
bool isPeriodicIntervalPresent() const {
138+
return periodicInterval != 0;
139+
}
140+
136141
/** Get interval. */
137142
periodic_interval_t getPeriodicInterval() const
138143
{
139-
return periodicInterval;
144+
return periodic_interval_t(periodicInterval);
140145
}
141146

142147
/** Get target address type in directed advertising. */
@@ -166,7 +171,7 @@ struct AdvertisingReportEvent {
166171
advertising_sid_t SID;
167172
advertising_power_t txPower;
168173
rssi_t rssi;
169-
periodic_interval_t periodicInterval;
174+
uint16_t periodicInterval;
170175
peer_address_type_t directAddressType;
171176
const address_t &directAddress;
172177
mbed::Span<const uint8_t> advertisingData;

features/FEATURE_BLE/ble/gap/Types.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ struct advertising_data_status_t : SafeEnum<advertising_data_status_t, uint8_t>
229229
* Explicit constructor from a raw value.
230230
*/
231231
explicit advertising_data_status_t(uint8_t raw_value) :
232-
SafeEnum(static_cast<advertising_data_status_t>(raw_value))
232+
SafeEnum(raw_value)
233233
{
234234
}
235235

features/FEATURE_BLE/source/BLE.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,8 @@ BLE::initImplementation(FunctionPointerWithContext<InitializationCompleteCallbac
142142

143143
// this stub is required by ARMCC otherwise link will systematically fail
144144
MBED_WEAK BLEInstanceBase* createBLEInstance() {
145-
MBED_ERROR(MBED_MAKE_ERROR(MBED_MODULE_BLE, MBED_ERROR_CODE_BLE_BACKEND_CREATION_FAILED), "Please provide an implementation for mbed BLE");
145+
MBED_ASSERT("No BLE instance implementation.");
146+
printf("Please provide an implementation for mbed BLE");
146147
return NULL;
147148
}
148149

features/FEATURE_BLE/source/generic/GenericGap.cpp

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1589,7 +1589,7 @@ void GenericGap::on_advertising_report(const pal::GapAdvertisingReportEvent &e)
15891589
/* SID - NO ADI FIELD IN THE PDU */ 0xFF,
15901590
/* tx power information not available */ 127,
15911591
advertising.rssi,
1592-
/* NO PERIODIC ADVERTISING */ periodic_interval_t(0),
1592+
/* NO PERIODIC ADVERTISING */ 0,
15931593
peer_address_type_t::ANONYMOUS,
15941594
ble::address_t (),
15951595
mbed::Span<const uint8_t>(advertising.data.data(), advertising.data.size())
@@ -2141,7 +2141,7 @@ ble_error_t GenericGap::setExtendedAdvertisingParameters(
21412141
params.getChannel39()
21422142
);
21432143

2144-
return _pal_gap.set_extended_advertising_parameters(
2144+
ble_error_t err = _pal_gap.set_extended_advertising_parameters(
21452145
handle,
21462146
event_properties,
21472147
params.getMinPrimaryInterval().value(),
@@ -2158,6 +2158,15 @@ ble_error_t GenericGap::setExtendedAdvertisingParameters(
21582158
/* SID */ (handle % 0x10),
21592159
params.getScanRequestNotification()
21602160
);
2161+
2162+
if (err) {
2163+
return err;
2164+
}
2165+
2166+
return _pal_gap.set_advertising_set_random_address(
2167+
handle,
2168+
_random_static_identity_address
2169+
);
21612170
}
21622171

21632172
ble_error_t GenericGap::setAdvertisingPayload(
@@ -2305,21 +2314,6 @@ ble_error_t GenericGap::startAdvertising(
23052314
}
23062315

23072316
if (is_extended_advertising_available()) {
2308-
ble::address_t random_address;
2309-
2310-
if (!getUnresolvableRandomAddress(random_address)) {
2311-
return BLE_ERROR_INTERNAL_STACK_FAILURE;
2312-
}
2313-
2314-
error = _pal_gap.set_advertising_set_random_address(
2315-
handle,
2316-
random_address
2317-
);
2318-
2319-
if (error) {
2320-
return error;
2321-
}
2322-
23232317
error = _pal_gap.extended_advertising_enable(
23242318
/* enable */ true,
23252319
/* number of advertising sets */ 1,
@@ -2648,7 +2642,7 @@ void GenericGap::on_extended_advertising_report(
26482642
advertising_sid,
26492643
tx_power,
26502644
rssi,
2651-
periodic_interval_t(periodic_advertising_interval),
2645+
periodic_advertising_interval,
26522646
(PeerAddressType_t::type) direct_address_type.value(),
26532647
(BLEProtocol::AddressBytes_t &) direct_address,
26542648
mbed::make_Span(data, data_length)
@@ -2927,7 +2921,7 @@ ble_error_t GenericGap::createSync(
29272921
return BLE_ERROR_NOT_IMPLEMENTED;
29282922
}
29292923

2930-
if (peerAddressType != peer_address_type_t::PUBLIC ||
2924+
if (peerAddressType != peer_address_type_t::PUBLIC &&
29312925
peerAddressType != peer_address_type_t::RANDOM
29322926
) {
29332927
return BLE_ERROR_INVALID_PARAM;

features/FEATURE_BLE/targets/TARGET_CORDIO/driver/CordioHCIDriver.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <stddef.h>
1818
#include <string.h>
1919

20+
#include "CordioBLE.h"
2021
#include "CordioHCIDriver.h"
2122
#include "hci_api.h"
2223
#include "hci_cmd.h"
@@ -139,10 +140,26 @@ void CordioHCIDriver::handle_reset_sequence(uint8_t *pMsg)
139140
HciReadBdAddrCmd();
140141
break;
141142

142-
case HCI_OPCODE_READ_BD_ADDR:
143+
case HCI_OPCODE_READ_BD_ADDR: {
143144
/* parse and store event parameters */
144145
BdaCpy(hciCoreCb.bdAddr, pMsg);
145146

147+
ble::address_t static_address;
148+
149+
if (get_random_static_address(static_address)) {
150+
// note: will send the HCI command to send the random address
151+
cordio::BLE::deviceInstance().getGap().setAddress(
152+
BLEProtocol::AddressType::RANDOM_STATIC,
153+
static_address.data()
154+
);
155+
} else {
156+
/* send next command in sequence */
157+
HciLeReadBufSizeCmd();
158+
}
159+
break;
160+
}
161+
162+
case HCI_OPCODE_LE_SET_RAND_ADDR:
146163
/* send next command in sequence */
147164
HciLeReadBufSizeCmd();
148165
break;
@@ -246,6 +263,11 @@ void CordioHCIDriver::handle_reset_sequence(uint8_t *pMsg)
246263
}
247264
}
248265

266+
bool CordioHCIDriver::get_random_static_address(ble::address_t& address)
267+
{
268+
return false;
269+
}
270+
249271
void CordioHCIDriver::signal_reset_sequence_done()
250272
{
251273
hci_mbed_os_signal_reset_sequence_done();

features/FEATURE_BLE/targets/TARGET_CORDIO/driver/CordioHCIDriver.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#include <stddef.h>
2121
#include <stdint.h>
22+
#include <BLETypes.h>
2223
#include "wsf_buf.h"
2324
#include "CordioHCITransportDriver.h"
2425

@@ -108,6 +109,13 @@ class CordioHCIDriver {
108109
*/
109110
virtual void handle_reset_sequence(uint8_t *msg);
110111

112+
/**
113+
* Get the random static address of the controller
114+
*
115+
* @return false if the address has not been set and true otherwise.
116+
*/
117+
virtual bool get_random_static_address(ble::address_t& address);
118+
111119
/**
112120
* Signal to the stack that the reset sequence has been done.
113121
*/

features/FEATURE_BLE/targets/TARGET_CORDIO/source/CordioBLE.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ wsfHandlerId_t stack_handler_id;
4848
*/
4949
MBED_WEAK ble::vendor::cordio::CordioHCIDriver& ble_cordio_get_hci_driver()
5050
{
51-
error("Please provide an implementation for the HCI driver");
51+
MBED_ASSERT("No HCI driver");
52+
printf("Please provide an implementation for the HCI driver");
5253
ble::vendor::cordio::CordioHCIDriver* bad_instance = NULL;
5354
return *bad_instance;
5455
}
@@ -100,7 +101,7 @@ BLE::BLE(CordioHCIDriver& hci_driver) :
100101
_last_update_us(0)
101102
{
102103
_hci_driver = &hci_driver;
103-
104+
stack_setup();
104105
}
105106

106107
BLE::~BLE() { }
@@ -400,7 +401,6 @@ void BLE::stack_setup()
400401
void BLE::start_stack_reset()
401402
{
402403
_hci_driver->initialize();
403-
stack_setup();
404404
DmDevReset();
405405
}
406406

features/FEATURE_BLE/targets/TARGET_CORDIO/source/CordioPalGap.cpp

Lines changed: 84 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,76 @@ ble_error_t Gap::set_extended_advertising_parameters(
670670
bool scan_request_notification
671671
)
672672
{
673+
uint8_t adv_type;
674+
675+
if (event_properties.use_legacy_pdu) {
676+
if (event_properties.directed == false) {
677+
if (event_properties.high_duty_cycle) {
678+
return BLE_ERROR_INVALID_PARAM;
679+
}
680+
681+
if (event_properties.connectable && event_properties.scannable == false) {
682+
return BLE_ERROR_INVALID_PARAM;
683+
}
684+
685+
if (event_properties.connectable && event_properties.scannable) {
686+
adv_type = DM_ADV_CONN_UNDIRECT;
687+
} else if (event_properties.scannable) {
688+
adv_type = DM_ADV_SCAN_UNDIRECT;
689+
} else {
690+
adv_type = DM_ADV_NONCONN_UNDIRECT;
691+
}
692+
} else {
693+
if (event_properties.scannable) {
694+
return BLE_ERROR_INVALID_PARAM;
695+
}
696+
697+
if (event_properties.connectable == false) {
698+
return BLE_ERROR_INVALID_PARAM;
699+
}
700+
701+
if (event_properties.high_duty_cycle) {
702+
adv_type = DM_ADV_CONN_DIRECT;
703+
} else {
704+
adv_type = DM_ADV_CONN_DIRECT_LO_DUTY;
705+
}
706+
}
707+
} else {
708+
if (event_properties.directed == false) {
709+
if (event_properties.high_duty_cycle) {
710+
return BLE_ERROR_INVALID_PARAM;
711+
}
712+
713+
if (event_properties.connectable && event_properties.scannable) {
714+
adv_type = DM_ADV_CONN_UNDIRECT;
715+
} else if (event_properties.scannable) {
716+
adv_type = DM_ADV_SCAN_UNDIRECT;
717+
} else if (event_properties.connectable) {
718+
adv_type = DM_EXT_ADV_CONN_UNDIRECT;
719+
} else {
720+
adv_type = DM_ADV_NONCONN_UNDIRECT;
721+
}
722+
} else {
723+
// note: not sure how to act with the high duty cycle in scannable
724+
// and non connectable mode. These cases looks correct from a Bluetooth
725+
// standpoint
726+
727+
if (event_properties.connectable && event_properties.scannable) {
728+
return BLE_ERROR_INVALID_PARAM;
729+
} else if (event_properties.connectable) {
730+
if (event_properties.high_duty_cycle) {
731+
adv_type = DM_ADV_CONN_DIRECT;
732+
} else {
733+
adv_type = DM_ADV_CONN_DIRECT_LO_DUTY;
734+
}
735+
} else if (event_properties.scannable) {
736+
adv_type = DM_EXT_ADV_SCAN_DIRECT;
737+
} else {
738+
adv_type = DM_EXT_ADV_NONCONN_DIRECT;
739+
}
740+
}
741+
}
742+
673743
DmAdvSetInterval(
674744
advertising_handle,
675745
primary_advertising_interval_min,
@@ -709,7 +779,7 @@ ble_error_t Gap::set_extended_advertising_parameters(
709779

710780
DmAdvConfig(
711781
advertising_handle,
712-
event_properties.value(), // TODO: use the raw value here ???
782+
adv_type,
713783
peer_address_type.value(),
714784
const_cast<uint8_t *>(peer_address.data())
715785
);
@@ -853,7 +923,7 @@ uint16_t Gap::get_maximum_advertising_data_length()
853923

854924
uint8_t Gap::get_max_number_of_advertising_sets()
855925
{
856-
return HciGetNumSupAdvSets();
926+
return std::min(HciGetNumSupAdvSets(), (uint8_t) DM_NUM_ADV_SETS);
857927
}
858928

859929
ble_error_t Gap::remove_advertising_set(advertising_handle_t advertising_handle)
@@ -911,12 +981,11 @@ ble_error_t Gap::extended_scan_enable(
911981
if (enable) {
912982
uint32_t duration_ms = duration * 10;
913983

914-
DmScanModeExt();
915984
DmScanStart(
916985
scanning_phys.value(),
917986
DM_DISC_MODE_NONE,
918987
extended_scan_type,
919-
filter_duplicates.value(), // TODO: cordio API incomplete ???
988+
filter_duplicates.value(),
920989
duration_ms > 0xFFFF ? 0xFFFF : duration_ms,
921990
period
922991
);
@@ -936,23 +1005,25 @@ ble_error_t Gap::periodic_advertising_create_sync(
9361005
uint16_t sync_timeout
9371006
)
9381007
{
939-
if (use_periodic_advertiser_list) {
940-
DmDevSetExtFilterPolicy(
941-
DM_ADV_HANDLE_DEFAULT,
942-
DM_FILT_POLICY_MODE_SYNC,
943-
HCI_FILT_PER_ADV_LIST
944-
);
945-
}
1008+
DmDevSetExtFilterPolicy(
1009+
DM_ADV_HANDLE_DEFAULT,
1010+
DM_FILT_POLICY_MODE_SYNC,
1011+
use_periodic_advertiser_list ? HCI_FILT_PER_ADV_LIST : HCI_FILT_NONE
1012+
);
9461013

947-
DmSyncStart(
1014+
dmSyncId_t sync_id = DmSyncStart(
9481015
advertising_sid,
9491016
peer_address_type.value(),
9501017
peer_address.data(),
9511018
allowed_skip,
9521019
sync_timeout
9531020
);
9541021

955-
return BLE_ERROR_INVALID_PARAM;
1022+
if (sync_id == DM_SYNC_ID_NONE) {
1023+
return BLE_ERROR_INTERNAL_STACK_FAILURE;
1024+
} else {
1025+
return BLE_ERROR_NONE;
1026+
}
9561027
}
9571028

9581029
ble_error_t Gap::cancel_periodic_advertising_create_sync()

features/FEATURE_BLE/targets/TARGET_CORDIO/stack/cordio_stack/ble-host/sources/stack/cfg/cfg_stack.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,13 @@ extern "C" {
8686

8787
/*! \brief Number of supported advertising sets: must be set to 1 for legacy advertising */
8888
#ifndef DM_NUM_ADV_SETS
89-
#define DM_NUM_ADV_SETS 1
89+
#define DM_NUM_ADV_SETS 3
9090
#endif
9191

9292
/*! \brief Number of scanner and initiator PHYs (LE 1M, LE 2M and LE Coded): must be set to 1 for
9393
legacy scanner and initiator */
9494
#ifndef DM_NUM_PHYS
95-
#define DM_NUM_PHYS 1
95+
#define DM_NUM_PHYS 3
9696
#endif
9797
/**@}*/
9898

features/FEATURE_BLE/targets/TARGET_CORDIO/stack/cordio_stack/ble-host/sources/stack/dm/dm_adv.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ typedef struct
151151
uint8_t advHandle;
152152
uint8_t op;
153153
uint8_t len;
154-
uint8_t *pData;
154+
uint8_t pData[];
155155
} dmAdvPerApiSetData_t;
156156

157157
/* Data structure for DM_ADV_PER_MSG_API_START */

0 commit comments

Comments
 (0)