diff --git a/services/core/java/com/android/server/power/ThermalManagerService.java b/services/core/java/com/android/server/power/ThermalManagerService.java index 6b2c6e30efe75..514caf25dcd0d 100644 --- a/services/core/java/com/android/server/power/ThermalManagerService.java +++ b/services/core/java/com/android/server/power/ThermalManagerService.java @@ -746,6 +746,8 @@ public class ThermalManagerService extends SystemService { } ret.add(new Temperature(t.value, t.type, t.name, t.throttlingStatus)); } + } catch (IllegalArgumentException | IllegalStateException e) { + Slog.e(TAG, "Couldn't getCurrentCoolingDevices due to invalid status", e); } catch (RemoteException e) { Slog.e(TAG, "Couldn't getCurrentTemperatures, reconnecting", e); connectToHal(); @@ -776,6 +778,8 @@ public class ThermalManagerService extends SystemService { } ret.add(new CoolingDevice(t.value, t.type, t.name)); } + } catch (IllegalArgumentException | IllegalStateException e) { + Slog.e(TAG, "Couldn't getCurrentCoolingDevices due to invalid status", e); } catch (RemoteException e) { Slog.e(TAG, "Couldn't getCurrentCoolingDevices, reconnecting", e); connectToHal(); @@ -799,6 +803,8 @@ public class ThermalManagerService extends SystemService { return Arrays.stream(halRet).filter(t -> t.type == type).collect( Collectors.toList()); + } catch (IllegalArgumentException | IllegalStateException e) { + Slog.e(TAG, "Couldn't getTemperatureThresholds due to invalid status", e); } catch (RemoteException e) { Slog.e(TAG, "Couldn't getTemperatureThresholds, reconnecting...", e); connectToHal(); @@ -824,15 +830,30 @@ public class ThermalManagerService extends SystemService { mInstance = IThermal.Stub.asInterface(binder); try { binder.linkToDeath(this, 0); - mInstance.registerThermalChangedCallback(mThermalChangedCallback); } catch (RemoteException e) { Slog.e(TAG, "Unable to connect IThermal AIDL instance", e); mInstance = null; } + if (mInstance != null) { + registerThermalChangedCallback(); + } } } } + @VisibleForTesting + void registerThermalChangedCallback() { + try { + mInstance.registerThermalChangedCallback(mThermalChangedCallback); + } catch (IllegalArgumentException | IllegalStateException e) { + Slog.e(TAG, "Couldn't registerThermalChangedCallback due to invalid status", + e); + } catch (RemoteException e) { + Slog.e(TAG, "Unable to connect IThermal AIDL instance", e); + mInstance = null; + } + } + @Override protected void dump(PrintWriter pw, String prefix) { synchronized (mHalLock) { diff --git a/services/tests/mockingservicestests/src/com/android/server/power/ThermalManagerServiceMockingTest.java b/services/tests/mockingservicestests/src/com/android/server/power/ThermalManagerServiceMockingTest.java index 34b17c7901b3b..c85ed26976c42 100644 --- a/services/tests/mockingservicestests/src/com/android/server/power/ThermalManagerServiceMockingTest.java +++ b/services/tests/mockingservicestests/src/com/android/server/power/ThermalManagerServiceMockingTest.java @@ -18,6 +18,7 @@ package com.android.server.power; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import android.hardware.thermal.CoolingType; @@ -83,6 +84,42 @@ public class ThermalManagerServiceMockingTest { assertEquals(halT.throttlingStatus, temperature.getStatus()); } + @Test + public void setCallback_illegalState_aidl() throws Exception { + Mockito.doThrow(new IllegalStateException()).when( + mAidlHalMock).registerThermalChangedCallback(Mockito.any()); + verifyWrapperStatusOnCallbackError(); + } + + @Test + public void setCallback_illegalArgument_aidl() throws Exception { + Mockito.doThrow(new IllegalStateException()).when( + mAidlHalMock).registerThermalChangedCallback(Mockito.any()); + verifyWrapperStatusOnCallbackError(); + } + + + void verifyWrapperStatusOnCallbackError() throws RemoteException { + android.hardware.thermal.Temperature halT1 = new android.hardware.thermal.Temperature(); + halT1.type = TemperatureType.MODEM; + halT1.name = "test1"; + Mockito.when(mAidlHalMock.getTemperaturesWithType(Mockito.anyInt())).thenReturn( + new android.hardware.thermal.Temperature[]{ + halT1 + }); + List ret = mAidlWrapper.getCurrentTemperatures(true, TemperatureType.MODEM); + Mockito.verify(mAidlHalMock, Mockito.times(1)).getTemperaturesWithType( + TemperatureType.MODEM); + assertNotNull(ret); + Temperature expectedT1 = new Temperature(halT1.value, halT1.type, halT1.name, + halT1.throttlingStatus); + List expectedRet = List.of(expectedT1); + // test that even if the callback fails to register without hal connection error, the + // wrapper should still work + assertTrue("Got temperature list as " + ret + " with different values compared to " + + expectedRet, expectedRet.containsAll(ret)); + } + @Test public void getCurrentTemperatures_withFilter_aidl() throws RemoteException { android.hardware.thermal.Temperature halT1 = new android.hardware.thermal.Temperature(); @@ -135,7 +172,42 @@ public class ThermalManagerServiceMockingTest { List expectedRet = List.of( new Temperature(halTInvalid.value, halTInvalid.type, halTInvalid.name, ThrottlingSeverity.NONE)); - assertEquals(expectedRet, ret); + assertTrue("Got temperature list as " + ret + " with different values compared to " + + expectedRet, expectedRet.containsAll(ret)); + } + + @Test + public void getCurrentTemperatures_illegalArgument_aidl() throws RemoteException { + Mockito.when(mAidlHalMock.getTemperatures()).thenThrow(new IllegalArgumentException()); + List ret = mAidlWrapper.getCurrentTemperatures(false, 0); + Mockito.verify(mAidlHalMock, Mockito.times(1)).getTemperatures(); + assertNotNull(ret); + assertEquals(0, ret.size()); + + Mockito.when(mAidlHalMock.getTemperaturesWithType(TemperatureType.MODEM)).thenThrow( + new IllegalArgumentException()); + ret = mAidlWrapper.getCurrentTemperatures(true, TemperatureType.MODEM); + Mockito.verify(mAidlHalMock, Mockito.times(1)).getTemperaturesWithType( + TemperatureType.MODEM); + assertNotNull(ret); + assertEquals(0, ret.size()); + } + + @Test + public void getCurrentTemperatures_illegalState_aidl() throws RemoteException { + Mockito.when(mAidlHalMock.getTemperatures()).thenThrow(new IllegalStateException()); + List ret = mAidlWrapper.getCurrentTemperatures(false, 0); + Mockito.verify(mAidlHalMock, Mockito.times(1)).getTemperatures(); + assertNotNull(ret); + assertEquals(0, ret.size()); + + Mockito.when(mAidlHalMock.getTemperaturesWithType(TemperatureType.MODEM)).thenThrow( + new IllegalStateException()); + ret = mAidlWrapper.getCurrentTemperatures(true, TemperatureType.MODEM); + Mockito.verify(mAidlHalMock, Mockito.times(1)).getTemperaturesWithType( + TemperatureType.MODEM); + assertNotNull(ret); + assertEquals(0, ret.size()); } @Test @@ -186,6 +258,40 @@ public class ThermalManagerServiceMockingTest { assertTrue("Got cooling device list as " + ret + ", expecting empty list", ret.isEmpty()); } + @Test + public void getCurrentCoolingDevices_illegalArgument_aidl() throws RemoteException { + Mockito.when(mAidlHalMock.getCoolingDevices()).thenThrow(new IllegalArgumentException()); + List ret = mAidlWrapper.getCurrentCoolingDevices(false, 0); + Mockito.verify(mAidlHalMock, Mockito.times(1)).getCoolingDevices(); + assertNotNull(ret); + assertEquals(0, ret.size()); + + Mockito.when(mAidlHalMock.getCoolingDevicesWithType(Mockito.anyInt())).thenThrow( + new IllegalArgumentException()); + ret = mAidlWrapper.getCurrentCoolingDevices(true, CoolingType.SPEAKER); + Mockito.verify(mAidlHalMock, Mockito.times(1)).getCoolingDevicesWithType( + CoolingType.SPEAKER); + assertNotNull(ret); + assertEquals(0, ret.size()); + } + + @Test + public void getCurrentCoolingDevices_illegalState_aidl() throws RemoteException { + Mockito.when(mAidlHalMock.getCoolingDevices()).thenThrow(new IllegalStateException()); + List ret = mAidlWrapper.getCurrentCoolingDevices(false, 0); + Mockito.verify(mAidlHalMock, Mockito.times(1)).getCoolingDevices(); + assertNotNull(ret); + assertEquals(0, ret.size()); + + Mockito.when(mAidlHalMock.getCoolingDevicesWithType(Mockito.anyInt())).thenThrow( + new IllegalStateException()); + ret = mAidlWrapper.getCurrentCoolingDevices(true, CoolingType.SPEAKER); + Mockito.verify(mAidlHalMock, Mockito.times(1)).getCoolingDevicesWithType( + CoolingType.SPEAKER); + assertNotNull(ret); + assertEquals(0, ret.size()); + } + @Test public void getTemperatureThresholds_withFilter_aidl() throws RemoteException { TemperatureThreshold halT1 = new TemperatureThreshold(); @@ -215,4 +321,44 @@ public class ThermalManagerServiceMockingTest { assertArrayEquals(halT1.hotThrottlingThresholds, threshold.hotThrottlingThresholds, 0.1f); assertArrayEquals(halT1.coldThrottlingThresholds, threshold.coldThrottlingThresholds, 0.1f); } + + @Test + public void getTemperatureThresholds_illegalArgument_aidl() throws RemoteException { + Mockito.when(mAidlHalMock.getTemperatureThresholdsWithType(Mockito.anyInt())).thenThrow( + new IllegalArgumentException()); + List ret = mAidlWrapper.getTemperatureThresholds(true, + Temperature.TYPE_SOC); + Mockito.verify(mAidlHalMock, Mockito.times(1)).getTemperatureThresholdsWithType( + Temperature.TYPE_SOC); + assertNotNull(ret); + assertEquals(0, ret.size()); + + Mockito.when(mAidlHalMock.getTemperatureThresholds()).thenThrow( + new IllegalArgumentException()); + ret = mAidlWrapper.getTemperatureThresholds(false, + Temperature.TYPE_SOC); + Mockito.verify(mAidlHalMock, Mockito.times(1)).getTemperatureThresholds(); + assertNotNull(ret); + assertEquals(0, ret.size()); + } + + @Test + public void getTemperatureThresholds_illegalState_aidl() throws RemoteException { + Mockito.when(mAidlHalMock.getTemperatureThresholdsWithType(Mockito.anyInt())).thenThrow( + new IllegalStateException()); + List ret = mAidlWrapper.getTemperatureThresholds(true, + Temperature.TYPE_SOC); + Mockito.verify(mAidlHalMock, Mockito.times(1)).getTemperatureThresholdsWithType( + Temperature.TYPE_SOC); + assertNotNull(ret); + assertEquals(0, ret.size()); + + Mockito.when(mAidlHalMock.getTemperatureThresholds()).thenThrow( + new IllegalStateException()); + ret = mAidlWrapper.getTemperatureThresholds(false, + Temperature.TYPE_SOC); + Mockito.verify(mAidlHalMock, Mockito.times(1)).getTemperatureThresholds(); + assertNotNull(ret); + assertEquals(0, ret.size()); + } }