Microbug / BLE_API_FOTA

Fork of BLE_API by Bluetooth Low Energy

Committer:
ktownsend
Date:
Wed Dec 18 11:52:37 2013 +0000
Revision:
14:6ea5d1012a64
Parent:
7:5e1f0d7f7c7d
Child:
18:86fe1e247a54
Fixed advertising interval checks

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ktownsend 2:ffc5216bd2cc 1 #include <stdio.h>
ktownsend 2:ffc5216bd2cc 2 #include <string.h>
ktownsend 2:ffc5216bd2cc 3
ktownsend 2:ffc5216bd2cc 4 #include "GapAdvertisingData.h"
ktownsend 2:ffc5216bd2cc 5
ktownsend 2:ffc5216bd2cc 6 /**************************************************************************/
ktownsend 2:ffc5216bd2cc 7 /*!
ktownsend 2:ffc5216bd2cc 8 @brief Creates a new GapAdvertisingData instance
ktownsend 2:ffc5216bd2cc 9
ktownsend 2:ffc5216bd2cc 10 @section EXAMPLE
ktownsend 2:ffc5216bd2cc 11
ktownsend 2:ffc5216bd2cc 12 @code
ktownsend 2:ffc5216bd2cc 13
ktownsend 2:ffc5216bd2cc 14 @endcode
ktownsend 2:ffc5216bd2cc 15 */
ktownsend 2:ffc5216bd2cc 16 /**************************************************************************/
ktownsend 2:ffc5216bd2cc 17 GapAdvertisingData::GapAdvertisingData(void)
ktownsend 2:ffc5216bd2cc 18 {
ktownsend 3:46de446e82ed 19 memset(_payload, 0, GAP_ADVERTISING_DATA_MAX_PAYLOAD);
ktownsend 3:46de446e82ed 20 _payloadLen = 0;
ktownsend 2:ffc5216bd2cc 21 }
ktownsend 2:ffc5216bd2cc 22
ktownsend 2:ffc5216bd2cc 23 /**************************************************************************/
ktownsend 2:ffc5216bd2cc 24 /*!
ktownsend 2:ffc5216bd2cc 25 Destructor
ktownsend 2:ffc5216bd2cc 26 */
ktownsend 2:ffc5216bd2cc 27 /**************************************************************************/
ktownsend 2:ffc5216bd2cc 28 GapAdvertisingData::~GapAdvertisingData(void)
ktownsend 2:ffc5216bd2cc 29 {
ktownsend 2:ffc5216bd2cc 30 }
ktownsend 3:46de446e82ed 31
ktownsend 3:46de446e82ed 32 /**************************************************************************/
ktownsend 3:46de446e82ed 33 /*!
ktownsend 4:50a31ff5f974 34 @brief Adds advertising data based on the specified AD type (see
ktownsend 4:50a31ff5f974 35 \ref DataType)
ktownsend 3:46de446e82ed 36
ktownsend 4:50a31ff5f974 37 @args[in] advDataType The Advertising \ref DataType to add
ktownsend 3:46de446e82ed 38 @args[in] payload Pointer to the payload contents
ktownsend 3:46de446e82ed 39 @args[in] len Size of the payload in bytes
ktownsend 3:46de446e82ed 40
ktownsend 3:46de446e82ed 41 @returns ble_error_t
ktownsend 3:46de446e82ed 42
ktownsend 3:46de446e82ed 43 @retval BLE_ERROR_NONE
ktownsend 3:46de446e82ed 44 Everything executed properly
ktownsend 4:50a31ff5f974 45
ktownsend 4:50a31ff5f974 46 @retval BLE_ERROR_BUFFER_OVERFLOW
ktownsend 4:50a31ff5f974 47 The specified data would cause the advertising buffer
ktownsend 4:50a31ff5f974 48 to overflow
ktownsend 4:50a31ff5f974 49
ktownsend 4:50a31ff5f974 50 @section EXAMPLE
ktownsend 4:50a31ff5f974 51
ktownsend 4:50a31ff5f974 52 @code
ktownsend 4:50a31ff5f974 53
ktownsend 4:50a31ff5f974 54 @endcode
ktownsend 4:50a31ff5f974 55 */
ktownsend 4:50a31ff5f974 56 /**************************************************************************/
ktownsend 4:50a31ff5f974 57 ble_error_t GapAdvertisingData::addData(DataType advDataType, uint8_t * payload, uint8_t len)
ktownsend 4:50a31ff5f974 58 {
ktownsend 4:50a31ff5f974 59 /* ToDo: Check if an AD type already exists and if the existing */
ktownsend 4:50a31ff5f974 60 /* value is exclusive or not (flags, etc.) */
ktownsend 4:50a31ff5f974 61
ktownsend 4:50a31ff5f974 62 /* Make sure we don't exceed the 31 byte payload limit */
ktownsend 14:6ea5d1012a64 63 if (_payloadLen + len + 2 >= GAP_ADVERTISING_DATA_MAX_PAYLOAD)
ktownsend 4:50a31ff5f974 64 return BLE_ERROR_BUFFER_OVERFLOW;
ktownsend 14:6ea5d1012a64 65
ktownsend 14:6ea5d1012a64 66 /* Field length */
ktownsend 14:6ea5d1012a64 67 memset(&_payload[_payloadLen], len+1, 1);
ktownsend 14:6ea5d1012a64 68 _payloadLen++;
ktownsend 4:50a31ff5f974 69
ktownsend 14:6ea5d1012a64 70 /* Field ID */
ktownsend 14:6ea5d1012a64 71 memset(&_payload[_payloadLen], (uint8_t)advDataType, 1);
ktownsend 14:6ea5d1012a64 72 _payloadLen++;
ktownsend 14:6ea5d1012a64 73
ktownsend 14:6ea5d1012a64 74 /* Payload */
ktownsend 4:50a31ff5f974 75 memcpy(&_payload[_payloadLen], payload, len);
ktownsend 4:50a31ff5f974 76 _payloadLen += len;
ktownsend 4:50a31ff5f974 77
ktownsend 4:50a31ff5f974 78 return BLE_ERROR_NONE;
ktownsend 4:50a31ff5f974 79 }
ktownsend 4:50a31ff5f974 80
ktownsend 4:50a31ff5f974 81 /**************************************************************************/
ktownsend 4:50a31ff5f974 82 /*!
ktownsend 4:50a31ff5f974 83 @brief Helper function to add \ref APPEARANCE data to the advertising
ktownsend 4:50a31ff5f974 84 payload
ktownsend 4:50a31ff5f974 85
ktownsend 4:50a31ff5f974 86 @args[in] appearance The \ref APPEARANCE value to add
ktownsend 4:50a31ff5f974 87
ktownsend 4:50a31ff5f974 88 @returns ble_error_t
ktownsend 4:50a31ff5f974 89
ktownsend 4:50a31ff5f974 90 @retval BLE_ERROR_NONE
ktownsend 4:50a31ff5f974 91 Everything executed properly
ktownsend 4:50a31ff5f974 92
ktownsend 3:46de446e82ed 93 @retval BLE_ERROR_BUFFER_OVERFLOW
ktownsend 3:46de446e82ed 94 The specified data would cause the advertising buffer
ktownsend 3:46de446e82ed 95 to overflow
ktownsend 3:46de446e82ed 96
ktownsend 3:46de446e82ed 97 @section EXAMPLE
ktownsend 3:46de446e82ed 98
ktownsend 3:46de446e82ed 99 @code
ktownsend 3:46de446e82ed 100
ktownsend 3:46de446e82ed 101 @endcode
ktownsend 3:46de446e82ed 102 */
ktownsend 3:46de446e82ed 103 /**************************************************************************/
ktownsend 4:50a31ff5f974 104 ble_error_t GapAdvertisingData::addAppearance(Appearance appearance)
ktownsend 3:46de446e82ed 105 {
ktownsend 4:50a31ff5f974 106 return addData(GapAdvertisingData::APPEARANCE, (uint8_t*)&appearance, 2);
ktownsend 4:50a31ff5f974 107 }
ktownsend 4:50a31ff5f974 108
ktownsend 4:50a31ff5f974 109 /**************************************************************************/
ktownsend 4:50a31ff5f974 110 /*!
ktownsend 4:50a31ff5f974 111 @brief Helper function to add \ref FLAGS data to the advertising
ktownsend 4:50a31ff5f974 112 payload
ktownsend 4:50a31ff5f974 113
ktownsend 4:50a31ff5f974 114 @args[in] flag The \ref FLAGS value to add
ktownsend 4:50a31ff5f974 115
ktownsend 4:50a31ff5f974 116 @para
ktownsend 4:50a31ff5f974 117 \ref LE_LIMITED_DISCOVERABLE - The peripheral is
ktownsend 4:50a31ff5f974 118 discoverable for a limited period of time
ktownsend 4:50a31ff5f974 119
ktownsend 4:50a31ff5f974 120 @para
ktownsend 4:50a31ff5f974 121 \ref LE_GENERAL_DISCOVERABLE - The peripheral is
ktownsend 4:50a31ff5f974 122 permanently discoverable
ktownsend 5:7635f81a8e09 123
ktownsend 5:7635f81a8e09 124 @para
ktownsend 5:7635f81a8e09 125 \ref BREDR_NOT_SUPPORTED - This peripheral is a
ktownsend 5:7635f81a8e09 126 Bluetooth Low Energy only device (no EDR support)
ktownsend 4:50a31ff5f974 127
ktownsend 4:50a31ff5f974 128 @returns ble_error_t
ktownsend 4:50a31ff5f974 129
ktownsend 4:50a31ff5f974 130 @retval BLE_ERROR_NONE
ktownsend 4:50a31ff5f974 131 Everything executed properly
ktownsend 4:50a31ff5f974 132
ktownsend 4:50a31ff5f974 133 @retval BLE_ERROR_BUFFER_OVERFLOW
ktownsend 4:50a31ff5f974 134 The specified data would cause the advertising buffer
ktownsend 4:50a31ff5f974 135 to overflow
ktownsend 3:46de446e82ed 136
ktownsend 4:50a31ff5f974 137 @section EXAMPLE
ktownsend 4:50a31ff5f974 138
ktownsend 4:50a31ff5f974 139 @code
ktownsend 4:50a31ff5f974 140
ktownsend 4:50a31ff5f974 141 @endcode
ktownsend 4:50a31ff5f974 142 */
ktownsend 4:50a31ff5f974 143 /**************************************************************************/
ktownsend 4:50a31ff5f974 144 ble_error_t GapAdvertisingData::addFlags(Flags flag)
ktownsend 4:50a31ff5f974 145 {
ktownsend 4:50a31ff5f974 146 return addData(GapAdvertisingData::FLAGS, (uint8_t*)&flag, 1);
ktownsend 4:50a31ff5f974 147 }
ktownsend 4:50a31ff5f974 148
ktownsend 4:50a31ff5f974 149 /**************************************************************************/
ktownsend 4:50a31ff5f974 150 /*!
ktownsend 4:50a31ff5f974 151 @brief Helper function to add \ref TX_POWER_LEVEL data to the
ktownsend 4:50a31ff5f974 152 advertising payload
ktownsend 4:50a31ff5f974 153
ktownsend 4:50a31ff5f974 154 @args[in] flag The \ref TX_POWER_LEVEL value to add
ktownsend 4:50a31ff5f974 155
ktownsend 4:50a31ff5f974 156 @returns ble_error_t
ktownsend 4:50a31ff5f974 157
ktownsend 4:50a31ff5f974 158 @retval BLE_ERROR_NONE
ktownsend 4:50a31ff5f974 159 Everything executed properly
ktownsend 4:50a31ff5f974 160
ktownsend 4:50a31ff5f974 161 @retval BLE_ERROR_BUFFER_OVERFLOW
ktownsend 4:50a31ff5f974 162 The specified data would cause the advertising buffer
ktownsend 4:50a31ff5f974 163 to overflow
ktownsend 3:46de446e82ed 164
ktownsend 4:50a31ff5f974 165 @section EXAMPLE
ktownsend 4:50a31ff5f974 166
ktownsend 4:50a31ff5f974 167 @code
ktownsend 4:50a31ff5f974 168
ktownsend 4:50a31ff5f974 169 @endcode
ktownsend 4:50a31ff5f974 170 */
ktownsend 4:50a31ff5f974 171 /**************************************************************************/
ktownsend 4:50a31ff5f974 172 ble_error_t GapAdvertisingData::addTxPower(int8_t txPower)
ktownsend 4:50a31ff5f974 173 {
ktownsend 4:50a31ff5f974 174 /* ToDo: Basic error checking to make sure txPower is in range */
ktownsend 4:50a31ff5f974 175 return addData(GapAdvertisingData::TX_POWER_LEVEL, (uint8_t*)&txPower, 1);
ktownsend 3:46de446e82ed 176 }
ktownsend 4:50a31ff5f974 177
ktownsend 4:50a31ff5f974 178 /**************************************************************************/
ktownsend 4:50a31ff5f974 179 /*!
ktownsend 4:50a31ff5f974 180 @brief Clears the payload and resets the payload length counter
ktownsend 4:50a31ff5f974 181 */
ktownsend 4:50a31ff5f974 182 /**************************************************************************/
ktownsend 4:50a31ff5f974 183 void GapAdvertisingData::clear(void)
ktownsend 4:50a31ff5f974 184 {
ktownsend 4:50a31ff5f974 185 memset(&_payload, 0, GAP_ADVERTISING_DATA_MAX_PAYLOAD);
ktownsend 4:50a31ff5f974 186 _payloadLen = 0;
ktownsend 4:50a31ff5f974 187 }
ktownsend 4:50a31ff5f974 188
ktownsend 4:50a31ff5f974 189 /**************************************************************************/
ktownsend 4:50a31ff5f974 190 /*!
ktownsend 7:5e1f0d7f7c7d 191 @brief Returns a pointer to the the current payload
ktownsend 7:5e1f0d7f7c7d 192
ktownsend 7:5e1f0d7f7c7d 193 @returns A pointer to the payload
ktownsend 7:5e1f0d7f7c7d 194 */
ktownsend 7:5e1f0d7f7c7d 195 /**************************************************************************/
ktownsend 7:5e1f0d7f7c7d 196 uint8_t * GapAdvertisingData::getPayload(void)
ktownsend 7:5e1f0d7f7c7d 197 {
ktownsend 7:5e1f0d7f7c7d 198 return _payload;
ktownsend 7:5e1f0d7f7c7d 199 }
ktownsend 7:5e1f0d7f7c7d 200
ktownsend 7:5e1f0d7f7c7d 201 /**************************************************************************/
ktownsend 7:5e1f0d7f7c7d 202 /*!
ktownsend 4:50a31ff5f974 203 @brief Returns the current payload length (0..31 bytes)
ktownsend 7:5e1f0d7f7c7d 204
ktownsend 7:5e1f0d7f7c7d 205 @returns The payload length in bytes
ktownsend 4:50a31ff5f974 206 */
ktownsend 4:50a31ff5f974 207 /**************************************************************************/
ktownsend 4:50a31ff5f974 208 uint8_t GapAdvertisingData::getPayloadLen(void)
ktownsend 4:50a31ff5f974 209 {
ktownsend 4:50a31ff5f974 210 return _payloadLen;
ktownsend 4:50a31ff5f974 211 }