テスト用です。

Dependencies:   mbed

Committer:
jksoft
Date:
Tue Oct 11 11:09:42 2016 +0000
Revision:
0:8468a4403fea
SB??ver;

Who changed what in which revision?

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