host.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. #include "ble_comm.h"
  2. #if USENAMEFR
  3. char Target_scan[TARFET_LEN_MAX] = "SH_E9F4";
  4. #else
  5. char Target_scan[TARFET_LEN_MAX] = {0x01, 0xf9, 0x84, 0x6a, 0x83, 0xeb};
  6. #endif
  7. static Ble_receive_handler_t Rec_h = NULL;
  8. #define APP_BLE_CONN_CFG_TAG 1 /**< Tag that refers to the BLE stack configuration set with @ref sd_ble_cfg_set. The default tag is @ref BLE_CONN_CFG_TAG_DEFAULT. */
  9. #define APP_BLE_OBSERVER_PRIO 3 /**< BLE observer priority of the application. There is no need to modify this value. */
  10. #define UART_TX_BUF_SIZE 1024 /**< UART TX buffer size. */
  11. #define UART_RX_BUF_SIZE 1024 /**< UART RX buffer size. */
  12. #define NUS_SERVICE_UUID_TYPE BLE_UUID_TYPE_VENDOR_BEGIN /**< UUID type for the Nordic UART Service (vendor specific). */
  13. #define ECHOBACK_BLE_UART_DATA 1 /**< Echo the UART data that is received over the Nordic UART Service (NUS) back to the sender. */
  14. BLE_NUS_C_DEF(m_ble_nus_c); /**< BLE Nordic UART Service (NUS) client instance. */
  15. NRF_BLE_GATT_DEF(m_gatt); /**< GATT module instance. */
  16. BLE_DB_DISCOVERY_DEF(m_db_disc); /**< Database discovery module instance. */
  17. NRF_BLE_SCAN_DEF(m_scan); /**< Scanning Module instance. */
  18. NRF_BLE_GQ_DEF(m_ble_gatt_queue, /**< BLE GATT Queue instance. */
  19. NRF_SDH_BLE_CENTRAL_LINK_COUNT,
  20. NRF_BLE_GQ_QUEUE_SIZE);
  21. static uint16_t m_ble_nus_max_data_len = BLE_GATT_ATT_MTU_DEFAULT - OPCODE_LENGTH - HANDLE_LENGTH; /**< Maximum length of data (in bytes) that can be transmitted to the peer by the Nordic UART service module. */
  22. unsigned char connect_to_server = 0;
  23. ble_gap_conn_params_t host_conn_params = {0};
  24. static void db_disc_handler(ble_db_discovery_evt_t *p_evt)
  25. {
  26. ble_nus_c_on_db_disc_evt(&m_ble_nus_c, p_evt);
  27. }
  28. void timer_init(void)
  29. {
  30. ret_code_t err_code = app_timer_init();
  31. APP_ERROR_CHECK(err_code);
  32. }
  33. static void db_discovery_init(void)
  34. {
  35. ble_db_discovery_init_t db_init;
  36. memset(&db_init, 0, sizeof(ble_db_discovery_init_t));
  37. db_init.evt_handler = db_disc_handler;
  38. db_init.p_gatt_queue = &m_ble_gatt_queue;
  39. ret_code_t err_code = ble_db_discovery_init(&db_init);
  40. APP_ERROR_CHECK(err_code);
  41. }
  42. void power_management_init(void)
  43. {
  44. ret_code_t err_code;
  45. err_code = nrf_pwr_mgmt_init();
  46. APP_ERROR_CHECK(err_code);
  47. }
  48. static void gatt_evt_handler(nrf_ble_gatt_t *p_gatt, nrf_ble_gatt_evt_t const *p_evt)
  49. {
  50. if (p_evt->evt_id == NRF_BLE_GATT_EVT_ATT_MTU_UPDATED)
  51. {
  52. BLE_PRINT("GATT MTU exchange completed.\r\r\n");
  53. m_ble_nus_max_data_len = p_evt->params.att_mtu_effective - OPCODE_LENGTH - HANDLE_LENGTH;
  54. BLE_PRINT("Ble NUS max data length set to 0x%X(%d)\r\r\n", m_ble_nus_max_data_len, m_ble_nus_max_data_len);
  55. }
  56. }
  57. void gatt_init(void)
  58. {
  59. ret_code_t err_code;
  60. err_code = nrf_ble_gatt_init(&m_gatt, gatt_evt_handler);
  61. APP_ERROR_CHECK(err_code);
  62. err_code = nrf_ble_gatt_att_mtu_central_set(&m_gatt, NRF_SDH_BLE_GATT_MAX_MTU_SIZE);
  63. APP_ERROR_CHECK(err_code);
  64. err_code = nrf_ble_gatt_att_mtu_periph_set(&m_gatt, NRF_SDH_BLE_GATT_MAX_MTU_SIZE);
  65. APP_ERROR_CHECK(err_code);
  66. }
  67. void scan_start(void)
  68. {
  69. ret_code_t ret;
  70. if(connect_to_server)return;
  71. ret = nrf_ble_scan_start(&m_scan);
  72. APP_ERROR_CHECK(ret);
  73. BLE_PRINT("scan_start -> scan_name <%s> \r\n", Target_scan);
  74. }
  75. static void ble_nus_c_evt_handler(ble_nus_c_t *p_ble_nus_c, ble_nus_c_evt_t const *p_ble_nus_evt)
  76. {
  77. ret_code_t err_code;
  78. switch (p_ble_nus_evt->evt_type)
  79. {
  80. case BLE_NUS_C_EVT_DISCOVERY_COMPLETE:
  81. BLE_PRINT("Discovery complete.\r\n");
  82. err_code = ble_nus_c_handles_assign(p_ble_nus_c, p_ble_nus_evt->conn_handle, &p_ble_nus_evt->handles);
  83. APP_ERROR_CHECK(err_code);
  84. err_code = ble_nus_c_tx_notif_enable(p_ble_nus_c);
  85. APP_ERROR_CHECK(err_code);
  86. BLE_PRINT("Connected to device with Nordic UART Service.\r\n");
  87. break;
  88. case BLE_NUS_C_EVT_NUS_TX_EVT: //作为主机接收从机的数据
  89. Rec_h(p_ble_nus_evt->p_data, p_ble_nus_evt->data_len);
  90. break;
  91. case BLE_NUS_C_EVT_DISCONNECTED:
  92. BLE_PRINT("Disconnected.\r\n");
  93. #if DEBUGBLE
  94. scan_start();
  95. #endif
  96. break;
  97. }
  98. }
  99. static void nus_error_handler(uint32_t nrf_error)
  100. {
  101. if (nrf_error == NRF_ERROR_RESOURCES)
  102. return;
  103. if (nrf_error == NRF_ERROR_INVALID_STATE)
  104. return;
  105. APP_ERROR_HANDLER(nrf_error);
  106. }
  107. static void nus_c_init(void)
  108. {
  109. ret_code_t err_code;
  110. ble_nus_c_init_t init;
  111. init.evt_handler = ble_nus_c_evt_handler;
  112. init.error_handler = nus_error_handler;
  113. init.p_gatt_queue = &m_ble_gatt_queue;
  114. err_code = ble_nus_c_init(&m_ble_nus_c, &init);
  115. APP_ERROR_CHECK(err_code);
  116. }
  117. #define host_connected_evt_num_max 3
  118. static uint8_t host_connected_evt_num = 0;
  119. static Ble_evt_cb ble_host_connected_evt_cb[host_connected_evt_num_max] = {0};
  120. int Ble_Host_Connectd_Evt_Regist(Ble_evt_cb cb)
  121. {
  122. for (int i = 0; i < host_connected_evt_num_max; i++)
  123. {
  124. if (ble_host_connected_evt_cb[i] == cb)
  125. return -1;
  126. if (ble_host_connected_evt_cb[i] == 0)
  127. {
  128. host_connected_evt_num++;
  129. ble_host_connected_evt_cb[i] = cb; //回调函数
  130. return 0;
  131. }
  132. }
  133. SEGGER_RTT_printf(0, "ble_evt_Regist -> too many!\n");
  134. return -2;
  135. }
  136. void ble_host_connected_evt_pcs(void)
  137. {
  138. for (int i = 0; i < host_connected_evt_num; i++)
  139. { //SEGGER_RTT_printf(0,"time_cb[%d]=%d\n",i,time_cb[i]);
  140. if (ble_host_connected_evt_cb[i])
  141. {
  142. ble_host_connected_evt_cb[i](); //回调函数
  143. }
  144. }
  145. }
  146. #define host_disconn_evt_num_max 16
  147. static uint8_t host_disconn_evt_num = 0;
  148. static Ble_evt_cb ble_Host_disconn_evt_cb[host_disconn_evt_num_max] = {0};
  149. int Ble_Host_Disconn_Evt_Regist(Ble_evt_cb cb)
  150. {
  151. for (int i = 0; i < host_disconn_evt_num_max; i++)
  152. {
  153. if (ble_Host_disconn_evt_cb[i] == cb)
  154. return -1;
  155. if (ble_Host_disconn_evt_cb[i] == 0)
  156. {
  157. host_disconn_evt_num++;
  158. ble_Host_disconn_evt_cb[i] = cb; //回调函数
  159. return 0;
  160. }
  161. }
  162. SEGGER_RTT_printf(0, "Ble_Slave_Disconn_Evt_Regist -> too many!\r\n");
  163. return -2;
  164. }
  165. void ble_host_dicconn_evt_pcs(void)
  166. {
  167. for (int i = 0; i < host_disconn_evt_num; i++)
  168. { //SEGGER_RTT_printf(0,"time_cb[%d]=%d\n",i,time_cb[i]);
  169. if (ble_Host_disconn_evt_cb[i])
  170. {
  171. ble_Host_disconn_evt_cb[i](); //回调函数
  172. }
  173. }
  174. }
  175. static void on_ble_central_evt(ble_evt_t const *p_ble_evt, void *p_context) //作为主设备时的处理
  176. {
  177. ret_code_t err_code;
  178. ble_gap_evt_t const *p_gap_evt = &p_ble_evt->evt.gap_evt;
  179. switch (p_ble_evt->header.evt_id)
  180. {
  181. case BLE_GAP_EVT_CONNECTED:
  182. err_code = ble_nus_c_handles_assign(&m_ble_nus_c, p_ble_evt->evt.gap_evt.conn_handle, NULL);
  183. APP_ERROR_CHECK(err_code);
  184. BLE_PRINT("start discovery services\r\n"); //添加开始发现服务提示
  185. connect_to_server = 1;
  186. gpio_mt_run(500);
  187. // start discovery of services. The NUS Client waits for a discovery result
  188. err_code = ble_db_discovery_start(&m_db_disc, p_ble_evt->evt.gap_evt.conn_handle);
  189. APP_ERROR_CHECK(err_code);
  190. ble_host_connected_evt_pcs();
  191. sd_ble_gap_rssi_start(m_ble_nus_c.conn_handle, BLE_GAP_RSSI_THRESHOLD_INVALID, 0);
  192. break;
  193. case BLE_GAP_EVT_DISCONNECTED:
  194. connect_to_server = 0;
  195. BLE_PRINT("Disconnected. conn_handle: 0x%x, reason: 0x%x",
  196. p_gap_evt->conn_handle,
  197. p_gap_evt->params.disconnected.reason);
  198. BLE_PRINT("Disconnected to Server.\r\n");
  199. ble_host_dicconn_evt_pcs();
  200. sd_ble_gap_rssi_stop(m_ble_nus_c.conn_handle);
  201. break;
  202. case BLE_GAP_EVT_TIMEOUT:
  203. if (p_gap_evt->params.timeout.src == BLE_GAP_TIMEOUT_SRC_CONN)
  204. {
  205. BLE_PRINT("Connection Request timed out.\r\n");
  206. }
  207. break;
  208. case BLE_GAP_EVT_SEC_PARAMS_REQUEST:
  209. // Pairing not supported.
  210. err_code = sd_ble_gap_sec_params_reply(p_ble_evt->evt.gap_evt.conn_handle, BLE_GAP_SEC_STATUS_PAIRING_NOT_SUPP, NULL, NULL);
  211. APP_ERROR_CHECK(err_code);
  212. BLE_PRINT("on_ble_central_evt -> BLE_GAP_EVT_SEC_PARAMS_REQUEST\r\n");
  213. break;
  214. case BLE_GAP_EVT_PHY_UPDATE_REQUEST:
  215. {
  216. BLE_PRINT("Connection 0x%x PHY update request.", p_ble_evt->evt.gap_evt.conn_handle);
  217. ble_gap_phys_t const phys =
  218. {
  219. .rx_phys = BLE_GAP_PHY_AUTO,
  220. .tx_phys = BLE_GAP_PHY_AUTO,
  221. };
  222. err_code = sd_ble_gap_phy_update(p_ble_evt->evt.gap_evt.conn_handle, &phys);
  223. APP_ERROR_CHECK(err_code);
  224. }
  225. break;
  226. case BLE_GAP_EVT_CONN_PARAM_UPDATE_REQUEST:
  227. // Accepting parameters requested by peer.
  228. err_code = sd_ble_gap_conn_param_update(p_gap_evt->conn_handle,
  229. &p_gap_evt->params.conn_param_update_request.conn_params); //主机接受从机连接参数更新连接参数
  230. APP_ERROR_CHECK(err_code);
  231. BLE_PRINT("on_ble_central_evt -> BLE_GAP_EVT_CONN_PARAM_UPDATE_REQUEST\r\n");
  232. break;
  233. case BLE_GAP_EVT_CONN_PARAM_UPDATE:
  234. BLE_PRINT("on_ble_central_evt -> BLE_GAP_EVT_CONN_PARAM_UPDATE\r\n");
  235. memcpy(&host_conn_params, &p_gap_evt->params.conn_param_update_request.conn_params, sizeof(ble_gap_conn_params_t));
  236. BLE_PRINT("min_conn_interval : %d * 1.25 ms\r\n", p_gap_evt->params.conn_param_update_request.conn_params.min_conn_interval);
  237. BLE_PRINT("max_conn_interval : %d * 1.25 ms\r\n", p_gap_evt->params.conn_param_update_request.conn_params.max_conn_interval);
  238. BLE_PRINT("slave_latency : %d\r\n", p_gap_evt->params.conn_param_update_request.conn_params.slave_latency);
  239. BLE_PRINT("conn_sup_timeout : %d * 10 ms\r\n", p_gap_evt->params.conn_param_update_request.conn_params.conn_sup_timeout);
  240. break;
  241. case BLE_GATTC_EVT_TIMEOUT:
  242. // Disconnect on GATT Client timeout event.
  243. BLE_PRINT("GATT Client Timeout.\r\n");
  244. err_code = sd_ble_gap_disconnect(p_ble_evt->evt.gattc_evt.conn_handle,
  245. BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
  246. APP_ERROR_CHECK(err_code);
  247. break;
  248. case BLE_GATTS_EVT_TIMEOUT:
  249. // Disconnect on GATT Server timeout event.
  250. BLE_PRINT("GATT Server Timeout.\r\n");
  251. err_code = sd_ble_gap_disconnect(p_ble_evt->evt.gatts_evt.conn_handle,
  252. BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
  253. APP_ERROR_CHECK(err_code);
  254. break;
  255. default:
  256. break;
  257. }
  258. }
  259. extern bool ble_evt_is_advertising_timeout(ble_evt_t const *p_ble_evt);
  260. extern void on_ble_peripheral_evt(ble_evt_t const *p_ble_evt); //作为从设备的处理
  261. static void ble_evt_handler(ble_evt_t const *p_ble_evt, void *p_context)
  262. {
  263. uint16_t conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
  264. uint16_t role = ble_conn_state_role(conn_handle);
  265. // Based on the role this device plays in the connection, dispatch to the right handler.不同角色下所做的处理
  266. if (role == BLE_GAP_ROLE_PERIPH || ble_evt_is_advertising_timeout(p_ble_evt))
  267. {
  268. // ble_nus_on_ble_evt(p_ble_evt, &m_nus);
  269. on_ble_peripheral_evt(p_ble_evt);
  270. }
  271. else if ((role == BLE_GAP_ROLE_CENTRAL) || (p_ble_evt->header.evt_id == BLE_GAP_EVT_ADV_REPORT))
  272. {
  273. // ble_nus_c_on_ble_evt(p_ble_evt, &m_ble_nus_c);
  274. on_ble_central_evt(p_ble_evt, NULL);
  275. }
  276. else
  277. {
  278. BLE_PRINT("ble_evt_handler -> other\r\n");
  279. }
  280. }
  281. void ble_stack_init(void)
  282. {
  283. ret_code_t err_code;
  284. err_code = nrf_sdh_enable_request();
  285. APP_ERROR_CHECK(err_code);
  286. // Configure the BLE stack using the default settings.
  287. // Fetch the start address of the application RAM.
  288. uint32_t ram_start = 0;
  289. err_code = nrf_sdh_ble_default_cfg_set(APP_BLE_CONN_CFG_TAG, &ram_start);
  290. APP_ERROR_CHECK(err_code);
  291. // Enable BLE stack.
  292. err_code = nrf_sdh_ble_enable(&ram_start);
  293. APP_ERROR_CHECK(err_code);
  294. // Register a handler for BLE events.
  295. NRF_SDH_BLE_OBSERVER(m_ble_observer, APP_BLE_OBSERVER_PRIO, ble_evt_handler, NULL);
  296. }
  297. #define PRINT_MAC 0
  298. /**@brief Function for handling Scanning Module events.
  299. */
  300. static void scan_evt_handler(scan_evt_t const *p_scan_evt)
  301. {
  302. ret_code_t err_code;
  303. switch (p_scan_evt->scan_evt_id)
  304. {
  305. case NRF_BLE_SCAN_EVT_CONNECTING_ERROR:
  306. {
  307. err_code = p_scan_evt->params.connecting_err.err_code;
  308. APP_ERROR_CHECK(err_code);
  309. BLE_PRINT("scan_evt_handler -> NRF_BLE_SCAN_EVT_CONNECTING_ERROR \r\n");
  310. }
  311. break;
  312. case NRF_BLE_SCAN_EVT_CONNECTED:
  313. {
  314. BLE_PRINT("scan_evt_handler -> NRF_BLE_SCAN_EVT_CONNECTED\r\n");
  315. #if PRINT_MAC
  316. ble_gap_evt_connected_t const *p_connected =
  317. p_scan_evt->params.connected.p_connected;
  318. // Scan is automatically stopped by the connection.
  319. BLE_PRINT("Connecting to Host %02x%02x %02x%02x %02x%02x\r\r\n",
  320. p_connected->peer_addr.addr[0],
  321. p_connected->peer_addr.addr[1],
  322. p_connected->peer_addr.addr[2],
  323. p_connected->peer_addr.addr[3],
  324. p_connected->peer_addr.addr[4],
  325. p_connected->peer_addr.addr[5]);
  326. #endif
  327. }
  328. break;
  329. case NRF_BLE_SCAN_EVT_SCAN_TIMEOUT:
  330. {
  331. BLE_PRINT("scan_evt_handler -> NRF_BLE_SCAN_EVT_SCAN_TIMEOUT -> Scan timed out.\r\n");
  332. scan_start();
  333. }
  334. break;
  335. case NRF_BLE_SCAN_EVT_FILTER_MATCH:
  336. {
  337. BLE_PRINT("scan_evt_handler -> NRF_BLE_SCAN_EVT_FILTER_MATCH\r\n");
  338. }
  339. break;
  340. case NRF_BLE_SCAN_EVT_WHITELIST_REQUEST:
  341. {
  342. BLE_PRINT("scan_evt_handler -> NRF_BLE_SCAN_EVT_WHITELIST_REQUEST\r\n");
  343. }
  344. break;
  345. case NRF_BLE_SCAN_EVT_WHITELIST_ADV_REPORT:
  346. {
  347. BLE_PRINT("scan_evt_handler -> NRF_BLE_SCAN_EVT_WHITELIST_ADV_REPORT\r\n");
  348. }
  349. break;
  350. case NRF_BLE_SCAN_EVT_NOT_FOUND:
  351. {
  352. // BLE_PRINT("scan_evt_handler -> NRF_BLE_SCAN_EVT_NOT_FOUND");
  353. }
  354. break;
  355. default:
  356. BLE_PRINT("scan_evt_handler -> default:%d \r\n", p_scan_evt->scan_evt_id);
  357. break;
  358. }
  359. }
  360. unsigned int send_bytes_server(uint8_t *bytes, uint16_t len)
  361. {
  362. if (connect_to_server == 0)
  363. {
  364. BLE_PRINT("send_bytes_server -> APP_ERR_DISCONN\r\n");
  365. return APP_ERR_DISCONN;
  366. }
  367. if (len > m_ble_nus_max_data_len)
  368. {
  369. BLE_PRINT("send_bytes_server -> fail ->overlength\r\n");
  370. return APP_ERR_OVERLENGTH;
  371. }
  372. if (NRF_SUCCESS != ble_nus_c_string_send(&m_ble_nus_c, bytes, len))
  373. {
  374. BLE_PRINT("send_bytes_server -> fail\r\n");
  375. }
  376. return APP_SUCCESS;
  377. }
  378. static void scan_init(void)
  379. {
  380. ret_code_t err_code;
  381. nrf_ble_scan_init_t init_scan;
  382. memset(&init_scan, 0, sizeof(init_scan));
  383. init_scan.connect_if_match = true;
  384. init_scan.conn_cfg_tag = APP_BLE_CONN_CFG_TAG;
  385. err_code = nrf_ble_scan_init(&m_scan, &init_scan, scan_evt_handler);
  386. APP_ERROR_CHECK(err_code);
  387. #if USENAMEFR
  388. err_code = nrf_ble_scan_filter_set(&m_scan, SCAN_NAME_FILTER, &Target_scan);
  389. APP_ERROR_CHECK(err_code);
  390. err_code = nrf_ble_scan_filters_enable(&m_scan, NRF_BLE_SCAN_NAME_FILTER, false);
  391. APP_ERROR_CHECK(err_code);
  392. BLE_PRINT("scan_init -> scan_name <%s> \r\n", Target_scan);
  393. #else
  394. err_code = nrf_ble_scan_filter_set(&m_scan, SCAN_ADDR_FILTER, &Target_scan);
  395. APP_ERROR_CHECK(err_code);
  396. err_code = nrf_ble_scan_filters_enable(&m_scan, NRF_BLE_SCAN_ADDR_FILTER, false);
  397. APP_ERROR_CHECK(err_code);
  398. BLE_PRINT("scan_start -> scan_MAC [ %02X %02X %02X %02X %02X %02X ]\r\n", Target_scan[0], Target_scan[1], Target_scan[2], Target_scan[3], Target_scan[4], Target_scan[5]);
  399. #endif
  400. }
  401. unsigned char host_isconnect(void)
  402. {
  403. return connect_to_server;
  404. }
  405. char ble_stack_init_sta = 1;
  406. char host_init_sta = 0;
  407. void host_init(Ble_receive_handler_t receive_handler)
  408. {
  409. if (receive_handler == NULL)
  410. {
  411. BLE_PRINT("host_init -> param err \r\n");
  412. return;
  413. }
  414. Rec_h = receive_handler;
  415. if (ble_stack_init_sta)
  416. {
  417. timer_init(); //
  418. power_management_init(); //
  419. ble_stack_init(); //
  420. gatt_init(); //
  421. ble_stack_init_sta = 0;
  422. }
  423. db_discovery_init();
  424. nus_c_init();
  425. scan_init();
  426. host_init_sta=1;
  427. }
  428. void err(int err_num)
  429. {
  430. BLE_PRINT("APP ERROR -> %d \r\n", err_num);
  431. }
  432. unsigned int host_set_scan_name(char *name, int len)
  433. {
  434. unsigned int err_code;
  435. if (len > TARFET_LEN_MAX)
  436. return APP_ERR_OVERLENGTH;
  437. if(connect_to_server)
  438. return APP_ERR_CONNECTED;
  439. memset(Target_scan, 0, TARFET_LEN_MAX);
  440. if(host_init_sta)
  441. {
  442. memcpy(Target_scan, name, len);
  443. err_code =nrf_ble_scan_all_filter_remove(&m_scan);
  444. APP_ERROR_CHECK(err_code);
  445. #if USENAMEFR
  446. err_code = nrf_ble_scan_filter_set(&m_scan, SCAN_NAME_FILTER, &Target_scan);
  447. APP_ERROR_CHECK(err_code);
  448. err_code = nrf_ble_scan_filters_enable(&m_scan, NRF_BLE_SCAN_NAME_FILTER, false);
  449. APP_ERROR_CHECK(err_code);
  450. BLE_PRINT("host_set_scan_name -> scan_name <%s> \r\n", Target_scan);
  451. #else
  452. err_code = nrf_ble_scan_filter_set(&m_scan, SCAN_ADDR_FILTER, &Target_scan);
  453. APP_ERROR_CHECK(err_code);
  454. err_code = nrf_ble_scan_filters_enable(&m_scan, NRF_BLE_SCAN_ADDR_FILTER, false);
  455. APP_ERROR_CHECK(err_code);
  456. BLE_PRINT("host_set_scan_name -> scan_MAC [ %02X %02X %02X %02X %02X %02X ]\r\n", Target_scan[0], Target_scan[1], Target_scan[2], Target_scan[3], Target_scan[4], Target_scan[5]);
  457. #endif
  458. }
  459. else
  460. {
  461. memcpy(Target_scan, name, len);
  462. }
  463. return APP_SUCCESS;
  464. }
  465. unsigned int Ble_update_conn_interval(float min_conn_interval, float max_conn_interval)
  466. {
  467. ret_code_t err_code;
  468. ble_gap_conn_params_t bgcp;
  469. //主机接受从机连接参数更新连接参数
  470. if (connect_to_server)
  471. {
  472. if ((max_conn_interval > 1.25 * 1599) || (max_conn_interval < min_conn_interval))
  473. return APP_ERR_PARAMERR;
  474. if (min_conn_interval < 7.5f)
  475. return APP_ERR_PARAMERR;
  476. bgcp.max_conn_interval = MSEC_TO_UNITS(max_conn_interval, UNIT_1_25_MS);
  477. bgcp.min_conn_interval = MSEC_TO_UNITS(min_conn_interval, UNIT_1_25_MS);
  478. bgcp.conn_sup_timeout = MSEC_TO_UNITS(4000, UNIT_10_MS);
  479. bgcp.slave_latency = 0;
  480. err_code = sd_ble_gap_conn_param_update(m_ble_nus_c.conn_handle, &bgcp);
  481. if(err_code != NRF_ERROR_INVALID_STATE && err_code != NRF_ERROR_BUSY && err_code != NRF_SUCCESS){
  482. APP_ERROR_CHECK(err_code);
  483. }
  484. return err_code;
  485. }
  486. else
  487. {
  488. return APP_ERR_DISCONN;
  489. }
  490. }
  491. void host_disconnect(void)
  492. {
  493. if (connect_to_server)
  494. sd_ble_gap_disconnect(m_ble_nus_c.conn_handle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
  495. }
  496. void host_get_conn_params(ble_gap_conn_params_t *p)
  497. {
  498. memcpy(p, &host_conn_params, sizeof(ble_gap_conn_params_t));
  499. }
  500. static signed char rssi = 0;
  501. signed char host_get_rssi(void)
  502. {
  503. unsigned char channel;
  504. if (connect_to_server == 0)
  505. return 1;
  506. sd_ble_gap_rssi_get(m_ble_nus_c.conn_handle, &rssi, &channel);
  507. ///BLE_PRINT("rssi= %d channel=%d\r\n", rssi, channel);
  508. return rssi;
  509. }