Home | History | Annotate | Download | only in examples
      1 /**
      2  * \file detect.c
      3  * Example program to detect a device and list capabilities.
      4  *
      5  * Copyright (C) 2005-2008 Linus Walleij <triad (at) df.lth.se>
      6  * Copyright (C) 2007 Ted Bullock <tbullock (at) canada.com>
      7  *
      8  * This library is free software; you can redistribute it and/or
      9  * modify it under the terms of the GNU Lesser General Public
     10  * License as published by the Free Software Foundation; either
     11  * version 2 of the License, or (at your option) any later version.
     12  *
     13  * This library is distributed in the hope that it will be useful,
     14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     16  * Lesser General Public License for more details.
     17  *
     18  * You should have received a copy of the GNU Lesser General Public
     19  * License along with this library; if not, write to the
     20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
     21  * Boston, MA 02111-1307, USA.
     22  */
     23 #include "common.h"
     24 #include "util.h"
     25 #include <unistd.h>
     26 #include <stdlib.h>
     27 #include <stdio.h>
     28 #include <string.h>
     29 #include <errno.h>
     30 
     31 #define XML_BUFSIZE 0x10000
     32 
     33 static void dump_xml_fragment(uint8_t *buf, uint32_t len)
     34 {
     35   static int endianness = 0; // 0 = LE, 1 = BE
     36   uint32_t bp = 0;
     37 
     38   while (bp < len) {
     39     if (buf[bp+0] == 0xFF && buf[bp+1] == 0xFE) {
     40       endianness = 0;
     41     } else if (buf[bp+0] == 0xFE && buf[bp+1] == 0xff) {
     42       endianness = 1;
     43     } else {
     44       uint16_t tmp;
     45 
     46       if (endianness == 0) {
     47 	tmp = buf[bp+1] << 8 | buf[bp+0];
     48       } else {
     49 	tmp = buf[bp+0] << 8 | buf[bp+1];
     50       }
     51       // Fix this some day, we only print ISO 8859-1 correctly here,
     52       // should atleast support UTF-8.
     53       printf("%c", (uint8_t) tmp);
     54     }
     55     bp += 2;
     56   }
     57   printf("\n");
     58 }
     59 
     60 int main (int argc, char **argv)
     61 {
     62   LIBMTP_raw_device_t * rawdevices;
     63   int numrawdevices;
     64   LIBMTP_error_number_t err;
     65   int i;
     66 
     67   LIBMTP_Init();
     68 
     69   fprintf(stdout, "libmtp version: " LIBMTP_VERSION_STRING "\n\n");
     70 
     71   fprintf(stdout, "Listing raw device(s)\n");
     72   err = LIBMTP_Detect_Raw_Devices(&rawdevices, &numrawdevices);
     73   switch(err) {
     74   case LIBMTP_ERROR_NO_DEVICE_ATTACHED:
     75     fprintf(stdout, "   No raw devices found.\n");
     76     return 0;
     77   case LIBMTP_ERROR_CONNECTING:
     78     fprintf(stderr, "Detect: There has been an error connecting. Exiting\n");
     79     return 1;
     80   case LIBMTP_ERROR_MEMORY_ALLOCATION:
     81     fprintf(stderr, "Detect: Encountered a Memory Allocation Error. Exiting\n");
     82     return 1;
     83   case LIBMTP_ERROR_NONE:
     84     {
     85       int i;
     86 
     87       fprintf(stdout, "   Found %d device(s):\n", numrawdevices);
     88       for (i = 0; i < numrawdevices; i++) {
     89 	if (rawdevices[i].device_entry.vendor != NULL ||
     90 	    rawdevices[i].device_entry.product != NULL) {
     91 	  fprintf(stdout, "   %s: %s (%04x:%04x) @ bus %d, dev %d\n",
     92 		  rawdevices[i].device_entry.vendor,
     93 		  rawdevices[i].device_entry.product,
     94 		  rawdevices[i].device_entry.vendor_id,
     95 		  rawdevices[i].device_entry.product_id,
     96 		  rawdevices[i].bus_location,
     97 		  rawdevices[i].devnum);
     98 	} else {
     99 	  fprintf(stdout, "   %04x:%04x @ bus %d, dev %d\n",
    100 		  rawdevices[i].device_entry.vendor_id,
    101 		  rawdevices[i].device_entry.product_id,
    102 		  rawdevices[i].bus_location,
    103 		  rawdevices[i].devnum);
    104 	}
    105       }
    106     }
    107     break;
    108   case LIBMTP_ERROR_GENERAL:
    109   default:
    110     fprintf(stderr, "Unknown connection error.\n");
    111     return 1;
    112   }
    113 
    114   /* Iterate over connected MTP devices */
    115   fprintf(stdout, "Attempting to connect device(s)\n");
    116   for (i = 0; i < numrawdevices; i++) {
    117     LIBMTP_mtpdevice_t *device;
    118     LIBMTP_file_t *files;
    119     char *friendlyname;
    120     char *syncpartner;
    121     char *sectime;
    122     char *devcert;
    123     uint16_t *filetypes;
    124     uint16_t filetypes_len;
    125     uint8_t maxbattlevel;
    126     uint8_t currbattlevel;
    127     int ret;
    128 
    129     device = LIBMTP_Open_Raw_Device(&rawdevices[i]);
    130     if (device == NULL) {
    131       fprintf(stderr, "Unable to open raw device %d\n", i);
    132       continue;
    133     }
    134 
    135     LIBMTP_Dump_Errorstack(device);
    136     LIBMTP_Clear_Errorstack(device);
    137     LIBMTP_Dump_Device_Info(device);
    138 
    139     printf("MTP-specific device properties:\n");
    140     // The friendly name
    141     friendlyname = LIBMTP_Get_Friendlyname(device);
    142     if (friendlyname == NULL) {
    143       fprintf(stdout, "   Friendly name: (NULL)\n");
    144     } else {
    145       fprintf(stdout, "   Friendly name: %s\n", friendlyname);
    146       free(friendlyname);
    147     }
    148     syncpartner = LIBMTP_Get_Syncpartner(device);
    149     if (syncpartner == NULL) {
    150       fprintf(stdout, "   Synchronization partner: (NULL)\n");
    151     } else {
    152       fprintf(stdout, "   Synchronization partner: %s\n", syncpartner);
    153       free(syncpartner);
    154     }
    155 
    156     // Some battery info
    157     ret = LIBMTP_Get_Batterylevel(device, &maxbattlevel, &currbattlevel);
    158     if (ret == 0) {
    159       fprintf(stdout, "   Battery level %d of %d (%d%%)\n",currbattlevel, maxbattlevel,
    160 	      (int) ((float) currbattlevel/ (float) maxbattlevel * 100.0));
    161     } else {
    162       // Silently ignore. Some devices does not support getting the
    163       // battery level.
    164       LIBMTP_Clear_Errorstack(device);
    165     }
    166 
    167     ret = LIBMTP_Get_Supported_Filetypes(device, &filetypes, &filetypes_len);
    168     if (ret == 0) {
    169       uint16_t i;
    170 
    171       printf("libmtp supported (playable) filetypes:\n");
    172       for (i = 0; i < filetypes_len; i++) {
    173 	fprintf(stdout, "   %s\n", LIBMTP_Get_Filetype_Description(filetypes[i]));
    174       }
    175     } else {
    176       LIBMTP_Dump_Errorstack(device);
    177       LIBMTP_Clear_Errorstack(device);
    178     }
    179 
    180     // Secure time XML fragment
    181     ret = LIBMTP_Get_Secure_Time(device, &sectime);
    182     if (ret == 0 && sectime != NULL) {
    183       fprintf(stdout, "\nSecure Time:\n%s\n", sectime);
    184       free(sectime);
    185     } else {
    186       // Silently ignore - there may be devices not supporting secure time.
    187       LIBMTP_Clear_Errorstack(device);
    188     }
    189 
    190     // Device certificate XML fragment
    191     ret = LIBMTP_Get_Device_Certificate(device, &devcert);
    192     if (ret == 0 && devcert != NULL) {
    193       fprintf(stdout, "\nDevice Certificate:\n%s\n", devcert);
    194       free(devcert);
    195     } else {
    196       fprintf(stdout, "Unable to acquire device certificate, perhaps this device "
    197 	      "does not support this\n");
    198       LIBMTP_Dump_Errorstack(device);
    199       LIBMTP_Clear_Errorstack(device);
    200     }
    201 
    202     // Try to get Media player device info XML file...
    203     files = LIBMTP_Get_Filelisting_With_Callback(device, NULL, NULL);
    204     if (files != NULL) {
    205       LIBMTP_file_t *file, *tmp;
    206       file = files;
    207       while (file != NULL) {
    208 	if (!strcmp(file->filename, "WMPInfo.xml") ||
    209 	    !strcmp(file->filename, "WMPinfo.xml") ||
    210 	    !strcmp(file->filename, "default-capabilities.xml")) {
    211 	    if (file->item_id != 0) {
    212 	      /* Dump this file */
    213 	      FILE *xmltmp = tmpfile();
    214 	      int tmpfiledescriptor = fileno(xmltmp);
    215 
    216 	      if (tmpfiledescriptor != -1) {
    217 		int ret = LIBMTP_Get_Track_To_File_Descriptor(device,
    218 							      file->item_id,
    219 							      tmpfiledescriptor,
    220 							      NULL,
    221 							      NULL);
    222 		if (ret == 0) {
    223 		  uint8_t *buf = NULL;
    224 		  uint32_t readbytes;
    225 
    226 		  buf = malloc(XML_BUFSIZE);
    227 		  if (buf == NULL) {
    228 		    printf("Could not allocate %08x bytes...\n", XML_BUFSIZE);
    229 		    LIBMTP_Dump_Errorstack(device);
    230 		    LIBMTP_Clear_Errorstack(device);
    231 		    free(rawdevices);
    232 		    return 1;
    233 		  }
    234 
    235 		  lseek(tmpfiledescriptor, 0, SEEK_SET);
    236 		  readbytes = read(tmpfiledescriptor, (void*) buf, XML_BUFSIZE);
    237 
    238 		  if (readbytes >= 2 && readbytes < XML_BUFSIZE) {
    239 		    fprintf(stdout, "\n%s file contents:\n", file->filename);
    240 		    dump_xml_fragment(buf, readbytes);
    241 		  } else {
    242 		    perror("Unable to read file");
    243 		    LIBMTP_Dump_Errorstack(device);
    244 		    LIBMTP_Clear_Errorstack(device);
    245 		  }
    246 		  free(buf);
    247 		} else {
    248 		  LIBMTP_Dump_Errorstack(device);
    249 		  LIBMTP_Clear_Errorstack(device);
    250 		}
    251 		fclose(xmltmp);
    252 	      }
    253 	    }
    254 	}
    255 	tmp = file;
    256 	file = file->next;
    257 	LIBMTP_destroy_file_t(tmp);
    258       }
    259     }
    260     LIBMTP_Release_Device(device);
    261   } /* End For Loop */
    262 
    263   free(rawdevices);
    264 
    265   printf("OK.\n");
    266 
    267   return 0;
    268 }
    269