test firmware for BLE Micro V1.3 1. test io, vcc and ble 2. act as a UART to BLE bridge

Dependencies:   BLE_API Buffer mbed

Fork of BLE_LEDBlinker by Bluetooth Low Energy

Committer:
arch
Date:
Tue Dec 08 06:02:25 2015 +0000
Revision:
11:c8cbc4bc2c17
test firmware for BLE Micro V1.3

Who changed what in which revision?

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