Implement health AIDL HAL.

Test: VTS
Test: manual charger mode
Test: recovery
Bug: 213273090

Change-Id: Ie02d7bc1e1c6e39bbab22f008b7767c5de620a73
This commit is contained in:
Yifan Hong 2021-10-25 17:14:40 -07:00
parent 745bcfa3b1
commit a636457b5a
8 changed files with 140 additions and 111 deletions

View file

@ -83,3 +83,9 @@ $(call add-clean-step, rm -f $(PRODUCT_OUT)/vendor/bin/hw/android.hardware.keyma
$(call add-clean-step, rm -f $(PRODUCT_OUT)/vendor/etc/init/android.hardware.keymaster@4.0-service.trusty.rc) $(call add-clean-step, rm -f $(PRODUCT_OUT)/vendor/etc/init/android.hardware.keymaster@4.0-service.trusty.rc)
$(call add-clean-step, rm -f $(PRODUCT_OUT)/vendor/etc/vintf/manifest/android.hardware.keymaster@4.0-service.trusty.xml) $(call add-clean-step, rm -f $(PRODUCT_OUT)/vendor/etc/vintf/manifest/android.hardware.keymaster@4.0-service.trusty.xml)
$(call add-clean-step, rm -f $(PRODUCT_OUT)/vendor/bin/hw/wait_for_strongbox) $(call add-clean-step, rm -f $(PRODUCT_OUT)/vendor/bin/hw/wait_for_strongbox)
# Health HAL to AIDL
$(call add-clean-step, find $(PRODUCT_OUT)/system -type f -name "*charger*" -print0 | xargs -0 rm -f)
$(call add-clean-step, find $(PRODUCT_OUT)/vendor -type f -name "*health@*" -print0 | xargs -0 rm -f)
$(call add-clean-step, find $(PRODUCT_OUT)/recovery/root -type f -name "*charger*" -print0 | xargs -0 rm -f)
$(call add-clean-step, find $(PRODUCT_OUT)/recovery/root -type f -name "*health@*" -print0 | xargs -0 rm -f)

View file

@ -3,17 +3,6 @@ import android.hardware.drm@1.2-service.widevine.rc
import init.exynos.sensorhub.rc import init.exynos.sensorhub.rc
import /vendor/etc/init/hw/init.aoc.rc import /vendor/etc/init/hw/init.aoc.rc
service vendor.charger /system/bin/charger
class charger
seclabel u:r:charger:s0
user system
group system wakelock input
capabilities SYS_BOOT
file /dev/kmsg w
file /sys/fs/pstore/console-ramoops-0 r
file /sys/fs/pstore/console-ramoops r
file /proc/last_kmsg r
on early-init on early-init
mount_all /vendor/etc/fstab.persist --early mount_all /vendor/etc/fstab.persist --early

View file

@ -925,8 +925,8 @@ PRODUCT_PACKAGES_DEBUG += \
$(NULL) $(NULL)
PRODUCT_PACKAGES += \ PRODUCT_PACKAGES += \
android.hardware.health@2.1-impl-gs101 \ android.hardware.health-service.gs101 \
android.hardware.health@2.1-service android.hardware.health-service.gs101_recovery \
# Audio # Audio
# Audio HAL Server & Default Implementations # Audio HAL Server & Default Implementations

View file

@ -13,7 +13,6 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package { package {
// See: http://go/android-license-faq // See: http://go/android-license-faq
// A large-scale-change added 'default_applicable_licenses' to import // A large-scale-change added 'default_applicable_licenses' to import
@ -24,36 +23,39 @@ package {
"//device/google/gs101:device_google_gs101_license", "//device/google/gs101:device_google_gs101_license",
], ],
} }
cc_defaults {
cc_library_shared { name: "android.hardware.health-service.gs101-defaults",
name: "android.hardware.health@2.1-impl-gs101", defaults: [
stem: "android.hardware.health@2.0-impl-2.1-gs101", "libhealth_aidl_impl_user",
"libhealth_aidl_charger_defaults",
proprietary: true, ],
relative_install_path: "hw", relative_install_path: "hw",
vintf_fragments: ["android.hardware.health-service.gs101.xml"],
srcs: [ srcs: [
"Health.cpp", "Health.cpp",
], ],
cflags: [ cflags: [
"-Wall", "-Wall",
"-Werror", "-Werror",
], ],
static_libs: [ static_libs: [
"android.hardware.health@1.0-convert", "libhealth_aidl_impl",
"libbatterymonitor",
"libhealth2impl",
"libhealthloop",
],
shared_libs: [
"libbase",
"libcutils",
"libhidlbase",
"libpixelhealth",
"libutils",
"android.hardware.health@2.0",
"android.hardware.health@2.1",
], ],
} }
cc_binary {
name: "android.hardware.health-service.gs101",
defaults: ["android.hardware.health-service.gs101-defaults"],
proprietary: true,
init_rc: ["android.hardware.health-service.gs101.rc"],
overrides: ["charger"],
shared_libs: [
"libpixelhealth",
],
}
cc_binary {
name: "android.hardware.health-service.gs101_recovery",
defaults: ["android.hardware.health-service.gs101-defaults"],
recovery: true,
init_rc: ["android.hardware.health-service.gs101_recovery.rc"],
overrides: ["charger.recovery"],
}

