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 30using android::hardware::hidl_vec; 31using android::hardware::health::V2_0::get_health_service; 32using android::hardware::health::V2_0::HealthInfo; 33using android::hardware::health::V2_0::IHealth; 34using android::hardware::health::V2_0::Result; 35using android::hardware::Return; 36using android::hardware::Void; 37 38using std::make_shared; 39using std::shared_ptr; 40 41namespace android { 42namespace os { 43namespace statsd { 44 45sp<android::hardware::health::V2_0::IHealth> gHealthHal = nullptr; 46 47bool getHealthHal() { 48 if (gHealthHal == nullptr) { 49 gHealthHal = get_health_service(); 50 } 51 return gHealthHal != nullptr; 52} 53 54ResourceHealthManagerPuller::ResourceHealthManagerPuller(int tagId) : StatsPuller(tagId) { 55} 56 57// TODO: add other health atoms (eg. Temperature). 58bool 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