2 * This file is part of the libserialport project.
4 * Copyright (C) 2014 Aurelien Jacobs <aurel@gnuage.org>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation, either version 3 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "libserialport.h"
21 #include "libserialport_internal.h"
23 SP_PRIV enum sp_return get_port_details(struct sp_port *port)
25 /* Description limited to 127 char,
26 anything longer would not be user friendly anyway */
27 char description[128];
28 int bus, address, vid, pid = -1;
29 char manufacturer[128], product[128], serial[128];
31 const char dir_name[] = "/sys/class/tty/%s/device/%s%s";
32 char sub_dir[32] = "", file_name[PATH_MAX];
33 char *ptr, *dev = port->name + 5;
37 if (strncmp(port->name, "/dev/", 5))
38 RETURN_ERROR(SP_ERR_ARG, "Device name not recognized (%s)", port->name);
40 snprintf(file_name, sizeof(file_name), "/sys/class/tty/%s", dev);
41 count = readlink(file_name, file_name, sizeof(file_name));
42 if (count <= 0 || count >= (int) sizeof(file_name)-1)
43 RETURN_ERROR(SP_ERR_ARG, "Device not found (%s)", port->name);
45 if (strstr(file_name, "bluetooth"))
46 port->transport = SP_TRANSPORT_BLUETOOTH;
47 else if (strstr(file_name, "usb"))
48 port->transport = SP_TRANSPORT_USB;
50 if (port->transport == SP_TRANSPORT_USB) {
52 strcat(sub_dir, "../");
54 snprintf(file_name, sizeof(file_name), dir_name, dev, sub_dir, "busnum");
55 if (!(file = fopen(file_name, "r")))
57 count = fscanf(file, "%d", &bus);
62 snprintf(file_name, sizeof(file_name), dir_name, dev, sub_dir, "devnum");
63 if (!(file = fopen(file_name, "r")))
65 count = fscanf(file, "%d", &address);
70 snprintf(file_name, sizeof(file_name), dir_name, dev, sub_dir, "idVendor");
71 if (!(file = fopen(file_name, "r")))
73 count = fscanf(file, "%4x", &vid);
78 snprintf(file_name, sizeof(file_name), dir_name, dev, sub_dir, "idProduct");
79 if (!(file = fopen(file_name, "r")))
81 count = fscanf(file, "%4x", &pid);
87 port->usb_address = address;
91 snprintf(file_name, sizeof(file_name), dir_name, dev, sub_dir, "product");
92 if ((file = fopen(file_name, "r"))) {
93 if ((ptr = fgets(description, sizeof(description), file))) {
94 ptr = description + strlen(description) - 1;
95 if (ptr >= description && *ptr == '\n')
97 port->description = strdup(description);
102 port->description = strdup(dev);
104 snprintf(file_name, sizeof(file_name), dir_name, dev, sub_dir, "manufacturer");
105 if ((file = fopen(file_name, "r"))) {
106 if ((ptr = fgets(manufacturer, sizeof(manufacturer), file))) {
107 ptr = manufacturer + strlen(manufacturer) - 1;
108 if (ptr >= manufacturer && *ptr == '\n')
110 port->usb_manufacturer = strdup(manufacturer);
115 snprintf(file_name, sizeof(file_name), dir_name, dev, sub_dir, "product");
116 if ((file = fopen(file_name, "r"))) {
117 if ((ptr = fgets(product, sizeof(product), file))) {
118 ptr = product + strlen(product) - 1;
119 if (ptr >= product && *ptr == '\n')
121 port->usb_product = strdup(product);
126 snprintf(file_name, sizeof(file_name), dir_name, dev, sub_dir, "serial");
127 if ((file = fopen(file_name, "r"))) {
128 if ((ptr = fgets(serial, sizeof(serial), file))) {
129 ptr = serial + strlen(serial) - 1;
130 if (ptr >= serial && *ptr == '\n')
132 port->usb_serial = strdup(serial);
140 port->description = strdup(dev);
142 if (port->transport == SP_TRANSPORT_BLUETOOTH) {
143 snprintf(file_name, sizeof(file_name), dir_name, dev, "", "address");
144 if ((file = fopen(file_name, "r"))) {
145 if ((ptr = fgets(baddr, sizeof(baddr), file))) {
146 ptr = baddr + strlen(baddr) - 1;
147 if (ptr >= baddr && *ptr == '\n')
149 port->bluetooth_address = strdup(baddr);
159 SP_PRIV enum sp_return list_ports(struct sp_port ***list)
161 char name[PATH_MAX], target[PATH_MAX];
162 struct dirent entry, *result;
163 #ifdef HAVE_SERIAL_STRUCT
164 struct serial_struct serial_info;
167 #ifndef HAVE_READLINKAT
168 char buf[sizeof(entry.d_name) + 16];
174 DEBUG("Enumerating tty devices");
175 if (!(dir = opendir("/sys/class/tty")))
176 RETURN_FAIL("could not open /sys/class/tty");
178 DEBUG("Iterating over results");
179 while (!readdir_r(dir, &entry, &result) && result) {
180 #ifdef HAVE_READLINKAT
181 len = readlinkat(dirfd(dir), entry.d_name, target, sizeof(target));
183 snprintf(buf, sizeof(buf), "/sys/class/tty/%s", entry.d_name);
184 len = readlink(buf, target, sizeof(target));
186 if (len <= 0 || len >= (int) sizeof(target)-1)
189 if (strstr(target, "virtual"))
191 snprintf(name, sizeof(name), "/dev/%s", entry.d_name);
192 DEBUG("Found device %s", name);
193 if (strstr(target, "serial8250")) {
194 /* The serial8250 driver has a hardcoded number of ports.
195 * The only way to tell which actually exist on a given system
196 * is to try to open them and make an ioctl call. */
197 DEBUG("serial8250 device, attempting to open");
198 if ((fd = open(name, O_RDWR | O_NONBLOCK | O_NOCTTY)) < 0) {
199 DEBUG("open failed, skipping");
202 #ifdef HAVE_SERIAL_STRUCT
203 ioctl_result = ioctl(fd, TIOCGSERIAL, &serial_info);
206 #ifdef HAVE_SERIAL_STRUCT
207 if (ioctl_result != 0) {
208 DEBUG("ioctl failed, skipping");
211 if (serial_info.type == PORT_UNKNOWN) {
212 DEBUG("port type is unknown, skipping");
217 DEBUG("Found port %s", name);
218 *list = list_append(*list, name);
220 SET_ERROR(ret, SP_ERR_MEM, "list append failed");