ble_gap_evt_app.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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_gap_evt_app.h"
  41. #include "ble_serialization.h"
  42. #include "app_util.h"
  43. #include "app_ble_gap_sec_keys.h"
  44. #include "ble_gap_struct_serialization.h"
  45. #include "ble_struct_serialization.h"
  46. #include "cond_field_serialization.h"
  47. #include <string.h>
  48. extern ser_ble_gap_app_keyset_t m_app_keys_table[];
  49. #ifndef S112
  50. uint32_t ble_gap_evt_adv_report_dec(uint8_t const * const p_buf,
  51. uint32_t packet_len,
  52. ble_evt_t * const p_event,
  53. uint32_t * const p_event_len)
  54. {
  55. SER_EVT_DEC_BEGIN(BLE_GAP_EVT_ADV_REPORT, gap, adv_report);
  56. SER_PULL_uint16(&p_event->evt.gap_evt.conn_handle);
  57. #if defined(NRF_SD_BLE_API_VERSION) && NRF_SD_BLE_API_VERSION >= 6
  58. //get buffer stored during scan start.
  59. app_ble_gap_scan_data_unset(false);
  60. #endif
  61. SER_PULL_FIELD(&p_event->evt.gap_evt.params.adv_report, ble_gap_evt_adv_report_t_dec);
  62. SER_EVT_DEC_END;
  63. }
  64. #endif //!S112
  65. uint32_t ble_gap_evt_auth_key_request_dec(uint8_t const * const p_buf,
  66. uint32_t packet_len,
  67. ble_evt_t * const p_event,
  68. uint32_t * const p_event_len)
  69. {
  70. SER_EVT_DEC_BEGIN(BLE_GAP_EVT_AUTH_KEY_REQUEST, gap, auth_key_request);
  71. SER_PULL_uint16(&p_event->evt.gap_evt.conn_handle);
  72. SER_PULL_uint8(&p_event->evt.gap_evt.params.auth_key_request.key_type);
  73. SER_EVT_DEC_END;
  74. }
  75. extern ser_ble_gap_app_keyset_t m_app_keys_table[];
  76. uint32_t ble_gap_evt_auth_status_dec(uint8_t const * const p_buf,
  77. uint32_t packet_len,
  78. ble_evt_t * const p_event,
  79. uint32_t * const p_event_len)
  80. {
  81. SER_EVT_DEC_BEGIN(BLE_GAP_EVT_AUTH_STATUS, gap, auth_status);
  82. SER_PULL_uint16(&p_event->evt.gap_evt.conn_handle);
  83. SER_PULL_FIELD(&p_event->evt.gap_evt.params.auth_status, ble_gap_evt_auth_status_t_dec);
  84. // keyset is an extension of standard event data - used to synchronize keys at application
  85. uint32_t conn_index;
  86. err_code = app_ble_gap_sec_context_find(p_event->evt.gap_evt.conn_handle, &conn_index);
  87. if (err_code == NRF_SUCCESS)
  88. {
  89. SER_PULL_FIELD(&(m_app_keys_table[conn_index].keyset), ble_gap_sec_keyset_t_dec);
  90. err_code = app_ble_gap_sec_context_destroy(p_event->evt.gap_evt.conn_handle);
  91. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  92. }
  93. SER_EVT_DEC_END;
  94. }
  95. uint32_t ble_gap_evt_conn_param_update_dec(uint8_t const * const p_buf,
  96. uint32_t packet_len,
  97. ble_evt_t * const p_event,
  98. uint32_t * const p_event_len)
  99. {
  100. SER_EVT_DEC_BEGIN(BLE_GAP_EVT_CONN_PARAM_UPDATE, gap, conn_param_update);
  101. SER_PULL_uint16(&p_event->evt.gap_evt.conn_handle);
  102. SER_PULL_FIELD(&p_event->evt.gap_evt.params.conn_param_update, ble_gap_evt_conn_param_update_t_dec);
  103. SER_EVT_DEC_END;
  104. }
  105. #ifndef S112
  106. uint32_t ble_gap_evt_conn_param_update_request_dec(uint8_t const * const p_buf,
  107. uint32_t packet_len,
  108. ble_evt_t * const p_event,
  109. uint32_t * const p_event_len)
  110. {
  111. SER_EVT_DEC_BEGIN(BLE_GAP_EVT_CONN_PARAM_UPDATE_REQUEST, gap, conn_param_update_request);
  112. SER_PULL_uint16(&p_event->evt.gap_evt.conn_handle);
  113. SER_PULL_FIELD(&p_event->evt.gap_evt.params.conn_param_update_request,
  114. ble_gap_evt_conn_param_update_request_t_dec);
  115. SER_EVT_DEC_END;
  116. }
  117. #endif
  118. uint32_t ble_gap_evt_conn_sec_update_dec(uint8_t const * const p_buf,
  119. uint32_t packet_len,
  120. ble_evt_t * const p_event,
  121. uint32_t * const p_event_len)
  122. {
  123. SER_EVT_DEC_BEGIN(BLE_GAP_EVT_CONN_SEC_UPDATE, gap, conn_sec_update);
  124. SER_PULL_uint16(&p_event->evt.gap_evt.conn_handle);
  125. SER_PULL_FIELD(&p_event->evt.gap_evt.params.conn_sec_update, ble_gap_evt_conn_sec_update_t_dec);
  126. SER_EVT_DEC_END;
  127. }
  128. uint32_t ble_gap_evt_connected_dec(uint8_t const * const p_buf,
  129. uint32_t packet_len,
  130. ble_evt_t * const p_event,
  131. uint32_t * const p_event_len)
  132. {
  133. SER_EVT_DEC_BEGIN(BLE_GAP_EVT_CONNECTED, gap, connected);
  134. SER_PULL_uint16(&p_event->evt.gap_evt.conn_handle);
  135. SER_PULL_FIELD(&p_event->evt.gap_evt.params.connected, ble_gap_evt_connected_t_dec);
  136. SER_EVT_DEC_END;
  137. }
  138. uint32_t ble_gap_evt_disconnected_dec(uint8_t const * const p_buf,
  139. uint32_t packet_len,
  140. ble_evt_t * const p_event,
  141. uint32_t * const p_event_len)
  142. {
  143. SER_EVT_DEC_BEGIN(BLE_GAP_EVT_DISCONNECTED, gap, disconnected);
  144. SER_PULL_uint16(&p_event->evt.gap_evt.conn_handle);
  145. SER_PULL_FIELD(&p_event->evt.gap_evt.params.disconnected, ble_gap_evt_disconnected_t_dec);
  146. SER_EVT_DEC_END;
  147. }
  148. uint32_t ble_gap_evt_key_pressed_dec(uint8_t const * const p_buf,
  149. uint32_t packet_len,
  150. ble_evt_t * const p_event,
  151. uint32_t * const p_event_len)
  152. {
  153. SER_EVT_DEC_BEGIN(BLE_GAP_EVT_KEY_PRESSED, gap, key_pressed);
  154. SER_PULL_uint16(&p_event->evt.gap_evt.conn_handle);
  155. SER_PULL_uint8(&p_event->evt.gap_evt.params.key_pressed.kp_not);
  156. SER_EVT_DEC_END;
  157. }
  158. uint32_t ble_gap_evt_lesc_dhkey_request_dec(uint8_t const * const p_buf,
  159. uint32_t packet_len,
  160. ble_evt_t * const p_event,
  161. uint32_t * const p_event_len)
  162. {
  163. SER_EVT_DEC_BEGIN(BLE_GAP_EVT_LESC_DHKEY_REQUEST, gap, lesc_dhkey_request);
  164. uint8_t ser_data;
  165. SER_PULL_uint16(&p_event->evt.gap_evt.conn_handle);
  166. // keyset is an extension of standard event data - used to synchronize keys at application
  167. uint32_t conn_index;
  168. err_code = app_ble_gap_sec_context_find(p_event->evt.gap_evt.conn_handle, &conn_index);
  169. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  170. p_event->evt.gap_evt.params.lesc_dhkey_request.p_pk_peer = m_app_keys_table[conn_index].keyset.keys_peer.p_pk;
  171. SER_PULL_COND(&p_event->evt.gap_evt.params.lesc_dhkey_request.p_pk_peer, ble_gap_lesc_p256_pk_t_dec);
  172. SER_PULL_uint8(&ser_data);
  173. p_event->evt.gap_evt.params.lesc_dhkey_request.oobd_req = ser_data & 0x01;
  174. SER_EVT_DEC_END;
  175. }
  176. #define PASSKEY_LEN sizeof (p_event->evt.gap_evt.params.passkey_display.passkey)
  177. uint32_t ble_gap_evt_passkey_display_dec(uint8_t const * const p_buf,
  178. uint32_t packet_len,
  179. ble_evt_t * const p_event,
  180. uint32_t * const p_event_len)
  181. {
  182. SER_EVT_DEC_BEGIN(BLE_GAP_EVT_PASSKEY_DISPLAY, gap, passkey_display);
  183. uint8_t ser_data;
  184. SER_PULL_uint16(&p_event->evt.gap_evt.conn_handle);
  185. SER_PULL_uint8array(p_event->evt.gap_evt.params.passkey_display.passkey, BLE_GAP_PASSKEY_LEN);
  186. SER_PULL_uint8(&ser_data);
  187. p_event->evt.gap_evt.params.passkey_display.match_request = (ser_data & 0x01);
  188. SER_EVT_DEC_END;
  189. }
  190. uint32_t ble_gap_evt_rssi_changed_dec(uint8_t const * const p_buf,
  191. uint32_t packet_len,
  192. ble_evt_t * const p_event,
  193. uint32_t * const p_event_len)
  194. {
  195. SER_EVT_DEC_BEGIN(BLE_GAP_EVT_RSSI_CHANGED, gap, rssi_changed);
  196. SER_PULL_uint16(&p_event->evt.gap_evt.conn_handle);
  197. SER_PULL_int8(&p_event->evt.gap_evt.params.rssi_changed.rssi);
  198. #if defined(NRF_SD_BLE_API_VERSION) && NRF_SD_BLE_API_VERSION > 5
  199. SER_PULL_uint8(&p_event->evt.gap_evt.params.rssi_changed.ch_index);
  200. #endif
  201. SER_EVT_DEC_END;
  202. }
  203. uint32_t ble_gap_evt_scan_req_report_dec(uint8_t const * const p_buf,
  204. uint32_t packet_len,
  205. ble_evt_t * const p_event,
  206. uint32_t * const p_event_len)
  207. {
  208. SER_EVT_DEC_BEGIN(BLE_GAP_EVT_SCAN_REQ_REPORT, gap, scan_req_report);
  209. SER_PULL_uint16(&p_event->evt.gap_evt.conn_handle);
  210. #if defined(NRF_SD_BLE_API_VERSION) && NRF_SD_BLE_API_VERSION > 5
  211. SER_PULL_uint8(&p_event->evt.gap_evt.params.scan_req_report.adv_handle);
  212. #endif
  213. SER_PULL_FIELD(&p_event->evt.gap_evt.params.scan_req_report.peer_addr, ble_gap_addr_t_dec);
  214. SER_PULL_int8(&p_event->evt.gap_evt.params.scan_req_report.rssi);
  215. SER_EVT_DEC_END;
  216. }
  217. uint32_t ble_gap_evt_sec_info_request_dec(uint8_t const * const p_buf,
  218. uint32_t packet_len,
  219. ble_evt_t * const p_event,
  220. uint32_t * const p_event_len)
  221. {
  222. SER_EVT_DEC_BEGIN(BLE_GAP_EVT_SEC_INFO_REQUEST, gap, sec_info_request);
  223. SER_PULL_uint16(&p_event->evt.gap_evt.conn_handle);
  224. SER_PULL_FIELD(&p_event->evt.gap_evt.params.sec_info_request, ble_gap_evt_sec_info_request_t_dec);
  225. SER_EVT_DEC_END;
  226. }
  227. uint32_t ble_gap_evt_sec_params_request_dec(uint8_t const * const p_buf,
  228. uint32_t packet_len,
  229. ble_evt_t * const p_event,
  230. uint32_t * const p_event_len)
  231. {
  232. SER_EVT_DEC_BEGIN(BLE_GAP_EVT_SEC_PARAMS_REQUEST, gap, sec_params_request);
  233. SER_PULL_uint16(&p_event->evt.gap_evt.conn_handle);
  234. SER_PULL_FIELD(&p_event->evt.gap_evt.params.sec_params_request, ble_gap_evt_sec_params_request_t_dec);
  235. SER_EVT_DEC_END;
  236. }
  237. uint32_t ble_gap_evt_sec_request_dec(uint8_t const * const p_buf,
  238. uint32_t packet_len,
  239. ble_evt_t * const p_event,
  240. uint32_t * const p_event_len)
  241. {
  242. SER_EVT_DEC_BEGIN(BLE_GAP_EVT_SEC_REQUEST, gap, sec_request);
  243. SER_PULL_uint16(&p_event->evt.gap_evt.conn_handle);
  244. SER_PULL_FIELD(&p_event->evt.gap_evt.params.sec_request, ble_gap_evt_sec_request_t_dec);
  245. SER_EVT_DEC_END;
  246. }
  247. uint32_t ble_gap_evt_timeout_dec(uint8_t const * const p_buf,
  248. uint32_t packet_len,
  249. ble_evt_t * const p_event,
  250. uint32_t * const p_event_len)
  251. {
  252. SER_EVT_DEC_BEGIN(BLE_GAP_EVT_TIMEOUT, gap, timeout);
  253. SER_PULL_uint16(&p_event->evt.gap_evt.conn_handle);
  254. SER_PULL_uint8(&p_event->evt.gap_evt.params.timeout.src);
  255. #if defined(NRF_SD_BLE_API_VERSION) && (NRF_SD_BLE_API_VERSION > 5) && !defined(S112)
  256. if (p_event->evt.gap_evt.params.timeout.src == BLE_GAP_TIMEOUT_SRC_SCAN)
  257. {
  258. SER_PULL_FIELD(&p_event->evt.gap_evt.params.timeout.params.adv_report_buffer, ble_data_t_dec);
  259. }
  260. #endif
  261. SER_EVT_DEC_END;
  262. }
  263. #if NRF_SD_BLE_API_VERSION >= 5
  264. uint32_t ble_gap_evt_phy_update_dec(uint8_t const * const p_buf,
  265. uint32_t packet_len,
  266. ble_evt_t * const p_event,
  267. uint32_t * const p_event_len)
  268. {
  269. SER_EVT_DEC_BEGIN(BLE_GAP_EVT_PHY_UPDATE, gap, phy_update);
  270. SER_PULL_uint16(&p_event->evt.gap_evt.conn_handle);
  271. SER_PULL_uint8(&p_event->evt.gap_evt.params.phy_update.status);
  272. SER_PULL_uint8(&p_event->evt.gap_evt.params.phy_update.tx_phy);
  273. SER_PULL_uint8(&p_event->evt.gap_evt.params.phy_update.rx_phy);
  274. SER_EVT_DEC_END;
  275. }
  276. uint32_t ble_gap_evt_phy_update_request_dec(uint8_t const * const p_buf,
  277. uint32_t packet_len,
  278. ble_evt_t * const p_event,
  279. uint32_t * const p_event_len)
  280. {
  281. SER_EVT_DEC_BEGIN(BLE_GAP_EVT_PHY_UPDATE_REQUEST, gap, phy_update);
  282. SER_PULL_uint16(&p_event->evt.gap_evt.conn_handle);
  283. SER_PULL_FIELD(&p_event->evt.gap_evt.params.phy_update_request, ble_gap_phys_t_dec);
  284. SER_EVT_DEC_END;
  285. }
  286. #endif
  287. #if NRF_SD_BLE_API_VERSION >= 4 && !defined(S112)
  288. uint32_t ble_gap_evt_data_length_update_request_dec(uint8_t const * const p_buf,
  289. uint32_t packet_len,
  290. ble_evt_t * const p_event,
  291. uint32_t * const p_event_len)
  292. {
  293. SER_EVT_DEC_BEGIN(BLE_GAP_EVT_DATA_LENGTH_UPDATE_REQUEST, gap, timeout);
  294. SER_PULL_uint16(&p_event->evt.gap_evt.conn_handle);
  295. SER_PULL_FIELD(&p_event->evt.gap_evt.params.data_length_update_request.peer_params, ble_gap_data_length_params_t_dec);
  296. SER_EVT_DEC_END;
  297. }
  298. uint32_t ble_gap_evt_data_length_update_dec(uint8_t const * const p_buf,
  299. uint32_t packet_len,
  300. ble_evt_t * const p_event,
  301. uint32_t * const p_event_len)
  302. {
  303. SER_EVT_DEC_BEGIN(BLE_GAP_EVT_DATA_LENGTH_UPDATE, gap, timeout);
  304. SER_PULL_uint16(&p_event->evt.gap_evt.conn_handle);
  305. SER_PULL_FIELD(&p_event->evt.gap_evt.params.data_length_update.effective_params, ble_gap_data_length_params_t_dec);
  306. SER_EVT_DEC_END;
  307. }
  308. #endif //NRF_SD_BLE_API_VERSION >= 4 @@ !defined(S112)
  309. #if NRF_SD_BLE_API_VERSION > 5
  310. #ifndef S112
  311. uint32_t ble_gap_evt_qos_channel_survey_report_dec(uint8_t const * const p_buf,
  312. uint32_t packet_len,
  313. ble_evt_t * const p_event,
  314. uint32_t * const p_event_len)
  315. {
  316. SER_EVT_DEC_BEGIN(BLE_GAP_EVT_QOS_CHANNEL_SURVEY_REPORT, gap, qos_channel_survey_report);
  317. SER_PULL_uint16(&p_event->evt.gap_evt.conn_handle);
  318. SER_PULL_uint8array((uint8_t *)p_event->evt.gap_evt.params.qos_channel_survey_report.channel_energy, BLE_GAP_CHANNEL_COUNT);
  319. SER_EVT_DEC_END;
  320. }
  321. #endif //S112
  322. uint32_t ble_gap_evt_adv_set_terminated_dec(uint8_t const * const p_buf,
  323. uint32_t packet_len,
  324. ble_evt_t * const p_event,
  325. uint32_t * const p_event_len)
  326. {
  327. SER_EVT_DEC_BEGIN(BLE_GAP_EVT_ADV_SET_TERMINATED, gap, adv_set_terminated);
  328. SER_PULL_uint16(&p_event->evt.gap_evt.conn_handle);
  329. SER_PULL_FIELD(&p_event->evt.gap_evt.params.adv_set_terminated, ble_gap_evt_adv_set_terminated_t_dec);
  330. SER_EVT_DEC_END;
  331. }
  332. #endif