31#ifndef DW_FRAMEWORK_CHANNEL_PARAMETERS_HPP_
32#define DW_FRAMEWORK_CHANNEL_PARAMETERS_HPP_
34#include <dw/core/base/Exception.hpp>
35#include <dw/core/container/BaseString.hpp>
36#include <dw/core/container/VectorFixed.hpp>
37#include <dw/core/system/NvMediaExt.h>
52 DW_CHANNEL_TYPE_SHMEM_LOCAL = 0,
53 DW_CHANNEL_TYPE_SHMEM_REMOTE = 1,
54 DW_CHANNEL_TYPE_EGLSTREAM = 2,
55 DW_CHANNEL_TYPE_SOCKET = 3,
56 DW_CHANNEL_TYPE_DDS = 4,
57 DW_CHANNEL_TYPE_NVSCI = 5,
65 case DW_CHANNEL_TYPE_SHMEM_LOCAL:
66 result =
"type=SHMEM_LOCAL";
68 case DW_CHANNEL_TYPE_SHMEM_REMOTE:
69 result =
"type=SHMEM_REMOTE";
71 case DW_CHANNEL_TYPE_EGLSTREAM:
72 result =
"type=EGLSTREAM";
74 case DW_CHANNEL_TYPE_SOCKET:
75 result =
"type=SOCKET";
77 case DW_CHANNEL_TYPE_DDS:
80 case DW_CHANNEL_TYPE_NVSCI:
81 result =
"type=NVSCI";
180 auto translatedSize = strtol(value.c_str(),
nullptr, 10);
181 return translatedSize;
187 auto translatedSize = ParseChannelParameter<int64_t>(value);
188 if (translatedSize < 0)
190 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"ParseChannelParameter: size_t is negative");
192 size_t result =
static_cast<size_t>(translatedSize);
199 auto translatedSize = ParseChannelParameter<size_t>(value);
200 uint32_t result =
static_cast<uint32_t
>(translatedSize);
207 auto translatedSize = ParseChannelParameter<size_t>(value);
208 if (translatedSize > 0xFFFF)
210 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"ChannelSocketParams: port is larger than uint16_t allows!");
212 uint16_t result =
static_cast<uint16_t
>(translatedSize);
220 if ((value ==
"true") || (value ==
"1"))
224 else if ((value ==
"false") || (value ==
"0"))
230 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"ParseChannelParameter: needs to be 'true' or 'false' or 1/0");
239 if (value ==
"producer")
243 else if (value ==
"consumer")
247 else if (value ==
"composite")
253 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"ParseChannelParameter: role unknown!");
262 if (value ==
"SHMEM_LOCAL")
264 result = ChannelType::DW_CHANNEL_TYPE_SHMEM_LOCAL;
266 else if (value ==
"SHMEM_REMOTE")
268 result = ChannelType::DW_CHANNEL_TYPE_SHMEM_REMOTE;
270 else if (value ==
"EGLSTREAM")
272 result = ChannelType::DW_CHANNEL_TYPE_EGLSTREAM;
274 else if (value ==
"SOCKET")
276 result = ChannelType::DW_CHANNEL_TYPE_SOCKET;
278 else if (value ==
"DDS")
280 result = ChannelType::DW_CHANNEL_TYPE_DDS;
282 else if (value ==
"NVSCI")
284 result = ChannelType::DW_CHANNEL_TYPE_NVSCI;
288 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"ParseChannelParameter: type unknown!");
298 if (value ==
"mailbox")
302 else if (value ==
"singleton")
308 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"ParseChannelParameter: ChannelMode unknown!");
316 result = ParseChannelParameter<T>(value);
319template <
size_t Size>
325template <
typename T,
size_t N>
333 endpos = value.find(
":", pos);
334 done = endpos == dw::core::FixedString<1>::NPOS;
335 size_t count = done ? endpos : endpos - pos;
338 result.push_back(entry);
348template <
typename T,
typename... Others>
351 if (key == staticKey)
359template <
typename... Others>
363 std::size_t pos = channelParams.find(
"=");
364 std::size_t value = 0;
365 std::size_t valueEnd = 0;
369 while (pos != dw::core::FixedString<1>::NPOS && value != dw::core::FixedString<1>::NPOS)
371 keyString = channelParams.substr(key, pos - key);
372 value = channelParams.find(
",", pos);
373 valueEnd = (value == dw::core::FixedString<1>::NPOS) ? (channelParams.length() - (pos + 1)) : (value - pos - 1);
374 valueString = channelParams.substr(pos + 1, valueEnd);
378 pos = channelParams.find(
"=", key);
387 bool producerFifo =
false,
388 uint16_t numBlockingConnections = 1)
390 std::stringstream ss;
391 ss.flags(std::ios::dec);
393 if (serverIP !=
nullptr)
400 ss <<
",producer-fifo=";
401 ss << static_cast<uint32_t>(producerFifo);
402 ss <<
",num-clients=";
403 ss << numBlockingConnections;
411 dw::core::FixedString<MAX_CHANNEL_ALL_PARAMS_SIZE> channelParams(params);
414 "producer-fifo", m_producerFifo,
416 "connect-timeout", m_connectTimeout);
431 bool m_producerFifo =
false;
432 dwTime_t m_connectTimeout = DW_TIME_INVALID;
445 dw::core::FixedString<MAX_CHANNEL_ALL_PARAMS_SIZE> channelParams(params);
458 static_assert(
decltype(
m_streamNames)::CAPACITY_AT_COMPILE_TIME < std::numeric_limits<uint16_t>::max(),
"ChannelNvSciStreamParams: number of outputs over limit");
468 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"ChannelNvSciStreamParams: stream name index out of range");
484 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"ChannelNvSciStreamParams: limiter maxPackets index out of range");
505 "fifo-size", m_fifoSize,
507 "singleton-id", m_singletonId,
509 "reuse", m_reuseEnabled,
510 "debug-port", m_debugPort,
511 "num-clients", m_clientsCount,
512 "debug-num-clients", m_debugClientsCount,
515 "data-offset", m_dataOffset,
516 "strict", m_strictFifo,
517 "sync-enabled", m_syncEnabled);
519 adjustPoolCapacity();
521 if (m_clientsCount == 0)
526 if (!m_singletonId.empty())
533 m_mailboxMode =
true;
537 m_singletonMode =
true;
540 if (!m_id.empty() && m_singletonId.empty())
542 m_singletonId = m_id;
553 case DW_CHANNEL_TYPE_SHMEM_LOCAL:
555 case DW_CHANNEL_TYPE_SOCKET:
558 case DW_CHANNEL_TYPE_NVSCI:
561 case DW_CHANNEL_TYPE_SHMEM_REMOTE:
562 case DW_CHANNEL_TYPE_EGLSTREAM:
563 case DW_CHANNEL_TYPE_DDS:
565 throw ExceptionWithStatus(DW_NOT_IMPLEMENTED,
"ChannelParams: no parameters for channel type");
578 const char*
getStr()
const {
return m_str.c_str(); }
590 m_strictFifo = strictFifo;
614 if (m_type != ChannelType::DW_CHANNEL_TYPE_SOCKET)
616 throw ExceptionWithStatus(DW_CALL_NOT_ALLOWED,
"ChannelParams: getSocketParams: channel is not of type SOCKET");
618 return m_socketParams;
623 if (m_type != ChannelType::DW_CHANNEL_TYPE_NVSCI)
625 throw ExceptionWithStatus(DW_CALL_NOT_ALLOWED,
"ChannelParams: getNvSciStreamParams: channel is not of type NVSCI");
627 return m_nvSciStreamParams;
631 void ValidateMailbox()
637 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"ChannelParams: Singleton and mailbox modes are incompatible with a fifo setting other than 1");
640 if (!m_mailboxMode && m_reuseEnabled)
642 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"ChannelParams: reuse=true specified when mode!=mailbox. Not valid");
644 if (m_mailboxMode && m_singletonMode)
646 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"ChannelParams: Singleton mode is incompatible mailbox mode");
650 void ValidateSingleton()
653 if (m_singletonMode && m_singletonId.empty())
655 m_singletonId = m_id;
657 if (!m_singletonMode && !m_singletonId.empty())
659 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"ChannelParams: Singleton mode requires both the mode set AND singletonId set");
661 if (m_singletonMode && (m_type != ChannelType::DW_CHANNEL_TYPE_SHMEM_LOCAL))
663 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"ChannelParams: Singleton mode is only valid for SHMEM_LOCAL channels");
667 void adjustPoolCapacity()
688 size_t m_fifoSize = 1;
697 uint32_t m_dataOffset = 0;
698 bool m_syncEnabled =
false;
699 bool m_mailboxMode =
false;
700 bool m_singletonMode =
false;
701 bool m_reuseEnabled =
false;
703 ChannelType m_type = ChannelType::DW_CHANNEL_TYPE_SHMEM_LOCAL;
704 bool m_strictFifo =
true;
706 uint16_t m_clientsCount = 1;
707 uint16_t m_debugClientsCount = 1;
709 uint16_t m_debugPort = 0;
711 ChannelSocketParams m_socketParams{};
712 ChannelNvSciStreamParams m_nvSciStreamParams{};
int64_t getLimiterMaxPackets(uint16_t index=0) const
ChannelNvSciStreamParams & operator=(const ChannelNvSciStreamParams &other)=default
ChannelNvSciStreamParams(const char *params)
uint32_t m_localClientCount
uint32_t getLocalClientCount() const
ChannelNvSciStreamParams(const ChannelNvSciStreamParams &other)=default
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
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
static ChannelParamStr getParamStr(const char *serverIP, uint16_t port, bool producerFifo=false, uint16_t numBlockingConnections=1)
dwTime_t getConnectTimeout() const
bool hasProducerFifo() const
ChannelSocketParams & operator=(const ChannelSocketParams &other)=default
ChannelParamStr getServerIP() const
ChannelSocketParams(const ChannelSocketParams &other)=default
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)
enum ChannelType :uint8_t { DW_CHANNEL_TYPE_SHMEM_LOCAL=0, DW_CHANNEL_TYPE_SHMEM_REMOTE=1, DW_CHANNEL_TYPE_EGLSTREAM=2, DW_CHANNEL_TYPE_SOCKET=3, DW_CHANNEL_TYPE_DDS=4, DW_CHANNEL_TYPE_NVSCI=5, } ChannelType
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
static constexpr uint16_t MAX_CHANNEL_CONSUMERS_COUNT