Merge "CAN Configurator Service" into rvc-dev

This commit is contained in:
Chris Weir
2020-03-26 22:28:11 +00:00
committed by Android (Google) Code Review
12 changed files with 599 additions and 20 deletions

View File

@@ -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",
],
}

View File

@@ -18,6 +18,7 @@
#include <android/hardware/automotive/can/1.0/ICanController.h>
#include <android/hidl/manager/1.2/IServiceManager.h>
#include <hidl-utils/hidl-utils.h>
#include <libcanhaltools/libcanhaltools.h>
#include <iostream>
#include <string>
@@ -41,34 +42,17 @@ static void usage() {
std::cerr << " bus name - name under which ICanBus will be published" << std::endl;
}
static hidl_vec<hidl_string> getControlServices() {
auto manager = hidl::manager::V1_2::IServiceManager::getService();
hidl_vec<hidl_string> 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<ICanController> ctrl, ICanController::InterfaceType iftype) {
hidl_vec<ICanController::InterfaceType> 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;

View File

@@ -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",
],
}

View File

@@ -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 <android-base/logging.h>
#include <android/hardware/automotive/can/1.0/ICanController.h>
#include <libcanhaltools/libcanhaltools.h>
#include <chrono>
#include <thread>
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;
}

View File

@@ -0,0 +1,3 @@
service canhalconfigurator /system/bin/canhalconfigurator
class core
oneshot

View File

@@ -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 <android-base/logging.h>
#include <google/protobuf/io/zero_copy_stream_impl.h>
#include <google/protobuf/text_format.h>
#include <hidl/HidlTransportSupport.h>
#include <libcanhaltools/libcanhaltools.h>
#include <fstream>
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<std::string> 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<CanBusConfig> 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<std::string, 3> 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<ICanController::BusConfig> 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<ICanController::InterfaceType> 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

View File

@@ -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 <android/hardware/automotive/can/1.0/ICanController.h>
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<CanBusConfig> 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<V1_0::ICanController::BusConfig> 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<V1_0::ICanController::InterfaceType> getHalIftype(const Bus& pb_bus);
} // namespace android::hardware::automotive::can::config

View File

@@ -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"],
}

View File

@@ -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;
};

View File

@@ -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",
],
}

View File

@@ -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 <android/hardware/automotive/can/1.0/ICanBus.h>
#include <android/hardware/automotive/can/1.0/ICanController.h>
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<hidl_string> 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<V1_0::ICanController> 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

View File

@@ -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 <android-base/logging.h>
#include <android/hardware/automotive/can/1.0/ICanController.h>
#include <android/hidl/manager/1.2/IServiceManager.h>
#include <hidl-utils/hidl-utils.h>
#include <iostream>
#include <string>
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<hidl_string> getControlServices() {
auto manager = hidl::manager::V1_2::IServiceManager::getService();
hidl_vec<hidl_string> 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<ICanController> ctrl, ICanController::InterfaceType iftype) {
hidl_vec<ICanController::InterfaceType> 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