taks / esp32-nimble

A wrapper for the ESP32 NimBLE Bluetooth stack.
Apache License 2.0
111 stars 34 forks source link

NimBLE: GAP procedure initiated: stop advertising. #126

Closed HaoboGu closed 4 months ago

HaoboGu commented 4 months ago

I'm using esp32c3 as a ble keyboard using esp32-nimble, but it failed to advertise. The full log is :

[2024-05-28T13:33:41Z INFO ] Connecting...
[2024-05-28T13:33:41Z INFO ] Using flash stub
Chip type:         esp32c3 (revision v0.4)
Crystal frequency: 40 MHz
Flash size:        4MB
Features:          WiFi, BLE

App/part. size:    710,736/4,128,768 bytes, 17.21%
[2024-05-28T13:33:41Z INFO ] Segment at address '0x0' has not changed, skipping write
[2024-05-28T13:33:41Z INFO ] Segment at address '0x8000' has not changed, skipping write
[00:00:03] [========================================]     390/390     0x10000                                                                                                                           [2024-05-28T13:33:46Z INFO ] Flashing has completed!
Commands:
    CTRL+R    Reset chip
    CTRL+C    Exit

ESP-ROM:esp32c3-api1-20210207
Build:Feb  7 2021
rst:0x15 (USB_UART_CHIP_RESET),boot:0xf (SPI_FAST_FLASH_BOOT)
Saved PC:0x4038055a
0x4038055a - call_start_cpu0
    at /Users/haobogu/Projects/keyboard/rmk/examples/use_rust/esp32c3_ble/.embuild/espressif/esp-idf/v5.1.3/components/esp_system/port/cpu_start.c:604
SPIWP:0xee
mode:DIO, clock div:2
load:0x3fcd5820,len:0x1714
load:0x403cc710,len:0x968
load:0x403ce710,len:0x2f9c
entry 0x403cc710
I (19) boot: ESP-IDF v5.1.2-342-gbcf1645e44 2nd stage bootloader
I (20) boot: compile time Dec 12 2023 10:50:58
I (20) boot: chip revision: v0.4
I (24) boot.esp32c3: SPI Speed      : 40MHz
I (29) boot.esp32c3: SPI Mode       : DIO
I (33) boot.esp32c3: SPI Flash Size : 4MB
I (38) boot: Enabling RNG early entropy source...
I (44) boot: Partition Table:
I (47) boot: ## Label            Usage          Type ST Offset   Length
I (54) boot:  0 nvs              WiFi data        01 02 00009000 00006000
I (62) boot:  1 phy_init         RF data          01 01 0000f000 00001000
I (69) boot:  2 factory          factory app      00 00 00010000 003f0000
I (77) boot: End of partition table
I (81) esp_image: segment 0: paddr=00010020 vaddr=3c080020 size=23d40h (146752) map
I (122) esp_image: segment 1: paddr=00033d68 vaddr=00000001 size=00044h (    68) 
I (122) esp_image: segment 2: paddr=00033db4 vaddr=3fc91400 size=02494h (  9364) load
I (130) esp_image: segment 3: paddr=00036250 vaddr=40380000 size=09dc8h ( 40392) load
I (146) esp_image: segment 4: paddr=00040020 vaddr=42000020 size=7635ch (484188) map
I (254) esp_image: segment 5: paddr=000b6384 vaddr=40389dc8 size=074a0h ( 29856) load
I (267) boot: Loaded app from partition at offset 0x10000
I (267) boot: Disabling RNG early entropy source...
I (278) cpu_start: Unicore app
I (279) cpu_start: Pro cpu up.
I (289) cpu_start: Pro cpu start user code
I (289) cpu_start: cpu freq: 160000000 Hz
I (289) cpu_start: Application information:
I (292) cpu_start: Project name:     libespidf
I (297) cpu_start: App version:      rmk-v0.1.15-11-gadca492-dirty
I (304) cpu_start: Compile time:     May 28 2024 21:33:10
I (310) cpu_start: ELF file SHA256:  0000000000000000...
I (316) cpu_start: ESP-IDF:          v5.1.3
I (321) cpu_start: Min chip rev:     v0.3
I (325) cpu_start: Max chip rev:     v1.99 
I (330) cpu_start: Chip rev:         v0.4
I (335) heap_init: Initializing. RAM available for dynamic allocation:
I (342) heap_init: At 3FC96670 len 00029990 (166 KiB): DRAM
I (348) heap_init: At 3FCC0000 len 0001C710 (113 KiB): DRAM/RETENTION
I (355) heap_init: At 3FCDC710 len 00002950 (10 KiB): DRAM/RETENTION/STACK
I (363) heap_init: At 50000010 len 00001FD8 (7 KiB): RTCRAM
I (371) spi_flash: detected chip: generic
I (374) spi_flash: flash io: dio
I (378) sleep: Configure to isolate all GPIO pins in sleep state
I (385) sleep: Enable automatic switching of GPIO sleep configuration
I (392) coexist: coex firmware version: 77cd7f8
I (397) coexist: coexist rom version 9387209
I (402) app_start: Starting scheduler on CPU0
I (407) main_task: Started on CPU0
I (411) main_task: Calling app_main()
[INFO] - Hello ESP BLE!
I (416) gpio: GPIO[3]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 0| Pulldown: 0| Intr:0 
I (425) gpio: GPIO[4]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 0| Pulldown: 0| Intr:0 
I (434) gpio: GPIO[5]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 0| Pulldown: 0| Intr:0 
I (443) gpio: GPIO[6]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 0| Pulldown: 0| Intr:0 
I (452) gpio: GPIO[7]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 0| Pulldown: 0| Intr:0 
I (462) gpio: GPIO[20]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 0| Pulldown: 0| Intr:0 
I (471) gpio: GPIO[21]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 0| Pulldown: 0| Intr:0 
[INFO] - Advertising..
I (486) BLE_INIT: BT controller compile version [9359a4d]
I (489) BLE_INIT: Bluetooth MAC: ec:da:3b:d1:87:c6

