From: Michael Wiebusch Date: Thu, 11 Jul 2013 08:29:03 +0000 (+0200) Subject: cleaned up a bit, added certain files to version control that haven't been tracked... X-Git-Url: https://jspc29.x-matter.uni-frankfurt.de/git/?a=commitdiff_plain;h=50a65a052138221ab1f2a96a9263c08ce6f55fc0;p=mvdsensorcontrol.git cleaned up a bit, added certain files to version control that haven't been tracked before --- diff --git a/xml_spielwiese/Common.pm b/tools/Common.pm similarity index 100% rename from xml_spielwiese/Common.pm rename to tools/Common.pm diff --git a/xml_spielwiese/ini2xml.pl b/tools/ini2xml.pl similarity index 100% rename from xml_spielwiese/ini2xml.pl rename to tools/ini2xml.pl diff --git a/tools/xml2ini.pl b/tools/xml2ini.pl new file mode 100755 index 0000000..bba98f5 --- /dev/null +++ b/tools/xml2ini.pl @@ -0,0 +1,477 @@ +#!/usr/bin/perl -w + +use strict; +use warnings; +use XML::LibXML; +use POSIX; +use Getopt::Long; +use Pod::Usage; +#use bignum qw/hex/; + +require Common; + + + +# TODO: + +# remove issues with spec file input, +# it comes with the specDbFileTag anyway! + +# pass specification path as argument +# changeAncestor is not available here, why does it not complain? + +# inheriting still not working, something wrong in sub merge trees 420,433 + + +# done: +# recognize confDir from configFile path +# cast error when field value is too big for field! + +# manage command line options +my $help=0; +my $configFile=0; +#my $specFile=0; +my $output=0; +my $verbose=0; +my $testblock=0; + +Getopt::Long::Configure(qw(gnu_getopt)); +GetOptions( + 'help|h' => \$help, + 'verbose|v' => \$verbose, + 'config|c=s' => \$configFile, +# 'spec|s=s' => \$specFile, + 'output|o=s' => \$output, +# 'redundant|r' => \$redundant, + 'testblock|t' => \$testblock + ) or pod2usage(2); +pod2usage(1) if $help; +### + +# unless specified by --output/-o, the print OUTPUT commands go to STDOUT +if ($output) { + open(OUTPUT, '>', $output) or die "could not open $output for writing!"; +} else { + *OUTPUT = *STDOUT; +} +# + +# if verbose flag is set, make verbose filehandle equal STDOUT and not /dev/null +# which means: if verbose flag is set, then really print the verbose print directives +unless ($verbose) { + open(VERBOSE, '>', "/dev/null") or die; +} else { + *VERBOSE = *STDOUT; +} + +#### room for testblocks ######################################## + +if($testblock){ + + print "\nstart testblock\n\n"; + + + my $bigstring = "09876543210"; + print $bigstring."\n"; + writeFieldInRegister(\$bigstring,"g",2,5); + print $bigstring."\n"; + + my $fraction = "123"; + padZeros(\$fraction,8); + print $fraction."\n"; + + + print "\nend testblock\n"; + exit; + + +} +################################################################# + + + +# ini and specfile arguments mandatory! +unless ($configFile) { + pod2usage(1); +} +# + + + + +# paste a lot of stuff/global variables from cgitest.pl + +my $parser = XML::LibXML->new(); +# defined earlier +my $specFile = ""; +#my $configFile = ""; +my $specFileName = ""; +my $configFileName = ""; +my $specTree; +my $configTree; +my $configMapsType; + +my %fileLevelHash; + +my $ancestryTree; +my @ancestryList; + +my $confDir = '../config'; # default, gets overwritten anyway +my $specDir = '../specs'; + +# separate configFileName and the containing directory path +my @configFilePath = split("/",$configFile); +$configFileName = pop(@configFilePath); +$confDir = join("/",@configFilePath); + +print VERBOSE "configFileName: $configFileName\nconfDir: $confDir\n"; + + + +# parse config and spec file +parseConfigAndSpec( $configFileName ); +# parse and integrate all ancestors of configFile into configTree +buildAncestry( $configFileName, "" ); +$configTree = $ancestryTree; # not so elegant, but works + + +print VERBOSE "specFileName: $specFileName\n specDir: $specDir\n"; + + + +# written beforrrrrreeeeeeee + +# # initialize the xml parser +# my $parser = XML::LibXML->new(); +# read in the JTAG specification file +# print VERBOSE "parsing specifications file...\n"; +# my $specTree = $parser->parse_file($specFile); +# my $configTree = $parser->parse_file($configFile); +my $specMaps = $specTree->findnodes("/MAPS")->shift(); +my $configMaps = $configTree->findnodes("/MAPS")->shift(); +# +# # perform compatibility checks +# +# $specFile =~ m/([^\/]+\.xml)$/; +# my $specFileName = $1; +# +my $configSpecDbFileName = $configMaps->findvalue("./\@specDbFile"); +unless ($configSpecDbFileName eq $specFileName) { + die "given specification file name does not match the specDbFile tag in the config file!\n"; +} + +unless ($configMaps->findvalue("./\@type") eq $specMaps->findvalue("./\@type")){ + die "MAPS type mismatch between settings and specification file!\n"; +} + +# create standard content for ini file + +print VERBOSE "beginning of output file:\n\n"; + +print OUTPUT "[General]\n"; +print OUTPUT "IRLEN=5\n"; +print OUTPUT "BYPASSREG=1F\n"; +print OUTPUT "[Data]\n"; + + + + + +my @specRegisters = $specMaps->findnodes("./register"); + + + +my $registerCounter = 0; +for my $specRegister ( @specRegisters){ + + my $registerName = $specRegister->findvalue("./\@name"); + my $registerId = $specRegister->findvalue("./\@id"); + my $registerSize = $specRegister->findvalue("./\@size"); + + my $registerValBinStr = zeros($registerSize); + + my $stringSize = ceil($registerSize/32)*8; # this fulfills Bertram's requirement to write the hex string in multiples of 8 nibbles = 32 bit words + + my @specFields = $specRegister->findnodes("./field"); + + for my $specField (@specFields){ + my $fieldName = $specField->findvalue("./\@name"); + my $fieldStart = $specField->findvalue("./\@start"); + my $fieldEnd = $specField->findvalue("./\@end"); + my $fieldSize = $specField->findvalue("./\@size"); + + print VERBOSE "processing $registerName -> $fieldName\n"; + # check for setting in the settings file + my $fieldValue = $configTree->findvalue("/MAPS/register[\@name='".$registerName."']/field[\@name='".$fieldName."']/\@value"); + if($fieldValue eq ""){ + print VERBOSE "use default setting\n"; + $fieldValue = $specField->findvalue("./\@defaultValue"); + } else { + print VERBOSE "use setting from config file\n"; + } + + + my $fieldValueHexString = stripBasePrefix(any2hex($fieldValue)); + #print VERBOSE "fieldValueHexString: $fieldValueHexString\n"; + my $fieldValueBinString = hexStr2binStr($fieldValueHexString); + + writeFieldInRegister(\$registerValBinStr,$fieldValueBinString,$fieldStart,$fieldEnd); + + # done with field! + + } + + my $registerValHexString = binStr2hexStr($registerValBinStr); + padZeros(\$registerValHexString,8); + + # print the interesting part of the ini file, the register values in Bertram Format! + print OUTPUT ";$registerName\n"; + printf OUTPUT ("%02d=%s,%d,%s\n",$registerCounter,$registerId,$registerSize,$registerValHexString); + + + + + + + + + + + $registerCounter++; +} + + + + + + + + + + + + + + + +###### all my SUBS ########### + +sub parseConfigAndSpec { + parseConfig( $_[0] ); + $specFileName = $configTree->findvalue("/MAPS/\@specDbFile"); + $specFile = $specDir . "/" . $specFileName; + unless ( -e $specFile ) { + print +"

specification file \"$specFileName\" could not be found in the specification directory \"$specDir\"

"; + exit; + } + $specTree = $parser->parse_file($specFile); +} + +sub parseConfig { + $configFileName = $_[0]; + $configFile = $confDir . "/" . $_[0]; + $configTree = $parser->parse_file($configFile); + $configMapsType = $configTree->findvalue("/MAPS/\@type"); +} + +sub buildAncestry { # recursive + my $xmlfileName = $_[0]; + my $parentXmlFileName = $_[1]; + if ( $xmlfileName eq "" ) { + return; + } + + # check if you are not including yourself + if ( $parentXmlFileName eq $xmlfileName ) { + print "

It's a bad idea to try to include yourself!

"; + print "

The faulty include directive was removed.

"; + print "

"; + changeAncestor(""); # break the evil circle where it was closed! + exit; + } + + # check against circular dependencies + for (@ancestryList) { # suppres circular dependencies! + if ( $xmlfileName eq $_ ) { + + print "

No circular includes, please!

"; + print "

"; + print $parentXmlFileName. "->" + . join( "->", @ancestryList ) . "->" + . $configFileName; + print "

"; + print "

The faulty include directive was removed.

"; + print + "

"; + changeAncestor(""); # break the evil circle where it was closed! + exit; + + #die "no circular includes please! $!"; + return; + } + } + + my $xmlfile = $confDir . "/" . $xmlfileName; + + # check if all files in the ancestry really exist! + unless ( -e $xmlfile ) { + + print "

