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


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