host.c 26 KB

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