以太网启动之后主动设置ip和路由

lowmem
Matthew 1 month ago
parent 752b5fe318
commit a58e86b519

@ -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)

@ -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
{

@ -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;

@ -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,6 +1653,7 @@ public class MicroPhotoService extends Service {
public void setStaticNetwork(String iface, String ip, String netmask, String gateway)
{
if (!TextUtils.equals("0.0.0.0", ip)) {
if (false) {
Intent intent = new Intent();
intent.putExtra("cmd", "setnet");
intent.putExtra("staticip", true);
@ -1665,6 +1667,51 @@ public class MicroPhotoService extends Service {
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)

Loading…
Cancel
Save