Preliminary main mbed library for nexpaq development

Committer:
nexpaq
Date:
Fri Nov 04 20:27:58 2016 +0000
Revision:
0:6c56fb4bc5f0
Moving to library for sharing updates

Who changed what in which revision?

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