Subversion Repositories HomeAutomation

Rev

Rev 1043 | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  1. #!/usr/bin/perl
  2.  
  3. # Written by Mattias Runge 20089
  4.  
  5. use IO::Socket;
  6. use Time::HiRes qw(usleep nanosleep);
  7.  
  8. $| = 1;
  9.  
  10. $numArgs = $#ARGV + 1;
  11.  
  12. my $hostname = "localhost";
  13. my $port = 1202;
  14. my $filename = "";
  15. my $hwid = "";
  16. my $bios = "false";
  17. my $reset = "false";
  18.  
  19. foreach $argnum (0 .. $#ARGV)
  20. {
  21.     if ($ARGV[$argnum] eq "-h")
  22.     {
  23.         $hostname = $ARGV[$argnum+1];
  24.         next;
  25.  
  26.     }
  27.     elsif ($ARGV[$argnum] eq "-p")
  28.     {
  29.         $port = $ARGV[$argnum+1];
  30.         next;
  31.  
  32.     }
  33.     elsif ($ARGV[$argnum] eq "-i")
  34.     {
  35.         $hwid = $ARGV[$argnum+1];
  36.         next;
  37.  
  38.     }
  39.     elsif ($ARGV[$argnum] eq "-f")
  40.     {
  41.         $filename = $ARGV[$argnum+1];
  42.         next;
  43.  
  44.     }
  45.     elsif ($ARGV[$argnum] eq "-b")
  46.     {
  47.         $bios = "true";
  48.     }
  49.     elsif ($ARGV[$argnum] eq "-r")
  50.     {
  51.         $reset = "true";
  52.     }
  53. }
  54.  
  55. if ($hwid eq "")
  56. {
  57.     die "You must specify a hardware id!\n";
  58.     exit 1;
  59. }
  60.  
  61. #0x14f90d1f to 0x1f0df914
  62. @hwidChars = split(//, $hwid);
  63. $hwid = @hwidChars[0]. @hwidChars[1]. @hwidChars[8]. @hwidChars[9]. @hwidChars[6]. @hwidChars[7]. @hwidChars[4]. @hwidChars[5]. @hwidChars[2]. @hwidChars[3];
  64.  
  65. print "atomDude settings:\n";
  66. print "Hardware id: " . $hwid . "\n";
  67. print "Atom address: " . $hostname . ":" . $port . "\n";
  68. print "Hexfile: " . $filename . "\n";
  69. print "Parameters: " . "\n";
  70.  
  71. my $sock = new IO::Socket::INET(
  72.                 PeerAddr => $hostname,
  73.                 PeerPort => $port,
  74.                 Proto => 'tcp'
  75.                 );
  76. die "Could not create socket: $!\n" unless $sock;
  77. print "Connected to Atom\n";
  78.  
  79. if ($filename ne "")
  80. {
  81.     open(FILE, $filename) || die("Could not open file!");
  82.     @fileData=<FILE>;
  83.     close(FILE);
  84.     print "Hexfile loaded\n";
  85.  
  86.     usleep(5000);
  87.     print $sock "programNode('", $hwid, "','";
  88.  
  89.     foreach $line (@fileData)
  90.     {
  91.         usleep(5000);
  92.         print $sock $line;
  93.     }
  94.  
  95.     usleep(5000);
  96.     print $sock "', ", $bios, ");\n";
  97.  
  98.     my $pLoop = 1;
  99.  
  100.     while ($pLoop eq 1)
  101.     {
  102.         $sock->recv($text, 128);
  103.        
  104.         if (rindex($text, "STOP") ne -1)
  105.         {
  106.             print $text;
  107.             $pLoop = 0;
  108.         }
  109.         elsif ($text ne '')
  110.         {
  111.             print $text;
  112.         }
  113.         else
  114.         {
  115.             print "Connection closed by other side!\n";
  116.             close($sock);
  117.             exit 1;
  118.         }
  119.     }
  120. }
  121.  
  122. if ($reset eq "true")
  123. {
  124.     usleep(5000);
  125.     print $sock "resetNode('", $hwid, "');\n";
  126.  
  127.     my $rLoop = 1;
  128.    
  129.     while ($rLoop eq 1)
  130.     {
  131.         $sock->recv($text, 128);
  132.         print $text;
  133.        
  134.         if (rindex($text, "Success") ne -1)
  135.         {
  136.             $rLoop = 0;
  137.         }
  138.         elsif (rindex($text, "Failed") ne -1)
  139.         {
  140.             close($sock);
  141.             exit 1;
  142.         }
  143.         elsif ($text eq '')
  144.         {
  145.             print "Connection closed by other side!\n";
  146.             close($sock);
  147.             exit 1;
  148.         }
  149.     }
  150. }
  151.  
  152.  
  153. usleep(5000);
  154. print $sock "quit();\n";
  155. usleep(5000);
  156. close($sock);
  157. exit 0;
  158.