Subversion Repositories HomeAutomation

Rev

Rev 1640 | 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.         # Find can id
  114.         $id = hex(substr($line, 4, 8));
  115.         # Find can id class
  116.         $class = ($id & 0x1E000000)>>25;
  117.         # If class is nmt
  118.         if ($class == 0) {
  119.             # Find nmt type
  120.             $nmttype = ($id & 0x00FF0000)>>16;
  121.             # If nmt type is bios started
  122.             if ($nmttype == 8) {
  123.                 print $line."";
  124.                 if (length($line) < 40) {
  125.                     print "Error: A node was started but it did not have a HWID\n";
  126.                     exit 1;
  127.                 }
  128.                
  129.                 # Find hw id from data field
  130.                 $hwid = (hex(substr($line, 38, 2)) << 24) + (hex(substr($line, 35, 2)) << 16)
  131.                     + (hex(substr($line, 32, 2)) << 8) + (hex(substr($line, 29, 2)) << 0);
  132.                 $hwidhex = sprintf("%08x", $hwid);
  133.                 print "Setting HWID in bios.inc to 0x".$hwidhex."\n";
  134.  
  135.                 # Find device type from data field
  136.                 $devtype = hex(substr($line, 26, 2));
  137.                 if ($devtype == 0)
  138.                 {
  139.                     print "\nWarning: Unknown device type, will not set MCU settings\n\n";
  140.                 }
  141.                 elsif ($devtype < $numdevices)
  142.                 {
  143.                     print "Setting device type to ".$devices[$devtype][0]."\n";
  144.                     $devsettings = 1;
  145.                 }
  146.  
  147.                 # Open bios.inc and replace all settings
  148.                 open (FP, "<".$biosfile) or die "Cannot open file $biosfile\n";
  149.                 @file = <FP>;
  150.                 close FP;
  151.                 open (FP, ">".$biosfile) or die "Cannot open file $biosfile\n";
  152.                 foreach $file (@file) {
  153.                     $file =~ s/^NODE_HW_ID=.*/NODE_HW_ID=0x$hwidhex/g;
  154.                     if ($devsettings==1)
  155.                     {
  156.                         $file =~ s/^MCU=.*/MCU=$devices[$devtype][0]/g;
  157.                         $file =~ s/^HIGHFUSE=.*/HIGHFUSE=$devices[$devtype][1]/g;
  158.                         $file =~ s/^LOWFUSE=.*/LOWFUSE=$devices[$devtype][2]/g;
  159.                         $file =~ s/^EXTFUSE=.*/EXTFUSE=$devices[$devtype][3]/g;
  160.                         $file =~ s/^BOOT_START=.*/BOOT_START=$devices[$devtype][4]/g;
  161.                     }
  162.                     print FP $file;
  163.                 }
  164.                 close FP;
  165.  
  166.                 exit 0;
  167.             }
  168.         }
  169.     }
  170. }
  171.  
  172.