espressif / esp-idf

Espressif IoT Development Framework. Official development framework for Espressif SoCs.
Apache License 2.0
13.46k stars 7.25k forks source link

ble_gattc_disc_all_svcs() fails on ESP32S3 with several peripherals (IDFGH-8664) #10109

Closed kojibuta closed 1 year ago

kojibuta commented 1 year ago

Answers checklist.

IDF version.

ESP-IDF v4.4.2-388-g755ce1077d

Operating System used.

macOS

How did you build your project?

Command line with idf.py

If you are using Windows, please specify command line type.

No response

Development Kit.

Custom board

Power Supply used.

USB

What is the expected behavior?

When using NimBLE (BLE central role) on ESP32-S3 it is not possible to discover services on some peripheral devices. The same code runs smoothly on ESP32 without errors.

After connection is established with a peripheral (event BLE_GAP_EVENT_CONNECT received with event->connect.status == 0) calling ble_gattc_disc_all_svcs should result in invokation of callback function ble_gatt_disc_svc_fn with error->status == 0.

What is the actual behavior?

When running the code on ESP32-S3, after connection is established with a peripheral, calling ble_gattc_disc_all_svcs results in immediate invokation of callback function ble_gatt_disc_svc_fn with error->status == 7.

Steps to reproduce.

  1. Flash central test_app to a ESP32-S3 board
  2. Power on the board
  3. Start monitoring the log
  4. Power on the peripheral
  5. Error appears on the log and service discovery fails
  6. Flash central test_app to a ESP32 board
  7. Power on the board
  8. Start monitoring the log
  9. Power on the peripheral
  10. Service discovery is successful and no error appears on the log

ESP32-S3 logs (SleepO2 device OK, B02T device KO) ESP32-S3 board + SleepO2 device: esp32-s3-peripheral-1.log ESP32 board + B02T device: esp32-s3-peripheral-2.log

ESP32 logs (SleepO2 and B02T device OK) ESP32 board + SleepO2 device: esp32-peripheral-1.log ESP32 board + B02T device: esp32-peripheral-2.log

Debug Logs.

--- idf_monitor on /dev/cu.usbserial-120 115200 ---
--- Quit: Ctrl+] | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H ---
ESP-ROM:esp32s3-20210327
Build:Mar 27 2021
rst:0x1 (POWERON),boot:0x8 (SPI_FAST_FLASH_BOOT)
SPIWP:0xee
mode:DIO, clock div:1
load:0x3fce3808,len:0x1670
load:0x403c9700,len:0xbb8
load:0x403cc700,len:0x2e8c
entry 0x403c9954
I (24) boot: ESP-IDF v4.4.2-388-g755ce1077d 2nd stage bootloader
I (25) boot: compile time 18:46:00
I (25) boot: chip revision: 0
I (28) boot.esp32s3: Boot SPI Speed : 80MHz
I (32) boot.esp32s3: SPI Mode       : DIO
I (37) boot.esp32s3: SPI Flash Size : 2MB
I (42) boot: Enabling RNG early entropy source...
I (47) boot: Partition Table:
I (51) boot: ## Label            Usage          Type ST Offset   Length
I (58) boot:  0 nvs              WiFi data        01 02 00009000 00006000
I (66) boot:  1 phy_init         RF data          01 01 0000f000 00001000
I (73) boot:  2 factory          factory app      00 00 00010000 00100000
I (81) boot: End of partition table
I (85) esp_image: segment 0: paddr=00010020 vaddr=3c050020 size=0e780h ( 59264) map
I (104) esp_image: segment 1: paddr=0001e7a8 vaddr=3fc95b70 size=01870h (  6256) load
I (105) esp_image: segment 2: paddr=00020020 vaddr=42000020 size=479c0h (293312) map
I (163) esp_image: segment 3: paddr=000679e8 vaddr=3fc973e0 size=0213ch (  8508) load
I (165) esp_image: segment 4: paddr=00069b2c vaddr=40374000 size=11b64h ( 72548) load
I (184) esp_image: segment 5: paddr=0007b698 vaddr=50000000 size=00010h (    16) load
I (192) boot: Loaded app from partition at offset 0x10000
I (192) boot: Disabling RNG early entropy source...
I (204) cpu_start: Pro cpu up.
I (205) cpu_start: Starting app cpu, entry point is 0x403751d8
0x403751d8: call_start_cpu1 at /Users/roberto/esp/esp-idf/components/esp_system/port/cpu_start.c:148

