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 #define LOG_TAG "powerhal-libperfmgr"
18
19 #include <android-base/logging.h>
20 #include <android-base/properties.h>
21 #include <android/binder_ibinder_platform.h>
22 #include <android/binder_manager.h>
23 #include <android/binder_process.h>
24 #include <perfmgr/HintManager.h>
25
26 #include <thread>
27
28 #include "MetricUploader.h"
29 #include "Power.h"
30 #include "PowerExt.h"
31 #include "PowerSessionManager.h"
32 #include "disp-power/DisplayLowPower.h"
33 #include "utils/ThermalStateListener.h"
34
35 using aidl::google::hardware::power::impl::pixel::DisplayLowPower;
36 using aidl::google::hardware::power::impl::pixel::MetricUploader;
37 using aidl::google::hardware::power::impl::pixel::Power;
38 using aidl::google::hardware::power::impl::pixel::PowerExt;
39 using aidl::google::hardware::power::impl::pixel::ThermalStateListener;
40 using ::android::perfmgr::HintManager;
41
42 constexpr std::string_view kPowerHalInitProp("vendor.powerhal.init");
43
main()44 int main() {
45 android::base::SetDefaultTag(LOG_TAG);
46 android::base::SetMinimumLogSeverity(android::base::INFO);
47 // Parse config but do not start the looper
48 HintManager *hm = HintManager::GetInstance();
49 if (!hm) {
50 LOG(FATAL) << "HintManager Init failed";
51 }
52
53 std::shared_ptr<DisplayLowPower> dlpw = std::make_shared<DisplayLowPower>();
54
55 // single thread
56 ABinderProcess_setThreadPoolMaxThreadCount(0);
57
58 // core service
59 std::shared_ptr<Power> pw = ndk::SharedRefBase::make<Power>(dlpw);
60 ndk::SpAIBinder pwBinder = pw->asBinder();
61 AIBinder_setMinSchedulerPolicy(pwBinder.get(), SCHED_NORMAL, -20);
62
63 // extension service
64 std::shared_ptr<PowerExt> pwExt = ndk::SharedRefBase::make<PowerExt>(dlpw);
65 auto pwExtBinder = pwExt->asBinder();
66 AIBinder_setMinSchedulerPolicy(pwExtBinder.get(), SCHED_NORMAL, -20);
67
68 // attach the extension to the same binder we will be registering
69 CHECK(STATUS_OK == AIBinder_setExtension(pwBinder.get(), pwExt->asBinder().get()));
70
71 const std::string instance = std::string() + Power::descriptor + "/default";
72 binder_status_t status = AServiceManager_addService(pw->asBinder().get(), instance.c_str());
73 CHECK(status == STATUS_OK);
74 LOG(INFO) << "Pixel Power HAL AIDL Service with Extension is started.";
75
76 std::thread initThread([&]() {
77 ::android::base::WaitForProperty(kPowerHalInitProp.data(), "1");
78 HintManager::GetInstance()->Start();
79 dlpw->Init();
80 MetricUploader::getInstance()->init();
81 ThermalStateListener::getInstance()->init();
82 });
83 initThread.detach();
84
85 ABinderProcess_joinThreadPool();
86
87 // should not reach
88 LOG(ERROR) << "Pixel Power HAL AIDL Service with Extension just died.";
89 return EXIT_FAILURE;
90 }
91