host.c 26 KB

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