-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathumtscardtool
executable file
·214 lines (193 loc) · 4.46 KB
/
umtscardtool
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/usr/bin/perl
#
# Control program for the Ericsson F3507g Minicard.
#
# (c) 2010 Andread Pohl
use strict;
use Device::SerialPort;
#use Getopt::Long;
$|++;
my $pin = "0808";
my $devNameControl = "Ericsson F3507g Mobile Broadband Minicard Data Modem";
my $devNameGPS = "Ericsson F3507g Mobile Broadband Minicard GPS Port";
my $port;
my %erinfo = ('0,1,0' => "GPRS",
'0,0,1' => "3G",
'0,0,2' => "HSDPA");
#$result = GetOptions("length=i" => \$length, # numeric
# "file=s" => \$data, # string
#"verbose" => \$verbose); # flag
main();
sub main {
init();
my $cmd = $ARGV[0];
if ($cmd eq "enable") {
if ($ARGV[1] eq "card") {
enableCard();
} elsif ($ARGV[1] eq "gps") {
}
} elsif ($cmd eq "disable") {
if ($ARGV[1] eq "card") {
disableCard();
} elsif ($ARGV[1] eq "gps") {
}
} elsif ($cmd eq "status") {
status();
} elsif ($cmd eq "send") {
print call($ARGV[1])."\n";
} else {
usage();
}
$port->close();
}
sub usage {
print <<__eof
Usage: $0 [Options] Command
Commands:
enable/disable {card|gps} Enable or disable a device.
status Show status (network, signal, ...)
send <at command> Send an AT command. (eg: $0 send AT)
__eof
;
}
#
# Status
#
sub status {
#$port->read_const_time(300);
my $indicators = call("AT+CIND?");
$indicators =~ s/\+CIND: //;
my ($signal, $service, $roam) = (split /,/, $indicators)[1,4,8];
$roam = $roam? "YES": "NO";
$signal ||= 0;
my $network = "NO";
my $capability = "NO";
if ($service) {
$network = call("AT+COPS?");
$network =~ s/.*"(.*)".*/\1/;
$network = "NO" unless length $network;
$capability = call("AT*ERINFO?");
$capability =~ s/\*ERINFO: //;
$capability = $erinfo{$capability};
$capability = "NO" unless length $capability;
}
my $connected = call("AT*ENAP?");
$connected =~ s/\*ENAP: //;
$connected = 0 if $connected eq "ERROR";
$connected = $connected? "YES": "NO";
print "Network: $network\n";
print "Signal: $signal\n";
print "Capability: $capability\n";
print "Roaming: $roam\n";
print "Connected: $connected\n";
}
#
# Enabling / Disabling devices
#
sub enableCard {
print "Enabling F3507g card ... ";
# Check if card is already enabled
if (call("AT+CFUN?") ne "+CFUN: 4") {
print "already enabled\n";
return;
}
# Check if PIN is required
if (call("AT+CPIN?") =~ /SIM PIN/) {
if (call("AT+CPIN=\"$pin\"") ne "OK") {
print "failed\n";
perror("Setting PIN");
}
}
# Enabling card
if (call("AT+CFUN=1") ne "OK") {
print "failed\n";
perror("Setting CFUN=1");
}
# Waiting for the card to be enabled
#if (!waitforstr("+PACSP", 10)) {
# print "failed\n";
# perror("Didn't receive '+PACSP'");
#}
print "ok\n";
}
sub disableCard {
print "Disabling F3507g card ... ";
# Check if card is already disabled
if (call("AT+CFUN?") eq "+CFUN: 4") {
print "already disabled\n";
return;
}
if (call("AT+CFUN=4") ne "OK") {
print "failed\n";
perror("Setting CFUN=4");
}
print "ok\n";
}
#
# Some base helper functions
#
sub init {
$port = Device::SerialPort->new(getDev($devNameControl));
if (defined $port) {
$port->read_char_time(0); # don't wait for each character
$port->read_const_time(1000); # 1 second per unfulfilled "read" call
} else {
print STDERR "Failed to open device\n";
exit 1;
}
}
my $last_cmd = "";
my $last_ret = "";
sub call {
my $cmd = shift;
my $ret = "";
$last_cmd = $cmd;
$port->write("$cmd\r\n");
my $tries = 10;
while ($tries > 0) {
my ($count, $buf) = $port->read(255);
if ($count > 0) {
my @lines = split("\r\n", $buf);
$ret = $lines[1]; # we support only 1 line aswers.
last;
} else {
$tries--;
}
}
$last_ret = $ret;
return $ret;
}
sub waitforstr {
my $str = shift;
my $tries = shift;
my $found = 0;
while ($tries > 0) {
my ($count, $buf) = $port->read(255);
if ($count > 0 && $buf =~ /$str/) {
$found = 1;
last;
}
$tries--;
}
return $found;
}
sub getDev {
my $devName = shift;
my $ret = "";
foreach my $dev (</sys/class/tty/ttyACM*/device/interface>) {
open F, "<$dev";
my $name = <F>;
close F;
chomp $name;
if ($name eq $devName) {
$dev =~ s|.*/(ttyACM\d+)/.*|\1|;
$ret = "/dev/$dev";
last;
}
}
return $ret;
}
sub perror {
print STDERR "Error: ".shift()." (AT_CMD='$last_cmd' RET='$last_ret')\n";
exit 1;
}