Subversion Repositories HomeAutomation

Rev

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

Rev 1881 Rev 1882
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';
14
 
17
 
15
$host = "localhost";
18
$host = "localhost";
16
$port = 1201;
19
$port = 1201;
17
 
20
 
18
# Get hostname and port from command line arguments
21
# Get hostname and port from command line arguments
19
if ( @ARGV > 0 ) {
22
if ( @ARGV > 0 ) {
20
    GetOptions( "host=s"    => \$host,          #
23
    GetOptions( "host=s"    => \$host,          #
21
            "port=i"    => \$port,          #
24
            "port=i"    => \$port,          #
22
                        );
25
                        );
23
}
26
}
24
 
27
 
25
my $path = dirname(abs_path($0));
28
my $path = dirname(abs_path($0));
26
 
29
 
27
# Read devices from separate file
30
# Read devices from separate file
28
if (-e $path."/devices.pl") {
31
if (-e $path."/devices.pl") {
29
    require $path."/devices.pl";
32
    require $path."/devices.pl";
30
} else {
33
} else {
31
    print "Error: No devices.pl was found in ".$path.". \n\n";
34
    print "Error: No devices.pl was found in ".$path.". \n\n";
32
    exit 1;
35
    exit 1;
33
}
36
}
34
 
37
 
35
$biosfile = "bios.inc";
38
$biosfile = "bios.inc";
36
open (FP, "+<".$biosfile) or die "Error: Cannot open file $biosfile\n";
39
open (FP, "+<".$biosfile) or die "Error: Cannot open file $biosfile\n";
37
@file = <FP>;
40
@file = <FP>;
38
close FP;
41
close FP;
39
 
42
 
40
 
43
 
41
# Check bios.inc for the settings that should be updated
44
# Check bios.inc for the settings that should be updated
42
my $foundidrow = 0;
45
my $foundidrow = 0;
43
my $foundmcurow = 0;
46
my $foundmcurow = 0;
44
my $foundhfrow = 0;
47
my $foundhfrow = 0;
45
my $foundlfrow = 0;
48
my $foundlfrow = 0;
Line 92... Line 95...
92
}
95
}
93
 
96
 
94
my $hwid = "";
97
my $hwid = "";
95
my @device = ();
98
my @device = ();
96
 
99
 
