📄 ptpcanon.c
字号:
/* ptpcanon.c * * Based on ptpcam.c * Copyright (C) 2001-2003 Mariusz Woloszyn <emsi@ipartners.pl> * * Canon related parts * Copyright (C) 2003 Nikolai Kopanygin <superkolik@mail.ru> * * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */#include <stdio.h>#include <stdlib.h>#include <string.h>#include <getopt.h>#include <usb.h>#include <unistd.h>#ifdef ENABLE_NLS# include <libintl.h># undef _# define _(String) dgettext (GETTEXT_PACKAGE, String)# ifdef gettext_noop# define N_(String) gettext_noop (String)# else# define N_(String) (String)# endif#else# define textdomain(String) (String)# define gettext(String) (String)# define dgettext(Domain,Message) (Message)# define dcgettext(Domain,Message,Type) (Message)# define bindtextdomain(Domain,Directory) (Domain)# define _(String) (String)# define N_(String) (String)#endif#include "ptpcanon.h"#define MAXNAMELEN 256voidusage(){ printf("USAGE: ptpcanon [OPTION]\n\n");}voidhelp(){ printf("USAGE: ptpcanon [OPTION]\n\n"); printf("Options:\n" " -h, --help Print this help message\n" " -B, --bus=BUS-NUMBER USB bus number\n" " -D, --dev=DEV-NUMBER USB assigned device number\n" " -r, --reset Reset the device\n" " -l, --list-devices List all PTP devices\n" " -p, --list-properties List all PTP device properties\n" " " "(e.g. focus mode, focus distance, etc.)\n" " -s, --show-property=NUMBER Display property details " "(or set its value,\n" " if used in conjunction with --val)\n" " --set-property=NUMBER Set property value (--val required)\n" " --val=VALUE Property value\n" " -f, --force Talk to non PTP devices\n" " -v, --verbose Be verbosive (print more debug)\n" " -c, --canon-test Test a Canon PTP camera\n" " -t, --take-shot Take a photo and save it to disk as noname.jpg by default (see also --name)\n" " -N, --name FILENAME Save the taken photo under FILENAME\n" " -F, --flash FLASH Use this flash value with --take-shot\n", " -Z, --zoom ZOOM Use this zoom value with --take-shot\n", " -b, --on-button Capture on the Shutter button press, use with --take-shot\n", "\n");}voidlist_devices(short force){ struct usb_bus *bus; struct usb_device *dev; int found=0; bus=init_usb(); for (; bus; bus = bus->next) for (dev = bus->devices; dev; dev = dev->next) { /* if it's a PTP device try to talk to it */ if ((dev->config->interface->altsetting->bInterfaceClass== USB_CLASS_PTP)||force) if (dev->descriptor.bDeviceClass!=USB_CLASS_HUB) { int n; struct usb_endpoint_descriptor *ep; PTPParams params; PTP_USB ptp_usb; //int inep=0, outep=0, intep=0; PTPDeviceInfo deviceinfo; if (!found){ printf("Listing devices...\n"); printf("bus/dev\tvendorID/prodID\tdevice model\n"); found=1; } ep = dev->config->interface->altsetting->endpoint; n=dev->config->interface->altsetting->bNumEndpoints; /* find endpoints *//* for (i=0;i<n;i++) { if (ep[i].bmAttributes==2) { if ((ep[i].bEndpointAddress&0x80)==0x80) inep=ep[i].bEndpointAddress; if ((ep[i].bEndpointAddress&0x80)==0) outep=ep[i].bEndpointAddress; } else if (ep[i].bmAttributes==3) { if ((ep[i].bEndpointAddress&0x80)==0x80) intep=ep[i].bEndpointAddress; } } ptp_usb.inep=inep; ptp_usb.outep=outep; ptp_usb.intep=intep;*/ find_endpoints(dev,&ptp_usb.inep,&ptp_usb.outep, &ptp_usb.intep); init_ptp_usb(¶ms, &ptp_usb, dev); CC(ptp_opensession (¶ms,1), "Could not open session!\n" "Try to reset the camera.\n"); CC(ptp_getdeviceinfo (¶ms, &deviceinfo), "Could not get device info!\n"); printf("%s/%s\t0x%04X/0x%04X\t%s\n", bus->dirname, dev->filename, dev->descriptor.idVendor, dev->descriptor.idProduct, deviceinfo.Model); CC(ptp_closesession(¶ms), "Could not close session!\n"); usb_release_interface(ptp_usb.handle, dev->config->interface->altsetting->bInterfaceNumber); } } if (!found) printf("\nFound no PTP devices\n"); printf("\n");}const char*get_property_description(PTPParams* params, uint16_t dpc){ int i; // Device Property descriptions struct { uint16_t dpc; const char *txt; } ptp_device_properties[] = { {PTP_DPC_Undefined, N_("PTP Undefined Property")}, {PTP_DPC_BatteryLevel, N_("Battery Level")}, {PTP_DPC_FunctionalMode, N_("Functional Mode")}, {PTP_DPC_ImageSize, N_("Image Size")}, {PTP_DPC_CompressionSetting, N_("Compression Setting")}, {PTP_DPC_WhiteBalance, N_("White Balance")}, {PTP_DPC_RGBGain, N_("RGB Gain")}, {PTP_DPC_FNumber, N_("F-Number")}, {PTP_DPC_FocalLength, N_("Focal Length")}, {PTP_DPC_FocusDistance, N_("Focus Distance")}, {PTP_DPC_FocusMode, N_("Focus Mode")}, {PTP_DPC_ExposureMeteringMode, N_("Exposure Metering Mode")}, {PTP_DPC_FlashMode, N_("Flash Mode")}, {PTP_DPC_ExposureTime, N_("Exposure Time")}, {PTP_DPC_ExposureProgramMode, N_("Exposure Program Mode")}, {PTP_DPC_ExposureIndex, N_("Exposure Index (film speed ISO)")}, {PTP_DPC_ExposureBiasCompensation, N_("Exposure Bias Compensation")}, {PTP_DPC_DateTime, N_("Date Time")}, {PTP_DPC_CaptureDelay, N_("Pre-Capture Delay")}, {PTP_DPC_StillCaptureMode, N_("Still Capture Mode")}, {PTP_DPC_Contrast, N_("Contrast")}, {PTP_DPC_Sharpness, N_("Sharpness")}, {PTP_DPC_DigitalZoom, N_("Digital Zoom")}, {PTP_DPC_EffectMode, N_("Effect Mode")}, {PTP_DPC_BurstNumber, N_("Burst Number")}, {PTP_DPC_BurstInterval, N_("Burst Interval")}, {PTP_DPC_TimelapseNumber, N_("Timelapse Number")}, {PTP_DPC_TimelapseInterval, N_("Timelapse Interval")}, {PTP_DPC_FocusMeteringMode, N_("Focus Metering Mode")}, {PTP_DPC_UploadURL, N_("Upload URL")}, {PTP_DPC_Artist, N_("Artist")}, {PTP_DPC_CopyrightInfo, N_("Copyright Info")}, {0,NULL} }; struct { uint16_t dpc; const char *txt; } ptp_device_properties_EK[] = { {PTP_DPC_EK_ColorTemperature, N_("EK: Color Temperature")}, {PTP_DPC_EK_DateTimeStampFormat, N_("EK: Date Time Stamp Format")}, {PTP_DPC_EK_BeepMode, N_("EK: Beep Mode")}, {PTP_DPC_EK_VideoOut, N_("EK: Video Out")}, {PTP_DPC_EK_PowerSaving, N_("EK: Power Saving")}, {PTP_DPC_EK_UI_Language, N_("EK: UI Language")}, {0,NULL} }; struct { uint16_t dpc; const char *txt; } ptp_device_properties_CANON[] = { {PTP_DPC_CANON_BeepMode, N_("CANON: Beep Mode")}, {PTP_DPC_CANON_UnixTime, N_("CANON: Time measured in" " secondssince 01-01-1970")}, {PTP_DPC_CANON_FlashMemory, N_("CANON: Flash Card Capacity")}, {PTP_DPC_CANON_CameraModel, N_("CANON: Camera Model")}, {0,NULL} }; if (dpc|PTP_DPC_EXTENSION_MASK==PTP_DPC_EXTENSION) switch (params->deviceinfo.VendorExtensionID) { case PTP_VENDOR_EASTMAN_KODAK: for (i=0; ptp_device_properties_EK[i].txt!=NULL; i++) if (ptp_device_properties_EK[i].dpc==dpc) return (ptp_device_properties_EK[i].txt); break; case PTP_VENDOR_CANON: for (i=0; ptp_device_properties_CANON[i].txt!=NULL; i++) if (ptp_device_properties_CANON[i].dpc==dpc) return (ptp_device_properties_CANON[i].txt); break; } for (i=0; ptp_device_properties[i].txt!=NULL; i++) if (ptp_device_properties[i].dpc==dpc) return (ptp_device_properties[i].txt); return NULL;}voidlist_properties (int busn, int devn, short force){ PTPParams params; PTP_USB ptp_usb; struct usb_device *dev; const char* propdesc; int i; printf("Listing properties...\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(¶ms, &ptp_usb, dev); CR(ptp_opensession (¶ms,1), "Could not open session!\n"); CR(ptp_getdeviceinfo (¶ms, ¶ms.deviceinfo), "Could not get device info\n"); printf("Querying: %s\n",params.deviceinfo.Model); for (i=0; i<params.deviceinfo.DevicePropertiesSupported_len;i++){ propdesc=get_property_description(¶ms, params.deviceinfo.DevicePropertiesSupported[i]); if (propdesc!=NULL) printf("0x%04x : %s\n",params.deviceinfo. DevicePropertiesSupported[i], propdesc); else printf("0x%04x : 0x%04x\n",params.deviceinfo. DevicePropertiesSupported[i], params.deviceinfo. DevicePropertiesSupported[i]); } CR(ptp_closesession(¶ms), "Could not close session!\n"); usb_release_interface(ptp_usb.handle, dev->config->interface->altsetting->bInterfaceNumber);}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;}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;#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!\nTry to reset the camera.\n"); CR(ptp_getdeviceinfo (¶ms, ¶ms.deviceinfo), "Could not get device info\nTry to reset the camera.\n"); propdesc=get_property_description(¶ms,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(¶ms, property)) { fprintf(stderr,"The device does not support this property!\n"); CR(ptp_closesession(¶ms), "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(¶ms,property,&dpd), "Could not get device property description!\n" "Try to reset the camera.\n"); 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"); printf ("Factory default value is "); if (dpd.FormFlag==PTP_DPFF_Enumeration) PRINT_PROPVAL_DEC(dpd.FactoryDefaultValue); else PRINT_PROPVAL_HEX(dpd.FactoryDefaultValue);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -