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