#!/usr/bin/perl
use warnings;
use strict;

use Device::SerialPort;
use Data::Dump qw(dump);

my $device = $ENV{DEVICE} || '/dev/ttyUSB0';

my $port = Device::SerialPort->new( $device ) || die "$device: $!";
$port->baudrate(115200) || die "fail $device baudrate";
$port->parity("none") || die "fail $device parity";
$port->databits(8)    || die "fail $device databits";
$port->stopbits(1)    || die "fail $device stopbits";
$port->handshake("rts")  || die "fail $device handshake";

$port->write_settings || die "no $device settings";

$port->user_msg(1);
$port->error_msg(1);
$port->debug(1);

$port->read_char_time(3000);  # 5 s char timeout
$port->read_const_time(3000); # 1 s read timeout

while ( my $drain = $port->input ) {
	warn "# drain $drain\n# /drain\n";
}


warn "## using $device\n";

sub c {
	my ($cmd,$message) = @_;
	print STDERR ">>> $cmd >>> $message\n";
	$port->write("$cmd\r");
#	$port->write_done(1); # flush
	$port->write_drain;
	my $ret = $port->read(1);
again:
	while ( my $c = $port->input ) {
		print STDERR "<<< ",dump($ret . $c), $/;
		$ret .= $c;
#		last if $c =~ m/^OK\r/;
	}
	goto again unless $ret =~ s/\r\n(OK|ERROR)\r\n$//s || warn "ERROR: can't find OK or ERROR status!\n";
	$ret =~ s/^[\r\n]+//;
	print "$cmd [$message] = $ret\n";
	return $ret;
}

c 'ATE0' => 'echo off';
#c 'ATZ' => 'reset';
#
c 'ATI'  => 'info';

c 'AT+CGMI'  => 'manufacturer';

c 'AT+CGMM'  => 'model';

c 'AT+CGMR'  => 'revision';

c 'AT+CGSN'  => 'serial';

c 'AT+GCAP'  => 'capabilities';

c 'AT+CIMI'  => 'IMSI';

c 'AT+CNUM'  => 'MSISDN';

#c 'AT+COPS=?'  => 'operators'; # times out

c 'AT+CLCK=?' => 'facility lock';

c 'AT^CVOICE?' => 'is voice enabled?';


$port->close || die "can't close port $!";

warn "## $device closed\n";

