host.c 22 KB

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