一、NetworkFactory注册
frameworks/base/services/java/com/android/server/SystemServer.java
frameworks/opt/net/wifi/service/java/com/android/server/wifi/WifiService.java
frameworks/opt/net/wifi/service/java/com/android/server/wifi/WifiServiceImpl.java
frameworks/opt/net/wifi/service/java/com/android/server/wifi/ClientModeImpl.java
frameworks/opt/net/wifi/service/java/com/android/server/wifi/WifiNetworkFactory.java
frameworks/libs/net/common/src_servicescommon/android/net/NetworkFactory.java
frameworks/base/core/java/android/net/ConnectivityManager.java
frameworks/base/services/core/java/com/android/server/ConnectivityService.java
*[SystemServer.java]
├──startOtherServices(@NonNull TimingsTraceAndSlog t)
└──if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI))
// Wifi Service must be started first for wifi-related services.
t.traceBegin("StartWifi");
mSystemServiceManager.startServiceFromJar(WIFI_SERVICE_CLASS, WIFI_APEX_SERVICE_JAR_PATH);//启动WifiService
t.traceEnd();
t.traceBegin("StartWifiScanning");
mSystemServiceManager.startServiceFromJar(WIFI_SCANNING_SERVICE_CLASS, WIFI_APEX_SERVICE_JAR_PATH);//启动WifiScanningService
t.traceEnd();
|
*[WifiService.java]
├──onBootPhase(int phase)
└──if (phase == SystemService.PHASE_SYSTEM_SERVICES_READY)
createNotificationChannels(mWifiContext);
mImpl.checkAndStartWifi();
└──else if (phase == SystemService.PHASE_BOOT_COMPLETED)
mImpl.handleBootCompleted();
|
*[WifiServiceImpl.java]
├──handleBootCompleted()
└──mWifiThreadRunner.post(() -> {
mClientModeImpl.handleBootCompleted();})
|
*[ClientModeImpl.java]
*
// Make the network factories.
mNetworkFactory = mWifiInjector.makeWifiNetworkFactory(mNetworkCapabilitiesFilter, mWifiConnectivityManager);//创建WifiNetworkFactory对象
// CHECKSTYLE:OFF IndentationCheck
addState(mDefaultState);
addState(mConnectModeState, mDefaultState);
addState(mL2ConnectedState, mConnectModeState);
addState(mObtainingIpState, mL2ConnectedState);
addState(mConnectedState, mL2ConnectedState);
addState(mRoamingState, mL2ConnectedState);
addState(mDisconnectingState, mConnectModeState);
addState(mDisconnectedState, mConnectModeState);
// CHECKSTYLE:ON IndentationCheck
setInitialState(mDefaultState);
*
├──handleBootCompleted()
└──sendMessage(CMD_BOOT_COMPLETED);
├──DefaultState.processMessage(Message message)
└──case CMD_BOOT_COMPLETED:
└──registerNetworkFactory()
├──registerNetworkFactory()
└──mNetworkFactory.register()//注册WifiNetworkFactory
*[WifiNetworkFactory.java]-->[NetworkFactory.java]//走父类的register方法
├──register()
└──mProvider = new NetworkProvider(mContext, NetworkFactoryImpl.this.getLooper(), logTag) {
@Override
public void onNetworkRequested(@NonNull NetworkRequest request, int score,int servingProviderId) {
handleAddRequest(request);
}
@Override
public void onNetworkRequestWithdrawn(@NonNull NetworkRequest request) {
handleRemoveRequest(request);
}
};
└──((ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE)).registerNetworkProvider(mProvider);//向ConnectivityManager注册NetworkProvider
|
*[ConnectivityManager.java]
├──registerNetworkProvider(@NonNull NetworkProvider provider)
└──int providerId = mService.registerNetworkProvider(provider.getMessenger(),provider.getName());
provider.setProviderId(providerId);
|
*[ConnectivityService.java]
├──registerNetworkProvider(Messenger messenger, String name)
└──NetworkProviderInfo npi = new NetworkProviderInfo(name, messenger,null /* asyncChannel */, nextNetworkProviderId(),
() -> unregisterNetworkProvider(messenger));
└──mHandler.sendMessage(mHandler.obtainMessage(EVENT_REGISTER_NETWORK_PROVIDER, npi));//发送EVENT_REGISTER_NETWORK_PROVIDER消息
├──InternalHandler.handleMessage(Message msg)
└──case EVENT_REGISTER_NETWORK_PROVIDER:
└──handleRegisterNetworkProvider((NetworkProviderInfo) msg.obj);
├──handleRegisterNetworkProvider(NetworkProviderInfo npi)
└──mNetworkProviderInfos.put(npi.messenger, npi);
└──npi.connect(mContext, mTrackerHandler);
├──NetworkProviderInfo.connect(Context context, Handler handler)
└──if (isLegacyNetworkFactory())//条件为false
mAsyncChannel.connect(context, handler, messenger);
└──else
messenger.getBinder().linkToDeath(mDeathRecipient, 0);//注册完成
二、NetworkAgent注册
frameworks/opt/net/wifi/service/java/com/android/server/wifi/ClientModeImpl.java
frameworks/base/core/java/android/net/NetworkAgent.java
frameworks/base/core/java/android/net/ConnectivityManager.java
frameworks/base/services/core/java/com/android/server/ConnectivityService.java
*[ClientModeImpl.java]
├──L2ConnectedState.enter()
└──mNetworkAgent = new WifiNetworkAgent(mContext, getHandler().getLooper(),"WifiNetworkAgent", nc, mLinkProperties, 60, naConfig,mNetworkFactory.getProvider());
├──ClientModeImpl&WifiNetworkAgent -->NetworkAgent.java
|
*[NetworkAgent.java]
├──register()
└──mNetwork = cm.registerNetworkAgent(new Messenger(mHandler),//向ConnectivityManager注册NetworkAgent
new NetworkInfo(mInitialConfiguration.info),
mInitialConfiguration.properties, mInitialConfiguration.capabilities,
mInitialConfiguration.score, mInitialConfiguration.config, providerId);
|
*[ConnectivityManager.java]
├──registerNetworkAgent(Messenger messenger, NetworkInfo ni, LinkProperties lp,NetworkCapabilities nc, int score, NetworkAgentConfig config)
└──return registerNetworkAgent(messenger, ni, lp, nc, score, config, NetworkProvider.ID_NONE)
├──registerNetworkAgent(Messenger messenger, NetworkInfo ni, LinkProperties lp,NetworkCapabilities nc, int score, NetworkAgentConfig config, int providerId)
└──return mService.registerNetworkAgent(messenger, ni, lp, nc, score, config, providerId)
|
*[ConnectivityService.java]
├──registerNetworkAgent(Messenger messenger, NetworkInfo networkInfo,LinkProperties linkProperties, NetworkCapabilities networkCapabilities,
int currentScore, NetworkAgentConfig networkAgentConfig, int providerId)
└──mDeps.getNetworkStack().makeNetworkMonitor(nai.network, name, new NetworkMonitorCallbacks(nai))
├──ConnectivityService&NetworkMonitorCallbacks
└──onNetworkMonitorCreated(INetworkMonitor networkMonitor)
└──mHandler.sendMessage(mHandler.obtainMessage(EVENT_REGISTER_NETWORK_AGENT,new Pair<>(mNai.getAndDestroy(), networkMonitor)))
├──ConnectivityService&InternalHandler
├──handleMessage(Message msg)
└──handleRegisterNetworkAgent(arg.first, arg.second)
├──handleRegisterNetworkAgent(NetworkAgentInfo nai, INetworkMonitor networkMonitor)
└──nai.onNetworkMonitorCreated(networkMonitor)
└──networkMonitor.start()//启动NetworkMonitor
└──nai.asyncChannel.connect(mContext, mTrackerHandler, nai.messenger)//通过AsyncChannel跨进程建立连接
└──mWifiScoreReport.setNetworkAgent(mNetworkAgent);
三、WiFi网络请求
frameworks/opt/net/wifi/service/java/com/android/server/wifi/ClientModeImpl.java
frameworks/base/core/java/android/net/NetworkAgent.java
frameworks/base/services/core/java/com/android/server/ConnectivityService.java
frameworks/base/core/java/android/net/NetworkProvider.java
frameworks/libs/net/common/src_servicescommon/android/net/NetworkFactory.java
frameworks/opt/net/wifi/service/java/com/android/server/wifi/WifiNetworkFactory.java
frameworks/opt/net/wifi/service/java/com/android/server/wifi/ClientModeImpl.java
frameworks/base/core/java/android/net/NetworkAgent.java
frameworks/base/services/core/java/com/android/server/ConnectivityService.java
frameworks/base/core/java/android/net/NetworkProvider.java
frameworks/libs/net/common/src_servicescommon/android/net/NetworkFactory.java
frameworks/opt/net/wifi/service/java/com/android/server/wifi/WifiNetworkFactory.java
*[ClientModeImpl.java]
├──ClientModeImpl&ConnectModeState
├──enter()
└──setupClientMode()
└──updateDataInterface()
└──mIpClientCallbacks = new IpClientCallbacksImpl()
├──ClientModeImpl&IpClientCallbacksImpl
├──onProvisioningSuccess(LinkProperties newLp)
└──sendMessage(CMD_UPDATE_LINKPROPERTIES, newLp);
└──sendMessage(CMD_IP_CONFIGURATION_SUCCESSFUL);
├──ClientModeImpl&L2ConnectedState
├──processMessage(Message message)
└──case CMD_IP_CONFIGURATION_SUCCESSFUL:
└──sendConnectedState()
├──sendConnectedState()
└──mNetworkAgent.markConnected();
|
*[NetworkAgent.java]
├──markConnected()
└──queueOrSendMessage(EVENT_NETWORK_INFO_CHANGED, mNetworkInfo);
├──queueOrSendMessage(int what, Object obj)
└──queueOrSendMessage(what, 0, 0, obj);
├──queueOrSendMessage(int what, int arg1, int arg2)
└──queueOrSendMessage(what, arg1, arg2, null);
├──queueOrSendMessage(int what, int arg1, int arg2, Object obj)
└──queueOrSendMessage(msg);
├──queueOrSendMessage(Message msg)
└──mAsyncChannel.sendMessage(msg);//通过AsyncChannel跨进程与ConnectivityService通信
|
*[ConnectivityService.java]
├──maybeHandleNetworkAgentMessage(Message msg)
└──case NetworkAgent.EVENT_NETWORK_INFO_CHANGED:
└──updateNetworkInfo(nai, info);
├──updateNetworkInfo(NetworkAgentInfo networkAgent, NetworkInfo info)
└──rematchAllNetworksAndRequests();
├──rematchAllNetworksAndRequests()
└──applyNetworkReassignment(changes, now);
├──applyNetworkReassignment(@NonNull final NetworkReassignment changes,final long now)
└──sendUpdatedScoreToFactories(event.mRequest.request, event.mNewNetwork);
├──sendUpdatedScoreToFactories(@NonNull NetworkRequest networkRequest,@Nullable NetworkAgentInfo nai)
└──npi.requestNetwork(networkRequest, score, serial);
├──ConnectivityService&NetworkProviderInfo
├──requestNetwork(NetworkRequest request, int score, int servingProviderId)
└──if (isLegacyNetworkFactory())//条件为true
mAsyncChannel.sendMessage(android.net.NetworkFactory.CMD_REQUEST_NETWORK, score,servingProviderId, request);
|
*[NetworkProvider.java]
├──NetworkProvider(@NonNull Context context, @NonNull Looper looper, @NonNull String name)//构造方法
└──handler.handleMessage(Message m)
└──case CMD_REQUEST_NETWORK:
└──onNetworkRequested((NetworkRequest) m.obj, m.arg1, m.arg2);
|
*[NetworkFactory.java]
├──register()//转到NetworkFactory注册中,创建了NetworkProvider对象
└──mProvider = new NetworkProvider(mContext, NetworkFactoryImpl.this.getLooper(), logTag) {
@Override
public void onNetworkRequested(@NonNull NetworkRequest request, int score,int servingProviderId) {
handleAddRequest(request);
├──handleAddRequest(@NonNull final NetworkRequest request)
└──evalRequest(n);
├──evalRequest(NetworkRequestInfo n)
└──if (shouldNeedNetworkFor(n))//用于判断保留当前的网络连接
log(" needNetworkFor");
needNetworkFor(n.request, n.score);
|
*[WifiNetworkFactory.java]
├──needNetworkFor(NetworkRequest networkRequest, int score)
└──retrieveWifiScanner();//周期性扫描
└──startPeriodicScans()
└──startScan();
└──else if (shouldReleaseNetworkFor(n))//判断需要断开当前网络连接
log(" releaseNetworkFor");
releaseNetworkFor(n.request);
└──else
log(" done");
}
@Override
public void onNetworkRequestWithdrawn(@NonNull NetworkRequest request) {
handleRemoveRequest(request);
}
};
└──else
sendMessageToNetworkProvider(NetworkProvider.CMD_REQUEST_NETWORK, score,servingProviderId, request);
└──sendNetworkChangeBroadcast(DetailedState.CONNECTED);//发送已经连接成功的广播