diff --git a/wifi/java/android/net/wifi/ScanResult.java b/wifi/java/android/net/wifi/ScanResult.java index f5b56225b8e51..aa3a13925894c 100644 --- a/wifi/java/android/net/wifi/ScanResult.java +++ b/wifi/java/android/net/wifi/ScanResult.java @@ -524,6 +524,161 @@ public final class ScanResult implements Parcelable { * {@hide} */ public final static int UNSPECIFIED = -1; + + /** + * 2.4 GHz band first channel number + * @hide + */ + public static final int BAND_24_GHZ_FIRST_CH_NUM = 1; + /** + * 2.4 GHz band last channel number + * @hide + */ + public static final int BAND_24_GHZ_LAST_CH_NUM = 14; + /** + * 2.4 GHz band frequency of first channel in MHz + * @hide + */ + public static final int BAND_24_GHZ_START_FREQ_MHZ = 2412; + /** + * 2.4 GHz band frequency of last channel in MHz + * @hide + */ + public static final int BAND_24_GHZ_END_FREQ_MHZ = 2484; + + /** + * 5 GHz band first channel number + * @hide + */ + public static final int BAND_5_GHZ_FIRST_CH_NUM = 32; + /** + * 5 GHz band last channel number + * @hide + */ + public static final int BAND_5_GHZ_LAST_CH_NUM = 173; + /** + * 5 GHz band frequency of first channel in MHz + * @hide + */ + public static final int BAND_5_GHZ_START_FREQ_MHZ = 5160; + /** + * 5 GHz band frequency of last channel in MHz + * @hide + */ + public static final int BAND_5_GHZ_END_FREQ_MHZ = 5865; + + /** + * 6 GHz band first channel number + * @hide + */ + public static final int BAND_6_GHZ_FIRST_CH_NUM = 1; + /** + * 6 GHz band last channel number + * @hide + */ + public static final int BAND_6_GHZ_LAST_CH_NUM = 233; + /** + * 6 GHz band frequency of first channel in MHz + * @hide + */ + public static final int BAND_6_GHZ_START_FREQ_MHZ = 5945; + /** + * 6 GHz band frequency of last channel in MHz + * @hide + */ + public static final int BAND_6_GHZ_END_FREQ_MHZ = 7105; + + /** + * Utility function to check if a frequency within 2.4 GHz band + * @param freqMhz frequency in MHz + * @return true if within 2.4GHz, false otherwise + * + * @hide + */ + public static boolean is24GHz(int freqMhz) { + return freqMhz >= BAND_24_GHZ_START_FREQ_MHZ && freqMhz <= BAND_24_GHZ_END_FREQ_MHZ; + } + + /** + * Utility function to check if a frequency within 5 GHz band + * @param freqMhz frequency in MHz + * @return true if within 5GHz, false otherwise + * + * @hide + */ + public static boolean is5GHz(int freqMhz) { + return freqMhz >= BAND_5_GHZ_START_FREQ_MHZ && freqMhz <= BAND_5_GHZ_END_FREQ_MHZ; + } + + /** + * Utility function to check if a frequency within 6 GHz band + * @param freqMhz + * @return true if within 6GHz, false otherwise + * + * @hide + */ + public static boolean is6GHz(int freqMhz) { + return freqMhz >= BAND_6_GHZ_START_FREQ_MHZ && freqMhz <= BAND_6_GHZ_END_FREQ_MHZ; + } + + /** + * Utility function to convert channel number/band to frequency in MHz + * @param channel number to convert + * @param band of channel to convert + * @return center frequency in Mhz of the channel, {@link UNSPECIFIED} if no match + * + * @hide + */ + public static int convertChannelToFrequencyMhz(int channel, @WifiScanner.WifiBand int band) { + if (band == WifiScanner.WIFI_BAND_24_GHZ) { + // Special case + if (channel == 14) { + return 2484; + } else if (channel >= BAND_24_GHZ_FIRST_CH_NUM && channel <= BAND_24_GHZ_LAST_CH_NUM) { + return ((channel - BAND_24_GHZ_FIRST_CH_NUM) * 5) + BAND_24_GHZ_START_FREQ_MHZ; + } else { + return UNSPECIFIED; + } + } + if (band == WifiScanner.WIFI_BAND_5_GHZ) { + if (channel >= BAND_5_GHZ_FIRST_CH_NUM && channel <= BAND_5_GHZ_LAST_CH_NUM) { + return ((channel - BAND_5_GHZ_FIRST_CH_NUM) * 5) + BAND_5_GHZ_START_FREQ_MHZ; + } else { + return UNSPECIFIED; + } + } + if (band == WifiScanner.WIFI_BAND_6_GHZ) { + if (channel >= BAND_6_GHZ_FIRST_CH_NUM && channel <= BAND_6_GHZ_LAST_CH_NUM) { + return ((channel - BAND_6_GHZ_FIRST_CH_NUM) * 5) + BAND_6_GHZ_START_FREQ_MHZ; + } else { + return UNSPECIFIED; + } + } + return UNSPECIFIED; + } + + /** + * Utility function to convert frequency in MHz to channel number + * @param freqMhz frequency in MHz + * @return channel number associated with given frequency, {@link UNSPECIFIED} if no match + * + * @hide + */ + public static int convertFrequencyMhzToChannel(int freqMhz) { + // Special case + if (freqMhz == 2484) { + return 14; + } else if (is24GHz(freqMhz)) { + return (freqMhz - BAND_24_GHZ_START_FREQ_MHZ) / 5 + BAND_24_GHZ_FIRST_CH_NUM; + } else if (is5GHz(freqMhz)) { + return ((freqMhz - BAND_5_GHZ_START_FREQ_MHZ) / 5) + BAND_5_GHZ_FIRST_CH_NUM; + } else if (is6GHz(freqMhz)) { + return ((freqMhz - BAND_6_GHZ_START_FREQ_MHZ) / 5) + BAND_6_GHZ_FIRST_CH_NUM; + } + + return UNSPECIFIED; + } + /** * @hide */ @@ -531,14 +686,6 @@ public final class ScanResult implements Parcelable { return ScanResult.is24GHz(frequency); } - /** - * @hide - * TODO: makes real freq boundaries - */ - public static boolean is24GHz(int freq) { - return freq > 2400 && freq < 2500; - } - /** * @hide */ @@ -553,21 +700,6 @@ public final class ScanResult implements Parcelable { return ScanResult.is6GHz(frequency); } - /** - * @hide - * TODO: makes real freq boundaries - */ - public static boolean is5GHz(int freq) { - return freq > 4900 && freq < 5900; - } - - /** - * @hide - */ - public static boolean is6GHz(int freq) { - return freq > 5925 && freq < 7125; - } - /** * @hide * anqp lines from supplicant BSS response diff --git a/wifi/tests/src/android/net/wifi/ScanResultTest.java b/wifi/tests/src/android/net/wifi/ScanResultTest.java index 6cb832463bc02..5516f433070ff 100644 --- a/wifi/tests/src/android/net/wifi/ScanResultTest.java +++ b/wifi/tests/src/android/net/wifi/ScanResultTest.java @@ -45,6 +45,68 @@ public class ScanResultTest { public static final @WifiAnnotations.WifiStandard int TEST_WIFI_STANDARD = ScanResult.WIFI_STANDARD_11AC; + /** + * Frequency to channel map. This include some frequencies used outside the US. + * Representing it using a vector (instead of map) for simplification. + */ + private static final int[] FREQUENCY_TO_CHANNEL_MAP = { + 2412, WifiScanner.WIFI_BAND_24_GHZ, 1, + 2417, WifiScanner.WIFI_BAND_24_GHZ, 2, + 2422, WifiScanner.WIFI_BAND_24_GHZ, 3, + 2427, WifiScanner.WIFI_BAND_24_GHZ, 4, + 2432, WifiScanner.WIFI_BAND_24_GHZ, 5, + 2437, WifiScanner.WIFI_BAND_24_GHZ, 6, + 2442, WifiScanner.WIFI_BAND_24_GHZ, 7, + 2447, WifiScanner.WIFI_BAND_24_GHZ, 8, + 2452, WifiScanner.WIFI_BAND_24_GHZ, 9, + 2457, WifiScanner.WIFI_BAND_24_GHZ, 10, + 2462, WifiScanner.WIFI_BAND_24_GHZ, 11, + /* 12, 13 are only legitimate outside the US. */ + 2467, WifiScanner.WIFI_BAND_24_GHZ, 12, + 2472, WifiScanner.WIFI_BAND_24_GHZ, 13, + /* 14 is for Japan, DSSS and CCK only. */ + 2484, WifiScanner.WIFI_BAND_24_GHZ, 14, + /* 34 valid in Japan. */ + 5170, WifiScanner.WIFI_BAND_5_GHZ, 34, + 5180, WifiScanner.WIFI_BAND_5_GHZ, 36, + 5190, WifiScanner.WIFI_BAND_5_GHZ, 38, + 5200, WifiScanner.WIFI_BAND_5_GHZ, 40, + 5210, WifiScanner.WIFI_BAND_5_GHZ, 42, + 5220, WifiScanner.WIFI_BAND_5_GHZ, 44, + 5230, WifiScanner.WIFI_BAND_5_GHZ, 46, + 5240, WifiScanner.WIFI_BAND_5_GHZ, 48, + 5260, WifiScanner.WIFI_BAND_5_GHZ, 52, + 5280, WifiScanner.WIFI_BAND_5_GHZ, 56, + 5300, WifiScanner.WIFI_BAND_5_GHZ, 60, + 5320, WifiScanner.WIFI_BAND_5_GHZ, 64, + 5500, WifiScanner.WIFI_BAND_5_GHZ, 100, + 5520, WifiScanner.WIFI_BAND_5_GHZ, 104, + 5540, WifiScanner.WIFI_BAND_5_GHZ, 108, + 5560, WifiScanner.WIFI_BAND_5_GHZ, 112, + 5580, WifiScanner.WIFI_BAND_5_GHZ, 116, + /* 120, 124, 128 valid in Europe/Japan. */ + 5600, WifiScanner.WIFI_BAND_5_GHZ, 120, + 5620, WifiScanner.WIFI_BAND_5_GHZ, 124, + 5640, WifiScanner.WIFI_BAND_5_GHZ, 128, + /* 132+ valid in US. */ + 5660, WifiScanner.WIFI_BAND_5_GHZ, 132, + 5680, WifiScanner.WIFI_BAND_5_GHZ, 136, + 5700, WifiScanner.WIFI_BAND_5_GHZ, 140, + /* 144 is supported by a subset of WiFi chips. */ + 5720, WifiScanner.WIFI_BAND_5_GHZ, 144, + 5745, WifiScanner.WIFI_BAND_5_GHZ, 149, + 5765, WifiScanner.WIFI_BAND_5_GHZ, 153, + 5785, WifiScanner.WIFI_BAND_5_GHZ, 157, + 5805, WifiScanner.WIFI_BAND_5_GHZ, 161, + 5825, WifiScanner.WIFI_BAND_5_GHZ, 165, + 5845, WifiScanner.WIFI_BAND_5_GHZ, 169, + 5865, WifiScanner.WIFI_BAND_5_GHZ, 173, + /* Now some 6GHz channels */ + 5945, WifiScanner.WIFI_BAND_6_GHZ, 1, + 5960, WifiScanner.WIFI_BAND_6_GHZ, 4, + 6100, WifiScanner.WIFI_BAND_6_GHZ, 32 + }; + /** * Setup before tests. */ @@ -183,6 +245,25 @@ public class ScanResultTest { + "RadioChainInfo: id=1, level=-54]", scanResult.toString()); } + /** + * verify frequency to channel conversion for all possible frequencies. + */ + @Test + public void convertFrequencyToChannel() throws Exception { + for (int i = 0; i < FREQUENCY_TO_CHANNEL_MAP.length; i += 3) { + assertEquals(FREQUENCY_TO_CHANNEL_MAP[i + 2], + ScanResult.convertFrequencyMhzToChannel(FREQUENCY_TO_CHANNEL_MAP[i])); + } + } + + /** + * Verify frequency to channel conversion failed for an invalid frequency. + */ + @Test + public void convertFrequencyToChannelWithInvalidFreq() throws Exception { + assertEquals(-1, ScanResult.convertFrequencyMhzToChannel(8000)); + } + /** * Write the provided {@link ScanResult} to a parcel and deserialize it. */