Subversion Repositories HomeAutomation

Rev

Rev 1667 | Rev 1878 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 1667 Rev 1668
1
#!/usr/bin/perl -w
1
#!/usr/bin/perl -w
2
################################################
2
################################################
3
#
3
#
4
# author: arune @ efnet
4
# author: arune @ efnet
5
#
5
#
6
################################################
6
################################################
7
 
7
 
8
use IO::Socket;
8
use IO::Socket;
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
 
14
 
15
$host = "localhost";
15
$host = "localhost";
16
$port = 1201;
16
$port = 1201;
17
 
17
 
18
# Get hostname and port from command line arguments
18
# Get hostname and port from command line arguments
19
if ( @ARGV > 0 ) {
19
if ( @ARGV > 0 ) {
20
    GetOptions( "host=s"    => \$host,          #
20
    GetOptions( "host=s"    => \$host,          #
21
            "port=i"    => \$port,          #
21
            "port=i"    => \$port,          #
22
                        );
22
                        );
23
}
23
}
24
 
24
 
25
my $path = dirname(abs_path($0));
25
my $path = dirname(abs_path($0));
26
 
26
 
27
# Read devices from separate file
27
# Read devices from separate file
28
if (-e $path."/devices.pl") {
28
if (-e $path."/devices.pl") {
29
    require $path."/devices.pl";
29
    require $path."/devices.pl";
30
} else {
30
} else {
31
    print "Error: No devices.pl was found in ".$path.". \n\n";
31
    print "Error: No devices.pl was found in ".$path.". \n\n";
32
    exit 1;
32
    exit 1;
33
}
33
}
34
 
34
 
35
$biosfile = "bios.inc";
35
$biosfile = "bios.inc";
36
open (FP, "+<".$biosfile) or die "Error: Cannot open file $biosfile\n";
36
open (FP, "+<".$biosfile) or die "Error: Cannot open file $biosfile\n";
37
@file = <FP>;
37
@file = <FP>;
38
close FP;
38
close FP;
39
 
39
 
40
 
40
 
