Eric Jeong | 38ae821 | 2020-01-14 10:25:10 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 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.car.watchdog; |
| 18 | |
Eric Jeong | ae2c04c | 2020-02-21 09:18:31 -0800 | [diff] [blame] | 19 | import static com.android.car.CarLog.TAG_WATCHDOG; |
| 20 | |
Eric Jeong | 38ae821 | 2020-01-14 10:25:10 -0800 | [diff] [blame] | 21 | import android.automotive.watchdog.ICarWatchdogClient; |
| 22 | import android.car.watchdog.ICarWatchdogService; |
Eric Jeong | 6df075f | 2020-03-11 16:12:23 -0700 | [diff] [blame] | 23 | import android.car.watchdoglib.CarWatchdogDaemonHelper; |
Eric Jeong | ae2c04c | 2020-02-21 09:18:31 -0800 | [diff] [blame] | 24 | import android.content.Context; |
| 25 | import android.os.Handler; |
Eric Jeong | ae2c04c | 2020-02-21 09:18:31 -0800 | [diff] [blame] | 26 | import android.os.Looper; |
| 27 | import android.os.RemoteException; |
Eric Jeong | ae2c04c | 2020-02-21 09:18:31 -0800 | [diff] [blame] | 28 | import android.util.Log; |
| 29 | |
| 30 | import androidx.annotation.VisibleForTesting; |
Eric Jeong | 38ae821 | 2020-01-14 10:25:10 -0800 | [diff] [blame] | 31 | |
| 32 | import com.android.car.CarServiceBase; |
| 33 | |
| 34 | import java.io.PrintWriter; |
| 35 | import java.lang.ref.WeakReference; |
| 36 | |
| 37 | /** |
| 38 | * Service to implement CarWatchdogManager API. |
| 39 | * |
| 40 | * <p>CarWatchdogService runs as car watchdog mediator, which checks clients' health status and |
| 41 | * reports the result to car watchdog server. |
| 42 | */ |
| 43 | public final class CarWatchdogService extends ICarWatchdogService.Stub implements CarServiceBase { |
| 44 | |
Eric Jeong | ae2c04c | 2020-02-21 09:18:31 -0800 | [diff] [blame] | 45 | private final Context mContext; |
| 46 | private final ICarWatchdogClientImpl mWatchdogClient; |
Eric Jeong | ae2c04c | 2020-02-21 09:18:31 -0800 | [diff] [blame] | 47 | private final Handler mMainHandler = new Handler(Looper.getMainLooper()); |
Eric Jeong | 6df075f | 2020-03-11 16:12:23 -0700 | [diff] [blame] | 48 | private CarWatchdogDaemonHelper mCarWatchdogDaemonHelper; |
| 49 | private final CarWatchdogDaemonHelper.OnConnectionChangeListener mConnectionListener = |
| 50 | (connected) -> { |
| 51 | if (connected) { |
| 52 | registerToDaemon(); |
| 53 | } |
| 54 | }; |
Eric Jeong | ae2c04c | 2020-02-21 09:18:31 -0800 | [diff] [blame] | 55 | |
| 56 | @VisibleForTesting |
Eric Jeong | 6df075f | 2020-03-11 16:12:23 -0700 | [diff] [blame] | 57 | public CarWatchdogService(Context context) { |
Eric Jeong | ae2c04c | 2020-02-21 09:18:31 -0800 | [diff] [blame] | 58 | mContext = context; |
Eric Jeong | ae2c04c | 2020-02-21 09:18:31 -0800 | [diff] [blame] | 59 | mWatchdogClient = new ICarWatchdogClientImpl(this); |
Eric Jeong | 6df075f | 2020-03-11 16:12:23 -0700 | [diff] [blame] | 60 | mCarWatchdogDaemonHelper = new CarWatchdogDaemonHelper(); |
Eric Jeong | 38ae821 | 2020-01-14 10:25:10 -0800 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public void init() { |
Eric Jeong | 6df075f | 2020-03-11 16:12:23 -0700 | [diff] [blame] | 65 | mCarWatchdogDaemonHelper.addOnConnectionChangeListener(mConnectionListener); |
| 66 | mCarWatchdogDaemonHelper.connect(); |
Eric Jeong | 38ae821 | 2020-01-14 10:25:10 -0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public void release() { |
Eric Jeong | 6df075f | 2020-03-11 16:12:23 -0700 | [diff] [blame] | 71 | unregisterFromDaemon(); |
| 72 | mCarWatchdogDaemonHelper.disconnect(); |
Eric Jeong | 38ae821 | 2020-01-14 10:25:10 -0800 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public void dump(PrintWriter writer) { |
Eric Jeong | ae2c04c | 2020-02-21 09:18:31 -0800 | [diff] [blame] | 77 | writer.println("*CarWatchdogService*"); |
Eric Jeong | 6df075f | 2020-03-11 16:12:23 -0700 | [diff] [blame] | 78 | // TODO(b/145556670): implement body. |
Eric Jeong | 38ae821 | 2020-01-14 10:25:10 -0800 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public void registerClient(ICarWatchdogClient client, int timeout) { |
| 83 | // TODO(b/145556670): implement body. |
| 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public void unregisterClient(ICarWatchdogClient client) { |
| 88 | // TODO(b/145556670): implement body. |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public void tellClientAlive(ICarWatchdogClient client, int sessionId) { |
| 93 | // TODO(b/145556670): implement body. |
| 94 | } |
| 95 | |
Eric Jeong | 6df075f | 2020-03-11 16:12:23 -0700 | [diff] [blame] | 96 | private void registerToDaemon() { |
Eric Jeong | ae2c04c | 2020-02-21 09:18:31 -0800 | [diff] [blame] | 97 | try { |
Eric Jeong | 6df075f | 2020-03-11 16:12:23 -0700 | [diff] [blame] | 98 | mCarWatchdogDaemonHelper.registerMediator(mWatchdogClient); |
| 99 | } catch (RemoteException | IllegalArgumentException | IllegalStateException e) { |
Eric Jeong | ae2c04c | 2020-02-21 09:18:31 -0800 | [diff] [blame] | 100 | Log.w(TAG_WATCHDOG, "Cannot register to car watchdog daemon: " + e); |
Eric Jeong | 6df075f | 2020-03-11 16:12:23 -0700 | [diff] [blame] | 101 | } |
| 102 | } |
| 103 | |
| 104 | private void unregisterFromDaemon() { |
| 105 | try { |
| 106 | mCarWatchdogDaemonHelper.unregisterMediator(mWatchdogClient); |
| 107 | } catch (RemoteException | IllegalArgumentException | IllegalStateException e) { |
| 108 | Log.w(TAG_WATCHDOG, "Cannot unregister from car watchdog daemon: " + e); |
Eric Jeong | ae2c04c | 2020-02-21 09:18:31 -0800 | [diff] [blame] | 109 | } |
Eric Jeong | ae2c04c | 2020-02-21 09:18:31 -0800 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | private void doHealthCheck(int sessionId) { |
| 113 | mMainHandler.post(() -> { |
Eric Jeong | ae2c04c | 2020-02-21 09:18:31 -0800 | [diff] [blame] | 114 | try { |
| 115 | // TODO(b/145556670): Check clients status and include them in the response. |
| 116 | int[] clientsNotResponding = new int[0]; |
Eric Jeong | 6df075f | 2020-03-11 16:12:23 -0700 | [diff] [blame] | 117 | mCarWatchdogDaemonHelper.tellMediatorAlive(mWatchdogClient, clientsNotResponding, |
| 118 | sessionId); |
| 119 | } catch (RemoteException | IllegalArgumentException | IllegalStateException e) { |
Eric Jeong | ae2c04c | 2020-02-21 09:18:31 -0800 | [diff] [blame] | 120 | Log.w(TAG_WATCHDOG, "Cannot respond to car watchdog daemon (sessionId=" |
| 121 | + sessionId + "): " + e); |
| 122 | } |
| 123 | }); |
| 124 | } |
| 125 | |
Eric Jeong | 38ae821 | 2020-01-14 10:25:10 -0800 | [diff] [blame] | 126 | private static final class ICarWatchdogClientImpl extends ICarWatchdogClient.Stub { |
| 127 | private final WeakReference<CarWatchdogService> mService; |
| 128 | |
| 129 | private ICarWatchdogClientImpl(CarWatchdogService service) { |
| 130 | mService = new WeakReference<>(service); |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public void checkIfAlive(int sessionId, int timeout) { |
Eric Jeong | ae2c04c | 2020-02-21 09:18:31 -0800 | [diff] [blame] | 135 | CarWatchdogService service = mService.get(); |
| 136 | if (service == null) { |
| 137 | Log.w(TAG_WATCHDOG, "CarWatchdogService is not available"); |
| 138 | return; |
| 139 | } |
| 140 | service.doHealthCheck(sessionId); |
Eric Jeong | 38ae821 | 2020-01-14 10:25:10 -0800 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | } |