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 #ifndef WATCHDOG_TESTCLIENT_SRC_WATCHDOGCLIENT_H_
18 #define WATCHDOG_TESTCLIENT_SRC_WATCHDOGCLIENT_H_
19 
20 #include <aidl/android/automotive/watchdog/BnCarWatchdog.h>
21 #include <aidl/android/automotive/watchdog/BnCarWatchdogClient.h>
22 #include <utils/Looper.h>
23 #include <utils/Mutex.h>
24 #include <utils/String16.h>
25 
26 namespace aidl {
27 namespace android {
28 namespace automotive {
29 namespace watchdog {
30 
31 struct CommandParam {
32     std::string timeout;
33     int inactiveAfterInSec;
34     int terminateAfterInSec;
35     bool forcedKill;
36     bool verbose;
37 };
38 
39 struct HealthCheckSession {
40     HealthCheckSession(int32_t sessionId = -1,
41                        TimeoutLength sessionTimeout = TimeoutLength::TIMEOUT_NORMAL) :
idHealthCheckSession42           id(sessionId),
43           timeout(sessionTimeout) {}
44 
45     int32_t id;
46     TimeoutLength timeout;
47 };
48 
49 class WatchdogClient : public BnCarWatchdogClient {
50 public:
51     explicit WatchdogClient(const ::android::sp<::android::Looper>& handlerLooper);
52 
53     ndk::ScopedAStatus checkIfAlive(int32_t sessionId, TimeoutLength timeout) override;
54     ndk::ScopedAStatus prepareProcessTermination() override;
55 
56     bool initialize(const CommandParam& param);
57     void finalize();
58 
59 private:
60     class MessageHandlerImpl : public ::android::MessageHandler {
61     public:
62         explicit MessageHandlerImpl(WatchdogClient* client);
63         void handleMessage(const ::android::Message& message) override;
64 
65     private:
66         WatchdogClient* mClient;
67     };
68 
69 private:
70     void respondToWatchdog();
71     void becomeInactive();
72     void terminateProcess();
73     void registerClient(const std::string& timeout);
74     void unregisterClient();
75 
76 private:
77     ::android::sp<::android::Looper> mHandlerLooper;
78     ::android::sp<MessageHandlerImpl> mMessageHandler;
79     bool mForcedKill;
80     bool mVerbose;
81     ::android::Mutex mMutex;
82     std::shared_ptr<ICarWatchdog> mWatchdogServer GUARDED_BY(mMutex);
83     std::shared_ptr<ICarWatchdogClient> mTestClient GUARDED_BY(mMutex);
84     bool mIsClientActive GUARDED_BY(mMutex);
85     HealthCheckSession mSession GUARDED_BY(mMutex);
86 };
87 
88 }  // namespace watchdog
89 }  // namespace automotive
90 }  // namespace android
91 }  // namespace aidl
92 
93 #endif  // WATCHDOG_TESTCLIENT_SRC_WATCHDOGCLIENT_H_
94