jgh

Dependents:   Migration

Fork of BLE_API by Bluetooth Low Energy

Committer:
vcoubard
Date:
Wed Apr 06 19:15:30 2016 +0100
Revision:
1179:4ab722f8dca0
Parent:
1174:551333f70bc9
Child:
1183:1589830dbdb7
Synchronized with git rev ca632aaf
Author: Andres Amaya Garcia
Update Gap state after advertising times out

The BLE API was not updating the Gap internal state when the advertising stops
because of a user timeout. This commit fixes the issue by updating the internal
state structure in Gap just before the registered callbacks are notified of the
advertising timeout.

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 1174:551333f70bc9 28 static const unsigned GAP_ADV_PARAMS_INTERVAL_MIN = 0x0020;
vcoubard 1174:551333f70bc9 29 static const unsigned GAP_ADV_PARAMS_INTERVAL_MIN_NONCON = 0x00A0;
vcoubard 1174:551333f70bc9 30 static const unsigned GAP_ADV_PARAMS_INTERVAL_MAX = 0x4000;
vcoubard 1174:551333f70bc9 31 static const unsigned GAP_ADV_PARAMS_TIMEOUT_MAX = 0x3FFF;
vcoubard 1174:551333f70bc9 32
vcoubard 1179:4ab722f8dca0 33 /*!
vcoubard 1174:551333f70bc9 34 * Encapsulates the peripheral advertising modes, which determine how
vcoubard 1174:551333f70bc9 35 * the device appears to other central devices in hearing range.
vcoubard 1174:551333f70bc9 36 */
vcoubard 1174:551333f70bc9 37 enum AdvertisingType_t {
vcoubard 1179:4ab722f8dca0 38 ADV_CONNECTABLE_UNDIRECTED, /**< Vol 3, Part C, Section 9.3.4 and Vol 6, Part B, Section 2.3.1.1 */
vcoubard 1179:4ab722f8dca0 39 ADV_CONNECTABLE_DIRECTED, /**< Vol 3, Part C, Section 9.3.3 and Vol 6, Part B, Section 2.3.1.2 */
vcoubard 1179:4ab722f8dca0 40 ADV_SCANNABLE_UNDIRECTED, /**< Include support for Scan Response payloads, see Vol 6, Part B, Section 2.3.1.4 */
vcoubard 1179:4ab722f8dca0 41 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 42 };
vcoubard 1179:4ab722f8dca0 43 typedef enum AdvertisingType_t AdvertisingType; /* Deprecated type alias. */
vcoubard 1174:551333f70bc9 44
vcoubard 1174:551333f70bc9 45 public:
vcoubard 1174:551333f70bc9 46 GapAdvertisingParams(AdvertisingType_t advType = ADV_CONNECTABLE_UNDIRECTED,
vcoubard 1174:551333f70bc9 47 uint16_t interval = GAP_ADV_PARAMS_INTERVAL_MIN_NONCON,
vcoubard 1174:551333f70bc9 48 uint16_t timeout = 0) : _advType(advType), _interval(interval), _timeout(timeout) {
vcoubard 1174:551333f70bc9 49 /* Interval checks. */
vcoubard 1174:551333f70bc9 50 if (_advType == ADV_CONNECTABLE_DIRECTED) {
vcoubard 1174:551333f70bc9 51 /* Interval must be 0 in directed connectable mode. */
vcoubard 1174:551333f70bc9 52 _interval = 0;
vcoubard 1174:551333f70bc9 53 } else if (_advType == ADV_NON_CONNECTABLE_UNDIRECTED) {
vcoubard 1174:551333f70bc9 54 /* Min interval is slightly larger than in other modes. */
vcoubard 1174:551333f70bc9 55 if (_interval < GAP_ADV_PARAMS_INTERVAL_MIN_NONCON) {
vcoubard 1174:551333f70bc9 56 _interval = GAP_ADV_PARAMS_INTERVAL_MIN_NONCON;
vcoubard 1174:551333f70bc9 57 }
vcoubard 1174:551333f70bc9 58 if (_interval > GAP_ADV_PARAMS_INTERVAL_MAX) {
vcoubard 1174:551333f70bc9 59 _interval = GAP_ADV_PARAMS_INTERVAL_MAX;
vcoubard 1174:551333f70bc9 60 }
vcoubard 1174:551333f70bc9 61 } else {
vcoubard 1174:551333f70bc9 62 /* Stay within interval limits. */
vcoubard 1174:551333f70bc9 63 if (_interval < GAP_ADV_PARAMS_INTERVAL_MIN) {
vcoubard 1174:551333f70bc9 64 _interval = GAP_ADV_PARAMS_INTERVAL_MIN;
vcoubard 1174:551333f70bc9 65 }
vcoubard 1174:551333f70bc9 66 if (_interval > GAP_ADV_PARAMS_INTERVAL_MAX) {
vcoubard 1174:551333f70bc9 67 _interval = GAP_ADV_PARAMS_INTERVAL_MAX;
vcoubard 1174:551333f70bc9 68 }
vcoubard 1174:551333f70bc9 69 }
vcoubard 1174:551333f70bc9 70
vcoubard 1174:551333f70bc9 71 /* Timeout checks. */
vcoubard 1174:551333f70bc9 72 if (timeout) {
vcoubard 1174:551333f70bc9 73 /* Stay within timeout limits. */
vcoubard 1174:551333f70bc9 74 if (_timeout > GAP_ADV_PARAMS_TIMEOUT_MAX) {
vcoubard 1174:551333f70bc9 75 _timeout = GAP_ADV_PARAMS_TIMEOUT_MAX;
vcoubard 1174:551333f70bc9 76 }
vcoubard 1174:551333f70bc9 77 }
vcoubard 1174:551333f70bc9 78 }
vcoubard 1174:551333f70bc9 79
vcoubard 1174:551333f70bc9 80 static const uint16_t UNIT_0_625_MS = 625; /**< Number of microseconds in 0.625 milliseconds. */
vcoubard 1174:551333f70bc9 81 static uint16_t MSEC_TO_ADVERTISEMENT_DURATION_UNITS(uint32_t durationInMillis) {
vcoubard 1174:551333f70bc9 82 return (durationInMillis * 1000) / UNIT_0_625_MS;
vcoubard 1174:551333f70bc9 83 }
vcoubard 1174:551333f70bc9 84 static uint16_t ADVERTISEMENT_DURATION_UNITS_TO_MS(uint16_t gapUnits) {
vcoubard 1174:551333f70bc9 85 return (gapUnits * UNIT_0_625_MS) / 1000;
vcoubard 1174:551333f70bc9 86 }
vcoubard 1174:551333f70bc9 87
vcoubard 1174:551333f70bc9 88 AdvertisingType_t getAdvertisingType(void) const {
vcoubard 1174:551333f70bc9 89 return _advType;
vcoubard 1174:551333f70bc9 90 }
vcoubard 1174:551333f70bc9 91
vcoubard 1174:551333f70bc9 92 /**
vcoubard 1179:4ab722f8dca0 93 * @return the advertisement interval (in milliseconds).
vcoubard 1174:551333f70bc9 94 */
vcoubard 1174:551333f70bc9 95 uint16_t getInterval(void) const {
vcoubard 1174:551333f70bc9 96 return ADVERTISEMENT_DURATION_UNITS_TO_MS(_interval);
vcoubard 1174:551333f70bc9 97 }
vcoubard 1174:551333f70bc9 98
vcoubard 1174:551333f70bc9 99 /**
vcoubard 1179:4ab722f8dca0 100 * @return the advertisement interval in advertisement duration units (0.625ms units).
vcoubard 1174:551333f70bc9 101 */
vcoubard 1174:551333f70bc9 102 uint16_t getIntervalInADVUnits(void) const {
vcoubard 1174:551333f70bc9 103 return _interval;
vcoubard 1174:551333f70bc9 104 }
vcoubard 1174:551333f70bc9 105
vcoubard 1174:551333f70bc9 106 uint16_t getTimeout(void) const {
vcoubard 1174:551333f70bc9 107 return _timeout;
vcoubard 1174:551333f70bc9 108 }
vcoubard 1174:551333f70bc9 109
vcoubard 1179:4ab722f8dca0 110 void setAdvertisingType(AdvertisingType_t newAdvType) {_advType = newAdvType; }
vcoubard 1179:4ab722f8dca0 111 void setInterval(uint16_t newInterval) {_interval = MSEC_TO_ADVERTISEMENT_DURATION_UNITS(newInterval);}
vcoubard 1179:4ab722f8dca0 112 void setTimeout(uint16_t newTimeout) {_timeout = newTimeout; }
vcoubard 1174:551333f70bc9 113
vcoubard 1174:551333f70bc9 114 private:
vcoubard 1179:4ab722f8dca0 115 AdvertisingType_t _advType;
vcoubard 1179:4ab722f8dca0 116 uint16_t _interval; /* In ADV duration units (i.e. 0.625ms). */
vcoubard 1179:4ab722f8dca0 117 uint16_t _timeout; /* In seconds. */
vcoubard 1174:551333f70bc9 118 };
vcoubard 1174:551333f70bc9 119
vcoubard 1179:4ab722f8dca0 120 #endif // ifndef __GAP_ADVERTISING_PARAMS_H__