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     // Silently ignore. Some devices does not support getting the 
00133     // battery level.
00134   }
00135 
00136   ret = LIBMTP_Get_Supported_Filetypes(device, &filetypes, &filetypes_len);
00137   if (ret == 0) {
00138     uint16_t i;
00139     
00140     printf("libmtp supported (playable) filetypes:\n");
00141     for (i = 0; i < filetypes_len; i++) {
00142       printf("   %s\n", LIBMTP_Get_Filetype_Description(filetypes[i]));
00143     }
00144   }
00145 
00146   // Secure time XML fragment
00147   ret = LIBMTP_Get_Secure_Time(device, &sectime);
00148   if (ret == 0 && sectime != NULL) {
00149     printf("\nSecure Time:\n%s\n", sectime);
00150     free(sectime);
00151   }
00152 
00153   // Device certificate XML fragment
00154   ret = LIBMTP_Get_Device_Certificate(device, &devcert);
00155   if (ret == 0 && devcert != NULL) {
00156     printf("\nDevice Certificate:\n%s\n", devcert);
00157     free(devcert);
00158   }
00159 
00160   // Try to get Media player device info XML file...
00161   files = LIBMTP_Get_Filelisting(device);
00162   if (files != NULL) {
00163     LIBMTP_file_t *file, *tmp;
00164     file = files;
00165     while (file != NULL) {
00166       if (!strcmp(file->filename, "WMPInfo.xml")) {
00167         xmlfileid = file->item_id;
00168       }
00169       tmp = file;
00170       file = file->next;
00171       LIBMTP_destroy_file_t(tmp);
00172     }
00173   }
00174   if (xmlfileid != 0) {
00175     FILE *xmltmp = tmpfile();
00176     int tmpfile = fileno(xmltmp);
00177     
00178     if (tmpfile != -1) {
00179       int ret = LIBMTP_Get_Track_To_File_Descriptor(device, xmlfileid, tmpfile, NULL, NULL);
00180       if (ret == 0) {
00181         uint8_t *buf = NULL;
00182         uint32_t readbytes;
00183 
00184         buf = malloc(XML_BUFSIZE);
00185         if (buf == NULL) {
00186           printf("Could not allocate %08x bytes...\n", XML_BUFSIZE);
00187           exit(1);
00188         }
00189         lseek(tmpfile, 0, SEEK_SET);
00190         readbytes = read(tmpfile, (void*) buf, XML_BUFSIZE);
00191         
00192         if (readbytes >= 2 && readbytes < XML_BUFSIZE) {
00193           printf("\nDevice description WMPInfo.xml file:\n");
00194           dump_xml_fragment(buf, readbytes);
00195         }
00196       }
00197       fclose(xmltmp);
00198     }
00199   }
00200 
00201   // King Fisher of Triad rocks your world!
00202   LIBMTP_Release_Device(device);
00203   printf("OK.\n");
00204   exit (0);
00205 }

Generated on Mon Nov 6 02:45:44 2006 for libmtp by  doxygen 1.4.7