ble_advdata.h 15 KB

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