Official Sheffield ARMBand micro:bit program
microbit/microbit-dal/BLE_API/ble/GapAdvertisingParams.h@0:b9164b348919, 2016-10-17 (annotated)
- Committer:
- MrBedfordVan
- Date:
- Mon Oct 17 12:41:20 2016 +0000
- Revision:
- 0:b9164b348919
Official Sheffield ARMBand Micro:bit program
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MrBedfordVan | 0:b9164b348919 | 1 | /* mbed Microcontroller Library |
MrBedfordVan | 0:b9164b348919 | 2 | * Copyright (c) 2006-2013 ARM Limited |
MrBedfordVan | 0:b9164b348919 | 3 | * |
MrBedfordVan | 0:b9164b348919 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
MrBedfordVan | 0:b9164b348919 | 5 | * you may not use this file except in compliance with the License. |
MrBedfordVan | 0:b9164b348919 | 6 | * You may obtain a copy of the License at |
MrBedfordVan | 0:b9164b348919 | 7 | * |
MrBedfordVan | 0:b9164b348919 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
MrBedfordVan | 0:b9164b348919 | 9 | * |
MrBedfordVan | 0:b9164b348919 | 10 | * Unless required by applicable law or agreed to in writing, software |
MrBedfordVan | 0:b9164b348919 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
MrBedfordVan | 0:b9164b348919 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
MrBedfordVan | 0:b9164b348919 | 13 | * See the License for the specific language governing permissions and |
MrBedfordVan | 0:b9164b348919 | 14 | * limitations under the License. |
MrBedfordVan | 0:b9164b348919 | 15 | */ |
MrBedfordVan | 0:b9164b348919 | 16 | |
MrBedfordVan | 0:b9164b348919 | 17 | #ifndef __GAP_ADVERTISING_PARAMS_H__ |
MrBedfordVan | 0:b9164b348919 | 18 | #define __GAP_ADVERTISING_PARAMS_H__ |
MrBedfordVan | 0:b9164b348919 | 19 | |
MrBedfordVan | 0:b9164b348919 | 20 | /** |
MrBedfordVan | 0:b9164b348919 | 21 | * This class provides a wrapper for the core advertising parameters, |
MrBedfordVan | 0:b9164b348919 | 22 | * including the advertising type (Connectable Undirected, |
MrBedfordVan | 0:b9164b348919 | 23 | * Non Connectable Undirected and so on), as well as the advertising and |
MrBedfordVan | 0:b9164b348919 | 24 | * timeout intervals. |
MrBedfordVan | 0:b9164b348919 | 25 | */ |
MrBedfordVan | 0:b9164b348919 | 26 | class GapAdvertisingParams { |
MrBedfordVan | 0:b9164b348919 | 27 | public: |
MrBedfordVan | 0:b9164b348919 | 28 | static const unsigned GAP_ADV_PARAMS_INTERVAL_MIN = 0x0020; |
MrBedfordVan | 0:b9164b348919 | 29 | static const unsigned GAP_ADV_PARAMS_INTERVAL_MIN_NONCON = 0x00A0; |
MrBedfordVan | 0:b9164b348919 | 30 | static const unsigned GAP_ADV_PARAMS_INTERVAL_MAX = 0x4000; |
MrBedfordVan | 0:b9164b348919 | 31 | static const unsigned GAP_ADV_PARAMS_TIMEOUT_MAX = 0x3FFF; |
MrBedfordVan | 0:b9164b348919 | 32 | |
MrBedfordVan | 0:b9164b348919 | 33 | /*! |
MrBedfordVan | 0:b9164b348919 | 34 | * Encapsulates the peripheral advertising modes, which determine how |
MrBedfordVan | 0:b9164b348919 | 35 | * the device appears to other central devices in hearing range. |
MrBedfordVan | 0:b9164b348919 | 36 | */ |
MrBedfordVan | 0:b9164b348919 | 37 | enum AdvertisingType_t { |
MrBedfordVan | 0:b9164b348919 | 38 | ADV_CONNECTABLE_UNDIRECTED, /**< Vol 3, Part C, Section 9.3.4 and Vol 6, Part B, Section 2.3.1.1 */ |
MrBedfordVan | 0:b9164b348919 | 39 | ADV_CONNECTABLE_DIRECTED, /**< Vol 3, Part C, Section 9.3.3 and Vol 6, Part B, Section 2.3.1.2 */ |
MrBedfordVan | 0:b9164b348919 | 40 | ADV_SCANNABLE_UNDIRECTED, /**< Include support for Scan Response payloads, see Vol 6, Part B, Section 2.3.1.4 */ |
MrBedfordVan | 0:b9164b348919 | 41 | ADV_NON_CONNECTABLE_UNDIRECTED /**< Vol 3, Part C, Section 9.3.2 and Vol 6, Part B, Section 2.3.1.3 */ |
MrBedfordVan | 0:b9164b348919 | 42 | }; |
MrBedfordVan | 0:b9164b348919 | 43 | typedef enum AdvertisingType_t AdvertisingType; /* Deprecated type alias. */ |
MrBedfordVan | 0:b9164b348919 | 44 | |
MrBedfordVan | 0:b9164b348919 | 45 | public: |
MrBedfordVan | 0:b9164b348919 | 46 | GapAdvertisingParams(AdvertisingType_t advType = ADV_CONNECTABLE_UNDIRECTED, |
MrBedfordVan | 0:b9164b348919 | 47 | uint16_t interval = GAP_ADV_PARAMS_INTERVAL_MIN_NONCON, |
MrBedfordVan | 0:b9164b348919 | 48 | uint16_t timeout = 0) : _advType(advType), _interval(interval), _timeout(timeout) { |
MrBedfordVan | 0:b9164b348919 | 49 | /* Interval checks. */ |
MrBedfordVan | 0:b9164b348919 | 50 | if (_advType == ADV_CONNECTABLE_DIRECTED) { |
MrBedfordVan | 0:b9164b348919 | 51 | /* Interval must be 0 in directed connectable mode. */ |
MrBedfordVan | 0:b9164b348919 | 52 | _interval = 0; |
MrBedfordVan | 0:b9164b348919 | 53 | } else if (_advType == ADV_NON_CONNECTABLE_UNDIRECTED) { |
MrBedfordVan | 0:b9164b348919 | 54 | /* Min interval is slightly larger than in other modes. */ |
MrBedfordVan | 0:b9164b348919 | 55 | if (_interval < GAP_ADV_PARAMS_INTERVAL_MIN_NONCON) { |
MrBedfordVan | 0:b9164b348919 | 56 | _interval = GAP_ADV_PARAMS_INTERVAL_MIN_NONCON; |
MrBedfordVan | 0:b9164b348919 | 57 | } |
MrBedfordVan | 0:b9164b348919 | 58 | if (_interval > GAP_ADV_PARAMS_INTERVAL_MAX) { |
MrBedfordVan | 0:b9164b348919 | 59 | _interval = GAP_ADV_PARAMS_INTERVAL_MAX; |
MrBedfordVan | 0:b9164b348919 | 60 | } |
MrBedfordVan | 0:b9164b348919 | 61 | } else { |
MrBedfordVan | 0:b9164b348919 | 62 | /* Stay within interval limits. */ |
MrBedfordVan | 0:b9164b348919 | 63 | if (_interval < GAP_ADV_PARAMS_INTERVAL_MIN) { |
MrBedfordVan | 0:b9164b348919 | 64 | _interval = GAP_ADV_PARAMS_INTERVAL_MIN; |
MrBedfordVan | 0:b9164b348919 | 65 | } |
MrBedfordVan | 0:b9164b348919 | 66 | if (_interval > GAP_ADV_PARAMS_INTERVAL_MAX) { |
MrBedfordVan | 0:b9164b348919 | 67 | _interval = GAP_ADV_PARAMS_INTERVAL_MAX; |
MrBedfordVan | 0:b9164b348919 | 68 | } |
MrBedfordVan | 0:b9164b348919 | 69 | } |
MrBedfordVan | 0:b9164b348919 | 70 | |
MrBedfordVan | 0:b9164b348919 | 71 | /* Timeout checks. */ |
MrBedfordVan | 0:b9164b348919 | 72 | if (timeout) { |
MrBedfordVan | 0:b9164b348919 | 73 | /* Stay within timeout limits. */ |
MrBedfordVan | 0:b9164b348919 | 74 | if (_timeout > GAP_ADV_PARAMS_TIMEOUT_MAX) { |
MrBedfordVan | 0:b9164b348919 | 75 | _timeout = GAP_ADV_PARAMS_TIMEOUT_MAX; |
MrBedfordVan | 0:b9164b348919 | 76 | } |
MrBedfordVan | 0:b9164b348919 | 77 | } |
MrBedfordVan | 0:b9164b348919 | 78 | } |
MrBedfordVan | 0:b9164b348919 | 79 | |
MrBedfordVan | 0:b9164b348919 | 80 | static const uint16_t UNIT_0_625_MS = 625; /**< Number of microseconds in 0.625 milliseconds. */ |
MrBedfordVan | 0:b9164b348919 | 81 | static uint16_t MSEC_TO_ADVERTISEMENT_DURATION_UNITS(uint32_t durationInMillis) { |
MrBedfordVan | 0:b9164b348919 | 82 | return (durationInMillis * 1000) / UNIT_0_625_MS; |
MrBedfordVan | 0:b9164b348919 | 83 | } |
MrBedfordVan | 0:b9164b348919 | 84 | static uint16_t ADVERTISEMENT_DURATION_UNITS_TO_MS(uint16_t gapUnits) { |
MrBedfordVan | 0:b9164b348919 | 85 | return (gapUnits * UNIT_0_625_MS) / 1000; |
MrBedfordVan | 0:b9164b348919 | 86 | } |
MrBedfordVan | 0:b9164b348919 | 87 | |
MrBedfordVan | 0:b9164b348919 | 88 | AdvertisingType_t getAdvertisingType(void) const { |
MrBedfordVan | 0:b9164b348919 | 89 | return _advType; |
MrBedfordVan | 0:b9164b348919 | 90 | } |
MrBedfordVan | 0:b9164b348919 | 91 | |
MrBedfordVan | 0:b9164b348919 | 92 | /** |
MrBedfordVan | 0:b9164b348919 | 93 | * @return the advertisement interval (in milliseconds). |
MrBedfordVan | 0:b9164b348919 | 94 | */ |
MrBedfordVan | 0:b9164b348919 | 95 | uint16_t getInterval(void) const { |
MrBedfordVan | 0:b9164b348919 | 96 | return ADVERTISEMENT_DURATION_UNITS_TO_MS(_interval); |
MrBedfordVan | 0:b9164b348919 | 97 | } |
MrBedfordVan | 0:b9164b348919 | 98 | |
MrBedfordVan | 0:b9164b348919 | 99 | /** |
MrBedfordVan | 0:b9164b348919 | 100 | * @return the advertisement interval in advertisement duration units (0.625ms units). |
MrBedfordVan | 0:b9164b348919 | 101 | */ |
MrBedfordVan | 0:b9164b348919 | 102 | uint16_t getIntervalInADVUnits(void) const { |
MrBedfordVan | 0:b9164b348919 | 103 | return _interval; |
MrBedfordVan | 0:b9164b348919 | 104 | } |
MrBedfordVan | 0:b9164b348919 | 105 | |
MrBedfordVan | 0:b9164b348919 | 106 | uint16_t getTimeout(void) const { |
MrBedfordVan | 0:b9164b348919 | 107 | return _timeout; |
MrBedfordVan | 0:b9164b348919 | 108 | } |
MrBedfordVan | 0:b9164b348919 | 109 | |
MrBedfordVan | 0:b9164b348919 | 110 | void setAdvertisingType(AdvertisingType_t newAdvType) {_advType = newAdvType; } |
MrBedfordVan | 0:b9164b348919 | 111 | void setInterval(uint16_t newInterval) {_interval = MSEC_TO_ADVERTISEMENT_DURATION_UNITS(newInterval);} |
MrBedfordVan | 0:b9164b348919 | 112 | void setTimeout(uint16_t newTimeout) {_timeout = newTimeout; } |
MrBedfordVan | 0:b9164b348919 | 113 | |
MrBedfordVan | 0:b9164b348919 | 114 | private: |
MrBedfordVan | 0:b9164b348919 | 115 | AdvertisingType_t _advType; |
MrBedfordVan | 0:b9164b348919 | 116 | uint16_t _interval; /* In ADV duration units (i.e. 0.625ms). */ |
MrBedfordVan | 0:b9164b348919 | 117 | uint16_t _timeout; /* In seconds. */ |
MrBedfordVan | 0:b9164b348919 | 118 | }; |
MrBedfordVan | 0:b9164b348919 | 119 | |
MrBedfordVan | 0:b9164b348919 | 120 | #endif // ifndef __GAP_ADVERTISING_PARAMS_H__ |