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 disabled="$(uci -q get wireless."${config}".disabled)"
31 if [ "${mode}" = "ap" ] && [ -n "${network}" ] &&
32 ([ -z "${trm_device}" ] || [ "${trm_device}" = "${device}" ])
35 if [ -z "${disabled}" ] || [ "${disabled}" = "1" ]
37 f_set "none" "${config}" "${network}" "up"
39 elif [ "${mode}" = "sta" ] && [ -n "${network}" ]
41 trm_stalist="${trm_stalist} ${config}_${network}"
42 if [ -z "${disabled}" ] || [ "${disabled}" = "0" ]
44 f_set "none" "${config}" "${network}" "down"
49 # function to set different wlan interface status
55 local interface="${3}"
58 if [ "${command}" = "up" ]
60 uci -q set wireless."${config}".disabled=0
61 ubus call network.interface."${interface}" "${command}"
62 trm_checklist="${trm_checklist} ${interface}"
63 elif [ "${command}" = "down" ]
65 uci -q set wireless."${config}".disabled=1
66 ubus call network.interface."${interface}" "${command}"
69 f_log "debug" "set ::: change: ${change}, config: ${config}, interface: ${interface}, command: ${command}, checklist: ${trm_checklist}, uci-changes: $(uci -q changes wireless)"
70 if [ -n "$(uci -q changes wireless)" ]
72 if [ "${change}" = "commit" ]
74 uci -q commit wireless
75 ubus call network reload
77 elif [ "${change}" = "partial" ]
79 ubus call network reload
81 elif [ "${change}" = "defer" ]
83 uci -q revert wireless
84 elif [ "${change}" = "revert" ]
86 uci -q revert wireless
87 ubus call network reload
93 # function to get ap ifnames by ubus call
100 json_load "$(ubus -S call network.wireless status)"
101 json_select "${device}"
102 json_get_keys if_list interfaces
103 json_select interfaces
104 for iface in ${if_list}
106 json_select "${iface}"
107 json_get_var name ifname
109 json_get_var cfg mode
110 if [ -n "${name}" ] && [ "${cfg}" = "ap" ]
112 trm_aplist="${trm_aplist} ${name}"
117 # function to check interface status on "up" event
121 local interface value
124 for interface in ${trm_checklist}
126 while [ $((cnt)) -lt 15 ]
128 json_load "$(ubus -S call network.interface."${interface}" status)"
129 json_get_var trm_status up
130 if [ "${trm_status}" = "1" ] || [ -n "${trm_uplink}" ]
132 f_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}))"
140 if [ -n "${trm_uplink}" ] && [ "${trm_status}" = "0" ]
142 ubus call network reload
143 eval "trm_count_${trm_uplink}=\$((trm_count_${trm_uplink}+1))"
146 f_log "info" "uplink ${trm_ssid} get lost"
147 elif [ -z "${trm_uplink}" ] && [ -n "${trm_checklist}" ]
153 # function to write to syslog
160 if [ -n "${log_msg}" ] && ([ "${class}" != "debug" ] || ([ "${class}" = "debug" ] && [ $((trm_debug)) -eq 1 ]))
162 logger -t "travelmate-${trm_ver}[${trm_pid}] ${class}" "${log_msg}" 2>&1
166 # source required system libraries
168 if [ -r "/lib/functions.sh" ] && [ -r "/usr/share/libubox/jshn.sh" ]
170 . "/lib/functions.sh"
171 . "/usr/share/libubox/jshn.sh"
174 f_log "error" "required system libraries not found"
178 # load uci config and check 'enabled' option
184 eval "${option}=\"${value}\""
187 config_load travelmate
189 if [ "${trm_enabled}" != "1" ]
191 f_log "info" "travelmate is currently disabled, please set 'trm_enabled' to '1' to use this service"
195 # check for preferred wireless tool
197 if [ $((trm_iw)) -eq 1 ]
199 trm_scanner="$(which iw)"
201 trm_scanner="$(which iwinfo)"
204 if [ -z "${trm_scanner}" ]
206 f_log "error" "no wireless tool for wlan scanning found, please install 'iw' or 'iwinfo'"
210 # infinitive loop to establish and track STA uplink connections
214 if [ -z "${trm_uplink}" ] || [ "${trm_status}" = "0" ]
219 config_foreach f_prepare wifi-iface
221 if [ -z "${trm_aplist}" ]
223 f_log "error" "no usable AP configuration found, please check '/etc/config/wireless'"
226 for ap in ${trm_aplist}
228 ubus -t 10 wait_for hostapd."${ap}"
229 if [ $((trm_iw)) -eq 1 ]
231 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 "\" "}}')"
233 trm_ssidlist="$(${trm_scanner} "${ap}" scan | awk '/ESSID: ".*"/{ORS=" ";if (!seen[$0]++) for(i=2; i<=NF; i++) print $i}')"
235 f_log "debug" "main ::: scan-tool: ${trm_scanner}, aplist: ${trm_aplist}, ssidlist: ${trm_ssidlist}"
236 if [ -n "${trm_ssidlist}" ]
238 if [ -z "${trm_stalist}" ]
240 f_log "error" "no usable STA configuration found, please check '/etc/config/wireless'"
243 for sta in ${trm_stalist}
245 trm_config="${sta%%_*}"
246 trm_network="${sta##*_}"
247 trm_ssid="\"$(uci -q get wireless."${trm_config}".ssid)\""
248 if [ $((trm_count_${trm_config})) -lt $((trm_maxretry)) ] || [ $((trm_maxretry)) -eq 0 ]
250 if [ -n "$(printf "${trm_ssidlist}" | grep -Fo "${trm_ssid}")" ]
252 f_set "partial" "${trm_config}" "${trm_network}" "up"
253 if [ "${trm_status}" = "1" ]
255 trm_checklist="${trm_network}"
256 trm_uplink="${trm_config}"
258 f_log "info" "wwan interface connected to uplink ${trm_ssid}"
262 eval "trm_count_${trm_config}=\$((trm_count_${trm_config}+1))"
265 elif [ $((trm_count_${trm_config})) -eq $((trm_maxretry)) ] && [ $((trm_maxretry)) -ne 0 ]
267 eval "trm_count_${trm_config}=\$((trm_count_${trm_config}+1))"
268 f_log "info" "uplink ${trm_ssid} disabled due to permanent connection failures"
276 if [ -n "${trm_uplink}" ]