app_pair.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*********************************************************************
  2. * INCLUDES
  3. */
  4. #include "ble_comm.h"
  5. #include "ble_gap.h"
  6. #include "nrf_gpio.h"
  7. #include "bsp_time.h"
  8. #include "app_flash.h"
  9. #include "hal_ble_uart0.h"
  10. #include "hal_ble_client.h"
  11. #include "hal_ble_host.h"
  12. #include "hal_led.h"
  13. #include "app_charge.h"
  14. #include "app_pair.h"
  15. #include "hal_mt.h"
  16. /*********************************************************************
  17. * LOCAL DEFINETION
  18. */
  19. /*********************************************************************
  20. * LOCAL VARIABLES
  21. */
  22. static ble_gap_addr_t mAddr;
  23. static uint16_t isConfigcnt = 0;
  24. static uint8_t PairFlagHost =0;
  25. /*********************************************************************
  26. * LOCAL FUNCTIONS
  27. */
  28. static pair_reslt_t Pair_result_t_head ={
  29. .next =NULL,
  30. };
  31. void app_UartPair_Regist(pair_reslt_t *cb){
  32. pair_reslt_t *target = &Pair_result_t_head;
  33. while(NULL != target->next){
  34. if(target->next == cb)return;//检查是否已经存在
  35. target = target->next;
  36. }
  37. cb->next = NULL;
  38. target->next = cb;
  39. }
  40. void app_UartPair_Clear(pair_reslt_t *cb){
  41. pair_reslt_t *target = &Pair_result_t_head;
  42. while(NULL != target->next){
  43. if(cb == target->next){
  44. target->next = cb->next;
  45. return;
  46. }
  47. target = target->next;
  48. }
  49. }
  50. static void app_UartPair_Result_Add(void){
  51. pair_reslt_t *target = &Pair_result_t_head;
  52. while(NULL != target->next){
  53. target->next->flag =1;
  54. target = target->next;
  55. }
  56. }
  57. void UART0_Config_Send(uint8_t cmd)
  58. {
  59. uint8_t buf[32];
  60. uint8_t L = 0;
  61. //MAC 地址
  62. for(int i=0;i<6;i++){
  63. buf[L++] = mAddr.addr[5-i];
  64. }
  65. //硬件版本
  66. buf[L++] = (uint8_t)(HARDWARE_VERSION>>24);
  67. buf[L++] = (uint8_t)(HARDWARE_VERSION>>16);
  68. buf[L++] = (uint8_t)(HARDWARE_VERSION>>8);
  69. buf[L++] = (uint8_t)(HARDWARE_VERSION>>0);
  70. //软件版本
  71. buf[L++] = (uint8_t)(SOFTWARE_VERSION>>8);
  72. buf[L++] = (uint8_t)(SOFTWARE_VERSION>>0);
  73. UART0_Tx_Send(0,cmd,buf,L);
  74. }
  75. #define PAIR_TIMES 50 //配对次数
  76. static void app_pair_Process(void)
  77. {
  78. static uint8_t flag = 0;
  79. if(app_charge_Getstate()!=BLE_CHARGE_PULLOUT){
  80. if(flag != 1){flag = 1;
  81. if(mFlash.isHost)UART0_Initialize(PIN_TXD_BLE,PIN_RXD_BLE,UART_HZ);
  82. else UART0_Initialize(UART0_INVALID_PIN,PIN_RXD_BLE,UART_HZ);
  83. }
  84. else if(isConfigcnt<PAIR_TIMES){ isConfigcnt++;
  85. if(mFlash.isHost){ //充电模式下主机主机申请配对
  86. UART0_Initialize(PIN_TXD_BLE,PIN_RXD_BLE,UART_HZ);
  87. UART0_Config_Send(UART0_T_CONFIG_ASK);
  88. UART0_Initialize(UART0_INVALID_PIN,PIN_RXD_BLE,UART_HZ);
  89. }
  90. }
  91. }else{
  92. if(flag != 2){flag = 2;
  93. UART0_unInit(PIN_TXD_BLE,PIN_RXD_BLE);
  94. }
  95. else if(isConfigcnt>0) isConfigcnt = 0;
  96. }
  97. static uint8_t Pair_state =0;
  98. static uint32_t tim =0;
  99. char buf[16];
  100. uint8_t i =0;
  101. switch(Pair_state){
  102. case 0:
  103. if(PairFlagHost){PairFlagHost =0;
  104. MT_Run(500);
  105. //配对时候清空所有的步数
  106. if(Flash_DeleteAllStep() != ZONE_OP_SUCCESS){Except_TxError(EXCEPT_DATEStep,"clear step fail");break;}
  107. memset(&mFlash.mStep,0,sizeof(FlashStep_t));
  108. mFlash.mClient.isConfig = 'C';
  109. if(Flash_SaveStep() != ZONE_OP_SUCCESS){
  110. Except_TxError(EXCEPT_DATEStep,"save step fail");
  111. DEBUG_LOG("EXCEPT_DATEStep save step fail\r\n");
  112. }
  113. DEBUG_LOG("mFlash.mClient.macAddr:%02X %02X %02X %02X %02X %02X\n",mFlash.mClient.macAddr[0],mFlash.mClient.macAddr[1],mFlash.mClient.macAddr[2],
  114. mFlash.mClient.macAddr[3],mFlash.mClient.macAddr[4],mFlash.mClient.macAddr[5]);
  115. //保存数据到备份区域里面
  116. for( i=0; i<RecordMacAddrL; i++){
  117. mBackup.macAddr_L[i] = mFlash.macHost[i]; //主机地址
  118. mBackup.macAddr_R[i] = mFlash.mClient.macAddr[i];//从机地址
  119. }
  120. mBackup.hardVersion = mFlash.mClient.hardVersion;
  121. mBackup.sotfVersion = mFlash.mClient.sotfVersion;
  122. mBackup.isConfig = mFlash.mClient.isConfig;
  123. if(Flash_SaveBackup() != ZONE_OP_SUCCESS)Except_TxError(EXCEPT_Pair,"pair save backup fail");
  124. Pair_state =1;
  125. tim = TIME_GetTicks();
  126. }
  127. break;
  128. case 1:
  129. if(TIME_GetTicks()-tim>=10000){tim = TIME_GetTicks();//10秒超时退出
  130. Pair_state =0;
  131. DEBUG_LOG("Pair_state =0 \r\n");
  132. }
  133. if(mFlash.isHost){//主机处理
  134. if(host_isconnect()){
  135. host_disconnect();
  136. }else{
  137. DEBUG_LOG(">>>>>ST_scan_start =0 \r\n");
  138. memset(buf,0,sizeof(buf));
  139. 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]);
  140. DEBUG_LOG("scanName(%d):%s\n",strlen(buf),buf);
  141. host_set_scan_name(buf,strlen(buf));
  142. ST_scan_stop();
  143. ST_scan_start();
  144. Pair_state = 0;
  145. }
  146. }else{//从机处理
  147. if(slave_isconnect()) slave_disconnect();
  148. else {
  149. memset(buf,0,sizeof(buf));
  150. 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]);
  151. DEBUG_LOG("advName(%d):%s\n",strlen(buf),buf);
  152. slave_set_adv_name(buf,strlen(buf));
  153. advertising_stop();
  154. slave_adv_init();
  155. DEBUG_LOG(">>>>>>>>advertising_start =0 \r\n");
  156. advertising_start();
  157. Pair_state = 0;
  158. }
  159. }
  160. break;
  161. default:Pair_state = 0;break;
  162. }
  163. }
  164. #if BLUE_LED_TEST_ENANBLE
  165. void app_config_LED(void)
  166. {
  167. static uint8_t state = 0;
  168. switch(state){
  169. case 0:
  170. Process_SetHoldOn(app_config_LED,1);
  171. LED_Start(LED_CONFIG,COLOR_BLUE);
  172. state = 1;
  173. Process_UpdatePeroid(app_config_LED,2000);
  174. break;
  175. case 1:
  176. Process_UpdatePeroid(app_config_LED,0);
  177. LED_Stop(LED_CONFIG);
  178. state = 0;
  179. Process_SetHoldOn(app_config_LED,0);
  180. Process_Stop(app_config_LED);
  181. break;
  182. default:state=0;Process_UpdatePeroid(app_config_LED,0);break;
  183. }
  184. }
  185. #endif
  186. void cb_UART0_R_CONFIG_ASK(void* handle)
  187. { //从机接收到配对申请
  188. UART0_Rx_t* target = handle;
  189. uint8_t mac[6];
  190. uint8_t ret = 0;
  191. uint8_t i =0;
  192. if(app_charge_Getstate()==BLE_CHARGE_PULLOUT) return; //非充电模式不匹配
  193. if(mFlash.isHost)return; //防止主机自发自收
  194. for(i=0;i<6;i++) mac[i] = target->pDat[i];
  195. 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);
  196. uint16_t sv = ((uint16_t)target->pDat[10]<<8) | ((uint16_t)target->pDat[11]<<0);
  197. UART0_Initialize(PIN_TXD_BLE,UART0_INVALID_PIN,UART_HZ);
  198. for(i=0; i < 10; i++){
  199. UART0_Config_Send(UART0_T_CONFIG_ACK);//应答主机申请
  200. }
  201. UART0_Initialize(UART0_INVALID_PIN,PIN_RXD_BLE,UART_HZ);
  202. for(i=0;i<6;i++){
  203. if(mFlash.macHost[i] != mac[i]){ //检验主机发过来的mac地址是否上次的一样
  204. ret =1;
  205. break;
  206. }
  207. }
  208. if(mFlash.mClient.hardVersion != hv || mFlash.mClient.sotfVersion != sv \
  209. || ret !=0
  210. ){
  211. DEBUG_LOG("host config:%d %d %d %d\n",hv,sv,ret);
  212. 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. for(i=0;i<6;i++){
  216. mFlash.mClient.macAddr[i] = mAddr.addr[5-i]; //从机自身mac地址
  217. mFlash.macHost[i] = mac[i]; //主机发过来的mac地址
  218. }
  219. PairFlagHost =1;
  220. }
  221. app_UartPair_Result_Add();
  222. #if BLUE_LED_TEST_ENANBLE
  223. Process_Start(0,"config_LED",app_config_LED);
  224. #endif
  225. }
  226. void cb_UART0_R_CONFIG_ACK(void* handle)
  227. { //主机接收到配对应答
  228. UART0_Rx_t* target = handle;
  229. uint8_t mac[6];
  230. uint8_t ret = 0;
  231. uint8_t i=0;
  232. if(app_charge_Getstate()==BLE_CHARGE_PULLOUT) return; //非充电模式不匹配
  233. if(!mFlash.isHost) return; //从机自发自收
  234. for(i=0;i<6;i++) mac[i] = target->pDat[i];
  235. 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);
  236. uint16_t sv = ((uint16_t)target->pDat[10]<<8) | ((uint16_t)target->pDat[11]<<0);
  237. isConfigcnt = PAIR_TIMES+1; //已配对,停止配对发送
  238. for(i=0;i<6;i++){
  239. if(mFlash.mClient.macAddr[i] != mac[i]){ //检验主机发过来的mac地址是否上次的一样
  240. ret =1;
  241. break;
  242. }
  243. }
  244. if(mFlash.mClient.hardVersion != hv || mFlash.mClient.sotfVersion != sv \
  245. || ret !=0
  246. ){
  247. DEBUG_LOG("mFlash config:%d %d %d %d\n\n",mFlash.mClient.hardVersion,mFlash.mClient.sotfVersion,mFlash.mClient.isConfig,ret);
  248. mFlash.mClient.hardVersion = hv;
  249. mFlash.mClient.sotfVersion = sv;
  250. for(i=0;i<6;i++){
  251. mFlash.mClient.macAddr[i] = mac[i]; //收到的从机mac地址
  252. mFlash.macHost[i] = mAddr.addr[5-i]; //主机本身mac地址
  253. }
  254. PairFlagHost =1;
  255. }
  256. app_UartPair_Result_Add();
  257. #if BLUE_LED_TEST_ENANBLE
  258. Process_Start(0,"config_LED",app_config_LED);
  259. #endif
  260. }
  261. void app_pair_Init(void)
  262. {
  263. uint32_t err_code = sd_ble_gap_addr_get(&mAddr); APP_ERROR_CHECK(err_code);
  264. Process_Start(100,"app_pair_Process",app_pair_Process);
  265. UART0_Rx_Regist(UART0_R_CONFIG_ASK,cb_UART0_R_CONFIG_ASK);
  266. UART0_Rx_Regist(UART0_R_CONFIG_ACK,cb_UART0_R_CONFIG_ACK);
  267. }