CARLA
Loading...
Searching...
No Matches
MiddlewareConfig.h
Go to the documentation of this file.
1// Copyright (c) 2026 Computer Vision Center (CVC) at the Universitat Autonoma de Barcelona (UAB).
2// This work is licensed under the terms of the MIT license.
3// For a copy, see <https://opensource.org/licenses/MIT>.
4
5#pragma once
6
7#include <cstdlib>
8
9namespace carla {
10namespace ros2 {
11
12/// Sentinel domain id meaning "no domain id was configured on the command line";
13/// each middleware falls back to its own native default.
14constexpr int kUnsetDomainId = -1;
15
16/// Lowest valid ROS 2 domain id.
17constexpr int kMinDomainId = 0;
18
19/// Highest valid ROS 2 domain id (RTPS maximum).
20constexpr int kMaxDomainId = 232;
21
22/// Domain id used when neither the command line nor the environment provides one.
23constexpr int kDefaultDomainId = 0;
24
25/// Name of the standard ROS 2 environment variable that selects the domain id.
26constexpr char kRosDomainIdEnvVar[] = "ROS_DOMAIN_ID";
27
28/// @return true if @a id is a valid ROS 2 domain id (0..232).
29/// The unset sentinel is intentionally NOT valid: callers test for it explicitly.
30inline bool IsValidDomainId(int id) {
31 return id >= kMinDomainId && id <= kMaxDomainId;
32}
33
34/// Where the effective domain id came from, in priority order.
35enum class DomainIdSource {
36 CommandLine, ///< From the --ros-domain-id command line option.
37 Environment, ///< From the ROS_DOMAIN_ID environment variable.
38 Default ///< Neither was set; the default domain (0) is used.
39};
40
41/// The resolved domain id together with the source it came from.
46
47/// Parse a ROS_DOMAIN_ID-style string into a valid domain id.
48/// @return true and writes @a out only when @a text is a base-10 integer that is
49/// a valid domain id (0..232). nullptr, empty, non-numeric, trailing-garbage and
50/// out-of-range inputs return false and leave @a out untouched.
51inline bool TryParseDomainId(const char* text, int& out) {
52 if (text == nullptr) {
53 return false;
54 }
55 char* end = nullptr;
56 const long value = std::strtol(text, &end, 10);
57 if (end == text) {
58 return false; // No digits were consumed.
59 }
60 while (*end == ' ' || *end == '\t' || *end == '\n' || *end == '\r') {
61 ++end;
62 }
63 if (*end != '\0') {
64 return false; // Trailing non-numeric characters.
65 }
66 if (value < kMinDomainId || value > kMaxDomainId) {
67 return false;
68 }
69 out = static_cast<int>(value);
70 return true;
71}
72
73/// Resolve the effective domain id from a command line value and an environment
74/// value, in priority order:
75/// 1. @a cli_value, when it is a valid domain id (0..232).
76/// 2. @a env_value (e.g. ROS_DOMAIN_ID), when it parses to a valid domain id.
77/// 3. kDefaultDomainId (0).
78/// This function is pure (no environment access), so the precedence rules can be
79/// unit-tested by passing the environment value as a string.
80inline ResolvedDomainId ResolveDomainId(int cli_value, const char* env_value) {
81 if (IsValidDomainId(cli_value)) {
82 return {cli_value, DomainIdSource::CommandLine};
83 }
84 int parsed = 0;
85 if (TryParseDomainId(env_value, parsed)) {
86 return {parsed, DomainIdSource::Environment};
87 }
89}
90
91/// Process-wide runtime configuration shared by every ROS 2 middleware
92/// implementation (FastDDS, CycloneDDS, and future ones such as Zenoh).
93///
94/// This belongs to the middleware abstraction layer, not to any single vendor:
95/// each concrete middleware reads this configuration when it establishes its
96/// transport context (a DDS DomainParticipant, a Zenoh session, ...) and
97/// translates the abstract values into its own mechanism, mapping
98/// kUnsetDomainId to its native default.
99///
100/// Set once at startup (via ROS2::Enable) before any publisher or subscriber is
101/// created. Reads after that point are stable. Because it is set before any
102/// entity exists, no synchronisation is required.
104 public:
105 /// Configure the ROS 2 domain id for all subsequent transport contexts.
106 /// Pass kUnsetDomainId to keep each middleware's native default.
107 static void SetDomainId(int id) {
108 Storage().domain_id = id;
109 }
110
111 /// @return The configured domain id, or kUnsetDomainId if none was set.
112 static int GetDomainId() {
113 return Storage().domain_id;
114 }
115
116 /// Resolve the effective domain id and its source from the configured value,
117 /// the ROS_DOMAIN_ID environment variable, and the default. See ResolveDomainId
118 /// for the priority order.
122
123 /// @return The effective domain id every middleware should use, after applying
124 /// the command-line value, the ROS_DOMAIN_ID environment variable and the
125 /// default in that order.
126 static int GetEffectiveDomainId() {
127 return ResolveEffective().id;
128 }
129
130 private:
131 struct Data {
133 };
134
135 /// Function-local static: a single instance shared across all translation units.
136 static Data& Storage() {
137 static Data data;
138 return data;
139 }
140};
141
142} // namespace ros2
143} // namespace carla
Process-wide runtime configuration shared by every ROS 2 middleware implementation (FastDDS,...
static ResolvedDomainId ResolveEffective()
Resolve the effective domain id and its source from the configured value, the ROS_DOMAIN_ID environme...
static void SetDomainId(int id)
Configure the ROS 2 domain id for all subsequent transport contexts.
static Data & Storage()
Function-local static: a single instance shared across all translation units.
bool IsValidDomainId(int id)
constexpr int kMinDomainId
Lowest valid ROS 2 domain id.
constexpr int kDefaultDomainId
Domain id used when neither the command line nor the environment provides one.
constexpr int kMaxDomainId
Highest valid ROS 2 domain id (RTPS maximum).
ResolvedDomainId ResolveDomainId(int cli_value, const char *env_value)
Resolve the effective domain id from a command line value and an environment value,...
constexpr int kUnsetDomainId
Sentinel domain id meaning "no domain id was configured on the command line"; each middleware falls b...
bool TryParseDomainId(const char *text, int &out)
Parse a ROS_DOMAIN_ID-style string into a valid domain id.
DomainIdSource
Where the effective domain id came from, in priority order.
@ Environment
From the ROS_DOMAIN_ID environment variable.
@ Default
Neither was set; the default domain (0) is used.
@ CommandLine
From the –ros-domain-id command line option.
constexpr char kRosDomainIdEnvVar[]
Name of the standard ROS 2 environment variable that selects the domain id.
This file contains definitions of common data structures used in traffic manager.
The resolved domain id together with the source it came from.