rcdiscover_discover.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: Heiko Hirschmueller
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_discover.h"
37 
38 #include "cli_utils.h"
39 
40 #include "rcdiscover/discover.h"
41 #include "rcdiscover/deviceinfo.h"
42 #include "rcdiscover/utils.h"
43 
44 #include <string>
45 #include <sstream>
46 #include <iostream>
47 #include <iomanip>
48 #include <cstring>
49 #include <chrono>
50 
51 static void printHelp(std::ostream &os, const std::string &command)
52 {
53  os << command << " [<args>]\n";
54  os << '\n';
55  os << "-h, --help Show this help and exit\n";
56  os << "-f name=<name> Filter by name\n";
57  os << "-f serial=<serial> Filter by serial number\n";
58  os << "-f mac=<mac> Filter by MAC address\n";
59  os << "-f iface=<mac> Filter by interface name\n";
60  os << "-f model=<model> Filter by model name\n";
61  os << "--iponly Show only the IP addresses of discovered sensors\n";
62  os << "--serialonly Show only the serial number of discovered sensors\n";
63 }
64 
65 int runDiscover(const std::string &command, int argc, char **argv)
66 {
67  // interpret command line parameters
68 
69  bool printheader = true;
70  bool iponly = false;
71  bool serialonly = false;
72  DeviceFilter device_filter;
73 
74  int i = 0;
75  while (i < argc)
76  {
77  std::string p = argv[i++];
78 
79  if (p == "-iponly" || p == "--iponly")
80  {
81  iponly = true;
82  printheader = false;
83  }
84  else if (p == "-serialonly" || p == "--serialonly")
85  {
86  serialonly = true;
87  printheader = false;
88  }
89  else if (p == "-f")
90  {
91  try
92  {
93  i += parseFilterArguments(argc - i, argv + i, device_filter);
94  }
95  catch (const std::invalid_argument &ex)
96  {
97  std::cerr << ex.what() << std::endl;
98  return 1;
99  }
100 
101  printheader = false;
102  }
103  else if (p == "-h" || p == "--help")
104  {
105  printHelp(std::cout, command);
106  return 0;
107  }
108  else
109  {
110  std::cerr << "Invalid argument: " << p << '\n';
111  printHelp(std::cerr, command);
112  return 1;
113  }
114  }
115 
116  // broadcast discover request
117 
118  rcdiscover::Discover discover;
119  discover.broadcastRequest();
120 
121  std::vector<rcdiscover::DeviceInfo> infos;
122 
123  // get all responses, sort them and remove multiple entries
124 
125  std::chrono::steady_clock::time_point tstart=std::chrono::steady_clock::now();
126  std::chrono::steady_clock::time_point tend=tstart;
127 
128  while (discover.getResponse(infos, 100) ||
129  std::chrono::duration<double, std::milli>(tend-tstart).count() < 1000)
130  {
131  tend=std::chrono::steady_clock::now();
132  }
133 
134  std::sort(infos.begin(), infos.end());
135  const auto it = std::unique(infos.begin(), infos.end(),
136  [](const rcdiscover::DeviceInfo &lhs,
137  const rcdiscover::DeviceInfo &rhs)
138  {
139  return lhs.getMAC() == rhs.getMAC() &&
140  lhs.getIfaceName() == rhs.getIfaceName();
141  });
142  infos.erase(it, infos.end());
143 
144  // go through all valid entries
145 
146  std::vector<rcdiscover::DeviceInfo> filtered_infos;
147  for (rcdiscover::DeviceInfo &info : infos)
148  {
149  if (!info.isValid())
150  {
151  continue;
152  }
153 
154  // filter as requested
155  if (!filterDevice(info, device_filter)) continue;
156 
157  filtered_infos.push_back(info);
158  }
159 
160  printDeviceTable(std::cout, filtered_infos, printheader, iponly, serialonly);
161 
162  return 0;
163 }
cli_utils.h
rcdiscover::Discover::broadcastRequest
void broadcastRequest()
Broadcasts a discovery command request.
Definition: discover.cc:85
discover.h
rcdiscover_discover.h
filterDevice
bool filterDevice(const rcdiscover::DeviceInfo &device_info, const DeviceFilter &filter)
Definition: cli_utils.cc:87
rcdiscover::Discover::getResponse
bool getResponse(std::vector< DeviceInfo > &info, int timeout_per_socket=1000)
Returns a discovery response.
Definition: discover.cc:107
utils.h
DeviceFilter
Definition: cli_utils.h:45
parseFilterArguments
int parseFilterArguments(int argc, char **argv, DeviceFilter &filter)
Definition: cli_utils.cc:50
runDiscover
int runDiscover(const std::string &command, int argc, char **argv)
Definition: rcdiscover_discover.cc:65
deviceinfo.h
rcdiscover::Discover
Definition: discover.h:50
rcdiscover::DeviceInfo
Definition: deviceinfo.h:46
printDeviceTable
void printDeviceTable(std::ostream &oss, const std::vector< rcdiscover::DeviceInfo > &devices, bool print_header, bool iponly, bool serialonly)
Definition: cli_utils.cc:186
printHelp
static void printHelp(std::ostream &os, const std::string &command)
Definition: rcdiscover_discover.cc:51


rcdiscover
Author(s): Heiko Hirschmueller , Raphael Schaller
autogenerated on Thu Aug 1 2024 02:55:56