nrf_ble_scan(6747).c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926
  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. int HS_AdParser(char* data)
  709. {
  710. int len;
  711. int j;
  712. if( *(data+1) == 0xff )
  713. return 0;
  714. len = *data;
  715. //NRF_LOG_RAW_INFO("Len:%d ", len );
  716. data++;
  717. //NRF_LOG_RAW_INFO("Flag:0x%02x \n", *data );
  718. SystemManager.PtrLocalNameLen = len;
  719. ISCPacket = 0;
  720. #if 1
  721. if( *data == 9)
  722. {
  723. SystemManager.PtrLocalName = data+1;
  724. if( strncmp("ICSCAR", data+1, 6) == 0 )
  725. {
  726. NRF_LOG_RAW_INFO("ICSCAR\n");
  727. SystemManager.TmpPtr = data+7;
  728. if( memcmp( SystemManager.TmpPtr, new_ble_addr.addr, 6) == 0 )
  729. {
  730. SystemManager.DevType = DEV_BAND_PARK;
  731. ISCPacket = 5;
  732. }
  733. SystemManager.BandMode = AES_ENC_CAR;
  734. }
  735. else if( strncmp("ICSEMG", data+1, 6) == 0 )
  736. {
  737. NRF_LOG_RAW_INFO("ICSEMG\n");
  738. SystemManager.TmpPtr = data+7;
  739. SystemManager.BandMode = AES_ENC_EMG;
  740. if( memcmp( SystemManager.TmpPtr, new_ble_addr.addr, 6) == 0 )
  741. {
  742. SystemManager.DevType = DEV_EMG_BAND;
  743. NRF_LOG_RAW_INFO("==> DEV_EMG_BAND\n");
  744. ISCPacket = 5;
  745. }
  746. }
  747. else if( strncmp("ICSB", data+1, 4) == 0 )
  748. {
  749. if( SystemManager.LedBlinkTimer < 0 )
  750. {
  751. ISCPacket = 1;
  752. //SystemTimer.TMR_CON_START = 0;
  753. SystemManager.DevType = DEV_BAND_ADV;
  754. }
  755. }else if( strncmp("ICSWES", data+1, 6) == 0 )
  756. {
  757. int i;
  758. char* pData = data;
  759. for(i=0; i<13; i++)
  760. NRF_LOG_RAW_INFO("%02X, ", pData[i]);
  761. NRF_LOG_RAW_INFO("\n");
  762. if( SystemManager.LedBlinkTimer < 0 )
  763. {
  764. // ISCPacket = 1;
  765. //SystemTimer.TMR_CON_START = 0;
  766. // SystemManager.DevType = DEV_WES_ADV;
  767. }
  768. ISCPacket = 5;
  769. SystemManager.DevType = 0;
  770. NRF_LOG_RAW_INFO("==> ICSWES\n");
  771. if( SystemManager.RegRequst == 1 ) // 등록모드면
  772. {
  773. ISCPacket = 1;
  774. SystemManager.DevType = DEV_WES_ADV;
  775. }
  776. SystemManager.TmpPtr = data+7;
  777. if( memcmp( SystemManager.TmpPtr, new_ble_addr.addr, 6) == 0 )
  778. {
  779. for(i=0; i<13; i++)
  780. NRF_LOG_RAW_INFO("%02X, ", pData[i]);
  781. NRF_LOG_RAW_INFO("\n");
  782. SystemManager.DevType = DEV_WES_ADV;
  783. ISCPacket = 1;
  784. NRF_LOG_RAW_INFO("==> ICSWES_MAC\n");
  785. }
  786. #if 0
  787. if( FindRegMac(ScanHSBuff) == 1 )
  788. {
  789. for(i=0; i<13; i++)
  790. NRF_LOG_RAW_INFO("%02X, ", pData[i]);
  791. NRF_LOG_RAW_INFO("\n");
  792. SystemManager.DevType = DEV_WES_ADV;
  793. ISCPacket = 1;
  794. NRF_LOG_RAW_INFO("==> ICSWES_MAC\n");
  795. }else{
  796. ///SystemManager.DevType = 0;
  797. }
  798. #endif
  799. SystemManager.BandMode = AES_ENC_WES;
  800. }else if( strncmp("ICSPCA", data+1, 6) == 0 )
  801. {
  802. int i;
  803. char* pData = data;
  804. for(i=0; i<17; i++)
  805. NRF_LOG_RAW_INFO("%02X, ", pData[i]);
  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. else if( strncmp("ICSDEVREG", data+1, 9) == 0 )
  829. {
  830. //return 0;
  831. ISCPacket = 2;
  832. }else if( strncmp("ICSDEVDEL", data+1, 9) == 0 )
  833. {
  834. //return 0;
  835. ISCPacket = 3;
  836. }else if( strncmp("ICSDEVDSP", data+1, 9) == 0 )
  837. {
  838. //return 0;
  839. ISCPacket = 4;
  840. }
  841. */
  842. #if 0
  843. if( ISCPacket > 0 )
  844. {
  845. for(j=1; j<len; j++)
  846. {
  847. data++;
  848. if( (*(data)>= 0x41&& *(data)<=0x5a) || (*(data)>= 0x61&& *(data)<=0x7a) )
  849. {
  850. NRF_LOG_RAW_INFO("%c ", *(data) );
  851. }else{
  852. NRF_LOG_RAW_INFO("0x%02x ", *(data) );
  853. }
  854. }
  855. NRF_LOG_RAW_INFO("\n") ;
  856. }
  857. #endif
  858. }
  859. #endif
  860. return 0;
  861. }
  862. void HS_ParseAdvPacket(char* data, int dataLen, int flag)
  863. {
  864. int len, validLen;
  865. char* pData;
  866. int i;
  867. int j;
  868. int v=0;
  869. int result;
  870. pData = data;
  871. validLen = dataLen;
  872. #if 1
  873. if( flag )
  874. {
  875. // NRF_LOG_RAW_INFO("MEM : %08x ==> ", pData) ;
  876. while(1)
  877. {
  878. result = HS_AdParser(pData);
  879. // if( result == 0 )
  880. // break;
  881. len = *pData;
  882. validLen -= len+1;
  883. //printf("len:%d validLen:%d\n", len, validLen);
  884. if( validLen <= 0 )
  885. break;
  886. pData += len+1;
  887. }
  888. }
  889. #endif
  890. }
  891. /**@brief Function for calling the BLE_GAP_EVT_ADV_REPORT event to check whether the received
  892. * scanning data matches the scan configuration.
  893. *
  894. * @param[in] p_scan_ctx Pointer to the Scanning Module instance.
  895. * @param[in] p_adv_report Advertising report.
  896. */
  897. // jakuja
  898. static void nrf_ble_scan_on_adv_report(nrf_ble_scan_t const * const p_scan_ctx,
  899. ble_gap_evt_adv_report_t const * const p_adv_report)
  900. {
  901. scan_evt_t scan_evt;
  902. int i;
  903. uint8_t *pdata;
  904. uint8_t addr[6];
  905. #if (NRF_BLE_SCAN_FILTER_ENABLE == 1)
  906. uint8_t filter_cnt = 0;
  907. uint8_t filter_match_cnt = 0;
  908. #endif
  909. memset(&scan_evt, 0, sizeof(scan_evt));
  910. scan_evt.p_scan_params = &p_scan_ctx->scan_params;
  911. pdata = p_adv_report->data.p_data;
  912. pAdvRep = p_adv_report->data.p_data;;
  913. #if 0 // jakuja
  914. for(i=0; i<p_adv_report->data.len; i++)
  915. NRF_LOG_RAW_INFO("%02X, ", p_adv_report->data.p_data[i]);
  916. NRF_LOG_RAW_INFO("\n");
  917. #endif
  918. // ONE PASS APP (안드로이드)
  919. 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){
  920. AppParkingCheck(p_adv_report->data.p_data);
  921. }
  922. // ONE PASS APP (IOS)
  923. 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){
  924. NRF_LOG_RAW_INFO("IOS APP PARKING\n");
  925. AppParkingCheck_IOS(p_adv_report->data.p_data);
  926. }
  927. HS_ParseAdvPacket(p_adv_report->data.p_data, p_adv_report->data.len, 1);
  928. if( SystemManager.DevType == DEV_NONE )
  929. goto SCAN_GOING;
  930. sprintf(ScanHSBuff, "%02x%02x%02x%02x%02x%02x", p_adv_report->peer_addr.addr[0],
  931. p_adv_report->peer_addr.addr[1],
  932. p_adv_report->peer_addr.addr[2],
  933. p_adv_report->peer_addr.addr[3],
  934. p_adv_report->peer_addr.addr[4],
  935. p_adv_report->peer_addr.addr[5]);
  936. ScanHSBuff[12] = 0;
  937. #if 1
  938. SystemManager.PeerAddr[0] = p_adv_report->peer_addr.addr[0];
  939. SystemManager.PeerAddr[1] = p_adv_report->peer_addr.addr[1];
  940. SystemManager.PeerAddr[2] = p_adv_report->peer_addr.addr[2];
  941. SystemManager.PeerAddr[3] = p_adv_report->peer_addr.addr[3];
  942. SystemManager.PeerAddr[4] = p_adv_report->peer_addr.addr[4];
  943. SystemManager.PeerAddr[5] = p_adv_report->peer_addr.addr[5];
  944. #endif
  945. SystemManager.PtrPeerAddrStr = ScanHSBuff;
  946. if( SystemManager.DevType == DEV_BAND_PARK )
  947. {
  948. NRF_LOG_RAW_INFO("================= Send Park\n");
  949. if( SystemManager.LedBlinkTimer && SystemTimer.PARK_KEEP_TIMER > 6000)
  950. {
  951. SystemManager.LedBlinkRun = 1;
  952. SystemManager.LedBlinkTimer = 3000;
  953. SystemTimer.LED_TOGGLE_TIMER = 0;
  954. SystemTimer.PARK_KEEP_TIMER = 0;
  955. SystemManager.DevType = 0;
  956. ISCPacket = 0;
  957. NRF_LOG_RAW_INFO("name len is %d ", SystemManager.PtrLocalNameLen);
  958. for( i=0; i<SystemManager.PtrLocalNameLen; i++)
  959. {
  960. NRF_LOG_RAW_INFO("%02x ", *(SystemManager.PtrLocalName+i) );
  961. }
  962. NRF_LOG_RAW_INFO("\n");
  963. }
  964. // goto SCAN_GOING;
  965. goto BAND_CONNECT;
  966. }
  967. else if( SystemManager.DevType == DEV_EMG_BUTTON )
  968. {
  969. NRF_LOG_RAW_INFO("================= DEV_EMG_BUTTON\n");
  970. ISCPacket = 1;
  971. //if( SystemManager.LedBlinkTimer < 0 )
  972. {
  973. SystemManager.LedBlinkRun = 1;
  974. SystemManager.LedBlinkTimer = 5000;
  975. SystemTimer.LED_TOGGLE_TIMER = 0;
  976. SystemManager.DevType = 0;
  977. //ISCPacket = 0;
  978. }
  979. //return;
  980. }
  981. else if( SystemManager.DevType == DEV_EMG_BAND )
  982. {
  983. NRF_LOG_RAW_INFO("================= DEV_EMG_BAND\n");
  984. ISCPacket = 2;
  985. //if( SystemManager.LedBlinkTimer < 0 )
  986. {
  987. //SystemManager.LedBlinkRun = 1;
  988. //SystemManager.LedBlinkTimer = 5000;
  989. //SystemTimer.LED_TOGGLE_TIMER = 0;
  990. //SystemManager.DevType = 0;
  991. }
  992. //return;
  993. }else if( SystemManager.DevType == DEV_WES_ADV )
  994. {
  995. NRF_LOG_RAW_INFO("================= DEV_WES_ADV\n");
  996. ISCPacket = 1;
  997. //if( SystemManager.LedBlinkTimer < 0 )
  998. {
  999. //SystemManager.LedBlinkRun = 1;
  1000. //SystemManager.LedBlinkTimer = 5000;
  1001. //SystemTimer.LED_TOGGLE_TIMER = 0;
  1002. //SystemManager.DevType = 0;
  1003. }
  1004. //return;
  1005. }else if( SystemManager.DevType == DEV_IOS_PARK )
  1006. {
  1007. NRF_LOG_RAW_INFO("================= DEV_IOS_PARK\n");
  1008. ISCPacket = 5;
  1009. //if( SystemManager.LedBlinkTimer < 0 )
  1010. {
  1011. //SystemManager.LedBlinkRun = 1;
  1012. //SystemManager.LedBlinkTimer = 5000;
  1013. //SystemTimer.LED_TOGGLE_TIMER = 0;
  1014. //SystemManager.DevType = 0;
  1015. }
  1016. //return;
  1017. }
  1018. if( ISCPacket == 1)
  1019. {
  1020. int i;
  1021. if( FindRegMac(ScanHSBuff) == 0 )
  1022. {
  1023. scan_evt.scan_evt_id = NRF_BLE_SCAN_EVT_NOT_FOUND;
  1024. scan_evt.params.p_not_found = p_adv_report;
  1025. // If the event handler is not NULL, notify the main application.
  1026. if (p_scan_ctx->evt_handler != NULL)
  1027. {
  1028. //p_scan_ctx->evt_handler(&scan_evt);
  1029. }
  1030. // Resume the scanning.
  1031. UNUSED_RETURN_VALUE(sd_ble_gap_scan_start(NULL, &p_scan_ctx->scan_buffer));
  1032. ISCPacket = 0;
  1033. if( SystemManager.RegRequst == 0 ) // 등록모드가 아니면
  1034. return;
  1035. }
  1036. //NRF_LOG_RAW_INFO("================= Emergency\n");
  1037. for( i=0; i<6; i++)
  1038. {
  1039. NRF_LOG_RAW_INFO("%02x ", p_adv_report->peer_addr.addr[i]);
  1040. }
  1041. NRF_LOG_RAW_INFO("\n");
  1042. ISCPacket = 0;
  1043. }
  1044. else if( ISCPacket == 2)
  1045. {
  1046. int i;
  1047. // NRF_LOG_RAW_INFO("================= Emergency\n");
  1048. for( i=0; i<6; i++)
  1049. {
  1050. NRF_LOG_RAW_INFO("%02x ", p_adv_report->peer_addr.addr[i]);
  1051. }
  1052. NRF_LOG_RAW_INFO("\n");
  1053. ISCPacket = 0;
  1054. }else{
  1055. if( SystemManager.DevType == DEV_IOS_PARK )
  1056. {
  1057. NRF_LOG_RAW_INFO("================= One Pass Connect\n");
  1058. goto BAND_CONNECT;
  1059. }
  1060. SCAN_GOING:
  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. return;
  1072. }
  1073. BAND_CONNECT:
  1074. // If the whitelist is used, do not check the filters and return.
  1075. if (is_whitelist_used(p_scan_ctx))
  1076. {
  1077. scan_evt.scan_evt_id = NRF_BLE_SCAN_EVT_WHITELIST_ADV_REPORT;
  1078. scan_evt.params.p_not_found = p_adv_report;
  1079. p_scan_ctx->evt_handler(&scan_evt);
  1080. UNUSED_RETURN_VALUE(sd_ble_gap_scan_start(NULL, &p_scan_ctx->scan_buffer));
  1081. nrf_ble_scan_connect_with_target(p_scan_ctx, p_adv_report);
  1082. return;
  1083. }
  1084. #if (NRF_BLE_SCAN_FILTER_ENABLE == 1)
  1085. bool const all_filter_mode = p_scan_ctx->scan_filters.all_filters_mode;
  1086. bool is_filter_matched = false;
  1087. #if (NRF_BLE_SCAN_ADDRESS_CNT > 0)
  1088. bool const addr_filter_enabled = p_scan_ctx->scan_filters.addr_filter.addr_filter_enabled;
  1089. #endif
  1090. #if (NRF_BLE_SCAN_NAME_CNT > 0)
  1091. bool const name_filter_enabled = p_scan_ctx->scan_filters.name_filter.name_filter_enabled;
  1092. #endif
  1093. #if (NRF_BLE_SCAN_SHORT_NAME_CNT > 0)
  1094. bool const short_name_filter_enabled =
  1095. p_scan_ctx->scan_filters.short_name_filter.short_name_filter_enabled;
  1096. #endif
  1097. #if (NRF_BLE_SCAN_UUID_CNT > 0)
  1098. bool const uuid_filter_enabled = p_scan_ctx->scan_filters.uuid_filter.uuid_filter_enabled;
  1099. #endif
  1100. #if (NRF_BLE_SCAN_APPEARANCE_CNT > 0)
  1101. bool const appearance_filter_enabled =
  1102. p_scan_ctx->scan_filters.appearance_filter.appearance_filter_enabled;
  1103. #endif
  1104. #if (NRF_BLE_SCAN_ADDRESS_CNT > 0)
  1105. // Check the address filter.
  1106. if (addr_filter_enabled)
  1107. {
  1108. // Number of active filters.
  1109. filter_cnt++;
  1110. if (adv_addr_compare(p_adv_report, p_scan_ctx))
  1111. {
  1112. // Number of filters matched.
  1113. filter_match_cnt++;
  1114. // Information about the filters matched.
  1115. scan_evt.params.filter_match.filter_match.address_filter_match = true;
  1116. is_filter_matched = true;
  1117. }
  1118. }
  1119. #endif
  1120. #if (NRF_BLE_SCAN_NAME_CNT > 0)
  1121. // Check the name filter.
  1122. if (name_filter_enabled)
  1123. {
  1124. filter_cnt++;
  1125. if (adv_name_compare(p_adv_report, p_scan_ctx))
  1126. {
  1127. filter_match_cnt++;
  1128. // Information about the filters matched.
  1129. scan_evt.params.filter_match.filter_match.name_filter_match = true;
  1130. is_filter_matched = true;
  1131. }
  1132. }
  1133. #endif
  1134. #if (NRF_BLE_SCAN_SHORT_NAME_CNT > 0)
  1135. if (short_name_filter_enabled)
  1136. {
  1137. filter_cnt++;
  1138. if (adv_short_name_compare(p_adv_report, p_scan_ctx))
  1139. {
  1140. filter_match_cnt++;
  1141. // Information about the filters matched.
  1142. scan_evt.params.filter_match.filter_match.short_name_filter_match = true;
  1143. is_filter_matched = true;
  1144. }
  1145. }
  1146. #endif
  1147. #if (NRF_BLE_SCAN_UUID_CNT > 0)
  1148. // Check the UUID filter.
  1149. if (uuid_filter_enabled)
  1150. {
  1151. filter_cnt++;
  1152. //if (adv_uuid_compare(p_adv_report, p_scan_ctx))
  1153. {
  1154. filter_match_cnt++;
  1155. // Information about the filters matched.
  1156. scan_evt.params.filter_match.filter_match.uuid_filter_match = true;
  1157. is_filter_matched = true;
  1158. }
  1159. }
  1160. #endif
  1161. #if (NRF_BLE_SCAN_APPEARANCE_CNT > 0)
  1162. // Check the appearance filter.
  1163. if (appearance_filter_enabled)
  1164. {
  1165. filter_cnt++;
  1166. if (adv_appearance_compare(p_adv_report, p_scan_ctx))
  1167. {
  1168. filter_match_cnt++;
  1169. // Information about the filters matched.
  1170. scan_evt.params.filter_match.filter_match.appearance_filter_match = true;
  1171. is_filter_matched = true;
  1172. }
  1173. }
  1174. scan_evt.scan_evt_id = NRF_BLE_SCAN_EVT_NOT_FOUND;
  1175. #endif
  1176. scan_evt.params.filter_match.p_adv_report = p_adv_report;
  1177. // In the multifilter mode, the number of the active filters must equal the number of the filters matched to generate the notification.
  1178. if (all_filter_mode && (filter_match_cnt == filter_cnt))
  1179. {
  1180. scan_evt.scan_evt_id = NRF_BLE_SCAN_EVT_FILTER_MATCH;
  1181. nrf_ble_scan_connect_with_target(p_scan_ctx, p_adv_report);
  1182. }
  1183. // In the normal filter mode, only one filter match is needed to generate the notification to the main application.
  1184. else if ((!all_filter_mode) && is_filter_matched)
  1185. {
  1186. scan_evt.scan_evt_id = NRF_BLE_SCAN_EVT_FILTER_MATCH;
  1187. nrf_ble_scan_connect_with_target(p_scan_ctx, p_adv_report);
  1188. }
  1189. else
  1190. {
  1191. scan_evt.scan_evt_id = NRF_BLE_SCAN_EVT_NOT_FOUND;
  1192. scan_evt.params.p_not_found = p_adv_report;
  1193. }
  1194. // If the event handler is not NULL, notify the main application.
  1195. if (p_scan_ctx->evt_handler != NULL)
  1196. {
  1197. p_scan_ctx->evt_handler(&scan_evt);
  1198. }
  1199. #endif // NRF_BLE_SCAN_FILTER_ENABLE
  1200. // Resume the scanning.
  1201. UNUSED_RETURN_VALUE(sd_ble_gap_scan_start(NULL, &p_scan_ctx->scan_buffer));
  1202. }
  1203. /**@brief Function for checking whether the whitelist is used.
  1204. *
  1205. * @param[in] p_scan_ctx Scanning Module instance.
  1206. */
  1207. bool is_whitelist_used(nrf_ble_scan_t const * const p_scan_ctx)
  1208. {
  1209. if (p_scan_ctx->scan_params.filter_policy == BLE_GAP_SCAN_FP_WHITELIST ||
  1210. p_scan_ctx->scan_params.filter_policy == BLE_GAP_SCAN_FP_WHITELIST_NOT_RESOLVED_DIRECTED)
  1211. {
  1212. return true;
  1213. }
  1214. return false;
  1215. }
  1216. /**@brief Function for restoring the default scanning parameters.
  1217. *
  1218. * @param[out] p_scan_ctx Pointer to the Scanning Module instance.
  1219. */
  1220. static void nrf_ble_scan_default_param_set(nrf_ble_scan_t * const p_scan_ctx)
  1221. {
  1222. // Set the default parameters.
  1223. p_scan_ctx->scan_params.active = 1;
  1224. #if (NRF_SD_BLE_API_VERSION > 7)
  1225. p_scan_ctx->scan_params.interval_us = NRF_BLE_SCAN_SCAN_INTERVAL * UNIT_0_625_MS;
  1226. p_scan_ctx->scan_params.window_us = NRF_BLE_SCAN_SCAN_WINDOW * UNIT_0_625_MS;
  1227. #else
  1228. p_scan_ctx->scan_params.interval = NRF_BLE_SCAN_SCAN_INTERVAL;
  1229. p_scan_ctx->scan_params.window = NRF_BLE_SCAN_SCAN_WINDOW;
  1230. #endif // #if (NRF_SD_BLE_API_VERSION > 7)
  1231. p_scan_ctx->scan_params.timeout = NRF_BLE_SCAN_SCAN_DURATION;
  1232. p_scan_ctx->scan_params.filter_policy = BLE_GAP_SCAN_FP_ACCEPT_ALL;
  1233. p_scan_ctx->scan_params.scan_phys = BLE_GAP_PHY_1MBPS;
  1234. }
  1235. /**@brief Function for setting the default connection parameters.
  1236. *
  1237. * @param[out] p_scan_ctx Pointer to the Scanning Module instance.
  1238. */
  1239. static void nrf_ble_scan_default_conn_param_set(nrf_ble_scan_t * const p_scan_ctx)
  1240. {
  1241. p_scan_ctx->conn_params.conn_sup_timeout =
  1242. (uint16_t)MSEC_TO_UNITS(NRF_BLE_SCAN_SUPERVISION_TIMEOUT, UNIT_10_MS);
  1243. p_scan_ctx->conn_params.min_conn_interval =
  1244. (uint16_t)MSEC_TO_UNITS(NRF_BLE_SCAN_MIN_CONNECTION_INTERVAL, UNIT_1_25_MS);
  1245. p_scan_ctx->conn_params.max_conn_interval =
  1246. (uint16_t)MSEC_TO_UNITS(NRF_BLE_SCAN_MAX_CONNECTION_INTERVAL, UNIT_1_25_MS);
  1247. p_scan_ctx->conn_params.slave_latency =
  1248. (uint16_t)NRF_BLE_SCAN_SLAVE_LATENCY;
  1249. }
  1250. /**@brief Function for calling the BLE_GAP_EVT_TIMEOUT event.
  1251. *
  1252. * @param[in] p_scan_ctx Pointer to the Scanning Module instance.
  1253. * @param[in] p_gap GAP event structure.
  1254. */
  1255. static void nrf_ble_scan_on_timeout(nrf_ble_scan_t const * const p_scan_ctx,
  1256. ble_gap_evt_t const * const p_gap)
  1257. {
  1258. ble_gap_evt_timeout_t const * p_timeout = &p_gap->params.timeout;
  1259. scan_evt_t scan_evt;
  1260. memset(&scan_evt, 0, sizeof(scan_evt));
  1261. if (p_timeout->src == BLE_GAP_TIMEOUT_SRC_SCAN)
  1262. {
  1263. NRF_LOG_DEBUG("BLE_GAP_SCAN_TIMEOUT");
  1264. if (p_scan_ctx->evt_handler != NULL)
  1265. {
  1266. scan_evt.scan_evt_id = NRF_BLE_SCAN_EVT_SCAN_TIMEOUT;
  1267. scan_evt.p_scan_params = &p_scan_ctx->scan_params;
  1268. scan_evt.params.timeout.src = p_timeout->src;
  1269. p_scan_ctx->evt_handler(&scan_evt);
  1270. }
  1271. }
  1272. }
  1273. /**@brief Function for stopping the scanning.
  1274. */
  1275. void nrf_ble_scan_stop(void)
  1276. {
  1277. // It is ok to ignore the function return value here, because this function can return NRF_SUCCESS or
  1278. // NRF_ERROR_INVALID_STATE, when app is not in the scanning state.
  1279. UNUSED_RETURN_VALUE(sd_ble_gap_scan_stop());
  1280. }
  1281. ret_code_t nrf_ble_scan_init(nrf_ble_scan_t * const p_scan_ctx,
  1282. nrf_ble_scan_init_t const * const p_init,
  1283. nrf_ble_scan_evt_handler_t evt_handler)
  1284. {
  1285. VERIFY_PARAM_NOT_NULL(p_scan_ctx);
  1286. p_scan_ctx->evt_handler = evt_handler;
  1287. #if (NRF_BLE_SCAN_FILTER_ENABLE == 1)
  1288. // Disable all scanning filters.
  1289. memset(&p_scan_ctx->scan_filters, 0, sizeof(p_scan_ctx->scan_filters));
  1290. #endif
  1291. // If the pointer to the initialization structure exist, use it to scan the configuration.
  1292. if (p_init != NULL)
  1293. {
  1294. p_scan_ctx->connect_if_match = p_init->connect_if_match;
  1295. p_scan_ctx->conn_cfg_tag = p_init->conn_cfg_tag;
  1296. if (p_init->p_scan_param != NULL)
  1297. {
  1298. p_scan_ctx->scan_params = *p_init->p_scan_param;
  1299. }
  1300. else
  1301. {
  1302. // Use the default static configuration.
  1303. nrf_ble_scan_default_param_set(p_scan_ctx);
  1304. }
  1305. if (p_init->p_conn_param != NULL)
  1306. {
  1307. p_scan_ctx->conn_params = *p_init->p_conn_param;
  1308. }
  1309. else
  1310. {
  1311. // Use the default static configuration.
  1312. nrf_ble_scan_default_conn_param_set(p_scan_ctx);
  1313. }
  1314. }
  1315. // If pointer is NULL, use the static default configuration.
  1316. else
  1317. {
  1318. nrf_ble_scan_default_param_set(p_scan_ctx);
  1319. nrf_ble_scan_default_conn_param_set(p_scan_ctx);
  1320. p_scan_ctx->connect_if_match = false;
  1321. }
  1322. // Assign a buffer where the advertising reports are to be stored by the SoftDevice.
  1323. p_scan_ctx->scan_buffer.p_data = p_scan_ctx->scan_buffer_data;
  1324. p_scan_ctx->scan_buffer.len = NRF_BLE_SCAN_BUFFER;
  1325. return NRF_SUCCESS;
  1326. }
  1327. ret_code_t nrf_ble_scan_start(nrf_ble_scan_t const * const p_scan_ctx)
  1328. {
  1329. VERIFY_PARAM_NOT_NULL(p_scan_ctx);
  1330. ret_code_t err_code;
  1331. scan_evt_t scan_evt;
  1332. memset(&scan_evt, 0, sizeof(scan_evt));
  1333. nrf_ble_scan_stop();
  1334. // If the whitelist is used and the event handler is not NULL, send the whitelist request to the main application.
  1335. if (is_whitelist_used(p_scan_ctx))
  1336. {
  1337. if (p_scan_ctx->evt_handler != NULL)
  1338. {
  1339. scan_evt.scan_evt_id = NRF_BLE_SCAN_EVT_WHITELIST_REQUEST;
  1340. p_scan_ctx->evt_handler(&scan_evt);
  1341. }
  1342. }
  1343. // Start the scanning.
  1344. err_code = sd_ble_gap_scan_start(&p_scan_ctx->scan_params, &p_scan_ctx->scan_buffer);
  1345. // It is okay to ignore this error, because the scan stopped earlier.
  1346. if ((err_code != NRF_ERROR_INVALID_STATE) && (err_code != NRF_SUCCESS))
  1347. {
  1348. NRF_LOG_ERROR("sd_ble_gap_scan_start returned 0x%x", err_code);
  1349. return (err_code);
  1350. }
  1351. NRF_LOG_DEBUG("Scanning");
  1352. return NRF_SUCCESS;
  1353. }
  1354. ret_code_t nrf_ble_scan_params_set(nrf_ble_scan_t * const p_scan_ctx,
  1355. ble_gap_scan_params_t const * const p_scan_param)
  1356. {
  1357. VERIFY_PARAM_NOT_NULL(p_scan_ctx);
  1358. nrf_ble_scan_stop();
  1359. if (p_scan_param != NULL)
  1360. {
  1361. // Assign new scanning parameters.
  1362. p_scan_ctx->scan_params = *p_scan_param;
  1363. }
  1364. else
  1365. {
  1366. // If NULL, use the default static configuration.
  1367. nrf_ble_scan_default_param_set(p_scan_ctx);
  1368. }
  1369. NRF_LOG_DEBUG("Scanning parameters have been changed successfully");
  1370. return NRF_SUCCESS;
  1371. }
  1372. /**@brief Function for calling the BLE_GAP_EVT_CONNECTED event.
  1373. *
  1374. * @param[in] p_scan_ctx Pointer to the Scanning Module instance.
  1375. * @param[in] p_gap_evt GAP event structure.
  1376. */
  1377. static void nrf_ble_scan_on_connected_evt(nrf_ble_scan_t const * const p_scan_ctx,
  1378. ble_gap_evt_t const * const p_gap_evt)
  1379. {
  1380. scan_evt_t scan_evt;
  1381. memset(&scan_evt, 0, sizeof(scan_evt));
  1382. scan_evt.scan_evt_id = NRF_BLE_SCAN_EVT_CONNECTED;
  1383. scan_evt.params.connected.p_connected = &p_gap_evt->params.connected;
  1384. scan_evt.params.connected.conn_handle = p_gap_evt->conn_handle;
  1385. scan_evt.p_scan_params = &p_scan_ctx->scan_params;
  1386. if (p_scan_ctx->evt_handler != NULL)
  1387. {
  1388. p_scan_ctx->evt_handler(&scan_evt);
  1389. }
  1390. }
  1391. ret_code_t nrf_ble_scan_copy_addr_to_sd_gap_addr(ble_gap_addr_t * p_gap_addr,
  1392. const uint8_t addr[BLE_GAP_ADDR_LEN])
  1393. {
  1394. uint16_t addr_type;
  1395. addr_type = nrf_ble_scan_address_type_decode(addr);
  1396. if (addr_type == BLE_ERROR_GAP_INVALID_BLE_ADDR)
  1397. {
  1398. return BLE_ERROR_GAP_INVALID_BLE_ADDR;
  1399. }
  1400. p_gap_addr->addr_type = addr_type;
  1401. for (uint8_t i = 0; i < BLE_GAP_ADDR_LEN; ++i)
  1402. {
  1403. p_gap_addr->addr[i] = addr[BLE_GAP_ADDR_LEN - (i + 1)];
  1404. }
  1405. return NRF_SUCCESS;
  1406. }
  1407. void nrf_ble_scan_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_contex)
  1408. {
  1409. nrf_ble_scan_t * p_scan_data = (nrf_ble_scan_t *)p_contex;
  1410. ble_gap_evt_adv_report_t const * p_adv_report = &p_ble_evt->evt.gap_evt.params.adv_report;
  1411. ble_gap_evt_t const * p_gap_evt = &p_ble_evt->evt.gap_evt;
  1412. switch (p_ble_evt->header.evt_id)
  1413. {
  1414. #if 1
  1415. case BLE_GAP_EVT_DISCONNECTED:
  1416. NRF_LOG_RAW_INFO("BLE_GAP_EVT_DISCONNECTED \n" );
  1417. break;
  1418. case BLE_GAP_EVT_CONN_PARAM_UPDATE:
  1419. NRF_LOG_RAW_INFO("BLE_GAP_EVT_CONN_PARAM_UPDATE \n" );
  1420. break;
  1421. case BLE_GAP_EVT_SEC_PARAMS_REQUEST:
  1422. NRF_LOG_RAW_INFO("BLE_GAP_EVT_SEC_PARAMS_REQUEST \n" );
  1423. break;
  1424. case BLE_GAP_EVT_SEC_INFO_REQUEST:
  1425. NRF_LOG_RAW_INFO("BLE_GAP_EVT_SEC_INFO_REQUEST \n" );
  1426. break;
  1427. case BLE_GAP_EVT_PASSKEY_DISPLAY:
  1428. NRF_LOG_RAW_INFO("BLE_GAP_EVT_PASSKEY_DISPLAY \n" );
  1429. break;
  1430. case BLE_GAP_EVT_KEY_PRESSED:
  1431. NRF_LOG_RAW_INFO("BLE_GAP_EVT_KEY_PRESSED \n" );
  1432. break;
  1433. case BLE_GAP_EVT_AUTH_KEY_REQUEST:
  1434. NRF_LOG_RAW_INFO("BLE_GAP_EVT_AUTH_KEY_REQUEST \n" );
  1435. break;
  1436. case BLE_GAP_EVT_LESC_DHKEY_REQUEST:
  1437. NRF_LOG_RAW_INFO("BLE_GAP_EVT_LESC_DHKEY_REQUEST \n" );
  1438. break;
  1439. case BLE_GAP_EVT_AUTH_STATUS:
  1440. NRF_LOG_RAW_INFO("BLE_GAP_EVT_AUTH_STATUS \n" );
  1441. break;
  1442. case BLE_GAP_EVT_CONN_SEC_UPDATE:
  1443. NRF_LOG_RAW_INFO("BLE_GAP_EVT_CONN_SEC_UPDATE \n" );
  1444. break;
  1445. case BLE_GAP_EVT_RSSI_CHANGED:
  1446. NRF_LOG_RAW_INFO("BLE_GAP_EVT_RSSI_CHANGED \n" );
  1447. break;
  1448. case BLE_GAP_EVT_SEC_REQUEST:
  1449. NRF_LOG_RAW_INFO("BLE_GAP_EVT_SEC_REQUEST \n" );
  1450. break;
  1451. case BLE_GAP_EVT_CONN_PARAM_UPDATE_REQUEST:
  1452. NRF_LOG_RAW_INFO("BLE_GAP_EVT_CONN_PARAM_UPDATE_REQUEST \n" );
  1453. break;
  1454. case BLE_GAP_EVT_PHY_UPDATE_REQUEST:
  1455. NRF_LOG_RAW_INFO("BLE_GAP_EVT_PHY_UPDATE_REQUEST \n" );
  1456. break;
  1457. case BLE_GAP_EVT_PHY_UPDATE:
  1458. NRF_LOG_RAW_INFO("BLE_GAP_EVT_PHY_UPDATE \n" );
  1459. break;
  1460. case BLE_GAP_EVT_DATA_LENGTH_UPDATE_REQUEST:
  1461. NRF_LOG_RAW_INFO("BLE_GAP_EVT_DATA_LENGTH_UPDATE_REQUEST \n" );
  1462. break;
  1463. case BLE_GAP_EVT_DATA_LENGTH_UPDATE:
  1464. NRF_LOG_RAW_INFO("BLE_GAP_EVT_DATA_LENGTH_UPDATE \n" );
  1465. break;
  1466. case BLE_GAP_EVT_QOS_CHANNEL_SURVEY_REPORT:
  1467. NRF_LOG_RAW_INFO("BLE_GAP_EVT_QOS_CHANNEL_SURVEY_REPORT \n" );
  1468. break;
  1469. case BLE_GAP_EVT_ADV_SET_TERMINATED:
  1470. NRF_LOG_RAW_INFO("BLE_GAP_EVT_ADV_SET_TERMINATED \n" );
  1471. break;
  1472. #endif
  1473. case BLE_GAP_EVT_SCAN_REQ_REPORT:
  1474. NRF_LOG_RAW_INFO("BLE_GAP_EVT_SCAN_REQ_REPORT \n" );
  1475. break;
  1476. case BLE_GAP_EVT_ADV_REPORT:
  1477. //NRF_LOG_RAW_INFO("===BLE_GAP_EVT_ADV_REPORT \n" );
  1478. nrf_ble_scan_on_adv_report(p_scan_data, p_adv_report);
  1479. break;
  1480. case BLE_GAP_EVT_TIMEOUT:
  1481. nrf_ble_scan_on_timeout(p_scan_data, p_gap_evt);
  1482. break;
  1483. case BLE_GAP_EVT_CONNECTED:
  1484. NRF_LOG_RAW_INFO("BLE_GAP_EVT_CONNECTED \n" );
  1485. nrf_ble_scan_on_connected_evt(p_scan_data, p_gap_evt);
  1486. break;
  1487. default:
  1488. break;
  1489. }
  1490. }
  1491. #endif // NRF_BLE_SCAN_ENABLED