How can we enable our addr as PUBLIC in our ble advertisement?

We would like configure our device with a public addr instead of the random as is default for NCS/Zephyr. We are currently on Zephyr v3.1.99 and NCS 2.1. I have tracked down the function that should set our device to a public addr but I'm getting an error that says public isn't supported. IAfter some research I've found a couple of places in the forum, github, and documentation that mentions public may not be supported yet. Right now our mobile apps and other factory equipment are expecting our device to advertise as a public addr. So we really need to figure out if this is even possible and if not, when will it be.

Here is the code that I'm using to try and setup the addr:

     bt_addr_le_t  ble_gap_addr;
    ble_gap_addr.type = BT_ADDR_LE_PUBLIC;
    memcpy(&ble_gap_addr.a.val, "\x62\x52\x42\x09\xe7\x34", sizeof(ble_gap_addr.a.val));
    ble_gap_addr.type = BT_ADDR_LE_PUBLIC;
    BT_ADDR_SET_STATIC(&ble_gap_addr.a);
    bt_id_create(&ble_gap_addr, NULL);

I'm seeing an error in my debug log when this code is getting run:

Image Confirmed Successfully!
[00:00:09.546,569] <err> bt_id: Only static random identity address supported
BLE advertisement state change: 0
BLE advertisement state change: 1

