Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/include/CanDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <functional>
#include <map>
#include <memory>
#include <set>
#include <string>
#include <vector>

Expand Down Expand Up @@ -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<std::string>& accepted_parameters) noexcept;

/**
* @brief Constructor for the CanDevice class.
*
Expand Down
8 changes: 8 additions & 0 deletions src/include/CanDeviceConfiguration.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include <map>
#include <optional>
#include <string>
#include <utility>
#include <vector>

/**
* @brief Configuration structure for a CanDevice.
Expand Down Expand Up @@ -109,6 +111,12 @@ struct CanDeviceConfiguration {
std::optional<uint32_t> sent_acknowledgement;

std::string to_string() const noexcept;

/**
* @brief The name and value of every parameter that was provided.
*/
std::vector<std::pair<std::string, std::string>> set_parameters()
const noexcept;
};

std::ostream& operator<<(std::ostream& os,
Expand Down
7 changes: 7 additions & 0 deletions src/include/CanVendorAnagate.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include <cstdint>
#include <map>
#include <mutex> //NOLINT
#include <set>
#include <string>
#include <thread>

#include "AnaGateDllCan.h"
Expand All @@ -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<std::string> accepted_parameters;

explicit CanVendorAnagate(const CanDeviceArguments& configuration);
inline ~CanVendorAnagate() override { vendor_close(); }

Expand Down
7 changes: 7 additions & 0 deletions src/include/CanVendorSocketCan.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include <unistd.h>

#include <cstring>
#include <set>
#include <string>
#include <thread> // NOLINT

#include "CanDevice.h"
Expand All @@ -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<std::string> accepted_parameters;

explicit CanVendorSocketCan(const CanDeviceArguments& args);

~CanVendorSocketCan() { vendor_close(); }
Expand Down
7 changes: 7 additions & 0 deletions src/include/CanVendorSocketCanSystec.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#define SRC_INCLUDE_CANVENDORSOCKETCANSYSTEC_H_

#include <memory>
#include <set>
#include <string>

#include "CanDevice.h"
#include "CanVendorSocketCan.h"
Expand All @@ -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<std::string> accepted_parameters;

explicit CanVendorSocketCanSystec(const CanDeviceArguments& args);
~CanVendorSocketCanSystec() { vendor_close(); }

Expand Down
19 changes: 19 additions & 0 deletions src/main/CanDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <algorithm>
#include <memory>
#include <set>
#include <stdexcept>
#include <string>
#include <vector>
Expand All @@ -15,6 +16,18 @@
#include "CanVendorSocketCanSystec.h"
#endif

void CanDevice::warn_ignored_parameters(
std::string_view vendor, const CanDeviceConfiguration& config,
const std::set<std::string>& 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.
*
Expand Down Expand Up @@ -156,17 +169,23 @@ std::unique_ptr<CanDevice> 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<CanVendorSocketCan>(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<CanVendorSocketCanSystec>(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<CanVendorAnagate>(configuration);
}

Expand Down
36 changes: 36 additions & 0 deletions src/main/CanDeviceConfiguration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include <sstream>
#include <stdexcept>
#include <string>
#include <utility>
#include <vector>

namespace {

Expand Down Expand Up @@ -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<std::pair<std::string, std::string>>
CanDeviceConfiguration::set_parameters() const noexcept {
std::vector<std::pair<std::string, std::string>> 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;
}
7 changes: 7 additions & 0 deletions src/main/CanVendorAnagate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <map>
#include <memory>
#include <mutex> // NOLINT
#include <set>
#include <sstream>
#include <string>
#include <vector>
Expand All @@ -15,6 +16,12 @@
std::mutex CanVendorAnagate::m_handles_lock;
std::map<int, CanVendorAnagate*> CanVendorAnagate::m_handles;

const std::set<std::string> 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.
*
Expand Down
5 changes: 5 additions & 0 deletions src/main/CanVendorSocketCan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#include <unistd.h>

#include <cstring>
#include <set>
#include <string>
#include <thread> // NOLINT
#include <vector>

Expand All @@ -21,6 +23,9 @@ constexpr auto EPOLL_WAIT_CYCLE_MS = 1000;
constexpr auto LIBSOCKETCAN_ERROR = -1;
constexpr auto LIBSOCKETCAN_SUCCESS = 0;

const std::set<std::string> CanVendorSocketCan::accepted_parameters = {
"bus_name", "bitrate", "vcan", "timeout"};

/**
* @brief Constructor for the CanVendorSocketCan class.
*
Expand Down
6 changes: 6 additions & 0 deletions src/main/CanVendorSocketCanSystec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
#include <LogIt.h>
#include <linux/can/error.h>

#include <set>
#include <string>

const std::set<std::string> CanVendorSocketCanSystec::accepted_parameters =
CanVendorSocketCan::accepted_parameters;

/**
* @brief Constructor for the CanVendorSocketCanSystec class.
*
Expand Down
Loading