Android 6.0 Ethernet流程分析记录

本文详细分析了Android 6.0系统中以太网EthernetService的启动流程,包括初始化EthernetServiceImpl、获取配置信息、注册监听及开启网络配置。重点关注了EthernetConfigStore如何读取和写入配置,以及EthManager与NetworkManagementService在设置以太网配置中的作用,揭示了底层通信的关键步骤。
摘要由CSDN通过智能技术生成

1、Android 开机后,启动系统服务EthernetService(frameworks/opt/net/ethernet/java/com/android/server/ethnet/EthernetService.java),初始化一个EthernetServiceImpl(frameworks/opt/net/ethernet/java/com/android/server/ethnet/EthernetServiceImpl.java)对象,然后调用EthernetServiceImpl的start()方法;

2、初始化EthernetServiceImpl对象,创建EthernetConfigStore对象并调用readIpAndProxyConfigurations()方法,获取Ethernet 配置信息得到IpConfiguration对象;

   public EthernetServiceImpl(Context context) {
        mContext = context;
        Log.i(TAG, "Creating EthernetConfigStore");
        mEthernetConfigStore = new EthernetConfigStore();
        mIpConfiguration = mEthernetConfigStore.readIpAndProxyConfigurations();

        Log.i(TAG, "Read stored IP configuration: " + mIpConfiguration);

        mTracker = new EthernetNetworkFactory(mListeners);
    }

  3、调用EthernetServiceImpl.start()方法,

    public void start() {
        Log.i(TAG, "Starting Ethernet service");

        HandlerThread handlerThread = new HandlerThread("EthernetServiceThread");
        handlerThread.start();
        mHandler = new Handler(handlerThread.getLooper());


        mTracker.start(mContext, mHandler);

        mStarted.set(true);
    }

主要是进入EthernetNetworkFactory.start() ,注册相关监听,开启网络配置

public synchronized void start(Context context, Handler target) {
        // The services we use.
        IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
        mNMService = INetworkManagementService.Stub.asInterface(b);
        mEthernetManager = (EthernetManager) context.getSystemService(Context.ETHERNET_SERVICE);

        // Interface match 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;

 	mHandler = target;//201808016 chen

        // 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(!iface.equals("eth0")) {
                            continue;
                        }
//
                        if (mNMService.getInterfaceConfig(iface).hasFlag("running")) {
                            updateInterfaceState(iface, true);
                        }
                        break;
                    }
                }
            }
        } catch (RemoteException|IllegalStateException e) {
            Log.e(TAG, "Could not get list of interfaces " + e);
        }
    }

4、读取网络配置方法 

 EthernetConfigStore.readIpAndProxyConfigurations()如下,EthernetConfigStore 继承自 IpConfigStore

   private static final String ipConfigFile = Environment.getDataDirectory() +
            "/misc/ethernet/ipconfig.txt";
   public IpConfiguration readIpAndProxyConfigurations() {
        SparseArray<IpConfiguration> networks = readIpAndProxyConfigurations(ipConfigFile);//d调用IpConfigStore中的方法

        if (networks.size() == 0) {
            Log.w(TAG, "No Ethernet configuration found. Using default.");
            return new IpConfiguration(IpAssignment.DHCP, ProxySettings.NONE, null, null);
        }

        if (networks.size() > 1) {
            // Currently we only support a single Ethernet interface.
            Log.w(TAG, "Multiple Ethernet configurations detected. Only reading first one.");
        }

        return networks.valueAt(0);
    }

 5、设置以太网,通过EthManager.setConfiguration(IpConfiguration)方法事件是调用的EthernetServiceImpl.setConfiguration

  /**
     * Set Ethernet configuration
     */
    @Override
    public void setConfiguration(IpConfiguration config) {
        if (!mStarte
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值