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_SERVICEMANAGER_H_
18 #define CPP_WATCHDOG_SERVER_SRC_SERVICEMANAGER_H_
19 
20 #include "IoOveruseMonitor.h"
21 #include "PressureMonitor.h"
22 #include "WatchdogBinderMediator.h"
23 #include "WatchdogPerfService.h"
24 #include "WatchdogProcessService.h"
25 #include "WatchdogServiceHelper.h"
26 
27 #include <android-base/result.h>
28 #include <utils/Looper.h>
29 #include <utils/RefBase.h>
30 #include <utils/StrongPointer.h>
31 
32 namespace android {
33 namespace automotive {
34 namespace watchdog {
35 
36 // Manages all the services that are run by the car watchdog daemon.
37 class ServiceManager final : virtual public android::RefBase {
38 public:
ServiceManager()39     ServiceManager() :
40           mWatchdogProcessService(nullptr),
41           mWatchdogPerfService(nullptr),
42           mWatchdogBinderMediator(nullptr),
43           mWatchdogServiceHelper(nullptr),
44           mIoOveruseMonitor(nullptr),
45           mPressureMonitor(nullptr) {}
46 
47     // Returns the singleton ServiceManager instance.
getInstance()48     static android::sp<ServiceManager> getInstance() {
49         if (sServiceManager == nullptr) {
50             sServiceManager = android::sp<ServiceManager>::make();
51         }
52         return sServiceManager;
53     }
54 
55     // Terminates all services and resets the singleton instance.
terminate()56     static void terminate() {
57         if (sServiceManager == nullptr) {
58             return;
59         }
60         sServiceManager->terminateServices();
61         sServiceManager.clear();
62     }
63 
64     // Starts early-init services.
65     android::base::Result<void> startServices(const android::sp<Looper>& mainLooper);
66 
67     // Returns the WatchdogProcessService instance.
getWatchdogProcessService()68     const android::sp<WatchdogProcessServiceInterface>& getWatchdogProcessService() {
69         return mWatchdogProcessService;
70     }
71 
72     // Returns the WatchdogServiceHelper instance.
getWatchdogServiceHelper()73     const android::sp<WatchdogServiceHelperInterface>& getWatchdogServiceHelper() {
74         return mWatchdogServiceHelper;
75     }
76 
77     // Returns the IoOveruseMonitor instance.
getIoOveruseMonitor()78     const android::sp<IoOveruseMonitorInterface>& getIoOveruseMonitor() {
79         return mIoOveruseMonitor;
80     }
81 
82 private:
83     inline static android::sp<ServiceManager> sServiceManager = nullptr;
84 
85     void terminateServices();
86     android::base::Result<void> startWatchdogProcessService(const android::sp<Looper>& mainLooper);
87     android::base::Result<void> startPressureMonitor();
88     android::base::Result<void> startWatchdogPerfService(
89             const sp<WatchdogServiceHelperInterface>& watchdogServiceHelper);
90 
91     android::sp<WatchdogProcessServiceInterface> mWatchdogProcessService;
92     android::sp<WatchdogPerfServiceInterface> mWatchdogPerfService;
93     std::shared_ptr<WatchdogBinderMediatorInterface> mWatchdogBinderMediator;
94     android::sp<WatchdogServiceHelperInterface> mWatchdogServiceHelper;
95     android::sp<IoOveruseMonitorInterface> mIoOveruseMonitor;
96     android::sp<PressureMonitorInterface> mPressureMonitor;
97 };
98 
99 }  // namespace watchdog
100 }  // namespace automotive
101 }  // namespace android
102 
103 #endif  // CPP_WATCHDOG_SERVER_SRC_SERVICEMANAGER_H_
104