Subversion Repositories HomeAutomation

Rev

Rev 2187 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 2187 Rev 2189
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 integer;
8
use integer;
9
use threads;
9
use threads;
10
use Cwd qw(abs_path);
10
use Cwd qw(abs_path);
11
use File::Basename;
11
use File::Basename;
12
use Getopt::Long;
12
use Getopt::Long;
13
use Time::HiRes qw(usleep nanosleep);
13
use Time::HiRes qw(usleep nanosleep);
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 atom functions from separate file
27
# Read atom functions from separate file
28
if (-e $path."/AtomLib.pl") {
28
if (-e $path."/AtomLib.pl") {
29
    require $path."/AtomLib.pl";
29
    require $path."/AtomLib.pl";
30
} else {
30
} else {
31
    print "Error: No AtomLib.pl was found in ".$path.". \n\n";
31
    print "Error: No AtomLib.pl was found in ".$path.". \n\n";
32
    exit 1;
32
    exit 1;
33
}
33
}
34
 
34
 
35
# Read devices from separate file
35
# Read devices from separate file
36
if (-e $path."/devices.pl") {
36
if (-e $path."/devices.pl") {
37
    require $path."/devices.pl";
37
    require $path."/devices.pl";
38
} else {
38
} else {
39
    print "Error: No devices.pl was found in ".$path.". \n\n";
39
    print "Error: No devices.pl was found in ".$path.". \n\n";
40
    exit 1;
40
    exit 1;
41
}
41
}
42
 
42
 
43
$biosfile = "bios.inc";
43
$biosfile = "bios.inc";
44
open (FP, "+<".$biosfile) or die "Error: Cannot open file $biosfile\n";
44
open (FP, "+<".$biosfile) or die "Error: Cannot open file $biosfile\n";
45
@file = <FP>;
45
@file = <FP>;
46
close FP;
46
close FP;
47
 
47
 
48
 
48
 
49
# Check bios.inc for the settings that should be updated
49
# Check bios.inc for the settings that should be updated
50
my $foundidrow = 0;
50
my $foundidrow = 0;
51
my $foundmcurow = 0;
51
my $foundmcurow = 0;
52
my $foundhfrow = 0;
52
my $foundhfrow = 0;
53
my $foundlfrow = 0;
53
my $foundlfrow = 0;
54
my $foundefrow = 0;
54
my $foundefrow = 0;
55
my $foundbootrow = 0;
55
my $foundbootrow = 0;
56
foreach $file (@file) {
56
foreach $file (@file) {
57
    if ($file =~ m/^NODE_HW_ID=/) {
57
    if ($file =~ m/^NODE_HW_ID=/) {
58
        print "\nCurrent id-setting: ".$file."\n";
58
        print "\nCurrent id-setting: ".$file."\n";
59
        $foundidrow = 1;
59
        $foundidrow = 1;
60
    }
60
    }
61
    if ($file =~ m/^MCU=/) {
61
    if ($file =~ m/^MCU=/) {
62
        $foundmcurow = 1;
62
        $foundmcurow = 1;
63
    }
63
    }
64
    if ($file =~ m/^HIGHFUSE=/) {
64
    if ($file =~ m/^HIGHFUSE=/) {
65
        $foundhfrow = 1;
65
        $foundhfrow = 1;
66
    }
66
    }
67
    if ($file =~ m/^LOWFUSE=/) {
67
    if ($file =~ m/^LOWFUSE=/) {
68
        $foundlfrow = 1;
68
        $foundlfrow = 1;
69
    }
69
    }
70
    if ($file =~ m/^EXTFUSE=/) {
70
    if ($file =~ m/^EXTFUSE=/) {
71
        $foundefrow = 1;
71
        $foundefrow = 1;
72
    }
72
    }
73
    if ($file =~ m/^BOOT_START=/) {
73
    if ($file =~ m/^BOOT_START=/) {
74
        $foundbootrow = 1;
74
        $foundbootrow = 1;
75
    }
75
    }
76
}
76
}
77
if ($foundidrow == 0) {
77
if ($foundidrow == 0) {
78
    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";
79
    exit 1;
79
    exit 1;
80
}
80
}
81
if ($foundmcurow == 0) {
81
if ($foundmcurow == 0) {
82
    print "Error: Did not find MCU= in file $biosfile\n";
82
    print "Error: Did not find MCU= in file $biosfile\n";
83
    exit 1;
83
    exit 1;
84
}
84
}
85
if ($foundhfrow == 0) {
85
if ($foundhfrow == 0) {
86
    print "Error: Did not find HIGHFUSE= in file $biosfile\n";
86
    print "Error: Did not find HIGHFUSE= in file $biosfile\n";
87
    exit 1;
87
    exit 1;
88
}
88
}
89
if ($foundlfrow == 0) {
89
if ($foundlfrow == 0) {
90
    print "Error: Did not find LOWFUSE= in file $biosfile\n";
90
    print "Error: Did not find LOWFUSE= in file $biosfile\n";
91
    exit 1;
91
    exit 1;
92
}
92
}
93
if ($foundefrow == 0) {
93
if ($foundefrow == 0) {
94
    print "Error: Did not find EXTFUSE= in file $biosfile\n";
94
    print "Error: Did not find EXTFUSE= in file $biosfile\n";
95
    exit 1;
95
    exit 1;
96
}
96
}
97
if ($foundbootrow == 0) {
97
if ($foundbootrow == 0) {
98
    print "Error: Did not find BOOT_START= in file $biosfile\n";
98
    print "Error: Did not find BOOT_START= in file $biosfile\n";
99
    exit 1;
99
    exit 1;
100
}
100
}
101
 
