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