/*
usb_cnct.c
2008.10.04-2008.10.08
*/
#include "config.h"
#include <stdio.h>
#include <string.h>
#include <usb.h>
#define VENDOR_LIST "./usb.ids"
void check_list( FILE *,unsigned int,unsigned int);
////////////////////////////////////////////////////////////////////////////////
void check_list(FILE *fp, unsigned int vendor, unsigned int product)
{
char line_buf[256];
int i = 0;
unsigned int get_num = 0;
unsigned int vid_num = 0;
unsigned int pid_num = 0;
int v_flag = 0;
int p_flag = 0;
fseek(fp, 0L, SEEK_SET);
while ( fgets( line_buf, 255, fp ) != NULL )
{
//// Product ID
if ( v_flag == 1 )
{
pid_num = 0;
if ( ( line_buf[0] == '\t' ) &&
(( line_buf[1] >= '0' && line_buf[1] <= '9' ) ||
( line_buf[1] >= 'a' && line_buf[1] <= 'f' ) ))
{
for ( i = 0 ; i < 4 ; i++ )
{
if ( line_buf[i+1] >= '0' && line_buf[i+1] <= '9' )
{
get_num = (line_buf[i+1]-0x30) << (12-(i*4));
}
else if ( line_buf[i+1] >= 'a' && line_buf[i+1] <= 'f' )
{
get_num = (line_buf[i+1]-0x57) << (12-(i*4));
}
else
{
get_num = 0;
}
pid_num = pid_num + get_num;
}
if ( pid_num == product )
{
p_flag = 1;
printf(" Product Name:%s\n",line_buf+7);
break;
}
}
}
//// Vendor ID
vid_num = 0;
if (( line_buf[0] >= '0' && line_buf[0] <= '9' ) ||
( line_buf[0] >= 'a' && line_buf[0] <= 'f' ) )
{
for ( i = 0 ; i < 4 ; i++ )
{
if ( line_buf[i] >= '0' && line_buf[i] <= '9' )
{
get_num = (line_buf[i]-0x30) << (12-(i*4));
}
else if ( line_buf[i] >= 'a' && line_buf[i] <= 'f' )
{
get_num = (line_buf[i]-0x57) << (12-(i*4));
}
else
{
get_num = 0;
}
vid_num = vid_num + get_num;
}
if ( vid_num == vendor )
{
v_flag = 1;
printf(" Vendor Name :%s",line_buf+6);
}
}
}
if ( v_flag == 0 )
printf(" Vendor Name :--unknown--\n");
if ( p_flag == 0 )
printf(" Product Name:--unknown--\n\n");
}
////////////////////////////////////////////////////////////////////////////////
int main(void)
{
// for initialize
struct usb_bus *busses;
// get each bus data
struct usb_bus *bus;
struct usb_device *dev;
int ret_val;
FILE *fp;
int v_list_f = 1;
unsigned int vendor_id;
unsigned int product_id;
printf("==USB Conected Device Check==\n");
// initial and get all bus device
usb_init();
ret_val = usb_find_busses();
printf("usb_find_busses return value=[%d]\n",ret_val);
ret_val = usb_find_devices();
printf("usb_find_devices return value=[%d]\n\n",ret_val);
busses = usb_get_busses();
if ( (fp=fopen(VENDOR_LIST,"rt")) == NULL )
{
fprintf(stderr,"Not found Vendor List File:%s\n\n",VENDOR_LIST);
v_list_f = 0;
}
////////
printf("======== Conected Infomation ========\n");
for ( bus = busses ; bus ; bus = bus->next )
{
for ( dev = bus->devices ; dev ; dev = dev->next )
{
if ( dev->descriptor.idVendor != 0 )
{
printf("bus=[0x%x], ",bus);
printf("dev=[0x%x]\n",dev);
printf(" device file locate=[/dev/bus/usb/%s/%s]\n",bus->dirname,dev->filename);
vendor_id = (unsigned int)dev->descriptor.idVendor;
product_id = (unsigned int)dev->descriptor.idProduct;
printf(" Vendor ID =[0x%04x]",vendor_id);
printf(" Product ID=[0x%04x]\n",product_id);
////
if ( v_list_f == 1)
check_list(fp,vendor_id,product_id);
}
}
}
if ( v_list_f == 1 )
fclose(fp);
return 0;
}
|