1 #!/bin/sh /etc/rc.common
2 # Copyright (c) 2016-2021 Dirk Brenken (dev@brenken.org)
3 # This is free software, licensed under the GNU General Public License v3.
5 # set (s)hellcheck exceptions
6 # shellcheck disable=2034,2086,2154,3043,3060
11 extra_command
"scan" "<radio> Scan for available nearby uplinks"
12 extra_command
"setup" "[<iface>] [<zone>] [<metric>] Setup the travelmate uplink interface, by default 'trm_wwan' with firewall zone 'wan' and metric '100'"
14 trm_init
="/etc/init.d/travelmate"
15 trm_script
="/usr/bin/travelmate.sh"
16 trm_pidfile
="/var/run/travelmate.pid"
20 if [ -s "${trm_pidfile}" ]; then
23 rc_procd start_service
28 if "${trm_init}" enabled
; then
29 if [ "${action}" = "boot" ]; then
32 procd_open_instance
"travelmate"
33 procd_set_param
command "${trm_script}" "${@}"
34 procd_set_param pidfile
"${trm_pidfile}"
35 procd_set_param nice
"$(uci_get travelmate global trm_nice "0")"
36 procd_set_param stdout
1
37 procd_set_param stderr
1
44 local ppid pid timeout
46 timeout
="$(uci_get travelmate global trm_timeout)"
48 if [ -s "${trm_pidfile}" ]; then
49 ppid
="$(cat "${trm_pidfile}" 2>/dev/null)"
50 if [ -n "${ppid}" ]; then
51 pid
="$(pgrep -xnf "sleep ${timeout:-60} 0" -P ${ppid} 2>/dev/null)"
52 if [ -n "${pid}" ]; then
53 kill -INT ${pid} 2>/dev
/null
61 rc_procd
"${trm_script}" stop
66 local key keylist value rtfile
68 rtfile
="$(uci_get travelmate global trm_rtfile "/tmp
/trm_runtime.json
")"
69 json_load_file
"${rtfile}" >/dev
/null
2>&1
70 if json_select data
>/dev
/null
2>&1; then
71 printf "%s\n" "::: travelmate runtime information"
73 for key
in ${keylist}; do
74 json_get_var value
"${key}"
75 printf " + %-18s : %s\n" "${key}" "${value}"
78 printf "%s\n" "::: no travelmate runtime information available"
84 local result scan_dev radio
="${1:-"radio0"}"
86 scan_dev
="$(ubus -S call network.wireless status 2>/dev/null | jsonfilter -q -l1 -e "@.
${radio}.interfaces
[0].ifname
")"
87 result
="$(iwinfo "${scan_dev:-${radio}}" scan 2>/dev/null |
88 awk 'BEGIN{FS="[[:space
:]]"}/Address:/{var1=$NF}/ESSID:/{var2="";
89 for(i=12;i<=NF;i++)if(var2==""){var2=$i}else{var2=var2" "$i}}/Channel:/{var3=$NF}/Quality:/{split($NF,var0,"/")}/Encryption:/{var4="";
90 for(j=12;j<=NF;j++)if(var4==""){var4=$j}else{var4=var4" "$j};printf " %-11i%-10s%-35s%-20s%s
\n",(var0[1]*100/var0[2]),var3,var2,var1,var4}' |
92 printf '%s\n' "::: Available nearby uplinks on '${scan_dev:-${radio}}'"
94 if [ -n "${result}" ]; then
95 printf '%-15s%-10s%-35s%-20s%s\n' " Strength" "Channel" "ESSID" "BSSID" "Encryption"
96 printf '%s\n' " --------------------------------------------------------------------------------------"
97 printf '%s\n' "${result}"
99 printf '%s\n' "::: No scan results"
105 local iface cnt
=0 input
="${1:-"trm_wwan"}" zone="${2:-"wan"}" metric="${3:-"100"}"
107 iface
="$(uci_get travelmate global trm_iface)"
108 input
="${input//[+*~%&\$@\"\' ]/}"
109 zone
="${zone//[+*~%&\$@\"\' ]/}"
110 metric
="${metric//[^0-9]/}"
112 if [ -n "${iface}" ] && [ "${iface}" = "${input}" ]; then
113 printf "%s\n" "The uplink interface '${input}' has been already configured"
114 elif [ -n "${input}" ]; then
115 if [ -n "${iface}" ]; then
117 del network."${iface}"
118 del network."${iface}6"
122 set travelmate.global.trm_enabled="1"
123 set travelmate.global.trm_iface="${input}"
124 set network."${input}"="interface"
125 set network."${input}".proto="dhcp"
126 set network."${input}".metric="${metric}"
127 set network."${input}6"=interface
128 set network."${input}6".device="@${input}"
129 set network."${input}6".proto="dhcpv6"
134 while [ -n "$(uci -q get firewall.@zone["${cnt}"].name)" ]; do
135 if [ "$(uci -q get firewall.@zone["${cnt}"].name)" = "${zone}" ]; then
136 if [ -n "${iface}" ]; then
138 del_list firewall.@zone["${cnt}"].network="${iface}"
139 del_list firewall.@zone["${cnt}"].network="${iface}6"
143 add_list firewall.@zone["${cnt}"].network="${input}"
144 add_list firewall.@zone["${cnt}"].network="${input}6"
152 if [ -n "${iface}" ]; then
154 while [ -n "$(uci -q get wireless.@wifi-iface["${cnt}"].network)" ]; do
155 if [ "$(uci -q get wireless.@wifi-iface["${cnt}"].network)" = "${iface}" ]; then
156 uci
-q set wireless.@wifi-iface
["${cnt}"].network
="${input}"
160 uci
-q commit wireless
162 /etc
/init.d
/network reload
>/dev
/null
2>&1
163 /etc
/init.d
/firewall reload
>/dev
/null
2>&1
164 "${trm_init}" restart
172 iface
="$(uci_get travelmate global trm_iface)"
173 delay
="$(uci_get travelmate global trm_triggerdelay "2")"
174 PROCD_RELOAD_DELAY
=$
((delay
* 1000))
176 if [ -n "${iface}" ]; then
177 procd_add_interface_trigger
"interface.*.down" "${iface}" "${trm_init}" reload
179 procd_add_raw_trigger
"interface.*.up" "${PROCD_RELOAD_DELAY}" "${trm_init}" start
180 procd_add_config_trigger
"config.change" "travelmate" "${trm_init}" restart