feat: add device changed callback registration and unregistration in OBCameraNodeDriver

This commit is contained in:
ob-yalian
2025-12-15 14:24:25 +08:00
parent bc408a244c
commit db77882d35
2 changed files with 19 additions and 5 deletions
@@ -93,6 +93,7 @@ class OBCameraNodeDriver : public rclcpp::Node {
std::string config_path_; std::string config_path_;
std::unique_ptr<ob::Context> ctx_ = nullptr; std::unique_ptr<ob::Context> ctx_ = nullptr;
rclcpp::Logger logger_; rclcpp::Logger logger_;
uint64_t device_changed_callback_id_ = 0; // Store callback ID for unregistering (OBCallbackId)
std::unique_ptr<OBCameraNode> ob_camera_node_ = nullptr; std::unique_ptr<OBCameraNode> ob_camera_node_ = nullptr;
std::unique_ptr<orbbec_lidar::OBLidarNode> ob_lidar_node_ = nullptr; std::unique_ptr<orbbec_lidar::OBLidarNode> ob_lidar_node_ = nullptr;
std::shared_ptr<ob::Device> device_ = nullptr; std::shared_ptr<ob::Device> device_ = nullptr;
+18 -5
View File
@@ -165,6 +165,18 @@ OBCameraNodeDriver::~OBCameraNodeDriver() {
if (ob_camera_node_) { if (ob_camera_node_) {
ob_camera_node_->stopGmslTrigger(); ob_camera_node_->stopGmslTrigger();
} }
// Unregister device changed callback before destroying context
if (ctx_ && device_changed_callback_id_ != 0) {
try {
ctx_->unregisterDeviceChangedCallback(device_changed_callback_id_);
device_changed_callback_id_ = 0;
} catch (...) {
RCLCPP_WARN_STREAM(logger_,
"Exception during device changed callback unregister in destructor");
}
}
if (orb_device_lock_shm_fd_ != -1) { if (orb_device_lock_shm_fd_ != -1) {
close(orb_device_lock_shm_fd_); close(orb_device_lock_shm_fd_);
orb_device_lock_shm_fd_ = -1; orb_device_lock_shm_fd_ = -1;
@@ -271,11 +283,12 @@ void OBCameraNodeDriver::init() {
RCLCPP_INFO_STREAM(logger_, "setUvcBackendType:" << uvc_backend_); RCLCPP_INFO_STREAM(logger_, "setUvcBackendType:" << uvc_backend_);
} }
ctx_->enableNetDeviceEnumeration(enumerate_net_device_); ctx_->enableNetDeviceEnumeration(enumerate_net_device_);
ctx_->setDeviceChangedCallback([this](const std::shared_ptr<ob::DeviceList> &removed_list, device_changed_callback_id_ = ctx_->registerDeviceChangedCallback(
const std::shared_ptr<ob::DeviceList> &added_list) { [this](const std::shared_ptr<ob::DeviceList> &removed_list,
onDeviceDisconnected(removed_list); const std::shared_ptr<ob::DeviceList> &added_list) {
onDeviceConnected(added_list); onDeviceDisconnected(removed_list);
}); onDeviceConnected(added_list);
});
check_connect_timer_ = check_connect_timer_ =
this->create_wall_timer(std::chrono::milliseconds(1000), [this]() { checkConnectTimer(); }); this->create_wall_timer(std::chrono::milliseconds(1000), [this]() { checkConnectTimer(); });
CHECK_NOTNULL(check_connect_timer_); CHECK_NOTNULL(check_connect_timer_);