]> sigrok.org Git - libserialport.git/blob - linux.c
linux: Fix warning by marking vid, pid variables unsigned.
[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;
29         unsigned int vid, pid;
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))
39                 RETURN_ERROR(SP_ERR_ARG, "Device name not recognized.");
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)
44                 RETURN_ERROR(SP_ERR_ARG, "Device not found.");
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 }
159
160 SP_PRIV enum sp_return list_ports(struct sp_port ***list)
161 {
162         char name[PATH_MAX], target[PATH_MAX];
163         struct dirent entry, *result;
164 #ifdef HAVE_SERIAL_STRUCT
165         struct serial_struct serial_info;
166         int ioctl_result;
167 #endif
168 #ifndef HAVE_READLINKAT
169         char buf[sizeof(entry.d_name) + 16];
170 #endif
171         int len, fd;
172         DIR *dir;
173         int ret = SP_OK;
174
175         DEBUG("Enumerating tty devices");
176         if (!(dir = opendir("/sys/class/tty")))
177                 RETURN_FAIL("could not open /sys/class/tty");
178
179         DEBUG("Iterating over results");
180         while (!readdir_r(dir, &entry, &result) && result) {
181 #ifdef HAVE_READLINKAT
182                 len = readlinkat(dirfd(dir), entry.d_name, target, sizeof(target));
183 #else
184                 snprintf(buf, sizeof(buf), "/sys/class/tty/%s", entry.d_name);
185                 len = readlink(buf, target, sizeof(target));
186 #endif
187                 if (len <= 0 || len >= (int) sizeof(target)-1)
188                         continue;
189                 target[len] = 0;
190                 if (strstr(target, "virtual"))
191                         continue;
192                 snprintf(name, sizeof(name), "/dev/%s", entry.d_name);
193                 DEBUG_FMT("Found device %s", name);
194                 if (strstr(target, "serial8250")) {
195                         /* The serial8250 driver has a hardcoded number of ports.
196                          * The only way to tell which actually exist on a given system
197                          * is to try to open them and make an ioctl call. */
198                         DEBUG("serial8250 device, attempting to open");
199                         if ((fd = open(name, O_RDWR | O_NONBLOCK | O_NOCTTY)) < 0) {
200                                 DEBUG("open failed, skipping");
201                                 continue;
202                         }
203 #ifdef HAVE_SERIAL_STRUCT
204                         ioctl_result = ioctl(fd, TIOCGSERIAL, &serial_info);
205 #endif
206                         close(fd);
207 #ifdef HAVE_SERIAL_STRUCT
208                         if (ioctl_result != 0) {
209                                 DEBUG("ioctl failed, skipping");
210                                 continue;
211                         }
212                         if (serial_info.type == PORT_UNKNOWN) {
213                                 DEBUG("port type is unknown, skipping");
214                                 continue;
215                         }
216 #endif
217                 }
218                 DEBUG_FMT("Found port %s", name);
219                 *list = list_append(*list, name);
220                 if (!list) {
221                         SET_ERROR(ret, SP_ERR_MEM, "list append failed");
222                         break;
223                 }
224         }
225         closedir(dir);
226
227         return ret;
228 }