configure.ac: Add #include guard to config.h.
[libserialport/gsi.git] / linux.c
blobd2cf1294646f1a7a7b1e4fd6fca1f5dcc11aa2bc
1 /*
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];
30 int bus, address;
31 unsigned int vid, pid;
32 char manufacturer[128], product[128], serial[128];
33 char baddr[32];
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;
37 FILE *file;
38 int i, count;
40 if (strncmp(port->name, "/dev/", 5))
41 RETURN_ERROR(SP_ERR_ARG, "Device name not recognized");
43 snprintf(file_name, sizeof(file_name), "/sys/class/tty/%s", dev);
44 count = readlink(file_name, file_name, sizeof(file_name));
45 if (count <= 0 || count >= (int)(sizeof(file_name) - 1))
46 RETURN_ERROR(SP_ERR_ARG, "Device not found");
47 file_name[count] = 0;
48 if (strstr(file_name, "bluetooth"))
49 port->transport = SP_TRANSPORT_BLUETOOTH;
50 else if (strstr(file_name, "usb"))
51 port->transport = SP_TRANSPORT_USB;
53 if (port->transport == SP_TRANSPORT_USB) {
54 for (i = 0; i < 5; i++) {
55 strcat(sub_dir, "../");
57 snprintf(file_name, sizeof(file_name), dir_name, dev, sub_dir, "busnum");
58 if (!(file = fopen(file_name, "r")))
59 continue;
60 count = fscanf(file, "%d", &bus);
61 fclose(file);
62 if (count != 1)
63 continue;
65 snprintf(file_name, sizeof(file_name), dir_name, dev, sub_dir, "devnum");
66 if (!(file = fopen(file_name, "r")))
67 continue;
68 count = fscanf(file, "%d", &address);
69 fclose(file);
70 if (count != 1)
71 continue;
73 snprintf(file_name, sizeof(file_name), dir_name, dev, sub_dir, "idVendor");
74 if (!(file = fopen(file_name, "r")))
75 continue;
76 count = fscanf(file, "%4x", &vid);
77 fclose(file);
78 if (count != 1)
79 continue;
81 snprintf(file_name, sizeof(file_name), dir_name, dev, sub_dir, "idProduct");
82 if (!(file = fopen(file_name, "r")))
83 continue;
84 count = fscanf(file, "%4x", &pid);
85 fclose(file);
86 if (count != 1)
87 continue;
89 port->usb_bus = bus;
90 port->usb_address = address;
91 port->usb_vid = vid;
92 port->usb_pid = pid;
94 snprintf(file_name, sizeof(file_name), dir_name, dev, sub_dir, "product");
95 if ((file = fopen(file_name, "r"))) {
96 if ((ptr = fgets(description, sizeof(description), file))) {
97 ptr = description + strlen(description) - 1;
98 if (ptr >= description && *ptr == '\n')
99 *ptr = 0;
100 port->description = strdup(description);
102 fclose(file);
104 if (!file || !ptr)
105 port->description = strdup(dev);
107 snprintf(file_name, sizeof(file_name), dir_name, dev, sub_dir, "manufacturer");
108 if ((file = fopen(file_name, "r"))) {
109 if ((ptr = fgets(manufacturer, sizeof(manufacturer), file))) {
110 ptr = manufacturer + strlen(manufacturer) - 1;
111 if (ptr >= manufacturer && *ptr == '\n')
112 *ptr = 0;
113 port->usb_manufacturer = strdup(manufacturer);
115 fclose(file);
118 snprintf(file_name, sizeof(file_name), dir_name, dev, sub_dir, "product");
119 if ((file = fopen(file_name, "r"))) {
120 if ((ptr = fgets(product, sizeof(product), file))) {
121 ptr = product + strlen(product) - 1;
122 if (ptr >= product && *ptr == '\n')
123 *ptr = 0;
124 port->usb_product = strdup(product);
126 fclose(file);
129 snprintf(file_name, sizeof(file_name), dir_name, dev, sub_dir, "serial");
130 if ((file = fopen(file_name, "r"))) {
131 if ((ptr = fgets(serial, sizeof(serial), file))) {
132 ptr = serial + strlen(serial) - 1;
133 if (ptr >= serial && *ptr == '\n')
134 *ptr = 0;
135 port->usb_serial = strdup(serial);
137 fclose(file);
140 /* If present, add serial to description for better identification. */
141 if (port->usb_serial && strlen(port->usb_serial)) {
142 snprintf(description, sizeof(description),
143 "%s - %s", port->description, port->usb_serial);
144 if (port->description)
145 free(port->description);
146 port->description = strdup(description);
149 break;
151 } else {
152 port->description = strdup(dev);
154 if (port->transport == SP_TRANSPORT_BLUETOOTH) {
155 snprintf(file_name, sizeof(file_name), dir_name, dev, "", "address");
156 if ((file = fopen(file_name, "r"))) {
157 if ((ptr = fgets(baddr, sizeof(baddr), file))) {
158 ptr = baddr + strlen(baddr) - 1;
159 if (ptr >= baddr && *ptr == '\n')
160 *ptr = 0;
161 port->bluetooth_address = strdup(baddr);
163 fclose(file);
168 RETURN_OK();
171 SP_PRIV enum sp_return list_ports(struct sp_port ***list)
173 char name[PATH_MAX], target[PATH_MAX];
174 struct dirent entry, *result;
175 #ifdef HAVE_SERIAL_STRUCT
176 struct serial_struct serial_info;
177 int ioctl_result;
178 #endif
179 char buf[sizeof(entry.d_name) + 16];
180 int len, fd;
181 DIR *dir;
182 int ret = SP_OK;
184 DEBUG("Enumerating tty devices");
185 if (!(dir = opendir("/sys/class/tty")))
186 RETURN_FAIL("Could not open /sys/class/tty");
188 DEBUG("Iterating over results");
189 while (!readdir_r(dir, &entry, &result) && result) {
190 snprintf(buf, sizeof(buf), "/sys/class/tty/%s", entry.d_name);
191 len = readlink(buf, target, sizeof(target));
192 if (len <= 0 || len >= (int)(sizeof(target) - 1))
193 continue;
194 target[len] = 0;
195 if (strstr(target, "virtual"))
196 continue;
197 snprintf(name, sizeof(name), "/dev/%s", entry.d_name);
198 DEBUG_FMT("Found device %s", name);
199 if (strstr(target, "serial8250")) {
201 * The serial8250 driver has a hardcoded number of ports.
202 * The only way to tell which actually exist on a given system
203 * is to try to open them and make an ioctl call.
205 DEBUG("serial8250 device, attempting to open");
206 if ((fd = open(name, O_RDWR | O_NONBLOCK | O_NOCTTY)) < 0) {
207 DEBUG("Open failed, skipping");
208 continue;
210 #ifdef HAVE_SERIAL_STRUCT
211 ioctl_result = ioctl(fd, TIOCGSERIAL, &serial_info);
212 #endif
213 close(fd);
214 #ifdef HAVE_SERIAL_STRUCT
215 if (ioctl_result != 0) {
216 DEBUG("ioctl failed, skipping");
217 continue;
219 if (serial_info.type == PORT_UNKNOWN) {
220 DEBUG("Port type is unknown, skipping");
221 continue;
223 #endif
225 DEBUG_FMT("Found port %s", name);
226 *list = list_append(*list, name);
227 if (!list) {
228 SET_ERROR(ret, SP_ERR_MEM, "List append failed");
229 break;
232 closedir(dir);
234 return ret;