| 1 | 16 | ▶ | use utf8; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 2 | 16 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 3 | 16 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | package Net::EGTS::Util; | ||||||||||||||||||||
| 6 | 16 | ▶ | use base qw(Exporter); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 16 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 16 | ▶ | use Digest::CRC qw(); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 16 | ▶ | use Date::Parse qw(); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | 16 | ▶ | use List::MoreUtils qw(natatime any); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | 16 | ▶ | use POSIX qw(); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | 16 | our @EXPORT = qw( | |||||||||||||||||||
| 15 | crc8 crc16 | ||||||||||||||||||||
| 16 | str2time time2new new2time strftime | ||||||||||||||||||||
| 17 | dumper_bitstring | ||||||||||||||||||||
| 18 | usize | ||||||||||||||||||||
| 19 | lat2mod mod2lat | ||||||||||||||||||||
| 20 | lon2mod mod2lon | ||||||||||||||||||||
| 21 | ); | ||||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | 16 | ▶ | use constant TIMESTAMP_20100101_000000_UTC => 1262304000; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | =head2 crc8 $bytes | ||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | CRC8 with EGTS customization | ||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | =cut | ||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | sub crc8($) { | ||||||||||||||||||||
| 32 | 16 | ▶ | use bytes; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 33 | 23 | ▶ | my $ctx = Digest::CRC->new( | ||||||||||||||||||
|
Subroutine
crc8: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 34 | width => 8, | ||||||||||||||||||||
| 35 | poly => 0x31, | ||||||||||||||||||||
| 36 | init => 0xff, | ||||||||||||||||||||
| 37 | xorout => 0x00, | ||||||||||||||||||||
| 38 | check => 0xf7, | ||||||||||||||||||||
| 39 | ); | ||||||||||||||||||||
| 40 | 23 | $ctx->add($_[0]); | |||||||||||||||||||
| 41 | 23 | return $ctx->digest; | |||||||||||||||||||
| 42 | } | ||||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | =head2 crc16 $bytes | ||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | CRC16 with EGTS customization | ||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | =cut | ||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | sub crc16($) { | ||||||||||||||||||||
| 51 | 16 | ▶ | use bytes; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 52 | 16 | ▶ | my $ctx = Digest::CRC->new( | ||||||||||||||||||
|
Subroutine
crc16: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 53 | width => 16, | ||||||||||||||||||||
| 54 | poly => 0x1021, | ||||||||||||||||||||
| 55 | init => 0xffff, | ||||||||||||||||||||
| 56 | xorout => 0x0000, | ||||||||||||||||||||
| 57 | check => 0x29b1, | ||||||||||||||||||||
| 58 | ); | ||||||||||||||||||||
| 59 | 16 | $ctx->add($_[0]); | |||||||||||||||||||
| 60 | 16 | return $ctx->digest; | |||||||||||||||||||
| 61 | } | ||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | =head2 strftime $format, time | ||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | Return formatted string. | ||||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | =cut | ||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | sub strftime { | ||||||||||||||||||||
| 70 | 1 | ▶ | POSIX::strftime @_; | ||||||||||||||||||
|
Subroutine
strftime: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 71 | } | ||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | =head2 str2time $str | ||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | Return timestamp from any time format | ||||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | =cut | ||||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | sub str2time($) { | ||||||||||||||||||||
| 80 | 4 | ▶ | return undef unless defined $_[0]; | ||||||||||||||||||
|
Subroutine
str2time: called CC=4 SCAR=14.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||
| 81 | 4 | ▶ | return undef unless length $_[0]; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 82 | 4 | ▶ | return $_[0] if $_[0] =~ m{^\d+$}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 83 | 4 | return Date::Parse::str2time( $_[0] ); | |||||||||||||||||||
| 84 | } | ||||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | =head2 time2new [$time] | ||||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | Return time from 2010 instead of 1970 | ||||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | =cut | ||||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | sub time2new(;$) { | ||||||||||||||||||||
| 93 | 7 | ▶ | my ($time) = @_; | ||||||||||||||||||
|
Subroutine
time2new: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||
| 94 | 7 | ▶ | $time //= time; | ||||||||||||||||||
|
Condition66%2/3
$time //= time
MC/DC0%0/2
$time //= time
$time
time
Truth table66%2/3
A //= B
A= $timeB= time
| |||||||||||||||||||||
| 95 | 7 | return ($time - TIMESTAMP_20100101_000000_UTC); | |||||||||||||||||||
| 96 | } | ||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | =head2 new2time [$time] | ||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | Return time from 1970 instead of 2010 | ||||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | =cut | ||||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | sub new2time($) { | ||||||||||||||||||||
| 105 | 1 | ▶ | my ($time) = @_; | ||||||||||||||||||
|
Subroutine
new2time: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 106 | 1 | return ($time + TIMESTAMP_20100101_000000_UTC); | |||||||||||||||||||
| 107 | } | ||||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | =head2 dumper_bitstring $bin, [$size] | ||||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | Return bitstring from I<$bin> chanked by I<$size> | ||||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | =cut | ||||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | sub dumper_bitstring($;$) { | ||||||||||||||||||||
| 116 | 6 | ▶ | my ($bin, $size) = @_; | ||||||||||||||||||
|
Subroutine
dumper_bitstring: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||
| 117 | 6 | my @bytes = ((unpack('B*', $bin)) =~ m{.{8}}g); | |||||||||||||||||||
| 118 | 6 | ▶ | my $it = natatime( ($size || 4), @bytes ); | ||||||||||||||||||
|
Condition50%1/2
$size || 4
MC/DC0%0/1
$size || 4
$size
Truth table50%1/2
A
A= $size
| |||||||||||||||||||||
| 119 | 6 | my @chunks; | |||||||||||||||||||
| 120 | 6 | ▶ | while (my @vals = $it->()) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 121 | 44 | push @chunks, join ' ', @vals; | |||||||||||||||||||
| 122 | } | ||||||||||||||||||||
| 123 | 6 | return join "\n", @chunks; | |||||||||||||||||||
| 124 | } | ||||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | =head2 usize $mask | ||||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | Return size in bytes of pack/unpack mask | ||||||||||||||||||||
| 129 | |||||||||||||||||||||
| 130 | =cut | ||||||||||||||||||||
| 131 | |||||||||||||||||||||
| 132 | sub usize($) { | ||||||||||||||||||||
| 133 | 514 | ▶ | my ($mask) = @_; | ||||||||||||||||||
|
Subroutine
usize: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||
| 134 | 16 | ▶ | use bytes; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 135 | 514 | ▶ | die 'Unknown "*" length' if $mask =~ m{^\w\*$}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 136 | 514 | return length pack $mask => 0; | |||||||||||||||||||
| 137 | } | ||||||||||||||||||||
| 138 | |||||||||||||||||||||
| 139 | =head2 lat2mod $latitude | ||||||||||||||||||||
| 140 | |||||||||||||||||||||
| 141 | Module from latitude | ||||||||||||||||||||
| 142 | |||||||||||||||||||||
| 143 | =cut | ||||||||||||||||||||
| 144 | |||||||||||||||||||||
| 145 | sub lat2mod($) { | ||||||||||||||||||||
| 146 | 4 | ▶ | return int( abs( $_[0] ) / 90 * 0xffffffff ); | ||||||||||||||||||
|
Subroutine
lat2mod: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 147 | } | ||||||||||||||||||||
| 148 | |||||||||||||||||||||
| 149 | =head2 mod2lat $module, $sign | ||||||||||||||||||||
| 150 | |||||||||||||||||||||
| 151 | Latitude from module and sign | ||||||||||||||||||||
| 152 | |||||||||||||||||||||
| 153 | =cut | ||||||||||||||||||||
| 154 | |||||||||||||||||||||
| 155 | sub mod2lat($$) { | ||||||||||||||||||||
| 156 | 3 | ▶ | my ($module, $sign) = @_; | ||||||||||||||||||
|
Subroutine
mod2lat: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 157 | 3 | ▶ | return $_[0] / 0xffffffff * 90 * ($sign ? -1 : 1); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 158 | } | ||||||||||||||||||||
| 159 | |||||||||||||||||||||
| 160 | =head2 lon2mod $longitude | ||||||||||||||||||||
| 161 | |||||||||||||||||||||
| 162 | Module from longitude | ||||||||||||||||||||
| 163 | |||||||||||||||||||||
| 164 | =cut | ||||||||||||||||||||
| 165 | |||||||||||||||||||||
| 166 | sub lon2mod($) { | ||||||||||||||||||||
| 167 | 4 | ▶ | return int( abs( $_[0] ) / 180 * 0xffffffff ); | ||||||||||||||||||
|
Subroutine
lon2mod: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 168 | } | ||||||||||||||||||||
| 169 | |||||||||||||||||||||
| 170 | =head2 mod2lon $module, $sign | ||||||||||||||||||||
| 171 | |||||||||||||||||||||
| 172 | Longitude from module and sign. | ||||||||||||||||||||
| 173 | |||||||||||||||||||||
| 174 | =cut | ||||||||||||||||||||
| 175 | |||||||||||||||||||||
| 176 | sub mod2lon($$) { | ||||||||||||||||||||
| 177 | 3 | ▶ | my ($module, $sign) = @_; | ||||||||||||||||||
|
Subroutine
mod2lon: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 178 | 3 | ▶ | return $_[0] / 0xffffffff * 180 * ($sign ? -1 : 1); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 179 | } | ||||||||||||||||||||
| 180 | |||||||||||||||||||||
| 181 | 16 | 1; | |||||||||||||||||||