101
 
102
my $hwid = "";
102
my $hwid = "";
103
my @device = ();
103
my @device = ();
104
 
104
 
105
#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";
106
#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\"};
107
#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\"};
108
my @lines;
108
my @lines;
-
 
109
 
-
 
110
print "Abort with Ctrl+c\n";
109
 
111
 
110
$socket = atomd_initialize($host, $port);
112
$socket = atomd_initialize($host, $port);
111
if ($port == 1203)
113
if ($port == 1203)
112
{
114
{
113
    atomjs_write($socket, "Node_WaitForInformationNative();\n");
115
    atomjs_write($socket, "Node_WaitForInformationNative();\n");
114
 
116
 
115
    $success = 0;
117
    $success = 0;
116
    for ($count = 0; $count < 50; $count++)
118
    for ($count = 0; $count < 300; $count++)
117
    {
119
    {
118
        atomjs_write($socket, "Node_WaitNodePollNative();\n");
120
        atomjs_write($socket, "Node_WaitNodePollNative();\n");
119
        $return = atomjs_read_line($socket);
121
        $return = atomjs_read_line($socket);
120
        if ($return =~ m/false/)
122
        if (($return =~ m/NoInfo/) || ($return =~ m/undefined/))
121
        {
123
        {
122
        }
124
        }
123
        elsif ($return =~ m/Error/)
125
        elsif ($return =~ m/Error/)
124
        {
126
        {
125
            die $return;
127
            die $return;
126
        }
128
        }
127
        else
129
        else
128
        {
130
        {
129
            @lines = split(/\t/, $return);
131
            @lines = split(/\t/, $return);
-
 
132
            $success = 1;
-
 
133
            last;
130
        }
134
        }
131
        usleep(200000);
135
        usleep(200000);
132
    }
136
    }
133
    if ($success == 0)
137
    if ($success == 0)
134
    {
138
    {
135
        die "\033[31mTimed out waiting for node to start.\033[0m\n";
139
        die "\033[31mTimed out waiting for node to start.\033[0m\n";
136
    }
140
    }
137
 
-
 
138
}
141
}
139
else
142
else
140
{
143
{
141
    usleep(100000);
144
    usleep(100000);
142
    atomd_kill_promt($socket);
145
    atomd_kill_promt($socket);
143
    atomd_send_command($socket, "Node_WaitForInformation");
146
    atomd_send_command($socket, "Node_WaitForInformation");
144
    #atomd_send_command($socket, "Node_GetInformation 0x7DD44E0A");
147
    #atomd_send_command($socket, "Node_GetInformation 0x7DD44E0A");
145
    $return = atomd_read_command_response($socket);
148
    $return = atomd_read_command_response($socket);
146
    @lines = split(/\n/, $return);
149
    @lines = split(/\n/, $return);
147
}
150
}
148
 
151
 
