1 /*
2  * Copyright (C) 2023 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 #include <aidl/android/hardware/power/IPower.h>
18 #include <aidl/google/hardware/power/extension/pixel/IPowerExt.h>
19 #include <android-base/logging.h>
20 #include <android/binder_manager.h>
21 
22 #include <getopt.h>
23 
24 using ::aidl::android::hardware::power::IPower;
25 using ::aidl::google::hardware::power::extension::pixel::IPowerExt;
26 
DualLogger(android::base::LogId id,android::base::LogSeverity severity,const char * tag,const char * file,unsigned int line,const char * message)27 static void DualLogger(android::base::LogId id, android::base::LogSeverity severity,
28                        const char *tag, const char *file, unsigned int line, const char *message) {
29     android::base::KernelLogger(id, severity, tag, file, line, message);
30     android::base::StderrLogger(id, severity, tag, file, line, message);
31 }
32 
printUsage(const char * exec_name)33 static void printUsage(const char *exec_name) {
34     android::base::SetLogger(android::base::StderrLogger);
35     std::string usage = exec_name;
36     usage = usage +
37             " is a command-line tool to send hint to Pixel Power HAL.\n"
38             "Usages:\n" +
39             exec_name +
40             " [options]\n"
41             "\n"
42             "Options:\n"
43             "   --mode, -m\n"
44             "       Mode for hint\n\n"
45             "   --enable, -e\n"
46             "       1 for enable mode, 0 for disable\n\n"
47             "   --boost, -b\n"
48             "       Boost for hint\n\n"
49             "   --duration, -d\n"
50             "       Boost duration\n\n"
51             "   --help, -h\n"
52             "       print this message\n\n";
53 
54     LOG(INFO) << usage;
55 }
56 
connect()57 static std::shared_ptr<IPowerExt> connect() {
58     const std::string kInstance = std::string(IPower::descriptor) + "/default";
59     ndk::SpAIBinder power_binder = ndk::SpAIBinder(AServiceManager_getService(kInstance.c_str()));
60     ndk::SpAIBinder ext_power_binder;
61     std::shared_ptr<IPowerExt> powerext = nullptr;
62 
63     if (power_binder.get() == nullptr) {
64         LOG(ERROR) << "Cannot get Power Hal Binder";
65         return powerext;
66     }
67 
68     if (STATUS_OK != AIBinder_getExtension(power_binder.get(), ext_power_binder.getR()) ||
69         ext_power_binder.get() == nullptr) {
70         LOG(ERROR) << "Cannot get Power Hal Extension Binder";
71         return powerext;
72     }
73 
74     powerext = IPowerExt::fromBinder(ext_power_binder);
75     if (powerext == nullptr) {
76         LOG(ERROR) << "Cannot get Power Hal Extension AIDL";
77     }
78 
79     return powerext;
80 }
81 
setMode(std::shared_ptr<IPowerExt> hal,const std::string & type,const bool & enable)82 static bool setMode(std::shared_ptr<IPowerExt> hal, const std::string &type, const bool &enable) {
83     if (!hal->setMode(type, enable).isOk()) {
84         LOG(ERROR) << "Fail to set mode: " << type << " enabled: " << enable;
85         return false;
86     } else {
87         LOG(INFO) << "Set mode: " << type << " enabled: " << enable;
88         return true;
89     }
90 }
91 
setBoost(std::shared_ptr<IPowerExt> hal,const std::string & type,const int32_t duration_ms)92 static bool setBoost(std::shared_ptr<IPowerExt> hal, const std::string &type,
93                      const int32_t duration_ms) {
94     if (!hal->setBoost(type, duration_ms).isOk()) {
95         LOG(ERROR) << "Fail to set boost: " << type << " duration: " << duration_ms;
96         return false;
97     } else {
98         LOG(INFO) << "Set boost: " << type << " duration: " << duration_ms;
99         return true;
100     }
101 }
102 
main(int argc,char * argv[])103 int main(int argc, char *argv[]) {
104     android::base::SetLogger(DualLogger);
105     std::string boost;
106     unsigned int duration_ms = 0;
107     std::string mode;
108     bool enabled = true;
109 
110     static struct option opts[] = {
111             {"boost", optional_argument, nullptr, 'b'},
112             {"duration", optional_argument, nullptr, 'd'},
113             {"mode", optional_argument, nullptr, 'm'},
114             {"enable", optional_argument, nullptr, 'e'},
115             {0, 0, 0, 0}  // termination of the option list
116     };
117 
118     int c = -1;
119     while ((c = getopt_long(argc, argv, "b:d:m:e:h", opts, nullptr)) != -1) {
120         switch (c) {
121             case 'b':
122                 boost = optarg;
123                 break;
124             case 'd':
125                 duration_ms = std::stoi(optarg);
126                 break;
127             case 'm':
128                 mode = optarg;
129                 break;
130             case 'e':
131                 enabled = std::stoi(optarg);
132                 break;
133             case 'h':
134                 printUsage(argv[0]);
135                 return 0;
136             default:
137                 printUsage(argv[0]);
138                 return 1;
139         }
140     }
141     if (boost.empty() && mode.empty()) {
142         LOG(ERROR) << "Need specify a boost or mode to send hint";
143         printUsage(argv[0]);
144         return 1;
145     }
146 
147     std::shared_ptr<IPowerExt> powerext = connect();
148     if (!powerext) {
149         return 1;
150     }
151 
152     if (!boost.empty() && !setBoost(powerext, boost, duration_ms)) {
153         return 1;
154     }
155 
156     if (!mode.empty() && !setMode(powerext, mode, enabled)) {
157         return 1;
158     }
159     return 0;
160 }
161