LibMultiSense/ChangeIpUtility/ChangeIpUtility.cc
Go to the documentation of this file.
1 
37 #ifdef WIN32
38 #ifndef WIN32_LEAN_AND_MEAN
39 #define WIN32_LEAN_AND_MEAN 1
40 #endif
41 
42 #include <windows.h>
43 #include <winsock2.h>
44 #else
45 #include <unistd.h>
46 #endif
47 
48 #include <chrono>
49 #include <csignal>
50 #include <filesystem>
51 #include <fstream>
52 #include <iostream>
53 #include <optional>
54 #include <string.h>
55 
56 #include <MultiSense/MultiSenseChannel.hh>
58 
59 #include "getopt/getopt.h"
60 
61 namespace lms = multisense;
62 
63 namespace
64 {
65 
66 void usage(const char *name)
67 {
68  std::cerr << "USAGE: " << name << " -e <extrinsics-file> -i <intrinsics-file> [<options>]" << std::endl;
69  std::cerr << "Where <options> are:" << std::endl;
70  std::cerr << "\t-a <current-address> : CURRENT IPV4 address (default=10.66.171.21)" << std::endl;
71  std::cerr << "\t-A <new-address> : NEW IPV4 address (default=10.66.171.21)" << std::endl;
72  std::cerr << "\t-G <new-gateway> : NEW IPV4 gateway (default=10.66.171.1)" << std::endl;
73  std::cerr << "\t-N <new-netmask> : NEW IPV4 netmask (default=255.255.255.0)" << std::endl;
74  std::cerr << "\t-b <interface> : send broadcast packet to a specified network interface."
75  "This resets the IP address to the new configured IP" << std::endl;
76  std::cerr << "\t-y : Disable confirmation prompt (default=false)" << std::endl;
77  exit(1);
78 }
79 
80 }
81 
82 int main(int argc, char** argv)
83 {
84  std::string ip_address = "10.66.171.21";
85  std::string new_ip_address = "10.66.171.21";
86  std::string new_gateway = "10.66.171.1";
87  std::string new_netmask = "255.255.255.0";
88  std::optional<std::string> interface = std::nullopt;
89  bool disable_confirmation = false;
90 
91  int c;
92  while(-1 != (c = getopt(argc, argv, "a:A:G:N:b:y")))
93  {
94  switch(c)
95  {
96  case 'a': ip_address = std::string(optarg); break;
97  case 'A': new_ip_address = std::string(optarg); break;
98  case 'G': new_gateway = std::string(optarg); break;
99  case 'N': new_netmask = std::string(optarg); break;
100  case 'b': interface = std::string(optarg); break;
101  case 'y': disable_confirmation = true; break;
102  default: usage(*argv); break;
103  }
104  }
105 
106  lms::Channel::Config config{ip_address};
107  config.connect_on_initialization = static_cast<bool>(interface);
108  const auto channel = lms::Channel::create(config);
109 
110  if (!channel)
111  {
112  std::cerr << "Failed to create channel" << std::endl;
113  return 1;
114  }
115 
116  if (!disable_confirmation)
117  {
118  std::cout << "NEW address: " << new_ip_address << std::endl;;
119  std::cout << "NEW gateway: " << new_gateway << std::endl;;
120  std::cout << "NEW netmask: " << new_netmask << std::endl;;
121 
122  if(interface)
123  {
124  std::cout << "** WARNING: All MultiSense devices attached to interface " << interface.value() <<
125  "will have their addresses changed **" << std::endl;
126  }
127 
128  std::cerr << "Really update network configuration? (y/n):" << std::endl;
129 
130  int reply = getchar();
131  if ('Y' != reply && 'y' != reply)
132  {
133  std::cout << "Aborting" << std::endl;
134  return 1;
135  }
136  }
137 
138  const lms::MultiSenseInfo::NetworkInfo new_info{new_ip_address, new_gateway, new_netmask};
139 
140  if (const auto status = channel->set_network_config(new_info, interface); status != lms::Status::OK)
141  {
142  std::cerr << "Unable to set new IP address: " << lms::to_string(status) << std::endl;
143  return 1;
144  }
145 
146  return 0;
147 }
usage
static void usage()
Definition: FirmwareUpdateUtility.cc:51
main
int main(int argc, char **argv)
Definition: LibMultiSense/ChangeIpUtility/ChangeIpUtility.cc:82
getopt.h
getopt
int getopt(int argc, char **argv, char *opts)
Definition: getopt.c:31
MultiSenseUtilities.hh
multisense::Channel::create
static std::unique_ptr< Channel > create(const Config &config, const ChannelImplementation &impl=ChannelImplementation::LEGACY)
Factory create function which allows for switching between different channel implementations.
Definition: factory.cc:42
multisense
Definition: factory.cc:39
optarg
char * optarg
Definition: getopt.c:29
multisense::to_string
std::string to_string(const Status &status)
Convert a status object to a user readable string.
Definition: utilities.cc:137


multisense_lib
Author(s):
autogenerated on Thu Apr 17 2025 02:49:08