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