es_adv.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /**
  2. * Copyright (c) 2016 - 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 "es_adv.h"
  41. #include "app_error.h"
  42. #include "es_adv_frame.h"
  43. #include "es_adv_timing.h"
  44. #include "es_tlm.h"
  45. #include "es_slot.h"
  46. #define MULTIPROT_BEACON_DELAY_MS 75 //!< Maximum delay of the beacon send by multiprotocol example.
  47. static es_adv_evt_handler_t m_adv_evt_handler; //!< Eddystone advertisement event handler.
  48. static bool m_is_connected = false; //!< Is the Eddystone beacon in a connected state.
  49. static bool m_remain_connectable = false; //!< Should the Eddystone beacon remain connectable.
  50. static uint8_t m_ecs_uuid_type = 0; //!< UUID type of the Eddystone Configuration Service.
  51. static uint16_t m_adv_interval = APP_CFG_NON_CONN_ADV_INTERVAL_MS; //!< Current advertisement interval.
  52. static uint8_t m_enc_advdata[BLE_GAP_ADV_SET_DATA_SIZE_MAX]; //!< Buffer for storing an encoded advertising set.
  53. static uint8_t m_enc_scan_response_data[BLE_GAP_ADV_SET_DATA_SIZE_MAX]; //!< Buffer for storing an encoded scan data.
  54. static uint8_t *mp_adv_handle; //!< Pointer to the advertising handle.
  55. /**@brief Struct that contains pointers to the encoded advertising data. */
  56. static ble_gap_adv_data_t m_adv_data =
  57. {
  58. .adv_data =
  59. {
  60. .p_data = m_enc_advdata,
  61. .len = BLE_GAP_ADV_SET_DATA_SIZE_MAX
  62. },
  63. .scan_rsp_data =
  64. {
  65. .p_data = m_enc_scan_response_data,
  66. .len = BLE_GAP_ADV_SET_DATA_SIZE_MAX
  67. }
  68. };
  69. /**@brief Function for invoking registered callback.
  70. *
  71. * @param[in] evt Event to issue to callback.
  72. */
  73. static void invoke_callback(es_adv_evt_t evt)
  74. {
  75. if (m_adv_evt_handler != NULL)
  76. {
  77. m_adv_evt_handler(evt);
  78. }
  79. }
  80. /**@brief Starting advertising.
  81. * @param[in] p_adv_params Advertisement parameters to use.
  82. */
  83. static void adv_start(ble_gap_adv_params_t * p_adv_params)
  84. {
  85. ret_code_t err_code = NRF_SUCCESS;
  86. es_tlm_adv_cnt_inc();
  87. err_code = sd_ble_gap_adv_set_configure(mp_adv_handle, &m_adv_data, p_adv_params);
  88. APP_ERROR_CHECK(err_code);
  89. err_code = sd_ble_gap_adv_start(*mp_adv_handle, BLE_CONN_CFG_TAG_DEFAULT);
  90. if (err_code != NRF_ERROR_BUSY && err_code != NRF_SUCCESS)
  91. {
  92. APP_ERROR_CHECK(err_code);
  93. }
  94. }
  95. /**@brief Given state of Eddystone beacon, get advertisement parameters. */
  96. static void get_adv_params(ble_gap_adv_params_t * p_adv_params,
  97. bool non_connectable,
  98. bool remain_connectable)
  99. {
  100. // Initialize advertising parameters (used when starting advertising).
  101. memset(p_adv_params, 0, sizeof(ble_gap_adv_params_t));
  102. // Non-connectable
  103. p_adv_params->properties.type = non_connectable
  104. ? BLE_GAP_ADV_TYPE_NONCONNECTABLE_NONSCANNABLE_UNDIRECTED
  105. : BLE_GAP_ADV_TYPE_CONNECTABLE_SCANNABLE_UNDIRECTED;
  106. p_adv_params->p_peer_addr = NULL; // Undirected advertisement.
  107. p_adv_params->filter_policy = BLE_GAP_ADV_FP_ANY;
  108. p_adv_params->primary_phy = BLE_GAP_PHY_1MBPS;
  109. if (non_connectable)
  110. {
  111. #ifdef MULTIPROTOCOL_802154_MODE
  112. /* In case the Eddystone component is used by multiprotocol example,
  113. calculate the interval taking into account that beacon may be sent with a delay.
  114. MULTIPROTOCOL_802154_MODE is defined for multiprotocol examples only.
  115. */
  116. p_adv_params->interval = MSEC_TO_UNITS(((m_adv_interval - MULTIPROT_BEACON_DELAY_MS) > 0 ? (m_adv_interval - MULTIPROT_BEACON_DELAY_MS) : m_adv_interval), UNIT_0_625_MS);
  117. #else
  118. p_adv_params->interval = MSEC_TO_UNITS(m_adv_interval, UNIT_0_625_MS);
  119. #endif // MULTIPROTOCOL_802154_MODE
  120. p_adv_params->duration = BLE_GAP_ADV_TIMEOUT_GENERAL_UNLIMITED;
  121. }
  122. else
  123. {
  124. p_adv_params->interval = MSEC_TO_UNITS(APP_CFG_CONNECTABLE_ADV_INTERVAL_MS, UNIT_0_625_MS);
  125. p_adv_params->duration = APP_CFG_CONNECTABLE_ADV_TIMEOUT;
  126. }
  127. }
  128. /**@brief Update advertisement data and start connectable advertisements. */
  129. static void connectable_adv_start(void)
  130. {
  131. ble_gap_adv_params_t connectable_adv_params;
  132. ble_advdata_t scrsp_data;
  133. ble_uuid_t scrp_uuids[] = {{BLE_UUID_ESCS_SERVICE, m_ecs_uuid_type}};
  134. memset(&scrsp_data, 0, sizeof(scrsp_data));
  135. scrsp_data.name_type = BLE_ADVDATA_FULL_NAME;
  136. scrsp_data.include_appearance = false;
  137. scrsp_data.uuids_complete.uuid_cnt = sizeof(scrp_uuids) / sizeof(scrp_uuids[0]);
  138. scrsp_data.uuids_complete.p_uuids = scrp_uuids;
  139. m_adv_data.scan_rsp_data.p_data = m_enc_scan_response_data;
  140. m_adv_data.scan_rsp_data.len = BLE_GAP_ADV_SET_DATA_SIZE_MAX;
  141. // As the data to be written does not depend on the slot_no, we can safely send
  142. es_adv_frame_fill_connectable_adv_data(&scrsp_data, &m_adv_data);
  143. get_adv_params(&connectable_adv_params, false, m_remain_connectable);
  144. adv_start(&connectable_adv_params);
  145. invoke_callback(ES_ADV_EVT_CONNECTABLE_ADV_STARTED);
  146. }
  147. static void adv_stop(void)
  148. {
  149. ret_code_t err_code;
  150. err_code = sd_ble_gap_adv_stop(*mp_adv_handle);
  151. if (err_code != NRF_ERROR_INVALID_STATE)
  152. {
  153. APP_ERROR_CHECK(err_code);
  154. }
  155. es_adv_timing_stop();
  156. }
  157. static void adv_restart(void)
  158. {
  159. if (!m_remain_connectable)
  160. {
  161. es_adv_start_non_connctable_adv();
  162. }
  163. else
  164. {
  165. connectable_adv_start();
  166. }
  167. }
  168. /**@brief Function handling events from @ref es_adv_timing.c.
  169. *
  170. * @param[in] p_evt Advertisement timing event.
  171. */
  172. static void adv_timing_callback(const es_adv_timing_evt_t * p_evt)
  173. {
  174. ret_code_t err_code;
  175. ble_gap_adv_params_t non_connectable_adv_params;
  176. const es_slot_reg_t * p_reg = es_slot_get_registry();
  177. // As new advertisement data will be loaded, stop advertising.
  178. err_code = sd_ble_gap_adv_stop(*mp_adv_handle);
  179. if (err_code != NRF_ERROR_INVALID_STATE && err_code != BLE_ERROR_INVALID_ADV_HANDLE)
  180. {
  181. APP_ERROR_CHECK(err_code);
  182. }
  183. // If a non-eTLM frame is to be advertised.
  184. if (p_evt->evt_id == ES_ADV_TIMING_EVT_ADV_SLOT)
  185. {
  186. err_code = sd_ble_gap_tx_power_set(BLE_GAP_TX_POWER_ROLE_ADV, 0, p_reg->slots[p_evt->slot_no].radio_tx_pwr);
  187. if (err_code != BLE_ERROR_INVALID_ADV_HANDLE)
  188. {
  189. APP_ERROR_CHECK(err_code);
  190. }
  191. es_adv_frame_fill_non_connectable_adv_data(p_evt->slot_no, false, &m_adv_data);
  192. }
  193. // If an eTLM frame is to be advertised
  194. else if (p_evt->evt_id == ES_ADV_TIMING_EVT_ADV_ETLM)
  195. {
  196. err_code = sd_ble_gap_tx_power_set(BLE_GAP_TX_POWER_ROLE_ADV, 0, p_reg->slots[p_reg->tlm_slot].radio_tx_pwr);
  197. APP_ERROR_CHECK(err_code);
  198. es_adv_frame_fill_non_connectable_adv_data(p_evt->slot_no, true, &m_adv_data);
  199. }
  200. invoke_callback(ES_ADV_EVT_NON_CONN_ADV);
  201. get_adv_params(&non_connectable_adv_params, true, m_remain_connectable);
  202. adv_start(&non_connectable_adv_params);
  203. }
  204. void es_adv_start_connectable_adv(void)
  205. {
  206. if (!m_is_connected)
  207. {
  208. adv_stop();
  209. connectable_adv_start();
  210. }
  211. }
  212. void es_adv_start_non_connctable_adv(void)
  213. {
  214. es_adv_timing_start(m_adv_interval);
  215. }
  216. void es_adv_remain_connectable_set(bool remain_connectable)
  217. {
  218. m_remain_connectable = remain_connectable;
  219. }
  220. bool es_adv_remain_connectable_get(void)
  221. {
  222. return m_remain_connectable;
  223. }
  224. void es_adv_on_ble_evt(ble_evt_t const * p_ble_evt)
  225. {
  226. switch (p_ble_evt->header.evt_id)
  227. {
  228. case BLE_GAP_EVT_CONNECTED:
  229. m_is_connected = true;
  230. // The beacon must provide these advertisements for the client to see updated values
  231. // during the connection.
  232. es_adv_start_non_connctable_adv();
  233. break;
  234. case BLE_GAP_EVT_DISCONNECTED:
  235. m_is_connected = false;
  236. // Stop all advertising to give some time for writing to flash.
  237. adv_stop();
  238. adv_restart();
  239. break;
  240. case BLE_GAP_EVT_ADV_SET_TERMINATED:
  241. if (p_ble_evt->evt.gap_evt.params.adv_set_terminated.reason == BLE_GAP_EVT_ADV_SET_TERMINATED_REASON_TIMEOUT &&
  242. !m_is_connected)
  243. {
  244. invoke_callback(ES_ADV_EVT_CONNECTABLE_ADV_STOPPED);
  245. adv_restart();
  246. }
  247. break;
  248. default:
  249. break;
  250. }
  251. }
  252. void es_adv_interval_set(nrf_ble_escs_adv_interval_t interval)
  253. {
  254. const es_slot_reg_t * p_reg = es_slot_get_registry();
  255. uint16_t min_valid_adv_interval;
  256. bool eTLM_required = (p_reg->num_configured_eid_slots > 0) && (p_reg->tlm_configured);
  257. min_valid_adv_interval = eTLM_required ? \
  258. p_reg->num_configured_slots * (APP_CONFIG_ADV_FRAME_SPACING_MS_MIN + \
  259. APP_CONFIG_ADV_FRAME_ETLM_SPACING_MS) \
  260. : \
  261. p_reg->num_configured_slots * APP_CONFIG_ADV_FRAME_SPACING_MS_MIN;
  262. m_adv_interval = (interval > min_valid_adv_interval) ? interval : min_valid_adv_interval;
  263. #ifdef APP_CONFIG_ADV_INTERVAL_MS_MAX
  264. if (m_adv_interval > APP_CONFIG_ADV_INTERVAL_MS_MAX)
  265. {
  266. m_adv_interval = APP_CONFIG_ADV_INTERVAL_MS_MAX;
  267. }
  268. #endif // APP_CONFIG_ADV_INTERVAL_MS_MAX
  269. }
  270. nrf_ble_escs_adv_interval_t es_adv_interval_get(void)
  271. {
  272. return m_adv_interval;
  273. }
  274. void es_adv_init(uint8_t ecs_uuid_type,
  275. es_adv_evt_handler_t adv_event_handler,
  276. nrf_ble_escs_adv_interval_t adv_interval,
  277. bool remain_connectable,
  278. uint8_t * const p_adv_handle)
  279. {
  280. m_ecs_uuid_type = ecs_uuid_type;
  281. m_adv_evt_handler = adv_event_handler;
  282. m_is_connected = false;
  283. m_remain_connectable = remain_connectable;
  284. m_adv_interval = adv_interval;
  285. mp_adv_handle = p_adv_handle;
  286. es_tlm_init();
  287. es_adv_timing_init(adv_timing_callback);
  288. }
  289. void es_adv_timers_init(void)
  290. {
  291. es_adv_timing_timers_init();
  292. }