📄 ptpcam.c
字号:
(long unsigned)params.handles.Handler[i], (unsigned) oi.ObjectCompressedSize, tm->tm_year+1900, tm->tm_mon+1,tm->tm_mday, tm->tm_hour, tm->tm_min, oi.Filename); } printf("\n"); close_camera(&ptp_usb, ¶ms, dev);}voiddelete_object (int busn, int devn, short force, uint32_t handle){ PTPParams params; PTP_USB ptp_usb; struct usb_device *dev; PTPObjectInfo oi; if (open_camera(busn, devn, force, &ptp_usb, ¶ms, &dev)<0) return; CR(ptp_getobjectinfo(¶ms,handle,&oi), "Could not get object info\n"); CR(ptp_deleteobject(¶ms, handle,0), "Could not delete object\n"); printf("\nObject 0x%08lx (%s) deleted.\n",(long unsigned) handle, oi.Filename); close_camera(&ptp_usb, ¶ms, dev);}voiddelete_all_files (int busn, int devn, short force){ PTPParams params; PTP_USB ptp_usb; struct usb_device *dev; PTPObjectInfo oi; uint32_t handle; int i; int ret; if (open_camera(busn, devn, force, &ptp_usb, ¶ms, &dev)<0) return; printf("Camera: %s\n",params.deviceinfo.Model); CR(ptp_getobjecthandles (¶ms,0xffffffff, 0x000000, 0x000000, ¶ms.handles),"Could not get object handles\n"); for (i=0; i<params.handles.n; i++) { handle=params.handles.Handler[i]; if ((ret=ptp_getobjectinfo(¶ms,handle, &oi))!=PTP_RC_OK){ fprintf(stderr,"Handle: 0x%08lx\n",(long unsigned) handle); fprintf(stderr,"ERROR: Could not get object info\n"); ptp_perror(¶ms,ret); if (ret==PTP_ERROR_IO) clear_stall(&ptp_usb); continue; } if (oi.ObjectFormat == PTP_OFC_Association) continue; CR(ptp_deleteobject(¶ms, handle,0), "Could not delete object\n"); printf("Object 0x%08lx (%s) deleted.\n",(long unsigned) handle, oi.Filename); } close_camera(&ptp_usb, ¶ms, dev);}voidsave_object(PTPParams *params, uint32_t handle, char* filename, PTPObjectInfo oi, int overwrite){ int file; char *image; int ret; struct utimbuf timebuf; file=open(filename, (overwrite==OVERWRITE_EXISTING?0:O_EXCL)|O_RDWR|O_CREAT|O_TRUNC,S_IRWXU|S_IRGRP); if (file==-1) { if (errno==EEXIST) { printf("Skipping file: \"%s\", file exists!\n",filename); goto out; } perror("open"); goto out; } lseek(file,oi.ObjectCompressedSize-1,SEEK_SET); ret=write(file,"",1); if (ret==-1) { perror("write"); goto out; } image=mmap(0,oi.ObjectCompressedSize,PROT_READ|PROT_WRITE,MAP_SHARED, file,0); if (image==MAP_FAILED) { perror("mmap"); close(file); goto out; } printf ("Saving file: \"%s\" ",filename); fflush(NULL); ret=ptp_getobject(params,handle,&image); munmap(image,oi.ObjectCompressedSize); if (close(file)==-1) { perror("close"); } timebuf.actime=oi.ModificationDate; timebuf.modtime=oi.CaptureDate; utime(filename,&timebuf); if (ret!=PTP_RC_OK) { printf ("error!\n"); ptp_perror(params,ret); if (ret==PTP_ERROR_IO) clear_stall((PTP_USB *)(params->data)); } else { printf("is done.\n"); }out: return;}voidget_save_object (PTPParams *params, uint32_t handle, char* filename, int overwrite){ PTPObjectInfo oi; int ret; memset(&oi, 0, sizeof(PTPObjectInfo)); if (verbose) printf ("Handle: 0x%08lx\n",(long unsigned) handle); if ((ret=ptp_getobjectinfo(params,handle, &oi))!=PTP_RC_OK) { fprintf(stderr, "Could not get object info\n"); ptp_perror(params,ret); if (ret==PTP_ERROR_IO) clear_stall((PTP_USB *)(params->data)); goto out; } if (oi.ObjectFormat == PTP_OFC_Association) goto out; if (filename==NULL) filename=(oi.Filename); save_object(params, handle, filename, oi, overwrite);out: return;}voidget_file (int busn, int devn, short force, uint32_t handle, char* filename,int overwrite){ PTPParams params; PTP_USB ptp_usb; struct usb_device *dev; if (open_camera(busn, devn, force, &ptp_usb, ¶ms, &dev)<0) return; printf("Camera: %s\n",params.deviceinfo.Model); get_save_object(¶ms, handle, filename, overwrite); close_camera(&ptp_usb, ¶ms, dev);}voidget_all_files (int busn, int devn, short force, int overwrite){ PTPParams params; PTP_USB ptp_usb; struct usb_device *dev; int i; if (open_camera(busn, devn, force, &ptp_usb, ¶ms, &dev)<0) return; printf("Camera: %s\n",params.deviceinfo.Model); CR(ptp_getobjecthandles (¶ms,0xffffffff, 0x000000, 0x000000, ¶ms.handles),"Could not get object handles\n"); for (i=0; i<params.handles.n; i++) { get_save_object (¶ms, params.handles.Handler[i], NULL, overwrite); } close_camera(&ptp_usb, ¶ms, dev);}voidlist_operations (int busn, int devn, short force){ PTPParams params; PTP_USB ptp_usb; struct usb_device *dev; int i; const char* name; printf("\nListing supported operations...\n"); if (open_camera(busn, devn, force, &ptp_usb, ¶ms, &dev)<0) return; printf("Camera: %s\n",params.deviceinfo.Model); for (i=0; i<params.deviceinfo.OperationsSupported_len; i++) { name=ptp_get_operation_name(¶ms, params.deviceinfo.OperationsSupported[i]); if (name==NULL) printf(" 0x%04x: UNKNOWN\n", params.deviceinfo.OperationsSupported[i]); else printf(" 0x%04x: %s\n", params.deviceinfo.OperationsSupported[i],name); } close_camera(&ptp_usb, ¶ms, dev);}voidlist_properties (int busn, int devn, short force){ PTPParams params; PTP_USB ptp_usb; struct usb_device *dev; const char* propname; int i; printf("\nListing properties...\n"); if (open_camera(busn, devn, force, &ptp_usb, ¶ms, &dev)<0) return;/* XXX */#if 0 CR(ptp_nikon_setcontrolmode(¶ms, 0x01), "Unable to set Nikon PC controll mode\n");#endif printf("Camera: %s\n",params.deviceinfo.Model); for (i=0; i<params.deviceinfo.DevicePropertiesSupported_len;i++){ propname=ptp_prop_getname(¶ms, params.deviceinfo.DevicePropertiesSupported[i]); if (propname!=NULL) printf(" 0x%04x: %s\n", params.deviceinfo.DevicePropertiesSupported[i], propname); else printf(" 0x%04x: UNKNOWN\n", params.deviceinfo.DevicePropertiesSupported[i]); } close_camera(&ptp_usb, ¶ms, dev);}shortprint_propval (uint16_t datatype, void* value, short hex);shortprint_propval (uint16_t datatype, void* value, short hex){ switch (datatype) { case PTP_DTC_INT8: printf("%hhi",*(char*)value); return 0; case PTP_DTC_UINT8: printf("%hhu",*(unsigned char*)value); return 0; case PTP_DTC_INT16: printf("%hi",*(int16_t*)value); return 0; case PTP_DTC_UINT16: if (hex==PTPCAM_PRINT_HEX) printf("0x%04hX (%hi)",*(uint16_t*)value, *(uint16_t*)value); else printf("%hi",*(uint16_t*)value); return 0; case PTP_DTC_INT32: printf("%li",(long int)*(int32_t*)value); return 0; case PTP_DTC_UINT32: if (hex==PTPCAM_PRINT_HEX) printf("0x%08lX (%lu)", (long unsigned) *(uint32_t*)value, (long unsigned) *(uint32_t*)value); else printf("%lu",(long unsigned)*(uint32_t*)value); return 0; case PTP_DTC_STR: printf("\"%s\"",(char *)value); } return -1;}uint16_tset_property (PTPParams* params, uint16_t property, const char* value, uint16_t datatype);uint16_tset_property (PTPParams* params, uint16_t property, const char* value, uint16_t datatype){ void* val=NULL; switch(datatype) { case PTP_DTC_INT8: val=malloc(sizeof(int8_t)); *(int8_t*)val=(int8_t)strtol(value,NULL,0); break; case PTP_DTC_UINT8: val=malloc(sizeof(uint8_t)); *(uint8_t*)val=(uint8_t)strtol(value,NULL,0); break; case PTP_DTC_INT16: val=malloc(sizeof(int16_t)); *(int16_t*)val=(int16_t)strtol(value,NULL,0); break; case PTP_DTC_UINT16: val=malloc(sizeof(uint16_t)); *(uint16_t*)val=(uint16_t)strtol(value,NULL,0); break; case PTP_DTC_INT32: val=malloc(sizeof(int32_t)); *(int32_t*)val=(int32_t)strtol(value,NULL,0); break; case PTP_DTC_UINT32: val=malloc(sizeof(uint32_t)); *(uint32_t*)val=(uint32_t)strtol(value,NULL,0); break; case PTP_DTC_STR: val=(void *)value; } return(ptp_setdevicepropvalue(params, property, val, datatype)); free(val); return 0;}voidgetset_property_internal (PTPParams* params, uint16_t property,const char* value);voidgetset_property_internal (PTPParams* params, uint16_t property,const char* value){ PTPDevicePropDesc dpd; const char* propname; const char *propdesc; uint16_t result; memset(&dpd,0,sizeof(dpd)); result=ptp_getdevicepropdesc(params,property,&dpd); if (result!=PTP_RC_OK) { ptp_perror(params,result); fprintf(stderr,"ERROR: " "Could not get device property description!\n" "Try to reset the camera.\n"); return ; } /* until this point dpd has to be free()ed */ propdesc=ptp_prop_getdesc(params, &dpd, NULL); propname=ptp_prop_getname(params,property); if (value==NULL) { /* property GET */ if (!verbose) { /* short output, default */ printf("'%s' is set to: ", propname==NULL?"UNKNOWN":propname); if (propdesc!=NULL) printf("[%s]", propdesc); else { if (dpd.FormFlag==PTP_DPFF_Enumeration) PRINT_PROPVAL_HEX(dpd.CurrentValue); else PRINT_PROPVAL_DEC(dpd.CurrentValue); } printf("\n"); } else { /* verbose output */ printf("%s: [0x%04x, ",propname==NULL?"UNKNOWN":propname, property); if (dpd.GetSet==PTP_DPGS_Get) printf ("readonly, "); else printf ("readwrite, "); printf ("%s] ", ptp_get_datatype_name(params, dpd.DataType)); printf ("\n Current value: "); if (dpd.FormFlag==PTP_DPFF_Enumeration) PRINT_PROPVAL_HEX(dpd.CurrentValue); else PRINT_PROPVAL_DEC(dpd.CurrentValue); if (propdesc!=NULL) printf(" [%s]", propdesc); printf ("\n Factory value: "); if (dpd.FormFlag==PTP_DPFF_Enumeration) PRINT_PROPVAL_HEX(dpd.FactoryDefaultValue); else PRINT_PROPVAL_DEC(dpd.FactoryDefaultValue); propdesc=ptp_prop_getdesc(params, &dpd, dpd.FactoryDefaultValue); if (propdesc!=NULL) printf(" [%s]", propdesc); printf("\n"); switch (dpd.FormFlag) { case PTP_DPFF_Enumeration: { int i; printf ("Enumerated:\n"); for(i=0;i<dpd.FORM.Enum.NumberOfValues;i++){ PRINT_PROPVAL_HEX( dpd.FORM.Enum.SupportedValue[i]); propdesc=ptp_prop_getdesc(params, &dpd, dpd.FORM.Enum.SupportedValue[i]); if (propdesc!=NULL) printf("\t[%s]", propdesc); printf("\n"); } } break; case PTP_DPFF_Range: printf ("Range ["); PRINT_PROPVAL_DEC(dpd.FORM.Range.MinimumValue); printf(" - "); PRINT_PROPVAL_DEC(dpd.FORM.Range.MaximumValue); printf("; step "); PRINT_PROPVAL_DEC(dpd.FORM.Range.StepSize); printf("]\n"); break; case PTP_DPFF_None: break; } } } else { uint16_t r; propdesc=ptp_prop_getdesc(params, &dpd, NULL); printf("'%s' is set to: ", propname==NULL?"UNKNOWN":propname); if (propdesc!=NULL) printf("[%s]", propdesc); else { if (dpd.FormFlag==PTP_DPFF_Enumeration) PRINT_PROPVAL_HEX(dpd.CurrentValue); else PRINT_PROPVAL_DEC(dpd.CurrentValue); } printf("\n"); propdesc=ptp_prop_getdescbystring(params, &dpd, value);/* if (propdesc==NULL) { fprintf(stderr, "ERROR: Unable to set property to unidentified value: '%s'\n", value); goto out; }*/ printf("Changing property value to %s [%s] ", value,propdesc); r=(set_property(params, property, value, dpd.DataType)); if (r!=PTP_RC_OK) { printf ("FAILED!!!\n"); fflush(NULL); ptp_perror(params,r); } else printf ("succeeded.\n"); }/* out: */ ptp_free_devicepropdesc(&dpd);}voidgetset_propertybyname (int busn,int devn,char* property,char* value,short force);voidgetset_propertybyname (int busn,int devn,char* property,char* value,short force){ PTPParams params; PTP_USB ptp_usb; struct usb_device *dev; char *p; uint16_t dpc; const char *propval=NULL; printf("\n"); if (open_camera(busn, devn, force, &ptp_usb, ¶ms, &dev)<0) return; printf("Camera: %s",params.deviceinfo.Model); if ((devn!=0)||(busn!=0)) printf(" (bus %i, dev %i)\n",busn,devn); else printf("\n"); if (property==NULL) { fprintf(stderr,"ERROR: no such property\n"); return; } /* 1. change all '-' in property and value to ' ' 2. change all ' ' in property and value to '-' 3. get property code by name 4. get value code by name 5. set property */ while ((p=index(property,'-'))!=NULL) { *p=' '; } dpc=ptp_prop_getcodebyname(¶ms, property); if (dpc==0) { fprintf(stderr, "ERROR: Could not find property '%s'\n", property); close_camera(&ptp_usb, ¶ms, dev); return; } if (!ptp_property_issupported(¶ms, dpc)) { fprintf(stderr,"The device does not support this property!\n"); close_camera(&ptp_usb, ¶ms, dev); return; } if (value!=NULL) { while ((p=index(value,'-'))!=NULL) { *p=' '; } propval=ptp_prop_getvalbyname(¶ms, value, dpc); if (propval==NULL) propval=value; } getset_property_internal (¶ms, dpc,propval); close_camera(&ptp_usb, ¶ms, dev);}voidgetset_property (int busn,int devn,uint16_t property,char* value,short force);voidgetset_property (int busn,int devn,uint16_t property,char* value,short force){ PTPParams params; PTP_USB ptp_usb;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -