Subversion Repositories HomeAutomation

Rev

Rev 902 | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
902 arune 1
#!/usr/bin/perl -w
2
################################################
3
#
4
# This file should not be edited for settings
5
# use settings.pl for that
6
#
7
# author: arune @ efnet
8
#
9
################################################
10
 
11
use IO::Socket;
12
use integer;
13
use threads;
14
use Cwd qw(abs_path);
15
use File::Basename;
16
 
17
$host = "localhost";
18
$port = 1200;
19
 
20
my $path = dirname(abs_path($0));
21
 
22
if (-e $path."/settings.pl") {
23
    require $path."/settings.pl";
24
} else {
25
    print "Warning: No settings.pl was found in ".$path.". \nUsing default settings localhost:1200 instead.\n\n";
26
}
27
 
28
$remote = IO::Socket::INET->new(
29
                    Proto    => "tcp",
30
                    PeerAddr => $host,
31
                    PeerPort => $port,
32
                )
33
               or die "Error: Cannot connect to port $port at $host\n";
34
 
35
$biosfile = "bios.inc";
36
open (FP, "+<".$biosfile) or die "Error: Cannot open file $biosfile\n";
37
@file = <FP>;
38
close FP;
39
$foundrow = 0;
40
foreach $file (@file) {
41
    if ($file =~ m/^NODE_HW_ID=<GENERATE_HW>/) {
42
        $foundrow = 1;
43
    }
44
}
45
if ($foundrow == 0) {
46
    print "Error: Did not find NODE_HW_ID=<GENERATE_HW> in file $biosfile\n";
47
    exit 1;
48
}
49
 
50
#print "\n";
51
print "Connect your node to the CAN-network (disconnect it first if it's already connected)\n";
52
print "Abort with Ctrl+c\n";
53
#read line from socket (blocking)
54
while ( $line = <$remote> ) {
55
#PKT 00087f00 1 0 39 13 01 20 78 56 34 12
56
        if (length($line) > 12) {
57
                $id = hex(substr($line, 4, 8));
58
                $class = ($id & 0x1E000000)>>25;
59
                if ($class == 0) {
60
                    $nmttype = ($id & 0x00FF0000)>>16;
61
                    if ($nmttype == 8) {
62
                        print $line."";
63
                        #print $class." ".$nmttype."\n";
64
                        if (length($line) < 40) {
65
                            print "Error: A node was started but it did not have a HWID\n";
66
                            exit 1;
67
                        }
68
                        $hwid = (hex(substr($line, 38, 2))<<24)+(hex(substr($line, 35, 2))<<16)+
69
                        (hex(substr($line, 32, 2))<<8)+(hex(substr($line, 29, 2))<<0);
70
                        $hwidhex = sprintf("%08x", $hwid);
71
                        print "Setting HWID in bios.inc to 0x".$hwidhex."\n";
72
 
73
                        open (FP, "<".$biosfile) or die "Cannot open file $biosfile\n";
74
                        @file = <FP>;
75
                        close FP;
76
                        open (FP, ">".$biosfile) or die "Cannot open file $biosfile\n";
77
                        foreach $file (@file) {
78
                            $file =~ s/^NODE_HW_ID=<GENERATE_HW>/NODE_HW_ID=0x$hwidhex/g;
79
                            print FP $file;
80
                        }
81
                        close FP;
82
 
83
                        exit 0;
84
 
85
                    }
86
                }
87
 
88
    }
89
}
90