| 1 | use utf8; | ||
| 2 | |||
| 3 | package Net::EGTS::Record; | ||
| 4 | use namespace::autoclean; | ||
| 5 | use Mouse; | ||
| 6 | |||
| 7 | use Carp; | ||
| 8 | use List::MoreUtils qw(natatime); | ||
| 9 | |||
| 10 | use Net::EGTS::Util qw(str2time time2new new2time usize dumper_bitstring); | ||
| 11 | use Net::EGTS::Types; | ||
| 12 | |||
| 13 | require Net::EGTS::SubRecord; | ||
| 14 | |||
| 15 | our $RN = 0; | ||
| 16 | |||
| 17 | =head1 NAME | ||
| 18 | |||
| 19 | Net::EGTS::Record - Record | ||
| 20 | |||
| 21 | =cut | ||
| 22 | |||
| 23 | # Record Length | ||
| 24 | has RL => | ||
| 25 | is => 'rw', | ||
| 26 | isa => 'USHORT', | ||
| 27 | lazy => 1, | ||
| 28 | builder => sub { | ||
| 29 | my ($self) = @_; | ||
| 30 | use bytes; | ||
| 31 | return length($self->RD); | ||
| 32 | }, | ||
| 33 | ; | ||
| 34 | |||
| 35 | # Record Number | ||
| 36 | has RN => | ||
| 37 | is => 'rw', | ||
| 38 | isa => 'USHORT', | ||
| 39 | lazy => 1, | ||
| 40 | builder => sub { | ||
| 41 | my $rn = $RN; | ||
| 42 | $RN = 0 unless ++$RN >= 0 && $RN <= 65535; | ||
| 43 | return $rn; | ||
| 44 | } | ||
| 45 | ; | ||
| 46 | |||
| 47 | |||
| 48 | # Flags: | ||
| 49 | # Source Service On Device | ||
| 50 | has SSOD => is => 'rw', isa => 'BIT1', default => 0x0; | ||
| 51 | # Recipient Service On Device | ||
| 52 | has RSOD => is => 'rw', isa => 'BIT1', default => 0x0; | ||
| 53 | # Group | ||
| 54 | has GRP => is => 'rw', isa => 'BIT1', default => 0x0; | ||
| 55 | # Record Processing Priority | ||
| 56 | has RPP => is => 'rw', isa => 'BIT2', default => 0x00; | ||
| 57 | # Time Field Exists | ||
| 58 | has TMFE => | ||
| 59 | is => 'rw', | ||
| 60 | isa => 'BIT1', | ||
| 61 | lazy => 1, | ||
| 62 | builder => sub{ defined $_[0]->TM ? 0x1 : 0x0 }, | ||
| 63 | ; | ||
| 64 | # Event ID Field Exists | ||
| 65 | has EVFE => | ||
| 66 | is => 'rw', | ||
| 67 | isa => 'BIT1', | ||
| 68 | lazy => 1, | ||
| 69 | builder => sub{ defined $_[0]->EVID ? 0x1 : 0x0 }, | ||
| 70 | ; | ||
| 71 | # Object ID Field Exists | ||
| 72 | has OBFE => | ||
| 73 | is => 'rw', | ||
| 74 | isa => 'BIT1', | ||
| 75 | lazy => 1, | ||
| 76 | builder => sub{ defined $_[0]->OID ? 0x1 : 0x0 }, | ||
| 77 | ; | ||
| 78 | |||
| 79 | # Optional: | ||
| 80 | # Object Identifier | ||
| 81 | has OID => is => 'rw', isa => 'Maybe[UINT]'; | ||
| 82 | # Event Identifier | ||
| 83 | has EVID => is => 'rw', isa => 'Maybe[UINT]'; | ||
| 84 | # Time | ||
| 85 | has TM => is => 'rw', isa => 'Maybe[UINT]'; | ||
| 86 | |||
| 87 | # Source Service Type | ||
| 88 | has SST => is => 'rw', isa => 'BYTE'; | ||
| 89 | # Recipient Service Type | ||
| 90 | has RST => is => 'rw', isa => 'BYTE'; | ||
| 91 | # Record Data | ||
| 92 | has RD => | ||
| 93 | is => 'rw', | ||
| 94 | isa => 'BINARY', | ||
| 95 | trigger => sub { | ||
| 96 | my ($self, $value, $old) = @_; | ||
| 97 | die 'Record Data too short' if length($value) < 3; | ||
| 98 | die 'Record Data too long' if length($value) > 65498; | ||
| 99 | } | ||
| 100 | ; | ||
| 101 | |||
| 102 | # Record binary | ||
| 103 | has bin => is => 'rw', isa => 'Str', default => ''; | ||
| 104 | |||
| 105 | # Array of decoded subrecords | ||
| 106 | has subrecords => | ||
| 107 | is => 'rw', | ||
| 108 | isa => 'ArrayRef[Net::EGTS::SubRecord]', | ||
| 109 | lazy => 1, | ||
| 110 | builder => sub { | ||
| 111 | my ($self) = @_; | ||
| 112 | return [] unless defined $self->RD; | ||
| 113 | return [] unless length $self->RD; | ||
| 114 | return Net::EGTS::SubRecord->decode_all( $self->RD ); | ||
| 115 | }, | ||
| 116 | ; | ||
| 117 | |||
| 118 | around BUILDARGS => sub { | ||
| 119 | my $orig = shift; | ||
| 120 | my $class = shift; | ||
| 121 | |||
| 122 | # simple scalar decoding support | ||
| 123 | my $bin = @_ % 2 ? shift : undef; | ||
| 124 | my %opts = @_; | ||
| 125 | |||
| 126 | # simple time support | ||
| 127 | if( defined( my $time = delete $opts{time} ) ) { | ||
| 128 | $opts{TM} = time2new str2time $time; | ||
| 129 | $opts{TMFE} = 1 if $opts{TM}; | ||
| 130 | } | ||
| 131 | |||
| 132 | return $class->$orig( bin => $bin, %opts ) if $bin; | ||
| 133 | return $class->$orig( %opts ); | ||
| 134 | }; | ||
| 135 | sub BUILD { | ||
| 136 | my $self = shift; | ||
| 137 | my $args = shift; | ||
| 138 | $self->decode( \$self->bin ) if length $self->bin; | ||
| 139 | } | ||
| 140 | |||
| 141 | # Get chunk from binary and store it | ||
| 142 | sub take { | ||
| 143 | my ($self, $bin, $mask, $length) = @_; | ||
| 144 | use bytes; | ||
| 145 | |||
| 146 | $length //= usize($mask); | ||
| 147 | confess "Can`t get chunk of length $length" if $length > length $$bin; | ||
| 148 | |||
| 149 | my $chunk = substr $$bin, 0 => $length, ''; | ||
| 150 | $self->bin( $self->bin . $chunk ); | ||
| 151 | |||
| 152 | return unpack $mask => $chunk; | ||
| 153 | } | ||
| 154 | |||
| 155 | # Helper to get portion of data | ||
| 156 | sub nip { | ||
| 157 | my ($self, $bin, $mask, $length) = @_; | ||
| 158 | use bytes; | ||
| 159 | |||
| 160 | $length //= usize($mask); | ||
| 161 | confess "Can`t get chunk of length $length" if $length > length $$bin; | ||
| 162 | |||
| 163 | my $chunk = substr $$bin, 0 => $length, ''; | ||
| 164 | return unpack $mask => $chunk; | ||
| 165 | } | ||
| 166 | |||
| 167 | =head2 encode | ||
| 168 | |||
| 169 | Build record as binary | ||
| 170 | |||
| 171 | =cut | ||
| 172 | |||
| 173 | sub encode { | ||
| 174 | my ($self) = @_; | ||
| 175 | use bytes; | ||
| 176 | |||
| 177 | croak 'Source Service Type required' unless defined $self->SST; | ||
| 178 | croak 'Recipient Service Type required' unless defined $self->RST; | ||
| 179 | croak 'Record Data required' unless defined $self->RD; | ||
| 180 | croak 'Wrong Record Length' unless $self->RL >= 3 && | ||
| 181 | $self->RL <= 65498; | ||
| 182 | my $mask = 'S S B8'; | ||
| 183 | |||
| 184 | # Optional fields | ||
| 185 | my @optional; | ||
| 186 | if( $self->OBFE || $self->GRP ) { | ||
| 187 | $mask = join ' ', $mask, 'L'; | ||
| 188 | push @optional, $self->OID; | ||
| 189 | } | ||
| 190 | if( $self->EVFE ) { | ||
| 191 | $mask = join ' ', $mask, 'L'; | ||
| 192 | push @optional, $self->EVID; | ||
| 193 | } | ||
| 194 | if( $self->TMFE ) { | ||
| 195 | $mask = join ' ', $mask, 'L'; | ||
| 196 | push @optional, $self->TM; | ||
| 197 | } | ||
| 198 | |||
| 199 | $mask = join ' ', $mask, 'C C a*'; | ||
| 200 | |||
| 201 | my $bin = pack $mask => | ||
| 202 | $self->RL, $self->RN, | ||
| 203 | sprintf( | ||
| 204 | '%b%b%b%02b%b%b%b', | ||
| 205 | $self->SSOD, $self->RSOD, $self->GRP, $self->RPP, $self->TMFE, | ||
| 206 | $self->EVFE, $self->OBFE, | ||
| 207 | ), | ||
| 208 | @optional, | ||
| 209 | $self->SST, $self->RST, $self->RD | ||
| 210 | ; | ||
| 211 | |||
| 212 | $self->bin( $bin ); | ||
| 213 | return $bin; | ||
| 214 | } | ||
| 215 | |||
| 216 | =head2 decode \$bin | ||
| 217 | |||
| 218 | Decode binary I<$bin> into record object. | ||
| 219 | The binary stream will be truncated! | ||
| 220 | |||
| 221 | =cut | ||
| 222 | |||
| 223 | sub decode { | ||
| 224 | my ($self, $bin) = @_; | ||
| 225 | use bytes; | ||
| 226 | |||
| 227 | $self->RL( $self->take($bin => 'S') ); | ||
| 228 | $self->RN( $self->take($bin => 'S') ); | ||
| 229 | |||
| 230 | my $flags = $self->take($bin => 'C'); | ||
| 231 | $self->SSOD( ($flags & 0b10000000) >> 7 ); | ||
| 232 | $self->RSOD( ($flags & 0b01000000) >> 6 ); | ||
| 233 | $self->GRP( ($flags & 0b00100000) >> 5 ); | ||
| 234 | $self->RPP( ($flags & 0b00011000) >> 3 ); | ||
| 235 | $self->TMFE( ($flags & 0b00000100) >> 2 ); | ||
| 236 | $self->EVFE( ($flags & 0b00000010) >> 1 ); | ||
| 237 | $self->OBFE( ($flags & 0b00000001) ); | ||
| 238 | |||
| 239 | $self->OID( $self->take($bin => 'L') ) if $self->OBFE || $self->GRP; | ||
| 240 | $self->EVID( $self->take($bin => 'L') ) if $self->EVFE; | ||
| 241 | $self->TM( $self->take($bin => 'L') ) if $self->TMFE; | ||
| 242 | |||
| 243 | $self->SST( $self->take($bin => 'C') ); | ||
| 244 | $self->RST( $self->take($bin => 'C') ); | ||
| 245 | |||
| 246 | $self->RD( $self->take($bin => 'a*' => $self->RL) ); | ||
| 247 | |||
| 248 | return $self; | ||
| 249 | } | ||
| 250 | |||
| 251 | =head2 decode_all \$bin | ||
| 252 | |||
| 253 | Parse all records from packet Service Frame Data | ||
| 254 | |||
| 255 | =cut | ||
| 256 | |||
| 257 | sub decode_all { | ||
| 258 | my ($class, $bin) = @_; | ||
| 259 | use bytes; | ||
| 260 | |||
| 261 | my @result; | ||
| 262 | while( my $length = length $bin ) { | ||
| 263 | my $self = Net::EGTS::Record->new->decode( \$bin ); | ||
| 264 | die 'Something wrong in records decode' unless $self; | ||
| 265 | |||
| 266 | push @result, $self; | ||
| 267 | } | ||
| 268 | |||
| 269 | return wantarray ? @result : \@result; | ||
| 270 | } | ||
| 271 | |||
| 272 | =head2 as_debug | ||
| 273 | |||
| 274 | Return human readable string | ||
| 275 | |||
| 276 | =cut | ||
| 277 | |||
| 278 | sub as_debug { | ||
| 279 | my ($self) = @_; | ||
| 280 | use bytes; | ||
| 281 | |||
| 282 | my @bytes = ((unpack('B*', $self->bin)) =~ m{.{8}}g); | ||
| 283 | |||
| 284 | my @str; | ||
| 285 | push @str => sprintf('RL: %s %s', splice @bytes, 0 => usize('S')); | ||
| 286 | push @str => sprintf('RN: %s %s', splice @bytes, 0 => usize('S')); | ||
| 287 | push @str => sprintf('FLAGS: %s', splice @bytes, 0 => usize('C')); | ||
| 288 | |||
| 289 | push @str => sprintf('OID: %s %s %s %s', splice @bytes, 0 => usize('L')) | ||
| 290 | if defined $self->OID; | ||
| 291 | push @str => sprintf('EVID: %s %s %s %s', splice @bytes, 0 => usize('L')) | ||
| 292 | if defined $self->EVID; | ||
| 293 | push @str => sprintf('TM: %s %s %s %s', splice @bytes, 0 => usize('L')) | ||
| 294 | if defined $self->TM; | ||
| 295 | |||
| 296 | push @str => sprintf('SST: %s', splice @bytes, 0 => usize('C')); | ||
| 297 | push @str => sprintf('RST: %s', splice @bytes, 0 => usize('C')); | ||
| 298 | |||
| 299 | my $it = natatime 4, @bytes; | ||
| 300 | my @chunks; | ||
| 301 | while (my @vals = $it->()) { | ||
| 302 | push @chunks, join(' ', @vals); | ||
| 303 | } | ||
| 304 | push @str => sprintf('RD: %s', join("\n ", @chunks)); | ||
| 305 | |||
| 306 | return join "\n", @str; | ||
| 307 | } | ||
| 308 | |||
| 309 | __PACKAGE__->meta->make_immutable(); |