I (0) cpu_start: App cpu up.
I (219) cpu_start: Pro cpu start user code
I (219) cpu_start: cpu freq: 160000000
I (219) cpu_start: Application information:
I (221) cpu_start: Project name:     test_app
I (226) cpu_start: App version:      driver-v1.1.6-b13-3-gbf6b4e6-di
I (233) cpu_start: Compile time:     Nov  4 2022 18:45:55
I (239) cpu_start: ELF file SHA256:  bc8b6e398803187d...
I (245) cpu_start: ESP-IDF:          v4.4.2-388-g755ce1077d
I (252) heap_init: Initializing. RAM available for dynamic allocation:
I (259) heap_init: At 3FC9B798 len 0004DF78 (311 KiB): D/IRAM
I (265) heap_init: At 3FCE9710 len 00005724 (21 KiB): STACK/DRAM
I (272) heap_init: At 3FCF0000 len 00008000 (32 KiB): DRAM
I (278) heap_init: At 600FE000 len 00002000 (8 KiB): RTCRAM
I (285) spi_flash: detected chip: gd
I (289) spi_flash: flash io: dio
W (293) spi_flash: Detected size(16384k) larger than the size in the binary image header(2048k). Using the size in the binary image header.
I (306) sleep: Configure to isolate all GPIO pins in sleep state
I (313) sleep: Enable automatic switching of GPIO sleep configuration
I (320) coexist: coexist rom version e7ae62f
I (325) cpu_start: Starting scheduler on PRO CPU.
I (0) cpu_start: Starting scheduler on APP CPU.
W (407) BT_INIT: esp_bt_controller_mem_release not implemented, return OK
W (407) BT_INIT: esp_bt_controller_mem_release not implemented, return OK
I (407) BT_INIT: BT controller compile version [421c279]
I (417) phy_init: phy_version 503,13653eb,Jun  1 2022,17:47:08
I (457) system_api: Base MAC address is not set
I (457) system_api: read default base MAC address from EFUSE
I (457) BT_INIT: Bluetooth MAC: f4:12:fa:cb:81:b6

I (467) TEST_APP: test_app_host_task
I (477) NimBLE: GAP procedure initiated: stop advertising.

I (477) TEST_APP: test_app_on_sync
I (477) TEST_APP: test_app_scan: scanning...
I (487) NimBLE: GAP procedure initiated: discovery; 
I (487) NimBLE: own_addr_type=0 filter_policy=0 passive=0 limited=0 filter_duplicates=1 
I (497) NimBLE: duration=forever
I (507) NimBLE: 

