31#ifndef DW_FRAMEWORK_CHANNEL_PARAMETERS_HPP_
32#define DW_FRAMEWORK_CHANNEL_PARAMETERS_HPP_
34#include <dwshared/dwfoundation/dw/core/base/ExceptionWithStatus.hpp>
35#include <dwshared/dwfoundation/dw/core/container/BaseString.hpp>
36#include <dwshared/dwfoundation/dw/core/container/VectorFixed.hpp>
37#include <dwshared/dwfoundation/dw/core/language/cxx23.hpp>
38#include <dwshared/dwfoundation/dw/core/safety/Safety.hpp>
39#include <dwshared/dwsockets/SocketClientServer.hpp>
40#include <dw/core/system/NvMediaExt.h>
76 return "type=SHMEM_LOCAL";
79 return "type=SHMEM_REMOTE";
82 return "type=EGLSTREAM";
96 dw::core::unreachable();
213 return dw::core::safeStrtol(value.c_str(),
nullptr, 10);
221 int64_t translatedSize{ParseChannelParameter<int64_t>(value)};
223 if (translatedSize < 0)
225 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"ParseChannelParameter: size_t is negative");
227 size_t result{
static_cast<size_t>(translatedSize)};
236 size_t translatedSize{ParseChannelParameter<size_t>(value)};
237 if (translatedSize > std::numeric_limits<uint32_t>::max())
239 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"ParseChannelParameter: value is larger than uint32_t allows");
241 uint32_t result{
static_cast<uint32_t
>(translatedSize)};
250 size_t translatedSize{ParseChannelParameter<size_t>(value)};
251 if (translatedSize > std::numeric_limits<uint16_t>::max())
253 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"ParseChannelParameter: port is larger than uint16_t allows!");
255 uint16_t result{
static_cast<uint16_t
>(translatedSize)};
264 size_t translatedSize{ParseChannelParameter<size_t>(value)};
265 if (translatedSize > std::numeric_limits<uint8_t>::max())
267 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"ParseChannelParameter: port is larger than uint8_t allows!");
269 uint8_t result{
static_cast<uint8_t
>(translatedSize)};
280 if ((value ==
"true") || (value ==
"1"))
285 else if ((value ==
"false") || (value ==
"0"))
291 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"ParseChannelParameter: needs to be 'true' or 'false' or 1/0");
302 if (value ==
"producer")
307 if (value ==
"consumer")
312 if (value ==
"composite")
316 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"ParseChannelParameter: role unknown!");
325 if (value ==
"SHMEM_LOCAL")
330 if (value ==
"SHMEM_REMOTE")
335 if (value ==
"EGLSTREAM")
340 if (value ==
"SOCKET")
350 if (value ==
"NVSCI")
359 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"ParseChannelParameter: type unknown!");
369 if (value ==
"mailbox")
374 else if (value ==
"singleton")
380 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"ParseChannelParameter: ChannelMode unknown!");
391 size_t pos{value.find(
".")};
396 ChannelParamStr second{value.substr(dw::core::safeAdd(pos, 1U).value())};
397 result.
socID = ParseChannelParameter<uint32_t>(first);
398 result.vmID = ParseChannelParameter<uint32_t>(second);
407 result = ParseChannelParameter<T>(value);
410template <
size_t Size>
418template <
typename T,
size_t N>
428 endpos = value.find(
":", pos);
429 bool done{endpos == dw::core::FixedString<1>::NPOS};
430 size_t count{done ? endpos : endpos - pos};
433 result.push_back(entry);
451template <
typename T,
typename... Others>
456 if (key == staticKey)
464template <
typename... Others>
472 std::size_t pos{channelParams.find(
"=")};
474 std::size_t value{0U};
476 std::size_t valueEnd{0U};
480 while (pos != dw::core::FixedString<1>::NPOS && value != dw::core::FixedString<1>::NPOS)
482 keyString = channelParams.substr(key, dw::core::safeSub(pos, key).value());
484 value = channelParams.find(
",", pos);
485 if (value == dw::core::FixedString<1>::NPOS)
488 valueEnd = dw::core::safeSub(channelParams.length(), dw::core::safeAdd(pos, 1U).value()).value();
493 valueEnd = dw::core::safeSub(dw::core::safeSub(value, pos).value(), 1U).value();
496 valueString = channelParams.substr(pos + 1U, valueEnd);
502 pos = channelParams.find(
"=", key);
511 bool producerFifo =
false,
512 uint16_t numBlockingConnections = 1U,
513 dw::core::FixedString<8>
const sockPrefix = dw::core::FixedString<8>())
515 std::stringstream ss{};
516 ss.flags(std::ios::dec);
518 if (serverIP !=
nullptr)
525 ss <<
",producer-fifo=";
527 ss << (producerFifo ? 1U : 0U);
528 ss <<
",num-clients=";
529 ss << numBlockingConnections;
530 ss <<
",sock-prefix=";
539 dw::core::FixedString<MAX_CHANNEL_ALL_PARAMS_SIZE> channelParams{params};
544 "producer-fifo", m_producerFifo,
548 "connect-timeout", m_connectTimeout,
550 "sock-prefix", m_sockPrefix);
561 dwshared::socketipc::SockPrefixStr
getSockPrefix()
const {
return m_sockPrefix; }
566 bool m_producerFifo{
false};
567 dwTime_t m_connectTimeout{DW_TIME_INVALID};
568 dwshared::socketipc::SockPrefixStr m_sockPrefix{};
581 dw::core::FixedString<MAX_CHANNEL_ALL_PARAMS_SIZE> channelParams{params};
599 static_assert(
decltype(
m_streamNames)::CAPACITY_AT_COMPILE_TIME < std::numeric_limits<size_t>::max(),
"ChannelNvSciStreamParams: number of outputs over limit");
615 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"ChannelNvSciStreamParams: stream name index out of range");
633 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"ChannelNvSciStreamParams: limiter maxPackets index out of range");
654 "compat-vendor", m_compatVendor,
656 "compat-app", m_compatApp,
658 "num-channel", m_numChannel);
659 m_compat = m_compatVendor;
662 m_compat += m_compatApp;
669 const char*
getCompat()
const {
return m_compat.c_str(); }
676 uint8_t m_numChannel{0U};
690 "fifo-size", m_fifoSize,
696 "connect-group-id", m_connectGroupID,
698 "singleton-id", m_singletonId,
702 "reuse", m_reuseEnabled,
704 "debug-port", m_debugPort,
706 "num-clients", m_clientsCount,
708 "debug-num-clients", m_debugClientsCount,
714 "data-offset", m_dataOffset,
716 "strict", m_strictFifo,
718 "sync-enabled", m_syncEnabled,
722 adjustPoolCapacity();
725 if (m_clientsCount == 0U)
731 if (!m_singletonId.empty())
738 m_mailboxMode =
true;
742 m_singletonMode =
true;
745 if (!m_id.empty() && m_singletonId.empty())
747 m_singletonId = m_id;
773 throw ExceptionWithStatus(DW_NOT_IMPLEMENTED,
"ChannelParams: no parameters for channel type");
785 const char*
getStr()
const {
return m_str.c_str(); }
797 m_strictFifo = strictFifo;
819 uint32_t
getUID()
const {
return m_uid; }
829 throw ExceptionWithStatus(DW_CALL_NOT_ALLOWED,
"ChannelParams: getSocketParams: channel is not of type SOCKET");
831 return m_socketParams;
838 throw ExceptionWithStatus(DW_CALL_NOT_ALLOWED,
"ChannelParams: getNvSciStreamParams: channel is not of type NVSCI");
840 return m_nvSciStreamParams;
847 throw ExceptionWithStatus(DW_CALL_NOT_ALLOWED,
"ChannelParams: getFSIParams: channel is not of type FSI");
853 void ValidateMailbox()
858 if (m_fifoSize != 1U)
860 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"ChannelParams: Singleton and mailbox modes are incompatible with a fifo setting other than 1");
863 if (!m_mailboxMode && m_reuseEnabled)
865 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"ChannelParams: reuse=true specified when mode!=mailbox. Not valid");
867 if (m_mailboxMode && m_singletonMode)
869 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"ChannelParams: Singleton mode is incompatible mailbox mode");
873 void ValidateSingleton()
876 if (m_singletonMode && m_singletonId.empty())
878 m_singletonId = m_id;
880 if (!m_singletonMode && !m_singletonId.empty())
882 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"ChannelParams: Singleton mode requires both the mode set AND singletonId set");
886 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"ChannelParams: Singleton mode is only valid for SHMEM_LOCAL channels");
890 void adjustPoolCapacity()
913 size_t m_fifoSize{1U};
922 uint32_t m_dataOffset{0U};
924 uint32_t m_connectGroupID{0U};
925 bool m_syncEnabled{
false};
926 bool m_mailboxMode{
false};
927 bool m_singletonMode{
false};
928 bool m_reuseEnabled{
false};
931 bool m_strictFifo{
true};
933 uint16_t m_clientsCount{1U};
934 uint16_t m_debugClientsCount{1U};
936 uint16_t m_debugPort{0U};
938 ChannelSocketParams m_socketParams{};
939 ChannelNvSciStreamParams m_nvSciStreamParams{};
940 ChannelFSIParams m_fsiParams{};
ChannelFSIParams()=default
ChannelFSIParams & operator=(const ChannelFSIParams &other)=default
uint8_t getNumChannel() const
ChannelFSIParams(const char *params)
ChannelFSIParams(const ChannelFSIParams &other)=default
const char * getCompat() const
ChannelNvSciStreamParams & operator=(const ChannelNvSciStreamParams &other)=default
size_t getLocalClientCount() const
ChannelNvSciStreamParams(const char *params)
int64_t getLimiterMaxPackets(size_t index=0U) const
dw::core::VectorFixed< ChannelStreamNameStr, 8 > m_streamNames
dw::core::VectorFixed< ChannelPeerLocation, 32 > m_lateLocs
size_t m_localClientCount
ChannelNvSciStreamParams(const ChannelNvSciStreamParams &other)=default
dw::core::span< ChannelPeerLocation const > getLateLocs() const
size_t getNumOutputs() const
ChannelNvSciStreamParams()=default
dw::core::VectorFixed< int64_t, 8 > m_limits
ChannelStreamNameStr getStreamName(size_t index=0U) const
void setMailboxMode(bool mailboxEnabled)
bool isStrictFifo() const
bool getReuseEnabled() const
size_t getPoolCapacity() const
const char * getStr() const
uint16_t getExpectedConnectionsCount() const
ChannelType getType() const
uint16_t getExpectedDebugConnectionsCount() const
ChannelParams & operator=(const ChannelParams &other)=default
const ChannelSocketParams & getSocketParams() const
ChannelParamStr getName() const
bool getSyncEnabled() const
const ChannelNvSciStreamParams & getNvSciStreamParams() const
bool getDebugMode() const
const ChannelFSIParams & getFSIParams() const
ChannelParamStr getSingletonId() const
uint32_t getConnectGroupID() const
uint16_t getDebugPort() const
void setStrictFifo(bool strictFifo)
ChannelRole getRole() const
bool getMailboxMode() const
ChannelParams(const ChannelParams &other)
ChannelParamStr getId() const
size_t getFifoSize() const
uint32_t getDataOffset() const
ChannelParams(const char *params)
bool getSingletonMode() const
dwTime_t getConnectTimeout() const
bool hasProducerFifo() const
ChannelSocketParams & operator=(const ChannelSocketParams &other)=default
dwshared::socketipc::SockPrefixStr getSockPrefix() const
ChannelParamStr getServerIP() const
ChannelSocketParams(const ChannelSocketParams &other)=default
static ChannelParamStr getParamStr(const char *serverIP, uint16_t port, bool producerFifo=false, uint16_t numBlockingConnections=1U, dw::core::FixedString< 8 > const sockPrefix=dw::core::FixedString< 8 >())
ChannelSocketParams(const char *params)
ChannelSocketParams()=default
static const char * ToParam(ChannelType channelType)
ChannelNvSciStreamEnabledComponents
static constexpr uint16_t MAX_CHANNEL_ALL_PARAMS_SIZE
dw::core::FixedString< MAX_CHANNEL_PARAM_SIZE > ChannelParamStr
@ DW_CHANNEL_ROLE_PRODUCER
allows producer only
@ DW_CHANNEL_ROLE_CONSUMER
allows consumer only
@ DW_CHANNEL_ROLE_COMPOSITE
allows both producer and consumer
static void ParseChannelParameters(const ChannelParamStr &, const ChannelParamStr &)
static constexpr uint16_t MAX_CHANNEL_STREAM_NAME_SIZE
constexpr bool IsProducer(ChannelRole role)
static T ParseChannelParameter(const ChannelParamStr &value)
constexpr ChannelNvSciStreamEnabledComponents operator&(ChannelNvSciStreamEnabledComponents a, ChannelNvSciStreamEnabledComponents b)
constexpr ChannelNvSciStreamEnabledComponents operator|(ChannelNvSciStreamEnabledComponents a, ChannelNvSciStreamEnabledComponents b)
static void ParseAllChannelParameters(const ChannelParamStr &channelParams, Others &&... others)
static constexpr uint16_t MAX_CHANNEL_PARAM_SIZE
constexpr bool IsConsumer(ChannelRole role)
static constexpr uint16_t MAX_CHANNEL_PRODUCERS_COUNT
@ CONNECTION_TYPE_DYNAMIC
dw::core::FixedString< MAX_CHANNEL_STREAM_NAME_SIZE > ChannelStreamNameStr
static constexpr uint16_t MAX_CHANNEL_CONSUMERS_COUNT
@ SHMEM_LOCAL
local shared memory
@ DDS
Data Distribution Service (DDS)
@ SHMEM_REMOTE
remote shared memory