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/safety/Safety.hpp>
38#include <dw/core/system/NvMediaExt.h>
71 case ChannelType::SHMEM_LOCAL:
73 result =
"type=SHMEM_LOCAL";
76 case ChannelType::SHMEM_REMOTE:
78 result =
"type=SHMEM_REMOTE";
81 case ChannelType::EGLSTREAM:
83 result =
"type=EGLSTREAM";
86 case ChannelType::SOCKET:
88 result =
"type=SOCKET";
91 case ChannelType::DDS:
96 case ChannelType::NVSCI:
98 result =
"type=NVSCI";
217 return dw::core::safeStrtol(value.c_str(),
nullptr, 10);
225 int64_t translatedSize{ParseChannelParameter<int64_t>(value)};
227 if (translatedSize < 0)
229 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"ParseChannelParameter: size_t is negative");
231 size_t result{
static_cast<size_t>(translatedSize)};
240 size_t translatedSize{ParseChannelParameter<size_t>(value)};
241 if (translatedSize > std::numeric_limits<uint32_t>::max())
243 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"ParseChannelParameter: value is larger than uint32_t allows");
245 uint32_t result{
static_cast<uint32_t
>(translatedSize)};
254 size_t translatedSize{ParseChannelParameter<size_t>(value)};
255 if (translatedSize > std::numeric_limits<uint16_t>::max())
257 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"ChannelSocketParams: port is larger than uint16_t allows!");
259 uint16_t result{
static_cast<uint16_t
>(translatedSize)};
270 if ((value ==
"true") || (value ==
"1"))
275 else if ((value ==
"false") || (value ==
"0"))
281 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"ParseChannelParameter: needs to be 'true' or 'false' or 1/0");
292 if (value ==
"producer")
297 if (value ==
"consumer")
302 if (value ==
"composite")
306 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"ParseChannelParameter: role unknown!");
315 if (value ==
"SHMEM_LOCAL")
317 return ChannelType::SHMEM_LOCAL;
320 if (value ==
"SHMEM_REMOTE")
322 return ChannelType::SHMEM_REMOTE;
325 if (value ==
"EGLSTREAM")
327 return ChannelType::EGLSTREAM;
330 if (value ==
"SOCKET")
332 return ChannelType::SOCKET;
337 return ChannelType::DDS;
340 if (value ==
"NVSCI")
342 return ChannelType::NVSCI;
344 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"ParseChannelParameter: type unknown!");
354 if (value ==
"mailbox")
359 else if (value ==
"singleton")
365 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"ParseChannelParameter: ChannelMode unknown!");
376 size_t pos{value.find(
".")};
383 result.
socID = ParseChannelParameter<uint32_t>(first);
384 result.vmID = ParseChannelParameter<uint32_t>(second);
393 result = ParseChannelParameter<T>(value);
396template <
size_t Size>
404template <
typename T,
size_t N>
414 endpos = value.find(
":", pos);
415 bool done{endpos == dw::core::FixedString<1>::NPOS};
416 size_t count{done ? endpos : endpos - pos};
419 result.push_back(entry);
437template <
typename T,
typename... Others>
442 if (key == staticKey)
450template <
typename... Others>
458 std::size_t pos{channelParams.find(
"=")};
460 std::size_t value{0U};
462 std::size_t valueEnd{0U};
466 while (pos != dw::core::FixedString<1>::NPOS && value != dw::core::FixedString<1>::NPOS)
470 keyString = channelParams.substr(key, pos - key);
472 value = channelParams.find(
",", pos);
476 valueEnd = (value == dw::core::FixedString<1>::NPOS) ? (channelParams.length() - (pos + 1U)) : (value - pos - 1U);
478 valueString = channelParams.substr(pos + 1U, valueEnd);
484 pos = channelParams.find(
"=", key);
493 bool producerFifo =
false,
494 uint16_t numBlockingConnections = 1U,
495 dw::core::FixedString<8>
const sockPrefix = dw::core::FixedString<8>())
497 std::stringstream ss{};
498 ss.flags(std::ios::dec);
500 if (serverIP !=
nullptr)
507 ss <<
",producer-fifo=";
509 ss << (producerFifo ? 1U : 0U);
510 ss <<
",num-clients=";
511 ss << numBlockingConnections;
512 ss <<
",sock-prefix=";
521 dw::core::FixedString<MAX_CHANNEL_ALL_PARAMS_SIZE> channelParams{params};
526 "producer-fifo", m_producerFifo,
530 "connect-timeout", m_connectTimeout,
532 "sock-prefix", m_sockPrefix);
548 bool m_producerFifo{
false};
549 dwTime_t m_connectTimeout{DW_TIME_INVALID};
550 dw::core::FixedString<8> m_sockPrefix{};
563 dw::core::FixedString<MAX_CHANNEL_ALL_PARAMS_SIZE> channelParams{params};
581 static_assert(
decltype(
m_streamNames)::CAPACITY_AT_COMPILE_TIME < std::numeric_limits<uint16_t>::max(),
"ChannelNvSciStreamParams: number of outputs over limit");
598 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"ChannelNvSciStreamParams: stream name index out of range");
616 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"ChannelNvSciStreamParams: limiter maxPackets index out of range");
639 "fifo-size", m_fifoSize,
645 "connect-group-id", m_connectGroupID,
647 "singleton-id", m_singletonId,
651 "reuse", m_reuseEnabled,
653 "debug-port", m_debugPort,
655 "num-clients", m_clientsCount,
657 "debug-num-clients", m_debugClientsCount,
663 "data-offset", m_dataOffset,
665 "strict", m_strictFifo,
667 "sync-enabled", m_syncEnabled);
669 adjustPoolCapacity();
672 if (m_clientsCount == 0U)
678 if (!m_singletonId.empty())
685 m_mailboxMode =
true;
689 m_singletonMode =
true;
692 if (!m_id.empty() && m_singletonId.empty())
694 m_singletonId = m_id;
706 case ChannelType::SHMEM_LOCAL:
709 case ChannelType::SOCKET:
713 case ChannelType::NVSCI:
717 case ChannelType::SHMEM_REMOTE:
719 case ChannelType::EGLSTREAM:
721 case ChannelType::DDS:
723 throw ExceptionWithStatus(DW_NOT_IMPLEMENTED,
"ChannelParams: no parameters for channel type");
735 const char*
getStr()
const {
return m_str.c_str(); }
747 m_strictFifo = strictFifo;
769 uint32_t
getUID()
const {
return m_uid; }
774 if (m_type != ChannelType::SOCKET)
776 throw ExceptionWithStatus(DW_CALL_NOT_ALLOWED,
"ChannelParams: getSocketParams: channel is not of type SOCKET");
778 return m_socketParams;
783 if (m_type != ChannelType::NVSCI)
785 throw ExceptionWithStatus(DW_CALL_NOT_ALLOWED,
"ChannelParams: getNvSciStreamParams: channel is not of type NVSCI");
787 return m_nvSciStreamParams;
791 void ValidateMailbox()
796 if (m_fifoSize != 1U)
798 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"ChannelParams: Singleton and mailbox modes are incompatible with a fifo setting other than 1");
801 if (!m_mailboxMode && m_reuseEnabled)
803 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"ChannelParams: reuse=true specified when mode!=mailbox. Not valid");
805 if (m_mailboxMode && m_singletonMode)
807 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"ChannelParams: Singleton mode is incompatible mailbox mode");
811 void ValidateSingleton()
814 if (m_singletonMode && m_singletonId.empty())
816 m_singletonId = m_id;
818 if (!m_singletonMode && !m_singletonId.empty())
820 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"ChannelParams: Singleton mode requires both the mode set AND singletonId set");
822 if (m_singletonMode && (m_type != ChannelType::SHMEM_LOCAL))
824 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"ChannelParams: Singleton mode is only valid for SHMEM_LOCAL channels");
828 void adjustPoolCapacity()
853 size_t m_fifoSize{1U};
862 uint32_t m_dataOffset{0U};
864 uint32_t m_connectGroupID{0U};
865 bool m_syncEnabled{
false};
866 bool m_mailboxMode{
false};
867 bool m_singletonMode{
false};
868 bool m_reuseEnabled{
false};
871 bool m_strictFifo{
true};
873 uint16_t m_clientsCount{1U};
874 uint16_t m_debugClientsCount{1U};
876 uint16_t m_debugPort{0U};
878 ChannelSocketParams m_socketParams{};
879 ChannelNvSciStreamParams m_nvSciStreamParams{};
ChannelNvSciStreamParams & operator=(const ChannelNvSciStreamParams &other)=default
int64_t getLimiterMaxPackets(uint16_t index=0U) const
ChannelNvSciStreamParams(const char *params)
uint32_t m_localClientCount
uint32_t getLocalClientCount() const
dw::core::VectorFixed< ChannelPeerLocation, 32 > m_lateLocs
ChannelNvSciStreamParams(const ChannelNvSciStreamParams &other)=default
dw::core::span< ChannelPeerLocation const > getLateLocs() const
const char * getStreamName(uint16_t index=0) const
ChannelNvSciStreamParams()=default
uint16_t getNumOutputs() const
dw::core::VectorFixed< dw::core::FixedString< 64 >, 8 > m_streamNames
dw::core::VectorFixed< int64_t, 8 > m_limits
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
bool getSyncEnabled() const
const ChannelNvSciStreamParams & getNvSciStreamParams() const
bool getDebugMode() 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
dw::core::FixedString< 8 > getSockPrefix() const
ChannelSocketParams & operator=(const ChannelSocketParams &other)=default
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 &)
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
enum class ChannelType :uint8_t { SHMEM_LOCAL=0, SHMEM_REMOTE=1, EGLSTREAM=2, SOCKET=3, DDS=4, NVSCI=5, } ChannelType
constexpr bool IsConsumer(ChannelRole role)
static constexpr uint16_t MAX_CHANNEL_PRODUCERS_COUNT
@ CONNECTION_TYPE_DYNAMIC
static constexpr uint16_t MAX_CHANNEL_CONSUMERS_COUNT