]>
git.ipfire.org Git - people/teissler/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 $self->_initialize($port, $baud);
38 if ($self->_is_working()) {
48 # Close connection to modem.
50 $self->{modem
}->close();
55 my ($self, $port, $baud) = @_;
57 # Establish connection to the modem.
58 $self->{modem
} = new Device
::Modem
(port
=> $port);
59 $self->{modem
}->connect(baudrate
=> $baud);
65 # Check if the modem responds to AT commands.
66 $self->{modem
}->atsend("AT\r\n");
68 my $response = $self->{modem
}->answer();
69 return ($response eq "OK");
76 # Terminate the AT command with newline.
79 $self->{modem
}->atsend($cmd);
81 my $response = $self->{modem
}->answer();
82 my @response = split(/\n/, $response);
84 # Trim leading and trailing spaces.
85 foreach my $line (@response) {
86 $line =~ s/^\s+|\s+$//g;
90 my $last_element = pop(@response);
91 unless ($last_element eq "OK") {
92 push(@response, $last_element);
95 $response = join("\n", @response);
97 return $self->_trim($response);
104 my $first_char = substr($input, 0, 1);
105 if ($first_char eq "+") {
106 my @output = split(/:/, $input);
118 return $self->_command("AT+GMI");
124 return $self->_command("AT+GMM");
127 sub get_software_version
() {
130 return $self->_command("AT+GMR");
136 return $self->_command("AT+GSN");
139 sub get_capabilities
() {
142 my $output = $self->_command("AT+GCAP");
143 return split(/,/, $output);
146 sub is_sim_unlocked
() {
156 if ($self->is_sim_unlocked()) {
157 return $self->_command("AT+CIMI");
161 sub get_network_registration
() {
165 foreach my $i ([0, 1]) {
166 my $output = $self->_command("AT+CREG?");
168 @elements = split(/,/, $output);
169 if ($#elements != 2) {
170 # Output in wrong format. Resetting.
171 $self->_command("AT+CREG=0");
175 if ($elements[0] == 0) {
176 if ($elements[1] == 0) {
177 return "NOT REGISTERED, NOT SEARCHING";
178 } elsif ($elements[1] == 1) {
179 return "REGISTERED TO HOME NETWORK";
180 } elsif ($elements[1] == 2) {
181 return "NOT REGISTERED, SEARCHING";
182 } elsif ($elements[1] == 3) {
183 return "REGISTRATION DENIED";
184 } elsif ($elements[1] == 5) {
185 return "REGISTERED, ROAMING";
192 sub _get_network_operator
() {
195 my $output = $self->_command("AT+COPS?");
198 my @elements = split(/,/, $output);
199 if ($#elements == 3) {
204 sub get_network_operator
() {
207 my ($mode, $format, $operator, $act) = $self->_get_network_operator();
212 sub get_network_mode
() {
215 my ($mode, $format, $operator, $act) = $self->_get_network_operator();
219 } elsif ($act == 1) {
220 return "Compact GSM";
221 } elsif ($act == 2) {
223 } elsif ($act == 3) {
224 return "GSM WITH EGPRS";
225 } elsif ($act == 4) {
226 return "UMTS WITH HSDPA";
227 } elsif ($act == 5) {
228 return "UMTS WITH HSUPA";
229 } elsif ($act == 6) {
230 return "UMTS WITH HSDPA+HSUPA";
231 } elsif ($act == 7) {
234 return "UNKNOWN ($act)";
238 sub _get_signal_quality
() {
241 my $output = $self->_command("AT+CSQ");
243 my @elements = split(/,/, $output);
244 if ($#elements == 1) {
249 sub get_signal_quality
() {
252 my ($rssi, $ber) = $self->_get_signal_quality();
255 unless ($rssi == 99) {
256 my $dbm = ($rssi * 2) - 113;
263 sub get_bit_error_rate
() {
266 my ($rssi, $ber) = $self->_get_signal_quality();
268 # 99 indicates unknown.
269 unless ($ber == 99) {