app_pair.c 9.2 KB

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