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 cancel connect #13390

Merged
merged 1 commit into from
Aug 11, 2020
Merged

Conversation

paul-szczepanek-arm
Copy link
Member

Preceeding PR #13365

Summary of changes

The command in GAP cancelConnect writes to random memory due to incorrect usage of cordio state machine. Additionally cancel command has faulty API as it does not accept the peer address.

This fixes the current cancelConnect to remove the random memory write and also adds an overload that allows to cancel a selected target.

Impact of changes

Migration actions required

Documentation

None. Doxy updated.


Pull request type

[x] Patch update (Bug fix / Target update / Docs update / Test update / Refactor)
[] Feature update (New feature / Functionality change / New API)
[] Major update (Breaking change E.g. Return code change / API behaviour change)

Test results

[] No Tests required for this change (E.g docs only update)
[] Covered by existing mbed-os tests (Greentea or Unittest)
[x] Tests / results supplied as part of this PR

Reviewers

@LDong-Arm

@paul-szczepanek-arm
Copy link
Member Author

Tested with new tests in the new ble-tests suite:

============================= test session starts ==============================
platform linux -- Python 3.8.2, pytest-4.6.9, py-1.8.1, pluggy-0.13.0 -- /usr/bin/python3.8
cachedir: .pytest_cache
rootdir: /home/paul/gh/tests-paul/ble-tests, inifile: pytest.ini
plugins: mock-1.10.4, pep8-1.0.6
collecting ... collected 2 items

test_gap_connection.py::test_cancel_connect 
------------------------------ live log teardown -------------------------------
20:23:16.568   INFO   -->|peripheral| ble shutdown
20:23:16.573   INFO   <--|peripheral| {"status": 0}
20:23:16.574   INFO   <--|peripheral| retcode: 0
20:23:16.575   INFO   -->|peripheral| echo on
20:23:16.578   INFO   <--|peripheral| ECHO is on
20:23:16.579   INFO   <--|peripheral| retcode: 0
20:23:16.580   INFO   -->|peripheral| set --vt100 on
20:23:16.582   INFO   <--|peripheral| 
20:23:16.583   INFO   <--|peripheral| retcode: 0
20:23:16.584   INFO   Stopping "peripheral" runner...
20:23:16.584   INFO   -->|peripheral| set retcode false
20:23:16.618   INFO   <--|peripheral| />set retcode false
20:23:16.670   INFO   -->|central| ble shutdown
20:23:16.675   INFO   <--|central| {"status": 0}
20:23:16.676   INFO   <--|central| retcode: 0
20:23:16.676   INFO   -->|central| echo on
20:23:16.681   INFO   <--|central| ECHO is on
20:23:16.682   INFO   <--|central| retcode: 0
20:23:16.682   INFO   -->|central| set --vt100 on
20:23:16.686   INFO   <--|central| 
20:23:16.687   INFO   <--|central| retcode: 0
20:23:16.687   INFO   Stopping "central" runner...
20:23:16.688   INFO   -->|central| set retcode false
20:23:16.726   INFO   <--|central| />set retcode false

test_gap_connection.py::test_cancel_connect_too_late 
------------------------------ live log teardown -------------------------------
20:23:22.111   INFO   -->|peripheral| ble shutdown
20:23:22.116   INFO   <--|peripheral| {"status": 0}
20:23:22.117   INFO   <--|peripheral| retcode: 0
20:23:22.119   INFO   -->|peripheral| echo on
20:23:22.125   INFO   <--|peripheral| ECHO is on
20:23:22.129   INFO   <--|peripheral| retcode: 0
20:23:22.132   INFO   -->|peripheral| set --vt100 on
20:23:22.136   INFO   <--|peripheral| 
20:23:22.137   INFO   <--|peripheral| retcode: 0
20:23:22.138   INFO   Stopping "peripheral" runner...
20:23:22.139   INFO   -->|peripheral| set retcode false
20:23:22.182   INFO   <--|peripheral| />set retcode false
20:23:22.245   INFO   -->|central| ble shutdown
20:23:22.249   INFO   <--|central| {"status": 0}
20:23:22.251   INFO   <--|central| retcode: 0
20:23:22.251   INFO   -->|central| echo on
20:23:22.255   INFO   <--|central| ECHO is on
20:23:22.257   INFO   <--|central| retcode: 0
20:23:22.258   INFO   -->|central| set --vt100 on
20:23:22.262   INFO   <--|central| 
20:23:22.266   INFO   <--|central| retcode: 0
20:23:22.269   INFO   Stopping "central" runner...
20:23:22.269   INFO   -->|central| set retcode false
20:23:22.304   INFO   <--|central| />set retcode false


