⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dispatcher.c

📁 A dispatcher system using leaky bucket architecture. The code was written for HC12, HCS08, and compi
💻 C
字号:
/** ###################################################################
**     Filename  : dispatcher.C
**     Project   : rs232
**     Processor : MC9S08GT60CFB
**     Compiler  : CodeWarrior HCS08 C Compiler
**     Date/Time : 10/3/2007, 8:29 PM
**     Contents  :
**         User source code
**
**     (c) Copyright UNIS, spol. s r.o. 1997-2008
**     UNIS, spol. s r.o.
**     Jundrovska 33
**     624 00 Brno
**     Czech Republic
**     http      : www.processorexpert.com
**     mail      : info@processorexpert.com
** ###################################################################*/

/* MODULE dispatcher */
#include "dispatcher.h"

///Leaky Loop Variables///
  byte dp_buffer[DMAXBSIZE];
  byte dp_packetrecieved[DMAXBSIZE]; 
  
  int dp_pstart, dp_pend, dp_pendreal;
  int dp_Rpacketsize;
  
  int dp_m,dp_q,dp_v,dp_endpacket;
  int testAshkan;
  
/// Dispatcher Variables ///  
/// The variables with small names are local variables used differently in different places //
  FunctionsTable fTable[FTABLESIZE];
  int dp_availableFunctions;
  int dp_i,dp_d, dp_fc ,  dp_fnamestart,dp_fnameend  ;
  byte * dp_fargumentptr;

  

  
////////////////// LEAKY LOOP ////////////////////////////////////////
  

byte dispatcher_LeakyLoop_charAt ( int im ) {
  while (im>=DMAXBSIZE) {
    im = im -  DMAXBSIZE;
  }
  return (dp_buffer[im]); 

}

//When a character Arrives,
//We first put it in the leaky loop bucket
//then we try to process it, if it contain a '/'  (it resemble a package)

void dispatcher_inchar(byte* inChar,int dp_size ) {
      
    dispatcher_LeakyLoop_Inchar(inChar,dp_size);
    
    //Check if there is a full packet in the leaking loop;
    for ( dp_v = 0 ; dp_v<dp_size ; dp_v++) {
      AS1_SendChar(inChar[dp_v]);
      if ( inChar[dp_v] == '/' ) {
        if (dispatcher_LeakyLoop_getpacket(dp_packetrecieved,&dp_Rpacketsize) ) {
            //AS1_SendChar('-');
            dispatcher_dispatch ( dp_packetrecieved ,dp_Rpacketsize); 
        } 
    }
  }
  
  
}


  
//Returns True if there is such a package.  
//and put it in packetrecieved and put the size in recievedSize

bool dispatcher_LeakyLoop_getpacket(byte *packetrecieved, int *recievedSize) {

  (*recievedSize) = 0;
  //find the begining of the packet in the buffer 
  for ( dp_q = dp_pstart; dp_q < dp_pend ; dp_q ++ ) {
    if ( dispatcher_LeakyLoop_charAt(dp_q) == '/' &&
         dispatcher_LeakyLoop_charAt(dp_q+1) == ',' &&
         dp_q+1 < dp_pend ) {
         
          dp_pstart = dp_q;
          break;
         }
  }
  
  for ( dp_q = dp_pstart; dp_q < dp_pend ; dp_q ++ ) {
    if ( dispatcher_LeakyLoop_charAt(dp_q-1) == ',' &&
         dispatcher_LeakyLoop_charAt(dp_q) == '/' &&
         dp_q < dp_pend ) {
         
          dp_endpacket = dp_q+1;
         }
  }
  
  
  
//dp_pend points to the memory cell which we would right :D  
 if ( dispatcher_LeakyLoop_charAt(dp_pstart) == '/'
  && dispatcher_LeakyLoop_charAt(dp_pstart+1) == ','
  && dispatcher_LeakyLoop_charAt(dp_endpacket-2) == ','
  && dispatcher_LeakyLoop_charAt(dp_endpacket-1) == '/'    ) {
  
     for (dp_q =0 ;  dp_q < dp_endpacket- dp_pstart; dp_q++) {
      packetrecieved[dp_q] = dispatcher_LeakyLoop_charAt(dp_pstart + dp_q);
      (*recievedSize)++;
    }
    
    dp_pstart= dp_endpacket;
    return TRUE;
  }else{
    return FALSE; 
  }
}



