Dependents:   sensomed

Committer:
switches
Date:
Tue Nov 08 18:27:11 2016 +0000
Revision:
0:0e018d759a2a
Initial commit

Who changed what in which revision?

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