Yutaka Yoshida / Mbed 2 deprecated BLE_WallbotBLE_Challenge

Dependencies:   mbed

Fork of BLE_WallbotBLE_Challenge by JKSoft

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers GapAdvertisingData.cpp Source File

GapAdvertisingData.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include <stdio.h>
00018 #include <string.h>
00019 
00020 #include "GapAdvertisingData.h"
00021 
00022 /**************************************************************************/
00023 /*!
00024     \brief  Creates a new GapAdvertisingData instance
00025 
00026     \par EXAMPLE
00027 
00028     \code
00029 
00030     \endcode
00031 */
00032 /**************************************************************************/
00033 GapAdvertisingData::GapAdvertisingData(void) : _payload(), _payloadLen(0), _appearance(GENERIC_TAG) {
00034     /* empty */
00035 }
00036 
00037 /**************************************************************************/
00038 /*!
00039     Destructor
00040 */
00041 /**************************************************************************/
00042 GapAdvertisingData::~GapAdvertisingData (void)
00043 {
00044 }
00045 
00046 /**************************************************************************/
00047 /*!
00048     \brief  Adds advertising data based on the specified AD type (see
00049             DataType)
00050 
00051     \args[in]   advDataType The Advertising 'DataType' to add
00052     \args[in]   payload     Pointer to the payload contents
00053     \args[in]   len         Size of the payload in bytes
00054 
00055     \returns    ble_error_t
00056 
00057     \retval     BLE_ERROR_NONE
00058                 Everything executed properly
00059 
00060     \retval     BLE_ERROR_BUFFER_OVERFLOW
00061                 The specified data would cause the advertising buffer
00062                 to overflow
00063 
00064     \par EXAMPLE
00065 
00066     \code
00067 
00068     \endcode
00069 */
00070 /**************************************************************************/
00071 ble_error_t GapAdvertisingData::addData(DataType advDataType, const uint8_t *payload, uint8_t len)
00072 {
00073     /* ToDo: Check if an AD type already exists and if the existing */
00074     /*       value is exclusive or not (flags, etc.) */
00075 
00076     /* Make sure we don't exceed the 31 byte payload limit */
00077     if (_payloadLen + len + 2 > GAP_ADVERTISING_DATA_MAX_PAYLOAD) {
00078         return BLE_ERROR_BUFFER_OVERFLOW;
00079     }
00080 
00081     /* Field length */
00082     memset(&_payload[_payloadLen], len + 1, 1);
00083     _payloadLen++;
00084 
00085     /* Field ID */
00086     memset(&_payload[_payloadLen], (uint8_t)advDataType, 1);
00087     _payloadLen++;
00088 
00089     /* Payload */
00090     memcpy(&_payload[_payloadLen], payload, len);
00091     _payloadLen += len;
00092 
00093     return BLE_ERROR_NONE;
00094 }
00095 
00096 /**************************************************************************/
00097 /*!
00098     \brief  Helper function to add APPEARANCE data to the advertising
00099             payload
00100 
00101     \args[in]   appearance  The APPEARANCE value to add
00102 
00103     \returns    ble_error_t
00104 
00105     \retval     BLE_ERROR_NONE
00106                 Everything executed properly
00107 
00108     \retval     BLE_ERROR_BUFFER_OVERFLOW
00109                 The specified data would cause the advertising buffer
00110                 to overflow
00111 
00112     \par EXAMPLE
00113 
00114     \code
00115 
00116     \endcode
00117 */
00118 /**************************************************************************/
00119 ble_error_t GapAdvertisingData::addAppearance(Appearance appearance)
00120 {
00121     _appearance = appearance;
00122     return addData(GapAdvertisingData::APPEARANCE, (uint8_t *)&appearance, 2);
00123 }
00124 
00125 /**************************************************************************/
00126 /*!
00127     \brief  Helper function to add FLAGS data to the advertising
00128             payload
00129 
00130     \args[in]   flag  The FLAGS value to add
00131 
00132                 \par LE_LIMITED_DISCOVERABLE
00133                 The peripheral is discoverable for a limited period of
00134                 time
00135 
00136                 \par LE_GENERAL_DISCOVERABLE
00137                 The peripheral is permanently discoverable
00138 
00139                 \par BREDR_NOT_SUPPORTED
00140                 This peripheral is a Bluetooth Low Energy only device
00141                 (no EDR support)
00142 
00143     \returns    ble_error_t
00144 
00145     \retval     BLE_ERROR_NONE
00146                 Everything executed properly
00147 
00148     \retval     BLE_ERROR_BUFFER_OVERFLOW
00149                 The specified data would cause the advertising buffer
00150                 to overflow
00151 
00152     \par EXAMPLE
00153 
00154     \code
00155 
00156     \endcode
00157 */
00158 /**************************************************************************/
00159 ble_error_t GapAdvertisingData::addFlags(uint8_t flags)
00160 {
00161     return addData(GapAdvertisingData::FLAGS, &flags, 1);
00162 }
00163 
00164 /**************************************************************************/
00165 /*!
00166     \brief  Helper function to add TX_POWER_LEVEL data to the
00167             advertising payload
00168 
00169     \args[in]   flag  The TX_POWER_LEVEL value to add
00170 
00171     \returns    ble_error_t
00172 
00173     \retval     BLE_ERROR_NONE
00174                 Everything executed properly
00175 
00176     \retval     BLE_ERROR_BUFFER_OVERFLOW
00177                 The specified data would cause the advertising buffer
00178                 to overflow
00179 
00180     \par EXAMPLE
00181 
00182     \code
00183 
00184     \endcode
00185 */
00186 /**************************************************************************/
00187 ble_error_t GapAdvertisingData::addTxPower(int8_t txPower)
00188 {
00189     /* ToDo: Basic error checking to make sure txPower is in range */
00190     return addData(GapAdvertisingData::TX_POWER_LEVEL, (uint8_t *)&txPower, 1);
00191 }
00192 
00193 /**************************************************************************/
00194 /*!
00195     \brief Clears the payload and resets the payload length counter
00196 */
00197 /**************************************************************************/
00198 void GapAdvertisingData::clear(void)
00199 {
00200     memset(&_payload, 0, GAP_ADVERTISING_DATA_MAX_PAYLOAD);
00201     _payloadLen = 0;
00202 }
00203 
00204 /**************************************************************************/
00205 /*!
00206     \brief Returns a pointer to the the current payload
00207 
00208     \returns    A pointer to the payload
00209 */
00210 /**************************************************************************/
00211 const uint8_t *GapAdvertisingData::getPayload(void) const
00212 {
00213     return (_payloadLen > 0) ? _payload : NULL;
00214 }
00215 
00216 /**************************************************************************/
00217 /*!
00218     \brief Returns the current payload length (0..31 bytes)
00219 
00220     \returns    The payload length in bytes
00221 */
00222 /**************************************************************************/
00223 uint8_t GapAdvertisingData::getPayloadLen(void) const
00224 {
00225     return _payloadLen;
00226 }
00227 
00228 /**************************************************************************/
00229 /*!
00230     \brief Returns the 16-bit appearance value for this device
00231 
00232     \returns    The 16-bit appearance value
00233 */
00234 /**************************************************************************/
00235 uint16_t GapAdvertisingData::getAppearance(void) const
00236 {
00237     return (uint16_t)_appearance;
00238 }