Skip to content

Commit ff0a2a9

Browse files
check connectible sizes
1 parent c5bad80 commit ff0a2a9

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

features/FEATURE_BLE/ble/generic/GenericGap.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -787,6 +787,8 @@ class GenericGap :
787787
BitArray<MAX_ADVERTISING_SETS> _existing_sets;
788788
BitArray<MAX_ADVERTISING_SETS> _active_sets;
789789
BitArray<MAX_ADVERTISING_SETS> _active_periodic_sets;
790+
BitArray<MAX_ADVERTISING_SETS> _connectable_payload_size_exceeded;
791+
BitArray<MAX_ADVERTISING_SETS> _set_is_connectable;
790792

791793
// deprecation flags
792794
mutable bool _deprecated_scan_api_used : 1;

features/FEATURE_BLE/source/generic/GenericGap.cpp

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1380,6 +1380,8 @@ ble_error_t GenericGap::reset(void)
13801380
_existing_sets.clear();
13811381
_active_sets.clear();
13821382
_active_periodic_sets.clear();
1383+
_connectable_payload_size_exceeded.clear();
1384+
_set_is_connectable.clear();
13831385

13841386
/* clear advertising set data on the controller */
13851387
_pal_gap.clear_advertising_sets();
@@ -2033,6 +2035,8 @@ ble_error_t GenericGap::createAdvertisingSet(
20332035
uint8_t new_handle = LEGACY_ADVERTISING_HANDLE + 1;
20342036
uint8_t end = getMaxAdvertisingSetNumber();
20352037

2038+
*handle = INVALID_ADVERTISING_HANDLE;
2039+
20362040
for (; new_handle < end; ++new_handle) {
20372041
if (!_existing_sets.get(new_handle)) {
20382042
ble_error_t err = setExtendedAdvertisingParameters(
@@ -2045,11 +2049,11 @@ ble_error_t GenericGap::createAdvertisingSet(
20452049

20462050
_existing_sets.set(new_handle);
20472051
*handle = new_handle;
2052+
20482053
return BLE_ERROR_NONE;
20492054
}
20502055
}
20512056

2052-
*handle = INVALID_ADVERTISING_HANDLE;
20532057
return BLE_ERROR_NO_MEM;
20542058
}
20552059

@@ -2086,6 +2090,8 @@ ble_error_t GenericGap::destroyAdvertisingSet(advertising_handle_t handle)
20862090
return err;
20872091
}
20882092

2093+
_connectable_payload_size_exceeded.clear(handle);
2094+
_set_is_connectable.clear(handle);
20892095
_existing_sets.clear(handle);
20902096
return BLE_ERROR_NONE;
20912097
}
@@ -2140,6 +2146,10 @@ ble_error_t GenericGap::setExtendedAdvertisingParameters(
21402146
return BLE_ERROR_INVALID_PARAM;
21412147
}
21422148

2149+
if (_active_sets.get(handle)) {
2150+
return BLE_ERROR_OPERATION_NOT_PERMITTED;
2151+
}
2152+
21432153
pal::advertising_event_properties_t event_properties(params.getType());
21442154
event_properties.include_tx_power = params.getTxPowerInHeader();
21452155
event_properties.omit_advertiser_address = params.getAnonymousAdvertising();
@@ -2173,6 +2183,12 @@ ble_error_t GenericGap::setExtendedAdvertisingParameters(
21732183
return err;
21742184
}
21752185

2186+
if (event_properties.connectable) {
2187+
_set_is_connectable.set(handle);
2188+
} else {
2189+
_set_is_connectable.clear(handle);
2190+
}
2191+
21762192
return _pal_gap.set_advertising_set_random_address(
21772193
handle,
21782194
_random_static_identity_address
@@ -2261,9 +2277,30 @@ ble_error_t GenericGap::setAdvertisingData(
22612277
}
22622278

22632279
if (payload.size() > getMaxAdvertisingDataLength()) {
2280+
MBED_WARNING(MBED_ERROR_INVALID_SIZE, "Payload size exceeds getMaxAdvertisingDataLength().");
22642281
return BLE_ERROR_INVALID_PARAM;
22652282
}
22662283

2284+
if (!_active_sets.get(handle) && payload.size() > getMaxActiveSetAdvertisingDataLength()) {
2285+
MBED_WARNING(MBED_ERROR_INVALID_SIZE, "Payload size for active sets needs to fit in a single operation"
2286+
" - not greater than getMaxActiveSetAdvertisingDataLength().");
2287+
return BLE_ERROR_INVALID_PARAM;
2288+
}
2289+
2290+
if (!scan_response) {
2291+
if (payload.size() > getMaxConnectableAdvertisingDataLength()) {
2292+
if (_active_sets.get(handle) && _set_is_connectable.get(handle)) {
2293+
MBED_WARNING(MBED_ERROR_INVALID_SIZE, "Payload size for connectable advertising"
2294+
" exceeds getMaxAdvertisingDataLength().");
2295+
return BLE_ERROR_INVALID_PARAM;
2296+
} else {
2297+
_connectable_payload_size_exceeded.set(handle);
2298+
}
2299+
} else {
2300+
_connectable_payload_size_exceeded.clear(handle);
2301+
}
2302+
}
2303+
22672304
// select the pal function
22682305
set_data_fn_t set_data = scan_response ?
22692306
&pal::Gap::set_extended_scan_response_data :
@@ -2322,6 +2359,11 @@ ble_error_t GenericGap::startAdvertising(
23222359
return BLE_ERROR_INVALID_PARAM;
23232360
}
23242361

2362+
if (_connectable_payload_size_exceeded.get(handle) && _set_is_connectable.get(handle)) {
2363+
MBED_WARNING(MBED_ERROR_INVALID_SIZE, "Payload size exceeds size allowed for connectable advertising.");
2364+
return BLE_ERROR_INVALID_STATE;
2365+
}
2366+
23252367
if (is_extended_advertising_available()) {
23262368
error = _pal_gap.extended_advertising_enable(
23272369
/* enable */ true,

0 commit comments

Comments
 (0)