From 05dd47b2527c184e8d70b2246631fc6015c7f636 Mon Sep 17 00:00:00 2001
From: Jan Michel <j.michel@gsi.de>
Date: Tue, 9 Jul 2013 17:08:22 +0200
Subject: [PATCH] startup.pl now creates both boards.ini and chains.ini
 according to setup xml file

---
 setup/testsetup.xml |   1 +
 tools/startup.pl    | 101 ++++++++++++++++++++++++++++++++++++++------
 2 files changed, 88 insertions(+), 14 deletions(-)

diff --git a/setup/testsetup.xml b/setup/testsetup.xml
index c8787b6..6c48468 100644
--- a/setup/testsetup.xml
+++ b/setup/testsetup.xml
@@ -8,6 +8,7 @@
     <chain id="0" name="chain0">
       <sensor id="0" 
               serial="5"
+              name="sensor5"
               config="sensor5.xml"
               enabled="true">
         <description>Dat einzige Ding dat mir habbe</description>
diff --git a/tools/startup.pl b/tools/startup.pl
index f2b13a6..fa956d3 100755
--- a/tools/startup.pl
+++ b/tools/startup.pl
@@ -5,6 +5,8 @@ use File::chdir;
 use XML::LibXML;
 use Getopt::Long;
 use Data::Dumper;
+use FileHandle;
+use Pod::Usage;
 $Data::Dumper::Terse = 1;
 $Data::Dumper::Useqq = 0;
 
@@ -13,7 +15,7 @@ my $defaultIniPath = "/tmp/inifiles";
 
 
 ###############################
-## Get options
+## Get'n'Check options
 ###############################
 Getopt::Long::Configure(qw(gnu_getopt));
 GetOptions(
@@ -48,25 +50,42 @@ my $name = $db->getDocumentElement->getAttribute('name');
 print STDERR "Loading setup $name from file $filename\n" if $verbose;
   
 foreach my $curctrl ($db->getDocumentElement->findnodes('controller')) {
-  print STDERR "Found controller id ".$curctrl->getAttribute('id')."\n" if $verbose;
-  $boardsini .= proto_boardsini($curctrl->getAttribute('name'),$curctrl->getAttribute('id'),$curctrl->getAttribute('address'));
+  my $ctrlname = $curctrl->getAttribute('name');
+  my $ctrlid   = $curctrl->getAttribute('id');
+  my $ctrladdr = $curctrl->getAttribute('address');
+  print STDERR "Found controller $ctrlname id $ctrlid\n" if $verbose;
+  $boardsini .= proto_boardsini($ctrlname,$ctrlid, $ctrladdr);
+
   foreach my $curchain ($curctrl->findnodes('chain')) {
-    print STDERR "  Found chain id ".$curchain->getAttribute('id')."\n" if $verbose;
+    my $chainid   = $curchain->getAttribute('id');
+    my $chainname = $curchain->getAttribute('name');
+    print STDERR "  Found chain $chainname id $chainid \n" if $verbose;
+    my (@sname, @sfile, @senable);
+    
     foreach my $cursensor ($curchain->findnodes('sensor')) {
-      print STDERR "    Found sensor id ".$cursensor->getAttribute('id')."\n" if $verbose;
+      my $sensid = $cursensor->getAttribute('id');
+      my $sensname = $cursensor->getAttribute('name');
+      my $sensfile = $cursensor->getAttribute('config');
+      my $sensena  = $cursensor->getAttribute('enabled');
+      print STDERR "    Found sensor $sensname id $sensid\n" if $verbose;
+      push(@sname,$sensname);
+      push(@sfile,$sensfile);
+      push(@senable,$sensena);
+
       foreach my $cfg ($cursensor->findnodes('config')) {
-        print STDERR "      Found config\n";
-        push(@commands,"./ui.pl -b ".$curctrl->getAttribute('name')." -o ".$cfg->textContent());
+        print STDERR "      Found config\n" if $verbose;
+        push(@commands,"./ui.pl -b $ctrlname -o ".$cfg->textContent());
         }
       }
     foreach my $cfg ($curchain->findnodes('config')) {
-      print STDERR "    Found config\n";
-      push(@commands,"./ui.pl -b ".$curctrl->getAttribute('name')." -c ".$curchain->getAttribute("name")." -o ".$cfg->textContent());
+      print STDERR "    Found config\n" if $verbose;
+      push(@commands,"./ui.pl -b $ctrlname -c $chainname -o ".$cfg->textContent());
       }
+    $chainsini->{$ctrlid} .= proto_chainsini($chainname,$chainid,$ctrladdr,\@sname,\@sfile,\@senable);
     }
   foreach my $cfg ($curctrl->findnodes('config')) {
-    print STDERR "  Found config\n";
-    push(@commands,"./ui.pl -b ".$curctrl->getAttribute('name')." -o ".$cfg->textContent());
+    print STDERR "  Found config\n" if $verbose;
+    push(@commands,"./ui.pl -b $ctrlname -o ".$cfg->textContent());
     }
   }
 
@@ -74,6 +93,15 @@ foreach my $curctrl ($db->getDocumentElement->findnodes('controller')) {
 ###############################
 ## Write ini files
 ###############################  
+my $boardsinifile = FileHandle->new($defaultIniPath."/boards.ini", 'w');
+print $boardsinifile $boardsini;
+$boardsinifile->close();
+  
+foreach my $c (keys $chainsini) {
+  my $chainsinifile = FileHandle->new($defaultIniPath."/chains_board$c.ini", 'w');
+  print $chainsinifile $chainsini->{$c};
+  $chainsinifile->close();
+  }
   
   
   
@@ -94,7 +122,7 @@ foreach my $cmd (@commands) {
 ###############################
 sub execute {
   my @o;
-  $o = qx($_[0]) unless $dryrun;
+  @o = qx($_[0]) unless $dryrun;
   print "Not running> $_[0]\n" if $dryrun;
   print Dumper @o if $verbose;
   }
@@ -116,12 +144,56 @@ CONFtrigmapsstart_trbnetAddr=0xb00b
 CONFtrigmapsreset_trbnetAddr=0xb00a
 CONFtrigrunjtag_trbnetAddr=0xb00f
 CONFtrigwriteonce_trbnetAddr=0xb014  
+
+
 %;
   return $r;
 }
 
 
 
+sub proto_chainsini {
+  my ($name,$id,$addr,$sname,$sfile,$senable) = @_;
+  $r = qq%
+[$name]
+FPGAtrbnetAddr=0x$addr
+RAMtrbnetAddr=0xa%.($id*2).qq%00
+CMDreg_trbnetAddr=0xa%.($id*2+1).qq%20
+RAMbase_trbnetAddr=0xa%.($id*2+1).qq%21
+DATAreg_trbnetAddr=0xa%.($id*2+1).qq%22
+STAT_NIBBLEStrbnetAddr=0x008d
+STATUSCRCCHECKtrbnetAddr=0xa%.($id*2+1).qq%00
+STATUS2RAM3BBASEADDRREGtrbnetAddr=0xa%.($id*2+1).qq%63
+STATUS2STARTEDtrbnetAddr=0xa%.($id*2+1).qq%64
+STATUS2RAM3BtrbnetAddr=0xa%.($id*2+1).qq%70
+CONFsignals_trbnetAddr=0xb020
+CONFresetafterfirstwrite_trbnetAddr=0xb011
+CONFresetbeforeinit_trbnetAddr=0xb010
+CONFtrigmapsstart_trbnetAddr=0xb00e
+CONFtrigmapsreset_trbnetAddr=0xb00d
+CONFtrigrunjtag_trbnetAddr=0xb00f
+CONFtrigwriteonce_trbnetAddr=0xb014
+CONFtriginitseq_trbnetAddr=0xb00c
+chainnr=$id
+DEBUGram1baddr=0xa%.($id*2+1).qq%47
+DEBUGram1bdata=0xa%.($id*2+1).qq%48
+DEBUGram1caddr=0xa%.($id*2+1).qq%49
+DEBUGram1cdata=0xa%.($id*2+1).qq%4a
+DEBUGram1crun=0xa%.($id*2+1).qq%4b
+
+;BEGIN GENERATE_CHAINORDER $name
+%;
+for (my $s=0;$s<scalar @{$sname};$s++) {
+  $sfile->[$s] =~ s\.xml\.ini\;
+  $r .=";" unless($senable->[$s]);
+  $r .= "JNR".$s."_".$sname->[$s]."=".$sfile->[$s]."\n";
+  }
+$r .=qq%;END GENERATE_CHAINORDER $name
+
+
+%;
+  return $r;
+}
 
 
 
@@ -133,7 +205,7 @@ __END__
 
 =head1 NAME
 
-startup.pl - Run the necessary steps to configure and start MAPS sensors.
+startup.pl - Run the necessary steps to configure and start MAPS sensors
 
 =head1 SYNOPSIS
 
@@ -142,7 +214,7 @@ startup.pl filename
  Options:
    -h, --help     brief help message
    -v, --verbose  be verbose to STDERR
-   -w, --warnings print warnings to STDERR
+   --dryrun       create but don't run scripts
 
 =head1 OPTIONS
 
@@ -165,5 +237,6 @@ Do everything despite actually executing configuration scripts.
 =head1 DESCRIPTION
 
 B<This program> runs the necessary steps to configure and start MAPS sensors
+Reads setup xml file and creates corresponding ini files for boards and chains.
 
 =cut
-- 
2.43.0