aws / amazon-freertos-ble-android-sdk

Android SDK for FreeRTOS Bluetooth Devices.
Apache License 2.0
49 stars 41 forks source link

Not able to fetch Wifi List after second time ble connection #45

Closed ameetghosh closed 2 years ago

ameetghosh commented 2 years ago

Describe the bug There is an issue when we do ble connection second time the wifi list is not getting fetched.

image

Error More is disabled and when trying to get the list of networks it getting failed

Build info What is the branch and tag the SDK side? master

System info What is the Android version? 11

Logs 2021-12-14 21:01:07.598 23943-23943/software.amazon.freertos.demo I/ViewRootImpl@9690e65[DeviceScanActivity]: ViewPostIme pointer 0 2021-12-14 21:01:07.659 23943-23943/software.amazon.freertos.demo I/ViewRootImpl@9690e65[DeviceScanActivity]: ViewPostIme pointer 1 2021-12-14 21:01:07.685 23943-23943/software.amazon.freertos.demo I/DeviceScanFragment: Connect switch isChecked: ON 2021-12-14 21:01:07.686 23943-23943/software.amazon.freertos.demo I/BluetoothAdapter: STATE_ON 2021-12-14 21:01:07.691 23943-23943/software.amazon.freertos.demo D/BluetoothGatt: connect() - device: 75:11:D0:3A:E8:AB, auto: false 2021-12-14 21:01:07.691 23943-23943/software.amazon.freertos.demo I/BluetoothAdapter: isSecureModeEnabled 2021-12-14 21:01:07.692 23943-23943/software.amazon.freertos.demo D/BluetoothGatt: registerApp() 2021-12-14 21:01:07.693 23943-23943/software.amazon.freertos.demo D/BluetoothGatt: registerApp() - UUID=c46b8139-ce9b-4a10-b170-6e662cb1813d 2021-12-14 21:01:07.699 23943-4958/software.amazon.freertos.demo D/BluetoothGatt: onClientRegistered() - status=0 clientIf=14 2021-12-14 21:01:08.552 23943-4958/software.amazon.freertos.demo D/BluetoothGatt: onClientConnectionState() - status=0 clientIf=14 device=75:11:D0:3A:E8:AB 2021-12-14 21:01:08.563 23943-4958/software.amazon.freertos.demo I/FRD: BLE connection state changed: 0; new state: BLE_CONNECTED 2021-12-14 21:01:08.563 23943-4958/software.amazon.freertos.demo I/FRD: Connected to GATT server. 2021-12-14 21:01:08.564 23943-4958/software.amazon.freertos.demo D/FRD: Processing BLE command: DISCOVER_SERVICES remaining mqtt queue 0, network queue 0 2021-12-14 21:01:08.564 23943-4958/software.amazon.freertos.demo D/BluetoothGatt: discoverServices() - device: 75:11:D0:3A:E8:AB 2021-12-14 21:01:09.018 23943-4958/software.amazon.freertos.demo D/BluetoothGatt: onConnectionUpdated() - Device=75:11:D0:3A:E8:AB interval=6 latency=0 timeout=500 status=0 2021-12-14 21:01:09.410 23943-4958/software.amazon.freertos.demo D/BluetoothGatt: onSearchComplete() = Device=75:11:D0:3A:E8:AB Status=0 2021-12-14 21:01:09.411 23943-4958/software.amazon.freertos.demo I/FRD: Discovered Ble gatt services successfully. Bonding state: 12 2021-12-14 21:01:09.411 23943-4958/software.amazon.freertos.demo D/FRD: GattService: 00001800-0000-1000-8000-00805f9b34fb 2021-12-14 21:01:09.411 23943-4958/software.amazon.freertos.demo D/FRD: |-characteristics: 00002a00-0000-1000-8000-00805f9b34fb 2021-12-14 21:01:09.412 23943-4958/software.amazon.freertos.demo D/FRD: |-characteristics: 00002a01-0000-1000-8000-00805f9b34fb 2021-12-14 21:01:09.412 23943-4958/software.amazon.freertos.demo D/FRD: GattService: 00001801-0000-1000-8000-00805f9b34fb 2021-12-14 21:01:09.412 23943-4958/software.amazon.freertos.demo D/FRD: |-characteristics: 00002a05-0000-1000-8000-00805f9b34fb 2021-12-14 21:01:09.412 23943-4958/software.amazon.freertos.demo D/FRD: GattService: 8a7f1168-48af-4efb-83b5-e679f932ff00 2021-12-14 21:01:09.412 23943-4958/software.amazon.freertos.demo D/FRD: |-characteristics: DEVICE_VERSION 2021-12-14 21:01:09.412 23943-4958/software.amazon.freertos.demo D/FRD: |-characteristics: IOT_ENDPOINT 2021-12-14 21:01:09.413 23943-4958/software.amazon.freertos.demo D/FRD: |-characteristics: DEVICE_MTU 2021-12-14 21:01:09.413 23943-4958/software.amazon.freertos.demo D/FRD: |-characteristics: DEVICE_PLATFORM 2021-12-14 21:01:09.413 23943-4958/software.amazon.freertos.demo D/FRD: |-characteristics: DEVICE_ID 2021-12-14 21:01:09.413 23943-4958/software.amazon.freertos.demo D/FRD: GattService: a9d7166a-d72e-40a9-a002-48044cc30100 2021-12-14 21:01:09.413 23943-4958/software.amazon.freertos.demo D/FRD: |-characteristics: NETWORK_CONTROL 2021-12-14 21:01:09.414 23943-4958/software.amazon.freertos.demo D/FRD: |-characteristics: NETWORK_TX 2021-12-14 21:01:09.414 23943-4958/software.amazon.freertos.demo D/FRD: |-characteristics: NETWORK_RX 2021-12-14 21:01:09.414 23943-4958/software.amazon.freertos.demo D/FRD: |-characteristics: NETWORK_TXLARGE 2021-12-14 21:01:09.414 23943-4958/software.amazon.freertos.demo D/FRD: |-characteristics: NETWORK_RXLARGE 2021-12-14 21:01:09.414 23943-4958/software.amazon.freertos.demo D/FRD: Getting ble software version on device. 2021-12-14 21:01:09.415 23943-4958/software.amazon.freertos.demo D/FRD: Ble operation is in progress. mqtt queue: 0 network queue: 1 2021-12-14 21:01:09.415 23943-4958/software.amazon.freertos.demo D/FRD: Processing BLE command: READ_CHARACTERISTIC remaining mqtt queue 0, network queue 0 2021-12-14 21:01:09.415 23943-4958/software.amazon.freertos.demo D/FRD: <-<-<- Reading from characteristic: DEVICE_VERSION 2021-12-14 21:01:09.438 23943-4958/software.amazon.freertos.demo D/FRD: ->->-> onCharacteristicRead status: 12 2021-12-14 21:01:09.438 23943-4958/software.amazon.freertos.demo D/FRD: There's no ble command in the queue. 2021-12-14 21:01:09.514 23943-4958/software.amazon.freertos.demo D/BluetoothGatt: onConnectionUpdated() - Device=75:11:D0:3A:E8:AB interval=36 latency=0 timeout=500 status=0

