ipv6_medium_ble.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  1. /**
  2. * Copyright (c) 2015 - 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 <stdint.h>
  41. #include "boards.h"
  42. #include "ipv6_medium.h"
  43. #include "ipv6_medium_ble.h"
  44. #include "ble_advdata.h"
  45. #include "ble_hci.h"
  46. #include "ble_srv_common.h"
  47. #include "ble_ipsp.h"
  48. #include "sdk_config.h"
  49. #include "nrf_sdm.h"
  50. #include "nrf_sdh.h"
  51. #include "nrf_sdh_ble.h"
  52. #ifdef COMMISSIONING_ENABLED
  53. #include "commissioning.h"
  54. #endif // COMMISSIONING_ENABLED
  55. #define PUBLIC_BLE_GAP_ADDR_CREATE_FROM_EUI64(ble_gap_addr, eui64, ble_gap_addr_type) \
  56. ble_gap_addr[0] = eui64[7]; \
  57. ble_gap_addr[1] = eui64[6]; \
  58. ble_gap_addr[2] = eui64[5]; \
  59. ble_gap_addr[3] = eui64[2]; \
  60. ble_gap_addr[4] = eui64[1]; \
  61. ble_gap_addr[5] = 0x00; \
  62. ble_gap_addr_type = BLE_GAP_ADDR_TYPE_PUBLIC;
  63. #define IOT_TIMER_DISABLE_API_PARAM_CHECK 0
  64. #if (IOT_TIMER_DISABLE_API_PARAM_CHECK == 0)
  65. #define NULL_PARAM_CHECK(PARAM) \
  66. if ((PARAM) == NULL) \
  67. { \
  68. return (NRF_ERROR_NULL); \
  69. }
  70. #else // IOT_TIMER_DISABLE_API_PARAM_CHECK
  71. #define NULL_PARAM_CHECK(PARAM)
  72. #endif //IOT_TIMER_DISABLE_API_PARAM_CHECK
  73. #define BLE_IPV6_MEDIUM_BLE_OBSERVER_PRIO 1 /**< BLE observer priority. */
  74. #define BLE_IPSP_TAG 35 /**< Identifies the L2CAP configuration used with SoftDevice. */
  75. static ipv6_medium_instance_id_t m_module_instance_id = 0x01; /**< Module instance identifier. As of today, only a single instance is supported. */
  76. static ipv6_medium_evt_handler_t m_ipv6_medium_evt_handler; /**< Pointer to the event handler procedure of the parent layer. */
  77. static ipv6_medium_error_handler_t m_ipv6_medium_error_handler; /**< Pointer to the error handler procedure of the parent layer. */
  78. static ble_gap_addr_t m_local_ble_addr; /**< Local BT device address. */
  79. static ipv6_medium_ble_gap_params_t * m_p_node_gap_params; /**< Pointer to advertising parameters to be used. */
  80. static ipv6_medium_ble_adv_params_t * m_p_node_adv_params; /**< Pointer to GAP parameters to be used. */
  81. #ifndef COMMISSIONING_ENABLED
  82. static ipv6_medium_ble_gap_params_t m_gap_params; /**< Advertising parameters w/o commissioning. */
  83. static ipv6_medium_ble_adv_params_t m_adv_params; /**< GAP parameters w/o commissioning. */
  84. static ble_uuid_t m_adv_uuids[] =
  85. {
  86. {BLE_UUID_IPSP_SERVICE, BLE_UUID_TYPE_BLE}
  87. }; /**< List of available service UUIDs in advertisement data. */
  88. #else // COMMISSIONING_ENABLED
  89. static uint16_t m_conn_handle = BLE_CONN_HANDLE_INVALID; /**< Handle of the active connection. */
  90. static bool m_connectable_mode_active = false; /**< Indicates if the node is in connectable mode. */
  91. static commissioning_id_mode_cb_t m_commissioning_id_mode_cb;
  92. static commissioning_poweroff_cb_t m_commissioning_power_off_cb;
  93. static bool m_adv_params_applied = false; /**< Indicates if advertising (and GAP) parameters have been applied. */
  94. #endif // COMMISSIONING_ENABLED
  95. static uint8_t m_adv_handle = BLE_GAP_ADV_SET_HANDLE_NOT_SET; /**< Advertising handle used to identify an advertising set. */
  96. static uint8_t m_enc_advdata[BLE_GAP_ADV_SET_DATA_SIZE_MAX]; /**< Buffer for storing an encoded advertising set. */
  97. /**@brief Struct that contains pointers to the encoded advertising data. */
  98. static ble_gap_adv_data_t m_adv_data =
  99. {
  100. .adv_data =
  101. {
  102. .p_data = m_enc_advdata,
  103. .len = BLE_GAP_ADV_SET_DATA_SIZE_MAX
  104. },
  105. .scan_rsp_data =
  106. {
  107. .p_data = NULL,
  108. .len = 0
  109. }
  110. };
  111. #if IPV6_MEDIUM_CONFIG_LOG_ENABLED
  112. #define NRF_LOG_MODULE_NAME ipv6_medium
  113. #define NRF_LOG_LEVEL IPV6_MEDIUM_CONFIG_LOG_LEVEL
  114. #define NRF_LOG_INFO_COLOR IPV6_MEDIUM_CONFIG_INFO_COLOR
  115. #define NRF_LOG_DEBUG_COLOR IPV6_MEDIUM_CONFIG_DEBUG_COLOR
  116. #include "nrf_log.h"
  117. NRF_LOG_MODULE_REGISTER();
  118. #define IPV6M_TRC NRF_LOG_DEBUG /**< Used for getting trace of execution in the module. */
  119. #define IPV6M_ERR NRF_LOG_ERROR /**< Used for logging errors in the module. */
  120. #define IPV6M_DUMP NRF_LOG_HEXDUMP_DEBUG /**< Used for dumping octet information to get details of bond information etc. */
  121. #define IPV6M_ENTRY() IPV6M_TRC(">> %s", __func__)
  122. #define IPV6M_EXIT() IPV6M_TRC("<< %s", __func__)
  123. #else // IPV6_MEDIUM_CONFIG_LOG_ENABLED
  124. #define IPV6M_TRC(...) /**< Disables traces. */
  125. #define IPV6M_DUMP(...) /**< Disables dumping of octet streams. */
  126. #define IPV6M_ERR(...) /**< Disables error logs. */
  127. #define IPV6M_ENTRY(...)
  128. #define IPV6M_EXIT(...)
  129. #endif // IPV6_MEDIUM_CONFIG_LOG_ENABLED
  130. #ifndef COMMISSIONING_ENABLED
  131. /**@brief Function for setting advertisement parameters.
  132. *
  133. * @details These parameters are applied if the Commissioning module is
  134. * not used or in Joining mode.
  135. */
  136. static void adv_params_set(void)
  137. {
  138. IPV6M_ENTRY();
  139. memset(&m_adv_params, 0x00, sizeof(m_adv_params));
  140. m_adv_params.advdata.name_type = BLE_ADVDATA_FULL_NAME;
  141. m_adv_params.advdata.flags = BLE_GAP_ADV_FLAG_BR_EDR_NOT_SUPPORTED;
  142. m_adv_params.advdata.uuids_complete.uuid_cnt = sizeof(m_adv_uuids) / sizeof(m_adv_uuids[0]);
  143. m_adv_params.advdata.uuids_complete.p_uuids = m_adv_uuids;
  144. m_adv_params.advparams.properties.type = BLE_GAP_ADV_TYPE_CONNECTABLE_SCANNABLE_UNDIRECTED;
  145. m_adv_params.advparams.p_peer_addr = NULL; // Undirected advertisement.
  146. m_adv_params.advparams.filter_policy = BLE_GAP_ADV_FP_ANY;
  147. m_adv_params.advparams.interval = APP_ADV_ADV_INTERVAL;
  148. m_adv_params.advparams.duration = APP_ADV_DURATION;
  149. IPV6M_EXIT();
  150. }
  151. /**@brief Function for setting GAP parameters.
  152. *
  153. * @details These parameters are applied if the Commissioning module is
  154. * not used or in Joining mode.
  155. */
  156. static void gap_params_set(void)
  157. {
  158. memset(&m_gap_params, 0x00, sizeof(m_gap_params));
  159. BLE_GAP_CONN_SEC_MODE_SET_OPEN(&m_gap_params.sec_mode);
  160. m_gap_params.appearance = BLE_APPEARANCE_UNKNOWN;
  161. m_gap_params.p_dev_name = (const uint8_t *)DEVICE_NAME;
  162. m_gap_params.dev_name_len = strlen(DEVICE_NAME);
  163. m_gap_params.gap_conn_params.min_conn_interval = (uint16_t)MIN_CONN_INTERVAL;
  164. m_gap_params.gap_conn_params.max_conn_interval = (uint16_t)MAX_CONN_INTERVAL;
  165. m_gap_params.gap_conn_params.slave_latency = SLAVE_LATENCY;
  166. m_gap_params.gap_conn_params.conn_sup_timeout = CONN_SUP_TIMEOUT;
  167. }
  168. #endif // COMMISSIONING_ENABLED
  169. /**@brief Function for applying the advertisement parameters.
  170. *
  171. * @details Encodes the required advertising data and passes it to the stack.
  172. */
  173. static void adv_params_apply(void)
  174. {
  175. uint32_t err_code;
  176. err_code = ble_advdata_encode(&m_p_node_adv_params->advdata, m_adv_data.adv_data.p_data, &m_adv_data.adv_data.len);
  177. APP_ERROR_CHECK(err_code);
  178. #ifndef COMMISSIONING_ENABLED
  179. err_code = sd_ble_gap_adv_set_configure(&m_adv_handle, &m_adv_data, &m_adv_params.advparams);
  180. APP_ERROR_CHECK(err_code);
  181. #else
  182. err_code = sd_ble_gap_adv_set_configure(&m_adv_handle, &m_adv_data, &m_p_node_adv_params->advparams);
  183. APP_ERROR_CHECK(err_code);
  184. #endif
  185. }
  186. /**@brief Function for applying the GAP configuration.
  187. *
  188. * @details This function sets up all the necessary GAP (Generic Access Profile) parameters of the
  189. * device including the device name, appearance, and the preferred connection parameters.
  190. */
  191. static void gap_params_apply(void)
  192. {
  193. uint32_t err_code;
  194. err_code = sd_ble_gap_device_name_set(&m_p_node_gap_params->sec_mode, \
  195. m_p_node_gap_params->p_dev_name, \
  196. m_p_node_gap_params->dev_name_len);
  197. APP_ERROR_CHECK(err_code);
  198. err_code = sd_ble_gap_appearance_set(m_p_node_gap_params->appearance);
  199. APP_ERROR_CHECK(err_code);
  200. err_code = sd_ble_gap_ppcp_set(&m_p_node_gap_params->gap_conn_params);
  201. APP_ERROR_CHECK(err_code);
  202. }
  203. /**@brief Function for handling the application's BLE Stack events and
  204. * passing them on to the applications as generic transport medium events.
  205. *
  206. * @param[in] p_ble_evt Bluetooth stack event.
  207. */
  208. static void on_ble_evt(ble_evt_t const * p_ble_evt)
  209. {
  210. ipv6_medium_evt_t ipv6_medium_evt;
  211. memset(&ipv6_medium_evt, 0x00, sizeof(ipv6_medium_evt));
  212. ipv6_medium_evt.ipv6_medium_instance_id.ipv6_medium_instance_id = m_module_instance_id;
  213. ipv6_medium_evt.ipv6_medium_instance_id.ipv6_medium_instance_type = IPV6_MEDIUM_ID_BLE;
  214. ipv6_medium_evt.medium_specific.ble.p_ble_evt = (ble_evt_t*)p_ble_evt;
  215. ipv6_medium_error_t ipv6_medium_error;
  216. memset(&ipv6_medium_error, 0x00, sizeof(ipv6_medium_error));
  217. ipv6_medium_error.ipv6_medium_instance_id.ipv6_medium_instance_id = m_module_instance_id;
  218. ipv6_medium_error.ipv6_medium_instance_id.ipv6_medium_instance_type = IPV6_MEDIUM_ID_BLE;
  219. bool do_notify_event = false;
  220. bool do_notify_error = false;
  221. switch (p_ble_evt->header.evt_id)
  222. {
  223. case BLE_GAP_EVT_CONNECTED:
  224. {
  225. #ifdef COMMISSIONING_ENABLED
  226. m_conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
  227. #endif // COMMISSIONING_ENABLED
  228. ipv6_medium_evt.ipv6_medium_evt_id = IPV6_MEDIUM_EVT_CONN_UP;
  229. do_notify_event = true;
  230. break;
  231. }
  232. case BLE_GAP_EVT_DISCONNECTED:
  233. {
  234. #ifdef COMMISSIONING_ENABLED
  235. m_conn_handle = BLE_CONN_HANDLE_INVALID;
  236. #endif // COMMISSIONING_ENABLED
  237. ipv6_medium_evt.ipv6_medium_evt_id = IPV6_MEDIUM_EVT_CONN_DOWN;
  238. do_notify_event = true;
  239. break;
  240. }
  241. case BLE_GAP_EVT_ADV_SET_TERMINATED:
  242. {
  243. if (p_ble_evt->evt.gap_evt.params.adv_set_terminated.reason == BLE_GAP_EVT_ADV_SET_TERMINATED_REASON_TIMEOUT)
  244. {
  245. ipv6_medium_evt.ipv6_medium_evt_id = IPV6_MEDIUM_EVT_CONNECTABLE_MODE_EXIT;
  246. do_notify_event = true;
  247. }
  248. else
  249. {
  250. // This is not necessarily an error, only added here to show error handler usage.
  251. ipv6_medium_error.medium_specific.ble.dummy_value = 0x13;
  252. do_notify_error = true;
  253. }
  254. break;
  255. }
  256. default:
  257. {
  258. ipv6_medium_evt.ipv6_medium_evt_id = IPV6_MEDIUM_EVT_PHY_SPECIFIC;
  259. do_notify_event = true;
  260. break;
  261. }
  262. }
  263. ble_ipsp_evt_handler(p_ble_evt);
  264. if (do_notify_event == true)
  265. {
  266. m_ipv6_medium_evt_handler(&ipv6_medium_evt);
  267. }
  268. if (do_notify_error == true)
  269. {
  270. m_ipv6_medium_error_handler(&ipv6_medium_error);
  271. }
  272. }
  273. /*
  274. * @brief Function for handling BLE events.
  275. *
  276. * @param[in] p_ble_evt Event received from the BLE stack.
  277. * @param[in] p_context Context.
  278. */
  279. static void ble_evt_handler(const ble_evt_t * p_ble_evt, void * p_context)
  280. {
  281. UNUSED_PARAMETER(p_context);
  282. #ifdef COMMISSIONING_ENABLED
  283. commissioning_ble_evt_handler(p_ble_evt);
  284. ble_ncfgs_ble_evt_handler(p_ble_evt);
  285. #endif // COMMISSIONING_ENABLED
  286. on_ble_evt(p_ble_evt);
  287. }
  288. /**@brief Function for initializing the BLE stack.
  289. *
  290. * @details Initializes the SoftDevice and the BLE event interrupt.
  291. */
  292. static uint32_t ble_stack_init(void)
  293. {
  294. ret_code_t err_code;
  295. uint32_t ram_start = 0;
  296. ble_cfg_t ble_cfg;
  297. err_code = nrf_sdh_enable_request();
  298. if (err_code == NRF_SUCCESS)
  299. {
  300. // Fetch the start address of the application RAM.
  301. err_code = nrf_sdh_ble_app_ram_start_get(&ram_start);
  302. }
  303. if (err_code == NRF_SUCCESS)
  304. {
  305. // Configure the maximum number of connections.
  306. memset(&ble_cfg, 0, sizeof(ble_cfg));
  307. ble_cfg.gap_cfg.role_count_cfg.periph_role_count = BLE_IPSP_MAX_CHANNELS;
  308. ble_cfg.gap_cfg.role_count_cfg.central_role_count = 0;
  309. ble_cfg.gap_cfg.role_count_cfg.central_sec_count = 0;
  310. err_code = sd_ble_cfg_set(BLE_GAP_CFG_ROLE_COUNT, &ble_cfg, ram_start);
  311. }
  312. if (err_code == NRF_SUCCESS)
  313. {
  314. memset(&ble_cfg, 0, sizeof(ble_cfg));
  315. // Configure total number of connections.
  316. ble_cfg.conn_cfg.conn_cfg_tag = BLE_IPSP_TAG;
  317. ble_cfg.conn_cfg.params.gap_conn_cfg.conn_count = BLE_IPSP_MAX_CHANNELS;
  318. ble_cfg.conn_cfg.params.gap_conn_cfg.event_length = BLE_GAP_EVENT_LENGTH_DEFAULT;
  319. err_code = sd_ble_cfg_set(BLE_CONN_CFG_GAP, &ble_cfg, ram_start);
  320. }
  321. if (err_code == NRF_SUCCESS)
  322. {
  323. memset(&ble_cfg, 0, sizeof(ble_cfg));
  324. // Configure the number of custom UUIDS.
  325. #ifdef COMMISSIONING_ENABLED
  326. ble_cfg.common_cfg.vs_uuid_cfg.vs_uuid_count = 1;
  327. #else
  328. ble_cfg.common_cfg.vs_uuid_cfg.vs_uuid_count = 0;
  329. #endif // COMMISSIONING_ENABLED
  330. err_code = sd_ble_cfg_set(BLE_COMMON_CFG_VS_UUID, &ble_cfg, ram_start);
  331. }
  332. if (err_code == NRF_SUCCESS)
  333. {
  334. memset(&ble_cfg, 0, sizeof(ble_cfg));
  335. // Set L2CAP channel configuration
  336. ble_cfg.conn_cfg.conn_cfg_tag = BLE_IPSP_TAG;
  337. ble_cfg.conn_cfg.params.l2cap_conn_cfg.rx_mps = BLE_IPSP_RX_MPS;
  338. ble_cfg.conn_cfg.params.l2cap_conn_cfg.rx_queue_size = BLE_IPSP_RX_BUFFER_COUNT;
  339. ble_cfg.conn_cfg.params.l2cap_conn_cfg.tx_mps = BLE_IPSP_TX_MPS;
  340. ble_cfg.conn_cfg.params.l2cap_conn_cfg.tx_queue_size = 1;
  341. ble_cfg.conn_cfg.params.l2cap_conn_cfg.ch_count = 1; // One IPSP channel per link.
  342. err_code = sd_ble_cfg_set(BLE_CONN_CFG_L2CAP, &ble_cfg, ram_start);
  343. }
  344. if (err_code == NRF_SUCCESS)
  345. {
  346. memset(&ble_cfg, 0, sizeof(ble_cfg));
  347. // Set the ATT table size.
  348. #ifdef COMMISSIONING_ENABLED
  349. ble_cfg.gatts_cfg.attr_tab_size.attr_tab_size = 1024;
  350. #else
  351. ble_cfg.gatts_cfg.attr_tab_size.attr_tab_size = 256;
  352. #endif // COMMISSIONING_ENABLED
  353. err_code = sd_ble_cfg_set(BLE_GATTS_CFG_ATTR_TAB_SIZE, &ble_cfg, ram_start);
  354. }
  355. if (err_code == NRF_SUCCESS)
  356. {
  357. err_code = nrf_sdh_ble_enable(&ram_start);
  358. }
  359. NRF_SDH_BLE_OBSERVER(m_ble_evt_observer, BLE_IPV6_MEDIUM_BLE_OBSERVER_PRIO, ble_evt_handler, NULL);
  360. return err_code;
  361. }
  362. uint32_t ipv6_medium_connectable_mode_enter(ipv6_medium_instance_id_t ipv6_medium_instance_id)
  363. {
  364. IPV6M_ENTRY();
  365. if (ipv6_medium_instance_id != m_module_instance_id)
  366. {
  367. return NRF_ERROR_INVALID_PARAM;
  368. }
  369. #ifdef COMMISSIONING_ENABLED
  370. if (m_adv_params_applied == false)
  371. {
  372. // Apply advertising (and GAP) parameters, if not applied when node mode changed.
  373. commissioning_gap_params_get(&m_p_node_gap_params);
  374. commissioning_adv_params_get(&m_p_node_adv_params);
  375. gap_params_apply();
  376. adv_params_apply();
  377. }
  378. m_adv_params_applied = false;
  379. #endif // COMMISSIONING_ENABLED
  380. adv_params_apply();
  381. uint32_t err_code = sd_ble_gap_adv_start(m_adv_handle, BLE_IPSP_TAG);
  382. #ifdef COMMISSIONING_ENABLED
  383. if (err_code == NRF_SUCCESS)
  384. {
  385. m_connectable_mode_active = true;
  386. }
  387. #endif // COMMISSIONING_ENABLED
  388. IPV6M_EXIT();
  389. return err_code;
  390. }
  391. uint32_t ipv6_medium_connectable_mode_exit(ipv6_medium_instance_id_t ipv6_medium_instance_id)
  392. {
  393. if (ipv6_medium_instance_id != m_module_instance_id)
  394. {
  395. return NRF_ERROR_INVALID_PARAM;
  396. }
  397. uint32_t err_code = sd_ble_gap_adv_stop(m_adv_handle);
  398. #ifdef COMMISSIONING_ENABLED
  399. if (err_code == NRF_SUCCESS)
  400. {
  401. m_connectable_mode_active = false;
  402. }
  403. #endif // COMMISSIONING_ENABLED
  404. return err_code;
  405. }
  406. uint32_t ipv6_medium_eui48_get(ipv6_medium_instance_id_t ipv6_medium_instance_id, \
  407. eui48_t * p_ipv6_medium_eui48)
  408. {
  409. if (ipv6_medium_instance_id != m_module_instance_id)
  410. {
  411. return NRF_ERROR_INVALID_PARAM;
  412. }
  413. ble_gap_addr_t local_ble_addr;
  414. uint32_t err_code = sd_ble_gap_addr_get(&local_ble_addr);
  415. memcpy(p_ipv6_medium_eui48->identifier, local_ble_addr.addr, EUI_48_SIZE);
  416. return err_code;
  417. }
  418. uint32_t ipv6_medium_eui48_set(ipv6_medium_instance_id_t ipv6_medium_instance_id, \
  419. eui48_t * p_ipv6_medium_eui48)
  420. {
  421. if (ipv6_medium_instance_id != m_module_instance_id)
  422. {
  423. return NRF_ERROR_INVALID_PARAM;
  424. }
  425. if (p_ipv6_medium_eui48->identifier[5] != 0x00)
  426. {
  427. return NRF_ERROR_INVALID_PARAM;
  428. }
  429. m_local_ble_addr.addr_type = BLE_GAP_ADDR_TYPE_PUBLIC;
  430. memcpy(m_local_ble_addr.addr, p_ipv6_medium_eui48->identifier, EUI_48_SIZE);
  431. return sd_ble_gap_addr_set(&m_local_ble_addr);
  432. }
  433. uint32_t ipv6_medium_eui64_get(ipv6_medium_instance_id_t ipv6_medium_instance_id, \
  434. eui64_t * p_ipv6_medium_eui64)
  435. {
  436. if (ipv6_medium_instance_id != m_module_instance_id)
  437. {
  438. return NRF_ERROR_INVALID_PARAM;
  439. }
  440. ble_gap_addr_t local_ble_addr;
  441. uint32_t err_code = sd_ble_gap_addr_get(&local_ble_addr);
  442. APP_ERROR_CHECK(err_code);
  443. IPV6_EUI64_CREATE_FROM_EUI48(p_ipv6_medium_eui64->identifier,
  444. local_ble_addr.addr,
  445. local_ble_addr.addr_type);
  446. return NRF_SUCCESS;
  447. }
  448. uint32_t ipv6_medium_eui64_set(ipv6_medium_instance_id_t ipv6_medium_instance_id, \
  449. eui64_t * p_ipv6_medium_eui64)
  450. {
  451. if (ipv6_medium_instance_id != m_module_instance_id)
  452. {
  453. return NRF_ERROR_INVALID_PARAM;
  454. }
  455. if ((p_ipv6_medium_eui64->identifier[0] != 0x02) ||
  456. (p_ipv6_medium_eui64->identifier[3] != 0xFF) ||
  457. (p_ipv6_medium_eui64->identifier[4] != 0xFE))
  458. {
  459. return NRF_ERROR_INVALID_PARAM;
  460. }
  461. ble_gap_addr_t local_ble_addr;
  462. PUBLIC_BLE_GAP_ADDR_CREATE_FROM_EUI64(local_ble_addr.addr, \
  463. p_ipv6_medium_eui64->identifier, \
  464. local_ble_addr.addr_type);
  465. return sd_ble_gap_addr_set(&local_ble_addr);
  466. }
  467. #ifdef COMMISSIONING_ENABLED
  468. void commissioning_evt_handler(commissioning_evt_t * p_commissioning_evt)
  469. {
  470. IPV6M_ENTRY();
  471. switch (p_commissioning_evt->commissioning_evt_id)
  472. {
  473. case COMMISSIONING_EVT_CONFIG_MODE_ENTER:
  474. // Fall-through.
  475. case COMMISSIONING_EVT_JOINING_MODE_ENTER:
  476. {
  477. m_commissioning_power_off_cb(p_commissioning_evt->power_off_enable_requested);
  478. if (m_conn_handle != BLE_CONN_HANDLE_INVALID)
  479. {
  480. // Making sure that advertising (and GAP) parameters are
  481. // applied when entering connectable mode the next time.
  482. m_adv_params_applied = false;
  483. UNUSED_VARIABLE(sd_ble_gap_disconnect(m_conn_handle, \
  484. BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION));
  485. }
  486. else
  487. {
  488. bool do_return_to_connectable_mode = m_connectable_mode_active;
  489. UNUSED_VARIABLE(ipv6_medium_connectable_mode_exit(m_module_instance_id));
  490. commissioning_gap_params_get(&m_p_node_gap_params);
  491. commissioning_adv_params_get(&m_p_node_adv_params);
  492. gap_params_apply();
  493. adv_params_apply();
  494. // Advertising and GAP parameters applied, making sure that
  495. // it is not repeated when entering connectable mode the next time.
  496. m_adv_params_applied = true;
  497. if (do_return_to_connectable_mode == true)
  498. {
  499. // Restart connectable mode, if the node was in connectable mode applying
  500. // the new parameters.
  501. UNUSED_VARIABLE(ipv6_medium_connectable_mode_enter(m_module_instance_id));
  502. }
  503. }
  504. break;
  505. }
  506. case COMMISSIONING_EVT_IDENTITY_MODE_ENTER:
  507. {
  508. m_commissioning_id_mode_cb(CMD_IDENTITY_MODE_ENTER);
  509. break;
  510. }
  511. case COMMISSIONING_EVT_IDENTITY_MODE_EXIT:
  512. {
  513. m_commissioning_id_mode_cb(CMD_IDENTITY_MODE_EXIT);
  514. break;
  515. }
  516. default:
  517. {
  518. // No implementation needed.
  519. break;
  520. }
  521. }
  522. IPV6M_EXIT();
  523. }
  524. #endif // COMMISSIONING_ENABLED
  525. uint32_t ipv6_medium_init(ipv6_medium_init_params_t * p_init_param, \
  526. ipv6_medium_type_t desired_medium_type, \
  527. ipv6_medium_instance_t * p_new_medium_instance)
  528. {
  529. IPV6M_ENTRY();
  530. uint32_t err_code = NRF_SUCCESS;
  531. NULL_PARAM_CHECK(p_init_param->ipv6_medium_evt_handler);
  532. if (desired_medium_type != IPV6_MEDIUM_ID_BLE)
  533. {
  534. return NRF_ERROR_INVALID_PARAM;
  535. }
  536. m_ipv6_medium_evt_handler = p_init_param->ipv6_medium_evt_handler;
  537. m_ipv6_medium_error_handler = p_init_param->ipv6_medium_error_handler;
  538. p_new_medium_instance->ipv6_medium_instance_type = IPV6_MEDIUM_ID_BLE;
  539. p_new_medium_instance->ipv6_medium_instance_id = m_module_instance_id;
  540. err_code = ble_stack_init();
  541. if (err_code != NRF_SUCCESS)
  542. {
  543. return err_code;
  544. }
  545. #ifndef COMMISSIONING_ENABLED
  546. gap_params_set();
  547. adv_params_set();
  548. m_p_node_gap_params = &m_gap_params;
  549. m_p_node_adv_params = &m_adv_params;
  550. gap_params_apply();
  551. #else // COMMISSIONING_ENABLED
  552. m_commissioning_id_mode_cb = p_init_param->commissioning_id_mode_cb;
  553. m_commissioning_power_off_cb = p_init_param->commissioning_power_off_cb;
  554. commissioning_init_params_t init_param;
  555. memset(&init_param, 0x00, sizeof(init_param));
  556. init_param.commissioning_evt_handler = commissioning_evt_handler;
  557. uint8_t new_mode;
  558. err_code = commissioning_init(&init_param, \
  559. &new_mode);
  560. commissioning_node_mode_change(new_mode);
  561. #endif // COMMISSIONING_ENABLED
  562. IPV6M_EXIT();
  563. return err_code;
  564. }