app_pair.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. /*********************************************************************
  16. * LOCAL DEFINETION
  17. */
  18. /*********************************************************************
  19. * LOCAL VARIABLES
  20. */
  21. static ble_gap_addr_t mAddr;
  22. static uint16_t isConfig = 0;
  23. /*********************************************************************
  24. * LOCAL FUNCTIONS
  25. */
  26. void UART0_Config_Send(uint8_t cmd)
  27. {
  28. uint8_t buf[32];
  29. uint8_t L = 0;
  30. //MAC 地址
  31. for(int i=0;i<6;i++){
  32. buf[L++] = mAddr.addr[5-i];
  33. }
  34. //硬件版本
  35. buf[L++] = (uint8_t)((uint16_t)HARDWARE_VERSION>>8);
  36. buf[L++] = (uint8_t)((uint16_t)HARDWARE_VERSION>>0);
  37. //软件版本
  38. buf[L++] = (uint8_t)((uint16_t)SOFTWARE_VERSION>>8);
  39. buf[L++] = (uint8_t)((uint16_t)SOFTWARE_VERSION>>0);
  40. UART0_Tx_Send(0,cmd,buf,L);
  41. }
  42. #define PAIR_TIMES 50 //配对次数
  43. void app_pair_Process(void)
  44. {
  45. static uint8_t flag = 0;
  46. if(app_charge_Getstate()!=BLE_Client_T_CHARGE_PULLOUT){
  47. if(flag != 1)
  48. {
  49. if(mFlash.isHost)UART0_Initialize(PIN_TXD_BLE,PIN_RXD_BLE,UART_HZ);
  50. else UART0_Initialize(UART0_INVALID_PIN,PIN_RXD_BLE,UART_HZ);
  51. flag = 1;
  52. }
  53. else if(isConfig<PAIR_TIMES){ isConfig++;
  54. if(mFlash.isHost){ //充电模式下主机主机申请配对
  55. UART0_Initialize(PIN_TXD_BLE,PIN_RXD_BLE,UART_HZ);
  56. UART0_Config_Send(UART0_T_CONFIG_ASK);
  57. UART0_Initialize(UART0_INVALID_PIN,PIN_RXD_BLE,UART_HZ);
  58. }
  59. }
  60. }else{
  61. if(flag != 2)
  62. {
  63. UART0_unInit(PIN_TXD_BLE,PIN_RXD_BLE);
  64. flag = 2;
  65. // nrf_gpio_cfg_output(PIN_LED_RUN); nrf_gpio_pin_write(PIN_LED_RUN,LED_SMALL_DISABLE);
  66. }
  67. else if(isConfig>0) isConfig = 0;
  68. }
  69. }
  70. void app_config_LED(void)
  71. {
  72. static uint8_t state = 0;
  73. switch(state){
  74. case 0:
  75. Process_SetHoldOn(app_config_LED,1);
  76. LED_Start(LED_CONFIG,COLOR_BLUE);
  77. state = 1;
  78. Process_UpdatePeroid(app_config_LED,2000);
  79. break;
  80. case 1:
  81. Process_UpdatePeroid(app_config_LED,0);
  82. LED_Stop(LED_CONFIG);
  83. state = 0;
  84. Process_SetHoldOn(app_config_LED,0);
  85. Process_Stop(app_config_LED);
  86. break;
  87. default:state=0;Process_UpdatePeroid(app_config_LED,0);break;
  88. }
  89. }
  90. void cb_UART0_R_CONFIG_ASK(void* handle)
  91. { //从机接收到配对申请
  92. UART0_Rx_t* target = handle;
  93. uint8_t mac[6];
  94. char buf[16];
  95. uint8_t ret = 0;
  96. if(app_charge_Getstate()==BLE_Client_T_CHARGE_PULLOUT) return; //非充电模式不匹配
  97. if(mFlash.isHost){
  98. return; //防止主机自发自收
  99. }
  100. for(int i=0;i<6;i++) mac[i] = target->pDat[i];
  101. uint16_t hv = ((uint16_t)target->pDat[6]<<8)| ((uint16_t)target->pDat[7]<<0);
  102. uint16_t sv = ((uint16_t)target->pDat[8]<<8)| ((uint16_t)target->pDat[9]<<0);
  103. if(slave_isconnect()) slave_disconnect();
  104. else advertising_stop();
  105. UART0_Initialize(PIN_TXD_BLE,UART0_INVALID_PIN,UART_HZ);
  106. for(int i=0; i < 10; i++){
  107. UART0_Config_Send(UART0_T_CONFIG_ACK);//应答主机申请
  108. }
  109. UART0_Initialize(UART0_INVALID_PIN,PIN_RXD_BLE,UART_HZ);
  110. mFlash.mClient.isConfig = 'C';
  111. if(mBackup.isConfig != mFlash.mClient.isConfig)ret = 1;
  112. for(int i=0;i<6;i++){
  113. mFlash.mClient.macAddr[i] = mAddr.addr[5-i]; //从机自身mac地址
  114. mFlash.macHost[i] = mac[i]; //主机发过来的mac地址
  115. if(mBackup.macAddr_L[i]!=mFlash.macHost[i]) ret = 1;
  116. if(mBackup.macAddr_R[i]!=mFlash.mClient.macAddr[i]) ret = 1;
  117. }
  118. mFlash.mClient.hardVersion = hv;
  119. mFlash.mClient.sotfVersion = sv;
  120. if( mBackup.hardVersion != mFlash.mClient.hardVersion) ret = 1;
  121. if( mBackup.sotfVersion != mFlash.mClient.sotfVersion) ret = 1;
  122. SEGGER_RTT_printf(0,"mFlash.mClient.hardVersion:%X\n",mFlash.mClient.hardVersion);
  123. SEGGER_RTT_printf(0,"mFlash.mClient.sotfVersion:%X\n",mFlash.mClient.sotfVersion);
  124. SEGGER_RTT_printf(0,"mFlash.mClient.macAddr:%02X %02X %02X %02X %02X %02X\n",mFlash.mClient.macAddr[0],mFlash.mClient.macAddr[1],mFlash.mClient.macAddr[2],mFlash.mClient.macAddr[3],mFlash.mClient.macAddr[4],mFlash.mClient.macAddr[5]);
  125. SEGGER_RTT_printf(0,"mFlash.macHost:%02X %02X %02X %02X %02X %02X\n",mFlash.macHost[0],mFlash.macHost[1],mFlash.macHost[2],mFlash.macHost[3],mFlash.macHost[4],mFlash.macHost[5]);
  126. Process_Start(0,"config_LED",app_config_LED);
  127. memset(buf,0,16);
  128. 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]);
  129. SEGGER_RTT_printf(0,"advName(%d):%s\n",strlen(buf),buf);
  130. slave_set_adv_name(buf,strlen(buf));
  131. slave_adv_init();
  132. if(ret >=1){
  133. for(int i=0;i<6;i++){
  134. mBackup.macAddr_L[i] = mFlash.macHost[i]; //主机地址
  135. mBackup.macAddr_R[i] = mFlash.mClient.macAddr[i];//从机地址
  136. }
  137. mBackup.hardVersion = mFlash.mClient.hardVersion;
  138. mBackup.sotfVersion = mFlash.mClient.sotfVersion;
  139. mBackup.isConfig = mFlash.mClient.isConfig;
  140. //SEGGER_RTT_printf(0,"AAAAAbackup config:%d,hardVersion:%d,sotfVersion:%d\n",mBackup.isConfig,mBackup.hardVersion,mBackup.sotfVersion);
  141. Flash_SaveInfomation();
  142. Flash_SaveBackup();
  143. }
  144. while(slave_isconnect());
  145. advertising_start();
  146. // nrf_gpio_cfg_output(PIN_LED_RUN); nrf_gpio_pin_write(PIN_LED_RUN,LED_SMALL_ENABLE);
  147. }
  148. void cb_UART0_R_CONFIG_ACK(void* handle)
  149. { //主机接收到配对应答
  150. UART0_Rx_t* target = handle;
  151. uint8_t mac[6];
  152. char buf[16];
  153. uint8_t ret = 0;
  154. if(app_charge_Getstate()==BLE_Client_T_CHARGE_PULLOUT) return; //非充电模式不匹配
  155. if(!mFlash.isHost) return; //从机自发自收
  156. for(int i=0;i<6;i++) mac[i] = target->pDat[i];
  157. uint16_t hv = ((uint16_t)target->pDat[6]<<8)| ((uint16_t)target->pDat[7]<<0);
  158. uint16_t sv = ((uint16_t)target->pDat[8]<<8)| ((uint16_t)target->pDat[9]<<0);
  159. isConfig = PAIR_TIMES+1; //已配对,停止配对发送
  160. mFlash.mClient.isConfig = 'C';
  161. if(mBackup.isConfig != mFlash.mClient.isConfig)ret = 1;
  162. for(int i=0;i<6;i++){
  163. mFlash.mClient.macAddr[i] = mac[i]; //收到的从机mac地址
  164. mFlash.macHost[i] = mAddr.addr[5-i]; //主机本身mac地址
  165. if(mBackup.macAddr_L[i]!=mFlash.macHost[i]) ret = 1;
  166. if(mBackup.macAddr_R[i]!=mFlash.mClient.macAddr[i]) ret = 1;
  167. }
  168. mFlash.mClient.hardVersion = hv;
  169. mFlash.mClient.sotfVersion = sv;
  170. if( mBackup.hardVersion != mFlash.mClient.hardVersion) ret = 1;
  171. if( mBackup.sotfVersion != mFlash.mClient.sotfVersion) ret = 1;
  172. SEGGER_RTT_printf(0,"mFlash.mClient.hardVersion:%X\n",mFlash.mClient.hardVersion);
  173. SEGGER_RTT_printf(0,"mFlash.mClient.sotfVersion:%X\n",mFlash.mClient.sotfVersion);
  174. SEGGER_RTT_printf(0,"mFlash.mClient.macAddr:%02X %02X %02X %02X %02X %02X\n",mFlash.mClient.macAddr[0],mFlash.mClient.macAddr[1],mFlash.mClient.macAddr[2],mFlash.mClient.macAddr[3],mFlash.mClient.macAddr[4],mFlash.mClient.macAddr[5]);
  175. SEGGER_RTT_printf(0,"mFlash.macHost:%02X %02X %02X %02X %02X %02X\n",mFlash.macHost[0],mFlash.macHost[1],mFlash.macHost[2],mFlash.macHost[3],mFlash.macHost[4],mFlash.macHost[5]);
  176. Process_Start(0,"config_LED",app_config_LED);
  177. memset(buf,0,16);
  178. 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]);
  179. SEGGER_RTT_printf(0,"scanName(%d):%s\n",strlen(buf),buf);
  180. host_set_scan_name(buf,strlen(buf));
  181. if(ret >=1){
  182. for(int i=0;i<6;i++){
  183. mBackup.macAddr_L[i] = mFlash.macHost[i]; //主机地址
  184. mBackup.macAddr_R[i] = mFlash.mClient.macAddr[i];//从机地址
  185. }
  186. mBackup.hardVersion = mFlash.mClient.hardVersion;
  187. mBackup.sotfVersion = mFlash.mClient.sotfVersion;
  188. mBackup.isConfig = mFlash.mClient.isConfig;
  189. //SEGGER_RTT_printf(0,"AAAAAbackup config:%d,hardVersion:%d,sotfVersion:%d\n",mBackup.isConfig,mBackup.hardVersion,mBackup.sotfVersion);
  190. Flash_SaveInfomation();
  191. Flash_SaveBackup();
  192. }
  193. // nrf_gpio_cfg_output(PIN_LED_RUN); nrf_gpio_pin_write(PIN_LED_RUN,LED_SMALL_ENABLE);
  194. }
  195. void app_pair_Init(void)
  196. {
  197. uint32_t err_code = sd_ble_gap_addr_get(&mAddr); APP_ERROR_CHECK(err_code);
  198. Process_Start(100,"app_pair_Process",app_pair_Process);
  199. UART0_Rx_Regist(UART0_R_CONFIG_ASK,cb_UART0_R_CONFIG_ASK);
  200. UART0_Rx_Regist(UART0_R_CONFIG_ACK,cb_UART0_R_CONFIG_ACK);
  201. }