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.

236 lines
10 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 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 "znordic.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. static void ble_evt_handler(ble_evt_t const* p_ble_evt, void* p_context) {
  34. uint32_t err_code;
  35. switch (p_ble_evt->header.evt_id) {
  36. case BLE_GAP_EVT_CONNECTED:
  37. m_conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
  38. break;
  39. case BLE_GAP_EVT_DISCONNECTED:
  40. m_conn_handle = BLE_CONN_HANDLE_INVALID;
  41. break;
  42. case BLE_GAP_EVT_PHY_UPDATE_REQUEST: {
  43. NRF_LOG_DEBUG("PHY update request.");
  44. ble_gap_phys_t const phys = {
  45. .rx_phys = BLE_GAP_PHY_AUTO,
  46. .tx_phys = BLE_GAP_PHY_AUTO,
  47. };
  48. err_code = sd_ble_gap_phy_update(p_ble_evt->evt.gap_evt.conn_handle, &phys);
  49. ZERROR_CHECK(err_code);
  50. } break;
  51. case BLE_GAP_EVT_SEC_PARAMS_REQUEST:
  52. // Pairing not supported
  53. err_code = sd_ble_gap_sec_params_reply(m_conn_handle, BLE_GAP_SEC_STATUS_PAIRING_NOT_SUPP, NULL, NULL);
  54. ZERROR_CHECK(err_code);
  55. break;
  56. case BLE_GATTS_EVT_SYS_ATTR_MISSING:
  57. // No system attributes have been stored.
  58. err_code = sd_ble_gatts_sys_attr_set(m_conn_handle, NULL, 0, 0);
  59. ZERROR_CHECK(err_code);
  60. break;
  61. case BLE_GATTC_EVT_TIMEOUT:
  62. // Disconnect on GATT Client timeout event.
  63. err_code = sd_ble_gap_disconnect(p_ble_evt->evt.gattc_evt.conn_handle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
  64. ZERROR_CHECK(err_code);
  65. break;
  66. case BLE_GATTS_EVT_TIMEOUT:
  67. // Disconnect on GATT Server timeout event.
  68. err_code = sd_ble_gap_disconnect(p_ble_evt->evt.gatts_evt.conn_handle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
  69. ZERROR_CHECK(err_code);
  70. break;
  71. default:
  72. // No implementation needed.
  73. break;
  74. }
  75. }
  76. static void gatt_evt_handler(nrf_ble_gatt_t* p_gatt, nrf_ble_gatt_evt_t const* p_evt) {
  77. /*******************************************************************************
  78. * MTU SIZE *
  79. *******************************************************************************/
  80. if ((m_conn_handle == p_evt->conn_handle) && (p_evt->evt_id == NRF_BLE_GATT_EVT_ATT_MTU_UPDATED)) {
  81. m_mtu_size = p_evt->params.att_mtu_effective - OPCODE_LENGTH - HANDLE_LENGTH;
  82. NRF_LOG_DEBUG("ATT MTU exchange completed. central %d peripheral %d", p_gatt->att_mtu_desired_central, p_gatt->att_mtu_desired_periph);
  83. }
  84. }
  85. static void on_conn_params_evt(ble_conn_params_evt_t* p_evt) {
  86. uint32_t err_code;
  87. if (p_evt->evt_type == BLE_CONN_PARAMS_EVT_FAILED) {
  88. err_code = sd_ble_gap_disconnect(m_conn_handle, BLE_HCI_CONN_INTERVAL_UNACCEPTABLE);
  89. ZERROR_CHECK(err_code);
  90. }
  91. }
  92. static void conn_params_error_handler(uint32_t nrf_error) { APP_ERROR_HANDLER(nrf_error); }
  93. static void on_adv_evt(ble_adv_evt_t ble_adv_evt) {
  94. switch (ble_adv_evt) {
  95. case BLE_ADV_EVT_FAST:
  96. // err_code = bsp_indication_set(BSP_INDICATE_ADVERTISING);
  97. // ZERROR_CHECK(err_code);
  98. break;
  99. case BLE_ADV_EVT_IDLE:
  100. // sleep_mode_enter();
  101. break;
  102. default:
  103. break;
  104. }
  105. }
  106. void zble_module_start_adv() {
  107. uint32_t err_code = ble_advertising_start(&m_advertising, BLE_ADV_MODE_FAST);
  108. ZERROR_CHECK(err_code);
  109. }
  110. void zble_module_stop_adv() {
  111. uint32_t err_code = sd_ble_gap_adv_stop(m_advertising.adv_handle);
  112. ZERROR_CHECK(err_code);
  113. }
  114. /*******************************************************************************
  115. * INIT *
  116. *******************************************************************************/
  117. void zble_module_init(zble_module_cfg_t* cfg) {
  118. /**
  119. * @brief
  120. * ʼЭջע¼̶??
  121. */
  122. { NRF_SDH_BLE_OBSERVER(m_ble_observer, 3, ble_evt_handler, NULL); }
  123. /*******************************************************************************
  124. * GAPʼ?? *
  125. *******************************************************************************/
  126. {
  127. uint32_t err_code;
  128. ble_gap_conn_params_t gap_conn_params;
  129. ble_gap_conn_sec_mode_t sec_mode;
  130. BLE_GAP_CONN_SEC_MODE_SET_OPEN(&sec_mode);
  131. err_code = sd_ble_gap_device_name_set(&sec_mode, (const uint8_t*)cfg->deviceName, strlen(cfg->deviceName));
  132. ZERROR_CHECK(err_code);
  133. memset(&gap_conn_params, 0, sizeof(gap_conn_params));
  134. gap_conn_params.min_conn_interval = MIN_CONN_INTERVAL;
  135. gap_conn_params.max_conn_interval = MAX_CONN_INTERVAL;
  136. gap_conn_params.slave_latency = SLAVE_LATENCY;
  137. gap_conn_params.conn_sup_timeout = CONN_SUP_TIMEOUT;
  138. err_code = sd_ble_gap_ppcp_set(&gap_conn_params);
  139. ZERROR_CHECK(err_code);
  140. }
  141. /*******************************************************************************
  142. * GATT ʼ?? *
  143. *******************************************************************************/
  144. {
  145. ret_code_t err_code;
  146. err_code = nrf_ble_gatt_init(&m_gatt, gatt_evt_handler);
  147. ZERROR_CHECK(err_code);
  148. err_code = nrf_ble_gatt_att_mtu_periph_set(&m_gatt, NRF_SDH_BLE_GATT_MAX_MTU_SIZE);
  149. ZERROR_CHECK(err_code);
  150. }
  151. /*******************************************************************************
  152. * ʼ?? *
  153. *******************************************************************************/
  154. {
  155. if (cfg->on_service_init) cfg->on_service_init();
  156. }
  157. /*******************************************************************************
  158. * ʼ?? *
  159. *******************************************************************************/
  160. {
  161. uint32_t err_code;
  162. ble_advertising_init_t init;
  163. memset(&init, 0, sizeof(init));
  164. init.advdata.name_type = BLE_ADVDATA_FULL_NAME;
  165. init.advdata.include_appearance = false;
  166. // init.advdata.flags = BLE_GAP_ADV_FLAGS_LE_ONLY_LIMITED_DISC_MODE;
  167. init.advdata.flags = BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE;
  168. init.srdata.uuids_complete.uuid_cnt = sizeof(m_adv_uuids) / sizeof(m_adv_uuids[0]);
  169. init.srdata.uuids_complete.p_uuids = m_adv_uuids;
  170. init.config.ble_adv_fast_enabled = true;
  171. init.config.ble_adv_fast_interval = APP_ADV_INTERVAL;
  172. init.config.ble_adv_fast_timeout = 0;
  173. init.evt_handler = on_adv_evt;
  174. err_code = ble_advertising_init(&m_advertising, &init);
  175. ZERROR_CHECK(err_code);
  176. ble_advertising_conn_cfg_tag_set(&m_advertising, APP_BLE_CONN_CFG_TAG);
  177. }
  178. /*******************************************************************************
  179. * Ӳʼ?? *
  180. *******************************************************************************/
  181. {
  182. uint32_t err_code;
  183. ble_conn_params_init_t cp_init;
  184. memset(&cp_init, 0, sizeof(cp_init));
  185. cp_init.p_conn_params = NULL;
  186. cp_init.first_conn_params_update_delay = FIRST_CONN_PARAMS_UPDATE_DELAY;
  187. cp_init.next_conn_params_update_delay = NEXT_CONN_PARAMS_UPDATE_DELAY;
  188. cp_init.max_conn_params_update_count = MAX_CONN_PARAMS_UPDATE_COUNT;
  189. cp_init.start_on_notify_cccd_handle = BLE_GATT_HANDLE_INVALID;
  190. cp_init.disconnect_on_fail = false;
  191. cp_init.evt_handler = on_conn_params_evt;
  192. cp_init.error_handler = conn_params_error_handler;
  193. err_code = ble_conn_params_init(&cp_init);
  194. ZERROR_CHECK(err_code);
  195. }
  196. // zble_module_start_adv();
  197. }
  198. int32_t zble_module_get_mtu_size() { return m_mtu_size; }