1 /*
2  * Copyright (c) 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 #include "conditions/condition_checker.h"
16 #ifdef COMMUNICATION_NETMANAGER_BASE_ENABLE
17 #include "net_conn_client.h"
18 #endif
19 #ifdef POWERMGR_BATTERY_MANAGER_ENABLE
20 #include "battery_srv_client.h"
21 #include "battery_info.h"
22 #endif
23 #include "work_sched_hilog.h"
24 
25 using namespace OHOS::NetManagerStandard;
26 
27 namespace OHOS {
28 namespace WorkScheduler {
ConditionChecker(const std::shared_ptr<WorkQueueManager> workQueueManager)29 ConditionChecker::ConditionChecker(const std::shared_ptr<WorkQueueManager> workQueueManager)
30 {
31     workQueueManager_ = workQueueManager;
32 }
33 
CheckAllStatus()34 void ConditionChecker::CheckAllStatus()
35 {
36     CheckNetworkStatus();
37     CheckChargerStatus();
38     CheckBatteryStatus();
39 }
40 
41 
CheckNetworkStatus()42 void ConditionChecker::CheckNetworkStatus()
43 {
44 #ifdef COMMUNICATION_NETMANAGER_BASE_ENABLE
45     WS_HILOGD("enter");
46     NetHandle netHandle;
47     int ret = NetConnClient::GetInstance().GetDefaultNet(netHandle);
48     if (ret != NETMANAGER_SUCCESS) {
49         WS_HILOGE("GetDefaultNet failed %{public}d", ret);
50         return;
51     }
52     NetAllCapabilities netAllCap;
53     ret = NetConnClient::GetInstance().GetNetCapabilities(netHandle, netAllCap);
54     if (ret != NETMANAGER_SUCCESS) {
55         WS_HILOGE("GetNetCapbilities failed, ret = %{public}d", ret);
56         return;
57     }
58     if (netAllCap.netCaps_.count(NetCap::NET_CAPABILITY_INTERNET)) {
59         if (netAllCap.bearerTypes_.count(BEARER_ETHERNET)) {
60             WS_HILOGI("BEARER_ETHERNET");
61             workQueueManager_->OnConditionChanged(WorkCondition::Type::NETWORK,
62                 std::make_shared<DetectorValue>(WorkCondition::NETWORK_TYPE_ETHERNET, 0, 0, std::string()));
63         } else if (netAllCap.bearerTypes_.count(BEARER_WIFI)) {
64             WS_HILOGI("BEARER_WIFI");
65             workQueueManager_->OnConditionChanged(WorkCondition::Type::NETWORK,
66                 std::make_shared<DetectorValue>(WorkCondition::NETWORK_TYPE_WIFI, 0, 0, std::string()));
67         } else if (netAllCap.bearerTypes_.count(BEARER_CELLULAR)) {
68             WS_HILOGI("BEARER_CELLULAR");
69             workQueueManager_->OnConditionChanged(WorkCondition::Type::NETWORK,
70                 std::make_shared<DetectorValue>(WorkCondition::NETWORK_TYPE_MOBILE, 0, 0, std::string()));
71         }
72     } else {
73         WS_HILOGI("no network");
74     }
75 #endif
76 }
77 
CheckChargerStatus()78 void ConditionChecker::CheckChargerStatus()
79 {
80 #ifdef POWERMGR_BATTERY_MANAGER_ENABLE
81     WS_HILOGD("enter");
82     auto type = PowerMgr::BatterySrvClient::GetInstance().GetPluggedType();
83     switch (type) {
84         case PowerMgr::BatteryPluggedType::PLUGGED_TYPE_AC:
85             WS_HILOGI("CHARGER_PLUGGED_AC");
86             workQueueManager_->OnConditionChanged(WorkCondition::Type::CHARGER,
87                 std::make_shared<DetectorValue>(WorkCondition::CHARGING_PLUGGED_AC,
88                 0, true, std::string()));
89             break;
90         case PowerMgr::BatteryPluggedType::PLUGGED_TYPE_USB:
91             WS_HILOGI("CHARGER_PLUGGED_USB");
92             workQueueManager_->OnConditionChanged(WorkCondition::Type::CHARGER,
93                 std::make_shared<DetectorValue>(WorkCondition::CHARGING_PLUGGED_USB,
94                 0, true, std::string()));
95             break;
96         case PowerMgr::BatteryPluggedType::PLUGGED_TYPE_WIRELESS:
97             WS_HILOGI("CHARGER_WIRELESS");
98             workQueueManager_->OnConditionChanged(WorkCondition::Type::CHARGER,
99                 std::make_shared<DetectorValue>(WorkCondition::CHARGING_PLUGGED_WIRELESS,
100                 0, true, std::string()));
101             break;
102 
103         case PowerMgr::BatteryPluggedType::PLUGGED_TYPE_NONE:
104         case PowerMgr::BatteryPluggedType::PLUGGED_TYPE_BUTT:
105             WS_HILOGI("CHARGER_PLUGGED_UNPLUGGED");
106             workQueueManager_->OnConditionChanged(WorkCondition::Type::CHARGER,
107                 std::make_shared<DetectorValue>(WorkCondition::CHARGING_UNPLUGGED, 0, false, std::string()));
108             break;
109         default:
110             break;
111     }
112 #endif
113 }
114 
CheckBatteryStatus()115 void ConditionChecker::CheckBatteryStatus()
116 {
117 #ifdef POWERMGR_BATTERY_MANAGER_ENABLE
118     WS_HILOGD("enter");
119     int32_t defaultCapacity = -1;
120     int32_t batteryCapacityLow = 20;
121     auto capacity = PowerMgr::BatterySrvClient::GetInstance().GetCapacity();
122     WS_HILOGD("capacity = %{public}d", capacity);
123     if (capacity == defaultCapacity) {
124         return;
125     } else if (capacity < batteryCapacityLow) {
126         WS_HILOGI("BATTERY_STATUS_LOW");
127         workQueueManager_->OnConditionChanged(WorkCondition::Type::BATTERY_STATUS,
128             std::make_shared<DetectorValue>(WorkCondition::BATTERY_STATUS_LOW, 0, 0, std::string()));
129     } else {
130         WS_HILOGI("BATTERY_STATUS_OKAY");
131         workQueueManager_->OnConditionChanged(WorkCondition::Type::BATTERY_STATUS,
132             std::make_shared<DetectorValue>(WorkCondition::BATTERY_STATUS_OKAY, 0, 0, std::string()));
133     }
134 #endif
135 }
136 } // namespace WorkScheduler
137 } // namespace OHOS