mirror of
https://github.com/eclipse-openvehicle-api/openvehicle-api.git
synced 2026-02-05 15:18:45 +00:00
23
sdv_services/can_communication_socket_can/CMakeLists.txt
Normal file
23
sdv_services/can_communication_socket_can/CMakeLists.txt
Normal file
@@ -0,0 +1,23 @@
|
||||
# Build for linux only
|
||||
if(UNIX)
|
||||
|
||||
# Define project
|
||||
project(can_com_sockets VERSION 1.0 LANGUAGES CXX)
|
||||
|
||||
# Copy the config files
|
||||
file (COPY ${PROJECT_SOURCE_DIR}/config/test_can_communication_sockets.toml DESTINATION ${CMAKE_BINARY_DIR}/tests/bin/config/)
|
||||
|
||||
# Define target
|
||||
add_library(can_com_sockets SHARED "can_com_sockets.h" "can_com_sockets.cpp")
|
||||
target_link_libraries(can_com_sockets ${CMAKE_THREAD_LIBS_INIT})
|
||||
target_link_options(can_com_sockets PRIVATE)
|
||||
|
||||
set_target_properties(can_com_sockets PROPERTIES PREFIX "")
|
||||
set_target_properties(can_com_sockets PROPERTIES SUFFIX ".sdv")
|
||||
|
||||
# Build dependencies
|
||||
add_dependencies(can_com_sockets CompileCoreIDL)
|
||||
|
||||
# Appending the service in the service list
|
||||
set(SDV_Service_List ${SDV_Service_List} can_com_sockets PARENT_SCOPE)
|
||||
endif()
|
||||
339
sdv_services/can_communication_socket_can/can_com_sockets.cpp
Normal file
339
sdv_services/can_communication_socket_can/can_com_sockets.cpp
Normal file
@@ -0,0 +1,339 @@
|
||||
#include "can_com_sockets.h"
|
||||
|
||||
void CCANSockets::Initialize(const sdv::u8string& rssObjectConfig)
|
||||
{
|
||||
std::deque<std::string> vecConfigInterfaces;
|
||||
try
|
||||
{
|
||||
sdv::toml::CTOMLParser config(rssObjectConfig.c_str());
|
||||
sdv::toml::CNodeCollection nodeSource = config.GetDirect("canSockets");
|
||||
if (nodeSource.GetType() == sdv::toml::ENodeType::node_array)
|
||||
{
|
||||
for (size_t nIndex = 0; nIndex < nodeSource.GetCount(); nIndex++)
|
||||
{
|
||||
sdv::toml::CNode nodeInterface = nodeSource[nIndex];
|
||||
if (nodeInterface.GetType() == sdv::toml::ENodeType::node_string)
|
||||
{
|
||||
std::string ssIfcName = nodeInterface.GetValue();
|
||||
if (!ssIfcName.empty())
|
||||
{
|
||||
vecConfigInterfaces.push_back(ssIfcName);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (vecConfigInterfaces.size() == 0)
|
||||
{
|
||||
auto node = config.GetDirect("canSockets");
|
||||
if (node.GetType() == sdv::toml::ENodeType::node_string)
|
||||
{
|
||||
vecConfigInterfaces.push_back(node.GetValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
catch (const sdv::toml::XTOMLParseException& e)
|
||||
{
|
||||
SDV_LOG_ERROR("Configuration could not be read: ", e.what());
|
||||
m_eStatus = sdv::EObjectStatus::initialization_failure;
|
||||
return;
|
||||
}
|
||||
|
||||
if (vecConfigInterfaces.size() == 0)
|
||||
{
|
||||
SDV_LOG_WARNING("Configuration failure, no 'canSockets' param found");
|
||||
}
|
||||
|
||||
if (!SetupCANSockets(vecConfigInterfaces))
|
||||
{
|
||||
m_eStatus = sdv::EObjectStatus::initialization_failure;
|
||||
return;
|
||||
}
|
||||
|
||||
m_threadReceive = std::thread(&CCANSockets::ReceiveThreadFunc, this);
|
||||
|
||||
m_eStatus = sdv::EObjectStatus::initialized;
|
||||
}
|
||||
|
||||
bool CCANSockets::SetupCANSockets(const std::deque<std::string>& vecConfigInterfaces)
|
||||
{
|
||||
// Retrieve the list of available interfaces
|
||||
std::set<std::string> availableInterfaces;
|
||||
struct ifaddrs *ifaddr, *ifa;
|
||||
if (getifaddrs(&ifaddr) != -1)
|
||||
{
|
||||
for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next)
|
||||
{
|
||||
availableInterfaces.insert(ifa->ifa_name);
|
||||
}
|
||||
}
|
||||
freeifaddrs(ifaddr);
|
||||
|
||||
CreateAndBindSockets(vecConfigInterfaces, availableInterfaces);
|
||||
|
||||
bool isSocketCreated = false;
|
||||
std::unique_lock<std::mutex> lock(m_mtxSockets);
|
||||
for (auto socket : m_vecSockets)
|
||||
{
|
||||
if ((socket.localSocket > 0) && (socket.networkInterface > 0))
|
||||
{
|
||||
isSocketCreated = true;
|
||||
SDV_LOG_INFO("Enabling CAN interface: ", socket.name);
|
||||
}
|
||||
}
|
||||
|
||||
if (isSocketCreated)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
SDV_LOG_ERROR(" Error: Socket list is empty.");
|
||||
return false;
|
||||
}
|
||||
|
||||
void CCANSockets::CreateAndBindSockets(const std::deque<std::string>& vecConfigInterfaces,
|
||||
const std::set<std::string>& availableInterfaces)
|
||||
{
|
||||
for (const auto& configInterface : vecConfigInterfaces)
|
||||
{
|
||||
SSocketDefinition socketDef;
|
||||
socketDef.name = "";
|
||||
socketDef.networkInterface = 0;
|
||||
socketDef.localSocket = -1;
|
||||
|
||||
// Create a socket
|
||||
int localSocket = socket(PF_CAN, SOCK_RAW, CAN_RAW);
|
||||
if (localSocket == -1)
|
||||
{
|
||||
SDV_LOG_ERROR("Error creating Socket");
|
||||
return;
|
||||
}
|
||||
|
||||
if(availableInterfaces.find(configInterface) == availableInterfaces.end())
|
||||
{
|
||||
SDV_LOG_WARNING("Interface not found: ", configInterface);
|
||||
std::unique_lock<std::mutex> lock(m_mtxSockets);
|
||||
m_vecSockets.push_back(socketDef);
|
||||
continue;
|
||||
}
|
||||
|
||||
auto ifIndex = if_nametoindex(configInterface.c_str());
|
||||
socketDef.networkInterface = ifIndex;
|
||||
|
||||
// Bind the socket to the specified CAN interface
|
||||
sockaddr_can sAddr = {};
|
||||
sAddr.can_family = AF_CAN;
|
||||
sAddr.can_ifindex = ifIndex;
|
||||
if (bind(localSocket, reinterpret_cast<sockaddr*>(&sAddr), sizeof(sAddr)) == -1)
|
||||
{
|
||||
SDV_LOG_ERROR("Error binding socket to interface ", configInterface);
|
||||
std::unique_lock<std::mutex> lock(m_mtxSockets);
|
||||
m_vecSockets.push_back(socketDef);
|
||||
close(localSocket);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Set socket to be non-blocking
|
||||
if (fcntl(localSocket, F_SETFL, O_NONBLOCK) == -1)
|
||||
{
|
||||
perror("fcntl - F_SETFL");
|
||||
SDV_LOG_ERROR("Error setting socket to be non-blocking:", configInterface);
|
||||
close(localSocket);
|
||||
std::unique_lock<std::mutex> lock(m_mtxSockets);
|
||||
m_vecSockets.push_back(socketDef);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Store the successfully configured socket
|
||||
socketDef.name = configInterface;
|
||||
socketDef.localSocket = localSocket;
|
||||
std::unique_lock<std::mutex> lock(m_mtxSockets);
|
||||
m_vecSockets.push_back(socketDef);
|
||||
}
|
||||
}
|
||||
|
||||
sdv::EObjectStatus CCANSockets::GetStatus() const
|
||||
{
|
||||
return m_eStatus;
|
||||
}
|
||||
|
||||
void CCANSockets::SetOperationMode(sdv::EOperationMode eMode)
|
||||
{
|
||||
switch (eMode)
|
||||
{
|
||||
case sdv::EOperationMode::configuring:
|
||||
if (m_eStatus == sdv::EObjectStatus::running || m_eStatus == sdv::EObjectStatus::initialized)
|
||||
m_eStatus = sdv::EObjectStatus::configuring;
|
||||
break;
|
||||
case sdv::EOperationMode::running:
|
||||
if (m_eStatus == sdv::EObjectStatus::configuring || m_eStatus == sdv::EObjectStatus::initialized)
|
||||
m_eStatus = sdv::EObjectStatus::running;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void CCANSockets::Shutdown()
|
||||
{
|
||||
m_eStatus = sdv::EObjectStatus::shutdown_in_progress;
|
||||
|
||||
// Wait until the receiving thread is finished.
|
||||
if (m_threadReceive.joinable())
|
||||
m_threadReceive.join();
|
||||
|
||||
std::unique_lock<std::mutex> lock(m_mtxSockets);
|
||||
for (const auto& socket : m_vecSockets)
|
||||
{
|
||||
if (socket.localSocket > 0)
|
||||
{
|
||||
close(socket.localSocket);
|
||||
}
|
||||
}
|
||||
|
||||
m_eStatus = sdv::EObjectStatus::destruction_pending;
|
||||
}
|
||||
|
||||
void CCANSockets::RegisterReceiver(/*in*/ sdv::can::IReceive* pReceiver)
|
||||
{
|
||||
if (m_eStatus != sdv::EObjectStatus::configuring) return;
|
||||
if (!pReceiver) return;
|
||||
|
||||
SDV_LOG_INFO("Registering VAPI CAN communication receiver...");
|
||||
|
||||
std::unique_lock<std::mutex> lock(m_mtxReceivers);
|
||||
m_setReceivers.insert(pReceiver);
|
||||
}
|
||||
|
||||
void CCANSockets::UnregisterReceiver(/*in*/ sdv::can::IReceive* pReceiver)
|
||||
{
|
||||
// NOTE: Normally the remove function should be called in the configuration mode. Since it doesn't give
|
||||
// feedback and the associated caller might delete any receiving function, allow the removal to take place even
|
||||
// when running.
|
||||
|
||||
if (!pReceiver) return;
|
||||
|
||||
SDV_LOG_INFO("Unregistering VAPI CAN communication receiver...");
|
||||
|
||||
std::unique_lock<std::mutex> lock(m_mtxReceivers);
|
||||
m_setReceivers.erase(pReceiver);
|
||||
}
|
||||
|
||||
sdv::sequence<sdv::u8string> CCANSockets::GetInterfaces() const
|
||||
{
|
||||
sdv::sequence<sdv::u8string> seqIfcNames;
|
||||
if (m_eStatus != sdv::EObjectStatus::running)
|
||||
{
|
||||
return seqIfcNames;
|
||||
}
|
||||
|
||||
std::unique_lock<std::mutex> lock(m_mtxSockets);
|
||||
for (const auto& rprInterface : m_vecSockets)
|
||||
{
|
||||
if (!rprInterface.name.empty())
|
||||
{
|
||||
seqIfcNames.push_back(rprInterface.name);
|
||||
}
|
||||
}
|
||||
|
||||
return seqIfcNames;
|
||||
}
|
||||
|
||||
void CCANSockets::Send(const sdv::can::SMessage& sMsg, uint32_t uiConfigIndex)
|
||||
{
|
||||
if (m_eStatus != sdv::EObjectStatus::running) return;
|
||||
if (sMsg.bCanFd) return; // CAN-FD not supported
|
||||
if (sMsg.seqData.size() > 8) return; // Invalid message length.
|
||||
|
||||
sockaddr_can sAddr{};
|
||||
can_frame sFrame{};
|
||||
|
||||
// Convert the message structure from VAPI SMessage to SocketCAN can_frame
|
||||
memset(&sFrame,0, sizeof(sFrame));
|
||||
sFrame.can_dlc = sMsg.seqData.size();
|
||||
if (sFrame.can_dlc > 8)
|
||||
return;
|
||||
|
||||
sFrame.can_id = sMsg.uiID;
|
||||
if (sMsg.bExtended)
|
||||
{
|
||||
sFrame.can_id |= CAN_EFF_FLAG;
|
||||
}
|
||||
|
||||
for (uint32_t index = 0; index < sFrame.can_dlc; ++index)
|
||||
{
|
||||
sFrame.data[index] = sMsg.seqData[index];
|
||||
}
|
||||
|
||||
std::unique_lock<std::mutex> lock(m_mtxSockets);
|
||||
|
||||
auto it = m_vecSockets.begin();
|
||||
std::advance(it, uiConfigIndex);
|
||||
if (it != m_vecSockets.end())
|
||||
{
|
||||
if ((it->localSocket > 0) && (it->networkInterface > 0))
|
||||
{
|
||||
sAddr.can_ifindex = it->networkInterface;
|
||||
sAddr.can_family = AF_CAN;
|
||||
sendto(it->localSocket, &sFrame, sizeof(can_frame), 0, reinterpret_cast<sockaddr*>(&sAddr), sizeof(sAddr));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void CCANSockets::ReceiveThreadFunc()
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
enum {retry, cont, exit} eNextStep = exit;
|
||||
switch (m_eStatus)
|
||||
{
|
||||
case sdv::EObjectStatus::configuring:
|
||||
case sdv::EObjectStatus::initialization_pending:
|
||||
case sdv::EObjectStatus::initialized:
|
||||
case sdv::EObjectStatus::initializing:
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
||||
eNextStep = retry;
|
||||
break;
|
||||
case sdv::EObjectStatus::running:
|
||||
eNextStep = cont;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
if (eNextStep == exit) break;
|
||||
if (eNextStep == retry) continue;
|
||||
|
||||
sockaddr_can sAddr{};
|
||||
can_frame sFrame{};
|
||||
socklen_t nAddrLen = sizeof(sAddr);
|
||||
|
||||
for (const auto& socket : m_vecSockets)
|
||||
{
|
||||
if ((socket.localSocket > 0) && (socket.networkInterface > 0))
|
||||
{
|
||||
//auto socketIndex = socket.localSocket;
|
||||
ssize_t nBytes = recvfrom(socket.localSocket, &sFrame, sizeof(can_frame), 0, reinterpret_cast<sockaddr*>(&sAddr), &nAddrLen);
|
||||
if (nBytes < 0)
|
||||
{
|
||||
// No data received... wait for 1 ms and try again
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
||||
continue;
|
||||
}
|
||||
|
||||
sdv::can::SMessage sMsg{};
|
||||
sMsg.uiID = sFrame.can_id;
|
||||
for (size_t nDataIndex = 0; nDataIndex < static_cast<size_t>(sFrame.can_dlc); nDataIndex++)
|
||||
{
|
||||
sMsg.seqData.push_back(sFrame.data[nDataIndex]);
|
||||
}
|
||||
|
||||
// Broadcast the message to the receivers
|
||||
std::unique_lock<std::mutex> lockReceivers(m_mtxReceivers);
|
||||
for (sdv::can::IReceive* pReceiver : m_setReceivers)
|
||||
{
|
||||
pReceiver->Receive(sMsg, socket.networkInterface);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
147
sdv_services/can_communication_socket_can/can_com_sockets.h
Normal file
147
sdv_services/can_communication_socket_can/can_com_sockets.h
Normal file
@@ -0,0 +1,147 @@
|
||||
#ifndef CAN_COM_SOCKET_H
|
||||
#define CAN_COM_SOCKET_H
|
||||
|
||||
#include <iostream>
|
||||
#include <cstring>
|
||||
#include <deque>
|
||||
#include <set>
|
||||
#include <thread>
|
||||
#include <mutex>
|
||||
#include <ifaddrs.h>
|
||||
#include <net/if.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <linux/can.h>
|
||||
#include <linux/if.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
#include <support/toml.h>
|
||||
#include <support/component_impl.h>
|
||||
#include <interfaces/can.h>
|
||||
|
||||
#ifndef __linux__
|
||||
// cppcheck-suppress preprocessorErrorDirective
|
||||
#error This code builds only on LINUX
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Component to establish Socket CAN communication between VAPI and external application
|
||||
*/
|
||||
class CCANSockets : public sdv::CSdvObject, public sdv::IObjectControl, public sdv::can::IRegisterReceiver,
|
||||
public sdv::can::ISend, sdv::can::IInformation
|
||||
{
|
||||
public:
|
||||
|
||||
// Interface map
|
||||
BEGIN_SDV_INTERFACE_MAP()
|
||||
SDV_INTERFACE_ENTRY(sdv::IObjectControl)
|
||||
SDV_INTERFACE_ENTRY(sdv::can::IRegisterReceiver)
|
||||
SDV_INTERFACE_ENTRY(sdv::can::ISend)
|
||||
SDV_INTERFACE_ENTRY(sdv::can::IInformation)
|
||||
END_SDV_INTERFACE_MAP()
|
||||
|
||||
DECLARE_OBJECT_CLASS_TYPE(sdv::EObjectType::Device)
|
||||
DECLARE_OBJECT_CLASS_NAME("CAN_Com_Sockets")
|
||||
DECLARE_DEFAULT_OBJECT_NAME("CAN_Communication_Object")
|
||||
DECLARE_OBJECT_SINGLETON()
|
||||
|
||||
/**
|
||||
* @brief Initialize the object. Overload of sdv::IObjectControl::Initialize.
|
||||
* @param[in] ssObjectConfig Optional configuration string.
|
||||
* The configuration contains either one interface name a list of interface names.
|
||||
* The Send() method must use the index of this list to determine the interface
|
||||
* In case of a single interface name the index is 0.
|
||||
* canSockets = "vcan0"
|
||||
* or
|
||||
* canSockets = ["vcan1", "vcan8", "vcan9", "vcan2"]
|
||||
*/
|
||||
virtual void Initialize(const sdv::u8string& ssObjectConfig) override;
|
||||
|
||||
/**
|
||||
* @brief Get the current status of the object. Overload of sdv::IObjectControl::GetStatus.
|
||||
* @return Return the current status of the object.
|
||||
*/
|
||||
virtual sdv::EObjectStatus GetStatus() const override;
|
||||
|
||||
/**
|
||||
* @brief Set the component operation mode. Overload of sdv::IObjectControl::SetOperationMode.
|
||||
* @param[in] eMode The operation mode, the component should run in.
|
||||
*/
|
||||
void SetOperationMode(sdv::EOperationMode eMode) override;
|
||||
|
||||
/**
|
||||
* @brief Shutdown called before the object is destroyed. Overload of sdv::IObjectControl::Shutdown.
|
||||
*/
|
||||
virtual void Shutdown() override;
|
||||
|
||||
/**
|
||||
* @brief Register a CAN message receiver. Overload of sdv::can::IRegisterReceiver::RegisterReceiver.
|
||||
* @param[in] pReceiver Pointer to the receiver interface.
|
||||
*/
|
||||
virtual void RegisterReceiver(/*in*/ sdv::can::IReceive* pReceiver) override;
|
||||
|
||||
/**
|
||||
* @brief Unregister a previously registered CAN message receiver. Overload of
|
||||
* sdv::can::IRegisterReceiver::UnregisterReceiver.
|
||||
* @param[in] pReceiver Pointer to the receiver interface.
|
||||
*/
|
||||
virtual void UnregisterReceiver(/*in*/ sdv::can::IReceive* pReceiver) override;
|
||||
|
||||
/**
|
||||
* @brief Send a CAN message. Overload of sdv::can::ISend::Send.
|
||||
* @param[in] sMsg Message to be sent.
|
||||
* @param[in] uiConfigIndex Interface index to use for sending.
|
||||
* Must match with the configuration list. In case configuration contains a single element the index is 0.
|
||||
* The message cannot be sent to all interfaces automatically
|
||||
*/
|
||||
virtual void Send(/*in*/ const sdv::can::SMessage& sMsg, /*in*/ uint32_t uiConfigIndex) override;
|
||||
|
||||
/**
|
||||
* @brief Get a list of interface names. Overload of sdv::can::IInformation::GetInterfaces.
|
||||
* @return Sequence containing the names of the interfaces.
|
||||
*/
|
||||
virtual sdv::sequence<sdv::u8string> GetInterfaces() const override;
|
||||
|
||||
private:
|
||||
/**
|
||||
* @brief Thread function to read data from all bound interfaces.
|
||||
*/
|
||||
void ReceiveThreadFunc();
|
||||
|
||||
/**
|
||||
* @brief Function to setup the sockets in the configuration
|
||||
* @param[in] vecConfigInterfaces List of interface names which should be connected to a socket
|
||||
*/
|
||||
bool SetupCANSockets(const std::deque<std::string>& vecConfigInterfaces);
|
||||
|
||||
/**
|
||||
* @brief Function to create and setup sockets, collected in m_vecSockets
|
||||
* @param[in] vecConfigInterfaces List of interface names which should be connected to a socket
|
||||
* @param[in] availableInterfaces List of available interface names
|
||||
*/
|
||||
void CreateAndBindSockets(const std::deque<std::string>& vecConfigInterfaces,
|
||||
const std::set<std::string>& availableInterfaces);
|
||||
|
||||
/**
|
||||
* @brief Socket definition structure
|
||||
*/
|
||||
struct SSocketDefinition
|
||||
{
|
||||
int networkInterface; ///< network interface, must be > 0
|
||||
int32_t localSocket; ///< local socket id; -1 represents an invalid socket element
|
||||
std::string name; ///< interface name, can be empty in case of an invalid socket element
|
||||
};
|
||||
|
||||
std::atomic<sdv::EObjectStatus> m_eStatus = sdv::EObjectStatus::initialization_pending; ///< Object status
|
||||
std::thread m_threadReceive; ///< Receive thread.
|
||||
mutable std::mutex m_mtxReceivers; ///< Protect the receiver set.
|
||||
std::set<sdv::can::IReceive*> m_setReceivers; ///< Set with receiver interfaces.
|
||||
mutable std::mutex m_mtxSockets; ///< Protect the socket list.
|
||||
std::deque<SSocketDefinition> m_vecSockets; ///< Socket list
|
||||
};
|
||||
|
||||
DEFINE_SDV_OBJECT(CCANSockets)
|
||||
|
||||
#endif // ! defined CAN_COM_SOCKET_H
|
||||
@@ -0,0 +1,19 @@
|
||||
[Configuration]
|
||||
Version = 100
|
||||
|
||||
[[Component]]
|
||||
Path = "task_timer.sdv"
|
||||
Class = "TaskTimerService"
|
||||
|
||||
[[Component]]
|
||||
Path = "data_dispatch_service.sdv"
|
||||
Class = "DataDispatchService"
|
||||
|
||||
[[Component]]
|
||||
Path = "can_com_sockets.sdv"
|
||||
Class = "CAN_Com_Sockets"
|
||||
canSockets=["llcecan0"]
|
||||
|
||||
[[Component]]
|
||||
Path = "can_dl.sdv"
|
||||
Class = "CAN_data_link"
|
||||
Reference in New Issue
Block a user