Android – Wifi 初始化過程

一張簡圖就是最基本的初始化過程啦!

稍微描述如下列各項:

1) 透過 new WifiService(context) 開啟wifi的服務, 也透過ConnectivityService() 開啟 connectivity服務
位置: android/frameworks/base/services/java/com/android/server/SystemServer.java

               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 {
                   int enableCne = 1;
                   if (!deviceHasSufficientMemory()) {
                       enableCne = SystemProperties.getInt("persist.cne.override.memlimit", 0);
                   }
                   int cneFeature = (enableCne == 1) ?
                       SystemProperties.getInt("persist.cne.feature", 0) : 0;

                   if ( cneFeature > 0 && cneFeature < 10 ) {
                       Slog.i(TAG, "QcConnectivity Service");
                       PathClassLoader qcsClassLoader =
                           new PathClassLoader("/system/framework/services-ext.jar",
                                   ClassLoader.getSystemClassLoader());
                       Class qcsClass =
                           qcsClassLoader.loadClass("com.android.server.QcConnectivityService");
                       Constructor qcsConstructor = qcsClass.getConstructor
                           (new Class[] {Context.class, INetworkManagementService.class,
                            INetworkStatsService.class, INetworkPolicyManager.class});
                       qcCon = qcsConstructor.newInstance(
                               context, networkManagement, networkStats, networkPolicy);
                       connectivity = (ConnectivityService) qcCon;
                   } else {
                       Slog.i(TAG, "Connectivity Service");
                       connectivity = new ConnectivityService( context, networkManagement,
                               networkStats, networkPolicy);
                   }
                   if (connectivity != null) {
                       ServiceManager.addService(Context.CONNECTIVITY_SERVICE, connectivity);
                       networkStats.bindConnectivityManager(connectivity);
                       networkPolicy.bindConnectivityManager(connectivity);
                       wifi.checkAndStartWifi();
                       wifiP2p.connectivityServiceReady();
                   }
               } catch (Throwable e) {
                   reportWtf("starting Connectivity Service", e);
               }

2) 由第一項SystemServer.java裡呼叫 WifiService()定義
位置: android/frameworks/base/services/java/com/android/server/wifi

/**
 * WifiService handles remote WiFi operation requests by implementing
 * the IWifiManager interface.
 *
 * @hide
 */
public final class WifiService extends IWifiManager.Stub {
    private static final String TAG = "WifiService";
    private static final boolean DBG = false;

    final WifiStateMachine mWifiStateMachine;

    private final Context mContext;

   ....

3) 由第一項SystemServer.java裡呼叫 ConnectivityService()
位置: android/frameworks/base/services/java/com/android/server/ConnectivityService.java

/**
 * @hide
 */
public class ConnectivityService extends IConnectivityManager.Stub {
    private static final String TAG = "ConnectivityService";

    protected static final boolean DBG = true;
    protected static final boolean VDBG = false;

    protected static final boolean LOGD_RULES = false;
    ...

4) 由第三項ConnectivityService.java裡呼叫 WifiStateTracker()
位置: android/frameworks/base/services/java/com/android/server/wifi/WifiStateTracker

    public WifiStateTracker(int netType, String networkName) {
        mNetworkInfo = new NetworkInfo(netType, 0, networkName, "");
        mLinkProperties = new LinkProperties();
        mLinkCapabilities = new LinkCapabilities();

        mNetworkInfo.setIsAvailable(false);
        setTeardownRequested(false);
    }

以上的話, 大致wifi的初始化ok

發表迴響