libmtp.c

来自「Media transfer protocol implementation o」· C语言 代码 · 共 2,016 行 · 第 1/5 页

C
2,016
字号
  dump_usbinfo(ptp_usb);  /* Print out some verbose information */  printf("Device info:\n");  printf("   Manufacturer: %s\n", params->deviceinfo.Manufacturer);  printf("   Model: %s\n", params->deviceinfo.Model);  printf("   Device version: %s\n", params->deviceinfo.DeviceVersion);  printf("   Serial number: %s\n", params->deviceinfo.SerialNumber);  printf("   Vendor extension ID: 0x%08x\n", params->deviceinfo.VendorExtensionID);  printf("   Vendor extension description: %s\n", params->deviceinfo.VendorExtensionDesc);  printf("Supported operations:\n");  for (i=0;i<params->deviceinfo.OperationsSupported_len;i++) {    char txt[256];    (void) ptp_render_opcode (params, params->deviceinfo.OperationsSupported[i], sizeof(txt), txt);    printf("   %04x: %s\n", params->deviceinfo.OperationsSupported[i], txt);  }  printf("Events supported:\n");  if (params->deviceinfo.EventsSupported_len == 0) {    printf("   None.\n");  } else {    for (i=0;i<params->deviceinfo.EventsSupported_len;i++) {      printf("   0x%04x\n", params->deviceinfo.EventsSupported[i]);    }  }  printf("Device Properties Supported:\n");  for (i=0;i<params->deviceinfo.DevicePropertiesSupported_len;i++) {    char const *propdesc = ptp_get_property_description(params, params->deviceinfo.DevicePropertiesSupported[i]);    if (propdesc != NULL) {      printf("   0x%04x: %s\n", params->deviceinfo.DevicePropertiesSupported[i], propdesc);    } else {      uint16_t prop = params->deviceinfo.DevicePropertiesSupported[i];      printf("   0x%04x: Unknown property\n", prop);    }  }  if (ptp_operation_issupported(params,PTP_OC_MTP_GetObjectPropsSupported)) {    printf("Playable File (Object) Types and Object Properties Supported:\n");    for (i=0;i<params->deviceinfo.ImageFormats_len;i++) {      char txt[256];      uint16_t ret;      uint16_t *props = NULL;      uint32_t propcnt = 0;      int j;      (void) ptp_render_ofc (params, params->deviceinfo.ImageFormats[i], sizeof(txt), txt);      printf("   %04x: %s\n", params->deviceinfo.ImageFormats[i], txt);      ret = ptp_mtp_getobjectpropssupported (params, params->deviceinfo.ImageFormats[i], &propcnt, &props);      if (ret != PTP_RC_OK) {	printf("      Error on query for object properties.\n");	printf("      Return code: 0x%04x (look this up in ptp.h for an explanation).\n",  ret);      } else {	for (j=0;j<propcnt;j++) {	  (void) ptp_render_mtp_propname(props[j],sizeof(txt),txt);	  printf("      %04x: %s\n", props[j], txt);	}	free(props);      }    }  }  printf("Special directories:\n");  printf("   Default music folder: 0x%08x\n", device->default_music_folder);  printf("   Default playlist folder: 0x%08x\n", device->default_playlist_folder);  printf("   Default picture folder: 0x%08x\n", device->default_picture_folder);  printf("   Default video folder: 0x%08x\n", device->default_video_folder);  printf("   Default organizer folder: 0x%08x\n", device->default_organizer_folder);  printf("   Default zencast folder: 0x%08x\n", device->default_zencast_folder);  printf("   Default album folder: 0x%08x\n", device->default_album_folder);}/** * This retrieves the model name (often equal to product name) * of an MTP device. * @param device a pointer to the device to get the model name for. * @return a newly allocated UTF-8 string representing the model name. *         The string must be freed by the caller after use. If the call *         was unsuccessful this will contain NULL. */char *LIBMTP_Get_Modelname(LIBMTP_mtpdevice_t *device){  char *retmodel = NULL;  PTPParams *params = (PTPParams *) device->params;  if (params->deviceinfo.Model != NULL) {    retmodel = strdup(params->deviceinfo.Model);  }  return retmodel;}/** * This retrieves the serial number of an MTP device. * @param device a pointer to the device to get the serial number for. * @return a newly allocated UTF-8 string representing the serial number. *         The string must be freed by the caller after use. If the call *         was unsuccessful this will contain NULL. */char *LIBMTP_Get_Serialnumber(LIBMTP_mtpdevice_t *device){  char *retnumber = NULL;  PTPParams *params = (PTPParams *) device->params;  if (params->deviceinfo.SerialNumber != NULL) {    retnumber = strdup(params->deviceinfo.SerialNumber);  }  return retnumber;}/** * This retrieves the device version (hardware and firmware version) of an * MTP device. * @param device a pointer to the device to get the device version for. * @return a newly allocated UTF-8 string representing the device version. *         The string must be freed by the caller after use. If the call *         was unsuccessful this will contain NULL. */char *LIBMTP_Get_Deviceversion(LIBMTP_mtpdevice_t *device){  char *retversion = NULL;  PTPParams *params = (PTPParams *) device->params;  if (params->deviceinfo.DeviceVersion != NULL) {    retversion = strdup(params->deviceinfo.DeviceVersion);  }  return retversion;}/** * This retrieves the "friendly name" of an MTP device. Usually * this is simply the name of the owner or something like * "John Doe's Digital Audio Player". This property should be supported * by all MTP devices. * @param device a pointer to the device to get the friendly name for. * @return a newly allocated UTF-8 string representing the friendly name. *         The string must be freed by the caller after use. * @see LIBMTP_Set_Friendlyname() */char *LIBMTP_Get_Friendlyname(LIBMTP_mtpdevice_t *device){  PTPPropertyValue propval;  char *retstring = NULL;  PTPParams *params = (PTPParams *) device->params;  if (!ptp_property_issupported(params, PTP_DPC_MTP_DeviceFriendlyName)) {    return NULL;  }  if (ptp_getdevicepropvalue(params,			     PTP_DPC_MTP_DeviceFriendlyName,			     &propval,			     PTP_DTC_STR) != PTP_RC_OK) {    return NULL;  }  if (propval.str != NULL) {    retstring = strdup(propval.str);    free(propval.str);  }  return retstring;}/** * Sets the "friendly name" of an MTP device. * @param device a pointer to the device to set the friendly name for. * @param friendlyname the new friendly name for the device. * @return 0 on success, any other value means failure. * @see LIBMTP_Get_Ownername() */int LIBMTP_Set_Friendlyname(LIBMTP_mtpdevice_t *device,			 char const * const friendlyname){  PTPPropertyValue propval;  PTPParams *params = (PTPParams *) device->params;  if (!ptp_property_issupported(params, PTP_DPC_MTP_DeviceFriendlyName)) {    return -1;  }  propval.str = (char *) friendlyname;  if (ptp_setdevicepropvalue(params,			     PTP_DPC_MTP_DeviceFriendlyName,			     &propval,			     PTP_DTC_STR) != PTP_RC_OK) {    return -1;  }  return 0;}/** * This retrieves the syncronization partner of an MTP device. This * property should be supported by all MTP devices. * @param device a pointer to the device to get the sync partner for. * @return a newly allocated UTF-8 string representing the synchronization *         partner. The string must be freed by the caller after use. * @see LIBMTP_Set_Syncpartner() */char *LIBMTP_Get_Syncpartner(LIBMTP_mtpdevice_t *device){  PTPPropertyValue propval;  char *retstring = NULL;  PTPParams *params = (PTPParams *) device->params;  if (!ptp_property_issupported(params, PTP_DPC_MTP_SynchronizationPartner)) {    return NULL;  }  if (ptp_getdevicepropvalue(params,			     PTP_DPC_MTP_SynchronizationPartner,			     &propval,			     PTP_DTC_STR) != PTP_RC_OK) {    return NULL;  }  if (propval.str != NULL) {    retstring = strdup(propval.str);    free(propval.str);  }  return retstring;}/** * Sets the synchronization partner of an MTP device. Note that * we have no idea what the effect of setting this to "foobar" * may be. But the general idea seems to be to tell which program * shall synchronize with this device and tell others to leave * it alone. * @param device a pointer to the device to set the sync partner for. * @param syncpartner the new synchronization partner for the device. * @return 0 on success, any other value means failure. * @see LIBMTP_Get_Syncpartner() */int LIBMTP_Set_Syncpartner(LIBMTP_mtpdevice_t *device,			 char const * const syncpartner){  PTPPropertyValue propval;  PTPParams *params = (PTPParams *) device->params;  if (!ptp_property_issupported(params, PTP_DPC_MTP_SynchronizationPartner)) {    return -1;  }  propval.str = (char *) syncpartner;  if (ptp_setdevicepropvalue(params,			     PTP_DPC_MTP_SynchronizationPartner,			     &propval,			     PTP_DTC_STR) != PTP_RC_OK) {    return -1;  }  return 0;}/** * This function finds out how much storage space is currently used * and any additional storage information. Storage may be a hard disk * or flash memory or whatever. * @param device a pointer to the device to get the storage info for. * @param total a pointer to a variable that will hold the *        total the total number of bytes available on this volume after *        the call. * @param free a pointer to a variable that will hold the number of *        free bytes available on this volume right now after the call. * @param storage_description a description of the storage. This may *        be NULL after the call even if it succeeded. If it is not NULL, *        it must be freed by the callee after use. * @param volume_label a volume label or similar. This may be NULL after the *        call even if it succeeded. If it is not NULL, it must be *        freed by the callee after use. * @return 0 if the storage info was successfully retrieved, any other *        value means failure. */int LIBMTP_Get_Storageinfo(LIBMTP_mtpdevice_t *device, uint64_t * const total,			uint64_t * const free, char ** const storage_description,			char ** const volume_label){  PTPStorageInfo storageInfo;  PTPParams *params = (PTPParams *) device->params;  if (!ptp_operation_issupported(params,PTP_OC_GetStorageInfo)) {    return -1;  }    if (ptp_getstorageinfo(params, device->storage_id, &storageInfo) != PTP_RC_OK) {    printf("LIBMTP_Get_Diskinfo(): failed to get disk info\n");    *total = 0;    *free = 0;    *storage_description = NULL;    *volume_label = NULL;    return -1;  }  *total = storageInfo.MaxCapability;  *free = storageInfo.FreeSpaceInBytes;  *storage_description = storageInfo.StorageDescription;  *volume_label = storageInfo.VolumeLabel;  return 0;}/** * Checks if the device can stora a file of this size or * if it's too big. * @param device a pointer to the device. * @param filesize the size of the file to check whether it will fit. * @return 0 if the file fits, any other value means failure. */static int check_if_file_fits(LIBMTP_mtpdevice_t *device, uint64_t const filesize) {  PTPParams *params = (PTPParams *) device->params;  uint64_t total;  uint64_t freebytes;  char *stdes = NULL;  char *vollab = NULL;  int ret;  // If we cannot check the storage, no big deal.  if (!ptp_operation_issupported(params,PTP_OC_GetStorageInfo)) {    return 0;  }    ret = LIBMTP_Get_Storageinfo(device, &total, &freebytes, &stdes, &vollab);  if (stdes != NULL) {    free(stdes);  }  if (vollab != NULL) {    free(vollab);  }  if (ret != 0) {    return -1;  } else {    if (filesize > freebytes) {      printf("check_if_file_fits(): device storage is full.\n");      return -1;    }  }  return 0;}/** * This function retrieves the current battery level on the device. * @param device a pointer to the device to get the battery level for. * @param maximum_level a pointer to a variable that will hold the *        maximum level of the battery if the call was successful. * @param current_level a pointer to a variable that will hold the *        current level of the battery if the call was successful. *        A value of 0 means that the device is on external power. * @return 0 if the storage info was successfully retrieved, any other *        means failure. A typical cause of failure is that *        the device does not support the battery level property. */int LIBMTP_Get_Batterylevel(LIBMTP_mtpdevice_t *device,			    uint8_t * const maximum_level,			    uint8_t * const current_level){  PTPPropertyValue propval;  uint16_t ret;  PTPParams *params = (PTPParams *) device->params;  *maximum_level = 0;  *current_level = 0;  if (!ptp_property_issupported(params, PTP_DPC_BatteryLevel)) {    return -1;  }  ret = ptp_getdevicepropvalue(params, PTP_DPC_BatteryLevel, &propval, PTP_DTC_UINT8);  if (ret != PTP_RC_OK) {    printf("LIBMTP_Get_Batterylevel(): could not get devcie property value.\n");    printf("Return code: 0x%04x (look this up in ptp.h for an explanation).\n",  ret);    return -1;  }  *maximum_level = device->maximum_battery_level;  *current_level = propval.u8;  return 0;}/** * Formats device storage (if the device supports the operation). * WARNING: This WILL delete all data from the device. Make sure you've * got confirmation from the user BEFORE you call this function. * * @param device a pointer to the device to format. * @return 0 on success, any other value means failure. */int LIBMTP_Format_Storage(LIBMTP_mtpdevice_t *device){  uint16_t ret;  PTPParams *params = (PTPParams *) device->params;  if (!ptp_operation_issupported(params,PTP_OC_FormatStore)) {    printf("LIBMTP_Format_Storage(): device cannot format storage\n");    return -1;  }  ret = ptp_formatstore(params, device->storage_id);  if (ret != PTP_RC_OK) {    printf("LIBMTP_Format_Storage(): failed to format storage\n");    printf("Return code: 0x%04x (look this up in ptp.h for an explanation).\n",  ret);    return -1;  }  return 0;}

⌨️ 快捷键说明

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