]>
git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - config/cfgroot/modem-lib.pl
2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2014 IPFire Team <info@ipfire.org> #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
20 ###############################################################################
35 # Initialize the connetion to the modem.
36 my $ret = $self->_initialize($port, $baud);
41 if ($self->_is_working()) {
51 # Close connection to modem.
53 $self->{modem
}->close();
58 my ($self, $port, $baud) = @_;
60 # Check if the character device actually exists.
65 # Establish connection to the modem.
66 $self->{modem
} = new Device
::Modem
(port
=> $port);
67 $self->{modem
}->connect(baudrate
=> $baud);
75 # Check if the modem responds to AT commands.
76 $self->{modem
}->atsend("AT\r\n");
78 my $response = $self->{modem
}->answer();
79 return ($response eq "OK");
86 # Terminate the AT command with newline.
89 $self->{modem
}->atsend($cmd);
91 my $response = $self->{modem
}->answer();
92 my @response = split(/\n/, $response);
94 # Trim leading and trailing spaces.
95 foreach my $line (@response) {
96 $line =~ s/^\s+|\s+$//g;
100 my $last_element = pop(@response);
101 unless ($last_element eq "OK") {
102 push(@response, $last_element);
105 $response = join("\n", @response);
107 return $self->_trim($response);
114 my $first_char = substr($input, 0, 1);
115 if ($first_char eq "+") {
116 my @output = split(/:/, $input);
128 return $self->_command("AT+GMI");
134 return $self->_command("AT+GMM");
137 sub get_software_version
() {
140 return $self->_command("AT+GMR");
146 return $self->_command("AT+GSN");
149 sub get_capabilities
() {
152 my $output = $self->_command("AT+GCAP");
153 return split(/,/, $output);
156 sub is_sim_unlocked
() {
166 if ($self->is_sim_unlocked()) {
167 return $self->_command("AT+CIMI");
171 sub get_network_registration
() {
175 foreach my $i ([0, 1]) {
176 my $output = $self->_command("AT+CREG?");
178 @elements = split(/,/, $output);
179 if ($#elements != 2) {
180 # Output in wrong format. Resetting.
181 $self->_command("AT+CREG=0");
185 if ($elements[0] == 0) {
186 if ($elements[1] == 0) {
187 return "NOT REGISTERED, NOT SEARCHING";
188 } elsif ($elements[1] == 1) {
189 return "REGISTERED TO HOME NETWORK";
190 } elsif ($elements[1] == 2) {
191 return "NOT REGISTERED, SEARCHING";
192 } elsif ($elements[1] == 3) {
193 return "REGISTRATION DENIED";
194 } elsif ($elements[1] == 5) {
195 return "REGISTERED, ROAMING";
202 sub _get_network_operator
() {
205 my $output = $self->_command("AT+COPS?");
208 my @elements = split(/,/, $output);
209 if ($#elements == 3) {
214 sub get_network_operator
() {
217 my ($mode, $format, $operator, $act) = $self->_get_network_operator();
222 sub get_network_mode
() {
225 my ($mode, $format, $operator, $act) = $self->_get_network_operator();
229 } elsif ($act == 1) {
230 return "Compact GSM";
231 } elsif ($act == 2) {
233 } elsif ($act == 3) {
234 return "GSM WITH EGPRS";
235 } elsif ($act == 4) {
236 return "UMTS WITH HSDPA";
237 } elsif ($act == 5) {
238 return "UMTS WITH HSUPA";
239 } elsif ($act == 6) {
240 return "UMTS WITH HSDPA+HSUPA";
241 } elsif ($act == 7) {
244 return "UNKNOWN ($act)";
248 sub _get_signal_quality
() {
251 my $output = $self->_command("AT+CSQ");
253 my @elements = split(/,/, $output);
254 if ($#elements == 1) {
259 sub get_signal_quality
() {
262 my ($rssi, $ber) = $self->_get_signal_quality();
265 unless ($rssi == 99) {
266 my $dbm = ($rssi * 2) - 113;
273 sub get_bit_error_rate
() {
276 my ($rssi, $ber) = $self->_get_signal_quality();
278 # 99 indicates unknown.
279 unless ($ber == 99) {