Commit 48941d13 authored by Bailey Berro's avatar Bailey Berro Committed by Commit Bot

SystemDataProvider: Implement GetBatteryInfo()

Implements the GetBatteryInfo(). Provides static information about the
battery included manufacturer and designed capacity.

Bug: 1128204
Change-Id: Icdc54b865d8506078258c4877f3203ad1fed38cf
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2419600
Commit-Queue: Bailey Berro <baileyberro@chromium.org>
Reviewed-by: default avatarZentaro Kavanagh <zentaro@chromium.org>
Reviewed-by: default avatarDominick Ng <dominickn@chromium.org>
Reviewed-by: default avatarJimmy Gong <jimmyxgong@chromium.org>
Cr-Commit-Position: refs/heads/master@{#809664}
parent c1aad7de
...@@ -65,6 +65,14 @@ void PopulateDeviceCapabilities(const healthd::TelemetryInfo& telemetry_info, ...@@ -65,6 +65,14 @@ void PopulateDeviceCapabilities(const healthd::TelemetryInfo& telemetry_info,
out_system_info.device_capabilities = std::move(capabilities); out_system_info.device_capabilities = std::move(capabilities);
} }
void PopulateBatteryInfo(const healthd::BatteryInfo& battery_info,
mojom::BatteryInfo& out_battery_info) {
out_battery_info.manufacturer = battery_info.vendor;
// Multiply by 1000 to convert amps to milliamps.
out_battery_info.charge_full_design_milliamp_hours =
battery_info.charge_full_design * 1000;
}
} // namespace } // namespace
SystemDataProvider::SystemDataProvider() = default; SystemDataProvider::SystemDataProvider() = default;
...@@ -80,6 +88,15 @@ void SystemDataProvider::GetSystemInfo(GetSystemInfoCallback callback) { ...@@ -80,6 +88,15 @@ void SystemDataProvider::GetSystemInfo(GetSystemInfoCallback callback) {
base::Unretained(this), std::move(callback))); base::Unretained(this), std::move(callback)));
} }
void SystemDataProvider::GetBatteryInfo(GetBatteryInfoCallback callback) {
BindCrosHealthdProbeServiceIfNeccessary();
probe_service_->ProbeTelemetryInfo(
{ProbeCategories::kBattery},
base::BindOnce(&SystemDataProvider::OnBatteryInfoProbeResponse,
base::Unretained(this), std::move(callback)));
}
void SystemDataProvider::OnSystemInfoProbeResponse( void SystemDataProvider::OnSystemInfoProbeResponse(
GetSystemInfoCallback callback, GetSystemInfoCallback callback,
healthd::TelemetryInfoPtr info_ptr) { healthd::TelemetryInfoPtr info_ptr) {
...@@ -124,6 +141,29 @@ void SystemDataProvider::OnSystemInfoProbeResponse( ...@@ -124,6 +141,29 @@ void SystemDataProvider::OnSystemInfoProbeResponse(
std::move(callback).Run(std::move(system_info)); std::move(callback).Run(std::move(system_info));
} }
void SystemDataProvider::OnBatteryInfoProbeResponse(
GetBatteryInfoCallback callback,
healthd::TelemetryInfoPtr info_ptr) {
mojom::BatteryInfoPtr battery_info = mojom::BatteryInfo::New();
if (info_ptr.is_null()) {
LOG(ERROR) << "Null response from croshealthd::ProbeTelemetryInfo.";
std::move(callback).Run(std::move(battery_info));
return;
}
const healthd::BatteryInfo* battery_info_ptr =
diagnostics::GetBatteryInfo(*info_ptr);
if (!battery_info_ptr) {
LOG(ERROR) << "BatteryInfo requested by device does not have a battery.";
std::move(callback).Run(std::move(battery_info));
return;
}
PopulateBatteryInfo(*battery_info_ptr, *battery_info.get());
std::move(callback).Run(std::move(battery_info));
}
void SystemDataProvider::BindCrosHealthdProbeServiceIfNeccessary() { void SystemDataProvider::BindCrosHealthdProbeServiceIfNeccessary() {
if (!probe_service_ || !probe_service_.is_connected()) { if (!probe_service_ || !probe_service_.is_connected()) {
cros_healthd::ServiceConnection::GetInstance()->GetProbeService( cros_healthd::ServiceConnection::GetInstance()->GetProbeService(
......
...@@ -23,6 +23,7 @@ class SystemDataProvider : public mojom::SystemDataProvider { ...@@ -23,6 +23,7 @@ class SystemDataProvider : public mojom::SystemDataProvider {
// mojom::SystemDataProvider: // mojom::SystemDataProvider:
void GetSystemInfo(GetSystemInfoCallback callback) override; void GetSystemInfo(GetSystemInfoCallback callback) override;
void GetBatteryInfo(GetBatteryInfoCallback callback) override;
private: private:
void BindCrosHealthdProbeServiceIfNeccessary(); void BindCrosHealthdProbeServiceIfNeccessary();
...@@ -33,6 +34,10 @@ class SystemDataProvider : public mojom::SystemDataProvider { ...@@ -33,6 +34,10 @@ class SystemDataProvider : public mojom::SystemDataProvider {
GetSystemInfoCallback callback, GetSystemInfoCallback callback,
cros_healthd::mojom::TelemetryInfoPtr info_ptr); cros_healthd::mojom::TelemetryInfoPtr info_ptr);
void OnBatteryInfoProbeResponse(
GetBatteryInfoCallback callback,
cros_healthd::mojom::TelemetryInfoPtr info_ptr);
mojo::Remote<cros_healthd::mojom::CrosHealthdProbeService> probe_service_; mojo::Remote<cros_healthd::mojom::CrosHealthdProbeService> probe_service_;
}; };
......
...@@ -22,6 +22,33 @@ namespace chromeos { ...@@ -22,6 +22,33 @@ namespace chromeos {
namespace diagnostics { namespace diagnostics {
namespace { namespace {
void SetProbeTelemetryInfoResponse(
cros_healthd::mojom::BatteryInfoPtr battery_info,
cros_healthd::mojom::CpuInfoPtr cpu_info,
cros_healthd::mojom::MemoryInfoPtr memory_info,
cros_healthd::mojom::SystemInfoPtr system_info) {
auto info = cros_healthd::mojom::TelemetryInfo::New();
if (system_info) {
info->system_result = cros_healthd::mojom::SystemResult::NewSystemInfo(
std::move(system_info));
}
if (battery_info) {
info->battery_result = cros_healthd::mojom::BatteryResult::NewBatteryInfo(
std::move(battery_info));
}
if (memory_info) {
info->memory_result = cros_healthd::mojom::MemoryResult::NewMemoryInfo(
std::move(memory_info));
}
if (cpu_info) {
info->cpu_result =
cros_healthd::mojom::CpuResult::NewCpuInfo(std::move(cpu_info));
}
cros_healthd::FakeCrosHealthdClient::Get()
->SetProbeTelemetryInfoResponseForTesting(info);
}
void SetCrosHealthdSystemInfoResponse(const std::string& board_name, void SetCrosHealthdSystemInfoResponse(const std::string& board_name,
const std::string& cpu_model, const std::string& cpu_model,
uint32_t total_memory_kib, uint32_t total_memory_kib,
...@@ -50,17 +77,66 @@ void SetCrosHealthdSystemInfoResponse(const std::string& board_name, ...@@ -50,17 +77,66 @@ void SetCrosHealthdSystemInfoResponse(const std::string& board_name,
cpu_info->num_total_threads = cpu_threads_count; cpu_info->num_total_threads = cpu_threads_count;
cpu_info->physical_cpus.emplace_back(std::move(physical_cpu_info)); cpu_info->physical_cpus.emplace_back(std::move(physical_cpu_info));
auto info = cros_healthd::mojom::TelemetryInfo::New(); SetProbeTelemetryInfoResponse(std::move(battery_info), std::move(cpu_info),
info->system_result = std::move(memory_info), std::move(system_info));
cros_healthd::mojom::SystemResult::NewSystemInfo(std::move(system_info)); }
info->battery_result = cros_healthd::mojom::BatteryResult::NewBatteryInfo(
std::move(battery_info)); // Constructs a BatteryInfoPtr. If |temperature| = 0, it will be omitted from
info->memory_result = // the response to simulate an empty temperature field.
cros_healthd::mojom::MemoryResult::NewMemoryInfo(std::move(memory_info)); cros_healthd::mojom::BatteryInfoPtr CreateCrosHealthdBatteryInfoResponse(
info->cpu_result = int64_t cycle_count,
cros_healthd::mojom::CpuResult::NewCpuInfo(std::move(cpu_info)); double voltage_now,
cros_healthd::FakeCrosHealthdClient::Get() const std::string& vendor,
->SetProbeTelemetryInfoResponseForTesting(info); const std::string& serial_number,
double charge_full_design,
double charge_full,
double voltage_min_design,
const std::string& model_name,
double charge_now,
double current_now,
const std::string& technology,
const std::string& status,
const base::Optional<std::string>& manufacture_date,
uint64_t temperature) {
cros_healthd::mojom::UInt64ValuePtr temp_value_ptr(
cros_healthd::mojom::UInt64Value::New());
if (temperature != 0) {
temp_value_ptr->value = temperature;
}
auto battery_info = cros_healthd::mojom::BatteryInfo::New(
cycle_count, voltage_now, vendor, serial_number, charge_full_design,
charge_full, voltage_min_design, model_name, charge_now, current_now,
technology, status, manufacture_date, std::move(temp_value_ptr));
return battery_info;
}
cros_healthd::mojom::BatteryInfoPtr CreateCrosHealthdBatteryInfoResponse(
const std::string& vendor,
double charge_full_design) {
return CreateCrosHealthdBatteryInfoResponse(
/*cycle_count=*/0,
/*voltage_now=*/0,
/*vendor=*/vendor,
/*serial_number=*/"",
/*charge_full_design=*/charge_full_design,
/*charge_full=*/0,
/*voltage_min_design=*/0,
/*model_name=*/"",
/*charge_now=*/0,
/*current_now=*/0,
/*technology=*/"",
/*status=*/"",
/*manufacture_date=*/base::nullopt,
/*temperature=*/0);
}
void SetCrosHealthdBatteryInfoResponse(const std::string& vendor,
double charge_full_design) {
cros_healthd::mojom::BatteryInfoPtr battery_info =
CreateCrosHealthdBatteryInfoResponse(vendor, charge_full_design);
SetProbeTelemetryInfoResponse(std::move(battery_info), /*cpu_info=*/nullptr,
/*memory_info=*/nullptr,
/*memory_info=*/nullptr);
} }
} // namespace } // namespace
...@@ -144,5 +220,28 @@ TEST_F(SystemDataProviderTest, NoBattery) { ...@@ -144,5 +220,28 @@ TEST_F(SystemDataProviderTest, NoBattery) {
run_loop.Run(); run_loop.Run();
} }
TEST_F(SystemDataProviderTest, BatteryInfo) {
const std::string expected_manufacturer = "manufacturer";
const double charge_full_amp_hours = 25;
SetCrosHealthdBatteryInfoResponse(expected_manufacturer,
charge_full_amp_hours);
const uint32_t expected_charge_full_design_milliamp_hours =
charge_full_amp_hours * 1000;
base::RunLoop run_loop;
system_data_provider_->GetBatteryInfo(
base::BindLambdaForTesting([&](mojom::BatteryInfoPtr ptr) {
ASSERT_TRUE(ptr);
EXPECT_EQ(expected_manufacturer, ptr->manufacturer);
EXPECT_EQ(expected_charge_full_design_milliamp_hours,
ptr->charge_full_design_milliamp_hours);
run_loop.Quit();
}));
run_loop.Run();
}
} // namespace diagnostics } // namespace diagnostics
} // namespace chromeos } // namespace chromeos
...@@ -24,10 +24,21 @@ struct SystemInfo { ...@@ -24,10 +24,21 @@ struct SystemInfo {
DeviceCapabilities device_capabilities; DeviceCapabilities device_capabilities;
}; };
// Contains information about the battery.
struct BatteryInfo {
string manufacturer;
uint32 charge_full_design_milliamp_hours;
};
// Provides telemetric information about the system. This API is exposed to the // Provides telemetric information about the system. This API is exposed to the
// Diagnostics SWA. // Diagnostics SWA.
interface SystemDataProvider { interface SystemDataProvider {
// Returns a snapshot of system information. |system_info| is static // Returns a snapshot of system information. |system_info| is static
// information that does not change during the lifetime of the service. // information that does not change during the lifetime of the service.
GetSystemInfo() => (SystemInfo system_info); GetSystemInfo() => (SystemInfo system_info);
// Returns information about the battery. |battery_info| is static
// information that does not change during the lifetime fo the service.
// If the device does not have a battery, returns an empty BatteryInfo.
GetBatteryInfo() => (BatteryInfo battery_info);
}; };
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment