]>
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; | |
46d8b0a0 | 39 | struct stat statbuf; |
e33dcf90 ML |
40 | |
41 | if (strncmp(port->name, "/dev/", 5)) | |
dc422c04 | 42 | RETURN_ERROR(SP_ERR_ARG, "Device name not recognized"); |
e33dcf90 ML |
43 | |
44 | snprintf(file_name, sizeof(file_name), "/sys/class/tty/%s", dev); | |
46d8b0a0 | 45 | if (lstat(file_name, &statbuf) == -1) |
46 | RETURN_ERROR(SP_ERR_ARG, "Device not found"); | |
47 | if (!S_ISLNK(statbuf.st_mode)) | |
48 | snprintf(file_name, sizeof(file_name), "/sys/class/tty/%s/device", dev); | |
e33dcf90 | 49 | count = readlink(file_name, file_name, sizeof(file_name)); |
dc422c04 UH |
50 | if (count <= 0 || count >= (int)(sizeof(file_name) - 1)) |
51 | RETURN_ERROR(SP_ERR_ARG, "Device not found"); | |
e33dcf90 ML |
52 | file_name[count] = 0; |
53 | if (strstr(file_name, "bluetooth")) | |
54 | port->transport = SP_TRANSPORT_BLUETOOTH; | |
55 | else if (strstr(file_name, "usb")) | |
56 | port->transport = SP_TRANSPORT_USB; | |
57 | ||
58 | if (port->transport == SP_TRANSPORT_USB) { | |
dc422c04 | 59 | for (i = 0; i < 5; i++) { |
e33dcf90 ML |
60 | strcat(sub_dir, "../"); |
61 | ||
62 | snprintf(file_name, sizeof(file_name), dir_name, dev, sub_dir, "busnum"); | |
63 | if (!(file = fopen(file_name, "r"))) | |
64 | continue; | |
65 | count = fscanf(file, "%d", &bus); | |
66 | fclose(file); | |
67 | if (count != 1) | |
68 | continue; | |
69 | ||
70 | snprintf(file_name, sizeof(file_name), dir_name, dev, sub_dir, "devnum"); | |
71 | if (!(file = fopen(file_name, "r"))) | |
72 | continue; | |
73 | count = fscanf(file, "%d", &address); | |
74 | fclose(file); | |
75 | if (count != 1) | |
76 | continue; | |
77 | ||
78 | snprintf(file_name, sizeof(file_name), dir_name, dev, sub_dir, "idVendor"); | |
79 | if (!(file = fopen(file_name, "r"))) | |
80 | continue; | |
81 | count = fscanf(file, "%4x", &vid); | |
82 | fclose(file); | |
83 | if (count != 1) | |
84 | continue; | |
85 | ||
86 | snprintf(file_name, sizeof(file_name), dir_name, dev, sub_dir, "idProduct"); | |
87 | if (!(file = fopen(file_name, "r"))) | |
88 | continue; | |
89 | count = fscanf(file, "%4x", &pid); | |
90 | fclose(file); | |
91 | if (count != 1) | |
92 | continue; | |
93 | ||
94 | port->usb_bus = bus; | |
95 | port->usb_address = address; | |
96 | port->usb_vid = vid; | |
97 | port->usb_pid = pid; | |
98 | ||
99 | snprintf(file_name, sizeof(file_name), dir_name, dev, sub_dir, "product"); | |
100 | if ((file = fopen(file_name, "r"))) { | |
101 | if ((ptr = fgets(description, sizeof(description), file))) { | |
102 | ptr = description + strlen(description) - 1; | |
103 | if (ptr >= description && *ptr == '\n') | |
104 | *ptr = 0; | |
105 | port->description = strdup(description); | |
106 | } | |
107 | fclose(file); | |
108 | } | |
109 | if (!file || !ptr) | |
110 | port->description = strdup(dev); | |
111 | ||
112 | snprintf(file_name, sizeof(file_name), dir_name, dev, sub_dir, "manufacturer"); | |
113 | if ((file = fopen(file_name, "r"))) { | |
114 | if ((ptr = fgets(manufacturer, sizeof(manufacturer), file))) { | |
115 | ptr = manufacturer + strlen(manufacturer) - 1; | |
116 | if (ptr >= manufacturer && *ptr == '\n') | |
117 | *ptr = 0; | |
118 | port->usb_manufacturer = strdup(manufacturer); | |
119 | } | |
120 | fclose(file); | |
121 | } | |
122 | ||
123 | snprintf(file_name, sizeof(file_name), dir_name, dev, sub_dir, "product"); | |
124 | if ((file = fopen(file_name, "r"))) { | |
125 | if ((ptr = fgets(product, sizeof(product), file))) { | |
126 | ptr = product + strlen(product) - 1; | |
127 | if (ptr >= product && *ptr == '\n') | |
128 | *ptr = 0; | |
129 | port->usb_product = strdup(product); | |
130 | } | |
131 | fclose(file); | |
132 | } | |
133 | ||
134 | snprintf(file_name, sizeof(file_name), dir_name, dev, sub_dir, "serial"); | |
135 | if ((file = fopen(file_name, "r"))) { | |
136 | if ((ptr = fgets(serial, sizeof(serial), file))) { | |
137 | ptr = serial + strlen(serial) - 1; | |
138 | if (ptr >= serial && *ptr == '\n') | |
139 | *ptr = 0; | |
140 | port->usb_serial = strdup(serial); | |
141 | } | |
142 | fclose(file); | |
143 | } | |
144 | ||
ea17bfca UJ |
145 | /* If present, add serial to description for better identification. */ |
146 | if (port->usb_serial && strlen(port->usb_serial)) { | |
7c1101dc | 147 | snprintf(description, sizeof(description), |
ea17bfca UJ |
148 | "%s - %s", port->description, port->usb_serial); |
149 | if (port->description) | |
150 | free(port->description); | |
151 | port->description = strdup(description); | |
152 | } | |
153 | ||
e33dcf90 ML |
154 | break; |
155 | } | |
156 | } else { | |
157 | port->description = strdup(dev); | |
158 | ||
159 | if (port->transport == SP_TRANSPORT_BLUETOOTH) { | |
160 | snprintf(file_name, sizeof(file_name), dir_name, dev, "", "address"); | |
161 | if ((file = fopen(file_name, "r"))) { | |
162 | if ((ptr = fgets(baddr, sizeof(baddr), file))) { | |
163 | ptr = baddr + strlen(baddr) - 1; | |
164 | if (ptr >= baddr && *ptr == '\n') | |
165 | *ptr = 0; | |
166 | port->bluetooth_address = strdup(baddr); | |
167 | } | |
168 | fclose(file); | |
169 | } | |
170 | } | |
171 | } | |
172 | ||
173 | RETURN_OK(); | |
174 | } | |
48a4076f | 175 | |
970f279a | 176 | SP_PRIV enum sp_return list_ports(struct sp_port ***list) |
48a4076f AJ |
177 | { |
178 | char name[PATH_MAX], target[PATH_MAX]; | |
179 | struct dirent entry, *result; | |
12056e2f | 180 | #ifdef HAVE_SERIAL_STRUCT |
48a4076f | 181 | struct serial_struct serial_info; |
12056e2f MC |
182 | int ioctl_result; |
183 | #endif | |
46d8b0a0 | 184 | char buf[sizeof(entry.d_name) + 23]; |
12056e2f | 185 | int len, fd; |
48a4076f AJ |
186 | DIR *dir; |
187 | int ret = SP_OK; | |
46d8b0a0 | 188 | struct stat statbuf; |
48a4076f AJ |
189 | |
190 | DEBUG("Enumerating tty devices"); | |
191 | if (!(dir = opendir("/sys/class/tty"))) | |
dc422c04 | 192 | RETURN_FAIL("Could not open /sys/class/tty"); |
48a4076f AJ |
193 | |
194 | DEBUG("Iterating over results"); | |
195 | while (!readdir_r(dir, &entry, &result) && result) { | |
5bd33b7c | 196 | snprintf(buf, sizeof(buf), "/sys/class/tty/%s", entry.d_name); |
46d8b0a0 | 197 | if (lstat(buf, &statbuf) == -1) |
198 | continue; | |
199 | if (!S_ISLNK(statbuf.st_mode)) | |
200 | snprintf(buf, sizeof(buf), "/sys/class/tty/%s/device", entry.d_name); | |
5bd33b7c | 201 | len = readlink(buf, target, sizeof(target)); |
dc422c04 | 202 | if (len <= 0 || len >= (int)(sizeof(target) - 1)) |
48a4076f AJ |
203 | continue; |
204 | target[len] = 0; | |
205 | if (strstr(target, "virtual")) | |
206 | continue; | |
207 | snprintf(name, sizeof(name), "/dev/%s", entry.d_name); | |
7890cef6 | 208 | DEBUG_FMT("Found device %s", name); |
48a4076f | 209 | if (strstr(target, "serial8250")) { |
dc422c04 UH |
210 | /* |
211 | * The serial8250 driver has a hardcoded number of ports. | |
48a4076f | 212 | * The only way to tell which actually exist on a given system |
dc422c04 UH |
213 | * is to try to open them and make an ioctl call. |
214 | */ | |
48a4076f AJ |
215 | DEBUG("serial8250 device, attempting to open"); |
216 | if ((fd = open(name, O_RDWR | O_NONBLOCK | O_NOCTTY)) < 0) { | |
dc422c04 | 217 | DEBUG("Open failed, skipping"); |
48a4076f AJ |
218 | continue; |
219 | } | |
12056e2f | 220 | #ifdef HAVE_SERIAL_STRUCT |
48a4076f | 221 | ioctl_result = ioctl(fd, TIOCGSERIAL, &serial_info); |
12056e2f | 222 | #endif |
48a4076f | 223 | close(fd); |
12056e2f | 224 | #ifdef HAVE_SERIAL_STRUCT |
48a4076f AJ |
225 | if (ioctl_result != 0) { |
226 | DEBUG("ioctl failed, skipping"); | |
227 | continue; | |
228 | } | |
229 | if (serial_info.type == PORT_UNKNOWN) { | |
dc422c04 | 230 | DEBUG("Port type is unknown, skipping"); |
48a4076f AJ |
231 | continue; |
232 | } | |
12056e2f | 233 | #endif |
48a4076f | 234 | } |
7890cef6 | 235 | DEBUG_FMT("Found port %s", name); |
48a4076f AJ |
236 | *list = list_append(*list, name); |
237 | if (!list) { | |
dc422c04 | 238 | SET_ERROR(ret, SP_ERR_MEM, "List append failed"); |
48a4076f AJ |
239 | break; |
240 | } | |
241 | } | |
242 | closedir(dir); | |
243 | ||
244 | return ret; | |
245 | } |