栏目分类:
子分类:
返回
终身学习网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
终身学习网 > IT > 软件开发 > 后端开发 > Java

android wifi framework创建softap(个人热点)

Java 更新时间:发布时间: 百科书网 趣学号

这是android 11高通车机系统的wifi热点日志流程 

    Line  87696: 05-30 17:04:43.457 10499 10499 V WifiManager: registerSoftApCallback: callback=com.android.settings.wifi.tether.WifiTetherSoftApManager$1@3c702c3, executor=android.os.HandlerExecutor@465c740
    Line  92004: 05-30 17:04:46.216 10499 10499 V WifiManager: unregisterSoftApCallback: callback=com.android.settings.wifi.tether.WifiTetherSoftApManager$1@3c702c3
    Line  93443: 05-30 17:04:47.399  1803  1881 E WifiManager: wqy- startTetheredHotspot
    Line  93444: 05-30 17:04:47.400   809  3327 E WifiService: wqy-startTetheredHotspot
    Line  93446: 05-30 17:04:47.401   809  3327 E WifiService: wqy-startSoftApInternal
    Line  93447: 05-30 17:04:47.401   809  3327 E WifiActiveModeWarden:  wqy-startSoftAp, 237line
    Line  93447: 05-30 17:04:47.401   809  3327 E WifiActiveModeWarden:  wqy-startSoftAp, 237line
    Line  93448: 05-30 17:04:47.401   809  1062 E WifiController:  wqy-processMessageFiltered, CMD_SET_AP, 855line
    Line  93449: 05-30 17:04:47.401   809  1062 E WifiController:  wqy-processMessageFiltered, CMD_SET_AP, msg.arg1 == 1 , 855line
    Line  93450: 05-30 17:04:47.402   809  1062 E WifiActiveModeWarden:  wqy-startSoftApModeManager
    Line  93450: 05-30 17:04:47.402   809  1062 E WifiActiveModeWarden:  wqy-startSoftApModeManager
    Line  93451: 05-30 17:04:47.402   809  1062 D WifiActiveModeWarden: Starting SoftApModeManager config = null
    Line  93455: 05-30 17:04:47.409   809  1062 D SoftApManager:  wqy-SoftApManager:  new SoftApManager
    Line  93455: 05-30 17:04:47.409   809  1062 D SoftApManager:  wqy-SoftApManager:  new SoftApManager
    Line  93455: 05-30 17:04:47.409   809  1062 D SoftApManager:  wqy-SoftApManager:  new SoftApManager
    Line  93455: 05-30 17:04:47.409   809  1062 D SoftApManager:  wqy-SoftApManager:  new SoftApManager
    Line  93456: 05-30 17:04:47.410   809  1062 D SoftApManager:  wqy-SoftApManager: softApConfig is null
    Line  93456: 05-30 17:04:47.410   809  1062 D SoftApManager:  wqy-SoftApManager: softApConfig is null
    Line  93456: 05-30 17:04:47.410   809  1062 D SoftApManager:  wqy-SoftApManager: softApConfig is null
    Line  93457: 05-30 17:04:47.410   809  1062 D SoftApManager:  wqy-SoftApManager: softApConfig is not null
    Line  93457: 05-30 17:04:47.410   809  1062 D SoftApManager:  wqy-SoftApManager: softApConfig is not null
    Line  93457: 05-30 17:04:47.410   809  1062 D SoftApManager:  wqy-SoftApManager: softApConfig is not null
    Line  93458: 05-30 17:04:47.410   809  1062 E WifiApConfigStore:  wqy-randomizeBssidIfUnset
    Line  93459: 05-30 17:04:47.410   809  1062 E WifiApConfigStore:  wqy-randomizeBssidIfUnset, bssid is null, set randoem bssid
    Line  93486: 05-30 17:04:47.482   809  1062 E WifiApConfigStore:  wqy-randomizeBssidIfUnset, bssid is not null
    Line  94639: 05-30 17:04:48.683   809  1062 D SoftApManager: band 1 iface wlan1 country US
    Line  94757: 05-30 17:04:48.871   809  1062 D SoftApManager: Soft AP is started 
    Line  94758: 05-30 17:04:48.873   809  1062 D SoftApManager: SoftAp is ready for use
    Line  94768: 05-30 17:04:48.878   809  1062 D SoftApManager: Resetting connected clients on start
    Line  94774: 05-30 17:04:48.880   809  1062 D SoftApManager: Timeout message scheduled, delay = 600000
    Line  96312: 05-30 17:04:50.182   809  1062 D SoftApManager: Channel switched. Frequency: 2412 Bandwidth: 2
    Line  96377: 05-30 17:04:50.296   809  1062 D SoftApManager: start to init softAP vendor
    Line  96378: 05-30 17:04:50.296   809  1062 I SoftApManager: wlan.sample.vendor_init not set to 1, not set sample values
    Line 118101: 05-30 17:05:08.309 10499 10499 V WifiManager: registerSoftApCallback: callback=com.android.settings.wifi.tether.WifiTetherSoftApManager$1@3c702c3, executor=android.os.HandlerExecutor@625c777
    Line 122649: 05-30 17:05:11.323 10499 10499 V WifiManager: unregisterSoftApCallback: callback=com.android.settings.wifi.tether.WifiTetherSoftApManager$1@3c702c3
    Line 124433: 05-30 17:05:13.102   809  1721 E WifiService: wqy-setWifiEnabled2
    Line 124435: 05-30 17:05:13.104   809  1062 D WifiActiveModeWarden: Starting ClientModeManager
    Line 125164: 05-30 17:05:13.497   809   832 E WifiService: wqy-startScan
    Line 128320: 05-30 17:05:16.200 10499 10499 V WifiManager: registerSoftApCallback: callback=com.android.settings.wifi.tether.WifiTetherSoftApManager$1@fd2bd8c, executor=android.os.HandlerExecutor@8c38cd5
    Line 131584: 05-30 17:05:19.063 10499 10499 V WifiManager: unregisterSoftApCallback: callback=com.android.settings.wifi.tether.WifiTetherSoftApManager$1@fd2bd8c
    Line 137173: 05-30 17:05:25.267   809  1062 D SoftApManager: CMD_ASSOCIATED_STATIONS_CHANGED, Client: 02:7d:6f:f1:50:18 isConnected: true
    Line 137176: 05-30 17:05:25.267   809  1062 D SoftApManager: The connected wifi stations have changed with count: 1: [WifiClient{mMacAddress=02:7d:6f:f1:50:18}]
    Line 137180: 05-30 17:05:25.269   809  1062 D SoftApManager: Timeout message canceled
    Line 206289: 05-30 17:06:29.222 10499 10499 V WifiManager: registerSoftApCallback: callback=com.android.settings.wifi.tether.WifiTetherSoftApManager$1@fd2bd8c, executor=android.os.HandlerExecutor@5dff756
    Line 209320: 05-30 17:06:31.406   809  3327 E WifiService: wqy-startScan
    Line 209968: 05-30 17:06:31.985 10499 10499 V WifiManager: unregisterSoftApCallback: callback=com.android.settings.wifi.tether.WifiTetherSoftApManager$1@fd2bd8c

1、一切调用都从framework的WifiManager开始, 根据注释,这儿的softapconfig有可能是指定的softap,也有可能使用persisted softap

frameworks/base/wifi/java/android/net/wifi/WifiManager.java

    
    @SystemApi
    @RequiresPermission(anyOf = {
            android.Manifest.permission.NETWORK_STACK,
            NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK
    })
    public boolean startTetheredHotspot(@Nullable SoftApConfiguration softApConfig) {
        Log.e(TAG, "wqy- startTetheredHotspot");
        try {
            return mService.startTetheredHotspot(softApConfig);
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
    }

2、然后会调用到WifiService,实际调用都在WifiServiceImpl,会调用到startSoftApInternal

frameworks/opt/net/wifi/service/java/com/android/server/wifi/WifiServiceImpl.java

    
    @Override
    public boolean startTetheredHotspot(@Nullable SoftApConfiguration softApConfig) {
        Log.e(TAG, "wqy-startTetheredHotspot");

        if (!startSoftApInternal(new SoftApModeConfiguration(
                WifiManager.IFACE_IP_MODE_TETHERED, softApConfig,
                mTetheredSoftApTracker.getSoftApCapability()))) {
            mTetheredSoftApTracker.setFailedWhileEnabling();
            return false;
        }

        return true;
    }
    
    private boolean startSoftApInternal(SoftApModeConfiguration apConfig) {
        int uid = Binder.getCallingUid();
        boolean privileged = isSettingsOrSuw(Binder.getCallingPid(), uid);
        mLog.trace("startSoftApInternal uid=% mode=%")
                .c(uid).c(apConfig.getTargetMode()).flush();

        // null wifiConfig is a meaningful input for CMD_SET_AP; it means to use the persistent
        // AP config.
        SoftApConfiguration softApConfig = apConfig.getSoftApConfiguration();
        if (softApConfig != null
                && (!WifiApConfigStore.validateApWifiConfiguration(softApConfig, privileged)
                    || !validateSoftApBand(softApConfig.getBand()))) {
            Log.e(TAG, "Invalid SoftApConfiguration");
            return false;
        }

        mActiveModeWarden.startSoftAp(apConfig);
        return true;
    }

这里有个WifiControll统一处理softap的状态,会在ActiveModeWarden.java里有个内部类的WifiControl, 这儿是设置wifisoftap配置信息的

frameworks/opt/net/wifi/service/java/com/android/server/wifi/ActiveModeWarden.java

    
    public void startSoftAp(SoftApModeConfiguration softApConfig) {
        mWifiController.sendMessage(WifiController.CMD_SET_AP, 1, 0, softApConfig);
    }

                    case CMD_SET_AP:
                        // note: CMD_SET_AP is handled/dropped in ECM mode - will not start here
                        if (msg.arg1 == 1) {
                            startSoftApModeManager((SoftApModeConfiguration) msg.obj);
                        } else {
                            stopSoftApModeManagers(msg.arg2);
                        }
                        break;

这里会创建一个softapmanager, 用于softap热点相关的操作        

ActiveModeManager manager =
                mWifiInjector.makeSoftApManager(listener, callback, softApConfig);

    
    private void startSoftApModeManager(@NonNull SoftApModeConfiguration softApConfig) {
        Log.d(TAG, "Starting SoftApModeManager config = "
                + softApConfig.getSoftApConfiguration());
        Preconditions.checkState(softApConfig.getTargetMode() == IFACE_IP_MODE_LOCAL_ONLY
                || softApConfig.getTargetMode() == IFACE_IP_MODE_TETHERED);

        WifiManager.SoftApCallback callback =
                softApConfig.getTargetMode() == IFACE_IP_MODE_LOCAL_ONLY
                        ? mLohsCallback : mSoftApCallback;
        SoftApListener listener = new SoftApListener();
        ActiveModeManager manager =
                mWifiInjector.makeSoftApManager(listener, callback, softApConfig);
        listener.setActiveModeManager(manager);
        manager.start();
        manager.setRole(getRoleForSoftApIpMode(softApConfig.getTargetMode()));
        mActiveModeManagers.add(manager);
    }

frameworks/opt/net/wifi/service/java/com/android/server/wifi/WifiInjector.java

    
    public SoftApManager makeSoftApManager(@NonNull ActiveModeManager.Listener listener,
                                           @NonNull WifiManager.SoftApCallback callback,
                                           @NonNull SoftApModeConfiguration config) {
        return new SoftApManager(mContext, mWifiHandlerThread.getLooper(),
                mFrameworkFacade, mWifiNative, mCountryCode.getCountryCode(), listener, callback,
                mWifiApConfigStore, config, mWifiMetrics, mSarManager, mWifiDiagnostics);
    }

这里当softapconfig不为null,并且bssid为null的时候,会自己创建一个随机的mac

frameworks/opt/net/wifi/service/java/com/android/server/wifi/SoftApManager.java

SoftApManager() {

if (softApConfig != null) {

mIsRandomizeBssid = softApConfig.getBssid() == null;

softApConfig = mWifiApConfigStore.randomizeBssidIfUnset(mContext, softApConfig);

}

}

这里会根据WIFI_UID获取签名证书相关的数据,并进行HMacSHA256的加密,也不能说加密,

然后用ssid值进行进行hash运算,经过一系列掩码运算,获取字节序里的6位16进制数作为mac地

址。

frameworks/opt/net/wifi/service/java/com/android/server/wifi/WifiApConfigStore.java

    SoftApConfiguration randomizeBssidIfUnset(Context context, SoftApConfiguration config) {
        Log.e(TAG, " wqy--randomizeBssidIfUnset");
        SoftApConfiguration.Builder configBuilder = new SoftApConfiguration.Builder(config);
        if (config.getBssid() == null && context.getResources().getBoolean(
                R.bool.config_wifi_ap_mac_randomization_supported)) {
            
            Log.e(TAG, " wqy-randomizeBssidIfUnset, bssid is null, set randoem bssid");
            MacAddress macAddress = mMacAddressUtil.calculatePersistentMac(config.getSsid(),
                    mMacAddressUtil.obtainMacRandHashFunctionForSap(Process.WIFI_UID));
            if (macAddress == null) {
                Log.e(TAG, "Failed to calculate MAC from SSID. "
                        + "Generating new random MAC instead.");
                macAddress = MacAddressUtils.createRandomUnicastAddress();
            }
            configBuilder.setBssid(macAddress);
        }
        Log.e(TAG, " wqy-randomizeBssidIfUnset, bssid is not null");
        return configBuilder.build();
    }

    
    public MacAddress calculatePersistentMac(String key, Mac hashFunction) {
        if (key == null || hashFunction == null) {
            return null;
        }

        Log.e(TAG, "wqy-calculatePersistentMac, key is " + key);
        byte[] hashedBytes;
        try {
            hashedBytes = hashFunction.doFinal(key.getBytes(StandardCharsets.UTF_8));
            Log.e(TAG, "wqy-calculatePersistentMac, hashedBytes is " + hashedBytes.toString());
        } catch (ProviderException | IllegalStateException e) {
            Log.e(TAG, "Failure in calculatePersistentMac", e);
            return null;
        }
        ByteBuffer bf = ByteBuffer.wrap(hashedBytes);
        Log.e(TAG, "wqy-calculatePersistentMac, bf = " + bf.toString());
        long longFromSsid = bf.getLong();
        
        
        longFromSsid &= MAC_ADDRESS_VALID_LONG_MASK;
        longFromSsid |= MAC_ADDRESS_LOCALLY_ASSIGNED_MASK;
        longFromSsid &= ~MAC_ADDRESS_MULTICAST_MASK;
        bf.clear();
        bf.putLong(0, longFromSsid);
        Log.e(TAG, "wqy-calculatePersistentMac, longFromSsid " + Long.toHexString(longFromSsid));

        // MacAddress.fromBytes requires input of length 6, which is obtained from the
        // last 6 bytes from the generated long.
        MacAddress macAddress = MacAddress.fromBytes(Arrays.copyOfRange(bf.array(), 2, 8));
        return macAddress;
    }
    private Mac obtainMacRandHashFunctionInternal(int uid, String alias) {
        try {
            KeyStore keyStore = AndroidKeyStoreProvider.getKeyStoreForUid(uid);
            // tries to retrieve the secret, and generate a new one if it's unavailable.
            Key key = keyStore.getKey(alias, null);
            if (key == null) {
                key = generateAndPersistNewMacRandomizationSecret(uid, alias);
            }
            if (key == null) {
                Log.e(TAG, "Failed to generate secret for " + alias);
                return null;
            }
            Mac result = Mac.getInstance("HmacSHA256");
            result.init(key);
            return result;
        } catch (KeyStoreException | NoSuchAlgorithmException | InvalidKeyException
                | UnrecoverableKeyException | NoSuchProviderException e) {
            Log.e(TAG, "Failure in obtainMacRandHashFunction", e);
            return null;
        }
    }

转载请注明:文章转载自 www.051e.com
本文地址:http://www.051e.com/it/959795.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 ©2023-2025 051e.com

ICP备案号:京ICP备12030808号