优化以太网处理

lowmem
Matthew 3 weeks ago
parent 8612c66ed3
commit 03110ac486

@ -409,6 +409,7 @@ add_library( # Sets the name of the library.
${TERM_CORE_ROOT}/Client/UpgradeReceiver.cpp
${TERM_CORE_ROOT}/Client/Database.cpp
${TERM_CORE_ROOT}/Client/SimulatorDevice.cpp
# ${TERM_CORE_ROOT}/Client/SimulatorDevice.cpp
${TERM_CORE_ROOT}/Client/DataController.cpp
)

@ -553,6 +553,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");
mSetStaticIpMid = env->GetMethodID(classService, "setStaticNetwork", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;I)V");
mExecuteCmdMid = env->GetMethodID(classService, "executeCommand", "(Ljava/lang/String;)I");
mConvertDngToPngMid = env->GetMethodID(classService, "convertDngToPng", "(Ljava/lang/String;Ljava/lang/String;)V");
@ -1613,79 +1614,15 @@ bool CPhoneDevice::TakePhotoWithNetCamera(IDevice::PHOTO_INFO& localPhotoInfo, c
XYLOG(XYLOG_SEVERITY_DEBUG, "Ethernet Power ON");
std::shared_ptr<PowerControl> ethernetPowerCtrl = std::make_shared<EthernetPowerCtrl>(localPhotoInfo.closeDelayTime);
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++)
{
// XYLOG(XYLOG_SEVERITY_INFO, "Before GetEthnetHandle %d IMGID=%u", idx, localPhotoInfo.photoId);
netHandle = GetEthnetHandle();
// XYLOG(XYLOG_SEVERITY_INFO, "After GetEthnetHandle %d NetHandle=%llu IMGID=%u", idx, netHandle, localPhotoInfo.photoId);
if (netHandle != 0)
{
break;
}
std::this_thread::sleep_for(std::chrono::milliseconds(128));
}
if (netHandle == 0)
{
// timeout
m_ethernetFailures++;
XYLOG(XYLOG_SEVERITY_ERROR, "Ethernet Not Existing CH=%u PR=%X PHOTOID=%u EthFailures=%u",
(uint32_t)localPhotoInfo.channel, (uint32_t)localPhotoInfo.preset, localPhotoInfo.photoId, m_ethernetFailures);
TakePhotoCb(0, localPhotoInfo, "", 0);
// XYLOG(XYLOG_SEVERITY_INFO, "After TakePhotoCb %d IMGID=%u", localPhotoInfo.photoId);
if (m_ethernetFailures > 3)
{
time_t rebootTime = GetRebootTime();
if (ts - rebootTime > 1800)
{
XYLOG(XYLOG_SEVERITY_INFO, "Will Reboot Dev As for too much ehternet failure %d IMGID=%u", m_ethernetFailures, localPhotoInfo.photoId);
// XYLOG(XYLOG_SEVERITY_INFO, "Before Reboot %d IMGID=%u", localPhotoInfo.photoId);
Reboot(REBOOT_TYPE_DEVICE, true, "Ethernet Not Existing");
// XYLOG(XYLOG_SEVERITY_INFO, "After Reboot %d IMGID=%u", localPhotoInfo.photoId);
}
}
return false;
}
else
uint64_t startTime = GetMicroTimeStamp();
SetStaticIp();
uint64_t ethDuration = GetMicroTimeStamp() - startTime;
if (ethDuration < 5000)
{
#if 0
unsigned int ip = 0;
unsigned int netMask = 0;
unsigned int gateway = 0;
char buf[32] = { 0 };
XYLOG(XYLOG_SEVERITY_INFO, "Before GetNetInfo %d IMGID=%u", localPhotoInfo.photoId);
if (GetNetInfo("eth0", ip, netMask, gateway))
{
XYLOG(XYLOG_SEVERITY_INFO, "After GetNetInfo %d IMGID=%u", localPhotoInfo.photoId);
// const
sockaddr_in addrIn = { AF_INET, 0, ip};
inet_ntop(AF_INET, &addrIn.sin_addr, buf, sizeof(buf)); //其中recvAddr为SOCKADDR_IN类型
XYLOG(XYLOG_SEVERITY_INFO, "After ntop %d IMGID=%u", localPhotoInfo.photoId);
}
XYLOG(XYLOG_SEVERITY_INFO, "Ethernet is Available Handle=%llu IP=%s CH=%u PR=%X PHOTOID=%u", (uint64_t)netHandle, buf, (uint32_t)localPhotoInfo.channel, (uint32_t)localPhotoInfo.preset, localPhotoInfo.photoId);
#endif
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);
}
#endif // USING_N938
// if (mBuildTime < 1738166400)
{
// 2025-01-30
SetStaticIp();
std::this_thread::sleep_for(std::chrono::milliseconds(5000 - ethDuration));
}
std::this_thread::sleep_for(std::chrono::milliseconds(3000));
net_handle_t netHandle = GetEthnetHandle();
std::string ip = GetIpStr(localPhotoInfo.ip);
std::unique_ptr<VendorCtrl> vendorCtrl(MakeVendorCtrl(localPhotoInfo.vendor, localPhotoInfo.channel, ip, localPhotoInfo.userName, localPhotoInfo.password, netHandle, false));
@ -1694,7 +1631,6 @@ 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;
}
@ -1820,44 +1756,16 @@ bool CPhoneDevice::TakeVideoWithNetCamera(IDevice::PHOTO_INFO& localPhotoInfo, c
XYLOG(XYLOG_SEVERITY_DEBUG, "Ethernet Power ON");
std::shared_ptr<PowerControl> ethernetPowerCtrl = std::make_shared<EthernetPowerCtrl>(1);
// std::shared_ptr<PowerControl> ethernetPowerCtrl;
net_handle_t netHandle = 0;
#if ((!defined USING_N938) && (!defined USING_PTZ))
netHandle = GetEthnetHandle();
if (netHandle == 0)
uint64_t startTime = GetMicroTimeStamp();
SetStaticIp();
uint64_t ethDuration = GetMicroTimeStamp() - startTime;
if (ethDuration < 5000)
{
// Wait about 10s
for (int idx = 0; idx < 128; idx++)
{
std::this_thread::sleep_for(std::chrono::milliseconds(128));
netHandle = GetEthnetHandle();
if (netHandle != 0)
{
break;
}
}
std::this_thread::sleep_for(std::chrono::milliseconds(5000 - ethDuration));
}
if (netHandle == 0)
{
// timeout
XYLOG(XYLOG_SEVERITY_ERROR, "Ethernet not existing CH=%u PR=%X PHOTOID=%u", (uint32_t)localPhotoInfo.channel, (uint32_t)localPhotoInfo.preset, localPhotoInfo.photoId);
#ifdef NDEBUG
TakePhotoCb(0, localPhotoInfo, "", 0);
return false;
#endif
}
else
{
XYLOG(XYLOG_SEVERITY_INFO, "Ethernet is Available CH=%u PR=%X PHOTOID=%u", (uint32_t)localPhotoInfo.channel, (uint32_t)localPhotoInfo.preset, localPhotoInfo.photoId);
}
#endif
SetStaticIp();
std::this_thread::sleep_for(std::chrono::milliseconds(256));
net_handle_t netHandle = GetEthnetHandle();
std::string ip = GetIpStr(localPhotoInfo.ip);
std::unique_ptr<VendorCtrl> vendorCtrl(MakeVendorCtrl(localPhotoInfo.vendor, localPhotoInfo.channel, ip, localPhotoInfo.userName, localPhotoInfo.password, netHandle, false));
if (!vendorCtrl)
@ -1902,7 +1810,6 @@ bool CPhoneDevice::TakeVideoWithNetCamera(IDevice::PHOTO_INFO& localPhotoInfo, c
XYLOG(XYLOG_SEVERITY_DEBUG, "Stop Recording CH=%u PR=%X PHOTOID=%u", (uint32_t)localPhotoInfo.channel, (unsigned int)localPhotoInfo.preset, localPhotoInfo.photoId);
ethernetPowerCtrl.reset();
XYLOG(XYLOG_SEVERITY_DEBUG, "Ethernet Power OFF");
@ -1947,42 +1854,15 @@ bool CPhoneDevice::StartPushStreaming(IDevice::PHOTO_INFO& photoInfo, const std:
std::shared_ptr<PowerControl> ethernetPowerCtrl = std::make_shared<EthernetPowerCtrl>(1);
// std::shared_ptr<PowerControl> ethernetPowerCtrl;
net_handle_t netHandle = 0;
#if ((!defined USING_N938) && (!defined USING_PTZ))
netHandle = GetEthnetHandle();
if (netHandle == 0)
{
// Wait about 10s
for (int idx = 0; idx < 84; idx++)
{
std::this_thread::sleep_for(std::chrono::milliseconds(128));
netHandle = GetEthnetHandle();
if (netHandle != 0)
{
break;
}
}
}
if (netHandle == 0)
{
// timeout
XYLOG(XYLOG_SEVERITY_ERROR, "Ethernet not existing CH=%u PR=%X PHOTOID=%u", (uint32_t)photoInfo.channel, (uint32_t)photoInfo.preset, photoInfo.photoId);
#ifdef NDEBUG
TakePhotoCb(0, photoInfo, "", 0);
return false;
#endif
}
else
uint64_t startTime = GetMicroTimeStamp();
SetStaticIp();
uint64_t ethDuration = GetMicroTimeStamp() - startTime;
if (ethDuration < 5000)
{
XYLOG(XYLOG_SEVERITY_INFO, "Ethernet is Available CH=%u PR=%X PHOTOID=%u", (uint32_t)photoInfo.channel, (uint32_t)photoInfo.preset, photoInfo.photoId);
std::this_thread::sleep_for(std::chrono::milliseconds(5000 - ethDuration));
}
#endif
SetStaticIp();
std::this_thread::sleep_for(std::chrono::milliseconds(256));
net_handle_t netHandle = GetEthnetHandle();
std::map<uint8_t, STREAMING_CONTEXT>::iterator it = m_streamings.find(photoInfo.channel);
if (it != m_streamings.end())
{
@ -4402,10 +4282,12 @@ void CPhoneDevice::SetStaticIp(const std::string& iface, const std::string& ip,
jstring jip = env->NewStringUTF(ip.c_str());
#endif
jstring jnetmask = env->NewStringUTF(netmask.c_str());
std::string ipPrefix = "192.168.68.0";
jstring jipPrefix = env->NewStringUTF(ipPrefix.c_str());
jstring jgw = env->NewStringUTF(gateway.c_str());
env->CallVoidMethod(m_javaService, mSetStaticIpMid, jiface, jip, jnetmask, jgw);
env->CallVoidMethod(m_javaService, mSetStaticIpMid, jiface, jip, jgw, jipPrefix, 24);
env->DeleteLocalRef(jgw);
env->DeleteLocalRef(jnetmask);
env->DeleteLocalRef(jipPrefix);
env->DeleteLocalRef(jip);
env->DeleteLocalRef(jiface);
@ -5246,10 +5128,7 @@ void CPhoneDevice::SetStaticIp()
}
else
{
#ifdef USING_N938
SetStaticIp("eth0", "0.0.0.0", "255.255.255.0", "192.168.1.1");
#endif
XYLOG(XYLOG_SEVERITY_WARNING, "No Static IP Confg");
SetStaticIp("eth0", "192.168.68.91", "255.255.255.0", "192.168.68.91");
}
}

@ -1177,13 +1177,6 @@ public class MicroPhotoService extends Service {
if (lp != null) {
final String iface = lp.getInterfaceName();
new Thread(new Runnable() {
@Override
public void run() {
setStaticNetwork(iface, "192.168.68.91", "192.168.68.91", "192.168.68.0", 24);
}
}).start();
List<LinkAddress> addresses = lp.getLinkAddresses();
if (addresses != null && addresses.size() > 0) {
for (LinkAddress linkAddress : addresses) {
@ -1812,124 +1805,6 @@ public class MicroPhotoService extends Service {
return exitCode;
}
public void setStaticNetwork(final String iface, final String ip, final String netmask, final String gateway)
{
// if (getCustomAppId() == 2) {
// N938
new Thread(new Runnable() {
@Override
public void run() {
setStaticNetwork(iface, ip, gateway, "192.168.68.0", 24);
}
}).start();
// }
if (!TextUtils.equals("0.0.0.0", ip)) {
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);
}
int exitValue = -1;
boolean success = false;
try {
/*
Process process = Runtime.getRuntime().exec("/system/xbin/su root");
DataOutputStream os = new DataOutputStream(process.getOutputStream());
// os.writeBytes("/system/bin/ifconfig " + iface + " down\n");
// os.writeBytes("/system/bin/ifconfig " + iface + " " + ip + " netmask " + netmask + " up\n");
// os.writeBytes("/system/bin/ip route add default via " + gateway + " dev " + iface + "\n");
os.writeBytes("/system/bin/ip route delete 192.168.68.0/24 table eth0 2>/dev/null || true\n");
os.writeBytes("/system/bin/ip route add 192.168.68.0/24 dev eth0 proto static scope link table eth0\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 eth0 prio 1000");
os.writeBytes("/system/bin/ip route flush cache\n");
os.writeBytes("exit\n"); // 重要退出su shell
os.flush();
exitValue = process.waitFor();
if (exitValue != 0) {
}
*/
/*
String downCommand = "/system/xbin/su root ifconfig " + iface + " down";
Process downProcess = Runtime.getRuntime().exec(downCommand);
exitValue = downProcess.waitFor();
// 2. 配置IP地址和子网掩码
String ipCommand = "/system/xbin/su root ifconfig " + iface + " " + ip + " netmask " + netmask + " up";
Process ipProcess = Runtime.getRuntime().exec(ipCommand);
exitValue = ipProcess.waitFor();
String gwCommand = "/system/xbin/su root /system/bin/ip route add default via " + gateway + " dev " + iface;
Process gwProcess = Runtime.getRuntime().exec(gwCommand);
exitValue = gwProcess.waitFor();
// String routeCommand = "/system/xbin/su root /system/bin/ip route delete 192.168.68.0/24 table 20";
String routeCommand = "/system/xbin/su root /system/bin/ip route delete 192.168.68.0/24 table eth0";
Process routeProcess = Runtime.getRuntime().exec(routeCommand);
exitValue = routeProcess.waitFor();
// routeCommand = "/system/xbin/su root /system/bin/ip route add 192.168.68.0/24 dev eth0 proto static scope link table 20";
routeCommand = "/system/xbin/su root /system/bin/ip route add 192.168.68.0/24 dev eth0 proto static scope link table eth0";
Process routeProcess2 = Runtime.getRuntime().exec(routeCommand);
exitValue = routeProcess2.waitFor();
*/
/*
sleep(100);
String routeCommand = "/system/xbin/su root /system/bin/ip rule add from all to 192.168.68.0/24 lookup eth0 prio 1000";
for (int idx = 0; idx < 10; idx++) {
Process routeProcess3 = Runtime.getRuntime().exec(routeCommand);
exitValue = routeProcess3.waitFor();
if (exitValue == 0 || exitValue == 2) {
infoLog("Add route successfully");
break;
} else {
BufferedReader reader = new BufferedReader(new InputStreamReader(routeProcess3.getErrorStream()));
String line;
StringBuilder error = new StringBuilder();
while ((line = reader.readLine()) != null) {
error.append(line).append("\n");
}
if (error.length() > 0) {
Log.e("RouteConfig", "Error output: " + error);
}
}
sleep(500);
}
if (exitValue != 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 void setStaticNetwork(String iface, String ip, String gateway, String ipPrefix, int ipPrefixLength) {
int exitValue = -1;
boolean success = false;
@ -1943,8 +1818,6 @@ public class MicroPhotoService extends Service {
os.flush();
exitValue = process.waitFor();
if (exitValue != 0) {
}
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
String line = null;
@ -1954,7 +1827,11 @@ public class MicroPhotoService extends Service {
error.append("\n");
}
Log.e(TAG, error.toString());
if (exitValue == 0) {
infoLog("Add route successfully Code=" + exitValue);
} else {
infoLog(error.toString());
}
} else {
Process process = Runtime.getRuntime().exec("/system/xbin/su root");
DataOutputStream os = new DataOutputStream(process.getOutputStream());
@ -1964,7 +1841,7 @@ public class MicroPhotoService extends Service {
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/sleep 3\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");
@ -2038,73 +1915,7 @@ public class MicroPhotoService extends Service {
}
}
public void setEthernetRoute(String iface, String ipPrefix, int ipPrefixLength) {
int exitValue = -1;
try {
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");
// 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) {
}
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 reader = new BufferedReader(new InputStreamReader(routeProcess3.getErrorStream()));
String line;
StringBuilder error = new StringBuilder();
while ((line = reader.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);
}
if (exitValue != 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)
{
public int executeCommand(String cmd) {
int resCode = -1;
try {
Process downProcess = Runtime.getRuntime().exec(cmd);
@ -2115,8 +1926,7 @@ public class MicroPhotoService extends Service {
return resCode;
}
public static void sendBroadcast(Context context, Intent intent)
{
public static void sendBroadcast(Context context, Intent intent) {
intent.setAction("com.xy.xsetting.action");
intent.setPackage("com.android.systemui");
intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);

Loading…
Cancel
Save