Device-Gsm

 view release on metacpan or  search on metacpan

lib/Device/Gsm.pm  view on Meta::CPAN

            }

        }

    }

    return $datetime;

}

#
# Delete a message from sim card
#
sub delete_sms {
    my $self      = shift;
    my $msg_index = shift;
    my $storage   = shift;
    my $ok;

    if (!defined $msg_index || $msg_index eq '') {
        $self->log->write(
            'warn',
            'undefined message number. cannot delete sms message'
        );
        return 0;
    }

    # Set default SMS storage if supported
    $self->storage($storage);

    $self->atsend(qq{AT+CMGD=$msg_index} . Device::Modem::CR);

    my $ans = $self->parse_answer($Device::Modem::STD_RESPONSE);
    if (index($ans, 'OK') > -1 || $ans =~ /\+CMGD/) {
        $ok = 1;
    }

    $self->log->write(
        'info',
        "deleting sms n.$msg_index from storage "
            . ($storage || "default")
            . " (result: `$ans') => "
            . ($ok ? 'ok' : '*FAILED*')
    );

    return $ok;
}

#
# Call forwarding
#
sub forward {
    my ($self, $reason, $mode, $number) = @_;

    $reason = lc $reason || 'unconditional';
    $mode   = lc $mode   || 'register';
    $number ||= '';

    my %reasons = (
        'unconditional' => 0,
        'busy'          => 1,
        'no reply'      => 2,
        'unreachable'   => 3
    );

    my %modes = (
        'disable'  => 0,
        'enable'   => 1,
        'query'    => 2,
        'register' => 3,
        'erase'    => 4
    );

    my $reasoncode = $reasons{$reason};
    my $modecode   = $modes{$mode};

    $self->log->write(
        'info',
        qq{setting $reason call forwarding to [$number]}
    );
    $self->atsend(
        qq{AT+CCFC=$reasoncode,$modecode,"$number"} . Device::Modem::CR);

    return $self->parse_answer($Device::Modem::STD_RESPONSE, 15000);
}

#
# Hangup and terminate active call(s)
# this overrides the `Device::Modem::hangup()' method
#
sub hangup {
    my $self = shift;
    $self->log->write('info', 'hanging up...');
    $self->attention();
    $self->atsend('AT+CHUP' . Device::Modem::CR);
    $self->flag('OFFHOOK', 0);
    $self->answer(undef, 5000);
}

#
# Who is the manufacturer of this device?
#
sub manufacturer {
    my $self = shift;
    my ($ok, $man);

    # We can't test for command support, because some phones, mainly Motorola
    # will spit out an error, instead of telling if CGMI is supported.
    $self->atsend('AT+CGMI' . Device::Modem::CR);
    ($ok, $man) = $self->parse_answer($Device::Modem::STD_RESPONSE);

    if ($ok ne 'OK') {
        $self->log->write(
            'warn',
            'manufacturer command ended with error [' . $ok . $man . ']'
        );
        return undef;
    }

    # Again, seems that Motorola phones will re-echo
    # the CGMI command header, instead of giving us the

lib/Device/Gsm.pm  view on Meta::CPAN

Example:

    my $gsm = Device::Gsm->new( port=>'/dev/ttyS0', log=>'syslog' );
    # ...
    if( $gsm->connect(baudrate => 19200) ) {
        print "Connected!";
    } else {
        print "Could not connect, sorry!";
    }
    # ...

=head2 datetime()

Used to get or set your phone/gsm modem date and time.

If called without parameters, it gets the current phone/gsm date and time in
"gsm" format, "YY/MM/DD,HH:MN:SS". For example C<03/12/15,22:48:59> means
December the 15th, at 10:48:59 PM. Example:

    $datestr = $gsm->datetime();

If called with parameters, sets the current phone/gsm date and time to that
of supplied value. Example:

    $newdate = $gsm->datetime( time() );

where C<time()> is the perl's builtin C<time()> function (see
C<perldoc -f time> for details). Another variant allows one to pass a
C<localtime> array to set the correspondent datetime. Example:
 
    $newdate = $gsm->datetime( localtime() );

(Note the list context). Again you can read the details for C<localtime>
function with C<perldoc -f localtime>.

If your device does not support this command, an B<undefined> value will be
returned in either case.


=head2 delete_sms()

This method deletes a message from your SIM card, given the message index
number.  Example:

    $gsm->delete_sms(3);

An optional second parameter specifies the "storage". It allows one to delete
messages from gsm phone memory or sim card memory. Example:

    # Deletes first message from gsm phone memory
    $gsm->delete_sms(1, 'ME');

    # Deletes 3rd message from sim card
    $gsm->delete_sms(3, 'SM');

By default, it uses the currently set storage, via the C<storage()> method.

=head2 forward()

Sets call forwarding. Accepts three arguments: reason, mode and number.
Reason can be the string C<unconditional>, C<busy>, C<no reply> and
C<unreachable>.  Mode can be the string C<disable>, C<enable>, C<query>,
C<register>, C<erase>.  

Example:

    # Set unconditional call forwarding to +47 123456789
    $gsm->forward('unconditional','register','+47123456789');

    # Erase unconditional call forwarding
    $gsm->forward('unconditional','erase');


=head2 hangup()

Hangs up the phone, terminating the active calls, if any.
This method has been never tested on real "live" conditions, but it needs to be
specialized for GSM phones, because it relies on C<+HUP> GSM command.
Example:

    $gsm->hangup();


=head2 imei()

Returns the device own IMEI number
(B<International Mobile Station Equipment Identity>).

This identifier is numeric and is supposed to be unique among all GSM mobile
devices and phones. Example:

    my $imei = $gsm->imei();


=head2 manufacturer()

Returns the device manufacturer, usually only the first word (example: C<Nokia>,
C<Siemens>, C<Falcom>, ...). Example:

    my $man_name = $gsm->manufacturer();
    if( $man_name eq 'Nokia' ) {
        print "We have a nokia phone...";
    } else {
        print "We have a $man_name phone...";
    }


=head2 messages()

This method is a somewhat unstable and subject to change, but for now it seems
to work. It is meant to extract all text SMS messages stored on your SIM card
or gsm phone.  In list context, it returns a list of messages (or undefined
value if no message or errors), every message being a C<Device::Gsm::Sms>
object.

The only parameter specifies the C<storage> where you want to read the
messages, and can assume some of the following values (but check your
phone/modem manual for special manufacturer values):

=over 4



( run in 1.176 second using v1.01-cache-2.11-cpan-75ffa21a3d4 )