I (493) phy_init: phy_version 1150,7c3c08f,Jan 24 2024,17:32:21
I (543) esp32_nimble::ble_device: BLE Host Task Started
I (546) NimBLE: GAP procedure initiated: stop advertising.

I (549) esp32_nimble::ble_device: Device Address: EC:DA:3B:D1:87:C6
[INFO] - set hid
[INFO] - got ble adv

And my advertising code is:

info!("Advertising..");
let keyboard_name = usb_config.product_name;
let device = BLEDevice::take();
BLEDevice::set_device_name(keyboard_name).ok();
device
    .security()
    .set_auth(AuthReq::all())
    .set_io_cap(SecurityIOCap::NoInputNoOutput)
    .resolve_rpa();
let server = device.get_server();
// Set disconnected callback
server.on_disconnect(|_, r| {
    if let Err(e) = r {
        warn!("BLE disconnected, error code: {}", e.code());
    }
    info!("Disconnected!");
});
server.on_connect(|_, _| {
    info!("Connected!");
});
let mut hid = BLEHIDDevice::new(server);

let input_keyboard = hid.input_report(BleCompositeReportType::Keyboard as u8);
let output_keyboard = hid.output_report(BleCompositeReportType::Keyboard as u8);

hid.pnp(
    VidSource::UsbIF as u8,
    usb_config.vid,
    usb_config.pid,
    0x0000,
);
hid.set_battery_level(80);
hid.hid_info(0x00, 0x03);
hid.report_map(BleKeyboardReport::desc());

info!("set hid");
let ble_advertising = device.get_advertising();
info!("got ble adv");
match ble_advertising
    .lock()
    .scan_response(false)
    .set_data(
        BLEAdvertisementData::new()
            .name(keyboard_name)
            .appearance(0x03C1)
            .add_service_uuid(hid.hid_service().lock().uuid()),
    )
    .and_then(|_| ble_advertising.lock().start())
{
    Ok(_) => info!("Advertising done"),
    Err(e) => error!("BLE advertising error, error code: {}", e.code()),
}
info!("advertising");        

As you can see, the code stop at ble_advertising, I suspect that it failed to acquire the lock, but I don't know why. Anyone has ideas? Thanks!

HaoboGu commented 4 months ago

fixed by:

        if let Err(e) = ble_advertising.lock().scan_response(false).set_data(
            BLEAdvertisementData::new()
                .name(keyboard_name)
                .appearance(0x03C1)
                .add_service_uuid(hid.hid_service().lock().uuid()),
        ) {
            error!("BLE advertising error, error code: {}", e.code());
        }

        if let Err(e) = ble_advertising.lock().start() {
            error!("BLE advertising start error: {}", e.code());
        }