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 #include "BaseWatchdogClient.h"
18
19 #include <android/binder_manager.h>
20
21 using aidl::android::automotive::watchdog::ICarWatchdog;
22 using aidl::android::automotive::watchdog::TimeoutLength;
23
24 namespace {
25
26 enum { WHAT_CHECK_ALIVE = 1 };
27
28 } // namespace
29
30 namespace android::hardware::automotive::utils {
31
BaseWatchdogClient(const sp<Looper> & handlerLooper)32 BaseWatchdogClient::BaseWatchdogClient(const sp<Looper>& handlerLooper)
33 : mHandlerLooper(handlerLooper), mCurrentSessionId(-1) {
34 mMessageHandler = new MessageHandlerImpl(this);
35 }
36
checkIfAlive(int32_t sessionId,TimeoutLength)37 ndk::ScopedAStatus BaseWatchdogClient::checkIfAlive(int32_t sessionId, TimeoutLength /*timeout*/) {
38 mHandlerLooper->removeMessages(mMessageHandler, WHAT_CHECK_ALIVE);
39 {
40 Mutex::Autolock lock(mMutex);
41 mCurrentSessionId = sessionId;
42 }
43 mHandlerLooper->sendMessage(mMessageHandler, Message(WHAT_CHECK_ALIVE));
44 return ndk::ScopedAStatus::ok();
45 }
46
prepareProcessTermination()47 ndk::ScopedAStatus BaseWatchdogClient::prepareProcessTermination() {
48 return ndk::ScopedAStatus::ok();
49 }
50
initialize()51 bool BaseWatchdogClient::initialize() {
52 ndk::SpAIBinder binder(
53 AServiceManager_getService("android.automotive.watchdog.ICarWatchdog/default"));
54 if (binder.get() == nullptr) {
55 ALOGE("Failed to get carwatchdog daemon");
56 return false;
57 }
58 std::shared_ptr<ICarWatchdog> server = ICarWatchdog::fromBinder(binder);
59 if (server == nullptr) {
60 ALOGE("Failed to connect to carwatchdog daemon");
61 return false;
62 }
63 mWatchdogServer = server;
64
65 binder = this->asBinder();
66 if (binder.get() == nullptr) {
67 ALOGE("Failed to get car watchdog client binder object");
68 return false;
69 }
70 std::shared_ptr<ICarWatchdogClient> client = ICarWatchdogClient::fromBinder(binder);
71 if (client == nullptr) {
72 ALOGE("Failed to get ICarWatchdogClient from binder");
73 return false;
74 }
75 mTestClient = client;
76 mWatchdogServer->registerClient(client, TimeoutLength::TIMEOUT_NORMAL);
77 ALOGI("Successfully registered the client to car watchdog server");
78 return true;
79 }
80
respondToWatchdog()81 void BaseWatchdogClient::respondToWatchdog() {
82 if (mWatchdogServer == nullptr) {
83 ALOGW("Cannot respond to car watchdog daemon: car watchdog daemon is not connected");
84 return;
85 }
86 int sessionId;
87 {
88 Mutex::Autolock lock(mMutex);
89 sessionId = mCurrentSessionId;
90 }
91 if (isClientHealthy()) {
92 ndk::ScopedAStatus status = mWatchdogServer->tellClientAlive(mTestClient, sessionId);
93 if (!status.isOk()) {
94 ALOGE("Failed to call tellClientAlive(session id = %d): %d", sessionId,
95 status.getStatus());
96 return;
97 }
98 }
99 }
100
MessageHandlerImpl(BaseWatchdogClient * client)101 BaseWatchdogClient::MessageHandlerImpl::MessageHandlerImpl(BaseWatchdogClient* client)
102 : mClient(client) {}
103
handleMessage(const Message & message)104 void BaseWatchdogClient::MessageHandlerImpl::handleMessage(const Message& message) {
105 switch (message.what) {
106 case WHAT_CHECK_ALIVE:
107 mClient->respondToWatchdog();
108 break;
109 default:
110 ALOGW("Unknown message: %d", message.what);
111 }
112 }
113
114 } // namespace android::hardware::automotive::utils
115