1/*
2 * Copyright (C) 2015 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 <unistd.h>
18
19#include <memory>
20
21#include <base/at_exit.h>
22#include <base/logging.h>
23#include <base/message_loop/message_loop.h>
24#include <base/sys_info.h>
25#include <base/time/time.h>
26#include <binderwrapper/binder_wrapper.h>
27#include <brillo/flag_helper.h>
28#include <nativepower/power_manager_client.h>
29#include <nativepower/wake_lock.h>
30
31namespace {
32
33// Seconds to sleep after acquiring a wake lock.
34const int kWakeLockSleepSec = 5;
35
36}  // namespace
37
38int main(int argc, char *argv[]) {
39  DEFINE_string(action, "",
40                "Action to perform (\"reboot\", \"shut_down\", \"suspend\", "
41                "\"wake_lock\")");
42
43  brillo::FlagHelper::Init(argc, argv, "Example power-management client.");
44  logging::InitLogging(logging::LoggingSettings());
45  base::AtExitManager at_exit;
46  base::MessageLoopForIO loop;
47  android::BinderWrapper::Create();
48
49  android::PowerManagerClient client;
50  CHECK(client.Init());
51
52  if (FLAGS_action == "reboot") {
53    LOG(INFO) << "Requesting reboot";
54    CHECK(client.Reboot(android::RebootReason::DEFAULT));
55  } else if (FLAGS_action == "shut_down") {
56    LOG(INFO) << "Requesting shutdown";
57    CHECK(client.ShutDown(android::ShutdownReason::DEFAULT));
58  } else if (FLAGS_action == "suspend") {
59    LOG(INFO) << "Requesting suspend";
60    CHECK(client.Suspend(base::SysInfo::Uptime(),
61                         android::SuspendReason::APPLICATION, 0 /* flags */));
62  } else if (FLAGS_action == "wake_lock") {
63    LOG(INFO) << "Creating wake lock";
64    std::unique_ptr<android::WakeLock> lock(
65        client.CreateWakeLock("power_example", "power"));
66    CHECK(lock) << "Lock not created";
67    LOG(INFO) << "Sleeping for " << kWakeLockSleepSec << " seconds";
68    sleep(kWakeLockSleepSec);
69  } else {
70    LOG(FATAL) << "Unknown action \"" << FLAGS_action << "\"";
71  }
72
73  LOG(INFO) << "Exiting";
74  return 0;
75}
76