Skip to content

Commit 3b25d2d

Browse files
authored
Merge pull request ARMmbed#13390 from paul-szczepanek-arm/fix-cancel-connect
Fix cancel connect
2 parents c7316dc + e877eb7 commit 3b25d2d

File tree

6 files changed

+80
-11
lines changed

6 files changed

+80
-11
lines changed

connectivity/FEATURE_BLE/include/ble/Gap.h

+20
Original file line numberDiff line numberDiff line change
@@ -1002,10 +1002,30 @@ class Gap {
10021002
/** Cancel the connection attempt. This is not guaranteed to succeed. As a result
10031003
* onConnectionComplete in the event handler will be called. Check the success parameter
10041004
* to see if the connection was created.
1005+
* @depreacted This version has a defective API. You must provide the address of the peer.
1006+
* Please use the cancelConnect that takes peer address as parameters.
1007+
* This call will attempt to cancel the most recently requested connection.
10051008
*
10061009
* @return BLE_ERROR_NONE if the connection attempt has been requested to be cancelled.
1010+
* Returns BLE_ERROR_OPERATION_NOT_PERMITTED if no ongoing connection for last used address found.
10071011
*/
1012+
MBED_DEPRECATED_SINCE("mbed-os-6.3.0", "Defective API. Please use the cancelConnect that takes peer address as parameters.")
10081013
ble_error_t cancelConnect();
1014+
1015+
/** Cancel the connection attempt. This is not guaranteed to succeed. As a result
1016+
* onConnectionComplete in the event handler will be called. Check the success parameter
1017+
* to see if the connection was created.
1018+
*
1019+
* @param peerAddressType Address type you want to cancel connection process for.
1020+
* @param peerAddress Address you want to cancel connection process for.
1021+
*
1022+
* @return BLE_ERROR_NONE if the connection attempt has been requested to be cancelled.
1023+
* Returns BLE_ERROR_OPERATION_NOT_PERMITTED if no ongoing connection for address found.
1024+
*/
1025+
ble_error_t cancelConnect(
1026+
peer_address_type_t peerAddressType,
1027+
const address_t &peerAddress
1028+
);
10091029
#endif // BLE_ROLE_CENTRAL
10101030

10111031
#if BLE_FEATURE_CONNECTABLE

connectivity/FEATURE_BLE/include/ble/internal/PalGap.h

+10-1
Original file line numberDiff line numberDiff line change
@@ -1298,13 +1298,22 @@ class PalGap {
12981298
/**
12991299
* Cancel the ongoing connection creation process.
13001300
*
1301+
* @param peer_address_type Type of address used by the advertiser. Not used
1302+
* if initiator_policy use the whitelist.
1303+
*
1304+
* @param Address used by the advertiser in its advertising packets. Not
1305+
* used if initiator_policy use the whitelist.
1306+
*
13011307
* @return BLE_ERROR_NONE if the request has been successfully sent or the
13021308
* appropriate error otherwise.
13031309
*
13041310
* @note: See Bluetooth 5 Vol 2 PartE: 7.8.13 LE create connection cancel
13051311
* command.
13061312
*/
1307-
virtual ble_error_t cancel_connection_creation() = 0;
1313+
virtual ble_error_t cancel_connection_creation(
1314+
peer_address_type_t peerAddressType,
1315+
const ble::address_t &peerAddress
1316+
) = 0;
13081317

13091318
/**
13101319
* Return the number of total whitelist entries that can be stored in the

connectivity/FEATURE_BLE/libraries/TARGET_CORDIO/include/ble/internal/PalGapImpl.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,10 @@ class PalGap : public interface::PalGap {
9797
uint16_t maximum_connection_event_length
9898
);
9999

100-
ble_error_t cancel_connection_creation();
100+
ble_error_t cancel_connection_creation(
101+
peer_address_type_t peerAddressType,
102+
const ble::address_t &peerAddress
103+
);
101104

102105
uint8_t read_white_list_capacity();
103106

connectivity/FEATURE_BLE/libraries/TARGET_CORDIO/source/PalGapImpl.cpp

+20-7
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
#include "PalGap.h"
1919
#include "hci_api.h"
2020
#include "dm_api.h"
21+
#include "dm_main.h"
22+
#include "dm_conn.h"
2123

2224
namespace ble {
2325

@@ -320,14 +322,25 @@ ble_error_t PalGap::create_connection(
320322
}
321323

322324

323-
ble_error_t PalGap::cancel_connection_creation()
325+
ble_error_t PalGap::cancel_connection_creation(
326+
peer_address_type_t peerAddressType,
327+
const ble::address_t &peerAddress
328+
)
324329
{
325-
DmConnClose(
326-
DM_CLIENT_ID_APP,
327-
/* connection handle - invalid */ DM_CONN_ID_NONE,
328-
/* reason - invalid (use success) */ 0x00
329-
);
330-
return BLE_ERROR_NONE;
330+
for (uint8_t connection_id = 0; connection_id < DM_CONN_MAX; connection_id++) {
331+
if (dmConnCb.ccb[connection_id].inUse &&
332+
dmConnCb.ccb[connection_id].state == DM_CONN_SM_ST_CONNECTING &&
333+
BdaCmp(dmConnCb.ccb[connection_id].peerAddr, peerAddress.data())) {
334+
DmConnClose(
335+
DM_CLIENT_ID_APP,
336+
/* connection handle */ connection_id + 1 /* connection IDs start at 1 */,
337+
/* reason - invalid (use success) */ 0x00
338+
);
339+
return BLE_ERROR_NONE;
340+
}
341+
}
342+
343+
return BLE_ERROR_OPERATION_NOT_PERMITTED;
331344
}
332345

