| Line No. | Rev | Author | Line |
|---|---|---|---|
| 1 | 6 | kaklik | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> |
| 2 | <html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"> |
||
| 3 | <title>Procyon AVRlib: stxetx.c Source File</title> |
||
| 4 | <link href="dox.css" rel="stylesheet" type="text/css"> |
||
| 5 | </head><body> |
||
| 6 | <!-- Generated by Doxygen 1.4.2 --> |
||
| 7 | <div class="qindex"><a class="qindex" href="main.html">Main Page</a> | <a class="qindex" href="modules.html">Modules</a> | <a class="qindex" href="annotated.html">Data Structures</a> | <a class="qindex" href="dirs.html">Directories</a> | <a class="qindex" href="files.html">File List</a> | <a class="qindex" href="functions.html">Data Fields</a> | <a class="qindex" href="globals.html">Globals</a> | <a class="qindex" href="pages.html">Related Pages</a></div> |
||
| 8 | <h1>stxetx.c</h1><a href="stxetx_8c.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment">00001 <span class="comment">/*! \file stxetx.c \brief STX/ETX Packet Protocol Implementation Library. */</span> |
||
| 9 | 00002 <span class="comment">//*****************************************************************************</span> |
||
| 10 | 00003 <span class="comment">//</span> |
||
| 11 | 00004 <span class="comment">// File Name : 'stxetx.c'</span> |
||
| 12 | 00005 <span class="comment">// Title : STX/ETX Packet Protocol Implementation Library</span> |
||
| 13 | 00006 <span class="comment">// Author : Pascal Stang - Copyright (C) 2002</span> |
||
| 14 | 00007 <span class="comment">// Created : 10/9/2002</span> |
||
| 15 | 00008 <span class="comment">// Revised : 6/30/2003</span> |
||
| 16 | 00009 <span class="comment">// Version : 0.1</span> |
||
| 17 | 00010 <span class="comment">// Target MCU : any</span> |
||
| 18 | 00011 <span class="comment">// Editor Tabs : 4</span> |
||
| 19 | 00012 <span class="comment">//</span> |
||
| 20 | 00013 <span class="comment">// Description : This library provides a set of functions needed to send and</span> |
||
| 21 | 00014 <span class="comment">// receive STX/ETX packets. STX/ETX is a simple packet protocol that can</span> |
||
| 22 | 00015 <span class="comment">// be wrapped around user data for one or more of the following reasons:</span> |
||
| 23 | 00016 <span class="comment">//</span> |
||
| 24 | 00017 <span class="comment">// 1. packetization is needed</span> |
||
| 25 | 00018 <span class="comment">// - Using packets can be helpful if your data naturally forms </span> |
||
| 26 | 00019 <span class="comment">// little "bunches" or if different types of data must be sent</span> |
||
| 27 | 00020 <span class="comment">// over the same channel (a serial cable, for example). If your</span> |
||
| 28 | 00021 <span class="comment">// data forms "bunches", you can send user data inside STX/ETX</span> |
||
| 29 | 00022 <span class="comment">// packets with a predetermined structure, like an array of A/D</span> |
||
| 30 | 00023 <span class="comment">// conversion results. If you need a way to tell the receiver</span> |
||
| 31 | 00024 <span class="comment">// what kind of data you're sending, you can use the TYPE field</span> |
||
| 32 | 00025 <span class="comment">// in the STX/ETX packet.</span> |
||
| 33 | 00026 <span class="comment">// 2. error checking is needed</span> |
||
| 34 | 00027 <span class="comment">// - STX/ETX packets will add a checksum to your data. This</span> |
||
| 35 | 00028 <span class="comment">// allows the receiver to verify that data was received correctly</span> |
||
| 36 | 00029 <span class="comment">// and is error-free. Packets which are corrupted in transmission</span> |
||
| 37 | 00030 <span class="comment">// and fail the the checksum test are automatically discarded.</span> |
||
| 38 | 00031 <span class="comment">// Error checking is especially useful when the data transmission</span> |
||
| 39 | 00032 <span class="comment">// channel is unreliable or noisy (examples: radio, infrared, long</span> |
||
| 40 | 00033 <span class="comment">// cables, etc)</span> |
||
| 41 | 00034 <span class="comment">// </span> |
||
| 42 | 00035 <span class="comment">// STX/ETX packets have the following structure:</span> |
||
| 43 | 00036 <span class="comment">//</span> |
||
| 44 | 00037 <span class="comment">// [STX][status][type][length][user data...][checksum][ETX]</span> |
||
| 45 | 00038 <span class="comment">//</span> |
||
| 46 | 00039 <span class="comment">// All fields are 1 byte except for user data which may be 0-255 bytes.</span> |
||
| 47 | 00040 <span class="comment">// Uppercase fields are constant (STX=0x02, ETX=0x03), lowercase fields</span> |
||
| 48 | 00041 <span class="comment">// vary. The length field is the number of bytes in the user data area.</span> |
||
| 49 | 00042 <span class="comment">// The checksum is the 8-bit sum of all bytes between but not including</span> |
||
| 50 | 00043 <span class="comment">// STX/ETX.</span> |
||
| 51 | 00044 <span class="comment">//</span> |
||
| 52 | 00045 <span class="comment">// This code is distributed under the GNU Public License</span> |
||
| 53 | 00046 <span class="comment">// which can be found at http://www.gnu.org/licenses/gpl.txt</span> |
||
| 54 | 00047 <span class="comment">//</span> |
||
| 55 | 00048 <span class="comment">//*****************************************************************************</span> |
||
| 56 | 00049 |
||
| 57 | 00050 <span class="preprocessor">#include "<a class="code" href="global_8h.html">global.h</a>"</span> |
||
| 58 | 00051 <span class="preprocessor">#include "<a class="code" href="stxetx_8h.html">stxetx.h</a>"</span> |
||
| 59 | 00052 <span class="comment">//#include "rprintf.h"</span> |
||
| 60 | 00053 |
||
| 61 | 00054 <span class="comment">// function pointer to data output routine</span> |
||
| 62 | 00055 <span class="keyword">static</span> void (*stxetxDataOut)(<span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> data); |
||
| 63 | 00056 |
||
| 64 | 00057 <span class="comment">// received packet data buffer</span> |
||
| 65 | 00058 <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> stxetxRxPacket[STXETX_MAXRXPACKETSIZE]; |
||
| 66 | 00059 |
||
| 67 | 00060 <span class="comment">// functions</span> |
||
| 68 | 00061 |
||
| 69 | 00062 |
||
| 70 | 00063 <span class="comment">// Initialize STX/ETX packet protocol library</span> |
||
| 71 | <a name="l00064"></a><a class="code" href="group__stxetx.html#ga0">00064</a> <span class="keywordtype">void</span> <a class="code" href="group__stxetx.html#ga0">stxetxInit</a>(<span class="keywordtype">void</span> (*dataout_func)(<span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> data)) |
||
| 72 | 00065 { |
||
| 73 | 00066 stxetxDataOut = dataout_func; |
||
| 74 | 00067 } |
||
| 75 | 00068 |
||
| 76 | 00069 <span class="comment">// Send/Create STX/ETX packet</span> |
||
| 77 | <a name="l00070"></a><a class="code" href="group__stxetx.html#ga1">00070</a> <span class="keywordtype">void</span> <a class="code" href="group__stxetx.html#ga1">stxetxSend</a>(<span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> status, <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> type, <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> datalength, <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span>* dataptr) |
||
| 78 | 00071 { |
||
| 79 | 00072 <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> checksum = 0; |
||
| 80 | 00073 <span class="keywordtype">unsigned</span> <span class="keywordtype">short</span> i; |
||
| 81 | 00074 |
||
| 82 | 00075 <span class="comment">// write packet header</span> |
||
| 83 | 00076 stxetxDataOut(STX); |
||
| 84 | 00077 stxetxDataOut(status); |
||
| 85 | 00078 stxetxDataOut(type); |
||
| 86 | 00079 stxetxDataOut(datalength); |
||
| 87 | 00080 <span class="comment">// update checksum</span> |
||
| 88 | 00081 checksum += status + type + datalength; |
||
| 89 | 00082 <span class="comment">// copy data into packet</span> |
||
| 90 | 00083 <span class="keywordflow">for</span>(i = 0; i < datalength; i++) |
||
| 91 | 00084 { |
||
| 92 | 00085 stxetxDataOut(*dataptr); |
||
| 93 | 00086 checksum += *dataptr; |
||
| 94 | 00087 dataptr++; |
||
| 95 | 00088 } |
||
| 96 | 00089 <span class="comment">// write packet trailer</span> |
||
| 97 | 00090 stxetxDataOut(checksum); |
||
| 98 | 00091 stxetxDataOut(ETX); |
||
| 99 | 00092 } |
||
| 100 | 00093 |
||
| 101 | 00094 <span class="comment">// process buffer containing STX/ETX packets</span> |
||
| 102 | <a name="l00095"></a><a class="code" href="group__stxetx.html#ga2">00095</a> <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> <a class="code" href="group__stxetx.html#ga2">stxetxProcess</a>(<a class="code" href="structstruct__cBuffer.html">cBuffer</a>* rxBuffer) |
||
| 103 | 00096 { |
||
| 104 | 00097 <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> foundpacket = FALSE; |
||
| 105 | 00098 <span class="keywordtype">unsigned</span> <span class="keywordtype">short</span> i; |
||
| 106 | 00099 <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> length, checksum; |
||
| 107 | 00100 <span class="comment">//unsigned char type;</span> |
||
| 108 | 00101 |
||
| 109 | 00102 <span class="comment">// process the buffer</span> |
||
| 110 | 00103 <span class="comment">// go through buffer looking for packets</span> |
||
| 111 | 00104 <span class="comment">// the STX must be located at least STXETX_HEADERLENGTH+STXETX_TRAILERLENGTH from end</span> |
||
| 112 | 00105 <span class="comment">// otherwise we must not have a complete packet</span> |
||
| 113 | 00106 <span class="keywordflow">while</span>( rxBuffer-><a class="code" href="structstruct__cBuffer.html#o2">datalength</a> >= ((u16)STXETX_HEADERLENGTH+(u16)STXETX_TRAILERLENGTH) ) |
||
| 114 | 00107 { |
||
| 115 | 00108 <span class="comment">// look for a potential start of packet</span> |
||
| 116 | 00109 <span class="keywordflow">if</span>(<a class="code" href="group__buffer.html#ga4">bufferGetAtIndex</a>(rxBuffer, 0) == STX) |
||
| 117 | 00110 { |
||
| 118 | 00111 <span class="comment">// if this is a start, then get the length</span> |
||
| 119 | 00112 length = <a class="code" href="group__buffer.html#ga4">bufferGetAtIndex</a>(rxBuffer, STXETX_LENGTHOFFSET); |
||
| 120 | 00113 |
||
| 121 | 00114 <span class="comment">// now we must have at least STXETX_HEADERLENGTH+length+STXETX_TRAILERLENGTH in buffer to continue</span> |
||
| 122 | 00115 <span class="keywordflow">if</span>(rxBuffer-><a class="code" href="structstruct__cBuffer.html#o2">datalength</a> >= ((u16)STXETX_HEADERLENGTH+length+(u16)STXETX_TRAILERLENGTH)) |
||
| 123 | 00116 { |
||
| 124 | 00117 <span class="comment">// check to see if ETX is in the right position</span> |
||
| 125 | 00118 <span class="keywordflow">if</span>(<a class="code" href="group__buffer.html#ga4">bufferGetAtIndex</a>(rxBuffer, STXETX_HEADERLENGTH+length+STXETX_TRAILERLENGTH-1) == ETX) |
||
| 126 | 00119 { |
||
| 127 | 00120 <span class="comment">// found potential packet</span> |
||
| 128 | 00121 <span class="comment">// test checksum</span> |
||
| 129 | 00122 checksum = 0; |
||
| 130 | 00123 <span class="comment">// sum data between STX and ETX, not including checksum itself</span> |
||
| 131 | 00124 <span class="comment">// (u16) casting needed to avoid unsigned/signed mismatch</span> |
||
| 132 | 00125 <span class="keywordflow">for</span>(i = 0; i<((u16)STXETX_HEADERLENGTH+length+(u16)STXETX_TRAILERLENGTH-(u16)STXETX_NOETXSTXCHECKSUM); i++) |
||
| 133 | 00126 { |
||
| 134 | 00127 checksum += <a class="code" href="group__buffer.html#ga4">bufferGetAtIndex</a>(rxBuffer, i+STXETX_STATUSOFFSET); |
||
| 135 | 00128 } |
||
| 136 | 00129 <span class="comment">// compare checksums</span> |
||
| 137 | 00130 <span class="keywordflow">if</span>(checksum == <a class="code" href="group__buffer.html#ga4">bufferGetAtIndex</a>(rxBuffer, STXETX_CHECKSUMOFFSET+length)) |
||
| 138 | 00131 { |
||
| 139 | 00132 <span class="comment">//we have a packet!</span> |
||
| 140 | 00133 foundpacket = TRUE; |
||
| 141 | 00134 |
||
| 142 | 00135 <span class="comment">// copy data to buffer</span> |
||
| 143 | 00136 <span class="comment">// (don't copy STX, ETX, or CHECKSUM)</span> |
||
| 144 | 00137 <span class="keywordflow">for</span>(i = 0; i < ((u16)STXETX_HEADERLENGTH+length-1); i++) |
||
| 145 | 00138 { |
||
| 146 | 00139 stxetxRxPacket[i] = <a class="code" href="group__buffer.html#ga4">bufferGetAtIndex</a>(rxBuffer, i+1); |
||
| 147 | 00140 } |
||
| 148 | 00141 |
||
| 149 | 00142 <span class="comment">// debug</span> |
||
| 150 | 00143 <span class="comment">//rprintf("STXETX Received packet type: 0x%x\n", bufferGetAtIndex(rxBuffer, STXETX_TYPEOFFSET));</span> |
||
| 151 | 00144 |
||
| 152 | 00145 <span class="comment">// dump this packet from the</span> |
||
| 153 | 00146 <a class="code" href="group__buffer.html#ga3">bufferDumpFromFront</a>(rxBuffer, STXETX_HEADERLENGTH+length+STXETX_TRAILERLENGTH); |
||
| 154 | 00147 |
||
| 155 | 00148 <span class="comment">// done with this processing session</span> |
||
| 156 | 00149 <span class="keywordflow">break</span>; |
||
| 157 | 00150 } |
||
| 158 | 00151 <span class="keywordflow">else</span> |
||
| 159 | 00152 { |
||
| 160 | 00153 <span class="comment">// checksum bad</span> |
||
| 161 | 00154 <span class="comment">//rprintf("STXETX Received packet with bad checksum\r\n");</span> |
||
| 162 | 00155 <span class="comment">// for now, we dump these</span> |
||
| 163 | 00156 <span class="comment">// dump this STX</span> |
||
| 164 | 00157 <a class="code" href="group__buffer.html#ga2">bufferGetFromFront</a>(rxBuffer); |
||
| 165 | 00158 } |
||
| 166 | 00159 } |
||
| 167 | 00160 <span class="keywordflow">else</span> |
||
| 168 | 00161 { |
||
| 169 | 00162 <span class="comment">// no ETX or ETX in wrong position</span> |
||
| 170 | 00163 <span class="comment">// dump this STX</span> |
||
| 171 | 00164 <a class="code" href="group__buffer.html#ga2">bufferGetFromFront</a>(rxBuffer); |
||
| 172 | 00165 } |
||
| 173 | 00166 } |
||
| 174 | 00167 <span class="keywordflow">else</span> |
||
| 175 | 00168 { |
||
| 176 | 00169 <span class="comment">// not enough data in buffer to decode pending packet</span> |
||
| 177 | 00170 <span class="comment">// wait until next time</span> |
||
| 178 | 00171 <span class="keywordflow">break</span>; |
||
| 179 | 00172 } |
||
| 180 | 00173 } |
||
| 181 | 00174 <span class="keywordflow">else</span> |
||
| 182 | 00175 { |
||
| 183 | 00176 <span class="comment">// this is not a start, dump it</span> |
||
| 184 | 00177 <a class="code" href="group__buffer.html#ga2">bufferGetFromFront</a>(rxBuffer); |
||
| 185 | 00178 } |
||
| 186 | 00179 } |
||
| 187 | 00180 |
||
| 188 | 00181 <span class="comment">// check if receive buffer is full with no packets decoding</span> |
||
| 189 | 00182 <span class="comment">// (ie. deadlocked on garbage data or packet that exceeds buffer size)</span> |
||
| 190 | 00183 <span class="keywordflow">if</span>(!<a class="code" href="group__buffer.html#ga6">bufferIsNotFull</a>(rxBuffer)) |
||
| 191 | 00184 { |
||
| 192 | 00185 <span class="comment">// dump receive buffer contents to relieve deadlock</span> |
||
| 193 | 00186 <a class="code" href="group__buffer.html#ga7">bufferFlush</a>(rxBuffer); |
||
| 194 | 00187 } |
||
| 195 | 00188 |
||
| 196 | 00189 <span class="keywordflow">return</span> foundpacket; |
||
| 197 | 00190 } |
||
| 198 | 00191 |
||
| 199 | <a name="l00192"></a><a class="code" href="group__stxetx.html#ga3">00192</a> <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> <a class="code" href="group__stxetx.html#ga3">stxetxGetRxPacketStatus</a>(<span class="keywordtype">void</span>) |
||
| 200 | 00193 { |
||
| 201 | 00194 <span class="comment">// return the packet's status</span> |
||
| 202 | 00195 <span class="comment">// (subtract 1 from the offset because the STX has already been discarded)</span> |
||
| 203 | 00196 <span class="keywordflow">return</span> stxetxRxPacket[STXETX_STATUSOFFSET-1]; |
||
| 204 | 00197 } |
||
| 205 | 00198 |
||
| 206 | <a name="l00199"></a><a class="code" href="group__stxetx.html#ga4">00199</a> <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> <a class="code" href="group__stxetx.html#ga4">stxetxGetRxPacketType</a>(<span class="keywordtype">void</span>) |
||
| 207 | 00200 { |
||
| 208 | 00201 <span class="comment">// return the packet's type</span> |
||
| 209 | 00202 <span class="comment">// (subtract 1 from the offset because the STX has already been discarded)</span> |
||
| 210 | 00203 <span class="keywordflow">return</span> stxetxRxPacket[STXETX_TYPEOFFSET-1]; |
||
| 211 | 00204 } |
||
| 212 | 00205 |
||
| 213 | <a name="l00206"></a><a class="code" href="group__stxetx.html#ga5">00206</a> <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> <a class="code" href="group__stxetx.html#ga5">stxetxGetRxPacketDatalength</a>(<span class="keywordtype">void</span>) |
||
| 214 | 00207 { |
||
| 215 | 00208 <span class="comment">// return the packet's datalength</span> |
||
| 216 | 00209 <span class="comment">// (subtract 1 from the offset because the STX has already been discarded)</span> |
||
| 217 | 00210 <span class="keywordflow">return</span> stxetxRxPacket[STXETX_LENGTHOFFSET-1]; |
||
| 218 | 00211 } |
||
| 219 | 00212 |
||
| 220 | <a name="l00213"></a><a class="code" href="group__stxetx.html#ga6">00213</a> <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span>* <a class="code" href="group__stxetx.html#ga6">stxetxGetRxPacketData</a>(<span class="keywordtype">void</span>) |
||
| 221 | 00214 { |
||
| 222 | 00215 <span class="comment">// return a pointer to the packet's data payload</span> |
||
| 223 | 00216 <span class="comment">// (subtract 1 from the offset because the STX has already been discarded)</span> |
||
| 224 | 00217 <span class="keywordflow">return</span> stxetxRxPacket+STXETX_DATAOFFSET-1; |
||
| 225 | 00218 } |
||
| 226 | </pre></div><hr size="1"><address style="align: right;"><small>Generated on Sun Oct 29 03:41:07 2006 for Procyon AVRlib by |
||
| 227 | <a href="http://www.doxygen.org/index.html"> |
||
| 228 | <img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.4.2 </small></address> |
||
| 229 | </body> |
||
| 230 | </html> |
Powered by WebSVN v2.8.3