00001 /*! \file stxetx.c \brief STX/ETX Packet Protocol Implementation Library. */ 00002 //***************************************************************************** 00003 // 00004 // File Name : 'stxetx.c' 00005 // Title : STX/ETX Packet Protocol Implementation Library 00006 // Author : Pascal Stang - Copyright (C) 2002 00007 // Created : 10/9/2002 00008 // Revised : 11/15/2002 00009 // Version : 0.1 00010 // Target MCU : any 00011 // Editor Tabs : 4 00012 // 00013 // Description : This library provides a set of functions needed to send and 00014 // receive STX/ETX packets. STX/ETX is a simple packet protocol that can 00015 // be wrapped around user data for one or more of the following reasons: 00016 // 00017 // 1. packetization is needed 00018 // - Using packets can be helpful if your data naturally forms 00019 // little "bunches" or if different types of data must be sent 00020 // over the same channel (a serial cable, for example). If your 00021 // data forms "bunches", you can send user data inside STX/ETX 00022 // packets with a predetermined structure, like an array of A/D 00023 // conversion results. If you need a way to tell the receiver 00024 // what kind of data you're sending, you can use the TYPE field 00025 // in the STX/ETX packet. 00026 // 2. error checking is needed 00027 // - STX/ETX packets will add a checksum to your data. This 00028 // allows the receiver to verify that data was received correctly 00029 // and is error-free. Packets which are corrupted in transmission 00030 // and fail the the checksum test are automatically discarded. 00031 // Error checking is especially useful when the data transmission 00032 // channel is unreliable or noisy (examples: radio, infrared, long 00033 // cables, etc) 00034 // 00035 // STX/ETX packets have the following structure: 00036 // 00037 // [STX][status][type][length][user data...][checksum][ETX] 00038 // 00039 // All fields are 1 byte except for user data which may be 0-255 bytes. 00040 // Uppercase fields are constant (STX=0x02, ETX=0x03), lowercase fields 00041 // vary. The length field is the number of bytes in the user data area. 00042 // The checksum is the 8-bit sum of all bytes between but not including 00043 // STX/ETX. 00044 // 00045 // This code is distributed under the GNU Public License 00046 // which can be found at http://www.gnu.org/licenses/gpl.txt 00047 // 00048 //***************************************************************************** 00049 00050 #include "global.h" 00051 #include "stxetx.h" 00052 00053 #include "rprintf.h" 00054 00055 // function pointer to data output routine 00056 static void (*stxetxDataOut)(unsigned char data); 00057 00058 // received packet data buffer 00059 unsigned char stxetxRxPacket[STXETX_MAXRXPACKETSIZE]; 00060 00061 // functions 00062 00063 00064 // Initialize STX/ETX packet protocol library 00065 void stxetxInit(void (*dataout_func)(unsigned char data)) 00066 { 00067 stxetxDataOut = dataout_func; 00068 } 00069 00070 // Send/Create STX/ETX packet 00071 void stxetxSend(unsigned char status, unsigned char type, unsigned char datalength, unsigned char* dataptr) 00072 { 00073 unsigned char checksum = 0; 00074 unsigned short i; 00075 00076 // write packet header 00077 stxetxDataOut(STX); 00078 stxetxDataOut(status); 00079 stxetxDataOut(type); 00080 stxetxDataOut(datalength); 00081 // update checksum 00082 checksum += type + datalength; 00083 00084 // copy data into packet 00085 for(i = 0; i < datalength; i++) 00086 { 00087 stxetxDataOut(*dataptr); 00088 checksum += *dataptr; 00089 dataptr++; 00090 } 00091 // write packet trailer 00092 stxetxDataOut(checksum); 00093 stxetxDataOut(ETX); 00094 } 00095 00096 // process buffer containing STX/ETX packets 00097 unsigned char stxetxProcess(cBuffer* rxBuffer) 00098 { 00099 unsigned char foundpacket = FALSE; 00100 unsigned short i,j,k; 00101 unsigned char length, checksum; 00102 //unsigned char type; 00103 00104 // process the buffer 00105 // go through buffer looking for packets 00106 // the STX must be located at least STXETX_HEADERLENGTH+STXETX_TRAILERLENGTH from end 00107 // otherwise we must not have a complete packet 00108 for(i = 0; i < (rxBuffer->datalength - (STXETX_HEADERLENGTH+STXETX_TRAILERLENGTH)); i++) 00109 { 00110 // look for a potential start of packet 00111 if(bufferGetAtIndex(rxBuffer, i) == STX) 00112 { 00113 // if this is a start, then get the length 00114 length = bufferGetAtIndex(rxBuffer, i+STXETX_LENGTHOFFSET); 00115 00116 // now we must be at least STXETX_HEADERLENGTH+length+STXETX_TRAILERLENGTH from buffer's end to continue 00117 if(rxBuffer->datalength >= (i+STXETX_HEADERLENGTH+length+STXETX_TRAILERLENGTH)) 00118 { 00119 // check to see if ETX is in the right position 00120 if(bufferGetAtIndex(rxBuffer, i+STXETX_HEADERLENGTH+length+STXETX_TRAILERLENGTH-1) == ETX) 00121 { 00122 // found potential packet 00123 // test checksum 00124 checksum = 0; 00125 // sum data between STX and ETX, not including checksum itself 00126 for(j = 0; j<(STXETX_HEADERLENGTH+length+STXETX_TRAILERLENGTH-STXETX_NOETXSTXCHECKSUM); j++) 00127 { 00128 checksum += bufferGetAtIndex(rxBuffer, i+j+STXETX_STATUSOFFSET); 00129 } 00130 // compare checksums 00131 if(checksum == bufferGetAtIndex(rxBuffer, i+STXETX_CHECKSUMOFFSET+length)) 00132 { 00133 //we have a packet! 00134 foundpacket = TRUE; 00135 00136 // copy data to buffer 00137 // (don't copy STX, ETX, or CHECKSUM) 00138 for(k = 0; k < (STXETX_HEADERLENGTH+length-1); k++) 00139 { 00140 stxetxRxPacket[k] = bufferGetAtIndex(rxBuffer, i+k+1); 00141 } 00142 00143 // get type for fun 00144 //type = bufferGetAtIndex(rxBuffer, i+STXETX_TYPEOFFSET); 00145 //rprintf("STXETX Received packet type: 0x%x\n",type); 00146 00147 // dump data from beginning of buffer out to end of packet 00148 // NOTE: we are dumping not only the received packet but also all 00149 // garbage data received before it 00150 bufferDumpFromFront(rxBuffer, i+STXETX_HEADERLENGTH+length+STXETX_TRAILERLENGTH); 00151 00152 // done with this processing session 00153 break; 00154 } 00155 } 00156 } 00157 } 00158 } 00159 00160 // check if receive buffer is full with no packets decoding 00161 // (ie. deadlocked on garbage data) 00162 if(!bufferIsNotFull(rxBuffer)) 00163 { 00164 // dump receive buffer contents to relieve deadlock 00165 bufferFlush(rxBuffer); 00166 } 00167 00168 return foundpacket; 00169 } 00170 00171 unsigned char stxetxGetRxPacketStatus(void) 00172 { 00173 // return the packet's status 00174 // (subtract 1 from the offset because the STX has already been discarded) 00175 return stxetxRxPacket[STXETX_STATUSOFFSET-1]; 00176 } 00177 00178 unsigned char stxetxGetRxPacketType(void) 00179 { 00180 // return the packet's type 00181 // (subtract 1 from the offset because the STX has already been discarded) 00182 return stxetxRxPacket[STXETX_TYPEOFFSET-1]; 00183 } 00184 00185 unsigned char stxetxGetRxPacketDatalength(void) 00186 { 00187 // return the packet's datalength 00188 // (subtract 1 from the offset because the STX has already been discarded) 00189 return stxetxRxPacket[STXETX_LENGTHOFFSET-1]; 00190 } 00191 00192 unsigned char* stxetxGetRxPacketData(void) 00193 { 00194 // return a pointer to the packet's data payload 00195 // (subtract 1 from the offset because the STX has already been discarded) 00196 return stxetxRxPacket+STXETX_DATAOFFSET-1; 00197 }
1.3-rc2