0
|
1 |
/*
|
|
2 |
* Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
* All rights reserved.
|
|
4 |
* This component and the accompanying materials are made available
|
|
5 |
* under the terms of "Eclipse Public License v1.0"
|
|
6 |
* which accompanies this distribution, and is available
|
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
*
|
|
9 |
* Initial Contributors:
|
|
10 |
* Nokia Corporation - initial contribution.
|
|
11 |
*
|
|
12 |
* Contributors:
|
|
13 |
*
|
|
14 |
* Description:
|
|
15 |
* System Includes
|
|
16 |
*
|
|
17 |
*/
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
#include <stdio.h>
|
|
22 |
#include <stdlib.h>
|
|
23 |
#include <assert.h>
|
|
24 |
#include <string.h>
|
|
25 |
|
|
26 |
|
|
27 |
/*************************************************************************************
|
|
28 |
*
|
|
29 |
* Local Includes
|
|
30 |
*
|
|
31 |
************************************************************************************/
|
|
32 |
#include "CSerialTcpRelay.h"
|
|
33 |
|
|
34 |
|
|
35 |
/*************************************************************************************
|
|
36 |
*
|
|
37 |
* Definitions
|
|
38 |
*
|
|
39 |
************************************************************************************/
|
|
40 |
#define TO_RETRY (1000)
|
|
41 |
#define TO_TIMEWAIT (3000)
|
|
42 |
#define INITIALSTRINGBUFFERSIZE (64)
|
|
43 |
|
|
44 |
|
|
45 |
/*************************************************************************************
|
|
46 |
*
|
|
47 |
* Prototypes
|
|
48 |
*
|
|
49 |
************************************************************************************/
|
|
50 |
void Relay( SOCKADDR_IN aSocketAddress, char *aSerialPortName );
|
|
51 |
void RelaySession( SOCKADDR_IN aSocketAddress, CSerialPort *aSerialPort );
|
|
52 |
void PrintRelayError( TRelayError aRelayError, int aErrCode );
|
|
53 |
|
|
54 |
|
|
55 |
/*************************************************************************************
|
|
56 |
*
|
|
57 |
* main()
|
|
58 |
*
|
|
59 |
************************************************************************************/
|
|
60 |
int main( int argc, char *argv[] )
|
|
61 |
{
|
|
62 |
char *serial_port_name;
|
|
63 |
SOCKADDR_IN socket_address;
|
|
64 |
WORD version;
|
|
65 |
WSADATA wsaData;
|
|
66 |
int err;
|
|
67 |
|
|
68 |
// check params
|
|
69 |
if( argc < 3 ) {
|
|
70 |
fprintf( stderr, "usage: %s <tcp_host> <tcp_port> <serial_port>\n", argv[0] );
|
|
71 |
return -1;
|
|
72 |
}
|
|
73 |
|
|
74 |
// process the params
|
|
75 |
memset( &socket_address, 0, sizeof(socket_address) );
|
|
76 |
socket_address.sin_family = AF_INET;
|
|
77 |
socket_address.sin_port = htons( atoi(argv[2]) );
|
|
78 |
socket_address.sin_addr.S_un.S_addr = inet_addr( argv[1] );
|
|
79 |
serial_port_name = argv[3];
|
|
80 |
|
|
81 |
// start the socket subsystem
|
|
82 |
version = MAKEWORD( 2, 2 );
|
|
83 |
err = WSAStartup( version, &wsaData );
|
|
84 |
assert( err == 0 );
|
|
85 |
|
|
86 |
// start the relay
|
|
87 |
Relay( socket_address, serial_port_name );
|
|
88 |
|
|
89 |
// done
|
|
90 |
WSACleanup();
|
|
91 |
return 0;
|
|
92 |
}
|
|
93 |
|
|
94 |
|
|
95 |
/*************************************************************************************
|
|
96 |
*
|
|
97 |
* Relay()
|
|
98 |
*
|
|
99 |
************************************************************************************/
|
|
100 |
void Relay( SOCKADDR_IN aSocketAddress, char *aSerialPortName )
|
|
101 |
{
|
|
102 |
int err;
|
|
103 |
CSerialPort serial_port;
|
|
104 |
|
|
105 |
// repeat...
|
|
106 |
while( 1 ) {
|
|
107 |
|
|
108 |
// open the serial port - if this fails then pause and retry
|
|
109 |
err = serial_port.OpenPort( aSerialPortName );
|
|
110 |
if( err != 0 ) {
|
|
111 |
fprintf( stderr, "Failed to open serial port %s (%d)\n", aSerialPortName, err );
|
|
112 |
Sleep( TO_RETRY );
|
|
113 |
continue;
|
|
114 |
}
|
|
115 |
|
|
116 |
// try and establish a session (might succeed, might fail, makes no difference here)
|
|
117 |
RelaySession( aSocketAddress, &serial_port );
|
|
118 |
|
|
119 |
// close the port
|
|
120 |
serial_port.ClosePort();
|
|
121 |
}
|
|
122 |
|
|
123 |
// done
|
|
124 |
return;
|
|
125 |
}
|
|
126 |
|
|
127 |
|
|
128 |
/*************************************************************************************
|
|
129 |
*
|
|
130 |
* RelaySession()
|
|
131 |
*
|
|
132 |
************************************************************************************/
|
|
133 |
void RelaySession( SOCKADDR_IN aSocketAddress, CSerialPort *aSerialPort )
|
|
134 |
{
|
|
135 |
char recv_buffer;
|
|
136 |
int err, errcode, bytes_read = 0, slen, initial_string_buffer_length;
|
|
137 |
CSerialTcpRelay relay;
|
|
138 |
TRelayError rerr;
|
|
139 |
|
|
140 |
int current_at_state = 0;
|
|
141 |
char expected_at_char[] = { 'A', 'T', 0x0d, 0 };
|
|
142 |
char initial_string_buffer[INITIALSTRINGBUFFERSIZE] = { 0 };
|
|
143 |
|
|
144 |
// wait for some bytes to arrive on the port
|
|
145 |
fprintf( stdout, "Waiting for new session\n" );
|
|
146 |
while( 1 ) {
|
|
147 |
|
|
148 |
// read from the serial port - if there is an error then exit
|
|
149 |
bytes_read = 1;
|
|
150 |
err = aSerialPort->ReceiveBytes( &recv_buffer, &bytes_read );
|
|
151 |
if( err != 0 ) {
|
|
152 |
fprintf( stderr, "Error reading from the serial port (%s)\n", strerror(err) );
|
|
153 |
break;
|
|
154 |
}
|
|
155 |
|
|
156 |
// if nothing was read then just continue
|
|
157 |
if( bytes_read == 0 ) {
|
|
158 |
continue;
|
|
159 |
}
|
|
160 |
|
|
161 |
// if we read a char that does not match the ignore string then try and
|
|
162 |
// create a session
|
|
163 |
if( recv_buffer != expected_at_char[current_at_state] ) {
|
|
164 |
strncpy( initial_string_buffer, expected_at_char, INITIALSTRINGBUFFERSIZE );
|
|
165 |
initial_string_buffer[current_at_state] = 0;
|
|
166 |
slen = strlen( initial_string_buffer );
|
|
167 |
assert( slen < (INITIALSTRINGBUFFERSIZE-2) );
|
|
168 |
initial_string_buffer[slen] = recv_buffer;
|
|
169 |
initial_string_buffer[slen+1] = 0;
|
|
170 |
initial_string_buffer_length = strlen( initial_string_buffer );
|
|
171 |
break;
|
|
172 |
}
|
|
173 |
|
|
174 |
// otherwise update the at_state
|
|
175 |
current_at_state += 1;
|
|
176 |
current_at_state = ((expected_at_char[current_at_state] == 0) ? 0 : current_at_state);
|
|
177 |
}
|
|
178 |
|
|
179 |
// some data was received - try and start a session
|
|
180 |
rerr = relay.InitialiseRelay( aSerialPort, aSocketAddress, initial_string_buffer, initial_string_buffer_length, &errcode );
|
|
181 |
if( rerr != RE_SUCCESS ) {
|
|
182 |
PrintRelayError( rerr, errcode );
|
|
183 |
return;
|
|
184 |
}
|
|
185 |
fprintf( stdout, "Session established\n" );
|
|
186 |
|
|
187 |
// session established - execute
|
|
188 |
rerr = relay.ExecuteRelay();
|
|
189 |
if( rerr != RE_SUCCESS ) {
|
|
190 |
fprintf( stderr, "ExecuteRelay returned %d\n", rerr );
|
|
191 |
}
|
|
192 |
fprintf( stdout, "Session closed\n" );
|
|
193 |
|
|
194 |
// do time-wait
|
|
195 |
Sleep( TO_TIMEWAIT );
|
|
196 |
}
|
|
197 |
|
|
198 |
|
|
199 |
/*************************************************************************************
|
|
200 |
*
|
|
201 |
* PrintRelayError()
|
|
202 |
*
|
|
203 |
************************************************************************************/
|
|
204 |
void PrintRelayError( TRelayError aRelayError, int aErrCode )
|
|
205 |
{
|
|
206 |
char *relay_error_string[] = { NULL, "socket() failed", "connect() failed", "initial send() failed" };
|
|
207 |
fprintf( stderr, "Failed to initialise the relay - %s - %d\n", relay_error_string[aRelayError], aErrCode );
|
|
208 |
}
|
|
209 |
|
|
210 |
|
|
211 |
|