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
16 #include "running_lock_counter.h"
17 #ifdef HAS_POWER_HISYSEVENT_PART
18 #include "hisysevent.h"
19 #endif
20 #include "hdf_base.h"
21 #include "system_operation.h"
22 #include "power_hdf_log.h"
23
24 namespace OHOS {
25 namespace HDI {
26 namespace Power {
27 namespace V1_2 {
Increase(const RunningLockInfo & info)28 int32_t RunningLockCounter::Increase(const RunningLockInfo &info)
29 {
30 auto iterator = runninglockInfos_.find(info.name);
31 if (iterator != runninglockInfos_.end()) {
32 if (info.timeoutMs < 0) {
33 HDF_LOGW("Lock counter increase failed, runninglock name=%{public}s is exist and timeout < 0",
34 info.name.c_str());
35 return HDF_FAILURE;
36 }
37 iterator->second.timeoutMs = info.timeoutMs;
38 return HDF_SUCCESS;
39 }
40 ++counter_;
41 if (counter_ == 1) {
42 SystemOperation::WriteWakeLock(tag_);
43 }
44 runninglockInfos_.emplace(info.name, info);
45 NotifyHiView(info, ChangedType::NOTIFY_RUNNINGLOCK_ADD, RunningLockState::RUNNINGLOCK_STATE_ENABLE);
46 return HDF_SUCCESS;
47 }
48
Decrease(const RunningLockInfo & info)49 int32_t RunningLockCounter::Decrease(const RunningLockInfo &info)
50 {
51 auto iterator = runninglockInfos_.find(info.name);
52 if (iterator == runninglockInfos_.end()) {
53 HDF_LOGW("Runninglock name=%{public}s is not exist, no need to decrease lock counter", info.name.c_str());
54 return HDF_ERR_NOT_SUPPORT;
55 }
56 --counter_;
57 if (counter_ == 0) {
58 SystemOperation::WriteWakeUnlock(tag_);
59 }
60 runninglockInfos_.erase(info.name);
61 NotifyHiView(info, ChangedType::NOTIFY_RUNNINGLOCK_REMOVE, RunningLockState::RUNNINGLOCK_STATE_DISABLE);
62 return HDF_SUCCESS;
63 }
64
Clean()65 void RunningLockCounter::Clean()
66 {
67 runninglockInfos_.clear();
68 }
69
NotifyHiView(const RunningLockInfo & info,ChangedType changeType,RunningLockState state)70 void RunningLockCounter::NotifyHiView(const RunningLockInfo &info, ChangedType changeType, RunningLockState state)
71 {
72 #ifdef HAS_POWER_HISYSEVENT_PART
73 const int logLevel = 2;
74 const std::string &tag = runninglockNotifyStr_.at(changeType);
75 const std::string bundleName = "";
76 HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::POWER, "RUNNINGLOCK",
77 HiviewDFX::HiSysEvent::EventType::STATISTIC,
78 "PID", info.pid, "UID", info.uid, "STATE", static_cast<int32_t>(state), "TYPE", type_, "NAME", info.name,
79 "BUNDLENAME", bundleName, "LOG_LEVEL", logLevel, "TAG", tag);
80 #endif
81 }
82 } // namespace V1_2
83 } // namespace Power
84 } // namespace HDI
85 } // namespace OHOS
86