• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #define DEBUG false  // STOPSHIP if true
18 #include "Log.h"
19 
20 #include <android/hardware/health/2.0/IHealth.h>
21 #include <healthhalutils/HealthHalUtils.h>
22 #include "external/ResourceHealthManagerPuller.h"
23 #include "external/StatsPuller.h"
24 
25 #include "ResourceHealthManagerPuller.h"
26 #include "logd/LogEvent.h"
27 #include "stats_log_util.h"
28 #include "statslog.h"
29 
30 using android::hardware::hidl_vec;
31 using android::hardware::Return;
32 using android::hardware::Void;
33 using android::hardware::health::V2_0::get_health_service;
34 using android::hardware::health::V2_0::HealthInfo;
35 using android::hardware::health::V2_0::IHealth;
36 using android::hardware::health::V2_0::Result;
37 
38 using std::make_shared;
39 using std::shared_ptr;
40 
41 namespace android {
42 namespace os {
43 namespace statsd {
44 
45 sp<android::hardware::health::V2_0::IHealth> gHealthHal = nullptr;
46 
getHealthHal()47 bool getHealthHal() {
48     if (gHealthHal == nullptr) {
49         gHealthHal = get_health_service();
50     }
51     return gHealthHal != nullptr;
52 }
53 
ResourceHealthManagerPuller(int tagId)54 ResourceHealthManagerPuller::ResourceHealthManagerPuller(int tagId) : StatsPuller(tagId) {
55 }
56 
57 // TODO(b/110565992): add other health atoms (eg. Temperature).
PullInternal(vector<shared_ptr<LogEvent>> * data)58 bool ResourceHealthManagerPuller::PullInternal(vector<shared_ptr<LogEvent>>* data) {
59     if (!getHealthHal()) {
60         ALOGE("Health Hal not loaded");
61         return false;
62     }
63 
64     int64_t wallClockTimestampNs = getWallClockNs();
65     int64_t elapsedTimestampNs = getElapsedRealtimeNs();
66 
67     data->clear();
68     bool result_success = true;
69 
70     // Get the data from the Health HAL (hardware/interfaces/health/1.0/types.hal).
71     Return<void> ret = gHealthHal->getHealthInfo([&](Result r, HealthInfo v) {
72         if (r != Result::SUCCESS) {
73             result_success = false;
74             return;
75         }
76         if (mTagId == android::util::REMAINING_BATTERY_CAPACITY) {
77             auto ptr = make_shared<LogEvent>(android::util::REMAINING_BATTERY_CAPACITY,
78                                              wallClockTimestampNs, elapsedTimestampNs);
79             ptr->write(v.legacy.batteryChargeCounter);
80             ptr->init();
81             data->push_back(ptr);
82         } else if (mTagId == android::util::FULL_BATTERY_CAPACITY) {
83             auto ptr = make_shared<LogEvent>(android::util::FULL_BATTERY_CAPACITY,
84                                              wallClockTimestampNs, elapsedTimestampNs);
85             ptr->write(v.legacy.batteryFullCharge);
86             ptr->init();
87             data->push_back(ptr);
88         } else if (mTagId == android::util::BATTERY_VOLTAGE) {
89             auto ptr = make_shared<LogEvent>(android::util::BATTERY_VOLTAGE, wallClockTimestampNs,
90                                              elapsedTimestampNs);
91             ptr->write(v.legacy.batteryVoltage);
92             ptr->init();
93             data->push_back(ptr);
94         } else if (mTagId == android::util::BATTERY_LEVEL) {
95             auto ptr = make_shared<LogEvent>(android::util::BATTERY_LEVEL, wallClockTimestampNs,
96                                              elapsedTimestampNs);
97             ptr->write(v.legacy.batteryLevel);
98             ptr->init();
99             data->push_back(ptr);
100         } else if (mTagId == android::util::BATTERY_CYCLE_COUNT) {
101             auto ptr = make_shared<LogEvent>(android::util::BATTERY_CYCLE_COUNT,
102                                              wallClockTimestampNs, elapsedTimestampNs);
103             ptr->write(v.legacy.batteryCycleCount);
104             ptr->init();
105             data->push_back(ptr);
106         } else {
107             ALOGE("Unsupported tag in ResourceHealthManagerPuller: %d", mTagId);
108         }
109     });
110     if (!result_success || !ret.isOk()) {
111         ALOGE("getHealthHal() failed: health HAL service not available. Description: %s",
112               ret.description().c_str());
113         if (!ret.isOk() && ret.isDeadObject()) {
114             gHealthHal = nullptr;
115         }
116         return false;
117     }
118     return true;
119 }
120 
121 }  // namespace statsd
122 }  // namespace os
123 }  // namespace android
124