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