nrf_ble_scan(1684).c 55 KB

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