Topics

Pairing/bonding Zephyr API? Pairing info?


frv
 

Hi,


Bonding:


If the pairing process has succeeded is bonding automatically done? 

I don't really understand the comment in the Bluetooth API  for the function call:

"For the vast majority of applications calling this function shouldn’t be needed."


void bt_set_bondable(bool enable)


"Set/clear the Bonding flag in the Authentication Requirements of SMP Pairing Request/Response data. The initial value of this flag depends on BT_BONDABLE Kconfig setting. For the vast majority of applications calling this function shouldn’t be needed."


BTW I don't find this API any longer in Bluetooth code (V1.14), probably no longer valid?


Pairing:


Also is there an API call to see if the device is already paired? Because after a board startup(power cycle) I don't want to set a new passkey if the device is already paired. 


In my case the BLE peripheral sets a random fixed passkey at startup and the connecting central needs to set this passkey after initiating the pairing process. 


Thanks,

Best regards,

Frank


Johan Hedberg
 

Hi Frank,


On 5 Feb 2019, at 15.25, frv <@frv> wrote:
If the pairing process has succeeded is bonding automatically done?
I don't really understand the comment in the Bluetooth API for the function call:
"For the vast majority of applications calling this function shouldn’t be needed."

void bt_set_bondable(bool enable)

"Set/clear the Bonding flag in the Authentication Requirements of SMP Pairing Request/Response data. The initial value of this flag depends on BT_BONDABLE Kconfig setting. For the vast majority of applications calling this function shouldn’t be needed."

BTW I don't find this API any longer in Bluetooth code (V1.14), probably no longer valid?
Why do you say that? It’s still there in include/bluetooth/conn.h. However, the comment is correct - Zephyr defaults to bondable, so you only need to touch this stuff if you want to perform qualification tests on non-bondable mode.

Pairing:

Also is there an API call to see if the device is already paired? Because after a board startup(power cycle) I don't want to set a new passkey if the device is already paired.

In my case the BLE peripheral sets a random fixed passkey at startup and the connecting central needs to set this passkey after initiating the pairing process.
You can iterate through existing bonds using the bt_foreach_bond() API, however it’s not clear to me why your application would need to use it - if a device is bonded then the previously stored LTK will be used, and if it’s not bonded then a new pairing process will be triggered. Note that you need to have flash storage support enabled, including CONFIG_BT_SETTINGS, in order for the bonding information to be persistently stored and reloaded after a power cycle.

Johan


frv
 
Edited

Hi Johan,

thanks for the feedback.

- conn.h and missing function void set_bondable():
Not sure what went wrong but in my git checkout I see after executing the command git branch in the zephyr/include/bluetooth/ : HEAD detached at v1.13.0, and in the conn.h I'm missing the set_bondable function call. 
Nevertheless it is clear for me that this is not an issue for my current problem.

- So why I need to know if I'm paired? Well maybe and probably my wrong doing of using the fixed passkey.... 

So my working flow is :
- in the BLE peripheral applic I use a fixed passkey that is randomly generated and set via the function call : bt_passkey_set
- my BLE central applic initiates the pairing process to the peripheral device. 
- at the central side, the passkey is asked that was generated in the peripheral.
- so after providing the correct passkey (can be done automatically via the BlueZ API, without manual interaction via a keyboard), the pairing is successfully.
- the BLE central applic can now successfully connect to the peripheral applic and subscribe to the "change" notifications of the heart rate measurement characteristic. 

 - now I restart/power cycle the nRF52 running the BLE peripheral applic, so I call again the "bt_passkey_set" as I don't check if the device is already paired (the reason for my question earlier). So this is probably already fishy... 

As the BLE Central application still sees that it is paired with the BLE peripheral, it just calls a function to make a connection, it will not initiate again a pairing process.
But the BLE peripheral, rejects the connection with returning an error, hci error code: 0x05, meaning authorization failure... 

However after rebuilding my Zephyr peripheral applic without the bt_passkey_set, I still get the connection rejection in the peripheral applic throwing the authorization failure as reason.

So what do I still do wrong? Forgive me I'm still learning... 

Best regards,
Frank

ADDITIONAL INFO: 
I used the peripheral_sc_only example to implement a secure connection.

So in my connected callback function I have this:

static void connected(struct bt_conn *conn, u8_t err)
{
        if (err)
        {
                printk("Connection failed (err %u)\n", err);
        }
        else
        {
                default_conn = bt_conn_ref(conn);
                printk("Connected\n");
                if (bt_conn_security(conn, BT_SECURITY_FIPS) < 0)
                {
                        printk("Failed to set security...\n");
                        bt_conn_disconnect(conn, 0);
                }
        }
}
 


frv
 
Edited

On Tue, Feb 5, 2019 at 02:51 PM, Johan Hedberg wrote:
Note that you need to have flash storage support enabled, including CONFIG_BT_SETTINGS, in order for the bonding information to be persistently stored and reloaded after a power cycle.
https://docs.zephyrproject.org/latest/reference/kconfig/CONFIG_BT_SETTINGS.html

Merging /home/frv/zephyr/samples/bluetooth/peripheral_hr/prj.conf warning: BT_SETTINGS (defined at subsys/bluetooth/host/Kconfig:139) was assigned the value 'y' but got the value 'n'.


When I run make menuconfig, 
BT_SETTINGS(=n) "Store Bluetooth state and configuration persistently"
I can't seem to get this to y(es).

When entering it "Store Bluetooth state and configuration persistenly (NEW) is in red. And nothing can be selected

Normal?

