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

Issue triggering event when receiving BLE data

Hey all I'm having [another] issue I just can't seem to crack. I think this is just down to me not understanding exactly how this works. I'm trying to trigger an event, or events, when a GATT write happens from the client to my app.

I'm following the blinky app, and I thought I was in good shape but the event never triggers, let alone, gets data.

I started off adding new write_handler to the ble_xxx_s structure, and creating typedef functions for them:

typedef void (*ble_ctcws_samples_write_handler_t) (uint16_t conn_handle, ble_ctcws_t * p_ctcws, uint8_t new_state);
typedef void (*ble_ctcws_config_write_handler_t) (uint16_t conn_handle, ble_ctcws_t * p_ctcws, uint8_t new_state);
typedef void (*ble_ctcws_status_write_handler_t) (uint16_t conn_handle, ble_ctcws_t * p_ctcws, uint8_t new_state);

struct ble_ctcws_s
{
    uint16_t                            config_service_handle;  /**< Handle of CTC Wireless Service (as provided by the BLE stack). */
    uint16_t                            data_service_handle;
    ble_gatts_char_handles_t            config_char_handles;      /**< Handles related to the config Characteristic. */
    ble_gatts_char_handles_t            samples_char_handles;    /**< Handles related to the samples Characteristic. */
    ble_gatts_char_handles_t            mem_size_char_handles;
    ble_gatts_char_handles_t            status_char_handles;
    ble_gatts_char_handles_t            data_char_handles;
    uint8_t                             uuid_type;              /**< UUID type for the CTC Wireless Service. */
    ble_ctcws_config_write_handler_t    config_write_handler;     /**< Event handler to be called when the Characteristic is written. */
    ble_ctcws_samples_write_handler_t   samples_write_handler;
    ble_ctcws_status_write_handler_t    status_write_handler;
    bool                                is_notification_supported;
    uint8_t                             initial_data_value;
    uint32_t                            initial_mem_value;
};

Then modified on_write function in ble_xxx.c:

static void on_write(ble_ctcws_t * p_ctcws, ble_evt_t const * p_ble_evt)
{
    ble_gatts_evt_write_t const * p_evt_write = &p_ble_evt->evt.gatts_evt.params.write;

    //take care of a write to config
    if ((p_evt_write->handle == p_ctcws->config_char_handles.value_handle)
        && (p_ctcws->config_write_handler != NULL))
    {
        p_ctcws->config_write_handler(p_ble_evt->evt.gap_evt.conn_handle, p_ctcws, p_evt_write->data[0]);
    }

    //take care of a write to samples
    if ((p_evt_write->handle == p_ctcws->samples_char_handles.value_handle)
        && (p_ctcws->samples_write_handler != NULL))
    {
        p_ctcws->samples_write_handler(p_ble_evt->evt.gap_evt.conn_handle, p_ctcws, p_evt_write->data[0]);
    }

    //take care of a write to samples
    if ((p_evt_write->handle == p_ctcws->status_char_handles.value_handle)
        && (p_ctcws->status_write_handler != NULL))
    {
        p_ctcws->status_write_handler(p_ble_evt->evt.gap_evt.conn_handle, p_ctcws, p_evt_write->data[0]);
    }
}


void ble_ctcws_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_context)
{
    ble_ctcws_t * p_ctcws = (ble_ctcws_t *)p_context;

    switch (p_ble_evt->header.evt_id)
    {
        case BLE_GATTS_EVT_WRITE:
            on_write(p_ctcws, p_ble_evt);
            printf("a write happend!\r\n");
            break;

        default:
            // No implementation needed.
            break;
    }
    
}

I never even get to the printf("A write happend\r\n"); in ble_ctcws_on_ble_evt, so I think it's something wrong before this.

I added the three handler functions to my main program but obviously never get there either.

Any thoughts on why this isn't triggering, and how I should go about getting a string from the 'write' to the function in my main?

Thanks

Parents
  • Hi,

    Are you getting any events at all in ble_ctcws_on_ble_evt() ? If you place a breakpoint at the function entry, do you hit the breakpoint? Did you register the function with NRF_SDH_BLE_OBSERVER() ?

    Something like this ?

    #define BLE_CTCWS_DEF(_name)                                                                          \
    static ble_ctcws_t _name;                                                                             \
    NRF_SDH_BLE_OBSERVER(_name ## _obs,                                                                 \
                         BLE_CTCWS_BLE_OBSERVER_PRIO,                                                     \
                         ble_ctcws_on_ble_evt, &_name)

    where BLE_CTCWS_BLE_OBSERVER_PRIO is e.g. 2

    And then in main.c, you have something like this:

    BLE_CTCWS_DEF(m_ctcws);    /**< My Service instance. */

    I would recommend that you take a look at this tutorial on how to create custom services.

Reply
  • Hi,

    Are you getting any events at all in ble_ctcws_on_ble_evt() ? If you place a breakpoint at the function entry, do you hit the breakpoint? Did you register the function with NRF_SDH_BLE_OBSERVER() ?

    Something like this ?

    #define BLE_CTCWS_DEF(_name)                                                                          \
    static ble_ctcws_t _name;                                                                             \
    NRF_SDH_BLE_OBSERVER(_name ## _obs,                                                                 \
                         BLE_CTCWS_BLE_OBSERVER_PRIO,                                                     \
                         ble_ctcws_on_ble_evt, &_name)

    where BLE_CTCWS_BLE_OBSERVER_PRIO is e.g. 2

    And then in main.c, you have something like this:

    BLE_CTCWS_DEF(m_ctcws);    /**< My Service instance. */

    I would recommend that you take a look at this tutorial on how to create custom services.

Children
Related