nrf_ble_scan(3374).c 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984
  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 "nrf_sdh.h"
  46. #include <string.h>
  47. #include "app_error.h"
  48. #include "nrf_assert.h"
  49. #include "sdk_macros.h"
  50. #include "ble_advdata.h"
  51. #define NRF_LOG_MODULE_NAME ble_scan
  52. #include "nrf_log.h"
  53. NRF_LOG_MODULE_REGISTER();
  54. #include <stdio.h>
  55. #include "our_service.h"
  56. #include "sysmgr.h"
  57. /**@brief Function for establishing the connection with a device.
  58. *
  59. * @details Connection is established if @ref NRF_BLE_SCAN_EVT_FILTER_MATCH
  60. * or @ref NRF_BLE_SCAN_EVT_WHITELIST_ADV_REPORT occurs and the module was
  61. * initialized in the automatic connection mode. This function can generate an event
  62. * to the main application when @ref sd_ble_gap_connect is used inside the function and it returns value
  63. * that is different than @ref NRF_SUCCESS.
  64. *
  65. * @param[in] p_scan_ctx Pointer to the Scanning Module instance.
  66. * @param[in] p_adv_report Advertising data.
  67. */
  68. static void nrf_ble_scan_connect_with_target(nrf_ble_scan_t const * const p_scan_ctx,
  69. ble_gap_evt_adv_report_t const * const p_adv_report)
  70. {
  71. ret_code_t err_code;
  72. scan_evt_t scan_evt;
  73. // For readability.
  74. ble_gap_addr_t const * p_addr = &p_adv_report->peer_addr;
  75. ble_gap_scan_params_t const * p_scan_params = &p_scan_ctx->scan_params;
  76. ble_gap_conn_params_t const * p_conn_params = &p_scan_ctx->conn_params;
  77. uint8_t con_cfg_tag = p_scan_ctx->conn_cfg_tag;
  78. // Return if the automatic connection is disabled.
  79. if (!p_scan_ctx->connect_if_match)
  80. {
  81. return;
  82. }
  83. // Stop scanning.
  84. nrf_ble_scan_stop();
  85. memset(&scan_evt, 0, sizeof(scan_evt));
  86. // Establish connection.
  87. err_code = sd_ble_gap_connect(p_addr,
  88. p_scan_params,
  89. p_conn_params,
  90. con_cfg_tag);
  91. NRF_LOG_DEBUG("Connecting");
  92. scan_evt.scan_evt_id = NRF_BLE_SCAN_EVT_CONNECTING_ERROR;
  93. scan_evt.params.connecting_err.err_code = err_code;
  94. NRF_LOG_DEBUG("Connection status: %d", err_code);
  95. // If an error occurred, send an event to the event handler.
  96. if ((err_code != NRF_SUCCESS) && (p_scan_ctx->evt_handler != NULL))
  97. {
  98. p_scan_ctx->evt_handler(&scan_evt);
  99. }
  100. }
  101. /**@brief Function for decoding the BLE address type.
  102. *
  103. * @param[in] p_addr The BLE address.
  104. *
  105. * @return Address type, or an error if the address type is incorrect, that is it does not match @ref BLE_GAP_ADDR_TYPES.
  106. *
  107. */
  108. static uint16_t nrf_ble_scan_address_type_decode(uint8_t const * p_addr)
  109. {
  110. uint8_t addr_type = p_addr[0];
  111. // See Bluetooth Core Specification Vol 6, Part B, section 1.3.
  112. addr_type = addr_type >> 6;
  113. addr_type &= 0x03;
  114. // Check address type.
  115. switch (addr_type)
  116. {
  117. case 0:
  118. {
  119. return BLE_GAP_ADDR_TYPE_RANDOM_PRIVATE_NON_RESOLVABLE;
  120. }
  121. case 1:
  122. {
  123. return BLE_GAP_ADDR_TYPE_PUBLIC;
  124. }
  125. case 2:
  126. {
  127. return BLE_GAP_ADDR_TYPE_RANDOM_PRIVATE_RESOLVABLE;
  128. }
  129. case 3:
  130. {
  131. return BLE_GAP_ADDR_TYPE_RANDOM_STATIC;
  132. }
  133. default:
  134. {
  135. return BLE_ERROR_GAP_INVALID_BLE_ADDR;
  136. }
  137. }
  138. }
  139. #if (NRF_BLE_SCAN_FILTER_ENABLE == 1)
  140. #if (NRF_BLE_SCAN_ADDRESS_CNT > 0)
  141. /**@brief Function for searching for the provided address in the advertisement packets.
  142. *
  143. * @details Use this function to parse the received advertising data for the provided address.
  144. *
  145. *
  146. * @param[in] p_adv_report Advertising data to parse.
  147. * @param[in] p_addr Address to search for. The address length must correspond to @ref BLE_GAP_ADDR_LEN.
  148. *
  149. * @return True if the provided address was found, false otherwise.
  150. */
  151. static bool find_peer_addr(ble_gap_evt_adv_report_t const * const p_adv_report,
  152. ble_gap_addr_t const * p_addr)
  153. {
  154. if (p_addr->addr_type == p_adv_report->peer_addr.addr_type)
  155. {
  156. // Compare addresses.
  157. if (memcmp(p_addr->addr,
  158. p_adv_report->peer_addr.addr,
  159. sizeof(p_adv_report->peer_addr.addr)) == 0)
  160. {
  161. return true;
  162. }
  163. }
  164. return false;
  165. }
  166. /** @brief Function for comparing the provided address with the addresses of the advertising devices.
  167. *
  168. * @param[in] p_adv_report Advertising data to parse.
  169. * @param[in] p_scan_ctx Pointer to the Scanning Module instance.
  170. *
  171. * @retval True when the address matches with the addresses of the advertising devices. False otherwise.
  172. */
  173. static bool adv_addr_compare(ble_gap_evt_adv_report_t const * const p_adv_report,
  174. nrf_ble_scan_t const * const p_scan_ctx)
  175. {
  176. ble_gap_addr_t const * p_addr = p_scan_ctx->scan_filters.addr_filter.target_addr;
  177. uint8_t counter = p_scan_ctx->scan_filters.addr_filter.addr_cnt;
  178. for (uint8_t index = 0; index < counter; index++)
  179. {
  180. // Search for address.
  181. if (find_peer_addr(p_adv_report, &p_addr[index]))
  182. {
  183. return true;
  184. }
  185. }
  186. return false;
  187. }
  188. /**@brief Function for adding target address to the scanning filter.
  189. *
  190. * @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.
  191. * @param[in,out] p_scan_ctx Pointer to the Scanning Module instance.
  192. *
  193. * @retval NRF_SUCCESS If the filter is added successfully or if you try to add a filter that was already added before.
  194. * @retval NRF_ERROR_NO_MEMORY If the number of available filters is exceeded.
  195. * @retval BLE_ERROR_GAP_INVALID_BLE_ADDR If the BLE address type is invalid.
  196. */
  197. static ret_code_t nrf_ble_scan_addr_filter_add(nrf_ble_scan_t * const p_scan_ctx,
  198. uint8_t const * p_addr)
  199. {
  200. ble_gap_addr_t * p_addr_filter = p_scan_ctx->scan_filters.addr_filter.target_addr;
  201. uint8_t * p_counter = &p_scan_ctx->scan_filters.addr_filter.addr_cnt;
  202. uint8_t index;
  203. uint16_t addr_type;
  204. uint8_t temp_addr[BLE_GAP_ADDR_LEN];
  205. // If no memory for filter.
  206. if (*p_counter >= NRF_BLE_SCAN_ADDRESS_CNT)
  207. {
  208. return NRF_ERROR_NO_MEM;
  209. }
  210. // Check for duplicated filter.
  211. for (index = 0; index < NRF_BLE_SCAN_ADDRESS_CNT; index++)
  212. {
  213. if (!memcmp(p_addr_filter[index].addr, p_addr, BLE_GAP_ADDR_LEN))
  214. {
  215. return NRF_SUCCESS;
  216. }
  217. }
  218. // Inverting the address.
  219. for (uint8_t i = 0; i < BLE_GAP_ADDR_LEN; i++)
  220. {
  221. temp_addr[i] = p_addr[(BLE_GAP_ADDR_LEN - 1) - i];
  222. }
  223. // Decode address type.
  224. addr_type = nrf_ble_scan_address_type_decode(temp_addr);
  225. if (addr_type == BLE_ERROR_GAP_INVALID_BLE_ADDR)
  226. {
  227. return BLE_ERROR_GAP_INVALID_BLE_ADDR;
  228. }
  229. // Add target address to filter.
  230. p_addr_filter[*p_counter].addr_type = (uint8_t)addr_type;
  231. for (uint8_t i = 0; i < BLE_GAP_ADDR_LEN; i++)
  232. {
  233. p_addr_filter[*p_counter].addr[i] = p_addr[i];
  234. }
  235. NRF_LOG_DEBUG("Filter set on address type %i, address 0x",
  236. p_addr_filter[*p_counter].addr_type);
  237. for (index = 0; index < BLE_GAP_ADDR_LEN; index++)
  238. {
  239. NRF_LOG_DEBUG("%x", p_addr_filter[*p_counter].addr[index]);
  240. }
  241. NRF_LOG_DEBUG("\n\r");
  242. // Increase the address filter counter.
  243. *p_counter += 1;
  244. return NRF_SUCCESS;
  245. }
  246. #endif // NRF_BLE_SCAN_ADDRESS_CNT
  247. #if (NRF_BLE_SCAN_NAME_CNT > 0)
  248. /** @brief Function for comparing the provided name with the advertised name.
  249. *
  250. * @param[in] p_adv_report Advertising data to parse.
  251. * @param[in] p_scan_ctx Pointer to the Scanning Module instance.
  252. *
  253. * @retval True when the names match. False otherwise.
  254. */
  255. static bool adv_name_compare(ble_gap_evt_adv_report_t const * p_adv_report,
  256. nrf_ble_scan_t const * const p_scan_ctx)
  257. {
  258. nrf_ble_scan_name_filter_t const * p_name_filter = &p_scan_ctx->scan_filters.name_filter;
  259. uint8_t counter =
  260. p_scan_ctx->scan_filters.name_filter.name_cnt;
  261. uint8_t index;
  262. uint16_t data_len;
  263. data_len = p_adv_report->data.len;
  264. // Compare the name found with the name filter.
  265. for (index = 0; index < counter; index++)
  266. {
  267. if (ble_advdata_name_find(p_adv_report->data.p_data,
  268. data_len,
  269. p_name_filter->target_name[index]))
  270. {
  271. return true;
  272. }
  273. }
  274. return false;
  275. }
  276. /**@brief Function for adding name of the peripheral to the scanning filter.
  277. *
  278. * @param[in] p_name Peripheral name.
  279. * @param[in,out] p_scan_ctx Pointer to the Scanning Module instance.
  280. *
  281. * @retval NRF_SUCCESS If the filter is added successfully or if you try to add a filter that was already added before.
  282. * @retval NRF_ERROR_NULL If a NULL pointer is passed as input.
  283. * @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.
  284. * @retval NRF_ERROR_NO_MEMORY If the number of available filters is exceeded.
  285. */
  286. static ret_code_t nrf_ble_scan_name_filter_add(nrf_ble_scan_t * const p_scan_ctx,
  287. char const * p_name)
  288. {
  289. uint8_t index;
  290. uint8_t * counter = &p_scan_ctx->scan_filters.name_filter.name_cnt;
  291. uint8_t name_len = strlen(p_name);
  292. // Check the name length.
  293. if ((name_len == 0) || (name_len > NRF_BLE_SCAN_NAME_MAX_LEN))
  294. {
  295. return NRF_ERROR_DATA_SIZE;
  296. }
  297. // If no memory for filter.
  298. if (*counter >= NRF_BLE_SCAN_NAME_CNT)
  299. {
  300. return NRF_ERROR_NO_MEM;
  301. }
  302. // Check for duplicated filter.
  303. for (index = 0; index < NRF_BLE_SCAN_NAME_CNT; index++)
  304. {
  305. if (!strcmp(p_scan_ctx->scan_filters.name_filter.target_name[index], p_name))
  306. {
  307. return NRF_SUCCESS;
  308. }
  309. }
  310. // Add name to filter.
  311. memcpy(p_scan_ctx->scan_filters.name_filter.target_name[(*counter)++],
  312. p_name,
  313. strlen(p_name));
  314. NRF_LOG_DEBUG("Adding filter on %s name", p_name);
  315. return NRF_SUCCESS;
  316. }
  317. #endif // NRF_BLE_SCAN_NAME_CNT
  318. #if (NRF_BLE_SCAN_SHORT_NAME_CNT > 0)
  319. /** @brief Function for comparing the provided short name with the advertised short name.
  320. *
  321. * @param[in] p_adv_report Advertising data to parse.
  322. * @param[in] p_scan_ctx Pointer to the Scanning Module instance.
  323. *
  324. * @retval True when the names match. False otherwise.
  325. */
  326. static bool adv_short_name_compare(ble_gap_evt_adv_report_t const * const p_adv_report,
  327. nrf_ble_scan_t const * const p_scan_ctx)
  328. {
  329. nrf_ble_scan_short_name_filter_t const * p_name_filter =
  330. &p_scan_ctx->scan_filters.short_name_filter;
  331. uint8_t counter = p_scan_ctx->scan_filters.short_name_filter.name_cnt;
  332. uint8_t index;
  333. uint16_t data_len;
  334. data_len = p_adv_report->data.len;
  335. // Compare the name found with the name filters.
  336. for (index = 0; index < counter; index++)
  337. {
  338. if (ble_advdata_short_name_find(p_adv_report->data.p_data,
  339. data_len,
  340. p_name_filter->short_name[index].short_target_name,
  341. p_name_filter->short_name[index].short_name_min_len))
  342. {
  343. return true;
  344. }
  345. }
  346. return false;
  347. }
  348. /**@brief Function for adding the short name of the peripheral to the scanning filter.
  349. *
  350. * @param[in] p_short_name Short name of the peripheral.
  351. * @param[in,out] p_scan_ctx Pointer to the Scanning Module instance.
  352. *
  353. * @retval NRF_SUCCESS If the filter is added successfully or if you try to add a filter that was already added before.
  354. * @retval NRF_ERROR_NULL If a NULL pointer is passed as input.
  355. * @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.
  356. * @retval NRF_ERROR_NO_MEMORY If the number of available filters is exceeded.
  357. */
  358. static ret_code_t nrf_ble_scan_short_name_filter_add(nrf_ble_scan_t * const p_scan_ctx,
  359. nrf_ble_scan_short_name_t const * p_short_name)
  360. {
  361. uint8_t index;
  362. uint8_t * p_counter =
  363. &p_scan_ctx->scan_filters.short_name_filter.name_cnt;
  364. nrf_ble_scan_short_name_filter_t * p_short_name_filter =
  365. &p_scan_ctx->scan_filters.short_name_filter;
  366. uint8_t name_len = strlen(p_short_name->p_short_name);
  367. // Check the name length.
  368. if ((name_len == 0) || (name_len > NRF_BLE_SCAN_SHORT_NAME_MAX_LEN))
  369. {
  370. return NRF_ERROR_DATA_SIZE;
  371. }
  372. // If no memory for filter.
  373. if (*p_counter >= NRF_BLE_SCAN_SHORT_NAME_CNT)
  374. {
  375. return NRF_ERROR_NO_MEM;
  376. }
  377. // Check for duplicated filter.
  378. for (index = 0; index < NRF_BLE_SCAN_SHORT_NAME_CNT; index++)
  379. {
  380. if (!strcmp(p_short_name_filter->short_name[index].short_target_name,
  381. p_short_name->p_short_name))
  382. {
  383. return NRF_SUCCESS;
  384. }
  385. }
  386. // Add name to the filter.
  387. p_short_name_filter->short_name[(*p_counter)].short_name_min_len =
  388. p_short_name->short_name_min_len;
  389. memcpy(p_short_name_filter->short_name[(*p_counter)++].short_target_name,
  390. p_short_name->p_short_name,
  391. strlen(p_short_name->p_short_name));
  392. NRF_LOG_DEBUG("Adding filter on %s name", p_short_name->p_short_name);
  393. return NRF_SUCCESS;
  394. }
  395. #endif
  396. #if (NRF_BLE_SCAN_UUID_CNT > 0)
  397. /**@brief Function for comparing the provided UUID with the UUID in the advertisement packets.
  398. *
  399. * @param[in] p_adv_report Advertising data to parse.
  400. * @param[in] p_scan_ctx Pointer to the Scanning Module instance.
  401. *
  402. * @return True if the UUIDs match. False otherwise.
  403. */
  404. static bool adv_uuid_compare(ble_gap_evt_adv_report_t const * const p_adv_report,
  405. nrf_ble_scan_t const * const p_scan_ctx)
  406. {
  407. nrf_ble_scan_uuid_filter_t const * p_uuid_filter = &p_scan_ctx->scan_filters.uuid_filter;
  408. bool const all_filters_mode = p_scan_ctx->scan_filters.all_filters_mode;
  409. uint8_t const counter =
  410. p_scan_ctx->scan_filters.uuid_filter.uuid_cnt;
  411. uint8_t index;
  412. uint16_t data_len;
  413. uint8_t uuid_match_cnt = 0;
  414. data_len = p_adv_report->data.len;
  415. for (index = 0; index < counter; index++)
  416. {
  417. if (ble_advdata_uuid_find(p_adv_report->data.p_data,
  418. data_len,
  419. &p_uuid_filter->uuid[index]))
  420. {
  421. uuid_match_cnt++;
  422. // In the normal filter mode, only one UUID is needed to match.
  423. if (!all_filters_mode)
  424. {
  425. break;
  426. }
  427. }
  428. else if (all_filters_mode)
  429. {
  430. break;
  431. }
  432. else
  433. {
  434. // Do nothing.
  435. }
  436. }
  437. // In the multifilter mode, all UUIDs must be found in the advertisement packets.
  438. if ((all_filters_mode && (uuid_match_cnt == counter)) ||
  439. ((!all_filters_mode) && (uuid_match_cnt > 0)))
  440. {
  441. return true;
  442. }
  443. return false;
  444. }
  445. /**@brief Function for adding UUID to the scanning filter.
  446. *
  447. * @param[in] uuid UUID, 16-bit size.
  448. * @param[in,out] p_scan_ctx Pointer to the Scanning Module instance.
  449. *
  450. * @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.
  451. * @retval NRF_ERROR_NO_MEMORY If the number of available filters is exceeded.
  452. */
  453. static ret_code_t nrf_ble_scan_uuid_filter_add(nrf_ble_scan_t * const p_scan_ctx,
  454. ble_uuid_t const * p_uuid)
  455. {
  456. ble_uuid_t * p_uuid_filter = p_scan_ctx->scan_filters.uuid_filter.uuid;
  457. uint8_t * p_counter = &p_scan_ctx->scan_filters.uuid_filter.uuid_cnt;
  458. uint8_t index;
  459. // If no memory.
  460. if (*p_counter >= NRF_BLE_SCAN_UUID_CNT)
  461. {
  462. return NRF_ERROR_NO_MEM;
  463. }
  464. // Check for duplicated filter.
  465. for (index = 0; index < NRF_BLE_SCAN_UUID_CNT; index++)
  466. {
  467. if (p_uuid_filter[index].uuid == p_uuid->uuid)
  468. {
  469. return NRF_SUCCESS;
  470. }
  471. }
  472. // Add UUID to the filter.
  473. p_uuid_filter[(*p_counter)++] = *p_uuid;
  474. NRF_LOG_DEBUG("Added filter on UUID %x", p_uuid->uuid);
  475. return NRF_SUCCESS;
  476. }
  477. #endif // NRF_BLE_SCAN_UUID_CNT
  478. #if (NRF_BLE_SCAN_APPEARANCE_CNT)
  479. /**@brief Function for comparing the provided appearance with the appearance in the advertisement packets.
  480. *
  481. * @param[in] p_adv_report Advertising data to parse.
  482. * @param[in,out] p_scan_ctx Pointer to the Scanning Module instance.
  483. *
  484. * @return True if the appearances match. False otherwise.
  485. */
  486. static bool adv_appearance_compare(ble_gap_evt_adv_report_t const * const p_adv_report,
  487. nrf_ble_scan_t const * const p_scan_ctx)
  488. {
  489. nrf_ble_scan_appearance_filter_t const * p_appearance_filter =
  490. &p_scan_ctx->scan_filters.appearance_filter;
  491. uint8_t const counter =
  492. p_scan_ctx->scan_filters.appearance_filter.appearance_cnt;
  493. uint8_t index;
  494. uint16_t data_len;
  495. data_len = p_adv_report->data.len;
  496. // Verify if the advertised appearance matches the provided appearance.
  497. for (index = 0; index < counter; index++)
  498. {
  499. if (ble_advdata_appearance_find(p_adv_report->data.p_data,
  500. data_len,
  501. &p_appearance_filter->appearance[index]))
  502. {
  503. return true;
  504. }
  505. }
  506. return false;
  507. }
  508. /**@brief Function for adding appearance to the scanning filter.
  509. *
  510. * @param[in] appearance Appearance to be added.
  511. * @param[in,out] p_scan_ctx Pointer to the Scanning Module instance.
  512. *
  513. * @retval NRF_SUCCESS If the filter is added successfully or if you try to add a filter that was already added before.
  514. * @retval NRF_ERROR_NULL If a NULL pointer is passed as input.
  515. * @retval NRF_ERROR_NO_MEMORY If the number of available filters is exceeded.
  516. */
  517. static ret_code_t nrf_ble_scan_appearance_filter_add(nrf_ble_scan_t * const p_scan_ctx,
  518. uint16_t appearance)
  519. {
  520. uint16_t * p_appearance_filter = p_scan_ctx->scan_filters.appearance_filter.appearance;
  521. uint8_t * p_counter = &p_scan_ctx->scan_filters.appearance_filter.appearance_cnt;
  522. uint8_t index;
  523. // If no memory.
  524. if (*p_counter >= NRF_BLE_SCAN_APPEARANCE_CNT)
  525. {
  526. return NRF_ERROR_NO_MEM;
  527. }
  528. // Check for duplicated filter.
  529. for ( index = 0; index < NRF_BLE_SCAN_APPEARANCE_CNT; index++)
  530. {
  531. if (p_appearance_filter[index] == appearance)
  532. {
  533. return NRF_SUCCESS;
  534. }
  535. }
  536. // Add appearance to the filter.
  537. p_appearance_filter[(*p_counter)++] = appearance;
  538. NRF_LOG_DEBUG("Added filter on appearance %x", appearance);
  539. return NRF_SUCCESS;
  540. }
  541. #endif // NRF_BLE_SCAN_APPEARANCE_CNT
  542. ret_code_t nrf_ble_scan_filter_set(nrf_ble_scan_t * const p_scan_ctx,
  543. nrf_ble_scan_filter_type_t type,
  544. void const * p_data)
  545. {
  546. VERIFY_PARAM_NOT_NULL(p_scan_ctx);
  547. VERIFY_PARAM_NOT_NULL(p_data);
  548. switch (type)
  549. {
  550. #if (NRF_BLE_SCAN_NAME_CNT > 0)
  551. case SCAN_NAME_FILTER:
  552. {
  553. char * p_name = (char *)p_data;
  554. return nrf_ble_scan_name_filter_add(p_scan_ctx, p_name);
  555. }
  556. #endif
  557. #if (NRF_BLE_SCAN_SHORT_NAME_CNT > 0)
  558. case SCAN_SHORT_NAME_FILTER:
  559. {
  560. nrf_ble_scan_short_name_t * p_short_name = (nrf_ble_scan_short_name_t *)p_data;
  561. return nrf_ble_scan_short_name_filter_add(p_scan_ctx, p_short_name);
  562. }
  563. #endif
  564. #if (NRF_BLE_SCAN_ADDRESS_CNT > 0)
  565. case SCAN_ADDR_FILTER:
  566. {
  567. uint8_t * p_addr = (uint8_t *)p_data;
  568. return nrf_ble_scan_addr_filter_add(p_scan_ctx, p_addr);
  569. }
  570. #endif
  571. #if (NRF_BLE_SCAN_UUID_CNT > 0)
  572. case SCAN_UUID_FILTER:
  573. {
  574. ble_uuid_t * p_uuid = (ble_uuid_t *)p_data;
  575. return nrf_ble_scan_uuid_filter_add(p_scan_ctx, p_uuid);
  576. }
  577. #endif
  578. #if (NRF_BLE_SCAN_APPEARANCE_CNT > 0)
  579. case SCAN_APPEARANCE_FILTER:
  580. {
  581. uint16_t appearance = *((uint16_t *)p_data);
  582. return nrf_ble_scan_appearance_filter_add(p_scan_ctx, appearance);
  583. }
  584. #endif
  585. default:
  586. return NRF_ERROR_INVALID_PARAM;
  587. }
  588. }
  589. ret_code_t nrf_ble_scan_all_filter_remove(nrf_ble_scan_t * const p_scan_ctx)
  590. {
  591. #if (NRF_BLE_SCAN_NAME_CNT > 0)
  592. nrf_ble_scan_name_filter_t * p_name_filter = &p_scan_ctx->scan_filters.name_filter;
  593. memset(p_name_filter->target_name, 0, sizeof(p_name_filter->target_name));
  594. p_name_filter->name_cnt = 0;
  595. #endif
  596. #if (NRF_BLE_SCAN_SHORT_NAME_CNT > 0)
  597. nrf_ble_scan_short_name_filter_t * p_short_name_filter =
  598. &p_scan_ctx->scan_filters.short_name_filter;
  599. memset(p_short_name_filter->short_name, 0, sizeof(p_short_name_filter->short_name));
  600. p_short_name_filter->name_cnt = 0;
  601. #endif
  602. #if (NRF_BLE_SCAN_ADDRESS_CNT > 0)
  603. nrf_ble_scan_addr_filter_t * p_addr_filter = &p_scan_ctx->scan_filters.addr_filter;
  604. memset(p_addr_filter->target_addr, 0, sizeof(p_addr_filter->target_addr));
  605. p_addr_filter->addr_cnt = 0;
  606. #endif
  607. #if (NRF_BLE_SCAN_UUID_CNT > 0)
  608. nrf_ble_scan_uuid_filter_t * p_uuid_filter = &p_scan_ctx->scan_filters.uuid_filter;
  609. memset(p_uuid_filter->uuid, 0, sizeof(p_uuid_filter->uuid));
  610. p_uuid_filter->uuid_cnt = 0;
  611. #endif
  612. #if (NRF_BLE_SCAN_APPEARANCE_CNT > 0)
  613. nrf_ble_scan_appearance_filter_t * p_appearance_filter =
  614. &p_scan_ctx->scan_filters.appearance_filter;
  615. memset(p_appearance_filter->appearance, 0, sizeof(p_appearance_filter->appearance));
  616. p_appearance_filter->appearance_cnt = 0;
  617. #endif
  618. return NRF_SUCCESS;
  619. }
  620. ret_code_t nrf_ble_scan_filters_enable(nrf_ble_scan_t * const p_scan_ctx,
  621. uint8_t mode,
  622. bool match_all)
  623. {
  624. VERIFY_PARAM_NOT_NULL(p_scan_ctx);
  625. // Check if the mode is correct.
  626. if ((!(mode & NRF_BLE_SCAN_ADDR_FILTER)) &&
  627. (!(mode & NRF_BLE_SCAN_NAME_FILTER)) &&
  628. (!(mode & NRF_BLE_SCAN_UUID_FILTER)) &&
  629. (!(mode & NRF_BLE_SCAN_SHORT_NAME_FILTER)) &&
  630. (!(mode & NRF_BLE_SCAN_APPEARANCE_FILTER)))
  631. {
  632. return NRF_ERROR_INVALID_PARAM;
  633. }
  634. ret_code_t err_code;
  635. // Disable filters.
  636. err_code = nrf_ble_scan_filters_disable(p_scan_ctx);
  637. ASSERT(err_code == NRF_SUCCESS);
  638. nrf_ble_scan_filters_t * p_filters = &p_scan_ctx->scan_filters;
  639. // Turn on the filters of your choice.
  640. #if (NRF_BLE_SCAN_ADDRESS_CNT > 0)
  641. if (mode & NRF_BLE_SCAN_ADDR_FILTER)
  642. {
  643. p_filters->addr_filter.addr_filter_enabled = true;
  644. }
  645. #endif
  646. #if (NRF_BLE_SCAN_NAME_CNT > 0)
  647. if (mode & NRF_BLE_SCAN_NAME_FILTER)
  648. {
  649. p_filters->name_filter.name_filter_enabled = true;
  650. }
  651. #endif
  652. #if (NRF_BLE_SCAN_SHORT_NAME_CNT > 0)
  653. if (mode & NRF_BLE_SCAN_SHORT_NAME_FILTER)
  654. {
  655. p_filters->short_name_filter.short_name_filter_enabled = true;
  656. }
  657. #endif
  658. #if (NRF_BLE_SCAN_UUID_CNT > 0)
  659. if (mode & NRF_BLE_SCAN_UUID_FILTER)
  660. {
  661. p_filters->uuid_filter.uuid_filter_enabled = true;
  662. }
  663. #endif
  664. #if (NRF_BLE_SCAN_APPEARANCE_CNT > 0)
  665. if (mode & NRF_BLE_SCAN_APPEARANCE_FILTER)
  666. {
  667. p_filters->appearance_filter.appearance_filter_enabled = true;
  668. }
  669. #endif
  670. // Select the filter mode.
  671. p_filters->all_filters_mode = match_all;
  672. return NRF_SUCCESS;
  673. }
  674. ret_code_t nrf_ble_scan_filters_disable(nrf_ble_scan_t * const p_scan_ctx)
  675. {
  676. VERIFY_PARAM_NOT_NULL(p_scan_ctx);
  677. // Disable all filters.
  678. #if (NRF_BLE_SCAN_NAME_CNT > 0)
  679. bool * p_name_filter_enabled = &p_scan_ctx->scan_filters.name_filter.name_filter_enabled;
  680. *p_name_filter_enabled = false;
  681. #endif
  682. #if (NRF_BLE_SCAN_ADDRESS_CNT > 0)
  683. bool * p_addr_filter_enabled = &p_scan_ctx->scan_filters.addr_filter.addr_filter_enabled;
  684. *p_addr_filter_enabled = false;
  685. #endif
  686. #if (NRF_BLE_SCAN_UUID_CNT > 0)
  687. bool * p_uuid_filter_enabled = &p_scan_ctx->scan_filters.uuid_filter.uuid_filter_enabled;
  688. *p_uuid_filter_enabled = false;
  689. #endif
  690. #if (NRF_BLE_SCAN_APPEARANCE_CNT > 0)
  691. bool * p_appearance_filter_enabled =
  692. &p_scan_ctx->scan_filters.appearance_filter.appearance_filter_enabled;
  693. *p_appearance_filter_enabled = false;
  694. #endif
  695. return NRF_SUCCESS;
  696. }
  697. ret_code_t nrf_ble_scan_filter_get(nrf_ble_scan_t * const p_scan_ctx,
  698. nrf_ble_scan_filters_t * p_status)
  699. {
  700. VERIFY_PARAM_NOT_NULL(p_scan_ctx);
  701. VERIFY_PARAM_NOT_NULL(p_status);
  702. *p_status = p_scan_ctx->scan_filters;
  703. return NRF_SUCCESS;
  704. }
  705. #endif // NRF_BLE_SCAN_FILTER_ENABLE
  706. int ISCPacket;
  707. char ScanHSBuff[15];
  708. uint8_t *pAdvRep;
  709. #if 0
  710. int HS_AdParser(char* data)
  711. {
  712. int len;
  713. int j;
  714. int i;
  715. char* pData = data;
  716. uint8_t temp_buf[6] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
  717. if( *(data+1) == 0xff )
  718. return 0;
  719. len = *data;
  720. //NRF_LOG_RAW_INFO("Len:%d ", len );
  721. data++;
  722. //NRF_LOG_RAW_INFO("Flag:0x%02x \n", *data );
  723. SystemManager.PtrLocalNameLen = len;
  724. ISCPacket = 0;
  725. if( *data == 9)
  726. {
  727. SystemManager.PtrLocalName = data+1;
  728. if( strncmp("ICSB", data+1, 4) == 0 )
  729. {
  730. if( SystemManager.LedBlinkTimer < 0 )
  731. {
  732. ISCPacket = 1;
  733. SystemManager.DevType = DEV_BAND_ADV;
  734. }
  735. }
  736. else if( strncmp("ICSCAR", data+1, 6) == 0 )
  737. {
  738. NRF_LOG_RAW_INFO("ICSCAR\n");
  739. SystemManager.TmpPtr = data+7;
  740. if( memcmp( SystemManager.TmpPtr, new_ble_addr.addr, 6) == 0 ) // Respons
  741. {
  742. SystemManager.DevType = DEV_BAND_PARK;
  743. ISCPacket = 5;
  744. }
  745. else if(memcmp( SystemManager.TmpPtr, temp_buf, 6) == 0 ) // Advertise
  746. {
  747. ISCPacket = 5;
  748. }
  749. // SystemManager.BandMode = AES_ENC_CAR;
  750. }
  751. else if( strncmp("ICSEMG", data+1, 6) == 0 )
  752. {
  753. NRF_LOG_RAW_INFO("ICSEMG\n");
  754. SystemManager.TmpPtr = data+7;
  755. // SystemManager.BandMode = AES_ENC_EMG;
  756. if( memcmp( SystemManager.TmpPtr, new_ble_addr.addr, 6) == 0 ) // Response
  757. {
  758. // SystemManager.DevType = DEV_EMG_BAND;
  759. NRF_LOG_RAW_INFO("==> DEV_EMG_BAND\n");
  760. ISCPacket = 5;
  761. }
  762. else if(memcmp( SystemManager.TmpPtr, temp_buf, 6) == 0 ) // Advertise
  763. {
  764. ISCPacket = 5;
  765. }
  766. }
  767. else if( strncmp("ICSWES", data+1, 6) == 0 )
  768. {
  769. for(i=0; i<13; i++){
  770. NRF_LOG_RAW_INFO("%02X, ", pData[i]);
  771. }
  772. NRF_LOG_RAW_INFO("\n");
  773. if( SystemManager.LedBlinkTimer < 0 )
  774. {
  775. // ISCPacket = 1;
  776. //SystemTimer.TMR_CON_START = 0;
  777. // SystemManager.DevType = DEV_WES_ADV;
  778. }
  779. ISCPacket = 5;
  780. SystemManager.DevType = 0;
  781. NRF_LOG_RAW_INFO("==> ICSWES\n");
  782. if( SystemManager.RegRequst == 1 ) // 등록모드면
  783. {
  784. ISCPacket = 1;
  785. SystemManager.DevType = DEV_WES_ADV;
  786. }
  787. SystemManager.TmpPtr = data+7;
  788. if( memcmp( SystemManager.TmpPtr, new_ble_addr.addr, 6) == 0 )
  789. {
  790. for(i=0; i<13; i++){
  791. NRF_LOG_RAW_INFO("%02X, ", pData[i]);
  792. }
  793. NRF_LOG_RAW_INFO("\n");
  794. SystemManager.DevType = DEV_WES_ADV;
  795. ISCPacket = 1;
  796. NRF_LOG_RAW_INFO("==> ICSWES_MAC\n");
  797. }
  798. SystemManager.BandMode = AES_ENC_WES;
  799. }
  800. else if( strncmp("ICSPCA", data+1, 6) == 0 )
  801. {
  802. // char* pData = data;
  803. for(i=0; i<17; i++){
  804. NRF_LOG_RAW_INFO("%02X, ", pData[i]);
  805. }
  806. NRF_LOG_RAW_INFO("\n");
  807. if( SystemManager.LedBlinkTimer < 0 )
  808. {
  809. //ISCPacket = 1;
  810. //SystemManager.DevType = DEV_IOS_PARK;
  811. }
  812. ISCPacket = 2;
  813. SystemManager.DevType = 0;
  814. NRF_LOG_RAW_INFO("==> ICSPCA\n");
  815. SystemManager.TmpPtr = data+7;
  816. if( memcmp( SystemManager.TmpPtr, new_ble_addr.addr, 6) == 0 )
  817. {
  818. for(i=0; i<17; i++)
  819. NRF_LOG_RAW_INFO("%02X, ", pData[i]);
  820. NRF_LOG_RAW_INFO("\n");
  821. SystemManager.DevType = DEV_IOS_PARK;
  822. ISCPacket = 5;
  823. NRF_LOG_RAW_INFO("==> ICSPCA_MAC\n");
  824. }
  825. SystemManager.BandMode = AES_ENC_PCA;
  826. }
  827. }
  828. return 0;
  829. }
  830. #else
  831. int HS_AdParser(char* data)
  832. {
  833. int len;
  834. int j;
  835. int i;
  836. char* pData = data;
  837. if( *(data+1) == 0xff )
  838. return 0;
  839. len = *data;
  840. //NRF_LOG_RAW_INFO("Len:%d ", len );
  841. data++;
  842. //NRF_LOG_RAW_INFO("Flag:0x%02x \n", *data );
  843. SystemManager.PtrLocalNameLen = len;
  844. ISCPacket = 0;
  845. if( *data == 9)
  846. {
  847. SystemManager.PtrLocalName = data+1;
  848. #if 0 // jakuja
  849. for(i=0; i<len; i++)
  850. NRF_LOG_RAW_INFO("%02X, ", data[i]);
  851. NRF_LOG_RAW_INFO("\n");
  852. #endif
  853. if( strncmp("ICSCAR", data+1, 6) == 0 )
  854. {
  855. NRF_LOG_RAW_INFO("ICSCAR\n");
  856. SystemManager.TmpPtr = data+7;
  857. if( memcmp( SystemManager.TmpPtr, new_ble_addr.addr, 6) == 0 )
  858. {
  859. SystemManager.DevType = DEV_BAND_PARK;
  860. ISCPacket = 5;
  861. }
  862. SystemManager.BandMode = AES_ENC_CAR;
  863. }
  864. else if( strncmp("ICSEMG", data+1, 6) == 0 )
  865. {
  866. NRF_LOG_RAW_INFO("ICSEMG\n");
  867. SystemManager.TmpPtr = data+7;
  868. SystemManager.BandMode = AES_ENC_EMG;
  869. if( memcmp( SystemManager.TmpPtr, new_ble_addr.addr, 6) == 0 )
  870. {
  871. SystemManager.DevType = DEV_EMG_BAND;
  872. NRF_LOG_RAW_INFO("==> DEV_EMG_BAND\n");
  873. ISCPacket = 5;
  874. }
  875. }
  876. else if( strncmp("ICSB", data+1, 4) == 0 )
  877. {
  878. if( SystemManager.LedBlinkTimer < 0 )
  879. {
  880. ISCPacket = 1;
  881. //SystemTimer.TMR_CON_START = 0;
  882. SystemManager.DevType = DEV_BAND_ADV;
  883. }
  884. }else if( strncmp("ICSWES", data+1, 6) == 0 )
  885. {
  886. if( SystemManager.LedBlinkTimer < 0 )
  887. {
  888. ISCPacket = 1;
  889. //SystemTimer.TMR_CON_START = 0;
  890. SystemManager.DevType = DEV_WES_ADV;
  891. }
  892. }else if( strncmp("ICSPCA", data+1, 6) == 0 )
  893. {
  894. for(i=0; i<17; i++)
  895. NRF_LOG_RAW_INFO("%02X, ", pData[i]);
  896. NRF_LOG_RAW_INFO("\n");
  897. SystemManager.TmpPtr = data+7;
  898. if( memcmp( SystemManager.TmpPtr, new_ble_addr.addr, 6) == 0 )
  899. {
  900. SystemManager.DevType = DEV_IOS_PARK;
  901. NRF_LOG_RAW_INFO("==> DEV_IOS_PARK\n");
  902. ISCPacket = 5;
  903. SystemManager.LocalNameAddr[0] = new_ble_addr.addr[0];
  904. SystemManager.LocalNameAddr[1] = new_ble_addr.addr[1];
  905. SystemManager.LocalNameAddr[2] = new_ble_addr.addr[2];
  906. SystemManager.LocalNameAddr[3] = new_ble_addr.addr[3];
  907. SystemManager.LocalNameAddr[4] = new_ble_addr.addr[4];
  908. SystemManager.LocalNameAddr[5] = new_ble_addr.addr[5];
  909. }
  910. SystemManager.BandMode = AES_ENC_PCA;
  911. // if( SystemManager.LedBlinkTimer < 0 )
  912. // {
  913. // ISCPacket = 1;
  914. // //SystemTimer.TMR_CON_START = 0;
  915. // SystemManager.DevType = DEV_IOS_PARK;
  916. // }
  917. }
  918. }
  919. return 0;
  920. }
  921. #endif
  922. void HS_ParseAdvPacket(char* data, int dataLen, int flag)
  923. {
  924. int len, validLen;
  925. char* pData;
  926. int i;
  927. int j;
  928. int v=0;
  929. int result;
  930. pData = data;
  931. validLen = dataLen;
  932. #if 1
  933. if( flag )
  934. {
  935. // NRF_LOG_RAW_INFO("MEM : %08x ==> ", pData) ;
  936. while(1)
  937. {
  938. result = HS_AdParser(pData);
  939. // if( result == 0 )
  940. // break;
  941. len = *pData;
  942. validLen -= len+1;
  943. //printf("len:%d validLen:%d\n", len, validLen);
  944. if( validLen <= 0 )
  945. break;
  946. pData += len+1;
  947. }
  948. }
  949. #endif
  950. }
  951. /**@brief Function for calling the BLE_GAP_EVT_ADV_REPORT event to check whether the received
  952. * scanning data matches the scan configuration.
  953. *
  954. * @param[in] p_scan_ctx Pointer to the Scanning Module instance.
  955. * @param[in] p_adv_report Advertising report.
  956. */
  957. // jakuja
  958. static void nrf_ble_scan_on_adv_report(nrf_ble_scan_t const * const p_scan_ctx, ble_gap_evt_adv_report_t const * const p_adv_report)
  959. {
  960. scan_evt_t scan_evt;
  961. int i;
  962. uint32_t err_code;
  963. uint8_t *pdata;
  964. uint8_t addr[6];
  965. #if (NRF_BLE_SCAN_FILTER_ENABLE == 1)
  966. uint8_t filter_cnt = 0;
  967. uint8_t filter_match_cnt = 0;
  968. #endif
  969. memset(&scan_evt, 0, sizeof(scan_evt));
  970. scan_evt.p_scan_params = &p_scan_ctx->scan_params;
  971. pdata = p_adv_report->data.p_data;
  972. pAdvRep = p_adv_report->data.p_data;;
  973. if( SystemTimer.SVR_SEND_TIMER < 1500 )
  974. goto SCAN_GOING;
  975. #if 0 // jakuja
  976. for(i=0; i<p_adv_report->data.len; i++)
  977. NRF_LOG_RAW_INFO("%02X, ", p_adv_report->data.p_data[i]);
  978. NRF_LOG_RAW_INFO("\n");
  979. #endif
  980. // NRF_LOG_RAW_INFO("m_nrf_sdh_continue : %d, m_nrf_sdh_enabled : %d ", m_nrf_sdh_continue, m_nrf_sdh_enabled);
  981. // ONE PASS APP (안드로이드)
  982. if(p_adv_report->data.p_data[3] == 0x19 && p_adv_report->data.p_data[4] == 0xFF && p_adv_report->data.p_data[5] == 0x31 && p_adv_report->data.p_data[6] == 0x01 && p_adv_report->data.p_data[7] == 0x02 && p_adv_report->data.p_data[8] == 0xEE){
  983. AppParkingCheck_Android(p_adv_report->data.p_data);
  984. goto SCAN_GOING;
  985. }
  986. // ONE PASS APP (IOS)
  987. if(p_adv_report->data.p_data[3] == 0x02 && p_adv_report->data.p_data[4] == 0x0A && p_adv_report->data.p_data[5] == 0x0C && p_adv_report->data.p_data[6] == 0x11 && p_adv_report->data.p_data[7] == 0x07 ){
  988. //NRF_LOG_RAW_INFO("IOS APP PARKING\n");
  989. AppParkingCheck_IOS(p_adv_report->data.p_data);
  990. goto SCAN_GOING;
  991. }
  992. HS_ParseAdvPacket(p_adv_report->data.p_data, p_adv_report->data.len, 1);
  993. if( SystemManager.DevType == DEV_NONE )
  994. goto SCAN_GOING;
  995. sprintf(ScanHSBuff, "%02x%02x%02x%02x%02x%02x", p_adv_report->peer_addr.addr[0],
  996. p_adv_report->peer_addr.addr[1],
  997. p_adv_report->peer_addr.addr[2],
  998. p_adv_report->peer_addr.addr[3],
  999. p_adv_report->peer_addr.addr[4],
  1000. p_adv_report->peer_addr.addr[5]);
  1001. ScanHSBuff[12] = 0;
  1002. #if 1
  1003. SystemManager.PeerAddr[0] = p_adv_report->peer_addr.addr[0];
  1004. SystemManager.PeerAddr[1] = p_adv_report->peer_addr.addr[1];
  1005. SystemManager.PeerAddr[2] = p_adv_report->peer_addr.addr[2];
  1006. SystemManager.PeerAddr[3] = p_adv_report->peer_addr.addr[3];
  1007. SystemManager.PeerAddr[4] = p_adv_report->peer_addr.addr[4];
  1008. SystemManager.PeerAddr[5] = p_adv_report->peer_addr.addr[5];
  1009. #endif
  1010. SystemManager.PtrPeerAddrStr = ScanHSBuff;
  1011. if( SystemManager.DevType == DEV_BAND_PARK )
  1012. {
  1013. NRF_LOG_RAW_INFO("================= Send Park\n");
  1014. if( SystemManager.LedBlinkTimer && SystemTimer.PARK_KEEP_TIMER > 6000)
  1015. {
  1016. SystemManager.LedBlinkRun = 1;
  1017. SystemManager.LedBlinkTimer = 3000;
  1018. SystemTimer.LED_TOGGLE_TIMER = 0;
  1019. SystemTimer.PARK_KEEP_TIMER = 0;
  1020. SystemManager.DevType = 0;
  1021. ISCPacket = 0;
  1022. NRF_LOG_RAW_INFO("name len is %d ", SystemManager.PtrLocalNameLen);
  1023. for( i=0; i<SystemManager.PtrLocalNameLen; i++)
  1024. {
  1025. NRF_LOG_RAW_INFO("%02x ", *(SystemManager.PtrLocalName+i) );
  1026. }
  1027. NRF_LOG_RAW_INFO("\n");
  1028. }
  1029. // goto SCAN_GOING;
  1030. goto BAND_CONNECT;
  1031. }
  1032. else if( SystemManager.DevType == DEV_EMG_BUTTON )
  1033. {
  1034. NRF_LOG_RAW_INFO("================= DEV_EMG_BUTTON\n");
  1035. ISCPacket = 1;
  1036. //if( SystemManager.LedBlinkTimer < 0 )
  1037. {
  1038. SystemManager.LedBlinkRun = 1;
  1039. SystemManager.LedBlinkTimer = 5000;
  1040. SystemTimer.LED_TOGGLE_TIMER = 0;
  1041. SystemManager.DevType = 0;
  1042. //ISCPacket = 0;
  1043. }
  1044. //return;
  1045. }
  1046. else if( SystemManager.DevType == DEV_EMG_BAND )
  1047. {
  1048. NRF_LOG_RAW_INFO("================= DEV_EMG_BAND\n");
  1049. ISCPacket = 2;
  1050. }else if( SystemManager.DevType == DEV_WES_ADV )
  1051. {
  1052. NRF_LOG_RAW_INFO("================= DEV_WES_ADV\n");
  1053. ISCPacket = 1;
  1054. }else if( SystemManager.DevType == DEV_IOS_PARK )
  1055. {
  1056. NRF_LOG_RAW_INFO("================= DEV_IOS_PARK\n");
  1057. NRF_LOG_RAW_INFO("name len is %d ", SystemManager.PtrLocalNameLen);
  1058. for( i=0; i<SystemManager.PtrLocalNameLen; i++)
  1059. {
  1060. NRF_LOG_RAW_INFO("%02x ", *(SystemManager.PtrLocalName+i) );
  1061. }
  1062. NRF_LOG_RAW_INFO("\n");
  1063. ISCPacket = 0;
  1064. // goto SCAN_GOING;
  1065. goto BAND_CONNECT;
  1066. }
  1067. if( ISCPacket == 1)
  1068. {
  1069. int i;
  1070. if( FindRegMac(ScanHSBuff) == 0 )
  1071. {
  1072. scan_evt.scan_evt_id = NRF_BLE_SCAN_EVT_NOT_FOUND;
  1073. scan_evt.params.p_not_found = p_adv_report;
  1074. // If the event handler is not NULL, notify the main application.
  1075. if (p_scan_ctx->evt_handler != NULL)
  1076. {
  1077. //p_scan_ctx->evt_handler(&scan_evt);
  1078. }
  1079. // Resume the scanning.
  1080. UNUSED_RETURN_VALUE(sd_ble_gap_scan_start(NULL, &p_scan_ctx->scan_buffer));
  1081. ISCPacket = 0;
  1082. if( SystemManager.RegRequst == 0 ) // 등록모드가 아니면
  1083. return;
  1084. }
  1085. //NRF_LOG_RAW_INFO("================= Emergency\n");
  1086. for( i=0; i<6; i++)
  1087. {
  1088. NRF_LOG_RAW_INFO("%02x ", p_adv_report->peer_addr.addr[i]);
  1089. }
  1090. NRF_LOG_RAW_INFO("\n");
  1091. ISCPacket = 0;
  1092. }
  1093. else if( ISCPacket == 2)
  1094. {
  1095. int i;
  1096. // NRF_LOG_RAW_INFO("================= Emergency\n");
  1097. for( i=0; i<6; i++)
  1098. {
  1099. NRF_LOG_RAW_INFO("%02x ", p_adv_report->peer_addr.addr[i]);
  1100. }
  1101. NRF_LOG_RAW_INFO("\n");
  1102. ISCPacket = 0;
  1103. }else{
  1104. if( SystemManager.DevType == DEV_IOS_PARK )
  1105. {
  1106. NRF_LOG_RAW_INFO("================= One Pass Connect\n");
  1107. goto BAND_CONNECT;
  1108. }
  1109. SCAN_GOING:
  1110. scan_evt.scan_evt_id = NRF_BLE_SCAN_EVT_NOT_FOUND;
  1111. scan_evt.params.p_not_found = p_adv_report;
  1112. // If the event handler is not NULL, notify the main application.
  1113. if (p_scan_ctx->evt_handler != NULL)
  1114. {
  1115. //p_scan_ctx->evt_handler(&scan_evt);
  1116. }
  1117. // Resume the scanning.
  1118. UNUSED_RETURN_VALUE(sd_ble_gap_scan_start(NULL, &p_scan_ctx->scan_buffer));
  1119. ISCPacket = 0;
  1120. return;
  1121. }
  1122. BAND_CONNECT:
  1123. // If the whitelist is used, do not check the filters and return.
  1124. if (is_whitelist_used(p_scan_ctx))
  1125. {
  1126. scan_evt.scan_evt_id = NRF_BLE_SCAN_EVT_WHITELIST_ADV_REPORT;
  1127. scan_evt.params.p_not_found = p_adv_report;
  1128. p_scan_ctx->evt_handler(&scan_evt);
  1129. UNUSED_RETURN_VALUE(sd_ble_gap_scan_start(NULL, &p_scan_ctx->scan_buffer));
  1130. nrf_ble_scan_connect_with_target(p_scan_ctx, p_adv_report);
  1131. return;
  1132. }
  1133. #if (NRF_BLE_SCAN_FILTER_ENABLE == 1)
  1134. bool const all_filter_mode = p_scan_ctx->scan_filters.all_filters_mode;
  1135. bool is_filter_matched = false;
  1136. #if (NRF_BLE_SCAN_ADDRESS_CNT > 0)
  1137. bool const addr_filter_enabled = p_scan_ctx->scan_filters.addr_filter.addr_filter_enabled;
  1138. #endif
  1139. #if (NRF_BLE_SCAN_NAME_CNT > 0)
  1140. bool const name_filter_enabled = p_scan_ctx->scan_filters.name_filter.name_filter_enabled;
  1141. #endif
  1142. #if (NRF_BLE_SCAN_SHORT_NAME_CNT > 0)
  1143. bool const short_name_filter_enabled =
  1144. p_scan_ctx->scan_filters.short_name_filter.short_name_filter_enabled;
  1145. #endif
  1146. #if (NRF_BLE_SCAN_UUID_CNT > 0)
  1147. bool const uuid_filter_enabled = p_scan_ctx->scan_filters.uuid_filter.uuid_filter_enabled;
  1148. #endif
  1149. #if (NRF_BLE_SCAN_APPEARANCE_CNT > 0)
  1150. bool const appearance_filter_enabled =
  1151. p_scan_ctx->scan_filters.appearance_filter.appearance_filter_enabled;
  1152. #endif
  1153. #if (NRF_BLE_SCAN_ADDRESS_CNT > 0)
  1154. // Check the address filter.
  1155. if (addr_filter_enabled)
  1156. {
  1157. // Number of active filters.
  1158. filter_cnt++;
  1159. if (adv_addr_compare(p_adv_report, p_scan_ctx))
  1160. {
  1161. // Number of filters matched.
  1162. filter_match_cnt++;
  1163. // Information about the filters matched.
  1164. scan_evt.params.filter_match.filter_match.address_filter_match = true;
  1165. is_filter_matched = true;
  1166. }
  1167. }
  1168. #endif
  1169. #if (NRF_BLE_SCAN_NAME_CNT > 0)
  1170. // Check the name filter.
  1171. if (name_filter_enabled)
  1172. {
  1173. filter_cnt++;
  1174. if (adv_name_compare(p_adv_report, p_scan_ctx))
  1175. {
  1176. filter_match_cnt++;
  1177. // Information about the filters matched.
  1178. scan_evt.params.filter_match.filter_match.name_filter_match = true;
  1179. is_filter_matched = true;
  1180. }
  1181. }
  1182. #endif
  1183. #if (NRF_BLE_SCAN_SHORT_NAME_CNT > 0)
  1184. if (short_name_filter_enabled)
  1185. {
  1186. filter_cnt++;
  1187. if (adv_short_name_compare(p_adv_report, p_scan_ctx))
  1188. {
  1189. filter_match_cnt++;
  1190. // Information about the filters matched.
  1191. scan_evt.params.filter_match.filter_match.short_name_filter_match = true;
  1192. is_filter_matched = true;
  1193. }
  1194. }
  1195. #endif
  1196. #if (NRF_BLE_SCAN_UUID_CNT > 0)
  1197. // Check the UUID filter.
  1198. if (uuid_filter_enabled)
  1199. {
  1200. filter_cnt++;
  1201. //if (adv_uuid_compare(p_adv_report, p_scan_ctx))
  1202. {
  1203. filter_match_cnt++;
  1204. // Information about the filters matched.
  1205. scan_evt.params.filter_match.filter_match.uuid_filter_match = true;
  1206. is_filter_matched = true;
  1207. }
  1208. }
  1209. #endif
  1210. #if (NRF_BLE_SCAN_APPEARANCE_CNT > 0)
  1211. // Check the appearance filter.
  1212. if (appearance_filter_enabled)
  1213. {
  1214. filter_cnt++;
  1215. if (adv_appearance_compare(p_adv_report, p_scan_ctx))
  1216. {
  1217. filter_match_cnt++;
  1218. // Information about the filters matched.
  1219. scan_evt.params.filter_match.filter_match.appearance_filter_match = true;
  1220. is_filter_matched = true;
  1221. }
  1222. }
  1223. scan_evt.scan_evt_id = NRF_BLE_SCAN_EVT_NOT_FOUND;
  1224. #endif
  1225. scan_evt.params.filter_match.p_adv_report = p_adv_report;
  1226. // In the multifilter mode, the number of the active filters must equal the number of the filters matched to generate the notification.
  1227. if (all_filter_mode && (filter_match_cnt == filter_cnt))
  1228. {
  1229. scan_evt.scan_evt_id = NRF_BLE_SCAN_EVT_FILTER_MATCH;
  1230. nrf_ble_scan_connect_with_target(p_scan_ctx, p_adv_report);
  1231. }
  1232. // In the normal filter mode, only one filter match is needed to generate the notification to the main application.
  1233. else if ((!all_filter_mode) && is_filter_matched)
  1234. {
  1235. scan_evt.scan_evt_id = NRF_BLE_SCAN_EVT_FILTER_MATCH;
  1236. nrf_ble_scan_connect_with_target(p_scan_ctx, p_adv_report);
  1237. }
  1238. else
  1239. {
  1240. scan_evt.scan_evt_id = NRF_BLE_SCAN_EVT_NOT_FOUND;
  1241. scan_evt.params.p_not_found = p_adv_report;
  1242. }
  1243. // If the event handler is not NULL, notify the main application.
  1244. if (p_scan_ctx->evt_handler != NULL)
  1245. {
  1246. p_scan_ctx->evt_handler(&scan_evt);
  1247. }
  1248. #endif // NRF_BLE_SCAN_FILTER_ENABLE
  1249. // Resume the scanning.
  1250. UNUSED_RETURN_VALUE(sd_ble_gap_scan_start(NULL, &p_scan_ctx->scan_buffer));
  1251. }
  1252. /**@brief Function for checking whether the whitelist is used.
  1253. *
  1254. * @param[in] p_scan_ctx Scanning Module instance.
  1255. */
  1256. bool is_whitelist_used(nrf_ble_scan_t const * const p_scan_ctx)
  1257. {
  1258. if (p_scan_ctx->scan_params.filter_policy == BLE_GAP_SCAN_FP_WHITELIST ||
  1259. p_scan_ctx->scan_params.filter_policy == BLE_GAP_SCAN_FP_WHITELIST_NOT_RESOLVED_DIRECTED)
  1260. {
  1261. return true;
  1262. }
  1263. return false;
  1264. }
  1265. /**@brief Function for restoring the default scanning parameters.
  1266. *
  1267. * @param[out] p_scan_ctx Pointer to the Scanning Module instance.
  1268. */
  1269. static void nrf_ble_scan_default_param_set(nrf_ble_scan_t * const p_scan_ctx)
  1270. {
  1271. // Set the default parameters.
  1272. p_scan_ctx->scan_params.active = 1;
  1273. #if (NRF_SD_BLE_API_VERSION > 7)
  1274. p_scan_ctx->scan_params.interval_us = NRF_BLE_SCAN_SCAN_INTERVAL * UNIT_0_625_MS;
  1275. p_scan_ctx->scan_params.window_us = NRF_BLE_SCAN_SCAN_WINDOW * UNIT_0_625_MS;
  1276. #else
  1277. p_scan_ctx->scan_params.interval = NRF_BLE_SCAN_SCAN_INTERVAL;
  1278. p_scan_ctx->scan_params.window = NRF_BLE_SCAN_SCAN_WINDOW;
  1279. #endif // #if (NRF_SD_BLE_API_VERSION > 7)
  1280. p_scan_ctx->scan_params.timeout = NRF_BLE_SCAN_SCAN_DURATION;
  1281. p_scan_ctx->scan_params.filter_policy = BLE_GAP_SCAN_FP_ACCEPT_ALL;
  1282. p_scan_ctx->scan_params.scan_phys = BLE_GAP_PHY_1MBPS;
  1283. }
  1284. /**@brief Function for setting the default connection parameters.
  1285. *
  1286. * @param[out] p_scan_ctx Pointer to the Scanning Module instance.
  1287. */
  1288. static void nrf_ble_scan_default_conn_param_set(nrf_ble_scan_t * const p_scan_ctx)
  1289. {
  1290. p_scan_ctx->conn_params.conn_sup_timeout =
  1291. (uint16_t)MSEC_TO_UNITS(NRF_BLE_SCAN_SUPERVISION_TIMEOUT, UNIT_10_MS);
  1292. p_scan_ctx->conn_params.min_conn_interval =
  1293. (uint16_t)MSEC_TO_UNITS(NRF_BLE_SCAN_MIN_CONNECTION_INTERVAL, UNIT_1_25_MS);
  1294. p_scan_ctx->conn_params.max_conn_interval =
  1295. (uint16_t)MSEC_TO_UNITS(NRF_BLE_SCAN_MAX_CONNECTION_INTERVAL, UNIT_1_25_MS);
  1296. p_scan_ctx->conn_params.slave_latency =
  1297. (uint16_t)NRF_BLE_SCAN_SLAVE_LATENCY;
  1298. }
  1299. /**@brief Function for calling the BLE_GAP_EVT_TIMEOUT event.
  1300. *
  1301. * @param[in] p_scan_ctx Pointer to the Scanning Module instance.
  1302. * @param[in] p_gap GAP event structure.
  1303. */
  1304. static void nrf_ble_scan_on_timeout(nrf_ble_scan_t const * const p_scan_ctx,
  1305. ble_gap_evt_t const * const p_gap)
  1306. {
  1307. ble_gap_evt_timeout_t const * p_timeout = &p_gap->params.timeout;
  1308. scan_evt_t scan_evt;
  1309. memset(&scan_evt, 0, sizeof(scan_evt));
  1310. if (p_timeout->src == BLE_GAP_TIMEOUT_SRC_SCAN)
  1311. {
  1312. NRF_LOG_DEBUG("BLE_GAP_SCAN_TIMEOUT");
  1313. if (p_scan_ctx->evt_handler != NULL)
  1314. {
  1315. scan_evt.scan_evt_id = NRF_BLE_SCAN_EVT_SCAN_TIMEOUT;
  1316. scan_evt.p_scan_params = &p_scan_ctx->scan_params;
  1317. scan_evt.params.timeout.src = p_timeout->src;
  1318. p_scan_ctx->evt_handler(&scan_evt);
  1319. }
  1320. }
  1321. }
  1322. /**@brief Function for stopping the scanning.
  1323. */
  1324. void nrf_ble_scan_stop(void)
  1325. {
  1326. // It is ok to ignore the function return value here, because this function can return NRF_SUCCESS or
  1327. // NRF_ERROR_INVALID_STATE, when app is not in the scanning state.
  1328. UNUSED_RETURN_VALUE(sd_ble_gap_scan_stop());
  1329. }
  1330. ret_code_t nrf_ble_scan_init(nrf_ble_scan_t * const p_scan_ctx,
  1331. nrf_ble_scan_init_t const * const p_init,
  1332. nrf_ble_scan_evt_handler_t evt_handler)
  1333. {
  1334. VERIFY_PARAM_NOT_NULL(p_scan_ctx);
  1335. p_scan_ctx->evt_handler = evt_handler;
  1336. #if (NRF_BLE_SCAN_FILTER_ENABLE == 1)
  1337. // Disable all scanning filters.
  1338. memset(&p_scan_ctx->scan_filters, 0, sizeof(p_scan_ctx->scan_filters));
  1339. #endif
  1340. // If the pointer to the initialization structure exist, use it to scan the configuration.
  1341. if (p_init != NULL)
  1342. {
  1343. p_scan_ctx->connect_if_match = p_init->connect_if_match;
  1344. p_scan_ctx->conn_cfg_tag = p_init->conn_cfg_tag;
  1345. if (p_init->p_scan_param != NULL)
  1346. {
  1347. p_scan_ctx->scan_params = *p_init->p_scan_param;
  1348. }
  1349. else
  1350. {
  1351. // Use the default static configuration.
  1352. nrf_ble_scan_default_param_set(p_scan_ctx);
  1353. }
  1354. if (p_init->p_conn_param != NULL)
  1355. {
  1356. p_scan_ctx->conn_params = *p_init->p_conn_param;
  1357. }
  1358. else
  1359. {
  1360. // Use the default static configuration.
  1361. nrf_ble_scan_default_conn_param_set(p_scan_ctx);
  1362. }
  1363. }
  1364. // If pointer is NULL, use the static default configuration.
  1365. else
  1366. {
  1367. nrf_ble_scan_default_param_set(p_scan_ctx);
  1368. nrf_ble_scan_default_conn_param_set(p_scan_ctx);
  1369. p_scan_ctx->connect_if_match = false;
  1370. }
  1371. // Assign a buffer where the advertising reports are to be stored by the SoftDevice.
  1372. p_scan_ctx->scan_buffer.p_data = p_scan_ctx->scan_buffer_data;
  1373. p_scan_ctx->scan_buffer.len = NRF_BLE_SCAN_BUFFER;
  1374. return NRF_SUCCESS;
  1375. }
  1376. ret_code_t nrf_ble_scan_start(nrf_ble_scan_t const * const p_scan_ctx)
  1377. {
  1378. VERIFY_PARAM_NOT_NULL(p_scan_ctx);
  1379. ret_code_t err_code;
  1380. scan_evt_t scan_evt;
  1381. memset(&scan_evt, 0, sizeof(scan_evt));
  1382. nrf_ble_scan_stop();
  1383. // If the whitelist is used and the event handler is not NULL, send the whitelist request to the main application.
  1384. if (is_whitelist_used(p_scan_ctx))
  1385. {
  1386. if (p_scan_ctx->evt_handler != NULL)
  1387. {
  1388. scan_evt.scan_evt_id = NRF_BLE_SCAN_EVT_WHITELIST_REQUEST;
  1389. p_scan_ctx->evt_handler(&scan_evt);
  1390. }
  1391. }
  1392. // Start the scanning.
  1393. err_code = sd_ble_gap_scan_start(&p_scan_ctx->scan_params, &p_scan_ctx->scan_buffer);
  1394. // It is okay to ignore this error, because the scan stopped earlier.
  1395. if ((err_code != NRF_ERROR_INVALID_STATE) && (err_code != NRF_SUCCESS))
  1396. {
  1397. NRF_LOG_ERROR("sd_ble_gap_scan_start returned 0x%x", err_code);
  1398. return (err_code);
  1399. }
  1400. NRF_LOG_DEBUG("Scanning");
  1401. return NRF_SUCCESS;
  1402. }
  1403. ret_code_t nrf_ble_scan_params_set(nrf_ble_scan_t * const p_scan_ctx,
  1404. ble_gap_scan_params_t const * const p_scan_param)
  1405. {
  1406. VERIFY_PARAM_NOT_NULL(p_scan_ctx);
  1407. nrf_ble_scan_stop();
  1408. if (p_scan_param != NULL)
  1409. {
  1410. // Assign new scanning parameters.
  1411. p_scan_ctx->scan_params = *p_scan_param;
  1412. }
  1413. else
  1414. {
  1415. // If NULL, use the default static configuration.
  1416. nrf_ble_scan_default_param_set(p_scan_ctx);
  1417. }
  1418. NRF_LOG_DEBUG("Scanning parameters have been changed successfully");
  1419. return NRF_SUCCESS;
  1420. }
  1421. /**@brief Function for calling the BLE_GAP_EVT_CONNECTED event.
  1422. *
  1423. * @param[in] p_scan_ctx Pointer to the Scanning Module instance.
  1424. * @param[in] p_gap_evt GAP event structure.
  1425. */
  1426. static void nrf_ble_scan_on_connected_evt(nrf_ble_scan_t const * const p_scan_ctx,
  1427. ble_gap_evt_t const * const p_gap_evt)
  1428. {
  1429. scan_evt_t scan_evt;
  1430. memset(&scan_evt, 0, sizeof(scan_evt));
  1431. scan_evt.scan_evt_id = NRF_BLE_SCAN_EVT_CONNECTED;
  1432. scan_evt.params.connected.p_connected = &p_gap_evt->params.connected;
  1433. scan_evt.params.connected.conn_handle = p_gap_evt->conn_handle;
  1434. scan_evt.p_scan_params = &p_scan_ctx->scan_params;
  1435. if (p_scan_ctx->evt_handler != NULL)
  1436. {
  1437. p_scan_ctx->evt_handler(&scan_evt);
  1438. }
  1439. }
  1440. ret_code_t nrf_ble_scan_copy_addr_to_sd_gap_addr(ble_gap_addr_t * p_gap_addr,
  1441. const uint8_t addr[BLE_GAP_ADDR_LEN])
  1442. {
  1443. uint16_t addr_type;
  1444. addr_type = nrf_ble_scan_address_type_decode(addr);
  1445. if (addr_type == BLE_ERROR_GAP_INVALID_BLE_ADDR)
  1446. {
  1447. return BLE_ERROR_GAP_INVALID_BLE_ADDR;
  1448. }
  1449. p_gap_addr->addr_type = addr_type;
  1450. for (uint8_t i = 0; i < BLE_GAP_ADDR_LEN; ++i)
  1451. {
  1452. p_gap_addr->addr[i] = addr[BLE_GAP_ADDR_LEN - (i + 1)];
  1453. }
  1454. return NRF_SUCCESS;
  1455. }
  1456. void nrf_ble_scan_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_contex)
  1457. {
  1458. nrf_ble_scan_t * p_scan_data = (nrf_ble_scan_t *)p_contex;
  1459. ble_gap_evt_adv_report_t const * p_adv_report = &p_ble_evt->evt.gap_evt.params.adv_report;
  1460. ble_gap_evt_t const * p_gap_evt = &p_ble_evt->evt.gap_evt;
  1461. switch (p_ble_evt->header.evt_id)
  1462. {
  1463. #if 1
  1464. case BLE_GAP_EVT_DISCONNECTED:
  1465. NRF_LOG_RAW_INFO("<------------------- BLE_GAP_EVT_DISCONNECTED ------------------->\n" );
  1466. break;
  1467. case BLE_GAP_EVT_CONN_PARAM_UPDATE:
  1468. NRF_LOG_RAW_INFO("BLE_GAP_EVT_CONN_PARAM_UPDATE \n" );
  1469. break;
  1470. case BLE_GAP_EVT_SEC_PARAMS_REQUEST:
  1471. NRF_LOG_RAW_INFO("BLE_GAP_EVT_SEC_PARAMS_REQUEST \n" );
  1472. break;
  1473. case BLE_GAP_EVT_SEC_INFO_REQUEST:
  1474. NRF_LOG_RAW_INFO("BLE_GAP_EVT_SEC_INFO_REQUEST \n" );
  1475. break;
  1476. case BLE_GAP_EVT_PASSKEY_DISPLAY:
  1477. NRF_LOG_RAW_INFO("BLE_GAP_EVT_PASSKEY_DISPLAY \n" );
  1478. break;
  1479. case BLE_GAP_EVT_KEY_PRESSED:
  1480. NRF_LOG_RAW_INFO("BLE_GAP_EVT_KEY_PRESSED \n" );
  1481. break;
  1482. case BLE_GAP_EVT_AUTH_KEY_REQUEST:
  1483. NRF_LOG_RAW_INFO("BLE_GAP_EVT_AUTH_KEY_REQUEST \n" );
  1484. break;
  1485. case BLE_GAP_EVT_LESC_DHKEY_REQUEST:
  1486. NRF_LOG_RAW_INFO("BLE_GAP_EVT_LESC_DHKEY_REQUEST \n" );
  1487. break;
  1488. case BLE_GAP_EVT_AUTH_STATUS:
  1489. NRF_LOG_RAW_INFO("BLE_GAP_EVT_AUTH_STATUS \n" );
  1490. break;
  1491. case BLE_GAP_EVT_CONN_SEC_UPDATE:
  1492. NRF_LOG_RAW_INFO("BLE_GAP_EVT_CONN_SEC_UPDATE \n" );
  1493. break;
  1494. case BLE_GAP_EVT_RSSI_CHANGED:
  1495. NRF_LOG_RAW_INFO("BLE_GAP_EVT_RSSI_CHANGED \n" );
  1496. break;
  1497. case BLE_GAP_EVT_SEC_REQUEST:
  1498. NRF_LOG_RAW_INFO("BLE_GAP_EVT_SEC_REQUEST \n" );
  1499. break;
  1500. case BLE_GAP_EVT_CONN_PARAM_UPDATE_REQUEST:
  1501. NRF_LOG_RAW_INFO("BLE_GAP_EVT_CONN_PARAM_UPDATE_REQUEST \n" );
  1502. break;
  1503. case BLE_GAP_EVT_PHY_UPDATE_REQUEST:
  1504. NRF_LOG_RAW_INFO("BLE_GAP_EVT_PHY_UPDATE_REQUEST \n" );
  1505. break;
  1506. case BLE_GAP_EVT_PHY_UPDATE:
  1507. NRF_LOG_RAW_INFO("BLE_GAP_EVT_PHY_UPDATE \n" );
  1508. break;
  1509. case BLE_GAP_EVT_DATA_LENGTH_UPDATE_REQUEST:
  1510. NRF_LOG_RAW_INFO("BLE_GAP_EVT_DATA_LENGTH_UPDATE_REQUEST \n" );
  1511. break;
  1512. case BLE_GAP_EVT_DATA_LENGTH_UPDATE:
  1513. NRF_LOG_RAW_INFO("BLE_GAP_EVT_DATA_LENGTH_UPDATE \n" );
  1514. break;
  1515. case BLE_GAP_EVT_QOS_CHANNEL_SURVEY_REPORT:
  1516. NRF_LOG_RAW_INFO("BLE_GAP_EVT_QOS_CHANNEL_SURVEY_REPORT \n" );
  1517. break;
  1518. case BLE_GAP_EVT_ADV_SET_TERMINATED:
  1519. NRF_LOG_RAW_INFO("BLE_GAP_EVT_ADV_SET_TERMINATED \n" );
  1520. break;
  1521. #endif
  1522. case BLE_GAP_EVT_SCAN_REQ_REPORT:
  1523. NRF_LOG_RAW_INFO("BLE_GAP_EVT_SCAN_REQ_REPORT \n" );
  1524. break;
  1525. case BLE_GAP_EVT_ADV_REPORT:
  1526. //NRF_LOG_RAW_INFO("===BLE_GAP_EVT_ADV_REPORT \n" );
  1527. nrf_ble_scan_on_adv_report(p_scan_data, p_adv_report);
  1528. break;
  1529. case BLE_GAP_EVT_TIMEOUT:
  1530. NRF_LOG_RAW_INFO("BLE_GAP_EVT_TIMEOUT \n" );
  1531. nrf_ble_scan_on_timeout(p_scan_data, p_gap_evt);
  1532. break;
  1533. case BLE_GAP_EVT_CONNECTED:
  1534. NRF_LOG_RAW_INFO("<--------------------- BLE_GAP_EVT_CONNECTED ---------------------> \n" );
  1535. nrf_ble_scan_on_connected_evt(p_scan_data, p_gap_evt);
  1536. break;
  1537. default:
  1538. break;
  1539. }
  1540. }
  1541. #endif // NRF_BLE_SCAN_ENABLED