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/HashContainer.hpp>
37#include <dwshared/dwfoundation/dw/core/container/StringView.hpp>
38#include <dwshared/dwfoundation/dw/core/container/VectorFixed.hpp>
39#include <dwshared/dwfoundation/dw/core/language/cxx23.hpp>
40#include <dwshared/dwfoundation/dw/core/safety/Safety.hpp>
41#include <dwshared/dwsockets/SocketClientServer.hpp>
42#include <dw/core/system/NvMediaExt.h>
74 const char* value{
"type=SHMEM_LOCAL"};
79 const char* value{
"type=SHMEM_REMOTE"};
84 const char* value{
"type=EGLSTREAM"};
89 const char* value{
"type=SOCKET"};
94 const char* value{
"type=DDS"};
99 const char* value{
"type=NVSCI"};
104 const char* value{
"type=FSI"};
108 dw::core::unreachable();
228 return dw::core::safeStrtol(value.c_str(),
nullptr, 10);
236 int64_t translatedSize{ParseChannelParameter<int64_t>(value)};
238 if (translatedSize < 0)
240 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"ParseChannelParameter: size_t is negative");
242 size_t result{
static_cast<size_t>(translatedSize)};
251 size_t translatedSize{ParseChannelParameter<size_t>(value)};
252 if (translatedSize > std::numeric_limits<uint32_t>::max())
254 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"ParseChannelParameter: value is larger than uint32_t allows");
256 uint32_t result{
static_cast<uint32_t
>(translatedSize)};
265 size_t translatedSize{ParseChannelParameter<size_t>(value)};
266 if (translatedSize > std::numeric_limits<uint16_t>::max())
268 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"ParseChannelParameter: port is larger than uint16_t allows!");
270 uint16_t result{
static_cast<uint16_t
>(translatedSize)};
279 size_t translatedSize{ParseChannelParameter<size_t>(value)};
280 if (translatedSize > std::numeric_limits<uint8_t>::max())
282 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"ParseChannelParameter: port is larger than uint8_t allows!");
284 uint8_t result{
static_cast<uint8_t
>(translatedSize)};
294 static const dw::core::StaticHashMap<ChannelParamStr, bool, 4> MAPPING{
300 if (!MAPPING.contains(value))
302 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"ParseChannelParameter: needs to be 'true' or 'false' or 1/0");
304 return MAPPING.at(value);
313 static const dw::core::StaticHashMap<ChannelParamStr, ChannelRole, 3> MAPPING{
320 if (!MAPPING.contains(value))
322 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"ParseChannelParameter: role unknown!");
324 return MAPPING.at(value);
333 static const dw::core::StaticHashMap<ChannelParamStr, ChannelType, 7> MAPPING{
344 if (!MAPPING.contains(value))
346 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"ParseChannelParameter: type unknown!");
348 return MAPPING.at(value);
357 static const dw::core::StaticHashMap<ChannelParamStr, ChannelMode, 2> MAPPING{
363 if (!MAPPING.contains(value))
365 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"ParseChannelParameter: ChannelMode unknown!");
367 return MAPPING.at(value);
376 size_t pos{value.find(
".")};
382 ChannelParamStr second{value.substr(dw::core::safeAdd(pos, 1U).value())};
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{dw::core::FixedString<1>::NPOS == endpos};
416 size_t count{done ? endpos : endpos - pos};
419 result.push_back(entry);
437template <
typename T,
typename... Others>
442 if (dw::core::StringView(key.data(), key.size()) == 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 (dw::core::FixedString<1>::NPOS != pos && dw::core::FixedString<1>::NPOS != value)
469 keyString = channelParams.substr(key, dw::core::safeSub(pos, key).value());
471 value = channelParams.find(
",", pos);
472 if (dw::core::FixedString<1>::NPOS == value)
477 valueEnd = dw::core::safeSub(channelParams.length(), dw::core::safeAdd(pos, 1U).value()).value();
483 valueEnd = dw::core::safeSub(dw::core::safeSub(value, pos).value(), 1U).value();
486 valueString = channelParams.substr(pos + 1U, valueEnd);
492 pos = channelParams.find(
"=", key);
501 bool producerFifo =
false,
502 uint16_t numBlockingConnections = 1U,
503 dw::core::FixedString<8>
const sockPrefix = dw::core::FixedString<8>())
505 std::stringstream ss{};
506 ss.flags(std::ios::dec);
508 if (
nullptr != serverIP)
515 ss <<
",producer-fifo=";
517 ss << (producerFifo ? 1U : 0U);
518 ss <<
",num-clients=";
519 ss << numBlockingConnections;
520 ss <<
",sock-prefix=";
529 dw::core::FixedString<MAX_CHANNEL_ALL_PARAMS_SIZE> channelParams{params};
531 dw::core::StringView{
"ip"}, m_serverIP,
532 dw::core::StringView{
"producer-fifo"}, m_producerFifo,
533 dw::core::StringView{
"id"}, m_port,
534 dw::core::StringView{
"connect-timeout"}, m_connectTimeout,
535 dw::core::StringView{
"sock-prefix"}, m_sockPrefix);
546 dwshared::socketipc::SockPrefixStr
getSockPrefix()
const {
return m_sockPrefix; }
551 bool m_producerFifo{
false};
552 dwTime_t m_connectTimeout{DW_TIME_INVALID};
553 dwshared::socketipc::SockPrefixStr m_sockPrefix{};
566 dw::core::FixedString<MAX_CHANNEL_ALL_PARAMS_SIZE> channelParams{params};
569 dw::core::StringView{
"limits"},
m_limits,
572 dw::core::StringView{
"late-locs"},
m_lateLocs);
581 static_assert(
decltype(
m_streamNames)::CAPACITY_AT_COMPILE_TIME < std::numeric_limits<size_t>::max(),
"ChannelNvSciStreamParams: number of outputs over limit");
597 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"ChannelNvSciStreamParams: stream name index out of range");
615 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"ChannelNvSciStreamParams: limiter maxPackets index out of range");
630 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"ChannelNvSciStreamParams: connect prio index out of range");
636 dw::core::VectorFixed<ChannelStreamNameStr, MAX_CHANNEL_STREAM_NAMES>
m_streamNames{};
638 dw::core::VectorFixed<int64_t, MAX_CHANNEL_STREAM_NAMES>
m_limits{};
651 dw::core::StringView{
"compat-vendor"}, m_compatVendor,
652 dw::core::StringView{
"compat-app"}, m_compatApp,
653 dw::core::StringView{
"num-channel"}, m_numChannel);
654 m_compat = m_compatVendor;
655 m_compat += dw::core::StringView{
","};
656 m_compat += m_compatApp;
663 const char*
getCompat()
const {
return m_compat.c_str(); }
670 uint8_t m_numChannel{0U};
683 dw::core::StringView{
"fifo-size"}, m_fifoSize,
684 dw::core::StringView{
"id"}, m_id,
685 dw::core::StringView{
"uid"}, m_uid,
686 dw::core::StringView{
"connect-group-id"}, m_connectGroupID,
687 dw::core::StringView{
"singleton-id"}, m_singletonId,
688 dw::core::StringView{
"mode"}, m_mode,
689 dw::core::StringView{
"reuse"}, m_reuseEnabled,
690 dw::core::StringView{
"debug-port"}, m_debugPort,
691 dw::core::StringView{
"num-clients"}, m_clientsCount,
692 dw::core::StringView{
"debug-num-clients"}, m_debugClientsCount,
693 dw::core::StringView{
"role"}, m_role,
694 dw::core::StringView{
"type"}, m_type,
695 dw::core::StringView{
"data-offset"}, m_dataOffset,
696 dw::core::StringView{
"strict"}, m_strictFifo,
697 dw::core::StringView{
"sync-enabled"}, m_syncEnabled,
698 dw::core::StringView{
"name"}, m_name,
699 dw::core::StringView{
"producer-fifo"}, m_producerFifo,
700 dw::core::StringView{
"sync-object-id"}, m_syncObjectId);
701 adjustPoolCapacity();
704 if (0U == m_clientsCount)
710 if (!m_singletonId.empty())
717 m_mailboxMode =
true;
721 m_singletonMode =
true;
724 if (!m_id.empty() && m_singletonId.empty())
726 m_singletonId = m_id;
752 throw ExceptionWithStatus(DW_NOT_IMPLEMENTED,
"ChannelParams: no parameters for channel type");
761 const char*
getStr()
const {
return m_str.c_str(); }
776 m_strictFifo = strictFifo;
798 uint32_t
getUID()
const {
return m_uid; }
808 throw ExceptionWithStatus(DW_CALL_NOT_ALLOWED,
"ChannelParams: getSocketParams: channel is not of type SOCKET");
810 return m_socketParams;
817 throw ExceptionWithStatus(DW_CALL_NOT_ALLOWED,
"ChannelParams: getNvSciStreamParams: channel is not of type NVSCI");
819 return m_nvSciStreamParams;
826 throw ExceptionWithStatus(DW_CALL_NOT_ALLOWED,
"ChannelParams: getFSIParams: channel is not of type FSI");
832 void ValidateMailbox()
837 if (1U != m_fifoSize)
839 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"ChannelParams: Singleton and mailbox modes are incompatible with a fifo setting other than 1");
842 if (!m_mailboxMode && m_reuseEnabled)
844 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"ChannelParams: reuse=true specified when mode!=mailbox. Not valid");
846 if (m_mailboxMode && m_singletonMode)
848 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"ChannelParams: Singleton mode is incompatible mailbox mode");
852 void ValidateSingleton()
855 if (m_singletonMode && m_singletonId.empty())
857 m_singletonId = m_id;
859 if (!m_singletonMode && !m_singletonId.empty())
861 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"ChannelParams: Singleton mode requires both the mode set AND singletonId set");
865 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"ChannelParams: Singleton mode is only valid for SHMEM_LOCAL channels");
869 void adjustPoolCapacity()
894 size_t m_fifoSize{1U};
902 bool m_producerFifo{
false};
904 uint32_t m_dataOffset{0U};
906 uint32_t m_connectGroupID{0U};
907 bool m_syncEnabled{
false};
908 bool m_mailboxMode{
false};
909 bool m_singletonMode{
false};
910 bool m_reuseEnabled{
false};
913 bool m_strictFifo{
true};
915 uint16_t m_clientsCount{1U};
916 uint16_t m_debugClientsCount{1U};
918 uint16_t m_debugPort{0U};
920 ChannelSocketParams m_socketParams{};
921 ChannelNvSciStreamParams m_nvSciStreamParams{};
922 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
uint32_t getConnectPrio(size_t index=0U) const
ChannelNvSciStreamParams & operator=(const ChannelNvSciStreamParams &other)=default
size_t getLocalClientCount() const
ChannelNvSciStreamParams(const char *params)
dw::core::VectorFixed< ChannelStreamNameStr, MAX_CHANNEL_STREAM_NAMES > m_streamNames
int64_t getLimiterMaxPackets(size_t index=0U) const
dw::core::VectorFixed< ChannelPeerLocation, 32 > m_lateLocs
dw::core::VectorFixed< uint32_t, MAX_CHANNEL_STREAM_NAMES > m_connectPrios
size_t m_localClientCount
ChannelNvSciStreamParams(const ChannelNvSciStreamParams &other)=default
dw::core::span< ChannelPeerLocation const > getLateLocs() const
size_t getNumOutputs() const
dw::core::VectorFixed< int64_t, MAX_CHANNEL_STREAM_NAMES > m_limits
ChannelNvSciStreamParams()=default
ChannelStreamNameStr getStreamName(size_t index=0U) const
ChannelParams(const ChannelParams &other)=default
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
void setFifoSize(size_t fifoSize)
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
const ChannelParamStr & getSyncObjectId() const
uint16_t getDebugPort() const
void setStrictFifo(bool strictFifo)
ChannelRole getRole() const
bool getMailboxMode() const
ChannelParamStr getId() const
bool hasProducerFifo() 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
ChannelNvSciStreamEnabledComponents
static constexpr uint16_t MAX_CHANNEL_STREAM_NAMES
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
const char * ToParam(ChannelType channelType)
@ 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