Skip to content

Commit

Permalink
More graceful handling of already plugged in devices based on wdi_sim…
Browse files Browse the repository at this point in the history
…ple.c
  • Loading branch information
j005u committed Apr 3, 2023
1 parent ea639a4 commit d42e14d
Showing 1 changed file with 82 additions and 10 deletions.
92 changes: 82 additions & 10 deletions driver_installer.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@

#include "libwdi.h"

#define RECOVERY_VID 0x2CA3
#define RECOVERY_PID 0x40
#define RECOVERY_DESC "DJI Recovery"
#define RECOVERY_INF_NAME "dji_recovery.inf"

struct target
{
unsigned short vid;
Expand All @@ -39,33 +34,110 @@ void waitforexit(int r) {
getch();
exit(r);
}

void diagnostics() {
printf("Scanning for DJI Recovery interface ignoring drivers. Please plug in your device now.\n");
static struct wdi_options_create_list ocl = { 0 };
ocl.list_all = TRUE;
ocl.list_hubs = TRUE;
ocl.trim_whitespaces = TRUE;
static struct wdi_device_info *ldev = {NULL, NULL, NULL, FALSE, 0, NULL, NULL, NULL, NULL};
int r = 0;

while (1) {
if (wdi_create_list(&ldev, &ocl) == WDI_SUCCESS) {
r = WDI_SUCCESS;
for (; (ldev != NULL) && (r == WDI_SUCCESS); ldev = ldev->next) {
if ( (ldev->vid == targets[0].vid) && (ldev->pid == targets[0].pid) ) {

printf("Found DJI Recovery device:\n");

printf("vid: 0x%08x\n", ldev->vid);
printf("pid: 0x%08x\n", ldev->pid);
printf("is_composite: %d\n", ldev->is_composite);
printf("mi: %d\n", ldev->mi);
printf("description: %s\n", ldev->desc);
printf("driver: %s\n", ldev->driver);
printf("device_id: %s\n", ldev->device_id);
printf("hardware_id: %s\n", ldev->hardware_id);
printf("compatible_id : %s\n", ldev->compatible_id);
printf("upper_filter : %s\n", ldev->upper_filter);
printf("driver_version : %lld\n", ldev->driver_version);

fflush(stdout);
waitforexit(0);
}
}
}
sleep(0.3);
}
}

int main(int argc, char **argv) {
//increase libwdi log level
if(argc == 2 && strncmp(argv[1], "-v", 2) == 0) {
wdi_set_log_level(WDI_LOG_LEVEL_DEBUG);
}
else {
wdi_set_log_level(WDI_LOG_LEVEL_NONE);
}

//do a diagnostics run
if(argc == 2 && strncmp(argv[1], "-d", 2) == 0) {
diagnostics();
}

struct target* ptr = targets;
for (int i=0; i<numtargets; i++, ptr++ ) {
struct wdi_device_info recovery_dev = {NULL, ptr->vid, ptr->pid, ptr->is_composite, ptr->iface, ptr->description, NULL, NULL, NULL, NULL, NULL, 0};
struct wdi_device_info recovery_dev = {NULL, ptr->vid, ptr->pid, ptr->is_composite, ptr->iface, ptr->description, NULL, NULL, NULL, NULL, NULL, 0};
struct wdi_device_info *ldev = {NULL, ptr->vid, ptr->pid, FALSE, 0, ptr->description, NULL, NULL, NULL};
int r;
static struct wdi_options_create_list ocl = { 0 };
static struct wdi_options_prepare_driver opd = { 0 };
static struct wdi_options_install_driver oid = { 0 };
opd.driver_type = ptr->driver_type;
opd.vendor_name = ptr->vendor_name;

static BOOL matching_device_found;

r = wdi_prepare_driver(&recovery_dev, NULL, ptr->inf_name, &opd);
if (r != WDI_SUCCESS) {
printf("Error: Unable to prepare %s driver for installation: %s.\n", ptr->description, wdi_strerror(r));
waitforexit(1);
}
printf("Installing %s driver, this may take a few minutes.\n", ptr->description);
r = wdi_install_driver(&recovery_dev, NULL, ptr->inf_name, &oid);
if(r != WDI_SUCCESS) {
printf("Error: Unable to install %s driver: %s.\n", ptr->description, wdi_strerror(r));
waitforexit(1);


// Try to match against a plugged device to avoid device manager prompts
matching_device_found = FALSE;
if (wdi_create_list(&ldev, &ocl) == WDI_SUCCESS) {
r = WDI_SUCCESS;
for (; (ldev != NULL) && (r == WDI_SUCCESS); ldev = ldev->next) {
if ( (ldev->vid == recovery_dev.vid) && (ldev->pid == recovery_dev.pid) && (ldev->mi == recovery_dev.mi) &&(ldev->is_composite == recovery_dev.is_composite) ) {

recovery_dev.hardware_id = ldev->hardware_id;
recovery_dev.device_id = ldev->device_id;
matching_device_found = TRUE;
printf("Found %s device already plugged in, replacing driver.\n", ptr->description);
r = wdi_install_driver(&recovery_dev, NULL, ptr->inf_name, &oid);
if(r != WDI_SUCCESS) {
printf("Error: Unable to install %s driver: %s.\n", ptr->description, wdi_strerror(r));
waitforexit(1);
}
}
}
}

// No plugged USB device matches this one -> install driver
if (!matching_device_found) {
r = wdi_install_driver(&recovery_dev, NULL, ptr->inf_name, &oid);
if(r != WDI_SUCCESS) {
printf("Error: Unable to install %s driver: %s.\n", ptr->description, wdi_strerror(r));
waitforexit(1);
}
}


printf("%s driver installed successfully.\n\n", ptr->description);

}
Expand Down

0 comments on commit d42e14d

Please sign in to comment.