You are trying to include a file that does not exist

"; + print "

"; + print "" + . $xmlfileName + . "" . "->" + . join( "->", @ancestryList ) . "->" + . $configFileName; + print "

"; + print "

The faulty include directive was removed.

"; + print "

"; + changeAncestor(""); # break the evil circle where it was closed! + exit; + return; + } + + my $xmltree; + if ( $xmlfile eq $configFile ) { + $xmltree = $configTree; + } + else { + + # we got an ancestor here, write him to the ancestor list + unshift( @ancestryList, $xmlfileName ); + $xmltree = $parser->parse_file($xmlfile); + + # tag all the fields with an heritage attribute + for my $field ( $xmltree->findnodes("//field") ) { + $field->setAttribute( "isHeritageFrom", "$xmlfileName" ); + } + } + + my $ancestorFileName = $xmltree->findvalue("/MAPS/\@inheritSettingsFrom") + || ""; + my $currentMapsType = $xmltree->findvalue("/MAPS/\@type"); + my $currentSpecFileName = $xmltree->findvalue("/MAPS/\@specDbFile"); + + # compliance checking MAPS type + unless ( $currentMapsType eq $configMapsType ) { + print +"

the included config file does not comply with the MAPS type of the current config file

"; + print "

The faulty include directive was removed.

"; + print "

"; + changeAncestor(""); # break the evil circle where it was closed! + exit; + } + + # compliance checking specDbFile + unless ( $currentSpecFileName eq $specFileName ) { + print +"

the included config file is not based on the same specification file as current config file

"; + print "

The faulty include directive was removed.

"; + print "

