feat: add DHCP support to IP configuration tool and update command-line options

This commit is contained in:
ob-yalian
2026-04-03 21:45:42 +08:00
parent 23ee217d41
commit 8f3e2dbef4
+98 -56
View File
@@ -14,12 +14,14 @@ using namespace ob;
struct CliArgs {
enum class Operation {
NONE,
DHCP,
SET_IP,
FORCE_IP,
};
bool help = false;
Operation operation = Operation::NONE;
bool dhcp = false;
bool dhcp_option_set = false;
std::string force_ip_mac;
std::string old_ip = "192.168.1.10";
int port = 8090;
@@ -75,19 +77,22 @@ void printHelp() {
std::cout
<< "Usage:\n"
<< " ros2 run orbbec_camera ip_config_tool --\\\n"
<< " <set_ip|force_ip> [options]\n"
<< " ros2 run orbbec_camera ip_config_tool -- set_ip [--enable_dhcp <true|false>]\\\n"
<< " <dhcp|set_ip|force_ip> [options]\n"
<< " ros2 run orbbec_camera ip_config_tool -- dhcp --enable_dhcp <true|false>\\\n"
<< " [--old_ip <ip>] [--port <port>]\n"
<< " ros2 run orbbec_camera ip_config_tool -- set_ip\\\n"
<< " [--old_ip <ip>] [--port <port>] [--new_ip <ip>] [--mask <ip>] [--gateway <ip>]\n"
<< " ros2 run orbbec_camera ip_config_tool -- force_ip --force_ip_mac <mac>\\\n"
<< " [--enable_dhcp <true|false>] [--new_ip <ip>] [--mask <ip>] [--gateway <ip>]\n"
<< " (legacy alias: set_device_ip)\n\n"
<< "Subcommands:\n"
<< " set_ip Configure IP on device by current device address.\n"
<< " dhcp Configure DHCP on device by current device address.\n"
<< " set_ip Configure static IP on device by current device address.\n"
<< " force_ip Force IP by MAC address.\n\n"
<< "Parameters:\n"
<< " --enable_dhcp <bool> DHCP flag for set-ip/force-ip (default: false).\n"
<< " --old_ip <ip> Current device IP for set-ip (default: 192.168.1.10).\n"
<< " --port <port> Device port for set-ip (default: 8090).\n"
<< " --enable_dhcp <bool> DHCP flag for dhcp/force-ip (default: false).\n"
<< " --old_ip <ip> Current device IP for dhcp/set-ip (default: 192.168.1.10).\n"
<< " --port <port> Device port for dhcp/set-ip (default: 8090).\n"
<< " --new_ip <ip> Static IP for set-ip/force-ip (default: 192.168.1.200).\n"
<< " --mask <ip> Subnet mask for set-ip/force-ip (default: 255.255.255.0).\n"
<< " --gateway <ip> Gateway for set-ip/force-ip (default: 192.168.1.1).\n"
@@ -95,15 +100,15 @@ void printHelp() {
"54:14:FD:06:07:DA).\n\n"
<< "Examples:\n"
<< "\n"
<< " [Set IP]\n"
<< " DHCP: ros2 run orbbec_camera ip_config_tool -- \\\n"
<< " set_ip \\\n"
<< " [DHCP]\n"
<< " Enable: ros2 run orbbec_camera ip_config_tool -- \\\n"
<< " dhcp \\\n"
<< " --old_ip 192.168.1.10 \\\n"
<< " --enable_dhcp true\n"
<< " [Set IP]\n"
<< " Static: ros2 run orbbec_camera ip_config_tool -- \\\n"
<< " set_ip \\\n"
<< " --old_ip 192.168.1.10 \\\n"
<< " --enable_dhcp false \\\n"
<< " --new_ip 192.168.1.200 \\\n"
<< " --mask 255.255.255.0 \\\n"
<< " --gateway 192.168.1.1\n"
@@ -126,9 +131,17 @@ bool parseArgs(int argc, char **argv, CliArgs &args, std::string &error) {
args.help = true;
return true;
}
if (current == "dhcp") {
if (args.operation != CliArgs::Operation::NONE) {
error = "Only one subcommand is allowed: dhcp, set_ip or force_ip";
return false;
}
args.operation = CliArgs::Operation::DHCP;
continue;
}
if (current == "set_ip" || current == "set-ip") {
if (args.operation != CliArgs::Operation::NONE) {
error = "Only one subcommand is allowed: set_ip or force_ip";
error = "Only one subcommand is allowed: dhcp, set_ip or force_ip";
return false;
}
args.operation = CliArgs::Operation::SET_IP;
@@ -136,7 +149,7 @@ bool parseArgs(int argc, char **argv, CliArgs &args, std::string &error) {
}
if (current == "force_ip" || current == "force-ip") {
if (args.operation != CliArgs::Operation::NONE) {
error = "Only one subcommand is allowed: set_ip or force_ip";
error = "Only one subcommand is allowed: dhcp, set_ip or force_ip";
return false;
}
args.operation = CliArgs::Operation::FORCE_IP;
@@ -148,6 +161,7 @@ bool parseArgs(int argc, char **argv, CliArgs &args, std::string &error) {
error = "--enable_dhcp expects true/false";
return false;
}
args.dhcp_option_set = true;
continue;
}
if (current == "--enable_dhcp") {
@@ -155,6 +169,7 @@ bool parseArgs(int argc, char **argv, CliArgs &args, std::string &error) {
error = "--enable_dhcp expects true/false";
return false;
}
args.dhcp_option_set = true;
continue;
}
@@ -243,7 +258,15 @@ bool parseArgs(int argc, char **argv, CliArgs &args, std::string &error) {
}
if (args.operation == CliArgs::Operation::NONE) {
error = "Missing subcommand. Use one of: set_ip, force_ip";
error = "Missing subcommand. Use one of: dhcp, set_ip, force_ip";
return false;
}
if (args.operation == CliArgs::Operation::DHCP && !args.dhcp_option_set) {
error = "dhcp requires --enable_dhcp <true|false>";
return false;
}
if (args.operation == CliArgs::Operation::SET_IP && args.dhcp_option_set) {
error = "set_ip only supports static IP now. Use subcommand 'dhcp' for --enable_dhcp";
return false;
}
if (args.operation == CliArgs::Operation::FORCE_IP && args.force_ip_mac.empty()) {
@@ -274,6 +297,39 @@ int main(int argc, char **argv) {
ob::Context::setLoggerSeverity(OBLogSeverity::OB_LOG_SEVERITY_OFF);
auto context = std::make_shared<ob::Context>();
if (args.operation == CliArgs::Operation::DHCP) {
RCLCPP_INFO(logger, "Connecting to device %s:%d ...", args.old_ip.c_str(), args.port);
auto device = context->createNetDevice(args.old_ip.c_str(), args.port);
const bool v2_supported =
device->isPropertySupported(OB_STRUCT_DEVICE_IP_ADDR_CONFIG_V2, OB_PERMISSION_READ_WRITE);
if (v2_supported) {
OBNetIpConfigV2 ip_config_v2{};
if (args.dhcp) {
ip_config_v2.flags = OB_NET_IP_FLAG_DHCP;
} else {
ip_config_v2.flags = OB_NET_IP_FLAG_PERSISTENT;
}
RCLCPP_INFO(logger, "Applying dhcp configuration with V2 property (1088)...");
device->setStructuredData(OB_STRUCT_DEVICE_IP_ADDR_CONFIG_V2,
reinterpret_cast<const uint8_t *>(&ip_config_v2),
sizeof(ip_config_v2));
RCLCPP_INFO(logger, "DHCP configuration applied successfully (V2).");
} else {
OBNetIpConfig ip_config{};
ip_config.dhcp = args.dhcp ? 1 : 0;
RCLCPP_WARN(logger,
"Device does not support IP config V2 (1088), fallback to legacy property (1041).");
RCLCPP_INFO(logger, "Applying dhcp configuration...");
device->setStructuredData(OB_STRUCT_DEVICE_IP_ADDR_CONFIG,
reinterpret_cast<const uint8_t *>(&ip_config), sizeof(ip_config));
RCLCPP_INFO(logger, "DHCP configuration applied successfully.");
}
RCLCPP_INFO(logger, "DHCP target state: %s", args.dhcp ? "enabled" : "disabled");
}
if (args.operation == CliArgs::Operation::SET_IP) {
RCLCPP_INFO(logger, "Connecting to device %s:%d ...", args.old_ip.c_str(), args.port);
auto device = context->createNetDevice(args.old_ip.c_str(), args.port);
@@ -304,9 +360,6 @@ int main(int argc, char **argv) {
OBNetIpConfigV2 ip_config_v2{};
ip_config_v2.flags = OB_NET_IP_FLAG_PERSISTENT;
if (args.dhcp) {
ip_config_v2.flags = static_cast<uint16_t>(ip_config_v2.flags | OB_NET_IP_FLAG_DHCP);
}
std::memcpy(ip_config_v2.address, address, sizeof(address));
std::memcpy(ip_config_v2.mask, mask, sizeof(mask));
std::memcpy(ip_config_v2.gateway, gateway, sizeof(gateway));
@@ -317,8 +370,7 @@ int main(int argc, char **argv) {
sizeof(ip_config_v2));
RCLCPP_INFO(logger, "Set-ip configuration applied successfully (V2).");
RCLCPP_INFO(logger, "Set-ip target DHCP: %s", args.dhcp ? "enabled" : "disabled");
RCLCPP_INFO(logger, "Set-ip target persistent(static): enabled");
RCLCPP_INFO(logger, "Set-ip target mode: static.");
RCLCPP_INFO(logger, "Set-ip target static IP: %d.%d.%d.%d", ip_config_v2.address[0],
ip_config_v2.address[1], ip_config_v2.address[2], ip_config_v2.address[3]);
RCLCPP_INFO(logger, "Set-ip target mask: %d.%d.%d.%d", ip_config_v2.mask[0],
@@ -327,54 +379,44 @@ int main(int argc, char **argv) {
ip_config_v2.gateway[1], ip_config_v2.gateway[2], ip_config_v2.gateway[3]);
} else {
OBNetIpConfig ip_config{};
ip_config.dhcp = args.dhcp ? 1 : 0;
if (!args.dhcp) {
uint8_t address[4] = {0};
uint8_t mask[4] = {0};
uint8_t gateway[4] = {0};
if (!parseIpString(args.new_ip, address)) {
RCLCPP_ERROR(logger, "Invalid new_ip format: %s", args.new_ip.c_str());
rclcpp::shutdown();
return 1;
}
if (!parseIpString(args.mask, mask)) {
RCLCPP_ERROR(logger, "Invalid mask format: %s", args.mask.c_str());
rclcpp::shutdown();
return 1;
}
if (!parseIpString(args.gateway, gateway)) {
RCLCPP_ERROR(logger, "Invalid gateway format: %s", args.gateway.c_str());
rclcpp::shutdown();
return 1;
}
std::memcpy(ip_config.address, address, sizeof(address));
std::memcpy(ip_config.mask, mask, sizeof(mask));
std::memcpy(ip_config.gateway, gateway, sizeof(gateway));
ip_config.dhcp = 0;
uint8_t address[4] = {0};
uint8_t mask[4] = {0};
uint8_t gateway[4] = {0};
if (!parseIpString(args.new_ip, address)) {
RCLCPP_ERROR(logger, "Invalid new_ip format: %s", args.new_ip.c_str());
rclcpp::shutdown();
return 1;
}
if (!parseIpString(args.mask, mask)) {
RCLCPP_ERROR(logger, "Invalid mask format: %s", args.mask.c_str());
rclcpp::shutdown();
return 1;
}
if (!parseIpString(args.gateway, gateway)) {
RCLCPP_ERROR(logger, "Invalid gateway format: %s", args.gateway.c_str());
rclcpp::shutdown();
return 1;
}
std::memcpy(ip_config.address, address, sizeof(address));
std::memcpy(ip_config.mask, mask, sizeof(mask));
std::memcpy(ip_config.gateway, gateway, sizeof(gateway));
RCLCPP_WARN(logger,
"Device does not support IP config V2 (1088), fallback to legacy property (1041).");
if (args.dhcp) {
RCLCPP_WARN(logger,
"Legacy IP config does not support DHCP and static IP simultaneously; static fields will be ignored.");
}
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 (args.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]);
}
RCLCPP_INFO(logger, "Set-ip target mode: static.");
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]);
}
}