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
17 #define LOG_TAG "LinearFakeValueGenerator"
18
19 #include <log/log.h>
20 #include <vhal_v2_0/VehicleUtils.h>
21
22 #include "LinearFakeValueGenerator.h"
23
24 namespace android {
25 namespace hardware {
26 namespace automotive {
27 namespace vehicle {
28 namespace V2_0 {
29
30 namespace impl {
31
LinearFakeValueGenerator(const VehiclePropValue & request)32 LinearFakeValueGenerator::LinearFakeValueGenerator(const VehiclePropValue& request) {
33 const auto& v = request.value;
34 mGenCfg = GeneratorCfg{
35 .propId = v.int32Values[1],
36 .initialValue = v.floatValues[0],
37 .currentValue = v.floatValues[0],
38 .dispersion = v.floatValues[1],
39 .increment = v.floatValues[2],
40 .interval = Nanos(v.int64Values[0]),
41 };
42 }
43
nextEvent()44 VehiclePropValue LinearFakeValueGenerator::nextEvent() {
45 mGenCfg.currentValue += mGenCfg.increment;
46 if (mGenCfg.currentValue > mGenCfg.initialValue + mGenCfg.dispersion) {
47 mGenCfg.currentValue = mGenCfg.initialValue - mGenCfg.dispersion;
48 }
49 VehiclePropValue event = {.prop = mGenCfg.propId};
50 auto& value = event.value;
51 switch (getPropType(event.prop)) {
52 case VehiclePropertyType::INT32:
53 value.int32Values.resize(1);
54 value.int32Values[0] = static_cast<int32_t>(mGenCfg.currentValue);
55 break;
56 case VehiclePropertyType::INT64:
57 value.int64Values.resize(1);
58 value.int64Values[0] = static_cast<int64_t>(mGenCfg.currentValue);
59 break;
60 case VehiclePropertyType::FLOAT:
61 value.floatValues.resize(1);
62 value.floatValues[0] = mGenCfg.currentValue;
63 break;
64 default:
65 ALOGE("%s: unsupported property type for 0x%x", __func__, event.prop);
66 }
67 TimePoint eventTime = Clock::now() + mGenCfg.interval;
68 event.timestamp = eventTime.time_since_epoch().count();
69 return event;
70 }
71
hasNext()72 bool LinearFakeValueGenerator::hasNext() {
73 return true;
74 }
75
76 } // namespace impl
77
78 } // namespace V2_0
79 } // namespace vehicle
80 } // namespace automotive
81 } // namespace hardware
82 } // namespace android
83