android wifi ethernet coexistence - sharmasadhna/mylearnings GitHub Wiki

udhcpc == Micro-dhcp-client for embedded systems, handle all options on commandline, and call external scripts for interface configurations

See this example for similar case: https://www.raspberrypi.org/forums/viewtopic.php?t=278033

To enable Wifi, ethernet coexistence, make ethernet default network score lower than wifi. This makes wifi, the default network interface, then we enable eth0 using routing table and ip rules.

frameworks/opt/net/ethernet/java/com/android/server/ethernet/EthernetNetworkFactory.java

====make ethernet default network score lower than wifi, and call try_eth_dual.cfg

frameworks/base/*

a/services/core/java/com/android/server/NetworkManagementService.java

b/services/core/java/com/android/server/ConnectivityService.java

-== Call script for ethernet routing table, based on if interface is up=try_eth_dual.cfg and down=try_remove_eth_dual.cfg==

**try_eth_dual.cfg **

#create the config file with subnet=auto if it does not exists # i.e 1st time when ethernet is plugged in, for next times, we write into this file from Settings application STATIC_IP and SUBNET

if [ ! -e /data/user_de/0/com.android.settings/files/eth_dual.cfg ]; then

    `echo subnet=auto >  /data/user_de/0/com.android.settings/files/eth_dual.cfg`

    `chown system /data/user_de/0/com.android.settings/files/eth_dual.cfg`

    `chgrp system /data/user_de/0/com.android.settings/files/eth_dual.cfg`

fi

STATIC_IP=$(cat /data/user_de/0/com.android.settings/files/eth_dual.cfg | grep static_ip | head -n1 | egrep -o [0-9]?[0-9]?[0-9]\.[0-9]?[0-9]?[0-9]\.[0-9]?[0-9]?[0-9]\.[0-9]?[0-9]?[0-9]\/[0-9]?[0-9])

SUBNET=$(cat /data/user_de/0/com.android.settings/files/eth_dual.cfg | grep subnet | head -n1 | egrep -o [0-9]?[0-9]?[0-9]\.[0-9]?[0-9]?[0-9]\.[0-9]?[0-9]?[0-9]\.[0-9]?[0-9]?[0-9]\/[0-9]?[0-9])

#If the interface is down (i.e nothing is plugged), do not set anything and exit immediately.

if [ ! -z $(ip link | grep eth0 | grep -o DOWN) ]; then

    `echo "try_eth_dual.sh called while eth0 interface is down, exiting."`

    `exit;`

fi

if [ -z $SUBNET ]; then #1st time, or DHCP from Settings, create IP rules and table for DHCP IP,

    `#if subnet=auto, both the subnetwork and the ip are defined by dhcp so static_ip field is ignored`

    `cat /data/user_de/0/com.android.settings/files/eth_dual.cfg | egrep ^subnet=auto$ && udhcpc -i eth0 -s /system/bin/udhcpc_full.script && exit`

    `echo "no_dual"`

    `exit`

else

    `echo "dual"`

fi

if [ -z $STATIC_IP ]; then

    `udhcpc -i eth0 -s /system/bin/udhcpc.script`       #/system/bin/udhcpc.script just do = ip address add $ip/$mask dev eth0 

else #STATIC_IP was set in Settings application, and thus in eth_dual.cfg file,

    `ip address add $STATIC_IP dev eth0`

fi

if [ -n $SUBNET ]; then #STATIC_IP was set in Settings application, and thus in eth_dual.cfg file,

    `while ip rule delete from 0/0 to 0/0 table 11 2>/dev/null; do true; done`

    `ip route flush table 11`

    `ip rule add prio 4000 table 11`

    `ip route add $SUBNET dev eth0 table 11`

fi

udhcpc.script

ip address add $ip/$mask dev eth0

udhcpc_full.script

while ip rule delete from 0/0 to 0/0 table 11 2>/dev/null; do true; done

ip address add $ip/$mask dev eth0

ip route flush table 11

ip rule add prio 4000 table 11

ip_1=$(echo $ip | cut -d '.' -f 1)

ip_2=$(echo $ip | cut -d '.' -f 2)

ip_3=$(echo $ip | cut -d '.' -f 3)

ip_4=$(echo $ip | cut -d '.' -f 4)

subnet_1=$(echo $subnet | cut -d '.' -f 1)

subnet_2=$(echo $subnet | cut -d '.' -f 2)

subnet_3=$(echo $subnet | cut -d '.' -f 3)

subnet_4=$(echo $subnet | cut -d '.' -f 4)

ip_1=$(expr $ip_1 \& $subnet_1)

ip_2=$(expr $ip_2 \& $subnet_2)

ip_3=$(expr $ip_3 \& $subnet_3)

ip_4=$(expr $ip_4 \& $subnet_4)

ip route add $ip_1.$ip_2.$ip_3.$ip_4/$mask dev eth0 table 11