qmodem-hc:fix bugs,add translate,add uci track

This commit is contained in:
fujr 2024-10-24 14:22:57 +08:00
parent a91061ef2e
commit cc9f7f0d07
6 changed files with 162 additions and 73 deletions

View File

@ -6,7 +6,7 @@ include $(TOPDIR)/rules.mk
PKG_NAME:=luci-app-qmodem-hc PKG_NAME:=luci-app-qmodem-hc
LUCI_TITLE:=hc-g80 sim switch LUCI_TITLE:=hc-g80 sim switch
LUCI_PKGARCH:=all LUCI_PKGARCH:=all
PKG_VERSION:=2.1 PKG_VERSION:=2.4
PKG_LICENSE:=GPLv3 PKG_LICENSE:=GPLv3
PKG_LINCESE_FILES:=LICENSE PKG_LINCESE_FILES:=LICENSE
PKG_MAINTAINER:=Tom <fjrcn@outlook.com> PKG_MAINTAINER:=Tom <fjrcn@outlook.com>

View File

@ -1,4 +1,5 @@
m = Map("qmodem_hc_sim", translate("SIM Settings")) m = Map("qmodem_hc_sim", translate("SIM Settings"))
uci = require "luci.model.uci".cursor()
s = m:section(NamedSection,"main","main", translate("SIM Settings")) s = m:section(NamedSection,"main","main", translate("SIM Settings"))
s.anonymous = true s.anonymous = true
s.addremove = false s.addremove = false
@ -16,13 +17,19 @@ judge_time.default = 5
ping_dest = s:option(DynamicList, "ping_dest", translate("Ping Destination")) ping_dest = s:option(DynamicList, "ping_dest", translate("Ping Destination"))
o = s:option(Value, "wwan_ifname", translate("WWAN Interface")) o = s:option(ListValue, "modem_config", translate("WWAN Interface"))
o.description = translate("Please enter the WWAN interface name") uci:foreach("qmodem", "modem-device",
o.template = "cbi/network_netlist" 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.widget = "optional"
o.nocreate = true
o.default = "cpewan0" o.nocreate = true
m:section(SimpleSection).template = "qmodem_hc/modem_sim" m:section(SimpleSection).template = "qmodem_hc/modem_sim"

View File

@ -19,3 +19,24 @@ msgstr "SIM2 (远离电源)"
msgid "Set SIM" msgid "Set SIM"
msgstr "设置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卡配置"

View File

@ -19,3 +19,24 @@ msgstr "SIM2 (远离电源)"
msgid "Set SIM" msgid "Set SIM"
msgstr "设置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卡配置"

View File

@ -0,0 +1,17 @@
#!/bin/sh
# Copyright (C) 2024 Tom <fjrcn@outlook.com>
/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

View File

@ -1,38 +1,40 @@
#!/bin/sh #!/bin/sh
. /lib/functions.sh
. /usr/share/qmodem/modem_util.sh
sim_gpio="/sys/class/gpio/sim/value" sim_gpio="/sys/class/gpio/sim/value"
modem_gpio="/sys/class/gpio/4g/value" modem_gpio="/sys/class/gpio/4g/value"
ping_dest=$(uci -q get qmodem_hc_sim.main.ping_dest) debug=0
wwan_ifname=$(uci -q get qmodem_hc_sim.main.wwan_ifname) debug_log()
[ -n $wwan_ifname ] && modem_config=$(uci -q get network.$wwan_ifname.modem_config) {
is_empty=$(uci -q get qmodem.$modem_config) [ "$debug" -eq 1 ] && echo $1
[ -z $is_empty ] && unset modem_config }
[ -z "$modem_config" ] && get_first_avalible_config # get detect config
netdev=$(ifstatus $wwan_ifname | jq -r .device) load_detect_config()
judge_time=$(uci -q get qmodem_hc_sim.main.judge_time) {
detect_interval=$(uci -q get qmodem_hc_sim.main.detect_interval) config_load qmodem_hc_sim
[ -z $detect_interval ] && detect_interval=10 config_get ping_dest main ping_dest
[ -z $judge_time ] && judge_time=5 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 cfg=$1
[ -n "$modem_config" ] && return local state
config_get state $1 state config_get state $cfg state
[ -n "$state" ] && [ "$state" != "disabled" ] && modem_config=$cfg [ -n "$state" ] && [ "$state" != "disabled" ] && modem_config=$cfg
} }
get_first_avalible_config() get_first_avalible_config()
{ {
. /lib/functions.sh
config_load qmodem config_load qmodem
config_foreach set_modem_config modem-device config_foreach _enabled_config modem-device
}
sendat()
{
tty=$1
cmd=$2
sms_tool_q -d $tty at $cmd 2>&1
} }
reboot_modem() { reboot_modem() {
@ -44,7 +46,7 @@ reboot_modem() {
switch_sim() { switch_sim() {
if [ -f $sim_gpio ]; then if [ -f $sim_gpio ]; then
sim_status=$(cat $sim_gpio) sim_status=$(cat $sim_gpio)
if [ $sim_status -eq 0 ]; then if [ "$sim_status" -eq 0 ]; then
echo 1 > $sim_gpio echo 1 > $sim_gpio
else else
echo 0 > $sim_gpio echo 0 > $sim_gpio
@ -54,6 +56,21 @@ switch_sim() {
fi 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_monitor() {
#ping_dest为空则不进行ping检测 ,如果有多个,用空格隔开 #ping_dest为空则不进行ping检测 ,如果有多个,用空格隔开
has_success=0 has_success=0
@ -66,6 +83,18 @@ ping_monitor() {
return 0 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() { at_dial_monitor() {
ttydev=$1 ttydev=$1
define_connect=$2 define_connect=$2
@ -73,7 +102,8 @@ at_dial_monitor() {
at_cmd="AT+CGPADDR=1" at_cmd="AT+CGPADDR=1"
[ "$define_connect" == "3" ] && at_cmd="AT+CGPADDR=3" [ "$define_connect" == "3" ] && at_cmd="AT+CGPADDR=3"
expect="+CGPADDR:" expect="+CGPADDR:"
result=$(sendat $ttydev $at_cmd | grep "$expect") result=$(at $ttydev $at_cmd | grep "$expect")
debug_log $result
if [ -n "$result" ];then 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") 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") 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 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() precheck()
{ {
config_load qmodem
modem_config=$1 modem_config=$1
config=$(uci -q show qmodem.$modem_config) config_get state $modem_config state
[ -z "$config" ] && return 1 # is empty or is disabled
ttydev=$(uci -q get qmodem.$modem_config.at_port) config_get at_port $modem_config at_port
enable_dial=$(uci -q get qmodem.$modem_config.enable_dial) config_get enable_dial $modem_config enable_dial 0
global_en=$(uci -q get qmodem.global.enable_dial) 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 [ "$global_en" == "0" ] && return 1
[ -z "$enable_dial" ] || [ "$enable_dial" == "0" ] && return 1 [ -z "$enable_dial" ] || [ "$enable_dial" == "0" ] && return 1
[ -z "$ttydev" ] && return 1 [ -z "$at_port" ] && return 1
[ ! -e "$ttydev" ] && return 1 [ ! -e "$at_port" ] && return 1
return 0 return 0
} }
fail_times=0 main_loop()
main_monitor() { {
while true;do
while true; do
#检测前置条件1.tty存在 2.配置信息存在 3.拨号功能已开启
precheck $modem_config precheck $modem_config
if [ $? -eq 1 ]; then if [ $? -eq 1 ];then
sleep $detect_interval sleep $detect_interval
continue continue
fi fi
#检查ping状态超过judge_time则切卡 get_associa_netdev $modem_config
if [ -n "$ping_dest" ]; then if [ -n "$ping_dest" ]; then
ping_monitor ping_monitor
ping_result=$? ping_result=$?
fi fi
if [ -n "$at_port" ] && [ -n "$define_connect" ];then
[ -z $ttydev ] && ttydev=$(uci -q get qmodem.$modem_config.at_port) at_dial_monitor $at_port $define_connect
[ -z $define_connect ] && define_connect=$(uci -q get qmodem.$modem_config.define_connect) dial_result=$?
if [ -n $define_connect ] && [ -n $ttydev ];then
at_dial_monitor $ttydev $define_connect
dial_result=$?
fi fi
if [ -n $ttydev ];then if [ -n "$at_port" ]; then
at_sim_monitor $ttydev; at_sim_monitor $at_port
sim_result=$? sim_result=$?
fi fi
debug_log "ping_result:$ping_result dial_result:$dial_result sim_result:$sim_result"
if [ -n "$ping_dest" ];then if [ -n "$ping_dest" ];then
#策略ping成功则重置fail_times否则fail_times累加 #策略ping成功则重置fail_times否则fail_times累加
@ -177,7 +199,8 @@ main_monitor() {
sleep $detect_interval sleep $detect_interval
done done
} }
if [ ! "$debug" -eq 1 ]; then
sleep 180 sleep 180
fi
main_monitor load_detect_config
main_loop