#!perl -w
#
# Perl script for recording data with the tool TShark (from Wireshark) and stopping the record
# if no more response telegrams are seen any more from the station in question.
# Tshark will create a ringbuffer (configured below). Note that TShark must support the S-Bus
# Plugin (provided with Wireshark 0.99.2) and the default capture adapter must be set to 
# the interface on which the PCD is visible (by using an hub)!
# Afterwards, try a PING command, execute over RS-232 the PCD2.M170 batch file and execute over
# RS-232 the PCD7.F655 batch file.
#
# Sript written by C. Durrer and P. Corbat for Saia Burgess Controls Ltd.
#
# History:	09.08.2006	CD: Creation
#               14.08.2006      CD: Added PING, PCD2.M170 and PCD7.F655 batch file
#		11.09.2006	CD: Added PC station to be pinged and stored in the PingLog.log
#

my $IP_AddressPCD = '192\.168\.6\.100';			# define IP addr. of PCD to be checked (\. is a point)
my $IP_AddressPCD_ClearText = "192.168.6.100";		# define the same address, but in clear text
my $IP_AddressPC = "192.168.6.1";				# define IP addr. of PC with OPC server
my $TSharkLocation = '"c:/Program Files/Wireshark/tshark"';	# where to find TShark (installation path of Wireshark)
my $LogFileSize = 1000;						# [kBytes]
my $LogFileAmount = 10;						# Amount of logfiles
my $LogFileLocation = '"d:/temp/LogFiles/TsharkLog.pcap"';
my $PingLog = "d:/temp/LogFiles/PingLog.log";

# No modifications should be necessary after this line *********************************************************************


# Log Ethernet traffic in a file *******************************************************************************************
my $TlgCounter = 0;			# telegramm counter
my $LastMatch = time;
my $ActualTime = $LastMatch;
my $MyDate = 0;

print "Starting TShark with the following options:\n";
print "Filesize:$LogFileSize\n";
print "Amount of files:$LogFileAmount\n";
print "Log file location: ${LogFileLocation}\n";

# 20 Mega and 10 files
open (TSHARK, '-|', "$TSharkLocation ".'-b filesize:20000 -b files:10 -R frame -w '."$LogFileLocation ".'-S -l') or die "Cannot open tshark!\n";

#open (TSHARK, '-|', "$TSharkLocation ".'-b filesize:1000 -b files:5 -R frame -w d:/temp/LogFiles/TsharkLog.pcap -S -l') or die "Cannot open tshark!\n";

#open TSHARK, '-|', "$TSharkLocation", "-b", "filesize:$LogFileSize", "-b", "files:$LogFileAmount", "-R", "frame", "-w", "${LogFileLocation}TsharkLog", "-S", "-l" or die "Cannot open tshark!\n";
#open TSHARK, '-|', "c:/Program Files/Wireshark/tshark", "-b", "filesize:1000", "-b", "files:5", "-R", "frame", "-w", "d:/temp/TShark/TsharkLog.pcap", "-S", "-l" or die "Cannot open tshark!\n";

while (<TSHARK>) {
	if (m{				# Pattern definition to match for response telegram:
		$IP_AddressPCD 		# beginning with ip addr: 172\.16\.1\.124 (\. means a point)
		\s->\s			# followed by a whitespace, -> and another whitespace character " -> "
		\w+\.\w+\.\w+\.\w+	# followed by a random IP address 
		\sS-Bus\sResponse}x) {	# followed by "S-Bus Response" (note the x after the pattern)
		print "S-Bus response n.$TlgCounter\n";
		#print "Found S-Bus response from concerned station!\n";
		$LastMatch = time;
		$TlgCounter++;		# Increment telegramm counter
	}
	else {
		$ActualTime = time;
		if (($ActualTime-$LastMatch)>10){	# time difference since last match
			last;
			# print "Closing TShark...";
			# close TSHARK;
			# die "\nThe capture has been stopped because station does not answer any more!\n";
		}
	}
}


$MyDate = gmtime;
open LOG1, ">>", $PingLog;
print LOG1 "\nFollowing pings executed at $MyDate:\n";
close LOG1;

# print "\n$MyDate:\n >> d:/temp/Logfiles/Pings.log";

# Test if PING command is possible ****************************************************************************************
print "Ping the Remote Station\n";
system "ping $IP_AddressPCD_ClearText >> $PingLog"; 	# ping the PCD and write to file


# Execute PCD batch file **************************************************************************************************
# print "Execute PCD Batch File\n";
# system "Srec_DxM170.bat";

# Execute Hyperterminal Trace-File to debug of PCD7.F655 *******************************************************************
# print "Execute Hyperterminal Trace-File\n";
# system "F655.exe";


# Test if PING command to PCD is possible *********************************************************************************
print "Ping the PC\n";
system "ping $IP_AddressPC >> $PingLog"; 		# ping the PC and write to file


print "Closing TShark...\n";		# close TShark...
close TSHARK;
die "\nThe capture has been stopped because station does not answer any more!\n";

#print "TShark died unexpectedly!";

# End of script capture.pl *************************************************************************************************
