ble_gap_evt_conn.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /**
  2. * Copyright (c) 2014 - 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 "ble.h"
  41. #include "ble_gap_evt_conn.h"
  42. #include <string.h>
  43. #include "ble_serialization.h"
  44. #include "cond_field_serialization.h"
  45. #include "ble_gap_struct_serialization.h"
  46. #include "ble_struct_serialization.h"
  47. #include "conn_ble_gap_sec_keys.h"
  48. #include "app_util.h"
  49. extern ser_ble_gap_conn_keyset_t m_conn_keys_table[];
  50. #ifndef S112
  51. uint32_t ble_gap_evt_adv_report_enc(ble_evt_t const * const p_event,
  52. uint32_t event_len,
  53. uint8_t * const p_buf,
  54. uint32_t * const p_buf_len)
  55. {
  56. SER_EVT_ENC_BEGIN(BLE_GAP_EVT_ADV_REPORT);
  57. SER_PUSH_uint16(&p_event->evt.gap_evt.conn_handle);
  58. SER_PUSH_FIELD(&p_event->evt.gap_evt.params.adv_report, ble_gap_evt_adv_report_t_enc);
  59. #if NRF_SD_BLE_API_VERSION > 5
  60. conn_ble_gap_scan_data_unset(false);
  61. #endif
  62. SER_EVT_ENC_END;
  63. }
  64. #endif //!S112
  65. uint32_t ble_gap_evt_auth_key_request_enc(ble_evt_t const * const p_event,
  66. uint32_t event_len,
  67. uint8_t * const p_buf,
  68. uint32_t * const p_buf_len)
  69. {
  70. SER_EVT_ENC_BEGIN(BLE_GAP_EVT_AUTH_KEY_REQUEST);
  71. SER_PUSH_uint16(&p_event->evt.gap_evt.conn_handle);
  72. SER_PUSH_uint8(&p_event->evt.gap_evt.params.auth_key_request.key_type);
  73. SER_EVT_ENC_END;
  74. }
  75. uint32_t ble_gap_evt_auth_status_enc(ble_evt_t const * const p_event,
  76. uint32_t event_len,
  77. uint8_t * const p_buf,
  78. uint32_t * const p_buf_len)
  79. {
  80. SER_EVT_ENC_BEGIN(BLE_GAP_EVT_AUTH_STATUS);
  81. SER_PUSH_uint16(&p_event->evt.gap_evt.conn_handle);
  82. SER_PUSH_FIELD(&p_event->evt.gap_evt.params.auth_status, ble_gap_evt_auth_status_t_enc);
  83. // keyset is an extension of standard event data - used to synchronize keys at application
  84. uint32_t conn_index;
  85. err_code = conn_ble_gap_sec_context_find(p_event->evt.gap_evt.conn_handle, &conn_index);
  86. if (err_code == NRF_SUCCESS)
  87. {
  88. SER_PUSH_FIELD(&(m_conn_keys_table[conn_index].keyset), ble_gap_sec_keyset_t_enc);
  89. err_code = conn_ble_gap_sec_context_destroy(p_event->evt.gap_evt.conn_handle);
  90. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  91. }
  92. else
  93. {
  94. err_code = NRF_SUCCESS;
  95. }
  96. SER_EVT_ENC_END;
  97. }
  98. uint32_t ble_gap_evt_conn_param_update_enc(ble_evt_t const * const p_event,
  99. uint32_t event_len,
  100. uint8_t * const p_buf,
  101. uint32_t * const p_buf_len)
  102. {
  103. SER_EVT_ENC_BEGIN(BLE_GAP_EVT_CONN_PARAM_UPDATE);
  104. SER_PUSH_uint16(&p_event->evt.gap_evt.conn_handle);
  105. SER_PUSH_FIELD(&p_event->evt.gap_evt.params.conn_param_update, ble_gap_evt_conn_param_update_t_enc);
  106. SER_EVT_ENC_END;
  107. }
  108. #ifndef S112
  109. uint32_t ble_gap_evt_conn_param_update_request_enc(ble_evt_t const * const p_event,
  110. uint32_t event_len,
  111. uint8_t * const p_buf,
  112. uint32_t * const p_buf_len)
  113. {
  114. SER_EVT_ENC_BEGIN(BLE_GAP_EVT_CONN_PARAM_UPDATE_REQUEST);
  115. SER_PUSH_uint16(&p_event->evt.gap_evt.conn_handle);
  116. SER_PUSH_FIELD(&p_event->evt.gap_evt.params.conn_param_update_request,
  117. ble_gap_evt_conn_param_update_request_t_enc);
  118. SER_EVT_ENC_END;
  119. }
  120. #endif //!S112
  121. uint32_t ble_gap_evt_conn_sec_update_enc(ble_evt_t const * const p_event,
  122. uint32_t event_len,
  123. uint8_t * const p_buf,
  124. uint32_t * const p_buf_len)
  125. {
  126. SER_EVT_ENC_BEGIN(BLE_GAP_EVT_CONN_SEC_UPDATE);
  127. SER_PUSH_uint16(&p_event->evt.gap_evt.conn_handle);
  128. SER_PUSH_FIELD(&p_event->evt.gap_evt.params.conn_sec_update, ble_gap_evt_conn_sec_update_t_enc);
  129. SER_EVT_ENC_END;
  130. }
  131. uint32_t ble_gap_evt_connected_enc(ble_evt_t const * const p_event,
  132. uint32_t event_len,
  133. uint8_t * const p_buf,
  134. uint32_t * const p_buf_len)
  135. {
  136. SER_EVT_ENC_BEGIN(BLE_GAP_EVT_CONNECTED);
  137. SER_PUSH_uint16(&p_event->evt.gap_evt.conn_handle);
  138. SER_PUSH_FIELD(&p_event->evt.gap_evt.params.connected, ble_gap_evt_connected_t_enc);
  139. SER_EVT_ENC_END;
  140. }
  141. uint32_t ble_gap_evt_disconnected_enc(ble_evt_t const * const p_event,
  142. uint32_t event_len,
  143. uint8_t * const p_buf,
  144. uint32_t * const p_buf_len)
  145. {
  146. SER_EVT_ENC_BEGIN(BLE_GAP_EVT_DISCONNECTED);
  147. SER_PUSH_uint16(&p_event->evt.gap_evt.conn_handle);
  148. SER_PUSH_FIELD(&p_event->evt.gap_evt.params.disconnected, ble_gap_evt_disconnected_t_enc);
  149. // If disconnected and context is not yet destroyed, destroy it now
  150. uint32_t conn_index;
  151. err_code = conn_ble_gap_sec_context_find(p_event->evt.gap_evt.conn_handle, &conn_index);
  152. if (err_code == NRF_SUCCESS)
  153. {
  154. err_code = conn_ble_gap_sec_context_destroy(p_event->evt.gap_evt.conn_handle);
  155. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  156. }
  157. err_code = NRF_SUCCESS;
  158. SER_EVT_ENC_END;
  159. }
  160. uint32_t ble_gap_evt_key_pressed_enc(ble_evt_t const * const p_event,
  161. uint32_t event_len,
  162. uint8_t * const p_buf,
  163. uint32_t * const p_buf_len)
  164. {
  165. SER_EVT_ENC_BEGIN(BLE_GAP_EVT_KEY_PRESSED);
  166. SER_PUSH_uint16(&p_event->evt.gap_evt.conn_handle);
  167. SER_PUSH_uint8(&p_event->evt.gap_evt.params.key_pressed.kp_not);
  168. SER_EVT_ENC_END;
  169. }
  170. uint32_t ble_gap_evt_lesc_dhkey_request_enc(ble_evt_t const * const p_event,
  171. uint32_t event_len,
  172. uint8_t * const p_buf,
  173. uint32_t * const p_buf_len)
  174. {
  175. SER_EVT_ENC_BEGIN(BLE_GAP_EVT_LESC_DHKEY_REQUEST);
  176. uint8_t ser_data = p_event->evt.gap_evt.params.lesc_dhkey_request.oobd_req & 0x01;
  177. SER_PUSH_uint16(&p_event->evt.gap_evt.conn_handle);
  178. SER_PUSH_COND(p_event->evt.gap_evt.params.lesc_dhkey_request.p_pk_peer, ble_gap_lesc_p256_pk_t_enc);
  179. SER_PUSH_uint8(&ser_data);
  180. SER_EVT_ENC_END;
  181. }
  182. #define PASSKEY_LEN sizeof (p_event->evt.gap_evt.params.passkey_display.passkey)
  183. uint32_t ble_gap_evt_passkey_display_enc(ble_evt_t const * const p_event,
  184. uint32_t event_len,
  185. uint8_t * const p_buf,
  186. uint32_t * const p_buf_len)
  187. {
  188. SER_EVT_ENC_BEGIN(BLE_GAP_EVT_PASSKEY_DISPLAY);
  189. uint8_t ser_data = p_event->evt.gap_evt.params.passkey_display.match_request & 0x01;
  190. SER_PUSH_uint16(&p_event->evt.gap_evt.conn_handle);
  191. SER_PUSH_uint8array(p_event->evt.gap_evt.params.passkey_display.passkey, BLE_GAP_PASSKEY_LEN);
  192. SER_PUSH_uint8(&ser_data);
  193. SER_EVT_ENC_END;
  194. }
  195. uint32_t ble_gap_evt_rssi_changed_enc(ble_evt_t const * const p_event,
  196. uint32_t event_len,
  197. uint8_t * const p_buf,
  198. uint32_t * const p_buf_len)
  199. {
  200. SER_EVT_ENC_BEGIN(BLE_GAP_EVT_RSSI_CHANGED);
  201. SER_PUSH_uint16(&p_event->evt.gap_evt.conn_handle);
  202. SER_PUSH_int8(&p_event->evt.gap_evt.params.rssi_changed.rssi);
  203. #if defined(NRF_SD_BLE_API_VERSION) && NRF_SD_BLE_API_VERSION > 5
  204. SER_PUSH_uint8(&p_event->evt.gap_evt.params.rssi_changed.ch_index);
  205. #endif
  206. SER_EVT_ENC_END;
  207. }
  208. uint32_t ble_gap_evt_scan_req_report_enc(ble_evt_t const * const p_event,
  209. uint32_t event_len,
  210. uint8_t * const p_buf,
  211. uint32_t * const p_buf_len)
  212. {
  213. SER_EVT_ENC_BEGIN(BLE_GAP_EVT_SCAN_REQ_REPORT);
  214. SER_PUSH_uint16(&p_event->evt.gap_evt.conn_handle);
  215. #if defined(NRF_SD_BLE_API_VERSION) && NRF_SD_BLE_API_VERSION > 5
  216. SER_PUSH_uint8(&p_event->evt.gap_evt.params.scan_req_report.adv_handle);
  217. #endif
  218. SER_PUSH_FIELD(&p_event->evt.gap_evt.params.scan_req_report.peer_addr, ble_gap_addr_t_enc);
  219. SER_PUSH_int8(&p_event->evt.gap_evt.params.scan_req_report.rssi);
  220. SER_EVT_ENC_END;
  221. }
  222. uint32_t ble_gap_evt_sec_info_request_enc(ble_evt_t const * const p_event,
  223. uint32_t event_len,
  224. uint8_t * const p_buf,
  225. uint32_t * const p_buf_len)
  226. {
  227. SER_EVT_ENC_BEGIN(BLE_GAP_EVT_SEC_INFO_REQUEST);
  228. SER_PUSH_uint16(&p_event->evt.gap_evt.conn_handle);
  229. SER_PUSH_FIELD(&p_event->evt.gap_evt.params.sec_info_request, ble_gap_evt_sec_info_request_t_enc);
  230. SER_EVT_ENC_END;
  231. }
  232. uint32_t ble_gap_evt_sec_params_request_enc(ble_evt_t const * const p_event,
  233. uint32_t event_len,
  234. uint8_t * const p_buf,
  235. uint32_t * const p_buf_len)
  236. {
  237. SER_EVT_ENC_BEGIN(BLE_GAP_EVT_SEC_PARAMS_REQUEST);
  238. SER_PUSH_uint16(&p_event->evt.gap_evt.conn_handle);
  239. SER_PUSH_FIELD(&p_event->evt.gap_evt.params.sec_params_request, ble_gap_evt_sec_params_request_t_enc);
  240. SER_EVT_ENC_END;
  241. }
  242. uint32_t ble_gap_evt_sec_request_enc(ble_evt_t const * const p_event,
  243. uint32_t event_len,
  244. uint8_t * const p_buf,
  245. uint32_t * const p_buf_len)
  246. {
  247. SER_EVT_ENC_BEGIN(BLE_GAP_EVT_SEC_REQUEST);
  248. SER_PUSH_uint16(&p_event->evt.gap_evt.conn_handle);
  249. SER_PUSH_FIELD(&p_event->evt.gap_evt.params.sec_request, ble_gap_evt_sec_request_t_enc);
  250. SER_EVT_ENC_END;
  251. }
  252. uint32_t ble_gap_evt_timeout_enc(ble_evt_t const * const p_event,
  253. uint32_t event_len,
  254. uint8_t * const p_buf,
  255. uint32_t * const p_buf_len)
  256. {
  257. SER_EVT_ENC_BEGIN(BLE_GAP_EVT_TIMEOUT);
  258. SER_PUSH_uint16(&p_event->evt.gap_evt.conn_handle);
  259. SER_PUSH_uint8(&p_event->evt.gap_evt.params.timeout.src);
  260. #if defined(NRF_SD_BLE_API_VERSION) && NRF_SD_BLE_API_VERSION > 5 && !defined(S112)
  261. if (p_event->evt.gap_evt.params.timeout.src == BLE_GAP_TIMEOUT_SRC_SCAN)
  262. {
  263. SER_PUSH_FIELD(&p_event->evt.gap_evt.params.timeout.params.adv_report_buffer, ble_data_t_enc);
  264. }
  265. #endif
  266. SER_EVT_ENC_END;
  267. }
  268. #if NRF_SD_BLE_API_VERSION >= 5
  269. uint32_t ble_gap_evt_phy_update_enc(ble_evt_t const * const p_event,
  270. uint32_t event_len,
  271. uint8_t * const p_buf,
  272. uint32_t * const p_buf_len)
  273. {
  274. SER_EVT_ENC_BEGIN(BLE_GAP_EVT_PHY_UPDATE);
  275. SER_PUSH_uint16(&p_event->evt.gap_evt.conn_handle);
  276. SER_PUSH_uint8(&p_event->evt.gap_evt.params.phy_update.status);
  277. SER_PUSH_uint8(&p_event->evt.gap_evt.params.phy_update.tx_phy);
  278. SER_PUSH_uint8(&p_event->evt.gap_evt.params.phy_update.rx_phy);
  279. SER_EVT_ENC_END;
  280. }
  281. uint32_t ble_gap_evt_phy_update_request_enc(ble_evt_t const * const p_event,
  282. uint32_t event_len,
  283. uint8_t * const p_buf,
  284. uint32_t * const p_buf_len)
  285. {
  286. SER_EVT_ENC_BEGIN(BLE_GAP_EVT_PHY_UPDATE_REQUEST);
  287. SER_PUSH_uint16(&p_event->evt.gap_evt.conn_handle);
  288. SER_PUSH_FIELD(&p_event->evt.gap_evt.params.phy_update_request, ble_gap_phys_t_enc);
  289. SER_EVT_ENC_END;
  290. }
  291. #endif
  292. #if NRF_SD_BLE_API_VERSION >= 4 && !defined(S112)
  293. uint32_t ble_gap_evt_data_length_update_request_enc(ble_evt_t const * const p_event,
  294. uint32_t event_len,
  295. uint8_t * const p_buf,
  296. uint32_t * const p_buf_len)
  297. {
  298. SER_EVT_ENC_BEGIN(BLE_GAP_EVT_DATA_LENGTH_UPDATE_REQUEST);
  299. SER_PUSH_uint16(&p_event->evt.gap_evt.conn_handle);
  300. SER_PUSH_FIELD(&p_event->evt.gap_evt.params.data_length_update_request.peer_params, ble_gap_data_length_params_t_enc);
  301. SER_EVT_ENC_END;
  302. }
  303. uint32_t ble_gap_evt_data_length_update_enc(ble_evt_t const * const p_event,
  304. uint32_t event_len,
  305. uint8_t * const p_buf,
  306. uint32_t * const p_buf_len)
  307. {
  308. SER_EVT_ENC_BEGIN(BLE_GAP_EVT_DATA_LENGTH_UPDATE);
  309. SER_PUSH_uint16(&p_event->evt.gap_evt.conn_handle);
  310. SER_PUSH_FIELD(&p_event->evt.gap_evt.params.data_length_update.effective_params, ble_gap_data_length_params_t_enc);
  311. SER_EVT_ENC_END;
  312. }
  313. #endif // NRF_SD_BLE_API_VERSION >= 4 && !defined(S112)
  314. #if NRF_SD_BLE_API_VERSION > 5
  315. uint32_t ble_gap_evt_adv_set_terminated_enc(ble_evt_t const * const p_event,
  316. uint32_t event_len,
  317. uint8_t * const p_buf,
  318. uint32_t * const p_buf_len)
  319. {
  320. SER_EVT_ENC_BEGIN(BLE_GAP_EVT_ADV_SET_TERMINATED);
  321. SER_PUSH_uint16(&p_event->evt.gap_evt.conn_handle);
  322. SER_PUSH_FIELD(&p_event->evt.gap_evt.params.adv_set_terminated, ble_gap_evt_adv_set_terminated_t_enc);
  323. SER_EVT_ENC_END;
  324. }
  325. #ifndef S112
  326. uint32_t ble_gap_evt_qos_channel_survey_report_enc(ble_evt_t const * const p_event,
  327. uint32_t event_len,
  328. uint8_t * const p_buf,
  329. uint32_t * const p_buf_len)
  330. {
  331. SER_EVT_ENC_BEGIN(BLE_GAP_EVT_QOS_CHANNEL_SURVEY_REPORT);
  332. SER_PUSH_uint16(&p_event->evt.gap_evt.conn_handle);
  333. SER_PUSH_uint8array((uint8_t *)p_event->evt.gap_evt.params.qos_channel_survey_report.channel_energy,
  334. BLE_GAP_CHANNEL_COUNT);
  335. SER_EVT_ENC_END;
  336. }
  337. #endif //!S112
  338. #endif//NRF_SD_BLE_API_VERSION > 5