]>
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 | ||
23 | 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 | } |