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

Received indication data corrupt

Hi!

I'm using an NRF51822 as a GATT client to write data to a characteristic. The peripheral responds with several 20 byte indications. When I receive the indications, the length is reported as 20 byte, but the data is incorrect. I logged the data I received and it looks like this:

e1768b1c0020823f0020bc030020000000000180
ca60bb1c0020823f00209c040020000000000180
5231de1c0020823f00200c050020000000000180
7148781c0020823f00207c050020000000000180

Basically, only the first 3 bytes are correct, the remaining 17 bytes are a slightly changing repeating pattern. I would appreciate it if someone could point me in the right direction what the source of this issue could be. Thank you very much!

Parents
  • Hi,

    It is hard to tell without the source, but it sounds like something that could happen if the variable holding the data, either on the sending or receiving side, goes out of scope. Since the APIs work with pointers, it is important that the data is not placed somewhere in memory that gets popped off the stack once the function returns. That usually means the variable holding the data (in both ends) should be a "static uint8_t" array.

    Regards,
    Terje

  • Thank you for your answer. I excluded the sending side as the source of the error, because I have a working communication with a mobile and linux application. I've suspect it's a memory layout problem or something with the interrupts of the softdevice, because the data is already corrupt when I read it from the hvx event.

    I've stripped the code down to the relevant bits:

    static uint32_t progress = 0;
    static uint8_t received_data[80];
    
    static void on_ble_evt(ble_evt_t * p_ble_evt)
    {
        uint32_t              err_code = 0;
        const ble_gap_evt_t * p_gap_evt = &p_ble_evt->evt.gap_evt;
    
        switch (p_ble_evt->header.evt_id)
        {
            case BLE_GATTC_EVT_HVX:
                ;const ble_gattc_evt_hvx_t hvx_evt = p_ble_evt->evt.gattc_evt.params.hvx;
                if(app_state == as_RECEIVE_DATA) {
                    memcpy((void*)(&received_data[progress]), hvx_evt.data, hvx_evt.len);
                    NRF_LOG_INFO("Received indication data:\r\n");
                        for(int i = 0; i < hvx_evt.len; i++) {
                            NRF_LOG_RAW_INFO("%02x", hvx_evt.data[i]);
                    }
                    NRF_LOG_INFO("\r\n");
                    progress += hvx_evt.len;
                }
                if(hvx_evt.type == BLE_GATT_HVX_INDICATION) {
                    err_code = sd_ble_gattc_hv_confirm(connection_handle, hvx_evt.handle);
                    APP_ERROR_CHECK(err_code);
                }
                break;
        }
    }

    Edit: I've written a small test case using another controller which sends out the string "Hello" via indication. On my receiving nRF51822, I only get He???, so I'm confident that the issue lies in the receiving end.

  • Hi,

    You should not declare the variables for the ble_gap_evt_t pointer and the ble_gattc_evt_hvx_t event structure const. It looks like removing the const keywords on those two lines should make things work as intended.

    Regards,
    Terje

Reply Children
Related