Loading automotive/vehicle/aidl/impl/fake_impl/obd2frame/include/FakeObd2Frame.h 0 → 100644 +64 −0 Original line number Diff line number Diff line /* * Copyright (C) 2021 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef android_hardware_automotive_vehicle_aidl_impl_fake_impl_obd2frame_include_FakeObd2Frame_H_ #define android_hardware_automotive_vehicle_aidl_impl_fake_impl_obd2frame_include_FakeObd2Frame_H_ #include <Obd2SensorStore.h> #include <VehicleHalTypes.h> #include <VehiclePropertyStore.h> #include <android-base/result.h> namespace android { namespace hardware { namespace automotive { namespace vehicle { namespace fake { namespace obd2frame { class FakeObd2Frame final { public: explicit FakeObd2Frame(std::shared_ptr<VehiclePropertyStore> propStore) : mPropStore(propStore) {} void initObd2LiveFrame( const ::aidl::android::hardware::automotive::vehicle::VehiclePropConfig& propConfig); void initObd2FreezeFrame( const ::aidl::android::hardware::automotive::vehicle::VehiclePropConfig& propConfig); ::android::base::Result<VehiclePropValuePool::RecyclableType> getObd2FreezeFrame( const ::aidl::android::hardware::automotive::vehicle::VehiclePropValue& requestedPropValue) const; ::android::base::Result<VehiclePropValuePool::RecyclableType> getObd2DtcInfo() const; ::aidl::android::hardware::automotive::vehicle::StatusCode clearObd2FreezeFrames( const ::aidl::android::hardware::automotive::vehicle::VehiclePropValue& propValue); static bool isDiagnosticProperty( const ::aidl::android::hardware::automotive::vehicle::VehiclePropConfig& propConfig); private: std::shared_ptr<VehiclePropertyStore> mPropStore; std::unique_ptr<Obd2SensorStore> fillDefaultObd2Frame(size_t numVendorIntegerSensors, size_t numVendorFloatSensors); }; } // namespace obd2frame } // namespace fake } // namespace vehicle } // namespace automotive } // namespace hardware } // namespace android #endif // android_hardware_automotive_vehicle_aidl_impl_fake_impl_obd2frame_include_FakeObd2Frame_H_ automotive/vehicle/aidl/impl/fake_impl/obd2frame/include/Obd2SensorStore.h +0 −1 Original line number Diff line number Diff line Loading @@ -60,7 +60,6 @@ class Obd2SensorStore final { // Stores an integer-valued sensor. ::aidl::android::hardware::automotive::vehicle::StatusCode setIntegerSensor(size_t index, int32_t value); // Stores a float-valued sensor. ::aidl::android::hardware::automotive::vehicle::StatusCode setFloatSensor( ::aidl::android::hardware::automotive::vehicle::DiagnosticFloatSensorIndex index, Loading automotive/vehicle/aidl/impl/fake_impl/obd2frame/src/FakeObd2Frame.cpp 0 → 100644 +199 −0 Original line number Diff line number Diff line /* * Copyright (C) 2021 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "FakeObd2Frame.h" #include "Obd2SensorStore.h" #include <PropertyUtils.h> #include <VehicleHalTypes.h> #include <VehiclePropertyStore.h> #include <VehicleUtils.h> #include <android-base/result.h> #include <utils/Log.h> namespace android { namespace hardware { namespace automotive { namespace vehicle { namespace fake { namespace obd2frame { using ::aidl::android::hardware::automotive::vehicle::DiagnosticFloatSensorIndex; using ::aidl::android::hardware::automotive::vehicle::DiagnosticIntegerSensorIndex; using ::aidl::android::hardware::automotive::vehicle::Obd2CommonIgnitionMonitors; using ::aidl::android::hardware::automotive::vehicle::Obd2FuelSystemStatus; using ::aidl::android::hardware::automotive::vehicle::Obd2FuelType; using ::aidl::android::hardware::automotive::vehicle::Obd2IgnitionMonitorKind; using ::aidl::android::hardware::automotive::vehicle::Obd2SecondaryAirStatus; using ::aidl::android::hardware::automotive::vehicle::Obd2SparkIgnitionMonitors; using ::aidl::android::hardware::automotive::vehicle::StatusCode; using ::aidl::android::hardware::automotive::vehicle::VehiclePropConfig; using ::aidl::android::hardware::automotive::vehicle::VehiclePropertyType; using ::aidl::android::hardware::automotive::vehicle::VehiclePropValue; using ::android::base::Error; using ::android::base::Result; std::unique_ptr<Obd2SensorStore> FakeObd2Frame::fillDefaultObd2Frame(size_t numVendorIntegerSensors, size_t numVendorFloatSensors) { std::unique_ptr<Obd2SensorStore> sensorStore(new Obd2SensorStore( mPropStore->getValuePool(), numVendorIntegerSensors, numVendorFloatSensors)); sensorStore->setIntegerSensor(DiagnosticIntegerSensorIndex::FUEL_SYSTEM_STATUS, toInt(Obd2FuelSystemStatus::CLOSED_LOOP)); sensorStore->setIntegerSensor(DiagnosticIntegerSensorIndex::MALFUNCTION_INDICATOR_LIGHT_ON, 0); sensorStore->setIntegerSensor(DiagnosticIntegerSensorIndex::IGNITION_MONITORS_SUPPORTED, toInt(Obd2IgnitionMonitorKind::SPARK)); sensorStore->setIntegerSensor( DiagnosticIntegerSensorIndex::IGNITION_SPECIFIC_MONITORS, toInt(Obd2CommonIgnitionMonitors::COMPONENTS_AVAILABLE) | toInt(Obd2CommonIgnitionMonitors::MISFIRE_AVAILABLE) | toInt(Obd2SparkIgnitionMonitors::AC_REFRIGERANT_AVAILABLE) | toInt(Obd2SparkIgnitionMonitors::EVAPORATIVE_SYSTEM_AVAILABLE)); sensorStore->setIntegerSensor(DiagnosticIntegerSensorIndex::INTAKE_AIR_TEMPERATURE, 35); sensorStore->setIntegerSensor(DiagnosticIntegerSensorIndex::COMMANDED_SECONDARY_AIR_STATUS, toInt(Obd2SecondaryAirStatus::FROM_OUTSIDE_OR_OFF)); sensorStore->setIntegerSensor(DiagnosticIntegerSensorIndex::NUM_OXYGEN_SENSORS_PRESENT, 1); sensorStore->setIntegerSensor(DiagnosticIntegerSensorIndex::RUNTIME_SINCE_ENGINE_START, 500); sensorStore->setIntegerSensor( DiagnosticIntegerSensorIndex::DISTANCE_TRAVELED_WITH_MALFUNCTION_INDICATOR_LIGHT_ON, 0); sensorStore->setIntegerSensor(DiagnosticIntegerSensorIndex::WARMUPS_SINCE_CODES_CLEARED, 51); sensorStore->setIntegerSensor( DiagnosticIntegerSensorIndex::DISTANCE_TRAVELED_SINCE_CODES_CLEARED, 365); sensorStore->setIntegerSensor(DiagnosticIntegerSensorIndex::ABSOLUTE_BAROMETRIC_PRESSURE, 30); sensorStore->setIntegerSensor(DiagnosticIntegerSensorIndex::CONTROL_MODULE_VOLTAGE, 12); sensorStore->setIntegerSensor(DiagnosticIntegerSensorIndex::AMBIENT_AIR_TEMPERATURE, 18); sensorStore->setIntegerSensor(DiagnosticIntegerSensorIndex::MAX_FUEL_AIR_EQUIVALENCE_RATIO, 1); sensorStore->setIntegerSensor(DiagnosticIntegerSensorIndex::FUEL_TYPE, toInt(Obd2FuelType::GASOLINE)); sensorStore->setFloatSensor(DiagnosticFloatSensorIndex::CALCULATED_ENGINE_LOAD, 0.153); sensorStore->setFloatSensor(DiagnosticFloatSensorIndex::SHORT_TERM_FUEL_TRIM_BANK1, -0.16); sensorStore->setFloatSensor(DiagnosticFloatSensorIndex::LONG_TERM_FUEL_TRIM_BANK1, -0.16); sensorStore->setFloatSensor(DiagnosticFloatSensorIndex::SHORT_TERM_FUEL_TRIM_BANK2, -0.16); sensorStore->setFloatSensor(DiagnosticFloatSensorIndex::LONG_TERM_FUEL_TRIM_BANK2, -0.16); sensorStore->setFloatSensor(DiagnosticFloatSensorIndex::INTAKE_MANIFOLD_ABSOLUTE_PRESSURE, 7.5); sensorStore->setFloatSensor(DiagnosticFloatSensorIndex::ENGINE_RPM, 1250.); sensorStore->setFloatSensor(DiagnosticFloatSensorIndex::VEHICLE_SPEED, 40.); sensorStore->setFloatSensor(DiagnosticFloatSensorIndex::TIMING_ADVANCE, 2.5); sensorStore->setFloatSensor(DiagnosticFloatSensorIndex::THROTTLE_POSITION, 19.75); sensorStore->setFloatSensor(DiagnosticFloatSensorIndex::OXYGEN_SENSOR1_VOLTAGE, 0.265); sensorStore->setFloatSensor(DiagnosticFloatSensorIndex::FUEL_TANK_LEVEL_INPUT, 0.824); sensorStore->setFloatSensor(DiagnosticFloatSensorIndex::EVAPORATION_SYSTEM_VAPOR_PRESSURE, -0.373); sensorStore->setFloatSensor(DiagnosticFloatSensorIndex::CATALYST_TEMPERATURE_BANK1_SENSOR1, 190.); sensorStore->setFloatSensor(DiagnosticFloatSensorIndex::RELATIVE_THROTTLE_POSITION, 3.); sensorStore->setFloatSensor(DiagnosticFloatSensorIndex::ABSOLUTE_THROTTLE_POSITION_B, 0.306); sensorStore->setFloatSensor(DiagnosticFloatSensorIndex::ACCELERATOR_PEDAL_POSITION_D, 0.188); sensorStore->setFloatSensor(DiagnosticFloatSensorIndex::ACCELERATOR_PEDAL_POSITION_E, 0.094); sensorStore->setFloatSensor(DiagnosticFloatSensorIndex::COMMANDED_THROTTLE_ACTUATOR, 0.024); return sensorStore; } void FakeObd2Frame::initObd2LiveFrame(const VehiclePropConfig& propConfig) { auto sensorStore = fillDefaultObd2Frame(static_cast<size_t>(propConfig.configArray[0]), static_cast<size_t>(propConfig.configArray[1])); auto liveObd2Frame = sensorStore->getSensorProperty(""); liveObd2Frame->prop = OBD2_LIVE_FRAME; mPropStore->writeValue(std::move(liveObd2Frame), /*updateStatus=*/true); } void FakeObd2Frame::initObd2FreezeFrame(const VehiclePropConfig& propConfig) { auto sensorStore = fillDefaultObd2Frame(static_cast<size_t>(propConfig.configArray[0]), static_cast<size_t>(propConfig.configArray[1])); static std::vector<std::string> sampleDtcs = {"P0070", "P0102", "P0123"}; for (auto&& dtc : sampleDtcs) { auto freezeFrame = sensorStore->getSensorProperty(dtc); freezeFrame->prop = OBD2_FREEZE_FRAME; mPropStore->writeValue(std::move(freezeFrame), /*updateStatus=*/true); } } Result<VehiclePropValuePool::RecyclableType> FakeObd2Frame::getObd2FreezeFrame( const VehiclePropValue& requestedPropValue) const { if (requestedPropValue.value.int64Values.size() != 1) { return Error(toInt(StatusCode::INVALID_ARG)) << "asked for OBD2_FREEZE_FRAME without valid timestamp"; } auto readValuesResult = mPropStore->readValuesForProperty(OBD2_FREEZE_FRAME); if (!readValuesResult.ok()) { return Error(toInt(StatusCode::INTERNAL_ERROR)) << "failed to read OBD2_FREEZE_FRAME property: " << readValuesResult.error().message(); } if (readValuesResult.value().size() == 0) { // Should no freeze frame be available at the given timestamp, a response of NOT_AVAILABLE // must be returned by the implementation return Error(toInt(StatusCode::NOT_AVAILABLE)); } auto timestamp = requestedPropValue.value.int64Values[0]; auto readValueResult = mPropStore->readValue(OBD2_FREEZE_FRAME, /*area=*/0, timestamp); if (!readValueResult.ok()) { return Error(toInt(StatusCode::INVALID_ARG)) << "asked for OBD2_FREEZE_FRAME at invalid timestamp"; } return readValueResult; } Result<VehiclePropValuePool::RecyclableType> FakeObd2Frame::getObd2DtcInfo() const { std::vector<int64_t> timestamps; auto result = mPropStore->readValuesForProperty(OBD2_FREEZE_FRAME); if (!result.ok()) { return Error(toInt(StatusCode::INTERNAL_ERROR)) << "failed to read OBD2_FREEZE_FRAME property: " << result.error().message(); } for (const auto& freezeFrame : result.value()) { timestamps.push_back(freezeFrame->timestamp); } auto outValue = mPropStore->getValuePool()->obtain(VehiclePropertyType::INT64_VEC, timestamps.size()); outValue->value.int64Values = timestamps; outValue->prop = OBD2_FREEZE_FRAME_INFO; return outValue; } StatusCode FakeObd2Frame::clearObd2FreezeFrames(const VehiclePropValue& propValue) { if (propValue.value.int64Values.size() == 0) { mPropStore->removeValuesForProperty(OBD2_FREEZE_FRAME); return StatusCode::OK; } for (int64_t timestamp : propValue.value.int64Values) { auto result = mPropStore->readValue(OBD2_FREEZE_FRAME, /*area=*/0, timestamp); if (!result.ok()) { ALOGE("asked for OBD2_FREEZE_FRAME at invalid timestamp"); return StatusCode::INVALID_ARG; } mPropStore->removeValue(*result.value()); } return StatusCode::OK; } bool FakeObd2Frame::isDiagnosticProperty(const VehiclePropConfig& propConfig) { return (propConfig.prop == OBD2_LIVE_FRAME || propConfig.prop == OBD2_FREEZE_FRAME || propConfig.prop == OBD2_FREEZE_FRAME_CLEAR || propConfig.prop == OBD2_FREEZE_FRAME_INFO); } } // namespace obd2frame } // namespace fake } // namespace vehicle } // namespace automotive } // namespace hardware } // namespace android automotive/vehicle/aidl/impl/fake_impl/obd2frame/test/FakeObd2FrameTest.cpp 0 → 100644 +204 −0 Original line number Diff line number Diff line /* * Copyright (C) 2021 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "FakeObd2Frame.h" #include <PropertyUtils.h> #include <VehicleObjectPool.h> #include <VehiclePropertyStore.h> #include <gtest/gtest.h> #include <utils/SystemClock.h> #include <set> namespace android { namespace hardware { namespace automotive { namespace vehicle { namespace fake { namespace obd2frame { using ::aidl::android::hardware::automotive::vehicle::VehiclePropConfig; using ::aidl::android::hardware::automotive::vehicle::VehicleProperty; using ::aidl::android::hardware::automotive::vehicle::VehiclePropertyAccess; using ::aidl::android::hardware::automotive::vehicle::VehiclePropertyChangeMode; using ::aidl::android::hardware::automotive::vehicle::VehiclePropValue; class FakeObd2FrameTest : public ::testing::Test { protected: void SetUp() override { std::shared_ptr<VehiclePropValuePool> valuePool = std::make_shared<VehiclePropValuePool>(); mPropertyStore = std::make_shared<VehiclePropertyStore>(valuePool); mObd2Frame = std::make_unique<FakeObd2Frame>(mPropertyStore); mPropertyStore->registerProperty(getObd2LiveFrameConfig()); mPropertyStore->registerProperty( getObd2FreezeFrameConfig(), [](const VehiclePropValue& propValue) { return propValue.timestamp; }); mPropertyStore->registerProperty(getObd2FreezeFrameInfoConfig()); } VehiclePropConfig getObd2LiveFrameConfig() { return VehiclePropConfig{.prop = OBD2_LIVE_FRAME, .access = VehiclePropertyAccess::READ, .changeMode = VehiclePropertyChangeMode::ON_CHANGE, .configArray = {1, 1}}; } VehiclePropConfig getObd2FreezeFrameConfig() { return VehiclePropConfig{.prop = OBD2_FREEZE_FRAME, .access = VehiclePropertyAccess::READ, .changeMode = VehiclePropertyChangeMode::ON_CHANGE, .configArray = {0, 0}}; } VehiclePropConfig getObd2FreezeFrameInfoConfig() { return VehiclePropConfig{.prop = OBD2_FREEZE_FRAME_INFO, .access = VehiclePropertyAccess::READ, .changeMode = VehiclePropertyChangeMode::ON_CHANGE}; } FakeObd2Frame* getFakeObd2Frame() { return mObd2Frame.get(); } VehiclePropertyStore* getPropertyStore() { return mPropertyStore.get(); } private: std::unique_ptr<FakeObd2Frame> mObd2Frame; std::shared_ptr<VehiclePropertyStore> mPropertyStore; }; TEST_F(FakeObd2FrameTest, testIsDiagnosticPropertyTrue) { for (auto prop : std::vector<int32_t>({ OBD2_LIVE_FRAME, OBD2_FREEZE_FRAME, OBD2_FREEZE_FRAME_CLEAR, OBD2_FREEZE_FRAME_INFO, })) { EXPECT_TRUE(FakeObd2Frame::isDiagnosticProperty(VehiclePropConfig{ .prop = prop, })); } } TEST_F(FakeObd2FrameTest, testIsDiagnosticPropertyFalse) { ASSERT_FALSE(FakeObd2Frame::isDiagnosticProperty(VehiclePropConfig{ .prop = toInt(VehicleProperty::INFO_VIN), })); } TEST_F(FakeObd2FrameTest, testInitObd2LiveFrame) { int64_t timestamp = elapsedRealtimeNano(); getFakeObd2Frame()->initObd2LiveFrame(getObd2LiveFrameConfig()); auto result = getPropertyStore()->readValue(OBD2_LIVE_FRAME); ASSERT_TRUE(result.ok()); auto& value = result.value(); EXPECT_GE(value->timestamp, timestamp); EXPECT_EQ(value->value.stringValue, ""); EXPECT_EQ(value->value.int32Values.size(), static_cast<size_t>(33)); EXPECT_EQ(value->value.floatValues.size(), static_cast<size_t>(72)); } TEST_F(FakeObd2FrameTest, testInitFreezeFrame) { getFakeObd2Frame()->initObd2FreezeFrame(getObd2FreezeFrameConfig()); auto result = getPropertyStore()->readValuesForProperty(OBD2_FREEZE_FRAME); ASSERT_TRUE(result.ok()); ASSERT_EQ(result.value().size(), static_cast<size_t>(3)); } TEST_F(FakeObd2FrameTest, testGetObd2DtcInfo) { getFakeObd2Frame()->initObd2FreezeFrame(getObd2FreezeFrameConfig()); auto result = getFakeObd2Frame()->getObd2DtcInfo(); ASSERT_TRUE(result.ok()); EXPECT_EQ(result.value()->prop, OBD2_FREEZE_FRAME_INFO); EXPECT_EQ(result.value()->value.int64Values.size(), static_cast<size_t>(3)); } TEST_F(FakeObd2FrameTest, testGetObd2FreezeFrame) { getFakeObd2Frame()->initObd2FreezeFrame(getObd2FreezeFrameConfig()); auto result = getFakeObd2Frame()->getObd2DtcInfo(); ASSERT_TRUE(result.ok()); ASSERT_EQ(result.value()->prop, OBD2_FREEZE_FRAME_INFO); ASSERT_EQ(result.value()->value.int64Values.size(), static_cast<size_t>(3)); std::set<std::string> dtcs; for (int64_t timestamp : result.value()->value.int64Values) { auto freezeFrameResult = getFakeObd2Frame()->getObd2FreezeFrame(VehiclePropValue{ .value.int64Values = {timestamp}, }); ASSERT_TRUE(freezeFrameResult.ok()); dtcs.insert(freezeFrameResult.value()->value.stringValue); } ASSERT_EQ(dtcs, std::set<std::string>({"P0070", "P0102", "P0123"})); } TEST_F(FakeObd2FrameTest, testClearObd2FreezeFrameAll) { getFakeObd2Frame()->initObd2FreezeFrame(getObd2FreezeFrameConfig()); auto result = getFakeObd2Frame()->getObd2DtcInfo(); ASSERT_TRUE(result.ok()); ASSERT_EQ(result.value()->prop, OBD2_FREEZE_FRAME_INFO); ASSERT_EQ(result.value()->value.int64Values.size(), static_cast<size_t>(3)); getFakeObd2Frame()->clearObd2FreezeFrames(VehiclePropValue{}); result = getFakeObd2Frame()->getObd2DtcInfo(); ASSERT_TRUE(result.ok()); EXPECT_EQ(result.value()->prop, OBD2_FREEZE_FRAME_INFO); EXPECT_EQ(result.value()->value.int64Values.size(), static_cast<size_t>(0)); } TEST_F(FakeObd2FrameTest, testClearObd2FreezeFrameByTimestamp) { getFakeObd2Frame()->initObd2FreezeFrame(getObd2FreezeFrameConfig()); auto result = getFakeObd2Frame()->getObd2DtcInfo(); ASSERT_TRUE(result.ok()); ASSERT_EQ(result.value()->prop, OBD2_FREEZE_FRAME_INFO); ASSERT_EQ(result.value()->value.int64Values.size(), static_cast<size_t>(3)); getFakeObd2Frame()->clearObd2FreezeFrames( VehiclePropValue{.value.int64Values = {result.value()->value.int64Values[0], result.value()->value.int64Values[1]}}); result = getFakeObd2Frame()->getObd2DtcInfo(); ASSERT_TRUE(result.ok()); EXPECT_EQ(result.value()->prop, OBD2_FREEZE_FRAME_INFO); EXPECT_EQ(result.value()->value.int64Values.size(), static_cast<size_t>(1)); } } // namespace obd2frame } // namespace fake } // namespace vehicle } // namespace automotive } // namespace hardware } // namespace android automotive/vehicle/aidl/impl/utils/common/include/VehiclePropertyStore.h +2 −0 Original line number Diff line number Diff line Loading @@ -110,6 +110,8 @@ class VehiclePropertyStore final { // Set a callback that would be called when a property value has been updated. void setOnValueChangeCallback(const OnValueChangeCallback& callback); inline std::shared_ptr<VehiclePropValuePool> getValuePool() { return mValuePool; } private: struct RecordId { int32_t area; Loading Loading
automotive/vehicle/aidl/impl/fake_impl/obd2frame/include/FakeObd2Frame.h 0 → 100644 +64 −0 Original line number Diff line number Diff line /* * Copyright (C) 2021 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef android_hardware_automotive_vehicle_aidl_impl_fake_impl_obd2frame_include_FakeObd2Frame_H_ #define android_hardware_automotive_vehicle_aidl_impl_fake_impl_obd2frame_include_FakeObd2Frame_H_ #include <Obd2SensorStore.h> #include <VehicleHalTypes.h> #include <VehiclePropertyStore.h> #include <android-base/result.h> namespace android { namespace hardware { namespace automotive { namespace vehicle { namespace fake { namespace obd2frame { class FakeObd2Frame final { public: explicit FakeObd2Frame(std::shared_ptr<VehiclePropertyStore> propStore) : mPropStore(propStore) {} void initObd2LiveFrame( const ::aidl::android::hardware::automotive::vehicle::VehiclePropConfig& propConfig); void initObd2FreezeFrame( const ::aidl::android::hardware::automotive::vehicle::VehiclePropConfig& propConfig); ::android::base::Result<VehiclePropValuePool::RecyclableType> getObd2FreezeFrame( const ::aidl::android::hardware::automotive::vehicle::VehiclePropValue& requestedPropValue) const; ::android::base::Result<VehiclePropValuePool::RecyclableType> getObd2DtcInfo() const; ::aidl::android::hardware::automotive::vehicle::StatusCode clearObd2FreezeFrames( const ::aidl::android::hardware::automotive::vehicle::VehiclePropValue& propValue); static bool isDiagnosticProperty( const ::aidl::android::hardware::automotive::vehicle::VehiclePropConfig& propConfig); private: std::shared_ptr<VehiclePropertyStore> mPropStore; std::unique_ptr<Obd2SensorStore> fillDefaultObd2Frame(size_t numVendorIntegerSensors, size_t numVendorFloatSensors); }; } // namespace obd2frame } // namespace fake } // namespace vehicle } // namespace automotive } // namespace hardware } // namespace android #endif // android_hardware_automotive_vehicle_aidl_impl_fake_impl_obd2frame_include_FakeObd2Frame_H_
automotive/vehicle/aidl/impl/fake_impl/obd2frame/include/Obd2SensorStore.h +0 −1 Original line number Diff line number Diff line Loading @@ -60,7 +60,6 @@ class Obd2SensorStore final { // Stores an integer-valued sensor. ::aidl::android::hardware::automotive::vehicle::StatusCode setIntegerSensor(size_t index, int32_t value); // Stores a float-valued sensor. ::aidl::android::hardware::automotive::vehicle::StatusCode setFloatSensor( ::aidl::android::hardware::automotive::vehicle::DiagnosticFloatSensorIndex index, Loading
automotive/vehicle/aidl/impl/fake_impl/obd2frame/src/FakeObd2Frame.cpp 0 → 100644 +199 −0 Original line number Diff line number Diff line /* * Copyright (C) 2021 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "FakeObd2Frame.h" #include "Obd2SensorStore.h" #include <PropertyUtils.h> #include <VehicleHalTypes.h> #include <VehiclePropertyStore.h> #include <VehicleUtils.h> #include <android-base/result.h> #include <utils/Log.h> namespace android { namespace hardware { namespace automotive { namespace vehicle { namespace fake { namespace obd2frame { using ::aidl::android::hardware::automotive::vehicle::DiagnosticFloatSensorIndex; using ::aidl::android::hardware::automotive::vehicle::DiagnosticIntegerSensorIndex; using ::aidl::android::hardware::automotive::vehicle::Obd2CommonIgnitionMonitors; using ::aidl::android::hardware::automotive::vehicle::Obd2FuelSystemStatus; using ::aidl::android::hardware::automotive::vehicle::Obd2FuelType; using ::aidl::android::hardware::automotive::vehicle::Obd2IgnitionMonitorKind; using ::aidl::android::hardware::automotive::vehicle::Obd2SecondaryAirStatus; using ::aidl::android::hardware::automotive::vehicle::Obd2SparkIgnitionMonitors; using ::aidl::android::hardware::automotive::vehicle::StatusCode; using ::aidl::android::hardware::automotive::vehicle::VehiclePropConfig; using ::aidl::android::hardware::automotive::vehicle::VehiclePropertyType; using ::aidl::android::hardware::automotive::vehicle::VehiclePropValue; using ::android::base::Error; using ::android::base::Result; std::unique_ptr<Obd2SensorStore> FakeObd2Frame::fillDefaultObd2Frame(size_t numVendorIntegerSensors, size_t numVendorFloatSensors) { std::unique_ptr<Obd2SensorStore> sensorStore(new Obd2SensorStore( mPropStore->getValuePool(), numVendorIntegerSensors, numVendorFloatSensors)); sensorStore->setIntegerSensor(DiagnosticIntegerSensorIndex::FUEL_SYSTEM_STATUS, toInt(Obd2FuelSystemStatus::CLOSED_LOOP)); sensorStore->setIntegerSensor(DiagnosticIntegerSensorIndex::MALFUNCTION_INDICATOR_LIGHT_ON, 0); sensorStore->setIntegerSensor(DiagnosticIntegerSensorIndex::IGNITION_MONITORS_SUPPORTED, toInt(Obd2IgnitionMonitorKind::SPARK)); sensorStore->setIntegerSensor( DiagnosticIntegerSensorIndex::IGNITION_SPECIFIC_MONITORS, toInt(Obd2CommonIgnitionMonitors::COMPONENTS_AVAILABLE) | toInt(Obd2CommonIgnitionMonitors::MISFIRE_AVAILABLE) | toInt(Obd2SparkIgnitionMonitors::AC_REFRIGERANT_AVAILABLE) | toInt(Obd2SparkIgnitionMonitors::EVAPORATIVE_SYSTEM_AVAILABLE)); sensorStore->setIntegerSensor(DiagnosticIntegerSensorIndex::INTAKE_AIR_TEMPERATURE, 35); sensorStore->setIntegerSensor(DiagnosticIntegerSensorIndex::COMMANDED_SECONDARY_AIR_STATUS, toInt(Obd2SecondaryAirStatus::FROM_OUTSIDE_OR_OFF)); sensorStore->setIntegerSensor(DiagnosticIntegerSensorIndex::NUM_OXYGEN_SENSORS_PRESENT, 1); sensorStore->setIntegerSensor(DiagnosticIntegerSensorIndex::RUNTIME_SINCE_ENGINE_START, 500); sensorStore->setIntegerSensor( DiagnosticIntegerSensorIndex::DISTANCE_TRAVELED_WITH_MALFUNCTION_INDICATOR_LIGHT_ON, 0); sensorStore->setIntegerSensor(DiagnosticIntegerSensorIndex::WARMUPS_SINCE_CODES_CLEARED, 51); sensorStore->setIntegerSensor( DiagnosticIntegerSensorIndex::DISTANCE_TRAVELED_SINCE_CODES_CLEARED, 365); sensorStore->setIntegerSensor(DiagnosticIntegerSensorIndex::ABSOLUTE_BAROMETRIC_PRESSURE, 30); sensorStore->setIntegerSensor(DiagnosticIntegerSensorIndex::CONTROL_MODULE_VOLTAGE, 12); sensorStore->setIntegerSensor(DiagnosticIntegerSensorIndex::AMBIENT_AIR_TEMPERATURE, 18); sensorStore->setIntegerSensor(DiagnosticIntegerSensorIndex::MAX_FUEL_AIR_EQUIVALENCE_RATIO, 1); sensorStore->setIntegerSensor(DiagnosticIntegerSensorIndex::FUEL_TYPE, toInt(Obd2FuelType::GASOLINE)); sensorStore->setFloatSensor(DiagnosticFloatSensorIndex::CALCULATED_ENGINE_LOAD, 0.153); sensorStore->setFloatSensor(DiagnosticFloatSensorIndex::SHORT_TERM_FUEL_TRIM_BANK1, -0.16); sensorStore->setFloatSensor(DiagnosticFloatSensorIndex::LONG_TERM_FUEL_TRIM_BANK1, -0.16); sensorStore->setFloatSensor(DiagnosticFloatSensorIndex::SHORT_TERM_FUEL_TRIM_BANK2, -0.16); sensorStore->setFloatSensor(DiagnosticFloatSensorIndex::LONG_TERM_FUEL_TRIM_BANK2, -0.16); sensorStore->setFloatSensor(DiagnosticFloatSensorIndex::INTAKE_MANIFOLD_ABSOLUTE_PRESSURE, 7.5); sensorStore->setFloatSensor(DiagnosticFloatSensorIndex::ENGINE_RPM, 1250.); sensorStore->setFloatSensor(DiagnosticFloatSensorIndex::VEHICLE_SPEED, 40.); sensorStore->setFloatSensor(DiagnosticFloatSensorIndex::TIMING_ADVANCE, 2.5); sensorStore->setFloatSensor(DiagnosticFloatSensorIndex::THROTTLE_POSITION, 19.75); sensorStore->setFloatSensor(DiagnosticFloatSensorIndex::OXYGEN_SENSOR1_VOLTAGE, 0.265); sensorStore->setFloatSensor(DiagnosticFloatSensorIndex::FUEL_TANK_LEVEL_INPUT, 0.824); sensorStore->setFloatSensor(DiagnosticFloatSensorIndex::EVAPORATION_SYSTEM_VAPOR_PRESSURE, -0.373); sensorStore->setFloatSensor(DiagnosticFloatSensorIndex::CATALYST_TEMPERATURE_BANK1_SENSOR1, 190.); sensorStore->setFloatSensor(DiagnosticFloatSensorIndex::RELATIVE_THROTTLE_POSITION, 3.); sensorStore->setFloatSensor(DiagnosticFloatSensorIndex::ABSOLUTE_THROTTLE_POSITION_B, 0.306); sensorStore->setFloatSensor(DiagnosticFloatSensorIndex::ACCELERATOR_PEDAL_POSITION_D, 0.188); sensorStore->setFloatSensor(DiagnosticFloatSensorIndex::ACCELERATOR_PEDAL_POSITION_E, 0.094); sensorStore->setFloatSensor(DiagnosticFloatSensorIndex::COMMANDED_THROTTLE_ACTUATOR, 0.024); return sensorStore; } void FakeObd2Frame::initObd2LiveFrame(const VehiclePropConfig& propConfig) { auto sensorStore = fillDefaultObd2Frame(static_cast<size_t>(propConfig.configArray[0]), static_cast<size_t>(propConfig.configArray[1])); auto liveObd2Frame = sensorStore->getSensorProperty(""); liveObd2Frame->prop = OBD2_LIVE_FRAME; mPropStore->writeValue(std::move(liveObd2Frame), /*updateStatus=*/true); } void FakeObd2Frame::initObd2FreezeFrame(const VehiclePropConfig& propConfig) { auto sensorStore = fillDefaultObd2Frame(static_cast<size_t>(propConfig.configArray[0]), static_cast<size_t>(propConfig.configArray[1])); static std::vector<std::string> sampleDtcs = {"P0070", "P0102", "P0123"}; for (auto&& dtc : sampleDtcs) { auto freezeFrame = sensorStore->getSensorProperty(dtc); freezeFrame->prop = OBD2_FREEZE_FRAME; mPropStore->writeValue(std::move(freezeFrame), /*updateStatus=*/true); } } Result<VehiclePropValuePool::RecyclableType> FakeObd2Frame::getObd2FreezeFrame( const VehiclePropValue& requestedPropValue) const { if (requestedPropValue.value.int64Values.size() != 1) { return Error(toInt(StatusCode::INVALID_ARG)) << "asked for OBD2_FREEZE_FRAME without valid timestamp"; } auto readValuesResult = mPropStore->readValuesForProperty(OBD2_FREEZE_FRAME); if (!readValuesResult.ok()) { return Error(toInt(StatusCode::INTERNAL_ERROR)) << "failed to read OBD2_FREEZE_FRAME property: " << readValuesResult.error().message(); } if (readValuesResult.value().size() == 0) { // Should no freeze frame be available at the given timestamp, a response of NOT_AVAILABLE // must be returned by the implementation return Error(toInt(StatusCode::NOT_AVAILABLE)); } auto timestamp = requestedPropValue.value.int64Values[0]; auto readValueResult = mPropStore->readValue(OBD2_FREEZE_FRAME, /*area=*/0, timestamp); if (!readValueResult.ok()) { return Error(toInt(StatusCode::INVALID_ARG)) << "asked for OBD2_FREEZE_FRAME at invalid timestamp"; } return readValueResult; } Result<VehiclePropValuePool::RecyclableType> FakeObd2Frame::getObd2DtcInfo() const { std::vector<int64_t> timestamps; auto result = mPropStore->readValuesForProperty(OBD2_FREEZE_FRAME); if (!result.ok()) { return Error(toInt(StatusCode::INTERNAL_ERROR)) << "failed to read OBD2_FREEZE_FRAME property: " << result.error().message(); } for (const auto& freezeFrame : result.value()) { timestamps.push_back(freezeFrame->timestamp); } auto outValue = mPropStore->getValuePool()->obtain(VehiclePropertyType::INT64_VEC, timestamps.size()); outValue->value.int64Values = timestamps; outValue->prop = OBD2_FREEZE_FRAME_INFO; return outValue; } StatusCode FakeObd2Frame::clearObd2FreezeFrames(const VehiclePropValue& propValue) { if (propValue.value.int64Values.size() == 0) { mPropStore->removeValuesForProperty(OBD2_FREEZE_FRAME); return StatusCode::OK; } for (int64_t timestamp : propValue.value.int64Values) { auto result = mPropStore->readValue(OBD2_FREEZE_FRAME, /*area=*/0, timestamp); if (!result.ok()) { ALOGE("asked for OBD2_FREEZE_FRAME at invalid timestamp"); return StatusCode::INVALID_ARG; } mPropStore->removeValue(*result.value()); } return StatusCode::OK; } bool FakeObd2Frame::isDiagnosticProperty(const VehiclePropConfig& propConfig) { return (propConfig.prop == OBD2_LIVE_FRAME || propConfig.prop == OBD2_FREEZE_FRAME || propConfig.prop == OBD2_FREEZE_FRAME_CLEAR || propConfig.prop == OBD2_FREEZE_FRAME_INFO); } } // namespace obd2frame } // namespace fake } // namespace vehicle } // namespace automotive } // namespace hardware } // namespace android
automotive/vehicle/aidl/impl/fake_impl/obd2frame/test/FakeObd2FrameTest.cpp 0 → 100644 +204 −0 Original line number Diff line number Diff line /* * Copyright (C) 2021 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "FakeObd2Frame.h" #include <PropertyUtils.h> #include <VehicleObjectPool.h> #include <VehiclePropertyStore.h> #include <gtest/gtest.h> #include <utils/SystemClock.h> #include <set> namespace android { namespace hardware { namespace automotive { namespace vehicle { namespace fake { namespace obd2frame { using ::aidl::android::hardware::automotive::vehicle::VehiclePropConfig; using ::aidl::android::hardware::automotive::vehicle::VehicleProperty; using ::aidl::android::hardware::automotive::vehicle::VehiclePropertyAccess; using ::aidl::android::hardware::automotive::vehicle::VehiclePropertyChangeMode; using ::aidl::android::hardware::automotive::vehicle::VehiclePropValue; class FakeObd2FrameTest : public ::testing::Test { protected: void SetUp() override { std::shared_ptr<VehiclePropValuePool> valuePool = std::make_shared<VehiclePropValuePool>(); mPropertyStore = std::make_shared<VehiclePropertyStore>(valuePool); mObd2Frame = std::make_unique<FakeObd2Frame>(mPropertyStore); mPropertyStore->registerProperty(getObd2LiveFrameConfig()); mPropertyStore->registerProperty( getObd2FreezeFrameConfig(), [](const VehiclePropValue& propValue) { return propValue.timestamp; }); mPropertyStore->registerProperty(getObd2FreezeFrameInfoConfig()); } VehiclePropConfig getObd2LiveFrameConfig() { return VehiclePropConfig{.prop = OBD2_LIVE_FRAME, .access = VehiclePropertyAccess::READ, .changeMode = VehiclePropertyChangeMode::ON_CHANGE, .configArray = {1, 1}}; } VehiclePropConfig getObd2FreezeFrameConfig() { return VehiclePropConfig{.prop = OBD2_FREEZE_FRAME, .access = VehiclePropertyAccess::READ, .changeMode = VehiclePropertyChangeMode::ON_CHANGE, .configArray = {0, 0}}; } VehiclePropConfig getObd2FreezeFrameInfoConfig() { return VehiclePropConfig{.prop = OBD2_FREEZE_FRAME_INFO, .access = VehiclePropertyAccess::READ, .changeMode = VehiclePropertyChangeMode::ON_CHANGE}; } FakeObd2Frame* getFakeObd2Frame() { return mObd2Frame.get(); } VehiclePropertyStore* getPropertyStore() { return mPropertyStore.get(); } private: std::unique_ptr<FakeObd2Frame> mObd2Frame; std::shared_ptr<VehiclePropertyStore> mPropertyStore; }; TEST_F(FakeObd2FrameTest, testIsDiagnosticPropertyTrue) { for (auto prop : std::vector<int32_t>({ OBD2_LIVE_FRAME, OBD2_FREEZE_FRAME, OBD2_FREEZE_FRAME_CLEAR, OBD2_FREEZE_FRAME_INFO, })) { EXPECT_TRUE(FakeObd2Frame::isDiagnosticProperty(VehiclePropConfig{ .prop = prop, })); } } TEST_F(FakeObd2FrameTest, testIsDiagnosticPropertyFalse) { ASSERT_FALSE(FakeObd2Frame::isDiagnosticProperty(VehiclePropConfig{ .prop = toInt(VehicleProperty::INFO_VIN), })); } TEST_F(FakeObd2FrameTest, testInitObd2LiveFrame) { int64_t timestamp = elapsedRealtimeNano(); getFakeObd2Frame()->initObd2LiveFrame(getObd2LiveFrameConfig()); auto result = getPropertyStore()->readValue(OBD2_LIVE_FRAME); ASSERT_TRUE(result.ok()); auto& value = result.value(); EXPECT_GE(value->timestamp, timestamp); EXPECT_EQ(value->value.stringValue, ""); EXPECT_EQ(value->value.int32Values.size(), static_cast<size_t>(33)); EXPECT_EQ(value->value.floatValues.size(), static_cast<size_t>(72)); } TEST_F(FakeObd2FrameTest, testInitFreezeFrame) { getFakeObd2Frame()->initObd2FreezeFrame(getObd2FreezeFrameConfig()); auto result = getPropertyStore()->readValuesForProperty(OBD2_FREEZE_FRAME); ASSERT_TRUE(result.ok()); ASSERT_EQ(result.value().size(), static_cast<size_t>(3)); } TEST_F(FakeObd2FrameTest, testGetObd2DtcInfo) { getFakeObd2Frame()->initObd2FreezeFrame(getObd2FreezeFrameConfig()); auto result = getFakeObd2Frame()->getObd2DtcInfo(); ASSERT_TRUE(result.ok()); EXPECT_EQ(result.value()->prop, OBD2_FREEZE_FRAME_INFO); EXPECT_EQ(result.value()->value.int64Values.size(), static_cast<size_t>(3)); } TEST_F(FakeObd2FrameTest, testGetObd2FreezeFrame) { getFakeObd2Frame()->initObd2FreezeFrame(getObd2FreezeFrameConfig()); auto result = getFakeObd2Frame()->getObd2DtcInfo(); ASSERT_TRUE(result.ok()); ASSERT_EQ(result.value()->prop, OBD2_FREEZE_FRAME_INFO); ASSERT_EQ(result.value()->value.int64Values.size(), static_cast<size_t>(3)); std::set<std::string> dtcs; for (int64_t timestamp : result.value()->value.int64Values) { auto freezeFrameResult = getFakeObd2Frame()->getObd2FreezeFrame(VehiclePropValue{ .value.int64Values = {timestamp}, }); ASSERT_TRUE(freezeFrameResult.ok()); dtcs.insert(freezeFrameResult.value()->value.stringValue); } ASSERT_EQ(dtcs, std::set<std::string>({"P0070", "P0102", "P0123"})); } TEST_F(FakeObd2FrameTest, testClearObd2FreezeFrameAll) { getFakeObd2Frame()->initObd2FreezeFrame(getObd2FreezeFrameConfig()); auto result = getFakeObd2Frame()->getObd2DtcInfo(); ASSERT_TRUE(result.ok()); ASSERT_EQ(result.value()->prop, OBD2_FREEZE_FRAME_INFO); ASSERT_EQ(result.value()->value.int64Values.size(), static_cast<size_t>(3)); getFakeObd2Frame()->clearObd2FreezeFrames(VehiclePropValue{}); result = getFakeObd2Frame()->getObd2DtcInfo(); ASSERT_TRUE(result.ok()); EXPECT_EQ(result.value()->prop, OBD2_FREEZE_FRAME_INFO); EXPECT_EQ(result.value()->value.int64Values.size(), static_cast<size_t>(0)); } TEST_F(FakeObd2FrameTest, testClearObd2FreezeFrameByTimestamp) { getFakeObd2Frame()->initObd2FreezeFrame(getObd2FreezeFrameConfig()); auto result = getFakeObd2Frame()->getObd2DtcInfo(); ASSERT_TRUE(result.ok()); ASSERT_EQ(result.value()->prop, OBD2_FREEZE_FRAME_INFO); ASSERT_EQ(result.value()->value.int64Values.size(), static_cast<size_t>(3)); getFakeObd2Frame()->clearObd2FreezeFrames( VehiclePropValue{.value.int64Values = {result.value()->value.int64Values[0], result.value()->value.int64Values[1]}}); result = getFakeObd2Frame()->getObd2DtcInfo(); ASSERT_TRUE(result.ok()); EXPECT_EQ(result.value()->prop, OBD2_FREEZE_FRAME_INFO); EXPECT_EQ(result.value()->value.int64Values.size(), static_cast<size_t>(1)); } } // namespace obd2frame } // namespace fake } // namespace vehicle } // namespace automotive } // namespace hardware } // namespace android
automotive/vehicle/aidl/impl/utils/common/include/VehiclePropertyStore.h +2 −0 Original line number Diff line number Diff line Loading @@ -110,6 +110,8 @@ class VehiclePropertyStore final { // Set a callback that would be called when a property value has been updated. void setOnValueChangeCallback(const OnValueChangeCallback& callback); inline std::shared_ptr<VehiclePropValuePool> getValuePool() { return mValuePool; } private: struct RecordId { int32_t area; Loading