usb 测试程序 mount 失败
本帖最后由 barca1202 于 2012-11-11 15:09:42 编辑 大家好,
我写了个usb的测试程序,目的是进行以下一些测试,attach/detach, mount/unmount, opendir/readdir/closedir, open file/read file/close file. 运行程序可以检测到 attach/detach, 但是mount失败。想请教一下问题出在哪里,怎么改正,谢谢各位。附上代码。
/*........................... FILE PROLOGUE ..........................*/
/*
.FP
***********************************************************************
*
* FILE NAME:
* usb_test_case.c
*
* PURPOSE:
* This file is used to verify that the API calls used to access and control the USB drive.
* DLE can use to determine when the USB drive has been detached during use.
***********************************************************************
.FP END
*/
/* ------------------------------ FILE INCLUSION ----------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h> /* Standard types */
#include <stdint.h> /* for integer types*/
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sched.h>
#include <fcntl.h>
#include <sys/file.h>
#include <time.h>
#include <usb.h>
#include <unistd.h>
#include <dirent.h>
#include <io.h>
#include <unistd.h>
#include <errno.h>
static char* blocknode = "/dev/ddev/sdusb";
static char* charnode = "/dev/ddev/usb.ctrl";
static char* mountpoint_1 = "/tmp/tftproot";
static char* mountpoint = "/tmp/tftproot/usb";
static int fh1;
static unsigned int bytesread;
void wait_for_event(int read_fd);
void walk_dir(const char *file);
void access_read(const char *file);
void open_file(void);
void read_file(void);
void close_file(void);
#define SCSI_DRIVER_MOUNT 1
#define SUC 0
#define ERR 1
/*
*******************************************************************************
-*
-* NAME:
-* wait_for_event
-*
-* PURPOSE:
-* This function is used to discover if any changes has happened on the USB port
-* or not and read the events generated by USB driver.
-*
-* PARAMETERS:
-*
-* INPUT: int read_fd - USB character device file handle
-*
-* OUTPUT: None
-*
-* RETURNS: None
-*
-* NOTES: None
-*
*******************************************************************************
*/
void wait_for_event(int read_fd)
{
usb_event *event = NULL;
int32_t return_read;
int no_event = 1;
printf("Entered into thread\n");
event = (usb_event *)malloc(sizeof(usb_event));
if (NULL == event)
{
printf("ERROR: No memory allocated for reading usb events\n");
exit(1);
}
while(no_event)
{
memset(event, 0, sizeof(usb_event));
return_read = read(read_fd, (char *)event, sizeof(usb_event));
//is this an event?
if (return_read == sizeof(usb_event))
{
printf("Event read is : %d\n", event->ue_type);
if (USB_EVENT_DEVICE_ATTACH == event->ue_type)
{
printf("USB Device is attached.\n");
if( (USB_SPEED_HIGH == event->ue_device.udi_speed) &&
(UDCLASS_MASS == event->ue_device.udi_class) &&
(UISUBCLASS_SCSI == event->ue_device.udi_subclass) )
{
no_event = 0;
}
}
else if(USB_EVENT_DEVICE_NOT_SUPPORTED == event->ue_type)
{
printf("Connected USB device is not supported.\n");
}
else
{
printf("USB Device is detached.\n");
no_event = 0;
}
}
/* Wait for 25ms before calling read system call again */
usleep(25);
}
}
/*
*******************************************************************************
-*
-* NAME:
-* walk_dir
-*
-* PURPOSE:
-* This function is used to walk directory tree on mounted filesystem
-*
-* PARAMETERS:
-*
-* INPUT: const char *file - USB mountpoint
-*
-* OUTPUT: None
-*
-* RETURNS: None
-*
-* NOTES: None
-*
*******************************************************************************
*/
void walk_dir(const char *file)
{
struct dirent *d;
DIR *dir;
struct stat fileattrib;
access_read(file);
if ((dir = opendir(file)) == NULL)
{
printf("opendir %s failed\n", file);
exit(1);
}
else
{
printf("opendir %s successfully\n", file);
}
errno = 0;
/* struct dirent *readdir(DIR *dirp); */
while ((d = readdir(dir)) != NULL)
{
printf("%s\n", d->d_name);
/* stat returns 0 if file exists */
if (stat(d->d_name, &fileattrib) == 0)
{
printf("Access Permissions of file: %s\n ", d->d_name);
printf("----------------------------\n");
printf("%d\n", fileattrib.st_mode);
}
}
if (errno != 0)
{
printf("readdir failed");
exit(1);
}
closedir(dir);
}
/*
*******************************************************************************
-*
-* NAME:
-* access_read
-*
-* PURPOSE:
-* This function is used to determine whether the specified file or directory exists and
-* can be accessed as read mode
-*
-* PARAMETERS:
-*
-* INPUT: const char *file - directory or file name
-*
-* OUTPUT: None
-*
-* RETURNS: None
*******************************************************************************
*/
void access_read(const char *file)
{
if ((access(file, 0)) != -1)
{
printf("%s exists\n", file);
/* check for read permission */
if ((access(file, R_OK)) == 0)
{
printf("%s can read\n", file);
}
else
{
printf(("%s can not read\n"), file);
exit(1);
}
}
else
{
printf("%s does not exist\n", file);
exit(1);
}
}
/*
*******************************************************************************
-*
-* NAME:
-* open_file
-*
-* PURPOSE:
-* This function is used to open file on mounted filesystem
-*
-* PARAMETERS:
-*
-* INPUT: none
-*
-* RETURNS: None
*******************************************************************************
*/
void open_file(void)
{
char s[100];
printf("Input the file path and name you want to open:\n");
scanf("%s",s);
fh1 = open(s, O_RDONLY);
if (fh1 == -1)
{
printf("Open failed on input file\n");
}
else
{
printf("Open succeeded on input file\n");
}
}
/*
*******************************************************************************
-*
-* NAME:
-* read_file
-*
-* PURPOSE:
-* This function is used to read file on mounted filesystem
-*
-* PARAMETERS:
-*
-* INPUT: none
-*
-* RETURNS: None
*******************************************************************************
*/
void read_file(void)
{
char buffer[60000];
unsigned int nbytes = 60000;
unsigned int bytesread;
if (fh1 == -1)
{
printf("Open failed on input file\n");
exit(1);
}
/* Read in input: */
if ((bytesread = read(fh1, buffer, nbytes)) <= 0)
{
printf("Problem reading file\n");
exit(1);
}
else
{
printf("Read %u bytes from file\n", bytesread);
}
}
/*
*******************************************************************************
-*
-* NAME:
-* close_file
-*
-* PURPOSE:
-* This function is used to close file on mounted filesystem
-*
-* PARAMETERS:
-*
-* INPUT: none
-*
-* RETURNS: None
*******************************************************************************
*/
void close_file(void)
{
if (fh1 == -1 || bytesread <= 0)
{
printf("Open failed on input file or reading file failed");
exit(1);
}
/* Read in input: */
if (close(fh1) == -1)
{
printf("Close file failed\n");
exit(1);
}
else
{
printf("File closed\n");
}
}
/*
.CP
*******************************************************************************
-* NAME:
-* main
-*
-* PURPOSE:
-* Main function.
-*
-* PARAMETERS:
-*
-* INPUT: int argc - Number of command line arguments
-* char **argv - List of command line arguments
-*
-* OUTPUT: None
-*
-* RETURNS: int - SUC : Success
-* ERR : Error
-*
-* NOTES: None
-*
*******************************************************************************
.CP END
*/
int main()
{
int fd = 0;
char c;
int status;
if(mkdir(mountpoint_1, 0700))
{
if(EEXIST != errno)
{
printf("ERROR: %s : Could not create mount point\n", mountpoint_1);
return ERR;
}
}
if(mkdir(mountpoint, 0700))
{
if(EEXIST != errno)
{
printf("ERROR: %s : Could not create mount point\n", mountpoint);
return ERR;
}
}
fd = open(charnode, O_RDWR);
if (-1 == fd )
{
printf("ERROR: %s : Device open failed\n", charnode);
return ERR;
}
do
{
printf("0 - EXIT\n");
printf("1 - WAIT FOR EVENT\n");
printf("2 - MOUNT R/W W/IOCTL\n");
printf("3 - MOUNT R/O W/IOCTL\n");
printf("4 - UNMOUNT W/IOCTL\n");
printf("5 - WALK DIRECTORY TREE\n");
printf("6 - OPEN A FILE FOR READING\n");
printf("7 - READ FROM A FILE\n");
printf("8 - CLOSE AN OPEN FILE\n");
printf(": \n");
scanf("%c", &c);
switch(c)
{
case '0':
break;
case '1':
wait_for_event(fd);
break;
case '2':
if(!(status = ioctl(fd, USB_SCSI_MOUNT_RDWR, blocknode)))
{
printf("MOUNTED RW!\n");
}
else
{
printf("ERROR MOUNTING: %d errno: %d\n", status, errno);
}
break;
case '3':
if(!(status = ioctl(fd, USB_SCSI_MOUNT_RDONLY, blocknode)))
{
printf("MOUNTED RO!\n");
}
else
{
printf("ERROR MOUNTING: %d errno: %d\n", status, errno);
}
break;
case '4':
if(!(status = ioctl(fd, USB_SCSI_UNMOUNT, NULL)))
{
printf("UNMOUNTED!\n");
}
else
{
printf("ERROR UNMOUNTING: %d errno: %d\n", status, errno);
}
break;
case '5':
walk_dir(mountpoint);
break;
case '6':
open_file();
break;
case '7':
read_file();
break;
case '8':
close_file();
break;
default:
printf("UNKNOWN COMMAND\n");
break;
}
}
while(c != '0');
close(fd);
return SUC;
}
[解决办法]
把dmesg的输出贴出来看看。