From cc9f7f0d07034949373e089e2170b7a518cc7098 Mon Sep 17 00:00:00 2001 From: fujr Date: Thu, 24 Oct 2024 14:22:57 +0800 Subject: [PATCH] qmodem-hc:fix bugs,add translate,add uci track --- luci/luci-app-qmodem-hc/Makefile | 2 +- .../luasrc/model/cbi/qmodem_hc/modem_sim.lua | 17 +- luci/luci-app-qmodem-hc/po/zh-cn/modem_hc.po | 21 +++ .../luci-app-qmodem-hc/po/zh_Hans/modem_hc.po | 21 +++ .../root/etc/uci-defaults/luci-app-qmodem-hc | 17 ++ .../root/usr/share/qmodem/modem_sim.sh | 157 ++++++++++-------- 6 files changed, 162 insertions(+), 73 deletions(-) create mode 100644 luci/luci-app-qmodem-hc/root/etc/uci-defaults/luci-app-qmodem-hc diff --git a/luci/luci-app-qmodem-hc/Makefile b/luci/luci-app-qmodem-hc/Makefile index b4c2c35..e91d664 100644 --- a/luci/luci-app-qmodem-hc/Makefile +++ b/luci/luci-app-qmodem-hc/Makefile @@ -6,7 +6,7 @@ include $(TOPDIR)/rules.mk PKG_NAME:=luci-app-qmodem-hc LUCI_TITLE:=hc-g80 sim switch LUCI_PKGARCH:=all -PKG_VERSION:=2.1 +PKG_VERSION:=2.4 PKG_LICENSE:=GPLv3 PKG_LINCESE_FILES:=LICENSE PKG_MAINTAINER:=Tom diff --git a/luci/luci-app-qmodem-hc/luasrc/model/cbi/qmodem_hc/modem_sim.lua b/luci/luci-app-qmodem-hc/luasrc/model/cbi/qmodem_hc/modem_sim.lua index 92e3866..ab0d660 100644 --- a/luci/luci-app-qmodem-hc/luasrc/model/cbi/qmodem_hc/modem_sim.lua +++ b/luci/luci-app-qmodem-hc/luasrc/model/cbi/qmodem_hc/modem_sim.lua @@ -1,4 +1,5 @@ m = Map("qmodem_hc_sim", translate("SIM Settings")) +uci = require "luci.model.uci".cursor() s = m:section(NamedSection,"main","main", translate("SIM Settings")) s.anonymous = true s.addremove = false @@ -16,13 +17,19 @@ judge_time.default = 5 ping_dest = s:option(DynamicList, "ping_dest", translate("Ping Destination")) -o = s:option(Value, "wwan_ifname", translate("WWAN Interface")) -o.description = translate("Please enter the WWAN interface name") -o.template = "cbi/network_netlist" +o = s:option(ListValue, "modem_config", translate("WWAN Interface")) +uci:foreach("qmodem", "modem-device", + function(s) + if s then + o:value(s['.name']) + end + end +) +-- o.description = translate("Please enter the WWAN interface name") +-- o.template = "cbi/network_netlist" -- o.widget = "optional" -o.nocreate = true -o.default = "cpewan0" +o.nocreate = true m:section(SimpleSection).template = "qmodem_hc/modem_sim" diff --git a/luci/luci-app-qmodem-hc/po/zh-cn/modem_hc.po b/luci/luci-app-qmodem-hc/po/zh-cn/modem_hc.po index 3b84ed0..bb184dd 100644 --- a/luci/luci-app-qmodem-hc/po/zh-cn/modem_hc.po +++ b/luci/luci-app-qmodem-hc/po/zh-cn/modem_hc.po @@ -19,3 +19,24 @@ msgstr "SIM2 (远离电源)" msgid "Set SIM" msgstr "设置SIM卡" + +msgid "SIM Settings" +msgstr "SIM卡设置" + +msgid "SIM Auto Switch" +msgstr "SIM卡自动切换" + +msgid "Network Detect Interval" +msgstr "网络检测间隔" + +msgid "Network Down Judge Times" +msgstr "网络下判断次数" + +msgid "Ping Destination" +msgstr "Ping目标" + +msgid "modem_config" +msgstr "模块配置" + +msgid "SIM Config" +msgstr "SIM卡配置" diff --git a/luci/luci-app-qmodem-hc/po/zh_Hans/modem_hc.po b/luci/luci-app-qmodem-hc/po/zh_Hans/modem_hc.po index 3b84ed0..bb184dd 100644 --- a/luci/luci-app-qmodem-hc/po/zh_Hans/modem_hc.po +++ b/luci/luci-app-qmodem-hc/po/zh_Hans/modem_hc.po @@ -19,3 +19,24 @@ msgstr "SIM2 (远离电源)" msgid "Set SIM" msgstr "设置SIM卡" + +msgid "SIM Settings" +msgstr "SIM卡设置" + +msgid "SIM Auto Switch" +msgstr "SIM卡自动切换" + +msgid "Network Detect Interval" +msgstr "网络检测间隔" + +msgid "Network Down Judge Times" +msgstr "网络下判断次数" + +msgid "Ping Destination" +msgstr "Ping目标" + +msgid "modem_config" +msgstr "模块配置" + +msgid "SIM Config" +msgstr "SIM卡配置" diff --git a/luci/luci-app-qmodem-hc/root/etc/uci-defaults/luci-app-qmodem-hc b/luci/luci-app-qmodem-hc/root/etc/uci-defaults/luci-app-qmodem-hc new file mode 100644 index 0000000..820fbab --- /dev/null +++ b/luci/luci-app-qmodem-hc/root/etc/uci-defaults/luci-app-qmodem-hc @@ -0,0 +1,17 @@ +#!/bin/sh +# Copyright (C) 2024 Tom + +/etc/init.d/modeminit enable +/etc/init.d/modem enable + +uci -q batch <<-EOF >/dev/null + delete ucitrack.@qmodem[-1] + add ucitrack qmodem + set ucitrack.@qmodem[-1].init=qmodem_hc_sim + add ucitrack qmodem_hc_sim + set ucitrack.@qmodem_hc_sim[-1].init=qmodem_hc_sim + commit ucitrack +EOF + +rm -rf /tmp/luci-*cache +exit 0 diff --git a/luci/luci-app-qmodem-hc/root/usr/share/qmodem/modem_sim.sh b/luci/luci-app-qmodem-hc/root/usr/share/qmodem/modem_sim.sh index 19175c5..ee3513b 100755 --- a/luci/luci-app-qmodem-hc/root/usr/share/qmodem/modem_sim.sh +++ b/luci/luci-app-qmodem-hc/root/usr/share/qmodem/modem_sim.sh @@ -1,38 +1,40 @@ #!/bin/sh +. /lib/functions.sh +. /usr/share/qmodem/modem_util.sh sim_gpio="/sys/class/gpio/sim/value" modem_gpio="/sys/class/gpio/4g/value" -ping_dest=$(uci -q get qmodem_hc_sim.main.ping_dest) -wwan_ifname=$(uci -q get qmodem_hc_sim.main.wwan_ifname) -[ -n $wwan_ifname ] && modem_config=$(uci -q get network.$wwan_ifname.modem_config) -is_empty=$(uci -q get qmodem.$modem_config) -[ -z $is_empty ] && unset modem_config -[ -z "$modem_config" ] && get_first_avalible_config -netdev=$(ifstatus $wwan_ifname | jq -r .device) -judge_time=$(uci -q get qmodem_hc_sim.main.judge_time) -detect_interval=$(uci -q get qmodem_hc_sim.main.detect_interval) -[ -z $detect_interval ] && detect_interval=10 -[ -z $judge_time ] && judge_time=5 +debug=0 +debug_log() +{ + [ "$debug" -eq 1 ] && echo $1 +} +# get detect config +load_detect_config() +{ + config_load qmodem_hc_sim + config_get ping_dest main ping_dest + config_get judge_time main judge_time 5 + config_get detect_interval main detect_interval 10 + config_get modem_config main modem_config + [ -z "$modem_config" ] && get_first_avalible_config + debug_log "ping_dest:$ping_dest" + debug_log "judge_time:$judge_time" + debug_log "detect_interval:$detect_interval" + debug_log "modem_config:$modem_config" +} -set_modem_config() +_enabled_config() { cfg=$1 - [ -n "$modem_config" ] && return - config_get state $1 state + local state + config_get state $cfg state [ -n "$state" ] && [ "$state" != "disabled" ] && modem_config=$cfg } get_first_avalible_config() { - . /lib/functions.sh config_load qmodem - config_foreach set_modem_config modem-device -} - -sendat() -{ - tty=$1 - cmd=$2 - sms_tool_q -d $tty at $cmd 2>&1 + config_foreach _enabled_config modem-device } reboot_modem() { @@ -44,7 +46,7 @@ reboot_modem() { switch_sim() { if [ -f $sim_gpio ]; then sim_status=$(cat $sim_gpio) - if [ $sim_status -eq 0 ]; then + if [ "$sim_status" -eq 0 ]; then echo 1 > $sim_gpio else echo 0 > $sim_gpio @@ -54,6 +56,21 @@ switch_sim() { fi } +_get_netdev() { + local modemconfig + config_load modemconfig $1 modem_config + [ "$modemconfig" != "$target_modemconfig" ] && return 1 + config_get netdev $1 ifname +} + +get_associa_netdev() { + config_load network + target_modemconfig=$1 + config_foreach _get_netdev interface + unset target_modemconfig +} + + ping_monitor() { #ping_dest为空则不进行ping检测 ,如果有多个,用空格隔开 has_success=0 @@ -66,6 +83,18 @@ ping_monitor() { return 0 } +at_sim_monitor() { + ttydev=$1 + #检查sim卡状态,有sim卡则返回1 + expect="+CPIN: READY" + result=$(at $ttydev "AT+CPIN?" | grep -o "$expect") + debug_log $result + if [ -n "$result" ]; then + return 1 + fi + return 0 +} + at_dial_monitor() { ttydev=$1 define_connect=$2 @@ -73,7 +102,8 @@ at_dial_monitor() { at_cmd="AT+CGPADDR=1" [ "$define_connect" == "3" ] && at_cmd="AT+CGPADDR=3" expect="+CGPADDR:" - result=$(sendat $ttydev $at_cmd | grep "$expect") + result=$(at $ttydev $at_cmd | grep "$expect") + debug_log $result if [ -n "$result" ];then ipv6=$(echo $result | grep -oE "\b([0-9a-fA-F]{0,4}:){2,7}[0-9a-fA-F]{0,4}\b") ipv4=$(echo $result | grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b") @@ -89,61 +119,53 @@ at_dial_monitor() { return 0 } -at_sim_monitor() { - ttydev=$1 - #检查sim卡状态,有sim卡则返回1 - expect="+CPIN: READY" - result=$(sendat $ttydev "AT+CPIN?" | grep -o "$expect") - if [ -n "$result" ]; then - return 1 - fi - return 0 -} - precheck() { + config_load qmodem modem_config=$1 - config=$(uci -q show qmodem.$modem_config) - [ -z "$config" ] && return 1 - ttydev=$(uci -q get qmodem.$modem_config.at_port) - enable_dial=$(uci -q get qmodem.$modem_config.enable_dial) - global_en=$(uci -q get qmodem.global.enable_dial) + config_get state $modem_config state + # is empty or is disabled + config_get at_port $modem_config at_port + config_get enable_dial $modem_config enable_dial 0 + config_get define_connect $modem_config define_connect 1 + config_get global_en main enable_dial 0 + debug_log "state:$state" + debug_log "at_port:$at_port" + debug_log "enable_dial:$enable_dial" + debug_log "define_connect:$define_connect" + debug_log "global_en:$global_en" + [ -z "$state" ] || [ "$state" == "disabled" ] && return 1 [ "$global_en" == "0" ] && return 1 [ -z "$enable_dial" ] || [ "$enable_dial" == "0" ] && return 1 - [ -z "$ttydev" ] && return 1 - [ ! -e "$ttydev" ] && return 1 + [ -z "$at_port" ] && return 1 + [ ! -e "$at_port" ] && return 1 return 0 - - } -fail_times=0 -main_monitor() { - - while true; do - #检测前置条件:1.tty存在 2.配置信息存在 3.拨号功能已开启 +main_loop() +{ + while true;do precheck $modem_config - if [ $? -eq 1 ]; then + if [ $? -eq 1 ];then sleep $detect_interval continue fi - #检查ping状态,超过judge_time则切卡 + get_associa_netdev $modem_config + if [ -n "$ping_dest" ]; then - ping_monitor - ping_result=$? + ping_monitor + ping_result=$? fi - - [ -z $ttydev ] && ttydev=$(uci -q get qmodem.$modem_config.at_port) - [ -z $define_connect ] && define_connect=$(uci -q get qmodem.$modem_config.define_connect) - if [ -n $define_connect ] && [ -n $ttydev ];then - at_dial_monitor $ttydev $define_connect - dial_result=$? + if [ -n "$at_port" ] && [ -n "$define_connect" ];then + at_dial_monitor $at_port $define_connect + dial_result=$? fi - if [ -n $ttydev ];then - at_sim_monitor $ttydev; + if [ -n "$at_port" ]; then + at_sim_monitor $at_port sim_result=$? fi - + + debug_log "ping_result:$ping_result dial_result:$dial_result sim_result:$sim_result" if [ -n "$ping_dest" ];then #策略:ping成功则重置fail_times,否则fail_times累加 @@ -177,7 +199,8 @@ main_monitor() { sleep $detect_interval done } - -sleep 180 - -main_monitor +if [ ! "$debug" -eq 1 ]; then + sleep 180 +fi +load_detect_config +main_loop