View file

@ -19,17 +19,20 @@
#include <android-base/file.h> #include <android-base/file.h>
#include <android-base/parseint.h> #include <android-base/parseint.h>
#include <android-base/strings.h> #include <android-base/strings.h>
#include <android/hardware/health/2.0/types.h> #include <android/hardware/health/translate-ndk.h>
#include <health2impl/Health.h> #include <health-impl/Health.h>
#include <health/utils.h> #include <health/utils.h>
#include <hal_conversion.h>
// Recovery doesn't have libpixelhealth and charger mode
#ifndef __ANDROID_RECOVERY__
#include <health-impl/ChargerUtils.h>
#include <pixelhealth/BatteryDefender.h> #include <pixelhealth/BatteryDefender.h>
#include <pixelhealth/BatteryMetricsLogger.h> #include <pixelhealth/BatteryMetricsLogger.h>
#include <pixelhealth/BatteryThermalControl.h> #include <pixelhealth/BatteryThermalControl.h>
#include <pixelhealth/ChargerDetect.h> #include <pixelhealth/ChargerDetect.h>
#include <pixelhealth/DeviceHealth.h> #include <pixelhealth/DeviceHealth.h>
#include <pixelhealth/LowBatteryShutdownMetrics.h> #include <pixelhealth/LowBatteryShutdownMetrics.h>
#endif // !__ANDROID_RECOVERY__
#include <chrono> #include <chrono>
#include <fstream> #include <fstream>
@ -41,15 +44,15 @@ namespace {
using namespace std::literals; using namespace std::literals;
using android::hardware::health::V1_0::hal_conversion::convertFromHealthInfo; using aidl::android::hardware::health::DiskStats;
using android::hardware::health::V1_0::hal_conversion::convertToHealthInfo; using aidl::android::hardware::health::HalHealthLoop;
using android::hardware::health::V2_0::DiskStats; using aidl::android::hardware::health::HealthInfo;
using android::hardware::health::V2_0::StorageAttribute; using aidl::android::hardware::health::StorageInfo;
using android::hardware::health::V2_0::StorageInfo;
using android::hardware::health::V2_0::Result;
using ::android::hardware::health::V2_1::IHealth;
using android::hardware::health::InitHealthdConfig; using android::hardware::health::InitHealthdConfig;
#ifndef __ANDROID_RECOVERY__
using aidl::android::hardware::health::charger::ChargerCallback;
using aidl::android::hardware::health::charger::ChargerModeMain;
using hardware::google::pixel::health::BatteryDefender; using hardware::google::pixel::health::BatteryDefender;
using hardware::google::pixel::health::BatteryMetricsLogger; using hardware::google::pixel::health::BatteryMetricsLogger;
using hardware::google::pixel::health::BatteryThermalControl; using hardware::google::pixel::health::BatteryThermalControl;
@ -72,6 +75,7 @@ static BatteryThermalControl battThermalControl(
static BatteryMetricsLogger battMetricsLogger(kBatteryResistance, kBatteryOCV); static BatteryMetricsLogger battMetricsLogger(kBatteryResistance, kBatteryOCV);
static LowBatteryShutdownMetrics shutdownMetrics(kVoltageAvg); static LowBatteryShutdownMetrics shutdownMetrics(kVoltageAvg);
static DeviceHealth deviceHealth; static DeviceHealth deviceHealth;
#endif // !__ANDROID_RECOVERY__
#define UFS_DIR "/dev/sys/block/bootdevice" #define UFS_DIR "/dev/sys/block/bootdevice"
constexpr char kUfsHealthEol[]{UFS_DIR "/health_descriptor/eol_info"}; constexpr char kUfsHealthEol[]{UFS_DIR "/health_descriptor/eol_info"};
@ -79,7 +83,6 @@ constexpr char kUfsHealthLifetimeA[]{UFS_DIR "/health_descriptor/life_time_estim
constexpr char kUfsHealthLifetimeB[]{UFS_DIR "/health_descriptor/life_time_estimation_b"}; constexpr char kUfsHealthLifetimeB[]{UFS_DIR "/health_descriptor/life_time_estimation_b"};
constexpr char kUfsVersion[]{UFS_DIR "/device_descriptor/specification_version"}; constexpr char kUfsVersion[]{UFS_DIR "/device_descriptor/specification_version"};
constexpr char kDiskStatsFile[]{"/sys/block/sda/stat"}; constexpr char kDiskStatsFile[]{"/sys/block/sda/stat"};
constexpr char kUFSName[]{"UFS0"};
static std::string ufs_version; static std::string ufs_version;
static uint16_t eol; static uint16_t eol;
@ -88,8 +91,10 @@ static uint16_t lifetimeB;
static std::chrono::system_clock::time_point ufs_last_query_time; static std::chrono::system_clock::time_point ufs_last_query_time;
constexpr auto kUfsQueryIntervalHours = std::chrono::hours{24}; constexpr auto kUfsQueryIntervalHours = std::chrono::hours{24};
#ifndef __ANDROID_RECOVERY__
static bool needs_wlc_updates = false; static bool needs_wlc_updates = false;
constexpr char kWlcCapacity[]{WLC_DIR "/capacity"}; constexpr char kWlcCapacity[]{WLC_DIR "/capacity"};
#endif // !__ANDROID_RECOVERY__
std::ifstream assert_open(const std::string &path) { std::ifstream assert_open(const std::string &path) {
std::ifstream stream(path); std::ifstream stream(path);
@ -118,12 +123,10 @@ void read_ufs_version(StorageInfo *info) {
info->version = ufs_version; info->version = ufs_version;
} }
void fill_ufs_storage_attribute(StorageAttribute *attr) { #ifdef __ANDROID_RECOVERY__
attr->isInternal = true; void private_healthd_board_init(struct healthd_config *) {}
attr->isBootDevice = true; int private_healthd_board_battery_update(HealthInfo *) { return 0; }
attr->name = kUFSName; #else // !__ANDROID__RECOVERY__
}
static bool FileExists(const std::string &filename) { static bool FileExists(const std::string &filename) {
struct stat buffer; struct stat buffer;
@ -140,26 +143,26 @@ void private_healthd_board_init(struct healthd_config *hc) {
} }
} }
int private_healthd_board_battery_update(struct android::BatteryProperties *props) { int private_healthd_board_battery_update(HealthInfo *health_info) {
deviceHealth.update(props); deviceHealth.update(health_info);
battThermalControl.updateThermalState(props); battThermalControl.updateThermalState(*health_info);
battMetricsLogger.logBatteryProperties(props); battMetricsLogger.logBatteryProperties(*health_info);
shutdownMetrics.logShutdownVoltage(props); shutdownMetrics.logShutdownVoltage(*health_info);
// Allow BatteryDefender to override online properties // Allow BatteryDefender to override online properties
ChargerDetect::onlineUpdate(props); ChargerDetect::onlineUpdate(health_info);
battDefender.update(props); battDefender.update(health_info);
if (needs_wlc_updates && if (needs_wlc_updates &&
!android::base::WriteStringToFile(std::to_string(props->batteryLevel), kWlcCapacity)) !android::base::WriteStringToFile(std::to_string(health_info->batteryLevel), kWlcCapacity))
LOG(INFO) << "Unable to write battery level to wireless capacity"; LOG(INFO) << "Unable to write battery level to wireless capacity";
return 0; return 0;
} }
#endif // __ANDROID_RECOVERY__
void private_get_storage_info(std::vector<StorageInfo> &vec_storage_info) { void private_get_storage_info(std::vector<StorageInfo> *vec_storage_info) {
vec_storage_info.resize(1); vec_storage_info->resize(1);
StorageInfo *storage_info = &vec_storage_info[0]; StorageInfo *storage_info = &vec_storage_info->at(0);
fill_ufs_storage_attribute(&storage_info->attr);
read_ufs_version(storage_info); read_ufs_version(storage_info);
@ -180,10 +183,9 @@ void private_get_storage_info(std::vector<StorageInfo> &vec_storage_info) {
return; return;
} }
void private_get_disk_stats(std::vector<DiskStats> &vec_stats) { void private_get_disk_stats(std::vector<DiskStats> *vec_stats) {
vec_stats.resize(1); vec_stats->resize(1);
DiskStats *stats = &vec_stats[0]; DiskStats *stats = &vec_stats->at(0);
fill_ufs_storage_attribute(&stats->attr);
auto stream = assert_open(kDiskStatsFile); auto stream = assert_open(kDiskStatsFile);
// Regular diskstats entries // Regular diskstats entries
@ -195,18 +197,14 @@ void private_get_disk_stats(std::vector<DiskStats> &vec_stats) {
} }
} // anonymous namespace } // anonymous namespace
namespace android { namespace aidl::android::hardware::health::implementation {
namespace hardware {
namespace health {
namespace V2_1 {
namespace implementation {
class HealthImpl : public Health { class HealthImpl : public Health {
public: public:
HealthImpl(std::unique_ptr<healthd_config>&& config) HealthImpl(std::string_view instance_name, std::unique_ptr<healthd_config>&& config)
: Health(std::move(config)) {} : Health(std::move(instance_name), std::move(config)) {}
Return<void> getStorageInfo(getStorageInfo_cb _hidl_cb) override; ndk::ScopedAStatus getDiskStats(std::vector<DiskStats>* out) override;
Return<void> getDiskStats(getDiskStats_cb _hidl_cb) override; ndk::ScopedAStatus getStorageInfo(std::vector<StorageInfo>* out) override;
protected: protected:
void UpdateHealthInfo(HealthInfo* health_info) override; void UpdateHealthInfo(HealthInfo* health_info) override;
@ -214,53 +212,57 @@ class HealthImpl : public Health {
}; };
void HealthImpl::UpdateHealthInfo(HealthInfo* health_info) { void HealthImpl::UpdateHealthInfo(HealthInfo* health_info) {
struct BatteryProperties props; private_healthd_board_battery_update(health_info);
convertFromHealthInfo(health_info->legacy.legacy, &props);
private_healthd_board_battery_update(&props);
convertToHealthInfo(&props, health_info->legacy.legacy);
} }
Return<void> HealthImpl::getStorageInfo(getStorageInfo_cb _hidl_cb) ndk::ScopedAStatus HealthImpl::getStorageInfo(std::vector<StorageInfo>* out)
{ {
std::vector<struct StorageInfo> info; private_get_storage_info(out);
private_get_storage_info(info); if (out->empty()) {
hidl_vec<struct StorageInfo> info_vec(info); return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
if (!info.size()) {
_hidl_cb(Result::NOT_SUPPORTED, info_vec);
} else {
_hidl_cb(Result::SUCCESS, info_vec);
} }
return Void(); return ndk::ScopedAStatus::ok();
} }
Return<void> HealthImpl::getDiskStats(getDiskStats_cb _hidl_cb) ndk::ScopedAStatus HealthImpl::getDiskStats(std::vector<DiskStats>* out)
{ {
std::vector<struct DiskStats> stats; private_get_disk_stats(out);
private_get_disk_stats(stats); if (out->empty()) {
hidl_vec<struct DiskStats> stats_vec(stats); return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
if (!stats.size()) {
_hidl_cb(Result::NOT_SUPPORTED, stats_vec);
} else {
_hidl_cb(Result::SUCCESS, stats_vec);
} }
return Void(); return ndk::ScopedAStatus::ok();
} }
} // namespace implementation } // namespace aidl::android::hardware::health::implementation
} // namespace V2_1
} // namespace health int main(int argc, char **argv) {
} // namespace hardware using ::aidl::android::hardware::health::implementation::HealthImpl;
} // namespace android
// Use kernel logging in recovery
#ifdef __ANDROID_RECOVERY__
android::base::InitLogging(argv, android::base::KernelLogger);
#endif
extern "C" IHealth* HIDL_FETCH_IHealth(const char* instance) {
using ::android::hardware::health::V2_1::implementation::HealthImpl;
if (instance != "default"sv) {
return nullptr;
}
auto config = std::make_unique<healthd_config>(); auto config = std::make_unique<healthd_config>();
InitHealthdConfig(config.get()); InitHealthdConfig(config.get());
private_healthd_board_init(config.get()); private_healthd_board_init(config.get());
return new HealthImpl(std::move(config)); auto binder =
ndk::SharedRefBase::make<HealthImpl>("default"sv, std::move(config));
if (argc >= 2 && argv[1] == "--charger"sv) {
// In regular mode, start charger UI.
#ifndef __ANDROID_RECOVERY__
LOG(INFO) << "Starting charger mode with UI.";
return ChargerModeMain(binder, std::make_shared<ChargerCallback>(binder));
#endif
// In recovery, ignore --charger arg.
LOG(INFO) << "Starting charger mode without UI.";
} else {
LOG(INFO) << "Starting health HAL.";
}
auto hal_health_loop = std::make_shared<HalHealthLoop>(binder, binder);
return hal_health_loop->StartLoop();
} }

View file

@ -0,0 +1,16 @@
service vendor.health-gs101 /vendor/bin/hw/android.hardware.health-service.gs101
class hal
user system
group system
capabilities WAKE_ALARM BLOCK_SUSPEND
file /dev/kmsg w
service vendor.charger-gs101 /vendor/bin/hw/android.hardware.health-service.gs101 --charger
class charger
seclabel u:r:charger_vendor:s0
user system
group system wakelock input
capabilities SYS_BOOT
file /dev/kmsg w
file /sys/fs/pstore/console-ramoops-0 r
file /sys/fs/pstore/console-ramoops r
file /proc/last_kmsg r

View file

@ -0,0 +1,7 @@
<manifest version="1.0" type="device">
<hal format="aidl">
<name>android.hardware.health</name>
<version>1</version>
<fqname>IHealth/default</fqname>
</hal>
</manifest>

View file

@ -0,0 +1,7 @@
service vendor.health-gs101 /system/bin/hw/android.hardware.health-service.gs101_recovery
class hal
seclabel u:r:hal_health_default:s0
user system
group system
capabilities WAKE_ALARM BLOCK_SUSPEND
file /dev/kmsg w