From 1c3c829525be19bb14ae31a0bd978b7a1b0f00cd Mon Sep 17 00:00:00 2001 From: Patrik Fimml Date: Tue, 10 Sep 2019 15:56:49 +0200 Subject: SoftApManager: Use SoftApModeConfiguration consistently - Use SoftApModeConfiguration directly instead of unpacking it. - Remove unneeded parameter since state machine is an inner class. - Remove null check in dump() since constructor guarantees non-null. - Split out setCountryCode() to make logic easier to follow. Preparation for follow-up changes where I'll modify SoftApManager logic and add a field to SoftApModeConfiguration. Bug: 132705022 Test: atest SoftApManagerTest Change-Id: I214d17a6f65bee15db6f7352795b04d2b3173edb --- .../com/android/server/wifi/SoftApManager.java | 117 +++++++++++---------- 1 file changed, 60 insertions(+), 57 deletions(-) (limited to 'service') diff --git a/service/java/com/android/server/wifi/SoftApManager.java b/service/java/com/android/server/wifi/SoftApManager.java index c6be91760..b02ec2402 100644 --- a/service/java/com/android/server/wifi/SoftApManager.java +++ b/service/java/com/android/server/wifi/SoftApManager.java @@ -84,8 +84,8 @@ public class SoftApManager implements ActiveModeManager { private final WifiMetrics mWifiMetrics; - private final int mMode; - private WifiConfiguration mApConfig; + @NonNull + private SoftApModeConfiguration mApConfig; private int mReportedFrequency = -1; private int mReportedBandwidth = -1; @@ -141,12 +141,10 @@ public class SoftApManager implements ActiveModeManager { mCountryCode = countryCode; mCallback = callback; mWifiApConfigStore = wifiApConfigStore; - mMode = apConfig.getTargetMode(); - WifiConfiguration config = apConfig.getWifiConfiguration(); - if (config == null) { - mApConfig = mWifiApConfigStore.getApConfiguration(); - } else { - mApConfig = config; + mApConfig = apConfig; + if (mApConfig.getWifiConfiguration() == null) { + WifiConfiguration wifiConfig = mWifiApConfigStore.getApConfiguration(); + mApConfig = new SoftApModeConfiguration(mApConfig.getTargetMode(), wifiConfig); } mWifiMetrics = wifiMetrics; mSarManager = sarManager; @@ -155,10 +153,10 @@ public class SoftApManager implements ActiveModeManager { } /** - * Start soft AP with the supplied config. + * Start soft AP, as configured in the constructor. */ public void start() { - mStateMachine.sendMessage(SoftApStateMachine.CMD_START, mApConfig); + mStateMachine.sendMessage(SoftApStateMachine.CMD_START); } /** @@ -183,7 +181,7 @@ public class SoftApManager implements ActiveModeManager { } public int getIpMode() { - return mMode; + return mApConfig.getTargetMode(); } /** @@ -195,15 +193,12 @@ public class SoftApManager implements ActiveModeManager { pw.println("current StateMachine mode: " + getCurrentStateName()); pw.println("mApInterfaceName: " + mApInterfaceName); pw.println("mIfaceIsUp: " + mIfaceIsUp); - pw.println("mMode: " + mMode); pw.println("mSoftApCountryCode: " + mCountryCode); - if (mApConfig != null) { - pw.println("mApConfig.SSID: " + mApConfig.SSID); - pw.println("mApConfig.apBand: " + mApConfig.apBand); - pw.println("mApConfig.hiddenSSID: " + mApConfig.hiddenSSID); - } else { - pw.println("mApConfig: null"); - } + pw.println("mApConfig.targetMode: " + mApConfig.getTargetMode()); + WifiConfiguration wifiConfig = mApConfig.getWifiConfiguration(); + pw.println("mApConfig.wifiConfiguration.SSID: " + wifiConfig.SSID); + pw.println("mApConfig.wifiConfiguration.apBand: " + wifiConfig.apBand); + pw.println("mApConfig.wifiConfiguration.hiddenSSID: " + wifiConfig.hiddenSSID); pw.println("mNumAssociatedStations: " + mNumAssociatedStations); pw.println("mTimeoutEnabled: " + mTimeoutEnabled); pw.println("mReportedFrequency: " + mReportedFrequency); @@ -241,49 +236,59 @@ public class SoftApManager implements ActiveModeManager { } intent.putExtra(WifiManager.EXTRA_WIFI_AP_INTERFACE_NAME, mApInterfaceName); - intent.putExtra(WifiManager.EXTRA_WIFI_AP_MODE, mMode); + intent.putExtra(WifiManager.EXTRA_WIFI_AP_MODE, mApConfig.getTargetMode()); mContext.sendStickyBroadcastAsUser(intent, UserHandle.ALL); } - /** - * Start a soft AP instance with the given configuration. - * @param config AP configuration - * @return integer result code - */ - private int startSoftAp(WifiConfiguration config) { - if (config == null || config.SSID == null) { - Log.e(TAG, "Unable to start soft AP without valid configuration"); - return ERROR_GENERIC; - } - - Log.d(TAG, "band " + config.apBand + " iface " - + mApInterfaceName + " country " + mCountryCode); - - // Setup country code + private int setCountryCode() { + int band = mApConfig.getWifiConfiguration().apBand; if (TextUtils.isEmpty(mCountryCode)) { - if (config.apBand == WifiConfiguration.AP_BAND_5GHZ) { + if (band == WifiConfiguration.AP_BAND_5GHZ) { // Country code is mandatory for 5GHz band. - Log.e(TAG, "Invalid country code, required for setting up " - + "soft ap in 5GHz"); + Log.e(TAG, "Invalid country code, required for setting up soft ap in 5GHz"); return ERROR_GENERIC; } // Absence of country code is not fatal for 2Ghz & Any band options. - } else if (!mWifiNative.setCountryCodeHal( + return SUCCESS; + } + + if (!mWifiNative.setCountryCodeHal( mApInterfaceName, mCountryCode.toUpperCase(Locale.ROOT))) { - if (config.apBand == WifiConfiguration.AP_BAND_5GHZ) { + if (band == WifiConfiguration.AP_BAND_5GHZ) { // Return an error if failed to set country code when AP is configured for // 5GHz band. - Log.e(TAG, "Failed to set country code, required for setting up " - + "soft ap in 5GHz"); + Log.e(TAG, "Failed to set country code, required for setting up soft ap in 5GHz"); return ERROR_GENERIC; } // Failure to set country code is not fatal for 2Ghz & Any band options. } + return SUCCESS; + } + + /** + * Start a soft AP instance as configured. + * + * @return integer result code + */ + private int startSoftAp() { + WifiConfiguration config = mApConfig.getWifiConfiguration(); + if (config == null || config.SSID == null) { + Log.e(TAG, "Unable to start soft AP without valid configuration"); + return ERROR_GENERIC; + } + + Log.d(TAG, "band " + config.apBand + " iface " + + mApInterfaceName + " country " + mCountryCode); + + int result = setCountryCode(); + if (result != SUCCESS) { + return result; + } // Make a copy of configuration for updating AP band and channel. WifiConfiguration localConfig = new WifiConfiguration(config); - int result = ApConfigUtil.updateApChannelConfig( + result = ApConfigUtil.updateApChannelConfig( mWifiNative, mCountryCode, mWifiApConfigStore.getAllowed2GChannel(), localConfig); @@ -389,7 +394,7 @@ public class SoftApManager implements ActiveModeManager { } updateApState(WifiManager.WIFI_AP_STATE_ENABLING, WifiManager.WIFI_AP_STATE_DISABLED, 0); - int result = startSoftAp((WifiConfiguration) message.obj); + int result = startSoftAp(); if (result != SUCCESS) { int failureReason = WifiManager.SAP_START_FAILURE_GENERAL; if (result == ERROR_NO_CHANNEL) { @@ -491,8 +496,8 @@ public class SoftApManager implements ActiveModeManager { } else { Log.e(TAG, "SoftApCallback is null. Dropping NumClientsChanged event."); } - mWifiMetrics.addSoftApNumAssociatedStationsChangedEvent(mNumAssociatedStations, - mMode); + mWifiMetrics.addSoftApNumAssociatedStationsChangedEvent( + mNumAssociatedStations, mApConfig.getTargetMode()); if (mNumAssociatedStations == 0) { scheduleTimeoutMessage(); @@ -518,7 +523,7 @@ public class SoftApManager implements ActiveModeManager { // the interface was up, but goes down sendMessage(CMD_INTERFACE_DOWN); } - mWifiMetrics.addSoftApUpChangedEvent(isUp, mMode); + mWifiMetrics.addSoftApUpChangedEvent(isUp, mApConfig.getTargetMode()); } @Override @@ -559,7 +564,7 @@ public class SoftApManager implements ActiveModeManager { cancelTimeoutMessage(); // Need this here since we are exiting |Started| state and won't handle any // future CMD_INTERFACE_STATUS_CHANGED events after this point - mWifiMetrics.addSoftApUpChangedEvent(false, mMode); + mWifiMetrics.addSoftApUpChangedEvent(false, mApConfig.getTargetMode()); updateApState(WifiManager.WIFI_AP_STATE_DISABLED, WifiManager.WIFI_AP_STATE_DISABLING, 0); @@ -571,14 +576,12 @@ public class SoftApManager implements ActiveModeManager { } private void updateUserBandPreferenceViolationMetricsIfNeeded() { - boolean bandPreferenceViolated = false; - if (mApConfig.apBand == WifiConfiguration.AP_BAND_2GHZ - && ScanResult.is5GHz(mReportedFrequency)) { - bandPreferenceViolated = true; - } else if (mApConfig.apBand == WifiConfiguration.AP_BAND_5GHZ - && ScanResult.is24GHz(mReportedFrequency)) { - bandPreferenceViolated = true; - } + int band = mApConfig.getWifiConfiguration().apBand; + boolean bandPreferenceViolated = + (band == WifiConfiguration.AP_BAND_2GHZ + && ScanResult.is5GHz(mReportedFrequency)) + || (band == WifiConfiguration.AP_BAND_5GHZ + && ScanResult.is24GHz(mReportedFrequency)); if (bandPreferenceViolated) { Log.e(TAG, "Channel does not satisfy user band preference: " + mReportedFrequency); @@ -603,7 +606,7 @@ public class SoftApManager implements ActiveModeManager { Log.d(TAG, "Channel switched. Frequency: " + mReportedFrequency + " Bandwidth: " + mReportedBandwidth); mWifiMetrics.addSoftApChannelSwitchedEvent(mReportedFrequency, - mReportedBandwidth, mMode); + mReportedBandwidth, mApConfig.getTargetMode()); updateUserBandPreferenceViolationMetricsIfNeeded(); break; case CMD_TIMEOUT_TOGGLE_CHANGED: -- cgit v1.2.3