blob: 34ca996149517906ad412aac7a319ee460c572d7 [file] [log] [blame]
Linus Walleij2c1bbd62009-11-07 15:15:03 +00001/**
tedbullock907432d2007-02-19 09:02:40 +00002 * \file detect.c
Linus Walleij543badf2007-02-05 19:07:38 +00003 * Example program to detect a device and list capabilities.
4 *
Linus Walleije012b8c2015-08-22 21:50:54 +02005 * Copyright (C) 2005-2015 Linus Walleij <triad@df.lth.se>
tedbullock0f033cb2007-02-14 20:56:54 +00006 * Copyright (C) 2007 Ted Bullock <tbullock@canada.com>
Linus Walleij543badf2007-02-05 19:07:38 +00007 *
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 */
Linus Walleijeb8c6fe2006-02-03 09:46:22 +000023#include "common.h"
Linus Walleij59765e82008-08-15 07:17:12 +000024#include "util.h"
Linus Walleijf6bc1782006-03-24 15:12:47 +000025#include <unistd.h>
Linus Walleijfa1374c2006-02-27 07:41:46 +000026#include <stdlib.h>
Linus Walleijf6bc1782006-03-24 15:12:47 +000027#include <stdio.h>
28#include <string.h>
tedbullock45810fe2007-02-26 04:00:43 +000029#include <errno.h>
Linus Walleijeb8c6fe2006-02-03 09:46:22 +000030
Linus Walleij91405592006-05-05 14:22:51 +000031#define XML_BUFSIZE 0x10000
32
33static 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;
Linus Walleij2c1bbd62009-11-07 15:15:03 +000037
Linus Walleij91405592006-05-05 14:22:51 +000038 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;
Linus Walleij2c1bbd62009-11-07 15:15:03 +000045
Linus Walleij91405592006-05-05 14:22:51 +000046 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
Linus Walleijeb8c6fe2006-02-03 09:46:22 +000060int main (int argc, char **argv)
61{
Linus Walleij63fd1e62008-04-23 23:49:43 +000062 LIBMTP_raw_device_t * rawdevices;
Linus Walleij63fd1e62008-04-23 23:49:43 +000063 int numrawdevices;
Linus Walleija700d222008-05-28 23:06:14 +000064 LIBMTP_error_number_t err;
Linus Walleij9b146182008-05-29 22:42:54 +000065 int i;
Linus Walleijeb8c6fe2006-02-03 09:46:22 +000066
nicklas79daadbf22009-09-28 18:19:34 +000067 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':
Catalin Patulea406d5f92012-07-20 19:00:24 -040074 LIBMTP_Set_Debug(LIBMTP_DEBUG_PTP | LIBMTP_DEBUG_DATA);
nicklas79daadbf22009-09-28 18:19:34 +000075 break;
76 }
77 }
78
79 argc -= optind;
80 argv += optind;
81
Linus Walleijeb8c6fe2006-02-03 09:46:22 +000082 LIBMTP_Init();
Linus Walleij1fd2d272006-05-08 09:22:01 +000083
tedbullockcd9f4992007-03-29 06:00:40 +000084 fprintf(stdout, "libmtp version: " LIBMTP_VERSION_STRING "\n\n");
Linus Walleij63fd1e62008-04-23 23:49:43 +000085
86 fprintf(stdout, "Listing raw device(s)\n");
Linus Walleija700d222008-05-28 23:06:14 +000087 err = LIBMTP_Detect_Raw_Devices(&rawdevices, &numrawdevices);
88 switch(err) {
89 case LIBMTP_ERROR_NO_DEVICE_ATTACHED:
Linus Walleijef2fb362008-05-28 20:59:09 +000090 fprintf(stdout, " No raw devices found.\n");
Linus Walleij9b146182008-05-29 22:42:54 +000091 return 0;
Linus Walleija700d222008-05-28 23:06:14 +000092 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;
Linus Walleij2c1bbd62009-11-07 15:15:03 +0000101
Linus Walleija700d222008-05-28 23:06:14 +0000102 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) {
Linus Walleij2c1bbd62009-11-07 15:15:03 +0000106 fprintf(stdout, " %s: %s (%04x:%04x) @ bus %d, dev %d\n",
Linus Walleija700d222008-05-28 23:06:14 +0000107 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 {
Linus Walleij2c1bbd62009-11-07 15:15:03 +0000114 fprintf(stdout, " %04x:%04x @ bus %d, dev %d\n",
Linus Walleija700d222008-05-28 23:06:14 +0000115 rawdevices[i].device_entry.vendor_id,
116 rawdevices[i].device_entry.product_id,
117 rawdevices[i].bus_location,
118 rawdevices[i].devnum);
119 }
Linus Walleij63fd1e62008-04-23 23:49:43 +0000120 }
121 }
Linus Walleija700d222008-05-28 23:06:14 +0000122 break;
123 case LIBMTP_ERROR_GENERAL:
124 default:
125 fprintf(stderr, "Unknown connection error.\n");
Linus Walleij9b146182008-05-29 22:42:54 +0000126 return 1;
Linus Walleij63fd1e62008-04-23 23:49:43 +0000127 }
128
Linus Walleij9b146182008-05-29 22:42:54 +0000129 /* Iterate over connected MTP devices */
tedbullock0f033cb2007-02-14 20:56:54 +0000130 fprintf(stdout, "Attempting to connect device(s)\n");
Linus Walleij9b146182008-05-29 22:42:54 +0000131 for (i = 0; i < numrawdevices; i++) {
132 LIBMTP_mtpdevice_t *device;
Linus Walleije012b8c2015-08-22 21:50:54 +0200133 LIBMTP_devicestorage_t *storage;
Linus Walleij9b146182008-05-29 22:42:54 +0000134 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;
tedbullock0f033cb2007-02-14 20:56:54 +0000143
Linus Walleije012b8c2015-08-22 21:50:54 +0200144 device = LIBMTP_Open_Raw_Device_Uncached(&rawdevices[i]);
Linus Walleij9b146182008-05-29 22:42:54 +0000145 if (device == NULL) {
146 fprintf(stderr, "Unable to open raw device %d\n", i);
147 continue;
148 }
tedbullock0f033cb2007-02-14 20:56:54 +0000149
Linus Walleij9b146182008-05-29 22:42:54 +0000150 LIBMTP_Dump_Errorstack(device);
151 LIBMTP_Clear_Errorstack(device);
152 LIBMTP_Dump_Device_Info(device);
Linus Walleij2c1bbd62009-11-07 15:15:03 +0000153
Linus Walleij9b146182008-05-29 22:42:54 +0000154 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);
Linus Walleij8ab54262006-06-21 07:12:28 +0000162 }
Linus Walleij9b146182008-05-29 22:42:54 +0000163 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);
Linus Walleijf6bc1782006-03-24 15:12:47 +0000169 }
Linus Walleij2c1bbd62009-11-07 15:15:03 +0000170
Linus Walleij9b146182008-05-29 22:42:54 +0000171 // Some battery info
172 ret = LIBMTP_Get_Batterylevel(device, &maxbattlevel, &currbattlevel);
173 if (ret == 0) {
Linus Walleij2c1bbd62009-11-07 15:15:03 +0000174 fprintf(stdout, " Battery level %d of %d (%d%%)\n",currbattlevel, maxbattlevel,
Linus Walleij9b146182008-05-29 22:42:54 +0000175 (int) ((float) currbattlevel/ (float) maxbattlevel * 100.0));
176 } else {
Linus Walleij2c1bbd62009-11-07 15:15:03 +0000177 // Silently ignore. Some devices does not support getting the
Linus Walleij9b146182008-05-29 22:42:54 +0000178 // battery level.
179 LIBMTP_Clear_Errorstack(device);
Linus Walleijf6bc1782006-03-24 15:12:47 +0000180 }
Linus Walleij2c1bbd62009-11-07 15:15:03 +0000181
Linus Walleij9b146182008-05-29 22:42:54 +0000182 ret = LIBMTP_Get_Supported_Filetypes(device, &filetypes, &filetypes_len);
183 if (ret == 0) {
184 uint16_t i;
Linus Walleij2c1bbd62009-11-07 15:15:03 +0000185
Linus Walleij9b146182008-05-29 22:42:54 +0000186 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 }
Linus Walleij2c1bbd62009-11-07 15:15:03 +0000194
Linus Walleij9b146182008-05-29 22:42:54 +0000195 // 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 }
Linus Walleij2c1bbd62009-11-07 15:15:03 +0000204
Linus Walleij9b146182008-05-29 22:42:54 +0000205 // Device certificate XML fragment
Linus Walleije012b8c2015-08-22 21:50:54 +0200206 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 }
Linus Walleij9b146182008-05-29 22:42:54 +0000224 }
Linus Walleijf6bc1782006-03-24 15:12:47 +0000225
Linus Walleije012b8c2015-08-22 21:50:54 +0200226 /* 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,
Stanisław Pitucha4c162fa2017-02-04 09:35:54 +1100234 LIBMTP_FILES_AND_FOLDERS_ROOT);
Linus Walleije012b8c2015-08-22 21:50:54 +0200235
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")) {
Linus Walleijb9255212008-12-13 22:26:26 +0000243 if (file->item_id != 0) {
244 /* Dump this file */
245 FILE *xmltmp = tmpfile();
246 int tmpfiledescriptor = fileno(xmltmp);
Linus Walleij2c1bbd62009-11-07 15:15:03 +0000247
Linus Walleijb9255212008-12-13 22:26:26 +0000248 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;
Linus Walleij2c1bbd62009-11-07 15:15:03 +0000257
Linus Walleijb9255212008-12-13 22:26:26 +0000258 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 }
Linus Walleij2c1bbd62009-11-07 15:15:03 +0000266
Linus Walleijb9255212008-12-13 22:26:26 +0000267 lseek(tmpfiledescriptor, 0, SEEK_SET);
268 readbytes = read(tmpfiledescriptor, (void*) buf, XML_BUFSIZE);
Linus Walleij2c1bbd62009-11-07 15:15:03 +0000269
Linus Walleijb9255212008-12-13 22:26:26 +0000270 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 }
Linus Walleije012b8c2015-08-22 21:50:54 +0200286 }
287 tmp = file;
288 file = file->next;
289 LIBMTP_destroy_file_t(tmp);
Linus Walleijb9255212008-12-13 22:26:26 +0000290 }
Linus Walleij9b146182008-05-29 22:42:54 +0000291 }
292 }
Linus Walleij9b146182008-05-29 22:42:54 +0000293 LIBMTP_Release_Device(device);
tedbullock0f033cb2007-02-14 20:56:54 +0000294 } /* End For Loop */
tedbullocke2f94fd2007-02-22 20:34:10 +0000295
Linus Walleij9b146182008-05-29 22:42:54 +0000296 free(rawdevices);
297
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000298 printf("OK.\n");
Linus Walleij2c1bbd62009-11-07 15:15:03 +0000299
300 return 0;
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000301}