2 # travelmate, a wlan connection manager for travel router
3 # written by Dirk Brenken (dev@brenken.org)
5 # This is free software, licensed under the GNU General Public License v3.
6 # You should have received a copy of the GNU General Public License
7 # along with this program. If not, see <http://www.gnu.org/licenses/>.
12 PATH
="/usr/sbin:/usr/bin:/sbin:/bin"
21 # function to prepare all relevant AP and STA interfaces
26 local mode
="$(uci -q get wireless."${config}".mode)"
27 local device
="$(uci -q get wireless."${config}".device)"
28 local network
="$(uci -q get wireless."${config}".network)"
29 local ifname
="$(uci -q get wireless."${config}".ifname)"
30 local disabled
="$(uci -q get wireless."${config}".disabled)"
32 if [ "${mode}" = "ap" ] &&
33 ([ -z "${trm_device}" ] || [ "${trm_device}" = "${device}" ])
35 trm_aplist
="${trm_aplist} ${ifname}"
36 if [ -z "${disabled}" ] ||
[ "${disabled}" = "1" ]
38 trm_set
"none" "${config}" "${network}" "up"
40 elif [ "${mode}" = "sta" ]
42 trm_stalist
="${trm_stalist} ${config}_${network}"
43 if [ -z "${disabled}" ] ||
[ "${disabled}" = "0" ]
45 trm_set
"none" "${config}" "${network}" "down"
50 # function to set different wlan interface status
56 local interface
="${3}"
59 if [ "${command}" = "up" ]
61 uci
-q set wireless.
"${config}".disabled
=0
62 ubus call network.interface.
"${interface}" "${command}"
63 trm_checklist
="${trm_checklist} ${interface}"
64 elif [ "${command}" = "down" ]
66 uci
-q set wireless.
"${config}".disabled
=1
67 ubus call network.interface.
"${interface}" "${command}"
70 trm_log
"debug" "set ::: change: ${change}, config: ${config}, interface: ${interface}, command: ${command}, checklist: ${trm_checklist}, uci-changes: $(uci -q changes wireless)"
71 if [ -n "$(uci -q changes wireless)" ]
73 if [ "${change}" = "commit" ]
75 uci
-q commit wireless
76 ubus call network reload
78 elif [ "${change}" = "partial" ]
80 ubus call network reload
82 elif [ "${change}" = "defer" ]
84 uci
-q revert wireless
85 elif [ "${change}" = "revert" ]
87 uci
-q revert wireless
88 ubus call network reload
94 # function to check interface status on "up" event
101 for interface
in ${trm_checklist}
103 while [ $
((cnt
)) -lt 15 ]
105 json_load
"$(ubus -S call network.interface."${interface}" status)"
106 json_get_var trm_status up
107 if [ "${trm_status}" = "1" ] ||
[ -n "${trm_uplink}" ]
109 trm_log
"debug" "check::: interface: ${interface}, status: ${trm_status}, uplink-cfg: ${trm_uplink}, uplink-ssid: ${trm_ssid}, loop-cnt: ${cnt}, error-cnt: $((trm_count_${trm_config}))"
117 if [ -n "${trm_uplink}" ] && [ "${trm_status}" = "0" ]
119 ubus call network reload
120 eval "trm_count_${trm_uplink}=\$((trm_count_${trm_uplink}+1))"
123 trm_log
"info" "uplink ${trm_ssid} get lost"
124 elif [ -z "${trm_uplink}" ] && [ -n "${trm_checklist}" ]
130 # function to write to syslog
137 if [ -n "${log_msg}" ] && ([ "${class}" != "debug" ] || ([ "${class}" = "debug" ] && [ $
((trm_debug
)) -eq 1 ]))
139 logger
-t "travelmate-${trm_ver}[${trm_pid}] ${class}" "${log_msg}" 2>&1
143 # source required system libraries
145 if [ -r "/lib/functions.sh" ] && [ -r "/usr/share/libubox/jshn.sh" ]
147 .
"/lib/functions.sh"
148 .
"/usr/share/libubox/jshn.sh"
151 trm_log
"error" "required system libraries not found"
155 # load uci config and check 'enabled' option
161 eval "${option}=\"${value}\""
164 config_load travelmate
166 if [ "${trm_enabled}" != "1" ]
168 trm_log
"info" "travelmate is currently disabled, please set 'trm_enabled' to '1' to use this service"
172 # check for preferred wireless tool
174 if [ $
((trm_iw
)) -eq 1 ]
176 trm_scanner
="$(which iw)"
178 trm_scanner
="$(which iwinfo)"
181 if [ -z "${trm_scanner}" ]
183 trm_log
"error" "no wireless tool for wlan scanning found, please install 'iw' or 'iwinfo'"
187 # infinitive loop to establish and track STA uplink connections
191 if [ -z "${trm_uplink}" ] ||
[ "${trm_status}" = "0" ]
196 config_foreach trm_prepare wifi-iface
198 for ap
in ${trm_aplist}
200 ubus
-t 10 wait_for hostapd.
"${ap}"
201 if [ $
((trm_iw
)) -eq 1 ]
203 trm_ssidlist
="$(${trm_scanner} dev "${ap}" scan 2>/dev/null | awk '/SSID: /{if(!seen[$0]++){printf "\"";for(i=2; i<=NF; i++)if(i==2)printf $i;else printf " "$i;printf "\" "}}')"
205 trm_ssidlist
="$(${trm_scanner} "${ap}" scan | awk '/ESSID: ".*"/{ORS=" ";if (!seen[$0]++) for(i=2; i<=NF; i++) print $i}')"
207 trm_log
"debug" "main ::: scan-tool: ${trm_scanner}, ssidlist: ${trm_ssidlist}"
208 if [ -n "${trm_ssidlist}" ]
210 for sta
in ${trm_stalist}
212 trm_config
="${sta%%_*}"
213 trm_network
="${sta##*_}"
214 trm_ifname
="$(uci -q get wireless."${trm_config}".ifname)"
215 trm_ssid
="\"$(uci -q get wireless."${trm_config}".ssid)\""
216 if [ $
((trm_count_
${trm_config})) -lt $
((trm_maxretry
)) ] ||
[ $
((trm_maxretry
)) -eq 0 ]
218 if [ -n "$(printf "${trm_ssidlist}" | grep -Fo "${trm_ssid}")" ]
220 trm_set
"partial" "${trm_config}" "${trm_network}" "up"
221 if [ "${trm_status}" = "1" ]
223 trm_checklist
="${trm_network}"
224 trm_uplink
="${trm_config}"
226 trm_log
"info" "wwan interface \"${trm_ifname}\" connected to uplink ${trm_ssid}"
230 eval "trm_count_${trm_config}=\$((trm_count_${trm_config}+1))"
233 elif [ $
((trm_count_
${trm_config})) -eq $
((trm_maxretry
)) ] && [ $
((trm_maxretry
)) -ne 0 ]
235 eval "trm_count_${trm_config}=\$((trm_count_${trm_config}+1))"
236 trm_log
"info" "uplink ${trm_ssid} disabled due to permanent connection failures"
244 if [ -n "${trm_uplink}" ]