diff --git a/app/src/main/assets/eth.sh b/app/src/main/assets/eth.sh index d5be72d8..f213805b 100644 --- a/app/src/main/assets/eth.sh +++ b/app/src/main/assets/eth.sh @@ -27,6 +27,15 @@ cleanup() { } 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 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 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 /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 WAITED=0 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 echo "Interface is UP after $WAITED seconds" break diff --git a/app/src/main/assets/ethtool b/app/src/main/assets/ethtool new file mode 100644 index 00000000..c28aa430 Binary files /dev/null and b/app/src/main/assets/ethtool differ diff --git a/app/src/main/assets/ethtool-v7a b/app/src/main/assets/ethtool-v7a new file mode 100644 index 00000000..6a5ba4dc Binary files /dev/null and b/app/src/main/assets/ethtool-v7a differ diff --git a/app/src/main/java/com/xypower/mpapp/MicroPhotoService.java b/app/src/main/java/com/xypower/mpapp/MicroPhotoService.java index 3d96f234..07108a17 100644 --- a/app/src/main/java/com/xypower/mpapp/MicroPhotoService.java +++ b/app/src/main/java/com/xypower/mpapp/MicroPhotoService.java @@ -270,14 +270,29 @@ public class MicroPhotoService extends Service { public void onCreate() { super.onCreate(); + Context context = getApplicationContext(); try { 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"); - - FilesUtils.copyAssetsFile(getApplicationContext(), "eth.sh", ethShellFile.getAbsolutePath()); + File ethToolFile = new File(filesDir, "ethtool"); + if (ethToolFile.exists()) { + 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(); for (Network nw : nws) { @@ -294,10 +309,14 @@ public class MicroPhotoService extends Service { mHander = new ServiceHandler(); - mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); + mNotificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE); mStateService = STATE_SERVICE.NOT_CONNECTED; - DeviceUtil.getPhoneState(this.getApplicationContext()); + try { + DeviceUtil.getPhoneState(context); + } catch (Exception ex) { + ex.printStackTrace(); + } mScreenaAtionReceiver = new ScreenActionReceiver(); @@ -335,7 +354,7 @@ public class MicroPhotoService extends Service { intentFilter.addAction(ACTION_STOP); 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); } - - /* - AlarmManager alarmManager = (AlarmManager) getApplicationContext().getSystemService(ALARM_SERVICE); - - while (true) { - AlarmManager.AlarmClockInfo aci = alarmManager.getNextAlarmClock(); - if (aci == null) { - break; - } + try { + enableGps(true); + requestPosition(); + } catch (Exception ex) { + ex.printStackTrace(); } - - */ - - enableGps(true); - requestPosition(); } @Override public void onDestroy() { diff --git a/common/src/main/java/com/xypower/common/FilesUtils.java b/common/src/main/java/com/xypower/common/FilesUtils.java index 1b890b09..45de33ce 100644 --- a/common/src/main/java/com/xypower/common/FilesUtils.java +++ b/common/src/main/java/com/xypower/common/FilesUtils.java @@ -3,6 +3,7 @@ package com.xypower.common; import android.content.Context; import android.content.res.AssetManager; import android.text.TextUtils; +import android.util.Log; import org.w3c.dom.Text; @@ -383,6 +384,16 @@ public class FilesUtils { 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); int len = -1; byte[] buffer = new byte[1024];