File Coverage

blib/lib/Device/Gsm/Sms/Token/SCA.pm
Criterion Covered Total %
statement 39 64 60.9
branch 6 14 42.8
condition 2 9 22.2
subroutine 4 5 80.0
pod n/a
total 51 92 55.4


line stmt bran cond sub pod time code
1             # Sms::Token::SCA - SMS SCA token (service center address)
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::SCA;
13 7     7   21 use integer;
  7         7  
  7         24  
14 7     7   141 use strict;
  7         10  
  7         90  
15 7     7   17 use Device::Gsm::Sms::Token;
  7         6  
  7         3024  
16              
17             @Sms::Token::SCA::ISA = ('Sms::Token');
18              
19             # takes (scalar message (string) reference)
20             # returns success/failure of decoding
21             # if all ok, removes SCA from message
22             sub decode {
23 17     17   20 my ($self, $rMessage) = @_;
24 17         15 my $ok = 0;
25 17         12 my ($length, $type, $address);
26 17         15 my $msg = $$rMessage;
27 17         14 my $msg_copy = $msg;
28              
29             # .------------.----------.---------------------------------.
30             # | LENGTH (1) | TYPE (1) | ADDRESS BCD DIGITS (0-8 octets) |
31             # `------------'----------'---------------------------------'
32 17         24 $length = substr $msg, 0, 2;
33              
34             # If length is `00', SCA = default end decoding ends
35 17 100       31 if ($length eq '00') {
36 2         4 $self->data('');
37 2         3 $self->state(Sms::Token::DECODED);
38              
39             # Remove length-octet read from message
40 2         3 $$rMessage = substr($$rMessage, 2);
41 2         6 return 1;
42             }
43              
44             # Begin decoding (length is number of octets for the SCA + 1 (length) )
45 15         81 $length = hex $length;
46              
47             # Length > 9 is impossible; max is 8 + 1 (length)
48 15 50       25 if ($length > 9) {
49 0         0 $self->data(undef);
50 0         0 $self->state(Sms::Token::ERROR);
51 0         0 return 0;
52             }
53              
54 15         44 $self->set('length' => $length);
55              
56             # Get type of message (81 = national, 91 = international, 80 = empty for status messages)
57 15         17 $type = substr $msg, 2, 2;
58 15 50 33     59 if ($type ne '81' and $type ne '91' and $type ne '80') {
      33        
59 0         0 $self->data(undef);
60 0         0 $self->state(Sms::Token::ERROR);
61 0         0 return 0;
62             }
63              
64 15         25 $self->set(type => $type);
65              
66             # Get rest of address
67 15         21 $address = substr $msg, 4, (($length - 1) << 1);
68              
69             # Reverse each pair of bcd digits
70 15         11 my $sca;
71 15         28 while ($address) {
72 88         349 $sca .= reverse substr($address, 0, 2);
73 88         123 $address = substr $address, 2;
74             }
75              
76             # Truncate last `F' if found (XXX)
77 15 50       24 chop $sca if substr($sca, -1) eq 'F';
78              
79             # If sca is international, put a '+' sign before
80 15 50       29 $sca = '+' . $sca if $type eq '91';
81              
82 15         37 $self->data($sca);
83 15         23 $self->set(type => $type);
84 15         22 $self->set('length' => $length);
85 15         29 $self->state(Sms::Token::DECODED);
86              
87             # Remove SCA info from message
88 15         28 $$rMessage = substr($msg, ($length + 1) << 1);
89              
90 15         38 return 1;
91             }
92              
93             #
94             # [token]->encode( [$data] )
95             #
96             # takes internal token data and encodes it, returning the result
97             # or undef value in case of errors
98             #
99             sub encode {
100 0     0     my $self = shift;
101              
102             # Take supplied data (optional) or object internal data
103 0           my $data = shift;
104 0 0 0       if (!defined $data || $data eq '') {
105 0           $data = $self->data();
106             }
107              
108             # Begin encoding as SCA
109 0           $data =~ s/\s+//g;
110              
111 0 0         my $type = index($data, '+') == 0 ? 91 : 81;
112              
113             # Remove all non-numbers
114 0           $data =~ s/\D//g;
115              
116 0           my $len = unpack 'H2' => chr(length $data);
117              
118 0           $data .= 'F';
119 0           my @digit = split // => $data;
120 0           my $encoded;
121              
122 0           while (@digit > 1) {
123 0           $encoded .= join '', reverse splice @digit, 0, 2;
124             }
125              
126 0           $data = uc $len . $type . $encoded;
127              
128 0           $self->data($data);
129 0           $self->set('length' => $len);
130 0           $self->set('type' => $type);
131 0           $self->state(Sms::Token::ENCODED);
132              
133 0           return $data;
134              
135             }
136              
137             1;