I (547) TEST_APP: test_app_gap_event: 7 BLE_GAP_EVENT_DISC
I (547) TEST_APP: test_app_gap_event: 7 BLE_GAP_EVENT_DISC
I (557) TEST_APP: test_app_gap_event: 7 BLE_GAP_EVENT_DISC
I (567) TEST_APP: test_app_gap_event: 7 BLE_GAP_EVENT_DISC
I (577) TEST_APP: test_app_gap_event: 7 BLE_GAP_EVENT_DISC
I (577) TEST_APP: test_app_gap_event: 7 BLE_GAP_EVENT_DISC
I (587) TEST_APP: test_app_gap_event: 7 BLE_GAP_EVENT_DISC
I (807) TEST_APP: test_app_gap_event: 7 BLE_GAP_EVENT_DISC
I (807) TEST_APP: test_app_gap_event: name miaomiao
I (877) TEST_APP: test_app_gap_event: 7 BLE_GAP_EVENT_DISC
I (877) TEST_APP: test_app_gap_event: 7 BLE_GAP_EVENT_DISC
I (877) TEST_APP: test_app_gap_event: name WGX_iBeacon
I (897) TEST_APP: test_app_gap_event: 7 BLE_GAP_EVENT_DISC
I (897) TEST_APP: test_app_gap_event: 7 BLE_GAP_EVENT_DISC
I (897) TEST_APP: test_app_gap_event: name B6
I (1127) TEST_APP: test_app_gap_event: 7 BLE_GAP_EVENT_DISC
I (1127) TEST_APP: test_app_gap_event: 7 BLE_GAP_EVENT_DISC
I (1127) TEST_APP: test_app_gap_event: 7 BLE_GAP_EVENT_DISC
I (1127) TEST_APP: test_app_gap_event: name Holy-IOT
I (1137) TEST_APP: test_app_gap_event: 7 BLE_GAP_EVENT_DISC
I (1137) TEST_APP: test_app_gap_event: 7 BLE_GAP_EVENT_DISC
I (1147) TEST_APP: test_app_gap_event: name Jinou_Beacon
I (1937) TEST_APP: test_app_gap_event: 7 BLE_GAP_EVENT_DISC
I (1937) TEST_APP: test_app_gap_event: 7 BLE_GAP_EVENT_DISC
I (1937) TEST_APP: test_app_gap_event: name ThermoBeacon
I (2807) TEST_APP: test_app_gap_event: 7 BLE_GAP_EVENT_DISC
I (2807) TEST_APP: test_app_gap_event: name DuoEK 1429
I (2807) TEST_APP: test_app_gap_event: 7 BLE_GAP_EVENT_DISC
I (5137) TEST_APP: test_app_gap_event: 7 BLE_GAP_EVENT_DISC
I (5147) TEST_APP: test_app_gap_event: 7 BLE_GAP_EVENT_DISC
I (5147) TEST_APP: test_app_gap_event: name BPM-188
I (5147) NimBLE: GAP procedure initiated: connect; 
I (5157) NimBLE: peer_addr_type=0 peer_addr=
I (5157) NimBLE: a4:c1:38:84:01:cc
I (5167) NimBLE:  scan_itvl=16 scan_window=16 itvl_min=24 itvl_max=40 latency=0 supervision_timeout=256 min_ce_len=0 max_ce_len=0 own_addr_type=0
I (5177) NimBLE: 

I (5177) TEST_APP: test_app_connect: connecting...
I (5237) TEST_APP: test_app_gap_event: 0 BLE_GAP_EVENT_CONNECT
I (5237) TEST_APP: test_app_gap_event: status=0
I (5237) TEST_APP: print_conn_desc: handle=1 our_ota_addr_type=0 our_ota_addr=f4:12:fa:cb:81:b6
I (5247) TEST_APP: print_conn_desc: our_id_addr_type=0 our_id_addr=f4:12:fa:cb:81:b6
I (5257) TEST_APP: print_conn_desc: peer_ota_addr_type=0 peer_ota_addr=a4:c1:38:84:01:cc
I (5267) TEST_APP: print_conn_desc: peer_id_addr_type=0 peer_id_addr=a4:c1:38:84:01:cc
I (5267) TEST_APP: print_conn_desc: conn_itvl=40 conn_latency=0 supervision_timeout=256 encrypted=0 authenticated=0 bonded=0
I (5287) NimBLE: GATT procedure initiated: discover all services

E (5567) TEST_APP: test_app_svc_disced: conn_handle=1 uuid= error_status=7
I (5567) NimBLE: GAP procedure initiated: connect; 
I (5567) NimBLE: peer_addr_type=0 peer_addr=
I (5577) NimBLE: a4:c1:38:84:01:cc
I (5577) NimBLE:  scan_itvl=16 scan_window=16 itvl_min=24 itvl_max=40 latency=0 supervision_timeout=256 min_ce_len=0 max_ce_len=0 own_addr_type=0
I (5587) NimBLE: 

