File Coverage

blib/lib/Device/Gsm/Sms/Token/VP.pm
Criterion Covered Total %
statement 21 30 70.0
branch 4 10 40.0
condition 0 5 0.0
subroutine 4 5 80.0
pod n/a
total 29 50 58.0


line stmt bran cond sub pod time code
1             # Sms::Token::VP - SMS VP (validity period) token
2             # Copyright (C) 2002-2015 Cosimo Streppone, cosimo@cpan.org
3             #
4             # This program is free software; you can redistribute it and/or modify
5             # it only under the terms of Perl itself.
6             #
7             # This program is distributed in the hope that it will be useful,
8             # but WITHOUT ANY WARRANTY; without even the implied warranty of
9             # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10             # Perl licensing terms for details.
11              
12             package Sms::Token::VP;
13 1     1   3 use integer;
  1         1  
  1         4  
14 1     1   20 use strict;
  1         0  
  1         13  
15 1     1   2 use Device::Gsm::Sms::Token;
  1         1  
  1         184  
16              
17             @Sms::Token::VP::ISA = ('Sms::Token');
18              
19             # takes (scalar message (string) reference)
20             # returns success/failure of decoding
21             # if all ok, removes token from message
22             sub decode {
23 2     2   2 my ($self, $rMessage) = @_;
24 2         3 my $ok = 0;
25              
26 2         4 my $vpf = $self->messageTokens('PDUTYPE')->VPF();
27              
28             # Check if VP flag is present
29 2 50       4 if ($vpf & 0x02) {
30              
31 2         3 my $vp = hex substr($$rMessage, 0, 2);
32              
33             # Decode value of VP field
34 2 50       6 if ($vp <= 0x8F) {
    50          
    50          
35 0         0 $vp = (($vp + 1) * 5) . ' minutes';
36             }
37             elsif ($vp <= 0xA7) {
38 0         0 $vp = ((24 + ($vp - 143)) * 30) . ' minutes';
39             }
40             elsif ($vp <= 0xC4) {
41 2         4 $vp = ($vp - 166) . ' days';
42             }
43             else {
44 0         0 $vp = ($vp - 192) . ' weeks';
45             }
46              
47 2         6 $self->set('validity_period' => $vp);
48 2         4 $self->data($vp);
49              
50             # Remove VP from message
51 2         3 $$rMessage = substr($$rMessage, 2);
52             }
53              
54 2         5 $self->state(Sms::Token::DECODED);
55              
56 2         5 return 1;
57             }
58              
59             #
60             # [token]->encode( [$data] )
61             #
62             # takes internal token data and encodes it, returning the result
63             # or undef value in case of errors
64             #
65             sub encode {
66 0     0     my $self = shift;
67              
68             # Take supplied data (optional) or object internal data
69 0           my $data = shift;
70 0 0 0       if (!defined $data || $data eq '') {
71 0           $data = $self->data();
72 0   0       $data ||= '00';
73             }
74              
75 0           return $data;
76             }
77              
78             1;