Commit 46b6ff75 authored by Andrei-Laurențiu Olteanu's avatar Andrei-Laurențiu Olteanu Committed by Commit Bot

[Telemetry SWX] Add battery charge routine

Add implementation to chrome://.

Add implementation to chrome-untrusted://.

Add tests.

Bug: b:162051831
Change-Id: I79b6010eb70f4209261273ea37d2069ce1b72195
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2418674
Commit-Queue: Laurențiu Olteanu <lolteanu@google.com>
Reviewed-by: default avatarTom Sepez <tsepez@chromium.org>
Reviewed-by: default avatarOleh Lamzin <lamzin@google.com>
Reviewed-by: default avatarMahmoud Gawad <mgawad@google.com>
Cr-Commit-Position: refs/heads/master@{#811153}
parent 2ce3b0f7
......@@ -220,4 +220,19 @@ void DiagnosticsService::RunBatteryDischargeRoutine(
std::move(callback)));
}
void DiagnosticsService::RunBatteryChargeRoutine(
uint32_t length_seconds,
uint32_t minimum_charge_percent_required,
RunBatteryChargeRoutineCallback callback) {
GetService()->RunBatteryChargeRoutine(
length_seconds, minimum_charge_percent_required,
base::BindOnce(
[](health::mojom::DiagnosticsService::RunBatteryChargeRoutineCallback
callback,
cros_healthd::mojom::RunRoutineResponsePtr ptr) {
std::move(callback).Run(converters::ConvertPtr(std::move(ptr)));
},
std::move(callback)));
}
} // namespace chromeos
......@@ -70,6 +70,10 @@ class DiagnosticsService : public health::mojom::DiagnosticsService {
uint32_t length_seconds,
uint32_t maximum_discharge_percent_allowed,
RunBatteryDischargeRoutineCallback callback) override;
void RunBatteryChargeRoutine(
uint32_t length_seconds,
uint32_t minimum_charge_percent_required,
RunBatteryChargeRoutineCallback callback) override;
// Pointer to real implementation.
mojo::Remote<cros_healthd::mojom::CrosHealthdDiagnosticsService> service_;
......
......@@ -219,6 +219,23 @@ interface DiagnosticsService {
RunBatteryDischargeRoutine(uint32 length_seconds,
uint32 maximum_discharge_percent_allowed)
=> (RunRoutineResponse response);
// Requests that the BatteryCharge routine is created and started on the
// platform. This routine checks the battery's charge rate over a period of
// time. This routine is only available if GetAvailableRoutines returned
// kBatteryCharge.
//
// The request:
// * |length_seconds| - length of time to run the routine for.
// * |minimum_charge_percent_required| - the routine will fail if the battery
// charges by less than this percentage.
//
// The response:
// * |response| - contains a unique identifier and status for the created
// routine.
RunBatteryChargeRoutine(uint32 length_seconds,
uint32 minimum_charge_percent_required)
=> (RunRoutineResponse response);
};
// Enumeration of each of the diagnostics routines the platform may support.
......
......@@ -39,6 +39,8 @@ dpsl_internal.Message = {
'DiagnosticsService.RunPrimeSearchRoutine',
DIAGNOSTICS_RUN_BATTERY_DISCHARGE_ROUTINE:
'DiagnosticsService.RunBatteryDischargeRoutine',
DIAGNOSTICS_RUN_BATTERY_CHARGE_ROUTINE:
'DiagnosticsService.RunBatteryChargeRoutine',
PROBE_TELEMETRY_INFO: 'ProbeService.ProbeTelemetryInfo',
};
......@@ -160,6 +162,15 @@ dpsl_internal.DiagnosticsRunPrimeSearchRoutineRequest;
*/
dpsl_internal.DiagnosticsRunBatteryDischargeRoutineRequest;
/**
* Request message sent by the unprivileged context to the privileged
* context to run battery charge routine.
* @typedef {{
* lengthSeconds: !number,
* minimumChargePercentRequired: !number}}
*/
dpsl_internal.DiagnosticsRunBatteryChargeRoutineRequest;
/**
* Response message sent by the privileged context containing routine
* information.
......
......@@ -506,6 +506,22 @@ class DiagnosticsProxy {
return await getOrCreateDiagnosticsService().runBatteryDischargeRoutine(
request.lengthSeconds, request.maximumDischargePercentAllowed);
};
/**
* Runs battery charge routine.
* @param { !Object } message
* @return { !RunRoutineResponsePromise }
*/
async handleRunBatteryChargeRoutine(message) {
const request =
/**
@type {!dpsl_internal.DiagnosticsRunBatteryChargeRoutineRequest}
*/
(message);
this.assertNumberIsPositive(request.lengthSeconds);
return await getOrCreateDiagnosticsService().runBatteryChargeRoutine(
request.lengthSeconds, request.minimumChargePercentRequired);
};
};
const diagnosticsProxy = new DiagnosticsProxy();
......@@ -852,6 +868,12 @@ untrustedMessagePipe.registerHandler(
(message) => diagnosticsProxy.handleRunBatteryDischargeRoutine(message),
message));
untrustedMessagePipe.registerHandler(
dpsl_internal.Message.DIAGNOSTICS_RUN_BATTERY_CHARGE_ROUTINE,
(message) => diagnosticsProxy.genericRunRoutineHandler(
(message) => diagnosticsProxy.handleRunBatteryChargeRoutine(message),
message));
untrustedMessagePipe.registerHandler(
dpsl_internal.Message.PROBE_TELEMETRY_INFO,
(message) => telemetryProxy.handleProbeTelemetryInfo(message));
......@@ -316,6 +316,32 @@ chromeos.test_support = {};
}
return response;
}
/**
* Requests battery charge routine to be run.
* @param { !number } lengthSeconds
* @param { !number } minimumChargePercentRequired
* @return { !Promise<!Object> }
* @public
*/
async runBatteryChargeRoutine(lengthSeconds, minimumChargePercentRequired) {
const message =
/**
@type {!dpsl_internal.DiagnosticsRunBatteryChargeRoutineRequest}
*/
({
lengthSeconds: lengthSeconds,
minimumChargePercentRequired: minimumChargePercentRequired
});
const response =
/** @type {!Object} */ (await messagePipe.sendMessage(
dpsl_internal.Message.DIAGNOSTICS_RUN_BATTERY_CHARGE_ROUTINE,
message));
if (response instanceof Error) {
throw response;
}
return response;
}
};
/**
......
......@@ -395,6 +395,8 @@ const untrustedTests = [
['UntrustedDiagnosticsRequestRunPrimeSearchRoutine'],
['UntrustedDiagnosticsRequestRunBatteryDischargeRoutineInvalidInput'],
['UntrustedDiagnosticsRequestRunBatteryDischargeRoutine'],
['UntrustedDiagnosticsRequestRunBatteryChargeRoutineInvalidInput'],
['UntrustedDiagnosticsRequestRunBatteryChargeRoutine'],
['UntrustedRequestTelemetryInfoUnknownCategory'],
['UntrustedRequestTelemetryInfo'],
[
......
......@@ -346,6 +346,40 @@ UNTRUSTED_TEST(
assertDeepEquals(response, {id: 123456789, status: 'ready'});
});
// Tests that runBatteryChargeRoutine throws the correct error when invalid
// enum is passed as input.
UNTRUSTED_TEST(
'UntrustedDiagnosticsRequestRunBatteryChargeRoutineInvalidInput',
async () => {
let caughtError1;
try {
await chromeos.diagnostics.runBatteryChargeRoutine(0, 23);
} catch (error) {
caughtError1 = error;
}
assertEquals(caughtError1.name, 'RangeError');
assertEquals(caughtError1.message, `Parameter must be positive.`);
let caughtError2;
try {
await chromeos.diagnostics.runBatteryChargeRoutine(-2147483648, 1);
} catch (error) {
caughtError2 = error;
}
assertEquals(caughtError2.name, 'RangeError');
assertEquals(caughtError2.message, `Parameter must be positive.`);
});
// Tests that runBatteryChargeRoutine returns the correct Object.
UNTRUSTED_TEST(
'UntrustedDiagnosticsRequestRunBatteryChargeRoutine', async () => {
const response =
await chromeos.diagnostics.runBatteryChargeRoutine(12, 5);
assertDeepEquals(response, {id: 123456789, status: 'ready'});
});
// Tests that TelemetryInfo throws an error if category is unknown.
UNTRUSTED_TEST('UntrustedRequestTelemetryInfoUnknownCategory', async () => {
let caughtError = {};
......
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