テスト用です。

Dependencies:   mbed

Committer:
jksoft
Date:
Tue Oct 11 11:09:42 2016 +0000
Revision:
0:8468a4403fea
SB??ver;

Who changed what in which revision?

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