#!/usr/bin/perl
# 以下は変更する必要なし
#-----------------------------------------------------------------------------
#
# RawHk2eventlog(version  $Id: eodv,v 1.4 2007/05/22 02:22:33 Exp $) 
#
# RawHkからをイベントログデータを抽出する
# 
# Usage:
# > RawHk2eventlog.pl RawHk****.log
#
# options: none
#
# example:
# RawHk2eventlog.pl RawHk****.txt				#(1)
#
#-----------------------------------------------------------------------------
# Libraries
#use strict;
#use warnings;

my $buf	= 0;
my $ch	= 0;
my $pint	= 0;
my $sint	= 0;
my $bnum	= 0;
my $flgc	= 0;
my $size	= 1000000;
my $str1	= "_temp1.txt";
my $str2	= "_temp2.txt";
my $str3	= "_temp3.txt";
my $str4	= "_temp4.txt";
my $str5	= "_temp5.txt";
my $str6	= "_temp6.txt";
my $filename1;
my $filename2;
my @dat = ();

for(my $i=0;$i<256;$i++)
{
	for(my $j=0;$j<256;$j++)
  	{
  		$dat[$i][$j] = "-";
  	}
}
#-----------------------------------------------------------------------------
# Code begin
#-----------------------------------------------------------------------------
# オプションがない場合
if ($#ARGV == -1)
{
	print "Error:ファイル名を指定してください\n";
}
# オプションが1つある場合
if ($#ARGV == 0)
{
	## 1.元ファイルを読み込みテキスト変換を行う
	##------------------------------------------------------------------------
	# 対象のファイルオープン
	open INFILE, "<$ARGV[0]" or die "file open error: $!";
	# バイナリモードに変更
	binmode INFILE;
	
	# 内容読み込み
	sysread(INFILE, $buf, $size);
	my @data = unpack("C*", $buf);
	
	# ファイルを閉じる
	close INFILE;
	
	
	## 2.HEX(テキスト)に変換したものを出力する
	##------------------------------------------------------------------------
	open OUT,">$str1" or die "file open error 1: $!";
	# Byte数読み込み
	for my $x (@data)
	{
		my $disp = sprintf("%02X",$x);
		print OUT $disp;
  	}
  	close OUT;
  	
  	## 3.HEXを読み込みヘッダ改行にする
  	##------------------------------------------------------------------------
  	open IN ,"<$str1" or die "file open error 1: $!";
  	my @line = <IN>;
  	close IN;
  	
  	open OUT,">$str2" or die "file open error 2: $!";
  	foreach(@line)
  	{
  		my $disp = $_;
  		$disp =~ s/1ACFFC1D/\n1ACFFC1D/gi;
  		print OUT 	$disp;
  	}
	close OUT;
	
	## 4.APID識別 0x4C0～0x4CF
  	##------------------------------------------------------------------------
  	open IN ,"<$str2" or die "file open error 2: $!";
  	@line = <IN>;
  	close IN;
  	
  	open OUT,">$str3" or die "file open error 3: $!";
  	foreach(@line)
  	{
  		my $disp = $_;
  		if(/1ACFFC1D/)
  		{
  			# APID部抽出
  			my $apid = substr($disp,24,4);
  			
  			# APIDの中にイベントログがあったら
  			if($apid =~ /04C[A-Z\d]/){
  				
  				# ヘッダ除去
  				$disp =~ s/1ACFFC1D[A-Z\d]{28}//gi;
  				
  				# 74Byte分のユーザーデータを抽出
  				$disp = substr($disp,0,148);
  				
  				# 64Byte分のストアデータを抽出
  				$disp = substr($disp,20,128);
  				
  				print OUT 	$disp."\n";
  			}
  		}
  	}
	close OUT;
	
	## 5.イベントログ内容RAWを出力
  	##------------------------------------------------------------------------
  	open IN ,"<$str3" or die "file open error 3: $!";
  	@line = <IN>;
  	close IN;
  	
  	$filename1 = (split(/\./,$ARGV[0]))[0];
  	$filename1 = $filename1."_eventlog_raw.csv";
  	open OUT,">$filename1" or die "file open error $filename1: $!";
  	
	# ヘッダ付与
	print OUT "#No,Sattime,TaskID,Error,Handle,ErrorCode,ErrorParameter,\n";
	
  	foreach(@line)
  	{
  		my $disp = $_;
		if(/\#/) {next};
		
		# ログ1
  		my $cnt1 		= substr($disp, 0+0 , 2);
  		my $sattime1	= substr($disp, 2+0 , 8);
  		my $task1		= substr($disp,10+0 , 2);
  		my $err1		= substr($disp,12+0 , 2);
  		my $handle1		= substr($disp,14+0 , 2);
   		my $code1		= substr($disp,16+0 , 2);
   		my $par1		= substr($disp,18+0 ,14);
   		
   		# ログ2
  		my $cnt2 		= substr($disp, 0+32, 2);
  		my $sattime2	= substr($disp, 2+32, 8);
  		my $task2		= substr($disp,10+32, 2);
  		my $err2		= substr($disp,12+32, 2);
  		my $handle2		= substr($disp,14+32, 2);
   		my $code2		= substr($disp,16+32, 2);
   		my $par2		= substr($disp,18+32,14);
   		
   		# ログ3
  		my $cnt3 		= substr($disp, 0+64, 2);
  		my $sattime3	= substr($disp, 2+64, 8);
  		my $task3		= substr($disp,10+64, 2);
  		my $err3		= substr($disp,12+64, 2);
  		my $handle3		= substr($disp,14+64, 2);
   		my $code3		= substr($disp,16+64, 2);
   		my $par3		= substr($disp,18+64,14);
   		
   		# ログ4
  		my $cnt4 		= substr($disp, 0+96, 2);
  		my $sattime4	= substr($disp, 2+96, 8);
  		my $task4		= substr($disp,10+96, 2);
  		my $err4		= substr($disp,12+96, 2);
  		my $handle4		= substr($disp,14+96, 2);
   		my $code4		= substr($disp,16+96, 2);
   		my $par4		= substr($disp,18+96,14);
   		
  		# 出力
  		print OUT 	$cnt1.",".
  					$sattime1.",".
  					$task1.",".
  					$err1.",".
  					$handle1.",".
  					$code1.",".
  					$par1.",".
  					"\n";
   		print OUT 	$cnt2.",".
  					$sattime2.",".
  					$task2.",".
  					$err2.",".
  					$handle2.",".
  					$code2.",".
  					$par2.",".
  					"\n";
   		print OUT 	$cnt3.",".
  					$sattime3.",".
  					$task3.",".
  					$err3.",".
  					$handle3.",".
  					$code3.",".
  					$par3.",".
  					"\n";
   		print OUT 	$cnt4.",".
  					$sattime4.",".
  					$task4.",".
  					$err4.",".
  					$handle4.",".
  					$code4.",".
  					$par4.",".
  					"\n"; 					 					 					
  	}
	close OUT;
  	
  	## 6.イベントログ内容COVを出力
  	##------------------------------------------------------------------------
  	open IN ,"<$filename1" or die "file open error $filename1: $!";
  	@line = <IN>;
  	close IN;
  	
  	$filename2 = (split(/\./,$ARGV[0]))[0];
  	open OUT,">$filename2"."_eventlog_cov.csv" or die "file open error $filename2: $!";
  	
  	# ヘッダ付与
	print OUT "#No,Sattime,TaskID,Error,Handle,ErrorCode,ErrorParameter,\n";
	
  	foreach(@line)
  	{
  		my $disp = $_;
		if(/\#/) {next};
				
		# ログ
		$cnt1 		= hex((split(/\,/,$disp))[0]);	# No
		$sattime1	= hex((split(/\,/,$disp))[1]);	# Sattime
		$task1		= hex((split(/\,/,$disp))[2]);	# Task
		$err1		= hex((split(/\,/,$disp))[3]);	# Error
		$handle1	= hex((split(/\,/,$disp))[4]);	# Handle
		$code1		= hex((split(/\,/,$disp))[5]);	# ErrorCode
		$par1		= ((split(/\,/,$disp))[6]);		# ErrorParameter
				
		# 出力
  		print OUT 	$cnt1.",".
  					$sattime1.",".
  					$task1.",".
  					$err1.",".
  					$handle1.",".
  					$code1.",".
  					$par1.",".
  					"\n";
  	}
	close OUT;
}

# オプションが２つある場合
if ($#ARGV == 1){	
}
exit;

#-----------------------------------------------------------------------------
# Sub code begin
#-----------------------------------------------------------------------------
sub show_usage()
{
 print "Usage:\n";
 print "> RawHk2dssline.pl [-h]\n";
 print "\n";
 print "options:\n";
 print "         -h : 利用方法を提示する\n";
 print "\n";
 print "example:\n";
 print "RawHk2dssline.pl -h                         #(1) HELP\n";
}