Wifi模块—源码分析获取IP地址(Android P)

本文详细分析了在Android P系统中,从底层到应用层的Wifi连接后获取IP地址的流程。从WifiMonitor监听事件触发,经由WifiStateMachine处理,再到设置网络状态和发送广播,最后在IpClient中完成IP配置。主要涉及WifiMonitor、WifiStateMachine、WifiTracker、WifiEnabler和IpClient等组件的交互过程。
摘要由CSDN通过智能技术生成

一 前言

        上一篇分析了连接AP的过程,当底层完成wifi连接,便会事件通知WifiMonitor,WifiMonitor监听到该事件后发送消息NETWORK_CONNECTION_EVENT。

                                       

二 代码具体流程

 1 frameworks/opt/net/wifi/service/java/com/android/server/wifi/WifiMonitor.java

 底层完成wifi连接,便会事件通知WifiMonitor,WifiMonitor监听到该事件后发送消息NETWORK_CONNECTION_EVENT。

/**
* Broadcast the network connection event to all the handlers registered for this event.
*
* @param iface Name of iface on which this occurred.
* @param networkId ID of the network in wpa_supplicant.
* @param bssid BSSID of the access point.
*/
public void broadcastNetworkConnectionEvent(String iface, int networkId, String bssid) {
    sendMessage(iface, NETWORK_CONNECTION_EVENT, networkId, 0, bssid);
}

 

2 frameworks/opt/net/wifi/service/java/com/android/server/wifi/WifiStateMachine.java

WifiStateMachine此时处于DisconnectedState,该状态未能处理该消息,抛给它的父状态ConnectModeState处理。

case WifiMonitor.NETWORK_CONNECTION_EVENT:
    if (mVerboseLoggingEnabled) log("Network connection established");
    mLastNetworkId = message.arg1;
    mWifiConfigManager.clearRecentFailureReason(mLastNetworkId);
    mLastBssid = (String) message.obj;
    reasonCode = message.arg2;
    // TODO: This check should not be needed after WifiStateMachinePrime refactor.
    // Currently, the last connected network configuration is left in
    // wpa_supplicant, this may result in wpa_supplicant initiating connection
    // to it after a config store reload. Hence the old network Id lookups may not
    // work, so disconnect the network and let network selector reselect a new
    // network.
    config = getCurrentWifiConfiguration();
    if (config != null) {
        mWifiInfo.setBSSID(mLastBssid);
        mWifiInfo.setNetworkId(mLastNetworkId);
        mWifiInfo.setMacAddress(mWifiNative.getMacAddress(mInterfaceName));

        ScanDetailCache scanDetailCache =
                mWifiConfigManager.getScanDetailCacheForNetwork(config.networkId);
        if (scanDetailCache != null && mLastBssid != null) {
            ScanResult scanResult = scanDetailCache.getScanResult(mLastBssid);
            if (scanResult != null) {
                mWifiInfo.setFrequency(scanResult.frequency);
            }
        }
        mWifiConnectivityManager.trackBssid(mLastBssid, true, reasonCode);
        // We need to get the updated pseudonym from supplicant for EAP-SIM/AKA/AKA'
        if (config.enterpriseConfig != null
                && TelephonyUtil.isSimEapMethod(
                        config.enterpriseConfig.getEapMethod())) {
            String anonymousIdentity =
                    mWifiNative.getEapAnonymousIdentity(mInterfaceName);
            if (anonymousIdentity != null) {
                config.enterpriseConfig.setAnonymousIdentity(anonymousIdentity);
            } else {
                Log.d(TAG, "Failed to get updated anonymous identity"
                        + " from supplicant, reset it in WifiConfiguration.");
                config.enterpriseConfig.setAnonymousIdentity(null);
            }
            mWifiConfigManager.addOrUpdateNetwork(config, Process.WIFI_UID);
        }
        sendNetworkStateChangeBroadcast(mLastBssid);
        transitionTo(mObtainingIpState);
    } else {
        logw("Connected to unknown networkId " + mLastNetworkId
                + ", disconnecting...");
        sendMessage(CMD_DISCONNECT);
    }
    break;

可以看到一些操作,将ap的相关属性值赋给mWifiInfo。

                         mWifiInfo.setBSSID(mLastBssid)
                         mWifiInfo.setNetworkId(mLastNetworkId)
                         mWifiInfo.setMacAddress(mWifiNative.getMacAddress(mInterfaceName))

更新Wifi的状态信息NetworkInfo,mWifiConfigManager.addOrUpdateNetwork(config, Process.WIFI_UID)

并调用sendNetworkStateChangeBroadcast(mLastBssid)。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值