Device-Gsm
view release on metacpan or search on metacpan
lib/Device/Gsm.pm view on Meta::CPAN
Returns the current registered or preferred GSM network operator. Example:
my $net_name = $gsm->network();
# Returns 'Wind Telecom Spa'
my($net_name, $net_code) = $gsm->network();
# Returns ('Wind Telecom Spa', '222 88')
This obviously varies depending on country and network operator. For me now,
it holds "Wind Telecomunicazioni SpA". It is not guaranteed that the mobile
phone returns the decoded network name. It can also return a gsm network code,
like C<222 88>. In this case, an attempt to decode the network name is made.
Be sure to call the C<network()> method when already registered to gsm
network. See C<register()> method.
=head2 signal_quality()
Returns the measure of signal quality expressed in dBm units, where near to
zero is better. An example value is -91 dBm, and reported value is C<-91>.
lib/Device/Gsm/Pdu.pm view on Meta::CPAN
return (uc $len . $type . $encoded);
}
sub decode_text_UCS2 {
my $encoded = shift;
return undef unless $encoded;
my $len = hex substr($encoded, 0, 2);
$encoded = substr $encoded, 2;
my $decoded = "";
while ($encoded) {
$decoded .= pack("C0U", hex(substr($encoded, 0, 4)));
$encoded = substr($encoded, 4);
}
return $decoded;
}
sub encode_text7 {
uc
unpack 'H*',
pack 'C b*',
length $_[0],
join '',
unpack '(b7)*', $_[0];
}
#
#return complete ud with udh
#remains for comatibility reasons with my production scripts :)
#
sub encode_text7_udh1 {
my $decoded = shift;
my $udh1 = shift;
my $decoded_length = length($decoded);
$decoded = Device::Gsm::Charset::iso8859_to_gsm0338($decoded);
my $pdu_msg = uc
unpack 'H*',
pack 'b*',
#add one bit of padding to align septet boundary
'0' . join '', unpack '(b7)*', $decoded;
#below add 7 septets length for udh1
return
sprintf("%02X", $decoded_length + Sms::Token::UDH::UDH1_LENGTH)
. $udh1
. $pdu_msg;
}
#
#encode text with padding
#
sub encode_text7_udh {
my $decoded = shift;
my $padding = shift;
$padding = 0 unless ($padding);
my $decoded_length = length($decoded);
$decoded = Device::Gsm::Charset::iso8859_to_gsm0338($decoded);
my $pdu_msg = uc
unpack 'H*',
pack 'b*',
#add bits of padding to align septet boundary
'0' x $padding . join '', unpack '(b7)*', $decoded;
#below add septets length of text
my $len_hex = sprintf("%02X", $decoded_length);
return
wantarray
? ($len_hex, $pdu_msg, $len_hex . $pdu_msg)
: $len_hex . $pdu_msg;
}
sub pdu_to_latin1 {
# Reattach a length octet.
my $s = shift;
my $len = length $s;
#arn "len=$len, len/2=", $len/2, "\n";
my $l = uc unpack("H*", pack("C", int(length($s) / 2 * 8 / 7)));
if (length($l) % 2 == 1) { $l = '0' . $l }
my $pdu = $l . $s;
#arn "l=$l, pdu=$pdu\n";
my $decoded = Device::Gsm::Pdu::decode_text7($pdu);
#arn "decoded_text7=$decoded\n";
my $latin1 = Device::Gsm::Charset::gsm0338_to_iso8859($decoded);
#arn "latin1=$latin1\n";
return $latin1;
}
sub latin1_to_pdu {
my $latin1_text = $_[0];
#arn "latin1=$latin1_text\n";
my $gsm0338 = Device::Gsm::Charset::iso8859_to_gsm0338($latin1_text);
lib/Device/Gsm/Sms.pm view on Meta::CPAN
= $2; # Status of message (REC READ/UNREAD, STO, ...);
$self->{'alpha'} = $3; # Alphanumeric representation of sender
$self->{'length'} = $4; # Final length of message
$self->{'pdu'} = $opt{'pdu'}; # PDU content
$self->{'storage'} = $opt{'storage'}; # Storage (SM or ME)
bless $self, $class;
if ($self->decode(Device::Gsm::Sms::SMS_DELIVER)) {
# _log('OK, message decoded correctly!');
}
elsif ($self->decode(Device::Gsm::Sms::SMS_STATUS)) {
}
else {
# _log('CASINO!');
undef $self;
}
}
elsif ($opt{'header'} =~ /\+CDS:\s*(\d+)/o) {
$self->{'mr'} = $1; # Message number
$self->{'pdu'} = $opt{'pdu'}; # PDU content
bless $self, $class;
if ($self->decode(Device::Gsm::Sms::SMS_STATUS)) {
# _log('OK, message decoded correctly!');
}
else {
# _log('CASINO!');
undef $self;
}
}
else {
lib/Device/Gsm/Sms.pm view on Meta::CPAN
# Get list of tokens for this message (from ::Sms::Structure)
my $cPdu = $self->{'pdu'};
# Check that PDU is not empty
return 0 unless $cPdu;
# Backup copy for "backtracking"
my $cPduCopy = $cPdu;
my @token_names = $self->structure();
my $decoded = 1;
#is udh in pdu?
my $udh_parsed = 0;
while (@token_names) {
# Create new token object
my $token = new Sms::Token(shift @token_names,
{ messageTokens => $self->{'tokens'} });
if (!defined $token) {
$decoded = 0;
last;
}
# If decoding is completed successfully, add token object to message
#_log('PDU BEFORE ['.$cPdu.']', length($cPdu) );
if ($token->decode(\$cPdu)) {
# Store token object into SMS message
$self->{'tokens'}->{ $token->name() } = $token;
lib/Device/Gsm/Sms.pm view on Meta::CPAN
}
}
#_log('PDU AFTER ['.$cPdu.']', length($cPdu) );
}
#_log("\n", 'PRESS ENTER TO CONTINUE'); <STDIN>;
return $decoded;
}
#
# Delete an sms message
#
sub delete {
my $self = $_[0];
my $gsm = $self->_parent();
my $ok;
lib/Device/Gsm/Sms.pm view on Meta::CPAN
Example:
$gsm = Device::Gsm->new();
...
my @msg = $gsm->messages();
$msg[0] && $msg[0]->delete();
=head2 new()
Basic constructor. You can build a new C<Device::Gsm::Sms> object from the
raw B<+CMGL> header and B<PDU> data. Those data is then decoded and a new
sms object is instanced and all information filled, to be available
for subsequent method calls.
The allowed parameters to new() method are:
=over 4
=item header
This is the raw B<+CMGL> header string as modem outputs when you
lib/Device/Gsm/Sms.pm view on Meta::CPAN
=head2 storage()
Returns the storage where SMS has been read from.
=head2 text()
Returns the textual content of sms message.
=head2 token()
Returns the given PDU token of the decoded message (internal usage).
=head2 type()
SMS messages can be of two types: SMS_SUBMIT and SMS_DELIVER, that are defined by
two constants with those names. type() method returns one of these two values.
Example:
if( $sms->type() == Device::Gsm::Sms::SMS_DELIVER ) {
# ...
lib/Device/Gsm/Sms/Token/DT.pm view on Meta::CPAN
. $self->get('year'));
$self->set('time' => $self->get('hour') . ':'
. $self->get('minute') . ':'
. $self->get('second'));
# TODO: add timezone decoding ...
$self->data($self->get('date') . ' '
. $self->get('time') . ' '
. $self->get('timezone'));
# Signal token as correctly decoded (?)
$self->state(Sms::Token::DECODED);
# Remove DT info from message
$$rMessage = substr($$rMessage, 14);
return 1;
}
#
# [token]->encode( [$data] )
lib/Device/Gsm/Sms/Token/SCTS.pm view on Meta::CPAN
. $self->get('year'));
$self->set('time' => $self->get('hour') . ':'
. $self->get('minute') . ':'
. $self->get('second'));
# TODO: add timezone decoding ...
$self->data($self->get('date') . ' '
. $self->get('time') . ' '
. $self->get('timezone'));
# Signal token as correctly decoded (?)
$self->state(Sms::Token::DECODED);
# Remove SCTS info from message
$$rMessage = substr($$rMessage, 14);
return 1;
}
#
# [token]->encode( [$data] )
t/05messages.t view on Meta::CPAN
my %options = ( baudrate => $baud );
$options{'pin'} = $pin if defined($pin) && $pin ne '';
ok( $gsm->connect(%options) );
my @msg = $gsm->messages();
foreach my $msg ( @msg ) {
print 'MSG ', $msg->{'index'}, "\n";
print ' ty', $msg->type(), "\n";
print 'PDU(', $msg->{'pdu'}, ")\n";
print 'DEC(', ($msg->{'decoded'}||''), ")\n";
print "-" x 72, "\n";
}
$gsm->disconnect();
}
t/06msgcodec.t view on Meta::CPAN
# Test for outgoing message decoding
[ '+CMGL: 5,3,,35' => '0011FF048160110000AD1CD4F29C0E6A97E7F3F0B90C32BFE52062D99E1E9775BAE3BC0D', 'Test message for Device::Gsm']
);
foreach my $m ( @messages ) {
#diag('-' x 72, "\n", "Header: $m->[0]\n", "PDU : $m->[1]\n");
my $msg = new Device::Gsm::Sms( header => $m->[0], pdu => $m->[1] );
#diag('Sender `', $msg->sender(), '\'');
#diag('Text `', $msg->text(), '\'');
if( $m->[2] ) {
is( $m->[2], $msg->text(), 'check of decoded message' );
} else {
is( $m->[2], $m->[2], 'missing decoded message text' );
}
}
( run in 0.300 second using v1.01-cache-2.11-cpan-a9ef4e587e4 )