|
From: <la...@no...> - 2010-12-07 10:41:50
|
/**
*This program is used to check whether a device is mtp device, it use the infomation exported to sysfs to do this check
*argument:the device path under sysfs for this device
*return:0 means it is a mtp-device, otherwise, return 1;
*usage:mtp_probe devicepath pattern
*'devicepath' is the path for the device under sysfs, 'pattern' is the pattern used to match against
*for eg,mtp_probe /sys/devices/pci0000:00/0000:00:1d.7/usb1/1-8 mtp
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <unistd.h>
#include <string.h>
#define PATH_LENGTH 256
#define LINE_LENGTH 1024
#define ENABLE_LOG
#ifdef ENABLE_LOG
void printlog (const char *format, ...)
{
FILE *fp;
fp = fopen("/tmp/probe.txt","a");
if(NULL != fp)
{
va_list args;
va_start (args, format);
vfprintf(fp,format,args);
fprintf(fp,"\n");
fflush(fp);
fclose(fp);
va_end (args);
}
}
#else
void printlog (const char *format, ...)
{
}
#endif
/**
This function check the interface directory for the interface of a device under sysfs,
to see if this device is a mtp device. The check is based on following criteria:
1. has string descriptor with the file name 'interface';
2. the string descriptor contains the specified pattern, indicated by argument 'pattern'
return 1 if above criteria meets, thus it is a mtp device, otherwise, return 0.
*/
int check_interface_subdir(const char* dirname, const char* pattern)
{
const char str_descriptor[] = "interface"; //the filename under sysfs for interface string descriptor
DIR *dir = NULL;
struct dirent *entry = NULL;
char filename[PATH_LENGTH];
struct stat filestat;
char *buf = NULL; //buf is used to hold the whole contents of the file 'interface'
char *temp = NULL; //used to hold the content read each time by 'fgets'
FILE *fp = NULL;
int ret = 0;//return value
dir = opendir(dirname);
if (!dir) {
printlog("fail to open interface dir:%s\n",dirname);
return 0;
}
while ((entry = readdir(dir)) != NULL) {
if ((strcmp(entry->d_name,str_descriptor)) != 0) {
continue;
}
printlog("find interface file\n");
snprintf(filename,sizeof(filename),"%s/%s",dirname,entry->d_name);
printlog("interface filename:%s\n",filename);
if ((stat(filename,&filestat)) == 0) {
int filesize = filestat.st_size;
printlog("filesize:%d\n",filesize);
if (filesize > 0) {
fp = fopen(filename,"r");
if (!fp) {
printlog("fail to open file to read\n");
break;
}
buf = (char *)malloc(filesize+1);
if (!buf) {
printlog("fail to alloc memory to read file\n");
fclose(fp);
break;
}
memset(buf,0,filesize+1);
temp = (char *)malloc(LINE_LENGTH);
if (!temp) {
printlog("fail to alloc memory for temp\n");
free(buf);
fclose(fp);
break;
}
memset(temp,0,LINE_LENGTH);
while((fgets(temp,LINE_LENGTH,fp)) != NULL) {
strncat(buf,temp,strlen(temp));
}
printlog("read out the whole contents:%sstrlen:%d\n",buf,strlen(buf));
if((strcasestr(buf,pattern)) != NULL) {
printlog("find the pattern\n");
ret = 1;
} else {
printlog("not find the pattern\n");
}
fclose(fp);
free(buf);
free(temp);
} //end of if (size > 0)
} else {
printlog("fail to get stat of file:\n");
} //end of if ((stat(filename,&filestat)) == 0)
break;
} //end of while ((entry = readdir(dir)) != NULL)
closedir(dir);
return ret;
}
int main(int argc, char** argv)
{
DIR *dir = NULL;
struct dirent *entry = NULL;
int found = 0;
if (argc < 3) {
printlog("Usage:check-mtp-device devicepath pattern\n");
exit(1);
}
printlog("the device path:%s, pattern:%s \n",argv[1],argv[2]);
dir = opendir(argv[1]);
if (!dir) {
printlog("fail to open the dir for device,exit\n");
return 1;
}
while ((entry = readdir(dir)) != NULL) {
//Skip directory which begins with '.'
if (entry->d_type == DT_DIR && entry->d_name[0] != '.') {
char interfacedir[PATH_LENGTH];
//TODO check if the name of directory be consistent with the pattern of interface directory
//usb sysfs naming scheme for an interface is like "hubno-portno:configno.interface",eg,"1-8:1.0"
//currently, just check whether it contains the character ':'
if((strchr(entry->d_name,':')) == NULL) {
printlog("skip the dir entry:%s\n",entry->d_name);
continue;
}
snprintf(interfacedir, sizeof(interfacedir),"%s/%s",argv[1],entry->d_name);
printlog("interfacedir to check:%s\n",interfacedir);
found = check_interface_subdir(interfacedir,argv[2]);
if (found) {
printlog("find the mtp interface\n");
break;
} else {
continue;
}
} else {
printlog("skip the entry:%s\n",entry->d_name);
continue;
}
} //end of while ((entry = readdir(dir)) != NULL)
closedir(dir);
if (found) {
printlog("found mtp device\n");
return 0;
} else {
printlog("not found mtp device \n");
return 1;
}
}
|