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

During BLE_GATTS_EVT_RW_AUTHORIZE_REQUEST: req.request.write.data is corrupted.

Hello all,

Please help!

I am using nrf52480 with SDK15.

I have been developing this application for quite some time now and this is a new problem.  I am using BLE_GATTS_EVT_RW_AUTHORIZE_REQUESTs for all of my characteristics.  When I send a request_write the length is correct but only the first 2 bytes of data are correct.  The rest is the same value of 020000000000000000000000000000000000000000000000980604001996 every time on any characteristic.  I am assuming that I am somehow overwriting the write request data value but I don't know how to debug this.  The part that confuses me even more is that the application is receiving the correct values and my application works find until I read from the characteristic again, then the data is garbage.  If my application is getting the right value then why would the value of the characteristic be garbage?  Or am I just not able to access the data in the request data value before replying with success?  Here is how I handle rw_request.

    ret_code_t err_code;
    ble_gatts_evt_rw_authorize_request_t  req;
    ble_gatts_rw_authorize_reply_params_t auth_reply;
    memset(&auth_reply, 0, sizeof(auth_reply));

    req = p_ble_evt->evt.gatts_evt.params.authorize_request;

    if (req.type != BLE_GATTS_AUTHORIZE_TYPE_INVALID)
    {
        if (req.type == BLE_GATTS_AUTHORIZE_TYPE_WRITE)
        {
            auth_reply.type = BLE_GATTS_AUTHORIZE_TYPE_WRITE;
            NRF_LOG_RAW_INFO("REQUEST WRITE VALUE: ");
            for(int i = 0; i < req.request.write.len; i++)
                NRF_LOG_RAW_INFO("%02x",*(req.request.write.data+i));
        }
        else
        {
            auth_reply.type = BLE_GATTS_AUTHORIZE_TYPE_READ;
        }
    }
    
    if (is_secure_characteristic_hash_valid)
    {
        NRF_LOG_INFO("Connection is SECURE");
        if (req.type == BLE_GATTS_AUTHORIZE_TYPE_WRITE)
        {
            auth_reply.params.write.gatt_status = BLE_GATT_STATUS_SUCCESS;
            auth_reply.params.write.len = req.request.write.len;
            auth_reply.params.write.p_data = req.request.write.data;
            auth_reply.params.write.update = 1;
            auth_reply.params.write.offset = 0;
        }
        else
        {
           NRF_LOG_RAW_INFO("READ VALUE: ");
           auth_reply.params.read.gatt_status = BLE_GATT_STATUS_SUCCESS;
           auth_reply.params.read.update = 0;
           //auth_reply.params.read.offset = 0;
        }
        err_code = sd_ble_gatts_rw_authorize_reply(p_ble_evt->evt.gatts_evt.conn_handle,
                                                   &auth_reply);
        APP_ERROR_CHECK(err_code);
    }

This is a new problem and did not exist in the old versions of my firmware.  Now that it is occurring even when I step back to older versions is seems to be occurring.  Did I permanently overwrite something on my device?  I have done "nrfjprog -e" and reflashed everything numerous times, reinstalled the lightblue app on my phone, and stepped back to older versions of my code.  I am at a total loss and will take any advice.

Thanks in advanced,

Bloq

  • Hello,

    The fact that the data is jibberish suggests that either you are reading from the wrong location, or you are using the wrong event. From where do you call this snippet?

    Assuming it is called in the ble_evt_handler, then you should first check the type of event:

    ble_evt_handler(p_ble_evt, ...)
    {
        switch (p_ble_evt->header.evt_id)
        {
            case BLE_GATTS_EVT_RW_AUTHORIZE_REQUEST:
            // Your code here.
            break;
        }
        ...
    }

    Di you check this?

    If it is the correct event, it the data should be stored in:

    p_ble_evt->evt.gatts_evt.params.authorize_request.request.write.data[i];

    Try printing it like this:

    for(int i = 0; i < req.request.write.len; i++)
    {
        NRF_LOG_RAW_INFO("%02x",req.request.write.data[i]));
    }
    NRF_LOG_RAW_INFO("\r\n"); // For good measure.

    Perhaps you were only printing the addresses of the data, or that it was the wrong type of event you were printing in.

    Let me know if it works or not.

    Best regards,

    Edvin

  • Yes I am handling the BLE_GATTS_EVT_RW_AUTHORIZE_REQUEST and have been for quite some time now.  I tried the new print, works the same.  Still getting 020000000000000000000000... from the 3rd byte on when writing a much longer value to any of my characteristics.  Any other ideas? thanks Bloq

  • Have you tried to sniff the connection? Perhaps that is actually the value being sent? What device is sending this data? What are you connected to? A phone? Another nRF running a central application? If you have not already, please try to use nRF Connect for iOS/Android and send the write requests from there.

    Is there any way for me to reproduce the issue that you are seeing using an nRF52832/nRF52840 DK?

    Best regards,

    Edvin

Related