Probably need to first get the Disk/Flash configured, what are appropriate values for the Flash?
Currently I'm using 117012 bytes or 22% of 512KB.

Thanks,
Best regards,

Frank


Johan Hedberg
 

Hi Frank,

Please try not to use any “edit” feature on the mailing lists web-interface. It makes it very difficult to understand what’s going on when reading your emails with a normal email client.

On 5 Feb 2019, at 19.48, frv <@frv> wrote:
When I run make menuconfig,
BT_SETTINGS(=n) "Store Bluetooth state and configuration persistently"
I can't seem to get this to y(es).

When entering it "Store Bluetooth state and configuration persistenly (NEW) is in red. And nothing can be selected

Normal?

Probably need to first get the Disk/Flash configured
Yes, you need to have flash and the settings subsystem enabled. Simplest is probably to look at any of the existing Bluetooth samples that already successfully use BT_SETTINGS. E.g. samples/bluetooth/peripheral is one such sample. peripheral_hr is (unfortunately) not. Note that even after enabling the Kconfig options you’ll also need to update your app to make a call to settings_load() after calling bt_enable(). Take a look at how and where the peripheral app does this in its main.c. As for the Kconfig options, I believe the following from the peripheral app’s prj.conf are the necessary options, assuming you’re ok with the FCB backend (there’s also an NFFS one):

CONFIG_BT_SETTINGS=y
CONFIG_FLASH=y
CONFIG_FLASH_PAGE_LAYOUT=y
CONFIG_FLASH_MAP=y
CONFIG_FCB=y
CONFIG_SETTINGS=y
CONFIG_SETTINGS_FCB=y

Johan


frv
 

Hi Johan,


Sorry for messing up my messages on the forum.

I will try to remember, unfortunately not the first time, I will do my best to do better in the future.


Anyway thanks for pointing me in the right direction.


I'm still getting familar with Zephyr and also with Bluetooth LE. 

Programming in different languages (C / C++) and platforms(Linux / Zephyr) makes it not easier, anyhow we love the challenges as SW developer :).


Ok, I will have a look at the Peripheral example and see how to continue with finalizing my BLE implementation on Zephyr. 


Thanks again for your feedback and I will keep the forum informed on the progress I'm making.


Best regards,

Frank


Van: Hedberg, Johan <johan.hedberg@...>
Verzonden: dinsdag 5 februari 2019 20:45:06
Aan: Vieren Frank
CC: devel@...
Onderwerp: Re: [Zephyr-devel] Pairing/bonding Zephyr API? Pairing info?
 
Hi Frank,

Please try not to use any “edit” feature on the mailing lists web-interface. It makes it very difficult to understand what’s going on when reading your emails with a normal email client.

> On 5 Feb 2019, at 19.48, frv <F.Vieren@...> wrote:
> When I run make menuconfig,
> BT_SETTINGS(=n) "Store Bluetooth state and configuration persistently"
> I can't seem to get this to y(es).
>
> When entering it "Store Bluetooth state and configuration persistenly (NEW) is in red. And nothing can be selected
>
> Normal?
>
> Probably need to first get the Disk/Flash configured

Yes, you need to have flash and the settings subsystem enabled. Simplest is probably to look at any of the existing Bluetooth samples that already successfully use BT_SETTINGS. E.g. samples/bluetooth/peripheral is one such sample. peripheral_hr is (unfortunately) not. Note that even after enabling the Kconfig options you’ll also need to update your app to make a call to settings_load() after calling bt_enable(). Take a look at how and where the peripheral app does this in its main.c. As for the Kconfig options, I believe the following from the peripheral app’s prj.conf are the necessary options, assuming you’re ok with the FCB backend (there’s also an NFFS one):

CONFIG_BT_SETTINGS=y
CONFIG_FLASH=y
CONFIG_FLASH_PAGE_LAYOUT=y
CONFIG_FLASH_MAP=y
CONFIG_FCB=y
CONFIG_SETTINGS=y
CONFIG_SETTINGS_FCB=y

Johan


frv
 

Hi Johan, all,


It all works like a charm now, after a power cycle the BLE central applic reconnects without authentication issues anymore. 

So indeed flash configuration was required as demonstrated in the BT peripheral applic. 

Sometimes hard to follow what happens behind the scene, e.g. settings_load. So a lot of work for me to understand better Zephyr and some of it subsystems. 


I'm wondering what will be my next issue. Anyhow very happy with the result so far. 

Basically I have all that is needed for the prototyping of our use case. Just have to replace the BT HR applic with a BT peripheral applic that keeps track of a button press on the nRF52 board. 


As I just do little modifications to the existing BT demo software, I wondering if there exist a better way to develop and debug coding on Zephyr without having to re-flash all the time when something needs to be modified in application. 


I know you can run an emulator on a host PC but probably without the BT HW this is hard to do unless the host BT device can be used? Is this possible?


Thanks!


Best regards,

Frank


Johan Hedberg
 

Hi Frank,

On 6 Feb 2019, at 11.21, frv <@frv> wrote:
It all works like a charm now, after a power cycle the BLE central applic reconnects without authentication issues anymore.
Good to hear!

I know you can run an emulator on a host PC but probably without the BT HW this is hard to do unless the host BT device can be used? Is this possible?
Yes, it’s possible, however you need to use Linux as your development environment (in case you were using MacOS or Windows until now).

There are instructions available here:

https://docs.zephyrproject.org/latest/subsystems/bluetooth/devel.html#bluetooth-qemu

Johan


frv
 

Hi Johan,

The QEMU concept looks great, just what I'm looking for. Thanks,
Best regards,
Frank