Subversion Repositories HomeAutomation

Rev

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

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