updated on Mon Jan 23 20:11:11 UTC 2012
[aur-mirror.git] / huawei-ussd / huawei-ussd.pl
blob22a8e55414b66e66fef1c4e337eba8a225954f2d
1 #!/usr/bin/perl
3 use Getopt::Std;
4 use Device::Gsm::Pdu;
5 use Text::Iconv;
7 # defaults
8 $opt_r = "/dev/ttyUSB2";
9 $opt_s = "/dev/ttyUSB0";
10 $conv = Text::Iconv->new('utf16be','utf8');
12 my $USAGE = <<__EOU;
14 Usage: $0 [-r input_port] [-s output_port] [-n] [-h] [-v] [-w] [-i] ussd_msg
16 Description:
17 Send and receive 7-bit PDU-encoded USSD messages.
18 Written and tested for Huawei E1550 GSM/UMTS USB modem.
20 Options:
21 -r port Port to receive data from. Default: $opt_r
22 -s port Port to send AT commands to. Default: $opt_s
23 -n Do not send any data to port. Useful with -v.
24 -h Print this help.
25 -v Be verbose.
26 -i Use iconv [from utf16be to utf8] to reply
27 -w reply workaround (try it if script can not decode reply)
28 __EOU
30 sub HELP_MESSAGE {print "$USAGE\n"; exit;}
31 sub VERSION_MESSAGE {};
32 getopts ('r:s:hnvwi');
33 HELP_MESSAGE() and exit if (! $ARGV[0]) or defined($opt_h);
35 print "USSD MSG: $ARGV[0]\n" if $opt_v;
36 my $ussd_req = Device::Gsm::Pdu::encode_text7($ARGV[0]);
37 $ussd_req =~ s/^..//;
38 print "PDU ENCODED: $ussd_req\n" if $opt_v;
40 my $ussd_reply;
41 if (! $opt_n) {
42 open (SENDPORT, '+<', $opt_s) or die "Can't open '$opt_s': $!\n";
43 print SENDPORT 'AT+CUSD=1,',$ussd_req,",15\r\n";
44 close SENDPORT;
45 open (RCVPORT, $opt_r) or die "Can't open '$opt_r': $!\n";
46 print "Waiting for USSD reply...\n" if $opt_v;
47 while (<RCVPORT>) {
48 chomp;
49 die "USSD ERROR\n" if $_ eq "+CUSD: 2";
50 if (/^\+CUSD: 0,\"([A-F0-9]+)\"/) {
51 $ussd_reply = $1;
52 print "PDU USSD REPLY: $ussd_reply\n" if $opt_v;
53 last;
55 print "Got unknown USSD message: $_\n" if /^\+CUSD:/ and $opt_v;
59 if ($ussd_reply) {
60 $iconved_reply = $conv->convert(pack('H*', $ussd_reply));
61 $decoded_ussd_reply = $opt_w ? pack('H*', $ussd_reply) : Device::Gsm::Pdu::decode_text7('00'.$ussd_reply);
62 print STDOUT "USSD REPLY: ".($opt_i ? $iconved_reply: $decoded_ussd_reply)."\n";
64 else
66 print "No USSD reply!\n";