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