Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 1 | /** |
tedbullock | 907432d | 2007-02-19 09:02:40 +0000 | [diff] [blame] | 2 | * \file detect.c |
Linus Walleij | 543badf | 2007-02-05 19:07:38 +0000 | [diff] [blame] | 3 | * Example program to detect a device and list capabilities. |
| 4 | * |
Linus Walleij | e012b8c | 2015-08-22 21:50:54 +0200 | [diff] [blame] | 5 | * Copyright (C) 2005-2015 Linus Walleij <triad@df.lth.se> |
tedbullock | 0f033cb | 2007-02-14 20:56:54 +0000 | [diff] [blame] | 6 | * Copyright (C) 2007 Ted Bullock <tbullock@canada.com> |
Linus Walleij | 543badf | 2007-02-05 19:07:38 +0000 | [diff] [blame] | 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 | */ |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 23 | #include "common.h" |
Linus Walleij | 59765e8 | 2008-08-15 07:17:12 +0000 | [diff] [blame] | 24 | #include "util.h" |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 25 | #include <unistd.h> |
Linus Walleij | fa1374c | 2006-02-27 07:41:46 +0000 | [diff] [blame] | 26 | #include <stdlib.h> |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 27 | #include <stdio.h> |
| 28 | #include <string.h> |
tedbullock | 45810fe | 2007-02-26 04:00:43 +0000 | [diff] [blame] | 29 | #include <errno.h> |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 30 | |
Linus Walleij | 9140559 | 2006-05-05 14:22:51 +0000 | [diff] [blame] | 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; |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 37 | |
Linus Walleij | 9140559 | 2006-05-05 14:22:51 +0000 | [diff] [blame] | 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; |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 45 | |
Linus Walleij | 9140559 | 2006-05-05 14:22:51 +0000 | [diff] [blame] | 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 | |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 60 | int main (int argc, char **argv) |
| 61 | { |
Linus Walleij | 63fd1e6 | 2008-04-23 23:49:43 +0000 | [diff] [blame] | 62 | LIBMTP_raw_device_t * rawdevices; |
Linus Walleij | 63fd1e6 | 2008-04-23 23:49:43 +0000 | [diff] [blame] | 63 | int numrawdevices; |
Linus Walleij | a700d22 | 2008-05-28 23:06:14 +0000 | [diff] [blame] | 64 | LIBMTP_error_number_t err; |
Linus Walleij | 9b14618 | 2008-05-29 22:42:54 +0000 | [diff] [blame] | 65 | int i; |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 66 | |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 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': |
Catalin Patulea | 406d5f9 | 2012-07-20 19:00:24 -0400 | [diff] [blame] | 74 | LIBMTP_Set_Debug(LIBMTP_DEBUG_PTP | LIBMTP_DEBUG_DATA); |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 75 | break; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | argc -= optind; |
| 80 | argv += optind; |
| 81 | |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 82 | LIBMTP_Init(); |
Linus Walleij | 1fd2d27 | 2006-05-08 09:22:01 +0000 | [diff] [blame] | 83 | |
tedbullock | cd9f499 | 2007-03-29 06:00:40 +0000 | [diff] [blame] | 84 | fprintf(stdout, "libmtp version: " LIBMTP_VERSION_STRING "\n\n"); |
Linus Walleij | 63fd1e6 | 2008-04-23 23:49:43 +0000 | [diff] [blame] | 85 | |
| 86 | fprintf(stdout, "Listing raw device(s)\n"); |
Linus Walleij | a700d22 | 2008-05-28 23:06:14 +0000 | [diff] [blame] | 87 | err = LIBMTP_Detect_Raw_Devices(&rawdevices, &numrawdevices); |
| 88 | switch(err) { |
| 89 | case LIBMTP_ERROR_NO_DEVICE_ATTACHED: |
Linus Walleij | ef2fb36 | 2008-05-28 20:59:09 +0000 | [diff] [blame] | 90 | fprintf(stdout, " No raw devices found.\n"); |
Linus Walleij | 9b14618 | 2008-05-29 22:42:54 +0000 | [diff] [blame] | 91 | return 0; |
Linus Walleij | a700d22 | 2008-05-28 23:06:14 +0000 | [diff] [blame] | 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; |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 101 | |
Linus Walleij | a700d22 | 2008-05-28 23:06:14 +0000 | [diff] [blame] | 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) { |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 106 | fprintf(stdout, " %s: %s (%04x:%04x) @ bus %d, dev %d\n", |
Linus Walleij | a700d22 | 2008-05-28 23:06:14 +0000 | [diff] [blame] | 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 { |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 114 | fprintf(stdout, " %04x:%04x @ bus %d, dev %d\n", |
Linus Walleij | a700d22 | 2008-05-28 23:06:14 +0000 | [diff] [blame] | 115 | rawdevices[i].device_entry.vendor_id, |
| 116 | rawdevices[i].device_entry.product_id, |
| 117 | rawdevices[i].bus_location, |
| 118 | rawdevices[i].devnum); |
| 119 | } |
Linus Walleij | 63fd1e6 | 2008-04-23 23:49:43 +0000 | [diff] [blame] | 120 | } |
| 121 | } |
Linus Walleij | a700d22 | 2008-05-28 23:06:14 +0000 | [diff] [blame] | 122 | break; |
| 123 | case LIBMTP_ERROR_GENERAL: |
| 124 | default: |
| 125 | fprintf(stderr, "Unknown connection error.\n"); |
Linus Walleij | 9b14618 | 2008-05-29 22:42:54 +0000 | [diff] [blame] | 126 | return 1; |
Linus Walleij | 63fd1e6 | 2008-04-23 23:49:43 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Linus Walleij | 9b14618 | 2008-05-29 22:42:54 +0000 | [diff] [blame] | 129 | /* Iterate over connected MTP devices */ |
tedbullock | 0f033cb | 2007-02-14 20:56:54 +0000 | [diff] [blame] | 130 | fprintf(stdout, "Attempting to connect device(s)\n"); |
Linus Walleij | 9b14618 | 2008-05-29 22:42:54 +0000 | [diff] [blame] | 131 | for (i = 0; i < numrawdevices; i++) { |
| 132 | LIBMTP_mtpdevice_t *device; |
Linus Walleij | e012b8c | 2015-08-22 21:50:54 +0200 | [diff] [blame] | 133 | LIBMTP_devicestorage_t *storage; |
Linus Walleij | 9b14618 | 2008-05-29 22:42:54 +0000 | [diff] [blame] | 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; |
tedbullock | 0f033cb | 2007-02-14 20:56:54 +0000 | [diff] [blame] | 143 | |
Linus Walleij | e012b8c | 2015-08-22 21:50:54 +0200 | [diff] [blame] | 144 | device = LIBMTP_Open_Raw_Device_Uncached(&rawdevices[i]); |
Linus Walleij | 9b14618 | 2008-05-29 22:42:54 +0000 | [diff] [blame] | 145 | if (device == NULL) { |
| 146 | fprintf(stderr, "Unable to open raw device %d\n", i); |
| 147 | continue; |
| 148 | } |
tedbullock | 0f033cb | 2007-02-14 20:56:54 +0000 | [diff] [blame] | 149 | |
Linus Walleij | 9b14618 | 2008-05-29 22:42:54 +0000 | [diff] [blame] | 150 | LIBMTP_Dump_Errorstack(device); |
| 151 | LIBMTP_Clear_Errorstack(device); |
| 152 | LIBMTP_Dump_Device_Info(device); |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 153 | |
Linus Walleij | 9b14618 | 2008-05-29 22:42:54 +0000 | [diff] [blame] | 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); |
Linus Walleij | 8ab5426 | 2006-06-21 07:12:28 +0000 | [diff] [blame] | 162 | } |
Linus Walleij | 9b14618 | 2008-05-29 22:42:54 +0000 | [diff] [blame] | 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); |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 169 | } |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 170 | |
Linus Walleij | 9b14618 | 2008-05-29 22:42:54 +0000 | [diff] [blame] | 171 | // Some battery info |
| 172 | ret = LIBMTP_Get_Batterylevel(device, &maxbattlevel, &currbattlevel); |
| 173 | if (ret == 0) { |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 174 | fprintf(stdout, " Battery level %d of %d (%d%%)\n",currbattlevel, maxbattlevel, |
Linus Walleij | 9b14618 | 2008-05-29 22:42:54 +0000 | [diff] [blame] | 175 | (int) ((float) currbattlevel/ (float) maxbattlevel * 100.0)); |
| 176 | } else { |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 177 | // Silently ignore. Some devices does not support getting the |
Linus Walleij | 9b14618 | 2008-05-29 22:42:54 +0000 | [diff] [blame] | 178 | // battery level. |
| 179 | LIBMTP_Clear_Errorstack(device); |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 180 | } |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 181 | |
Linus Walleij | 9b14618 | 2008-05-29 22:42:54 +0000 | [diff] [blame] | 182 | ret = LIBMTP_Get_Supported_Filetypes(device, &filetypes, &filetypes_len); |
| 183 | if (ret == 0) { |
| 184 | uint16_t i; |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 185 | |
Linus Walleij | 9b14618 | 2008-05-29 22:42:54 +0000 | [diff] [blame] | 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 | } |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 194 | |
Linus Walleij | 9b14618 | 2008-05-29 22:42:54 +0000 | [diff] [blame] | 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); |
| 199 | free(sectime); |
| 200 | } else { |
| 201 | // Silently ignore - there may be devices not supporting secure time. |
| 202 | LIBMTP_Clear_Errorstack(device); |
| 203 | } |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 204 | |
Linus Walleij | 9b14618 | 2008-05-29 22:42:54 +0000 | [diff] [blame] | 205 | // Device certificate XML fragment |
Linus Walleij | e012b8c | 2015-08-22 21:50:54 +0200 | [diff] [blame] | 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 | } |
Linus Walleij | 9b14618 | 2008-05-29 22:42:54 +0000 | [diff] [blame] | 224 | } |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 225 | |
Linus Walleij | e012b8c | 2015-08-22 21:50:54 +0200 | [diff] [blame] | 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, |
Stanisław Pitucha | 4c162fa | 2017-02-04 09:35:54 +1100 | [diff] [blame] | 234 | LIBMTP_FILES_AND_FOLDERS_ROOT); |
Linus Walleij | e012b8c | 2015-08-22 21:50:54 +0200 | [diff] [blame] | 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")) { |
Linus Walleij | b925521 | 2008-12-13 22:26:26 +0000 | [diff] [blame] | 243 | if (file->item_id != 0) { |
| 244 | /* Dump this file */ |
| 245 | FILE *xmltmp = tmpfile(); |
| 246 | int tmpfiledescriptor = fileno(xmltmp); |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 247 | |
Linus Walleij | b925521 | 2008-12-13 22:26:26 +0000 | [diff] [blame] | 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; |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 257 | |
Linus Walleij | b925521 | 2008-12-13 22:26:26 +0000 | [diff] [blame] | 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 | } |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 266 | |
Linus Walleij | b925521 | 2008-12-13 22:26:26 +0000 | [diff] [blame] | 267 | lseek(tmpfiledescriptor, 0, SEEK_SET); |
| 268 | readbytes = read(tmpfiledescriptor, (void*) buf, XML_BUFSIZE); |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 269 | |
Linus Walleij | b925521 | 2008-12-13 22:26:26 +0000 | [diff] [blame] | 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 | } |
Linus Walleij | e012b8c | 2015-08-22 21:50:54 +0200 | [diff] [blame] | 286 | } |
| 287 | tmp = file; |
| 288 | file = file->next; |
| 289 | LIBMTP_destroy_file_t(tmp); |
Linus Walleij | b925521 | 2008-12-13 22:26:26 +0000 | [diff] [blame] | 290 | } |
Linus Walleij | 9b14618 | 2008-05-29 22:42:54 +0000 | [diff] [blame] | 291 | } |
| 292 | } |
Linus Walleij | 9b14618 | 2008-05-29 22:42:54 +0000 | [diff] [blame] | 293 | LIBMTP_Release_Device(device); |
tedbullock | 0f033cb | 2007-02-14 20:56:54 +0000 | [diff] [blame] | 294 | } /* End For Loop */ |
tedbullock | e2f94fd | 2007-02-22 20:34:10 +0000 | [diff] [blame] | 295 | |
Linus Walleij | 9b14618 | 2008-05-29 22:42:54 +0000 | [diff] [blame] | 296 | free(rawdevices); |
| 297 | |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 298 | printf("OK.\n"); |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 299 | |
| 300 | return 0; |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 301 | } |