Serial port friendly name on Linux

This commit is contained in:
Vojtech Kral 2018-05-21 17:29:26 +02:00
parent 3307dbc0fb
commit ee5a5df2a7

View File

@ -1,9 +1,13 @@
#include "Serial.hpp" #include "Serial.hpp"
#include <algorithm> #include <algorithm>
#include <string>
#include <vector>
#include <fstream>
#include <boost/algorithm/string/predicate.hpp> #include <boost/algorithm/string/predicate.hpp>
#include <boost/filesystem.hpp> #include <boost/filesystem.hpp>
#include <boost/format.hpp>
#if _WIN32 #if _WIN32
#include <Windows.h> #include <Windows.h>
@ -38,6 +42,18 @@ static bool looks_like_printer(const std::string &friendly_name)
return friendly_name.find("Original Prusa") != std::string::npos; return friendly_name.find("Original Prusa") != std::string::npos;
} }
#ifdef __linux__
static std::string get_tty_friendly_name(const std::string &path, const std::string &name)
{
const auto sysfs_product = (boost::format("/sys/class/tty/%1%/device/../product") % name).str();
std::ifstream file(sysfs_product);
std::string product;
std::getline(file, product);
return file.good() ? (boost::format("%1% (%2%)") % product % path).str() : path;
}
#endif
std::vector<SerialPortInfo> scan_serial_ports_extended() std::vector<SerialPortInfo> scan_serial_ports_extended()
{ {
std::vector<SerialPortInfo> output; std::vector<SerialPortInfo> output;
@ -139,10 +155,15 @@ std::vector<SerialPortInfo> scan_serial_ports_extended()
std::string name = dir_entry.path().filename().string(); std::string name = dir_entry.path().filename().string();
for (const char *prefix : prefixes) { for (const char *prefix : prefixes) {
if (boost::starts_with(name, prefix)) { if (boost::starts_with(name, prefix)) {
const auto path = dir_entry.path().string();
SerialPortInfo spi; SerialPortInfo spi;
spi.port = dir_entry.path().string(); spi.port = path;
spi.hardware_id = spi.port; spi.hardware_id = path;
spi.friendly_name = spi.port; #ifdef __linux__
spi.friendly_name = get_tty_friendly_name(path, name);
#else
spi.friendly_name = path;
#endif
output.emplace_back(std::move(spi)); output.emplace_back(std::move(spi));
break; break;
} }