Nix (Dev) 3.5.10
dev - 3.5.10 - 1af9301
Loading...
Searching...
No Matches
ISocketCANInterface.hpp
Go to the documentation of this file.
1#ifndef IV_SRC_COMMS_PEAK_NETDEV_IPEAKINTERFACENETDEV_HPP_
2#define IV_SRC_COMMS_PEAK_NETDEV_IPEAKINTERFACENETDEV_HPP_
3
6#include "core/system.hpp"
7#include "core/Timestamp.hpp"
8#include "core/utils.hpp"
10
11#include <cstdint>
12#include <iostream>
13
15{
16
17const std::string PEAK_MODULE_NAME = "peak_usb";
18const std::string IXXAT_MODULE_NAME = "ix_usb_can";
19const std::string COMMAND_PEAK_MODULE_IS_LOADED = "lsmod | grep " + PEAK_MODULE_NAME;
20const std::string COMMAND_PEAK_MODULE_LOAD = "sudo -n modprobe " + PEAK_MODULE_NAME;
21const std::string COMMAND_IXXAT_MODULE_IS_LOADED = "lsmod | grep " + IXXAT_MODULE_NAME;
22const std::string COMMAND_IXXAT_MODULE_LOAD = "sudo -n modprobe " + IXXAT_MODULE_NAME;
23const std::string COMMAND_INTERFACE_EXISTS = "ip link show ";
24const std::string COMMAND_INTERFACE_UP = "cat /sys/class/net/{}/operstate";
25const std::string COMMAND_INTERFACE_SET = "sudo -n ip link set ";
26const std::string COMMAND_INTERFACE_SET_BITRATE = "sudo -n ip link set {} type can bitrate {}";
27
34{
35public:
36 virtual ~ISocketCANInterface() = default;
37
38 virtual void *ctrlOpen(const char *deviceId, iv::model::comms::socketcan::eBaudRate baudRate) = 0;
39 virtual uint32_t ctrlInit(void *socket, iv::model::comms::socketcan::eBaudRate baudRate) = 0;
40 virtual uint32_t ctrlReceive(void *socket, std::shared_ptr<CMessageCan> &pStcMessage) = 0;
41 virtual uint32_t ctrlSend(void *socket, const CMessageCan &pStcMessage) = 0;
42 virtual void ctrlClose(void *socket) = 0;
43
44protected:
46 {
48 {
50 {
51 throw std::runtime_error("Cannot load kernel module " + PEAK_MODULE_NAME);
52 }
53 }
54 }
55
57 {
59 {
61 {
62 throw std::runtime_error("Cannot load kernel module " + IXXAT_MODULE_NAME);
63 }
64 }
65 }
66
67 static bool prvExistsInterface(const std::string &deviceId)
68 {
69 return not iv::system::executeCommand(COMMAND_INTERFACE_EXISTS + " " + deviceId).empty();
70 }
71
72 static bool prvIsInterfaceUp(const std::string &deviceId)
73 {
74 static std::map<std::string, iv::types::timestamp> lastCheckedInterfaces {};
75
77
78 if (not lastCheckedInterfaces.contains(deviceId) or iv::time::utils::safeAbsoluteDiff(lastCheckedInterfaces.at(deviceId), iv::time::Timestamp().inMilliseconds()) > 500)
79 {
80 bool result = iv::system::executeCommand(fmt::format(fmt::runtime(COMMAND_INTERFACE_UP), deviceId)).find("up") !=
81 std::string::npos;
82
83 if (not result)
84 {
85 lastCheckedInterfaces.erase(deviceId);
86 return result;
87 }
88
89 lastCheckedInterfaces.insert_or_assign(deviceId, now);
90 }
91
92 return true;
93
94 }
95
96 static bool prvSetInterface(const std::string &deviceId, bool up)
97 {
98 std::string commandResult =
99 iv::system::executeCommand(COMMAND_INTERFACE_SET + " " + deviceId + (up ? " up" : " down"));
100
101 if (commandResult.find("Cannot find device") != std::string::npos)
102 {
103#ifndef NDEBUG
104 std::cout << "Cannot find device " << deviceId
105 << ". Probably the device is not connected or the device id is wrong." << std::endl;
106#endif
107 }
108 else if (commandResult.find("argument") != std::string::npos)
109 {
110#ifndef NDEBUG
111 std::cout << "Cannot set interface " << deviceId << (up ? "up" : "down")
112 << ". Probably the device is not connected or the device id is wrong." << std::endl;
113#endif
114 }
115 else
116 {
117 return true;
118 }
119
120 return false;
121 }
122
123 static bool prvSetBitrate(const std::string &deviceId, uint64_t btr0)
124 {
125 std::string commandResult =
126 iv::system::executeCommand(fmt::format(fmt::runtime(COMMAND_INTERFACE_SET_BITRATE), deviceId, btr0));
127
128 if (commandResult.find("Cannot find device") != std::string::npos)
129 {
130#ifndef NDEBUG
131 std::cout << "Cannot find device " << deviceId
132 << ". Probably the device is not connected or the device id is wrong." << std::endl;
133#endif
134 }
135 else if (commandResult.find("argument") != std::string::npos)
136 {
137#ifndef NDEBUG
138 std::cout << "Cannot set bitrate " << btr0 << " for interface " << deviceId
139 << ". Probably the device is not connected or the device id is wrong." << std::endl;
140#endif
141 }
142 else
143 {
144 return true;
145 }
146
147 return false;
148 }
149
150 std::string m_interfaceName {};
151};
152
153}// namespace iv::comms::socketcan
154
155#endif//IV_SRC_COMMS_PEAK_NETDEV_IPEAKINTERFACENETDEV_HPP_
Definition CMessageCan.hpp:13
Interface for communication with SocketCAN.Designed to be extended by different types of drivers.
Definition ISocketCANInterface.hpp:34
static void prvPeakKernelModuleLoad()
Definition ISocketCANInterface.hpp:45
static bool prvExistsInterface(const std::string &deviceId)
Definition ISocketCANInterface.hpp:67
static bool prvSetInterface(const std::string &deviceId, bool up)
Definition ISocketCANInterface.hpp:96
virtual uint32_t ctrlInit(void *socket, iv::model::comms::socketcan::eBaudRate baudRate)=0
virtual void ctrlClose(void *socket)=0
static bool prvIsInterfaceUp(const std::string &deviceId)
Definition ISocketCANInterface.hpp:72
virtual uint32_t ctrlSend(void *socket, const CMessageCan &pStcMessage)=0
static bool prvSetBitrate(const std::string &deviceId, uint64_t btr0)
Definition ISocketCANInterface.hpp:123
virtual void * ctrlOpen(const char *deviceId, iv::model::comms::socketcan::eBaudRate baudRate)=0
static void prvIxxatKernelModuleLoad()
Definition ISocketCANInterface.hpp:56
std::string m_interfaceName
Definition ISocketCANInterface.hpp:150
virtual uint32_t ctrlReceive(void *socket, std::shared_ptr< CMessageCan > &pStcMessage)=0
Definition Timestamp.hpp:17
iv::types::milliseconds inMilliseconds() const
Definition Timestamp.hpp:136
Definition enums.hpp:11
const std::string COMMAND_INTERFACE_EXISTS
Definition ISocketCANInterface.hpp:23
const std::string COMMAND_IXXAT_MODULE_LOAD
Definition ISocketCANInterface.hpp:22
const std::string PEAK_MODULE_NAME
Definition ISocketCANInterface.hpp:17
const std::string COMMAND_INTERFACE_SET
Definition ISocketCANInterface.hpp:25
const std::string COMMAND_IXXAT_MODULE_IS_LOADED
Definition ISocketCANInterface.hpp:21
const std::string COMMAND_INTERFACE_UP
Definition ISocketCANInterface.hpp:24
const std::string COMMAND_INTERFACE_SET_BITRATE
Definition ISocketCANInterface.hpp:26
const std::string COMMAND_PEAK_MODULE_IS_LOADED
Definition ISocketCANInterface.hpp:19
const std::string IXXAT_MODULE_NAME
Definition ISocketCANInterface.hpp:18
const std::string COMMAND_PEAK_MODULE_LOAD
Definition ISocketCANInterface.hpp:20
eBaudRate
Definition SocketCanSettings.hpp:27
std::string executeCommand(const std::string &command, int *exitCode)
Executes a command and obtains the stdout.
Definition system.cpp:91
iv::types::timestamp safeAbsoluteDiff(const iv::types::timestamp a, const iv::types::timestamp b)
Definition utils.hpp:304
uint64_t timestamp
Definition types.hpp:21