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-2006 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             # $Id$
13              
14             package Sms::Token::VP;
15 1     1   5 use integer;
  1         3  
  1         7  
16 1     1   29 use strict;
  1         2  
  1         28  
17 1     1   6 use Device::Gsm::Sms::Token;
  1         2  
  1         300  
18              
19             @Sms::Token::VP::ISA = ('Sms::Token');
20              
21             # takes (scalar message (string) reference)
22             # returns success/failure of decoding
23             # if all ok, removes token from message
24             sub decode {
25 2     2   4 my ($self, $rMessage) = @_;
26 2         3 my $ok = 0;
27              
28 2         12 my $vpf = $self->messageTokens('PDUTYPE')->VPF();
29              
30             # Check if VP flag is present
31 2 50       8 if ($vpf & 0x02) {
32              
33 2         9 my $vp = hex substr($$rMessage, 0, 2);
34              
35             # Decode value of VP field
36 2 50       14 if ($vp <= 0x8F) {
    50          
    50          
37 0         0 $vp = (($vp + 1) * 5) . ' minutes';
38             }
39             elsif ($vp <= 0xA7) {
40 0         0 $vp = ((24 + ($vp - 143)) * 30) . ' minutes';
41             }
42             elsif ($vp <= 0xC4) {
43 2         7 $vp = ($vp - 166) . ' days';
44             }
45             else {
46 0         0 $vp = ($vp - 192) . ' weeks';
47             }
48              
49 2         12 $self->set('validity_period' => $vp);
50 2         9 $self->data($vp);
51              
52             # Remove VP from message
53 2         7 $$rMessage = substr($$rMessage, 2);
54             }
55              
56 2         10 $self->state(Sms::Token::DECODED);
57              
58 2         8 return 1;
59             }
60              
61             #
62             # [token]->encode( [$data] )
63             #
64             # takes internal token data and encodes it, returning the result
65             # or undef value in case of errors
66             #
67             sub encode {
68 0     0     my $self = shift;
69              
70             # Take supplied data (optional) or object internal data
71 0           my $data = shift;
72 0 0 0       if (!defined $data || $data eq '') {
73 0           $data = $self->data();
74 0   0       $data ||= '00';
75             }
76              
77 0           return $data;
78             }
79              
80             1;