Changed the device name.

Dependents:   BLE_Health_Thermometer_HeartRateMonitor

Fork of BLE_API_Native_IRC by Yoshihiro TSUBOI

Committer:
ghz2000
Date:
Mon Jun 16 15:06:48 2014 +0000
Revision:
20:a58a51c92075
Parent:
13:1800682b5703
Add HeartRateMonitor

Who changed what in which revision?

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