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.

283 lines
12 KiB

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