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;
17 
18 import android.hardware.automotive.vehicle.V2_0.VehicleApPowerStateReq;
19 import android.util.Log;
20 
21 import com.android.car.hal.PowerHalService;
22 import com.android.car.hal.VehicleHal;
23 
24 import java.util.LinkedList;
25 
26 public class MockedPowerHalService extends PowerHalService {
27     private static final String TAG = MockedPowerHalService.class.getSimpleName();
28 
29     private final boolean mIsPowerStateSupported;
30     private final boolean mIsDeepSleepAllowed;
31     private final boolean mIsTimedWakeupAllowed;
32     private PowerState mCurrentPowerState = new PowerState(VehicleApPowerStateReq.ON, 0);
33     private PowerEventListener mListener;
34     private SignalListener mSignalListener;
35 
36     private final LinkedList<int[]> mSentStates = new LinkedList<>();
37 
38     interface SignalListener {
sendingSignal(int signal)39         void sendingSignal(int signal);
40     }
41 
MockedPowerHalService(boolean isPowerStateSupported, boolean isDeepSleepAllowed, boolean isTimedWakeupAllowed)42     public MockedPowerHalService(boolean isPowerStateSupported, boolean isDeepSleepAllowed,
43             boolean isTimedWakeupAllowed) {
44         super(new VehicleHal(null, null, null, null));
45         mIsPowerStateSupported = isPowerStateSupported;
46         mIsDeepSleepAllowed = isDeepSleepAllowed;
47         mIsTimedWakeupAllowed = isTimedWakeupAllowed;
48     }
49 
50     @Override
setListener(PowerEventListener listener)51     public synchronized void setListener(PowerEventListener listener) {
52         mListener = listener;
53     }
54 
55     // For testing purposes only
setSignalListener(SignalListener listener)56     public synchronized void setSignalListener(SignalListener listener) {
57         mSignalListener =  listener;
58     }
59 
60     @Override
sendWaitForVhal()61     public void sendWaitForVhal() {
62         Log.i(TAG, "sendBootComplete");
63         doSendState(SET_WAIT_FOR_VHAL, 0);
64     }
65 
66     @Override
sendSleepEntry(int wakeupTimeSec)67     public void sendSleepEntry(int wakeupTimeSec) {
68         Log.i(TAG, "sendSleepEntry");
69         doSendState(SET_DEEP_SLEEP_ENTRY, wakeupTimeSec);
70     }
71 
72     @Override
sendSleepExit()73     public void sendSleepExit() {
74         Log.i(TAG, "sendSleepExit");
75         doSendState(SET_DEEP_SLEEP_EXIT, 0);
76     }
77 
78     @Override
sendShutdownPostpone(int postponeTimeMs)79     public void sendShutdownPostpone(int postponeTimeMs) {
80         Log.i(TAG, "sendShutdownPostpone");
81         doSendState(SET_SHUTDOWN_POSTPONE, postponeTimeMs);
82     }
83 
84     @Override
sendShutdownStart(int wakeupTimeSec)85     public void sendShutdownStart(int wakeupTimeSec) {
86         Log.i(TAG, "sendShutdownStart");
87         doSendState(SET_SHUTDOWN_START, wakeupTimeSec);
88     }
89 
90     @Override
sendShutdownCancel()91     public void sendShutdownCancel() {
92         Log.i(TAG, "sendShutdownCancel");
93         doSendState(SET_SHUTDOWN_CANCELLED, 0);
94     }
95 
waitForSend(long timeoutMs)96     public synchronized int[] waitForSend(long timeoutMs) throws Exception {
97         if (mSentStates.size() == 0) {
98             wait(timeoutMs);
99         }
100         return mSentStates.removeFirst();
101     }
102 
doSendState(int state, int param)103     private synchronized void doSendState(int state, int param) {
104         SignalListener listener;
105         synchronized (this) {
106             listener = mSignalListener;
107         }
108         if (listener != null) {
109             listener.sendingSignal(state);
110         }
111         int[] toSend = new int[] {state, param};
112         mSentStates.addLast(toSend);
113         notifyAll();
114     }
115 
116     @Override
isPowerStateSupported()117     public boolean isPowerStateSupported() {
118         return mIsPowerStateSupported;
119     }
120 
121     @Override
isDeepSleepAllowed()122     public boolean isDeepSleepAllowed() {
123         return mIsDeepSleepAllowed;
124     }
125 
126     @Override
isTimedWakeupAllowed()127     public boolean isTimedWakeupAllowed() {
128         return mIsTimedWakeupAllowed;
129     }
130 
131     @Override
getCurrentPowerState()132     public synchronized PowerState getCurrentPowerState() {
133         return mCurrentPowerState;
134     }
135 
setCurrentPowerState(PowerState state)136     public void setCurrentPowerState(PowerState state) {
137         setCurrentPowerState(state, true);
138     }
139 
setCurrentPowerState(PowerState state, boolean notify)140     public void setCurrentPowerState(PowerState state, boolean notify) {
141         PowerEventListener listener;
142         synchronized (this) {
143             mCurrentPowerState = state;
144             listener = mListener;
145         }
146         if (listener != null && notify) {
147             listener.onApPowerStateChange(state);
148         }
149     }
150 }
151