package GSPSupport; use strict; use warnings; use XML::Simple; use Carp; use English; sub parse { open(my $ifh, '<', 'projectname.txt') or croak($!); my $projectname = <$ifh>; close $ifh; chomp $projectname; if(!length($projectname)) { croak("No projectname given!"); } my $devicesfile = '/home/cavac/src/gardenspaceagency/missioncontrol/devices.xml'; if(!-f $devicesfile) { $devicesfile = '../devices.xml'; } my $deviceconfig = XMLin($devicesfile); my $devices = $deviceconfig->{device}; if(!defined($devices->{$projectname})) { croak("Unknown project $projectname"); } my $config = $devices->{$projectname}; my @rf24routing; push @rf24routing, $config->{id}; while((scalar @rf24routing < 5)) { push @rf24routing, 0; } $config->{rf24routing} = \@rf24routing; return $config; } sub loadClacksConfig { my $configfile = '/home/cavac/src/clacksconfig.xml'; if(!-f $configfile) { $configfile = '../clacksconfig.xml'; } my $config = XMLin($configfile); return $config; } sub emptyframe { my $config = shift; my @outframe = (0) x 30; # Sender, reciever $outframe[0] = 0x01; # Linksender MODEM for(my $i = 0; $i < 5; $i++) { $outframe[$i + 1] = $config->{rf24routing}->[$i]; } $outframe[6] = 0x01; # Real sender MODEM $outframe[7] = $config->{id}; # Real reciever return @outframe; } sub frame2packet { my @frame = @_; my $packet = ''; foreach my $byte (@frame) { my $lowbyte = ($byte & 0x0f) + 65; my $highbyte = ($byte >> 4) + 65; $packet .= chr($highbyte); $packet .= chr($lowbyte); } return $packet; } sub packet2frame { my $packet = shift; my @chars = split//, $packet; my @frame = (); # Decode to bytes while(@chars) { my $high = shift @chars; my $low = shift @chars; my $val = ((ord($high) - 65) << 4) + (ord($low) - 65); push @frame, $val; } return @frame; } sub decodeErrorFrame { my @frame = @_; if($frame[12] == 1) { print "MODBUS ERROR\n"; } elsif($frame[12] == 2) { print "INVALID PAYLOAD_LENGTH ERROR\n"; } elsif($frame[12] == 3) { print "MODBUS_SLAVE_NOT_SET ERROR\n"; } elsif($frame[12] == 4) { print "REBOOT_DETECTED ERROR\n"; } elsif($frame[12] == 5) { print "REQUEST_REJECTED ERROR\n"; } elsif($frame[12] == 6) { print "MOSFET_TEMPERATURE ERROR\n"; } return; } sub slurpBinFile { my $fname = shift; # Read in file in binary mode, slurping it into a single scalar. # We have to make sure we use binmode *and* turn on the line termination variable completly # to work around the multiple idiosynchrasies of Perl on Windows open(my $fh, "<", $fname) or croak($ERRNO); local $INPUT_RECORD_SEPARATOR = undef; binmode($fh); my $data = <$fh>; close($fh); return $data; } 1;