Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: BLE_ANCS_SDAPI BLE_temperature BLE_HeartRate writable_gatt ... more
btle/btle_discovery.h@246:1098730d84de, 2015-06-19 (annotated)
- Committer:
 - rgrover1
 - Date:
 - Fri Jun 19 15:55:22 2015 +0100
 - Revision:
 - 246:1098730d84de
 - Parent:
 - 245:3abc61d38db3
 - Child:
 - 247:df37e7bb3f71
 
Synchronized with git rev 9516ab88
Author: Rohit Grover
white space diffs.
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 | 239:693a1f145b5a | 20 | #include "blecommon.h" | 
| rgrover1 | 239:693a1f145b5a | 21 | #include "UUID.h" | 
| rgrover1 | 239:693a1f145b5a | 22 | #include "Gap.h" | 
| rgrover1 | 239:693a1f145b5a | 23 | #include "ble_gattc.h" | 
| rgrover1 | 239:693a1f145b5a | 24 | #include <stdio.h> | 
| rgrover1 | 239:693a1f145b5a | 25 | |
| rgrover1 | 239:693a1f145b5a | 26 | /**@brief Structure for holding information about the service and the characteristics found during | 
| rgrover1 | 239:693a1f145b5a | 27 | * the discovery process. | 
| rgrover1 | 239:693a1f145b5a | 28 | */ | 
| rgrover1 | 239:693a1f145b5a | 29 | struct DiscoveredService { | 
| rgrover1 | 239:693a1f145b5a | 30 | void setup(ShortUUIDBytes_t uuidIn, Gap::Handle_t start, Gap::Handle_t end) { | 
| rgrover1 | 239:693a1f145b5a | 31 | uuid = uuidIn; | 
| rgrover1 | 239:693a1f145b5a | 32 | startHandle = start; | 
| rgrover1 | 239:693a1f145b5a | 33 | endHandle = end; | 
| rgrover1 | 239:693a1f145b5a | 34 | } | 
| rgrover1 | 239:693a1f145b5a | 35 | |
| rgrover1 | 239:693a1f145b5a | 36 | ShortUUIDBytes_t uuid; /**< UUID of the service. */ | 
| rgrover1 | 239:693a1f145b5a | 37 | Gap::Handle_t startHandle; /**< Service Handle Range. */ | 
| rgrover1 | 239:693a1f145b5a | 38 | Gap::Handle_t endHandle; /**< Service Handle Range. */ | 
| rgrover1 | 239:693a1f145b5a | 39 | }; | 
| rgrover1 | 239:693a1f145b5a | 40 | |
| rgrover1 | 239:693a1f145b5a | 41 | /**@brief Structure for holding information about the service and the characteristics found during | 
| rgrover1 | 239:693a1f145b5a | 42 | * the discovery process. | 
| rgrover1 | 239:693a1f145b5a | 43 | */ | 
| rgrover1 | 239:693a1f145b5a | 44 | struct DiscoveredCharacteristic { | 
| rgrover1 | 239:693a1f145b5a | 45 | struct Properties_t { | 
| rgrover1 | 239:693a1f145b5a | 46 | static const uint8_t BROADCAST_PROPERTY_MASK = 0x01; | 
| rgrover1 | 239:693a1f145b5a | 47 | static const uint8_t READ_PROPERTY_MASK = 0x02; | 
| rgrover1 | 239:693a1f145b5a | 48 | static const uint8_t WRITE_WO_RESPONSE_PROPERTY_MASK = 0x04; | 
| rgrover1 | 239:693a1f145b5a | 49 | static const uint8_t WRITE_PROPERTY_MASK = 0x08; | 
| rgrover1 | 239:693a1f145b5a | 50 | static const uint8_t NOTIFY_PROPERTY_MASK = 0x10; | 
| rgrover1 | 239:693a1f145b5a | 51 | static const uint8_t INDICATE_PROPERTY_MASK = 0x20; | 
| rgrover1 | 239:693a1f145b5a | 52 | static const uint8_t AUTH_SIGNED_PROPERTY_MASK = 0x40; | 
| rgrover1 | 239:693a1f145b5a | 53 | |
| rgrover1 | 239:693a1f145b5a | 54 | Properties_t() : broadcast(0), read(0), write_wo_resp(0), write(0), notify(0), indicate(0), auth_signed_wr(0) { | 
| rgrover1 | 239:693a1f145b5a | 55 | /* empty */ | 
| rgrover1 | 239:693a1f145b5a | 56 | } | 
| rgrover1 | 239:693a1f145b5a | 57 | |
| rgrover1 | 239:693a1f145b5a | 58 | Properties_t(uint8_t props) : | 
| rgrover1 | 239:693a1f145b5a | 59 | broadcast(props & BROADCAST_PROPERTY_MASK), | 
| rgrover1 | 239:693a1f145b5a | 60 | read(props & READ_PROPERTY_MASK), | 
| rgrover1 | 239:693a1f145b5a | 61 | write_wo_resp(props & WRITE_WO_RESPONSE_PROPERTY_MASK), | 
| rgrover1 | 239:693a1f145b5a | 62 | write(props & WRITE_PROPERTY_MASK), | 
| rgrover1 | 239:693a1f145b5a | 63 | notify(props & NOTIFY_PROPERTY_MASK), | 
| rgrover1 | 239:693a1f145b5a | 64 | indicate(props & INDICATE_PROPERTY_MASK), | 
| rgrover1 | 239:693a1f145b5a | 65 | auth_signed_wr(props & AUTH_SIGNED_PROPERTY_MASK) { | 
| rgrover1 | 239:693a1f145b5a | 66 | /* empty*/ | 
| rgrover1 | 239:693a1f145b5a | 67 | } | 
| rgrover1 | 239:693a1f145b5a | 68 | |
| rgrover1 | 239:693a1f145b5a | 69 | uint8_t broadcast :1; /**< Broadcasting of the value permitted. */ | 
| rgrover1 | 239:693a1f145b5a | 70 | uint8_t read :1; /**< Reading the value permitted. */ | 
| rgrover1 | 239:693a1f145b5a | 71 | uint8_t write_wo_resp :1; /**< Writing the value with Write Command permitted. */ | 
| rgrover1 | 239:693a1f145b5a | 72 | uint8_t write :1; /**< Writing the value with Write Request permitted. */ | 
| rgrover1 | 239:693a1f145b5a | 73 | uint8_t notify :1; /**< Notications of the value permitted. */ | 
| rgrover1 | 239:693a1f145b5a | 74 | uint8_t indicate :1; /**< Indications of the value permitted. */ | 
| rgrover1 | 239:693a1f145b5a | 75 | uint8_t auth_signed_wr :1; /**< Writing the value with Signed Write Command permitted. */ | 
| rgrover1 | 239:693a1f145b5a | 76 | }; | 
| rgrover1 | 239:693a1f145b5a | 77 | |
| rgrover1 | 239:693a1f145b5a | 78 | void setup(ShortUUIDBytes_t uuidIn, Properties_t propsIn, Gap::Handle_t declHandleIn, Gap::Handle_t valueHandleIn) { | 
| rgrover1 | 239:693a1f145b5a | 79 | uuid = uuidIn; | 
| rgrover1 | 239:693a1f145b5a | 80 | props = propsIn; | 
| rgrover1 | 239:693a1f145b5a | 81 | declHandle = declHandleIn; | 
| rgrover1 | 239:693a1f145b5a | 82 | valueHandle = valueHandleIn; | 
| rgrover1 | 239:693a1f145b5a | 83 | } | 
| rgrover1 | 239:693a1f145b5a | 84 | |
| rgrover1 | 239:693a1f145b5a | 85 | ShortUUIDBytes_t uuid; | 
| rgrover1 | 239:693a1f145b5a | 86 | Properties_t props; | 
| rgrover1 | 239:693a1f145b5a | 87 | Gap::Handle_t declHandle; | 
| rgrover1 | 239:693a1f145b5a | 88 | Gap::Handle_t valueHandle; | 
| rgrover1 | 239:693a1f145b5a | 89 | }; | 
| rgrover1 | 239:693a1f145b5a | 90 | |
| rgrover1 | 239:693a1f145b5a | 91 | class ServiceDiscovery { | 
| rgrover1 | 239:693a1f145b5a | 92 | public: | 
| rgrover1 | 246:1098730d84de | 93 | static const unsigned BLE_DB_DISCOVERY_MAX_SRV = 4; /**< Maximum number of services we can retain information for after a single discovery. */ | 
| rgrover1 | 246:1098730d84de | 94 | static const unsigned BLE_DB_DISCOVERY_MAX_CHAR_PER_SRV = 4; /**< Maximum number of characteristics per service we can retain information for. */ | 
| rgrover1 | 246:1098730d84de | 95 | static const uint16_t SRV_DISC_START_HANDLE = 0x0001; /**< The start handle value used during service discovery. */ | 
| rgrover1 | 239:693a1f145b5a | 96 | |
| rgrover1 | 239:693a1f145b5a | 97 | typedef void (*ServiceCallback_t)(void); | 
| rgrover1 | 239:693a1f145b5a | 98 | typedef void (*CharacteristicCallback_t)(void); | 
| rgrover1 | 239:693a1f145b5a | 99 | |
| rgrover1 | 239:693a1f145b5a | 100 | public: | 
| rgrover1 | 239:693a1f145b5a | 101 | static ble_error_t launch(Gap::Handle_t connectionHandle, | 
| rgrover1 | 239:693a1f145b5a | 102 | ServiceCallback_t sc, | 
| rgrover1 | 239:693a1f145b5a | 103 | CharacteristicCallback_t cc = NULL); | 
| rgrover1 | 239:693a1f145b5a | 104 | static ble_error_t launch(Gap::Handle_t connectionHandle, | 
| rgrover1 | 239:693a1f145b5a | 105 | UUID matchingServiceUUID, | 
| rgrover1 | 239:693a1f145b5a | 106 | ServiceCallback_t sc, | 
| rgrover1 | 239:693a1f145b5a | 107 | UUID matchingCharacteristicUUID = ShortUUIDBytes_t(BLE_UUID_UNKNOWN), | 
| rgrover1 | 239:693a1f145b5a | 108 | CharacteristicCallback_t cc = NULL); | 
| rgrover1 | 239:693a1f145b5a | 109 | |
| rgrover1 | 239:693a1f145b5a | 110 | static ServiceDiscovery *getSingleton(void); | 
| rgrover1 | 239:693a1f145b5a | 111 | |
| rgrover1 | 239:693a1f145b5a | 112 | public: | 
| rgrover1 | 239:693a1f145b5a | 113 | void terminate(void) { | 
| rgrover1 | 242:73fc02cc20b1 | 114 | sDiscoveryActive = false; | 
| rgrover1 | 239:693a1f145b5a | 115 | printf("end of service discovery\r\n"); | 
| rgrover1 | 239:693a1f145b5a | 116 | } | 
| rgrover1 | 239:693a1f145b5a | 117 | |
| rgrover1 | 245:3abc61d38db3 | 118 | void terminateCharacteristicDiscovery(void) { | 
| rgrover1 | 245:3abc61d38db3 | 119 | cDiscoveryActive = false; | 
| rgrover1 | 245:3abc61d38db3 | 120 | sDiscoveryActive = true; | 
| rgrover1 | 245:3abc61d38db3 | 121 | serviceIndex++; /* Progress service index to keep discovery alive. */ | 
| rgrover1 | 245:3abc61d38db3 | 122 | } | 
| rgrover1 | 245:3abc61d38db3 | 123 | |
| rgrover1 | 239:693a1f145b5a | 124 | void resetDiscoveredServices(void) { | 
| rgrover1 | 242:73fc02cc20b1 | 125 | numServices = 0; | 
| rgrover1 | 242:73fc02cc20b1 | 126 | serviceIndex = 0; | 
| rgrover1 | 239:693a1f145b5a | 127 | memset(services, 0, sizeof(DiscoveredService) * BLE_DB_DISCOVERY_MAX_SRV); | 
| rgrover1 | 239:693a1f145b5a | 128 | } | 
| rgrover1 | 239:693a1f145b5a | 129 | |
| rgrover1 | 245:3abc61d38db3 | 130 | protected: | 
| rgrover1 | 245:3abc61d38db3 | 131 | void resetDiscoveredCharacteristics(void) { | 
| rgrover1 | 245:3abc61d38db3 | 132 | numCharacteristics = 0; | 
| rgrover1 | 245:3abc61d38db3 | 133 | characteristicIndex = 0; | 
| rgrover1 | 245:3abc61d38db3 | 134 | memset(characteristics, 0, sizeof(DiscoveredCharacteristic) * BLE_DB_DISCOVERY_MAX_CHAR_PER_SRV); | 
| rgrover1 | 245:3abc61d38db3 | 135 | } | 
| rgrover1 | 245:3abc61d38db3 | 136 | |
| rgrover1 | 245:3abc61d38db3 | 137 | public: | 
| rgrover1 | 243:a966506d1e5b | 138 | void serviceDiscoveryStarted(Gap::Handle_t connectionHandle) { | 
| rgrover1 | 243:a966506d1e5b | 139 | connHandle = connectionHandle; | 
| rgrover1 | 243:a966506d1e5b | 140 | resetDiscoveredServices(); | 
| rgrover1 | 243:a966506d1e5b | 141 | sDiscoveryActive = true; | 
| rgrover1 | 243:a966506d1e5b | 142 | cDiscoveryActive = false; | 
| rgrover1 | 243:a966506d1e5b | 143 | } | 
| rgrover1 | 243:a966506d1e5b | 144 | |
| rgrover1 | 243:a966506d1e5b | 145 | protected: | 
| rgrover1 | 245:3abc61d38db3 | 146 | void characteristicDiscoveryStarted(Gap::Handle_t connectionHandle) { | 
| rgrover1 | 245:3abc61d38db3 | 147 | connHandle = connectionHandle; | 
| rgrover1 | 245:3abc61d38db3 | 148 | resetDiscoveredCharacteristics(); | 
| rgrover1 | 245:3abc61d38db3 | 149 | cDiscoveryActive = true; | 
| rgrover1 | 245:3abc61d38db3 | 150 | sDiscoveryActive = false; | 
| rgrover1 | 245:3abc61d38db3 | 151 | } | 
| rgrover1 | 245:3abc61d38db3 | 152 | |
| rgrover1 | 245:3abc61d38db3 | 153 | protected: | 
| rgrover1 | 243:a966506d1e5b | 154 | ServiceDiscovery() { | 
| rgrover1 | 243:a966506d1e5b | 155 | /* empty */ | 
| rgrover1 | 243:a966506d1e5b | 156 | } | 
| rgrover1 | 243:a966506d1e5b | 157 | |
| rgrover1 | 243:a966506d1e5b | 158 | protected: | 
| rgrover1 | 243:a966506d1e5b | 159 | DiscoveredService services[BLE_DB_DISCOVERY_MAX_SRV]; /**< Information related to the current service being discovered. | 
| rgrover1 | 243:a966506d1e5b | 160 | * This is intended for internal use during service discovery. */ | 
| rgrover1 | 243:a966506d1e5b | 161 | DiscoveredCharacteristic characteristics[BLE_DB_DISCOVERY_MAX_CHAR_PER_SRV]; | 
| rgrover1 | 243:a966506d1e5b | 162 | |
| rgrover1 | 243:a966506d1e5b | 163 | uint16_t connHandle; /**< Connection handle as provided by the SoftDevice. */ | 
| rgrover1 | 243:a966506d1e5b | 164 | uint8_t serviceIndex; /**< Index of the current service being discovered. This is intended for internal use during service discovery.*/ | 
| rgrover1 | 243:a966506d1e5b | 165 | uint8_t numServices; /**< Number of services at the peers GATT database.*/ | 
| rgrover1 | 243:a966506d1e5b | 166 | uint8_t characteristicIndex; /**< Index of the current characteristic being discovered. This is intended for internal use during service discovery.*/ | 
| rgrover1 | 243:a966506d1e5b | 167 | uint8_t numCharacteristics; /**< Number of characteristics within the service.*/ | 
| rgrover1 | 243:a966506d1e5b | 168 | |
| rgrover1 | 243:a966506d1e5b | 169 | bool sDiscoveryActive; | 
| rgrover1 | 243:a966506d1e5b | 170 | bool cDiscoveryActive; | 
| rgrover1 | 243:a966506d1e5b | 171 | }; | 
| rgrover1 | 243:a966506d1e5b | 172 | |
| rgrover1 | 243:a966506d1e5b | 173 | class NordicServiceDiscovery : public ServiceDiscovery { | 
| rgrover1 | 243:a966506d1e5b | 174 | public: | 
| rgrover1 | 243:a966506d1e5b | 175 | void setupDiscoveredServices(const ble_gattc_evt_prim_srvc_disc_rsp_t *response); | 
| rgrover1 | 243:a966506d1e5b | 176 | void setupDiscoveredCharacteristics(const ble_gattc_evt_char_disc_rsp_t *response); | 
| rgrover1 | 243:a966506d1e5b | 177 | |
| rgrover1 | 244:57c98fe71376 | 178 | public: | 
| rgrover1 | 244:57c98fe71376 | 179 | ble_error_t launchCharacteristicDiscovery(Gap::Handle_t connectionHandle, Gap::Handle_t startHandle, Gap::Handle_t endHandle); | 
| rgrover1 | 244:57c98fe71376 | 180 | |
| rgrover1 | 243:a966506d1e5b | 181 | public: | 
| rgrover1 | 239:693a1f145b5a | 182 | void progressCharacteristicDiscovery() { | 
| rgrover1 | 242:73fc02cc20b1 | 183 | while (cDiscoveryActive && (characteristicIndex < numCharacteristics)) { | 
| rgrover1 | 239:693a1f145b5a | 184 | /* THIS IS WHERE THE CALLBACK WILL GO */ | 
| rgrover1 | 242:73fc02cc20b1 | 185 | printf("%x [%u]\r\n", characteristics[characteristicIndex].uuid, characteristics[characteristicIndex].valueHandle); | 
| rgrover1 | 239:693a1f145b5a | 186 | |
| rgrover1 | 242:73fc02cc20b1 | 187 | characteristicIndex++; | 
| rgrover1 | 239:693a1f145b5a | 188 | } | 
| rgrover1 | 239:693a1f145b5a | 189 | |
| rgrover1 | 242:73fc02cc20b1 | 190 | if (cDiscoveryActive) { | 
| rgrover1 | 242:73fc02cc20b1 | 191 | Gap::Handle_t startHandle = characteristics[characteristicIndex - 1].valueHandle + 1; | 
| rgrover1 | 242:73fc02cc20b1 | 192 | Gap::Handle_t endHandle = services[serviceIndex].endHandle; | 
| rgrover1 | 239:693a1f145b5a | 193 | resetDiscoveredCharacteristics(); | 
| rgrover1 | 239:693a1f145b5a | 194 | |
| rgrover1 | 239:693a1f145b5a | 195 | if (startHandle < endHandle) { | 
| rgrover1 | 239:693a1f145b5a | 196 | ble_gattc_handle_range_t handleRange = { | 
| rgrover1 | 239:693a1f145b5a | 197 | .start_handle = startHandle, | 
| rgrover1 | 239:693a1f145b5a | 198 | .end_handle = endHandle | 
| rgrover1 | 239:693a1f145b5a | 199 | }; | 
| rgrover1 | 239:693a1f145b5a | 200 | printf("char discovery returned %u\r\n", sd_ble_gattc_characteristics_discover(connHandle, &handleRange)); | 
| rgrover1 | 239:693a1f145b5a | 201 | } else { | 
| rgrover1 | 239:693a1f145b5a | 202 | terminateCharacteristicDiscovery(); | 
| rgrover1 | 239:693a1f145b5a | 203 | } | 
| rgrover1 | 239:693a1f145b5a | 204 | } | 
| rgrover1 | 239:693a1f145b5a | 205 | } | 
| rgrover1 | 239:693a1f145b5a | 206 | |
| rgrover1 | 239:693a1f145b5a | 207 | void progressServiceDiscovery() { | 
| rgrover1 | 242:73fc02cc20b1 | 208 | while (sDiscoveryActive && (serviceIndex < numServices)) { | 
| rgrover1 | 239:693a1f145b5a | 209 | /* THIS IS WHERE THE CALLBACK WILL GO */ | 
| rgrover1 | 242:73fc02cc20b1 | 210 | printf("%x [%u %u]\r\n", services[serviceIndex].uuid, services[serviceIndex].startHandle, services[serviceIndex].endHandle); | 
| rgrover1 | 239:693a1f145b5a | 211 | |
| rgrover1 | 239:693a1f145b5a | 212 | if (true) { /* characteristic discovery is optional. */ | 
| rgrover1 | 242:73fc02cc20b1 | 213 | launchCharacteristicDiscovery(connHandle, services[serviceIndex].startHandle, services[serviceIndex].endHandle); | 
| rgrover1 | 239:693a1f145b5a | 214 | } else { | 
| rgrover1 | 242:73fc02cc20b1 | 215 | serviceIndex++; /* Progress service index to keep discovery alive. */ | 
| rgrover1 | 239:693a1f145b5a | 216 | } | 
| rgrover1 | 239:693a1f145b5a | 217 | } | 
| rgrover1 | 242:73fc02cc20b1 | 218 | if (sDiscoveryActive && (numServices > 0) && (serviceIndex > 0)) { | 
| rgrover1 | 242:73fc02cc20b1 | 219 | Gap::Handle_t endHandle = services[serviceIndex - 1].endHandle; | 
| rgrover1 | 239:693a1f145b5a | 220 | resetDiscoveredServices(); | 
| rgrover1 | 239:693a1f145b5a | 221 | |
| rgrover1 | 239:693a1f145b5a | 222 | printf("services discover returned %u\r\n", sd_ble_gattc_primary_services_discover(connHandle, endHandle, NULL)); | 
| rgrover1 | 239:693a1f145b5a | 223 | } | 
| rgrover1 | 239:693a1f145b5a | 224 | } | 
| rgrover1 | 239:693a1f145b5a | 225 | |
| rgrover1 | 240:75b69581d1dd | 226 | }; | 
| rgrover1 | 240:75b69581d1dd | 227 | |
| rgrover1 | 239:693a1f145b5a | 228 | #endif /*_BTLE_DISCOVERY_H_*/ | 