333346

connectivity/FEATURE_BLE/libraries/ble-api-implementation/include/ble/internal/GapImpl.h

+9
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,11 @@ class Gap :
209209
const ConnectionParameters &connectionParams
210210
);
211211

212+
ble_error_t cancelConnect(
213+
peer_address_type_t peerAddressType,
214+
const address_t &peerAddress
215+
);
216+
212217
ble_error_t cancelConnect();
213218
#endif // BLE_ROLE_CENTRAL
214219

@@ -596,6 +601,10 @@ class Gap :
596601
BitArray<BLE_GAP_MAX_ADVERTISING_SETS> _set_is_connectable;
597602

598603
bool _user_manage_connection_parameter_requests : 1;
604+
605+
/* these need be removed when the deprecated cancelConnect() is removed */
606+
peer_address_type_t _last_used_peer_address_type = peer_address_type_t::ANONYMOUS;
607+
ble::address_t _last_used_peer_address;
599608
};
600609

601610
} // namespace ble

connectivity/FEATURE_BLE/libraries/ble-api-implementation/source/GapImpl.cpp

+17-2
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,9 @@ ble_error_t Gap::connect(
507507
return BLE_ERROR_INVALID_PARAM;
508508
}
509509

510+
_last_used_peer_address_type = peerAddressType;
511+
_last_used_peer_address = peerAddress;
512+
510513
// ensure scan is stopped.
511514
_pal_gap.scan_enable(false, false);
512515

@@ -538,6 +541,9 @@ ble_error_t Gap::connect(
538541
adjusted_address_type = peer_address_type_t::RANDOM;
539542
}
540543

544+
_last_used_peer_address_type = adjusted_address_type;
545+
_last_used_peer_address = peerAddress;
546+
541547
return _pal_gap.extended_create_connection(
542548
connectionParams.getFilter(),
543549
connectionParams.getOwnAddressType(),
@@ -625,12 +631,21 @@ ble_error_t Gap::rejectConnectionParametersUpdate(
625631
);
626632
}
627633

628-
629634
ble_error_t Gap::cancelConnect()
630635
{
631-
return _pal_gap.cancel_connection_creation();
636+
if (_last_used_peer_address_type != peer_address_type_t::ANONYMOUS) {
637+
return cancelConnect(_last_used_peer_address_type, _last_used_peer_address);
638+
}
639+
return BLE_ERROR_OPERATION_NOT_PERMITTED;
632640
}
633641

642+
ble_error_t Gap::cancelConnect(
643+
peer_address_type_t peerAddressType,
644+
const ble::address_t &peerAddress
645+
)
646+
{
647+
return _pal_gap.cancel_connection_creation(peerAddressType, peerAddress);
648+
}
634649

635650
ble_error_t Gap::readPhy(ble::connection_handle_t connection)
636651
{

0 commit comments

Comments
 (0)