CARLA
Loading...
Searching...
No Matches
ZenohSharedSession.cpp
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#ifndef CARLA_ROS2_MIDDLEWARE_TESTING
6#ifdef CARLA_ROS2_MIDDLEWARE_ZENOH
7
10#include "carla/Logging.h"
11
12#include <dlfcn.h>
13
14namespace carla {
15namespace ros2 {
16
17namespace {
18
19std::string compute_zid_string(const z_loaned_session_t* session) {
20 z_id_t zid = z_info_zid(session);
21 z_owned_string_t s;
22 z_id_to_string(&zid, &s);
23 const z_loaned_string_t* ls = z_loan(s);
24 std::string result(z_string_data(ls), z_string_len(ls));
25 z_drop(z_move(s));
26 return result;
27}
28
29// Resolve the shipped default config relative to this shared library. The
30// build stages zenoh_session_config.json5 next to libcarla-ros2-native.so
31// (both in the plugin Binaries/Linux and in the packaged tree), so dladdr on
32// a symbol we own gives the directory to look in. This works in both editor
33// and packaged runs, unlike __FILE__ (which points at a build-machine source
34// path that does not exist after packaging).
35std::string default_config_path() {
36 Dl_info info;
37 if (dladdr(reinterpret_cast<const void*>(&zenoh_get_shared_session), &info) &&
38 info.dli_fname != nullptr) {
39 const std::string so{info.dli_fname};
40 const size_t slash = so.find_last_of('/');
41 if (slash != std::string::npos) {
42 return so.substr(0, slash) + "/zenoh_session_config.json5";
43 }
44 }
45 // Last resort: look in the current working directory.
46 return "zenoh_session_config.json5";
47}
48
49// rmw_zenoh-compatible: ZENOH_CONFIG_OVERRIDE='k1/k2=v1;k3=v2' patches
50// individual keys on top of the loaded config.
51void apply_config_overrides(z_loaned_config_t* cfg) {
52 const char* env = std::getenv("ZENOH_CONFIG_OVERRIDE");
53 if (env == nullptr || env[0] == '\0') return;
54
55 std::string s(env);
56 size_t pos = 0;
57 while (pos < s.size()) {
58 size_t semi = s.find(';', pos);
59 if (semi == std::string::npos) semi = s.size();
60 std::string pair = s.substr(pos, semi - pos);
61 size_t eq = pair.find('=');
62 if (eq != std::string::npos) {
63 std::string key = pair.substr(0, eq);
64 std::string val = pair.substr(eq + 1);
65 if (zc_config_insert_json5(cfg, key.c_str(), val.c_str()) != Z_OK) {
66 log_warning("ZENOH_CONFIG_OVERRIDE: failed to apply '", pair, "'");
67 }
68 }
69 pos = semi + 1;
70 }
71}
72
73} // namespace
74
75const z_loaned_session_t* zenoh_get_shared_session() {
76 static z_owned_session_t session = [] {
77 z_owned_config_t cfg;
78 // rmw_zenoh-compatible: ZENOH_SESSION_CONFIG_URI swaps the whole file;
79 // otherwise use the shipped default sitting next to this .cpp.
80 const char* uri = std::getenv("ZENOH_SESSION_CONFIG_URI");
81 const std::string path = (uri != nullptr && uri[0] != '\0')
82 ? std::string(uri) : default_config_path();
83 if (zc_config_from_file(&cfg, path.c_str()) != Z_OK) {
84 log_error("ZenohSharedSession: failed to read config at '", path,
85 "'; falling back to zenoh defaults");
86 z_config_default(&cfg);
87 }
88 apply_config_overrides(z_loan_mut(cfg));
89
90 z_owned_session_t s;
91 if (z_open(&s, z_move(cfg), nullptr) != Z_OK) {
92 log_error("ZenohSharedSession: z_open failed");
93 }
94 return s;
95 }();
96
97 // The function-local static keeps the carla_bridge node liveliness token
98 // alive for the process lifetime so rmw_zenoh peers continue to see the
99 // node in their graph cache for as long as the session is up.
100 static z_owned_liveliness_token_t node_token = [] {
101 const z_loaned_session_t* loaned = z_session_loan(&session);
102 const std::string ke_str = zenoh_make_node_liveliness_keyexpr(
103 zenoh_ros_domain_id(), compute_zid_string(loaned));
104 z_view_keyexpr_t ke;
105 z_view_keyexpr_from_str(&ke, ke_str.c_str());
106 z_owned_liveliness_token_t tok;
107 if (z_liveliness_declare_token(loaned, &tok, z_loan(ke), nullptr) != Z_OK) {
108 log_error("ZenohSharedSession: z_liveliness_declare_token failed");
109 }
110 return tok;
111 }();
112 (void)node_token;
113
114 return z_session_loan(&session);
115}
116
117const std::string& zenoh_session_zid() {
118 static const std::string cached =
119 compute_zid_string(zenoh_get_shared_session());
120 return cached;
121}
122
123} // namespace ros2
124} // namespace carla
125
126#endif // CARLA_ROS2_MIDDLEWARE_ZENOH
127#endif // !CARLA_ROS2_MIDDLEWARE_TESTING
This file contains definitions of common data structures used in traffic manager.
static void log_error(Args &&... args)
Definition Logging.h:110
static void log_warning(Args &&... args)
Definition Logging.h:96