ble_advdata.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /**
  2. * Copyright (c) 2012 - 2019, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. /** @file
  41. *
  42. * @defgroup ble_sdk_lib_advdata Advertising and Scan Response Data Encoder
  43. * @{
  44. * @ingroup ble_sdk_lib
  45. * @brief Functions for encoding data in the Advertising and Scan Response Data format,
  46. * and for passing the data to the stack.
  47. */
  48. #ifndef BLE_ADVDATA_H__
  49. #define BLE_ADVDATA_H__
  50. #include <stdint.h>
  51. #include <stdbool.h>
  52. #include <string.h>
  53. #include "ble.h"
  54. #include "sdk_common.h"
  55. #ifdef __cplusplus
  56. extern "C" {
  57. #endif
  58. #define AD_LENGTH_FIELD_SIZE 1UL /**< Advertising Data and Scan Response format contains 1 octet for the length. */
  59. #define AD_TYPE_FIELD_SIZE 1UL /**< Advertising Data and Scan Response format contains 1 octet for the AD type. */
  60. #define AD_DATA_OFFSET (AD_LENGTH_FIELD_SIZE + AD_TYPE_FIELD_SIZE) /**< Offset for the AD data field of the Advertising Data and Scan Response format. */
  61. #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. */
  62. #define AD_TYPE_BLE_DEVICE_ADDR_DATA_SIZE (BLE_GAP_ADDR_LEN + \
  63. AD_TYPE_BLE_DEVICE_ADDR_TYPE_SIZE) /**< Data size (in octets) of the LE Bluetooth Device Address AD type. */
  64. #define AD_TYPE_BLE_DEVICE_ADDR_SIZE (AD_DATA_OFFSET + \
  65. AD_TYPE_BLE_DEVICE_ADDR_DATA_SIZE) /**< Size (in octets) of the LE Bluetooth Device Address AD type. */
  66. #define AD_TYPE_APPEARANCE_DATA_SIZE 2UL /**< Data size (in octets) of the Appearance AD type. */
  67. #define AD_TYPE_APPEARANCE_SIZE (AD_DATA_OFFSET + \
  68. AD_TYPE_APPEARANCE_DATA_SIZE) /**< Size (in octets) of the Appearance AD type. */
  69. #define AD_TYPE_FLAGS_DATA_SIZE 1UL /**< Data size (in octets) of the Flags AD type. */
  70. #define AD_TYPE_FLAGS_SIZE (AD_DATA_OFFSET + \
  71. AD_TYPE_FLAGS_DATA_SIZE) /**< Size (in octets) of the Flags AD type. */
  72. #define AD_TYPE_TX_POWER_LEVEL_DATA_SIZE 1UL /**< Data size (in octets) of the TX Power Level AD type. */
  73. #define AD_TYPE_TX_POWER_LEVEL_SIZE (AD_DATA_OFFSET + \
  74. AD_TYPE_TX_POWER_LEVEL_DATA_SIZE) /**< Size (in octets) of the TX Power Level AD type. */
  75. #define AD_TYPE_CONN_INT_DATA_SIZE 4UL /**< Data size (in octets) of the Slave Connection Interval Range AD type. */
  76. #define AD_TYPE_CONN_INT_SIZE (AD_DATA_OFFSET + \
  77. AD_TYPE_CONN_INT_DATA_SIZE) /**< Data size (in octets) of the Slave Connection Interval Range AD type. */
  78. #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. */
  79. #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. */
  80. #define BLE_ADV_DATA_MATCH_FULL_NAME 0xff
  81. /**@brief Security Manager TK value. */
  82. typedef struct
  83. {
  84. uint8_t tk[BLE_GAP_SEC_KEY_LEN]; /**< Array containing TK value in little-endian format. */
  85. } ble_advdata_tk_value_t;
  86. /**@brief Advertising data LE Role types. This enumeration contains the options available for the LE role inside
  87. * the advertising data. */
  88. typedef enum
  89. {
  90. BLE_ADVDATA_ROLE_NOT_PRESENT = 0, /**< LE Role AD structure not present. */
  91. BLE_ADVDATA_ROLE_ONLY_PERIPH, /**< Only Peripheral Role supported. */
  92. BLE_ADVDATA_ROLE_ONLY_CENTRAL, /**< Only Central Role supported. */
  93. BLE_ADVDATA_ROLE_BOTH_PERIPH_PREFERRED, /**< Peripheral and Central Role supported. Peripheral Role preferred for connection establishment. */
  94. BLE_ADVDATA_ROLE_BOTH_CENTRAL_PREFERRED /**< Peripheral and Central Role supported. Central Role preferred for connection establishment */
  95. } ble_advdata_le_role_t;
  96. /**@brief Advertising data name type. This enumeration contains the options available for the device name inside
  97. * the advertising data. */
  98. typedef enum
  99. {
  100. BLE_ADVDATA_NO_NAME=0, /**< Include no device name in advertising data. */
  101. BLE_ADVDATA_SHORT_NAME=1, /**< Include short device name in advertising data. */
  102. BLE_ADVDATA_FULL_NAME=2 /**< Include full device name in advertising data. */
  103. } ble_advdata_name_type_t;
  104. /**@brief UUID list type. */
  105. typedef struct
  106. {
  107. uint16_t uuid_cnt; /**< Number of UUID entries. */
  108. ble_uuid_t * p_uuids; /**< Pointer to UUID array entries. */
  109. } ble_advdata_uuid_list_t;
  110. /**@brief Connection interval range structure. */
  111. typedef struct
  112. {
  113. uint16_t min_conn_interval; /**< Minimum connection interval, in units of 1.25 ms, range 6 to 3200 (7.5 ms to 4 s). */
  114. 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. */
  115. } ble_advdata_conn_int_t;
  116. /**@brief Manufacturer specific data structure. */
  117. typedef struct
  118. {
  119. uint16_t company_identifier; /**< Company identifier code. */
  120. uint8_array_t data; /**< Additional manufacturer specific data. */
  121. } ble_advdata_manuf_data_t;
  122. /**@brief Service data structure. */
  123. typedef struct
  124. {
  125. uint16_t service_uuid; /**< Service UUID. */
  126. uint8_array_t data; /**< Additional service data. */
  127. } ble_advdata_service_data_t;
  128. /**@brief Advertising data structure. This structure contains all options and data needed for encoding and
  129. * setting the advertising data. */
  130. typedef struct
  131. {
  132. ble_advdata_name_type_t name_type; /**< Type of device name. */
  133. uint8_t short_name_len; /**< Length of short device name (if short type is specified). */
  134. bool include_appearance; /**< Determines if Appearance shall be included. */
  135. uint8_t flags; /**< Advertising data Flags field. */
  136. int8_t * p_tx_power_level; /**< TX Power Level field. */
  137. ble_advdata_uuid_list_t uuids_more_available; /**< List of UUIDs in the 'More Available' list. */
  138. ble_advdata_uuid_list_t uuids_complete; /**< List of UUIDs in the 'Complete' list. */
  139. ble_advdata_uuid_list_t uuids_solicited; /**< List of solicited UUIDs. */
  140. ble_advdata_conn_int_t * p_slave_conn_int; /**< Slave Connection Interval Range. */
  141. ble_advdata_manuf_data_t * p_manuf_specific_data; /**< Manufacturer specific data. */
  142. ble_advdata_service_data_t * p_service_data_array; /**< Array of Service data structures. */
  143. uint8_t service_data_count; /**< Number of Service data structures. */
  144. bool include_ble_device_addr; /**< Determines if LE Bluetooth Device Address shall be included. */
  145. 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. */
  146. 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.*/
  147. 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.*/
  148. ble_gap_lesc_oob_data_t * p_lesc_data; /**< LE Secure Connections OOB data. Included when different from NULL. @warning This field can be used only for NFC. For BLE advertising, set it to NULL.*/
  149. } ble_advdata_t;
  150. /**@brief Function for encoding data in the Advertising and Scan Response data format (AD structures).
  151. *
  152. * @details This function encodes data into the Advertising and Scan Response data format
  153. * (AD structures) based on the fields in the supplied structures. This function can be
  154. * used to create a payload of Advertising packet or Scan Response packet, or a payload of
  155. * NFC message intended for initiating the Out-of-Band pairing.
  156. *
  157. * @param[in] p_advdata Pointer to the structure for specifying the content of encoded data.
  158. * @param[out] p_encoded_data Pointer to the buffer where encoded data will be returned.
  159. * @param[in,out] p_len \c in: Size of \p p_encoded_data buffer.
  160. * \c out: Length of encoded data.
  161. *
  162. * @retval NRF_SUCCESS If the operation was successful.
  163. * @retval NRF_ERROR_INVALID_PARAM If the operation failed because a wrong parameter was provided in
  164. * \p p_advdata.
  165. * @retval NRF_ERROR_DATA_SIZE If the operation failed because not all the requested data could
  166. * fit into the provided buffer or some encoded AD structure is too
  167. * long and its length cannot be encoded with one octet.
  168. *
  169. * @warning This API may override the application's request to use the long name and use a short name
  170. * instead. This truncation will occur in case the long name does not fit the provided buffer size.
  171. * The application can specify a preferred short name length if truncation is required.
  172. * For example, if the complete device name is ABCD_HRMonitor, the application can specify the short name
  173. * length to be 8, so that the short device name appears as ABCD_HRM instead of ABCD_HRMo or ABCD_HRMoni
  174. * if the available size for the short name is 9 or 12 respectively, to have a more appropriate short name.
  175. * However, it should be noted that this is just a preference that the application can specify, and
  176. * if the preference is too large to fit in the provided buffer, the name can be truncated further.
  177. */
  178. ret_code_t ble_advdata_encode(ble_advdata_t const * const p_advdata,
  179. uint8_t * const p_encoded_data,
  180. uint16_t * const p_len);
  181. /**@brief Function for searching encoded Advertising or Scan Response data for specific data types.
  182. *
  183. * @details This function searches through encoded data e.g. the data produced by
  184. * @ref ble_advdata_encode, or the data found in Advertising reports
  185. * (@ref BLE_GAP_EVT_ADV_REPORT), and gives the offset of the data within the data buffer.
  186. * The data with type \p ad_type can be found at p_encoded_data[*p_offset] after calling
  187. * the function. This function can iterate through multiple instances of data of one
  188. * type by calling it again with the offset provided by the previous call.
  189. *
  190. * Example code for finding multiple instances of one type of data:
  191. * offset = 0;
  192. * ble_advdata_search(&data, len, &offset, AD_TYPE);
  193. * first_instance_of_data = data[offset];
  194. * ble_advdata_search(&data, len, &offset, AD_TYPE);
  195. * second_instance_of_data = data[offset];
  196. *
  197. * @param[in] p_encoded_data The data buffer containing the encoded Advertising data.
  198. * @param[in] data_len The length of the data buffer \p p_encoded_data.
  199. * @param[inout] p_offset \c in: The offset to start searching from.
  200. * \c out: The offset the data type can be found at.
  201. * This value is not changed if the call returns 0.
  202. * @param[in] ad_type The type of data to search for.
  203. *
  204. * @return The length of the found data, or 0 if no data was found with the the type \p ad_type,
  205. * or if \p p_encoded_data or \p p_offset were NULL.
  206. */
  207. uint16_t ble_advdata_search(uint8_t const * p_encoded_data,
  208. uint16_t data_len,
  209. uint16_t * p_offset,
  210. uint8_t ad_type);
  211. /**@brief Function for getting specific data from encoded Advertising or Scan Response data.
  212. *
  213. * @details This function searches through encoded data e.g. the data produced by
  214. * @ref ble_advdata_encode, or the data found in Advertising reports
  215. * (@ref BLE_GAP_EVT_ADV_REPORT), and returns a pointer directly to the data within the
  216. * data buffer.
  217. *
  218. * Example code:
  219. * ad_type_data = ble_advdata_parse(&data, len, AD_TYPE);
  220. *
  221. * @param[in] p_encoded_data Data buffer containing the encoded Advertising data.
  222. * @param[in] data_len Length of the data buffer \p p_encoded_data.
  223. * @param[in] ad_type Type of data to search for.
  224. *
  225. * @return Pointer to the found data, or NULL if no data was found with the type \p ad_type,
  226. * or if \p p_encoded_data or \p p_data_len were NULL.
  227. */
  228. uint8_t * ble_advdata_parse(uint8_t * p_encoded_data,
  229. uint16_t data_len,
  230. uint8_t ad_type);
  231. /**@brief Function for searching through encoded Advertising data for a complete local name.
  232. *
  233. * @param[in] p_encoded_data Data buffer containing the encoded Advertising data.
  234. * @param[in] data_len Length of the data buffer \p p_encoded_data.
  235. * @param[in] p_target_name Name to search for.
  236. *
  237. * @retval true If \p p_target_name was found among \p p_encoded_data, as a complete local name.
  238. * @retval false If \p p_target_name was not found among \p p_encoded_data, or if \p p_encoded_data
  239. * or \p p_target_name was NULL.
  240. */
  241. bool ble_advdata_name_find(uint8_t const * p_encoded_data,
  242. uint16_t data_len,
  243. char const * p_target_name);
  244. /**@brief Function for searching through encoded Advertising data for a device shortened name.
  245. *
  246. * @param[in] p_encoded_data Data buffer containing the encoded Advertising data.
  247. * @param[in] data_len Length of the data buffer \p p_encoded_data.
  248. * @param[in] p_target_name Name to search for.
  249. * @param[in] short_name_min_len Minimum length of the shortened name.
  250. * For example, if the advertising data has a shortened name 'No' and this parameter is
  251. * set to 4 with a target_name set to Nordic_XXX it will return false, but if
  252. * the shortened name in the advertising data is 'Nord', it will return true.
  253. * @note: If the shortened name in the Advertising data has the same length as the target name,
  254. * this function will return false, since this means that the complete name is actually
  255. * longer, thus different than the target name.
  256. *
  257. * @retval true If \p p_target_name was found among \p p_encoded_data, as short local name.
  258. * @retval false If \p p_target_name was not found among \p p_encoded_data, or if \p p_encoded_data
  259. * or \p p_target_name was NULL.
  260. */
  261. bool ble_advdata_short_name_find(uint8_t const * p_encoded_data,
  262. uint16_t data_len,
  263. char const * p_target_name,
  264. uint8_t const short_name_min_len);
  265. /**@brief Function for searching through encoded Advertising data for a UUID (16-bit or 128-bit).
  266. *
  267. * @param[in] p_encoded_data Data buffer containing the encoded Advertising data.
  268. * @param[in] data_len Length of the data buffer \p p_encoded_data.
  269. * @param[in] p_target_uuid UUID to search for.
  270. *
  271. * @retval true If \p p_target_uuid was found among \p p_encoded_data.
  272. * @retval false If \p p_target_uuid was not found among \p p_encoded_data, or if \p p_encoded_data
  273. * or \p p_target_uuid was NULL.
  274. */
  275. bool ble_advdata_uuid_find(uint8_t const * p_encoded_data,
  276. uint16_t data_len,
  277. ble_uuid_t const * p_target_uuid);
  278. /**@brief Function for searching through encoded Advertising data for an appearance.
  279. *
  280. * @param[in] p_encoded_data Data buffer containing the encoded Advertising data.
  281. * @param[in] data_len Length of the data buffer \p p_encoded_data.
  282. * @param[in] p_target_appearance Appearance to search for.
  283. *
  284. * @retval true If \p p_target_appearance was found among \p p_encoded_data.
  285. * @retval false If \p p_target_appearance was not found among \p p_encoded_data, or if \p p_encoded_data
  286. * or \p p_target_appearance was NULL.
  287. */
  288. bool ble_advdata_appearance_find(uint8_t const * p_encoded_data,
  289. uint16_t data_len,
  290. uint16_t const * p_target_appearance);
  291. #ifdef __cplusplus
  292. }
  293. #endif
  294. #endif // BLE_ADVDATA_H__
  295. /** @} */