1 /*
2 * Copyright (c) 2024-2024 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 "charge_delay_state_collection.h"
17
18 #ifdef BATTERY_MANAGER_ENABLE
19 #include "battery_info.h"
20 #include "battery_srv_client.h"
21 #endif
22 #include "charger_state_collection.h"
23 #include "common_event_subscriber.h"
24 #include "common_event_data.h"
25 #include "common_event_manager.h"
26 #include "common_event_support.h"
27 #include "file_operation.h"
28 #include "securec.h"
29 #include "string_ex.h"
30 #include "string_operation.h"
31 #include "thermal_service.h"
32 #include "thermal_common.h"
33
34 namespace OHOS {
35 namespace PowerMgr {
Init()36 bool ChargeDelayStateCollection::Init()
37 {
38 THERMAL_HILOGD(COMP_SVC, "ChargeDelayStateCollection Init...");
39 return this->RegisterEvent();
40 }
41
InitParam(std::string & params)42 bool ChargeDelayStateCollection::InitParam(std::string& params)
43 {
44 THERMAL_HILOGD(COMP_SVC, "Enter");
45 params_ = params;
46 THERMAL_HILOGD(COMP_SVC, "init charge delay time info");
47 delayTime_ = static_cast<uint32_t>(strtol(params_.c_str(), nullptr, STRTOL_FORMART_DEC));
48 return true;
49 }
50
GetState()51 std::string ChargeDelayStateCollection::GetState()
52 {
53 std::lock_guard<std::mutex> lock(mutex_);
54 return ToString(criticalState_);
55 }
56
RegisterEvent()57 bool ChargeDelayStateCollection::RegisterEvent()
58 {
59 auto tms = ThermalService::GetInstance();
60 if (tms == nullptr) {
61 return false;
62 }
63 auto receiver = tms->GetStateMachineObj()->GetCommonEventReceiver();
64 if (receiver == nullptr) {
65 return false;
66 }
67
68 #ifdef BATTERY_MANAGER_ENABLE
69 EventHandle batteryPowerConnectedHandler =
70 [this](const EventFwk::CommonEventData& data) { this->HandlerPowerConnected(data); };
71 receiver->AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_POWER_CONNECTED, batteryPowerConnectedHandler);
72
73 EventHandle batteryPowerDisconnectedHandler =
74 [this](const EventFwk::CommonEventData& data) { this->HandlerPowerDisconnected(data); };
75 receiver->AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_POWER_DISCONNECTED, batteryPowerDisconnectedHandler);
76 #endif
77 return true;
78 }
79
80 #ifdef BATTERY_MANAGER_ENABLE
HandlerPowerDisconnected(const EventFwk::CommonEventData & data)81 void ChargeDelayStateCollection::HandlerPowerDisconnected(const EventFwk::CommonEventData& data)
82 {
83 std::lock_guard<std::mutex> lock(mutex_);
84 auto csc = ChargerStateCollection::GetInstance();
85 if (csc == nullptr) {
86 THERMAL_HILOGE(COMP_SVC, "ChargerStateCollection GetInstance failed");
87 return;
88 }
89 if (!csc->GetCharge()) {
90 return;
91 }
92 csc->SetCharge(false);
93 criticalState_ = CRITICAL_STATE;
94 THERMAL_HILOGI(COMP_SVC, "ChargeDelayStateCollection HandlerPowerDisconnected");
95 StopDelayTimer();
96 StartDelayTimer();
97 }
98
HandlerPowerConnected(const EventFwk::CommonEventData & data)99 void ChargeDelayStateCollection::HandlerPowerConnected(const EventFwk::CommonEventData& data)
100 {
101 std::lock_guard<std::mutex> lock(mutex_);
102 auto csc = ChargerStateCollection::GetInstance();
103 if (csc == nullptr) {
104 THERMAL_HILOGE(COMP_SVC, "ChargerStateCollection GetInstance failed");
105 return;
106 }
107 if (csc->GetCharge()) {
108 return;
109 }
110 if (delayTimerId_ > 0) {
111 StopDelayTimer();
112 }
113 csc->SetCharge(true);
114 criticalState_ = NON_CRITICAL_STATE;
115 THERMAL_HILOGI(COMP_SVC, "ChargeDelayStateCollection HandlerPowerConnected");
116 }
117 #endif
118
StartDelayTimer()119 bool ChargeDelayStateCollection::StartDelayTimer()
120 {
121 auto thermalTimer = std::make_shared<ThermalTimer>();
122 auto timerInfo = std::make_shared<ThermalTimerInfo>();
123 timerInfo->SetType(ThermalTimer::TIMER_TYPE_WAKEUP | ThermalTimer::TIMER_TYPE_EXACT);
124 timerInfo->SetCallbackInfo([this] { ResetState(); });
125
126 delayTimerId_ = thermalTimer->CreateTimer(timerInfo);
127 int64_t curMsecTimestam = MiscServices::TimeServiceClient::GetInstance()->GetWallTimeMs();
128
129 return thermalTimer->StartTimer(delayTimerId_, static_cast<uint64_t>(delayTime_ + curMsecTimestam));
130 }
131
StopDelayTimer()132 void ChargeDelayStateCollection::StopDelayTimer()
133 {
134 if (delayTimerId_ > 0) {
135 auto thermalTimer = std::make_shared<ThermalTimer>();
136 if (!thermalTimer->StopTimer(delayTimerId_)) {
137 THERMAL_HILOGE(COMP_SVC, "ChargeDelayStateCollection failed to stop delay timer");
138 }
139 thermalTimer->DestroyTimer(delayTimerId_);
140 delayTimerId_ = 0;
141 }
142 }
143
SetState(const std::string & stateValue)144 void ChargeDelayStateCollection::SetState(const std::string& stateValue)
145 {
146 }
147
ResetState()148 void ChargeDelayStateCollection::ResetState()
149 {
150 THERMAL_HILOGI(COMP_SVC, "ChargeDelayStateCollection ResetState");
151 std::lock_guard<std::mutex> lock(mutex_);
152 criticalState_ = NON_CRITICAL_STATE;
153 delayTimerId_ = 0;
154 }
155
DecideState(const std::string & value)156 bool ChargeDelayStateCollection::DecideState(const std::string& value)
157 {
158 #ifdef BATTERY_MANAGER_ENABLE
159 THERMAL_HILOGD(COMP_SVC, "Enter: Consider the influence of critical state of charge, %{public}d", criticalState_);
160 std::lock_guard<std::mutex> lock(mutex_);
161 if ((value == ToString(NON_CRITICAL_STATE) && criticalState_ == NON_CRITICAL_STATE) ||
162 (value == ToString(CRITICAL_STATE) && criticalState_ == CRITICAL_STATE)) {
163 THERMAL_HILOGD(COMP_SVC, "current critical state = %{public}d", criticalState_);
164 return true;
165 }
166 return false;
167 #endif
168
169 return true;
170 }
171 } // namespace PowerMgr
172 } // namespace OHOS
173