First Commit
[librecmc/package-feed.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.7"
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 f_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 disabled="$(uci -q get wireless."${config}".disabled)"
30
31     if [ "${mode}" = "ap" ] && [ -n "${network}" ] &&
32         ([ -z "${trm_device}" ] || [ "${trm_device}" = "${device}" ])
33     then
34         f_ifname "${device}"
35         if [ -z "${disabled}" ] || [ "${disabled}" = "1" ]
36         then
37             f_set "none" "${config}" "${network}" "up"
38         fi
39     elif [ "${mode}" = "sta" ] && [ -n "${network}" ]
40     then
41         trm_stalist="${trm_stalist} ${config}_${network}"
42         if [ -z "${disabled}" ] || [ "${disabled}" = "0" ]
43         then
44             f_set "none" "${config}" "${network}" "down"
45         fi
46     fi
47 }
48
49 # function to set different wlan interface status
50 #
51 f_set()
52 {
53     local change="${1}"
54     local config="${2}"
55     local interface="${3}"
56     local command="${4}"
57
58     if [ "${command}" = "up" ]
59     then
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" ]
64     then
65         uci -q set wireless."${config}".disabled=1
66         ubus call network.interface."${interface}" "${command}"
67     fi
68
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)" ]
71     then
72         if [ "${change}" = "commit" ]
73         then
74             uci -q commit wireless
75             ubus call network reload
76             f_check
77         elif [ "${change}" = "partial" ]
78         then
79             ubus call network reload
80             f_check
81         elif [ "${change}" = "defer" ]
82         then
83             uci -q revert wireless
84         elif [ "${change}" = "revert" ]
85         then
86             uci -q revert wireless
87             ubus call network reload
88             f_check
89         fi
90     fi
91 }
92
93 # function to get ap ifnames  by ubus call
94 #
95 f_ifname()
96 {
97     local device="${1}"
98     local name cfg
99
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}
105     do
106         json_select "${iface}"
107         json_get_var name ifname
108         json_select "config"
109         json_get_var cfg mode
110         if [ -n "${name}" ] && [ "${cfg}" = "ap" ]
111         then
112             trm_aplist="${trm_aplist} ${name}"
113         fi
114     done
115 }
116
117 # function to check interface status on "up" event
118 #
119 f_check()
120 {
121     local interface value
122     local cnt=0
123
124     for interface in ${trm_checklist}
125     do
126         while [ $((cnt)) -lt 15 ]
127         do
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}" ]
131             then
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}))"
133                 json_cleanup
134                 break
135             fi
136             cnt=$((cnt+1))
137             sleep 1
138         done
139     done
140     if [ -n "${trm_uplink}" ] && [ "${trm_status}" = "0" ]
141     then
142         ubus call network reload
143         eval "trm_count_${trm_uplink}=\$((trm_count_${trm_uplink}+1))"
144         trm_checklist=""
145         trm_uplink=""
146         f_log "info" "uplink ${trm_ssid} get lost"
147     elif [ -z "${trm_uplink}" ] && [ -n "${trm_checklist}" ]
148     then
149         trm_checklist=""
150     fi
151 }
152
153 # function to write to syslog
154 #
155 f_log()
156 {
157     local class="${1}"
158     local log_msg="${2}"
159
160     if [ -n "${log_msg}" ] && ([ "${class}" != "debug" ] || ([ "${class}" = "debug" ] && [ $((trm_debug)) -eq 1 ]))
161     then
162         logger -t "travelmate-${trm_ver}[${trm_pid}] ${class}" "${log_msg}" 2>&1
163     fi
164 }
165
166 # source required system libraries
167 #
168 if [ -r "/lib/functions.sh" ] && [ -r "/usr/share/libubox/jshn.sh" ]
169 then
170     . "/lib/functions.sh"
171     . "/usr/share/libubox/jshn.sh"
172     json_init
173 else
174     f_log "error" "required system libraries not found"
175     exit 255
176 fi
177
178 # load uci config and check 'enabled' option
179 #
180 option_cb()
181 {
182     local option="${1}"
183     local value="${2}"
184     eval "${option}=\"${value}\""
185 }
186
187 config_load travelmate
188
189 if [ "${trm_enabled}" != "1" ]
190 then
191     f_log "info" "travelmate is currently disabled, please set 'trm_enabled' to '1' to use this service"
192     exit 0
193 fi
194
195 # check for preferred wireless tool
196 #
197 if [ $((trm_iw)) -eq 1 ]
198 then
199     trm_scanner="$(which iw)"
200 else
201     trm_scanner="$(which iwinfo)"
202 fi
203
204 if [ -z "${trm_scanner}" ]
205 then
206     f_log "error" "no wireless tool for wlan scanning found, please install 'iw' or 'iwinfo'"
207     exit 1
208 fi
209
210 # infinitive loop to establish and track STA uplink connections
211 #
212 while true
213 do
214     if [ -z "${trm_uplink}" ] || [ "${trm_status}" = "0" ]
215     then
216         trm_aplist=""
217         trm_stalist=""
218         config_load wireless
219         config_foreach f_prepare wifi-iface
220         f_set "commit"
221         if [ -z "${trm_aplist}" ]
222         then
223             f_log "error" "no usable AP configuration found, please check '/etc/config/wireless'"
224             exit 1
225         fi
226         for ap in ${trm_aplist}
227         do
228             ubus -t 10 wait_for hostapd."${ap}"
229             if [ $((trm_iw)) -eq 1 ]
230             then
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 "\" "}}')"
232             else
233                 trm_ssidlist="$(${trm_scanner} "${ap}" scan | awk '/ESSID: ".*"/{ORS=" ";if (!seen[$0]++) for(i=2; i<=NF; i++) print $i}')"
234             fi
235             f_log "debug" "main ::: scan-tool: ${trm_scanner}, aplist: ${trm_aplist}, ssidlist: ${trm_ssidlist}"
236             if [ -n "${trm_ssidlist}" ]
237             then
238                 if [ -z "${trm_stalist}" ]
239                 then
240                     f_log "error" "no usable STA configuration found, please check '/etc/config/wireless'"
241                     exit 1
242                 fi
243                 for sta in ${trm_stalist}
244                 do
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 ]
249                     then
250                         if [ -n "$(printf "${trm_ssidlist}" | grep -Fo "${trm_ssid}")" ]
251                         then
252                             f_set "partial" "${trm_config}" "${trm_network}" "up"
253                             if [ "${trm_status}" = "1" ]
254                             then
255                                 trm_checklist="${trm_network}"
256                                 trm_uplink="${trm_config}"
257                                 f_set "defer"
258                                 f_log "info" "wwan interface connected to uplink ${trm_ssid}" 
259                                 break 2
260                             else
261                                 f_set "revert"
262                                 eval "trm_count_${trm_config}=\$((trm_count_${trm_config}+1))"
263                             fi
264                         fi
265                     elif [ $((trm_count_${trm_config})) -eq $((trm_maxretry)) ] && [ $((trm_maxretry)) -ne 0 ]
266                     then
267                         eval "trm_count_${trm_config}=\$((trm_count_${trm_config}+1))"
268                         f_log "info" "uplink ${trm_ssid} disabled due to permanent connection failures"
269                     fi
270                 done
271             fi
272             sleep 5
273         done
274     else
275         f_check
276         if [ -n "${trm_uplink}" ]
277         then
278             sleep ${trm_loop}
279         fi
280     fi
281 done