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

BLE C++ Wrapper

I am trying to wrap BLE into a nice c++ class. For this i am using ble_app_uart example. I think it was working fine until i was up to clean the helper functions. After that i got error_code: 8 at SOFTDEVICE_HANDLER_INIT(&clock_lf_cfg, NULL);.

Here is a full example. ble_app_uart_modified.rar

In file ble_wrapper.cpp:626 when i comment command ble_advertising_start there is no error.

I can't seem to find the reason. Is is possible that this is connected to C++ class? Are there some other ideas? Is there a way to narrow the issue?

Thanks.

Parents
  • Inside ble_advertising_start() there is a call to m_evt_handler(m_adv_evt):588. In advertising_init(), ble_advertising_init() is called with the parameter static_on_adv_evt, which you have implemented as follows:

    void BLEWrapper::static_on_adv_evt(ble_adv_evt_t ble_adv_evt)
    {
      /* static implementation of on_adv_evt for use with sdk callback function */
      BLEWrapper::getInstance()->on_adv_evt(ble_adv_evt);
    }
    

    Since your call to BLEWrapper::getInstance() from main.cpp have not completed, _pInstance have not yet been set, and the check in getInstance():

    if(_pInstance == NULL)
    {
      _pInstance = new BLEWrapper();
    }
    

    will evaluate to true. This will create an infinite loop of new BLEWrapper-instances, and softdevice initialization will fail with error code 8, since it is allready initialized.

    You need to refactor your code to start advertising after init, or use another method of getting the instance.

    Best regards,

    Jørgen

Reply
  • Inside ble_advertising_start() there is a call to m_evt_handler(m_adv_evt):588. In advertising_init(), ble_advertising_init() is called with the parameter static_on_adv_evt, which you have implemented as follows:

    void BLEWrapper::static_on_adv_evt(ble_adv_evt_t ble_adv_evt)
    {
      /* static implementation of on_adv_evt for use with sdk callback function */
      BLEWrapper::getInstance()->on_adv_evt(ble_adv_evt);
    }
    

    Since your call to BLEWrapper::getInstance() from main.cpp have not completed, _pInstance have not yet been set, and the check in getInstance():

    if(_pInstance == NULL)
    {
      _pInstance = new BLEWrapper();
    }
    

    will evaluate to true. This will create an infinite loop of new BLEWrapper-instances, and softdevice initialization will fail with error code 8, since it is allready initialized.

    You need to refactor your code to start advertising after init, or use another method of getting the instance.

    Best regards,

    Jørgen

Children
Related