Background Android KitKat default already support Ethernet wired network, as long as a little configuration, you can directly use, test results, web browser and download are no problem, and the system can do with WiFi coexistence, do not affect the function, here is a brief introduction to how to enable Ethernet, and briefly analyze its code and process.
Linux configuration section
Linux needs to be able to support wired networks and generate ETH network device nodes.
Android Configuration Overlay
The main is overlay inside add Ethernet network type support: Frameworks/base/core/res/res/values/config.xml
<string-array translatable= "false" Name= "Radioattributes" > <item> "All" </item> <item > "7,1" </item> <item> "9,1" </item> </string-array>
Where 9 corresponds to the Ethernet network type, which is defined inin Connectivitymanager.java
/** * The Ethernet data connection. When active, all data traffic * would use the This network type ' s interface by default * (it has a default route). */public static final int type_ethernet = 9;
init.<board>.rc
you need to add DHCP and IP renew services in init
# dhcpcd# # Eth0service DHCPCD_ETH0/SYSTEM/BIN/DHCPCD-ABKL class main disabled oneshot# IP renew# # Eth0ser Vice Iprenew_eth0/system/bin/dhcpcd-n class main disabled oneshot
Process Analysis
The Connectivityserviceconnectivityservice constructor will read the network configuration inside the radioattributes.
Public Connectivityservice (context context, Inetworkmanagementservice Netmanager, inetworkstatsservice Statsservice, Inetworkpolicymanager Policymanager, networkfactory netfactory) { if (DBG) log (" Connectivityservice starting up ");
Load Device Network attributes from Resources string[] rastrings = Context.getresources (). Getstringarray ( com.android.internal.r.array.radioattributes); for (String rastring:rastrings) { radioattributes r = new Radioattributes (rastring); if (vdbg) log ("rastring=" + rastring + "r=" + R); if (R.mtype > Connectivitymanager.max_radio_type) { Loge ("Error in radioattributes-ignoring attempt to define Typ E "+ r.mtype); Continue; } if (Mradioattributes[r.mtype]! = null) { Loge ("Error in radioattributes-ignoring attempt to redefine type" + R.mtype); Continue; } Mradioattributes[r.mtype] = r; }
Based on the network configuration data, the Ethernetdatatracker will be created and the listener can start listening startmonitoring
Create and start trackers for hard-coded networks for (int targetnetworktype:mprioritylist) { final NETWORKCO Nfig config = Mnetconfigs[targetnetworktype]; Final Networkstatetracker tracker; try { tracker = netfactory.createtracker (targetnetworktype, config); Mnettrackers[targetnetworktype] = tracker; } catch (IllegalArgumentException e) { slog.e (TAG, "problem creating" + getnetworktypename (targetnetworktype) + "Tracker:" + e); Continue; } Tracker.startmonitoring (context, mtrackerhandler); if (Config.isdefault ()) { tracker.reconnect (); } }
EthernetdatatrackerEthernetdatatracker will look for the first wired network device starting with ETH, open and start DHCP
<!--Regex of Wired Ethernet ifaces-- <string translatable= "false" Name= "Config_ethernet_iface_regex" >eth\\d</string>
Sifacematch = Context.getresources (). getString (com.android.internal.r.string.config_ethe Rnet_iface_regex); try {final string[] ifaces = mnmservice.listinterfaces (); for (String iface:ifaces) {if (Iface.matches (Sifacematch)) {Mnmservice.setinterfaceu P (iface); interfaceconfiguration config = mnmservice.getinterfaceconfig (iface); if (getethernetcarrierstate (iface) = = 1) {miface = Iface; Mlinkup = true; Mnetworkinfo.setisavailable (TRUE); if (config! = null && MHWADDR = = null) {mhwaddr = Config.gethardwareaddress (); if (mhwaddr! = null) {mnetworkinfo.setextrainfo (MHWADDR); } } } //If a DHCP client had previously been started for the interface, then stop it networkutils.stopdhcp (IFA CE); }} reconnect (); } catch (RemoteException e) {log.e (TAG, "Could not get list of interfaces" + E); }
After DHCP succeeds, notify the Networkmanagementservice network is connected, this time the upper application can begin to perform network operation.
Mnetworkinfo.setdetailedstate (detailedstate.connected, NULL, MHWADDR); Message msg = Mcshandler.obtainmessage (event_state_changed, mnetworkinfo); Msg.sendtotarget ();
If there are multiple network ports, the Ethernetdatatracker obviously cannot meet the requirements and must be extended.