void dispatcher_LeakyLoop_Inchar(byte* inChar,int dp_size ) {
    if ( dp_pend + dp_size >= dp_pstart + DMAXBSIZE ) //We have more characters in the buffer than possible
    {
        /* !!!!!!!!!!! ERROR !!!!!!!!!!!!!!!!!! */
        dp_pend = dp_pstart = 0;
    }
    
    //If both pointers have gone through one loop, it's like non of them have !
    if ( (dp_pend >= DMAXBSIZE)  && (dp_pstart >= DMAXBSIZE) ) { 
      dp_pend = dp_pend - DMAXBSIZE;
      dp_pstart= dp_pstart - DMAXBSIZE;
    }
    
    //Add the incomming characters to the buffer
    for (dp_m = 0 ; dp_m<dp_size ; dp_m++) {
      if ( dp_pend>= DMAXBSIZE) {
        dp_pendreal = dp_pend - DMAXBSIZE;
      }else {
        dp_pendreal = dp_pend;
      }
      
      dp_buffer[ dp_pendreal ] = inChar[dp_m] ;     //Put the incoming character at the end of the buffer
      dp_pend ++;                             //Increase the pointer to the end of the buffer
    }
  }
   
  
  //////////////////// Dispatcher ////////////////////////////
  
  void dispatcher_init(void) {
     //Leaky Loop init 
     dp_pstart = dp_pend = dp_pendreal = 0;
     
     //Dispatcher init
     dp_availableFunctions = 0;
  }

  void dispatcher_clearFtable(void) {
      dp_availableFunctions = 0; 
  }  
  
  void dispatcher_addFunction(byte *Name,void (*functionPointer)(byte *inStructure)) {
    dp_i = 0;
    
    //Copy the CString Name to the name :D
    while ( Name[dp_i] != '\n' && dp_i<MessageHeaderMaxLength ) {
      fTable[dp_availableFunctions].channelName[dp_i] = Name[dp_i];
      dp_i++;
    }
    
    if ( dp_i < 1 )
     return;
    
    if ( functionPointer == 0 )
      return;
    
    //Copy the function pointer to the fTable.
    fTable[dp_availableFunctions].functionPointer = functionPointer; 
    
    dp_availableFunctions ++ ;
  }


  
  void dispatcher_dispatch(byte *Packet, int psize  ) {
  dp_fnamestart=dp_fnameend=0;
  
    //Get the name of the function
    for ( dp_d = 0 ; dp_d<psize ; dp_d ++ ) {
      if (Packet[dp_d]==',') {
        dp_fnamestart = dp_d+1 ; //The first character after ,
        break;
      }
    }
  
    for ( dp_d = dp_fnamestart+1 ; dp_d<psize ; dp_d ++ ) {
      if (Packet[dp_d]==','){
        dp_fnameend = dp_d ;
        break;
      }
    }
    
    
    //Try to search for the function name
    for ( dp_fc  = 0 ; dp_fc <dp_availableFunctions; dp_fc ++) {
        
        dp_d = 0;
        while (  fTable[dp_fc].channelName[dp_d] == Packet[ dp_fnamestart + dp_d]   ){
        
            if ( dp_d == dp_fnameend- dp_fnamestart -1 ) {
              //The name is a full match 
              dp_fargumentptr = Packet + dp_fnameend+1 ;
              (*fTable[dp_fc].functionPointer)( (byte *)dp_fargumentptr  );  //Call the function which handle that packet
            }
            dp_d++;
        }
        
    }
    
   }
 
 


/* END dispatcher */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -