]>
Commit | Line | Data |
---|---|---|
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 | 23 | SP_PRIV enum sp_return get_port_details(struct sp_port *port) |
e33dcf90 | 24 | { |
dc422c04 UH |
25 | /* |
26 | * Description limited to 127 char, anything longer | |
27 | * would not be user friendly anyway. | |
28 | */ | |
e33dcf90 | 29 | char description[128]; |
0666ccc7 ML |
30 | int bus, address; |
31 | unsigned int vid, pid; | |
e33dcf90 ML |
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; | |
39 | ||
40 | if (strncmp(port->name, "/dev/", 5)) | |
dc422c04 | 41 | RETURN_ERROR(SP_ERR_ARG, "Device name not recognized"); |
e33dcf90 ML |
42 | |
43 | snprintf(file_name, sizeof(file_name), "/sys/class/tty/%s", dev); | |
44 | count = readlink(file_name, file_name, sizeof(file_name)); | |
dc422c04 UH |
45 | if (count <= 0 || count >= (int)(sizeof(file_name) - 1)) |
46 | RETURN_ERROR(SP_ERR_ARG, "Device not found"); | |
e33dcf90 ML |
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; | |
52 | ||
53 | if (port->transport == SP_TRANSPORT_USB) { | |
dc422c04 | 54 | for (i = 0; i < 5; i++) { |
e33dcf90 ML |
55 | strcat(sub_dir, "../"); |
56 | ||
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; | |
64 | ||
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; | |
72 | ||
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; | |
80 | ||
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; | |
88 | ||
89 | port->usb_bus = bus; | |
90 | port->usb_address = address; | |
91 | port->usb_vid = vid; | |
92 | port->usb_pid = pid; | |
93 | ||
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); | |
101 | } | |
102 | fclose(file); | |
103 | } | |
104 | if (!file || !ptr) | |
105 | port->description = strdup(dev); | |
106 | ||
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); | |
114 | } | |
115 | fclose(file); | |
116 | } | |
117 | ||
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); | |
125 | } | |
126 | fclose(file); | |
127 | } | |
128 | ||
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); | |
136 | } | |
137 | fclose(file); | |
138 | } | |
139 | ||
ea17bfca UJ |
140 | /* If present, add serial to description for better identification. */ |
141 | if (port->usb_serial && strlen(port->usb_serial)) { | |
7c1101dc | 142 | snprintf(description, sizeof(description), |
ea17bfca UJ |
143 | "%s - %s", port->description, port->usb_serial); |
144 | if (port->description) | |
145 | free(port->description); | |
146 | port->description = strdup(description); | |
147 | } | |
148 | ||
e33dcf90 ML |
149 | break; |
150 | } | |
151 | } else { | |
152 | port->description = strdup(dev); | |
153 | ||
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); | |
162 | } | |
163 | fclose(file); | |
164 | } | |
165 | } | |
166 | } | |
167 | ||
168 | RETURN_OK(); | |
169 | } | |
48a4076f | 170 | |
970f279a | 171 | SP_PRIV enum sp_return list_ports(struct sp_port ***list) |
48a4076f AJ |
172 | { |
173 | char name[PATH_MAX], target[PATH_MAX]; | |
174 | struct dirent entry, *result; | |
12056e2f | 175 | #ifdef HAVE_SERIAL_STRUCT |
48a4076f | 176 | struct serial_struct serial_info; |
12056e2f MC |
177 | int ioctl_result; |
178 | #endif | |
5bd33b7c | 179 | char buf[sizeof(entry.d_name) + 16]; |
12056e2f | 180 | int len, fd; |
48a4076f AJ |
181 | DIR *dir; |
182 | int ret = SP_OK; | |
183 | ||
184 | DEBUG("Enumerating tty devices"); | |
185 | if (!(dir = opendir("/sys/class/tty"))) | |
dc422c04 | 186 | RETURN_FAIL("Could not open /sys/class/tty"); |
48a4076f AJ |
187 | |
188 | DEBUG("Iterating over results"); | |
189 | while (!readdir_r(dir, &entry, &result) && result) { | |
5bd33b7c MC |
190 | snprintf(buf, sizeof(buf), "/sys/class/tty/%s", entry.d_name); |
191 | len = readlink(buf, target, sizeof(target)); | |
dc422c04 | 192 | if (len <= 0 || len >= (int)(sizeof(target) - 1)) |
48a4076f AJ |
193 | continue; |
194 | target[len] = 0; | |
195 | if (strstr(target, "virtual")) | |
196 | continue; | |
197 | snprintf(name, sizeof(name), "/dev/%s", entry.d_name); | |
7890cef6 | 198 | DEBUG_FMT("Found device %s", name); |
48a4076f | 199 | if (strstr(target, "serial8250")) { |
dc422c04 UH |
200 | /* |
201 | * The serial8250 driver has a hardcoded number of ports. | |
48a4076f | 202 | * The only way to tell which actually exist on a given system |
dc422c04 UH |
203 | * is to try to open them and make an ioctl call. |
204 | */ | |
48a4076f AJ |
205 | DEBUG("serial8250 device, attempting to open"); |
206 | if ((fd = open(name, O_RDWR | O_NONBLOCK | O_NOCTTY)) < 0) { | |
dc422c04 | 207 | DEBUG("Open failed, skipping"); |
48a4076f AJ |
208 | continue; |
209 | } | |
12056e2f | 210 | #ifdef HAVE_SERIAL_STRUCT |
48a4076f | 211 | ioctl_result = ioctl(fd, TIOCGSERIAL, &serial_info); |
12056e2f | 212 | #endif |
48a4076f | 213 | close(fd); |
12056e2f | 214 | #ifdef HAVE_SERIAL_STRUCT |
48a4076f AJ |
215 | if (ioctl_result != 0) { |
216 | DEBUG("ioctl failed, skipping"); | |
217 | continue; | |
218 | } | |
219 | if (serial_info.type == PORT_UNKNOWN) { | |
dc422c04 | 220 | DEBUG("Port type is unknown, skipping"); |
48a4076f AJ |
221 | continue; |
222 | } | |
12056e2f | 223 | #endif |
48a4076f | 224 | } |
7890cef6 | 225 | DEBUG_FMT("Found port %s", name); |
48a4076f AJ |
226 | *list = list_append(*list, name); |
227 | if (!list) { | |
dc422c04 | 228 | SET_ERROR(ret, SP_ERR_MEM, "List append failed"); |
48a4076f AJ |
229 | break; |
230 | } | |
231 | } | |
232 | closedir(dir); | |
233 | ||
234 | return ret; | |
235 | } |