BLE_API_Tiny_BLE

Dependents:   CSSE4011_BLE_IMU

Fork of BLE_API by Bluetooth Low Energy

Committer:
Rohit Grover
Date:
Thu Jul 31 14:39:35 2014 +0100
Revision:
111:189ff241dae1
Parent:
110:d5a824cee1d3
Child:
116:ca826083980e
rename BLEDeviceInstanceBase.hpp to .h; our online toolchain has problems with exporting .hpp files

Who changed what in which revision?

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