"; + changeAncestor(""); # break the evil circle where it was closed! + exit; + } + unless ( $ancestorFileName eq "" ) { + + # this block is executed when an ancestor is found + buildAncestry( $ancestorFileName, $xmlfileName ) + ; # recursion, second argument is the target file from THIS parent recursion call + + #integrate the current tree into the ancestryTree, overwrite older settings + mergeTrees( $ancestryTree, $xmltree ); + } + else { + +# this block is called when you are at the root of the ancestry -> you are THE Father +# begin the ancestry tree + + $ancestryTree = $xmltree; + } + + my $counter = 1; + for my $file ( reverse(@ancestryList) ) { + $fileLevelHash{$file} = $counter; + if ( $counter < 6 ) { + $counter++; + } + } + +} + +sub mergeTrees +{ # give me two trees, tree1 overwrites everything in tree0 and will consequently be integrated in tree0 + my $tree0 = $_[0]; + my $tree1 = $_[1]; + + my $maps0 = $tree0->findnodes("/MAPS")->shift(); + my $maps1 = $tree1->findnodes("/MAPS")->shift(); + + my @registers0 = $tree0->findnodes("/MAPS/register"); + my @registers1 = $tree1->findnodes("/MAPS/register"); + + for my $register1 (@registers1) { + my $registerName = $register1->findvalue("./\@name"); + if ( $registerName eq "" ) { + die "registerName in tree1 undefined!"; + } + + # if current register does not exist in tree0, create it + my $register0 = + $maps0->findnodes( "./register[\@name='" . $registerName . "']" ) + ->shift(); + unless( defined($register0) ) { + $register0 = $maps0->addNewChild( "", "register" ); + $register0->setAttribute( "name", $registerName ); + } + + my @fields1 = $register1->findnodes("./field"); + for my $field1 (@fields1) { + my $fieldName = $field1->findvalue("./\@name"); + + # if field with same name exists in tree0/register0 delete it! + my $field0 = + $register0->findnodes( "./field[\@name='" . $fieldName . "']" ) + ->shift(); + if ( defined($field0) ) { + $register0->removeChild($field0); + } + + # copy current field from tree1/register1 to tree0/register0 + + $register0->addChild($field1); + } + + } +} + +############################### +#### Feierabend! +############################### +__END__ + +=head1 NAME + +xml2ini.pl - convert an xml file compliant with the database system of Jan and Michael to a "Bertram Style" ini file + +=head1 SYNOPSIS + +xml2ini.pl -c config.xml -s specFile.xml [-o output.ini] + + Options: + -h, --help brief help message + -v, --verbose detailed debugging info about ongoing actions + -c, --config specifies the input config xml file + -s, --spec specifies the specification xml file by + which the .ini file is to be encoded + !!! do not use !!! + -o, --output specifies the output filename, if ommitted, + the generated ini file is written to STDOUT + -t, --testblock execute only testblock + + +=back + +=head1 DESCRIPTION + +=cut diff --git a/xml_spielwiese/xml2ini_old.pl b/xml_spielwiese/xml2ini_old.pl deleted file mode 100755 index d92eb49..0000000 --- a/xml_spielwiese/xml2ini_old.pl +++ /dev/null @@ -1,98 +0,0 @@ -#!/usr/bin/perl -w - -use strict; -use warnings; -use XML::LibXML; -use POSIX; - -my $parser = XML::LibXML->new(); - -my $specfile = "./MIMOSA26_JTAG_SPEC.xml"; -my $spectree = $parser->parse_file($specfile); - -my $setfile = "./settings.xml"; -my $settree = $parser->parse_file($setfile); - -my @registers = $spectree->findnodes("/MAPS/register"); - - - -my $specMapsType = $spectree->findvalue("/MAPS/\@type"); -my $setMapsType = $settree->findvalue("/MAPS/\@type"); - -# check if specification file and settings file are compatible -if ($specMapsType ne $setMapsType) { - die "MAPS type mismatch between settings and specification file!\n"; -} - - - - - -my $registerCounter = 0; -for my $register ( @registers){ - - register2hex($register); - $registerCounter++; -} - -#print $new_xml->toString(1); - - -sub register2hex { -# TODO -# try catch blocks? -# good work so far - my $register = $_[0]; - my $registerName = $register->findvalue("./\@name"); - my $registerId = $register->findvalue("./\@id"); - my $registerSize = $register->findvalue("./\@size"); - my $stringSize = ceil($registerSize/32)*8; - - - my $result = 0; - - my @fields = $register->findnodes("./field"); - - for my $field (@fields){ - my $name = $field->findvalue("./\@name"); - my $start = $field->findvalue("./\@start"); - my $end = $field->findvalue("./\@end"); - my $size = $field->findvalue("./\@size"); - - # check for setting in the settings file - my $value = $settree->findvalue("/MAPS/register[\@name='".$registerName."']/field[\@name='".$name."']/\@value"); - if ($value ne "") { - #print "Setting found! $value\n"; - } else { # if nothing found, then use the default value from the specfile - $value = $field->findvalue("./\@defaultValue"); - #print "Use default setting: $value\n"; - } - - $value = any2dec($value); # convert any numeric code to decimal - my $calcedsize = $end-$start+1; - if ( $calcedsize != $size) { - die "start/stop/size mismatch in register $registerName : field $name\n"; - } - #print "$name: $start -> $end = $calcedsize/$size\n"; - $result = $result | ($value<<$start); - #print "value: $value\n"; - } - my $resultstring = sprintf("%0".$stringSize."X",$result); - print ";$registerName\n"; - printf ("%02d=%s,%d,%s\n",$registerCounter,$registerId,$registerSize,$resultstring); - -} - - -sub any2dec { # converts numeric expressions 0x, 0b or decimal to decimal - - my $argument = $_[0]; - #print "any2dec input argument $argument\n"; - - if ( $argument =~ m/0[bxBX]/) { - return oct $argument; - } else { - return $argument; - } -}