AndroidのBLEラジコンプロポアプリ「BLEPropo」と接続し、RCサーボとDCモータを制御するプログラムです。 BLE Nanoで動作を確認しています。 BLEPropo → https://github.com/lipoyang/BLEPropo

Dependencies:   BLE_API mbed

BLEを使ったAndroid用ラジコンプロポアプリ「BLEPropo」に対応するBLE Nano用ファームウェアです。
BLEPropoは、GitHubにて公開中。
https://github.com/lipoyang/BLEPropo
/media/uploads/lipoyang/blepropo_ui.png
ラジコンは、mbed HRM1017とRCサーボやDCモータを組み合わせて作ります。
/media/uploads/lipoyang/ministeer3.jpg
回路図
/media/uploads/lipoyang/ministeer3.pdf

Committer:
lipoyang
Date:
Sat Mar 14 12:02:48 2015 +0000
Revision:
5:7f89fca19a9e
-convert nRF51822 library to a folder

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lipoyang 5:7f89fca19a9e 1 /* mbed Microcontroller Library
lipoyang 5:7f89fca19a9e 2 * Copyright (c) 2006-2013 ARM Limited
lipoyang 5:7f89fca19a9e 3 *
lipoyang 5:7f89fca19a9e 4 * Licensed under the Apache License, Version 2.0 (the "License");
lipoyang 5:7f89fca19a9e 5 * you may not use this file except in compliance with the License.
lipoyang 5:7f89fca19a9e 6 * You may obtain a copy of the License at
lipoyang 5:7f89fca19a9e 7 *
lipoyang 5:7f89fca19a9e 8 * http://www.apache.org/licenses/LICENSE-2.0
lipoyang 5:7f89fca19a9e 9 *
lipoyang 5:7f89fca19a9e 10 * Unless required by applicable law or agreed to in writing, software
lipoyang 5:7f89fca19a9e 11 * distributed under the License is distributed on an "AS IS" BASIS,
lipoyang 5:7f89fca19a9e 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
lipoyang 5:7f89fca19a9e 13 * See the License for the specific language governing permissions and
lipoyang 5:7f89fca19a9e 14 * limitations under the License.
lipoyang 5:7f89fca19a9e 15 */
lipoyang 5:7f89fca19a9e 16
lipoyang 5:7f89fca19a9e 17 #include "nRF51GattServer.h"
lipoyang 5:7f89fca19a9e 18 #include "mbed.h"
lipoyang 5:7f89fca19a9e 19
lipoyang 5:7f89fca19a9e 20 #include "common/common.h"
lipoyang 5:7f89fca19a9e 21 #include "btle/custom/custom_helper.h"
lipoyang 5:7f89fca19a9e 22
lipoyang 5:7f89fca19a9e 23 #include "nRF51Gap.h"
lipoyang 5:7f89fca19a9e 24
lipoyang 5:7f89fca19a9e 25 /**************************************************************************/
lipoyang 5:7f89fca19a9e 26 /*!
lipoyang 5:7f89fca19a9e 27 @brief Adds a new service to the GATT table on the peripheral
lipoyang 5:7f89fca19a9e 28
lipoyang 5:7f89fca19a9e 29 @returns ble_error_t
lipoyang 5:7f89fca19a9e 30
lipoyang 5:7f89fca19a9e 31 @retval BLE_ERROR_NONE
lipoyang 5:7f89fca19a9e 32 Everything executed properly
lipoyang 5:7f89fca19a9e 33
lipoyang 5:7f89fca19a9e 34 @section EXAMPLE
lipoyang 5:7f89fca19a9e 35
lipoyang 5:7f89fca19a9e 36 @code
lipoyang 5:7f89fca19a9e 37
lipoyang 5:7f89fca19a9e 38 @endcode
lipoyang 5:7f89fca19a9e 39 */
lipoyang 5:7f89fca19a9e 40 /**************************************************************************/
lipoyang 5:7f89fca19a9e 41 ble_error_t nRF51GattServer::addService(GattService &service)
lipoyang 5:7f89fca19a9e 42 {
lipoyang 5:7f89fca19a9e 43 /* ToDo: Make sure we don't overflow the array, etc. */
lipoyang 5:7f89fca19a9e 44 /* ToDo: Make sure this service UUID doesn't already exist (?) */
lipoyang 5:7f89fca19a9e 45 /* ToDo: Basic validation */
lipoyang 5:7f89fca19a9e 46
lipoyang 5:7f89fca19a9e 47 /* Add the service to the nRF51 */
lipoyang 5:7f89fca19a9e 48 ble_uuid_t nordicUUID;
lipoyang 5:7f89fca19a9e 49 nordicUUID = custom_convert_to_nordic_uuid(service.getUUID());
lipoyang 5:7f89fca19a9e 50
lipoyang 5:7f89fca19a9e 51 uint16_t serviceHandle;
lipoyang 5:7f89fca19a9e 52 ASSERT( ERROR_NONE ==
lipoyang 5:7f89fca19a9e 53 sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY,
lipoyang 5:7f89fca19a9e 54 &nordicUUID,
lipoyang 5:7f89fca19a9e 55 &serviceHandle),
lipoyang 5:7f89fca19a9e 56 BLE_ERROR_PARAM_OUT_OF_RANGE );
lipoyang 5:7f89fca19a9e 57 service.setHandle(serviceHandle);
lipoyang 5:7f89fca19a9e 58
lipoyang 5:7f89fca19a9e 59 /* Add characteristics to the service */
lipoyang 5:7f89fca19a9e 60 for (uint8_t i = 0; i < service.getCharacteristicCount(); i++) {
lipoyang 5:7f89fca19a9e 61 GattCharacteristic *p_char = service.getCharacteristic(i);
lipoyang 5:7f89fca19a9e 62
lipoyang 5:7f89fca19a9e 63 /* Skip any incompletely defined, read-only characteristics. */
lipoyang 5:7f89fca19a9e 64 if ((p_char->getValueAttribute().getValuePtr() == NULL) &&
lipoyang 5:7f89fca19a9e 65 (p_char->getValueAttribute().getInitialLength() == 0) &&
lipoyang 5:7f89fca19a9e 66 (p_char->getProperties() == GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ)) {
lipoyang 5:7f89fca19a9e 67 continue;
lipoyang 5:7f89fca19a9e 68 }
lipoyang 5:7f89fca19a9e 69
lipoyang 5:7f89fca19a9e 70 nordicUUID = custom_convert_to_nordic_uuid(p_char->getValueAttribute().getUUID());
lipoyang 5:7f89fca19a9e 71
lipoyang 5:7f89fca19a9e 72 ASSERT ( ERROR_NONE ==
lipoyang 5:7f89fca19a9e 73 custom_add_in_characteristic(BLE_GATT_HANDLE_INVALID,
lipoyang 5:7f89fca19a9e 74 &nordicUUID,
lipoyang 5:7f89fca19a9e 75 p_char->getProperties(),
lipoyang 5:7f89fca19a9e 76 p_char->getValueAttribute().getValuePtr(),
lipoyang 5:7f89fca19a9e 77 p_char->getValueAttribute().getInitialLength(),
lipoyang 5:7f89fca19a9e 78 p_char->getValueAttribute().getMaxLength(),
lipoyang 5:7f89fca19a9e 79 p_char->isReadAuthorizationEnabled(),
lipoyang 5:7f89fca19a9e 80 p_char->isWriteAuthorizationEnabled(),
lipoyang 5:7f89fca19a9e 81 &nrfCharacteristicHandles[characteristicCount]),
lipoyang 5:7f89fca19a9e 82 BLE_ERROR_PARAM_OUT_OF_RANGE );
lipoyang 5:7f89fca19a9e 83
lipoyang 5:7f89fca19a9e 84 /* Update the characteristic handle */
lipoyang 5:7f89fca19a9e 85 uint16_t charHandle = characteristicCount;
lipoyang 5:7f89fca19a9e 86 p_characteristics[characteristicCount++] = p_char;
lipoyang 5:7f89fca19a9e 87
lipoyang 5:7f89fca19a9e 88 p_char->getValueAttribute().setHandle(charHandle);
lipoyang 5:7f89fca19a9e 89
lipoyang 5:7f89fca19a9e 90 /* Add optional descriptors if any */
lipoyang 5:7f89fca19a9e 91 /* ToDo: Make sure we don't overflow the array */
lipoyang 5:7f89fca19a9e 92 for (uint8_t j = 0; j < p_char->getDescriptorCount(); j++) {
lipoyang 5:7f89fca19a9e 93 GattAttribute *p_desc = p_char->getDescriptor(j);
lipoyang 5:7f89fca19a9e 94
lipoyang 5:7f89fca19a9e 95 nordicUUID = custom_convert_to_nordic_uuid(p_desc->getUUID());
lipoyang 5:7f89fca19a9e 96
lipoyang 5:7f89fca19a9e 97 ASSERT ( ERROR_NONE ==
lipoyang 5:7f89fca19a9e 98 custom_add_in_descriptor(BLE_GATT_HANDLE_INVALID,
lipoyang 5:7f89fca19a9e 99 &nordicUUID,
lipoyang 5:7f89fca19a9e 100 p_desc->getValuePtr(),
lipoyang 5:7f89fca19a9e 101 p_desc->getInitialLength(),
lipoyang 5:7f89fca19a9e 102 p_desc->getMaxLength(),
lipoyang 5:7f89fca19a9e 103 &nrfDescriptorHandles[descriptorCount]),
lipoyang 5:7f89fca19a9e 104 BLE_ERROR_PARAM_OUT_OF_RANGE );
lipoyang 5:7f89fca19a9e 105
lipoyang 5:7f89fca19a9e 106 uint16_t descHandle = descriptorCount;
lipoyang 5:7f89fca19a9e 107 p_descriptors[descriptorCount++] = p_desc;
lipoyang 5:7f89fca19a9e 108 p_desc->setHandle(descHandle);
lipoyang 5:7f89fca19a9e 109 }
lipoyang 5:7f89fca19a9e 110 }
lipoyang 5:7f89fca19a9e 111
lipoyang 5:7f89fca19a9e 112 serviceCount++;
lipoyang 5:7f89fca19a9e 113
lipoyang 5:7f89fca19a9e 114 return BLE_ERROR_NONE;
lipoyang 5:7f89fca19a9e 115 }
lipoyang 5:7f89fca19a9e 116
lipoyang 5:7f89fca19a9e 117 /**************************************************************************/
lipoyang 5:7f89fca19a9e 118 /*!
lipoyang 5:7f89fca19a9e 119 @brief Reads the value of a characteristic, based on the service
lipoyang 5:7f89fca19a9e 120 and characteristic index fields
lipoyang 5:7f89fca19a9e 121
lipoyang 5:7f89fca19a9e 122 @param[in] charHandle
lipoyang 5:7f89fca19a9e 123 The handle of the GattCharacteristic to read from
lipoyang 5:7f89fca19a9e 124 @param[in] buffer
lipoyang 5:7f89fca19a9e 125 Buffer to hold the the characteristic's value
lipoyang 5:7f89fca19a9e 126 (raw byte array in LSB format)
lipoyang 5:7f89fca19a9e 127 @param[in] len
lipoyang 5:7f89fca19a9e 128 The number of bytes read into the buffer
lipoyang 5:7f89fca19a9e 129
lipoyang 5:7f89fca19a9e 130 @returns ble_error_t
lipoyang 5:7f89fca19a9e 131
lipoyang 5:7f89fca19a9e 132 @retval BLE_ERROR_NONE
lipoyang 5:7f89fca19a9e 133 Everything executed properly
lipoyang 5:7f89fca19a9e 134
lipoyang 5:7f89fca19a9e 135 @section EXAMPLE
lipoyang 5:7f89fca19a9e 136
lipoyang 5:7f89fca19a9e 137 @code
lipoyang 5:7f89fca19a9e 138
lipoyang 5:7f89fca19a9e 139 @endcode
lipoyang 5:7f89fca19a9e 140 */
lipoyang 5:7f89fca19a9e 141 /**************************************************************************/
lipoyang 5:7f89fca19a9e 142 ble_error_t nRF51GattServer::readValue(uint16_t charHandle, uint8_t buffer[], uint16_t *const lengthP)
lipoyang 5:7f89fca19a9e 143 {
lipoyang 5:7f89fca19a9e 144 ASSERT( ERROR_NONE ==
lipoyang 5:7f89fca19a9e 145 sd_ble_gatts_value_get(nrfCharacteristicHandles[charHandle].value_handle, 0, lengthP, buffer),
lipoyang 5:7f89fca19a9e 146 BLE_ERROR_PARAM_OUT_OF_RANGE);
lipoyang 5:7f89fca19a9e 147
lipoyang 5:7f89fca19a9e 148 return BLE_ERROR_NONE;
lipoyang 5:7f89fca19a9e 149 }
lipoyang 5:7f89fca19a9e 150
lipoyang 5:7f89fca19a9e 151 /**************************************************************************/
lipoyang 5:7f89fca19a9e 152 /*!
lipoyang 5:7f89fca19a9e 153 @brief Updates the value of a characteristic, based on the service
lipoyang 5:7f89fca19a9e 154 and characteristic index fields
lipoyang 5:7f89fca19a9e 155
lipoyang 5:7f89fca19a9e 156 @param[in] charHandle
lipoyang 5:7f89fca19a9e 157 The handle of the GattCharacteristic to write to
lipoyang 5:7f89fca19a9e 158 @param[in] buffer
lipoyang 5:7f89fca19a9e 159 Data to use when updating the characteristic's value
lipoyang 5:7f89fca19a9e 160 (raw byte array in LSB format)
lipoyang 5:7f89fca19a9e 161 @param[in] len
lipoyang 5:7f89fca19a9e 162 The number of bytes in buffer
lipoyang 5:7f89fca19a9e 163
lipoyang 5:7f89fca19a9e 164 @returns ble_error_t
lipoyang 5:7f89fca19a9e 165
lipoyang 5:7f89fca19a9e 166 @retval BLE_ERROR_NONE
lipoyang 5:7f89fca19a9e 167 Everything executed properly
lipoyang 5:7f89fca19a9e 168
lipoyang 5:7f89fca19a9e 169 @section EXAMPLE
lipoyang 5:7f89fca19a9e 170
lipoyang 5:7f89fca19a9e 171 @code
lipoyang 5:7f89fca19a9e 172
lipoyang 5:7f89fca19a9e 173 @endcode
lipoyang 5:7f89fca19a9e 174 */
lipoyang 5:7f89fca19a9e 175 /**************************************************************************/
lipoyang 5:7f89fca19a9e 176 ble_error_t nRF51GattServer::updateValue(uint16_t charHandle, uint8_t buffer[], uint16_t len, bool localOnly)
lipoyang 5:7f89fca19a9e 177 {
lipoyang 5:7f89fca19a9e 178 uint16_t gapConnectionHandle = nRF51Gap::getInstance().getConnectionHandle();
lipoyang 5:7f89fca19a9e 179 ble_error_t returnValue = BLE_ERROR_NONE;
lipoyang 5:7f89fca19a9e 180
lipoyang 5:7f89fca19a9e 181 if (localOnly) {
lipoyang 5:7f89fca19a9e 182 /* Only update locally regardless of notify/indicate */
lipoyang 5:7f89fca19a9e 183 ASSERT_INT( ERROR_NONE,
lipoyang 5:7f89fca19a9e 184 sd_ble_gatts_value_set(nrfCharacteristicHandles[charHandle].value_handle, 0, &len, buffer),
lipoyang 5:7f89fca19a9e 185 BLE_ERROR_PARAM_OUT_OF_RANGE );
lipoyang 5:7f89fca19a9e 186 return BLE_ERROR_NONE;
lipoyang 5:7f89fca19a9e 187 }
lipoyang 5:7f89fca19a9e 188
lipoyang 5:7f89fca19a9e 189 if ((p_characteristics[charHandle]->getProperties() &
lipoyang 5:7f89fca19a9e 190 (GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE |
lipoyang 5:7f89fca19a9e 191 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY)) &&
lipoyang 5:7f89fca19a9e 192 (gapConnectionHandle != BLE_CONN_HANDLE_INVALID)) {
lipoyang 5:7f89fca19a9e 193 /* HVX update for the characteristic value */
lipoyang 5:7f89fca19a9e 194 ble_gatts_hvx_params_t hvx_params;
lipoyang 5:7f89fca19a9e 195
lipoyang 5:7f89fca19a9e 196 hvx_params.handle = nrfCharacteristicHandles[charHandle].value_handle;
lipoyang 5:7f89fca19a9e 197 hvx_params.type =
lipoyang 5:7f89fca19a9e 198 (p_characteristics[charHandle]->getProperties() &
lipoyang 5:7f89fca19a9e 199 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) ?
lipoyang 5:7f89fca19a9e 200 BLE_GATT_HVX_NOTIFICATION : BLE_GATT_HVX_INDICATION;
lipoyang 5:7f89fca19a9e 201 hvx_params.offset = 0;
lipoyang 5:7f89fca19a9e 202 hvx_params.p_data = buffer;
lipoyang 5:7f89fca19a9e 203 hvx_params.p_len = &len;
lipoyang 5:7f89fca19a9e 204
lipoyang 5:7f89fca19a9e 205 error_t error = (error_t) sd_ble_gatts_hvx(gapConnectionHandle, &hvx_params);
lipoyang 5:7f89fca19a9e 206
lipoyang 5:7f89fca19a9e 207 /* ERROR_INVALID_STATE, ERROR_BUSY, ERROR_GATTS_SYS_ATTR_MISSING and
lipoyang 5:7f89fca19a9e 208 *ERROR_NO_TX_BUFFERS the ATT table has been updated. */
lipoyang 5:7f89fca19a9e 209 if ((error != ERROR_NONE) && (error != ERROR_INVALID_STATE) &&
lipoyang 5:7f89fca19a9e 210 (error != ERROR_BLE_NO_TX_BUFFERS) && (error != ERROR_BUSY) &&
lipoyang 5:7f89fca19a9e 211 (error != ERROR_BLEGATTS_SYS_ATTR_MISSING)) {
lipoyang 5:7f89fca19a9e 212 ASSERT_INT( ERROR_NONE,
lipoyang 5:7f89fca19a9e 213 sd_ble_gatts_value_set(nrfCharacteristicHandles[charHandle].value_handle, 0, &len, buffer),
lipoyang 5:7f89fca19a9e 214 BLE_ERROR_PARAM_OUT_OF_RANGE );
lipoyang 5:7f89fca19a9e 215 }
lipoyang 5:7f89fca19a9e 216
lipoyang 5:7f89fca19a9e 217 /* Notifications consume application buffers. The return value can
lipoyang 5:7f89fca19a9e 218 be used for resending notifications.
lipoyang 5:7f89fca19a9e 219 */
lipoyang 5:7f89fca19a9e 220 if (error != ERROR_NONE) {
lipoyang 5:7f89fca19a9e 221 returnValue = BLE_STACK_BUSY;
lipoyang 5:7f89fca19a9e 222 }
lipoyang 5:7f89fca19a9e 223 } else {
lipoyang 5:7f89fca19a9e 224 ASSERT_INT( ERROR_NONE,
lipoyang 5:7f89fca19a9e 225 sd_ble_gatts_value_set(nrfCharacteristicHandles[charHandle].value_handle, 0, &len, buffer),
lipoyang 5:7f89fca19a9e 226 BLE_ERROR_PARAM_OUT_OF_RANGE );
lipoyang 5:7f89fca19a9e 227 }
lipoyang 5:7f89fca19a9e 228
lipoyang 5:7f89fca19a9e 229 return returnValue;
lipoyang 5:7f89fca19a9e 230 }
lipoyang 5:7f89fca19a9e 231
lipoyang 5:7f89fca19a9e 232 /**************************************************************************/
lipoyang 5:7f89fca19a9e 233 /*!
lipoyang 5:7f89fca19a9e 234 @brief Callback handler for events getting pushed up from the SD
lipoyang 5:7f89fca19a9e 235 */
lipoyang 5:7f89fca19a9e 236 /**************************************************************************/
lipoyang 5:7f89fca19a9e 237 void nRF51GattServer::hwCallback(ble_evt_t *p_ble_evt)
lipoyang 5:7f89fca19a9e 238 {
lipoyang 5:7f89fca19a9e 239 uint16_t handle_value;
lipoyang 5:7f89fca19a9e 240 GattServerEvents::gattEvent_t eventType;
lipoyang 5:7f89fca19a9e 241 const ble_gatts_evt_t *gattsEventP = &p_ble_evt->evt.gatts_evt;
lipoyang 5:7f89fca19a9e 242
lipoyang 5:7f89fca19a9e 243 switch (p_ble_evt->header.evt_id) {
lipoyang 5:7f89fca19a9e 244 case BLE_GATTS_EVT_WRITE:
lipoyang 5:7f89fca19a9e 245 /* There are 2 use case here: Values being updated & CCCD (indicate/notify) enabled */
lipoyang 5:7f89fca19a9e 246
lipoyang 5:7f89fca19a9e 247 /* 1.) Handle CCCD changes */
lipoyang 5:7f89fca19a9e 248 handle_value = gattsEventP->params.write.handle;
lipoyang 5:7f89fca19a9e 249 for (uint8_t i = 0; i<characteristicCount; i++) {
lipoyang 5:7f89fca19a9e 250 if ((p_characteristics[i]->getProperties() & (GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY)) &&
lipoyang 5:7f89fca19a9e 251 (nrfCharacteristicHandles[i].cccd_handle == handle_value)) {
lipoyang 5:7f89fca19a9e 252 uint16_t cccd_value =
lipoyang 5:7f89fca19a9e 253 (gattsEventP->params.write.data[1] << 8) |
lipoyang 5:7f89fca19a9e 254 gattsEventP->params.write.data[0]; /* Little Endian but M0 may be mis-aligned */
lipoyang 5:7f89fca19a9e 255
lipoyang 5:7f89fca19a9e 256 if (((p_characteristics[i]->getProperties() & GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE) && (cccd_value & BLE_GATT_HVX_INDICATION)) ||
lipoyang 5:7f89fca19a9e 257 ((p_characteristics[i]->getProperties() & GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) && (cccd_value & BLE_GATT_HVX_NOTIFICATION))) {
lipoyang 5:7f89fca19a9e 258 eventType = GattServerEvents::GATT_EVENT_UPDATES_ENABLED;
lipoyang 5:7f89fca19a9e 259 } else {
lipoyang 5:7f89fca19a9e 260 eventType = GattServerEvents::GATT_EVENT_UPDATES_DISABLED;
lipoyang 5:7f89fca19a9e 261 }
lipoyang 5:7f89fca19a9e 262
lipoyang 5:7f89fca19a9e 263 handleEvent(eventType, i);
lipoyang 5:7f89fca19a9e 264 return;
lipoyang 5:7f89fca19a9e 265 }
lipoyang 5:7f89fca19a9e 266 }
lipoyang 5:7f89fca19a9e 267
lipoyang 5:7f89fca19a9e 268 /* 2.) Changes to the characteristic value will be handled with other events below */
lipoyang 5:7f89fca19a9e 269 eventType = GattServerEvents::GATT_EVENT_DATA_WRITTEN;
lipoyang 5:7f89fca19a9e 270 break;
lipoyang 5:7f89fca19a9e 271
lipoyang 5:7f89fca19a9e 272 case BLE_GATTS_EVT_HVC:
lipoyang 5:7f89fca19a9e 273 /* Indication confirmation received */
lipoyang 5:7f89fca19a9e 274 eventType = GattServerEvents::GATT_EVENT_CONFIRMATION_RECEIVED;
lipoyang 5:7f89fca19a9e 275 handle_value = gattsEventP->params.hvc.handle;
lipoyang 5:7f89fca19a9e 276 break;
lipoyang 5:7f89fca19a9e 277
lipoyang 5:7f89fca19a9e 278 case BLE_EVT_TX_COMPLETE: {
lipoyang 5:7f89fca19a9e 279 handleDataSentEvent(p_ble_evt->evt.common_evt.params.tx_complete.count);
lipoyang 5:7f89fca19a9e 280 return;
lipoyang 5:7f89fca19a9e 281 }
lipoyang 5:7f89fca19a9e 282
lipoyang 5:7f89fca19a9e 283 case BLE_GATTS_EVT_SYS_ATTR_MISSING:
lipoyang 5:7f89fca19a9e 284 sd_ble_gatts_sys_attr_set(gattsEventP->conn_handle, NULL, 0);
lipoyang 5:7f89fca19a9e 285 return;
lipoyang 5:7f89fca19a9e 286
lipoyang 5:7f89fca19a9e 287 case BLE_GATTS_EVT_RW_AUTHORIZE_REQUEST:
lipoyang 5:7f89fca19a9e 288 switch (gattsEventP->params.authorize_request.type) {
lipoyang 5:7f89fca19a9e 289 case BLE_GATTS_AUTHORIZE_TYPE_READ:
lipoyang 5:7f89fca19a9e 290 eventType = GattServerEvents::GATT_EVENT_READ_AUTHORIZATION_REQ;
lipoyang 5:7f89fca19a9e 291 handle_value = gattsEventP->params.authorize_request.request.read.handle;
lipoyang 5:7f89fca19a9e 292 break;
lipoyang 5:7f89fca19a9e 293 case BLE_GATTS_AUTHORIZE_TYPE_WRITE:
lipoyang 5:7f89fca19a9e 294 eventType = GattServerEvents::GATT_EVENT_WRITE_AUTHORIZATION_REQ;
lipoyang 5:7f89fca19a9e 295 handle_value = gattsEventP->params.authorize_request.request.write.handle;
lipoyang 5:7f89fca19a9e 296 break;
lipoyang 5:7f89fca19a9e 297 default:
lipoyang 5:7f89fca19a9e 298 return;
lipoyang 5:7f89fca19a9e 299 }
lipoyang 5:7f89fca19a9e 300 break;
lipoyang 5:7f89fca19a9e 301
lipoyang 5:7f89fca19a9e 302 default:
lipoyang 5:7f89fca19a9e 303 return;
lipoyang 5:7f89fca19a9e 304 }
lipoyang 5:7f89fca19a9e 305
lipoyang 5:7f89fca19a9e 306 /* Find index (charHandle) in the pool */
lipoyang 5:7f89fca19a9e 307 for (uint8_t i = 0; i < characteristicCount; i++) {
lipoyang 5:7f89fca19a9e 308 if (nrfCharacteristicHandles[i].value_handle == handle_value) {
lipoyang 5:7f89fca19a9e 309 switch (eventType) {
lipoyang 5:7f89fca19a9e 310 case GattServerEvents::GATT_EVENT_DATA_WRITTEN: {
lipoyang 5:7f89fca19a9e 311 GattCharacteristicWriteCBParams cbParams = {
lipoyang 5:7f89fca19a9e 312 .charHandle = i,
lipoyang 5:7f89fca19a9e 313 .op = static_cast<GattCharacteristicWriteCBParams::Type>(gattsEventP->params.write.op),
lipoyang 5:7f89fca19a9e 314 .offset = gattsEventP->params.write.offset,
lipoyang 5:7f89fca19a9e 315 .len = gattsEventP->params.write.len,
lipoyang 5:7f89fca19a9e 316 .data = gattsEventP->params.write.data
lipoyang 5:7f89fca19a9e 317 };
lipoyang 5:7f89fca19a9e 318 handleDataWrittenEvent(&cbParams);
lipoyang 5:7f89fca19a9e 319 break;
lipoyang 5:7f89fca19a9e 320 }
lipoyang 5:7f89fca19a9e 321 case GattServerEvents::GATT_EVENT_WRITE_AUTHORIZATION_REQ: {
lipoyang 5:7f89fca19a9e 322 GattCharacteristicWriteAuthCBParams cbParams = {
lipoyang 5:7f89fca19a9e 323 .charHandle = i,
lipoyang 5:7f89fca19a9e 324 .offset = gattsEventP->params.authorize_request.request.write.offset,
lipoyang 5:7f89fca19a9e 325 .len = gattsEventP->params.authorize_request.request.write.len,
lipoyang 5:7f89fca19a9e 326 .data = gattsEventP->params.authorize_request.request.write.data,
lipoyang 5:7f89fca19a9e 327 };
lipoyang 5:7f89fca19a9e 328 ble_gatts_rw_authorize_reply_params_t reply = {
lipoyang 5:7f89fca19a9e 329 .type = BLE_GATTS_AUTHORIZE_TYPE_WRITE,
lipoyang 5:7f89fca19a9e 330 .params = {
lipoyang 5:7f89fca19a9e 331 .write = {
lipoyang 5:7f89fca19a9e 332 .gatt_status = (p_characteristics[i]->authorizeWrite(&cbParams) ?
lipoyang 5:7f89fca19a9e 333 BLE_GATT_STATUS_SUCCESS : BLE_GATT_STATUS_ATTERR_WRITE_NOT_PERMITTED)
lipoyang 5:7f89fca19a9e 334 }
lipoyang 5:7f89fca19a9e 335 }
lipoyang 5:7f89fca19a9e 336 };
lipoyang 5:7f89fca19a9e 337 sd_ble_gatts_rw_authorize_reply(gattsEventP->conn_handle, &reply);
lipoyang 5:7f89fca19a9e 338
lipoyang 5:7f89fca19a9e 339 /*
lipoyang 5:7f89fca19a9e 340 * If write-authorization is enabled for a characteristic,
lipoyang 5:7f89fca19a9e 341 * AUTHORIZATION_REQ event (if replied with true) is *not*
lipoyang 5:7f89fca19a9e 342 * followed by another DATA_WRITTEN event; so we still need
lipoyang 5:7f89fca19a9e 343 * to invoke handleDataWritten(), much the same as we would
lipoyang 5:7f89fca19a9e 344 * have done if write-authorization had not been enabled.
lipoyang 5:7f89fca19a9e 345 */
lipoyang 5:7f89fca19a9e 346 if (reply.params.write.gatt_status == BLE_GATT_STATUS_SUCCESS) {
lipoyang 5:7f89fca19a9e 347 GattCharacteristicWriteCBParams cbParams = {
lipoyang 5:7f89fca19a9e 348 .charHandle = i,
lipoyang 5:7f89fca19a9e 349 .op = static_cast<GattCharacteristicWriteCBParams::Type>(gattsEventP->params.authorize_request.request.write.op),
lipoyang 5:7f89fca19a9e 350 .offset = gattsEventP->params.authorize_request.request.write.offset,
lipoyang 5:7f89fca19a9e 351 .len = gattsEventP->params.authorize_request.request.write.len,
lipoyang 5:7f89fca19a9e 352 .data = gattsEventP->params.authorize_request.request.write.data,
lipoyang 5:7f89fca19a9e 353 };
lipoyang 5:7f89fca19a9e 354 handleDataWrittenEvent(&cbParams);
lipoyang 5:7f89fca19a9e 355 }
lipoyang 5:7f89fca19a9e 356 break;
lipoyang 5:7f89fca19a9e 357 }
lipoyang 5:7f89fca19a9e 358 case GattServerEvents::GATT_EVENT_READ_AUTHORIZATION_REQ: {
lipoyang 5:7f89fca19a9e 359 GattCharacteristicReadAuthCBParams cbParams = {
lipoyang 5:7f89fca19a9e 360 .charHandle = i,
lipoyang 5:7f89fca19a9e 361 .offset = gattsEventP->params.authorize_request.request.read.offset,
lipoyang 5:7f89fca19a9e 362 .len = 0,
lipoyang 5:7f89fca19a9e 363 .data = NULL
lipoyang 5:7f89fca19a9e 364 };
lipoyang 5:7f89fca19a9e 365
lipoyang 5:7f89fca19a9e 366 /* Ask for authorization and, potentially, new data. Use updated parameters to construct reply. */
lipoyang 5:7f89fca19a9e 367 p_characteristics[i]->authorizeRead(&cbParams);
lipoyang 5:7f89fca19a9e 368
lipoyang 5:7f89fca19a9e 369 ble_gatts_rw_authorize_reply_params_t reply = {
lipoyang 5:7f89fca19a9e 370 .type = BLE_GATTS_AUTHORIZE_TYPE_READ
lipoyang 5:7f89fca19a9e 371 };
lipoyang 5:7f89fca19a9e 372
lipoyang 5:7f89fca19a9e 373 if (cbParams.authorizationReply == true) {
lipoyang 5:7f89fca19a9e 374 reply.params.read.gatt_status = BLE_GATT_STATUS_SUCCESS;
lipoyang 5:7f89fca19a9e 375
lipoyang 5:7f89fca19a9e 376 if (cbParams.data != NULL) {
lipoyang 5:7f89fca19a9e 377 reply.params.read.update = 1;
lipoyang 5:7f89fca19a9e 378 reply.params.read.offset = cbParams.offset;
lipoyang 5:7f89fca19a9e 379 reply.params.read.len = cbParams.len;
lipoyang 5:7f89fca19a9e 380 reply.params.read.p_data = cbParams.data;
lipoyang 5:7f89fca19a9e 381 }
lipoyang 5:7f89fca19a9e 382 } else {
lipoyang 5:7f89fca19a9e 383 reply.params.read.gatt_status = BLE_GATT_STATUS_ATTERR_READ_NOT_PERMITTED;
lipoyang 5:7f89fca19a9e 384 }
lipoyang 5:7f89fca19a9e 385
lipoyang 5:7f89fca19a9e 386 sd_ble_gatts_rw_authorize_reply(gattsEventP->conn_handle, &reply);
lipoyang 5:7f89fca19a9e 387 break;
lipoyang 5:7f89fca19a9e 388 }
lipoyang 5:7f89fca19a9e 389
lipoyang 5:7f89fca19a9e 390 default:
lipoyang 5:7f89fca19a9e 391 handleEvent(eventType, i);
lipoyang 5:7f89fca19a9e 392 break;
lipoyang 5:7f89fca19a9e 393 }
lipoyang 5:7f89fca19a9e 394 }
lipoyang 5:7f89fca19a9e 395 }
lipoyang 5:7f89fca19a9e 396 }
lipoyang 5:7f89fca19a9e 397
lipoyang 5:7f89fca19a9e 398 ble_error_t
lipoyang 5:7f89fca19a9e 399 nRF51GattServer::initializeGATTDatabase(void)
lipoyang 5:7f89fca19a9e 400 {
lipoyang 5:7f89fca19a9e 401 /* Empty. Services are populated in the GattDatabase through addService(). */
lipoyang 5:7f89fca19a9e 402 return BLE_ERROR_NONE;
lipoyang 5:7f89fca19a9e 403 }