========================== 2 passed in 20.35 seconds ===========================

Process finished with exit code 0

-------------------------------- live log setup --------------------------------
20:23:02.076   INFO   Starting runner threads for "central"
20:23:02.469   INFO   <--|central| ARM Ltd
20:23:03.192   INFO   -->|central| set --retcode true
20:23:03.228   INFO   <--|central| />set --retcode true
20:23:03.229   INFO   <--|central| retcode: 0
20:23:03.230   INFO   -->|central| echo off
20:23:03.244   INFO   <--|central| />echo off
20:23:03.245   INFO   <--|central| ECHO is off
20:23:03.246   INFO   <--|central| retcode: 0
20:23:03.247   INFO   -->|central| set --vt100 off
20:23:03.251   INFO   <--|central| retcode: 0
20:23:03.252   INFO   -->|central| ble init
20:23:03.269   INFO   <--|central| {"status": 0}
20:23:03.270   INFO   <--|central| retcode: 0
20:23:03.274   INFO   Starting runner threads for "peripheral"
20:23:03.658   INFO   <--|peripheral| ARM Ltd
20:23:04.387   INFO   -->|peripheral| set --retcode true
20:23:04.425   INFO   <--|peripheral| />set --retcode true
20:23:04.426   INFO   <--|peripheral| retcode: 0
20:23:04.429   INFO   -->|peripheral| echo off
20:23:04.442   INFO   <--|peripheral| />echo off
20:23:04.444   INFO   <--|peripheral| ECHO is off
20:23:04.445   INFO   <--|peripheral| retcode: 0
20:23:04.445   INFO   -->|peripheral| set --vt100 off
20:23:04.449   INFO   <--|peripheral| retcode: 0
20:23:04.450   INFO   -->|peripheral| ble init
20:23:04.467   INFO   <--|peripheral| {"status": 0}
20:23:04.469   INFO   <--|peripheral| retcode: 0
-------------------------------- live log call ---------------------------------
20:23:04.470   INFO   -->|peripheral| gap getAddress
20:23:04.480   INFO   <--|peripheral| {"status": 0,"result": {"address_type": "RANDOM","address": "D8:E2:FB:7F:0A:A8"}}
20:23:04.481   INFO   <--|peripheral| retcode: 0
20:23:04.481   INFO   -->|central| gap startConnecting RANDOM D8:E2:FB:7F:0A:A8
20:23:04.489   INFO   <--|central| {"status": 0}
20:23:04.490   INFO   <--|central| retcode: 0
20:23:05.491   INFO   -->|central| gap cancelConnect RANDOM D8:E2:FB:7F:0A:A8
20:23:05.500   INFO   <--|central| {"status": 0}
20:23:05.501   INFO   <--|central| retcode: 0
20:23:06.503   INFO   -->|peripheral| gap waitForConnection 10000
20:23:07.503   INFO   peripheral: Waiting for "retcode: -1" string... Timeout in 29 s
20:23:07.508   INFO   <--|peripheral| {
20:23:08.509   INFO   peripheral: Waiting for "retcode: -1" string... Timeout in 28 s
20:23:09.510   INFO   peripheral: Waiting for "retcode: -1" string... Timeout in 27 s
20:23:10.513   INFO   peripheral: Waiting for "retcode: -1" string... Timeout in 26 s
20:23:11.513   INFO   peripheral: Waiting for "retcode: -1" string... Timeout in 25 s
20:23:12.514   INFO   peripheral: Waiting for "retcode: -1" string... Timeout in 24 s
20:23:13.514   INFO   peripheral: Waiting for "retcode: -1" string... Timeout in 23 s
20:23:14.515   INFO   peripheral: Waiting for "retcode: -1" string... Timeout in 22 s
20:23:15.515   INFO   peripheral: Waiting for "retcode: -1" string... Timeout in 21 s
20:23:16.511   INFO   <--|peripheral| "status": -1,"error": "timeout"}
20:23:16.512   INFO   <--|peripheral| retcode: -1
20:23:16.513   INFO   -->|peripheral| advDataBuilder clear
20:23:16.518   INFO   <--|peripheral| {"status": 0}
20:23:16.519   INFO   <--|peripheral| retcode: 0
20:23:16.519   INFO   -->|peripheral| advDataBuilder setFlags LE_GENERAL_DISCOVERABLE
20:23:16.527   INFO   <--|peripheral| {"status": 0}
20:23:16.528   INFO   <--|peripheral| retcode: 0
20:23:16.528   INFO   -->|peripheral| advDataBuilder setFlags BREDR_NOT_SUPPORTED
20:23:16.535   INFO   <--|peripheral| {"status": 0}
20:23:16.536   INFO   <--|peripheral| retcode: 0
20:23:16.537   INFO   -->|peripheral| gap applyAdvPayloadFromBuilder 0
20:23:16.543   INFO   <--|peripheral| {"status": 0}
20:23:16.544   INFO   <--|peripheral| retcode: 0
20:23:16.545   INFO   -->|peripheral| advParams setType CONNECTABLE_UNDIRECTED
20:23:16.552   INFO   <--|peripheral| {"status": 0}
20:23:16.553   INFO   <--|peripheral| retcode: 0
20:23:16.553   INFO   -->|peripheral| gap setAdvertisingParameters 0
20:23:16.559   INFO   <--|peripheral| {"status": 0}
20:23:16.560   INFO   <--|peripheral| retcode: 0
20:23:16.560   INFO   -->|peripheral| gap startAdvertising 0 0 0
20:23:16.565   INFO   <--|peripheral| {"status": 0}
20:23:16.567   INFO   <--|peripheral| retcode: 0
PASSED                                                                   [ 50%]
-------------------------------- live log setup --------------------------------
20:23:16.787   INFO   Starting runner threads for "central"
20:23:17.306   INFO   <--|central| ARM Ltd
20:23:17.903   INFO   -->|central| set --retcode true
20:23:17.941   INFO   <--|central| />set --retcode true
20:23:17.942   INFO   <--|central| retcode: 0
20:23:17.942   INFO   -->|central| echo off
20:23:17.956   INFO   <--|central| />echo off
20:23:17.959   INFO   <--|central| ECHO is off
20:23:17.960   INFO   <--|central| retcode: 0
20:23:17.960   INFO   -->|central| set --vt100 off
20:23:17.965   INFO   <--|central| retcode: 0
20:23:17.965   INFO   -->|central| ble init
20:23:17.982   INFO   <--|central| {"status": 0}
20:23:17.984   INFO   <--|central| retcode: 0
20:23:17.985   INFO   Starting runner threads for "peripheral"
20:23:18.511   INFO   <--|peripheral| ARM Ltd
20:23:19.103   INFO   -->|peripheral| set --retcode true
20:23:19.140   INFO   <--|peripheral| />set --retcode true
20:23:19.141   INFO   <--|peripheral| retcode: 0
20:23:19.141   INFO   -->|peripheral| echo off
20:23:19.155   INFO   <--|peripheral| />echo off
20:23:19.156   INFO   <--|peripheral| ECHO is off
20:23:19.157   INFO   <--|peripheral| retcode: 0
20:23:19.158   INFO   -->|peripheral| set --vt100 off
20:23:19.163   INFO   <--|peripheral| retcode: 0
20:23:19.164   INFO   -->|peripheral| ble init
20:23:19.182   INFO   <--|peripheral| {"status": 0}
20:23:19.183   INFO   <--|peripheral| retcode: 0
-------------------------------- live log call ---------------------------------
20:23:19.184   INFO   -->|peripheral| gap getAddress
20:23:19.194   INFO   <--|peripheral| {"status": 0,"result": {"address_type": "RANDOM","address": "D8:E2:FB:7F:0A:A8"}}
20:23:19.195   INFO   <--|peripheral| retcode: 0
20:23:19.196   INFO   -->|peripheral| advDataBuilder clear
20:23:19.201   INFO   <--|peripheral| {"status": 0}
20:23:19.207   INFO   <--|peripheral| retcode: 0
20:23:19.207   INFO   -->|peripheral| advDataBuilder setFlags LE_GENERAL_DISCOVERABLE
20:23:19.215   INFO   <--|peripheral| {"status": 0}
20:23:19.216   INFO   <--|peripheral| retcode: 0
20:23:19.216   INFO   -->|peripheral| advDataBuilder setFlags BREDR_NOT_SUPPORTED
20:23:19.223   INFO   <--|peripheral| {"status": 0}
20:23:19.224   INFO   <--|peripheral| retcode: 0
20:23:19.225   INFO   -->|peripheral| gap applyAdvPayloadFromBuilder 0
20:23:19.231   INFO   <--|peripheral| {"status": 0}
20:23:19.232   INFO   <--|peripheral| retcode: 0
20:23:19.233   INFO   -->|peripheral| advParams setType CONNECTABLE_UNDIRECTED
20:23:19.240   INFO   <--|peripheral| {"status": 0}
20:23:19.241   INFO   <--|peripheral| retcode: 0
20:23:19.241   INFO   -->|peripheral| gap setAdvertisingParameters 0
20:23:19.247   INFO   <--|peripheral| {"status": 0}
20:23:19.248   INFO   <--|peripheral| retcode: 0
20:23:19.248   INFO   -->|peripheral| gap getAddress
20:23:19.258   INFO   <--|peripheral| {"status": 0,"result": {"address_type": "RANDOM","address": "D8:E2:FB:7F:0A:A8"}}
20:23:19.259   INFO   <--|peripheral| retcode: 0
20:23:19.260   INFO   -->|peripheral| gap startAdvertising 0 0 0
20:23:19.266   INFO   <--|peripheral| {"status": 0}
20:23:19.267   INFO   <--|peripheral| retcode: 0
20:23:19.268   INFO   -->|central| gap connect RANDOM D8:E2:FB:7F:0A:A8
20:23:19.268   INFO   -->|peripheral| gap waitForConnection 10000
20:23:19.937   INFO   <--|central| {"status": 0,"result": {"status": "BLE_ERROR_NONE","peer_address_type": "RANDOM","peer_address": "D8:E2:FB:7F:0A:A8","interval": 56,"latency": 0,"supervision_timeout": 100,"connection_handle": 1,"own_role": "CENTRAL","master_clock_accuracy": 0}}
20:23:19.937   INFO   <--|peripheral| {"status": 0,"result": {"status": "BLE_ERROR_NONE","peer_address_type": "RANDOM","peer_address": "D3:A6:95:6A:3B:56","interval": 56,"latency": 0,"supervision_timeout": 100,"connection_handle": 1,"own_role": "PERIPHERAL","master_clock_accuracy": 0}}
20:23:19.938   INFO   <--|central| retcode: 0
20:23:19.939   INFO   <--|peripheral| retcode: 0
20:23:19.954   INFO   <--|peripheral| <<< {"type": "event","name": "advertising_end","value": {"advertising_handle": 0,"completed_events": 0,"is_connected": true,"connection_handle": 0}}
20:23:20.039   INFO   -->|central| gap cancelConnect RANDOM D8:E2:FB:7F:0A:A8
20:23:20.047   INFO   <--|central| {"status": 0}
20:23:20.048   INFO   <--|central| retcode: 0
20:23:22.052   INFO   -->|central| gap disconnect 1 USER_TERMINATION
20:23:22.053   INFO   -->|peripheral| gap waitForDisconnection 10000
20:23:22.068   INFO   <--|central| {"status": 0}
20:23:22.068   INFO   <--|central| retcode: 0
20:23:22.100   INFO   <--|peripheral| {"status": 0,"result": {"connection_handle": 1,"reason": "REMOTE_USER_TERMINATED_CONNECTION"}}
20:23:22.102   INFO   <--|peripheral| retcode: 0
20:23:22.104   INFO   <--|central| <<< {"type": "event","name": "disconnection_complete","value": {"connection_handle": 1,"reason": "LOCAL_HOST_TERMINATED_CONNECTION"}}
PASSED                                                                   [100%]

