BLE API as used in the blog entry.

Dependents:   BLE_iBeacon_Exercise

Fork of BLE_API_Native by Kevin Townsend

Committer:
ktownsend
Date:
Thu Feb 06 11:51:06 2014 +0000
Revision:
0:4c3097c65247
Native mode drivers

Who changed what in which revision?

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