Home | History | Annotate | Download | only in car
      1 /*
      2  * Copyright (C) 2015 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 package com.android.car;
     17 
     18 import android.car.test.CarTestManager;
     19 import android.car.test.ICarTest;
     20 import android.content.Context;
     21 import android.util.Log;
     22 
     23 import com.android.car.hal.VehicleHal;
     24 import com.android.car.vehiclenetwork.IVehicleNetworkHalMock;
     25 import com.android.car.vehiclenetwork.VehicleNetwork;
     26 import com.android.car.vehiclenetwork.VehicleNetworkProto.VehiclePropConfigs;
     27 import com.android.car.vehiclenetwork.VehiclePropValueParcelable;
     28 
     29 import java.io.PrintWriter;
     30 
     31 /**
     32  * Service to allow testing / mocking vehicle HAL.
     33  * This service uses Vehicle HAL APIs directly (one exception) as vehicle HAL mocking anyway
     34  * requires accessing that level directly.
     35  */
     36 public class CarTestService extends ICarTest.Stub implements CarServiceBase {
     37 
     38     private final Context mContext;
     39     private final VehicleNetwork mVehicleNetwork;
     40     private final ICarImpl mICarImpl;
     41     private boolean mInMocking = false;
     42     private int mMockingFlags = 0;
     43     private Exception mException = null;
     44 
     45     public CarTestService(Context context, ICarImpl carImpl) {
     46         mContext = context;
     47         mICarImpl = carImpl;
     48         mVehicleNetwork = VehicleHal.getInstance().getVehicleNetwork();
     49     }
     50 
     51     @Override
     52     public void init() {
     53         // nothing to do.
     54         // This service should not reset anything for init / release to maintain mocking.
     55     }
     56 
     57     @Override
     58     public void release() {
     59         // nothing to do
     60         // This service should not reset anything for init / release to maintain mocking.
     61     }
     62 
     63     @Override
     64     public void dump(PrintWriter writer) {
     65         writer.println("*CarTestService*");
     66         writer.println(" mInMocking" + mInMocking);
     67     }
     68 
     69     @Override
     70     public void injectEvent(VehiclePropValueParcelable value) {
     71         ICarImpl.assertVehicleHalMockPermission(mContext);
     72         mVehicleNetwork.injectEvent(value.value);
     73     }
     74 
     75     @Override
     76     public void startMocking(final IVehicleNetworkHalMock mock, final int flags) {
     77         ICarImpl.assertVehicleHalMockPermission(mContext);
     78         CarServiceUtils.runOnMainSync(new Runnable() {
     79             @Override
     80             public void run() {
     81                 try {
     82                     mVehicleNetwork.startMocking(mock);
     83                     VehicleHal.getInstance().startMocking();
     84                     mICarImpl.startMocking();
     85                     synchronized (this) {
     86                         mInMocking = true;
     87                         mMockingFlags = flags;
     88                         mException = null;
     89                     }
     90                 } catch (Exception e) {
     91                     Log.w(CarLog.TAG_TEST, "startMocking failed", e);
     92                     synchronized (this) {
     93                         mException = e;
     94                     }
     95                 }
     96                 Log.i(CarLog.TAG_TEST, "start vehicle HAL mocking, flags:0x" +
     97                         Integer.toHexString(flags));
     98             }
     99         });
    100         synchronized (this) {
    101             if (mException != null) {
    102                 throw new IllegalStateException(mException);
    103             }
    104         }
    105     }
    106 
    107     @Override
    108     public void stopMocking(final IVehicleNetworkHalMock mock) {
    109         ICarImpl.assertVehicleHalMockPermission(mContext);
    110         CarServiceUtils.runOnMainSync(new Runnable() {
    111             @Override
    112             public void run() {
    113                 try {
    114                     mVehicleNetwork.stopMocking(mock);
    115                     VehicleHal.getInstance().stopMocking();
    116                     mICarImpl.stopMocking();
    117                     synchronized (this) {
    118                         mInMocking = false;
    119                         mMockingFlags = 0;
    120                         mException = null;
    121                     }
    122                     Log.i(CarLog.TAG_TEST, "stop vehicle HAL mocking");
    123                 } catch (Exception e) {
    124                     Log.w(CarLog.TAG_TEST, "stopMocking failed", e);
    125                 }
    126             }
    127         });
    128     }
    129 
    130     @Override
    131     public boolean isPropertySupported(int property) {
    132         VehiclePropConfigs configs = VehicleHal.getInstance().getVehicleNetwork().listProperties(
    133                 property);
    134         return configs != null;
    135     }
    136 
    137     public synchronized boolean isInMocking() {
    138         return mInMocking;
    139     }
    140 
    141     public synchronized boolean shouldDoRealShutdownInMocking() {
    142         return (mMockingFlags & CarTestManager.FLAG_MOCKING_REAL_SHUTDOWN) != 0;
    143     }
    144 }
    145