File Coverage

blib/lib/Data/ESN.pm
Criterion Covered Total %
statement 50 50 100.0
branch 16 16 100.0
condition 6 6 100.0
subroutine 11 11 100.0
pod 5 5 100.0
total 88 88 100.0


line stmt bran cond sub pod time code
1             package Data::ESN;
2              
3 2     2   86523 use 5.006;
  2         5  
4 2     2   9 use strict;
  2         3  
  2         39  
5 2     2   9 use warnings;
  2         8  
  2         53  
6              
7 2     2   7 use Exporter;
  2         8  
  2         86  
8 2     2   11 use Carp;
  2         2  
  2         167  
9              
10              
11 2     2   9 use vars qw(@ISA @EXPORT_OK);
  2         3  
  2         1269  
12              
13             # base class Exporter
14             our @ISA = qw(Exporter);
15              
16             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             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             sub esn_to_hex {
73              
74 3     3 1 59 my $esn = shift;
75              
76 3 100       7 unless ( esn_is_dec($esn) ) {
77 1         24 carp "invalid ESN ($esn) used for decimal to hex conversion 'esn_to_hex";
78 1         556 return 0;
79             }
80              
81             # take first three digits, convert to hex
82 2         5 $esn =~ m/^(.{3})(.{8})$/;
83              
84 2         8 my $manufacturer = sprintf("%02x", $1);
85 2         5 my $device = sprintf("%06x", $2);
86              
87 2         21 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             sub esn_to_dec {
99              
100 2     2 1 34 my $esn = shift;
101              
102 2 100       6 unless ( esn_is_hex($esn) ) {
103 1         13 carp "invalid ESN ($esn) used for decimal to hex conversion 'esn_to_dec";
104 1         598 return 0;
105             }
106              
107             # take first two digits, convert to hex
108 1         3 $esn =~ m/^(.{2})(.{6})$/;
109              
110 1         6 my $manufacturer = sprintf("%03d", hex $1);
111 1         4 my $device = sprintf("%08d", hex $2);
112              
113 1         18 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             sub esn_valid {
126              
127 3     3 1 6 my $esn = shift;
128              
129 3 100       4 return 'hex' if esn_is_hex($esn);
130 2 100       5 return 'dec' if esn_is_dec($esn);
131 1         4 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             sub esn_is_hex {
143              
144 9     9 1 12 my $esn = shift;
145              
146             # a valid esn is defined, 8 digits in length, and digit / a thru f characters
147 9 100 100     56 if ( defined $esn and $esn =~ /^[0-9a-f]{8}$/i ) {
148 3         18 return 1;
149             } else {
150 6         19 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             sub esn_is_dec {
164              
165 11     11 1 14 my $esn = shift;
166              
167 11 100 100     62 if ( defined $esn and $esn =~ /^[0-9]{11}$/ ) {
168 6         15 $esn =~ m/^(\d{3})(\d{8})$/;
169 6         11 my $manufacturer = $1;
170 6         10 my $device = $2;
171              
172             # manufacturer ID can be 0x00 to 0xFF
173 6 100       20 return 0 if $manufacturer > 255;
174              
175             # devcie ID can be 0x000000 to 0xFFFFFF
176 5 100       15 return 0 if $device > 16777215;
177            
178             # if we got here, we have a valid decimal ESN
179 4         18 return 1;
180              
181             } else {
182              
183 5         34 return 0;
184              
185             }
186              
187             }
188              
189              
190             =head1 AUTHOR
191              
192             Adam Wohld, C<< >>
193              
194             =head1 BUGS
195              
196             Please report any bugs or feature requests to C, or through
197             the web interface at L. 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
217              
218             =item * AnnoCPAN: Annotated CPAN documentation
219              
220             L
221              
222             =item * CPAN Ratings
223              
224             L
225              
226             =item * Search CPAN
227              
228             L
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             1; # End of Data::ESN