48 throw std::invalid_argument(
"-f expects an option");
51 const std::string p = argv[0];
53 if (p.compare(0, 5,
"name=") == 0)
55 filter.
name.push_back(p.substr(5));
57 else if (p.compare(0, 7,
"serial=") == 0)
59 filter.
serial.push_back(p.substr(7));
61 else if (p.compare(0, 4,
"mac=") == 0)
63 filter.
mac.push_back(p.substr(4));
65 else if (p.compare(0, 6,
"iface=") == 0)
67 filter.
iface.push_back(p.substr(6));
69 else if (p.compare(0, 6,
"model=") == 0)
71 filter.
model.push_back(p.substr(6));
75 throw std::invalid_argument(
"Unknown option for parameter -f: " + p);
84 const auto matches_filter = [](
const std::string &str,
85 const std::vector<std::string> &filter)
87 return filter.empty() ||
88 std::any_of(filter.begin(), filter.end(),
89 [&str](
const std::string &f)
95 const std::string &name = device_info.
getUserName().empty()
98 if (!matches_filter(name, filter.
name))
return false;
100 if (!matches_filter(serial, filter.
serial))
return false;
102 if (!matches_filter(mac, filter.
mac))
return false;
104 if (!matches_filter(iface, filter.
iface))
return false;
106 if (!matches_filter(model, filter.
model))
return false;
116 std::vector<rcdiscover::DeviceInfo> infos;
120 std::sort(infos.begin(), infos.end());
121 infos.erase(std::unique(infos.begin(), infos.end(),
125 return lhs.
getMAC() == rhs.getMAC() &&
129 std::vector<rcdiscover::DeviceInfo> filtered_devices;
130 for (
const auto &info : infos)
134 filtered_devices.push_back(info);
137 return filtered_devices;
141 const std::vector<std::vector<std::string>> &to_be_printed)
143 std::size_t max_columns = 0;
144 for (
const auto &row : to_be_printed)
146 max_columns = std::max(max_columns, row.size());
149 std::vector<std::size_t> column_width(max_columns, 0);
150 for (
const auto &row : to_be_printed)
152 for (std::size_t col = 0; col < row.size(); ++col)
154 column_width[col] = std::max(column_width[col], row[col].size());
158 for (
const auto &row : to_be_printed)
160 for (std::size_t col = 0; col < row.size(); ++col)
162 std::string s = row[col];
163 if (col < row.size() - 1)
165 s.append(column_width[col] - s.length(),
' ');
175 const std::vector<rcdiscover::DeviceInfo> &devices,
177 bool iponly,
bool serialonly)
179 std::vector<std::vector<std::string>> to_be_printed;
183 to_be_printed.push_back({
"Name",
"Serial Number",
"IP",
"MAC",
"Model",
"Interface(s)"});
187 for (
const auto &info : devices)
191 if (info.getMAC() == last_info->
getMAC() && !iponly && !serialonly)
194 to_be_printed.back().back() +=
"," + info.
getIfaceName();
199 to_be_printed.emplace_back();
200 auto &print = to_be_printed.back();
204 print.push_back(
ip2string(info.getIP()));
208 print.push_back(info.getSerialNumber());
212 const std::string &name = info.getUserName().empty()
213 ? info.getModelName()
214 : info.getUserName();
216 print.push_back(name);
217 print.push_back(info.getSerialNumber());
218 print.push_back(
ip2string(info.getIP()));
220 print.push_back(info.getModelName());
221 print.push_back(info.getIfaceName());
std::vector< rcdiscover::DeviceInfo > discoverWithFilter(const DeviceFilter &filter)
void printDeviceTable(std::ostream &oss, const std::vector< rcdiscover::DeviceInfo > &devices, bool print_header, bool iponly, bool serialonly)
std::vector< std::string > serial
bool wildcardMatch(std::string::const_iterator str_first, std::string::const_iterator str_last, std::string::const_iterator p_first, std::string::const_iterator p_last)
std::string ip2string(const uint32_t ip)
std::vector< std::string > name
bool filterDevice(const rcdiscover::DeviceInfo &device_info, const DeviceFilter &filter)
const std::string & getModelName() const
Returns the model name.
std::vector< std::string > model
std::vector< std::string > iface
void broadcastRequest()
Broadcasts a discovery command request.
void printTable(std::ostream &oss, const std::vector< std::vector< std::string >> &to_be_printed)
const std::string & getSerialNumber() const
Returns the serial number.
std::vector< std::string > mac
std::string mac2string(const uint64_t mac)
int parseFilterArguments(int argc, char **argv, DeviceFilter &filter)
bool getResponse(std::vector< DeviceInfo > &info, int timeout_per_socket=1000)
Returns a discovery response.
uint64_t getMAC() const
Returns 6 bytes with the MAC address of the device.
const std::string & getUserName() const
Returns the user name.
const std::string & getIfaceName() const
Returns the name of the interface on which this device was found.