| /* |
| * Copyright (C) 2021 The Android Open Source Project |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| |
| package com.android.car; |
| |
| import android.annotation.Nullable; |
| import android.car.builtin.os.ServiceManagerHelper; |
| import android.car.builtin.util.Slogf; |
| import android.hardware.automotive.vehicle.IVehicle; |
| import android.hardware.automotive.vehicle.SubscribeOptions; |
| import android.os.RemoteException; |
| import android.os.ServiceSpecificException; |
| |
| import com.android.car.hal.HalClientCallback; |
| import com.android.car.hal.HalPropConfig; |
| import com.android.car.hal.HalPropValue; |
| import com.android.car.hal.HalPropValueBuilder; |
| import com.android.internal.annotations.VisibleForTesting; |
| |
| final class AidlVehicleStub extends VehicleStub { |
| |
| private static final String AIDL_VHAL_SERVICE = |
| "android.hardware.automotive.vehicle.IVehicle/default"; |
| |
| private final IVehicle mAidlVehicle; |
| private final HalPropValueBuilder mPropValueBuilder; |
| |
| AidlVehicleStub() { |
| this(getAidlVehicle()); |
| } |
| |
| @VisibleForTesting |
| AidlVehicleStub(IVehicle aidlVehicle) { |
| mAidlVehicle = aidlVehicle; |
| mPropValueBuilder = new HalPropValueBuilder(/*isAidl=*/true); |
| } |
| |
| |
| /** |
| * Gets a HalPropValueBuilder that could be used to build a HalPropValue. |
| * |
| * @return a builder to build HalPropValue. |
| */ |
| @Override |
| public HalPropValueBuilder getHalPropValueBuilder() { |
| return mPropValueBuilder; |
| } |
| |
| /** |
| * Returns whether this vehicle stub is connecting to a valid vehicle HAL. |
| * |
| * @return Whether this vehicle stub is connecting to a valid vehicle HAL. |
| */ |
| @Override |
| public boolean isValid() { |
| return mAidlVehicle != null; |
| } |
| |
| /** |
| * Gets the interface descriptor for the connecting vehicle HAL. |
| * |
| * @return the interface descriptor. |
| * @throws IllegalStateException If unable to get the descriptor. |
| */ |
| @Override |
| public String getInterfaceDescriptor() throws IllegalStateException { |
| try { |
| return mAidlVehicle.asBinder().getInterfaceDescriptor(); |
| } catch (RemoteException e) { |
| throw new IllegalStateException("Unable to get Vehicle HAL interface descriptor", e); |
| } |
| } |
| |
| /** |
| * Register a death recipient that would be called when vehicle HAL died. |
| * |
| * @param recipient A death recipient. |
| * @throws IllegalStateException If unable to register the death recipient. |
| */ |
| @Override |
| public void linkToDeath(IVehicleDeathRecipient recipient) throws IllegalStateException { |
| try { |
| mAidlVehicle.asBinder().linkToDeath(recipient, /*flag=*/ 0); |
| } catch (RemoteException e) { |
| throw new IllegalStateException("Failed to linkToDeath Vehicle HAL"); |
| } |
| } |
| |
| /** |
| * Unlink a previously linked death recipient. |
| * |
| * @param recipient A previously linked death recipient. |
| */ |
| @Override |
| public void unlinkToDeath(IVehicleDeathRecipient recipient) { |
| mAidlVehicle.asBinder().unlinkToDeath(recipient, /*flag=*/ 0); |
| } |
| |
| /** |
| * Get all property configs. |
| * |
| * @return All the property configs. |
| * @throws RemoteException if the remote operation fails. |
| * @throws ServiceSpecificException if VHAL returns service specific error. |
| */ |
| @Override |
| public HalPropConfig[] getAllPropConfigs() |
| throws RemoteException, ServiceSpecificException { |
| // TODO(b/205774940): Call AIDL APIs. |
| return null; |
| } |
| |
| /** |
| * Subscribe to a property. |
| * |
| * @param callback The VehicleStubCallback that would be called for subscribe events. |
| * @param options The list of subscribe options. |
| * @throws RemoteException if the remote operation fails. |
| * @throws ServiceSpecificException if VHAL returns service specific error. |
| */ |
| @Override |
| public void subscribe(VehicleStubCallback callback, SubscribeOptions[] options) |
| throws RemoteException, ServiceSpecificException { |
| // TODO(b/205774940): Call AIDL APIs. |
| return; |
| } |
| |
| /** |
| * Unsubscribe to a property. |
| * |
| * @param callback The previously subscribed callback to unsubscribe. |
| * @param prop The ID for the property to unsubscribe. |
| * @throws RemoteException if the remote operation fails. |
| * @throws ServiceSpecificException if VHAL returns service specific error. |
| */ |
| @Override |
| public void unsubscribe(VehicleStubCallback callback, int prop) |
| throws RemoteException, ServiceSpecificException { |
| // TODO(b/205774940): Call AIDL APIs. |
| return; |
| } |
| |
| /** |
| * Get a new {@code VehicleStubCallback} that could be used to subscribe/unsubscribe. |
| * |
| * @param callback A callback that could be used to receive events. |
| * @return a {@code VehicleStubCallback} that could be passed to subscribe/unsubscribe. |
| */ |
| @Override |
| public VehicleStubCallback newCallback(HalClientCallback callback) { |
| // TODO(b/205774940): Return AIDL callback. |
| return null; |
| } |
| |
| /** |
| * Get a property. |
| * |
| * @param requestedPropValue The property to get. |
| * @return The vehicle property value. |
| * @throws RemoteException if the remote operation fails. |
| * @throws ServiceSpecificException if VHAL returns service specific error. |
| */ |
| @Override |
| @Nullable |
| public HalPropValue get(HalPropValue requestedPropValue) |
| throws RemoteException, ServiceSpecificException { |
| // TODO(b/205774940): Call AIDL APIs. |
| return null; |
| } |
| |
| /** |
| * Set a property. |
| * |
| * @param propValue The property to set. |
| * @throws RemoteException if the remote operation fails. |
| * @throws ServiceSpecificException if VHAL returns service specific error. |
| */ |
| @Override |
| public void set(HalPropValue propValue) throws RemoteException, ServiceSpecificException { |
| // TODO(b/205774940): Call AIDL APIs. |
| return; |
| } |
| |
| @Nullable |
| private static IVehicle getAidlVehicle() { |
| try { |
| return IVehicle.Stub.asInterface( |
| ServiceManagerHelper.waitForDeclaredService(AIDL_VHAL_SERVICE)); |
| } catch (RuntimeException e) { |
| Slogf.w(CarLog.TAG_SERVICE, "Failed to get \"" + AIDL_VHAL_SERVICE + "\" service", e); |
| } |
| return null; |
| } |
| } |