Subversion Repositories HomeAutomation

Rev

Rev 1882 | Rev 1885 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 1882 Rev 1884
Line 9... Line 9...
9
use integer;
9
use integer;
10
use threads;
10
use threads;
11
use Cwd qw(abs_path);
11
use Cwd qw(abs_path);
12
use File::Basename;
12
use File::Basename;
13
use Getopt::Long;
13
use Getopt::Long;
14
 
-
 
15
use IO::Select;
-
 
16
use Fcntl ':seek';
-
 
17
 
14
 
18
$host = "localhost";
15
$host = "localhost";
19
$port = 1201;
16
$port = 1201;
20
 
17
 
21
# Get hostname and port from command line arguments
18
# Get hostname and port from command line arguments
22
if ( @ARGV > 0 ) {
19
if ( @ARGV > 0 ) {
23
    GetOptions( "host=s"    => \$host,          #
20
    GetOptions( "host=s"    => \$host,          #
24
            "port=i"    => \$port,          #
21
            "port=i"    => \$port,          #
25
                        );
22
                        );
26
}
23
}
27
 
24
 
28
my $path = dirname(abs_path($0));
25
my $path = dirname(abs_path($0));
-
 
26
 
-
 
27
# Read atom functions from separate file
-
 
28
if (-e $path."/AtomLib.pl") {
-
 
29
    require $path."/AtomLib.pl";
-
 
30
} else {
-
 
31
    print "Error: No AtomLib.pl was found in ".$path.". \n\n";
-
 
32
    exit 1;
-
 
33
}
29
 
34
 
