]> sigrok.org Git - libserialport.git/blame - linux.c
Don't use readlinkat if it does not exist
[libserialport.git] / linux.c
CommitLineData
e33dcf90
ML
1/*
2 * This file is part of the libserialport project.
3 *
4 * Copyright (C) 2014 Aurelien Jacobs <aurel@gnuage.org>
5 *
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.
10 *
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.
15 *
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/>.
18 */
19
20#include "libserialport.h"
21#include "libserialport_internal.h"
22
970f279a 23SP_PRIV enum sp_return get_port_details(struct sp_port *port)
e33dcf90
ML
24{
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];
30 char baddr[32];
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;
34 FILE *file;
35 int i, count;
36
37 if (strncmp(port->name, "/dev/", 5))
38 RETURN_ERROR(SP_ERR_ARG, "Device name not recognized (%s)", port->name);
39
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);
44 file_name[count] = 0;
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;
49
50 if (port->transport == SP_TRANSPORT_USB) {
51 for (i=0; i<5; i++) {
52 strcat(sub_dir, "../");
53
54 snprintf(file_name, sizeof(file_name), dir_name, dev, sub_dir, "busnum");
55 if (!(file = fopen(file_name, "r")))
56 continue;
57 count = fscanf(file, "%d", &bus);
58 fclose(file);
59 if (count != 1)
60 continue;
61
62 snprintf(file_name, sizeof(file_name), dir_name, dev, sub_dir, "devnum");
63 if (!(file = fopen(file_name, "r")))
64 continue;
65 count = fscanf(file, "%d", &address);
66 fclose(file);
67 if (count != 1)
68 continue;
69
70 snprintf(file_name, sizeof(file_name), dir_name, dev, sub_dir, "idVendor");
71 if (!(file = fopen(file_name, "r")))
72 continue;
73 count = fscanf(file, "%4x", &vid);
74 fclose(file);
75 if (count != 1)
76 continue;
77
78 snprintf(file_name, sizeof(file_name), dir_name, dev, sub_dir, "idProduct");
79 if (!(file = fopen(file_name, "r")))
80 continue;
81 count = fscanf(file, "%4x", &pid);
82 fclose(file);
83 if (count != 1)
84 continue;
85
86 port->usb_bus = bus;
87 port->usb_address = address;
88 port->usb_vid = vid;
89 port->usb_pid = pid;
90
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')
96 *ptr = 0;
97 port->description = strdup(description);
98 }
99 fclose(file);
100 }
101 if (!file || !ptr)
102 port->description = strdup(dev);
103
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')
109 *ptr = 0;
110 port->usb_manufacturer = strdup(manufacturer);
111 }
112 fclose(file);
113 }
114
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')
120 *ptr = 0;
121 port->usb_product = strdup(product);
122 }
123 fclose(file);
124 }
125
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')
131 *ptr = 0;
132 port->usb_serial = strdup(serial);
133 }
134 fclose(file);
135 }
136
137 break;
138 }
139 } else {
140 port->description = strdup(dev);
141
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')
148 *ptr = 0;
149 port->bluetooth_address = strdup(baddr);
150 }
151 fclose(file);
152 }
153 }
154 }
155
156 RETURN_OK();
157}
48a4076f 158
970f279a 159SP_PRIV enum sp_return list_ports(struct sp_port ***list)
48a4076f
AJ
160{
161 char name[PATH_MAX], target[PATH_MAX];
162 struct dirent entry, *result;
163 struct serial_struct serial_info;
5bd33b7c
MC
164#ifndef HAVE_READLINKAT
165 char buf[sizeof(entry.d_name) + 16];
166#endif
48a4076f
AJ
167 int len, fd, ioctl_result;
168 DIR *dir;
169 int ret = SP_OK;
170
171 DEBUG("Enumerating tty devices");
172 if (!(dir = opendir("/sys/class/tty")))
173 RETURN_FAIL("could not open /sys/class/tty");
174
175 DEBUG("Iterating over results");
176 while (!readdir_r(dir, &entry, &result) && result) {
5bd33b7c 177#ifdef HAVE_READLINKAT
48a4076f 178 len = readlinkat(dirfd(dir), entry.d_name, target, sizeof(target));
5bd33b7c
MC
179#else
180 snprintf(buf, sizeof(buf), "/sys/class/tty/%s", entry.d_name);
181 len = readlink(buf, target, sizeof(target));
182#endif
48a4076f
AJ
183 if (len <= 0 || len >= (int) sizeof(target)-1)
184 continue;
185 target[len] = 0;
186 if (strstr(target, "virtual"))
187 continue;
188 snprintf(name, sizeof(name), "/dev/%s", entry.d_name);
189 DEBUG("Found device %s", name);
190 if (strstr(target, "serial8250")) {
191 /* The serial8250 driver has a hardcoded number of ports.
192 * The only way to tell which actually exist on a given system
193 * is to try to open them and make an ioctl call. */
194 DEBUG("serial8250 device, attempting to open");
195 if ((fd = open(name, O_RDWR | O_NONBLOCK | O_NOCTTY)) < 0) {
196 DEBUG("open failed, skipping");
197 continue;
198 }
199 ioctl_result = ioctl(fd, TIOCGSERIAL, &serial_info);
200 close(fd);
201 if (ioctl_result != 0) {
202 DEBUG("ioctl failed, skipping");
203 continue;
204 }
205 if (serial_info.type == PORT_UNKNOWN) {
206 DEBUG("port type is unknown, skipping");
207 continue;
208 }
209 }
210 DEBUG("Found port %s", name);
211 *list = list_append(*list, name);
212 if (!list) {
213 SET_ERROR(ret, SP_ERR_MEM, "list append failed");
214 break;
215 }
216 }
217 closedir(dir);
218
219 return ret;
220}