赞
踩
需求
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。
工作记录。。。。。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。