1 /*
2 * Copyright (C) 2022-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 "tel_ril_handler.h"
17
18 #include "telephony_log_wrapper.h"
19
20 namespace OHOS {
21 namespace Telephony {
ProcessEvent(const AppExecFwk::InnerEvent::Pointer & event)22 void TelRilHandler::ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event)
23 {
24 if (event == nullptr) {
25 TELEPHONY_LOGE("ProcessEvent, event is nullptr");
26 return;
27 }
28 auto id = event->GetInnerEventId();
29 auto serial = event->GetParam();
30 TELEPHONY_LOGD(
31 "ProcessEvent, id:%{public}d, serial:%{public}d, reqLockSerialNum_:%{public}d, ackLockSerialNum_:%{public}d",
32 id, static_cast<int>(serial), static_cast<int>(reqLockSerialNum_), static_cast<int>(ackLockSerialNum_));
33 switch (id) {
34 case RUNNING_LOCK_TIMEOUT_EVENT_ID:
35 if (serial == reqLockSerialNum_) {
36 TELEPHONY_LOGI("Running lock timeout, id:%{public}d, serial:%{public}d, reqLockSerialNum_:%{public}d",
37 id, static_cast<int>(serial), static_cast<int>(reqLockSerialNum_));
38 ReleaseRunningLock(NORMAL_RUNNING_LOCK);
39 }
40 break;
41 case ACK_RUNNING_LOCK_TIMEOUT_EVENT_ID:
42 if (serial == ackLockSerialNum_) {
43 TELEPHONY_LOGI("Running lock timeout, id:%{public}d, serial:%{public}d, ackLockSerialNum_:%{public}d",
44 id, static_cast<int>(serial), static_cast<int>(ackLockSerialNum_));
45 ReleaseRunningLock(ACK_RUNNING_LOCK);
46 }
47 break;
48 default:
49 TELEPHONY_LOGW("ProcessEvent, invalid id:%{public}d.", id);
50 break;
51 }
52 }
53
OnInit(void)54 void TelRilHandler::OnInit(void)
55 {
56 #ifdef ABILITY_POWER_SUPPORT
57 auto &powerMgrClient = PowerMgr::PowerMgrClient::GetInstance();
58 ackRunningLock_ = powerMgrClient.CreateRunningLock(
59 "telRilAckRunningLock", PowerMgr::RunningLockType::RUNNINGLOCK_BACKGROUND_PHONE);
60 reqRunningLock_ = powerMgrClient.CreateRunningLock(
61 "telRilRequestRunningLock", PowerMgr::RunningLockType::RUNNINGLOCK_BACKGROUND_PHONE);
62 #endif
63 reqRunningLockCount_ = 0;
64 reqLockSerialNum_ = 0;
65 ackLockSerialNum_ = 0;
66 }
67
ApplyRunningLock(int32_t lockType)68 void TelRilHandler::ApplyRunningLock(int32_t lockType)
69 {
70 #ifdef ABILITY_POWER_SUPPORT
71 if (ackRunningLock_ == nullptr) {
72 auto &powerMgrClient = PowerMgr::PowerMgrClient::GetInstance();
73 ackRunningLock_ = powerMgrClient.CreateRunningLock(
74 "telRilAckRunningLock", PowerMgr::RunningLockType::RUNNINGLOCK_BACKGROUND_PHONE);
75 ackLockSerialNum_ = 0;
76 }
77 if (reqRunningLock_ == nullptr) {
78 auto &powerMgrClient = PowerMgr::PowerMgrClient::GetInstance();
79 reqRunningLock_ = powerMgrClient.CreateRunningLock(
80 "telRilRequestRunningLock", PowerMgr::RunningLockType::RUNNINGLOCK_BACKGROUND_PHONE);
81 reqRunningLockCount_ = 0;
82 reqLockSerialNum_ = 0;
83 }
84 std::lock_guard<std::mutex> lockRequest(mutexRunningLock_);
85 if ((reqRunningLock_ != nullptr) && (lockType == NORMAL_RUNNING_LOCK)) {
86 reqRunningLockCount_++;
87 reqLockSerialNum_++;
88 if (!reqRunningLock_->IsUsed()) {
89 reqRunningLock_->Lock();
90 }
91 TELEPHONY_LOGD("ApplyRunningLock, reqLockSerialNum_:%{public}d", static_cast<int>(reqLockSerialNum_));
92 this->SendEvent(RUNNING_LOCK_TIMEOUT_EVENT_ID, reqLockSerialNum_, RUNNING_LOCK_DEFAULT_TIMEOUT_MS);
93 } else if (ackRunningLock_ != nullptr && lockType == ACK_RUNNING_LOCK) {
94 ackLockSerialNum_++;
95 ackRunningLock_->Lock(DELAR_RELEASE_RUNNING_LOCK_TIMEOUT_MS);
96 TELEPHONY_LOGI("ApplyRunningLock,ackLockSerialNum_:%{public}d", static_cast<int>(ackLockSerialNum_));
97 this->SendEvent(ACK_RUNNING_LOCK_TIMEOUT_EVENT_ID, ackLockSerialNum_, ACK_RUNNING_LOCK_DEFAULT_TIMEOUT_MS);
98 } else {
99 TELEPHONY_LOGE("ApplyRunningLock, lockType:%{public}d is invalid", lockType);
100 }
101 #endif
102 }
103
ReduceRunningLock(int32_t lockType)104 void TelRilHandler::ReduceRunningLock(int32_t lockType)
105 {
106 #ifdef ABILITY_POWER_SUPPORT
107 std::lock_guard<std::mutex> lockRequest(mutexRunningLock_);
108 TELEPHONY_LOGD("ReduceRunningLock, reqRunningLockCount_:%{public}d", static_cast<int>(reqRunningLockCount_));
109 if ((reqRunningLock_ != nullptr) && (lockType == NORMAL_RUNNING_LOCK)) {
110 if (reqRunningLockCount_ > 1) {
111 reqRunningLockCount_--;
112 } else {
113 reqRunningLockCount_ = 0;
114 TELEPHONY_LOGD("ReduceRunningLock, UnLock");
115 ReleaseRunningLockDelay(lockType);
116 }
117 } else {
118 TELEPHONY_LOGW("ReduceRunningLock type %{public}d don't processe.", lockType);
119 }
120 #endif
121 }
122
ReleaseRunningLock(int32_t lockType)123 void TelRilHandler::ReleaseRunningLock(int32_t lockType)
124 {
125 #ifdef ABILITY_POWER_SUPPORT
126 if (reqRunningLock_ == nullptr || ackRunningLock_ == nullptr) {
127 TELEPHONY_LOGE("reqRunningLock_ or ackRunningLock_ is nullptr");
128 return;
129 }
130 std::lock_guard<std::mutex> lockRequest(mutexRunningLock_);
131 TELEPHONY_LOGD("ReleaseRunningLock, lockType:%{public}d", lockType);
132 if (lockType == NORMAL_RUNNING_LOCK) {
133 reqRunningLockCount_ = 0;
134 ReleaseRunningLockDelay(lockType);
135 } else if (lockType == ACK_RUNNING_LOCK) {
136 ReleaseRunningLockDelay(lockType);
137 } else {
138 TELEPHONY_LOGE("ReleaseRunningLock, lockType:%{public}d is invalid", lockType);
139 }
140 #endif
141 }
142
ReleaseRunningLockDelay(int32_t lockType)143 void TelRilHandler::ReleaseRunningLockDelay(int32_t lockType)
144 {
145 #ifdef ABILITY_POWER_SUPPORT
146 int ret = ERR_OK;
147 if (lockType == NORMAL_RUNNING_LOCK) {
148 ret = reqRunningLock_->UnLock();
149 } else if (lockType == ACK_RUNNING_LOCK) {
150 ret = ackRunningLock_->UnLock();
151 } else {
152 TELEPHONY_LOGE("ReleaseRunningLockDelay, lockType:%{public}d is invalid", lockType);
153 }
154 if (ret != PowerMgr::E_GET_POWER_SERVICE_FAILED) {
155 return;
156 }
157
158 TELEPHONY_LOGI("ReleaseRunningLockDelay, lockType:%{public}d, no found power service", lockType);
159 if (lockType == NORMAL_RUNNING_LOCK) {
160 this->SendEvent(RUNNING_LOCK_TIMEOUT_EVENT_ID, reqLockSerialNum_, DELAR_RELEASE_RUNNING_LOCK_TIMEOUT_MS);
161 } else if (lockType == ACK_RUNNING_LOCK) {
162 this->SendEvent(ACK_RUNNING_LOCK_TIMEOUT_EVENT_ID, ackLockSerialNum_, DELAR_RELEASE_RUNNING_LOCK_TIMEOUT_MS);
163 } else {
164 // do nothing, never come in
165 TELEPHONY_LOGE("lockType is invalid");
166 }
167 #endif
168 }
169
170 } // namespace Telephony
171 } // namespace OHOS
172