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:
Thu May 29 16:21:46 2014 +0100
Revision:
59:2c30cb482915
Parent:
58:0642c323b751
Child:
76:103fac6e36d1
GapAdvertisingData::addData() should use > for bounds checking of payload

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