| 1 | |||
| 2 | =head1 NAME | ||
| 3 | |||
| 4 | STANAG - Library for processing STANAG 4586 messages | ||
| 5 | |||
| 6 | =head1 SYNOPSIS | ||
| 7 | |||
| 8 | use STANAG qw(Get_Vehicle_Name Get_Vehicle_Subtype); | ||
| 9 | |||
| 10 | use strict; | ||
| 11 | use warnings; | ||
| 12 | use Data::Dumper; | ||
| 13 | |||
| 14 | my $hashref = STANAG::Vehicle_ID(); #get blank hash | ||
| 15 | |||
| 16 | $hashref->{Vehicle_Type} = 22; #stick in some sample data | ||
| 17 | $hashref->{Vehicle_Subtype} = 101; | ||
| 18 | |||
| 19 | print Dumper($hashref); #see what's in it | ||
| 20 | |||
| 21 | my $flat = STANAG::Encode_Vehicle_ID($hashref); #package up for sending (computes xsums) | ||
| 22 | my $hashref2 = STANAG::Decode_Vehicle_ID($flat); #unpackage into hash (checks xsums) | ||
| 23 | |||
| 24 | my $hashref3 = STANAG::Vehicle_ID(); #grab a new hash with latest used values | ||
| 25 | #so this one gets the values we put into $hashref | ||
| 26 | |||
| 27 | print Dumper($hashref2); | ||
| 28 | print Dumper($hashref3); #these print the same thing | ||
| 29 | |||
| 30 | print Get_Vehicle_Name($hashref2->{Vehicle_Type}),"\n"; #demo decoding some numeric codes. | ||
| 31 | print Get_Vehicle_Subtype($hashref2->{Vehicle_Subtype}),"\n"; | ||
| 32 | |||
| 33 | =head1 ABSTRACT | ||
| 34 | |||
| 35 | Library acts as a wrapper around Parse::Binary::FixedFormat for STANAG 4586 messages. Most of the internals were autogenerated from the 4586 specification. | ||
| 36 | |||
| 37 | The module also stores an internal state hash representing a history of the latest values encoded or decoded. | ||
| 38 | This way when you get a message and decode it, and then want to respond, the new template is prepopulated. | ||
| 39 | |||
| 40 | =head1 EXPORT | ||
| 41 | |||
| 42 | None by default. | ||
| 43 | |||
| 44 | Available are: | ||
| 45 | Get_Vehicle_Name | ||
| 46 | Get_Vehicle_Subtype | ||
| 47 | Get_Country_Name | ||
| 48 | |||
| 49 | =over | ||
| 50 | =cut | ||
| 51 | |||
| 52 | 0 | ▶ | use Parse::Binary::FixedFormat; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 53 | use List::Util qw(sum); | ||
| 54 | use AutoLoader; | ||
| 55 | use strict; | ||
| 56 | use warnings; | ||
| 57 | |||
| 58 | package STANAG; | ||
| 59 | |||
| 60 | use STANAG::CountryAndVehicleIDs; | ||
| 61 | use STANAG::Messages; | ||
| 62 | |||
| 63 | my %state; # holds any data that's been encoded or decoded for filling future messages | ||
| 64 | |||
| 65 | use Exporter 'import'; | ||
| 66 | our @EXPORT_OK = qw(Get_Vehicle_Name Get_Vehicle_Subtype Get_Country_Name); | ||
| 67 | |||
| 68 | our $VERSION = '0.01'; | ||
| 69 | |||
| 70 | =item AUTOLOAD | ||
| 71 | This function handles all access to the messages. This is the main interface to this module. | ||
| 72 | |||
| 73 | A call of STANAG::<message> will return a hashref for that message | ||
| 74 | |||
| 75 | A call of STANAG::Encode_<message>(hashref) will return the message, formatted and with xsums added | ||
| 76 | |||
| 77 | A call of STANAG::Decode_<message>(message) will return the hashref corresponding the the message | ||
| 78 | and will issue warnings if the xsums don't match. | ||
| 79 | =cut | ||
| 80 | use Carp qw(croak); | ||
| 81 | sub AUTOLOAD { | ||
| 82 | our $AUTOLOAD; | ||
| 83 | no strict 'refs'; | ||
| 84 | |||
| 85 | if($AUTOLOAD =~ /.*::(?:Encode_)(.*)/) { | ||
| 86 | croak "Undefined subroutine &$AUTOLOAD called" unless exists $messages{$1}; | ||
| 87 | my $element = $1; | ||
| 88 | *$AUTOLOAD = sub { #Handles Encode_<message> calls | ||
| 89 | #this could be maybe optimized by assuming the xsum will always be last. | ||
| 90 | my $hashref = shift; | ||
| 91 | my $flat = $messages{$element}->format($hashref); | ||
| 92 | $hashref->{Checksum} = Checksum($flat); | ||
| 93 | for( keys %{ $hashref } ) { #auto-apply to state | ||
| 94 | $state{$_} = $hashref->{$_}; | ||
| 95 | } | ||
| 96 | return $messages{$element}->format($hashref); | ||
| 97 | } | ||
| 98 | } elsif($AUTOLOAD =~ /.*::(?:Decode_)(.*)/) { | ||
| 99 | croak "Undefined subroutine &$AUTOLOAD called" unless exists $messages{$1}; | ||
| 100 | my $element = $1; | ||
| 101 | *$AUTOLOAD = sub { #Handles Decode_<message> calls | ||
| 102 | my $xsum = Checksum($_[0]); | ||
| 103 | my $hashref = $messages{$element}->unformat($_[0]); | ||
| 104 | if($xsum != $hashref->{Checksum}) { | ||
| 105 | warn "unmatched xsum for $element message - expected $xsum got ".$hashref->{Checksum} | ||
| 106 | } | ||
| 107 | for( keys %{ $hashref } ) { #auto-apply to state | ||
| 108 | $state{$_} = $hashref->{$_}; | ||
| 109 | } | ||
| 110 | return $hashref; | ||
| 111 | } | ||
| 112 | } elsif($AUTOLOAD =~ /.*::(.*)/) { | ||
| 113 | return if $1 eq "DESTROY"; | ||
| 114 | unless (exists $messages{$1}) { | ||
| 115 | $AutoLoader::AUTOLOAD = $AUTOLOAD; | ||
| 116 | goto &AutoLoader::AUTOLOAD; | ||
| 117 | } | ||
| 118 | my $element = $1; | ||
| 119 | *$AUTOLOAD = sub { #Handles <message> calls, gives blank hash | ||
| 120 | my $hashref = $messages{$element}->blank(); | ||
| 121 | for( keys %{ $hashref } ) { #auto-fill from state | ||
| 122 | $hashref->{$_} = $state{$_} if exists $state{$_}; | ||
| 123 | } | ||
| 124 | return $hashref; | ||
| 125 | } | ||
| 126 | } goto &$AUTOLOAD; | ||
| 127 | } | ||
| 128 | |||
| 129 | 1; | ||
| 130 | |||
| 131 | __END__ | ||