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