From 789547b5030f42fd33f4a5d4e5f9ffd8c7647e02 Mon Sep 17 00:00:00 2001 From: Matthew Date: Thu, 22 May 2025 18:36:10 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E4=BB=A5=E5=A4=AA=E7=BD=91?= =?UTF-8?q?=E7=BB=9C=E7=9A=84=E8=AE=BE=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/assets/eth.sh | 30 ++++ app/src/main/cpp/PhoneDevice.cpp | 6 +- .../com/xypower/mpapp/MicroPhotoService.java | 152 +++++++++++------- 3 files changed, 127 insertions(+), 61 deletions(-) create mode 100644 app/src/main/assets/eth.sh diff --git a/app/src/main/assets/eth.sh b/app/src/main/assets/eth.sh new file mode 100644 index 00000000..8a5b3f25 --- /dev/null +++ b/app/src/main/assets/eth.sh @@ -0,0 +1,30 @@ +#!/system/bin/sh + +/system/bin/ip link set eth0 down +/system/bin/ip addr flush dev eth0 +/system/bin/ip addr add 192.168.68.91/24 broadcast 192.168.68.255 dev eth0 +/system/bin/ip link set eth0 up +/system/bin/sleep 1 +/system/bin/ip route delete 192.168.68.0/24 table 20 +/system/bin/ip route add 192.168.68.0/24 dev eth0 proto static scope link table 20 +ADD_ROUTE_STATUS=$? + +if [ $ADD_ROUTE_STATUS -eq 0 ]; then + echo "路由添加命令成功执行" +else + echo "路由添加命令失败,状态码: $ADD_ROUTE_STATUS" + exit $ADD_ROUTE_STATUS +fi + +/system/bin/ip route flush cache +/system/bin/ip rule del to 192.168.68.0/24 +/system/bin/ip rule add from all to 192.168.68.0/24 lookup 20 prio 1000 +/system/bin/ip route flush cache + +if /system/bin/ip rule | grep -q "192.168.68.0/24"; then + echo "路由配置成功" + exit 0 +else + echo "路由配置失败" + exit 1 +fi diff --git a/app/src/main/cpp/PhoneDevice.cpp b/app/src/main/cpp/PhoneDevice.cpp index 2e1f76e5..f5d3b2c0 100644 --- a/app/src/main/cpp/PhoneDevice.cpp +++ b/app/src/main/cpp/PhoneDevice.cpp @@ -1613,10 +1613,10 @@ bool CPhoneDevice::TakePhotoWithNetCamera(IDevice::PHOTO_INFO& localPhotoInfo, c XYLOG(XYLOG_SEVERITY_DEBUG, "Ethernet Power ON"); std::shared_ptr ethernetPowerCtrl = std::make_shared(localPhotoInfo.closeDelayTime); - std::this_thread::sleep_for(std::chrono::milliseconds(5000)); + std::this_thread::sleep_for(std::chrono::milliseconds(2000)); net_handle_t netHandle = 0; -#if ((!defined USING_N938) && (!defined USING_PTZ)) +#if 0 // Wait about 10s for (int idx = 0; idx < 84; idx++) { @@ -1683,7 +1683,7 @@ bool CPhoneDevice::TakePhotoWithNetCamera(IDevice::PHOTO_INFO& localPhotoInfo, c // 2025-01-30 SetStaticIp(); } - std::this_thread::sleep_for(std::chrono::milliseconds(256)); + std::this_thread::sleep_for(std::chrono::milliseconds(3000)); std::string ip = GetIpStr(localPhotoInfo.ip); std::unique_ptr vendorCtrl(MakeVendorCtrl(localPhotoInfo.vendor, localPhotoInfo.channel, ip, localPhotoInfo.userName, localPhotoInfo.password, netHandle, false)); diff --git a/app/src/main/java/com/xypower/mpapp/MicroPhotoService.java b/app/src/main/java/com/xypower/mpapp/MicroPhotoService.java index aa598ce8..6619d621 100644 --- a/app/src/main/java/com/xypower/mpapp/MicroPhotoService.java +++ b/app/src/main/java/com/xypower/mpapp/MicroPhotoService.java @@ -271,6 +271,11 @@ public class MicroPhotoService extends Service { try { if (usingEthernet()) { + + File ethShellFile = new File(getFilesDir(), "eth.sh"); + + FilesUtils.copyAssetsFile(getApplicationContext(), "eth.sh", ethShellFile.getAbsolutePath()); + mConnectivityManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); Network[] nws = mConnectivityManager.getAllNetworks(); @@ -1174,11 +1179,7 @@ public class MicroPhotoService extends Service { new Thread(new Runnable() { @Override public void run() { - if (getCustomAppId() == 2 || getCustomAppId() == 1) { - setStaticNetwork(iface, "192.168.68.91", "192.168.68.91", "192.168.68.0", 24); - } else { - setEthernetRoute(iface, "192.168.68.0", 24); - } + setStaticNetwork(iface, "192.168.68.91", "192.168.68.91", "192.168.68.0", 24); } }).start(); @@ -1918,71 +1919,105 @@ public class MicroPhotoService extends Service { int exitValue = -1; boolean success = false; try { + File ethShellFile = new File(getFilesDir(), "eth.sh"); + if (ethShellFile.exists()) { + Process process = Runtime.getRuntime().exec("/system/xbin/su"); + DataOutputStream os = new DataOutputStream(process.getOutputStream()); + os.writeBytes("/system/bin/sh " + ethShellFile.getAbsolutePath() + "\n"); + os.writeBytes("exit\n"); // 重要:退出su shell - Process process = Runtime.getRuntime().exec("/system/xbin/su root"); - DataOutputStream os = new DataOutputStream(process.getOutputStream()); + os.flush(); + exitValue = process.waitFor(); + if (exitValue != 0) { + } - // os.writeBytes("/system/bin/ip link set eth0 down\n"); - os.writeBytes("/system/bin/ip addr add 192.168.68.91/24 broadcast 192.168.68.255 dev eth0\n"); - // os.writeBytes("/system/bin/ip link set eth0 up\n"); - // os.writeBytes("/system/bin/ip route delete 192.168.68.0/24 table 20 2>/dev/null || true\n"); - os.writeBytes("/system/bin/ip route replace 192.168.68.0/24 dev eth0 proto static scope link table 20\n"); - os.writeBytes("/system/bin/ip route flush cache\n"); - // os.writeBytes("echo 'nameserver 8.8.8.8' > /etc/resolv.conf\n"); - os.writeBytes("/system/bin/ip rule del to 192.168.68.0/24 2>/dev/null || true\n"); - os.writeBytes("/system/bin/ip rule add from all to 192.168.68.0/24 lookup 20 prio 1000\n"); - os.writeBytes("/system/bin/ip route flush cache\n"); - os.writeBytes("exit\n"); // 重要:退出su shell - os.flush(); - exitValue = process.waitFor(); - if (exitValue != 0) { - } + BufferedReader reader = new BufferedReader(new InputStreamReader(process.getErrorStream())); + String line = null; + StringBuilder error = new StringBuilder(); + while ((line = reader.readLine()) != null) { + error.append(line); + error.append("\n"); + } - BufferedReader reader = new BufferedReader(new InputStreamReader(process.getErrorStream())); - String line = null; - StringBuilder error = new StringBuilder(); - while ((line = reader.readLine()) != null) { - error.append(line); - error.append("\n"); - } + Log.e(TAG, error.toString()); + } else { + Process process = Runtime.getRuntime().exec("/system/xbin/su root"); + DataOutputStream os = new DataOutputStream(process.getOutputStream()); - Log.e(TAG, error.toString()); + os.writeBytes("/system/bin/ip link set eth0 down\n"); + os.writeBytes("/system/bin/sleep 1\n"); // Add delay + os.writeBytes("/system/bin/ip addr flush dev eth0\n"); // Clear existing config + os.writeBytes("/system/bin/ip addr add 192.168.68.91/24 broadcast 192.168.68.255 dev eth0\n"); + os.writeBytes("/system/bin/ip link set eth0 up\n"); + os.writeBytes("/system/bin/sleep 2\n"); + os.writeBytes("/system/bin/ip route delete 192.168.68.0/24 table 20 2>/dev/null || true\n"); + os.writeBytes("/system/bin/ip route add 192.168.68.0/24 dev eth0 proto static scope link table 20\n"); + os.writeBytes("/system/bin/ip route flush cache\n"); + // os.writeBytes("echo 'nameserver 8.8.8.8' > /etc/resolv.conf\n"); + os.writeBytes("/system/bin/ip rule del to 192.168.68.0/24 2>/dev/null || true\n"); + os.writeBytes("/system/bin/ip rule add from all to 192.168.68.0/24 lookup 20 prio 1000\n"); + os.writeBytes("/system/bin/ip route flush cache\n"); - sleep(100); - for (int idx = 0; idx < 10; idx++) { - Process routeProcess3 = Runtime.getRuntime().exec("/system/xbin/su root"); - DataOutputStream os3 = new DataOutputStream(routeProcess3.getOutputStream()); - // os3.writeBytes("/system/bin/ip rule del to 192.168.68.0/24 2>/dev/null || true\n"); - os3.writeBytes("/system/bin/ip rule add from all to 192.168.68.0/24 lookup 20 prio 1000\n"); - os3.writeBytes("CMD_EXIT_CODE=$?\n"); // 保存返回值 - os3.writeBytes("echo \"CMD_RESULT:$CMD_EXIT_CODE\"\n"); // 输出标记和返回值 - os3.writeBytes("/system/bin/ip route flush cache\n"); - os3.writeBytes("exit\n"); // 重要:退出su shell - os3.flush(); + // Verify routes were added + os.writeBytes("if ! /system/bin/ip rule | grep '192.168.68.0/24'; then\n"); + os.writeBytes(" echo 'Route rule failed to apply, retrying...'\n"); + os.writeBytes(" sleep 1\n"); + os.writeBytes(" /system/bin/ip rule add from all to 192.168.68.0/24 lookup 20 prio 1000\n"); + os.writeBytes(" /system/bin/ip route flush cache\n"); + os.writeBytes("fi\n"); - int commandExitCode = -1; - BufferedReader reader2 = new BufferedReader(new InputStreamReader(routeProcess3.getErrorStream())); - StringBuilder error2 = new StringBuilder(); - while ((line = reader2.readLine()) != null) { - if (line.startsWith("CMD_RESULT:")) { - commandExitCode = Integer.parseInt(line.substring(11)); - Log.d("RouteConfig", "Command exit code: " + commandExitCode); - break; - } + os.writeBytes("exit\n"); // 重要:退出su shell + os.flush(); + exitValue = process.waitFor(); + if (exitValue != 0) { } - exitValue = routeProcess3.waitFor(); - if (exitValue == 0 || commandExitCode == 2) { - infoLog("Add route successfully Code=" + exitValue); - break; - } else { + BufferedReader reader = new BufferedReader(new InputStreamReader(process.getErrorStream())); + String line = null; + StringBuilder error = new StringBuilder(); + while ((line = reader.readLine()) != null) { + error.append(line); + error.append("\n"); + } + + Log.e(TAG, error.toString()); + sleep(100); + for (int idx = 0; idx < 10; idx++) { + Process routeProcess3 = Runtime.getRuntime().exec("/system/xbin/su root"); + DataOutputStream os3 = new DataOutputStream(routeProcess3.getOutputStream()); + // os3.writeBytes("/system/bin/ip rule del to 192.168.68.0/24 2>/dev/null || true\n"); + os3.writeBytes("/system/bin/ip rule add from all to 192.168.68.0/24 lookup 20 prio 1000\n"); + os3.writeBytes("CMD_EXIT_CODE=$?\n"); // 保存返回值 + os3.writeBytes("echo \"CMD_RESULT:$CMD_EXIT_CODE\"\n"); // 输出标记和返回值 + os3.writeBytes("/system/bin/ip route flush cache\n"); + os3.writeBytes("exit\n"); // 重要:退出su shell + os3.flush(); + + int commandExitCode = -1; + BufferedReader reader2 = new BufferedReader(new InputStreamReader(routeProcess3.getErrorStream())); + StringBuilder error2 = new StringBuilder(); + while ((line = reader2.readLine()) != null) { + if (line.startsWith("CMD_RESULT:")) { + commandExitCode = Integer.parseInt(line.substring(11)); + Log.d("RouteConfig", "Command exit code: " + commandExitCode); + break; + } + } + + exitValue = routeProcess3.waitFor(); + if (exitValue == 0 || commandExitCode == 2) { + infoLog("Add route successfully Code=" + exitValue); + break; + } else { + + } + sleep(500); } - sleep(500); - } - if (exitValue != 0) { } + + } catch (Exception e) { Log.e(TAG, "Failed to set interface down: " + e.getMessage()); } @@ -1995,6 +2030,7 @@ public class MicroPhotoService extends Service { Process process = Runtime.getRuntime().exec("/system/xbin/su root"); DataOutputStream os = new DataOutputStream(process.getOutputStream()); + os.writeBytes("/system/bin/ip addr add 192.168.68.91/24 broadcast 192.168.68.255 dev eth0\n"); // os.writeBytes("/system/bin/ip route delete 192.168.68.0/24 table 20 2>/dev/null || true\n"); os.writeBytes("/system/bin/ip route replace 192.168.68.0/24 dev eth0 proto static scope link table 20\n"); os.writeBytes("/system/bin/ip route flush cache\n");