diff --git a/btle/custom/custom_helper.cpp b/btle/custom/custom_helper.cpp index ea8d98e..5259a7a 100644 --- a/btle/custom/custom_helper.cpp +++ b/btle/custom/custom_helper.cpp @@ -189,6 +189,7 @@ error_t custom_add_in_characteristic(uint16_t service_handle, uint8_t *p_data, uint16_t min_length, uint16_t max_length, + bool writeAuthorization, ble_gatts_char_handles_t *p_char_handle) { /* Characteristic metadata */ @@ -212,7 +213,7 @@ error_t custom_add_in_characteristic(uint16_t service_handle, (char_props.notify || char_props.indicate) ? &cccd_md : NULL; /* Attribute declaration */ - ble_gatts_attr_md_t attr_md = {0}; + ble_gatts_attr_md_t attr_md = {.wr_auth = writeAuthorization}; attr_md.vloc = BLE_GATTS_VLOC_STACK; attr_md.vlen = (min_length == max_length) ? 0 : 1; diff --git a/btle/custom/custom_helper.h b/btle/custom/custom_helper.h index d4fa3cc..4742e66 100644 --- a/btle/custom/custom_helper.h +++ b/btle/custom/custom_helper.h @@ -36,6 +36,7 @@ error_t custom_add_in_characteristic(uint16_t service_handle, uint8_t *p_data, uint16_t min_length, uint16_t max_length, + bool writeAuthorization, ble_gatts_char_handles_t *p_char_handle); error_t custom_add_in_descriptor(uint16_t char_handle, diff --git a/nRF51GattServer.cpp b/nRF51GattServer.cpp index aa38d06..79c0d72 100644 --- a/nRF51GattServer.cpp +++ b/nRF51GattServer.cpp @@ -76,6 +76,7 @@ ble_error_t nRF51GattServer::addService(GattService &service) p_char->getValueAttribute().getValuePtr(), p_char->getValueAttribute().getInitialLength(), p_char->getValueAttribute().getMaxLength(), + p_char->isWriteAuthorizationEnabled(), &nrfCharacteristicHandles[characteristicCount]), BLE_ERROR_PARAM_OUT_OF_RANGE ); @@ -274,6 +275,14 @@ void nRF51GattServer::hwCallback(ble_evt_t *p_ble_evt) sd_ble_gatts_sys_attr_set(gattsEventP->conn_handle, NULL, 0); return; + case BLE_GATTS_EVT_RW_AUTHORIZE_REQUEST: + if (gattsEventP->params.authorize_request.type != BLE_GATTS_AUTHORIZE_TYPE_WRITE) { + return; /* we don't handle anything other than write authorization at the moment */ + } + eventType = GattServerEvents::GATT_EVENT_WRITE_AUTHORIZATION_REQ; + handle_value = gattsEventP->params.authorize_request.request.write.handle; + break; + default: return; } @@ -293,6 +302,25 @@ void nRF51GattServer::hwCallback(ble_evt_t *p_ble_evt) handleDataWrittenEvent(&cbParams); break; } + case GattServerEvents::GATT_EVENT_WRITE_AUTHORIZATION_REQ: { + GattCharacteristicWriteAuthCBParams cbParams = { + .charHandle = i, + .offset = gattsEventP->params.authorize_request.request.write.offset, + .len = gattsEventP->params.authorize_request.request.write.len, + .data = gattsEventP->params.authorize_request.request.write.data, + }; + ble_gatts_rw_authorize_reply_params_t reply = { + .type = BLE_GATTS_AUTHORIZE_TYPE_WRITE, + .params { + .write = { + .gatt_status = (p_characteristics[i]->authorizeWrite(&cbParams) ? + BLE_GATT_STATUS_SUCCESS : BLE_GATT_STATUS_ATTERR_WRITE_NOT_PERMITTED) + } + } + }; + sd_ble_gatts_rw_authorize_reply(gattsEventP->conn_handle, &reply); + break; + } default: handleEvent(eventType, i); break;