rcdiscover_force_ip.cc
Go to the documentation of this file.
1 /*
2  * rcdiscover - the network discovery tool for Roboception devices
3  *
4  * Copyright (c) 2018 Roboception GmbH
5  * All rights reserved
6  *
7  * Author: Raphael Schaller
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * 3. Neither the name of the copyright holder nor the names of its contributors
20  * may be used to endorse or promote products derived from this software without
21  * specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 #include "rcdiscover_force_ip.h"
37 
38 #include "cli_utils.h"
39 
40 #include <rcdiscover/utils.h>
41 #include <rcdiscover/force_ip.h>
42 
43 #include <stdexcept>
44 #include <iostream>
45 
46 static void printHelp(std::ostream &os, const std::string &command)
47 {
48  os << "Usage: ";
49  os << command << " [<args>] <IP address> <subnet mask> <default gateway>\n";
50  os << '\n';
51  os << "-h, --help Show this help and exit\n";
52  os << "-f name=<name> Filter by name\n";
53  os << "-f serial=<serial> Filter by serial number\n";
54  os << "-f mac=<mac> Filter by MAC address\n";
55  os << "-f iface=<mac> Filter by interface name\n";
56  os << "-f model=<model> Filter by model name\n";
57  os << "-y Assume 'yes' for all queries\n";
58 }
59 
60 int runForceIP(const std::string &command, int argc, char **argv)
61 {
62  DeviceFilter device_filter{};
63  bool yes = false;
64 
65  int i = 0;
66  while (i < argc)
67  {
68  std::string p = argv[i++];
69 
70  if (p == "-y")
71  {
72  yes = true;
73  }
74  else if (p == "-f")
75  {
76  try
77  {
78  i += parseFilterArguments(argc - i, argv + i, device_filter);
79  }
80  catch (const std::invalid_argument &ex)
81  {
82  std::cerr << ex.what() << std::endl;
83  printHelp(std::cerr, command);
84  return 1;
85  }
86  }
87  else if (p == "-h" || p == "--help")
88  {
89  printHelp(std::cout, command);
90  return 0;
91  }
92  else if (!p.empty() && p[0] == '-')
93  {
94  std::cerr << "Invalid argument: " << p << '\n';
95  printHelp(std::cerr, command);
96  return 1;
97  }
98  else
99  {
100  --i;
101  break;
102  }
103  }
104 
105  if (device_filter.mac.empty() &&
106  device_filter.name.empty() &&
107  device_filter.serial.empty())
108  {
109  std::cerr << "No filter set" << std::endl;
110  printHelp(std::cerr, command);
111  return 1;
112  }
113 
114  if (i + 3 != argc)
115  {
116  std::cerr << "IP address, subnet mask and default gateway must be set" << std::endl;
117  printHelp(std::cerr, command);
118  return 1;
119  }
120 
121  std::array<uint8_t, 4> ip;
122  std::array<uint8_t, 4> mask;
123  std::array<uint8_t, 4> gateway;
124 
125  try
126  {
127  ip = string2ip(argv[i++]);
128  }
129  catch (const std::exception &ex)
130  {
131  std::cerr << "Cannot parse IP address" << std::endl;
132  printHelp(std::cerr, command);
133  return 1;
134  }
135 
136  try
137  {
138  mask = string2ip(argv[i++]);
139  }
140  catch (const std::exception &ex)
141  {
142  std::cerr << "Cannot parse subnet mask" << std::endl;
143  printHelp(std::cerr, command);
144  return 1;
145  }
146 
147  try
148  {
149  gateway = string2ip(argv[i++]);
150  }
151  catch (const std::exception &ex)
152  {
153  std::cerr << "Cannot parse default gateway" << std::endl;
154  printHelp(std::cerr, command);
155  return 1;
156  }
157 
158  const auto devices = discoverWithFilter(device_filter);
159 
160  if (devices.empty())
161  {
162  std::cout << "No device found" << std::endl;
163  return 0;
164  }
165 
166  if (devices.size() > 1)
167  {
168  std::cout << "Found more than one device matching the filter criteria." << std::endl;
169  printDeviceTable(std::cout, devices, true, false, false);
170  return 0;
171  }
172 
173  std::cout << "Setting the IP address of the following device:\n";
174  printDeviceTable(std::cout, devices, true, false, false);
175 
176  if (!yes)
177  {
178  std::cout << "Are you sure? [y/N] ";
179  std::string answer;
180  std::getline(std::cin, answer);
181  if (answer != "y" && answer != "Y")
182  {
183  std::cout << "Cancel" << std::endl;
184  return 0;
185  }
186  }
187 
188  for (const auto &device : devices)
189  {
190  rcdiscover::ForceIP force_ip;
191  force_ip.sendCommand(device.getMAC(),
192  byteArrayToInt(ip),
193  byteArrayToInt(mask),
194  byteArrayToInt(gateway));
195  }
196 
197  std::cout << "Done" << std::endl;
198 
199  return 0;
200 }
void sendCommand(std::uint64_t mac, std::uint32_t ip, std::uint32_t subnet, std::uint32_t gateway)
Send FORCEIP_CMD.
Definition: force_ip.cc:29
Class for sending GigE Vision FORCEIP_CMD to camera.
Definition: force_ip.h:26
std::vector< rcdiscover::DeviceInfo > discoverWithFilter(const DeviceFilter &filter)
Definition: cli_utils.cc:110
void printDeviceTable(std::ostream &oss, const std::vector< rcdiscover::DeviceInfo > &devices, bool print_header, bool iponly, bool serialonly)
Definition: cli_utils.cc:174
MinFittingType< N >::type byteArrayToInt(const std::array< std::uint8_t, N > &a)
Definition: utils.h:145
int runForceIP(const std::string &command, int argc, char **argv)
int parseFilterArguments(int argc, char **argv, DeviceFilter &filter)
Definition: cli_utils.cc:44
std::array< uint8_t, 4 > string2ip(const std::string &ip)
Definition: utils.h:128
static void printHelp(std::ostream &os, const std::string &command)


rcdiscover
Author(s): Heiko Hirschmueller , Raphael Schaller
autogenerated on Sun Apr 18 2021 02:16:32