测试平台:MT2712,Android9.0,Linux4.9
使用场景:
Android车机,通过USB线连接T-box、手机、PC电脑,进而使用他们的网络上网。
其实Android系统已经支持USB共享网络,但相应的产品系统中还未开启USB共享网络,那么这个时候就需要添加USB网络共享的一些配置,使得产品支持此功能。
Android USB网络共享基于RNDIS实现。

一、代码修改:

1.内核添加RNDIS的支持

CONFIG_USB_USBNET=y
CONFIG_USB_NET_DM9601=y
CONFIG_USB_NET_SR9700=y
CONFIG_USB_NET_SMSC75XX=y
CONFIG_USB_NET_SMSC95XX=y
+CONFIG_USB_NET_RNDIS_HOST=y

具体的网卡名称:
路径:kernel-4.9/drivers/net/usb/usbnet.c

usbnet_probe (struct usb_interface *udev, const struct usb_device_id *prod)
{
.........................................
	dev->net = net;
	strcpy (net->name, "usb%d");
	printk("usbnet_probe usb");
	memcpy (net->dev_addr, node_id, sizeof node_id);
........................................
	// allow device-specific bind/init procedures
	// NOTE net->name still not usable ...
	if (info->bind) {
		// heuristic:  "usb%d" for links we know are two-host,
		// else "eth%d" when there's reasonable doubt.  userspace
		// can rename the link if it knows better.
		if ((dev->driver_info->flags & FLAG_ETHER) != 0 &&
		    ((dev->driver_info->flags & FLAG_POINTTOPOINT) == 0 ||
		     (net->dev_addr [0] & 0x02) == 0))
				strcpy (net->name, "eth%d");
		/* WLAN devices should always be named "wlan%d" */
		if ((dev->driver_info->flags & FLAG_WLAN) != 0)
			strcpy(net->name, "wlan%d");
		/* WWAN devices should always be named "wwan%d" */
		if ((dev->driver_info->flags & FLAG_WWAN) != 0)
			strcpy(net->name, "wwan%d");
		}
	}

网卡名称默认为usbx,如果是以太网,名称改为ethx,如果是WLAN,名称改为wlanx。

2.init.rc中增加两个针对usb0的配置服务

增加两个针对usb0的配置服务,才能让framework配置得了usb0网卡(服务是是USB网络共享的核心)
具体在MT2712平台,修改的文件为:init.mt2712.ab.emmc.rc

#启动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

3.frameworks中修改配置

对应Android源码,frameworks中添加配置(这边主要是讲述网络是如何启动的,是选择那个网络)
frameworks/base/core/res/res/values/config.xml
原始为:

<string translatable="false" name="config_ethernet_iface_regex">eth\\d</string>

即支持以太网
修改为:

<string translatable="false" name="config_ethernet_iface_regex">usb\\d</string>

即支持USB共享网络。
也可以修改为同时支持以太网和USB共享网络,再根据实际情况,由APP决定使用哪一种网络。

<string translatable="false" name="config_ethernet_iface_regex">eth\\d|usb\\d</string>

config_ethernet_iface_regex的调用流程如下:
路径:frameworks/opt/net/ethernet/java/com/android/server/ethernet/EthernetTracker.java

EthernetTracker(Context context, Handler handler) {
    mHandler = handler;

    // The services we use.
    IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
    mNMService = INetworkManagementService.Stub.asInterface(b);
	在这里就获取上面设定config_ethernet_iface_regex的值
    // Interface match regex.
    mIfaceMatch = context.getResources().getString(
            com.android.internal.R.string.config_ethernet_iface_regex);

    // Read default Ethernet interface configuration from resources
    final String[] interfaceConfigs = context.getResources().getStringArray(
            com.android.internal.R.array.config_ethernet_interfaces);
    for (String strConfig : interfaceConfigs) {
        parseEthernetConfig(strConfig);
    }

    mConfigStore = new EthernetConfigStore();

    NetworkCapabilities nc = createNetworkCapabilities(true /* clear default capabilities */);
    mFactory = new EthernetNetworkFactory(handler, context, nc);
    mFactory.register();
}

    private void maybeTrackInterface(String iface) {
        if (DBG) Log.i(TAG, "maybeTrackInterface " + iface);
        // If we don't already track this interface, and if this interface matches
        // our regex, start tracking it.
        if (!iface.matches(mIfaceMatch) || mFactory.hasInterface(iface)) {
            return;
        }

        if (mIpConfigForDefaultInterface != null) {
            updateIpConfiguration(iface, mIpConfigForDefaultInterface);
            mIpConfigForDefaultInterface = null;
        }
        addInterface(iface);
    }

同时支持以太网和USB共享网络的情况,可以参考:https://blog.csdn.net/xcyansmile/article/details/109361979

二、调试

Android车机与T-box通过USB连接,Android车机为Host,T-box为Device。
1.准备
Android手机设为USB共享网络:
设置->无线和网络->移动网络共享->USB共享网路
2.USB连接后,在Android车机输入lsusb,以确认有新的USB设备插入。

