• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * \file detect.c
3  * Example program to detect a device and list capabilities.
4  *
5  * Copyright (C) 2005-2015 Linus Walleij <triad@df.lth.se>
6  * Copyright (C) 2007 Ted Bullock <tbullock@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 
dump_xml_fragment(uint8_t * buf,uint32_t len)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 
main(int argc,char ** argv)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   int opt;
68   extern int optind;
69   extern char *optarg;
70 
71   while ((opt = getopt(argc, argv, "d")) != -1 ) {
72     switch (opt) {
73     case 'd':
74       LIBMTP_Set_Debug(LIBMTP_DEBUG_PTP | LIBMTP_DEBUG_DATA);
75       break;
76     }
77   }
78 
79   argc -= optind;
80   argv += optind;
81 
82   LIBMTP_Init();
83 
84   fprintf(stdout, "libmtp version: " LIBMTP_VERSION_STRING "\n\n");
85 
86   fprintf(stdout, "Listing raw device(s)\n");
87   err = LIBMTP_Detect_Raw_Devices(&rawdevices, &numrawdevices);
88   switch(err) {
89   case LIBMTP_ERROR_NO_DEVICE_ATTACHED:
90     fprintf(stdout, "   No raw devices found.\n");
91     return 0;
92   case LIBMTP_ERROR_CONNECTING:
93     fprintf(stderr, "Detect: There has been an error connecting. Exiting\n");
94     return 1;
95   case LIBMTP_ERROR_MEMORY_ALLOCATION:
96     fprintf(stderr, "Detect: Encountered a Memory Allocation Error. Exiting\n");
97     return 1;
98   case LIBMTP_ERROR_NONE:
99     {
100       int i;
101 
102       fprintf(stdout, "   Found %d device(s):\n", numrawdevices);
103       for (i = 0; i < numrawdevices; i++) {
104 	if (rawdevices[i].device_entry.vendor != NULL ||
105 	    rawdevices[i].device_entry.product != NULL) {
106 	  fprintf(stdout, "   %s: %s (%04x:%04x) @ bus %d, dev %d\n",
107 		  rawdevices[i].device_entry.vendor,
108 		  rawdevices[i].device_entry.product,
109 		  rawdevices[i].device_entry.vendor_id,
110 		  rawdevices[i].device_entry.product_id,
111 		  rawdevices[i].bus_location,
112 		  rawdevices[i].devnum);
113 	} else {
114 	  fprintf(stdout, "   %04x:%04x @ bus %d, dev %d\n",
115 		  rawdevices[i].device_entry.vendor_id,
116 		  rawdevices[i].device_entry.product_id,
117 		  rawdevices[i].bus_location,
118 		  rawdevices[i].devnum);
119 	}
120       }
121     }
122     break;
123   case LIBMTP_ERROR_GENERAL:
124   default:
125     fprintf(stderr, "Unknown connection error.\n");
126     return 1;
127   }
128 
129   /* Iterate over connected MTP devices */
130   fprintf(stdout, "Attempting to connect device(s)\n");
131   for (i = 0; i < numrawdevices; i++) {
132     LIBMTP_mtpdevice_t *device;
133     LIBMTP_devicestorage_t *storage;
134     char *friendlyname;
135     char *syncpartner;
136     char *sectime;
137     char *devcert;
138     uint16_t *filetypes;
139     uint16_t filetypes_len;
140     uint8_t maxbattlevel;
141     uint8_t currbattlevel;
142     int ret;
143 
144     device = LIBMTP_Open_Raw_Device_Uncached(&rawdevices[i]);
145     if (device == NULL) {
146       fprintf(stderr, "Unable to open raw device %d\n", i);
147       continue;
148     }
149 
150     LIBMTP_Dump_Errorstack(device);
151     LIBMTP_Clear_Errorstack(device);
152     LIBMTP_Dump_Device_Info(device);
153 
154     printf("MTP-specific device properties:\n");
155     // The friendly name
156     friendlyname = LIBMTP_Get_Friendlyname(device);
157     if (friendlyname == NULL) {
158       fprintf(stdout, "   Friendly name: (NULL)\n");
159     } else {
160       fprintf(stdout, "   Friendly name: %s\n", friendlyname);
161       free(friendlyname);
162     }
163     syncpartner = LIBMTP_Get_Syncpartner(device);
164     if (syncpartner == NULL) {
165       fprintf(stdout, "   Synchronization partner: (NULL)\n");
166     } else {
167       fprintf(stdout, "   Synchronization partner: %s\n", syncpartner);
168       free(syncpartner);
169     }
170 
171     // Some battery info
172     ret = LIBMTP_Get_Batterylevel(device, &maxbattlevel, &currbattlevel);
173     if (ret == 0) {
174       fprintf(stdout, "   Battery level %d of %d (%d%%)\n",currbattlevel, maxbattlevel,
175 	      (int) ((float) currbattlevel/ (float) maxbattlevel * 100.0));
176     } else {
177       // Silently ignore. Some devices does not support getting the
178       // battery level.
179       LIBMTP_Clear_Errorstack(device);
180     }
181 
182     ret = LIBMTP_Get_Supported_Filetypes(device, &filetypes, &filetypes_len);
183     if (ret == 0) {
184       uint16_t i;
185 
186       printf("libmtp supported (playable) filetypes:\n");
187       for (i = 0; i < filetypes_len; i++) {
188 	fprintf(stdout, "   %s\n", LIBMTP_Get_Filetype_Description(filetypes[i]));
189       }
190     } else {
191       LIBMTP_Dump_Errorstack(device);
192       LIBMTP_Clear_Errorstack(device);
193     }
194 
195     // Secure time XML fragment
196     ret = LIBMTP_Get_Secure_Time(device, &sectime);
197     if (ret == 0 && sectime != NULL) {
198       fprintf(stdout, "\nSecure Time:\n%s\n", sectime);
199       free(sectime);
200     } else {
201       // Silently ignore - there may be devices not supporting secure time.
202       LIBMTP_Clear_Errorstack(device);
203     }
204 
205     // Device certificate XML fragment
206     if (rawdevices[i].device_entry.vendor_id == 0x041e) {
207       /*
208        * This code is currently disabled except for vendors we
209        * know does support it: all devices say that
210        * they support getting a device certificate but a lot of
211        * them obviously doesn't, instead they crash when you try
212        * to obtain it.
213        */
214       ret = LIBMTP_Get_Device_Certificate(device, &devcert);
215       if (ret == 0 && devcert != NULL) {
216 	fprintf(stdout, "\nDevice Certificate:\n%s\n", devcert);
217 	free(devcert);
218       } else {
219 	fprintf(stdout, "Unable to acquire device certificate, perhaps this device "
220 		"does not support this\n");
221 	LIBMTP_Dump_Errorstack(device);
222 	LIBMTP_Clear_Errorstack(device);
223       }
224     }
225 
226     /* Try to get Media player device info XML file... */
227     /* Loop over storages */
228     for (storage = device->storage; storage != 0; storage = storage->next) {
229       LIBMTP_file_t *files;
230 
231       /* Get file listing for the root directory, no other dirs */
232       files = LIBMTP_Get_Files_And_Folders(device,
233 					   storage->id,
234 					   LIBMTP_FILES_AND_FOLDERS_ROOT);
235 
236       if (files != NULL) {
237 	LIBMTP_file_t *file, *tmp;
238 	file = files;
239 	while (file != NULL) {
240 	  if (!strcmp(file->filename, "WMPInfo.xml") ||
241 	      !strcmp(file->filename, "WMPinfo.xml") ||
242 	      !strcmp(file->filename, "default-capabilities.xml")) {
243 	    if (file->item_id != 0) {
244 	      /* Dump this file */
245 	      FILE *xmltmp = tmpfile();
246 	      int tmpfiledescriptor = fileno(xmltmp);
247 
248 	      if (tmpfiledescriptor != -1) {
249 		int ret = LIBMTP_Get_Track_To_File_Descriptor(device,
250 							      file->item_id,
251 							      tmpfiledescriptor,
252 							      NULL,
253 							      NULL);
254 		if (ret == 0) {
255 		  uint8_t *buf = NULL;
256 		  uint32_t readbytes;
257 
258 		  buf = malloc(XML_BUFSIZE);
259 		  if (buf == NULL) {
260 		    printf("Could not allocate %08x bytes...\n", XML_BUFSIZE);
261 		    LIBMTP_Dump_Errorstack(device);
262 		    LIBMTP_Clear_Errorstack(device);
263 		    free(rawdevices);
264 		    return 1;
265 		  }
266 
267 		  lseek(tmpfiledescriptor, 0, SEEK_SET);
268 		  readbytes = read(tmpfiledescriptor, (void*) buf, XML_BUFSIZE);
269 
270 		  if (readbytes >= 2 && readbytes < XML_BUFSIZE) {
271 		    fprintf(stdout, "\n%s file contents:\n", file->filename);
272 		    dump_xml_fragment(buf, readbytes);
273 		  } else {
274 		    perror("Unable to read file");
275 		    LIBMTP_Dump_Errorstack(device);
276 		    LIBMTP_Clear_Errorstack(device);
277 		  }
278 		  free(buf);
279 		} else {
280 		  LIBMTP_Dump_Errorstack(device);
281 		  LIBMTP_Clear_Errorstack(device);
282 		}
283 		fclose(xmltmp);
284 	      }
285 	    }
286 	  }
287 	  tmp = file;
288 	  file = file->next;
289 	  LIBMTP_destroy_file_t(tmp);
290 	}
291       }
292     }
293     LIBMTP_Release_Device(device);
294   } /* End For Loop */
295 
296   free(rawdevices);
297 
298   printf("OK.\n");
299 
300   return 0;
301 }
302