Updated to latest online libraries (See also mbed Apps: HelloBlue, FOTA4)

Dependencies:   BLE_API mbed nRF51822

Fork of BLE_Default_APP by Yihui Xiong

Committer:
yihui
Date:
Fri Oct 10 03:36:28 2014 +0000
Revision:
1:a607cd9655d7
use NVIC_SystemReset() to run bootloader

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yihui 1:a607cd9655d7 1 /* mbed Microcontroller Library
yihui 1:a607cd9655d7 2 * Copyright (c) 2006-2013 ARM Limited
yihui 1:a607cd9655d7 3 *
yihui 1:a607cd9655d7 4 * Licensed under the Apache License, Version 2.0 (the "License");
yihui 1:a607cd9655d7 5 * you may not use this file except in compliance with the License.
yihui 1:a607cd9655d7 6 * You may obtain a copy of the License at
yihui 1:a607cd9655d7 7 *
yihui 1:a607cd9655d7 8 * http://www.apache.org/licenses/LICENSE-2.0
yihui 1:a607cd9655d7 9 *
yihui 1:a607cd9655d7 10 * Unless required by applicable law or agreed to in writing, software
yihui 1:a607cd9655d7 11 * distributed under the License is distributed on an "AS IS" BASIS,
yihui 1:a607cd9655d7 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
yihui 1:a607cd9655d7 13 * See the License for the specific language governing permissions and
yihui 1:a607cd9655d7 14 * limitations under the License.
yihui 1:a607cd9655d7 15 */
yihui 1:a607cd9655d7 16
yihui 1:a607cd9655d7 17 #include "nRF51GattServer.h"
yihui 1:a607cd9655d7 18 #include "mbed.h"
yihui 1:a607cd9655d7 19
yihui 1:a607cd9655d7 20 #include "common/common.h"
yihui 1:a607cd9655d7 21 #include "btle/custom/custom_helper.h"
yihui 1:a607cd9655d7 22
yihui 1:a607cd9655d7 23 #include "nRF51Gap.h"
yihui 1:a607cd9655d7 24
yihui 1:a607cd9655d7 25 /**************************************************************************/
yihui 1:a607cd9655d7 26 /*!
yihui 1:a607cd9655d7 27 @brief Adds a new service to the GATT table on the peripheral
yihui 1:a607cd9655d7 28
yihui 1:a607cd9655d7 29 @returns ble_error_t
yihui 1:a607cd9655d7 30
yihui 1:a607cd9655d7 31 @retval BLE_ERROR_NONE
yihui 1:a607cd9655d7 32 Everything executed properly
yihui 1:a607cd9655d7 33
yihui 1:a607cd9655d7 34 @section EXAMPLE
yihui 1:a607cd9655d7 35
yihui 1:a607cd9655d7 36 @code
yihui 1:a607cd9655d7 37
yihui 1:a607cd9655d7 38 @endcode
yihui 1:a607cd9655d7 39 */
yihui 1:a607cd9655d7 40 /**************************************************************************/
yihui 1:a607cd9655d7 41 ble_error_t nRF51GattServer::addService(GattService &service)
yihui 1:a607cd9655d7 42 {
yihui 1:a607cd9655d7 43 /* ToDo: Make sure we don't overflow the array, etc. */
yihui 1:a607cd9655d7 44 /* ToDo: Make sure this service UUID doesn't already exist (?) */
yihui 1:a607cd9655d7 45 /* ToDo: Basic validation */
yihui 1:a607cd9655d7 46
yihui 1:a607cd9655d7 47 /* Add the service to the nRF51 */
yihui 1:a607cd9655d7 48 ble_uuid_t nordicUUID;
yihui 1:a607cd9655d7 49 nordicUUID = custom_convert_to_nordic_uuid(service.getUUID());
yihui 1:a607cd9655d7 50
yihui 1:a607cd9655d7 51 uint16_t serviceHandle;
yihui 1:a607cd9655d7 52 ASSERT( ERROR_NONE ==
yihui 1:a607cd9655d7 53 sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY,
yihui 1:a607cd9655d7 54 &nordicUUID,
yihui 1:a607cd9655d7 55 &serviceHandle),
yihui 1:a607cd9655d7 56 BLE_ERROR_PARAM_OUT_OF_RANGE );
yihui 1:a607cd9655d7 57 service.setHandle(serviceHandle);
yihui 1:a607cd9655d7 58
yihui 1:a607cd9655d7 59 /* Add characteristics to the service */
yihui 1:a607cd9655d7 60 for (uint8_t i = 0; i < service.getCharacteristicCount(); i++) {
yihui 1:a607cd9655d7 61 GattCharacteristic *p_char = service.getCharacteristic(i);
yihui 1:a607cd9655d7 62
yihui 1:a607cd9655d7 63 /* Skip any incompletely defined, read-only characteristics. */
yihui 1:a607cd9655d7 64 if ((p_char->getValueAttribute().getValuePtr() == NULL) &&
yihui 1:a607cd9655d7 65 (p_char->getValueAttribute().getInitialLength() == 0) &&
yihui 1:a607cd9655d7 66 (p_char->getProperties() == GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ)) {
yihui 1:a607cd9655d7 67 continue;
yihui 1:a607cd9655d7 68 }
yihui 1:a607cd9655d7 69
yihui 1:a607cd9655d7 70 nordicUUID = custom_convert_to_nordic_uuid(p_char->getValueAttribute().getUUID());
yihui 1:a607cd9655d7 71
yihui 1:a607cd9655d7 72 ASSERT ( ERROR_NONE ==
yihui 1:a607cd9655d7 73 custom_add_in_characteristic(BLE_GATT_HANDLE_INVALID,
yihui 1:a607cd9655d7 74 &nordicUUID,
yihui 1:a607cd9655d7 75 p_char->getProperties(),
yihui 1:a607cd9655d7 76 p_char->getValueAttribute().getValuePtr(),
yihui 1:a607cd9655d7 77 p_char->getValueAttribute().getInitialLength(),
yihui 1:a607cd9655d7 78 p_char->getValueAttribute().getMaxLength(),
yihui 1:a607cd9655d7 79 &nrfCharacteristicHandles[characteristicCount]),
yihui 1:a607cd9655d7 80 BLE_ERROR_PARAM_OUT_OF_RANGE );
yihui 1:a607cd9655d7 81
yihui 1:a607cd9655d7 82 /* Update the characteristic handle */
yihui 1:a607cd9655d7 83 uint16_t charHandle = characteristicCount;
yihui 1:a607cd9655d7 84 p_characteristics[characteristicCount++] = p_char;
yihui 1:a607cd9655d7 85
yihui 1:a607cd9655d7 86 p_char->getValueAttribute().setHandle(charHandle);
yihui 1:a607cd9655d7 87
yihui 1:a607cd9655d7 88 /* Add optional descriptors if any */
yihui 1:a607cd9655d7 89 /* ToDo: Make sure we don't overflow the array */
yihui 1:a607cd9655d7 90 for (uint8_t j = 0; j < p_char->getDescriptorCount(); j++) {
yihui 1:a607cd9655d7 91 GattAttribute *p_desc = p_char->getDescriptor(j);
yihui 1:a607cd9655d7 92
yihui 1:a607cd9655d7 93 nordicUUID = custom_convert_to_nordic_uuid(p_desc->getUUID());
yihui 1:a607cd9655d7 94
yihui 1:a607cd9655d7 95 ASSERT ( ERROR_NONE ==
yihui 1:a607cd9655d7 96 custom_add_in_descriptor(BLE_GATT_HANDLE_INVALID,
yihui 1:a607cd9655d7 97 &nordicUUID,
yihui 1:a607cd9655d7 98 p_desc->getValuePtr(),
yihui 1:a607cd9655d7 99 p_desc->getInitialLength(),
yihui 1:a607cd9655d7 100 p_desc->getMaxLength(),
yihui 1:a607cd9655d7 101 &nrfDescriptorHandles[descriptorCount]),
yihui 1:a607cd9655d7 102 BLE_ERROR_PARAM_OUT_OF_RANGE );
yihui 1:a607cd9655d7 103
yihui 1:a607cd9655d7 104 uint16_t descHandle = descriptorCount;
yihui 1:a607cd9655d7 105 p_descriptors[descriptorCount++] = p_desc;
yihui 1:a607cd9655d7 106 p_desc->setHandle(descHandle);
yihui 1:a607cd9655d7 107 }
yihui 1:a607cd9655d7 108 }
yihui 1:a607cd9655d7 109
yihui 1:a607cd9655d7 110 serviceCount++;
yihui 1:a607cd9655d7 111
yihui 1:a607cd9655d7 112 return BLE_ERROR_NONE;
yihui 1:a607cd9655d7 113 }
yihui 1:a607cd9655d7 114
yihui 1:a607cd9655d7 115 /**************************************************************************/
yihui 1:a607cd9655d7 116 /*!
yihui 1:a607cd9655d7 117 @brief Reads the value of a characteristic, based on the service
yihui 1:a607cd9655d7 118 and characteristic index fields
yihui 1:a607cd9655d7 119
yihui 1:a607cd9655d7 120 @param[in] charHandle
yihui 1:a607cd9655d7 121 The handle of the GattCharacteristic to read from
yihui 1:a607cd9655d7 122 @param[in] buffer
yihui 1:a607cd9655d7 123 Buffer to hold the the characteristic's value
yihui 1:a607cd9655d7 124 (raw byte array in LSB format)
yihui 1:a607cd9655d7 125 @param[in] len
yihui 1:a607cd9655d7 126 The number of bytes read into the buffer
yihui 1:a607cd9655d7 127
yihui 1:a607cd9655d7 128 @returns ble_error_t
yihui 1:a607cd9655d7 129
yihui 1:a607cd9655d7 130 @retval BLE_ERROR_NONE
yihui 1:a607cd9655d7 131 Everything executed properly
yihui 1:a607cd9655d7 132
yihui 1:a607cd9655d7 133 @section EXAMPLE
yihui 1:a607cd9655d7 134
yihui 1:a607cd9655d7 135 @code
yihui 1:a607cd9655d7 136
yihui 1:a607cd9655d7 137 @endcode
yihui 1:a607cd9655d7 138 */
yihui 1:a607cd9655d7 139 /**************************************************************************/
yihui 1:a607cd9655d7 140 ble_error_t nRF51GattServer::readValue(uint16_t charHandle, uint8_t buffer[], uint16_t *const lengthP)
yihui 1:a607cd9655d7 141 {
yihui 1:a607cd9655d7 142 ASSERT( ERROR_NONE ==
yihui 1:a607cd9655d7 143 sd_ble_gatts_value_get(nrfCharacteristicHandles[charHandle].value_handle, 0, lengthP, buffer),
yihui 1:a607cd9655d7 144 BLE_ERROR_PARAM_OUT_OF_RANGE);
yihui 1:a607cd9655d7 145
yihui 1:a607cd9655d7 146 return BLE_ERROR_NONE;
yihui 1:a607cd9655d7 147 }
yihui 1:a607cd9655d7 148
yihui 1:a607cd9655d7 149 /**************************************************************************/
yihui 1:a607cd9655d7 150 /*!
yihui 1:a607cd9655d7 151 @brief Updates the value of a characteristic, based on the service
yihui 1:a607cd9655d7 152 and characteristic index fields
yihui 1:a607cd9655d7 153
yihui 1:a607cd9655d7 154 @param[in] charHandle
yihui 1:a607cd9655d7 155 The handle of the GattCharacteristic to write to
yihui 1:a607cd9655d7 156 @param[in] buffer
yihui 1:a607cd9655d7 157 Data to use when updating the characteristic's value
yihui 1:a607cd9655d7 158 (raw byte array in LSB format)
yihui 1:a607cd9655d7 159 @param[in] len
yihui 1:a607cd9655d7 160 The number of bytes in buffer
yihui 1:a607cd9655d7 161
yihui 1:a607cd9655d7 162 @returns ble_error_t
yihui 1:a607cd9655d7 163
yihui 1:a607cd9655d7 164 @retval BLE_ERROR_NONE
yihui 1:a607cd9655d7 165 Everything executed properly
yihui 1:a607cd9655d7 166
yihui 1:a607cd9655d7 167 @section EXAMPLE
yihui 1:a607cd9655d7 168
yihui 1:a607cd9655d7 169 @code
yihui 1:a607cd9655d7 170
yihui 1:a607cd9655d7 171 @endcode
yihui 1:a607cd9655d7 172 */
yihui 1:a607cd9655d7 173 /**************************************************************************/
yihui 1:a607cd9655d7 174 ble_error_t nRF51GattServer::updateValue(uint16_t charHandle, uint8_t buffer[], uint16_t len, bool localOnly)
yihui 1:a607cd9655d7 175 {
yihui 1:a607cd9655d7 176 uint16_t gapConnectionHandle = nRF51Gap::getInstance().getConnectionHandle();
yihui 1:a607cd9655d7 177
yihui 1:a607cd9655d7 178 if (localOnly) {
yihui 1:a607cd9655d7 179 /* Only update locally regardless of notify/indicate */
yihui 1:a607cd9655d7 180 ASSERT_INT( ERROR_NONE,
yihui 1:a607cd9655d7 181 sd_ble_gatts_value_set(nrfCharacteristicHandles[charHandle].value_handle, 0, &len, buffer),
yihui 1:a607cd9655d7 182 BLE_ERROR_PARAM_OUT_OF_RANGE );
yihui 1:a607cd9655d7 183 return BLE_ERROR_NONE;
yihui 1:a607cd9655d7 184 }
yihui 1:a607cd9655d7 185
yihui 1:a607cd9655d7 186 if ((p_characteristics[charHandle]->getProperties() &
yihui 1:a607cd9655d7 187 (GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE |
yihui 1:a607cd9655d7 188 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY)) &&
yihui 1:a607cd9655d7 189 (gapConnectionHandle != BLE_CONN_HANDLE_INVALID)) {
yihui 1:a607cd9655d7 190 /* HVX update for the characteristic value */
yihui 1:a607cd9655d7 191 ble_gatts_hvx_params_t hvx_params;
yihui 1:a607cd9655d7 192
yihui 1:a607cd9655d7 193 hvx_params.handle = nrfCharacteristicHandles[charHandle].value_handle;
yihui 1:a607cd9655d7 194 hvx_params.type =
yihui 1:a607cd9655d7 195 (p_characteristics[charHandle]->getProperties() &
yihui 1:a607cd9655d7 196 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) ?
yihui 1:a607cd9655d7 197 BLE_GATT_HVX_NOTIFICATION : BLE_GATT_HVX_INDICATION;
yihui 1:a607cd9655d7 198 hvx_params.offset = 0;
yihui 1:a607cd9655d7 199 hvx_params.p_data = buffer;
yihui 1:a607cd9655d7 200 hvx_params.p_len = &len;
yihui 1:a607cd9655d7 201
yihui 1:a607cd9655d7 202 error_t error = (error_t) sd_ble_gatts_hvx(gapConnectionHandle, &hvx_params);
yihui 1:a607cd9655d7 203
yihui 1:a607cd9655d7 204 /* ERROR_INVALID_STATE, ERROR_BUSY, ERROR_GATTS_SYS_ATTR_MISSING and
yihui 1:a607cd9655d7 205 *ERROR_NO_TX_BUFFERS the ATT table has been updated. */
yihui 1:a607cd9655d7 206 if ((error != ERROR_NONE) && (error != ERROR_INVALID_STATE) &&
yihui 1:a607cd9655d7 207 (error != ERROR_BLE_NO_TX_BUFFERS) && (error != ERROR_BUSY) &&
yihui 1:a607cd9655d7 208 (error != ERROR_BLEGATTS_SYS_ATTR_MISSING)) {
yihui 1:a607cd9655d7 209 ASSERT_INT( ERROR_NONE,
yihui 1:a607cd9655d7 210 sd_ble_gatts_value_set(nrfCharacteristicHandles[charHandle].value_handle, 0, &len, buffer),
yihui 1:a607cd9655d7 211 BLE_ERROR_PARAM_OUT_OF_RANGE );
yihui 1:a607cd9655d7 212 }
yihui 1:a607cd9655d7 213 } else {
yihui 1:a607cd9655d7 214 ASSERT_INT( ERROR_NONE,
yihui 1:a607cd9655d7 215 sd_ble_gatts_value_set(nrfCharacteristicHandles[charHandle].value_handle, 0, &len, buffer),
yihui 1:a607cd9655d7 216 BLE_ERROR_PARAM_OUT_OF_RANGE );
yihui 1:a607cd9655d7 217 }
yihui 1:a607cd9655d7 218
yihui 1:a607cd9655d7 219 return BLE_ERROR_NONE;
yihui 1:a607cd9655d7 220 }
yihui 1:a607cd9655d7 221
yihui 1:a607cd9655d7 222 /**************************************************************************/
yihui 1:a607cd9655d7 223 /*!
yihui 1:a607cd9655d7 224 @brief Callback handler for events getting pushed up from the SD
yihui 1:a607cd9655d7 225 */
yihui 1:a607cd9655d7 226 /**************************************************************************/
yihui 1:a607cd9655d7 227 void nRF51GattServer::hwCallback(ble_evt_t *p_ble_evt)
yihui 1:a607cd9655d7 228 {
yihui 1:a607cd9655d7 229 uint16_t handle_value;
yihui 1:a607cd9655d7 230 GattServerEvents::gattEvent_t eventType;
yihui 1:a607cd9655d7 231 const ble_gatts_evt_t *gattsEventP = &p_ble_evt->evt.gatts_evt;
yihui 1:a607cd9655d7 232
yihui 1:a607cd9655d7 233 switch (p_ble_evt->header.evt_id) {
yihui 1:a607cd9655d7 234 case BLE_GATTS_EVT_WRITE:
yihui 1:a607cd9655d7 235 /* There are 2 use case here: Values being updated & CCCD (indicate/notify) enabled */
yihui 1:a607cd9655d7 236
yihui 1:a607cd9655d7 237 /* 1.) Handle CCCD changes */
yihui 1:a607cd9655d7 238 handle_value = gattsEventP->params.write.handle;
yihui 1:a607cd9655d7 239 for (uint8_t i = 0; i<characteristicCount; i++) {
yihui 1:a607cd9655d7 240 if ((p_characteristics[i]->getProperties() & (GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY)) &&
yihui 1:a607cd9655d7 241 (nrfCharacteristicHandles[i].cccd_handle == handle_value)) {
yihui 1:a607cd9655d7 242 uint16_t cccd_value =
yihui 1:a607cd9655d7 243 (gattsEventP->params.write.data[1] << 8) |
yihui 1:a607cd9655d7 244 gattsEventP->params.write.data[0]; /* Little Endian but M0 may be mis-aligned */
yihui 1:a607cd9655d7 245
yihui 1:a607cd9655d7 246 if (((p_characteristics[i]->getProperties() & GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE) && (cccd_value & BLE_GATT_HVX_INDICATION)) ||
yihui 1:a607cd9655d7 247 ((p_characteristics[i]->getProperties() & GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) && (cccd_value & BLE_GATT_HVX_NOTIFICATION))) {
yihui 1:a607cd9655d7 248 eventType = GattServerEvents::GATT_EVENT_UPDATES_ENABLED;
yihui 1:a607cd9655d7 249 } else {
yihui 1:a607cd9655d7 250 eventType = GattServerEvents::GATT_EVENT_UPDATES_DISABLED;
yihui 1:a607cd9655d7 251 }
yihui 1:a607cd9655d7 252
yihui 1:a607cd9655d7 253 handleEvent(eventType, i);
yihui 1:a607cd9655d7 254 return;
yihui 1:a607cd9655d7 255 }
yihui 1:a607cd9655d7 256 }
yihui 1:a607cd9655d7 257
yihui 1:a607cd9655d7 258 /* 2.) Changes to the characteristic value will be handled with other events below */
yihui 1:a607cd9655d7 259 eventType = GattServerEvents::GATT_EVENT_DATA_WRITTEN;
yihui 1:a607cd9655d7 260 break;
yihui 1:a607cd9655d7 261
yihui 1:a607cd9655d7 262 case BLE_GATTS_EVT_HVC:
yihui 1:a607cd9655d7 263 /* Indication confirmation received */
yihui 1:a607cd9655d7 264 eventType = GattServerEvents::GATT_EVENT_CONFIRMATION_RECEIVED;
yihui 1:a607cd9655d7 265 handle_value = gattsEventP->params.hvc.handle;
yihui 1:a607cd9655d7 266 break;
yihui 1:a607cd9655d7 267
yihui 1:a607cd9655d7 268 case BLE_EVT_TX_COMPLETE: {
yihui 1:a607cd9655d7 269 handleDataSentEvent(p_ble_evt->evt.common_evt.params.tx_complete.count);
yihui 1:a607cd9655d7 270 return;
yihui 1:a607cd9655d7 271 }
yihui 1:a607cd9655d7 272
yihui 1:a607cd9655d7 273 case BLE_GATTS_EVT_SYS_ATTR_MISSING:
yihui 1:a607cd9655d7 274 sd_ble_gatts_sys_attr_set(gattsEventP->conn_handle, NULL, 0);
yihui 1:a607cd9655d7 275 return;
yihui 1:a607cd9655d7 276
yihui 1:a607cd9655d7 277 default:
yihui 1:a607cd9655d7 278 return;
yihui 1:a607cd9655d7 279 }
yihui 1:a607cd9655d7 280
yihui 1:a607cd9655d7 281 /* Find index (charHandle) in the pool */
yihui 1:a607cd9655d7 282 for (uint8_t i = 0; i<characteristicCount; i++) {
yihui 1:a607cd9655d7 283 if (nrfCharacteristicHandles[i].value_handle == handle_value) {
yihui 1:a607cd9655d7 284 switch (eventType) {
yihui 1:a607cd9655d7 285 case GattServerEvents::GATT_EVENT_DATA_WRITTEN: {
yihui 1:a607cd9655d7 286 GattCharacteristicWriteCBParams cbParams = {
yihui 1:a607cd9655d7 287 .charHandle = i,
yihui 1:a607cd9655d7 288 .op = static_cast<GattCharacteristicWriteCBParams::Type>(gattsEventP->params.write.op),
yihui 1:a607cd9655d7 289 .offset = gattsEventP->params.write.offset,
yihui 1:a607cd9655d7 290 .len = gattsEventP->params.write.len,
yihui 1:a607cd9655d7 291 .data = gattsEventP->params.write.data
yihui 1:a607cd9655d7 292 };
yihui 1:a607cd9655d7 293 handleDataWrittenEvent(&cbParams);
yihui 1:a607cd9655d7 294 break;
yihui 1:a607cd9655d7 295 }
yihui 1:a607cd9655d7 296 default:
yihui 1:a607cd9655d7 297 handleEvent(eventType, i);
yihui 1:a607cd9655d7 298 break;
yihui 1:a607cd9655d7 299 }
yihui 1:a607cd9655d7 300 }
yihui 1:a607cd9655d7 301 }
yihui 1:a607cd9655d7 302 }