How to handle reading long GATT characteristics in nimble?

Cobaltum8
Posts: 1
Joined: Mon Mar 31, 2025 11:37 am

How to handle reading long GATT characteristics in nimble?

Postby Cobaltum8 » Mon Apr 07, 2025 6:36 pm

I want to make a characteristic that returns long data from the log buffer. I wrote such a callback

Code: Select all

#define LOG_RING_BUFFER_SIZE 2048
void create_logs_dump(uint8_t* buffer);

static uint8_t log_buffer[LOG_RING_BUFFER_SIZE];

int log_gatt_chr_access_cb(uint16_t conn_handle,
                           uint16_t attr_handle,
                           struct ble_gatt_access_ctxt* ctxt,
                           void* arg)
{
    assert(ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR);
    const uint16_t mtu = ble_att_mtu(conn_handle);
    ESP_LOGI(TAG, "Read logs chr offset=%u; MTU=%u", ctxt->offset, mtu);

    if (ctxt->offset == 0)
    {
        create_logs_dump(log_buffer);
    }

    size_t to_copy;
    if (LOG_RING_BUFFER_SIZE - ctxt->offset <= mtu)
    {
        to_copy = LOG_RING_BUFFER_SIZE - ctxt->offset;
    }
    else
    {
        to_copy = mtu;
    }

    int rc = os_mbuf_append(ctxt->om, &log_buffer[ctxt->offset], to_copy);

    return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
}
Output in logs
I (533647) LOG_SVC: Read logs chr offset=0; MTU=23
I (533747) LOG_SVC: Read logs chr offset=22; MTU=23

To test queries I use nRF Connect. I don't understand why the data doesn't continue to be read. Maybe I'm doing something completely wrong. I need advice on how to handle long characteristics.

Who is online

Users browsing this forum: Baidu [Spider], Google [Bot] and 1 guest