diff --git a/app/src/main/cpp/GPIOControl.h b/app/src/main/cpp/GPIOControl.h index ceea8862..2a62dd55 100644 --- a/app/src/main/cpp/GPIOControl.h +++ b/app/src/main/cpp/GPIOControl.h @@ -60,6 +60,7 @@ #define CMD_SET_INIT_STATUS 401 #define CMD_SET_5V_PWR_ENABLE 517 +#define CMD_SET_NEW_OTG_STATE 507 #else // defined(USING_PTZ) diff --git a/app/src/main/cpp/PhoneDevice.cpp b/app/src/main/cpp/PhoneDevice.cpp index 1c0ccc36..d193a07d 100644 --- a/app/src/main/cpp/PhoneDevice.cpp +++ b/app/src/main/cpp/PhoneDevice.cpp @@ -547,6 +547,7 @@ CPhoneDevice::CPhoneDevice(JavaVM* vm, jobject service, const std::string& appPa mExecHdrplusMid = env->GetMethodID(classService, "execHdrplus", "(IILjava/lang/String;Ljava/lang/String;)I"); mSetStaticIpMid = env->GetMethodID(classService, "setStaticNetwork", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V"); + mExecuteCmdMid = env->GetMethodID(classService, "executeCommand", "(Ljava/lang/String;)I"); mConvertDngToPngMid = env->GetMethodID(classService, "convertDngToPng", "(Ljava/lang/String;Ljava/lang/String;)V"); @@ -1672,7 +1673,7 @@ bool CPhoneDevice::TakePhotoWithNetCamera(IDevice::PHOTO_INFO& localPhotoInfo, c XYLOG(XYLOG_SEVERITY_INFO, "Ethernet is Available Handle=%llu CH=%u PR=%X PHOTOID=%u", (uint64_t)netHandle, (uint32_t)localPhotoInfo.channel, (uint32_t)localPhotoInfo.preset, localPhotoInfo.photoId); } - if (mBuildTime < 1738166400) + // if (mBuildTime < 1738166400) { // 2025-01-30 SetStaticIp(); @@ -1687,6 +1688,7 @@ bool CPhoneDevice::TakePhotoWithNetCamera(IDevice::PHOTO_INFO& localPhotoInfo, c { XYLOG(XYLOG_SEVERITY_ERROR, "Vendor(%u) not Supported CH=%u PR=%X PHOTOID=%u", (uint32_t)localPhotoInfo.vendor, (uint32_t)localPhotoInfo.channel, (unsigned int)localPhotoInfo.preset, localPhotoInfo.photoId); TakePhotoCb(0, localPhotoInfo, "", 0); + ShutdownEthernet(); return false; } @@ -1736,6 +1738,7 @@ bool CPhoneDevice::TakePhotoWithNetCamera(IDevice::PHOTO_INFO& localPhotoInfo, c std::this_thread::sleep_for(std::chrono::milliseconds(1000)); } + ShutdownEthernet(); ethernetPowerCtrl.reset(); XYLOG(XYLOG_SEVERITY_DEBUG, "Ethernet Power OFF"); @@ -4349,6 +4352,21 @@ net_handle_t CPhoneDevice::GetEthnetHandle() const void CPhoneDevice::SetStaticIp(const std::string& iface, const std::string& ip, const std::string& netmask, const std::string& gateway) { +#if 0 + std::string argv = " root ifconfig "; + argv += iface; + argv += " down"; + int resCode = execl("/system/xbin/su", "root", "ifconfig", iface.c_str(), "down", NULL); + + argv = " root ifconfig "; + argv += iface; + argv += " " + ip; + argv += " netmask " + netmask; + argv += " up"; + resCode = execl("/system/xbin/su", argv.c_str(), NULL); + + // execv("/system/xbin/su", " root ifconfig ", "start", "-a", "android.intent.action.VIEW", "-d", "http://qq.com", "-n", "com.android.browser/.BrowserActivity", NULL); +#else JNIEnv* env = NULL; jboolean ret = JNI_FALSE; bool didAttachThread = false; @@ -4376,6 +4394,46 @@ void CPhoneDevice::SetStaticIp(const std::string& iface, const std::string& ip, { m_vm->DetachCurrentThread(); } +#endif +} + +int CPhoneDevice::ExecuteCommand(const std::string& cmd) +{ + JNIEnv* env = NULL; + jboolean ret = JNI_FALSE; + bool didAttachThread = false; + bool res = GetJniEnv(m_vm, &env, didAttachThread); + if (!res) + { + ALOGE("Failed to get JNI Env"); + } + + jstring jcmd = env->NewStringUTF(cmd.c_str()); + jint resCode = env->CallIntMethod(m_javaService, mExecuteCmdMid, jcmd); + env->DeleteLocalRef(jcmd); + + if (didAttachThread) + { + m_vm->DetachCurrentThread(); + } + return resCode; +} + +void CPhoneDevice::ShutdownEthernet() +{ + std::string cmd = "/system/xbin/su root ifconfig "; + cmd += m_network->iface; + cmd += " down"; + ExecuteCommand(cmd); + // const char* args[] = {"root", "ifconfig", m_network->iface.c_str(), "down", NULL}; + +#if 0 + int resCode = execv("/system/xbin/su", args); + if (resCode == 0) + { + int aa = 0; + } +#endif } void CPhoneDevice::ConvertDngToPng(const std::string& dngPath, const std::string& pngPath) @@ -5159,10 +5217,10 @@ void CPhoneDevice::SetStaticIp() ipStr = "0.0.0.0"; } } +#endif SetStaticIp(m_network->iface, ipStr, m_network->netmask, m_network->gateway); XYLOG(XYLOG_SEVERITY_INFO, "Set Static IP on %s: %s(%s)", m_network->iface.c_str(), m_network->ip.c_str(), ipStr.c_str()); -#endif } else { diff --git a/app/src/main/cpp/PhoneDevice.h b/app/src/main/cpp/PhoneDevice.h index a5b0eb2c..261ebe8b 100644 --- a/app/src/main/cpp/PhoneDevice.h +++ b/app/src/main/cpp/PhoneDevice.h @@ -362,6 +362,8 @@ protected: void SetStaticIp(const std::string& iface, const std::string& ip, const std::string& netmask, const std::string& gateway); void ConvertDngToPng(const std::string& dngPath, const std::string& pngPath); void SetStaticIp(); + void ShutdownEthernet(); + int ExecuteCommand(const std::string& cmd); static std::string BuildCaptureResultInfo(ACameraMetadata* result, uint32_t ldr, uint32_t duration, bool burst); @@ -395,6 +397,7 @@ protected: jmethodID mRequestPositionMid; jmethodID mExecHdrplusMid; jmethodID mSetStaticIpMid; + jmethodID mExecuteCmdMid; jmethodID mConvertDngToPngMid; diff --git a/app/src/main/java/com/xypower/mpapp/MicroPhotoService.java b/app/src/main/java/com/xypower/mpapp/MicroPhotoService.java index 850ffad7..2e9299d1 100644 --- a/app/src/main/java/com/xypower/mpapp/MicroPhotoService.java +++ b/app/src/main/java/com/xypower/mpapp/MicroPhotoService.java @@ -70,6 +70,7 @@ import com.xypower.mpapp.v2.Camera2VideoActivity; import com.xypower.mpapp.video.RawActivity; import java.io.BufferedReader; +import java.io.DataOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; @@ -1652,19 +1653,65 @@ public class MicroPhotoService extends Service { public void setStaticNetwork(String iface, String ip, String netmask, String gateway) { if (!TextUtils.equals("0.0.0.0", ip)) { - Intent intent = new Intent(); - intent.putExtra("cmd", "setnet"); - intent.putExtra("staticip", true); - intent.putExtra("iface", iface); - intent.putExtra("ip", ip); - intent.putExtra("netmask", netmask); - if (!TextUtils.isEmpty(gateway)) { - intent.putExtra("gateway", gateway); + if (false) { + Intent intent = new Intent(); + intent.putExtra("cmd", "setnet"); + intent.putExtra("staticip", true); + intent.putExtra("iface", iface); + intent.putExtra("ip", ip); + intent.putExtra("netmask", netmask); + if (!TextUtils.isEmpty(gateway)) { + intent.putExtra("gateway", gateway); + } + intent.putExtra("dns1", "0.0.0.0"); + intent.putExtra("dns2", "0.0.0.0"); + sendBroadcast(getApplicationContext(), intent); } - intent.putExtra("dns1", "0.0.0.0"); - intent.putExtra("dns2", "0.0.0.0"); - sendBroadcast(getApplicationContext(), intent); + + boolean success = false; + try { + + Process process = Runtime.getRuntime().exec("/system/xbin/su root"); + DataOutputStream os = new DataOutputStream(process.getOutputStream()); + + os.writeBytes("ifconfig " + iface + " down\n"); + os.writeBytes("ifconfig " + iface + " " + ip + " netmask " + netmask + " up\n"); + os.writeBytes("route add default gw " + gateway + "\n"); + os.writeBytes("/system/bin/ip route delete 192.168.68.0/24 table 20\n"); + os.writeBytes("/system/bin/ip route add 192.168.68.0/24 dev eth0 proto static scope link table 20\n"); + // os.writeBytes("echo 'nameserver 8.8.8.8' > /etc/resolv.conf\n"); + os.writeBytes("exit\n"); // 重要:退出su shell + os.flush(); + int exitValue = process.waitFor(); + + // Process downProcess = Runtime.getRuntime().exec("/system/xbin/su root ifconfig " + iface + " down"); + // downProcess.waitFor(); + + // 2. 配置IP地址和子网掩码 + // String ipCommand = "/system/xbin/su root ifconfig " + iface + " " + ip + " netmask " + netmask + " up"; + // Process ipProcess = Runtime.getRuntime().exec(ipCommand); + // if (ipProcess.waitFor() != 0) { + // } + + + // os.writeBytes("/system/bin/ip route delete 192.168.68.0/24 table 20\n"); + // os.writeBytes("/system/bin/ip route add 192.168.68.0/24 dev eth0 proto static scope link table 20\n"); + } catch (Exception e) { + Log.e(TAG, "Failed to set interface down: " + e.getMessage()); + } + } + } + + public int executeCommand(String cmd) + { + int resCode = -1; + try { + Process downProcess = Runtime.getRuntime().exec(cmd); + resCode = downProcess.waitFor(); + } catch (Exception ex) { + ex.printStackTrace(); } + return resCode; } public static void sendBroadcast(Context context, Intent intent)