3 * Example program to detect a device and list capabilities.
5 * Copyright (C) 2005-2015 Linus Walleij <triad@df.lth.se>
6 * Copyright (C) 2007 Ted Bullock <tbullock@canada.com>
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.
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.
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.
31 #define XML_BUFSIZE 0x10000
33 static void dump_xml_fragment(uint8_t *buf, uint32_t len)
35 static int endianness = 0; // 0 = LE, 1 = BE
39 if (buf[bp+0] == 0xFF && buf[bp+1] == 0xFE) {
41 } else if (buf[bp+0] == 0xFE && buf[bp+1] == 0xff) {
46 if (endianness == 0) {
47 tmp = buf[bp+1] << 8 | buf[bp+0];
49 tmp = buf[bp+0] << 8 | buf[bp+1];
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);
60 int main (int argc, char **argv)
62 LIBMTP_raw_device_t * rawdevices;
64 LIBMTP_error_number_t err;
71 while ((opt = getopt(argc, argv, "d")) != -1 ) {
74 LIBMTP_Set_Debug(LIBMTP_DEBUG_PTP | LIBMTP_DEBUG_DATA);
84 fprintf(stdout, "libmtp version: " LIBMTP_VERSION_STRING "\n\n");
86 fprintf(stdout, "Listing raw device(s)\n");
87 err = LIBMTP_Detect_Raw_Devices(&rawdevices, &numrawdevices);
89 case LIBMTP_ERROR_NO_DEVICE_ATTACHED:
90 fprintf(stdout, " No raw devices found.\n");
92 case LIBMTP_ERROR_CONNECTING:
93 fprintf(stderr, "Detect: There has been an error connecting. Exiting\n");
95 case LIBMTP_ERROR_MEMORY_ALLOCATION:
96 fprintf(stderr, "Detect: Encountered a Memory Allocation Error. Exiting\n");
98 case LIBMTP_ERROR_NONE:
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);
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);
123 case LIBMTP_ERROR_GENERAL:
125 fprintf(stderr, "Unknown connection error.\n");
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;
139 uint16_t filetypes_len;
140 uint8_t maxbattlevel;
141 uint8_t currbattlevel;
144 device = LIBMTP_Open_Raw_Device_Uncached(&rawdevices[i]);
145 if (device == NULL) {
146 fprintf(stderr, "Unable to open raw device %d\n", i);
150 LIBMTP_Dump_Errorstack(device);
151 LIBMTP_Clear_Errorstack(device);
152 LIBMTP_Dump_Device_Info(device);
154 printf("MTP-specific device properties:\n");
156 friendlyname = LIBMTP_Get_Friendlyname(device);
157 if (friendlyname == NULL) {
158 fprintf(stdout, " Friendly name: (NULL)\n");
160 fprintf(stdout, " Friendly name: %s\n", friendlyname);
163 syncpartner = LIBMTP_Get_Syncpartner(device);
164 if (syncpartner == NULL) {
165 fprintf(stdout, " Synchronization partner: (NULL)\n");
167 fprintf(stdout, " Synchronization partner: %s\n", syncpartner);
172 ret = LIBMTP_Get_Batterylevel(device, &maxbattlevel, &currbattlevel);
174 fprintf(stdout, " Battery level %d of %d (%d%%)\n",currbattlevel, maxbattlevel,
175 (int) ((float) currbattlevel/ (float) maxbattlevel * 100.0));
177 // Silently ignore. Some devices does not support getting the
179 LIBMTP_Clear_Errorstack(device);
182 ret = LIBMTP_Get_Supported_Filetypes(device, &filetypes, &filetypes_len);
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]));
191 LIBMTP_Dump_Errorstack(device);
192 LIBMTP_Clear_Errorstack(device);
195 // Secure time XML fragment
196 ret = LIBMTP_Get_Secure_Time(device, §ime);
197 if (ret == 0 && sectime != NULL) {
198 fprintf(stdout, "\nSecure Time:\n%s\n", sectime);
201 // Silently ignore - there may be devices not supporting secure time.
202 LIBMTP_Clear_Errorstack(device);
205 // Device certificate XML fragment
206 if (rawdevices[i].device_entry.vendor_id == 0x041e) {
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
214 ret = LIBMTP_Get_Device_Certificate(device, &devcert);
215 if (ret == 0 && devcert != NULL) {
216 fprintf(stdout, "\nDevice Certificate:\n%s\n", devcert);
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);
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;
231 /* Get file listing for the root directory, no other dirs */
232 files = LIBMTP_Get_Files_And_Folders(device,
237 LIBMTP_file_t *file, *tmp;
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) {
245 FILE *xmltmp = tmpfile();
246 int tmpfiledescriptor = fileno(xmltmp);
248 if (tmpfiledescriptor != -1) {
249 int ret = LIBMTP_Get_Track_To_File_Descriptor(device,
258 buf = malloc(XML_BUFSIZE);
260 printf("Could not allocate %08x bytes...\n", XML_BUFSIZE);
261 LIBMTP_Dump_Errorstack(device);
262 LIBMTP_Clear_Errorstack(device);
267 lseek(tmpfiledescriptor, 0, SEEK_SET);
268 readbytes = read(tmpfiledescriptor, (void*) buf, XML_BUFSIZE);
270 if (readbytes >= 2 && readbytes < XML_BUFSIZE) {
271 fprintf(stdout, "\n%s file contents:\n", file->filename);
272 dump_xml_fragment(buf, readbytes);
274 perror("Unable to read file");
275 LIBMTP_Dump_Errorstack(device);
276 LIBMTP_Clear_Errorstack(device);
280 LIBMTP_Dump_Errorstack(device);
281 LIBMTP_Clear_Errorstack(device);
289 LIBMTP_destroy_file_t(tmp);
293 LIBMTP_Release_Device(device);