From cc10527e50dbf264ecdd7b6083e572f57b462570 Mon Sep 17 00:00:00 2001 From: Artur Wolf Date: Tue, 17 Sep 2024 10:39:29 +0200 Subject: [PATCH 1/2] Code cleanup --- include/DsVeosCoSim/DsVeosCoSim.h | 88 +++++++------- shared/BackgroundService.h | 2 +- shared/CMakeLists.txt | 1 - shared/ClientServerTestHelper.cpp | 2 +- shared/ClientServerTestHelper.h | 4 +- shared/CoSimServerWrapper.cpp | 92 --------------- shared/CoSimServerWrapper.h | 48 -------- shared/Generator.cpp | 4 +- shared/Generator.h | 10 +- shared/Helper.cpp | 4 +- shared/Helper.h | 7 +- shared/LogHelper.cpp | 28 +++-- shared/LogHelper.h | 15 ++- src/BusBuffer.cpp | 16 +-- src/BusBuffer.h | 64 ++++++----- src/CoSimClient.cpp | 51 +++++---- src/CoSimClient.h | 26 +++-- src/CoSimServer.cpp | 37 +++--- src/CoSimServer.h | 37 +++--- src/CoSimTypes.h | 108 +++++++----------- src/Communication/Channel.h | 18 +++ src/Communication/LocalChannel.cpp | 19 --- src/Communication/LocalChannel.h | 2 +- src/Communication/SocketChannel.h | 4 +- src/DsVeosCoSim.cpp | 13 ++- src/Helpers/Logger.cpp | 8 +- src/Helpers/RingBuffer.h | 4 +- src/Helpers/ShmRingBuffer.h | 8 +- src/IoBuffer.cpp | 52 +++++---- src/IoBuffer.h | 48 ++++---- src/OsAbstraction/Handle.h | 6 +- src/OsAbstraction/NamedEvent.h | 2 +- src/OsAbstraction/Socket.cpp | 24 ++-- src/OsAbstraction/Socket.h | 2 +- src/Protocol.cpp | 63 +++++----- src/Protocol.h | 52 +++++---- test/TestBusBuffer.cpp | 18 +-- test/TestCoSim.cpp | 19 +-- test/TestIoBuffer.cpp | 22 ++-- test/TestProtocol.cpp | 37 +++--- .../ClientCoSimCallbackBased.cpp | 2 +- .../ClientCoSimPollingBased.cpp | 2 +- .../PerformanceTestServer/ServerCoSim.cpp | 9 +- utilities/TestClient/Program.cpp | 20 ++-- utilities/TestServer/Program.cpp | 39 ++++--- 45 files changed, 506 insertions(+), 631 deletions(-) delete mode 100644 shared/CoSimServerWrapper.cpp delete mode 100644 shared/CoSimServerWrapper.h diff --git a/include/DsVeosCoSim/DsVeosCoSim.h b/include/DsVeosCoSim/DsVeosCoSim.h index bea25bd..0682511 100644 --- a/include/DsVeosCoSim/DsVeosCoSim.h +++ b/include/DsVeosCoSim/DsVeosCoSim.h @@ -49,47 +49,55 @@ /** * \brief Defines the maximum length of a CAN message payload. */ -#define DSVEOSCOSIM_CAN_MESSAGE_MAX_LENGTH 64 +enum { + DSVEOSCOSIM_CAN_MESSAGE_MAX_LENGTH = 64 +}; /** * \brief Defines the maximum length of an ethernet message payload. */ -#define DSVEOSCOSIM_ETH_MESSAGE_MAX_LENGTH 9018 +enum { + DSVEOSCOSIM_ETH_MESSAGE_MAX_LENGTH = 9018 +}; /** * \brief Defines the maximum length of a LIN message payload. */ -#define DSVEOSCOSIM_LIN_MESSAGE_MAX_LENGTH 8 +enum { + DSVEOSCOSIM_LIN_MESSAGE_MAX_LENGTH = 8 +}; /** * \brief Defines the maximum length of an ethernet address (MAC address). */ -#define DSVEOSCOSIM_ETH_ADDRESS_LENGTH 6 +enum { + DSVEOSCOSIM_ETH_ADDRESS_LENGTH = 6 +}; /** * \brief Represents a handle to be used for communicating with a dSPACE VEOS CoSim server. */ -typedef void* DsVeosCoSim_Handle; +typedef void* DsVeosCoSim_Handle; // NOLINT /** * \brief Represents an IO signal id. */ -typedef uint32_t DsVeosCoSim_IoSignalId; +typedef uint32_t DsVeosCoSim_IoSignalId; // NOLINT /** * \brief Represents a bus controller id. */ -typedef uint32_t DsVeosCoSim_BusControllerId; +typedef uint32_t DsVeosCoSim_BusControllerId; // NOLINT /** * \brief Represents the simulation time in nanoseconds. */ -typedef int64_t DsVeosCoSim_SimulationTime; +typedef int64_t DsVeosCoSim_SimulationTime; // NOLINT /** * \brief Represents a result of a function. */ -typedef enum DsVeosCoSim_Result { +typedef enum DsVeosCoSim_Result { // NOLINT /** * \brief Will be returned if the API function was successful. */ @@ -101,7 +109,7 @@ typedef enum DsVeosCoSim_Result { DsVeosCoSim_Result_Error, /** - * \brief Will be returned if the receive API function found an empty buffer. + * \brief Will be returned if the reception API function found an empty buffer. */ DsVeosCoSim_Result_Empty, @@ -126,7 +134,7 @@ typedef enum DsVeosCoSim_Result { /** * \brief Represents a command for the non-blocking API. */ -typedef enum DsVeosCoSim_Command { +typedef enum DsVeosCoSim_Command { // NOLINT /** * \brief No command. */ @@ -168,7 +176,7 @@ typedef enum DsVeosCoSim_Command { /** * \brief Represents the severity of a log message. */ -typedef enum DsVeosCoSim_Severity { +typedef enum DsVeosCoSim_Severity { // NOLINT /** * \brief Error message. */ @@ -195,7 +203,7 @@ typedef enum DsVeosCoSim_Severity { /** * \brief Represents the reason of a simulation termination. */ -typedef enum DsVeosCoSim_TerminateReason { +typedef enum DsVeosCoSim_TerminateReason { // NOLINT /** * \brief Simulation finished successfully. */ @@ -212,7 +220,7 @@ typedef enum DsVeosCoSim_TerminateReason { /** * \brief Represents the connection state. */ -typedef enum DsVeosCoSim_ConnectionState { +typedef enum DsVeosCoSim_ConnectionState { // NOLINT /** * \brief Disconnected. */ @@ -229,7 +237,7 @@ typedef enum DsVeosCoSim_ConnectionState { /** * \brief Represents the data type of IO signal. */ -typedef enum DsVeosCoSim_DataType { +typedef enum DsVeosCoSim_DataType { // NOLINT /** * \brief Boolean. Underlying data type is uint8. 0 is equal to false, != 0 is equal to true. */ @@ -291,7 +299,7 @@ typedef enum DsVeosCoSim_DataType { /** * \brief Represents the size kind of IO signal. */ -typedef enum DsVeosCoSim_SizeKind { +typedef enum DsVeosCoSim_SizeKind { // NOLINT /** * \brief The IO signal size is fixed. */ @@ -308,7 +316,7 @@ typedef enum DsVeosCoSim_SizeKind { /** * \brief Represents an IO signal. */ -typedef struct DsVeosCoSim_IoSignal { // NOLINT(readability-identifier-naming) +typedef struct DsVeosCoSim_IoSignal { // NOLINT /** * \brief Unique id of the IO signal. */ @@ -339,7 +347,7 @@ typedef struct DsVeosCoSim_IoSignal { // NOLINT(readability-identifier-naming) /** * \brief Represents a CAN controller. */ -typedef struct DsVeosCoSim_CanController { // NOLINT(readability-identifier-naming) +typedef struct DsVeosCoSim_CanController { // NOLINT /** * \brief Unique id of the CAN controller. */ @@ -379,7 +387,7 @@ typedef struct DsVeosCoSim_CanController { // NOLINT(readability-identifier-nam /** * \brief Underlying data type of the flags of a CAN message. */ -typedef uint32_t DsVeosCoSim_CanMessageFlags; +typedef uint32_t DsVeosCoSim_CanMessageFlags; // NOLINT /** * \brief Represents the flags of a CAN message. @@ -425,7 +433,7 @@ enum { /** * \brief Represents a CAN message. */ -typedef struct DsVeosCoSim_CanMessage { // NOLINT(readability-identifier-naming) +typedef struct DsVeosCoSim_CanMessage { // NOLINT /** * \brief The simulation time when the CAN message was received. * For received messages only. @@ -461,7 +469,7 @@ typedef struct DsVeosCoSim_CanMessage { // NOLINT(readability-identifier-naming /** * \brief Represents an ethernet controller. */ -typedef struct DsVeosCoSim_EthController { // NOLINT(readability-identifier-naming) +typedef struct DsVeosCoSim_EthController { // NOLINT /** * \brief Unique id of the ethernet controller. */ @@ -501,7 +509,7 @@ typedef struct DsVeosCoSim_EthController { // NOLINT(readability-identifier-nam /** * \brief Underlying data type of the flags of an ethernet message. */ -typedef uint32_t DsVeosCoSim_EthMessageFlags; +typedef uint32_t DsVeosCoSim_EthMessageFlags; // NOLINT /** * \brief Represents the flags of an ethernet message. @@ -529,7 +537,7 @@ enum { /** * \brief Represents an ethernet message. */ -typedef struct DsVeosCoSim_EthMessage { // NOLINT(readability-identifier-naming) +typedef struct DsVeosCoSim_EthMessage { // NOLINT /** * \brief The simulation time when the ethernet message was received. * For received messages only. @@ -565,7 +573,7 @@ typedef struct DsVeosCoSim_EthMessage { // NOLINT(readability-identifier-naming /** * \brief Represents the type of the LIN controller. */ -typedef enum DsVeosCoSim_LinControllerType { +typedef enum DsVeosCoSim_LinControllerType { // NOLINT /** * \brief LIN controller is a responder. */ @@ -582,7 +590,7 @@ typedef enum DsVeosCoSim_LinControllerType { /** * \brief Represents an LIN controller. */ -typedef struct DsVeosCoSim_LinController { // NOLINT(readability-identifier-naming) +typedef struct DsVeosCoSim_LinController { // NOLINT /** * \brief Unique id of the LIN controller. */ @@ -622,7 +630,7 @@ typedef struct DsVeosCoSim_LinController { // NOLINT(readability-identifier-nam /** * \brief Underlying data type of the flags of a LIN message. */ -typedef uint32_t DsVeosCoSim_LinMessageFlags; +typedef uint32_t DsVeosCoSim_LinMessageFlags; // NOLINT /** * \brief Represents the flags of a LIN message. @@ -704,7 +712,7 @@ enum { /** * \brief Represents a LIN message. */ -typedef struct DsVeosCoSim_LinMessage { // NOLINT(readability-identifier-naming) +typedef struct DsVeosCoSim_LinMessage { // NOLINT /** * \brief The simulation time when the LIN message was received. * For received messages only. @@ -742,14 +750,14 @@ typedef struct DsVeosCoSim_LinMessage { // NOLINT(readability-identifier-naming * \param severity The severity of the message. * \param logMessage The log message. */ -typedef void (*DsVeosCoSim_LogCallback)(DsVeosCoSim_Severity severity, const char* logMessage); +typedef void (*DsVeosCoSim_LogCallback)(DsVeosCoSim_Severity severity, const char* logMessage); // NOLINT /** * \brief Represents a simulation state changed or step callback function pointer. * \param simulationTime The current simulation time. * \param userData The user data passed via DsVeosCoSim_SetCallbacks. */ -typedef void (*DsVeosCoSim_SimulationCallback)(DsVeosCoSim_SimulationTime simulationTime, void* userData); +typedef void (*DsVeosCoSim_SimulationCallback)(DsVeosCoSim_SimulationTime simulationTime, void* userData); // NOLINT /** * \brief Represents a simulation terminated callback function pointer. @@ -757,7 +765,7 @@ typedef void (*DsVeosCoSim_SimulationCallback)(DsVeosCoSim_SimulationTime simula * \param reason The termination reason. * \param userData The user data passed via DsVeosCoSim_SetCallbacks. */ -typedef void (*DsVeosCoSim_SimulationTerminatedCallback)(DsVeosCoSim_SimulationTime simulationTime, +typedef void (*DsVeosCoSim_SimulationTerminatedCallback)(DsVeosCoSim_SimulationTime simulationTime, // NOLINT DsVeosCoSim_TerminateReason reason, void* userData); @@ -769,7 +777,7 @@ typedef void (*DsVeosCoSim_SimulationTerminatedCallback)(DsVeosCoSim_SimulationT * \param value The changed data. * \param userData The user data passed via DsVeosCoSim_SetCallbacks. */ -typedef void (*DsVeosCoSim_IncomingSignalChangedCallback)(DsVeosCoSim_SimulationTime simulationTime, +typedef void (*DsVeosCoSim_IncomingSignalChangedCallback)(DsVeosCoSim_SimulationTime simulationTime, // NOLINT const DsVeosCoSim_IoSignal* incomingSignal, uint32_t length, const void* value, @@ -782,7 +790,7 @@ typedef void (*DsVeosCoSim_IncomingSignalChangedCallback)(DsVeosCoSim_Simulation * \param message The received message. * \param userData The user data passed via DsVeosCoSim_SetCallbacks. */ -typedef void (*DsVeosCoSim_CanMessageReceivedCallback)(DsVeosCoSim_SimulationTime simulationTime, +typedef void (*DsVeosCoSim_CanMessageReceivedCallback)(DsVeosCoSim_SimulationTime simulationTime, // NOLINT const DsVeosCoSim_CanController* canController, const DsVeosCoSim_CanMessage* message, void* userData); @@ -794,7 +802,7 @@ typedef void (*DsVeosCoSim_CanMessageReceivedCallback)(DsVeosCoSim_SimulationTim * \param message The received message. * \param userData The user data passed via DsVeosCoSim_SetCallbacks. */ -typedef void (*DsVeosCoSim_EthMessageReceivedCallback)(DsVeosCoSim_SimulationTime simulationTime, +typedef void (*DsVeosCoSim_EthMessageReceivedCallback)(DsVeosCoSim_SimulationTime simulationTime, // NOLINT const DsVeosCoSim_EthController* ethController, const DsVeosCoSim_EthMessage* message, void* userData); @@ -806,7 +814,7 @@ typedef void (*DsVeosCoSim_EthMessageReceivedCallback)(DsVeosCoSim_SimulationTim * \param message The received message. * \param userData The user data passed via DsVeosCoSim_SetCallbacks. */ -typedef void (*DsVeosCoSim_LinMessageReceivedCallback)(DsVeosCoSim_SimulationTime simulationTime, +typedef void (*DsVeosCoSim_LinMessageReceivedCallback)(DsVeosCoSim_SimulationTime simulationTime, // NOLINT const DsVeosCoSim_LinController* linController, const DsVeosCoSim_LinMessage* message, void* userData); @@ -814,7 +822,7 @@ typedef void (*DsVeosCoSim_LinMessageReceivedCallback)(DsVeosCoSim_SimulationTim /** * \brief Represents the callbacks that will be fired during the co-simulation. */ -typedef struct DsVeosCoSim_Callbacks { // NOLINT(readability-identifier-naming) +typedef struct DsVeosCoSim_Callbacks { // NOLINT /** * \brief Will be called when the simulation started in dSPACE VEOS. */ @@ -858,26 +866,26 @@ typedef struct DsVeosCoSim_Callbacks { // NOLINT(readability-identifier-naming) /** * \brief Will be called when a CAN message was received from dSPACE VEOS. * If this callback is registered, then DsVeosCoSim_ReceiveCanMessage will always return - * DsVeosCoSim_Result_Empty. + * DsVeosCoSim_Result_Empty. */ DsVeosCoSim_CanMessageReceivedCallback canMessageReceivedCallback; /** * \brief Will be called when an ethernet message was received from dSPACE VEOS. * If this callback is registered, then DsVeosCoSim_ReceiveEthMessage will always return - * DsVeosCoSim_Result_Empty. + * DsVeosCoSim_Result_Empty. */ DsVeosCoSim_EthMessageReceivedCallback ethMessageReceivedCallback; /** * \brief Will be called when a LIN message was received from dSPACE VEOS. * If this callback is registered, then DsVeosCoSim_ReceiveLinMessage will always return - * DsVeosCoSim_Result_Empty. + * DsVeosCoSim_Result_Empty. */ DsVeosCoSim_LinMessageReceivedCallback linMessageReceivedCallback; /** - * \brief An arbitrary object the will be passed to every callback. + * \brief An arbitrary object that will be passed to every callback. */ void* userData; } DsVeosCoSim_Callbacks; @@ -885,7 +893,7 @@ typedef struct DsVeosCoSim_Callbacks { // NOLINT(readability-identifier-naming) /** * \brief Represents the data that will be used for establishing the connection. */ -typedef struct DsVeosCoSim_ConnectConfig { // NOLINT(readability-identifier-naming) +typedef struct DsVeosCoSim_ConnectConfig { // NOLINT /** * \brief The IP address of the dSPACE VEOS CoSim server. "127.0.0.1" if not specified. */ diff --git a/shared/BackgroundService.h b/shared/BackgroundService.h index 285c507..8018d32 100644 --- a/shared/BackgroundService.h +++ b/shared/BackgroundService.h @@ -13,7 +13,7 @@ class BackgroundService final { ~BackgroundService() noexcept; BackgroundService(const BackgroundService&) = delete; - BackgroundService& operator=(BackgroundService const&) = delete; + BackgroundService& operator=(const BackgroundService&) = delete; BackgroundService(BackgroundService&&) = delete; BackgroundService& operator=(BackgroundService&&) = delete; diff --git a/shared/CMakeLists.txt b/shared/CMakeLists.txt index f5ae0d1..3c99e0f 100644 --- a/shared/CMakeLists.txt +++ b/shared/CMakeLists.txt @@ -5,7 +5,6 @@ add_library( STATIC BackgroundService.cpp ClientServerTestHelper.cpp - CoSimServerWrapper.cpp Generator.cpp Helper.cpp LogHelper.cpp diff --git a/shared/ClientServerTestHelper.cpp b/shared/ClientServerTestHelper.cpp index 02fbe2f..0d515b5 100644 --- a/shared/ClientServerTestHelper.cpp +++ b/shared/ClientServerTestHelper.cpp @@ -57,7 +57,7 @@ bool IsSendingLinMessagesEnabled() { return g_sendLinMessages; } -bool SendSomeData(DsVeosCoSim::SimulationTime simulationTime, const RunTimeInfo& runTimeInfo) { +bool SendSomeData(DsVeosCoSim_SimulationTime simulationTime, const RunTimeInfo& runTimeInfo) { static int64_t lastHalfSecond = -1; static int64_t counter = 0; const int64_t currentHalfSecond = simulationTime / 500000000; diff --git a/shared/ClientServerTestHelper.h b/shared/ClientServerTestHelper.h index 46713f5..78dd898 100644 --- a/shared/ClientServerTestHelper.h +++ b/shared/ClientServerTestHelper.h @@ -13,7 +13,7 @@ struct RunTimeInfo { std::vector linControllers; std::vector incomingSignals; std::vector outgoingSignals; - std::function write; + std::function write; std::function transmitCan; std::function transmitEth; std::function transmitLin; @@ -29,4 +29,4 @@ bool IsSendingCanMessagesEnabled(); bool IsSendingEthMessagesEnabled(); bool IsSendingLinMessagesEnabled(); -[[nodiscard]] bool SendSomeData(DsVeosCoSim::SimulationTime simulationTime, const RunTimeInfo& runTimeInfo); \ No newline at end of file +[[nodiscard]] bool SendSomeData(DsVeosCoSim_SimulationTime simulationTime, const RunTimeInfo& runTimeInfo); \ No newline at end of file diff --git a/shared/CoSimServerWrapper.cpp b/shared/CoSimServerWrapper.cpp deleted file mode 100644 index 91f7326..0000000 --- a/shared/CoSimServerWrapper.cpp +++ /dev/null @@ -1,92 +0,0 @@ -// Copyright dSPACE GmbH. All rights reserved. - -#include "CoSimServerWrapper.h" - -using namespace DsVeosCoSim; - -CoSimServerWrapper::CoSimServerWrapper() { - _stopBackgroundThread = false; - _backgroundThread = std::thread([this] { - RunBackground(); - }); -} - -CoSimServerWrapper::~CoSimServerWrapper() { - _stopBackgroundThread = true; - _backgroundThread.join(); -} - -void CoSimServerWrapper::Load(const CoSimServerConfig& config) { - std::lock_guard lock(_mutex); - _server.Load(config); -} - -void CoSimServerWrapper::Unload() { - std::lock_guard lock(_mutex); - _server.Unload(); -} - -void CoSimServerWrapper::Start(SimulationTime simulationTime) { - std::lock_guard lock(_mutex); - _server.Start(simulationTime); -} - -void CoSimServerWrapper::Stop(SimulationTime simulationTime) { - std::lock_guard lock(_mutex); - _server.Stop(simulationTime); -} - -void CoSimServerWrapper::Terminate(SimulationTime simulationTime, TerminateReason reason) { - std::lock_guard lock(_mutex); - _server.Terminate(simulationTime, reason); -} - -void CoSimServerWrapper::Pause(SimulationTime simulationTime) { - std::lock_guard lock(_mutex); - _server.Pause(simulationTime); -} - -void CoSimServerWrapper::Continue(SimulationTime simulationTime) { - std::lock_guard lock(_mutex); - _server.Continue(simulationTime); -} - -void CoSimServerWrapper::Step(SimulationTime simulationTime, SimulationTime& nextSimulationTime) { - std::lock_guard lock(_mutex); - _server.Step(simulationTime, nextSimulationTime); -} - -void CoSimServerWrapper::Write(IoSignalId signalId, uint32_t length, const void* value) { - std::lock_guard lock(_mutex); - _server.Write(signalId, length, value); -} - -bool CoSimServerWrapper::Transmit(const DsVeosCoSim_CanMessage& message) { - std::lock_guard lock(_mutex); - return _server.Transmit(message); -} - -bool CoSimServerWrapper::Transmit(const DsVeosCoSim_EthMessage& message) { - std::lock_guard lock(_mutex); - return _server.Transmit(message); -} - -bool CoSimServerWrapper::Transmit(const DsVeosCoSim_LinMessage& message) { - std::lock_guard lock(_mutex); - return _server.Transmit(message); -} - -uint16_t CoSimServerWrapper::GetLocalPort() const { - return _server.GetLocalPort(); -} - -void CoSimServerWrapper::RunBackground() { - while (!_stopBackgroundThread) { - { - std::lock_guard lock(_mutex); - _server.BackgroundService(); - } - - std::this_thread::sleep_for(std::chrono::milliseconds(10)); - } -} diff --git a/shared/CoSimServerWrapper.h b/shared/CoSimServerWrapper.h deleted file mode 100644 index 5003d42..0000000 --- a/shared/CoSimServerWrapper.h +++ /dev/null @@ -1,48 +0,0 @@ -// Copyright dSPACE GmbH. All rights reserved. - -#pragma once - -#include - -#include "CoSimServer.h" -#include "CoSimTypes.h" - -class CoSimServerWrapper final { -public: - CoSimServerWrapper(); - ~CoSimServerWrapper(); - - CoSimServerWrapper(const CoSimServerWrapper&) = delete; - CoSimServerWrapper& operator=(CoSimServerWrapper const&) = delete; - - CoSimServerWrapper(CoSimServerWrapper&&) = delete; - CoSimServerWrapper& operator=(CoSimServerWrapper&&) = delete; - - void Load(const DsVeosCoSim::CoSimServerConfig& config); - void Unload(); - - void Start(DsVeosCoSim::SimulationTime simulationTime); - void Stop(DsVeosCoSim::SimulationTime simulationTime); - void Terminate(DsVeosCoSim::SimulationTime simulationTime, DsVeosCoSim::TerminateReason reason); - void Pause(DsVeosCoSim::SimulationTime simulationTime); - void Continue(DsVeosCoSim::SimulationTime simulationTime); - void Step(DsVeosCoSim::SimulationTime simulationTime, DsVeosCoSim::SimulationTime& nextSimulationTime); - - void Write(DsVeosCoSim::IoSignalId signalId, uint32_t length, const void* value); - - [[nodiscard]] bool Transmit(const DsVeosCoSim_CanMessage& message); - [[nodiscard]] bool Transmit(const DsVeosCoSim_EthMessage& message); - [[nodiscard]] bool Transmit(const DsVeosCoSim_LinMessage& message); - - [[nodiscard]] uint16_t GetLocalPort() const; - -private: - void RunBackground(); - - DsVeosCoSim::CoSimServer _server; - - bool _stopBackgroundThread{}; - std::thread _backgroundThread; - - std::recursive_mutex _mutex; -}; diff --git a/shared/Generator.cpp b/shared/Generator.cpp index d360d44..56c8f2c 100644 --- a/shared/Generator.cpp +++ b/shared/Generator.cpp @@ -72,13 +72,13 @@ IoSignal CreateSignal(DsVeosCoSim_DataType dataType, DsVeosCoSim_SizeKind sizeKi return signal; } -std::vector GenerateIoData(const IoSignal& signal) { +std::vector GenerateIoData(const DsVeosCoSim_IoSignal& signal) { std::vector data = CreateZeroedIoData(signal); FillWithRandom(data.data(), data.size()); return data; } -std::vector CreateZeroedIoData(const IoSignal& signal) { +std::vector CreateZeroedIoData(const DsVeosCoSim_IoSignal& signal) { std::vector data; data.resize(GetDataTypeSize(signal.dataType) * signal.length); return data; diff --git a/shared/Generator.h b/shared/Generator.h index e33d204..63975aa 100644 --- a/shared/Generator.h +++ b/shared/Generator.h @@ -31,16 +31,16 @@ template [[nodiscard]] DsVeosCoSim::IoSignal CreateSignal(DsVeosCoSim_DataType dataType); [[nodiscard]] DsVeosCoSim::IoSignal CreateSignal(DsVeosCoSim_DataType dataType, DsVeosCoSim_SizeKind sizeKind); -[[nodiscard]] std::vector GenerateIoData(const DsVeosCoSim::IoSignal& signal); -[[nodiscard]] std::vector CreateZeroedIoData(const DsVeosCoSim::IoSignal& signal); +[[nodiscard]] std::vector GenerateIoData(const DsVeosCoSim_IoSignal& signal); +[[nodiscard]] std::vector CreateZeroedIoData(const DsVeosCoSim_IoSignal& signal); void FillWithRandom(DsVeosCoSim::CanController& controller); void FillWithRandom(DsVeosCoSim::EthController& controller); void FillWithRandom(DsVeosCoSim::LinController& controller); -void FillWithRandom(DsVeosCoSim::CanMessage& message, DsVeosCoSim::BusControllerId controllerId); -void FillWithRandom(DsVeosCoSim::EthMessage& message, DsVeosCoSim::BusControllerId controllerId); -void FillWithRandom(DsVeosCoSim::LinMessage& message, DsVeosCoSim::BusControllerId controllerId); +void FillWithRandom(DsVeosCoSim::CanMessage& message, DsVeosCoSim_BusControllerId controllerId); +void FillWithRandom(DsVeosCoSim::EthMessage& message, DsVeosCoSim_BusControllerId controllerId); +void FillWithRandom(DsVeosCoSim::LinMessage& message, DsVeosCoSim_BusControllerId controllerId); [[nodiscard]] std::vector CreateSignals(size_t count); diff --git a/shared/Helper.cpp b/shared/Helper.cpp index a648352..471185b 100644 --- a/shared/Helper.cpp +++ b/shared/Helper.cpp @@ -15,6 +15,8 @@ #include #else #include +#include +#include #endif using namespace DsVeosCoSim; @@ -55,7 +57,7 @@ bool StartUp() { return true; } -int32_t getChar() { +int32_t GetChar() { #ifdef _WIN32 return _getch(); #else diff --git a/shared/Helper.h b/shared/Helper.h index 0d65270..d43ad51 100644 --- a/shared/Helper.h +++ b/shared/Helper.h @@ -3,16 +3,13 @@ #pragma once #include -#include // IWYU pragma: keep +#include // IWYU pragma: keep, NOLINT #include "Communication/SocketChannel.h" #include "OsAbstraction/Socket.h" #ifdef _WIN32 #include "Communication/LocalChannel.h" -#else -#include -#include #endif constexpr uint32_t Infinite = UINT32_MAX; // NOLINT @@ -28,7 +25,7 @@ constexpr uint32_t Infinite = UINT32_MAX; // NOLINT } \ } while (0) -int32_t getChar(); +[[nodiscard]] int32_t GetChar(); constexpr uint32_t DefaultTimeout = 1000; // NOLINT diff --git a/shared/LogHelper.cpp b/shared/LogHelper.cpp index e944bf8..e4e782c 100644 --- a/shared/LogHelper.cpp +++ b/shared/LogHelper.cpp @@ -40,7 +40,7 @@ std::string DataToString(const uint8_t* data, uint32_t dataLength, char separato } std::string DataTypeValueToString(const void* value, uint32_t index, DsVeosCoSim_DataType dataType) { - switch (dataType) { + switch (dataType) { // NOLINT case DsVeosCoSim_DataType_Bool: return std::to_string(static_cast(value)[index]); case DsVeosCoSim_DataType_Int8: @@ -63,9 +63,9 @@ std::string DataTypeValueToString(const void* value, uint32_t index, DsVeosCoSim return std::to_string(static_cast(value)[index]); case DsVeosCoSim_DataType_Float64: return std::to_string(static_cast(value)[index]); - default: // NOLINT(clang-diagnostic-covered-switch-default) - return ""; } + + throw std::runtime_error("Invalid data type."); } std::string ValueToString(const void* value, uint32_t length, DsVeosCoSim_DataType dataType) { @@ -100,23 +100,21 @@ void InitializeOutput() { SetLogCallback(OnLogCallback); } -void OnLogCallback(Severity severity, std::string_view message) { +void OnLogCallback(DsVeosCoSim_Severity severity, std::string_view message) { g_lastMessage = message; - switch (severity) { - case Severity::Error: + switch (severity) { // NOLINT + case DsVeosCoSim_Severity_Error: print(red, "{}\n", message); break; - case Severity::Warning: + case DsVeosCoSim_Severity_Warning: print(yellow, "{}\n", message); break; - case Severity::Info: + case DsVeosCoSim_Severity_Info: print(white, "{}\n", message); break; - case Severity::Trace: + case DsVeosCoSim_Severity_Trace: print(gray, "{}\n", message); break; - default: // NOLINT(clang-diagnostic-covered-switch-default) - break; } } @@ -129,7 +127,7 @@ void LogIoSignal(const DsVeosCoSim_IoSignal& ioSignal) { ioSignal.length); } -void LogIoData(SimulationTime simulationTime, +void LogIoData(DsVeosCoSim_SimulationTime simulationTime, const DsVeosCoSim_IoSignal& ioSignal, uint32_t length, const void* value) { @@ -174,7 +172,7 @@ void LogLinController(const DsVeosCoSim_LinController& controller) { controller.clusterName); } -void LogCanMessage(SimulationTime simulationTime, +void LogCanMessage(DsVeosCoSim_SimulationTime simulationTime, const DsVeosCoSim_CanController& controller, const DsVeosCoSim_CanMessage& message) { print(blue, @@ -187,7 +185,7 @@ void LogCanMessage(SimulationTime simulationTime, CanMessageFlagsToString(message.flags)); } -void LogEthMessage(SimulationTime simulationTime, +void LogEthMessage(DsVeosCoSim_SimulationTime simulationTime, const DsVeosCoSim_EthController& controller, const DsVeosCoSim_EthMessage& message) { const uint8_t* data = message.data; @@ -219,7 +217,7 @@ void LogEthMessage(SimulationTime simulationTime, } } -void LogLinMessage(SimulationTime simulationTime, +void LogLinMessage(DsVeosCoSim_SimulationTime simulationTime, const DsVeosCoSim_LinController& controller, const DsVeosCoSim_LinMessage& message) { print(green, diff --git a/shared/LogHelper.h b/shared/LogHelper.h index e33240c..158c9de 100644 --- a/shared/LogHelper.h +++ b/shared/LogHelper.h @@ -2,15 +2,18 @@ #pragma once -#include "CoSimTypes.h" +#include +#include + +#include "DsVeosCoSim/DsVeosCoSim.h" void InitializeOutput(); -void OnLogCallback(DsVeosCoSim::Severity severity, std::string_view message); +void OnLogCallback(DsVeosCoSim_Severity severity, std::string_view message); void LogIoSignal(const DsVeosCoSim_IoSignal& ioSignal); -void LogIoData(DsVeosCoSim::SimulationTime simulationTime, +void LogIoData(DsVeosCoSim_SimulationTime simulationTime, const DsVeosCoSim_IoSignal& ioSignal, uint32_t length, const void* value); @@ -19,13 +22,13 @@ void LogCanController(const DsVeosCoSim_CanController& controller); void LogEthController(const DsVeosCoSim_EthController& controller); void LogLinController(const DsVeosCoSim_LinController& controller); -void LogCanMessage(DsVeosCoSim::SimulationTime simulationTime, +void LogCanMessage(DsVeosCoSim_SimulationTime simulationTime, const DsVeosCoSim_CanController& controller, const DsVeosCoSim_CanMessage& message); -void LogEthMessage(DsVeosCoSim::SimulationTime simulationTime, +void LogEthMessage(DsVeosCoSim_SimulationTime simulationTime, const DsVeosCoSim_EthController& controller, const DsVeosCoSim_EthMessage& message); -void LogLinMessage(DsVeosCoSim::SimulationTime simulationTime, +void LogLinMessage(DsVeosCoSim_SimulationTime simulationTime, const DsVeosCoSim_LinController& controller, const DsVeosCoSim_LinMessage& message); diff --git a/src/BusBuffer.cpp b/src/BusBuffer.cpp index 85e2bd2..dd2ee3f 100644 --- a/src/BusBuffer.cpp +++ b/src/BusBuffer.cpp @@ -218,22 +218,22 @@ BusBuffer::BusBuffer(CoSimType coSimType, BusBuffer::BusBuffer(CoSimType coSimType, ConnectionKind connectionKind, const std::string& name, - const std::vector& controllers) - : BusBuffer(coSimType, connectionKind, name, controllers, {}, {}) { + const std::vector& canControllers) + : BusBuffer(coSimType, connectionKind, name, canControllers, {}, {}) { } BusBuffer::BusBuffer(CoSimType coSimType, ConnectionKind connectionKind, const std::string& name, - const std::vector& controllers) - : BusBuffer(coSimType, connectionKind, name, {}, controllers, {}) { + const std::vector& ethControllers) + : BusBuffer(coSimType, connectionKind, name, {}, ethControllers, {}) { } BusBuffer::BusBuffer(CoSimType coSimType, ConnectionKind connectionKind, const std::string& name, - const std::vector& controllers) - : BusBuffer(coSimType, connectionKind, name, {}, {}, controllers) { + const std::vector& linControllers) + : BusBuffer(coSimType, connectionKind, name, {}, {}, linControllers) { } void BusBuffer::ClearData() const { @@ -277,7 +277,9 @@ bool BusBuffer::Serialize(ChannelWriter& writer) const { return true; } -bool BusBuffer::Deserialize(ChannelReader& reader, SimulationTime simulationTime, const Callbacks& callbacks) const { +bool BusBuffer::Deserialize(ChannelReader& reader, + DsVeosCoSim_SimulationTime simulationTime, + const Callbacks& callbacks) const { CheckResultWithMessage(_canReceiveBuffer->Deserialize(reader, simulationTime, callbacks.canMessageReceivedCallback), "Could not receive CAN messages."); CheckResultWithMessage(_ethReceiveBuffer->Deserialize(reader, simulationTime, callbacks.ethMessageReceivedCallback), diff --git a/src/BusBuffer.h b/src/BusBuffer.h index 71c3f34..d65ac8c 100644 --- a/src/BusBuffer.h +++ b/src/BusBuffer.h @@ -44,11 +44,11 @@ concept Message = requires(TMessage& m, TMessageExtern& e, ChannelWriter& writer }; struct CanMessage { - SimulationTime timestamp{}; - BusControllerId controllerId{}; + DsVeosCoSim_SimulationTime timestamp{}; + DsVeosCoSim_BusControllerId controllerId{}; uint32_t controllerIndex{}; uint32_t id{}; - CanMessageFlags flags{}; + DsVeosCoSim_CanMessageFlags flags{}; uint32_t length{}; std::array data{}; @@ -57,17 +57,17 @@ struct CanMessage { void WriteTo(DsVeosCoSim_CanMessage& message) const; void ReadFrom(const DsVeosCoSim_CanMessage& message); - [[nodiscard]] operator DsVeosCoSim_CanMessage() const; + [[nodiscard]] operator DsVeosCoSim_CanMessage() const; // NOLINT private: void CheckMaxLength() const; }; struct EthMessage { - SimulationTime timestamp{}; - BusControllerId controllerId{}; + DsVeosCoSim_SimulationTime timestamp{}; + DsVeosCoSim_BusControllerId controllerId{}; uint32_t controllerIndex{}; - EthMessageFlags flags{}; + DsVeosCoSim_EthMessageFlags flags{}; uint32_t length{}; std::array data{}; @@ -76,18 +76,18 @@ struct EthMessage { void WriteTo(DsVeosCoSim_EthMessage& message) const; void ReadFrom(const DsVeosCoSim_EthMessage& message); - [[nodiscard]] operator DsVeosCoSim_EthMessage() const; + [[nodiscard]] operator DsVeosCoSim_EthMessage() const; // NOLINT private: void CheckMaxLength() const; }; struct LinMessage { - SimulationTime timestamp{}; - BusControllerId controllerId{}; + DsVeosCoSim_SimulationTime timestamp{}; + DsVeosCoSim_BusControllerId controllerId{}; uint32_t controllerIndex{}; uint32_t id{}; - LinMessageFlags flags{}; + DsVeosCoSim_LinMessageFlags flags{}; uint32_t length{}; std::array data{}; @@ -96,16 +96,16 @@ struct LinMessage { void WriteTo(DsVeosCoSim_LinMessage& message) const; void ReadFrom(const DsVeosCoSim_LinMessage& message); - [[nodiscard]] operator DsVeosCoSim_LinMessage() const; + [[nodiscard]] operator DsVeosCoSim_LinMessage() const; // NOLINT private: void CheckMaxLength() const; }; -template TMessage, ControllerExtern TControllerExtern> +template class BusProtocolBufferBase { protected: - using Callback = std::function; + using Callback = std::function; struct ControllerExtension { TControllerExtern info{}; @@ -122,7 +122,7 @@ class BusProtocolBufferBase { virtual ~BusProtocolBufferBase() noexcept = default; BusProtocolBufferBase(const BusProtocolBufferBase&) = delete; - BusProtocolBufferBase& operator=(BusProtocolBufferBase const&) = delete; + BusProtocolBufferBase& operator=(const BusProtocolBufferBase&) = delete; BusProtocolBufferBase(BusProtocolBufferBase&&) = delete; BusProtocolBufferBase& operator=(BusProtocolBufferBase&&) = delete; @@ -185,7 +185,9 @@ class BusProtocolBufferBase { return SerializeInternal(writer); } - [[nodiscard]] bool Deserialize(ChannelReader& reader, SimulationTime simulationTime, const Callback& callback) { + [[nodiscard]] bool Deserialize(ChannelReader& reader, + DsVeosCoSim_SimulationTime simulationTime, + const Callback& callback) { if (_coSimType == CoSimType::Client) { std::lock_guard lock(_mutex); return DeserializeInternal(reader, simulationTime, callback); @@ -204,10 +206,10 @@ class BusProtocolBufferBase { [[nodiscard]] virtual bool SerializeInternal(ChannelWriter& writer) = 0; [[nodiscard]] virtual bool DeserializeInternal(ChannelReader& reader, - SimulationTime simulationTime, + DsVeosCoSim_SimulationTime simulationTime, const Callback& callback) = 0; - [[nodiscard]] ControllerExtension& FindController(BusControllerId controllerId) { + [[nodiscard]] ControllerExtension& FindController(DsVeosCoSim_BusControllerId controllerId) { const auto search = _controllers.find(controllerId); if (search != _controllers.end()) { return search->second; @@ -216,7 +218,7 @@ class BusProtocolBufferBase { throw CoSimException(fmt::format("Controller id {} is unknown.", controllerId)); } - std::unordered_map _controllers; + std::unordered_map _controllers; private: CoSimType _coSimType{}; @@ -224,8 +226,8 @@ class BusProtocolBufferBase { }; template TMessage, ControllerExtern TControllerExtern> -class RemoteBusProtocolBuffer : public BusProtocolBufferBase { - using Base = BusProtocolBufferBase; +class RemoteBusProtocolBuffer final : public BusProtocolBufferBase { + using Base = BusProtocolBufferBase; using Extension = typename Base::ControllerExtension; public: @@ -233,7 +235,7 @@ class RemoteBusProtocolBuffer : public BusProtocolBufferBase TMessage, ControllerExtern TControllerExtern> -class LocalBusProtocolBuffer : public BusProtocolBufferBase { - using Base = BusProtocolBufferBase; +class LocalBusProtocolBuffer final : public BusProtocolBufferBase { + using Base = BusProtocolBufferBase; using Extension = typename Base::ControllerExtension; public: @@ -356,7 +358,7 @@ class LocalBusProtocolBuffer : public BusProtocolBufferBase; - using EthBufferBase = BusProtocolBufferBase; - using LinBufferBase = BusProtocolBufferBase; + using CanBufferBase = BusProtocolBufferBase; + using EthBufferBase = BusProtocolBufferBase; + using LinBufferBase = BusProtocolBufferBase; public: BusBuffer(CoSimType coSimType, @@ -524,7 +526,7 @@ class BusBuffer { [[nodiscard]] bool Serialize(ChannelWriter& writer) const; [[nodiscard]] bool Deserialize(ChannelReader& reader, - SimulationTime simulationTime, + DsVeosCoSim_SimulationTime simulationTime, const Callbacks& callbacks) const; private: diff --git a/src/CoSimClient.cpp b/src/CoSimClient.cpp index 009b4da..63c3e8e 100644 --- a/src/CoSimClient.cpp +++ b/src/CoSimClient.cpp @@ -62,19 +62,19 @@ void CoSimClient::Disconnect() { } } -ConnectionState CoSimClient::GetConnectionState() const { +DsVeosCoSim_ConnectionState CoSimClient::GetConnectionState() const { if (_isConnected) { - return ConnectionState::Connected; + return DsVeosCoSim_ConnectionState_Connected; } - return ConnectionState::Disconnected; + return DsVeosCoSim_ConnectionState_Disconnected; } void CoSimClient::SetCallbacks(const Callbacks& callbacks) { _callbacks = callbacks; if (_callbacks.callbacks.canMessageReceivedCallback) { - _callbacks.canMessageReceivedCallback = [this](SimulationTime simulationTime, + _callbacks.canMessageReceivedCallback = [this](DsVeosCoSim_SimulationTime simulationTime, const DsVeosCoSim_CanController& canController, const DsVeosCoSim_CanMessage& message) { _callbacks.callbacks.canMessageReceivedCallback(simulationTime, @@ -85,7 +85,7 @@ void CoSimClient::SetCallbacks(const Callbacks& callbacks) { } if (_callbacks.callbacks.ethMessageReceivedCallback) { - _callbacks.ethMessageReceivedCallback = [this](SimulationTime simulationTime, + _callbacks.ethMessageReceivedCallback = [this](DsVeosCoSim_SimulationTime simulationTime, const DsVeosCoSim_EthController& ethController, const DsVeosCoSim_EthMessage& message) { _callbacks.callbacks.ethMessageReceivedCallback(simulationTime, @@ -96,7 +96,7 @@ void CoSimClient::SetCallbacks(const Callbacks& callbacks) { } if (_callbacks.callbacks.linMessageReceivedCallback) { - _callbacks.linMessageReceivedCallback = [this](SimulationTime simulationTime, + _callbacks.linMessageReceivedCallback = [this](DsVeosCoSim_SimulationTime simulationTime, const DsVeosCoSim_LinController& linController, const DsVeosCoSim_LinMessage& message) { _callbacks.callbacks.linMessageReceivedCallback(simulationTime, @@ -107,7 +107,7 @@ void CoSimClient::SetCallbacks(const Callbacks& callbacks) { } if (_callbacks.callbacks.incomingSignalChangedCallback) { - _callbacks.incomingSignalChangedCallback = [this](SimulationTime simulationTime, + _callbacks.incomingSignalChangedCallback = [this](DsVeosCoSim_SimulationTime simulationTime, const DsVeosCoSim_IoSignal& ioSignal, uint32_t length, const void* value) { @@ -120,31 +120,32 @@ void CoSimClient::SetCallbacks(const Callbacks& callbacks) { } if (_callbacks.callbacks.simulationStartedCallback) { - _callbacks.simulationStartedCallback = [this](SimulationTime simulationTime) { + _callbacks.simulationStartedCallback = [this](DsVeosCoSim_SimulationTime simulationTime) { _callbacks.callbacks.simulationStartedCallback(simulationTime, _callbacks.callbacks.userData); }; } if (_callbacks.callbacks.simulationStoppedCallback) { - _callbacks.simulationStoppedCallback = [this](SimulationTime simulationTime) { + _callbacks.simulationStoppedCallback = [this](DsVeosCoSim_SimulationTime simulationTime) { _callbacks.callbacks.simulationStoppedCallback(simulationTime, _callbacks.callbacks.userData); }; } if (_callbacks.callbacks.simulationPausedCallback) { - _callbacks.simulationPausedCallback = [this](SimulationTime simulationTime) { + _callbacks.simulationPausedCallback = [this](DsVeosCoSim_SimulationTime simulationTime) { _callbacks.callbacks.simulationPausedCallback(simulationTime, _callbacks.callbacks.userData); }; } if (_callbacks.callbacks.simulationContinuedCallback) { - _callbacks.simulationContinuedCallback = [this](SimulationTime simulationTime) { + _callbacks.simulationContinuedCallback = [this](DsVeosCoSim_SimulationTime simulationTime) { _callbacks.callbacks.simulationContinuedCallback(simulationTime, _callbacks.callbacks.userData); }; } if (_callbacks.callbacks.simulationTerminatedCallback) { - _callbacks.simulationTerminatedCallback = [this](SimulationTime simulationTime, TerminateReason reason) { + _callbacks.simulationTerminatedCallback = [this](DsVeosCoSim_SimulationTime simulationTime, + DsVeosCoSim_TerminateReason reason) { _callbacks.callbacks.simulationTerminatedCallback(simulationTime, static_cast(reason), _callbacks.callbacks.userData); @@ -152,13 +153,13 @@ void CoSimClient::SetCallbacks(const Callbacks& callbacks) { } if (_callbacks.callbacks.simulationBeginStepCallback) { - _callbacks.simulationBeginStepCallback = [this](SimulationTime simulationTime) { + _callbacks.simulationBeginStepCallback = [this](DsVeosCoSim_SimulationTime simulationTime) { _callbacks.callbacks.simulationBeginStepCallback(simulationTime, _callbacks.callbacks.userData); }; } if (_callbacks.callbacks.simulationEndStepCallback) { - _callbacks.simulationEndStepCallback = [this](SimulationTime simulationTime) { + _callbacks.simulationEndStepCallback = [this](DsVeosCoSim_SimulationTime simulationTime) { _callbacks.callbacks.simulationEndStepCallback(simulationTime, _callbacks.callbacks.userData); }; } @@ -185,7 +186,7 @@ void CoSimClient::StartPollingBasedCoSimulation(const Callbacks& callbacks) { SetCallbacks(callbacks); } -bool CoSimClient::PollCommand(SimulationTime& simulationTime, Command& command, bool returnOnPing) { +bool CoSimClient::PollCommand(DsVeosCoSim_SimulationTime& simulationTime, Command& command, bool returnOnPing) { EnsureIsConnected(); EnsureIsInResponderModeNonBlocking(); @@ -217,7 +218,7 @@ bool CoSimClient::FinishCommand() { return true; } -void CoSimClient::SetNextSimulationTime(SimulationTime simulationTime) { +void CoSimClient::SetNextSimulationTime(DsVeosCoSim_SimulationTime simulationTime) { EnsureIsConnected(); _nextSimulationTime = simulationTime; @@ -235,14 +236,14 @@ void CoSimClient::Stop() { _nextCommand.exchange(Command::Stop); } -void CoSimClient::Terminate(TerminateReason terminateReason) { +void CoSimClient::Terminate(DsVeosCoSim_TerminateReason terminateReason) { EnsureIsConnected(); switch (terminateReason) { - case TerminateReason::Finished: + case DsVeosCoSim_TerminateReason_Finished: _nextCommand.exchange(Command::TerminateFinished); break; - case TerminateReason::Error: + case DsVeosCoSim_TerminateReason_Error: _nextCommand.exchange(Command::Terminate); break; default: // NOLINT(clang-diagnostic-covered-switch-default) @@ -262,7 +263,7 @@ void CoSimClient::Continue() { _nextCommand.exchange(Command::Continue); } -SimulationTime CoSimClient::GetStepSize() const { +DsVeosCoSim_SimulationTime CoSimClient::GetStepSize() const { EnsureIsConnected(); return _stepSize; @@ -296,19 +297,19 @@ std::vector CoSimClient::GetOutgoingSignals() const { return _outgoingSignals; } -void CoSimClient::Write(IoSignalId outgoingSignalId, uint32_t length, const void* value) const { +void CoSimClient::Write(DsVeosCoSim_IoSignalId outgoingSignalId, uint32_t length, const void* value) const { EnsureIsConnected(); _ioBuffer->Write(outgoingSignalId, length, value); } -void CoSimClient::Read(IoSignalId incomingSignalId, uint32_t& length, void* value) const { +void CoSimClient::Read(DsVeosCoSim_IoSignalId incomingSignalId, uint32_t& length, void* value) const { EnsureIsConnected(); _ioBuffer->Read(incomingSignalId, length, value); } -void CoSimClient::Read(IoSignalId incomingSignalId, uint32_t& length, const void** value) const { +void CoSimClient::Read(DsVeosCoSim_IoSignalId incomingSignalId, uint32_t& length, const void** value) const { EnsureIsConnected(); _ioBuffer->Read(incomingSignalId, length, value); @@ -615,7 +616,7 @@ bool CoSimClient::RunCallbackBasedCoSimulationInternal() { return true; } -bool CoSimClient::PollCommandInternal(SimulationTime& simulationTime, Command& command, bool returnOnPing) { +bool CoSimClient::PollCommandInternal(DsVeosCoSim_SimulationTime& simulationTime, Command& command, bool returnOnPing) { simulationTime = _currentSimulationTime; command = Command::Terminate; @@ -738,7 +739,7 @@ bool CoSimClient::OnStop() { } bool CoSimClient::OnTerminate() { - TerminateReason reason{}; + DsVeosCoSim_TerminateReason reason{}; CheckResultWithMessage(Protocol::ReadTerminate(_channel->GetReader(), _currentSimulationTime, reason), "Could not read terminate frame."); diff --git a/src/CoSimClient.h b/src/CoSimClient.h index 24e1c89..b5cdb5c 100644 --- a/src/CoSimClient.h +++ b/src/CoSimClient.h @@ -30,19 +30,19 @@ class CoSimClient final { [[nodiscard]] bool Connect(const ConnectConfig& connectConfig); void Disconnect(); - [[nodiscard]] ConnectionState GetConnectionState() const; + [[nodiscard]] DsVeosCoSim_ConnectionState GetConnectionState() const; - [[nodiscard]] SimulationTime GetStepSize() const; + [[nodiscard]] DsVeosCoSim_SimulationTime GetStepSize() const; [[nodiscard]] bool RunCallbackBasedCoSimulation(const Callbacks& callbacks); void StartPollingBasedCoSimulation(const Callbacks& callbacks); - [[nodiscard]] bool PollCommand(SimulationTime& simulationTime, Command& command, bool returnOnPing); + [[nodiscard]] bool PollCommand(DsVeosCoSim_SimulationTime& simulationTime, Command& command, bool returnOnPing); [[nodiscard]] bool FinishCommand(); - void SetNextSimulationTime(SimulationTime simulationTime); + void SetNextSimulationTime(DsVeosCoSim_SimulationTime simulationTime); void Start(); void Stop(); - void Terminate(TerminateReason terminateReason); + void Terminate(DsVeosCoSim_TerminateReason terminateReason); void Pause(); void Continue(); @@ -52,10 +52,10 @@ class CoSimClient final { [[nodiscard]] std::vector GetIncomingSignals() const; [[nodiscard]] std::vector GetOutgoingSignals() const; - void Write(IoSignalId outgoingSignalId, uint32_t length, const void* value) const; + void Write(DsVeosCoSim_IoSignalId outgoingSignalId, uint32_t length, const void* value) const; - void Read(IoSignalId incomingSignalId, uint32_t& length, void* value) const; - void Read(IoSignalId incomingSignalId, uint32_t& length, const void** value) const; + void Read(DsVeosCoSim_IoSignalId incomingSignalId, uint32_t& length, void* value) const; + void Read(DsVeosCoSim_IoSignalId incomingSignalId, uint32_t& length, const void** value) const; void GetCanControllers(uint32_t* controllersCount, const DsVeosCoSim_CanController** controllers) const; void GetEthControllers(uint32_t* controllersCount, const DsVeosCoSim_EthController** controllers) const; @@ -86,7 +86,9 @@ class CoSimClient final { [[nodiscard]] bool ReceiveConnectResponse(); [[nodiscard]] bool RunCallbackBasedCoSimulationInternal(); - [[nodiscard]] bool PollCommandInternal(SimulationTime& simulationTime, Command& command, bool returnOnPing); + [[nodiscard]] bool PollCommandInternal(DsVeosCoSim_SimulationTime& simulationTime, + Command& command, + bool returnOnPing); [[nodiscard]] bool FinishCommandInternal(); [[nodiscard]] bool OnStep(); @@ -107,10 +109,10 @@ class CoSimClient final { bool _isConnected{}; Callbacks _callbacks{}; - SimulationTime _currentSimulationTime{}; - SimulationTime _nextSimulationTime{}; + DsVeosCoSim_SimulationTime _currentSimulationTime{}; + DsVeosCoSim_SimulationTime _nextSimulationTime{}; - SimulationTime _stepSize{}; + DsVeosCoSim_SimulationTime _stepSize{}; std::string _remoteIpAddress; std::string _serverName; diff --git a/src/CoSimServer.cpp b/src/CoSimServer.cpp index 493d20d..f1145ff 100644 --- a/src/CoSimServer.cpp +++ b/src/CoSimServer.cpp @@ -60,7 +60,7 @@ void CoSimServer::Unload() { } } -void CoSimServer::Start(SimulationTime simulationTime) { +void CoSimServer::Start(DsVeosCoSim_SimulationTime simulationTime) { if (!_channel) { if (_isClientOptional) { return; @@ -83,7 +83,7 @@ void CoSimServer::Start(SimulationTime simulationTime) { } } -void CoSimServer::Stop(SimulationTime simulationTime) { +void CoSimServer::Stop(DsVeosCoSim_SimulationTime simulationTime) { if (!_channel) { return; } @@ -93,7 +93,7 @@ void CoSimServer::Stop(SimulationTime simulationTime) { } } -void CoSimServer::Terminate(SimulationTime simulationTime, TerminateReason reason) { +void CoSimServer::Terminate(DsVeosCoSim_SimulationTime simulationTime, DsVeosCoSim_TerminateReason reason) { if (!_channel) { return; } @@ -103,7 +103,7 @@ void CoSimServer::Terminate(SimulationTime simulationTime, TerminateReason reaso } } -void CoSimServer::Pause(SimulationTime simulationTime) { +void CoSimServer::Pause(DsVeosCoSim_SimulationTime simulationTime) { if (!_channel) { return; } @@ -113,7 +113,7 @@ void CoSimServer::Pause(SimulationTime simulationTime) { } } -void CoSimServer::Continue(SimulationTime simulationTime) { +void CoSimServer::Continue(DsVeosCoSim_SimulationTime simulationTime) { if (!_channel) { return; } @@ -123,7 +123,7 @@ void CoSimServer::Continue(SimulationTime simulationTime) { } } -void CoSimServer::Step(SimulationTime simulationTime, SimulationTime& nextSimulationTime) { +void CoSimServer::Step(DsVeosCoSim_SimulationTime simulationTime, DsVeosCoSim_SimulationTime& nextSimulationTime) { if (!_channel) { return; } @@ -137,7 +137,7 @@ void CoSimServer::Step(SimulationTime simulationTime, SimulationTime& nextSimula HandlePendingCommand(command); } -void CoSimServer::Write(IoSignalId signalId, uint32_t length, const void* value) const { +void CoSimServer::Write(DsVeosCoSim_IoSignalId signalId, uint32_t length, const void* value) const { if (!_channel) { return; } @@ -145,7 +145,7 @@ void CoSimServer::Write(IoSignalId signalId, uint32_t length, const void* value) _ioBuffer->Write(signalId, length, value); } -void CoSimServer::Read(IoSignalId signalId, uint32_t& length, const void** value) const { +void CoSimServer::Read(DsVeosCoSim_IoSignalId signalId, uint32_t& length, const void** value) const { if (!_channel) { return; } @@ -206,40 +206,41 @@ uint16_t CoSimServer::GetLocalPort() const { return 0; } -bool CoSimServer::StartInternal(SimulationTime simulationTime) const { +bool CoSimServer::StartInternal(DsVeosCoSim_SimulationTime simulationTime) const { CheckResultWithMessage(Protocol::SendStart(_channel->GetWriter(), simulationTime), "Could not send start frame."); CheckResultWithMessage(WaitForOkFrame(), "Could not receive ok frame."); return true; } -bool CoSimServer::StopInternal(SimulationTime simulationTime) const { +bool CoSimServer::StopInternal(DsVeosCoSim_SimulationTime simulationTime) const { CheckResultWithMessage(Protocol::SendStop(_channel->GetWriter(), simulationTime), "Could not send stop frame."); CheckResultWithMessage(WaitForOkFrame(), "Could not receive ok frame."); return true; } -bool CoSimServer::TerminateInternal(SimulationTime simulationTime, TerminateReason reason) const { +bool CoSimServer::TerminateInternal(DsVeosCoSim_SimulationTime simulationTime, + DsVeosCoSim_TerminateReason reason) const { CheckResultWithMessage(Protocol::SendTerminate(_channel->GetWriter(), simulationTime, reason), "Could not send terminate frame."); CheckResultWithMessage(WaitForOkFrame(), "Could not receive ok frame."); return true; } -bool CoSimServer::PauseInternal(SimulationTime simulationTime) const { +bool CoSimServer::PauseInternal(DsVeosCoSim_SimulationTime simulationTime) const { CheckResultWithMessage(Protocol::SendPause(_channel->GetWriter(), simulationTime), "Could not send pause frame."); CheckResultWithMessage(WaitForOkFrame(), "Could not receive ok frame."); return true; } -bool CoSimServer::ContinueInternal(SimulationTime simulationTime) const { +bool CoSimServer::ContinueInternal(DsVeosCoSim_SimulationTime simulationTime) const { CheckResultWithMessage(Protocol::SendContinue(_channel->GetWriter(), simulationTime), "Could not send continue frame."); CheckResultWithMessage(WaitForOkFrame(), "Could not receive ok frame."); return true; } -bool CoSimServer::StepInternal(SimulationTime simulationTime, - SimulationTime& nextSimulationTime, +bool CoSimServer::StepInternal(DsVeosCoSim_SimulationTime simulationTime, + DsVeosCoSim_SimulationTime& nextSimulationTime, Command& command) const { CheckResultWithMessage(Protocol::SendStep(_channel->GetWriter(), simulationTime, *_ioBuffer, *_busBuffer), "Could not send step frame."); @@ -460,7 +461,7 @@ bool CoSimServer::WaitForConnectFrame(uint32_t& version, std::string& clientName } } -bool CoSimServer::WaitForStepOkFrame(SimulationTime& simulationTime, Command& command) const { +bool CoSimServer::WaitForStepOkFrame(DsVeosCoSim_SimulationTime& simulationTime, Command& command) const { FrameKind frameKind{}; CheckResult(Protocol::ReceiveHeader(_channel->GetReader(), frameKind)); @@ -494,7 +495,7 @@ void CoSimServer::HandlePendingCommand(Command command) const { _callbacks.simulationStoppedCallback({}); break; case Command::Terminate: - _callbacks.simulationTerminatedCallback({}, TerminateReason::Error); + _callbacks.simulationTerminatedCallback({}, DsVeosCoSim_TerminateReason_Error); break; case Command::Pause: _callbacks.simulationPausedCallback({}); @@ -503,7 +504,7 @@ void CoSimServer::HandlePendingCommand(Command command) const { _callbacks.simulationContinuedCallback({}); break; case Command::TerminateFinished: - _callbacks.simulationTerminatedCallback({}, TerminateReason::Finished); + _callbacks.simulationTerminatedCallback({}, DsVeosCoSim_TerminateReason_Finished); break; case Command::None: case Command::Step: diff --git a/src/CoSimServer.h b/src/CoSimServer.h index 9e5891b..0dfa958 100644 --- a/src/CoSimServer.h +++ b/src/CoSimServer.h @@ -24,7 +24,7 @@ struct CoSimServerConfig { bool isClientOptional{}; bool startPortMapper{}; bool registerAtPortMapper = true; - SimulationTime stepSize{}; + DsVeosCoSim_SimulationTime stepSize{}; LogCallback logCallback; SimulationCallback simulationStartedCallback; SimulationCallback simulationStoppedCallback; @@ -55,16 +55,16 @@ class CoSimServer final { void Load(const CoSimServerConfig& config); void Unload(); - void Start(SimulationTime simulationTime); - void Stop(SimulationTime simulationTime); - void Terminate(SimulationTime simulationTime, TerminateReason reason); - void Pause(SimulationTime simulationTime); - void Continue(SimulationTime simulationTime); - void Step(SimulationTime simulationTime, SimulationTime& nextSimulationTime); + void Start(DsVeosCoSim_SimulationTime simulationTime); + void Stop(DsVeosCoSim_SimulationTime simulationTime); + void Terminate(DsVeosCoSim_SimulationTime simulationTime, DsVeosCoSim_TerminateReason reason); + void Pause(DsVeosCoSim_SimulationTime simulationTime); + void Continue(DsVeosCoSim_SimulationTime simulationTime); + void Step(DsVeosCoSim_SimulationTime simulationTime, DsVeosCoSim_SimulationTime& nextSimulationTime); - void Write(IoSignalId signalId, uint32_t length, const void* value) const; + void Write(DsVeosCoSim_IoSignalId signalId, uint32_t length, const void* value) const; - void Read(IoSignalId signalId, uint32_t& length, const void** value) const; + void Read(DsVeosCoSim_IoSignalId signalId, uint32_t& length, const void** value) const; [[nodiscard]] bool Transmit(const DsVeosCoSim_CanMessage& message) const; [[nodiscard]] bool Transmit(const DsVeosCoSim_EthMessage& message) const; @@ -75,13 +75,14 @@ class CoSimServer final { [[nodiscard]] uint16_t GetLocalPort() const; private: - [[nodiscard]] bool StartInternal(SimulationTime simulationTime) const; - [[nodiscard]] bool StopInternal(SimulationTime simulationTime) const; - [[nodiscard]] bool TerminateInternal(SimulationTime simulationTime, TerminateReason reason) const; - [[nodiscard]] bool PauseInternal(SimulationTime simulationTime) const; - [[nodiscard]] bool ContinueInternal(SimulationTime simulationTime) const; - [[nodiscard]] bool StepInternal(SimulationTime simulationTime, - SimulationTime& nextSimulationTime, + [[nodiscard]] bool StartInternal(DsVeosCoSim_SimulationTime simulationTime) const; + [[nodiscard]] bool StopInternal(DsVeosCoSim_SimulationTime simulationTime) const; + [[nodiscard]] bool TerminateInternal(DsVeosCoSim_SimulationTime simulationTime, + DsVeosCoSim_TerminateReason reason) const; + [[nodiscard]] bool PauseInternal(DsVeosCoSim_SimulationTime simulationTime) const; + [[nodiscard]] bool ContinueInternal(DsVeosCoSim_SimulationTime simulationTime) const; + [[nodiscard]] bool StepInternal(DsVeosCoSim_SimulationTime simulationTime, + DsVeosCoSim_SimulationTime& nextSimulationTime, Command& command) const; void CloseConnection(); @@ -94,7 +95,7 @@ class CoSimServer final { [[nodiscard]] bool WaitForOkFrame() const; [[nodiscard]] bool WaitForPingOkFrame(Command& command) const; [[nodiscard]] bool WaitForConnectFrame(uint32_t& version, std::string& clientName) const; - [[nodiscard]] bool WaitForStepOkFrame(SimulationTime& simulationTime, Command& command) const; + [[nodiscard]] bool WaitForStepOkFrame(DsVeosCoSim_SimulationTime& simulationTime, Command& command) const; void HandlePendingCommand(Command command) const; @@ -115,7 +116,7 @@ class CoSimServer final { std::string _serverName; Callbacks _callbacks{}; bool _isClientOptional{}; - SimulationTime _stepSize{}; + DsVeosCoSim_SimulationTime _stepSize{}; bool _registerAtPortMapper{}; std::vector _incomingSignals; diff --git a/src/CoSimTypes.h b/src/CoSimTypes.h index 445f9d9..9e75589 100644 --- a/src/CoSimTypes.h +++ b/src/CoSimTypes.h @@ -7,7 +7,7 @@ #include #include #include -#include +#include // NOLINT #include "DsVeosCoSim/DsVeosCoSim.h" @@ -18,9 +18,7 @@ constexpr uint32_t EthMessageMaxLength = DSVEOSCOSIM_ETH_MESSAGE_MAX_LENGTH; // constexpr uint32_t LinMessageMaxLength = DSVEOSCOSIM_LIN_MESSAGE_MAX_LENGTH; // NOLINT constexpr uint32_t EthAddressLength = DSVEOSCOSIM_ETH_ADDRESS_LENGTH; -using SimulationTime = DsVeosCoSim_SimulationTime; - -[[nodiscard]] inline double SimulationTimeToSeconds(SimulationTime simulationTime) { +[[nodiscard]] inline double SimulationTimeToSeconds(DsVeosCoSim_SimulationTime simulationTime) { return DSVEOSCOSIM_SIMULATION_TIME_TO_SECONDS(simulationTime); } @@ -99,58 +97,41 @@ enum class Command { return std::to_string(static_cast(command)); } -enum class Severity { - Error = DsVeosCoSim_Severity_Error, - Warning = DsVeosCoSim_Severity_Warning, - Info = DsVeosCoSim_Severity_Info, - Trace = DsVeosCoSim_Severity_Trace -}; - -[[nodiscard]] inline std::string ToString(Severity severity) { - switch (severity) { - case Severity::Error: +[[nodiscard]] inline std::string ToString(DsVeosCoSim_Severity severity) { + switch (severity) { // NOLINT + case DsVeosCoSim_Severity_Error: return "Error"; - case Severity::Warning: + case DsVeosCoSim_Severity_Warning: return "Warning"; - case Severity::Info: + case DsVeosCoSim_Severity_Info: return "Info"; - case Severity::Trace: + case DsVeosCoSim_Severity_Trace: return "Trace"; } - return std::to_string(static_cast(severity)); + return std::to_string(severity); } -enum class TerminateReason { - Finished = DsVeosCoSim_TerminateReason_Finished, - Error = DsVeosCoSim_TerminateReason_Error -}; - -inline std::string ToString(TerminateReason terminateReason) { - switch (terminateReason) { - case TerminateReason::Finished: +inline std::string ToString(DsVeosCoSim_TerminateReason terminateReason) { + switch (terminateReason) { // NOLINT + case DsVeosCoSim_TerminateReason_Finished: return "Finished"; - case TerminateReason::Error: + case DsVeosCoSim_TerminateReason_Error: return "Error"; } - return std::to_string(static_cast(terminateReason)); + return std::to_string(terminateReason); } -enum class ConnectionState { - Connected = DsVeosCoSim_ConnectionState_Connected, - Disconnected = DsVeosCoSim_ConnectionState_Disconnected -}; - -inline std::string ToString(ConnectionState connectionState) { - switch (connectionState) { - case ConnectionState::Connected: +inline std::string ToString(DsVeosCoSim_ConnectionState connectionState) { + switch (connectionState) { // NOLINT + case DsVeosCoSim_ConnectionState_Connected: return "Connected"; - case ConnectionState::Disconnected: + case DsVeosCoSim_ConnectionState_Disconnected: return "Disconnected"; } - return std::to_string(static_cast(connectionState)); + return std::to_string(connectionState); } [[nodiscard]] inline size_t GetDataTypeSize(DsVeosCoSim_DataType dataType) { @@ -240,9 +221,7 @@ enum class SimulationState { enum class Mode { }; -using CanMessageFlags = DsVeosCoSim_CanMessageFlags; - -[[nodiscard]] inline std::string CanMessageFlagsToString(CanMessageFlags flags) { +[[nodiscard]] inline std::string CanMessageFlagsToString(DsVeosCoSim_CanMessageFlags flags) { std::string flagsStr; if ((flags & DsVeosCoSim_CanMessageFlags_Loopback) != 0) { @@ -276,9 +255,7 @@ using CanMessageFlags = DsVeosCoSim_CanMessageFlags; return flagsStr; } -using EthMessageFlags = DsVeosCoSim_EthMessageFlags; - -[[nodiscard]] inline std::string EthMessageFlagsToString(EthMessageFlags flags) { +[[nodiscard]] inline std::string EthMessageFlagsToString(DsVeosCoSim_EthMessageFlags flags) { std::string flagsStr; if ((flags & DsVeosCoSim_EthMessageFlags_Loopback) != 0) { @@ -300,9 +277,7 @@ using EthMessageFlags = DsVeosCoSim_EthMessageFlags; return flagsStr; } -using LinMessageFlags = DsVeosCoSim_LinMessageFlags; - -[[nodiscard]] inline std::string LinMessageFlagsToString(LinMessageFlags flags) { +[[nodiscard]] inline std::string LinMessageFlagsToString(DsVeosCoSim_LinMessageFlags flags) { std::string flagsStr; if ((flags & DsVeosCoSim_LinMessageFlags_Loopback) != 0) { @@ -360,16 +335,14 @@ using LinMessageFlags = DsVeosCoSim_LinMessageFlags; return flagsStr; } -using IoSignalId = DsVeosCoSim_IoSignalId; - struct IoSignal { - IoSignalId id{}; + DsVeosCoSim_IoSignalId id{}; uint32_t length{}; DsVeosCoSim_DataType dataType{}; DsVeosCoSim_SizeKind sizeKind{}; std::string name; - [[nodiscard]] operator DsVeosCoSim_IoSignal() const { + [[nodiscard]] operator DsVeosCoSim_IoSignal() const { // NOLINT DsVeosCoSim_IoSignal signal{}; signal.id = id; signal.length = length; @@ -391,10 +364,8 @@ struct IoSignal { return ioSignals; } -using BusControllerId = DsVeosCoSim_BusControllerId; - struct CanController { - BusControllerId id{}; + DsVeosCoSim_BusControllerId id{}; uint32_t queueSize{}; uint64_t bitsPerSecond{}; uint64_t flexibleDataRateBitsPerSecond{}; @@ -402,7 +373,7 @@ struct CanController { std::string channelName; std::string clusterName; - [[nodiscard]] operator DsVeosCoSim_CanController() const { + [[nodiscard]] operator DsVeosCoSim_CanController() const { // NOLINT DsVeosCoSim_CanController controller{}; controller.id = id; controller.queueSize = queueSize; @@ -427,7 +398,7 @@ struct CanController { } struct EthController { - BusControllerId id{}; + DsVeosCoSim_BusControllerId id{}; uint32_t queueSize{}; uint64_t bitsPerSecond{}; std::array macAddress{}; @@ -435,7 +406,7 @@ struct EthController { std::string channelName; std::string clusterName; - [[nodiscard]] operator DsVeosCoSim_EthController() const { + [[nodiscard]] operator DsVeosCoSim_EthController() const { // NOLINT DsVeosCoSim_EthController controller{}; controller.id = id; controller.queueSize = queueSize; @@ -460,7 +431,7 @@ struct EthController { } struct LinController { - BusControllerId id{}; + DsVeosCoSim_BusControllerId id{}; uint32_t queueSize{}; uint64_t bitsPerSecond{}; DsVeosCoSim_LinControllerType type{}; @@ -468,7 +439,7 @@ struct LinController { std::string channelName; std::string clusterName; - [[nodiscard]] operator DsVeosCoSim_LinController() const { + [[nodiscard]] operator DsVeosCoSim_LinController() const { // NOLINT DsVeosCoSim_LinController controller{}; controller.id = id; controller.queueSize = queueSize; @@ -492,19 +463,22 @@ struct LinController { return linControllers; } -using LogCallback = std::function; +using LogCallback = std::function; -using SimulationCallback = std::function; -using SimulationTerminatedCallback = std::function; -using IncomingSignalChangedCallback = std::function< - void(SimulationTime simulationTime, const DsVeosCoSim_IoSignal& ioSignal, uint32_t length, const void* value)>; -using CanMessageReceivedCallback = std::function; +using SimulationTerminatedCallback = + std::function; +using IncomingSignalChangedCallback = std::function; +using CanMessageReceivedCallback = std::function; -using EthMessageReceivedCallback = std::function; -using LinMessageReceivedCallback = std::function; diff --git a/src/Communication/Channel.h b/src/Communication/Channel.h index 11a9835..87a6559 100644 --- a/src/Communication/Channel.h +++ b/src/Communication/Channel.h @@ -14,6 +14,12 @@ class ChannelWriter { public: virtual ~ChannelWriter() noexcept = default; + ChannelWriter(const ChannelWriter&) = delete; + ChannelWriter& operator=(const ChannelWriter&) = delete; + + ChannelWriter(ChannelWriter&&) noexcept = default; + ChannelWriter& operator=(ChannelWriter&&) noexcept = default; + template [[nodiscard]] bool Write(const T& value) { static_assert(std::is_trivially_copyable_v); @@ -33,6 +39,12 @@ class ChannelReader { public: virtual ~ChannelReader() noexcept = default; + ChannelReader(const ChannelReader&) = delete; + ChannelReader& operator=(const ChannelReader&) = delete; + + ChannelReader(ChannelReader&&) noexcept = default; + ChannelReader& operator=(ChannelReader&&) noexcept = default; + template [[nodiscard]] bool Read(T& value) { static_assert(std::is_trivially_copyable_v); @@ -50,6 +62,12 @@ class Channel { public: virtual ~Channel() noexcept = default; + Channel(const Channel&) = delete; + Channel& operator=(const Channel&) = delete; + + Channel(Channel&&) noexcept = default; + Channel& operator=(Channel&&) noexcept = default; + virtual void Disconnect() = 0; [[nodiscard]] virtual ChannelWriter& GetWriter() = 0; diff --git a/src/Communication/LocalChannel.cpp b/src/Communication/LocalChannel.cpp index 4714d8e..854bdee 100644 --- a/src/Communication/LocalChannel.cpp +++ b/src/Communication/LocalChannel.cpp @@ -196,21 +196,11 @@ bool LocalChannelWriter::EndWrite() { } bool LocalChannelWriter::WaitForFreeSpace(uint32_t& currentSize) { -#if 0 - _newDataEvent.Set(); - for (int32_t i = 0; i < 1000; i++) { - currentSize = _writeIndex - _header->readIndex.load(); - if (currentSize < BufferSize) { - return true; - } - } -#else (void)SignalAndWait(_newDataEvent, _newSpaceEvent, 1); currentSize = _writeIndex - _header->readIndex.load(); if (currentSize < BufferSize) { return true; } -#endif while (!_newSpaceEvent.Wait(1)) { currentSize = _writeIndex - _header->readIndex.load(); @@ -271,15 +261,6 @@ bool LocalChannelReader::Read(void* destination, size_t size) { } bool LocalChannelReader::BeginRead(uint32_t& currentSize) { -#if 0 - for (int32_t i = 0; i < 1000; i++) { - currentSize = _header->writeIndex.load() - _readIndex; - if (currentSize > 0) { - return true; - } - } -#endif - while (!_newDataEvent.Wait(1)) { currentSize = _header->writeIndex.load() - _readIndex; if (currentSize > 0) { diff --git a/src/Communication/LocalChannel.h b/src/Communication/LocalChannel.h index db013ff..1db58a6 100644 --- a/src/Communication/LocalChannel.h +++ b/src/Communication/LocalChannel.h @@ -123,7 +123,7 @@ class LocalChannelServer final { ~LocalChannelServer() noexcept = default; LocalChannelServer(const LocalChannelServer&) = delete; - LocalChannelServer& operator=(LocalChannelServer const&) = delete; + LocalChannelServer& operator=(const LocalChannelServer&) = delete; LocalChannelServer(LocalChannelServer&&) = delete; LocalChannelServer& operator=(LocalChannelServer&&) = delete; diff --git a/src/Communication/SocketChannel.h b/src/Communication/SocketChannel.h index c6f7c1e..327c800 100644 --- a/src/Communication/SocketChannel.h +++ b/src/Communication/SocketChannel.h @@ -91,7 +91,7 @@ class TcpChannelServer final { ~TcpChannelServer() noexcept = default; TcpChannelServer(const TcpChannelServer&) = delete; - TcpChannelServer& operator=(TcpChannelServer const&) = delete; + TcpChannelServer& operator=(const TcpChannelServer&) = delete; TcpChannelServer(TcpChannelServer&&) = delete; TcpChannelServer& operator=(TcpChannelServer&&) = delete; @@ -114,7 +114,7 @@ class UdsChannelServer final { ~UdsChannelServer() noexcept = default; UdsChannelServer(const UdsChannelServer&) = delete; - UdsChannelServer& operator=(UdsChannelServer const&) = delete; + UdsChannelServer& operator=(const UdsChannelServer&) = delete; UdsChannelServer(UdsChannelServer&&) = delete; UdsChannelServer& operator=(UdsChannelServer&&) = delete; diff --git a/src/DsVeosCoSim.cpp b/src/DsVeosCoSim.cpp index c6064c5..ccfc7de 100644 --- a/src/DsVeosCoSim.cpp +++ b/src/DsVeosCoSim.cpp @@ -1,6 +1,7 @@ // Copyright dSPACE GmbH. All rights reserved. #include "DsVeosCoSim/DsVeosCoSim.h" + #include #include "CoSimClient.h" @@ -25,9 +26,9 @@ DsVeosCoSim_LogCallback g_logCallback; void DsVeosCoSim_SetLogCallback(DsVeosCoSim_LogCallback logCallback) { g_logCallback = logCallback; - SetLogCallback([](Severity severity, std::string_view message) { + SetLogCallback([](DsVeosCoSim_Severity severity, std::string_view message) { if (g_logCallback) { - g_logCallback(static_cast(severity), message.data()); + g_logCallback(severity, message.data()); } }); } @@ -47,7 +48,7 @@ void DsVeosCoSim_Destroy(DsVeosCoSim_Handle handle) { delete client; } -DsVeosCoSim_Result DsVeosCoSim_Connect(DsVeosCoSim_Handle handle, DsVeosCoSim_ConnectConfig connectConfig) { +DsVeosCoSim_Result DsVeosCoSim_Connect(DsVeosCoSim_Handle handle, DsVeosCoSim_ConnectConfig connectConfig) { // NOLINT CheckNotNull(handle); auto* const client = static_cast(handle); @@ -105,7 +106,7 @@ DsVeosCoSim_Result DsVeosCoSim_GetConnectionState(DsVeosCoSim_Handle handle, const auto* const client = static_cast(handle); try { - *connectionState = static_cast(client->GetConnectionState()); + *connectionState = client->GetConnectionState(); return DsVeosCoSim_Result_Ok; } catch (const std::exception& exception) { @@ -116,7 +117,7 @@ DsVeosCoSim_Result DsVeosCoSim_GetConnectionState(DsVeosCoSim_Handle handle, } DsVeosCoSim_Result DsVeosCoSim_RunCallbackBasedCoSimulation(DsVeosCoSim_Handle handle, - DsVeosCoSim_Callbacks callbacks) { + DsVeosCoSim_Callbacks callbacks) { // NOLINT CheckNotNull(handle); auto* const client = static_cast(handle); @@ -138,7 +139,7 @@ DsVeosCoSim_Result DsVeosCoSim_RunCallbackBasedCoSimulation(DsVeosCoSim_Handle h } DsVeosCoSim_Result DsVeosCoSim_StartPollingBasedCoSimulation(DsVeosCoSim_Handle handle, - DsVeosCoSim_Callbacks callbacks) { + DsVeosCoSim_Callbacks callbacks) { // NOLINT CheckNotNull(handle); auto* const client = static_cast(handle); diff --git a/src/Helpers/Logger.cpp b/src/Helpers/Logger.cpp index 8ddf847..6e97e7c 100644 --- a/src/Helpers/Logger.cpp +++ b/src/Helpers/Logger.cpp @@ -17,28 +17,28 @@ void SetLogCallback(LogCallback logCallback) { void LogError(std::string_view message) { const auto logCallback = g_logCallback; if (logCallback) { - logCallback(Severity::Error, message.data()); + logCallback(DsVeosCoSim_Severity_Error, message.data()); } } void LogWarning(std::string_view message) { const auto logCallback = g_logCallback; if (logCallback) { - logCallback(Severity::Warning, message); + logCallback(DsVeosCoSim_Severity_Warning, message); } } void LogInfo(std::string_view message) { const auto logCallback = g_logCallback; if (logCallback) { - logCallback(Severity::Info, message); + logCallback(DsVeosCoSim_Severity_Info, message); } } void LogTrace(std::string_view message) { const auto logCallback = g_logCallback; if (logCallback) { - logCallback(Severity::Trace, message); + logCallback(DsVeosCoSim_Severity_Trace, message); } } diff --git a/src/Helpers/RingBuffer.h b/src/Helpers/RingBuffer.h index e2d4483..f730eb4 100644 --- a/src/Helpers/RingBuffer.h +++ b/src/Helpers/RingBuffer.h @@ -54,7 +54,7 @@ class RingBuffer final { _writeIndex = 0; } - _size++; + ++_size; } [[nodiscard]] T& PopFront() { @@ -62,7 +62,7 @@ class RingBuffer final { throw std::runtime_error("Ring buffer is empty."); } - _size--; + --_size; T& item = _items[_readIndex]; diff --git a/src/Helpers/ShmRingBuffer.h b/src/Helpers/ShmRingBuffer.h index fe150b3..83828a0 100644 --- a/src/Helpers/ShmRingBuffer.h +++ b/src/Helpers/ShmRingBuffer.h @@ -55,7 +55,7 @@ class ShmRingBuffer final { _writeIndex = 0; } - _size++; + ++_size; } [[nodiscard]] T& PopFront() { @@ -63,7 +63,7 @@ class ShmRingBuffer final { throw std::runtime_error("SHM ring buffer is empty."); } - _size--; + --_size; T& item = _items[_readIndex]; @@ -81,8 +81,8 @@ class ShmRingBuffer final { uint32_t _readIndex{}; // Read and written by reader uint32_t _writeIndex{}; // Read and written by writer - // Zero sized array would be correct here, since the items are inside of a shared memory. But that leads to - // warnings, so we add an additional element here + // Zero sized array would be correct here, since the items are inside a shared memory. But that leads to + // warnings, so we add set the size to 1 T _items[1]{}; }; diff --git a/src/IoBuffer.cpp b/src/IoBuffer.cpp index a5a2312..b7f36e2 100644 --- a/src/IoBuffer.cpp +++ b/src/IoBuffer.cpp @@ -14,11 +14,11 @@ namespace DsVeosCoSim { namespace { void CheckSizeKind(DsVeosCoSim_SizeKind sizeKind, const std::string& name) { - switch (sizeKind) { + switch (sizeKind) { // NOLINT case DsVeosCoSim_SizeKind_Fixed: case DsVeosCoSim_SizeKind_Variable: return; - default: // NOLINT(clang-diagnostic-covered-switch-default) + default: throw CoSimException(fmt::format("Unknown size kind '{}' for IO signal '{}'.", ToString(sizeKind), name)); } } @@ -69,7 +69,7 @@ void IoPartBufferBase::ClearData() { ClearDataInternal(); } -void IoPartBufferBase::Write(IoSignalId signalId, uint32_t length, const void* value) { +void IoPartBufferBase::Write(DsVeosCoSim_IoSignalId signalId, uint32_t length, const void* value) { if (_coSimType == CoSimType::Client) { std::lock_guard lock(_mutex); WriteInternal(signalId, length, value); @@ -79,7 +79,7 @@ void IoPartBufferBase::Write(IoSignalId signalId, uint32_t length, const void* v WriteInternal(signalId, length, value); } -void IoPartBufferBase::Read(IoSignalId signalId, uint32_t& length, void* value) { +void IoPartBufferBase::Read(DsVeosCoSim_IoSignalId signalId, uint32_t& length, void* value) { if (_coSimType == CoSimType::Client) { std::lock_guard lock(_mutex); ReadInternal(signalId, length, value); @@ -89,7 +89,7 @@ void IoPartBufferBase::Read(IoSignalId signalId, uint32_t& length, void* value) ReadInternal(signalId, length, value); } -void IoPartBufferBase::Read(IoSignalId signalId, uint32_t& length, const void** value) { +void IoPartBufferBase::Read(DsVeosCoSim_IoSignalId signalId, uint32_t& length, const void** value) { if (_coSimType == CoSimType::Client) { std::lock_guard lock(_mutex); ReadInternal(signalId, length, value); @@ -108,7 +108,9 @@ bool IoPartBufferBase::Serialize(ChannelWriter& writer) { return SerializeInternal(writer); } -bool IoPartBufferBase::Deserialize(ChannelReader& reader, SimulationTime simulationTime, const Callbacks& callbacks) { +bool IoPartBufferBase::Deserialize(ChannelReader& reader, + DsVeosCoSim_SimulationTime simulationTime, + const Callbacks& callbacks) { if (_coSimType == CoSimType::Client) { std::lock_guard lock(_mutex); return DeserializeInternal(reader, simulationTime, callbacks); @@ -117,7 +119,7 @@ bool IoPartBufferBase::Deserialize(ChannelReader& reader, SimulationTime simulat return DeserializeInternal(reader, simulationTime, callbacks); } -IoPartBufferBase::MetaData& IoPartBufferBase::FindMetaData(IoSignalId signalId) { +IoPartBufferBase::MetaData& IoPartBufferBase::FindMetaData(DsVeosCoSim_IoSignalId signalId) { const auto search = _metaDataLookup.find(signalId); if (search != _metaDataLookup.end()) { return search->second; @@ -156,7 +158,7 @@ void RemoteIoPartBuffer::ClearDataInternal() { } } -void RemoteIoPartBuffer::WriteInternal(IoSignalId signalId, uint32_t length, const void* value) { +void RemoteIoPartBuffer::WriteInternal(DsVeosCoSim_IoSignalId signalId, uint32_t length, const void* value) { MetaData& metaData = FindMetaData(signalId); Data& data = _dataVector[metaData.signalIndex]; @@ -197,7 +199,7 @@ void RemoteIoPartBuffer::WriteInternal(IoSignalId signalId, uint32_t length, con } } -void RemoteIoPartBuffer::ReadInternal(IoSignalId signalId, uint32_t& length, void* value) { +void RemoteIoPartBuffer::ReadInternal(DsVeosCoSim_IoSignalId signalId, uint32_t& length, void* value) { MetaData& metaData = FindMetaData(signalId); Data& data = _dataVector[metaData.signalIndex]; @@ -206,7 +208,7 @@ void RemoteIoPartBuffer::ReadInternal(IoSignalId signalId, uint32_t& length, voi (void)::memcpy(value, data.buffer.data(), totalSize); } -void RemoteIoPartBuffer::ReadInternal(IoSignalId signalId, uint32_t& length, const void** value) { +void RemoteIoPartBuffer::ReadInternal(DsVeosCoSim_IoSignalId signalId, uint32_t& length, const void** value) { MetaData& metaData = FindMetaData(signalId); Data& data = _dataVector[metaData.signalIndex]; @@ -241,13 +243,13 @@ bool RemoteIoPartBuffer::SerializeInternal(ChannelWriter& writer) { } bool RemoteIoPartBuffer::DeserializeInternal(ChannelReader& reader, - SimulationTime simulationTime, + DsVeosCoSim_SimulationTime simulationTime, const Callbacks& callbacks) { uint32_t ioSignalChangedCount = 0; CheckResultWithMessage(reader.Read(ioSignalChangedCount), "Could not read count of changed signals."); for (uint32_t i = 0; i < ioSignalChangedCount; i++) { - IoSignalId signalId{}; + DsVeosCoSim_IoSignalId signalId{}; CheckResultWithMessage(reader.Read(signalId), "Could not read signal id."); MetaData& metaData = FindMetaData(signalId); @@ -344,7 +346,7 @@ void LocalIoPartBuffer::ClearDataInternal() { } } -void LocalIoPartBuffer::WriteInternal(IoSignalId signalId, uint32_t length, const void* value) { +void LocalIoPartBuffer::WriteInternal(DsVeosCoSim_IoSignalId signalId, uint32_t length, const void* value) { MetaData& metaData = FindMetaData(signalId); Data& data = _dataVector[metaData.signalIndex]; @@ -388,7 +390,7 @@ void LocalIoPartBuffer::WriteInternal(IoSignalId signalId, uint32_t length, cons } } -void LocalIoPartBuffer::ReadInternal(IoSignalId signalId, uint32_t& length, void* value) { +void LocalIoPartBuffer::ReadInternal(DsVeosCoSim_IoSignalId signalId, uint32_t& length, void* value) { MetaData& metaData = FindMetaData(signalId); Data& data = _dataVector[metaData.signalIndex]; @@ -399,7 +401,7 @@ void LocalIoPartBuffer::ReadInternal(IoSignalId signalId, uint32_t& length, void (void)::memcpy(value, dataBuffer->data, totalSize); } -void LocalIoPartBuffer::ReadInternal(IoSignalId signalId, uint32_t& length, const void** value) { +void LocalIoPartBuffer::ReadInternal(DsVeosCoSim_IoSignalId signalId, uint32_t& length, const void** value) { MetaData& metaData = FindMetaData(signalId); Data& data = _dataVector[metaData.signalIndex]; @@ -409,7 +411,7 @@ void LocalIoPartBuffer::ReadInternal(IoSignalId signalId, uint32_t& length, cons *value = dataBuffer->data; } -bool LocalIoPartBuffer::SerializeInternal([[maybe_unused]] ChannelWriter& writer) { +bool LocalIoPartBuffer::SerializeInternal(ChannelWriter& writer) { auto size = static_cast(_changedSignalsQueue.Size()); CheckResultWithMessage(writer.Write(size), "Could not write count of changed signals."); if (_changedSignalsQueue.IsEmpty()) { @@ -428,14 +430,14 @@ bool LocalIoPartBuffer::SerializeInternal([[maybe_unused]] ChannelWriter& writer return true; } -bool LocalIoPartBuffer::DeserializeInternal([[maybe_unused]] ChannelReader& reader, - SimulationTime simulationTime, +bool LocalIoPartBuffer::DeserializeInternal(ChannelReader& reader, + DsVeosCoSim_SimulationTime simulationTime, const Callbacks& callbacks) { uint32_t ioSignalChangedCount = 0; CheckResultWithMessage(reader.Read(ioSignalChangedCount), "Could not read count of changed signals."); for (uint32_t i = 0; i < ioSignalChangedCount; i++) { - IoSignalId signalId{}; + DsVeosCoSim_IoSignalId signalId{}; CheckResultWithMessage(reader.Read(signalId), "Could not read signal id."); MetaData& metaData = FindMetaData(signalId); @@ -456,7 +458,7 @@ bool LocalIoPartBuffer::DeserializeInternal([[maybe_unused]] ChannelReader& read return true; } -LocalIoPartBuffer::DataBuffer* LocalIoPartBuffer::GetDataBuffer(size_t offset) { +LocalIoPartBuffer::DataBuffer* LocalIoPartBuffer::GetDataBuffer(size_t offset) const { return reinterpret_cast((static_cast(_sharedMemory.data()) + offset)); } @@ -501,15 +503,15 @@ void IoBuffer::ClearData() const { _writeBuffer->ClearData(); } -void IoBuffer::Write(IoSignalId signalId, uint32_t length, const void* value) const { +void IoBuffer::Write(DsVeosCoSim_IoSignalId signalId, uint32_t length, const void* value) const { _writeBuffer->Write(signalId, length, value); } -void IoBuffer::Read(IoSignalId signalId, uint32_t& length, void* value) const { +void IoBuffer::Read(DsVeosCoSim_IoSignalId signalId, uint32_t& length, void* value) const { _readBuffer->Read(signalId, length, value); } -void IoBuffer::Read(IoSignalId signalId, uint32_t& length, const void** value) const { +void IoBuffer::Read(DsVeosCoSim_IoSignalId signalId, uint32_t& length, const void** value) const { _readBuffer->Read(signalId, length, value); } @@ -517,7 +519,9 @@ bool IoBuffer::Serialize(ChannelWriter& writer) const { return _writeBuffer->Serialize(writer); } -bool IoBuffer::Deserialize(ChannelReader& reader, SimulationTime simulationTime, const Callbacks& callbacks) const { +bool IoBuffer::Deserialize(ChannelReader& reader, + DsVeosCoSim_SimulationTime simulationTime, + const Callbacks& callbacks) const { return _readBuffer->Deserialize(reader, simulationTime, callbacks); } diff --git a/src/IoBuffer.h b/src/IoBuffer.h index 4687684..fbc7cce 100644 --- a/src/IoBuffer.h +++ b/src/IoBuffer.h @@ -38,29 +38,31 @@ class IoPartBufferBase { void ClearData(); - void Write(IoSignalId signalId, uint32_t length, const void* value); - void Read(IoSignalId signalId, uint32_t& length, void* value); - void Read(IoSignalId signalId, uint32_t& length, const void** value); + void Write(DsVeosCoSim_IoSignalId signalId, uint32_t length, const void* value); + void Read(DsVeosCoSim_IoSignalId signalId, uint32_t& length, void* value); + void Read(DsVeosCoSim_IoSignalId signalId, uint32_t& length, const void** value); [[nodiscard]] bool Serialize(ChannelWriter& writer); - [[nodiscard]] bool Deserialize(ChannelReader& reader, SimulationTime simulationTime, const Callbacks& callbacks); + [[nodiscard]] bool Deserialize(ChannelReader& reader, + DsVeosCoSim_SimulationTime simulationTime, + const Callbacks& callbacks); protected: virtual void ClearDataInternal() = 0; - virtual void WriteInternal(IoSignalId signalId, uint32_t length, const void* value) = 0; - virtual void ReadInternal(IoSignalId signalId, uint32_t& length, void* value) = 0; - virtual void ReadInternal(IoSignalId signalId, uint32_t& length, const void** value) = 0; + virtual void WriteInternal(DsVeosCoSim_IoSignalId signalId, uint32_t length, const void* value) = 0; + virtual void ReadInternal(DsVeosCoSim_IoSignalId signalId, uint32_t& length, void* value) = 0; + virtual void ReadInternal(DsVeosCoSim_IoSignalId signalId, uint32_t& length, const void** value) = 0; [[nodiscard]] virtual bool SerializeInternal(ChannelWriter& writer) = 0; [[nodiscard]] virtual bool DeserializeInternal(ChannelReader& reader, - SimulationTime simulationTime, + DsVeosCoSim_SimulationTime simulationTime, const Callbacks& callbacks) = 0; - [[nodiscard]] MetaData& FindMetaData(IoSignalId signalId); + [[nodiscard]] MetaData& FindMetaData(DsVeosCoSim_IoSignalId signalId); CoSimType _coSimType{}; - std::unordered_map _metaDataLookup; + std::unordered_map _metaDataLookup; RingBuffer _changedSignalsQueue; private: @@ -87,13 +89,13 @@ class RemoteIoPartBuffer final : public IoPartBufferBase { protected: void ClearDataInternal() override; - void WriteInternal(IoSignalId signalId, uint32_t length, const void* value) override; - void ReadInternal(IoSignalId signalId, uint32_t& length, void* value) override; - void ReadInternal(IoSignalId signalId, uint32_t& length, const void** value) override; + void WriteInternal(DsVeosCoSim_IoSignalId signalId, uint32_t length, const void* value) override; + void ReadInternal(DsVeosCoSim_IoSignalId signalId, uint32_t& length, void* value) override; + void ReadInternal(DsVeosCoSim_IoSignalId signalId, uint32_t& length, const void** value) override; [[nodiscard]] bool SerializeInternal(ChannelWriter& writer) override; [[nodiscard]] bool DeserializeInternal(ChannelReader& reader, - SimulationTime simulationTime, + DsVeosCoSim_SimulationTime simulationTime, const Callbacks& callbacks) override; private: @@ -127,17 +129,17 @@ class LocalIoPartBuffer final : public IoPartBufferBase { protected: void ClearDataInternal() override; - void WriteInternal(IoSignalId signalId, uint32_t length, const void* value) override; - void ReadInternal(IoSignalId signalId, uint32_t& length, void* value) override; - void ReadInternal(IoSignalId signalId, uint32_t& length, const void** value) override; + void WriteInternal(DsVeosCoSim_IoSignalId signalId, uint32_t length, const void* value) override; + void ReadInternal(DsVeosCoSim_IoSignalId signalId, uint32_t& length, void* value) override; + void ReadInternal(DsVeosCoSim_IoSignalId signalId, uint32_t& length, const void** value) override; [[nodiscard]] bool SerializeInternal(ChannelWriter& writer) override; [[nodiscard]] bool DeserializeInternal(ChannelReader& reader, - SimulationTime simulationTime, + DsVeosCoSim_SimulationTime simulationTime, const Callbacks& callbacks) override; private: - [[nodiscard]] DataBuffer* GetDataBuffer(size_t offset); + [[nodiscard]] DataBuffer* GetDataBuffer(size_t offset) const; static void FlipBuffers(Data& data); @@ -164,13 +166,13 @@ class IoBuffer { void ClearData() const; - void Write(IoSignalId signalId, uint32_t length, const void* value) const; - void Read(IoSignalId signalId, uint32_t& length, void* value) const; - void Read(IoSignalId signalId, uint32_t& length, const void** value) const; + void Write(DsVeosCoSim_IoSignalId signalId, uint32_t length, const void* value) const; + void Read(DsVeosCoSim_IoSignalId signalId, uint32_t& length, void* value) const; + void Read(DsVeosCoSim_IoSignalId signalId, uint32_t& length, const void** value) const; [[nodiscard]] bool Serialize(ChannelWriter& writer) const; [[nodiscard]] bool Deserialize(ChannelReader& reader, - SimulationTime simulationTime, + DsVeosCoSim_SimulationTime simulationTime, const Callbacks& callbacks) const; private: diff --git a/src/OsAbstraction/Handle.h b/src/OsAbstraction/Handle.h index 2cb4334..5b7e8df 100644 --- a/src/OsAbstraction/Handle.h +++ b/src/OsAbstraction/Handle.h @@ -11,7 +11,7 @@ namespace DsVeosCoSim { class Handle final { public: Handle() = default; - Handle(void* handle); // NOLINT(google-explicit-constructor, hicpp-explicit-conversions) + Handle(void* handle); // NOLINT ~Handle() noexcept; Handle(const Handle&) = delete; @@ -20,10 +20,10 @@ class Handle final { Handle(Handle&&) noexcept; Handle& operator=(Handle&&) noexcept; - operator void*() const noexcept; // NOLINT(google-explicit-constructor, hicpp-explicit-conversions) + operator void*() const noexcept; // NOLINT void Wait() const; - bool Wait(uint32_t milliseconds) const; + [[nodiscard]] bool Wait(uint32_t milliseconds) const; private: void* _handle{}; diff --git a/src/OsAbstraction/NamedEvent.h b/src/OsAbstraction/NamedEvent.h index 22eff22..2bf81f3 100644 --- a/src/OsAbstraction/NamedEvent.h +++ b/src/OsAbstraction/NamedEvent.h @@ -28,7 +28,7 @@ class NamedEvent final { [[nodiscard]] static NamedEvent OpenExisting(const std::string& name); [[nodiscard]] static std::optional TryOpenExisting(const std::string& name); - operator Handle&() noexcept; // NOLINT(google-explicit-constructor, hicpp-explicit-conversions) + operator Handle&() noexcept; // NOLINT void Set(); void Wait() const; diff --git a/src/OsAbstraction/Socket.cpp b/src/OsAbstraction/Socket.cpp index 3f32042..8d0fbd0 100644 --- a/src/OsAbstraction/Socket.cpp +++ b/src/OsAbstraction/Socket.cpp @@ -204,11 +204,11 @@ bool ConnectWithTimeout(socket_t& socket, throw OsAbstractionException("Could not connect.", errorCode); } - fd_set set; + fd_set set{}; FD_ZERO(&set); FD_SET(socket, &set); - timeval timeout; + timeval timeout{}; timeout.tv_sec = static_cast(timeoutInMilliseconds / 1000); timeout.tv_usec = static_cast(timeoutInMilliseconds % 1000) * 1000; @@ -384,7 +384,7 @@ bool Socket::IsValid() const { return _socket != InvalidSocket; } -void Socket::EnableIpv6Only() const { // NOLINT(readability-convert-member-functions-to-static) +void Socket::EnableIpv6Only() const { // NOLINT // On windows, IPv6 only is enabled by default #ifndef _WIN32 int32_t flags = 1; @@ -447,7 +447,7 @@ std::optional Socket::TryConnect(std::string_view ipAddress, return {}; } -bool Socket::TryConnect(const std::string& name) { +bool Socket::TryConnect(const std::string& name) const { EnsureIsValid(); if (_addressFamily != AddressFamily::Uds) { @@ -491,7 +491,7 @@ void Socket::BindForIpv4(uint16_t port, bool enableRemoteAccess) const { address.sin_port = ::htons(port); address.sin_addr.s_addr = enableRemoteAccess ? INADDR_ANY : ::htonl(INADDR_LOOPBACK); - if (::bind(_socket, reinterpret_cast(&address), static_cast(sizeof(address))) < 0) { + if (::bind(_socket, reinterpret_cast(&address), sizeof(address)) < 0) { throw OsAbstractionException("Could not bind socket.", GetLastNetworkError()); } } @@ -502,7 +502,7 @@ void Socket::BindForIpv6(uint16_t port, bool enableRemoteAccess) const { address.sin6_port = ::htons(port); address.sin6_addr = enableRemoteAccess ? in6addr_any : in6addr_loopback; - if (::bind(_socket, reinterpret_cast(&address), static_cast(sizeof(address))) < 0) { + if (::bind(_socket, reinterpret_cast(&address), sizeof(address)) < 0) { throw OsAbstractionException("Could not bind socket.", GetLastNetworkError()); } } @@ -544,11 +544,7 @@ void Socket::EnableReuseAddress() const { } int32_t flags = 1; - if (::setsockopt(_socket, - SOL_SOCKET, - SO_REUSEADDR, - reinterpret_cast(&flags), - static_cast(sizeof(flags))) < 0) { + if (::setsockopt(_socket, SOL_SOCKET, SO_REUSEADDR, reinterpret_cast(&flags), sizeof(flags)) < 0) { throw OsAbstractionException("Could not enable socket option reuse address.", GetLastNetworkError()); } } @@ -557,11 +553,7 @@ void Socket::EnableNoDelay() const { EnsureIsValid(); int32_t flags = 1; - if (::setsockopt(_socket, - IPPROTO_TCP, - TCP_NODELAY, - reinterpret_cast(&flags), - static_cast(sizeof(flags))) < 0) { + if (::setsockopt(_socket, IPPROTO_TCP, TCP_NODELAY, reinterpret_cast(&flags), sizeof(flags)) < 0) { throw OsAbstractionException("Could not enable TCP option no delay.", GetLastNetworkError()); } } diff --git a/src/OsAbstraction/Socket.h b/src/OsAbstraction/Socket.h index c73b91f..9c32768 100644 --- a/src/OsAbstraction/Socket.h +++ b/src/OsAbstraction/Socket.h @@ -59,7 +59,7 @@ class Socket { uint16_t localPort, uint32_t timeoutInMilliseconds); - [[nodiscard]] bool TryConnect(const std::string& name); + [[nodiscard]] bool TryConnect(const std::string& name) const; void EnableIpv6Only() const; void Bind(uint16_t port, bool enableRemoteAccess) const; void Bind(const std::string& name); diff --git a/src/Protocol.cpp b/src/Protocol.cpp index 8a316a1..ffbeb81 100644 --- a/src/Protocol.cpp +++ b/src/Protocol.cpp @@ -278,7 +278,7 @@ bool ReadConnect(ChannelReader& reader, bool SendConnectOk(ChannelWriter& writer, uint32_t protocolVersion, Mode clientMode, - SimulationTime stepSize, + DsVeosCoSim_SimulationTime stepSize, SimulationState simulationState, const std::vector& incomingSignals, const std::vector& outgoingSignals, @@ -302,7 +302,7 @@ bool SendConnectOk(ChannelWriter& writer, bool ReadConnectOk(ChannelReader& reader, uint32_t& protocolVersion, Mode& clientMode, - SimulationTime& stepSize, + DsVeosCoSim_SimulationTime& stepSize, SimulationState& simulationState, std::vector& incomingSignals, std::vector& outgoingSignals, @@ -321,31 +321,33 @@ bool ReadConnectOk(ChannelReader& reader, return true; } -bool SendStart(ChannelWriter& writer, SimulationTime simulationTime) { +bool SendStart(ChannelWriter& writer, DsVeosCoSim_SimulationTime simulationTime) { CheckResult(WriteHeader(writer, FrameKind::Start)); CheckResultWithMessage(writer.Write(simulationTime), "Could not write simulation time."); CheckResultWithMessage(writer.EndWrite(), "Could not finish frame."); return true; } -bool ReadStart(ChannelReader& reader, SimulationTime& simulationTime) { +bool ReadStart(ChannelReader& reader, DsVeosCoSim_SimulationTime& simulationTime) { CheckResultWithMessage(reader.Read(simulationTime), "Could not read simulation time."); return true; } -bool SendStop(ChannelWriter& writer, SimulationTime simulationTime) { +bool SendStop(ChannelWriter& writer, DsVeosCoSim_SimulationTime simulationTime) { CheckResult(WriteHeader(writer, FrameKind::Stop)); CheckResultWithMessage(writer.Write(simulationTime), "Could not write simulation time."); CheckResultWithMessage(writer.EndWrite(), "Could not finish frame."); return true; } -bool ReadStop(ChannelReader& reader, SimulationTime& simulationTime) { +bool ReadStop(ChannelReader& reader, DsVeosCoSim_SimulationTime& simulationTime) { CheckResultWithMessage(reader.Read(simulationTime), "Could not read simulation time."); return true; } -bool SendTerminate(ChannelWriter& writer, SimulationTime simulationTime, TerminateReason reason) { +bool SendTerminate(ChannelWriter& writer, + DsVeosCoSim_SimulationTime simulationTime, + DsVeosCoSim_TerminateReason reason) { CheckResult(WriteHeader(writer, FrameKind::Terminate)); CheckResultWithMessage(writer.Write(simulationTime), "Could not write simulation time."); CheckResultWithMessage(writer.Write(reason), "Could not write reason."); @@ -353,37 +355,42 @@ bool SendTerminate(ChannelWriter& writer, SimulationTime simulationTime, Termina return true; } -bool ReadTerminate(ChannelReader& reader, SimulationTime& simulationTime, TerminateReason& reason) { +bool ReadTerminate(ChannelReader& reader, + DsVeosCoSim_SimulationTime& simulationTime, + DsVeosCoSim_TerminateReason& reason) { CheckResultWithMessage(reader.Read(simulationTime), "Could not read simulation time."); CheckResultWithMessage(reader.Read(reason), "Could not read reason."); return true; } -bool SendPause(ChannelWriter& writer, SimulationTime simulationTime) { +bool SendPause(ChannelWriter& writer, DsVeosCoSim_SimulationTime simulationTime) { CheckResult(WriteHeader(writer, FrameKind::Pause)); CheckResultWithMessage(writer.Write(simulationTime), "Could not write simulation time."); CheckResultWithMessage(writer.EndWrite(), "Could not finish frame."); return true; } -bool ReadPause(ChannelReader& reader, SimulationTime& simulationTime) { +bool ReadPause(ChannelReader& reader, DsVeosCoSim_SimulationTime& simulationTime) { CheckResultWithMessage(reader.Read(simulationTime), "Could not read simulation time."); return true; } -bool SendContinue(ChannelWriter& writer, SimulationTime simulationTime) { +bool SendContinue(ChannelWriter& writer, DsVeosCoSim_SimulationTime simulationTime) { CheckResult(WriteHeader(writer, FrameKind::Continue)); CheckResultWithMessage(writer.Write(simulationTime), "Could not write simulation time."); CheckResultWithMessage(writer.EndWrite(), "Could not finish frame."); return true; } -bool ReadContinue(ChannelReader& reader, SimulationTime& simulationTime) { +bool ReadContinue(ChannelReader& reader, DsVeosCoSim_SimulationTime& simulationTime) { CheckResultWithMessage(reader.Read(simulationTime), "Could not read simulation time."); return true; } -bool SendStep(ChannelWriter& writer, SimulationTime simulationTime, IoBuffer& ioBuffer, BusBuffer& busBuffer) { +bool SendStep(ChannelWriter& writer, + DsVeosCoSim_SimulationTime simulationTime, + const IoBuffer& ioBuffer, + const BusBuffer& busBuffer) { CheckResult(WriteHeader(writer, FrameKind::Step)); CheckResultWithMessage(writer.Write(simulationTime), "Could not write simulation time."); CheckResultWithMessage(ioBuffer.Serialize(writer), "Could not write IO buffer data."); @@ -393,9 +400,9 @@ bool SendStep(ChannelWriter& writer, SimulationTime simulationTime, IoBuffer& io } bool ReadStep(ChannelReader& reader, - SimulationTime& simulationTime, - IoBuffer& ioBuffer, - BusBuffer& busBuffer, + DsVeosCoSim_SimulationTime& simulationTime, + const IoBuffer& ioBuffer, + const BusBuffer& busBuffer, const Callbacks& callbacks) { CheckResultWithMessage(reader.Read(simulationTime), "Could not read simulation time."); @@ -409,12 +416,12 @@ bool ReadStep(ChannelReader& reader, } bool SendStepOk(ChannelWriter& writer, - SimulationTime simulationTime, + DsVeosCoSim_SimulationTime nextSimulationTime, Command command, - IoBuffer& ioBuffer, - BusBuffer& busBuffer) { + const IoBuffer& ioBuffer, + const BusBuffer& busBuffer) { CheckResult(WriteHeader(writer, FrameKind::StepOk)); - CheckResultWithMessage(writer.Write(simulationTime), "Could not write simulation time."); + CheckResultWithMessage(writer.Write(nextSimulationTime), "Could not write simulation time."); CheckResultWithMessage(writer.Write(command), "Could not write command."); CheckResultWithMessage(ioBuffer.Serialize(writer), "Could not write IO buffer data."); CheckResultWithMessage(busBuffer.Serialize(writer), "Could not write bus buffer data."); @@ -423,20 +430,22 @@ bool SendStepOk(ChannelWriter& writer, } bool ReadStepOk(ChannelReader& reader, - SimulationTime& simulationTime, + DsVeosCoSim_SimulationTime& nextSimulationTime, Command& command, - IoBuffer& ioBuffer, - BusBuffer& busBuffer, + const IoBuffer& ioBuffer, + const BusBuffer& busBuffer, const Callbacks& callbacks) { - CheckResultWithMessage(reader.Read(simulationTime), "Could not read simulation time."); + CheckResultWithMessage(reader.Read(nextSimulationTime), "Could not read simulation time."); CheckResultWithMessage(reader.Read(command), "Could not read command."); if (callbacks.simulationBeginStepCallback) { - callbacks.simulationBeginStepCallback(simulationTime); + callbacks.simulationBeginStepCallback(nextSimulationTime); } - CheckResultWithMessage(ioBuffer.Deserialize(reader, simulationTime, callbacks), "Could not read IO buffer data."); - CheckResultWithMessage(busBuffer.Deserialize(reader, simulationTime, callbacks), "Could not read bus buffer data."); + CheckResultWithMessage(ioBuffer.Deserialize(reader, nextSimulationTime, callbacks), + "Could not read IO buffer data."); + CheckResultWithMessage(busBuffer.Deserialize(reader, nextSimulationTime, callbacks), + "Could not read bus buffer data."); return true; } diff --git a/src/Protocol.h b/src/Protocol.h index f5a50fe..34eecee 100644 --- a/src/Protocol.h +++ b/src/Protocol.h @@ -105,7 +105,7 @@ namespace Protocol { [[nodiscard]] bool SendConnectOk(ChannelWriter& writer, uint32_t protocolVersion, Mode clientMode, - SimulationTime stepSize, + DsVeosCoSim_SimulationTime stepSize, SimulationState simulationState, const std::vector& incomingSignals, const std::vector& outgoingSignals, @@ -115,7 +115,7 @@ namespace Protocol { [[nodiscard]] bool ReadConnectOk(ChannelReader& reader, uint32_t& protocolVersion, Mode& clientMode, - SimulationTime& stepSize, + DsVeosCoSim_SimulationTime& stepSize, SimulationState& simulationState, std::vector& incomingSignals, std::vector& outgoingSignals, @@ -123,41 +123,45 @@ namespace Protocol { std::vector& ethControllers, std::vector& linControllers); -[[nodiscard]] bool SendStart(ChannelWriter& writer, SimulationTime simulationTime); -[[nodiscard]] bool ReadStart(ChannelReader& reader, SimulationTime& simulationTime); +[[nodiscard]] bool SendStart(ChannelWriter& writer, DsVeosCoSim_SimulationTime simulationTime); +[[nodiscard]] bool ReadStart(ChannelReader& reader, DsVeosCoSim_SimulationTime& simulationTime); -[[nodiscard]] bool SendStop(ChannelWriter& writer, SimulationTime simulationTime); -[[nodiscard]] bool ReadStop(ChannelReader& reader, SimulationTime& simulationTime); +[[nodiscard]] bool SendStop(ChannelWriter& writer, DsVeosCoSim_SimulationTime simulationTime); +[[nodiscard]] bool ReadStop(ChannelReader& reader, DsVeosCoSim_SimulationTime& simulationTime); -[[nodiscard]] bool SendTerminate(ChannelWriter& writer, SimulationTime simulationTime, TerminateReason reason); -[[nodiscard]] bool ReadTerminate(ChannelReader& reader, SimulationTime& simulationTime, TerminateReason& reason); +[[nodiscard]] bool SendTerminate(ChannelWriter& writer, + DsVeosCoSim_SimulationTime simulationTime, + DsVeosCoSim_TerminateReason reason); +[[nodiscard]] bool ReadTerminate(ChannelReader& reader, + DsVeosCoSim_SimulationTime& simulationTime, + DsVeosCoSim_TerminateReason& reason); -[[nodiscard]] bool SendPause(ChannelWriter& writer, SimulationTime simulationTime); -[[nodiscard]] bool ReadPause(ChannelReader& reader, SimulationTime& simulationTime); +[[nodiscard]] bool SendPause(ChannelWriter& writer, DsVeosCoSim_SimulationTime simulationTime); +[[nodiscard]] bool ReadPause(ChannelReader& reader, DsVeosCoSim_SimulationTime& simulationTime); -[[nodiscard]] bool SendContinue(ChannelWriter& writer, SimulationTime simulationTime); -[[nodiscard]] bool ReadContinue(ChannelReader& reader, SimulationTime& simulationTime); +[[nodiscard]] bool SendContinue(ChannelWriter& writer, DsVeosCoSim_SimulationTime simulationTime); +[[nodiscard]] bool ReadContinue(ChannelReader& reader, DsVeosCoSim_SimulationTime& simulationTime); [[nodiscard]] bool SendStep(ChannelWriter& writer, - SimulationTime simulationTime, - IoBuffer& ioBuffer, - BusBuffer& busBuffer); + DsVeosCoSim_SimulationTime simulationTime, + const IoBuffer& ioBuffer, + const BusBuffer& busBuffer); [[nodiscard]] bool ReadStep(ChannelReader& reader, - SimulationTime& simulationTime, - IoBuffer& ioBuffer, - BusBuffer& busBuffer, + DsVeosCoSim_SimulationTime& simulationTime, + const IoBuffer& ioBuffer, + const BusBuffer& busBuffer, const Callbacks& callbacks); [[nodiscard]] bool SendStepOk(ChannelWriter& writer, - SimulationTime nextSimulationTime, + DsVeosCoSim_SimulationTime nextSimulationTime, Command command, - IoBuffer& ioBuffer, - BusBuffer& busBuffer); + const IoBuffer& ioBuffer, + const BusBuffer& busBuffer); [[nodiscard]] bool ReadStepOk(ChannelReader& reader, - SimulationTime& nextSimulationTime, + DsVeosCoSim_SimulationTime& nextSimulationTime, Command& command, - IoBuffer& ioBuffer, - BusBuffer& busBuffer, + const IoBuffer& ioBuffer, + const BusBuffer& busBuffer, const Callbacks& callbacks); [[nodiscard]] bool SendGetPort(ChannelWriter& writer, std::string_view serverName); diff --git a/test/TestBusBuffer.cpp b/test/TestBusBuffer.cpp index 4ebbdaa..11ea163 100644 --- a/test/TestBusBuffer.cpp +++ b/test/TestBusBuffer.cpp @@ -40,7 +40,7 @@ void Transfer(BusBuffer& senderBusBuffer, BusBuffer& receiverBusBuffer) { template void Transfer(BusBuffer& senderBusBuffer, BusBuffer& receiverBusBuffer, - SimulationTime simulationTime, + DsVeosCoSim_SimulationTime simulationTime, const std::vector>& expectedCallbacks) { TcpChannelServer server(0, true); uint16_t port = server.GetLocalPort(); @@ -54,7 +54,7 @@ void Transfer(BusBuffer& senderBusBuffer, size_t index{}; Callbacks callbacks{}; if constexpr (std::is_same_v) { - callbacks.canMessageReceivedCallback = [&](SimulationTime simTime, + callbacks.canMessageReceivedCallback = [&](DsVeosCoSim_SimulationTime simTime, const DsVeosCoSim_CanController& controller, const DsVeosCoSim_CanMessage& message) { ASSERT_EQ(simTime, simulationTime); @@ -70,7 +70,7 @@ void Transfer(BusBuffer& senderBusBuffer, } if constexpr (std::is_same_v) { - callbacks.ethMessageReceivedCallback = [&](SimulationTime simTime, + callbacks.ethMessageReceivedCallback = [&](DsVeosCoSim_SimulationTime simTime, const DsVeosCoSim_EthController& controller, const DsVeosCoSim_EthMessage& message) { ASSERT_EQ(simTime, simulationTime); @@ -86,7 +86,7 @@ void Transfer(BusBuffer& senderBusBuffer, } if constexpr (std::is_same_v) { - callbacks.linMessageReceivedCallback = [&](SimulationTime simTime, + callbacks.linMessageReceivedCallback = [&](DsVeosCoSim_SimulationTime simTime, const DsVeosCoSim_LinController& controller, const DsVeosCoSim_LinMessage& message) { ASSERT_EQ(simTime, simulationTime); @@ -203,7 +203,7 @@ using Parameters = Types - static std::string GetName([[maybe_unused]] int32_t index) { + static std::string GetName([[maybe_unused]] int32_t index) { // NOLINT using TController = typename T::Controller; CoSimType coSimType = T::GetCoSimType(); ConnectionKind connectionKind = T::GetConnectionKind(); @@ -452,7 +452,7 @@ TYPED_TEST(TestBusBuffer, ReceiveMessageOnEmptyBufferByEvent) { std::vector> expectedEvents; - SimulationTime simulationTime = GenerateI64(); + DsVeosCoSim_SimulationTime simulationTime = GenerateI64(); // Act and assert Transfer(senderBusBuffer, receiverBusBuffer, simulationTime, expectedEvents); @@ -519,7 +519,7 @@ TYPED_TEST(TestBusBuffer, ReceiveTransmittedMessageByEvent) { ASSERT_TRUE(senderBusBuffer.Transmit(sendMessage)); expectedEvents.push_back({controller, sendMessage}); - SimulationTime simulationTime = GenerateI64(); + DsVeosCoSim_SimulationTime simulationTime = GenerateI64(); // Act and assert Transfer(senderBusBuffer, receiverBusBuffer, simulationTime, expectedEvents); @@ -600,7 +600,7 @@ TYPED_TEST(TestBusBuffer, ReceiveTransmittedMessagesByEventWithTransfer) { ASSERT_TRUE(senderBusBuffer.Transmit(sendMessage)); } - SimulationTime simulationTime = GenerateI64(); + DsVeosCoSim_SimulationTime simulationTime = GenerateI64(); // Act and assert Transfer(senderBusBuffer, receiverBusBuffer, simulationTime, expectedEvents); @@ -670,7 +670,7 @@ TYPED_TEST(TestBusBuffer, DoNotReceiveNotFullyTransmittedMessageByEvent) { FillWithRandom(sendMessage, controller.id); ASSERT_TRUE(senderBusBuffer.Transmit(sendMessage)); - SimulationTime simulationTime = GenerateI64(); + DsVeosCoSim_SimulationTime simulationTime = GenerateI64(); // Act and assert Transfer(fakeSenderBusBuffer, receiverBusBuffer, simulationTime, expectedEvents); // Should not transfer anything diff --git a/test/TestCoSim.cpp b/test/TestCoSim.cpp index 25e75f9..f24a88d 100644 --- a/test/TestCoSim.cpp +++ b/test/TestCoSim.cpp @@ -69,7 +69,7 @@ TEST_F(TestCoSim, StartServerWithoutOptionalClient) { CoSimServer server; server.Load(config); - const SimulationTime simulationTime = GenerateI64(); + const DsVeosCoSim_SimulationTime simulationTime = GenerateI64(); // Act and assert ASSERT_NO_THROW(server.Start(simulationTime)); @@ -83,7 +83,7 @@ TEST_F(TestCoSim, StopServerWithoutOptionalClient) { server.Load(config); server.Start(GenerateI64()); - const SimulationTime simulationTime = GenerateI64(); + const DsVeosCoSim_SimulationTime simulationTime = GenerateI64(); // Act and assert ASSERT_NO_THROW(server.Stop(simulationTime)); @@ -97,7 +97,7 @@ TEST_F(TestCoSim, PauseServerWithoutOptionalClient) { server.Load(config); server.Start(GenerateI64()); - const SimulationTime simulationTime = GenerateI64(); + const DsVeosCoSim_SimulationTime simulationTime = GenerateI64(); // Act and assert ASSERT_NO_THROW(server.Pause(simulationTime)); @@ -112,7 +112,7 @@ TEST_F(TestCoSim, ContinueServerWithoutOptionalClient) { server.Start(GenerateI64()); server.Pause(GenerateI64()); - const SimulationTime simulationTime = GenerateI64(); + const DsVeosCoSim_SimulationTime simulationTime = GenerateI64(); // Act and assert ASSERT_NO_THROW(server.Continue(simulationTime)); @@ -126,8 +126,9 @@ TEST_F(TestCoSim, TerminateServerWithoutOptionalClient) { server.Load(config); server.Start(GenerateI64()); - const SimulationTime simulationTime = GenerateI64(); - const TerminateReason reason = GenerateRandom(TerminateReason::Finished, TerminateReason::Error); + const DsVeosCoSim_SimulationTime simulationTime = GenerateI64(); + const DsVeosCoSim_TerminateReason reason = + GenerateRandom(DsVeosCoSim_TerminateReason_Finished, DsVeosCoSim_TerminateReason_Error); // Act and assert ASSERT_NO_THROW(server.Terminate(simulationTime, reason)); @@ -141,8 +142,8 @@ TEST_F(TestCoSim, StepServerWithoutOptionalClient) { server.Load(config); server.Start(GenerateI64()); - const SimulationTime simulationTime = GenerateI64(); - SimulationTime nextSimulationTime{}; + const DsVeosCoSim_SimulationTime simulationTime = GenerateI64(); + DsVeosCoSim_SimulationTime nextSimulationTime{}; // Act and assert ASSERT_NO_THROW(server.Step(simulationTime, nextSimulationTime)); @@ -225,7 +226,7 @@ TEST_P(TestCoSim, DisconnectFromServerWithMandatoryClient) { Event stoppedEvent; CoSimServerConfig config = CreateServerConfig(); - config.simulationStoppedCallback = [&](SimulationTime) { + config.simulationStoppedCallback = [&](DsVeosCoSim_SimulationTime) { stoppedEvent.Set(); }; diff --git a/test/TestIoBuffer.cpp b/test/TestIoBuffer.cpp index 43656ac..703871c 100644 --- a/test/TestIoBuffer.cpp +++ b/test/TestIoBuffer.cpp @@ -58,18 +58,20 @@ void TransferWithEvents(IoBuffer& writerIoBuffer, IoBuffer& readerIoBuffer, std: SocketChannel senderChannel = ConnectToTcpChannel("127.0.0.1", port); SocketChannel receiverChannel = Accept(server); - SimulationTime simulationTime = GenerateI64(); + DsVeosCoSim_SimulationTime simulationTime = GenerateI64(); Callbacks callbacks{}; - callbacks.incomingSignalChangedCallback = - [&](SimulationTime simTime, const DsVeosCoSim_IoSignal& changedIoSignal, uint32_t length, const void* value) { - ASSERT_EQ(simTime, simulationTime); - ASSERT_FALSE(eventData.empty()); - ASSERT_EQ(eventData[0].signal.id, changedIoSignal.id); - ASSERT_EQ(eventData[0].signal.length, length); - AssertByteArray(eventData[0].data.data(), value, eventData[0].data.size()); - eventData.pop_back(); - }; + callbacks.incomingSignalChangedCallback = [&](DsVeosCoSim_SimulationTime simTime, + const DsVeosCoSim_IoSignal& changedIoSignal, + uint32_t length, + const void* value) { + ASSERT_EQ(simTime, simulationTime); + ASSERT_FALSE(eventData.empty()); + ASSERT_EQ(eventData[0].signal.id, changedIoSignal.id); + ASSERT_EQ(eventData[0].signal.length, length); + AssertByteArray(eventData[0].data.data(), value, eventData[0].data.size()); + eventData.pop_back(); + }; ASSERT_TRUE(writerIoBuffer.Serialize(senderChannel.GetWriter())); ASSERT_TRUE(senderChannel.GetWriter().EndWrite()); diff --git a/test/TestProtocol.cpp b/test/TestProtocol.cpp index e779a07..ac60e29 100644 --- a/test/TestProtocol.cpp +++ b/test/TestProtocol.cpp @@ -156,7 +156,7 @@ TEST_P(TestProtocol, SendAndReceiveConnectOk) { const uint32_t sendProtocolVersion = GenerateU32(); constexpr Mode sendMode{}; - const SimulationTime sendStepSize = GenerateI64(); + const DsVeosCoSim_SimulationTime sendStepSize = GenerateI64(); constexpr SimulationState sendSimulationState{}; const std::vector sendIncomingSignals = CreateSignals(2); const std::vector sendOutgoingSignals = CreateSignals(3); @@ -181,7 +181,7 @@ TEST_P(TestProtocol, SendAndReceiveConnectOk) { uint32_t receiveProtocolVersion{}; Mode receiveMode{}; - SimulationTime receiveStepSize{}; + DsVeosCoSim_SimulationTime receiveStepSize{}; SimulationState receiveSimulationState{}; std::vector receiveIncomingSignals; std::vector receiveOutgoingSignals; @@ -212,7 +212,7 @@ TEST_P(TestProtocol, SendAndReceiveStart) { // Arrange CustomSetUp(GetParam()); - const SimulationTime sendSimulationTime = GenerateI64(); + const DsVeosCoSim_SimulationTime sendSimulationTime = GenerateI64(); // Act ASSERT_TRUE(Protocol::SendStart(_senderChannel->GetWriter(), sendSimulationTime)); @@ -220,7 +220,7 @@ TEST_P(TestProtocol, SendAndReceiveStart) { // Assert AssertFrame(FrameKind::Start); - SimulationTime receiveSimulationTime{}; + DsVeosCoSim_SimulationTime receiveSimulationTime{}; ASSERT_TRUE(Protocol::ReadStart(_receiverChannel->GetReader(), receiveSimulationTime)); ASSERT_EQ(sendSimulationTime, receiveSimulationTime); } @@ -229,7 +229,7 @@ TEST_P(TestProtocol, SendAndReceiveStop) { // Arrange CustomSetUp(GetParam()); - const SimulationTime sendSimulationTime = GenerateI64(); + const DsVeosCoSim_SimulationTime sendSimulationTime = GenerateI64(); // Act ASSERT_TRUE(Protocol::SendStop(_senderChannel->GetWriter(), sendSimulationTime)); @@ -237,7 +237,7 @@ TEST_P(TestProtocol, SendAndReceiveStop) { // Assert AssertFrame(FrameKind::Stop); - SimulationTime receiveSimulationTime{}; + DsVeosCoSim_SimulationTime receiveSimulationTime{}; ASSERT_TRUE(Protocol::ReadStop(_receiverChannel->GetReader(), receiveSimulationTime)); ASSERT_EQ(sendSimulationTime, receiveSimulationTime); } @@ -246,8 +246,9 @@ TEST_P(TestProtocol, SendAndReceiveTerminate) { // Arrange CustomSetUp(GetParam()); - const SimulationTime sendSimulationTime = GenerateI64(); - const TerminateReason sendTerminateReason = GenerateRandom(TerminateReason::Finished, TerminateReason::Error); + const DsVeosCoSim_SimulationTime sendSimulationTime = GenerateI64(); + const DsVeosCoSim_TerminateReason sendTerminateReason = + GenerateRandom(DsVeosCoSim_TerminateReason_Finished, DsVeosCoSim_TerminateReason_Error); // Act ASSERT_TRUE(Protocol::SendTerminate(_senderChannel->GetWriter(), sendSimulationTime, sendTerminateReason)); @@ -255,8 +256,8 @@ TEST_P(TestProtocol, SendAndReceiveTerminate) { // Assert AssertFrame(FrameKind::Terminate); - SimulationTime receiveSimulationTime{}; - TerminateReason receiveTerminateReason{}; + DsVeosCoSim_SimulationTime receiveSimulationTime{}; + DsVeosCoSim_TerminateReason receiveTerminateReason{}; ASSERT_TRUE(Protocol::ReadTerminate(_receiverChannel->GetReader(), receiveSimulationTime, receiveTerminateReason)); ASSERT_EQ(sendSimulationTime, receiveSimulationTime); ASSERT_EQ(sendTerminateReason, receiveTerminateReason); @@ -266,7 +267,7 @@ TEST_P(TestProtocol, SendAndReceivePause) { // Arrange CustomSetUp(GetParam()); - const SimulationTime sendSimulationTime = GenerateI64(); + const DsVeosCoSim_SimulationTime sendSimulationTime = GenerateI64(); // Act ASSERT_TRUE(Protocol::SendPause(_senderChannel->GetWriter(), sendSimulationTime)); @@ -274,7 +275,7 @@ TEST_P(TestProtocol, SendAndReceivePause) { // Assert AssertFrame(FrameKind::Pause); - SimulationTime receiveSimulationTime{}; + DsVeosCoSim_SimulationTime receiveSimulationTime{}; ASSERT_TRUE(Protocol::ReadPause(_receiverChannel->GetReader(), receiveSimulationTime)); ASSERT_EQ(sendSimulationTime, receiveSimulationTime); } @@ -283,7 +284,7 @@ TEST_P(TestProtocol, SendAndReceiveContinue) { // Arrange CustomSetUp(GetParam()); - const SimulationTime sendSimulationTime = GenerateI64(); + const DsVeosCoSim_SimulationTime sendSimulationTime = GenerateI64(); // Act ASSERT_TRUE(Protocol::SendContinue(_senderChannel->GetWriter(), sendSimulationTime)); @@ -291,7 +292,7 @@ TEST_P(TestProtocol, SendAndReceiveContinue) { // Assert AssertFrame(FrameKind::Continue); - SimulationTime receiveSimulationTime{}; + DsVeosCoSim_SimulationTime receiveSimulationTime{}; ASSERT_TRUE(Protocol::ReadContinue(_receiverChannel->GetReader(), receiveSimulationTime)); ASSERT_EQ(sendSimulationTime, receiveSimulationTime); } @@ -301,7 +302,7 @@ TEST_P(TestProtocol, SendAndReceiveStep) { ConnectionKind connectionKind = GetParam(); CustomSetUp(connectionKind); - const SimulationTime sendSimulationTime = GenerateI64(); + const DsVeosCoSim_SimulationTime sendSimulationTime = GenerateI64(); const std::string ioBufferName = GenerateString("IoBuffer名前"); IoBuffer clientIoBuffer(CoSimType::Client, connectionKind, ioBufferName, {}, {}); @@ -317,7 +318,7 @@ TEST_P(TestProtocol, SendAndReceiveStep) { // Assert AssertFrame(FrameKind::Step); - SimulationTime receiveSimulationTime{}; + DsVeosCoSim_SimulationTime receiveSimulationTime{}; ASSERT_TRUE( Protocol::ReadStep(_receiverChannel->GetReader(), receiveSimulationTime, serverIoBuffer, serverBusBuffer, {})); ASSERT_EQ(sendSimulationTime, receiveSimulationTime); @@ -328,7 +329,7 @@ TEST_P(TestProtocol, SendAndReceiveStepOk) { ConnectionKind connectionKind = GetParam(); CustomSetUp(connectionKind); - const SimulationTime sendSimulationTime = GenerateI64(); + const DsVeosCoSim_SimulationTime sendSimulationTime = GenerateI64(); const auto sendCommand = static_cast(GenerateU32()); @@ -350,7 +351,7 @@ TEST_P(TestProtocol, SendAndReceiveStepOk) { // Assert AssertFrame(FrameKind::StepOk); - SimulationTime receiveSimulationTime{}; + DsVeosCoSim_SimulationTime receiveSimulationTime{}; Command receiveCommand{}; ASSERT_TRUE(Protocol::ReadStepOk(_receiverChannel->GetReader(), receiveSimulationTime, diff --git a/utilities/PerformanceTestClient/ClientCoSimCallbackBased.cpp b/utilities/PerformanceTestClient/ClientCoSimCallbackBased.cpp index aca5ceb..77fa3df 100644 --- a/utilities/PerformanceTestClient/ClientCoSimCallbackBased.cpp +++ b/utilities/PerformanceTestClient/ClientCoSimCallbackBased.cpp @@ -29,7 +29,7 @@ void CoSimClientRun(std::string_view host, Event& connectedEvent, uint64_t& coun connectedEvent.Set(); Callbacks callbacks{}; - callbacks.simulationEndStepCallback = [&](SimulationTime) { + callbacks.simulationEndStepCallback = [&](DsVeosCoSim_SimulationTime) { if (isStopped) { coSimClient.Disconnect(); } diff --git a/utilities/PerformanceTestClient/ClientCoSimPollingBased.cpp b/utilities/PerformanceTestClient/ClientCoSimPollingBased.cpp index 635a13b..83af046 100644 --- a/utilities/PerformanceTestClient/ClientCoSimPollingBased.cpp +++ b/utilities/PerformanceTestClient/ClientCoSimPollingBased.cpp @@ -32,7 +32,7 @@ void CoSimClientRun(std::string_view host, Event& connectedEvent, uint64_t& coun coSimClient.StartPollingBasedCoSimulation({}); while (!isStopped) { - SimulationTime simulationTime{}; + DsVeosCoSim_SimulationTime simulationTime{}; Command command{}; MUST_BE_TRUE(coSimClient.PollCommand(simulationTime, command, false)); diff --git a/utilities/PerformanceTestServer/ServerCoSim.cpp b/utilities/PerformanceTestServer/ServerCoSim.cpp index 50aa863..4f7a60c 100644 --- a/utilities/PerformanceTestServer/ServerCoSim.cpp +++ b/utilities/PerformanceTestServer/ServerCoSim.cpp @@ -3,7 +3,6 @@ #include #include "CoSimServer.h" -#include "CoSimServerWrapper.h" #include "LogHelper.h" #include "Logger.h" #include "PerformanceTestHelper.h" @@ -25,21 +24,21 @@ void CoSimServerRun() { config.logCallback = OnLogCallback; config.startPortMapper = false; config.registerAtPortMapper = false; - config.simulationStoppedCallback = [&stopSimulation](SimulationTime) { + config.simulationStoppedCallback = [&stopSimulation](DsVeosCoSim_SimulationTime) { stopSimulation = true; }; - CoSimServerWrapper server; + CoSimServer server; server.Load(config); while (true) { - SimulationTime simulationTime{}; + DsVeosCoSim_SimulationTime simulationTime{}; server.Start(simulationTime); stopSimulation = false; while (!stopSimulation) { - SimulationTime nextSimulationTime{}; + DsVeosCoSim_SimulationTime nextSimulationTime{}; server.Step(simulationTime, nextSimulationTime); simulationTime++; diff --git a/utilities/TestClient/Program.cpp b/utilities/TestClient/Program.cpp index 8b745ed..91dbadd 100644 --- a/utilities/TestClient/Program.cpp +++ b/utilities/TestClient/Program.cpp @@ -24,7 +24,7 @@ RunTimeInfo g_runTimeInfo; std::thread g_simulationThread; -void OnSimulationPostStepCallback(SimulationTime simulationTime) { +void OnSimulationPostStepCallback(DsVeosCoSim_SimulationTime simulationTime) { (void)SendSomeData(simulationTime, g_runTimeInfo); } @@ -33,29 +33,29 @@ void StartSimulationThread(const std::function& function) { g_simulationThread.detach(); } -void OnSimulationStartedCallback(SimulationTime simulationTime) { +void OnSimulationStartedCallback(DsVeosCoSim_SimulationTime simulationTime) { LogInfo("Simulation started at {} s.", SimulationTimeToSeconds(simulationTime)); } -void OnSimulationStoppedCallback(SimulationTime simulationTime) { +void OnSimulationStoppedCallback(DsVeosCoSim_SimulationTime simulationTime) { LogInfo("Simulation stopped at {} s.", SimulationTimeToSeconds(simulationTime)); } -void OnSimulationTerminatedCallback(SimulationTime simulationTime, TerminateReason reason) { +void OnSimulationTerminatedCallback(DsVeosCoSim_SimulationTime simulationTime, DsVeosCoSim_TerminateReason reason) { LogInfo("Simulation terminated with reason {} at {} s.", ToString(reason), SimulationTimeToSeconds(simulationTime)); } -void OnSimulationPausedCallback(SimulationTime simulationTime) { +void OnSimulationPausedCallback(DsVeosCoSim_SimulationTime simulationTime) { LogInfo("Simulation paused at {} s.", SimulationTimeToSeconds(simulationTime)); } -void OnSimulationContinuedCallback(SimulationTime simulationTime) { +void OnSimulationContinuedCallback(DsVeosCoSim_SimulationTime simulationTime) { LogInfo("Simulation continued at {} s.", SimulationTimeToSeconds(simulationTime)); } [[nodiscard]] bool Connect(std::string_view host, std::string_view serverName) { LogInfo("Connecting ..."); - if (g_coSimClient->GetConnectionState() == ConnectionState::Connected) { + if (g_coSimClient->GetConnectionState() == DsVeosCoSim_ConnectionState_Connected) { LogInfo("Already connected."); return true; } @@ -125,7 +125,7 @@ void OnSimulationContinuedCallback(SimulationTime simulationTime) { g_runTimeInfo.transmitLin = [&](const DsVeosCoSim_LinMessage& message) { return g_coSimClient->Transmit(message); }; - g_runTimeInfo.write = [&](IoSignalId signalId, uint32_t length, const void* value) { + g_runTimeInfo.write = [&](DsVeosCoSim_IoSignalId signalId, uint32_t length, const void* value) { return g_coSimClient->Write(signalId, length, value); }; @@ -172,7 +172,7 @@ void HostClient(std::string_view host, std::string_view serverName) { StartSimulationThread(RunCallbackBasedCoSimulation); while (true) { - switch (getChar()) { + switch (GetChar()) { case CTRL('c'): Disconnect(); return; @@ -198,7 +198,7 @@ void HostClient(std::string_view host, std::string_view serverName) { g_coSimClient->Pause(); break; case 't': - g_coSimClient->Terminate(TerminateReason::Error); + g_coSimClient->Terminate(DsVeosCoSim_TerminateReason_Error); break; case 'n': g_coSimClient->Continue(); diff --git a/utilities/TestServer/Program.cpp b/utilities/TestServer/Program.cpp index 91dac1f..81ffdef 100644 --- a/utilities/TestServer/Program.cpp +++ b/utilities/TestServer/Program.cpp @@ -1,13 +1,14 @@ // Copyright dSPACE GmbH. All rights reserved. #include +#include #include #include #include +#include "BackgroundService.h" #include "ClientServerTestHelper.h" #include "CoSimServer.h" -#include "CoSimServerWrapper.h" #include "CoSimTypes.h" #include "Generator.h" #include "Helper.h" @@ -31,11 +32,12 @@ enum class State { bool g_stopSimulationThread; std::thread g_simulationThread; -SimulationTime g_currentTime; +DsVeosCoSim_SimulationTime g_currentTime; RunTimeInfo g_runTimeInfo; -std::unique_ptr g_server; +std::unique_ptr g_server; +std::unique_ptr g_backgroundService; State g_state; std::thread::id g_simulationThreadId; @@ -58,13 +60,15 @@ std::string format_as(State state) { } void DoSimulation() { + g_backgroundService.reset(); + g_simulationThreadId = std::this_thread::get_id(); while (!g_stopSimulationThread) { if (!SendSomeData(g_currentTime, g_runTimeInfo)) { - return; + break; } - SimulationTime nextSimulationTime{}; + DsVeosCoSim_SimulationTime nextSimulationTime{}; g_server->Step(g_currentTime, nextSimulationTime); if (nextSimulationTime > g_currentTime) { @@ -73,6 +77,8 @@ void DoSimulation() { g_currentTime += 1000000; } } + + g_backgroundService = std::make_unique(*g_server); } void StopSimulationThread() { @@ -111,6 +117,8 @@ void StartSimulation() { g_currentTime = 0; LogInfo("Starting ..."); + g_backgroundService.reset(); + g_server->Start(g_currentTime); StartSimulationThread(); @@ -190,33 +198,34 @@ void TerminateSimulation() { StopSimulationThread(); - g_server->Terminate(g_currentTime, TerminateReason::Error); + g_server->Terminate(g_currentTime, DsVeosCoSim_TerminateReason_Error); g_state = State::Terminated; LogInfo("Terminated."); } -void OnSimulationStartedCallback(SimulationTime /* unused */) { +void OnSimulationStartedCallback([[maybe_unused]] DsVeosCoSim_SimulationTime simulationTime) { LogInfo("Received simulation started event."); StartSimulation(); } -void OnSimulationStoppedCallback(SimulationTime /* unused */) { +void OnSimulationStoppedCallback([[maybe_unused]] DsVeosCoSim_SimulationTime simulationTime) { LogInfo("Received simulation stopped event."); StopSimulation(); } -void OnSimulationPausedCallback(SimulationTime /* unused */) { +void OnSimulationPausedCallback([[maybe_unused]] DsVeosCoSim_SimulationTime simulationTime) { LogInfo("Received simulation paused event."); PauseSimulation(); } -void OnSimulationContinuedCallback(SimulationTime /* unused */) { +void OnSimulationContinuedCallback([[maybe_unused]] DsVeosCoSim_SimulationTime simulationTime) { LogInfo("Received simulation continued event."); ContinueSimulation(); } -void OnSimulationTerminatedCallback(SimulationTime /* unused */, TerminateReason /* unused */) { +void OnSimulationTerminatedCallback([[maybe_unused]] DsVeosCoSim_SimulationTime simulationTime, + [[maybe_unused]] DsVeosCoSim_TerminateReason terminateReason) { LogInfo("Received simulation continued event."); TerminateSimulation(); } @@ -250,7 +259,7 @@ void LoadSimulation(bool isClientOptional, std::string_view name) { config.incomingSignals = CreateSignals(1); config.outgoingSignals = CreateSignals(1); - g_server = std::make_unique(); + g_server = std::make_unique(); g_server->Load(config); g_state = State::Stopped; @@ -268,10 +277,12 @@ void LoadSimulation(bool isClientOptional, std::string_view name) { g_runTimeInfo.transmitLin = [&](const DsVeosCoSim_LinMessage& message) { return g_server->Transmit(message); }; - g_runTimeInfo.write = [&](IoSignalId signalId, uint32_t length, const void* value) { + g_runTimeInfo.write = [&](DsVeosCoSim_IoSignalId signalId, uint32_t length, const void* value) { return g_server->Write(signalId, length, value); }; + g_backgroundService = std::make_unique(*g_server); + LogInfo("Loaded."); } @@ -289,7 +300,7 @@ void HostServer(bool isClientOptional, std::string_view name) { LoadSimulation(isClientOptional, name); while (true) { - switch (getChar()) { + switch (GetChar()) { case CTRL('c'): return; case 'l': From d5ce91ade9977aad389d2e53f5e48228daccce97 Mon Sep 17 00:00:00 2001 From: Artur Wolf Date: Tue, 17 Sep 2024 16:30:37 +0200 Subject: [PATCH 2/2] Fixed tests for Linux --- shared/LogHelper.cpp | 18 +++++++++++------- src/CoSimTypes.h | 16 +++++++++------- src/OsAbstraction/Socket.cpp | 3 ++- test/Communication/TestTcpChannel.cpp | 2 ++ test/OsAbstraction/TestTcpSocket.cpp | 2 ++ utilities/TestClient/Program.cpp | 12 +++++++----- 6 files changed, 33 insertions(+), 20 deletions(-) diff --git a/shared/LogHelper.cpp b/shared/LogHelper.cpp index e4e782c..0782293 100644 --- a/shared/LogHelper.cpp +++ b/shared/LogHelper.cpp @@ -40,7 +40,7 @@ std::string DataToString(const uint8_t* data, uint32_t dataLength, char separato } std::string DataTypeValueToString(const void* value, uint32_t index, DsVeosCoSim_DataType dataType) { - switch (dataType) { // NOLINT + switch (dataType) { case DsVeosCoSim_DataType_Bool: return std::to_string(static_cast(value)[index]); case DsVeosCoSim_DataType_Int8: @@ -63,6 +63,8 @@ std::string DataTypeValueToString(const void* value, uint32_t index, DsVeosCoSim return std::to_string(static_cast(value)[index]); case DsVeosCoSim_DataType_Float64: return std::to_string(static_cast(value)[index]); + case DsVeosCoSim_DataType_INT_MAX_SENTINEL_DO_NOT_USE_: + break; } throw std::runtime_error("Invalid data type."); @@ -102,7 +104,7 @@ void InitializeOutput() { void OnLogCallback(DsVeosCoSim_Severity severity, std::string_view message) { g_lastMessage = message; - switch (severity) { // NOLINT + switch (severity) { case DsVeosCoSim_Severity_Error: print(red, "{}\n", message); break; @@ -115,6 +117,8 @@ void OnLogCallback(DsVeosCoSim_Severity severity, std::string_view message) { case DsVeosCoSim_Severity_Trace: print(gray, "{}\n", message); break; + case DsVeosCoSim_Severity_INT_MAX_SENTINEL_DO_NOT_USE_: + break; } } @@ -133,7 +137,7 @@ void LogIoData(DsVeosCoSim_SimulationTime simulationTime, const void* value) { print(violet, "{},{},{},{}\n", - SimulationTimeToSeconds(simulationTime), + DSVEOSCOSIM_SIMULATION_TIME_TO_SECONDS(simulationTime), ioSignal.name, length, ValueToString(value, length, ioSignal.dataType)); @@ -177,7 +181,7 @@ void LogCanMessage(DsVeosCoSim_SimulationTime simulationTime, const DsVeosCoSim_CanMessage& message) { print(blue, "{},{},{},{},{},CAN,{}\n", - SimulationTimeToSeconds(simulationTime), + DSVEOSCOSIM_SIMULATION_TIME_TO_SECONDS(simulationTime), controller.name, message.id, message.length, @@ -198,7 +202,7 @@ void LogEthMessage(DsVeosCoSim_SimulationTime simulationTime, print(cyan, "{},{},{}-{},{},{},ETH,{},{}\n", - SimulationTimeToSeconds(simulationTime), + DSVEOSCOSIM_SIMULATION_TIME_TO_SECONDS(simulationTime), controller.name, macAddress2, macAddress1, @@ -209,7 +213,7 @@ void LogEthMessage(DsVeosCoSim_SimulationTime simulationTime, } else { print(cyan, "{},{},{},{},ETH,{}\n", - SimulationTimeToSeconds(simulationTime), + DSVEOSCOSIM_SIMULATION_TIME_TO_SECONDS(simulationTime), controller.name, length, DataToString(data, length, '-'), @@ -222,7 +226,7 @@ void LogLinMessage(DsVeosCoSim_SimulationTime simulationTime, const DsVeosCoSim_LinMessage& message) { print(green, "{},{},{},{},{},LIN,{}\n", - SimulationTimeToSeconds(simulationTime), + DSVEOSCOSIM_SIMULATION_TIME_TO_SECONDS(simulationTime), controller.name, message.id, message.length, diff --git a/src/CoSimTypes.h b/src/CoSimTypes.h index 9e75589..f4ca317 100644 --- a/src/CoSimTypes.h +++ b/src/CoSimTypes.h @@ -18,10 +18,6 @@ constexpr uint32_t EthMessageMaxLength = DSVEOSCOSIM_ETH_MESSAGE_MAX_LENGTH; // constexpr uint32_t LinMessageMaxLength = DSVEOSCOSIM_LIN_MESSAGE_MAX_LENGTH; // NOLINT constexpr uint32_t EthAddressLength = DSVEOSCOSIM_ETH_ADDRESS_LENGTH; -[[nodiscard]] inline double SimulationTimeToSeconds(DsVeosCoSim_SimulationTime simulationTime) { - return DSVEOSCOSIM_SIMULATION_TIME_TO_SECONDS(simulationTime); -} - class CoSimException final : public std::runtime_error { public: explicit CoSimException(std::string_view message) : std::runtime_error(message.data()) { @@ -98,7 +94,7 @@ enum class Command { } [[nodiscard]] inline std::string ToString(DsVeosCoSim_Severity severity) { - switch (severity) { // NOLINT + switch (severity) { case DsVeosCoSim_Severity_Error: return "Error"; case DsVeosCoSim_Severity_Warning: @@ -107,28 +103,34 @@ enum class Command { return "Info"; case DsVeosCoSim_Severity_Trace: return "Trace"; + case DsVeosCoSim_Severity_INT_MAX_SENTINEL_DO_NOT_USE_: + break; } return std::to_string(severity); } inline std::string ToString(DsVeosCoSim_TerminateReason terminateReason) { - switch (terminateReason) { // NOLINT + switch (terminateReason) { case DsVeosCoSim_TerminateReason_Finished: return "Finished"; case DsVeosCoSim_TerminateReason_Error: return "Error"; + case DsVeosCoSim_TerminateReason_INT_MAX_SENTINEL_DO_NOT_USE_: + break; } return std::to_string(terminateReason); } inline std::string ToString(DsVeosCoSim_ConnectionState connectionState) { - switch (connectionState) { // NOLINT + switch (connectionState) { case DsVeosCoSim_ConnectionState_Connected: return "Connected"; case DsVeosCoSim_ConnectionState_Disconnected: return "Disconnected"; + case DsVeosCoSim_ConnectionState_INT_MAX_SENTINEL_DO_NOT_USE_: + break; } return std::to_string(connectionState); diff --git a/src/OsAbstraction/Socket.cpp b/src/OsAbstraction/Socket.cpp index 8d0fbd0..fd87fb0 100644 --- a/src/OsAbstraction/Socket.cpp +++ b/src/OsAbstraction/Socket.cpp @@ -368,6 +368,8 @@ void Socket::Close() { return; } + Shutdown(); + _socket = InvalidSocket; _addressFamily = {}; @@ -376,7 +378,6 @@ void Socket::Close() { _path = {}; } - Shutdown(); CloseSocket(socket); } diff --git a/test/Communication/TestTcpChannel.cpp b/test/Communication/TestTcpChannel.cpp index 151a2a9..ca8c8ea 100644 --- a/test/Communication/TestTcpChannel.cpp +++ b/test/Communication/TestTcpChannel.cpp @@ -48,6 +48,7 @@ TEST_F(TestTcpChannel, ServerStartWithZeroPort) { ASSERT_NE(0, port); } +#ifdef _WIN32 TEST_P(TestTcpChannel, ConnectWithoutStart) { // Arrange Param param = GetParam(); @@ -66,6 +67,7 @@ TEST_P(TestTcpChannel, ConnectWithoutStart) { // Assert ASSERT_FALSE(connectedChannel); } +#endif TEST_P(TestTcpChannel, Connect) { // Arrange diff --git a/test/OsAbstraction/TestTcpSocket.cpp b/test/OsAbstraction/TestTcpSocket.cpp index 9ca7651..c813389 100644 --- a/test/OsAbstraction/TestTcpSocket.cpp +++ b/test/OsAbstraction/TestTcpSocket.cpp @@ -73,6 +73,7 @@ TEST_P(TestTcpSocket, Listen) { ASSERT_NO_THROW(serverSocket.Listen()); } +#ifdef _WIN32 TEST_P(TestTcpSocket, ConnectWithoutListening) { // Arrange Param param = GetParam(); @@ -87,6 +88,7 @@ TEST_P(TestTcpSocket, ConnectWithoutListening) { // Assert ASSERT_FALSE(connectedSocket); } +#endif TEST_P(TestTcpSocket, Connect) { // Arrange diff --git a/utilities/TestClient/Program.cpp b/utilities/TestClient/Program.cpp index 91dbadd..fbea7a2 100644 --- a/utilities/TestClient/Program.cpp +++ b/utilities/TestClient/Program.cpp @@ -34,23 +34,25 @@ void StartSimulationThread(const std::function& function) { } void OnSimulationStartedCallback(DsVeosCoSim_SimulationTime simulationTime) { - LogInfo("Simulation started at {} s.", SimulationTimeToSeconds(simulationTime)); + LogInfo("Simulation started at {} s.", DSVEOSCOSIM_SIMULATION_TIME_TO_SECONDS(simulationTime)); } void OnSimulationStoppedCallback(DsVeosCoSim_SimulationTime simulationTime) { - LogInfo("Simulation stopped at {} s.", SimulationTimeToSeconds(simulationTime)); + LogInfo("Simulation stopped at {} s.", DSVEOSCOSIM_SIMULATION_TIME_TO_SECONDS(simulationTime)); } void OnSimulationTerminatedCallback(DsVeosCoSim_SimulationTime simulationTime, DsVeosCoSim_TerminateReason reason) { - LogInfo("Simulation terminated with reason {} at {} s.", ToString(reason), SimulationTimeToSeconds(simulationTime)); + LogInfo("Simulation terminated with reason {} at {} s.", + ToString(reason), + DSVEOSCOSIM_SIMULATION_TIME_TO_SECONDS(simulationTime)); } void OnSimulationPausedCallback(DsVeosCoSim_SimulationTime simulationTime) { - LogInfo("Simulation paused at {} s.", SimulationTimeToSeconds(simulationTime)); + LogInfo("Simulation paused at {} s.", DSVEOSCOSIM_SIMULATION_TIME_TO_SECONDS(simulationTime)); } void OnSimulationContinuedCallback(DsVeosCoSim_SimulationTime simulationTime) { - LogInfo("Simulation continued at {} s.", SimulationTimeToSeconds(simulationTime)); + LogInfo("Simulation continued at {} s.", DSVEOSCOSIM_SIMULATION_TIME_TO_SECONDS(simulationTime)); } [[nodiscard]] bool Connect(std::string_view host, std::string_view serverName) {