1 /*
2 * Copyright (c) 2024-2025 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 #include "policy/power_mode_policy.h"
16
17 #ifdef POWERMGR_BATTERY_MANAGER_ENABLE
18 #include "battery_srv_client.h"
19 #endif
20 #include "power_mgr_client.h"
21 #include "power_mode_info.h"
22 #include "work_sched_hilog.h"
23
24 using namespace std;
25 using namespace OHOS::PowerMgr;
26
27 namespace OHOS {
28 namespace WorkScheduler {
29 #ifdef POWERMGR_BATTERY_MANAGER_ENABLE
30 const int32_t COUNT_POWER_MODE_CRUCIAL = 1;
31 #endif
32 const int32_t COUNT_POWER_MODE_NORMAL = 3;
33
PowerModePolicy(shared_ptr<WorkPolicyManager> workPolicyManager)34 PowerModePolicy::PowerModePolicy(shared_ptr<WorkPolicyManager> workPolicyManager)
35 {
36 workPolicyManager_ = workPolicyManager;
37 }
38
~PowerModePolicy()39 PowerModePolicy::~PowerModePolicy()
40 {
41 }
42
GetPolicyMaxRunning(WorkSchedSystemPolicy & systemPolicy)43 int32_t PowerModePolicy::GetPolicyMaxRunning(WorkSchedSystemPolicy& systemPolicy)
44 {
45 int32_t res = COUNT_POWER_MODE_NORMAL;
46 auto mode = PowerMgrClient::GetInstance().GetDeviceMode();
47 if (mode == PowerMode::NORMAL_MODE || mode == PowerMode::PERFORMANCE_MODE) {
48 WS_HILOGD("power mode: %{public}d, PolicyRes: %{public}d", mode, res);
49 systemPolicy.powerMode = static_cast<uint32_t>(mode);
50 systemPolicy.SetPolicyName("POWER_MODE_POLICY", res);
51 return res;
52 }
53 #ifdef POWERMGR_BATTERY_MANAGER_ENABLE
54 auto charge = BatterySrvClient::GetInstance().GetChargingStatus();
55 if (charge == BatteryChargeState::CHARGE_STATE_NONE || charge == BatteryChargeState::CHARGE_STATE_DISABLE) {
56 res = COUNT_POWER_MODE_CRUCIAL;
57 WS_HILOGI("charge: %{public}d, power mode: %{public}d, PolicyRes: %{public}d", charge, mode, res);
58 }
59 #endif
60 WS_HILOGD("power mode: %{public}d, PolicyRes: %{public}d", mode, res);
61 systemPolicy.powerMode = static_cast<uint32_t>(mode);
62 systemPolicy.SetPolicyName("POWER_MODE_POLICY", res);
63 return res;
64 }
65 } // namespace WorkScheduler
66 } // namespace OHOS