29#pragma GCC diagnostic push
30#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
32#pragma GCC diagnostic pop
35#include <unordered_set>
39#include "androidUTIL.h"
50#include <wx/msw/winundef.h>
53#ifdef OCPN_USE_NEWSERIAL
65#ifdef HAVE_LINUX_SERIAL_H
66#include <linux/serial.h>
69#ifdef HAVE_SYS_IOCTL_H
77#ifdef HAVE_SYS_FCNTL_H
81#ifdef HAVE_SYS_TYPES_H
91#include <linux/serial.h>
104#include <wx/arrstr.h>
112DEFINE_GUID(GARMIN_DETECT_GUID, 0x2c9c45c2L, 0x8e7d, 0x4c08, 0xa1, 0x2d, 0x81,
113 0x6b, 0xba, 0xe7, 0x22, 0xc0);
118#ifndef GUID_CLASS_COMPORT
119DEFINE_GUID(GUID_CLASS_COMPORT, 0x86e0d1e0L, 0x8089, 0x11d0, 0x9c, 0xe4, 0x08,
120 0x00, 0x3e, 0x30, 0x1f, 0x73);
127 device_data(
const std::string& p,
const std::string& i) : info(i), path(p) {}
133 symlink(
const std::string& p,
const std::string& t) : path(p), target(t) {}
137static int isTTYreal(
const char* dev) {
138 if (strncmp(
"/dev/tty0", dev, 9) == 0)
return 1;
139 if (strncmp(
"/dev/ttyU", dev, 9) == 0)
return 1;
140 if (strcmp(
"/dev/gps", dev) == 0)
return 1;
144#elif defined(HAVE_LINUX_SERIAL_H) && defined(HAVE_SYS_STAT_H)
147static std::string device_path(
const char* dev) {
148 if (strstr(dev,
"/sysfs/") != 0)
return std::string(dev);
149 std::string path(dev);
150 return std::string(
"/dev") + path.substr(path.rfind(
'/'));
153static int isTTYreal(
const char* dev) {
155#pragma GCC diagnostic push
156#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
162 std::string path = device_path(dev);
163 int fd = open(path.c_str(), O_RDONLY | O_NONBLOCK | O_NOCTTY);
165 struct serial_struct serinfo;
166 if (ioctl(fd, TIOCGSERIAL, &serinfo) == 0) {
167 ok = serinfo.type != PORT_UNKNOWN;
172 if (ioctl(fd, TIOCMGET, &modem_sts) == 0) {
173 ok = (modem_sts & (TIOCM_CTS | TIOCM_LE | TIOCM_DSR)) != 0;
177 if (fd >= 0) close(fd);
181 static const std::vector<std::regex> patterns = {
182 std::regex(
"ttyS[0-3]$", std::regex_constants::ECMAScript),
183 std::regex(
"ttyUSB", std::regex_constants::ECMAScript),
184 std::regex(
"ttyACM", std::regex_constants::ECMAScript),
185 std::regex(
"ttyAMA", std::regex_constants::ECMAScript)};
186 for (
auto re : patterns) {
187 if (std::regex_search(dev, re)) {
195#pragma GCC diagnostic pop
199static int isTTYreal(
const char* dev) {
return 1; }
204 return isTTYreal(data.path.c_str());
207#if defined(HAVE_DIRENT_H) && defined(HAVE_READLINK)
209#define HAVE_SYSFS_PORTS
212static std::vector<std::string> get_device_candidates() {
213 std::vector<std::string> devices;
216 dir = opendir(
"/sys/class/tty");
218 wxLogWarning(
"Cannot open /sys/class/tty: %s", strerror(errno));
221 const std::string prefix(
"/dev/");
222 for (ent = readdir(dir); ent; ent = readdir(dir)) {
223 devices.push_back(prefix + ent->d_name);
230static std::vector<struct symlink> get_all_links() {
231 std::vector<struct symlink> links;
234 dir = opendir(
"/dev");
236 wxLogError(
"Cannot open /dev: %s", strerror(errno));
239 const std::string prefix(
"/dev/");
240 for (ent = readdir(dir); ent; ent = readdir(dir)) {
242 const std::string path(prefix + ent->d_name);
243 int r = lstat(path.c_str(), &buf);
245 wxLogDebug(
"get_all_links: Cannot stat %s: %s", path.c_str(),
247 }
else if (S_ISLNK(buf.st_mode)) {
248 char buff[PATH_MAX + 1];
249 assert(readlink(path.c_str(), buff, PATH_MAX) >= 0);
250 std::string target(buff);
251 struct symlink link(path.c_str(), prefix + target);
252 links.push_back(link);
260static wxArrayString* EnumerateSysfsSerialPorts() {
261 std::vector<std::string> ports;
262 auto all_ports = get_device_candidates();
263 wxLogDebug(
"Enumerate: found %d candidates", all_ports.size());
264 for (
auto p : all_ports) {
265 if (isTTYreal(p.c_str())) ports.push_back(p);
267 wxLogDebug(
"Enumerate: found %d good ports", ports.size());
269 std::unordered_set<std::string>(ports.begin(), ports.end());
271 auto all_links = get_all_links();
272 wxLogDebug(
"Enumerate: found %d links", all_links.size());
273 for (
auto l : all_links) {
274 if (targets.find(l.target) != targets.end()) ports.push_back(l.path);
276 wxLogDebug(
"Enumerate: found %d devices", ports.size());
278 auto wx_ports =
new wxArrayString();
279 for (
auto p : ports) {
287#if defined(HAVE_LIBUDEV)
290static std::string get_device_info(
struct udev_device* ud) {
292 const char* prop = udev_device_get_property_value(ud,
"ID_VENDOR");
293 if (prop) info += prop;
294 prop = udev_device_get_property_value(ud,
"ID_MODEL");
295 if (prop) info += std::string(
" - ") + prop;
300#pragma GCC diagnostic push
301#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
304static std::vector<struct device_data> get_links(
struct udev_device* dev,
305 const std::regex& exclude) {
306 std::vector<struct device_data> items;
307 std::string info(
" link -> ");
308 info += udev_device_get_devnode(dev);
309 struct udev_list_entry* link = udev_device_get_devlinks_list_entry(dev);
311 const char* linkname = udev_list_entry_get_name(link);
312 if (!std::regex_search(linkname, exclude)) {
314 items.push_back(item);
316 link = udev_list_entry_get_next(link);
321static std::vector<struct device_data> enumerate_udev_ports(
struct udev* udev) {
322 struct udev_enumerate* enumerate = udev_enumerate_new(udev);
323 udev_enumerate_add_match_subsystem(enumerate,
"tty");
324 udev_enumerate_scan_devices(enumerate);
325 struct udev_list_entry* devices = udev_enumerate_get_list_entry(enumerate);
327 const std::regex bad_ttys(
".*tty[0-9][0-9]|^/dev/serial/.*|.*ttyS[0-9][0-9]");
328 std::vector<struct device_data> items;
329 struct udev_list_entry* entry;
330 udev_list_entry_foreach(entry, devices) {
331 const char*
const path = udev_list_entry_get_name(entry);
332 struct udev_device* device = udev_device_new_from_syspath(udev, path);
333 const char*
const devnode = udev_device_get_devnode(device);
334 struct device_data item(devnode, get_device_info(device));
335 if (!std::regex_search(devnode, bad_ttys) &&
336 (isTTYreal(path) || item.info.length() > 0)) {
337 items.push_back(item);
338 auto links = get_links(device, bad_ttys);
339 items.insert(items.end(), links.begin(), links.end());
341 udev_device_unref(device);
346#pragma GCC diagnostic pop
347static wxArrayString* EnumerateUdevSerialPorts() {
348 struct udev* udev = udev_new();
349 auto dev_items = enumerate_udev_ports(udev);
350 wxArrayString* ports =
new wxArrayString;
351 for (
const auto& item : dev_items) {
352 std::string port(item.path);
353 if (item.info.size() > 0) port += std::string(
" - ") + item.info;
362static wxArrayString* EnumerateWindowsSerialPorts() {
363 wxArrayString* preturn =
new wxArrayString;
374 for (
int i = 1; i < g_nCOMPortCheck; i++) {
376 s.Printf(
"COM%d", i);
379 DWORD dwSize =
sizeof(COMMCONFIG);
380 if (GetDefaultCommConfig(s.fn_str(), &cc, &dwSize))
381 preturn->Add(wxString(s));
388 GUID* guidDev = (GUID*)&GUID_CLASS_COMPORT;
390 HDEVINFO hDevInfo = INVALID_HANDLE_VALUE;
392 hDevInfo = SetupDiGetClassDevs(guidDev, NULL, NULL,
393 DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
395 if (hDevInfo != INVALID_HANDLE_VALUE) {
397 SP_DEVICE_INTERFACE_DATA ifcData;
399 ifcData.cbSize =
sizeof(SP_DEVICE_INTERFACE_DATA);
400 for (DWORD ii = 0; bOk; ii++) {
401 bOk = SetupDiEnumDeviceInterfaces(hDevInfo, NULL, guidDev, ii, &ifcData);
405 SP_DEVINFO_DATA devdata = {
sizeof(SP_DEVINFO_DATA)};
406 bOk = SetupDiGetDeviceInterfaceDetail(hDevInfo, &ifcData, NULL, 0, NULL,
411 if (GetLastError() ==
417 TCHAR fname[256] = {0};
418 TCHAR desc[256] = {0};
420 BOOL bSuccess = SetupDiGetDeviceRegistryProperty(
421 hDevInfo, &devdata, SPDRP_FRIENDLYNAME, NULL, (PBYTE)fname,
422 sizeof(fname), NULL);
424 bSuccess = bSuccess && SetupDiGetDeviceRegistryProperty(
425 hDevInfo, &devdata, SPDRP_DEVICEDESC, NULL,
426 (PBYTE)desc,
sizeof(desc), NULL);
431 bool bFoundCom =
false;
433 HKEY hDeviceRegistryKey =
434 SetupDiOpenDevRegKey(hDevInfo, &devdata, DICS_FLAG_GLOBAL, 0,
435 DIREG_DEV, KEY_QUERY_VALUE);
436 if (INVALID_HANDLE_VALUE != hDeviceRegistryKey) {
439 LPCWSTR cstr = wport;
440 MultiByteToWideChar(0, 0,
"PortName", -1, wport, 80);
441 DWORD len =
sizeof(dname);
443 int result = RegQueryValueEx(hDeviceRegistryKey, cstr, 0,
444 &RegKeyType, (PBYTE)dname, &len);
445 if (result == 0) bFoundCom =
true;
449 wxString port(dname, wxConvUTF8);
454 for (
unsigned int n = 0; n < preturn->GetCount(); n++) {
455 if ((preturn->Item(n)).IsSameAs(port)) {
456 preturn->RemoveAt(n);
460 wxString desc_name(desc, wxConvUTF8);
473 HDEVINFO hdeviceinfo = INVALID_HANDLE_VALUE;
475 hdeviceinfo = SetupDiGetClassDevs((GUID*)&GARMIN_DETECT_GUID, NULL, NULL,
476 DIGCF_PRESENT | DIGCF_INTERFACEDEVICE);
478 if (hdeviceinfo != INVALID_HANDLE_VALUE) {
479 if (GarminProtocolHandler::IsGarminPlugged()) {
480 wxLogMessage(
"EnumerateSerialPorts() Found Garmin USB Device.");
481 preturn->Add(
"Garmin-USB");
490#if defined(OCPN_USE_SYSFS_PORTS) && defined(HAVE_SYSFS_PORTS)
494#elif defined(OCPN_USE_UDEV_PORTS) && defined(HAVE_LIBUDEV)
498#elif defined(__ANDROID__)
502#elif defined(__WXOSX__)
505 wxArrayString* preturn =
new wxArrayString;
506 char* paPortNames[MAX_SERIAL_PORTS];
509 memset(paPortNames, 0x00,
sizeof(paPortNames));
510 iPortNameCount = FindSerialPortNames(&paPortNames[0], MAX_SERIAL_PORTS);
511 for (
int iPortIndex = 0; iPortIndex < iPortNameCount; iPortIndex++) {
512 wxString sm(paPortNames[iPortIndex], wxConvUTF8);
514 free(paPortNames[iPortIndex]);
519#elif defined(__WXMSW__)
523#elif defined(OCPN_USE_NEWSERIAL)
526 wxArrayString* preturn =
new wxArrayString;
528 for (
auto it = ports.begin(); it != ports.end(); ++it) {
529 wxString port(it->port);
530 if (it->description.length() > 0 && it->description !=
"n/a") {
532 port.Append(wxString::FromUTF8((it->description).c_str()));
541#error "Cannot enumerate serial ports (missing libudev.h?)"
Global variables stored in configuration file.
MacOS hardware probing functions.
wxArrayString * EnumerateSerialPorts(void)
Enumerate all serial ports.
std::vector< PortInfo > list_ports()
Lists the serial ports available on the system.