/* * USBsniff log mapper to C code for Syntek and Genesys webcam * Copyright (C) 2008 Olivier Lorin * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ /* * Usage : relog logfile [prefixe] (prefixe defaulted to 's' if none provided) * * E.g. "relog file.log Z" produces "file.log.c" which contains data arrays (send or received) prefixed Z (Z000 for the first to Z999 maximum) * The prefixe is to differenciate the data arrays when mapping an intialization file or a capture file or anything * so that there's no duplicates of the name of the data arrays if several ".c" files have to be included in another one. */ #include #include #include #include void modifFin(char *lgn) { if (lgn[strlen(lgn)-2]==0xd) { lgn[strlen(lgn)-2]='\n'; lgn[strlen(lgn)-1]='\0'; } } void supprFin(char *lgn) { lgn[strlen(lgn)-1]='\0'; } void transfoBayerRVB(unsigned char* ime, int lg, int h, unsigned char* ims); void ecrisRTx(FILE *fc); char unsigned enteteBMP24[]={ 0x42, 0x4d, 0x36, 0x10, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x36, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x80, 0x02, 0x00, 0x00, 0xe0, 0x01, 0x00, 0x00, 0x01, 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; int mode4=0; // mode4 se positionne tout seul pour le pilote syntek (paquets vides de 4 octets au lieu de 2) int largeur=640,hauteur=480; int main(int argc,char** argv) { if (argc<2) { printf(" %s fichierLog [PrefixeTableaux=msc] \n\n",argv[0]); exit(1); } char lgno[257]=" ",*lgn=lgno+1,noms[256],nomi[256],oct[256*4], lgn2[256]; char ch[32]="00 00 00 00 00 00 00 00\0",capture[20],*p; char *pref=(char*)"msc"; if (argc==3) pref=argv[2]; unsigned int type=0,lg,val,valiso,req,index,out,lgt=0,endpoint,feature,offset,intervalle,longueur=0,nombre=0,nvlongueur,ms,ms0=0,exms=0, msc=0, numUrbDown=0, numUrbBack=0;; unsigned char *img,*ptim; img=new unsigned char[1600*1200*4]; char libusb[512]; int ok,n,iface,exiface=-1,altsetting,exaltsetting=-1,get=0,setup=0,im=0,numim=0,numChamp=0,tabEnCours=0,ngen,lgd,dejaIsoc=0,exout=1,l=1,posoct; int dejamalpredit=0,ondepassedeja=0,offset0=0,estimlg,estimht,decbmp,modeDiff=0,nbIface,valConf,config,attr,sansflux=0; FILE *fi; if (*pref=='-') { pref=(char*)"X"; modeDiff=1; } if (*pref=='f') { pref=(char*)"sF"; sansflux=1; } sprintf(noms,"%s.c",argv[1]); printf("Sortie du code dans %s\n",noms); FILE *fc=fopen(noms,"wt"); fprintf(fc,"// %s %s %s\n\n",argv[0],argv[1],(argv[2])?argv[2]:""); // ecrisRTx(fc); if (!modeDiff) fprintf(fc,"// ctrl_in/ctrl_out ... acknowledgement => Tps : // (ackN - in/outN)+ (in/outN+1 - ackN)ms\n"); if (!modeDiff) fprintf(fc,"void fonction_%s()\n{\n\tmsg(\"Entrée dans 'fonction_%s'\");\n\n",pref,pref); strcpy(noms,argv[1]); strcat(noms,"log"); FILE *fs=fopen(noms,"wb"); // printf("Sortie du nouveau log dans %s\n",noms); fprintf(fs,"D'après le log \"%s\"\n\n",argv[1]); FILE *f=fopen(argv[1],"rt"); while (fgets(lgn,256,f)!=NULL) { modifFin(lgn); if (lgn[0]=='[' && strstr(lgn,"going down")) { if (!fgets(lgn2,256,f)) break; modifFin(lgn2); if (!modeDiff && !strstr(lgn2, "ISOCH")) { exms=ms; ms=atoi(lgn+1); numUrbDown=atoi(lgn+19); if (ms>=0) if (msc>exms) fprintf(fc, " // %5d.0+%4d.0ms", msc-exms, ms-msc); else fprintf(fc, " // - %4d.0ms", ms-exms); } strcpy(lgn, lgn2); } //if (ms==0) ms=1; if (ms0==0) { ms0=ms; exms=0; } else ms-=ms0; } if (lgn[0]=='[' && strstr(lgn,"coming back")) { if (!fgets(lgn2,256,f)) break; modifFin(lgn2); if (!strstr(lgn2, "ISOCH") && atoi(lgn+19)==numUrbDown) msc=atoi(lgn+1); strcpy(lgn, lgn2); } if (type==5 && strstr(lgn,"-- URB")) { if (strstr(lgn,"ISOCH_TRANSFER")==NULL) fprintf(fs,"\n\n"); else fprintf(fs,"\n"); } if (strstr(lgn,"URB_FUNCTION_VENDOR")) { type=0; supprFin(lgn); fprintf(fs,"%s",lgn+7); } else if (strstr(lgn,"URB_FUNCTION_CONTROL")) { type=1; supprFin(lgn); fprintf(fs,"%s",lgn+7); } else if (strstr(lgn,"_SELECT_CONFIG")) { type=2; supprFin(lgn); fprintf(fs,"%s",lgn+7); if (!modeDiff) { fprintf(fc,"\n//SELECT_CONFIG"); } } else if (strstr(lgn,"_SET_FEATURE")) { type=20;fprintf(fs,"%s",lgn+7); if (!modeDiff) { fprintf(fc,"\n//SET_FEATURE"); } } else if (strstr(lgn,"GET_DESCRIPTOR")) { type=3; supprFin(lgn); fprintf(fs,"%s",lgn+7); if (!modeDiff) { fprintf(fc,"\n//GET_DESCRIPTOR"); } get=1; } else if (strstr(lgn,"SELECT_INTERFACE")) { type=4; supprFin(lgn); fprintf(fs,"%s",lgn+7); if (!modeDiff) { fprintf(fc,"\n//SELECT_INTERFACE"); } } else if (!sansflux && strstr(lgn,"ISOCH_TRANSFER")) { type=5; supprFin(lgn); fprintf(fs,"%s",lgn+7); if (!dejaIsoc && !modeDiff) { dejaIsoc=1; fprintf(fc,"\n//\tpremier transfert isochrone"); } if (!modeDiff && !sansflux) fprintf(fc,"."); } else if (strstr(lgn,"ABORT_PIPE")) { type=6; fprintf(fs,"%s",lgn+7); if (!modeDiff) { fprintf(fc,"\n//ABORT_PIPE"); } } else if (strstr(lgn,"RESET_PIPE")) { type=7; fprintf(fs,"%s",lgn+7); if (!modeDiff) { fprintf(fc,"\n//RESET_PIPE"); offset0=0; longueur=0; nombre=0; im=0; lgt=0; } } else if (strstr(lgn,"-- URB") && !strstr(lgn,"ISOCH_TRANSFER")) { type=8; fprintf(fs,"%s",lgn+7); if (!modeDiff) fprintf(fc,"\n//%s",lgn+7); } else if (strstr(lgn,"ISOCH_TRANSFER")) type=9; if (type==0 || type==3) { if (strstr(lgn,"(U")) { fprintf(fs,"%s",strstr(lgn,"(")); if (strstr(lgn,"_OUT")) { out=1; ch[0]='4'; } else { out=0; if (get==0) ch[0]='c'; else ch[0]='8'; } if (exout!=out) fprintf(fc,"\n"); exout=out; } if (strstr(lgn,"Length")) { sscanf(lgn+25,"%x",&lg); ch[18]=lgn[31];ch[19]=lgn[32]; ch[21]=lgn[29];ch[22]=lgn[30]; } if (strstr(lgn,"Req")) { sscanf(lgn+28,"%x",&req); ch[3]=lgn[34];ch[4]=lgn[35]; } if (strstr(lgn,"Value")) { sscanf(lgn+28,"%x",&val); ch[6]=lgn[34];ch[7]=lgn[35]; ch[9]=lgn[32];ch[10]=lgn[33]; } if (strstr(lgn,"DescriptorType")) { sscanf(lgn+25,"%x",&val); fprintf(fs,"Lg=%04x desc=%02x idx=%04x\n",lg,val,index); ch[6]='0';ch[7]='0'; ch[9]=lgn[31];ch[10]=lgn[32]; ch[3]='0';ch[4]='6'; if (lg==0) { sprintf(libusb,"\n\tctrl_%s(dev, 0x%c0, %d, 0x%04x, 0x%04x, %d, NULL);",(out)?"out":"in",ch[0],req,val,index,lg); // exms=ms; } else { sprintf(libusb,"\n\tctrl_%s(dev, 0x%c0, %d, 0x%04x, 0x%04x, %d, (void*)%s%03d);",(out)?"out":"in",ch[0],req,val,index,lg,pref,numChamp); // exms=ms; } } if (strstr(lgn,"Index") && type==0 && out==1) { sscanf(lgn+28,"%x",&index); fprintf(fs,"Lg=%04x req=%02d val=%04x idx=%04x\n",lg,req,val,index); ch[12]=lgn[34];ch[13]=lgn[35]; ch[15]=lgn[32];ch[16]=lgn[33]; if (lg==0) { fprintf(fc,"\n\tctrl_%s(dev, 0x%c0, %2d, 0x%04x, 0x%04x,%3d, NULL);",(out)?"out":"in",ch[0],req,val,index,lg); // exms=ms; } else { //fprintf(fc,"\tctrl_%s(dev, 0x%c0, %2d, 0x%04x, 0x%04x,%3d, (void*)%s%03d); // %3dms %d\n",(out)?"out":"in",ch[0],req,val,index,lg,pref,numChamp,ms-exms, ms); fprintf(fc,"\n\tctrl_%s(dev, 0x%c0, %2d, 0x%04x, 0x%04x,%3d, \"%s\");",(out)?"out":"in",ch[0],req,val,index,lg, oct); // exms=ms; if (!modeDiff) numChamp++; } } if (strstr(lgn,"Index") && type==0 && out==0) { sscanf(lgn+28,"%x",&index); ch[12]=lgn[34];ch[13]=lgn[35]; ch[15]=lgn[32];ch[16]=lgn[33]; } if (strstr(lgn,": ") && strstr(lgn,"IR")==NULL && strstr(lgn,"j")==NULL) { fprintf(fs,"%s",strstr(lgn,":")); p=strstr(lgn,": ")+2; // if (!tabEnCours) { tabEnCours=1; fprintf(fc,"char %s%03d[]={%s",pref,numChamp,(lg<170)?" ":"\n"); ngen=0; } if (!tabEnCours) { tabEnCours=1; posoct=0; ngen=0; } for (n=0;n<(strlen(p)+1)/3;n++) { ngen++; sprintf(oct+posoct*4,"\\x%c%c",p[3*n],p[3*n+1]); posoct++; } // if (ngen==lg) fprintf(fc," };\n");// else fprintf(fc,"\n"); } else tabEnCours=0; } if (type==20 && strstr(lgn,"FeatureSelector")) { sscanf(lgn+20,"%x",&feature); sprintf(ch,"00 03 %02x %02x 00 00 00 00",feature%256,feature/256); } if (type==1) { if (strstr(lgn,"(U")) { fprintf(fs,"%s",strstr(lgn,"(")); if (strstr(lgn,"_OUT")) out=1; else out=0; if (exout!=out) fprintf(fc,"\n"); exout=out; } if (strstr(lgn,": ") && strstr(lgn,"IR")==NULL && out==0 && setup==0) // Données reçues { fprintf(fs,"%s",strstr(lgn,":")); p=strstr(lgn,": ")+2; if (lg>1) { if (!tabEnCours) { tabEnCours=1; fprintf(fc,"\n%schar %s%03d[]={%s",(ch[0]=='8')?"/*":"",pref,numChamp,(lg<170)?" ":"\n"); ngen=0; } for (n=0;n<(strlen(p)+1)/3;n++) { ngen++; fprintf(fc,"0x%c%c%s",p[3*n],p[3*n+1],(ngenlongueur) { longueur=nvlongueur; if (nombre!=0) fprintf(fc,"\n//\tINFOBUF %d paquets [%d octets : %d]",nombre,longueur,longueur/nombre); } } if (strstr(lgn,"NumberOfPackets") && nombre==0) { sscanf(lgn+25,"%x",&nombre); if (longueur!=0) fprintf(fc,"\n//\t INFOBUF %d paquets [%d octets : %d]",nombre,longueur,longueur/nombre); } // Sert à quoi? if (strstr(lgn,"IsoPacket[0].Offset = 0") && offset0==0) offset0=1; if (strstr(lgn,"IsoPacket[1].Offset") && offset0==1) { offset0=2; sscanf(lgn+24,"%d",&offset); fprintf(fc,"\n//\toffset de %d octets",offset); fprintf(fs,"offset de %d octets\n",offset); } if (strstr(lgn,".Length")) { sscanf(lgn+24,"%d",&lgd); fprintf(fs,"+%d",lgd); if (lgd>2+2*mode4) { if (mode4==0) lgt+=lgd-2; else if (lgd!=8) lgt+=lgd-4; fprintf(fs,"/%d",lgt); if (!modeDiff && !sansflux) fprintf(fc,"o"); } if (mode4 && lgd==4) im=0; } // Longueur du paquet de données if (strstr(lgn,"00000000: ")!=NULL && (strstr(lgn,"00000000: 02 00")==NULL) && mode4==0) { fprintf(fs,"\n réception de %s",strstr(lgn,": ")+2); supprFin(lgn); if (!modeDiff && !sansflux) { fprintf(fc, "x"); fprintf(fc,"\n//\tréception de %s",strstr(lgn,": ")+2); } if (strstr(lgn,"00000000: 80 00") || strstr(lgn,"00000000: a0 00")) mode4=1; } if (strstr(lgn,": ") && ((lgd==8 && mode4 && (lgn[14]=='8' || lgn[14]=='a'))||(lgd==2 && strstr(lgn," 02 02")))) // Fin de données, écriture { if (ondepassedeja) { fprintf(fs,"\nOn finit de dépasser ************************** Ligne %d\n",l); if (!modeDiff && !sansflux) fprintf(fc,"\n//\ton finit de dépasser... ************************** Ligne %d",l); printf("Fin de dépassement de taille réservée pour l'image en ligne %d.\n",l); fflush(stdout); ondepassedeja=0; } fprintf(fs,"\n ** FIN (%d <=> %.0f x %.0f) ** \n\n",lgt,sqrt(lgt*4/3),sqrt(lgt*4/3)*3/4); if (!modeDiff && !sansflux) fprintf(fc,"\n//\tfin de données (%d <=> %.0f x %.0f)",lgt,sqrt(lgt*4/3),sqrt(lgt*4/3)*3/4); estimlg=(int)sqrt(lgt*4/3); if (abs(estimlg- 320)<80) estimlg=320; else if (abs(estimlg-1280)<80) estimlg=1280; else if (abs(estimlg-1600)<200) estimlg=1600; else if (abs(estimlg- 800)<80) estimlg=800; else estimlg=640; estimht=lgt/estimlg; largeur=estimlg; hauteur=estimht; if (hauteur>1200) hauteur=1200; if (lgt>1600*1200) lgt=1600*1200; decbmp=lgt-largeur*hauteur; if (decbmp<0) decbmp=0; decbmp=0; if (hauteur>0) { transfoBayerRVB(img+decbmp,largeur,hauteur,img+largeur*hauteur+decbmp); *(int*)(enteteBMP24+2)=largeur*hauteur*3+0x36; *(int*)(enteteBMP24+18)=largeur; *(int*)(enteteBMP24+22)=hauteur; *(int*)(enteteBMP24+34)=largeur*hauteur*3; sprintf(nomi,"im%03d.bmp",numim); fi=fopen(nomi,"wb"); fwrite(enteteBMP24,54,1,fi); fwrite(img+largeur*hauteur+decbmp,3*largeur*hauteur,1,fi); // sprintf(nomi,"im%03d.pgm",numim); // fi=fopen(nomi,"wb"); // fprintf(fi,"P5 640 480 255 "); // fwrite(entete,54,1,fi); // fwrite(img,640*480,1,fi); fclose(fi); if (!modeDiff && !sansflux) fprintf(fc, " Écriture de %s", nomi); printf("Écriture de %s (%d x %d)\n",nomi,largeur,hauteur); fflush(stdout); } numim++; im=0; lgt=0; } if (strstr(lgn,": ") && strstr(lgn,"IR")==NULL && lgd>2+2*mode4) // Les données { if (lgd==8 && im!=0) lgt+=4; if (im==0) { ptim=img; im=1; if (!modeDiff && !sansflux) { fprintf(fc,"\n//\tdébut de données"); } } p=strstr(lgn,": ")+2; if (strstr(lgn,"00000000: 02 00")!=NULL) p+=6; if (strstr(lgn,"00000000: 00 ")!=NULL) p+=12; for (n=0;n<(strlen(p)+1)/3;n++) { sscanf(p+3*n,"%x",&valiso); capture[n]=valiso; } if (ptim-img+(strlen(p)+1)/3<=largeur*hauteur && ptim-img+(strlen(p)+1)/3<=1600*1200) { memcpy(ptim,capture,(strlen(p)+1)/3); ptim+=(strlen(p)+1)/3; } else if (ondepassedeja==0) { fprintf(fs,"\nOn dépasse ou on ne sait pas s'arrêter ************************** Ligne %d\n",l); if (!modeDiff && !sansflux) fprintf(fc,"\n//\ton dépasse ou on ne sait pas s'arrêter... ************************** Ligne %d",l); printf("Dépassement de taille réservée pour l'image en ligne %d.\n",l); fflush(stdout); ondepassedeja=1; } } } l++; } fclose(f); fclose(fs); if (!modeDiff) fprintf(fc,"\n\tmsg(\"Sortie de 'fonction_%s'\");\n}",pref); fclose(fc); //remove(noms); delete[] img; } void transfoBayerRVB(unsigned char* ime, int lg, int ht, unsigned char* ims) { int x,y,z,xy,yx,inv,mod; inv=1; // inversion haut-bas (0 ou 1) mod=0; // varie le décalage en xy de la composante verte (0 ou 1) xy=0; // inversion rouge-bleu (0 ou 2) if (mode4) { inv=0; mod=0; xy=2; } if (inv) { for (y=0;y>>\":\"<<<\",pref, req, val, index, lg);\n\ if (lg==0) r=libusb_control_transfer(handle, pref, req, val, index, NULL, lg, 1000);\n\ else\n\ if (lg==1) r=libusb_control_transfer(handle, pref, req, val, index, pdonnees, lg, 1000);\n\ else\n\ if (lg>1) { usleep(50); r=libusb_control_transfer(handle, pref, req, val, index, pdonnees, lg, 5000); }\n\ \n\ if (r<0) die(\"ctrl transfer failed %d\", r);\n\ if (lg>1 && r