单导联心电记录仪
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.

584 lines
20 KiB

1 year ago
  1. #include <stdint.h>
  2. #include <string.h>
  3. #include "app_timer.h"
  4. #include "app_uart.h"
  5. #include "app_util_platform.h"
  6. #include "ble_advdata.h"
  7. #include "ble_advertising.h"
  8. #include "ble_conn_params.h"
  9. #include "ble_hci.h"
  10. #include "ble_nus.h"
  11. #include "bsp_btn_ble.h"
  12. #include "nordic_common.h"
  13. #include "nrf.h"
  14. #include "nrf_ble_gatt.h"
  15. #include "nrf_ble_qwr.h"
  16. #include "nrf_pwr_mgmt.h"
  17. #include "nrf_sdh.h"
  18. #include "nrf_sdh_ble.h"
  19. #include "nrf_sdh_soc.h"
  20. #if defined(UART_PRESENT)
  21. #include "nrf_uart.h"
  22. #endif
  23. #if defined(UARTE_PRESENT)
  24. #include "nrf_uarte.h"
  25. #endif
  26. #include "nrf_log.h"
  27. #include "nrf_log_ctrl.h"
  28. #include "nrf_log_default_backends.h"
  29. #define APP_BLE_CONN_CFG_TAG 1 /**< A tag identifying the SoftDevice BLE configuration. */
  30. #define DEVICE_NAME "Nordic_UART" /**< Name of device. Will be included in the advertising data. */
  31. #define NUS_SERVICE_UUID_TYPE BLE_UUID_TYPE_VENDOR_BEGIN /**< UUID type for the Nordic UART Service (vendor specific). */
  32. #define APP_BLE_OBSERVER_PRIO 3 /**< Application's BLE observer priority. You shouldn't need to modify this value. */
  33. #define APP_ADV_INTERVAL 64 /**< The advertising interval (in units of 0.625 ms. This value corresponds to 40 ms). */
  34. #define APP_ADV_DURATION 18000 /**< The advertising duration (180 seconds) in units of 10 milliseconds. */
  35. #define MIN_CONN_INTERVAL MSEC_TO_UNITS(20, UNIT_1_25_MS) /**< Minimum acceptable connection interval (20 ms), Connection interval uses 1.25 ms units. */
  36. #define MAX_CONN_INTERVAL MSEC_TO_UNITS(75, UNIT_1_25_MS) /**< Maximum acceptable connection interval (75 ms), Connection interval uses 1.25 ms units. */
  37. #define SLAVE_LATENCY 0 /**< Slave latency. */
  38. #define CONN_SUP_TIMEOUT MSEC_TO_UNITS(4000, UNIT_10_MS) /**< Connection supervisory timeout (4 seconds), Supervision Timeout uses 10 ms units. */
  39. #define FIRST_CONN_PARAMS_UPDATE_DELAY APP_TIMER_TICKS(5000) /**< Time from initiating event (connect or start of notification) to first time sd_ble_gap_conn_param_update is called (5 seconds). */
  40. #define NEXT_CONN_PARAMS_UPDATE_DELAY APP_TIMER_TICKS(30000) /**< Time between each call to sd_ble_gap_conn_param_update after the first call (30 seconds). */
  41. #define MAX_CONN_PARAMS_UPDATE_COUNT 3 /**< Number of attempts before giving up the connection parameter negotiation. */
  42. #define DEAD_BEEF 0xDEADBEEF /**< Value used as error code on stack dump, can be used to identify stack location on stack unwind. */
  43. #define UART_TX_BUF_SIZE 256 /**< UART TX buffer size. */
  44. #define UART_RX_BUF_SIZE 256 /**< UART RX buffer size. */
  45. BLE_NUS_DEF(m_nus, NRF_SDH_BLE_TOTAL_LINK_COUNT); /**< BLE NUS service instance. */
  46. NRF_BLE_GATT_DEF(m_gatt); /**< GATT module instance. */
  47. NRF_BLE_QWR_DEF(m_qwr); /**< Context for the Queued Write module.*/
  48. BLE_ADVERTISING_DEF(m_advertising); /**< Advertising module instance. */
  49. static uint16_t m_conn_handle = BLE_CONN_HANDLE_INVALID; /**< Handle of the current connection. */
  50. static uint16_t m_ble_nus_max_data_len = BLE_GATT_ATT_MTU_DEFAULT - 3; /**< Maximum length of data (in bytes) that can be transmitted to the peer by the Nordic UART service module. */
  51. static ble_uuid_t m_adv_uuids[] = /**< Universally unique service identifier. */
  52. {{BLE_UUID_NUS_SERVICE, NUS_SERVICE_UUID_TYPE}};
  53. /**@brief Function for assert macro callback.
  54. *
  55. * @details This function will be called in case of an assert in the SoftDevice.
  56. *
  57. * @warning This handler is an example only and does not fit a final product. You need to analyse
  58. * how your product is supposed to react in case of Assert.
  59. * @warning On assert from the SoftDevice, the system can only recover on reset.
  60. *
  61. * @param[in] line_num Line number of the failing ASSERT call.
  62. * @param[in] p_file_name File name of the failing ASSERT call.
  63. */
  64. void assert_nrf_callback(uint16_t line_num, const uint8_t *p_file_name) { app_error_handler(DEAD_BEEF, line_num, p_file_name); }
  65. /**@brief Function for initializing the timer module.
  66. */
  67. static void timers_init(void) {
  68. ret_code_t err_code = app_timer_init();
  69. APP_ERROR_CHECK(err_code);
  70. }
  71. /**@brief Function for the GAP initialization.
  72. *
  73. * @details This function will set up all the necessary GAP (Generic Access Profile) parameters of
  74. * the device. It also sets the permissions and appearance.
  75. */
  76. static void gap_params_init(void) {
  77. uint32_t err_code;
  78. ble_gap_conn_params_t gap_conn_params;
  79. ble_gap_conn_sec_mode_t sec_mode;
  80. BLE_GAP_CONN_SEC_MODE_SET_OPEN(&sec_mode);
  81. err_code = sd_ble_gap_device_name_set(&sec_mode, (const uint8_t *)DEVICE_NAME, strlen(DEVICE_NAME));
  82. APP_ERROR_CHECK(err_code);
  83. memset(&gap_conn_params, 0, sizeof(gap_conn_params));
  84. gap_conn_params.min_conn_interval = MIN_CONN_INTERVAL;
  85. gap_conn_params.max_conn_interval = MAX_CONN_INTERVAL;
  86. gap_conn_params.slave_latency = SLAVE_LATENCY;
  87. gap_conn_params.conn_sup_timeout = CONN_SUP_TIMEOUT;
  88. err_code = sd_ble_gap_ppcp_set(&gap_conn_params);
  89. APP_ERROR_CHECK(err_code);
  90. }
  91. /**@brief Function for handling Queued Write Module errors.
  92. *
  93. * @details A pointer to this function will be passed to each service which may need to inform the
  94. * application about an error.
  95. *
  96. * @param[in] nrf_error Error code containing information about what went wrong.
  97. */
  98. static void nrf_qwr_error_handler(uint32_t nrf_error) { APP_ERROR_HANDLER(nrf_error); }
  99. /**@brief Function for handling the data from the Nordic UART Service.
  100. *
  101. * @details This function will process the data received from the Nordic UART BLE Service and send
  102. * it to the UART module.
  103. *
  104. * @param[in] p_evt Nordic UART Service event.
  105. */
  106. /**@snippet [Handling the data received over BLE] */
  107. static void nus_data_handler(ble_nus_evt_t *p_evt) {
  108. if (p_evt->type == BLE_NUS_EVT_RX_DATA) {
  109. uint32_t err_code;
  110. NRF_LOG_DEBUG("Received data from BLE NUS. Writing data on UART.");
  111. NRF_LOG_HEXDUMP_DEBUG(p_evt->params.rx_data.p_data, p_evt->params.rx_data.length);
  112. for (uint32_t i = 0; i < p_evt->params.rx_data.length; i++) {
  113. do {
  114. err_code = app_uart_put(p_evt->params.rx_data.p_data[i]);
  115. if ((err_code != NRF_SUCCESS) && (err_code != NRF_ERROR_BUSY)) {
  116. NRF_LOG_ERROR("Failed receiving NUS message. Error 0x%x. ", err_code);
  117. APP_ERROR_CHECK(err_code);
  118. }
  119. } while (err_code == NRF_ERROR_BUSY);
  120. }
  121. if (p_evt->params.rx_data.p_data[p_evt->params.rx_data.length - 1] == '\r') {
  122. while (app_uart_put('\n') == NRF_ERROR_BUSY)
  123. ;
  124. }
  125. }
  126. }
  127. /**@snippet [Handling the data received over BLE] */
  128. /**@brief Function for initializing services that will be used by the application.
  129. */
  130. static void services_init(void) {
  131. uint32_t err_code;
  132. ble_nus_init_t nus_init;
  133. nrf_ble_qwr_init_t qwr_init = {0};
  134. // Initialize Queued Write Module.
  135. qwr_init.error_handler = nrf_qwr_error_handler;
  136. err_code = nrf_ble_qwr_init(&m_qwr, &qwr_init);
  137. APP_ERROR_CHECK(err_code);
  138. // Initialize NUS.
  139. memset(&nus_init, 0, sizeof(nus_init));
  140. nus_init.data_handler = nus_data_handler;
  141. err_code = ble_nus_init(&m_nus, &nus_init);
  142. APP_ERROR_CHECK(err_code);
  143. }
  144. /**@brief Function for handling an event from the Connection Parameters Module.
  145. *
  146. * @details This function will be called for all events in the Connection Parameters Module
  147. * which are passed to the application.
  148. *
  149. * @note All this function does is to disconnect. This could have been done by simply setting
  150. * the disconnect_on_fail config parameter, but instead we use the event handler
  151. * mechanism to demonstrate its use.
  152. *
  153. * @param[in] p_evt Event received from the Connection Parameters Module.
  154. */
  155. static void on_conn_params_evt(ble_conn_params_evt_t *p_evt) {
  156. uint32_t err_code;
  157. if (p_evt->evt_type == BLE_CONN_PARAMS_EVT_FAILED) {
  158. err_code = sd_ble_gap_disconnect(m_conn_handle, BLE_HCI_CONN_INTERVAL_UNACCEPTABLE);
  159. APP_ERROR_CHECK(err_code);
  160. }
  161. }
  162. /**@brief Function for handling errors from the Connection Parameters module.
  163. *
  164. * @param[in] nrf_error Error code containing information about what went wrong.
  165. */
  166. static void conn_params_error_handler(uint32_t nrf_error) { APP_ERROR_HANDLER(nrf_error); }
  167. /**@brief Function for initializing the Connection Parameters module.
  168. */
  169. static void conn_params_init(void) {
  170. uint32_t err_code;
  171. ble_conn_params_init_t cp_init;
  172. memset(&cp_init, 0, sizeof(cp_init));
  173. cp_init.p_conn_params = NULL;
  174. cp_init.first_conn_params_update_delay = FIRST_CONN_PARAMS_UPDATE_DELAY;
  175. cp_init.next_conn_params_update_delay = NEXT_CONN_PARAMS_UPDATE_DELAY;
  176. cp_init.max_conn_params_update_count = MAX_CONN_PARAMS_UPDATE_COUNT;
  177. cp_init.start_on_notify_cccd_handle = BLE_GATT_HANDLE_INVALID;
  178. cp_init.disconnect_on_fail = false;
  179. cp_init.evt_handler = on_conn_params_evt;
  180. cp_init.error_handler = conn_params_error_handler;
  181. err_code = ble_conn_params_init(&cp_init);
  182. APP_ERROR_CHECK(err_code);
  183. }
  184. /**@brief Function for putting the chip into sleep mode.
  185. *
  186. * @note This function will not return.
  187. */
  188. static void sleep_mode_enter(void) {
  189. uint32_t err_code = bsp_indication_set(BSP_INDICATE_IDLE);
  190. APP_ERROR_CHECK(err_code);
  191. // Prepare wakeup buttons.
  192. err_code = bsp_btn_ble_sleep_mode_prepare();
  193. APP_ERROR_CHECK(err_code);
  194. // Go to system-off mode (this function will not return; wakeup will cause a reset).
  195. err_code = sd_power_system_off();
  196. APP_ERROR_CHECK(err_code);
  197. }
  198. /**@brief Function for handling advertising events.
  199. *
  200. * @details This function will be called for advertising events which are passed to the application.
  201. *
  202. * @param[in] ble_adv_evt Advertising event.
  203. */
  204. static void on_adv_evt(ble_adv_evt_t ble_adv_evt) {
  205. uint32_t err_code;
  206. switch (ble_adv_evt) {
  207. case BLE_ADV_EVT_FAST:
  208. err_code = bsp_indication_set(BSP_INDICATE_ADVERTISING);
  209. APP_ERROR_CHECK(err_code);
  210. break;
  211. case BLE_ADV_EVT_IDLE:
  212. sleep_mode_enter();
  213. break;
  214. default:
  215. break;
  216. }
  217. }
  218. /**@brief Function for handling BLE events.
  219. *
  220. * @param[in] p_ble_evt Bluetooth stack event.
  221. * @param[in] p_context Unused.
  222. */
  223. static void ble_evt_handler(ble_evt_t const *p_ble_evt, void *p_context) {
  224. uint32_t err_code;
  225. switch (p_ble_evt->header.evt_id) {
  226. case BLE_GAP_EVT_CONNECTED:
  227. NRF_LOG_INFO("Connected");
  228. err_code = bsp_indication_set(BSP_INDICATE_CONNECTED);
  229. APP_ERROR_CHECK(err_code);
  230. m_conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
  231. err_code = nrf_ble_qwr_conn_handle_assign(&m_qwr, m_conn_handle);
  232. APP_ERROR_CHECK(err_code);
  233. break;
  234. case BLE_GAP_EVT_DISCONNECTED:
  235. NRF_LOG_INFO("Disconnected");
  236. // LED indication will be changed when advertising starts.
  237. m_conn_handle = BLE_CONN_HANDLE_INVALID;
  238. break;
  239. case BLE_GAP_EVT_PHY_UPDATE_REQUEST: {
  240. NRF_LOG_DEBUG("PHY update request.");
  241. ble_gap_phys_t const phys = {
  242. .rx_phys = BLE_GAP_PHY_AUTO,
  243. .tx_phys = BLE_GAP_PHY_AUTO,
  244. };
  245. err_code = sd_ble_gap_phy_update(p_ble_evt->evt.gap_evt.conn_handle, &phys);
  246. APP_ERROR_CHECK(err_code);
  247. } break;
  248. case BLE_GAP_EVT_SEC_PARAMS_REQUEST:
  249. // Pairing not supported
  250. err_code = sd_ble_gap_sec_params_reply(m_conn_handle, BLE_GAP_SEC_STATUS_PAIRING_NOT_SUPP, NULL, NULL);
  251. APP_ERROR_CHECK(err_code);
  252. break;
  253. case BLE_GATTS_EVT_SYS_ATTR_MISSING:
  254. // No system attributes have been stored.
  255. err_code = sd_ble_gatts_sys_attr_set(m_conn_handle, NULL, 0, 0);
  256. APP_ERROR_CHECK(err_code);
  257. break;
  258. case BLE_GATTC_EVT_TIMEOUT:
  259. // Disconnect on GATT Client timeout event.
  260. err_code = sd_ble_gap_disconnect(p_ble_evt->evt.gattc_evt.conn_handle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
  261. APP_ERROR_CHECK(err_code);
  262. break;
  263. case BLE_GATTS_EVT_TIMEOUT:
  264. // Disconnect on GATT Server timeout event.
  265. err_code = sd_ble_gap_disconnect(p_ble_evt->evt.gatts_evt.conn_handle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
  266. APP_ERROR_CHECK(err_code);
  267. break;
  268. default:
  269. // No implementation needed.
  270. break;
  271. }
  272. }
  273. /**@brief Function for the SoftDevice initialization.
  274. *
  275. * @details This function initializes the SoftDevice and the BLE event interrupt.
  276. */
  277. static void ble_stack_init(void) {
  278. ret_code_t err_code;
  279. err_code = nrf_sdh_enable_request();
  280. APP_ERROR_CHECK(err_code);
  281. // Configure the BLE stack using the default settings.
  282. // Fetch the start address of the application RAM.
  283. uint32_t ram_start = 0;
  284. err_code = nrf_sdh_ble_default_cfg_set(APP_BLE_CONN_CFG_TAG, &ram_start);
  285. APP_ERROR_CHECK(err_code);
  286. // Enable BLE stack.
  287. err_code = nrf_sdh_ble_enable(&ram_start);
  288. APP_ERROR_CHECK(err_code);
  289. // Register a handler for BLE events.
  290. NRF_SDH_BLE_OBSERVER(m_ble_observer, APP_BLE_OBSERVER_PRIO, ble_evt_handler, NULL);
  291. }
  292. /**@brief Function for handling events from the GATT library. */
  293. void gatt_evt_handler(nrf_ble_gatt_t *p_gatt, nrf_ble_gatt_evt_t const *p_evt) {
  294. if ((m_conn_handle == p_evt->conn_handle) && (p_evt->evt_id == NRF_BLE_GATT_EVT_ATT_MTU_UPDATED)) {
  295. m_ble_nus_max_data_len = p_evt->params.att_mtu_effective - OPCODE_LENGTH - HANDLE_LENGTH;
  296. NRF_LOG_INFO("Data len is set to 0x%X(%d)", m_ble_nus_max_data_len, m_ble_nus_max_data_len);
  297. }
  298. NRF_LOG_DEBUG("ATT MTU exchange completed. central 0x%x peripheral 0x%x", p_gatt->att_mtu_desired_central, p_gatt->att_mtu_desired_periph);
  299. }
  300. /**@brief Function for initializing the GATT library. */
  301. void gatt_init(void) {
  302. ret_code_t err_code;
  303. err_code = nrf_ble_gatt_init(&m_gatt, gatt_evt_handler);
  304. APP_ERROR_CHECK(err_code);
  305. err_code = nrf_ble_gatt_att_mtu_periph_set(&m_gatt, NRF_SDH_BLE_GATT_MAX_MTU_SIZE);
  306. APP_ERROR_CHECK(err_code);
  307. }
  308. /**@brief Function for handling events from the BSP module.
  309. *
  310. * @param[in] event Event generated by button press.
  311. */
  312. void bsp_event_handler(bsp_event_t event) {
  313. uint32_t err_code;
  314. switch (event) {
  315. case BSP_EVENT_SLEEP:
  316. sleep_mode_enter();
  317. break;
  318. case BSP_EVENT_DISCONNECT:
  319. err_code = sd_ble_gap_disconnect(m_conn_handle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
  320. if (err_code != NRF_ERROR_INVALID_STATE) {
  321. APP_ERROR_CHECK(err_code);
  322. }
  323. break;
  324. case BSP_EVENT_WHITELIST_OFF:
  325. if (m_conn_handle == BLE_CONN_HANDLE_INVALID) {
  326. err_code = ble_advertising_restart_without_whitelist(&m_advertising);
  327. if (err_code != NRF_ERROR_INVALID_STATE) {
  328. APP_ERROR_CHECK(err_code);
  329. }
  330. }
  331. break;
  332. default:
  333. break;
  334. }
  335. }
  336. /**@brief Function for handling app_uart events.
  337. *
  338. * @details This function will receive a single character from the app_uart module and append it to
  339. * a string. The string will be be sent over BLE when the last character received was a
  340. * 'new line' '\n' (hex 0x0A) or if the string has reached the maximum data length.
  341. */
  342. /**@snippet [Handling the data received over UART] */
  343. void uart_event_handle(app_uart_evt_t *p_event) {
  344. static uint8_t data_array[BLE_NUS_MAX_DATA_LEN];
  345. static uint8_t index = 0;
  346. uint32_t err_code;
  347. switch (p_event->evt_type) {
  348. case APP_UART_DATA_READY:
  349. UNUSED_VARIABLE(app_uart_get(&data_array[index]));
  350. index++;
  351. if ((data_array[index - 1] == '\n') || (data_array[index - 1] == '\r') || (index >= m_ble_nus_max_data_len)) {
  352. if (index > 1) {
  353. NRF_LOG_DEBUG("Ready to send data over BLE NUS");
  354. NRF_LOG_HEXDUMP_DEBUG(data_array, index);
  355. do {
  356. uint16_t length = (uint16_t)index;
  357. err_code = ble_nus_data_send(&m_nus, data_array, &length, m_conn_handle);
  358. if ((err_code != NRF_ERROR_INVALID_STATE) && (err_code != NRF_ERROR_RESOURCES) && (err_code != NRF_ERROR_NOT_FOUND)) {
  359. APP_ERROR_CHECK(err_code);
  360. }
  361. } while (err_code == NRF_ERROR_RESOURCES);
  362. }
  363. index = 0;
  364. }
  365. break;
  366. case APP_UART_COMMUNICATION_ERROR:
  367. APP_ERROR_HANDLER(p_event->data.error_communication);
  368. break;
  369. case APP_UART_FIFO_ERROR:
  370. APP_ERROR_HANDLER(p_event->data.error_code);
  371. break;
  372. default:
  373. break;
  374. }
  375. }
  376. /**@snippet [Handling the data received over UART] */
  377. /**@brief Function for initializing the UART module.
  378. */
  379. /**@snippet [UART Initialization] */
  380. static void uart_init(void) {
  381. uint32_t err_code;
  382. app_uart_comm_params_t const comm_params = {
  383. .rx_pin_no = RX_PIN_NUMBER,
  384. .tx_pin_no = TX_PIN_NUMBER,
  385. .rts_pin_no = RTS_PIN_NUMBER,
  386. .cts_pin_no = CTS_PIN_NUMBER,
  387. .flow_control = APP_UART_FLOW_CONTROL_DISABLED,
  388. .use_parity = false,
  389. #if defined(UART_PRESENT)
  390. .baud_rate = NRF_UART_BAUDRATE_115200
  391. #else
  392. .baud_rate = NRF_UARTE_BAUDRATE_115200
  393. #endif
  394. };
  395. APP_UART_FIFO_INIT(&comm_params, UART_RX_BUF_SIZE, UART_TX_BUF_SIZE, uart_event_handle, APP_IRQ_PRIORITY_LOWEST, err_code);
  396. APP_ERROR_CHECK(err_code);
  397. }
  398. /**@snippet [UART Initialization] */
  399. /**@brief Function for initializing the Advertising functionality.
  400. */
  401. static void advertising_init(void) {
  402. uint32_t err_code;
  403. ble_advertising_init_t init;
  404. memset(&init, 0, sizeof(init));
  405. init.advdata.name_type = BLE_ADVDATA_FULL_NAME;
  406. init.advdata.include_appearance = false;
  407. init.advdata.flags = BLE_GAP_ADV_FLAGS_LE_ONLY_LIMITED_DISC_MODE;
  408. init.srdata.uuids_complete.uuid_cnt = sizeof(m_adv_uuids) / sizeof(m_adv_uuids[0]);
  409. init.srdata.uuids_complete.p_uuids = m_adv_uuids;
  410. init.config.ble_adv_fast_enabled = true;
  411. init.config.ble_adv_fast_interval = APP_ADV_INTERVAL;
  412. init.config.ble_adv_fast_timeout = APP_ADV_DURATION;
  413. init.evt_handler = on_adv_evt;
  414. err_code = ble_advertising_init(&m_advertising, &init);
  415. APP_ERROR_CHECK(err_code);
  416. ble_advertising_conn_cfg_tag_set(&m_advertising, APP_BLE_CONN_CFG_TAG);
  417. }
  418. /**@brief Function for initializing buttons and leds.
  419. *
  420. * @param[out] p_erase_bonds Will be true if the clear bonding button was pressed to wake the application up.
  421. */
  422. static void buttons_leds_init(bool *p_erase_bonds) {
  423. bsp_event_t startup_event;
  424. uint32_t err_code = bsp_init(BSP_INIT_LEDS | BSP_INIT_BUTTONS, bsp_event_handler);
  425. APP_ERROR_CHECK(err_code);
  426. err_code = bsp_btn_ble_init(NULL, &startup_event);
  427. APP_ERROR_CHECK(err_code);
  428. *p_erase_bonds = (startup_event == BSP_EVENT_CLEAR_BONDING_DATA);
  429. }
  430. /**@brief Function for initializing the nrf log module.
  431. */
  432. static void log_init(void) {
  433. ret_code_t err_code = NRF_LOG_INIT(NULL);
  434. APP_ERROR_CHECK(err_code);
  435. NRF_LOG_DEFAULT_BACKENDS_INIT();
  436. }
  437. /**@brief Function for initializing power management.
  438. */
  439. static void power_management_init(void) {
  440. ret_code_t err_code;
  441. err_code = nrf_pwr_mgmt_init();
  442. APP_ERROR_CHECK(err_code);
  443. }
  444. /**@brief Function for handling the idle state (main loop).
  445. *
  446. * @details If there is no pending log operation, then sleep until next the next event occurs.
  447. */
  448. static void idle_state_handle(void) {
  449. if (NRF_LOG_PROCESS() == false) {
  450. nrf_pwr_mgmt_run();
  451. }
  452. }
  453. /**@brief Function for starting advertising.
  454. */
  455. static void advertising_start(void) {
  456. uint32_t err_code = ble_advertising_start(&m_advertising, BLE_ADV_MODE_FAST);
  457. APP_ERROR_CHECK(err_code);
  458. }
  459. /**@brief Application main function.
  460. */
  461. int main(void) {
  462. bool erase_bonds;
  463. // // Initialize.
  464. // uart_init();
  465. // log_init();
  466. // timers_init();
  467. // buttons_leds_init(&erase_bonds);
  468. // power_management_init();
  469. // ble_stack_init();
  470. // gap_params_init();
  471. // gatt_init();
  472. services_init();
  473. advertising_init();
  474. conn_params_init();
  475. // Start execution.
  476. printf("\r\nUART started.\r\n");
  477. NRF_LOG_INFO("Debug logging for UART over RTT started.");
  478. advertising_start();
  479. // Enter main loop.
  480. for (;;) {
  481. idle_state_handle();
  482. }
  483. }
  484. /**
  485. * @}
  486. */