diff --git a/features/FEATURE_BLE/ble/gap/AdvertisingDataParser.h b/features/FEATURE_BLE/ble/gap/AdvertisingDataParser.h index e41610b9fe..1bc947cad0 100644 --- a/features/FEATURE_BLE/ble/gap/AdvertisingDataParser.h +++ b/features/FEATURE_BLE/ble/gap/AdvertisingDataParser.h @@ -87,7 +87,7 @@ public: element_t next() { element_t element = { - (ble::adv_data_type_t::type) data[TYPE_INDEX], + (ble::adv_data_type_t::type) data[position + TYPE_INDEX], data.subspan(position + VALUE_INDEX, current_length() - (TYPE_SIZE)) }; diff --git a/features/FEATURE_BLE/ble/gap/AdvertisingParameters.h b/features/FEATURE_BLE/ble/gap/AdvertisingParameters.h index 18e9c83f37..304abddaad 100644 --- a/features/FEATURE_BLE/ble/gap/AdvertisingParameters.h +++ b/features/FEATURE_BLE/ble/gap/AdvertisingParameters.h @@ -135,7 +135,7 @@ public: _minInterval(minInterval), _maxInterval(maxInterval), _peerAddressType(target_peer_address_type_t::PUBLIC), - _ownAddressType(own_address_type_t::PUBLIC), + _ownAddressType(own_address_type_t::RANDOM), _policy(advertising_filter_policy_t::NO_FILTER), _primaryPhy(phy_t::LE_1M), _secondaryPhy(phy_t::LE_1M), diff --git a/features/FEATURE_BLE/ble/gap/ConnectionParameters.h b/features/FEATURE_BLE/ble/gap/ConnectionParameters.h index d93052a48f..33aeaf5c46 100644 --- a/features/FEATURE_BLE/ble/gap/ConnectionParameters.h +++ b/features/FEATURE_BLE/ble/gap/ConnectionParameters.h @@ -145,10 +145,10 @@ public: phy_t phy = phy_t::LE_1M, scan_interval_t scanInterval = scan_interval_t::min(), scan_window_t scanWindow = scan_window_t::min(), - conn_interval_t minConnectionInterval = conn_interval_t::min(), - conn_interval_t maxConnectionInterval = conn_interval_t::max(), + conn_interval_t minConnectionInterval = conn_interval_t(50), + conn_interval_t maxConnectionInterval = conn_interval_t(100), slave_latency_t slaveLatency = slave_latency_t::min(), - supervision_timeout_t connectionSupervisionTimeout = supervision_timeout_t::max(), + supervision_timeout_t connectionSupervisionTimeout = supervision_timeout_t(100), conn_event_length_t minEventLength = conn_event_length_t::min(), conn_event_length_t maxEventLength = conn_event_length_t::max() ); @@ -215,7 +215,7 @@ public: * * @return A reference to this. */ - ConnectionParameters &setFilterPolicy(initiator_filter_policy_t filterPolicy) + ConnectionParameters &setFilter(initiator_filter_policy_t filterPolicy) { _filterPolicy = filterPolicy; @@ -282,7 +282,7 @@ public: * * @return The initiator policy. */ - initiator_filter_policy_t getFilterPolicy() const + initiator_filter_policy_t getFilter() const { return _filterPolicy; } diff --git a/features/FEATURE_BLE/ble/gap/Events.h b/features/FEATURE_BLE/ble/gap/Events.h index 3c09137517..38c910a34c 100644 --- a/features/FEATURE_BLE/ble/gap/Events.h +++ b/features/FEATURE_BLE/ble/gap/Events.h @@ -152,7 +152,7 @@ struct AdvertisingReportEvent { } /** Get payload. */ - const mbed::Span &getAdvertisingData() const + const mbed::Span &getPayload() const { return advertisingData; } @@ -630,10 +630,10 @@ private: * * @see ble::Gap::EventHandler::onDisconnectionComplete(). */ -struct DisconnectionEvent { +struct DisconnectionCompleteEvent { #if !defined(DOXYGEN_ONLY) - DisconnectionEvent( + DisconnectionCompleteEvent( connection_handle_t connectionHandle, const disconnection_reason_t &reason ) : diff --git a/features/FEATURE_BLE/ble/gap/Gap.h b/features/FEATURE_BLE/ble/gap/Gap.h index ecf281fc1b..194369c623 100644 --- a/features/FEATURE_BLE/ble/gap/Gap.h +++ b/features/FEATURE_BLE/ble/gap/Gap.h @@ -427,7 +427,7 @@ public: * * @see disconnect() */ - virtual void onDisconnectionComplete(const DisconnectionEvent &event) + virtual void onDisconnectionComplete(const DisconnectionCompleteEvent &event) { } @@ -536,7 +536,7 @@ public: * * @return Maximum advertising data length supported. */ - virtual uint8_t getMaxAdvertisingDataLength(); + virtual uint16_t getMaxAdvertisingDataLength(); /** Create an advertising set and apply the passed in parameters. The handle returned * by this function must be used for all other calls that accept an advertising handle. @@ -711,8 +711,8 @@ public: /** Start scanning. * - * @param filtering Filtering policy. * @param duration How long to scan for. Special value 0 means scan forever. + * @param filtering Filtering policy. * @param period How long to scan for in single period. If the period is 0 and duration * is nonzero the scan will last for single duration. * @@ -726,8 +726,8 @@ public: * @see EventHandler::onScanTimeout when scanning timeout. */ virtual ble_error_t startScan( - duplicates_filter_t filtering = duplicates_filter_t::DISABLE, scan_duration_t duration = scan_duration_t::forever(), + duplicates_filter_t filtering = duplicates_filter_t::DISABLE, scan_period_t period = scan_period_t(0) ); diff --git a/features/FEATURE_BLE/ble/gap/ScanParameters.h b/features/FEATURE_BLE/ble/gap/ScanParameters.h index 494cc818f6..9eb9c610bc 100644 --- a/features/FEATURE_BLE/ble/gap/ScanParameters.h +++ b/features/FEATURE_BLE/ble/gap/ScanParameters.h @@ -121,7 +121,7 @@ public: scan_window_t scan_interval = scan_interval_t::min(), scan_interval_t scan_window = scan_window_t::min(), bool active_scanning = false, - own_address_type_t own_address_type = own_address_type_t::PUBLIC, + own_address_type_t own_address_type = own_address_type_t::RANDOM, scanning_filter_policy_t scanning_filter_policy = scanning_filter_policy_t::NO_FILTER ) : own_address_type(own_address_type), diff --git a/features/FEATURE_BLE/ble/gap/Types.h b/features/FEATURE_BLE/ble/gap/Types.h index 59f07ba91b..1eab24978a 100644 --- a/features/FEATURE_BLE/ble/gap/Types.h +++ b/features/FEATURE_BLE/ble/gap/Types.h @@ -891,6 +891,9 @@ struct peripheral_privacy_configuration_t { PERFORM_AUTHENTICATION_PROCEDURE }; + MBED_DEPRECATED_SINCE("mbed-os-5.11", "Use resolution_strategy_t instead.") + typedef resolution_strategy_t ResolutionStrategy; + /** * Connection strategy to use when a connection request contains a * private resolvable address. @@ -941,6 +944,9 @@ struct central_privay_configuration_t { RESOLVE_AND_FILTER }; + MBED_DEPRECATED_SINCE("mbed-os-5.11", "Use resolution_strategy_t instead.") + typedef resolution_strategy_t ResolutionStrategy; + /** * Resolution strategy applied to advertising packets received by the * local device. diff --git a/features/FEATURE_BLE/ble/generic/GenericGap.h b/features/FEATURE_BLE/ble/generic/GenericGap.h index d3a02ffdd9..8b3fd4af13 100644 --- a/features/FEATURE_BLE/ble/generic/GenericGap.h +++ b/features/FEATURE_BLE/ble/generic/GenericGap.h @@ -90,7 +90,7 @@ public: /** @copydoc Gap::getMaxAdvertisingDataLength */ - virtual uint8_t getMaxAdvertisingDataLength(); + virtual uint16_t getMaxAdvertisingDataLength(); /** @copydoc Gap::createAdvertisingSet */ @@ -175,8 +175,8 @@ public: /** @copydoc Gap::startScan */ virtual ble_error_t startScan( - duplicates_filter_t filtering, scan_duration_t duration, + duplicates_filter_t filtering, scan_period_t period ); diff --git a/features/FEATURE_BLE/source/BLE.cpp b/features/FEATURE_BLE/source/BLE.cpp index e61356c075..e9f66c3bb2 100644 --- a/features/FEATURE_BLE/source/BLE.cpp +++ b/features/FEATURE_BLE/source/BLE.cpp @@ -17,6 +17,7 @@ #include #include "ble/BLE.h" #include "ble/BLEInstanceBase.h" +#include "platform/mbed_critical.h" #if defined(TARGET_OTA_ENABLED) #include "ble/services/DFUService.h" @@ -299,16 +300,19 @@ void BLE::waitForEvent(void) void BLE::processEvents() { + core_util_critical_section_enter(); if (event_signaled == false) { + core_util_critical_section_exit(); return; } + event_signaled = false; + core_util_critical_section_exit(); + if (!transport) { MBED_ERROR(MBED_MAKE_ERROR(MBED_MODULE_BLE, MBED_ERROR_CODE_BLE_BACKEND_NOT_INITIALIZED), "bad handle to underlying transport"); } - event_signaled = false; - transport->processEvents(); } @@ -328,11 +332,14 @@ void BLE::onEventsToProcess(const BLE::OnEventsToProcessCallback_t& callback) void BLE::signalEventsToProcess() { + core_util_critical_section_enter(); if (event_signaled == true) { + core_util_critical_section_exit(); return; } event_signaled = true; + core_util_critical_section_exit(); if (whenEventsToProcess) { OnEventsToProcessCallbackContext params = { diff --git a/features/FEATURE_BLE/source/gap/ConnectionParameters.cpp b/features/FEATURE_BLE/source/gap/ConnectionParameters.cpp index 3ed5b646ef..837a706f85 100644 --- a/features/FEATURE_BLE/source/gap/ConnectionParameters.cpp +++ b/features/FEATURE_BLE/source/gap/ConnectionParameters.cpp @@ -30,7 +30,7 @@ ConnectionParameters::ConnectionParameters( conn_event_length_t maxEventLength ) : _filterPolicy(initiator_filter_policy_t::NO_FILTER), - _ownAddressType(own_address_type_t::PUBLIC) + _ownAddressType(own_address_type_t::RANDOM) { for (uint8_t i = 0; i < MAX_PARAM_PHYS; ++i) { _enabledPhy[i] = false; diff --git a/features/FEATURE_BLE/source/gap/Gap.cpp b/features/FEATURE_BLE/source/gap/Gap.cpp index 4cb4c6a50e..3515ddd531 100644 --- a/features/FEATURE_BLE/source/gap/Gap.cpp +++ b/features/FEATURE_BLE/source/gap/Gap.cpp @@ -30,7 +30,7 @@ uint8_t Gap::getMaxAdvertisingSetNumber() return 1; } -uint8_t Gap::getMaxAdvertisingDataLength() +uint16_t Gap::getMaxAdvertisingDataLength() { /* Requesting action from porter(s): override this API if this capability is supported. */ return LEGACY_ADVERTISING_MAX_SIZE; @@ -145,8 +145,8 @@ ble_error_t Gap::setScanParameters(const ScanParameters ¶ms) }; ble_error_t Gap::startScan( - duplicates_filter_t filtering, scan_duration_t duration, + duplicates_filter_t filtering, scan_period_t period ) { diff --git a/features/FEATURE_BLE/source/generic/GenericGap.cpp b/features/FEATURE_BLE/source/generic/GenericGap.cpp index a4928a07df..9191b55fbe 100644 --- a/features/FEATURE_BLE/source/generic/GenericGap.cpp +++ b/features/FEATURE_BLE/source/generic/GenericGap.cpp @@ -675,10 +675,13 @@ ble_error_t GenericGap::connect( return BLE_ERROR_INVALID_PARAM; } + // ensure scan is stopped. + _pal_gap.scan_enable(false, false); + return _pal_gap.create_connection( connectionParams.getScanIntervalArray()[0], connectionParams.getScanWindowArray()[0], - connectionParams.getFilterPolicy(), + connectionParams.getFilter(), (pal::connection_peer_address_type_t::type) peerAddressType.value(), peerAddress, connectionParams.getOwnAddressType(), @@ -691,6 +694,9 @@ ble_error_t GenericGap::connect( ); } + // ensure scan is stopped. + _pal_gap.extended_scan_enable(false, pal::duplicates_filter_t::DISABLE, 0, 0); + // reduce the address type to public or random peer_address_type_t adjusted_address_type(peer_address_type_t::PUBLIC); @@ -701,7 +707,7 @@ ble_error_t GenericGap::connect( } return _pal_gap.extended_create_connection( - connectionParams.getFilterPolicy(), + connectionParams.getFilter(), connectionParams.getOwnAddressType(), adjusted_address_type, peerAddress, @@ -1442,7 +1448,7 @@ void GenericGap::processDisconnectionEvent( if (_eventHandler) { _eventHandler->onDisconnectionComplete( - DisconnectionEvent( + DisconnectionCompleteEvent( handle, (disconnection_reason_t::type) reason ) @@ -1997,7 +2003,7 @@ uint8_t GenericGap::getMaxAdvertisingSetNumber() } } -uint8_t GenericGap::getMaxAdvertisingDataLength() +uint16_t GenericGap::getMaxAdvertisingDataLength() { useVersionTwoAPI(); return _pal_gap.get_maximum_advertising_data_length(); @@ -2053,7 +2059,7 @@ ble_error_t GenericGap::destroyAdvertisingSet(advertising_handle_t handle) return BLE_ERROR_INVALID_PARAM; } - if (_existing_sets.get(handle) == false) { + if (!_existing_sets.get(handle)) { return BLE_ERROR_INVALID_PARAM; } @@ -2357,10 +2363,14 @@ ble_error_t GenericGap::stopAdvertising(advertising_handle_t handle) return BLE_ERROR_INVALID_PARAM; } - if (_existing_sets.get(handle)) { + if (!_existing_sets.get(handle)) { return BLE_ERROR_INVALID_PARAM; } + if (!_active_sets.get(handle)) { + return BLE_ERROR_INVALID_STATE; + } + ble_error_t status; if (is_extended_advertising_available()) { @@ -2427,7 +2437,7 @@ ble_error_t GenericGap::setPeriodicAdvertisingParameters( } if (!_existing_sets.get(handle)) { - return BLE_ERROR_INVALID_STATE; + return BLE_ERROR_INVALID_PARAM; } return _pal_gap.set_periodic_advertising_parameters( @@ -2454,7 +2464,7 @@ ble_error_t GenericGap::setPeriodicAdvertisingPayload( } if (!_existing_sets.get(handle)) { - return BLE_ERROR_INVALID_STATE; + return BLE_ERROR_INVALID_PARAM; } if (payload.size() > getMaxAdvertisingDataLength()) { @@ -2511,7 +2521,7 @@ ble_error_t GenericGap::startPeriodicAdvertising(advertising_handle_t handle) } if (!_existing_sets.get(handle)) { - return BLE_ERROR_INVALID_STATE; + return BLE_ERROR_INVALID_PARAM; } if (_active_sets.get(handle) == false) { @@ -2544,7 +2554,7 @@ ble_error_t GenericGap::stopPeriodicAdvertising(advertising_handle_t handle) } if (!_existing_sets.get(handle)) { - return BLE_ERROR_INVALID_STATE; + return BLE_ERROR_INVALID_PARAM; } if (_active_periodic_sets.get(handle) == false) { @@ -2859,8 +2869,8 @@ ble_error_t GenericGap::setScanParameters(const ScanParameters ¶ms) } ble_error_t GenericGap::startScan( - duplicates_filter_t filtering, scan_duration_t duration, + duplicates_filter_t filtering, scan_period_t period ) { diff --git a/features/FEATURE_BLE/targets/TARGET_CORDIO/source/CordioPalGap.cpp b/features/FEATURE_BLE/targets/TARGET_CORDIO/source/CordioPalGap.cpp index 1179fa776e..7b15cfe3d9 100644 --- a/features/FEATURE_BLE/targets/TARGET_CORDIO/source/CordioPalGap.cpp +++ b/features/FEATURE_BLE/targets/TARGET_CORDIO/source/CordioPalGap.cpp @@ -510,30 +510,7 @@ void Gap::gap_handler(const wsfMsgHdr_t *msg) handler->on_periodic_advertising_sync_loss(evt->syncHandle); } break; - - case DM_CONN_OPEN_IND: { - if (!handler) { - break; - } - - // TODO: filter with old event ... - const hciLeConnCmplEvt_t *evt = (const hciLeConnCmplEvt_t *) msg; - handler->on_enhanced_connection_complete( - hci_error_code_t(evt->status), - evt->handle, - connection_role_t(evt->role), - connection_peer_address_type_t(evt->addrType), - evt->peerAddr, - evt->localRpa, - evt->peerRpa, - evt->connInterval, - evt->connLatency, - evt->supTimeout, - clock_accuracy_t(evt->clockAccuracy) - ); - } - break; - + case DM_SCAN_REQ_RCVD_IND: { if (!handler) { break;