my version with changed conversion between duration units

Fork of BLE_API by Bluetooth Low Energy

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers GapAdvertisingParams.h Source File

GapAdvertisingParams.h

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015 */
00016 
00017 #ifndef __GAP_ADVERTISING_PARAMS_H__
00018 #define __GAP_ADVERTISING_PARAMS_H__
00019 
00020 
00021 /**************************************************************************/
00022 /*!
00023     \brief
00024     This class provides a wrapper for the core advertising parameters,
00025     including the advertising type (Connectable Undirected,
00026     Non Connectable Undirected, etc.), as well as the advertising and
00027     timeout intervals.
00028 
00029     \par
00030     See the following for more information on advertising types:
00031 
00032     \li \c Bluetooth Core Specification 4.0 (Vol. 6), Part B, Section 2.3.1
00033     \li \c Bluetooth Core Specification 4.0 (Vol. 3), Part C, Section 9.3
00034 */
00035 /**************************************************************************/
00036 class GapAdvertisingParams {
00037 public:
00038     static const unsigned GAP_ADV_PARAMS_INTERVAL_MIN        = 0x0020;
00039     static const unsigned GAP_ADV_PARAMS_INTERVAL_MIN_NONCON = 0x00A0;
00040     static const unsigned GAP_ADV_PARAMS_INTERVAL_MAX        = 0x4000;
00041     static const unsigned GAP_ADV_PARAMS_TIMEOUT_MAX         = 0x3FFF;
00042 
00043     /*!
00044      * Encapsulates the peripheral advertising modes, which determine how
00045      * the device appears to other central devices in hearing range
00046      */
00047     enum AdvertisingType_t  {
00048         ADV_CONNECTABLE_UNDIRECTED,     /**< Vol 3, Part C, Section 9.3.4 and Vol 6, Part B, Section 2.3.1.1 */
00049         ADV_CONNECTABLE_DIRECTED,       /**< Vol 3, Part C, Section 9.3.3 and Vol 6, Part B, Section 2.3.1.2 */
00050         ADV_SCANNABLE_UNDIRECTED,       /**< Include support for Scan Response payloads, see Vol 6, Part B, Section 2.3.1.4 */
00051         ADV_NON_CONNECTABLE_UNDIRECTED  /**< Vol 3, Part C, Section 9.3.2 and Vol 6, Part B, Section 2.3.1.3 */
00052     };
00053     typedef enum AdvertisingType_t  AdvertisingType; /* deprecated type alias. */
00054 
00055 public:
00056     GapAdvertisingParams(AdvertisingType_t  advType  = ADV_CONNECTABLE_UNDIRECTED,
00057                          uint16_t          interval = GAP_ADV_PARAMS_INTERVAL_MIN_NONCON,
00058                          uint16_t          timeout  = 0) : _advType(advType), _interval(interval), _timeout(timeout) {
00059         /* Interval checks */
00060         if (_advType == ADV_CONNECTABLE_DIRECTED) {
00061             /* Interval must be 0 in directed connectable mode */
00062             _interval = 0;
00063         } else if (_advType == ADV_NON_CONNECTABLE_UNDIRECTED) {
00064             /* Min interval is slightly larger than in other modes */
00065             if (_interval < GAP_ADV_PARAMS_INTERVAL_MIN_NONCON) {
00066                 _interval = GAP_ADV_PARAMS_INTERVAL_MIN_NONCON;
00067             }
00068             if (_interval > GAP_ADV_PARAMS_INTERVAL_MAX) {
00069                 _interval = GAP_ADV_PARAMS_INTERVAL_MAX;
00070             }
00071         } else {
00072             /* Stay within interval limits */
00073             if (_interval < GAP_ADV_PARAMS_INTERVAL_MIN) {
00074                 _interval = GAP_ADV_PARAMS_INTERVAL_MIN;
00075             }
00076             if (_interval > GAP_ADV_PARAMS_INTERVAL_MAX) {
00077                 _interval = GAP_ADV_PARAMS_INTERVAL_MAX;
00078             }
00079         }
00080 
00081         /* Timeout checks */
00082         if (timeout) {
00083             /* Stay within timeout limits */
00084             if (_timeout > GAP_ADV_PARAMS_TIMEOUT_MAX) {
00085                 _timeout = GAP_ADV_PARAMS_TIMEOUT_MAX;
00086             }
00087         }
00088     }
00089     
00090     static const uint16_t UNIT_1_25_MS  = 1250; /**< Number of microseconds in 1.25 milliseconds. */
00091     static const uint16_t UNIT_0_625_MS = 625;  /**< Number of microseconds in 0.625 milliseconds. */
00092     static uint16_t MSEC_TO_GAP_DURATION_UNITS(uint32_t durationInMillis) {
00093         return (durationInMillis * 1000) / UNIT_1_25_MS;
00094     }
00095     static uint16_t MSEC_TO_ADVERTISEMENT_DURATION_UNITS(uint32_t durationInMillis) {
00096         return (durationInMillis * 1000) / UNIT_0_625_MS;
00097     }
00098     static uint16_t ADVERTISEMENT_DURATION_UNITS_TO_MS(uint16_t gapUnits) {
00099         return (gapUnits * UNIT_0_625_MS) / 1000;
00100     }
00101 
00102     AdvertisingType_t  getAdvertisingType(void) const {return _advType; }
00103     uint16_t          getInterval(void)        const {return _interval;}
00104     uint16_t          getTimeout(void)         const {return _timeout; }
00105 
00106     void setAdvertisingType(AdvertisingType_t  newAdvType) {_advType = newAdvType;  }
00107     void setInterval(uint16_t newInterval)                {_interval = ADVERTISEMENT_DURATION_UNITS_TO_MS(newInterval);}
00108     void setTimeout(uint16_t newTimeout)                  {_timeout = newTimeout;  }
00109 
00110 private:
00111     AdvertisingType_t  _advType;
00112     uint16_t          _interval;
00113     uint16_t          _timeout;
00114 };
00115 
00116 #endif // ifndef __GAP_ADVERTISING_PARAMS_H__