1094332d3Sopenharmony_ci/* 2094332d3Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd. 3094332d3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4094332d3Sopenharmony_ci * you may not use this file except in compliance with the License. 5094332d3Sopenharmony_ci * You may obtain a copy of the License at 6094332d3Sopenharmony_ci * 7094332d3Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8094332d3Sopenharmony_ci * 9094332d3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10094332d3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11094332d3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12094332d3Sopenharmony_ci * See the License for the specific language governing permissions and 13094332d3Sopenharmony_ci * limitations under the License. 14094332d3Sopenharmony_ci */ 15094332d3Sopenharmony_ci 16094332d3Sopenharmony_ci#include "running_lock_counter.h" 17094332d3Sopenharmony_ci#ifdef HAS_POWER_HISYSEVENT_PART 18094332d3Sopenharmony_ci#include "hisysevent.h" 19094332d3Sopenharmony_ci#endif 20094332d3Sopenharmony_ci#include "hdf_base.h" 21094332d3Sopenharmony_ci#include "system_operation.h" 22094332d3Sopenharmony_ci#include "power_hdf_log.h" 23094332d3Sopenharmony_ci 24094332d3Sopenharmony_cinamespace OHOS { 25094332d3Sopenharmony_cinamespace HDI { 26094332d3Sopenharmony_cinamespace Power { 27094332d3Sopenharmony_cinamespace V1_2 { 28094332d3Sopenharmony_ciint32_t RunningLockCounter::Increase(const RunningLockInfo &info) 29094332d3Sopenharmony_ci{ 30094332d3Sopenharmony_ci auto iterator = runninglockInfos_.find(info.name); 31094332d3Sopenharmony_ci if (iterator != runninglockInfos_.end()) { 32094332d3Sopenharmony_ci if (info.timeoutMs < 0) { 33094332d3Sopenharmony_ci HDF_LOGW("Lock counter increase failed, runninglock name=%{public}s is exist and timeout < 0", 34094332d3Sopenharmony_ci info.name.c_str()); 35094332d3Sopenharmony_ci return HDF_FAILURE; 36094332d3Sopenharmony_ci } 37094332d3Sopenharmony_ci iterator->second.timeoutMs = info.timeoutMs; 38094332d3Sopenharmony_ci return HDF_SUCCESS; 39094332d3Sopenharmony_ci } 40094332d3Sopenharmony_ci ++counter_; 41094332d3Sopenharmony_ci if (counter_ == 1) { 42094332d3Sopenharmony_ci SystemOperation::WriteWakeLock(tag_); 43094332d3Sopenharmony_ci } 44094332d3Sopenharmony_ci runninglockInfos_.emplace(info.name, info); 45094332d3Sopenharmony_ci NotifyHiView(info, ChangedType::NOTIFY_RUNNINGLOCK_ADD, RunningLockState::RUNNINGLOCK_STATE_ENABLE); 46094332d3Sopenharmony_ci return HDF_SUCCESS; 47094332d3Sopenharmony_ci} 48094332d3Sopenharmony_ci 49094332d3Sopenharmony_ciint32_t RunningLockCounter::Decrease(const RunningLockInfo &info) 50094332d3Sopenharmony_ci{ 51094332d3Sopenharmony_ci auto iterator = runninglockInfos_.find(info.name); 52094332d3Sopenharmony_ci if (iterator == runninglockInfos_.end()) { 53094332d3Sopenharmony_ci HDF_LOGW("Runninglock name=%{public}s is not exist, no need to decrease lock counter", info.name.c_str()); 54094332d3Sopenharmony_ci return HDF_ERR_NOT_SUPPORT; 55094332d3Sopenharmony_ci } 56094332d3Sopenharmony_ci --counter_; 57094332d3Sopenharmony_ci if (counter_ == 0) { 58094332d3Sopenharmony_ci SystemOperation::WriteWakeUnlock(tag_); 59094332d3Sopenharmony_ci } 60094332d3Sopenharmony_ci runninglockInfos_.erase(info.name); 61094332d3Sopenharmony_ci NotifyHiView(info, ChangedType::NOTIFY_RUNNINGLOCK_REMOVE, RunningLockState::RUNNINGLOCK_STATE_DISABLE); 62094332d3Sopenharmony_ci return HDF_SUCCESS; 63094332d3Sopenharmony_ci} 64094332d3Sopenharmony_ci 65094332d3Sopenharmony_civoid RunningLockCounter::Clean() 66094332d3Sopenharmony_ci{ 67094332d3Sopenharmony_ci runninglockInfos_.clear(); 68094332d3Sopenharmony_ci} 69094332d3Sopenharmony_ci 70094332d3Sopenharmony_civoid RunningLockCounter::NotifyHiView(const RunningLockInfo &info, ChangedType changeType, RunningLockState state) 71094332d3Sopenharmony_ci{ 72094332d3Sopenharmony_ci#ifdef HAS_POWER_HISYSEVENT_PART 73094332d3Sopenharmony_ci const int logLevel = 2; 74094332d3Sopenharmony_ci const std::string &tag = runninglockNotifyStr_.at(changeType); 75094332d3Sopenharmony_ci const std::string bundleName = ""; 76094332d3Sopenharmony_ci HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::POWER, "RUNNINGLOCK", 77094332d3Sopenharmony_ci HiviewDFX::HiSysEvent::EventType::STATISTIC, 78094332d3Sopenharmony_ci "PID", info.pid, "UID", info.uid, "STATE", static_cast<int32_t>(state), "TYPE", type_, "NAME", info.name, 79094332d3Sopenharmony_ci "BUNDLENAME", bundleName, "LOG_LEVEL", logLevel, "TAG", tag); 80094332d3Sopenharmony_ci#endif 81094332d3Sopenharmony_ci} 82094332d3Sopenharmony_ci} // namespace V1_2 83094332d3Sopenharmony_ci} // namespace Power 84094332d3Sopenharmony_ci} // namespace HDI 85094332d3Sopenharmony_ci} // namespace OHOS 86