Bus 003 Device 001: ID 1d6b:0002
Bus 001 Device 001: ID 1d6b:0002
Bus 001 Device 002: ID 2c7c:0125
Bus 004 Device 001: ID 1d6b:0003
Bus 002 Device 001: ID 1d6b:0003

未插入前为:

Bus 003 Device 001: ID 1d6b:0002
Bus 001 Device 001: ID 1d6b:0002
Bus 004 Device 001: ID 1d6b:0003
Bus 002 Device 001: ID 1d6b:0003

3.查看网卡是否生成:ifconfig -a

console:/ # ifconfig -a
ip6tnl0   Link encap:UNSPEC  
          NOARP  MTU:1452  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0 
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 
          collisions:0 txqueuelen:1 
          RX bytes:0 TX bytes:0 

p2p0      Link encap:Ethernet  HWaddr 02:76:1c:60:1a:16
          BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0 
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 
          collisions:0 txqueuelen:1000 
          RX bytes:0 TX bytes:0 

wlan0     Link encap:Ethernet  HWaddr 00:76:1c:60:1a:16  Driver mtk_sdio_client
          UP BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0 
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 
          collisions:0 txqueuelen:1000 
          RX bytes:0 TX bytes:0 

sit0      Link encap:IPv6-in-IPv4  
          NOARP  MTU:1480  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0 
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 
          collisions:0 txqueuelen:1 
          RX bytes:0 TX bytes:0 

ip_vti0   Link encap:UNSPEC  
          NOARP  MTU:1480  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0 
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 
          collisions:0 txqueuelen:1 
          RX bytes:0 TX bytes:0 

tunl0     Link encap:UNSPEC  
          NOARP  MTU:1480  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0 
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 
          collisions:0 txqueuelen:1 
          RX bytes:0 TX bytes:0 

ip6_vti0  Link encap:UNSPEC  
          NOARP  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0 
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 
          collisions:0 txqueuelen:1 
          RX bytes:0 TX bytes:0 

usb0      Link encap:Ethernet  HWaddr 36:28:7d:39:88:1b  Driver rndis_host
          inet addr:192.168.225.24  Bcast:192.168.225.255  Mask:255.255.255.0 
          inet6 addr: fe80::c888:d5a1:7f11:5ea0/64 Scope: Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:250 errors:0 dropped:0 overruns:0 frame:0 
          TX packets:333 errors:0 dropped:0 overruns:0 carrier:0 
          collisions:0 txqueuelen:1000 
          RX bytes:72265 TX bytes:70616 

eth0      Link encap:Ethernet  HWaddr 00:55:7b:b5:7d:f7  Driver dwmac-mediatek
          BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0 
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 
          collisions:0 txqueuelen:1000 
          RX bytes:0 TX bytes:0 
          Interrupt:13 Base address:0x4000 

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0 
          inet6 addr: ::1/128 Scope: Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:43 errors:0 dropped:0 overruns:0 frame:0 
          TX packets:43 errors:0 dropped:0 overruns:0 carrier:0 
          collisions:0 txqueuelen:1 
          RX bytes:3860 TX bytes:3860

这里有网卡usb0生成。

4.确认联网是否成功:ifconfig

console:/ # ifconfig
wlan0     Link encap:Ethernet  HWaddr 00:76:1c:60:1a:16  Driver mtk_sdio_client
          UP BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0 
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 
          collisions:0 txqueuelen:1000 
          RX bytes:0 TX bytes:0 

usb0      Link encap:Ethernet  HWaddr 36:28:7d:39:88:1b  Driver rndis_host
          inet addr:192.168.225.24  Bcast:192.168.225.255  Mask:255.255.255.0 
          inet6 addr: fe80::c888:d5a1:7f11:5ea0/64 Scope: Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:279 errors:0 dropped:0 overruns:0 frame:0 
          TX packets:361 errors:0 dropped:0 overruns:0 carrier:0 
          collisions:0 txqueuelen:1000 
          RX bytes:73769 TX bytes:74424 

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0 
          inet6 addr: ::1/128 Scope: Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:43 errors:0 dropped:0 overruns:0 frame:0 
          TX packets:43 errors:0 dropped:0 overruns:0 carrier:0 
          collisions:0 txqueuelen:1 
          RX bytes:3860 TX bytes:3860

看到usb0,即表示联网成功,**“RX bytes:73769 TX bytes:74424”**表示接收和发送的数据量。

注意事项:保证Linux kernel中生成的网卡名和frameworks中联网使用的网卡名一致。

参考:https://blog.csdn.net/xcyansmile/article/details/109361979

GitHub 加速计划 / li / linux-dash
10.39 K
1.2 K
下载
A beautiful web dashboard for Linux
最近提交(Master分支:2 个月前 )
186a802e added ecosystem file for PM2 4 年前
5def40a3 Add host customization support for the NodeJS version 4 年前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