Subversion Repositories Projects

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
212 dhylands 1
/****************************************************************************
2
*
3
*     Copyright (c) 2003 Dave Hylands
4
*           All Rights Reserved
5
*
6
*   Permission is granted to any individual or institution to use, copy, or
7
*   redistribute this software so long as it is not sold for profit, and that
8
*   this copyright notice is retained.
9
*
10
****************************************************************************/
11
/**
12
*
13
*   @file   CommServer.cpp
14
*
15
*   @brief  tcpComm server for testing the serial program.
16
*
17
****************************************************************************/
18
 
19
// ---- Include Files -------------------------------------------------------
20
 
21
#define _WIN32_WINNT    0x0400  // Needed for IsDebuggerPresent
22
 
23
#include <windows.h>
24
 
25
#include "Error.h"
26
#include "LogMessage.h"
27
#include "SerialComm.h"
28
#include "TcpComm.h"
29
 
30
// ---- Public Variables ----------------------------------------------------
31
// ---- Private Constants and Types -----------------------------------------
32
// ---- Private Variables ---------------------------------------------------
33
// ---- Private Function Prototypes -----------------------------------------
34
 
35
#if 0
36
#define DBG(x)    LOG_DEBUG(x)
37
#else
38
#define DBG(x)
39
#endif
40
 
41
void LogHandler( LogMessage::Severity sev, const char *msg, const char *file, int lineNum );
42
 
43
int RealMain( int argc, char **argv );
44
 
45
DWORD WINAPI TcpToSerial( LPVOID param );
46
 
47
// ---- Functions -----------------------------------------------------------
48
 
49
/**
50
 * @addtogroup Comm
51
 * @{
52
 */
53
 
54
TcpComm         tcpComm( TcpComm::Server );
55
SerialComm      serComm;
56
 
57
//***************************************************************************
58
/**
59
*   Program entry point. This just wraps the real main for ease of use
60
*       in the debugger.
61
*
62
*   @returns    0 is the program exits successfully, non-zero to indicate
63
*               that an error occurred.
64
*/
65
 
66
int main
67
(
68
    int     argc,   ///< Number of command line arguments.
69
    char  **argv    ///< Array of command line arguments.
70
)
71
{
72
        int rc = RealMain( argc, argv );
73
 
74
    if ( IsDebuggerPresent() )
75
    {
76
        printf( "\nPress RETURN to exit\n" );
77
        getchar();
78
    }
79
 
80
        return rc;
81
}
82
 
83
//***************************************************************************
84
/**
85
*   Main program.
86
*
87
*   @returns    0 is the program exits successfully, non-zero to indicate
88
*               that an error occurred.
89
*/
90
 
91
int RealMain
92
(
93
    int     argc,   ///< Number of command line arguments.
94
    char  **argv    ///< Array of command line arguments.
95
)
96
{
97
    char *serOptions = "";
98
    char *tcpOptions = "";
99
 
100
    LogMessage::SetHandler( LogHandler );
101
 
102
    if ( argc > 1 )
103
    {
104
        serOptions = argv[ 1 ];
105
 
106
        if ( *serOptions == '-' )
107
        {
108
            LOG_ERROR( "Usage: CommServer <serial-options> [<TCP port>]" );
109
            LOG_ERROR( "  where <serial-options> is a single argument which is" );
110
            LOG_ERROR( "  compatible with the BuildCommDCB function, something" );
111
            LOG_ERROR( "  like \"COM1: baud=9600 parity=N data=8 stop=1\"" );
112
            return 1;
113
        }
114
 
115
        if ( argc > 2 )
116
        {
117
            tcpOptions = argv[ 2 ];
118
        }
119
    }
120
 
121
 
122
    if ( !serComm.Open( serOptions ))
123
    {
124
        return 1;
125
    }
126
    LOG_INFO( "Serial Port " << serComm.Name() << " opened " << serComm.ConnectionInfo() );
127
 
128
    while ( true )
129
    {
130
        if ( !tcpComm.Open( tcpOptions ))
131
        {
132
            return 1;
133
        }
134
 
135
        // Start a thread for TcpToSerial
136
 
137
        CreateThread( NULL,
138
                      0,
139
                      TcpToSerial,
140
                      NULL,
141
                      0,
142
                      NULL );
143
 
144
        // Now perform the SerialToTcp functionality
145
 
146
        while ( true )
147
        {
148
            char    buf[ 100 ];
149
            size_t  bytesRead;
150
            size_t  bytesWritten;
151
 
152
                        DBG( "SerialToTcp: Waiting for data from serial port" );
153
 
154
            if ( !serComm.Read( buf, sizeof( buf ), &bytesRead ))
155
            {
156
                break;
157
            }
158
 
159
                DBG( "SerialToTcp: Writing to TCP\n" );
160
 
161
            if ( !tcpComm.Write( buf, bytesRead, &bytesWritten ))
162
            {
163
                break;
164
            }
165
        }
166
                tcpComm.AbortRead();
167
        tcpComm.Close();
168
 
169
        LOG_INFO( "" );
170
    }
171
    serComm.Close();
172
 
173
    DBG( "Exiting CommServer application" );
174
    return 0;
175
 
176
} // main
177
 
178
//***************************************************************************
179
/**
180
*   Handles the log messages.
181
*/
182
void LogHandler( LogMessage::Severity sev, const char *msg, const char *file, int lineNum )
183
{
184
        char    *sevStr;
185
 
186
    switch ( sev )
187
    {
188
        case LogMessage::Info:      sevStr = "INFO";    break;
189
        case LogMessage::Debug:     sevStr = "DEBUG";   break;
190
        case LogMessage::Warning:   sevStr = "WARNING"; break;
191
        case LogMessage::Error:     sevStr = "ERROR";   break;
192
        case LogMessage::Assert:    sevStr = "ASSERT";  break;
193
        case LogMessage::Fatal:     sevStr = "FATAL";   break;
194
        default:                    sevStr = "*****";   break;
195
    }
196
 
197
    fprintf( stderr, "%s: %s\n", sevStr, msg );
198
    fflush( stderr );
199
 
200
} // LogHandler
201
 
202
//***************************************************************************
203
/**
204
*   Read data from the tcp port and write it to the console.
205
*/
206
 
207
DWORD WINAPI TcpToSerial
208
(
209
    LPVOID param    ///< Parameter passed to CreateThread.
210
)
211
{
212
    DBG( "TcpToSerial started" );
213
 
214
    while ( true )
215
    {
216
        size_t  bytesRead;
217
        size_t  bytesWritten;
218
        char    buf[ 1024 ];
219
 
220
        DBG( "TcpToSerial: Waiting for data from TCP" );
221
 
222
        if ( !tcpComm.Read( buf, sizeof( buf ), &bytesRead ))
223
        {
224
            break;
225
        }
226
 
227
                if ( bytesRead == 0 )
228
                {
229
            LOG_INFO( "Connection terminated by " << tcpComm.ConnectionInfo() );
230
                        break;
231
                }
232
 
233
        DBG( "TcpToSerial: Writing to Serial port\n" );
234
 
235
        if ( !serComm.Write( buf, bytesRead, &bytesWritten ))
236
        {
237
            break;
238
        }
239
    }
240
        serComm.AbortRead();
241
 
242
    DBG( "Exiting TcpToSerial" );
243
 
244
        return 0;
245
 
246
} // SerialToConsole
247
 
248
 
249
/** @} */