h2zero / esp-nimble-cpp

C++ library for the esp32 NimBLE stack based on and mostly compatible with @nkolban cpp_utils BLE library.
https://h2zero.github.io/esp-nimble-cpp/
Apache License 2.0
181 stars 62 forks source link

Fix notifications not being sent. #199

Closed h2zero closed 1 month ago

h2zero commented 1 month ago

The connection handle was not initialized correctly when recording the subscribe status causing the call to send notification/indications to fail with BLE_HS_ENOTCONN.

@finger563 please test, works for me.

finger563 commented 1 month ago

That definitely helped some (battery level now updates appropriately in my example) but the HID is still non functional - I think it may have to do with the change to auth you added in this pr? or maybe something else is still messed up somewhere

h2zero commented 1 month ago

The logic for the auth code hasn't changed, just cleaned up.

finger563 commented 1 month ago

should I expect to see logs of the form adding disc UUID: 0xYYYY, handle: 0x0000, such as:

[Hid Service Example/I][0.300]: Starting
[BleGattServer/I][0.301]: Initializing GATT server with device name: 'ESP++ HID'
I (318) BLE_INIT: BT controller compile version [c66a703]
I (319) BLE_INIT: Bluetooth MAC: f4:12:fa:5a:85:92
I (321) phy_init: phy_version 680,a6008b2,Jun  4 2024,16:41:10
[BleGattServer/I][0.373]: Creating server
[BleGattServer/I][0.374]: Creating device info service
[BleGattServer/I][0.375]: Creating battery service
[Hid Service Example/I][0.376]: Report Descriptor:
[Hid Service Example/I][0.377]:   Size: 170
[Hid Service Example/I][0.378]:   Data: [0x5, 0x1, 0x9, 0x5, 0xa1, 0x1, 0x85, 0x1, 0x9, 0x1, 0xa1, 0x0, 0x9, 0x30, 0x9, 0x31, 0x15, 0x0, 0x27, 0xfe, 0xff, 0x0, 0x0, 0x75, 0x10, 0x95, 0x2, 0x81, 0x2, 0xc0, 0x9, 0x1, 0xa1, 0x0, 0x9, 0x32, 0x9, 0x35, 0x15, 0x0, 0x27, 0xfe, 0xff, 0x0, 0x0, 0x75, 0x10, 0x95, 0x2, 0x81, 0x2, 0xc0, 0x5, 0x2, 0x9, 0xc5, 0x15, 0x0, 0x26, 0xff, 0x3, 0x95, 0x1, 0x75, 0xa, 0x81, 0x2, 0x95, 0x1, 0x75, 0x6, 0x81, 0x1, 0x5, 0x2, 0x9, 0xc4, 0x15, 0x0, 0x26, 0xff, 0x3, 0x95, 0x1, 0x75, 0xa, 0x81, 0x2, 0x95, 0x1, 0x75, 0x6, 0x81, 0x1, 0x5, 0x1, 0x9, 0x39, 0x15, 0x1, 0x25, 0x8, 0x35, 0x0, 0x46, 0x3b, 0x1, 0x65, 0x14, 0x55, 0x0, 0x75, 0x4, 0x95, 0x1, 0x81, 0x42, 0x95, 0x1, 0x75, 0x4, 0x81, 0x1, 0x5, 0x9, 0x19, 0x1, 0x29, 0xf, 0x15, 0x0, 0x25, 0x1, 0x75, 0x1, 0x95, 0xf, 0x81, 0x2, 0x95, 0x1, 0x75, 0x1, 0x81, 0x1, 0x85, 0x2, 0x5, 0x8, 0x19, 0x61, 0x29, 0x64, 0x15, 0x0, 0x25, 0x1, 0x75, 0x1, 0x95, 0x4, 0x91, 0x2, 0x95, 0x1, 0x75, 0x4, 0x91, 0x1, 0xc0]
adding disc UUID: 0x2908, handle: 0x0000
adding disc UUID: 0x2908, handle: 0x0000
adding disc UUID: 0x2904, handle: 0x0000
[BleGattServer/I][0.383]: Setting legacy advertising data
[BleGattServer/I][0.385]: Starting legacy advertising for 0 ms
[Hid Service Example/I][4.405]: Device connected
[Hid Service Example/I][5.387]: Connected devices: 1
[Hid Service Example/I][5.733]:             Names: ["Pixel 6"]
[BleGattServer/I][5.735]: RSSI for connected device 62:df:3b:41:be:6f: -62
[Hid Service Example/I][5.736]:             RSSIs: [-62]
[Hid Service Example/I][6.645]: Device authenticated
[Hid Service Example/I][21.573]: Device disconnected: REMOTE_USER_TERMINATED

emphasis being that the handle for the descriptor is 0x0000.

h2zero commented 1 month ago

Hmm, looks like some debugging got left behind. That message can be ignored, i'll clean it up after.

As for the onConfirmPIN issue, it's working fine on my end, check your function signature:

    void onConfirmPIN(NimBLEConnInfo& connInfo, uint32_t pass_key) override {
        printf("The passkey YES/NO number: %" PRIu32 "\n", pass_key);
        /** Inject false if passkeys don't match. */
        NimBLEDevice::injectConfirmPIN(connInfo, true);
    };
finger563 commented 1 month ago

Oh there's no issue pairing (this is just the HID example, not the GFPS code) - I'm pairing properly and the device stays connected just fine. I'm just seeing that the HID service is not able to be read properly (likely since it's the service that supports encryption, and this device is paired).

click to see complete log with DEBUG esp-nimble-cpp log enabled ```console [Hid Service Example/I][0.298]: Starting [BleGattServer/I][0.299]: Initializing GATT server with device name: 'ESP++ HID' I (317) BLE_INIT: BT controller compile version [c66a703] I (319) BLE_INIT: Bluetooth MAC: f4:12:fa:5a:85:92 I (320) phy_init: phy_version 680,a6008b2,Jun 4 2024,16:41:10 I (366) NimBLEDevice: BLE Host Task Started I (372) NimBLEDevice: NimBle host synced. [BleGattServer/I][0.372]: Creating server [BleGattServer/I][0.373]: Creating device info service [BleGattServer/I][0.374]: Creating battery service [Hid Service Example/I][0.375]: Report Descriptor: [Hid Service Example/I][0.376]: Size: 170 [Hid Service Example/I][0.377]: Data: [0x5, 0x1, 0x9, 0x5, 0xa1, 0x1, 0x85, 0x1, 0x9, 0x1, 0xa1, 0x0, 0x9, 0x30, 0x9, 0x31, 0x15, 0x0, 0x27, 0xfe, 0xff, 0x0, 0x0, 0x75, 0x10, 0x95, 0x2, 0x81, 0x2, 0xc0, 0x9, 0x1, 0xa1, 0x0, 0x9, 0x32, 0x9, 0x35, 0x15, 0x0, 0x27, 0xfe, 0xff, 0x0, 0x0, 0x75, 0x10, 0x95, 0x2, 0x81, 0x2, 0xc0, 0x5, 0x2, 0x9, 0xc5, 0x15, 0x0, 0x26, 0xff, 0x3, 0x95, 0x1, 0x75, 0xa, 0x81, 0x2, 0x95, 0x1, 0x75, 0x6, 0x81, 0x1, 0x5, 0x2, 0x9, 0xc4, 0x15, 0x0, 0x26, 0xff, 0x3, 0x95, 0x1, 0x75, 0xa, 0x81, 0x2, 0x95, 0x1, 0x75, 0x6, 0x81, 0x1, 0x5, 0x1, 0x9, 0x39, 0x15, 0x1, 0x25, 0x8, 0x35, 0x0, 0x46, 0x3b, 0x1, 0x65, 0x14, 0x55, 0x0, 0x75, 0x4, 0x95, 0x1, 0x81, 0x42, 0x95, 0x1, 0x75, 0x4, 0x81, 0x1, 0x5, 0x9, 0x19, 0x1, 0x29, 0xf, 0x15, 0x0, 0x25, 0x1, 0x75, 0x1, 0x95, 0xf, 0x81, 0x2, 0x95, 0x1, 0x75, 0x1, 0x81, 0x1, 0x85, 0x2, 0x5, 0x8, 0x19, 0x61, 0x29, 0x64, 0x15, 0x0, 0x25, 0x1, 0x75, 0x1, 0x95, 0x4, 0x91, 0x2, 0x95, 0x1, 0x75, 0x4, 0x91, 0x1, 0xc0] adding disc UUID: 0x2908, handle: 0x0000 adding disc UUID: 0x2908, handle: 0x0000 adding disc UUID: 0x2904, handle: 0x0000 primary service uuid 0x1800 handle 1 end_handle 5 characteristic uuid 0x2a00 def_handle 2 val_handle 3 min_key_size 0 flags [READ] characteristic uuid 0x2a01 def_handle 4 val_handle 5 min_key_size 0 flags [READ] primary service uuid 0x1801 handle 6 end_handle 13 characteristic uuid 0x2a05 def_handle 7 val_handle 8 min_key_size 0 flags [INDICATE] ccc descriptor uuid 0x2902 handle 9 min_key_size 0 flags [READ|WRITE] characteristic uuid 0x2b3a def_handle 10 val_handle 11 min_key_size 0 flags [READ] characteristic uuid 0x2b29 def_handle 12 val_handle 13 min_key_size 0 flags [READ|WRITE] primary service uuid 0x1812 handle 14 end_handle 29 characteristic uuid 0x2a4a def_handle 15 val_handle 16 min_key_size 0 flags [READ] characteristic uuid 0x2a4b def_handle 17 val_handle 18 min_key_size 0 flags [READ] characteristic uuid 0x2a4c def_handle 19 val_handle 20 min_key_size 0 flags [WRITE_NO_RSP] characteristic uuid 0x2a4e def_handle 21 val_handle 22 min_key_size 0 flags [READ|WRITE_NO_RSP] characteristic uuid 0x2a4d def_handle 23 val_handle 24 min_key_size 0 flags [READ|NOTIFY|READ_ENC] ccc descriptor uuid 0x2902 handle 25 min_key_size 0 flags [READ|WRITE] descriptor uuid 0x2908 handle 26 min_key_size 0 flags [READ|READ_ENC] characteristic uuid 0x2a4d def_handle 27 val_handle 28 min_key_size 0 flags [READ|WRITE_NO_RSP|WRITE|READ_ENC|WRITE_ENC] descriptor uuid 0x2908 handle 29 min_key_size 0 flags [READ|WRITE|READ_ENC|WRITE_ENC] primary service uuid 0x180a handle 30 end_handle 44 characteristic uuid 0x2a50 def_handle 31 val_handle 32 min_key_size 0 flags [READ] characteristic uuid 0x2a29 def_handle 33 val_handle 34 min_key_size 0 flags [READ] characteristic uuid 0x2a24 def_handle 35 val_handle 36 min_key_size 0 flags [READ] characteristic uuid 0x2a25 def_handle 37 val_handle 38 min_key_size 0 flags [READ] characteristic uuid 0x2a28 def_handle 39 val_handle 40 min_key_size 0 flags [READ] characteristic uuid 0x2a26 def_handle 41 val_handle 42 min_key_size 0 flags [READ] characteristic uuid 0x2a27 def_handle 43 val_handle 44 min_key_size 0 flags [READ] primary service uuid 0x180f handle 45 end_handle 49 characteristic uuid 0x2a19 def_handle 46 val_handle 47 min_key_size 0 flags [READ|NOTIFY] ccc descriptor uuid 0x2902 handle 48 min_key_size 0 flags [READ|WRITE] descriptor uuid 0x2904 handle 49 min_key_size 0 flags [READ] [BleGattServer/I][0.396]: Setting legacy advertising data [BleGattServer/I][0.397]: Starting legacy advertising for 0 ms [Hid Service Example/I][37.338]: Device connected [Hid Service Example/I][37.400]: Connected devices: 1 I (37521) NimBLEClient: Found 2 services I (38060) NimBLERemoteValueAttribute: Read complete; status=0 I (38061) NimBLERemoteValueAttribute: Read complete; status=14 [Hid Service Example/I][38.062]: Names: ["Pixel 6"] [BleGattServer/I][38.063]: RSSI for connected device 62:df:3b:41:be:6f: -75 [Hid Service Example/I][38.064]: RSSIs: [-75] [Hid Service Example/I][39.962]: Device authenticated I (41525) NimBLEServer: subscribe event; attr_handle=8, subscribed: false I (42560) NimBLEServer: subscribe event; attr_handle=47, subscribed: true I (42561) NimBLECharacteristic: New subscribe value for conn: 1 val: 1 I (42875) NimBLEServer: subscribe event; attr_handle=24, subscribed: true I (42876) NimBLECharacteristic: New subscribe value for conn: 1 val: 1 ```
finger563 commented 1 month ago

Confirmed, GFPS pairing (which doesn't require encryption) works fine, but HID still fails (the test I have for that has both GFPS and HID).

Edit:

The HID input report is configured with the properties READ | NOTIFY | READ_ENC, which I believe is the part causing the failure. I'll test the example by commenting out the READ_ENC and see if that fixes it.

Edit 2: As expected, Android and iOS don't like that.

h2zero commented 1 month ago

I'm not able to repro this behavior, can you make a minimal example for me to debug this?

h2zero commented 1 month ago

For now I will merge this, please open an issue to track the HID problem.

finger563 commented 1 month ago

Sure I'll try to make a basic example. In the meantime, if you're able and willing to run the espp hid_service example that shows the behavior

finger563 commented 1 month ago

@h2zero I've created a (relatively) simple example which exhibits the error using just NimBLE classes for BLE. The error is related to the broken support for NimBLEAttValue::setValue(std::vector<uint8_t>).

https://github.com/h2zero/esp-nimble-cpp/issues/200

Edit: example can be found here: https://github.com/finger563/nimble-cpp-hid-issue-example/blob/main/main/main.cpp#L91