I (5627) TEST_APP: test_app_gap_event: 0 BLE_GAP_EVENT_CONNECT
I (5627) TEST_APP: test_app_gap_event: status=0
I (5627) TEST_APP: print_conn_desc: handle=1 our_ota_addr_type=0 our_ota_addr=f4:12:fa:cb:81:b6
I (5637) TEST_APP: print_conn_desc: our_id_addr_type=0 our_id_addr=f4:12:fa:cb:81:b6
I (5647) TEST_APP: print_conn_desc: peer_ota_addr_type=0 peer_ota_addr=a4:c1:38:84:01:cc
I (5657) TEST_APP: print_conn_desc: peer_id_addr_type=0 peer_id_addr=a4:c1:38:84:01:cc
I (5657) TEST_APP: print_conn_desc: conn_itvl=40 conn_latency=0 supervision_timeout=256 encrypted=0 authenticated=0 bonded=0
I (5677) NimBLE: GATT procedure initiated: discover all services

E (5967) TEST_APP: test_app_svc_disced: conn_handle=1 uuid= error_status=7
I (5967) NimBLE: GAP procedure initiated: connect; 
I (5967) NimBLE: peer_addr_type=0 peer_addr=
I (5977) NimBLE: a4:c1:38:84:01:cc
I (5977) NimBLE:  scan_itvl=16 scan_window=16 itvl_min=24 itvl_max=40 latency=0 supervision_timeout=256 min_ce_len=0 max_ce_len=0 own_addr_type=0
I (5987) NimBLE: 

I (6017) TEST_APP: test_app_gap_event: 0 BLE_GAP_EVENT_CONNECT
I (6017) TEST_APP: test_app_gap_event: status=0
I (6017) TEST_APP: print_conn_desc: handle=1 our_ota_addr_type=0 our_ota_addr=f4:12:fa:cb:81:b6
I (6027) TEST_APP: print_conn_desc: our_id_addr_type=0 our_id_addr=f4:12:fa:cb:81:b6
I (6037) TEST_APP: print_conn_desc: peer_ota_addr_type=0 peer_ota_addr=a4:c1:38:84:01:cc
I (6047) TEST_APP: print_conn_desc: peer_id_addr_type=0 peer_id_addr=a4:c1:38:84:01:cc
I (6047) TEST_APP: print_conn_desc: conn_itvl=40 conn_latency=0 supervision_timeout=256 encrypted=0 authenticated=0 bonded=0
I (6067) NimBLE: GATT procedure initiated: discover all services

E (6367) TEST_APP: test_app_svc_disced: conn_handle=1 uuid= error_status=7
I (6367) NimBLE: GAP procedure initiated: connect; 
I (6367) NimBLE: peer_addr_type=0 peer_addr=
I (6377) NimBLE: a4:c1:38:84:01:cc
I (6377) NimBLE:  scan_itvl=16 scan_window=16 itvl_min=24 itvl_max=40 latency=0 supervision_timeout=256 min_ce_len=0 max_ce_len=0 own_addr_type=0
I (6387) NimBLE: 

I (6407) TEST_APP: test_app_gap_event: 0 BLE_GAP_EVENT_CONNECT
I (6407) TEST_APP: test_app_gap_event: status=0
I (6407) TEST_APP: print_conn_desc: handle=1 our_ota_addr_type=0 our_ota_addr=f4:12:fa:cb:81:b6
I (6417) TEST_APP: print_conn_desc: our_id_addr_type=0 our_id_addr=f4:12:fa:cb:81:b6
I (6427) TEST_APP: print_conn_desc: peer_ota_addr_type=0 peer_ota_addr=a4:c1:38:84:01:cc
I (6427) TEST_APP: print_conn_desc: peer_id_addr_type=0 peer_id_addr=a4:c1:38:84:01:cc
I (6437) TEST_APP: print_conn_desc: conn_itvl=40 conn_latency=0 supervision_timeout=256 encrypted=0 authenticated=0 bonded=0
I (6447) NimBLE: GATT procedure initiated: discover all services

E (6717) TEST_APP: test_app_svc_disced: conn_handle=1 uuid= error_status=7
I (6717) TEST_APP: test_app_gap_event: 1 BLE_GAP_EVENT_DISCONNECT

