Subversion Repositories HomeAutomation

Rev

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

  1. #!/usr/bin/perl
  2.  
  3. # Written by Mattias Runge 2008-2010
  4.  
  5. use Cwd 'abs_path';
  6. use File::Basename;
  7. use Time::HiRes qw( usleep );
  8.  
  9. $| = 1;
  10.  
  11. $numArgs = $#ARGV + 1;
  12.  
  13. my $hostname = "localhost";
  14. my $port = 1201;
  15. my $filename = "";
  16. my $hwid = "";
  17. my $bios = 0;
  18. my $reset = "false";
  19.  
  20. foreach $argnum (0 .. $#ARGV)
  21. {
  22.     if ($ARGV[$argnum] eq "-h")
  23.     {
  24.         $hostname = $ARGV[$argnum+1];
  25.         next;
  26.  
  27.     }
  28.     elsif ($ARGV[$argnum] eq "-p")
  29.     {
  30.         $port = $ARGV[$argnum+1];
  31.         next;
  32.  
  33.     }
  34.     elsif ($ARGV[$argnum] eq "-i")
  35.     {
  36.         $hwid = $ARGV[$argnum+1];
  37.         next;
  38.  
  39.     }
  40.     elsif ($ARGV[$argnum] eq "-f")
  41.     {
  42.         $filename = abs_path($ARGV[$argnum+1]);
  43.         next;
  44.  
  45.     }
  46.     elsif ($ARGV[$argnum] eq "-b")
  47.     {
  48.         $bios = 1;
  49.     }
  50.     elsif ($ARGV[$argnum] eq "-r")
  51.     {
  52.         $reset = "true";
  53.     }
  54. }
  55.  
  56. if ($hwid eq "")
  57. {
  58.     die "You must specify a hardware id!\n";
  59.     exit 1;
  60. }
  61.  
  62. my $path = dirname(abs_path($0));
  63.  
  64. # Read atom functions from separate file
  65. if (-e $path."/setConfig/AtomLib.pl") {
  66.     require $path."/setConfig/AtomLib.pl";
  67. } else {
  68.     print "Error: No AtomLib.pl was found in ".$path.". \n\n";
  69.     exit 1;
  70. }
  71.  
  72. #0x14f90d1f to 0x1f0df914
  73. @hwidChars = split(//, $hwid);
  74. $hwid = $hwidChars[0]. $hwidChars[1]. uc($hwidChars[8]. $hwidChars[9]. $hwidChars[6]. $hwidChars[7]. $hwidChars[4]. $hwidChars[5]. $hwidChars[2]. $hwidChars[3]);
  75.  
  76. print "atomDude settings:\n";
  77. print "Hardware id: " . $hwid . "\n";
  78. print "Atom address: " . $hostname . ":" . $port . "\n";
  79. print "Hexfile: " . $filename . "\n";
  80. #print "Parameters: " . "\n";
  81.  
  82. ############################## RESET ##############################
  83. if ($reset eq "true" && $filename eq "")
  84. {
  85.     if ($port == 1203)
  86.     {
  87.         $socket = atomd_initialize($hostname, $port);
  88.         atomjs_write($socket, "Node_ResetNative('".$hwid."');\n");
  89.         $response = atomjs_read_line($socket);
  90.         if ($response =~ m/Error/)
  91.         {
  92.             die $response;
  93.         }
  94.         print $response;
  95.  
  96.         $success = 0;
  97.         for ($count = 0; $count < 15; $count++)
  98.         {
  99.             atomjs_write($socket, "Node_ResetPollNative();\n");
  100.             if (atomjs_read_line($socket) =~ m/true/)
  101.             {
  102.                 print "\033[32m" . $hwid . " started okay.\033[0m\n";
  103.                 $success = 1;
  104.                 last;
  105.             }
  106.             usleep(200000);
  107.         }
  108.         if ($success == 0)
  109.         {
  110.             print "\033[31mTimed out waiting for reset.\033[0m\n";
  111.         }
  112.     }
  113.     else
  114.     {
  115.         $socket = atomd_initialize($hostname, $port);
  116.  
  117.         usleep(100000);
  118.         atomd_kill_promt($socket);
  119.         atomd_send_command($socket, "Node_Reset $hwid");
  120.         print atomd_read_command_response($socket);
  121.         #my $result = system("atomic -s $hostname -p $port -c \"Node_Reset $hwid\"");
  122.         #exit($result);
  123.     }
  124. }
  125. ############################## REPROGRAM ##############################
  126. else
  127. {
  128.     if ($port == 1203)
  129.     {
  130.         open (FP, "+<".$filename) or die "Error: Cannot open file $filename\n";
  131.         @filecontents = <FP>;
  132.         close FP;
  133.         $socket = atomd_initialize($hostname, $port);
  134.         atomjs_write($socket, "Node_ClearHex();\n");
  135.         atomjs_read_line($socket);
  136.         my $linenums = 0;
  137.         foreach $filerow (@filecontents)
  138.         {
  139.             $filerow =~ s/^\s+//;
  140.             $filerow =~ s/\s+$//;
  141.             $linenums += 1;
  142.             $command = "Node_AppendHexNative('".$filerow."');\n";
  143.             atomjs_write($socket, $command);
  144.             $response = atomjs_read_line($socket);
  145.             if ($response =~ m/Error/)
  146.             {
  147.                 die $response;
  148.             }
  149.         }
  150.         $command = "Node_ProgramNative('".$hwid."', '".$bios."', '".$linenums."');\n";
  151.         atomjs_write($socket, $command);
  152.         #print $command;
  153.         $response = atomjs_read_line($socket);
  154.         if ($response =~ m/Error/)
  155.         {
  156.             die $response;
  157.         }
  158.         print $response;
  159. #atomd_disconnect($socket);
  160.         $success = 0;
  161.         for ($count = 0; $count < 150; $count++)
  162.         {
  163.             atomjs_write($socket, "Node_ProgramPollNative();\n");
  164.             if (atomjs_read_line($socket) =~ m/true/)
  165.             {
  166.                 print "\033[32m" . $hwid . " started okay.\033[0m\n";
  167.                 $success = 1;
  168.                 last;
  169.             }
  170.             usleep(200000);
  171.         }
  172.         if ($success == 0)
  173.         {
  174.             print "\033[31mTimed out waiting for program to finish.\033[0m\n";
  175.         }
  176.     }
  177.     else
  178.     {
  179.         open (FP, "+<".$filename) or die "Error: Cannot open file $filename\n";
  180.         @filecontents = <FP>;
  181.         close FP;
  182.         $socket = atomd_initialize($hostname, $port);
  183.         usleep(100000);
  184.         atomd_kill_promt($socket);
  185.    
  186.         #print "Sending Node_ClearHex\n";
  187.         atomd_send_command($socket, "Node_ClearHex");
  188.         atomd_kill_promt($socket);
  189.    
  190.         my $linenums = 0;
  191.         foreach $filerow (@filecontents)
  192.         {
  193.             #print "Sending Node_AppendHex $filerow \n";
  194.             #$filerow =~ s/^\s+//;
  195.             #$filerow =~ s/\s+$//;
  196.             atomd_send_command($socket, "Node_AppendHex $filerow");
  197.             atomd_kill_promt($socket);
  198.             $linenums += 1;
  199.         }
  200.         #print "Sending Node_ProgramHex $hwid $bios $linenums \n";
  201.         atomd_send_command($socket, "Node_ProgramHex $hwid $bios $linenums");
  202.  
  203.         print atomd_read_command_response($socket);
  204.  
  205. #       my $result = system("atomic -s $hostname -p $port -c \"Node_Program $hwid $bios $filename\"");
  206. #       exit($result);
  207.     }
  208. }
  209.  
  210. exit(0);
  211.