Parents
  • I have also tried to use bt_ctlr_set_public_addr() as well. But I get an undefined reference to 'bt_ctlr_set_public_addr' even with bluetooth/controller.h is included.

  • Hi,

    bt_ctlr_set_public_addr() is the right function (this internally calls sdc_hci_cmd_vs_zephyr_write_bd_addr() when using the SoftDevice controllr).

    shellpry_jiobit said:
    But I get an undefined reference to 'bt_ctlr_set_public_addr' even with bluetooth/controller.h is included.

    That is odd. Could it be that you have a typo in the include or where you call the function? Can you show the build output (I am particularly wondering about warnings that would arise from such a typo).

  • Sure. This is build error I get when using bt_cltr_set_public_addr().

    FAILED: zephyr/zephyr_pre0.elf zephyr/zephyr_pre0.map /home/shellpry/Documents/west-workspace/wiced-jiobit/WICED-SDK/build/zephyr/zephyr_pre0.map
    : && ccache /home/shellpry/gnuarmemb/bin/arm-none-eabi-gcc  -Wl,--wrap,malloc -Wl,--wrap,calloc -Wl,--wrap,realloc -Wl,--wrap,free zephyr/CMakeFiles/zephyr_pre0.dir/misc/empty_file.c.obj -o zephyr/zephyr_pre0.elf  zephyr/CMakeFiles/offsets.dir/./arch/arm/core/offsets/offsets.c.obj  -fuse-ld=bfd  -Wl,-T  zephyr/linker_zephyr_pre0.cmd  -Wl,-Map=/home/shellpry/Documents/west-workspace/wiced-jiobit/WICED-SDK/build/zephyr/zephyr_pre0.map  -Wl,--whole-archive  app/libapp.a  zephyr/libzephyr.a  zephyr/arch/common/libarch__common.a  zephyr/arch/arch/arm/core/aarch32/libarch__arm__core__aarch32.a  zephyr/arch/arch/arm/core/aarch32/cortex_m/libarch__arm__core__aarch32__cortex_m.a  zephyr/arch/arch/arm/core/aarch32/cortex_m/cmse/libarch__arm__core__aarch32__cortex_m__cmse.a  zephyr/arch/arch/arm/core/aarch32/mpu/libarch__arm__core__aarch32__mpu.a  zephyr/lib/libc/newlib/liblib__libc__newlib.a  zephyr/lib/posix/liblib__posix.a  zephyr/soc/arm/common/cortex_m/libsoc__arm__common__cortex_m.a  zephyr/boards/boards/arm/hawking_cpuapp/lib..__..__wiced-jiobit__jiobit-zephyr__boards__arm__hawking.a  zephyr/subsys/bluetooth/common/libsubsys__bluetooth__common.a  zephyr/subsys/bluetooth/host/libsubsys__bluetooth__host.a  zephyr/subsys/dfu/boot/libsubsys__dfu__boot.a  zephyr/subsys/net/libsubsys__net.a  zephyr/subsys/net/ip/libsubsys__net__ip.a  zephyr/subsys/random/libsubsys__random.a  zephyr/drivers/clock_control/libdrivers__clock_control.a  zephyr/drivers/console/libdrivers__console.a  zephyr/drivers/gpio/libdrivers__gpio.a  zephyr/drivers/i2c/libdrivers__i2c.a  zephyr/drivers/pwm/libdrivers__pwm.a  zephyr/drivers/spi/libdrivers__spi.a  zephyr/drivers/flash/libdrivers__flash.a  zephyr/drivers/serial/libdrivers__serial.a  zephyr/drivers/bluetooth/libdrivers__bluetooth.a  zephyr/drivers/entropy/libdrivers__entropy.a  zephyr/drivers/timer/libdrivers__timer.a  zephyr/drivers/pinctrl/libdrivers__pinctrl.a  zephyr/drivers/mbox/libdrivers__mbox.a  modules/nrf/lib/fatal_error/lib..__nrf__lib__fatal_error.a  modules/nrf/subsys/dfu/dfu_target/lib..__nrf__subsys__dfu__dfu_target.a  modules/nrf/drivers/hw_cc310/lib..__nrf__drivers__hw_cc310.a  modules/mcuboot/boot/bootutil/zephyr/libmcuboot_util.a  modules/mbedtls/libmodules__mbedtls.a  modules/hal_nordic/nrfx/libmodules__hal_nordic__nrfx.a  modules/libmetal/libmetal/lib/libmetal.a  modules/open-amp/open-amp/lib/libopen_amp.a  -Wl,--no-whole-archive  zephyr/kernel/libkernel.a  -L"/home/shellpry/gnuarmemb/bin/../lib/gcc/arm-none-eabi/9.2.1/thumb/v8-m.main/nofp"  -L/home/shellpry/Documents/west-workspace/wiced-jiobit/WICED-SDK/build/zephyr  -lgcc  zephyr/arch/common/libisr_tables.a  -no-pie  -mcpu=cortex-m33  -mthumb  -mabi=aapcs  -mfp16-format=ieee  -Wl,--gc-sections  -Wl,--build-id=none  -Wl,--sort-common=descending  -Wl,--sort-section=alignment  -Wl,-u,_OffsetAbsSyms  -Wl,-u,_ConfigAbsSyms  -nostdlib  -static  -Wl,-X  -Wl,-N  -Wl,--orphan-handling=warn  -lm  -Wl,-lc  -L"/home/shellpry/gnuarmemb/arm-none-eabi"/lib/thumb/v8-m.main/nofp  -Wl,-lgcc  -lc  -specs=nano.specs  /home/shellpry/Documents/west-workspace/ncs/nrfxlib/crypto/nrf_cc312_platform/lib/cortex-m33/soft-float/no-interrupts/libnrf_cc312_platform_0.9.15.a  -lc && cd /home/shellpry/Documents/west-workspace/wiced-jiobit/WICED-SDK/build/zephyr && /usr/bin/cmake -E echo
    /home/shellpry/gnuarmemb/bin/../lib/gcc/arm-none-eabi/9.2.1/../../../../arm-none-eabi/bin/ld.bfd: app/libapp.a(jio_ble_dev_zephyr.c.obj): in function `set_bt_addr':
    /home/shellpry/Documents/west-workspace/wiced-jiobit/WICED-SDK/jiobit/firmware_common/jio_ble_dev/zephyr/jio_ble_dev_zephyr.c:257: undefined reference to `bt_ctlr_set_public_addr'
    collect2: error: ld returned 1 exit status
    ninja: build stopped: subcommand failed.
    FATAL ERROR: command exited with status 1: /usr/bin/cmake --build /home/shellpry/Documents/west-workspace/wiced-jiobit/WICED-SDK/build

     *  The terminal process "/usr/bin/bash '-c', 'west build --pristine -b hawking_cpuapp apps/jiobit_bare/zephyr'" terminated with exit code: 1.
     *  Terminal will be reused by tasks, press any key to close it. 

    This is my function:

    static void set_bt_addr(const char *addr_str, const char *type_str)
    {
        int err;
        bt_addr_le_t addr;
        err = bt_addr_le_from_str(addr_str, type_str, &addr);
        if (err) {
            printk("Invalid BT address (err %d)\n", err);
        }
        if (addr.type == BT_ADDR_LE_PUBLIC) {
            bt_ctlr_set_public_addr(addr.a.val);
            return;
        } else {
            err = bt_id_create(&addr, NULL);
            if (err < 0) {
                printk("Creating new ID failed (err %d)\n", err);
            }
        }
    }

    and my includes:

    // System includes
    #include <bluetooth/bluetooth.h>
    #include <bluetooth/conn.h>
    #include <bluetooth/gap.h>
    #include <bluetooth/gatt.h>
    #include <bluetooth/hci.h>
    #include <bluetooth/uuid.h>
    #include <mbedtls/sha256.h>
    #include <shell/shell.h>
    #include <shell/shell_uart.h>
    #include <stdbool.h>
    #include <stdint.h>
    #include <stdio.h>
    #include <zephyr.h>
    #include <bluetooth/controller.h>

  • Hi,

    I forgot a key point, this only works on the network core. For code running from the application core you should use sdc_hci_cmd_vs_zephyr_write_bd_addr() instead. See this post.

  • I'm trying to use sdc_hci_cmd_vs_zephyr_write_bd_addr()

            sdc_hci_cmd_vs_zephyr_write_bd_addr_t *ble_gap_addr;
            ble_gap_addr->bd_addr[0] = 2;
            ble_gap_addr->bd_addr[1] = 2;
            ble_gap_addr->bd_addr[2] = 2;
            ble_gap_addr->bd_addr[3] = 2;
            ble_gap_addr->bd_addr[4] = 2;
            ble_gap_addr->bd_addr[5] = 2;
            sdc_hci_cmd_vs_zephyr_write_bd_addr(ble_gap_addr);

    But I'm getting compile errors again.

    /home/shellpry/Documents/west-workspace/wiced-jiobit/WICED-SDK/jiobit/firmware_common/jio_ble_dev/zephyr/jio_ble_dev_zephyr.c: In function 'jio_ble_dev_start':
    /home/shellpry/Documents/west-workspace/wiced-jiobit/WICED-SDK/jiobit/firmware_common/jio_ble_dev/zephyr/jio_ble_dev_zephyr.c:279:34: error: 'ble_gap_addr' may be used uninitialized in this function [-Werror=maybe-uninitialized]
      279 |         ble_gap_addr->bd_addr[0] = 2;
          |         ~~~~~~~~~~~~~~~~~~~~~~~~~^~~
    cc1: all warnings being treated as errors
    [310/627] Linking C static library zephyr/libzephyr.a
    ninja: build stopped: subcommand failed.
    FATAL ERROR: command exited with status 1: /usr/bin/cmake --build /home/shellpry/Documents/west-workspace/wiced-jiobit/WICED-SDK/build

    I thought maybe this is because the PUBLIC address needs to be read from the chip first. So I tried to use sdc_hci_cmd_ip_read_bd_addr() instead of setting the bd_addr manually. But gives me the following build error:

    /home/shellpry/Documents/west-workspace/wiced-jiobit/WICED-SDK/jiobit/firmware_common/jio_ble_dev/zephyr/jio_ble_dev_zephyr.c: In function 'jio_ble_dev_start':
    /home/shellpry/Documents/west-workspace/wiced-jiobit/WICED-SDK/jiobit/firmware_common/jio_ble_dev/zephyr/jio_ble_dev_zephyr.c:285:15: error: implicit declaration of function 'sdc_hci_cmd_ip_read_bd_addr'; did you mean 'sdc_hci_cmd_le_read_phy'? [-Werror=implicit-function-declaration]
      285 |         err = sdc_hci_cmd_ip_read_bd_addr(ble_gap_addr);
          |               ^~~~~~~~~~~~~~~~~~~~~~~~~~~
          |               sdc_hci_cmd_le_read_phy
  • Hi,

    You are getting this error:

    jio_ble_dev_zephyr.c:279:34: error: 'ble_gap_addr' may be used uninitialized in this function [-Werror=maybe-uninitialized]

    And that makes sense, because of your code, where you make a pointer to a sdc_hci_cmd_vs_zephyr_write_bd_addr instance that exists nowhere, so this is uninitialized memory at a random location. The code snippet you have posted should be something like this instead:

            sdc_hci_cmd_vs_zephyr_write_bd_addr_t ble_gap_addr; // note that there is no * here
            ble_gap_addr.bd_addr[0] = 2; // note the . and not -> here
            ble_gap_addr.bd_addr[1] = 2;
            ble_gap_addr.bd_addr[2] = 2;
            ble_gap_addr.bd_addr[3] = 2;
            ble_gap_addr.bd_addr[4] = 2;
            ble_gap_addr.bd_addr[5] = 2;
            sdc_hci_cmd_vs_zephyr_write_bd_addr(&ble_gap_addr); // note the & here

