• 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;
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 
waitForSend(long timeoutMs)90     public synchronized int[] waitForSend(long timeoutMs) throws Exception {
91         if (mSentStates.size() == 0) {
92             wait(timeoutMs);
93         }
94         return mSentStates.removeFirst();
95     }
96 
doSendState(int state, int param)97     private synchronized void doSendState(int state, int param) {
98         SignalListener listener;
99         synchronized (this) {
100             listener = mSignalListener;
101         }
102         if (listener != null) {
103             listener.sendingSignal(state);
104         }
105         int[] toSend = new int[] {state, param};
106         mSentStates.addLast(toSend);
107         notifyAll();
108     }
109 
110     @Override
isPowerStateSupported()111     public boolean isPowerStateSupported() {
112         return mIsPowerStateSupported;
113     }
114 
115     @Override
isDeepSleepAllowed()116     public boolean isDeepSleepAllowed() {
117         return mIsDeepSleepAllowed;
118     }
119 
120     @Override
isTimedWakeupAllowed()121     public boolean isTimedWakeupAllowed() {
122         return mIsTimedWakeupAllowed;
123     }
124 
125     @Override
getCurrentPowerState()126     public synchronized PowerState getCurrentPowerState() {
127         return mCurrentPowerState;
128     }
129 
setCurrentPowerState(PowerState state)130     public void setCurrentPowerState(PowerState state) {
131         setCurrentPowerState(state, true);
132     }
133 
setCurrentPowerState(PowerState state, boolean notify)134     public void setCurrentPowerState(PowerState state, boolean notify) {
135         PowerEventListener listener;
136         synchronized (this) {
137             mCurrentPowerState = state;
138             listener = mListener;
139         }
140         if (listener != null && notify) {
141             listener.onApPowerStateChange(state);
142         }
143     }
144 }
145