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   char *friendlyname;
    00042   char *syncpartner;
    00043   char *sectime;
    00044   char *devcert;
    00045   uint16_t *filetypes;
    00046   uint16_t filetypes_len;
    00047   uint8_t maxbattlevel;
    00048   uint8_t currbattlevel;
    00049   int ret;
    00050   int probeonly = 0;
    00051
    00052   LIBMTP_Init();
    00053
    00054   if (argc > 1 && !strcmp(argv[1], "-p")) {
    00055     probeonly = 1;
    00056   }
    00057
    00058   if (probeonly) {
    00059     uint16_t vid;
    00060     uint16_t pid;
    00061
    00062     ret = LIBMTP_Detect_Descriptor(&vid, &pid);
    00063     if (ret > 0) {
    00064       printf("DETECTED MTP DEVICE WITH VID:%04x, PID:%04X\n", vid, pid);
    00065       exit(0);
    00066     } else {
    00067       exit(1);
    00068     }
    00069   }
    00070
    00071   device = LIBMTP_Get_First_Device();
    00072   if (device == NULL) {
    00073     printf("No devices.\n");
    00074     exit (0);
    00075   }
    00076
    00077   LIBMTP_Dump_Device_Info(device);
    00078
    00079   printf("MTP-specific device properties:\n");
    00080   // The friendly name
    00081   friendlyname = LIBMTP_Get_Friendlyname(device);
    00082   if (friendlyname == NULL) {
    00083     printf("   Friendly name: (NULL)\n");
    00084   } else {
    00085     printf("   Friendly name: %s\n", friendlyname);
    00086     free(friendlyname);
    00087   }
    00088   syncpartner = LIBMTP_Get_Syncpartner(device);
    00089   if (syncpartner == NULL) {
    00090     printf("   Synchronization partner: (NULL)\n");
    00091   } else {
    00092     printf("   Synchronization partner: %s\n", syncpartner);
    00093     free(syncpartner);
    00094   }
    00095
    00096   // Some battery info
    00097   ret = LIBMTP_Get_Batterylevel(device, &maxbattlevel, &currbattlevel);
    00098   if (ret == 0) {
    00099     printf("   Battery level %d of %d (%d%%)\n",currbattlevel, maxbattlevel,
    00100            (int) ((float) currbattlevel/ (float) maxbattlevel * 100.0));
    00101   } else {
    00102     // Silently ignore. Some devices does not support getting the
    00103     // battery level.
    00104   }
    00105
    00106   ret = LIBMTP_Get_Supported_Filetypes(device, &filetypes, &filetypes_len);
    00107   if (ret == 0) {
    00108     uint16_t i;
    00109
    00110     printf("libmtp supported (playable) filetypes:\n");
    00111     for (i = 0; i < filetypes_len; i++) {
    00112       printf("   %s\n", LIBMTP_Get_Filetype_Description(filetypes[i]));
    00113     }
    00114   }
    00115
    00116   // Secure time XML fragment
    00117   ret = LIBMTP_Get_Secure_Time(device, &sectime);
    00118   if (ret == 0 && sectime != NULL) {
    00119     printf("\nSecure Time:\n%s\n", sectime);
    00120     free(sectime);
    00121   }
    00122
    00123   // Device certificate XML fragment
    00124   ret = LIBMTP_Get_Device_Certificate(device, &devcert);
    00125   if (ret == 0 && devcert != NULL) {
    00126     printf("\nDevice Certificate:\n%s\n", devcert);
    00127     free(devcert);
    00128   }
    00129
    00130   // Try to get Media player device info XML file...
    00131   files = LIBMTP_Get_Filelisting_With_Callback(device, NULL, NULL);
    00132   if (files != NULL) {
    00133     LIBMTP_file_t *file, *tmp;
    00134     file = files;
    00135     while (file != NULL) {
    00136       if (!strcmp(file->filename, "WMPInfo.xml")) {
    00137         xmlfileid = file->item_id;
    00138       }
    00139       tmp = file;
    00140       file = file->next;
    00141       LIBMTP_destroy_file_t(tmp);
    00142     }
    00143   }
    00144   if (xmlfileid != 0) {
    00145     FILE *xmltmp = tmpfile();
    00146     int tmpfile = fileno(xmltmp);
    00147
    00148     if (tmpfile != -1) {
    00149       int ret = LIBMTP_Get_Track_To_File_Descriptor(device, xmlfileid, tmpfile, NULL, NULL);
    00150       if (ret == 0) {
    00151         uint8_t *buf = NULL;
    00152         uint32_t readbytes;
    00153
    00154         buf = malloc(XML_BUFSIZE);
    00155         if (buf == NULL) {
    00156           printf("Could not allocate %08x bytes...\n", XML_BUFSIZE);
    00157           exit(1);
    00158         }
    00159         lseek(tmpfile, 0, SEEK_SET);
    00160         readbytes = read(tmpfile, (void*) buf, XML_BUFSIZE);
    00161
    00162         if (readbytes >= 2 && readbytes < XML_BUFSIZE) {
    00163           printf("\nDevice description WMPInfo.xml file:\n");
    00164           dump_xml_fragment(buf, readbytes);
    00165         }
    00166       }
    00167       fclose(xmltmp);
    00168     }
    00169   }
    00170
    00171   // King Fisher of Triad rocks your world!
    00172   LIBMTP_Release_Device(device);
    00173   printf("OK.\n");
    00174   exit (0);
    00175 }