Skip to content

Commit d42e14d

Browse files
committed
More graceful handling of already plugged in devices based on wdi_simple.c
1 parent ea639a4 commit d42e14d

File tree

1 file changed

+82
-10
lines changed

1 file changed

+82
-10
lines changed

driver_installer.c

Lines changed: 82 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,6 @@
99

1010
#include "libwdi.h"
1111

12-
#define RECOVERY_VID 0x2CA3
13-
#define RECOVERY_PID 0x40
14-
#define RECOVERY_DESC "DJI Recovery"
15-
#define RECOVERY_INF_NAME "dji_recovery.inf"
16-
1712
struct target
1813
{
1914
unsigned short vid;
@@ -39,33 +34,110 @@ void waitforexit(int r) {
3934
getch();
4035
exit(r);
4136
}
37+
38+
void diagnostics() {
39+
printf("Scanning for DJI Recovery interface ignoring drivers. Please plug in your device now.\n");
40+
static struct wdi_options_create_list ocl = { 0 };
41+
ocl.list_all = TRUE;
42+
ocl.list_hubs = TRUE;
43+
ocl.trim_whitespaces = TRUE;
44+
static struct wdi_device_info *ldev = {NULL, NULL, NULL, FALSE, 0, NULL, NULL, NULL, NULL};
45+
int r = 0;
46+
47+
while (1) {
48+
if (wdi_create_list(&ldev, &ocl) == WDI_SUCCESS) {
49+
r = WDI_SUCCESS;
50+
for (; (ldev != NULL) && (r == WDI_SUCCESS); ldev = ldev->next) {
51+
if ( (ldev->vid == targets[0].vid) && (ldev->pid == targets[0].pid) ) {
52+
53+
printf("Found DJI Recovery device:\n");
54+
55+
printf("vid: 0x%08x\n", ldev->vid);
56+
printf("pid: 0x%08x\n", ldev->pid);
57+
printf("is_composite: %d\n", ldev->is_composite);
58+
printf("mi: %d\n", ldev->mi);
59+
printf("description: %s\n", ldev->desc);
60+
printf("driver: %s\n", ldev->driver);
61+
printf("device_id: %s\n", ldev->device_id);
62+
printf("hardware_id: %s\n", ldev->hardware_id);
63+
printf("compatible_id : %s\n", ldev->compatible_id);
64+
printf("upper_filter : %s\n", ldev->upper_filter);
65+
printf("driver_version : %lld\n", ldev->driver_version);
66+
67+
fflush(stdout);
68+
waitforexit(0);
69+
}
70+
}
71+
}
72+
sleep(0.3);
73+
}
74+
}
75+
4276
int main(int argc, char **argv) {
77+
//increase libwdi log level
4378
if(argc == 2 && strncmp(argv[1], "-v", 2) == 0) {
4479
wdi_set_log_level(WDI_LOG_LEVEL_DEBUG);
4580
}
4681
else {
4782
wdi_set_log_level(WDI_LOG_LEVEL_NONE);
4883
}
84+
85+
//do a diagnostics run
86+
if(argc == 2 && strncmp(argv[1], "-d", 2) == 0) {
87+
diagnostics();
88+
}
89+
4990
struct target* ptr = targets;
5091
for (int i=0; i<numtargets; i++, ptr++ ) {
51-
struct wdi_device_info recovery_dev = {NULL, ptr->vid, ptr->pid, ptr->is_composite, ptr->iface, ptr->description, NULL, NULL, NULL, NULL, NULL, 0};
92+
struct wdi_device_info recovery_dev = {NULL, ptr->vid, ptr->pid, ptr->is_composite, ptr->iface, ptr->description, NULL, NULL, NULL, NULL, NULL, 0};
93+
struct wdi_device_info *ldev = {NULL, ptr->vid, ptr->pid, FALSE, 0, ptr->description, NULL, NULL, NULL};
5294
int r;
95+
static struct wdi_options_create_list ocl = { 0 };
5396
static struct wdi_options_prepare_driver opd = { 0 };
5497
static struct wdi_options_install_driver oid = { 0 };
5598
opd.driver_type = ptr->driver_type;
5699
opd.vendor_name = ptr->vendor_name;
57100

101+
static BOOL matching_device_found;
102+
58103
r = wdi_prepare_driver(&recovery_dev, NULL, ptr->inf_name, &opd);
59104
if (r != WDI_SUCCESS) {
60105
printf("Error: Unable to prepare %s driver for installation: %s.\n", ptr->description, wdi_strerror(r));
61106
waitforexit(1);
62107
}
63108
printf("Installing %s driver, this may take a few minutes.\n", ptr->description);
64-
r = wdi_install_driver(&recovery_dev, NULL, ptr->inf_name, &oid);
65-
if(r != WDI_SUCCESS) {
66-
printf("Error: Unable to install %s driver: %s.\n", ptr->description, wdi_strerror(r));
67-
waitforexit(1);
109+
110+
111+
// Try to match against a plugged device to avoid device manager prompts
112+
matching_device_found = FALSE;
113+
if (wdi_create_list(&ldev, &ocl) == WDI_SUCCESS) {
114+
r = WDI_SUCCESS;
115+
for (; (ldev != NULL) && (r == WDI_SUCCESS); ldev = ldev->next) {
116+
if ( (ldev->vid == recovery_dev.vid) && (ldev->pid == recovery_dev.pid) && (ldev->mi == recovery_dev.mi) &&(ldev->is_composite == recovery_dev.is_composite) ) {
117+
118+
recovery_dev.hardware_id = ldev->hardware_id;
119+
recovery_dev.device_id = ldev->device_id;
120+
matching_device_found = TRUE;
121+
printf("Found %s device already plugged in, replacing driver.\n", ptr->description);
122+
r = wdi_install_driver(&recovery_dev, NULL, ptr->inf_name, &oid);
123+
if(r != WDI_SUCCESS) {
124+
printf("Error: Unable to install %s driver: %s.\n", ptr->description, wdi_strerror(r));
125+
waitforexit(1);
126+
}
127+
}
128+
}
129+
}
130+
131+
// No plugged USB device matches this one -> install driver
132+
if (!matching_device_found) {
133+
r = wdi_install_driver(&recovery_dev, NULL, ptr->inf_name, &oid);
134+
if(r != WDI_SUCCESS) {
135+
printf("Error: Unable to install %s driver: %s.\n", ptr->description, wdi_strerror(r));
136+
waitforexit(1);
137+
}
68138
}
139+
140+
69141
printf("%s driver installed successfully.\n\n", ptr->description);
70142

71143
}

0 commit comments

Comments
 (0)