• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.hal;
18 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.net.MacAddress;
22 import android.util.Log;
23 
24 import com.android.server.wifi.util.NativeUtil;
25 
26 import java.util.List;
27 import java.util.function.Supplier;
28 
29 /**
30  * Wrapper around a WifiApIface.
31  * May be initialized using a HIDL or AIDL WifiApIface.
32  */
33 public class WifiApIface implements WifiHal.WifiInterface {
34     private static final String TAG = "WifiApIface";
35     private final IWifiApIface mWifiApIface;
36 
WifiApIface(@onNull android.hardware.wifi.V1_0.IWifiApIface apIface)37     public WifiApIface(@NonNull android.hardware.wifi.V1_0.IWifiApIface apIface) {
38         mWifiApIface = createWifiApIfaceHidlImplMockable(apIface);
39     }
40 
WifiApIface(@onNull android.hardware.wifi.IWifiApIface apIface)41     public WifiApIface(@NonNull android.hardware.wifi.IWifiApIface apIface) {
42         mWifiApIface = createWifiApIfaceAidlImplMockable(apIface);
43     }
44 
createWifiApIfaceHidlImplMockable( android.hardware.wifi.V1_0.IWifiApIface apIface)45     protected WifiApIfaceHidlImpl createWifiApIfaceHidlImplMockable(
46             android.hardware.wifi.V1_0.IWifiApIface apIface) {
47         return new WifiApIfaceHidlImpl(apIface);
48     }
49 
createWifiApIfaceAidlImplMockable( android.hardware.wifi.IWifiApIface apIface)50     protected WifiApIfaceAidlImpl createWifiApIfaceAidlImplMockable(
51             android.hardware.wifi.IWifiApIface apIface) {
52         return new WifiApIfaceAidlImpl(apIface);
53     }
54 
validateAndCall(String methodStr, T defaultVal, @NonNull Supplier<T> supplier)55     private <T> T validateAndCall(String methodStr, T defaultVal, @NonNull Supplier<T> supplier) {
56         if (mWifiApIface == null) {
57             Log.wtf(TAG, "Cannot call " + methodStr + " because mWifiApIface is null");
58             return defaultVal;
59         }
60         return supplier.get();
61     }
62 
63     /**
64      * See comments for {@link IWifiApIface#getName()}
65      */
66     @Override
67     @Nullable
getName()68     public String getName() {
69         return validateAndCall("getName", null,
70                 () -> mWifiApIface.getName());
71     }
72 
73     /**
74      * See comments for {@link IWifiApIface#getBridgedInstances()}
75      */
getBridgedInstances()76     public List<String> getBridgedInstances() {
77         return validateAndCall("getBridgedInstances", null,
78                 () -> mWifiApIface.getBridgedInstances());
79     }
80 
81     /**
82      * See comments for {@link IWifiApIface#getFactoryMacAddress()}
83      */
getFactoryMacAddress()84     public MacAddress getFactoryMacAddress() {
85         return validateAndCall("getFactoryMacAddress", null,
86                 () -> mWifiApIface.getFactoryMacAddress());
87     }
88 
89     /**
90      * See comments for {@link IWifiApIface#setCountryCode(byte[])}
91      */
setCountryCode(String countryCode)92     public boolean setCountryCode(String countryCode) {
93         if (countryCode == null || countryCode.length() != 2) {
94             Log.e(TAG, "Invalid country code " + countryCode);
95             return false;
96         }
97         try {
98             final byte[] code = NativeUtil.stringToByteArray(countryCode);
99             return validateAndCall("setCountryCode", false,
100                     () -> mWifiApIface.setCountryCode(code));
101         } catch (IllegalArgumentException e) {
102             Log.e(TAG, "Invalid country code " + countryCode + ", error: " + e);
103             return false;
104         }
105 
106     }
107 
108     /**
109      * See comments for {@link IWifiApIface#resetToFactoryMacAddress()}
110      */
resetToFactoryMacAddress()111     public boolean resetToFactoryMacAddress() {
112         return validateAndCall("resetToFactoryMacAddress", false,
113                 () -> mWifiApIface.resetToFactoryMacAddress());
114     }
115 
116     /**
117      * See comments for {@link IWifiApIface#isSetMacAddressSupported()}
118      */
isSetMacAddressSupported()119     public boolean isSetMacAddressSupported() {
120         return validateAndCall("isSetMacAddressSupported", false,
121                 () -> mWifiApIface.isSetMacAddressSupported());
122     }
123 
124     /**
125      * See comments for {@link IWifiApIface#setMacAddress(MacAddress)}
126      */
setMacAddress(MacAddress mac)127     public boolean setMacAddress(MacAddress mac) {
128         if (mac == null) {
129             Log.e(TAG, "setMacAddress received a null MAC address");
130             return false;
131         }
132         return validateAndCall("setMacAddress", false,
133                 () -> mWifiApIface.setMacAddress(mac));
134     }
135 }
136