From 12d039cbc5fdd17b89daa6611e1a941d4e5ca7e7 Mon Sep 17 00:00:00 2001 From: fujr Date: Sun, 19 May 2024 20:52:47 +0800 Subject: [PATCH] refactor: dial logic and modem detect logic --- luci-app-modem/Makefile | 6 +- luci-app-modem/luasrc/controller/modem.lua | 66 +++++++++++++++++++ .../luasrc/view/modem/modem_debug.htm | 63 ++++++++++++++++++ luci-app-modem/root/etc/config/modem | 1 + luci-app-modem/root/etc/init.d/modem_scan | 8 +-- .../root/usr/share/modem/modem_debug.sh | 8 +-- .../root/usr/share/modem/modem_dial.sh | 33 ++++++---- .../root/usr/share/modem/modem_util.sh | 19 +----- .../root/usr/share/modem/usb_modem_scan.sh | 4 +- 9 files changed, 165 insertions(+), 43 deletions(-) mode change 100644 => 100755 luci-app-modem/root/usr/share/modem/usb_modem_scan.sh diff --git a/luci-app-modem/Makefile b/luci-app-modem/Makefile index 4211048..482f00a 100644 --- a/luci-app-modem/Makefile +++ b/luci-app-modem/Makefile @@ -3,7 +3,7 @@ include $(TOPDIR)/rules.mk -PKG_NAME:=luci-app-modem +PKG_NAME:=luci-app-5gmodem LUCI_TITLE:=LuCI support for Modem LUCI_PKGARCH:=all PKG_VERSION:=1.4.4 @@ -27,13 +27,11 @@ LUCI_DEPENDS:=+luci-compat +kmod-usb-net +kmod-usb-net-cdc-ether +kmod-usb-acm \ +kmod-pcie_mhi \ +pciutils \ +quectel-CM-5G \ - +modemmanager \ - +luci-proto-modemmanager \ +sms-tool \ +jq +grep +bc\ -define Package/luci-app-modem/conffiles +define Package/luci-app-5gmodem/conffiles /etc/config/modem endef diff --git a/luci-app-modem/luasrc/controller/modem.lua b/luci-app-modem/luasrc/controller/modem.lua index 1525636..80f3bf2 100644 --- a/luci-app-modem/luasrc/controller/modem.lua +++ b/luci-app-modem/luasrc/controller/modem.lua @@ -43,6 +43,8 @@ function index() entry({"admin", "network", "modem", "send_at_command"}, call("sendATCommand"), nil).leaf = true entry({"admin", "network", "modem", "get_imei"}, call("getIMEI"), nil).leaf = true entry({"admin", "network", "modem", "set_imei"}, call("setIMEI"), nil).leaf = true + entry({"admin", "network", "modem", "get_sim"}, call("getSIM"), nil).leaf = true + entry({"admin", "network", "modem", "set_sim"}, call("setSIM"), nil).leaf = true -- entry({"admin", "network", "modem", "get_modem_debug_info"}, call("getModemDebugInfo"), nil).leaf = true --插件设置 @@ -723,6 +725,70 @@ function getIMEI() end +function getSimSlot(sim_path) + local sim_slot = fs.readfile(sim_path) + local current_slot = string.match(sim_slot, "%d") + if current_slot == "0" then + return "SIM2" + else + return "SIM1" + end +end + +function getNextBootSlot() + local fw_print_cmd = "fw_printenv -n sim2" + local nextboot_slot = shell(fw_print_cmd) + if nextboot_slot == "" then + return "SIM1" + else + return "SIM2" + end +end + +function writeJsonResponse(current_slot, nextboot_slot) + local result_json = {} + result_json["current_slot"] = current_slot + result_json["nextboot_slot"] = nextboot_slot + luci.http.prepare_content("application/json") + luci.http.write_json(result_json) +end + +function getSIM() + local sim_path = "/sys/class/gpio/sim/value" + local current_slot = getSimSlot(sim_path) + local nextboot_slot = getNextBootSlot() + writeJsonResponse(current_slot, nextboot_slot) +end + +function setSIM() + local sim_gpio = "/sys/class/gpio/sim/value" + local modem_gpio = "/sys/class/gpio/4g/value" + local sim_slot = http.formvalue("slot") + local pre_detect = getSimSlot(sim_gpio) + + local reset_module = 1 + if pre_detect == sim_slot then + reset_module = 0 + end + if sim_slot == "SIM1" then + sysfs_cmd = "echo 1 >"..sim_gpio + fw_setenv_cmd = "fw_setenv sim2" + elseif sim_slot == "SIM2" then + sysfs_cmd = "echo 0 >"..sim_gpio + fw_setenv_cmd = "fw_setenv sim2 1" + end + shell(sysfs_cmd) + shell(fw_setenv_cmd) + if reset_module == 1 then + shell("echo 0 >"..modem_gpio) + os.execute("sleep 1") + shell("echo 1 >"..modem_gpio) + end + local current_slot = getSimSlot(sim_gpio) + local nextboot_slot = getNextBootSlot() + writeJsonResponse(current_slot, nextboot_slot) +end + function setIMEI() local at_port = http.formvalue("port") local imei = http.formvalue("imei") diff --git a/luci-app-modem/luasrc/view/modem/modem_debug.htm b/luci-app-modem/luasrc/view/modem/modem_debug.htm index 17eb548..e4bd1c2 100644 --- a/luci-app-modem/luasrc/view/modem/modem_debug.htm +++ b/luci-app-modem/luasrc/view/modem/modem_debug.htm @@ -791,9 +791,17 @@ else if (data_tab == "set_imei") { get_imei_info(); } + else if (data_tab == "set_sim" ){ + XHR.get('<%=luci.dispatcher.build_url("admin", "network", "modem", "get_sim")%>', { "port": at_port }, + function (x, data) { + set_sim_view(data); + } + ); + } } + // 定时触发更新AT串口 XHR.poll(5,'<%=luci.dispatcher.build_url("admin", "network", "modem", "get_at_port")%>', null, (function() @@ -1211,7 +1219,24 @@ } + function set_sim_view(slot){ + let sim_current_slot = slot["current_slot"]; + let sim_nextboot_slot = slot["nextboot_slot"]; + sim_current_view = document.getElementById("sim_slot_current"); + sim_nextboot_view = document.getElementById("sim_slot_nextboot"); + sim_current_view.innerHTML = sim_current_slot; + sim_nextboot_view.innerHTML = sim_nextboot_slot; + } + function set_sim(){ + select = document.getElementById("sim_slot_select"); + slot = select.value; + XHR.get('<%=luci.dispatcher.build_url("admin", "network", "modem", "set_sim")%>', {"slot": slot }, + function (x, data) { + set_sim_view(data); + } + ); + }
@@ -1351,6 +1376,7 @@
  • <%:LockBand Settings%>
  • <%:Lock Cell/Arfcn Settings%>
  • <%:Set IMEI%>
  • +
  • <%:Switch SIM%>
  • @@ -1670,6 +1696,43 @@
    + + +
    diff --git a/luci-app-modem/root/etc/config/modem b/luci-app-modem/root/etc/config/modem index 91832b9..8e32086 100644 --- a/luci-app-modem/root/etc/config/modem +++ b/luci-app-modem/root/etc/config/modem @@ -3,6 +3,7 @@ config global 'global' option enable_dial '1' option modem_number '0' option manual_configuration '0' + option ethernet "cpewan0" config custom-commands option description '****************通用****************' diff --git a/luci-app-modem/root/etc/init.d/modem_scan b/luci-app-modem/root/etc/init.d/modem_scan index 710d0be..0726a69 100755 --- a/luci-app-modem/root/etc/init.d/modem_scan +++ b/luci-app-modem/root/etc/init.d/modem_scan @@ -1,5 +1,5 @@ #!/bin/sh /etc/rc.common -START=90 +START=95 STOP=13 USE_PROCD=1 @@ -11,9 +11,9 @@ MODEM_RUN_CONFIG="${MODEM_RUNDIR}/config.cache" start_service() { mkdir -p $MODEM_RUNDIR - local scan=$(uci -q get modem.@global[0].manual_configuration) - echo "scan=$scan" - if [ "$scan" = "0" ]; then + local dontscan=$(uci -q get modem.@global[0].manual_configuration) + logger -t modem_scan "manual_configuration: $dontscan" + if [ "$dontscan" == "0" ]; then procd_open_instance "modem_scan_service" procd_set_param command /usr/share/modem/usb_modem_scan.sh procd_close_instance diff --git a/luci-app-modem/root/usr/share/modem/modem_debug.sh b/luci-app-modem/root/usr/share/modem/modem_debug.sh index 44b6b46..0a83129 100755 --- a/luci-app-modem/root/usr/share/modem/modem_debug.sh +++ b/luci-app-modem/root/usr/share/modem/modem_debug.sh @@ -64,14 +64,14 @@ get_quick_commands() } #拨号日志 -# $1:AT命令 +# $1:log mesg # $2:日志路径 dial_log() { - local at_command="$1" + local logmsg="$1" local path="$2" #打印日志 local update_time=$(date +"%Y-%m-%d %H:%M:%S") - echo "[${update_time}] Send AT command ${at_command} to modem" >> "${path}" -} \ No newline at end of file + echo "[${update_time}] ${logmsg} " >> "${path}" +} diff --git a/luci-app-modem/root/usr/share/modem/modem_dial.sh b/luci-app-modem/root/usr/share/modem/modem_dial.sh index d913abb..cb4e485 100755 --- a/luci-app-modem/root/usr/share/modem/modem_dial.sh +++ b/luci-app-modem/root/usr/share/modem/modem_dial.sh @@ -60,6 +60,7 @@ config_get auth $modem_config auth config_get at_port $modem_config at_port config_get manufacturer $modem_config manufacturer config_get platform $modem_config platform +config_get define_connect $modem_config define_connect modem_netcard=$(ls $(find $modem_path -name net |tail -1) | awk -F'/' '{print $NF}') interface_name=wwan_5g_$(echo $modem_config | grep -oE "[0-9]+") interface6_name=wwan6_5g_$(echo $modem_config | grep -oE "[0-9]+") @@ -79,11 +80,13 @@ check_ip() check_ip_command="AT+CGPADDR=1" ;; "lte") - check_ip_command="AT+CGPADDR=1" - ;; - "lte_mediatek") - check_ip_command="AT+CGPADDR=3" + if [ "$define_connect" = "3" ];then + check_ip_command="AT+CGPADDR=3" + else + check_ip_command="AT+CGPADDR=1" + fi ;; + esac ;; "fibocom") @@ -139,6 +142,8 @@ set_if() uci set network.${interface_name}.defaultroute='1' uci set network.${interface_name}.peerdns='0' uci set network.${interface_name}.metric='10' + uci add_list network.${interface_name}.dns='114.114.114.114' + uci add_list network.${interface_name}.dns='119.29.29.29' #添加或修改网络配置 @@ -147,8 +152,6 @@ set_if() uci set network.${interface6_name}.extendprefix='1' uci set network.${interface6_name}.ifname="@${interface_name}" uci set network.${interface6_name}.device="@${interface_name}" - uci commit network - local num=$(uci show firewall | grep "name='wan'" | wc -l) local wwan_num=$(uci -q get firewall.@zone[$num].network | grep -w "${interface_name}" | wc -l) @@ -161,6 +164,7 @@ set_if() fi uci commit network uci commit firewall + ifup ${interface_name} dial_log "create interface $interface_name" "$log_path" fi @@ -182,7 +186,7 @@ set_if() uci set network.${interface_name}.device="${set_modem_netcard}" uci commit network - /etc/init.d/network restart + ifup ${interface_name} dial_log "set interface $interface_name to $modem_netcard" "$log_path" fi } @@ -192,7 +196,6 @@ flush_if() uci delete network.${interface_name} uci delete network.${interface6_name} uci commit network - /etc/init.d/network restart dial_log "delete interface $interface_name" "$log_path" } @@ -311,8 +314,13 @@ at_dial() cgdcont_command="AT+CGDCONT=1,\"$pdp_type\",\"$apn\"" ;; "lte") - at_command="AT+QNETDEVCTL=1,3,1" - cgdcont_command="AT+CGDCONT=1,\"$pdp_type\",\"$apn\"" + if [ "$define_connect" = "3" ];then + at_command="AT+QNETDEVCTL=3,3,1" + cgdcont_command="AT+CGDCONT=3,\"$pdp_type\",\"$apn\"" + else + at_command="AT+QNETDEVCTL=1,3,1" + cgdcont_command="AT+CGDCONT=1,\"$pdp_type\",\"$apn\"" + fi ;; *) at_command="AT+QNETDEVCTL=1,3,1" @@ -352,7 +360,7 @@ ip_change_fm350() { dial_log "ip_change_fm350" "$log_path" at_command="AT+CGPADDR=3" - local ipv4_config=$(at ${at_port} ${at_command} | grep "+CGPADDR: " | awk -F',' '{print $2}' | sed 's/"//g') + local ipv4_config=$(at ${at_port} ${at_command} | cut -d, -f2 | grep -oE '[0-9]+.[0-9]+.[0-9]+.[0-9]+') local public_dns1_ipv4="223.5.5.5" local public_dns2_ipv4="119.29.29.29" local public_dns1_ipv6="2400:3200::1" @@ -388,7 +396,8 @@ ip_change_fm350() uci add_list network.${interface_name}.dns="${ipv4_dns1}" uci add_list network.${interface_name}.dns="${ipv4_dns2}" uci commit network - /etc/init.d/network restart + ifdown ${interface_name} + ifup ${interface_name} dial_log "set interface $interface_name to $ipv4_config" "$log_path" } diff --git a/luci-app-modem/root/usr/share/modem/modem_util.sh b/luci-app-modem/root/usr/share/modem/modem_util.sh index 711e60a..0e36048 100755 --- a/luci-app-modem/root/usr/share/modem/modem_util.sh +++ b/luci-app-modem/root/usr/share/modem/modem_util.sh @@ -725,24 +725,7 @@ enable_dial() # $1:网络设备 disable_dial() { - local network="$1" - - local i=0 - while true; do - #查看该网络设备的配置是否启用 - local modem_network=$(uci -q get modem.@dial-config[${i}].network) - [ -z "$modem_network" ] && break - if [ "$network" = "$modem_network" ]; then - local enable=$(uci -q get modem.@dial-config[${i}].enable) - if [ "$enable" = "1" ]; then - uci set modem.@dial-config[${i}].enable=0 - uci commit modem - service modem reload - break - fi - fi - i=$((i+1)) - done + /etc/init.d/modem_network restart } #设置模组串口 diff --git a/luci-app-modem/root/usr/share/modem/usb_modem_scan.sh b/luci-app-modem/root/usr/share/modem/usb_modem_scan.sh old mode 100644 new mode 100755 index 819d106..1a05483 --- a/luci-app-modem/root/usr/share/modem/usb_modem_scan.sh +++ b/luci-app-modem/root/usr/share/modem/usb_modem_scan.sh @@ -1,3 +1,5 @@ -sleep 10 +#!/bin/sh +sleep 15 +logger -t usb_modem_scan "Start to scan USB modem" source /usr/share/modem/modem_scan.sh modem_scan