您的位置:首页 > 运维架构 > Linux

dnw for linux: Ubuntu下可用,无需编译驱动,mini2440可用

2013-09-18 22:37 405 查看
1.安装所需库文件sudoapt-getinstalllibusb-dev

2.源代码如下

/*dnw2linuxmainfile.Thisdependsonlibusb.
*
*Author:Fox<hulifox008@163.com>
*License:GPL
*
*/

#include<stdio.h>
#include<usb.h>
#include<errno.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>

#defineQQ2440_SECBULK_IDVENDOR		0x5345
#defineQQ2440_SECBULK_IDPRODUCT	0x1234

structusb_dev_handle*open_port(){
structusb_bus*busses,*bus;

usb_init();
usb_find_busses();
usb_find_devices();

busses=usb_get_busses();
for(bus=busses;bus;bus=bus->next){
structusb_device*dev;
for(dev=bus->devices;dev;dev=dev->next){
printf("idVendor:0x%x\t,ipProduct:0x%x\n",
dev->descriptor.idVendor,dev->descriptor.idProduct);
if(QQ2440_SECBULK_IDVENDOR==dev->descriptor.idVendor
&&QQ2440_SECBULK_IDPRODUCT==dev->descriptor.idProduct){
printf("Targetusbdevicefound!\n");
structusb_dev_handle*hdev=usb_open(dev);
if(!hdev){
perror("Cannotopendevice");
}else{
if(0!=usb_claim_interface(hdev,0)){
perror("Cannotclaiminterface");
usb_close(hdev);
hdev=NULL;
}
}
returnhdev;
}
}
}

printf("Targetusbdevicenotfound!\n");

returnNULL;
}

voidusage(){
printf("Usage:dnw2<file>\n\n");
}

unsignedchar*prepare_write_buf(char*filename,unsignedint*len){
unsignedchar*write_buf=NULL;
structstatfs;

intfd=open(filename,O_RDONLY);
if(-1==fd){
perror("Cannotopenfile");
returnNULL;
}
if(-1==fstat(fd,&fs)){
perror("Cannotgetfilesize");
gotoerror;
}
write_buf=(unsignedchar*)malloc(fs.st_size+10);
if(NULL==write_buf){
perror("mallocfailed");
gotoerror;
}

if(fs.st_size!=read(fd,write_buf+8,fs.st_size)){
perror("Readingfilefailed");
gotoerror;
}

printf("Filename:%s\n",filename);
printf("Filesize:%dbytes\n",fs.st_size);

*((u_int32_t*)write_buf)=0x30000000;//downloadaddress
*((u_int32_t*)write_buf+1)=fs.st_size+10;//downloadsize;

*len=fs.st_size+10;
returnwrite_buf;

error:if(fd!=-1)
close(fd);
if(NULL!=write_buf)
free(write_buf);
fs.st_size=0;
returnNULL;

}

intmain(intargc,char*argv[]){
if(2!=argc){
usage();
return1;
}

structusb_dev_handle*hdev=open_port();
if(!hdev){
return1;
}

unsignedintlen=0;
unsignedchar*write_buf=prepare_write_buf(argv[1],&len);
if(NULL==write_buf)
return1;

unsignedintremain=len;
unsignedinttowrite;
printf("Writingdata...\n");
while(remain){
towrite=remain>512?512:remain;
if(towrite!=usb_bulk_write(hdev,0x03,write_buf+(len-remain),
towrite,3000)){
perror("usb_bulk_writefailed");
break;
}
remain-=towrite;
printf("\r%d%\t%dbytes",(len-remain)*100/len,len
-remain);
fflush(stdout);
}
if(0==remain)
printf("Done!\n");
return0;
}






3.gccdnw.c-odnw-lusb



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: