app_pair.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. switch(Pair_state){
  101. case 0:
  102. if(PairFlagHost){PairFlagHost =0;
  103. MT_Run(500);
  104. //配对时候清空所有的步数
  105. if(Flash_DeleteAllStep() != ZONE_OP_SUCCESS){Except_TxError(EXCEPT_DATEStep,"clear step fail");break;}
  106. memset(&mFlash.mStep,0,sizeof(FlashStep_t));
  107. mFlash.mClient.isConfig = 'C';
  108. if(Flash_SaveStep() != ZONE_OP_SUCCESS){
  109. Except_TxError(EXCEPT_DATEStep,"save step fail");
  110. DEBUG_LOG("EXCEPT_DATEStep save step fail\r\n");
  111. }
  112. DEBUG_LOG("mFlash.mClient.macAddr:%02X %02X %02X %02X %02X %02X\n",mFlash.mClient.macAddr[0],mFlash.mClient.macAddr[1],mFlash.mClient.macAddr[2],
  113. mFlash.mClient.macAddr[3],mFlash.mClient.macAddr[4],mFlash.mClient.macAddr[5]);
  114. Pair_state =1;
  115. tim = TIME_GetTicks();
  116. }
  117. break;
  118. case 1:
  119. if(TIME_GetTicks()-tim>=10000){tim = TIME_GetTicks();//10秒超时退出
  120. Pair_state =0;
  121. DEBUG_LOG("Pair_state =0 \r\n");
  122. }
  123. if(mFlash.isHost){//主机处理
  124. if(host_isconnect()){
  125. host_disconnect();
  126. }else{
  127. DEBUG_LOG(">>>>>ST_scan_start =0 \r\n");
  128. memset(buf,0,sizeof(buf));
  129. 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]);
  130. DEBUG_LOG("scanName(%d):%s\n",strlen(buf),buf);
  131. host_set_scan_name(buf,strlen(buf));
  132. ST_scan_stop();
  133. ST_scan_start();
  134. Pair_state = 0;
  135. }
  136. }else{//从机处理
  137. if(slave_isconnect()) slave_disconnect();
  138. else {
  139. memset(buf,0,sizeof(buf));
  140. 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]);
  141. DEBUG_LOG("advName(%d):%s\n",strlen(buf),buf);
  142. slave_set_adv_name(buf,strlen(buf));
  143. advertising_stop();
  144. slave_adv_init();
  145. DEBUG_LOG(">>>>>>>>advertising_start =0 \r\n");
  146. advertising_start();
  147. Pair_state = 0;
  148. }
  149. }
  150. break;
  151. default:Pair_state = 0;break;
  152. }
  153. }
  154. #if BLUE_LED_TEST_ENANBLE
  155. void app_config_LED(void)
  156. {
  157. static uint8_t state = 0;
  158. switch(state){
  159. case 0:
  160. Process_SetHoldOn(app_config_LED,1);
  161. LED_Start(LED_CONFIG,COLOR_BLUE);
  162. state = 1;
  163. Process_UpdatePeroid(app_config_LED,2000);
  164. break;
  165. case 1:
  166. Process_UpdatePeroid(app_config_LED,0);
  167. LED_Stop(LED_CONFIG);
  168. state = 0;
  169. Process_SetHoldOn(app_config_LED,0);
  170. Process_Stop(app_config_LED);
  171. break;
  172. default:state=0;Process_UpdatePeroid(app_config_LED,0);break;
  173. }
  174. }
  175. #endif
  176. void cb_UART0_R_CONFIG_ASK(void* handle)
  177. { //从机接收到配对申请
  178. UART0_Rx_t* target = handle;
  179. uint8_t mac[6];
  180. uint8_t ret = 0;
  181. uint8_t i =0;
  182. if(app_charge_Getstate()==BLE_CHARGE_PULLOUT) return; //非充电模式不匹配
  183. if(mFlash.isHost)return; //防止主机自发自收
  184. for(i=0;i<6;i++) mac[i] = target->pDat[i];
  185. 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);
  186. uint16_t sv = ((uint16_t)target->pDat[10]<<8) | ((uint16_t)target->pDat[11]<<0);
  187. UART0_Initialize(PIN_TXD_BLE,UART0_INVALID_PIN,UART_HZ);
  188. for(i=0; i < 10; i++){
  189. UART0_Config_Send(UART0_T_CONFIG_ACK);//应答主机申请
  190. }
  191. UART0_Initialize(UART0_INVALID_PIN,PIN_RXD_BLE,UART_HZ);
  192. for(i=0;i<6;i++){
  193. if(mFlash.macHost[i] != mac[i]){ //检验主机发过来的mac地址是否上次的一样
  194. ret =1;
  195. break;
  196. }
  197. }
  198. if(mFlash.mClient.hardVersion != hv || mFlash.mClient.sotfVersion != sv \
  199. || ret !=0
  200. ){
  201. DEBUG_LOG("host config:%d %d %d %d\n",hv,sv,ret);
  202. DEBUG_LOG("mFlash.mClient:%d %d %d %d\n\n",mFlash.mClient.hardVersion,mFlash.mClient.sotfVersion,mFlash.mClient.isConfig,ret);
  203. mFlash.mClient.hardVersion = hv;
  204. mFlash.mClient.sotfVersion = sv;
  205. for(i=0;i<6;i++){
  206. mFlash.mClient.macAddr[i] = mAddr.addr[5-i]; //从机自身mac地址
  207. mFlash.macHost[i] = mac[i]; //主机发过来的mac地址
  208. }
  209. PairFlagHost =1;
  210. }
  211. app_UartPair_Result_Add();
  212. #if BLUE_LED_TEST_ENANBLE
  213. Process_Start(0,"config_LED",app_config_LED);
  214. #endif
  215. }
  216. void cb_UART0_R_CONFIG_ACK(void* handle)
  217. { //主机接收到配对应答
  218. UART0_Rx_t* target = handle;
  219. uint8_t mac[6];
  220. uint8_t ret = 0;
  221. uint8_t i=0;
  222. if(app_charge_Getstate()==BLE_CHARGE_PULLOUT) return; //非充电模式不匹配
  223. if(!mFlash.isHost) return; //从机自发自收
  224. for(i=0;i<6;i++) mac[i] = target->pDat[i];
  225. 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);
  226. uint16_t sv = ((uint16_t)target->pDat[10]<<8) | ((uint16_t)target->pDat[11]<<0);
  227. isConfigcnt = PAIR_TIMES+1; //已配对,停止配对发送
  228. for(i=0;i<6;i++){
  229. if(mFlash.mClient.macAddr[i] != mac[i]){ //检验主机发过来的mac地址是否上次的一样
  230. ret =1;
  231. break;
  232. }
  233. }
  234. if(mFlash.mClient.hardVersion != hv || mFlash.mClient.sotfVersion != sv \
  235. || ret !=0
  236. ){
  237. DEBUG_LOG("mFlash config:%d %d %d %d\n\n",mFlash.mClient.hardVersion,mFlash.mClient.sotfVersion,mFlash.mClient.isConfig,ret);
  238. mFlash.mClient.hardVersion = hv;
  239. mFlash.mClient.sotfVersion = sv;
  240. for(i=0;i<6;i++){
  241. mFlash.mClient.macAddr[i] = mac[i]; //收到的从机mac地址
  242. mFlash.macHost[i] = mAddr.addr[5-i]; //主机本身mac地址
  243. }
  244. PairFlagHost =1;
  245. }
  246. app_UartPair_Result_Add();
  247. #if BLUE_LED_TEST_ENANBLE
  248. Process_Start(0,"config_LED",app_config_LED);
  249. #endif
  250. }
  251. void app_pair_Init(void)
  252. {
  253. uint32_t err_code = sd_ble_gap_addr_get(&mAddr); APP_ERROR_CHECK(err_code);
  254. Process_Start(100,"app_pair_Process",app_pair_Process);
  255. UART0_Rx_Regist(UART0_R_CONFIG_ASK,cb_UART0_R_CONFIG_ASK);
  256. UART0_Rx_Regist(UART0_R_CONFIG_ACK,cb_UART0_R_CONFIG_ACK);
  257. }