]> git.karo-electronics.de Git - karo-tx-redboot.git/blob - tools/src/tools/ecostest/common/eCosTestSerialFilter.h
Initial revision
[karo-tx-redboot.git] / tools / src / tools / ecostest / common / eCosTestSerialFilter.h
1 //####COPYRIGHTBEGIN####
2 //                                                                          
3 // ----------------------------------------------------------------------------
4 // Copyright (C) 1998, 1999, 2000 Red Hat, Inc.
5 //
6 // This program is part of the eCos host tools.
7 //
8 // This program is free software; you can redistribute it and/or modify it 
9 // under the terms of the GNU General Public License as published by the Free 
10 // Software Foundation; either version 2 of the License, or (at your option) 
11 // any later version.
12 // 
13 // This program is distributed in the hope that it will be useful, but WITHOUT 
14 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
15 // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for 
16 // more details.
17 // 
18 // You should have received a copy of the GNU General Public License along with
19 // this program; if not, write to the Free Software Foundation, Inc., 
20 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21 //
22 // ----------------------------------------------------------------------------
23 //                                                                          
24 //####COPYRIGHTEND####
25 //=================================================================
26 //
27 //        eCosTestSerialFilter.h
28 //
29 //        Serial test filter class
30 //
31 //=================================================================
32 //=================================================================
33 //#####DESCRIPTIONBEGIN####
34 //
35 // Author(s):     jskov
36 // Contributors:  jskov
37 // Date:          1999-03-01
38 //####DESCRIPTIONEND####
39 #ifndef _CECOSSERIALFILTER_H
40 #define _CECOSSERIALFILTER_H
41
42 #include "eCosStd.h"
43 #include "eCosTest.h"
44 #include "eCosSocket.h"
45 #include "eCosSerial.h"
46
47 //----------------------------------------------------------------------------
48 // Macros to help extract values from the argument string.
49 // Note: This is probably not an ideal solution, but it was easy to make :)
50
51 #define INIT_VALUE(__args)                \
52     unsigned int v;                       \
53     char *__ptr1, *__ptr2 = (__args)      \
54
55 #define SET_VALUE(__type, __slot) {          \
56     __ptr1 = strchr(__ptr2, (int) ':');      \
57     if (*__ptr2 == '\0')                     \
58            (__slot) = (__type)-1;            \
59     else {                                   \
60         if (__ptr1)                          \
61             *__ptr1 = 0;                     \
62         else                                 \
63             __ptr1 = strchr( __ptr2, 0) - 1; \
64         v = atoi(__ptr2);                    \
65         __ptr2 = __ptr1+1;                   \
66         (__slot) = (__type) v;               \
67     }                                        \
68 }
69
70
71 //----------------------------------------------------------------------------
72 // Structures used by the filter.
73 struct filter_abort_t {
74     const unsigned char* data_ptr;
75     int data_len;
76     
77     filter_abort_t():
78         data_ptr(NULL),
79         data_len(0)
80         {}
81 };
82
83 typedef enum {
84     FLOW_NONE=0,
85     FLOW_XONXOFF_RX=1,
86     FLOW_XONXOFF_TX=2,
87     FLOW_RTSCTS_RX=4,
88     FLOW_RTSCTS_TX=8,
89     FLOW_DSRDTR_RX=16,
90     FLOW_DSRDTR_TX=32
91 } flow_cfg_t;
92
93 typedef struct ser_cfg {
94     int baud_rate;
95     int data_bits;
96     CeCosSerial::StopBitsType stop_bits;
97     bool parity;
98     
99     unsigned int flags;
100     // etc...
101 } ser_cfg_t;
102
103 typedef enum {
104     MODE_NO_ECHO = 0,
105     MODE_EOP_ECHO,
106     MODE_DUPLEX_ECHO
107 } cyg_mode_t;
108
109
110 //----------------------------------------------------------------------------
111 // The filter class
112 class CeCosTestSerialFilter;
113
114 class CeCosTestSerialFilter {
115 public:
116     // Constructor
117     CeCosTestSerialFilter();
118     ~CeCosTestSerialFilter();
119
120     // Configuration methods
121     void SetConsoleOutput(bool bConsoleOutput) 
122         { m_bOptConsoleOutput = bConsoleOutput; }
123     void SetSerialDebug(bool bSerialDebug) 
124         { m_bOptSerDebug = bSerialDebug; }
125     void SetFilterTrace(bool bFilterTrace) 
126         { m_bOptFilterTrace = bFilterTrace; }
127
128
129     bool FilterFunctionProper(void*& pBuf,
130                               unsigned int& nRead,
131                               CeCosSerial& serial,
132                               CeCosSocket& socket);
133
134 private:
135     enum {MAX_CMD_LEN=128};
136     enum data_origin_t {SF_TARGET=0, SF_FILTER} ;
137     
138     // Output methods
139     void GDBWrite(const char* pszStr);
140     void ConsoleWrite(const char* pszStr);
141     void Trace(const char* pszFormat, ...);
142     void Log(const char* pszFormat, ...);
143
144     void PrintHex(const unsigned char* d1, int len, 
145                   data_origin_t origin=SF_TARGET);
146
147     // Target read/write methods
148     void TargetWrite(CeCosSerial &pSer, 
149                      const unsigned char* buffer, int len);
150     void TargetASCIIWrite(CeCosSerial &pSer, const char* s);
151     bool TargetRead(CeCosSerial &pSer, 
152                     unsigned char* buffer, int len);
153         
154     // Configuration CMD and helper methods
155     void ParseConfig(char* args, ser_cfg_t* new_cfg);
156     bool SetConfig(CeCosSerial &pSer, const ser_cfg_t* new_cfg, 
157                    ser_cfg_t* old_cfg);
158     bool VerifyConfig(CeCosSerial &pSer, ser_cfg_t* new_cfg);
159     void CMD_ChangeConfig(CeCosSerial &pSer, char* cfg_str);
160     void CMD_DefaultConfig(CeCosSerial &pSer);
161
162     // Other CMD methods.
163     void CMD_TestBinary(CeCosSerial &pSer, char* args);
164     void CMD_TestText(CeCosSerial &pSer, char* args);
165     void CMD_TestPing(CeCosSerial &pSer, char* args);
166
167
168     // Misc helper methods
169     int DoCRC(unsigned char* data, int size);
170     void SendChecksum(CeCosSerial &pSer, int crc);
171     void SendStatus(CeCosSerial &pSer, int state);
172     void ReceiveDone(CeCosSerial &pSer, unsigned char* data_in, int size);
173     void DispatchCommand(CeCosSerial &pSer, char* cmd);
174
175     // Options used for configuring behavior.
176     bool m_bOptConsoleOutput;
177     bool m_bOptSerDebug;
178     bool m_bOptFilterTrace;
179
180     // Buffer holding unread bytes.
181     unsigned char* m_xUnreadBuffer;     // unread_buffer;
182     int m_nUnreadBufferIndex;           // unread_buffer_ix;
183     int m_nUnreadBufferSize;            // unread_buffer_size = 0;
184
185     unsigned char* m_xStoredTraceBuffer;// We need this to avoid outputting
186                                         // serial tracing when the target
187                                         // last sent an incomplete packet, so
188                                         // we store it here temporarily until
189                                         // the entire packet arrives
190     unsigned int m_nStoredTraceBufferSize; // size of above
191
192     // Filter state
193     bool m_bNullFilter;
194     int  m_nCmdIndex;
195     bool m_bCmdFlag;
196     char m_aCmd[MAX_CMD_LEN];
197     bool m_bFirstCommandSeen;           // We need this to avoid outputting
198                                         // serial tracing while GDB is trying
199                                         // to connect, or it will get confused.
200
201     CeCosSocket* m_cGDBSocket;      // gdb_socket
202 };
203
204 extern bool CALLBACK SerialFilterFunction(void*& pBuf,
205                                           unsigned int& nRead,
206                                           CeCosSerial& serial,
207                                           CeCosSocket& socket,
208                                           void* pParem);
209     
210 #endif // _CECOSSERIALFILTER_H