41
# Check bios.inc for the settings that should be updated
41
# Check bios.inc for the settings that should be updated
42
my $foundidrow = 0;
42
my $foundidrow = 0;
43
my $foundmcurow = 0;
43
my $foundmcurow = 0;
44
my $foundhfrow = 0;
44
my $foundhfrow = 0;
45
my $foundlfrow = 0;
45
my $foundlfrow = 0;
46
my $foundefrow = 0;
46
my $foundefrow = 0;
47
my $foundbootrow = 0;
47
my $foundbootrow = 0;
48
foreach $file (@file) {
48
foreach $file (@file) {
49
    if ($file =~ m/^NODE_HW_ID=/) {
49
    if ($file =~ m/^NODE_HW_ID=/) {
50
        print "\nCurrent id-setting: ".$file."\n";
50
        print "\nCurrent id-setting: ".$file."\n";
51
        $foundidrow = 1;
51
        $foundidrow = 1;
52
    }
52
    }
53
    if ($file =~ m/^MCU=/) {
53
    if ($file =~ m/^MCU=/) {
54
        $foundmcurow = 1;
54
        $foundmcurow = 1;
55
    }
55
    }
56
    if ($file =~ m/^HIGHFUSE=/) {
56
    if ($file =~ m/^HIGHFUSE=/) {
57
        $foundhfrow = 1;
57
        $foundhfrow = 1;
58
    }
58
    }
59
    if ($file =~ m/^LOWFUSE=/) {
59
    if ($file =~ m/^LOWFUSE=/) {
60
        $foundlfrow = 1;
60
        $foundlfrow = 1;
61
    }
61
    }
62
    if ($file =~ m/^EXTFUSE=/) {
62
    if ($file =~ m/^EXTFUSE=/) {
63
        $foundefrow = 1;
63
        $foundefrow = 1;
64
    }
64
    }
65
    if ($file =~ m/^BOOT_START=/) {
65
    if ($file =~ m/^BOOT_START=/) {
66
        $foundbootrow = 1;
66
        $foundbootrow = 1;
67
    }
67
    }
68
}
68
}
69
if ($foundidrow == 0) {
69
if ($foundidrow == 0) {
70
    print "Error: Did not find NODE_HW_ID= in file $biosfile\n";
70
    print "Error: Did not find NODE_HW_ID= in file $biosfile\n";
71
    exit 1;
71
    exit 1;
72
}
72
}
73
if ($foundmcurow == 0) {
73
if ($foundmcurow == 0) {
74
    print "Error: Did not find MCU= in file $biosfile\n";
74
    print "Error: Did not find MCU= in file $biosfile\n";
75
    exit 1;
75
    exit 1;
76
}
76
}
77
if ($foundhfrow == 0) {
77
if ($foundhfrow == 0) {
78
    print "Error: Did not find HIGHFUSE= in file $biosfile\n";
78
    print "Error: Did not find HIGHFUSE= in file $biosfile\n";
79
    exit 1;
79
    exit 1;
80
}
80
}
81
if ($foundlfrow == 0) {
81
if ($foundlfrow == 0) {
82
    print "Error: Did not find LOWFUSE= in file $biosfile\n";
82
    print "Error: Did not find LOWFUSE= in file $biosfile\n";
83
    exit 1;
83
    exit 1;
84
}
84
}
85
if ($foundefrow == 0) {
85
if ($foundefrow == 0) {
86
    print "Error: Did not find EXTFUSE= in file $biosfile\n";
86
    print "Error: Did not find EXTFUSE= in file $biosfile\n";
87
    exit 1;
87
    exit 1;
88
}
88
}
89
if ($foundbootrow == 0) {
89
if ($foundbootrow == 0) {
90
    print "Error: Did not find BOOT_START= in file $biosfile\n";
90
    print "Error: Did not find BOOT_START= in file $biosfile\n";
91
    exit 1;
91
    exit 1;
92
}
92
}
93
 
93
 
94
my $hwid = "";
94
my $hwid = "";
95
my @device = ();
95
my @device = ();
96
 
96
 
