ble_advdata.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  1. /**
  2. * Copyright (c) 2012 - 2018, 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. #include "ble_advdata.h"
  41. #include "ble_gap.h"
  42. #include "ble_srv_common.h"
  43. #include "sdk_common.h"
  44. // NOTE: For now, Security Manager Out of Band Flags (OOB) are omitted from the advertising data.
  45. // Types of LE Bluetooth Device Address AD type
  46. #define AD_TYPE_BLE_DEVICE_ADDR_TYPE_PUBLIC 0UL
  47. #define AD_TYPE_BLE_DEVICE_ADDR_TYPE_RANDOM 1UL
  48. #define UUID16_SIZE 2 /**< Size of 16 bit UUID. */
  49. #define UUID32_SIZE 4 /**< Size of 32 bit UUID. */
  50. #define UUID128_SIZE 16 /**< Size of 128 bit UUID. */
  51. #define N_AD_TYPES 2 /**< The number of Advertising data types to search for at a time. */
  52. static ret_code_t ble_device_addr_encode(uint8_t * p_encoded_data,
  53. uint16_t * p_offset,
  54. uint16_t max_size)
  55. {
  56. ret_code_t err_code;
  57. ble_gap_addr_t device_addr;
  58. // Check for buffer overflow.
  59. if (((*p_offset) + AD_TYPE_BLE_DEVICE_ADDR_SIZE) > max_size)
  60. {
  61. return NRF_ERROR_DATA_SIZE;
  62. }
  63. // Get BLE address.
  64. err_code = sd_ble_gap_addr_get(&device_addr);
  65. VERIFY_SUCCESS(err_code);
  66. // Encode LE Bluetooth Device Address.
  67. p_encoded_data[*p_offset] = (uint8_t)(AD_TYPE_FIELD_SIZE +
  68. AD_TYPE_BLE_DEVICE_ADDR_DATA_SIZE);
  69. *p_offset += AD_LENGTH_FIELD_SIZE;
  70. p_encoded_data[*p_offset] = BLE_GAP_AD_TYPE_LE_BLUETOOTH_DEVICE_ADDRESS;
  71. *p_offset += AD_TYPE_FIELD_SIZE;
  72. memcpy(&p_encoded_data[*p_offset], &device_addr.addr[0], BLE_GAP_ADDR_LEN);
  73. *p_offset += BLE_GAP_ADDR_LEN;
  74. if (BLE_GAP_ADDR_TYPE_PUBLIC == device_addr.addr_type)
  75. {
  76. p_encoded_data[*p_offset] = AD_TYPE_BLE_DEVICE_ADDR_TYPE_PUBLIC;
  77. }
  78. else
  79. {
  80. p_encoded_data[*p_offset] = AD_TYPE_BLE_DEVICE_ADDR_TYPE_RANDOM;
  81. }
  82. *p_offset += AD_TYPE_BLE_DEVICE_ADDR_TYPE_SIZE;
  83. return NRF_SUCCESS;
  84. }
  85. static ret_code_t name_encode(const ble_advdata_t * p_advdata,
  86. uint8_t * p_encoded_data,
  87. uint16_t * p_offset,
  88. uint16_t max_size)
  89. {
  90. ret_code_t err_code;
  91. uint16_t rem_adv_data_len;
  92. uint16_t actual_length;
  93. uint8_t adv_data_format;
  94. // Validate parameters
  95. if ((BLE_ADVDATA_SHORT_NAME == p_advdata->name_type) && (0 == p_advdata->short_name_len))
  96. {
  97. return NRF_ERROR_INVALID_PARAM;
  98. }
  99. // Check for buffer overflow.
  100. if ( (((*p_offset) + AD_DATA_OFFSET) > max_size) ||
  101. ( (BLE_ADVDATA_SHORT_NAME == p_advdata->name_type) &&
  102. (((*p_offset) + AD_DATA_OFFSET + p_advdata->short_name_len) > max_size)))
  103. {
  104. return NRF_ERROR_DATA_SIZE;
  105. }
  106. rem_adv_data_len = max_size - (*p_offset) - AD_DATA_OFFSET;
  107. actual_length = rem_adv_data_len;
  108. // Get GAP device name and length
  109. err_code = sd_ble_gap_device_name_get(&p_encoded_data[(*p_offset) + AD_DATA_OFFSET],
  110. &actual_length);
  111. VERIFY_SUCCESS(err_code);
  112. // Check if device intend to use short name and it can fit available data size.
  113. // If the name is shorter than the preferred short name length then it is no longer
  114. // a short name and is in fact the complete name of the device.
  115. if (((p_advdata->name_type == BLE_ADVDATA_FULL_NAME) ||
  116. (actual_length <= p_advdata->short_name_len)) &&
  117. (actual_length <= rem_adv_data_len))
  118. {
  119. // Complete device name can fit, setting Complete Name in Adv Data.
  120. adv_data_format = BLE_GAP_AD_TYPE_COMPLETE_LOCAL_NAME;
  121. }
  122. else
  123. {
  124. // Else short name needs to be used. Or application has requested use of short name.
  125. adv_data_format = BLE_GAP_AD_TYPE_SHORT_LOCAL_NAME;
  126. // If application has set a preference on the short name size, it needs to be considered,
  127. // else fit what can be fit.
  128. if ((BLE_ADVDATA_SHORT_NAME == p_advdata->name_type) &&
  129. (p_advdata->short_name_len <= rem_adv_data_len))
  130. {
  131. // Short name fits available size.
  132. actual_length = p_advdata->short_name_len;
  133. }
  134. // Else whatever can fit the data buffer will be packed.
  135. else
  136. {
  137. actual_length = rem_adv_data_len;
  138. }
  139. }
  140. // There is only 1 byte intended to encode length which is (actual_length + AD_TYPE_FIELD_SIZE)
  141. if (actual_length > (0x00FF - AD_TYPE_FIELD_SIZE))
  142. {
  143. return NRF_ERROR_DATA_SIZE;
  144. }
  145. // Complete name field in encoded data.
  146. p_encoded_data[*p_offset] = (uint8_t)(AD_TYPE_FIELD_SIZE + actual_length);
  147. *p_offset += AD_LENGTH_FIELD_SIZE;
  148. p_encoded_data[*p_offset] = adv_data_format;
  149. *p_offset += AD_TYPE_FIELD_SIZE;
  150. *p_offset += actual_length;
  151. return NRF_SUCCESS;
  152. }
  153. static ret_code_t appearance_encode(uint8_t * p_encoded_data,
  154. uint16_t * p_offset,
  155. uint16_t max_size)
  156. {
  157. ret_code_t err_code;
  158. uint16_t appearance;
  159. // Check for buffer overflow.
  160. if (((*p_offset) + AD_TYPE_APPEARANCE_SIZE) > max_size)
  161. {
  162. return NRF_ERROR_DATA_SIZE;
  163. }
  164. // Get GAP appearance field.
  165. err_code = sd_ble_gap_appearance_get(&appearance);
  166. VERIFY_SUCCESS(err_code);
  167. // Encode Length, AD Type and Appearance.
  168. p_encoded_data[*p_offset] = (uint8_t)(AD_TYPE_FIELD_SIZE + AD_TYPE_APPEARANCE_DATA_SIZE);
  169. *p_offset += AD_LENGTH_FIELD_SIZE;
  170. p_encoded_data[*p_offset] = BLE_GAP_AD_TYPE_APPEARANCE;
  171. *p_offset += AD_TYPE_FIELD_SIZE;
  172. *p_offset += uint16_encode(appearance, &p_encoded_data[*p_offset]);
  173. return NRF_SUCCESS;
  174. }
  175. static ret_code_t flags_encode(int8_t flags,
  176. uint8_t * p_encoded_data,
  177. uint16_t * p_offset,
  178. uint16_t max_size)
  179. {
  180. // Check for buffer overflow.
  181. if (((*p_offset) + AD_TYPE_FLAGS_SIZE) > max_size)
  182. {
  183. return NRF_ERROR_DATA_SIZE;
  184. }
  185. // Encode flags.
  186. p_encoded_data[*p_offset] = (uint8_t)(AD_TYPE_FIELD_SIZE + AD_TYPE_FLAGS_DATA_SIZE);
  187. *p_offset += AD_LENGTH_FIELD_SIZE;
  188. p_encoded_data[*p_offset] = BLE_GAP_AD_TYPE_FLAGS;
  189. *p_offset += AD_TYPE_FIELD_SIZE;
  190. p_encoded_data[*p_offset] = flags;
  191. *p_offset += AD_TYPE_FLAGS_DATA_SIZE;
  192. return NRF_SUCCESS;
  193. }
  194. static ret_code_t tx_power_level_encode(int8_t tx_power_level,
  195. uint8_t * p_encoded_data,
  196. uint16_t * p_offset,
  197. uint16_t max_size)
  198. {
  199. // Check for buffer overflow.
  200. if (((*p_offset) + AD_TYPE_TX_POWER_LEVEL_SIZE) > max_size)
  201. {
  202. return NRF_ERROR_DATA_SIZE;
  203. }
  204. // Encode TX Power Level.
  205. p_encoded_data[*p_offset] = (uint8_t)(AD_TYPE_FIELD_SIZE +
  206. AD_TYPE_TX_POWER_LEVEL_DATA_SIZE);
  207. *p_offset += AD_LENGTH_FIELD_SIZE;
  208. p_encoded_data[*p_offset] = BLE_GAP_AD_TYPE_TX_POWER_LEVEL;
  209. *p_offset += AD_TYPE_FIELD_SIZE;
  210. p_encoded_data[*p_offset] = tx_power_level;
  211. *p_offset += AD_TYPE_TX_POWER_LEVEL_DATA_SIZE;
  212. return NRF_SUCCESS;
  213. }
  214. static ret_code_t uuid_list_sized_encode(const ble_advdata_uuid_list_t * p_uuid_list,
  215. uint8_t adv_type,
  216. uint8_t uuid_size,
  217. uint8_t * p_encoded_data,
  218. uint16_t * p_offset,
  219. uint16_t max_size)
  220. {
  221. int i;
  222. bool is_heading_written = false;
  223. uint16_t start_pos = *p_offset;
  224. uint16_t length;
  225. for (i = 0; i < p_uuid_list->uuid_cnt; i++)
  226. {
  227. ret_code_t err_code;
  228. uint8_t encoded_size;
  229. ble_uuid_t uuid = p_uuid_list->p_uuids[i];
  230. // Find encoded uuid size.
  231. err_code = sd_ble_uuid_encode(&uuid, &encoded_size, NULL);
  232. VERIFY_SUCCESS(err_code);
  233. // Check size.
  234. if (encoded_size == uuid_size)
  235. {
  236. uint8_t heading_bytes = (is_heading_written) ? 0 : AD_DATA_OFFSET;
  237. // Check for buffer overflow
  238. if (((*p_offset) + encoded_size + heading_bytes) > max_size)
  239. {
  240. return NRF_ERROR_DATA_SIZE;
  241. }
  242. if (!is_heading_written)
  243. {
  244. // Write AD structure heading.
  245. *p_offset += AD_LENGTH_FIELD_SIZE;
  246. p_encoded_data[*p_offset] = adv_type;
  247. *p_offset += AD_TYPE_FIELD_SIZE;
  248. is_heading_written = true;
  249. }
  250. // Write UUID.
  251. err_code = sd_ble_uuid_encode(&uuid, &encoded_size, &p_encoded_data[*p_offset]);
  252. VERIFY_SUCCESS(err_code);
  253. *p_offset += encoded_size;
  254. }
  255. }
  256. if (is_heading_written)
  257. {
  258. // Write length.
  259. length = (*p_offset) - (start_pos + AD_LENGTH_FIELD_SIZE);
  260. // There is only 1 byte intended to encode length
  261. if (length > 0x00FF)
  262. {
  263. return NRF_ERROR_DATA_SIZE;
  264. }
  265. p_encoded_data[start_pos] = (uint8_t)length;
  266. }
  267. return NRF_SUCCESS;
  268. }
  269. static ret_code_t uuid_list_encode(const ble_advdata_uuid_list_t * p_uuid_list,
  270. uint8_t adv_type_16,
  271. uint8_t adv_type_128,
  272. uint8_t * p_encoded_data,
  273. uint16_t * p_offset,
  274. uint16_t max_size)
  275. {
  276. ret_code_t err_code;
  277. // Encode 16 bit UUIDs.
  278. err_code = uuid_list_sized_encode(p_uuid_list,
  279. adv_type_16,
  280. sizeof(uint16_le_t),
  281. p_encoded_data,
  282. p_offset,
  283. max_size);
  284. VERIFY_SUCCESS(err_code);
  285. // Encode 128 bit UUIDs.
  286. err_code = uuid_list_sized_encode(p_uuid_list,
  287. adv_type_128,
  288. sizeof(ble_uuid128_t),
  289. p_encoded_data,
  290. p_offset,
  291. max_size);
  292. VERIFY_SUCCESS(err_code);
  293. return NRF_SUCCESS;
  294. }
  295. static ret_code_t conn_int_check(const ble_advdata_conn_int_t *p_conn_int)
  296. {
  297. // Check Minimum Connection Interval.
  298. if ((p_conn_int->min_conn_interval < 0x0006) ||
  299. (
  300. (p_conn_int->min_conn_interval > 0x0c80) &&
  301. (p_conn_int->min_conn_interval != 0xffff)
  302. )
  303. )
  304. {
  305. return NRF_ERROR_INVALID_PARAM;
  306. }
  307. // Check Maximum Connection Interval.
  308. if ((p_conn_int->max_conn_interval < 0x0006) ||
  309. (
  310. (p_conn_int->max_conn_interval > 0x0c80) &&
  311. (p_conn_int->max_conn_interval != 0xffff)
  312. )
  313. )
  314. {
  315. return NRF_ERROR_INVALID_PARAM;
  316. }
  317. // Make sure Minimum Connection Interval is not bigger than Maximum Connection Interval.
  318. if ((p_conn_int->min_conn_interval != 0xffff) &&
  319. (p_conn_int->max_conn_interval != 0xffff) &&
  320. (p_conn_int->min_conn_interval > p_conn_int->max_conn_interval)
  321. )
  322. {
  323. return NRF_ERROR_INVALID_PARAM;
  324. }
  325. return NRF_SUCCESS;
  326. }
  327. static ret_code_t conn_int_encode(const ble_advdata_conn_int_t * p_conn_int,
  328. uint8_t * p_encoded_data,
  329. uint16_t * p_offset,
  330. uint16_t max_size)
  331. {
  332. ret_code_t err_code;
  333. // Check for buffer overflow.
  334. if (((*p_offset) + AD_TYPE_CONN_INT_SIZE) > max_size)
  335. {
  336. return NRF_ERROR_DATA_SIZE;
  337. }
  338. // Check parameters.
  339. err_code = conn_int_check(p_conn_int);
  340. VERIFY_SUCCESS(err_code);
  341. // Encode Length and AD Type.
  342. p_encoded_data[*p_offset] = (uint8_t)(AD_TYPE_FIELD_SIZE + AD_TYPE_CONN_INT_DATA_SIZE);
  343. *p_offset += AD_LENGTH_FIELD_SIZE;
  344. p_encoded_data[*p_offset] = BLE_GAP_AD_TYPE_SLAVE_CONNECTION_INTERVAL_RANGE;
  345. *p_offset += AD_TYPE_FIELD_SIZE;
  346. // Encode Minimum and Maximum Connection Intervals.
  347. *p_offset += uint16_encode(p_conn_int->min_conn_interval, &p_encoded_data[*p_offset]);
  348. *p_offset += uint16_encode(p_conn_int->max_conn_interval, &p_encoded_data[*p_offset]);
  349. return NRF_SUCCESS;
  350. }
  351. static ret_code_t manuf_specific_data_encode(const ble_advdata_manuf_data_t * p_manuf_sp_data,
  352. uint8_t * p_encoded_data,
  353. uint16_t * p_offset,
  354. uint16_t max_size)
  355. {
  356. uint32_t data_size = AD_TYPE_MANUF_SPEC_DATA_ID_SIZE + p_manuf_sp_data->data.size;
  357. // Check for buffer overflow.
  358. if (((*p_offset) + AD_DATA_OFFSET + data_size) > max_size)
  359. {
  360. return NRF_ERROR_DATA_SIZE;
  361. }
  362. // There is only 1 byte intended to encode length which is (data_size + AD_TYPE_FIELD_SIZE)
  363. if (data_size > (0x00FF - AD_TYPE_FIELD_SIZE))
  364. {
  365. return NRF_ERROR_DATA_SIZE;
  366. }
  367. // Encode Length and AD Type.
  368. p_encoded_data[*p_offset] = (uint8_t)(AD_TYPE_FIELD_SIZE + data_size);
  369. *p_offset += AD_LENGTH_FIELD_SIZE;
  370. p_encoded_data[*p_offset] = BLE_GAP_AD_TYPE_MANUFACTURER_SPECIFIC_DATA;
  371. *p_offset += AD_TYPE_FIELD_SIZE;
  372. // Encode Company Identifier.
  373. *p_offset += uint16_encode(p_manuf_sp_data->company_identifier, &p_encoded_data[*p_offset]);
  374. // Encode additional manufacturer specific data.
  375. if (p_manuf_sp_data->data.size > 0)
  376. {
  377. if (p_manuf_sp_data->data.p_data == NULL)
  378. {
  379. return NRF_ERROR_INVALID_PARAM;
  380. }
  381. memcpy(&p_encoded_data[*p_offset], p_manuf_sp_data->data.p_data, p_manuf_sp_data->data.size);
  382. *p_offset += p_manuf_sp_data->data.size;
  383. }
  384. return NRF_SUCCESS;
  385. }
  386. // Implemented only for 16-bit UUIDs
  387. static ret_code_t service_data_encode(const ble_advdata_t * p_advdata,
  388. uint8_t * p_encoded_data,
  389. uint16_t * p_offset,
  390. uint16_t max_size)
  391. {
  392. uint8_t i;
  393. // Check parameter consistency.
  394. if (p_advdata->p_service_data_array == NULL)
  395. {
  396. return NRF_ERROR_INVALID_PARAM;
  397. }
  398. for (i = 0; i < p_advdata->service_data_count; i++)
  399. {
  400. ble_advdata_service_data_t * p_service_data;
  401. uint32_t data_size;
  402. p_service_data = &p_advdata->p_service_data_array[i];
  403. // For now implemented only for 16-bit UUIDs
  404. data_size = AD_TYPE_SERV_DATA_16BIT_UUID_SIZE + p_service_data->data.size;
  405. // There is only 1 byte intended to encode length which is (data_size + AD_TYPE_FIELD_SIZE)
  406. if (data_size > (0x00FF - AD_TYPE_FIELD_SIZE))
  407. {
  408. return NRF_ERROR_DATA_SIZE;
  409. }
  410. // Encode Length and AD Type.
  411. p_encoded_data[*p_offset] = (uint8_t)(AD_TYPE_FIELD_SIZE + data_size);
  412. *p_offset += AD_LENGTH_FIELD_SIZE;
  413. p_encoded_data[*p_offset] = BLE_GAP_AD_TYPE_SERVICE_DATA;
  414. *p_offset += AD_TYPE_FIELD_SIZE;
  415. // Encode service 16-bit UUID.
  416. *p_offset += uint16_encode(p_service_data->service_uuid, &p_encoded_data[*p_offset]);
  417. // Encode additional service data.
  418. if (p_service_data->data.size > 0)
  419. {
  420. if (p_service_data->data.p_data == NULL)
  421. {
  422. return NRF_ERROR_INVALID_PARAM;
  423. }
  424. memcpy(&p_encoded_data[*p_offset], p_service_data->data.p_data, p_service_data->data.size);
  425. *p_offset += p_service_data->data.size;
  426. }
  427. }
  428. return NRF_SUCCESS;
  429. }
  430. ret_code_t ble_advdata_encode(ble_advdata_t const * const p_advdata,
  431. uint8_t * const p_encoded_data,
  432. uint16_t * const p_len)
  433. {
  434. ret_code_t err_code = NRF_SUCCESS;
  435. uint16_t max_size = *p_len;
  436. *p_len = 0;
  437. // Encode LE Bluetooth Device Address
  438. if (p_advdata->include_ble_device_addr)
  439. {
  440. err_code = ble_device_addr_encode(p_encoded_data, p_len, max_size);
  441. VERIFY_SUCCESS(err_code);
  442. }
  443. // Encode appearance.
  444. if (p_advdata->include_appearance)
  445. {
  446. err_code = appearance_encode(p_encoded_data, p_len, max_size);
  447. VERIFY_SUCCESS(err_code);
  448. }
  449. //Encode Flags
  450. if (p_advdata->flags != 0 )
  451. {
  452. err_code = flags_encode(p_advdata->flags, p_encoded_data, p_len, max_size);
  453. VERIFY_SUCCESS(err_code);
  454. }
  455. // Encode TX power level.
  456. if (p_advdata->p_tx_power_level != NULL)
  457. {
  458. err_code = tx_power_level_encode(*p_advdata->p_tx_power_level,
  459. p_encoded_data,
  460. p_len,
  461. max_size);
  462. VERIFY_SUCCESS(err_code);
  463. }
  464. // Encode 'more available' uuid list.
  465. if (p_advdata->uuids_more_available.uuid_cnt > 0)
  466. {
  467. err_code = uuid_list_encode(&p_advdata->uuids_more_available,
  468. BLE_GAP_AD_TYPE_16BIT_SERVICE_UUID_MORE_AVAILABLE,
  469. BLE_GAP_AD_TYPE_128BIT_SERVICE_UUID_MORE_AVAILABLE,
  470. p_encoded_data,
  471. p_len,
  472. max_size);
  473. VERIFY_SUCCESS(err_code);
  474. }
  475. // Encode 'complete' uuid list.
  476. if (p_advdata->uuids_complete.uuid_cnt > 0)
  477. {
  478. err_code = uuid_list_encode(&p_advdata->uuids_complete,
  479. BLE_GAP_AD_TYPE_16BIT_SERVICE_UUID_COMPLETE,
  480. BLE_GAP_AD_TYPE_128BIT_SERVICE_UUID_COMPLETE,
  481. p_encoded_data,
  482. p_len,
  483. max_size);
  484. VERIFY_SUCCESS(err_code);
  485. }
  486. // Encode 'solicited service' uuid list.
  487. if (p_advdata->uuids_solicited.uuid_cnt > 0)
  488. {
  489. err_code = uuid_list_encode(&p_advdata->uuids_solicited,
  490. BLE_GAP_AD_TYPE_SOLICITED_SERVICE_UUIDS_16BIT,
  491. BLE_GAP_AD_TYPE_SOLICITED_SERVICE_UUIDS_128BIT,
  492. p_encoded_data,
  493. p_len,
  494. max_size);
  495. VERIFY_SUCCESS(err_code);
  496. }
  497. // Encode Slave Connection Interval Range.
  498. if (p_advdata->p_slave_conn_int != NULL)
  499. {
  500. err_code = conn_int_encode(p_advdata->p_slave_conn_int, p_encoded_data, p_len, max_size);
  501. VERIFY_SUCCESS(err_code);
  502. }
  503. // Encode Manufacturer Specific Data.
  504. if (p_advdata->p_manuf_specific_data != NULL)
  505. {
  506. err_code = manuf_specific_data_encode(p_advdata->p_manuf_specific_data,
  507. p_encoded_data,
  508. p_len,
  509. max_size);
  510. VERIFY_SUCCESS(err_code);
  511. }
  512. // Encode Service Data.
  513. if (p_advdata->service_data_count > 0)
  514. {
  515. err_code = service_data_encode(p_advdata, p_encoded_data, p_len, max_size);
  516. VERIFY_SUCCESS(err_code);
  517. }
  518. // Encode name. WARNING: it is encoded last on purpose since too long device name is truncated.
  519. if (p_advdata->name_type != BLE_ADVDATA_NO_NAME)
  520. {
  521. err_code = name_encode(p_advdata, p_encoded_data, p_len, max_size);
  522. VERIFY_SUCCESS(err_code);
  523. }
  524. return err_code;
  525. }
  526. uint16_t ble_advdata_search(uint8_t const * p_encoded_data,
  527. uint16_t data_len,
  528. uint16_t * p_offset,
  529. uint8_t ad_type)
  530. {
  531. if ((p_encoded_data == NULL) || (p_offset == NULL))
  532. {
  533. return 0;
  534. }
  535. uint16_t i = 0;
  536. while (((i < *p_offset) || (p_encoded_data[i + 1] != ad_type)) && (i < data_len))
  537. {
  538. // Jump to next data.
  539. i += (p_encoded_data[i] + 1);
  540. }
  541. if (i >= data_len)
  542. {
  543. return 0;
  544. }
  545. else
  546. {
  547. *p_offset = i + 2;
  548. return (p_encoded_data[i] - 1);
  549. }
  550. }
  551. uint8_t * ble_advdata_parse(uint8_t * p_encoded_data,
  552. uint16_t data_len,
  553. uint8_t ad_type)
  554. {
  555. uint16_t offset = 0;
  556. uint16_t len = ble_advdata_search(p_encoded_data, data_len, &offset, ad_type);
  557. if (len == 0)
  558. {
  559. return NULL;
  560. }
  561. else
  562. {
  563. return &p_encoded_data[offset];
  564. }
  565. }
  566. bool ble_advdata_name_find(uint8_t const * p_encoded_data,
  567. uint16_t data_len,
  568. char const * p_target_name)
  569. {
  570. uint16_t parsed_name_len;
  571. uint8_t const * p_parsed_name;
  572. uint16_t data_offset = 0;
  573. if (p_target_name == NULL)
  574. {
  575. return false;
  576. }
  577. parsed_name_len = ble_advdata_search(p_encoded_data,
  578. data_len,
  579. &data_offset,
  580. BLE_GAP_AD_TYPE_COMPLETE_LOCAL_NAME);
  581. p_parsed_name = &p_encoded_data[data_offset];
  582. if ( (data_offset != 0)
  583. && (parsed_name_len != 0)
  584. && (strlen(p_target_name) == parsed_name_len)
  585. && (memcmp(p_target_name, p_parsed_name, parsed_name_len) == 0))
  586. {
  587. return true;
  588. }
  589. return false;
  590. }
  591. bool ble_advdata_short_name_find(uint8_t const * p_encoded_data,
  592. uint16_t data_len,
  593. char const * p_target_name,
  594. uint8_t const short_name_min_len)
  595. {
  596. uint16_t parsed_name_len;
  597. uint8_t const * p_parsed_name;
  598. uint16_t data_offset = 0;
  599. if (p_target_name == NULL)
  600. {
  601. return false;
  602. }
  603. parsed_name_len = ble_advdata_search(p_encoded_data,
  604. data_len,
  605. &data_offset,
  606. BLE_GAP_AD_TYPE_SHORT_LOCAL_NAME);
  607. p_parsed_name = &p_encoded_data[data_offset];
  608. if ( (data_offset != 0)
  609. && (parsed_name_len != 0)
  610. && (parsed_name_len >= short_name_min_len)
  611. && (parsed_name_len < strlen(p_target_name))
  612. && (memcmp(p_target_name, p_parsed_name, parsed_name_len) == 0))
  613. {
  614. return true;
  615. }
  616. return false;
  617. }
  618. bool ble_advdata_uuid_find(uint8_t const * p_encoded_data,
  619. uint16_t data_len,
  620. ble_uuid_t const * p_target_uuid)
  621. {
  622. ret_code_t err_code;
  623. uint16_t data_offset = 0;
  624. uint8_t raw_uuid_len = UUID128_SIZE;
  625. uint8_t const * p_parsed_uuid;
  626. uint16_t parsed_uuid_len = data_len;
  627. uint8_t raw_uuid[UUID128_SIZE];
  628. uint8_t ad_types[N_AD_TYPES];
  629. err_code = sd_ble_uuid_encode(p_target_uuid, &raw_uuid_len, raw_uuid);
  630. if ((p_encoded_data == NULL) || (err_code != NRF_SUCCESS))
  631. {
  632. // Invalid p_encoded_data or p_target_uuid.
  633. return false;
  634. }
  635. switch (raw_uuid_len)
  636. {
  637. case UUID16_SIZE:
  638. ad_types[0] = BLE_GAP_AD_TYPE_16BIT_SERVICE_UUID_COMPLETE;
  639. ad_types[1] = BLE_GAP_AD_TYPE_16BIT_SERVICE_UUID_MORE_AVAILABLE;
  640. break;
  641. case UUID32_SIZE:
  642. // Not currently supported by sd_ble_uuid_encode().
  643. ad_types[0] = BLE_GAP_AD_TYPE_32BIT_SERVICE_UUID_COMPLETE;
  644. ad_types[1] = BLE_GAP_AD_TYPE_32BIT_SERVICE_UUID_MORE_AVAILABLE;
  645. break;
  646. case UUID128_SIZE:
  647. ad_types[0] = BLE_GAP_AD_TYPE_128BIT_SERVICE_UUID_COMPLETE;
  648. ad_types[1] = BLE_GAP_AD_TYPE_128BIT_SERVICE_UUID_MORE_AVAILABLE;
  649. break;
  650. default:
  651. return false;
  652. }
  653. for (uint8_t i = 0; (i < N_AD_TYPES) && (data_offset == 0); i++)
  654. {
  655. parsed_uuid_len = ble_advdata_search(p_encoded_data, data_len, &data_offset, ad_types[i]);
  656. }
  657. if (data_offset == 0)
  658. {
  659. // Could not find any relevant UUIDs in the encoded data.
  660. return false;
  661. }
  662. p_parsed_uuid = &p_encoded_data[data_offset];
  663. // Verify if any UUID matches the given UUID.
  664. for (uint16_t list_offset = 0; list_offset < parsed_uuid_len; list_offset += raw_uuid_len)
  665. {
  666. if (memcmp(&p_parsed_uuid[list_offset], raw_uuid, raw_uuid_len) == 0)
  667. {
  668. return true;
  669. }
  670. }
  671. // Could not find the UUID among the encoded data.
  672. return false;
  673. }
  674. bool ble_advdata_appearance_find(uint8_t const * p_encoded_data,
  675. uint16_t data_len,
  676. uint16_t const * p_target_appearance)
  677. {
  678. uint16_t data_offset = 0;
  679. uint8_t appearance_len;
  680. uint16_t decoded_appearance;
  681. appearance_len = ble_advdata_search(p_encoded_data, data_len, &data_offset, BLE_GAP_AD_TYPE_APPEARANCE);
  682. if ( (data_offset == 0)
  683. || (p_target_appearance == NULL)
  684. || (appearance_len == 0))
  685. {
  686. // Could not find any Appearance in the encoded data, or invalid p_target_appearance.
  687. return false;
  688. }
  689. decoded_appearance = uint16_decode(&p_encoded_data[data_offset]);
  690. if (decoded_appearance == *p_target_appearance)
  691. {
  692. return true;
  693. }
  694. // Could not find the appearance among the encoded data.
  695. return false;
  696. }