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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Connection between android-netsim and Zephyr BT Stack work arounds #220

Open
vChavezB opened this issue Jul 24, 2023 · 5 comments
Open

Connection between android-netsim and Zephyr BT Stack work arounds #220

vChavezB opened this issue Jul 24, 2023 · 5 comments

Comments

@vChavezB
Copy link

vChavezB commented Jul 24, 2023

As a follow upt to the issues I posted

#215
#217

I have managed to connect the Zephyr Bluetooth stack to the Android emulator with the bumble hci-bridge 馃憤

I open this issue to discuss the workarounds to make it work.

After some trial and error I figured out that the following commands from host to controller (zephyr tcp-client to android-netsim) should be filtered and not sent to the android-netsim.

  • HCI_SET_CONTROLLER_TO_HOST_FLOW_CONTROL_COMMAND
  • HCI_LE_CONNECTION_UPDATE_COMMAND
  • HCI_LE_SET_PHY_COMMAND
  • HCI_LE_READ_REMOTE_FEATURES_COMMAND
  • HCI_HOST_NUMBER_OF_COMPLETED_PACKETS_COMMAND

An example of how to make it work

  1. Make sure your Zephyr OS is updated to at least this commit

    zephyrproject-rtos/zephyr@33c922a

  2. Compile the BLE Hearbeat sensor sample from zephyr located here .

    For any other zephyr project be sure to set these configurations as follows

    • CONFIG_BT_HCI_ACL_FLOW_CONTROL=n
    • CONFIG_BT_SMP_SC_PAIR_ONLY=n
  3. Run the android emulator.

    emulator -avd Pixel_7_API_34 -packet-streamer-endpoint default

    Note: Only tested with API 34, other APIs <34 gave me some problems

  4. Create the hci-bridge with bumble and the filters for hci commands I mentioned above

    bumble-hci-bridge tcp-server:_:9000 android-netsim 0x03:0x0031,0x08:0x013,0x08:0x032,0x08:0x016,0x03:0x035

    update: If in zephyr you set CONFIG_BT_HCI_ACL_FLOW_CONTROL=n you do not need to filter the hci commands. I.e., you can run bumble-hci-bridge tcp-server:_:9000 android-netsim

  5. Run the zephyr binary

    zephyr.exe --bt-dev=127.0.0.1:9000

    Note: This instruction assume that zephyr binary has access to localhost. In case Android emulator and bumble are installed in Windows and using WSL to compile and execute the zephyr binary, then you need to get the proxy IP for localhost by running cat /etc/resolv.conf and obtaining the nameserver ip address.

And here a small video of the Proof of concept. Note I have not tested every option from the BT Zephyr stack or the APIs so this is still experimental.

Upper left screen is the HCI traffice between zephyr bluetooth stack (host) and the android-netsim (Controller). Lower window is the zephyr binary running the peripheral_hr example in the Windows Subsystem for Linux. And on the right is the Android emulator running with Android API 34 and the NRF Connect App to test the GATT notification of the peripheral_hr sample.

clideo_editor_bc0051d87a63439b9a0a10a32635c453.mp4
@barbibulle
Copy link
Collaborator

Nice demo!
For HCI_SET_CONTROLLER_TO_HOST_FLOW_CONTROL_COMMAND, that's now supported (just merged PR).
For the other ones, the netsim controller should definitely support them. I'm surprised you needed to filter them out. Did you get an error message or crash from netsimd without filtering?

@vChavezB
Copy link
Author

vChavezB commented Jul 25, 2023

Most of the errors I encountered where related to the controller from android not recognizing the commands. Did not check logcat though.

About the command HCI_SET_CONTROLLER_TO_HOST_FLOW_CONTROL_COMMAND

I get this error

INFO:bumble.bridge:[HOST->CONTROLLER] HCI_SET_CONTROLLER_TO_HOST_FLOW_CONTROL_COMMAND: 01
WARNING:bumble.transport.common:exception while waiting for packet: <AioRpcError of RPC that terminated with:
        status = StatusCode.UNKNOWN
        details = "Stream removed"
        debug_error_string = "UNKNOWN:Error received from peer ipv6:%5B::1%5D:52739 {grpc_message:"Stream removed", grpc_status:2, created_time:"2023-07-25T07:00:19.294477541+00:00"}"

Also I wanted to ask if its possible to make the tcp server send packets without delay. I noticed that sometimes several hci packets are packed in the same tcp payload. The Zephyr TCP client I wrote is simple and cannot decode multiple hci packets in the same payload.

Update: I found the part of the spec to decode HCI packets and modified it to parse correctly if multiple hci packets are in the same tcp payload. After decoding missing hci packets due to this issue, the following commands are the only ones that have to be filtered:

  • HCI_SET_CONTROLLER_TO_HOST_FLOW_CONTROL_COMMAND
  • HCI_HOST_NUMBER_OF_COMPLETED_PACKETS_COMMAND

python .\hci_bridge.py tcp-server:_:9000 android-netsim 0x03:0x0031,0x03:0x035

@barbibulle
Copy link
Collaborator

I'll check with the netsim team if they can add support for those HCI commands. In the meantime, filtering them with the HCI bridge seems like a good workaround.

@vChavezB
Copy link
Author

I figured a workaround to not have to filter the hci commands. Flow control can be disabled in the zephyr BT stack by setting the KCONFIG option CONFIG_BT_HCI_ACL_FLOW_CONTROL=n.

@vChavezB vChavezB changed the title Connection with Zephyr BT Stack work arounds Connection between android-netsim and Zephyr BT Stack work arounds Jul 30, 2023
@vChavezB
Copy link
Author

vChavezB commented Aug 1, 2023

Based on more tests the LE Extended commands are needed (when using the bumble virtual controller not android-netsim). According to this comment #229 (comment)

this will be fixed once this branch is merged https://github.com/google/bumble/tree/gbg/controller-extended-advertising

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants