app_pair.c 9.8 KB

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