hal_ble_host.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. #include "hal_ble_host.h"
  2. #include "system.h"
  3. #include "bsp_time.h"
  4. /************************ 函数声明 ***********************************/
  5. extern int send_bytes_server(uint8_t * bytes, uint16_t len);
  6. void BLE_Host_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. static BLE_Host_Callback_cb clientcb;
  14. int BLE_Host_Rx_Regist_Game(BLE_Host_Callback_cb cb) //注册
  15. {
  16. clientcb = cb;
  17. return 0;
  18. }
  19. void BLE_Host_Push(unsigned char* p,int len)
  20. {
  21. volatile unsigned char *W=RxW; //这里要与上面指针相同
  22. if(len<5) return;
  23. unsigned char L = p[1];
  24. unsigned char LF = ~p[2];
  25. if(p[0]==0xAA&&L==LF&&L==len&&p[3]<5){
  26. clientcb(p,len);
  27. }else{
  28. for(int i=0;i<len;i++){
  29. W=RxW+1; if(W>=RxBuf+RxLen) W=RxBuf; //取下一位置(到顶转到底)
  30. if(W!=RxR){*RxW=*(p+i); RxW=W;}
  31. else break;
  32. }
  33. }
  34. }
  35. static unsigned int CheckLen(void) //检查RX接收了多少数据
  36. {
  37. unsigned int Len; //short
  38. volatile unsigned char *W=RxW; volatile unsigned char *R=RxR;
  39. if(W>=R)Len=W-R;else Len=(W+RxLen)-R; //这样正确(中途中断改变也变不了结果)
  40. return Len;
  41. }
  42. //static unsigned char ReadByte(void) //读RX中数锯,地指加一,和丢弃
  43. //{
  44. // unsigned char R=*RxR; //读数
  45. // if(RxR!=RxW){ if(RxR+1>=(RxBuf+RxLen))RxR =RxBuf; else RxR++;}//下标
  46. // return R;
  47. //}
  48. static unsigned char CheckByte(unsigned short n) //看RX中数锯,地指不变,
  49. {
  50. volatile unsigned char *R=RxR+n;
  51. if(R>=(RxBuf+RxLen))R-=RxLen;
  52. return *R;
  53. }
  54. static void Discard(unsigned short n) //丢弃RX数据几位
  55. {
  56. while(n){ n--;
  57. if(RxR==RxW) return;
  58. if(RxR+1>=RxBuf+RxLen){RxR=RxBuf;} else RxR++; //下标
  59. }
  60. }
  61. //********************* 接收 **********************//
  62. #define BLE_Host_RX_CMD_LEN 256
  63. static BLE_Host_Rx_t mBLE_Host_Rx[BLE_NUM_OF];
  64. static unsigned char cmdDatBuf[BLE_Host_RX_CMD_LEN]; //存放一条完整命令
  65. static unsigned int rxNum = 0;
  66. int BLE_Host_Rx_Regist(BLE_CMD_n cmd,BLE_Host_Callback cb) //注册
  67. {
  68. if(rxNum>=BLE_NUM_OF) return -1;
  69. mBLE_Host_Rx[rxNum].cb = cb;
  70. mBLE_Host_Rx[rxNum].cmd = cmd;
  71. mBLE_Host_Rx[rxNum].pDat = cmdDatBuf;
  72. rxNum++;
  73. return 0;
  74. }
  75. //********************* 接收协议 **********************//
  76. //协议(1位头+ 1位长度+ 1位长度反码+ 1命令+ N数据+ 1效验)
  77. static void Protocol(unsigned char len) //协议处理
  78. {
  79. #if DEBUG_BLE_Host
  80. DEBUG_LOG("Rx_BLE_Host(%d):",CheckByte(1)); for(int i=0;i<len;i++){DEBUG_LOG("%02X ",CheckByte(i));} DEBUG_LOG("\r\n");
  81. #endif
  82. uint8_t cmd = CheckByte(3);
  83. if(cmd<5){
  84. uint8_t buf[BLE_Host_RX_CMD_LEN];
  85. for(int i=0;i<len-5;i++) buf[i] = CheckByte(i+4);
  86. BLE_Host_Tx_Send(0,(BLE_CMD_n)cmd,buf,len-5);
  87. }
  88. if(len<5) return;
  89. for(int j=0;j<rxNum;j++){
  90. if(mBLE_Host_Rx[j].cmd==cmd&&mBLE_Host_Rx[j].cb){
  91. for(int i=0;i<len-5;i++) mBLE_Host_Rx[j].pDat[i] = CheckByte(i+4);
  92. mBLE_Host_Rx[j].datLen = len-5;
  93. mBLE_Host_Rx[j].cb((BLE_Host_Rx_t*)&mBLE_Host_Rx[j]);
  94. break;
  95. }
  96. }
  97. }
  98. //协议(1位头+ 1位长度+ 1位长度反码+ 1命令+ N数据+ 1效验)
  99. void BLE_Host_Rx_Process(void)
  100. {
  101. static unsigned char R=0;
  102. static unsigned char L=0;
  103. //接收缓存有数据,就全部处理
  104. while(1){
  105. switch( R ){
  106. case 0: if( CheckLen()<3 )return; else{
  107. if(CheckByte(0)!=0xAA){ Discard(1);}
  108. else{unsigned char LF=CheckByte(2);LF=~LF; L=CheckByte(1); if((LF==L)&&(L>=5)){ R++;}else { Discard(1);}}
  109. } break;
  110. // 多收数据 7 = 3位头+ 1位长度负数+ 1位长度+ 2效验
  111. case 1: if( CheckLen()<L) { return; }else{ //DEBUG_LOG("Rx:"); for(int i=0;i<L;i++){DEBUG_LOG("%02X ",CheckByte(i));} DEBUG_LOG("\r\n");
  112. unsigned char i,ver=0;
  113. for(i=0;i<L-1;i++){ ver += CheckByte(i); }
  114. if(CheckByte(L-1)==ver){ Protocol(L);Discard(L);//丢弃整条命令
  115. } else Discard(1);
  116. R=0;
  117. } break;
  118. default: R=0;break; }
  119. }
  120. }
  121. //********************* 发送协议 **********************//
  122. //协议(1位头+ 1位长度+ 1位长度反码+ 1命令+ N数据+ 1效验)
  123. void BLE_Host_Send(BLE_CMD_n cmd,unsigned char *pDat,unsigned char datLen) //协议发送
  124. {
  125. unsigned char buf[255];
  126. unsigned char ver=0;
  127. unsigned char i,L=0,Len=datLen+5;
  128. if(datLen > 250)return;
  129. buf[L]=0xAA; ver+=buf[L++]; // 头
  130. buf[L]=Len; ver+=buf[L++]; //1位长度
  131. buf[L]=~Len; ver+=buf[L++]; //1位长度反码
  132. buf[L]=cmd; ver+=buf[L++]; //1命令
  133. for(i=0;i<datLen; i++){ buf[L]=pDat[i];ver+=buf[L++];} //数据
  134. buf[L++]=ver; //效验
  135. #if DEBUG_BLE_Host
  136. DEBUG_LOG("Tx_BLE_Host:"); for(int i=0;i<L;i++){DEBUG_LOG("%02X ",buf[i]);} DEBUG_LOG("\r\n");
  137. #endif
  138. send_bytes_server(buf,L); //压入发送缓存
  139. }
  140. static BLE_Host_Tx_t* head_handle = 0;
  141. void BLE_Host_Tx_Send(BLE_Host_Tx_t* handle,BLE_CMD_n cmd,unsigned char *pDat,unsigned char datLen)
  142. {
  143. BLE_Host_Tx_t* target = head_handle;
  144. if(handle){
  145. handle->cmd = cmd;
  146. handle->pDat = pDat;
  147. handle->datLen = datLen;
  148. handle->tcnt = handle->t;
  149. if(handle->n>0) handle->ncnt = handle->n-1;
  150. else handle->ncnt = 0;
  151. handle->holdon = 1;
  152. while(target){ //检查是否已经存在
  153. if(target==handle){
  154. // DEBUG_LOG("handle(%d)\n",handle);
  155. return;
  156. }
  157. target = target->next ;
  158. }
  159. // DEBUG_LOG("add handle(%d)\n",handle);
  160. handle->next = head_handle;
  161. head_handle = handle;
  162. }
  163. BLE_Host_Send(cmd,pDat,datLen);
  164. }
  165. void BLE_Host_Tx_Clear(BLE_Host_Tx_t* handle)
  166. {
  167. BLE_Host_Tx_t** curr;
  168. for(curr=&head_handle;*curr;){
  169. BLE_Host_Tx_t* entry = *curr;
  170. if(entry==handle){
  171. handle->holdon = 0;
  172. *curr = entry->next;
  173. }else{
  174. curr = &entry->next;
  175. }
  176. }
  177. }
  178. void BLE_Host_Tx_Process(void)
  179. {
  180. BLE_Host_Tx_t* target;
  181. uint8_t holdon = 0;
  182. static uint32_t tim = 0;
  183. uint32_t tim_dif =0;
  184. for(target=head_handle; target; target=target->next) {
  185. if(target->tcnt > 0){
  186. tim_dif = (TIME_GetTicks()- tim);
  187. tim = TIME_GetTicks();
  188. if(tim_dif < HeartTime_Interval)tim_dif = HeartTime_Interval;
  189. if(target->tcnt >= tim_dif)target->tcnt -= tim_dif;
  190. else target->tcnt =0;
  191. if(target->tcnt==0){
  192. if(target->ncnt>0){target->ncnt--;
  193. target->tcnt = target->t;
  194. BLE_Host_Send((BLE_CMD_n)target->cmd,target->pDat,target->datLen);
  195. }else{
  196. BLE_Host_Tx_Clear(target);
  197. if(target->cb){
  198. target->cb(target);
  199. }
  200. }
  201. }
  202. }
  203. holdon |= target->holdon;
  204. }
  205. Process_SetHoldOn(BLE_Host_Tx_Process,holdon);
  206. }
  207. void BLE_Host_Initialize(void)
  208. {
  209. Process_Start(0,"BLE_Host_Rx",BLE_Host_Rx_Process);
  210. Process_Start(HeartTime_Interval,"BLE_Host_Tx",BLE_Host_Tx_Process);
  211. }