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)
26 * Description limited to 127 char, anything longer
27 * would not be user friendly anyway.
29 char description[128];
31 unsigned int vid, pid;
32 char manufacturer[128], product[128], serial[128];
34 const char dir_name[] = "/sys/class/tty/%s/device/%s%s";
35 char sub_dir[32] = "", file_name[PATH_MAX];
36 char *ptr, *dev = port->name + 5;
41 if (strncmp(port->name, "/dev/", 5))
42 RETURN_ERROR(SP_ERR_ARG, "Device name not recognized");
44 snprintf(file_name, sizeof(file_name), "/sys/class/tty/%s", dev);
45 if (lstat(file_name, &statbuf) == -1)
46 RETURN_ERROR(SP_ERR_ARG, "Device not found");
47 if (!S_ISLNK(statbuf.st_mode))
48 snprintf(file_name, sizeof(file_name), "/sys/class/tty/%s/device", dev);
49 count = readlink(file_name, file_name, sizeof(file_name));
50 if (count <= 0 || count >= (int)(sizeof(file_name) - 1))
51 RETURN_ERROR(SP_ERR_ARG, "Device not found");
53 if (strstr(file_name, "bluetooth"))
54 port->transport = SP_TRANSPORT_BLUETOOTH;
55 else if (strstr(file_name, "usb"))
56 port->transport = SP_TRANSPORT_USB;
58 if (port->transport == SP_TRANSPORT_USB) {
59 for (i = 0; i < 5; i++) {
60 strcat(sub_dir, "../");
62 snprintf(file_name, sizeof(file_name), dir_name, dev, sub_dir, "busnum");
63 if (!(file = fopen(file_name, "r")))
65 count = fscanf(file, "%d", &bus);
70 snprintf(file_name, sizeof(file_name), dir_name, dev, sub_dir, "devnum");
71 if (!(file = fopen(file_name, "r")))
73 count = fscanf(file, "%d", &address);
78 snprintf(file_name, sizeof(file_name), dir_name, dev, sub_dir, "idVendor");
79 if (!(file = fopen(file_name, "r")))
81 count = fscanf(file, "%4x", &vid);
86 snprintf(file_name, sizeof(file_name), dir_name, dev, sub_dir, "idProduct");
87 if (!(file = fopen(file_name, "r")))
89 count = fscanf(file, "%4x", &pid);
95 port->usb_address = address;
99 snprintf(file_name, sizeof(file_name), dir_name, dev, sub_dir, "product");
100 if ((file = fopen(file_name, "r"))) {
101 if ((ptr = fgets(description, sizeof(description), file))) {
102 ptr = description + strlen(description) - 1;
103 if (ptr >= description && *ptr == '\n')
105 port->description = strdup(description);
110 port->description = strdup(dev);
112 snprintf(file_name, sizeof(file_name), dir_name, dev, sub_dir, "manufacturer");
113 if ((file = fopen(file_name, "r"))) {
114 if ((ptr = fgets(manufacturer, sizeof(manufacturer), file))) {
115 ptr = manufacturer + strlen(manufacturer) - 1;
116 if (ptr >= manufacturer && *ptr == '\n')
118 port->usb_manufacturer = strdup(manufacturer);
123 snprintf(file_name, sizeof(file_name), dir_name, dev, sub_dir, "product");
124 if ((file = fopen(file_name, "r"))) {
125 if ((ptr = fgets(product, sizeof(product), file))) {
126 ptr = product + strlen(product) - 1;
127 if (ptr >= product && *ptr == '\n')
129 port->usb_product = strdup(product);
134 snprintf(file_name, sizeof(file_name), dir_name, dev, sub_dir, "serial");
135 if ((file = fopen(file_name, "r"))) {
136 if ((ptr = fgets(serial, sizeof(serial), file))) {
137 ptr = serial + strlen(serial) - 1;
138 if (ptr >= serial && *ptr == '\n')
140 port->usb_serial = strdup(serial);
145 /* If present, add serial to description for better identification. */
146 if (port->usb_serial && strlen(port->usb_serial)) {
147 snprintf(description, sizeof(description),
148 "%s - %s", port->description, port->usb_serial);
149 if (port->description)
150 free(port->description);
151 port->description = strdup(description);
157 port->description = strdup(dev);
159 if (port->transport == SP_TRANSPORT_BLUETOOTH) {
160 snprintf(file_name, sizeof(file_name), dir_name, dev, "", "address");
161 if ((file = fopen(file_name, "r"))) {
162 if ((ptr = fgets(baddr, sizeof(baddr), file))) {
163 ptr = baddr + strlen(baddr) - 1;
164 if (ptr >= baddr && *ptr == '\n')
166 port->bluetooth_address = strdup(baddr);
176 SP_PRIV enum sp_return list_ports(struct sp_port ***list)
178 char name[PATH_MAX], target[PATH_MAX];
179 struct dirent entry, *result;
180 #ifdef HAVE_SERIAL_STRUCT
181 struct serial_struct serial_info;
184 char buf[sizeof(entry.d_name) + 23];
190 DEBUG("Enumerating tty devices");
191 if (!(dir = opendir("/sys/class/tty")))
192 RETURN_FAIL("Could not open /sys/class/tty");
194 DEBUG("Iterating over results");
195 while (!readdir_r(dir, &entry, &result) && result) {
196 snprintf(buf, sizeof(buf), "/sys/class/tty/%s", entry.d_name);
197 if (lstat(buf, &statbuf) == -1)
199 if (!S_ISLNK(statbuf.st_mode))
200 snprintf(buf, sizeof(buf), "/sys/class/tty/%s/device", entry.d_name);
201 len = readlink(buf, target, sizeof(target));
202 if (len <= 0 || len >= (int)(sizeof(target) - 1))
205 if (strstr(target, "virtual"))
207 snprintf(name, sizeof(name), "/dev/%s", entry.d_name);
208 DEBUG_FMT("Found device %s", name);
209 if (strstr(target, "serial8250")) {
211 * The serial8250 driver has a hardcoded number of ports.
212 * The only way to tell which actually exist on a given system
213 * is to try to open them and make an ioctl call.
215 DEBUG("serial8250 device, attempting to open");
216 if ((fd = open(name, O_RDWR | O_NONBLOCK | O_NOCTTY)) < 0) {
217 DEBUG("Open failed, skipping");
220 #ifdef HAVE_SERIAL_STRUCT
221 ioctl_result = ioctl(fd, TIOCGSERIAL, &serial_info);
224 #ifdef HAVE_SERIAL_STRUCT
225 if (ioctl_result != 0) {
226 DEBUG("ioctl failed, skipping");
229 if (serial_info.type == PORT_UNKNOWN) {
230 DEBUG("Port type is unknown, skipping");
235 DEBUG_FMT("Found port %s", name);
236 *list = list_append(*list, name);
238 SET_ERROR(ret, SP_ERR_MEM, "List append failed");