Android 12 WiFi 连接流程

packages/apps/Settings/src/com/android/settings/wifi/WifiSettings.java
packages/modules/Wifi/framework/java/android/net/wifi/WifiManager.java
packages/modules/Wifi/service/java/com/android/server/wifi/WifiServiceImpl.java
packages/modules/Wifi/service/java/com/android/server/wifi/ConnectHelper.java
packages/modules/Wifi/service/java/com/android/server/wifi/ConcreteClientModeManager.java
packages/modules/Wifi/service/java/com/android/server/wifi/ClientModeImpl.java
packages/modules/Wifi/service/java/com/android/server/wifi/WifiNative.java
packages/modules/Wifi/service/java/com/android/server/wifi/SupplicantStaIfaceHal.java
packages/modules/Wifi/service/java/com/android/server/wifi/SupplicantStaNetworkHal.java

WifiSettings.java =>onSubmit(WifiDialog2 dialog):
                    -if (dialogMode == WifiConfigUiBase2.MODE_MODIFY)
                       --if (config == null)
                           Toast.makeText(getContext(), R.string.wifi_failed_save_message,Toast.LENGTH_SHORT).show();//config为null,弹出Toast
                       --else
                           mWifiManager.save(config, mSaveListener);//修改信息后,将信息进行保存
                    -else if (dialogMode == WifiConfigUiBase2.MODE_CONNECT || (dialogMode == WifiConfigUiBase2.MODE_VIEW && wifiEntry.canConnect()))
                       --if (config == null)
                           connect(wifiEntry, false /* editIfNoConfig */,false /* fullScreenEdit*/);
                       --else
                          mWifiManager.connect(config, new WifiConnectActionListener());//调用connect进行连接
-->WifiManager.java=>connect(@NonNull WifiConfiguration config, @Nullable ActionListener listener):connectInternal(config, WifiConfiguration.INVALID_NETWORK_ID, listener);
                       ->connectInternal(@Nullable WifiConfiguration config, int networkId,@Nullable ActionListener listener):mService.connect(config, networkId, listenerProxy);
------>WifiServiceImpl.java=> connect(WifiConfiguration config, int netId, @Nullable IActionListener callback):
                              -mMakeBeforeBreakManager.stopAllSecondaryTransientClientModeManagers(() ->mConnectHelper.connectToNetwork(result, wrapper, uid));
-------->ConnectHelper.java=>connectToNetwork(@NonNull NetworkUpdateResult result,@NonNull ActionListenerWrapper wrapper,int callingUid):
                             -connectToNetwork(mActiveModeWarden.getPrimaryClientModeManager(), result, wrapper, callingUid);//mActiveModeWarden.getPrimaryClientModeManager()状态机获取当前状态
                             ->connectToNetwork(@NonNull ClientModeManager clientModeManager,@NonNull NetworkUpdateResult result,@NonNull ActionListenerWrapper wrapper,int callingUid):
                               -mWifiConfigManager.updateBeforeConnect(netId, callingUid);
                               -clientModeManager.connectNetwork(result, wrapper, callingUid);//此时状态机应在ConnectModeState
---------->ConcreteClientModeManager.java=>connectNetwork(NetworkUpdateResult result, ActionListenerWrapper wrapper,int callingUid):-getClientMode().connectNetwork(result, wrapper, callingUid);
                                           ->getClientMode():
                                             -if (mClientModeImpl != null)
                                                return mClientModeImpl;//此时应在ClientModeImpl
                                             -if (mScanOnlyModeImpl != null)
                                                return mScanOnlyModeImpl;
                                             return mDefaultClientModeManager
------------>ClientModeImpl.java=>connectNetwork(NetworkUpdateResult result, ActionListenerWrapper wrapper,int callingUid):
                                  -Message message =obtainMessage(CMD_CONNECT_NETWORK, new ConnectNetworkMessage(result, wrapper));//将CMD_CONNECT_NETWORK加入到消息队列
                                  -sendMessage(message);
                                  ->ClientModeImpl$ConnectableState->processMessage(Message message):
                                    -case CMD_CONNECT_NETWORK:
                                      -connectToUserSelectNetwork(netId, message.sendingUid, result.hasCredentialChanged());
                                      -cnm.listener.sendSuccess();
                                  ->connectToUserSelectNetwork(int netId, int uid, boolean forceReconnect):
                                    -startConnectToNetwork(netId, uid, SUPPLICANT_BSSID_ANY);
                                  ->startConnectToNetwork(int networkId, int uid, String bssid):sendMessage(CMD_START_CONNECT, networkId, uid, bssid);//发送CMD_START_CONNECT消息
                                  ->ClientModeImpl$ConnectableState->processMessage(Message message):
                                    -case CMD_START_CONNECT:
                                      -mLastScanRssi = mWifiConfigManager.findScanRssi(netId,
                                      -mWifiHealthMonitor.getScanRssiValidTimeMs());
                                      -mWifiScoreCard.noteConnectionAttempt(mWifiInfo, mLastScanRssi, config.SSID);
                                      -mWifiBlocklistMonitor.setAllowlistSsids(config.SSID, Collections.emptyList());
                                      -mWifiBlocklistMonitor.updateFirmwareRoamingConfiguration(Set.of(config.SSID));
                                      -updateWifiConfigOnStartConnection(config, bssid);
                                      -reportConnectionAttemptStart(config, mTargetBssid,WifiMetricsProto.ConnectionEvent.ROAM_UNRELATED);
                                      -String currentMacAddress = mWifiNative.getMacAddress(mInterfaceName);//从底层获取MAC地址
                                      -mWifiInfo.setMacAddress(currentMacAddress);//设置MAC地址
                                      -mTargetWifiConfiguration = config;
                                      -mNetworkNotFoundEventCount = 0;
                                      -if (config.isFilsSha256Enabled() || config.isFilsSha384Enabled())
                                         boolean isIpClientStarted = startIpClient(config, true);
                                        -if (isIpClientStarted)
                                           mIpClientWithPreConnection = true;
                                           break;
                                      -connectToNetwork(config);//连接
                                  ->connectToNetwork(WifiConfiguration config):
                                    -if ((config != null) && mWifiNative.connectToNetwork(mInterfaceName, config))
                                      -transitionTo(mL2ConnectingState);
                                    -else
                                      -stopIpClient();
-------------->WifiNative.java=>connectToNetwork(@NonNull String ifaceName, WifiConfiguration configuration):
                                -mWifiCondManager.abortScan(ifaceName);
                                -return mSupplicantStaIfaceHal.connectToNetwork(ifaceName, configuration);
---------------->SupplicantStaIfaceHal.java=> connectToNetwork(@NonNull String ifaceName, @NonNull WifiConfiguration config):
                                              -Pair pair = addNetworkAndSaveConfig(ifaceName, config);
                                              -->PairaddNetworkAndSaveConfig(@NonNull String ifaceName, WifiConfiguration config):
                                                 -SupplicantStaNetworkHal network = addNetwork(ifaceName);
                                              -->SupplicantStaNetworkHal addNetwork(@NonNull String ifaceName):
                                                 -ISupplicantStaIface iface = checkSupplicantStaIfaceAndLogFailure(ifaceName, methodStr);
                                                 -iface.addNetwork((SupplicantStatus status,ISupplicantNetwork network) -> {
                                                    if (checkStatusAndLogFailure(status, methodStr)) {
                                                      newNetwork.value = network;
                                                    }
                                                  });//通过HIDL进入Supplicant
                                              -networkHandle.select()
------------------>SupplicantStaNetworkHal.java=>select():
                                                 -SupplicantStatus status = mISupplicantStaNetwork.select();

addNetwork:                
external/wpa_supplicant_8/wpa_supplicant/hidl/1.4/sta_iface.cpp
external/wpa_supplicant_8/wpa_supplicant/wpa_supplicant.c
external/wpa_supplicant_8/wpa_supplicant/config.c
external/wpa_supplicant_8/wpa_supplicant/notify.c
external/wpa_supplicant_8/wpa_supplicant/hidl/1.4/hidl.cpp

wpa_supplicant_add_network->wpa_config_add_network & wpas_notify_network_added->wpas_hidl_register_network

sta_iface.cpp=> StaIface::addNetworkInternal() =>struct wpa_ssid *ssid = wpa_supplicant_add_network(wpa_s);
-->wpa_supplicant.c=>struct wpa_ssid * wpa_supplicant_add_network(struct wpa_supplicant *wpa_s)
                     =>ssid = wpa_config_add_network(wpa_s->conf);
                     =>wpas_notify_network_added(wpa_s, ssid);
---->config.c=>struct wpa_ssid * wpa_config_add_network(struct wpa_config *config) =>wpa_config_update_prio_list(config);
               ==>int wpa_config_update_prio_list(struct wpa_config *config) =>wpa_config_add_prio_network(config, ssid)
               ==>int wpa_config_add_prio_network(struct wpa_config *config,struct wpa_ssid *ssid)
---->notify.c=>void wpas_notify_network_added(struct wpa_supplicant *wpa_s,struct wpa_ssid *ssid)
               =>if (!ssid->p2p_group && wpa_s->global->p2p_group_formation != wpa_s)
		 wpas_dbus_register_network(wpa_s, ssid);
               #ifdef CONFIG_HIDL
		 wpas_hidl_register_network(wpa_s, ssid);
               #endif
------>hidl.cpp=>int wpas_hidl_register_network(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid)
                 =>return hidl_manager->registerNetwork(wpa_s, ssid);

select:
external/wpa_supplicant_8/wpa_supplicant/hidl/1.4/sta_network.cpp
external/wpa_supplicant_8/wpa_supplicant/wpa_supplicant.c
/external/wpa_supplicant_8/wpa_supplicant/events.c
external/wpa_supplicant_8/src/rsn_supp/wpa.c

select->selectInternal->wpa_supplicant_select_network->wpa_supplicant_fast_associate->wpas_select_network_from_last_scan->wpa_supplicant_pick_network->wpa_supplicant_select_bss->wpa_supplicant_connect->wpa_supplicant_associate->wpas_start_assoc_cb->wpa_sm_set_assoc_wpa_ie

sta_network.cpp=>Return StaNetwork::select(select_cb _hidl_cb)
                 return validateAndCall(this, SupplicantStatusCode::FAILURE_NETWORK_INVALID,&StaNetwork::selectInternal, _hidl_cb);
               =>SupplicantStatus StaNetwork::selectInternal()
                 wpa_supplicant_select_network(wpa_s, wpa_ssid);
-->wpa_supplicant.c=>void wpa_supplicant_select_network(struct wpa_supplicant *wpa_s,struct wpa_ssid *ssid)
                     wpa_supplicant_fast_associate(wpa_s)
---->events.c=>int wpa_supplicant_fast_associate(struct wpa_supplicant *wpa_s)
               return wpas_select_network_from_last_scan(wpa_s, 0, 1);
             =>static int wpas_select_network_from_last_scan(struct wpa_supplicant *wpa_s,int new_scan, int own_request)
               selected = wpa_supplicant_pick_network(wpa_s, &ssid);
               wpa_supplicant_connect(wpa_s, selected, ssid)
             =>struct wpa_bss * wpa_supplicant_pick_network(struct wpa_supplicant *wpa_s,struct wpa_ssid **selected_ssid)
               if (next_ssid && next_ssid->priority == wpa_s->conf->pssid[prio]->priority) {
                   selected = wpa_supplicant_select_bss(wpa_s, next_ssid, selected_ssid, 1);
                   if (selected)
                       break;
               }
               selected = wpa_supplicant_select_bss(wpa_s, wpa_s->conf->pssid[prio],selected_ssid, 0);
               if (selected)
                   break;
             =>static struct wpa_bss *wpa_supplicant_select_bss(struct wpa_supplicant *wpa_s,struct wpa_ssid *group,struct wpa_ssid **selected_ssid,int only_first_ssid)
               ssid = wpa_scan_res_match(wpa_s, i, bss, group,only_first_ssid, 0)
             =>int wpa_supplicant_connect(struct wpa_supplicant *wpa_s,struct wpa_bss *selected,struct wpa_ssid *ssid)
               wpa_supplicant_associate(wpa_s, selected, ssid);
------>wpa_supplicant.c=>void wpa_supplicant_associate(struct wpa_supplicant *wpa_s,struct wpa_bss *bss, struct wpa_ssid *ssid)
                         radio_add_work(wpa_s, bss ? bss->freq : 0, "connect", 1,wpas_start_assoc_cb, cwork)
                       =>static void wpas_start_assoc_cb(struct wpa_radio_work *work, int deinit)
                         wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
-------->wpa.c=>int wpa_sm_set_assoc_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)

你可能感兴趣的:(Android,Wifi,模块,android,java)