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 android_hardware_automotive_vehicle_V2_0_WatchdogClient_H_
18 #define android_hardware_automotive_vehicle_V2_0_WatchdogClient_H_
19 
20 #include "VehicleHalManager.h"
21 
22 #include <aidl/android/automotive/watchdog/BnCarWatchdog.h>
23 #include <aidl/android/automotive/watchdog/BnCarWatchdogClient.h>
24 #include <utils/Looper.h>
25 #include <utils/Mutex.h>
26 
27 namespace android {
28 namespace hardware {
29 namespace automotive {
30 namespace vehicle {
31 namespace V2_0 {
32 
33 class WatchdogClient : public aidl::android::automotive::watchdog::BnCarWatchdogClient {
34   public:
35     explicit WatchdogClient(const ::android::sp<::android::Looper>& handlerLooper,
36                             VehicleHalManager* vhalManager);
37 
38     ndk::ScopedAStatus checkIfAlive(
39             int32_t sessionId, aidl::android::automotive::watchdog::TimeoutLength timeout) override;
40     ndk::ScopedAStatus prepareProcessTermination() override;
41 
42     bool initialize();
43 
44   private:
45     class MessageHandlerImpl : public ::android::MessageHandler {
46       public:
47         explicit MessageHandlerImpl(WatchdogClient* client);
48         void handleMessage(const ::android::Message& message) override;
49 
50       private:
51         WatchdogClient* mClient;
52     };
53 
54   private:
55     void respondToWatchdog();
56     bool isClientHealthy() const;
57 
58   private:
59     ::android::sp<::android::Looper> mHandlerLooper;
60     ::android::sp<MessageHandlerImpl> mMessageHandler;
61     std::shared_ptr<aidl::android::automotive::watchdog::ICarWatchdog> mWatchdogServer;
62     std::shared_ptr<aidl::android::automotive::watchdog::ICarWatchdogClient> mTestClient;
63     VehicleHalManager* mVhalManager;
64     ::android::Mutex mMutex;
65     int mCurrentSessionId GUARDED_BY(mMutex);
66 };
67 
68 }  // namespace V2_0
69 }  // namespace vehicle
70 }  // namespace automotive
71 }  // namespace hardware
72 }  // namespace android
73 
74 #endif  // android_hardware_automotive_vehicle_V2_0_WatchdogClient_H_
75