当前位置: 代码迷 >> Android >> Android4.4(MT8685)源码WIFI-初始化一
  详细解决方案

Android4.4(MT8685)源码WIFI-初始化一

热度:616   发布时间:2016-04-28 02:57:04.0
Android4.4(MT8685)源码WIFI--初始化1

在Android系统启动的时候,会在./frameworks/base/services/java/com/android/server/SystemServer.java类中启动许多服务,其中和Wifi相关的服务就在这里初始化。在SystemServer的main方法中,我们可以看到

thr.initAndLoop();

这里调用了它的内部类对象的initAndLoop方法,在这个方法的作用主要就是初始化系统启动需要加载的服务的,我们在这个方法中可以找到

try {                    Slog.i(TAG, "Wi-Fi Service");                    wifi = new WifiService(context);                    ServiceManager.addService(Context.WIFI_SERVICE, wifi);                } catch (Throwable e) {                    reportWtf("starting Wi-Fi Service", e);                }                try {                    Slog.i(TAG, "Connectivity Service");                    connectivity = new ConnectivityService(                            context, networkManagement, networkStats, networkPolicy);                    ServiceManager.addService(Context.CONNECTIVITY_SERVICE, connectivity);                    networkStats.bindConnectivityManager(connectivity);                    networkPolicy.bindConnectivityManager(connectivity);                    wifiP2p.connectivityServiceReady();                    wifi.checkAndStartWifi();                } catch (Throwable e) {                    reportWtf("starting Connectivity Service", e);                }

这里我们可以看到初始化了一个WifiService和ConnectivityService对象,并把这2个对象注册到ServiceManager中,然后通过创建的对象wifi 来调用checkAndStartWifi方法,具体的WifiService的分析我们下一章再做详细介绍,现在我们简单分析下ConnectivityService。

ConnectivityService是管理系统中所有的数据连接服务,其中就包括Wifi,我们来看看它的构造函数,路径:./frameworks/base/services/java/com/android/server/ConnectivityService.java

public ConnectivityService(Context context, INetworkManagementService netd,            INetworkStatsService statsService, INetworkPolicyManager policyManager) {        // Currently, omitting a NetworkFactory will create one internally        // TODO: create here when we have cleaner WiMAX support        this(context, netd, statsService, policyManager, null);    }


这里就是调用了同名的构造方法

public ConnectivityService(Context context, INetworkManagementService netManager,            INetworkStatsService statsService, INetworkPolicyManager policyManager,            NetworkFactory netFactory) {        if (DBG) log("ConnectivityService starting up");        HandlerThread handlerThread = new HandlerThread("ConnectivityServiceThread");        handlerThread.start();        mHandler = new InternalHandler(handlerThread.getLooper());        mTrackerHandler = new NetworkStateTrackerHandler(handlerThread.getLooper());// Create and start trackers for hard-coded networks        for (int targetNetworkType : mPriorityList) {            final NetworkConfig config = mNetConfigs[targetNetworkType];            final NetworkStateTracker tracker;            try {                /** M: Hotspot Manager @{*/                if(config.radio == TYPE_USB){                    log("new UsbDataStateTracker");                    mNetTrackers[targetNetworkType] = new UsbDataStateTracker(targetNetworkType, config.name, mNetd);                    mNetTrackers[targetNetworkType].startMonitoring(context, mTrackerHandler);                    continue;                }                 if(true == FeatureOption.MTK_ALPS_BOX_SUPPORT && config.radio == TYPE_ETHERNET){	                    if (DBG) log("Starting Ethernet Service.");                    mNetTrackers[TYPE_ETHERNET] = makeEthStateTracker(context);                    if(mNetTrackers[TYPE_ETHERNET] != null){                        if (DBG) log("mNetTrackers[TYPE_ETHERNET] != null");                        mNetTrackers[TYPE_ETHERNET].startMonitoring(context, mTrackerHandler);                        if (config.isDefault()) {                            mNetTrackers[TYPE_ETHERNET].reconnect();                        }                      }                                  continue;                                }                /*@} */		/*@} */                ///M:new a Teledongle Data Tracker @by mtk94127 @{                if(FeatureOption.MTK_EXTERNAL_DONGLE_SUPPORT && (config.radio == TYPE_TEDONGLE)) {                    log("new TedongleDataStateTracker");                    mNetTrackers[targetNetworkType] = makeTedongleDataTracker();                    log("new TedongleDataStateTracker medium!");                    mNetTrackers[targetNetworkType].startMonitoring(context, mTrackerHandler);                    log("new TedongleDataStateTracker end!");                    continue;                }                ///M:@}                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();            }        }}

这里根据com.android.internal.R.array.networkAttributes配置的属性去创建一个个tracker,并监听其状态,tracker.startMonitoring(context, mTrackerHandler);可以看出tracker可以通过mTrackerHandler向ConnectivityService发送消息,这些消息有

 /**     * The network state has changed and the NetworkInfo object     * contains the new state.     *     * msg.what = EVENT_STATE_CHANGED     * msg.obj = NetworkInfo object     */    public static final int EVENT_STATE_CHANGED = BASE_NETWORK_STATE_TRACKER;    /**     * msg.what = EVENT_CONFIGURATION_CHANGED     * msg.obj = NetworkInfo object     */    public static final int EVENT_CONFIGURATION_CHANGED = BASE_NETWORK_STATE_TRACKER + 1;    /**     * msg.what = EVENT_RESTORE_DEFAULT_NETWORK     * msg.obj = FeatureUser object     */    public static final int EVENT_RESTORE_DEFAULT_NETWORK = BASE_NETWORK_STATE_TRACKER + 2;    /**     * msg.what = EVENT_NETWORK_SUBTYPE_CHANGED     * msg.obj = NetworkInfo object     */    public static final int EVENT_NETWORK_SUBTYPE_CHANGED = BASE_NETWORK_STATE_TRACKER + 3;    /**     * msg.what = EVENT_NETWORK_CONNECTED     * msg.obj = LinkProperties object     */    public static final int EVENT_NETWORK_CONNECTED = BASE_NETWORK_STATE_TRACKER + 4;    /**     * msg.what = EVENT_NETWORK_CONNECTION_DISCONNECTED     * msg.obj = LinkProperties object, same iface name     */    public static final int EVENT_NETWORK_DISCONNECTED = BASE_NETWORK_STATE_TRACKER + 5;

当WiFi的连接AP成功后,就会想 ConnectivityService发送一个EVENT_STATE_CHANGED消息。

接着我们来看创建了哪些tracker,找到createTracker方法

 @Override        public NetworkStateTracker createTracker(int targetNetworkType, NetworkConfig config) {            switch (config.radio) {                case TYPE_WIFI:                    return new WifiStateTracker(targetNetworkType, config.name);                case TYPE_MOBILE:                    return new MobileDataStateTracker(targetNetworkType, config.name);                case TYPE_DUMMY:                    return new DummyDataStateTracker(targetNetworkType, config.name);                case TYPE_BLUETOOTH:                    return BluetoothTetheringDataTracker.getInstance();                case TYPE_WIMAX:                    return makeWimaxStateTracker(mContext, mTrackerHandler);                case TYPE_CSD:                    return new CsdStateTracker(targetNetworkType, config.name);                default:                    throw new IllegalArgumentException(                            "Trying to create a NetworkStateTracker for an unknown radio type: "                            + config.radio);            }        }

这里我们主要关注TYPE_WIFI,创建了一个WifiStateTracker对象,进入到WifiStateTracker的构造方法

 public WifiStateTracker(int netType, String networkName) {        ///M:@{        mNetType = netType;        ///@}        mNetworkInfo = new NetworkInfo(netType, 0, networkName, "");        mLinkProperties = new LinkProperties();        mLinkCapabilities = new LinkCapabilities();        mNetworkInfo.setIsAvailable(false);        setTeardownRequested(false);    }

接着调用startMonitoring

 /**     * Begin monitoring wifi connectivity     */    public void startMonitoring(Context context, Handler target) {        mCsHandler = target;        mContext = context;        mWifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);        IntentFilter filter = new IntentFilter();        ///M:seperate p2p and wifi @{        if (mNetType == ConnectivityManager.TYPE_WIFI) {           filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);           filter.addAction(WifiManager.LINK_CONFIGURATION_CHANGED_ACTION);        }        ///@}                mWifiStateReceiver = new WifiStateReceiver();        mContext.registerReceiver(mWifiStateReceiver, filter);    }


 这里就是注册了一个广播用于接收NETWORK_STATE_CHANGED_ACTION和LINK_CONFIGURATION_CHANGED_ACTION广播,ConnectivityService我们就分析到这里,下一章我们将详细分析WifiService。

  相关解决方案