High level Bluetooth Low Energy API and radio abstraction layer

Dependencies:   nRF51822

Dependents:   LinkNode_LIS3DH

Fork of BLE_API by Bluetooth Low Energy

Committer:
ktownsend
Date:
Thu Dec 12 02:20:54 2013 +0000
Revision:
4:50a31ff5f974
Parent:
3:46de446e82ed
Child:
5:7635f81a8e09
More GAP refactoring

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 4:50a31ff5f974 63 if (_payloadLen + len >= GAP_ADVERTISING_DATA_MAX_PAYLOAD)
ktownsend 4:50a31ff5f974 64 return BLE_ERROR_BUFFER_OVERFLOW;
ktownsend 4:50a31ff5f974 65
ktownsend 4:50a31ff5f974 66 memcpy(&_payload[_payloadLen], payload, len);
ktownsend 4:50a31ff5f974 67 _payloadLen += len;
ktownsend 4:50a31ff5f974 68
ktownsend 4:50a31ff5f974 69 return BLE_ERROR_NONE;
ktownsend 4:50a31ff5f974 70 }
ktownsend 4:50a31ff5f974 71
ktownsend 4:50a31ff5f974 72 /**************************************************************************/
ktownsend 4:50a31ff5f974 73 /*!
ktownsend 4:50a31ff5f974 74 @brief Helper function to add \ref APPEARANCE data to the advertising
ktownsend 4:50a31ff5f974 75 payload
ktownsend 4:50a31ff5f974 76
ktownsend 4:50a31ff5f974 77 @args[in] appearance The \ref APPEARANCE value to add
ktownsend 4:50a31ff5f974 78
ktownsend 4:50a31ff5f974 79 @returns ble_error_t
ktownsend 4:50a31ff5f974 80
ktownsend 4:50a31ff5f974 81 @retval BLE_ERROR_NONE
ktownsend 4:50a31ff5f974 82 Everything executed properly
ktownsend 4:50a31ff5f974 83
ktownsend 3:46de446e82ed 84 @retval BLE_ERROR_BUFFER_OVERFLOW
ktownsend 3:46de446e82ed 85 The specified data would cause the advertising buffer
ktownsend 3:46de446e82ed 86 to overflow
ktownsend 3:46de446e82ed 87
ktownsend 3:46de446e82ed 88 @section EXAMPLE
ktownsend 3:46de446e82ed 89
ktownsend 3:46de446e82ed 90 @code
ktownsend 3:46de446e82ed 91
ktownsend 3:46de446e82ed 92 @endcode
ktownsend 3:46de446e82ed 93 */
ktownsend 3:46de446e82ed 94 /**************************************************************************/
ktownsend 4:50a31ff5f974 95 ble_error_t GapAdvertisingData::addAppearance(Appearance appearance)
ktownsend 3:46de446e82ed 96 {
ktownsend 4:50a31ff5f974 97 return addData(GapAdvertisingData::APPEARANCE, (uint8_t*)&appearance, 2);
ktownsend 4:50a31ff5f974 98 }
ktownsend 4:50a31ff5f974 99
ktownsend 4:50a31ff5f974 100 /**************************************************************************/
ktownsend 4:50a31ff5f974 101 /*!
ktownsend 4:50a31ff5f974 102 @brief Helper function to add \ref FLAGS data to the advertising
ktownsend 4:50a31ff5f974 103 payload
ktownsend 4:50a31ff5f974 104
ktownsend 4:50a31ff5f974 105 @args[in] flag The \ref FLAGS value to add
ktownsend 4:50a31ff5f974 106
ktownsend 4:50a31ff5f974 107 @para
ktownsend 4:50a31ff5f974 108 \ref LE_LIMITED_DISCOVERABLE - The peripheral is
ktownsend 4:50a31ff5f974 109 discoverable for a limited period of time
ktownsend 4:50a31ff5f974 110
ktownsend 4:50a31ff5f974 111 @para
ktownsend 4:50a31ff5f974 112 \ref LE_GENERAL_DISCOVERABLE - The peripheral is
ktownsend 4:50a31ff5f974 113 permanently discoverable
ktownsend 4:50a31ff5f974 114
ktownsend 4:50a31ff5f974 115 @returns ble_error_t
ktownsend 4:50a31ff5f974 116
ktownsend 4:50a31ff5f974 117 @retval BLE_ERROR_NONE
ktownsend 4:50a31ff5f974 118 Everything executed properly
ktownsend 4:50a31ff5f974 119
ktownsend 4:50a31ff5f974 120 @retval BLE_ERROR_BUFFER_OVERFLOW
ktownsend 4:50a31ff5f974 121 The specified data would cause the advertising buffer
ktownsend 4:50a31ff5f974 122 to overflow
ktownsend 3:46de446e82ed 123
ktownsend 4:50a31ff5f974 124 @section EXAMPLE
ktownsend 4:50a31ff5f974 125
ktownsend 4:50a31ff5f974 126 @code
ktownsend 4:50a31ff5f974 127
ktownsend 4:50a31ff5f974 128 @endcode
ktownsend 4:50a31ff5f974 129 */
ktownsend 4:50a31ff5f974 130 /**************************************************************************/
ktownsend 4:50a31ff5f974 131 ble_error_t GapAdvertisingData::addFlags(Flags flag)
ktownsend 4:50a31ff5f974 132 {
ktownsend 4:50a31ff5f974 133 return addData(GapAdvertisingData::FLAGS, (uint8_t*)&flag, 1);
ktownsend 4:50a31ff5f974 134 }
ktownsend 4:50a31ff5f974 135
ktownsend 4:50a31ff5f974 136 /**************************************************************************/
ktownsend 4:50a31ff5f974 137 /*!
ktownsend 4:50a31ff5f974 138 @brief Helper function to add \ref TX_POWER_LEVEL data to the
ktownsend 4:50a31ff5f974 139 advertising payload
ktownsend 4:50a31ff5f974 140
ktownsend 4:50a31ff5f974 141 @args[in] flag The \ref TX_POWER_LEVEL value to add
ktownsend 4:50a31ff5f974 142
ktownsend 4:50a31ff5f974 143 @returns ble_error_t
ktownsend 4:50a31ff5f974 144
ktownsend 4:50a31ff5f974 145 @retval BLE_ERROR_NONE
ktownsend 4:50a31ff5f974 146 Everything executed properly
ktownsend 4:50a31ff5f974 147
ktownsend 4:50a31ff5f974 148 @retval BLE_ERROR_BUFFER_OVERFLOW
ktownsend 4:50a31ff5f974 149 The specified data would cause the advertising buffer
ktownsend 4:50a31ff5f974 150 to overflow
ktownsend 3:46de446e82ed 151
ktownsend 4:50a31ff5f974 152 @section EXAMPLE
ktownsend 4:50a31ff5f974 153
ktownsend 4:50a31ff5f974 154 @code
ktownsend 4:50a31ff5f974 155
ktownsend 4:50a31ff5f974 156 @endcode
ktownsend 4:50a31ff5f974 157 */
ktownsend 4:50a31ff5f974 158 /**************************************************************************/
ktownsend 4:50a31ff5f974 159 ble_error_t GapAdvertisingData::addTxPower(int8_t txPower)
ktownsend 4:50a31ff5f974 160 {
ktownsend 4:50a31ff5f974 161 /* ToDo: Basic error checking to make sure txPower is in range */
ktownsend 4:50a31ff5f974 162 return addData(GapAdvertisingData::TX_POWER_LEVEL, (uint8_t*)&txPower, 1);
ktownsend 3:46de446e82ed 163 }
ktownsend 4:50a31ff5f974 164
ktownsend 4:50a31ff5f974 165 /**************************************************************************/
ktownsend 4:50a31ff5f974 166 /*!
ktownsend 4:50a31ff5f974 167 @brief Clears the payload and resets the payload length counter
ktownsend 4:50a31ff5f974 168 */
ktownsend 4:50a31ff5f974 169 /**************************************************************************/
ktownsend 4:50a31ff5f974 170 void GapAdvertisingData::clear(void)
ktownsend 4:50a31ff5f974 171 {
ktownsend 4:50a31ff5f974 172 memset(&_payload, 0, GAP_ADVERTISING_DATA_MAX_PAYLOAD);
ktownsend 4:50a31ff5f974 173 _payloadLen = 0;
ktownsend 4:50a31ff5f974 174 }
ktownsend 4:50a31ff5f974 175
ktownsend 4:50a31ff5f974 176 /**************************************************************************/
ktownsend 4:50a31ff5f974 177 /*!
ktownsend 4:50a31ff5f974 178 @brief Returns the current payload length (0..31 bytes)
ktownsend 4:50a31ff5f974 179 */
ktownsend 4:50a31ff5f974 180 /**************************************************************************/
ktownsend 4:50a31ff5f974 181 uint8_t GapAdvertisingData::getPayloadLen(void)
ktownsend 4:50a31ff5f974 182 {
ktownsend 4:50a31ff5f974 183 return _payloadLen;
ktownsend 4:50a31ff5f974 184 }