• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.android.car.hal;
17 
18 import static org.mockito.Mockito.mock;
19 import static org.mockito.Mockito.when;
20 
21 import android.content.Context;
22 import android.hardware.automotive.vehicle.VehicleApPowerStateReq;
23 import android.util.Log;
24 
25 import com.android.car.CarServiceUtils;
26 import com.android.car.VehicleStub;
27 import com.android.internal.annotations.GuardedBy;
28 
29 import java.util.LinkedList;
30 import java.util.concurrent.TimeoutException;
31 
32 public class MockedPowerHalService extends PowerHalService {
33     private static final String TAG = MockedPowerHalService.class.getSimpleName();
34 
35     private final boolean mIsPowerStateSupported;
36     private final boolean mIsTimedWakeupAllowed;
37 
38     private final Object mLock = new Object();
39 
40     @GuardedBy("mLock")
41     private PowerState mCurrentPowerState = new PowerState(VehicleApPowerStateReq.ON, 0);
42 
43     @GuardedBy("mLock")
44     private PowerEventListener mListener;
45 
46     @GuardedBy("mLock")
47     private SignalListener mSignalListener;
48 
49     @GuardedBy("mLock")
50     private final LinkedList<int[]> mSentStates = new LinkedList<>();
51 
52     private boolean mIsDeepSleepAllowed;
53     private boolean mIsHibernationAllowed;
54     @PowerState.ShutdownType
55     private int mRequestedShutdownPowerState = PowerState.SHUTDOWN_TYPE_UNDEFINED;
56 
57     public interface SignalListener {
sendingSignal(int signal)58         void sendingSignal(int signal);
59     }
60 
createVehicleHalWithMockedServices()61     private static VehicleHal createVehicleHalWithMockedServices() {
62         HalPropValueBuilder propValueBuilder = new HalPropValueBuilder(/*isAidl=*/true);
63         VehicleStub vehicleStub = mock(VehicleStub.class);
64         when(vehicleStub.getHalPropValueBuilder()).thenReturn(propValueBuilder);
65         VehicleHal mockedVehicleHal = new VehicleHal(
66                 mock(Context.class),
67                 mock(PowerHalService.class),
68                 mock(PropertyHalService.class),
69                 mock(InputHalService.class),
70                 mock(VmsHalService.class),
71                 mock(UserHalService.class),
72                 mock(DiagnosticHalService.class),
73                 mock(ClusterHalService.class),
74                 mock(TimeHalService.class),
75                 CarServiceUtils.getHandlerThread(VehicleHal.class.getSimpleName()),
76                 vehicleStub);
77 
78         return mockedVehicleHal;
79     }
80 
MockedPowerHalService(boolean isPowerStateSupported, boolean isDeepSleepAllowed, boolean isHibernationAllowed, boolean isTimedWakeupAllowed)81     public MockedPowerHalService(boolean isPowerStateSupported, boolean isDeepSleepAllowed,
82             boolean isHibernationAllowed, boolean isTimedWakeupAllowed) {
83         super(mock(Context.class), createVehicleHalWithMockedServices());
84         mIsPowerStateSupported = isPowerStateSupported;
85         mIsDeepSleepAllowed = isDeepSleepAllowed;
86         mIsHibernationAllowed = isHibernationAllowed;
87         mIsTimedWakeupAllowed = isTimedWakeupAllowed;
88     }
89 
90     @Override
setListener(PowerEventListener listener)91     public void setListener(PowerEventListener listener) {
92         synchronized (mLock) {
93             mListener = listener;
94         }
95     }
96 
97     // For testing purposes only
setSignalListener(SignalListener listener)98     public void setSignalListener(SignalListener listener) {
99         synchronized (mLock) {
100             mSignalListener = listener;
101         }
102     }
103 
104     @Override
sendOn()105     public void sendOn() {
106         Log.i(TAG, "sendOn");
107         doSendState(SET_ON, 0);
108     }
109 
110     @Override
sendWaitForVhal()111     public void sendWaitForVhal() {
112         Log.i(TAG, "sendWaitForVhal");
113         doSendState(SET_WAIT_FOR_VHAL, 0);
114     }
115 
116     @Override
sendSleepEntry(int wakeupTimeSec)117     public void sendSleepEntry(int wakeupTimeSec) {
118         Log.i(TAG, "sendSleepEntry");
119         doSendState(SET_DEEP_SLEEP_ENTRY, wakeupTimeSec);
120     }
121 
122     @Override
sendSleepExit()123     public void sendSleepExit() {
124         Log.i(TAG, "sendSleepExit");
125         doSendState(SET_DEEP_SLEEP_EXIT, 0);
126     }
127 
128     @Override
sendShutdownPostpone(int postponeTimeMs)129     public void sendShutdownPostpone(int postponeTimeMs) {
130         Log.i(TAG, "sendShutdownPostpone");
131         doSendState(SET_SHUTDOWN_POSTPONE, postponeTimeMs);
132     }
133 
134     @Override
sendShutdownStart(int wakeupTimeSec)135     public void sendShutdownStart(int wakeupTimeSec) {
136         Log.i(TAG, "sendShutdownStart");
137         doSendState(SET_SHUTDOWN_START, wakeupTimeSec);
138     }
139 
140     @Override
sendShutdownCancel()141     public void sendShutdownCancel() {
142         Log.i(TAG, "sendShutdownCancel");
143         doSendState(SET_SHUTDOWN_CANCELLED, 0);
144     }
145 
146     @Override
sendShutdownPrepare()147     public void sendShutdownPrepare() {
148         Log.i(TAG, "sendShutdownPrepare");
149         super.sendShutdownPrepare();
150     }
151 
152     @Override
sendHibernationEntry(int wakeupTimeSec)153     public void sendHibernationEntry(int wakeupTimeSec) {
154         Log.i(TAG, "sendHibernationEntry");
155         doSendState(SET_HIBERNATION_ENTRY, wakeupTimeSec);
156     }
157 
158     @Override
sendHibernationExit()159     public void sendHibernationExit() {
160         Log.i(TAG, "sendHibernationExit");
161         doSendState(SET_HIBERNATION_EXIT, 0);
162     }
163 
164     @Override
requestShutdownAp(@owerState.ShutdownType int powerState, boolean runGarageMode)165     public void requestShutdownAp(@PowerState.ShutdownType int powerState, boolean runGarageMode) {
166         mRequestedShutdownPowerState = powerState;
167     }
168 
getRequestedShutdownPowerState()169     public int getRequestedShutdownPowerState() {
170         return mRequestedShutdownPowerState;
171     }
172 
waitForSend(long timeoutMs)173     public int[] waitForSend(long timeoutMs) throws Exception {
174         long now = System.currentTimeMillis();
175         long deadline = now + timeoutMs;
176         synchronized (mLock) {
177             while (mSentStates.isEmpty() && now < deadline) {
178                 mLock.wait(deadline - now);
179                 now = System.currentTimeMillis();
180             }
181             if (mSentStates.isEmpty()) {
182                 throw new TimeoutException("mSentStates is still empty "
183                         + "(monitor was not notified in " + timeoutMs + " ms)");
184             }
185             return mSentStates.removeFirst();
186         }
187     }
188 
doSendState(int state, int param)189     private void doSendState(int state, int param) {
190         int[] toSend = new int[] {state, param};
191         SignalListener listener;
192         synchronized (mLock) {
193             listener = mSignalListener;
194             mSentStates.addLast(toSend);
195             mLock.notifyAll();
196         }
197         if (listener != null) {
198             listener.sendingSignal(state);
199         }
200     }
201 
202     @Override
isPowerStateSupported()203     public boolean isPowerStateSupported() {
204         return mIsPowerStateSupported;
205     }
206 
207     @Override
isDeepSleepAllowed()208     public boolean isDeepSleepAllowed() {
209         return mIsDeepSleepAllowed;
210     }
211 
212     @Override
isHibernationAllowed()213     public boolean isHibernationAllowed() {
214         return mIsHibernationAllowed;
215     }
216 
217     @Override
isTimedWakeupAllowed()218     public boolean isTimedWakeupAllowed() {
219         return mIsTimedWakeupAllowed;
220     }
221 
222     @Override
getCurrentPowerState()223     public PowerState getCurrentPowerState() {
224         synchronized (mLock) {
225             return mCurrentPowerState;
226         }
227     }
228 
setDeepSleepEnabled(boolean enabled)229     public void setDeepSleepEnabled(boolean enabled) {
230         mIsDeepSleepAllowed = enabled;
231     }
232 
setHibernationEnabled(boolean enabled)233     public void setHibernationEnabled(boolean enabled) {
234         mIsHibernationAllowed = enabled;
235     }
236 
setCurrentPowerState(PowerState state)237     public void setCurrentPowerState(PowerState state) {
238         setCurrentPowerState(state, true);
239     }
240 
setCurrentPowerState(PowerState state, boolean notify)241     public void setCurrentPowerState(PowerState state, boolean notify) {
242         PowerEventListener listener;
243         synchronized (mLock) {
244             mCurrentPowerState = state;
245             listener = mListener;
246         }
247         if (listener != null && notify) {
248             listener.onApPowerStateChange(state);
249         }
250     }
251 }
252