123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- #include "hal_ble_client.h"
- #include "system.h"
- #include "bsp_time.h"
- #include "nrf_gpio.h"
- extern unsigned int send_bytes_client(unsigned char *bytes, uint16_t len);
- void BLE_Client_Tx_Process(void);
- static const int RxLen = 1024;
- static volatile unsigned char RxBuf[RxLen];
- static volatile unsigned char* RxW=RxBuf;
- static volatile unsigned char* RxR=RxBuf;
- void BLE_Client_Push(unsigned char* p,int len)
- {
- volatile unsigned char *W=RxW;
- if(len<=0) return;
- for(int i=0;i<len;i++){
- W=RxW+1; if(W>=RxBuf+RxLen) W=RxBuf;
- if(W!=RxR){*RxW=*(p+i); RxW=W;}
- else break;
- }
- }
- static unsigned int CheckLen(void)
- {
- unsigned int Len;
- volatile unsigned char *W=RxW; volatile unsigned char *R=RxR;
- if(W>=R)Len=W-R;else Len=(W+RxLen)-R;
- return Len;
- }
- static unsigned char CheckByte(unsigned short n)
- {
- volatile unsigned char *R=RxR+n;
- if(R>=(RxBuf+RxLen))R-=RxLen;
- return *R;
- }
- static void Discard(unsigned short n)
- {
- while(n){ n--;
- if(RxR==RxW) return;
- if(RxR+1>=RxBuf+RxLen){RxR=RxBuf;} else RxR++;
- }
- }
- #define BLE_Client_RX_CMD_LEN 256
- static BLE_Client_Rx_t mBLE_Client_Rx[BLE_NUM_OF];
- static unsigned char cmdDatBuf[BLE_Client_RX_CMD_LEN];
- static unsigned int rxNum = 0;
- int BLE_Client_Rx_Regist(BLE_CMD_n cmd,BLE_Client_Callback cb)
- {
- if(rxNum>=BLE_NUM_OF) return -1;
- mBLE_Client_Rx[rxNum].cb = cb;
- mBLE_Client_Rx[rxNum].cmd = cmd;
- mBLE_Client_Rx[rxNum].pDat = cmdDatBuf;
- rxNum++;
- return 0;
- }
- static void Protocol(unsigned char len)
- {
- #if DEBUG_BLE_Client
- DEBUG_LOG("Rx_BLE_Client(%d):",CheckByte(1)); for(int i=0;i<len;i++){DEBUG_LOG("%02X ",CheckByte(i));} DEBUG_LOG("\r\n");
- #endif
- uint8_t cmd = CheckByte(3);
- if(len<5) return;
- for(int j=0;j<rxNum;j++){
- if(mBLE_Client_Rx[j].cmd==cmd&&mBLE_Client_Rx[j].cb){
- for(int i=0;i<len-5;i++) mBLE_Client_Rx[j].pDat[i] = CheckByte(i+4);
- mBLE_Client_Rx[j].datLen = len-5;
- mBLE_Client_Rx[j].cb((BLE_Client_Rx_t*)&mBLE_Client_Rx[j]);
- break;
- }
- }
- }
- void BLE_Client_Rx_Process(void)
- {
- static unsigned char R=0;
- static unsigned char L=0;
-
- while(1){
- switch( R ){
- case 0: if( CheckLen()<3 )return; else{
- if(CheckByte(0)!=0xAA){ Discard(1);}
- else{unsigned char LF=CheckByte(2);LF=~LF; L=CheckByte(1); if((LF==L)&&(L>=5)){ R++;}else { Discard(1);}}
- } break;
-
- case 1: if( CheckLen()<L) { return; }else{
- unsigned char i,ver=0;
- for(i=0;i<L-1;i++){ ver += CheckByte(i); }
- if(CheckByte(L-1)==ver){ Protocol(L);Discard(L);
- } else Discard(1);
- R=0;
- } break;
- default: R=0;break; }
- }
- }
- int BLE_Client_Send(BLE_CMD_n cmd,unsigned char *pDat,unsigned char datLen)
- {
- unsigned char buf[255];
- unsigned char ver=0;
- unsigned char i,L=0,Len=datLen+5;
- if(datLen > 250)return -1;
- buf[L]=0xAA; ver+=buf[L++];
- buf[L]=Len; ver+=buf[L++];
- buf[L]=~Len; ver+=buf[L++];
- buf[L]=cmd; ver+=buf[L++];
- for(i=0;i<datLen; i++){ buf[L]=pDat[i];ver+=buf[L++];}
- buf[L++]=ver;
- #if DEBUG_BLE_Client
- DEBUG_LOG("Tx_BLE_Client:"); for(int i=0;i<L;i++){DEBUG_LOG("%02X ",buf[i]);} DEBUG_LOG("\r\n");
- #endif
- return send_bytes_client(buf,L);
- }
- static BLE_Client_Tx_t* head_handle = 0;
- void BLE_Client_Tx_Send(BLE_Client_Tx_t* handle,BLE_CMD_n cmd,unsigned char *pDat,unsigned char datLen)
- {
- BLE_Client_Tx_t* target = head_handle;
- if(handle){
- handle->cmd = cmd;
- handle->pDat = pDat;
- handle->datLen = datLen;
- handle->tcnt = handle->t;
- if(handle->n>0) handle->ncnt = handle->n-1;
- else handle->ncnt = 0;
- handle->holdon = 1;
- while(target){
- if(target==handle){
- return;
- }
- target = target->next ;
- }
- handle->next = head_handle;
- head_handle = handle;
- }
- BLE_Client_Send(cmd,pDat,datLen);
- }
- void BLE_Client_Tx_Clear(BLE_Client_Tx_t* handle)
- {
- BLE_Client_Tx_t** curr;
- for(curr=&head_handle;*curr;){
- BLE_Client_Tx_t* entry = *curr;
- if(entry==handle){
- handle->holdon = 0;
- *curr = entry->next;
- }else{
- curr = &entry->next;
- }
- }
- }
- void BLE_Client_Tx_Process(void)
- {
- BLE_Client_Tx_t* target;
- uint8_t holdon = 0;
- static uint32_t tim = 0;
- uint32_t tim_dif =0;
- for(target=head_handle; target; target=target->next) {
- if(target->tcnt > 0){
-
- tim_dif = (TIME_GetTicks()- tim);
- tim = TIME_GetTicks();
- if(tim_dif < HeartTime_Interval)tim_dif = HeartTime_Interval;
-
- if(target->tcnt >= tim_dif)target->tcnt -= tim_dif;
- else target->tcnt =0;
-
- if(target->tcnt==0){
- if(target->ncnt>0){target->ncnt--;
- target->tcnt = target->t;
- BLE_Client_Send(target->cmd,target->pDat,target->datLen);
-
- }else{
- BLE_Client_Tx_Clear(target);
- if(target->cb){
- target->cb(target);
- }
- }
- }
- }
- holdon |= target->holdon;
- }
- Process_SetHoldOn(BLE_Client_Tx_Process,holdon);
- }
- #include "ble_comm.h"
- void BLE_Client_Initialize(void)
- {
- Process_Start(0,"BLE_Client_Rx",BLE_Client_Rx_Process);
- Process_Start(HeartTime_Interval,"BLE_Client_Tx",BLE_Client_Tx_Process);
- #if USEFIFO
- Process_Start(0,"send_bytes_client_pcs",send_bytes_client_pcs);
- #endif
- }
|