conn_mw_ble_gap.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318
  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_conn.h"
  41. #include "ser_config.h"
  42. #include "conn_mw_ble_gap.h"
  43. #include "ble_serialization.h"
  44. #include "conn_ble_gap_sec_keys.h"
  45. #include <stddef.h>
  46. extern ser_ble_gap_conn_keyset_t m_conn_keys_table[SER_MAX_CONNECTIONS];
  47. #if NRF_SD_BLE_API_VERSION > 5 && !defined(S112)
  48. static uint8_t * mp_scan_data;
  49. #endif
  50. /* Id used to identify buffer allocated for scan reports. */
  51. #define SCAN_BUFFER_ID 1
  52. #ifndef S112
  53. uint32_t conn_mw_ble_gap_connect(uint8_t const * const p_rx_buf,
  54. uint32_t rx_buf_len,
  55. uint8_t * const p_tx_buf,
  56. uint32_t * const p_tx_buf_len)
  57. {
  58. SER_ASSERT_NOT_NULL(p_rx_buf);
  59. SER_ASSERT_NOT_NULL(p_tx_buf);
  60. SER_ASSERT_NOT_NULL(p_tx_buf_len);
  61. ble_gap_addr_t addr;
  62. ble_gap_addr_t * p_addr = &addr;
  63. ble_gap_scan_params_t scan_params;
  64. ble_gap_scan_params_t * p_scan_params = &scan_params;
  65. ble_gap_conn_params_t conn_params;
  66. ble_gap_conn_params_t * p_conn_params = &conn_params;
  67. uint32_t err_code = NRF_SUCCESS;
  68. uint32_t sd_err_code;
  69. #if NRF_SD_BLE_API_VERSION >= 4
  70. uint8_t conn_cfg_tag;
  71. err_code = ble_gap_connect_req_dec(p_rx_buf, rx_buf_len, &p_addr,
  72. &p_scan_params, &p_conn_params, &conn_cfg_tag);
  73. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  74. sd_err_code = sd_ble_gap_connect(p_addr, p_scan_params, p_conn_params, conn_cfg_tag);
  75. #else
  76. err_code = ble_gap_connect_req_dec(p_rx_buf, rx_buf_len, &p_addr,
  77. &p_scan_params, &p_conn_params);
  78. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  79. sd_err_code = sd_ble_gap_connect(p_addr, p_scan_params, p_conn_params);
  80. #endif
  81. err_code = ble_gap_connect_rsp_enc(sd_err_code, p_tx_buf, p_tx_buf_len);
  82. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  83. return err_code;
  84. }
  85. uint32_t conn_mw_ble_gap_connect_cancel(uint8_t const * const p_rx_buf,
  86. uint32_t rx_buf_len,
  87. uint8_t * const p_tx_buf,
  88. uint32_t * const p_tx_buf_len)
  89. {
  90. SER_ASSERT_NOT_NULL(p_rx_buf);
  91. SER_ASSERT_NOT_NULL(p_tx_buf);
  92. SER_ASSERT_NOT_NULL(p_tx_buf_len);
  93. uint32_t err_code = NRF_SUCCESS;
  94. uint32_t sd_err_code;
  95. sd_err_code = sd_ble_gap_connect_cancel();
  96. err_code = ble_gap_connect_cancel_rsp_enc(sd_err_code, p_tx_buf, p_tx_buf_len);
  97. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  98. return err_code;
  99. }
  100. uint32_t conn_mw_ble_gap_scan_start(uint8_t const * const p_rx_buf,
  101. uint32_t rx_buf_len,
  102. uint8_t * const p_tx_buf,
  103. uint32_t * const p_tx_buf_len)
  104. {
  105. SER_ASSERT_NOT_NULL(p_rx_buf);
  106. SER_ASSERT_NOT_NULL(p_tx_buf);
  107. SER_ASSERT_NOT_NULL(p_tx_buf_len);
  108. uint32_t err_code = NRF_SUCCESS;
  109. uint32_t sd_err_code;
  110. ble_gap_scan_params_t scan_params;
  111. ble_gap_scan_params_t * p_scan_params = &scan_params;
  112. #if defined(NRF_SD_BLE_API_VERSION) && NRF_SD_BLE_API_VERSION > 5
  113. ble_data_t adv_report_buffer;
  114. ble_data_t * p_adv_report_buffer = &adv_report_buffer;
  115. mp_scan_data = conn_ble_gap_ble_data_buf_alloc(SCAN_BUFFER_ID);
  116. adv_report_buffer.p_data = mp_scan_data;
  117. adv_report_buffer.len = SER_MAX_ADV_DATA;
  118. err_code = ble_gap_scan_start_req_dec(p_rx_buf, rx_buf_len, &p_scan_params, &p_adv_report_buffer);
  119. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  120. if (p_adv_report_buffer)
  121. {
  122. conn_ble_gap_scan_data_set(p_adv_report_buffer->p_data);
  123. }
  124. sd_err_code = sd_ble_gap_scan_start(p_scan_params, p_adv_report_buffer);
  125. if (sd_err_code != NRF_SUCCESS)
  126. {
  127. conn_ble_gap_scan_data_unset(true);
  128. }
  129. err_code = ble_gap_scan_start_rsp_enc(sd_err_code, p_adv_report_buffer, p_tx_buf, p_tx_buf_len);
  130. #else
  131. err_code = ble_gap_scan_start_req_dec(p_rx_buf, rx_buf_len, &p_scan_params);
  132. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  133. sd_err_code = sd_ble_gap_scan_start(p_scan_params);
  134. err_code = ble_gap_scan_start_rsp_enc(sd_err_code, p_tx_buf, p_tx_buf_len);
  135. #endif
  136. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  137. return err_code;
  138. }
  139. uint32_t conn_mw_ble_gap_scan_stop(uint8_t const * const p_rx_buf,
  140. uint32_t rx_buf_len,
  141. uint8_t * const p_tx_buf,
  142. uint32_t * const p_tx_buf_len)
  143. {
  144. SER_ASSERT_NOT_NULL(p_rx_buf);
  145. SER_ASSERT_NOT_NULL(p_tx_buf);
  146. SER_ASSERT_NOT_NULL(p_tx_buf_len);
  147. uint32_t err_code = NRF_SUCCESS;
  148. uint32_t sd_err_code;
  149. #if defined(NRF_SD_BLE_API_VERSION) && NRF_SD_BLE_API_VERSION > 5
  150. conn_ble_gap_scan_data_unset(true);
  151. #endif
  152. sd_err_code = sd_ble_gap_scan_stop();
  153. err_code = ble_gap_scan_stop_rsp_enc(sd_err_code, p_tx_buf, p_tx_buf_len);
  154. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  155. return err_code;
  156. }
  157. uint32_t conn_mw_ble_gap_encrypt(uint8_t const * const p_rx_buf,
  158. uint32_t rx_buf_len,
  159. uint8_t * const p_tx_buf,
  160. uint32_t * const p_tx_buf_len)
  161. {
  162. SER_ASSERT_NOT_NULL(p_rx_buf);
  163. SER_ASSERT_NOT_NULL(p_tx_buf);
  164. SER_ASSERT_NOT_NULL(p_tx_buf_len);
  165. uint32_t err_code = NRF_SUCCESS;
  166. uint32_t sd_err_code;
  167. uint16_t conn_handle;
  168. ble_gap_master_id_t master_id;
  169. ble_gap_master_id_t *p_master_id = &master_id;
  170. ble_gap_enc_info_t enc_info;
  171. ble_gap_enc_info_t *p_enc_info = &enc_info;
  172. err_code = ble_gap_encrypt_req_dec(p_rx_buf, rx_buf_len, &conn_handle, &p_master_id, &p_enc_info);
  173. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  174. sd_err_code = sd_ble_gap_encrypt(conn_handle, p_master_id, p_enc_info);
  175. err_code = ble_gap_encrypt_rsp_enc(sd_err_code, p_tx_buf, p_tx_buf_len);
  176. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  177. return err_code;
  178. }
  179. #endif //!S112
  180. #if defined(NRF_SD_BLE_API_VERSION) && NRF_SD_BLE_API_VERSION < 6
  181. uint32_t conn_mw_ble_gap_adv_data_set(uint8_t const * const p_rx_buf,
  182. uint32_t rx_buf_len,
  183. uint8_t * const p_tx_buf,
  184. uint32_t * const p_tx_buf_len)
  185. {
  186. SER_ASSERT_NOT_NULL(p_rx_buf);
  187. SER_ASSERT_NOT_NULL(p_tx_buf);
  188. SER_ASSERT_NOT_NULL(p_tx_buf_len);
  189. uint8_t data[BLE_GAP_ADV_MAX_SIZE];
  190. uint8_t * p_data = data;
  191. uint8_t dlen = sizeof (data);
  192. uint8_t sr_data[BLE_GAP_ADV_MAX_SIZE];
  193. uint8_t * p_sr_data = sr_data;
  194. uint8_t srdlen = sizeof (sr_data);
  195. uint32_t err_code = NRF_SUCCESS;
  196. uint32_t sd_err_code;
  197. err_code = ble_gap_adv_data_set_req_dec(p_rx_buf,
  198. rx_buf_len,
  199. &p_data,
  200. &dlen,
  201. &p_sr_data,
  202. &srdlen);
  203. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  204. sd_err_code = sd_ble_gap_adv_data_set(p_data, dlen, p_sr_data, srdlen);
  205. err_code = ble_gap_adv_data_set_rsp_enc(sd_err_code, p_tx_buf, p_tx_buf_len);
  206. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  207. return err_code;
  208. }
  209. #endif
  210. uint32_t conn_mw_ble_gap_adv_start(uint8_t const * const p_rx_buf,
  211. uint32_t rx_buf_len,
  212. uint8_t * const p_tx_buf,
  213. uint32_t * const p_tx_buf_len)
  214. {
  215. SER_ASSERT_NOT_NULL(p_rx_buf);
  216. SER_ASSERT_NOT_NULL(p_tx_buf);
  217. SER_ASSERT_NOT_NULL(p_tx_buf_len);
  218. uint32_t err_code = NRF_SUCCESS;
  219. uint32_t sd_err_code;
  220. #if NRF_SD_BLE_API_VERSION > 5
  221. uint8_t conn_cfg_tag;
  222. uint8_t adv_handle;
  223. err_code = ble_gap_adv_start_req_dec(p_rx_buf, rx_buf_len, &adv_handle, &conn_cfg_tag);
  224. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  225. sd_err_code = sd_ble_gap_adv_start(adv_handle, conn_cfg_tag);
  226. #else
  227. ble_gap_addr_t peer_addr;
  228. ble_gap_adv_params_t adv_params;
  229. ble_gap_adv_params_t * p_adv_params;
  230. adv_params.p_peer_addr = &peer_addr;
  231. p_adv_params = &adv_params;
  232. #if NRF_SD_BLE_API_VERSION >= 4
  233. uint8_t conn_cfg_tag;
  234. err_code = ble_gap_adv_start_req_dec(p_rx_buf, rx_buf_len, &p_adv_params, &conn_cfg_tag);
  235. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  236. sd_err_code = sd_ble_gap_adv_start(p_adv_params, conn_cfg_tag);
  237. #else
  238. err_code = ble_gap_adv_start_req_dec(p_rx_buf, rx_buf_len, &p_adv_params);
  239. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  240. sd_err_code = sd_ble_gap_adv_start(p_adv_params);
  241. #endif
  242. #endif
  243. err_code = ble_gap_adv_start_rsp_enc(sd_err_code, p_tx_buf, p_tx_buf_len);
  244. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  245. return err_code;
  246. }
  247. uint32_t conn_mw_ble_gap_adv_stop(uint8_t const * const p_rx_buf,
  248. uint32_t rx_buf_len,
  249. uint8_t * const p_tx_buf,
  250. uint32_t * const p_tx_buf_len)
  251. {
  252. SER_ASSERT_NOT_NULL(p_rx_buf);
  253. SER_ASSERT_NOT_NULL(p_tx_buf);
  254. SER_ASSERT_NOT_NULL(p_tx_buf_len);
  255. uint32_t err_code = NRF_SUCCESS;
  256. uint32_t sd_err_code;
  257. #if NRF_SD_BLE_API_VERSION > 5
  258. uint8_t adv_handle;
  259. err_code = ble_gap_adv_stop_req_dec(p_rx_buf, rx_buf_len, &adv_handle);
  260. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  261. sd_err_code = sd_ble_gap_adv_stop(adv_handle);
  262. #else
  263. sd_err_code = sd_ble_gap_adv_stop();
  264. #endif
  265. err_code = ble_gap_adv_stop_rsp_enc(sd_err_code, p_tx_buf, p_tx_buf_len);
  266. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  267. return err_code;
  268. }
  269. uint32_t conn_mw_ble_gap_conn_param_update(uint8_t const * const p_rx_buf,
  270. uint32_t rx_buf_len,
  271. uint8_t * const p_tx_buf,
  272. uint32_t * const p_tx_buf_len)
  273. {
  274. SER_ASSERT_NOT_NULL(p_rx_buf);
  275. SER_ASSERT_NOT_NULL(p_tx_buf);
  276. SER_ASSERT_NOT_NULL(p_tx_buf_len);
  277. uint16_t conn_handle;
  278. ble_gap_conn_params_t conn_params;
  279. ble_gap_conn_params_t * p_conn_params = &conn_params;
  280. uint32_t err_code = NRF_SUCCESS;
  281. uint32_t sd_err_code;
  282. err_code = ble_gap_conn_param_update_req_dec(p_rx_buf, rx_buf_len, &conn_handle, &p_conn_params);
  283. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  284. sd_err_code = sd_ble_gap_conn_param_update(conn_handle, p_conn_params);
  285. err_code = ble_gap_conn_param_update_rsp_enc(sd_err_code, p_tx_buf, p_tx_buf_len);
  286. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  287. return err_code;
  288. }
  289. uint32_t conn_mw_ble_gap_disconnect(uint8_t const * const p_rx_buf,
  290. uint32_t rx_buf_len,
  291. uint8_t * const p_tx_buf,
  292. uint32_t * const p_tx_buf_len)
  293. {
  294. SER_ASSERT_NOT_NULL(p_rx_buf);
  295. SER_ASSERT_NOT_NULL(p_tx_buf);
  296. SER_ASSERT_NOT_NULL(p_tx_buf_len);
  297. uint16_t conn_handle;
  298. uint8_t hci_status_code;
  299. uint32_t err_code = NRF_SUCCESS;
  300. uint32_t sd_err_code;
  301. err_code = ble_gap_disconnect_req_dec(p_rx_buf, rx_buf_len, &conn_handle, &hci_status_code);
  302. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  303. sd_err_code = sd_ble_gap_disconnect(conn_handle, hci_status_code);
  304. err_code = ble_gap_disconnect_rsp_enc(sd_err_code, p_tx_buf, p_tx_buf_len);
  305. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  306. return err_code;
  307. }
  308. uint32_t conn_mw_ble_gap_tx_power_set(uint8_t const * const p_rx_buf,
  309. uint32_t rx_buf_len,
  310. uint8_t * const p_tx_buf,
  311. uint32_t * const p_tx_buf_len)
  312. {
  313. SER_ASSERT_NOT_NULL(p_rx_buf);
  314. SER_ASSERT_NOT_NULL(p_tx_buf);
  315. SER_ASSERT_NOT_NULL(p_tx_buf_len);
  316. int8_t tx_power;
  317. uint32_t err_code = NRF_SUCCESS;
  318. uint32_t sd_err_code;
  319. #if NRF_SD_BLE_API_VERSION > 5
  320. uint8_t role;
  321. uint16_t handle;
  322. err_code = ble_gap_tx_power_set_req_dec(p_rx_buf, rx_buf_len, &role, &handle, &tx_power);
  323. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  324. sd_err_code = sd_ble_gap_tx_power_set(role, handle, tx_power);
  325. #else
  326. err_code = ble_gap_tx_power_set_req_dec(p_rx_buf, rx_buf_len, &tx_power);
  327. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  328. sd_err_code = sd_ble_gap_tx_power_set(tx_power);
  329. #endif
  330. err_code = ble_gap_tx_power_set_rsp_enc(sd_err_code, p_tx_buf, p_tx_buf_len);
  331. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  332. return err_code;
  333. }
  334. uint32_t conn_mw_ble_gap_appearance_set(uint8_t const * const p_rx_buf,
  335. uint32_t rx_buf_len,
  336. uint8_t * const p_tx_buf,
  337. uint32_t * const p_tx_buf_len)
  338. {
  339. SER_ASSERT_NOT_NULL(p_rx_buf);
  340. SER_ASSERT_NOT_NULL(p_tx_buf);
  341. SER_ASSERT_NOT_NULL(p_tx_buf_len);
  342. uint16_t appearance;
  343. uint32_t err_code = NRF_SUCCESS;
  344. uint32_t sd_err_code;
  345. err_code = ble_gap_appearance_set_req_dec(p_rx_buf, rx_buf_len, &appearance);
  346. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  347. sd_err_code = sd_ble_gap_appearance_set(appearance);
  348. err_code = ble_gap_appearance_set_rsp_enc(sd_err_code, p_tx_buf, p_tx_buf_len);
  349. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  350. return err_code;
  351. }
  352. uint32_t conn_mw_ble_gap_appearance_get(uint8_t const * const p_rx_buf,
  353. uint32_t rx_buf_len,
  354. uint8_t * const p_tx_buf,
  355. uint32_t * const p_tx_buf_len)
  356. {
  357. SER_ASSERT_NOT_NULL(p_rx_buf);
  358. SER_ASSERT_NOT_NULL(p_tx_buf);
  359. SER_ASSERT_NOT_NULL(p_tx_buf_len);
  360. uint16_t appearance;
  361. uint16_t * p_appearance = &appearance;
  362. uint32_t err_code = NRF_SUCCESS;
  363. uint32_t sd_err_code;
  364. err_code = ble_gap_appearance_get_req_dec(p_rx_buf, rx_buf_len, &p_appearance);
  365. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  366. sd_err_code = sd_ble_gap_appearance_get(p_appearance);
  367. err_code = ble_gap_appearance_get_rsp_enc(sd_err_code, p_tx_buf, p_tx_buf_len, p_appearance);
  368. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  369. return err_code;
  370. }
  371. uint32_t conn_mw_ble_gap_ppcp_set(uint8_t const * const p_rx_buf,
  372. uint32_t rx_buf_len,
  373. uint8_t * const p_tx_buf,
  374. uint32_t * const p_tx_buf_len)
  375. {
  376. SER_ASSERT_NOT_NULL(p_rx_buf);
  377. SER_ASSERT_NOT_NULL(p_tx_buf);
  378. SER_ASSERT_NOT_NULL(p_tx_buf_len);
  379. ble_gap_conn_params_t conn_params;
  380. ble_gap_conn_params_t * p_conn_params = &conn_params;
  381. uint32_t err_code = NRF_SUCCESS;
  382. uint32_t sd_err_code;
  383. err_code = ble_gap_ppcp_set_req_dec(p_rx_buf, rx_buf_len, &p_conn_params);
  384. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  385. sd_err_code = sd_ble_gap_ppcp_set(p_conn_params);
  386. err_code = ble_gap_ppcp_set_rsp_enc(sd_err_code, p_tx_buf, p_tx_buf_len);
  387. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  388. return err_code;
  389. }
  390. uint32_t conn_mw_ble_gap_ppcp_get(uint8_t const * const p_rx_buf,
  391. uint32_t rx_buf_len,
  392. uint8_t * const p_tx_buf,
  393. uint32_t * const p_tx_buf_len)
  394. {
  395. SER_ASSERT_NOT_NULL(p_rx_buf);
  396. SER_ASSERT_NOT_NULL(p_tx_buf);
  397. SER_ASSERT_NOT_NULL(p_tx_buf_len);
  398. ble_gap_conn_params_t conn_params;
  399. ble_gap_conn_params_t * p_conn_params = &conn_params;
  400. uint32_t err_code = NRF_SUCCESS;
  401. uint32_t sd_err_code;
  402. err_code = ble_gap_ppcp_get_req_dec(p_rx_buf, rx_buf_len, &p_conn_params);
  403. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  404. sd_err_code = sd_ble_gap_ppcp_get(p_conn_params);
  405. err_code = ble_gap_ppcp_get_rsp_enc(sd_err_code, p_tx_buf, p_tx_buf_len, p_conn_params);
  406. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  407. return err_code;
  408. }
  409. uint32_t conn_mw_ble_gap_device_name_get(uint8_t const * const p_rx_buf,
  410. uint32_t rx_buf_len,
  411. uint8_t * const p_tx_buf,
  412. uint32_t * const p_tx_buf_len)
  413. {
  414. SER_ASSERT_NOT_NULL(p_rx_buf);
  415. SER_ASSERT_NOT_NULL(p_tx_buf);
  416. SER_ASSERT_NOT_NULL(p_tx_buf_len);
  417. uint32_t err_code = NRF_SUCCESS;
  418. uint32_t sd_err_code;
  419. uint8_t dev_name[BLE_GAP_DEVNAME_MAX_LEN];
  420. uint8_t * p_dev_name = dev_name;
  421. uint16_t len;
  422. uint16_t * p_len = &len;
  423. uint16_t buf_len = 0;
  424. err_code = ble_gap_device_name_get_req_dec(p_rx_buf, rx_buf_len, &p_dev_name, &p_len);
  425. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  426. if (p_len)
  427. {
  428. buf_len = *p_len;
  429. }
  430. sd_err_code = sd_ble_gap_device_name_get(p_dev_name, p_len);
  431. if (p_len)
  432. {
  433. buf_len = MIN(buf_len, *p_len);
  434. }
  435. err_code = ble_gap_device_name_get_rsp_enc(sd_err_code, p_dev_name, p_len, buf_len,
  436. p_tx_buf, p_tx_buf_len);
  437. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  438. return err_code;
  439. }
  440. uint32_t conn_mw_ble_gap_device_name_set(uint8_t const * const p_rx_buf,
  441. uint32_t rx_buf_len,
  442. uint8_t * const p_tx_buf,
  443. uint32_t * const p_tx_buf_len)
  444. {
  445. SER_ASSERT_NOT_NULL(p_rx_buf);
  446. SER_ASSERT_NOT_NULL(p_tx_buf);
  447. SER_ASSERT_NOT_NULL(p_tx_buf_len);
  448. uint32_t err_code = NRF_SUCCESS;
  449. uint32_t sd_err_code;
  450. ble_gap_conn_sec_mode_t write_perm;
  451. ble_gap_conn_sec_mode_t * p_write_perm = &write_perm;
  452. uint8_t dev_name[BLE_GAP_DEVNAME_MAX_LEN];
  453. uint8_t * p_dev_name = dev_name;
  454. uint16_t len = BLE_GAP_DEVNAME_MAX_LEN;
  455. err_code = ble_gap_device_name_set_req_dec(p_rx_buf,
  456. rx_buf_len,
  457. &p_write_perm,
  458. &p_dev_name,
  459. &len);
  460. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  461. sd_err_code = sd_ble_gap_device_name_set(p_write_perm, p_dev_name, len);
  462. err_code = ble_gap_device_name_set_rsp_enc(sd_err_code, p_tx_buf, p_tx_buf_len);
  463. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  464. return err_code;
  465. }
  466. uint32_t conn_mw_ble_gap_authenticate(uint8_t const * const p_rx_buf,
  467. uint32_t rx_buf_len,
  468. uint8_t * const p_tx_buf,
  469. uint32_t * const p_tx_buf_len)
  470. {
  471. SER_ASSERT_NOT_NULL(p_rx_buf);
  472. SER_ASSERT_NOT_NULL(p_tx_buf);
  473. SER_ASSERT_NOT_NULL(p_tx_buf_len);
  474. uint32_t err_code = NRF_SUCCESS;
  475. uint32_t sd_err_code;
  476. uint16_t conn_handle;
  477. ble_gap_sec_params_t sec_params;
  478. ble_gap_sec_params_t * p_sec_params = &sec_params;
  479. err_code = ble_gap_authenticate_req_dec(p_rx_buf, rx_buf_len, &conn_handle, &p_sec_params);
  480. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  481. sd_err_code = sd_ble_gap_authenticate(conn_handle, p_sec_params);
  482. err_code = ble_gap_authenticate_rsp_enc(sd_err_code, p_tx_buf, p_tx_buf_len);
  483. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  484. return err_code;
  485. }
  486. uint32_t conn_mw_ble_gap_sec_params_reply(uint8_t const * const p_rx_buf,
  487. uint32_t rx_buf_len,
  488. uint8_t * const p_tx_buf,
  489. uint32_t * const p_tx_buf_len)
  490. {
  491. SER_ASSERT_NOT_NULL(p_rx_buf);
  492. SER_ASSERT_NOT_NULL(p_tx_buf);
  493. SER_ASSERT_NOT_NULL(p_tx_buf_len);
  494. uint32_t err_code = NRF_SUCCESS;
  495. uint32_t sd_err_code;
  496. uint32_t sec_tab_index = 0;
  497. uint16_t * p_conn_handle;
  498. uint8_t sec_status;
  499. ble_gap_sec_params_t sec_params;
  500. ble_gap_sec_params_t * p_sec_params = &sec_params;
  501. // Allocate global security context for soft device
  502. err_code = conn_ble_gap_sec_context_create(&sec_tab_index);
  503. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  504. p_conn_handle = &(m_conn_keys_table[sec_tab_index].conn_handle);
  505. // Set up global structure for command decoder
  506. ble_gap_sec_keyset_t * p_sec_keyset = &(m_conn_keys_table[sec_tab_index].keyset);
  507. p_sec_keyset->keys_own.p_enc_key = &(m_conn_keys_table[sec_tab_index].enc_key_own);
  508. p_sec_keyset->keys_own.p_id_key = &(m_conn_keys_table[sec_tab_index].id_key_own);
  509. p_sec_keyset->keys_own.p_sign_key = &(m_conn_keys_table[sec_tab_index].sign_key_own);
  510. p_sec_keyset->keys_own.p_pk = &(m_conn_keys_table[sec_tab_index].pk_own);
  511. p_sec_keyset->keys_peer.p_enc_key = &(m_conn_keys_table[sec_tab_index].enc_key_peer);
  512. p_sec_keyset->keys_peer.p_id_key = &(m_conn_keys_table[sec_tab_index].id_key_peer);
  513. p_sec_keyset->keys_peer.p_sign_key = &(m_conn_keys_table[sec_tab_index].sign_key_peer);
  514. p_sec_keyset->keys_peer.p_pk = &(m_conn_keys_table[sec_tab_index].pk_peer);
  515. err_code = ble_gap_sec_params_reply_req_dec(p_rx_buf,
  516. rx_buf_len,
  517. p_conn_handle,
  518. &sec_status,
  519. &p_sec_params,
  520. &p_sec_keyset);
  521. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  522. if (p_sec_keyset == NULL)
  523. {
  524. //If no keyset was sent destroy the context.
  525. err_code = conn_ble_gap_sec_context_destroy(*p_conn_handle);
  526. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  527. }
  528. sd_err_code = sd_ble_gap_sec_params_reply(*p_conn_handle, sec_status, p_sec_params, p_sec_keyset);
  529. err_code = ble_gap_sec_params_reply_rsp_enc(sd_err_code, p_tx_buf, p_tx_buf_len, p_sec_keyset);
  530. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  531. return err_code;
  532. }
  533. uint32_t conn_mw_ble_gap_auth_key_reply(uint8_t const * const p_rx_buf,
  534. uint32_t rx_buf_len,
  535. uint8_t * const p_tx_buf,
  536. uint32_t * const p_tx_buf_len)
  537. {
  538. SER_ASSERT_NOT_NULL(p_rx_buf);
  539. SER_ASSERT_NOT_NULL(p_tx_buf);
  540. SER_ASSERT_NOT_NULL(p_tx_buf_len);
  541. uint32_t err_code = NRF_SUCCESS;
  542. uint32_t sd_err_code;
  543. uint16_t conn_handle;
  544. uint8_t key_type;
  545. uint8_t key[BLE_GAP_SEC_KEY_LEN];
  546. uint8_t * p_key = key;
  547. err_code = ble_gap_auth_key_reply_req_dec(p_rx_buf, rx_buf_len, &conn_handle, &key_type, &p_key);
  548. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  549. sd_err_code = sd_ble_gap_auth_key_reply(conn_handle, key_type, p_key);
  550. err_code = ble_gap_auth_key_reply_rsp_enc(sd_err_code, p_tx_buf, p_tx_buf_len);
  551. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  552. return err_code;
  553. }
  554. uint32_t conn_mw_ble_gap_sec_info_reply(uint8_t const * const p_rx_buf,
  555. uint32_t rx_buf_len,
  556. uint8_t * const p_tx_buf,
  557. uint32_t * const p_tx_buf_len)
  558. {
  559. SER_ASSERT_NOT_NULL(p_rx_buf);
  560. SER_ASSERT_NOT_NULL(p_tx_buf);
  561. SER_ASSERT_NOT_NULL(p_tx_buf_len);
  562. uint32_t err_code = NRF_SUCCESS;
  563. uint32_t sd_err_code;
  564. uint16_t conn_handle;
  565. ble_gap_enc_info_t enc_info;
  566. ble_gap_enc_info_t * p_enc_info = &enc_info;
  567. ble_gap_irk_t id_info;
  568. ble_gap_irk_t * p_id_info = &id_info;
  569. ble_gap_sign_info_t sign_info;
  570. ble_gap_sign_info_t * p_sign_info = &sign_info;
  571. err_code = ble_gap_sec_info_reply_req_dec(p_rx_buf,
  572. rx_buf_len,
  573. &conn_handle,
  574. &p_enc_info,
  575. &p_id_info,
  576. &p_sign_info);
  577. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  578. sd_err_code = sd_ble_gap_sec_info_reply(conn_handle, p_enc_info, p_id_info, p_sign_info);
  579. err_code = ble_gap_sec_info_reply_rsp_enc(sd_err_code, p_tx_buf, p_tx_buf_len);
  580. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  581. return err_code;
  582. }
  583. uint32_t conn_mw_ble_gap_conn_sec_get(uint8_t const * const p_rx_buf,
  584. uint32_t rx_buf_len,
  585. uint8_t * const p_tx_buf,
  586. uint32_t * const p_tx_buf_len)
  587. {
  588. SER_ASSERT_NOT_NULL(p_rx_buf);
  589. SER_ASSERT_NOT_NULL(p_tx_buf);
  590. SER_ASSERT_NOT_NULL(p_tx_buf_len);
  591. uint32_t err_code = NRF_SUCCESS;
  592. uint32_t sd_err_code;
  593. uint16_t conn_handle;
  594. ble_gap_conn_sec_t conn_sec;
  595. ble_gap_conn_sec_t * p_conn_sec = &conn_sec;
  596. err_code = ble_gap_conn_sec_get_req_dec(p_rx_buf, rx_buf_len, &conn_handle, &p_conn_sec);
  597. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  598. sd_err_code = sd_ble_gap_conn_sec_get(conn_handle, p_conn_sec);
  599. err_code = ble_gap_conn_sec_get_rsp_enc(sd_err_code, p_conn_sec, p_tx_buf, p_tx_buf_len);
  600. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  601. return err_code;
  602. }
  603. uint32_t conn_mw_ble_gap_rssi_start(uint8_t const * const p_rx_buf,
  604. uint32_t rx_buf_len,
  605. uint8_t * const p_tx_buf,
  606. uint32_t * const p_tx_buf_len)
  607. {
  608. SER_ASSERT_NOT_NULL(p_rx_buf);
  609. SER_ASSERT_NOT_NULL(p_tx_buf);
  610. SER_ASSERT_NOT_NULL(p_tx_buf_len);
  611. uint32_t err_code = NRF_SUCCESS;
  612. uint32_t sd_err_code;
  613. uint16_t conn_handle;
  614. uint8_t threshold_dbm;
  615. uint8_t skip_count;
  616. err_code = ble_gap_rssi_start_req_dec(p_rx_buf, rx_buf_len, &conn_handle, &threshold_dbm, &skip_count);
  617. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  618. sd_err_code = sd_ble_gap_rssi_start(conn_handle, threshold_dbm, skip_count);
  619. err_code = ble_gap_rssi_start_rsp_enc(sd_err_code, p_tx_buf, p_tx_buf_len);
  620. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  621. return err_code;
  622. }
  623. uint32_t conn_mw_ble_gap_rssi_stop(uint8_t const * const p_rx_buf,
  624. uint32_t rx_buf_len,
  625. uint8_t * const p_tx_buf,
  626. uint32_t * const p_tx_buf_len)
  627. {
  628. SER_ASSERT_NOT_NULL(p_rx_buf);
  629. SER_ASSERT_NOT_NULL(p_tx_buf);
  630. SER_ASSERT_NOT_NULL(p_tx_buf_len);
  631. uint32_t err_code = NRF_SUCCESS;
  632. uint32_t sd_err_code;
  633. uint16_t conn_handle;
  634. err_code = ble_gap_rssi_stop_req_dec(p_rx_buf, rx_buf_len, &conn_handle);
  635. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  636. sd_err_code = sd_ble_gap_rssi_stop(conn_handle);
  637. err_code = ble_gap_rssi_stop_rsp_enc(sd_err_code, p_tx_buf, p_tx_buf_len);
  638. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  639. return err_code;
  640. }
  641. uint32_t conn_mw_ble_gap_rssi_get(uint8_t const * const p_rx_buf,
  642. uint32_t rx_buf_len,
  643. uint8_t * const p_tx_buf,
  644. uint32_t * const p_tx_buf_len)
  645. {
  646. SER_ASSERT_NOT_NULL(p_rx_buf);
  647. SER_ASSERT_NOT_NULL(p_tx_buf);
  648. SER_ASSERT_NOT_NULL(p_tx_buf_len);
  649. uint32_t err_code = NRF_SUCCESS;
  650. uint32_t sd_err_code;
  651. uint16_t conn_handle;
  652. int8_t rssi;
  653. int8_t * p_rssi = &rssi;
  654. #if NRF_SD_BLE_API_VERSION > 5
  655. uint8_t ch_index;
  656. uint8_t * p_ch_index = &ch_index;
  657. err_code = ble_gap_rssi_get_req_dec(p_rx_buf, rx_buf_len, &conn_handle, &p_rssi, &p_ch_index);
  658. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  659. sd_err_code = sd_ble_gap_rssi_get(conn_handle, p_rssi, p_ch_index);
  660. err_code = ble_gap_rssi_get_rsp_enc(sd_err_code, p_tx_buf, p_tx_buf_len, p_rssi, p_ch_index);
  661. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  662. #else
  663. err_code = ble_gap_rssi_get_req_dec(p_rx_buf, rx_buf_len, &conn_handle, &p_rssi);
  664. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  665. sd_err_code = sd_ble_gap_rssi_get(conn_handle, p_rssi);
  666. err_code = ble_gap_rssi_get_rsp_enc(sd_err_code, p_tx_buf, p_tx_buf_len, p_rssi);
  667. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  668. #endif
  669. return err_code;
  670. }
  671. uint32_t conn_mw_ble_gap_keypress_notify(uint8_t const * const p_rx_buf,
  672. uint32_t rx_buf_len,
  673. uint8_t * const p_tx_buf,
  674. uint32_t * const p_tx_buf_len)
  675. {
  676. SER_ASSERT_NOT_NULL(p_rx_buf);
  677. SER_ASSERT_NOT_NULL(p_tx_buf);
  678. SER_ASSERT_NOT_NULL(p_tx_buf_len);
  679. uint32_t err_code = NRF_SUCCESS;
  680. uint32_t sd_err_code;
  681. uint16_t conn_handle;
  682. uint8_t kp_not;
  683. err_code = ble_gap_keypress_notify_req_dec(p_rx_buf, rx_buf_len, &conn_handle, &kp_not);
  684. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  685. sd_err_code = sd_ble_gap_keypress_notify(conn_handle, kp_not);
  686. err_code = ble_gap_keypress_notify_rsp_enc(sd_err_code, p_tx_buf, p_tx_buf_len);
  687. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  688. return err_code;
  689. }
  690. uint32_t conn_mw_ble_gap_lesc_dhkey_reply(uint8_t const * const p_rx_buf,
  691. uint32_t rx_buf_len,
  692. uint8_t * const p_tx_buf,
  693. uint32_t * const p_tx_buf_len)
  694. {
  695. SER_ASSERT_NOT_NULL(p_rx_buf);
  696. SER_ASSERT_NOT_NULL(p_tx_buf);
  697. SER_ASSERT_NOT_NULL(p_tx_buf_len);
  698. uint32_t err_code = NRF_SUCCESS;
  699. uint32_t sd_err_code;
  700. uint16_t conn_handle;
  701. ble_gap_lesc_dhkey_t dhkey;
  702. ble_gap_lesc_dhkey_t * p_dhkey = &dhkey;
  703. err_code = ble_gap_lesc_dhkey_reply_req_dec(p_rx_buf, rx_buf_len, &conn_handle, &p_dhkey);
  704. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  705. sd_err_code = sd_ble_gap_lesc_dhkey_reply(conn_handle, p_dhkey);
  706. err_code = ble_gap_lesc_dhkey_reply_rsp_enc(sd_err_code, p_tx_buf, p_tx_buf_len);
  707. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  708. return err_code;
  709. }
  710. uint32_t conn_mw_ble_gap_lesc_oob_data_set(uint8_t const * const p_rx_buf,
  711. uint32_t rx_buf_len,
  712. uint8_t * const p_tx_buf,
  713. uint32_t * const p_tx_buf_len)
  714. {
  715. SER_ASSERT_NOT_NULL(p_rx_buf);
  716. SER_ASSERT_NOT_NULL(p_tx_buf);
  717. SER_ASSERT_NOT_NULL(p_tx_buf_len);
  718. uint32_t err_code = NRF_SUCCESS;
  719. uint32_t sd_err_code;
  720. uint16_t conn_handle;
  721. ble_gap_lesc_oob_data_t own;
  722. ble_gap_lesc_oob_data_t peer;
  723. ble_gap_lesc_oob_data_t * p_own = &own;
  724. ble_gap_lesc_oob_data_t * p_peer = &peer;
  725. err_code = ble_gap_lesc_oob_data_set_req_dec(p_rx_buf, rx_buf_len, &conn_handle, &p_own, &p_peer);
  726. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  727. sd_err_code = sd_ble_gap_lesc_oob_data_set(conn_handle, p_own, p_peer);
  728. err_code = ble_gap_lesc_oob_data_set_rsp_enc(sd_err_code, p_tx_buf, p_tx_buf_len);
  729. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  730. return err_code;
  731. }
  732. uint32_t conn_mw_ble_gap_lesc_oob_data_get(uint8_t const * const p_rx_buf,
  733. uint32_t rx_buf_len,
  734. uint8_t * const p_tx_buf,
  735. uint32_t * const p_tx_buf_len)
  736. {
  737. SER_ASSERT_NOT_NULL(p_rx_buf);
  738. SER_ASSERT_NOT_NULL(p_tx_buf);
  739. SER_ASSERT_NOT_NULL(p_tx_buf_len);
  740. uint32_t err_code = NRF_SUCCESS;
  741. uint32_t sd_err_code;
  742. uint16_t conn_handle;
  743. ble_gap_lesc_oob_data_t own;
  744. ble_gap_lesc_oob_data_t * p_own = &own;
  745. ble_gap_lesc_p256_pk_t pk;
  746. ble_gap_lesc_p256_pk_t * p_pk = &pk;
  747. err_code = ble_gap_lesc_oob_data_get_req_dec(p_rx_buf, rx_buf_len, &conn_handle, &p_pk, &p_own);
  748. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  749. sd_err_code = sd_ble_gap_lesc_oob_data_get(conn_handle, p_pk, p_own);
  750. err_code = ble_gap_lesc_oob_data_get_rsp_enc(sd_err_code, p_own, p_tx_buf, p_tx_buf_len);
  751. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  752. return err_code;
  753. }
  754. uint32_t conn_mw_ble_gap_addr_set(uint8_t const * const p_rx_buf,
  755. uint32_t rx_buf_len,
  756. uint8_t * const p_tx_buf,
  757. uint32_t * const p_tx_buf_len)
  758. {
  759. SER_ASSERT_NOT_NULL(p_rx_buf);
  760. SER_ASSERT_NOT_NULL(p_tx_buf);
  761. SER_ASSERT_NOT_NULL(p_tx_buf_len);
  762. ble_gap_addr_t addr;
  763. ble_gap_addr_t * p_addr = &addr;
  764. uint32_t err_code = NRF_SUCCESS;
  765. uint32_t sd_err_code;
  766. err_code = ble_gap_addr_set_req_dec(p_rx_buf, rx_buf_len, &p_addr);
  767. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  768. sd_err_code = sd_ble_gap_addr_set(p_addr);
  769. err_code = ble_gap_addr_set_rsp_enc(sd_err_code, p_tx_buf, p_tx_buf_len);
  770. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  771. return err_code;
  772. }
  773. uint32_t conn_mw_ble_gap_addr_get(uint8_t const * const p_rx_buf,
  774. uint32_t rx_buf_len,
  775. uint8_t * const p_tx_buf,
  776. uint32_t * const p_tx_buf_len)
  777. {
  778. SER_ASSERT_NOT_NULL(p_rx_buf);
  779. SER_ASSERT_NOT_NULL(p_tx_buf);
  780. SER_ASSERT_NOT_NULL(p_tx_buf_len);
  781. ble_gap_addr_t addr;
  782. ble_gap_addr_t * p_addr = &addr;
  783. uint32_t err_code = NRF_SUCCESS;
  784. uint32_t sd_err_code;
  785. err_code = ble_gap_addr_get_req_dec(p_rx_buf, rx_buf_len, &p_addr);
  786. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  787. sd_err_code = sd_ble_gap_addr_get(p_addr);
  788. err_code = ble_gap_addr_get_rsp_enc(sd_err_code, p_tx_buf, p_tx_buf_len, p_addr);
  789. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  790. return err_code;
  791. }
  792. uint32_t conn_mw_ble_gap_privacy_set(uint8_t const * const p_rx_buf,
  793. uint32_t rx_buf_len,
  794. uint8_t * const p_tx_buf,
  795. uint32_t * const p_tx_buf_len)
  796. {
  797. SER_ASSERT_NOT_NULL(p_rx_buf);
  798. SER_ASSERT_NOT_NULL(p_tx_buf);
  799. SER_ASSERT_NOT_NULL(p_tx_buf_len);
  800. uint32_t err_code = NRF_SUCCESS;
  801. uint32_t sd_err_code;
  802. ble_gap_privacy_params_t privacy_params;
  803. ble_gap_privacy_params_t * p_privacy_params = &privacy_params;
  804. ble_gap_irk_t irk;
  805. privacy_params.p_device_irk = &irk;
  806. err_code = ble_gap_privacy_set_req_dec(p_rx_buf, rx_buf_len, &p_privacy_params);
  807. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  808. sd_err_code = sd_ble_gap_privacy_set(p_privacy_params);
  809. err_code = ble_gap_privacy_set_rsp_enc(sd_err_code, p_tx_buf, p_tx_buf_len);
  810. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  811. return err_code;
  812. }
  813. uint32_t conn_mw_ble_gap_privacy_get(uint8_t const * const p_rx_buf,
  814. uint32_t rx_buf_len,
  815. uint8_t * const p_tx_buf,
  816. uint32_t * const p_tx_buf_len)
  817. {
  818. SER_ASSERT_NOT_NULL(p_rx_buf);
  819. SER_ASSERT_NOT_NULL(p_tx_buf);
  820. SER_ASSERT_NOT_NULL(p_tx_buf_len);
  821. uint32_t err_code = NRF_SUCCESS;
  822. uint32_t sd_err_code;
  823. ble_gap_privacy_params_t privacy_params;
  824. ble_gap_privacy_params_t * p_privacy_params = &privacy_params;
  825. ble_gap_irk_t irk;
  826. privacy_params.p_device_irk = &irk;
  827. err_code = ble_gap_privacy_get_req_dec(p_rx_buf, rx_buf_len, &p_privacy_params);
  828. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  829. sd_err_code = sd_ble_gap_privacy_get(p_privacy_params);
  830. err_code = ble_gap_privacy_get_rsp_enc(sd_err_code, p_tx_buf, p_tx_buf_len, p_privacy_params);
  831. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  832. return err_code;
  833. }
  834. uint32_t conn_mw_ble_gap_whitelist_set(uint8_t const * const p_rx_buf,
  835. uint32_t rx_buf_len,
  836. uint8_t * const p_tx_buf,
  837. uint32_t * const p_tx_buf_len)
  838. {
  839. SER_ASSERT_NOT_NULL(p_rx_buf);
  840. SER_ASSERT_NOT_NULL(p_tx_buf);
  841. SER_ASSERT_NOT_NULL(p_tx_buf_len);
  842. uint32_t err_code = NRF_SUCCESS;
  843. uint32_t sd_err_code;
  844. uint8_t length;
  845. ble_gap_addr_t wl_addr_array[BLE_GAP_WHITELIST_ADDR_MAX_COUNT];
  846. ble_gap_addr_t * p_wl_addrs_array[BLE_GAP_WHITELIST_ADDR_MAX_COUNT];
  847. for (uint8_t i = 0; i < BLE_GAP_WHITELIST_ADDR_MAX_COUNT; ++i)
  848. {
  849. p_wl_addrs_array[i] = &wl_addr_array[i];
  850. }
  851. ble_gap_addr_t * * pp_wl_addrs = p_wl_addrs_array;
  852. err_code = ble_gap_whitelist_set_req_dec(p_rx_buf, rx_buf_len, &pp_wl_addrs, &length);
  853. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  854. sd_err_code = sd_ble_gap_whitelist_set((ble_gap_addr_t const * *)pp_wl_addrs, length);
  855. err_code = ble_gap_whitelist_set_rsp_enc(sd_err_code, p_tx_buf, p_tx_buf_len);
  856. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  857. return err_code;
  858. }
  859. uint32_t conn_mw_ble_gap_device_identities_set(uint8_t const * const p_rx_buf,
  860. uint32_t rx_buf_len,
  861. uint8_t * const p_tx_buf,
  862. uint32_t * const p_tx_buf_len)
  863. {
  864. SER_ASSERT_NOT_NULL(p_rx_buf);
  865. SER_ASSERT_NOT_NULL(p_tx_buf);
  866. SER_ASSERT_NOT_NULL(p_tx_buf_len);
  867. uint32_t err_code = NRF_SUCCESS;
  868. uint32_t sd_err_code;
  869. uint8_t length;
  870. ble_gap_id_key_t id_key_array[BLE_GAP_DEVICE_IDENTITIES_MAX_COUNT];
  871. ble_gap_id_key_t * p_id_key_array[BLE_GAP_DEVICE_IDENTITIES_MAX_COUNT];
  872. ble_gap_irk_t irk_array[BLE_GAP_DEVICE_IDENTITIES_MAX_COUNT];
  873. ble_gap_irk_t * p_irk_array[BLE_GAP_DEVICE_IDENTITIES_MAX_COUNT];
  874. for (uint8_t i = 0; i < BLE_GAP_DEVICE_IDENTITIES_MAX_COUNT; ++i)
  875. {
  876. p_id_key_array[i] = &id_key_array[i];
  877. p_irk_array[i] = &irk_array[i];
  878. }
  879. ble_gap_id_key_t * * pp_id_keys = p_id_key_array;
  880. ble_gap_irk_t * * pp_local_irks = p_irk_array;
  881. err_code = ble_gap_device_identities_set_req_dec(p_rx_buf, rx_buf_len,
  882. &pp_id_keys,
  883. &pp_local_irks,
  884. &length);
  885. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  886. sd_err_code = sd_ble_gap_device_identities_set((ble_gap_id_key_t const * *) pp_id_keys,
  887. (ble_gap_irk_t const * *) pp_local_irks,
  888. length);
  889. err_code = ble_gap_device_identities_set_rsp_enc(sd_err_code, p_tx_buf, p_tx_buf_len);
  890. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  891. return err_code;
  892. }
  893. #if NRF_SD_BLE_API_VERSION >= 5
  894. uint32_t conn_mw_ble_gap_phy_update(uint8_t const * const p_rx_buf,
  895. uint32_t rx_buf_len,
  896. uint8_t * const p_tx_buf,
  897. uint32_t * const p_tx_buf_len)
  898. {
  899. SER_ASSERT_NOT_NULL(p_rx_buf);
  900. SER_ASSERT_NOT_NULL(p_tx_buf);
  901. SER_ASSERT_NOT_NULL(p_tx_buf_len);
  902. uint32_t err_code = NRF_SUCCESS;
  903. uint32_t sd_err_code;
  904. uint16_t conn_handle;
  905. ble_gap_phys_t gap_phys;
  906. ble_gap_phys_t * p_gap_phys = &gap_phys;
  907. err_code = ble_gap_phy_update_req_dec(p_rx_buf, rx_buf_len, &conn_handle, &p_gap_phys);
  908. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  909. sd_err_code = sd_ble_gap_phy_update(conn_handle, p_gap_phys);
  910. err_code = ble_gap_phy_update_rsp_enc(sd_err_code, p_tx_buf, p_tx_buf_len);
  911. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  912. return err_code;
  913. }
  914. #endif
  915. #if NRF_SD_BLE_API_VERSION >= 4 && !defined(S112)
  916. uint32_t conn_mw_ble_gap_data_length_update(uint8_t const * const p_rx_buf,
  917. uint32_t rx_buf_len,
  918. uint8_t * const p_tx_buf,
  919. uint32_t * const p_tx_buf_len)
  920. {
  921. SER_ASSERT_NOT_NULL(p_rx_buf);
  922. SER_ASSERT_NOT_NULL(p_tx_buf);
  923. SER_ASSERT_NOT_NULL(p_tx_buf_len);
  924. uint32_t err_code = NRF_SUCCESS;
  925. uint32_t sd_err_code;
  926. uint16_t conn_handle;
  927. ble_gap_data_length_params_t dl_params;
  928. ble_gap_data_length_params_t * p_dl_params = &dl_params;
  929. ble_gap_data_length_limitation_t dl_limitation;
  930. ble_gap_data_length_limitation_t * p_dl_limitation = &dl_limitation;
  931. err_code = ble_gap_data_length_update_req_dec(p_rx_buf, rx_buf_len, &conn_handle, &p_dl_params, &p_dl_limitation);
  932. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  933. sd_err_code = sd_ble_gap_data_length_update(conn_handle, p_dl_params,p_dl_limitation );
  934. err_code = ble_gap_data_length_update_rsp_enc(sd_err_code, p_tx_buf, p_tx_buf_len, p_dl_limitation);
  935. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  936. return err_code;
  937. }
  938. #endif //NRF_SD_BLE_API_VERSION >= 4 && !defined(S112)
  939. #if NRF_SD_BLE_API_VERSION > 5
  940. uint32_t conn_mw_ble_gap_adv_set_configure(uint8_t const * const p_rx_buf,
  941. uint32_t rx_buf_len,
  942. uint8_t * const p_tx_buf,
  943. uint32_t * const p_tx_buf_len)
  944. {
  945. SER_ASSERT_NOT_NULL(p_rx_buf);
  946. SER_ASSERT_NOT_NULL(p_tx_buf);
  947. SER_ASSERT_NOT_NULL(p_tx_buf_len);
  948. uint32_t err_code = NRF_SUCCESS;
  949. uint32_t sd_err_code;
  950. uint8_t adv_handle;
  951. uint8_t * p_adv_handle = &adv_handle;
  952. ble_gap_adv_data_t adv_data;
  953. ble_gap_adv_data_t * p_adv_data = &adv_data;
  954. adv_data.adv_data.len = SER_MAX_ADV_DATA;
  955. adv_data.adv_data.p_data = NULL;
  956. adv_data.scan_rsp_data.len = SER_MAX_ADV_DATA;
  957. adv_data.scan_rsp_data.p_data = NULL;
  958. ble_gap_addr_t addr;
  959. ble_gap_adv_params_t adv_params;
  960. adv_params.p_peer_addr = &addr;
  961. ble_gap_adv_params_t * p_adv_params = &adv_params;
  962. err_code = ble_gap_adv_set_configure_req_dec(p_rx_buf, rx_buf_len, &p_adv_handle, &p_adv_data, &p_adv_params);
  963. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  964. sd_err_code = sd_ble_gap_adv_set_configure(p_adv_handle, p_adv_data, p_adv_params);
  965. if ((sd_err_code != NRF_SUCCESS) && p_adv_data)
  966. {
  967. (void)conn_ble_gap_ble_data_buf_free(p_adv_data->adv_data.p_data);
  968. (void)conn_ble_gap_ble_data_buf_free(p_adv_data->scan_rsp_data.p_data);
  969. }
  970. err_code = ble_gap_adv_set_configure_rsp_enc(sd_err_code, p_tx_buf, p_tx_buf_len, p_adv_data, p_adv_handle);
  971. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  972. return err_code;
  973. }
  974. #ifndef S112
  975. uint32_t conn_mw_ble_gap_qos_channel_survey_start(uint8_t const * const p_rx_buf,
  976. uint32_t rx_buf_len,
  977. uint8_t * const p_tx_buf,
  978. uint32_t * const p_tx_buf_len)
  979. {
  980. SER_ASSERT_NOT_NULL(p_rx_buf);
  981. SER_ASSERT_NOT_NULL(p_tx_buf);
  982. SER_ASSERT_NOT_NULL(p_tx_buf_len);
  983. uint32_t err_code = NRF_SUCCESS;
  984. uint32_t sd_err_code;
  985. uint32_t interval_us;
  986. err_code = ble_gap_qos_channel_survey_start_req_dec(p_rx_buf, rx_buf_len, &interval_us);
  987. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  988. sd_err_code = sd_ble_gap_qos_channel_survey_start(interval_us);
  989. err_code = ble_gap_qos_channel_survey_start_rsp_enc(sd_err_code, p_tx_buf, p_tx_buf_len);
  990. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  991. return err_code;
  992. }
  993. uint32_t conn_mw_ble_gap_qos_channel_survey_stop(uint8_t const * const p_rx_buf,
  994. uint32_t rx_buf_len,
  995. uint8_t * const p_tx_buf,
  996. uint32_t * const p_tx_buf_len)
  997. {
  998. SER_ASSERT_NOT_NULL(p_rx_buf);
  999. SER_ASSERT_NOT_NULL(p_tx_buf);
  1000. SER_ASSERT_NOT_NULL(p_tx_buf_len);
  1001. uint32_t err_code = NRF_SUCCESS;
  1002. uint32_t sd_err_code;
  1003. err_code = ble_gap_qos_channel_survey_stop_req_dec(p_rx_buf, rx_buf_len);
  1004. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  1005. sd_err_code = sd_ble_gap_qos_channel_survey_stop();
  1006. err_code = ble_gap_qos_channel_survey_stop_rsp_enc(sd_err_code, p_tx_buf, p_tx_buf_len);
  1007. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  1008. return err_code;
  1009. }
  1010. #endif //!S112
  1011. #endif