#!/usr/bin/perl -w
################################################
#
# author: arune @ efnet
#
################################################
use IO::Socket;
use integer;
use threads;
use File::Basename;
use Getopt::Long;
$host = "localhost";
$port = 1200;
# Get hostname and port from command line arguments
if ( @ARGV > 0 ) {
GetOptions( "host=s" => \$host, #
"port=i" => \$port, #
);
}
my $path = dirname(abs_path($0));
# Read devices from separate file
if (-e $path."/devices.pl") {
} else {
print "Error: No devices.pl was found in ".$path.". \n\n";
}
# Length of devices array
$numdevices=@devices;
# Connect to candaemon
$remote = IO::Socket::INET->new(
Proto => "tcp",
PeerAddr => $host,
PeerPort => $port,
)
or die "Error: Cannot connect to port $port at $host\n";
$biosfile = "bios.inc";
open (FP
, "+<".$biosfile) or die "Error: Cannot open file $biosfile\n";
@file = <FP>;
# Check bios.inc for the settings that should be updated
my $foundidrow = 0;
my $foundmcurow = 0;
my $foundhfrow = 0;
my $foundlfrow = 0;
my $foundefrow = 0;
my $foundbootrow = 0;
foreach $file (@file) {
if ($file =~ m/^NODE_HW_ID=/) {
print "\nCurrent id-setting: ".$file."\n";
$foundidrow = 1;
}
if ($file =~ m/^MCU=/) {
$foundmcurow = 1;
}
if ($file =~ m/^HIGHFUSE=/) {
$foundhfrow = 1;
}
if ($file =~ m/^LOWFUSE=/) {
$foundlfrow = 1;
}
if ($file =~ m/^EXTFUSE=/) {
$foundefrow = 1;
}
if ($file =~ m/^BOOT_START=/) {
$foundbootrow = 1;
}
}
if ($foundidrow == 0) {
print "Error: Did not find NODE_HW_ID= in file $biosfile\n";
}
if ($foundmcurow == 0) {
print "Error: Did not find MCU= in file $biosfile\n";
}
if ($foundhfrow == 0) {
print "Error: Did not find HIGHFUSE= in file $biosfile\n";
}
if ($foundlfrow == 0) {
print "Error: Did not find LOWFUSE= in file $biosfile\n";
}
if ($foundefrow == 0) {
print "Error: Did not find EXTFUSE= in file $biosfile\n";
}
if ($foundbootrow == 0) {
print "Error: Did not find BOOT_START= in file $biosfile\n";
}
# Define variable to know if a device was found
my $devsettings=0;
print "Connect your node to the CAN-network (disconnect it first if it's already connected)\n";
print "Abort with Ctrl+c\n";
#read line from socket (blocking)
while ( $line = <$remote> ) {
#PKT 00087f00 1 0 39 13 01 20 78 56 34 12
# Find can id
# Find can id class
$class = ($id & 0x1E000000)>>25;
# If class is nmt
if ($class == 0) {
# Find nmt type
$nmttype = ($id & 0x00FF0000)>>16;
# If nmt type is bios started
if ($nmttype == 8) {
print "Error: A node was started but it did not have a HWID\n";
}
# Find hw id from data field
print "Setting HWID in bios.inc to 0x".$hwidhex."\n";
# Find device type from data field
if ($devtype == 0)
{
print "\nWarning: Unknown device type, will not set MCU settings\n\n";
}
elsif ($devtype < $numdevices)
{
print "Setting device type to ".$devices[$devtype][0]."\n";
$devsettings = 1;
}
# Open bios.inc and replace all settings
open (FP
, "<".$biosfile) or die "Cannot open file $biosfile\n";
@file = <FP>;
open (FP
, ">".$biosfile) or die "Cannot open file $biosfile\n";
foreach $file (@file) {
$file =~ s/^NODE_HW_ID=.*/NODE_HW_ID=0x$hwidhex/g;
if ($devsettings==1)
{
$file =~ s/^MCU=.*/MCU=$devices[$devtype][0]/g;
$file =~ s/^HIGHFUSE=.*/HIGHFUSE=$devices[$devtype][1]/g;
$file =~ s/^LOWFUSE=.*/LOWFUSE=$devices[$devtype][2]/g;
$file =~ s/^EXTFUSE=.*/EXTFUSE=$devices[$devtype][3]/g;
$file =~ s/^BOOT_START=.*/BOOT_START=$devices[$devtype][4]/g;
}
}
}
}
}
}