This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Calling bt_le_adv_start() from a C++ module does not compile

I have cloned the BLE code from the periperal_uart example provided in the SDK.  However, I am using C++ so I am perform my BLE initialization inside a .cpp file (i.e. main.cpp).  The problem is how the SDK's internal BT_LE_ADV_PARAM_INIT() macro expands when calling the bt_le_adv_start() method.  The macro generates an invalid C++ construct - see the compiler error below.  And yes - I have tried moving the offending code to a C file - and it compiles.  However, I am not comfortable with this work around since while it compiles - I am not sure the actual generated code will be valid for C++ app (because the array made not really be in scope even though it compiled under my hybrid .c/.cpp files).

Is this a know bug with the SDK?  If yes - is there a timeline for a fix?

../src/main.cpp: In function 'void main()':
C:/compilers/nordic-nrfconnect/zephyr/include/bluetooth/bluetooth.h:612:30: error: taking address of temporary array
612 | ((struct bt_le_adv_param[]) { \
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~
613 | BT_LE_ADV_PARAM_INIT(_options, _int_min, _int_max, _peer) \
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
614 | })
| ~~
C:/compilers/nordic-nrfconnect/zephyr/include/bluetooth/bluetooth.h:621:24: note: in expansion of macro 'BT_LE_ADV_PARAM'
621 | #define BT_LE_ADV_CONN BT_LE_ADV_PARAM(BT_LE_ADV_OPT_CONNECTABLE, \
| ^~~~~~~~~~~~~~~
../src/main.cpp:108:28: note: in expansion of macro 'BT_LE_ADV_CONN'
108 | err = bt_le_adv_start( BT_LE_ADV_CONN, ad, ARRAY_SIZE( ad ), sd, ARRAY_SIZE( sd ) );
| ^~~~~~~~~~~~~~
[89/283] Building C object modules/hal_nordic/CMakeFiles/modules__ha...dic-nrfconnect/modules/hal/nordic/nrfx/drivers/src/nrfx_gpiote.c.obj
ninja: build stopped: subcommand failed.

Source Code snippet:

==============================

#define DEVICE_NAME CONFIG_BT_DEVICE_NAME
#define DEVICE_NAME_LEN (sizeof(DEVICE_NAME) - 1)

static const struct bt_data ad[] = {
BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)),
BT_DATA(BT_DATA_NAME_COMPLETE, DEVICE_NAME, DEVICE_NAME_LEN),
};

static const struct bt_data sd[] = {
BT_DATA_BYTES(BT_DATA_UUID128_ALL, BT_UUID_NUS_VAL),
};

....

// Start advertising
err = bt_le_adv_start( BT_LE_ADV_CONN, ad, ARRAY_SIZE( ad ), sd, ARRAY_SIZE( sd ) );
if ( err )

Related