1 /*
2 * Copyright (C) 2021 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 #include "FakeObd2Frame.h"
18 #include "Obd2SensorStore.h"
19
20 #include <PropertyUtils.h>
21 #include <VehicleHalTypes.h>
22 #include <VehiclePropertyStore.h>
23 #include <VehicleUtils.h>
24
25 #include <android-base/result.h>
26 #include <utils/Log.h>
27
28 namespace android {
29 namespace hardware {
30 namespace automotive {
31 namespace vehicle {
32 namespace fake {
33 namespace obd2frame {
34
35 using ::aidl::android::hardware::automotive::vehicle::DiagnosticFloatSensorIndex;
36 using ::aidl::android::hardware::automotive::vehicle::DiagnosticIntegerSensorIndex;
37 using ::aidl::android::hardware::automotive::vehicle::Obd2CommonIgnitionMonitors;
38 using ::aidl::android::hardware::automotive::vehicle::Obd2FuelSystemStatus;
39 using ::aidl::android::hardware::automotive::vehicle::Obd2FuelType;
40 using ::aidl::android::hardware::automotive::vehicle::Obd2IgnitionMonitorKind;
41 using ::aidl::android::hardware::automotive::vehicle::Obd2SecondaryAirStatus;
42 using ::aidl::android::hardware::automotive::vehicle::Obd2SparkIgnitionMonitors;
43 using ::aidl::android::hardware::automotive::vehicle::StatusCode;
44 using ::aidl::android::hardware::automotive::vehicle::VehiclePropConfig;
45 using ::aidl::android::hardware::automotive::vehicle::VehiclePropertyType;
46 using ::aidl::android::hardware::automotive::vehicle::VehiclePropValue;
47 using ::android::base::Result;
48
fillDefaultObd2Frame(size_t numVendorIntegerSensors,size_t numVendorFloatSensors)49 std::unique_ptr<Obd2SensorStore> FakeObd2Frame::fillDefaultObd2Frame(size_t numVendorIntegerSensors,
50 size_t numVendorFloatSensors) {
51 std::unique_ptr<Obd2SensorStore> sensorStore(new Obd2SensorStore(
52 mPropStore->getValuePool(), numVendorIntegerSensors, numVendorFloatSensors));
53
54 sensorStore->setIntegerSensor(DiagnosticIntegerSensorIndex::FUEL_SYSTEM_STATUS,
55 toInt(Obd2FuelSystemStatus::CLOSED_LOOP));
56 sensorStore->setIntegerSensor(DiagnosticIntegerSensorIndex::MALFUNCTION_INDICATOR_LIGHT_ON, 0);
57 sensorStore->setIntegerSensor(DiagnosticIntegerSensorIndex::IGNITION_MONITORS_SUPPORTED,
58 toInt(Obd2IgnitionMonitorKind::SPARK));
59 sensorStore->setIntegerSensor(
60 DiagnosticIntegerSensorIndex::IGNITION_SPECIFIC_MONITORS,
61 toInt(Obd2CommonIgnitionMonitors::COMPONENTS_AVAILABLE) |
62 toInt(Obd2CommonIgnitionMonitors::MISFIRE_AVAILABLE) |
63 toInt(Obd2SparkIgnitionMonitors::AC_REFRIGERANT_AVAILABLE) |
64 toInt(Obd2SparkIgnitionMonitors::EVAPORATIVE_SYSTEM_AVAILABLE));
65 sensorStore->setIntegerSensor(DiagnosticIntegerSensorIndex::INTAKE_AIR_TEMPERATURE, 35);
66 sensorStore->setIntegerSensor(DiagnosticIntegerSensorIndex::COMMANDED_SECONDARY_AIR_STATUS,
67 toInt(Obd2SecondaryAirStatus::FROM_OUTSIDE_OR_OFF));
68 sensorStore->setIntegerSensor(DiagnosticIntegerSensorIndex::NUM_OXYGEN_SENSORS_PRESENT, 1);
69 sensorStore->setIntegerSensor(DiagnosticIntegerSensorIndex::RUNTIME_SINCE_ENGINE_START, 500);
70 sensorStore->setIntegerSensor(
71 DiagnosticIntegerSensorIndex::DISTANCE_TRAVELED_WITH_MALFUNCTION_INDICATOR_LIGHT_ON, 0);
72 sensorStore->setIntegerSensor(DiagnosticIntegerSensorIndex::WARMUPS_SINCE_CODES_CLEARED, 51);
73 sensorStore->setIntegerSensor(
74 DiagnosticIntegerSensorIndex::DISTANCE_TRAVELED_SINCE_CODES_CLEARED, 365);
75 sensorStore->setIntegerSensor(DiagnosticIntegerSensorIndex::ABSOLUTE_BAROMETRIC_PRESSURE, 30);
76 sensorStore->setIntegerSensor(DiagnosticIntegerSensorIndex::CONTROL_MODULE_VOLTAGE, 12);
77 sensorStore->setIntegerSensor(DiagnosticIntegerSensorIndex::AMBIENT_AIR_TEMPERATURE, 18);
78 sensorStore->setIntegerSensor(DiagnosticIntegerSensorIndex::MAX_FUEL_AIR_EQUIVALENCE_RATIO, 1);
79 sensorStore->setIntegerSensor(DiagnosticIntegerSensorIndex::FUEL_TYPE,
80 toInt(Obd2FuelType::GASOLINE));
81 sensorStore->setFloatSensor(DiagnosticFloatSensorIndex::CALCULATED_ENGINE_LOAD, 0.153);
82 sensorStore->setFloatSensor(DiagnosticFloatSensorIndex::SHORT_TERM_FUEL_TRIM_BANK1, -0.16);
83 sensorStore->setFloatSensor(DiagnosticFloatSensorIndex::LONG_TERM_FUEL_TRIM_BANK1, -0.16);
84 sensorStore->setFloatSensor(DiagnosticFloatSensorIndex::SHORT_TERM_FUEL_TRIM_BANK2, -0.16);
85 sensorStore->setFloatSensor(DiagnosticFloatSensorIndex::LONG_TERM_FUEL_TRIM_BANK2, -0.16);
86 sensorStore->setFloatSensor(DiagnosticFloatSensorIndex::INTAKE_MANIFOLD_ABSOLUTE_PRESSURE, 7.5);
87 sensorStore->setFloatSensor(DiagnosticFloatSensorIndex::ENGINE_RPM, 1250.);
88 sensorStore->setFloatSensor(DiagnosticFloatSensorIndex::VEHICLE_SPEED, 40.);
89 sensorStore->setFloatSensor(DiagnosticFloatSensorIndex::TIMING_ADVANCE, 2.5);
90 sensorStore->setFloatSensor(DiagnosticFloatSensorIndex::THROTTLE_POSITION, 19.75);
91 sensorStore->setFloatSensor(DiagnosticFloatSensorIndex::OXYGEN_SENSOR1_VOLTAGE, 0.265);
92 sensorStore->setFloatSensor(DiagnosticFloatSensorIndex::FUEL_TANK_LEVEL_INPUT, 0.824);
93 sensorStore->setFloatSensor(DiagnosticFloatSensorIndex::EVAPORATION_SYSTEM_VAPOR_PRESSURE,
94 -0.373);
95 sensorStore->setFloatSensor(DiagnosticFloatSensorIndex::CATALYST_TEMPERATURE_BANK1_SENSOR1,
96 190.);
97 sensorStore->setFloatSensor(DiagnosticFloatSensorIndex::RELATIVE_THROTTLE_POSITION, 3.);
98 sensorStore->setFloatSensor(DiagnosticFloatSensorIndex::ABSOLUTE_THROTTLE_POSITION_B, 0.306);
99 sensorStore->setFloatSensor(DiagnosticFloatSensorIndex::ACCELERATOR_PEDAL_POSITION_D, 0.188);
100 sensorStore->setFloatSensor(DiagnosticFloatSensorIndex::ACCELERATOR_PEDAL_POSITION_E, 0.094);
101 sensorStore->setFloatSensor(DiagnosticFloatSensorIndex::COMMANDED_THROTTLE_ACTUATOR, 0.024);
102
103 return sensorStore;
104 }
105
initObd2LiveFrame(const VehiclePropConfig & propConfig)106 void FakeObd2Frame::initObd2LiveFrame(const VehiclePropConfig& propConfig) {
107 auto sensorStore = fillDefaultObd2Frame(static_cast<size_t>(propConfig.configArray[0]),
108 static_cast<size_t>(propConfig.configArray[1]));
109 auto liveObd2Frame = sensorStore->getSensorProperty("");
110 liveObd2Frame->prop = OBD2_LIVE_FRAME;
111
112 mPropStore->writeValue(std::move(liveObd2Frame), /*updateStatus=*/true);
113 }
114
initObd2FreezeFrame(const VehiclePropConfig & propConfig)115 void FakeObd2Frame::initObd2FreezeFrame(const VehiclePropConfig& propConfig) {
116 auto sensorStore = fillDefaultObd2Frame(static_cast<size_t>(propConfig.configArray[0]),
117 static_cast<size_t>(propConfig.configArray[1]));
118
119 static std::vector<std::string> sampleDtcs = {"P0070", "P0102", "P0123"};
120 for (auto&& dtc : sampleDtcs) {
121 auto freezeFrame = sensorStore->getSensorProperty(dtc);
122 freezeFrame->prop = OBD2_FREEZE_FRAME;
123
124 mPropStore->writeValue(std::move(freezeFrame), /*updateStatus=*/true);
125 }
126 }
127
getObd2FreezeFrame(const VehiclePropValue & requestedPropValue) const128 VhalResult<VehiclePropValuePool::RecyclableType> FakeObd2Frame::getObd2FreezeFrame(
129 const VehiclePropValue& requestedPropValue) const {
130 if (requestedPropValue.value.int64Values.size() != 1) {
131 return StatusError(StatusCode::INVALID_ARG)
132 << "asked for OBD2_FREEZE_FRAME without valid timestamp";
133 }
134 auto readValuesResult = mPropStore->readValuesForProperty(OBD2_FREEZE_FRAME);
135 if (!readValuesResult.ok()) {
136 return StatusError(StatusCode::INTERNAL_ERROR)
137 << "failed to read OBD2_FREEZE_FRAME property: "
138 << readValuesResult.error().message();
139 }
140 if (readValuesResult.value().size() == 0) {
141 // Should no freeze frame be available at the given timestamp, a response of NOT_AVAILABLE
142 // must be returned by the implementation
143 return StatusError(StatusCode::NOT_AVAILABLE);
144 }
145 auto timestamp = requestedPropValue.value.int64Values[0];
146 auto readValueResult = mPropStore->readValue(OBD2_FREEZE_FRAME, /*area=*/0, timestamp);
147 if (!readValueResult.ok()) {
148 return StatusError(StatusCode::INVALID_ARG)
149 << "asked for OBD2_FREEZE_FRAME at invalid timestamp";
150 }
151 return readValueResult;
152 }
153
getObd2DtcInfo() const154 VhalResult<VehiclePropValuePool::RecyclableType> FakeObd2Frame::getObd2DtcInfo() const {
155 std::vector<int64_t> timestamps;
156 auto result = mPropStore->readValuesForProperty(OBD2_FREEZE_FRAME);
157 if (!result.ok()) {
158 return StatusError(StatusCode::INTERNAL_ERROR)
159 << "failed to read OBD2_FREEZE_FRAME property: " << result.error().message();
160 }
161 for (const auto& freezeFrame : result.value()) {
162 timestamps.push_back(freezeFrame->timestamp);
163 }
164 auto outValue =
165 mPropStore->getValuePool()->obtain(VehiclePropertyType::INT64_VEC, timestamps.size());
166 outValue->value.int64Values = timestamps;
167 outValue->prop = OBD2_FREEZE_FRAME_INFO;
168 return outValue;
169 }
170
clearObd2FreezeFrames(const VehiclePropValue & propValue)171 VhalResult<void> FakeObd2Frame::clearObd2FreezeFrames(const VehiclePropValue& propValue) {
172 if (propValue.value.int64Values.size() == 0) {
173 mPropStore->removeValuesForProperty(OBD2_FREEZE_FRAME);
174 return {};
175 }
176 for (int64_t timestamp : propValue.value.int64Values) {
177 auto result = mPropStore->readValue(OBD2_FREEZE_FRAME, 0, timestamp);
178 if (!result.ok()) {
179 return StatusError(StatusCode::INVALID_ARG)
180 << "asked for OBD2_FREEZE_FRAME at invalid timestamp, error: %s"
181 << result.error().message();
182 }
183 mPropStore->removeValue(*result.value());
184 }
185 return {};
186 }
187
isDiagnosticProperty(const VehiclePropConfig & propConfig)188 bool FakeObd2Frame::isDiagnosticProperty(const VehiclePropConfig& propConfig) {
189 return (propConfig.prop == OBD2_LIVE_FRAME || propConfig.prop == OBD2_FREEZE_FRAME ||
190 propConfig.prop == OBD2_FREEZE_FRAME_CLEAR ||
191 propConfig.prop == OBD2_FREEZE_FRAME_INFO);
192 }
193
194 } // namespace obd2frame
195 } // namespace fake
196 } // namespace vehicle
197 } // namespace automotive
198 } // namespace hardware
199 } // namespace android
200