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 "IoOveruseMonitor.h"
20 #include "WatchdogInternalHandler.h"
21 #include "WatchdogPerfService.h"
22 #include "WatchdogProcessService.h"
23 #include "WatchdogServiceHelper.h"
24 
25 #include <aidl/android/automotive/watchdog/BnCarWatchdog.h>
26 #include <aidl/android/automotive/watchdog/ICarWatchdogClient.h>
27 #include <aidl/android/automotive/watchdog/ICarWatchdogMonitor.h>
28 #include <aidl/android/automotive/watchdog/IResourceOveruseListener.h>
29 #include <aidl/android/automotive/watchdog/ResourceOveruseStats.h>
30 #include <aidl/android/automotive/watchdog/ResourceType.h>
31 #include <aidl/android/automotive/watchdog/StateType.h>
32 #include <aidl/android/automotive/watchdog/TimeoutLength.h>
33 #include <android-base/result.h>
34 #include <android/binder_auto_utils.h>
35 #include <android/binder_libbinder.h>
36 #include <android/binder_manager.h>
37 #include <gtest/gtest_prod.h>
38 #include <utils/Errors.h>
39 #include <utils/RefBase.h>
40 #include <utils/String16.h>
41 #include <utils/StrongPointer.h>
42 #include <utils/Vector.h>
43 
44 #include <functional>
45 
46 namespace android {
47 namespace automotive {
48 namespace watchdog {
49 
50 class ServiceManager;
51 
52 // Forward declaration for testing use only.
53 namespace internal {
54 
55 class WatchdogBinderMediatorPeer;
56 
57 }  // namespace internal
58 
59 class WatchdogBinderMediatorInterface : public aidl::android::automotive::watchdog::BnCarWatchdog {
60 public:
61     virtual android::base::Result<void> init() = 0;
62     virtual void terminate() = 0;
63 };
64 
65 // WatchdogBinderMediator implements the public carwatchdog binder APIs such that it forwards
66 // the calls either to process ANR or performance services.
67 class WatchdogBinderMediator final : public WatchdogBinderMediatorInterface {
68 public:
69     WatchdogBinderMediator(
70             const android::sp<WatchdogProcessServiceInterface>& watchdogProcessService,
71             const android::sp<WatchdogPerfServiceInterface>& watchdogPerfService,
72             const android::sp<WatchdogServiceHelperInterface>& watchdogServiceHelper,
73             const android::sp<IoOveruseMonitorInterface>& ioOveruseMonitor,
74             const std::function<android::base::Result<void>(const char*, ndk::ICInterface*, bool,
75                                                             int)>& addServiceHandler = nullptr);
~WatchdogBinderMediator()76     ~WatchdogBinderMediator() { terminate(); }
77 
78     // Implements ICarWatchdog.aidl APIs.
79     binder_status_t dump(int fd, const char** args, uint32_t numArgs) override;
80     ndk::ScopedAStatus registerClient(
81             const std::shared_ptr<aidl::android::automotive::watchdog::ICarWatchdogClient>& client,
82             aidl::android::automotive::watchdog::TimeoutLength timeout) override;
83     ndk::ScopedAStatus unregisterClient(
84             const std::shared_ptr<aidl::android::automotive::watchdog::ICarWatchdogClient>& client)
85             override;
86     ndk::ScopedAStatus tellClientAlive(
87             const std::shared_ptr<aidl::android::automotive::watchdog::ICarWatchdogClient>& client,
88             int32_t sessionId) override;
89     ndk::ScopedAStatus addResourceOveruseListener(
90             const std::vector<aidl::android::automotive::watchdog::ResourceType>& resourceTypes,
91             const std::shared_ptr<aidl::android::automotive::watchdog::IResourceOveruseListener>&
92                     listener);
93     ndk::ScopedAStatus removeResourceOveruseListener(
94             const std::shared_ptr<aidl::android::automotive::watchdog::IResourceOveruseListener>&
95                     listener);
96     ndk::ScopedAStatus getResourceOveruseStats(
97             const std::vector<aidl::android::automotive::watchdog::ResourceType>& resourceTypes,
98             std::vector<aidl::android::automotive::watchdog::ResourceOveruseStats>*
99                     resourceOveruseStats);
100 
101     // Deprecated APIs.
102     ndk::ScopedAStatus registerMediator(
103             const std::shared_ptr<aidl::android::automotive::watchdog::ICarWatchdogClient>&
104                     mediator) override;
105     ndk::ScopedAStatus unregisterMediator(
106             const std::shared_ptr<aidl::android::automotive::watchdog::ICarWatchdogClient>&
107                     mediator) override;
108     ndk::ScopedAStatus registerMonitor(
109             const std::shared_ptr<aidl::android::automotive::watchdog::ICarWatchdogMonitor>&
110                     monitor) override;
111     ndk::ScopedAStatus unregisterMonitor(
112             const std::shared_ptr<aidl::android::automotive::watchdog::ICarWatchdogMonitor>&
113                     monitor) override;
114     ndk::ScopedAStatus tellMediatorAlive(
115             const std::shared_ptr<aidl::android::automotive::watchdog::ICarWatchdogClient>&
116                     mediator,
117             const std::vector<int32_t>& clientsNotResponding, int32_t sessionId) override;
118     ndk::ScopedAStatus tellDumpFinished(
119             const std::shared_ptr<aidl::android::automotive::watchdog::ICarWatchdogMonitor>&
120                     monitor,
121             int32_t pid) override;
122     ndk::ScopedAStatus notifySystemStateChange(aidl::android::automotive::watchdog::StateType type,
123                                                int32_t arg1, int32_t arg2) override;
124 
125 protected:
126     android::base::Result<void> init();
127 
terminate()128     void terminate() {
129         mWatchdogProcessService.clear();
130         mWatchdogPerfService.clear();
131         mIoOveruseMonitor.clear();
132         if (mWatchdogInternalHandler != nullptr) {
133             mWatchdogInternalHandler->terminate();
134             mWatchdogInternalHandler.reset();
135         }
136     }
137 
138 private:
139     android::sp<WatchdogProcessServiceInterface> mWatchdogProcessService;
140     android::sp<WatchdogPerfServiceInterface> mWatchdogPerfService;
141     android::sp<WatchdogServiceHelperInterface> mWatchdogServiceHelper;
142     android::sp<IoOveruseMonitorInterface> mIoOveruseMonitor;
143     std::shared_ptr<WatchdogInternalHandlerInterface> mWatchdogInternalHandler;
144 
145     // Used by tests to stub the call to IServiceManager.
146     std::function<android::base::Result<void>(const char*, ndk::ICInterface*, bool, int)>
147             mAddServiceHandler;
148 
149     friend class ServiceManager;
150 
151     // For unit tests.
152     friend class internal::WatchdogBinderMediatorPeer;
153     FRIEND_TEST(WatchdogBinderMediatorTest, TestInit);
154     FRIEND_TEST(WatchdogBinderMediatorTest, TestErrorOnInitWithNullServiceInstances);
155 };
156 
157 }  // namespace watchdog
158 }  // namespace automotive
159 }  // namespace android
160