nrf_ble_scan.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304
  1. /**
  2. * Copyright (c) 2018 - 2020, 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 "sdk_common.h"
  41. #if NRF_MODULE_ENABLED(NRF_BLE_SCAN)
  42. #include "sdk_config.h"
  43. #include <stdlib.h>
  44. #include "nrf_ble_scan.h"
  45. #include <string.h>
  46. #include "app_error.h"
  47. #include "nrf_assert.h"
  48. #include "sdk_macros.h"
  49. #include "ble_advdata.h"
  50. #define NRF_LOG_MODULE_NAME ble_scan
  51. #include "nrf_log.h"
  52. NRF_LOG_MODULE_REGISTER();
  53. /**@brief Function for establishing the connection with a device.
  54. *
  55. * @details Connection is established if @ref NRF_BLE_SCAN_EVT_FILTER_MATCH
  56. * or @ref NRF_BLE_SCAN_EVT_WHITELIST_ADV_REPORT occurs and the module was
  57. * initialized in the automatic connection mode. This function can generate an event
  58. * to the main application when @ref sd_ble_gap_connect is used inside the function and it returns value
  59. * that is different than @ref NRF_SUCCESS.
  60. *
  61. * @param[in] p_scan_ctx Pointer to the Scanning Module instance.
  62. * @param[in] p_adv_report Advertising data.
  63. */
  64. static void nrf_ble_scan_connect_with_target(nrf_ble_scan_t const * const p_scan_ctx,
  65. ble_gap_evt_adv_report_t const * const p_adv_report)
  66. {
  67. ret_code_t err_code;
  68. scan_evt_t scan_evt;
  69. // For readability.
  70. ble_gap_addr_t const * p_addr = &p_adv_report->peer_addr;
  71. ble_gap_scan_params_t const * p_scan_params = &p_scan_ctx->scan_params;
  72. ble_gap_conn_params_t const * p_conn_params = &p_scan_ctx->conn_params;
  73. uint8_t con_cfg_tag = p_scan_ctx->conn_cfg_tag;
  74. // Return if the automatic connection is disabled.
  75. if (!p_scan_ctx->connect_if_match)
  76. {
  77. return;
  78. }
  79. // Stop scanning.
  80. nrf_ble_scan_stop();
  81. memset(&scan_evt, 0, sizeof(scan_evt));
  82. // Establish connection.
  83. err_code = sd_ble_gap_connect(p_addr,
  84. p_scan_params,
  85. p_conn_params,
  86. con_cfg_tag);
  87. NRF_LOG_DEBUG("Connecting");
  88. scan_evt.scan_evt_id = NRF_BLE_SCAN_EVT_CONNECTING_ERROR;
  89. scan_evt.params.connecting_err.err_code = err_code;
  90. NRF_LOG_DEBUG("Connection status: %d", err_code);
  91. // If an error occurred, send an event to the event handler.
  92. if ((err_code != NRF_SUCCESS) && (p_scan_ctx->evt_handler != NULL))
  93. {
  94. p_scan_ctx->evt_handler(&scan_evt);
  95. }
  96. }
  97. /**@brief Function for decoding the BLE address type.
  98. *
  99. * @param[in] p_addr The BLE address.
  100. *
  101. * @return Address type, or an error if the address type is incorrect, that is it does not match @ref BLE_GAP_ADDR_TYPES.
  102. *
  103. */
  104. static uint16_t nrf_ble_scan_address_type_decode(uint8_t const * p_addr)
  105. {
  106. uint8_t addr_type = p_addr[0];
  107. // See Bluetooth Core Specification Vol 6, Part B, section 1.3.
  108. addr_type = addr_type >> 6;
  109. addr_type &= 0x03;
  110. // Check address type.
  111. switch (addr_type)
  112. {
  113. case 0:
  114. {
  115. return BLE_GAP_ADDR_TYPE_RANDOM_PRIVATE_NON_RESOLVABLE;
  116. }
  117. case 1:
  118. {
  119. return BLE_GAP_ADDR_TYPE_PUBLIC;
  120. }
  121. case 2:
  122. {
  123. return BLE_GAP_ADDR_TYPE_RANDOM_PRIVATE_RESOLVABLE;
  124. }
  125. case 3:
  126. {
  127. return BLE_GAP_ADDR_TYPE_RANDOM_STATIC;
  128. }
  129. default:
  130. {
  131. return BLE_ERROR_GAP_INVALID_BLE_ADDR;
  132. }
  133. }
  134. }
  135. #if (NRF_BLE_SCAN_FILTER_ENABLE == 1)
  136. #if (NRF_BLE_SCAN_ADDRESS_CNT > 0)
  137. /**@brief Function for searching for the provided address in the advertisement packets.
  138. *
  139. * @details Use this function to parse the received advertising data for the provided address.
  140. *
  141. *
  142. * @param[in] p_adv_report Advertising data to parse.
  143. * @param[in] p_addr Address to search for. The address length must correspond to @ref BLE_GAP_ADDR_LEN.
  144. *
  145. * @return True if the provided address was found, false otherwise.
  146. */
  147. static bool find_peer_addr(ble_gap_evt_adv_report_t const * const p_adv_report,
  148. ble_gap_addr_t const * p_addr)
  149. {
  150. if (p_addr->addr_type == p_adv_report->peer_addr.addr_type)
  151. {
  152. // Compare addresses.
  153. if (memcmp(p_addr->addr,
  154. p_adv_report->peer_addr.addr,
  155. sizeof(p_adv_report->peer_addr.addr)) == 0)
  156. {
  157. return true;
  158. }
  159. }
  160. return false;
  161. }
  162. /** @brief Function for comparing the provided address with the addresses of the advertising devices.
  163. *
  164. * @param[in] p_adv_report Advertising data to parse.
  165. * @param[in] p_scan_ctx Pointer to the Scanning Module instance.
  166. *
  167. * @retval True when the address matches with the addresses of the advertising devices. False otherwise.
  168. */
  169. static bool adv_addr_compare(ble_gap_evt_adv_report_t const * const p_adv_report,
  170. nrf_ble_scan_t const * const p_scan_ctx)
  171. {
  172. ble_gap_addr_t const * p_addr = p_scan_ctx->scan_filters.addr_filter.target_addr;
  173. uint8_t counter = p_scan_ctx->scan_filters.addr_filter.addr_cnt;
  174. for (uint8_t index = 0; index < counter; index++)
  175. {
  176. // Search for address.
  177. if (find_peer_addr(p_adv_report, &p_addr[index]))
  178. {
  179. return true;
  180. }
  181. }
  182. return false;
  183. }
  184. /**@brief Function for adding target address to the scanning filter.
  185. *
  186. * @param[in] p_addr Target address in the format required by the SoftDevice. If you need to convert the address, use @ref nrf_ble_scan_copy_addr_to_sd_gap_addr. The address length must correspond to @ref BLE_GAP_ADDR_LEN.
  187. * @param[in,out] p_scan_ctx Pointer to the Scanning Module instance.
  188. *
  189. * @retval NRF_SUCCESS If the filter is added successfully or if you try to add a filter that was already added before.
  190. * @retval NRF_ERROR_NO_MEMORY If the number of available filters is exceeded.
  191. * @retval BLE_ERROR_GAP_INVALID_BLE_ADDR If the BLE address type is invalid.
  192. */
  193. static ret_code_t nrf_ble_scan_addr_filter_add(nrf_ble_scan_t * const p_scan_ctx,
  194. uint8_t const * p_addr)
  195. {
  196. ble_gap_addr_t * p_addr_filter = p_scan_ctx->scan_filters.addr_filter.target_addr;
  197. uint8_t * p_counter = &p_scan_ctx->scan_filters.addr_filter.addr_cnt;
  198. uint8_t index;
  199. uint16_t addr_type;
  200. uint8_t temp_addr[BLE_GAP_ADDR_LEN];
  201. // If no memory for filter.
  202. if (*p_counter >= NRF_BLE_SCAN_ADDRESS_CNT)
  203. {
  204. return NRF_ERROR_NO_MEM;
  205. }
  206. // Check for duplicated filter.
  207. for (index = 0; index < NRF_BLE_SCAN_ADDRESS_CNT; index++)
  208. {
  209. if (!memcmp(p_addr_filter[index].addr, p_addr, BLE_GAP_ADDR_LEN))
  210. {
  211. return NRF_SUCCESS;
  212. }
  213. }
  214. // Inverting the address.
  215. for (uint8_t i = 0; i < BLE_GAP_ADDR_LEN; i++)
  216. {
  217. temp_addr[i] = p_addr[(BLE_GAP_ADDR_LEN - 1) - i];
  218. }
  219. // Decode address type.
  220. addr_type = nrf_ble_scan_address_type_decode(temp_addr);
  221. if (addr_type == BLE_ERROR_GAP_INVALID_BLE_ADDR)
  222. {
  223. return BLE_ERROR_GAP_INVALID_BLE_ADDR;
  224. }
  225. // Add target address to filter.
  226. p_addr_filter[*p_counter].addr_type = (uint8_t)addr_type;
  227. for (uint8_t i = 0; i < BLE_GAP_ADDR_LEN; i++)
  228. {
  229. p_addr_filter[*p_counter].addr[i] = p_addr[i];
  230. }
  231. NRF_LOG_DEBUG("Filter set on address type %i, address 0x",
  232. p_addr_filter[*p_counter].addr_type);
  233. for (index = 0; index < BLE_GAP_ADDR_LEN; index++)
  234. {
  235. NRF_LOG_DEBUG("%x", p_addr_filter[*p_counter].addr[index]);
  236. }
  237. NRF_LOG_DEBUG("\n\r");
  238. // Increase the address filter counter.
  239. *p_counter += 1;
  240. return NRF_SUCCESS;
  241. }
  242. #endif // NRF_BLE_SCAN_ADDRESS_CNT
  243. #if (NRF_BLE_SCAN_NAME_CNT > 0)
  244. /** @brief Function for comparing the provided name with the advertised name.
  245. *
  246. * @param[in] p_adv_report Advertising data to parse.
  247. * @param[in] p_scan_ctx Pointer to the Scanning Module instance.
  248. *
  249. * @retval True when the names match. False otherwise.
  250. */
  251. static bool adv_name_compare(ble_gap_evt_adv_report_t const * p_adv_report,
  252. nrf_ble_scan_t const * const p_scan_ctx)
  253. {
  254. nrf_ble_scan_name_filter_t const * p_name_filter = &p_scan_ctx->scan_filters.name_filter;
  255. uint8_t counter =
  256. p_scan_ctx->scan_filters.name_filter.name_cnt;
  257. uint8_t index;
  258. uint16_t data_len;
  259. data_len = p_adv_report->data.len;
  260. // Compare the name found with the name filter.
  261. for (index = 0; index < counter; index++)
  262. {
  263. if (ble_advdata_name_find(p_adv_report->data.p_data,
  264. data_len,
  265. p_name_filter->target_name[index]))
  266. {
  267. return true;
  268. }
  269. }
  270. return false;
  271. }
  272. /**@brief Function for adding name of the peripheral to the scanning filter.
  273. *
  274. * @param[in] p_name Peripheral name.
  275. * @param[in,out] p_scan_ctx Pointer to the Scanning Module instance.
  276. *
  277. * @retval NRF_SUCCESS If the filter is added successfully or if you try to add a filter that was already added before.
  278. * @retval NRF_ERROR_NULL If a NULL pointer is passed as input.
  279. * @retval NRF_ERROR_DATA_SIZE If the name filter length is too long. The maximum filter name length corresponds to @ref NRF_BLE_SCAN_NAME_MAX_LEN.
  280. * @retval NRF_ERROR_NO_MEMORY If the number of available filters is exceeded.
  281. */
  282. static ret_code_t nrf_ble_scan_name_filter_add(nrf_ble_scan_t * const p_scan_ctx,
  283. char const * p_name)
  284. {
  285. uint8_t index;
  286. uint8_t * counter = &p_scan_ctx->scan_filters.name_filter.name_cnt;
  287. uint8_t name_len = strlen(p_name);
  288. // Check the name length.
  289. if ((name_len == 0) || (name_len > NRF_BLE_SCAN_NAME_MAX_LEN))
  290. {
  291. return NRF_ERROR_DATA_SIZE;
  292. }
  293. // If no memory for filter.
  294. if (*counter >= NRF_BLE_SCAN_NAME_CNT)
  295. {
  296. return NRF_ERROR_NO_MEM;
  297. }
  298. // Check for duplicated filter.
  299. for (index = 0; index < NRF_BLE_SCAN_NAME_CNT; index++)
  300. {
  301. if (!strcmp(p_scan_ctx->scan_filters.name_filter.target_name[index], p_name))
  302. {
  303. return NRF_SUCCESS;
  304. }
  305. }
  306. // Add name to filter.
  307. memcpy(p_scan_ctx->scan_filters.name_filter.target_name[(*counter)++],
  308. p_name,
  309. strlen(p_name));
  310. NRF_LOG_DEBUG("Adding filter on %s name", p_name);
  311. return NRF_SUCCESS;
  312. }
  313. #endif // NRF_BLE_SCAN_NAME_CNT
  314. #if (NRF_BLE_SCAN_SHORT_NAME_CNT > 0)
  315. /** @brief Function for comparing the provided short name with the advertised short name.
  316. *
  317. * @param[in] p_adv_report Advertising data to parse.
  318. * @param[in] p_scan_ctx Pointer to the Scanning Module instance.
  319. *
  320. * @retval True when the names match. False otherwise.
  321. */
  322. static bool adv_short_name_compare(ble_gap_evt_adv_report_t const * const p_adv_report,
  323. nrf_ble_scan_t const * const p_scan_ctx)
  324. {
  325. nrf_ble_scan_short_name_filter_t const * p_name_filter =
  326. &p_scan_ctx->scan_filters.short_name_filter;
  327. uint8_t counter = p_scan_ctx->scan_filters.short_name_filter.name_cnt;
  328. uint8_t index;
  329. uint16_t data_len;
  330. data_len = p_adv_report->data.len;
  331. // Compare the name found with the name filters.
  332. for (index = 0; index < counter; index++)
  333. {
  334. if (ble_advdata_short_name_find(p_adv_report->data.p_data,
  335. data_len,
  336. p_name_filter->short_name[index].short_target_name,
  337. p_name_filter->short_name[index].short_name_min_len))
  338. {
  339. return true;
  340. }
  341. }
  342. return false;
  343. }
  344. /**@brief Function for adding the short name of the peripheral to the scanning filter.
  345. *
  346. * @param[in] p_short_name Short name of the peripheral.
  347. * @param[in,out] p_scan_ctx Pointer to the Scanning Module instance.
  348. *
  349. * @retval NRF_SUCCESS If the filter is added successfully or if you try to add a filter that was already added before.
  350. * @retval NRF_ERROR_NULL If a NULL pointer is passed as input.
  351. * @retval NRF_ERROR_DATA_SIZE If the name filter length is too long. The maximum filter name length corresponds to @ref NRF_BLE_SCAN_SHORT_NAME_MAX_LEN.
  352. * @retval NRF_ERROR_NO_MEMORY If the number of available filters is exceeded.
  353. */
  354. static ret_code_t nrf_ble_scan_short_name_filter_add(nrf_ble_scan_t * const p_scan_ctx,
  355. nrf_ble_scan_short_name_t const * p_short_name)
  356. {
  357. uint8_t index;
  358. uint8_t * p_counter =
  359. &p_scan_ctx->scan_filters.short_name_filter.name_cnt;
  360. nrf_ble_scan_short_name_filter_t * p_short_name_filter =
  361. &p_scan_ctx->scan_filters.short_name_filter;
  362. uint8_t name_len = strlen(p_short_name->p_short_name);
  363. // Check the name length.
  364. if ((name_len == 0) || (name_len > NRF_BLE_SCAN_SHORT_NAME_MAX_LEN))
  365. {
  366. return NRF_ERROR_DATA_SIZE;
  367. }
  368. // If no memory for filter.
  369. if (*p_counter >= NRF_BLE_SCAN_SHORT_NAME_CNT)
  370. {
  371. return NRF_ERROR_NO_MEM;
  372. }
  373. // Check for duplicated filter.
  374. for (index = 0; index < NRF_BLE_SCAN_SHORT_NAME_CNT; index++)
  375. {
  376. if (!strcmp(p_short_name_filter->short_name[index].short_target_name,
  377. p_short_name->p_short_name))
  378. {
  379. return NRF_SUCCESS;
  380. }
  381. }
  382. // Add name to the filter.
  383. p_short_name_filter->short_name[(*p_counter)].short_name_min_len =
  384. p_short_name->short_name_min_len;
  385. memcpy(p_short_name_filter->short_name[(*p_counter)++].short_target_name,
  386. p_short_name->p_short_name,
  387. strlen(p_short_name->p_short_name));
  388. NRF_LOG_DEBUG("Adding filter on %s name", p_short_name->p_short_name);
  389. return NRF_SUCCESS;
  390. }
  391. #endif
  392. #if (NRF_BLE_SCAN_UUID_CNT > 0)
  393. /**@brief Function for comparing the provided UUID with the UUID in the advertisement packets.
  394. *
  395. * @param[in] p_adv_report Advertising data to parse.
  396. * @param[in] p_scan_ctx Pointer to the Scanning Module instance.
  397. *
  398. * @return True if the UUIDs match. False otherwise.
  399. */
  400. static bool adv_uuid_compare(ble_gap_evt_adv_report_t const * const p_adv_report,
  401. nrf_ble_scan_t const * const p_scan_ctx)
  402. {
  403. nrf_ble_scan_uuid_filter_t const * p_uuid_filter = &p_scan_ctx->scan_filters.uuid_filter;
  404. bool const all_filters_mode = p_scan_ctx->scan_filters.all_filters_mode;
  405. uint8_t const counter =
  406. p_scan_ctx->scan_filters.uuid_filter.uuid_cnt;
  407. uint8_t index;
  408. uint16_t data_len;
  409. uint8_t uuid_match_cnt = 0;
  410. data_len = p_adv_report->data.len;
  411. for (index = 0; index < counter; index++)
  412. {
  413. if (ble_advdata_uuid_find(p_adv_report->data.p_data,
  414. data_len,
  415. &p_uuid_filter->uuid[index]))
  416. {
  417. uuid_match_cnt++;
  418. // In the normal filter mode, only one UUID is needed to match.
  419. if (!all_filters_mode)
  420. {
  421. break;
  422. }
  423. }
  424. else if (all_filters_mode)
  425. {
  426. break;
  427. }
  428. else
  429. {
  430. // Do nothing.
  431. }
  432. }
  433. // In the multifilter mode, all UUIDs must be found in the advertisement packets.
  434. if ((all_filters_mode && (uuid_match_cnt == counter)) ||
  435. ((!all_filters_mode) && (uuid_match_cnt > 0)))
  436. {
  437. return true;
  438. }
  439. return false;
  440. }
  441. /**@brief Function for adding UUID to the scanning filter.
  442. *
  443. * @param[in] uuid UUID, 16-bit size.
  444. * @param[in,out] p_scan_ctx Pointer to the Scanning Module instance.
  445. *
  446. * @retval NRF_SUCCESS If the scanning started. Otherwise, an error code is returned, also if you tried to add a filter that was already added before.
  447. * @retval NRF_ERROR_NO_MEMORY If the number of available filters is exceeded.
  448. */
  449. static ret_code_t nrf_ble_scan_uuid_filter_add(nrf_ble_scan_t * const p_scan_ctx,
  450. ble_uuid_t const * p_uuid)
  451. {
  452. ble_uuid_t * p_uuid_filter = p_scan_ctx->scan_filters.uuid_filter.uuid;
  453. uint8_t * p_counter = &p_scan_ctx->scan_filters.uuid_filter.uuid_cnt;
  454. uint8_t index;
  455. // If no memory.
  456. if (*p_counter >= NRF_BLE_SCAN_UUID_CNT)
  457. {
  458. return NRF_ERROR_NO_MEM;
  459. }
  460. // Check for duplicated filter.
  461. for (index = 0; index < NRF_BLE_SCAN_UUID_CNT; index++)
  462. {
  463. if (p_uuid_filter[index].uuid == p_uuid->uuid)
  464. {
  465. return NRF_SUCCESS;
  466. }
  467. }
  468. // Add UUID to the filter.
  469. p_uuid_filter[(*p_counter)++] = *p_uuid;
  470. NRF_LOG_DEBUG("Added filter on UUID %x", p_uuid->uuid);
  471. return NRF_SUCCESS;
  472. }
  473. #endif // NRF_BLE_SCAN_UUID_CNT
  474. #if (NRF_BLE_SCAN_APPEARANCE_CNT)
  475. /**@brief Function for comparing the provided appearance with the appearance in the advertisement packets.
  476. *
  477. * @param[in] p_adv_report Advertising data to parse.
  478. * @param[in,out] p_scan_ctx Pointer to the Scanning Module instance.
  479. *
  480. * @return True if the appearances match. False otherwise.
  481. */
  482. static bool adv_appearance_compare(ble_gap_evt_adv_report_t const * const p_adv_report,
  483. nrf_ble_scan_t const * const p_scan_ctx)
  484. {
  485. nrf_ble_scan_appearance_filter_t const * p_appearance_filter =
  486. &p_scan_ctx->scan_filters.appearance_filter;
  487. uint8_t const counter =
  488. p_scan_ctx->scan_filters.appearance_filter.appearance_cnt;
  489. uint8_t index;
  490. uint16_t data_len;
  491. data_len = p_adv_report->data.len;
  492. // Verify if the advertised appearance matches the provided appearance.
  493. for (index = 0; index < counter; index++)
  494. {
  495. if (ble_advdata_appearance_find(p_adv_report->data.p_data,
  496. data_len,
  497. &p_appearance_filter->appearance[index]))
  498. {
  499. return true;
  500. }
  501. }
  502. return false;
  503. }
  504. /**@brief Function for adding appearance to the scanning filter.
  505. *
  506. * @param[in] appearance Appearance to be added.
  507. * @param[in,out] p_scan_ctx Pointer to the Scanning Module instance.
  508. *
  509. * @retval NRF_SUCCESS If the filter is added successfully or if you try to add a filter that was already added before.
  510. * @retval NRF_ERROR_NULL If a NULL pointer is passed as input.
  511. * @retval NRF_ERROR_NO_MEMORY If the number of available filters is exceeded.
  512. */
  513. static ret_code_t nrf_ble_scan_appearance_filter_add(nrf_ble_scan_t * const p_scan_ctx,
  514. uint16_t appearance)
  515. {
  516. uint16_t * p_appearance_filter = p_scan_ctx->scan_filters.appearance_filter.appearance;
  517. uint8_t * p_counter = &p_scan_ctx->scan_filters.appearance_filter.appearance_cnt;
  518. uint8_t index;
  519. // If no memory.
  520. if (*p_counter >= NRF_BLE_SCAN_APPEARANCE_CNT)
  521. {
  522. return NRF_ERROR_NO_MEM;
  523. }
  524. // Check for duplicated filter.
  525. for ( index = 0; index < NRF_BLE_SCAN_APPEARANCE_CNT; index++)
  526. {
  527. if (p_appearance_filter[index] == appearance)
  528. {
  529. return NRF_SUCCESS;
  530. }
  531. }
  532. // Add appearance to the filter.
  533. p_appearance_filter[(*p_counter)++] = appearance;
  534. NRF_LOG_DEBUG("Added filter on appearance %x", appearance);
  535. return NRF_SUCCESS;
  536. }
  537. #endif // NRF_BLE_SCAN_APPEARANCE_CNT
  538. ret_code_t nrf_ble_scan_filter_set(nrf_ble_scan_t * const p_scan_ctx,
  539. nrf_ble_scan_filter_type_t type,
  540. void const * p_data)
  541. {
  542. VERIFY_PARAM_NOT_NULL(p_scan_ctx);
  543. VERIFY_PARAM_NOT_NULL(p_data);
  544. switch (type)
  545. {
  546. #if (NRF_BLE_SCAN_NAME_CNT > 0)
  547. case SCAN_NAME_FILTER:
  548. {
  549. char * p_name = (char *)p_data;
  550. return nrf_ble_scan_name_filter_add(p_scan_ctx, p_name);
  551. }
  552. #endif
  553. #if (NRF_BLE_SCAN_SHORT_NAME_CNT > 0)
  554. case SCAN_SHORT_NAME_FILTER:
  555. {
  556. nrf_ble_scan_short_name_t * p_short_name = (nrf_ble_scan_short_name_t *)p_data;
  557. return nrf_ble_scan_short_name_filter_add(p_scan_ctx, p_short_name);
  558. }
  559. #endif
  560. #if (NRF_BLE_SCAN_ADDRESS_CNT > 0)
  561. case SCAN_ADDR_FILTER:
  562. {
  563. uint8_t * p_addr = (uint8_t *)p_data;
  564. return nrf_ble_scan_addr_filter_add(p_scan_ctx, p_addr);
  565. }
  566. #endif
  567. #if (NRF_BLE_SCAN_UUID_CNT > 0)
  568. case SCAN_UUID_FILTER:
  569. {
  570. ble_uuid_t * p_uuid = (ble_uuid_t *)p_data;
  571. return nrf_ble_scan_uuid_filter_add(p_scan_ctx, p_uuid);
  572. }
  573. #endif
  574. #if (NRF_BLE_SCAN_APPEARANCE_CNT > 0)
  575. case SCAN_APPEARANCE_FILTER:
  576. {
  577. uint16_t appearance = *((uint16_t *)p_data);
  578. return nrf_ble_scan_appearance_filter_add(p_scan_ctx, appearance);
  579. }
  580. #endif
  581. default:
  582. return NRF_ERROR_INVALID_PARAM;
  583. }
  584. }
  585. ret_code_t nrf_ble_scan_all_filter_remove(nrf_ble_scan_t * const p_scan_ctx)
  586. {
  587. #if (NRF_BLE_SCAN_NAME_CNT > 0)
  588. nrf_ble_scan_name_filter_t * p_name_filter = &p_scan_ctx->scan_filters.name_filter;
  589. memset(p_name_filter->target_name, 0, sizeof(p_name_filter->target_name));
  590. p_name_filter->name_cnt = 0;
  591. #endif
  592. #if (NRF_BLE_SCAN_SHORT_NAME_CNT > 0)
  593. nrf_ble_scan_short_name_filter_t * p_short_name_filter =
  594. &p_scan_ctx->scan_filters.short_name_filter;
  595. memset(p_short_name_filter->short_name, 0, sizeof(p_short_name_filter->short_name));
  596. p_short_name_filter->name_cnt = 0;
  597. #endif
  598. #if (NRF_BLE_SCAN_ADDRESS_CNT > 0)
  599. nrf_ble_scan_addr_filter_t * p_addr_filter = &p_scan_ctx->scan_filters.addr_filter;
  600. memset(p_addr_filter->target_addr, 0, sizeof(p_addr_filter->target_addr));
  601. p_addr_filter->addr_cnt = 0;
  602. #endif
  603. #if (NRF_BLE_SCAN_UUID_CNT > 0)
  604. nrf_ble_scan_uuid_filter_t * p_uuid_filter = &p_scan_ctx->scan_filters.uuid_filter;
  605. memset(p_uuid_filter->uuid, 0, sizeof(p_uuid_filter->uuid));
  606. p_uuid_filter->uuid_cnt = 0;
  607. #endif
  608. #if (NRF_BLE_SCAN_APPEARANCE_CNT > 0)
  609. nrf_ble_scan_appearance_filter_t * p_appearance_filter =
  610. &p_scan_ctx->scan_filters.appearance_filter;
  611. memset(p_appearance_filter->appearance, 0, sizeof(p_appearance_filter->appearance));
  612. p_appearance_filter->appearance_cnt = 0;
  613. #endif
  614. return NRF_SUCCESS;
  615. }
  616. ret_code_t nrf_ble_scan_filters_enable(nrf_ble_scan_t * const p_scan_ctx,
  617. uint8_t mode,
  618. bool match_all)
  619. {
  620. VERIFY_PARAM_NOT_NULL(p_scan_ctx);
  621. // Check if the mode is correct.
  622. if ((!(mode & NRF_BLE_SCAN_ADDR_FILTER)) &&
  623. (!(mode & NRF_BLE_SCAN_NAME_FILTER)) &&
  624. (!(mode & NRF_BLE_SCAN_UUID_FILTER)) &&
  625. (!(mode & NRF_BLE_SCAN_SHORT_NAME_FILTER)) &&
  626. (!(mode & NRF_BLE_SCAN_APPEARANCE_FILTER)))
  627. {
  628. return NRF_ERROR_INVALID_PARAM;
  629. }
  630. ret_code_t err_code;
  631. // Disable filters.
  632. err_code = nrf_ble_scan_filters_disable(p_scan_ctx);
  633. ASSERT(err_code == NRF_SUCCESS);
  634. nrf_ble_scan_filters_t * p_filters = &p_scan_ctx->scan_filters;
  635. // Turn on the filters of your choice.
  636. #if (NRF_BLE_SCAN_ADDRESS_CNT > 0)
  637. if (mode & NRF_BLE_SCAN_ADDR_FILTER)
  638. {
  639. p_filters->addr_filter.addr_filter_enabled = true;
  640. }
  641. #endif
  642. #if (NRF_BLE_SCAN_NAME_CNT > 0)
  643. if (mode & NRF_BLE_SCAN_NAME_FILTER)
  644. {
  645. p_filters->name_filter.name_filter_enabled = true;
  646. }
  647. #endif
  648. #if (NRF_BLE_SCAN_SHORT_NAME_CNT > 0)
  649. if (mode & NRF_BLE_SCAN_SHORT_NAME_FILTER)
  650. {
  651. p_filters->short_name_filter.short_name_filter_enabled = true;
  652. }
  653. #endif
  654. #if (NRF_BLE_SCAN_UUID_CNT > 0)
  655. if (mode & NRF_BLE_SCAN_UUID_FILTER)
  656. {
  657. p_filters->uuid_filter.uuid_filter_enabled = true;
  658. }
  659. #endif
  660. #if (NRF_BLE_SCAN_APPEARANCE_CNT > 0)
  661. if (mode & NRF_BLE_SCAN_APPEARANCE_FILTER)
  662. {
  663. p_filters->appearance_filter.appearance_filter_enabled = true;
  664. }
  665. #endif
  666. // Select the filter mode.
  667. p_filters->all_filters_mode = match_all;
  668. return NRF_SUCCESS;
  669. }
  670. ret_code_t nrf_ble_scan_filters_disable(nrf_ble_scan_t * const p_scan_ctx)
  671. {
  672. VERIFY_PARAM_NOT_NULL(p_scan_ctx);
  673. // Disable all filters.
  674. #if (NRF_BLE_SCAN_NAME_CNT > 0)
  675. bool * p_name_filter_enabled = &p_scan_ctx->scan_filters.name_filter.name_filter_enabled;
  676. *p_name_filter_enabled = false;
  677. #endif
  678. #if (NRF_BLE_SCAN_ADDRESS_CNT > 0)
  679. bool * p_addr_filter_enabled = &p_scan_ctx->scan_filters.addr_filter.addr_filter_enabled;
  680. *p_addr_filter_enabled = false;
  681. #endif
  682. #if (NRF_BLE_SCAN_UUID_CNT > 0)
  683. bool * p_uuid_filter_enabled = &p_scan_ctx->scan_filters.uuid_filter.uuid_filter_enabled;
  684. *p_uuid_filter_enabled = false;
  685. #endif
  686. #if (NRF_BLE_SCAN_APPEARANCE_CNT > 0)
  687. bool * p_appearance_filter_enabled =
  688. &p_scan_ctx->scan_filters.appearance_filter.appearance_filter_enabled;
  689. *p_appearance_filter_enabled = false;
  690. #endif
  691. return NRF_SUCCESS;
  692. }
  693. ret_code_t nrf_ble_scan_filter_get(nrf_ble_scan_t * const p_scan_ctx,
  694. nrf_ble_scan_filters_t * p_status)
  695. {
  696. VERIFY_PARAM_NOT_NULL(p_scan_ctx);
  697. VERIFY_PARAM_NOT_NULL(p_status);
  698. *p_status = p_scan_ctx->scan_filters;
  699. return NRF_SUCCESS;
  700. }
  701. #endif // NRF_BLE_SCAN_FILTER_ENABLE
  702. /**@brief Function for calling the BLE_GAP_EVT_ADV_REPORT event to check whether the received
  703. * scanning data matches the scan configuration.
  704. *
  705. * @param[in] p_scan_ctx Pointer to the Scanning Module instance.
  706. * @param[in] p_adv_report Advertising report.
  707. */
  708. static void nrf_ble_scan_on_adv_report(nrf_ble_scan_t const * const p_scan_ctx,
  709. ble_gap_evt_adv_report_t const * const p_adv_report)
  710. {
  711. scan_evt_t scan_evt;
  712. #if (NRF_BLE_SCAN_FILTER_ENABLE == 1)
  713. uint8_t filter_cnt = 0;
  714. uint8_t filter_match_cnt = 0;
  715. #endif
  716. memset(&scan_evt, 0, sizeof(scan_evt));
  717. scan_evt.p_scan_params = &p_scan_ctx->scan_params;
  718. // If the whitelist is used, do not check the filters and return.
  719. if (is_whitelist_used(p_scan_ctx))
  720. {
  721. scan_evt.scan_evt_id = NRF_BLE_SCAN_EVT_WHITELIST_ADV_REPORT;
  722. scan_evt.params.p_not_found = p_adv_report;
  723. p_scan_ctx->evt_handler(&scan_evt);
  724. UNUSED_RETURN_VALUE(sd_ble_gap_scan_start(NULL, &p_scan_ctx->scan_buffer));
  725. nrf_ble_scan_connect_with_target(p_scan_ctx, p_adv_report);
  726. return;
  727. }
  728. #if (NRF_BLE_SCAN_FILTER_ENABLE == 1)
  729. bool const all_filter_mode = p_scan_ctx->scan_filters.all_filters_mode;
  730. bool is_filter_matched = false;
  731. #if (NRF_BLE_SCAN_ADDRESS_CNT > 0)
  732. bool const addr_filter_enabled = p_scan_ctx->scan_filters.addr_filter.addr_filter_enabled;
  733. #endif
  734. #if (NRF_BLE_SCAN_NAME_CNT > 0)
  735. bool const name_filter_enabled = p_scan_ctx->scan_filters.name_filter.name_filter_enabled;
  736. #endif
  737. #if (NRF_BLE_SCAN_SHORT_NAME_CNT > 0)
  738. bool const short_name_filter_enabled =
  739. p_scan_ctx->scan_filters.short_name_filter.short_name_filter_enabled;
  740. #endif
  741. #if (NRF_BLE_SCAN_UUID_CNT > 0)
  742. bool const uuid_filter_enabled = p_scan_ctx->scan_filters.uuid_filter.uuid_filter_enabled;
  743. #endif
  744. #if (NRF_BLE_SCAN_APPEARANCE_CNT > 0)
  745. bool const appearance_filter_enabled =
  746. p_scan_ctx->scan_filters.appearance_filter.appearance_filter_enabled;
  747. #endif
  748. #if (NRF_BLE_SCAN_ADDRESS_CNT > 0)
  749. // Check the address filter.
  750. if (addr_filter_enabled)
  751. {
  752. // Number of active filters.
  753. filter_cnt++;
  754. if (adv_addr_compare(p_adv_report, p_scan_ctx))
  755. {
  756. // Number of filters matched.
  757. filter_match_cnt++;
  758. // Information about the filters matched.
  759. scan_evt.params.filter_match.filter_match.address_filter_match = true;
  760. is_filter_matched = true;
  761. }
  762. }
  763. #endif
  764. #if (NRF_BLE_SCAN_NAME_CNT > 0)
  765. // Check the name filter.
  766. if (name_filter_enabled)
  767. {
  768. filter_cnt++;
  769. if (adv_name_compare(p_adv_report, p_scan_ctx))
  770. {
  771. filter_match_cnt++;
  772. // Information about the filters matched.
  773. scan_evt.params.filter_match.filter_match.name_filter_match = true;
  774. is_filter_matched = true;
  775. }
  776. }
  777. #endif
  778. #if (NRF_BLE_SCAN_SHORT_NAME_CNT > 0)
  779. if (short_name_filter_enabled)
  780. {
  781. filter_cnt++;
  782. if (adv_short_name_compare(p_adv_report, p_scan_ctx))
  783. {
  784. filter_match_cnt++;
  785. // Information about the filters matched.
  786. scan_evt.params.filter_match.filter_match.short_name_filter_match = true;
  787. is_filter_matched = true;
  788. }
  789. }
  790. #endif
  791. #if (NRF_BLE_SCAN_UUID_CNT > 0)
  792. // Check the UUID filter.
  793. if (uuid_filter_enabled)
  794. {
  795. filter_cnt++;
  796. if (adv_uuid_compare(p_adv_report, p_scan_ctx))
  797. {
  798. filter_match_cnt++;
  799. // Information about the filters matched.
  800. scan_evt.params.filter_match.filter_match.uuid_filter_match = true;
  801. is_filter_matched = true;
  802. }
  803. }
  804. #endif
  805. #if (NRF_BLE_SCAN_APPEARANCE_CNT > 0)
  806. // Check the appearance filter.
  807. if (appearance_filter_enabled)
  808. {
  809. filter_cnt++;
  810. if (adv_appearance_compare(p_adv_report, p_scan_ctx))
  811. {
  812. filter_match_cnt++;
  813. // Information about the filters matched.
  814. scan_evt.params.filter_match.filter_match.appearance_filter_match = true;
  815. is_filter_matched = true;
  816. }
  817. }
  818. scan_evt.scan_evt_id = NRF_BLE_SCAN_EVT_NOT_FOUND;
  819. #endif
  820. scan_evt.params.filter_match.p_adv_report = p_adv_report;
  821. // In the multifilter mode, the number of the active filters must equal the number of the filters matched to generate the notification.
  822. if (all_filter_mode && (filter_match_cnt == filter_cnt))
  823. {
  824. scan_evt.scan_evt_id = NRF_BLE_SCAN_EVT_FILTER_MATCH;
  825. nrf_ble_scan_connect_with_target(p_scan_ctx, p_adv_report);
  826. }
  827. // In the normal filter mode, only one filter match is needed to generate the notification to the main application.
  828. else if ((!all_filter_mode) && is_filter_matched)
  829. {
  830. scan_evt.scan_evt_id = NRF_BLE_SCAN_EVT_FILTER_MATCH;
  831. nrf_ble_scan_connect_with_target(p_scan_ctx, p_adv_report);
  832. }
  833. else
  834. {
  835. scan_evt.scan_evt_id = NRF_BLE_SCAN_EVT_NOT_FOUND;
  836. scan_evt.params.p_not_found = p_adv_report;
  837. }
  838. // If the event handler is not NULL, notify the main application.
  839. if (p_scan_ctx->evt_handler != NULL)
  840. {
  841. p_scan_ctx->evt_handler(&scan_evt);
  842. }
  843. #endif // NRF_BLE_SCAN_FILTER_ENABLE
  844. // Resume the scanning.
  845. UNUSED_RETURN_VALUE(sd_ble_gap_scan_start(NULL, &p_scan_ctx->scan_buffer));
  846. }
  847. /**@brief Function for checking whether the whitelist is used.
  848. *
  849. * @param[in] p_scan_ctx Scanning Module instance.
  850. */
  851. bool is_whitelist_used(nrf_ble_scan_t const * const p_scan_ctx)
  852. {
  853. if (p_scan_ctx->scan_params.filter_policy == BLE_GAP_SCAN_FP_WHITELIST ||
  854. p_scan_ctx->scan_params.filter_policy == BLE_GAP_SCAN_FP_WHITELIST_NOT_RESOLVED_DIRECTED)
  855. {
  856. return true;
  857. }
  858. return false;
  859. }
  860. /**@brief Function for restoring the default scanning parameters.
  861. *
  862. * @param[out] p_scan_ctx Pointer to the Scanning Module instance.
  863. */
  864. static void nrf_ble_scan_default_param_set(nrf_ble_scan_t * const p_scan_ctx)
  865. {
  866. // Set the default parameters.
  867. p_scan_ctx->scan_params.active = 1;
  868. #if (NRF_SD_BLE_API_VERSION > 7)
  869. p_scan_ctx->scan_params.interval_us = NRF_BLE_SCAN_SCAN_INTERVAL * UNIT_0_625_MS;
  870. p_scan_ctx->scan_params.window_us = NRF_BLE_SCAN_SCAN_WINDOW * UNIT_0_625_MS;
  871. #else
  872. p_scan_ctx->scan_params.interval = NRF_BLE_SCAN_SCAN_INTERVAL;
  873. p_scan_ctx->scan_params.window = NRF_BLE_SCAN_SCAN_WINDOW;
  874. #endif // #if (NRF_SD_BLE_API_VERSION > 7)
  875. p_scan_ctx->scan_params.timeout = NRF_BLE_SCAN_SCAN_DURATION;
  876. p_scan_ctx->scan_params.filter_policy = BLE_GAP_SCAN_FP_ACCEPT_ALL;
  877. p_scan_ctx->scan_params.scan_phys = BLE_GAP_PHY_1MBPS;
  878. }
  879. /**@brief Function for setting the default connection parameters.
  880. *
  881. * @param[out] p_scan_ctx Pointer to the Scanning Module instance.
  882. */
  883. static void nrf_ble_scan_default_conn_param_set(nrf_ble_scan_t * const p_scan_ctx)
  884. {
  885. p_scan_ctx->conn_params.conn_sup_timeout =
  886. (uint16_t)MSEC_TO_UNITS(NRF_BLE_SCAN_SUPERVISION_TIMEOUT, UNIT_10_MS);
  887. p_scan_ctx->conn_params.min_conn_interval =
  888. (uint16_t)MSEC_TO_UNITS(NRF_BLE_SCAN_MIN_CONNECTION_INTERVAL, UNIT_1_25_MS);
  889. p_scan_ctx->conn_params.max_conn_interval =
  890. (uint16_t)MSEC_TO_UNITS(NRF_BLE_SCAN_MAX_CONNECTION_INTERVAL, UNIT_1_25_MS);
  891. p_scan_ctx->conn_params.slave_latency =
  892. (uint16_t)NRF_BLE_SCAN_SLAVE_LATENCY;
  893. }
  894. /**@brief Function for calling the BLE_GAP_EVT_TIMEOUT event.
  895. *
  896. * @param[in] p_scan_ctx Pointer to the Scanning Module instance.
  897. * @param[in] p_gap GAP event structure.
  898. */
  899. static void nrf_ble_scan_on_timeout(nrf_ble_scan_t const * const p_scan_ctx,
  900. ble_gap_evt_t const * const p_gap)
  901. {
  902. ble_gap_evt_timeout_t const * p_timeout = &p_gap->params.timeout;
  903. scan_evt_t scan_evt;
  904. memset(&scan_evt, 0, sizeof(scan_evt));
  905. if (p_timeout->src == BLE_GAP_TIMEOUT_SRC_SCAN)
  906. {
  907. NRF_LOG_DEBUG("BLE_GAP_SCAN_TIMEOUT");
  908. if (p_scan_ctx->evt_handler != NULL)
  909. {
  910. scan_evt.scan_evt_id = NRF_BLE_SCAN_EVT_SCAN_TIMEOUT;
  911. scan_evt.p_scan_params = &p_scan_ctx->scan_params;
  912. scan_evt.params.timeout.src = p_timeout->src;
  913. p_scan_ctx->evt_handler(&scan_evt);
  914. }
  915. }
  916. }
  917. /**@brief Function for stopping the scanning.
  918. */
  919. void nrf_ble_scan_stop(void)
  920. {
  921. // It is ok to ignore the function return value here, because this function can return NRF_SUCCESS or
  922. // NRF_ERROR_INVALID_STATE, when app is not in the scanning state.
  923. UNUSED_RETURN_VALUE(sd_ble_gap_scan_stop());
  924. }
  925. ret_code_t nrf_ble_scan_init(nrf_ble_scan_t * const p_scan_ctx,
  926. nrf_ble_scan_init_t const * const p_init,
  927. nrf_ble_scan_evt_handler_t evt_handler)
  928. {
  929. VERIFY_PARAM_NOT_NULL(p_scan_ctx);
  930. p_scan_ctx->evt_handler = evt_handler;
  931. #if (NRF_BLE_SCAN_FILTER_ENABLE == 1)
  932. // Disable all scanning filters.
  933. memset(&p_scan_ctx->scan_filters, 0, sizeof(p_scan_ctx->scan_filters));
  934. #endif
  935. // If the pointer to the initialization structure exist, use it to scan the configuration.
  936. if (p_init != NULL)
  937. {
  938. p_scan_ctx->connect_if_match = p_init->connect_if_match;
  939. p_scan_ctx->conn_cfg_tag = p_init->conn_cfg_tag;
  940. if (p_init->p_scan_param != NULL)
  941. {
  942. p_scan_ctx->scan_params = *p_init->p_scan_param;
  943. }
  944. else
  945. {
  946. // Use the default static configuration.
  947. nrf_ble_scan_default_param_set(p_scan_ctx);
  948. }
  949. if (p_init->p_conn_param != NULL)
  950. {
  951. p_scan_ctx->conn_params = *p_init->p_conn_param;
  952. }
  953. else
  954. {
  955. // Use the default static configuration.
  956. nrf_ble_scan_default_conn_param_set(p_scan_ctx);
  957. }
  958. }
  959. // If pointer is NULL, use the static default configuration.
  960. else
  961. {
  962. nrf_ble_scan_default_param_set(p_scan_ctx);
  963. nrf_ble_scan_default_conn_param_set(p_scan_ctx);
  964. p_scan_ctx->connect_if_match = false;
  965. }
  966. // Assign a buffer where the advertising reports are to be stored by the SoftDevice.
  967. p_scan_ctx->scan_buffer.p_data = p_scan_ctx->scan_buffer_data;
  968. p_scan_ctx->scan_buffer.len = NRF_BLE_SCAN_BUFFER;
  969. return NRF_SUCCESS;
  970. }
  971. ret_code_t nrf_ble_scan_start(nrf_ble_scan_t const * const p_scan_ctx)
  972. {
  973. VERIFY_PARAM_NOT_NULL(p_scan_ctx);
  974. ret_code_t err_code;
  975. scan_evt_t scan_evt;
  976. memset(&scan_evt, 0, sizeof(scan_evt));
  977. nrf_ble_scan_stop();
  978. // If the whitelist is used and the event handler is not NULL, send the whitelist request to the main application.
  979. if (is_whitelist_used(p_scan_ctx))
  980. {
  981. if (p_scan_ctx->evt_handler != NULL)
  982. {
  983. scan_evt.scan_evt_id = NRF_BLE_SCAN_EVT_WHITELIST_REQUEST;
  984. p_scan_ctx->evt_handler(&scan_evt);
  985. }
  986. }
  987. // Start the scanning.
  988. err_code = sd_ble_gap_scan_start(&p_scan_ctx->scan_params, &p_scan_ctx->scan_buffer);
  989. // It is okay to ignore this error, because the scan stopped earlier.
  990. if ((err_code != NRF_ERROR_INVALID_STATE) && (err_code != NRF_SUCCESS))
  991. {
  992. NRF_LOG_ERROR("sd_ble_gap_scan_start returned 0x%x", err_code);
  993. return (err_code);
  994. }
  995. NRF_LOG_DEBUG("Scanning");
  996. return NRF_SUCCESS;
  997. }
  998. ret_code_t nrf_ble_scan_params_set(nrf_ble_scan_t * const p_scan_ctx,
  999. ble_gap_scan_params_t const * const p_scan_param)
  1000. {
  1001. VERIFY_PARAM_NOT_NULL(p_scan_ctx);
  1002. nrf_ble_scan_stop();
  1003. if (p_scan_param != NULL)
  1004. {
  1005. // Assign new scanning parameters.
  1006. p_scan_ctx->scan_params = *p_scan_param;
  1007. }
  1008. else
  1009. {
  1010. // If NULL, use the default static configuration.
  1011. nrf_ble_scan_default_param_set(p_scan_ctx);
  1012. }
  1013. NRF_LOG_DEBUG("Scanning parameters have been changed successfully");
  1014. return NRF_SUCCESS;
  1015. }
  1016. /**@brief Function for calling the BLE_GAP_EVT_CONNECTED event.
  1017. *
  1018. * @param[in] p_scan_ctx Pointer to the Scanning Module instance.
  1019. * @param[in] p_gap_evt GAP event structure.
  1020. */
  1021. static void nrf_ble_scan_on_connected_evt(nrf_ble_scan_t const * const p_scan_ctx,
  1022. ble_gap_evt_t const * const p_gap_evt)
  1023. {
  1024. scan_evt_t scan_evt;
  1025. memset(&scan_evt, 0, sizeof(scan_evt));
  1026. scan_evt.scan_evt_id = NRF_BLE_SCAN_EVT_CONNECTED;
  1027. scan_evt.params.connected.p_connected = &p_gap_evt->params.connected;
  1028. scan_evt.params.connected.conn_handle = p_gap_evt->conn_handle;
  1029. scan_evt.p_scan_params = &p_scan_ctx->scan_params;
  1030. if (p_scan_ctx->evt_handler != NULL)
  1031. {
  1032. p_scan_ctx->evt_handler(&scan_evt);
  1033. }
  1034. }
  1035. ret_code_t nrf_ble_scan_copy_addr_to_sd_gap_addr(ble_gap_addr_t * p_gap_addr,
  1036. const uint8_t addr[BLE_GAP_ADDR_LEN])
  1037. {
  1038. uint16_t addr_type;
  1039. addr_type = nrf_ble_scan_address_type_decode(addr);
  1040. if (addr_type == BLE_ERROR_GAP_INVALID_BLE_ADDR)
  1041. {
  1042. return BLE_ERROR_GAP_INVALID_BLE_ADDR;
  1043. }
  1044. p_gap_addr->addr_type = addr_type;
  1045. for (uint8_t i = 0; i < BLE_GAP_ADDR_LEN; ++i)
  1046. {
  1047. p_gap_addr->addr[i] = addr[BLE_GAP_ADDR_LEN - (i + 1)];
  1048. }
  1049. return NRF_SUCCESS;
  1050. }
  1051. void nrf_ble_scan_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_contex)
  1052. {
  1053. nrf_ble_scan_t * p_scan_data = (nrf_ble_scan_t *)p_contex;
  1054. ble_gap_evt_adv_report_t const * p_adv_report = &p_ble_evt->evt.gap_evt.params.adv_report;
  1055. ble_gap_evt_t const * p_gap_evt = &p_ble_evt->evt.gap_evt;
  1056. switch (p_ble_evt->header.evt_id)
  1057. {
  1058. case BLE_GAP_EVT_ADV_REPORT:
  1059. nrf_ble_scan_on_adv_report(p_scan_data, p_adv_report);
  1060. break;
  1061. case BLE_GAP_EVT_TIMEOUT:
  1062. nrf_ble_scan_on_timeout(p_scan_data, p_gap_evt);
  1063. break;
  1064. case BLE_GAP_EVT_CONNECTED:
  1065. nrf_ble_scan_on_connected_evt(p_scan_data, p_gap_evt);
  1066. break;
  1067. default:
  1068. break;
  1069. }
  1070. }
  1071. #endif // NRF_BLE_SCAN_ENABLED