ble

Dependencies:   HC_SR04_Ultrasonic_Library Servo mbed

Fork of FIP_REV1 by Robotique FIP

Committer:
julientiron
Date:
Thu Jul 09 13:33:36 2015 +0000
Revision:
4:69a35a56ac48
Parent:
0:3d641e170a74
BLE

Who changed what in which revision?

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