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

esp_prov: switch from bluez/dbus to bleak (IDFGH-7556) #9117

Closed
wants to merge 4 commits into from

Conversation

ccutrer
Copy link
Contributor

@ccutrer ccutrer commented Jun 7, 2022

to enable multiplatform BLE compatibility

tested on MacOS 12.4 and Ubuntu 20.04

also includes several discrete commits to allow interactive provisioning if necessary data isn't provided on command line.

@CLAassistant
Copy link

CLAassistant commented Jun 7, 2022

CLA assistant check
All committers have signed the CLA.

@espressif-bot espressif-bot added the Status: Opened Issue is new label Jun 7, 2022
@github-actions github-actions bot changed the title esp_prov: switch from bluez/dbus to bleak esp_prov: switch from bluez/dbus to bleak (IDFGH-7556) Jun 7, 2022
@ccutrer ccutrer force-pushed the esp_prov_bleak branch 2 times, most recently from fcb3f81 to 8112f67 Compare June 7, 2022 22:56
@Alvin1Zhang
Copy link
Collaborator

Thanks for your contribution.

@ccutrer
Copy link
Contributor Author

ccutrer commented Jun 8, 2022

I updated my commits - I had installed the pre-commit hook halfway through, but apparently git commit --amend doesn't re-run the hooks when there are no other changes, so it ran clean. I had to undo the commit and re-stage them so it would run against all of the changes.

@mahavirj
Copy link
Member

mahavirj commented Jun 9, 2022

@ccutrer Thank you for sending this PR. It looks mighty useful, we are performing some tests on this and then we shall queue it for internal merge.

@HiFiPhile
Copy link
Contributor

Tested working on Archlinux but failed on Windows 10 with error Protocol Error 0x05: Insufficient Authentication

Full log:

