Nordic nrf51 sdk sources. Mirrored from https://github.com/ARMmbed/nrf51-sdk.

Dependents:   nRF51822 nRF51822

Committer:
vcoubard
Date:
Thu Apr 07 17:37:56 2016 +0100
Revision:
28:041dac1366b2
Parent:
20:a90c48eb1d30
Child:
29:286940b7ee5a
Synchronized with git rev 012b8118
Author: Liyou Zhou
Pull in files from sdk 10.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
vcoubard 28:041dac1366b2 1 /* Copyright (c) 2012 Nordic Semiconductor. All Rights Reserved.
vcoubard 28:041dac1366b2 2 *
vcoubard 28:041dac1366b2 3 * The information contained herein is property of Nordic Semiconductor ASA.
vcoubard 28:041dac1366b2 4 * Terms and conditions of usage are described in detail in NORDIC
vcoubard 28:041dac1366b2 5 * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
vcoubard 28:041dac1366b2 6 *
vcoubard 28:041dac1366b2 7 * Licensees are granted free, non-transferable use of the information. NO
vcoubard 28:041dac1366b2 8 * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
vcoubard 28:041dac1366b2 9 * the file.
vcoubard 28:041dac1366b2 10 *
Vincent Coubard 0:f2542974c862 11 */
Vincent Coubard 0:f2542974c862 12
Vincent Coubard 0:f2542974c862 13 /** @file
Vincent Coubard 0:f2542974c862 14 *
vcoubard 28:041dac1366b2 15 * @defgroup ble_sdk_lib_advdata Advertising and Scan Response Data Encoder
Vincent Coubard 0:f2542974c862 16 * @{
Vincent Coubard 0:f2542974c862 17 * @ingroup ble_sdk_lib
vcoubard 28:041dac1366b2 18 * @brief Functions for encoding data in the Advertising and Scan Response Data format,
vcoubard 28:041dac1366b2 19 * and for passing the data to the stack.
Vincent Coubard 0:f2542974c862 20 */
Vincent Coubard 0:f2542974c862 21
Vincent Coubard 0:f2542974c862 22 #ifndef BLE_ADVDATA_H__
Vincent Coubard 0:f2542974c862 23 #define BLE_ADVDATA_H__
Vincent Coubard 0:f2542974c862 24
Vincent Coubard 0:f2542974c862 25 #include <stdint.h>
Vincent Coubard 0:f2542974c862 26 #include <stdbool.h>
Vincent Coubard 0:f2542974c862 27 #include <string.h>
Vincent Coubard 0:f2542974c862 28 #include "ble.h"
Vincent Coubard 0:f2542974c862 29 #include "app_util.h"
Vincent Coubard 0:f2542974c862 30
vcoubard 28:041dac1366b2 31
vcoubard 28:041dac1366b2 32 #define ADV_LENGTH_FIELD_SIZE 1UL /**< Advertising Data and Scan Response format contains 1 octet for the length. */
vcoubard 28:041dac1366b2 33 #define ADV_AD_TYPE_FIELD_SIZE 1UL /**< Advertising Data and Scan Response format contains 1 octet for the AD type. */
vcoubard 28:041dac1366b2 34 #define ADV_AD_DATA_OFFSET (ADV_LENGTH_FIELD_SIZE + \
vcoubard 28:041dac1366b2 35 ADV_AD_TYPE_FIELD_SIZE) /**< Offset for the AD data field of the Advertising Data and Scan Response format. */
vcoubard 28:041dac1366b2 36 #define AD_TYPE_TK_VALUE_DATA_SIZE (sizeof(ble_advdata_tk_value_t)) /**< Data size (in octets) of the Security Manager TK value AD type. */
vcoubard 28:041dac1366b2 37 #define AD_TYPE_TK_VALUE_SIZE (ADV_AD_DATA_OFFSET + \
vcoubard 28:041dac1366b2 38 AD_TYPE_TK_VALUE_DATA_SIZE) /**< Size (in octets) of the Security Manager TK value AD type. */
vcoubard 28:041dac1366b2 39 #define AD_TYPE_LE_ROLE_DATA_SIZE 1UL /**< Data size (in octets) of the LE Bluetooth Device Address AD type. */
vcoubard 28:041dac1366b2 40 #define AD_TYPE_LE_ROLE_SIZE (ADV_AD_DATA_OFFSET + \
vcoubard 28:041dac1366b2 41 AD_TYPE_LE_ROLE_DATA_SIZE) /**< Size (in octets) of the LE Bluetooth Device Address AD type. */
vcoubard 28:041dac1366b2 42 #define AD_TYPE_BLE_DEVICE_ADDR_TYPE_SIZE 1UL /**< Data size (in octets) of the Address type of the LE Bluetooth Device Address AD type. */
vcoubard 28:041dac1366b2 43 #define AD_TYPE_BLE_DEVICE_ADDR_DATA_SIZE (BLE_GAP_ADDR_LEN + \
vcoubard 28:041dac1366b2 44 AD_TYPE_BLE_DEVICE_ADDR_TYPE_SIZE) /**< Data size (in octets) of the LE Bluetooth Device Address AD type. */
vcoubard 28:041dac1366b2 45 #define AD_TYPE_BLE_DEVICE_ADDR_SIZE (ADV_AD_DATA_OFFSET + \
vcoubard 28:041dac1366b2 46 AD_TYPE_BLE_DEVICE_ADDR_DATA_SIZE) /**< Size (in octets) of the LE Bluetooth Device Address AD type. */
vcoubard 28:041dac1366b2 47 #define AD_TYPE_APPEARANCE_DATA_SIZE 2UL /**< Data size (in octets) of the Appearance AD type. */
vcoubard 28:041dac1366b2 48 #define AD_TYPE_APPEARANCE_SIZE (ADV_AD_DATA_OFFSET + \
vcoubard 28:041dac1366b2 49 AD_TYPE_APPEARANCE_DATA_SIZE) /**< Size (in octets) of the Appearance AD type. */
vcoubard 28:041dac1366b2 50 #define AD_TYPE_FLAGS_DATA_SIZE 1UL /**< Data size (in octets) of the Flags AD type. */
vcoubard 28:041dac1366b2 51 #define AD_TYPE_FLAGS_SIZE (ADV_AD_DATA_OFFSET + \
vcoubard 28:041dac1366b2 52 AD_TYPE_FLAGS_DATA_SIZE) /**< Size (in octets) of the Flags AD type. */
vcoubard 28:041dac1366b2 53 #define AD_TYPE_TX_POWER_LEVEL_DATA_SIZE 1UL /**< Data size (in octets) of the TX Power Level AD type. */
vcoubard 28:041dac1366b2 54 #define AD_TYPE_TX_POWER_LEVEL_SIZE (ADV_AD_DATA_OFFSET + \
vcoubard 28:041dac1366b2 55 AD_TYPE_TX_POWER_LEVEL_DATA_SIZE) /**< Size (in octets) of the TX Power Level AD type. */
vcoubard 28:041dac1366b2 56 #define AD_TYPE_CONN_INT_DATA_SIZE 4UL /**< Data size (in octets) of the Slave Connection Interval Range AD type. */
vcoubard 28:041dac1366b2 57 #define AD_TYPE_CONN_INT_SIZE (ADV_AD_DATA_OFFSET + \
vcoubard 28:041dac1366b2 58 AD_TYPE_CONN_INT_DATA_SIZE) /**< Data size (in octets) of the Slave Connection Interval Range AD type. */
vcoubard 28:041dac1366b2 59 #define AD_TYPE_MANUF_SPEC_DATA_ID_SIZE 2UL /**< Size (in octets) of the Company Identifier Code, which is a part of the Manufacturer Specific Data AD type. */
vcoubard 28:041dac1366b2 60 #define AD_TYPE_SERV_DATA_16BIT_UUID_SIZE 2UL /**< Size (in octets) of the 16-bit UUID, which is a part of the Service Data AD type. */
vcoubard 28:041dac1366b2 61 #define AD_TYPE_OOB_FLAGS_DATA_SIZE 1UL /**< Data size (in octets) of the Security Manager OOB Flags AD type. */
vcoubard 28:041dac1366b2 62 #define AD_TYPE_OOB_FLAGS_SIZE (ADV_AD_DATA_OFFSET + \
vcoubard 28:041dac1366b2 63 AD_TYPE_OOB_FLAGS_DATA_SIZE) /**< Size (in octets) of the Security Manager OOB Flags AD type. */
vcoubard 28:041dac1366b2 64
vcoubard 28:041dac1366b2 65 #define AD_TYPE_SEC_MGR_OOB_FLAG_SET 1U /**< Security Manager OOB Flag set. Flag selection is done using _POS defines */
vcoubard 28:041dac1366b2 66 #define AD_TYPE_SEC_MGR_OOB_FLAG_CLEAR 0U /**< Security Manager OOB Flag clear. Flag selection is done using _POS defines */
vcoubard 28:041dac1366b2 67 #define AD_TYPE_SEC_MGR_OOB_FLAG_OOB_DATA_PRESENT_POS 0UL /**< Security Manager OOB Data Present Flag position. */
vcoubard 28:041dac1366b2 68 #define AD_TYPE_SEC_MGR_OOB_FLAG_OOB_LE_SUPPORTED_POS 1UL /**< Security Manager OOB Low Energy Supported Flag position. */
vcoubard 28:041dac1366b2 69 #define AD_TYPE_SEC_MGR_OOB_FLAG_SIM_LE_AND_EP_POS 2UL /**< Security Manager OOB Simultaneous LE and BR/EDR to Same Device Capable Flag position. */
vcoubard 28:041dac1366b2 70 #define AD_TYPE_SEC_MGR_OOB_ADDRESS_TYPE_PUBLIC 0UL /**< Security Manager OOB Public Address type. */
vcoubard 28:041dac1366b2 71 #define AD_TYPE_SEC_MGR_OOB_ADDRESS_TYPE_RANDOM 1UL /**< Security Manager OOB Random Address type. */
vcoubard 28:041dac1366b2 72 #define AD_TYPE_SEC_MGR_OOB_FLAG_ADDRESS_TYPE_POS 3UL /**< Security Manager OOB Address type Flag (0 = Public Address, 1 = Random Address) position. */
vcoubard 28:041dac1366b2 73
vcoubard 28:041dac1366b2 74
vcoubard 28:041dac1366b2 75 /**@brief Security Manager TK value. */
vcoubard 28:041dac1366b2 76 typedef struct
vcoubard 28:041dac1366b2 77 {
vcoubard 28:041dac1366b2 78 uint8_t tk[BLE_GAP_SEC_KEY_LEN]; /**< Array containing TK value. */
vcoubard 28:041dac1366b2 79 } ble_advdata_tk_value_t;
vcoubard 28:041dac1366b2 80
vcoubard 28:041dac1366b2 81 /**@brief Advertising data LE Role types. This enumeration contains the options available for the LE role inside
vcoubard 28:041dac1366b2 82 * the advertising data. */
vcoubard 28:041dac1366b2 83 typedef enum
vcoubard 28:041dac1366b2 84 {
vcoubard 28:041dac1366b2 85 BLE_ADVDATA_ROLE_NOT_PRESENT = 0, /**< LE Role AD structure not present. */
vcoubard 28:041dac1366b2 86 BLE_ADVDATA_ROLE_ONLY_PERIPH, /**< Only Peripheral Role supported. */
vcoubard 28:041dac1366b2 87 BLE_ADVDATA_ROLE_ONLY_CENTRAL, /**< Only Central Role supported. */
vcoubard 28:041dac1366b2 88 BLE_ADVDATA_ROLE_BOTH_PERIPH_PREFERRED, /**< Peripheral and Central Role supported. Peripheral Role preferred for connection establishment. */
vcoubard 28:041dac1366b2 89 BLE_ADVDATA_ROLE_BOTH_CENTRAL_PREFERRED /**< Peripheral and Central Role supported. Central Role preferred for connection establishment */
vcoubard 28:041dac1366b2 90 } ble_advdata_le_role_t;
vcoubard 28:041dac1366b2 91
vcoubard 28:041dac1366b2 92 /**@brief Advertising data name type. This enumeration contains the options available for the device name inside
Vincent Coubard 0:f2542974c862 93 * the advertising data. */
Vincent Coubard 0:f2542974c862 94 typedef enum
Vincent Coubard 0:f2542974c862 95 {
Vincent Coubard 0:f2542974c862 96 BLE_ADVDATA_NO_NAME, /**< Include no device name in advertising data. */
Vincent Coubard 0:f2542974c862 97 BLE_ADVDATA_SHORT_NAME, /**< Include short device name in advertising data. */
Vincent Coubard 0:f2542974c862 98 BLE_ADVDATA_FULL_NAME /**< Include full device name in advertising data. */
Vincent Coubard 0:f2542974c862 99 } ble_advdata_name_type_t;
Vincent Coubard 0:f2542974c862 100
Vincent Coubard 0:f2542974c862 101 /**@brief UUID list type. */
Vincent Coubard 0:f2542974c862 102 typedef struct
Vincent Coubard 0:f2542974c862 103 {
Vincent Coubard 0:f2542974c862 104 uint16_t uuid_cnt; /**< Number of UUID entries. */
Vincent Coubard 0:f2542974c862 105 ble_uuid_t * p_uuids; /**< Pointer to UUID array entries. */
Vincent Coubard 0:f2542974c862 106 } ble_advdata_uuid_list_t;
Vincent Coubard 0:f2542974c862 107
Vincent Coubard 0:f2542974c862 108 /**@brief Connection interval range structure. */
Vincent Coubard 0:f2542974c862 109 typedef struct
Vincent Coubard 0:f2542974c862 110 {
vcoubard 28:041dac1366b2 111 uint16_t min_conn_interval; /**< Minimum connection interval, in units of 1.25 ms, range 6 to 3200 (7.5 ms to 4 s). */
vcoubard 28:041dac1366b2 112 uint16_t max_conn_interval; /**< Maximum connection interval, in units of 1.25 ms, range 6 to 3200 (7.5 ms to 4 s). The value 0xFFFF indicates no specific maximum. */
Vincent Coubard 0:f2542974c862 113 } ble_advdata_conn_int_t;
Vincent Coubard 0:f2542974c862 114
Vincent Coubard 0:f2542974c862 115 /**@brief Manufacturer specific data structure. */
Vincent Coubard 0:f2542974c862 116 typedef struct
Vincent Coubard 0:f2542974c862 117 {
vcoubard 28:041dac1366b2 118 uint16_t company_identifier; /**< Company identifier code. */
Vincent Coubard 0:f2542974c862 119 uint8_array_t data; /**< Additional manufacturer specific data. */
Vincent Coubard 0:f2542974c862 120 } ble_advdata_manuf_data_t;
Vincent Coubard 0:f2542974c862 121
Vincent Coubard 0:f2542974c862 122 /**@brief Service data structure. */
Vincent Coubard 0:f2542974c862 123 typedef struct
Vincent Coubard 0:f2542974c862 124 {
Vincent Coubard 0:f2542974c862 125 uint16_t service_uuid; /**< Service UUID. */
Vincent Coubard 0:f2542974c862 126 uint8_array_t data; /**< Additional service data. */
Vincent Coubard 0:f2542974c862 127 } ble_advdata_service_data_t;
Vincent Coubard 0:f2542974c862 128
vcoubard 28:041dac1366b2 129 /**@brief Advertising data structure. This structure contains all options and data needed for encoding and
Vincent Coubard 0:f2542974c862 130 * setting the advertising data. */
Vincent Coubard 0:f2542974c862 131 typedef struct
Vincent Coubard 0:f2542974c862 132 {
Vincent Coubard 0:f2542974c862 133 ble_advdata_name_type_t name_type; /**< Type of device name. */
Vincent Coubard 0:f2542974c862 134 uint8_t short_name_len; /**< Length of short device name (if short type is specified). */
Vincent Coubard 0:f2542974c862 135 bool include_appearance; /**< Determines if Appearance shall be included. */
Vincent Coubard 0:f2542974c862 136 uint8_t flags; /**< Advertising data Flags field. */
Vincent Coubard 0:f2542974c862 137 int8_t * p_tx_power_level; /**< TX Power Level field. */
Vincent Coubard 0:f2542974c862 138 ble_advdata_uuid_list_t uuids_more_available; /**< List of UUIDs in the 'More Available' list. */
Vincent Coubard 0:f2542974c862 139 ble_advdata_uuid_list_t uuids_complete; /**< List of UUIDs in the 'Complete' list. */
vcoubard 28:041dac1366b2 140 ble_advdata_uuid_list_t uuids_solicited; /**< List of solicited UUIDs. */
Vincent Coubard 0:f2542974c862 141 ble_advdata_conn_int_t * p_slave_conn_int; /**< Slave Connection Interval Range. */
Vincent Coubard 0:f2542974c862 142 ble_advdata_manuf_data_t * p_manuf_specific_data; /**< Manufacturer specific data. */
Vincent Coubard 0:f2542974c862 143 ble_advdata_service_data_t * p_service_data_array; /**< Array of Service data structures. */
Vincent Coubard 0:f2542974c862 144 uint8_t service_data_count; /**< Number of Service data structures. */
vcoubard 28:041dac1366b2 145 bool include_ble_device_addr; /**< Determines if LE Bluetooth Device Address shall be included. */
vcoubard 28:041dac1366b2 146 ble_advdata_le_role_t le_role; /**< LE Role field. Included when different from @ref BLE_ADVDATA_ROLE_NOT_PRESENT.*/
vcoubard 28:041dac1366b2 147 ble_advdata_tk_value_t * p_tk_value; /**< Security Manager TK value field. Included when different from NULL.*/
vcoubard 28:041dac1366b2 148 uint8_t * p_sec_mgr_oob_flags; /**< Security Manager Out Of Band Flags field. Included when different from NULL.*/
Vincent Coubard 0:f2542974c862 149 } ble_advdata_t;
Vincent Coubard 0:f2542974c862 150
vcoubard 28:041dac1366b2 151 /**@brief Function for encoding data in the Advertising and Scan Response data format
vcoubard 28:041dac1366b2 152 * (AD structures).
vcoubard 28:041dac1366b2 153 *
vcoubard 28:041dac1366b2 154 * @details This function encodes data into the Advertising and Scan Response data format
vcoubard 28:041dac1366b2 155 * (AD structures) based on the selections in the supplied structures. This function can be used to
vcoubard 28:041dac1366b2 156 * create a payload of Advertising packet or Scan Response packet, or a payload of NFC
vcoubard 28:041dac1366b2 157 * message intended for initiating the Out-of-Band pairing.
vcoubard 28:041dac1366b2 158 *
vcoubard 28:041dac1366b2 159 * @param[in] p_advdata Pointer to the structure for specifying the content of encoded data.
vcoubard 28:041dac1366b2 160 * @param[out] p_encoded_data Pointer to the buffer where encoded data will be returned.
vcoubard 28:041dac1366b2 161 * @param[in,out] p_len \c in: Size of \p p_encoded_data buffer.
vcoubard 28:041dac1366b2 162 * \c out: Length of encoded data.
vcoubard 28:041dac1366b2 163 *
vcoubard 28:041dac1366b2 164 * @retval NRF_SUCCESS If the operation was successful.
vcoubard 28:041dac1366b2 165 * @retval NRF_ERROR_INVALID_PARAM If the operation failed because a wrong parameter was provided in \p p_advdata.
vcoubard 28:041dac1366b2 166 * @retval NRF_ERROR_DATA_SIZE If the operation failed because not all the requested data could fit into the
vcoubard 28:041dac1366b2 167 * provided buffer or some encoded AD structure is too long and its
vcoubard 28:041dac1366b2 168 * length cannot be encoded with one octet.
vcoubard 28:041dac1366b2 169 *
vcoubard 28:041dac1366b2 170 * @warning This API may override the application's request to use the long name and use a short name
vcoubard 28:041dac1366b2 171 * instead. This truncation will occur in case the long name does not fit the provided buffer size.
vcoubard 28:041dac1366b2 172 * The application can specify a preferred short name length if truncation is required.
vcoubard 28:041dac1366b2 173 * For example, if the complete device name is ABCD_HRMonitor, the application can specify the short name
vcoubard 28:041dac1366b2 174 * length to be 8, so that the short device name appears as ABCD_HRM instead of ABCD_HRMo or ABCD_HRMoni
vcoubard 28:041dac1366b2 175 * if the available size for the short name is 9 or 12 respectively, to have a more appropriate short name.
vcoubard 28:041dac1366b2 176 * However, it should be noted that this is just a preference that the application can specify, and
vcoubard 28:041dac1366b2 177 * if the preference is too large to fit in the provided buffer, the name can be truncated further.
vcoubard 28:041dac1366b2 178 */
vcoubard 28:041dac1366b2 179 uint32_t adv_data_encode(ble_advdata_t const * const p_advdata,
vcoubard 28:041dac1366b2 180 uint8_t * const p_encoded_data,
vcoubard 28:041dac1366b2 181 uint16_t * const p_len);
vcoubard 28:041dac1366b2 182
Vincent Coubard 0:f2542974c862 183 /**@brief Function for encoding and setting the advertising data and/or scan response data.
Vincent Coubard 0:f2542974c862 184 *
Vincent Coubard 0:f2542974c862 185 * @details This function encodes advertising data and/or scan response data based on the selections
Vincent Coubard 0:f2542974c862 186 * in the supplied structures, and passes the encoded data to the stack.
Vincent Coubard 0:f2542974c862 187 *
Vincent Coubard 0:f2542974c862 188 * @param[in] p_advdata Structure for specifying the content of the advertising data.
Vincent Coubard 0:f2542974c862 189 * Set to NULL if advertising data is not to be set.
Vincent Coubard 0:f2542974c862 190 * @param[in] p_srdata Structure for specifying the content of the scan response data.
Vincent Coubard 0:f2542974c862 191 * Set to NULL if scan response data is not to be set.
Vincent Coubard 0:f2542974c862 192 *
vcoubard 28:041dac1366b2 193 * @retval NRF_SUCCESS If the operation was successful.
vcoubard 28:041dac1366b2 194 * @retval NRF_ERROR_INVALID_PARAM If the operation failed because a wrong parameter was provided in \p p_advdata.
vcoubard 28:041dac1366b2 195 * @retval NRF_ERROR_DATA_SIZE If the operation failed because not all the requested data could fit into the
vcoubard 28:041dac1366b2 196 * advertising packet. The maximum size of the advertisement packet
vcoubard 28:041dac1366b2 197 * is @ref BLE_GAP_ADV_MAX_SIZE.
Vincent Coubard 0:f2542974c862 198 *
vcoubard 28:041dac1366b2 199 * @warning This API may override the application's request to use the long name and use a short name
vcoubard 28:041dac1366b2 200 * instead. This truncation will occur in case the long name does not fit the provided buffer size.
vcoubard 28:041dac1366b2 201 * The application can specify a preferred short name length if truncation is required.
vcoubard 28:041dac1366b2 202 * For example, if the complete device name is ABCD_HRMonitor, the application can specify the short name
vcoubard 28:041dac1366b2 203 * length to be 8, so that the short device name appears as ABCD_HRM instead of ABCD_HRMo or ABCD_HRMoni
vcoubard 28:041dac1366b2 204 * if the available size for the short name is 9 or 12 respectively, to have a more appropriate short name.
vcoubard 28:041dac1366b2 205 * However, it should be noted that this is just a preference that the application can specify, and
vcoubard 28:041dac1366b2 206 * if the preference is too large to fit in the provided buffer, the name can be truncated further.
Vincent Coubard 0:f2542974c862 207 */
Vincent Coubard 0:f2542974c862 208 uint32_t ble_advdata_set(const ble_advdata_t * p_advdata, const ble_advdata_t * p_srdata);
Vincent Coubard 0:f2542974c862 209
Vincent Coubard 0:f2542974c862 210 #endif // BLE_ADVDATA_H__
Vincent Coubard 0:f2542974c862 211
vcoubard 1:ebc0e0ef0a11 212 /** @} */