diff --git a/src/include/CanDevice.h b/src/include/CanDevice.h index fe429e22..a2257f5a 100644 --- a/src/include/CanDevice.h +++ b/src/include/CanDevice.h @@ -4,6 +4,7 @@ #include #include #include +#include #include #include @@ -72,6 +73,19 @@ struct CanDevice { std::string_view vendor, const CanDeviceArguments& configuration); protected: + /** + * @brief Logs a warning for every configuration parameter that was + * provided but is not in the vendor's list of accepted parameters. + * + * @param vendor The name of the vendor the parameters are checked against. + * @param config The configuration provided by the user. + * @param accepted_parameters The names of the parameters the vendor takes + * into account. + */ + static void warn_ignored_parameters( + std::string_view vendor, const CanDeviceConfiguration& config, + const std::set& accepted_parameters) noexcept; + /** * @brief Constructor for the CanDevice class. * diff --git a/src/include/CanDeviceConfiguration.h b/src/include/CanDeviceConfiguration.h index be194608..12bc3b24 100644 --- a/src/include/CanDeviceConfiguration.h +++ b/src/include/CanDeviceConfiguration.h @@ -6,6 +6,8 @@ #include #include #include +#include +#include /** * @brief Configuration structure for a CanDevice. @@ -109,6 +111,12 @@ struct CanDeviceConfiguration { std::optional sent_acknowledgement; std::string to_string() const noexcept; + + /** + * @brief The name and value of every parameter that was provided. + */ + std::vector> set_parameters() + const noexcept; }; std::ostream& operator<<(std::ostream& os, diff --git a/src/include/CanVendorAnagate.h b/src/include/CanVendorAnagate.h index b1f9a674..251f9cbe 100644 --- a/src/include/CanVendorAnagate.h +++ b/src/include/CanVendorAnagate.h @@ -5,6 +5,8 @@ #include #include #include //NOLINT +#include +#include #include #include "AnaGateDllCan.h" @@ -24,6 +26,11 @@ struct CanVendorAnagate : CanDevice { AnaInt32 nBufferLen, AnaInt32 nFlags, AnaInt32 hHandle) noexcept; + /** + * @brief The configuration parameters this vendor takes into account. + */ + static const std::set accepted_parameters; + explicit CanVendorAnagate(const CanDeviceArguments& configuration); inline ~CanVendorAnagate() override { vendor_close(); } diff --git a/src/include/CanVendorSocketCan.h b/src/include/CanVendorSocketCan.h index 591a0f82..dae346b0 100644 --- a/src/include/CanVendorSocketCan.h +++ b/src/include/CanVendorSocketCan.h @@ -7,6 +7,8 @@ #include #include +#include +#include #include // NOLINT #include "CanDevice.h" @@ -22,6 +24,11 @@ * methods to open, close, and send CAN frames using the SocketCAN interface. */ struct CanVendorSocketCan : CanDevice { + /** + * @brief The configuration parameters this vendor takes into account. + */ + static const std::set accepted_parameters; + explicit CanVendorSocketCan(const CanDeviceArguments& args); ~CanVendorSocketCan() { vendor_close(); } diff --git a/src/include/CanVendorSocketCanSystec.h b/src/include/CanVendorSocketCanSystec.h index 396fd94a..b7bef603 100644 --- a/src/include/CanVendorSocketCanSystec.h +++ b/src/include/CanVendorSocketCanSystec.h @@ -2,6 +2,8 @@ #define SRC_INCLUDE_CANVENDORSOCKETCANSYSTEC_H_ #include +#include +#include #include "CanDevice.h" #include "CanVendorSocketCan.h" @@ -18,6 +20,11 @@ * SocketCan due to a kernel-panic bug on Systec linux module. */ struct CanVendorSocketCanSystec : CanDevice { + /** + * @brief The configuration parameters this vendor takes into account. + */ + static const std::set accepted_parameters; + explicit CanVendorSocketCanSystec(const CanDeviceArguments& args); ~CanVendorSocketCanSystec() { vendor_close(); } diff --git a/src/main/CanDevice.cpp b/src/main/CanDevice.cpp index 204b70ea..3884d29d 100644 --- a/src/main/CanDevice.cpp +++ b/src/main/CanDevice.cpp @@ -2,6 +2,7 @@ #include #include +#include #include #include #include @@ -15,6 +16,18 @@ #include "CanVendorSocketCanSystec.h" #endif +void CanDevice::warn_ignored_parameters( + std::string_view vendor, const CanDeviceConfiguration& config, + const std::set& accepted_parameters) noexcept { + for (const auto& [name, value] : config.set_parameters()) { + if (accepted_parameters.count(name) == 0) { + LOG(Log::WRN, CanLogIt::h()) + << "Ignoring configuration parameter " << name << "=" << value + << ": it is not accepted by vendor " << vendor; + } + } +} + /** * @brief Opens the CAN device for communication. * @@ -156,17 +169,23 @@ std::unique_ptr CanDevice::create( #ifndef _WIN32 if (vendor == "socketcan") { LOG(Log::DBG, CanLogIt::h()) << "Creating SocketCAN CAN device"; + warn_ignored_parameters(vendor, configuration.config, + CanVendorSocketCan::accepted_parameters); return std::make_unique(configuration); } if (vendor == "socketcan_systec") { LOG(Log::DBG, CanLogIt::h()) << "Creating SocketCAN Systec CAN device"; + warn_ignored_parameters(vendor, configuration.config, + CanVendorSocketCanSystec::accepted_parameters); return std::make_unique(configuration); } #endif if (vendor == "anagate") { LOG(Log::DBG, CanLogIt::h()) << "Creating Anagate CAN device"; + warn_ignored_parameters(vendor, configuration.config, + CanVendorAnagate::accepted_parameters); return std::make_unique(configuration); } diff --git a/src/main/CanDeviceConfiguration.cpp b/src/main/CanDeviceConfiguration.cpp index 5244e1bd..b2bcb6f8 100644 --- a/src/main/CanDeviceConfiguration.cpp +++ b/src/main/CanDeviceConfiguration.cpp @@ -9,6 +9,8 @@ #include #include #include +#include +#include namespace { @@ -190,3 +192,37 @@ std::ostream& operator<<(std::ostream& os, const CanDeviceConfiguration& config) noexcept { return os << config.to_string(); } + +/** + * @brief Lists the name and value of every parameter that was provided. + * + * @return A vector of (name, value) pairs, one for each optional field that + * holds a value. + */ +std::vector> +CanDeviceConfiguration::set_parameters() const noexcept { + std::vector> parameters; + + if (bus_name.has_value()) + parameters.emplace_back("bus_name", bus_name.value()); + if (bus_number.has_value()) + parameters.emplace_back("bus_number", std::to_string(bus_number.value())); + if (host.has_value()) parameters.emplace_back("host", host.value()); + if (bitrate.has_value()) + parameters.emplace_back("bitrate", std::to_string(bitrate.value())); + if (enable_termination.has_value()) + parameters.emplace_back("enable_termination", + enable_termination.value() ? "true" : "false"); + if (high_speed.has_value()) + parameters.emplace_back("high_speed", + high_speed.value() ? "true" : "false"); + if (timeout.has_value()) + parameters.emplace_back("timeout", std::to_string(timeout.value())); + if (vcan.has_value()) + parameters.emplace_back("vcan", vcan.value() ? "true" : "false"); + if (sent_acknowledgement.has_value()) + parameters.emplace_back("sent_acknowledgement", + std::to_string(sent_acknowledgement.value())); + + return parameters; +} diff --git a/src/main/CanVendorAnagate.cpp b/src/main/CanVendorAnagate.cpp index 40af348d..c58c5ea1 100644 --- a/src/main/CanVendorAnagate.cpp +++ b/src/main/CanVendorAnagate.cpp @@ -6,6 +6,7 @@ #include #include #include // NOLINT +#include #include #include #include @@ -15,6 +16,12 @@ std::mutex CanVendorAnagate::m_handles_lock; std::map CanVendorAnagate::m_handles; +const std::set CanVendorAnagate::accepted_parameters = { + "bus_number", "host", + "bitrate", "enable_termination", + "high_speed", "sent_acknowledgement", + "timeout"}; + /** * @brief Callback function to handle incoming CAN frames from the AnaGate DLL. * diff --git a/src/main/CanVendorSocketCan.cpp b/src/main/CanVendorSocketCan.cpp index 1078c4ee..d6cf9dea 100644 --- a/src/main/CanVendorSocketCan.cpp +++ b/src/main/CanVendorSocketCan.cpp @@ -11,6 +11,8 @@ #include #include +#include +#include #include // NOLINT #include @@ -21,6 +23,9 @@ constexpr auto EPOLL_WAIT_CYCLE_MS = 1000; constexpr auto LIBSOCKETCAN_ERROR = -1; constexpr auto LIBSOCKETCAN_SUCCESS = 0; +const std::set CanVendorSocketCan::accepted_parameters = { + "bus_name", "bitrate", "vcan", "timeout"}; + /** * @brief Constructor for the CanVendorSocketCan class. * diff --git a/src/main/CanVendorSocketCanSystec.cpp b/src/main/CanVendorSocketCanSystec.cpp index dc9d7818..b8ff0604 100644 --- a/src/main/CanVendorSocketCanSystec.cpp +++ b/src/main/CanVendorSocketCanSystec.cpp @@ -4,6 +4,12 @@ #include #include +#include +#include + +const std::set CanVendorSocketCanSystec::accepted_parameters = + CanVendorSocketCan::accepted_parameters; + /** * @brief Constructor for the CanVendorSocketCanSystec class. *