ravibhagavandas commented 2 years ago

Hello,

Looking at the issue it seems the SDK is failing to read device version version characteristic as noted in the error below:

2021-12-14 21:01:09.415 23943-4958/software.amazon.freertos.demo D/FRD: <-<-<- Reading from characteristic: DEVICE_VERSION
2021-12-14 21:01:09.438 23943-4958/software.amazon.freertos.demo D/FRD: ->->-> onCharacteristicRead status: 12

status = 12 indicates GATT_INSUF_KEY_SIZE which probably is called due to invalid pairing.

Could you share the device side logs for the same issue ? What device is being used as MCU ? Which Amazon FreeRTOS version is running on the device?

Also could you provide additional details like

  1. What type of connection is created ? Is it pairing with Numerical comparison ?
  2. did you delete bond on either side of the peer before attempting the connection?
vineet2888 commented 2 years ago

@ravibhagavandas Thanks for replying.

Please find the response to the questions below:

We are using NimBLE stack.

Q 1. Could you share the device side logs for the same issue ? On the device side we are getting ble disconnect event only.

Q 2. What device is being used as MCU ? We are using ESP32.

Q3. Which Amazon FreeRTOS version is running on the device? FreeRTOS version : 202012

Q4. What type of connection is created ? Is it pairing with Numerical comparison ? No , Numerical comparison is not used in pairing.

