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.Car;
     19 import android.car.test.ICarTest;
     20 import android.content.Context;
     21 import android.os.IBinder;
     22 import android.os.RemoteException;
     23 import android.util.Log;
     24 
     25 import java.io.PrintWriter;
     26 import java.util.Arrays;
     27 import java.util.HashMap;
     28 import java.util.Map;
     29 
     30 /**
     31  * Service to allow testing / mocking vehicle HAL.
     32  * This service uses Vehicle HAL APIs directly (one exception) as vehicle HAL mocking anyway
     33  * requires accessing that level directly.
     34  */
     35 class CarTestService extends ICarTest.Stub implements CarServiceBase {
     36 
     37     private static final String TAG = CarTestService.class.getSimpleName();
     38 
     39     private final Context mContext;
     40     private final ICarImpl mICarImpl;
     41 
     42     private final Map<IBinder, TokenDeathRecipient> mTokens = new HashMap<>();
     43 
     44     CarTestService(Context context, ICarImpl carImpl) {
     45         mContext = context;
     46         mICarImpl = carImpl;
     47     }
     48 
     49     @Override
     50     public void init() {
     51         // nothing to do.
     52         // This service should not reset anything for init / release to maintain mocking.
     53     }
     54 
     55     @Override
     56     public void release() {
     57         // nothing to do
     58         // This service should not reset anything for init / release to maintain mocking.
     59     }
     60 
     61     @Override
     62     public void dump(PrintWriter writer) {
     63         writer.println("*CarTestService*");
     64         writer.println(" mTokens:" + Arrays.toString(mTokens.entrySet().toArray()));
     65     }
     66 
     67     @Override
     68     public void stopCarService(IBinder token) throws RemoteException {
     69         Log.d(TAG, "stopCarService, token: " + token);
     70         ICarImpl.assertPermission(mContext, Car.PERMISSION_CAR_TEST_SERVICE);
     71 
     72         synchronized (this) {
     73             if (mTokens.containsKey(token)) {
     74                 Log.w(TAG, "Calling stopCarService twice with the same token.");
     75                 return;
     76             }
     77 
     78             TokenDeathRecipient deathRecipient = new TokenDeathRecipient(token);
     79             mTokens.put(token, deathRecipient);
     80             token.linkToDeath(deathRecipient, 0);
     81 
     82             if (mTokens.size() == 1) {
     83                 mICarImpl.release();
     84             }
     85         }
     86     }
     87 
     88     @Override
     89     public void startCarService(IBinder token) throws RemoteException {
     90         Log.d(TAG, "startCarService, token: " + token);
     91         ICarImpl.assertPermission(mContext, Car.PERMISSION_CAR_TEST_SERVICE);
     92         releaseToken(token);
     93     }
     94 
     95     private synchronized void releaseToken(IBinder token) {
     96         Log.d(TAG, "releaseToken, token: " + token);
     97         DeathRecipient deathRecipient = mTokens.remove(token);
     98         if (deathRecipient != null) {
     99             token.unlinkToDeath(deathRecipient, 0);
    100         }
    101 
    102         if (mTokens.size() == 0) {
    103             mICarImpl.init();
    104         }
    105     }
    106 
    107     private class TokenDeathRecipient implements DeathRecipient {
    108         private final IBinder mToken;
    109 
    110         TokenDeathRecipient(IBinder token) throws RemoteException {
    111             mToken = token;
    112         }
    113 
    114         @Override
    115         public void binderDied() {
    116             releaseToken(mToken);
    117         }
    118     }
    119 }