blob: a6db10042e293fbb4317322fe37bf8ece5d4dfba [file] [log] [blame]
Eric Jeong38ae8212020-01-14 10:25:10 -08001/*
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
17package com.android.car.watchdog;
18
Eric Jeongae2c04c2020-02-21 09:18:31 -080019import static com.android.car.CarLog.TAG_WATCHDOG;
20
Eric Jeong38ae8212020-01-14 10:25:10 -080021import android.automotive.watchdog.ICarWatchdogClient;
22import android.car.watchdog.ICarWatchdogService;
Eric Jeong6df075f2020-03-11 16:12:23 -070023import android.car.watchdoglib.CarWatchdogDaemonHelper;
Eric Jeongae2c04c2020-02-21 09:18:31 -080024import android.content.Context;
25import android.os.Handler;
Eric Jeongae2c04c2020-02-21 09:18:31 -080026import android.os.Looper;
27import android.os.RemoteException;
Eric Jeongae2c04c2020-02-21 09:18:31 -080028import android.util.Log;
29
30import androidx.annotation.VisibleForTesting;
Eric Jeong38ae8212020-01-14 10:25:10 -080031
32import com.android.car.CarServiceBase;
33
34import java.io.PrintWriter;
35import 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 */
43public final class CarWatchdogService extends ICarWatchdogService.Stub implements CarServiceBase {
44
Eric Jeongae2c04c2020-02-21 09:18:31 -080045 private final Context mContext;
46 private final ICarWatchdogClientImpl mWatchdogClient;
Eric Jeongae2c04c2020-02-21 09:18:31 -080047 private final Handler mMainHandler = new Handler(Looper.getMainLooper());
Eric Jeong6df075f2020-03-11 16:12:23 -070048 private CarWatchdogDaemonHelper mCarWatchdogDaemonHelper;
49 private final CarWatchdogDaemonHelper.OnConnectionChangeListener mConnectionListener =
50 (connected) -> {
51 if (connected) {
52 registerToDaemon();
53 }
54 };
Eric Jeongae2c04c2020-02-21 09:18:31 -080055
56 @VisibleForTesting
Eric Jeong6df075f2020-03-11 16:12:23 -070057 public CarWatchdogService(Context context) {
Eric Jeongae2c04c2020-02-21 09:18:31 -080058 mContext = context;
Eric Jeongae2c04c2020-02-21 09:18:31 -080059 mWatchdogClient = new ICarWatchdogClientImpl(this);
Eric Jeong6df075f2020-03-11 16:12:23 -070060 mCarWatchdogDaemonHelper = new CarWatchdogDaemonHelper();
Eric Jeong38ae8212020-01-14 10:25:10 -080061 }
62
63 @Override
64 public void init() {
Eric Jeong6df075f2020-03-11 16:12:23 -070065 mCarWatchdogDaemonHelper.addOnConnectionChangeListener(mConnectionListener);
66 mCarWatchdogDaemonHelper.connect();
Eric Jeong38ae8212020-01-14 10:25:10 -080067 }
68
69 @Override
70 public void release() {
Eric Jeong6df075f2020-03-11 16:12:23 -070071 unregisterFromDaemon();
72 mCarWatchdogDaemonHelper.disconnect();
Eric Jeong38ae8212020-01-14 10:25:10 -080073 }
74
75 @Override
76 public void dump(PrintWriter writer) {
Eric Jeongae2c04c2020-02-21 09:18:31 -080077 writer.println("*CarWatchdogService*");
Eric Jeong6df075f2020-03-11 16:12:23 -070078 // TODO(b/145556670): implement body.
Eric Jeong38ae8212020-01-14 10:25:10 -080079 }
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 Jeong6df075f2020-03-11 16:12:23 -070096 private void registerToDaemon() {
Eric Jeongae2c04c2020-02-21 09:18:31 -080097 try {
Eric Jeong6df075f2020-03-11 16:12:23 -070098 mCarWatchdogDaemonHelper.registerMediator(mWatchdogClient);
99 } catch (RemoteException | IllegalArgumentException | IllegalStateException e) {
Eric Jeongae2c04c2020-02-21 09:18:31 -0800100 Log.w(TAG_WATCHDOG, "Cannot register to car watchdog daemon: " + e);
Eric Jeong6df075f2020-03-11 16:12:23 -0700101 }
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 Jeongae2c04c2020-02-21 09:18:31 -0800109 }
Eric Jeongae2c04c2020-02-21 09:18:31 -0800110 }
111
112 private void doHealthCheck(int sessionId) {
113 mMainHandler.post(() -> {
Eric Jeongae2c04c2020-02-21 09:18:31 -0800114 try {
115 // TODO(b/145556670): Check clients status and include them in the response.
116 int[] clientsNotResponding = new int[0];
Eric Jeong6df075f2020-03-11 16:12:23 -0700117 mCarWatchdogDaemonHelper.tellMediatorAlive(mWatchdogClient, clientsNotResponding,
118 sessionId);
119 } catch (RemoteException | IllegalArgumentException | IllegalStateException e) {
Eric Jeongae2c04c2020-02-21 09:18:31 -0800120 Log.w(TAG_WATCHDOG, "Cannot respond to car watchdog daemon (sessionId="
121 + sessionId + "): " + e);
122 }
123 });
124 }
125
Eric Jeong38ae8212020-01-14 10:25:10 -0800126 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 Jeongae2c04c2020-02-21 09:18:31 -0800135 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 Jeong38ae8212020-01-14 10:25:10 -0800141 }
142 }
143}