2 * This file is part of the libserialport project.
4 * Copyright (C) 2013 Martin Ling <martin-libserialport@earth.li>
5 * Copyright (C) 2014 Aurelien Jacobs <aurel@gnuage.org>
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "libserialport.h"
22 #include "libserialport_internal.h"
24 SP_PRIV enum sp_return get_port_details(struct sp_port *port)
27 * Description limited to 127 char, anything longer
28 * would not be user friendly anyway.
30 char description[128];
32 unsigned int vid, pid;
33 char manufacturer[128], product[128], serial[128];
35 const char dir_name[] = "/sys/class/tty/%s/device/%s%s";
36 char sub_dir[32] = "", file_name[PATH_MAX];
37 char *ptr, *dev = port->name + 5;
42 if (strncmp(port->name, "/dev/", 5))
43 RETURN_ERROR(SP_ERR_ARG, "Device name not recognized");
45 snprintf(file_name, sizeof(file_name), "/sys/class/tty/%s", dev);
46 if (lstat(file_name, &statbuf) == -1)
47 RETURN_ERROR(SP_ERR_ARG, "Device not found");
48 if (!S_ISLNK(statbuf.st_mode))
49 snprintf(file_name, sizeof(file_name), "/sys/class/tty/%s/device", dev);
50 count = readlink(file_name, file_name, sizeof(file_name));
51 if (count <= 0 || count >= (int)(sizeof(file_name) - 1))
52 RETURN_ERROR(SP_ERR_ARG, "Device not found");
54 if (strstr(file_name, "bluetooth"))
55 port->transport = SP_TRANSPORT_BLUETOOTH;
56 else if (strstr(file_name, "usb"))
57 port->transport = SP_TRANSPORT_USB;
59 if (port->transport == SP_TRANSPORT_USB) {
60 for (i = 0; i < 5; i++) {
61 strcat(sub_dir, "../");
63 snprintf(file_name, sizeof(file_name), dir_name, dev, sub_dir, "busnum");
64 if (!(file = fopen(file_name, "r")))
66 count = fscanf(file, "%d", &bus);
71 snprintf(file_name, sizeof(file_name), dir_name, dev, sub_dir, "devnum");
72 if (!(file = fopen(file_name, "r")))
74 count = fscanf(file, "%d", &address);
79 snprintf(file_name, sizeof(file_name), dir_name, dev, sub_dir, "idVendor");
80 if (!(file = fopen(file_name, "r")))
82 count = fscanf(file, "%4x", &vid);
87 snprintf(file_name, sizeof(file_name), dir_name, dev, sub_dir, "idProduct");
88 if (!(file = fopen(file_name, "r")))
90 count = fscanf(file, "%4x", &pid);
96 port->usb_address = address;
100 snprintf(file_name, sizeof(file_name), dir_name, dev, sub_dir, "product");
101 if ((file = fopen(file_name, "r"))) {
102 if ((ptr = fgets(description, sizeof(description), file))) {
103 ptr = description + strlen(description) - 1;
104 if (ptr >= description && *ptr == '\n')
106 port->description = strdup(description);
111 port->description = strdup(dev);
113 snprintf(file_name, sizeof(file_name), dir_name, dev, sub_dir, "manufacturer");
114 if ((file = fopen(file_name, "r"))) {
115 if ((ptr = fgets(manufacturer, sizeof(manufacturer), file))) {
116 ptr = manufacturer + strlen(manufacturer) - 1;
117 if (ptr >= manufacturer && *ptr == '\n')
119 port->usb_manufacturer = strdup(manufacturer);
124 snprintf(file_name, sizeof(file_name), dir_name, dev, sub_dir, "product");
125 if ((file = fopen(file_name, "r"))) {
126 if ((ptr = fgets(product, sizeof(product), file))) {
127 ptr = product + strlen(product) - 1;
128 if (ptr >= product && *ptr == '\n')
130 port->usb_product = strdup(product);
135 snprintf(file_name, sizeof(file_name), dir_name, dev, sub_dir, "serial");
136 if ((file = fopen(file_name, "r"))) {
137 if ((ptr = fgets(serial, sizeof(serial), file))) {
138 ptr = serial + strlen(serial) - 1;
139 if (ptr >= serial && *ptr == '\n')
141 port->usb_serial = strdup(serial);
146 /* If present, add serial to description for better identification. */
147 if (port->usb_serial && strlen(port->usb_serial)) {
148 snprintf(description, sizeof(description),
149 "%s - %s", port->description, port->usb_serial);
150 if (port->description)
151 free(port->description);
152 port->description = strdup(description);
158 port->description = strdup(dev);
160 if (port->transport == SP_TRANSPORT_BLUETOOTH) {
161 snprintf(file_name, sizeof(file_name), dir_name, dev, "", "address");
162 if ((file = fopen(file_name, "r"))) {
163 if ((ptr = fgets(baddr, sizeof(baddr), file))) {
164 ptr = baddr + strlen(baddr) - 1;
165 if (ptr >= baddr && *ptr == '\n')
167 port->bluetooth_address = strdup(baddr);
177 SP_PRIV enum sp_return list_ports(struct sp_port ***list)
179 char name[PATH_MAX], target[PATH_MAX];
180 struct dirent entry, *result;
181 #ifdef HAVE_SERIAL_STRUCT
182 struct serial_struct serial_info;
185 char buf[sizeof(entry.d_name) + 23];
191 DEBUG("Enumerating tty devices");
192 if (!(dir = opendir("/sys/class/tty")))
193 RETURN_FAIL("Could not open /sys/class/tty");
195 DEBUG("Iterating over results");
196 while (!readdir_r(dir, &entry, &result) && result) {
197 snprintf(buf, sizeof(buf), "/sys/class/tty/%s", entry.d_name);
198 if (lstat(buf, &statbuf) == -1)
200 if (!S_ISLNK(statbuf.st_mode))
201 snprintf(buf, sizeof(buf), "/sys/class/tty/%s/device", entry.d_name);
202 len = readlink(buf, target, sizeof(target));
203 if (len <= 0 || len >= (int)(sizeof(target) - 1))
206 if (strstr(target, "virtual"))
208 snprintf(name, sizeof(name), "/dev/%s", entry.d_name);
209 DEBUG_FMT("Found device %s", name);
210 if (strstr(target, "serial8250")) {
212 * The serial8250 driver has a hardcoded number of ports.
213 * The only way to tell which actually exist on a given system
214 * is to try to open them and make an ioctl call.
216 DEBUG("serial8250 device, attempting to open");
217 if ((fd = open(name, O_RDWR | O_NONBLOCK | O_NOCTTY)) < 0) {
218 DEBUG("Open failed, skipping");
221 #ifdef HAVE_SERIAL_STRUCT
222 ioctl_result = ioctl(fd, TIOCGSERIAL, &serial_info);
225 #ifdef HAVE_SERIAL_STRUCT
226 if (ioctl_result != 0) {
227 DEBUG("ioctl failed, skipping");
230 if (serial_info.type == PORT_UNKNOWN) {
231 DEBUG("Port type is unknown, skipping");
236 DEBUG_FMT("Found port %s", name);
237 *list = list_append(*list, name);
239 SET_ERROR(ret, SP_ERR_MEM, "List append failed");