config changes

Fork of nRF51822 by Nordic Semiconductor

Committer:
rgrover1
Date:
Tue Jul 21 13:23:44 2015 +0100
Revision:
389:5982ee136c47
Synchronized with git rev b743df2a
Author: Andrew Fernandes
Remove unnecessary 'compiler_abstraction.h' to get rid of duplicate '__ASM' warning.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rgrover1 389:5982ee136c47 1 /* mbed Microcontroller Library
rgrover1 389:5982ee136c47 2 * Copyright (c) 2006-2013 ARM Limited
rgrover1 389:5982ee136c47 3 *
rgrover1 389:5982ee136c47 4 * Licensed under the Apache License, Version 2.0 (the "License");
rgrover1 389:5982ee136c47 5 * you may not use this file except in compliance with the License.
rgrover1 389:5982ee136c47 6 * You may obtain a copy of the License at
rgrover1 389:5982ee136c47 7 *
rgrover1 389:5982ee136c47 8 * http://www.apache.org/licenses/LICENSE-2.0
rgrover1 389:5982ee136c47 9 *
rgrover1 389:5982ee136c47 10 * Unless required by applicable law or agreed to in writing, software
rgrover1 389:5982ee136c47 11 * distributed under the License is distributed on an "AS IS" BASIS,
rgrover1 389:5982ee136c47 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
rgrover1 389:5982ee136c47 13 * See the License for the specific language governing permissions and
rgrover1 389:5982ee136c47 14 * limitations under the License.
rgrover1 389:5982ee136c47 15 */
rgrover1 389:5982ee136c47 16
rgrover1 389:5982ee136c47 17 #include "nRF51ServiceDiscovery.h"
rgrover1 389:5982ee136c47 18
rgrover1 389:5982ee136c47 19 ble_error_t
rgrover1 389:5982ee136c47 20 nRF51ServiceDiscovery::launchCharacteristicDiscovery(Gap::Handle_t connectionHandle,
rgrover1 389:5982ee136c47 21 Gap::Handle_t startHandle,
rgrover1 389:5982ee136c47 22 Gap::Handle_t endHandle)
rgrover1 389:5982ee136c47 23 {
rgrover1 389:5982ee136c47 24 characteristicDiscoveryStarted(connectionHandle);
rgrover1 389:5982ee136c47 25
rgrover1 389:5982ee136c47 26 ble_gattc_handle_range_t handleRange = {
rgrover1 389:5982ee136c47 27 .start_handle = startHandle,
rgrover1 389:5982ee136c47 28 .end_handle = endHandle
rgrover1 389:5982ee136c47 29 };
rgrover1 389:5982ee136c47 30 uint32_t rc;
rgrover1 389:5982ee136c47 31 if ((rc = sd_ble_gattc_characteristics_discover(connectionHandle, &handleRange)) != NRF_SUCCESS) {
rgrover1 389:5982ee136c47 32 terminateCharacteristicDiscovery();
rgrover1 389:5982ee136c47 33 switch (rc) {
rgrover1 389:5982ee136c47 34 case BLE_ERROR_INVALID_CONN_HANDLE:
rgrover1 389:5982ee136c47 35 case NRF_ERROR_INVALID_ADDR:
rgrover1 389:5982ee136c47 36 return BLE_ERROR_INVALID_PARAM;
rgrover1 389:5982ee136c47 37 case NRF_ERROR_BUSY:
rgrover1 389:5982ee136c47 38 return BLE_STACK_BUSY;
rgrover1 389:5982ee136c47 39 default:
rgrover1 389:5982ee136c47 40 case NRF_ERROR_INVALID_STATE:
rgrover1 389:5982ee136c47 41 return BLE_ERROR_INVALID_STATE;
rgrover1 389:5982ee136c47 42 }
rgrover1 389:5982ee136c47 43 }
rgrover1 389:5982ee136c47 44
rgrover1 389:5982ee136c47 45 return BLE_ERROR_NONE;
rgrover1 389:5982ee136c47 46 }
rgrover1 389:5982ee136c47 47
rgrover1 389:5982ee136c47 48 void
rgrover1 389:5982ee136c47 49 nRF51ServiceDiscovery::setupDiscoveredServices(const ble_gattc_evt_prim_srvc_disc_rsp_t *response)
rgrover1 389:5982ee136c47 50 {
rgrover1 389:5982ee136c47 51 serviceIndex = 0;
rgrover1 389:5982ee136c47 52 numServices = response->count;
rgrover1 389:5982ee136c47 53
rgrover1 389:5982ee136c47 54 /* Account for the limitation on the number of discovered services we can handle at a time. */
rgrover1 389:5982ee136c47 55 if (numServices > BLE_DB_DISCOVERY_MAX_SRV) {
rgrover1 389:5982ee136c47 56 numServices = BLE_DB_DISCOVERY_MAX_SRV;
rgrover1 389:5982ee136c47 57 }
rgrover1 389:5982ee136c47 58
rgrover1 389:5982ee136c47 59 serviceUUIDDiscoveryQueue.reset();
rgrover1 389:5982ee136c47 60 for (unsigned serviceIndex = 0; serviceIndex < numServices; serviceIndex++) {
rgrover1 389:5982ee136c47 61 if (response->services[serviceIndex].uuid.type == BLE_UUID_TYPE_UNKNOWN) {
rgrover1 389:5982ee136c47 62 serviceUUIDDiscoveryQueue.enqueue(serviceIndex);
rgrover1 389:5982ee136c47 63 services[serviceIndex].setup(response->services[serviceIndex].handle_range.start_handle,
rgrover1 389:5982ee136c47 64 response->services[serviceIndex].handle_range.end_handle);
rgrover1 389:5982ee136c47 65 } else {
rgrover1 389:5982ee136c47 66 services[serviceIndex].setup(response->services[serviceIndex].uuid.uuid,
rgrover1 389:5982ee136c47 67 response->services[serviceIndex].handle_range.start_handle,
rgrover1 389:5982ee136c47 68 response->services[serviceIndex].handle_range.end_handle);
rgrover1 389:5982ee136c47 69 }
rgrover1 389:5982ee136c47 70 }
rgrover1 389:5982ee136c47 71
rgrover1 389:5982ee136c47 72 /* Trigger discovery of service UUID if necessary. */
rgrover1 389:5982ee136c47 73 if (serviceUUIDDiscoveryQueue.getCount()) {
rgrover1 389:5982ee136c47 74 serviceUUIDDiscoveryQueue.triggerFirst();
rgrover1 389:5982ee136c47 75 }
rgrover1 389:5982ee136c47 76 }
rgrover1 389:5982ee136c47 77
rgrover1 389:5982ee136c47 78 void
rgrover1 389:5982ee136c47 79 nRF51ServiceDiscovery::setupDiscoveredCharacteristics(const ble_gattc_evt_char_disc_rsp_t *response)
rgrover1 389:5982ee136c47 80 {
rgrover1 389:5982ee136c47 81 characteristicIndex = 0;
rgrover1 389:5982ee136c47 82 numCharacteristics = response->count;
rgrover1 389:5982ee136c47 83
rgrover1 389:5982ee136c47 84 /* Account for the limitation on the number of discovered characteristics we can handle at a time. */
rgrover1 389:5982ee136c47 85 if (numCharacteristics > BLE_DB_DISCOVERY_MAX_CHAR_PER_SRV) {
rgrover1 389:5982ee136c47 86 numCharacteristics = BLE_DB_DISCOVERY_MAX_CHAR_PER_SRV;
rgrover1 389:5982ee136c47 87 }
rgrover1 389:5982ee136c47 88
rgrover1 389:5982ee136c47 89 charUUIDDiscoveryQueue.reset();
rgrover1 389:5982ee136c47 90 for (unsigned charIndex = 0; charIndex < numCharacteristics; charIndex++) {
rgrover1 389:5982ee136c47 91 if (response->chars[charIndex].uuid.type == BLE_UUID_TYPE_UNKNOWN) {
rgrover1 389:5982ee136c47 92 charUUIDDiscoveryQueue.enqueue(charIndex);
rgrover1 389:5982ee136c47 93 characteristics[charIndex].setup(gattc,
rgrover1 389:5982ee136c47 94 connHandle,
rgrover1 389:5982ee136c47 95 response->chars[charIndex].char_props,
rgrover1 389:5982ee136c47 96 response->chars[charIndex].handle_decl,
rgrover1 389:5982ee136c47 97 response->chars[charIndex].handle_value);
rgrover1 389:5982ee136c47 98 } else {
rgrover1 389:5982ee136c47 99 characteristics[charIndex].setup(gattc,
rgrover1 389:5982ee136c47 100 connHandle,
rgrover1 389:5982ee136c47 101 response->chars[charIndex].uuid.uuid,
rgrover1 389:5982ee136c47 102 response->chars[charIndex].char_props,
rgrover1 389:5982ee136c47 103 response->chars[charIndex].handle_decl,
rgrover1 389:5982ee136c47 104 response->chars[charIndex].handle_value);
rgrover1 389:5982ee136c47 105 }
rgrover1 389:5982ee136c47 106 }
rgrover1 389:5982ee136c47 107
rgrover1 389:5982ee136c47 108 /* Trigger discovery of char UUID if necessary. */
rgrover1 389:5982ee136c47 109 if (charUUIDDiscoveryQueue.getCount()) {
rgrover1 389:5982ee136c47 110 charUUIDDiscoveryQueue.triggerFirst();
rgrover1 389:5982ee136c47 111 }
rgrover1 389:5982ee136c47 112 }
rgrover1 389:5982ee136c47 113
rgrover1 389:5982ee136c47 114 void
rgrover1 389:5982ee136c47 115 nRF51ServiceDiscovery::progressCharacteristicDiscovery(void)
rgrover1 389:5982ee136c47 116 {
rgrover1 389:5982ee136c47 117 /* Iterate through the previously discovered characteristics cached in characteristics[]. */
rgrover1 389:5982ee136c47 118 while ((state == CHARACTERISTIC_DISCOVERY_ACTIVE) && (characteristicIndex < numCharacteristics)) {
rgrover1 389:5982ee136c47 119 if ((matchingCharacteristicUUID == UUID::ShortUUIDBytes_t(BLE_UUID_UNKNOWN)) ||
rgrover1 389:5982ee136c47 120 ((matchingCharacteristicUUID == characteristics[characteristicIndex].getShortUUID()) &&
rgrover1 389:5982ee136c47 121 (matchingServiceUUID != UUID::ShortUUIDBytes_t(BLE_UUID_UNKNOWN)))) {
rgrover1 389:5982ee136c47 122 if (characteristicCallback) {
rgrover1 389:5982ee136c47 123 characteristicCallback(&characteristics[characteristicIndex]);
rgrover1 389:5982ee136c47 124 }
rgrover1 389:5982ee136c47 125 }
rgrover1 389:5982ee136c47 126
rgrover1 389:5982ee136c47 127 characteristicIndex++;
rgrover1 389:5982ee136c47 128 }
rgrover1 389:5982ee136c47 129
rgrover1 389:5982ee136c47 130 /* Relaunch discovery of new characteristics beyond the last entry cached in characteristics[]. */
rgrover1 389:5982ee136c47 131 if (state == CHARACTERISTIC_DISCOVERY_ACTIVE) {
rgrover1 389:5982ee136c47 132 /* Determine the ending handle of the last cached characteristic. */
rgrover1 389:5982ee136c47 133 Gap::Handle_t startHandle = characteristics[characteristicIndex - 1].getValueHandle() + 1;
rgrover1 389:5982ee136c47 134 Gap::Handle_t endHandle = services[serviceIndex].getEndHandle();
rgrover1 389:5982ee136c47 135 resetDiscoveredCharacteristics(); /* Note: resetDiscoveredCharacteristics() must come after fetching start and end Handles. */
rgrover1 389:5982ee136c47 136
rgrover1 389:5982ee136c47 137 if (startHandle < endHandle) {
rgrover1 389:5982ee136c47 138 ble_gattc_handle_range_t handleRange = {
rgrover1 389:5982ee136c47 139 .start_handle = startHandle,
rgrover1 389:5982ee136c47 140 .end_handle = endHandle
rgrover1 389:5982ee136c47 141 };
rgrover1 389:5982ee136c47 142 if (sd_ble_gattc_characteristics_discover(connHandle, &handleRange) != NRF_SUCCESS) {
rgrover1 389:5982ee136c47 143 terminateCharacteristicDiscovery();
rgrover1 389:5982ee136c47 144 }
rgrover1 389:5982ee136c47 145 } else {
rgrover1 389:5982ee136c47 146 terminateCharacteristicDiscovery();
rgrover1 389:5982ee136c47 147 }
rgrover1 389:5982ee136c47 148 }
rgrover1 389:5982ee136c47 149 }
rgrover1 389:5982ee136c47 150
rgrover1 389:5982ee136c47 151 void
rgrover1 389:5982ee136c47 152 nRF51ServiceDiscovery::progressServiceDiscovery(void)
rgrover1 389:5982ee136c47 153 {
rgrover1 389:5982ee136c47 154 /* Iterate through the previously discovered services cached in services[]. */
rgrover1 389:5982ee136c47 155 while ((state == SERVICE_DISCOVERY_ACTIVE) && (serviceIndex < numServices)) {
rgrover1 389:5982ee136c47 156 if ((matchingServiceUUID == UUID::ShortUUIDBytes_t(BLE_UUID_UNKNOWN)) ||
rgrover1 389:5982ee136c47 157 (matchingServiceUUID == services[serviceIndex].getUUID().getShortUUID())) {
rgrover1 389:5982ee136c47 158
rgrover1 389:5982ee136c47 159 if (serviceCallback && (matchingCharacteristicUUID == UUID::ShortUUIDBytes_t(BLE_UUID_UNKNOWN))) {
rgrover1 389:5982ee136c47 160 serviceCallback(&services[serviceIndex]);
rgrover1 389:5982ee136c47 161 }
rgrover1 389:5982ee136c47 162
rgrover1 389:5982ee136c47 163 if ((state == SERVICE_DISCOVERY_ACTIVE) && characteristicCallback) {
rgrover1 389:5982ee136c47 164 launchCharacteristicDiscovery(connHandle, services[serviceIndex].getStartHandle(), services[serviceIndex].getEndHandle());
rgrover1 389:5982ee136c47 165 } else {
rgrover1 389:5982ee136c47 166 serviceIndex++;
rgrover1 389:5982ee136c47 167 }
rgrover1 389:5982ee136c47 168 } else {
rgrover1 389:5982ee136c47 169 serviceIndex++;
rgrover1 389:5982ee136c47 170 }
rgrover1 389:5982ee136c47 171 }
rgrover1 389:5982ee136c47 172
rgrover1 389:5982ee136c47 173 /* Relaunch discovery of new services beyond the last entry cached in services[]. */
rgrover1 389:5982ee136c47 174 if ((state == SERVICE_DISCOVERY_ACTIVE) && (numServices > 0) && (serviceIndex > 0)) {
rgrover1 389:5982ee136c47 175 /* Determine the ending handle of the last cached service. */
rgrover1 389:5982ee136c47 176 Gap::Handle_t endHandle = services[serviceIndex - 1].getEndHandle();
rgrover1 389:5982ee136c47 177 resetDiscoveredServices(); /* Note: resetDiscoveredServices() must come after fetching endHandle. */
rgrover1 389:5982ee136c47 178
rgrover1 389:5982ee136c47 179 if (endHandle == SRV_DISC_END_HANDLE) {
rgrover1 389:5982ee136c47 180 terminateServiceDiscovery();
rgrover1 389:5982ee136c47 181 } else {
rgrover1 389:5982ee136c47 182 if (sd_ble_gattc_primary_services_discover(connHandle, endHandle, NULL) != NRF_SUCCESS) {
rgrover1 389:5982ee136c47 183 terminateServiceDiscovery();
rgrover1 389:5982ee136c47 184 }
rgrover1 389:5982ee136c47 185 }
rgrover1 389:5982ee136c47 186 }
rgrover1 389:5982ee136c47 187 }
rgrover1 389:5982ee136c47 188
rgrover1 389:5982ee136c47 189 void
rgrover1 389:5982ee136c47 190 nRF51ServiceDiscovery::ServiceUUIDDiscoveryQueue::triggerFirst(void)
rgrover1 389:5982ee136c47 191 {
rgrover1 389:5982ee136c47 192 while (numIndices) { /* loop until a call to char_value_by_uuid_read() succeeds or we run out of pending indices. */
rgrover1 389:5982ee136c47 193 parentDiscoveryObject->state = DISCOVER_SERVICE_UUIDS;
rgrover1 389:5982ee136c47 194
rgrover1 389:5982ee136c47 195 unsigned serviceIndex = getFirst();
rgrover1 389:5982ee136c47 196 ble_uuid_t uuid = {
rgrover1 389:5982ee136c47 197 .uuid = BLE_UUID_SERVICE_PRIMARY,
rgrover1 389:5982ee136c47 198 .type = BLE_UUID_TYPE_BLE,
rgrover1 389:5982ee136c47 199 };
rgrover1 389:5982ee136c47 200 ble_gattc_handle_range_t handleRange = {
rgrover1 389:5982ee136c47 201 .start_handle = parentDiscoveryObject->services[serviceIndex].getStartHandle(),
rgrover1 389:5982ee136c47 202 .end_handle = parentDiscoveryObject->services[serviceIndex].getEndHandle(),
rgrover1 389:5982ee136c47 203 };
rgrover1 389:5982ee136c47 204 if (sd_ble_gattc_char_value_by_uuid_read(parentDiscoveryObject->connHandle, &uuid, &handleRange) == NRF_SUCCESS) {
rgrover1 389:5982ee136c47 205 return;
rgrover1 389:5982ee136c47 206 }
rgrover1 389:5982ee136c47 207
rgrover1 389:5982ee136c47 208 /* Skip this service if we fail to launch a read for its service-declaration
rgrover1 389:5982ee136c47 209 * attribute. Its UUID will remain INVALID, and it may not match any filters. */
rgrover1 389:5982ee136c47 210 dequeue();
rgrover1 389:5982ee136c47 211 }
rgrover1 389:5982ee136c47 212
rgrover1 389:5982ee136c47 213 /* Switch back to service discovery upon exhausting the service-indices pending UUID discovery. */
rgrover1 389:5982ee136c47 214 if (parentDiscoveryObject->state == DISCOVER_SERVICE_UUIDS) {
rgrover1 389:5982ee136c47 215 parentDiscoveryObject->state = SERVICE_DISCOVERY_ACTIVE;
rgrover1 389:5982ee136c47 216 }
rgrover1 389:5982ee136c47 217 }
rgrover1 389:5982ee136c47 218
rgrover1 389:5982ee136c47 219 void
rgrover1 389:5982ee136c47 220 nRF51ServiceDiscovery::CharUUIDDiscoveryQueue::triggerFirst(void)
rgrover1 389:5982ee136c47 221 {
rgrover1 389:5982ee136c47 222 while (numIndices) { /* loop until a call to char_value_by_uuid_read() succeeds or we run out of pending indices. */
rgrover1 389:5982ee136c47 223 parentDiscoveryObject->state = DISCOVER_CHARACTERISTIC_UUIDS;
rgrover1 389:5982ee136c47 224
rgrover1 389:5982ee136c47 225 unsigned charIndex = getFirst();
rgrover1 389:5982ee136c47 226 ble_uuid_t uuid = {
rgrover1 389:5982ee136c47 227 .uuid = BLE_UUID_CHARACTERISTIC,
rgrover1 389:5982ee136c47 228 .type = BLE_UUID_TYPE_BLE,
rgrover1 389:5982ee136c47 229 };
rgrover1 389:5982ee136c47 230 ble_gattc_handle_range_t handleRange = { };
rgrover1 389:5982ee136c47 231 handleRange.start_handle = parentDiscoveryObject->characteristics[charIndex].getDeclHandle();
rgrover1 389:5982ee136c47 232 handleRange.end_handle = parentDiscoveryObject->characteristics[charIndex].getDeclHandle() + 1;
rgrover1 389:5982ee136c47 233 if (sd_ble_gattc_char_value_by_uuid_read(parentDiscoveryObject->connHandle, &uuid, &handleRange) == NRF_SUCCESS) {
rgrover1 389:5982ee136c47 234 return;
rgrover1 389:5982ee136c47 235 }
rgrover1 389:5982ee136c47 236
rgrover1 389:5982ee136c47 237 /* Skip this service if we fail to launch a read for its service-declaration
rgrover1 389:5982ee136c47 238 * attribute. Its UUID will remain INVALID, and it may not match any filters. */
rgrover1 389:5982ee136c47 239 dequeue();
rgrover1 389:5982ee136c47 240 }
rgrover1 389:5982ee136c47 241
rgrover1 389:5982ee136c47 242 /* Switch back to service discovery upon exhausting the service-indices pending UUID discovery. */
rgrover1 389:5982ee136c47 243 if (parentDiscoveryObject->state == DISCOVER_CHARACTERISTIC_UUIDS) {
rgrover1 389:5982ee136c47 244 parentDiscoveryObject->state = CHARACTERISTIC_DISCOVERY_ACTIVE;
rgrover1 389:5982ee136c47 245 }
rgrover1 389:5982ee136c47 246 }
rgrover1 389:5982ee136c47 247
rgrover1 389:5982ee136c47 248 void
rgrover1 389:5982ee136c47 249 nRF51ServiceDiscovery::processDiscoverUUIDResponse(const ble_gattc_evt_char_val_by_uuid_read_rsp_t *response)
rgrover1 389:5982ee136c47 250 {
rgrover1 389:5982ee136c47 251 if (state == DISCOVER_SERVICE_UUIDS) {
rgrover1 389:5982ee136c47 252 if ((response->count == 1) && (response->value_len == UUID::LENGTH_OF_LONG_UUID)) {
rgrover1 389:5982ee136c47 253 UUID::LongUUIDBytes_t uuid;
rgrover1 389:5982ee136c47 254 /* Switch longUUID bytes to MSB */
rgrover1 389:5982ee136c47 255 for (unsigned i = 0; i < UUID::LENGTH_OF_LONG_UUID; i++) {
rgrover1 389:5982ee136c47 256 uuid[i] = response->handle_value[0].p_value[UUID::LENGTH_OF_LONG_UUID - 1 - i];
rgrover1 389:5982ee136c47 257 }
rgrover1 389:5982ee136c47 258
rgrover1 389:5982ee136c47 259 unsigned serviceIndex = serviceUUIDDiscoveryQueue.dequeue();
rgrover1 389:5982ee136c47 260 services[serviceIndex].setupLongUUID(uuid);
rgrover1 389:5982ee136c47 261
rgrover1 389:5982ee136c47 262 serviceUUIDDiscoveryQueue.triggerFirst();
rgrover1 389:5982ee136c47 263 } else {
rgrover1 389:5982ee136c47 264 serviceUUIDDiscoveryQueue.dequeue();
rgrover1 389:5982ee136c47 265 }
rgrover1 389:5982ee136c47 266 } else if (state == DISCOVER_CHARACTERISTIC_UUIDS) {
rgrover1 389:5982ee136c47 267 if ((response->count == 1) && (response->value_len == UUID::LENGTH_OF_LONG_UUID + 1 /* props */ + 2 /* value handle */)) {
rgrover1 389:5982ee136c47 268 UUID::LongUUIDBytes_t uuid;
rgrover1 389:5982ee136c47 269 /* Switch longUUID bytes to MSB */
rgrover1 389:5982ee136c47 270 for (unsigned i = 0; i < UUID::LENGTH_OF_LONG_UUID; i++) {
rgrover1 389:5982ee136c47 271 uuid[i] = response->handle_value[0].p_value[3 + UUID::LENGTH_OF_LONG_UUID - 1 - i];
rgrover1 389:5982ee136c47 272 }
rgrover1 389:5982ee136c47 273
rgrover1 389:5982ee136c47 274 unsigned charIndex = charUUIDDiscoveryQueue.dequeue();
rgrover1 389:5982ee136c47 275 characteristics[charIndex].setupLongUUID(uuid);
rgrover1 389:5982ee136c47 276
rgrover1 389:5982ee136c47 277 charUUIDDiscoveryQueue.triggerFirst();
rgrover1 389:5982ee136c47 278 } else {
rgrover1 389:5982ee136c47 279 charUUIDDiscoveryQueue.dequeue();
rgrover1 389:5982ee136c47 280 }
rgrover1 389:5982ee136c47 281 }
rgrover1 389:5982ee136c47 282 }