7157a30559d8198da1b9fa5e07c6ac05dd2c40c8
[huawei.git] / at-chat.pl
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4
5 use Device::SerialPort;
6 use Data::Dump qw(dump);
7
8 my $device = $ENV{DEVICE} || '/dev/ttyUSB0';
9
10 my $port = Device::SerialPort->new( $device ) || die "$device: $!";
11 $port->baudrate(115200) || die "fail $device baudrate";
12 $port->parity("none") || die "fail $device parity";
13 $port->databits(8)    || die "fail $device databits";
14 $port->stopbits(1)    || die "fail $device stopbits";
15 $port->handshake("rts")  || die "fail $device handshake";
16
17 $port->write_settings || die "no $device settings";
18
19 $port->user_msg(1);
20 $port->error_msg(1);
21 $port->debug(1);
22
23 $port->read_char_time(3000);  # 5 s char timeout
24 $port->read_const_time(3000); # 1 s read timeout
25
26 while ( my $drain = $port->input ) {
27         warn "# drain $drain\n# /drain\n";
28 }
29
30
31 warn "## using $device\n";
32
33 sub c {
34         my ($cmd,$message) = @_;
35         print STDERR ">>> $cmd >>> $message\n";
36         $port->write("$cmd\r");
37 #       $port->write_done(1); # flush
38         $port->write_drain;
39         my $ret = $port->read(1);
40 again:
41         while ( my $c = $port->input ) {
42                 print STDERR "<<< ",dump($ret . $c), $/;
43                 $ret .= $c;
44 #               last if $c =~ m/^OK\r/;
45         }
46         goto again unless $ret =~ s/\r\n(OK|ERROR)\r\n$//s || warn "ERROR: can't find OK or ERROR status!\n";
47         $ret =~ s/^[\r\n]+//;
48         print "$cmd [$message] = $ret\n";
49         return $ret;
50 }
51
52 c 'ATE0' => 'echo off';
53 #c 'ATZ' => 'reset';
54 #
55 c 'ATI'  => 'info';
56
57 c 'AT+CGMI'  => 'manufacturer';
58
59 c 'AT+CGMM'  => 'model';
60
61 c 'AT+CGMR'  => 'revision';
62
63 c 'AT+CGSN'  => 'serial';
64
65 c 'AT+GCAP'  => 'capabilities';
66
67 c 'AT+CIMI'  => 'IMSI';
68
69 c 'AT+CNUM'  => 'MSISDN';
70
71 #c 'AT+COPS=?'  => 'operators'; # times out
72
73 c 'AT+CLCK=?' => 'facility lock';
74
75 c 'AT^HWVER' => 'hardware version';
76
77 c 'AT^CVOICE?' => 'is voice enabled?';
78
79 c 'AT^DDSETEX=?' => 'voice output';
80
81 c 'AT^SYSINFO' => 'sys info';
82
83
84 $port->close || die "can't close port $!";
85
86 warn "## $device closed\n";
87