mirror of
https://github.com/PX4/PX4-Autopilot.git
synced 2026-12-02 14:30:59 +08:00
fix(zenoh): wait for interface readiness before opening a session
Zenoh can enter a blocking session open before its NuttX network interface has finished starting. Check interface readiness once at startup, with a bounded wait that honors stop requests, while keeping the existing session retry loop. Initialize the session handle for safe early cleanup and retain atomic connection status. Have getNetworkConfig() initialize and terminate its output strings so both callers receive valid C strings. Assisted-by: Claude:claude-fable-5-1 Assisted-by: Codex:gpt-6-astra Signed-off-by: YG_Kim <yeogyeom1@naver.com>
This commit is contained in:
1 parent
c4e4ef98e9
commit
82d49a0392
3 files changed
+100
-8
No files matched your search
@@ -44,6 +44,13 @@
|
||||
#include <ctype.h>
|
||||
#include <string.h>
|
||||
|
||||
#ifdef __PX4_NUTTX
|
||||
#include <net/if.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/socket.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
#include <zenoh-pico.h>
|
||||
|
||||
// CycloneDDS CDR Deserializer
|
||||
@@ -86,7 +93,7 @@ void toCamelCase(char *input)
|
||||
ZENOH::ZENOH():
|
||||
ModuleParams(nullptr)
|
||||
{
|
||||
|
||||
z_internal_null(&_s);
|
||||
}
|
||||
|
||||
ZENOH::~ZENOH()
|
||||
@@ -213,6 +220,71 @@ int ZENOH::generate_rmw_zenoh_topic_liveliness_keyexpr(const z_id_t *id, const c
|
||||
#endif
|
||||
}
|
||||
|
||||
bool ZENOH::waitForLink(const char *locator)
|
||||
{
|
||||
#ifdef __PX4_NUTTX
|
||||
|
||||
// Only IP transports and scouting use the network interface.
|
||||
if (locator[0] != '\0' && strncmp(locator, "tcp/", 4) != 0 && strncmp(locator, "udp/", 4) != 0) {
|
||||
return !should_exit();
|
||||
}
|
||||
|
||||
ifreq req {};
|
||||
strncpy(req.ifr_name, "eth0", sizeof(req.ifr_name) - 1);
|
||||
|
||||
// Locator configuration is a semicolon-separated list after '#'.
|
||||
for (const char *entry = strchr(locator, '#'); entry; entry = strchr(entry, ';')) {
|
||||
entry++;
|
||||
|
||||
if (strncmp(entry, "iface=", 6) == 0) {
|
||||
entry += 6;
|
||||
const size_t len = strcspn(entry, ";");
|
||||
|
||||
if (len == 0 || len >= sizeof(req.ifr_name)) {
|
||||
return !should_exit();
|
||||
}
|
||||
|
||||
memcpy(req.ifr_name, entry, len);
|
||||
req.ifr_name[len] = '\0';
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
const int fd = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
|
||||
if (fd < 0) {
|
||||
PX4_WARN("%s: cannot check the link (%d), opening the session anyway", req.ifr_name, errno);
|
||||
return !should_exit();
|
||||
}
|
||||
|
||||
constexpr unsigned max_retries = 50;
|
||||
constexpr useconds_t retry_interval_us = 100000; // 100 ms, up to 5 s total
|
||||
constexpr unsigned link_flags = IFF_UP | IFF_RUNNING;
|
||||
bool link_up = false;
|
||||
|
||||
for (unsigned retry = 0; retry < max_retries && !should_exit(); retry++) {
|
||||
if (ioctl(fd, SIOCGIFFLAGS, &req) >= 0 && (req.ifr_flags & link_flags) == link_flags) {
|
||||
link_up = true;
|
||||
break;
|
||||
}
|
||||
|
||||
px4_usleep(retry_interval_us);
|
||||
}
|
||||
|
||||
close(fd);
|
||||
|
||||
if (!link_up && !should_exit()) {
|
||||
// Driver flags do not guarantee router readiness. Still attempt the session on timeout.
|
||||
PX4_WARN("%s: link wait timed out, opening the session anyway", req.ifr_name);
|
||||
}
|
||||
|
||||
#else
|
||||
(void)locator;
|
||||
#endif
|
||||
|
||||
return !should_exit();
|
||||
}
|
||||
|
||||
int ZENOH::setupSession()
|
||||
{
|
||||
char mode[NET_MODE_SIZE];
|
||||
@@ -222,6 +294,10 @@ int ZENOH::setupSession()
|
||||
|
||||
_config.getNetworkConfig(mode, locator);
|
||||
|
||||
if (!waitForLink(locator)) {
|
||||
return -EINTR;
|
||||
}
|
||||
|
||||
PX4_INFO("Opening session...");
|
||||
|
||||
do {
|
||||
@@ -470,7 +546,7 @@ void ZENOH::cleanupSession()
|
||||
z_drop(z_session_move(&_s));
|
||||
}
|
||||
|
||||
connected = false;
|
||||
_connected.store(false);
|
||||
}
|
||||
|
||||
void ZENOH::run()
|
||||
@@ -481,14 +557,19 @@ void ZENOH::run()
|
||||
_sub_count = _config.getSubCount();
|
||||
px4_pollfd_struct_t pfds[_pub_count];
|
||||
|
||||
if (setupSession() < 0) {
|
||||
PX4_ERR("Failed to setup Zenoh session");
|
||||
const int setup_ret = setupSession();
|
||||
|
||||
if (setup_ret < 0) {
|
||||
if (setup_ret != -EINTR) {
|
||||
PX4_ERR("Failed to setup Zenoh session");
|
||||
}
|
||||
|
||||
cleanupSession();
|
||||
exit_and_cleanup(desc);
|
||||
return;
|
||||
}
|
||||
|
||||
connected = true;
|
||||
_connected.store(true);
|
||||
|
||||
PX4_INFO("Starting reading/writing tasks...");
|
||||
|
||||
@@ -580,7 +661,7 @@ Zenoh demo bridge
|
||||
|
||||
int ZENOH::print_status()
|
||||
{
|
||||
if (connected) {
|
||||
if (_connected.load()) {
|
||||
PX4_INFO("Connected");
|
||||
|
||||
} else {
|
||||
|
||||
@@ -41,6 +41,7 @@
|
||||
|
||||
#include <px4_platform_common/module_params.h>
|
||||
#include <px4_platform_common/module.h>
|
||||
#include <px4_platform_common/atomic.h>
|
||||
#include <perf/perf_counter.h>
|
||||
#include <uORB/Publication.hpp>
|
||||
#include <uORB/topics/parameter_update.h>
|
||||
@@ -103,6 +104,9 @@ private:
|
||||
int setupTopics(px4_pollfd_struct_t *pfds);
|
||||
void cleanupSession();
|
||||
|
||||
// Wait up to 5 s for NuttX interface flags; return false on a stop request.
|
||||
bool waitForLink(const char *locator);
|
||||
|
||||
Zenoh_Config _config;
|
||||
|
||||
int _pub_count;
|
||||
@@ -111,7 +115,8 @@ private:
|
||||
Zenoh_Subscriber **_zenoh_subscribers = nullptr;
|
||||
|
||||
z_owned_session_t _s;
|
||||
bool connected = false;
|
||||
// written by the module task, read by print_status() from the shell task
|
||||
px4::atomic_bool _connected{false};
|
||||
|
||||
px4_guid_t _px4_guid{};
|
||||
|
||||
|
||||
@@ -433,8 +433,11 @@ int Zenoh_Config::parse_csv_line(char *line, const char **fields, int max_fields
|
||||
}
|
||||
void Zenoh_Config::getNetworkConfig(char *mode, char *locator)
|
||||
{
|
||||
mode[0] = '\0';
|
||||
locator[0] = '\0';
|
||||
|
||||
FILE *fp;
|
||||
char buffer[NET_CONFIG_LINE_SIZE];
|
||||
char buffer[NET_CONFIG_LINE_SIZE] {};
|
||||
|
||||
fp = fopen(ZENOH_NET_CONFIG_PATH, "r");
|
||||
|
||||
@@ -455,6 +458,7 @@ void Zenoh_Config::getNetworkConfig(char *mode, char *locator)
|
||||
if (config_mode) {
|
||||
config_mode[strcspn(config_mode, "\n")] = 0;
|
||||
strncpy(mode, config_mode, NET_MODE_SIZE);
|
||||
mode[NET_MODE_SIZE - 1] = '\0';
|
||||
|
||||
} else {
|
||||
mode[0] = 0;
|
||||
@@ -463,6 +467,7 @@ void Zenoh_Config::getNetworkConfig(char *mode, char *locator)
|
||||
if (nfields >= 2) {
|
||||
const char *config_locator = fields[1];
|
||||
strncpy(locator, config_locator, NET_LOCATOR_SIZE);
|
||||
locator[NET_LOCATOR_SIZE - 1] = '\0';
|
||||
|
||||
} else {
|
||||
locator[0] = 0;
|
||||
@@ -470,6 +475,7 @@ void Zenoh_Config::getNetworkConfig(char *mode, char *locator)
|
||||
|
||||
} else {
|
||||
printf("Failed to open the file\n");
|
||||
return;
|
||||
}
|
||||
|
||||
//Close the file
|
||||
|
||||
Reference in new issue
Block a user