test
Fork of nRF51822 by
btle/btle_discovery.h@292:01b717803437, 2015-06-19 (annotated)
- Committer:
- rgrover1
- Date:
- Fri Jun 19 15:55:27 2015 +0100
- Revision:
- 292:01b717803437
- Parent:
- 291:d7590ed48009
- Child:
- 293:cd86acfe41be
Synchronized with git rev 3d9816a4
Author: Rohit Grover
add class for ServicesNeedingUUIDDiscovery
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rgrover1 | 239:693a1f145b5a | 1 | /* mbed Microcontroller Library |
rgrover1 | 239:693a1f145b5a | 2 | * Copyright (c) 2006-2013 ARM Limited |
rgrover1 | 239:693a1f145b5a | 3 | * |
rgrover1 | 239:693a1f145b5a | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
rgrover1 | 239:693a1f145b5a | 5 | * you may not use this file except in compliance with the License. |
rgrover1 | 239:693a1f145b5a | 6 | * You may obtain a copy of the License at |
rgrover1 | 239:693a1f145b5a | 7 | * |
rgrover1 | 239:693a1f145b5a | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
rgrover1 | 239:693a1f145b5a | 9 | * |
rgrover1 | 239:693a1f145b5a | 10 | * Unless required by applicable law or agreed to in writing, software |
rgrover1 | 239:693a1f145b5a | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
rgrover1 | 239:693a1f145b5a | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
rgrover1 | 239:693a1f145b5a | 13 | * See the License for the specific language governing permissions and |
rgrover1 | 239:693a1f145b5a | 14 | * limitations under the License. |
rgrover1 | 239:693a1f145b5a | 15 | */ |
rgrover1 | 239:693a1f145b5a | 16 | |
rgrover1 | 239:693a1f145b5a | 17 | #ifndef _BTLE_DISCOVERY_H_ |
rgrover1 | 239:693a1f145b5a | 18 | #define _BTLE_DISCOVERY_H_ |
rgrover1 | 239:693a1f145b5a | 19 | |
rgrover1 | 247:df37e7bb3f71 | 20 | #include "ble.h" |
rgrover1 | 263:d5da37e9a93b | 21 | #include "ServiceDiscovery.h" |
rgrover1 | 247:df37e7bb3f71 | 22 | |
rgrover1 | 247:df37e7bb3f71 | 23 | void bleGattcEventHandler(const ble_evt_t *p_ble_evt); |
rgrover1 | 239:693a1f145b5a | 24 | |
rgrover1 | 250:db3c51656388 | 25 | class NordicServiceDiscovery : public ServiceDiscovery |
rgrover1 | 250:db3c51656388 | 26 | { |
rgrover1 | 248:71ef03789dd3 | 27 | public: |
rgrover1 | 260:ea2798f615e5 | 28 | static const uint16_t SRV_DISC_START_HANDLE = 0x0001; /**< The start handle value used during service discovery. */ |
rgrover1 | 284:29fb6af6671d | 29 | static const uint16_t SRV_DISC_END_HANDLE = 0xFFFF; /**< The end handle value used during service discovery. */ |
rgrover1 | 260:ea2798f615e5 | 30 | |
rgrover1 | 289:b33301b019e6 | 31 | public: |
rgrover1 | 248:71ef03789dd3 | 32 | static const unsigned BLE_DB_DISCOVERY_MAX_SRV = 4; /**< Maximum number of services we can retain information for after a single discovery. */ |
rgrover1 | 248:71ef03789dd3 | 33 | static const unsigned BLE_DB_DISCOVERY_MAX_CHAR_PER_SRV = 4; /**< Maximum number of characteristics per service we can retain information for. */ |
rgrover1 | 260:ea2798f615e5 | 34 | |
rgrover1 | 260:ea2798f615e5 | 35 | public: |
rgrover1 | 288:1d85c4a4d397 | 36 | NordicServiceDiscovery() : serviceIndex(0), |
rgrover1 | 288:1d85c4a4d397 | 37 | numServices(0), |
rgrover1 | 288:1d85c4a4d397 | 38 | characteristicIndex(0), |
rgrover1 | 288:1d85c4a4d397 | 39 | numCharacteristics(0), |
rgrover1 | 288:1d85c4a4d397 | 40 | state(INACTIVE), |
rgrover1 | 288:1d85c4a4d397 | 41 | services(), |
rgrover1 | 288:1d85c4a4d397 | 42 | characteristics(), |
rgrover1 | 292:01b717803437 | 43 | serviceIndicesNeedingUUIDDiscovery(this), |
rgrover1 | 288:1d85c4a4d397 | 44 | onTerminationCallback(NULL) { |
rgrover1 | 288:1d85c4a4d397 | 45 | /* empty */ |
rgrover1 | 288:1d85c4a4d397 | 46 | } |
rgrover1 | 288:1d85c4a4d397 | 47 | |
rgrover1 | 288:1d85c4a4d397 | 48 | public: |
rgrover1 | 260:ea2798f615e5 | 49 | ble_error_t launchCharacteristicDiscovery(Gap::Handle_t connectionHandle, Gap::Handle_t startHandle, Gap::Handle_t endHandle); |
rgrover1 | 239:693a1f145b5a | 50 | |
rgrover1 | 239:693a1f145b5a | 51 | public: |
rgrover1 | 248:71ef03789dd3 | 52 | void setupDiscoveredServices(const ble_gattc_evt_prim_srvc_disc_rsp_t *response); |
rgrover1 | 248:71ef03789dd3 | 53 | void setupDiscoveredCharacteristics(const ble_gattc_evt_char_disc_rsp_t *response); |
rgrover1 | 248:71ef03789dd3 | 54 | |
rgrover1 | 290:83c994bf62d0 | 55 | void triggerServiceUUIDDiscovery(void); |
rgrover1 | 290:83c994bf62d0 | 56 | void processDiscoverUUIDResponse(const ble_gattc_evt_char_val_by_uuid_read_rsp_t *response); |
rgrover1 | 290:83c994bf62d0 | 57 | void removeFirstServiceNeedingUUIDDiscovery(void); |
rgrover1 | 290:83c994bf62d0 | 58 | |
rgrover1 | 248:71ef03789dd3 | 59 | void terminateServiceDiscovery(void) { |
rgrover1 | 280:cbaa4cb83548 | 60 | bool wasActive = isActive(); |
rgrover1 | 286:042b01e59cf8 | 61 | state = INACTIVE; |
rgrover1 | 280:cbaa4cb83548 | 62 | |
rgrover1 | 280:cbaa4cb83548 | 63 | if (wasActive && onTerminationCallback) { |
rgrover1 | 280:cbaa4cb83548 | 64 | onTerminationCallback(connHandle); |
rgrover1 | 280:cbaa4cb83548 | 65 | } |
rgrover1 | 239:693a1f145b5a | 66 | } |
rgrover1 | 239:693a1f145b5a | 67 | |
rgrover1 | 245:3abc61d38db3 | 68 | void terminateCharacteristicDiscovery(void) { |
rgrover1 | 286:042b01e59cf8 | 69 | if (state == CHARACTERISTIC_DISCOVERY_ACTIVE) { |
rgrover1 | 286:042b01e59cf8 | 70 | state = SERVICE_DISCOVERY_ACTIVE; |
rgrover1 | 286:042b01e59cf8 | 71 | } |
rgrover1 | 245:3abc61d38db3 | 72 | serviceIndex++; /* Progress service index to keep discovery alive. */ |
rgrover1 | 245:3abc61d38db3 | 73 | } |
rgrover1 | 245:3abc61d38db3 | 74 | |
rgrover1 | 279:6e5e79f1fb61 | 75 | bool isActive(void) const { |
rgrover1 | 286:042b01e59cf8 | 76 | return state != INACTIVE; |
rgrover1 | 279:6e5e79f1fb61 | 77 | } |
rgrover1 | 279:6e5e79f1fb61 | 78 | |
rgrover1 | 280:cbaa4cb83548 | 79 | void setOnTermination(TerminationCallback_t callback) { |
rgrover1 | 280:cbaa4cb83548 | 80 | onTerminationCallback = callback; |
rgrover1 | 280:cbaa4cb83548 | 81 | } |
rgrover1 | 280:cbaa4cb83548 | 82 | |
rgrover1 | 260:ea2798f615e5 | 83 | private: |
rgrover1 | 239:693a1f145b5a | 84 | void resetDiscoveredServices(void) { |
rgrover1 | 242:73fc02cc20b1 | 85 | numServices = 0; |
rgrover1 | 242:73fc02cc20b1 | 86 | serviceIndex = 0; |
rgrover1 | 239:693a1f145b5a | 87 | memset(services, 0, sizeof(DiscoveredService) * BLE_DB_DISCOVERY_MAX_SRV); |
rgrover1 | 239:693a1f145b5a | 88 | } |
rgrover1 | 239:693a1f145b5a | 89 | |
rgrover1 | 245:3abc61d38db3 | 90 | void resetDiscoveredCharacteristics(void) { |
rgrover1 | 245:3abc61d38db3 | 91 | numCharacteristics = 0; |
rgrover1 | 245:3abc61d38db3 | 92 | characteristicIndex = 0; |
rgrover1 | 245:3abc61d38db3 | 93 | memset(characteristics, 0, sizeof(DiscoveredCharacteristic) * BLE_DB_DISCOVERY_MAX_CHAR_PER_SRV); |
rgrover1 | 245:3abc61d38db3 | 94 | } |
rgrover1 | 245:3abc61d38db3 | 95 | |
rgrover1 | 245:3abc61d38db3 | 96 | public: |
rgrover1 | 243:a966506d1e5b | 97 | void serviceDiscoveryStarted(Gap::Handle_t connectionHandle) { |
rgrover1 | 291:d7590ed48009 | 98 | connHandle = connectionHandle; |
rgrover1 | 243:a966506d1e5b | 99 | resetDiscoveredServices(); |
rgrover1 | 286:042b01e59cf8 | 100 | state = SERVICE_DISCOVERY_ACTIVE; |
rgrover1 | 243:a966506d1e5b | 101 | } |
rgrover1 | 243:a966506d1e5b | 102 | |
rgrover1 | 260:ea2798f615e5 | 103 | private: |
rgrover1 | 245:3abc61d38db3 | 104 | void characteristicDiscoveryStarted(Gap::Handle_t connectionHandle) { |
rgrover1 | 291:d7590ed48009 | 105 | connHandle = connectionHandle; |
rgrover1 | 245:3abc61d38db3 | 106 | resetDiscoveredCharacteristics(); |
rgrover1 | 286:042b01e59cf8 | 107 | state = CHARACTERISTIC_DISCOVERY_ACTIVE; |
rgrover1 | 245:3abc61d38db3 | 108 | } |
rgrover1 | 245:3abc61d38db3 | 109 | |
rgrover1 | 251:d4e0cf5e8751 | 110 | private: |
rgrover1 | 292:01b717803437 | 111 | class ServiceIndicesNeedingUUIDDiscovery { |
rgrover1 | 292:01b717803437 | 112 | public: |
rgrover1 | 292:01b717803437 | 113 | ServiceIndicesNeedingUUIDDiscovery(NordicServiceDiscovery *parent) : |
rgrover1 | 292:01b717803437 | 114 | numIndices(0), |
rgrover1 | 292:01b717803437 | 115 | serviceIndices(), |
rgrover1 | 292:01b717803437 | 116 | parentContainer(parent) { |
rgrover1 | 292:01b717803437 | 117 | /* empty */ |
rgrover1 | 292:01b717803437 | 118 | } |
rgrover1 | 292:01b717803437 | 119 | |
rgrover1 | 292:01b717803437 | 120 | public: |
rgrover1 | 292:01b717803437 | 121 | void reset(void) { |
rgrover1 | 292:01b717803437 | 122 | numIndices = 0; |
rgrover1 | 292:01b717803437 | 123 | for (unsigned index = 0; index < BLE_DB_DISCOVERY_MAX_SRV; index++) { |
rgrover1 | 292:01b717803437 | 124 | serviceIndices[index] = INVALID_SERVICE_INDEX; |
rgrover1 | 292:01b717803437 | 125 | } |
rgrover1 | 292:01b717803437 | 126 | } |
rgrover1 | 292:01b717803437 | 127 | void append(int serviceIndex) { |
rgrover1 | 292:01b717803437 | 128 | serviceIndices[numIndices++] = serviceIndex; |
rgrover1 | 292:01b717803437 | 129 | } |
rgrover1 | 292:01b717803437 | 130 | void removeFirst(void) { |
rgrover1 | 292:01b717803437 | 131 | numIndices--; |
rgrover1 | 292:01b717803437 | 132 | for (unsigned index = 0; index < numIndices; index++) { |
rgrover1 | 292:01b717803437 | 133 | serviceIndices[index] = serviceIndices[index + 1]; |
rgrover1 | 292:01b717803437 | 134 | } |
rgrover1 | 292:01b717803437 | 135 | } |
rgrover1 | 292:01b717803437 | 136 | size_t getCount(void) const { |
rgrover1 | 292:01b717803437 | 137 | return numIndices; |
rgrover1 | 292:01b717803437 | 138 | } |
rgrover1 | 292:01b717803437 | 139 | |
rgrover1 | 292:01b717803437 | 140 | void triggerFirst(void); |
rgrover1 | 292:01b717803437 | 141 | |
rgrover1 | 292:01b717803437 | 142 | private: |
rgrover1 | 292:01b717803437 | 143 | static const int INVALID_SERVICE_INDEX = -1; |
rgrover1 | 292:01b717803437 | 144 | |
rgrover1 | 292:01b717803437 | 145 | private: |
rgrover1 | 292:01b717803437 | 146 | size_t numIndices; |
rgrover1 | 292:01b717803437 | 147 | int serviceIndices[BLE_DB_DISCOVERY_MAX_SRV]; |
rgrover1 | 292:01b717803437 | 148 | |
rgrover1 | 292:01b717803437 | 149 | NordicServiceDiscovery *parentContainer; |
rgrover1 | 292:01b717803437 | 150 | }; |
rgrover1 | 292:01b717803437 | 151 | friend class ServiceIndicesNeedingUUIDDiscovery; |
rgrover1 | 292:01b717803437 | 152 | |
rgrover1 | 292:01b717803437 | 153 | private: |
rgrover1 | 251:d4e0cf5e8751 | 154 | friend void bleGattcEventHandler(const ble_evt_t *p_ble_evt); |
rgrover1 | 251:d4e0cf5e8751 | 155 | void progressCharacteristicDiscovery(void); |
rgrover1 | 251:d4e0cf5e8751 | 156 | void progressServiceDiscovery(void); |
rgrover1 | 239:693a1f145b5a | 157 | |
rgrover1 | 248:71ef03789dd3 | 158 | private: |
rgrover1 | 248:71ef03789dd3 | 159 | uint8_t serviceIndex; /**< Index of the current service being discovered. This is intended for internal use during service discovery.*/ |
rgrover1 | 248:71ef03789dd3 | 160 | uint8_t numServices; /**< Number of services at the peers GATT database.*/ |
rgrover1 | 248:71ef03789dd3 | 161 | uint8_t characteristicIndex; /**< Index of the current characteristic being discovered. This is intended for internal use during service discovery.*/ |
rgrover1 | 248:71ef03789dd3 | 162 | uint8_t numCharacteristics; /**< Number of characteristics within the service.*/ |
rgrover1 | 248:71ef03789dd3 | 163 | |
rgrover1 | 286:042b01e59cf8 | 164 | enum State_t { |
rgrover1 | 286:042b01e59cf8 | 165 | INACTIVE, |
rgrover1 | 286:042b01e59cf8 | 166 | SERVICE_DISCOVERY_ACTIVE, |
rgrover1 | 286:042b01e59cf8 | 167 | CHARACTERISTIC_DISCOVERY_ACTIVE, |
rgrover1 | 290:83c994bf62d0 | 168 | DISCOVER_SERVICE_UUIDS, |
rgrover1 | 286:042b01e59cf8 | 169 | } state; |
rgrover1 | 248:71ef03789dd3 | 170 | |
rgrover1 | 248:71ef03789dd3 | 171 | DiscoveredService services[BLE_DB_DISCOVERY_MAX_SRV]; /**< Information related to the current service being discovered. |
rgrover1 | 248:71ef03789dd3 | 172 | * This is intended for internal use during service discovery. */ |
rgrover1 | 248:71ef03789dd3 | 173 | DiscoveredCharacteristic characteristics[BLE_DB_DISCOVERY_MAX_CHAR_PER_SRV]; |
rgrover1 | 280:cbaa4cb83548 | 174 | |
rgrover1 | 292:01b717803437 | 175 | ServiceIndicesNeedingUUIDDiscovery serviceIndicesNeedingUUIDDiscovery; |
rgrover1 | 292:01b717803437 | 176 | |
rgrover1 | 280:cbaa4cb83548 | 177 | TerminationCallback_t onTerminationCallback; |
rgrover1 | 240:75b69581d1dd | 178 | }; |
rgrover1 | 240:75b69581d1dd | 179 | |
rgrover1 | 239:693a1f145b5a | 180 | #endif /*_BTLE_DISCOVERY_H_*/ |