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