• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 #include "running_lock_timer_handler.h"
16 
17 #include "common_timer_errors.h"
18 #include "hdf_log.h"
19 
20 namespace OHOS {
21 namespace HDI {
22 namespace Power {
23 namespace V1_1 {
24 namespace {
25 const std::string RUNNINGLOCK_TIMER_HANDLER_NAME = "RunningLock.Timer.Handler";
26 }
~RunningLockTimerHandler()27 RunningLockTimerHandler::~RunningLockTimerHandler()
28 {
29     if (handlerTimer_ != nullptr) {
30         handlerTimer_->Shutdown();
31     }
32 }
33 
RegisterRunningLockTimer(const RunningLockInfo & info,const std::function<void ()> & callback,bool once)34 bool RunningLockTimerHandler::RegisterRunningLockTimer(
35     const RunningLockInfo &info, const std::function<void()> &callback, bool once)
36 {
37     if (handlerTimer_ == nullptr) {
38         handlerTimer_ = std::make_unique<OHOS::Utils::Timer>(RUNNINGLOCK_TIMER_HANDLER_NAME);
39         handlerTimer_->Setup();
40     }
41     RunningLockType runninglockType = info.type;
42     std::string runninglockName = info.name;
43     uint32_t timeoutMs = info.timeoutMs;
44     uint32_t lastTimerId = GetRunningLockTimerId(runninglockType, runninglockName);
45     if (lastTimerId != OHOS::Utils::TIMER_ERR_DEAL_FAILED) {
46         HDF_LOGI("Running lock timer is exist, unregister old timerId=%{public}d, register new timer", lastTimerId);
47         UnregisterTimer(lastTimerId);
48     }
49     uint32_t curTimerId = handlerTimer_->Register(callback, timeoutMs, once);
50     if (curTimerId == OHOS::Utils::TIMER_ERR_DEAL_FAILED) {
51         HDF_LOGW("Register running lock timer failed");
52         if (lastTimerId != OHOS::Utils::TIMER_ERR_DEAL_FAILED) {
53             RemoveRunningLockTimerMap(runninglockType, runninglockName);
54         }
55         return false;
56     }
57     AddRunningLockTimerMap(runninglockType, runninglockName, curTimerId);
58     return true;
59 }
60 
UnregisterRunningLockTimer(const RunningLockInfo & info)61 bool RunningLockTimerHandler::UnregisterRunningLockTimer(const RunningLockInfo &info)
62 {
63     RunningLockType runninglockType = info.type;
64     std::string runninglockName = info.name;
65     uint32_t timerId = GetRunningLockTimerId(runninglockType, runninglockName);
66     if (timerId != OHOS::Utils::TIMER_ERR_DEAL_FAILED) {
67         HDF_LOGI("Running lock timer is exist, unregister timerId=%{public}d", timerId);
68         UnregisterTimer(timerId);
69         RemoveRunningLockTimerMap(runninglockType, runninglockName);
70     }
71     return true;
72 }
73 
GetRunningLockTimerId(RunningLockType type,std::string name)74 uint32_t RunningLockTimerHandler::GetRunningLockTimerId(RunningLockType type, std::string name)
75 {
76     uint32_t timerId = OHOS::Utils::TIMER_ERR_DEAL_FAILED;
77     auto typeIter = runninglockTimerMap_.find(type);
78     if (typeIter != runninglockTimerMap_.end()) {
79         auto nameIter = typeIter->second.find(name);
80         if (nameIter != typeIter->second.end()) {
81             timerId = nameIter->second;
82         }
83     }
84     return timerId;
85 }
86 
AddRunningLockTimerMap(RunningLockType type,std::string name,uint32_t timerId)87 void RunningLockTimerHandler::AddRunningLockTimerMap(RunningLockType type, std::string name, uint32_t timerId)
88 {
89     auto typeIter = runninglockTimerMap_.find(type);
90     if (typeIter == runninglockTimerMap_.end()) {
91         std::map<std::string, uint32_t> timerIdMap;
92         timerIdMap.emplace(name, timerId);
93         runninglockTimerMap_.emplace(type, timerIdMap);
94         return;
95     }
96     auto nameIter = typeIter->second.find(name);
97     if (nameIter == typeIter->second.end()) {
98         typeIter->second.emplace(name, timerId);
99         return;
100     }
101     nameIter->second = timerId;
102 }
103 
RemoveRunningLockTimerMap(RunningLockType type,std::string name)104 void RunningLockTimerHandler::RemoveRunningLockTimerMap(RunningLockType type, std::string name)
105 {
106     auto typeIter = runninglockTimerMap_.find(type);
107     if (typeIter != runninglockTimerMap_.end()) {
108         auto nameIter = typeIter->second.find(name);
109         if (nameIter != typeIter->second.end()) {
110             typeIter->second.erase(name);
111             if (typeIter->second.size() == 0) {
112                 runninglockTimerMap_.erase(type);
113             }
114         }
115     }
116 }
117 
UnregisterTimer(uint32_t timerId)118 void RunningLockTimerHandler::UnregisterTimer(uint32_t timerId)
119 {
120     if (handlerTimer_ != nullptr) {
121         handlerTimer_->Unregister(timerId);
122     }
123 }
124 } // namespace V1_1
125 } // namespace Power
126 } // namespace HDI
127 } // namespace OHOS