You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

681 lines
22 KiB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
  1. /**
  2. * Copyright (c) 2016 - 2021, 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 <stdio.h>
  41. #include <stdint.h>
  42. #include <stdbool.h>
  43. #include "nordic_common.h"
  44. #include "app_error.h"
  45. #include "app_uart.h"
  46. #include "ble_db_discovery.h"
  47. #include "app_timer.h"
  48. #include "app_util.h"
  49. #include "bsp_btn_ble.h"
  50. #include "ble.h"
  51. #include "ble_gap.h"
  52. #include "ble_hci.h"
  53. #include "nrf_sdh.h"
  54. #include "nrf_sdh_ble.h"
  55. #include "nrf_sdh_soc.h"
  56. #include "zble_nus_c.h"
  57. #include "nrf_ble_gatt.h"
  58. #include "nrf_pwr_mgmt.h"
  59. #include "nrf_ble_scan.h"
  60. #include "nrf_log.h"
  61. #include "nrf_log_ctrl.h"
  62. #include "nrf_log_default_backends.h"
  63. #define APP_BLE_CONN_CFG_TAG 1 /**< Tag that refers to the BLE stack configuration set with @ref sd_ble_cfg_set. The default tag is @ref BLE_CONN_CFG_TAG_DEFAULT. */
  64. #define APP_BLE_OBSERVER_PRIO 3 /**< BLE observer priority of the application. There is no need to modify this value. */
  65. #define UART_TX_BUF_SIZE 256 /**< UART TX buffer size. */
  66. #define UART_RX_BUF_SIZE 256 /**< UART RX buffer size. */
  67. #define NUS_SERVICE_UUID_TYPE BLE_UUID_TYPE_VENDOR_BEGIN /**< UUID type for the Nordic UART Service (vendor specific). */
  68. #define ECHOBACK_BLE_UART_DATA 0 /**< Echo the UART data that is received over the Nordic UART Service (NUS) back to the sender. */
  69. BLE_NUS_C_DEF(m_ble_nus_c); /**< BLE Nordic UART Service (NUS) client instance. */
  70. NRF_BLE_GATT_DEF(m_gatt); /**< GATT module instance. */
  71. BLE_DB_DISCOVERY_DEF(m_db_disc); /**< Database discovery module instance. */
  72. NRF_BLE_SCAN_DEF(m_scan); /**< Scanning Module instance. */
  73. NRF_BLE_GQ_DEF(m_ble_gatt_queue, /**< BLE GATT Queue instance. */
  74. NRF_SDH_BLE_CENTRAL_LINK_COUNT,
  75. NRF_BLE_GQ_QUEUE_SIZE);
  76. static uint16_t m_ble_nus_max_data_len = BLE_GATT_ATT_MTU_DEFAULT - OPCODE_LENGTH - HANDLE_LENGTH; /**< Maximum length of data (in bytes) that can be transmitted to the peer by the Nordic UART service module. */
  77. /**@brief NUS UUID. */
  78. static ble_uuid_t const m_nus_uuid =
  79. {
  80. .uuid = BLE_UUID_NUS_SERVICE,
  81. .type = NUS_SERVICE_UUID_TYPE
  82. };
  83. /**@brief Function for handling asserts in the SoftDevice.
  84. *
  85. * @details This function is called in case of an assert in the SoftDevice.
  86. *
  87. * @warning This handler is only an example and is not meant for the final product. You need to analyze
  88. * how your product is supposed to react in case of assert.
  89. * @warning On assert from the SoftDevice, the system can only recover on reset.
  90. *
  91. * @param[in] line_num Line number of the failing assert call.
  92. * @param[in] p_file_name File name of the failing assert call.
  93. */
  94. void assert_nrf_callback(uint16_t line_num, const uint8_t * p_file_name)
  95. {
  96. app_error_handler(0xDEADBEEF, line_num, p_file_name);
  97. }
  98. /**@brief Function for handling the Nordic UART Service Client errors.
  99. *
  100. * @param[in] nrf_error Error code containing information about what went wrong.
  101. */
  102. static void nus_error_handler(uint32_t nrf_error)
  103. {
  104. APP_ERROR_HANDLER(nrf_error);
  105. }
  106. /**@brief Function to start scanning. */
  107. static void scan_start(void)
  108. {
  109. ret_code_t ret;
  110. ret = nrf_ble_scan_start(&m_scan);
  111. APP_ERROR_CHECK(ret);
  112. ret = bsp_indication_set(BSP_INDICATE_SCANNING);
  113. APP_ERROR_CHECK(ret);
  114. }
  115. /**@brief Function for handling Scanning Module events.
  116. */
  117. static void scan_evt_handler(scan_evt_t const * p_scan_evt)
  118. {
  119. ret_code_t err_code;
  120. switch(p_scan_evt->scan_evt_id)
  121. {
  122. case NRF_BLE_SCAN_EVT_CONNECTING_ERROR:
  123. {
  124. err_code = p_scan_evt->params.connecting_err.err_code;
  125. APP_ERROR_CHECK(err_code);
  126. } break;
  127. case NRF_BLE_SCAN_EVT_CONNECTED:
  128. {
  129. ble_gap_evt_connected_t const * p_connected =
  130. p_scan_evt->params.connected.p_connected;
  131. // Scan is automatically stopped by the connection.
  132. NRF_LOG_INFO("Connecting to target %02x%02x%02x%02x%02x%02x",
  133. p_connected->peer_addr.addr[0],
  134. p_connected->peer_addr.addr[1],
  135. p_connected->peer_addr.addr[2],
  136. p_connected->peer_addr.addr[3],
  137. p_connected->peer_addr.addr[4],
  138. p_connected->peer_addr.addr[5]
  139. );
  140. } break;
  141. case NRF_BLE_SCAN_EVT_SCAN_TIMEOUT:
  142. {
  143. NRF_LOG_INFO("Scan timed out.");
  144. scan_start();
  145. } break;
  146. default:
  147. break;
  148. }
  149. }
  150. /**@brief Function for initializing the scanning and setting the filters.
  151. */
  152. static void scan_init(void)
  153. {
  154. ret_code_t err_code;
  155. nrf_ble_scan_init_t init_scan;
  156. memset(&init_scan, 0, sizeof(init_scan));
  157. init_scan.connect_if_match = true;
  158. init_scan.conn_cfg_tag = APP_BLE_CONN_CFG_TAG;
  159. err_code = nrf_ble_scan_init(&m_scan, &init_scan, scan_evt_handler);
  160. APP_ERROR_CHECK(err_code);
  161. err_code = nrf_ble_scan_filter_set(&m_scan, SCAN_UUID_FILTER, &m_nus_uuid);
  162. APP_ERROR_CHECK(err_code);
  163. err_code = nrf_ble_scan_filters_enable(&m_scan, NRF_BLE_SCAN_UUID_FILTER, false);
  164. APP_ERROR_CHECK(err_code);
  165. }
  166. /**@brief Function for handling database discovery events.
  167. *
  168. * @details This function is a callback function to handle events from the database discovery module.
  169. * Depending on the UUIDs that are discovered, this function forwards the events
  170. * to their respective services.
  171. *
  172. * @param[in] p_event Pointer to the database discovery event.
  173. */
  174. static void db_disc_handler(ble_db_discovery_evt_t * p_evt)
  175. {
  176. ble_nus_c_on_db_disc_evt(&m_ble_nus_c, p_evt);
  177. }
  178. /**@brief Function for handling characters received by the Nordic UART Service (NUS).
  179. *
  180. * @details This function takes a list of characters of length data_len and prints the characters out on UART.
  181. * If @ref ECHOBACK_BLE_UART_DATA is set, the data is sent back to sender.
  182. */
  183. static void ble_nus_chars_received_uart_print(uint8_t * p_data, uint16_t data_len)
  184. {
  185. ret_code_t ret_val;
  186. NRF_LOG_DEBUG("Receiving data.");
  187. NRF_LOG_HEXDUMP_DEBUG(p_data, data_len);
  188. for (uint32_t i = 0; i < data_len; i++)
  189. {
  190. do
  191. {
  192. ret_val = app_uart_put(p_data[i]);
  193. if ((ret_val != NRF_SUCCESS) && (ret_val != NRF_ERROR_BUSY))
  194. {
  195. NRF_LOG_ERROR("app_uart_put failed for index 0x%04x.", i);
  196. APP_ERROR_CHECK(ret_val);
  197. }
  198. } while (ret_val == NRF_ERROR_BUSY);
  199. }
  200. if (p_data[data_len-1] == '\r')
  201. {
  202. while (app_uart_put('\n') == NRF_ERROR_BUSY);
  203. }
  204. if (ECHOBACK_BLE_UART_DATA)
  205. {
  206. // Send data back to the peripheral.
  207. do
  208. {
  209. ret_val = ble_nus_c_string_send(&m_ble_nus_c, p_data, data_len);
  210. if ((ret_val != NRF_SUCCESS) && (ret_val != NRF_ERROR_BUSY))
  211. {
  212. NRF_LOG_ERROR("Failed sending NUS message. Error 0x%x. ", ret_val);
  213. APP_ERROR_CHECK(ret_val);
  214. }
  215. } while (ret_val == NRF_ERROR_BUSY);
  216. }
  217. }
  218. /**@brief Function for handling app_uart events.
  219. *
  220. * @details This function receives a single character from the app_uart module and appends it to
  221. * a string. The string is sent over BLE when the last character received is a
  222. * 'new line' '\n' (hex 0x0A) or if the string reaches the maximum data length.
  223. */
  224. void uart_event_handle(app_uart_evt_t * p_event)
  225. {
  226. static uint8_t data_array[BLE_NUS_MAX_DATA_LEN];
  227. static uint16_t index = 0;
  228. uint32_t ret_val;
  229. switch (p_event->evt_type)
  230. {
  231. /**@snippet [Handling data from UART] */
  232. case APP_UART_DATA_READY:
  233. UNUSED_VARIABLE(app_uart_get(&data_array[index]));
  234. index++;
  235. if ((data_array[index - 1] == '\n') ||
  236. (data_array[index - 1] == '\r') ||
  237. (index >= (m_ble_nus_max_data_len)))
  238. {
  239. NRF_LOG_DEBUG("Ready to send data over BLE NUS");
  240. NRF_LOG_HEXDUMP_DEBUG(data_array, index);
  241. do
  242. {
  243. ret_val = ble_nus_c_string_send(&m_ble_nus_c, data_array, index);
  244. if ( (ret_val != NRF_ERROR_INVALID_STATE) && (ret_val != NRF_ERROR_RESOURCES) )
  245. {
  246. APP_ERROR_CHECK(ret_val);
  247. }
  248. } while (ret_val == NRF_ERROR_RESOURCES);
  249. index = 0;
  250. }
  251. break;
  252. /**@snippet [Handling data from UART] */
  253. case APP_UART_COMMUNICATION_ERROR:
  254. NRF_LOG_ERROR("Communication error occurred while handling UART.");
  255. APP_ERROR_HANDLER(p_event->data.error_communication);
  256. break;
  257. case APP_UART_FIFO_ERROR:
  258. NRF_LOG_ERROR("Error occurred in FIFO module used by UART.");
  259. APP_ERROR_HANDLER(p_event->data.error_code);
  260. break;
  261. default:
  262. break;
  263. }
  264. }
  265. /**@brief Callback handling Nordic UART Service (NUS) client events.
  266. *
  267. * @details This function is called to notify the application of NUS client events.
  268. *
  269. * @param[in] p_ble_nus_c NUS client handle. This identifies the NUS client.
  270. * @param[in] p_ble_nus_evt Pointer to the NUS client event.
  271. */
  272. /**@snippet [Handling events from the ble_nus_c module] */
  273. static void ble_nus_c_evt_handler(ble_nus_c_t * p_ble_nus_c, ble_nus_c_evt_t const * p_ble_nus_evt)
  274. {
  275. ret_code_t err_code;
  276. switch (p_ble_nus_evt->evt_type)
  277. {
  278. case BLE_NUS_C_EVT_DISCOVERY_COMPLETE:
  279. NRF_LOG_INFO("Discovery complete.");
  280. err_code = ble_nus_c_handles_assign(p_ble_nus_c, p_ble_nus_evt->conn_handle, &p_ble_nus_evt->handles);
  281. APP_ERROR_CHECK(err_code);
  282. err_code = ble_nus_c_tx_notif_enable(p_ble_nus_c);
  283. APP_ERROR_CHECK(err_code);
  284. NRF_LOG_INFO("Connected to device with Nordic UART Service.");
  285. break;
  286. case BLE_NUS_C_EVT_NUS_TX_EVT:
  287. ble_nus_chars_received_uart_print(p_ble_nus_evt->p_data, p_ble_nus_evt->data_len);
  288. break;
  289. case BLE_NUS_C_EVT_DISCONNECTED:
  290. NRF_LOG_INFO("Disconnected.");
  291. scan_start();
  292. break;
  293. }
  294. }
  295. /**@snippet [Handling events from the ble_nus_c module] */
  296. /**
  297. * @brief Function for handling shutdown events.
  298. *
  299. * @param[in] event Shutdown type.
  300. */
  301. static bool shutdown_handler(nrf_pwr_mgmt_evt_t event)
  302. {
  303. ret_code_t err_code;
  304. err_code = bsp_indication_set(BSP_INDICATE_IDLE);
  305. APP_ERROR_CHECK(err_code);
  306. switch (event)
  307. {
  308. case NRF_PWR_MGMT_EVT_PREPARE_WAKEUP:
  309. // Prepare wakeup buttons.
  310. err_code = bsp_btn_ble_sleep_mode_prepare();
  311. APP_ERROR_CHECK(err_code);
  312. break;
  313. default:
  314. break;
  315. }
  316. return true;
  317. }
  318. NRF_PWR_MGMT_HANDLER_REGISTER(shutdown_handler, APP_SHUTDOWN_HANDLER_PRIORITY);
  319. /**@brief Function for handling BLE events.
  320. *
  321. * @param[in] p_ble_evt Bluetooth stack event.
  322. * @param[in] p_context Unused.
  323. */
  324. static void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context)
  325. {
  326. ret_code_t err_code;
  327. ble_gap_evt_t const * p_gap_evt = &p_ble_evt->evt.gap_evt;
  328. switch (p_ble_evt->header.evt_id)
  329. {
  330. case BLE_GAP_EVT_CONNECTED:
  331. err_code = ble_nus_c_handles_assign(&m_ble_nus_c, p_ble_evt->evt.gap_evt.conn_handle, NULL);
  332. APP_ERROR_CHECK(err_code);
  333. err_code = bsp_indication_set(BSP_INDICATE_CONNECTED);
  334. APP_ERROR_CHECK(err_code);
  335. // start discovery of services. The NUS Client waits for a discovery result
  336. err_code = ble_db_discovery_start(&m_db_disc, p_ble_evt->evt.gap_evt.conn_handle);
  337. APP_ERROR_CHECK(err_code);
  338. break;
  339. case BLE_GAP_EVT_DISCONNECTED:
  340. NRF_LOG_INFO("Disconnected. conn_handle: 0x%x, reason: 0x%x",
  341. p_gap_evt->conn_handle,
  342. p_gap_evt->params.disconnected.reason);
  343. break;
  344. case BLE_GAP_EVT_TIMEOUT:
  345. if (p_gap_evt->params.timeout.src == BLE_GAP_TIMEOUT_SRC_CONN)
  346. {
  347. NRF_LOG_INFO("Connection Request timed out.");
  348. }
  349. break;
  350. case BLE_GAP_EVT_SEC_PARAMS_REQUEST:
  351. // Pairing not supported.
  352. err_code = sd_ble_gap_sec_params_reply(p_ble_evt->evt.gap_evt.conn_handle, BLE_GAP_SEC_STATUS_PAIRING_NOT_SUPP, NULL, NULL);
  353. APP_ERROR_CHECK(err_code);
  354. break;
  355. case BLE_GAP_EVT_CONN_PARAM_UPDATE_REQUEST:
  356. // Accepting parameters requested by peer.
  357. err_code = sd_ble_gap_conn_param_update(p_gap_evt->conn_handle,
  358. &p_gap_evt->params.conn_param_update_request.conn_params);
  359. APP_ERROR_CHECK(err_code);
  360. break;
  361. case BLE_GAP_EVT_PHY_UPDATE_REQUEST:
  362. {
  363. NRF_LOG_DEBUG("PHY update request.");
  364. ble_gap_phys_t const phys =
  365. {
  366. .rx_phys = BLE_GAP_PHY_AUTO,
  367. .tx_phys = BLE_GAP_PHY_AUTO,
  368. };
  369. err_code = sd_ble_gap_phy_update(p_ble_evt->evt.gap_evt.conn_handle, &phys);
  370. APP_ERROR_CHECK(err_code);
  371. } break;
  372. case BLE_GATTC_EVT_TIMEOUT:
  373. // Disconnect on GATT Client timeout event.
  374. NRF_LOG_DEBUG("GATT Client Timeout.");
  375. err_code = sd_ble_gap_disconnect(p_ble_evt->evt.gattc_evt.conn_handle,
  376. BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
  377. APP_ERROR_CHECK(err_code);
  378. break;
  379. case BLE_GATTS_EVT_TIMEOUT:
  380. // Disconnect on GATT Server timeout event.
  381. NRF_LOG_DEBUG("GATT Server Timeout.");
  382. err_code = sd_ble_gap_disconnect(p_ble_evt->evt.gatts_evt.conn_handle,
  383. BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
  384. APP_ERROR_CHECK(err_code);
  385. break;
  386. default:
  387. break;
  388. }
  389. }
  390. /**@brief Function for initializing the BLE stack.
  391. *
  392. * @details Initializes the SoftDevice and the BLE event interrupt.
  393. */
  394. static void ble_stack_init(void)
  395. {
  396. ret_code_t err_code;
  397. err_code = nrf_sdh_enable_request();
  398. APP_ERROR_CHECK(err_code);
  399. // Configure the BLE stack using the default settings.
  400. // Fetch the start address of the application RAM.
  401. uint32_t ram_start = 0;
  402. err_code = nrf_sdh_ble_default_cfg_set(APP_BLE_CONN_CFG_TAG, &ram_start);
  403. APP_ERROR_CHECK(err_code);
  404. // Enable BLE stack.
  405. err_code = nrf_sdh_ble_enable(&ram_start);
  406. APP_ERROR_CHECK(err_code);
  407. // Register a handler for BLE events.
  408. NRF_SDH_BLE_OBSERVER(m_ble_observer, APP_BLE_OBSERVER_PRIO, ble_evt_handler, NULL);
  409. }
  410. /**@brief Function for handling events from the GATT library. */
  411. void gatt_evt_handler(nrf_ble_gatt_t * p_gatt, nrf_ble_gatt_evt_t const * p_evt)
  412. {
  413. if (p_evt->evt_id == NRF_BLE_GATT_EVT_ATT_MTU_UPDATED)
  414. {
  415. NRF_LOG_INFO("ATT MTU exchange completed.");
  416. m_ble_nus_max_data_len = p_evt->params.att_mtu_effective - OPCODE_LENGTH - HANDLE_LENGTH;
  417. NRF_LOG_INFO("Ble NUS max data length set to 0x%X(%d)", m_ble_nus_max_data_len, m_ble_nus_max_data_len);
  418. }
  419. }
  420. /**@brief Function for initializing the GATT library. */
  421. void gatt_init(void)
  422. {
  423. ret_code_t err_code;
  424. err_code = nrf_ble_gatt_init(&m_gatt, gatt_evt_handler);
  425. APP_ERROR_CHECK(err_code);
  426. err_code = nrf_ble_gatt_att_mtu_central_set(&m_gatt, NRF_SDH_BLE_GATT_MAX_MTU_SIZE);
  427. APP_ERROR_CHECK(err_code);
  428. }
  429. /**@brief Function for handling events from the BSP module.
  430. *
  431. * @param[in] event Event generated by button press.
  432. */
  433. void bsp_event_handler(bsp_event_t event)
  434. {
  435. ret_code_t err_code;
  436. switch (event)
  437. {
  438. case BSP_EVENT_SLEEP:
  439. nrf_pwr_mgmt_shutdown(NRF_PWR_MGMT_SHUTDOWN_GOTO_SYSOFF);
  440. break;
  441. case BSP_EVENT_DISCONNECT:
  442. err_code = sd_ble_gap_disconnect(m_ble_nus_c.conn_handle,
  443. BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
  444. if (err_code != NRF_ERROR_INVALID_STATE)
  445. {
  446. APP_ERROR_CHECK(err_code);
  447. }
  448. break;
  449. default:
  450. break;
  451. }
  452. }
  453. /**@brief Function for initializing the UART. */
  454. static void uart_init(void)
  455. {
  456. ret_code_t err_code;
  457. app_uart_comm_params_t const comm_params =
  458. {
  459. .rx_pin_no = RX_PIN_NUMBER,
  460. .tx_pin_no = TX_PIN_NUMBER,
  461. .rts_pin_no = RTS_PIN_NUMBER,
  462. .cts_pin_no = CTS_PIN_NUMBER,
  463. .flow_control = APP_UART_FLOW_CONTROL_DISABLED,
  464. .use_parity = false,
  465. .baud_rate = UART_BAUDRATE_BAUDRATE_Baud921600
  466. };
  467. APP_UART_FIFO_INIT(&comm_params,
  468. UART_RX_BUF_SIZE,
  469. UART_TX_BUF_SIZE,
  470. uart_event_handle,
  471. APP_IRQ_PRIORITY_LOWEST,
  472. err_code);
  473. APP_ERROR_CHECK(err_code);
  474. }
  475. /**@brief Function for initializing the Nordic UART Service (NUS) client. */
  476. static void nus_c_init(void)
  477. {
  478. ret_code_t err_code;
  479. ble_nus_c_init_t init;
  480. init.evt_handler = ble_nus_c_evt_handler;
  481. init.error_handler = nus_error_handler;
  482. init.p_gatt_queue = &m_ble_gatt_queue;
  483. err_code = ble_nus_c_init(&m_ble_nus_c, &init);
  484. APP_ERROR_CHECK(err_code);
  485. }
  486. /**@brief Function for initializing buttons and leds. */
  487. static void buttons_leds_init(void)
  488. {
  489. ret_code_t err_code;
  490. bsp_event_t startup_event;
  491. err_code = bsp_init(BSP_INIT_LEDS, bsp_event_handler);
  492. APP_ERROR_CHECK(err_code);
  493. err_code = bsp_btn_ble_init(NULL, &startup_event);
  494. APP_ERROR_CHECK(err_code);
  495. }
  496. /**@brief Function for initializing the timer. */
  497. static void timer_init(void)
  498. {
  499. ret_code_t err_code = app_timer_init();
  500. APP_ERROR_CHECK(err_code);
  501. }
  502. /**@brief Function for initializing the nrf log module. */
  503. static void log_init(void)
  504. {
  505. ret_code_t err_code = NRF_LOG_INIT(NULL);
  506. APP_ERROR_CHECK(err_code);
  507. NRF_LOG_DEFAULT_BACKENDS_INIT();
  508. }
  509. /**@brief Function for initializing power management.
  510. */
  511. static void power_management_init(void)
  512. {
  513. ret_code_t err_code;
  514. err_code = nrf_pwr_mgmt_init();
  515. APP_ERROR_CHECK(err_code);
  516. }
  517. /** @brief Function for initializing the database discovery module. */
  518. static void db_discovery_init(void)
  519. {
  520. ble_db_discovery_init_t db_init;
  521. memset(&db_init, 0, sizeof(ble_db_discovery_init_t));
  522. db_init.evt_handler = db_disc_handler;
  523. db_init.p_gatt_queue = &m_ble_gatt_queue;
  524. ret_code_t err_code = ble_db_discovery_init(&db_init);
  525. APP_ERROR_CHECK(err_code);
  526. }
  527. /**@brief Function for handling the idle state (main loop).
  528. *
  529. * @details Handles any pending log operations, then sleeps until the next event occurs.
  530. */
  531. static void idle_state_handle(void)
  532. {
  533. if (NRF_LOG_PROCESS() == false)
  534. {
  535. nrf_pwr_mgmt_run();
  536. }
  537. }
  538. int main(void)
  539. {
  540. // Initialize.
  541. log_init();
  542. timer_init();
  543. uart_init();
  544. buttons_leds_init();
  545. db_discovery_init();
  546. power_management_init();
  547. ble_stack_init();
  548. gatt_init();
  549. nus_c_init();
  550. scan_init();
  551. // Start execution.
  552. printf("BLE UART central example started.\r\n");
  553. NRF_LOG_INFO("BLE UART central example started.");
  554. scan_start();
  555. // Enter main loop.
  556. for (;;)
  557. {
  558. idle_state_handle();
  559. }
  560. }