app_pair.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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)Except_TxError(EXCEPT_DATEStep,"save step fail");
  110. DEBUG_LOG("mFlash.mClient.macAddr:%02X %02X %02X %02X %02X %02X\n",mFlash.mClient.macAddr[0],mFlash.mClient.macAddr[1],mFlash.mClient.macAddr[2],
  111. mFlash.mClient.macAddr[3],mFlash.mClient.macAddr[4],mFlash.mClient.macAddr[5]);
  112. //保存备份区域里面的数据
  113. for(i=0;i<6;i++){
  114. mBackup.macAddr_L[i] = mFlash.macHost[i]; //主机地址
  115. mBackup.macAddr_R[i] = mFlash.mClient.macAddr[i];//从机地址
  116. }
  117. mBackup.hardVersion = mFlash.mClient.hardVersion;
  118. mBackup.sotfVersion = mFlash.mClient.sotfVersion;
  119. mBackup.isConfig = mFlash.mClient.isConfig;
  120. if(Flash_SaveBackup() != ZONE_OP_SUCCESS)Except_TxError(EXCEPT_Pair,"save backup fail");
  121. Pair_state =1;
  122. tim = TIME_GetTicks();
  123. }
  124. break;
  125. case 1:
  126. if(TIME_GetTicks()-tim>=10000){tim = TIME_GetTicks();//10秒超时退出
  127. Pair_state =0;
  128. DEBUG_LOG("Pair_state =0 \r\n");
  129. }
  130. if(mFlash.isHost){//主机处理
  131. if(host_isconnect()){
  132. host_disconnect();
  133. }else{
  134. DEBUG_LOG(">>>>>ST_scan_start =0 \r\n");
  135. memset(buf,0,sizeof(buf));
  136. 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]);
  137. DEBUG_LOG("scanName(%d):%s\n",strlen(buf),buf);
  138. host_set_scan_name(buf,strlen(buf));
  139. ST_scan_stop();
  140. ST_scan_start();
  141. Pair_state = 0;
  142. }
  143. }else{//从机处理
  144. if(slave_isconnect()) slave_disconnect();
  145. else {
  146. memset(buf,0,sizeof(buf));
  147. 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]);
  148. DEBUG_LOG("advName(%d):%s\n",strlen(buf),buf);
  149. slave_set_adv_name(buf,strlen(buf));
  150. advertising_stop();
  151. slave_adv_init();
  152. DEBUG_LOG(">>>>>>>>advertising_start =0 \r\n");
  153. advertising_start();
  154. Pair_state = 0;
  155. }
  156. }
  157. break;
  158. default:Pair_state = 0;break;
  159. }
  160. }
  161. #if BLUE_LED_TEST_ENANBLE
  162. void app_config_LED(void)
  163. {
  164. static uint8_t state = 0;
  165. switch(state){
  166. case 0:
  167. Process_SetHoldOn(app_config_LED,1);
  168. LED_Start(LED_CONFIG,COLOR_BLUE);
  169. state = 1;
  170. Process_UpdatePeroid(app_config_LED,2000);
  171. break;
  172. case 1:
  173. Process_UpdatePeroid(app_config_LED,0);
  174. LED_Stop(LED_CONFIG);
  175. state = 0;
  176. Process_SetHoldOn(app_config_LED,0);
  177. Process_Stop(app_config_LED);
  178. break;
  179. default:state=0;Process_UpdatePeroid(app_config_LED,0);break;
  180. }
  181. }
  182. #endif
  183. void cb_UART0_R_CONFIG_ASK(void* handle)
  184. { //从机接收到配对申请
  185. UART0_Rx_t* target = handle;
  186. uint8_t mac[6];
  187. uint8_t ret = 0;
  188. uint8_t i =0;
  189. if(app_charge_Getstate()==BLE_CHARGE_PULLOUT) return; //非充电模式不匹配
  190. if(mFlash.isHost)return; //防止主机自发自收
  191. for(i=0;i<6;i++) mac[i] = target->pDat[i];
  192. 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);
  193. uint16_t sv = ((uint16_t)target->pDat[10]<<8) | ((uint16_t)target->pDat[11]<<0);
  194. UART0_Initialize(PIN_TXD_BLE,UART0_INVALID_PIN,UART_HZ);
  195. for(i=0; i < 10; i++){
  196. UART0_Config_Send(UART0_T_CONFIG_ACK);//应答主机申请
  197. }
  198. UART0_Initialize(UART0_INVALID_PIN,PIN_RXD_BLE,UART_HZ);
  199. for(i=0;i<6;i++){
  200. if(mFlash.macHost[i] != mac[i]){ //检验主机发过来的mac地址是否上次的一样
  201. ret =1;
  202. break;
  203. }
  204. }
  205. if(mFlash.mClient.hardVersion != hv || mFlash.mClient.sotfVersion != sv \
  206. || mBackup.isConfig != mFlash.mClient.isConfig
  207. || ret !=0
  208. ){
  209. DEBUG_LOG("backup config:%d %d %d %d\n",hv,sv,ret);
  210. DEBUG_LOG("backup config:%d %d %d %d\n\n",mFlash.mClient.hardVersion,mFlash.mClient.sotfVersion,mFlash.mClient.isConfig,ret);
  211. mFlash.mClient.hardVersion = hv;
  212. mFlash.mClient.sotfVersion = sv;
  213. mBackup.hardVersion = mFlash.mClient.hardVersion;
  214. mBackup.sotfVersion = mFlash.mClient.sotfVersion;
  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. || mBackup.isConfig != mFlash.mClient.isConfig
  246. || ret !=0
  247. ){
  248. DEBUG_LOG("backup config:%d %d %d %d\n",hv,sv,mBackup.isConfig,ret);
  249. DEBUG_LOG("backup config:%d %d %d %d\n\n",mFlash.mClient.hardVersion,mFlash.mClient.sotfVersion,mFlash.mClient.isConfig,ret);
  250. mFlash.mClient.hardVersion = hv;
  251. mFlash.mClient.sotfVersion = sv;
  252. mBackup.hardVersion = hv;
  253. mBackup.sotfVersion = sv;
  254. for(i=0;i<6;i++){
  255. mFlash.mClient.macAddr[i] = mac[i]; //收到的从机mac地址
  256. mFlash.macHost[i] = mAddr.addr[5-i]; //主机本身mac地址
  257. }
  258. PairFlagHost =1;
  259. }
  260. app_UartPair_Result_Add();
  261. #if BLUE_LED_TEST_ENANBLE
  262. Process_Start(0,"config_LED",app_config_LED);
  263. #endif
  264. }
  265. void app_pair_Init(void)
  266. {
  267. uint32_t err_code = sd_ble_gap_addr_get(&mAddr); APP_ERROR_CHECK(err_code);
  268. Process_Start(100,"app_pair_Process",app_pair_Process);
  269. UART0_Rx_Regist(UART0_R_CONFIG_ASK,cb_UART0_R_CONFIG_ASK);
  270. UART0_Rx_Regist(UART0_R_CONFIG_ACK,cb_UART0_R_CONFIG_ACK);
  271. }