97
print "/usr/bin/atomic -s $host -p $port -c \"Node_WaitForInformation\"\n";
100
#print "/usr/bin/atomic -s $host -p $port -c \"Node_WaitForInformation\"\n";
98
#my @lines = qx{/usr/bin/atomic -s $host -p $port -c \"Node_GetInformation 0xB23F54EA\"};
101
#my @lines = qx{/usr/bin/atomic -s $host -p $port -c \"Node_GetInformation 0xB23F54EA\"};
99
my @lines = qx{/usr/bin/atomic -s $host -p $port -c \"Node_WaitForInformation\"};
102
#my @lines = qx{/usr/bin/atomic -s $host -p $port -c \"Node_WaitForInformation\"};
-
 
103
 
-
 
104
$socket = atomd_initialize($host, $port);
-
 
105
atomd_send_command($socket, "Node_WaitForInformation");
-
 
106
$return = atomd_read_command_response($socket);
-
 
107
 
-
 
108
my @lines = split(/\n/, $return);
100
 
109
 
101
foreach (@lines)
110
foreach (@lines)
102
{
111
{
103
    my $line = $_;
112
    my $line = $_;
104
    print($_);
113
    print($line."\n");
105
    chomp($line);
114
    chomp($line);
106
   
115
   
107
    if (index($line, "Id") != -1)
116
    if (index($line, "Id") != -1)
108
    {
117
    {
109
        my $temp = substr($line, index($line, ":") + 1);
118
        my $temp = substr($line, index($line, ":") + 1);
Line 122... Line 131...
122
   
131
   
123
        $devicename =~ s/^\s+//;
132
        $devicename =~ s/^\s+//;
124
        $devicename =~ s/\s+$//;
133
        $devicename =~ s/\s+$//;
125
   
134
   
126
        @device = $devices[0];
135
        @device = $devices[0];
127
       
-
 
128
        foreach (@devices)
136
        foreach (@devices)
129
        {
137
        {
130
            if (@{$_}[0] eq $devicename)
138
            if (@{$_}[0] eq $devicename)
131
            {
139
            {
132
                @device = @{$_};
140
                @device = @{$_};
133
            }
141
            }
134
        }
142
        }
135
       
143
       
136
        if ($device[0] eq "unknown")
144
        if ($device[0] eq "unknown")
137
        {
145
        {
138
            print "\nWarning: Unknown device type, will not set MCU settings\n\n";
146
            print "\nWarning: Unknown device type, will not set MCU settings\n\n";
139
        }
147
        }
140
        else
148
        else
141
        {
149
        {
142
            print "Setting device type to ".$device[0]."\n";
150
            print "Setting device type to ".$device[0]."\n";
143
        }
151
        }
144
    }
152
    }
145
}
153
}
146
 
154
 
147
if ($hwid eq "")
155
if ($hwid eq "")
148
{
156
{
149
    exit(1);
157
    exit(1);
150
}
158
}
151
 
159
 
152
# Open bios.inc and replace all settings
160
# Open bios.inc and replace all settings
153
open (FP, "<".$biosfile) or die "Cannot open file $biosfile\n";
161
open (FP, "<".$biosfile) or die "Cannot open file $biosfile\n";
154
@file = <FP>;
162
@file = <FP>;
155
close FP;
163
close FP;
156
open (FP, ">".$biosfile) or die "Cannot open file $biosfile\n";
164
open (FP, ">".$biosfile) or die "Cannot open file $biosfile\n";
157
foreach $file (@file) {
165
foreach $file (@file) {
158
    $file =~ s/^NODE_HW_ID=.*/NODE_HW_ID=$hwid/g;
166
    $file =~ s/^NODE_HW_ID=.*/NODE_HW_ID=$hwid/g;
159
    if ($device[0] ne "unknown")
167
    if ($device[0] ne "unknown")
160
    {
168
    {
Line 165... Line 173...
165
        $file =~ s/^BOOT_START=.*/BOOT_START=$device[4]/g;
173
        $file =~ s/^BOOT_START=.*/BOOT_START=$device[4]/g;
166
    }
174
    }
167
    print FP $file;
175
    print FP $file;
168
}
176
}
169
close FP;
177
close FP;
170
 
178
 
171
exit(0);
179
exit(0);
172
 
180
 
-
 
181
 
-
 
182
# TODO: Move all atomic-protocol functions to a separate file/lib
173
 
183
 
174
sub atomd_connect
184
sub atomd_connect
175
{
185
{
176
    ($host, $port) = @_;
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";
177
   
194
 
-
 
195
    return $socket;
178
}
196
}
179
 
197
 
180
sub atomd_read_packet
198
sub atomd_read_packet
181
{
199
{
182
    ($socket) = @_;
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)
183
     
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;
184
}
234
}
185
 
235
 
186
sub atomd_write_packet
236
sub atomd_write_packet
187
{
237
{
188
    ($socket, $command, $payload) = @_;
238
    ($socket, $command, $payload) = @_;
189
   
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;
190
}
245
}
191
 
246
 
192
 
247
 
193
sub atomd_data_available
248
sub atomd_data_available
194
{
249
{
195
    ($socket) = @_;
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;
196
     
259
 
-
 
260
    #Restore buffer position
-
 
261
    seek($socket, $position, SEEK_SET);
-
 
262
 
-
 
263
    return $has_data;
197
}
264
}
198
 
265
 
199
 
266
 
200
sub atomd_initialize
267
sub atomd_initialize
201
{
268
{
202
    ($host, $port) = @_;
269
    ($host, $port) = @_;
203
    $socket = atomd_connect("127.0.0.1", 1202);
270
    $socket = atomd_connect($host, $port);
204
 
-
 
205
    while (atomd_data_available($socket))
-
 
206
    {
-
 
207
        $packet = atomd_read_packet($socket); # Read initial prompt
-
 
208
    }
-
 
209
   
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";
210
    return $socket;
287
    return $socket;
211
}
288
}
212
 
289
 
213
 
290
 
214
sub atomd_send_command
291
sub atomd_send_command
215
{
292
{
216
    ($socket, $command) = @_;
293
    ($socket, $command) = @_;
217
    atomd_write_packet($socket, "RESP", $command);
294
    atomd_write_packet($socket, "RESP", $command);
218
}
295
}
219
 
296
 
220
 
297
 
221
sub atomd_read_command_response
298
sub atomd_read_command_response
222
{
299
{
223
    ($socket) = @_;
300
    ($socket) = @_;
224
     
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;
225
}
317
}
226
 
318