keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 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 | |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame] | 18 | import android.car.Car; |
Keun-young Park | e54ac27 | 2016-02-16 19:02:18 -0800 | [diff] [blame] | 19 | import android.car.test.ICarTest; |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 20 | import android.content.Context; |
Keun-young Park | a54e116 | 2016-07-29 14:54:06 -0700 | [diff] [blame] | 21 | import android.os.IBinder; |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame] | 22 | import android.os.RemoteException; |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 23 | import android.util.Log; |
| 24 | |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 25 | import java.io.PrintWriter; |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame] | 26 | import java.util.Arrays; |
| 27 | import java.util.HashMap; |
| 28 | import java.util.Map; |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 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 | */ |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame] | 35 | class CarTestService extends ICarTest.Stub implements CarServiceBase { |
| 36 | |
| 37 | private static final String TAG = CarTestService.class.getSimpleName(); |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 38 | |
| 39 | private final Context mContext; |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 40 | private final ICarImpl mICarImpl; |
| 41 | |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame] | 42 | private final Map<IBinder, TokenDeathRecipient> mTokens = new HashMap<>(); |
| 43 | |
| 44 | CarTestService(Context context, ICarImpl carImpl) { |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 45 | mContext = context; |
| 46 | mICarImpl = carImpl; |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | @Override |
| 50 | public void init() { |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 51 | // nothing to do. |
| 52 | // This service should not reset anything for init / release to maintain mocking. |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | @Override |
| 56 | public void release() { |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 57 | // nothing to do |
| 58 | // This service should not reset anything for init / release to maintain mocking. |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | @Override |
| 62 | public void dump(PrintWriter writer) { |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 63 | writer.println("*CarTestService*"); |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame] | 64 | writer.println(" mTokens:" + Arrays.toString(mTokens.entrySet().toArray())); |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | @Override |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame] | 68 | public void stopCarService(IBinder token) throws RemoteException { |
| 69 | Log.d(TAG, "stopCarService, token: " + token); |
| 70 | ICarImpl.assertPermission(mContext, Car.PERMISSION_CAR_TEST_SERVICE); |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 71 | |
Keun-young Park | 43cfff2 | 2016-02-19 12:49:52 -0800 | [diff] [blame] | 72 | synchronized (this) { |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame] | 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(); |
Keun-young Park | 43cfff2 | 2016-02-19 12:49:52 -0800 | [diff] [blame] | 84 | } |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 85 | } |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | @Override |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame] | 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); |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 93 | } |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 94 | |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame] | 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 | } |
Keun-young Park | a28d7b2 | 2016-02-29 16:54:29 -0800 | [diff] [blame] | 105 | } |
| 106 | |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame] | 107 | private class TokenDeathRecipient implements DeathRecipient { |
| 108 | private final IBinder mToken; |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 109 | |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame] | 110 | TokenDeathRecipient(IBinder token) throws RemoteException { |
| 111 | mToken = token; |
| 112 | } |
Keun-young Park | a54e116 | 2016-07-29 14:54:06 -0700 | [diff] [blame] | 113 | |
Keun-young Park | a54e116 | 2016-07-29 14:54:06 -0700 | [diff] [blame] | 114 | @Override |
| 115 | public void binderDied() { |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame] | 116 | releaseToken(mToken); |
Keun-young Park | a54e116 | 2016-07-29 14:54:06 -0700 | [diff] [blame] | 117 | } |
| 118 | } |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame] | 119 | } |