travelmate: update 0.7.2
[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 # set initial defaults
10 #
11 LC_ALL=C
12 PATH="/usr/sbin:/usr/bin:/sbin:/bin"
13 trm_ver="0.7.2"
14 trm_sysver="$(ubus -S call system board | jsonfilter -e '@.release.description')"
15 trm_enabled=0
16 trm_debug=0
17 trm_automatic=1
18 trm_maxretry=3
19 trm_maxwait=30
20 trm_timeout=60
21 trm_iw="$(command -v iw)"
22 trm_rtfile="/tmp/trm_runtime.json"
23
24 # source required system library
25 #
26 if [ -r "/lib/functions.sh" ] && [ -r "/usr/share/libubox/jshn.sh" ]
27 then
28 . "/lib/functions.sh"
29 . "/usr/share/libubox/jshn.sh"
30 else
31 f_log "error" "system libraries not found"
32 fi
33
34 # f_envload: load travelmate environment
35 #
36 f_envload()
37 {
38 # initialize lists
39 #
40 trm_aplist=""
41 trm_stalist=""
42 trm_radiolist=""
43
44 # load uci config and check 'enabled' option
45 #
46 option_cb()
47 {
48 local option="${1}"
49 local value="${2}"
50 eval "${option}=\"${value}\""
51 }
52 config_load travelmate
53
54 if [ ${trm_enabled} -ne 1 ]
55 then
56 f_log "info " "travelmate is currently disabled, please set 'trm_enabled' to '1' to use this service"
57 exit 0
58 fi
59
60 # check for wireless tool
61 #
62 if [ -z "${trm_iw}" ]
63 then
64 f_log "error" "no wireless tool found, please install package 'iw'"
65 fi
66 }
67
68 # f_prepare: gather radio information & bring down all STA interfaces
69 #
70 f_prepare()
71 {
72 local config="${1}"
73 local mode="$(uci -q get wireless."${config}".mode)"
74 local radio="$(uci -q get wireless."${config}".device)"
75 local disabled="$(uci -q get wireless."${config}".disabled)"
76
77 if [ "${mode}" = "ap" ] && ([ -z "${disabled}" ] || [ "${disabled}" = "0" ]) && \
78 ([ -z "${trm_radio}" ] || [ "${trm_radio}" = "${radio}" ])
79 then
80 trm_radiolist="${trm_radiolist} ${radio}"
81 elif [ "${mode}" = "sta" ]
82 then
83 trm_stalist="${trm_stalist} ${config}_${radio}"
84 if [ -z "${disabled}" ] || [ "${disabled}" = "0" ]
85 then
86 uci -q set wireless."${config}".disabled=1
87 fi
88 fi
89 f_log "debug" "mode: ${mode}, radio: ${radio}, config: ${config}, disabled: ${disabled}"
90 }
91
92 # f_check: check interface status
93 #
94 f_check()
95 {
96 local ifname radio cnt=1 mode="${1}"
97 trm_ifstatus="false"
98
99 while [ ${cnt} -le ${trm_maxwait} ]
100 do
101 if [ "${mode}" = "ap" ]
102 then
103 for radio in ${trm_radiolist}
104 do
105 trm_ifstatus="$(ubus -S call network.wireless status | jsonfilter -e "@.${radio}.up")"
106 if [ "${trm_ifstatus}" = "true" ]
107 then
108 trm_aplist="${trm_aplist} $(ubus -S call network.wireless status | jsonfilter -e "@.${radio}.interfaces[@.config.mode=\"ap\"].ifname")_${radio}"
109 ifname="${trm_aplist}"
110 else
111 trm_aplist=""
112 trm_ifstatus="false"
113 break
114 fi
115 done
116 else
117 ifname="$(ubus -S call network.wireless status | jsonfilter -l1 -e '@.*.interfaces[@.config.mode="sta"].ifname')"
118 if [ -n "${ifname}" ]
119 then
120 trm_ifstatus="$(ubus -S call network.interface dump | jsonfilter -e "@.interface[@.device=\"${ifname}\"].up")"
121 fi
122 fi
123 if [ "${mode}" = "initial" ] || [ "${trm_ifstatus}" = "true" ]
124 then
125 break
126 fi
127 cnt=$((cnt+1))
128 sleep 1
129 done
130 f_log "debug" "mode: ${mode}, name: ${ifname}, status: ${trm_ifstatus}, count: ${cnt}, max-wait: ${trm_maxwait}, automatic: ${trm_automatic}"
131 }
132
133 # f_jsnupdate: update runtime information
134 #
135 f_jsnupdate()
136 {
137 local iface="${1}" radio="${2}" ssid="${3}"
138
139 json_init
140 json_add_object "data"
141 json_add_string "travelmate_version" "${trm_ver}"
142 json_add_string "station_connection" "${trm_ifstatus}"
143 json_add_string "station_ssid" "${ssid}"
144 json_add_string "station_interface" "${iface}"
145 json_add_string "station_radio" "${radio}"
146 json_add_string "last_rundate" "$(/bin/date "+%d.%m.%Y %H:%M:%S")"
147 json_add_string "system" "${trm_sysver}"
148 json_close_object
149 json_dump > "${trm_rtfile}"
150 }
151
152 # f_status: output runtime information
153 #
154 f_status()
155 {
156 local key keylist value
157
158 if [ -s "${trm_rtfile}" ]
159 then
160 printf "%s\n" "::: travelmate runtime information"
161 json_load "$(cat "${trm_rtfile}" 2>/dev/null)"
162 json_select data
163 json_get_keys keylist
164 for key in ${keylist}
165 do
166 json_get_var value "${key}"
167 printf " %-18s : %s\n" "${key}" "${value}"
168 done
169 fi
170 }
171
172 # f_log: write to syslog, exit on error
173 #
174 f_log()
175 {
176 local class="${1}"
177 local log_msg="${2}"
178
179 if [ -n "${log_msg}" ] && ([ "${class}" != "debug" ] || [ ${trm_debug} -eq 1 ])
180 then
181 logger -t "travelmate-[${trm_ver}] ${class}" "${log_msg}"
182 if [ "${class}" = "error" ]
183 then
184 logger -t "travelmate-[${trm_ver}] ${class}" "Please check 'https://github.com/openwrt/packages/blob/master/net/travelmate/files/README.md' (${trm_sysver})"
185 exit 255
186 fi
187 fi
188 }
189
190 # f_main: main function for connection handling
191 #
192 f_main()
193 {
194 local config ssid_list ap ap_radio sta_ssid sta_radio sta_iface cnt=1
195
196 f_check "initial"
197 if [ "${trm_ifstatus}" != "true" ]
198 then
199 config_load wireless
200 config_foreach f_prepare wifi-iface
201 if [ -n "$(uci -q changes wireless)" ]
202 then
203 uci -q commit wireless
204 ubus call network reload
205 fi
206 f_check "ap"
207 f_log "debug" "ap-list: ${trm_aplist}, sta-list: ${trm_stalist}"
208 if [ -z "${trm_aplist}" ] || [ -z "${trm_stalist}" ]
209 then
210 f_log "error" "no usable AP/STA configuration found"
211 fi
212 for ap in ${trm_aplist}
213 do
214 cnt=1
215 ap_radio="${ap##*_}"
216 ap="${ap%%_*}"
217 if [ -z "$(printf "%s" "${trm_stalist}" | grep -Fo "_${ap_radio}")" ]
218 then
219 continue
220 fi
221 while [ ${cnt} -le ${trm_maxretry} ]
222 do
223 ssid_list="$(${trm_iw} dev "${ap}" scan 2>/dev/null | \
224 awk '/SSID: /{if(!seen[$0]++){printf "\"";for(i=2; i<=NF; i++)if(i==2)printf $i;else printf " "$i;printf "\" "}}')"
225 f_log "debug" "iw: ${trm_iw}, ap: ${ap}, ssids: ${ssid_list}"
226 if [ -n "${ssid_list}" ]
227 then
228 for sta in ${trm_stalist}
229 do
230 config="${sta%%_*}"
231 sta_radio="${sta##*_}"
232 sta_ssid="$(uci -q get wireless."${config}".ssid)"
233 sta_iface="$(uci -q get wireless."${config}".network)"
234 if [ -n "$(printf "%s" "${ssid_list}" | grep -Fo "\"${sta_ssid}\"")" ] && [ "${ap_radio}" = "${sta_radio}" ]
235 then
236 uci -q set wireless."${config}".disabled=0
237 ubus call network reload
238 f_check "sta"
239 if [ "${trm_ifstatus}" = "true" ]
240 then
241 uci -q commit wireless
242 f_log "info " "interface '${sta_iface}' on '${sta_radio}' connected to uplink '${sta_ssid}' (${trm_sysver})"
243 f_jsnupdate "${sta_iface}" "${sta_radio}" "${sta_ssid}"
244 return 0
245 else
246 uci -q revert wireless
247 ubus call network reload
248 f_log "info " "interface '${sta_iface}' on '${sta_radio}' can't connect to uplink '${sta_ssid}' (${trm_sysver})"
249 f_jsnupdate "${sta_iface}" "${sta_radio}" "${sta_ssid}"
250 fi
251 fi
252 done
253 fi
254 cnt=$((cnt+1))
255 sleep 5
256 done
257 done
258 else
259 if [ ! -s "${trm_rtfile}" ]
260 then
261 config="$(ubus -S call network.wireless status | jsonfilter -l1 -e '@.*.interfaces[@.config.mode="sta"].section')"
262 sta_radio="$(uci -q get wireless."${config}".device)"
263 sta_ssid="$(uci -q get wireless."${config}".ssid)"
264 sta_iface="$(uci -q get wireless."${config}".network)"
265 f_jsnupdate "${sta_iface}" "${sta_radio}" "${sta_ssid}"
266 fi
267 fi
268 }
269
270 # handle different travelmate actions
271 #
272 f_envload
273 case "${1}" in
274 status)
275 f_status
276 ;;
277 *)
278 f_main
279 while [ ${trm_automatic} -eq 1 ]
280 do
281 sleep ${trm_timeout}
282 f_envload
283 f_main
284 done
285 ;;
286 esac
287 exit 0