1 //####COPYRIGHTBEGIN####
3 // ----------------------------------------------------------------------------
4 // Copyright (C) 1998, 1999, 2000 Red Hat, Inc.
6 // This program is part of the eCos host tools.
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)
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
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.
22 // ----------------------------------------------------------------------------
24 //####COPYRIGHTEND####
25 //=================================================================
31 //=================================================================
32 //=================================================================
33 //#####DESCRIPTIONBEGIN####
36 // Contributors: jskov
38 // Description: This program acts as a filter between GDB and the test
39 // running on the target, allowing testing of the serial
40 // driver without confusing GDB.
41 // Usage: Run the program with one argument, the serial port
42 // on with the target is connected.
43 // Run serial test in GDB, connecting to the target with
44 // 'target remote localhost:5678'.
47 // o Add timeout setup and handling for recovery, can rely on testing
48 // agent to control global timeout.
49 // o Saving chunks that caused transfer failure?
50 // - In SEND with echo, do CRC on 32-byte sub-packets
51 // o Additional To Do items under each sub-protocol function.
52 // o Option to get all serial IO written (in hex, > and < prepends
53 // input/output lines) to a file.
54 // o Clean up the mess in this file....
55 // o Make main() listen on two ports - use one for ser filter, the other for
56 // null filter connections.
57 // o Maybe use environment variable to find X10 reset command.
58 //####DESCRIPTIONEND####
60 #include "eCosTestSerialFilter.h"
61 #include "eCosTestDownloadFilter.h"
62 #include "eCosTestMonitorFilter.h"
63 #include "eCosTestUtils.h"
64 #include "eCosThreadUtils.h"
65 #include "eCosTrace.h"
67 bool opt_ser_debug = false;
68 bool opt_null_filter = false;
69 bool opt_console_output = false;
70 bool opt_X10_reset = false;
71 bool opt_filter_trace = false;
73 bool opt_monitor = false;
76 no_gdb(const char* pszPort, int nBaud,
77 CeCosSocket::FilterFunc *pSerialToSocketFilterFunc,
78 void *pParam, bool *pbStop)
80 fprintf(stderr, "no_gdb, listening on %s\n",pszPort);
82 CeCosSocket dummy_socket;
84 serial.SetBlockingReads(false);
87 // Open serial device.
88 if (!serial.Open(pszPort,nBaud)){
89 ERROR("Couldn't open port %s\n",pszPort);
91 // Flush the serial buffer.
96 void *pBuf=malloc(BUFSIZE);
98 while(rc && (0==pbStop || !(*pbStop))){
102 if(serial.Read(pBuf,BUFSIZE,nRead)){
106 CeCosThreadUtils::Sleep(1);
108 fprintf(stderr, "Serial read failed (%d)\n", errno);
112 if(pSerialToSocketFilterFunc){
113 rc=pSerialToSocketFilterFunc(pBuf,nRead,serial,dummy_socket,
122 main(int argc, char** argv)
125 int baud_rate, nTcpPort;
127 bool opt_no_gdb = false;
130 if(!CeCosTestUtils::CommandLine(argc, argv, false)){
138 // redundant - -v does this
139 CeCosTrace ::EnableTracing(CeCosTrace::TRACE_LEVEL_TRACE);
142 opt_filter_trace = true;
145 opt_ser_debug = true;
152 // fall through! Output on console when no GDB.
154 opt_console_output = true;
157 opt_null_filter = true;
160 // X-10 port to reset when connection drops
161 opt_X10_reset = true;
162 opt_X10_port[0] = argv[i][2];
163 opt_X10_port[1] = argv[i][3];
166 fprintf(stderr,"Unrecognized switch %s\n",argv[i]);
170 for(int j=i;j<argc;j++){
180 if(!((3==argc && opt_no_gdb) || (4==argc && !opt_no_gdb)))
187 baud_rate=atoi(argv[2]);
189 nTcpPort=atoi(argv[1]);
191 fprintf(stderr,"Invalid port %s\n",argv[1]);
192 return main(0,argv); // Provoke usage message
196 baud_rate=atoi(argv[3]);
198 nSock = CeCosSocket::Listen(nTcpPort);
200 fprintf(stderr, "Couldn't access socket.\n");
201 throw "listen failed";
207 fprintf(stdout, "Monitor mode - will not interact with data streams...\n");
211 CeCosTestMonitorFilter* host_filter =
212 new CeCosTestMonitorFilter();
213 CeCosTestMonitorFilter* target_filter =
214 new CeCosTestMonitorFilter();
216 // Set filter directions
217 host_filter->SetOrigin(CeCosTestMonitorFilter::MF_HOST);
218 target_filter->SetOrigin(CeCosTestMonitorFilter::MF_TARGET);
221 host_filter->SetVerbose(true);
222 target_filter->SetVerbose(true);
224 // Set filter functions
225 CeCosSocket::FilterFunc *host_filter_function =
226 &SerialMonitorFunction;
227 CeCosSocket::FilterFunc *target_filter_function =
228 &SerialMonitorFunction;
231 CeCosSocket::ConnectSocketToSerial(nSock, ser_port,
233 target_filter_function,
234 (void*)target_filter,
235 host_filter_function,
239 catch (const char* p) {
240 fprintf(stderr, "Caught filter crash: %s\n", p);
243 delete target_filter;
249 CeCosTestSerialFilter* serial_filter =
250 new CeCosTestSerialFilter();
251 CeCosTestDownloadFilter* download_filter =
252 new CeCosTestDownloadFilter();
254 // Set filter configuration
255 serial_filter->SetFilterTrace(opt_filter_trace);
256 serial_filter->SetSerialDebug(opt_ser_debug);
257 serial_filter->SetConsoleOutput(opt_console_output);
259 // Set download filter configuration
260 download_filter->SetFilterTrace(opt_filter_trace);
261 download_filter->SetSerialDebug(opt_ser_debug);
263 // Set serial side filter
264 CeCosSocket::FilterFunc *ser_filter_function =
265 &SerialFilterFunction;
267 ser_filter_function = NULL;
269 // Set socket side filter
270 CeCosSocket::FilterFunc *sock_filter_function =
271 &DownloadFilterFunction;
275 no_gdb(ser_port, baud_rate, ser_filter_function,
276 (void*)serial_filter, NULL);
278 CeCosSocket::ConnectSocketToSerial(nSock, ser_port,
281 (void*)serial_filter,
282 sock_filter_function,
283 (void*)download_filter,
286 catch (const char* p) {
287 fprintf(stderr, "Caught filter crash: %s\n", p);
292 sprintf(X10_cmd, "/usr/unsupported/bin/x10cli x10 5000 %c %c",
293 opt_X10_port[0], opt_X10_port[1]);
297 delete serial_filter;
298 delete download_filter;
304 const char *pszMe="ser_filter";
305 fprintf(stderr,"Usage: %s [-c -S -0 -Xab] TcpIPport SerialPort BaudRate\n"
306 " or: %s -n [-c -S -0 -Xab] SerialPort BaudRate\n"
308 " -f: Enable filter output tracing.\n"
309 " -S: Output data read from serial line.\n"
310 " -c: Output data on console instead of via GDB.\n"
311 " -m: Work only as a monitor filter. Implies -c.\n"
313 " -0: Use null filter.\n"
314 " -Xab: Reset X-10 Port 'a b' when TCP connection breaks\n",pszMe,pszMe);
315 CeCosTestUtils::UsageMessage();