30
# Read devices from separate file
35
# Read devices from separate file
31
if (-e $path."/devices.pl") {
36
if (-e $path."/devices.pl") {
32
    require $path."/devices.pl";
37
    require $path."/devices.pl";
33
} else {
38
} else {
Line 50... Line 55...
50
my $foundbootrow = 0;
55
my $foundbootrow = 0;
51
foreach $file (@file) {
56
foreach $file (@file) {
52
    if ($file =~ m/^NODE_HW_ID=/) {
57
    if ($file =~ m/^NODE_HW_ID=/) {
53
        print "\nCurrent id-setting: ".$file."\n";
58
        print "\nCurrent id-setting: ".$file."\n";
54
        $foundidrow = 1;
59
        $foundidrow = 1;
55
    }
60
    }
56
    if ($file =~ m/^MCU=/) {
61
    if ($file =~ m/^MCU=/) {
57
        $foundmcurow = 1;
62
        $foundmcurow = 1;
58
    }
63
    }
59
    if ($file =~ m/^HIGHFUSE=/) {
64
    if ($file =~ m/^HIGHFUSE=/) {
60
        $foundhfrow = 1;
65
        $foundhfrow = 1;
61
    }
66
    }
62
    if ($file =~ m/^LOWFUSE=/) {
67
    if ($file =~ m/^LOWFUSE=/) {
63
        $foundlfrow = 1;
68
        $foundlfrow = 1;
64
    }
69
    }
65
    if ($file =~ m/^EXTFUSE=/) {
70
    if ($file =~ m/^EXTFUSE=/) {
66
        $foundefrow = 1;
71
        $foundefrow = 1;
67
    }
72
    }
68
    if ($file =~ m/^BOOT_START=/) {
73
    if ($file =~ m/^BOOT_START=/) {
69
        $foundbootrow = 1;
74
        $foundbootrow = 1;
70
    }
75
    }
71
}
76
}
72
if ($foundidrow == 0) {
77
if ($foundidrow == 0) {
73
    print "Error: Did not find NODE_HW_ID= in file $biosfile\n";
78
    print "Error: Did not find NODE_HW_ID= in file $biosfile\n";
74
    exit 1;
79
    exit 1;
75
}
80
}
76
if ($foundmcurow == 0) {
81
if ($foundmcurow == 0) {
77
    print "Error: Did not find MCU= in file $biosfile\n";
82
    print "Error: Did not find MCU= in file $biosfile\n";
78
    exit 1;
83
    exit 1;
79
}
84
}
80
if ($foundhfrow == 0) {
85
if ($foundhfrow == 0) {
81
    print "Error: Did not find HIGHFUSE= in file $biosfile\n";
86
    print "Error: Did not find HIGHFUSE= in file $biosfile\n";
82
    exit 1;
87
    exit 1;
83
}
88
}
84
if ($foundlfrow == 0) {
89
if ($foundlfrow == 0) {
85
    print "Error: Did not find LOWFUSE= in file $biosfile\n";
90
    print "Error: Did not find LOWFUSE= in file $biosfile\n";
86
    exit 1;
91
    exit 1;
87
}
92
}
88
if ($foundefrow == 0) {
93
if ($foundefrow == 0) {
89
    print "Error: Did not find EXTFUSE= in file $biosfile\n";
94
    print "Error: Did not find EXTFUSE= in file $biosfile\n";
90
    exit 1;
95
    exit 1;
91
}
96
}
92
if ($foundbootrow == 0) {
97
if ($foundbootrow == 0) {
93
    print "Error: Did not find BOOT_START= in file $biosfile\n";
98
    print "Error: Did not find BOOT_START= in file $biosfile\n";
94
    exit 1;
99
    exit 1;
95
}
100
}
96
 
101
 
97
my $hwid = "";
102
my $hwid = "";
98
my @device = ();
103
my @device = ();
99
 
104
 
100
#print "/usr/bin/atomic -s $host -p $port -c \"Node_WaitForInformation\"\n";
105
#print "/usr/bin/atomic -s $host -p $port -c \"Node_WaitForInformation\"\n";
101
#my @lines = qx{/usr/bin/atomic -s $host -p $port -c \"Node_GetInformation 0xB23F54EA\"};
106
#my @lines = qx{/usr/bin/atomic -s $host -p $port -c \"Node_GetInformation 0xB23F54EA\"};
102
#my @lines = qx{/usr/bin/atomic -s $host -p $port -c \"Node_WaitForInformation\"};
107
#my @lines = qx{/usr/bin/atomic -s $host -p $port -c \"Node_WaitForInformation\"};
103
 
108
 
104
$socket = atomd_initialize($host, $port);
109
$socket = atomd_initialize($host, $port);
105
atomd_send_command($socket, "Node_WaitForInformation");
110
atomd_send_command($socket, "Node_WaitForInformation");
-
 
111
#atomd_send_command($socket, "Node_GetInformation 0x7DD44E0A");
106
$return = atomd_read_command_response($socket);
112
$return = atomd_read_command_response($socket);
107
 
113
 
108
my @lines = split(/\n/, $return);
114
my @lines = split(/\n/, $return);
109
 
115
 
-
 
116
my $foundDeviceType = 0;
110
foreach (@lines)
117
foreach (@lines)
111
{
118
{
112
    my $line = $_;
119
    my $line = $_;
113
    print($line."\n");
120
    print($line."\n");
114
    chomp($line);
121
    chomp($line);
115
   
122
   
116
    if (index($line, "Id") != -1)
123
    if (index($line, "Id") != -1)
117
    {
124
    {
118
        my $temp = substr($line, index($line, ":") + 1);
125
        my $temp = substr($line, index($line, ":") + 1);
119
   
126
   
120
        $temp =~ s/^\s+//;
127
        $temp =~ s/^\s+//;
121
        $temp =~ s/\s+$//;
128
        $temp =~ s/\s+$//;
122
 
129
 
123
        my @hwidChars = split(//, $temp);
130
        my @hwidChars = split(//, $temp);
124
        $hwid = $hwidChars[0]. $hwidChars[1]. $hwidChars[8]. $hwidChars[9]. $hwidChars[6]. $hwidChars[7]. $hwidChars[4]. $hwidChars[5]. $hwidChars[2]. $hwidChars[3];
131
        $hwid = $hwidChars[0]. $hwidChars[1]. $hwidChars[8]. $hwidChars[9]. $hwidChars[6]. $hwidChars[7]. $hwidChars[4]. $hwidChars[5]. $hwidChars[2]. $hwidChars[3];
125
 
132
 
126
        print "Setting HWID in bios.inc to ".$hwid."\n";
133
        print "Setting HWID in bios.inc to ".$hwid."\n";
127
    }
134
    }
128
    elsif (index($line, "Device Type") != -1)
135
    elsif (index($line, "Device Type") != -1)
129
    {
136
    {
130
        $devicename = substr($line, index($line, ":") + 1);
137
        $devicename = substr($line, index($line, ":") + 1);
131
   
138
   
132
        $devicename =~ s/^\s+//;
139
        $devicename =~ s/^\s+//;
133
        $devicename =~ s/\s+$//;
140
        $devicename =~ s/\s+$//;
134
   
141
   
135
        @device = $devices[0];
142
        @device = $devices[0];
136
        foreach (@devices)
143
        foreach (@devices)
137
        {
144
        {
138
            if (@{$_}[0] eq $devicename)
145
            if (@{$_}[0] eq $devicename)
139
            {
146
            {
140
                @device = @{$_};
147
                @device = @{$_};
-
 
148
                $foundDeviceType = 1;
-
 
149
            }
141
            }
150
        }
142
        }
-
 
143
       
151
       
144
        if ($device[0] eq "unknown")
152
        if ($device[0] eq "unknown")
145
        {
153
        {
146
            print "\nWarning: Unknown device type, will not set MCU settings\n\n";
154
            print "\nWarning: Unknown device type, will not set MCU settings\n\n";
147
        }
155
        }
148
        else
156
        else
149
        {
157
        {
150
            print "Setting device type to ".$device[0]."\n";
158
            print "Setting device type to ".$device[0]."\n";
151
        }
159
        }
152
    }
160
    }
153
}
161
}
154
 
162
 
155
if ($hwid eq "")
163
if ($hwid eq "" || $foundDeviceType == 0)
156
{
164
{
157
    exit(1);
165
    exit(1);
158
}
166
}
159
 
167
 
160
# Open bios.inc and replace all settings
168
# Open bios.inc and replace all settings
161
open (FP, "<".$biosfile) or die "Cannot open file $biosfile\n";
169
open (FP, "<".$biosfile) or die "Cannot open file $biosfile\n";
162
@file = <FP>;
170
@file = <FP>;
163
close FP;
171
close FP;
164
open (FP, ">".$biosfile) or die "Cannot open file $biosfile\n";
172
open (FP, ">".$biosfile) or die "Cannot open file $biosfile\n";
Line 169... Line 177...
169
        $file =~ s/^MCU=.*/MCU=$device[0]/g;
177
        $file =~ s/^MCU=.*/MCU=$device[0]/g;
170
        $file =~ s/^HIGHFUSE=.*/HIGHFUSE=$device[1]/g;
178
        $file =~ s/^HIGHFUSE=.*/HIGHFUSE=$device[1]/g;
171
        $file =~ s/^LOWFUSE=.*/LOWFUSE=$device[2]/g;
179
        $file =~ s/^LOWFUSE=.*/LOWFUSE=$device[2]/g;
172
        $file =~ s/^EXTFUSE=.*/EXTFUSE=$device[3]/g;
180
        $file =~ s/^EXTFUSE=.*/EXTFUSE=$device[3]/g;
173
        $file =~ s/^BOOT_START=.*/BOOT_START=$device[4]/g;
181
        $file =~ s/^BOOT_START=.*/BOOT_START=$device[4]/g;
174
    }
182
    }
175
    print FP $file;
183
    print FP $file;
176
}
184
}
177
close FP;
185
close FP;
178
 
