31#ifndef DW_FRAMEWORK_CHANNEL_PARAMETERS_HPP_
32#define DW_FRAMEWORK_CHANNEL_PARAMETERS_HPP_
34#include <dwcgf/Types.hpp>
36#include <dw/core/base/Types.h>
37#include <dw/core/container/BaseString.hpp>
38#include <dw/core/container/VectorFixed.hpp>
39#include <dw/core/system/NvMediaExt.h>
51 DW_CHANNEL_TYPE_SHMEM_LOCAL = 0,
52 DW_CHANNEL_TYPE_SHMEM_REMOTE = 1,
53 DW_CHANNEL_TYPE_EGLSTREAM = 2,
54 DW_CHANNEL_TYPE_SOCKET = 3,
55 DW_CHANNEL_TYPE_DDS = 4,
56 DW_CHANNEL_TYPE_NVSCI = 5,
64 case DW_CHANNEL_TYPE_SHMEM_LOCAL:
65 result =
"type=SHMEM_LOCAL";
67 case DW_CHANNEL_TYPE_SHMEM_REMOTE:
68 result =
"type=SHMEM_REMOTE";
70 case DW_CHANNEL_TYPE_EGLSTREAM:
71 result =
"type=EGLSTREAM";
73 case DW_CHANNEL_TYPE_SOCKET:
74 result =
"type=SOCKET";
76 case DW_CHANNEL_TYPE_DDS:
79 case DW_CHANNEL_TYPE_NVSCI:
80 result =
"type=NVSCI";
177 std::size_t start = 0;
182 end = value.find(
"|", start);
184 ChannelParamStr valueSubString = (end != dw::core::FixedString<1>::NPOS) ? value.substr(start, end - start) : value.substr(start, value.length() - start);
186 if (valueSubString ==
"CPU")
190 else if (valueSubString ==
"EGL")
194 else if (valueSubString ==
"CUDA")
198 else if (valueSubString ==
"PVA")
202 else if (valueSubString ==
"DLA")
206 else if (valueSubString ==
"NVMEDIA")
212 throw Exception(DW_INVALID_ARGUMENT,
"ChannelNvSciStreamParams: enabledComponents sub-value unrecongnized: ", valueSubString);
216 }
while (end != dw::core::FixedString<1>::NPOS);
225 if (value ==
"dynamic")
229 else if (value ==
"static")
235 throw Exception(DW_INVALID_ARGUMENT,
"ChannelNvSciStreamParams: connection phase value unrecongnized: ", value);
244 if (value ==
"thread")
248 else if (value ==
"process")
252 else if (value ==
"vm")
256 else if (value ==
"chip")
262 throw Exception(DW_INVALID_ARGUMENT,
"ChannelNvSciStreamParams: reach value unrecongnized: ", value);
271 auto translatedSize = strtol(value.c_str(),
nullptr, 10);
272 return translatedSize;
278 auto translatedSize = ParseChannelParameter<int64_t>(value);
279 if (translatedSize < 0)
281 throw Exception(DW_INVALID_ARGUMENT,
"ParseChannelParameter: size_t is negative");
283 size_t result =
static_cast<size_t>(translatedSize);
290 auto translatedSize = ParseChannelParameter<size_t>(value);
291 uint32_t result =
static_cast<uint32_t
>(translatedSize);
298 auto translatedSize = ParseChannelParameter<size_t>(value);
299 if (translatedSize > 0xFFFF)
301 throw Exception(DW_INVALID_ARGUMENT,
"ChannelSocketParams: port is larger than uint16_t allows!");
303 uint16_t result =
static_cast<uint16_t
>(translatedSize);
311 if ((value ==
"true") || (value ==
"1"))
315 else if ((value ==
"false") || (value ==
"0"))
321 throw Exception(DW_INVALID_ARGUMENT,
"ParseChannelParameter: needs to be 'true' or 'false' or 1/0");
330 if (value ==
"producer")
334 else if (value ==
"consumer")
338 else if (value ==
"composite")
344 throw Exception(DW_INVALID_ARGUMENT,
"ParseChannelParameter: role unknown!");
353 if (value ==
"SHMEM_LOCAL")
355 result = ChannelType::DW_CHANNEL_TYPE_SHMEM_LOCAL;
357 else if (value ==
"SHMEM_REMOTE")
359 result = ChannelType::DW_CHANNEL_TYPE_SHMEM_REMOTE;
361 else if (value ==
"EGLSTREAM")
363 result = ChannelType::DW_CHANNEL_TYPE_EGLSTREAM;
365 else if (value ==
"SOCKET")
367 result = ChannelType::DW_CHANNEL_TYPE_SOCKET;
369 else if (value ==
"DDS")
371 result = ChannelType::DW_CHANNEL_TYPE_DDS;
373 else if (value ==
"NVSCI")
375 result = ChannelType::DW_CHANNEL_TYPE_NVSCI;
379 throw Exception(DW_INVALID_ARGUMENT,
"ParseChannelParameter: type unknown!");
389 if (value ==
"mailbox")
393 else if (value ==
"singleton")
399 throw Exception(DW_INVALID_ARGUMENT,
"ParseChannelParameter: ChannelMode unknown!");
407 result = ParseChannelParameter<T>(value);
410template <
size_t Size>
416template <
typename T,
size_t N>
424 endpos = value.find(
":", pos);
425 done = endpos == dw::core::FixedString<1>::NPOS;
426 size_t count = done ? endpos : endpos - pos;
429 result.push_back(entry);
439template <
typename T,
typename... Others>
442 if (key == staticKey)
450template <
typename... Others>
454 std::size_t pos = channelParams.find(
"=");
455 std::size_t value = 0;
456 std::size_t valueEnd = 0;
460 while (pos != dw::core::FixedString<1>::NPOS && value != dw::core::FixedString<1>::NPOS)
462 keyString = channelParams.substr(key, pos - key);
463 value = channelParams.find(
",", pos);
464 valueEnd = (value == dw::core::FixedString<1>::NPOS) ? (channelParams.length() - (pos + 1)) : (value - pos - 1);
465 valueString = channelParams.substr(pos + 1, valueEnd);
469 pos = channelParams.find(
"=", key);
478 bool producerFifo =
false,
479 uint16_t numBlockingConnections = 1)
481 std::stringstream ss;
482 ss.flags(std::ios::dec);
484 if (serverIP !=
nullptr)
491 ss <<
",producer-fifo=";
492 ss << static_cast<uint32_t>(producerFifo);
493 ss <<
",num-clients=";
494 ss << numBlockingConnections;
502 dw::core::FixedString<MAX_CHANNEL_ALL_PARAMS_SIZE> channelParams(params);
505 "producer-fifo", m_producerFifo,
520 bool m_producerFifo =
false;
533 dw::core::FixedString<MAX_CHANNEL_ALL_PARAMS_SIZE> channelParams(params);
561 static_assert(
decltype(
m_streamNames)::CAPACITY_AT_COMPILE_TIME < std::numeric_limits<uint16_t>::max(),
"ChannelNvSciStreamParams: number of outputs over limit");
574 throw Exception(DW_INVALID_ARGUMENT,
"ChannelNvSciStreamParams: stream name index out of range");
587 throw Exception(DW_INVALID_ARGUMENT,
"ChannelNvSciStreamParams: reach index out of range");
608 "fifo-size", m_fifoSize,
610 "singleton-id", m_singletonId,
611 "trace-name", m_traceId,
614 "reuse", m_reuseEnabled,
615 "debug-port", m_debugPort,
616 "num-clients", m_clientsCount,
617 "debug-num-clients", m_debugClientsCount,
620 "data-offset", m_dataOffset,
621 "strict", m_strictFifo,
622 "sync-enabled", m_syncEnabled);
624 adjustPoolCapacity();
626 if (m_clientsCount == 0)
631 if (!m_singletonId.empty())
638 m_mailboxMode =
true;
642 m_singletonMode =
true;
645 if (!m_id.empty() && m_singletonId.empty())
647 m_singletonId = m_id;
658 case DW_CHANNEL_TYPE_SHMEM_LOCAL:
660 case DW_CHANNEL_TYPE_SOCKET:
663 case DW_CHANNEL_TYPE_NVSCI:
666 case DW_CHANNEL_TYPE_SHMEM_REMOTE:
667 case DW_CHANNEL_TYPE_EGLSTREAM:
668 case DW_CHANNEL_TYPE_DDS:
670 throw Exception(DW_NOT_IMPLEMENTED,
"ChannelParams: no parameters for channel type");
683 const char*
getStr()
const {
return m_str.c_str(); }
695 m_strictFifo = strictFifo;
720 if (m_type != ChannelType::DW_CHANNEL_TYPE_SOCKET)
722 throw Exception(DW_CALL_NOT_ALLOWED,
"ChannelParams: getSocketParams: channel is not of type SOCKET");
724 return m_socketParams;
729 if (m_type != ChannelType::DW_CHANNEL_TYPE_NVSCI)
731 throw Exception(DW_CALL_NOT_ALLOWED,
"ChannelParams: getNvSciStreamParams: channel is not of type NVSCI");
733 return m_nvSciStreamParams;
737 void ValidateMailbox()
743 throw Exception(DW_INVALID_ARGUMENT,
"ChannelParams: Singleton and mailbox modes are incompatible with a fifo setting other than 1");
746 if (!m_mailboxMode && m_reuseEnabled)
748 throw Exception(DW_INVALID_ARGUMENT,
"ChannelParams: reuse=true specified when mode!=mailbox. Not valid");
750 if (m_mailboxMode && m_singletonMode)
752 throw Exception(DW_INVALID_ARGUMENT,
"ChannelParams: Singleton mode is incompatible mailbox mode");
756 void ValidateSingleton()
759 if (m_singletonMode && m_singletonId.empty())
761 m_singletonId = m_id;
763 if (!m_singletonMode && !m_singletonId.empty())
765 throw Exception(DW_INVALID_ARGUMENT,
"ChannelParams: Singleton mode requires both the mode set AND singletonId set");
767 if (m_singletonMode && (m_type != ChannelType::DW_CHANNEL_TYPE_SHMEM_LOCAL))
769 throw Exception(DW_INVALID_ARGUMENT,
"ChannelParams: Singleton mode is only valid for SHMEM_LOCAL channels");
773 void adjustPoolCapacity()
795 size_t m_fifoSize = 1;
804 uint32_t m_dataOffset = 0;
805 bool m_syncEnabled =
false;
806 bool m_mailboxMode =
false;
807 bool m_singletonMode =
false;
808 bool m_reuseEnabled =
false;
810 ChannelType m_type = ChannelType::DW_CHANNEL_TYPE_SHMEM_LOCAL;
811 bool m_strictFifo =
true;
813 uint16_t m_clientsCount = 1;
814 uint16_t m_debugClientsCount = 1;
816 uint16_t m_debugPort = 0;
818 ChannelSocketParams m_socketParams{};
819 ChannelNvSciStreamParams m_nvSciStreamParams{};
bool isEnabledComponentPva() const
bool isEnabledComponentCpu() const
ChannelNvSciStreamParams & operator=(const ChannelNvSciStreamParams &other)=default
bool isEnabledComponentDla() const
ChannelConnectionType m_connectionType
dw::core::VectorFixed< ChannelReach, 8 > m_reaches
ChannelNvSciStreamEnabledComponents getEnabledComponents() const
ChannelNvSciStreamParams(const char *params)
bool isConnectionTypeDynamic() const
bool isEnabledComponentCuda() const
ChannelNvSciStreamParams(const ChannelNvSciStreamParams &other)=default
const char * getStreamName(uint16_t index=0) const
bool isConnectionTypeStatic() const
ChannelNvSciStreamEnabledComponents m_enabledComponents
ChannelConnectionType getChannelConnectionType() const
ChannelNvSciStreamParams()=default
uint16_t getNumOutputs() const
dw::core::VectorFixed< dw::core::FixedString< 64 >, 8 > m_streamNames
dwTime_t getTimeoutUsec() const
bool isEnabledComponentEgl() const
bool isEnabledComponentNvmedia() const
ChannelReach getChannelReach(uint16_t index=0) 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
bool getSyncEnabled() const
const ChannelNvSciStreamParams & getNvSciStreamParams() const
bool getDebugMode() const
ChannelParamStr getSingletonId() const
ChannelParamStr getTraceId() 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)
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
@ DW_CHANNEL_ROLE_CONSUMER
@ DW_CHANNEL_ROLE_UNKNOWN
@ DW_CHANNEL_ROLE_COMPOSITE
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