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

INVALID_PARAM for sd_ble_gatts_rw_authorize_reply

Hi,

I've recently hit an issue where sd_ble_gatts_rw_authorize_reply in my code returns INVALID_PARAM when I try and write a long set of data to a characteristic using the NRF Connect app. Shorter data works perfectly.

It's using totally standard code from Nordic's examples:

  case BLE_GATTS_EVT_RW_AUTHORIZE_REQUEST:
  {
      ble_gatts_evt_rw_authorize_request_t  req;
      ble_gatts_rw_authorize_reply_params_t auth_reply;

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

      if (req.type != BLE_GATTS_AUTHORIZE_TYPE_INVALID)
      {
          if ((req.request.write.op == BLE_GATTS_OP_PREP_WRITE_REQ)     ||
              (req.request.write.op == BLE_GATTS_OP_EXEC_WRITE_REQ_NOW) ||
              (req.request.write.op == BLE_GATTS_OP_EXEC_WRITE_REQ_CANCEL))
          {
              if (req.type == BLE_GATTS_AUTHORIZE_TYPE_WRITE)
              {
                  auth_reply.type = BLE_GATTS_AUTHORIZE_TYPE_WRITE;
              }
              else
              {
                  auth_reply.type = BLE_GATTS_AUTHORIZE_TYPE_READ;
              }
              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);
              APP_ERROR_CHECK(err_code);
          }
      }
  } break; // BLE_GATTS_EVT_RW_AUTHORIZE_REQUEST

edit: Simply download the Nordic UART example firmware and follow the steps below. It has exactly the same problem

It's using one service - the standard Nordic UART using Nordic's NUS library, but I'm doing that with Espruino. It's trivial to reproduce - simply download the nrf52832dk hex file from www.espruino.com/.../, connect with nRF Connect on Android, and then go to the NUS RX characteristic and send one really big packet - it reboots with an error.

(You can see the error better if you connect via the USB UART at 9600 baud first and send Serial1.setConsole(1), then connect with nRF Connect, then send the big packet)

Any ideas what I might be doing wrong? I'm using s132_nrf52_3.0.0_softdevice.hex (SDK 12.1) at the moment. I know that's not the current one but it'll be a pain to change.

If I create a second service and characteristic and write a lot of data into that I get exactly the same problem as well.

thanks!

Parents
  • The problem is that the SDK NUS example does not handle "Cancel All Prepared Writes" in the Execute Write Request command properly. In this case, the call to sd_ble_gatts_rw_authorize_reply() must use BLE_GATT_STATUS_SUCCESS, which is the only legal value (it is not legal to not accept "Cancel All Prepared Writes"). The following fix is for SDK 13, but it should be very similar on older SDKs (ble_app_uart_main.diff):

    diff --git a/examples/ble_peripheral/ble_app_uart/main.c b/examples/ble_peripheral/ble_app_uart/main.c
    index ed651a7..c5efd3f 100644
    --- a/examples/ble_peripheral/ble_app_uart/main.c
    +++ b/examples/ble_peripheral/ble_app_uart/main.c
    @@ -389,7 +389,15 @@ static void on_ble_evt(ble_evt_t * p_ble_evt)
                         {
                             auth_reply.type = BLE_GATTS_AUTHORIZE_TYPE_READ;
                         }
    -                    auth_reply.params.write.gatt_status = APP_FEATURE_NOT_SUPPORTED;
    +                    
    +                    if (req.request.write.op == BLE_GATTS_OP_EXEC_WRITE_REQ_CANCEL)
    +                    {
    +                        auth_reply.params.write.gatt_status = BLE_GATT_STATUS_SUCCESS;
    +                    }
    +                    else
    +                    {
    +                        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);
                         APP_ERROR_CHECK(err_code);
    
  • Hello, I faced a similar problem which was solved by the above snippet of code. However, the device gets stuck after generating BLE_GATTS_OP_PREP_WRITE_REQ and BLE_GATTS_OP_EXEC_WRITE_REQ_CANCEL consecutively and BLE_GATTS_EVT_WRITE is never called. 

    How do I write the data after authorization is complete? 

    I am using nRF5_SDK_14.0.0_3bcc1f7 and s132_nrf52_5.0.0_softdevice.hex

    Thanks!

  • Hi!

    Could you make a new ticket on DevZone, since this ticket is over two years old.

    Best regards,

    Heidi

Reply Children
Related