diff --git a/automotive/can/1.0/vts/functional/VtsHalCanBusV1_0TargetTest.cpp b/automotive/can/1.0/vts/functional/VtsHalCanBusV1_0TargetTest.cpp index d91d9f5035..a6e7baddc9 100644 --- a/automotive/can/1.0/vts/functional/VtsHalCanBusV1_0TargetTest.cpp +++ b/automotive/can/1.0/vts/functional/VtsHalCanBusV1_0TargetTest.cpp @@ -18,6 +18,7 @@ #include #include #include +#include #include #include #include @@ -27,6 +28,8 @@ namespace android::hardware::automotive::can::V1_0::vts { using hardware::hidl_vec; +using InterfaceType = ICanController::InterfaceType; +using IfId = ICanController::BusConfig::InterfaceId; struct CanMessageListener : public can::V1_0::ICanMessageListener { virtual Return onReceive(const can::V1_0::CanMessage&) override { return {}; } @@ -41,22 +44,62 @@ class CanBusHalTest : public ::testing::TestWithParam { virtual void SetUp() override; virtual void TearDown() override; + bool up(InterfaceType iftype, const std::string& srvname, const std::string& ifname); + std::tuple> listen(const hidl_vec& filter, const sp& listener); sp listenForErrors(const sp& listener); sp mCanBus; + sp mCanController; }; void CanBusHalTest::SetUp() { mCanBus = ICanBus::getService(GetParam()); ASSERT_TRUE(mCanBus) << "Couldn't open CAN Bus: " << GetParam(); + const auto controllers = getAllHalInstanceNames(ICanController::descriptor); + ASSERT_GT(controllers.size(), 0u); + // just grab the first one + mCanController = ICanController::getService(controllers[0]); + ASSERT_TRUE(mCanController) << "Couldn't open CAN Controller: " << controllers[0]; + + // this will throw an error if the bus is already up, but we have to try. + up(InterfaceType::VIRTUAL, GetParam(), "vcan0"); } void CanBusHalTest::TearDown() { mCanBus.clear(); } +bool CanBusHalTest::up(InterfaceType iftype, const std::string& srvname, + const std::string& ifname) { + ICanController::BusConfig config = {}; + config.name = srvname; + + // TODO(b/146214370): move interfaceId constructors to a library + if (iftype == InterfaceType::SOCKETCAN) { + IfId::Socketcan socketcan = {}; + socketcan.ifname(ifname); + config.interfaceId.socketcan(socketcan); + } else if (iftype == InterfaceType::SLCAN) { + IfId::Slcan slcan = {}; + slcan.ttyname(ifname); + config.interfaceId.slcan(slcan); + } else if (iftype == InterfaceType::VIRTUAL) { + config.interfaceId.virtualif({ifname}); + } else { + ADD_FAILURE() << "Unexpected iftype: " << toString(iftype); + } + + const auto upresult = mCanController->upInterface(config); + if (upresult != ICanController::Result::OK) { + // upInterface returns INVALID_STATE if the interface is already up (which is fine). + EXPECT_EQ(ICanController::Result::INVALID_STATE, upresult) + << ifname << " can't be brought up!"; + } + return true; +} + std::tuple> CanBusHalTest::listen( const hidl_vec& filter, const sp& listener) { Result halResult;