Rev 907 | Rev 974 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 907 | Rev 935 | ||
|---|---|---|---|
| 1 | #!/usr/bin/perl |
1 | #!/usr/bin/perl |
| 2 | 2 | ||
| 3 | # |
3 | # |
| 4 | # for use with serial ports, install module with apt-get install libdevice-serialport-perl |
4 | # for use with serial ports, install module with apt-get install libdevice-serialport-perl |
| 5 | # |
5 | # |
| 6 | # |
6 | # |
| 7 | # |
7 | # |
| 8 | 8 | ||
| 9 | 9 | ||
| 10 | $hubport = 1200; |
10 | $hubport = 1200; |
| 11 | #$udpip = "193.11.254.22"; |
11 | #$udpip = "193.11.254.22"; |
| 12 | #$udpport = 1100; |
12 | #$udpport = 1100; |
| 13 | 13 | ||
| 14 | 14 | ||
| 15 | require 5.002; |
15 | require 5.002; |
| 16 | use IO::Socket; |
16 | use IO::Socket; |
| 17 | use IO::Select; |
17 | use IO::Select; |
| 18 | use threads; |
18 | use threads; |
| 19 | use Getopt::Long; |
19 | use Getopt::Long; |
| 20 | 20 | ||
| 21 | use POSIX qw(setsid); |
21 | use POSIX qw(setsid); |
| 22 | 22 | ||
| 23 | $binaryname = "canDaemon.pl"; |
23 | $binaryname = "canDaemon.pl"; |
| 24 | if ( @ARGV > 0 ) { |
24 | if ( @ARGV > 0 ) { |
| 25 | GetOptions( "baudrate=i" => \$baudarg, # |
25 | GetOptions( "baudrate=i" => \$baudarg, # |
| 26 | "server=s" => \$serverarg, # |
26 | "server=s" => \$serverarg, # |
| 27 | "port=i" => \$portarg, # |
27 | "port=i" => \$portarg, # |
| 28 | "tcpport=i" => \$tcpportarg, # |
28 | "tcpport=i" => \$tcpportarg, # |
| 29 | "device=s" => \$devicearg, # |
29 | "device=s" => \$devicearg, # |
| 30 | "help" => \$help, # |
30 | "help" => \$help, # |
| 31 | "xDaemon" => \$daemon, # |
31 | "xDaemon" => \$daemon, # |
| 32 | ); |
32 | ); |
| 33 | } else { |
33 | } else { |
| 34 | $help = 1; |
34 | $help = 1; |
| 35 | } |
35 | } |
| 36 | 36 | ||
| 37 | if ($help) { |
37 | if ($help) { |
| 38 | print "Usage ./$binaryname [options]\n"; |
38 | print "Usage ./$binaryname [options]\n"; |
| 39 | print "Options:\n"; |
39 | print "Options:\n"; |
| 40 | print " -x Make canDaemon detach as a UNIX daemon\n"; |
40 | print " -x Make canDaemon detach as a UNIX daemon\n"; |
| 41 | print " -d <device> (udp or /dev/ttyXYZ) Choose hardware for communication\n"; |
41 | print " -d <device> (udp or /dev/ttyXYZ) Choose hardware for communication\n"; |
| 42 | print " -b <baudrate> Baudrate, for serial devices\n"; |
42 | print " -b <baudrate> Baudrate, for serial devices\n"; |
| 43 | print " -s <server> Server, for udp devices\n"; |
43 | print " -s <server> Server, for udp devices\n"; |
| 44 | print " -p <port> Port, for udp devices\n"; |
44 | print " -p <port> Port, for udp devices\n"; |
| 45 | print " -t <tcpport> Port, for tcp server (default 1200)\n"; |
45 | print " -t <tcpport> Port, for tcp server (default 1200)\n"; |
| 46 | print " -h Shows this usage\n"; |
46 | print " -h Shows this usage\n"; |
| 47 | print "\n"; |
47 | print "\n"; |
| 48 | print "example: ./$binaryname -d /dev/ttyUSB0 -b 38400\n"; |
48 | print "example: ./$binaryname -d /dev/ttyUSB0 -b 38400\n"; |
| 49 | print "example: ./$binaryname -d udp -s 192.168.0.10 -p 1100\n"; |
49 | print "example: ./$binaryname -d udp -s 192.168.0.10 -p 1100\n"; |
| 50 | 50 | ||
| 51 | exit 0; |
51 | exit 0; |
| 52 | } |
52 | } |
| 53 | if ($serverarg) { |
53 | if ($serverarg) { |
| 54 | $udpip = $serverarg; |
54 | $udpip = $serverarg; |
| 55 | } |
55 | } |
| 56 | #print $udpip; |
56 | #print $udpip; |
| 57 | if ($portarg) { |
57 | if ($portarg) { |
| 58 | $udpport = $portarg; |
58 | $udpport = $portarg; |
| 59 | } |
59 | } |
| 60 | if ($tcpportarg) { |
60 | if ($tcpportarg) { |
| 61 | $hubport = $tcpportarg; |
61 | $hubport = $tcpportarg; |
| 62 | } |
62 | } |
| 63 | 63 | ||
| 64 | if ($daemon) { |
64 | if ($daemon) { |
| 65 | chdir '/' or die "Can't chdir to /: $!"; |
65 | chdir '/' or die "Can't chdir to /: $!"; |
| 66 | umask 0; |
66 | umask 0; |
| 67 | open STDIN, '/dev/null' or die "Can't read /dev/null: $!"; |
67 | open STDIN, '/dev/null' or die "Can't read /dev/null: $!"; |
| 68 | open STDOUT, '>/dev/null' or die "Can't write to /dev/null: $!"; |
68 | open STDOUT, '>/dev/null' or die "Can't write to /dev/null: $!"; |
| 69 | #open STDERR, '>/dev/null' or die "Can't write to /dev/null: $!"; |
69 | #open STDERR, '>/dev/null' or die "Can't write to /dev/null: $!"; |
| 70 | defined(my $pid = fork) or die "Can't fork: $!"; |
70 | defined(my $pid = fork) or die "Can't fork: $!"; |
| 71 | exit if $pid; |
71 | exit if $pid; |
| 72 | setsid or die "Can't start a new session: $!"; |
72 | setsid or die "Can't start a new session: $!"; |
| 73 | } |
73 | } |
| 74 | 74 | ||
| 75 | $C2SSTARTSTR = 253; |
75 | $C2SSTARTSTR = 253; |
| 76 | $C2SENDSTR = 250; |
76 | $C2SENDSTR = 250; |
| 77 | $C2PINGSTR = 251; |
77 | $C2PINGSTR = 251; |
| 78 | 78 | ||
| 79 | ### create a tcp server ### |
79 | ### create a tcp server ### |
| 80 | $linelength=1000; |
80 | $linelength=1000; |
| 81 | &tcpServerInit; |
81 | &tcpServerInit; |
| 82 | $thr = threads->new(\&tcpServerThread); |
82 | $thr = threads->new(\&tcpServerThread); |
| 83 | $thr->detach; |
83 | $thr->detach; |
| 84 | 84 | ||
| 85 | ### create a connection to hardware ### |
85 | ### create a connection to hardware ### |
| 86 | if ($devicearg) { |
86 | if ($devicearg) { |
| 87 | if ($devicearg =~ m/^udp$/) { |
87 | if ($devicearg =~ m/^udp$/) { |
| 88 | ### if the hardware is an udp node ### |
88 | ### if the hardware is an udp node ### |
| 89 | &udpServerInit; |
89 | &udpServerInit; |
| 90 | $thr = threads->new(\&udpServerThread); |
90 | $thr = threads->new(\&udpServerThread); |
| 91 | $thr->detach; |
91 | $thr->detach; |
| 92 | } |
92 | } |
| 93 | ### else, the hardware is serial, check for device, this might not work in windows ### |
93 | ### else, the hardware is serial, check for device, this might not work in windows ### |
| 94 | # elsif (-e $devicearg) { |
94 | # elsif (-e $devicearg) { |
| 95 | else { |
95 | else { |
| 96 | if (!$baudarg) { |
96 | if (!$baudarg) { |
| 97 | print "For a serial device you need to specifiy baudrate, now using default 19200\n"; |
97 | print "For a serial device you need to specifiy baudrate, now using default 19200\n"; |
| 98 | $baudarg = 19200; |
98 | $baudarg = 19200; |
| 99 | } |
99 | } |
| 100 | 100 | ||
| 101 | eval 'use Device::SerialPort'; ### install module with aptitude install libdevice-serialport-perl |
101 | eval 'use Device::SerialPort'; ### install module with aptitude install libdevice-serialport-perl |
| 102 | if ($@) { |
102 | if ($@) { |
| 103 | eval 'use Win32::SerialPort'; |
103 | eval 'use Win32::SerialPort'; |
| 104 | if ($@) { |
104 | if ($@) { |
| 105 | print "Could not find Device::SerialPort or Win32::SerialPort, I quit.\n"; |
105 | print "Could not find Device::SerialPort or Win32::SerialPort, I quit.\n"; |
| 106 | exit 0; |
106 | exit 0; |
| 107 | } else { |
107 | } else { |
| 108 | $usingWin32Serial = 1; |
108 | $usingWin32Serial = 1; |
| 109 | } |
109 | } |
| 110 | } else { |
110 | } else { |
| 111 | $usingDeviceSerial = 1; |
111 | $usingDeviceSerial = 1; |
| 112 | } |
112 | } |
| 113 | 113 | ||
| 114 | &serialConnInit; |
114 | &serialConnInit; |
| 115 | $thr = threads->new(\&serialConnThread); |
115 | $thr = threads->new(\&serialConnThread); |
| 116 | $thr->detach; |
116 | $thr->detach; |
| 117 | } |
117 | } |
| 118 | # } else { |
118 | # } else { |
| 119 | # print "Argument $devicearg is not correct\n"; |
119 | # print "Argument $devicearg is not correct\n"; |
| 120 | # exit 0; |
120 | # exit 0; |
| 121 | # } |
121 | # } |
| 122 | } |
122 | } |
| 123 | 123 | ||
| 124 | 124 | ||
| 125 | ### loop here until user aborts with ctrl+c ### |
125 | ### loop here until user aborts with ctrl+c ### |
| 126 | while (1) { sleep 1; } |
126 | while (1) { sleep 1; } |
| 127 | 127 | ||
| 128 | 128 | ||
| 129 | ##################### Hardware connection to TCP HUB ############################ |
129 | ##################### Hardware connection to TCP HUB ############################ |
| 130 | sub hardwareConnThread { |
130 | sub hardwareConnThread { |
| 131 | ### Check for data from HUB ### |
131 | ### Check for data from HUB ### |
| 132 | while ( $newmsg = <$hwCremote> ) { |
132 | while ( $newmsg = <$hwCremote> ) { |
| 133 | ### Convert to can2serial format ### |
133 | ### Convert to can2serial format ### |
| 134 | &stringToc2s($newmsg, $retstring); |
134 | &stringToc2s($newmsg, $retstring); |
| 135 | if ($retstring) { |
135 | if ($retstring) { |
| 136 | #print length($retstring)."\n"; |
136 | #print length($retstring)."\n"; |
| 137 | #&c2sToString($retstring, $newretstring); |
137 | #&c2sToString($retstring, $newretstring); |
| 138 | #print "TO SEND: ".$newretstring."\n"; |
138 | #print "TO SEND: ".$newretstring."\n"; |
| 139 | #print $udpCsocket $retstring; |
139 | #print $udpCsocket $retstring; |
| 140 | 140 | ||
| 141 | ### Send data ### |
141 | ### Send data ### |
| 142 | if (length($hardwareSendFunction) > 0) { |
142 | if (length($hardwareSendFunction) > 0) { |
| 143 | &{$hardwareSendFunction}($retstring); |
143 | &{$hardwareSendFunction}($retstring); |
| 144 | } |
144 | } |
| 145 | 145 | ||
| 146 | } |
146 | } |
| 147 | } |
147 | } |
| 148 | } |
148 | } |
| 149 | 149 | ||
| 150 | sub hardwareConnInit { |
150 | sub hardwareConnInit { |
| 151 | print "Connecting hardware to tcpServer\n"; |
151 | print "Connecting hardware to tcpServer\n"; |
| 152 | $hwCremote = IO::Socket::INET->new(Proto => "tcp", PeerAddr => "localhost", PeerPort => $hubport,) |
152 | $hwCremote = IO::Socket::INET->new(Proto => "tcp", PeerAddr => "localhost", PeerPort => $hubport,) |
| 153 | or die "cannot connect to $hubport port at localhost"; |
153 | or die "cannot connect to $hubport port at localhost"; |
| 154 | } |
154 | } |
| 155 | 155 | ||
| 156 | ##################### Serial Connection ############################ |
156 | ##################### Serial Connection ############################ |
| 157 | sub serialConnThread { |
157 | sub serialConnThread { |
| 158 | $serialBuffer = ""; |
158 | $serialBuffer = ""; |
| 159 | while(1) { |
159 | while(1) { |
| 160 | my ($count,$saw)=$serialPort->read(1); # will read _up to_ 1 chars |
160 | my ($count,$saw)=$serialPort->read(1); # will read _up to_ 1 chars |
| 161 | if ($count > 0) { |
161 | if ($count > 0) { |
| 162 | $serialBuffer.=$saw; |
162 | $serialBuffer.=$saw; |
| 163 | serialConnProcessBuffer($serialBuffer); |
163 | serialConnProcessBuffer($serialBuffer); |
| 164 | } |
164 | } |
| 165 | } |
165 | } |
| 166 | } |
166 | } |
| 167 | 167 | ||
| 168 | sub serialConnProcessBuffer { |
168 | sub serialConnProcessBuffer { |
| 169 | my $input = $_[0]; |
169 | my $input = $_[0]; |
| 170 | my $output = $input; |
170 | my $output = $input; |
| 171 | my $newmsg; |
171 | my $newmsg; |
| 172 | my $retstring; |
172 | my $retstring; |
| 173 | 173 | ||
| 174 | ### Cut all leading chars that is not a start-char ### |
174 | ### Cut all leading chars that is not a start-char ### |
| 175 | for ($i = 0; $i < length($input); $i++) { |
175 | for ($i = 0; $i < length($input); $i++) { |
| - | 176 | if (ord(substr($input, 0, 1)) != $C2SSTARTSTR) { |
|
| 176 | if (ord(substr($input, $i, 1)) == $C2SSTARTSTR) { |
177 | if (ord(substr($input, $i, 1)) == $C2SSTARTSTR) { |
| 177 | $input = substr($input, $i); |
178 | $input = substr($input, $i); |
| 178 | $output = $input; |
179 | $output = $input; |
| 179 | #print "debug: cutting character\n"; |
180 | #print "debug: cutting character\n"; |
| 180 | break; |
181 | break; |
| 181 | } |
182 | } |
| 182 | if (ord(substr($input, $i, 1)) == $C2PINGSTR) { |
183 | if (ord(substr($input, $i, 1)) == $C2PINGSTR) { |
| 183 | print "Got pong from hardware\n"; |
184 | print "Got pong from hardware\n"; |
| 184 | $input = substr($input, $i+1); |
185 | $input = substr($input, $i+1); |
| 185 | $output = $input; |
186 | $output = $input; |
| - | 187 | } |
|
| - | 188 | } else { |
|
| - | 189 | break; |
|
| 186 | } |
190 | } |
| 187 | } |
191 | } |
| 188 | ### Check for possible complete packets ### |
192 | ### Check for possible complete packets ### |
| 189 | for ($i = 0; $i < length($input)-16; $i++) { |
193 | for ($i = 0; $i < length($input)-16; $i++) { |
| 190 | my $newmsg = substr($input, $i, 17); |
194 | my $newmsg = substr($input, $i, 17); |
| 191 | $retstring = ""; |
195 | $retstring = ""; |
| 192 | &c2sToString($newmsg, $retstring); |
196 | &c2sToString($newmsg, $retstring); |
| 193 | if (length($retstring)>0) { |
197 | if (length($retstring)>0) { |
| 194 | if ($hwCremote) { |
198 | if ($hwCremote) { |
| 195 | ### Send to tcp hub ### |
199 | ### Send to tcp hub ### |
| 196 | print $hwCremote $retstring . "\n"; |
200 | print $hwCremote $retstring . "\n"; |
| 197 | } |
201 | } |
| 198 | #print $retstring."\n"; |
202 | #print $retstring."\n"; |
| 199 | $output = substr($input, $i+17); |
203 | $output = substr($input, $i+17); |
| 200 | $i+=16; |
204 | $i+=16; |
| 201 | } |
205 | } |
| 202 | } |
206 | } |
| 203 | 207 | ||
| 204 | ### Store modified buffer ### |
208 | ### Store modified buffer ### |
| 205 | $_[0] = $output; |
209 | $_[0] = $output; |
| 206 | } |
210 | } |
| 207 | 211 | ||
| 208 | sub serialConnInit { |
212 | sub serialConnInit { |
| 209 | print "Connecting to hardware over serial port\n"; |
213 | print "Connecting to hardware over serial port\n"; |
| 210 | $quiet=0; |
214 | $quiet=0; |
| 211 | if ($usingDeviceSerial == 1) { |
215 | if ($usingDeviceSerial == 1) { |
| 212 | $serialPort = new Device::SerialPort($devicearg, $quiet) || die "failed to open serial port, $devicearg is not valid?"; |
216 | $serialPort = new Device::SerialPort($devicearg, $quiet) || die "failed to open serial port, $devicearg is not valid?"; |
| 213 | } elsif ($usingWin32Serial == 1) { |
217 | } elsif ($usingWin32Serial == 1) { |
| 214 | $serialPort = new Win32::SerialPort($devicearg, $quiet) || die "failed to open serial port, $devicearg is not valid?"; |
218 | $serialPort = new Win32::SerialPort($devicearg, $quiet) || die "failed to open serial port, $devicearg is not valid?"; |
| 215 | } |
219 | } |
| 216 | $serialPort->databits(8) || die "failed setting databits"; |
220 | $serialPort->databits(8) || die "failed setting databits"; |
| 217 | $serialPort->baudrate($baudarg) || die "failed setting baudrate"; |
221 | $serialPort->baudrate($baudarg) || die "failed setting baudrate"; |
| 218 | $serialPort->parity("none") || die "failed setting parity"; |
222 | $serialPort->parity("none") || die "failed setting parity"; |
| 219 | $serialPort->stopbits(1) || die "failed setting stopbits"; |
223 | $serialPort->stopbits(1) || die "failed setting stopbits"; |
| 220 | $serialPort->handshake("none") || die "failed setting handshake"; |
224 | $serialPort->handshake("none") || die "failed setting handshake"; |
| 221 | $serialPort->write_settings || die "no settings"; |
225 | $serialPort->write_settings || die "no settings"; |
| 222 | 226 | ||
| 223 | $serialPort->read_char_time(0); # don't wait for each character |
227 | $serialPort->read_char_time(0); # don't wait for each character |
| 224 | $serialPort->read_const_time(1000); # 1 second per unfulfilled "read" call |
228 | $serialPort->read_const_time(1000); # 1 second per unfulfilled "read" call |
| 225 | 229 | ||
| 226 | print "Testing connection to hardware: ping (hardware should answer with a pong)\n"; |
230 | print "Testing connection to hardware: ping (hardware should answer with a pong)\n"; |
| 227 | serialConnSend(chr($C2PINGSTR)); |
231 | serialConnSend(chr($C2PINGSTR)); |
| 228 | 232 | ||
| 229 | ### create a connection between the hardware and the tcp-server ### |
233 | ### create a connection between the hardware and the tcp-server ### |
| 230 | &hardwareConnInit; |
234 | &hardwareConnInit; |
| 231 | $hardwareSendFunction = "serialConnSend"; ### set up callback for a 'send data' function |
235 | $hardwareSendFunction = "serialConnSend"; ### set up callback for a 'send data' function |
| 232 | $thr = threads->new(\&hardwareConnThread); |
236 | $thr = threads->new(\&hardwareConnThread); |
| 233 | $thr->detach; |
237 | $thr->detach; |
| 234 | } |
238 | } |
| 235 | 239 | ||
| 236 | sub serialConnSend { |
240 | sub serialConnSend { |
| 237 | $cnt = $serialPort->write($_[0]); |
241 | $cnt = $serialPort->write($_[0]); |
| 238 | } |
242 | } |
| 239 | 243 | ||
| 240 | ##################### UDP Connection ############################ |
244 | ##################### UDP Connection ############################ |
| 241 | sub udpServerThread { |
245 | sub udpServerThread { |
| 242 | $MAXLEN = 1024; |
246 | $MAXLEN = 1024; |
| 243 | my $newmsg; |
247 | my $newmsg; |
| 244 | my $retstring; |
248 | my $retstring; |
| 245 | while ($udpSsocket->recv($newmsg, $MAXLEN)) { |
249 | while ($udpSsocket->recv($newmsg, $MAXLEN)) { |
| 246 | my($udpport, $ipaddr) = sockaddr_in($udpSsocket->peername); |
250 | my($udpport, $ipaddr) = sockaddr_in($udpSsocket->peername); |
| 247 | $peer_addr = inet_ntoa($ipaddr); |
251 | $peer_addr = inet_ntoa($ipaddr); |
| 248 | 252 | ||
| 249 | ### check ipaddr to ip in commandlinearg ### |
253 | ### check ipaddr to ip in commandlinearg ### |
| 250 | if ($udpip == $peer_addr) { |
254 | if ($udpip == $peer_addr) { |
| 251 | #check len of incomming data |
255 | #check len of incomming data |
| 252 | if (length($newmsg) > 16 && length($newmsg) < 20) { |
256 | if (length($newmsg) > 16 && length($newmsg) < 20) { |
| 253 | $retstring = ""; |
257 | $retstring = ""; |
| 254 | &c2sToString($newmsg, $retstring); |
258 | &c2sToString($newmsg, $retstring); |
| 255 | if (length($retstring) > 0 && $hwCremote) { |
259 | if (length($retstring) > 0 && $hwCremote) { |
| 256 | print $hwCremote $retstring . "\n"; |
260 | print $hwCremote $retstring . "\n"; |
| 257 | } |
261 | } |
| 258 | } |
262 | } |
| 259 | } |
263 | } |
| 260 | } |
264 | } |
| 261 | 265 | ||
| 262 | } |
266 | } |
| 263 | 267 | ||
| 264 | sub udpServerInit { |
268 | sub udpServerInit { |
| 265 | print "Starting udpServer\n"; |
269 | print "Starting udpServer\n"; |
| 266 | $udpSsocket = IO::Socket::INET->new(LocalPort => $udpport, Proto => 'udp') |
270 | $udpSsocket = IO::Socket::INET->new(LocalPort => $udpport, Proto => 'udp') |
| 267 | or die "socket: $@"; |
271 | or die "socket: $@"; |
| 268 | 272 | ||
| 269 | $udpCsocket = IO::Socket::INET->new(PeerPort => $udpport, Proto => 'udp', PeerAddr => $udpip) |
273 | $udpCsocket = IO::Socket::INET->new(PeerPort => $udpport, Proto => 'udp', PeerAddr => $udpip) |
| 270 | or die "socket: $@"; |
274 | or die "socket: $@"; |
| 271 | 275 | ||
| 272 | #här ska keep-alive/start-paket skickas till ipt enligt commandline-arg |
276 | #här ska keep-alive/start-paket skickas till ipt enligt commandline-arg |
| 273 | 277 | ||
| 274 | ### create a connection between the hardware and the tcp-server ### |
278 | ### create a connection between the hardware and the tcp-server ### |
| 275 | &hardwareConnInit; |
279 | &hardwareConnInit; |
| 276 | $hardwareSendFunction = "udpServerSend"; ### set up callback for a 'send data' function |
280 | $hardwareSendFunction = "udpServerSend"; ### set up callback for a 'send data' function |
| 277 | $thr = threads->new(\&hardwareConnThread); |
281 | $thr = threads->new(\&hardwareConnThread); |
| 278 | $thr->detach; |
282 | $thr->detach; |
| 279 | 283 | ||
| 280 | } |
284 | } |
| 281 | 285 | ||
| 282 | sub udpServerSend { |
286 | sub udpServerSend { |
| 283 | print $udpCsocket $_[0]; |
287 | print $udpCsocket $_[0]; |
| 284 | } |
288 | } |
| 285 | 289 | ||
| 286 | ##################### TCP HUB ############################ |
290 | ##################### TCP HUB ############################ |
| 287 | sub tcpServerThread { |
291 | sub tcpServerThread { |
| 288 | while(@ready = $tcpSselect->can_read) { |
292 | while(@ready = $tcpSselect->can_read) { |
| 289 | for $tcpSsocket (@ready) { |
293 | for $tcpSsocket (@ready) { |
| 290 | if($tcpSsocket == $tcpSlisten) { |
294 | if($tcpSsocket == $tcpSlisten) { |
| 291 | &tcpServerConn; |
295 | &tcpServerConn; |
| 292 | } else { |
296 | } else { |
| 293 | $tcpSsocket->recv($line,$linelength); |
297 | $tcpSsocket->recv($line,$linelength); |
| 294 | if ($line eq "") { |
298 | if ($line eq "") { |
| 295 | &tcpServerClientDis; |
299 | &tcpServerClientDis; |
| 296 | } else { |
300 | } else { |
| 297 | $line =~ s/\n.*//gm; #remove newline and everything after it |
301 | $line =~ s/\n.*//gm; #remove newline and everything after it |
| 298 | $line =~ s/\r.*//gm; #remove linefeed and everything after it |
302 | $line =~ s/\r.*//gm; #remove linefeed and everything after it |
| 299 | print "Got packet '".$line."' from client ".$tcpSsocket->fileno."\n"; |
303 | print "Got packet '".$line."' from client ".$tcpSsocket->fileno."\n"; |
| 300 | 304 | ||
| 301 | $rxerror = 0; |
305 | $rxerror = 0; |
| 302 | testPacket($line, 0, $rxerror); |
306 | testPacket($line, 0, $rxerror); |
| 303 | 307 | ||
| 304 | if ($rxerror == 0) { |
308 | if ($rxerror == 0) { |
| 305 | #Add newline to each packet |
309 | #Add newline to each packet |
| 306 | $line .= "\n"; |
310 | $line .= "\n"; |
| 307 | 311 | ||
| 308 | &tcpServerBroadcastExcept($line, $tcpSsocket); |
312 | &tcpServerBroadcastExcept($line, $tcpSsocket); |
| 309 | } |
313 | } |
| 310 | } |
314 | } |
| 311 | } |
315 | } |
| 312 | } |
316 | } |
| 313 | } |
317 | } |
| 314 | } |
318 | } |
| 315 | 319 | ||
| 316 | #initiate the tcp server |
320 | #initiate the tcp server |
| 317 | sub tcpServerInit { |
321 | sub tcpServerInit { |
| 318 | $tcpSlisten = IO::Socket::INET->new(Proto => "tcp", LocalPort => $hubport, Listen => 1, Reuse => 1) |
322 | $tcpSlisten = IO::Socket::INET->new(Proto => "tcp", LocalPort => $hubport, Listen => 1, Reuse => 1) |
| 319 | or die $!; |
323 | or die $!; |
| 320 | 324 | ||
| 321 | $tcpSselect = IO::Select->new($tcpSlisten); |
325 | $tcpSselect = IO::Select->new($tcpSlisten); |
| 322 | print "Starting tcpServer\n"; |
326 | print "Starting tcpServer\n"; |
| 323 | } |
327 | } |
| 324 | 328 | ||
| 325 | #a client connected |
329 | #a client connected |
| 326 | sub tcpServerConn { |
330 | sub tcpServerConn { |
| 327 | $new = $tcpSlisten->accept; |
331 | $new = $tcpSlisten->accept; |
| 328 | $tcpSselect->add($new); |
332 | $tcpSselect->add($new); |
| 329 | print $new->fileno . ": connected\n"; |
333 | print $new->fileno . ": connected\n"; |
| 330 | } |
334 | } |
| 331 | 335 | ||
| 332 | #a client disconnected |
336 | #a client disconnected |
| 333 | sub tcpServerClientDis { |
337 | sub tcpServerClientDis { |
| 334 | print $tcpSsocket->fileno . ": disconnected\n"; |
338 | print $tcpSsocket->fileno . ": disconnected\n"; |
| 335 | $tcpSselect->remove($tcpSsocket); |
339 | $tcpSselect->remove($tcpSsocket); |
| 336 | $tcpSsocket->close; |
340 | $tcpSsocket->close; |
| 337 | } |
341 | } |
| 338 | 342 | ||
| 339 | #subroutine sends data to all connected clients except for one specified |
343 | #subroutine sends data to all connected clients except for one specified |
| 340 | sub tcpServerBroadcastExcept { |
344 | sub tcpServerBroadcastExcept { |
| 341 | my $first = 0; |
345 | my $first = 0; |
| 342 | my $data = $_[0]; |
346 | my $data = $_[0]; |
| 343 | my $exception = $_[1]; |
347 | my $exception = $_[1]; |
| 344 | for $eachsocket ($tcpSselect->handles) { |
348 | for $eachsocket ($tcpSselect->handles) { |
| 345 | if ($eachsocket==$tcpSlisten) { #dont send to self |
349 | if ($eachsocket==$tcpSlisten) { #dont send to self |
| 346 | next; |
350 | next; |
| 347 | } elsif ($eachsocket==$exception) { #dont send to the specified exception |
351 | } elsif ($eachsocket==$exception) { #dont send to the specified exception |
| 348 | next; |
352 | next; |
| 349 | } else { |
353 | } else { |
| 350 | if ($first==0) { |
354 | if ($first==0) { |
| 351 | $first = 1; |
355 | $first = 1; |
| 352 | print "Sending to client ".$eachsocket->fileno; |
356 | print "Sending to client ".$eachsocket->fileno; |
| 353 | } else { |
357 | } else { |
| 354 | print ", ".$eachsocket->fileno; |
358 | print ", ".$eachsocket->fileno; |
| 355 | } |
359 | } |
| 356 | $eachsocket->send($data) or do { |
360 | $eachsocket->send($data) or do { |
| 357 | &tcpServerClientDis; |
361 | &tcpServerClientDis; |
| 358 | } |
362 | } |
| 359 | } |
363 | } |
| 360 | } |
364 | } |
| 361 | if ($first==1) { |
365 | if ($first==1) { |
| 362 | print "\n"; |
366 | print "\n"; |
| 363 | } |
367 | } |
| 364 | } |
368 | } |
| 365 | 369 | ||
| 366 | ##################### General functions ############################ |
370 | ##################### General functions ############################ |
| 367 | 371 | ||
| 368 | #this function should do a number of tests on $_[0] and print messages if packet is malformed and return 1 in $_[2] |
372 | #this function should do a number of tests on $_[0] and print messages if packet is malformed and return 1 in $_[2] |
| 369 | #messages can be suppressed by having $_[1] as 1 |
373 | #messages can be suppressed by having $_[1] as 1 |
| 370 | sub testPacket { |
374 | sub testPacket { |
| 371 | my $input = $_[0]; |
375 | my $input = $_[0]; |
| 372 | my $messages = $_[1]; |
376 | my $messages = $_[1]; |
| 373 | 377 | ||
| 374 | #line should be parsed somewhat here, check PKT, AUT and so on |
378 | #line should be parsed somewhat here, check PKT, AUT and so on |
| 375 | $testErr = 0; |
379 | $testErr = 0; |
| 376 | if (length($input) < 16) { |
380 | if (length($input) < 16) { |
| 377 | $testErr = 1; |
381 | $testErr = 1; |
| 378 | if ($messages!=1) {print "Packet was malformed, not long enough\n";} |
382 | if ($messages!=1) {print "Packet was malformed, not long enough\n";} |
| 379 | } |
383 | } |
| 380 | if ($testErr == 0 && substr($input, 0, 4) != "PKT ") { |
384 | if ($testErr == 0 && substr($input, 0, 4) != "PKT ") { |
| 381 | $testErr = 1; |
385 | $testErr = 1; |
| 382 | if ($messages!=1) {print "Packet was malformed, should start with PKT\n";} |
386 | if ($messages!=1) {print "Packet was malformed, should start with PKT\n";} |
| 383 | } |
387 | } |
| 384 | #if (substr($line, 12, 1) != " " || substr($line, 14, 1) != " ") { |
388 | #if (substr($line, 12, 1) != " " || substr($line, 14, 1) != " ") { |
| 385 | # $testErr = 1; |
389 | # $testErr = 1; |
| 386 | # print "Packet was malformed, spaces not in the correct place\n"; |
390 | # print "Packet was malformed, spaces not in the correct place\n"; |
| 387 | #} |
391 | #} |
| 388 | @splitinput = split(/ +/, $input); |
392 | @splitinput = split(/ +/, $input); |
| 389 | $inputarrlen = @splitinput; |
393 | $inputarrlen = @splitinput; |
| 390 | if ($testErr == 0 && ($inputarrlen < 4 || $inputarrlen > 12)) { |
394 | if ($testErr == 0 && ($inputarrlen < 4 || $inputarrlen > 12)) { |
| 391 | $testErr = 1; |
395 | $testErr = 1; |
| 392 | if ($messages!=1) {print "Packet was malformed, to many or to few parts\n";} |
396 | if ($messages!=1) {print "Packet was malformed, to many or to few parts\n";} |
| 393 | } |
397 | } |
| 394 | if ($testErr == 0 && length($splitinput[1]) != 8) { |
398 | if ($testErr == 0 && length($splitinput[1]) != 8) { |
| 395 | $testErr = 1; |
399 | $testErr = 1; |
| 396 | if ($messages!=1) {print "Packet was malformed, id part must be 8 digits (padded with zeros)\n";} |
400 | if ($messages!=1) {print "Packet was malformed, id part must be 8 digits (padded with zeros)\n";} |
| 397 | } |
401 | } |
| 398 | if ($testErr == 0 && length($splitinput[2]) != 1) { |
402 | if ($testErr == 0 && length($splitinput[2]) != 1) { |
| 399 | $testErr = 1; |
403 | $testErr = 1; |
| 400 | if ($messages!=1) {print "Packet was malformed, ext part must be 1 digit\n";} |
404 | if ($messages!=1) {print "Packet was malformed, ext part must be 1 digit\n";} |
| 401 | } |
405 | } |
| 402 | if ($testErr == 0 && length($splitinput[3]) != 1) { |
406 | if ($testErr == 0 && length($splitinput[3]) != 1) { |
| 403 | $testErr = 1; |
407 | $testErr = 1; |
| 404 | if ($messages!=1) {print "Packet was malformed, rtr part must be 1 digit\n";} |
408 | if ($messages!=1) {print "Packet was malformed, rtr part must be 1 digit\n";} |
| 405 | } |
409 | } |
| 406 | for ($i = 4; $i < $inputarrlen; $i++) { |
410 | for ($i = 4; $i < $inputarrlen; $i++) { |
| 407 | if ($testErr == 0 && length($splitinput[$i]) != 2) { |
411 | if ($testErr == 0 && length($splitinput[$i]) != 2) { |
| 408 | $testErr = 1; |
412 | $testErr = 1; |
| 409 | if ($messages!=1) {print "Packet was malformed, data parts must be 2 digits (padded with zero)\n";} |
413 | if ($messages!=1) {print "Packet was malformed, data parts must be 2 digits (padded with zero)\n";} |
| 410 | } |
414 | } |
| 411 | } |
415 | } |
| 412 | $checkstrlen = length($input)-4; |
416 | $checkstrlen = length($input)-4; |
| 413 | $checkstr = substr($input, 4, $checkstrlen); |
417 | $checkstr = substr($input, 4, $checkstrlen); |
| 414 | if ($testErr == 0 && $checkstr !~ m/^[0-9a-fA-F ]*$/) { |
418 | if ($testErr == 0 && $checkstr !~ m/^[0-9a-fA-F ]*$/) { |
| 415 | $testErr = 1; |
419 | $testErr = 1; |
| 416 | if ($messages!=1) {print "Packet was malformed, not hex\n";} |
420 | if ($messages!=1) {print "Packet was malformed, not hex\n";} |
| 417 | } |
421 | } |
| 418 | 422 | ||
| 419 | $_[2] = $testErr; |
423 | $_[2] = $testErr; |
| 420 | } |
424 | } |
| 421 | 425 | ||
| 422 | #this function should parse $_[0] (can2serial-data) and save in $_[1] as string |
426 | #this function should parse $_[0] (can2serial-data) and save in $_[1] as string |
| 423 | sub c2sToString { |
427 | sub c2sToString { |
| 424 | my $input = $_[0]; |
428 | my $input = $_[0]; |
| 425 | my $output = ""; |
429 | my $output = ""; |
| 426 | #if not correct input data, then dont save anything in $_[1] |
430 | #if not correct input data, then dont save anything in $_[1] |
| 427 | if (ord(substr($input, 0, 1))==$C2SSTARTSTR && ord(substr($input, 16, 1))==$C2SENDSTR) { |
431 | if (ord(substr($input, 0, 1))==$C2SSTARTSTR && ord(substr($input, 16, 1))==$C2SENDSTR) { |
| 428 | #$output = "boundaries=ok "; |
432 | #$output = "boundaries=ok "; |
| 429 | $id = ord(substr($input, 1, 1)) + ord(substr($input, 2, 1))*256 + ord(substr($input, 3, 1))*256*256 + ord(substr($input, 4, 1))*256*256*256; |
433 | $id = ord(substr($input, 1, 1)) + ord(substr($input, 2, 1))*256 + ord(substr($input, 3, 1))*256*256 + ord(substr($input, 4, 1))*256*256*256; |
| 430 | $ext = ord(substr($input, 5, 1)); |
434 | $ext = ord(substr($input, 5, 1)); |
| 431 | $rtr = ord(substr($input, 6, 1)); |
435 | $rtr = ord(substr($input, 6, 1)); |
| 432 | $dl = ord(substr($input, 7, 1)); |
436 | $dl = ord(substr($input, 7, 1)); |
| 433 | if ($ext < 2 && $rtr < 2 && $dl < 9) { |
437 | if ($ext < 2 && $rtr < 2 && $dl < 9) { |
| 434 | $output .= sprintf "%08x ", $id; |
438 | $output .= sprintf "%08x ", $id; |
| 435 | $output .= $ext . " " . $rtr; |
439 | $output .= $ext . " " . $rtr; |
| 436 | for ($i = 0; $i < $dl; $i++) { |
440 | for ($i = 0; $i < $dl; $i++) { |
| 437 | $data = ord(substr($input, 8+$i, 1)); |
441 | $data = ord(substr($input, 8+$i, 1)); |
| 438 | $output .= sprintf " %02x", $data; |
442 | $output .= sprintf " %02x", $data; |
| 439 | } |
443 | } |
| 440 | $_[1] = "PKT ".$output; |
444 | $_[1] = "PKT ".$output; |
| 441 | } else { |
445 | } else { |
| 442 | #print "debug: incorrect ext, rtr or dl\n"; |
446 | #print "debug: incorrect ext, rtr or dl\n"; |
| 443 | 447 | ||
| 444 | } |
448 | } |
| 445 | } else { |
449 | } else { |
| 446 | #print "debug: incorrect boundaries\n"; |
450 | #print "debug: incorrect boundaries\n"; |
| 447 | 451 | ||
| 448 | } |
452 | } |
| 449 | } |
453 | } |
| 450 | 454 | ||
| 451 | #this function should parse $_[0] (string) and save in $_[1] as can2serial-data |
455 | #this function should parse $_[0] (string) and save in $_[1] as can2serial-data |
| 452 | sub stringToc2s { |
456 | sub stringToc2s { |
| 453 | my $input = $_[0]; |
457 | my $input = $_[0]; |
| 454 | my $output = ""; |
458 | my $output = ""; |
| 455 | #if not correct input data, then dont save anything in $_[1] |
459 | #if not correct input data, then dont save anything in $_[1] |
| 456 | if (length($input) > 15) { |
460 | if (length($input) > 15) { |
| 457 | #minor checks, the data from the tcp-server should be valid |
461 | #minor checks, the data from the tcp-server should be valid |
| 458 | @splitinput = split(/ +/, $input); |
462 | @splitinput = split(/ +/, $input); |
| 459 | $inputarrlen = @splitinput; |
463 | $inputarrlen = @splitinput; |
| 460 | if (substr($input, 0, 4) == "PKT " && substr($input, 12, 1) == " " && substr($input, 14, 1) == " " && $inputarrlen > 3 && $inputarrlen < 13) { |
464 | if (substr($input, 0, 4) == "PKT " && substr($input, 12, 1) == " " && substr($input, 14, 1) == " " && $inputarrlen > 3 && $inputarrlen < 13) { |
| 461 | $id = hex($splitinput[1]); |
465 | $id = hex($splitinput[1]); |
| 462 | $ext = hex($splitinput[2]); |
466 | $ext = hex($splitinput[2]); |
| 463 | $rtr = hex($splitinput[3]); |
467 | $rtr = hex($splitinput[3]); |
| 464 | @data = @splitinput[4..($inputarrlen-1)]; |
468 | @data = @splitinput[4..($inputarrlen-1)]; |
| 465 | $dl = @data; |
469 | $dl = @data; |
| 466 | 470 | ||
| 467 | $output .= chr($C2SSTARTSTR); |
471 | $output .= chr($C2SSTARTSTR); |
| 468 | $output .= chr($id & 0xff); |
472 | $output .= chr($id & 0xff); |
| 469 | $output .= chr(($id>>8) & 0xff); |
473 | $output .= chr(($id>>8) & 0xff); |
| 470 | $output .= chr(($id>>16) & 0xff); |
474 | $output .= chr(($id>>16) & 0xff); |
| 471 | $output .= chr(($id>>24) & 0xff); |
475 | $output .= chr(($id>>24) & 0xff); |
| 472 | $output .= chr($ext); |
476 | $output .= chr($ext); |
| 473 | $output .= chr($rtr); |
477 | $output .= chr($rtr); |
| 474 | $output .= chr($dl); |
478 | $output .= chr($dl); |
| 475 | for ($i = 0; $i < 8; $i++) { |
479 | for ($i = 0; $i < 8; $i++) { |
| 476 | if ($i < $dl) { |
480 | if ($i < $dl) { |
| 477 | $output .= chr(hex($data[$i])); |
481 | $output .= chr(hex($data[$i])); |
| 478 | } else { |
482 | } else { |
| 479 | $output .= chr(" "); |
483 | $output .= chr(" "); |
| 480 | } |
484 | } |
| 481 | } |
485 | } |
| 482 | $output .= chr($C2SENDSTR); |
486 | $output .= chr($C2SENDSTR); |
| 483 | $_[1] = $output; |
487 | $_[1] = $output; |
| 484 | } |
488 | } |
| 485 | } |
489 | } |
| 486 | } |
490 | } |
| 487 | 491 | ||