149
my $foundDeviceType = 0;
152
my $foundDeviceType = 0;
150
foreach (@lines)
153
foreach (@lines)
151
{
154
{
152
    my $line = $_;
155
    my $line = $_;
153
    print($line."\n");
156
    print($line."\n");
154
    chomp($line);
157
    chomp($line);
155
   
158
   
156
    if (index($line, "Id") != -1)
159
    if (index($line, "Id") != -1)
157
    {
160
    {
158
        my $temp = substr($line, index($line, ":") + 1);
161
        my $temp = substr($line, index($line, ":") + 1);
159
   
162
   
160
        $temp =~ s/^\s+//;
163
        $temp =~ s/^\s+//;
161
        $temp =~ s/\s+$//;
164
        $temp =~ s/\s+$//;
162
 
165
 
163
        my @hwidChars = split(//, $temp);
166
        my @hwidChars = split(//, $temp);
164
        $hwid = $hwidChars[0]. $hwidChars[1]. $hwidChars[8]. $hwidChars[9]. $hwidChars[6]. $hwidChars[7]. $hwidChars[4]. $hwidChars[5]. $hwidChars[2]. $hwidChars[3];
167
        $hwid = $hwidChars[0]. $hwidChars[1]. $hwidChars[8]. $hwidChars[9]. $hwidChars[6]. $hwidChars[7]. $hwidChars[4]. $hwidChars[5]. $hwidChars[2]. $hwidChars[3];
165
 
168
 
166
        print "Setting HWID in bios.inc to ".$hwid."\n";
169
        print "\033[32mSetting HWID in bios.inc to ".$hwid."\033[0m\n";
167
    }
170
    }
168
    elsif (index($line, "Device Type") != -1)
171
    elsif (index($line, "Device Type") != -1)
169
    {
172
    {
170
        $devicename = substr($line, index($line, ":") + 1);
173
        $devicename = substr($line, index($line, ":") + 1);
171
   
174
   
172
        $devicename =~ s/^\s+//;
175
        $devicename =~ s/^\s+//;
173
        $devicename =~ s/\s+$//;
176
        $devicename =~ s/\s+$//;
174
       
177
       
175
        @device = $devices[0];
178
        @device = $devices[0];
176
        foreach (@devices)
179
        foreach (@devices)
177
        {
180
        {
178
            if (@{$_}[0] eq $devicename)
181
            if (@{$_}[0] eq $devicename)
179
            {
182
            {
180
                @device = @{$_};
183
                @device = @{$_};
181
                $foundDeviceType = 1;
184
                $foundDeviceType = 1;
182
            }
185
            }
183
        }
186
        }
184
       
187
       
185
        if ($foundDeviceType == 0)
188
        if ($foundDeviceType == 0)
186
        {
189
        {
187
            print "\nWarning: Unknown device type, will not set MCU settings\n\n";
190
            print "\nWarning: Unknown device type, will not set MCU settings\n\n";
188
        }
191
        }
189
        else
192
        else
190
        {
193
        {
191
            print "Setting device type to ".$device[0]."\n";
194
            print "Setting device type to ".$device[0]."\n";
192
        }
195
        }
193
    }
196
    }
194
}
197
}
195
 
198
 
196
if ($hwid eq "" || $foundDeviceType == 0)
199
if ($hwid eq "" || $foundDeviceType == 0)
197
{
200
{
198
    exit(1);
201
    exit(1);
199
}
202
}
200
 
203
 
201
# Open bios.inc and replace all settings
204
# Open bios.inc and replace all settings
202
open (FP, "<".$biosfile) or die "Cannot open file $biosfile\n";
205
open (FP, "<".$biosfile) or die "Cannot open file $biosfile\n";
203
@file = <FP>;
206
@file = <FP>;
204
close FP;
207
close FP;
205
open (FP, ">".$biosfile) or die "Cannot open file $biosfile\n";
208
open (FP, ">".$biosfile) or die "Cannot open file $biosfile\n";
206
foreach $file (@file) {
209
foreach $file (@file) {
207
    $file =~ s/^NODE_HW_ID=.*/NODE_HW_ID=$hwid/g;
210
    $file =~ s/^NODE_HW_ID=.*/NODE_HW_ID=$hwid/g;
208
    if ($device[0] ne "unknown")
211
    if ($device[0] ne "unknown")
209
    {
212
    {
210
        $file =~ s/^MCU=.*/MCU=$device[0]/g;
213
        $file =~ s/^MCU=.*/MCU=$device[0]/g;
211
        $file =~ s/^HIGHFUSE=.*/HIGHFUSE=$device[1]/g;
214
        $file =~ s/^HIGHFUSE=.*/HIGHFUSE=$device[1]/g;
212
        $file =~ s/^LOWFUSE=.*/LOWFUSE=$device[2]/g;
215
        $file =~ s/^LOWFUSE=.*/LOWFUSE=$device[2]/g;
213
        $file =~ s/^EXTFUSE=.*/EXTFUSE=$device[3]/g;
216
        $file =~ s/^EXTFUSE=.*/EXTFUSE=$device[3]/g;
214
        $file =~ s/^BOOT_START=.*/BOOT_START=$device[4]/g;
217
        $file =~ s/^BOOT_START=.*/BOOT_START=$device[4]/g;
215
    }
218
    }
216
    print FP $file;
219
    print FP $file;
217
}
220
}
218
close FP;
221
close FP;
219
 
222
 
220
exit(0);
223
exit(0);
221
 
224
 
222
 
225