Android系统添加USB以太网支持
参考:
http://blog.csdn.net/weiqing1981127/article/details/8521600
一.以太网framework分析
启动流程:SystemServer类的main函数:The main entry point from zygote,开机的时候执行
main : SystemServer.java (frameworks\base\services\java\com\android\server)
new SystemServer().run();
run //系统server的初始化
startOtherServices(); //初始化startBootstrapServices();,startCoreServices();之后
ServiceManager.addService(Context.NETWORKMANAGEMENT_SERVICE, networkManagement);
mSystemServiceManager.startService(ETHERNET_SERVICE_CLASS); //初始化Internet server
//SystemServiceManager.java (frameworks\base\services\core\java\com\android\server)
startService(String className) //Starts a service by class name. //SystemServiceManager.java (frameworks\base\services\core\java\com\android\server)
startService(serviceClass); //Creates and starts a system service
service = constructor.newInstance(mContext); //构建一个实例
mServices.add(service); //注册sever
service.onStart(); //启动servece
EthernetService : 这里我们分析的severce是internet的
//EthernetService.java (frameworks\opt\net\ethernet\java\com\android\server\ethernet)
new EthernetServiceImpl(context); //new EthernetServiceImpl
publishBinderService(Context.ETHERNET_SERVICE, mImpl); 启动EthernetServiceImpl的start\
//EthernetServiceImpl.java (frameworks\opt\net\ethernet\java\com\android\server\ethernet),用来操作以太网
EthernetServiceImpl //构造函数
new EthernetConfigStore(); //This class provides an API to store and manage Ethernet network configuration.
readIpAndProxyConfigurations
writeIpAndProxyConfigurations
new EthernetNetworkFactory(mListeners); //Manages connectivity for an Ethernet interface.
//EthernetNetworkFactory.java (frameworks\opt\net\ethernet\java\com\android\server\ethernet)
new NetworkInfo(ConnectivityManager.TYPE_ETHERNET, 0, NETWORK_TYPE, ""); //Describes the status of a network interface.
new LinkProperties(); //Describes the properties of a network link.
initNetworkCapabilities(); //设置网卡的功能
start() //EthernetServiceImpl的执行函数,Starting Ethernet service
new HandlerThread("EthernetServiceThread"); //初始化 Thread,创建线程Handy class for starting a new thread that has a looper
handlerThread.start(); //启动线程
new Handler(handlerThread.getLooper()); //用来发送和处理与线程消息队列相关的Message以及Runnable对象
mTracker.start(mContext, mHandler); //执行EthernetNetworkFactory类中的start
start //EthernetNetworkFactory中,Begin monitoring connectivity
mIfaceMatch = context.getResources().getString(com.android.internal.R.string.config_ethernet_iface_regex); // Interface match regex.匹配
mFactory = new LocalNetworkFactory(NETWORK_TYPE, context, target.getLooper()); // Create and register our NetworkFactory.
mFactory.register();
mInterfaceObserver = new InterfaceObserver(); //Start tracking interface change events.
mNMService.listInterfaces(); // If an Ethernet interface is already connected, start tracking that.
updateInterfaceState(iface, true); //We have our interface. Track it.//注册
mFactory.setScoreFilter(up ? NETWORK_SCORE : -1); //设置scroe值;这个是网络优先级判断依据
mStarted.set(true); //设置serverce已经启动
二.以太网mt6735移植-kernel部分
1.config里面添加一些宏定义
+ONFIG_MISC_VIA=y
+CONFIG_USB_USBNET=y+CONFIG_USB_NET_AX8817X=y+CONFIG_USB_NET_CDCETHER=y+CONFIG_USB_NET_CDC_EEM=y+CONFIG_USB_NET_CDC_NCM=y+CONFIG_USB_NET_DM9601=y+CONFIG_USB_NET_SMSC75XX=n+CONFIG_USB_NET_SMSC95XX=n+CONFIG_USB_NET_GL620A=n+CONFIG_USB_NET_NET1080=y+CONFIG_USB_NET_PLUSB=y+CONFIG_USB_NET_MCS7830=n+CONFIG_USB_NET_RNDIS_HOST=y+CONFIG_USB_NET_CDC_SUBSET=y+CONFIG_USB_ALI_M5632=n+CONFIG_USB_AN2720=n+CONFIG_USB_BELKIN=y+CONFIG_USB_ARMLINUX=y+CONFIG_USB_EPSON2888=n+CONFIG_USB_KC2190=n+CONFIG_USB_NET_ZAURUS=y+CONFIG_USB_NET_CX82310_ETH=n+CONFIG_USB_NET_KALMIA=n+CONFIG_USB_NET_QMI_WWAN=n+CONFIG_USB_HSO=y+CONFIG_USB_NET_INT51X1=n+CONFIG_USB_CDC_PHONET=n+CONFIG_USB_IPHETH=n+CONFIG_USB_SIERRA_NET=n+CONFIG_USB_VL600=n
三.以太网mt6735移植-中间层部分
1.DHCP的util
system/core/libnetutils/dhcp_utils.c的dhcp_do_request函数403行
这里主要是自动分配ip的一个文件,这里需要改动下参数
原来:
// snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s:-A -f %s -h %s %s", DAEMON_NAME,
// p2p_interface,DHCP_CONFIG_PATH, prop_value, interface);
改成:
snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s", DAEMON_NAME, interface);
ALOGE("dhcp_do_request property_get ok daemon_cmd = %s", daemon_cmd);
原来:
// snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s:-A -f %s %s", DAEMON_NAME, p2p_interface,
// DHCP_CONFIG_PATH, interface);
改成:
snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s", DAEMON_NAME, interface);
ALOGE("dhcp_do_request property_get fail daemon_cmd = %s", daemon_cmd);
2.加上系统sevice
+//Add for usb to RJ45 Ethernet
+service dhcpcd_eth0 /system/bin/dhcpcd -ABKL -d eth0
+ class main
+ disabled
+ oneshot
+//End
+//Add for usb to RJ45 Athernet
+service iprenew_eth0 /system/bin/dhcpcd -n
+ class main
+ disabled
+ oneshot
四.以太网mt6735移植-framework部分
1.启动ETHERNET_SERVICE_CLASS
frameworks/base/services/java/com/android/server/SystemServer.java的startOtherServices函数里面:
// if (mPackageManager.hasSystemFeature(PackageManager.FEATURE_ETHERNET)) {
Slog.e(TAG, "on mSystemServiceManager.startService");
mSystemServiceManager.startService(ETHERNET_SERVICE_CLASS); //启动service
// }
2.在标准的Framework中添加自己的新的资源
frameworks/base/core/res/res/values/config.xml 这是framework的默认配置
<string-array name="config_statusBarIcons"> //这里是添加配置栏
+<item><xliff:g id="id">ethernet</xliff:g></item>
<string-array translatable="false" name="networkAttributes"> //这里是framework里面需要的属性
+<item>"ethernet,9,9,1,-1,true"</item>
<string-array translatable="false" name="radioAttributes"> //这里是添加无线的属性
+<item>"9,9"</item>
+<string-array translatable="false" name="config_tether_ethernet_regexs"> //这里是加入一个端点,比如eth0
+<item>"eth\\d"</item>
+</string-array>
3.在系统的framework/base/core/res/res中添加字串等资源,需要在framework/base/core/res/res/values/symbols.xml中同步添加资源字串,否则编译会出错。
frameworks/base/core/res/res/values/symbols.xml //这里添加是因为在config.xml里面添加了新的一栏
<java-symbol type="array" name="config_tether_ethernet_regexs" />
四.以太网mt6735移植-app部分(Setting)
1.packages/apps/Settings/AndroidManifest.xml, 注册一些东西
<!-- Ethernet controls -->
<activity android:name=".EthernetSettings"
android:label="@string/eth_radio_ctrl_title"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.settings.ETHERNET_SETTINGS" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.VOICE_LAUNCH" />
</intent-filter>
</activity>
<activity android:name=".ethernet.EthernetEnabler" android:label="@string/eth_setting"
android:clearTaskOnLaunch="true"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.settings.ETHERNET_SETTINGS" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.VOICE_LAUNCH" />
<category android:name="com.android.settings.SHORTCUT" />
</intent-filter>
</activity>
<activity android:name=".ethernet.EthernetConfigDialog" android:label="@string/eth_conf_perf_title"
android:clearTaskOnLaunch="true"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.settings.ETHERNET_SETTINGS" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.VOICE_LAUNCH" />
<category android:name="com.android.settings.SHORTCUT" />
</intent-filter>
</activity>
2.增加eth用的的字符串资源
packages/apps/Settings/res/values/strings.xml
<!-- Ethernet configuration dialog -->
<string name="eth_config_title">Configure Ethernet device</string>
<string name="eth_setting">Ethernet setting</string>
<string name="eth_dev_list">Ethernet Devices:</string>
<string name="eth_con_type">Connection Type</string>
<string name="eth_con_type_dhcp">DHCP</string>
<string name="eth_con_type_manual">Static IP</string>
<string name="eth_dns1">DNS address 1</string>
<string name="eth_dns2">DNS address 2</string>
<string name="eth_gw">Gateway address</string>
<string name="eth_ipaddr">IPv4 address</string>
<string name="eth_quick_toggle_title">Ethernet</string>
<string name="eth_quick_toggle_summary">Turn on Ethernet</string>
<string name="eth_radio_ctrl_title">Ethernet</string>
<string name="eth_conf_perf_title">Ethernet configuration</string>
<string name="eth_conf_summary">Configure Ethernet devices</string>
<string name="eth_pre_length">Network prefix length</string>
<string name="eth_toggle_summary_off">Turn off Ethernet</string>
<string name="eth_toggle_summary_on">Turn on Ethernet</string>
3.添加Setting子菜单
packages/apps/Settings/res/xml/dashboard_categories.xml
<!-- Ethernet -->
<dashboard-tile
android:id="@+id/ethernet_settings"
android:title="@string/eth_radio_ctrl_title"
android:fragment="com.android.settings.EthernetSettings"
android:icon="@drawable/ic_settings_ethernet" />
所谓的dashboard就是Settings模块首界面的一个抽象。而dashboard-categorys则是设置分类集合的抽象,而dashboard-category是分类的抽象,dashboard-tile就是分类下每个选项的抽象了。代码中的List<DashboardCategory>对应dashboard-categorys, DashboardCategory对应dashboard-category,而dashboard-tile则对因代码中的DashboardTile。
4.eth图标, layout文件,媒体声音,Seting文件
图标:packages/apps/Settings/res/drawable-hdpi/
,附件ic_settings_ethernet.png
layout:packages/apps/Settings/res/layout/
媒体声音:packages/apps/Settings/res/raw/
Seting文件:packages/apps/Settings/res/xml/ethernet_settings.xml
5.Setting程序:packages/apps/Settings/src/com/android/settings/EthernetSettings.java
setting的一些操作
子activity文件:packages/apps/Settings/src/com/android/settings/ethernet