Background Android kitkat supports Ethernet wired networks by default. It can be used directly with a slight configuration. The test results, network browsers and downloads are all correct, and the system can coexist with wifi, this section briefly describes how to enable Ethernet and briefly analyzes its code and process.
Linux Configuration
Linux must support wired networks to generate eth network device nodes.
Configure overlay for Android
The Ethernet network type added in overlay is supported: frameworks/base/core/res/values/config. xml.
"1,1"
"7,1"
"9,1"
9 corresponds to the Ethernet network type, which is defined in ConnectivityManager. java.
/** * The Ethernet data connection. When active, all data traffic * will use this network type's interface by default * (it has a default route). */ public static final int TYPE_ETHERNET = 9;
Init. . Rc
Dhcp and ip renew services need to be added in init.
# DHCPCD# # eth0service dhcpcd_eth0 /system/bin/dhcpcd -ABKL class main disabled oneshot# IP Renew# # eth0service iprenew_eth0 /system/bin/dhcpcd -n class main disabled oneshot
Process Analysis
The ConnectivityServiceConnectivityService constructor reads the network configuration in 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 type " + 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 is created and the system starts to listen to startMonitoring.
// Create and start trackers for hard-coded networks for (int targetNetworkType : mPriorityList) { final NetworkConfig 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 search for the first wired network device starting with eth and start dhcp.
eth\\d
sIfaceMatch = context.getResources().getString( com.android.internal.R.string.config_ethernet_iface_regex); try { final String[] ifaces = mNMService.listInterfaces(); for (String iface : ifaces) { if (iface.matches(sIfaceMatch)) { mNMService.setInterfaceUp(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 this interface, then stop it NetworkUtils.stopDhcp(iface); } } reconnect(); } catch (RemoteException e) { Log.e(TAG, "Could not get list of interfaces " + e); }
After DHCP is successful, the NetworkManagementService network is notified to be connected. At this time, the upper-layer application can start to perform network operations.
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.