Reply
  • Hi,

    You are getting this error:

    jio_ble_dev_zephyr.c:279:34: error: 'ble_gap_addr' may be used uninitialized in this function [-Werror=maybe-uninitialized]

    And that makes sense, because of your code, where you make a pointer to a sdc_hci_cmd_vs_zephyr_write_bd_addr instance that exists nowhere, so this is uninitialized memory at a random location. The code snippet you have posted should be something like this instead:

            sdc_hci_cmd_vs_zephyr_write_bd_addr_t ble_gap_addr; // note that there is no * here
            ble_gap_addr.bd_addr[0] = 2; // note the . and not -> here
            ble_gap_addr.bd_addr[1] = 2;
            ble_gap_addr.bd_addr[2] = 2;
            ble_gap_addr.bd_addr[3] = 2;
            ble_gap_addr.bd_addr[4] = 2;
            ble_gap_addr.bd_addr[5] = 2;
            sdc_hci_cmd_vs_zephyr_write_bd_addr(&ble_gap_addr); // note the & here

Children
  • Well, don't I feel silly now. LOL! Thank you!

    But I'm still getting an error during compile after the corrections from above.

    /home/shellpry/Documents/west-workspace/ncs/nrfxlib/crypto/nrf_cc312_platform/lib/cortex-m33/soft-float/no-interrupts/libnrf_cc312_platform_0.9.15.a  -lc && cd /home/shellpry/Documents/west-workspace/wiced-jiobit/WICED-SDK/build/zephyr && /usr/bin/cmake -E echo
    /home/shellpry/gnuarmemb/bin/../lib/gcc/arm-none-eabi/9.2.1/../../../../arm-none-eabi/bin/ld.bfd: app/libapp.a(jio_ble_dev_zephyr.c.obj): in function `jio_ble_dev_start':
    /home/shellpry/Documents/west-workspace/wiced-jiobit/WICED-SDK/jiobit/firmware_common/jio_ble_dev/zephyr/jio_ble_dev_zephyr.c:290: undefined reference to `sdc_hci_cmd_vs_zephyr_write_bd_addr'
    collect2: error: ld returned 1 exit status
    ninja: build stopped: subcommand failed.
    FATAL ERROR: command exited with status 1: /usr/bin/cmake --build /home/shellpry/Documents/west-workspace/wiced-jiobit/WICED-SDK/build

     *  The terminal process "/usr/bin/bash '-c', 'west build --pristine -b hawking_cpuapp apps/jiobit_bare/zephyr'" terminated with exit code: 1.
     *  Terminal will be reused by tasks, press any key to close it.

Related