Commit 77cc304d authored by Andrei-Laurențiu Olteanu's avatar Andrei-Laurențiu Olteanu Committed by Commit Bot

[Telemetry SWX] Add battery discharge routine

Add implementation to chrome://.

Add implementation to chrome-untrusted://.

Add tests.

Bug: b:162051831
Change-Id: Ia0389cc9f7fbd052559121b8c69d521cc433777a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2414178
Commit-Queue: Laurențiu Olteanu <lolteanu@google.com>
Reviewed-by: default avatarOleh Lamzin <lamzin@google.com>
Reviewed-by: default avatarTom Sepez <tsepez@chromium.org>
Reviewed-by: default avatarMahmoud Gawad <mgawad@google.com>
Cr-Commit-Position: refs/heads/master@{#810172}
parent 5befba76
......@@ -205,4 +205,19 @@ void DiagnosticsService::RunPrimeSearchRoutine(
std::move(callback)));
}
void DiagnosticsService::RunBatteryDischargeRoutine(
uint32_t length_seconds,
uint32_t maximum_discharge_percent_allowed,
RunBatteryDischargeRoutineCallback callback) {
GetService()->RunBatteryDischargeRoutine(
length_seconds, maximum_discharge_percent_allowed,
base::BindOnce(
[](health::mojom::DiagnosticsService::
RunBatteryDischargeRoutineCallback callback,
cros_healthd::mojom::RunRoutineResponsePtr ptr) {
std::move(callback).Run(converters::ConvertPtr(std::move(ptr)));
},
std::move(callback)));
}
} // namespace chromeos
......@@ -66,6 +66,10 @@ class DiagnosticsService : public health::mojom::DiagnosticsService {
void RunPrimeSearchRoutine(uint32_t length_seconds,
uint64_t max_num,
RunPrimeSearchRoutineCallback callback) override;
void RunBatteryDischargeRoutine(
uint32_t length_seconds,
uint32_t maximum_discharge_percent_allowed,
RunBatteryDischargeRoutineCallback callback) override;
// Pointer to real implementation.
mojo::Remote<cros_healthd::mojom::CrosHealthdDiagnosticsService> service_;
......
......@@ -201,6 +201,24 @@ interface DiagnosticsService {
// routine.
RunPrimeSearchRoutine(uint32 length_seconds, uint64 max_num)
=> (RunRoutineResponse response);
// Requests that the BatteryDischarge routine is created and started on the
// platform. This routine checks the battery's discharge rate over a period of
// time. This routine is only available if GetAvailableRoutines returned
// kBatteryDischarge.
//
// The request:
// * |length_seconds| - length of time to run the routine for.
// * |maximum_discharge_percent_allowed| - the routine will fail if the
// battery discharges by more than
// this percentage.
//
// The response:
// * |response| - contains a unique identifier and status for the created
// routine.
RunBatteryDischargeRoutine(uint32 length_seconds,
uint32 maximum_discharge_percent_allowed)
=> (RunRoutineResponse response);
};
// Enumeration of each of the diagnostics routines the platform may support.
......
......@@ -37,6 +37,8 @@ dpsl_internal.Message = {
'DiagnosticsService.RunNvmeSelfTestRoutine',
DIAGNOSTICS_RUN_PRIME_SEARCH_ROUTINE:
'DiagnosticsService.RunPrimeSearchRoutine',
DIAGNOSTICS_RUN_BATTERY_DISCHARGE_ROUTINE:
'DiagnosticsService.RunBatteryDischargeRoutine',
PROBE_TELEMETRY_INFO: 'ProbeService.ProbeTelemetryInfo',
};
......@@ -149,6 +151,15 @@ dpsl_internal.DiagnosticsRunNvmeSelfTestRoutineRequest;
*/
dpsl_internal.DiagnosticsRunPrimeSearchRoutineRequest;
/**
* Request message sent by the unprivileged context to the privileged
* context to run battery discharge routine.
* @typedef {{
* lengthSeconds: !number,
* maximumDischargePercentAllowed: !number}}
*/
dpsl_internal.DiagnosticsRunBatteryDischargeRoutineRequest;
/**
* Response message sent by the privileged context containing routine
* information.
......
......@@ -490,6 +490,22 @@ class DiagnosticsProxy {
return await getOrCreateDiagnosticsService().runPrimeSearchRoutine(
request.lengthSeconds, request.maximumNumber);
};
/**
* Runs battery discharge routine.
* @param { !Object } message
* @return { !RunRoutineResponsePromise }
*/
async handleRunBatteryDischargeRoutine(message) {
const request =
/**
@type {!dpsl_internal.DiagnosticsRunBatteryDischargeRoutineRequest}
*/
(message);
this.assertNumberIsPositive(request.lengthSeconds);
return await getOrCreateDiagnosticsService().runBatteryDischargeRoutine(
request.lengthSeconds, request.maximumDischargePercentAllowed);
};
};
const diagnosticsProxy = new DiagnosticsProxy();
......@@ -830,6 +846,12 @@ untrustedMessagePipe.registerHandler(
(message) => diagnosticsProxy.handleRunPrimeSearchRoutine(message),
message));
untrustedMessagePipe.registerHandler(
dpsl_internal.Message.DIAGNOSTICS_RUN_BATTERY_DISCHARGE_ROUTINE,
(message) => diagnosticsProxy.genericRunRoutineHandler(
(message) => diagnosticsProxy.handleRunBatteryDischargeRoutine(message),
message));
untrustedMessagePipe.registerHandler(
dpsl_internal.Message.PROBE_TELEMETRY_INFO,
(message) => telemetryProxy.handleProbeTelemetryInfo(message));
......@@ -289,6 +289,33 @@ chromeos.test_support = {};
}
return response;
}
/**
* Requests battery discharge routine to be run.
* @param { !number } lengthSeconds
* @param { !number } maximumDischargePercentAllowed
* @return { !Promise<!Object> }
* @public
*/
async runBatteryDischargeRoutine(
lengthSeconds, maximumDischargePercentAllowed) {
const message =
/**
@type {!dpsl_internal.DiagnosticsRunBatteryDischargeRoutineRequest}
*/
({
lengthSeconds: lengthSeconds,
maximumDischargePercentAllowed: maximumDischargePercentAllowed
});
const response =
/** @type {!Object} */ (await messagePipe.sendMessage(
dpsl_internal.Message.DIAGNOSTICS_RUN_BATTERY_DISCHARGE_ROUTINE,
message));
if (response instanceof Error) {
throw response;
}
return response;
}
};
/**
......
......@@ -393,6 +393,8 @@ const untrustedTests = [
['UntrustedDiagnosticsRequestRunNvmeSelfTestRoutine'],
['UntrustedDiagnosticsRequestRunPrimeSearchRoutineInvalidInput'],
['UntrustedDiagnosticsRequestRunPrimeSearchRoutine'],
['UntrustedDiagnosticsRequestRunBatteryDischargeRoutineInvalidInput'],
['UntrustedDiagnosticsRequestRunBatteryDischargeRoutine'],
['UntrustedRequestTelemetryInfoUnknownCategory'],
['UntrustedRequestTelemetryInfo'],
[
......
......@@ -328,6 +328,40 @@ UNTRUSTED_TEST('UntrustedDiagnosticsRequestRunPrimeSearchRoutine', async () => {
assertDeepEquals(response, {id: 123456789, status: 'ready'});
});
// Tests that runBatteryDischargeRoutine throws the correct error when invalid
// enum is passed as input.
UNTRUSTED_TEST(
'UntrustedDiagnosticsRequestRunBatteryDischargeRoutineInvalidInput',
async () => {
let caughtError1;
try {
await chromeos.diagnostics.runBatteryDischargeRoutine(0, 10);
} catch (error) {
caughtError1 = error;
}
assertEquals(caughtError1.name, 'RangeError');
assertEquals(caughtError1.message, `Parameter must be positive.`);
let caughtError2;
try {
await chromeos.diagnostics.runBatteryDischargeRoutine(-2147483648, 5);
} catch (error) {
caughtError2 = error;
}
assertEquals(caughtError2.name, 'RangeError');
assertEquals(caughtError2.message, `Parameter must be positive.`);
});
// Tests that runBatteryDischargeRoutine returns the correct Object.
UNTRUSTED_TEST(
'UntrustedDiagnosticsRequestRunBatteryDischargeRoutine', async () => {
const response =
await chromeos.diagnostics.runBatteryDischargeRoutine(12, 2);
assertDeepEquals(response, {id: 123456789, status: 'ready'});
});
// Tests that TelemetryInfo can be successfully requested from
// from chrome-untrusted://.
UNTRUSTED_TEST('UntrustedRequestTelemetryInfo', async () => {
......
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