Skip to content

Commit

Permalink
added a silly test for future thinking
Browse files Browse the repository at this point in the history
  • Loading branch information
todbot committed Mar 1, 2012
1 parent 424fc9b commit 2fa7bf0
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 16 deletions.
109 changes: 93 additions & 16 deletions usbSearch.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
/*
* usbSearch -- Search for USB devices by VID and PID, shows serial numbers
*
* http://github.com/todbot/usbSearch/
*
* 2012, Tod E. Kurt, http://todbot.com/blog/
*
*
* Uses DispHealper : http://disphelper.sourceforge.net/
* Also see:
* http://blogs.msdn.com/b/powershell/archive/2007/02/24/displaying-usb-devices-using-wmi.aspx
* http://technet.microsoft.com/en-us/library/ee156618.aspx
*
* Notable VIDs & PIDs combos:
* VID 0403 - FTDI
*
* VID 0403 / PID 6001 - Arduino Diecimila
*
*/

Expand All @@ -21,23 +37,22 @@
//
void usage(void)
{
fprintf(stderr, "Usage: usbSearch [-w] [-h] [-n] [-v] \n");
fprintf(stderr, "\t-w : Wait for device to appear\n");
fprintf(stderr, "\t-r : Use hard reboot if device not online\n");
fprintf(stderr, "\t-n : No reboot after programming\n");
fprintf(stderr, "\t-v : Verbose output\n");
fprintf(stderr, "\n<MCU> = atmega32u4 | at90usb162 | at90usb646 | at90usb1286\n");
fprintf(stderr, "\nFor more information, please visit:\n");
fprintf(stderr, "Usage: usbSearch [-h] [-v] [-vid <vid>] [-pid <pid>]\n");
fprintf(stderr, "\t-vid <vid> : Search by Vendor ID\n");
fprintf(stderr, "\t-pid <pid> : Search by Product ID\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;
char* VIDstr;
char* PIDstr;

void do_silly_test(void);

//
void parse_options(int argc, char **argv)
Expand Down Expand Up @@ -83,7 +98,8 @@ int do_usbSearch(void)
sprintf( vidpidstr, "VID_%s&PID_%s", VIDstr, PIDstr);
}

if(verbose) printf("Searching for '%s'...\n", vidpidstr);
if(verbose)
printf("Searching for '%s'...\n", vidpidstr);

DISPATCH_OBJ(wmiSvc);
DISPATCH_OBJ(colDevices);
Expand All @@ -97,6 +113,10 @@ int do_usbSearch(void)
L".ExecQuery(%S)", L"Select * From Win32_USBControllerDevice");



if(0) do_silly_test();


int match_count = 0;

FOR_EACH(objDevice, colDevices, NULL) {
Expand All @@ -105,24 +125,24 @@ int do_usbSearch(void)
char* pch;

dhGetValue(L"%s", &str1, objDevice, L".Dependent");

if(verbose>1) printf("\n'%s'.\n\n", str1);
if(verbose>1) printf("'%s'.\n", str1);
pch = strtok( str1, "\"");
pch = strtok( NULL, "\"");
if(verbose>1) printf("'%s'\n", pch);
if(verbose>2) printf("'%s'\n", pch);

char* word;
int i;
int match = 0;
char vpstr[1000];
char vpstr[1000]; // FIXME: hack

// pos 0 = "USB", pos 1 = "VID_01234&PID=ABCD", pos 2 = serial number
for (word = strtok( pch, "\\" ), i=0; word;
word = strtok(NULL, "\\" ),i++ ) {
if( i==0 && strstr( word, "USB" ) != NULL ) { // match
match = 1;
}
if( i==1 && match==1 && strstr( word, vidpidstr ) != NULL ) { // match
if( i==1 && match==1 && strstr( word, vidpidstr ) != NULL ) {
match = 2;
strcpy(vpstr,word);
}
Expand All @@ -132,7 +152,7 @@ int do_usbSearch(void)
printf("Found VID:%s PID:%s serial:%s\n", vstr, pstr, word);
match_count++;
}
if(verbose>1) printf("%d:'%s'\n", i,word );
if(verbose>2) printf("%d:'%s'\n", i,word );
}

dhFreeString(str1);
Expand All @@ -144,7 +164,8 @@ int do_usbSearch(void)

dhUninitialize(TRUE);

if(verbose) printf("Found %d match%s\n", match_count,(match_count==1)?"":"es");
if(verbose)
printf("Found %d match%s\n", match_count,(match_count==1)?"":"es");
return 0;
}

Expand All @@ -160,3 +181,59 @@ int main(int argc, char **argv)
return 0;

}



void do_silly_test(void)
{
// could do something like this too, once you get a list of Dependents
// from Win32_USBControllerDevice
// see: http://msdn.microsoft.com/en-us/library/windows/desktop/aa387884(v=vs.85).aspx
// and: http://msdn.microsoft.com/en-us/library/windows/desktop/aa394353(v=vs.85).aspx
DISPATCH_OBJ(aDevice);
char *str2 = NULL;
// dhGetObject( L"winmgmts:\\\\BOKBOKXP\\root\\cimv2:Win32_PnPEntity.DeviceID=\"USBPRINT\\\\HPHP_COLOR_LASERJET_8500_PS\\\\6&10299E26&0&USB009\"",
// arduino diecimilia
dhGetObject( L"winmgmts:\\\\BOKBOKXP\\root\\cimv2:Win32_PnPEntity.DeviceID=\"USB\\\\VID_0403&PID_6001\\\\A6004CCF\"",
NULL, &aDevice );
dhGetValue(L"%s", &str2, aDevice, L".Caption");
printf("Caption:%s\n",str2);
dhGetValue(L"%s", &str2, aDevice, L".CreationClassName");
printf("CreationClassName:%s\n",str2);
dhGetValue(L"%s", &str2, aDevice, L".Description");
printf("Description:%s\n",str2);
dhGetValue(L"%s", &str2, aDevice, L".DeviceID");
printf("DeviceID:%s\n",str2);
dhGetValue(L"%s", &str2, aDevice, L".Name");
printf("Name:%s\n",str2);
dhGetValue(L"%s", &str2, aDevice, L".PNPDeviceID");
printf("PNPDeviceID:%s\n",str2);
dhGetValue(L"%s", &str2, aDevice, L".Status");
printf("Status:%s\n",str2);
dhGetValue(L"%s", &str2, aDevice, L".SystemCreationClassName");
printf("SystemCreationClassName:%s\n",str2);
dhGetValue(L"%s", &str2, aDevice, L".SystemName");
printf("SystemName:%s\n",str2);
//dhGetValue(L"%s", &str2, aDevice, L".HardwareID"); // no work arrays
//printf("HardwareID:%s\n",str2);
dhGetValue(L"%s", &str2, aDevice, L".Service");
printf("Service:%s\n",str2);

printf("\n\n");
}


/*
// getWmiStr:
// Helper function to create wmi moniker incorporating computer name.
static LPWSTR getWmiStr(LPCWSTR str)
{
static WCHAR szWmiStr[256];
wcscpy(szWmiStr, L"winmgmts:{impersonationLevel=impersonate}!\\\\");
wcsncat(szWmiStr, str, 128);
return szWmiStr;
}
*/
Binary file modified usbSearch.exe
100755 → 100644
Binary file not shown.

0 comments on commit 2fa7bf0

Please sign in to comment.