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

📄 ptpcam.c

📁 PTP is the draft ptp protocol with all source under linux, can support most of usb still camera.
💻 C
📖 第 1 页 / 共 3 页
字号:
		else			printf("  0x%04x: 0x%04x\n",				params.deviceinfo.DevicePropertiesSupported[i],				params.deviceinfo.DevicePropertiesSupported[i]);	}	CR(ptp_closesession(&params), "Could not close session!\n");	close_usb(&ptp_usb, 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("%i",*(int32_t*)value);			return 0;		case PTP_DTC_UINT32:			if (hex==PTPCAM_PRINT_HEX)				printf("0x%08X (%i)",*(uint32_t*)value,					*(uint32_t*)value);			else				printf("%i",*(uint32_t*)value);			return 0;		case PTP_DTC_STR:			printf("\"%s\"",(char *)value);	}	return -1;}uint16_tset_property (PTPParams* params,		uint16_t property, char* value, uint16_t datatype);uint16_tset_property (PTPParams* params,		uint16_t property, 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 (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;	struct usb_device *dev;	PTPDevicePropDesc dpd;	const char* propdesc;	printf ("\n");#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);	CR(ptp_opensession (&params,1),		"Could not open session!\nTry to reset the camera.\n");	CR(ptp_getdeviceinfo (&params, &params.deviceinfo),		"Could not get device info\nTry to reset the camera.\n");	propdesc=ptp_get_property_name(&params,property);	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");		CR(ptp_closesession(&params), "Could not close session!\n"			"Try to reset the camera.\n");		return;	}	printf("Property '%s'\n",propdesc);	memset(&dpd,0,sizeof(dpd));	CR(ptp_getdevicepropdesc(&params,property,&dpd),		"Could not get device property description!\n"		"Try to reset the camera.\n");	if (verbose)		printf ("Data type is 0x%04x\n",dpd.DataType);	printf ("Current value is ");	if (dpd.FormFlag==PTP_DPFF_Enumeration)		PRINT_PROPVAL_DEC(dpd.CurrentValue);	else 		PRINT_PROPVAL_HEX(dpd.CurrentValue);	printf("\n");	if (value==NULL) {		printf ("Factory default value is ");		if (dpd.FormFlag==PTP_DPFF_Enumeration)			PRINT_PROPVAL_DEC(dpd.FactoryDefaultValue);		else 			PRINT_PROPVAL_HEX(dpd.FactoryDefaultValue);		printf("\n");		printf("The property is ");		if (dpd.GetSet==PTP_DPGS_Get)			printf ("read only");		else			printf ("settable");		switch (dpd.FormFlag) {		case PTP_DPFF_Enumeration:			printf (", enumerated. Allowed values are:\n");			{				int i;				for(i=0;i<dpd.FORM.Enum.NumberOfValues;i++){					PRINT_PROPVAL_HEX(					dpd.FORM.Enum.SupportedValue[i]);					printf("\n");				}			}			break;		case PTP_DPFF_Range:			printf (", within range:\n");			PRINT_PROPVAL_DEC(dpd.FORM.Range.MinimumValue);			printf(" - ");			PRINT_PROPVAL_DEC(dpd.FORM.Range.MaximumValue);			printf("; step size: ");			PRINT_PROPVAL_DEC(dpd.FORM.Range.StepSize);			printf("\n");			break;		case PTP_DPFF_None:			printf(".\n");		}	} else {		uint16_t r;		printf("Setting property value to '%s'\n",value);		r=(set_property(&params, property, value, dpd.DataType));		if (r!=PTP_RC_OK)		        ptp_perror(&params,r);	}	ptp_free_devicepropdesc(&dpd);	CR(ptp_closesession(&params), "Could not close session!\n"	"Try to reset the camera.\n");	close_usb(&ptp_usb, 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;	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-property",1,0,'s'},		{"set-property",1,0,'s'},		{"get-file",1,0,'g'},		{"get-all-files",0,0,'G'},		{"capture",0,0,'c'},		{"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}	};		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);			}			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);	}	return 0;}

⌨️ 快捷键说明

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