• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "battery_stats_info.h"
17 
18 #include "stats_common.h"
19 #include "stats_log.h"
20 
21 namespace OHOS {
22 namespace PowerMgr {
Marshalling(Parcel & parcel) const23 bool BatteryStatsInfo::Marshalling(Parcel& parcel) const
24 {
25     STATS_WRITE_PARCEL_WITH_RET(COMP_FWK, parcel, Int32, uid_, false);
26     STATS_WRITE_PARCEL_WITH_RET(COMP_FWK, parcel, Int32, static_cast<int32_t>(type_), false);
27     STATS_WRITE_PARCEL_WITH_RET(COMP_FWK, parcel, Double, totalPowerMah_, false);
28     STATS_HILOGD(COMP_FWK, "uid: %{public}d, type: %{public}d, power: %{public}lf", uid_, type_, totalPowerMah_);
29     return true;
30 }
31 
Unmarshalling(Parcel & parcel)32 std::shared_ptr<BatteryStatsInfo> BatteryStatsInfo::Unmarshalling(Parcel& parcel)
33 {
34     std::shared_ptr<BatteryStatsInfo> statsInfo = std::make_shared<BatteryStatsInfo>();
35     if (statsInfo == nullptr) {
36         STATS_HILOGW(COMP_FWK, "BatteryStatsInfo object creation failed");
37         return statsInfo;
38     }
39     if (!statsInfo->ReadFromParcel(parcel)) {
40         STATS_HILOGW(COMP_FWK, "Read from parcel failed");
41         statsInfo = nullptr;
42     }
43     return statsInfo;
44 }
45 
ReadFromParcel(Parcel & parcel)46 bool BatteryStatsInfo::ReadFromParcel(Parcel &parcel)
47 {
48     STATS_READ_PARCEL_WITH_RET(COMP_FWK, parcel, Int32, uid_, false);
49     int32_t type = static_cast<int32_t>(0);
50     STATS_READ_PARCEL_WITH_RET(COMP_FWK, parcel, Int32, type, false);
51     type_ = static_cast<ConsumptionType>(type);
52     STATS_READ_PARCEL_WITH_RET(COMP_FWK, parcel, Double, totalPowerMah_, false);
53     STATS_HILOGD(COMP_FWK, "uid: %{public}d, type: %{public}d, power: %{public}lf", uid_, type_, totalPowerMah_);
54     return true;
55 }
56 
SetUid(int32_t uid)57 void BatteryStatsInfo::SetUid(int32_t uid)
58 {
59     if (uid > StatsUtils::INVALID_VALUE) {
60         uid_ = uid;
61     }
62     STATS_HILOGD(COMP_FWK, "Uid: %{public}d", uid);
63 }
64 
SetUserId(int32_t userId)65 void BatteryStatsInfo::SetUserId(int32_t userId)
66 {
67     if (userId > StatsUtils::INVALID_VALUE) {
68         userId_ = userId;
69     }
70     STATS_HILOGD(COMP_FWK, "UserId: %{public}d", userId);
71 }
72 
SetConsumptioType(ConsumptionType type)73 void BatteryStatsInfo::SetConsumptioType(ConsumptionType type)
74 {
75     type_ = type;
76 }
77 
SetPower(double power)78 void BatteryStatsInfo::SetPower(double power)
79 {
80     if (uid_ > StatsUtils::INVALID_VALUE) {
81         STATS_HILOGD(COMP_FWK, "Set APP power: %{public}lfmAh for uid: %{public}d", totalPowerMah_, uid_);
82     } else {
83         STATS_HILOGD(COMP_FWK, "Set power: %{public}lfmAh for part: %{public}s", totalPowerMah_,
84             ConvertConsumptionType(type_).c_str());
85     }
86     totalPowerMah_ = power;
87 }
88 
GetUid()89 int32_t BatteryStatsInfo::GetUid()
90 {
91     return uid_;
92 }
93 
GetUserId()94 int32_t BatteryStatsInfo::GetUserId()
95 {
96     return userId_;
97 }
98 
GetConsumptionType()99 BatteryStatsInfo::ConsumptionType BatteryStatsInfo::GetConsumptionType()
100 {
101     return type_;
102 }
103 
GetPower()104 double BatteryStatsInfo::GetPower()
105 {
106     if (uid_ > StatsUtils::INVALID_VALUE) {
107         STATS_HILOGD(COMP_FWK, "Get app power: %{public}lfmAh for uid: %{public}d", totalPowerMah_, uid_);
108     } else {
109         STATS_HILOGD(COMP_FWK, "Get power: %{public}lfmAh for part: %{public}s", totalPowerMah_,
110             ConvertConsumptionType(type_).c_str());
111     }
112     return totalPowerMah_;
113 }
114 
ConvertTypeForPart(ConsumptionType type)115 std::string BatteryStatsInfo::ConvertTypeForPart(ConsumptionType type)
116 {
117     std::string result = "";
118     switch (type) {
119         case CONSUMPTION_TYPE_BLUETOOTH:
120             result = GET_VARIABLE_NAME(CONSUMPTION_TYPE_BLUETOOTH);
121             break;
122         case CONSUMPTION_TYPE_IDLE:
123             result = GET_VARIABLE_NAME(CONSUMPTION_TYPE_IDLE);
124             break;
125         case CONSUMPTION_TYPE_PHONE:
126             result = GET_VARIABLE_NAME(CONSUMPTION_TYPE_PHONE);
127             break;
128         case CONSUMPTION_TYPE_SCREEN:
129             result = GET_VARIABLE_NAME(CONSUMPTION_TYPE_SCREEN);
130             break;
131         case CONSUMPTION_TYPE_WIFI:
132             result = GET_VARIABLE_NAME(CONSUMPTION_TYPE_WIFI);
133             break;
134         default:
135             break;
136     }
137     STATS_HILOGD(COMP_FWK, "Convert to %{public}s", result.c_str());
138     return result;
139 }
140 
ConvertTypeForApp(ConsumptionType type)141 std::string BatteryStatsInfo::ConvertTypeForApp(ConsumptionType type)
142 {
143     std::string result = "";
144     switch (type) {
145         case CONSUMPTION_TYPE_USER:
146             result = GET_VARIABLE_NAME(CONSUMPTION_TYPE_USER);
147             break;
148         case CONSUMPTION_TYPE_APP:
149             result = GET_VARIABLE_NAME(CONSUMPTION_TYPE_APP);
150             break;
151         case CONSUMPTION_TYPE_CAMERA:
152             result = GET_VARIABLE_NAME(CONSUMPTION_TYPE_CAMERA);
153             break;
154         case CONSUMPTION_TYPE_FLASHLIGHT:
155             result = GET_VARIABLE_NAME(CONSUMPTION_TYPE_FLASHLIGHT);
156             break;
157         case CONSUMPTION_TYPE_AUDIO:
158             result = GET_VARIABLE_NAME(CONSUMPTION_TYPE_AUDIO);
159             break;
160         case CONSUMPTION_TYPE_SENSOR:
161             result = GET_VARIABLE_NAME(CONSUMPTION_TYPE_SENSOR);
162             break;
163         case CONSUMPTION_TYPE_GNSS:
164             result = GET_VARIABLE_NAME(CONSUMPTION_TYPE_GNSS);
165             break;
166         case CONSUMPTION_TYPE_CPU:
167             result = GET_VARIABLE_NAME(CONSUMPTION_TYPE_CPU);
168             break;
169         case CONSUMPTION_TYPE_WAKELOCK:
170             result = GET_VARIABLE_NAME(CONSUMPTION_TYPE_WAKELOCK);
171             break;
172         case CONSUMPTION_TYPE_ALARM:
173             result = GET_VARIABLE_NAME(CONSUMPTION_TYPE_ALARM);
174             break;
175         default:
176             break;
177     }
178     STATS_HILOGD(COMP_FWK, "Convert to %{public}s", result.c_str());
179     return result;
180 }
181 
ConvertConsumptionType(ConsumptionType type)182 std::string BatteryStatsInfo::ConvertConsumptionType(ConsumptionType type)
183 {
184     std::string result = "";
185     switch (type) {
186         case CONSUMPTION_TYPE_BLUETOOTH:
187         case CONSUMPTION_TYPE_IDLE:
188         case CONSUMPTION_TYPE_PHONE:
189         case CONSUMPTION_TYPE_SCREEN:
190         case CONSUMPTION_TYPE_WIFI:
191             result = ConvertTypeForPart(type);
192             break;
193         case CONSUMPTION_TYPE_USER:
194         case CONSUMPTION_TYPE_APP:
195         case CONSUMPTION_TYPE_CAMERA:
196         case CONSUMPTION_TYPE_FLASHLIGHT:
197         case CONSUMPTION_TYPE_AUDIO:
198         case CONSUMPTION_TYPE_SENSOR:
199         case CONSUMPTION_TYPE_GNSS:
200         case CONSUMPTION_TYPE_CPU:
201         case CONSUMPTION_TYPE_WAKELOCK:
202         case CONSUMPTION_TYPE_ALARM:
203             result = ConvertTypeForApp(type);
204             break;
205         default:
206             STATS_HILOGD(COMP_FWK, "Conversion failed because the type was illegal, return empty string");
207             break;
208     }
209     return result;
210 }
211 } // namespace PowerMgr
212 } // namespace OHOS
213