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     // TODO: (chenhaosjtuacm) remove "{}" if AGL compiler updated
50     VehiclePropValue event = {.timestamp = {}, .areaId = {}, .prop = mGenCfg.propId};
51     auto& value = event.value;
52     switch (getPropType(event.prop)) {
53         case VehiclePropertyType::INT32:
54             value.int32Values.resize(1);
55             value.int32Values[0] = static_cast<int32_t>(mGenCfg.currentValue);
56             break;
57         case VehiclePropertyType::INT64:
58             value.int64Values.resize(1);
59             value.int64Values[0] = static_cast<int64_t>(mGenCfg.currentValue);
60             break;
61         case VehiclePropertyType::FLOAT:
62             value.floatValues.resize(1);
63             value.floatValues[0] = mGenCfg.currentValue;
64             break;
65         default:
66             ALOGE("%s: unsupported property type for 0x%x", __func__, event.prop);
67     }
68     TimePoint eventTime = Clock::now() + mGenCfg.interval;
69     event.timestamp = eventTime.time_since_epoch().count();
70     return event;
71 }
72 
hasNext()73 bool LinearFakeValueGenerator::hasNext() {
74     return true;
75 }
76 
77 }  // namespace impl
78 
79 }  // namespace V2_0
80 }  // namespace vehicle
81 }  // namespace automotive
82 }  // namespace hardware
83 }  // namespace android
84