1 /*
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.server.wifi.util;
18 
19 import android.annotation.NonNull;
20 import android.hardware.wifi.WifiChannelWidthInMhz;
21 import android.hardware.wifi.common.OuiKeyedData;
22 import android.hardware.wifi.supplicant.KeyMgmtMask;
23 import android.net.wifi.ScanResult;
24 import android.net.wifi.WifiAnnotations;
25 import android.net.wifi.WifiConfiguration;
26 import android.util.Log;
27 
28 import java.util.ArrayList;
29 import java.util.BitSet;
30 import java.util.List;
31 
32 /**
33  * Provide utility functions for HAL AIDL implementation.
34  */
35 public class HalAidlUtil {
36     private static final String TAG = "HalAidlUtil";
37 
supplicantMaskValueToWifiConfigurationBitSet(int supplicantMask, int supplicantValue, BitSet bitset, int bitSetPosition)38     private static int supplicantMaskValueToWifiConfigurationBitSet(int supplicantMask,
39             int supplicantValue, BitSet bitset, int bitSetPosition) {
40         bitset.set(bitSetPosition, (supplicantMask & supplicantValue) == supplicantValue);
41         int modifiedSupplicantMask = supplicantMask & ~supplicantValue;
42         return modifiedSupplicantMask;
43     }
44 
45     /** Convert supplicant key management mask to framework key management mask. */
supplicantToWifiConfigurationKeyMgmtMask(int mask)46     public static BitSet supplicantToWifiConfigurationKeyMgmtMask(int mask) {
47         BitSet bitset = new BitSet();
48         mask = supplicantMaskValueToWifiConfigurationBitSet(
49                 mask, KeyMgmtMask.NONE, bitset,
50                 WifiConfiguration.KeyMgmt.NONE);
51         mask = supplicantMaskValueToWifiConfigurationBitSet(
52                 mask, KeyMgmtMask.WPA_PSK, bitset,
53                 WifiConfiguration.KeyMgmt.WPA_PSK);
54         mask = supplicantMaskValueToWifiConfigurationBitSet(
55                 mask, KeyMgmtMask.WPA_EAP, bitset,
56                 WifiConfiguration.KeyMgmt.WPA_EAP);
57         mask = supplicantMaskValueToWifiConfigurationBitSet(
58                 mask, KeyMgmtMask.IEEE8021X, bitset,
59                 WifiConfiguration.KeyMgmt.IEEE8021X);
60         mask = supplicantMaskValueToWifiConfigurationBitSet(
61                 mask, KeyMgmtMask.OSEN, bitset,
62                 WifiConfiguration.KeyMgmt.OSEN);
63         mask = supplicantMaskValueToWifiConfigurationBitSet(
64                 mask, KeyMgmtMask.FT_PSK, bitset,
65                 WifiConfiguration.KeyMgmt.FT_PSK);
66         mask = supplicantMaskValueToWifiConfigurationBitSet(
67                 mask, KeyMgmtMask.FT_EAP, bitset,
68                 WifiConfiguration.KeyMgmt.FT_EAP);
69         mask = supplicantMaskValueToWifiConfigurationBitSet(
70                 mask, KeyMgmtMask.SAE,
71                 bitset, WifiConfiguration.KeyMgmt.SAE);
72         mask = supplicantMaskValueToWifiConfigurationBitSet(
73                 mask, KeyMgmtMask.OWE,
74                 bitset, WifiConfiguration.KeyMgmt.OWE);
75         mask = supplicantMaskValueToWifiConfigurationBitSet(
76                 mask, KeyMgmtMask.SUITE_B_192,
77                 bitset, WifiConfiguration.KeyMgmt.SUITE_B_192);
78         mask = supplicantMaskValueToWifiConfigurationBitSet(
79                 mask, KeyMgmtMask.WPA_PSK_SHA256,
80                 bitset, WifiConfiguration.KeyMgmt.WPA_PSK_SHA256);
81         mask = supplicantMaskValueToWifiConfigurationBitSet(
82                 mask, KeyMgmtMask.WPA_EAP_SHA256,
83                 bitset, WifiConfiguration.KeyMgmt.WPA_EAP_SHA256);
84         mask = supplicantMaskValueToWifiConfigurationBitSet(
85                 mask, KeyMgmtMask.WAPI_PSK,
86                 bitset, WifiConfiguration.KeyMgmt.WAPI_PSK);
87         mask = supplicantMaskValueToWifiConfigurationBitSet(
88                 mask, KeyMgmtMask.WAPI_CERT,
89                 bitset, WifiConfiguration.KeyMgmt.WAPI_CERT);
90         mask = supplicantMaskValueToWifiConfigurationBitSet(
91                 mask, KeyMgmtMask.FILS_SHA256,
92                 bitset, WifiConfiguration.KeyMgmt.FILS_SHA256);
93         mask = supplicantMaskValueToWifiConfigurationBitSet(
94                 mask, KeyMgmtMask.FILS_SHA384,
95                 bitset, WifiConfiguration.KeyMgmt.FILS_SHA384);
96         mask = supplicantMaskValueToWifiConfigurationBitSet(
97                 mask, KeyMgmtMask.DPP,
98                 bitset, WifiConfiguration.KeyMgmt.DPP);
99         if (mask != 0) {
100             throw new IllegalArgumentException(
101                     "invalid key mgmt mask from supplicant: " + mask);
102         }
103         return bitset;
104     }
105 
106     /**
107      * Convert a list of {@link android.net.wifi.OuiKeyedData} to its HAL equivalent.
108      * Note: Invalid instances in the input list will be skipped.
109      */
frameworkToHalOuiKeyedDataList( List<android.net.wifi.OuiKeyedData> frameworkList)110     public static OuiKeyedData[] frameworkToHalOuiKeyedDataList(
111             List<android.net.wifi.OuiKeyedData> frameworkList) {
112         List<OuiKeyedData> halList = new ArrayList<>();
113         for (android.net.wifi.OuiKeyedData frameworkData : frameworkList) {
114             if (frameworkData == null || !frameworkData.validate()) {
115                 Log.e(TAG, "Invalid framework OuiKeyedData: " + frameworkData);
116                 continue;
117             }
118             OuiKeyedData halData = new OuiKeyedData();
119             halData.oui = frameworkData.getOui();
120             halData.vendorData = frameworkData.getData();
121             halList.add(halData);
122         }
123         return halList.toArray(new OuiKeyedData[halList.size()]);
124     }
125 
126     /**
127      * Convert a list of HAL OuiKeyedData its framework equivalent.
128      */
halToFrameworkOuiKeyedDataList( @onNull OuiKeyedData[] halList)129     public static List<android.net.wifi.OuiKeyedData> halToFrameworkOuiKeyedDataList(
130             @NonNull OuiKeyedData[] halList) {
131         if (halList == null) {
132             return new ArrayList<>();
133         }
134         List<android.net.wifi.OuiKeyedData> frameworkList = new ArrayList<>();
135         for (OuiKeyedData halData : halList) {
136             try {
137                 android.net.wifi.OuiKeyedData frameworkData =
138                         new android.net.wifi.OuiKeyedData.Builder(
139                                 halData.oui, halData.vendorData).build();
140                 frameworkList.add(frameworkData);
141             } catch (Exception e) {
142                 Log.e(TAG, "Invalid HAL OuiKeyedData: " + e);
143             }
144         }
145         return frameworkList;
146     }
147 
148     /**
149      * Convert HAL channelBandwidth to framework enum
150      */
151     @WifiAnnotations.ChannelWidth
getChannelBandwidthFromHal(int channelBandwidth)152     public static int getChannelBandwidthFromHal(int channelBandwidth) {
153         switch (channelBandwidth) {
154             case WifiChannelWidthInMhz.WIDTH_40:
155                 return ScanResult.CHANNEL_WIDTH_40MHZ;
156             case WifiChannelWidthInMhz.WIDTH_80:
157                 return ScanResult.CHANNEL_WIDTH_80MHZ;
158             case WifiChannelWidthInMhz.WIDTH_160:
159                 return ScanResult.CHANNEL_WIDTH_160MHZ;
160             case WifiChannelWidthInMhz.WIDTH_80P80:
161                 return ScanResult.CHANNEL_WIDTH_80MHZ_PLUS_MHZ;
162             case WifiChannelWidthInMhz.WIDTH_320:
163                 return ScanResult.CHANNEL_WIDTH_320MHZ;
164             default:
165                 return ScanResult.CHANNEL_WIDTH_20MHZ;
166         }
167     }
168 }
169