Microbug / BLE_API

Fork of BLE_API by Bluetooth Low Energy

Committer:
Rohit Grover
Date:
Fri Jul 04 11:27:20 2014 +0100
Revision:
100:1e80e8032c13
Parent:
99:58c47085e816
Child:
102:39e30ac179a7
accumulateAdvertisingPayload(flags) should allow for combinations of flags

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ktownsend 29:011e95ce78b8 1 /* mbed Microcontroller Library
ktownsend 29:011e95ce78b8 2 * Copyright (c) 2006-2013 ARM Limited
ktownsend 29:011e95ce78b8 3 *
ktownsend 29:011e95ce78b8 4 * Licensed under the Apache License, Version 2.0 (the "License");
ktownsend 29:011e95ce78b8 5 * you may not use this file except in compliance with the License.
ktownsend 29:011e95ce78b8 6 * You may obtain a copy of the License at
ktownsend 29:011e95ce78b8 7 *
ktownsend 29:011e95ce78b8 8 * http://www.apache.org/licenses/LICENSE-2.0
ktownsend 29:011e95ce78b8 9 *
ktownsend 29:011e95ce78b8 10 * Unless required by applicable law or agreed to in writing, software
ktownsend 29:011e95ce78b8 11 * distributed under the License is distributed on an "AS IS" BASIS,
ktownsend 29:011e95ce78b8 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
ktownsend 29:011e95ce78b8 13 * See the License for the specific language governing permissions and
ktownsend 29:011e95ce78b8 14 * limitations under the License.
ktownsend 29:011e95ce78b8 15 */
Rohit Grover 34:da2ea8cd6216 16
Rohit Grover 81:fd8c6792281b 17 #ifndef __BLE_DEVICE__
Rohit Grover 81:fd8c6792281b 18 #define __BLE_DEVICE__
ktownsend 29:011e95ce78b8 19
ktownsend 29:011e95ce78b8 20 #include "mbed.h"
ktownsend 29:011e95ce78b8 21 #include "blecommon.h"
ktownsend 29:011e95ce78b8 22 #include "hw/Gap.h"
ktownsend 29:011e95ce78b8 23 #include "hw/GattServer.h"
ktownsend 29:011e95ce78b8 24
Rohit Grover 81:fd8c6792281b 25 class BLEDeviceInstanceBase; /* forward declaration */
Rohit Grover 41:7ae5c71cd44c 26
Rohit Grover 41:7ae5c71cd44c 27 /**
Rohit Grover 81:fd8c6792281b 28 * BLEDevice uses composition to hide an interface object encapsulating the
Rohit Grover 41:7ae5c71cd44c 29 * backend transport.
Rohit Grover 41:7ae5c71cd44c 30 *
Rohit Grover 41:7ae5c71cd44c 31 * The following API is used to create the singleton interface object. An
Rohit Grover 41:7ae5c71cd44c 32 * implementation for this function must be provided by the device-specific
Rohit Grover 41:7ae5c71cd44c 33 * library, otherwise there will be a linker error.
Rohit Grover 41:7ae5c71cd44c 34 */
Rohit Grover 81:fd8c6792281b 35 extern BLEDeviceInstanceBase *createBLEDeviceInstance(void);
Rohit Grover 41:7ae5c71cd44c 36
Rohit Grover 41:7ae5c71cd44c 37 /**
Rohit Grover 41:7ae5c71cd44c 38 * The base class used to abstract away BLE capable radio transceivers or SOCs,
Rohit Grover 41:7ae5c71cd44c 39 * to enable this BLE API to work with any radio transparently.
Rohit Grover 41:7ae5c71cd44c 40 */
Rohit Grover 81:fd8c6792281b 41 class BLEDevice
ktownsend 29:011e95ce78b8 42 {
Rohit Grover 34:da2ea8cd6216 43 public:
Rohit Grover 95:5d2102351bf4 44 /**
Rohit Grover 95:5d2102351bf4 45 * Initialize the BLE controller. This should be called before using
Rohit Grover 95:5d2102351bf4 46 * anything else in the BLE_API.
Rohit Grover 95:5d2102351bf4 47 */
Rohit Grover 41:7ae5c71cd44c 48 ble_error_t init();
Rohit Grover 41:7ae5c71cd44c 49 ble_error_t reset(void);
Rohit Grover 35:f3b0c1192cf7 50
Rohit Grover 35:f3b0c1192cf7 51 /* GAP specific APIs */
Rohit Grover 35:f3b0c1192cf7 52 public:
Rohit Grover 37:7e8de07642e0 53 /**
Rohit Grover 37:7e8de07642e0 54 * Set the BTLE MAC address and type.
Rohit Grover 37:7e8de07642e0 55 * @return
Rohit Grover 37:7e8de07642e0 56 */
Rohit Grover 38:02cf26a2a4db 57 ble_error_t setAddress(Gap::addr_type_t type, const uint8_t address[6]);
Rohit Grover 35:f3b0c1192cf7 58
Rohit Grover 46:33d87f468a0d 59 /**
Rohit Grover 46:33d87f468a0d 60 * @param[in] advType
Rohit Grover 46:33d87f468a0d 61 * The GAP advertising mode to use for this device. Valid
Rohit Grover 46:33d87f468a0d 62 * values are defined in AdvertisingType:
Rohit Grover 46:33d87f468a0d 63 *
Rohit Grover 46:33d87f468a0d 64 * \par ADV_NON_CONNECTABLE_UNDIRECTED
Rohit Grover 46:33d87f468a0d 65 * All connections to the peripheral device will be refused.
Rohit Grover 46:33d87f468a0d 66 *
Rohit Grover 46:33d87f468a0d 67 * \par ADV_CONNECTABLE_DIRECTED
Rohit Grover 46:33d87f468a0d 68 * Only connections from a pre-defined central device will be
Rohit Grover 46:33d87f468a0d 69 * accepted.
Rohit Grover 46:33d87f468a0d 70 *
Rohit Grover 46:33d87f468a0d 71 * \par ADV_CONNECTABLE_UNDIRECTED
Rohit Grover 46:33d87f468a0d 72 * Any central device can connect to this peripheral.
Rohit Grover 46:33d87f468a0d 73 *
Rohit Grover 46:33d87f468a0d 74 * \par ADV_SCANNABLE_UNDIRECTED
Rohit Grover 46:33d87f468a0d 75 * Any central device can connect to this peripheral, and
Rohit Grover 46:33d87f468a0d 76 * the secondary Scan Response payload will be included or
Rohit Grover 46:33d87f468a0d 77 * available to central devices.
Rohit Grover 46:33d87f468a0d 78 *
Rohit Grover 46:33d87f468a0d 79 * \par
Rohit Grover 46:33d87f468a0d 80 * See Bluetooth Core Specification 4.0 (Vol. 3), Part C,
Rohit Grover 46:33d87f468a0d 81 * Section 9.3 and Core Specification 4.0 (Vol. 6), Part B,
Rohit Grover 46:33d87f468a0d 82 * Section 2.3.1 for further information on GAP connection
Rohit Grover 46:33d87f468a0d 83 * modes
Rohit Grover 46:33d87f468a0d 84 */
Rohit Grover 46:33d87f468a0d 85 void setAdvertisingType(GapAdvertisingParams::AdvertisingType);
Rohit Grover 46:33d87f468a0d 86
Rohit Grover 46:33d87f468a0d 87 /**
Rohit Grover 46:33d87f468a0d 88 * @param[in] interval
Rohit Grover 46:33d87f468a0d 89 * Advertising interval between 0x0020 and 0x4000 in 0.625ms
Rohit Grover 46:33d87f468a0d 90 * units (20ms to 10.24s). If using non-connectable mode
Rohit Grover 46:33d87f468a0d 91 * (ADV_NON_CONNECTABLE_UNDIRECTED) this min value is
Rohit Grover 46:33d87f468a0d 92 * 0x00A0 (100ms). To reduce the likelihood of collisions, the
Rohit Grover 46:33d87f468a0d 93 * link layer perturbs this interval by a pseudo-random delay
Rohit Grover 46:33d87f468a0d 94 * with a range of 0 ms to 10 ms for each advertising event.
Rohit Grover 46:33d87f468a0d 95 *
Rohit Grover 46:33d87f468a0d 96 * \par
Rohit Grover 46:33d87f468a0d 97 * Decreasing this value will allow central devices to detect
Rohit Grover 46:33d87f468a0d 98 * your peripheral faster at the expense of more power being
Rohit Grover 46:33d87f468a0d 99 * used by the radio due to the higher data transmit rate.
Rohit Grover 46:33d87f468a0d 100 *
Rohit Grover 46:33d87f468a0d 101 * \par
Rohit Grover 46:33d87f468a0d 102 * This field must be set to 0 if connectionMode is equal
Rohit Grover 46:33d87f468a0d 103 * to ADV_CONNECTABLE_DIRECTED
Rohit Grover 46:33d87f468a0d 104 *
Rohit Grover 46:33d87f468a0d 105 * \par
Rohit Grover 46:33d87f468a0d 106 * See Bluetooth Core Specification, Vol 3., Part C,
Rohit Grover 46:33d87f468a0d 107 * Appendix A for suggested advertising intervals.
Rohit Grover 46:33d87f468a0d 108 */
Rohit Grover 46:33d87f468a0d 109 void setAdvertisingInterval(uint16_t interval);
Rohit Grover 46:33d87f468a0d 110
Rohit Grover 46:33d87f468a0d 111 /**
Rohit Grover 46:33d87f468a0d 112 * @param[in] timeout
Rohit Grover 46:33d87f468a0d 113 * Advertising timeout between 0x1 and 0x3FFF (1 and 16383)
Rohit Grover 46:33d87f468a0d 114 * in seconds. Enter 0 to disable the advertising timeout.
Rohit Grover 46:33d87f468a0d 115 */
Rohit Grover 46:33d87f468a0d 116 void setAdvertisingTimeout(uint16_t timeout);
Rohit Grover 46:33d87f468a0d 117
Rohit Grover 46:33d87f468a0d 118 /**
Rohit Grover 46:33d87f468a0d 119 * Please refer to the APIs above.
Rohit Grover 46:33d87f468a0d 120 */
Rohit Grover 46:33d87f468a0d 121 void setAdvertisingParams(const GapAdvertisingParams &advParams);
Rohit Grover 46:33d87f468a0d 122
Rohit Grover 95:5d2102351bf4 123 /**
Rohit Grover 95:5d2102351bf4 124 * Reset any advertising payload prepared from prior calls to
Rohit Grover 95:5d2102351bf4 125 * accumulateAdvertisingPayload().
Rohit Grover 95:5d2102351bf4 126 */
Rohit Grover 49:03a51f0006f8 127 void clearAdvertisingPayload(void);
Rohit Grover 95:5d2102351bf4 128
Rohit Grover 95:5d2102351bf4 129 /**
Rohit Grover 95:5d2102351bf4 130 * Accumulate an AD structure in the advertising payload. Please note that
Rohit Grover 95:5d2102351bf4 131 * the payload is limited to 31 bytes. The SCAN_RESPONSE message may be used
Rohit Grover 95:5d2102351bf4 132 * as an additional 31 bytes if the advertising payload proves to be too
Rohit Grover 95:5d2102351bf4 133 * small.
Rohit Grover 95:5d2102351bf4 134 *
Rohit Grover 95:5d2102351bf4 135 * @param flags
Rohit Grover 100:1e80e8032c13 136 * The flags to be added. Multiple flags may be specified in
Rohit Grover 100:1e80e8032c13 137 * combination.
Rohit Grover 95:5d2102351bf4 138 */
Rohit Grover 100:1e80e8032c13 139 ble_error_t accumulateAdvertisingPayload(uint8_t flags);
Rohit Grover 95:5d2102351bf4 140
Rohit Grover 95:5d2102351bf4 141 /**
Rohit Grover 95:5d2102351bf4 142 * Accumulate an AD structure in the advertising payload. Please note that
Rohit Grover 95:5d2102351bf4 143 * the payload is limited to 31 bytes. The SCAN_RESPONSE message may be used
Rohit Grover 95:5d2102351bf4 144 * as an additional 31 bytes if the advertising payload proves to be too
Rohit Grover 95:5d2102351bf4 145 * small.
Rohit Grover 95:5d2102351bf4 146 *
Rohit Grover 95:5d2102351bf4 147 * @param app
Rohit Grover 95:5d2102351bf4 148 * The appearance of the peripheral.
Rohit Grover 95:5d2102351bf4 149 */
Rohit Grover 49:03a51f0006f8 150 ble_error_t accumulateAdvertisingPayload(GapAdvertisingData::Appearance app);
Rohit Grover 95:5d2102351bf4 151
Rohit Grover 95:5d2102351bf4 152 /**
Rohit Grover 95:5d2102351bf4 153 * Accumulate an AD structure in the advertising payload. Please note that
Rohit Grover 95:5d2102351bf4 154 * the payload is limited to 31 bytes. The SCAN_RESPONSE message may be used
Rohit Grover 95:5d2102351bf4 155 * as an additional 31 bytes if the advertising payload proves to be too
Rohit Grover 95:5d2102351bf4 156 * small.
Rohit Grover 95:5d2102351bf4 157 *
Rohit Grover 95:5d2102351bf4 158 * @param app
Rohit Grover 95:5d2102351bf4 159 * The max transmit power to be used by the controller. This is
Rohit Grover 95:5d2102351bf4 160 * only a hint.
Rohit Grover 95:5d2102351bf4 161 */
Rohit Grover 49:03a51f0006f8 162 ble_error_t accumulateAdvertisingPayloadTxPower(int8_t power);
Rohit Grover 95:5d2102351bf4 163
Rohit Grover 95:5d2102351bf4 164 /**
Rohit Grover 95:5d2102351bf4 165 * @param app
Rohit Grover 95:5d2102351bf4 166 * The appearance of the peripheral.
Rohit Grover 95:5d2102351bf4 167 */
Rohit Grover 95:5d2102351bf4 168 /**
Rohit Grover 95:5d2102351bf4 169 * Accumulate a variable length byte-stream as an AD structure in the
Rohit Grover 95:5d2102351bf4 170 * advertising payload. Please note that the payload is limited to 31 bytes.
Rohit Grover 95:5d2102351bf4 171 * The SCAN_RESPONSE message may be used as an additional 31 bytes if the
Rohit Grover 95:5d2102351bf4 172 * advertising payload proves to be too small.
Rohit Grover 95:5d2102351bf4 173 *
Rohit Grover 95:5d2102351bf4 174 * @param type The type which describes the variable length data.
Rohit Grover 95:5d2102351bf4 175 * @param data data bytes.
Rohit Grover 95:5d2102351bf4 176 * @param len length of data.
Rohit Grover 95:5d2102351bf4 177 */
Rohit Grover 74:301042c34cb8 178 ble_error_t accumulateAdvertisingPayload(GapAdvertisingData::DataType type, const uint8_t *data, uint8_t len);
Rohit Grover 49:03a51f0006f8 179
Rohit Grover 95:5d2102351bf4 180 /**
Rohit Grover 95:5d2102351bf4 181 * Start advertising (GAP Discoverable, Connectable modes, Broadcast
Rohit Grover 95:5d2102351bf4 182 * Procedure).
Rohit Grover 95:5d2102351bf4 183 */
Rohit Grover 46:33d87f468a0d 184 ble_error_t startAdvertising(void);
Rohit Grover 95:5d2102351bf4 185
Rohit Grover 95:5d2102351bf4 186 /**
Rohit Grover 95:5d2102351bf4 187 * Stop advertising (GAP Discoverable, Connectable modes, Broadcast
Rohit Grover 95:5d2102351bf4 188 * Procedure).
Rohit Grover 95:5d2102351bf4 189 */
Rohit Grover 36:9ec94579bb78 190 ble_error_t stopAdvertising(void);
Rohit Grover 46:33d87f468a0d 191
Rohit Grover 36:9ec94579bb78 192 ble_error_t disconnect(void);
Rohit Grover 35:f3b0c1192cf7 193
Rohit Grover 64:95529f47b782 194 /* APIs to set GAP callbacks. */
Rohit Grover 63:653378e782ea 195 void onTimeout(Gap::EventCallback_t timeoutCallback);
Rohit Grover 63:653378e782ea 196 void onConnection(Gap::EventCallback_t connectionCallback);
Rohit Grover 95:5d2102351bf4 197 /**
Rohit Grover 95:5d2102351bf4 198 * Used to setup a callback for GAP disconnection.
Rohit Grover 95:5d2102351bf4 199 */
Rohit Grover 63:653378e782ea 200 void onDisconnection(Gap::EventCallback_t disconnectionCallback);
Rohit Grover 63:653378e782ea 201
Rohit Grover 95:5d2102351bf4 202 /**
Rohit Grover 95:5d2102351bf4 203 * Setup a callback for the GATT event DATA_SENT.
Rohit Grover 95:5d2102351bf4 204 */
Rohit Grover 99:58c47085e816 205 void onDataSent(GattServer::ServerEventCallback_t callback);
Rohit Grover 95:5d2102351bf4 206
Rohit Grover 95:5d2102351bf4 207 /**
Rohit Grover 95:5d2102351bf4 208 * Setup a callback for when a characteristic has its value updated by a
Rohit Grover 95:5d2102351bf4 209 * client.
Rohit Grover 95:5d2102351bf4 210 */
Rohit Grover 64:95529f47b782 211 void onDataWritten(GattServer::EventCallback_t callback);
Rohit Grover 64:95529f47b782 212 void onUpdatesEnabled(GattServer::EventCallback_t callback);
Rohit Grover 64:95529f47b782 213 void onUpdatesDisabled(GattServer::EventCallback_t callback);
Rohit Grover 64:95529f47b782 214 void onConfirmationReceived(GattServer::EventCallback_t callback);
Rohit Grover 64:95529f47b782 215
Rohit Grover 95:5d2102351bf4 216 /**
Rohit Grover 95:5d2102351bf4 217 * Add a service declaration to the local server ATT table. Also add the
Rohit Grover 95:5d2102351bf4 218 * characteristics contained within.
Rohit Grover 95:5d2102351bf4 219 */
Rohit Grover 72:bd485a3b18c5 220 ble_error_t addService(GattService &service);
Rohit Grover 72:bd485a3b18c5 221
Rohit Grover 73:eeb1ac3545e9 222 Gap::GapState_t getGapState(void) const;
Rohit Grover 94:455363a6eb90 223
Rohit Grover 94:455363a6eb90 224 ble_error_t readCharacteristicValue(uint16_t handle, uint8_t *const buffer, uint16_t *const lengthP);
Rohit Grover 94:455363a6eb90 225 ble_error_t updateCharacteristicValue(uint16_t handle, const uint8_t* value, uint16_t size, bool localOnly = false);
Rohit Grover 73:eeb1ac3545e9 226
Rohit Grover 78:5d356ef94347 227 /**
Rohit Grover 79:b6284f5928bb 228 * Yield control to the BLE stack or to other tasks waiting for events. This
Rohit Grover 79:b6284f5928bb 229 * is a sleep function which will return when there is an application
Rohit Grover 79:b6284f5928bb 230 * specific interrupt, but the MCU might wake up several times before
Rohit Grover 80:e2c50c4a1e9a 231 * returning (to service the stack). This is not always interchangeable with
Rohit Grover 80:e2c50c4a1e9a 232 * WFE().
Rohit Grover 78:5d356ef94347 233 */
Rohit Grover 78:5d356ef94347 234 void waitForEvent(void);
Rohit Grover 78:5d356ef94347 235
Rohit Grover 41:7ae5c71cd44c 236 public:
Rohit Grover 98:d751acb2543f 237 BLEDevice() : transport(createBLEDeviceInstance()), advParams(), advPayload(), scanResponse(), needToSetAdvPayload(true) {
Rohit Grover 49:03a51f0006f8 238 advPayload.clear();
Rohit Grover 49:03a51f0006f8 239 scanResponse.clear();
Rohit Grover 41:7ae5c71cd44c 240 }
Rohit Grover 41:7ae5c71cd44c 241
Rohit Grover 41:7ae5c71cd44c 242 private:
Rohit Grover 81:fd8c6792281b 243 BLEDeviceInstanceBase *const transport; /* the device specific backend */
Rohit Grover 46:33d87f468a0d 244
Rohit Grover 49:03a51f0006f8 245 GapAdvertisingParams advParams;
Rohit Grover 49:03a51f0006f8 246 GapAdvertisingData advPayload;
Rohit Grover 49:03a51f0006f8 247 GapAdvertisingData scanResponse;
Rohit Grover 75:d08bdef22500 248
Rohit Grover 75:d08bdef22500 249 /* Accumulation of AD structures in the advertisement payload should
Rohit Grover 75:d08bdef22500 250 * eventually result in a call to the target's setAdvertisingData() before
Rohit Grover 95:5d2102351bf4 251 * the server begins advertising. This flag marks the status of the pending update.*/
Rohit Grover 98:d751acb2543f 252 bool needToSetAdvPayload;
Rohit Grover 98:d751acb2543f 253
Rohit Grover 98:d751acb2543f 254 public:
Rohit Grover 98:d751acb2543f 255 /**
Rohit Grover 98:d751acb2543f 256 * This API is typically used as an internal helper to udpate the transport
Rohit Grover 98:d751acb2543f 257 * backend with advertising data before starting to advertise. It may also
Rohit Grover 98:d751acb2543f 258 * be explicity used to dynamically reset the accumulated advertising
Rohit Grover 98:d751acb2543f 259 * payload; to do this, the application can clear and re-accumulate a new
Rohit Grover 98:d751acb2543f 260 * advertising payload before using this API.
Rohit Grover 98:d751acb2543f 261 */
Rohit Grover 98:d751acb2543f 262 ble_error_t setAdvertisingPayload(void);
Rohit Grover 46:33d87f468a0d 263
Rohit Grover 46:33d87f468a0d 264 /**
Rohit Grover 46:33d87f468a0d 265 * DEPRECATED
Rohit Grover 46:33d87f468a0d 266 */
Rohit Grover 46:33d87f468a0d 267 public:
Rohit Grover 75:d08bdef22500 268 ble_error_t setAdvertisingData(const GapAdvertisingData &ADStructures, const GapAdvertisingData &scanResponse);
Rohit Grover 48:ec0c28e62abf 269 ble_error_t setAdvertisingData(const GapAdvertisingData &ADStructures);
Rohit Grover 48:ec0c28e62abf 270
Rohit Grover 46:33d87f468a0d 271 ble_error_t startAdvertising(const GapAdvertisingParams &advParams);
Rohit Grover 41:7ae5c71cd44c 272 };
Rohit Grover 41:7ae5c71cd44c 273
Rohit Grover 41:7ae5c71cd44c 274 /**
Rohit Grover 41:7ae5c71cd44c 275 * The interface for the transport object to be created by the target library's
Rohit Grover 81:fd8c6792281b 276 * createBLEDeviceInstance().
Rohit Grover 41:7ae5c71cd44c 277 */
Rohit Grover 81:fd8c6792281b 278 class BLEDeviceInstanceBase
Rohit Grover 41:7ae5c71cd44c 279 {
Rohit Grover 41:7ae5c71cd44c 280 public:
Rohit Grover 78:5d356ef94347 281 virtual Gap& getGap() = 0;
Rohit Grover 78:5d356ef94347 282 virtual GattServer& getGattServer() = 0;
Rohit Grover 78:5d356ef94347 283 virtual ble_error_t init(void) = 0;
Rohit Grover 78:5d356ef94347 284 virtual ble_error_t reset(void) = 0;
Rohit Grover 78:5d356ef94347 285 virtual void waitForEvent(void) = 0;
ktownsend 29:011e95ce78b8 286 };
ktownsend 29:011e95ce78b8 287
Rohit Grover 41:7ae5c71cd44c 288
Rohit Grover 81:fd8c6792281b 289 /* BLEDevice methods. Most of these simply forward the calls to the underlying
Rohit Grover 41:7ae5c71cd44c 290 * transport.*/
Rohit Grover 41:7ae5c71cd44c 291
Rohit Grover 41:7ae5c71cd44c 292 inline ble_error_t
Rohit Grover 81:fd8c6792281b 293 BLEDevice::init()
Rohit Grover 77:1436ecf09583 294 {
Rohit Grover 41:7ae5c71cd44c 295 return transport->init();
Rohit Grover 41:7ae5c71cd44c 296 }
Rohit Grover 41:7ae5c71cd44c 297
Rohit Grover 41:7ae5c71cd44c 298 inline ble_error_t
Rohit Grover 81:fd8c6792281b 299 BLEDevice::reset(void)
Rohit Grover 77:1436ecf09583 300 {
Rohit Grover 41:7ae5c71cd44c 301 return transport->reset();
Rohit Grover 41:7ae5c71cd44c 302 }
Rohit Grover 41:7ae5c71cd44c 303
Rohit Grover 39:a57137537521 304 inline ble_error_t
Rohit Grover 81:fd8c6792281b 305 BLEDevice::setAddress(Gap::addr_type_t type, const uint8_t address[6])
Rohit Grover 77:1436ecf09583 306 {
Rohit Grover 41:7ae5c71cd44c 307 return transport->getGap().setAddress(type, address);
Rohit Grover 36:9ec94579bb78 308 }
Rohit Grover 36:9ec94579bb78 309
Rohit Grover 46:33d87f468a0d 310 inline void
Rohit Grover 81:fd8c6792281b 311 BLEDevice::setAdvertisingType(GapAdvertisingParams::AdvertisingType advType)
Rohit Grover 77:1436ecf09583 312 {
Rohit Grover 46:33d87f468a0d 313 advParams.setAdvertisingType(advType);
Rohit Grover 46:33d87f468a0d 314 }
Rohit Grover 46:33d87f468a0d 315
Rohit Grover 46:33d87f468a0d 316 inline void
Rohit Grover 81:fd8c6792281b 317 BLEDevice::setAdvertisingInterval(uint16_t interval)
Rohit Grover 77:1436ecf09583 318 {
Rohit Grover 46:33d87f468a0d 319 advParams.setInterval(interval);
Rohit Grover 46:33d87f468a0d 320 }
Rohit Grover 46:33d87f468a0d 321
Rohit Grover 46:33d87f468a0d 322 inline void
Rohit Grover 81:fd8c6792281b 323 BLEDevice::setAdvertisingTimeout(uint16_t timeout)
Rohit Grover 77:1436ecf09583 324 {
Rohit Grover 46:33d87f468a0d 325 advParams.setTimeout(timeout);
Rohit Grover 46:33d87f468a0d 326 }
Rohit Grover 46:33d87f468a0d 327
Rohit Grover 46:33d87f468a0d 328 inline void
Rohit Grover 81:fd8c6792281b 329 BLEDevice::setAdvertisingParams(const GapAdvertisingParams &newAdvParams)
Rohit Grover 77:1436ecf09583 330 {
Rohit Grover 46:33d87f468a0d 331 advParams = newAdvParams;
Rohit Grover 46:33d87f468a0d 332 }
Rohit Grover 46:33d87f468a0d 333
Rohit Grover 49:03a51f0006f8 334 inline void
Rohit Grover 81:fd8c6792281b 335 BLEDevice::clearAdvertisingPayload(void)
Rohit Grover 77:1436ecf09583 336 {
Rohit Grover 98:d751acb2543f 337 needToSetAdvPayload = true;
Rohit Grover 49:03a51f0006f8 338 advPayload.clear();
Rohit Grover 49:03a51f0006f8 339 }
Rohit Grover 49:03a51f0006f8 340
Rohit Grover 49:03a51f0006f8 341 inline ble_error_t
Rohit Grover 100:1e80e8032c13 342 BLEDevice::accumulateAdvertisingPayload(uint8_t flags)
Rohit Grover 77:1436ecf09583 343 {
Rohit Grover 98:d751acb2543f 344 needToSetAdvPayload = true;
Rohit Grover 49:03a51f0006f8 345 return advPayload.addFlags(flags);
Rohit Grover 49:03a51f0006f8 346 }
Rohit Grover 49:03a51f0006f8 347
Rohit Grover 49:03a51f0006f8 348 inline ble_error_t
Rohit Grover 81:fd8c6792281b 349 BLEDevice::accumulateAdvertisingPayload(GapAdvertisingData::Appearance app)
Rohit Grover 77:1436ecf09583 350 {
Rohit Grover 98:d751acb2543f 351 needToSetAdvPayload = true;
Rohit Grover 49:03a51f0006f8 352 return advPayload.addAppearance(app);
Rohit Grover 49:03a51f0006f8 353 }
Rohit Grover 49:03a51f0006f8 354
Rohit Grover 49:03a51f0006f8 355 inline ble_error_t
Rohit Grover 81:fd8c6792281b 356 BLEDevice::accumulateAdvertisingPayloadTxPower(int8_t txPower)
Rohit Grover 77:1436ecf09583 357 {
Rohit Grover 98:d751acb2543f 358 needToSetAdvPayload = true;
Rohit Grover 49:03a51f0006f8 359 return advPayload.addTxPower(txPower);
Rohit Grover 49:03a51f0006f8 360 }
Rohit Grover 49:03a51f0006f8 361
Rohit Grover 49:03a51f0006f8 362 inline ble_error_t
Rohit Grover 81:fd8c6792281b 363 BLEDevice::accumulateAdvertisingPayload(GapAdvertisingData::DataType type, const uint8_t *data, uint8_t len)
Rohit Grover 77:1436ecf09583 364 {
Rohit Grover 98:d751acb2543f 365 needToSetAdvPayload = true;
Rohit Grover 49:03a51f0006f8 366 return advPayload.addData(type, data, len);
Rohit Grover 49:03a51f0006f8 367 }
Rohit Grover 49:03a51f0006f8 368
Rohit Grover 39:a57137537521 369 inline ble_error_t
Rohit Grover 98:d751acb2543f 370 BLEDevice::setAdvertisingPayload(void) {
Rohit Grover 98:d751acb2543f 371 needToSetAdvPayload = false;
Rohit Grover 98:d751acb2543f 372 return transport->getGap().setAdvertisingData(advPayload, scanResponse);
Rohit Grover 98:d751acb2543f 373 }
Rohit Grover 98:d751acb2543f 374
Rohit Grover 98:d751acb2543f 375 inline ble_error_t
Rohit Grover 81:fd8c6792281b 376 BLEDevice::startAdvertising(void)
Rohit Grover 77:1436ecf09583 377 {
Rohit Grover 98:d751acb2543f 378 if (needToSetAdvPayload) {
Rohit Grover 98:d751acb2543f 379 ble_error_t rc;
Rohit Grover 98:d751acb2543f 380 if ((rc = setAdvertisingPayload()) != BLE_ERROR_NONE) {
Rohit Grover 98:d751acb2543f 381 return rc;
Rohit Grover 98:d751acb2543f 382 }
Rohit Grover 49:03a51f0006f8 383 }
Rohit Grover 49:03a51f0006f8 384
Rohit Grover 41:7ae5c71cd44c 385 return transport->getGap().startAdvertising(advParams);
Rohit Grover 36:9ec94579bb78 386 }
Rohit Grover 36:9ec94579bb78 387
Rohit Grover 39:a57137537521 388 inline ble_error_t
Rohit Grover 81:fd8c6792281b 389 BLEDevice::stopAdvertising(void)
Rohit Grover 77:1436ecf09583 390 {
Rohit Grover 41:7ae5c71cd44c 391 return transport->getGap().stopAdvertising();
Rohit Grover 36:9ec94579bb78 392 }
Rohit Grover 36:9ec94579bb78 393
Rohit Grover 39:a57137537521 394 inline ble_error_t
Rohit Grover 81:fd8c6792281b 395 BLEDevice::disconnect(void)
Rohit Grover 77:1436ecf09583 396 {
Rohit Grover 41:7ae5c71cd44c 397 return transport->getGap().disconnect();
Rohit Grover 36:9ec94579bb78 398 }
Rohit Grover 36:9ec94579bb78 399
Rohit Grover 64:95529f47b782 400 inline void
Rohit Grover 81:fd8c6792281b 401 BLEDevice::onTimeout(Gap::EventCallback_t timeoutCallback)
Rohit Grover 77:1436ecf09583 402 {
Rohit Grover 64:95529f47b782 403 transport->getGap().setOnTimeout(timeoutCallback);
Rohit Grover 64:95529f47b782 404 }
Rohit Grover 64:95529f47b782 405
Rohit Grover 64:95529f47b782 406 inline void
Rohit Grover 81:fd8c6792281b 407 BLEDevice::onConnection(Gap::EventCallback_t connectionCallback)
Rohit Grover 77:1436ecf09583 408 {
Rohit Grover 64:95529f47b782 409 transport->getGap().setOnConnection(connectionCallback);
Rohit Grover 64:95529f47b782 410 }
Rohit Grover 64:95529f47b782 411
Rohit Grover 64:95529f47b782 412 inline void
Rohit Grover 81:fd8c6792281b 413 BLEDevice::onDisconnection(Gap::EventCallback_t disconnectionCallback)
Rohit Grover 77:1436ecf09583 414 {
Rohit Grover 64:95529f47b782 415 transport->getGap().setOnDisconnection(disconnectionCallback);
Rohit Grover 64:95529f47b782 416 }
Rohit Grover 64:95529f47b782 417
Rohit Grover 64:95529f47b782 418 inline void
Rohit Grover 99:58c47085e816 419 BLEDevice::onDataSent(GattServer::ServerEventCallback_t callback)
Rohit Grover 77:1436ecf09583 420 {
Rohit Grover 64:95529f47b782 421 transport->getGattServer().setOnDataSent(callback);
Rohit Grover 64:95529f47b782 422 }
Rohit Grover 64:95529f47b782 423
Rohit Grover 64:95529f47b782 424 inline void
Rohit Grover 81:fd8c6792281b 425 BLEDevice::onDataWritten(GattServer::EventCallback_t callback)
Rohit Grover 77:1436ecf09583 426 {
Rohit Grover 64:95529f47b782 427 transport->getGattServer().setOnDataWritten(callback);
Rohit Grover 64:95529f47b782 428 }
Rohit Grover 64:95529f47b782 429
Rohit Grover 64:95529f47b782 430 inline void
Rohit Grover 81:fd8c6792281b 431 BLEDevice::onUpdatesEnabled(GattServer::EventCallback_t callback)
Rohit Grover 77:1436ecf09583 432 {
Rohit Grover 64:95529f47b782 433 transport->getGattServer().setOnUpdatesEnabled(callback);
Rohit Grover 64:95529f47b782 434 }
Rohit Grover 64:95529f47b782 435
Rohit Grover 64:95529f47b782 436 inline void
Rohit Grover 81:fd8c6792281b 437 BLEDevice::onUpdatesDisabled(GattServer::EventCallback_t callback)
Rohit Grover 77:1436ecf09583 438 {
Rohit Grover 64:95529f47b782 439 transport->getGattServer().setOnUpdatesDisabled(callback);
Rohit Grover 64:95529f47b782 440 }
Rohit Grover 64:95529f47b782 441
Rohit Grover 64:95529f47b782 442 inline void
Rohit Grover 81:fd8c6792281b 443 BLEDevice::onConfirmationReceived(GattServer::EventCallback_t callback)
Rohit Grover 77:1436ecf09583 444 {
Rohit Grover 64:95529f47b782 445 transport->getGattServer().setOnConfirmationReceived(callback);
Rohit Grover 64:95529f47b782 446 }
Rohit Grover 64:95529f47b782 447
Rohit Grover 72:bd485a3b18c5 448 inline ble_error_t
Rohit Grover 81:fd8c6792281b 449 BLEDevice::addService(GattService &service)
Rohit Grover 77:1436ecf09583 450 {
Rohit Grover 72:bd485a3b18c5 451 return transport->getGattServer().addService(service);
Rohit Grover 72:bd485a3b18c5 452 }
Rohit Grover 72:bd485a3b18c5 453
Rohit Grover 73:eeb1ac3545e9 454 inline Gap::GapState_t
Rohit Grover 81:fd8c6792281b 455 BLEDevice::getGapState(void) const
Rohit Grover 77:1436ecf09583 456 {
Rohit Grover 73:eeb1ac3545e9 457 return transport->getGap().getState();
Rohit Grover 73:eeb1ac3545e9 458 }
Rohit Grover 73:eeb1ac3545e9 459
Rohit Grover 94:455363a6eb90 460 inline ble_error_t BLEDevice::readCharacteristicValue(uint16_t handle, uint8_t *const buffer, uint16_t *const lengthP)
Rohit Grover 94:455363a6eb90 461 {
Rohit Grover 94:455363a6eb90 462 return transport->getGattServer().readValue(handle, buffer, lengthP);
Rohit Grover 94:455363a6eb90 463 }
Rohit Grover 94:455363a6eb90 464
Rohit Grover 74:301042c34cb8 465 inline ble_error_t
Rohit Grover 81:fd8c6792281b 466 BLEDevice::updateCharacteristicValue(uint16_t handle, const uint8_t* value, uint16_t size, bool localOnly)
Rohit Grover 77:1436ecf09583 467 {
Rohit Grover 74:301042c34cb8 468 return transport->getGattServer().updateValue(handle, const_cast<uint8_t *>(value), size, localOnly);
Rohit Grover 74:301042c34cb8 469 }
Rohit Grover 74:301042c34cb8 470
Rohit Grover 78:5d356ef94347 471 inline void
Rohit Grover 81:fd8c6792281b 472 BLEDevice::waitForEvent(void)
Rohit Grover 78:5d356ef94347 473 {
Rohit Grover 78:5d356ef94347 474 transport->waitForEvent();
Rohit Grover 78:5d356ef94347 475 }
Rohit Grover 78:5d356ef94347 476
Rohit Grover 48:ec0c28e62abf 477 /*
Rohit Grover 48:ec0c28e62abf 478 * ALL OF THE FOLLOWING METHODS ARE DEPRECATED
Rohit Grover 48:ec0c28e62abf 479 */
Rohit Grover 48:ec0c28e62abf 480
Rohit Grover 48:ec0c28e62abf 481 inline ble_error_t
Rohit Grover 81:fd8c6792281b 482 BLEDevice::setAdvertisingData(const GapAdvertisingData &ADStructures, const GapAdvertisingData &scanResponse)
Rohit Grover 77:1436ecf09583 483 {
Rohit Grover 98:d751acb2543f 484 needToSetAdvPayload = false;
Rohit Grover 48:ec0c28e62abf 485 return transport->getGap().setAdvertisingData(ADStructures, scanResponse);
Rohit Grover 48:ec0c28e62abf 486 }
Rohit Grover 48:ec0c28e62abf 487
Rohit Grover 48:ec0c28e62abf 488 inline ble_error_t
Rohit Grover 81:fd8c6792281b 489 BLEDevice::setAdvertisingData(const GapAdvertisingData &ADStructures)
Rohit Grover 77:1436ecf09583 490 {
Rohit Grover 48:ec0c28e62abf 491 GapAdvertisingData scanResponse;
Rohit Grover 98:d751acb2543f 492
Rohit Grover 98:d751acb2543f 493 needToSetAdvPayload = false;
Rohit Grover 48:ec0c28e62abf 494 return transport->getGap().setAdvertisingData(ADStructures, scanResponse);
Rohit Grover 48:ec0c28e62abf 495 }
Rohit Grover 48:ec0c28e62abf 496
Rohit Grover 46:33d87f468a0d 497 inline ble_error_t
Rohit Grover 81:fd8c6792281b 498 BLEDevice::startAdvertising(const GapAdvertisingParams &_advParams)
Rohit Grover 77:1436ecf09583 499 {
Rohit Grover 46:33d87f468a0d 500 return transport->getGap().startAdvertising(_advParams);
Rohit Grover 46:33d87f468a0d 501 }
Rohit Grover 46:33d87f468a0d 502
Rohit Grover 81:fd8c6792281b 503 #endif // ifndef __BLE_DEVICE__