More Information.

Error always occur using some peripheral devices, but not with every device. Error occurs using ESP-IDF release/v4.4, release/v5.0 and latest versions.

Some peripheral causing the error are:

Some peripheral working without errors are:

kojibuta commented 11 months ago

Hi @SatishSolankeEsp, Using the workaround you suggest (i.e. CONFIG_BT_BLE_50_FEATURES_SUPPORTED=n) I am now able to use stable relase v5.1.1. It works with all devices on ESP32 and ESP32-S3. Thank you. If I understand correctly, disabling CONFIG_BT_BLE_50_FEATURES_SUPPORTED=n on ESP32-S3 enables channel select algorithm #1 by default. While enabling BLE5.0 features enables channel select algorithm #2 by default. Recently I started working with ESP32-C6 because I would like to add Zigbee support to my firmware. I found the same problem I encountered with ESP32-S3 but unfortunately the workaround does not work. That is, disabling BLE5.0 features does not switch to channel select algorithm #1. I also tried disabling CONFIG_BT_LE_50_FEATURE_SUPPORT=n without success. I found other teams are facing similar problems (e.g. https://esp32.com/viewtopic.php?t=28832). Is there a programmatic way of choosing which channel selection algorithm to use before calling esp_ble_gattc_open() ? This would be very helpful because we would like support both BLE4.2 and BLE5.0 features. Thank you.

SatishSolankeEsp commented 11 months ago

HI @kojibuta I'll look into it and get back to you. Are you trying to use esp32c6 in a central or peripheral role? Thanks, Satish

kojibuta commented 11 months ago

Hi @SatishSolankeEsp, I am trying to use ESP32-C6 in a central role. I actively use ESP32 and ESP32-S3 in a central role and I would like to port my firmware on ESP32-C6 too.

SatishSolankeEsp commented 10 months ago

Hi @kojibuta , Look like we have not ported the change on esp32c6 from esp32s3, will share the controller lib on Monday. Please let me know which IDF release are you using of esp32c6(IDFv5.1 or IDFv5.2).

Thanks, Satish

kojibuta commented 10 months ago

Hi @SatishSolankeEsp,

At the moment I am using the latest master branch, because of another bug in the Wi-Fi stack fixed recently #12478. I can use either the 5.1 or the 5.2 for the production build, let me know which one is going to get the fix from esp32s3. Thank you

SatishSolankeEsp commented 10 months ago

Hi @kojibuta , That is fine could you please replace attach lib in below path esp-idf/components/bt/controller/lib_esp32c6/esp32c6-bt-lib Lib is for CS#1 algo so peripheral should be used ble4.2 4.0 . esp32c6_lib_fix.zip

let me know the result. Thanks, Satish

kojibuta commented 10 months ago

Hi @SatishSolankeEsp,

The lib fix solves the issue with channel select algorithm on esp32c6. Now I am able to connect to a 4.0/4.2 device successfully.

Does the fix automatically select the correct channel select algorithm based on the device? Or is it statically linked and it always use CS#2 if CONFIG_BT_LE_50_FEATURE_SUPPORT=y ?

Thank you

SatishSolankeEsp commented 10 months ago

Hi @kojibuta , We will merge the change on IDF version 5.1,5.2, and master as well, the procedure would be the same as esp32s3. Thanks, Satish

nicklasb commented 5 months ago

Hi @kojibuta , We will merge the change on IDF version 5.1,5.2, and master as well, the procedure would be the same as esp32s3. Thanks, Satish

Has these fixes been merged? I am having similar issues with two T-Beams connecting to each others. Basically I am being connected to, then calling ble_gattc_disc_all_svcs() in the BLE_GAP_EVENT_CONNECT callback and gets a 7 for return value. Just would be quite odd if I wasn't connected while actually being connected to.

ShenWeilong commented 5 months ago

hi @kojibuta @nicklasb ,

We have merged the change on versions 5.1, 5.2, and the master branch. You can try the latest tag and disable the 5.0 features. Then the CS#2 will be disabled as well. The following configs should be disabled: