1 /*
2 * Copyright (c) 2021-2022 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 "charger_state_collection.h"
17
18 #include "battery_info.h"
19 #include "battery_srv_client.h"
20 #include "common_event_subscriber.h"
21 #include "common_event_data.h"
22 #include "common_event_manager.h"
23 #include "common_event_support.h"
24 #include "file_operation.h"
25 #include "securec.h"
26 #include "string_ex.h"
27 #include "string_operation.h"
28 #include "thermal_service.h"
29 #include "thermal_common.h"
30
31 using namespace OHOS::EventFwk;
32 namespace OHOS {
33 namespace PowerMgr {
34 namespace {
35 auto g_service = DelayedSpSingleton<ThermalService>::GetInstance();
36 IdleState g_idleStateConfig {};
37 IdleState g_cachedIdleState {};
38 bool g_isChargeIdle = true;
39 const std::string CHARGER_ON = "1";
40 const std::string CHARGER_OFF = "0";
41 }
Init()42 bool ChargerStateCollection::Init()
43 {
44 g_idleStateConfig = g_service->GetStateMachineObj()->GetIdleStateConfig();
45 if (!RegisterEvent()) {
46 return false;
47 }
48 return true;
49 }
50
InitParam(std::string & params)51 bool ChargerStateCollection::InitParam(std::string& params)
52 {
53 THERMAL_HILOGD(COMP_SVC, "Enter");
54 params_ = params;
55 return true;
56 }
57
GetState()58 std::string ChargerStateCollection::GetState()
59 {
60 THERMAL_HILOGD(COMP_SVC, "charger state = %{public}s", mockState_.c_str());
61 if (!g_service->GetFlag()) {
62 return mockState_;
63 } else {
64 return state_;
65 }
66 }
67
RegisterEvent()68 bool ChargerStateCollection::RegisterEvent()
69 {
70 if (g_service == nullptr) {
71 return false;
72 }
73
74 auto receiver = g_service->GetStateMachineObj()->GetCommonEventReceiver();
75 if (receiver == nullptr) {
76 return false;
77 }
78 bool ret;
79 THERMAL_HILOGI(COMP_SVC, "start register battery change event");
80 EventHandle batteryChangedHandler =
81 std::bind(&ChargerStateCollection::HandleChangerStatusCompleted, this, std::placeholders::_1);
82 ret = receiver->Start(CommonEventSupport::COMMON_EVENT_BATTERY_CHANGED, batteryChangedHandler);
83 if (!ret) {
84 THERMAL_HILOGE(COMP_SVC, "fail to COMMON_EVENT_BATTERY_CHANGED");
85 return false;
86 }
87 THERMAL_HILOGI(COMP_SVC, "start register thermal level change event");
88 EventHandle thermalLevelHandler =
89 std::bind(&ChargerStateCollection::HandleThermalLevelCompleted, this, std::placeholders::_1);
90 ret = receiver->Start(CommonEventSupport::COMMON_EVENT_THERMAL_LEVEL_CHANGED, thermalLevelHandler);
91 if (!ret) {
92 THERMAL_HILOGE(COMP_SVC, "fail to COMMON_EVENT_THERMAL_LEVEL_CHANGED");
93 return false;
94 }
95 return true;
96 }
97
HandleChangerStatusCompleted(const CommonEventData & data)98 void ChargerStateCollection::HandleChangerStatusCompleted(const CommonEventData& data)
99 {
100 g_cachedIdleState.soc = data.GetWant().GetIntParam(BatteryInfo::COMMON_EVENT_KEY_CAPACITY, -1);
101 g_cachedIdleState.current = data.GetWant().GetIntParam(BatteryInfo::COMMON_EVENT_KEY_PLUGGED_NOW_CURRENT, -1);
102 g_cachedIdleState.charging = data.GetWant().GetIntParam(BatteryInfo::COMMON_EVENT_KEY_CHARGE_STATE, -1);
103 HandleChargeIdleState();
104
105 switch (g_cachedIdleState.charging) {
106 case static_cast<int32_t>(BatteryChargeState::CHARGE_STATE_DISABLE): {
107 state_ = ToString(DISABLE);
108 break;
109 }
110 case static_cast<int32_t>(BatteryChargeState::CHARGE_STATE_ENABLE): {
111 state_ = ToString(ENABLE);
112 break;
113 }
114 case static_cast<int32_t>(BatteryChargeState::CHARGE_STATE_FULL): {
115 state_ = ToString(FULL);
116 break;
117 }
118 case static_cast<int32_t>(BatteryChargeState::CHARGE_STATE_NONE): {
119 state_ = ToString(NONE);
120 break;
121 }
122 case static_cast<int>(BatteryChargeState::CHARGE_STATE_BUTT): {
123 state_ = ToString(BUTT);
124 break;
125 }
126 default:
127 break;
128 }
129 }
130
SetState()131 void ChargerStateCollection::SetState()
132 {
133 }
134
DecideState(const std::string & value)135 bool ChargerStateCollection::DecideState(const std::string& value)
136 {
137 THERMAL_HILOGD(COMP_SVC, "Enter");
138 auto& batterySrvClient = BatterySrvClient::GetInstance();
139 BatteryChargeState chargeState = batterySrvClient.GetChargingStatus();
140 if ((value == CHARGER_ON && chargeState == BatteryChargeState::CHARGE_STATE_ENABLE) ||
141 (value == CHARGER_OFF && chargeState == BatteryChargeState::CHARGE_STATE_NONE)) {
142 return true;
143 }
144 return false;
145 }
146
HandleChargeIdleState()147 void ChargerStateCollection::HandleChargeIdleState()
148 {
149 THERMAL_HILOGI(COMP_SVC, "soc=%{public}d, charging==%{public}d, current==%{public}d, level==%{public}d",
150 g_cachedIdleState.soc, g_cachedIdleState.charging, g_cachedIdleState.current,
151 g_cachedIdleState.level);
152 bool isIdle = ((g_cachedIdleState.soc >= g_idleStateConfig.soc) &&
153 (g_cachedIdleState.charging == g_idleStateConfig.charging) &&
154 (g_cachedIdleState.current >= g_idleStateConfig.current) &&
155 (g_cachedIdleState.level <= g_idleStateConfig.level));
156 if (isIdle != g_isChargeIdle) {
157 THERMAL_HILOGI(COMP_SVC, "idle state changed, start to broadcast event");
158 PublishIdleEvent(isIdle, CommonEventSupport::COMMON_EVENT_CHARGE_IDLE);
159 PublishIdleEvent(isIdle, CommonEventSupport::COMMON_EVENT_DEVICE_IDLE_MODE_CHANGED);
160 g_isChargeIdle = isIdle;
161 }
162 }
163
PublishIdleEvent(bool isIdle,const std::string commonEventSupport)164 void ChargerStateCollection::PublishIdleEvent(bool isIdle, const std::string commonEventSupport)
165 {
166 Want want;
167 auto code = static_cast<uint32_t>(ChargeIdleEventCode::EVENT_CODE_CHARGE_IDLE_STATE);
168 want.SetParam(ToString(code), isIdle);
169 want.SetAction(commonEventSupport);
170 CommonEventData commonData;
171 commonData.SetWant(want);
172 CommonEventPublishInfo publishInfo;
173 publishInfo.SetOrdered(false);
174 if (!CommonEventManager::PublishCommonEvent(commonData, publishInfo)) {
175 THERMAL_HILOGW(COMP_SVC, "failed to publish charge idle event");
176 }
177 }
178
HandleThermalLevelCompleted(const CommonEventData & data)179 void ChargerStateCollection::HandleThermalLevelCompleted(const CommonEventData& data)
180 {
181 std::string key = ToString(static_cast<int32_t>(ThermalCommonEventCode::CODE_THERMAL_LEVEL_CHANGED));
182 int32_t level = data.GetWant().GetIntParam(key, -1);
183 g_cachedIdleState.level =level;
184 HandleChargeIdleState();
185 }
186 } // namespace PowerMgr
187 } // namespace OHOS
188