ble_advertising.c 27 KB

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