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