Home | History | Annotate | Download | only in daemon
      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 #ifndef SYSTEM_NATIVEPOWER_DAEMON_POWER_MANAGER_H_
     18 #define SYSTEM_NATIVEPOWER_DAEMON_POWER_MANAGER_H_
     19 
     20 #include <memory>
     21 
     22 #include <base/files/file_path.h>
     23 #include <base/macros.h>
     24 #include <base/time/time.h>
     25 #include <nativepower/BnPowerManager.h>
     26 
     27 #include "system_property_setter.h"
     28 #include "wake_lock_manager.h"
     29 
     30 namespace android {
     31 
     32 class PowerManager : public BnPowerManager {
     33  public:
     34   // The part of the reboot or shutdown system properties' values that appears
     35   // before the reason. These strings are hardcoded in
     36   // system/core/init/builtins.cpp.
     37   static const char kRebootPrefix[];
     38   static const char kShutdownPrefix[];
     39 
     40   // Value written to |power_state_path_| to suspend the system to memory.
     41   static const char kPowerStateSuspend[];
     42 
     43   PowerManager();
     44   ~PowerManager() override;
     45 
     46   // Must be called before Init().
     47   void set_property_setter_for_testing(
     48       std::unique_ptr<SystemPropertySetterInterface> setter) {
     49     property_setter_ = std::move(setter);
     50   }
     51 
     52   // Must be called before Init().
     53   void set_wake_lock_manager_for_testing(
     54       std::unique_ptr<WakeLockManagerInterface> manager) {
     55     wake_lock_manager_ = std::move(manager);
     56   }
     57 
     58   void set_power_state_path_for_testing(const base::FilePath& path) {
     59     power_state_path_ = path;
     60   }
     61 
     62   // Initializes the object, returning true on success.
     63   bool Init();
     64 
     65   // BnPowerManager:
     66   status_t acquireWakeLock(int flags,
     67                            const sp<IBinder>& lock,
     68                            const String16& tag,
     69                            const String16& packageName,
     70                            bool isOneWay=false) override;
     71   status_t acquireWakeLockWithUid(int flags,
     72                                   const sp<IBinder>& lock,
     73                                   const String16& tag,
     74                                   const String16& packageName,
     75                                   int uid,
     76                                   bool isOneWay=false) override;
     77   status_t releaseWakeLock(const sp<IBinder>& lock,
     78                            int flags,
     79                            bool isOneWay=false) override;
     80   status_t updateWakeLockUids(const sp<IBinder>& lock,
     81                               int len,
     82                               const int* uids,
     83                               bool isOneWay=false) override;
     84   status_t powerHint(int hintId, int data) override;
     85   status_t goToSleep(int64_t event_time_ms, int reason, int flags) override;
     86   status_t reboot(bool confirm, const String16& reason, bool wait) override;
     87   status_t shutdown(bool confirm, const String16& reason, bool wait) override;
     88   status_t crash(const String16& message) override;
     89 
     90  private:
     91   // Helper method for acquireWakeLock*(). Returns true on success.
     92   bool AddWakeLockRequest(const sp<IBinder>& lock,
     93                           const String16& tag,
     94                           const String16& packageName,
     95                           int uid);
     96 
     97   std::unique_ptr<SystemPropertySetterInterface> property_setter_;
     98   std::unique_ptr<WakeLockManagerInterface> wake_lock_manager_;
     99 
    100   // Path to sysfs file that can be written to change the power state.
    101   base::FilePath power_state_path_;
    102 
    103   // System uptime (as duration since boot) when userspace was last resumed from
    104   // suspend. Initially unset.
    105   base::TimeDelta last_resume_uptime_;
    106 
    107   DISALLOW_COPY_AND_ASSIGN(PowerManager);
    108 };
    109 
    110 }  // namespace android
    111 
    112 #endif  // SYSTEM_NATIVEPOWER_DAEMON_POWER_MANAGER_H_
    113