349 lines
11 KiB
Bash
Executable File
349 lines
11 KiB
Bash
Executable File
#!/bin/sh
|
||
# rpcd plugin "esim-tools" for the LuCI page (installed as /usr/libexec/rpcd/esim-tools).
|
||
#
|
||
# status settings, where the AT/MBIM ports are (USB path, driver), interface,
|
||
# ModemManager, dependencies, kit version, auto-update, history
|
||
# modem AT state: ATI, slot, SIM, operator, registration, signal
|
||
# profiles cached lpac answers (profile list, chip info) with their age
|
||
# run start a whitelisted esim command as the background job
|
||
# job the job's command, state, exit code and output
|
||
# cancel stop the job: esim gets TERM and still restarts ModemManager
|
||
# at one raw AT command to AT_PORT
|
||
# config_set AT_PORT / WDM / IFACE into /etc/esim-tools/esim.conf
|
||
# update_check latest commit on the git server vs the installed one
|
||
#
|
||
# rpcd kills calls after ~30 s; anything with lpac or a modem reset takes
|
||
# longer (ModemManager stop/start), hence the job.
|
||
|
||
. /usr/share/libubox/jshn.sh
|
||
export PATH=/usr/sbin:/usr/bin:/sbin:/bin
|
||
umask 022
|
||
|
||
KIT_DIR=/etc/esim-tools
|
||
CONF=$KIT_DIR/esim.conf
|
||
CACHE=$KIT_DIR/cache
|
||
HISTORY=$KIT_DIR/history.log
|
||
JOB=/tmp/esim-tools-job
|
||
|
||
b() { "$@" >/dev/null 2>&1 && echo 1 || echo 0; }
|
||
|
||
job_running() { [ -f "$JOB/pid" ] && kill -0 "$(cat "$JOB/pid")" 2>/dev/null; }
|
||
|
||
reply_error() {
|
||
json_init
|
||
json_add_string error "$1"
|
||
json_dump
|
||
exit 0
|
||
}
|
||
|
||
# AT_PORT, WDM, IFACE with the script's defaults
|
||
load_config() {
|
||
esim config > /tmp/esim-tools-cfg.$$ 2>/dev/null
|
||
while read -r k v; do
|
||
eval "cfg_$k=\$v"
|
||
done < /tmp/esim-tools-cfg.$$
|
||
rm -f /tmp/esim-tools-cfg.$$
|
||
}
|
||
|
||
# port_info <dev>: fields of the current json object - which USB device and
|
||
# interface the node belongs to (the M.2 modem shows up as a USB device)
|
||
port_info() {
|
||
n=${1##*/}
|
||
case "$n" in
|
||
cdc-wdm*) s=/sys/class/usbmisc/$n/device ;;
|
||
wwan*) s=/sys/class/wwan/$n/device ;;
|
||
*) s=/sys/class/tty/$n/device ;;
|
||
esac
|
||
json_add_string dev "$1"
|
||
json_add_boolean exists "$(b test -e "$1")"
|
||
i=$(readlink -f "$s" 2>/dev/null)
|
||
while [ -n "$i" ] && [ ! -f "$i/bInterfaceNumber" ]; do
|
||
i=${i%/*}
|
||
done
|
||
[ -n "$i" ] || return 0
|
||
u=${i%/*}
|
||
json_add_string ifnum "$(cat "$i/bInterfaceNumber")"
|
||
json_add_string driver "$(basename "$(readlink "$i/driver")" 2>/dev/null)"
|
||
json_add_string usb "${u##*/}"
|
||
json_add_string id "$(cat "$u/idVendor"):$(cat "$u/idProduct")"
|
||
json_add_string product "$(cat "$u/manufacturer" 2>/dev/null) $(cat "$u/product" 2>/dev/null)"
|
||
json_add_string speed "$(cat "$u/speed" 2>/dev/null)"
|
||
}
|
||
|
||
m_status() {
|
||
load_config
|
||
json_init
|
||
json_add_string at_port "$cfg_at_port"
|
||
json_add_string wdm "$cfg_wdm"
|
||
json_add_string iface "$cfg_iface"
|
||
json_add_object at_info
|
||
port_info "$cfg_at_port"
|
||
json_close_object
|
||
json_add_object wdm_info
|
||
port_info "$cfg_wdm"
|
||
json_close_object
|
||
|
||
# every modem node, for the port selects
|
||
json_add_array ports
|
||
for d in /dev/ttyUSB* /dev/ttyACM* /dev/cdc-wdm* /dev/wwan*; do
|
||
[ -e "$d" ] || continue
|
||
json_add_object ""
|
||
port_info "$d"
|
||
json_close_object
|
||
done
|
||
json_close_array
|
||
json_add_array ifaces
|
||
for n in $(uci -q show network | sed -n 's/^network\.\([^.=]*\)=interface$/\1/p'); do
|
||
json_add_object ""
|
||
json_add_string name "$n"
|
||
json_add_string proto "$(uci -q get "network.$n.proto")"
|
||
json_close_object
|
||
done
|
||
json_close_array
|
||
|
||
st=$(ifstatus "$cfg_iface" 2>/dev/null)
|
||
json_add_boolean iface_exists "$(b test -n "$st")"
|
||
json_add_boolean iface_up "$(b test "$(jsonfilter -s "$st" -e '@.up' 2>/dev/null)" = true)"
|
||
json_add_string iface_proto "$(uci -q get "network.$cfg_iface.proto")"
|
||
json_add_string iface_ip "$(jsonfilter -s "$st" -e '@["ipv4-address"][0].address' 2>/dev/null)"
|
||
json_add_string iface_uptime "$(jsonfilter -s "$st" -e '@.uptime' 2>/dev/null)"
|
||
json_add_string iface_device "$(jsonfilter -s "$st" -e '@.l3_device' 2>/dev/null)"
|
||
json_add_string apn "$(uci -q get "network.$cfg_iface.apn")"
|
||
json_add_boolean mm_running "$(b pidof ModemManager)"
|
||
|
||
json_add_object deps
|
||
for c in lpac uqmi sms_tool jsonfilter; do
|
||
json_add_boolean "$c" "$(b command -v "$c")"
|
||
done
|
||
json_close_object
|
||
|
||
json_add_string version "$(cat "$KIT_DIR/VERSION" 2>/dev/null)"
|
||
json_add_boolean auto_update "$(b grep -q 'esim auto-update' /etc/crontabs/root)"
|
||
json_add_string auto_update_last "$(tail -n 1 "$KIT_DIR/auto-update.log" 2>/dev/null)"
|
||
json_add_boolean busy "$(b job_running)"
|
||
|
||
# "<date>\t<rc>\t<command>", newest first
|
||
json_add_array history
|
||
tail -n 15 "$HISTORY" 2>/dev/null | sed -n '1!G;h;$p' > /tmp/esim-tools-hist.$$
|
||
while IFS="$(printf '\t')" read -r t rc c; do
|
||
json_add_object ""
|
||
json_add_string time "$t"
|
||
json_add_int rc "$rc"
|
||
json_add_string cmd "$c"
|
||
json_close_object
|
||
done < /tmp/esim-tools-hist.$$
|
||
rm -f /tmp/esim-tools-hist.$$
|
||
json_close_array
|
||
json_dump
|
||
}
|
||
|
||
m_modem() {
|
||
json_init
|
||
# the job owns the modem (resets it, lpac talks to it)
|
||
if job_running; then
|
||
json_add_boolean busy 1
|
||
json_add_string cmd "$(cat "$JOB/cmd")"
|
||
json_dump
|
||
return
|
||
fi
|
||
out=$(esim state 2>&1) || reply_error "$(echo "$out" | tail -n 1 | sed 's/^!! //')"
|
||
echo "$out" > /tmp/esim-tools-state.$$
|
||
while read -r k v; do
|
||
json_add_string "$k" "$v"
|
||
done < /tmp/esim-tools-state.$$
|
||
rm -f /tmp/esim-tools-state.$$
|
||
json_dump
|
||
}
|
||
|
||
# lpac's last lines are JSON already: glued as is
|
||
m_profiles() {
|
||
l=$(cat "$CACHE/profiles.json" 2>/dev/null)
|
||
c=$(cat "$CACHE/chip.json" 2>/dev/null)
|
||
lt=$(date -r "$CACHE/profiles.json" +%s 2>/dev/null)
|
||
ct=$(date -r "$CACHE/chip.json" +%s 2>/dev/null)
|
||
printf '{"list":%s,"list_time":%s,"chip":%s,"chip_time":%s,"now":%s}\n' \
|
||
"${l:-null}" "${lt:-0}" "${c:-null}" "${ct:-0}" "$(date +%s)"
|
||
}
|
||
|
||
iccid_ok() {
|
||
case "$1" in '' | *[!0-9A-Fa-f]*) return 1 ;; esac
|
||
[ ${#1} -ge 18 ] && [ ${#1} -le 22 ]
|
||
}
|
||
|
||
m_run() {
|
||
input=$(cat)
|
||
cmd=$(jsonfilter -s "$input" -e '@.cmd' 2>/dev/null)
|
||
a1=$(jsonfilter -s "$input" -e '@.a' 2>/dev/null)
|
||
a2=$(jsonfilter -s "$input" -e '@.b' 2>/dev/null)
|
||
a3=$(jsonfilter -s "$input" -e '@.c' 2>/dev/null)
|
||
flag=$(jsonfilter -s "$input" -e '@.flag' 2>/dev/null)
|
||
label=
|
||
case "$cmd" in
|
||
refresh) set -- esim refresh ;;
|
||
enable | disable)
|
||
iccid_ok "$a1" || reply_error "bad ICCID"
|
||
set -- esim "$cmd" "$a1"
|
||
;;
|
||
delete)
|
||
iccid_ok "$a1" || reply_error "bad ICCID"
|
||
set -- esim delete "$a1" --yes
|
||
;;
|
||
rename)
|
||
iccid_ok "$a1" || reply_error "bad ICCID"
|
||
a2=$(printf '%s' "$a2" | tr -d '\r\n\t')
|
||
[ -n "$a2" ] || reply_error "пустое имя"
|
||
[ ${#a2} -le 64 ] || reply_error "имя длиннее 64 символов"
|
||
set -- esim rename "$a1" "$a2"
|
||
;;
|
||
add)
|
||
# a = SM-DP+ address, b = matching id, c = confirmation code
|
||
case "$a1" in '' | *[!A-Za-z0-9.:-]*) reply_error "bad SM-DP+ address" ;; esac
|
||
case "$a2" in '' | *[!A-Za-z0-9_.-]*) reply_error "bad matching id" ;; esac
|
||
case "$a3" in *[!A-Za-z0-9]*) reply_error "bad confirmation code" ;; esac
|
||
en=--no-enable
|
||
[ "$flag" = true ] || [ "$flag" = 1 ] && en=--enable
|
||
set -- esim add "$a1" "$a2"
|
||
[ -n "$a3" ] && set -- "$@" "$a3"
|
||
set -- "$@" "$en"
|
||
# the activation code stays out of the page and the history
|
||
label="esim add $a1 ***${a3:+ ***} $en"
|
||
;;
|
||
slot)
|
||
case "$a1" in esim | sim) ;; *) reply_error "bad slot" ;; esac
|
||
set -- esim slot "$a1"
|
||
;;
|
||
apn)
|
||
case "$a1" in '' | *[!A-Za-z0-9._-]*) reply_error "bad APN" ;; esac
|
||
set -- esim apn "$a1"
|
||
;;
|
||
update) set -- esim update ;;
|
||
autoupdate-on) set -- esim auto-update on ;;
|
||
autoupdate-off) set -- esim auto-update off ;;
|
||
*) reply_error "unknown command: $cmd" ;;
|
||
esac
|
||
start_job "${label:-$*}" "$@"
|
||
}
|
||
|
||
# start_job <label> cmd args...: run it in the background, reply at once
|
||
start_job() {
|
||
job_running && reply_error "занято: $(cat "$JOB/cmd")"
|
||
label=$1
|
||
shift
|
||
rm -rf "$JOB"
|
||
mkdir -p "$JOB"
|
||
echo "$label" > "$JOB/cmd"
|
||
# detached from rpcd: all fds redirected, so the call returns at once
|
||
(
|
||
"$@"
|
||
rc=$?
|
||
echo $rc > "$JOB/rc"
|
||
printf '%s\t%s\t%s\n' "$(date '+%Y-%m-%d %H:%M:%S')" "$rc" "$label" >> "$HISTORY"
|
||
tail -n 50 "$HISTORY" > "$HISTORY.tmp" && mv "$HISTORY.tmp" "$HISTORY"
|
||
) < /dev/null > "$JOB/log" 2>&1 &
|
||
echo $! > "$JOB/pid"
|
||
json_init
|
||
json_add_string started "$label"
|
||
json_dump
|
||
}
|
||
|
||
# children <pid>: direct children from /proc (no pgrep -P in busybox by default)
|
||
children() {
|
||
for s in /proc/[0-9]*/stat; do
|
||
read -r p _ _ pp _ < "$s" 2>/dev/null && [ "$pp" = "$1" ] && echo "$p"
|
||
done
|
||
}
|
||
|
||
# kill_tree <pid>: children first, so esim's own TERM trap (restore) runs
|
||
# once its foreground lpac/sleep is gone
|
||
kill_tree() {
|
||
for c in $(children "$1"); do
|
||
kill_tree "$c"
|
||
done
|
||
kill -TERM "$1" 2>/dev/null
|
||
}
|
||
|
||
m_cancel() {
|
||
job_running || reply_error "ничего не выполняется"
|
||
echo ">> прервано со страницы" >> "$JOB/log"
|
||
# the job's subshell itself stays: it writes rc and the history line
|
||
for c in $(children "$(cat "$JOB/pid")"); do
|
||
kill_tree "$c"
|
||
done
|
||
json_init
|
||
json_add_boolean cancelled 1
|
||
json_dump
|
||
}
|
||
|
||
m_job() {
|
||
json_init
|
||
json_add_string cmd "$(cat "$JOB/cmd" 2>/dev/null)"
|
||
json_add_boolean running "$(b job_running)"
|
||
[ -f "$JOB/rc" ] && json_add_int rc "$(cat "$JOB/rc")"
|
||
json_add_string log "$(tail -c 20000 "$JOB/log" 2>/dev/null)"
|
||
json_dump
|
||
}
|
||
|
||
m_at() {
|
||
input=$(cat)
|
||
cmd=$(jsonfilter -s "$input" -e '@.cmd' 2>/dev/null | tr -d '\r\n')
|
||
case "$cmd" in [Aa][Tt]*) ;; *) reply_error "команда должна начинаться с AT" ;; esac
|
||
job_running && reply_error "занято: $(cat "$JOB/cmd")"
|
||
load_config
|
||
json_init
|
||
json_add_string port "$cfg_at_port"
|
||
json_add_string output "$(esim at "$cmd" 2>&1)"
|
||
json_dump
|
||
}
|
||
|
||
m_config_set() {
|
||
input=$(cat)
|
||
at_port=$(jsonfilter -s "$input" -e '@.at_port' 2>/dev/null)
|
||
wdm=$(jsonfilter -s "$input" -e '@.wdm' 2>/dev/null)
|
||
iface=$(jsonfilter -s "$input" -e '@.iface' 2>/dev/null)
|
||
for d in "$at_port" "$wdm"; do
|
||
case "$d" in /dev/[A-Za-z0-9]*) ;; *) reply_error "bad device: $d" ;; esac
|
||
case "${d#/dev/}" in *[!A-Za-z0-9_-]*) reply_error "bad device: $d" ;; esac
|
||
done
|
||
case "$iface" in '' | *[!A-Za-z0-9_]*) reply_error "bad interface: $iface" ;; esac
|
||
job_running && reply_error "занято: $(cat "$JOB/cmd")"
|
||
mkdir -p "$KIT_DIR"
|
||
{
|
||
[ -f "$CONF" ] && grep -vE '^(AT_PORT|WDM|IFACE)=' "$CONF"
|
||
printf 'AT_PORT=%s\nWDM=%s\nIFACE=%s\n' "$at_port" "$wdm" "$iface"
|
||
} > "$CONF.tmp" && mv "$CONF.tmp" "$CONF"
|
||
json_init
|
||
json_add_boolean saved 1
|
||
json_dump
|
||
}
|
||
|
||
m_update_check() {
|
||
out=$(esim check-update 2>&1) || reply_error "$(echo "$out" | tail -n 1 | sed 's/^!! //')"
|
||
json_init
|
||
echo "$out" > /tmp/esim-tools-upd.$$
|
||
while read -r k v; do
|
||
json_add_string "$k" "$v"
|
||
done < /tmp/esim-tools-upd.$$
|
||
rm -f /tmp/esim-tools-upd.$$
|
||
json_dump
|
||
}
|
||
|
||
case "$1" in
|
||
list)
|
||
echo '{"status":{},"modem":{},"profiles":{},"job":{},"cancel":{},"run":{"cmd":"str","a":"str","b":"str","c":"str","flag":true},"at":{"cmd":"str"},"config_set":{"at_port":"str","wdm":"str","iface":"str"},"update_check":{}}'
|
||
;;
|
||
call)
|
||
case "$2" in
|
||
status) m_status ;;
|
||
modem) m_modem ;;
|
||
profiles) m_profiles ;;
|
||
job) m_job ;;
|
||
cancel) m_cancel ;;
|
||
run) m_run ;;
|
||
at) m_at ;;
|
||
config_set) m_config_set ;;
|
||
update_check) m_update_check ;;
|
||
*) reply_error "unknown method" ;;
|
||
esac
|
||
;;
|
||
esac
|