library for BLE_GAP_backpack
Dependencies: nrf51-sdk
Fork of nRF51822 by
nRF5xGattServer.cpp
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2006-2013 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #include "nRF5xGattServer.h" 00018 #ifdef YOTTA_CFG_MBED_OS 00019 #include "mbed-drivers/mbed.h" 00020 #else 00021 #include "mbed.h" 00022 #endif 00023 00024 #include "common/common.h " 00025 #include "btle/custom/custom_helper.h" 00026 00027 #include "nRF5xn.h" 00028 00029 /**************************************************************************/ 00030 /*! 00031 @brief Adds a new service to the GATT table on the peripheral 00032 00033 @returns ble_error_t 00034 00035 @retval BLE_ERROR_NONE 00036 Everything executed properly 00037 00038 @section EXAMPLE 00039 00040 @code 00041 00042 @endcode 00043 */ 00044 /**************************************************************************/ 00045 ble_error_t nRF5xGattServer::addService(GattService &service) 00046 { 00047 /* ToDo: Make sure this service UUID doesn't already exist (?) */ 00048 /* ToDo: Basic validation */ 00049 00050 /* Add the service to the nRF51 */ 00051 ble_uuid_t nordicUUID; 00052 nordicUUID = custom_convert_to_nordic_uuid(service.getUUID()); 00053 00054 uint16_t serviceHandle; 00055 ASSERT( ERROR_NONE == 00056 sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY, 00057 &nordicUUID, 00058 &serviceHandle), 00059 BLE_ERROR_PARAM_OUT_OF_RANGE ); 00060 service.setHandle(serviceHandle); 00061 00062 /* Add characteristics to the service */ 00063 for (uint8_t i = 0; i < service.getCharacteristicCount(); i++) { 00064 if (characteristicCount >= BLE_TOTAL_CHARACTERISTICS) { 00065 return BLE_ERROR_NO_MEM; 00066 } 00067 GattCharacteristic *p_char = service.getCharacteristic(i); 00068 00069 /* Skip any incompletely defined, read-only characteristics. */ 00070 if ((p_char->getValueAttribute().getValuePtr() == NULL) && 00071 (p_char->getValueAttribute().getLength() == 0) && 00072 (p_char->getProperties() == GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ)) { 00073 continue; 00074 } 00075 00076 nordicUUID = custom_convert_to_nordic_uuid(p_char->getValueAttribute().getUUID()); 00077 00078 /* The user-description descriptor is a special case which needs to be 00079 * handled at the time of adding the characteristic. The following block 00080 * is meant to discover its presence. */ 00081 const uint8_t *userDescriptionDescriptorValuePtr = NULL; 00082 uint16_t userDescriptionDescriptorValueLen = 0; 00083 for (uint8_t j = 0; j < p_char->getDescriptorCount(); j++) { 00084 GattAttribute *p_desc = p_char->getDescriptor(j); 00085 if (p_desc->getUUID() == BLE_UUID_DESCRIPTOR_CHAR_USER_DESC) { 00086 userDescriptionDescriptorValuePtr = p_desc->getValuePtr(); 00087 userDescriptionDescriptorValueLen = p_desc->getLength(); 00088 } 00089 } 00090 00091 ASSERT ( ERROR_NONE == 00092 custom_add_in_characteristic(BLE_GATT_HANDLE_INVALID, 00093 &nordicUUID, 00094 p_char->getProperties(), 00095 p_char->getRequiredSecurity(), 00096 p_char->getValueAttribute().getValuePtr(), 00097 p_char->getValueAttribute().getLength(), 00098 p_char->getValueAttribute().getMaxLength(), 00099 p_char->getValueAttribute().hasVariableLength(), 00100 userDescriptionDescriptorValuePtr, 00101 userDescriptionDescriptorValueLen, 00102 p_char->isReadAuthorizationEnabled(), 00103 p_char->isWriteAuthorizationEnabled(), 00104 &nrfCharacteristicHandles[characteristicCount]), 00105 BLE_ERROR_PARAM_OUT_OF_RANGE ); 00106 00107 /* Update the characteristic handle */ 00108 p_characteristics[characteristicCount] = p_char; 00109 p_char->getValueAttribute().setHandle(nrfCharacteristicHandles[characteristicCount].value_handle); 00110 characteristicCount++; 00111 00112 /* Add optional descriptors if any */ 00113 for (uint8_t j = 0; j < p_char->getDescriptorCount(); j++) { 00114 if (descriptorCount >= BLE_TOTAL_DESCRIPTORS) { 00115 return BLE_ERROR_NO_MEM; 00116 } 00117 00118 GattAttribute *p_desc = p_char->getDescriptor(j); 00119 /* skip the user-description-descriptor here; this has already been handled when adding the characteristic (above). */ 00120 if (p_desc->getUUID() == BLE_UUID_DESCRIPTOR_CHAR_USER_DESC) { 00121 continue; 00122 } 00123 00124 nordicUUID = custom_convert_to_nordic_uuid(p_desc->getUUID()); 00125 00126 ASSERT(ERROR_NONE == 00127 custom_add_in_descriptor(BLE_GATT_HANDLE_INVALID, 00128 &nordicUUID, 00129 p_desc->getValuePtr(), 00130 p_desc->getLength(), 00131 p_desc->getMaxLength(), 00132 p_desc->hasVariableLength(), 00133 &nrfDescriptorHandles[descriptorCount]), 00134 BLE_ERROR_PARAM_OUT_OF_RANGE); 00135 00136 p_descriptors[descriptorCount] = p_desc; 00137 p_desc->setHandle(nrfDescriptorHandles[descriptorCount]); 00138 descriptorCount++; 00139 } 00140 } 00141 00142 serviceCount++; 00143 00144 return BLE_ERROR_NONE; 00145 } 00146 00147 /**************************************************************************/ 00148 /*! 00149 @brief Reads the value of a characteristic, based on the service 00150 and characteristic index fields 00151 00152 @param[in] attributeHandle 00153 The handle of the GattCharacteristic to read from 00154 @param[in] buffer 00155 Buffer to hold the the characteristic's value 00156 (raw byte array in LSB format) 00157 @param[in/out] len 00158 input: Length in bytes to be read. 00159 output: Total length of attribute value upon successful return. 00160 00161 @returns ble_error_t 00162 00163 @retval BLE_ERROR_NONE 00164 Everything executed properly 00165 */ 00166 /**************************************************************************/ 00167 ble_error_t nRF5xGattServer::read(GattAttribute::Handle_t attributeHandle, uint8_t buffer[], uint16_t *lengthP) 00168 { 00169 return read(BLE_CONN_HANDLE_INVALID, attributeHandle, buffer, lengthP); 00170 } 00171 00172 ble_error_t nRF5xGattServer::read(Gap::Handle_t connectionHandle, GattAttribute::Handle_t attributeHandle, uint8_t buffer[], uint16_t *lengthP) 00173 { 00174 ble_gatts_value_t value = { 00175 .len = *lengthP, 00176 .offset = 0, 00177 .p_value = buffer, 00178 }; 00179 00180 ASSERT( ERROR_NONE == 00181 sd_ble_gatts_value_get(connectionHandle, attributeHandle, &value), 00182 BLE_ERROR_PARAM_OUT_OF_RANGE); 00183 *lengthP = value.len; 00184 00185 return BLE_ERROR_NONE; 00186 } 00187 00188 /**************************************************************************/ 00189 /*! 00190 @brief Updates the value of a characteristic, based on the service 00191 and characteristic index fields 00192 00193 @param[in] charHandle 00194 The handle of the GattCharacteristic to write to 00195 @param[in] buffer 00196 Data to use when updating the characteristic's value 00197 (raw byte array in LSB format) 00198 @param[in] len 00199 The number of bytes in buffer 00200 00201 @returns ble_error_t 00202 00203 @retval BLE_ERROR_NONE 00204 Everything executed properly 00205 */ 00206 /**************************************************************************/ 00207 ble_error_t nRF5xGattServer::write(GattAttribute::Handle_t attributeHandle, const uint8_t buffer[], uint16_t len, bool localOnly) 00208 { 00209 return write(BLE_CONN_HANDLE_INVALID, attributeHandle, buffer, len, localOnly); 00210 } 00211 00212 ble_error_t nRF5xGattServer::write(Gap::Handle_t connectionHandle, GattAttribute::Handle_t attributeHandle, const uint8_t buffer[], uint16_t len, bool localOnly) 00213 { 00214 ble_error_t returnValue = BLE_ERROR_NONE; 00215 00216 ble_gatts_value_t value = { 00217 .len = len, 00218 .offset = 0, 00219 .p_value = const_cast<uint8_t *>(buffer), 00220 }; 00221 00222 if (localOnly) { 00223 /* Only update locally regardless of notify/indicate */ 00224 ASSERT_INT( ERROR_NONE, 00225 sd_ble_gatts_value_set(connectionHandle, attributeHandle, &value), 00226 BLE_ERROR_PARAM_OUT_OF_RANGE ); 00227 return BLE_ERROR_NONE; 00228 } 00229 00230 int characteristicIndex = resolveValueHandleToCharIndex(attributeHandle); 00231 if ((characteristicIndex != -1) && 00232 (p_characteristics[characteristicIndex]->getProperties() & (GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY))) { 00233 /* HVX update for the characteristic value */ 00234 ble_gatts_hvx_params_t hvx_params; 00235 00236 hvx_params.handle = attributeHandle; 00237 hvx_params.type = 00238 (p_characteristics[characteristicIndex]->getProperties() & GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) ? BLE_GATT_HVX_NOTIFICATION : BLE_GATT_HVX_INDICATION; 00239 hvx_params.offset = 0; 00240 hvx_params.p_data = const_cast<uint8_t *>(buffer); 00241 hvx_params.p_len = &len; 00242 00243 if (connectionHandle == BLE_CONN_HANDLE_INVALID) { /* use the default connection handle if the caller hasn't specified a valid connectionHandle. */ 00244 nRF5xGap &gap = (nRF5xGap &) nRF5xn::Instance(BLE::DEFAULT_INSTANCE).getGap(); 00245 connectionHandle = gap.getConnectionHandle(); 00246 } 00247 error_t error = (error_t) sd_ble_gatts_hvx(connectionHandle, &hvx_params); 00248 if (error != ERROR_NONE) { 00249 switch (error) { 00250 case ERROR_BLE_NO_TX_BUFFERS: /* Notifications consume application buffers. The return value can be used for resending notifications. */ 00251 case ERROR_BUSY: 00252 returnValue = BLE_STACK_BUSY; 00253 break; 00254 00255 case ERROR_INVALID_STATE: 00256 case ERROR_BLEGATTS_SYS_ATTR_MISSING: 00257 returnValue = BLE_ERROR_INVALID_STATE; 00258 break; 00259 00260 default : 00261 ASSERT_INT( ERROR_NONE, 00262 sd_ble_gatts_value_set(connectionHandle, attributeHandle, &value), 00263 BLE_ERROR_PARAM_OUT_OF_RANGE ); 00264 00265 /* Notifications consume application buffers. The return value can 00266 * be used for resending notifications. */ 00267 returnValue = BLE_STACK_BUSY; 00268 break; 00269 } 00270 } 00271 } else { 00272 uint32_t err = sd_ble_gatts_value_set(connectionHandle, attributeHandle, &value); 00273 switch(err) { 00274 case NRF_SUCCESS: 00275 returnValue = BLE_ERROR_NONE; 00276 break; 00277 case NRF_ERROR_INVALID_ADDR: 00278 case NRF_ERROR_INVALID_PARAM: 00279 returnValue = BLE_ERROR_INVALID_PARAM; 00280 break; 00281 case NRF_ERROR_NOT_FOUND: 00282 case NRF_ERROR_DATA_SIZE: 00283 case BLE_ERROR_INVALID_CONN_HANDLE: 00284 case BLE_ERROR_GATTS_INVALID_ATTR_TYPE: 00285 returnValue = BLE_ERROR_PARAM_OUT_OF_RANGE; 00286 break; 00287 case NRF_ERROR_FORBIDDEN: 00288 returnValue = BLE_ERROR_OPERATION_NOT_PERMITTED; 00289 break; 00290 default: 00291 returnValue = BLE_ERROR_UNSPECIFIED; 00292 break; 00293 } 00294 } 00295 00296 return returnValue; 00297 } 00298 00299 ble_error_t nRF5xGattServer::areUpdatesEnabled(const GattCharacteristic &characteristic, bool *enabledP) 00300 { 00301 /* Forward the call with the default connection handle. */ 00302 nRF5xGap &gap = (nRF5xGap &) nRF5xn::Instance(BLE::DEFAULT_INSTANCE).getGap(); 00303 return areUpdatesEnabled(gap.getConnectionHandle(), characteristic, enabledP); 00304 } 00305 00306 ble_error_t nRF5xGattServer::areUpdatesEnabled(Gap::Handle_t connectionHandle, const GattCharacteristic &characteristic, bool *enabledP) 00307 { 00308 int characteristicIndex = resolveValueHandleToCharIndex(characteristic.getValueHandle()); 00309 if (characteristicIndex == -1) { 00310 return BLE_ERROR_INVALID_PARAM; 00311 } 00312 00313 /* Read the cccd value from the GATT server. */ 00314 GattAttribute::Handle_t cccdHandle = nrfCharacteristicHandles[characteristicIndex].cccd_handle; 00315 uint16_t cccdValue; 00316 uint16_t length = sizeof(cccdValue); 00317 ble_error_t rc = read(connectionHandle, cccdHandle, reinterpret_cast<uint8_t *>(&cccdValue), &length); 00318 if (rc != BLE_ERROR_NONE) { 00319 return rc; 00320 } 00321 if (length != sizeof(cccdValue)) { 00322 return BLE_ERROR_INVALID_STATE; 00323 } 00324 00325 /* Check for NOTFICATION or INDICATION in CCCD. */ 00326 if ((cccdValue & BLE_GATT_HVX_NOTIFICATION) || (cccdValue & BLE_GATT_HVX_INDICATION)) { 00327 *enabledP = true; 00328 } 00329 00330 return BLE_ERROR_NONE; 00331 } 00332 00333 /**************************************************************************/ 00334 /*! 00335 @brief Clear nRF5xGattServer's state. 00336 00337 @returns ble_error_t 00338 00339 @retval BLE_ERROR_NONE 00340 Everything executed properly 00341 */ 00342 /**************************************************************************/ 00343 ble_error_t nRF5xGattServer::reset(void) 00344 { 00345 /* Clear all state that is from the parent, including private members */ 00346 if (GattServer::reset() != BLE_ERROR_NONE) { 00347 return BLE_ERROR_INVALID_STATE; 00348 } 00349 00350 /* Clear derived class members */ 00351 memset(p_characteristics, 0, sizeof(p_characteristics)); 00352 memset(p_descriptors, 0, sizeof(p_descriptors)); 00353 memset(nrfCharacteristicHandles, 0, sizeof(ble_gatts_char_handles_t)); 00354 memset(nrfDescriptorHandles, 0, sizeof(nrfDescriptorHandles)); 00355 descriptorCount = 0; 00356 00357 return BLE_ERROR_NONE; 00358 } 00359 00360 /**************************************************************************/ 00361 /*! 00362 @brief Callback handler for events getting pushed up from the SD 00363 */ 00364 /**************************************************************************/ 00365 void nRF5xGattServer::hwCallback(ble_evt_t *p_ble_evt) 00366 { 00367 GattAttribute::Handle_t handle_value; 00368 GattServerEvents::gattEvent_t eventType; 00369 const ble_gatts_evt_t *gattsEventP = &p_ble_evt->evt.gatts_evt; 00370 00371 switch (p_ble_evt->header.evt_id) { 00372 case BLE_GATTS_EVT_WRITE: { 00373 /* There are 2 use case here: Values being updated & CCCD (indicate/notify) enabled */ 00374 00375 /* 1.) Handle CCCD changes */ 00376 handle_value = gattsEventP->params.write.handle; 00377 int characteristicIndex = resolveCCCDHandleToCharIndex(handle_value); 00378 if ((characteristicIndex != -1) && 00379 (p_characteristics[characteristicIndex]->getProperties() & 00380 (GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY))) { 00381 00382 uint16_t cccd_value = (gattsEventP->params.write.data[1] << 8) | gattsEventP->params.write.data[0]; /* Little Endian but M0 may be mis-aligned */ 00383 00384 if (((p_characteristics[characteristicIndex]->getProperties() & GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE) && (cccd_value & BLE_GATT_HVX_INDICATION)) || 00385 ((p_characteristics[characteristicIndex]->getProperties() & GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) && (cccd_value & BLE_GATT_HVX_NOTIFICATION))) { 00386 eventType = GattServerEvents::GATT_EVENT_UPDATES_ENABLED; 00387 } else { 00388 eventType = GattServerEvents::GATT_EVENT_UPDATES_DISABLED; 00389 } 00390 00391 handleEvent(eventType, p_characteristics[characteristicIndex]->getValueHandle()); 00392 return; 00393 } 00394 00395 /* 2.) Changes to the characteristic value will be handled with other events below */ 00396 eventType = GattServerEvents::GATT_EVENT_DATA_WRITTEN; 00397 } 00398 break; 00399 00400 case BLE_GATTS_EVT_HVC: 00401 /* Indication confirmation received */ 00402 eventType = GattServerEvents::GATT_EVENT_CONFIRMATION_RECEIVED; 00403 handle_value = gattsEventP->params.hvc.handle; 00404 break; 00405 00406 case BLE_EVT_TX_COMPLETE: { 00407 handleDataSentEvent(p_ble_evt->evt.common_evt.params.tx_complete.count); 00408 return; 00409 } 00410 00411 case BLE_GATTS_EVT_SYS_ATTR_MISSING: 00412 sd_ble_gatts_sys_attr_set(gattsEventP->conn_handle, NULL, 0, 0); 00413 return; 00414 00415 case BLE_GATTS_EVT_RW_AUTHORIZE_REQUEST: 00416 switch (gattsEventP->params.authorize_request.type) { 00417 case BLE_GATTS_AUTHORIZE_TYPE_READ: 00418 eventType = GattServerEvents::GATT_EVENT_READ_AUTHORIZATION_REQ; 00419 handle_value = gattsEventP->params.authorize_request.request.read.handle; 00420 break; 00421 case BLE_GATTS_AUTHORIZE_TYPE_WRITE: 00422 eventType = GattServerEvents::GATT_EVENT_WRITE_AUTHORIZATION_REQ; 00423 handle_value = gattsEventP->params.authorize_request.request.write.handle; 00424 break; 00425 default: 00426 return; 00427 } 00428 break; 00429 00430 default: 00431 return; 00432 } 00433 00434 int characteristicIndex = resolveValueHandleToCharIndex(handle_value); 00435 if (characteristicIndex == -1) { 00436 return; 00437 } 00438 00439 /* Find index (charHandle) in the pool */ 00440 switch (eventType) { 00441 case GattServerEvents::GATT_EVENT_DATA_WRITTEN: { 00442 GattWriteCallbackParams cbParams = { 00443 .connHandle = gattsEventP->conn_handle, 00444 .handle = handle_value, 00445 .writeOp = static_cast<GattWriteCallbackParams::WriteOp_t>(gattsEventP->params.write.op), 00446 .offset = gattsEventP->params.write.offset, 00447 .len = gattsEventP->params.write.len, 00448 .data = gattsEventP->params.write.data 00449 }; 00450 handleDataWrittenEvent(&cbParams); 00451 break; 00452 } 00453 case GattServerEvents::GATT_EVENT_WRITE_AUTHORIZATION_REQ: { 00454 GattWriteAuthCallbackParams cbParams = { 00455 .connHandle = gattsEventP->conn_handle, 00456 .handle = handle_value, 00457 .offset = gattsEventP->params.authorize_request.request.write.offset, 00458 .len = gattsEventP->params.authorize_request.request.write.len, 00459 .data = gattsEventP->params.authorize_request.request.write.data, 00460 .authorizationReply = AUTH_CALLBACK_REPLY_SUCCESS /* the callback handler must leave this member 00461 * set to AUTH_CALLBACK_REPLY_SUCCESS if the client 00462 * request is to proceed. */ 00463 }; 00464 ble_gatts_rw_authorize_reply_params_t reply = { 00465 .type = BLE_GATTS_AUTHORIZE_TYPE_WRITE, 00466 .params = { 00467 .write = { 00468 .gatt_status = p_characteristics[characteristicIndex]->authorizeWrite(&cbParams) 00469 } 00470 } 00471 }; 00472 sd_ble_gatts_rw_authorize_reply(gattsEventP->conn_handle, &reply); 00473 00474 /* 00475 * If write-authorization is enabled for a characteristic, 00476 * AUTHORIZATION_REQ event (if replied with true) is *not* 00477 * followed by another DATA_WRITTEN event; so we still need 00478 * to invoke handleDataWritten(), much the same as we would 00479 * have done if write-authorization had not been enabled. 00480 */ 00481 if (reply.params.write.gatt_status == BLE_GATT_STATUS_SUCCESS) { 00482 GattWriteCallbackParams cbParams = { 00483 .connHandle = gattsEventP->conn_handle, 00484 .handle = handle_value, 00485 .writeOp = static_cast<GattWriteCallbackParams::WriteOp_t>(gattsEventP->params.authorize_request.request.write.op), 00486 .offset = gattsEventP->params.authorize_request.request.write.offset, 00487 .len = gattsEventP->params.authorize_request.request.write.len, 00488 .data = gattsEventP->params.authorize_request.request.write.data, 00489 }; 00490 handleDataWrittenEvent(&cbParams); 00491 } 00492 break; 00493 } 00494 case GattServerEvents::GATT_EVENT_READ_AUTHORIZATION_REQ: { 00495 GattReadAuthCallbackParams cbParams = { 00496 .connHandle = gattsEventP->conn_handle, 00497 .handle = handle_value, 00498 .offset = gattsEventP->params.authorize_request.request.read.offset, 00499 .len = 0, 00500 .data = NULL, 00501 .authorizationReply = AUTH_CALLBACK_REPLY_SUCCESS /* the callback handler must leave this member 00502 * set to AUTH_CALLBACK_REPLY_SUCCESS if the client 00503 * request is to proceed. */ 00504 }; 00505 00506 ble_gatts_rw_authorize_reply_params_t reply = { 00507 .type = BLE_GATTS_AUTHORIZE_TYPE_READ, 00508 .params = { 00509 .read = { 00510 .gatt_status = p_characteristics[characteristicIndex]->authorizeRead(&cbParams) 00511 } 00512 } 00513 }; 00514 00515 if (cbParams.authorizationReply == BLE_GATT_STATUS_SUCCESS) { 00516 if (cbParams.data != NULL) { 00517 reply.params.read.update = 1; 00518 reply.params.read.offset = cbParams.offset; 00519 reply.params.read.len = cbParams.len; 00520 reply.params.read.p_data = cbParams.data; 00521 } 00522 } 00523 00524 sd_ble_gatts_rw_authorize_reply(gattsEventP->conn_handle, &reply); 00525 break; 00526 } 00527 00528 default: 00529 handleEvent(eventType, handle_value); 00530 break; 00531 } 00532 }
Generated on Wed Jul 13 2022 11:18:00 by 1.7.2