How to send a write response error as a peripheral?

I wrote a reply to a 5-year old question which appeared to be related to my needs but the answer was not what I was looking for.

The case is very straight forward:

The client writes a command on a control point, however, the client has not yet enabled the control point to receive indications.

Consequently, I would like to respond to the BLE_GATTS_EVT_WRITE event with an error. I was hoping for some SoftDevice method like sd_gatts_write_rsp(_) where I could set the error code the client is to receive since I cannot indicate an error message on the control point.

The message sequence diagrams show how I can edit/modify/block a write request from entering crap into my service tables database, but there is no way that I have found to send a reply to the client. I have worked on other libraries where I could do this, in fact I was responsible for setting all the responses to write requests. Maybe SoftDevice is just trying to simplify my life by handling all responses under the hood; implicitly assuming it can handle all possible needs.

I think the error code I want to respond with is 0xFD (Client Characteristic Configuration Descriptor Improperly Configured).

How can I do this (I do not see how the write with authentication option SoftDevice provides helps). There is still no control over the response to the client in that case. I do use that option to control what gets read and could use it to control what gets written. It's not what I need here.

As an aside there is a button to the right with that toggles between 'turn notifications on' or 'turn notifications off'. It is not clear what the state of notifications are given the text displayed. If I see 'off' I am assuming notifications are on and vice versa. It would be clearer with 'notifications are now on' and 'notifications are now off' as the toggle options.

Parents
  • Hi,

    The only case where you can specify the response yourself is when responding to a Write Request with Authorization (as shown in this MCS). You can see an some examples of this in the SDK, for instance in <SDK 17.1.0>\examples\peripheral\usbd_ble_uart_freertos\main.c, around line 515:

                        auth_reply.params.write.gatt_status = APP_FEATURE_NOT_SUPPORTED;
                        err_code = sd_ble_gatts_rw_authorize_reply(p_ble_evt->evt.gatts_evt.conn_handle,
                                                                   &auth_reply);

  • Cool! This is what I was looking for::

    sd_ble_gatts_rw_authorize_reply(....)

    But I did not find the 'reply' option in the sequence diagrams and did not catch it in the list of sd methods. But I will have to shift my writes to rw authorize. (I already do that for reads.)

    !! I have further questions: !!

    This is a control point. When I configure the characteristic for authorized read/writes does that include writing to the CCCD of the characteristic or just when writing to the characteristic itself?

    When I write to the characteristic it is just a command for the server to do something. Do I need to do a reply in that case?

    The only time I want to return an error code to the client is if the client writes a command and has not enabled the CCCD. In that case is the code below sufficient?

    So the only thing I need to enter in the reply is the status code?

        ble_gatts_rw_authorize_reply_params_t reply;
        //reply.type = BLE_GATTS_AUTHORIZE_TYPE_WRITE;
        reply.params.write.gatt_status = BLE_GATT_STATUS_ATTERR_CPS_CCCD_CONFIG_ERROR;
        sd_ble_gatts_rw_authorize_reply(p_ble_evt->evt.common_evt.conn_handle, &reply);

Reply
  • Cool! This is what I was looking for::

    sd_ble_gatts_rw_authorize_reply(....)

    But I did not find the 'reply' option in the sequence diagrams and did not catch it in the list of sd methods. But I will have to shift my writes to rw authorize. (I already do that for reads.)

    !! I have further questions: !!

    This is a control point. When I configure the characteristic for authorized read/writes does that include writing to the CCCD of the characteristic or just when writing to the characteristic itself?

    When I write to the characteristic it is just a command for the server to do something. Do I need to do a reply in that case?

    The only time I want to return an error code to the client is if the client writes a command and has not enabled the CCCD. In that case is the code below sufficient?

    So the only thing I need to enter in the reply is the status code?

        ble_gatts_rw_authorize_reply_params_t reply;
        //reply.type = BLE_GATTS_AUTHORIZE_TYPE_WRITE;
        reply.params.write.gatt_status = BLE_GATT_STATUS_ATTERR_CPS_CCCD_CONFIG_ERROR;
        sd_ble_gatts_rw_authorize_reply(p_ble_evt->evt.common_evt.conn_handle, &reply);

Children
No Data
Related