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