#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <gtk/gtk.h>
#include "callbacks.h"
#include "interface.h"
#include "support.h"
////
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// for getpid()
#include <sys/types.h>
#include <unistd.h>
#include "callbacks.h"
#include "interface.h"
#include "support.h"
// libusb
#include <usb.h>
////////
#define VENDOR_LIST "./usb.ids"
gchar Rt_buff[4096];
////////////////////////////////////////////////////////////////////////////////
void
on_file_f_1_activate (GtkMenuItem *menuitem,
gpointer user_data)
{
}
////////////////////////////////////////////////////////////////////////////////
void
on_exit_e_1_activate (GtkMenuItem *menuitem,
gpointer user_data)
{
gtk_main_quit();
}
////////////////////////////////////////////////////////////////////////////////
void
set_text_view_disp_point(gchar *command )
{
gchar tmp_file[64];
FILE *ftmp;
pid_t this_pid;
gchar command_save[256]; //
gchar line_buff[256]; //
gint line=0;
// make tmp file name
this_pid = getpid();
sprintf(tmp_file, "./gtn%d.tmp",this_pid);
sprintf
(
command_save,
"%s > %s",
command,tmp_file
);
system(command_save);
ftmp = fopen(tmp_file,"r");
line = 0;
while( fgets(line_buff,256,ftmp) != NULL )
{
line++;
if ( line == 1 )
strcpy(Rt_buff,line_buff);
else
strcat(Rt_buff,line_buff);
}
fclose(ftmp);
remove(tmp_file);
}
////////////////////////////////////////////////////////////////////////////////
void
on_more_dev_button_clicked (GtkButton *button,
gpointer user_data)
{
gchar command[256]; //
gint slen = 0;
GtkTextBuffer *gtvbuffer;
GtkTextView *more_textview = (GtkTextView*)lookup_widget ( GTK_WIDGET(button), "more_textview") ;
gtvbuffer = gtk_text_view_get_buffer(more_textview);
// clear buffer
gtk_text_buffer_set_text(gtvbuffer,"",0);
sprintf
(
command,
"cat /proc/bus/usb/devices"
);
set_text_view_disp_point(command);
slen = strlen(Rt_buff);
gtk_text_buffer_set_text(gtvbuffer,Rt_buff,slen);
}
////////////////////////////////////////////////////////////////////////////////
void
on_lsusb_button_clicked (GtkButton *button,
gpointer user_data)
{
gchar command[256]; //
gint slen = 0;
GtkTextBuffer *gtvbuffer;
GtkTextView *lsusb_textview = (GtkTextView*)lookup_widget ( GTK_WIDGET(button), "lsusb_textview") ;
gtvbuffer = gtk_text_view_get_buffer(lsusb_textview);
// clear buffer
gtk_text_buffer_set_text(gtvbuffer,"",0);
sprintf
(
command,
"/sbin/lsusb"
);
system(command);
set_text_view_disp_point(command);
slen = strlen(Rt_buff);
gtk_text_buffer_set_text(gtvbuffer,Rt_buff,slen);
}
////////////////////////////////////////////////////////////////////////////////
// check list
////////////////////////////////////////////////////////////////////////////////
void check_list(FILE *fp, FILE *ftmp, 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;
fprintf(ftmp, " 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;
fprintf(ftmp, " Vendor Name :%s",line_buf+6);
}
}
}
if ( v_flag == 0 )
fprintf(ftmp, " Vendor Name :--unknown--\n");
if ( p_flag == 0 )
fprintf(ftmp, " Product Name:--unknown--\n\n");
}
////////////////////////////////////////////////////////////////////////////////
// view altsetting
////////////////////////////////////////////////////////////////////////////////
void view_endpoint(struct usb_endpoint_descriptor *endpoint, FILE *ftmp)
{
fprintf(ftmp, " +bEndpointAddress =[0x%x]\n", endpoint->bEndpointAddress);
}
////////////////////////////////////////////////////////////////////////////////
// view altsetting
////////////////////////////////////////////////////////////////////////////////
void view_altsetting(struct usb_interface_descriptor *altsetting, FILE *ftmp)
{
int i = 0;
fprintf(ftmp, " +bLength =[0x%x]\n", altsetting->bLength);
fprintf(ftmp, " +bInterfaceNumber =[0x%x]\n", altsetting->bInterfaceNumber);
fprintf(ftmp, " +bAlternateSetting =[0x%x]\n", altsetting->bAlternateSetting);
fprintf(ftmp, " +bNumEndpoints =[%d]\n", altsetting->bNumEndpoints);
for ( i = 0; i < altsetting->bNumEndpoints ; i++ )
{
view_endpoint(&altsetting->endpoint[i], ftmp);
}
}
////////////////////////////////////////////////////////////////////////////////
// view inferface
////////////////////////////////////////////////////////////////////////////////
void view_interface(struct usb_interface *interface, FILE *ftmp)
{
int i = 0;
fprintf(ftmp, " +num_altsetting =[%d]\n", interface->num_altsetting);
for ( i = 0; i < interface->num_altsetting ; i++ )
{
view_altsetting(&interface->altsetting[i], ftmp);
}
}
////////////////////////////////////////////////////////////////////////////////
// view config
////////////////////////////////////////////////////////////////////////////////
void view_config(struct usb_config_descriptor *config, FILE *ftmp)
{
int i = 0;
fprintf(ftmp, " +bConfigurationValue=[0x%x]\n",config->bConfigurationValue);
fprintf(ftmp, " +MaxPower =[%d]\n", config->MaxPower);
fprintf(ftmp, " +bNumInterfaces =[0x%x]\n", config->bNumInterfaces);
for ( i = 0; i < config->bNumInterfaces ; i++ )
{
view_interface(&config->interface[i], ftmp);
}
}
////////////////////////////////////////////////////////////////////////////////
void
on_libusb_button_clicked (GtkButton *button,
gpointer user_data)
{
// for initialize
struct usb_bus *busses;
// get each bus data
struct usb_bus *bus;
struct usb_device *dev;
gint ret_busses = 0;
gint ret_devices = 0;
gchar tmp_file[64];
FILE *fp, *ftmp;
int v_list_f = 1;
unsigned int vendor_id;
unsigned int product_id;
int i = 0;
pid_t this_pid;
gchar line_buff[256]; //
gint line=0;
gint slen = 0;
GtkTextBuffer *gtvbuffer;
GtkTextView *libusb_textview = (GtkTextView*)lookup_widget ( GTK_WIDGET(button), "libusb_textview") ;
// temp file open
// make tmp file name
this_pid = getpid();
sprintf(tmp_file, "./gtn%d.tmp",this_pid);
ftmp = fopen(tmp_file,"w");
usb_init();
ret_busses = usb_find_busses();
ret_devices = usb_find_devices();
fprintf(ftmp, "usb_find_busses return value=[%d]\n",ret_busses);
fprintf(ftmp, "usb_find_devices return value=[%d]\n\n",ret_devices);
busses = usb_get_busses();
if ( (fp=fopen(VENDOR_LIST,"rt")) == NULL )
{
fprintf(ftmp, "Not found Vendor List File:%s\n\n",VENDOR_LIST);
v_list_f = 0;
}
////////
fprintf(ftmp, "======== Conected Infomation ========\n");
for ( bus = busses ; bus ; bus = bus->next )
{
for ( dev = bus->devices ; dev ; dev = dev->next )
{
if ( dev->descriptor.idVendor != 0 )
{
fprintf(ftmp, "bus=[0x%x], ",bus);
fprintf(ftmp, "dev=[0x%x]\n",dev);
fprintf(ftmp, " device file locate=[/dev/bus/usb/%s/%s]\n",bus->dirname,dev->filename);
fprintf(ftmp, " bNumConfigurations=[0x%x]\n",dev->descriptor.bNumConfigurations);
for ( i = 0; i < dev->descriptor.bNumConfigurations ; i++ )
{
view_config(&dev->config[i], ftmp);
}
vendor_id = (unsigned int)dev->descriptor.idVendor;
product_id = (unsigned int)dev->descriptor.idProduct;
fprintf(ftmp, " Vendor ID =[0x%04x]",vendor_id);
fprintf(ftmp, " Product ID=[0x%04x]\n",product_id);
////
if ( v_list_f == 1)
check_list(fp, ftmp, vendor_id,product_id);
}
}
}
if ( v_list_f == 1 )
fclose(fp);
fclose(ftmp);
gtvbuffer = gtk_text_view_get_buffer(libusb_textview);
// clear buffer
gtk_text_buffer_set_text(gtvbuffer,"",0);
//// read tmp file
ftmp = fopen(tmp_file,"r");
line = 0;
while( fgets(line_buff,256,ftmp) != NULL )
{
line++;
if ( line == 1 )
strcpy(Rt_buff,line_buff);
else
strcat(Rt_buff,line_buff);
}
fclose(ftmp);
remove(tmp_file);
slen = strlen(Rt_buff);
gtk_text_buffer_set_text(gtvbuffer,Rt_buff,slen);
}
////////////////////////////////////////////////////////////////////////////////
void
on_exit_button_clicked (GtkButton *button,
gpointer user_data)
{
gtk_main_quit();
}
////////////////////////////////////////////////////////////////////////////////
void
on_main_window_destroy (GtkObject *object,
gpointer user_data)
{
gtk_main_quit();
}
//////////////////////////////////////EOF///////////////////////////////////////
|