/** * Copyright (c) 2020, The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef CPP_WATCHDOG_SERVER_SRC_WATCHDOGBINDERMEDIATOR_H_ #define CPP_WATCHDOG_SERVER_SRC_WATCHDOGBINDERMEDIATOR_H_ #include "IoOveruseMonitor.h" #include "WatchdogInternalHandler.h" #include "WatchdogPerfService.h" #include "WatchdogProcessService.h" #include "WatchdogServiceHelper.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace android { namespace automotive { namespace watchdog { class ServiceManager; // Forward declaration for testing use only. namespace internal { class WatchdogBinderMediatorPeer; } // namespace internal class WatchdogBinderMediatorInterface : public aidl::android::automotive::watchdog::BnCarWatchdog { public: virtual android::base::Result init() = 0; virtual void terminate() = 0; }; // WatchdogBinderMediator implements the public carwatchdog binder APIs such that it forwards // the calls either to process ANR or performance services. class WatchdogBinderMediator final : public WatchdogBinderMediatorInterface { public: WatchdogBinderMediator( const android::sp& watchdogProcessService, const android::sp& watchdogPerfService, const android::sp& watchdogServiceHelper, const android::sp& ioOveruseMonitor, const std::function(const char*, ndk::ICInterface*, bool, int)>& addServiceHandler = nullptr); ~WatchdogBinderMediator() { terminate(); } // Implements ICarWatchdog.aidl APIs. binder_status_t dump(int fd, const char** args, uint32_t numArgs) override; ndk::ScopedAStatus registerClient( const std::shared_ptr& client, aidl::android::automotive::watchdog::TimeoutLength timeout) override; ndk::ScopedAStatus unregisterClient( const std::shared_ptr& client) override; ndk::ScopedAStatus tellClientAlive( const std::shared_ptr& client, int32_t sessionId) override; ndk::ScopedAStatus addResourceOveruseListener( const std::vector& resourceTypes, const std::shared_ptr& listener); ndk::ScopedAStatus removeResourceOveruseListener( const std::shared_ptr& listener); ndk::ScopedAStatus getResourceOveruseStats( const std::vector& resourceTypes, std::vector* resourceOveruseStats); // Deprecated APIs. ndk::ScopedAStatus registerMediator( const std::shared_ptr& mediator) override; ndk::ScopedAStatus unregisterMediator( const std::shared_ptr& mediator) override; ndk::ScopedAStatus registerMonitor( const std::shared_ptr& monitor) override; ndk::ScopedAStatus unregisterMonitor( const std::shared_ptr& monitor) override; ndk::ScopedAStatus tellMediatorAlive( const std::shared_ptr& mediator, const std::vector& clientsNotResponding, int32_t sessionId) override; ndk::ScopedAStatus tellDumpFinished( const std::shared_ptr& monitor, int32_t pid) override; ndk::ScopedAStatus notifySystemStateChange(aidl::android::automotive::watchdog::StateType type, int32_t arg1, int32_t arg2) override; protected: android::base::Result init(); void terminate() { mWatchdogProcessService.clear(); mWatchdogPerfService.clear(); mIoOveruseMonitor.clear(); if (mWatchdogInternalHandler != nullptr) { mWatchdogInternalHandler->terminate(); mWatchdogInternalHandler.reset(); } } private: android::sp mWatchdogProcessService; android::sp mWatchdogPerfService; android::sp mWatchdogServiceHelper; android::sp mIoOveruseMonitor; std::shared_ptr mWatchdogInternalHandler; // Used by tests to stub the call to IServiceManager. std::function(const char*, ndk::ICInterface*, bool, int)> mAddServiceHandler; friend class ServiceManager; // For unit tests. friend class internal::WatchdogBinderMediatorPeer; FRIEND_TEST(WatchdogBinderMediatorTest, TestInit); FRIEND_TEST(WatchdogBinderMediatorTest, TestErrorOnInitWithNullServiceInstances); }; } // namespace watchdog } // namespace automotive } // namespace android #endif // CPP_WATCHDOG_SERVER_SRC_WATCHDOGBINDERMEDIATOR_H_