hal_ble_client.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #include "hal_ble_client.h"
  2. #include "system.h"
  3. #include "bsp_time.h"
  4. /************************ 函数声明 ***********************************/
  5. extern unsigned int send_bytes_client(unsigned char *bytes, uint16_t len);
  6. void BLE_Client_Tx_Process(void);
  7. /************************ 变量 ***********************************/
  8. /********************** 环形缓存区 *************************/
  9. static const int RxLen = 1024;
  10. static volatile unsigned char RxBuf[RxLen];
  11. static volatile unsigned char* RxW=RxBuf;
  12. static volatile unsigned char* RxR=RxBuf;
  13. void BLE_Client_Push(unsigned char* p,int len)
  14. {
  15. volatile unsigned char *W=RxW; //这里要与上面指针相同
  16. if(len<=0) return;
  17. for(int i=0;i<len;i++){
  18. W=RxW+1; if(W>=RxBuf+RxLen) W=RxBuf; //取下一位置(到顶转到底)
  19. if(W!=RxR){*RxW=*(p+i); RxW=W;}
  20. else break;
  21. }
  22. }
  23. static unsigned int CheckLen(void) //检查RX接收了多少数据
  24. {
  25. unsigned int Len; //short
  26. volatile unsigned char *W=RxW; volatile unsigned char *R=RxR;
  27. if(W>=R)Len=W-R;else Len=(W+RxLen)-R; //这样正确(中途中断改变也变不了结果)
  28. return Len;
  29. }
  30. //static unsigned char ReadByte(void) //读RX中数锯,地指加一,和丢弃
  31. //{
  32. // unsigned char R=*RxR; //读数
  33. // if(RxR!=RxW){ if(RxR+1>=(RxBuf+RxLen))RxR =RxBuf; else RxR++;}//下标
  34. // return R;
  35. //}
  36. static unsigned char CheckByte(unsigned short n) //看RX中数锯,地指不变,
  37. {
  38. volatile unsigned char *R=RxR+n;
  39. if(R>=(RxBuf+RxLen))R-=RxLen;
  40. return *R;
  41. }
  42. static void Discard(unsigned short n) //丢弃RX数据几位
  43. {
  44. while(n){ n--;
  45. if(RxR==RxW) return;
  46. if(RxR+1>=RxBuf+RxLen){RxR=RxBuf;} else RxR++; //下标
  47. }
  48. }
  49. //********************* 接收 **********************//
  50. #define BLE_Client_RX_CMD_LEN 256
  51. static BLE_Client_Rx_t mBLE_Client_Rx[BLE_Client_NUM_OF_R];
  52. static unsigned char cmdDatBuf[BLE_Client_RX_CMD_LEN]; //存放一条完整命令
  53. static unsigned int rxNum = 0;
  54. int BLE_Client_Rx_Regist(unsigned char cmd,BLE_Client_Callback cb) //注册
  55. {
  56. if(rxNum>=BLE_Client_NUM_OF_R) return -1;
  57. mBLE_Client_Rx[rxNum].cb = cb;
  58. mBLE_Client_Rx[rxNum].cmd = cmd;
  59. mBLE_Client_Rx[rxNum].pDat = cmdDatBuf;
  60. rxNum++;
  61. return 0;
  62. }
  63. //********************* 接收协议 **********************//
  64. //协议(1位头+ 1位长度+ 1位长度反码+ 1命令+ N数据+ 1效验)
  65. static void Protocol(unsigned char len) //协议处理
  66. {
  67. #if DEBUG_BLE_Client
  68. SEGGER_RTT_printf(0,"Rx_BLE_Client(%d):",CheckByte(1)); for(int i=0;i<len;i++){SEGGER_RTT_printf(0,"%02X ",CheckByte(i));} SEGGER_RTT_printf(0,"\r\n");
  69. #endif
  70. uint8_t cmd = CheckByte(3);
  71. if(len<5) return;
  72. for(int j=0;j<rxNum;j++){
  73. if(mBLE_Client_Rx[j].cmd==cmd&&mBLE_Client_Rx[j].cb){
  74. for(int i=0;i<len-5;i++) mBLE_Client_Rx[j].pDat[i] = CheckByte(i+4);
  75. mBLE_Client_Rx[j].datLen = len-5;
  76. mBLE_Client_Rx[j].cb((BLE_Client_Rx_t*)&mBLE_Client_Rx[j]);
  77. break;
  78. }
  79. }
  80. }
  81. //协议(1位头+ 1位长度+ 1位长度反码+ 1命令+ N数据+ 1效验)
  82. void BLE_Client_Rx_Process(void)
  83. {
  84. static unsigned char R=0;
  85. static unsigned char L=0;
  86. //接收缓存有数据,就全部处理
  87. while(1){
  88. switch( R ){
  89. case 0: if( CheckLen()<3 )return; else{
  90. if(CheckByte(0)!=0xAA){ Discard(1);}
  91. else{unsigned char LF=CheckByte(2);LF=~LF; L=CheckByte(1); if((LF==L)&&(L>=5)){ R++;}else { Discard(1);}}
  92. } break;
  93. // 多收数据 7 = 3位头+ 1位长度负数+ 1位长度+ 2效验
  94. case 1: if( CheckLen()<L) { return; }else{ //SEGGER_RTT_printf(0,"Rx:"); for(int i=0;i<L;i++){SEGGER_RTT_printf(0,"%02X ",CheckByte(i));} SEGGER_RTT_printf(0,"\r\n");
  95. unsigned char i,ver=0;
  96. for(i=0;i<L-1;i++){ ver += CheckByte(i); }
  97. if(CheckByte(L-1)==ver){ Protocol(L);Discard(L);//丢弃整条命令
  98. } else Discard(1);
  99. R=0;
  100. } break;
  101. default: R=0;break; }
  102. }
  103. }
  104. //********************* 发送协议 **********************//
  105. //协议(1位头+ 1位长度+ 1位长度反码+ 1命令+ N数据+ 1效验)
  106. void BLE_Client_Send(unsigned char cmd,unsigned char *pDat,unsigned char datLen) //协议发送
  107. {
  108. unsigned char buf[255];
  109. unsigned char ver=0;
  110. unsigned char i,L=0,Len=datLen+5;
  111. buf[L]=0xAA; ver+=buf[L++]; // 头
  112. buf[L]=Len; ver+=buf[L++]; //1位长度
  113. buf[L]=~Len; ver+=buf[L++]; // 1位长度反码
  114. buf[L]=cmd; ver+=buf[L++]; //1命令
  115. for(i=0;i<datLen; i++){ buf[L]=pDat[i];ver+=buf[L++];} //数据
  116. buf[L++]=ver; //效验
  117. #if DEBUG_BLE_Client
  118. SEGGER_RTT_printf(0,"Tx_BLE_Client:"); for(int i=0;i<L;i++){SEGGER_RTT_printf(0,"%02X ",buf[i]);} SEGGER_RTT_printf(0,"\r\n");
  119. #endif
  120. send_bytes_client(buf,L); //压入发送缓存
  121. }
  122. static BLE_Client_Tx_t* head_handle = 0;
  123. void BLE_Client_Tx_Send(BLE_Client_Tx_t* handle,unsigned char cmd,unsigned char *pDat,unsigned char datLen)
  124. {
  125. BLE_Client_Tx_t* target = head_handle;
  126. if(handle){
  127. handle->cmd = cmd;
  128. handle->pDat = pDat;
  129. handle->datLen = datLen;
  130. handle->tcnt = handle->t;
  131. if(handle->n>0) handle->ncnt = handle->n-1;
  132. else handle->ncnt = 0;
  133. handle->holdon = 1;
  134. while(target){ //检查是否已经存在
  135. if(target==handle){
  136. return;
  137. }
  138. target = target->next ;
  139. }
  140. handle->next = head_handle;
  141. head_handle = handle;
  142. }
  143. BLE_Client_Send(cmd,pDat,datLen);
  144. }
  145. void BLE_Client_Tx_Clear(BLE_Client_Tx_t* handle)
  146. {
  147. BLE_Client_Tx_t** curr;
  148. for(curr=&head_handle;*curr;){
  149. BLE_Client_Tx_t* entry = *curr;
  150. if(entry==handle){
  151. handle->holdon = 0;
  152. *curr = entry->next;
  153. }else{
  154. curr = &entry->next;
  155. }
  156. }
  157. }
  158. void BLE_Client_Tx_Process(void)
  159. {
  160. BLE_Client_Tx_t* target;
  161. uint8_t holdon = 0;
  162. for(target=head_handle; target; target=target->next) {
  163. if(target->tcnt > 0){
  164. if(target->tcnt >= HeartTime_Interval)target->tcnt -= HeartTime_Interval;
  165. else target->tcnt =0;
  166. if(target->tcnt==0){
  167. if(target->ncnt>0){target->ncnt--;
  168. target->tcnt = target->t;
  169. BLE_Client_Send(target->cmd,target->pDat,target->datLen);
  170. }else{
  171. BLE_Client_Tx_Clear(target);
  172. if(target->cb){
  173. target->cb(target);
  174. }
  175. }
  176. }
  177. }
  178. holdon |= target->holdon;
  179. }
  180. Process_SetHoldOn(BLE_Client_Tx_Process,holdon);
  181. }
  182. void BLE_Client_Initialize(void)
  183. {
  184. Process_Start(0,"BLE_Client_Rx",BLE_Client_Rx_Process);
  185. Process_Start(10,"BLE_Client_Tx",BLE_Client_Tx_Process);
  186. }