8f0068780e9a9ed13079dd9b8a30d54de27acb30
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"
14 trm_sysver
="$(ubus -S call system board | jsonfilter -e '@.release.description')"
21 trm_iwinfo
="$(command -v iwinfo)"
23 trm_rtfile
="/tmp/trm_runtime.json"
25 # f_envload: load travelmate environment
35 # load uci config and check 'enabled' option
41 eval "${option}=\"${value}\""
43 config_load travelmate
45 if [ ${trm_enabled} -ne 1 ]
47 f_log
"info " "travelmate is currently disabled, please set 'trm_enabled' to '1' to use this service"
52 # f_prepare: gather radio information & bring down all STA interfaces
57 local mode
="$(uci -q get wireless."${config}".mode)"
58 local radio
="$(uci -q get wireless."${config}".device)"
59 local disabled
="$(uci -q get wireless."${config}".disabled)"
61 if ([ -z "${trm_radio}" ] || [ "${trm_radio}" = "${radio}" ]) && \
62 [ -z "$(printf "%s
" "${trm_radiolist}" | grep -Fo " ${radio}")" ]
64 trm_radiolist
="${trm_radiolist} ${radio}"
66 if [ "${mode}" = "sta" ]
68 trm_stalist
="${trm_stalist} ${config}_${radio}"
69 if [ -z "${disabled}" ] ||
[ "${disabled}" = "0" ]
71 uci
-q set wireless.
"${config}".disabled
=1
74 f_log
"debug" "prepare: ${mode}, radio: ${radio}, config: ${config}, disabled: ${disabled}"
77 # f_check: check interface status
81 local ifname radio status cnt
=1 mode
="${1}"
84 ubus call network reload
85 while [ ${cnt} -le ${trm_maxwait} ]
87 status
="$(ubus -S call network.wireless status 2>/dev/null)"
90 if [ "${mode}" = "dev" ]
92 for radio
in ${trm_radiolist}
94 trm_ifstatus
="$(printf "%s
" "${status}" | jsonfilter -l1 -e "@.
${radio}.up
")"
95 if [ "${trm_ifstatus}" = "true" ]
97 trm_devlist
="${trm_devlist} ${radio}"
103 ifname
="${trm_devlist}"
105 ifname
="$(printf "%s
" "${status}" | jsonfilter -l1 -e '@.*.interfaces[@.config.mode="sta
"].ifname')"
106 if [ -n "${ifname}" ]
108 trm_ifstatus
="$(ubus -S call network.interface dump 2>/dev/null | jsonfilter -l1 -e "@.interface
[@.device
=\"${ifname}\"].up
")"
111 if [ "${mode}" = "initial" ] ||
[ "${trm_ifstatus}" = "true" ]
119 f_log
"debug" "check: ${mode}, name: ${ifname}, status: ${trm_ifstatus}, count: ${cnt}, max-wait: ${trm_maxwait}, automatic: ${trm_automatic}"
122 # f_jsnupdate: update runtime information
126 local iface
="${1}" radio="${2}" ssid="${3}"
129 json_add_object
"data"
130 json_add_string
"travelmate_version" "${trm_ver}"
131 json_add_string
"station_connection" "${trm_ifstatus}"
132 json_add_string
"station_ssid" "${ssid}"
133 json_add_string
"station_interface" "${iface}"
134 json_add_string
"station_radio" "${radio}"
135 json_add_string
"last_rundate" "$(/bin/date "+%d.
%m.
%Y
%H
:%M
:%S
")"
136 json_add_string
"system" "${trm_sysver}"
138 json_dump
> "${trm_rtfile}"
141 # f_status: output runtime information
145 local key keylist value
147 if [ -s "${trm_rtfile}" ]
149 printf "%s\n" "::: travelmate runtime information"
150 json_load
"$(cat "${trm_rtfile}" 2>/dev/null)"
152 json_get_keys keylist
153 for key
in ${keylist}
155 json_get_var value
"${key}"
156 printf " %-18s : %s\n" "${key}" "${value}"
161 # f_log: write to syslog, exit on error
168 if [ -n "${log_msg}" ] && ([ "${class}" != "debug" ] || [ ${trm_debug} -eq 1 ])
170 logger -t "travelmate-
[${trm_ver}] ${class}" "${log_msg}"
171 if [ "${class}" = "error
" ]
173 logger -t "travelmate-
[${trm_ver}] ${class}" "Please check 'https://github.com/openwrt/packages/blob/master/net/travelmate/files/README.md' (${trm_sysver})"
179 # f_main: main function for connection handling
183 local dev config ssid_list sta_ssid sta_radio sta_iface cnt=1
186 if [ "${trm_ifstatus}" != "true
" ]
190 config_foreach f_prepare wifi-iface
191 if [ -n "$
(uci
-q changes wireless
)" ]
193 uci -q commit wireless
196 f_log "debug
" "main
: ${trm_devlist}, sta-list
: ${trm_stalist}"
197 for dev in ${trm_devlist}
200 if [ -z "$
(printf "%s" "${trm_stalist}" |
grep -Fo "_${dev}")" ]
204 while [ ${cnt} -le ${trm_maxretry} ]
206 ssid_list="$
(${trm_iwinfo} "${dev}" scan | awk '/ESSID: "/{ORS=" ";if (!seen[$0]++) for(i=2; i<=NF; i++) print $i}')"
207 f_log "debug" "main: ${trm_iwinfo}, dev: ${dev}, ssids: ${ssid_list}"
208 if [ -n "${ssid_list}" ]
210 for sta in ${trm_stalist}
213 sta_radio="${sta##*_}"
214 sta_ssid="$(uci -q get wireless."${config}".ssid)"
215 sta_iface="$(uci -q get wireless."${config}".network)"
216 if [ -n "$(printf "%s" "${ssid_list}" | grep -Fo "\"${sta_ssid}\"")" ] && [ "${dev}" = "${sta_radio}" ]
218 uci -q set wireless."${config}".disabled=0
220 if [ "${trm_ifstatus}" = "true" ]
222 uci -q commit wireless
223 f_log "info " "interface '${sta_iface}' on '${sta_radio}' connected to uplink '${sta_ssid}' (${trm_sysver})"
224 f_jsnupdate "${sta_iface}" "${sta_radio}" "${sta_ssid}"
226 elif [ ${cnt} -eq ${trm_maxretry} ]
228 uci -q set wireless."${config}".disabled=1
229 uci -q set wireless."${config}".ssid="${sta_ssid}_err
"
230 uci -q commit wireless
232 f_log "info
" "interface
'can't connect to uplink
'${sta_ssid}' (${cnt}/${trm_maxretry}), uplink disabled (${trm_sysver})"
234 uci -q revert wireless
236 f_log "info " "can't connect to uplink
'${sta_ssid}' (${cnt}/${trm_maxretry}) (${trm_sysver})"
238 f_jsnupdate "${sta_iface}" "${sta_radio}" "${sta_ssid}"
246 if [ ! -s "${trm_rtfile}" ]
249 f_jsnupdate "n/a" "n/a" "n/a"
252 if [ ! -s "${trm_rtfile}" ]
254 config="$(ubus -S call network.wireless status | jsonfilter -l1 -e '@.
*.interfaces
[@.config.mode
="sta"].section
')"
255 sta_radio="$(uci -q get wireless."${config}".device)"
256 sta_ssid="$(uci -q get wireless."${config}".ssid)"
257 sta_iface="$(uci -q get wireless."${config}".network)"
258 f_jsnupdate "${sta_iface}" "${sta_radio}" "${sta_ssid}"
263 # source required system libraries
265 if [ -r "/lib/functions.sh" ] && [ -r "/usr/share/libubox/jshn.sh" ]
267 . "/lib/functions.sh"
268 . "/usr/share/libubox/jshn.sh"
270 f_log "error" "system libraries not found"
273 # handle different travelmate actions
282 while [ ${trm_automatic} -eq 1 ]