File Coverage

blib/lib/Device/Gsm/Sms/Token.pm
Criterion Covered Total %
statement 54 63 85.7
branch 10 20 50.0
condition 2 5 40.0
subroutine 14 16 87.5
pod n/a
total 80 104 76.9


line stmt bran cond sub pod time code
1             # Device::Gsm::Sms::Token - SMS PDU message parser 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             # Commercial support is available. Write me if you are
13             # interested in new features or software support.
14              
15             package Sms::Token;
16              
17 11     11   32 use strict;
  11         9  
  11         240  
18 11     11   1650 use integer;
  11         37  
  11         71  
19 11     11   205 use Carp 'croak';
  11         11  
  11         523  
20              
21             # Token possible states
22 11     11   35 use constant ERROR => 0;
  11         11  
  11         454  
23 11     11   36 use constant ENCODED => 1;
  11         11  
  11         409  
24 11     11   35 use constant DECODED => 2;
  11         12  
  11         4730  
25              
26             #
27             # new token ( @data )
28             #
29             sub new {
30 106     106   1752 my ($proto, $name, $options) = @_;
31              
32             # my $class = ref $proto || $proto;
33 106   50     311 $options->{'data'} ||= [];
34              
35             # Cannot load a token without its name
36 106 50 33     308 if (!defined $name || $name eq '') {
37 0         0 return undef;
38             }
39              
40             # Create basic structure for a token
41             my %token = (
42              
43             # Name of token, see ->name()
44             __name => $name,
45              
46             # Data that token contains
47             __data => $options->{'data'},
48              
49             # Decoded? or error?
50             __state => '',
51              
52             # This is used to access other tokens in the "message"
53 106         249 __messageTokens => $options->{'messageTokens'}
54             );
55              
56             # Dynamically load required token module
57 106         110 eval { require "Device/Gsm/Sms/Token/$name.pm" };
  106         4789  
58 106 50       163 if ($@) {
59 0         0 warn( 'cannot load Device::Gsm::Sms::Token::'
60             . $name
61             . ' plug-in for decoding. Error: '
62             . $@);
63 0         0 return undef;
64             }
65              
66             # Try "static blessing" =:-o and see if it works
67 106         249 bless \%token, 'Sms::Token::' . $name;
68             }
69              
70             #
71             # Get/set internal token data
72             #
73             sub data {
74 105     105   80 my $self = shift;
75 105 50       151 if (@_) {
76 105 50       146 if (!defined $_[0]) {
77 0         0 $self->{'__data'} = [];
78             }
79             else {
80 105         177 $self->{'__data'} = [@_];
81             }
82             }
83 105         181 $self->{'__data'};
84             }
85              
86             # Must be implemented in real token
87             sub decode {
88 0     0   0 croak('decode() not implemented in token base class');
89 0         0 return 0;
90             }
91              
92             # Must be implemented in real token
93             sub encode {
94 0     0   0 croak('encode() not implemented in token base class');
95 0         0 return 0;
96             }
97              
98             sub get {
99 313     313   229 my ($self, $info) = @_;
100 313 50       338 return undef unless $info;
101              
102 313         552 return $self->{"_$info"};
103             }
104              
105             # XXX This must be filled by the higher level object that
106             # treats the entire message in tokens
107             #
108             # [token]->messageTokens( [name] )
109             #
110             sub messageTokens {
111              
112             # Usually this is a hash of token objects, accessible by key (token name)
113 2     2   2 my $self = shift;
114 2         1 my $name;
115 2 50       5 if (@_) {
116 2         3 $name = shift;
117             }
118 2 50       3 if (defined $name) {
119 2         9 return $self->{'__messageTokens'}->{$name};
120             }
121             else {
122 0         0 return $self->{'__messageTokens'};
123             }
124             }
125              
126             sub name {
127 208     208   130 my $self = shift;
128 208         479 return $self->{'__name'};
129             }
130              
131             sub set {
132 400     400   323 my ($self, $info, $newval) = @_;
133 400 50       454 return undef unless $info;
134 400 50       431 $newval = undef unless defined $newval;
135 400         702 $self->{"_$info"} = $newval;
136             }
137              
138             sub state {
139 89     89   65 my $self = shift;
140 89         99 return $self->{'__state'};
141             }
142              
143             sub toString {
144 13     13   271 my $self = shift;
145 13         13 my $string;
146 13 50       31 if (ref $self->{'__data'} eq 'ARRAY') {
147 13         13 $string = join '', @{ $self->{'__data'} };
  13         26  
148             }
149 13         32 return $string;
150             }
151              
152             1;
153