| 1 | use utf8; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | package Net::EGTS::Util; | ||
| 6 | use base qw(Exporter); | ||
| 7 | |||
| 8 | use Carp; | ||
| 9 | use Digest::CRC qw(); | ||
| 10 | use Date::Parse qw(); | ||
| 11 | use List::MoreUtils qw(natatime any); | ||
| 12 | use POSIX qw(); | ||
| 13 | |||
| 14 | 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 | use constant TIMESTAMP_20100101_000000_UTC => 1262304000; | ||
| 24 | |||
| 25 | =head2 crc8 $bytes | ||
| 26 | |||
| 27 | CRC8 with EGTS customization | ||
| 28 | |||
| 29 | =cut | ||
| 30 | |||
| 31 | sub crc8($) { | ||
| 32 | use bytes; | ||
| 33 | my $ctx = Digest::CRC->new( | ||
| 34 | width => 8, | ||
| 35 | poly => 0x31, | ||
| 36 | init => 0xff, | ||
| 37 | xorout => 0x00, | ||
| 38 | check => 0xf7, | ||
| 39 | ); | ||
| 40 | $ctx->add($_[0]); | ||
| 41 | return $ctx->digest; | ||
| 42 | } | ||
| 43 | |||
| 44 | =head2 crc16 $bytes | ||
| 45 | |||
| 46 | CRC16 with EGTS customization | ||
| 47 | |||
| 48 | =cut | ||
| 49 | |||
| 50 | sub crc16($) { | ||
| 51 | use bytes; | ||
| 52 | my $ctx = Digest::CRC->new( | ||
| 53 | width => 16, | ||
| 54 | poly => 0x1021, | ||
| 55 | init => 0xffff, | ||
| 56 | xorout => 0x0000, | ||
| 57 | check => 0x29b1, | ||
| 58 | ); | ||
| 59 | $ctx->add($_[0]); | ||
| 60 | return $ctx->digest; | ||
| 61 | } | ||
| 62 | |||
| 63 | =head2 strftime $format, time | ||
| 64 | |||
| 65 | Return formatted string. | ||
| 66 | |||
| 67 | =cut | ||
| 68 | |||
| 69 | sub strftime { | ||
| 70 | POSIX::strftime @_; | ||
| 71 | } | ||
| 72 | |||
| 73 | =head2 str2time $str | ||
| 74 | |||
| 75 | Return timestamp from any time format | ||
| 76 | |||
| 77 | =cut | ||
| 78 | |||
| 79 | sub str2time($) { | ||
| 80 | return undef unless defined $_[0]; | ||
| 81 | return undef unless length $_[0]; | ||
| 82 | return $_[0] if $_[0] =~ m{^\d+$}; | ||
| 83 | 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 | my ($time) = @_; | ||
| 94 | $time //= time; | ||
| 95 | 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 | my ($time) = @_; | ||
| 106 | 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 | my ($bin, $size) = @_; | ||
| 117 | my @bytes = ((unpack('B*', $bin)) =~ m{.{8}}g); | ||
| 118 | my $it = natatime( ($size || 4), @bytes ); | ||
| 119 | my @chunks; | ||
| 120 | while (my @vals = $it->()) { | ||
| 121 | push @chunks, join ' ', @vals; | ||
| 122 | } | ||
| 123 | 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 | my ($mask) = @_; | ||
| 134 | use bytes; | ||
| 135 | die 'Unknown "*" length' if $mask =~ m{^\w\*$}; | ||
| 136 | return length pack $mask => 0; | ||
| 137 | } | ||
| 138 | |||
| 139 | =head2 lat2mod $latitude | ||
| 140 | |||
| 141 | Module from latitude | ||
| 142 | |||
| 143 | =cut | ||
| 144 | |||
| 145 | sub lat2mod($) { | ||
| 146 | return int( abs( $_[0] ) / 90 * 0xffffffff ); | ||
| 147 | } | ||
| 148 | |||
| 149 | =head2 mod2lat $module, $sign | ||
| 150 | |||
| 151 | Latitude from module and sign | ||
| 152 | |||
| 153 | =cut | ||
| 154 | |||
| 155 | sub mod2lat($$) { | ||
| 156 | my ($module, $sign) = @_; | ||
| 157 | return $_[0] / 0xffffffff * 90 * ($sign ? -1 : 1); | ||
| 158 | } | ||
| 159 | |||
| 160 | =head2 lon2mod $longitude | ||
| 161 | |||
| 162 | Module from longitude | ||
| 163 | |||
| 164 | =cut | ||
| 165 | |||
| 166 | sub lon2mod($) { | ||
| 167 | return int( abs( $_[0] ) / 180 * 0xffffffff ); | ||
| 168 | } | ||
| 169 | |||
| 170 | =head2 mod2lon $module, $sign | ||
| 171 | |||
| 172 | Longitude from module and sign. | ||
| 173 | |||
| 174 | =cut | ||
| 175 | |||
| 176 | sub mod2lon($$) { | ||
| 177 | my ($module, $sign) = @_; | ||
| 178 | return $_[0] / 0xffffffff * 180 * ($sign ? -1 : 1); | ||
| 179 | } | ||
| 180 | |||
| 181 | 1; |