Adding custom BLE services to Matter

We are developing Matter/Thread based Smart Lock product. One of the product requirements is that  the product should be able to connect to a BLE mobile APP at all times i.e. before and after provisioned into matter/Thread network. The BLE will be used to configure the product directly instead of the ecosystems APP like Apple Home, Google Home etc.

I have followed the below:

https://docs.nordicsemi.com/bundle/ncs-latest/page/nrf/protocols/matter/getting_started/adding_bt_services.html

Matter: Door lock (nordicsemi.com) including Nordic UART services.

And also reviewed various examples for creating custom BLE services.

Based on the above, 

  1. Successfully built “NUS” (provided by Nordic SDK) and  tried it. It works.
  2. Successfully duplicated the “NUS” by copying the source files “nus.c, nus.h, bt_nus_service.h, bt_nus_service.cpp”, modified ONLY UUUID and the functions calls,  built and tested the custom service
    1. I did not add any  config parameters. “CONFIG_CHIP_NUS”  builds  the Nordic NUS service and also the custom BLE service.
    2. Nordic's nRF toolbox and nRF connect shows both the services. Was able to send the "Lock" and "Unlock" commands from the Apps.
  3. The above method will work for us however, we do not want “Nordic UART services” running. It consumes not only memory, resources but also  allows anyone to connect to the service which is a security issue for us. Please note that this is a door lock and we have proprietary means to provide security which requires custom BLE services.
  4. I tried to build the custom BLE service only.
    1. From the same example above,  I removed  “CONFIG_BLE_NUS” from proj.conf
    2. Looking at the BLE examples  that Nordic provided and also from the Zephyr example (zephyr/samples/bluetooth/peripheral/prj.conf at main · zephyrproject-rtos/zephyr (github.com), I added the below config parameters to proj.conf

----------------------------------------

CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=2048

 

CONFIG_BT_RX_STACK_SIZE=1536

 

CONFIG_BT=y

CONFIG_LOG=y

CONFIG_BT_SMP=y

CONFIG_BT_SIGNING=y

CONFIG_BT_PERIPHERAL=y

CONFIG_BT_DIS=y

CONFIG_BT_ATT_PREPARE_COUNT=5

 

CONFIG_BT_DEVICE_NAME="MTI Sample Lock"

CONFIG_BT_DEVICE_APPEARANCE=833

CONFIG_BT_DEVICE_NAME_DYNAMIC=y

CONFIG_BT_DEVICE_NAME_MAX=65

 

CONFIG_BT_KEYS_OVERWRITE_OLDEST=y

CONFIG_BT_SETTINGS=y

-----------------

 

When Flashed to nRF5340 DK, the DK is not   starting. Please see the crash messages from the debug port.

-------

 

[08:47:26:806] uart:~$ *** Booting nRF Connect SDK v3.5.99-ncs1-1 ***␍␊

[08:47:26:878] E: 77 [DL]Failed to create BLE identity: 0␍␊

[08:47:26:882] E: 81 [DL]BLEManager initialization failed: 200000c␍␊

[08:47:26:888] E: PlatformMgr().InitChipStack() failed [Error: 0]␍␊

[08:47:26:894] E: Exited with code 200000c␍␊

               

                       ----

 

Further probing and experimenting, I found that CONFIG_BT_SMP=y is causing the crash. If I remove  "CONFIG_BT_SMP and CONFIG_BT_SIGNING"  ( I also made changed the code to remove security),  the crash would go away but it caused issues with Matter provisioning (Matter provisioning hung). Regardless, this will not help us because we need secure LE..

 

                It looks like there is an issue with the CONFIG settings and I can not find any  information from Nordic Dev/Blogs  or from the Blogs.

 

                One of the reason that we are interested in nRF SoC’s is due to this feature so please help to resolve.

 

                It will be great if  you can provide sample code for BLE custom service that runs with Matter.   The example from NUS is great but it is not enough.

Thanks

Subu Muthu

  • Hi,

    Just to confirm, you have made a custom BLE service that you want to use with Matter? Have you tested the service in a BLE-only example without Matter, to verify that the service itself works as expected?

    Can you clarify what the custom BLE service is supposed to do and what additional features you need that is not provided by the NUS guide? It might not be that the BLE sample you are using (zephyr/samples/bluetooth/peripheral/prj.conf at main · zephyrproject-rtos/zephyr (github.com)) is the best starting point, and it might be better to use one of the BLE samples in the nRF Connect SDK instead (https://github.com/nrfconnect/sdk-nrf/tree/main/samples/bluetooth).

    Best regards,
    Marte

  • Hi Marte,

    Thanks for responding!

    Yes, we need a custom BLE service that works with Matter. You also asked why we can  not use NUS and what additional features are needed.  

    It is not the additional feature that we are looking for. We already tried NUS. In our view, it is good example for prototyping but we could  take the source code, modify and develop our custom  BLE service based on source code provided by Nordic SDK.

    What changes are we looking for in NUS?

    1.  NUS service puts out the pairing code via debug terminal. Our product - door lock, does not have access to debug terminal. we will come up with different solution.
    2. Our product is door lock and  it is access control market application. With that the security is the most important one and we will require the below modifications:
      1. Instead of NUS UUID's, we will have company specific UUID
      2. Whitelisting will be done to connect the smart lock to  our App only.
    3. We have few other proprietary  schemes which I do not want to describe here.

    So, as a first step (Step #1),  we made a copy of NUS source code from  “nus.c, nus.h, bt_nus_service.h, bt_nus_service.cpp”, modified ONLY UUUID's and  renamed the function call appropriately.

    NOTE: we did not make any other modifications.

    The "prj.conf" had,

    -----------

    #CONFIG_CHIP_NUS=y - disabled it

    #--- Enable BLE

    CONFIG_BT=y

    CONFIG_BT_SMP=y

    CONFIG_BT_PERIPHERAL=y

    ---------

    We built and tried. The device did not even start. Below is the crash/ log from debug terminal:

    ----

    [08:47:26:806] uart:~$ *** Booting nRF Connect SDK v3.5.99-ncs1-1 ***␍␊

    [08:47:26:878] E: 77 [DL]Failed to create BLE identity: 0␍␊

    [08:47:26:882] E: 81 [DL]BLEManager initialization failed: 200000c␍␊

    [08:47:26:888] E: PlatformMgr().InitChipStack() failed [Error: 0]␍␊

    [08:47:26:894] E: Exited with code 200000c␍␊

    ----

    Step #2:

    When we modified the "prj.conf" (note that SMP is disabled)

    --

    -----------

    #CONFIG_CHIP_NUS=y - disabled it

    #--- Enable BLE

    CONFIG_BT=y

    #CONFIG_BT_SMP=y -- disable the security

    CONFIG_BT_PERIPHERAL=y

    ---------

    Then modified the source code to remove the security, built and tried it. This time, the crash did not occur, the device started and we were able to connect to the BLE service while running Matter.

    Step #3:

    Then we added the "CONFIG_BT_SMP=y" , kept the same source code as in Step #2, build and tried. The device failed to start and the crash occurred as in Step #1 (same logs).

    So in summary,

    1. The custom BLE service is a copy of Nordic's NUS code. We verified it without security.
    2. The stack does not start and crashes when  the build is made with "CONFIG_BT_SMP=y".
    3. I do not believe my custom BLE code is causing the crash. The crash occurs at the init stages of CHIP stack.

    I can share the source code but what you will see is the NUS source code with functional calls renamed to something else.

    Please advise.

    Thanks.

    Subu

  • Hi Subu,

    Thank you for the additional information. I am looking into this and will get back to you tomorrow.

    Best regards,
    Marte

  • Hi Subu

    SubuMuthu said:
    So, as a first step (Step #1),  we made a copy of NUS source code from  “nus.c, nus.h, bt_nus_service.h, bt_nus_service.cpp”, modified ONLY UUUID's and  renamed the function call appropriately.

    So you did not include the custom service you made in this test case, only enabled Bluetooth, SMP and peripheral role?

    SubuMuthu said:
    The stack does not start and crashes when  the build is made with "CONFIG_BT_SMP=y".

    Disabling bonding with CONFIG_BT_BONDABLE=n should fix this issue. Bonding cannot be enabled for Matter BLE services, as this makes it non-compliant with the Matter specification due to the requirement that a static random address shall be used for Matter BLE service advertising purposes.

    If you look at https://github.com/nrfconnect/sdk-connectedhomeip/blob/v2.6.1/config/nrfconnect/chip-module/Kconfig.features#L104-L105, you can see that bonding gets disabled when CONFIG_CHIP_NUS is enabled, which is why you do not see this issue when using NUS in Matter samples.

    Best regards,
    Marte

  • Thank you Marte,

    It worked!  The below are the exact changes that I made.

    #---Enable BLE

    CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=2048

    CONFIG_BT=y
    CONFIG_BT_SMP=y

    CONFIG_BT_BONDABLE=n

    CONFIG_BT_RX_STACK_SIZE=1536

    CONFIG_BT_DEVICE_APPEARANCE=833

    Appreciate your support!

    Subu

Related