forked from todbot/usbSearch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlistComPorts.c
227 lines (192 loc) · 5.44 KB
/
listComPorts.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
* listComPorts.c -- list COM ports
*
* http://github.com/todbot/usbSearch/
*
* 2012, Tod E. Kurt, http://todbot.com/blog/
*
*
* Uses DispHealper : http://disphelper.sourceforge.net/
*
* Notable VIDs & PIDs combos:
* VID 0403 - FTDI
*
* VID 0403 / PID 6001 - Arduino Diecimila
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdarg.h>
#include <string.h>
#include <unistd.h>
#include <windows.h>
#include <setupapi.h>
#include <ddk/hidsdi.h>
#include <ddk/hidclass.h>
#include "disphelper.h"
//
void usage(void)
{
fprintf(stderr, "Usage: listComPorts [-h] [-v] [-vid <vid>] [-pid <pid>]\n");
fprintf(stderr, "\t-vid <vid> : Search by Vendor ID\n");
fprintf(stderr, "\t-papilio-duo : Find Com Port of Papilio DUO FPGA\n");
fprintf(stderr, "\t-papilio : Find Com Port of Papilio FPGA\n");
fprintf(stderr, "\t-arduino : Find Com Port of Papilio DUO Arduino ATmega32U4\n");
fprintf(stderr, "\t-bootloader : Find Com Port of Papilio DUO Arduino ATmega32U4 Bootloader\n");
fprintf(stderr, "\t-v : Verbose output (more for more output)\n");
fprintf(stderr, "\t-h : This help message\n");
fprintf(stderr, "\nFor more info, https://github.com/todbot/usbSearch\n");
exit(1);
}
// command-line options
int verbose = 0;
int papilio=0;
int papilioduo=0;
int arduino=0;
int bootloader=0;
char* VIDstr;
char* PIDstr;
void do_silly_test(void);
//
void parse_options(int argc, char **argv)
{
int i;
const char *arg;
for (i=1; i<argc; i++) {
arg = argv[i];
//printf("arg: %s\n", arg);
if (*arg == '-') { // starts with a dash
if (strcmp(arg, "-h") == 0) {
usage();
} else if (strcmp(arg, "-v") == 0) {
verbose++;
} else if (strcmp(arg, "-papilio") == 0) {
papilio++;
} else if (strcmp(arg, "-papilio-duo") == 0) {
papilioduo++;
} else if (strcmp(arg, "-arduino") == 0) {
arduino++;
} else if (strcmp(arg, "-bootloader") == 0) {
bootloader++;
} else if (strcmp(arg, "-vid") == 0) {
VIDstr = argv[++i];
} else if (strcmp(arg, "-pid") == 0) {
PIDstr = argv[++i];
}
}
}
}
//
int listComPorts(void)
{
if(verbose)
printf("Searching for COM ports...\n");
DISPATCH_OBJ(wmiSvc);
DISPATCH_OBJ(colDevices);
dhInitialize(TRUE);
dhToggleExceptions(TRUE);
dhGetObject(L"winmgmts:{impersonationLevel=impersonate}!\\\\.\\root\\cimv2",
//dhGetObject(L"winmgmts:\\\\.\\root\\cimv2",
NULL, &wmiSvc);
dhGetValue(L"%o", &colDevices, wmiSvc,
L".ExecQuery(%S)",
L"Select * from Win32_PnPEntity");
int port_count = 0;
int error = 1;
int found = 0;
dhToggleExceptions(FALSE);
FOR_EACH(objDevice, colDevices, NULL) {
char* name = NULL;
char* pnpid = NULL;
char* manu = NULL;
char* match;
dhGetValue(L"%s", &name, objDevice, L".Name");
dhGetValue(L"%s", &pnpid, objDevice, L".PnPDeviceID");
if(verbose>1) printf("'%s'.\n", name);
if( name != NULL && ((match = strstr( name, "(COM" )) != NULL) ) { // look for "(COM23)"
char* comname = strtok( match, "()");
// 'Manufacturuer' can be null, so only get it if we need it
dhGetValue(L"%s", &manu, objDevice, L".Manufacturer");
port_count++;
if (VIDstr != NULL) { //Are we searching for a VID
if( pnpid != NULL && ((match = strstr( pnpid, VIDstr )) != NULL) ) {// If searching for VID
printf("%s\n",comname);
error = 0;
}
} else if (papilio) {
if( pnpid != NULL && ((match = strstr( pnpid, "VID_0403+PID_6010" )) != NULL) ) {// If searching for Papilio FPGA serial
printf("%s\n",comname);
found = 1;
error = 0;
}
else {
if (!found) {
//printf("No Papilio port detected.");
error = 1;
}
}
} else if (papilioduo) {
if( pnpid != NULL && ((match = strstr( pnpid, "VID_0403+PID_7BC0" )) != NULL) ) {// If searching for Papilio DUO FPGA serial
printf("%s\n",comname);
found = 1;
error = 0;
}
else {
if (!found) {
//printf("No Papilio port detected.");
error = 1;
}
}
} else if (arduino) {
if( pnpid != NULL && ((match = strstr( pnpid, "VID_1D50&PID_60A5&MI_00" )) != NULL) ) {// If searching for Papilio FPGA serial
printf("%s\n",comname);
found = 1;
error = 0;
}
else {
if (!found) {
//printf("No Arduino port detected.");
error = 1;
}
}
} else if (bootloader) {
if( pnpid != NULL && ((match = strstr( pnpid, "VID_1D50&PID_60A4" )) != NULL) ) {// If searching for Papilio FPGA serial
printf("%s\n",comname);
found = 1;
error = 0;
}
else {
if (!found) {
//printf("No Arduino port detected.");
error = 1;
}
}
} else {
if(verbose)
printf("%s - %s - %s - %s\n",comname, name, manu, pnpid);
else
printf("%s - %s \n",comname, name);
error = 0;
}
dhFreeString(manu);
}
dhFreeString(name);
dhFreeString(pnpid);
} NEXT(objDevice);
SAFE_RELEASE(colDevices);
SAFE_RELEASE(wmiSvc);
dhUninitialize(TRUE);
if(verbose)
printf("Found %d port%s\n",port_count,(port_count==1)?"":"s");
if(error)
return 1;
else
return 0;
}
//
int main(int argc, char **argv)
{
parse_options(argc, argv);
return listComPorts();
}