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

tx_buffer_process

Can someone explain the proper way to use the tx_buffer_process function present in many of the service libraries.

It seems this function is used to handle requests to read/write the ble service, but I'm not entirely clear on how it's being used. It seems to me that the cccd_configure() function for example is adding a write command to the buffer and then calling tx_buffer process.

I've modified this function to write to the characteristic value instead of the CCCD, but when I call tx_buffer_process, I get an NRF_BUSY error. I see that the tx_buffer_process function will not advance the m_tx_index in this case, but I don't see anywhere in the sample where the tx_buffer_process is called again to keep attempting to send out any pending messages.

static uint32_t value_set(uint16_t conn_handle, uint16_t handle_value)
{
    tx_message_t * p_msg;
    p_msg              = &m_tx_buffer[m_tx_insert_index++];     /* Set pointer to buffer tail */
    
    m_tx_insert_index &= TX_BUFFER_MASK;                        /* Increment buffer tail */
    
    
    /* Connection handle + message type (R/W) */
    p_msg->conn_handle                         = conn_handle;       /* Set connection handle for message */
    p_msg->type                                = WRITE_REQ;         /* Message type = write */
    
    /* Setup data to be written */
    p_msg->req.write_req.gattc_value[0]        = 0xAF;
    
    /* Write Parameters */
    p_msg->req.write_req.gattc_params.write_op = BLE_GATT_OP_WRITE_REQ;
    p_msg->req.write_req.gattc_params.handle   = handle_value;      /* Writing to characteristic value */
    p_msg->req.write_req.gattc_params.offset   = 0;
    p_msg->req.write_req.gattc_params.len      = 1;                 /* Length of value */
    p_msg->req.write_req.gattc_params.p_value  = p_msg->req.write_req.gattc_value;      /* Pointer to Write Data */
    
    return tx_buffer_process();        /* Send the message */
}

tx_buffer_process from ble_bas_c.c

/**@brief Function for passing any pending request from the buffer to the stack.
 */
static void tx_buffer_process(void)
{
    if (m_tx_index != m_tx_insert_index)
    {
        uint32_t err_code;

        if (m_tx_buffer[m_tx_index].type == READ_REQ)
        {
            err_code = sd_ble_gattc_read(m_tx_buffer[m_tx_index].conn_handle,
                                         m_tx_buffer[m_tx_index].req.read_handle,
                                         0);
        }
        else
        {
            err_code = sd_ble_gattc_write(m_tx_buffer[m_tx_index].conn_handle,
                                          &m_tx_buffer[m_tx_index].req.write_req.gattc_params);
        }
        if (err_code == NRF_SUCCESS)
        {
            LOG("[HRS_C]: SD Read/Write API returns Success..\r\n");
            m_tx_index++;
            m_tx_index &= TX_BUFFER_MASK;
        }
        else
        {
            LOG("[HRS_C]: SD Read/Write API returns error. This message sending will be "
                "attempted again..\r\n");
        }
    }
}
Related