nordic

Fork of nRF51822 by Nordic Semiconductor

Committer:
rgrover1
Date:
Fri Jun 19 15:55:31 2015 +0100
Revision:
320:cf62d065e70a
Parent:
319:1edcbcd1bf21
Child:
321:b1281aaf0076
Synchronized with git rev e3c989a0
Author: Rohit Grover
handle the read response event. :)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rgrover1 244:a966506d1e5b 1 /* mbed Microcontroller Library
rgrover1 244:a966506d1e5b 2 * Copyright (c) 2006-2013 ARM Limited
rgrover1 244:a966506d1e5b 3 *
rgrover1 244:a966506d1e5b 4 * Licensed under the Apache License, Version 2.0 (the "License");
rgrover1 244:a966506d1e5b 5 * you may not use this file except in compliance with the License.
rgrover1 244:a966506d1e5b 6 * You may obtain a copy of the License at
rgrover1 244:a966506d1e5b 7 *
rgrover1 244:a966506d1e5b 8 * http://www.apache.org/licenses/LICENSE-2.0
rgrover1 244:a966506d1e5b 9 *
rgrover1 244:a966506d1e5b 10 * Unless required by applicable law or agreed to in writing, software
rgrover1 244:a966506d1e5b 11 * distributed under the License is distributed on an "AS IS" BASIS,
rgrover1 244:a966506d1e5b 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
rgrover1 244:a966506d1e5b 13 * See the License for the specific language governing permissions and
rgrover1 244:a966506d1e5b 14 * limitations under the License.
rgrover1 244:a966506d1e5b 15 */
rgrover1 244:a966506d1e5b 16
rgrover1 288:faa0dc7ae513 17 #include <stdio.h>
rgrover1 288:faa0dc7ae513 18
rgrover1 240:693a1f145b5a 19 #include "blecommon.h"
rgrover1 240:693a1f145b5a 20 #include "UUID.h"
rgrover1 240:693a1f145b5a 21 #include "Gap.h"
rgrover1 240:693a1f145b5a 22 #include "nrf_error.h"
rgrover1 240:693a1f145b5a 23 #include "btle_discovery.h"
rgrover1 240:693a1f145b5a 24 #include "ble_err.h"
rgrover1 244:a966506d1e5b 25
rgrover1 279:26e3f0ef1e14 26 static NordicServiceDiscovery sdSingleton;
rgrover1 315:2e7378b25505 27 DiscoveredCharacteristic::ReadCallback_t DiscoveredCharacteristic::onDataReadCallback = NULL;
rgrover1 240:693a1f145b5a 28
rgrover1 240:693a1f145b5a 29 ble_error_t
rgrover1 276:fd709cd8b1b2 30 ServiceDiscovery::launch(Gap::Handle_t connectionHandle,
rgrover1 276:fd709cd8b1b2 31 ServiceCallback_t sc,
rgrover1 276:fd709cd8b1b2 32 CharacteristicCallback_t cc,
rgrover1 276:fd709cd8b1b2 33 const UUID &matchingServiceUUIDIn,
rgrover1 276:fd709cd8b1b2 34 const UUID &matchingCharacteristicUUIDIn)
rgrover1 240:693a1f145b5a 35 {
rgrover1 282:2280bb1a9a9e 36 if (isActive()) {
rgrover1 282:2280bb1a9a9e 37 return BLE_ERROR_INVALID_STATE;
rgrover1 282:2280bb1a9a9e 38 }
rgrover1 282:2280bb1a9a9e 39
rgrover1 279:26e3f0ef1e14 40 sdSingleton.serviceCallback = sc;
rgrover1 279:26e3f0ef1e14 41 sdSingleton.characteristicCallback = cc;
rgrover1 279:26e3f0ef1e14 42 sdSingleton.matchingServiceUUID = matchingServiceUUIDIn;
rgrover1 279:26e3f0ef1e14 43 sdSingleton.matchingCharacteristicUUID = matchingCharacteristicUUIDIn;
rgrover1 274:8ada9e8111a3 44
rgrover1 279:26e3f0ef1e14 45 sdSingleton.serviceDiscoveryStarted(connectionHandle);
rgrover1 240:693a1f145b5a 46
rgrover1 240:693a1f145b5a 47 uint32_t rc;
rgrover1 249:71ef03789dd3 48 if ((rc = sd_ble_gattc_primary_services_discover(connectionHandle, NordicServiceDiscovery::SRV_DISC_START_HANDLE, NULL)) != NRF_SUCCESS) {
rgrover1 279:26e3f0ef1e14 49 sdSingleton.terminate();
rgrover1 240:693a1f145b5a 50 switch (rc) {
rgrover1 240:693a1f145b5a 51 case NRF_ERROR_INVALID_PARAM:
rgrover1 240:693a1f145b5a 52 case BLE_ERROR_INVALID_CONN_HANDLE:
rgrover1 240:693a1f145b5a 53 return BLE_ERROR_INVALID_PARAM;
rgrover1 240:693a1f145b5a 54 case NRF_ERROR_BUSY:
rgrover1 240:693a1f145b5a 55 return BLE_STACK_BUSY;
rgrover1 240:693a1f145b5a 56 default:
rgrover1 240:693a1f145b5a 57 case NRF_ERROR_INVALID_STATE:
rgrover1 240:693a1f145b5a 58 return BLE_ERROR_INVALID_STATE;
rgrover1 240:693a1f145b5a 59 }
rgrover1 240:693a1f145b5a 60 }
rgrover1 240:693a1f145b5a 61
rgrover1 240:693a1f145b5a 62 return BLE_ERROR_NONE;
rgrover1 240:693a1f145b5a 63 }
rgrover1 240:693a1f145b5a 64
rgrover1 249:71ef03789dd3 65 void
rgrover1 249:71ef03789dd3 66 ServiceDiscovery::terminate(void)
rgrover1 249:71ef03789dd3 67 {
rgrover1 279:26e3f0ef1e14 68 sdSingleton.terminateServiceDiscovery();
rgrover1 249:71ef03789dd3 69 }
rgrover1 249:71ef03789dd3 70
rgrover1 280:6e5e79f1fb61 71 bool
rgrover1 280:6e5e79f1fb61 72 ServiceDiscovery::isActive(void)
rgrover1 280:6e5e79f1fb61 73 {
rgrover1 280:6e5e79f1fb61 74 return sdSingleton.isActive();
rgrover1 280:6e5e79f1fb61 75 }
rgrover1 280:6e5e79f1fb61 76
rgrover1 281:cbaa4cb83548 77 void ServiceDiscovery::onTermination(TerminationCallback_t callback) {
rgrover1 281:cbaa4cb83548 78 sdSingleton.setOnTermination(callback);
rgrover1 281:cbaa4cb83548 79 }
rgrover1 281:cbaa4cb83548 80
rgrover1 244:a966506d1e5b 81 ble_error_t
rgrover1 253:4b3f294415f5 82 NordicServiceDiscovery::launchCharacteristicDiscovery(Gap::Handle_t connectionHandle,
rgrover1 253:4b3f294415f5 83 Gap::Handle_t startHandle,
rgrover1 253:4b3f294415f5 84 Gap::Handle_t endHandle)
rgrover1 253:4b3f294415f5 85 {
rgrover1 279:26e3f0ef1e14 86 sdSingleton.characteristicDiscoveryStarted(connectionHandle);
rgrover1 240:693a1f145b5a 87
rgrover1 240:693a1f145b5a 88 ble_gattc_handle_range_t handleRange = {
rgrover1 240:693a1f145b5a 89 .start_handle = startHandle,
rgrover1 240:693a1f145b5a 90 .end_handle = endHandle
rgrover1 240:693a1f145b5a 91 };
rgrover1 240:693a1f145b5a 92 uint32_t rc;
rgrover1 240:693a1f145b5a 93 if ((rc = sd_ble_gattc_characteristics_discover(connectionHandle, &handleRange)) != NRF_SUCCESS) {
rgrover1 279:26e3f0ef1e14 94 sdSingleton.terminateCharacteristicDiscovery();
rgrover1 240:693a1f145b5a 95 switch (rc) {
rgrover1 240:693a1f145b5a 96 case BLE_ERROR_INVALID_CONN_HANDLE:
rgrover1 240:693a1f145b5a 97 case NRF_ERROR_INVALID_ADDR:
rgrover1 240:693a1f145b5a 98 return BLE_ERROR_INVALID_PARAM;
rgrover1 240:693a1f145b5a 99 case NRF_ERROR_BUSY:
rgrover1 240:693a1f145b5a 100 return BLE_STACK_BUSY;
rgrover1 240:693a1f145b5a 101 default:
rgrover1 240:693a1f145b5a 102 case NRF_ERROR_INVALID_STATE:
rgrover1 240:693a1f145b5a 103 return BLE_ERROR_INVALID_STATE;
rgrover1 240:693a1f145b5a 104 }
rgrover1 240:693a1f145b5a 105 }
rgrover1 240:693a1f145b5a 106
rgrover1 240:693a1f145b5a 107 return BLE_ERROR_NONE;
rgrover1 240:693a1f145b5a 108 }
rgrover1 240:693a1f145b5a 109
rgrover1 240:693a1f145b5a 110 void
rgrover1 306:293634834813 111 NordicServiceDiscovery::ServiceUUIDDiscoveryQueue::triggerFirst(void)
rgrover1 291:83c994bf62d0 112 {
rgrover1 294:cd86acfe41be 113 while (numIndices) { /* loop until a call to char_value_by_uuid_read() succeeds or we run out of pending indices. */
rgrover1 302:59e7404a4ea3 114 parentDiscoveryObject->state = DISCOVER_SERVICE_UUIDS;
rgrover1 291:83c994bf62d0 115
rgrover1 296:5566c7b83bb5 116 unsigned serviceIndex = getFirst();
rgrover1 291:83c994bf62d0 117 ble_uuid_t uuid = {
rgrover1 291:83c994bf62d0 118 .uuid = BLE_UUID_SERVICE_PRIMARY,
rgrover1 291:83c994bf62d0 119 .type = BLE_UUID_TYPE_BLE,
rgrover1 291:83c994bf62d0 120 };
rgrover1 291:83c994bf62d0 121 ble_gattc_handle_range_t handleRange = {
rgrover1 302:59e7404a4ea3 122 .start_handle = parentDiscoveryObject->services[serviceIndex].getStartHandle(),
rgrover1 302:59e7404a4ea3 123 .end_handle = parentDiscoveryObject->services[serviceIndex].getEndHandle(),
rgrover1 291:83c994bf62d0 124 };
rgrover1 302:59e7404a4ea3 125 if (sd_ble_gattc_char_value_by_uuid_read(parentDiscoveryObject->connHandle, &uuid, &handleRange) == NRF_SUCCESS) {
rgrover1 294:cd86acfe41be 126 return;
rgrover1 291:83c994bf62d0 127 }
rgrover1 291:83c994bf62d0 128
rgrover1 294:cd86acfe41be 129 /* Skip this service if we fail to launch a read for its service-declaration
rgrover1 294:cd86acfe41be 130 * attribute. Its UUID will remain INVALID, and it may not match any filters. */
rgrover1 300:19064275c0e0 131 dequeue();
rgrover1 291:83c994bf62d0 132 }
rgrover1 291:83c994bf62d0 133
rgrover1 294:cd86acfe41be 134 /* Switch back to service discovery upon exhausting the service-indices pending UUID discovery. */
rgrover1 302:59e7404a4ea3 135 if (parentDiscoveryObject->state == DISCOVER_SERVICE_UUIDS) {
rgrover1 302:59e7404a4ea3 136 parentDiscoveryObject->state = SERVICE_DISCOVERY_ACTIVE;
rgrover1 291:83c994bf62d0 137 }
rgrover1 291:83c994bf62d0 138 }
rgrover1 291:83c994bf62d0 139
rgrover1 291:83c994bf62d0 140 void
rgrover1 307:76e2e7349319 141 NordicServiceDiscovery::CharUUIDDiscoveryQueue::triggerFirst(void)
rgrover1 307:76e2e7349319 142 {
rgrover1 307:76e2e7349319 143 while (numIndices) { /* loop until a call to char_value_by_uuid_read() succeeds or we run out of pending indices. */
rgrover1 307:76e2e7349319 144 parentDiscoveryObject->state = DISCOVER_CHARACTERISTIC_UUIDS;
rgrover1 307:76e2e7349319 145
rgrover1 307:76e2e7349319 146 unsigned charIndex = getFirst();
rgrover1 307:76e2e7349319 147 ble_uuid_t uuid = {
rgrover1 307:76e2e7349319 148 .uuid = BLE_UUID_CHARACTERISTIC,
rgrover1 307:76e2e7349319 149 .type = BLE_UUID_TYPE_BLE,
rgrover1 307:76e2e7349319 150 };
rgrover1 307:76e2e7349319 151 ble_gattc_handle_range_t handleRange = {
rgrover1 307:76e2e7349319 152 .start_handle = parentDiscoveryObject->characteristics[charIndex].getDeclHandle(),
rgrover1 307:76e2e7349319 153 .end_handle = parentDiscoveryObject->characteristics[charIndex].getDeclHandle() + 1,
rgrover1 307:76e2e7349319 154 };
rgrover1 307:76e2e7349319 155 if (sd_ble_gattc_char_value_by_uuid_read(parentDiscoveryObject->connHandle, &uuid, &handleRange) == NRF_SUCCESS) {
rgrover1 307:76e2e7349319 156 return;
rgrover1 307:76e2e7349319 157 }
rgrover1 307:76e2e7349319 158
rgrover1 307:76e2e7349319 159 /* Skip this service if we fail to launch a read for its service-declaration
rgrover1 307:76e2e7349319 160 * attribute. Its UUID will remain INVALID, and it may not match any filters. */
rgrover1 307:76e2e7349319 161 dequeue();
rgrover1 307:76e2e7349319 162 }
rgrover1 307:76e2e7349319 163
rgrover1 307:76e2e7349319 164 /* Switch back to service discovery upon exhausting the service-indices pending UUID discovery. */
rgrover1 307:76e2e7349319 165 if (parentDiscoveryObject->state == DISCOVER_CHARACTERISTIC_UUIDS) {
rgrover1 307:76e2e7349319 166 parentDiscoveryObject->state = CHARACTERISTIC_DISCOVERY_ACTIVE;
rgrover1 307:76e2e7349319 167 }
rgrover1 307:76e2e7349319 168 }
rgrover1 307:76e2e7349319 169
rgrover1 307:76e2e7349319 170 void
rgrover1 291:83c994bf62d0 171 NordicServiceDiscovery::processDiscoverUUIDResponse(const ble_gattc_evt_char_val_by_uuid_read_rsp_t *response)
rgrover1 291:83c994bf62d0 172 {
rgrover1 300:19064275c0e0 173 if (state == DISCOVER_SERVICE_UUIDS) {
rgrover1 300:19064275c0e0 174 if ((response->count == 1) && (response->value_len == UUID::LENGTH_OF_LONG_UUID)) {
rgrover1 300:19064275c0e0 175 UUID::LongUUIDBytes_t uuid;
rgrover1 300:19064275c0e0 176 /* Switch longUUID bytes to MSB */
rgrover1 300:19064275c0e0 177 for (unsigned i = 0; i < UUID::LENGTH_OF_LONG_UUID; i++) {
rgrover1 300:19064275c0e0 178 uuid[i] = response->handle_value[0].p_value[UUID::LENGTH_OF_LONG_UUID - 1 - i];
rgrover1 300:19064275c0e0 179 }
rgrover1 291:83c994bf62d0 180
rgrover1 306:293634834813 181 unsigned serviceIndex = serviceUUIDDiscoveryQueue.dequeue();
rgrover1 300:19064275c0e0 182 services[serviceIndex].setupLongUUID(uuid);
rgrover1 300:19064275c0e0 183
rgrover1 306:293634834813 184 serviceUUIDDiscoveryQueue.triggerFirst();
rgrover1 307:76e2e7349319 185 } else {
rgrover1 307:76e2e7349319 186 serviceUUIDDiscoveryQueue.dequeue();
rgrover1 307:76e2e7349319 187 }
rgrover1 307:76e2e7349319 188 } else if (state == DISCOVER_CHARACTERISTIC_UUIDS) {
rgrover1 307:76e2e7349319 189 if ((response->count == 1) && (response->value_len == UUID::LENGTH_OF_LONG_UUID + 1 /* props */ + 2 /* value handle */)) {
rgrover1 307:76e2e7349319 190 UUID::LongUUIDBytes_t uuid;
rgrover1 307:76e2e7349319 191 /* Switch longUUID bytes to MSB */
rgrover1 307:76e2e7349319 192 for (unsigned i = 0; i < UUID::LENGTH_OF_LONG_UUID; i++) {
rgrover1 307:76e2e7349319 193 uuid[i] = response->handle_value[0].p_value[3 + UUID::LENGTH_OF_LONG_UUID - 1 - i];
rgrover1 307:76e2e7349319 194 }
rgrover1 307:76e2e7349319 195
rgrover1 307:76e2e7349319 196 unsigned charIndex = charUUIDDiscoveryQueue.dequeue();
rgrover1 307:76e2e7349319 197 characteristics[charIndex].setupLongUUID(uuid);
rgrover1 307:76e2e7349319 198
rgrover1 307:76e2e7349319 199 charUUIDDiscoveryQueue.triggerFirst();
rgrover1 307:76e2e7349319 200 } else {
rgrover1 307:76e2e7349319 201 charUUIDDiscoveryQueue.dequeue();
rgrover1 297:8d25fe68d731 202 }
rgrover1 297:8d25fe68d731 203 }
rgrover1 291:83c994bf62d0 204 }
rgrover1 291:83c994bf62d0 205
rgrover1 291:83c994bf62d0 206 void
rgrover1 241:75b69581d1dd 207 NordicServiceDiscovery::setupDiscoveredServices(const ble_gattc_evt_prim_srvc_disc_rsp_t *response)
rgrover1 240:693a1f145b5a 208 {
rgrover1 243:73fc02cc20b1 209 serviceIndex = 0;
rgrover1 243:73fc02cc20b1 210 numServices = response->count;
rgrover1 240:693a1f145b5a 211
rgrover1 240:693a1f145b5a 212 /* Account for the limitation on the number of discovered services we can handle at a time. */
rgrover1 243:73fc02cc20b1 213 if (numServices > BLE_DB_DISCOVERY_MAX_SRV) {
rgrover1 243:73fc02cc20b1 214 numServices = BLE_DB_DISCOVERY_MAX_SRV;
rgrover1 240:693a1f145b5a 215 }
rgrover1 240:693a1f145b5a 216
rgrover1 306:293634834813 217 serviceUUIDDiscoveryQueue.reset();
rgrover1 243:73fc02cc20b1 218 for (unsigned serviceIndex = 0; serviceIndex < numServices; serviceIndex++) {
rgrover1 284:c4c2aeab86c3 219 if (response->services[serviceIndex].uuid.type == BLE_UUID_TYPE_UNKNOWN) {
rgrover1 306:293634834813 220 serviceUUIDDiscoveryQueue.enqueue(serviceIndex);
rgrover1 295:a2219bad9d4a 221 services[serviceIndex].setup(response->services[serviceIndex].handle_range.start_handle,
rgrover1 295:a2219bad9d4a 222 response->services[serviceIndex].handle_range.end_handle);
rgrover1 295:a2219bad9d4a 223 } else {
rgrover1 295:a2219bad9d4a 224 services[serviceIndex].setup(response->services[serviceIndex].uuid.uuid,
rgrover1 295:a2219bad9d4a 225 response->services[serviceIndex].handle_range.start_handle,
rgrover1 295:a2219bad9d4a 226 response->services[serviceIndex].handle_range.end_handle);
rgrover1 284:c4c2aeab86c3 227 }
rgrover1 240:693a1f145b5a 228 }
rgrover1 291:83c994bf62d0 229
rgrover1 291:83c994bf62d0 230 /* Trigger discovery of service UUID if necessary. */
rgrover1 306:293634834813 231 if (serviceUUIDDiscoveryQueue.getCount()) {
rgrover1 306:293634834813 232 serviceUUIDDiscoveryQueue.triggerFirst();
rgrover1 291:83c994bf62d0 233 }
rgrover1 240:693a1f145b5a 234 }
rgrover1 240:693a1f145b5a 235
rgrover1 240:693a1f145b5a 236 void
rgrover1 241:75b69581d1dd 237 NordicServiceDiscovery::setupDiscoveredCharacteristics(const ble_gattc_evt_char_disc_rsp_t *response)
rgrover1 240:693a1f145b5a 238 {
rgrover1 243:73fc02cc20b1 239 characteristicIndex = 0;
rgrover1 243:73fc02cc20b1 240 numCharacteristics = response->count;
rgrover1 240:693a1f145b5a 241
rgrover1 240:693a1f145b5a 242 /* Account for the limitation on the number of discovered characteristics we can handle at a time. */
rgrover1 243:73fc02cc20b1 243 if (numCharacteristics > BLE_DB_DISCOVERY_MAX_CHAR_PER_SRV) {
rgrover1 243:73fc02cc20b1 244 numCharacteristics = BLE_DB_DISCOVERY_MAX_CHAR_PER_SRV;
rgrover1 240:693a1f145b5a 245 }
rgrover1 240:693a1f145b5a 246
rgrover1 307:76e2e7349319 247 charUUIDDiscoveryQueue.reset();
rgrover1 243:73fc02cc20b1 248 for (unsigned charIndex = 0; charIndex < numCharacteristics; charIndex++) {
rgrover1 307:76e2e7349319 249 if (response->chars[charIndex].uuid.type == BLE_UUID_TYPE_UNKNOWN) {
rgrover1 307:76e2e7349319 250 charUUIDDiscoveryQueue.enqueue(charIndex);
rgrover1 312:5d5abc30e3a8 251 characteristics[charIndex].setup(connHandle,
rgrover1 312:5d5abc30e3a8 252 *(const uint8_t *)(&response->chars[charIndex].char_props),
rgrover1 307:76e2e7349319 253 response->chars[charIndex].handle_decl,
rgrover1 307:76e2e7349319 254 response->chars[charIndex].handle_value);
rgrover1 307:76e2e7349319 255 } else {
rgrover1 312:5d5abc30e3a8 256 characteristics[charIndex].setup(connHandle,
rgrover1 312:5d5abc30e3a8 257 response->chars[charIndex].uuid.uuid,
rgrover1 307:76e2e7349319 258 *(const uint8_t *)(&response->chars[charIndex].char_props),
rgrover1 307:76e2e7349319 259 response->chars[charIndex].handle_decl,
rgrover1 307:76e2e7349319 260 response->chars[charIndex].handle_value);
rgrover1 307:76e2e7349319 261 }
rgrover1 307:76e2e7349319 262 }
rgrover1 307:76e2e7349319 263
rgrover1 307:76e2e7349319 264 /* Trigger discovery of char UUID if necessary. */
rgrover1 307:76e2e7349319 265 if (charUUIDDiscoveryQueue.getCount()) {
rgrover1 307:76e2e7349319 266 charUUIDDiscoveryQueue.triggerFirst();
rgrover1 240:693a1f145b5a 267 }
rgrover1 244:a966506d1e5b 268 }
rgrover1 244:a966506d1e5b 269
rgrover1 252:d4e0cf5e8751 270 void
rgrover1 252:d4e0cf5e8751 271 NordicServiceDiscovery::progressCharacteristicDiscovery(void)
rgrover1 252:d4e0cf5e8751 272 {
rgrover1 277:ae6e5129b395 273 /* Iterate through the previously discovered characteristics cached in characteristics[]. */
rgrover1 287:042b01e59cf8 274 while ((state == CHARACTERISTIC_DISCOVERY_ACTIVE) && (characteristicIndex < numCharacteristics)) {
rgrover1 286:55ac765bcc8d 275 if ((matchingCharacteristicUUID == UUID::ShortUUIDBytes_t(BLE_UUID_UNKNOWN)) ||
rgrover1 283:6633922380bf 276 (matchingCharacteristicUUID == characteristics[characteristicIndex].getShortUUID())) {
rgrover1 283:6633922380bf 277 if (characteristicCallback) {
rgrover1 319:1edcbcd1bf21 278 characteristicCallback(&characteristics[characteristicIndex]);
rgrover1 283:6633922380bf 279 }
rgrover1 256:d228656b0cd1 280 }
rgrover1 252:d4e0cf5e8751 281
rgrover1 252:d4e0cf5e8751 282 characteristicIndex++;
rgrover1 252:d4e0cf5e8751 283 }
rgrover1 252:d4e0cf5e8751 284
rgrover1 277:ae6e5129b395 285 /* Relaunch discovery of new characteristics beyond the last entry cached in characteristics[]. */
rgrover1 287:042b01e59cf8 286 if (state == CHARACTERISTIC_DISCOVERY_ACTIVE) {
rgrover1 277:ae6e5129b395 287 /* Determine the ending handle of the last cached characteristic. */
rgrover1 265:5533ac0adf4b 288 Gap::Handle_t startHandle = characteristics[characteristicIndex - 1].getValueHandle() + 1;
rgrover1 265:5533ac0adf4b 289 Gap::Handle_t endHandle = services[serviceIndex].getEndHandle();
rgrover1 275:f82e0ecba699 290 resetDiscoveredCharacteristics(); /* Note: resetDiscoveredCharacteristics() must come after fetching start and end Handles. */
rgrover1 252:d4e0cf5e8751 291
rgrover1 252:d4e0cf5e8751 292 if (startHandle < endHandle) {
rgrover1 252:d4e0cf5e8751 293 ble_gattc_handle_range_t handleRange = {
rgrover1 252:d4e0cf5e8751 294 .start_handle = startHandle,
rgrover1 252:d4e0cf5e8751 295 .end_handle = endHandle
rgrover1 252:d4e0cf5e8751 296 };
rgrover1 259:62d6bf03e6a4 297 if (sd_ble_gattc_characteristics_discover(connHandle, &handleRange) != NRF_SUCCESS) {
rgrover1 259:62d6bf03e6a4 298 terminateCharacteristicDiscovery();
rgrover1 259:62d6bf03e6a4 299 }
rgrover1 252:d4e0cf5e8751 300 } else {
rgrover1 278:91705df8495e 301 terminateCharacteristicDiscovery();
rgrover1 252:d4e0cf5e8751 302 }
rgrover1 252:d4e0cf5e8751 303 }
rgrover1 252:d4e0cf5e8751 304 }
rgrover1 252:d4e0cf5e8751 305
rgrover1 252:d4e0cf5e8751 306 void
rgrover1 252:d4e0cf5e8751 307 NordicServiceDiscovery::progressServiceDiscovery(void)
rgrover1 252:d4e0cf5e8751 308 {
rgrover1 277:ae6e5129b395 309 /* Iterate through the previously discovered services cached in services[]. */
rgrover1 287:042b01e59cf8 310 while ((state == SERVICE_DISCOVERY_ACTIVE) && (serviceIndex < numServices)) {
rgrover1 286:55ac765bcc8d 311 if ((matchingServiceUUID == UUID::ShortUUIDBytes_t(BLE_UUID_UNKNOWN)) ||
rgrover1 299:c10ee33f68ab 312 (matchingServiceUUID == services[serviceIndex].getUUID().getShortUUID())) {
rgrover1 278:91705df8495e 313 if (serviceCallback) {
rgrover1 278:91705df8495e 314 serviceCallback(services[serviceIndex]);
rgrover1 278:91705df8495e 315 }
rgrover1 252:d4e0cf5e8751 316
rgrover1 287:042b01e59cf8 317 if ((state == SERVICE_DISCOVERY_ACTIVE) && characteristicCallback) {
rgrover1 278:91705df8495e 318 launchCharacteristicDiscovery(connHandle, services[serviceIndex].getStartHandle(), services[serviceIndex].getEndHandle());
rgrover1 278:91705df8495e 319 } else {
rgrover1 278:91705df8495e 320 serviceIndex++;
rgrover1 278:91705df8495e 321 }
rgrover1 252:d4e0cf5e8751 322 } else {
rgrover1 278:91705df8495e 323 serviceIndex++;
rgrover1 252:d4e0cf5e8751 324 }
rgrover1 252:d4e0cf5e8751 325 }
rgrover1 277:ae6e5129b395 326
rgrover1 277:ae6e5129b395 327 /* Relaunch discovery of new services beyond the last entry cached in services[]. */
rgrover1 287:042b01e59cf8 328 if ((state == SERVICE_DISCOVERY_ACTIVE) && (numServices > 0) && (serviceIndex > 0)) {
rgrover1 277:ae6e5129b395 329 /* Determine the ending handle of the last cached service. */
rgrover1 265:5533ac0adf4b 330 Gap::Handle_t endHandle = services[serviceIndex - 1].getEndHandle();
rgrover1 275:f82e0ecba699 331 resetDiscoveredServices(); /* Note: resetDiscoveredServices() must come after fetching endHandle. */
rgrover1 252:d4e0cf5e8751 332
rgrover1 285:29fb6af6671d 333 if (endHandle == SRV_DISC_END_HANDLE) {
rgrover1 259:62d6bf03e6a4 334 terminateServiceDiscovery();
rgrover1 285:29fb6af6671d 335 } else {
rgrover1 285:29fb6af6671d 336 if (sd_ble_gattc_primary_services_discover(connHandle, endHandle, NULL) != NRF_SUCCESS) {
rgrover1 285:29fb6af6671d 337 terminateServiceDiscovery();
rgrover1 285:29fb6af6671d 338 }
rgrover1 259:62d6bf03e6a4 339 }
rgrover1 252:d4e0cf5e8751 340 }
rgrover1 252:d4e0cf5e8751 341 }
rgrover1 252:d4e0cf5e8751 342
rgrover1 244:a966506d1e5b 343 void bleGattcEventHandler(const ble_evt_t *p_ble_evt)
rgrover1 244:a966506d1e5b 344 {
rgrover1 244:a966506d1e5b 345 switch (p_ble_evt->header.evt_id) {
rgrover1 244:a966506d1e5b 346 case BLE_GATTC_EVT_PRIM_SRVC_DISC_RSP:
rgrover1 244:a966506d1e5b 347 switch (p_ble_evt->evt.gattc_evt.gatt_status) {
rgrover1 244:a966506d1e5b 348 case BLE_GATT_STATUS_SUCCESS:
rgrover1 279:26e3f0ef1e14 349 sdSingleton.setupDiscoveredServices(&p_ble_evt->evt.gattc_evt.params.prim_srvc_disc_rsp);
rgrover1 244:a966506d1e5b 350 break;
rgrover1 244:a966506d1e5b 351
rgrover1 244:a966506d1e5b 352 case BLE_GATT_STATUS_ATTERR_ATTRIBUTE_NOT_FOUND:
rgrover1 244:a966506d1e5b 353 default:
rgrover1 279:26e3f0ef1e14 354 sdSingleton.terminate();
rgrover1 244:a966506d1e5b 355 break;
rgrover1 244:a966506d1e5b 356 }
rgrover1 244:a966506d1e5b 357 break;
rgrover1 244:a966506d1e5b 358
rgrover1 244:a966506d1e5b 359 case BLE_GATTC_EVT_CHAR_DISC_RSP:
rgrover1 244:a966506d1e5b 360 switch (p_ble_evt->evt.gattc_evt.gatt_status) {
rgrover1 244:a966506d1e5b 361 case BLE_GATT_STATUS_SUCCESS:
rgrover1 279:26e3f0ef1e14 362 sdSingleton.setupDiscoveredCharacteristics(&p_ble_evt->evt.gattc_evt.params.char_disc_rsp);
rgrover1 244:a966506d1e5b 363 break;
rgrover1 244:a966506d1e5b 364
rgrover1 244:a966506d1e5b 365 case BLE_GATT_STATUS_ATTERR_ATTRIBUTE_NOT_FOUND:
rgrover1 244:a966506d1e5b 366 default:
rgrover1 279:26e3f0ef1e14 367 sdSingleton.terminateCharacteristicDiscovery();
rgrover1 244:a966506d1e5b 368 break;
rgrover1 244:a966506d1e5b 369 }
rgrover1 244:a966506d1e5b 370 break;
rgrover1 314:d579d093f23f 371
rgrover1 314:d579d093f23f 372 case BLE_GATTC_EVT_CHAR_VAL_BY_UUID_READ_RSP:
rgrover1 291:83c994bf62d0 373 if (sdSingleton.isActive()) {
rgrover1 291:83c994bf62d0 374 sdSingleton.processDiscoverUUIDResponse(&p_ble_evt->evt.gattc_evt.params.char_val_by_uuid_read_rsp);
rgrover1 291:83c994bf62d0 375 }
rgrover1 291:83c994bf62d0 376 break;
rgrover1 320:cf62d065e70a 377
rgrover1 320:cf62d065e70a 378 case BLE_GATTC_EVT_READ_RSP:
rgrover1 320:cf62d065e70a 379 printf("BLE_GATTC_EVT_READ_RSP\r\n");
rgrover1 320:cf62d065e70a 380 if (DiscoveredCharacteristic::onDataReadCallback != NULL) {
rgrover1 320:cf62d065e70a 381 DiscoveredCharacteristic::ReadResponse_t response = {
rgrover1 320:cf62d065e70a 382 .handle = p_ble_evt->evt.gattc_evt.params.read_rsp.handle,
rgrover1 320:cf62d065e70a 383 .offset = p_ble_evt->evt.gattc_evt.params.read_rsp.offset,
rgrover1 320:cf62d065e70a 384 .len = p_ble_evt->evt.gattc_evt.params.read_rsp.len,
rgrover1 320:cf62d065e70a 385 .data = p_ble_evt->evt.gattc_evt.params.read_rsp.data,
rgrover1 320:cf62d065e70a 386 };
rgrover1 320:cf62d065e70a 387 DiscoveredCharacteristic::onDataReadCallback(&response);
rgrover1 320:cf62d065e70a 388 }
rgrover1 320:cf62d065e70a 389 break;
rgrover1 244:a966506d1e5b 390 }
rgrover1 244:a966506d1e5b 391
rgrover1 279:26e3f0ef1e14 392 sdSingleton.progressCharacteristicDiscovery();
rgrover1 279:26e3f0ef1e14 393 sdSingleton.progressServiceDiscovery();
rgrover1 240:693a1f145b5a 394 }