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

ble_app_template

Hello. I just started learn ble module and some confusing about ble_app_uart:

  1. in file ble_nus.c we have function on_write this function calling when trigerred BLE_GATTS_EVT_WRITE and analyze what kind of handle we got. If there's tx handle we do something misunderstanding for me

    p_nus->data_handler(p_nus, p_evt_write->data, p_evt_write->len);

We calling it's like function and it's confused to me. Is it like some type of template?

  1. In any case in that example I understand where and how I can use value from ble:

    static void nus_data_handler(ble_nus_t * p_nus, uint8_t * p_data, uint16_t length)

I can't find similar to that function in ble_app_template example. Where I can handle gotten value there?

Parents
  • Hi,

    1. Here we use something called function pointers. The function pointer type is declared as

      typedef void (*ble_nus_data_handler_t) (ble_nus_t * p_nus, uint8_t * p_data, uint16_t length); and in the p_nus struct, we have the data_handler variable that is of the above function pointer type

      { uint8_t uuid_type; /< UUID type for Nordic UART Service Base UUID. */ uint16_t service_handle; /< Handle of Nordic UART Service (as provided by the SoftDevice). */ ble_gatts_char_handles_t tx_handles; /< Handles related to the TX characteristic (as provided by the SoftDevice). */ ble_gatts_char_handles_t rx_handles; /< Handles related to the RX characteristic (as provided by the SoftDevice). / uint16_t conn_handle; /< Handle of the current connection (as provided by the SoftDevice). BLE_CONN_HANDLE_INVALID if not in a connection. */ bool is_notification_enabled; /< Variable to indicate if the peer has enabled notification of the RX characteristic./ ble_nus_data_handler_t data_handler; /**< Event handler to be called for handling received data. */ };

    In the main.c function services_init() we initialize the service structure, and point to the actual function that will be executed when p_nus->data_handler(...) is called:

    nus_init.data_handler = nus_data_handler;
    err_code = ble_nus_init(&m_nus, &nus_init);
    
    
    uint32_t ble_nus_init(ble_nus_t * p_nus, const ble_nus_init_t * p_nus_init)
    {
    ..
    p_nus->data_handler            = p_nus_init->data_handler;
    ...
    }
    

    So when p_nus->data_handler(p_nus, p_evt_write->data, p_evt_write->len); is called, the function that will be called is nus_data_handler():

    static void nus_data_handler(ble_nus_t * p_nus, uint8_t * p_data, uint16_t length)
    {
        for (uint32_t i = 0; i < length; i++)
        {
            while (app_uart_put(p_data[i]) != NRF_SUCCESS);
        }
        while (app_uart_put('\r') != NRF_SUCCESS);
        while (app_uart_put('\n') != NRF_SUCCESS);
    }
    
    1. With the template function you have to write your own handler depending on what characteristic and services you are implementing.
Reply Children
No Data
Related