Find the answer to your Linux question:
Results 1 to 8 of 8
Hi guys, I want to find a simple and quick way to permit my application to bring a file located in my file system and write it into an external ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined! s1rUK's Avatar
    Join Date
    Jun 2010
    Location
    Milan
    Posts
    5

    How to transmit a whole file by source-code to USB drive


    Hi guys,

    I want to find a simple and quick way to permit my application to bring a file located in my file system and write it into an external USB device. I have to use Bulk Transfer Only mode that means to use stream of characters only. I looked through the LibUsb library using it in my IDE I can transmit a string.

    To transfer a whole file I suppose I've to dodge the file system building a char sequence that permits to recognise a file in the file system and to put it into the usb device.

    Suggestions? Cheers..

  2. #2
    Just Joined!
    Join Date
    Dec 2009
    Location
    California
    Posts
    98
    I'm a bit confused. Does the USB device have a file system on it? I don't understand the Bulk Transfer comment. You just want to read a file from your file system and put that file on the USB device which contains a file system?
    How about:
    system("cp /tmp/foobar /mnt/foobar");

  3. #3
    Just Joined!
    Join Date
    Apr 2005
    Location
    Central Florida
    Posts
    12
    Have a look here.
    www_maxim-ic.com/quick_view2.cfm/qv_pk/3639

  4. $spacer_open
    $spacer_close
  5. #4
    Just Joined! s1rUK's Avatar
    Join Date
    Jun 2010
    Location
    Milan
    Posts
    5
    Damn! With system( "sudo /bin/cp -p ..." ) it works!

    Thank you guys!

  6. #5
    Just Joined! s1rUK's Avatar
    Join Date
    Jun 2010
    Location
    Milan
    Posts
    5
    As a requirement of my task I have to work in the Bulk-Transfer-Only that means using stream to communicate between host and USB mass storage.

    At beginning I gleaned to create a stream of packets and for each of'em I had to assign a header to describe my request to usb device. In BTO is permitted only stream, like all types of USB trasmission except the Control Transfer.

    I wonder if I've really solved my problem with a system call?

  7. #6
    Just Joined!
    Join Date
    Aug 2006
    Location
    North Dakota, USA
    Posts
    14
    It seems like there is more to your request than what is written--either that or it is overly-complicated. What is your task?

    From the explanation, it seems like your application is trying to do standard filesystem stuff but somehow trying to avoid the filesystem... why? If the system that the application is running on is always going to be Linux, the application should be allowing Linux to mount the drive and then do all the filesystem operations that way. If it is not always going to be Linux, then it should *definitely* let the operating system mount the usb filesystem before doing anything to it.

  8. #7
    Just Joined! s1rUK's Avatar
    Join Date
    Jun 2010
    Location
    Milan
    Posts
    5
    @slaughter, it's possible enables an user to claim an interface against the Linux Kernel and then operate to it, i.e. get information of USB device, send a stream of characters.

    A method of LibUsb permits to detach a kernel driver from the interface specified, as follows:

    Code:
    int usb_detach_kernel_driver_np(usb_dev_handle *dev, int interface);
    The following part of code is not used when I'm using a system call with system(command)

    Code:
    		if (LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP)
    			{
    				//Detach kernel driver (USBHID) from device interface. (Linux hack)
    				usb_detach_kernel_driver_np(handle, 0);
    				usb_detach_kernel_driver_np(handle, 1);
    			}
    
    			if (usb_set_configuration(handle, 1) == 0)
    			{
    				printf("set configuration to 1.\n");
    				int setconf = 0;
    				setconf = usb_set_configuration(handle, 1);
    				printf("my configuration: %d \n", setconf);
    			}
    			else
    			{
    				printf("failed setting configuration to 1.\n");
    				exit(0);
    			}
    
    			//claim USB interface
    			if (usb_claim_interface(handle, 0) == 0)
    			{
    				printf("claiming interface 0.\n");
    				int claim = 0;
    				claim = usb_claim_interface(handle, 0);
    				printf("my claim: %d \n", claim);
    			}
    			else
    			{
    				printf("failed claiming interface 0.\n");
    				exit(0);
    			}
    
    if (usb_set_altinterface(handle, 0) == 0)
    			{
    				printf("set altinterface 0.\n");
    				int setalter = 0;
    				setalter = usb_set_altinterface(handle, 0);
    				printf("setalter mio : %d \n", setalter);
    			} else
    			{
    				printf("failed setting altinterface 0.\n");
    				exit(0);
    			}
    
    
    			FILE *pf;
    			int a;
    			int size;
    
    			pf = fopen("OutputReport.txt", "r");
    
    			fseek(pf, 0, SEEK_END);
    			size = ftell(pf);
    			
    			char str[size];
    			char *tmp = str;
    			if (pf) 
                            {
    				fseek(pf, 0, SEEK_SET);
    				while (!feof(pf)) 
                                    {
    					tmp = fgetc(pf);
    					printf("%c", (int) tmp);
    					tmp++;
    
    				}
    				fclose(pf);
    			} 
                            else 
                            {
    				printf("Error opening file. \n");
    			}
    
    			//Stream Tx in Byte
    			int bulk = usb_bulk_write(handle, 0x02, str, size, 512);
    
    			printf("My Bulk: %d \n", bulk);
    
    			usb_release_interface(handle, 0);
    			usb_close(handle);
    			printf("released and closed device\n");
    		}
    	}
    I would like to transmit a sequence of characters where I pass a whole file read somewhere in my file system, like the copy command using a system call.

  9. #8
    Just Joined!
    Join Date
    Apr 2005
    Location
    Central Florida
    Posts
    12
    Have you tried using dd ?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •