Bill Schilit / BLE_API

Fork of BLE_API by Bluetooth Low Energy

Committer:
ktownsend
Date:
Thu Jan 09 11:03:10 2014 +0000
Revision:
27:4a83843f04b0
Parent:
18:86fe1e247a54
Child:
31:2c94f0501807
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 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 /**************************************************************************/
ktownsend 2:ffc5216bd2cc 33 GapAdvertisingData::GapAdvertisingData(void)
ktownsend 2:ffc5216bd2cc 34 {
ktownsend 3:46de446e82ed 35 memset(_payload, 0, GAP_ADVERTISING_DATA_MAX_PAYLOAD);
ktownsend 3:46de446e82ed 36 _payloadLen = 0;
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
ktownsend 3:46de446e82ed 56
ktownsend 18:86fe1e247a54 57 \returns ble_error_t
ktownsend 3:46de446e82ed 58
ktownsend 18:86fe1e247a54 59 \retval BLE_ERROR_NONE
ktownsend 3:46de446e82ed 60 Everything executed properly
ktownsend 4:50a31ff5f974 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
ktownsend 4:50a31ff5f974 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 /**************************************************************************/
ktownsend 4:50a31ff5f974 73 ble_error_t GapAdvertisingData::addData(DataType advDataType, uint8_t * payload, uint8_t len)
ktownsend 4:50a31ff5f974 74 {
ktownsend 4:50a31ff5f974 75 /* ToDo: Check if an AD type already exists and if the existing */
ktownsend 4:50a31ff5f974 76 /* value is exclusive or not (flags, etc.) */
ktownsend 4:50a31ff5f974 77
ktownsend 4:50a31ff5f974 78 /* Make sure we don't exceed the 31 byte payload limit */
ktownsend 14:6ea5d1012a64 79 if (_payloadLen + len + 2 >= GAP_ADVERTISING_DATA_MAX_PAYLOAD)
ktownsend 4:50a31ff5f974 80 return BLE_ERROR_BUFFER_OVERFLOW;
ktownsend 14:6ea5d1012a64 81
ktownsend 14:6ea5d1012a64 82 /* Field length */
ktownsend 14:6ea5d1012a64 83 memset(&_payload[_payloadLen], len+1, 1);
ktownsend 14:6ea5d1012a64 84 _payloadLen++;
ktownsend 4:50a31ff5f974 85
ktownsend 14:6ea5d1012a64 86 /* Field ID */
ktownsend 14:6ea5d1012a64 87 memset(&_payload[_payloadLen], (uint8_t)advDataType, 1);
ktownsend 14:6ea5d1012a64 88 _payloadLen++;
ktownsend 14:6ea5d1012a64 89
ktownsend 14:6ea5d1012a64 90 /* Payload */
ktownsend 4:50a31ff5f974 91 memcpy(&_payload[_payloadLen], payload, len);
ktownsend 4:50a31ff5f974 92 _payloadLen += len;
ktownsend 4:50a31ff5f974 93
ktownsend 4:50a31ff5f974 94 return BLE_ERROR_NONE;
ktownsend 4:50a31ff5f974 95 }
ktownsend 4:50a31ff5f974 96
ktownsend 4:50a31ff5f974 97 /**************************************************************************/
ktownsend 4:50a31ff5f974 98 /*!
ktownsend 18:86fe1e247a54 99 \brief Helper function to add APPEARANCE data to the advertising
ktownsend 4:50a31ff5f974 100 payload
ktownsend 4:50a31ff5f974 101
ktownsend 18:86fe1e247a54 102 \args[in] appearance The APPEARANCE value to add
ktownsend 4:50a31ff5f974 103
ktownsend 18:86fe1e247a54 104 \returns ble_error_t
ktownsend 4:50a31ff5f974 105
ktownsend 18:86fe1e247a54 106 \retval BLE_ERROR_NONE
ktownsend 4:50a31ff5f974 107 Everything executed properly
ktownsend 4:50a31ff5f974 108
ktownsend 18:86fe1e247a54 109 \retval BLE_ERROR_BUFFER_OVERFLOW
ktownsend 3:46de446e82ed 110 The specified data would cause the advertising buffer
ktownsend 3:46de446e82ed 111 to overflow
ktownsend 3:46de446e82ed 112
ktownsend 18:86fe1e247a54 113 \par EXAMPLE
ktownsend 3:46de446e82ed 114
ktownsend 18:86fe1e247a54 115 \code
ktownsend 3:46de446e82ed 116
ktownsend 18:86fe1e247a54 117 \endcode
ktownsend 3:46de446e82ed 118 */
ktownsend 3:46de446e82ed 119 /**************************************************************************/
ktownsend 4:50a31ff5f974 120 ble_error_t GapAdvertisingData::addAppearance(Appearance appearance)
ktownsend 3:46de446e82ed 121 {
ktownsend 4:50a31ff5f974 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
ktownsend 5:7635f81a8e09 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
ktownsend 4:50a31ff5f974 144
ktownsend 18:86fe1e247a54 145 \retval BLE_ERROR_NONE
ktownsend 4:50a31ff5f974 146 Everything executed properly
ktownsend 4:50a31ff5f974 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
ktownsend 3:46de446e82ed 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 {
ktownsend 4:50a31ff5f974 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
ktownsend 4:50a31ff5f974 170
ktownsend 18:86fe1e247a54 171 \returns ble_error_t
ktownsend 4:50a31ff5f974 172
ktownsend 18:86fe1e247a54 173 \retval BLE_ERROR_NONE
ktownsend 4:50a31ff5f974 174 Everything executed properly
ktownsend 4:50a31ff5f974 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
ktownsend 3:46de446e82ed 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 */
ktownsend 4:50a31ff5f974 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
ktownsend 7:5e1f0d7f7c7d 207
ktownsend 18:86fe1e247a54 208 \returns A pointer to the payload
ktownsend 7:5e1f0d7f7c7d 209 */
ktownsend 7:5e1f0d7f7c7d 210 /**************************************************************************/
ktownsend 7:5e1f0d7f7c7d 211 uint8_t * GapAdvertisingData::getPayload(void)
ktownsend 7:5e1f0d7f7c7d 212 {
ktownsend 7:5e1f0d7f7c7d 213 return _payload;
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)
ktownsend 7:5e1f0d7f7c7d 219
ktownsend 18:86fe1e247a54 220 \returns The payload length in bytes
ktownsend 4:50a31ff5f974 221 */
ktownsend 4:50a31ff5f974 222 /**************************************************************************/
ktownsend 4:50a31ff5f974 223 uint8_t GapAdvertisingData::getPayloadLen(void)
ktownsend 4:50a31ff5f974 224 {
ktownsend 4:50a31ff5f974 225 return _payloadLen;
ktownsend 4:50a31ff5f974 226 }