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

275 lines
12 KiB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
  1. #include "zble_module.h"
  2. #include <stdint.h>
  3. #include <string.h>
  4. #include "sys.h"
  5. BLE_NUS_DEF(m_nus, NRF_SDH_BLE_TOTAL_LINK_COUNT); /**< BLE NUS service instance. */
  6. NRF_BLE_QWR_DEF(m_qwr); /**< Context for the Queued Write module.*/
  7. /*******************************************************************************
  8. * 广 *
  9. *******************************************************************************/
  10. #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. */
  11. #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. */
  12. #define SLAVE_LATENCY 0 /**< Slave latency. */
  13. #define CONN_SUP_TIMEOUT MSEC_TO_UNITS(4000, UNIT_10_MS) /**< Connection supervisory timeout (4 seconds), Supervision Timeout uses 10 ms units. */
  14. #define NUS_SERVICE_UUID_TYPE BLE_UUID_TYPE_VENDOR_BEGIN /**< UUID type for the Nordic UART Service (vendor specific). */
  15. static ble_uuid_t m_adv_uuids[] = /**< Universally unique service identifier. */
  16. {{BLE_UUID_NUS_SERVICE, NUS_SERVICE_UUID_TYPE}};
  17. #define APP_ADV_INTERVAL 64 /**< The advertising interval (in units of 0.625 ms. This value corresponds to 40 ms). */
  18. #define APP_ADV_DURATION 18000 /**< The advertising duration (180 seconds) in units of 10 milliseconds. */
  19. BLE_ADVERTISING_DEF(m_advertising); /**< Advertising module instance. */
  20. /*******************************************************************************
  21. * GATT配置 *
  22. *******************************************************************************/
  23. NRF_BLE_GATT_DEF(m_gatt); /**< GATT module instance. */
  24. /*******************************************************************************
  25. * *
  26. *******************************************************************************/
  27. #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). */
  28. #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). */
  29. #define MAX_CONN_PARAMS_UPDATE_COUNT 3 /**< Number of attempts before giving up the connection parameter negotiation. */
  30. /*******************************************************************************
  31. * CODE *
  32. *******************************************************************************/
  33. static uint16_t m_conn_handle = BLE_CONN_HANDLE_INVALID; /**< 当前连接句柄 */
  34. static uint16_t m_mtu_size = BLE_GATT_ATT_MTU_DEFAULT - 3;
  35. /**@brief Function for putting the chip into sleep mode.
  36. *
  37. * @note This function will not return.
  38. */
  39. static void sleep_mode_enter(void) {
  40. #if 0
  41. uint32_t err_code = bsp_indication_set(BSP_INDICATE_IDLE);
  42. ZERROR_CHECK(err_code);
  43. // Prepare wakeup buttons.
  44. err_code = bsp_btn_ble_sleep_mode_prepare();
  45. ZERROR_CHECK(err_code);
  46. // Go to system-off mode (this function will not return; wakeup will cause a reset).
  47. err_code = sd_power_system_off();
  48. ZERROR_CHECK(err_code);
  49. #endif
  50. }
  51. static void ble_evt_handler(ble_evt_t const* p_ble_evt, void* p_context) {
  52. uint32_t err_code;
  53. switch (p_ble_evt->header.evt_id) {
  54. case BLE_GAP_EVT_CONNECTED:
  55. NRF_LOG_INFO("Connected");
  56. m_conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
  57. break;
  58. case BLE_GAP_EVT_DISCONNECTED:
  59. NRF_LOG_INFO("Disconnected");
  60. m_conn_handle = BLE_CONN_HANDLE_INVALID;
  61. break;
  62. case BLE_GAP_EVT_PHY_UPDATE_REQUEST: {
  63. NRF_LOG_DEBUG("PHY update request.");
  64. ble_gap_phys_t const phys = {
  65. .rx_phys = BLE_GAP_PHY_AUTO,
  66. .tx_phys = BLE_GAP_PHY_AUTO,
  67. };
  68. err_code = sd_ble_gap_phy_update(p_ble_evt->evt.gap_evt.conn_handle, &phys);
  69. ZERROR_CHECK(err_code);
  70. } break;
  71. case BLE_GAP_EVT_SEC_PARAMS_REQUEST:
  72. // Pairing not supported
  73. err_code = sd_ble_gap_sec_params_reply(m_conn_handle, BLE_GAP_SEC_STATUS_PAIRING_NOT_SUPP, NULL, NULL);
  74. ZERROR_CHECK(err_code);
  75. break;
  76. case BLE_GATTS_EVT_SYS_ATTR_MISSING:
  77. // No system attributes have been stored.
  78. err_code = sd_ble_gatts_sys_attr_set(m_conn_handle, NULL, 0, 0);
  79. ZERROR_CHECK(err_code);
  80. break;
  81. case BLE_GATTC_EVT_TIMEOUT:
  82. // Disconnect on GATT Client timeout event.
  83. err_code = sd_ble_gap_disconnect(p_ble_evt->evt.gattc_evt.conn_handle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
  84. ZERROR_CHECK(err_code);
  85. break;
  86. case BLE_GATTS_EVT_TIMEOUT:
  87. // Disconnect on GATT Server timeout event.
  88. err_code = sd_ble_gap_disconnect(p_ble_evt->evt.gatts_evt.conn_handle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
  89. ZERROR_CHECK(err_code);
  90. break;
  91. default:
  92. // No implementation needed.
  93. break;
  94. }
  95. }
  96. static void gatt_evt_handler(nrf_ble_gatt_t* p_gatt, nrf_ble_gatt_evt_t const* p_evt) {
  97. /*******************************************************************************
  98. * MTU SIZE *
  99. *******************************************************************************/
  100. if ((m_conn_handle == p_evt->conn_handle) && (p_evt->evt_id == NRF_BLE_GATT_EVT_ATT_MTU_UPDATED)) {
  101. m_mtu_size = p_evt->params.att_mtu_effective - OPCODE_LENGTH - HANDLE_LENGTH;
  102. NRF_LOG_DEBUG("ATT MTU exchange completed. central %d peripheral %d", p_gatt->att_mtu_desired_central, p_gatt->att_mtu_desired_periph);
  103. }
  104. }
  105. static void on_conn_params_evt(ble_conn_params_evt_t* p_evt) {
  106. uint32_t err_code;
  107. if (p_evt->evt_type == BLE_CONN_PARAMS_EVT_FAILED) {
  108. err_code = sd_ble_gap_disconnect(m_conn_handle, BLE_HCI_CONN_INTERVAL_UNACCEPTABLE);
  109. ZERROR_CHECK(err_code);
  110. }
  111. }
  112. static void conn_params_error_handler(uint32_t nrf_error) { APP_ERROR_HANDLER(nrf_error); }
  113. static void on_adv_evt(ble_adv_evt_t ble_adv_evt) {
  114. uint32_t err_code;
  115. switch (ble_adv_evt) {
  116. case BLE_ADV_EVT_FAST:
  117. // err_code = bsp_indication_set(BSP_INDICATE_ADVERTISING);
  118. // ZERROR_CHECK(err_code);
  119. break;
  120. case BLE_ADV_EVT_IDLE:
  121. // sleep_mode_enter();
  122. break;
  123. default:
  124. break;
  125. }
  126. }
  127. void zble_module_start_adv() {
  128. uint32_t err_code = ble_advertising_start(&m_advertising, BLE_ADV_MODE_FAST);
  129. ZERROR_CHECK(err_code);
  130. }
  131. void zble_module_stop_adv() {
  132. uint32_t err_code = sd_ble_gap_adv_stop(m_advertising.adv_handle);
  133. ZERROR_CHECK(err_code);
  134. }
  135. /*******************************************************************************
  136. * INIT *
  137. *******************************************************************************/
  138. void zble_module_init(zble_module_cfg_t* cfg) {
  139. /**
  140. * @brief
  141. *
  142. */
  143. { NRF_SDH_BLE_OBSERVER(m_ble_observer, 3, ble_evt_handler, NULL); }
  144. /*******************************************************************************
  145. * GAP初始化 *
  146. *******************************************************************************/
  147. {
  148. uint32_t err_code;
  149. ble_gap_conn_params_t gap_conn_params;
  150. ble_gap_conn_sec_mode_t sec_mode;
  151. BLE_GAP_CONN_SEC_MODE_SET_OPEN(&sec_mode);
  152. err_code = sd_ble_gap_device_name_set(&sec_mode, (const uint8_t*)cfg->deviceName, strlen(cfg->deviceName));
  153. ZERROR_CHECK(err_code);
  154. memset(&gap_conn_params, 0, sizeof(gap_conn_params));
  155. gap_conn_params.min_conn_interval = MIN_CONN_INTERVAL;
  156. gap_conn_params.max_conn_interval = MAX_CONN_INTERVAL;
  157. gap_conn_params.slave_latency = SLAVE_LATENCY;
  158. gap_conn_params.conn_sup_timeout = CONN_SUP_TIMEOUT;
  159. err_code = sd_ble_gap_ppcp_set(&gap_conn_params);
  160. ZERROR_CHECK(err_code);
  161. }
  162. /*******************************************************************************
  163. * GATT *
  164. *******************************************************************************/
  165. {
  166. ret_code_t err_code;
  167. err_code = nrf_ble_gatt_init(&m_gatt, gatt_evt_handler);
  168. ZERROR_CHECK(err_code);
  169. err_code = nrf_ble_gatt_att_mtu_periph_set(&m_gatt, NRF_SDH_BLE_GATT_MAX_MTU_SIZE);
  170. ZERROR_CHECK(err_code);
  171. }
  172. /*******************************************************************************
  173. * *
  174. *******************************************************************************/
  175. {
  176. // if (cfg->on_service_init) cfg->on_service_init();
  177. uint32_t err_code;
  178. ble_nus_init_t nus_init;
  179. nrf_ble_qwr_init_t qwr_init = {0};
  180. // Initialize Queued Write Module.
  181. qwr_init.error_handler = NULL;
  182. err_code = nrf_ble_qwr_init(&m_qwr, &qwr_init);
  183. APP_ERROR_CHECK(err_code);
  184. // Initialize NUS.
  185. memset(&nus_init, 0, sizeof(nus_init));
  186. nus_init.data_handler = NULL;
  187. err_code = ble_nus_init(&m_nus, &nus_init);
  188. APP_ERROR_CHECK(err_code);
  189. }
  190. /*******************************************************************************
  191. * 广 *
  192. *******************************************************************************/
  193. {
  194. uint32_t err_code;
  195. ble_advertising_init_t init;
  196. memset(&init, 0, sizeof(init));
  197. init.advdata.name_type = BLE_ADVDATA_FULL_NAME;
  198. init.advdata.include_appearance = false;
  199. init.advdata.flags = BLE_GAP_ADV_FLAGS_LE_ONLY_LIMITED_DISC_MODE;
  200. init.srdata.uuids_complete.uuid_cnt = sizeof(m_adv_uuids) / sizeof(m_adv_uuids[0]);
  201. init.srdata.uuids_complete.p_uuids = m_adv_uuids;
  202. init.config.ble_adv_fast_enabled = true;
  203. init.config.ble_adv_fast_interval = APP_ADV_INTERVAL;
  204. init.config.ble_adv_fast_timeout = APP_ADV_DURATION;
  205. init.evt_handler = on_adv_evt;
  206. err_code = ble_advertising_init(&m_advertising, &init);
  207. ZERROR_CHECK(err_code);
  208. ble_advertising_conn_cfg_tag_set(&m_advertising, APP_BLE_CONN_CFG_TAG);
  209. }
  210. /*******************************************************************************
  211. * *
  212. *******************************************************************************/
  213. {
  214. uint32_t err_code;
  215. ble_conn_params_init_t cp_init;
  216. memset(&cp_init, 0, sizeof(cp_init));
  217. cp_init.p_conn_params = NULL;
  218. cp_init.first_conn_params_update_delay = FIRST_CONN_PARAMS_UPDATE_DELAY;
  219. cp_init.next_conn_params_update_delay = NEXT_CONN_PARAMS_UPDATE_DELAY;
  220. cp_init.max_conn_params_update_count = MAX_CONN_PARAMS_UPDATE_COUNT;
  221. cp_init.start_on_notify_cccd_handle = BLE_GATT_HANDLE_INVALID;
  222. cp_init.disconnect_on_fail = false;
  223. cp_init.evt_handler = on_conn_params_evt;
  224. cp_init.error_handler = conn_params_error_handler;
  225. err_code = ble_conn_params_init(&cp_init);
  226. ZERROR_CHECK(err_code);
  227. }
  228. // zble_module_start_adv();
  229. }