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"
22 # source required system libraries
24 if [ -r "/lib/functions.sh" ]
28 f_log
"error" "status ::: required system library not found"
31 # load uci config and check 'enabled' option
37 eval "${option}=\"${value}\""
39 config_load travelmate
41 if [ ${trm_enabled} -ne 1 ]
43 f_log
"info " "status ::: travelmate is currently disabled, please set 'trm_enabled' to '1' to use this service"
47 # check for preferred wireless tool
49 if [ ${trm_iw} -eq 1 ]
51 trm_scanner
="$(which iw)"
53 trm_scanner
="$(which iwinfo)"
55 if [ -z "${trm_scanner}" ]
57 f_log
"error" "status ::: no wireless tool for wlan scanning found, please install 'iw' or 'iwinfo'"
61 # function to bring down all STA interfaces
66 local mode
="$(uci -q get wireless."${config}".mode)"
67 local network
="$(uci -q get wireless."${config}".network)"
68 local disabled
="$(uci -q get wireless."${config}".disabled)"
70 if [ "${mode}" = "sta" ] && [ -n "${network}" ]
72 trm_stalist
="${trm_stalist} ${config}_${network}"
73 if [ -z "${disabled}" ] ||
[ "${disabled}" = "0" ]
75 uci
-q set wireless.
"${config}".disabled
=1
76 f_log
"debug" "prepare ::: config: ${config}, interface: ${network}"
83 local ifname cnt
=1 mode
="${1}"
86 while [ ${cnt} -le ${trm_maxwait} ]
88 if [ "${mode}" = "ap" ]
90 trm_ifstatus
="$(ubus -S call network.wireless status | jsonfilter -l1 -e '@.*.up')"
92 ifname
="$(ubus -S call network.wireless status | jsonfilter -l1 -e '@.*.interfaces[@.config.mode="sta
"].ifname')"
95 trm_ifstatus
="$(ubus -S call network.interface dump | jsonfilter -e "@.interface
[@.device
=\"${ifname}\"].up
")"
98 if [ "${mode}" = "initial" ] ||
[ "${trm_ifstatus}" = "true" ]
105 f_log
"debug" "check ::: mode: ${mode}, name: ${ifname}, status: ${trm_ifstatus}, count: ${cnt}, max-wait: ${trm_maxwait}"
108 # function to write to syslog
115 if [ -n "${log_msg}" ] && ([ "${class}" != "debug" ] || [ ${trm_debug} -eq 1 ])
117 logger -t "travelmate-
[${trm_ver}] ${class}" "${log_msg}"
118 if [ "${class}" = "error
" ]
127 local ap_list ssid_list config network ssid cnt=1
128 local sysver="$
(ubus
-S call system board | jsonfilter
-e '@.release.description')"
131 if [ "${trm_ifstatus}" != "true
" ]
134 config_foreach f_prepare wifi-iface
135 if [ -n "$
(uci
-q changes wireless
)" ]
137 uci -q commit wireless
138 ubus call network reload
141 ap_list="$
(ubus
-S call network.wireless status | jsonfilter
-e '@.*.interfaces[@.config.mode="ap"].ifname')"
142 f_log "debug
" "main
::: ap-list
: ${ap_list}, sta-list
: ${trm_stalist}"
143 if [ -z "${ap_list}" ] || [ -z "${trm_stalist}" ]
145 f_log "error
" "status
::: no usable AP
/STA configuration found
"
149 while [ ${cnt} -le ${trm_maxretry} ]
151 if [ ${trm_iw} -eq 1 ]
153 ssid_list="$
(${trm_scanner} dev
"${ap}" scan
2>/dev
/null | \
154 awk '/SSID: /{if(!seen[$0]++){printf "\"";for(i=2; i<=NF; i++)if(i==2)printf $i;else printf " "$i;printf "\" "}}')"
156 ssid_list="$
(${trm_scanner} "${ap}" scan | \
157 awk '/ESSID: ".*"/{ORS=" ";if (!seen[$0]++) for(i=2; i<=NF; i++) print $i}')"
159 f_log "debug
" "main
::: scan-tool
: ${trm_scanner}, ssidlist
: ${ssid_list}"
160 if [ -n "${ssid_list}" ]
162 for sta in ${trm_stalist}
166 ssid="\"$
(uci
-q get wireless.
"${config}".ssid
)\""
167 if [ -n "$
(printf "${ssid_list}" |
grep -Fo "${ssid}")" ]
169 uci -q set wireless."${config}".disabled=0
170 uci -q commit wireless
171 ubus call network reload
173 if [ "${trm_ifstatus}" = "true
" ]
175 f_log "info
" "status
::: wwan interface connected to uplink
${ssid} (${cnt}/${trm_maxretry}, ${sysver})"
179 uci -q set wireless."${config}".disabled=1
180 uci -q commit wireless
181 ubus call network reload
182 f_log "info
" "status
::: wwan interface can
't connect to uplink ${ssid} (${cnt}/${trm_maxretry}, ${sysver})"
187 f_log "info " "status ::: empty uplink list (${cnt}/${trm_maxretry}, ${sysver})"
193 f_log "info " "status ::: no wwan uplink found (${sysver})"
195 f_log "info " "status ::: wwan uplink still connected (${sysver})"
199 if [ "${trm_procd}" = "true" ]