RK:网络共存、Shell脚本
Platform: RK3288
OS: Android 5.1
Kernel: 3.10
1.1.需求:a.以太网、4G网络共存,以太网可使用内外网 b.自动切网络
1.2.调试命令:网络连接 adb shell dumpsys connectivity 查看网络优先等级
2.1.网络共存
--- a/frameworks/base/services/core/java/com/android/server/ConnectivityService.java +++ b/frameworks/base/services/core/java/com/android/server/ConnectivityService.java @@ -159,6 +159,8 @@ public class ConnectivityService extends IConnectivityManager.Stub private static final boolean SAMPLE_DBG = false; private static final boolean LOGD_RULES = false; + private static final boolean LOGD_BLOCKED_NETWORKINFO = true; + private static final boolean ENABLE_NETWORK_COEXIST = true; // TODO: create better separation between radio types and network types @@ -4039,7 +4041,12 @@ public class ConnectivityService extends IConnectivityManager.Stub loge("Dead network still had at least " + nr); break; } - nai.asyncChannel.disconnect(); + if (ENABLE_NETWORK_COEXIST) { + log("Gatsby gatsby Skip teardownUnneededNetwork: " + nai.name()); + } else { + nai.asyncChannel.disconnect(); + } + } private void handleLingerComplete(NetworkAgentInfo oldNetwork) {
2.2.网络优先等级
diff --git a/frameworks/opt/net/ethernet/java/com/android/server/ethernet/EthernetNetworkFactory.java b/frameworks/opt/net/ethernet/java/com/android/server/ethernet/EthernetNetworkFactory.java index ab9c16a..2670e15 100755 --- a/frameworks/opt/net/ethernet/java/com/android/server/ethernet/EthernetNetworkFactory.java +++ b/frameworks/opt/net/ethernet/java/com/android/server/ethernet/EthernetNetworkFactory.java @@ -82,7 +82,7 @@ import java.lang.Exception; class EthernetNetworkFactory { private static final String NETWORK_TYPE = "Ethernet"; private static final String TAG = "EthernetNetworkFactory"; - private static final int NETWORK_SCORE = 150; + private static final int NETWORK_SCORE = 30; private static final boolean DBG = true; private static final boolean VDBG = false;
3.1.network_control.sh 以太网检测脚本
#!/system/bin/sh #ECHO="/system/bin/log -t policy_route" ECHO=echo iface_wlan=wlan0 iface_ethernet=eth0 #脚本得到gataway #gataway =$(ip route show table eth0 | grep "default" | busybox awk '{print $3}') ##################################################### ## ## ## 思路 ## | 判断是否插入网线 ## | ## |有IP地址,ping DNS ok, 证明外网是通的 ## eth0 UP|有IP地址,ping 3 times DNS fail, 证明外网线断开,down/up eth0 ## |无IP地址,等待20s 获取IP地址,若还没有,可以上层出问题 down/up eth0 ## ## ## eth0 down 不考虑这种情况 ## ## ##################################################### count_succ=0 count_fail=0 dwup_times=0 while [ 1 ] do IF_DNUP=0 ip=0 iswireinsert=$(cat /sys/class/net/eth0/carrier) #$ECHO "#####################iswireinsert=$iswireinsert###############################" IF_DNUP=$(busybox ifconfig eth0 | grep "UP" | busybox awk '{print $1}') ip=$(busybox ifconfig eth0 | grep 'inet addr' | busybox sed 's/^.*addr://g' | busybox sed 's/Bcast.*//g') #$ECHO "#####################IF_DNUP=$IF_DNUP ip=$ip###############################" if [ "$iswireinsert" == "1" ]; then if [ "$IF_DNUP" == "UP" ]; then if [ "$ip" != "" ]; then #ping -c 3 -I $iface_ethernet 8.8.8.8 ping -c 3 -I $iface_ethernet www.baidu.com if [ "$?" == "0" ] ; then #count_succ=`busybox expr $count_succ + 1` #$ECHO "ping success $count_succ times" #$ECHO "ping success" count_fail=0 else count_fail=`busybox expr $count_fail + 1` #$ECHO "ping fail $count_fail times" if [ "$count_fail" -eq 3 ]; then busybox ifconfig eth0 down sleep 1 busybox ifconfig eth0 up sleep 15 dwup_times=`busybox expr $dwup_times + 1` count_fail=0 fi fi else #$ECHO "Link up, no ip address, wait for 15s" if [ "$ip" == "" ]; then #$ECHO "LinkDownUp to obain ip $ip" busybox ifconfig eth0 down sleep 1 busybox ifconfig eth0 up sleep 15 fi fi else $ECHO "eth0 not LinkUp or The wire is not connected" fi else $ECHO "The wire is not insert" fi #$ECHO "DownUp_times = $dwup_times" sleep 10 done exit 0
3.2.network_check.sh 网络切换机制
当 以太网能 ping通外网 情况下,关闭4G模块上网功能 4G模块信号图标不显示
当 以太网不能 ping通外网 ping 的通 内网 情况下 ,开启4G模块上网功能 4G模块信号图
#!/system/bin/sh #ECHO="/system/bin/log -t policy_route" ECHO=echo iface_wlan=wlan0 iface_ethernet=eth0 iface_ppp=ppp0 #脚本得到gataway #gataway =$(ip route show table eth0 | grep "default" | busybox awk '{print $3}') ##################################################### ## ## ## 思路 ## | 判断是否插入网线 ## | ## |有IP地址,ping DNS ok, 证明外网是通的 ## eth0 UP|有IP地址,ping 3 times DNS fail, 证明外网线断开,down/up eth0 ## |无IP地址,等待20s 获取IP地址,若还没有,可以上层出问题 down/up eth0 ## ## ## eth0 down 不考虑这种情况 ## ## ##################################################### count_succ=0 count_fail=0 dwup_times=0 while [ 1 ] do IF_DNUP=0 ip=0 iswireinsert=$(cat /sys/class/net/eth0/carrier) #$ECHO "#####################iswireinsert=$iswireinsert###############################" IF_DNUP=$(busybox ifconfig eth0 | grep "UP" | busybox awk '{print $1}') ip=$(busybox ifconfig eth0 | grep 'inet addr' | busybox sed 's/^.*addr://g' | busybox sed 's/Bcast.*//g') #$ECHO "#####################IF_DNUP=$IF_DNUP ip=$ip###############################" if [ "$iswireinsert" == "1" ]; then if [ "$IF_DNUP" == "UP" ]; then if [ "$ip" != "" ]; then ping -c 3 -I $iface_ethernet 8.8.8.8 #ping -c 3 -I $iface_ethernet www.baidu.com if [ "$?" == "0" ] ; then count_succ=`busybox expr $count_succ + 1` $ECHO "ping success $count_succ times" #$ECHO "ping success" if ping -c 1 -I $iface_ppp www.baidu.com > /dev/null; then echo "$iface_ppp Ping is successful." stop ril-daemon fi count_fail=0 else count_fail=`busybox expr $count_fail + 1` $ECHO "ping fail $count_fail times" if [ "$count_fail" -eq 1 ]; then stop ril-daemon sleep 1 start ril-daemon fi fi else #$ECHO "Link up, no ip address, wait for 15s" if [ "$ip" == "" ]; then $ECHO "LinkDownUp to obain ip $ip" #busybox ifconfig eth0 down #sleep 1 #busybox ifconfig eth0 up #sleep 15 fi fi else $ECHO "eth0 not LinkUp or The wire is not connected" fi else $ECHO "The wire is not insert" fi #$ECHO "DownUp_times = $dwup_times" sleep 10 done exit 0
4G保活
#!/system/bin/sh ECHO=echo iface_ppp0=ppp0 model_power_class="/sys/class/xh_custom/xh_custom_gpio/device/xh_3g_power" model_power_up=1 model_power_down=0 ##################################################### ## ## ## 思路 ## | 开机ping 外网DNS 2分钟轮询一次 轮询超过15次 模块重新上电 重新加载ril服务 重新加载ril服务超过24次则reboot机器 ## ##################################################### count_fail=0 ril_count=0 while [ 1 ] do #ping -c 3 -I $iface_ppp0 8.8.8.8 ppp0_exits=`netcfg | grep ppp0 | busybox awk '{print $1}'` if [ x$ppp0_exits == x$iface_ppp0 ];then ping -c 3 -I $iface_ppp0 www.baidu.com if [ "$?" == "0" ] ; then count_fail=0 ril_count=0 else count_fail=`busybox expr $count_fail + 1` $ECHO "ping fail $count_fail times" if [ "$ril_count" -eq 24 ]; then reboot fi if [ "$count_fail" -eq 30 ]; then $ECHO "$model_power_down" > "$model_power_class" sleep 5 $ECHO "$model_power_up" > "$model_power_class" sleep 5 busybox pkill -f /system/bin/pppd sleep 5 stop ril-daemon sleep 5 start ril-daemon sleep 180 ril_count=`busybox expr $ril_count + 1` $ECHO "ril_count start $ril_count times" count_fail=0 fi fi fi sleep 120 done exit 0