High level Bluetooth Low Energy API and radio abstraction layer

Dependencies:   nRF51822

Dependents:   LinkNode_LIS3DH

Fork of BLE_API by Bluetooth Low Energy

Committer:
ktownsend
Date:
Thu Jan 09 11:03:10 2014 +0000
Revision:
27:4a83843f04b0
Parent:
19:a6f33421746c
Child:
34:da2ea8cd6216
Matching file names and apache headers

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ktownsend 27:4a83843f04b0 1 /* mbed Microcontroller Library
ktownsend 27:4a83843f04b0 2 * Copyright (c) 2006-2013 ARM Limited
ktownsend 27:4a83843f04b0 3 *
ktownsend 27:4a83843f04b0 4 * Licensed under the Apache License, Version 2.0 (the "License");
ktownsend 27:4a83843f04b0 5 * you may not use this file except in compliance with the License.
ktownsend 27:4a83843f04b0 6 * You may obtain a copy of the License at
ktownsend 27:4a83843f04b0 7 *
ktownsend 27:4a83843f04b0 8 * http://www.apache.org/licenses/LICENSE-2.0
ktownsend 27:4a83843f04b0 9 *
ktownsend 27:4a83843f04b0 10 * Unless required by applicable law or agreed to in writing, software
ktownsend 27:4a83843f04b0 11 * distributed under the License is distributed on an "AS IS" BASIS,
ktownsend 27:4a83843f04b0 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
ktownsend 27:4a83843f04b0 13 * See the License for the specific language governing permissions and
ktownsend 27:4a83843f04b0 14 * limitations under the License.
ktownsend 27:4a83843f04b0 15 */
ktownsend 27:4a83843f04b0 16
ktownsend 2:ffc5216bd2cc 17 #include <stdio.h>
ktownsend 2:ffc5216bd2cc 18 #include <string.h>
ktownsend 2:ffc5216bd2cc 19
ktownsend 4:50a31ff5f974 20 #include "blecommon.h"
ktownsend 2:ffc5216bd2cc 21 #include "GapAdvertisingParams.h"
ktownsend 2:ffc5216bd2cc 22
ktownsend 2:ffc5216bd2cc 23 /**************************************************************************/
ktownsend 2:ffc5216bd2cc 24 /*!
ktownsend 18:86fe1e247a54 25 \brief
ktownsend 18:86fe1e247a54 26 Instantiates a new GapAdvertisingParams instance
ktownsend 4:50a31ff5f974 27
ktownsend 18:86fe1e247a54 28 \param[in] advType
ktownsend 6:425638944835 29 The GAP advertising mode to use for this device. Valid
ktownsend 19:a6f33421746c 30 values are defined in AdvertisingType:
ktownsend 2:ffc5216bd2cc 31
ktownsend 18:86fe1e247a54 32 \par ADV_NON_CONNECTABLE_UNDIRECTED
ktownsend 18:86fe1e247a54 33 All connections to the peripheral device will be refused.
ktownsend 4:50a31ff5f974 34
ktownsend 18:86fe1e247a54 35 \par ADV_CONNECTABLE_DIRECTED
ktownsend 18:86fe1e247a54 36 Only connections from a pre-defined central device will be
ktownsend 18:86fe1e247a54 37 accepted.
ktownsend 4:50a31ff5f974 38
ktownsend 18:86fe1e247a54 39 \par ADV_CONNECTABLE_UNDIRECTED
ktownsend 18:86fe1e247a54 40 Any central device can connect to this peripheral.
ktownsend 6:425638944835 41
ktownsend 18:86fe1e247a54 42 \par ADV_SCANNABLE_UNDIRECTED
ktownsend 18:86fe1e247a54 43 Any central device can connect to this peripheral, and
ktownsend 18:86fe1e247a54 44 the secondary Scan Response payload will be included or
ktownsend 18:86fe1e247a54 45 available to central devices.
ktownsend 18:86fe1e247a54 46
ktownsend 19:a6f33421746c 47 \par
ktownsend 18:86fe1e247a54 48 See Bluetooth Core Specification 4.0 (Vol. 3), Part C,
ktownsend 18:86fe1e247a54 49 Section 9.3 and Core Specification 4.0 (Vol. 6), Part B,
ktownsend 18:86fe1e247a54 50 Section 2.3.1 for further information on GAP connection
ktownsend 18:86fe1e247a54 51 modes
ktownsend 4:50a31ff5f974 52
ktownsend 18:86fe1e247a54 53 \param[in] interval
ktownsend 14:6ea5d1012a64 54 Advertising interval between 0x0020 and 0x4000 in 0.625ms
ktownsend 14:6ea5d1012a64 55 units (20ms to 10.24s). If using non-connectable mode
ktownsend 18:86fe1e247a54 56 (ADV_NON_CONNECTABLE_UNDIRECTED) this min value is
ktownsend 14:6ea5d1012a64 57 0x00A0 (100ms).
ktownsend 4:50a31ff5f974 58
ktownsend 18:86fe1e247a54 59 \par
ktownsend 4:50a31ff5f974 60 Increasing this value will allow central devices to detect
ktownsend 4:50a31ff5f974 61 your peripheral faster at the expense of more power being
ktownsend 4:50a31ff5f974 62 used by the radio due to the higher data transmit rate.
ktownsend 4:50a31ff5f974 63
ktownsend 19:a6f33421746c 64 \par
ktownsend 18:86fe1e247a54 65 This field must be set to 0 if connectionMode is equal
ktownsend 18:86fe1e247a54 66 to ADV_CONNECTABLE_DIRECTED
ktownsend 14:6ea5d1012a64 67
ktownsend 19:a6f33421746c 68 \par
ktownsend 18:86fe1e247a54 69 See Bluetooth Core Specification, Vol 3., Part C,
ktownsend 19:a6f33421746c 70 Appendix A for suggested advertising intervals.
ktownsend 14:6ea5d1012a64 71
ktownsend 18:86fe1e247a54 72 \param[in] timeout
ktownsend 4:50a31ff5f974 73 Advertising timeout between 0x1 and 0x3FFF (1 and 16383)
ktownsend 4:50a31ff5f974 74 in seconds. Enter 0 to disable the advertising timeout.
ktownsend 4:50a31ff5f974 75
ktownsend 18:86fe1e247a54 76 \par EXAMPLE
ktownsend 2:ffc5216bd2cc 77
ktownsend 18:86fe1e247a54 78 \code
ktownsend 2:ffc5216bd2cc 79
ktownsend 18:86fe1e247a54 80 \endcode
ktownsend 2:ffc5216bd2cc 81 */
ktownsend 2:ffc5216bd2cc 82 /**************************************************************************/
ktownsend 6:425638944835 83 GapAdvertisingParams::GapAdvertisingParams(AdvertisingType advType, uint16_t interval, uint16_t timeout)
ktownsend 2:ffc5216bd2cc 84 {
ktownsend 6:425638944835 85 _advType = advType;
ktownsend 2:ffc5216bd2cc 86 _interval = interval;
ktownsend 2:ffc5216bd2cc 87 _timeout = timeout;
ktownsend 4:50a31ff5f974 88
ktownsend 4:50a31ff5f974 89 /* Interval checks */
ktownsend 6:425638944835 90 if (_advType == ADV_CONNECTABLE_DIRECTED)
ktownsend 4:50a31ff5f974 91 {
ktownsend 4:50a31ff5f974 92 /* Interval must be 0 in directed connectable mode */
ktownsend 4:50a31ff5f974 93 _interval = 0;
ktownsend 4:50a31ff5f974 94 }
ktownsend 14:6ea5d1012a64 95 else if (_advType == ADV_NON_CONNECTABLE_UNDIRECTED)
ktownsend 14:6ea5d1012a64 96 {
ktownsend 14:6ea5d1012a64 97 /* Min interval is slightly larger than in other modes */
ktownsend 14:6ea5d1012a64 98 if (_interval < GAP_ADV_PARAMS_INTERVAL_MIN_NONCON)
ktownsend 14:6ea5d1012a64 99 {
ktownsend 14:6ea5d1012a64 100 _interval = GAP_ADV_PARAMS_INTERVAL_MIN_NONCON;
ktownsend 14:6ea5d1012a64 101 }
ktownsend 14:6ea5d1012a64 102 if (_interval > GAP_ADV_PARAMS_INTERVAL_MAX)
ktownsend 14:6ea5d1012a64 103 {
ktownsend 14:6ea5d1012a64 104 _interval = GAP_ADV_PARAMS_INTERVAL_MAX;
ktownsend 14:6ea5d1012a64 105 }
ktownsend 14:6ea5d1012a64 106 }
ktownsend 4:50a31ff5f974 107 else
ktownsend 4:50a31ff5f974 108 {
ktownsend 4:50a31ff5f974 109 /* Stay within interval limits */
ktownsend 4:50a31ff5f974 110 if (_interval < GAP_ADV_PARAMS_INTERVAL_MIN)
ktownsend 4:50a31ff5f974 111 {
ktownsend 4:50a31ff5f974 112 _interval = GAP_ADV_PARAMS_INTERVAL_MIN;
ktownsend 4:50a31ff5f974 113 }
ktownsend 4:50a31ff5f974 114 if (_interval > GAP_ADV_PARAMS_INTERVAL_MAX)
ktownsend 4:50a31ff5f974 115 {
ktownsend 4:50a31ff5f974 116 _interval = GAP_ADV_PARAMS_INTERVAL_MAX;
ktownsend 4:50a31ff5f974 117 }
ktownsend 4:50a31ff5f974 118 }
ktownsend 4:50a31ff5f974 119
ktownsend 4:50a31ff5f974 120 /* Timeout checks */
ktownsend 4:50a31ff5f974 121 if (timeout)
ktownsend 4:50a31ff5f974 122 {
ktownsend 4:50a31ff5f974 123 /* Stay within timeout limits */
ktownsend 4:50a31ff5f974 124 if (_timeout > GAP_ADV_PARAMS_TIMEOUT_MAX)
ktownsend 4:50a31ff5f974 125 {
ktownsend 4:50a31ff5f974 126 _timeout = GAP_ADV_PARAMS_TIMEOUT_MAX;
ktownsend 4:50a31ff5f974 127 }
ktownsend 4:50a31ff5f974 128 }
ktownsend 2:ffc5216bd2cc 129 }
ktownsend 2:ffc5216bd2cc 130
ktownsend 2:ffc5216bd2cc 131 /**************************************************************************/
ktownsend 2:ffc5216bd2cc 132 /*!
ktownsend 2:ffc5216bd2cc 133 Destructor
ktownsend 2:ffc5216bd2cc 134 */
ktownsend 2:ffc5216bd2cc 135 /**************************************************************************/
ktownsend 2:ffc5216bd2cc 136 GapAdvertisingParams::~GapAdvertisingParams(void)
ktownsend 2:ffc5216bd2cc 137 {
ktownsend 2:ffc5216bd2cc 138 }
ktownsend 7:5e1f0d7f7c7d 139
ktownsend 7:5e1f0d7f7c7d 140 /**************************************************************************/
ktownsend 7:5e1f0d7f7c7d 141 /*!
ktownsend 18:86fe1e247a54 142 \brief returns the current Advertising Type value
ktownsend 7:5e1f0d7f7c7d 143 */
ktownsend 7:5e1f0d7f7c7d 144 /**************************************************************************/
ktownsend 7:5e1f0d7f7c7d 145 GapAdvertisingParams::AdvertisingType GapAdvertisingParams::getAdvertisingType(void)
ktownsend 7:5e1f0d7f7c7d 146 {
ktownsend 7:5e1f0d7f7c7d 147 return _advType;
ktownsend 7:5e1f0d7f7c7d 148 }
ktownsend 9:124ae067ae27 149
ktownsend 9:124ae067ae27 150 /**************************************************************************/
ktownsend 9:124ae067ae27 151 /*!
ktownsend 18:86fe1e247a54 152 \brief returns the current Advertising Delay (in units of 0.625ms)
ktownsend 9:124ae067ae27 153 */
ktownsend 9:124ae067ae27 154 /**************************************************************************/
ktownsend 9:124ae067ae27 155 uint16_t GapAdvertisingParams::getInterval(void)
ktownsend 9:124ae067ae27 156 {
ktownsend 9:124ae067ae27 157 return _interval;
ktownsend 9:124ae067ae27 158 }
ktownsend 9:124ae067ae27 159
ktownsend 9:124ae067ae27 160 /**************************************************************************/
ktownsend 9:124ae067ae27 161 /*!
ktownsend 18:86fe1e247a54 162 \brief returns the current Advertising Timeout (in seconds)
ktownsend 9:124ae067ae27 163 */
ktownsend 9:124ae067ae27 164 /**************************************************************************/
ktownsend 9:124ae067ae27 165 uint16_t GapAdvertisingParams::getTimeout(void)
ktownsend 9:124ae067ae27 166 {
ktownsend 9:124ae067ae27 167 return _timeout;
ktownsend 9:124ae067ae27 168 }