Subversion Repositories HomeAutomation

Rev

Rev 1638 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  1. #!/usr/bin/perl -w
  2. ################################################
  3. #
  4. # author: arune @ efnet
  5. #
  6. ################################################
  7.  
  8. use IO::Socket;
  9. use integer;
  10. use threads;
  11. use Cwd qw(abs_path);
  12. use File::Basename;
  13. use Getopt::Long;
  14.  
  15. $host = "localhost";
  16. $port = 1200;
  17.  
  18. # Get hostname and port from command line arguments
  19. if ( @ARGV > 0 ) {
  20.     GetOptions( "host=s"    => \$host,          #
  21.             "port=i"    => \$port,          #
  22.                         );
  23. }
  24.  
  25. my $path = dirname(abs_path($0));
  26.  
  27. # Read devices from separate file
  28. if (-e $path."/devices.pl") {
  29.     require $path."/devices.pl";
  30. } else {
  31.     print "Error: No devices.pl was found in ".$path.". \n\n";
  32.     exit 1;
  33. }
  34.  
  35. # Length of devices array
  36. $numdevices=@devices;
  37.  
  38. # Connect to candaemon
  39. $remote = IO::Socket::INET->new(
  40.                     Proto    => "tcp",
  41.                     PeerAddr => $host,
  42.                     PeerPort => $port,
  43.                 )
  44.                or die "Error: Cannot connect to port $port at $host\n";
  45.  
  46. $biosfile = "bios.inc";
  47. open (FP, "+<".$biosfile) or die "Error: Cannot open file $biosfile\n";
  48. @file = <FP>;
  49. close FP;
  50.  
  51. # Check bios.inc for the settings that should be updated
  52. my $foundidrow = 0;
  53. my $foundmcurow = 0;
  54. my $foundhfrow = 0;
  55. my $foundlfrow = 0;
  56. my $foundefrow = 0;
  57. my $foundbootrow = 0;
  58. foreach $file (@file) {
  59.     if ($file =~ m/^NODE_HW_ID=/) {
  60.         print "\nCurrent id-setting: ".$file."\n";
  61.         $foundidrow = 1;
  62.     }
  63.     if ($file =~ m/^MCU=/) {
  64.         $foundmcurow = 1;
  65.     }
  66.     if ($file =~ m/^HIGHFUSE=/) {
  67.         $foundhfrow = 1;
  68.     }
  69.     if ($file =~ m/^LOWFUSE=/) {
  70.         $foundlfrow = 1;
  71.     }
  72.     if ($file =~ m/^EXTFUSE=/) {
  73.         $foundefrow = 1;
  74.     }
  75.     if ($file =~ m/^BOOT_START=/) {
  76.         $foundbootrow = 1;
  77.     }
  78. }
  79. if ($foundidrow == 0) {
  80.     print "Error: Did not find NODE_HW_ID= in file $biosfile\n";
  81.     exit 1;
  82. }
  83. if ($foundmcurow == 0) {
  84.     print "Error: Did not find MCU= in file $biosfile\n";
  85.     exit 1;
  86. }
  87. if ($foundhfrow == 0) {
  88.     print "Error: Did not find HIGHFUSE= in file $biosfile\n";
  89.     exit 1;
  90. }
  91. if ($foundlfrow == 0) {
  92.     print "Error: Did not find LOWFUSE= in file $biosfile\n";
  93.     exit 1;
  94. }
  95. if ($foundefrow == 0) {
  96.     print "Error: Did not find EXTFUSE= in file $biosfile\n";
  97.     exit 1;
  98. }
  99. if ($foundbootrow == 0) {
  100.     print "Error: Did not find BOOT_START= in file $biosfile\n";
  101.     exit 1;
  102. }
  103.  
  104. # Define variable to know if a device was found
  105. my $devsettings=0;
  106.  
  107. print "Connect your node to the CAN-network (disconnect it first if it's already connected)\n";
  108. print "Abort with Ctrl+c\n";
  109. #read line from socket (blocking)
  110. while ( $line = <$remote> ) {
  111. #PKT 00087f00 1 0 39 13 01 20 78 56 34 12
  112.         if (length($line) > 12) {
  113.                 $id = hex(substr($line, 4, 8));
  114.                 $class = ($id & 0x1E000000)>>25;
  115.                 if ($class == 0) {
  116.                     $nmttype = ($id & 0x00FF0000)>>16;
  117.                     if ($nmttype == 8) {
  118.                         print $line."";
  119.                         #print $class." ".$nmttype."\n";
  120.                         if (length($line) < 40) {
  121.                             print "Error: A node was started but it did not have a HWID\n";
  122.                             exit 1;
  123.                         }
  124.                         $hwid = (hex(substr($line, 38, 2))<<24)+(hex(substr($line, 35, 2))<<16)+
  125.                             (hex(substr($line, 32, 2))<<8)+(hex(substr($line, 29, 2))<<0);
  126.                         $hwidhex = sprintf("%08x", $hwid);
  127.                         print "Setting HWID in bios.inc to 0x".$hwidhex."\n";
  128.                        
  129.                         $devtype = hex(substr($line, 26, 2));
  130.                         if ($devtype == 0)
  131.                         {
  132.                             print "\nWarning: Unknown device type, will not set MCU settings\n\n";
  133.                         }
  134.                         elsif ($devtype < $numdevices)
  135.                         {
  136.                             print "Setting device type to ".$devices[$devtype][0]."\n";
  137.                             $devsettings = 1;
  138.                         }
  139.  
  140.                         open (FP, "<".$biosfile) or die "Cannot open file $biosfile\n";
  141.                         @file = <FP>;
  142.                         close FP;
  143.                         open (FP, ">".$biosfile) or die "Cannot open file $biosfile\n";
  144.                         foreach $file (@file) {
  145.                             $file =~ s/^NODE_HW_ID=.*/NODE_HW_ID=0x$hwidhex/g;
  146.                             if ($devsettings==1)
  147.                             {
  148.                                 $file =~ s/^MCU=.*/MCU=$devices[$devtype][0]/g;
  149.                                 $file =~ s/^HIGHFUSE=.*/HIGHFUSE=$devices[$devtype][1]/g;
  150.                                 $file =~ s/^LOWFUSE=.*/LOWFUSE=$devices[$devtype][2]/g;
  151.                                 $file =~ s/^EXTFUSE=.*/EXTFUSE=$devices[$devtype][3]/g;
  152.                                 $file =~ s/^BOOT_START=.*/BOOT_START=$devices[$devtype][4]/g;
  153.                             }
  154.                             print FP $file;
  155.                         }
  156.                         close FP;
  157.                        
  158.                         exit 0;
  159.                        
  160.                     }
  161.                 }
  162.                
  163.     }
  164. }
  165.  
  166.