Originally from Donal's blog article. http://mbed.org/users/donalm/code/BLE_Health_Thermometer_Blog/ Changed low freq. clock source from XTAL to IRC.
Dependents: BLE_Health_Thermometer_IRC BLE_RCBController_micono_test BLE_konashi_PIO_test BLE_ADT7410_TMP102_Sample ... more
Fork of BLE_API_Native_blog by
GapAdvertisingData.cpp@0:4c3097c65247, 2014-02-06 (annotated)
- Committer:
- ktownsend
- Date:
- Thu Feb 06 11:51:06 2014 +0000
- Revision:
- 0:4c3097c65247
- Child:
- 13:1800682b5703
Native mode drivers
Who changed what in which revision?
User | Revision | Line number | New 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 | 0:4c3097c65247 | 37 | } |
ktownsend | 0:4c3097c65247 | 38 | |
ktownsend | 0:4c3097c65247 | 39 | /**************************************************************************/ |
ktownsend | 0:4c3097c65247 | 40 | /*! |
ktownsend | 0:4c3097c65247 | 41 | Destructor |
ktownsend | 0:4c3097c65247 | 42 | */ |
ktownsend | 0:4c3097c65247 | 43 | /**************************************************************************/ |
ktownsend | 0:4c3097c65247 | 44 | GapAdvertisingData::~GapAdvertisingData(void) |
ktownsend | 0:4c3097c65247 | 45 | { |
ktownsend | 0:4c3097c65247 | 46 | } |
ktownsend | 0:4c3097c65247 | 47 | |
ktownsend | 0:4c3097c65247 | 48 | /**************************************************************************/ |
ktownsend | 0:4c3097c65247 | 49 | /*! |
ktownsend | 0:4c3097c65247 | 50 | \brief Adds advertising data based on the specified AD type (see |
ktownsend | 0:4c3097c65247 | 51 | DataType) |
ktownsend | 0:4c3097c65247 | 52 | |
ktownsend | 0:4c3097c65247 | 53 | \args[in] advDataType The Advertising 'DataType' to add |
ktownsend | 0:4c3097c65247 | 54 | \args[in] payload Pointer to the payload contents |
ktownsend | 0:4c3097c65247 | 55 | \args[in] len Size of the payload in bytes |
ktownsend | 0:4c3097c65247 | 56 | |
ktownsend | 0:4c3097c65247 | 57 | \returns ble_error_t |
ktownsend | 0:4c3097c65247 | 58 | |
ktownsend | 0:4c3097c65247 | 59 | \retval BLE_ERROR_NONE |
ktownsend | 0:4c3097c65247 | 60 | Everything executed properly |
ktownsend | 0:4c3097c65247 | 61 | |
ktownsend | 0:4c3097c65247 | 62 | \retval BLE_ERROR_BUFFER_OVERFLOW |
ktownsend | 0:4c3097c65247 | 63 | The specified data would cause the advertising buffer |
ktownsend | 0:4c3097c65247 | 64 | to overflow |
ktownsend | 0:4c3097c65247 | 65 | |
ktownsend | 0:4c3097c65247 | 66 | \par EXAMPLE |
ktownsend | 0:4c3097c65247 | 67 | |
ktownsend | 0:4c3097c65247 | 68 | \code |
ktownsend | 0:4c3097c65247 | 69 | |
ktownsend | 0:4c3097c65247 | 70 | \endcode |
ktownsend | 0:4c3097c65247 | 71 | */ |
ktownsend | 0:4c3097c65247 | 72 | /**************************************************************************/ |
ktownsend | 0:4c3097c65247 | 73 | ble_error_t GapAdvertisingData::addData(DataType advDataType, uint8_t * payload, uint8_t len) |
ktownsend | 0:4c3097c65247 | 74 | { |
ktownsend | 0:4c3097c65247 | 75 | /* ToDo: Check if an AD type already exists and if the existing */ |
ktownsend | 0:4c3097c65247 | 76 | /* value is exclusive or not (flags, etc.) */ |
ktownsend | 0:4c3097c65247 | 77 | |
ktownsend | 0:4c3097c65247 | 78 | /* Make sure we don't exceed the 31 byte payload limit */ |
ktownsend | 0:4c3097c65247 | 79 | if (_payloadLen + len + 2 >= GAP_ADVERTISING_DATA_MAX_PAYLOAD) |
ktownsend | 0:4c3097c65247 | 80 | return BLE_ERROR_BUFFER_OVERFLOW; |
ktownsend | 0:4c3097c65247 | 81 | |
ktownsend | 0:4c3097c65247 | 82 | /* Field length */ |
ktownsend | 0:4c3097c65247 | 83 | memset(&_payload[_payloadLen], len+1, 1); |
ktownsend | 0:4c3097c65247 | 84 | _payloadLen++; |
ktownsend | 0:4c3097c65247 | 85 | |
ktownsend | 0:4c3097c65247 | 86 | /* Field ID */ |
ktownsend | 0:4c3097c65247 | 87 | memset(&_payload[_payloadLen], (uint8_t)advDataType, 1); |
ktownsend | 0:4c3097c65247 | 88 | _payloadLen++; |
ktownsend | 0:4c3097c65247 | 89 | |
ktownsend | 0:4c3097c65247 | 90 | /* Payload */ |
ktownsend | 0:4c3097c65247 | 91 | memcpy(&_payload[_payloadLen], payload, len); |
ktownsend | 0:4c3097c65247 | 92 | _payloadLen += len; |
ktownsend | 0:4c3097c65247 | 93 | |
ktownsend | 0:4c3097c65247 | 94 | return BLE_ERROR_NONE; |
ktownsend | 0:4c3097c65247 | 95 | } |
ktownsend | 0:4c3097c65247 | 96 | |
ktownsend | 0:4c3097c65247 | 97 | /**************************************************************************/ |
ktownsend | 0:4c3097c65247 | 98 | /*! |
ktownsend | 0:4c3097c65247 | 99 | \brief Helper function to add APPEARANCE data to the advertising |
ktownsend | 0:4c3097c65247 | 100 | payload |
ktownsend | 0:4c3097c65247 | 101 | |
ktownsend | 0:4c3097c65247 | 102 | \args[in] appearance The APPEARANCE value to add |
ktownsend | 0:4c3097c65247 | 103 | |
ktownsend | 0:4c3097c65247 | 104 | \returns ble_error_t |
ktownsend | 0:4c3097c65247 | 105 | |
ktownsend | 0:4c3097c65247 | 106 | \retval BLE_ERROR_NONE |
ktownsend | 0:4c3097c65247 | 107 | Everything executed properly |
ktownsend | 0:4c3097c65247 | 108 | |
ktownsend | 0:4c3097c65247 | 109 | \retval BLE_ERROR_BUFFER_OVERFLOW |
ktownsend | 0:4c3097c65247 | 110 | The specified data would cause the advertising buffer |
ktownsend | 0:4c3097c65247 | 111 | to overflow |
ktownsend | 0:4c3097c65247 | 112 | |
ktownsend | 0:4c3097c65247 | 113 | \par EXAMPLE |
ktownsend | 0:4c3097c65247 | 114 | |
ktownsend | 0:4c3097c65247 | 115 | \code |
ktownsend | 0:4c3097c65247 | 116 | |
ktownsend | 0:4c3097c65247 | 117 | \endcode |
ktownsend | 0:4c3097c65247 | 118 | */ |
ktownsend | 0:4c3097c65247 | 119 | /**************************************************************************/ |
ktownsend | 0:4c3097c65247 | 120 | ble_error_t GapAdvertisingData::addAppearance(Appearance appearance) |
ktownsend | 0:4c3097c65247 | 121 | { |
ktownsend | 0:4c3097c65247 | 122 | return addData(GapAdvertisingData::APPEARANCE, (uint8_t*)&appearance, 2); |
ktownsend | 0:4c3097c65247 | 123 | } |
ktownsend | 0:4c3097c65247 | 124 | |
ktownsend | 0:4c3097c65247 | 125 | /**************************************************************************/ |
ktownsend | 0:4c3097c65247 | 126 | /*! |
ktownsend | 0:4c3097c65247 | 127 | \brief Helper function to add FLAGS data to the advertising |
ktownsend | 0:4c3097c65247 | 128 | payload |
ktownsend | 0:4c3097c65247 | 129 | |
ktownsend | 0:4c3097c65247 | 130 | \args[in] flag The FLAGS value to add |
ktownsend | 0:4c3097c65247 | 131 | |
ktownsend | 0:4c3097c65247 | 132 | \par LE_LIMITED_DISCOVERABLE |
ktownsend | 0:4c3097c65247 | 133 | The peripheral is discoverable for a limited period of |
ktownsend | 0:4c3097c65247 | 134 | time |
ktownsend | 0:4c3097c65247 | 135 | |
ktownsend | 0:4c3097c65247 | 136 | \par LE_GENERAL_DISCOVERABLE |
ktownsend | 0:4c3097c65247 | 137 | The peripheral is permanently discoverable |
ktownsend | 0:4c3097c65247 | 138 | |
ktownsend | 0:4c3097c65247 | 139 | \par BREDR_NOT_SUPPORTED |
ktownsend | 0:4c3097c65247 | 140 | This peripheral is a Bluetooth Low Energy only device |
ktownsend | 0:4c3097c65247 | 141 | (no EDR support) |
ktownsend | 0:4c3097c65247 | 142 | |
ktownsend | 0:4c3097c65247 | 143 | \returns ble_error_t |
ktownsend | 0:4c3097c65247 | 144 | |
ktownsend | 0:4c3097c65247 | 145 | \retval BLE_ERROR_NONE |
ktownsend | 0:4c3097c65247 | 146 | Everything executed properly |
ktownsend | 0:4c3097c65247 | 147 | |
ktownsend | 0:4c3097c65247 | 148 | \retval BLE_ERROR_BUFFER_OVERFLOW |
ktownsend | 0:4c3097c65247 | 149 | The specified data would cause the advertising buffer |
ktownsend | 0:4c3097c65247 | 150 | to overflow |
ktownsend | 0:4c3097c65247 | 151 | |
ktownsend | 0:4c3097c65247 | 152 | \par EXAMPLE |
ktownsend | 0:4c3097c65247 | 153 | |
ktownsend | 0:4c3097c65247 | 154 | \code |
ktownsend | 0:4c3097c65247 | 155 | |
ktownsend | 0:4c3097c65247 | 156 | \endcode |
ktownsend | 0:4c3097c65247 | 157 | */ |
ktownsend | 0:4c3097c65247 | 158 | /**************************************************************************/ |
ktownsend | 0:4c3097c65247 | 159 | ble_error_t GapAdvertisingData::addFlags(Flags flag) |
ktownsend | 0:4c3097c65247 | 160 | { |
ktownsend | 0:4c3097c65247 | 161 | return addData(GapAdvertisingData::FLAGS, (uint8_t*)&flag, 1); |
ktownsend | 0:4c3097c65247 | 162 | } |
ktownsend | 0:4c3097c65247 | 163 | |
ktownsend | 0:4c3097c65247 | 164 | /**************************************************************************/ |
ktownsend | 0:4c3097c65247 | 165 | /*! |
ktownsend | 0:4c3097c65247 | 166 | \brief Helper function to add TX_POWER_LEVEL data to the |
ktownsend | 0:4c3097c65247 | 167 | advertising payload |
ktownsend | 0:4c3097c65247 | 168 | |
ktownsend | 0:4c3097c65247 | 169 | \args[in] flag The TX_POWER_LEVEL value to add |
ktownsend | 0:4c3097c65247 | 170 | |
ktownsend | 0:4c3097c65247 | 171 | \returns ble_error_t |
ktownsend | 0:4c3097c65247 | 172 | |
ktownsend | 0:4c3097c65247 | 173 | \retval BLE_ERROR_NONE |
ktownsend | 0:4c3097c65247 | 174 | Everything executed properly |
ktownsend | 0:4c3097c65247 | 175 | |
ktownsend | 0:4c3097c65247 | 176 | \retval BLE_ERROR_BUFFER_OVERFLOW |
ktownsend | 0:4c3097c65247 | 177 | The specified data would cause the advertising buffer |
ktownsend | 0:4c3097c65247 | 178 | to overflow |
ktownsend | 0:4c3097c65247 | 179 | |
ktownsend | 0:4c3097c65247 | 180 | \par EXAMPLE |
ktownsend | 0:4c3097c65247 | 181 | |
ktownsend | 0:4c3097c65247 | 182 | \code |
ktownsend | 0:4c3097c65247 | 183 | |
ktownsend | 0:4c3097c65247 | 184 | \endcode |
ktownsend | 0:4c3097c65247 | 185 | */ |
ktownsend | 0:4c3097c65247 | 186 | /**************************************************************************/ |
ktownsend | 0:4c3097c65247 | 187 | ble_error_t GapAdvertisingData::addTxPower(int8_t txPower) |
ktownsend | 0:4c3097c65247 | 188 | { |
ktownsend | 0:4c3097c65247 | 189 | /* ToDo: Basic error checking to make sure txPower is in range */ |
ktownsend | 0:4c3097c65247 | 190 | return addData(GapAdvertisingData::TX_POWER_LEVEL, (uint8_t*)&txPower, 1); |
ktownsend | 0:4c3097c65247 | 191 | } |
ktownsend | 0:4c3097c65247 | 192 | |
ktownsend | 0:4c3097c65247 | 193 | /**************************************************************************/ |
ktownsend | 0:4c3097c65247 | 194 | /*! |
ktownsend | 0:4c3097c65247 | 195 | \brief Clears the payload and resets the payload length counter |
ktownsend | 0:4c3097c65247 | 196 | */ |
ktownsend | 0:4c3097c65247 | 197 | /**************************************************************************/ |
ktownsend | 0:4c3097c65247 | 198 | void GapAdvertisingData::clear(void) |
ktownsend | 0:4c3097c65247 | 199 | { |
ktownsend | 0:4c3097c65247 | 200 | memset(&_payload, 0, GAP_ADVERTISING_DATA_MAX_PAYLOAD); |
ktownsend | 0:4c3097c65247 | 201 | _payloadLen = 0; |
ktownsend | 0:4c3097c65247 | 202 | } |
ktownsend | 0:4c3097c65247 | 203 | |
ktownsend | 0:4c3097c65247 | 204 | /**************************************************************************/ |
ktownsend | 0:4c3097c65247 | 205 | /*! |
ktownsend | 0:4c3097c65247 | 206 | \brief Returns a pointer to the the current payload |
ktownsend | 0:4c3097c65247 | 207 | |
ktownsend | 0:4c3097c65247 | 208 | \returns A pointer to the payload |
ktownsend | 0:4c3097c65247 | 209 | */ |
ktownsend | 0:4c3097c65247 | 210 | /**************************************************************************/ |
ktownsend | 0:4c3097c65247 | 211 | uint8_t * GapAdvertisingData::getPayload(void) |
ktownsend | 0:4c3097c65247 | 212 | { |
ktownsend | 0:4c3097c65247 | 213 | return (_payloadLen > 0) ? _payload : NULL; |
ktownsend | 0:4c3097c65247 | 214 | } |
ktownsend | 0:4c3097c65247 | 215 | |
ktownsend | 0:4c3097c65247 | 216 | /**************************************************************************/ |
ktownsend | 0:4c3097c65247 | 217 | /*! |
ktownsend | 0:4c3097c65247 | 218 | \brief Returns the current payload length (0..31 bytes) |
ktownsend | 0:4c3097c65247 | 219 | |
ktownsend | 0:4c3097c65247 | 220 | \returns The payload length in bytes |
ktownsend | 0:4c3097c65247 | 221 | */ |
ktownsend | 0:4c3097c65247 | 222 | /**************************************************************************/ |
ktownsend | 0:4c3097c65247 | 223 | uint8_t GapAdvertisingData::getPayloadLen(void) |
ktownsend | 0:4c3097c65247 | 224 | { |
ktownsend | 0:4c3097c65247 | 225 | return _payloadLen; |
ktownsend | 0:4c3097c65247 | 226 | } |