blib/lib/Data/ESN.pm

filter: Statementstmt 100% 54 / 54 Branchbran 100% 16 / 16 Conditioncond 100% 6 / 6 MC/DCmcdc 100% 4 / 4 Subroutinesub 100% 11 / 11 Podpod 100% 5 / 5 totaltotal 100% 96 / 96 CC 11 SCAR 10.0
CC
11
Cov
100%
esn_is_dec
5.0
esn_valid
3.0
esn_is_hex
3.0
CRAP 2.7
↑ Summary
1 package Data::ESN;
2  
3 3 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10  
11 3 ▶use vars qw(@ISA @EXPORT_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 # base class Exporter
14 3 our @ISA = qw(Exporter);
15  
16 3 our @EXPORT_OK = qw(esn_to_hex esn_to_dec esn_valid esn_is_hex esn_is_dec);
17  
18  
19  
20 =head1 NAME
21  
22 Data::ESN - Manipulate mobile ESN values
23  
24 =head1 VERSION
25  
26 Version 0.04
27  
28 =cut
29  
30 3 our $VERSION = '0.04';
31  
32  
33 =head1 SYNOPSIS
34  
35 Quick summary of what the module does.
36  
37 Perhaps a little code snippet.
38  
39 use Data::ESN qw( esn_to_hex esn_to_dec esn_valid esn_is_hex esn_is_dec );
40  
41 my $esn_hex = '2df812ca';
42  
43 # if we have a Hex ESN, convert it to Decimal
44 my $esn_dec = esn_to_dec( $esn_hex ) if esn_is_hex( $esn_hex );
45  
46 # if we have a Decimal ESN, convert it to Hex
47 $esn_hex = esn_to_hex( $esn_dec ) if esn_is_dec( $esn_dec );
48  
49 print "valid esn found" if esn_valid( $esn_hex );
50  
51 # print if we have a 'dec' or 'hex' ESN
52 print "our ESN is a " . esn_valid( $esn_hex ) . " esn.";
53
54  
55 =head1 EXPORT
56  
57 esn_to_hex
58 esn_to_dec
59 esn_valid
60 esn_is_hex
61 esn_is_dec
62  
63 =head1 SUBROUTINES/METHODS
64  
65 =head2 esn_to_hex
66  
67 Converts a Decimal ESN to Hex. If an invalid ESN is used, it will throw
68 a warning and return to 0.
69  
70 =cut
71  
72 3 ▶sub esn_to_hex {
Subroutine esn_to_hex: called CC=2 SCAR=6.9
Pod documented
73  
74 3 my $esn = shift;
75  
76 3 ▶ unless ( esn_is_dec($esn) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (esn_is_dec($esn)) 1 2 3
77 1 carp "invalid ESN ($esn) used for decimal to hex conversion 'esn_to_hex";
78 1 return 0;
79 }
80  
81 # take first three digits, convert to hex
82 2 $esn =~ m/^(.{3})(.{8})$/;
83  
84 2 my $manufacturer = sprintf("%02x", $1);
85 2 my $device = sprintf("%06x", $2);
86  
87 2 return $manufacturer . $device;
88  
89 }
90  
91 =head2 esn_to_dec
92  
93 Convert a Hex ESN to Decimal. If an invalid Hex ESN is used, it will throw
94 a warning and return 0.
95  
96 =cut
97  
98 2 ▶sub esn_to_dec {
Subroutine esn_to_dec: called CC=2 SCAR=6.9
Pod documented
99  
100 2 my $esn = shift;
101  
102 2 ▶ unless ( esn_is_hex($esn) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (esn_is_hex($esn)) 1 1 2
103 1 carp "invalid ESN ($esn) used for decimal to hex conversion 'esn_to_dec";
104 1 return 0;
105 }
106  
107 # take first two digits, convert to hex
108 1 $esn =~ m/^(.{2})(.{6})$/;
109  
110 1 my $manufacturer = sprintf("%03d", hex $1);
111 1 my $device = sprintf("%08d", hex $2);
112  
113 1 return $manufacturer . $device;
114  
115 }
116  
117 =head2 esn_valid
118  
119 Check to see if the ESN looks like a valid ESN, either Decimal or Hex. If it
120 looks like a Decimal ESN, it returns "dec". If it looks like a Hex ESN it
121 returns "hex". If it doesn't match either it returns 0
122  
123 =cut
124  
125 3 ▶sub esn_valid {
Subroutine esn_valid: called CC=3 SCAR=11.0
Pod documented
126  
127 3 my $esn = shift;
128  
129 3 ▶ return 'hex' if esn_is_hex($esn);
Branch: 1 branch
BranchTrueFalseTotal
if esn_is_hex($esn) 1 2 3
130 2 ▶ return 'dec' if esn_is_dec($esn);
Branch: 1 branch
BranchTrueFalseTotal
if esn_is_dec($esn) 1 1 2
131 1 return 0;
132  
133 }
134  
135 =head2 esn_is_hex
136  
137 If ESN appears to be Hex, return 1, else return 0. A valid Hex ESN is 8 digits
138 in length, 00000000 to FFFFFFFF inclusive.
139  
140 =cut
141  
142 9 ▶sub esn_is_hex {
Subroutine esn_is_hex: called CC=3 SCAR=11.0
Pod documented
143  
144 9 my $esn = shift;
145  
146 # a valid esn is defined, 8 digits in length, and digit / a thru f characters
147 9 ▶ if ( defined $esn and $esn =~ /^[0-9a-f]{8}$/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $esn and $esn =~ /^[0-9a-f]{8}$/i) { } 3 6 9
Condition100%3/3
defined $esn and $esn =~ /^[0-9a-f]{8}$/i
!ll&&!rl&&r
153
MC/DC100%2/2
defined $esn and $esn =~ /^[0-9a-f]{8}$/i
defined $esn $esn =~ /^[0-9a-f]{8}$/i
Truth table100%3/3
A and B
A= defined $esnB= $esn =~ /^[0-9a-f]{8}$/i
ABresult
0X0
100
111
148 3 return 1;
149 } else {
150 6 return 0;
151 }
152  
153 }
154  
155 =head2 esn_is_dec
156  
157 If ESN appears to be Decimal, return 1, else return 0. A valid Decimal ESN is
158 11 digits in length, with the first three digits between 0 and 255 inclusive
159 and the last 8 digits between 00000000 and 16777215 inclusive.
160  
161 =cut
162  
163 11 ▶sub esn_is_dec {
Subroutine esn_is_dec: called CC=5 SCAR=16.1
Pod documented
164  
165 11 my $esn = shift;
166  
167 11 ▶ if ( defined $esn and $esn =~ /^[0-9]{11}$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $esn and $esn =~ /^[0-9]{11}$/) { } 6 5 11
Condition100%3/3
defined $esn and $esn =~ /^[0-9]{11}$/
!ll&&!rl&&r
146
MC/DC100%2/2
defined $esn and $esn =~ /^[0-9]{11}$/
defined $esn $esn =~ /^[0-9]{11}$/
Truth table100%3/3
A and B
A= defined $esnB= $esn =~ /^[0-9]{11}$/
ABresult
0X0
100
111
168 6 $esn =~ m/^(\d{3})(\d{8})$/;
169 6 my $manufacturer = $1;
170 6 my $device = $2;
171  
172 # manufacturer ID can be 0x00 to 0xFF
173 6 ▶ return 0 if $manufacturer > 255;
Branch: 1 branch
BranchTrueFalseTotal
if $manufacturer > 255 1 5 6
174  
175 # devcie ID can be 0x000000 to 0xFFFFFF
176 5 ▶ return 0 if $device > 16777215;
Branch: 1 branch
BranchTrueFalseTotal
if $device > 16777215 1 4 5
177
178 # if we got here, we have a valid decimal ESN
179 4 return 1;
180  
181 } else {
182  
183 5 return 0;
184  
185 }
186  
187 }
188  
189  
190 =head1 AUTHOR
191  
192 Adam Wohld, C<< <adam at spatialsystems.org> >>
193  
194 =head1 BUGS
195  
196 Please report any bugs or feature requests to C<bug-data-esn at rt.cpan.org>, or through
197 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Data-ESN>. I will be notified, and then you'll
198 automatically be notified of progress on your bug as I make changes.
199  
200  
201  
202  
203 =head1 SUPPORT
204  
205 You can find documentation for this module with the perldoc command.
206  
207 perldoc Data::ESN
208  
209  
210 You can also look for information at:
211  
212 =over 4
213  
214 =item * RT: CPAN's request tracker (report bugs here)
215  
216 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Data-ESN>
217  
218 =item * AnnoCPAN: Annotated CPAN documentation
219  
220 L<http://annocpan.org/dist/Data-ESN>
221  
222 =item * CPAN Ratings
223  
224 L<http://cpanratings.perl.org/d/Data-ESN>
225  
226 =item * Search CPAN
227  
228 L<http://search.cpan.org/dist/Data-ESN/>
229  
230 =back
231  
232  
233 =head1 ACKNOWLEDGEMENTS
234  
235  
236 =head1 LICENSE AND COPYRIGHT
237  
238 Copyright 2012 Adam Wohld.
239  
240 This program is free software; you can redistribute it and/or modify it
241 under the terms of either: the GNU General Public License as published
242 by the Free Software Foundation; or the Artistic License.
243  
244 See http://dev.perl.org/licenses/ for more information.
245  
246  
247 =cut
248  
249 3 1; # End of Data::ESN
↑ Summary