Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit fd769f99 authored by beiyifb's avatar beiyifb
Browse files

Thermalmanagerservice: Add shell interface to inject temperatures

Add a new shellcommand to thermalservice that allows injecting temperature samples into the queue, identically to how the HAL callbacks would do this, which triggers the same shutdown flow that would normally be used.

Test: 1) verify that injecting shutdown events triggers the alert service and shuts off the headset:
adb shell cmd thermalservice inject-temperature CPU SHUTDOWN cpu-0-2-usr 95.0
adb shell cmd thermalservice inject-temperature BATTERY SHUTDOWN batt-usr 53.0
2) verify that the injected temperature is in the cached temperature list (adb shell dumpsys thermalservice, after the command above):
Cached temperatures: Temperature{mValue=53.0, mType=3, mName=batt-usr, mStatus=6}
3) verify that invalid shell commands return errors:
adb shell cmd thermalservice inject-temperature stuff
adb shell cmd thermalservice inject-temperature battery stuff
Invalid temperature type: stuff
Invalid throttle status: stuff

Change-Id: I95e4064fe753095edd0fa2e87e337c3d2d3d9556
parent c80d564f
Loading
Loading
Loading
Loading
+94 −0
Original line number Diff line number Diff line
@@ -547,6 +547,8 @@ public class ThermalManagerService extends SystemService {
        @Override
        public int onCommand(String cmd) {
            switch(cmd != null ? cmd : "") {
                case "inject-temperature":
                    return runInjectTemperature();
                case "override-status":
                    return runOverrideStatus();
                case "reset":
@@ -569,6 +571,95 @@ public class ThermalManagerService extends SystemService {
            }
        }


        private int runInjectTemperature() {
            final long token = Binder.clearCallingIdentity();
            try {
                final PrintWriter pw = getOutPrintWriter();
                int type;
                String typeName = getNextArgRequired();
                switch (typeName.toUpperCase()) {
                    case "UNKNOWN":
                        type = Temperature.TYPE_UNKNOWN;
                        break;
                    case "CPU":
                        type = Temperature.TYPE_CPU;
                        break;
                    case "GPU":
                        type = Temperature.TYPE_GPU;
                        break;
                    case "BATTERY":
                        type = Temperature.TYPE_BATTERY;
                        break;
                    case "SKIN":
                        type = Temperature.TYPE_SKIN;
                        break;
                    case "USB_PORT":
                        type = Temperature.TYPE_USB_PORT;
                        break;
                    case "POWER_AMPLIFIER":
                        type = Temperature.TYPE_POWER_AMPLIFIER;
                        break;
                    case "BCL_VOLTAGE":
                        type = Temperature.TYPE_BCL_VOLTAGE;
                        break;
                    case "BCL_CURRENT":
                        type = Temperature.TYPE_BCL_CURRENT;
                        break;
                    case "BCL_PERCENTAGE":
                        type = Temperature.TYPE_BCL_PERCENTAGE;
                        break;
                    case "NPU":
                        type = Temperature.TYPE_NPU;
                        break;
                    default:
                        pw.println("Invalid temperature type: " + typeName);
                        return -1;
                }
                int throttle;
                String throttleName = getNextArgRequired();
                switch (throttleName.toUpperCase()) {
                    case "NONE":
                        throttle = Temperature.THROTTLING_NONE;
                        break;
                    case "LIGHT":
                        throttle = Temperature.THROTTLING_LIGHT;
                        break;
                    case "MODERATE":
                        throttle = Temperature.THROTTLING_MODERATE;
                        break;
                    case "SEVERE":
                        throttle = Temperature.THROTTLING_SEVERE;
                        break;
                    case "CRITICAL":
                        throttle = Temperature.THROTTLING_CRITICAL;
                        break;
                    case "EMERGENCY":
                        throttle = Temperature.THROTTLING_EMERGENCY;
                        break;
                    case "SHUTDOWN":
                        throttle = Temperature.THROTTLING_SHUTDOWN;
                        break;
                    default:
                        pw.println("Invalid throttle status: " + throttleName);
                        return -1;
                }
                String name = getNextArgRequired();
                float value = 28.0f;
                try {
                    String valueStr = getNextArg();
                    if (valueStr != null) value = Float.parseFloat(valueStr);
                } catch (RuntimeException ex) {
                    pw.println("Error: " + ex.toString());
                    return -1;
                }
                onTemperatureChanged(new Temperature(value, type, name, throttle), true);
                return 0;
            } finally {
                Binder.restoreCallingIdentity(token);
            }
        }

        private int runOverrideStatus() {
            final long token = Binder.clearCallingIdentity();
            try {
@@ -601,6 +692,9 @@ public class ThermalManagerService extends SystemService {
            pw.println("  help");
            pw.println("    Print this help text.");
            pw.println("");
            pw.println("  inject-temperature TYPE STATUS NAME [VALUE]");
            pw.println("    injects a new temperature sample for the specified device.");
            pw.println("    type and status strings follow the names in android.os.Temperature.");
            pw.println("  override-status STATUS");
            pw.println("    sets and locks the thermal status of the device to STATUS.");
            pw.println("    status code is defined in android.os.Temperature.");