test code 123

Dependencies:   mbed

Fork of LinkNode-Test by Qi Yao

Committer:
youkee
Date:
Thu Sep 01 05:14:03 2016 +0000
Revision:
0:1ad0e04b1bc5
change internal time from 1s to 200ms

Who changed what in which revision?

UserRevisionLine numberNew contents of line
youkee 0:1ad0e04b1bc5 1 /* mbed Microcontroller Library
youkee 0:1ad0e04b1bc5 2 * Copyright (c) 2006-2013 ARM Limited
youkee 0:1ad0e04b1bc5 3 *
youkee 0:1ad0e04b1bc5 4 * Licensed under the Apache License, Version 2.0 (the "License");
youkee 0:1ad0e04b1bc5 5 * you may not use this file except in compliance with the License.
youkee 0:1ad0e04b1bc5 6 * You may obtain a copy of the License at
youkee 0:1ad0e04b1bc5 7 *
youkee 0:1ad0e04b1bc5 8 * http://www.apache.org/licenses/LICENSE-2.0
youkee 0:1ad0e04b1bc5 9 *
youkee 0:1ad0e04b1bc5 10 * Unless required by applicable law or agreed to in writing, software
youkee 0:1ad0e04b1bc5 11 * distributed under the License is distributed on an "AS IS" BASIS,
youkee 0:1ad0e04b1bc5 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
youkee 0:1ad0e04b1bc5 13 * See the License for the specific language governing permissions and
youkee 0:1ad0e04b1bc5 14 * limitations under the License.
youkee 0:1ad0e04b1bc5 15 */
youkee 0:1ad0e04b1bc5 16
youkee 0:1ad0e04b1bc5 17 #ifndef __NRF_SERVICE_DISCOVERY_H__
youkee 0:1ad0e04b1bc5 18 #define __NRF_SERVICE_DISCOVERY_H__
youkee 0:1ad0e04b1bc5 19
youkee 0:1ad0e04b1bc5 20 #include "ble/ServiceDiscovery.h"
youkee 0:1ad0e04b1bc5 21 #include "ble/DiscoveredService.h"
youkee 0:1ad0e04b1bc5 22 #include "nRF5xDiscoveredCharacteristic.h"
youkee 0:1ad0e04b1bc5 23
youkee 0:1ad0e04b1bc5 24 #include "ble.h"
youkee 0:1ad0e04b1bc5 25 #include "ble_gattc.h"
youkee 0:1ad0e04b1bc5 26
youkee 0:1ad0e04b1bc5 27 class nRF5xGattClient; /* forward declaration */
youkee 0:1ad0e04b1bc5 28
youkee 0:1ad0e04b1bc5 29 class nRF5xServiceDiscovery : public ServiceDiscovery
youkee 0:1ad0e04b1bc5 30 {
youkee 0:1ad0e04b1bc5 31 public:
youkee 0:1ad0e04b1bc5 32 static const uint16_t SRV_DISC_START_HANDLE = 0x0001; /**< The start handle value used during service discovery. */
youkee 0:1ad0e04b1bc5 33 static const uint16_t SRV_DISC_END_HANDLE = 0xFFFF; /**< The end handle value used during service discovery. */
youkee 0:1ad0e04b1bc5 34
youkee 0:1ad0e04b1bc5 35 public:
youkee 0:1ad0e04b1bc5 36 static const unsigned BLE_DB_DISCOVERY_MAX_SRV = 4; /**< Maximum number of services we can retain information for after a single discovery. */
youkee 0:1ad0e04b1bc5 37 static const unsigned BLE_DB_DISCOVERY_MAX_CHAR_PER_SRV = 4; /**< Maximum number of characteristics per service we can retain information for. */
youkee 0:1ad0e04b1bc5 38
youkee 0:1ad0e04b1bc5 39 public:
youkee 0:1ad0e04b1bc5 40 nRF5xServiceDiscovery(nRF5xGattClient *gattcIn) :
youkee 0:1ad0e04b1bc5 41 gattc(gattcIn),
youkee 0:1ad0e04b1bc5 42 serviceIndex(0),
youkee 0:1ad0e04b1bc5 43 numServices(0),
youkee 0:1ad0e04b1bc5 44 characteristicIndex(0),
youkee 0:1ad0e04b1bc5 45 numCharacteristics(0),
youkee 0:1ad0e04b1bc5 46 state(INACTIVE),
youkee 0:1ad0e04b1bc5 47 services(),
youkee 0:1ad0e04b1bc5 48 characteristics(),
youkee 0:1ad0e04b1bc5 49 serviceUUIDDiscoveryQueue(this),
youkee 0:1ad0e04b1bc5 50 charUUIDDiscoveryQueue(this),
youkee 0:1ad0e04b1bc5 51 onTerminationCallback(NULL) {
youkee 0:1ad0e04b1bc5 52 /* empty */
youkee 0:1ad0e04b1bc5 53 }
youkee 0:1ad0e04b1bc5 54
youkee 0:1ad0e04b1bc5 55 virtual ble_error_t launch(Gap::Handle_t connectionHandle,
youkee 0:1ad0e04b1bc5 56 ServiceDiscovery::ServiceCallback_t sc,
youkee 0:1ad0e04b1bc5 57 ServiceDiscovery::CharacteristicCallback_t cc,
youkee 0:1ad0e04b1bc5 58 const UUID &matchingServiceUUIDIn,
youkee 0:1ad0e04b1bc5 59 const UUID &matchingCharacteristicUUIDIn)
youkee 0:1ad0e04b1bc5 60 {
youkee 0:1ad0e04b1bc5 61 if (isActive()) {
youkee 0:1ad0e04b1bc5 62 return BLE_ERROR_INVALID_STATE;
youkee 0:1ad0e04b1bc5 63 }
youkee 0:1ad0e04b1bc5 64
youkee 0:1ad0e04b1bc5 65 serviceCallback = sc;
youkee 0:1ad0e04b1bc5 66 characteristicCallback = cc;
youkee 0:1ad0e04b1bc5 67 matchingServiceUUID = matchingServiceUUIDIn;
youkee 0:1ad0e04b1bc5 68 matchingCharacteristicUUID = matchingCharacteristicUUIDIn;
youkee 0:1ad0e04b1bc5 69
youkee 0:1ad0e04b1bc5 70 serviceDiscoveryStarted(connectionHandle);
youkee 0:1ad0e04b1bc5 71
youkee 0:1ad0e04b1bc5 72 uint32_t rc;
youkee 0:1ad0e04b1bc5 73 if ((rc = sd_ble_gattc_primary_services_discover(connectionHandle, SRV_DISC_START_HANDLE, NULL)) != NRF_SUCCESS) {
youkee 0:1ad0e04b1bc5 74 terminate();
youkee 0:1ad0e04b1bc5 75 switch (rc) {
youkee 0:1ad0e04b1bc5 76 case NRF_ERROR_INVALID_PARAM:
youkee 0:1ad0e04b1bc5 77 case BLE_ERROR_INVALID_CONN_HANDLE:
youkee 0:1ad0e04b1bc5 78 return BLE_ERROR_INVALID_PARAM;
youkee 0:1ad0e04b1bc5 79 case NRF_ERROR_BUSY:
youkee 0:1ad0e04b1bc5 80 return BLE_STACK_BUSY;
youkee 0:1ad0e04b1bc5 81 default:
youkee 0:1ad0e04b1bc5 82 case NRF_ERROR_INVALID_STATE:
youkee 0:1ad0e04b1bc5 83 return BLE_ERROR_INVALID_STATE;
youkee 0:1ad0e04b1bc5 84 }
youkee 0:1ad0e04b1bc5 85 }
youkee 0:1ad0e04b1bc5 86
youkee 0:1ad0e04b1bc5 87 return BLE_ERROR_NONE;
youkee 0:1ad0e04b1bc5 88 }
youkee 0:1ad0e04b1bc5 89
youkee 0:1ad0e04b1bc5 90 virtual bool isActive(void) const {
youkee 0:1ad0e04b1bc5 91 return state != INACTIVE;
youkee 0:1ad0e04b1bc5 92 }
youkee 0:1ad0e04b1bc5 93
youkee 0:1ad0e04b1bc5 94 virtual void terminate(void) {
youkee 0:1ad0e04b1bc5 95 terminateServiceDiscovery();
youkee 0:1ad0e04b1bc5 96 }
youkee 0:1ad0e04b1bc5 97
youkee 0:1ad0e04b1bc5 98 virtual void onTermination(ServiceDiscovery::TerminationCallback_t callback) {
youkee 0:1ad0e04b1bc5 99 onTerminationCallback = callback;
youkee 0:1ad0e04b1bc5 100 }
youkee 0:1ad0e04b1bc5 101
youkee 0:1ad0e04b1bc5 102 private:
youkee 0:1ad0e04b1bc5 103 ble_error_t launchCharacteristicDiscovery(Gap::Handle_t connectionHandle, Gap::Handle_t startHandle, Gap::Handle_t endHandle);
youkee 0:1ad0e04b1bc5 104
youkee 0:1ad0e04b1bc5 105 private:
youkee 0:1ad0e04b1bc5 106 void setupDiscoveredServices(const ble_gattc_evt_prim_srvc_disc_rsp_t *response);
youkee 0:1ad0e04b1bc5 107 void setupDiscoveredCharacteristics(const ble_gattc_evt_char_disc_rsp_t *response);
youkee 0:1ad0e04b1bc5 108
youkee 0:1ad0e04b1bc5 109 void triggerServiceUUIDDiscovery(void);
youkee 0:1ad0e04b1bc5 110 void processDiscoverUUIDResponse(const ble_gattc_evt_char_val_by_uuid_read_rsp_t *response);
youkee 0:1ad0e04b1bc5 111 void removeFirstServiceNeedingUUIDDiscovery(void);
youkee 0:1ad0e04b1bc5 112
youkee 0:1ad0e04b1bc5 113 void terminateServiceDiscovery(void) {
youkee 0:1ad0e04b1bc5 114 bool wasActive = isActive();
youkee 0:1ad0e04b1bc5 115 state = INACTIVE;
youkee 0:1ad0e04b1bc5 116
youkee 0:1ad0e04b1bc5 117 if (wasActive && onTerminationCallback) {
youkee 0:1ad0e04b1bc5 118 onTerminationCallback(connHandle);
youkee 0:1ad0e04b1bc5 119 }
youkee 0:1ad0e04b1bc5 120 }
youkee 0:1ad0e04b1bc5 121
youkee 0:1ad0e04b1bc5 122 void terminateCharacteristicDiscovery(void) {
youkee 0:1ad0e04b1bc5 123 if (state == CHARACTERISTIC_DISCOVERY_ACTIVE) {
youkee 0:1ad0e04b1bc5 124 state = SERVICE_DISCOVERY_ACTIVE;
youkee 0:1ad0e04b1bc5 125 }
youkee 0:1ad0e04b1bc5 126 serviceIndex++; /* Progress service index to keep discovery alive. */
youkee 0:1ad0e04b1bc5 127 }
youkee 0:1ad0e04b1bc5 128
youkee 0:1ad0e04b1bc5 129 private:
youkee 0:1ad0e04b1bc5 130 void resetDiscoveredServices(void) {
youkee 0:1ad0e04b1bc5 131 numServices = 0;
youkee 0:1ad0e04b1bc5 132 serviceIndex = 0;
youkee 0:1ad0e04b1bc5 133 }
youkee 0:1ad0e04b1bc5 134
youkee 0:1ad0e04b1bc5 135 void resetDiscoveredCharacteristics(void) {
youkee 0:1ad0e04b1bc5 136 numCharacteristics = 0;
youkee 0:1ad0e04b1bc5 137 characteristicIndex = 0;
youkee 0:1ad0e04b1bc5 138 }
youkee 0:1ad0e04b1bc5 139
youkee 0:1ad0e04b1bc5 140 private:
youkee 0:1ad0e04b1bc5 141 void serviceDiscoveryStarted(Gap::Handle_t connectionHandle) {
youkee 0:1ad0e04b1bc5 142 connHandle = connectionHandle;
youkee 0:1ad0e04b1bc5 143 resetDiscoveredServices();
youkee 0:1ad0e04b1bc5 144 state = SERVICE_DISCOVERY_ACTIVE;
youkee 0:1ad0e04b1bc5 145 }
youkee 0:1ad0e04b1bc5 146
youkee 0:1ad0e04b1bc5 147 private:
youkee 0:1ad0e04b1bc5 148 void characteristicDiscoveryStarted(Gap::Handle_t connectionHandle) {
youkee 0:1ad0e04b1bc5 149 connHandle = connectionHandle;
youkee 0:1ad0e04b1bc5 150 resetDiscoveredCharacteristics();
youkee 0:1ad0e04b1bc5 151 state = CHARACTERISTIC_DISCOVERY_ACTIVE;
youkee 0:1ad0e04b1bc5 152 }
youkee 0:1ad0e04b1bc5 153
youkee 0:1ad0e04b1bc5 154 private:
youkee 0:1ad0e04b1bc5 155 /**
youkee 0:1ad0e04b1bc5 156 * A datatype to contain service-indices for which long UUIDs need to be
youkee 0:1ad0e04b1bc5 157 * discovered using read_val_by_uuid().
youkee 0:1ad0e04b1bc5 158 */
youkee 0:1ad0e04b1bc5 159 class ServiceUUIDDiscoveryQueue {
youkee 0:1ad0e04b1bc5 160 public:
youkee 0:1ad0e04b1bc5 161 ServiceUUIDDiscoveryQueue(nRF5xServiceDiscovery *parent) :
youkee 0:1ad0e04b1bc5 162 numIndices(0),
youkee 0:1ad0e04b1bc5 163 serviceIndices(),
youkee 0:1ad0e04b1bc5 164 parentDiscoveryObject(parent) {
youkee 0:1ad0e04b1bc5 165 /* empty */
youkee 0:1ad0e04b1bc5 166 }
youkee 0:1ad0e04b1bc5 167
youkee 0:1ad0e04b1bc5 168 public:
youkee 0:1ad0e04b1bc5 169 void reset(void) {
youkee 0:1ad0e04b1bc5 170 numIndices = 0;
youkee 0:1ad0e04b1bc5 171 for (unsigned i = 0; i < BLE_DB_DISCOVERY_MAX_SRV; i++) {
youkee 0:1ad0e04b1bc5 172 serviceIndices[i] = INVALID_INDEX;
youkee 0:1ad0e04b1bc5 173 }
youkee 0:1ad0e04b1bc5 174 }
youkee 0:1ad0e04b1bc5 175 void enqueue(int serviceIndex) {
youkee 0:1ad0e04b1bc5 176 serviceIndices[numIndices++] = serviceIndex;
youkee 0:1ad0e04b1bc5 177 }
youkee 0:1ad0e04b1bc5 178 int dequeue(void) {
youkee 0:1ad0e04b1bc5 179 if (numIndices == 0) {
youkee 0:1ad0e04b1bc5 180 return INVALID_INDEX;
youkee 0:1ad0e04b1bc5 181 }
youkee 0:1ad0e04b1bc5 182
youkee 0:1ad0e04b1bc5 183 unsigned valueToReturn = serviceIndices[0];
youkee 0:1ad0e04b1bc5 184 numIndices--;
youkee 0:1ad0e04b1bc5 185 for (unsigned i = 0; i < numIndices; i++) {
youkee 0:1ad0e04b1bc5 186 serviceIndices[i] = serviceIndices[i + 1];
youkee 0:1ad0e04b1bc5 187 }
youkee 0:1ad0e04b1bc5 188
youkee 0:1ad0e04b1bc5 189 return valueToReturn;
youkee 0:1ad0e04b1bc5 190 }
youkee 0:1ad0e04b1bc5 191 unsigned getFirst(void) const {
youkee 0:1ad0e04b1bc5 192 return serviceIndices[0];
youkee 0:1ad0e04b1bc5 193 }
youkee 0:1ad0e04b1bc5 194 size_t getCount(void) const {
youkee 0:1ad0e04b1bc5 195 return numIndices;
youkee 0:1ad0e04b1bc5 196 }
youkee 0:1ad0e04b1bc5 197
youkee 0:1ad0e04b1bc5 198 /**
youkee 0:1ad0e04b1bc5 199 * Trigger UUID discovery for the first of the enqueued ServiceIndices.
youkee 0:1ad0e04b1bc5 200 */
youkee 0:1ad0e04b1bc5 201 void triggerFirst(void);
youkee 0:1ad0e04b1bc5 202
youkee 0:1ad0e04b1bc5 203 private:
youkee 0:1ad0e04b1bc5 204 static const int INVALID_INDEX = -1;
youkee 0:1ad0e04b1bc5 205
youkee 0:1ad0e04b1bc5 206 private:
youkee 0:1ad0e04b1bc5 207 size_t numIndices;
youkee 0:1ad0e04b1bc5 208 int serviceIndices[BLE_DB_DISCOVERY_MAX_SRV];
youkee 0:1ad0e04b1bc5 209
youkee 0:1ad0e04b1bc5 210 nRF5xServiceDiscovery *parentDiscoveryObject;
youkee 0:1ad0e04b1bc5 211 };
youkee 0:1ad0e04b1bc5 212 friend class ServiceUUIDDiscoveryQueue;
youkee 0:1ad0e04b1bc5 213
youkee 0:1ad0e04b1bc5 214 /**
youkee 0:1ad0e04b1bc5 215 * A datatype to contain characteristic-indices for which long UUIDs need to
youkee 0:1ad0e04b1bc5 216 * be discovered using read_val_by_uuid().
youkee 0:1ad0e04b1bc5 217 */
youkee 0:1ad0e04b1bc5 218 class CharUUIDDiscoveryQueue {
youkee 0:1ad0e04b1bc5 219 public:
youkee 0:1ad0e04b1bc5 220 CharUUIDDiscoveryQueue(nRF5xServiceDiscovery *parent) :
youkee 0:1ad0e04b1bc5 221 numIndices(0),
youkee 0:1ad0e04b1bc5 222 charIndices(),
youkee 0:1ad0e04b1bc5 223 parentDiscoveryObject(parent) {
youkee 0:1ad0e04b1bc5 224 /* empty */
youkee 0:1ad0e04b1bc5 225 }
youkee 0:1ad0e04b1bc5 226
youkee 0:1ad0e04b1bc5 227 public:
youkee 0:1ad0e04b1bc5 228 void reset(void) {
youkee 0:1ad0e04b1bc5 229 numIndices = 0;
youkee 0:1ad0e04b1bc5 230 for (unsigned i = 0; i < BLE_DB_DISCOVERY_MAX_SRV; i++) {
youkee 0:1ad0e04b1bc5 231 charIndices[i] = INVALID_INDEX;
youkee 0:1ad0e04b1bc5 232 }
youkee 0:1ad0e04b1bc5 233 }
youkee 0:1ad0e04b1bc5 234 void enqueue(int serviceIndex) {
youkee 0:1ad0e04b1bc5 235 charIndices[numIndices++] = serviceIndex;
youkee 0:1ad0e04b1bc5 236 }
youkee 0:1ad0e04b1bc5 237 int dequeue(void) {
youkee 0:1ad0e04b1bc5 238 if (numIndices == 0) {
youkee 0:1ad0e04b1bc5 239 return INVALID_INDEX;
youkee 0:1ad0e04b1bc5 240 }
youkee 0:1ad0e04b1bc5 241
youkee 0:1ad0e04b1bc5 242 unsigned valueToReturn = charIndices[0];
youkee 0:1ad0e04b1bc5 243 numIndices--;
youkee 0:1ad0e04b1bc5 244 for (unsigned i = 0; i < numIndices; i++) {
youkee 0:1ad0e04b1bc5 245 charIndices[i] = charIndices[i + 1];
youkee 0:1ad0e04b1bc5 246 }
youkee 0:1ad0e04b1bc5 247
youkee 0:1ad0e04b1bc5 248 return valueToReturn;
youkee 0:1ad0e04b1bc5 249 }
youkee 0:1ad0e04b1bc5 250 unsigned getFirst(void) const {
youkee 0:1ad0e04b1bc5 251 return charIndices[0];
youkee 0:1ad0e04b1bc5 252 }
youkee 0:1ad0e04b1bc5 253 size_t getCount(void) const {
youkee 0:1ad0e04b1bc5 254 return numIndices;
youkee 0:1ad0e04b1bc5 255 }
youkee 0:1ad0e04b1bc5 256
youkee 0:1ad0e04b1bc5 257 /**
youkee 0:1ad0e04b1bc5 258 * Trigger UUID discovery for the first of the enqueued charIndices.
youkee 0:1ad0e04b1bc5 259 */
youkee 0:1ad0e04b1bc5 260 void triggerFirst(void);
youkee 0:1ad0e04b1bc5 261
youkee 0:1ad0e04b1bc5 262 private:
youkee 0:1ad0e04b1bc5 263 static const int INVALID_INDEX = -1;
youkee 0:1ad0e04b1bc5 264
youkee 0:1ad0e04b1bc5 265 private:
youkee 0:1ad0e04b1bc5 266 size_t numIndices;
youkee 0:1ad0e04b1bc5 267 int charIndices[BLE_DB_DISCOVERY_MAX_CHAR_PER_SRV];
youkee 0:1ad0e04b1bc5 268
youkee 0:1ad0e04b1bc5 269 nRF5xServiceDiscovery *parentDiscoveryObject;
youkee 0:1ad0e04b1bc5 270 };
youkee 0:1ad0e04b1bc5 271 friend class CharUUIDDiscoveryQueue;
youkee 0:1ad0e04b1bc5 272
youkee 0:1ad0e04b1bc5 273 private:
youkee 0:1ad0e04b1bc5 274 friend void bleGattcEventHandler(const ble_evt_t *p_ble_evt);
youkee 0:1ad0e04b1bc5 275 void progressCharacteristicDiscovery(void);
youkee 0:1ad0e04b1bc5 276 void progressServiceDiscovery(void);
youkee 0:1ad0e04b1bc5 277
youkee 0:1ad0e04b1bc5 278 private:
youkee 0:1ad0e04b1bc5 279 nRF5xGattClient *gattc;
youkee 0:1ad0e04b1bc5 280
youkee 0:1ad0e04b1bc5 281 private:
youkee 0:1ad0e04b1bc5 282 uint8_t serviceIndex; /**< Index of the current service being discovered. This is intended for internal use during service discovery.*/
youkee 0:1ad0e04b1bc5 283 uint8_t numServices; /**< Number of services at the peers GATT database.*/
youkee 0:1ad0e04b1bc5 284 uint8_t characteristicIndex; /**< Index of the current characteristic being discovered. This is intended for internal use during service discovery.*/
youkee 0:1ad0e04b1bc5 285 uint8_t numCharacteristics; /**< Number of characteristics within the service.*/
youkee 0:1ad0e04b1bc5 286
youkee 0:1ad0e04b1bc5 287 enum State_t {
youkee 0:1ad0e04b1bc5 288 INACTIVE,
youkee 0:1ad0e04b1bc5 289 SERVICE_DISCOVERY_ACTIVE,
youkee 0:1ad0e04b1bc5 290 CHARACTERISTIC_DISCOVERY_ACTIVE,
youkee 0:1ad0e04b1bc5 291 DISCOVER_SERVICE_UUIDS,
youkee 0:1ad0e04b1bc5 292 DISCOVER_CHARACTERISTIC_UUIDS,
youkee 0:1ad0e04b1bc5 293 } state;
youkee 0:1ad0e04b1bc5 294
youkee 0:1ad0e04b1bc5 295 DiscoveredService services[BLE_DB_DISCOVERY_MAX_SRV]; /**< Information related to the current service being discovered.
youkee 0:1ad0e04b1bc5 296 * This is intended for internal use during service discovery. */
youkee 0:1ad0e04b1bc5 297 nRF5xDiscoveredCharacteristic characteristics[BLE_DB_DISCOVERY_MAX_CHAR_PER_SRV];
youkee 0:1ad0e04b1bc5 298
youkee 0:1ad0e04b1bc5 299 ServiceUUIDDiscoveryQueue serviceUUIDDiscoveryQueue;
youkee 0:1ad0e04b1bc5 300 CharUUIDDiscoveryQueue charUUIDDiscoveryQueue;
youkee 0:1ad0e04b1bc5 301
youkee 0:1ad0e04b1bc5 302 TerminationCallback_t onTerminationCallback;
youkee 0:1ad0e04b1bc5 303 };
youkee 0:1ad0e04b1bc5 304
youkee 0:1ad0e04b1bc5 305 #endif /*__NRF_SERVICE_DISCOVERY_H__*/