High level Bluetooth Low Energy API and radio abstraction layer

Dependencies:   nRF51822

Dependents:   LinkNode_LIS3DH

Fork of BLE_API by Bluetooth Low Energy

Committer:
Rohit Grover
Date:
Mon Jun 09 08:29:22 2014 +0100
Revision:
76:103fac6e36d1
Parent:
59:2c30cb482915
Child:
96:6f4c8e545d38
slowly switching to astyle code formatting as recommended by the team

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 */
Rohit Grover 34:da2ea8cd6216 16
ktownsend 2:ffc5216bd2cc 17 #include <stdio.h>
ktownsend 2:ffc5216bd2cc 18 #include <string.h>
ktownsend 2:ffc5216bd2cc 19
ktownsend 2:ffc5216bd2cc 20 #include "GapAdvertisingData.h"
ktownsend 2:ffc5216bd2cc 21
ktownsend 2:ffc5216bd2cc 22 /**************************************************************************/
ktownsend 2:ffc5216bd2cc 23 /*!
ktownsend 18:86fe1e247a54 24 \brief Creates a new GapAdvertisingData instance
ktownsend 2:ffc5216bd2cc 25
ktownsend 18:86fe1e247a54 26 \par EXAMPLE
ktownsend 2:ffc5216bd2cc 27
ktownsend 18:86fe1e247a54 28 \code
ktownsend 2:ffc5216bd2cc 29
ktownsend 18:86fe1e247a54 30 \endcode
ktownsend 2:ffc5216bd2cc 31 */
ktownsend 2:ffc5216bd2cc 32 /**************************************************************************/
Rohit Grover 76:103fac6e36d1 33 GapAdvertisingData::GapAdvertisingData(void) : _payload(), _payloadLen(0), _appearance(GENERIC_TAG) {
Rohit Grover 76:103fac6e36d1 34 /* empty */
ktownsend 2:ffc5216bd2cc 35 }
ktownsend 2:ffc5216bd2cc 36
ktownsend 2:ffc5216bd2cc 37 /**************************************************************************/
ktownsend 2:ffc5216bd2cc 38 /*!
ktownsend 2:ffc5216bd2cc 39 Destructor
ktownsend 2:ffc5216bd2cc 40 */
ktownsend 2:ffc5216bd2cc 41 /**************************************************************************/
ktownsend 2:ffc5216bd2cc 42 GapAdvertisingData::~GapAdvertisingData(void)
ktownsend 2:ffc5216bd2cc 43 {
ktownsend 2:ffc5216bd2cc 44 }
ktownsend 3:46de446e82ed 45
ktownsend 3:46de446e82ed 46 /**************************************************************************/
ktownsend 3:46de446e82ed 47 /*!
ktownsend 18:86fe1e247a54 48 \brief Adds advertising data based on the specified AD type (see
ktownsend 18:86fe1e247a54 49 DataType)
ktownsend 3:46de446e82ed 50
ktownsend 18:86fe1e247a54 51 \args[in] advDataType The Advertising 'DataType' to add
ktownsend 18:86fe1e247a54 52 \args[in] payload Pointer to the payload contents
ktownsend 18:86fe1e247a54 53 \args[in] len Size of the payload in bytes
Rohit Grover 34:da2ea8cd6216 54
ktownsend 18:86fe1e247a54 55 \returns ble_error_t
Rohit Grover 34:da2ea8cd6216 56
ktownsend 18:86fe1e247a54 57 \retval BLE_ERROR_NONE
ktownsend 3:46de446e82ed 58 Everything executed properly
Rohit Grover 34:da2ea8cd6216 59
ktownsend 18:86fe1e247a54 60 \retval BLE_ERROR_BUFFER_OVERFLOW
ktownsend 4:50a31ff5f974 61 The specified data would cause the advertising buffer
ktownsend 4:50a31ff5f974 62 to overflow
Rohit Grover 34:da2ea8cd6216 63
ktownsend 18:86fe1e247a54 64 \par EXAMPLE
ktownsend 4:50a31ff5f974 65
ktownsend 18:86fe1e247a54 66 \code
ktownsend 4:50a31ff5f974 67
ktownsend 18:86fe1e247a54 68 \endcode
ktownsend 4:50a31ff5f974 69 */
ktownsend 4:50a31ff5f974 70 /**************************************************************************/
Rohit Grover 76:103fac6e36d1 71 ble_error_t GapAdvertisingData::addData(DataType advDataType, const uint8_t *payload, uint8_t len)
ktownsend 4:50a31ff5f974 72 {
ktownsend 4:50a31ff5f974 73 /* ToDo: Check if an AD type already exists and if the existing */
ktownsend 4:50a31ff5f974 74 /* value is exclusive or not (flags, etc.) */
Rohit Grover 34:da2ea8cd6216 75
ktownsend 4:50a31ff5f974 76 /* Make sure we don't exceed the 31 byte payload limit */
Rohit Grover 59:2c30cb482915 77 if (_payloadLen + len + 2 > GAP_ADVERTISING_DATA_MAX_PAYLOAD) {
ktownsend 4:50a31ff5f974 78 return BLE_ERROR_BUFFER_OVERFLOW;
Rohit Grover 34:da2ea8cd6216 79 }
ktownsend 14:6ea5d1012a64 80
ktownsend 14:6ea5d1012a64 81 /* Field length */
Rohit Grover 34:da2ea8cd6216 82 memset(&_payload[_payloadLen], len + 1, 1);
ktownsend 14:6ea5d1012a64 83 _payloadLen++;
Rohit Grover 34:da2ea8cd6216 84
ktownsend 14:6ea5d1012a64 85 /* Field ID */
Rohit Grover 34:da2ea8cd6216 86 memset(&_payload[_payloadLen], (uint8_t)advDataType, 1);
ktownsend 14:6ea5d1012a64 87 _payloadLen++;
Rohit Grover 34:da2ea8cd6216 88
ktownsend 14:6ea5d1012a64 89 /* Payload */
ktownsend 4:50a31ff5f974 90 memcpy(&_payload[_payloadLen], payload, len);
ktownsend 4:50a31ff5f974 91 _payloadLen += len;
Rohit Grover 34:da2ea8cd6216 92
ktownsend 4:50a31ff5f974 93 return BLE_ERROR_NONE;
ktownsend 4:50a31ff5f974 94 }
ktownsend 4:50a31ff5f974 95
ktownsend 4:50a31ff5f974 96 /**************************************************************************/
ktownsend 4:50a31ff5f974 97 /*!
ktownsend 18:86fe1e247a54 98 \brief Helper function to add APPEARANCE data to the advertising
ktownsend 4:50a31ff5f974 99 payload
ktownsend 4:50a31ff5f974 100
ktownsend 18:86fe1e247a54 101 \args[in] appearance The APPEARANCE value to add
Rohit Grover 34:da2ea8cd6216 102
ktownsend 18:86fe1e247a54 103 \returns ble_error_t
Rohit Grover 34:da2ea8cd6216 104
ktownsend 18:86fe1e247a54 105 \retval BLE_ERROR_NONE
ktownsend 4:50a31ff5f974 106 Everything executed properly
Rohit Grover 34:da2ea8cd6216 107
ktownsend 18:86fe1e247a54 108 \retval BLE_ERROR_BUFFER_OVERFLOW
ktownsend 3:46de446e82ed 109 The specified data would cause the advertising buffer
ktownsend 3:46de446e82ed 110 to overflow
Rohit Grover 34:da2ea8cd6216 111
ktownsend 18:86fe1e247a54 112 \par EXAMPLE
ktownsend 3:46de446e82ed 113
ktownsend 18:86fe1e247a54 114 \code
ktownsend 3:46de446e82ed 115
ktownsend 18:86fe1e247a54 116 \endcode
ktownsend 3:46de446e82ed 117 */
ktownsend 3:46de446e82ed 118 /**************************************************************************/
ktownsend 4:50a31ff5f974 119 ble_error_t GapAdvertisingData::addAppearance(Appearance appearance)
ktownsend 3:46de446e82ed 120 {
bogdanm 31:2c94f0501807 121 _appearance = appearance;
Rohit Grover 34:da2ea8cd6216 122 return addData(GapAdvertisingData::APPEARANCE, (uint8_t *)&appearance, 2);
ktownsend 4:50a31ff5f974 123 }
ktownsend 4:50a31ff5f974 124
ktownsend 4:50a31ff5f974 125 /**************************************************************************/
ktownsend 4:50a31ff5f974 126 /*!
ktownsend 18:86fe1e247a54 127 \brief Helper function to add FLAGS data to the advertising
ktownsend 4:50a31ff5f974 128 payload
ktownsend 4:50a31ff5f974 129
ktownsend 18:86fe1e247a54 130 \args[in] flag The FLAGS value to add
ktownsend 4:50a31ff5f974 131
ktownsend 18:86fe1e247a54 132 \par LE_LIMITED_DISCOVERABLE
ktownsend 18:86fe1e247a54 133 The peripheral is discoverable for a limited period of
ktownsend 18:86fe1e247a54 134 time
ktownsend 4:50a31ff5f974 135
ktownsend 18:86fe1e247a54 136 \par LE_GENERAL_DISCOVERABLE
ktownsend 18:86fe1e247a54 137 The peripheral is permanently discoverable
Rohit Grover 34:da2ea8cd6216 138
ktownsend 18:86fe1e247a54 139 \par BREDR_NOT_SUPPORTED
ktownsend 18:86fe1e247a54 140 This peripheral is a Bluetooth Low Energy only device
ktownsend 18:86fe1e247a54 141 (no EDR support)
ktownsend 4:50a31ff5f974 142
ktownsend 18:86fe1e247a54 143 \returns ble_error_t
Rohit Grover 34:da2ea8cd6216 144
ktownsend 18:86fe1e247a54 145 \retval BLE_ERROR_NONE
ktownsend 4:50a31ff5f974 146 Everything executed properly
Rohit Grover 34:da2ea8cd6216 147
ktownsend 18:86fe1e247a54 148 \retval BLE_ERROR_BUFFER_OVERFLOW
ktownsend 4:50a31ff5f974 149 The specified data would cause the advertising buffer
ktownsend 4:50a31ff5f974 150 to overflow
Rohit Grover 34:da2ea8cd6216 151
ktownsend 18:86fe1e247a54 152 \par EXAMPLE
ktownsend 4:50a31ff5f974 153
ktownsend 18:86fe1e247a54 154 \code
ktownsend 4:50a31ff5f974 155
ktownsend 18:86fe1e247a54 156 \endcode
ktownsend 4:50a31ff5f974 157 */
ktownsend 4:50a31ff5f974 158 /**************************************************************************/
ktownsend 4:50a31ff5f974 159 ble_error_t GapAdvertisingData::addFlags(Flags flag)
ktownsend 4:50a31ff5f974 160 {
Rohit Grover 34:da2ea8cd6216 161 return addData(GapAdvertisingData::FLAGS, (uint8_t *)&flag, 1);
ktownsend 4:50a31ff5f974 162 }
ktownsend 4:50a31ff5f974 163
ktownsend 4:50a31ff5f974 164 /**************************************************************************/
ktownsend 4:50a31ff5f974 165 /*!
ktownsend 18:86fe1e247a54 166 \brief Helper function to add TX_POWER_LEVEL data to the
ktownsend 4:50a31ff5f974 167 advertising payload
ktownsend 4:50a31ff5f974 168
ktownsend 18:86fe1e247a54 169 \args[in] flag The TX_POWER_LEVEL value to add
Rohit Grover 34:da2ea8cd6216 170
ktownsend 18:86fe1e247a54 171 \returns ble_error_t
Rohit Grover 34:da2ea8cd6216 172
ktownsend 18:86fe1e247a54 173 \retval BLE_ERROR_NONE
ktownsend 4:50a31ff5f974 174 Everything executed properly
Rohit Grover 34:da2ea8cd6216 175
ktownsend 18:86fe1e247a54 176 \retval BLE_ERROR_BUFFER_OVERFLOW
ktownsend 4:50a31ff5f974 177 The specified data would cause the advertising buffer
ktownsend 4:50a31ff5f974 178 to overflow
Rohit Grover 34:da2ea8cd6216 179
ktownsend 18:86fe1e247a54 180 \par EXAMPLE
ktownsend 4:50a31ff5f974 181
ktownsend 18:86fe1e247a54 182 \code
ktownsend 4:50a31ff5f974 183
ktownsend 18:86fe1e247a54 184 \endcode
ktownsend 4:50a31ff5f974 185 */
ktownsend 4:50a31ff5f974 186 /**************************************************************************/
ktownsend 4:50a31ff5f974 187 ble_error_t GapAdvertisingData::addTxPower(int8_t txPower)
ktownsend 4:50a31ff5f974 188 {
ktownsend 4:50a31ff5f974 189 /* ToDo: Basic error checking to make sure txPower is in range */
Rohit Grover 34:da2ea8cd6216 190 return addData(GapAdvertisingData::TX_POWER_LEVEL, (uint8_t *)&txPower, 1);
ktownsend 3:46de446e82ed 191 }
ktownsend 4:50a31ff5f974 192
ktownsend 4:50a31ff5f974 193 /**************************************************************************/
ktownsend 4:50a31ff5f974 194 /*!
ktownsend 18:86fe1e247a54 195 \brief Clears the payload and resets the payload length counter
ktownsend 4:50a31ff5f974 196 */
ktownsend 4:50a31ff5f974 197 /**************************************************************************/
ktownsend 4:50a31ff5f974 198 void GapAdvertisingData::clear(void)
ktownsend 4:50a31ff5f974 199 {
ktownsend 4:50a31ff5f974 200 memset(&_payload, 0, GAP_ADVERTISING_DATA_MAX_PAYLOAD);
ktownsend 4:50a31ff5f974 201 _payloadLen = 0;
ktownsend 4:50a31ff5f974 202 }
ktownsend 4:50a31ff5f974 203
ktownsend 4:50a31ff5f974 204 /**************************************************************************/
ktownsend 4:50a31ff5f974 205 /*!
ktownsend 18:86fe1e247a54 206 \brief Returns a pointer to the the current payload
Rohit Grover 34:da2ea8cd6216 207
ktownsend 18:86fe1e247a54 208 \returns A pointer to the payload
ktownsend 7:5e1f0d7f7c7d 209 */
ktownsend 7:5e1f0d7f7c7d 210 /**************************************************************************/
Rohit Grover 40:d405c9b1419d 211 const uint8_t *GapAdvertisingData::getPayload(void) const
ktownsend 7:5e1f0d7f7c7d 212 {
bogdanm 31:2c94f0501807 213 return (_payloadLen > 0) ? _payload : NULL;
ktownsend 7:5e1f0d7f7c7d 214 }
ktownsend 7:5e1f0d7f7c7d 215
ktownsend 7:5e1f0d7f7c7d 216 /**************************************************************************/
ktownsend 7:5e1f0d7f7c7d 217 /*!
ktownsend 18:86fe1e247a54 218 \brief Returns the current payload length (0..31 bytes)
Rohit Grover 34:da2ea8cd6216 219
ktownsend 18:86fe1e247a54 220 \returns The payload length in bytes
ktownsend 4:50a31ff5f974 221 */
ktownsend 4:50a31ff5f974 222 /**************************************************************************/
Rohit Grover 40:d405c9b1419d 223 uint8_t GapAdvertisingData::getPayloadLen(void) const
ktownsend 4:50a31ff5f974 224 {
ktownsend 4:50a31ff5f974 225 return _payloadLen;
ktownsend 4:50a31ff5f974 226 }
bogdanm 31:2c94f0501807 227
bogdanm 31:2c94f0501807 228 /**************************************************************************/
bogdanm 31:2c94f0501807 229 /*!
bogdanm 31:2c94f0501807 230 \brief Returns the 16-bit appearance value for this device
Rohit Grover 34:da2ea8cd6216 231
bogdanm 31:2c94f0501807 232 \returns The 16-bit appearance value
bogdanm 31:2c94f0501807 233 */
bogdanm 31:2c94f0501807 234 /**************************************************************************/
Rohit Grover 40:d405c9b1419d 235 uint16_t GapAdvertisingData::getAppearance(void) const
bogdanm 31:2c94f0501807 236 {
bogdanm 31:2c94f0501807 237 return (uint16_t)_appearance;
bogdanm 31:2c94f0501807 238 }