186
 
179
exit(0);
187
exit(0);
180
 
-
 
181
 
-
 
182
# TODO: Move all atomic-protocol functions to a separate file/lib
-
 
183
 
-
 
184
sub atomd_connect
-
 
185
{
-
 
186
    ($host, $port) = @_;
-
 
187
    $socket = IO::Socket::INET->new(
-
 
188
            Proto    => "tcp",
-
 
189
            PeerAddr => $host,
-
 
190
            PeerPort => $port,
-
 
191
            Blocking => 1,
-
 
192
        )
-
 
193
        or die "Error: Cannot connect to port $port at $host\n";
-
 
194
 
-
 
195
    return $socket;
-
 
196
}
-
 
197
 
-
 
198
sub atomd_read_packet
-
 
199
{
-
 
200
    ($socket) = @_;
-
 
201
    $command="";
-
 
202
    #while (length($command)<4)
-
 
203
    #{
-
 
204
        #sysread blocks and reads at least 1 byte before returning
-
 
205
    #   $bytes = sysread($socket, $buf, 1);
-
 
206
    #   $command .= $buf;
-
 
207
    #}
-
 
208
    read($socket, $command, 4);
-
 
209
 
-
 
210
    $payload_length="";
-
 
211
    #while (length($payload_length)<4)
-
 
212
    #{
-
 
213
    #   $bytes = sysread($socket, $buf, 1);
-
 
214
    #   $payload_length .= $buf;
-
 
215
    #}
-
 
216
    read($socket, $payload_length, 4);
-
 
217
 
-
 
218
    $payload_length = int($payload_length);
-
 
219
    $payload="";
-
 
220
    if ($payload_length > 0)
-
 
221
    {
-
 
222
        #while (length($payload) < $payload_length)
-
 
223
        #{
-
 
224
        #   $bytes = sysread($socket, $buf, 1);
-
 
225
        #   $payload .= $buf;
-
 
226
        #}
-
 
227
        read($socket, $payload, $payload_length);
-
 
228
    }
-
 
229
   
-
 
230
    $payload_length = sprintf("%04d", $payload_length);
-
 
231
   
-
 
232
    #print "areadpacket ".$command . $payload_length . $payload."\n";
-
 
233
    return $command . $payload_length . $payload;
-
 
234
}
-
 
235
 
-
 
236
sub atomd_write_packet
-
 
237
{
-
 
238
    ($socket, $command, $payload) = @_;
-
 
239
   
-
 
240
    $payload_length = sprintf("%04d", length($payload)+1);
-
 
241
    $packet = $command.$payload_length.$payload.chr(0);
-
 
242
 
-
 
243
    #print "awritepacket ".$packet."\n";
-
 
244
    print $socket $packet;
-
 
245
}
-
 
246
 
-
 
247
 
-
 
248
sub atomd_data_available
-
 
249
{
-
 
250
    ($socket) = @_;
-
 
251
    #Get the current buffer position
-
 
252
    $position = tell($socket);
-
 
253
 
-
 
254
    #Set the current position to end of buffer
-
 
255
    seek($socket, -1, SEEK_END);
-
 
256
 
-
 
257
    #Compare end postion to stored position
-
 
258
    $has_data = tell($socket) > $position;
-
 
259
 
-
 
260
    #Restore buffer position
-
 
261
    seek($socket, $position, SEEK_SET);
-
 
262
 
-
 
263
    return $has_data;
-
 
264
}
-
 
265
 
-
 
266
 
-
 
267
sub atomd_initialize
-
 
268
{
-
 
269
    ($host, $port) = @_;
-
 
270
    $socket = atomd_connect($host, $port);
-
 
271
   
-
 
272
    #while (!atomd_data_available($socket)) {}
-
 
273
   
-
 
274
    #while (atomd_data_available($socket))
-
 
275
    #{
-
 
276
    #   $packet = atomd_read_packet($socket); # Read initial prompt
-
 
277
    #}
-
 
278
   
-
 
279
    # Read initial prompt
-
 
280
    $s = IO::Select->new();
-
 
281
    $s->add($socket);
-
 
282
    $s->can_read(1);
-
 
283
   
-
 
284
    sysread($socket, $data, 9000);
-
 
285
    $data="";
-
 
286
    #print "ainit".$data."\n";
-
 
287
    return $socket;
-
 
288
}
-
 
289
 
-
 
290
 
-
 
291
sub atomd_send_command
-
 
292
{
-
 
293
    ($socket, $command) = @_;
-
 
294
    atomd_write_packet($socket, "RESP", $command);
-
 
295
}
-
 
296
 
-
 
297
 
-
 
298
sub atomd_read_command_response
-
 
299
{
-
 
300
    ($socket) = @_;
-
 
301
 
-
 
302
    $response = "";
-
 
303
 
-
 
304
    while (1)
-
 
305
    {
-
 
306
        $packet = atomd_read_packet($socket);
-
 
307
 
-
 
308
        if (substr($packet, 0, 4) ne "TEXT")
-
 
309
        {
-
 
310
            last;
-
 
311
        }
-
 
312
 
-
 
313
        $response .= substr($packet, 8) . "\n";
-
 
314
    }
-
 
315
 
-
 
316
    return $response;
-
 
317
}
-
 
318
 
188