rcdiscover_reconnect.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_reconnect.h"
37 
38 #include "cli_utils.h"
39 
40 #include <rcdiscover/discover.h>
41 #include <rcdiscover/force_ip.h>
42 
43 #include <stdexcept>
44 #include <iostream>
45 #include <algorithm>
46 
47 static void printHelp(std::ostream &os, const std::string &command)
48 {
49  os << "Usage: ";
50  os << command << " [<args>]\n";
51  os << '\n';
52  os << "-h, --help Show this help and exit\n";
53  os << "-f name=<name> Filter by name\n";
54  os << "-f serial=<serial> Filter by serial number\n";
55  os << "-f mac=<mac> Filter by MAC address\n";
56  os << "-f iface=<mac> Filter by interface name\n";
57  os << "-f model=<model> Filter by model name\n";
58  os << "-y Assume 'yes' for all queries\n";
59 }
60 
61 int runReconnect(const std::string &command, int argc, char **argv)
62 {
63  DeviceFilter device_filter{};
64  bool yes = false;
65 
66  int i=0;
67  while (i < argc)
68  {
69  std::string p=argv[i++];
70 
71  if (p == "-y")
72  {
73  yes = true;
74  }
75  else if (p == "-f")
76  {
77  try
78  {
79  i += parseFilterArguments(argc - i, argv + i, device_filter);
80  }
81  catch (const std::invalid_argument &ex)
82  {
83  std::cerr << ex.what() << std::endl;
84  printHelp(std::cerr, command);
85  return 1;
86  }
87  }
88  else if (p == "-h" || p == "--help")
89  {
90  printHelp(std::cout, command);
91  return 0;
92  }
93  else
94  {
95  std::cerr << "Invalid argument: " << p << '\n';
96  printHelp(std::cerr, command);
97  return 1;
98  }
99  }
100 
101  if (device_filter.mac.empty() &&
102  device_filter.name.empty() &&
103  device_filter.serial.empty())
104  {
105  std::cerr << "No filter set" << std::endl;
106  printHelp(std::cerr, command);
107  return 1;
108  }
109 
110  const auto devices = discoverWithFilter(device_filter);
111 
112  if (devices.empty())
113  {
114  std::cout << "No device found" << std::endl;
115  return 0;
116  }
117 
118  std::cout << "Reconnecting the following devices:\n";
119  printDeviceTable(std::cout, devices, true, false, false);
120 
121  if (!yes)
122  {
123  std::cout << "Are you sure? [y/N] ";
124  std::string answer;
125  std::getline(std::cin, answer);
126  if (answer != "y" && answer != "Y")
127  {
128  std::cout << "Cancel" << std::endl;
129  return 0;
130  }
131  }
132 
133  for (const auto &device : devices)
134  {
135  rcdiscover::ForceIP force_ip;
136  force_ip.sendCommand(device.getMAC(), 0, 0, 0);
137  }
138 
139  std::cout << "Done" << std::endl;
140 
141  return 0;
142 }
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
int parseFilterArguments(int argc, char **argv, DeviceFilter &filter)
Definition: cli_utils.cc:44
int runReconnect(const std::string &command, int argc, char **argv)
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