Android系统中如何添加USB网络共享

Android系统中如何添加USB网络共享

类别

需求

索引类别

USB网络共享

问题描述

平台是RT1296,在Android系统中已经有支持USB网络共享,但相应的产品系统中还未开启USB网络共享,那么这个时候就需要添加USB网络共享的一些配置,使得产品支持此功能。(以下是个人的一些理解,如有错误,请各位大佬多多指教)

代码关联

对应的代码修改部分如下:

①对应内核中添加配置(系统中添加USB网络驱动)

@@ -837,3 +837,6 @@ CONFIG_ZRAM=y
 CONFIG_ZRAM_LZ4_COMPRESS=y
 CONFIG_ZSMALLOC=y
 # CONFIG_ZSMALLOC_STAT is not set
+CONFIG_USB_USBNET=y
+CONFIG_USB_IPHETH=y
+CONFIG_USB_NET_RNDIS_HOST=y

②在init.rc中增加两个针对usb0的配置服务,才能让framework配置得了usb0网卡(服务是是USB网络共享的核心)

@@ -134,3 +134,16 @@ on property:persist.iso.state=unmounted
#在开机启动的时候会发送一个bootcomplete广播,接收广播后启动scdetect、rootshellservice
 on property:dev.bootcomplete=1
     start scdetect
     start rootshellservice
#启动usb网络分配dhcp
+service dhcpcd_usb0 /system/bin/dhcpcd -ABDKL -f/system/etc/dhcpcd/dhcpcd.conf
+    class main
+    disabled
+    oneshot
+    seclabel u:r:dhcp:s0
#启动usb网络分配ip
+service iprenew_usb0 /system/bin/dhcpcd -n
+    class main
+    disabled
+    oneshot
+    seclabel u:r:dhcp:s0

③对应Android源码,frameworks中添加配置(这边主要是讲述网络是如何启动的,是选择那个网络)

/frameworks/base/core/res/res/values/config.xml 
     <bool name="config_enableMonitorInputSource">true</bool>
-    <string translatable="false" name="config_ethernet_iface_regex">eth\\d</string>
+    <string translatable="false" name="config_ethernet_iface_regex">eth\\d|usb\\d</string>

上面的作用是提供两种网络的方式,一种是以太网络,一种是走usb共享

    /frameworks/opt/net/ethernet/java/com/android/server/ethernet/EthernetNetworkFactory.java
     public synchronized void start(Context context, Handler target) {
        Log.i(TAG, "monitoring start");
        // The services we use.
        IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
        mNMService = INetworkManagementService.Stub.asInterface(b);
        mEthernetManager = (EthernetManager) context.getSystemService(Context.ETHERNET_SERVICE);

        //在这里就获取上面设定config_ethernet_iface_regex的值
        mIfaceMatch = context.getResources().getString(
                com.android.internal.R.string.config_ethernet_iface_regex);

        // Create and register our NetworkFactory.
        mFactory = new LocalNetworkFactory(NETWORK_TYPE, context, target.getLooper());
        mFactory.setCapabilityFilter(mNetworkCapabilities);
        mFactory.setScoreFilter(-1); // this set high when we have an iface
        mFactory.register();

        mContext = context;

        // Start tracking interface change events.
        mInterfaceObserver = new InterfaceObserver();
        try {
            mNMService.registerObserver(mInterfaceObserver);
        } catch (RemoteException e) {
            Log.e(TAG, "Could not register InterfaceObserver " + e);
        }

        // If an Ethernet interface is already connected, start tracking that.
        // Otherwise, the first Ethernet interface to appear will be tracked.
        try {
            final String[] ifaces = mNMService.listInterfaces();
            for (String iface : ifaces) {
                synchronized(this) {
                    //在这里判断是采用哪个网络
                    if (maybeTrackInterface(iface)) { 
                        // We have our interface. Track it.
                        // Note: if the interface already has link (e.g., if we
                        // crashed and got restarted while it was running),
                        // we need to fake a link up notification so we start
                        // configuring it. Since we're already holding the lock,
                        // any real link up/down notification will only arrive
                        // after we've done this.
                        if (mNMService.getInterfaceConfig(iface).hasFlag("running")) {
                            updateInterfaceState(iface, true);
                        }
                        break;
                    }
                }
            }
            *
            *
            *
            *
        }
    }

    //在这边可以选择是走那个网络是eth还是usb         
    private boolean maybeTrackInterface(String iface) {
        // If we don't already have an interface, and if this interface matches
        // our regex, start tracking it.
        if (!iface.matches(mIfaceMatch) || isTrackingInterface())
            return false;

        if(UbusRpc.isOpenWrt() && !UbusRpc.isOttWifi()){
            if(!iface.equals("eth9"))
                return false;
        } else if(!iface.equals(SystemProperties.get("net.eth.iface", "eth0"))){
            return false;
        }

        Log.d(TAG, "Started tracking interface " + iface);
        setInterfaceUp(iface);
        return true;
    }

④最后在设置一个属性值:setprop net.eth.iface usb0/eth0 (usb0强制网络走手机USB网络共享)

应用层发送一个广播,这样就可以自由的切换是使用USB还是以太网。

 Intent intent = new Intent("rtk.intent.action.SET_ETH_IFACE");
 intent.putExtra("EXTRA_ETH_IFACE"," usb0/eth0 ");
 sendBroadcast(intent);

对应frameworks的接收应用发送的广播代码:

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

private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver(){
 @Override
 public void onReceive(Context context, Intent intent) {
 String action = intent.getAction();
 	if("rtk.intent.action.SET_ETH_IFACE".equals(action)){
 			String iface = intent.getStringExtra("EXTRA_ETH_IFACE");
	 		if(iface==null) iface="eth0";
 			SystemProperties.set("net.eth.iface", iface);
 			Log.i(TAG, "restart EthernetNetworkFactory");
 			enforceConnectivityInternalPermission();
 			synchronized (mIpConfiguration) {
 				mTracker.stop(); 
 				//重启网络,即调用第③步的start
 				mTracker.start(mContext, mHandler); 
			}
 		}
 	}
 };

如果没有效果,请排查一下/sys/class/net/中是否有新生成一个usb0的节点,如果没有生成的话排查第①第②步是否正常开启。

知识拓展

参考资料:https://blog.csdn.net/tankai19880619/article/details/44559419?_t=t

改进建议

若有错误请指正,悉心接受大佬的指点 谢谢 ! v

工作记录。。。。。

  • 7
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值