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

sd_ble_gatts_value_get

nrf52832.

SDK14.2

Segger ES V3.34a

I am using sd_ble_gatts_value_get, in C file named ble_cus.c to retrieve the 'value' of a characteristic as written to by the nrf-connect APP.

The 'value' is a short string, saved into a buffer which can be successfully passed to the NRF_LOG_INFO.  "Great!"

 How do I pass/share this string buffer so that it an be accessed in the main.c?

I have tried declaring the buffer in the header file ble.cus.h as an extern and then including ble.cus.h in my main.c file.

**********************************************

ble.cus.h 

extern char data_buffer[];

**********************************************

**********************************************

ble_cus.c


uint16_t i = 0;                            //reset the counter

char data_buffer[3];

ble_gatts_value_t rx_data;

// Initialize value struct.
memset(&rx_data, 0, sizeof(rx_data));

rx_data.offset = 0; //reset back to zero
rx_data.len = 3;    //  I only send 3 chars from nrf connect
//rx_data.p_value = &data_buffer;
rx_data.p_value = (uint8_t*) &data_buffer;



sd_ble_gatts_value_get(p_ble_evt->evt.gatts_evt.conn_handle, p_cus->org_handles.value_handle, &rx_data);

for(i = 0; i < 3; i++)
{
NRF_LOG_INFO("%02x",data_buffer[i]);  

}

**********************************************************

**********************************************************

main.c

#include blue_cus.h

uint16_t i = 0;

for(i = 0; i < 3; i++)
{
NRF_LOG_INFO("%02x", &data_buffer[i]);

}

*********************************************************

ERROR on Build

main.c:1459: undefined reference to `data_buffer'

If I comment out NRF_LOG_INFO("%02x", &data_buffer[i]); from my main.c there are no error and the NRF_LOG_INFO("%02x", &data_buffer[i]); in ble_cus.c spits out the data to the Output screen.

How else can i share this data, to my main.c?

Parents
  • Hi,

    You need to declare it with size in the header file, like this (where it seems SIZE is 3 in your case):

    extern char data_buffer[SIZE];

    Then in one of the implementation (.c) files you actually create the array:

    char data_buffer[SIZE];

    In other files where you need to access it you either include the header file where it is external, or simply declare it as an extern before you use it (using the same declaration as suggested for use in the header file).

     

Reply
  • Hi,

    You need to declare it with size in the header file, like this (where it seems SIZE is 3 in your case):

    extern char data_buffer[SIZE];

    Then in one of the implementation (.c) files you actually create the array:

    char data_buffer[SIZE];

    In other files where you need to access it you either include the header file where it is external, or simply declare it as an extern before you use it (using the same declaration as suggested for use in the header file).

     

Children
  • Hi,

    I have made the following changes, as per your suggestions

    *************************************************
    ble_cus.h

    #define SIZE 3

    extern char data_buffer[SIZE]; //Definition of the org buffer
    *************************************************

    **********************************************
    ble_cus.c


    uint16_t i = 0; //reset the counter
    char data_buffer[3];


    ble_gatts_value_t rx_data;

    // Initialize value struct.
    memset(&rx_data, 0, sizeof(rx_data));

    rx_data.offset = 0; //reset back to zero
    rx_data.len = 3; // I only send 3 chars from nrf connect
    rx_data.p_value = (uint8_t*) &data_buffer;

    sd_ble_gatts_value_get(p_ble_evt->evt.gatts_evt.conn_handle, p_cus->org_handles.value_handle, &rx_data);

    for(i = 0; i < 3; i++)
    {
    NRF_LOG_INFO("%02x",data_buffer[i]);

    }
    **********************************************************

    **********************************************************
    main.c

    #include blue_cus.h

    extern char data_buffer[3];

    uint16_t i = 0;


    for(i = 0; i < 3; i++)
    {
    NRF_LOG_INFO("%02x", &data_buffer[i]);

    }
    *********************************************************

    Same error as before.
    main.c:1460: undefined reference to `data_buffer'

  • The "undefined reference to" error comes from the linker and indicates that it cannot find the data_buffer, so it is not compiled for some reason (or optimized away). Can you verify that you actually build your ble_cus.c file?

  • Hi Einar,

    I got it to work by declaring my buffer 'extern char data_buffer[]' in the header file ble_cus.h.

    But my mistake was to define the buffer in the ble_cus.c file.

    I changed my  define to my main.c - 'char data_buffer[3];' and included the ble_cus.h file in both 'c' files.

    Thanks for the help, I was banging my head against a brick wall with this problem.  

Related