1 /*
2 * Copyright (c) 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
16 #include <map>
17 #include <cinttypes>
18
19 #include "ohos.batteryStatistics.proj.hpp"
20 #include "ohos.batteryStatistics.impl.hpp"
21 #include "taihe/runtime.hpp"
22 #include "stdexcept"
23 #include "battery_stats_client.h"
24 #include "stats_log.h"
25
26 using namespace taihe;
27 using namespace ohos::batteryStatistics;
28 using namespace OHOS::PowerMgr;
29
30 namespace {
31 std::map<StatsError, std::string> g_errorTable = {
32 {StatsError::ERR_CONNECTION_FAIL, "Failed to connect to the service."},
33 {StatsError::ERR_PERMISSION_DENIED, "Permission is denied" },
34 {StatsError::ERR_SYSTEM_API_DENIED, "System permission is denied" },
35 {StatsError::ERR_PARAM_INVALID, "Invalid input parameter." }
36 };
37
38 using type_key_t = ohos::batteryStatistics::ConsumptionType::key_t;
39
TaiheConsumptionTypeWrapper(OHOS::PowerMgr::BatteryStatsInfo::ConsumptionType type)40 ohos::batteryStatistics::ConsumptionType TaiheConsumptionTypeWrapper(
41 OHOS::PowerMgr::BatteryStatsInfo::ConsumptionType type)
42 {
43 ohos::batteryStatistics::ConsumptionType taiheConsumptionType = type_key_t::CONSUMPTION_TYPE_INVALID;
44 switch (type) {
45 case OHOS::PowerMgr::BatteryStatsInfo::ConsumptionType::CONSUMPTION_TYPE_APP:
46 taiheConsumptionType = type_key_t::CONSUMPTION_TYPE_APP;
47 break;
48 case OHOS::PowerMgr::BatteryStatsInfo::ConsumptionType::CONSUMPTION_TYPE_BLUETOOTH:
49 taiheConsumptionType = type_key_t::CONSUMPTION_TYPE_BLUETOOTH;
50 break;
51 case OHOS::PowerMgr::BatteryStatsInfo::ConsumptionType::CONSUMPTION_TYPE_IDLE:
52 taiheConsumptionType = type_key_t::CONSUMPTION_TYPE_IDLE;
53 break;
54 case OHOS::PowerMgr::BatteryStatsInfo::ConsumptionType::CONSUMPTION_TYPE_PHONE:
55 taiheConsumptionType = type_key_t::CONSUMPTION_TYPE_PHONE;
56 break;
57 case OHOS::PowerMgr::BatteryStatsInfo::ConsumptionType::CONSUMPTION_TYPE_RADIO:
58 taiheConsumptionType = type_key_t::CONSUMPTION_TYPE_RADIO;
59 break;
60 case OHOS::PowerMgr::BatteryStatsInfo::ConsumptionType::CONSUMPTION_TYPE_SCREEN:
61 taiheConsumptionType = type_key_t::CONSUMPTION_TYPE_SCREEN;
62 break;
63 case OHOS::PowerMgr::BatteryStatsInfo::ConsumptionType::CONSUMPTION_TYPE_USER:
64 taiheConsumptionType = type_key_t::CONSUMPTION_TYPE_USER;
65 break;
66 case OHOS::PowerMgr::BatteryStatsInfo::ConsumptionType::CONSUMPTION_TYPE_WIFI:
67 taiheConsumptionType = type_key_t::CONSUMPTION_TYPE_WIFI;
68 break;
69 default:
70 STATS_HILOGI(COMP_FWK, "Invalid return type:%{public}d", static_cast<int32_t>(type));
71 }
72 return taiheConsumptionType;
73 }
74
GetBatteryStatsSync()75 taihe::array<ohos::batteryStatistics::BatteryStatsInfo> GetBatteryStatsSync()
76 {
77 BatteryStatsInfoList nativeStatsInfos = BatteryStatsClient::GetInstance().GetBatteryStats();
78 StatsError code = BatteryStatsClient::GetInstance().GetLastError();
79 std::vector<ohos::batteryStatistics::BatteryStatsInfo> tmpVector;
80 if (code != StatsError::ERR_OK && g_errorTable.find(code) != g_errorTable.end()) {
81 taihe::set_business_error(static_cast<int32_t>(code), g_errorTable[code]);
82 return taihe::array_view<ohos::batteryStatistics::BatteryStatsInfo>(tmpVector);
83 }
84 for (const auto& item : nativeStatsInfos) {
85 auto type = TaiheConsumptionTypeWrapper(item->GetConsumptionType());
86 if (type.get_key() == type_key_t::CONSUMPTION_TYPE_INVALID) {
87 continue;
88 }
89 ohos::batteryStatistics::BatteryStatsInfo statsInfo = {
90 .uid = item->GetUid(),
91 .type = type,
92 .power = item->GetPower()
93 };
94 tmpVector.push_back(statsInfo);
95 }
96 STATS_HILOGI(COMP_FWK, "GetBatteryStatsSync success, size %{public}zu", tmpVector.size());
97 return taihe::array_view<ohos::batteryStatistics::BatteryStatsInfo>(tmpVector);
98 }
99
GetAppPowerValue(int32_t uid)100 double GetAppPowerValue(int32_t uid)
101 {
102 double appStatsMah = BatteryStatsClient::GetInstance().GetAppStatsMah(uid);
103 StatsError code = BatteryStatsClient::GetInstance().GetLastError();
104 STATS_HILOGI(COMP_FWK, "get app stats mah: %{public}lf for uid: %{public}d", appStatsMah, uid);
105 if (code != StatsError::ERR_OK && g_errorTable.find(code) != g_errorTable.end()) {
106 taihe::set_business_error(static_cast<int32_t>(code), g_errorTable[code]);
107 }
108 return appStatsMah;
109 }
110
GetAppPowerPercent(int32_t uid)111 double GetAppPowerPercent(int32_t uid)
112 {
113 double appStatsPercent = BatteryStatsClient::GetInstance().GetAppStatsPercent(uid);
114 StatsError code = BatteryStatsClient::GetInstance().GetLastError();
115 STATS_HILOGI(COMP_FWK, "get app stats percent: %{public}lf for uid: %{public}d", appStatsPercent, uid);
116 if (code != StatsError::ERR_OK && g_errorTable.find(code) != g_errorTable.end()) {
117 taihe::set_business_error(static_cast<int32_t>(code), g_errorTable[code]);
118 }
119 return appStatsPercent;
120 }
121
GetHardwareUnitPowerValue(ohos::batteryStatistics::ConsumptionType type)122 double GetHardwareUnitPowerValue(ohos::batteryStatistics::ConsumptionType type)
123 {
124 OHOS::PowerMgr::BatteryStatsInfo::ConsumptionType nativeType =
125 OHOS::PowerMgr::BatteryStatsInfo::ConsumptionType(static_cast<int32_t>(type));
126 double partStatsMah = BatteryStatsClient::GetInstance().GetPartStatsMah(nativeType);
127 StatsError code = BatteryStatsClient::GetInstance().GetLastError();
128 STATS_HILOGI(COMP_FWK, "get part stats mah: %{public}lf for type: %{public}d", partStatsMah, nativeType);
129 if (code != StatsError::ERR_OK && g_errorTable.find(code) != g_errorTable.end()) {
130 taihe::set_business_error(static_cast<int32_t>(code), g_errorTable[code]);
131 }
132 return partStatsMah;
133 }
134
GetHardwareUnitPowerPercent(ohos::batteryStatistics::ConsumptionType type)135 double GetHardwareUnitPowerPercent(ohos::batteryStatistics::ConsumptionType type)
136 {
137 OHOS::PowerMgr::BatteryStatsInfo::ConsumptionType nativeType =
138 OHOS::PowerMgr::BatteryStatsInfo::ConsumptionType(static_cast<int32_t>(type));
139 double partStatsPercent = BatteryStatsClient::GetInstance().GetPartStatsPercent(nativeType);
140 StatsError code = BatteryStatsClient::GetInstance().GetLastError();
141 STATS_HILOGD(COMP_FWK, "get part stats percent: %{public}lf for type: %{public}d", partStatsPercent, nativeType);
142 if (code != StatsError::ERR_OK && g_errorTable.find(code) != g_errorTable.end()) {
143 taihe::set_business_error(static_cast<int32_t>(code), g_errorTable[code]);
144 }
145 return partStatsPercent;
146 }
147 } // namespace
148
149 // Since these macros are auto-generate, lint will cause false positive
150 // NOLINTBEGIN
151 TH_EXPORT_CPP_API_GetBatteryStatsSync(GetBatteryStatsSync);
152 TH_EXPORT_CPP_API_GetAppPowerValue(GetAppPowerValue);
153 TH_EXPORT_CPP_API_GetAppPowerPercent(GetAppPowerPercent);
154 TH_EXPORT_CPP_API_GetHardwareUnitPowerValue(GetHardwareUnitPowerValue);
155 TH_EXPORT_CPP_API_GetHardwareUnitPowerPercent(GetHardwareUnitPowerPercent);
156 // NOLINTEND