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 android.wifi.mockwifi.nl80211;
18 
19 import android.net.wifi.nl80211.IClientInterface;
20 import android.net.wifi.nl80211.ISendMgmtFrameEvent;
21 import android.net.wifi.nl80211.IWifiScannerImpl;
22 import android.util.ArraySet;
23 import android.util.Log;
24 
25 import java.util.Collections;
26 import java.util.Set;
27 
28 public class IClientInterfaceImp extends IClientInterface.Stub {
29     private static final String TAG = "IClientInterfaceImp";
30 
31     private IWifiScannerImp mIWifiScannerImp;
32     private String mIfaceName = null;
33     private ClientInterfaceMock mClientInterfaceMock;
34 
35     public interface ClientInterfaceMock {
signalPoll()36         default int[] signalPoll() {
37             return null;
38         }
39 
getPacketCounters()40         default int[] getPacketCounters() {
41             return null;
42         }
43 
getMacAddress()44         default byte[] getMacAddress() {
45             return null;
46         }
47 
getInterfaceName()48         default String getInterfaceName() {
49             return null;
50         }
51     }
52 
IClientInterfaceImp(String ifaceName)53     public IClientInterfaceImp(String ifaceName) {
54         mIfaceName = ifaceName;
55         mIWifiScannerImp = new IWifiScannerImp(ifaceName);
56     }
57 
isMethodOverridden(ClientInterfaceMock clientInterfaceMock, String methodName)58     private boolean isMethodOverridden(ClientInterfaceMock clientInterfaceMock,
59             String methodName) throws NoSuchMethodException {
60         return !clientInterfaceMock.getClass().getMethod(methodName).getDeclaringClass().equals(
61                 ClientInterfaceMock.class);
62     }
63 
setClientInterfaceMock(ClientInterfaceMock clientInterfaceMock)64     public Set<String> setClientInterfaceMock(ClientInterfaceMock clientInterfaceMock) {
65         Set<String> overriddenMethods = new ArraySet<>();
66         try {
67             if (isMethodOverridden(clientInterfaceMock, "signalPoll")) {
68                 overriddenMethods.add("signalPoll");
69             }
70             if (isMethodOverridden(clientInterfaceMock, "getPacketCounters")) {
71                 overriddenMethods.add("getPacketCounters");
72             }
73             if (isMethodOverridden(clientInterfaceMock, "getMacAddress")) {
74                 overriddenMethods.add("getMacAddress");
75             }
76             if (isMethodOverridden(clientInterfaceMock, "getInterfaceName")) {
77                 overriddenMethods.add("getInterfaceName");
78             }
79         } catch (NoSuchMethodException e) {
80             Log.e(TAG, "Reflection error: " + e);
81             return Collections.emptySet();
82         }
83 
84         mClientInterfaceMock = clientInterfaceMock;
85         return overriddenMethods;
86     }
87 
88     // Supported methods in IClientInterface.aidl
89     @Override
signalPoll()90     public int[] signalPoll() {
91         Log.d(TAG, "signalPoll");
92         if (mClientInterfaceMock == null) {
93             Log.e(TAG, "signalPoll: null mock!");
94             return null;
95         }
96         return mClientInterfaceMock.signalPoll();
97     }
98 
99     @Override
getPacketCounters()100     public int[] getPacketCounters() {
101         Log.d(TAG, "getPacketCounters");
102         // TODO: Mock it when we have a use (test) case.
103         return null;
104     }
105 
106     @Override
getMacAddress()107     public byte[] getMacAddress() {
108         Log.d(TAG, "getMacAddress");
109         // TODO: Mock it when we have a use (test) case.
110         return null;
111     }
112 
113     @Override
getInterfaceName()114     public String getInterfaceName() {
115         Log.d(TAG, "getInterfaceName");
116         // TODO: Mock it when we have a use (test) case.
117         return null;
118     }
119 
120     @Override
getWifiScannerImpl()121     public IWifiScannerImpl getWifiScannerImpl() {
122         Log.d(TAG, "getWifiScannerImpl");
123         return mIWifiScannerImp;
124     }
125 
126     // CHECKSTYLE:OFF Generated code
127     @Override
SendMgmtFrame(byte[] frame, ISendMgmtFrameEvent callback, int mcs)128     public void SendMgmtFrame(byte[] frame, ISendMgmtFrameEvent callback, int mcs) {
129         Log.d(TAG, "SendMgmtFrame");
130         // TODO: Mock it when we have a use (test) case.
131     }
132     // CHECKSTYLE:ON Generated code
133 }
134