VNG board

Fork of BLE_API by Bluetooth Low Energy

Committer:
rgrover1
Date:
Fri Dec 12 15:52:48 2014 +0000
Revision:
254:fb2a891a0d98
Parent:
106:a20be740075d
Synchronized with git rev c7d406c2
Author: Rohit Grover
ServiceDataPayload can have a maximum of 18 bytes of URIData.

fixes #6

Who changed what in which revision?

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