test

Dependencies:   nrf51-sdk

Dependents:   microbit-dal

Fork of nRF51822 by Lancaster University

Committer:
rgrover1
Date:
Fri Jun 19 15:55:23 2015 +0100
Revision:
253:f30c1233e9db
Parent:
252:4b3f294415f5
Child:
256:2f2e9f1d6275
Synchronized with git rev 4ece950d
Author: Rohit Grover
updated the definition of service/Characteristic callbacks

Who changed what in which revision?

UserRevisionLine numberNew 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 <stdio.h>
rgrover1 247:df37e7bb3f71 21 #include "ble.h"
rgrover1 239:693a1f145b5a 22 #include "UUID.h"
rgrover1 239:693a1f145b5a 23 #include "Gap.h"
rgrover1 247:df37e7bb3f71 24
rgrover1 247:df37e7bb3f71 25 void bleGattcEventHandler(const ble_evt_t *p_ble_evt);
rgrover1 239:693a1f145b5a 26
rgrover1 239:693a1f145b5a 27 /**@brief Structure for holding information about the service and the characteristics found during
rgrover1 239:693a1f145b5a 28 * the discovery process.
rgrover1 239:693a1f145b5a 29 */
rgrover1 239:693a1f145b5a 30 struct DiscoveredService {
rgrover1 239:693a1f145b5a 31 void setup(ShortUUIDBytes_t uuidIn, Gap::Handle_t start, Gap::Handle_t end) {
rgrover1 239:693a1f145b5a 32 uuid = uuidIn;
rgrover1 239:693a1f145b5a 33 startHandle = start;
rgrover1 239:693a1f145b5a 34 endHandle = end;
rgrover1 239:693a1f145b5a 35 }
rgrover1 239:693a1f145b5a 36
rgrover1 252:4b3f294415f5 37 ShortUUIDBytes_t uuid; /**< UUID of the service. */
rgrover1 239:693a1f145b5a 38 Gap::Handle_t startHandle; /**< Service Handle Range. */
rgrover1 239:693a1f145b5a 39 Gap::Handle_t endHandle; /**< Service Handle Range. */
rgrover1 239:693a1f145b5a 40 };
rgrover1 239:693a1f145b5a 41
rgrover1 239:693a1f145b5a 42 /**@brief Structure for holding information about the service and the characteristics found during
rgrover1 239:693a1f145b5a 43 * the discovery process.
rgrover1 239:693a1f145b5a 44 */
rgrover1 239:693a1f145b5a 45 struct DiscoveredCharacteristic {
rgrover1 239:693a1f145b5a 46 struct Properties_t {
rgrover1 239:693a1f145b5a 47 static const uint8_t BROADCAST_PROPERTY_MASK = 0x01;
rgrover1 239:693a1f145b5a 48 static const uint8_t READ_PROPERTY_MASK = 0x02;
rgrover1 239:693a1f145b5a 49 static const uint8_t WRITE_WO_RESPONSE_PROPERTY_MASK = 0x04;
rgrover1 239:693a1f145b5a 50 static const uint8_t WRITE_PROPERTY_MASK = 0x08;
rgrover1 239:693a1f145b5a 51 static const uint8_t NOTIFY_PROPERTY_MASK = 0x10;
rgrover1 239:693a1f145b5a 52 static const uint8_t INDICATE_PROPERTY_MASK = 0x20;
rgrover1 239:693a1f145b5a 53 static const uint8_t AUTH_SIGNED_PROPERTY_MASK = 0x40;
rgrover1 239:693a1f145b5a 54
rgrover1 239:693a1f145b5a 55 Properties_t() : broadcast(0), read(0), write_wo_resp(0), write(0), notify(0), indicate(0), auth_signed_wr(0) {
rgrover1 239:693a1f145b5a 56 /* empty */
rgrover1 239:693a1f145b5a 57 }
rgrover1 239:693a1f145b5a 58
rgrover1 239:693a1f145b5a 59 Properties_t(uint8_t props) :
rgrover1 239:693a1f145b5a 60 broadcast(props & BROADCAST_PROPERTY_MASK),
rgrover1 239:693a1f145b5a 61 read(props & READ_PROPERTY_MASK),
rgrover1 239:693a1f145b5a 62 write_wo_resp(props & WRITE_WO_RESPONSE_PROPERTY_MASK),
rgrover1 239:693a1f145b5a 63 write(props & WRITE_PROPERTY_MASK),
rgrover1 239:693a1f145b5a 64 notify(props & NOTIFY_PROPERTY_MASK),
rgrover1 239:693a1f145b5a 65 indicate(props & INDICATE_PROPERTY_MASK),
rgrover1 239:693a1f145b5a 66 auth_signed_wr(props & AUTH_SIGNED_PROPERTY_MASK) {
rgrover1 239:693a1f145b5a 67 /* empty*/
rgrover1 239:693a1f145b5a 68 }
rgrover1 239:693a1f145b5a 69
rgrover1 239:693a1f145b5a 70 uint8_t broadcast :1; /**< Broadcasting of the value permitted. */
rgrover1 239:693a1f145b5a 71 uint8_t read :1; /**< Reading the value permitted. */
rgrover1 239:693a1f145b5a 72 uint8_t write_wo_resp :1; /**< Writing the value with Write Command permitted. */
rgrover1 239:693a1f145b5a 73 uint8_t write :1; /**< Writing the value with Write Request permitted. */
rgrover1 239:693a1f145b5a 74 uint8_t notify :1; /**< Notications of the value permitted. */
rgrover1 239:693a1f145b5a 75 uint8_t indicate :1; /**< Indications of the value permitted. */
rgrover1 239:693a1f145b5a 76 uint8_t auth_signed_wr :1; /**< Writing the value with Signed Write Command permitted. */
rgrover1 239:693a1f145b5a 77 };
rgrover1 239:693a1f145b5a 78
rgrover1 239:693a1f145b5a 79 void setup(ShortUUIDBytes_t uuidIn, Properties_t propsIn, Gap::Handle_t declHandleIn, Gap::Handle_t valueHandleIn) {
rgrover1 239:693a1f145b5a 80 uuid = uuidIn;
rgrover1 239:693a1f145b5a 81 props = propsIn;
rgrover1 239:693a1f145b5a 82 declHandle = declHandleIn;
rgrover1 239:693a1f145b5a 83 valueHandle = valueHandleIn;
rgrover1 239:693a1f145b5a 84 }
rgrover1 239:693a1f145b5a 85
rgrover1 239:693a1f145b5a 86 ShortUUIDBytes_t uuid;
rgrover1 239:693a1f145b5a 87 Properties_t props;
rgrover1 239:693a1f145b5a 88 Gap::Handle_t declHandle;
rgrover1 239:693a1f145b5a 89 Gap::Handle_t valueHandle;
rgrover1 239:693a1f145b5a 90 };
rgrover1 239:693a1f145b5a 91
rgrover1 239:693a1f145b5a 92 class ServiceDiscovery {
rgrover1 239:693a1f145b5a 93 public:
rgrover1 253:f30c1233e9db 94 typedef void (*ServiceCallback_t)(const DiscoveredService &);
rgrover1 253:f30c1233e9db 95 typedef void (*CharacteristicCallback_t)(const DiscoveredCharacteristic &);
rgrover1 239:693a1f145b5a 96
rgrover1 239:693a1f145b5a 97 public:
rgrover1 239:693a1f145b5a 98 static ble_error_t launch(Gap::Handle_t connectionHandle,
rgrover1 239:693a1f145b5a 99 ServiceCallback_t sc,
rgrover1 239:693a1f145b5a 100 CharacteristicCallback_t cc = NULL);
rgrover1 239:693a1f145b5a 101 static ble_error_t launch(Gap::Handle_t connectionHandle,
rgrover1 239:693a1f145b5a 102 UUID matchingServiceUUID,
rgrover1 239:693a1f145b5a 103 ServiceCallback_t sc,
rgrover1 239:693a1f145b5a 104 UUID matchingCharacteristicUUID = ShortUUIDBytes_t(BLE_UUID_UNKNOWN),
rgrover1 239:693a1f145b5a 105 CharacteristicCallback_t cc = NULL);
rgrover1 239:693a1f145b5a 106
rgrover1 248:71ef03789dd3 107 static void terminate(void);
rgrover1 248:71ef03789dd3 108
rgrover1 248:71ef03789dd3 109 protected:
rgrover1 248:71ef03789dd3 110 Gap::Handle_t connHandle; /**< Connection handle as provided by the SoftDevice. */
rgrover1 248:71ef03789dd3 111 ServiceCallback_t serviceCallback;
rgrover1 248:71ef03789dd3 112 CharacteristicCallback_t characteristicCallback;
rgrover1 248:71ef03789dd3 113 };
rgrover1 248:71ef03789dd3 114
rgrover1 250:db3c51656388 115 class NordicServiceDiscovery : public ServiceDiscovery
rgrover1 250:db3c51656388 116 {
rgrover1 248:71ef03789dd3 117 public:
rgrover1 248:71ef03789dd3 118 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 119 static const unsigned BLE_DB_DISCOVERY_MAX_CHAR_PER_SRV = 4; /**< Maximum number of characteristics per service we can retain information for. */
rgrover1 248:71ef03789dd3 120 static const uint16_t SRV_DISC_START_HANDLE = 0x0001; /**< The start handle value used during service discovery. */
rgrover1 239:693a1f145b5a 121
rgrover1 239:693a1f145b5a 122 public:
rgrover1 248:71ef03789dd3 123 void setupDiscoveredServices(const ble_gattc_evt_prim_srvc_disc_rsp_t *response);
rgrover1 248:71ef03789dd3 124 void setupDiscoveredCharacteristics(const ble_gattc_evt_char_disc_rsp_t *response);
rgrover1 248:71ef03789dd3 125
rgrover1 248:71ef03789dd3 126 void terminateServiceDiscovery(void) {
rgrover1 242:73fc02cc20b1 127 sDiscoveryActive = false;
rgrover1 248:71ef03789dd3 128 cDiscoveryActive = false;
rgrover1 239:693a1f145b5a 129 printf("end of service discovery\r\n");
rgrover1 239:693a1f145b5a 130 }
rgrover1 239:693a1f145b5a 131
rgrover1 245:3abc61d38db3 132 void terminateCharacteristicDiscovery(void) {
rgrover1 245:3abc61d38db3 133 cDiscoveryActive = false;
rgrover1 245:3abc61d38db3 134 sDiscoveryActive = true;
rgrover1 245:3abc61d38db3 135 serviceIndex++; /* Progress service index to keep discovery alive. */
rgrover1 245:3abc61d38db3 136 }
rgrover1 245:3abc61d38db3 137
rgrover1 239:693a1f145b5a 138 void resetDiscoveredServices(void) {
rgrover1 242:73fc02cc20b1 139 numServices = 0;
rgrover1 242:73fc02cc20b1 140 serviceIndex = 0;
rgrover1 239:693a1f145b5a 141 memset(services, 0, sizeof(DiscoveredService) * BLE_DB_DISCOVERY_MAX_SRV);
rgrover1 239:693a1f145b5a 142 }
rgrover1 239:693a1f145b5a 143
rgrover1 245:3abc61d38db3 144 protected:
rgrover1 245:3abc61d38db3 145 void resetDiscoveredCharacteristics(void) {
rgrover1 245:3abc61d38db3 146 numCharacteristics = 0;
rgrover1 245:3abc61d38db3 147 characteristicIndex = 0;
rgrover1 245:3abc61d38db3 148 memset(characteristics, 0, sizeof(DiscoveredCharacteristic) * BLE_DB_DISCOVERY_MAX_CHAR_PER_SRV);
rgrover1 245:3abc61d38db3 149 }
rgrover1 245:3abc61d38db3 150
rgrover1 245:3abc61d38db3 151 public:
rgrover1 243:a966506d1e5b 152 void serviceDiscoveryStarted(Gap::Handle_t connectionHandle) {
rgrover1 243:a966506d1e5b 153 connHandle = connectionHandle;
rgrover1 243:a966506d1e5b 154 resetDiscoveredServices();
rgrover1 243:a966506d1e5b 155 sDiscoveryActive = true;
rgrover1 243:a966506d1e5b 156 cDiscoveryActive = false;
rgrover1 243:a966506d1e5b 157 }
rgrover1 243:a966506d1e5b 158
rgrover1 243:a966506d1e5b 159 protected:
rgrover1 245:3abc61d38db3 160 void characteristicDiscoveryStarted(Gap::Handle_t connectionHandle) {
rgrover1 245:3abc61d38db3 161 connHandle = connectionHandle;
rgrover1 245:3abc61d38db3 162 resetDiscoveredCharacteristics();
rgrover1 245:3abc61d38db3 163 cDiscoveryActive = true;
rgrover1 245:3abc61d38db3 164 sDiscoveryActive = false;
rgrover1 245:3abc61d38db3 165 }
rgrover1 245:3abc61d38db3 166
rgrover1 244:57c98fe71376 167 public:
rgrover1 244:57c98fe71376 168 ble_error_t launchCharacteristicDiscovery(Gap::Handle_t connectionHandle, Gap::Handle_t startHandle, Gap::Handle_t endHandle);
rgrover1 244:57c98fe71376 169
rgrover1 251:d4e0cf5e8751 170 private:
rgrover1 251:d4e0cf5e8751 171 friend void bleGattcEventHandler(const ble_evt_t *p_ble_evt);
rgrover1 251:d4e0cf5e8751 172 void progressCharacteristicDiscovery(void);
rgrover1 251:d4e0cf5e8751 173 void progressServiceDiscovery(void);
rgrover1 239:693a1f145b5a 174
rgrover1 248:71ef03789dd3 175 private:
rgrover1 248:71ef03789dd3 176 uint8_t serviceIndex; /**< Index of the current service being discovered. This is intended for internal use during service discovery.*/
rgrover1 248:71ef03789dd3 177 uint8_t numServices; /**< Number of services at the peers GATT database.*/
rgrover1 248:71ef03789dd3 178 uint8_t characteristicIndex; /**< Index of the current characteristic being discovered. This is intended for internal use during service discovery.*/
rgrover1 248:71ef03789dd3 179 uint8_t numCharacteristics; /**< Number of characteristics within the service.*/
rgrover1 248:71ef03789dd3 180
rgrover1 248:71ef03789dd3 181 bool sDiscoveryActive;
rgrover1 248:71ef03789dd3 182 bool cDiscoveryActive;
rgrover1 248:71ef03789dd3 183
rgrover1 248:71ef03789dd3 184 DiscoveredService services[BLE_DB_DISCOVERY_MAX_SRV]; /**< Information related to the current service being discovered.
rgrover1 248:71ef03789dd3 185 * This is intended for internal use during service discovery. */
rgrover1 248:71ef03789dd3 186 DiscoveredCharacteristic characteristics[BLE_DB_DISCOVERY_MAX_CHAR_PER_SRV];
rgrover1 240:75b69581d1dd 187 };
rgrover1 240:75b69581d1dd 188
rgrover1 239:693a1f145b5a 189 #endif /*_BTLE_DISCOVERY_H_*/