Fixed ui, fixed lpac loop

This commit is contained in:
Sam Latsin 2026-10-13 11:34:05 +03:00
parent ca0119ddb3
commit fcd2b86ba1
4 changed files with 78 additions and 13 deletions

View File

@ -8,7 +8,7 @@
},
"write": {
"ubus": {
"esim-tools": [ "run", "at", "config_set" ]
"esim-tools": [ "run", "cancel", "at", "config_set" ]
}
}
}

View File

@ -13,6 +13,7 @@ var callModem = rpc.declare({ object: 'esim-tools', method: 'modem', expect: { '
var callProfiles = rpc.declare({ object: 'esim-tools', method: 'profiles', expect: { '': {} } });
var callJob = rpc.declare({ object: 'esim-tools', method: 'job', expect: { '': {} } });
var callRun = rpc.declare({ object: 'esim-tools', method: 'run', params: [ 'cmd', 'a', 'b', 'c', 'flag' ], expect: { '': {} } });
var callCancel = rpc.declare({ object: 'esim-tools', method: 'cancel', expect: { '': {} } });
var callAt = rpc.declare({ object: 'esim-tools', method: 'at', params: [ 'cmd' ], expect: { '': {} } });
var callConfigSet = rpc.declare({ object: 'esim-tools', method: 'config_set', params: [ 'at_port', 'wdm', 'iface' ], expect: { '': {} } });
var callUpdateCheck = rpc.declare({ object: 'esim-tools', method: 'update_check', expect: { '': {} } });
@ -230,9 +231,9 @@ return view.extend({
'style': 'width:24px;height:24px;vertical-align:middle;margin-right:.4em' }) : '';
rows.push(E('tr', { 'class': 'tr' }, [
E('td', { 'class': 'td' }, on ? E('strong', { 'style': GREEN }, '● включён') : E('span', '○ выключен')),
E('td', { 'class': 'td' }, [ icon, E('strong', pr.profileNickname || pr.serviceProviderName || '—'),
pr.profileNickname && pr.serviceProviderName ? [ E('br'), desc(pr.serviceProviderName) ] : '',
pr.profileName && pr.profileName != pr.serviceProviderName ? [ E('br'), desc(pr.profileName) ] : '' ]),
E('td', { 'class': 'td' }, [].concat(icon, E('strong', pr.profileNickname || pr.serviceProviderName || '—'),
pr.profileNickname && pr.serviceProviderName ? [ E('br'), desc(pr.serviceProviderName) ] : [],
pr.profileName && pr.profileName != pr.serviceProviderName ? [ E('br'), desc(pr.profileName) ] : [])),
E('td', { 'class': 'td' }, E('code', pr.iccid)),
E('td', { 'class': 'td right', 'style': 'white-space:nowrap' }, [
on
@ -446,7 +447,7 @@ return view.extend({
E('table', { 'class': 'table' }, [
row('AT-порт', portText(st.at_info)),
row('MBIM (lpac)', portText(st.wdm_info)),
row('Интерфейс ' + st.iface, [ ifState, st.iface_proto ? desc(' (' + st.iface_proto + ')') : '' ]),
row('Интерфейс ' + st.iface, [].concat(ifState, st.iface_proto ? desc(' (' + st.iface_proto + ')') : '')),
row('ModemManager', st.mm_running ? 'работает' : E('strong', { 'style': RED },
st.busy ? 'остановлен на время операции' : 'остановлен')),
row('APN', [ apn, ' ', this.button('Применить', 'apply', 'apn', function() {
@ -520,6 +521,7 @@ return view.extend({
var state = j.running ? E('em', 'выполняется…')
: j.rc === 0 ? E('strong', { 'style': GREEN }, 'готово') : E('strong', { 'style': RED }, 'ошибка (код ' + j.rc + ')');
dom.content(this.jobTitle, [ 'Последняя команда: ', E('code', j.cmd), ' — ', state ]);
this.cancelBtn.style.display = j.running ? '' : 'none';
this.jobLog.textContent = j.log || '';
this.jobLog.scrollTop = this.jobLog.scrollHeight;
},
@ -593,7 +595,16 @@ return view.extend({
this.maintBox = box();
this.jobTitle = E('h3');
this.jobLog = E('pre', { 'style': 'white-space:pre-wrap;max-height:24em;overflow:auto' });
this.jobBox = E('div', { 'class': 'cbi-section' }, [ this.jobTitle, this.jobLog ]);
this.cancelBtn = E('button', { 'class': 'btn cbi-button cbi-button-negative', 'style': 'display:none',
'click': ui.createHandlerFn(this, function() {
if (!window.confirm('Прервать команду? Скрипт остановится и запустит ModemManager обратно.'))
return;
return callCancel().then(function(r) {
if (r.error)
ui.addNotification(null, E('p', r.error), 'warning');
});
}) }, 'Прервать');
this.jobBox = E('div', { 'class': 'cbi-section' }, [ this.jobTitle, this.jobLog, E('div', { 'class': 'right' }, this.cancelBtn) ]);
this.renderAll(data[0], data[1]);
this.showJob(data[2]);

View File

@ -7,6 +7,7 @@
# 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
@ -245,6 +246,34 @@ start_job() {
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)"
@ -300,7 +329,7 @@ m_update_check() {
case "$1" in
list)
echo '{"status":{},"modem":{},"profiles":{},"job":{},"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":{}}'
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
@ -308,6 +337,7 @@ case "$1" in
modem) m_modem ;;
profiles) m_profiles ;;
job) m_job ;;
cancel) m_cancel ;;
run) m_run ;;
at) m_at ;;
config_set) m_config_set ;;

View File

@ -35,8 +35,13 @@ UQMI_BIN="$(PATH=/usr/sbin:/usr/bin:/sbin:/bin command -v uqmi)"
PREPARED=0
# зависший порт не должен вешать скрипт (и страницу LuCI)
TO=
command -v timeout >/dev/null 2>&1 && TO="timeout 5"
# lpac через uqmi -m может повиснуть на cdc-wdm навсегда, если MBIM не готов
TO= LTO= LTO_DL=
if command -v timeout >/dev/null 2>&1; then
TO="timeout 5"
LTO="timeout 90"
LTO_DL="timeout 300"
fi
usage() {
cat <<'EOF'
@ -97,6 +102,16 @@ reset_modem() {
sleep 5
}
# после перезагрузки модема cdc-wdm появляется позже AT-порта
wait_wdm() {
i=0
while [ ! -e "$WDM" ]; do
[ $i -ge 20 ] && die "нет $WDM после перезагрузки модема"
sleep 2
i=$((i + 1))
done
}
ensure_esim_slot() {
if ! at "AT^SWITCH_SLOT?" | grep -q "SIM2 ENABLE"; then
msg "переключаю модем на eSIM (SIM2)"
@ -121,8 +136,9 @@ prepare() {
[ -n "$UQMI_BIN" ] || die "не найдено: uqmi (opkg install uqmi)"
wait_modem
ensure_esim_slot
# ModemManager - до переключения слота: иначе после перезагрузки модема он
# начинает его опрашивать, и uqmi -m потом висит на cdc-wdm
msg "останавливаю ModemManager"
PREPARED=1
trap restore EXIT
@ -132,6 +148,9 @@ prepare() {
killall mbim-proxy uqmi 2>/dev/null
sleep 2
ensure_esim_slot
wait_wdm
mkdir -p "$WRAP_DIR"
printf '#!/bin/sh\nexec %s -m "$@"\n' "$UQMI_BIN" > "$WRAP_DIR/uqmi"
chmod +x "$WRAP_DIR/uqmi"
@ -144,7 +163,10 @@ prepare() {
}
run_lpac() {
lpac "$@" > "$LOG" 2>&1
$LTO lpac "$@" > "$LOG" 2>&1
rc=$?
# 124 - timeout (coreutils), 143 - TERM (busybox)
case $rc in 124 | 143) echo "lpac $1 $2: нет ответа от чипа за 90 с" >> "$LOG" ;; esac
tail -n1 "$LOG" | grep -q '"message":"success"'
}
@ -167,7 +189,7 @@ cache_list() {
}
notifications() {
lpac notification process -a -r >/dev/null 2>&1
$LTO lpac notification process -a -r >/dev/null 2>&1
}
profile_state() {
@ -208,6 +230,7 @@ cmd_at() {
}
cmd_info() {
msg "читаю данные чипа"
run_lpac chip info || die "чип не отвечает: $(lpac_err)"
save_cache chip
d="$(tail -n1 "$LOG")"
@ -217,6 +240,7 @@ cmd_info() {
}
cmd_list() {
msg "читаю список профилей"
run_lpac profile list || die "не удалось получить список: $(lpac_err)"
save_cache profiles
data="$(tail -n1 "$LOG")"
@ -283,7 +307,7 @@ cmd_add() {
[ -n "$cc" ] && set -- "$@" -c "$cc"
msg "скачиваю профиль, это может занять до минуты"
lpac "$@" 2>&1 | tee "$LOG" | while read -r line; do
$LTO_DL lpac "$@" 2>&1 | tee "$LOG" | while read -r line; do
step="$(echo "$line" | jsonfilter -e '@.payload.message' 2>/dev/null)"
[ -n "$step" ] && echo " $step"
done