mbed HRM11017を使ってkonashi.jsでナイトライダー

Dependencies:   BLE_API_Native_IRC mbed

Fork of BLE_RCBController by Junichi Katsu

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)
00034 {
00035     memset(_payload, 0, GAP_ADVERTISING_DATA_MAX_PAYLOAD);
00036     _payloadLen = 0;
00037     _appearance = GENERIC_TAG;
00038 }
00039 
00040 /**************************************************************************/
00041 /*!
00042     Destructor
00043 */
00044 /**************************************************************************/
00045 GapAdvertisingData::~GapAdvertisingData (void)
00046 {
00047 }
00048 
00049 /**************************************************************************/
00050 /*!
00051     \brief  Adds advertising data based on the specified AD type (see
00052             DataType)
00053 
00054     \args[in]   advDataType The Advertising 'DataType' to add
00055     \args[in]   payload     Pointer to the payload contents
00056     \args[in]   len         Size of the payload in bytes
00057     
00058     \returns    ble_error_t
00059     
00060     \retval     BLE_ERROR_NONE
00061                 Everything executed properly
00062                 
00063     \retval     BLE_ERROR_BUFFER_OVERFLOW
00064                 The specified data would cause the advertising buffer
00065                 to overflow
00066     
00067     \par EXAMPLE
00068 
00069     \code
00070 
00071     \endcode
00072 */
00073 /**************************************************************************/
00074 ble_error_t GapAdvertisingData::addData(DataType advDataType, uint8_t * payload, uint8_t len)
00075 {
00076     /* ToDo: Check if an AD type already exists and if the existing */
00077     /*       value is exclusive or not (flags, etc.) */
00078     
00079     /* Make sure we don't exceed the 31 byte payload limit */
00080     if (_payloadLen + len + 2 >= GAP_ADVERTISING_DATA_MAX_PAYLOAD)
00081         return BLE_ERROR_BUFFER_OVERFLOW;
00082 
00083     /* Field length */
00084     memset(&_payload[_payloadLen], len+1, 1);
00085     _payloadLen++;
00086     
00087     /* Field ID */
00088     memset(&_payload[_payloadLen], (uint8_t)advDataType, 1); 
00089     _payloadLen++;
00090        
00091     /* Payload */
00092     memcpy(&_payload[_payloadLen], payload, len);
00093     _payloadLen += len;
00094     
00095     return BLE_ERROR_NONE;
00096 }
00097 
00098 /**************************************************************************/
00099 /*!
00100     \brief  Helper function to add APPEARANCE data to the advertising
00101             payload
00102 
00103     \args[in]   appearance  The APPEARANCE value to add
00104     
00105     \returns    ble_error_t
00106     
00107     \retval     BLE_ERROR_NONE
00108                 Everything executed properly
00109                 
00110     \retval     BLE_ERROR_BUFFER_OVERFLOW
00111                 The specified data would cause the advertising buffer
00112                 to overflow
00113     
00114     \par EXAMPLE
00115 
00116     \code
00117 
00118     \endcode
00119 */
00120 /**************************************************************************/
00121 ble_error_t GapAdvertisingData::addAppearance(Appearance appearance)
00122 {
00123     _appearance = appearance;
00124     return addData(GapAdvertisingData::APPEARANCE, (uint8_t*)&appearance, 2);
00125 }
00126 
00127 /**************************************************************************/
00128 /*!
00129     \brief  Helper function to add FLAGS data to the advertising
00130             payload
00131 
00132     \args[in]   flag  The FLAGS value to add
00133 
00134                 \par LE_LIMITED_DISCOVERABLE
00135                 The peripheral is discoverable for a limited period of
00136                 time
00137 
00138                 \par LE_GENERAL_DISCOVERABLE
00139                 The peripheral is permanently discoverable
00140                 
00141                 \par BREDR_NOT_SUPPORTED
00142                 This peripheral is a Bluetooth Low Energy only device
00143                 (no EDR support)
00144 
00145     \returns    ble_error_t
00146     
00147     \retval     BLE_ERROR_NONE
00148                 Everything executed properly
00149                 
00150     \retval     BLE_ERROR_BUFFER_OVERFLOW
00151                 The specified data would cause the advertising buffer
00152                 to overflow
00153     
00154     \par EXAMPLE
00155 
00156     \code
00157 
00158     \endcode
00159 */
00160 /**************************************************************************/
00161 ble_error_t GapAdvertisingData::addFlags(Flags flag)
00162 {
00163     return addData(GapAdvertisingData::FLAGS, (uint8_t*)&flag, 1);
00164 }
00165 
00166 /**************************************************************************/
00167 /*!
00168     \brief  Helper function to add TX_POWER_LEVEL data to the
00169             advertising payload
00170 
00171     \args[in]   flag  The TX_POWER_LEVEL value to add
00172     
00173     \returns    ble_error_t
00174     
00175     \retval     BLE_ERROR_NONE
00176                 Everything executed properly
00177                 
00178     \retval     BLE_ERROR_BUFFER_OVERFLOW
00179                 The specified data would cause the advertising buffer
00180                 to overflow
00181     
00182     \par EXAMPLE
00183 
00184     \code
00185 
00186     \endcode
00187 */
00188 /**************************************************************************/
00189 ble_error_t GapAdvertisingData::addTxPower(int8_t txPower)
00190 {
00191     /* ToDo: Basic error checking to make sure txPower is in range */
00192     return addData(GapAdvertisingData::TX_POWER_LEVEL, (uint8_t*)&txPower, 1);
00193 }
00194 
00195 /**************************************************************************/
00196 /*!
00197     \brief Clears the payload and resets the payload length counter
00198 */
00199 /**************************************************************************/
00200 void GapAdvertisingData::clear(void)
00201 {
00202     memset(&_payload, 0, GAP_ADVERTISING_DATA_MAX_PAYLOAD);
00203     _payloadLen = 0;
00204 }
00205 
00206 /**************************************************************************/
00207 /*!
00208     \brief Returns a pointer to the the current payload
00209     
00210     \returns    A pointer to the payload
00211 */
00212 /**************************************************************************/
00213 uint8_t * GapAdvertisingData::getPayload(void)
00214 {
00215     return (_payloadLen > 0) ? _payload : NULL;
00216 }
00217 
00218 /**************************************************************************/
00219 /*!
00220     \brief Returns the current payload length (0..31 bytes)
00221     
00222     \returns    The payload length in bytes
00223 */
00224 /**************************************************************************/
00225 uint8_t GapAdvertisingData::getPayloadLen(void)
00226 {
00227     return _payloadLen;
00228 }
00229 
00230 /**************************************************************************/
00231 /*!
00232     \brief Returns the 16-bit appearance value for this device
00233     
00234     \returns    The 16-bit appearance value
00235 */
00236 /**************************************************************************/
00237 uint16_t GapAdvertisingData::getAppearance(void)
00238 {
00239     return (uint16_t)_appearance;
00240 }