@mergify mergify bot added the needs: work label Aug 3, 2020
@ciarmcom ciarmcom added the release-type: patch Indentifies a PR as containing just a patch label Aug 3, 2020
@ciarmcom ciarmcom requested review from LDong-Arm and a team August 3, 2020 21:00
@ciarmcom
Copy link
Member

ciarmcom commented Aug 3, 2020

@paul-szczepanek-arm, thank you for your changes.
@LDong-Arm @ARMmbed/mbed-os-maintainers please review.

}

if (connection_id < DM_CONN_MAX) {
connection_id++;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is it incremented?

connectivity/FEATURE_BLE/include/ble/Gap.h Show resolved Hide resolved
Comment on lines 344 to 351
DmConnClose(
DM_CLIENT_ID_APP,
/* connection handle */ connection_id,
/* reason - invalid (use success) */ 0x00
);
}

return BLE_ERROR_NONE;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The DmConnClose call can be nested into the for loop so we don't need to check DM_CONN_MAX twice. In the loop, return BLE_ERROR_NONE after DmConnClose. If we reach the end, no address matches so we could return some kind of error.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, changed as requested

Comment on lines 637 to 639
if (_last_used_peer_address_type != peer_address_type_t::ANONYMOUS) {
return cancelConnect(_last_used_peer_address_type, _last_used_peer_address);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that if the previous connection got disconnected or didn't succeed, _last_used_peer_address_type, _last_used_peer_address still have the old values. Don't know if it's worth resetting them somewhere - the legacy cancelConnect() is deprecated and may not be worth complicating.

Anyway the if condition has little value for that reason in my opinion.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The if checks if it has been set, otherwise I'd be checking random addresses. The cancel connect does nothing if there isn't an ongoing connection attempt for the address.

Copy link
Contributor

@LDong-Arm LDong-Arm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, waiting #13365

@paul-szczepanek-arm
Copy link
Member Author

had to force push to rebase on preceding PR

@paul-szczepanek-arm paul-szczepanek-arm force-pushed the fix-cancel-connect branch 3 times, most recently from df89b69 to a9fd028 Compare August 7, 2020 11:09
@paul-szczepanek-arm paul-szczepanek-arm force-pushed the fix-cancel-connect branch 2 times, most recently from a14d513 to 7a539f5 Compare August 7, 2020 13:56
@paul-szczepanek-arm
Copy link
Member Author

this is good for CI

@paul-szczepanek-arm
Copy link
Member Author

wait, this is showing up as all wrong, I need to fix the base

@mergify
Copy link

mergify bot commented Aug 10, 2020

This PR cannot be merged due to conflicts. Please rebase to resolve them.

@paul-szczepanek-arm
Copy link
Member Author

paul-szczepanek-arm commented Aug 10, 2020

I apologise, this is now rebased correctly and good to go

@paul-szczepanek-arm
Copy link
Member Author

Fixed the brace on newline

@0xc0170
Copy link
Contributor

0xc0170 commented Aug 11, 2020

CI started

@mbed-ci
Copy link

mbed-ci commented Aug 11, 2020

Jenkins CI Test : ✔️ SUCCESS

Build Number: 1 | 🔒 Jenkins CI Job | 🌐 Logs & Artifacts

CLICK for Detailed Summary

jobs Status
jenkins-ci/mbed-os-ci_unittests ✔️
jenkins-ci/mbed-os-ci_build-ARM ✔️
jenkins-ci/mbed-os-ci_build-GCC_ARM ✔️
jenkins-ci/mbed-os-ci_greentea-test ✔️
jenkins-ci/mbed-os-ci_dynamic-memory-usage ✔️
jenkins-ci/mbed-os-ci_cloud-client-pytest ✔️

Copy link
Member

@pan- pan- left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@paul-szczepanek-arm

Thank you for the fix Paul but I have difficulties to understand why the public facing API and pal API were changed. At the HCI level there is no address asked to cancel the process that creates a connection everything is canceled.

Can you revert the user facing and PAL API change. The only change needed should be in the PAL implementation.

@@ -1002,10 +1002,30 @@ class Gap {
/** Cancel the connection attempt. This is not guaranteed to succeed. As a result
* onConnectionComplete in the event handler will be called. Check the success parameter
* to see if the connection was created.
* @depreacted This version has a defective API. You must provide the address of the peer.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo: depreacted -> deprecated.

@paul-szczepanek-arm
Copy link
Member Author

Cordio requires the ble address to identify the control block.

This is a merged PR. You might want to just talk to me over slack.

@paul-szczepanek-arm
Copy link
Member Author

#13462

@mbedmain mbedmain removed release-type: patch Indentifies a PR as containing just a patch Release-pending labels Aug 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants