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 20 import android.annotation.NonNull; 21 import android.annotation.Nullable; 22 import android.os.RemoteException; 23 import android.os.ServiceSpecificException; 24 import android.util.Log; 25 26 /** 27 * AIDL implementation of the IWifiP2pIface interface. 28 */ 29 public class WifiP2pIfaceAidlImpl implements IWifiP2pIface { 30 private static final String TAG = "WifiP2pIfaceAidlImpl"; 31 private android.hardware.wifi.IWifiP2pIface mWifiP2pIface; 32 private final Object mLock = new Object(); 33 private String mIfaceName; 34 WifiP2pIfaceAidlImpl(@onNull android.hardware.wifi.IWifiP2pIface p2pIface)35 public WifiP2pIfaceAidlImpl(@NonNull android.hardware.wifi.IWifiP2pIface p2pIface) { 36 mWifiP2pIface = p2pIface; 37 } 38 39 /** 40 * See comments for {@link com.android.server.wifi.hal.IWifiP2pIface#getName()} 41 */ 42 @Override 43 @Nullable getName()44 public String getName() { 45 final String methodStr = "getName"; 46 synchronized (mLock) { 47 if (!checkIfaceAndLogFailure(methodStr)) return null; 48 if (mIfaceName != null) return mIfaceName; 49 try { 50 String ifaceName = mWifiP2pIface.getName(); 51 mIfaceName = ifaceName; 52 return mIfaceName; 53 } catch (RemoteException e) { 54 handleRemoteException(e, methodStr); 55 } catch (ServiceSpecificException e) { 56 handleServiceSpecificException(e, methodStr); 57 } 58 return null; 59 } 60 } 61 checkIfaceAndLogFailure(String methodStr)62 private boolean checkIfaceAndLogFailure(String methodStr) { 63 if (mWifiP2pIface == null) { 64 Log.e(TAG, "Unable to call " + methodStr + " because iface is null."); 65 return false; 66 } 67 return true; 68 } 69 handleRemoteException(RemoteException e, String methodStr)70 private void handleRemoteException(RemoteException e, String methodStr) { 71 mWifiP2pIface = null; 72 Log.e(TAG, methodStr + " failed with remote exception: " + e); 73 } 74 handleServiceSpecificException(ServiceSpecificException e, String methodStr)75 private void handleServiceSpecificException(ServiceSpecificException e, String methodStr) { 76 Log.e(TAG, methodStr + " failed with service-specific exception: " + e); 77 } 78 } 79