mirror of
https://github.com/orbbec/OrbbecSDK_ROS2.git
synced 2026-09-12 19:20:20 +08:00
add force-ip mode to set_device_ip and isolate it from lla/set-ip paths
This commit is contained in:
@@ -24,22 +24,50 @@ bool parseIpString(const std::string &ip_str, uint8_t ip[4]) {
|
||||
return i == 4;
|
||||
}
|
||||
|
||||
bool isParamProvided(int argc, char **argv, const std::string &key) {
|
||||
const std::string pattern = key + ":=";
|
||||
for (int i = 1; i < argc; ++i) {
|
||||
const std::string arg(argv[i]);
|
||||
if (arg.find(pattern) != std::string::npos) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void printHelp() {
|
||||
std::cout << "Usage:\n"
|
||||
<< " ros2 run orbbec_camera set_device_ip --ros-args [params]\n\n"
|
||||
<< "Parameters:\n"
|
||||
<< " -p old_ip:=<ip> Current device IP (default: 192.168.1.10)\n"
|
||||
<< " -p port:=<port> Device port (default: 8090)\n"
|
||||
<< " -p dhcp:=<bool> Enable DHCP (default: false)\n"
|
||||
<< " -p lla:=<bool> Enable LLA (default: false)\n"
|
||||
<< " -p new_ip:=<ip> New static IP (default: 192.168.1.200)\n"
|
||||
<< " -p mask:=<ip> Subnet mask (default: 255.255.255.0)\n"
|
||||
<< " -p gateway:=<ip> Gateway (default: 192.168.1.1)\n\n"
|
||||
<< " -p old_ip:=<ip> Current device IP (default: 192.168.1.10)\n"
|
||||
<< " -p port:=<port> Device port (default: 8090)\n"
|
||||
<< " -p enable_lla:=<bool> Set LLA switch directly (true: enable, false: disable, default: false)\n"
|
||||
<< " Note: LLA is applied only when this parameter is explicitly provided.\n"
|
||||
<< " -p enable_set_ip:=<bool> Enable set-ip operation (default: false)\n"
|
||||
<< " -p dhcp:=<bool> DHCP flag for set-ip/force-ip config (default: false)\n"
|
||||
<< " -p new_ip:=<ip> Static IP for set-ip/force-ip (default: 192.168.1.200)\n"
|
||||
<< " -p mask:=<ip> Subnet mask for set-ip/force-ip (default: 255.255.255.0)\n"
|
||||
<< " -p gateway:=<ip> Gateway for set-ip/force-ip (default: 192.168.1.1)\n"
|
||||
<< " -p enable_force_ip:=<bool> Enable force-ip operation (default: false)\n"
|
||||
<< " -p force_ip_mac:=<mac> Target MAC for force-ip (optional, e.g. 54:14:FD:06:07:DA)\n\n"
|
||||
<< "Examples:\n"
|
||||
<< " ros2 run orbbec_camera set_device_ip --ros-args "
|
||||
"-p old_ip:=192.168.1.10 -p new_ip:=192.168.1.200\n"
|
||||
<< " ros2 run orbbec_camera set_device_ip --ros-args "
|
||||
"-p old_ip:=192.168.1.10 -p dhcp:=true\n";
|
||||
<< "\n"
|
||||
<< " [LLA]\n"
|
||||
<< " enable: ros2 run orbbec_camera set_device_ip --ros-args -p old_ip:=192.168.1.10 -p enable_lla:=true\n"
|
||||
<< " disable: ros2 run orbbec_camera set_device_ip --ros-args -p old_ip:=192.168.1.10 -p enable_lla:=false\n"
|
||||
<< "\n"
|
||||
<< " [Set IP]\n"
|
||||
<< " DHCP: ros2 run orbbec_camera set_device_ip --ros-args \\\n"
|
||||
<< " -p old_ip:=192.168.1.10 -p enable_set_ip:=true -p dhcp:=true\n"
|
||||
<< " Static: ros2 run orbbec_camera set_device_ip --ros-args \\\n"
|
||||
<< " -p old_ip:=192.168.1.10 -p enable_set_ip:=true -p dhcp:=false \\\n"
|
||||
<< " -p new_ip:=192.168.1.200 -p mask:=255.255.255.0 -p gateway:=192.168.1.1\n"
|
||||
<< "\n"
|
||||
<< " [Force IP]\n"
|
||||
<< " by MAC: ros2 run orbbec_camera set_device_ip --ros-args \\\n"
|
||||
<< " -p enable_force_ip:=true \\\n"
|
||||
<< " -p force_ip_mac:=54:14:FD:06:07:DA -p dhcp:=false \\\n"
|
||||
<< " -p new_ip:=192.168.1.200 -p mask:=255.255.255.0 -p gateway:=192.168.1.1\n";
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
@@ -57,65 +85,155 @@ int main(int argc, char **argv) {
|
||||
|
||||
std::string device_ip_str = node->declare_parameter<std::string>("old_ip", "192.168.1.10");
|
||||
int port = node->declare_parameter<int>("port", 8090);
|
||||
|
||||
bool enable_lla = node->declare_parameter<bool>("enable_lla", false);
|
||||
bool do_lla = isParamProvided(argc, argv, "enable_lla");
|
||||
|
||||
bool enable_set_ip = node->declare_parameter<bool>("enable_set_ip", false);
|
||||
bool dhcp = node->declare_parameter<bool>("dhcp", false);
|
||||
bool lla = node->declare_parameter<bool>("lla", false);
|
||||
std::string new_ip_str = node->declare_parameter<std::string>("new_ip", "192.168.1.200");
|
||||
std::string mask_str = node->declare_parameter<std::string>("mask", "255.255.255.0");
|
||||
std::string gateway_str = node->declare_parameter<std::string>("gateway", "192.168.1.1");
|
||||
|
||||
ob_net_ip_config ip_config{};
|
||||
bool enable_force_ip = node->declare_parameter<bool>("enable_force_ip", false);
|
||||
std::string force_ip_mac = node->declare_parameter<std::string>("force_ip_mac", "");
|
||||
|
||||
if (!do_lla && !enable_set_ip && !enable_force_ip) {
|
||||
RCLCPP_ERROR(logger,
|
||||
"No operation enabled. Please enable at least one of: enable_lla, enable_set_ip, enable_force_ip.");
|
||||
rclcpp::shutdown();
|
||||
return 1;
|
||||
}
|
||||
|
||||
OBNetIpConfig ip_config{};
|
||||
ip_config.dhcp = dhcp ? 1 : 0;
|
||||
|
||||
if (!dhcp) {
|
||||
if ((enable_set_ip || enable_force_ip) && !dhcp) {
|
||||
if (!parseIpString(new_ip_str, ip_config.address)) {
|
||||
RCLCPP_ERROR(logger, "Invalid new_ip format: %s", new_ip_str.c_str());
|
||||
rclcpp::shutdown();
|
||||
return 1;
|
||||
}
|
||||
if (!parseIpString(mask_str, ip_config.mask)) {
|
||||
RCLCPP_ERROR(logger, "Invalid mask format: %s", mask_str.c_str());
|
||||
rclcpp::shutdown();
|
||||
return 1;
|
||||
}
|
||||
if (!parseIpString(gateway_str, ip_config.gateway)) {
|
||||
RCLCPP_ERROR(logger, "Invalid gateway format: %s", gateway_str.c_str());
|
||||
rclcpp::shutdown();
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
RCLCPP_INFO(logger, "Connecting to device %s:%d ...", device_ip_str.c_str(), port);
|
||||
ob::Context::setLoggerSeverity(OBLogSeverity::OB_LOG_SEVERITY_OFF);
|
||||
auto context = std::make_shared<ob::Context>();
|
||||
auto device = context->createNetDevice(device_ip_str.c_str(), port);
|
||||
|
||||
if (device->isPropertySupported(OB_PROP_DEVICE_NETWORK_LLA_BOOL, OB_PERMISSION_READ_WRITE)) {
|
||||
device->setBoolProperty(OB_PROP_DEVICE_NETWORK_LLA_BOOL, lla);
|
||||
RCLCPP_INFO(logger, "LLA mode %s.", lla ? "enabled" : "disabled");
|
||||
} else {
|
||||
RCLCPP_WARN(logger, "LLA property is not supported on this device.");
|
||||
if (do_lla || enable_set_ip) {
|
||||
RCLCPP_INFO(logger, "Connecting to device %s:%d ...", device_ip_str.c_str(), port);
|
||||
auto device = context->createNetDevice(device_ip_str.c_str(), port);
|
||||
|
||||
if (do_lla) {
|
||||
if (device->isPropertySupported(OB_PROP_DEVICE_NETWORK_LLA_BOOL, OB_PERMISSION_READ_WRITE)) {
|
||||
device->setBoolProperty(OB_PROP_DEVICE_NETWORK_LLA_BOOL, enable_lla);
|
||||
RCLCPP_INFO(logger, "LLA set successfully. target=%s", enable_lla ? "enabled" : "disabled");
|
||||
} else {
|
||||
RCLCPP_WARN(logger, "LLA property is not supported on this device.");
|
||||
}
|
||||
} else {
|
||||
RCLCPP_INFO(logger, "LLA operation skipped (enable_lla not explicitly provided).");
|
||||
}
|
||||
|
||||
if (enable_set_ip) {
|
||||
RCLCPP_INFO(logger, "Applying set-ip configuration...");
|
||||
device->setStructuredData(OB_STRUCT_DEVICE_IP_ADDR_CONFIG,
|
||||
reinterpret_cast<const uint8_t *>(&ip_config), sizeof(ip_config));
|
||||
|
||||
RCLCPP_INFO(logger, "Set-ip configuration applied successfully.");
|
||||
if (dhcp) {
|
||||
RCLCPP_INFO(logger, "Set-ip target mode: DHCP.");
|
||||
} else {
|
||||
RCLCPP_INFO(logger, "Set-ip target static IP: %d.%d.%d.%d", ip_config.address[0],
|
||||
ip_config.address[1], ip_config.address[2], ip_config.address[3]);
|
||||
RCLCPP_INFO(logger, "Set-ip target mask: %d.%d.%d.%d", ip_config.mask[0], ip_config.mask[1],
|
||||
ip_config.mask[2], ip_config.mask[3]);
|
||||
RCLCPP_INFO(logger, "Set-ip target gateway: %d.%d.%d.%d", ip_config.gateway[0], ip_config.gateway[1],
|
||||
ip_config.gateway[2], ip_config.gateway[3]);
|
||||
}
|
||||
} else {
|
||||
RCLCPP_INFO(logger, "Set-ip operation skipped (enable_set_ip=false).");
|
||||
}
|
||||
}
|
||||
|
||||
RCLCPP_INFO(logger, "Setting IP configuration...");
|
||||
device->setStructuredData(OB_STRUCT_DEVICE_IP_ADDR_CONFIG,
|
||||
reinterpret_cast<const uint8_t *>(&ip_config), sizeof(ip_config));
|
||||
if (enable_force_ip) {
|
||||
std::string mac = force_ip_mac;
|
||||
if (mac.empty()) {
|
||||
auto list = context->queryDeviceList();
|
||||
size_t ethernet_count = 0;
|
||||
std::string single_ethernet_mac;
|
||||
std::string mac_by_old_ip;
|
||||
|
||||
RCLCPP_INFO(logger, "IP configuration applied successfully.");
|
||||
if (dhcp) {
|
||||
RCLCPP_INFO(logger, "DHCP mode enabled.");
|
||||
} else {
|
||||
RCLCPP_INFO(logger, "Static IP set to %d.%d.%d.%d", ip_config.address[0],
|
||||
ip_config.address[1], ip_config.address[2], ip_config.address[3]);
|
||||
RCLCPP_INFO(logger, "Mask: %d.%d.%d.%d", ip_config.mask[0], ip_config.mask[1],
|
||||
ip_config.mask[2], ip_config.mask[3]);
|
||||
RCLCPP_INFO(logger, "Gateway: %d.%d.%d.%d", ip_config.gateway[0], ip_config.gateway[1],
|
||||
ip_config.gateway[2], ip_config.gateway[3]);
|
||||
for (size_t i = 0; i < list->deviceCount(); ++i) {
|
||||
if (std::string(list->getConnectionType(i)) != "Ethernet") {
|
||||
continue;
|
||||
}
|
||||
++ethernet_count;
|
||||
const std::string cur_mac = list->getUid(i);
|
||||
const std::string cur_ip = list->getIpAddress(i);
|
||||
if (ethernet_count == 1) {
|
||||
single_ethernet_mac = cur_mac;
|
||||
}
|
||||
if (!device_ip_str.empty() && cur_ip == device_ip_str) {
|
||||
mac_by_old_ip = cur_mac;
|
||||
}
|
||||
}
|
||||
|
||||
if (!mac_by_old_ip.empty()) {
|
||||
mac = mac_by_old_ip;
|
||||
RCLCPP_INFO(logger, "force_ip_mac not provided, selected MAC by old_ip(%s): %s",
|
||||
device_ip_str.c_str(), mac.c_str());
|
||||
} else if (ethernet_count == 1) {
|
||||
mac = single_ethernet_mac;
|
||||
RCLCPP_INFO(logger, "force_ip_mac not provided, selected the only Ethernet device MAC: %s",
|
||||
mac.c_str());
|
||||
} else {
|
||||
RCLCPP_ERROR(logger,
|
||||
"force_ip_mac is required when multiple Ethernet devices exist and old_ip does not match any device.");
|
||||
rclcpp::shutdown();
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
RCLCPP_INFO(logger, "Applying force-ip to MAC %s ...", mac.c_str());
|
||||
if (context->forceIp(mac.c_str(), ip_config)) {
|
||||
RCLCPP_INFO(logger, "Force-ip operation applied successfully.");
|
||||
if (dhcp) {
|
||||
RCLCPP_INFO(logger, "Force-ip target mode: DHCP.");
|
||||
} else {
|
||||
RCLCPP_INFO(logger, "Force-ip target static IP: %s", new_ip_str.c_str());
|
||||
RCLCPP_INFO(logger, "Force-ip target mask: %s", mask_str.c_str());
|
||||
RCLCPP_INFO(logger, "Force-ip target gateway: %s", gateway_str.c_str());
|
||||
}
|
||||
} else {
|
||||
RCLCPP_ERROR(logger, "Force-ip failed (SDK returned false).");
|
||||
rclcpp::shutdown();
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
} catch (ob::Error &e) {
|
||||
RCLCPP_ERROR(logger, "set_device_ip: %s", e.getMessage());
|
||||
rclcpp::shutdown();
|
||||
return 1;
|
||||
} catch (const std::exception &e) {
|
||||
RCLCPP_ERROR(logger, "set_device_ip: %s", e.what());
|
||||
rclcpp::shutdown();
|
||||
return 1;
|
||||
} catch (...) {
|
||||
RCLCPP_ERROR(logger, "set_device_ip: unknown error");
|
||||
rclcpp::shutdown();
|
||||
return 1;
|
||||
}
|
||||
|
||||
rclcpp::shutdown();
|
||||
|
||||
Reference in New Issue
Block a user