⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ptpcam.c

📁 Linux平台。一个好用的ptp传输工具。用来从相机向pc传输照片
💻 C
📖 第 1 页 / 共 4 页
字号:
	struct usb_device *dev;	printf ("\n");	if (open_camera(busn, devn, force, &ptp_usb, &params, &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 (!ptp_property_issupported(&params, property))	{		fprintf(stderr,"The device does not support this property!\n");		close_camera(&ptp_usb, &params, dev);		return;	}	getset_property_internal (&params, property,value);#if 0	memset(&dpd,0,sizeof(dpd));	CR(ptp_getdevicepropdesc(&params,property,&dpd),		"Could not get device property description!\n"		"Try to reset the camera.\n");	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 [%s]", ptp_prop_tostr(&params, &dpd,							NULL), 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 [%s]", ptp_prop_tostr(&params, &dpd, NULL), propdesc);		else		{			if (dpd.FormFlag==PTP_DPFF_Enumeration)				PRINT_PROPVAL_HEX(dpd.CurrentValue);			else 				PRINT_PROPVAL_DEC(dpd.CurrentValue);		}		printf("\n");		printf("Changing property value to '%s' ",value);		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");	}	ptp_free_devicepropdesc(&dpd);#endif	close_camera(&ptp_usb, &params, dev);}voidshow_all_properties (int busn,int devn,short force, int unknown);voidshow_all_properties (int busn,int devn,short force, int unknown){	PTPParams params;	PTP_USB ptp_usb;	struct usb_device *dev;	PTPDevicePropDesc dpd;	const char* propname;	const char *propdesc;	int i;	printf ("\n");	if (open_camera(busn, devn, force, &ptp_usb, &params, &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");	for (i=0; i<params.deviceinfo.DevicePropertiesSupported_len;i++) {		propname=ptp_prop_getname(&params,				params.deviceinfo.DevicePropertiesSupported[i]);		if ((unknown) && (propname!=NULL)) continue;		printf("0x%04x: ",				params.deviceinfo.DevicePropertiesSupported[i]);		memset(&dpd,0,sizeof(dpd));		CR(ptp_getdevicepropdesc(&params,			params.deviceinfo.DevicePropertiesSupported[i],&dpd),			"Could not get device property description!\n"			"Try to reset the camera.\n");		propdesc= ptp_prop_getdesc(&params, &dpd, NULL);		PRINT_PROPVAL_HEX(dpd.CurrentValue);		if (verbose) {			printf (" (%s",propname==NULL?"UNKNOWN":propname);			if (propdesc!=NULL)				printf(": %s)",propdesc);			else				printf(")");		}			printf("\n");		ptp_free_devicepropdesc(&dpd);	}	close_camera(&ptp_usb, &params, dev);}intusb_get_endpoint_status(PTP_USB* ptp_usb, int ep, uint16_t* status){	 return (usb_control_msg(ptp_usb->handle,		USB_DP_DTH|USB_RECIP_ENDPOINT, USB_REQ_GET_STATUS,		USB_FEATURE_HALT, ep, (char *)status, 2, 3000));}intusb_clear_stall_feature(PTP_USB* ptp_usb, int ep){	return (usb_control_msg(ptp_usb->handle,		USB_RECIP_ENDPOINT, USB_REQ_CLEAR_FEATURE, USB_FEATURE_HALT,		ep, NULL, 0, 3000));}intusb_ptp_get_device_status(PTP_USB* ptp_usb, uint16_t* devstatus);intusb_ptp_get_device_status(PTP_USB* ptp_usb, uint16_t* devstatus){	return (usb_control_msg(ptp_usb->handle,		USB_DP_DTH|USB_TYPE_CLASS|USB_RECIP_INTERFACE,		USB_REQ_GET_DEVICE_STATUS, 0, 0,		(char *)devstatus, 4, 3000));}intusb_ptp_device_reset(PTP_USB* ptp_usb);intusb_ptp_device_reset(PTP_USB* ptp_usb){	return (usb_control_msg(ptp_usb->handle,		USB_TYPE_CLASS|USB_RECIP_INTERFACE,		USB_REQ_DEVICE_RESET, 0, 0, NULL, 0, 3000));}voidreset_device (int busn, int devn, short force);voidreset_device (int busn, int devn, short force){	PTPParams params;	PTP_USB ptp_usb;	struct usb_device *dev;	uint16_t status;	uint16_t devstatus[2] = {0,0};	int ret;#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(&params, &ptp_usb, dev);		/* get device status (devices likes that regardless of its result)*/	usb_ptp_get_device_status(&ptp_usb,devstatus);		/* check the in endpoint status*/	ret = usb_get_endpoint_status(&ptp_usb,ptp_usb.inep,&status);	if (ret<0) perror ("usb_get_endpoint_status()");	/* and clear the HALT condition if happend*/	if (status) {		printf("Resetting input pipe!\n");		ret=usb_clear_stall_feature(&ptp_usb,ptp_usb.inep);		if (ret<0)perror ("usb_clear_stall_feature()");	}	status=0;	/* check the out endpoint status*/	ret = usb_get_endpoint_status(&ptp_usb,ptp_usb.outep,&status);	if (ret<0) perror ("usb_get_endpoint_status()");	/* and clear the HALT condition if happend*/	if (status) {		printf("Resetting output pipe!\n");		ret=usb_clear_stall_feature(&ptp_usb,ptp_usb.outep);		if (ret<0)perror ("usb_clear_stall_feature()");	}	status=0;	/* check the interrupt endpoint status*/	ret = usb_get_endpoint_status(&ptp_usb,ptp_usb.intep,&status);	if (ret<0)perror ("usb_get_endpoint_status()");	/* and clear the HALT condition if happend*/	if (status) {		printf ("Resetting interrupt pipe!\n");		ret=usb_clear_stall_feature(&ptp_usb,ptp_usb.intep);		if (ret<0)perror ("usb_clear_stall_feature()");	}	/* get device status (now there should be some results)*/	ret = usb_ptp_get_device_status(&ptp_usb,devstatus);	if (ret<0) 		perror ("usb_ptp_get_device_status()");	else	{		if (devstatus[1]==PTP_RC_OK) 			printf ("Device status OK\n");		else			printf ("Device status 0x%04x\n",devstatus[1]);	}		/* finally reset the device (that clears prevoiusly opened sessions)*/	ret = usb_ptp_device_reset(&ptp_usb);	if (ret<0)perror ("usb_ptp_device_reset()");	/* get device status (devices likes that regardless of its result)*/	usb_ptp_get_device_status(&ptp_usb,devstatus);	close_usb(&ptp_usb, dev);}/* main program  */intmain(int argc, char ** argv){	int busn=0,devn=0;	int action=0;	short force=0;	int overwrite=SKIP_IF_EXISTS;	uint16_t property=0;	char* value=NULL;	char* propstr=NULL;	uint32_t handle=0;	char *filename=NULL;	int num=0;	/* parse options */	int option_index = 0,opt;	static struct option loptions[] = {		{"help",0,0,'h'},		{"bus",1,0,0},		{"dev",1,0,0},		{"reset",0,0,'r'},		{"list-devices",0,0,'l'},		{"list-files",0,0,'L'},		{"list-operations",1,0,'o'},		{"list-properties",0,0,'p'},		{"show-all-properties",0,0,0},		{"show-unknown-properties",0,0,0},		{"show-property",1,0,'s'},		{"set-property",1,0,'s'},		{"set",1,0,0},		{"get-file",1,0,'g'},		{"get-all-files",0,0,'G'},		{"capture",0,0,'c'},		{"nikon-dc",0,0,0},		{"ndc",0,0,0},		{"nikon-ic",0,0,0},		{"nic",0,0,0},		{"nikon-dc2",0,0,0},		{"ndc2",0,0,0},		{"loop-capture",1,0,0},		{"delete-object",1,0,'d'},		{"delete-all-files",1,0,'D'},		{"info",0,0,'i'},		{"val",1,0,0},		{"filename",1,0,0},		{"overwrite",0,0,0},		{"force",0,0,'f'},		{"verbose",2,0,'v'},		{0,0,0,0}	};	/* register signal handlers */	signal(SIGINT, ptpcam_siginthandler);		while(1) {		opt = getopt_long (argc, argv, "LhlcipfroGg:Dd:s:v::", loptions, &option_index);		if (opt==-1) break;			switch (opt) {		/* set parameters */		case 0:			if (!(strcmp("val",loptions[option_index].name)))				value=strdup(optarg);			if (!(strcmp("filename",loptions[option_index].name)))				filename=strdup(optarg);			if (!(strcmp("overwrite",loptions[option_index].name)))				overwrite=OVERWRITE_EXISTING;			if (!(strcmp("bus",loptions[option_index].name)))				busn=strtol(optarg,NULL,10);			if (!(strcmp("dev",loptions[option_index].name)))				devn=strtol(optarg,NULL,10);			if (!(strcmp("loop-capture",loptions[option_index].name)))			{				action=ACT_LOOP_CAPTURE;				num=strtol(optarg,NULL,10);			}			if (!(strcmp("show-all-properties", loptions[option_index].name)))				action=ACT_SHOW_ALL_PROPERTIES;			if (!(strcmp("show-unknown-properties", loptions[option_index].name)))				action=ACT_SHOW_UNKNOWN_PROPERTIES;			if (!(strcmp("set",loptions[option_index].name)))			{				propstr=strdup(optarg);				action=ACT_SET_PROPBYNAME;			}			if (!strcmp("nikon-dc", loptions[option_index].name) ||			    !strcmp("ndc", loptions[option_index].name))			{			    action=ACT_NIKON_DC;			}			if (!strcmp("nikon-ic", loptions[option_index].name) ||			    !strcmp("nic", loptions[option_index].name))			{			    action=ACT_NIKON_IC;			}			if (!strcmp("nikon-dc2", loptions[option_index].name) ||			    !strcmp("ndc2", loptions[option_index].name))			{			    action=ACT_NIKON_DC2;			}			break;		case 'f':			force=~force;			break;		case 'v':			if (optarg) 				verbose=strtol(optarg,NULL,10);			else				verbose=1;			/*printf("VERBOSE LEVEL  = %i\n",verbose);*/			break;		/* actions */		case 'h':			help();			break;		case 'r':			action=ACT_DEVICE_RESET;			break;		case 'l':			action=ACT_LIST_DEVICES;			break;		case 'p':			action=ACT_LIST_PROPERTIES;			break;		case 's':			action=ACT_GETSET_PROPERTY;			property=strtol(optarg,NULL,16);			break;		case 'o':			action=ACT_LIST_OPERATIONS;			break;		case 'i':			action=ACT_SHOW_INFO;			break;		case 'c':			action=ACT_CAPTURE;			break;		case 'L':			action=ACT_LIST_FILES;			break;		case 'g':			action=ACT_GET_FILE;			handle=strtol(optarg,NULL,16);			break;		case 'G':			action=ACT_GET_ALL_FILES;			break;		case 'd':			action=ACT_DELETE_OBJECT;			handle=strtol(optarg,NULL,16);			break;		case 'D':			action=ACT_DELETE_ALL_FILES;		case '?':			break;		default:			fprintf(stderr,"getopt returned character code 0%o\n",				opt);			break;		}	}	if (argc==1) {		usage();		return 0;	}	switch (action) {		case ACT_DEVICE_RESET:			reset_device(busn,devn,force);			break;		case ACT_LIST_DEVICES:			list_devices(force);			break;		case ACT_LIST_PROPERTIES:			list_properties(busn,devn,force);			break;		case ACT_GETSET_PROPERTY:			getset_property(busn,devn,property,value,force);			break;		case ACT_SHOW_INFO:			show_info(busn,devn,force);			break;		case ACT_LIST_OPERATIONS:			list_operations(busn,devn,force);			break;		case ACT_LIST_FILES:			list_files(busn,devn,force);			break;		case ACT_GET_FILE:			get_file(busn,devn,force,handle,filename,overwrite);			break;		case ACT_GET_ALL_FILES:			get_all_files(busn,devn,force,overwrite);			break;		case ACT_CAPTURE:			capture_image(busn,devn,force);			break;		case ACT_DELETE_OBJECT:			delete_object(busn,devn,force,handle);			break;		case ACT_DELETE_ALL_FILES:			delete_all_files(busn,devn,force);			break;		case ACT_LOOP_CAPTURE:			loop_capture (busn,devn,force,num,overwrite);			break;		case ACT_SHOW_ALL_PROPERTIES:			show_all_properties(busn,devn,force,0);			break;		case ACT_SHOW_UNKNOWN_PROPERTIES:			show_all_properties(busn,devn,force,1);			break;		case ACT_SET_PROPBYNAME:			getset_propertybyname(busn,devn,propstr,value,force);			break;		case ACT_NIKON_DC:			nikon_direct_capture(busn,devn,force,filename,overwrite);			break;		case ACT_NIKON_IC:			nikon_initiate_dc (busn,devn,force);			break;		case ACT_NIKON_DC2:			nikon_direct_capture2(busn,devn,force,filename,overwrite);	}	return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -