Android wifi连接和获取IP分析

wifi 连接&获取IP 流程图

Android wifi连接和获取IP分析_第1张图片

代码流程分析

一、关联阶段

1. WifiSettings.submit – > WifiManager

  • WifiSettings 干的事情比较简单,当在dialog完成ssid 以及密码填充后,直接call WifiManager save 即可
  • WifiManager 收到Save 之后,就开始了wifi 连接之旅
packages/apps/Settings/src/com/android/settings/wifi/WifiSettings.java
void submit(WifiConfigController configController){
	... 
	mWifiManager.save(config, mSaveListener);
}

2. WifiSettings.submit – > WifiManager

  • WifiManager 这里相当于传达室,信件到了,然后按个铃铛,把信息传达给WifiSerivceImpl
  • 主要是将SAVE_NETWORK 给到 WifiService(WifiServiceImpl 是真正干活的)
frameworks/base/wifi/java/android/net/wifi/WifiManager.java
public void save(WifiConfiguration config, ActionListener listener) {
    getChannel().sendMessage(SAVE_NETWORK, 0, putListener(listener), config);
}

3. WifiServiceImpl --> WifiStateMachine

  • WifiServiceImpl又将 SAVE_NETWORK 送到中级cmd处理站: WifiStateMachine
  • 这里扩展一下,如我们的移除热点(其实也通过Settings 将remove cmd 送到WifiServieImpl进行下一步传输)
frameworks/opt/net/wifi/service/java/com/android/server/wifi/WifiServiceImpl.java
private class ClientHandler extends WifiHandler {
	case WifiManager.SAVE_NETWORK:
	{
		mWifiStateMachine.sendMessage(Message.obtain(msg));
}
}

4. WifiStateMachine–> WifiStateMachine

  • 从save network 连接热点
  • 又一次转化, 转为CMD_START_CONNECT送到内部状态机(ConnectModeState )处理
  • 送到WifiNative 进行处理Connect,回顾以上(Android 基本套路大框架还是不变的: apps – services – native – drv(kernel))
frameworks/opt/net/wifi/service/java/com/android/server/wifi/WifiStateMachine.java
public void startConnectToNetwork(int networkId, int uid, String bssid) {
	sendMessage(CMD_START_CONNECT, networkId, uid, bssid);
}

class ConnectModeState extends State {
	case WifiManager.SAVE_NETWORK:
	   startConnectToNetwork(netId, message.sendingUid, SUPPLICANT_BSSID_ANY);// 连接热点
	case CMD_START_CONNECT:
		mWifiNative.connectToNetwork(mInterfaceName, config); // 转到wifiNative 进行connect 
}

5. WifiNative–> SupplicantStaIfaceHal --> WifMonitor

  • SupplicantStaIfaceHal 添加网络,select (实则将connect 传给到 wpa_supplicant)
  • wpa_supplicant完成一系列与路由器的之间的beacon帧(probe、assoc、4way-handshake 、group-handshake)后,再能取到路由器的颁发的认可证(既是拿到最后的compelted)
  • 最后,通过wifiMonitor上报wpa_supplicant 任务已完成,既是完成连接的第一阶段(关联阶段)
  • WiFiMonitor上报的事件既是,NETWORK_CONNECTION_EVENT (又一次回到WifiStateMachine)
frameworks/opt/net/wifi/service/java/com/android/server/wifi/SupplicantStaIfaceHal.java
 public boolean connectToNetwork(@NonNull String ifaceName, @NonNull WifiConfiguration config)
{
	  Pair pair =
                      addNetworkAndSaveConfig(ifaceName, config);
	 networkHandle.select(); 
}

public void onStateChanged(int newState, byte[/* 6 */] bssid, int id,ArrayList ssid) {
	if (newSupplicantState == SupplicantState.COMPLETED){
		mWifiMonitor.broadcastNetworkConnectionEvent(mIfaceName, getCurrentNetworkId(mIfaceName), bssidStr);
	}
}

二、获取IP 阶段

6. WifMonitor --> WifiStateMachine

  • 由5的NETWORK_CONNECTION_EVENT 事件上报,又一次回到了WifiStateMachine
  • 而且还是在ConnectModeState 状态机里,为啥?(又回到这里)
  • 因为,1~5步只完成了关联,还有最后一步(要获取IP),拿到IP后才能算是一个有证的男人,
  • 革命尚未成功,苦逼仍要继续(且看下面是如何获取IP …)
  • 先是,广而告知(已取得真经),然后立马切换到mObtainingIpState状态机
frameworks/opt/net/wifi/service/java/com/android/server/wifi/WifiStateMachine.java
class ConnectModeState extends State {
	case WifiMonitor.NETWORK_CONNECTION_EVENT:
		.... 
		sendNetworkStateChangeBroadcast(mLastBssid);
		transitionTo(mObtainingIpState);
}

7. WifiStateMachine.ObtainingIpState

  • ObtainingIpState 获取IP 的流程中规中矩,先是update 系统的State
  • 停掉IpClient (自从Android N 后,获取IP已废弃了dhcpd,扶持了另一位的IpClient 、IpManager来完成自己的事业 )
  • 清掉bssid以避免其影响到漫游导致断链
  • Ipclient 开始接管舞台(启动获取IP)
frameworks/opt/net/wifi/service/java/com/android/server/wifi/WifiStateMachine.java
class ObtainingIpState extends State {
	... 
	setNetworkDetailedState(DetailedState.OBTAINING_IPADDR);
	clearTargetBssid("ObtainingIpAddress");
	stopIpClient();
	mIpClient.setHttpProxy(currentConfig.getHttpProxy());
	IpClient.buildProvisioningConfiguration(); 
	.... 
}

你可能感兴趣的:(Android,wifi,android,wifi)