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 "statslog.h"
28 #include "stats_log_util.h"
29 
30 using android::hardware::hidl_vec;
31 using android::hardware::health::V2_0::get_health_service;
32 using android::hardware::health::V2_0::HealthInfo;
33 using android::hardware::health::V2_0::IHealth;
34 using android::hardware::health::V2_0::Result;
35 using android::hardware::Return;
36 using android::hardware::Void;
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: 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     Return<void> ret = gHealthHal->getHealthInfo([&](Result r, HealthInfo v) {
71         if (r != Result::SUCCESS) {
72             result_success = false;
73             return;
74         }
75         if (mTagId == android::util::REMAINING_BATTERY_CAPACITY) {
76             auto ptr = make_shared<LogEvent>(android::util::REMAINING_BATTERY_CAPACITY,
77                 wallClockTimestampNs, elapsedTimestampNs);
78             ptr->write(v.legacy.batteryChargeCounter);
79             ptr->init();
80             data->push_back(ptr);
81         } else if (mTagId == android::util::FULL_BATTERY_CAPACITY) {
82             auto ptr = make_shared<LogEvent>(android::util::FULL_BATTERY_CAPACITY,
83                 wallClockTimestampNs, elapsedTimestampNs);
84             ptr->write(v.legacy.batteryFullCharge);
85             ptr->init();
86             data->push_back(ptr);
87         } else {
88             ALOGE("Unsupported tag in ResourceHealthManagerPuller: %d", mTagId);
89         }
90     });
91     if (!result_success || !ret.isOk()) {
92         ALOGE("getHealthHal() failed: health HAL service not available. Description: %s",
93                 ret.description().c_str());
94         if (!ret.isOk() && ret.isDeadObject()) {
95             gHealthHal = nullptr;
96         }
97         return false;
98     }
99     return true;
100 }
101 
102 }  // namespace statsd
103 }  // namespace os
104 }  // namespace android
105