ble_advertising.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  1. /**
  2. * Copyright (c) 2015 - 2019, 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(BLE_ADVERTISING)
  42. #include "ble_advdata.h"
  43. #include "ble_advertising.h"
  44. #include "nrf_soc.h"
  45. #include "nrf_log.h"
  46. #include "sdk_errors.h"
  47. #include "nrf_sdh_ble.h"
  48. #include "nrf_sdh_soc.h"
  49. #define BLE_ADV_MODES (5) /**< Total number of possible advertising modes. */
  50. /**@brief Function for checking if the whitelist is in use.
  51. *
  52. * @param[in] p_advertising Advertising module instance.
  53. */
  54. static bool whitelist_has_entries(ble_advertising_t * const p_advertising)
  55. {
  56. return p_advertising->whitelist_in_use;
  57. }
  58. /**@brief Function for checking if an address is valid.
  59. *
  60. * @param[in] p_addr Pointer to a bluetooth address.
  61. */
  62. static bool addr_is_valid(uint8_t const * const p_addr)
  63. {
  64. for (uint32_t i = 0; i < BLE_GAP_ADDR_LEN; i++)
  65. {
  66. if (p_addr[i] != 0)
  67. {
  68. return true;
  69. }
  70. }
  71. return false;
  72. }
  73. /**@brief Function for checking the next advertising mode.
  74. *
  75. * @param[in] adv_mode Current advertising mode.
  76. */
  77. static ble_adv_mode_t adv_mode_next_get(ble_adv_mode_t adv_mode)
  78. {
  79. return (ble_adv_mode_t)((adv_mode + 1) % BLE_ADV_MODES);
  80. }
  81. /**@brief Function for handling the Connected event.
  82. *
  83. * @param[in] p_ble_evt Event received from the BLE stack.
  84. */
  85. static void on_connected(ble_advertising_t * const p_advertising, ble_evt_t const * p_ble_evt)
  86. {
  87. if (p_ble_evt->evt.gap_evt.params.connected.role == BLE_GAP_ROLE_PERIPH)
  88. {
  89. p_advertising->current_slave_link_conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
  90. }
  91. }
  92. /**@brief Function for handling the Disconnected event.
  93. *
  94. * @param[in] p_advertising Advertising module instance.
  95. * @param[in] p_ble_evt Event received from the BLE stack.
  96. */
  97. static void on_disconnected(ble_advertising_t * const p_advertising, ble_evt_t const * p_ble_evt)
  98. {
  99. uint32_t ret;
  100. p_advertising->whitelist_temporarily_disabled = false;
  101. if (p_ble_evt->evt.gap_evt.conn_handle == p_advertising->current_slave_link_conn_handle &&
  102. p_advertising->adv_modes_config.ble_adv_on_disconnect_disabled == false)
  103. {
  104. ret = ble_advertising_start(p_advertising, BLE_ADV_MODE_DIRECTED_HIGH_DUTY);
  105. if ((ret != NRF_SUCCESS) && (p_advertising->error_handler != NULL))
  106. {
  107. p_advertising->error_handler(ret);
  108. }
  109. }
  110. }
  111. /**@brief Function for handling the Timeout event.
  112. *
  113. * @param[in] p_advertising Advertising module instance.
  114. * @param[in] p_ble_evt Event received from the BLE stack.
  115. */
  116. static void on_terminated(ble_advertising_t * const p_advertising, ble_evt_t const * p_ble_evt)
  117. {
  118. ret_code_t ret;
  119. if (p_ble_evt->header.evt_id != BLE_GAP_EVT_ADV_SET_TERMINATED)
  120. {
  121. // Nothing to do.
  122. return;
  123. }
  124. if ( p_ble_evt->evt.gap_evt.params.adv_set_terminated.reason == BLE_GAP_EVT_ADV_SET_TERMINATED_REASON_TIMEOUT
  125. ||p_ble_evt->evt.gap_evt.params.adv_set_terminated.reason == BLE_GAP_EVT_ADV_SET_TERMINATED_REASON_LIMIT_REACHED)
  126. {
  127. // Start advertising in the next mode.
  128. ret = ble_advertising_start(p_advertising, adv_mode_next_get(p_advertising->adv_mode_current));
  129. if ((ret != NRF_SUCCESS) && (p_advertising->error_handler != NULL))
  130. {
  131. p_advertising->error_handler(ret);
  132. }
  133. }
  134. }
  135. /**@brief Get the next available advertising mode.
  136. *
  137. * @param[in] p_advertising Advertising module instance.
  138. * @param[in] adv_mode Requested advertising mode.
  139. *
  140. * @returns adv_mode if possible, or the best available mode if not.
  141. */
  142. static ble_adv_mode_t adv_mode_next_avail_get(ble_advertising_t * const p_advertising,
  143. ble_adv_mode_t adv_mode)
  144. {
  145. bool peer_addr_is_valid = addr_is_valid(p_advertising->peer_address.addr);
  146. // If a mode is disabled, continue to the next mode.
  147. switch (adv_mode)
  148. {
  149. case BLE_ADV_MODE_DIRECTED_HIGH_DUTY:
  150. if ( (p_advertising->adv_modes_config.ble_adv_directed_high_duty_enabled)
  151. && (!p_advertising->adv_modes_config.ble_adv_extended_enabled)
  152. && (peer_addr_is_valid))
  153. {
  154. return BLE_ADV_MODE_DIRECTED_HIGH_DUTY;
  155. }
  156. // Fallthrough.
  157. case BLE_ADV_MODE_DIRECTED:
  158. if ((p_advertising->adv_modes_config.ble_adv_directed_enabled) && peer_addr_is_valid)
  159. {
  160. return BLE_ADV_MODE_DIRECTED;
  161. }
  162. // Fallthrough.
  163. case BLE_ADV_MODE_FAST:
  164. if (p_advertising->adv_modes_config.ble_adv_fast_enabled)
  165. {
  166. return BLE_ADV_MODE_FAST;
  167. }
  168. // Fallthrough.
  169. case BLE_ADV_MODE_SLOW:
  170. if (p_advertising->adv_modes_config.ble_adv_slow_enabled)
  171. {
  172. return BLE_ADV_MODE_SLOW;
  173. }
  174. // Fallthrough.
  175. default:
  176. return BLE_ADV_MODE_IDLE;
  177. }
  178. }
  179. /**@brief Function for starting high duty directed advertising.
  180. *
  181. * @param[in] p_advertising Advertising instance.
  182. * @param[out] p_adv_params Advertising parameters.
  183. *
  184. * @return NRF_SUCCESS
  185. */
  186. static ret_code_t set_adv_mode_directed_high_duty(ble_advertising_t * const p_advertising,
  187. ble_gap_adv_params_t * p_adv_params)
  188. {
  189. p_advertising->adv_evt = BLE_ADV_EVT_DIRECTED_HIGH_DUTY;
  190. p_advertising->p_adv_data = NULL;
  191. p_adv_params->p_peer_addr = &(p_advertising->peer_address);
  192. p_adv_params->interval = 0;
  193. p_adv_params->properties.type = BLE_GAP_ADV_TYPE_CONNECTABLE_NONSCANNABLE_DIRECTED_HIGH_DUTY_CYCLE;
  194. p_adv_params->duration = BLE_GAP_ADV_TIMEOUT_HIGH_DUTY_MAX;
  195. return NRF_SUCCESS;
  196. }
  197. /**@brief Function for starting directed slow advertising.
  198. *
  199. * @param[in] p_advertising Advertising module instance.
  200. * @param[out] p_adv_params Advertising parameters.
  201. *
  202. * @return NRF_SUCCESS
  203. */
  204. static ret_code_t set_adv_mode_directed(ble_advertising_t * const p_advertising,
  205. ble_gap_adv_params_t * p_adv_params)
  206. {
  207. p_advertising->adv_evt = BLE_ADV_EVT_DIRECTED;
  208. #if !defined (S112) && !defined(S312)
  209. if (p_advertising->adv_modes_config.ble_adv_extended_enabled)
  210. {
  211. p_adv_params->properties.type = BLE_GAP_ADV_TYPE_EXTENDED_CONNECTABLE_NONSCANNABLE_DIRECTED;
  212. }
  213. else
  214. {
  215. #endif // !defined (S112) && !defined(S312)
  216. p_adv_params->properties.type = BLE_GAP_ADV_TYPE_CONNECTABLE_NONSCANNABLE_DIRECTED;
  217. #if !defined (S112) && !defined(S312)
  218. }
  219. #endif // !defined (S112) && !defined(S312)
  220. p_adv_params->duration = p_advertising->adv_modes_config.ble_adv_directed_timeout;
  221. p_advertising->p_adv_data = NULL;
  222. p_adv_params->p_peer_addr = &p_advertising->peer_address;
  223. p_adv_params->interval = p_advertising->adv_modes_config.ble_adv_directed_interval;
  224. return NRF_SUCCESS;
  225. }
  226. /**@brief Function for indicating whether to use whitelist for advertising.
  227. *
  228. * @param[in] p_advertising Advertising module instance.
  229. *
  230. * @return Whether to use whitelist.
  231. */
  232. static bool use_whitelist(ble_advertising_t * const p_advertising)
  233. {
  234. return((p_advertising->adv_modes_config.ble_adv_whitelist_enabled) &&
  235. (!p_advertising->whitelist_temporarily_disabled) &&
  236. (whitelist_has_entries(p_advertising)));
  237. }
  238. /**@brief Function for setting new advertising flags in the advertising parameters.
  239. *
  240. * @param[in] p_advertising Advertising module instance.
  241. * @param[in] flags New flags.
  242. *
  243. * @return Any error from @ref sd_ble_gap_adv_set_configure.
  244. */
  245. static ret_code_t flags_set(ble_advertising_t * const p_advertising, uint8_t flags)
  246. {
  247. uint8_t * p_flags = ble_advdata_parse(p_advertising->adv_data.adv_data.p_data,
  248. p_advertising->adv_data.adv_data.len,
  249. BLE_GAP_AD_TYPE_FLAGS);
  250. if (p_flags != NULL)
  251. {
  252. *p_flags = flags;
  253. }
  254. return sd_ble_gap_adv_set_configure(&p_advertising->adv_handle, &p_advertising->adv_data, &p_advertising->adv_params);
  255. }
  256. /**@brief Function for starting fast advertising.
  257. *
  258. * @param[in] p_advertising Advertising module instance.
  259. * @param[out] p_adv_params Advertising parameters.
  260. *
  261. * @return NRF_SUCCESS or an error from @ref flags_set().
  262. */
  263. static ret_code_t set_adv_mode_fast(ble_advertising_t * const p_advertising,
  264. ble_gap_adv_params_t * p_adv_params)
  265. {
  266. ret_code_t ret;
  267. p_adv_params->interval = p_advertising->adv_modes_config.ble_adv_fast_interval;
  268. p_adv_params->duration = p_advertising->adv_modes_config.ble_adv_fast_timeout;
  269. #if !defined (S112) && !defined(S312)
  270. if (p_advertising->adv_modes_config.ble_adv_extended_enabled)
  271. {
  272. p_advertising->adv_params.properties.type = BLE_GAP_ADV_TYPE_EXTENDED_CONNECTABLE_NONSCANNABLE_UNDIRECTED;
  273. }
  274. else
  275. {
  276. #endif // !defined (S112) && !defined(S312)
  277. p_advertising->adv_params.properties.type = BLE_GAP_ADV_TYPE_CONNECTABLE_SCANNABLE_UNDIRECTED;
  278. #if !defined (S112) && !defined(S312)
  279. }
  280. #endif // !defined (S112) && !defined(S312)
  281. if (use_whitelist(p_advertising))
  282. {
  283. p_adv_params->filter_policy = BLE_GAP_ADV_FP_FILTER_CONNREQ;
  284. // Set correct flags.
  285. ret = flags_set(p_advertising, BLE_GAP_ADV_FLAG_BR_EDR_NOT_SUPPORTED);
  286. VERIFY_SUCCESS(ret);
  287. p_advertising->adv_evt = BLE_ADV_EVT_FAST_WHITELIST;
  288. }
  289. else
  290. {
  291. p_advertising->adv_evt = BLE_ADV_EVT_FAST;
  292. }
  293. p_advertising->p_adv_data = &(p_advertising->adv_data);
  294. return NRF_SUCCESS;
  295. }
  296. /**@brief Function for starting slow advertising.
  297. *
  298. * @param[in] p_advertising Advertising module instance.
  299. * @param[out] p_adv_params Advertising parameters.
  300. *
  301. * @return NRF_SUCCESS or an error from @ref flags_set().
  302. */
  303. static ret_code_t set_adv_mode_slow(ble_advertising_t * const p_advertising,
  304. ble_gap_adv_params_t * p_adv_params)
  305. {
  306. ret_code_t ret;
  307. p_adv_params->interval = p_advertising->adv_modes_config.ble_adv_slow_interval;
  308. p_adv_params->duration = p_advertising->adv_modes_config.ble_adv_slow_timeout;
  309. #if !defined (S112) && !defined(S312)
  310. if (p_advertising->adv_modes_config.ble_adv_extended_enabled)
  311. {
  312. p_advertising->adv_params.properties.type = BLE_GAP_ADV_TYPE_EXTENDED_CONNECTABLE_NONSCANNABLE_UNDIRECTED;
  313. }
  314. else
  315. {
  316. #endif // !defined (S112) && !defined(S312)
  317. p_advertising->adv_params.properties.type = BLE_GAP_ADV_TYPE_CONNECTABLE_SCANNABLE_UNDIRECTED;
  318. #if !defined (S112) && !defined(S312)
  319. }
  320. #endif // !defined (S112) && !defined(S312)
  321. if (use_whitelist(p_advertising))
  322. {
  323. p_adv_params->filter_policy = BLE_GAP_ADV_FP_FILTER_CONNREQ;
  324. // Set correct flags.
  325. ret = flags_set(p_advertising, BLE_GAP_ADV_FLAG_BR_EDR_NOT_SUPPORTED);
  326. VERIFY_SUCCESS(ret);
  327. p_advertising->adv_evt = BLE_ADV_EVT_SLOW_WHITELIST;
  328. }
  329. else
  330. {
  331. p_advertising->adv_evt = BLE_ADV_EVT_SLOW;
  332. }
  333. p_advertising->p_adv_data = &(p_advertising->adv_data);
  334. return NRF_SUCCESS;
  335. }
  336. /**@brief Function for checking if an advertising module configuration is legal.
  337. *
  338. * @details Advertising module can not be initialized if high duty directed advertising is used
  339. * together with extended advertising.
  340. *
  341. * @param[in] p_config Pointer to the configuration.
  342. *
  343. * @return True If the configuration is valid.
  344. * @return False If the configuration is invalid.
  345. */
  346. static bool config_is_valid(ble_adv_modes_config_t const * const p_config)
  347. {
  348. if ((p_config->ble_adv_directed_high_duty_enabled == true) &&
  349. (p_config->ble_adv_extended_enabled == true))
  350. {
  351. return false;
  352. }
  353. #if !defined (S140)
  354. else if ( p_config->ble_adv_primary_phy == BLE_GAP_PHY_CODED ||
  355. p_config->ble_adv_secondary_phy == BLE_GAP_PHY_CODED)
  356. {
  357. return false;
  358. }
  359. #endif // !defined (S140)
  360. else
  361. {
  362. return true;
  363. }
  364. }
  365. void ble_advertising_conn_cfg_tag_set(ble_advertising_t * const p_advertising,
  366. uint8_t ble_cfg_tag)
  367. {
  368. p_advertising->conn_cfg_tag = ble_cfg_tag;
  369. }
  370. uint32_t ble_advertising_init(ble_advertising_t * const p_advertising,
  371. ble_advertising_init_t const * const p_init)
  372. {
  373. uint32_t ret;
  374. if ((p_init == NULL) || (p_advertising == NULL))
  375. {
  376. return NRF_ERROR_NULL;
  377. }
  378. if (!config_is_valid(&p_init->config))
  379. {
  380. return NRF_ERROR_INVALID_PARAM;
  381. }
  382. p_advertising->adv_mode_current = BLE_ADV_MODE_IDLE;
  383. p_advertising->adv_modes_config = p_init->config;
  384. p_advertising->conn_cfg_tag = BLE_CONN_CFG_TAG_DEFAULT;
  385. p_advertising->evt_handler = p_init->evt_handler;
  386. p_advertising->error_handler = p_init->error_handler;
  387. p_advertising->current_slave_link_conn_handle = BLE_CONN_HANDLE_INVALID;
  388. p_advertising->p_adv_data = &p_advertising->adv_data;
  389. memset(&p_advertising->peer_address, 0, sizeof(p_advertising->peer_address));
  390. // Copy advertising data.
  391. if (!p_advertising->initialized)
  392. {
  393. p_advertising->adv_handle = BLE_GAP_ADV_SET_HANDLE_NOT_SET;
  394. }
  395. p_advertising->adv_data.adv_data.p_data = p_advertising->enc_advdata;
  396. if (p_advertising->adv_modes_config.ble_adv_extended_enabled == true)
  397. {
  398. #ifdef BLE_GAP_ADV_SET_DATA_SIZE_EXTENDED_CONNECTABLE_MAX_SUPPORTED
  399. p_advertising->adv_data.adv_data.len = BLE_GAP_ADV_SET_DATA_SIZE_EXTENDED_CONNECTABLE_MAX_SUPPORTED;
  400. #else
  401. p_advertising->adv_data.adv_data.len = BLE_GAP_ADV_SET_DATA_SIZE_MAX;
  402. #endif // BLE_GAP_ADV_SET_DATA_SIZE_EXTENDED_CONNECTABLE_MAX_SUPPORTED
  403. }
  404. else
  405. {
  406. p_advertising->adv_data.adv_data.len = BLE_GAP_ADV_SET_DATA_SIZE_MAX;
  407. }
  408. ret = ble_advdata_encode(&p_init->advdata, p_advertising->enc_advdata, &p_advertising->adv_data.adv_data.len);
  409. VERIFY_SUCCESS(ret);
  410. p_advertising->adv_data.scan_rsp_data.p_data = p_advertising->enc_scan_rsp_data;
  411. if (p_advertising->adv_modes_config.ble_adv_extended_enabled == true)
  412. {
  413. #ifdef BLE_GAP_ADV_SET_DATA_SIZE_EXTENDED_CONNECTABLE_MAX_SUPPORTED
  414. p_advertising->adv_data.scan_rsp_data.len = BLE_GAP_ADV_SET_DATA_SIZE_EXTENDED_CONNECTABLE_MAX_SUPPORTED;
  415. #else
  416. p_advertising->adv_data.scan_rsp_data.len = BLE_GAP_ADV_SET_DATA_SIZE_MAX;
  417. #endif // BLE_GAP_ADV_SET_DATA_SIZE_EXTENDED_CONNECTABLE_MAX_SUPPORTED
  418. }
  419. else
  420. {
  421. p_advertising->adv_data.scan_rsp_data.len = BLE_GAP_ADV_SET_DATA_SIZE_MAX;
  422. }
  423. ret = ble_advdata_encode(&p_init->srdata,
  424. p_advertising->adv_data.scan_rsp_data.p_data,
  425. &p_advertising->adv_data.scan_rsp_data.len);
  426. VERIFY_SUCCESS(ret);
  427. // Configure a initial advertising configuration. The advertising data and and advertising
  428. // parameters will be changed later when we call @ref ble_advertising_start, but must be set
  429. // to legal values here to define an advertising handle.
  430. p_advertising->adv_params.primary_phy = BLE_GAP_PHY_1MBPS;
  431. p_advertising->adv_params.duration = p_advertising->adv_modes_config.ble_adv_fast_timeout;
  432. p_advertising->adv_params.properties.type = BLE_GAP_ADV_TYPE_CONNECTABLE_SCANNABLE_UNDIRECTED;
  433. p_advertising->adv_params.p_peer_addr = NULL;
  434. p_advertising->adv_params.filter_policy = BLE_GAP_ADV_FP_ANY;
  435. p_advertising->adv_params.interval = p_advertising->adv_modes_config.ble_adv_fast_interval;
  436. ret = sd_ble_gap_adv_set_configure(&p_advertising->adv_handle, NULL, &p_advertising->adv_params);
  437. VERIFY_SUCCESS(ret);
  438. p_advertising->initialized = true;
  439. return ret;
  440. }
  441. /**@brief Function for checking that a phy define value matches one of the valid phys from the SD.
  442. *
  443. * @param[in] PHY to be validated.
  444. *
  445. * @retval true If the PHY value is valid (1mbit, 2mbit, coded).
  446. * @retval false If the PHY value is invalid.
  447. */
  448. static bool phy_is_valid(uint32_t const * const p_phy)
  449. {
  450. if ((*p_phy) == BLE_GAP_PHY_1MBPS ||
  451. (*p_phy) == BLE_GAP_PHY_2MBPS
  452. #if defined (S140)
  453. || (*p_phy) == BLE_GAP_PHY_CODED
  454. #endif // !defined (S140)
  455. )
  456. {
  457. return true;
  458. }
  459. else
  460. {
  461. return false;
  462. }
  463. }
  464. uint32_t ble_advertising_start(ble_advertising_t * const p_advertising,
  465. ble_adv_mode_t advertising_mode)
  466. {
  467. uint32_t ret;
  468. if (p_advertising->initialized == false)
  469. {
  470. return NRF_ERROR_INVALID_STATE;
  471. }
  472. p_advertising->adv_mode_current = advertising_mode;
  473. memset(&p_advertising->peer_address, 0, sizeof(p_advertising->peer_address));
  474. if ( ((p_advertising->adv_modes_config.ble_adv_directed_high_duty_enabled) && (p_advertising->adv_mode_current == BLE_ADV_MODE_DIRECTED_HIGH_DUTY))
  475. ||((p_advertising->adv_modes_config.ble_adv_directed_enabled) && (p_advertising->adv_mode_current == BLE_ADV_MODE_DIRECTED_HIGH_DUTY))
  476. ||((p_advertising->adv_modes_config.ble_adv_directed_enabled) && (p_advertising->adv_mode_current == BLE_ADV_MODE_DIRECTED))
  477. )
  478. {
  479. if (p_advertising->evt_handler != NULL)
  480. {
  481. p_advertising->peer_addr_reply_expected = true;
  482. p_advertising->evt_handler(BLE_ADV_EVT_PEER_ADDR_REQUEST);
  483. }
  484. else
  485. {
  486. p_advertising->peer_addr_reply_expected = false;
  487. }
  488. }
  489. p_advertising->adv_mode_current = adv_mode_next_avail_get(p_advertising, advertising_mode);
  490. // Fetch the whitelist.
  491. if ((p_advertising->evt_handler != NULL) &&
  492. (p_advertising->adv_mode_current == BLE_ADV_MODE_FAST || p_advertising->adv_mode_current == BLE_ADV_MODE_SLOW) &&
  493. (p_advertising->adv_modes_config.ble_adv_whitelist_enabled) &&
  494. (!p_advertising->whitelist_temporarily_disabled))
  495. {
  496. p_advertising->whitelist_in_use = false;
  497. p_advertising->whitelist_reply_expected = true;
  498. p_advertising->evt_handler(BLE_ADV_EVT_WHITELIST_REQUEST);
  499. }
  500. else
  501. {
  502. p_advertising->whitelist_reply_expected = false;
  503. }
  504. // Initialize advertising parameters with default values.
  505. memset(&p_advertising->adv_params, 0, sizeof(p_advertising->adv_params));
  506. p_advertising->adv_params.properties.type = BLE_GAP_ADV_TYPE_CONNECTABLE_SCANNABLE_UNDIRECTED;
  507. // Use 1MBIT as primary phy if no phy was selected.
  508. if (phy_is_valid(&p_advertising->adv_modes_config.ble_adv_primary_phy))
  509. {
  510. p_advertising->adv_params.primary_phy = p_advertising->adv_modes_config.ble_adv_primary_phy;
  511. }
  512. else
  513. {
  514. p_advertising->adv_params.primary_phy = BLE_GAP_PHY_1MBPS;
  515. }
  516. if (p_advertising->adv_modes_config.ble_adv_extended_enabled)
  517. {
  518. // Use 1MBIT as secondary phy if no phy was selected.
  519. if (phy_is_valid(&p_advertising->adv_modes_config.ble_adv_primary_phy))
  520. {
  521. p_advertising->adv_params.secondary_phy = p_advertising->adv_modes_config.ble_adv_secondary_phy;
  522. }
  523. else
  524. {
  525. p_advertising->adv_params.secondary_phy = BLE_GAP_PHY_1MBPS;
  526. }
  527. }
  528. p_advertising->adv_params.filter_policy = BLE_GAP_ADV_FP_ANY;
  529. // Set advertising parameters and events according to selected advertising mode.
  530. switch (p_advertising->adv_mode_current)
  531. {
  532. case BLE_ADV_MODE_DIRECTED_HIGH_DUTY:
  533. ret = set_adv_mode_directed_high_duty(p_advertising, &p_advertising->adv_params);
  534. break;
  535. case BLE_ADV_MODE_DIRECTED:
  536. ret = set_adv_mode_directed(p_advertising, &p_advertising->adv_params);
  537. break;
  538. case BLE_ADV_MODE_FAST:
  539. ret = set_adv_mode_fast(p_advertising, &p_advertising->adv_params);
  540. break;
  541. case BLE_ADV_MODE_SLOW:
  542. ret = set_adv_mode_slow(p_advertising, &p_advertising->adv_params);
  543. break;
  544. case BLE_ADV_MODE_IDLE:
  545. p_advertising->adv_evt = BLE_ADV_EVT_IDLE;
  546. break;
  547. default:
  548. break;
  549. }
  550. if (p_advertising->adv_mode_current != BLE_ADV_MODE_IDLE)
  551. {
  552. ret = sd_ble_gap_adv_set_configure(&p_advertising->adv_handle, p_advertising->p_adv_data, &p_advertising->adv_params);
  553. if (ret != NRF_SUCCESS)
  554. {
  555. return ret;
  556. }
  557. ret = sd_ble_gap_adv_start(p_advertising->adv_handle, p_advertising->conn_cfg_tag);
  558. if (ret != NRF_SUCCESS)
  559. {
  560. return ret;
  561. }
  562. }
  563. if (p_advertising->evt_handler != NULL)
  564. {
  565. p_advertising->evt_handler(p_advertising->adv_evt);
  566. }
  567. return NRF_SUCCESS;
  568. }
  569. void ble_advertising_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_context)
  570. {
  571. ble_advertising_t * p_advertising = (ble_advertising_t *)p_context;
  572. switch (p_ble_evt->header.evt_id)
  573. {
  574. case BLE_GAP_EVT_CONNECTED:
  575. on_connected(p_advertising, p_ble_evt);
  576. break;
  577. // Upon disconnection, whitelist will be activated and direct advertising is started.
  578. case BLE_GAP_EVT_DISCONNECTED:
  579. on_disconnected(p_advertising, p_ble_evt);
  580. break;
  581. // Upon terminated advertising (time-out), the next advertising mode is started.
  582. case BLE_GAP_EVT_ADV_SET_TERMINATED:
  583. on_terminated(p_advertising, p_ble_evt);
  584. break;
  585. default:
  586. break;
  587. }
  588. }
  589. void ble_advertising_on_sys_evt(uint32_t evt_id, void * p_context)
  590. {
  591. // Changes to advertising and fds components have made this function obsolete.
  592. // This function will be removed in a future major release.
  593. }
  594. uint32_t ble_advertising_peer_addr_reply(ble_advertising_t * const p_advertising,
  595. ble_gap_addr_t * p_peer_address)
  596. {
  597. if (!p_advertising->peer_addr_reply_expected)
  598. {
  599. return NRF_ERROR_INVALID_STATE;
  600. }
  601. p_advertising->peer_addr_reply_expected = false;
  602. memcpy(&p_advertising->peer_address, p_peer_address, sizeof(p_advertising->peer_address));
  603. return NRF_SUCCESS;
  604. }
  605. uint32_t ble_advertising_whitelist_reply(ble_advertising_t * const p_advertising,
  606. ble_gap_addr_t const * p_gap_addrs,
  607. uint32_t addr_cnt,
  608. ble_gap_irk_t const * p_gap_irks,
  609. uint32_t irk_cnt)
  610. {
  611. if (!p_advertising->whitelist_reply_expected)
  612. {
  613. return NRF_ERROR_INVALID_STATE;
  614. }
  615. p_advertising->whitelist_reply_expected = false;
  616. p_advertising->whitelist_in_use = ((addr_cnt > 0) || (irk_cnt > 0));
  617. return NRF_SUCCESS;
  618. }
  619. uint32_t ble_advertising_restart_without_whitelist(ble_advertising_t * const p_advertising)
  620. {
  621. ret_code_t ret;
  622. (void) sd_ble_gap_adv_stop(p_advertising->adv_handle);
  623. p_advertising->whitelist_temporarily_disabled = true;
  624. p_advertising->whitelist_in_use = false;
  625. p_advertising->adv_params.filter_policy = BLE_GAP_ADV_FP_ANY;
  626. // Set correct flags.
  627. ret = flags_set(p_advertising, BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
  628. VERIFY_SUCCESS(ret);
  629. ret = ble_advertising_start(p_advertising, p_advertising->adv_mode_current);
  630. if ((ret != NRF_SUCCESS) && (p_advertising->error_handler != NULL))
  631. {
  632. p_advertising->error_handler(ret);
  633. }
  634. return NRF_SUCCESS;
  635. }
  636. void ble_advertising_modes_config_set(ble_advertising_t * const p_advertising,
  637. ble_adv_modes_config_t const * const p_adv_modes_config)
  638. {
  639. p_advertising->adv_modes_config = *p_adv_modes_config;
  640. }
  641. ret_code_t ble_advertising_advdata_update(ble_advertising_t * const p_advertising,
  642. ble_gap_adv_data_t * const p_new_advdata_buf,
  643. bool permanent)
  644. {
  645. if (permanent)
  646. {
  647. memcpy(&p_advertising->adv_data, p_new_advdata_buf, sizeof(p_advertising->adv_data));
  648. p_advertising->p_adv_data = &p_advertising->adv_data;
  649. }
  650. else
  651. {
  652. p_advertising->p_adv_data = p_new_advdata_buf;
  653. }
  654. return sd_ble_gap_adv_set_configure(&p_advertising->adv_handle,
  655. p_advertising->p_adv_data,
  656. NULL);
  657. }
  658. #endif // NRF_MODULE_ENABLED(BLE_ADVERTISING)