-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbalance.pl
executable file
·71 lines (61 loc) · 1.68 KB
/
balance.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/perl
#use Device::Gsm::Pdu;
##use Text::Iconv;
##$conv = Text::Iconv->new('utf16be','utf8');
use Encode;
# defaults
$opt_r = "/dev/ttyUSB2";
$opt_s = "/dev/ttyUSB0";
my $ussd = '*100#';
print "USSD MSG: $ussd\n";
my $ussd_req = ussd_pdu($ussd);
print "PDU ENCODED: [$ussd_req]\n";
#$ussd_req = Device::Gsm::Pdu::encode_text7($ussd);
#$ussd_req =~ s/^..//;
#print "PDU ENCODED: [$ussd_req]\n";
my $ussd_reply;
open (SENDPORT, '+<', $opt_s) or die "Can't open '$opt_s': $!\n";
print SENDPORT 'AT+CUSD=1,',"$ussd_req",",15\r\n";
close SENDPORT;
open (RCVPORT, $opt_r) or die "Can't open '$opt_r': $!\n";
print "Waiting for USSD reply...\n";
while (<RCVPORT>) {
chomp;
die "USSD ERROR\n" if $_ eq "+CUSD: 2";
if (/^\+CUSD: 0,\"([A-F0-9]+)\"/) {
$ussd_reply = $1;
print "PDU USSD REPLY: $ussd_reply\n";
last;
}
print "Got unknown USSD message: $_\n" if /^\+CUSD:/;
}
if($ussd_reply){
$decoded_ussd_reply = pdu_ussd_utf8($ussd_reply);
print STDOUT "USSD REPLY: $decoded_ussd_reply\n";
}else{
print "No USSD reply!\n";
}
sub ussd_pdu{
my $l_ussd=shift;
@a=split(//,unpack("b*",$l_ussd));
for ($i=7; $i < $#a; $i+=8){
$a[$i]="";
}
my $l_ret= uc(unpack("H*", pack("b*", join("", @a))))."";
return $l_ret;
}
sub pdu_ussd{
my $l_pdu=shift;
@a=split(//,unpack("b*", pack("H*",$l_pdu)));
for ($i=6; $i < $#a; $i+=7) {
$a[$i].="0";
}
my $l_ret= pack("b*", join("", @a))."";
return $l_ret;
}
sub pdu_ussd_utf8{
my $l_pdu=shift;
my $l_ret=pack "H*",$ussd_reply;
Encode::from_to($decoded_ussd_reply,'UTF-16BE','UTF-8');
return $l_ret;
}