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

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