app_pair.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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(isScan)nrf_ble_scan_stop();
  118. return;
  119. }
  120. uint8_t charge_state_c = hal_charge_Getstate();
  121. switch(state){//ble_advertising_init_t init;
  122. case 0:
  123. if((charge_state_c == BLE_CHARGE_INSERT || charge_state_c == BLE_CHARGE_DONE)){
  124. PAIR_DEBUG_LOG("<<<<<<<<<<<<<<<<<<<<<<<<\r\n");
  125. PAIR_DEBUG_LOG("start scan\r\n");
  126. advdata_report_Evt_Regist(Pair_adv_cb);
  127. ST_scan_stop();
  128. host_set_scan_name((char *)"***********",sizeof("***********"));
  129. ST_scan_start();
  130. state =1;
  131. // connetflag = 0;
  132. ScanFinishFlag = 0;
  133. }
  134. break;
  135. case 1:
  136. if(!(charge_state_c == BLE_CHARGE_INSERT || charge_state_c == BLE_CHARGE_DONE)){
  137. PAIR_DEBUG_LOG("charge out\r\n");
  138. // advdata_report_Evt_del(Pair_adv_cb);
  139. ST_scan_stop();
  140. state = 0;
  141. LED_Stop(LED_PAIR);
  142. // LED_Stop(LED_SCAN);
  143. if(host_isconnect())host_disconnect();
  144. }
  145. if(ScanFinishFlag>0){ScanFinishFlag = 0;
  146. state =1;
  147. advdata_report_Evt_del(Pair_adv_cb);
  148. if(host_isconnect()){host_disconnect();}
  149. uint32_t ScanResult = sd_ble_gap_scan_stop();
  150. host_set_scan_name(pair_name,strlen(pair_name));
  151. ST_scan_start();
  152. PAIR_DEBUG_LOG("ST_scan_start*********************\n");
  153. // Process_UpdatePeroid(app_pair_Right_Process,100);
  154. }
  155. if(host_isconnect()){
  156. PAIR_DEBUG_LOG("1");
  157. // if(connetflag==0){ connetflag = 1;
  158. // ST_scan_stop();
  159. // }
  160. app_ble_send_mac();
  161. // if(0 == flag){flag =1;
  162. // gpio_mt_run(1000);
  163. // }
  164. }
  165. // else{
  166. // flag =0;
  167. // }
  168. break;
  169. }
  170. }
  171. static void cb_Host_BLE_PAIR(void* handle)
  172. {
  173. BLE_Host_Rx_t* target = handle;
  174. if(host_isconnect()){host_disconnect();}
  175. if(target->pDat[12]==0) return;
  176. ST_scan_stop();
  177. blink_cnt = 1;
  178. LED_Start(LED_PAIR,COLOR_BLUE);
  179. // LED_Stop(LED_SCAN);
  180. // ST_scan_stop();
  181. uint8_t ret =0,i =0;
  182. uint8_t mymacbuf[6];
  183. // PAIR_DEBUG_LOG(">>>>>>>>cb_Host_BLE_PAIR =%d \r\n",target->datLen);
  184. uint8_t GetMacAddr[6]={0};
  185. memcpy(GetMacAddr,target->pDat,sizeof(GetMacAddr));
  186. 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);
  187. uint16_t sv = ((uint16_t)target->pDat[10]<<8) | ((uint16_t)target->pDat[11]<<0);
  188. for(i=0;i<6;i++){
  189. if(mFlash.macHost[i] != GetMacAddr[i]){
  190. ret =1;
  191. break;
  192. }
  193. }
  194. // PAIR_DEBUG_LOG("target->pDat[12] %d\n",target->pDat[12]);
  195. if(mFlash.mClient.hardVersion != hv || mFlash.mClient.sotfVersion != sv || ret !=0){
  196. PAIR_DEBUG_LOG("host config:%d %d %d %d\n",hv,sv,ret);
  197. PAIR_DEBUG_LOG("mFlash.mClient:%d %d %d %d\n\n",mFlash.mClient.hardVersion,mFlash.mClient.sotfVersion,mFlash.mClient.isConfig,ret);
  198. mFlash.mClient.hardVersion = hv;
  199. mFlash.mClient.sotfVersion = sv;
  200. Get_MACaddr(mymacbuf);
  201. for(i=0;i<6;i++){
  202. mFlash.mClient.macAddr[i] = mymacbuf[i]; //从机自身mac地址
  203. mFlash.macHost[i] = GetMacAddr[i]; //主机发过来的mac地址
  204. }
  205. app_pair_flash_save();
  206. char buf[16];
  207. memset(buf,0,sizeof(buf));
  208. //
  209. 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]);
  210. PAIR_DEBUG_LOG("advName(%d):%s\n",strlen(buf),buf);
  211. slave_set_adv_name(buf,strlen(buf));
  212. advertising_stop();
  213. slave_adv_init();
  214. PAIR_DEBUG_LOG(">>>>>>>>advertising_start =0 \r\n");
  215. advertising_start();
  216. }
  217. }
  218. static void cb_Client_BLE_PAIR(void* handle)
  219. {
  220. uint8_t ret =0;
  221. uint8_t i =0;
  222. uint8_t mymacbuf[6];
  223. uint8_t RightMacAddr[6]={0};
  224. BLE_Client_Rx_t* target = handle;
  225. if(hal_charge_Getstate() == BLE_CHARGE_PULLOUT )return;
  226. app_ble_send_mac();
  227. PAIR_DEBUG_LOG(">>>>>>>>cb_Client_BLE_PAIR =0;\r\n");
  228. memcpy(RightMacAddr,target->pDat,sizeof(RightMacAddr));
  229. 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);
  230. uint16_t sv = ((uint16_t)target->pDat[10]<<8) | ((uint16_t)target->pDat[11]<<0);
  231. blink_cnt = 1;
  232. LED_Start(LED_PAIR,COLOR_BLUE);
  233. for(i=0;i<6;i++){
  234. if(mFlash.mClient.macAddr[i] != RightMacAddr[i]){
  235. ret =1;
  236. break;
  237. }
  238. }
  239. if(mFlash.mClient.hardVersion != hv || mFlash.mClient.sotfVersion != sv || ret !=0){
  240. PAIR_DEBUG_LOG("mFlash config:%d %d %d %d\n\n",mFlash.mClient.hardVersion,mFlash.mClient.sotfVersion,mFlash.mClient.isConfig,ret);
  241. mFlash.mClient.hardVersion = hv;
  242. mFlash.mClient.sotfVersion = sv;
  243. Get_MACaddr(mymacbuf);
  244. for(i=0;i<6;i++){
  245. mFlash.mClient.macAddr[i] = RightMacAddr[i]; //收到的从机mac地址
  246. mFlash.macHost[i] = mymacbuf[i]; //主机本身mac地址
  247. }
  248. UpdateScanFlag =1;
  249. ST_scan_stop();
  250. if(host_isconnect()){host_disconnect();}
  251. }
  252. }
  253. void app_led_blink_process(){
  254. if(blink_cnt>0){
  255. if(blink_cnt%2) LED_SetColor(LED_PAIR,COLOR_BLUE);
  256. else LED_SetColor(LED_PAIR,COLOR_BLACK);
  257. if(blink_cnt>=20){
  258. blink_cnt = 0;
  259. LED_Stop(LED_PAIR);
  260. }
  261. blink_cnt++;
  262. }
  263. }
  264. void app_pair_Initialize(void)
  265. {
  266. Process_Start(100,"app_pair_Right_Process",app_pair_Right_Process);
  267. Process_Start(100,"app_pair_Left",app_pair_Left_Process);
  268. Process_Start(500,"Blink_flag",app_led_blink_process);
  269. BLE_Client_Rx_Regist(BLE_PAIR,cb_Client_BLE_PAIR);
  270. BLE_Host_Rx_Regist(BLE_PAIR,cb_Host_BLE_PAIR);
  271. }