Subversion Repositories HomeAutomation

Rev

Rev 1892 | Rev 2187 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed

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