host.c 23 KB

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