1 /* 2 * Copyright (C) 2018 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.vehiclehal.test; 17 18 import static org.junit.Assert.assertEquals; 19 20 import android.hardware.automotive.vehicle.V2_0.IVehicle; 21 import android.hardware.automotive.vehicle.V2_0.StatusCode; 22 import android.hardware.automotive.vehicle.V2_0.VehiclePropValue; 23 import android.os.RemoteException; 24 25 import com.android.car.vehiclehal.VehiclePropValueBuilder; 26 27 import java.time.Duration; 28 29 class LinearVhalEventGenerator implements VhalEventGenerator { 30 31 private final IVehicle mVehicle; 32 33 private int mProp; 34 private Duration mInterval; 35 private float mInitialValue; 36 private float mDispersion; 37 private float mIncrement; 38 39 LinearVhalEventGenerator(IVehicle vehicle) { 40 mVehicle = vehicle; 41 reset(); 42 } 43 44 LinearVhalEventGenerator reset() { 45 mProp = 0; 46 mInterval = Duration.ofSeconds(1); 47 mInitialValue = 1000; 48 mDispersion = 0; 49 mInitialValue = 0; 50 return this; 51 } 52 53 LinearVhalEventGenerator setIntervalMs(long intervalMs) { 54 mInterval = Duration.ofMillis(intervalMs); 55 return this; 56 } 57 58 LinearVhalEventGenerator setInitialValue(float initialValue) { 59 mInitialValue = initialValue; 60 return this; 61 } 62 63 LinearVhalEventGenerator setDispersion(float dispersion) { 64 mDispersion = dispersion; 65 return this; 66 } 67 68 LinearVhalEventGenerator setIncrement(float increment) { 69 mIncrement = increment; 70 return this; 71 } 72 73 LinearVhalEventGenerator setProp(int prop) { 74 mProp = prop; 75 return this; 76 } 77 78 @Override 79 public void start() throws RemoteException { 80 VehiclePropValue request = 81 VehiclePropValueBuilder.newBuilder(GENERATE_FAKE_DATA_CONTROLLING_PROPERTY) 82 .addIntValue(CMD_START_LINEAR, mProp) 83 .setInt64Value(mInterval.toNanos()) 84 .addFloatValue(mInitialValue, mDispersion, mIncrement) 85 .build(); 86 assertEquals(StatusCode.OK, mVehicle.set(request)); 87 } 88 89 @Override 90 public void stop() throws RemoteException { 91 VehiclePropValue request = 92 VehiclePropValueBuilder.newBuilder(GENERATE_FAKE_DATA_CONTROLLING_PROPERTY) 93 .addIntValue(CMD_STOP_LINEAR, mProp) 94 .build(); 95 assertEquals(StatusCode.OK, mVehicle.set(request)); 96 } 97 } 98