97
print "/usr/bin/atomic -s $host -p $port -c \"Node_WaitForInformation\"\n";
97
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\"};
98
my @lines = qx{/usr/bin/atomic -s $host -p $port -c \"Node_GetInformation 0xB23F54EA\"};
99
 
99
 
100
foreach (@lines)
100
foreach (@lines)
101
{
101
{
102
    my $line = $_;
102
    my $line = $_;
103
    print($_);
103
    print($_);
104
    chomp($line);
104
    chomp($line);
105
   
105
   
106
    if (index($line, "Id") != -1)
106
    if (index($line, "Id") != -1)
107
    {
107
    {
108
        my $temp = substr($line, index($line, ":") + 1);
108
        my $temp = substr($line, index($line, ":") + 1);
109
   
109
   
110
        $temp =~ s/^\s+//;
110
        $temp =~ s/^\s+//;
111
        $temp =~ s/\s+$//;
111
        $temp =~ s/\s+$//;
112
 
112
 
113
        my @hwidChars = split(//, $temp);
113
        my @hwidChars = split(//, $temp);
114
        $hwid = $hwidChars[0]. $hwidChars[1]. $hwidChars[8]. $hwidChars[9]. $hwidChars[6]. $hwidChars[7]. $hwidChars[4]. $hwidChars[5]. $hwidChars[2]. $hwidChars[3];
114
        $hwid = $hwidChars[0]. $hwidChars[1]. $hwidChars[8]. $hwidChars[9]. $hwidChars[6]. $hwidChars[7]. $hwidChars[4]. $hwidChars[5]. $hwidChars[2]. $hwidChars[3];
115
 
115
 
116
        print "Setting HWID in bios.inc to ".$hwid."\n";
116
        print "Setting HWID in bios.inc to ".$hwid."\n";
117
    }
117
    }
118
    elsif (index($line, "DeviceType") != -1)
118
    elsif (index($line, "DeviceType") != -1)
119
    {
119
    {
120
        $devicename = substr($line, index($line, ":") + 1);
120
        $devicename = substr($line, index($line, ":") + 1);
121
   
121
   
122
        $devicename =~ s/^\s+//;
122
        $devicename =~ s/^\s+//;
123
        $devicename =~ s/\s+$//;
123
        $devicename =~ s/\s+$//;
124
   
124
   
125
        @device = $devices[0];
125
        @device = $devices[0];
126
       
126
       
127
        foreach (@devices)
127
        foreach (@devices)
128
        {
128
        {
129
            if (@{$_}[5] eq $devicename)
129
            if (@{$_}[0] eq $devicename)
130
            {
130
            {
131
                @device = @{$_};
131
                @device = @{$_};
132
            }
132
            }
133
        }
133
        }
134
       
134
       
135
        if ($device[0] eq "N/A")
135
        if ($device[0] eq "unknown")
136
        {
136
        {
137
            print "\nWarning: Unknown device type, will not set MCU settings\n\n";
137
            print "\nWarning: Unknown device type, will not set MCU settings\n\n";
138
        }
138
        }
139
        else
139
        else
140
        {
140
        {
141
            print "Setting device type to ".$device[0]."\n";
141
            print "Setting device type to ".$device[0]."\n";
142
        }
142
        }
143
    }
143
    }
144
}
144
}
145
 
145
 
146
if ($hwid eq "")
146
if ($hwid eq "")
147
{
147
{
148
    exit(1);
148
    exit(1);
149
}
149
}
150
 
150
 
151
# Open bios.inc and replace all settings
151
# Open bios.inc and replace all settings
152
open (FP, "<".$biosfile) or die "Cannot open file $biosfile\n";
152
open (FP, "<".$biosfile) or die "Cannot open file $biosfile\n";
153
@file = <FP>;
153
@file = <FP>;
154
close FP;
154
close FP;
155
open (FP, ">".$biosfile) or die "Cannot open file $biosfile\n";
155
open (FP, ">".$biosfile) or die "Cannot open file $biosfile\n";
156
foreach $file (@file) {
156
foreach $file (@file) {
157
    $file =~ s/^NODE_HW_ID=.*/NODE_HW_ID=$hwid/g;
157
    $file =~ s/^NODE_HW_ID=.*/NODE_HW_ID=$hwid/g;
158
    if ($device[0] ne "N/A")
158
    if ($device[0] ne "unknown")
159
    {
159
    {
160
        $file =~ s/^MCU=.*/MCU=$device[0]/g;
160
        $file =~ s/^MCU=.*/MCU=$device[0]/g;
161
        $file =~ s/^HIGHFUSE=.*/HIGHFUSE=$device[1]/g;
161
        $file =~ s/^HIGHFUSE=.*/HIGHFUSE=$device[1]/g;
162
        $file =~ s/^LOWFUSE=.*/LOWFUSE=$device[2]/g;
162
        $file =~ s/^LOWFUSE=.*/LOWFUSE=$device[2]/g;
163
        $file =~ s/^EXTFUSE=.*/EXTFUSE=$device[3]/g;
163
        $file =~ s/^EXTFUSE=.*/EXTFUSE=$device[3]/g;
164
        $file =~ s/^BOOT_START=.*/BOOT_START=$device[4]/g;
164
        $file =~ s/^BOOT_START=.*/BOOT_START=$device[4]/g;
165
    }
165
    }
166
    print FP $file;
166
    print FP $file;
167
}
167
}
168
close FP;
168
close FP;
169
 
169
 
170
exit 0;
170
exit(0);
171
 
171
 
172
 
172
 
173
 
173