• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef POWERMGR_RUNNING_LOCK_HUB_H
17 #define POWERMGR_RUNNING_LOCK_HUB_H
18 
19 #include <map>
20 #include <memory>
21 #include <mutex>
22 #include <string>
23 
24 #include <unique_fd.h>
25 
26 #include "suspend/irunning_lock_hub.h"
27 #include "suspend/isuspend_controller.h"
28 
29 namespace OHOS {
30 namespace PowerMgr {
31 namespace Suspend {
32 class RunningLockHub : public IRunningLockHub {
33 public:
RunningLockHub(const std::shared_ptr<ISuspendController> & sc)34     explicit RunningLockHub(const std::shared_ptr<ISuspendController>& sc) : sc_(sc) {};
~RunningLockHub()35     ~RunningLockHub() override {};
36 
37     void Acquire(const std::string& name) override;
38     void Release(const std::string& name) override;
39 
40 private:
41     bool InitFd();
42     bool SaveLockFile(const std::string& name, bool isAcquire);
43     void NotifySuspendCounter(bool isAcquire);
44 
45     static constexpr const char * const LOCK_PATH = "/sys/power/wake_lock";
46     static constexpr const char * const UNLOCK_PATH = "/sys/power/wake_unlock";
47 
48     UniqueFd lockFd_{-1};
49     UniqueFd unlockFd_{-1};
50     std::shared_ptr<ISuspendController> sc_;
51     std::mutex mutex_;
52     std::map<std::string, uint32_t> runningLockMap_;
53 };
54 } // namespace Suspend
55 } // namespace PowerMgr
56 } // namespace OHOS
57 #endif // POWERMGR_RUNNING_LOCK_HUB_H
58