示例#1
0
int main(int argc, char*argv[])
{
     int ret;
     GThread *rexy_thread;

     g_thread_init(NULL);
     gdk_threads_init();

     gtk_init(&argc, &argv);

     build_device();

     RalKernelBuffer = g_async_queue_new();

     if ( !RalKernelBuffer )
     {
	  printf("can't create message buffer\n");
	  return -1;
     }

     rexy_thread = g_thread_create( rexyThread, NULL, TRUE, NULL);

     gdk_threads_enter();
     gtk_main();
     gdk_threads_leave();

     g_thread_join( rexy_thread );
	
     return 0;
}
/**
 * Update topology tree
 * @author Dan Streetman
 */
JNIEXPORT jint JNICALL Java_com_ibm_jusb_os_linux_JavaxUsb_nativeTopologyUpdater
			( JNIEnv *env, jclass JavaxUsb, jobject linuxUsbServices, jobject connectedDevices, jobject disconnectedDevices )
{
	int busses, port, devices = 0;
	struct dirent **buslist;
	char *orig_dir = getcwd(NULL,0);

	jclass LinuxUsbServices = (*env)->GetObjectClass( env, linuxUsbServices );
	jfieldID usbRootHubImpID = (*env)->GetFieldID( env, LinuxUsbServices, "usbRootHubImp", "Lcom/ibm/jusb/UsbRootHubImp;" );
	jobject rootHub = (*env)->GetObjectField( env, linuxUsbServices, usbRootHubImpID );

	if (chdir(USBDEVFS_PATH) || (0 > (busses = scandir(".", &buslist, select_dirent_dir, alphasort))) ) {
		dbg( MSG_ERROR, "nativeTopologyUpdater : Could not access : %s\n", USBDEVFS_PATH );
		return -1;
	}

	for (port=0; port<busses; port++) {
		if (chdir(buslist[port]->d_name)) {
			dbg( MSG_ERROR, "nativeTopologyUpdater : Could not access %s/%s\n", USBDEVFS_PATH, buslist[port]->d_name );
		} else {
			struct dirent **devlist = NULL;
			int bus, hcAddress, devs;

			bus = atoi( buslist[port]->d_name );
			devs = scandir(".", &devlist, select_dirent_reg, alphasort);

			errno = 0;
			if (0 > devs) {
				dbg( MSG_ERROR, "nativeTopologyUpdater : Could not access device nodes in %s/%s : %s\n", USBDEVFS_PATH, buslist[port]->d_name, strerror(errno) );
			} else if (!devs) {
				dbg( MSG_ERROR, "nativeTopologyUpdater : No device nodes found in %s/%s\n", USBDEVFS_PATH, buslist[port]->d_name );
			} else {
				/* Hopefully, the host controller has the lowest numbered address on this bus! */
				hcAddress = atoi( devlist[0]->d_name );
				devices += build_device( env, JavaxUsb, linuxUsbServices, bus, hcAddress, rootHub, port, connectedDevices, disconnectedDevices );
			}

			while (0 < devs) free(devlist[--devs]);
			if (devlist) free(devlist);
		}
		chdir(USBDEVFS_PATH);
		free(buslist[port]);
	}
	free(buslist);

	(*env)->DeleteLocalRef( env, LinuxUsbServices );

	if (rootHub) (*env)->DeleteLocalRef( env, rootHub );

	if (orig_dir) {
		chdir(orig_dir);
		free(orig_dir);
	}

	return 0;
}
static inline int build_device( JNIEnv *env, jclass JavaxUsb, jobject linuxUsbServices, unsigned char bus, unsigned char dev,
	jobject parent, int parentport, jobject connectedDevices, jobject disconnectedDevices )
{
	int fd = 0, port, ncfg;
	int devices = 0;
	char node[4] = { 0, };
	char *path = (char*)getcwd( NULL, 0 );
	char *key = NULL;
	struct usbdevfs_ioctl *usbioctl = NULL;
	struct usbdevfs_hub_portinfo *portinfo = NULL;
	struct usbdevfs_connectinfo *connectinfo = NULL;
	struct jusb_device_descriptor *dev_desc;

	jobject device = NULL, existingDevice;
	jstring speedString = NULL, keyString = NULL;

	jclass LinuxUsbServices = (*env)->GetObjectClass( env, linuxUsbServices );
	jmethodID createUsbHubImp = (*env)->GetStaticMethodID( env, JavaxUsb, "createUsbHubImp", "(Ljava/lang/String;I)Lcom/ibm/jusb/UsbHubImp;" );
	jmethodID createUsbDeviceImp = (*env)->GetStaticMethodID( env, JavaxUsb, "createUsbDeviceImp", "(Ljava/lang/String;)Lcom/ibm/jusb/UsbDeviceImp;" );
	jmethodID configureUsbDeviceImp = (*env)->GetStaticMethodID( env, JavaxUsb, "configureUsbDeviceImp", "(Lcom/ibm/jusb/UsbDeviceImp;BBBBBBBBBBSSSSLjava/lang/String;)V" );
	jmethodID checkUsbDeviceImp = (*env)->GetMethodID( env, LinuxUsbServices, "checkUsbDeviceImp", "(Lcom/ibm/jusb/UsbHubImp;ILcom/ibm/jusb/UsbDeviceImp;Ljava/util/List;Ljava/util/List;)Lcom/ibm/jusb/UsbDeviceImp;" );

	if (!path) {
		dbg( MSG_ERROR, "nativeTopologyUpdater.build_device : Could not get current directory!\n" );
		goto BUILD_DEVICE_EXIT;
	}

	key = malloc(strlen(path) + 5);
	sprintf( key, "%s/%3.03d", path, dev );
	keyString = (*env)->NewStringUTF( env, key );

	dbg( MSG_DEBUG2, "nativeTopologyUpdater.build_device : Building device %s\n", key );

	sprintf( node, "%3.03d", dev );
	fd = open( node, O_RDWR );
	if ( 0 >= fd ) {
		dbg( MSG_ERROR, "nativeTopologyUpdater.build_device : Could not access %s\n", key );
		goto BUILD_DEVICE_EXIT;
	}

	if (!(dev_desc = get_descriptor( fd ))) {
		dbg( MSG_ERROR, "nativeTopologyUpdater.build_device : Short read on device descriptor\n" );
		goto BUILD_DEVICE_EXIT;
	}

	if (dev_desc->bDeviceClass == USB_CLASS_HUB) {
		usbioctl = malloc(sizeof(*usbioctl));
		portinfo = malloc(sizeof(*portinfo));
		usbioctl->ioctl_code = USBDEVFS_HUB_PORTINFO;
		usbioctl->ifno = 0;
		usbioctl->data = portinfo;
		errno = 0;
		if (0 >= ioctl( fd, USBDEVFS_IOCTL, usbioctl )) {
			dbg( MSG_ERROR, "nativeTopologyUpdater.build_device : Could not get portinfo from hub, error = %d\n", errno );
			goto BUILD_DEVICE_EXIT;
		} else {
		  dbg( MSG_DEBUG2, "nativeTopologyUpdater.build_device : Device is hub with %d ports\n",portinfo->nports );
		}
		free(usbioctl);
		usbioctl = NULL;
		device = (*env)->CallStaticObjectMethod( env, JavaxUsb, createUsbHubImp, keyString, portinfo->nports );
	} else {
	  device = (*env)->CallStaticObjectMethod( env, JavaxUsb, createUsbDeviceImp, keyString );
	}

	connectinfo = malloc(sizeof(*connectinfo));
	errno = 0;
	if (ioctl( fd, USBDEVFS_CONNECTINFO, connectinfo )) {
		dbg( MSG_ERROR, "nativeTopologyUpdater.build_device : Could not get connectinfo from device, error = %d\n", errno );
		goto BUILD_DEVICE_EXIT;
	} else {
	  dbg( MSG_DEBUG2, "nativeTopologyUpdater.build_device : Device speed is %s\n", (connectinfo->slow?"1.5 Mbps":"12 Mbps") );
	}
	speedString = (*env)->NewStringUTF( env, ( connectinfo->slow ? "1.5 Mbps" : "12 Mbps" ) );
	free(connectinfo);
	connectinfo = NULL;

	(*env)->CallStaticVoidMethod( env, JavaxUsb, configureUsbDeviceImp, device, 
		dev_desc->bLength, dev_desc->bDescriptorType,
		dev_desc->bDeviceClass, dev_desc->bDeviceSubClass, dev_desc->bDeviceProtocol,
		dev_desc->bMaxPacketSize0, dev_desc->iManufacturer, dev_desc->iProduct, dev_desc->iSerialNumber,
		dev_desc->bNumConfigurations, dev_desc->idVendor, dev_desc->idProduct,
		dev_desc->bcdDevice, dev_desc->bcdUSB, speedString );
	(*env)->DeleteLocalRef( env, speedString );
	speedString = NULL;

	/* Build config descriptors */
	for (ncfg=0; ncfg<dev_desc->bNumConfigurations; ncfg++) {
		if (build_config( env, JavaxUsb, fd, device, bus, dev )) {
			dbg( MSG_ERROR, "nativeTopologyUpdater.build_device : Could not get config %d for device %d\n", ncfg, dev );
			goto BUILD_DEVICE_EXIT;
		}
	}

	existingDevice = (*env)->CallObjectMethod( env, linuxUsbServices, checkUsbDeviceImp, parent, parentport+1, device, connectedDevices, disconnectedDevices );
	(*env)->DeleteLocalRef( env, device );
	device = existingDevice;

	/* This device is set up and ready! */
	devices = 1;
	close( fd );
	fd = 0;

	if ((dev_desc->bDeviceClass == USB_CLASS_HUB) && portinfo)
		for (port=0; port<(portinfo->nports); port++)
			if (portinfo->port[port]) {
				dbg( MSG_DEBUG2, "nativeTopologyUpdater.build_device : Building device attached to port %d\n", portinfo->port[port]);
				devices += build_device( env, JavaxUsb, linuxUsbServices, bus, portinfo->port[port], device, port, connectedDevices, disconnectedDevices );
			}

BUILD_DEVICE_EXIT:
	if (fd) close(fd);
	if (device) (*env)->DeleteLocalRef( env, device );
	if (connectinfo) free(connectinfo);
	if (dev_desc) free(dev_desc);
	if (usbioctl) free(usbioctl);
	if (portinfo) free(portinfo);
	if (keyString) (*env)->DeleteLocalRef( env, keyString );
	if (speedString) (*env)->DeleteLocalRef( env, speedString );
	if (path) free(path);
	if (key) free(key);

	return devices;
}