nrf_ble_scan.c 53 KB

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