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