app_pair.c 9.3 KB

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