From 01247f9508f3ac20a0302c2f7f9d9aa211dfcb55 Mon Sep 17 00:00:00 2001 From: chrisweir Date: Thu, 9 Jan 2020 15:55:44 -0800 Subject: [PATCH] CAN Configurator Service Configurator service for the CAN HAL and extracting some of the CAN HAL configuration logic into a library for the various tools to share. Bug: 142653776 Test: Manual Change-Id: Id33871da851bcb442a7f851919f713ec913830ff (cherry picked from commit 33ce7505d150a39eb288f3062ee2090afc734199) --- automotive/can/1.0/tools/Android.bp | 9 ++ automotive/can/1.0/tools/canhalctrl.cpp | 24 +-- .../can/1.0/tools/configurator/Android.bp | 34 ++++ .../tools/configurator/canhalconfigurator.cpp | 103 ++++++++++++ .../tools/configurator/canhalconfigurator.rc | 3 + .../1.0/tools/configurator/canprototools.cpp | 152 ++++++++++++++++++ .../1.0/tools/configurator/canprototools.h | 49 ++++++ .../1.0/tools/configurator/proto/Android.bp | 28 ++++ .../configurator/proto/canbus_config.proto | 52 ++++++ .../can/1.0/tools/libcanhaltools/Android.bp | 32 ++++ .../include/libcanhaltools/libcanhaltools.h | 48 ++++++ .../tools/libcanhaltools/libcanhaltools.cpp | 85 ++++++++++ 12 files changed, 599 insertions(+), 20 deletions(-) create mode 100644 automotive/can/1.0/tools/configurator/Android.bp create mode 100644 automotive/can/1.0/tools/configurator/canhalconfigurator.cpp create mode 100644 automotive/can/1.0/tools/configurator/canhalconfigurator.rc create mode 100644 automotive/can/1.0/tools/configurator/canprototools.cpp create mode 100644 automotive/can/1.0/tools/configurator/canprototools.h create mode 100644 automotive/can/1.0/tools/configurator/proto/Android.bp create mode 100644 automotive/can/1.0/tools/configurator/proto/canbus_config.proto create mode 100644 automotive/can/1.0/tools/libcanhaltools/Android.bp create mode 100644 automotive/can/1.0/tools/libcanhaltools/include/libcanhaltools/libcanhaltools.h create mode 100644 automotive/can/1.0/tools/libcanhaltools/libcanhaltools.cpp diff --git a/automotive/can/1.0/tools/Android.bp b/automotive/can/1.0/tools/Android.bp index 21f364b629..a6c40d933a 100644 --- a/automotive/can/1.0/tools/Android.bp +++ b/automotive/can/1.0/tools/Android.bp @@ -27,6 +27,9 @@ cc_binary { header_libs: [ "android.hardware.automotive.can@hidl-utils-lib", ], + static_libs: [ + "android.hardware.automotive.can@libcanhaltools", + ], } cc_binary { @@ -42,6 +45,9 @@ cc_binary { header_libs: [ "android.hardware.automotive.can@hidl-utils-lib", ], + static_libs: [ + "android.hardware.automotive.can@libcanhaltools", + ], } cc_binary { @@ -54,4 +60,7 @@ cc_binary { "android.hardware.automotive.can@1.0", "libhidlbase", ], + static_libs: [ + "android.hardware.automotive.can@libcanhaltools", + ], } diff --git a/automotive/can/1.0/tools/canhalctrl.cpp b/automotive/can/1.0/tools/canhalctrl.cpp index 33755bfffd..c186b74386 100644 --- a/automotive/can/1.0/tools/canhalctrl.cpp +++ b/automotive/can/1.0/tools/canhalctrl.cpp @@ -18,6 +18,7 @@ #include #include #include +#include #include #include @@ -41,34 +42,17 @@ static void usage() { std::cerr << " bus name - name under which ICanBus will be published" << std::endl; } -static hidl_vec getControlServices() { - auto manager = hidl::manager::V1_2::IServiceManager::getService(); - hidl_vec services; - manager->listManifestByInterface(ICanController::descriptor, hidl_utils::fill(&services)); - if (services.size() == 0) { - std::cerr << "No ICanController services registered (missing privileges?)" << std::endl; - exit(-1); - } - return services; -} - -static bool isSupported(sp ctrl, ICanController::InterfaceType iftype) { - hidl_vec supported; - if (!ctrl->getSupportedInterfaceTypes(hidl_utils::fill(&supported)).isOk()) return false; - return supported.contains(iftype); -} - static int up(const std::string& busName, ICanController::InterfaceType type, const std::string& interface, uint32_t bitrate) { bool anySupported = false; - for (auto&& service : getControlServices()) { + for (auto&& service : libcanhaltools::getControlServices()) { auto ctrl = ICanController::getService(service); if (ctrl == nullptr) { std::cerr << "Couldn't open ICanController/" << service; continue; } - if (!isSupported(ctrl, type)) continue; + if (!libcanhaltools::isSupported(ctrl, type)) continue; anySupported = true; ICanController::BusConfig config = {}; @@ -111,7 +95,7 @@ static int up(const std::string& busName, ICanController::InterfaceType type, } static int down(const std::string& busName) { - for (auto&& service : getControlServices()) { + for (auto&& service : libcanhaltools::getControlServices()) { auto ctrl = ICanController::getService(service); if (ctrl == nullptr) continue; diff --git a/automotive/can/1.0/tools/configurator/Android.bp b/automotive/can/1.0/tools/configurator/Android.bp new file mode 100644 index 0000000000..2c4bc1d6fc --- /dev/null +++ b/automotive/can/1.0/tools/configurator/Android.bp @@ -0,0 +1,34 @@ +// +// Copyright (C) 2020 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. +// + +cc_binary { + name: "canhalconfigurator", + init_rc: ["canhalconfigurator.rc"], + defaults: ["android.hardware.automotive.can@defaults"], + srcs: [ + "canhalconfigurator.cpp", + "canprototools.cpp", + ], + shared_libs: [ + "android.hardware.automotive.can@1.0", + "libhidlbase", + "libprotobuf-cpp-full", + ], + static_libs: [ + "android.hardware.automotive.can@1.x-config-format", + "android.hardware.automotive.can@libcanhaltools", + ], +} diff --git a/automotive/can/1.0/tools/configurator/canhalconfigurator.cpp b/automotive/can/1.0/tools/configurator/canhalconfigurator.cpp new file mode 100644 index 0000000000..a100f06f1b --- /dev/null +++ b/automotive/can/1.0/tools/configurator/canhalconfigurator.cpp @@ -0,0 +1,103 @@ +/* + * Copyright (C) 2020 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 "canbus_config.pb.h" +#include "canprototools.h" + +#include +#include +#include + +#include +#include + +namespace android::hardware::automotive::can { + +using ICanController = V1_0::ICanController; + +/** + * Takes output from parsed protobuf config and uses it to configure the CAN HAL. + * + * \param pb_cfg is an instance of the autogenerated protobuf object for our configuration. + * \return boolean status, true on success, false on failure. + */ +static bool processPbCfg(const config::CanBusConfig& pb_cfg) { + for (auto const& bus : pb_cfg.buses()) { + if (bus.name().empty()) { + LOG(ERROR) << "Invalid config: Bus config must have a valid name field"; + return false; + } + + LOG(INFO) << "Configure " << bus.name(); + auto bus_cfg = config::fromPbBus(bus); + if (!bus_cfg.has_value()) { + return false; + } + + // TODO(149405589): remove this sleep and associated includes. + std::this_thread::sleep_for(std::chrono::seconds(1)); + if (libcanhaltools::configureIface(*bus_cfg) != ICanController::Result::OK) { + LOG(ERROR) << "No controller supports " << bus.name() << std::endl; + // TODO(149405589): add retry logic in case a bus fails to come up. + continue; + } + LOG(INFO) << bus.name() << " has been successfully configured!"; + } + return true; +} + +/** + * This kicks off the CAN HAL configuration process. This starts the following: + * 1. Reading the config file + * 2. Setting up CAN buses + * 3. Handling services + * \param filepath is a string specifying the absolute path of the config file + * \return boolean status, true on success, false on failure + */ +static bool configuratorStart(const std::string& filepath) { + base::SetDefaultTag("CanConfigurator"); + + auto pb_cfg = config::parseConfigFile(filepath); + if (!pb_cfg.has_value()) { + return false; + } + + // process the rest of the config file data and configure the CAN buses. + if (!processPbCfg(*pb_cfg)) { + return false; + } + LOG(INFO) << "CAN HAL has been configured!"; + return true; +} + +} // namespace android::hardware::automotive::can + +int main(int argc, char* argv[]) { + std::string config_filepath = "/etc/canbus_config.pb"; + + // allow for CLI specification of a config file. + if (argc == 2) { + config_filepath = argv[1]; + } else if (argc > 2) { + std::cerr << "usage: " << argv[0] << " [optional config filepath]"; + return 1; + } + + if (!::android::hardware::automotive::can::configuratorStart(config_filepath)) { + return 1; + } + return 0; +} diff --git a/automotive/can/1.0/tools/configurator/canhalconfigurator.rc b/automotive/can/1.0/tools/configurator/canhalconfigurator.rc new file mode 100644 index 0000000000..12c24652dd --- /dev/null +++ b/automotive/can/1.0/tools/configurator/canhalconfigurator.rc @@ -0,0 +1,3 @@ +service canhalconfigurator /system/bin/canhalconfigurator + class core + oneshot diff --git a/automotive/can/1.0/tools/configurator/canprototools.cpp b/automotive/can/1.0/tools/configurator/canprototools.cpp new file mode 100644 index 0000000000..8e6b2b1d45 --- /dev/null +++ b/automotive/can/1.0/tools/configurator/canprototools.cpp @@ -0,0 +1,152 @@ +/* + * Copyright (C) 2020 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 "canprototools.h" + +#include +#include +#include +#include +#include + +#include + +namespace android::hardware::automotive::can::config { + +using ICanController = V1_0::ICanController; + +/** + * Helper function for parseConfigFile. readString is used to get the fist n characters (n) from an + * istream object (s) and return it as a string object. + * + * \param s istream of the file you intend to read. + * \param n streamsize object of the number of characters you'd like. + * \return optional string containing up to n characters from the stream(s) you provided. + */ +static std::optional readString(std::istream& s, std::streamsize n) { + char buff[n]; + auto got = s.read(buff, n).gcount(); + if (!s.good() && !s.eof()) return std::nullopt; + return std::string(buff, 0, std::min(n, got)); +} + +std::optional parseConfigFile(const std::string& filepath) { + std::ifstream cfg_stream(filepath); + + // text headers that would be present in a plaintext proto config file. + static const std::array text_headers = {"buses", "#", "controller"}; + auto cfg_file_snippet = readString(cfg_stream, 10); + + if (!cfg_file_snippet.has_value()) { + LOG(ERROR) << "Can't open " << filepath << " for reading"; + return std::nullopt; + } + cfg_stream.seekg(0); + + // check if any of the textHeaders are at the start of the config file. + bool text_format = false; + for (auto const& header : text_headers) { + if (cfg_file_snippet->compare(0, header.length(), header) == 0) { + text_format = true; + break; + } + } + + CanBusConfig config; + if (text_format) { + google::protobuf::io::IstreamInputStream pb_stream(&cfg_stream); + if (!google::protobuf::TextFormat::Parse(&pb_stream, &config)) { + LOG(ERROR) << "Failed to parse (text format) " << filepath; + return std::nullopt; + } + } else if (!config.ParseFromIstream(&cfg_stream)) { + LOG(ERROR) << "Failed to parse (binary format) " << filepath; + return std::nullopt; + } + return config; +} + +std::optional fromPbBus(const Bus& pb_bus) { + ICanController::BusConfig bus_cfg = {}; + bus_cfg.name = pb_bus.name(); + + switch (pb_bus.iface_type_case()) { + case Bus::kNative: { + const auto ifname = pb_bus.native().ifname(); + if (ifname.empty()) { + LOG(ERROR) << "Invalid config: native type bus must have an iface name"; + return std::nullopt; + } + bus_cfg.bitrate = pb_bus.bitrate(); + ICanController::BusConfig::InterfaceId::Socketcan socketcan = {}; + socketcan.ifname(ifname); + bus_cfg.interfaceId.socketcan(socketcan); + // TODO(b/142654031) - add support for serial number as an option instead of ifname. + break; + } + case Bus::kSlcan: { + const auto ttyname = pb_bus.slcan().ttyname(); + if (ttyname.empty()) { + LOG(ERROR) << "Invalid config: slcan type bus must have a tty name"; + return std::nullopt; + } + bus_cfg.bitrate = pb_bus.bitrate(); + ICanController::BusConfig::InterfaceId::Slcan slcan = {}; + slcan.ttyname(pb_bus.slcan().ttyname()); + bus_cfg.interfaceId.slcan(slcan); + break; + } + case Bus::kVirtual: { + // Theoretically, we could just create the next available vcan iface. + const auto ifname = pb_bus.virtual_().ifname(); + if (ifname.empty()) { + LOG(ERROR) << "Invalid config: native type bus must have an iface name"; + return std::nullopt; + } + bus_cfg.interfaceId.virtualif({ifname}); + break; + } + case Bus::kIndexed: { + const auto index = pb_bus.indexed().index(); + if (index > UINT8_MAX) { + LOG(ERROR) << "Interface index out of range: " << index; + return std::nullopt; + } + bus_cfg.interfaceId.indexed({uint8_t(index)}); + break; + } + default: + LOG(ERROR) << "Invalid config: bad interface type for " << bus_cfg.name; + return std::nullopt; + } + return bus_cfg; +} + +std::optional getHalIftype(const Bus& pb_bus) { + switch (pb_bus.iface_type_case()) { + case Bus::kNative: + return ICanController::InterfaceType::SOCKETCAN; + case Bus::kSlcan: + return ICanController::InterfaceType::SLCAN; + case Bus::kVirtual: + return ICanController::InterfaceType::VIRTUAL; + case Bus::kIndexed: + return ICanController::InterfaceType::INDEXED; + default: + return std::nullopt; + } +} + +} // namespace android::hardware::automotive::can::config diff --git a/automotive/can/1.0/tools/configurator/canprototools.h b/automotive/can/1.0/tools/configurator/canprototools.h new file mode 100644 index 0000000000..b7f2b6f0d2 --- /dev/null +++ b/automotive/can/1.0/tools/configurator/canprototools.h @@ -0,0 +1,49 @@ +/* + * Copyright (C) 2020 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. + */ +#pragma once + +#include "canbus_config.pb.h" + +#include + +namespace android::hardware::automotive::can::config { + +/** + * This reads the protobuf config file into a protobuf object. Both text based protobuf files as + * well as binary format protobuf files are supported. + * + * \param filepath string containing the name of the config file to read. + * \return a CanBusConfig protobuf object constructed from the config file. + */ +std::optional parseConfigFile(const std::string& filepath); + +/** + * Converts protobuf format single-bus config object to a HAL bus config object. + * + * \param pb_bus is the protobuf object representing a the configuration of one CAN bus. + * \return a converted HAL bus config object. + */ +std::optional fromPbBus(const Bus& pb_bus); + +/** + * Get the CAN HAL interface type specified by a given protobuf config object. + * + * \param pb_bus is the protobuf object representing a the configuration of one CAN bus. + * \return the CAN HAL interface type. + */ +std::optional getHalIftype(const Bus& pb_bus); + +} // namespace android::hardware::automotive::can::config diff --git a/automotive/can/1.0/tools/configurator/proto/Android.bp b/automotive/can/1.0/tools/configurator/proto/Android.bp new file mode 100644 index 0000000000..05e120524a --- /dev/null +++ b/automotive/can/1.0/tools/configurator/proto/Android.bp @@ -0,0 +1,28 @@ +// +// Copyright (C) 2020 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. +// + +cc_library_static { + name: "android.hardware.automotive.can@1.x-config-format", + defaults: ["android.hardware.automotive.can@defaults"], + proto: { + export_proto_headers: true, + type: "full", + }, + strip: { + keep_symbols: true, + }, + srcs: ["canbus_config.proto"], +} diff --git a/automotive/can/1.0/tools/configurator/proto/canbus_config.proto b/automotive/can/1.0/tools/configurator/proto/canbus_config.proto new file mode 100644 index 0000000000..9aa33aceb5 --- /dev/null +++ b/automotive/can/1.0/tools/configurator/proto/canbus_config.proto @@ -0,0 +1,52 @@ +/* + * Copyright (C) 2020 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. + */ + +syntax = "proto3"; + +package android.hardware.automotive.can.config; + +message IfaceNative { + string ifname = 1; + repeated string serialno = 2; +}; + +message IfaceSlcan { + string ttyname = 1; + repeated string serialno = 2; +}; + +message IfaceVirtual { + string ifname = 1; +}; + +message IfaceIndexed { + uint32 index = 1; +}; + +message Bus { + string name = 1; // this is the name presented in the HAL + oneof iface_type { + IfaceNative native = 2; + IfaceSlcan slcan = 3; + IfaceVirtual virtual = 4; + IfaceIndexed indexed = 5; + } + uint32 bitrate = 6; +}; + +message CanBusConfig { + repeated Bus buses = 1; +}; diff --git a/automotive/can/1.0/tools/libcanhaltools/Android.bp b/automotive/can/1.0/tools/libcanhaltools/Android.bp new file mode 100644 index 0000000000..cee9eef20d --- /dev/null +++ b/automotive/can/1.0/tools/libcanhaltools/Android.bp @@ -0,0 +1,32 @@ +// +// Copyright (C) 2020 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. +// + +cc_library_static { + name: "android.hardware.automotive.can@libcanhaltools", + defaults: ["android.hardware.automotive.can@defaults"], + vendor_available: true, + srcs: [ + "libcanhaltools.cpp", + ], + export_include_dirs: ["include"], + shared_libs: [ + "android.hardware.automotive.can@1.0", + "libhidlbase", + ], + header_libs: [ + "android.hardware.automotive.can@hidl-utils-lib", + ], +} diff --git a/automotive/can/1.0/tools/libcanhaltools/include/libcanhaltools/libcanhaltools.h b/automotive/can/1.0/tools/libcanhaltools/include/libcanhaltools/libcanhaltools.h new file mode 100644 index 0000000000..bbd1fe5873 --- /dev/null +++ b/automotive/can/1.0/tools/libcanhaltools/include/libcanhaltools/libcanhaltools.h @@ -0,0 +1,48 @@ +/* + * Copyright (C) 2020 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. + */ + +#pragma once + +#include +#include + +namespace android::hardware::automotive::can::libcanhaltools { + +/** + * Fetch the list of registered can controller services. + * + * \return list of service names identifying the registered can controllers. + */ +hidl_vec getControlServices(); + +/** + * Determine if an can controller supports a specific interface type. + * + * \param ctrl a pointer to a can controller instance to check for interface support. + * \param iftype the interface type we wish to check if ctrl supports. + * \return true if iftype is supported by ctrl, false if not supported. + */ +bool isSupported(sp ctrl, V1_0::ICanController::InterfaceType iftype); + +/** + * Configures a CAN interface through the CAN HAL and brings it up. + * + * \param can_config this holds the parameters for configuring a CAN bus. + * \return status passed back from the CAN HAL, should be OK on success. + */ +V1_0::ICanController::Result configureIface(V1_0::ICanController::BusConfig can_config); + +} // namespace android::hardware::automotive::can::libcanhaltools diff --git a/automotive/can/1.0/tools/libcanhaltools/libcanhaltools.cpp b/automotive/can/1.0/tools/libcanhaltools/libcanhaltools.cpp new file mode 100644 index 0000000000..9192e2f52d --- /dev/null +++ b/automotive/can/1.0/tools/libcanhaltools/libcanhaltools.cpp @@ -0,0 +1,85 @@ +/* + * Copyright (C) 2020 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 "libcanhaltools/libcanhaltools.h" + +#include +#include +#include +#include + +#include +#include + +namespace android::hardware::automotive::can::libcanhaltools { + +using ICanBus = V1_0::ICanBus; +using ICanController = V1_0::ICanController; +using IfIdDisc = ICanController::BusConfig::InterfaceId::hidl_discriminator; + +hidl_vec getControlServices() { + auto manager = hidl::manager::V1_2::IServiceManager::getService(); + hidl_vec services; + manager->listManifestByInterface(ICanController::descriptor, hidl_utils::fill(&services)); + CHECK(services.size() > 0) << "No ICanController services registered (missing privileges?)" + << std::endl; + return services; +} + +bool isSupported(sp ctrl, ICanController::InterfaceType iftype) { + hidl_vec supported; + if (!ctrl->getSupportedInterfaceTypes(hidl_utils::fill(&supported)).isOk()) return false; + return supported.contains(iftype); +} + +ICanController::InterfaceType getIftype(ICanController::BusConfig can_config) { + switch (can_config.interfaceId.getDiscriminator()) { + case IfIdDisc::socketcan: + return ICanController::InterfaceType::SOCKETCAN; + case IfIdDisc::slcan: + return ICanController::InterfaceType::SLCAN; + case IfIdDisc::virtualif: + return ICanController::InterfaceType::VIRTUAL; + case IfIdDisc::indexed: + return ICanController::InterfaceType::INDEXED; + default: + CHECK(false) << "HAL returned unexpected interface type!"; + } +} + +ICanController::Result configureIface(ICanController::BusConfig can_config) { + auto iftype = getIftype(can_config); + auto can_controller_list = getControlServices(); + for (auto const& service : can_controller_list) { + auto ctrl = ICanController::getService(service); + if (ctrl == nullptr) { + LOG(ERROR) << "Couldn't open ICanController/" << service; + continue; + } + + if (!libcanhaltools::isSupported(ctrl, iftype)) continue; + + const auto up_result = ctrl->upInterface(can_config); + if (up_result != ICanController::Result::OK) { + LOG(ERROR) << "Failed to bring " << can_config.name << " up: " << toString(up_result) + << std::endl; + } + return up_result; + } + return ICanController::Result::NOT_SUPPORTED; +} + +} // namespace android::hardware::automotive::can::libcanhaltools