Beware that this post is related to an SDK in maintenance mode
More Info: Consider nRF Connect SDK for new designs
This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Passing context pointer to NRF_SDH_BLE_OBSERVER

Hi,

I'm using nRF5 SDK v. 14.0.0. I'm trying to pass a context pointer to the NRF_SDH_BLE_OBSERVER macro, but am running into compilation errors. I want to do something like:

void foo(some_struct_t* bar) {
  NRF_SDH_BLE_OBSERVER(m_ble_observer, 1, ble_evt_handler, (void*)bar);
}

but this fails to compile with errors like:

../../../foo.c: In function 'foo':
../../../foo.c:383:60: error: initializer element is not constant
   NRF_SDH_BLE_OBSERVER(m_ble_observer, 1, ble_evt_handler, (void*)bar);
                                                            ^
../../../external/vendor/nRF5_SDK/components/softdevice/common/nrf_sdh_ble.h:90:18: note: in definition of macro 'NRF_SDH_BLE_OBSERVER'
     .p_context = _context                                                                           \
                  ^
../../../foo.c:383:60: note: (near initialization for 'm_ble_observer.p_context')
   NRF_SDH_BLE_OBSERVER(m_ble_observer, 1, ble_evt_handler, (void*)bar);
                                                            ^
../../../external/vendor/nRF5_SDK/components/softdevice/common/nrf_sdh_ble.h:90:18: note: in definition of macro 'NRF_SDH_BLE_OBSERVER'
     .p_context = _context                                                                           \
                  ^
ninja: build stopped: subcommand failed.

Is this intentional? Is it not possible to pass an arbitrary void* pointer as the context pointer to NRF_SDH_BLE_OBSERVER?

If instead of (void*)bar I pass the address of a file-scope static variable then it compiles, but this is not the behavior I want.

Thanks for any advice,

Robbie

Parents
  • Hi,

    I'm not sure I understand what you are trying to achieve. How is this different from passing the address of a static variable?

    If you look at the definition of NRF_SDH_BLE_OBSERVER, you see that the _context parameter is used as initializer for the static variable created. The parameter cannot be variable as you try to do.

    Best regards,
    Jørgen

  • I'm Robbie's colleague: the reason we want it is so that we can set a pointer to a dynamically-computed location in RAM. Or, at least, an address that isn't known to the compiler when compiling the module that calls NRF_SDH_BLE_OBSERVER.

    The alternative is that we have to make a file-scope variable that acts as a trampoline. Sprinking these kinds of trampoline pointers throughout our codebase makes it very hard to protect them using the MWU in case they get scribbled over by a bug.

Reply
  • I'm Robbie's colleague: the reason we want it is so that we can set a pointer to a dynamically-computed location in RAM. Or, at least, an address that isn't known to the compiler when compiling the module that calls NRF_SDH_BLE_OBSERVER.

    The alternative is that we have to make a file-scope variable that acts as a trampoline. Sprinking these kinds of trampoline pointers throughout our codebase makes it very hard to protect them using the MWU in case they get scribbled over by a bug.

Children
No Data
Related