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 ssize_t size = readlink(path.c_str(), buff, PATH_MAX);
250 assert(size > 0 && size < PATH_MAX);
251 std::string target(buff, size);
252 struct symlink link(path.c_str(), prefix + target);
253 links.push_back(link);
261static wxArrayString* EnumerateSysfsSerialPorts() {
262 std::vector<std::string> ports;
263 auto all_ports = get_device_candidates();
264 wxLogDebug(
"Enumerate: found %d candidates", all_ports.size());
265 for (
auto p : all_ports) {
266 if (isTTYreal(p.c_str())) ports.push_back(p);
268 wxLogDebug(
"Enumerate: found %d good ports", ports.size());
270 std::unordered_set<std::string>(ports.begin(), ports.end());
272 auto all_links = get_all_links();
273 wxLogDebug(
"Enumerate: found %d links", all_links.size());
274 for (
auto l : all_links) {
275 if (targets.find(l.target) != targets.end()) ports.push_back(l.path);
277 wxLogDebug(
"Enumerate: found %d devices", ports.size());
279 auto wx_ports =
new wxArrayString();
280 for (
auto p : ports) {
288#if defined(HAVE_LIBUDEV)
291static std::string get_device_info(
struct udev_device* ud) {
293 const char* prop = udev_device_get_property_value(ud,
"ID_VENDOR");
294 if (prop) info += prop;
295 prop = udev_device_get_property_value(ud,
"ID_MODEL");
296 if (prop) info += std::string(
" - ") + prop;
301#pragma GCC diagnostic push
302#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
305static std::vector<struct device_data> get_links(
struct udev_device* dev,
306 const std::regex& exclude) {
307 std::vector<struct device_data> items;
308 std::string info(
" link -> ");
309 info += udev_device_get_devnode(dev);
310 struct udev_list_entry* link = udev_device_get_devlinks_list_entry(dev);
312 const char* linkname = udev_list_entry_get_name(link);
313 if (!std::regex_search(linkname, exclude)) {
315 items.push_back(item);
317 link = udev_list_entry_get_next(link);
322static std::vector<struct device_data> enumerate_udev_ports(
struct udev* udev) {
323 struct udev_enumerate* enumerate = udev_enumerate_new(udev);
324 udev_enumerate_add_match_subsystem(enumerate,
"tty");
325 udev_enumerate_scan_devices(enumerate);
326 struct udev_list_entry* devices = udev_enumerate_get_list_entry(enumerate);
328 const std::regex bad_ttys(
".*tty[0-9][0-9]|^/dev/serial/.*|.*ttyS[0-9][0-9]");
329 std::vector<struct device_data> items;
330 struct udev_list_entry* entry;
331 udev_list_entry_foreach(entry, devices) {
332 const char*
const path = udev_list_entry_get_name(entry);
333 struct udev_device* device = udev_device_new_from_syspath(udev, path);
334 const char*
const devnode = udev_device_get_devnode(device);
335 struct device_data item(devnode, get_device_info(device));
336 if (!std::regex_search(devnode, bad_ttys) &&
337 (isTTYreal(path) || item.info.length() > 0)) {
338 items.push_back(item);
339 auto links = get_links(device, bad_ttys);
340 items.insert(items.end(), links.begin(), links.end());
342 udev_device_unref(device);
344 udev_enumerate_unref(enumerate);
348#pragma GCC diagnostic pop
349static wxArrayString* EnumerateUdevSerialPorts() {
350 struct udev* udev = udev_new();
351 auto dev_items = enumerate_udev_ports(udev);
352 wxArrayString* ports =
new wxArrayString;
353 for (
const auto& item : dev_items) {
354 std::string port(item.path);
355 if (item.info.size() > 0) port += std::string(
" - ") + item.info;
365static wxArrayString* EnumerateWindowsSerialPorts() {
366 wxArrayString* preturn =
new wxArrayString;
377 for (
int i = 1; i < g_nCOMPortCheck; i++) {
379 s.Printf(
"COM%d", i);
382 DWORD dwSize =
sizeof(COMMCONFIG);
383 if (GetDefaultCommConfig(s.fn_str(), &cc, &dwSize))
384 preturn->Add(wxString(s));
391 GUID* guidDev = (GUID*)&GUID_CLASS_COMPORT;
393 HDEVINFO hDevInfo = INVALID_HANDLE_VALUE;
395 hDevInfo = SetupDiGetClassDevs(guidDev, NULL, NULL,
396 DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
398 if (hDevInfo != INVALID_HANDLE_VALUE) {
400 SP_DEVICE_INTERFACE_DATA ifcData;
402 ifcData.cbSize =
sizeof(SP_DEVICE_INTERFACE_DATA);
403 for (DWORD ii = 0; bOk; ii++) {
404 bOk = SetupDiEnumDeviceInterfaces(hDevInfo, NULL, guidDev, ii, &ifcData);
408 SP_DEVINFO_DATA devdata = {
sizeof(SP_DEVINFO_DATA)};
409 bOk = SetupDiGetDeviceInterfaceDetail(hDevInfo, &ifcData, NULL, 0, NULL,
414 if (GetLastError() ==
420 TCHAR fname[256] = {0};
421 TCHAR desc[256] = {0};
423 BOOL bSuccess = SetupDiGetDeviceRegistryProperty(
424 hDevInfo, &devdata, SPDRP_FRIENDLYNAME, NULL, (PBYTE)fname,
425 sizeof(fname), NULL);
427 bSuccess = bSuccess && SetupDiGetDeviceRegistryProperty(
428 hDevInfo, &devdata, SPDRP_DEVICEDESC, NULL,
429 (PBYTE)desc,
sizeof(desc), NULL);
434 bool bFoundCom =
false;
436 HKEY hDeviceRegistryKey =
437 SetupDiOpenDevRegKey(hDevInfo, &devdata, DICS_FLAG_GLOBAL, 0,
438 DIREG_DEV, KEY_QUERY_VALUE);
439 if (INVALID_HANDLE_VALUE != hDeviceRegistryKey) {
442 LPCWSTR cstr = wport;
443 MultiByteToWideChar(0, 0,
"PortName", -1, wport, 80);
444 DWORD len =
sizeof(dname);
446 int result = RegQueryValueEx(hDeviceRegistryKey, cstr, 0,
447 &RegKeyType, (PBYTE)dname, &len);
448 if (result == 0) bFoundCom =
true;
452 wxString port(dname, wxConvUTF8);
457 for (
unsigned int n = 0; n < preturn->GetCount(); n++) {
458 if ((preturn->Item(n)).IsSameAs(port)) {
459 preturn->RemoveAt(n);
463 wxString desc_name(desc, wxConvUTF8);
476 HDEVINFO hdeviceinfo = INVALID_HANDLE_VALUE;
478 hdeviceinfo = SetupDiGetClassDevs((GUID*)&GARMIN_DETECT_GUID, NULL, NULL,
479 DIGCF_PRESENT | DIGCF_INTERFACEDEVICE);
481 if (hdeviceinfo != INVALID_HANDLE_VALUE) {
482 if (GarminProtocolHandler::IsGarminPlugged()) {
483 wxLogMessage(
"EnumerateSerialPorts() Found Garmin USB Device.");
484 preturn->Add(
"Garmin-USB");
493#if defined(OCPN_USE_SYSFS_PORTS) && defined(HAVE_SYSFS_PORTS)
497#elif defined(OCPN_USE_UDEV_PORTS) && defined(HAVE_LIBUDEV)
501#elif defined(__ANDROID__)
505#elif defined(__WXOSX__)
508 wxArrayString* preturn =
new wxArrayString;
509 char* paPortNames[MAX_SERIAL_PORTS];
512 memset(paPortNames, 0x00,
sizeof(paPortNames));
513 iPortNameCount = FindSerialPortNames(&paPortNames[0], MAX_SERIAL_PORTS);
514 for (
int iPortIndex = 0; iPortIndex < iPortNameCount; iPortIndex++) {
515 wxString sm(paPortNames[iPortIndex], wxConvUTF8);
517 free(paPortNames[iPortIndex]);
522#elif defined(__WXMSW__)
526#elif defined(OCPN_USE_NEWSERIAL)
529 wxArrayString* preturn =
new wxArrayString;
531 for (
auto it = ports.begin(); it != ports.end(); ++it) {
532 wxString port(it->port);
533 if (it->description.length() > 0 && it->description !=
"n/a") {
535 port.Append(wxString::FromUTF8((it->description).c_str()));
544#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.