fork

Fork of nRF51822 by Nordic Semiconductor

Committer:
Rohit Grover
Date:
Wed May 28 12:55:18 2014 +0100
Revision:
6:bbb4357dc135
Parent:
2:49e7ee9e88ab
Child:
7:dd6465921aaa
running uncrustify against a few sources

Who changed what in which revision?

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