ble_advertising.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  1. /**
  2. * Copyright (c) 2015 - 2018, 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)
  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)
  216. p_adv_params->properties.type = BLE_GAP_ADV_TYPE_CONNECTABLE_NONSCANNABLE_DIRECTED;
  217. #if !defined (S112)
  218. }
  219. #endif // !defined (S112)
  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)
  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)
  277. p_advertising->adv_params.properties.type = BLE_GAP_ADV_TYPE_CONNECTABLE_SCANNABLE_UNDIRECTED;
  278. #if !defined (S112)
  279. }
  280. #endif // !defined (S112)
  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)
  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)
  317. p_advertising->adv_params.properties.type = BLE_GAP_ADV_TYPE_CONNECTABLE_SCANNABLE_UNDIRECTED;
  318. #if !defined (S112)
  319. }
  320. #endif // !defined (S112)
  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. if (&p_init->srdata != NULL)
  411. {
  412. p_advertising->adv_data.scan_rsp_data.p_data = p_advertising->enc_scan_rsp_data;
  413. if (p_advertising->adv_modes_config.ble_adv_extended_enabled == true)
  414. {
  415. #ifdef BLE_GAP_ADV_SET_DATA_SIZE_EXTENDED_CONNECTABLE_MAX_SUPPORTED
  416. p_advertising->adv_data.scan_rsp_data.len = BLE_GAP_ADV_SET_DATA_SIZE_EXTENDED_CONNECTABLE_MAX_SUPPORTED;
  417. #else
  418. p_advertising->adv_data.scan_rsp_data.len = BLE_GAP_ADV_SET_DATA_SIZE_MAX;
  419. #endif // BLE_GAP_ADV_SET_DATA_SIZE_EXTENDED_CONNECTABLE_MAX_SUPPORTED
  420. }
  421. else
  422. {
  423. p_advertising->adv_data.scan_rsp_data.len = BLE_GAP_ADV_SET_DATA_SIZE_MAX;
  424. }
  425. ret = ble_advdata_encode(&p_init->srdata,
  426. p_advertising->adv_data.scan_rsp_data.p_data,
  427. &p_advertising->adv_data.scan_rsp_data.len);
  428. VERIFY_SUCCESS(ret);
  429. }
  430. else
  431. {
  432. p_advertising->adv_data.scan_rsp_data.p_data = NULL;
  433. p_advertising->adv_data.scan_rsp_data.len = 0;
  434. }
  435. // Configure a initial advertising configuration. The advertising data and and advertising
  436. // parameters will be changed later when we call @ref ble_advertising_start, but must be set
  437. // to legal values here to define an advertising handle.
  438. p_advertising->adv_params.primary_phy = BLE_GAP_PHY_1MBPS;
  439. p_advertising->adv_params.duration = p_advertising->adv_modes_config.ble_adv_fast_timeout;
  440. p_advertising->adv_params.properties.type = BLE_GAP_ADV_TYPE_CONNECTABLE_SCANNABLE_UNDIRECTED;
  441. p_advertising->adv_params.p_peer_addr = NULL;
  442. p_advertising->adv_params.filter_policy = BLE_GAP_ADV_FP_ANY;
  443. p_advertising->adv_params.interval = p_advertising->adv_modes_config.ble_adv_fast_interval;
  444. ret = sd_ble_gap_adv_set_configure(&p_advertising->adv_handle, NULL, &p_advertising->adv_params);
  445. VERIFY_SUCCESS(ret);
  446. p_advertising->initialized = true;
  447. return ret;
  448. }
  449. /**@brief Function for checking that a phy define value matches one of the valid phys from the SD.
  450. *
  451. * @param[in] PHY to be validated.
  452. *
  453. * @retval true If the PHY value is valid (1mbit, 2mbit, coded).
  454. * @retval false If the PHY value is invalid.
  455. */
  456. static bool phy_is_valid(uint32_t const * const p_phy)
  457. {
  458. if ((*p_phy) == BLE_GAP_PHY_1MBPS ||
  459. (*p_phy) == BLE_GAP_PHY_2MBPS
  460. #if defined (S140)
  461. || (*p_phy) == BLE_GAP_PHY_CODED
  462. #endif // !defined (S140)
  463. )
  464. {
  465. return true;
  466. }
  467. else
  468. {
  469. return false;
  470. }
  471. }
  472. uint32_t ble_advertising_start(ble_advertising_t * const p_advertising,
  473. ble_adv_mode_t advertising_mode)
  474. {
  475. uint32_t ret;
  476. if (p_advertising->initialized == false)
  477. {
  478. return NRF_ERROR_INVALID_STATE;
  479. }
  480. p_advertising->adv_mode_current = advertising_mode;
  481. memset(&p_advertising->peer_address, 0, sizeof(p_advertising->peer_address));
  482. if ( ((p_advertising->adv_modes_config.ble_adv_directed_high_duty_enabled) && (p_advertising->adv_mode_current == BLE_ADV_MODE_DIRECTED_HIGH_DUTY))
  483. ||((p_advertising->adv_modes_config.ble_adv_directed_enabled) && (p_advertising->adv_mode_current == BLE_ADV_MODE_DIRECTED_HIGH_DUTY))
  484. ||((p_advertising->adv_modes_config.ble_adv_directed_enabled) && (p_advertising->adv_mode_current == BLE_ADV_MODE_DIRECTED))
  485. )
  486. {
  487. if (p_advertising->evt_handler != NULL)
  488. {
  489. p_advertising->peer_addr_reply_expected = true;
  490. p_advertising->evt_handler(BLE_ADV_EVT_PEER_ADDR_REQUEST);
  491. }
  492. else
  493. {
  494. p_advertising->peer_addr_reply_expected = false;
  495. }
  496. }
  497. p_advertising->adv_mode_current = adv_mode_next_avail_get(p_advertising, advertising_mode);
  498. // Fetch the whitelist.
  499. if ((p_advertising->evt_handler != NULL) &&
  500. (p_advertising->adv_mode_current == BLE_ADV_MODE_FAST || p_advertising->adv_mode_current == BLE_ADV_MODE_SLOW) &&
  501. (p_advertising->adv_modes_config.ble_adv_whitelist_enabled) &&
  502. (!p_advertising->whitelist_temporarily_disabled))
  503. {
  504. p_advertising->whitelist_in_use = false;
  505. p_advertising->whitelist_reply_expected = true;
  506. p_advertising->evt_handler(BLE_ADV_EVT_WHITELIST_REQUEST);
  507. }
  508. else
  509. {
  510. p_advertising->whitelist_reply_expected = false;
  511. }
  512. // Initialize advertising parameters with default values.
  513. memset(&p_advertising->adv_params, 0, sizeof(p_advertising->adv_params));
  514. p_advertising->adv_params.properties.type = BLE_GAP_ADV_TYPE_CONNECTABLE_SCANNABLE_UNDIRECTED;
  515. // Use 1MBIT as primary phy if no phy was selected.
  516. if (phy_is_valid(&p_advertising->adv_modes_config.ble_adv_primary_phy))
  517. {
  518. p_advertising->adv_params.primary_phy = p_advertising->adv_modes_config.ble_adv_primary_phy;
  519. }
  520. else
  521. {
  522. p_advertising->adv_params.primary_phy = BLE_GAP_PHY_1MBPS;
  523. }
  524. if (p_advertising->adv_modes_config.ble_adv_extended_enabled)
  525. {
  526. // Use 1MBIT as secondary phy if no phy was selected.
  527. if (phy_is_valid(&p_advertising->adv_modes_config.ble_adv_primary_phy))
  528. {
  529. p_advertising->adv_params.secondary_phy = p_advertising->adv_modes_config.ble_adv_secondary_phy;
  530. }
  531. else
  532. {
  533. p_advertising->adv_params.secondary_phy = BLE_GAP_PHY_1MBPS;
  534. }
  535. }
  536. p_advertising->adv_params.filter_policy = BLE_GAP_ADV_FP_ANY;
  537. // Set advertising parameters and events according to selected advertising mode.
  538. switch (p_advertising->adv_mode_current)
  539. {
  540. case BLE_ADV_MODE_DIRECTED_HIGH_DUTY:
  541. ret = set_adv_mode_directed_high_duty(p_advertising, &p_advertising->adv_params);
  542. break;
  543. case BLE_ADV_MODE_DIRECTED:
  544. ret = set_adv_mode_directed(p_advertising, &p_advertising->adv_params);
  545. break;
  546. case BLE_ADV_MODE_FAST:
  547. ret = set_adv_mode_fast(p_advertising, &p_advertising->adv_params);
  548. break;
  549. case BLE_ADV_MODE_SLOW:
  550. ret = set_adv_mode_slow(p_advertising, &p_advertising->adv_params);
  551. break;
  552. case BLE_ADV_MODE_IDLE:
  553. p_advertising->adv_evt = BLE_ADV_EVT_IDLE;
  554. break;
  555. default:
  556. break;
  557. }
  558. if (p_advertising->adv_mode_current != BLE_ADV_MODE_IDLE)
  559. {
  560. ret = sd_ble_gap_adv_set_configure(&p_advertising->adv_handle, p_advertising->p_adv_data, &p_advertising->adv_params);
  561. if (ret != NRF_SUCCESS)
  562. {
  563. return ret;
  564. }
  565. ret = sd_ble_gap_adv_start(p_advertising->adv_handle, p_advertising->conn_cfg_tag);
  566. if (ret != NRF_SUCCESS)
  567. {
  568. return ret;
  569. }
  570. }
  571. if (p_advertising->evt_handler != NULL)
  572. {
  573. p_advertising->evt_handler(p_advertising->adv_evt);
  574. }
  575. return NRF_SUCCESS;
  576. }
  577. void ble_advertising_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_context)
  578. {
  579. ble_advertising_t * p_advertising = (ble_advertising_t *)p_context;
  580. switch (p_ble_evt->header.evt_id)
  581. {
  582. case BLE_GAP_EVT_CONNECTED:
  583. on_connected(p_advertising, p_ble_evt);
  584. break;
  585. // Upon disconnection, whitelist will be activated and direct advertising is started.
  586. case BLE_GAP_EVT_DISCONNECTED:
  587. on_disconnected(p_advertising, p_ble_evt);
  588. break;
  589. // Upon terminated advertising (time-out), the next advertising mode is started.
  590. case BLE_GAP_EVT_ADV_SET_TERMINATED:
  591. on_terminated(p_advertising, p_ble_evt);
  592. break;
  593. default:
  594. break;
  595. }
  596. }
  597. void ble_advertising_on_sys_evt(uint32_t evt_id, void * p_context)
  598. {
  599. // Changes to advertising and fds components have made this function obsolete.
  600. // This function will be removed in a future major release.
  601. }
  602. uint32_t ble_advertising_peer_addr_reply(ble_advertising_t * const p_advertising,
  603. ble_gap_addr_t * p_peer_address)
  604. {
  605. if (!p_advertising->peer_addr_reply_expected)
  606. {
  607. return NRF_ERROR_INVALID_STATE;
  608. }
  609. p_advertising->peer_addr_reply_expected = false;
  610. memcpy(&p_advertising->peer_address, p_peer_address, sizeof(p_advertising->peer_address));
  611. return NRF_SUCCESS;
  612. }
  613. uint32_t ble_advertising_whitelist_reply(ble_advertising_t * const p_advertising,
  614. ble_gap_addr_t const * p_gap_addrs,
  615. uint32_t addr_cnt,
  616. ble_gap_irk_t const * p_gap_irks,
  617. uint32_t irk_cnt)
  618. {
  619. if (!p_advertising->whitelist_reply_expected)
  620. {
  621. return NRF_ERROR_INVALID_STATE;
  622. }
  623. p_advertising->whitelist_reply_expected = false;
  624. p_advertising->whitelist_in_use = ((addr_cnt > 0) || (irk_cnt > 0));
  625. return NRF_SUCCESS;
  626. }
  627. uint32_t ble_advertising_restart_without_whitelist(ble_advertising_t * const p_advertising)
  628. {
  629. ret_code_t ret;
  630. (void) sd_ble_gap_adv_stop(p_advertising->adv_handle);
  631. p_advertising->whitelist_temporarily_disabled = true;
  632. p_advertising->whitelist_in_use = false;
  633. p_advertising->adv_params.filter_policy = BLE_GAP_ADV_FP_ANY;
  634. // Set correct flags.
  635. ret = flags_set(p_advertising, BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
  636. VERIFY_SUCCESS(ret);
  637. ret = ble_advertising_start(p_advertising, p_advertising->adv_mode_current);
  638. if ((ret != NRF_SUCCESS) && (p_advertising->error_handler != NULL))
  639. {
  640. p_advertising->error_handler(ret);
  641. }
  642. return NRF_SUCCESS;
  643. }
  644. void ble_advertising_modes_config_set(ble_advertising_t * const p_advertising,
  645. ble_adv_modes_config_t const * const p_adv_modes_config)
  646. {
  647. p_advertising->adv_modes_config = *p_adv_modes_config;
  648. }
  649. ret_code_t ble_advertising_advdata_update(ble_advertising_t * const p_advertising,
  650. ble_gap_adv_data_t * const p_new_advdata_buf,
  651. bool permanent)
  652. {
  653. if (permanent)
  654. {
  655. memcpy(&p_advertising->adv_data, p_new_advdata_buf, sizeof(p_advertising->adv_data));
  656. p_advertising->p_adv_data = &p_advertising->adv_data;
  657. }
  658. else
  659. {
  660. p_advertising->p_adv_data = p_new_advdata_buf;
  661. }
  662. return sd_ble_gap_adv_set_configure(&p_advertising->adv_handle,
  663. p_advertising->p_adv_data,
  664. NULL);
  665. }
  666. #endif // NRF_MODULE_ENABLED(BLE_ADVERTISING)