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.hardware.wifi.V1_0.WifiStatus;
21 import android.hardware.wifi.V1_0.WifiStatusCode;
22 import android.os.RemoteException;
23 import android.util.Log;
24 
25 import com.android.server.wifi.util.GeneralUtil.Mutable;
26 
27 /**
28  * HIDL implementation of the IWifiP2pIface interface.
29  */
30 public class WifiP2pIfaceHidlImpl implements IWifiP2pIface {
31     private static final String TAG = "WifiP2pIfaceHidlImpl";
32     private android.hardware.wifi.V1_0.IWifiP2pIface mWifiP2pIface;
33     private String mIfaceName;
34 
WifiP2pIfaceHidlImpl(@onNull android.hardware.wifi.V1_0.IWifiP2pIface p2pIface)35     public WifiP2pIfaceHidlImpl(@NonNull android.hardware.wifi.V1_0.IWifiP2pIface p2pIface) {
36         mWifiP2pIface = p2pIface;
37     }
38 
39     /**
40      * See comments for {@link com.android.server.wifi.hal.IWifiP2pIface#getName()}
41      */
getName()42     public String getName() {
43         final String methodStr = "getName";
44         if (mWifiP2pIface == null) {
45             handleNullIface(methodStr);
46             return null;
47         }
48         if (mIfaceName != null) return mIfaceName;
49         Mutable<String> nameResp = new Mutable<>();
50         try {
51             mWifiP2pIface.getName((WifiStatus status, String name) -> {
52                 if (isOk(status, methodStr)) {
53                     nameResp.value = name;
54                     mIfaceName = name;
55                 }
56             });
57         } catch (RemoteException e) {
58             Log.e(TAG, "Exception in " + methodStr + ": " + e);
59         }
60         return nameResp.value;
61     }
62 
isOk(WifiStatus status, String methodStr)63     private boolean isOk(WifiStatus status, String methodStr) {
64         if (status.code == WifiStatusCode.SUCCESS) return true;
65         Log.e(TAG, methodStr + " failed with status: " + status);
66         return false;
67     }
68 
handleNullIface(String methodStr)69     private void handleNullIface(String methodStr) {
70         Log.e(TAG, "Cannot call " + methodStr + " because mWifiP2pIface is null");
71     }
72 }
73