• 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_SERVICEMANAGER_H_
18 #define CPP_WATCHDOG_SERVER_SRC_SERVICEMANAGER_H_
19 
20 #include "IoOveruseMonitor.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 
getInstance()45     static android::sp<ServiceManager> getInstance() {
46         if (sServiceManager == nullptr) {
47             sServiceManager = android::sp<ServiceManager>::make();
48         }
49         return sServiceManager;
50     }
51 
terminate()52     static void terminate() {
53         if (sServiceManager == nullptr) {
54             return;
55         }
56         sServiceManager->terminateServices();
57         sServiceManager.clear();
58     }
59 
60     // Starts early-init services.
61     android::base::Result<void> startServices(const android::sp<Looper>& mainLooper);
62 
63     // Returns the WatchdogProcessService instance.
getWatchdogProcessService()64     const android::sp<WatchdogProcessServiceInterface>& getWatchdogProcessService() {
65         return mWatchdogProcessService;
66     }
67 
68     // Returns the WatchdogServiceHelper instance.
getWatchdogServiceHelper()69     const android::sp<WatchdogServiceHelperInterface>& getWatchdogServiceHelper() {
70         return mWatchdogServiceHelper;
71     }
72 
73     // Returns the IoOveruseMonitor instance.
getIoOveruseMonitor()74     const android::sp<IoOveruseMonitorInterface>& getIoOveruseMonitor() {
75         return mIoOveruseMonitor;
76     }
77 
78 private:
79     inline static android::sp<ServiceManager> sServiceManager = nullptr;
80 
81     void terminateServices();
82     android::base::Result<void> startWatchdogProcessService(const android::sp<Looper>& mainLooper);
83     android::base::Result<void> startWatchdogPerfService(
84             const sp<WatchdogServiceHelperInterface>& watchdogServiceHelper);
85 
86     android::sp<WatchdogProcessServiceInterface> mWatchdogProcessService;
87     android::sp<WatchdogPerfServiceInterface> mWatchdogPerfService;
88     std::shared_ptr<WatchdogBinderMediatorInterface> mWatchdogBinderMediator;
89     android::sp<WatchdogServiceHelperInterface> mWatchdogServiceHelper;
90     android::sp<IoOveruseMonitorInterface> mIoOveruseMonitor;
91 };
92 
93 }  // namespace watchdog
94 }  // namespace automotive
95 }  // namespace android
96 
97 #endif  // CPP_WATCHDOG_SERVER_SRC_SERVICEMANAGER_H_
98