1 /*
2 * Copyright (C) 2024 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 <cstdint>
18 #include <cstdlib>
19
20 #include <android-base/logging.h>
21 #include <gflags/gflags.h>
22
23 #include "common/libs/fs/shared_fd.h"
24 #include "common/libs/utils/result.h"
25 #include "common/libs/utils/subprocess.h"
26 #include "host/libs/command_util/runner/defs.h"
27 #include "host/libs/command_util/util.h"
28 #include "host/libs/config/cuttlefish_config.h"
29
30 DEFINE_int32(instance_num, cuttlefish::GetInstance(),
31 "Which instance to trigger a power button event.");
32
33 namespace cuttlefish {
34 namespace {
35
PowerbtnCvdMain()36 Result<void> PowerbtnCvdMain() {
37 const CuttlefishConfig* config =
38 CF_EXPECT(CuttlefishConfig::Get(), "Failed to obtain config object");
39 auto instance = config->ForInstance(FLAGS_instance_num);
40
41 Command command(instance.crosvm_binary());
42 command.AddParameter("powerbtn");
43 command.AddParameter(instance.CrosvmSocketPath());
44
45 LOG(INFO) << "Pressing power button";
46 std::string output;
47 std::string error;
48 auto ret = RunWithManagedStdio(std::move(command), NULL, &output, &error);
49 CF_EXPECT_EQ(ret, 0,
50 "crosvm powerbtn returned: " << ret << "\n"
51 << output << "\n"
52 << error);
53 return {};
54 }
55
56 } // namespace
57 } // namespace cuttlefish
58
main(int argc,char ** argv)59 int main(int argc, char** argv) {
60 ::android::base::InitLogging(argv, android::base::StderrLogger);
61 google::ParseCommandLineFlags(&argc, &argv, true);
62 cuttlefish::Result<void> result = cuttlefish::PowerbtnCvdMain();
63 if (!result.ok()) {
64 LOG(ERROR) << result.error().FormatForEnv();
65 return EXIT_FAILURE;
66 }
67 return EXIT_SUCCESS;
68 }
69