2022-06-13 16:28:57,669 bleak.backends.winrt.scanner DEBUG: Received 4A:5D:63:68:75:8F: Unknown.
2022-06-13 16:28:57,673 bleak.backends.winrt.scanner DEBUG: Received 27:E0:BB:A9:65:36: Unknown.
2022-06-13 16:28:57,739 bleak.backends.winrt.scanner DEBUG: 21 devices found. Watcher status: 3.
Connecting...
2022-06-13 16:28:57,778 bleak.backends.winrt.scanner DEBUG: Received 07:FE:CB:51:63:05: Unknown.
2022-06-13 16:28:57,783 bleak.backends.winrt.scanner DEBUG: Received 6E:97:48:68:7F:95: Unknown.
2022-06-13 16:28:57,784 bleak.backends.winrt.scanner DEBUG: Received 6E:97:48:68:7F:95: Unknown.
2022-06-13 16:28:57,896 bleak.backends.winrt.scanner DEBUG: Received 63:30:F1:98:32:00: Unknown.
2022-06-13 16:28:58,020 bleak.backends.winrt.scanner DEBUG: Received E7:5A:F5:6F:17:3D: Unknown.
2022-06-13 16:28:58,267 bleak.backends.winrt.scanner DEBUG: Received 62:B3:8E:1E:3D:0E: Unknown.
2022-06-13 16:28:58,268 bleak.backends.winrt.scanner DEBUG: Received 62:B3:8E:1E:3D:0E: Unknown.
2022-06-13 16:28:58,364 bleak.backends.winrt.scanner DEBUG: Received 7C:DF:A1:D1:D3:D6: Unknown.
2022-06-13 16:28:58,366 bleak.backends.winrt.scanner DEBUG: 6 devices found. Watcher status: 3.
2022-06-13 16:28:58,366 bleak.backends.winrt.client DEBUG: Connecting to BLE device @ 7C:DF:A1:D1:D3:D6
2022-06-13 16:29:01,156 bleak.backends.winrt.client DEBUG: session_status_changed_event_handler: id: <_bleak_winrt_Windows_Devices_Bluetooth.BluetoothDeviceId object at 0x0000000003BDFC50>, error: 0, status: 1
2022-06-13 16:29:01,156 bleak.backends.winrt.client DEBUG: Get Services...
2022-06-13 16:29:03,082 bleak.backends.winrt.client INFO: Services resolved for BleakClientWinRT (7C:DF:A1:D1:D3:D6)
Getting Services...
2022-06-13 16:29:03,202 bleak.backends.winrt.client DEBUG: Read Descriptor 0004 : bytearray(b'prov-scan')
2022-06-13 16:29:03,321 bleak.backends.winrt.client DEBUG: Read Descriptor 0007 : bytearray(b'prov-session')
2022-06-13 16:29:03,442 bleak.backends.winrt.client DEBUG: Read Descriptor 000A : bytearray(b'prov-config')
2022-06-13 16:29:03,562 bleak.backends.winrt.client DEBUG: Read Descriptor 000D : bytearray(b'proto-ver')
2022-06-13 16:29:03,682 bleak.backends.winrt.client DEBUG: Read Descriptor 0010 : bytearray(b'config')
Disconnecting...
Traceback (most recent call last):
  File "D:\mcu\esp-idf\tools\esp_prov\esp_prov.py", line 478, in <module>
    asyncio.run(main())
  File "C:\Program Files\Python39\lib\asyncio\runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "C:\Program Files\Python39\lib\asyncio\base_events.py", line 642, in run_until_complete
    return future.result()
  File "D:\mcu\esp-idf\tools\esp_prov\esp_prov.py", line 374, in main
    if not await has_capability(obj_transport):
  File "D:\mcu\esp-idf\tools\esp_prov\esp_prov.py", line 112, in has_capability
    response = await tp.send_data('proto-ver', capability)
  File "D:\mcu\esp-idf\tools\esp_prov\transport\transport_ble.py", line 51, in send_data
    return await self.cli.send_data(self.name_uuid_lookup[ep_name], data)
  File "D:\mcu\esp-idf\tools\esp_prov\transport\ble_cli.py", line 153, in send_data
    await self.device.write_gatt_char(characteristic_uuid, bytearray(data.encode('latin-1')), True)
  File "C:\Tool\esp-idf\python_env\idf5.0_py3.9_env\lib\site-packages\bleak\backends\winrt\client.py", line 617, in write_gatt_char
    _ensure_success(
  File "C:\Tool\esp-idf\python_env\idf5.0_py3.9_env\lib\site-packages\bleak\backends\winrt\client.py", line 97, in _ensure_success
    raise BleakError(
bleak.exc.BleakError: Could not write value bytearray(b'none') to characteristic 000B: Protocol Error 0x05: Insufficient Authentication

@ccutrer
Copy link
Contributor Author

ccutrer commented Jun 13, 2022

Apparently Windows requires pairing. Tested working on Windows 11 and Windows 10.

@HiFiPhile
Copy link
Contributor

Apparently Windows requires pairing. Tested working on Windows 11 and Windows 10.

Yes f8ba16f is working on Windows 10.

print('{0: >4} {1: <33} {2: <12}'.format(
'S.N.', 'Name', 'Address'))
for i in range(len(devices)):
print('[{0: >2}] {1: <33} {2: <12}'.format(i + 1, devices[i].name or 'Unknown', devices[i].address))
Copy link
Contributor

@HiFiPhile HiFiPhile Jun 14, 2022

Choose a reason for hiding this comment

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

devices[i].name need more handling.

On windows some device name have a bytearray part which cause following error:

==== BLE Discovery results ====
S.N. Name                              Address
[ 1] Linkify_00000A                    84:F7:03:33:A3:EE
Traceback (most recent call last):
  File "E:\mcu\esp-idf/tools/esp_prov\esp_prov.py", line 524, in <module>
    asyncio.run(main())
  File "C:\Program Files\Python39\lib\asyncio\runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "C:\Program Files\Python39\lib\asyncio\base_events.py", line 642, in run_until_complete
    return future.result()
  File "E:\mcu\esp-idf/tools/esp_prov\esp_prov.py", line 397, in main
    obj_transport = await get_transport(args.mode.lower(), args.name)
  File "E:\mcu\esp-idf/tools/esp_prov\esp_prov.py", line 70, in get_transport
    await tp.connect(devname=service_name)
  File "E:\mcu\esp-idf/tools/esp_prov\transport\transport_ble.py", line 26, in connect
    if not await self.cli.connect(devname=devname, iface='hci0',
  File "E:\mcu\esp-idf/tools/esp_prov\transport\ble_cli.py", line 66, in connect
    print('[{0: >2}] {1: <33} {2: <12}'.format(i + 1, devices[i].name, devices[i].address))
TypeError: unsupported format string passed to NoneType.__format__

If I add

            for i in range(len(devices)):
                print(devices[i])

I got :

88:C6:26:B1:43:21: IBM Corp. (b'\x00~Q\x00\x00\x0bT3\xcb\x9b\xa8\x8b\x88\xc6&\xb1C!\x12')
90:DD:5D:D4:93:A7: Apple, Inc. (b'\x0f\x08\xc0\n\x86b\xb1\x00\x04\x0b\x10\x02\x01\x00')
5B:41:61:6C:CE:C6: Apple, Inc. (b'\x10\x06W\x1e\xb8\x1d\xf3K')
21:E1:31:80:D3:00: Apple, Inc. (b'\t\x06\x03C\xc0\xa8\x01a')
F4:98:29:D7:7A:36: Apple, Inc. (b'\x12\x02\x00\x03')
84:F7:03:33:A3:EE: Linkify_00000A
15:11:FB:F5:8A:08: Apple, Inc. (b'\t\x06\x03A\xc0\xa8\x01\x18')

Copy link
Contributor Author

Choose a reason for hiding this comment

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

are you sure you're running the latest commit? I pushed up a fix for unsupported format string passed to NoneType.__format__ (adding or 'Unknown') right around the time you commented that it's working on Windows 10. Assuming you are, would the fix be to change it to str(device[I].name or 'Unknown')? I'm a relative novice in Python.

Copy link
Contributor

Choose a reason for hiding this comment

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

are you sure you're running the latest commit?

You are right, it's already fixed in last commit.

@espressif-bot espressif-bot added Status: In Progress Work is in progress and removed Status: Opened Issue is new labels Jun 20, 2022
@laukik-hase
Copy link
Collaborator

laukik-hase commented Jun 21, 2022

Hello, @ccutrer!

Thank you for your contribution! Could you please rebase your PR with the latest master (c2ccc38) and resolve the merge conflicts?

@ccutrer
Copy link
Contributor Author

ccutrer commented Jun 21, 2022

@laukik-hase: done.

@laukik-hase
Copy link
Collaborator

sha=265ad3eea657fb3d4f064fbb49132ff8b128d1b8

@laukik-hase laukik-hase added the PR-Sync-Merge Pull request sync as merge commit label Jun 22, 2022
@espressif-bot espressif-bot added the Resolution: NA Issue resolution is unavailable label Jun 24, 2022
@espressif-bot espressif-bot added Status: Done Issue is done internally Resolution: Done Issue is done internally and removed Status: In Progress Work is in progress Resolution: NA Issue resolution is unavailable labels Jun 24, 2022
@laukik-hase
Copy link
Collaborator

Merged with 3f77c65

@laukik-hase laukik-hase closed this Jul 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
PR-Sync-Merge Pull request sync as merge commit Resolution: Done Issue is done internally Status: Done Issue is done internally
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants