libmtp: detect.c

- Main Page
- Modules
- Data Structures
- Files
- Directories
- Examples

# detect.c

    00001 #include "common.h"
    00002 #include <unistd.h>
    00003 #include <stdlib.h>
    00004 #include <stdio.h>
    00005 #include <string.h>
    00006
    00007 #define XML_BUFSIZE 0x10000
    00008
    00009 static void dump_xml_fragment(uint8_t *buf, uint32_t len)
    00010 {
    00011   static int endianness = 0; // 0 = LE, 1 = BE
    00012   uint32_t bp = 0;
    00013
    00014   while (bp < len) {
    00015     if (buf[bp+0] == 0xFF && buf[bp+1] == 0xFE) {
    00016       endianness = 0;
    00017     } else if (buf[bp+0] == 0xFE && buf[bp+1] == 0xff) {
    00018       endianness = 1;
    00019     } else {
    00020       uint16_t tmp;
    00021
    00022       if (endianness == 0) {
    00023         tmp = buf[bp+1] << 8 | buf[bp+0];
    00024       } else {
    00025         tmp = buf[bp+0] << 8 | buf[bp+1];
    00026       }
    00027       // Fix this some day, we only print ISO 8859-1 correctly here,
    00028       // should atleast support UTF-8.
    00029       printf("%c", (uint8_t) tmp);
    00030     }
    00031     bp += 2;
    00032   }
    00033   printf("\n");
    00034 }
    00035
    00036 int main (int argc, char **argv)
    00037 {
    00038   LIBMTP_mtpdevice_t *device;
    00039   LIBMTP_file_t *files;
    00040   uint32_t xmlfileid = 0;
    00041   uint64_t totalbytes;
    00042   uint64_t freebytes;
    00043   char *storage_description;
    00044   char *volume_label;
    00045   char *friendlyname;
    00046   char *syncpartner;
    00047   char *sectime;
    00048   char *devcert;
    00049   uint16_t *filetypes;
    00050   uint16_t filetypes_len;
    00051   uint8_t maxbattlevel;
    00052   uint8_t currbattlevel;
    00053   int ret;
    00054   int probeonly = 0;
    00055
    00056   LIBMTP_Init();
    00057
    00058   if (argc > 1 && !strcmp(argv[1], "-p")) {
    00059     probeonly = 1;
    00060   }
    00061
    00062   if (probeonly) {
    00063     uint16_t vid;
    00064     uint16_t pid;
    00065
    00066     ret = LIBMTP_Detect_Descriptor(&vid, &pid);
    00067     if (ret > 0) {
    00068       printf("DETECTED MTP DEVICE WITH VID:%04x, PID:%04X\n", vid, pid);
    00069       exit(0);
    00070     } else {
    00071       exit(1);
    00072     }
    00073   }
    00074
    00075   device = LIBMTP_Get_First_Device();
    00076   if (device == NULL) {
    00077     printf("No devices.\n");
    00078     exit (0);
    00079   }
    00080
    00081   LIBMTP_Dump_Device_Info(device);
    00082
    00083   printf("MTP-specific device properties:\n");
    00084   // The friendly name
    00085   friendlyname = LIBMTP_Get_Friendlyname(device);
    00086   if (friendlyname == NULL) {
    00087     printf("   Friendly name: (NULL)\n");
    00088   } else {
    00089     printf("   Friendly name: %s\n", friendlyname);
    00090     free(friendlyname);
    00091   }
    00092   syncpartner = LIBMTP_Get_Syncpartner(device);
    00093   if (syncpartner == NULL) {
    00094     printf("   Synchronization partner: (NULL)\n");
    00095   } else {
    00096     printf("   Synchronization partner: %s\n", syncpartner);
    00097     free(syncpartner);
    00098   }
    00099
    00100   // Some storage info
    00101   ret = LIBMTP_Get_Storageinfo(device, &totalbytes, &freebytes, &storage_description, &volume_label);
    00102   if (ret == 0) {
    00103 #ifdef __WIN32__
    00104     printf("   Total bytes on device: %I64u (%I64u MB)\n",
    00105            totalbytes, totalbytes/(1024*1024));
    00106     printf("   Free bytes on device: %I64u (%I64u MB)\n",
    00107            freebytes, freebytes/(1024*1024));
    00108 #else
    00109     printf("   Total bytes on device: %llu (%llu MB)\n",
    00110            totalbytes, totalbytes/(1024*1024));
    00111     printf("   Free bytes on device: %llu (%llu MB)\n",
    00112            freebytes, freebytes/(1024*1024));
    00113 #endif
    00114     if (storage_description != NULL) {
    00115       printf("   Storage description: \"%s\"\n", storage_description);
    00116       free(storage_description);
    00117     }
    00118     if (volume_label != NULL) {
    00119       printf("   Volume label: \"%s\"\n", volume_label);
    00120       free(volume_label);
    00121     }
    00122   } else {
    00123     printf("   Error getting disk info...\n");
    00124   }
    00125
    00126   // Some battery info
    00127   ret = LIBMTP_Get_Batterylevel(device, &maxbattlevel, &currbattlevel);
    00128   if (ret == 0) {
    00129     printf("   Battery level %d of %d (%d%%)\n",currbattlevel, maxbattlevel,
    00130            (int) ((float) currbattlevel/ (float) maxbattlevel * 100.0));
    00131   } else {
    00132     printf("   Error getting battery info...\n");
    00133   }
    00134
    00135   ret = LIBMTP_Get_Supported_Filetypes(device, &filetypes, &filetypes_len);
    00136   if (ret == 0) {
    00137     uint16_t i;
    00138
    00139     printf("libmtp supported (playable) filetypes:\n");
    00140     for (i = 0; i < filetypes_len; i++) {
    00141       printf("   %s\n", LIBMTP_Get_Filetype_Description(filetypes[i]));
    00142     }
    00143   }
    00144
    00145   // Secure time XML fragment
    00146   ret = LIBMTP_Get_Secure_Time(device, &sectime);
    00147   if (ret == 0 && sectime != NULL) {
    00148     printf("\nSecure Time:\n%s\n", sectime);
    00149     free(sectime);
    00150   }
    00151
    00152   // Device certificate XML fragment
    00153   ret = LIBMTP_Get_Device_Certificate(device, &devcert);
    00154   if (ret == 0 && devcert != NULL) {
    00155     printf("\nDevice Certificate:\n%s\n", devcert);
    00156     free(devcert);
    00157   }
    00158
    00159   // Try to get device info XML file...
    00160   files = LIBMTP_Get_Filelisting(device);
    00161   if (files != NULL) {
    00162     LIBMTP_file_t *file, *tmp;
    00163     file = files;
    00164     while (file != NULL) {
    00165       if (!strcmp(file->filename, "WMPInfo.xml")) {
    00166         xmlfileid = file->item_id;
    00167       }
    00168       tmp = file;
    00169       file = file->next;
    00170       LIBMTP_destroy_file_t(tmp);
    00171     }
    00172   }
    00173   if (xmlfileid != 0) {
    00174     FILE *xmltmp = tmpfile();
    00175     int tmpfile = fileno(xmltmp);
    00176
    00177     if (tmpfile != -1) {
    00178       int ret = LIBMTP_Get_Track_To_File_Descriptor(device, xmlfileid, tmpfile, NULL, NULL);
    00179       if (ret == 0) {
    00180         uint8_t *buf = NULL;
    00181         uint32_t readbytes;
    00182
    00183         buf = malloc(XML_BUFSIZE);
    00184         if (buf == NULL) {
    00185           printf("Could not allocate %08x bytes...\n", XML_BUFSIZE);
    00186           exit(1);
    00187         }
    00188         lseek(tmpfile, 0, SEEK_SET);
    00189         readbytes = read(tmpfile, (void*) buf, XML_BUFSIZE);
    00190
    00191         if (readbytes >= 2 && readbytes < XML_BUFSIZE) {
    00192           printf("\nDevice description WMPInfo.xml file:\n");
    00193           dump_xml_fragment(buf, readbytes);
    00194         }
    00195       }
    00196       fclose(xmltmp);
    00197     }
    00198   }
    00199
    00200   // King Fisher of Triad rocks your world!
    00201   LIBMTP_Release_Device(device);
    00202   printf("OK.\n");
    00203   exit (0);
    00204 }