detect.c

00001 
00023 #include "common.h"
00024 #include <unistd.h>
00025 #include <stdlib.h>
00026 #include <stdio.h>
00027 #include <string.h>
00028 #include <errno.h>
00029 
00030 #define XML_BUFSIZE 0x10000
00031 
00032 static void dump_xml_fragment(uint8_t *buf, uint32_t len)
00033 {
00034   static int endianness = 0; // 0 = LE, 1 = BE
00035   uint32_t bp = 0;
00036   
00037   while (bp < len) {
00038     if (buf[bp+0] == 0xFF && buf[bp+1] == 0xFE) {
00039       endianness = 0;
00040     } else if (buf[bp+0] == 0xFE && buf[bp+1] == 0xff) {
00041       endianness = 1;
00042     } else {
00043       uint16_t tmp;
00044       
00045       if (endianness == 0) {
00046         tmp = buf[bp+1] << 8 | buf[bp+0];
00047       } else {
00048         tmp = buf[bp+0] << 8 | buf[bp+1];
00049       }
00050       // Fix this some day, we only print ISO 8859-1 correctly here,
00051       // should atleast support UTF-8.
00052       printf("%c", (uint8_t) tmp);
00053     }
00054     bp += 2;
00055   }
00056   printf("\n");
00057 }
00058 
00059 int main (int argc, char **argv)
00060 {
00061   LIBMTP_mtpdevice_t *device, *iter;
00062   LIBMTP_file_t *files;
00063   uint32_t xmlfileid = 0;
00064   char *friendlyname;
00065   char *syncpartner;
00066   char *sectime;
00067   char *devcert;
00068   uint16_t *filetypes;
00069   uint16_t filetypes_len;
00070   uint8_t maxbattlevel;
00071   uint8_t currbattlevel;
00072   uint32_t numdevices;
00073   int ret;
00074   int probeonly = 0;
00075 
00076   LIBMTP_Init();
00077 
00078   if (argc > 1 && !strcmp(argv[1], "-p")) {
00079     probeonly = 1;
00080   }
00081 
00082   if (probeonly) {
00083 //    uint16_t vid;
00084 //    uint16_t pid;
00085 //
00086 //    ret = LIBMTP_Detect_Descriptor(&vid, &pid);
00087 //    if (ret > 0) {
00088 //      printf("DETECTED MTP DEVICE WITH VID:%04x, PID:%04X\n", vid, pid);
00089 //      exit(0);
00090 //    } else {
00091 //      exit(1);
00092 //    }
00093     fprintf(stdout, "LIBMTP Panic: Probing has been disabled until it has "
00094             "been refactored to\nuse multiple devices\n");
00095   }
00096 
00097   fprintf(stdout, "Attempting to connect device(s)\n");
00098 
00099   switch(LIBMTP_Get_Connected_Devices(&device))
00100   {
00101   case LIBMTP_ERROR_NO_DEVICE_ATTACHED:
00102     fprintf(stdout, "Detect: No Devices have been found\n");
00103     return 0;
00104   case LIBMTP_ERROR_CONNECTING:
00105     fprintf(stderr, "Detect: There has been an error connecting. Exiting\n");
00106     return 1;
00107   case LIBMTP_ERROR_MEMORY_ALLOCATION:
00108     fprintf(stderr, "Detect: Encountered a Memory Allocation Error. Exiting\n");
00109     return 1;
00110  
00111   /* Unknown general errors - This should never execute */
00112   case LIBMTP_ERROR_GENERAL:
00113   default:
00114     fprintf(stderr, "Detect: There has been an unknown error, please report "
00115                     "this to the libmtp developers\n");
00116   return 1;
00117 
00118   /* Successfully connected at least one device, so continue */
00119   case LIBMTP_ERROR_NONE:
00120         numdevices = LIBMTP_Number_Devices_In_List(device);
00121     fprintf(stdout, "Detect: Successfully connected %u devices\n", numdevices);
00122   }
00123 
00124   /* iterate through connected MTP devices */
00125   for(iter = device; iter != NULL; iter = iter->next)
00126   {
00127   
00128   LIBMTP_Dump_Errorstack(iter);
00129   LIBMTP_Clear_Errorstack(iter);
00130   LIBMTP_Dump_Device_Info(iter);
00131   
00132   printf("MTP-specific device properties:\n");
00133   // The friendly name
00134   friendlyname = LIBMTP_Get_Friendlyname(iter);
00135   if (friendlyname == NULL) {
00136     fprintf(stdout, "   Friendly name: (NULL)\n");
00137   } else {
00138     fprintf(stdout, "   Friendly name: %s\n", friendlyname);
00139     free(friendlyname);
00140   }
00141   syncpartner = LIBMTP_Get_Syncpartner(iter);
00142   if (syncpartner == NULL) {
00143     fprintf(stdout, "   Synchronization partner: (NULL)\n");
00144   } else {
00145    fprintf(stdout, "   Synchronization partner: %s\n", syncpartner);
00146     free(syncpartner);
00147   }
00148 
00149   // Some battery info
00150   ret = LIBMTP_Get_Batterylevel(iter, &maxbattlevel, &currbattlevel);
00151   if (ret == 0) {
00152     fprintf(stdout, "   Battery level %d of %d (%d%%)\n",currbattlevel, maxbattlevel, 
00153            (int) ((float) currbattlevel/ (float) maxbattlevel * 100.0));
00154   } else {
00155     // Silently ignore. Some devices does not support getting the 
00156     // battery level.
00157     LIBMTP_Clear_Errorstack(iter);
00158   }
00159 
00160   ret = LIBMTP_Get_Supported_Filetypes(iter, &filetypes, &filetypes_len);
00161   if (ret == 0) {
00162     uint16_t i;
00163     
00164     printf("libmtp supported (playable) filetypes:\n");
00165     for (i = 0; i < filetypes_len; i++) {
00166       fprintf(stdout, "   %s\n", LIBMTP_Get_Filetype_Description(filetypes[i]));
00167     }
00168   } else {
00169     LIBMTP_Dump_Errorstack(iter);
00170     LIBMTP_Clear_Errorstack(iter);
00171   }
00172 
00173   // Secure time XML fragment
00174   ret = LIBMTP_Get_Secure_Time(iter, &sectime);
00175   if (ret == 0 && sectime != NULL) {
00176     fprintf(stdout, "\nSecure Time:\n%s\n", sectime);
00177     free(sectime);
00178   } else {
00179     // Silently ignore - there may be devices not supporting secure time.
00180     LIBMTP_Clear_Errorstack(iter);
00181   }
00182 
00183   // Device certificate XML fragment
00184   ret = LIBMTP_Get_Device_Certificate(iter, &devcert);
00185   if (ret == 0 && devcert != NULL) {
00186     fprintf(stdout, "\nDevice Certificate:\n%s\n", devcert);
00187     free(devcert);
00188   } else {
00189     fprintf(stdout, "Unable to acquire device certificate, perhaps this device "
00190                     "does not support this\n");
00191     LIBMTP_Dump_Errorstack(iter);
00192     LIBMTP_Clear_Errorstack(iter);
00193   }
00194 
00195   // Try to get Media player device info XML file...
00196   files = LIBMTP_Get_Filelisting_With_Callback(iter, NULL, NULL);
00197   if (files != NULL) {
00198     LIBMTP_file_t *file, *tmp;
00199     file = files;
00200     while (file != NULL) {
00201       if (!strcmp(file->filename, "WMPInfo.xml") ||
00202                 !strcmp(file->filename, "WMPinfo.xml"))
00203       {
00204         xmlfileid = file->item_id;
00205       }
00206       tmp = file;
00207       file = file->next;
00208       LIBMTP_destroy_file_t(tmp);
00209     }
00210   }
00211   if (xmlfileid == 0)
00212         fprintf(stdout, "WMPInfo.xml Does not exist on this device\n");
00213   if (xmlfileid != 0)
00214   {
00215     FILE *xmltmp = tmpfile();
00216     int tmpfiledescriptor = fileno(xmltmp);
00217     
00218     if (tmpfiledescriptor != -1)
00219     {
00220       int ret = LIBMTP_Get_Track_To_File_Descriptor(iter,
00221                                                     xmlfileid,
00222                                                     tmpfiledescriptor,
00223                                                     NULL,
00224                                                     NULL);
00225       if (ret == 0)
00226       {
00227         uint8_t *buf = NULL;
00228         uint32_t readbytes;
00229 
00230         buf = malloc(XML_BUFSIZE);
00231         if (buf == NULL)
00232         {
00233           printf("Could not allocate %08x bytes...\n", XML_BUFSIZE);
00234           LIBMTP_Dump_Errorstack(iter);
00235           LIBMTP_Clear_Errorstack(iter);
00236           LIBMTP_Release_Device_List(device);
00237           return 1;
00238         }
00239         
00240         lseek(tmpfiledescriptor, 0, SEEK_SET);
00241         readbytes = read(tmpfiledescriptor, (void*) buf, XML_BUFSIZE);
00242         
00243         if (readbytes >= 2 && readbytes < XML_BUFSIZE)
00244         {
00245           fprintf(stdout, "\nDevice description WMPInfo.xml file:\n");
00246           dump_xml_fragment(buf, readbytes);
00247         }
00248         else
00249         {
00250           perror("Unable to read WMPInfo.xml");
00251           LIBMTP_Dump_Errorstack(iter);
00252           LIBMTP_Clear_Errorstack(iter);
00253         }
00254         free(buf);
00255       }
00256       else
00257       {
00258         LIBMTP_Dump_Errorstack(iter);
00259         LIBMTP_Clear_Errorstack(iter);
00260       }
00261       fclose(xmltmp);
00262     }
00263   }
00264 
00265   } /* End For Loop */
00266 
00267   LIBMTP_Release_Device_List(device);
00268   printf("OK.\n");
00269   
00270   return 0; 
00271 }

Generated on Fri Sep 14 22:09:19 2007 for libmtp by  doxygen 1.5.1