app_pair - 副本 (2).c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /*********************************************************************
  2. * INCLUDES
  3. */
  4. #include "app_pair.h"
  5. #include "hal_ws2812.h"
  6. #include "ble_comm.h"
  7. #include "app_flash.h"
  8. #include "hal_charge.h"
  9. #include "hal_ble_common.h"
  10. #include "bsp_time.h"
  11. #include "hal_ble_client.h"
  12. #include "hal_ble_host.h"
  13. #include "hal_led.h"
  14. #include "ble_advertising.h"
  15. #include "app_detectIsHost.h"
  16. #include "app_chargerpin_conn_detect.h"
  17. #define PAIR_DEBUGLOG_ENABLE 0
  18. #if PAIR_DEBUGLOG_ENABLE
  19. #define PAIR_DEBUG_LOG(format,...) SEGGER_RTT_printf(0,format, ##__VA_ARGS__);
  20. #else
  21. #define PAIR_DEBUG_LOG(format,...) ;
  22. #endif
  23. static uint8_t ScanFinishFlag =0;
  24. static char pair_name[8]={0};
  25. static ble_gap_addr_t p_addr={0};
  26. void Pair_adv_cb(ble_gap_evt_adv_report_t const * p_adv_report)
  27. {
  28. if(p_adv_report->rssi>-20 && p_adv_report->data.len == 15 ){
  29. // PAIR_DEBUG_LOG("rssi %d: tx_power:%d\r\n",p_adv_report->rssi,p_adv_report->tx_power);
  30. PAIR_DEBUG_LOG("addr:");for(int i=0;i<6;i++){PAIR_DEBUG_LOG("%02X,",p_adv_report->peer_addr.addr[5-i]);}PAIR_DEBUG_LOG("\r\n");
  31. if(ScanFinishFlag==0 && p_adv_report->data.p_data[8] == 'S' && p_adv_report->data.p_data[9] == 'H' && p_adv_report->data.p_data[10] == '_'){
  32. ScanFinishFlag =1;
  33. memset(pair_name,0,sizeof(pair_name));
  34. memcpy(pair_name,&p_adv_report->data.p_data[8],7);
  35. PAIR_DEBUG_LOG("p_data:");for(int i=0;i<p_adv_report->data.len;i++){PAIR_DEBUG_LOG("%02X,",p_adv_report->data.p_data[i]);}PAIR_DEBUG_LOG("\r\n");
  36. PAIR_DEBUG_LOG("name:(%s)\r\n",pair_name);
  37. memcpy(&p_addr, &p_adv_report->peer_addr, sizeof(p_addr));
  38. PAIR_DEBUG_LOG("p_addr addr:");for(int i=0;i<6;i++){PAIR_DEBUG_LOG("%02X,",p_addr.addr[5-i]);}PAIR_DEBUG_LOG("\r\n");
  39. LED_Start(LED_SCAN,COLOR_CYAN);
  40. // LED_Start(LED_SCAN,COLOR_CYAN);
  41. }
  42. }
  43. }
  44. static void app_pair_flash_save(void){
  45. mFlash.mClient.isConfig ='C';
  46. mBackup.hardVersion = mFlash.mClient.hardVersion;
  47. mBackup.sotfVersion = mFlash.mClient.sotfVersion;
  48. mBackup.isConfig = mFlash.mClient.isConfig;
  49. //保存数据到备份区域里面
  50. for (uint8_t i = 0; i < RecordMacAddrL; i++)
  51. {
  52. mBackup.macAddr_L[i] = mFlash.macHost[i];//从机地址
  53. mBackup.macAddr_R[i] = mFlash.mClient.macAddr[i]; //主机地址
  54. }
  55. if (Flash_SaveBackup() != ZONE_OP_SUCCESS){Except_TxError(EXCEPT_Pair, "pair save backup fail");}
  56. PAIR_DEBUG_LOG("mFlash.mClient.isConfig:%X\n", mFlash.mClient.isConfig);
  57. PAIR_DEBUG_LOG("mFlash.mClient.macAddr:%02X %02X %02X\n", mFlash.mClient.macAddr[0], mFlash.mClient.macAddr[1], mFlash.mClient.macAddr[2]);
  58. PAIR_DEBUG_LOG("mFlash.macHost:%02X %02X %02X \n", mFlash.macHost[0], mFlash.macHost[1], mFlash.macHost[2]);
  59. if (Flash_DeleteAllStep() != ZONE_OP_SUCCESS){Except_TxError(EXCEPT_Pair, "pair clear step fail");}
  60. memset(&mFlash.mStep, 0, sizeof(FlashStep_t));
  61. if (Flash_SaveInfomation() != ZONE_OP_SUCCESS){Except_TxError(EXCEPT_Pair, "Flash_SaveInfomation fail");}
  62. }
  63. static int app_ble_send_mac(){
  64. uint8_t buf[32];
  65. uint8_t L=0;
  66. uint8_t mymac[6];
  67. int result =0;
  68. Get_MACaddr(mymac);
  69. memcpy(buf,mymac,6);
  70. L +=6;
  71. //硬件版本
  72. buf[L++] = (uint8_t)((uint32_t)HARDWARE_VERSION>>24);
  73. buf[L++] = (uint8_t)((uint32_t)HARDWARE_VERSION>>16);
  74. buf[L++] = (uint8_t)((uint32_t)HARDWARE_VERSION>>8);
  75. buf[L++] = (uint8_t)((uint32_t)HARDWARE_VERSION>>0);
  76. //软件版本
  77. buf[L++] = (uint8_t)(SOFTWARE_VERSION>>8);
  78. buf[L++] = (uint8_t)(SOFTWARE_VERSION>>0);
  79. if(BLE_CHARGE_INSERT == hal_charge_Getstate() || BLE_CHARGE_DONE == hal_charge_Getstate())
  80. buf[L++] = 1;
  81. else
  82. buf[L++] = 0;
  83. if(mFlash.isHost)
  84. result = BLE_Client_Send(BLE_PAIR,buf,L);
  85. else
  86. BLE_Host_Send(BLE_PAIR,buf,L);
  87. return result;
  88. }
  89. static uint8_t UpdateScanFlag =0;
  90. static uint8_t blink_cnt =0;
  91. static void app_pair_Left_Process(void)
  92. {
  93. if(0 == get_LR_readly()){return;}
  94. if(!mFlash.isHost){return;}
  95. uint8_t charge_state_c = hal_charge_Getstate();
  96. if(UpdateScanFlag >0){
  97. if(host_isconnect()){host_disconnect();}
  98. else{
  99. app_pair_flash_save();
  100. char buf[16];
  101. memset(buf,0,sizeof(buf));
  102. sprintf(buf,"%02X%02X%02X%02X%02X%02X",mFlash.macHost[0],mFlash.macHost[1],mFlash.macHost[2],mFlash.mClient.macAddr[3],mFlash.mClient.macAddr[4],mFlash.mClient.macAddr[5]);
  103. PAIR_DEBUG_LOG("scanName(%d):%s\n",strlen(buf),buf);
  104. if(APP_SUCCESS == host_set_scan_name(buf,strlen(buf))){
  105. UpdateScanFlag =0;
  106. }
  107. }
  108. }
  109. if(blink_cnt > 0 && BLE_CHARGE_PULLOUT == charge_state_c ){
  110. blink_cnt = 0;
  111. LED_Stop(LED_PAIR);
  112. }
  113. }
  114. uint8_t app_pair_UpdateScanNameFlag(void){
  115. return UpdateScanFlag;
  116. }
  117. static void app_pair_Right_Process(void)
  118. {
  119. static uint8_t state =0;
  120. static uint8_t flag =0;
  121. if(0 == get_LR_readly()){return;}
  122. if(mFlash.isHost){
  123. // if(isScan)nrf_ble_scan_stop();
  124. return;
  125. }
  126. uint8_t charge_state_c = hal_charge_Getstate();
  127. switch(state){//ble_advertising_init_t init;
  128. case 0:
  129. if((charge_state_c == BLE_CHARGE_INSERT || charge_state_c == BLE_CHARGE_DONE)){
  130. PAIR_DEBUG_LOG("<<<<<<<<<<<<<<<<<<<<<<<<\r\n");
  131. PAIR_DEBUG_LOG("start scan\r\n");
  132. advdata_report_Evt_Regist(Pair_adv_cb);
  133. ST_scan_stop();
  134. host_set_scan_name((char *)"***********",sizeof("***********"));
  135. ST_scan_start();
  136. LED_Start(LED_SCAN,COLOR_ORANGE);
  137. state =1;
  138. // connetflag = 0;
  139. ScanFinishFlag = 0;
  140. }
  141. break;
  142. case 1:
  143. if(!(charge_state_c == BLE_CHARGE_INSERT || charge_state_c == BLE_CHARGE_DONE)){
  144. PAIR_DEBUG_LOG("charge out\r\n");
  145. // advdata_report_Evt_del(Pair_adv_cb);
  146. ST_scan_stop();
  147. state = 0;
  148. LED_Stop(LED_PAIR);
  149. LED_Stop(LED_SCAN);
  150. if(host_isconnect())host_disconnect();
  151. }
  152. if(ScanFinishFlag>0){ScanFinishFlag = 0;
  153. state =1;
  154. advdata_report_Evt_del(Pair_adv_cb);
  155. if(host_isconnect()){host_disconnect();}
  156. LED_Start(LED_SCAN,COLOR_WHITE);
  157. // sd_ble_gap_scan_stop();
  158. Client_connted((const ble_gap_addr_t *)&p_addr);
  159. //
  160. // uint32_t ScanResult = sd_ble_gap_scan_stop();
  161. //
  162. // uint32_t SetScanNameResult = host_set_scan_name(pair_name,strlen(pair_name));
  163. // logble("Scan name:%s, %d , %d \r\n",pair_name,ScanResult,SetScanNameResult);
  164. //
  165. // ST_scan_start();
  166. PAIR_DEBUG_LOG("ST_scan_start*********************\n");
  167. // Process_UpdatePeroid(app_pair_Right_Process,100);
  168. }
  169. if(host_isconnect()){
  170. PAIR_DEBUG_LOG("1");
  171. // if(connetflag==0){ connetflag = 1;
  172. // ST_scan_stop();
  173. // }
  174. app_ble_send_mac();
  175. if(0 == flag){flag =1;
  176. gpio_mt_run(1000);
  177. LED_Start(LED_SCAN,COLOR_GREEN);
  178. }
  179. }
  180. else{
  181. flag =0;
  182. }
  183. break;
  184. }
  185. }
  186. static void cb_Host_BLE_PAIR(void* handle)
  187. {
  188. BLE_Host_Rx_t* target = handle;
  189. if(host_isconnect()){host_disconnect();}
  190. if(target->pDat[12]==0) return;
  191. ST_scan_stop();
  192. blink_cnt = 1;
  193. LED_Start(LED_PAIR,COLOR_BLUE);
  194. LED_Stop(LED_SCAN);
  195. // ST_scan_stop();
  196. uint8_t ret =0,i =0;
  197. uint8_t mymacbuf[6];
  198. // PAIR_DEBUG_LOG(">>>>>>>>cb_Host_BLE_PAIR =%d \r\n",target->datLen);
  199. uint8_t GetMacAddr[6]={0};
  200. memcpy(GetMacAddr,target->pDat,sizeof(GetMacAddr));
  201. uint32_t hv = ((uint32_t)target->pDat[6]<<24)| ((uint32_t)target->pDat[7]<<16) | ((uint32_t)target->pDat[8]<<8) | ((uint32_t)target->pDat[9]<<0);
  202. uint16_t sv = ((uint16_t)target->pDat[10]<<8) | ((uint16_t)target->pDat[11]<<0);
  203. for(i=0;i<6;i++){
  204. if(mFlash.macHost[i] != GetMacAddr[i]){
  205. ret =1;
  206. break;
  207. }
  208. }
  209. // PAIR_DEBUG_LOG("target->pDat[12] %d\n",target->pDat[12]);
  210. if(mFlash.mClient.hardVersion != hv || mFlash.mClient.sotfVersion != sv || ret !=0){
  211. PAIR_DEBUG_LOG("host config:%d %d %d %d\n",hv,sv,ret);
  212. PAIR_DEBUG_LOG("mFlash.mClient:%d %d %d %d\n\n",mFlash.mClient.hardVersion,mFlash.mClient.sotfVersion,mFlash.mClient.isConfig,ret);
  213. mFlash.mClient.hardVersion = hv;
  214. mFlash.mClient.sotfVersion = sv;
  215. Get_MACaddr(mymacbuf);
  216. for(i=0;i<6;i++){
  217. mFlash.mClient.macAddr[i] = mymacbuf[i]; //从机自身mac地址
  218. mFlash.macHost[i] = GetMacAddr[i]; //主机发过来的mac地址
  219. }
  220. app_pair_flash_save();
  221. char buf[16];
  222. memset(buf,0,sizeof(buf));
  223. //
  224. sprintf(buf,"%02X%02X%02X%02X%02X%02X",mFlash.macHost[0],mFlash.macHost[1],mFlash.macHost[2],mFlash.mClient.macAddr[3],mFlash.mClient.macAddr[4],mFlash.mClient.macAddr[5]);
  225. PAIR_DEBUG_LOG("advName(%d):%s\n",strlen(buf),buf);
  226. slave_set_adv_name(buf,strlen(buf));
  227. advertising_stop();
  228. slave_adv_init();
  229. PAIR_DEBUG_LOG(">>>>>>>>advertising_start =0 \r\n");
  230. advertising_start();
  231. }
  232. }
  233. static void cb_Client_BLE_PAIR(void* handle)
  234. {
  235. uint8_t ret =0;
  236. uint8_t i =0;
  237. uint8_t mymacbuf[6];
  238. uint8_t RightMacAddr[6]={0};
  239. BLE_Client_Rx_t* target = handle;
  240. if(hal_charge_Getstate() == BLE_CHARGE_PULLOUT )return;
  241. int result = app_ble_send_mac();
  242. logble("app_ble_send_mac:%d \r\n",result);
  243. PAIR_DEBUG_LOG(">>>>>>>>cb_Client_BLE_PAIR =0;\r\n");
  244. memcpy(RightMacAddr,target->pDat,sizeof(RightMacAddr));
  245. uint32_t hv = ((uint32_t)target->pDat[6]<<24)| ((uint32_t)target->pDat[7]<<16) | ((uint32_t)target->pDat[8]<<8) | ((uint32_t)target->pDat[9]<<0);
  246. uint16_t sv = ((uint16_t)target->pDat[10]<<8) | ((uint16_t)target->pDat[11]<<0);
  247. blink_cnt = 1;
  248. LED_Start(LED_PAIR,COLOR_BLUE);
  249. for(i=0;i<6;i++){
  250. if(mFlash.mClient.macAddr[i] != RightMacAddr[i]){
  251. ret =1;
  252. break;
  253. }
  254. }
  255. if(mFlash.mClient.hardVersion != hv || mFlash.mClient.sotfVersion != sv || ret !=0){
  256. PAIR_DEBUG_LOG("mFlash config:%d %d %d %d\n\n",mFlash.mClient.hardVersion,mFlash.mClient.sotfVersion,mFlash.mClient.isConfig,ret);
  257. mFlash.mClient.hardVersion = hv;
  258. mFlash.mClient.sotfVersion = sv;
  259. Get_MACaddr(mymacbuf);
  260. for(i=0;i<6;i++){
  261. mFlash.mClient.macAddr[i] = RightMacAddr[i]; //收到的从机mac地址
  262. mFlash.macHost[i] = mymacbuf[i]; //主机本身mac地址
  263. }
  264. UpdateScanFlag =1;
  265. ST_scan_stop();
  266. if(host_isconnect()){host_disconnect();}
  267. }
  268. }
  269. void app_led_blink_process(){
  270. if(blink_cnt>0){
  271. if(blink_cnt%2) LED_SetColor(LED_PAIR,COLOR_BLUE);
  272. else LED_SetColor(LED_PAIR,COLOR_BLACK);
  273. if(blink_cnt>=20){
  274. blink_cnt = 0;
  275. LED_Stop(LED_PAIR);
  276. }
  277. blink_cnt++;
  278. }
  279. }
  280. void app_pair_Initialize(void)
  281. {
  282. Process_Start(100,"app_pair_Right_Process",app_pair_Right_Process);
  283. Process_Start(100,"app_pair_Left",app_pair_Left_Process);
  284. Process_Start(500,"Blink_flag",app_led_blink_process);
  285. BLE_Client_Rx_Regist(BLE_PAIR,cb_Client_BLE_PAIR);
  286. BLE_Host_Rx_Regist(BLE_PAIR,cb_Host_BLE_PAIR);
  287. }