Subversion Repositories HomeAutomation

Rev

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

Rev 1640 Rev 1643
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 = 1200;
16
$port = 1200;
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
# Length of devices array
35
# Length of devices array
36
$numdevices=@devices;
36
$numdevices=@devices;
37
 
37
 
38
# Connect to candaemon
38
# Connect to candaemon
39
$remote = IO::Socket::INET->new(
39
$remote = IO::Socket::INET->new(
40
                    Proto    => "tcp",
40
                    Proto    => "tcp",
41
                    PeerAddr => $host,
41
                    PeerAddr => $host,
42
                    PeerPort => $port,
42
                    PeerPort => $port,
43
                )
43
                )
44
               or die "Error: Cannot connect to port $port at $host\n";
44
               or die "Error: Cannot connect to port $port at $host\n";
45
 
45
 
46
$biosfile = "bios.inc";
46
$biosfile = "bios.inc";
47
open (FP, "+<".$biosfile) or die "Error: Cannot open file $biosfile\n";
47
open (FP, "+<".$biosfile) or die "Error: Cannot open file $biosfile\n";
48
@file = <FP>;
48
@file = <FP>;
49
close FP;
49
close FP;
50
 
50
 
51
# Check bios.inc for the settings that should be updated
51
# Check bios.inc for the settings that should be updated
52
my $foundidrow = 0;
52
my $foundidrow = 0;
53
my $foundmcurow = 0;
53
my $foundmcurow = 0;
54
my $foundhfrow = 0;
54
my $foundhfrow = 0;
55
my $foundlfrow = 0;
55
my $foundlfrow = 0;
56
my $foundefrow = 0;
56
my $foundefrow = 0;
57
my $foundbootrow = 0;
57
my $foundbootrow = 0;
58
foreach $file (@file) {
58
foreach $file (@file) {
59
    if ($file =~ m/^NODE_HW_ID=/) {
59
    if ($file =~ m/^NODE_HW_ID=/) {
60
        print "\nCurrent id-setting: ".$file."\n";
60
        print "\nCurrent id-setting: ".$file."\n";
61
        $foundidrow = 1;
61
        $foundidrow = 1;
62
    }
62
    }
63
    if ($file =~ m/^MCU=/) {
63
    if ($file =~ m/^MCU=/) {
64
        $foundmcurow = 1;
64
        $foundmcurow = 1;
65
    }
65
    }
66
    if ($file =~ m/^HIGHFUSE=/) {
66
    if ($file =~ m/^HIGHFUSE=/) {
67
        $foundhfrow = 1;
67
        $foundhfrow = 1;
68
    }
68
    }
69
    if ($file =~ m/^LOWFUSE=/) {
69
    if ($file =~ m/^LOWFUSE=/) {
70
        $foundlfrow = 1;
70
        $foundlfrow = 1;
71
    }
71
    }
72
    if ($file =~ m/^EXTFUSE=/) {
72
    if ($file =~ m/^EXTFUSE=/) {
73
        $foundefrow = 1;
73
        $foundefrow = 1;
74
    }
74
    }
75
    if ($file =~ m/^BOOT_START=/) {
75
    if ($file =~ m/^BOOT_START=/) {
76
        $foundbootrow = 1;
76
        $foundbootrow = 1;
77
    }
77
    }
78
}
78
}
79
if ($foundidrow == 0) {
79
if ($foundidrow == 0) {
80
    print "Error: Did not find NODE_HW_ID= in file $biosfile\n";
80
    print "Error: Did not find NODE_HW_ID= in file $biosfile\n";
81
    exit 1;
81
    exit 1;
82
}
82
}
83
if ($foundmcurow == 0) {
83
if ($foundmcurow == 0) {
84
    print "Error: Did not find MCU= in file $biosfile\n";
84
    print "Error: Did not find MCU= in file $biosfile\n";
85
    exit 1;
85
    exit 1;
86
}
86
}
87
if ($foundhfrow == 0) {
87
if ($foundhfrow == 0) {
88
    print "Error: Did not find HIGHFUSE= in file $biosfile\n";
88
    print "Error: Did not find HIGHFUSE= in file $biosfile\n";
89
    exit 1;
89
    exit 1;
90
}
90
}
91
if ($foundlfrow == 0) {
91
if ($foundlfrow == 0) {
92
    print "Error: Did not find LOWFUSE= in file $biosfile\n";
92
    print "Error: Did not find LOWFUSE= in file $biosfile\n";
93
    exit 1;
93
    exit 1;
94
}
94
}
95
if ($foundefrow == 0) {
95
if ($foundefrow == 0) {
96
    print "Error: Did not find EXTFUSE= in file $biosfile\n";
96
    print "Error: Did not find EXTFUSE= in file $biosfile\n";
97
    exit 1;
97
    exit 1;
98
}
98
}
99
if ($foundbootrow == 0) {
99
if ($foundbootrow == 0) {
100
    print "Error: Did not find BOOT_START= in file $biosfile\n";
100
    print "Error: Did not find BOOT_START= in file $biosfile\n";
101
    exit 1;
101
    exit 1;
102
}
102
}
103
 
103
 
104
# Define variable to know if a device was found
104
# Define variable to know if a device was found
105
my $devsettings=0;
105
my $devsettings=0;
106
 
106
 
