# Device::Nokia
# Author: Tim Pope

# A Perl class to interface GSM devices as AT modems
# Basically just a few enhancements to Device::Gsm

package Device::Nokia;
$Device$Revision: 1.1 $ =~ /(\d+)\.(\d+)/;

use strict;
use Device::Gsm;

@Device::Nokia::ISA = ('Device::Gsm');

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

	# Test if manufacturer code command is supported
	if( $self->test_command('+CGMI') ) {

		$self->atsend( 'AT+CGMI' . Device::Modem::CR );
		($ok, $man) = $self->parse_answer();

		$self->log->write('info', 'manufacturer of this device appears to be ['.$man.']');

	}

	return $man || $ok;

}
#
# What is the model of this device?
#
sub model() {
	my $self = shift;
	my($code, $model);

	# Test if manufacturer code command is supported
	if( $self->test_command('+CGMM') ) {

		$self->atsend( 'AT+CGMM' . Device::Modem::CR );
		($code, $model) = $self->parse_answer();

		$self->log->write('info', 'model of this device is ['.$model.']');

	}

	return $model || $code;
}

#
# Get mobile phone indicators
#
sub indicators() {
	my $self = shift;
	my($supported,$values,%results);

	# Test if manufacturer code command is supported
	if( $self->test_command('+CIND') ) {
		$self->atsend( 'AT+CIND=?' . Device::Modem::CR );
		($_, $supported) = $self->parse_answer();
		$supported =~ s/^\+CIND: //;
		$self->atsend( 'AT+CIND?' . Device::Modem::CR );
		($_, $values) = $self->parse_answer();
		$values =~ s/^\+CIND: //;
		#@values = split (/,/, $values);
		foreach (split (/,/, $values)) {
		    $supported =~ s/\("([^"]*)",\([^)]*\)\),?//;
		    $results{$1} = $_;
		}
		$self->log->write('info', 'Indicator data retrieved (' . scalar(keys(%results)) . 'values)');

	}
	return %results;
}

#
# Get mobile phone battery strength
#
sub battery_strength() {
	my $self = shift;
	# Error code, dBm (signal power), bit error rate
	my($code, $strength, $line_power);

	# Test if signal quality command is implemented
	if( $self->test_command('+CBC') ) {

		$self->atsend( 'AT+CBC' . Device::Modem::CR );
		($code, $strength) = $self->parse_answer();

		if( $strength =~ /\+CBC: (\d+),(\d+)/ ) {

			($line_power, $strength) = ($1, $2);

			$self->log->write('info', 'battery strength is ['.$strength.'], line power ['.$line_power.']');

		} else {

			$self->log->write('warn', 'cannot obtain battery strength');

		}

	} else {

		$self->log->write('warn', 'battery strength command not supported!');

	}

	return wantarray ? ($line_power, $strength) : $strength;

}

1;

__END__
