• 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 class WatchdogBinderMediatorInterface : public BnCarWatchdog {
56 public:
57     virtual android::base::Result<void> init() = 0;
58     virtual void terminate() = 0;
59     virtual status_t dump(int fd, const Vector<android::String16>& args) = 0;
60 };
61 
62 // WatchdogBinderMediator implements the public carwatchdog binder APIs such that it forwards
63 // the calls either to process ANR or performance services.
64 class WatchdogBinderMediator final : public WatchdogBinderMediatorInterface {
65 public:
66     WatchdogBinderMediator(
67             const android::sp<WatchdogProcessServiceInterface>& watchdogProcessService,
68             const android::sp<WatchdogPerfServiceInterface>& watchdogPerfService,
69             const android::sp<WatchdogServiceHelperInterface>& watchdogServiceHelper,
70             const std::function<android::base::Result<void>(const char*,
71                                                             const android::sp<android::IBinder>&)>&
72                     addServiceHandler = nullptr);
~WatchdogBinderMediator()73     ~WatchdogBinderMediator() { terminate(); }
74 
75     // Implements ICarWatchdog.aidl APIs.
76     status_t dump(int fd, const Vector<android::String16>& args) override;
77     android::binder::Status registerClient(const android::sp<ICarWatchdogClient>& client,
78                                            TimeoutLength timeout) override;
79     android::binder::Status unregisterClient(
80             const android::sp<ICarWatchdogClient>& client) override;
81     android::binder::Status tellClientAlive(const android::sp<ICarWatchdogClient>& client,
82                                             int32_t sessionId) override;
83     android::binder::Status addResourceOveruseListener(
84             const std::vector<ResourceType>& resourceTypes,
85             const android::sp<IResourceOveruseListener>& listener);
86     android::binder::Status removeResourceOveruseListener(
87             const android::sp<IResourceOveruseListener>& listener);
88     android::binder::Status getResourceOveruseStats(
89             const std::vector<ResourceType>& resourceTypes,
90             std::vector<ResourceOveruseStats>* resourceOveruseStats);
91 
92     // Deprecated APIs.
93     android::binder::Status registerMediator(
94             const android::sp<ICarWatchdogClient>& mediator) override;
95     android::binder::Status unregisterMediator(
96             const android::sp<ICarWatchdogClient>& mediator) override;
97     android::binder::Status registerMonitor(
98             const android::sp<ICarWatchdogMonitor>& monitor) override;
99     android::binder::Status unregisterMonitor(
100             const android::sp<ICarWatchdogMonitor>& monitor) override;
101     android::binder::Status tellMediatorAlive(const android::sp<ICarWatchdogClient>& mediator,
102                                               const std::vector<int32_t>& clientsNotResponding,
103                                               int32_t sessionId) override;
104     android::binder::Status tellDumpFinished(const android::sp<ICarWatchdogMonitor>& monitor,
105                                              int32_t pid) override;
106     android::binder::Status notifySystemStateChange(StateType type, int32_t arg1,
107                                                     int32_t arg2) override;
108 
109 protected:
110     android::base::Result<void> init();
111 
terminate()112     void terminate() {
113         mWatchdogProcessService.clear();
114         mWatchdogPerfService.clear();
115         mIoOveruseMonitor.clear();
116         if (mWatchdogInternalHandler != nullptr) {
117             mWatchdogInternalHandler->terminate();
118             mWatchdogInternalHandler.clear();
119         }
120     }
121 
122 private:
123     status_t dumpServices(int fd, const Vector<String16>& args);
124     status_t dumpHelpText(const int fd, const std::string& errorMsg);
125 
126     android::sp<WatchdogProcessServiceInterface> mWatchdogProcessService;
127     android::sp<WatchdogPerfServiceInterface> mWatchdogPerfService;
128     android::sp<WatchdogServiceHelperInterface> mWatchdogServiceHelper;
129     android::sp<IoOveruseMonitorInterface> mIoOveruseMonitor;
130     android::sp<WatchdogInternalHandler> mWatchdogInternalHandler;
131 
132     // Used by tests to stub the call to IServiceManager.
133     std::function<android::base::Result<void>(const char*, const android::sp<android::IBinder>&)>
134             mAddServiceHandler;
135 
136     friend class ServiceManager;
137 
138     // For unit tests.
139     friend class internal::WatchdogBinderMediatorPeer;
140     FRIEND_TEST(WatchdogBinderMediatorTest, TestInit);
141     FRIEND_TEST(WatchdogBinderMediatorTest, TestErrorOnInitWithNullServiceInstances);
142     FRIEND_TEST(WatchdogBinderMediatorTest, TestTerminate);
143     FRIEND_TEST(WatchdogBinderMediatorTest, TestHandlesEmptyDumpArgs);
144 };
145 
146 }  // namespace watchdog
147 }  // namespace automotive
148 }  // namespace android
149 
150 #endif  // CPP_WATCHDOG_SERVER_SRC_WATCHDOGBINDERMEDIATOR_H_
151