2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2012 IPFire Network Development Team #
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 ###############################################################################
22 # Exit codes from the chat(8) command:
28 function modem_chat
() {
34 while [ $# -gt 0 ]; do
37 timeout
=$
(cli_get_val
${1})
40 answer
=$
(cli_get_val
${1})
53 assert serial_exists
${device}
54 assert serial_is_unlocked
${device}
60 log DEBUG
"Sending command to ${device}: ${command}"
63 # This cannot be run with -x.
66 chat
-V -s -t ${timeout} "" "${command}" "${answer}" \
67 < ${device} > ${device} || exit $?
68 print # Print line feed.
69 ) 2>&1 | __modem_chat_process_output "${answer}" ${quiet}
71 local ret=${PIPESTATUS[0]}
73 # Return the exit code of the chat command.
79 # When the timeout condition hit, the expected string has not
80 # been received in the given amount of time.
81 ${CHAT_TIMEOUT}|${CHAT_ERROR})
86 return ${EXIT_CONF_ERROR}
90 log WARNING "Received unknown
exit code from chat
(8): ${ret}"
94 function __modem_chat_process_output() {
98 if enabled quiet; then
99 # Just throw everything away.
108 log DEBUG "Output
[${counter}]: ${line}"
110 # Also skip empty lines.
111 [ -n "${line}" ] || continue
113 # Ignore all volatile messages.
114 [ "${line:0:1}" = "^
" ] && continue
116 counter=$(( ${counter} + 1 ))
118 # Skip the first line, because that's out command.
119 [ ${counter} -eq 1 ] && continue
121 # Filter out the expected answer.
122 [ "${line}" = "${answer}" ] && continue
129 # Exit codes of the sim_status function.
135 function modem_sim_status() {
140 output=$(modem_chat ${device} "AT
+CPIN?
")
141 assert_check_retval $?
143 # Strip leading +CPIN: from the output.
148 log DEBUG "${device}'s SIM is unlocked or doesn't need a PIN.
"
149 return ${EXIT_SIM_READY}
152 log DEBUG "${device}'s SIM is waiting for a PIN."
153 return ${EXIT_SIM_PIN}
156 log DEBUG "${device}'s SIM is PUK locked.
"
157 return ${EXIT_SIM_PUK}
161 log WARNING "${device}: Invalid output of the AT
+CPIN?
command.
"
162 return ${EXIT_SIM_UNKNOWN}
165 function modem_sim_unlocked() {
169 modem_sim_status "${device}"
172 [ ${ret} -eq ${EXIT_SIM_READY} ] && return ${EXIT_TRUE} || return ${EXIT_FALSE}
175 function modem_sim_locked() {
176 modem_sim_unlocked $@ && return ${EXIT_FALSE} || return ${EXIT_TRUE}
179 function modem_sim_unlock() {
186 local command="AT
+CPIN
=${pin}"
189 if isset new_pin; then
190 command="${command},${new_pin}"
193 modem_chat --timeout=2 --quiet "${device}" "${command}"
198 log INFO "Successfully unlocked SIM card on
${device}.
"
201 log ERROR "Could not unlock SIM card on
${device}.
"
209 # Returns the vendor of the modem.
210 # For example: "huawei
"
211 function modem_get_manufacturer() {
216 output=$(modem_chat ${device} "AT
+GMI
")
217 assert_check_retval $?
219 [ "${output:0:1}" = "+" ] && output=${output#*: }
220 output=${output//\"/}
225 function modem_get_model() {
230 output=$(modem_chat ${device} "AT
+GMM
")
231 assert_check_retval $?
233 [ "${output:0:1}" = "+" ] && output=${output#*: }
234 output=${output//\"/}
239 function modem_get_software_version() {
244 output=$(modem_chat ${device} "AT
+GMR
")
245 assert_check_retval $?
247 [ "${output:0:1}" = "+" ] && output=${output#*: }
248 output=${output//\"/}
253 function modem_get_sim_imsi() {
257 # Make sure the SIM card is unlocked for this operation.
258 assert modem_sim_unlocked ${device}
260 modem_chat ${device} "AT
+CIMI
"
263 function modem_get_device_imei() {
268 output=$(modem_chat --timeout=1 ${device} "AT
+CGSN
") || assert_check_retval $?
271 if [ ${ret} -eq ${EXIT_OK} ]; then
278 function modem_is_mobile() {
282 # Check if the device can return it's IMEI.
283 # If not, it's probably a serial 56k modem or something
286 modem_get_device_imei ${device} &>/dev/null
289 # Exit codes of the network registration function.
290 EXIT_REG_REGISTERED_TO_HOME_NETWORK=0
291 EXIT_REG_NOT_REGISTERED_NOT_SEARCHING=1
292 EXIT_REG_NOT_REGISTERED_SEARCHING=2
293 EXIT_REG_REGISTRATION_DENIED=3
294 EXIT_REG_REGISTERED_ROAMING=4
297 function modem_get_network_registration() {
301 # Make sure the SIM card is unlocked for this operation.
302 assert modem_sim_unlocked ${device}
305 output=$(modem_chat ${device} "AT
+CREG?
")
306 assert_check_retval $?
308 # Cut out unneeded parts of the message.
313 local status=${output%,*}
315 # The status variable must be zero.
316 [ ${status} -ne 0 ] && break
318 local stat=${output#*,}
321 return ${EXIT_REG_NOT_REGISTERED_NOT_SEARCHING}
324 return ${EXIT_REG_REGISTERED_TO_HOME_NETWORK}
327 return ${EXIT_REG_NOT_REGISTERED_SEARCHING}
330 return ${EXIT_REG_REGISTRATION_DENIED}
333 return ${EXIT_REG_REGISTERED_ROAMING}
336 return ${EXIT_REG_UNKNOWN}
342 # Apparently the output of the CREG? command was not in
343 # the right format. The modem will be tried to be set to the
346 modem_set_network_registration ${device} 0
347 modem_get_network_registration ${device}
350 function modem_set_network_registration() {
354 # Make sure the SIM card is unlocked for this operation.
355 assert modem_sim_unlocked ${device}
360 modem_chat ${device} "AT
+CREG
=${mode}"
363 function modem_scan_networks() {
367 # Make sure the SIM card is unlocked for this operation.
368 assert modem_sim_unlocked ${device}
371 output=$(modem_chat --timeout=60 ${device} "AT
+COPS
=?
")
372 assert_check_retval $?
376 # XXX the output is not very nice to parse.
379 function __modem_get_network_operator() {
383 # Make sure the SIM card is unlocked for this operation.
384 assert modem_sim_unlocked ${device}
387 assert isset argument
390 output=$(modem_chat ${device} "AT
+COPS?
")
391 assert_check_retval $?
395 local mode format operator act
396 read mode format operator act <<< "${output//,/ }"
398 # Remove all " from the operator string.
399 operator
=${operator//\"/}
405 function modem_get_network_operator
() {
409 __modem_get_network_operator
${device} operator
412 # Exit codes of the network operator mode function.
414 EXIT_OPMODE_COMPACTGSM
=1
415 EXIT_OPMODE_GSM_WITH_EGPRS
=2
417 EXIT_OPMODE_UMTS_WITH_HSDPA
=4
418 EXIT_OPMODE_UMTS_WITH_HSUPA
=5
419 EXIT_OPMODE_UMTS_WITH_HSDPA_AND_HSUPA
=6
420 EXIT_OPMODE_UNKNOWN
=7
422 function modem_get_network_mode
() {
427 output
=$
(__modem_get_network_operator
${device} act
)
428 assert_check_retval $?
433 return ${EXIT_OPMODE_GSM}
437 return ${EXIT_OPMODE_COMPACTGSM}
441 return ${EXIT_OPMODE_UMTS}
444 print
"UMTS with HSDPA"
445 return ${EXIT_OPMODE_UMTS_WITH_HSDPA}
448 print
"UMTS with HSUPA"
449 return ${EXIT_OPMODE_UMTS_WITH_HSUPA}
452 print
"UMTS with HSDPA and HSUPA"
453 return ${EXIT_OPMODE_UMTS_WITH_HSDPA_AND_HSUPA}
457 return ${EXIT_OPMODE_UNKNOWN}
462 function __modem_get_signal_quality
() {
466 # Make sure the SIM card is unlocked for this operation.
467 assert modem_sim_unlocked
${device}
470 assert isset argument
473 output
=$
(modem_chat
${device} "AT+CSQ")
474 assert_check_retval $?
480 local rssi
=${output%,*}
481 local ber
=${output#*,}
487 log ERROR
"Unknown format for AT+CSQ: ${device}: ${output}"
494 function modem_get_signal_quality
() {
499 rssi
=$
(__modem_get_signal_quality
${device} rssi
)
500 assert_check_retval $?
502 isset rssi ||
return ${EXIT_ERROR}
504 # 99 indicates an unknown signal strength.
505 [ ${rssi} -eq 99 ] && return ${EXIT_UNKNOWN}
507 local dbm
=$
(( ${rssi} * 2 ))
508 dbm
=$
(( ${dbm} - 113 ))
514 function modem_get_bit_error_rate
() {
519 ber
=$
(__modem_get_signal_quality
${device} ber
)
520 assert_check_retval $?
522 isset ber ||
return ${EXIT_ERROR}
524 # 99 indicates that the bit error rate could not be detected or
525 # is unknown for some other reason.
526 [ ${ber} -eq 99 ] && return ${EXIT_UNKNOWN}