优化以太网网络设置

lowmem
Matthew 3 weeks ago
parent 436058fefa
commit adb24119ef

@ -27,6 +27,15 @@ cleanup() {
} }
trap cleanup INT TERM trap cleanup INT TERM
SCRIPT_PATH="$0"
# 确保路径是绝对路径
case "$SCRIPT_PATH" in
/*) ;; # 已经是绝对路径
*) SCRIPT_PATH="$PWD/$SCRIPT_PATH" ;;
esac
SCRIPT_DIR=$(dirname "$SCRIPT_PATH")
echo "Script directory detected as: $SCRIPT_DIR"
# Only configure rp_filter for eth0 interface # Only configure rp_filter for eth0 interface
echo 0 > /proc/sys/net/ipv4/conf/eth0/rp_filter 2>/dev/null || true echo 0 > /proc/sys/net/ipv4/conf/eth0/rp_filter 2>/dev/null || true
@ -64,6 +73,24 @@ fi
/system/bin/ip route flush table $ROUTE_TABLE /system/bin/ip route flush table $ROUTE_TABLE
/system/bin/ip rule del to $ETH_NETWORK/$ETH_NETMASK 2>/dev/null || true /system/bin/ip rule del to $ETH_NETWORK/$ETH_NETMASK 2>/dev/null || true
# Configure physical layer with ethtool (while interface is DOWN)
if [ -x "$SCRIPT_DIR/ethtool" ]; then
echo "Using ethtool from script directory: $SCRIPT_DIR/ethtool"
"$SCRIPT_DIR/ethtool" -s eth0 speed 10 duplex full autoneg off
# 其次尝试使用原路径
elif [ -x "/data/data/com.xypower.mpapp/files/ethtool" ]; then
echo "Configuring eth0 to 10Mbps full duplex..."
/data/data/com.xypower.mpapp/files/ethtool -s eth0 speed 10 duplex full autoneg off
else
echo "Warning: ethtool not found, falling back to sysfs configuration" >&2
# Try sysfs configuration as fallback
if [ -f "/sys/class/net/eth0/speed" ]; then
echo "off" > /sys/class/net/eth0/autoneg 2>/dev/null || true
echo "10" > /sys/class/net/eth0/speed 2>/dev/null || true
echo "full" > /sys/class/net/eth0/duplex 2>/dev/null || true
fi
fi
# Configure IP address # Configure IP address
/system/bin/ip addr add $ETH_IP/$ETH_NETMASK broadcast $ETH_BROADCAST dev eth0 /system/bin/ip addr add $ETH_IP/$ETH_NETMASK broadcast $ETH_BROADCAST dev eth0
@ -73,7 +100,7 @@ fi
# Use loop to wait for interface UP instead of fixed sleep # Use loop to wait for interface UP instead of fixed sleep
WAITED=0 WAITED=0
while [ $WAITED -lt $MAX_UP_WAIT ]; do while [ $WAITED -lt $MAX_UP_WAIT ]; do
IF_STATUS=$(/system/bin/ip link show eth0 | grep -c "state UP") IF_STATUS=$(/system/bin/ip link show eth0 | grep -c ",UP")
if [ "$IF_STATUS" = "1" ]; then if [ "$IF_STATUS" = "1" ]; then
echo "Interface is UP after $WAITED seconds" echo "Interface is UP after $WAITED seconds"
break break

Binary file not shown.

Binary file not shown.

@ -270,14 +270,29 @@ public class MicroPhotoService extends Service {
public void onCreate() { public void onCreate() {
super.onCreate(); super.onCreate();
Context context = getApplicationContext();
try { try {
if (usingEthernet()) { if (usingEthernet()) {
try {
File filesDir = context.getFilesDir();
File ethShellFile = new File(filesDir, "eth.sh");
if (ethShellFile.exists()) {
ethShellFile.delete();
}
FilesUtils.copyAssetsFile(context, "eth.sh", ethShellFile.getAbsolutePath());
File ethShellFile = new File(getFilesDir(), "eth.sh"); File ethToolFile = new File(filesDir, "ethtool");
if (ethToolFile.exists()) {
FilesUtils.copyAssetsFile(getApplicationContext(), "eth.sh", ethShellFile.getAbsolutePath()); ethToolFile.delete();
}
String srcFileName = android.os.Process.is64Bit() ? "ethtool" : "ethtool-v7a";
FilesUtils.copyAssetsFile(context, srcFileName, ethToolFile.getAbsolutePath());
ethToolFile.setExecutable(true);
} catch (Exception ex) {
ex.printStackTrace();
}
mConnectivityManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); mConnectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
Network[] nws = mConnectivityManager.getAllNetworks(); Network[] nws = mConnectivityManager.getAllNetworks();
for (Network nw : nws) { for (Network nw : nws) {
@ -294,10 +309,14 @@ public class MicroPhotoService extends Service {
mHander = new ServiceHandler(); mHander = new ServiceHandler();
mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); mNotificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
mStateService = STATE_SERVICE.NOT_CONNECTED; mStateService = STATE_SERVICE.NOT_CONNECTED;
DeviceUtil.getPhoneState(this.getApplicationContext()); try {
DeviceUtil.getPhoneState(context);
} catch (Exception ex) {
ex.printStackTrace();
}
mScreenaAtionReceiver = new ScreenActionReceiver(); mScreenaAtionReceiver = new ScreenActionReceiver();
@ -335,7 +354,7 @@ public class MicroPhotoService extends Service {
intentFilter.addAction(ACTION_STOP); intentFilter.addAction(ACTION_STOP);
intentFilter.addAction(ACTION_UPDATE_CONFIGS); intentFilter.addAction(ACTION_UPDATE_CONFIGS);
LocalBroadcastManager.getInstance(getApplicationContext()).registerReceiver (mLocalMsgReceiver, intentFilter); LocalBroadcastManager.getInstance(context).registerReceiver (mLocalMsgReceiver, intentFilter);
} }
{ {
@ -354,21 +373,12 @@ public class MicroPhotoService extends Service {
getApplicationContext().registerReceiver(mHeartBeatReceiver, filter); getApplicationContext().registerReceiver(mHeartBeatReceiver, filter);
} }
try {
/* enableGps(true);
AlarmManager alarmManager = (AlarmManager) getApplicationContext().getSystemService(ALARM_SERVICE); requestPosition();
} catch (Exception ex) {
while (true) { ex.printStackTrace();
AlarmManager.AlarmClockInfo aci = alarmManager.getNextAlarmClock();
if (aci == null) {
break;
}
} }
*/
enableGps(true);
requestPosition();
} }
@Override @Override
public void onDestroy() { public void onDestroy() {

@ -3,6 +3,7 @@ package com.xypower.common;
import android.content.Context; import android.content.Context;
import android.content.res.AssetManager; import android.content.res.AssetManager;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log;
import org.w3c.dom.Text; import org.w3c.dom.Text;
@ -383,6 +384,16 @@ public class FilesUtils {
file.delete(); file.delete();
} }
File parentDir = file.getParentFile();
if (parentDir != null && !parentDir.exists()) {
parentDir.mkdirs();
}
if (parentDir != null && !parentDir.canWrite()) {
Log.e("FilesUtils", "No write permission to directory: " + parentDir.getAbsolutePath());
return;
}
fos = new FileOutputStream(file); fos = new FileOutputStream(file);
int len = -1; int len = -1;
byte[] buffer = new byte[1024]; byte[] buffer = new byte[1024];

Loading…
Cancel
Save