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 #pragma once
18 
19 #include <aidl/android/automotive/watchdog/BnCarWatchdog.h>
20 #include <aidl/android/automotive/watchdog/BnCarWatchdogClient.h>
21 #include <utils/Looper.h>
22 #include <utils/Mutex.h>
23 
24 namespace android::hardware::automotive::utils {
25 
26 class BaseWatchdogClient : public aidl::android::automotive::watchdog::BnCarWatchdogClient {
27   public:
28     ndk::ScopedAStatus checkIfAlive(
29             int32_t sessionId, aidl::android::automotive::watchdog::TimeoutLength timeout) override;
30     ndk::ScopedAStatus prepareProcessTermination() override;
31 
32     bool initialize();
33 
34     virtual ~BaseWatchdogClient() = default;
35 
36   protected:
37     explicit BaseWatchdogClient(const ::android::sp<::android::Looper>& handlerLooper);
38     virtual bool isClientHealthy() const = 0;
39 
40   private:
41     class MessageHandlerImpl : public ::android::MessageHandler {
42       public:
43         explicit MessageHandlerImpl(BaseWatchdogClient* client);
44         void handleMessage(const ::android::Message& message) override;
45 
46       private:
47         BaseWatchdogClient* mClient;
48     };
49 
50   private:
51     void respondToWatchdog();
52 
53   private:
54     ::android::sp<::android::Looper> mHandlerLooper;
55     ::android::sp<MessageHandlerImpl> mMessageHandler;
56     std::shared_ptr<aidl::android::automotive::watchdog::ICarWatchdog> mWatchdogServer;
57     std::shared_ptr<aidl::android::automotive::watchdog::ICarWatchdogClient> mTestClient;
58     ::android::Mutex mMutex;
59     int mCurrentSessionId GUARDED_BY(mMutex);
60 };
61 
62 }  // namespace android::hardware::automotive::utils
63