Q5. Did you delete bond on either side of the peer before attempting the connection? Deleting the entry from paired device list of mobile only. We are not doing anything at device side to delete the bonding.

Apart from that there are following configuration which we are using

/ Enable WIFI provisioning GATT service. /

define IOT_BLE_ENABLE_WIFI_PROVISIONING ( 1 )

define IOT_BLE_ENABLE_GATT_DEMO ( 0 )

/ Disable numeric comparison /

define IOT_BLE_ENABLE_NUMERIC_COMPARISON ( 0 )

define IOT_BLE_ENABLE_SECURE_CONNECTION ( 0 )

define IOT_BLE_INPUT_OUTPUT ( eBTIONone )

define IOT_BLE_ENCRYPTION_REQUIRED ( 1 )

define IOT_BLE_ENABLE_MQTT ( 0 )

define IOT_BLE_WIFI_PROVISIONING_MAX_SAVED_NETWORKS ( 1 )

define IOT_BLE_MAX_BONDED_DEVICES ( 3 )

vineet2888 commented 2 years ago

Hi @ravibhagavandas

I have observed this issue on amazon freertos release 202107. I am using wifi provisioning demo in the mentioned freertos release and freertos demo android app. When i am trying to reprovision the device , i am not able to get wifi scan list. [WifiProvisioningDebugLog_AFR_Version 202107.txt](https://github.com/aws/amazon-freertos-ble-android-sdk/files/7779161/WifiProvisioningDe

https://user-images.githubusercontent.com/23029767/147450758-ecfeecae-16ae-469a-a7d4-7730a9842be0.mp4

bugLog_AFR_Version.202107.txt)

Please find the debug log and provisioning video attached.

ravibhagavandas commented 2 years ago

For 202107 version, WiFi provisioning demo, stops the WiFi provisioning task and the loop exits when a WiFi network is provisioned successfully. Related code here: https://github.com/aws/amazon-freertos/blob/202107.00/demos/wifi_provisioning/aws_wifi_connect_task.c#L111 You can choose to customize the demo for your use case.

ravibhagavandas commented 2 years ago

For the original issue in 202012 version, we identified this to be a bug in vendors sdk staged for that version. This could be fixed by patching Amazon FreeRTOS 202012 version as follows:

  1. Create a file key_size.patch with contents below:

    diff --git a/nimble/host/src/ble_sm.c b/nimble/host/src/ble_sm.c
    index faf20ed2..61916ce4 100644
    --- a/nimble/host/src/ble_sm.c
    +++ b/nimble/host/src/ble_sm.c
    @@ -2114,6 +2114,7 @@ ble_sm_key_exch_exec(struct ble_sm_proc *proc, struct ble_sm_result *res,
         /* store LTK before sending since ble_sm_tx consumes tx mbuf */
         memcpy(proc->our_keys.ltk, enc_info->ltk, 16);
         proc->our_keys.ltk_valid = 1;
    +        proc->our_keys.key_size = proc->key_size;
    
         rc = ble_sm_tx(proc->conn_handle, txom);
         if (rc != 0) {
    @@ -2291,6 +2292,7 @@ ble_sm_enc_info_rx(uint16_t conn_handle, struct os_mbuf **om,
         proc->rx_key_flags &= ~BLE_SM_KE_F_ENC_INFO;
         proc->peer_keys.ltk_valid = 1;
         memcpy(proc->peer_keys.ltk, cmd->ltk, 16);
    +        proc->peer_keys.key_size = proc->key_size;
    
         ble_sm_key_rxed(proc, res);
     }
  2. From home folder of amazon freertos, Copy patch to vendors folder: cp key_size.patch vendors/espressif/esp-idf/components/nimble/nimble
  3. Apply patch:
    cd vendors/espressif/esp-idf/components/nimble/nimble
    git apply --whitespace=fix key_size.patch