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