107
print "Connect your node to the CAN-network (disconnect it first if it's already connected)\n";
107
print "Connect your node to the CAN-network (disconnect it first if it's already connected)\n";
108
print "Abort with Ctrl+c\n";
108
print "Abort with Ctrl+c\n";
109
#read line from socket (blocking)
109
#read line from socket (blocking)
110
while ( $line = <$remote> ) {
110
while ( $line = <$remote> ) {
111
#PKT 00087f00 1 0 39 13 01 20 78 56 34 12
111
#PKT 00087f00 1 0 39 13 01 20 78 56 34 12
112
        if (length($line) > 12) {
112
    if (length($line) > 12) {
-
 
113
        # Find can id
113
                $id = hex(substr($line, 4, 8));
114
        $id = hex(substr($line, 4, 8));
-
 
115
        # Find can id class
114
                $class = ($id & 0x1E000000)>>25;
116
        $class = ($id & 0x1E000000)>>25;
-
 
117
        # If class is nmt
115
                if ($class == 0) {
118
        if ($class == 0) {
-
 
119
            # Find nmt type
116
                    $nmttype = ($id & 0x00FF0000)>>16;
120
            $nmttype = ($id & 0x00FF0000)>>16;
-
 
121
            # If nmt type is bios started
117
                    if ($nmttype == 8) {
122
            if ($nmttype == 8) {
118
                        print $line."";
123
                print $line."";
119
                        #print $class." ".$nmttype."\n";
-
 
120
                        if (length($line) < 40) {
124
                if (length($line) < 40) {
121
                            print "Error: A node was started but it did not have a HWID\n";
125
                    print "Error: A node was started but it did not have a HWID\n";
122
                            exit 1;
126
                    exit 1;
123
                        }
127
                }
-
 
128
               
-
 
129
                # Find hw id from data field
124
                        $hwid = (hex(substr($line, 38, 2))<<24)+(hex(substr($line, 35, 2))<<16)+
130
                $hwid = (hex(substr($line, 38, 2)) << 24) + (hex(substr($line, 35, 2)) << 16)
125
                            (hex(substr($line, 32, 2))<<8)+(hex(substr($line, 29, 2))<<0);
131
                    + (hex(substr($line, 32, 2)) << 8) + (hex(substr($line, 29, 2)) << 0);
126
                        $hwidhex = sprintf("%08x", $hwid);
132
                $hwidhex = sprintf("%08x", $hwid);
127
                        print "Setting HWID in bios.inc to 0x".$hwidhex."\n";
133
                print "Setting HWID in bios.inc to 0x".$hwidhex."\n";
128
                       
134
 
-
 
135
                # Find device type from data field
129
                        $devtype = hex(substr($line, 26, 2));
136
                $devtype = hex(substr($line, 26, 2));
130
                        if ($devtype == 0)
137
                if ($devtype == 0)
131
                        {
138
                {
132
                            print "\nWarning: Unknown device type, will not set MCU settings\n\n";
139
                    print "\nWarning: Unknown device type, will not set MCU settings\n\n";
133
                        }
140
                }
134
                        elsif ($devtype < $numdevices)
141
                elsif ($devtype < $numdevices)
135
                        {
142
                {
136
                            print "Setting device type to ".$devices[$devtype][0]."\n";
143
                    print "Setting device type to ".$devices[$devtype][0]."\n";
137
                            $devsettings = 1;
144
                    $devsettings = 1;
138
                        }
145
                }
139
 
146
 
-
 
147
                # Open bios.inc and replace all settings
140
                        open (FP, "<".$biosfile) or die "Cannot open file $biosfile\n";
148
                open (FP, "<".$biosfile) or die "Cannot open file $biosfile\n";
141
                        @file = <FP>;
149
                @file = <FP>;
142
                        close FP;
150
                close FP;
143
                        open (FP, ">".$biosfile) or die "Cannot open file $biosfile\n";
151
                open (FP, ">".$biosfile) or die "Cannot open file $biosfile\n";
144
                        foreach $file (@file) {
152
                foreach $file (@file) {
145
                            $file =~ s/^NODE_HW_ID=.*/NODE_HW_ID=0x$hwidhex/g;
153
                    $file =~ s/^NODE_HW_ID=.*/NODE_HW_ID=0x$hwidhex/g;
146
                            if ($devsettings==1)
154
                    if ($devsettings==1)
147
                            {
155
                    {
148
                                $file =~ s/^MCU=.*/MCU=$devices[$devtype][0]/g;
156
                        $file =~ s/^MCU=.*/MCU=$devices[$devtype][0]/g;
149
                                $file =~ s/^HIGHFUSE=.*/HIGHFUSE=$devices[$devtype][1]/g;
157
                        $file =~ s/^HIGHFUSE=.*/HIGHFUSE=$devices[$devtype][1]/g;
150
                                $file =~ s/^LOWFUSE=.*/LOWFUSE=$devices[$devtype][2]/g;
158
                        $file =~ s/^LOWFUSE=.*/LOWFUSE=$devices[$devtype][2]/g;
151
                                $file =~ s/^EXTFUSE=.*/EXTFUSE=$devices[$devtype][3]/g;
159
                        $file =~ s/^EXTFUSE=.*/EXTFUSE=$devices[$devtype][3]/g;
152
                                $file =~ s/^BOOT_START=.*/BOOT_START=$devices[$devtype][4]/g;
160
                        $file =~ s/^BOOT_START=.*/BOOT_START=$devices[$devtype][4]/g;
153
                            }
161
                    }
154
                            print FP $file;
162
                    print FP $file;
155
                        }
163
                }
156
                        close FP;
164
                close FP;
157
                       
165
 
158
                        exit 0;
166
                exit 0;
159
                       
-
 
160
                    }
167
            }
161
                }
168
        }
162
               
-
 
163
    }
169
    }
164
}
170
}
165
 
171
 
166
 
172