1cb57344ce792f41e58bc476840a82eba14b0fde9Daniel Erat/*
2cb57344ce792f41e58bc476840a82eba14b0fde9Daniel Erat * Copyright (C) 2015 The Android Open Source Project
3cb57344ce792f41e58bc476840a82eba14b0fde9Daniel Erat *
4cb57344ce792f41e58bc476840a82eba14b0fde9Daniel Erat * Licensed under the Apache License, Version 2.0 (the "License");
5cb57344ce792f41e58bc476840a82eba14b0fde9Daniel Erat * you may not use this file except in compliance with the License.
6cb57344ce792f41e58bc476840a82eba14b0fde9Daniel Erat * You may obtain a copy of the License at
7cb57344ce792f41e58bc476840a82eba14b0fde9Daniel Erat *
8cb57344ce792f41e58bc476840a82eba14b0fde9Daniel Erat *      http://www.apache.org/licenses/LICENSE-2.0
9cb57344ce792f41e58bc476840a82eba14b0fde9Daniel Erat *
10cb57344ce792f41e58bc476840a82eba14b0fde9Daniel Erat * Unless required by applicable law or agreed to in writing, software
11cb57344ce792f41e58bc476840a82eba14b0fde9Daniel Erat * distributed under the License is distributed on an "AS IS" BASIS,
12cb57344ce792f41e58bc476840a82eba14b0fde9Daniel Erat * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13cb57344ce792f41e58bc476840a82eba14b0fde9Daniel Erat * See the License for the specific language governing permissions and
14cb57344ce792f41e58bc476840a82eba14b0fde9Daniel Erat * limitations under the License.
15cb57344ce792f41e58bc476840a82eba14b0fde9Daniel Erat */
16cb57344ce792f41e58bc476840a82eba14b0fde9Daniel Erat
174f13a604088be86b9df607655d75b039aa5da4d2Daniel Erat#include <sys/types.h>
184f13a604088be86b9df607655d75b039aa5da4d2Daniel Erat
19cb57344ce792f41e58bc476840a82eba14b0fde9Daniel Erat#include <map>
204f13a604088be86b9df607655d75b039aa5da4d2Daniel Erat#include <memory>
21cb57344ce792f41e58bc476840a82eba14b0fde9Daniel Erat#include <string>
22c2a4b05a931fbf3ecbf1915049b549b33dae8ba5Daniel Erat#include <vector>
23cb57344ce792f41e58bc476840a82eba14b0fde9Daniel Erat
24cb57344ce792f41e58bc476840a82eba14b0fde9Daniel Erat#include <base/macros.h>
25cb57344ce792f41e58bc476840a82eba14b0fde9Daniel Erat#include <nativepower/BnPowerManager.h>
26cb57344ce792f41e58bc476840a82eba14b0fde9Daniel Erat
27cb57344ce792f41e58bc476840a82eba14b0fde9Daniel Eratnamespace android {
28cb57344ce792f41e58bc476840a82eba14b0fde9Daniel Erat
294f13a604088be86b9df607655d75b039aa5da4d2Daniel Eratclass WakeLockManagerStub;
304f13a604088be86b9df607655d75b039aa5da4d2Daniel Erat
31cb57344ce792f41e58bc476840a82eba14b0fde9Daniel Erat// Stub implementation of BnPowerManager for use in tests.
324f13a604088be86b9df607655d75b039aa5da4d2Daniel Erat//
334f13a604088be86b9df607655d75b039aa5da4d2Daniel Erat// The BinderWrapper singleton must be initialized before using this class.
34cb57344ce792f41e58bc476840a82eba14b0fde9Daniel Eratclass PowerManagerStub : public BnPowerManager {
35cb57344ce792f41e58bc476840a82eba14b0fde9Daniel Erat public:
36cb57344ce792f41e58bc476840a82eba14b0fde9Daniel Erat  PowerManagerStub();
37cb57344ce792f41e58bc476840a82eba14b0fde9Daniel Erat  ~PowerManagerStub() override;
38cb57344ce792f41e58bc476840a82eba14b0fde9Daniel Erat
39cb57344ce792f41e58bc476840a82eba14b0fde9Daniel Erat  // Constructs a string that can be compared with one returned by
404f13a604088be86b9df607655d75b039aa5da4d2Daniel Erat  // GetWakeLockString().
414f13a604088be86b9df607655d75b039aa5da4d2Daniel Erat  static std::string ConstructWakeLockString(const std::string& tag,
424f13a604088be86b9df607655d75b039aa5da4d2Daniel Erat                                             const std::string& package,
434f13a604088be86b9df607655d75b039aa5da4d2Daniel Erat                                             uid_t uid);
44cb57344ce792f41e58bc476840a82eba14b0fde9Daniel Erat
45e4d4a81d6b56d66970f0332c6db2b898f60510c8Daniel Erat  // Constructs a string that can be compared with one returned by
46e4d4a81d6b56d66970f0332c6db2b898f60510c8Daniel Erat  // GetSuspendRequestString().
47e4d4a81d6b56d66970f0332c6db2b898f60510c8Daniel Erat  static std::string ConstructSuspendRequestString(int64_t event_time_ms,
48e4d4a81d6b56d66970f0332c6db2b898f60510c8Daniel Erat                                                   int reason,
49e4d4a81d6b56d66970f0332c6db2b898f60510c8Daniel Erat                                                   int flags);
50cb57344ce792f41e58bc476840a82eba14b0fde9Daniel Erat
51e4d4a81d6b56d66970f0332c6db2b898f60510c8Daniel Erat  size_t num_suspend_requests() const { return suspend_requests_.size(); }
52c2a4b05a931fbf3ecbf1915049b549b33dae8ba5Daniel Erat  const std::vector<std::string>& reboot_reasons() const {
53c2a4b05a931fbf3ecbf1915049b549b33dae8ba5Daniel Erat    return reboot_reasons_;
54c2a4b05a931fbf3ecbf1915049b549b33dae8ba5Daniel Erat  }
55c2a4b05a931fbf3ecbf1915049b549b33dae8ba5Daniel Erat  const std::vector<std::string>& shutdown_reasons() const {
56c2a4b05a931fbf3ecbf1915049b549b33dae8ba5Daniel Erat    return shutdown_reasons_;
57c2a4b05a931fbf3ecbf1915049b549b33dae8ba5Daniel Erat  }
58c2a4b05a931fbf3ecbf1915049b549b33dae8ba5Daniel Erat
594f13a604088be86b9df607655d75b039aa5da4d2Daniel Erat  // Returns the number of currently-registered wake locks.
604f13a604088be86b9df607655d75b039aa5da4d2Daniel Erat  int GetNumWakeLocks() const;
614f13a604088be86b9df607655d75b039aa5da4d2Daniel Erat
624f13a604088be86b9df607655d75b039aa5da4d2Daniel Erat  // Returns a string describing the wake lock registered for |binder|, or an
634f13a604088be86b9df607655d75b039aa5da4d2Daniel Erat  // empty string if no wake lock is present.
644f13a604088be86b9df607655d75b039aa5da4d2Daniel Erat  std::string GetWakeLockString(const sp<IBinder>& binder) const;
65cb57344ce792f41e58bc476840a82eba14b0fde9Daniel Erat
66e4d4a81d6b56d66970f0332c6db2b898f60510c8Daniel Erat  // Returns a string describing position |index| in |suspend_requests_|.
67e4d4a81d6b56d66970f0332c6db2b898f60510c8Daniel Erat  std::string GetSuspendRequestString(size_t index) const;
68e4d4a81d6b56d66970f0332c6db2b898f60510c8Daniel Erat
69cb57344ce792f41e58bc476840a82eba14b0fde9Daniel Erat  // BnPowerManager:
70cb57344ce792f41e58bc476840a82eba14b0fde9Daniel Erat  status_t acquireWakeLock(int flags,
71cb57344ce792f41e58bc476840a82eba14b0fde9Daniel Erat                           const sp<IBinder>& lock,
72cb57344ce792f41e58bc476840a82eba14b0fde9Daniel Erat                           const String16& tag,
73cb57344ce792f41e58bc476840a82eba14b0fde9Daniel Erat                           const String16& packageName,
74cb57344ce792f41e58bc476840a82eba14b0fde9Daniel Erat                           bool isOneWay=false) override;
75cb57344ce792f41e58bc476840a82eba14b0fde9Daniel Erat  status_t acquireWakeLockWithUid(int flags,
76cb57344ce792f41e58bc476840a82eba14b0fde9Daniel Erat                                  const sp<IBinder>& lock,
77cb57344ce792f41e58bc476840a82eba14b0fde9Daniel Erat                                  const String16& tag,
78cb57344ce792f41e58bc476840a82eba14b0fde9Daniel Erat                                  const String16& packageName,
79cb57344ce792f41e58bc476840a82eba14b0fde9Daniel Erat                                  int uid,
80cb57344ce792f41e58bc476840a82eba14b0fde9Daniel Erat                                  bool isOneWay=false) override;
81cb57344ce792f41e58bc476840a82eba14b0fde9Daniel Erat  status_t releaseWakeLock(const sp<IBinder>& lock,
82cb57344ce792f41e58bc476840a82eba14b0fde9Daniel Erat                           int flags,
83cb57344ce792f41e58bc476840a82eba14b0fde9Daniel Erat                           bool isOneWay=false) override;
84cb57344ce792f41e58bc476840a82eba14b0fde9Daniel Erat  status_t updateWakeLockUids(const sp<IBinder>& lock,
85cb57344ce792f41e58bc476840a82eba14b0fde9Daniel Erat                              int len,
86cb57344ce792f41e58bc476840a82eba14b0fde9Daniel Erat                              const int* uids,
87cb57344ce792f41e58bc476840a82eba14b0fde9Daniel Erat                              bool isOneWay=false) override;
88cb57344ce792f41e58bc476840a82eba14b0fde9Daniel Erat  status_t powerHint(int hintId, int data) override;
8977ca78c8d297564db431a5b3a9165e170bb7c72aDaniel Erat  status_t goToSleep(int64_t event_time_ms, int reason, int flags) override;
9077ca78c8d297564db431a5b3a9165e170bb7c72aDaniel Erat  status_t reboot(bool confirm, const String16& reason, bool wait) override;
9177ca78c8d297564db431a5b3a9165e170bb7c72aDaniel Erat  status_t shutdown(bool confirm, const String16& reason, bool wait) override;
9277ca78c8d297564db431a5b3a9165e170bb7c72aDaniel Erat  status_t crash(const String16& message) override;
93cb57344ce792f41e58bc476840a82eba14b0fde9Daniel Erat
94cb57344ce792f41e58bc476840a82eba14b0fde9Daniel Erat private:
95e4d4a81d6b56d66970f0332c6db2b898f60510c8Daniel Erat  // Details about a request passed to goToSleep().
96e4d4a81d6b56d66970f0332c6db2b898f60510c8Daniel Erat  struct SuspendRequest {
976df89a05e0ff87b7d51be58b6e39407034f9e6f5Alex Vakulenko    SuspendRequest(int64_t uptime_ms, int reason, int flags);
98e4d4a81d6b56d66970f0332c6db2b898f60510c8Daniel Erat
996df89a05e0ff87b7d51be58b6e39407034f9e6f5Alex Vakulenko    int64_t event_time_ms;
100e4d4a81d6b56d66970f0332c6db2b898f60510c8Daniel Erat    int reason;
101e4d4a81d6b56d66970f0332c6db2b898f60510c8Daniel Erat    int flags;
102e4d4a81d6b56d66970f0332c6db2b898f60510c8Daniel Erat  };
103e4d4a81d6b56d66970f0332c6db2b898f60510c8Daniel Erat
1044f13a604088be86b9df607655d75b039aa5da4d2Daniel Erat  std::unique_ptr<WakeLockManagerStub> wake_lock_manager_;
105cb57344ce792f41e58bc476840a82eba14b0fde9Daniel Erat
106e4d4a81d6b56d66970f0332c6db2b898f60510c8Daniel Erat  // Information about calls to goToSleep(), in the order they were made.
107e4d4a81d6b56d66970f0332c6db2b898f60510c8Daniel Erat  using SuspendRequests = std::vector<SuspendRequest>;
108e4d4a81d6b56d66970f0332c6db2b898f60510c8Daniel Erat  SuspendRequests suspend_requests_;
109e4d4a81d6b56d66970f0332c6db2b898f60510c8Daniel Erat
110c2a4b05a931fbf3ecbf1915049b549b33dae8ba5Daniel Erat  // Reasons passed to reboot() and shutdown(), in the order in which they were
111c2a4b05a931fbf3ecbf1915049b549b33dae8ba5Daniel Erat  // received.
112c2a4b05a931fbf3ecbf1915049b549b33dae8ba5Daniel Erat  std::vector<std::string> reboot_reasons_;
113c2a4b05a931fbf3ecbf1915049b549b33dae8ba5Daniel Erat  std::vector<std::string> shutdown_reasons_;
114c2a4b05a931fbf3ecbf1915049b549b33dae8ba5Daniel Erat
115cb57344ce792f41e58bc476840a82eba14b0fde9Daniel Erat  DISALLOW_COPY_AND_ASSIGN(PowerManagerStub);
116cb57344ce792f41e58bc476840a82eba14b0fde9Daniel Erat};
117cb57344ce792f41e58bc476840a82eba14b0fde9Daniel Erat
118cb57344ce792f41e58bc476840a82eba14b0fde9Daniel Erat}  // namespace android
119