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

sd_ble_gatts_rw_authorize_reply() sends "invalid handle" in the air after queued write

I am trying to implement the following sequence, using SD s130 :

GATTS Queued Writes: Stack handled, one or more attributes require authorization (var #1)

First thing, the chart looks wrong on one small detail: the first "Prepare Write Req" is triggering a BLE_EVT_USER_MEM_REQUEST and a BLE_GATTS_EVT_RW_AUTHORIZE_REQUEST just after in my code, but according to the chart, only the first event is triggered, which is not very coherent. I suppose the chart is missing a few arrows here.

But my real problem happens later. My device receives 5 differents "Prepare write Req" (all on handle 57, total len: 83) and one "Execute Write Request". My firmware receives then six BLE_GATTS_EVT_RW_AUTHORIZE_REQUEST. In some version of my code, all of them are replied immediately with "sd_ble_gatts_rw_authorize_reply(WRITE, SUCCESS)" and everything is fine.

In another version, the first five ones are replied immediately, but the last one (after the Exec Write Req) is sent after a while (~1s), and in that case, the internal return code is successful, but I can see in the air that the SD is answering an error with "invalid handle" and "handle = 0x0202".

I cannot understand why this error is sent, and this is blocking me.

Maybe this is a coincidence, but the data received in the first "Prepare Write Req" is also starting with 0x0202.

At some point, I was sending a notification with sd_ble_gatts_hvx() after receiveing the Exec Write Req and before calling the related sd_ble_gatts_rw_authorize_reply(), and I thought it was the cause of the bug (but why the return code was successful?), but after reworking my code and removing this call, the problem is still occurring. I suppose there is no other call meanwhile to the SD, but I am not totally sure. At least there is no superfluous sd_ble_gatts_rw_authorize_reply(). The only difference between the working and the failing versions seems to be the delay (and of course I need this delay to check in detail the content of the queud write).

Also, there is no ATT packets in the air between the "Exec Write Req" and the error response.

The exact content of the Error response is:

  • Opcode: Error Response
  • Requested Opcode: Execute Write Request
  • Attribute handle in error: 514 / 0x0202
  • Error code: Invalid Handle 0x1

Is there any detailed doc about all sd_ble_gatts_rw_authorize_reply() constraints?

IMPORTANT NOTE: while writing this question, I just thought about the fact that I am modifying the buffer containing the queued write (the one given for BLE_EVT_USER_MEM_REQUEST) to make my processing easier. I think this is the cause of my problem. I still post this message, as it may help other people, since I do not think it is explained anywhere in the doc...

Related