travelmate: update 0.2.5
[feed/packages.git] / net / travelmate / files / travelmate.sh
1 #!/bin/sh
2 # travelmate, a wlan connection manager for travel router
3 # written by Dirk Brenken (dev@brenken.org)
4
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/>.
8
9 # prepare environment
10 #
11 LC_ALL=C
12 PATH="/usr/sbin:/usr/bin:/sbin:/bin"
13 trm_pid="${$}"
14 trm_ver="0.2.5"
15 trm_debug=0
16 trm_loop=30
17 trm_maxretry=3
18 trm_iw=1
19 trm_device=""
20
21 # function to prepare all relevant AP and STA interfaces
22 #
23 trm_prepare()
24 {
25 local config="${1}"
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)"
31
32 if [ "${mode}" = "ap" ] &&
33 ([ -z "${trm_device}" ] || [ "${trm_device}" = "${device}" ])
34 then
35 trm_aplist="${trm_aplist} ${ifname}"
36 if [ -z "${disabled}" ] || [ "${disabled}" = "1" ]
37 then
38 trm_set "none" "${config}" "${network}" "up"
39 fi
40 elif [ "${mode}" = "sta" ]
41 then
42 trm_stalist="${trm_stalist} ${config}_${network}"
43 if [ -z "${disabled}" ] || [ "${disabled}" = "0" ]
44 then
45 trm_set "none" "${config}" "${network}" "down"
46 fi
47 fi
48 }
49
50 # function to set different wlan interface status
51 #
52 trm_set()
53 {
54 local change="${1}"
55 local config="${2}"
56 local interface="${3}"
57 local command="${4}"
58
59 if [ "${command}" = "up" ]
60 then
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" ]
65 then
66 uci -q set wireless."${config}".disabled=1
67 ubus call network.interface."${interface}" "${command}"
68 fi
69
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)" ]
72 then
73 if [ "${change}" = "commit" ]
74 then
75 uci -q commit wireless
76 ubus call network reload
77 trm_check
78 elif [ "${change}" = "partial" ]
79 then
80 ubus call network reload
81 trm_check
82 elif [ "${change}" = "defer" ]
83 then
84 uci -q revert wireless
85 elif [ "${change}" = "revert" ]
86 then
87 uci -q revert wireless
88 ubus call network reload
89 trm_check
90 fi
91 fi
92 }
93
94 # function to check interface status on "up" event
95 #
96 trm_check()
97 {
98 local interface value
99 local cnt=0
100
101 for interface in ${trm_checklist}
102 do
103 while [ $((cnt)) -lt 15 ]
104 do
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}" ]
108 then
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}))"
110 json_cleanup
111 break
112 fi
113 cnt=$((cnt+1))
114 sleep 1
115 done
116 done
117 if [ -n "${trm_uplink}" ] && [ "${trm_status}" = "0" ]
118 then
119 ubus call network reload
120 eval "trm_count_${trm_uplink}=\$((trm_count_${trm_uplink}+1))"
121 trm_checklist=""
122 trm_uplink=""
123 trm_log "info" "uplink ${trm_ssid} get lost"
124 elif [ -z "${trm_uplink}" ] && [ -n "${trm_checklist}" ]
125 then
126 trm_checklist=""
127 fi
128 }
129
130 # function to write to syslog
131 #
132 trm_log()
133 {
134 local class="${1}"
135 local log_msg="${2}"
136
137 if [ -n "${log_msg}" ] && ([ "${class}" != "debug" ] || ([ "${class}" = "debug" ] && [ $((trm_debug)) -eq 1 ]))
138 then
139 logger -t "travelmate-${trm_ver}[${trm_pid}] ${class}" "${log_msg}" 2>&1
140 fi
141 }
142
143 # source required system libraries
144 #
145 if [ -r "/lib/functions.sh" ] && [ -r "/usr/share/libubox/jshn.sh" ]
146 then
147 . "/lib/functions.sh"
148 . "/usr/share/libubox/jshn.sh"
149 json_init
150 else
151 trm_log "error" "required system libraries not found"
152 exit 255
153 fi
154
155 # load uci config and check 'enabled' option
156 #
157 option_cb()
158 {
159 local option="${1}"
160 local value="${2}"
161 eval "${option}=\"${value}\""
162 }
163
164 config_load travelmate
165
166 if [ "${trm_enabled}" != "1" ]
167 then
168 trm_log "info" "travelmate is currently disabled, please set 'trm_enabled' to '1' to use this service"
169 exit 0
170 fi
171
172 # check for preferred wireless tool
173 #
174 if [ $((trm_iw)) -eq 1 ]
175 then
176 trm_scanner="$(which iw)"
177 else
178 trm_scanner="$(which iwinfo)"
179 fi
180
181 if [ -z "${trm_scanner}" ]
182 then
183 trm_log "error" "no wireless tool for wlan scanning found, please install 'iw' or 'iwinfo'"
184 exit 255
185 fi
186
187 # infinitive loop to establish and track STA uplink connections
188 #
189 while true
190 do
191 if [ -z "${trm_uplink}" ] || [ "${trm_status}" = "0" ]
192 then
193 trm_aplist=""
194 trm_stalist=""
195 config_load wireless
196 config_foreach trm_prepare wifi-iface
197 trm_set "commit"
198 for ap in ${trm_aplist}
199 do
200 ubus -t 10 wait_for hostapd."${ap}"
201 if [ $((trm_iw)) -eq 1 ]
202 then
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 "\" "}}')"
204 else
205 trm_ssidlist="$(${trm_scanner} "${ap}" scan | awk '/ESSID: ".*"/{ORS=" ";if (!seen[$0]++) for(i=2; i<=NF; i++) print $i}')"
206 fi
207 trm_log "debug" "main ::: scan-tool: ${trm_scanner}, ssidlist: ${trm_ssidlist}"
208 if [ -n "${trm_ssidlist}" ]
209 then
210 for sta in ${trm_stalist}
211 do
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 ]
217 then
218 if [ -n "$(printf "${trm_ssidlist}" | grep -Fo "${trm_ssid}")" ]
219 then
220 trm_set "partial" "${trm_config}" "${trm_network}" "up"
221 if [ "${trm_status}" = "1" ]
222 then
223 trm_checklist="${trm_network}"
224 trm_uplink="${trm_config}"
225 trm_set "defer"
226 trm_log "info" "wwan interface \"${trm_ifname}\" connected to uplink ${trm_ssid}"
227 break 2
228 else
229 trm_set "revert"
230 eval "trm_count_${trm_config}=\$((trm_count_${trm_config}+1))"
231 fi
232 fi
233 elif [ $((trm_count_${trm_config})) -eq $((trm_maxretry)) ] && [ $((trm_maxretry)) -ne 0 ]
234 then
235 eval "trm_count_${trm_config}=\$((trm_count_${trm_config}+1))"
236 trm_log "info" "uplink ${trm_ssid} disabled due to permanent connection failures"
237 fi
238 done
239 fi
240 sleep 5
241 done
242 else
243 trm_check
244 if [ -n "${trm_uplink}" ]
245 then
246 sleep ${trm_loop}
247 fi
248 fi
249 done