Subversion Repositories HomeAutomation

Rev

Rev 2158 | Rev 2161 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
1666 runge 1
#!/usr/bin/perl
2
 
3
# Written by Mattias Runge 2008-2010
4
 
5
use Cwd 'abs_path';
1885 arune 6
use File::Basename;
1890 arune 7
use Time::HiRes qw( usleep );
1666 runge 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
 
1885 arune 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
 
1666 runge 72
#0x14f90d1f to 0x1f0df914
73
@hwidChars = split(//, $hwid);
1877 arune 74
$hwid = $hwidChars[0]. $hwidChars[1]. uc($hwidChars[8]. $hwidChars[9]. $hwidChars[6]. $hwidChars[7]. $hwidChars[4]. $hwidChars[5]. $hwidChars[2]. $hwidChars[3]);
1666 runge 75
 
76
print "atomDude settings:\n";
77
print "Hardware id: " . $hwid . "\n";
78
print "Atom address: " . $hostname . ":" . $port . "\n";
79
print "Hexfile: " . $filename . "\n";
2159 arune 80
#print "Parameters: " . "\n";
1666 runge 81
 
82
if ($reset eq "true" && $filename eq "")
83
{
2158 arune 84
    if ($port == 1203)
85
    {
86
        $socket = atomd_initialize($hostname, $port);
87
        atomjs_write($socket, "Node_ResetNative('".$hwid."');\n");
88
        print atomjs_read_line($socket);
2159 arune 89
        $success = 0;
2158 arune 90
        for ($count = 0; $count < 15; $count++)
91
        {
92
            atomjs_write($socket, "Node_ResetPollNative();\n");
93
            if (atomjs_read_line($socket) =~ m/true/)
94
            {
95
                print "\033[32m" . $hwid . " started okay.\033[0m\n";
2159 arune 96
                $success = 1;
2158 arune 97
                last;
98
            }
99
            usleep(200000);
100
        }
2159 arune 101
        if ($success == 0)
102
        {
103
            print "\033[31mTimed out waiting for reset.\033[0m\n";
104
        }
2158 arune 105
    }
106
    else
107
    {
108
        $socket = atomd_initialize($hostname, $port);
109
 
110
        usleep(100000);
111
        atomd_kill_promt($socket);
112
        atomd_send_command($socket, "Node_Reset $hwid");
113
        print atomd_read_command_response($socket);
114
        #my $result = system("atomic -s $hostname -p $port -c \"Node_Reset $hwid\"");
115
        #exit($result);
116
    }
1666 runge 117
}
118
else
119
{
2158 arune 120
    if ($port == 1203)
121
    {
122
        open (FP, "+<".$filename) or die "Error: Cannot open file $filename\n";
123
        @filecontents = <FP>;
124
        close FP;
125
        $socket = atomd_initialize($hostname, $port);
2159 arune 126
        atomjs_write($socket, "Node_ClearHex();\n");
127
        atomjs_read_line($socket);
128
        my $linenums = 0;
129
        foreach $filerow (@filecontents)
130
        {
2158 arune 131
            $filerow =~ s/^\s+//;
132
            $filerow =~ s/\s+$//;
2159 arune 133
            $linenums += 1;
134
            $command = "Node_AppendHexNative('".$filerow."');\n";
135
            atomjs_write($socket, $command);
136
            atomjs_read_line($socket);
2158 arune 137
        }
2159 arune 138
        $command = "Node_ProgramNative('".$hwid."', '".$bios."', '".$linenums."');\n";
139
        atomjs_write($socket, $command);
140
        #print $command;
141
        print atomjs_read_line($socket);
142
atomd_disconnect($socket);
143
#       $success = 0;
144
#       for ($count = 0; $count < 150; $count++)
145
#       {
146
#           atomjs_write($socket, "Node_ProgramPollNative();\n");
147
#           if (atomjs_read_line($socket) =~ m/true/)
148
#           {
149
#               print "\033[32m" . $hwid . " started okay.\033[0m\n";
150
#               $success = 1;
151
#               last;
152
#           }
153
#           usleep(200000);
154
#       }
155
#       if ($success == 0)
156
#       {
157
#           print "\033[31mTimed out waiting for program to finish.\033[0m\n";
158
#       }
2158 arune 159
    }
160
    else
161
    {
162
        open (FP, "+<".$filename) or die "Error: Cannot open file $filename\n";
163
        @filecontents = <FP>;
164
        close FP;
165
        $socket = atomd_initialize($hostname, $port);
166
        usleep(100000);
167
        atomd_kill_promt($socket);
1890 arune 168
 
2158 arune 169
        #print "Sending Node_ClearHex\n";
170
        atomd_send_command($socket, "Node_ClearHex");
171
        atomd_kill_promt($socket);
1967 arune 172
 
2158 arune 173
        my $linenums = 0;
2159 arune 174
        foreach $filerow (@filecontents)
175
        {
2158 arune 176
            #print "Sending Node_AppendHex $filerow \n";
177
            #$filerow =~ s/^\s+//;
178
            #$filerow =~ s/\s+$//;
179
            atomd_send_command($socket, "Node_AppendHex $filerow");
180
            atomd_kill_promt($socket);
181
            $linenums += 1;
182
        }
183
        #print "Sending Node_ProgramHex $hwid $bios $linenums \n";
184
        atomd_send_command($socket, "Node_ProgramHex $hwid $bios $linenums");
1890 arune 185
 
2158 arune 186
        print atomd_read_command_response($socket);
1913 arune 187
 
2158 arune 188
#       my $result = system("atomic -s $hostname -p $port -c \"Node_Program $hwid $bios $filename\"");
189
#       exit($result);
2159 arune 190
    }
1666 runge 191
}
192
 
193
exit(0);