]>
Commit | Line | Data |
---|---|---|
e33dcf90 ML |
1 | /* |
2 | * This file is part of the libserialport project. | |
3 | * | |
f77bb46d | 4 | * Copyright (C) 2013 Martin Ling <martin-libserialport@earth.li> |
e33dcf90 ML |
5 | * Copyright (C) 2014 Aurelien Jacobs <aurel@gnuage.org> |
6 | * | |
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. | |
11 | * | |
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. | |
16 | * | |
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/>. | |
19 | */ | |
20 | ||
1a584c45 | 21 | #include <config.h> |
e33dcf90 ML |
22 | #include "libserialport.h" |
23 | #include "libserialport_internal.h" | |
24 | ||
fa106ef1 CS |
25 | /* |
26 | * The 'e' modifier for O_CLOEXEC is glibc >= 2.7 only, hence not | |
27 | * portable, so provide an own wrapper for this functionality. | |
28 | */ | |
29 | static FILE *fopen_cloexec_rdonly(const char *pathname) | |
30 | { | |
31 | int fd; | |
32 | if ((fd = open(pathname, O_RDONLY | O_CLOEXEC)) < 0) | |
33 | return NULL; | |
34 | return fdopen(fd, "r"); | |
35 | } | |
36 | ||
970f279a | 37 | SP_PRIV enum sp_return get_port_details(struct sp_port *port) |
e33dcf90 | 38 | { |
dc422c04 UH |
39 | /* |
40 | * Description limited to 127 char, anything longer | |
41 | * would not be user friendly anyway. | |
42 | */ | |
e33dcf90 | 43 | char description[128]; |
0666ccc7 ML |
44 | int bus, address; |
45 | unsigned int vid, pid; | |
e33dcf90 ML |
46 | char manufacturer[128], product[128], serial[128]; |
47 | char baddr[32]; | |
48 | const char dir_name[] = "/sys/class/tty/%s/device/%s%s"; | |
9118f753 | 49 | char sub_dir[32] = "", link_name[PATH_MAX], file_name[PATH_MAX]; |
e33dcf90 ML |
50 | char *ptr, *dev = port->name + 5; |
51 | FILE *file; | |
52 | int i, count; | |
46d8b0a0 | 53 | struct stat statbuf; |
e33dcf90 ML |
54 | |
55 | if (strncmp(port->name, "/dev/", 5)) | |
dc422c04 | 56 | RETURN_ERROR(SP_ERR_ARG, "Device name not recognized"); |
e33dcf90 | 57 | |
9118f753 ML |
58 | snprintf(link_name, sizeof(link_name), "/sys/class/tty/%s", dev); |
59 | if (lstat(link_name, &statbuf) == -1) | |
46d8b0a0 | 60 | RETURN_ERROR(SP_ERR_ARG, "Device not found"); |
61 | if (!S_ISLNK(statbuf.st_mode)) | |
9118f753 ML |
62 | snprintf(link_name, sizeof(link_name), "/sys/class/tty/%s/device", dev); |
63 | count = readlink(link_name, file_name, sizeof(file_name)); | |
dc422c04 UH |
64 | if (count <= 0 || count >= (int)(sizeof(file_name) - 1)) |
65 | RETURN_ERROR(SP_ERR_ARG, "Device not found"); | |
e33dcf90 ML |
66 | file_name[count] = 0; |
67 | if (strstr(file_name, "bluetooth")) | |
68 | port->transport = SP_TRANSPORT_BLUETOOTH; | |
69 | else if (strstr(file_name, "usb")) | |
70 | port->transport = SP_TRANSPORT_USB; | |
71 | ||
72 | if (port->transport == SP_TRANSPORT_USB) { | |
dc422c04 | 73 | for (i = 0; i < 5; i++) { |
e33dcf90 ML |
74 | strcat(sub_dir, "../"); |
75 | ||
76 | snprintf(file_name, sizeof(file_name), dir_name, dev, sub_dir, "busnum"); | |
fa106ef1 | 77 | if (!(file = fopen_cloexec_rdonly(file_name))) |
e33dcf90 ML |
78 | continue; |
79 | count = fscanf(file, "%d", &bus); | |
80 | fclose(file); | |
81 | if (count != 1) | |
82 | continue; | |
83 | ||
84 | snprintf(file_name, sizeof(file_name), dir_name, dev, sub_dir, "devnum"); | |
fa106ef1 | 85 | if (!(file = fopen_cloexec_rdonly(file_name))) |
e33dcf90 ML |
86 | continue; |
87 | count = fscanf(file, "%d", &address); | |
88 | fclose(file); | |
89 | if (count != 1) | |
90 | continue; | |
91 | ||
92 | snprintf(file_name, sizeof(file_name), dir_name, dev, sub_dir, "idVendor"); | |
fa106ef1 | 93 | if (!(file = fopen_cloexec_rdonly(file_name))) |
e33dcf90 ML |
94 | continue; |
95 | count = fscanf(file, "%4x", &vid); | |
96 | fclose(file); | |
97 | if (count != 1) | |
98 | continue; | |
99 | ||
100 | snprintf(file_name, sizeof(file_name), dir_name, dev, sub_dir, "idProduct"); | |
fa106ef1 | 101 | if (!(file = fopen_cloexec_rdonly(file_name))) |
e33dcf90 ML |
102 | continue; |
103 | count = fscanf(file, "%4x", &pid); | |
104 | fclose(file); | |
105 | if (count != 1) | |
106 | continue; | |
107 | ||
108 | port->usb_bus = bus; | |
109 | port->usb_address = address; | |
110 | port->usb_vid = vid; | |
111 | port->usb_pid = pid; | |
112 | ||
113 | snprintf(file_name, sizeof(file_name), dir_name, dev, sub_dir, "product"); | |
fa106ef1 | 114 | if ((file = fopen_cloexec_rdonly(file_name))) { |
e33dcf90 ML |
115 | if ((ptr = fgets(description, sizeof(description), file))) { |
116 | ptr = description + strlen(description) - 1; | |
117 | if (ptr >= description && *ptr == '\n') | |
118 | *ptr = 0; | |
119 | port->description = strdup(description); | |
120 | } | |
121 | fclose(file); | |
122 | } | |
123 | if (!file || !ptr) | |
124 | port->description = strdup(dev); | |
125 | ||
126 | snprintf(file_name, sizeof(file_name), dir_name, dev, sub_dir, "manufacturer"); | |
fa106ef1 | 127 | if ((file = fopen_cloexec_rdonly(file_name))) { |
e33dcf90 ML |
128 | if ((ptr = fgets(manufacturer, sizeof(manufacturer), file))) { |
129 | ptr = manufacturer + strlen(manufacturer) - 1; | |
130 | if (ptr >= manufacturer && *ptr == '\n') | |
131 | *ptr = 0; | |
132 | port->usb_manufacturer = strdup(manufacturer); | |
133 | } | |
134 | fclose(file); | |
135 | } | |
136 | ||
137 | snprintf(file_name, sizeof(file_name), dir_name, dev, sub_dir, "product"); | |
fa106ef1 | 138 | if ((file = fopen_cloexec_rdonly(file_name))) { |
e33dcf90 ML |
139 | if ((ptr = fgets(product, sizeof(product), file))) { |
140 | ptr = product + strlen(product) - 1; | |
141 | if (ptr >= product && *ptr == '\n') | |
142 | *ptr = 0; | |
143 | port->usb_product = strdup(product); | |
144 | } | |
145 | fclose(file); | |
146 | } | |
147 | ||
148 | snprintf(file_name, sizeof(file_name), dir_name, dev, sub_dir, "serial"); | |
fa106ef1 | 149 | if ((file = fopen_cloexec_rdonly(file_name))) { |
e33dcf90 ML |
150 | if ((ptr = fgets(serial, sizeof(serial), file))) { |
151 | ptr = serial + strlen(serial) - 1; | |
152 | if (ptr >= serial && *ptr == '\n') | |
153 | *ptr = 0; | |
154 | port->usb_serial = strdup(serial); | |
155 | } | |
156 | fclose(file); | |
157 | } | |
158 | ||
ea17bfca UJ |
159 | /* If present, add serial to description for better identification. */ |
160 | if (port->usb_serial && strlen(port->usb_serial)) { | |
7c1101dc | 161 | snprintf(description, sizeof(description), |
ea17bfca UJ |
162 | "%s - %s", port->description, port->usb_serial); |
163 | if (port->description) | |
164 | free(port->description); | |
165 | port->description = strdup(description); | |
166 | } | |
167 | ||
e33dcf90 ML |
168 | break; |
169 | } | |
170 | } else { | |
171 | port->description = strdup(dev); | |
172 | ||
173 | if (port->transport == SP_TRANSPORT_BLUETOOTH) { | |
174 | snprintf(file_name, sizeof(file_name), dir_name, dev, "", "address"); | |
fa106ef1 | 175 | if ((file = fopen_cloexec_rdonly(file_name))) { |
e33dcf90 ML |
176 | if ((ptr = fgets(baddr, sizeof(baddr), file))) { |
177 | ptr = baddr + strlen(baddr) - 1; | |
178 | if (ptr >= baddr && *ptr == '\n') | |
179 | *ptr = 0; | |
180 | port->bluetooth_address = strdup(baddr); | |
181 | } | |
182 | fclose(file); | |
183 | } | |
184 | } | |
185 | } | |
186 | ||
187 | RETURN_OK(); | |
188 | } | |
48a4076f | 189 | |
970f279a | 190 | SP_PRIV enum sp_return list_ports(struct sp_port ***list) |
48a4076f AJ |
191 | { |
192 | char name[PATH_MAX], target[PATH_MAX]; | |
df3b70a8 | 193 | struct dirent *entry; |
f1c916ed | 194 | #ifdef HAVE_STRUCT_SERIAL_STRUCT |
48a4076f | 195 | struct serial_struct serial_info; |
12056e2f MC |
196 | int ioctl_result; |
197 | #endif | |
df3b70a8 | 198 | char buf[sizeof(entry->d_name) + 23]; |
12056e2f | 199 | int len, fd; |
48a4076f AJ |
200 | DIR *dir; |
201 | int ret = SP_OK; | |
46d8b0a0 | 202 | struct stat statbuf; |
48a4076f AJ |
203 | |
204 | DEBUG("Enumerating tty devices"); | |
205 | if (!(dir = opendir("/sys/class/tty"))) | |
dc422c04 | 206 | RETURN_FAIL("Could not open /sys/class/tty"); |
48a4076f AJ |
207 | |
208 | DEBUG("Iterating over results"); | |
df3b70a8 AJ |
209 | while ((entry = readdir(dir))) { |
210 | snprintf(buf, sizeof(buf), "/sys/class/tty/%s", entry->d_name); | |
46d8b0a0 | 211 | if (lstat(buf, &statbuf) == -1) |
212 | continue; | |
213 | if (!S_ISLNK(statbuf.st_mode)) | |
df3b70a8 | 214 | snprintf(buf, sizeof(buf), "/sys/class/tty/%s/device", entry->d_name); |
5bd33b7c | 215 | len = readlink(buf, target, sizeof(target)); |
dc422c04 | 216 | if (len <= 0 || len >= (int)(sizeof(target) - 1)) |
48a4076f AJ |
217 | continue; |
218 | target[len] = 0; | |
219 | if (strstr(target, "virtual")) | |
220 | continue; | |
df3b70a8 | 221 | snprintf(name, sizeof(name), "/dev/%s", entry->d_name); |
7890cef6 | 222 | DEBUG_FMT("Found device %s", name); |
48a4076f | 223 | if (strstr(target, "serial8250")) { |
dc422c04 UH |
224 | /* |
225 | * The serial8250 driver has a hardcoded number of ports. | |
48a4076f | 226 | * The only way to tell which actually exist on a given system |
dc422c04 UH |
227 | * is to try to open them and make an ioctl call. |
228 | */ | |
48a4076f | 229 | DEBUG("serial8250 device, attempting to open"); |
fa106ef1 | 230 | if ((fd = open(name, O_RDWR | O_NONBLOCK | O_NOCTTY | O_CLOEXEC)) < 0) { |
dc422c04 | 231 | DEBUG("Open failed, skipping"); |
48a4076f AJ |
232 | continue; |
233 | } | |
f1c916ed | 234 | #ifdef HAVE_STRUCT_SERIAL_STRUCT |
48a4076f | 235 | ioctl_result = ioctl(fd, TIOCGSERIAL, &serial_info); |
12056e2f | 236 | #endif |
48a4076f | 237 | close(fd); |
f1c916ed | 238 | #ifdef HAVE_STRUCT_SERIAL_STRUCT |
48a4076f AJ |
239 | if (ioctl_result != 0) { |
240 | DEBUG("ioctl failed, skipping"); | |
241 | continue; | |
242 | } | |
243 | if (serial_info.type == PORT_UNKNOWN) { | |
dc422c04 | 244 | DEBUG("Port type is unknown, skipping"); |
48a4076f AJ |
245 | continue; |
246 | } | |
12056e2f | 247 | #endif |
48a4076f | 248 | } |
7890cef6 | 249 | DEBUG_FMT("Found port %s", name); |
48a4076f | 250 | *list = list_append(*list, name); |
0c3f38b8 | 251 | if (!*list) { |
dc422c04 | 252 | SET_ERROR(ret, SP_ERR_MEM, "List append failed"); |
48a4076f AJ |
253 | break; |
254 | } | |
255 | } | |
256 | closedir(dir); | |
257 | ||
258 | return ret; | |
259 | } |