📄 ptpcam.c
字号:
printf(" serial number: '%s'\n",params.deviceinfo.SerialNumber); printf(" device version: %s\n",params.deviceinfo.DeviceVersion); printf(" extension ID: 0x%08x\n", params.deviceinfo.VendorExtensionID); printf(" extension description: %s\n", params.deviceinfo.VendorExtensionDesc); printf(" extension version: 0x%04x\n", params.deviceinfo.VendorExtensionVersion); printf("\n"); close_camera(&ptp_usb, ¶ms, dev);}voidcapture_image (int busn, int devn, short force){ PTPParams params; PTP_USB ptp_usb; PTPContainer event; struct usb_device *dev; short ret; printf("\nInitiating captue...\n"); if (open_camera(busn, devn, force, &ptp_usb, ¶ms, &dev)<0) return; /* capture timeout should be longer */ ptpcam_usb_timeout=USB_CAPTURE_TIMEOUT; CR(ptp_initiatecapture (¶ms, 0x0, 0), "Could not capture.\n"); ret=ptp_usb_event_wait(¶ms,&event); if (verbose) printf ("Event received %08x, ret=%x\n", event.Code, ret); if (ret!=PTP_RC_OK) goto err; if (event.Code==PTP_EC_CaptureComplete) { printf ("Camera reported 'capture completed' but the object information is missing.\n"); goto out; } while (event.Code==PTP_EC_ObjectAdded) { printf ("Object added 0x%08x\n", event.Param1); if (ptp_usb_event_wait(¶ms, &event)!=PTP_RC_OK) goto err; if (verbose) printf ("Event received %08x, ret=%x\n", event.Code, ret); if (event.Code==PTP_EC_CaptureComplete) { printf ("Capture completed successfully!\n"); goto out; } } err: printf("Events receiving error. Capture status unknown.\n");out: ptpcam_usb_timeout=USB_TIMEOUT; close_camera(&ptp_usb, ¶ms, dev);}voidloop_capture (int busn, int devn, short force, int n, int overwrite){ PTPParams params; PTP_USB ptp_usb; PTPContainer event; struct usb_device *dev; int file; PTPObjectInfo oi; uint32_t handle=0; char *image; int ret; char *filename; if (open_camera(busn, devn, force, &ptp_usb, ¶ms, &dev)<0) return; /* capture timeout should be longer */ ptpcam_usb_timeout=USB_CAPTURE_TIMEOUT; CR(ptp_getdeviceinfo (¶ms, ¶ms.deviceinfo), "Could not get device info\n"); printf("Camera: %s\n",params.deviceinfo.Model); /* local loop */ while (n>0) { /* capture */ printf("\nInitiating captue...\n"); CR(ptp_initiatecapture (¶ms, 0x0, 0),"Could not capture\n"); n--; ret=ptp_usb_event_wait(¶ms,&event); if (verbose) printf ("Event received %08x, ret=%x\n", event.Code, ret); if (ret!=PTP_RC_OK) goto err; if (event.Code==PTP_EC_CaptureComplete) { printf ("CANNOT DOWNLOAD: got 'capture completed' but the object information is missing.\n"); goto out; } while (event.Code==PTP_EC_ObjectAdded) { printf ("Object added 0x%08x\n", event.Param1); handle=event.Param1; if (ptp_usb_event_wait(¶ms, &event)!=PTP_RC_OK) goto err; if (verbose) printf ("Event received %08x, ret=%x\n", event.Code, ret); if (event.Code==PTP_EC_CaptureComplete) goto download; }download: memset(&oi, 0, sizeof(PTPObjectInfo)); if (verbose) printf ("Downloading: 0x%08x\n",handle); if ((ret=ptp_getobjectinfo(¶ms,handle, &oi))!=PTP_RC_OK){ fprintf(stderr,"ERROR: Could not get object info\n"); ptp_perror(¶ms,ret); if (ret==PTP_ERROR_IO) clear_stall(&ptp_usb, dev); continue; } if (oi.ObjectFormat == PTP_OFC_Association) goto out; filename=(oi.Filename); 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(¶ms,handle,&image); munmap(image,oi.ObjectCompressedSize); close(file); if (ret!=PTP_RC_OK) { printf ("error!\n"); ptp_perror(¶ms,ret); if (ret==PTP_ERROR_IO) clear_stall(&ptp_usb, dev); } else { /* and delete from camera! */ printf("is done...\nDeleting from camera.\n"); CR(ptp_deleteobject(¶ms, handle,0), "Could not delete object\n"); printf("Object 0x%08x (%s) deleted.\n",handle, oi.Filename); }out: ; }err: ptpcam_usb_timeout=USB_TIMEOUT; close_camera(&ptp_usb, ¶ms, dev);}voidlist_files (int busn, int devn, short force){ PTPParams params; PTP_USB ptp_usb; struct usb_device *dev; int i; PTPObjectInfo oi; printf("\nListing files...\n"); if (open_camera(busn, devn, force, &ptp_usb, ¶ms, &dev)<0) return; CR(ptp_getdeviceinfo (¶ms, ¶ms.deviceinfo), "Could not get device info\n"); printf("Camera: %s\n",params.deviceinfo.Model); CR(ptp_getobjecthandles (¶ms,0xffffffff, 0x000000, 0x000000, ¶ms.handles),"Could not get object handles\n"); printf("Handler: size: \tname:\n"); for (i = 0; i < params.handles.n; i++) { CR(ptp_getobjectinfo(¶ms,params.handles.Handler[i], &oi),"Could not get object info\n"); if (oi.ObjectFormat == PTP_OFC_Association) continue; printf("0x%08x: % 9i\t%s\n",params.handles.Handler[i], oi.ObjectCompressedSize, 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%08x (%s) deleted.\n",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; CR(ptp_getdeviceinfo (¶ms, ¶ms.deviceinfo), "Could not get device info\n"); 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%08x\n",handle); fprintf(stderr,"ERROR: Could not get object info\n"); ptp_perror(¶ms,ret); if (ret==PTP_ERROR_IO) clear_stall(&ptp_usb, dev); continue; } if (oi.ObjectFormat == PTP_OFC_Association) continue; CR(ptp_deleteobject(¶ms, handle,0), "Could not delete object\n"); printf("Object 0x%08x (%s) deleted.\n",handle, oi.Filename); } close_camera(&ptp_usb, ¶ms, dev);}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; struct utimbuf timebuf; int file; PTPObjectInfo oi; char *image; int ret; if (open_camera(busn, devn, force, &ptp_usb, ¶ms, &dev)<0) return; CR(ptp_getdeviceinfo (¶ms, ¶ms.deviceinfo), "Could not get device info\n"); printf("Camera: %s\n",params.deviceinfo.Model); if (verbose) printf ("Handle: 0x%08x\n",handle); CR(ptp_getobjectinfo(¶ms,handle, &oi), "Could not get object info\n"); if (oi.ObjectFormat == PTP_OFC_Association) goto out; if (filename==NULL) filename=(oi.Filename); 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); write(file,"",1); if (file<0) goto out; image=mmap(0,oi.ObjectCompressedSize,PROT_READ|PROT_WRITE,MAP_SHARED, file,0); if (image==MAP_FAILED) { close(file); goto out; } printf ("Saving file: \"%s\" ",filename); fflush(NULL); ret=ptp_getobject(¶ms,handle,&image); munmap(image,oi.ObjectCompressedSize); close(file); timebuf.actime=oi.ModificationDate; timebuf.modtime=oi.CaptureDate; utime(filename,&timebuf); if (ret!=PTP_RC_OK) { printf ("error!\n"); ptp_perror(¶ms,ret); } else { printf("is done.\n"); }out: 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; struct utimbuf timebuf; int file; PTPObjectInfo oi; uint32_t handle; char *image; int ret; int i; char *filename; if (open_camera(busn, devn, force, &ptp_usb, ¶ms, &dev)<0) return; CR(ptp_getdeviceinfo (¶ms, ¶ms.deviceinfo), "Could not get device info\n"); 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++) { memset(&oi, 0, sizeof(PTPObjectInfo)); handle=params.handles.Handler[i]; if (verbose) printf ("Handle: 0x%08x\n",handle); if ((ret=ptp_getobjectinfo(¶ms,handle, &oi))!=PTP_RC_OK){ fprintf(stderr,"ERROR: Could not get object info\n"); ptp_perror(¶ms,ret); if (ret==PTP_ERROR_IO) clear_stall(&ptp_usb, dev); continue; } if (oi.ObjectFormat == PTP_OFC_Association) goto out; filename=(oi.Filename); 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); continue; } 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(¶ms,handle,&image); munmap(image,oi.ObjectCompressedSize); close(file); timebuf.actime=oi.ModificationDate; timebuf.modtime=oi.CaptureDate; utime(filename,&timebuf); if (ret!=PTP_RC_OK) { printf ("error!\n"); ptp_perror(¶ms,ret); if (ret==PTP_ERROR_IO) clear_stall(&ptp_usb, dev); } else { printf("is done.\n"); }out: ; } 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 0#ifdef DEBUG printf("dev %i\tbus %i\n",devn,busn);#endif dev=find_device(busn,devn,force); if (dev==NULL) { fprintf(stderr,"could not find any device matching given " "bus/dev numbers\n"); exit(-1); } find_endpoints(dev,&ptp_usb.inep,&ptp_usb.outep,&ptp_usb.intep); init_ptp_usb(¶ms, &ptp_usb, dev); CR(ptp_opensession (¶ms,1), "Could not open session!\n");#endif if (open_camera(busn, devn, force, &ptp_usb, ¶ms, &dev)<0) return; CR(ptp_getdeviceinfo (¶ms, ¶ms.deviceinfo), "Could not get device info\n"); 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: 0x%04x\n", params.deviceinfo.OperationsSupported[i], params.deviceinfo.OperationsSupported[i]); else printf(" 0x%04x: %s\n", params.deviceinfo.OperationsSupported[i],name); } CR(ptp_closesession(¶ms), "Could not close session!\n"); close_usb(&ptp_usb, dev);}voidlist_properties (int busn, int devn, short force){ PTPParams params; PTP_USB ptp_usb; struct usb_device *dev; const char* propdesc; int i; printf("\nListing properties...\n");#ifdef DEBUG printf("dev %i\tbus %i\n",devn,busn);#endif#if 0 dev=find_device(busn,devn,force); if (dev==NULL) { fprintf(stderr,"could not find any device matching given " "bus/dev numbers\n"); exit(-1); } find_endpoints(dev,&ptp_usb.inep,&ptp_usb.outep,&ptp_usb.intep); init_ptp_usb(¶ms, &ptp_usb, dev); CR(ptp_opensession (¶ms,1), "Could not open session!\n");#endif if (open_camera(busn, devn, force, &ptp_usb, ¶ms, &dev)<0) return; CR(ptp_getdeviceinfo (¶ms, ¶ms.deviceinfo), "Could not get device info\n"); printf("Camera: %s\n",params.deviceinfo.Model); for (i=0; i<params.deviceinfo.DevicePropertiesSupported_len;i++){ propdesc=ptp_get_property_name(¶ms, params.deviceinfo.DevicePropertiesSupported[i]); if (propdesc!=NULL) printf(" 0x%04x: %s\n", params.deviceinfo.DevicePropertiesSupported[i], propdesc);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -