nRF5 SDK is not maintained anymore
More Info: Consider nRF Connect SDK for new designs
This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Define a BLE service instance on the header file

Hi, 

I'm currently development firmware for a application using a Nordic chip. This application will use some drivers allowing the communication with sensors and for the transmission of the sensor data I will use the BLE. So this application is expected to use 4 BLE services, but right now I only have implemented 3 (BAS, NUS and LLS). For the Battery and Nordic UART Service, I have define the each instance on a header file and everything work as expected. When I implement the Link Loss Service, I have done the same thing (define service macro on the header file) but, for some reason, I can't connect to the BLE. When I define the Link Loss Service instance on the .c file, the BLE work perfectly with the 3 Services. 

There is something special with the Link Loss Service that restrict the instance definition to the .c file?

Thanks in advance!

Parents Reply
  • jpedroliveira said:
    But for some reason, when declaring the instance in the .h file

    I guess maybe you have this .h file included in different .c files? In that case, you will end up with multiple instances of the link loss service structure, but only one will be correctly initialized. This means your program will end up executing a null pointer when processing the connection event and trigger a hardfault exception.

    Edit: just to be clear, the function pointer I'm referring to is '.evt_handler'. I realize the arrow I drew in the picture above can be misleading.

Children
  • Hi! Sorry for the late answer. Regarding your question, after doing some tests I believe that you are correct. The .h file are been called in different .c files. 

    But I have some questions, the others instances are also defined there, why for them there is no problem? And also, can I do something to define the LLS instance one time only (on the .h file)? 

    Thanks

  • Hi, no worries.

    jpedroliveira said:
    But I have some questions, the others instances are also defined there, why for them there is no problem?

    It is problematic to have multiple instances of the service structs even you don't notice any problems right away. The reason the others don't cause a crash in their respective on_connect() implementations is that they happen to include additional checks that will prevent the program from calling the zero-initialized function pointer.

    e.g. here is the on_connect() implementation in ble_nus.c:

    jpedroliveira said:
    And also, can I do something to define the LLS instance one time only (on the .h file)? 

    Then you have to declared the variable as "extern" in your header file.

  • Hi! 

    So I think we can close this ticket! Thank you so much for your help

Related