High level Bluetooth Low Energy API and radio abstraction layer

Dependents:   BLE_ANCS_SDAPI BLE_temperature BLE_HeartRate BLE_ANCS_SDAPI_IRC ... more

Overview

The BLE_API is a high level abstraction for using Bluetooth Low Energy on multiple platforms. For details and examples using the BLE_API please see the BLE_API Summary Page. Or click on the API Documentation tab above.

Supported Services

Supported services can be found in the BLE_API/services folder.

Committer:
Vincent Coubard
Date:
Wed Sep 14 14:18:00 2016 +0100
Revision:
1208:65474dc93927
Parent:
1183:1589830dbdb7
Sync with 8d97fced5440d78c9557693b6d1632f1ab5d77b7

2016-09-01 08:21:37+01:00: Vincent Coubard
version v2.7.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
vcoubard 1174:551333f70bc9 1 /* mbed Microcontroller Library
vcoubard 1174:551333f70bc9 2 * Copyright (c) 2006-2013 ARM Limited
vcoubard 1174:551333f70bc9 3 *
vcoubard 1174:551333f70bc9 4 * Licensed under the Apache License, Version 2.0 (the "License");
vcoubard 1174:551333f70bc9 5 * you may not use this file except in compliance with the License.
vcoubard 1174:551333f70bc9 6 * You may obtain a copy of the License at
vcoubard 1174:551333f70bc9 7 *
vcoubard 1174:551333f70bc9 8 * http://www.apache.org/licenses/LICENSE-2.0
vcoubard 1174:551333f70bc9 9 *
vcoubard 1174:551333f70bc9 10 * Unless required by applicable law or agreed to in writing, software
vcoubard 1174:551333f70bc9 11 * distributed under the License is distributed on an "AS IS" BASIS,
vcoubard 1174:551333f70bc9 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
vcoubard 1174:551333f70bc9 13 * See the License for the specific language governing permissions and
vcoubard 1174:551333f70bc9 14 * limitations under the License.
vcoubard 1174:551333f70bc9 15 */
vcoubard 1174:551333f70bc9 16
vcoubard 1174:551333f70bc9 17 #ifndef __GAP_ADVERTISING_PARAMS_H__
vcoubard 1174:551333f70bc9 18 #define __GAP_ADVERTISING_PARAMS_H__
vcoubard 1174:551333f70bc9 19
vcoubard 1174:551333f70bc9 20 /**
vcoubard 1174:551333f70bc9 21 * This class provides a wrapper for the core advertising parameters,
vcoubard 1174:551333f70bc9 22 * including the advertising type (Connectable Undirected,
vcoubard 1174:551333f70bc9 23 * Non Connectable Undirected and so on), as well as the advertising and
vcoubard 1174:551333f70bc9 24 * timeout intervals.
vcoubard 1174:551333f70bc9 25 */
vcoubard 1174:551333f70bc9 26 class GapAdvertisingParams {
vcoubard 1174:551333f70bc9 27 public:
vcoubard 1183:1589830dbdb7 28 /**
vcoubard 1183:1589830dbdb7 29 * Minimum Advertising interval for connectable undirected and connectable
vcoubard 1183:1589830dbdb7 30 * directed events in 625us units - 20ms.
vcoubard 1183:1589830dbdb7 31 */
vcoubard 1174:551333f70bc9 32 static const unsigned GAP_ADV_PARAMS_INTERVAL_MIN = 0x0020;
vcoubard 1183:1589830dbdb7 33 /**
vcoubard 1183:1589830dbdb7 34 * Minimum Advertising interval for scannable and non-connectable
vcoubard 1183:1589830dbdb7 35 * undirected events in 625us units - 100ms.
vcoubard 1183:1589830dbdb7 36 */
vcoubard 1174:551333f70bc9 37 static const unsigned GAP_ADV_PARAMS_INTERVAL_MIN_NONCON = 0x00A0;
vcoubard 1183:1589830dbdb7 38 /**
vcoubard 1183:1589830dbdb7 39 * Maximum Advertising interval in 625us units - 10.24s.
vcoubard 1183:1589830dbdb7 40 */
vcoubard 1174:551333f70bc9 41 static const unsigned GAP_ADV_PARAMS_INTERVAL_MAX = 0x4000;
vcoubard 1183:1589830dbdb7 42 /**
vcoubard 1183:1589830dbdb7 43 * Maximum advertising timeout seconds.
vcoubard 1183:1589830dbdb7 44 */
vcoubard 1174:551333f70bc9 45 static const unsigned GAP_ADV_PARAMS_TIMEOUT_MAX = 0x3FFF;
vcoubard 1174:551333f70bc9 46
vcoubard 1183:1589830dbdb7 47 /**
vcoubard 1174:551333f70bc9 48 * Encapsulates the peripheral advertising modes, which determine how
vcoubard 1174:551333f70bc9 49 * the device appears to other central devices in hearing range.
vcoubard 1174:551333f70bc9 50 */
vcoubard 1174:551333f70bc9 51 enum AdvertisingType_t {
vcoubard 1183:1589830dbdb7 52 ADV_CONNECTABLE_UNDIRECTED, /**< Vol 3, Part C, Section 9.3.4 and Vol 6, Part B, Section 2.3.1.1. */
vcoubard 1183:1589830dbdb7 53 ADV_CONNECTABLE_DIRECTED, /**< Vol 3, Part C, Section 9.3.3 and Vol 6, Part B, Section 2.3.1.2. */
vcoubard 1183:1589830dbdb7 54 ADV_SCANNABLE_UNDIRECTED, /**< Include support for Scan Response payloads, see Vol 6, Part B, Section 2.3.1.4. */
vcoubard 1183:1589830dbdb7 55 ADV_NON_CONNECTABLE_UNDIRECTED /**< Vol 3, Part C, Section 9.3.2 and Vol 6, Part B, Section 2.3.1.3. */
vcoubard 1174:551333f70bc9 56 };
vcoubard 1183:1589830dbdb7 57 /**
vcoubard 1183:1589830dbdb7 58 * Type alias for GapAdvertisingParams::AdvertisingType_t.
vcoubard 1183:1589830dbdb7 59 *
vcoubard 1183:1589830dbdb7 60 * @deprecated This type alias will be dropped in future releases.
vcoubard 1183:1589830dbdb7 61 */
vcoubard 1183:1589830dbdb7 62 typedef enum AdvertisingType_t AdvertisingType;
vcoubard 1174:551333f70bc9 63
vcoubard 1174:551333f70bc9 64 public:
vcoubard 1183:1589830dbdb7 65 /**
vcoubard 1183:1589830dbdb7 66 * Construct an instance of GapAdvertisingParams.
vcoubard 1183:1589830dbdb7 67 *
vcoubard 1183:1589830dbdb7 68 * @param[in] advType
vcoubard 1183:1589830dbdb7 69 * Type of advertising. Default is
vcoubard 1183:1589830dbdb7 70 * GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED.
vcoubard 1183:1589830dbdb7 71 * @param[in] interval
vcoubard 1183:1589830dbdb7 72 * Advertising interval in units of 0.625ms. Default is
vcoubard 1183:1589830dbdb7 73 * GapAdvertisingParams::GAP_ADV_PARAMS_INTERVAL_MIN_NONCON.
vcoubard 1183:1589830dbdb7 74 * @param[in] timeout
vcoubard 1183:1589830dbdb7 75 * Advertising timeout. Default is 0.
vcoubard 1183:1589830dbdb7 76 */
vcoubard 1174:551333f70bc9 77 GapAdvertisingParams(AdvertisingType_t advType = ADV_CONNECTABLE_UNDIRECTED,
vcoubard 1174:551333f70bc9 78 uint16_t interval = GAP_ADV_PARAMS_INTERVAL_MIN_NONCON,
vcoubard 1174:551333f70bc9 79 uint16_t timeout = 0) : _advType(advType), _interval(interval), _timeout(timeout) {
vcoubard 1174:551333f70bc9 80 /* Interval checks. */
vcoubard 1174:551333f70bc9 81 if (_advType == ADV_CONNECTABLE_DIRECTED) {
vcoubard 1174:551333f70bc9 82 /* Interval must be 0 in directed connectable mode. */
vcoubard 1174:551333f70bc9 83 _interval = 0;
vcoubard 1174:551333f70bc9 84 } else if (_advType == ADV_NON_CONNECTABLE_UNDIRECTED) {
vcoubard 1174:551333f70bc9 85 /* Min interval is slightly larger than in other modes. */
vcoubard 1174:551333f70bc9 86 if (_interval < GAP_ADV_PARAMS_INTERVAL_MIN_NONCON) {
vcoubard 1174:551333f70bc9 87 _interval = GAP_ADV_PARAMS_INTERVAL_MIN_NONCON;
vcoubard 1174:551333f70bc9 88 }
vcoubard 1174:551333f70bc9 89 if (_interval > GAP_ADV_PARAMS_INTERVAL_MAX) {
vcoubard 1174:551333f70bc9 90 _interval = GAP_ADV_PARAMS_INTERVAL_MAX;
vcoubard 1174:551333f70bc9 91 }
vcoubard 1174:551333f70bc9 92 } else {
vcoubard 1174:551333f70bc9 93 /* Stay within interval limits. */
vcoubard 1174:551333f70bc9 94 if (_interval < GAP_ADV_PARAMS_INTERVAL_MIN) {
vcoubard 1174:551333f70bc9 95 _interval = GAP_ADV_PARAMS_INTERVAL_MIN;
vcoubard 1174:551333f70bc9 96 }
vcoubard 1174:551333f70bc9 97 if (_interval > GAP_ADV_PARAMS_INTERVAL_MAX) {
vcoubard 1174:551333f70bc9 98 _interval = GAP_ADV_PARAMS_INTERVAL_MAX;
vcoubard 1174:551333f70bc9 99 }
vcoubard 1174:551333f70bc9 100 }
vcoubard 1174:551333f70bc9 101
vcoubard 1174:551333f70bc9 102 /* Timeout checks. */
vcoubard 1174:551333f70bc9 103 if (timeout) {
vcoubard 1174:551333f70bc9 104 /* Stay within timeout limits. */
vcoubard 1174:551333f70bc9 105 if (_timeout > GAP_ADV_PARAMS_TIMEOUT_MAX) {
vcoubard 1174:551333f70bc9 106 _timeout = GAP_ADV_PARAMS_TIMEOUT_MAX;
vcoubard 1174:551333f70bc9 107 }
vcoubard 1174:551333f70bc9 108 }
vcoubard 1174:551333f70bc9 109 }
vcoubard 1174:551333f70bc9 110
vcoubard 1174:551333f70bc9 111 static const uint16_t UNIT_0_625_MS = 625; /**< Number of microseconds in 0.625 milliseconds. */
vcoubard 1183:1589830dbdb7 112 /**
vcoubard 1183:1589830dbdb7 113 * Convert milliseconds to units of 0.625ms.
vcoubard 1183:1589830dbdb7 114 *
vcoubard 1183:1589830dbdb7 115 * @param[in] durationInMillis
vcoubard 1183:1589830dbdb7 116 * The number of milliseconds to convert.
vcoubard 1183:1589830dbdb7 117 *
vcoubard 1183:1589830dbdb7 118 * @return The value of @p durationInMillis in units of 0.625ms.
vcoubard 1183:1589830dbdb7 119 */
vcoubard 1174:551333f70bc9 120 static uint16_t MSEC_TO_ADVERTISEMENT_DURATION_UNITS(uint32_t durationInMillis) {
vcoubard 1174:551333f70bc9 121 return (durationInMillis * 1000) / UNIT_0_625_MS;
vcoubard 1174:551333f70bc9 122 }
vcoubard 1183:1589830dbdb7 123 /**
vcoubard 1183:1589830dbdb7 124 * Convert units of 0.625ms to milliseconds.
vcoubard 1183:1589830dbdb7 125 *
vcoubard 1183:1589830dbdb7 126 * @param[in] gapUnits
vcoubard 1183:1589830dbdb7 127 * The number of units of 0.625ms to convert.
vcoubard 1183:1589830dbdb7 128 *
vcoubard 1183:1589830dbdb7 129 * @return The value of @p gapUnits in milliseconds.
vcoubard 1183:1589830dbdb7 130 */
vcoubard 1174:551333f70bc9 131 static uint16_t ADVERTISEMENT_DURATION_UNITS_TO_MS(uint16_t gapUnits) {
vcoubard 1174:551333f70bc9 132 return (gapUnits * UNIT_0_625_MS) / 1000;
vcoubard 1174:551333f70bc9 133 }
vcoubard 1174:551333f70bc9 134
vcoubard 1183:1589830dbdb7 135 /**
vcoubard 1183:1589830dbdb7 136 * Get the advertising type.
vcoubard 1183:1589830dbdb7 137 *
vcoubard 1183:1589830dbdb7 138 * @return The advertising type.
vcoubard 1183:1589830dbdb7 139 */
vcoubard 1174:551333f70bc9 140 AdvertisingType_t getAdvertisingType(void) const {
vcoubard 1174:551333f70bc9 141 return _advType;
vcoubard 1174:551333f70bc9 142 }
vcoubard 1174:551333f70bc9 143
vcoubard 1174:551333f70bc9 144 /**
vcoubard 1183:1589830dbdb7 145 * Get the advertising interval in milliseconds.
vcoubard 1183:1589830dbdb7 146 *
vcoubard 1183:1589830dbdb7 147 * @return The advertisement interval (in milliseconds).
vcoubard 1174:551333f70bc9 148 */
vcoubard 1174:551333f70bc9 149 uint16_t getInterval(void) const {
vcoubard 1174:551333f70bc9 150 return ADVERTISEMENT_DURATION_UNITS_TO_MS(_interval);
vcoubard 1174:551333f70bc9 151 }
vcoubard 1174:551333f70bc9 152
vcoubard 1174:551333f70bc9 153 /**
vcoubard 1183:1589830dbdb7 154 * Get the advertisement interval in units of 0.625ms.
vcoubard 1183:1589830dbdb7 155 *
vcoubard 1183:1589830dbdb7 156 * @return The advertisement interval in advertisement duration units (0.625ms units).
vcoubard 1174:551333f70bc9 157 */
vcoubard 1174:551333f70bc9 158 uint16_t getIntervalInADVUnits(void) const {
vcoubard 1174:551333f70bc9 159 return _interval;
vcoubard 1174:551333f70bc9 160 }
vcoubard 1174:551333f70bc9 161
vcoubard 1183:1589830dbdb7 162 /**
vcoubard 1183:1589830dbdb7 163 * Get The advertising timeout.
vcoubard 1183:1589830dbdb7 164 *
vcoubard 1183:1589830dbdb7 165 * @return The advertising timeout (in seconds).
vcoubard 1183:1589830dbdb7 166 */
vcoubard 1174:551333f70bc9 167 uint16_t getTimeout(void) const {
vcoubard 1174:551333f70bc9 168 return _timeout;
vcoubard 1174:551333f70bc9 169 }
vcoubard 1174:551333f70bc9 170
vcoubard 1183:1589830dbdb7 171 /**
vcoubard 1183:1589830dbdb7 172 * Set the advertising type.
vcoubard 1183:1589830dbdb7 173 *
vcoubard 1183:1589830dbdb7 174 * @param[in] newAdvType
vcoubard 1183:1589830dbdb7 175 * The new advertising type.
vcoubard 1183:1589830dbdb7 176 */
vcoubard 1183:1589830dbdb7 177 void setAdvertisingType(AdvertisingType_t newAdvType) {
vcoubard 1183:1589830dbdb7 178 _advType = newAdvType;
vcoubard 1183:1589830dbdb7 179 }
vcoubard 1183:1589830dbdb7 180
vcoubard 1183:1589830dbdb7 181 /**
vcoubard 1183:1589830dbdb7 182 * Set the advertising interval in milliseconds.
vcoubard 1183:1589830dbdb7 183 *
vcoubard 1183:1589830dbdb7 184 * @param[in] newInterval
vcoubard 1183:1589830dbdb7 185 * The new advertising interval in milliseconds.
vcoubard 1183:1589830dbdb7 186 */
vcoubard 1183:1589830dbdb7 187 void setInterval(uint16_t newInterval) {
vcoubard 1183:1589830dbdb7 188 _interval = MSEC_TO_ADVERTISEMENT_DURATION_UNITS(newInterval);
vcoubard 1183:1589830dbdb7 189 }
vcoubard 1183:1589830dbdb7 190
vcoubard 1183:1589830dbdb7 191 /**
vcoubard 1183:1589830dbdb7 192 * Set the advertising timeout.
vcoubard 1183:1589830dbdb7 193 *
vcoubard 1183:1589830dbdb7 194 * @param[in] newTimeout
vcoubard 1183:1589830dbdb7 195 * The new advertising timeout (in seconds).
vcoubard 1183:1589830dbdb7 196 */
vcoubard 1183:1589830dbdb7 197 void setTimeout(uint16_t newTimeout) {
vcoubard 1183:1589830dbdb7 198 _timeout = newTimeout;
vcoubard 1183:1589830dbdb7 199 }
vcoubard 1174:551333f70bc9 200
vcoubard 1174:551333f70bc9 201 private:
vcoubard 1183:1589830dbdb7 202 AdvertisingType_t _advType; /**< The advertising type. */
vcoubard 1183:1589830dbdb7 203 uint16_t _interval; /**< The advertising interval in ADV duration units (i.e. 0.625ms). */
vcoubard 1183:1589830dbdb7 204 uint16_t _timeout; /**< The advertising timeout in seconds. */
vcoubard 1174:551333f70bc9 205 };
vcoubard 1174:551333f70bc9 206
vcoubard 1183:1589830dbdb7 207 #endif /* ifndef __GAP_ADVERTISING_PARAMS_H__ */