es_adv.c 12 KB

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