No changes
Fork of nRF51822 by
btle/btle_discovery.h@280:cbaa4cb83548, 2015-06-19 (annotated)
- Committer:
- rgrover1
- Date:
- Fri Jun 19 15:55:26 2015 +0100
- Revision:
- 280:cbaa4cb83548
- Parent:
- 279:6e5e79f1fb61
- Child:
- 284:29fb6af6671d
Synchronized with git rev 7a7b768e
Author: Rohit Grover
get onTerminationCallback invoked at the end of serviceDiscovery
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 | 260:ea2798f615e5 | 29 | |
rgrover1 | 260:ea2798f615e5 | 30 | private: |
rgrover1 | 248:71ef03789dd3 | 31 | 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 | 32 | 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 | 33 | |
rgrover1 | 260:ea2798f615e5 | 34 | public: |
rgrover1 | 260:ea2798f615e5 | 35 | ble_error_t launchCharacteristicDiscovery(Gap::Handle_t connectionHandle, Gap::Handle_t startHandle, Gap::Handle_t endHandle); |
rgrover1 | 239:693a1f145b5a | 36 | |
rgrover1 | 239:693a1f145b5a | 37 | public: |
rgrover1 | 248:71ef03789dd3 | 38 | void setupDiscoveredServices(const ble_gattc_evt_prim_srvc_disc_rsp_t *response); |
rgrover1 | 248:71ef03789dd3 | 39 | void setupDiscoveredCharacteristics(const ble_gattc_evt_char_disc_rsp_t *response); |
rgrover1 | 248:71ef03789dd3 | 40 | |
rgrover1 | 248:71ef03789dd3 | 41 | void terminateServiceDiscovery(void) { |
rgrover1 | 280:cbaa4cb83548 | 42 | bool wasActive = isActive(); |
rgrover1 | 280:cbaa4cb83548 | 43 | |
rgrover1 | 242:73fc02cc20b1 | 44 | sDiscoveryActive = false; |
rgrover1 | 248:71ef03789dd3 | 45 | cDiscoveryActive = false; |
rgrover1 | 280:cbaa4cb83548 | 46 | |
rgrover1 | 280:cbaa4cb83548 | 47 | if (wasActive && onTerminationCallback) { |
rgrover1 | 280:cbaa4cb83548 | 48 | onTerminationCallback(connHandle); |
rgrover1 | 280:cbaa4cb83548 | 49 | } |
rgrover1 | 239:693a1f145b5a | 50 | } |
rgrover1 | 239:693a1f145b5a | 51 | |
rgrover1 | 245:3abc61d38db3 | 52 | void terminateCharacteristicDiscovery(void) { |
rgrover1 | 245:3abc61d38db3 | 53 | cDiscoveryActive = false; |
rgrover1 | 245:3abc61d38db3 | 54 | sDiscoveryActive = true; |
rgrover1 | 245:3abc61d38db3 | 55 | serviceIndex++; /* Progress service index to keep discovery alive. */ |
rgrover1 | 245:3abc61d38db3 | 56 | } |
rgrover1 | 245:3abc61d38db3 | 57 | |
rgrover1 | 279:6e5e79f1fb61 | 58 | bool isActive(void) const { |
rgrover1 | 279:6e5e79f1fb61 | 59 | return (sDiscoveryActive || cDiscoveryActive); |
rgrover1 | 279:6e5e79f1fb61 | 60 | } |
rgrover1 | 279:6e5e79f1fb61 | 61 | |
rgrover1 | 280:cbaa4cb83548 | 62 | void setOnTermination(TerminationCallback_t callback) { |
rgrover1 | 280:cbaa4cb83548 | 63 | onTerminationCallback = callback; |
rgrover1 | 280:cbaa4cb83548 | 64 | } |
rgrover1 | 280:cbaa4cb83548 | 65 | |
rgrover1 | 260:ea2798f615e5 | 66 | private: |
rgrover1 | 239:693a1f145b5a | 67 | void resetDiscoveredServices(void) { |
rgrover1 | 242:73fc02cc20b1 | 68 | numServices = 0; |
rgrover1 | 242:73fc02cc20b1 | 69 | serviceIndex = 0; |
rgrover1 | 239:693a1f145b5a | 70 | memset(services, 0, sizeof(DiscoveredService) * BLE_DB_DISCOVERY_MAX_SRV); |
rgrover1 | 239:693a1f145b5a | 71 | } |
rgrover1 | 239:693a1f145b5a | 72 | |
rgrover1 | 245:3abc61d38db3 | 73 | void resetDiscoveredCharacteristics(void) { |
rgrover1 | 245:3abc61d38db3 | 74 | numCharacteristics = 0; |
rgrover1 | 245:3abc61d38db3 | 75 | characteristicIndex = 0; |
rgrover1 | 245:3abc61d38db3 | 76 | memset(characteristics, 0, sizeof(DiscoveredCharacteristic) * BLE_DB_DISCOVERY_MAX_CHAR_PER_SRV); |
rgrover1 | 245:3abc61d38db3 | 77 | } |
rgrover1 | 245:3abc61d38db3 | 78 | |
rgrover1 | 245:3abc61d38db3 | 79 | public: |
rgrover1 | 243:a966506d1e5b | 80 | void serviceDiscoveryStarted(Gap::Handle_t connectionHandle) { |
rgrover1 | 243:a966506d1e5b | 81 | connHandle = connectionHandle; |
rgrover1 | 243:a966506d1e5b | 82 | resetDiscoveredServices(); |
rgrover1 | 243:a966506d1e5b | 83 | sDiscoveryActive = true; |
rgrover1 | 243:a966506d1e5b | 84 | cDiscoveryActive = false; |
rgrover1 | 243:a966506d1e5b | 85 | } |
rgrover1 | 243:a966506d1e5b | 86 | |
rgrover1 | 260:ea2798f615e5 | 87 | private: |
rgrover1 | 245:3abc61d38db3 | 88 | void characteristicDiscoveryStarted(Gap::Handle_t connectionHandle) { |
rgrover1 | 245:3abc61d38db3 | 89 | connHandle = connectionHandle; |
rgrover1 | 245:3abc61d38db3 | 90 | resetDiscoveredCharacteristics(); |
rgrover1 | 245:3abc61d38db3 | 91 | cDiscoveryActive = true; |
rgrover1 | 245:3abc61d38db3 | 92 | sDiscoveryActive = false; |
rgrover1 | 245:3abc61d38db3 | 93 | } |
rgrover1 | 245:3abc61d38db3 | 94 | |
rgrover1 | 251:d4e0cf5e8751 | 95 | private: |
rgrover1 | 251:d4e0cf5e8751 | 96 | friend void bleGattcEventHandler(const ble_evt_t *p_ble_evt); |
rgrover1 | 251:d4e0cf5e8751 | 97 | void progressCharacteristicDiscovery(void); |
rgrover1 | 251:d4e0cf5e8751 | 98 | void progressServiceDiscovery(void); |
rgrover1 | 239:693a1f145b5a | 99 | |
rgrover1 | 248:71ef03789dd3 | 100 | private: |
rgrover1 | 248:71ef03789dd3 | 101 | uint8_t serviceIndex; /**< Index of the current service being discovered. This is intended for internal use during service discovery.*/ |
rgrover1 | 248:71ef03789dd3 | 102 | uint8_t numServices; /**< Number of services at the peers GATT database.*/ |
rgrover1 | 248:71ef03789dd3 | 103 | uint8_t characteristicIndex; /**< Index of the current characteristic being discovered. This is intended for internal use during service discovery.*/ |
rgrover1 | 248:71ef03789dd3 | 104 | uint8_t numCharacteristics; /**< Number of characteristics within the service.*/ |
rgrover1 | 248:71ef03789dd3 | 105 | |
rgrover1 | 248:71ef03789dd3 | 106 | bool sDiscoveryActive; |
rgrover1 | 248:71ef03789dd3 | 107 | bool cDiscoveryActive; |
rgrover1 | 248:71ef03789dd3 | 108 | |
rgrover1 | 248:71ef03789dd3 | 109 | DiscoveredService services[BLE_DB_DISCOVERY_MAX_SRV]; /**< Information related to the current service being discovered. |
rgrover1 | 248:71ef03789dd3 | 110 | * This is intended for internal use during service discovery. */ |
rgrover1 | 248:71ef03789dd3 | 111 | DiscoveredCharacteristic characteristics[BLE_DB_DISCOVERY_MAX_CHAR_PER_SRV]; |
rgrover1 | 280:cbaa4cb83548 | 112 | |
rgrover1 | 280:cbaa4cb83548 | 113 | TerminationCallback_t onTerminationCallback; |
rgrover1 | 240:75b69581d1dd | 114 | }; |
rgrover1 | 240:75b69581d1dd | 115 | |
rgrover1 | 239:693a1f145b5a | 116 | #endif /*_BTLE_DISCOVERY_H_*/ |