Linus Walleij | 10c5842 | 2006-06-02 08:51:53 +0000 | [diff] [blame] | 1 | /** |
| 2 | * \file libmtp.c |
Linus Walleij | 2f45d22 | 2007-02-02 22:47:39 +0000 | [diff] [blame] | 3 | * |
Linus Walleij | a8b8889 | 2011-03-03 19:58:26 +0100 | [diff] [blame] | 4 | * Copyright (C) 2005-2011 Linus Walleij <triad@df.lth.se> |
Richard Low | 36e447c | 2008-01-20 15:24:55 +0000 | [diff] [blame] | 5 | * Copyright (C) 2005-2008 Richard A. Low <richard@wentnet.com> |
Linus Walleij | 9521e2b | 2007-07-10 21:50:42 +0000 | [diff] [blame] | 6 | * Copyright (C) 2007 Ted Bullock <tbullock@canada.com> |
| 7 | * Copyright (C) 2007 Tero Saarni <tero.saarni@gmail.com> |
Linus Walleij | de8193f | 2008-01-27 14:55:31 +0000 | [diff] [blame] | 8 | * Copyright (C) 2008 Florent Mertens <flomertens@gmail.com> |
Linus Walleij | 2f45d22 | 2007-02-02 22:47:39 +0000 | [diff] [blame] | 9 | * |
| 10 | * This library is free software; you can redistribute it and/or |
| 11 | * modify it under the terms of the GNU Lesser General Public |
| 12 | * License as published by the Free Software Foundation; either |
| 13 | * version 2 of the License, or (at your option) any later version. |
| 14 | * |
| 15 | * This library is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 18 | * Lesser General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU Lesser General Public |
| 21 | * License along with this library; if not, write to the |
| 22 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 23 | * Boston, MA 02111-1307, USA. |
| 24 | * |
Linus Walleij | 10c5842 | 2006-06-02 08:51:53 +0000 | [diff] [blame] | 25 | * This file provides an interface "glue" to the underlying |
| 26 | * PTP implementation from libgphoto2. It uses some local |
| 27 | * code to convert from/to UTF-8 (stored in unicode.c/.h) |
| 28 | * and some small utility functions, mainly for debugging |
| 29 | * (stored in util.c/.h). |
| 30 | * |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 31 | * The three PTP files (ptp.c, ptp.h and ptp-pack.c) are |
Linus Walleij | 10c5842 | 2006-06-02 08:51:53 +0000 | [diff] [blame] | 32 | * plain copied from the libhphoto2 codebase. |
| 33 | * |
| 34 | * The files libusb-glue.c/.h are just what they say: an |
| 35 | * interface to libusb for the actual, physical USB traffic. |
| 36 | */ |
Linus Walleij | c6d7c98 | 2009-05-10 13:24:36 +0000 | [diff] [blame] | 37 | #include "config.h" |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 38 | #include "libmtp.h" |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 39 | #include "unicode.h" |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 40 | #include "ptp.h" |
Linus Walleij | 7cf0264 | 2011-11-16 23:44:58 +0100 | [diff] [blame] | 41 | #include "libusb-glue.h" |
Linus Walleij | 3e667ae | 2007-10-29 23:29:39 +0000 | [diff] [blame] | 42 | #include "device-flags.h" |
Linus Walleij | f3c4405 | 2008-08-16 21:14:56 +0000 | [diff] [blame] | 43 | #include "playlist-spl.h" |
Linus Walleij | 8f6e0d9 | 2009-10-10 21:39:44 +0000 | [diff] [blame] | 44 | #include "util.h" |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 45 | |
Sajid Anwar | 8dca41d | 2012-08-18 20:36:13 +0200 | [diff] [blame] | 46 | #include "mtpz.h" |
| 47 | |
Marcin Niestroj | 718b290 | 2017-06-10 16:36:18 +0200 | [diff] [blame] | 48 | #include <stdarg.h> |
Linus Walleij | 2eaaff0 | 2009-01-15 21:30:36 +0000 | [diff] [blame] | 49 | #include <stdlib.h> |
Catalin Patulea | da25de0 | 2012-07-19 00:57:42 -0400 | [diff] [blame] | 50 | #include <limits.h> |
Linus Walleij | 2eaaff0 | 2009-01-15 21:30:36 +0000 | [diff] [blame] | 51 | #include <unistd.h> |
Linus Walleij | 7beba57 | 2006-11-29 08:56:12 +0000 | [diff] [blame] | 52 | #include <string.h> |
| 53 | #include <sys/types.h> |
| 54 | #include <sys/stat.h> |
| 55 | #include <fcntl.h> |
Linus Walleij | 4126b30 | 2007-09-22 22:13:55 +0000 | [diff] [blame] | 56 | #include <time.h> |
Linus Walleij | f3c4405 | 2008-08-16 21:14:56 +0000 | [diff] [blame] | 57 | #include <errno.h> |
Linus Walleij | 7beba57 | 2006-11-29 08:56:12 +0000 | [diff] [blame] | 58 | #ifdef _MSC_VER // For MSVC++ |
| 59 | #define USE_WINDOWS_IO_H |
| 60 | #include <io.h> |
| 61 | #endif |
| 62 | |
Linus Walleij | 7cf0264 | 2011-11-16 23:44:58 +0100 | [diff] [blame] | 63 | |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 64 | /** |
| 65 | * Global debug level |
| 66 | * We use a flag system to enable a part of logs. |
Catalin Patulea | da25de0 | 2012-07-19 00:57:42 -0400 | [diff] [blame] | 67 | * |
| 68 | * The LIBMTP_DEBUG environment variable sets the debug flags for any binary |
| 69 | * that uses libmtp and calls LIBMTP_Init. The value can be given in decimal |
| 70 | * (must not start with "0" or it will be interpreted in octal), or in |
| 71 | * hexadecimal (must start with "0x"). |
| 72 | * |
| 73 | * The value "-1" enables all debug flags. |
| 74 | * |
| 75 | * Some of the utilities in examples/ also take a command-line flag "-d" that |
Catalin Patulea | 406d5f9 | 2012-07-20 19:00:24 -0400 | [diff] [blame] | 76 | * enables LIBMTP_DEBUG_PTP and LIBMTP_DEBUG_DATA (same as setting |
| 77 | * LIBMTP_DEBUG=9). |
Catalin Patulea | da25de0 | 2012-07-19 00:57:42 -0400 | [diff] [blame] | 78 | * |
| 79 | * Flags (combine by adding the hex values): |
Catalin Patulea | 406d5f9 | 2012-07-20 19:00:24 -0400 | [diff] [blame] | 80 | * 0x00 [0000 0000] : LIBMTP_DEBUG_NONE : no debug (default) |
| 81 | * 0x01 [0000 0001] : LIBMTP_DEBUG_PTP : PTP debug |
| 82 | * 0x02 [0000 0010] : LIBMTP_DEBUG_PLST : Playlist debug |
| 83 | * 0x04 [0000 0100] : LIBMTP_DEBUG_USB : USB debug |
| 84 | * 0x08 [0000 1000] : LIBMTP_DEBUG_DATA : USB data debug |
| 85 | * |
| 86 | * (Please keep this list in sync with libmtp.h.) |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 87 | */ |
nicklas79 | 0358408 | 2009-09-28 18:20:16 +0000 | [diff] [blame] | 88 | int LIBMTP_debug = LIBMTP_DEBUG_NONE; |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 89 | |
Linus Walleij | d1e14e0 | 2008-12-14 01:07:30 +0000 | [diff] [blame] | 90 | |
Linus Walleij | c86afbd | 2006-05-04 19:05:24 +0000 | [diff] [blame] | 91 | /* |
| 92 | * This is a mapping between libmtp internal MTP filetypes and |
| 93 | * the libgphoto2/PTP equivalent defines. We need this because |
| 94 | * otherwise the libmtp.h device has to be dependent on ptp.h |
| 95 | * to be installed too, and we don't want that. |
| 96 | */ |
Linus Walleij | cf42f45 | 2006-11-28 08:32:49 +0000 | [diff] [blame] | 97 | //typedef struct filemap_struct filemap_t; |
| 98 | typedef struct filemap_struct { |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 99 | char *description; /**< Text description for the file type */ |
| 100 | LIBMTP_filetype_t id; /**< LIBMTP internal type for the file type */ |
| 101 | uint16_t ptp_id; /**< PTP ID for the filetype */ |
Linus Walleij | cf42f45 | 2006-11-28 08:32:49 +0000 | [diff] [blame] | 102 | struct filemap_struct *next; |
| 103 | } filemap_t; |
Linus Walleij | c86afbd | 2006-05-04 19:05:24 +0000 | [diff] [blame] | 104 | |
Richard Low | 6f07084 | 2009-05-03 10:26:16 +0000 | [diff] [blame] | 105 | /* |
| 106 | * This is a mapping between libmtp internal MTP properties and |
| 107 | * the libgphoto2/PTP equivalent defines. We need this because |
| 108 | * otherwise the libmtp.h device has to be dependent on ptp.h |
| 109 | * to be installed too, and we don't want that. |
| 110 | */ |
| 111 | typedef struct propertymap_struct { |
| 112 | char *description; /**< Text description for the property */ |
| 113 | LIBMTP_property_t id; /**< LIBMTP internal type for the property */ |
| 114 | uint16_t ptp_id; /**< PTP ID for the property */ |
| 115 | struct propertymap_struct *next; |
| 116 | } propertymap_t; |
| 117 | |
Philip Langdale | 0a57651 | 2016-04-09 14:22:25 -0700 | [diff] [blame] | 118 | /* |
| 119 | * This is a simple container for holding our callback and user_data |
| 120 | * for parsing onwards to the usb_event_async function. |
| 121 | */ |
| 122 | typedef struct event_cb_data_struct { |
| 123 | LIBMTP_event_cb_fn cb; |
| 124 | void *user_data; |
| 125 | } event_cb_data_t; |
| 126 | |
Linus Walleij | f67bca9 | 2006-05-29 09:33:39 +0000 | [diff] [blame] | 127 | // Global variables |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 128 | // This holds the global filetype mapping table |
Lei Zhang | 8173213 | 2013-09-04 17:27:35 -0700 | [diff] [blame] | 129 | static filemap_t *g_filemap = NULL; |
Richard Low | 6f07084 | 2009-05-03 10:26:16 +0000 | [diff] [blame] | 130 | // This holds the global property mapping table |
Lei Zhang | 8173213 | 2013-09-04 17:27:35 -0700 | [diff] [blame] | 131 | static propertymap_t *g_propertymap = NULL; |
Linus Walleij | f67bca9 | 2006-05-29 09:33:39 +0000 | [diff] [blame] | 132 | |
Linus Walleij | cf42f45 | 2006-11-28 08:32:49 +0000 | [diff] [blame] | 133 | /* |
| 134 | * Forward declarations of local (static) functions. |
| 135 | */ |
| 136 | static int register_filetype(char const * const description, LIBMTP_filetype_t const id, |
| 137 | uint16_t const ptp_id); |
Linus Walleij | 7beba57 | 2006-11-29 08:56:12 +0000 | [diff] [blame] | 138 | static void init_filemap(); |
Richard Low | 6f07084 | 2009-05-03 10:26:16 +0000 | [diff] [blame] | 139 | static int register_property(char const * const description, LIBMTP_property_t const id, |
| 140 | uint16_t const ptp_id); |
| 141 | static void init_propertymap(); |
Linus Walleij | 2715c44 | 2007-01-20 22:35:29 +0000 | [diff] [blame] | 142 | static void add_error_to_errorstack(LIBMTP_mtpdevice_t *device, |
| 143 | LIBMTP_error_number_t errornumber, |
| 144 | char const * const error_text); |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 145 | static void add_ptp_error_to_errorstack(LIBMTP_mtpdevice_t *device, |
| 146 | uint16_t ptp_error, |
| 147 | char const * const error_text); |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 148 | static void flush_handles(LIBMTP_mtpdevice_t *device); |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 149 | static void get_handles_recursively(LIBMTP_mtpdevice_t *device, |
| 150 | PTPParams *params, |
Linus Walleij | 6bf68b4 | 2007-09-03 22:50:02 +0000 | [diff] [blame] | 151 | uint32_t storageid, |
| 152 | uint32_t parent); |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 153 | static void free_storage_list(LIBMTP_mtpdevice_t *device); |
| 154 | static int sort_storage_by(LIBMTP_mtpdevice_t *device, int const sortby); |
Linus Walleij | 88babc8 | 2014-06-02 21:32:14 +0200 | [diff] [blame] | 155 | static uint32_t get_writeable_storageid(LIBMTP_mtpdevice_t *device, |
| 156 | uint64_t fitsize); |
Linus Walleij | 6bf68b4 | 2007-09-03 22:50:02 +0000 | [diff] [blame] | 157 | static int get_storage_freespace(LIBMTP_mtpdevice_t *device, |
| 158 | LIBMTP_devicestorage_t *storage, |
| 159 | uint64_t *freespace); |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 160 | static int check_if_file_fits(LIBMTP_mtpdevice_t *device, |
Linus Walleij | 6bf68b4 | 2007-09-03 22:50:02 +0000 | [diff] [blame] | 161 | LIBMTP_devicestorage_t *storage, |
| 162 | uint64_t const filesize); |
Linus Walleij | f67bca9 | 2006-05-29 09:33:39 +0000 | [diff] [blame] | 163 | static uint16_t map_libmtp_type_to_ptp_type(LIBMTP_filetype_t intype); |
| 164 | static LIBMTP_filetype_t map_ptp_type_to_libmtp_type(uint16_t intype); |
Richard Low | 6f07084 | 2009-05-03 10:26:16 +0000 | [diff] [blame] | 165 | static uint16_t map_libmtp_property_to_ptp_property(LIBMTP_property_t inproperty); |
| 166 | static LIBMTP_property_t map_ptp_property_to_libmtp_property(uint16_t intype); |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 167 | static int get_device_unicode_property(LIBMTP_mtpdevice_t *device, |
Linus Walleij | cf223e6 | 2006-06-19 09:31:53 +0000 | [diff] [blame] | 168 | char **unicstring, uint16_t property); |
Linus Walleij | 2955956 | 2007-08-22 21:38:04 +0000 | [diff] [blame] | 169 | static uint16_t adjust_u16(uint16_t val, PTPObjectPropDesc *opd); |
| 170 | static uint32_t adjust_u32(uint32_t val, PTPObjectPropDesc *opd); |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 171 | static char *get_iso8601_stamp(void); |
Linus Walleij | 9901e22 | 2006-11-30 12:28:19 +0000 | [diff] [blame] | 172 | static char *get_string_from_object(LIBMTP_mtpdevice_t *device, uint32_t const object_id, |
| 173 | uint16_t const attribute_id); |
Richard Low | c3f5fc3 | 2007-07-29 09:40:50 +0000 | [diff] [blame] | 174 | static uint64_t get_u64_from_object(LIBMTP_mtpdevice_t *device,uint32_t const object_id, |
| 175 | uint16_t const attribute_id, uint64_t const value_default); |
Linus Walleij | 9901e22 | 2006-11-30 12:28:19 +0000 | [diff] [blame] | 176 | static uint32_t get_u32_from_object(LIBMTP_mtpdevice_t *device,uint32_t const object_id, |
| 177 | uint16_t const attribute_id, uint32_t const value_default); |
| 178 | static uint16_t get_u16_from_object(LIBMTP_mtpdevice_t *device, uint32_t const object_id, |
| 179 | uint16_t const attribute_id, uint16_t const value_default); |
| 180 | static uint8_t get_u8_from_object(LIBMTP_mtpdevice_t *device, uint32_t const object_id, |
| 181 | uint16_t const attribute_id, uint8_t const value_default); |
| 182 | static int set_object_string(LIBMTP_mtpdevice_t *device, uint32_t const object_id, |
| 183 | uint16_t const attribute_id, char const * const string); |
| 184 | static int set_object_u32(LIBMTP_mtpdevice_t *device, uint32_t const object_id, |
| 185 | uint16_t const attribute_id, uint32_t const value); |
| 186 | static int set_object_u16(LIBMTP_mtpdevice_t *device, uint32_t const object_id, |
| 187 | uint16_t const attribute_id, uint16_t const value); |
| 188 | static int set_object_u8(LIBMTP_mtpdevice_t *device, uint32_t const object_id, |
| 189 | uint16_t const attribute_id, uint8_t const value); |
Linus Walleij | 8ab5426 | 2006-06-21 07:12:28 +0000 | [diff] [blame] | 190 | static void get_track_metadata(LIBMTP_mtpdevice_t *device, uint16_t objectformat, |
| 191 | LIBMTP_track_t *track); |
Linus Walleij | a89a794 | 2008-12-14 23:19:34 +0000 | [diff] [blame] | 192 | static LIBMTP_folder_t *get_subfolders_for_folder(LIBMTP_folder_t *list, uint32_t parent); |
Linus Walleij | 7beba57 | 2006-11-29 08:56:12 +0000 | [diff] [blame] | 193 | static int create_new_abstract_list(LIBMTP_mtpdevice_t *device, |
| 194 | char const * const name, |
Linus Walleij | adce4a5 | 2007-04-23 07:28:11 +0000 | [diff] [blame] | 195 | char const * const artist, |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 196 | char const * const composer, |
Linus Walleij | adce4a5 | 2007-04-23 07:28:11 +0000 | [diff] [blame] | 197 | char const * const genre, |
Linus Walleij | 7beba57 | 2006-11-29 08:56:12 +0000 | [diff] [blame] | 198 | uint32_t const parenthandle, |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 199 | uint32_t const storageid, |
Linus Walleij | 7beba57 | 2006-11-29 08:56:12 +0000 | [diff] [blame] | 200 | uint16_t const objectformat, |
| 201 | char const * const suffix, |
| 202 | uint32_t * const newid, |
| 203 | uint32_t const * const tracks, |
| 204 | uint32_t const no_tracks); |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 205 | static int update_abstract_list(LIBMTP_mtpdevice_t *device, |
| 206 | char const * const name, |
| 207 | char const * const artist, |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 208 | char const * const composer, |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 209 | char const * const genre, |
| 210 | uint32_t const objecthandle, |
| 211 | uint16_t const objectformat, |
| 212 | uint32_t const * const tracks, |
| 213 | uint32_t const no_tracks); |
Richard Low | d3b1702 | 2009-04-11 12:37:39 +0000 | [diff] [blame] | 214 | static int send_file_object_info(LIBMTP_mtpdevice_t *device, LIBMTP_file_t *filedata); |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 215 | static void add_object_to_cache(LIBMTP_mtpdevice_t *device, uint32_t object_id); |
Linus Walleij | 7752b95 | 2007-10-19 20:11:46 +0000 | [diff] [blame] | 216 | static void update_metadata_cache(LIBMTP_mtpdevice_t *device, uint32_t object_id); |
Linus Walleij | 8d8c435 | 2008-09-23 23:04:16 +0000 | [diff] [blame] | 217 | static int set_object_filename(LIBMTP_mtpdevice_t *device, |
| 218 | uint32_t object_id, |
| 219 | uint16_t ptp_type, |
| 220 | const char **newname); |
Linus Walleij | 094b450 | 2009-09-22 22:28:33 +0000 | [diff] [blame] | 221 | static char *generate_unique_filename(PTPParams* params, char const * const filename); |
| 222 | static int check_filename_exists(PTPParams* params, char const * const filename); |
Philip Langdale | 0a57651 | 2016-04-09 14:22:25 -0700 | [diff] [blame] | 223 | static void LIBMTP_Handle_Event(PTPContainer *ptp_event, |
| 224 | LIBMTP_event_t *event, uint32_t *out1); |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 225 | |
Richard Low | 5b4023c | 2009-04-16 19:14:38 +0000 | [diff] [blame] | 226 | /** |
| 227 | * These are to wrap the get/put handlers to convert from the MTP types to PTP types |
| 228 | * in a reliable way |
| 229 | */ |
| 230 | typedef struct _MTPDataHandler { |
| 231 | MTPDataGetFunc getfunc; |
| 232 | MTPDataPutFunc putfunc; |
Linus Walleij | d866d24 | 2009-08-23 21:50:39 +0000 | [diff] [blame] | 233 | void *priv; |
Richard Low | 5b4023c | 2009-04-16 19:14:38 +0000 | [diff] [blame] | 234 | } MTPDataHandler; |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 235 | |
Linus Walleij | d866d24 | 2009-08-23 21:50:39 +0000 | [diff] [blame] | 236 | static uint16_t get_func_wrapper(PTPParams* params, void* priv, unsigned long wantlen, unsigned char *data, unsigned long *gotlen); |
Marcus Meissner | 3d692dc | 2016-02-21 12:34:06 +0100 | [diff] [blame] | 237 | static uint16_t put_func_wrapper(PTPParams* params, void* priv, unsigned long sendlen, unsigned char *data); |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 238 | |
Linus Walleij | cf42f45 | 2006-11-28 08:32:49 +0000 | [diff] [blame] | 239 | /** |
Linus Walleij | 01fc9c8 | 2009-03-10 23:52:09 +0000 | [diff] [blame] | 240 | * Checks if a filename ends with ".ogg". Used in various |
| 241 | * situations when the device has no idea that it support |
| 242 | * OGG but still does. |
Linus Walleij | d98e897 | 2009-03-08 22:57:17 +0000 | [diff] [blame] | 243 | * |
| 244 | * @param name string to be checked. |
| 245 | * @return 0 if this does not end with ogg, any other |
| 246 | * value means it does. |
| 247 | */ |
| 248 | static int has_ogg_extension(char *name) { |
| 249 | char *ptype; |
| 250 | |
| 251 | if (name == NULL) |
| 252 | return 0; |
| 253 | ptype = strrchr(name,'.'); |
| 254 | if (ptype == NULL) |
| 255 | return 0; |
| 256 | if (!strcasecmp (ptype, ".ogg")) |
| 257 | return 1; |
| 258 | return 0; |
| 259 | } |
| 260 | |
Linus Walleij | 89bb1cd | 2009-07-24 21:03:36 +0000 | [diff] [blame] | 261 | /** |
| 262 | * Checks if a filename ends with ".flac". Used in various |
| 263 | * situations when the device has no idea that it support |
| 264 | * FLAC but still does. |
| 265 | * |
| 266 | * @param name string to be checked. |
| 267 | * @return 0 if this does not end with flac, any other |
| 268 | * value means it does. |
| 269 | */ |
| 270 | static int has_flac_extension(char *name) { |
| 271 | char *ptype; |
| 272 | |
| 273 | if (name == NULL) |
| 274 | return 0; |
| 275 | ptype = strrchr(name,'.'); |
| 276 | if (ptype == NULL) |
| 277 | return 0; |
| 278 | if (!strcasecmp (ptype, ".flac")) |
| 279 | return 1; |
| 280 | return 0; |
| 281 | } |
| 282 | |
| 283 | |
Linus Walleij | d98e897 | 2009-03-08 22:57:17 +0000 | [diff] [blame] | 284 | |
| 285 | /** |
Linus Walleij | cf42f45 | 2006-11-28 08:32:49 +0000 | [diff] [blame] | 286 | * Create a new file mapping entry |
| 287 | * @return a newly allocated filemapping entry. |
| 288 | */ |
| 289 | static filemap_t *new_filemap_entry() |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 290 | { |
Linus Walleij | cf42f45 | 2006-11-28 08:32:49 +0000 | [diff] [blame] | 291 | filemap_t *filemap; |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 292 | |
Linus Walleij | cf42f45 | 2006-11-28 08:32:49 +0000 | [diff] [blame] | 293 | filemap = (filemap_t *)malloc(sizeof(filemap_t)); |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 294 | |
Linus Walleij | f67bca9 | 2006-05-29 09:33:39 +0000 | [diff] [blame] | 295 | if( filemap != NULL ) { |
| 296 | filemap->description = NULL; |
| 297 | filemap->id = LIBMTP_FILETYPE_UNKNOWN; |
| 298 | filemap->ptp_id = PTP_OFC_Undefined; |
Linus Walleij | f67bca9 | 2006-05-29 09:33:39 +0000 | [diff] [blame] | 299 | filemap->next = NULL; |
| 300 | } |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 301 | |
Linus Walleij | f67bca9 | 2006-05-29 09:33:39 +0000 | [diff] [blame] | 302 | return filemap; |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 303 | } |
| 304 | |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 305 | /** |
| 306 | * Register an MTP or PTP filetype for data retrieval |
| 307 | * |
| 308 | * @param description Text description of filetype |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 309 | * @param id libmtp internal filetype id |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 310 | * @param ptp_id PTP filetype id |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 311 | * @return 0 for success any other value means error. |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 312 | */ |
Linus Walleij | cf42f45 | 2006-11-28 08:32:49 +0000 | [diff] [blame] | 313 | static int register_filetype(char const * const description, LIBMTP_filetype_t const id, |
| 314 | uint16_t const ptp_id) |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 315 | { |
Linus Walleij | cf42f45 | 2006-11-28 08:32:49 +0000 | [diff] [blame] | 316 | filemap_t *new = NULL, *current; |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 317 | |
| 318 | // Has this LIBMTP filetype been registered before ? |
Lei Zhang | 8173213 | 2013-09-04 17:27:35 -0700 | [diff] [blame] | 319 | current = g_filemap; |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 320 | while (current != NULL) { |
Linus Walleij | f67bca9 | 2006-05-29 09:33:39 +0000 | [diff] [blame] | 321 | if(current->id == id) { |
| 322 | break; |
| 323 | } |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 324 | current = current->next; |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 325 | } |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 326 | |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 327 | // Create the entry |
| 328 | if(current == NULL) { |
Linus Walleij | f67bca9 | 2006-05-29 09:33:39 +0000 | [diff] [blame] | 329 | new = new_filemap_entry(); |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 330 | if(new == NULL) { |
| 331 | return 1; |
| 332 | } |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 333 | |
Linus Walleij | f67bca9 | 2006-05-29 09:33:39 +0000 | [diff] [blame] | 334 | new->id = id; |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 335 | if(description != NULL) { |
| 336 | new->description = strdup(description); |
| 337 | } |
Linus Walleij | f67bca9 | 2006-05-29 09:33:39 +0000 | [diff] [blame] | 338 | new->ptp_id = ptp_id; |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 339 | |
Linus Walleij | f67bca9 | 2006-05-29 09:33:39 +0000 | [diff] [blame] | 340 | // Add the entry to the list |
Lei Zhang | 8173213 | 2013-09-04 17:27:35 -0700 | [diff] [blame] | 341 | if(g_filemap == NULL) { |
| 342 | g_filemap = new; |
Linus Walleij | f67bca9 | 2006-05-29 09:33:39 +0000 | [diff] [blame] | 343 | } else { |
Lei Zhang | 8173213 | 2013-09-04 17:27:35 -0700 | [diff] [blame] | 344 | current = g_filemap; |
Linus Walleij | f67bca9 | 2006-05-29 09:33:39 +0000 | [diff] [blame] | 345 | while (current->next != NULL ) current=current->next; |
| 346 | current->next = new; |
| 347 | } |
| 348 | // Update the existing entry |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 349 | } else { |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 350 | if (current->description != NULL) { |
| 351 | free(current->description); |
| 352 | } |
Linus Walleij | f67bca9 | 2006-05-29 09:33:39 +0000 | [diff] [blame] | 353 | current->description = NULL; |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 354 | if(description != NULL) { |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 355 | current->description = strdup(description); |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 356 | } |
Linus Walleij | f67bca9 | 2006-05-29 09:33:39 +0000 | [diff] [blame] | 357 | current->ptp_id = ptp_id; |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 358 | } |
| 359 | |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 360 | return 0; |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 361 | } |
| 362 | |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 363 | static void init_filemap() |
| 364 | { |
Linus Walleij | a8b8889 | 2011-03-03 19:58:26 +0100 | [diff] [blame] | 365 | register_filetype("Folder", LIBMTP_FILETYPE_FOLDER, PTP_OFC_Association); |
Linus Walleij | 5fb4713 | 2006-12-30 15:35:48 +0000 | [diff] [blame] | 366 | register_filetype("MediaCard", LIBMTP_FILETYPE_MEDIACARD, PTP_OFC_MTP_MediaCard); |
Linus Walleij | cf42f45 | 2006-11-28 08:32:49 +0000 | [diff] [blame] | 367 | register_filetype("RIFF WAVE file", LIBMTP_FILETYPE_WAV, PTP_OFC_WAV); |
Linus Walleij | 5fb4713 | 2006-12-30 15:35:48 +0000 | [diff] [blame] | 368 | register_filetype("ISO MPEG-1 Audio Layer 3", LIBMTP_FILETYPE_MP3, PTP_OFC_MP3); |
| 369 | register_filetype("ISO MPEG-1 Audio Layer 2", LIBMTP_FILETYPE_MP2, PTP_OFC_MTP_MP2); |
Linus Walleij | cf42f45 | 2006-11-28 08:32:49 +0000 | [diff] [blame] | 370 | register_filetype("Microsoft Windows Media Audio", LIBMTP_FILETYPE_WMA, PTP_OFC_MTP_WMA); |
| 371 | register_filetype("Ogg container format", LIBMTP_FILETYPE_OGG, PTP_OFC_MTP_OGG); |
Linus Walleij | 5fb4713 | 2006-12-30 15:35:48 +0000 | [diff] [blame] | 372 | register_filetype("Free Lossless Audio Codec (FLAC)", LIBMTP_FILETYPE_FLAC, PTP_OFC_MTP_FLAC); |
| 373 | register_filetype("Advanced Audio Coding (AAC)/MPEG-2 Part 7/MPEG-4 Part 3", LIBMTP_FILETYPE_AAC, PTP_OFC_MTP_AAC); |
Richard Low | d3b1702 | 2009-04-11 12:37:39 +0000 | [diff] [blame] | 374 | register_filetype("MPEG-4 Part 14 Container Format (Audio Emphasis)", LIBMTP_FILETYPE_M4A, PTP_OFC_MTP_M4A); |
| 375 | register_filetype("MPEG-4 Part 14 Container Format (Audio+Video Emphasis)", LIBMTP_FILETYPE_MP4, PTP_OFC_MTP_MP4); |
Linus Walleij | cf42f45 | 2006-11-28 08:32:49 +0000 | [diff] [blame] | 376 | register_filetype("Audible.com Audio Codec", LIBMTP_FILETYPE_AUDIBLE, PTP_OFC_MTP_AudibleCodec); |
Linus Walleij | cf42f45 | 2006-11-28 08:32:49 +0000 | [diff] [blame] | 377 | register_filetype("Undefined audio file", LIBMTP_FILETYPE_UNDEF_AUDIO, PTP_OFC_MTP_UndefinedAudio); |
| 378 | register_filetype("Microsoft Windows Media Video", LIBMTP_FILETYPE_WMV, PTP_OFC_MTP_WMV); |
| 379 | register_filetype("Audio Video Interleave", LIBMTP_FILETYPE_AVI, PTP_OFC_AVI); |
| 380 | register_filetype("MPEG video stream", LIBMTP_FILETYPE_MPEG, PTP_OFC_MPEG); |
| 381 | register_filetype("Microsoft Advanced Systems Format", LIBMTP_FILETYPE_ASF, PTP_OFC_ASF); |
| 382 | register_filetype("Apple Quicktime container format", LIBMTP_FILETYPE_QT, PTP_OFC_QT); |
| 383 | register_filetype("Undefined video file", LIBMTP_FILETYPE_UNDEF_VIDEO, PTP_OFC_MTP_UndefinedVideo); |
| 384 | register_filetype("JPEG file", LIBMTP_FILETYPE_JPEG, PTP_OFC_EXIF_JPEG); |
Linus Walleij | 5fb4713 | 2006-12-30 15:35:48 +0000 | [diff] [blame] | 385 | register_filetype("JP2 file", LIBMTP_FILETYPE_JP2, PTP_OFC_JP2); |
| 386 | register_filetype("JPX file", LIBMTP_FILETYPE_JPX, PTP_OFC_JPX); |
Linus Walleij | cf42f45 | 2006-11-28 08:32:49 +0000 | [diff] [blame] | 387 | register_filetype("JFIF file", LIBMTP_FILETYPE_JFIF, PTP_OFC_JFIF); |
| 388 | register_filetype("TIFF bitmap file", LIBMTP_FILETYPE_TIFF, PTP_OFC_TIFF); |
| 389 | register_filetype("BMP bitmap file", LIBMTP_FILETYPE_BMP, PTP_OFC_BMP); |
| 390 | register_filetype("GIF bitmap file", LIBMTP_FILETYPE_GIF, PTP_OFC_GIF); |
| 391 | register_filetype("PICT bitmap file", LIBMTP_FILETYPE_PICT, PTP_OFC_PICT); |
| 392 | register_filetype("Portable Network Graphics", LIBMTP_FILETYPE_PNG, PTP_OFC_PNG); |
| 393 | register_filetype("Microsoft Windows Image Format", LIBMTP_FILETYPE_WINDOWSIMAGEFORMAT, PTP_OFC_MTP_WindowsImageFormat); |
| 394 | register_filetype("VCalendar version 1", LIBMTP_FILETYPE_VCALENDAR1, PTP_OFC_MTP_vCalendar1); |
| 395 | register_filetype("VCalendar version 2", LIBMTP_FILETYPE_VCALENDAR2, PTP_OFC_MTP_vCalendar2); |
| 396 | register_filetype("VCard version 2", LIBMTP_FILETYPE_VCARD2, PTP_OFC_MTP_vCard2); |
| 397 | register_filetype("VCard version 3", LIBMTP_FILETYPE_VCARD3, PTP_OFC_MTP_vCard3); |
| 398 | register_filetype("Undefined Windows executable file", LIBMTP_FILETYPE_WINEXEC, PTP_OFC_MTP_UndefinedWindowsExecutable); |
| 399 | register_filetype("Text file", LIBMTP_FILETYPE_TEXT, PTP_OFC_Text); |
| 400 | register_filetype("HTML file", LIBMTP_FILETYPE_HTML, PTP_OFC_HTML); |
Linus Walleij | 5fb4713 | 2006-12-30 15:35:48 +0000 | [diff] [blame] | 401 | register_filetype("XML file", LIBMTP_FILETYPE_XML, PTP_OFC_MTP_XMLDocument); |
| 402 | register_filetype("DOC file", LIBMTP_FILETYPE_DOC, PTP_OFC_MTP_MSWordDocument); |
| 403 | register_filetype("XLS file", LIBMTP_FILETYPE_XLS, PTP_OFC_MTP_MSExcelSpreadsheetXLS); |
| 404 | register_filetype("PPT file", LIBMTP_FILETYPE_PPT, PTP_OFC_MTP_MSPowerpointPresentationPPT); |
| 405 | register_filetype("MHT file", LIBMTP_FILETYPE_MHT, PTP_OFC_MTP_MHTCompiledHTMLDocument); |
Linus Walleij | a05b980 | 2006-12-08 08:50:30 +0000 | [diff] [blame] | 406 | register_filetype("Firmware file", LIBMTP_FILETYPE_FIRMWARE, PTP_OFC_MTP_Firmware); |
Richard Low | d3b1702 | 2009-04-11 12:37:39 +0000 | [diff] [blame] | 407 | register_filetype("Abstract Album file", LIBMTP_FILETYPE_ALBUM, PTP_OFC_MTP_AbstractAudioAlbum); |
| 408 | register_filetype("Abstract Playlist file", LIBMTP_FILETYPE_PLAYLIST, PTP_OFC_MTP_AbstractAudioVideoPlaylist); |
Linus Walleij | cf42f45 | 2006-11-28 08:32:49 +0000 | [diff] [blame] | 409 | register_filetype("Undefined filetype", LIBMTP_FILETYPE_UNKNOWN, PTP_OFC_Undefined); |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 410 | } |
Linus Walleij | 16c51f0 | 2006-05-04 13:20:22 +0000 | [diff] [blame] | 411 | |
Linus Walleij | 16c51f0 | 2006-05-04 13:20:22 +0000 | [diff] [blame] | 412 | /** |
| 413 | * Returns the PTP filetype that maps to a certain libmtp internal file type. |
| 414 | * @param intype the MTP library interface type |
| 415 | * @return the PTP (libgphoto2) interface type |
| 416 | */ |
| 417 | static uint16_t map_libmtp_type_to_ptp_type(LIBMTP_filetype_t intype) |
| 418 | { |
Linus Walleij | cf42f45 | 2006-11-28 08:32:49 +0000 | [diff] [blame] | 419 | filemap_t *current; |
Linus Walleij | 16c51f0 | 2006-05-04 13:20:22 +0000 | [diff] [blame] | 420 | |
Lei Zhang | 8173213 | 2013-09-04 17:27:35 -0700 | [diff] [blame] | 421 | current = g_filemap; |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 422 | |
| 423 | while (current != NULL) { |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 424 | if(current->id == intype) { |
| 425 | return current->ptp_id; |
| 426 | } |
| 427 | current = current->next; |
Linus Walleij | 16c51f0 | 2006-05-04 13:20:22 +0000 | [diff] [blame] | 428 | } |
Linus Walleij | 1fd2d27 | 2006-05-08 09:22:01 +0000 | [diff] [blame] | 429 | // printf("map_libmtp_type_to_ptp_type: unknown filetype.\n"); |
Linus Walleij | 16c51f0 | 2006-05-04 13:20:22 +0000 | [diff] [blame] | 430 | return PTP_OFC_Undefined; |
| 431 | } |
| 432 | |
| 433 | |
| 434 | /** |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 435 | * Returns the MTP internal interface type that maps to a certain ptp |
| 436 | * interface type. |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 437 | * @param intype the PTP (libgphoto2) interface type |
Linus Walleij | 8ab5426 | 2006-06-21 07:12:28 +0000 | [diff] [blame] | 438 | * @return the MTP library interface type |
Linus Walleij | 16c51f0 | 2006-05-04 13:20:22 +0000 | [diff] [blame] | 439 | */ |
| 440 | static LIBMTP_filetype_t map_ptp_type_to_libmtp_type(uint16_t intype) |
| 441 | { |
Linus Walleij | cf42f45 | 2006-11-28 08:32:49 +0000 | [diff] [blame] | 442 | filemap_t *current; |
Linus Walleij | 16c51f0 | 2006-05-04 13:20:22 +0000 | [diff] [blame] | 443 | |
Lei Zhang | 8173213 | 2013-09-04 17:27:35 -0700 | [diff] [blame] | 444 | current = g_filemap; |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 445 | |
| 446 | while (current != NULL) { |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 447 | if(current->ptp_id == intype) { |
| 448 | return current->id; |
| 449 | } |
| 450 | current = current->next; |
Linus Walleij | 16c51f0 | 2006-05-04 13:20:22 +0000 | [diff] [blame] | 451 | } |
Linus Walleij | 1fd2d27 | 2006-05-08 09:22:01 +0000 | [diff] [blame] | 452 | // printf("map_ptp_type_to_libmtp_type: unknown filetype.\n"); |
Linus Walleij | 16c51f0 | 2006-05-04 13:20:22 +0000 | [diff] [blame] | 453 | return LIBMTP_FILETYPE_UNKNOWN; |
| 454 | } |
| 455 | |
Richard Low | 6f07084 | 2009-05-03 10:26:16 +0000 | [diff] [blame] | 456 | /** |
| 457 | * Create a new property mapping entry |
| 458 | * @return a newly allocated propertymapping entry. |
| 459 | */ |
| 460 | static propertymap_t *new_propertymap_entry() |
| 461 | { |
| 462 | propertymap_t *propertymap; |
| 463 | |
| 464 | propertymap = (propertymap_t *)malloc(sizeof(propertymap_t)); |
| 465 | |
| 466 | if( propertymap != NULL ) { |
| 467 | propertymap->description = NULL; |
| 468 | propertymap->id = LIBMTP_PROPERTY_UNKNOWN; |
| 469 | propertymap->ptp_id = 0; |
| 470 | propertymap->next = NULL; |
| 471 | } |
| 472 | |
| 473 | return propertymap; |
| 474 | } |
| 475 | |
| 476 | /** |
| 477 | * Register an MTP or PTP property for data retrieval |
| 478 | * |
| 479 | * @param description Text description of property |
| 480 | * @param id libmtp internal property id |
| 481 | * @param ptp_id PTP property id |
| 482 | * @return 0 for success any other value means error. |
| 483 | */ |
| 484 | static int register_property(char const * const description, LIBMTP_property_t const id, |
| 485 | uint16_t const ptp_id) |
| 486 | { |
| 487 | propertymap_t *new = NULL, *current; |
| 488 | |
| 489 | // Has this LIBMTP propety been registered before ? |
Lei Zhang | 8173213 | 2013-09-04 17:27:35 -0700 | [diff] [blame] | 490 | current = g_propertymap; |
Richard Low | 6f07084 | 2009-05-03 10:26:16 +0000 | [diff] [blame] | 491 | while (current != NULL) { |
| 492 | if(current->id == id) { |
| 493 | break; |
| 494 | } |
| 495 | current = current->next; |
| 496 | } |
| 497 | |
| 498 | // Create the entry |
| 499 | if(current == NULL) { |
| 500 | new = new_propertymap_entry(); |
| 501 | if(new == NULL) { |
| 502 | return 1; |
| 503 | } |
| 504 | |
| 505 | new->id = id; |
| 506 | if(description != NULL) { |
| 507 | new->description = strdup(description); |
| 508 | } |
| 509 | new->ptp_id = ptp_id; |
| 510 | |
| 511 | // Add the entry to the list |
Lei Zhang | 8173213 | 2013-09-04 17:27:35 -0700 | [diff] [blame] | 512 | if(g_propertymap == NULL) { |
| 513 | g_propertymap = new; |
Richard Low | 6f07084 | 2009-05-03 10:26:16 +0000 | [diff] [blame] | 514 | } else { |
Lei Zhang | 8173213 | 2013-09-04 17:27:35 -0700 | [diff] [blame] | 515 | current = g_propertymap; |
Richard Low | 6f07084 | 2009-05-03 10:26:16 +0000 | [diff] [blame] | 516 | while (current->next != NULL ) current=current->next; |
| 517 | current->next = new; |
| 518 | } |
| 519 | // Update the existing entry |
| 520 | } else { |
| 521 | if (current->description != NULL) { |
| 522 | free(current->description); |
| 523 | } |
| 524 | current->description = NULL; |
| 525 | if(description != NULL) { |
| 526 | current->description = strdup(description); |
| 527 | } |
| 528 | current->ptp_id = ptp_id; |
| 529 | } |
| 530 | |
| 531 | return 0; |
| 532 | } |
| 533 | |
| 534 | static void init_propertymap() |
| 535 | { |
| 536 | register_property("Storage ID", LIBMTP_PROPERTY_StorageID, PTP_OPC_StorageID); |
| 537 | register_property("Object Format", LIBMTP_PROPERTY_ObjectFormat, PTP_OPC_ObjectFormat); |
| 538 | register_property("Protection Status", LIBMTP_PROPERTY_ProtectionStatus, PTP_OPC_ProtectionStatus); |
| 539 | register_property("Object Size", LIBMTP_PROPERTY_ObjectSize, PTP_OPC_ObjectSize); |
| 540 | register_property("Association Type", LIBMTP_PROPERTY_AssociationType, PTP_OPC_AssociationType); |
| 541 | register_property("Association Desc", LIBMTP_PROPERTY_AssociationDesc, PTP_OPC_AssociationDesc); |
| 542 | register_property("Object File Name", LIBMTP_PROPERTY_ObjectFileName, PTP_OPC_ObjectFileName); |
| 543 | register_property("Date Created", LIBMTP_PROPERTY_DateCreated, PTP_OPC_DateCreated); |
| 544 | register_property("Date Modified", LIBMTP_PROPERTY_DateModified, PTP_OPC_DateModified); |
| 545 | register_property("Keywords", LIBMTP_PROPERTY_Keywords, PTP_OPC_Keywords); |
| 546 | register_property("Parent Object", LIBMTP_PROPERTY_ParentObject, PTP_OPC_ParentObject); |
| 547 | register_property("Allowed Folder Contents", LIBMTP_PROPERTY_AllowedFolderContents, PTP_OPC_AllowedFolderContents); |
| 548 | register_property("Hidden", LIBMTP_PROPERTY_Hidden, PTP_OPC_Hidden); |
| 549 | register_property("System Object", LIBMTP_PROPERTY_SystemObject, PTP_OPC_SystemObject); |
| 550 | register_property("Persistant Unique Object Identifier", LIBMTP_PROPERTY_PersistantUniqueObjectIdentifier, PTP_OPC_PersistantUniqueObjectIdentifier); |
| 551 | register_property("Sync ID", LIBMTP_PROPERTY_SyncID, PTP_OPC_SyncID); |
| 552 | register_property("Property Bag", LIBMTP_PROPERTY_PropertyBag, PTP_OPC_PropertyBag); |
| 553 | register_property("Name", LIBMTP_PROPERTY_Name, PTP_OPC_Name); |
| 554 | register_property("Created By", LIBMTP_PROPERTY_CreatedBy, PTP_OPC_CreatedBy); |
| 555 | register_property("Artist", LIBMTP_PROPERTY_Artist, PTP_OPC_Artist); |
| 556 | register_property("Date Authored", LIBMTP_PROPERTY_DateAuthored, PTP_OPC_DateAuthored); |
| 557 | register_property("Description", LIBMTP_PROPERTY_Description, PTP_OPC_Description); |
| 558 | register_property("URL Reference", LIBMTP_PROPERTY_URLReference, PTP_OPC_URLReference); |
| 559 | register_property("Language Locale", LIBMTP_PROPERTY_LanguageLocale, PTP_OPC_LanguageLocale); |
| 560 | register_property("Copyright Information", LIBMTP_PROPERTY_CopyrightInformation, PTP_OPC_CopyrightInformation); |
| 561 | register_property("Source", LIBMTP_PROPERTY_Source, PTP_OPC_Source); |
| 562 | register_property("Origin Location", LIBMTP_PROPERTY_OriginLocation, PTP_OPC_OriginLocation); |
| 563 | register_property("Date Added", LIBMTP_PROPERTY_DateAdded, PTP_OPC_DateAdded); |
| 564 | register_property("Non Consumable", LIBMTP_PROPERTY_NonConsumable, PTP_OPC_NonConsumable); |
| 565 | register_property("Corrupt Or Unplayable", LIBMTP_PROPERTY_CorruptOrUnplayable, PTP_OPC_CorruptOrUnplayable); |
| 566 | register_property("Producer Serial Number", LIBMTP_PROPERTY_ProducerSerialNumber, PTP_OPC_ProducerSerialNumber); |
| 567 | register_property("Representative Sample Format", LIBMTP_PROPERTY_RepresentativeSampleFormat, PTP_OPC_RepresentativeSampleFormat); |
| 568 | register_property("Representative Sample Sise", LIBMTP_PROPERTY_RepresentativeSampleSize, PTP_OPC_RepresentativeSampleSize); |
| 569 | register_property("Representative Sample Height", LIBMTP_PROPERTY_RepresentativeSampleHeight, PTP_OPC_RepresentativeSampleHeight); |
| 570 | register_property("Representative Sample Width", LIBMTP_PROPERTY_RepresentativeSampleWidth, PTP_OPC_RepresentativeSampleWidth); |
| 571 | register_property("Representative Sample Duration", LIBMTP_PROPERTY_RepresentativeSampleDuration, PTP_OPC_RepresentativeSampleDuration); |
| 572 | register_property("Representative Sample Data", LIBMTP_PROPERTY_RepresentativeSampleData, PTP_OPC_RepresentativeSampleData); |
| 573 | register_property("Width", LIBMTP_PROPERTY_Width, PTP_OPC_Width); |
| 574 | register_property("Height", LIBMTP_PROPERTY_Height, PTP_OPC_Height); |
| 575 | register_property("Duration", LIBMTP_PROPERTY_Duration, PTP_OPC_Duration); |
| 576 | register_property("Rating", LIBMTP_PROPERTY_Rating, PTP_OPC_Rating); |
| 577 | register_property("Track", LIBMTP_PROPERTY_Track, PTP_OPC_Track); |
| 578 | register_property("Genre", LIBMTP_PROPERTY_Genre, PTP_OPC_Genre); |
| 579 | register_property("Credits", LIBMTP_PROPERTY_Credits, PTP_OPC_Credits); |
| 580 | register_property("Lyrics", LIBMTP_PROPERTY_Lyrics, PTP_OPC_Lyrics); |
| 581 | register_property("Subscription Content ID", LIBMTP_PROPERTY_SubscriptionContentID, PTP_OPC_SubscriptionContentID); |
| 582 | register_property("Produced By", LIBMTP_PROPERTY_ProducedBy, PTP_OPC_ProducedBy); |
| 583 | register_property("Use Count", LIBMTP_PROPERTY_UseCount, PTP_OPC_UseCount); |
| 584 | register_property("Skip Count", LIBMTP_PROPERTY_SkipCount, PTP_OPC_SkipCount); |
| 585 | register_property("Last Accessed", LIBMTP_PROPERTY_LastAccessed, PTP_OPC_LastAccessed); |
| 586 | register_property("Parental Rating", LIBMTP_PROPERTY_ParentalRating, PTP_OPC_ParentalRating); |
| 587 | register_property("Meta Genre", LIBMTP_PROPERTY_MetaGenre, PTP_OPC_MetaGenre); |
| 588 | register_property("Composer", LIBMTP_PROPERTY_Composer, PTP_OPC_Composer); |
| 589 | register_property("Effective Rating", LIBMTP_PROPERTY_EffectiveRating, PTP_OPC_EffectiveRating); |
| 590 | register_property("Subtitle", LIBMTP_PROPERTY_Subtitle, PTP_OPC_Subtitle); |
| 591 | register_property("Original Release Date", LIBMTP_PROPERTY_OriginalReleaseDate, PTP_OPC_OriginalReleaseDate); |
| 592 | register_property("Album Name", LIBMTP_PROPERTY_AlbumName, PTP_OPC_AlbumName); |
| 593 | register_property("Album Artist", LIBMTP_PROPERTY_AlbumArtist, PTP_OPC_AlbumArtist); |
| 594 | register_property("Mood", LIBMTP_PROPERTY_Mood, PTP_OPC_Mood); |
| 595 | register_property("DRM Status", LIBMTP_PROPERTY_DRMStatus, PTP_OPC_DRMStatus); |
| 596 | register_property("Sub Description", LIBMTP_PROPERTY_SubDescription, PTP_OPC_SubDescription); |
| 597 | register_property("Is Cropped", LIBMTP_PROPERTY_IsCropped, PTP_OPC_IsCropped); |
| 598 | register_property("Is Color Corrected", LIBMTP_PROPERTY_IsColorCorrected, PTP_OPC_IsColorCorrected); |
| 599 | register_property("Image Bit Depth", LIBMTP_PROPERTY_ImageBitDepth, PTP_OPC_ImageBitDepth); |
| 600 | register_property("f Number", LIBMTP_PROPERTY_Fnumber, PTP_OPC_Fnumber); |
| 601 | register_property("Exposure Time", LIBMTP_PROPERTY_ExposureTime, PTP_OPC_ExposureTime); |
| 602 | register_property("Exposure Index", LIBMTP_PROPERTY_ExposureIndex, PTP_OPC_ExposureIndex); |
| 603 | register_property("Display Name", LIBMTP_PROPERTY_DisplayName, PTP_OPC_DisplayName); |
| 604 | register_property("Body Text", LIBMTP_PROPERTY_BodyText, PTP_OPC_BodyText); |
| 605 | register_property("Subject", LIBMTP_PROPERTY_Subject, PTP_OPC_Subject); |
| 606 | register_property("Priority", LIBMTP_PROPERTY_Priority, PTP_OPC_Priority); |
| 607 | register_property("Given Name", LIBMTP_PROPERTY_GivenName, PTP_OPC_GivenName); |
| 608 | register_property("Middle Names", LIBMTP_PROPERTY_MiddleNames, PTP_OPC_MiddleNames); |
| 609 | register_property("Family Name", LIBMTP_PROPERTY_FamilyName, PTP_OPC_FamilyName); |
| 610 | register_property("Prefix", LIBMTP_PROPERTY_Prefix, PTP_OPC_Prefix); |
| 611 | register_property("Suffix", LIBMTP_PROPERTY_Suffix, PTP_OPC_Suffix); |
| 612 | register_property("Phonetic Given Name", LIBMTP_PROPERTY_PhoneticGivenName, PTP_OPC_PhoneticGivenName); |
| 613 | register_property("Phonetic Family Name", LIBMTP_PROPERTY_PhoneticFamilyName, PTP_OPC_PhoneticFamilyName); |
| 614 | register_property("Email: Primary", LIBMTP_PROPERTY_EmailPrimary, PTP_OPC_EmailPrimary); |
| 615 | register_property("Email: Personal 1", LIBMTP_PROPERTY_EmailPersonal1, PTP_OPC_EmailPersonal1); |
| 616 | register_property("Email: Personal 2", LIBMTP_PROPERTY_EmailPersonal2, PTP_OPC_EmailPersonal2); |
| 617 | register_property("Email: Business 1", LIBMTP_PROPERTY_EmailBusiness1, PTP_OPC_EmailBusiness1); |
| 618 | register_property("Email: Business 2", LIBMTP_PROPERTY_EmailBusiness2, PTP_OPC_EmailBusiness2); |
| 619 | register_property("Email: Others", LIBMTP_PROPERTY_EmailOthers, PTP_OPC_EmailOthers); |
| 620 | register_property("Phone Number: Primary", LIBMTP_PROPERTY_PhoneNumberPrimary, PTP_OPC_PhoneNumberPrimary); |
| 621 | register_property("Phone Number: Personal", LIBMTP_PROPERTY_PhoneNumberPersonal, PTP_OPC_PhoneNumberPersonal); |
| 622 | register_property("Phone Number: Personal 2", LIBMTP_PROPERTY_PhoneNumberPersonal2, PTP_OPC_PhoneNumberPersonal2); |
| 623 | register_property("Phone Number: Business", LIBMTP_PROPERTY_PhoneNumberBusiness, PTP_OPC_PhoneNumberBusiness); |
| 624 | register_property("Phone Number: Business 2", LIBMTP_PROPERTY_PhoneNumberBusiness2, PTP_OPC_PhoneNumberBusiness2); |
| 625 | register_property("Phone Number: Mobile", LIBMTP_PROPERTY_PhoneNumberMobile, PTP_OPC_PhoneNumberMobile); |
| 626 | register_property("Phone Number: Mobile 2", LIBMTP_PROPERTY_PhoneNumberMobile2, PTP_OPC_PhoneNumberMobile2); |
| 627 | register_property("Fax Number: Primary", LIBMTP_PROPERTY_FaxNumberPrimary, PTP_OPC_FaxNumberPrimary); |
| 628 | register_property("Fax Number: Personal", LIBMTP_PROPERTY_FaxNumberPersonal, PTP_OPC_FaxNumberPersonal); |
| 629 | register_property("Fax Number: Business", LIBMTP_PROPERTY_FaxNumberBusiness, PTP_OPC_FaxNumberBusiness); |
| 630 | register_property("Pager Number", LIBMTP_PROPERTY_PagerNumber, PTP_OPC_PagerNumber); |
| 631 | register_property("Phone Number: Others", LIBMTP_PROPERTY_PhoneNumberOthers, PTP_OPC_PhoneNumberOthers); |
| 632 | register_property("Primary Web Address", LIBMTP_PROPERTY_PrimaryWebAddress, PTP_OPC_PrimaryWebAddress); |
| 633 | register_property("Personal Web Address", LIBMTP_PROPERTY_PersonalWebAddress, PTP_OPC_PersonalWebAddress); |
| 634 | register_property("Business Web Address", LIBMTP_PROPERTY_BusinessWebAddress, PTP_OPC_BusinessWebAddress); |
| 635 | register_property("Instant Messenger Address 1", LIBMTP_PROPERTY_InstantMessengerAddress, PTP_OPC_InstantMessengerAddress); |
| 636 | register_property("Instant Messenger Address 2", LIBMTP_PROPERTY_InstantMessengerAddress2, PTP_OPC_InstantMessengerAddress2); |
| 637 | register_property("Instant Messenger Address 3", LIBMTP_PROPERTY_InstantMessengerAddress3, PTP_OPC_InstantMessengerAddress3); |
| 638 | register_property("Postal Address: Personal: Full", LIBMTP_PROPERTY_PostalAddressPersonalFull, PTP_OPC_PostalAddressPersonalFull); |
| 639 | register_property("Postal Address: Personal: Line 1", LIBMTP_PROPERTY_PostalAddressPersonalFullLine1, PTP_OPC_PostalAddressPersonalFullLine1); |
| 640 | register_property("Postal Address: Personal: Line 2", LIBMTP_PROPERTY_PostalAddressPersonalFullLine2, PTP_OPC_PostalAddressPersonalFullLine2); |
| 641 | register_property("Postal Address: Personal: City", LIBMTP_PROPERTY_PostalAddressPersonalFullCity, PTP_OPC_PostalAddressPersonalFullCity); |
| 642 | register_property("Postal Address: Personal: Region", LIBMTP_PROPERTY_PostalAddressPersonalFullRegion, PTP_OPC_PostalAddressPersonalFullRegion); |
| 643 | register_property("Postal Address: Personal: Postal Code", LIBMTP_PROPERTY_PostalAddressPersonalFullPostalCode, PTP_OPC_PostalAddressPersonalFullPostalCode); |
| 644 | register_property("Postal Address: Personal: Country", LIBMTP_PROPERTY_PostalAddressPersonalFullCountry, PTP_OPC_PostalAddressPersonalFullCountry); |
| 645 | register_property("Postal Address: Business: Full", LIBMTP_PROPERTY_PostalAddressBusinessFull, PTP_OPC_PostalAddressBusinessFull); |
| 646 | register_property("Postal Address: Business: Line 1", LIBMTP_PROPERTY_PostalAddressBusinessLine1, PTP_OPC_PostalAddressBusinessLine1); |
| 647 | register_property("Postal Address: Business: Line 2", LIBMTP_PROPERTY_PostalAddressBusinessLine2, PTP_OPC_PostalAddressBusinessLine2); |
| 648 | register_property("Postal Address: Business: City", LIBMTP_PROPERTY_PostalAddressBusinessCity, PTP_OPC_PostalAddressBusinessCity); |
| 649 | register_property("Postal Address: Business: Region", LIBMTP_PROPERTY_PostalAddressBusinessRegion, PTP_OPC_PostalAddressBusinessRegion); |
| 650 | register_property("Postal Address: Business: Postal Code", LIBMTP_PROPERTY_PostalAddressBusinessPostalCode, PTP_OPC_PostalAddressBusinessPostalCode); |
| 651 | register_property("Postal Address: Business: Country", LIBMTP_PROPERTY_PostalAddressBusinessCountry, PTP_OPC_PostalAddressBusinessCountry); |
| 652 | register_property("Postal Address: Other: Full", LIBMTP_PROPERTY_PostalAddressOtherFull, PTP_OPC_PostalAddressOtherFull); |
| 653 | register_property("Postal Address: Other: Line 1", LIBMTP_PROPERTY_PostalAddressOtherLine1, PTP_OPC_PostalAddressOtherLine1); |
| 654 | register_property("Postal Address: Other: Line 2", LIBMTP_PROPERTY_PostalAddressOtherLine2, PTP_OPC_PostalAddressOtherLine2); |
| 655 | register_property("Postal Address: Other: City", LIBMTP_PROPERTY_PostalAddressOtherCity, PTP_OPC_PostalAddressOtherCity); |
| 656 | register_property("Postal Address: Other: Region", LIBMTP_PROPERTY_PostalAddressOtherRegion, PTP_OPC_PostalAddressOtherRegion); |
| 657 | register_property("Postal Address: Other: Postal Code", LIBMTP_PROPERTY_PostalAddressOtherPostalCode, PTP_OPC_PostalAddressOtherPostalCode); |
| 658 | register_property("Postal Address: Other: Counrtry", LIBMTP_PROPERTY_PostalAddressOtherCountry, PTP_OPC_PostalAddressOtherCountry); |
| 659 | register_property("Organization Name", LIBMTP_PROPERTY_OrganizationName, PTP_OPC_OrganizationName); |
| 660 | register_property("Phonetic Organization Name", LIBMTP_PROPERTY_PhoneticOrganizationName, PTP_OPC_PhoneticOrganizationName); |
| 661 | register_property("Role", LIBMTP_PROPERTY_Role, PTP_OPC_Role); |
| 662 | register_property("Birthdate", LIBMTP_PROPERTY_Birthdate, PTP_OPC_Birthdate); |
| 663 | register_property("Message To", LIBMTP_PROPERTY_MessageTo, PTP_OPC_MessageTo); |
| 664 | register_property("Message CC", LIBMTP_PROPERTY_MessageCC, PTP_OPC_MessageCC); |
| 665 | register_property("Message BCC", LIBMTP_PROPERTY_MessageBCC, PTP_OPC_MessageBCC); |
| 666 | register_property("Message Read", LIBMTP_PROPERTY_MessageRead, PTP_OPC_MessageRead); |
| 667 | register_property("Message Received Time", LIBMTP_PROPERTY_MessageReceivedTime, PTP_OPC_MessageReceivedTime); |
| 668 | register_property("Message Sender", LIBMTP_PROPERTY_MessageSender, PTP_OPC_MessageSender); |
| 669 | register_property("Activity Begin Time", LIBMTP_PROPERTY_ActivityBeginTime, PTP_OPC_ActivityBeginTime); |
| 670 | register_property("Activity End Time", LIBMTP_PROPERTY_ActivityEndTime, PTP_OPC_ActivityEndTime); |
| 671 | register_property("Activity Location", LIBMTP_PROPERTY_ActivityLocation, PTP_OPC_ActivityLocation); |
| 672 | register_property("Activity Required Attendees", LIBMTP_PROPERTY_ActivityRequiredAttendees, PTP_OPC_ActivityRequiredAttendees); |
| 673 | register_property("Optional Attendees", LIBMTP_PROPERTY_ActivityOptionalAttendees, PTP_OPC_ActivityOptionalAttendees); |
| 674 | register_property("Activity Resources", LIBMTP_PROPERTY_ActivityResources, PTP_OPC_ActivityResources); |
| 675 | register_property("Activity Accepted", LIBMTP_PROPERTY_ActivityAccepted, PTP_OPC_ActivityAccepted); |
| 676 | register_property("Owner", LIBMTP_PROPERTY_Owner, PTP_OPC_Owner); |
| 677 | register_property("Editor", LIBMTP_PROPERTY_Editor, PTP_OPC_Editor); |
| 678 | register_property("Webmaster", LIBMTP_PROPERTY_Webmaster, PTP_OPC_Webmaster); |
| 679 | register_property("URL Source", LIBMTP_PROPERTY_URLSource, PTP_OPC_URLSource); |
| 680 | register_property("URL Destination", LIBMTP_PROPERTY_URLDestination, PTP_OPC_URLDestination); |
| 681 | register_property("Time Bookmark", LIBMTP_PROPERTY_TimeBookmark, PTP_OPC_TimeBookmark); |
| 682 | register_property("Object Bookmark", LIBMTP_PROPERTY_ObjectBookmark, PTP_OPC_ObjectBookmark); |
| 683 | register_property("Byte Bookmark", LIBMTP_PROPERTY_ByteBookmark, PTP_OPC_ByteBookmark); |
| 684 | register_property("Last Build Date", LIBMTP_PROPERTY_LastBuildDate, PTP_OPC_LastBuildDate); |
| 685 | register_property("Time To Live", LIBMTP_PROPERTY_TimetoLive, PTP_OPC_TimetoLive); |
| 686 | register_property("Media GUID", LIBMTP_PROPERTY_MediaGUID, PTP_OPC_MediaGUID); |
| 687 | register_property("Total Bit Rate", LIBMTP_PROPERTY_TotalBitRate, PTP_OPC_TotalBitRate); |
| 688 | register_property("Bit Rate Type", LIBMTP_PROPERTY_BitRateType, PTP_OPC_BitRateType); |
| 689 | register_property("Sample Rate", LIBMTP_PROPERTY_SampleRate, PTP_OPC_SampleRate); |
| 690 | register_property("Number Of Channels", LIBMTP_PROPERTY_NumberOfChannels, PTP_OPC_NumberOfChannels); |
| 691 | register_property("Audio Bit Depth", LIBMTP_PROPERTY_AudioBitDepth, PTP_OPC_AudioBitDepth); |
| 692 | register_property("Scan Depth", LIBMTP_PROPERTY_ScanDepth, PTP_OPC_ScanDepth); |
| 693 | register_property("Audio WAVE Codec", LIBMTP_PROPERTY_AudioWAVECodec, PTP_OPC_AudioWAVECodec); |
| 694 | register_property("Audio Bit Rate", LIBMTP_PROPERTY_AudioBitRate, PTP_OPC_AudioBitRate); |
| 695 | register_property("Video Four CC Codec", LIBMTP_PROPERTY_VideoFourCCCodec, PTP_OPC_VideoFourCCCodec); |
| 696 | register_property("Video Bit Rate", LIBMTP_PROPERTY_VideoBitRate, PTP_OPC_VideoBitRate); |
| 697 | register_property("Frames Per Thousand Seconds", LIBMTP_PROPERTY_FramesPerThousandSeconds, PTP_OPC_FramesPerThousandSeconds); |
| 698 | register_property("Key Frame Distance", LIBMTP_PROPERTY_KeyFrameDistance, PTP_OPC_KeyFrameDistance); |
| 699 | register_property("Buffer Size", LIBMTP_PROPERTY_BufferSize, PTP_OPC_BufferSize); |
| 700 | register_property("Encoding Quality", LIBMTP_PROPERTY_EncodingQuality, PTP_OPC_EncodingQuality); |
| 701 | register_property("Encoding Profile", LIBMTP_PROPERTY_EncodingProfile, PTP_OPC_EncodingProfile); |
| 702 | register_property("Buy flag", LIBMTP_PROPERTY_BuyFlag, PTP_OPC_BuyFlag); |
| 703 | register_property("Unknown property", LIBMTP_PROPERTY_UNKNOWN, 0); |
| 704 | } |
| 705 | |
| 706 | /** |
| 707 | * Returns the PTP property that maps to a certain libmtp internal property type. |
| 708 | * @param inproperty the MTP library interface property |
| 709 | * @return the PTP (libgphoto2) property type |
| 710 | */ |
| 711 | static uint16_t map_libmtp_property_to_ptp_property(LIBMTP_property_t inproperty) |
| 712 | { |
| 713 | propertymap_t *current; |
| 714 | |
Lei Zhang | 8173213 | 2013-09-04 17:27:35 -0700 | [diff] [blame] | 715 | current = g_propertymap; |
Richard Low | 6f07084 | 2009-05-03 10:26:16 +0000 | [diff] [blame] | 716 | |
| 717 | while (current != NULL) { |
| 718 | if(current->id == inproperty) { |
| 719 | return current->ptp_id; |
| 720 | } |
| 721 | current = current->next; |
| 722 | } |
| 723 | return 0; |
| 724 | } |
| 725 | |
| 726 | |
| 727 | /** |
| 728 | * Returns the MTP internal interface property that maps to a certain ptp |
| 729 | * interface property. |
| 730 | * @param inproperty the PTP (libgphoto2) interface property |
| 731 | * @return the MTP library interface property |
| 732 | */ |
| 733 | static LIBMTP_property_t map_ptp_property_to_libmtp_property(uint16_t inproperty) |
| 734 | { |
| 735 | propertymap_t *current; |
| 736 | |
Lei Zhang | 8173213 | 2013-09-04 17:27:35 -0700 | [diff] [blame] | 737 | current = g_propertymap; |
Richard Low | 6f07084 | 2009-05-03 10:26:16 +0000 | [diff] [blame] | 738 | |
| 739 | while (current != NULL) { |
| 740 | if(current->ptp_id == inproperty) { |
| 741 | return current->id; |
| 742 | } |
| 743 | current = current->next; |
| 744 | } |
| 745 | // printf("map_ptp_type_to_libmtp_type: unknown filetype.\n"); |
| 746 | return LIBMTP_PROPERTY_UNKNOWN; |
| 747 | } |
| 748 | |
Linus Walleij | fa1374c | 2006-02-27 07:41:46 +0000 | [diff] [blame] | 749 | |
Linus Walleij | 6946ac5 | 2006-03-21 06:51:22 +0000 | [diff] [blame] | 750 | /** |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 751 | * Set the debug level. |
| 752 | * |
| 753 | * By default, the debug level is set to '0' (disable). |
| 754 | */ |
| 755 | void LIBMTP_Set_Debug(int level) |
| 756 | { |
| 757 | if (LIBMTP_debug || level) |
Catalin Patulea | da25de0 | 2012-07-19 00:57:42 -0400 | [diff] [blame] | 758 | LIBMTP_ERROR("LIBMTP_Set_Debug: Setting debugging level to %d (0x%02x) " |
| 759 | "(%s)\n", level, level, level ? "on" : "off"); |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 760 | |
| 761 | LIBMTP_debug = level; |
| 762 | } |
| 763 | |
| 764 | |
| 765 | /** |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 766 | * Initialize the library. You are only supposed to call this |
| 767 | * one, before using the library for the first time in a program. |
| 768 | * Never re-initialize libmtp! |
| 769 | * |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 770 | * The only thing this does at the moment is to initialise the |
Sajid Anwar | 8dca41d | 2012-08-18 20:36:13 +0200 | [diff] [blame] | 771 | * filetype mapping table, as well as load MTPZ data if necessary. |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 772 | */ |
| 773 | void LIBMTP_Init(void) |
| 774 | { |
Catalin Patulea | da25de0 | 2012-07-19 00:57:42 -0400 | [diff] [blame] | 775 | const char *env_debug = getenv("LIBMTP_DEBUG"); |
| 776 | if (env_debug) { |
| 777 | const long debug_flags = strtol(env_debug, NULL, 0); |
| 778 | if (debug_flags != LONG_MIN && debug_flags != LONG_MAX && |
| 779 | INT_MIN <= debug_flags && debug_flags <= INT_MAX) { |
| 780 | LIBMTP_Set_Debug(debug_flags); |
| 781 | } else { |
| 782 | fprintf(stderr, "LIBMTP_Init: error setting debug flags from environment " |
| 783 | "value \"%s\"\n", env_debug); |
| 784 | } |
| 785 | } |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 786 | |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 787 | init_filemap(); |
Richard Low | 6f07084 | 2009-05-03 10:26:16 +0000 | [diff] [blame] | 788 | init_propertymap(); |
Sajid Anwar | 8dca41d | 2012-08-18 20:36:13 +0200 | [diff] [blame] | 789 | |
| 790 | if (mtpz_loaddata() == -1) |
| 791 | use_mtpz = 0; |
| 792 | else |
| 793 | use_mtpz = 1; |
| 794 | |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 795 | return; |
| 796 | } |
| 797 | |
Linus Walleij | cf42f45 | 2006-11-28 08:32:49 +0000 | [diff] [blame] | 798 | |
| 799 | /** |
| 800 | * This helper function returns a textual description for a libmtp |
| 801 | * file type to be used in dialog boxes etc. |
| 802 | * @param intype the libmtp internal filetype to get a description for. |
| 803 | * @return a string representing the filetype, this must <b>NOT</b> |
| 804 | * be free():ed by the caller! |
| 805 | */ |
| 806 | char const * LIBMTP_Get_Filetype_Description(LIBMTP_filetype_t intype) |
| 807 | { |
| 808 | filemap_t *current; |
| 809 | |
Lei Zhang | 8173213 | 2013-09-04 17:27:35 -0700 | [diff] [blame] | 810 | current = g_filemap; |
Linus Walleij | cf42f45 | 2006-11-28 08:32:49 +0000 | [diff] [blame] | 811 | |
| 812 | while (current != NULL) { |
| 813 | if(current->id == intype) { |
| 814 | return current->description; |
| 815 | } |
| 816 | current = current->next; |
| 817 | } |
| 818 | |
| 819 | return "Unknown filetype"; |
| 820 | } |
| 821 | |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 822 | /** |
Richard Low | 6f07084 | 2009-05-03 10:26:16 +0000 | [diff] [blame] | 823 | * This helper function returns a textual description for a libmtp |
| 824 | * property to be used in dialog boxes etc. |
| 825 | * @param inproperty the libmtp internal property to get a description for. |
| 826 | * @return a string representing the filetype, this must <b>NOT</b> |
| 827 | * be free():ed by the caller! |
| 828 | */ |
| 829 | char const * LIBMTP_Get_Property_Description(LIBMTP_property_t inproperty) |
| 830 | { |
| 831 | propertymap_t *current; |
| 832 | |
Lei Zhang | 8173213 | 2013-09-04 17:27:35 -0700 | [diff] [blame] | 833 | current = g_propertymap; |
Richard Low | 6f07084 | 2009-05-03 10:26:16 +0000 | [diff] [blame] | 834 | |
| 835 | while (current != NULL) { |
| 836 | if(current->id == inproperty) { |
| 837 | return current->description; |
| 838 | } |
| 839 | current = current->next; |
| 840 | } |
| 841 | |
| 842 | return "Unknown property"; |
| 843 | } |
| 844 | |
| 845 | /** |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 846 | * This function will do its best to fit a 16bit |
| 847 | * value into a PTP object property if the property |
| 848 | * is limited in range or step sizes. |
| 849 | */ |
Linus Walleij | 2955956 | 2007-08-22 21:38:04 +0000 | [diff] [blame] | 850 | static uint16_t adjust_u16(uint16_t val, PTPObjectPropDesc *opd) |
| 851 | { |
| 852 | switch (opd->FormFlag) { |
| 853 | case PTP_DPFF_Range: |
| 854 | if (val < opd->FORM.Range.MinimumValue.u16) { |
| 855 | return opd->FORM.Range.MinimumValue.u16; |
| 856 | } |
| 857 | if (val > opd->FORM.Range.MaximumValue.u16) { |
| 858 | return opd->FORM.Range.MaximumValue.u16; |
| 859 | } |
| 860 | // Round down to last step. |
| 861 | if (val % opd->FORM.Range.StepSize.u16 != 0) { |
| 862 | return val - (val % opd->FORM.Range.StepSize.u16); |
| 863 | } |
| 864 | return val; |
| 865 | break; |
| 866 | case PTP_DPFF_Enumeration: |
| 867 | { |
| 868 | int i; |
| 869 | uint16_t bestfit = opd->FORM.Enum.SupportedValue[0].u16; |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 870 | |
Linus Walleij | 2955956 | 2007-08-22 21:38:04 +0000 | [diff] [blame] | 871 | for (i=0; i<opd->FORM.Enum.NumberOfValues; i++) { |
| 872 | if (val == opd->FORM.Enum.SupportedValue[i].u16) { |
| 873 | return val; |
| 874 | } |
| 875 | // Rough guess of best fit |
| 876 | if (opd->FORM.Enum.SupportedValue[i].u16 < val) { |
| 877 | bestfit = opd->FORM.Enum.SupportedValue[i].u16; |
| 878 | } |
| 879 | } |
| 880 | // Just some default that'll work. |
| 881 | return bestfit; |
| 882 | } |
| 883 | default: |
| 884 | // Will accept any value |
| 885 | break; |
| 886 | } |
| 887 | return val; |
| 888 | } |
| 889 | |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 890 | /** |
| 891 | * This function will do its best to fit a 32bit |
| 892 | * value into a PTP object property if the property |
| 893 | * is limited in range or step sizes. |
| 894 | */ |
Linus Walleij | 2955956 | 2007-08-22 21:38:04 +0000 | [diff] [blame] | 895 | static uint32_t adjust_u32(uint32_t val, PTPObjectPropDesc *opd) |
| 896 | { |
| 897 | switch (opd->FormFlag) { |
| 898 | case PTP_DPFF_Range: |
| 899 | if (val < opd->FORM.Range.MinimumValue.u32) { |
| 900 | return opd->FORM.Range.MinimumValue.u32; |
| 901 | } |
| 902 | if (val > opd->FORM.Range.MaximumValue.u32) { |
| 903 | return opd->FORM.Range.MaximumValue.u32; |
| 904 | } |
| 905 | // Round down to last step. |
| 906 | if (val % opd->FORM.Range.StepSize.u32 != 0) { |
| 907 | return val - (val % opd->FORM.Range.StepSize.u32); |
| 908 | } |
| 909 | return val; |
| 910 | break; |
| 911 | case PTP_DPFF_Enumeration: |
| 912 | { |
| 913 | int i; |
| 914 | uint32_t bestfit = opd->FORM.Enum.SupportedValue[0].u32; |
| 915 | |
| 916 | for (i=0; i<opd->FORM.Enum.NumberOfValues; i++) { |
| 917 | if (val == opd->FORM.Enum.SupportedValue[i].u32) { |
| 918 | return val; |
| 919 | } |
| 920 | // Rough guess of best fit |
| 921 | if (opd->FORM.Enum.SupportedValue[i].u32 < val) { |
| 922 | bestfit = opd->FORM.Enum.SupportedValue[i].u32; |
| 923 | } |
| 924 | } |
| 925 | // Just some default that'll work. |
| 926 | return bestfit; |
| 927 | } |
| 928 | default: |
| 929 | // Will accept any value |
| 930 | break; |
| 931 | } |
| 932 | return val; |
| 933 | } |
| 934 | |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 935 | /** |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 936 | * This function returns a newly created ISO 8601 timestamp with the |
| 937 | * current time in as high precision as possible. It even adds |
| 938 | * the time zone if it can. |
| 939 | */ |
| 940 | static char *get_iso8601_stamp(void) |
| 941 | { |
| 942 | time_t curtime; |
| 943 | struct tm *loctime; |
| 944 | char tmp[64]; |
| 945 | |
| 946 | curtime = time(NULL); |
| 947 | loctime = localtime(&curtime); |
| 948 | strftime (tmp, sizeof(tmp), "%Y%m%dT%H%M%S.0%z", loctime); |
| 949 | return strdup(tmp); |
| 950 | } |
| 951 | |
| 952 | /** |
Richard Low | 6f07084 | 2009-05-03 10:26:16 +0000 | [diff] [blame] | 953 | * Gets the allowed values (range or enum) for a property |
| 954 | * @param device a pointer to an MTP device |
| 955 | * @param property the property to query |
| 956 | * @param filetype the filetype of the object you want to set values for |
| 957 | * @param allowed_vals pointer to a LIBMTP_allowed_values_t struct to |
| 958 | * receive the allowed values. Call LIBMTP_destroy_allowed_values_t |
| 959 | * on this on successful completion. |
| 960 | * @return 0 on success, any other value means failure |
| 961 | */ |
| 962 | int LIBMTP_Get_Allowed_Property_Values(LIBMTP_mtpdevice_t *device, LIBMTP_property_t const property, |
| 963 | LIBMTP_filetype_t const filetype, LIBMTP_allowed_values_t *allowed_vals) |
| 964 | { |
| 965 | PTPObjectPropDesc opd; |
| 966 | uint16_t ret = 0; |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 967 | |
Richard Low | 6f07084 | 2009-05-03 10:26:16 +0000 | [diff] [blame] | 968 | ret = ptp_mtp_getobjectpropdesc(device->params, map_libmtp_property_to_ptp_property(property), map_libmtp_type_to_ptp_type(filetype), &opd); |
| 969 | if (ret != PTP_RC_OK) { |
| 970 | add_ptp_error_to_errorstack(device, ret, "LIBMTP_Get_Allowed_Property_Values(): could not get property description."); |
| 971 | return -1; |
| 972 | } |
| 973 | |
| 974 | if (opd.FormFlag == PTP_OPFF_Enumeration) { |
| 975 | int i = 0; |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 976 | |
Richard Low | 6f07084 | 2009-05-03 10:26:16 +0000 | [diff] [blame] | 977 | allowed_vals->is_range = 0; |
| 978 | allowed_vals->num_entries = opd.FORM.Enum.NumberOfValues; |
| 979 | |
| 980 | switch (opd.DataType) |
| 981 | { |
| 982 | case PTP_DTC_INT8: |
| 983 | allowed_vals->i8vals = malloc(sizeof(int8_t) * opd.FORM.Enum.NumberOfValues); |
| 984 | allowed_vals->datatype = LIBMTP_DATATYPE_INT8; |
| 985 | break; |
| 986 | case PTP_DTC_UINT8: |
| 987 | allowed_vals->u8vals = malloc(sizeof(uint8_t) * opd.FORM.Enum.NumberOfValues); |
| 988 | allowed_vals->datatype = LIBMTP_DATATYPE_UINT8; |
| 989 | break; |
| 990 | case PTP_DTC_INT16: |
| 991 | allowed_vals->i16vals = malloc(sizeof(int16_t) * opd.FORM.Enum.NumberOfValues); |
| 992 | allowed_vals->datatype = LIBMTP_DATATYPE_INT16; |
| 993 | break; |
| 994 | case PTP_DTC_UINT16: |
| 995 | allowed_vals->u16vals = malloc(sizeof(uint16_t) * opd.FORM.Enum.NumberOfValues); |
| 996 | allowed_vals->datatype = LIBMTP_DATATYPE_UINT16; |
| 997 | break; |
| 998 | case PTP_DTC_INT32: |
| 999 | allowed_vals->i32vals = malloc(sizeof(int32_t) * opd.FORM.Enum.NumberOfValues); |
| 1000 | allowed_vals->datatype = LIBMTP_DATATYPE_INT32; |
| 1001 | break; |
| 1002 | case PTP_DTC_UINT32: |
| 1003 | allowed_vals->u32vals = malloc(sizeof(uint32_t) * opd.FORM.Enum.NumberOfValues); |
| 1004 | allowed_vals->datatype = LIBMTP_DATATYPE_UINT32; |
| 1005 | break; |
| 1006 | case PTP_DTC_INT64: |
| 1007 | allowed_vals->i64vals = malloc(sizeof(int64_t) * opd.FORM.Enum.NumberOfValues); |
| 1008 | allowed_vals->datatype = LIBMTP_DATATYPE_INT64; |
| 1009 | break; |
| 1010 | case PTP_DTC_UINT64: |
| 1011 | allowed_vals->u64vals = malloc(sizeof(uint64_t) * opd.FORM.Enum.NumberOfValues); |
| 1012 | allowed_vals->datatype = LIBMTP_DATATYPE_UINT64; |
| 1013 | break; |
| 1014 | } |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 1015 | |
Richard Low | 6f07084 | 2009-05-03 10:26:16 +0000 | [diff] [blame] | 1016 | for (i = 0; i < opd.FORM.Enum.NumberOfValues; i++) { |
| 1017 | switch (opd.DataType) |
| 1018 | { |
| 1019 | case PTP_DTC_INT8: |
| 1020 | allowed_vals->i8vals[i] = opd.FORM.Enum.SupportedValue[i].i8; |
| 1021 | break; |
| 1022 | case PTP_DTC_UINT8: |
| 1023 | allowed_vals->u8vals[i] = opd.FORM.Enum.SupportedValue[i].u8; |
| 1024 | break; |
| 1025 | case PTP_DTC_INT16: |
| 1026 | allowed_vals->i16vals[i] = opd.FORM.Enum.SupportedValue[i].i16; |
| 1027 | break; |
| 1028 | case PTP_DTC_UINT16: |
| 1029 | allowed_vals->u16vals[i] = opd.FORM.Enum.SupportedValue[i].u16; |
| 1030 | break; |
| 1031 | case PTP_DTC_INT32: |
| 1032 | allowed_vals->i32vals[i] = opd.FORM.Enum.SupportedValue[i].i32; |
| 1033 | break; |
| 1034 | case PTP_DTC_UINT32: |
| 1035 | allowed_vals->u32vals[i] = opd.FORM.Enum.SupportedValue[i].u32; |
| 1036 | break; |
| 1037 | case PTP_DTC_INT64: |
| 1038 | allowed_vals->i64vals[i] = opd.FORM.Enum.SupportedValue[i].i64; |
| 1039 | break; |
| 1040 | case PTP_DTC_UINT64: |
| 1041 | allowed_vals->u64vals[i] = opd.FORM.Enum.SupportedValue[i].u64; |
| 1042 | break; |
| 1043 | } |
| 1044 | } |
| 1045 | ptp_free_objectpropdesc(&opd); |
| 1046 | return 0; |
| 1047 | } else if (opd.FormFlag == PTP_OPFF_Range) { |
| 1048 | allowed_vals->is_range = 1; |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 1049 | |
Richard Low | 6f07084 | 2009-05-03 10:26:16 +0000 | [diff] [blame] | 1050 | switch (opd.DataType) |
| 1051 | { |
| 1052 | case PTP_DTC_INT8: |
| 1053 | allowed_vals->i8min = opd.FORM.Range.MinimumValue.i8; |
| 1054 | allowed_vals->i8max = opd.FORM.Range.MaximumValue.i8; |
| 1055 | allowed_vals->i8step = opd.FORM.Range.StepSize.i8; |
| 1056 | allowed_vals->datatype = LIBMTP_DATATYPE_INT8; |
| 1057 | break; |
| 1058 | case PTP_DTC_UINT8: |
| 1059 | allowed_vals->u8min = opd.FORM.Range.MinimumValue.u8; |
| 1060 | allowed_vals->u8max = opd.FORM.Range.MaximumValue.u8; |
| 1061 | allowed_vals->u8step = opd.FORM.Range.StepSize.u8; |
| 1062 | allowed_vals->datatype = LIBMTP_DATATYPE_UINT8; |
| 1063 | break; |
| 1064 | case PTP_DTC_INT16: |
| 1065 | allowed_vals->i16min = opd.FORM.Range.MinimumValue.i16; |
| 1066 | allowed_vals->i16max = opd.FORM.Range.MaximumValue.i16; |
| 1067 | allowed_vals->i16step = opd.FORM.Range.StepSize.i16; |
| 1068 | allowed_vals->datatype = LIBMTP_DATATYPE_INT16; |
| 1069 | break; |
| 1070 | case PTP_DTC_UINT16: |
| 1071 | allowed_vals->u16min = opd.FORM.Range.MinimumValue.u16; |
| 1072 | allowed_vals->u16max = opd.FORM.Range.MaximumValue.u16; |
| 1073 | allowed_vals->u16step = opd.FORM.Range.StepSize.u16; |
| 1074 | allowed_vals->datatype = LIBMTP_DATATYPE_UINT16; |
| 1075 | break; |
| 1076 | case PTP_DTC_INT32: |
| 1077 | allowed_vals->i32min = opd.FORM.Range.MinimumValue.i32; |
| 1078 | allowed_vals->i32max = opd.FORM.Range.MaximumValue.i32; |
| 1079 | allowed_vals->i32step = opd.FORM.Range.StepSize.i32; |
| 1080 | allowed_vals->datatype = LIBMTP_DATATYPE_INT32; |
| 1081 | break; |
| 1082 | case PTP_DTC_UINT32: |
| 1083 | allowed_vals->u32min = opd.FORM.Range.MinimumValue.u32; |
| 1084 | allowed_vals->u32max = opd.FORM.Range.MaximumValue.u32; |
| 1085 | allowed_vals->u32step = opd.FORM.Range.StepSize.u32; |
| 1086 | allowed_vals->datatype = LIBMTP_DATATYPE_UINT32; |
| 1087 | break; |
| 1088 | case PTP_DTC_INT64: |
| 1089 | allowed_vals->i64min = opd.FORM.Range.MinimumValue.i64; |
| 1090 | allowed_vals->i64max = opd.FORM.Range.MaximumValue.i64; |
| 1091 | allowed_vals->i64step = opd.FORM.Range.StepSize.i64; |
| 1092 | allowed_vals->datatype = LIBMTP_DATATYPE_INT64; |
| 1093 | break; |
| 1094 | case PTP_DTC_UINT64: |
| 1095 | allowed_vals->u64min = opd.FORM.Range.MinimumValue.u64; |
| 1096 | allowed_vals->u64max = opd.FORM.Range.MaximumValue.u64; |
| 1097 | allowed_vals->u64step = opd.FORM.Range.StepSize.u64; |
| 1098 | allowed_vals->datatype = LIBMTP_DATATYPE_UINT64; |
| 1099 | break; |
| 1100 | } |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 1101 | return 0; |
Richard Low | 6f07084 | 2009-05-03 10:26:16 +0000 | [diff] [blame] | 1102 | } else |
| 1103 | return -1; |
| 1104 | } |
| 1105 | |
| 1106 | /** |
| 1107 | * Destroys a LIBMTP_allowed_values_t struct |
| 1108 | * @param allowed_vals the struct to destroy |
| 1109 | */ |
| 1110 | void LIBMTP_destroy_allowed_values_t(LIBMTP_allowed_values_t *allowed_vals) |
| 1111 | { |
| 1112 | if (!allowed_vals->is_range) |
| 1113 | { |
| 1114 | switch (allowed_vals->datatype) |
| 1115 | { |
| 1116 | case LIBMTP_DATATYPE_INT8: |
| 1117 | if (allowed_vals->i8vals) |
| 1118 | free(allowed_vals->i8vals); |
| 1119 | break; |
| 1120 | case LIBMTP_DATATYPE_UINT8: |
| 1121 | if (allowed_vals->u8vals) |
| 1122 | free(allowed_vals->u8vals); |
| 1123 | break; |
| 1124 | case LIBMTP_DATATYPE_INT16: |
| 1125 | if (allowed_vals->i16vals) |
| 1126 | free(allowed_vals->i16vals); |
| 1127 | break; |
| 1128 | case LIBMTP_DATATYPE_UINT16: |
| 1129 | if (allowed_vals->u16vals) |
| 1130 | free(allowed_vals->u16vals); |
| 1131 | break; |
| 1132 | case LIBMTP_DATATYPE_INT32: |
| 1133 | if (allowed_vals->i32vals) |
| 1134 | free(allowed_vals->i32vals); |
| 1135 | break; |
| 1136 | case LIBMTP_DATATYPE_UINT32: |
| 1137 | if (allowed_vals->u32vals) |
| 1138 | free(allowed_vals->u32vals); |
| 1139 | break; |
| 1140 | case LIBMTP_DATATYPE_INT64: |
| 1141 | if (allowed_vals->i64vals) |
| 1142 | free(allowed_vals->i64vals); |
| 1143 | break; |
| 1144 | case LIBMTP_DATATYPE_UINT64: |
| 1145 | if (allowed_vals->u64vals) |
| 1146 | free(allowed_vals->u64vals); |
| 1147 | break; |
| 1148 | } |
| 1149 | } |
| 1150 | } |
| 1151 | |
| 1152 | /** |
| 1153 | * Determine if a property is supported for a given file type |
| 1154 | * @param device a pointer to an MTP device |
| 1155 | * @param property the property to query |
| 1156 | * @param filetype the filetype of the object you want to set values for |
| 1157 | * @return 0 if not supported, positive if supported, negative on error |
| 1158 | */ |
| 1159 | int LIBMTP_Is_Property_Supported(LIBMTP_mtpdevice_t *device, LIBMTP_property_t const property, |
| 1160 | LIBMTP_filetype_t const filetype) |
| 1161 | { |
| 1162 | uint16_t *props = NULL; |
| 1163 | uint32_t propcnt = 0; |
| 1164 | uint16_t ret = 0; |
| 1165 | int i = 0; |
| 1166 | int supported = 0; |
| 1167 | uint16_t ptp_prop = map_libmtp_property_to_ptp_property(property); |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 1168 | |
Lei Zhang | caa1bbc | 2013-02-13 15:12:02 -0800 | [diff] [blame] | 1169 | if (!ptp_operation_issupported(device->params, PTP_OC_MTP_GetObjectPropsSupported)) |
| 1170 | return 0; |
| 1171 | |
Richard Low | 6f07084 | 2009-05-03 10:26:16 +0000 | [diff] [blame] | 1172 | ret = ptp_mtp_getobjectpropssupported(device->params, map_libmtp_type_to_ptp_type(filetype), &propcnt, &props); |
| 1173 | if (ret != PTP_RC_OK) { |
| 1174 | add_ptp_error_to_errorstack(device, ret, "LIBMTP_Is_Property_Supported(): could not get properties supported."); |
| 1175 | return -1; |
| 1176 | } |
| 1177 | |
| 1178 | for (i = 0; i < propcnt; i++) { |
| 1179 | if (props[i] == ptp_prop) { |
| 1180 | supported = 1; |
| 1181 | break; |
| 1182 | } |
| 1183 | } |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 1184 | |
Richard Low | 6f07084 | 2009-05-03 10:26:16 +0000 | [diff] [blame] | 1185 | free(props); |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 1186 | |
Richard Low | 6f07084 | 2009-05-03 10:26:16 +0000 | [diff] [blame] | 1187 | return supported; |
| 1188 | } |
| 1189 | |
| 1190 | /** |
| 1191 | * Retrieves a string from an object |
| 1192 | * |
| 1193 | * @param device a pointer to an MTP device. |
| 1194 | * @param object_id Object reference |
| 1195 | * @param attribute_id MTP attribute ID |
| 1196 | * @return valid string or NULL on failure. The returned string |
| 1197 | * must bee <code>free()</code>:ed by the caller after |
| 1198 | * use. |
| 1199 | */ |
| 1200 | char *LIBMTP_Get_String_From_Object(LIBMTP_mtpdevice_t *device, uint32_t const object_id, |
| 1201 | LIBMTP_property_t const attribute_id) |
| 1202 | { |
| 1203 | return get_string_from_object(device, object_id, attribute_id); |
| 1204 | } |
| 1205 | |
| 1206 | /** |
| 1207 | * Retrieves an unsigned 64-bit integer from an object attribute |
| 1208 | * |
| 1209 | * @param device a pointer to an MTP device. |
| 1210 | * @param object_id Object reference |
| 1211 | * @param attribute_id MTP attribute ID |
| 1212 | * @param value_default Default value to return on failure |
| 1213 | * @return the value |
| 1214 | */ |
| 1215 | uint64_t LIBMTP_Get_u64_From_Object(LIBMTP_mtpdevice_t *device,uint32_t const object_id, |
| 1216 | LIBMTP_property_t const attribute_id, uint64_t const value_default) |
| 1217 | { |
| 1218 | return get_u64_from_object(device, object_id, map_libmtp_property_to_ptp_property(attribute_id), value_default); |
| 1219 | } |
| 1220 | |
| 1221 | /** |
| 1222 | * Retrieves an unsigned 32-bit integer from an object attribute |
| 1223 | * |
| 1224 | * @param device a pointer to an MTP device. |
| 1225 | * @param object_id Object reference |
| 1226 | * @param attribute_id MTP attribute ID |
| 1227 | * @param value_default Default value to return on failure |
| 1228 | * @return the value |
| 1229 | */ |
| 1230 | uint32_t LIBMTP_Get_u32_From_Object(LIBMTP_mtpdevice_t *device,uint32_t const object_id, |
| 1231 | LIBMTP_property_t const attribute_id, uint32_t const value_default) |
| 1232 | { |
| 1233 | return get_u32_from_object(device, object_id, map_libmtp_property_to_ptp_property(attribute_id), value_default); |
| 1234 | } |
| 1235 | |
| 1236 | /** |
| 1237 | * Retrieves an unsigned 16-bit integer from an object attribute |
| 1238 | * |
| 1239 | * @param device a pointer to an MTP device. |
| 1240 | * @param object_id Object reference |
| 1241 | * @param attribute_id MTP attribute ID |
| 1242 | * @param value_default Default value to return on failure |
| 1243 | * @return a value |
| 1244 | */ |
| 1245 | uint16_t LIBMTP_Get_u16_From_Object(LIBMTP_mtpdevice_t *device, uint32_t const object_id, |
| 1246 | LIBMTP_property_t const attribute_id, uint16_t const value_default) |
| 1247 | { |
| 1248 | return get_u16_from_object(device, object_id, map_libmtp_property_to_ptp_property(attribute_id), value_default); |
| 1249 | } |
| 1250 | |
| 1251 | /** |
| 1252 | * Retrieves an unsigned 8-bit integer from an object attribute |
| 1253 | * |
| 1254 | * @param device a pointer to an MTP device. |
| 1255 | * @param object_id Object reference |
| 1256 | * @param attribute_id MTP attribute ID |
| 1257 | * @param value_default Default value to return on failure |
| 1258 | * @return a value |
| 1259 | */ |
| 1260 | uint8_t LIBMTP_Get_u8_From_Object(LIBMTP_mtpdevice_t *device, uint32_t const object_id, |
| 1261 | LIBMTP_property_t const attribute_id, uint8_t const value_default) |
| 1262 | { |
| 1263 | return get_u8_from_object(device, object_id, map_libmtp_property_to_ptp_property(attribute_id), value_default); |
| 1264 | } |
| 1265 | |
| 1266 | /** |
| 1267 | * Sets an object attribute from a string |
| 1268 | * |
| 1269 | * @param device a pointer to an MTP device. |
| 1270 | * @param object_id Object reference |
| 1271 | * @param attribute_id MTP attribute ID |
| 1272 | * @param string string value to set |
| 1273 | * @return 0 on success, any other value means failure |
| 1274 | */ |
| 1275 | int LIBMTP_Set_Object_String(LIBMTP_mtpdevice_t *device, uint32_t const object_id, |
| 1276 | LIBMTP_property_t const attribute_id, char const * const string) |
| 1277 | { |
| 1278 | return set_object_string(device, object_id, map_libmtp_property_to_ptp_property(attribute_id), string); |
| 1279 | } |
| 1280 | |
| 1281 | |
| 1282 | /** |
| 1283 | * Sets an object attribute from an unsigned 32-bit integer |
| 1284 | * |
| 1285 | * @param device a pointer to an MTP device. |
| 1286 | * @param object_id Object reference |
| 1287 | * @param attribute_id MTP attribute ID |
| 1288 | * @param value 32-bit unsigned integer to set |
| 1289 | * @return 0 on success, any other value means failure |
| 1290 | */ |
| 1291 | int LIBMTP_Set_Object_u32(LIBMTP_mtpdevice_t *device, uint32_t const object_id, |
| 1292 | LIBMTP_property_t const attribute_id, uint32_t const value) |
| 1293 | { |
| 1294 | return set_object_u32(device, object_id, map_libmtp_property_to_ptp_property(attribute_id), value); |
| 1295 | } |
| 1296 | |
| 1297 | /** |
| 1298 | * Sets an object attribute from an unsigned 16-bit integer |
| 1299 | * |
| 1300 | * @param device a pointer to an MTP device. |
| 1301 | * @param object_id Object reference |
| 1302 | * @param attribute_id MTP attribute ID |
| 1303 | * @param value 16-bit unsigned integer to set |
| 1304 | * @return 0 on success, any other value means failure |
| 1305 | */ |
| 1306 | int LIBMTP_Set_Object_u16(LIBMTP_mtpdevice_t *device, uint32_t const object_id, |
| 1307 | LIBMTP_property_t const attribute_id, uint16_t const value) |
| 1308 | { |
| 1309 | return set_object_u16(device, object_id, map_libmtp_property_to_ptp_property(attribute_id), value); |
| 1310 | } |
| 1311 | |
| 1312 | /** |
| 1313 | * Sets an object attribute from an unsigned 8-bit integer |
| 1314 | * |
| 1315 | * @param device a pointer to an MTP device. |
| 1316 | * @param object_id Object reference |
| 1317 | * @param attribute_id MTP attribute ID |
| 1318 | * @param value 8-bit unsigned integer to set |
| 1319 | * @return 0 on success, any other value means failure |
| 1320 | */ |
| 1321 | int LIBMTP_Set_Object_u8(LIBMTP_mtpdevice_t *device, uint32_t const object_id, |
| 1322 | LIBMTP_property_t const attribute_id, uint8_t const value) |
| 1323 | { |
| 1324 | return set_object_u8(device, object_id, map_libmtp_property_to_ptp_property(attribute_id), value); |
| 1325 | } |
| 1326 | |
| 1327 | /** |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 1328 | * Retrieves a string from an object |
| 1329 | * |
| 1330 | * @param device a pointer to an MTP device. |
| 1331 | * @param object_id Object reference |
| 1332 | * @param attribute_id PTP attribute ID |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 1333 | * @return valid string or NULL on failure. The returned string |
| 1334 | * must bee <code>free()</code>:ed by the caller after |
| 1335 | * use. |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 1336 | */ |
Linus Walleij | 9901e22 | 2006-11-30 12:28:19 +0000 | [diff] [blame] | 1337 | static char *get_string_from_object(LIBMTP_mtpdevice_t *device, uint32_t const object_id, |
Linus Walleij | 4ef39e6 | 2006-09-19 14:11:52 +0000 | [diff] [blame] | 1338 | uint16_t const attribute_id) |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 1339 | { |
| 1340 | PTPPropertyValue propval; |
| 1341 | char *retstring = NULL; |
Marcus Meissner | 9d404be | 2015-03-22 14:37:54 +0100 | [diff] [blame] | 1342 | PTPParams *params; |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 1343 | uint16_t ret; |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 1344 | MTPProperties *prop; |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 1345 | |
Marcus Meissner | 9d404be | 2015-03-22 14:37:54 +0100 | [diff] [blame] | 1346 | if (!device || !object_id) |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 1347 | return NULL; |
Marcus Meissner | 9d404be | 2015-03-22 14:37:54 +0100 | [diff] [blame] | 1348 | |
| 1349 | params = (PTPParams *) device->params; |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 1350 | |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 1351 | prop = ptp_find_object_prop_in_cache(params, object_id, attribute_id); |
| 1352 | if (prop) { |
| 1353 | if (prop->propval.str != NULL) |
| 1354 | return strdup(prop->propval.str); |
| 1355 | else |
| 1356 | return NULL; |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 1357 | } |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 1358 | |
Linus Walleij | a823a70 | 2006-08-27 21:27:46 +0000 | [diff] [blame] | 1359 | ret = ptp_mtp_getobjectpropvalue(params, object_id, attribute_id, &propval, PTP_DTC_STR); |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 1360 | if (ret == PTP_RC_OK) { |
Linus Walleij | a823a70 | 2006-08-27 21:27:46 +0000 | [diff] [blame] | 1361 | if (propval.str != NULL) { |
| 1362 | retstring = (char *) strdup(propval.str); |
| 1363 | free(propval.str); |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 1364 | } |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 1365 | } else { |
| 1366 | add_ptp_error_to_errorstack(device, ret, "get_string_from_object(): could not get object string."); |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 1367 | } |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 1368 | |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 1369 | return retstring; |
| 1370 | } |
| 1371 | |
| 1372 | /** |
Richard Low | c3f5fc3 | 2007-07-29 09:40:50 +0000 | [diff] [blame] | 1373 | * Retrieves an unsigned 64-bit integer from an object attribute |
| 1374 | * |
| 1375 | * @param device a pointer to an MTP device. |
| 1376 | * @param object_id Object reference |
| 1377 | * @param attribute_id PTP attribute ID |
| 1378 | * @param value_default Default value to return on failure |
| 1379 | * @return the value |
| 1380 | */ |
| 1381 | static uint64_t get_u64_from_object(LIBMTP_mtpdevice_t *device,uint32_t const object_id, |
| 1382 | uint16_t const attribute_id, uint64_t const value_default) |
| 1383 | { |
| 1384 | PTPPropertyValue propval; |
| 1385 | uint64_t retval = value_default; |
Marcus Meissner | 9d404be | 2015-03-22 14:37:54 +0100 | [diff] [blame] | 1386 | PTPParams *params; |
Richard Low | c3f5fc3 | 2007-07-29 09:40:50 +0000 | [diff] [blame] | 1387 | uint16_t ret; |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 1388 | MTPProperties *prop; |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 1389 | |
Marcus Meissner | 9d404be | 2015-03-22 14:37:54 +0100 | [diff] [blame] | 1390 | if (!device) |
Richard Low | c3f5fc3 | 2007-07-29 09:40:50 +0000 | [diff] [blame] | 1391 | return value_default; |
Marcus Meissner | 9d404be | 2015-03-22 14:37:54 +0100 | [diff] [blame] | 1392 | |
| 1393 | params = (PTPParams *) device->params; |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 1394 | |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 1395 | prop = ptp_find_object_prop_in_cache(params, object_id, attribute_id); |
| 1396 | if (prop) |
| 1397 | return prop->propval.u64; |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 1398 | |
Richard Low | c3f5fc3 | 2007-07-29 09:40:50 +0000 | [diff] [blame] | 1399 | ret = ptp_mtp_getobjectpropvalue(params, object_id, |
| 1400 | attribute_id, |
| 1401 | &propval, |
| 1402 | PTP_DTC_UINT64); |
| 1403 | if (ret == PTP_RC_OK) { |
| 1404 | retval = propval.u64; |
| 1405 | } else { |
| 1406 | add_ptp_error_to_errorstack(device, ret, "get_u64_from_object(): could not get unsigned 64bit integer from object."); |
| 1407 | } |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 1408 | |
Richard Low | c3f5fc3 | 2007-07-29 09:40:50 +0000 | [diff] [blame] | 1409 | return retval; |
| 1410 | } |
| 1411 | |
| 1412 | /** |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 1413 | * Retrieves an unsigned 32-bit integer from an object attribute |
| 1414 | * |
| 1415 | * @param device a pointer to an MTP device. |
| 1416 | * @param object_id Object reference |
| 1417 | * @param attribute_id PTP attribute ID |
Linus Walleij | 5acfa74 | 2006-05-29 14:51:59 +0000 | [diff] [blame] | 1418 | * @param value_default Default value to return on failure |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 1419 | * @return the value |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 1420 | */ |
Linus Walleij | 9901e22 | 2006-11-30 12:28:19 +0000 | [diff] [blame] | 1421 | static uint32_t get_u32_from_object(LIBMTP_mtpdevice_t *device,uint32_t const object_id, |
Linus Walleij | 4ef39e6 | 2006-09-19 14:11:52 +0000 | [diff] [blame] | 1422 | uint16_t const attribute_id, uint32_t const value_default) |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 1423 | { |
| 1424 | PTPPropertyValue propval; |
| 1425 | uint32_t retval = value_default; |
Marcus Meissner | 9d404be | 2015-03-22 14:37:54 +0100 | [diff] [blame] | 1426 | PTPParams *params; |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 1427 | uint16_t ret; |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 1428 | MTPProperties *prop; |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 1429 | |
Marcus Meissner | 9d404be | 2015-03-22 14:37:54 +0100 | [diff] [blame] | 1430 | if (!device) |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 1431 | return value_default; |
Marcus Meissner | 9d404be | 2015-03-22 14:37:54 +0100 | [diff] [blame] | 1432 | |
| 1433 | params = (PTPParams *) device->params; |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 1434 | |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 1435 | prop = ptp_find_object_prop_in_cache(params, object_id, attribute_id); |
| 1436 | if (prop) |
| 1437 | return prop->propval.u32; |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 1438 | |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 1439 | ret = ptp_mtp_getobjectpropvalue(params, object_id, |
| 1440 | attribute_id, |
| 1441 | &propval, |
| 1442 | PTP_DTC_UINT32); |
| 1443 | if (ret == PTP_RC_OK) { |
| 1444 | retval = propval.u32; |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 1445 | } else { |
| 1446 | add_ptp_error_to_errorstack(device, ret, "get_u32_from_object(): could not get unsigned 32bit integer from object."); |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 1447 | } |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 1448 | return retval; |
| 1449 | } |
| 1450 | |
| 1451 | /** |
| 1452 | * Retrieves an unsigned 16-bit integer from an object attribute |
| 1453 | * |
| 1454 | * @param device a pointer to an MTP device. |
| 1455 | * @param object_id Object reference |
| 1456 | * @param attribute_id PTP attribute ID |
Linus Walleij | 5acfa74 | 2006-05-29 14:51:59 +0000 | [diff] [blame] | 1457 | * @param value_default Default value to return on failure |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 1458 | * @return a value |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 1459 | */ |
Linus Walleij | 9901e22 | 2006-11-30 12:28:19 +0000 | [diff] [blame] | 1460 | static uint16_t get_u16_from_object(LIBMTP_mtpdevice_t *device, uint32_t const object_id, |
Linus Walleij | 4ef39e6 | 2006-09-19 14:11:52 +0000 | [diff] [blame] | 1461 | uint16_t const attribute_id, uint16_t const value_default) |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 1462 | { |
| 1463 | PTPPropertyValue propval; |
| 1464 | uint16_t retval = value_default; |
Marcus Meissner | 9d404be | 2015-03-22 14:37:54 +0100 | [diff] [blame] | 1465 | PTPParams *params; |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 1466 | uint16_t ret; |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 1467 | MTPProperties *prop; |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 1468 | |
Marcus Meissner | 9d404be | 2015-03-22 14:37:54 +0100 | [diff] [blame] | 1469 | if (!device) |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 1470 | return value_default; |
Marcus Meissner | 9d404be | 2015-03-22 14:37:54 +0100 | [diff] [blame] | 1471 | |
| 1472 | params = (PTPParams *) device->params; |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 1473 | |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 1474 | // This O(n) search should not be used so often, since code |
| 1475 | // using the cached properties don't usually call this function. |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 1476 | prop = ptp_find_object_prop_in_cache(params, object_id, attribute_id); |
| 1477 | if (prop) |
| 1478 | return prop->propval.u16; |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 1479 | |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 1480 | ret = ptp_mtp_getobjectpropvalue(params, object_id, |
| 1481 | attribute_id, |
| 1482 | &propval, |
| 1483 | PTP_DTC_UINT16); |
| 1484 | if (ret == PTP_RC_OK) { |
| 1485 | retval = propval.u16; |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 1486 | } else { |
| 1487 | add_ptp_error_to_errorstack(device, ret, "get_u16_from_object(): could not get unsigned 16bit integer from object."); |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 1488 | } |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 1489 | |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 1490 | return retval; |
| 1491 | } |
| 1492 | |
| 1493 | /** |
Linus Walleij | 99310d4 | 2006-11-01 08:29:39 +0000 | [diff] [blame] | 1494 | * Retrieves an unsigned 8-bit integer from an object attribute |
| 1495 | * |
| 1496 | * @param device a pointer to an MTP device. |
| 1497 | * @param object_id Object reference |
| 1498 | * @param attribute_id PTP attribute ID |
| 1499 | * @param value_default Default value to return on failure |
| 1500 | * @return a value |
| 1501 | */ |
Linus Walleij | 9901e22 | 2006-11-30 12:28:19 +0000 | [diff] [blame] | 1502 | static uint8_t get_u8_from_object(LIBMTP_mtpdevice_t *device, uint32_t const object_id, |
Linus Walleij | 99310d4 | 2006-11-01 08:29:39 +0000 | [diff] [blame] | 1503 | uint16_t const attribute_id, uint8_t const value_default) |
| 1504 | { |
| 1505 | PTPPropertyValue propval; |
| 1506 | uint8_t retval = value_default; |
Marcus Meissner | 9d404be | 2015-03-22 14:37:54 +0100 | [diff] [blame] | 1507 | PTPParams *params; |
Linus Walleij | 99310d4 | 2006-11-01 08:29:39 +0000 | [diff] [blame] | 1508 | uint16_t ret; |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 1509 | MTPProperties *prop; |
Linus Walleij | 99310d4 | 2006-11-01 08:29:39 +0000 | [diff] [blame] | 1510 | |
Marcus Meissner | 9d404be | 2015-03-22 14:37:54 +0100 | [diff] [blame] | 1511 | if (!device) |
Linus Walleij | 99310d4 | 2006-11-01 08:29:39 +0000 | [diff] [blame] | 1512 | return value_default; |
Marcus Meissner | 9d404be | 2015-03-22 14:37:54 +0100 | [diff] [blame] | 1513 | |
| 1514 | params = (PTPParams *) device->params; |
Linus Walleij | 99310d4 | 2006-11-01 08:29:39 +0000 | [diff] [blame] | 1515 | |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 1516 | // This O(n) search should not be used so often, since code |
| 1517 | // using the cached properties don't usually call this function. |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 1518 | prop = ptp_find_object_prop_in_cache(params, object_id, attribute_id); |
| 1519 | if (prop) |
| 1520 | return prop->propval.u8; |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 1521 | |
Linus Walleij | 99310d4 | 2006-11-01 08:29:39 +0000 | [diff] [blame] | 1522 | ret = ptp_mtp_getobjectpropvalue(params, object_id, |
| 1523 | attribute_id, |
| 1524 | &propval, |
| 1525 | PTP_DTC_UINT8); |
| 1526 | if (ret == PTP_RC_OK) { |
| 1527 | retval = propval.u8; |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 1528 | } else { |
| 1529 | add_ptp_error_to_errorstack(device, ret, "get_u8_from_object(): could not get unsigned 8bit integer from object."); |
Linus Walleij | 99310d4 | 2006-11-01 08:29:39 +0000 | [diff] [blame] | 1530 | } |
| 1531 | |
| 1532 | return retval; |
| 1533 | } |
| 1534 | |
| 1535 | /** |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 1536 | * Sets an object attribute from a string |
| 1537 | * |
| 1538 | * @param device a pointer to an MTP device. |
| 1539 | * @param object_id Object reference |
| 1540 | * @param attribute_id PTP attribute ID |
| 1541 | * @param string string value to set |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 1542 | * @return 0 on success, any other value means failure |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 1543 | */ |
Linus Walleij | 9901e22 | 2006-11-30 12:28:19 +0000 | [diff] [blame] | 1544 | static int set_object_string(LIBMTP_mtpdevice_t *device, uint32_t const object_id, |
Linus Walleij | 4ef39e6 | 2006-09-19 14:11:52 +0000 | [diff] [blame] | 1545 | uint16_t const attribute_id, char const * const string) |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 1546 | { |
| 1547 | PTPPropertyValue propval; |
Marcus Meissner | 9d404be | 2015-03-22 14:37:54 +0100 | [diff] [blame] | 1548 | PTPParams *params; |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 1549 | uint16_t ret; |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 1550 | |
Marcus Meissner | 9d404be | 2015-03-22 14:37:54 +0100 | [diff] [blame] | 1551 | if (!device || !string) |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 1552 | return -1; |
Marcus Meissner | 9d404be | 2015-03-22 14:37:54 +0100 | [diff] [blame] | 1553 | |
| 1554 | params = (PTPParams *) device->params; |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 1555 | |
Linus Walleij | b275318 | 2007-02-26 08:11:38 +0000 | [diff] [blame] | 1556 | if (!ptp_operation_issupported(params,PTP_OC_MTP_SetObjectPropValue)) { |
| 1557 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "set_object_string(): could not set object string: " |
| 1558 | "PTP_OC_MTP_SetObjectPropValue not supported."); |
| 1559 | return -1; |
| 1560 | } |
Linus Walleij | a823a70 | 2006-08-27 21:27:46 +0000 | [diff] [blame] | 1561 | propval.str = (char *) string; |
| 1562 | ret = ptp_mtp_setobjectpropvalue(params, object_id, attribute_id, &propval, PTP_DTC_STR); |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 1563 | if (ret != PTP_RC_OK) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 1564 | add_ptp_error_to_errorstack(device, ret, "set_object_string(): could not set object string."); |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 1565 | return -1; |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 1566 | } |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 1567 | |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 1568 | return 0; |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 1569 | } |
| 1570 | |
Linus Walleij | 2955956 | 2007-08-22 21:38:04 +0000 | [diff] [blame] | 1571 | |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 1572 | /** |
| 1573 | * Sets an object attribute from an unsigned 32-bit integer |
| 1574 | * |
| 1575 | * @param device a pointer to an MTP device. |
| 1576 | * @param object_id Object reference |
| 1577 | * @param attribute_id PTP attribute ID |
| 1578 | * @param value 32-bit unsigned integer to set |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 1579 | * @return 0 on success, any other value means failure |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 1580 | */ |
Linus Walleij | 9901e22 | 2006-11-30 12:28:19 +0000 | [diff] [blame] | 1581 | static int set_object_u32(LIBMTP_mtpdevice_t *device, uint32_t const object_id, |
Linus Walleij | 4ef39e6 | 2006-09-19 14:11:52 +0000 | [diff] [blame] | 1582 | uint16_t const attribute_id, uint32_t const value) |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 1583 | { |
| 1584 | PTPPropertyValue propval; |
Marcus Meissner | 9d404be | 2015-03-22 14:37:54 +0100 | [diff] [blame] | 1585 | PTPParams *params; |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 1586 | uint16_t ret; |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 1587 | |
Marcus Meissner | 9d404be | 2015-03-22 14:37:54 +0100 | [diff] [blame] | 1588 | if (!device) |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 1589 | return -1; |
Marcus Meissner | 9d404be | 2015-03-22 14:37:54 +0100 | [diff] [blame] | 1590 | |
| 1591 | params = (PTPParams *) device->params; |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 1592 | |
Linus Walleij | b275318 | 2007-02-26 08:11:38 +0000 | [diff] [blame] | 1593 | if (!ptp_operation_issupported(params,PTP_OC_MTP_SetObjectPropValue)) { |
| 1594 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "set_object_u32(): could not set unsigned 32bit integer property: " |
| 1595 | "PTP_OC_MTP_SetObjectPropValue not supported."); |
| 1596 | return -1; |
| 1597 | } |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 1598 | |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 1599 | propval.u32 = value; |
| 1600 | ret = ptp_mtp_setobjectpropvalue(params, object_id, attribute_id, &propval, PTP_DTC_UINT32); |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 1601 | if (ret != PTP_RC_OK) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 1602 | add_ptp_error_to_errorstack(device, ret, "set_object_u32(): could not set unsigned 32bit integer property."); |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 1603 | return -1; |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 1604 | } |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 1605 | |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 1606 | return 0; |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 1607 | } |
| 1608 | |
| 1609 | /** |
| 1610 | * Sets an object attribute from an unsigned 16-bit integer |
| 1611 | * |
| 1612 | * @param device a pointer to an MTP device. |
| 1613 | * @param object_id Object reference |
| 1614 | * @param attribute_id PTP attribute ID |
| 1615 | * @param value 16-bit unsigned integer to set |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 1616 | * @return 0 on success, any other value means failure |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 1617 | */ |
Linus Walleij | 9901e22 | 2006-11-30 12:28:19 +0000 | [diff] [blame] | 1618 | static int set_object_u16(LIBMTP_mtpdevice_t *device, uint32_t const object_id, |
Linus Walleij | 4ef39e6 | 2006-09-19 14:11:52 +0000 | [diff] [blame] | 1619 | uint16_t const attribute_id, uint16_t const value) |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 1620 | { |
| 1621 | PTPPropertyValue propval; |
Marcus Meissner | 9d404be | 2015-03-22 14:37:54 +0100 | [diff] [blame] | 1622 | PTPParams *params; |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 1623 | uint16_t ret; |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 1624 | |
Marcus Meissner | 9d404be | 2015-03-22 14:37:54 +0100 | [diff] [blame] | 1625 | if (!device) |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 1626 | return 1; |
Marcus Meissner | 9d404be | 2015-03-22 14:37:54 +0100 | [diff] [blame] | 1627 | |
| 1628 | params = (PTPParams *) device->params; |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 1629 | |
Linus Walleij | b275318 | 2007-02-26 08:11:38 +0000 | [diff] [blame] | 1630 | if (!ptp_operation_issupported(params,PTP_OC_MTP_SetObjectPropValue)) { |
| 1631 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "set_object_u16(): could not set unsigned 16bit integer property: " |
| 1632 | "PTP_OC_MTP_SetObjectPropValue not supported."); |
| 1633 | return -1; |
| 1634 | } |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 1635 | propval.u16 = value; |
| 1636 | ret = ptp_mtp_setobjectpropvalue(params, object_id, attribute_id, &propval, PTP_DTC_UINT16); |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 1637 | if (ret != PTP_RC_OK) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 1638 | add_ptp_error_to_errorstack(device, ret, "set_object_u16(): could not set unsigned 16bit integer property."); |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 1639 | return 1; |
| 1640 | } |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 1641 | |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 1642 | return 0; |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 1643 | } |
| 1644 | |
| 1645 | /** |
Linus Walleij | 99310d4 | 2006-11-01 08:29:39 +0000 | [diff] [blame] | 1646 | * Sets an object attribute from an unsigned 8-bit integer |
| 1647 | * |
| 1648 | * @param device a pointer to an MTP device. |
| 1649 | * @param object_id Object reference |
| 1650 | * @param attribute_id PTP attribute ID |
| 1651 | * @param value 8-bit unsigned integer to set |
| 1652 | * @return 0 on success, any other value means failure |
| 1653 | */ |
Linus Walleij | 9901e22 | 2006-11-30 12:28:19 +0000 | [diff] [blame] | 1654 | static int set_object_u8(LIBMTP_mtpdevice_t *device, uint32_t const object_id, |
| 1655 | uint16_t const attribute_id, uint8_t const value) |
Linus Walleij | 99310d4 | 2006-11-01 08:29:39 +0000 | [diff] [blame] | 1656 | { |
| 1657 | PTPPropertyValue propval; |
Marcus Meissner | 9d404be | 2015-03-22 14:37:54 +0100 | [diff] [blame] | 1658 | PTPParams *params; |
Linus Walleij | 99310d4 | 2006-11-01 08:29:39 +0000 | [diff] [blame] | 1659 | uint16_t ret; |
| 1660 | |
Marcus Meissner | 9d404be | 2015-03-22 14:37:54 +0100 | [diff] [blame] | 1661 | if (!device) |
Linus Walleij | 99310d4 | 2006-11-01 08:29:39 +0000 | [diff] [blame] | 1662 | return 1; |
Marcus Meissner | 9d404be | 2015-03-22 14:37:54 +0100 | [diff] [blame] | 1663 | |
| 1664 | params = (PTPParams *) device->params; |
Linus Walleij | 99310d4 | 2006-11-01 08:29:39 +0000 | [diff] [blame] | 1665 | |
Linus Walleij | b275318 | 2007-02-26 08:11:38 +0000 | [diff] [blame] | 1666 | if (!ptp_operation_issupported(params,PTP_OC_MTP_SetObjectPropValue)) { |
| 1667 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "set_object_u8(): could not set unsigned 8bit integer property: " |
| 1668 | "PTP_OC_MTP_SetObjectPropValue not supported."); |
| 1669 | return -1; |
| 1670 | } |
Linus Walleij | 99310d4 | 2006-11-01 08:29:39 +0000 | [diff] [blame] | 1671 | propval.u8 = value; |
| 1672 | ret = ptp_mtp_setobjectpropvalue(params, object_id, attribute_id, &propval, PTP_DTC_UINT8); |
| 1673 | if (ret != PTP_RC_OK) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 1674 | add_ptp_error_to_errorstack(device, ret, "set_object_u8(): could not set unsigned 8bit integer property."); |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 1675 | return 1; |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 1676 | } |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 1677 | |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 1678 | return 0; |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 1679 | } |
| 1680 | |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 1681 | /** |
Linus Walleij | 039d1dd | 2007-02-23 22:34:07 +0000 | [diff] [blame] | 1682 | * Get the first (as in "first in the list of") connected MTP device. |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 1683 | * @return a device pointer. |
Linus Walleij | 039d1dd | 2007-02-23 22:34:07 +0000 | [diff] [blame] | 1684 | * @see LIBMTP_Get_Connected_Devices() |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 1685 | */ |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 1686 | LIBMTP_mtpdevice_t *LIBMTP_Get_First_Device(void) |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 1687 | { |
tedbullock | e771364 | 2007-02-18 23:10:09 +0000 | [diff] [blame] | 1688 | LIBMTP_mtpdevice_t *first_device = NULL; |
Linus Walleij | 64691b7 | 2008-05-31 22:55:18 +0000 | [diff] [blame] | 1689 | LIBMTP_raw_device_t *devices; |
| 1690 | int numdevs; |
| 1691 | LIBMTP_error_number_t ret; |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 1692 | |
Linus Walleij | 64691b7 | 2008-05-31 22:55:18 +0000 | [diff] [blame] | 1693 | ret = LIBMTP_Detect_Raw_Devices(&devices, &numdevs); |
| 1694 | if (ret != LIBMTP_ERROR_NONE) { |
Richard Low | 0f79476 | 2007-02-23 22:06:27 +0000 | [diff] [blame] | 1695 | return NULL; |
Linus Walleij | a8a19cc | 2007-02-02 22:13:17 +0000 | [diff] [blame] | 1696 | } |
tedbullock | 44b0ff7 | 2007-02-22 22:20:28 +0000 | [diff] [blame] | 1697 | |
Linus Walleij | 64691b7 | 2008-05-31 22:55:18 +0000 | [diff] [blame] | 1698 | if (devices == NULL || numdevs == 0) { |
Marcus Meissner | b521477 | 2015-10-04 13:08:20 +0200 | [diff] [blame] | 1699 | free(devices); |
Linus Walleij | 64691b7 | 2008-05-31 22:55:18 +0000 | [diff] [blame] | 1700 | return NULL; |
tedbullock | 44b0ff7 | 2007-02-22 22:20:28 +0000 | [diff] [blame] | 1701 | } |
Linus Walleij | 64691b7 | 2008-05-31 22:55:18 +0000 | [diff] [blame] | 1702 | |
| 1703 | first_device = LIBMTP_Open_Raw_Device(&devices[0]); |
| 1704 | free(devices); |
tedbullock | e771364 | 2007-02-18 23:10:09 +0000 | [diff] [blame] | 1705 | return first_device; |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 1706 | } |
| 1707 | |
| 1708 | /** |
Linus Walleij | d1e14e0 | 2008-12-14 01:07:30 +0000 | [diff] [blame] | 1709 | * Overriding debug function. |
| 1710 | * This way we can disable debug prints. |
| 1711 | */ |
| 1712 | static void |
| 1713 | #ifdef __GNUC__ |
| 1714 | __attribute__((__format__(printf,2,0))) |
| 1715 | #endif |
| 1716 | LIBMTP_ptp_debug(void *data, const char *format, va_list args) |
| 1717 | { |
nicklas79 | 0358408 | 2009-09-28 18:20:16 +0000 | [diff] [blame] | 1718 | if ((LIBMTP_debug & LIBMTP_DEBUG_PTP) != 0) { |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 1719 | vfprintf (stderr, format, args); |
Catalin Patulea | da25de0 | 2012-07-19 00:57:42 -0400 | [diff] [blame] | 1720 | fprintf (stderr, "\n"); |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 1721 | fflush (stderr); |
| 1722 | } |
Linus Walleij | d1e14e0 | 2008-12-14 01:07:30 +0000 | [diff] [blame] | 1723 | } |
| 1724 | |
| 1725 | /** |
| 1726 | * Overriding error function. |
| 1727 | * This way we can capture all error etc to our errorstack. |
| 1728 | */ |
| 1729 | static void |
| 1730 | #ifdef __GNUC__ |
| 1731 | __attribute__((__format__(printf,2,0))) |
| 1732 | #endif |
| 1733 | LIBMTP_ptp_error(void *data, const char *format, va_list args) |
| 1734 | { |
| 1735 | // if (data == NULL) { |
| 1736 | vfprintf (stderr, format, args); |
| 1737 | fflush (stderr); |
| 1738 | /* |
| 1739 | FIXME: find out how we shall get the device here. |
| 1740 | } else { |
| 1741 | PTP_USB *ptp_usb = data; |
| 1742 | LIBMTP_mtpdevice_t *device = ...; |
| 1743 | char buf[2048]; |
| 1744 | |
| 1745 | vsnprintf (buf, sizeof (buf), format, args); |
| 1746 | add_error_to_errorstack(device, |
| 1747 | LIBMTP_ERROR_PTP_LAYER, |
| 1748 | buf); |
| 1749 | } |
| 1750 | */ |
| 1751 | } |
| 1752 | |
| 1753 | /** |
Linus Walleij | 38d7ee8 | 2010-07-24 22:17:21 +0000 | [diff] [blame] | 1754 | * Parses the extension descriptor, there may be stuff in |
| 1755 | * this that we want to know about. |
| 1756 | */ |
| 1757 | static void parse_extension_descriptor(LIBMTP_mtpdevice_t *mtpdevice, |
| 1758 | char *desc) |
| 1759 | { |
| 1760 | int start = 0; |
| 1761 | int end = 0; |
| 1762 | |
Lei Zhang | 766bcb8 | 2013-02-13 15:12:30 -0800 | [diff] [blame] | 1763 | /* NULL on Canon A70 */ |
| 1764 | if (!desc) |
| 1765 | return; |
| 1766 | |
Linus Walleij | 38d7ee8 | 2010-07-24 22:17:21 +0000 | [diff] [blame] | 1767 | /* descriptors are divided by semicolons */ |
| 1768 | while (end < strlen(desc)) { |
Linus Walleij | 03f0c03 | 2011-10-22 00:01:14 +0200 | [diff] [blame] | 1769 | /* Skip past initial whitespace */ |
Marcus Meissner | 1f1d865 | 2015-04-07 23:30:53 +0200 | [diff] [blame] | 1770 | while ((end < strlen(desc)) && (desc[start] == ' ' )) { |
Linus Walleij | 03f0c03 | 2011-10-22 00:01:14 +0200 | [diff] [blame] | 1771 | start++; |
| 1772 | end++; |
| 1773 | } |
| 1774 | /* Detect extension */ |
Marcus Meissner | 1f1d865 | 2015-04-07 23:30:53 +0200 | [diff] [blame] | 1775 | while ((end < strlen(desc)) && (desc[end] != ';')) |
Linus Walleij | 38d7ee8 | 2010-07-24 22:17:21 +0000 | [diff] [blame] | 1776 | end++; |
| 1777 | if (end < strlen(desc)) { |
| 1778 | char *element = strndup(desc + start, end-start); |
| 1779 | if (element) { |
| 1780 | int i = 0; |
| 1781 | // printf(" Element: \"%s\"\n", element); |
| 1782 | |
| 1783 | /* Parse for an extension */ |
Marcus Meissner | 1f1d865 | 2015-04-07 23:30:53 +0200 | [diff] [blame] | 1784 | while ((i < strlen(element)) && (element[i] != ':')) |
Linus Walleij | 38d7ee8 | 2010-07-24 22:17:21 +0000 | [diff] [blame] | 1785 | i++; |
| 1786 | if (i < strlen(element)) { |
| 1787 | char *name = strndup(element, i); |
Marcus Meissner | f93179d | 2016-10-24 22:42:38 +0200 | [diff] [blame] | 1788 | int major = 0, minor = 0; |
Linus Walleij | 38d7ee8 | 2010-07-24 22:17:21 +0000 | [diff] [blame] | 1789 | |
Marcus Meissner | f93179d | 2016-10-24 22:42:38 +0200 | [diff] [blame] | 1790 | /* extension versions have to be MAJOR.MINOR, but Samsung has one |
| 1791 | * with just 0, so just cope with those cases too */ |
| 1792 | if ( (2 == sscanf(element+i+1,"%d.%d",&major,&minor)) || |
| 1793 | (1 == sscanf(element+i+1,"%d",&major)) |
| 1794 | ) { |
Linus Walleij | 38d7ee8 | 2010-07-24 22:17:21 +0000 | [diff] [blame] | 1795 | LIBMTP_device_extension_t *extension; |
Marcus Meissner | f93179d | 2016-10-24 22:42:38 +0200 | [diff] [blame] | 1796 | |
Linus Walleij | 38d7ee8 | 2010-07-24 22:17:21 +0000 | [diff] [blame] | 1797 | extension = malloc(sizeof(LIBMTP_device_extension_t)); |
| 1798 | extension->name = name; |
| 1799 | extension->major = major; |
| 1800 | extension->minor = minor; |
| 1801 | extension->next = NULL; |
| 1802 | if (mtpdevice->extensions == NULL) { |
| 1803 | mtpdevice->extensions = extension; |
| 1804 | } else { |
| 1805 | LIBMTP_device_extension_t *tmp = mtpdevice->extensions; |
| 1806 | while (tmp->next != NULL) |
| 1807 | tmp = tmp->next; |
| 1808 | tmp->next = extension; |
| 1809 | } |
Linus Walleij | 38d7ee8 | 2010-07-24 22:17:21 +0000 | [diff] [blame] | 1810 | } else { |
| 1811 | LIBMTP_ERROR("LIBMTP ERROR: couldnt parse extension %s\n", |
| 1812 | element); |
| 1813 | } |
| 1814 | } |
| 1815 | free(element); |
| 1816 | } |
| 1817 | } |
| 1818 | end++; |
| 1819 | start = end; |
| 1820 | } |
| 1821 | } |
| 1822 | |
| 1823 | /** |
Linus Walleij | bdb89bd | 2008-05-28 23:32:35 +0000 | [diff] [blame] | 1824 | * This function opens a device from a raw device. It is the |
| 1825 | * preferred way to access devices in the new interface where |
| 1826 | * several devices can come and go as the library is working |
| 1827 | * on a certain device. |
| 1828 | * @param rawdevice the raw device to open a "real" device for. |
| 1829 | * @return an open device. |
| 1830 | */ |
Linus Walleij | a8b8889 | 2011-03-03 19:58:26 +0100 | [diff] [blame] | 1831 | LIBMTP_mtpdevice_t *LIBMTP_Open_Raw_Device_Uncached(LIBMTP_raw_device_t *rawdevice) |
Linus Walleij | bdb89bd | 2008-05-28 23:32:35 +0000 | [diff] [blame] | 1832 | { |
| 1833 | LIBMTP_mtpdevice_t *mtp_device; |
| 1834 | uint8_t bs = 0; |
| 1835 | PTPParams *current_params; |
Linus Walleij | 3c64325 | 2008-05-31 23:22:28 +0000 | [diff] [blame] | 1836 | PTP_USB *ptp_usb; |
Linus Walleij | bdb89bd | 2008-05-28 23:32:35 +0000 | [diff] [blame] | 1837 | LIBMTP_error_number_t err; |
| 1838 | int i; |
| 1839 | |
| 1840 | /* Allocate dynamic space for our device */ |
| 1841 | mtp_device = (LIBMTP_mtpdevice_t *) malloc(sizeof(LIBMTP_mtpdevice_t)); |
| 1842 | /* Check if there was a memory allocation error */ |
| 1843 | if(mtp_device == NULL) { |
| 1844 | /* There has been an memory allocation error. We are going to ignore this |
| 1845 | device and attempt to continue */ |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 1846 | |
Linus Walleij | bdb89bd | 2008-05-28 23:32:35 +0000 | [diff] [blame] | 1847 | /* TODO: This error statement could probably be a bit more robust */ |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 1848 | LIBMTP_ERROR("LIBMTP PANIC: connect_usb_devices encountered a memory " |
Linus Walleij | bdb89bd | 2008-05-28 23:32:35 +0000 | [diff] [blame] | 1849 | "allocation error with device %d on bus %d, trying to continue", |
| 1850 | rawdevice->devnum, rawdevice->bus_location); |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 1851 | |
Linus Walleij | bdb89bd | 2008-05-28 23:32:35 +0000 | [diff] [blame] | 1852 | return NULL; |
| 1853 | } |
Marcus Meissner | 097a052 | 2015-03-22 14:30:36 +0100 | [diff] [blame] | 1854 | memset(mtp_device, 0, sizeof(LIBMTP_mtpdevice_t)); |
Linus Walleij | a8b8889 | 2011-03-03 19:58:26 +0100 | [diff] [blame] | 1855 | // Non-cached by default |
| 1856 | mtp_device->cached = 0; |
Linus Walleij | 3c64325 | 2008-05-31 23:22:28 +0000 | [diff] [blame] | 1857 | |
| 1858 | /* Create PTP params */ |
| 1859 | current_params = (PTPParams *) malloc(sizeof(PTPParams)); |
| 1860 | if (current_params == NULL) { |
| 1861 | free(mtp_device); |
Linus Walleij | bdb89bd | 2008-05-28 23:32:35 +0000 | [diff] [blame] | 1862 | return NULL; |
Linus Walleij | 3c64325 | 2008-05-31 23:22:28 +0000 | [diff] [blame] | 1863 | } |
| 1864 | memset(current_params, 0, sizeof(PTPParams)); |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 1865 | current_params->device_flags = rawdevice->device_entry.device_flags; |
| 1866 | current_params->nrofobjects = 0; |
Marcus Meissner | aa7d91a | 2017-03-16 15:59:48 +0100 | [diff] [blame] | 1867 | current_params->cachetime = 2; |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 1868 | current_params->objects = NULL; |
| 1869 | current_params->response_packet_size = 0; |
| 1870 | current_params->response_packet = NULL; |
Linus Walleij | d1e14e0 | 2008-12-14 01:07:30 +0000 | [diff] [blame] | 1871 | /* This will be a pointer to PTP_USB later */ |
| 1872 | current_params->data = NULL; |
| 1873 | /* Set upp local debug and error functions */ |
| 1874 | current_params->debug_func = LIBMTP_ptp_debug; |
| 1875 | current_params->error_func = LIBMTP_ptp_error; |
Linus Walleij | 3c64325 | 2008-05-31 23:22:28 +0000 | [diff] [blame] | 1876 | /* TODO: Will this always be little endian? */ |
| 1877 | current_params->byteorder = PTP_DL_LE; |
| 1878 | current_params->cd_locale_to_ucs2 = iconv_open("UCS-2LE", "UTF-8"); |
| 1879 | current_params->cd_ucs2_to_locale = iconv_open("UTF-8", "UCS-2LE"); |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 1880 | |
Linus Walleij | 3c64325 | 2008-05-31 23:22:28 +0000 | [diff] [blame] | 1881 | if(current_params->cd_locale_to_ucs2 == (iconv_t) -1 || |
| 1882 | current_params->cd_ucs2_to_locale == (iconv_t) -1) { |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 1883 | LIBMTP_ERROR("LIBMTP PANIC: Cannot open iconv() converters to/from UCS-2!\n" |
Linus Walleij | 3c64325 | 2008-05-31 23:22:28 +0000 | [diff] [blame] | 1884 | "Too old stdlibc, glibc and libiconv?\n"); |
| 1885 | free(current_params); |
| 1886 | free(mtp_device); |
| 1887 | return NULL; |
| 1888 | } |
| 1889 | mtp_device->params = current_params; |
| 1890 | |
Linus Walleij | 3c64325 | 2008-05-31 23:22:28 +0000 | [diff] [blame] | 1891 | /* Create usbinfo, this also opens the session */ |
| 1892 | err = configure_usb_device(rawdevice, |
| 1893 | current_params, |
| 1894 | &mtp_device->usbinfo); |
| 1895 | if (err != LIBMTP_ERROR_NONE) { |
| 1896 | free(current_params); |
| 1897 | free(mtp_device); |
| 1898 | return NULL; |
| 1899 | } |
| 1900 | ptp_usb = (PTP_USB*) mtp_device->usbinfo; |
| 1901 | /* Set pointer back to params */ |
| 1902 | ptp_usb->params = current_params; |
| 1903 | |
Linus Walleij | bdb89bd | 2008-05-28 23:32:35 +0000 | [diff] [blame] | 1904 | /* Cache the device information for later use */ |
| 1905 | if (ptp_getdeviceinfo(current_params, |
| 1906 | ¤t_params->deviceinfo) != PTP_RC_OK) { |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 1907 | LIBMTP_ERROR("LIBMTP PANIC: Unable to read device information on device " |
Linus Walleij | bdb89bd | 2008-05-28 23:32:35 +0000 | [diff] [blame] | 1908 | "%d on bus %d, trying to continue", |
| 1909 | rawdevice->devnum, rawdevice->bus_location); |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 1910 | |
Linus Walleij | bdb89bd | 2008-05-28 23:32:35 +0000 | [diff] [blame] | 1911 | /* Prevent memory leaks for this device */ |
| 1912 | free(mtp_device->usbinfo); |
| 1913 | free(mtp_device->params); |
| 1914 | current_params = NULL; |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 1915 | free(mtp_device); |
Linus Walleij | bdb89bd | 2008-05-28 23:32:35 +0000 | [diff] [blame] | 1916 | return NULL; |
| 1917 | } |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 1918 | |
Linus Walleij | 1473595 | 2010-04-25 04:56:49 +0000 | [diff] [blame] | 1919 | /* Check: if this is a PTP device, is it really tagged as MTP? */ |
| 1920 | if (current_params->deviceinfo.VendorExtensionID != 0x00000006) { |
| 1921 | LIBMTP_ERROR("LIBMTP WARNING: no MTP vendor extension on device " |
| 1922 | "%d on bus %d", |
| 1923 | rawdevice->devnum, rawdevice->bus_location); |
| 1924 | LIBMTP_ERROR("LIBMTP WARNING: VendorExtensionID: %08x", |
| 1925 | current_params->deviceinfo.VendorExtensionID); |
| 1926 | LIBMTP_ERROR("LIBMTP WARNING: VendorExtensionDesc: %s", |
| 1927 | current_params->deviceinfo.VendorExtensionDesc); |
| 1928 | LIBMTP_ERROR("LIBMTP WARNING: this typically means the device is PTP " |
| 1929 | "(i.e. a camera) but not an MTP device at all. " |
| 1930 | "Trying to continue anyway."); |
| 1931 | } |
| 1932 | |
Linus Walleij | 38d7ee8 | 2010-07-24 22:17:21 +0000 | [diff] [blame] | 1933 | parse_extension_descriptor(mtp_device, |
| 1934 | current_params->deviceinfo.VendorExtensionDesc); |
| 1935 | |
Linus Walleij | a566f60 | 2010-07-24 22:46:36 +0000 | [diff] [blame] | 1936 | /* |
Linus Walleij | 03f0c03 | 2011-10-22 00:01:14 +0200 | [diff] [blame] | 1937 | * Android has a number of bugs, force-assign these bug flags |
Linus Walleij | 15c7d1b | 2012-01-07 01:37:31 +0100 | [diff] [blame] | 1938 | * if Android is encountered. Same thing for devices we detect |
| 1939 | * as SONY NWZ Walkmen. I have no clue what "sony.net/WMFU" means |
| 1940 | * I just know only NWZs have it. |
Linus Walleij | 03f0c03 | 2011-10-22 00:01:14 +0200 | [diff] [blame] | 1941 | */ |
| 1942 | { |
| 1943 | LIBMTP_device_extension_t *tmpext = mtp_device->extensions; |
Linus Walleij | 4c570a9 | 2012-07-17 22:33:58 +0200 | [diff] [blame] | 1944 | int is_microsoft_com_wpdna = 0; |
| 1945 | int is_android = 0; |
| 1946 | int is_sony_net_wmfu = 0; |
| 1947 | int is_sonyericsson_com_se = 0; |
Linus Walleij | 03f0c03 | 2011-10-22 00:01:14 +0200 | [diff] [blame] | 1948 | |
Linus Walleij | 4c570a9 | 2012-07-17 22:33:58 +0200 | [diff] [blame] | 1949 | /* Loop over extensions and set flags */ |
Linus Walleij | 03f0c03 | 2011-10-22 00:01:14 +0200 | [diff] [blame] | 1950 | while (tmpext != NULL) { |
Linus Walleij | 4c570a9 | 2012-07-17 22:33:58 +0200 | [diff] [blame] | 1951 | if (!strcmp(tmpext->name, "microsoft.com/WPDNA")) |
| 1952 | is_microsoft_com_wpdna = 1; |
| 1953 | if (!strcmp(tmpext->name, "android.com")) |
| 1954 | is_android = 1; |
| 1955 | if (!strcmp(tmpext->name, "sony.net/WMFU")) |
| 1956 | is_sony_net_wmfu = 1; |
| 1957 | if (!strcmp(tmpext->name, "sonyericsson.com/SE")) |
| 1958 | is_sonyericsson_com_se = 1; |
Linus Walleij | 03f0c03 | 2011-10-22 00:01:14 +0200 | [diff] [blame] | 1959 | tmpext = tmpext->next; |
| 1960 | } |
Linus Walleij | 4c570a9 | 2012-07-17 22:33:58 +0200 | [diff] [blame] | 1961 | |
| 1962 | /* Check for specific stacks */ |
| 1963 | if (is_microsoft_com_wpdna && is_sonyericsson_com_se && !is_android) { |
| 1964 | /* |
| 1965 | * The Aricent stack seems to be detected by providing WPDNA, the SonyEricsson |
| 1966 | * extension and NO Android extension. |
| 1967 | */ |
| 1968 | ptp_usb->rawdevice.device_entry.device_flags |= DEVICE_FLAGS_ARICENT_BUGS; |
| 1969 | LIBMTP_INFO("Aricent MTP stack device detected, assigning default bug flags\n"); |
| 1970 | } |
| 1971 | else if (is_android) { |
| 1972 | /* |
| 1973 | * If bugs are fixed in later versions, test on tmpext->major, tmpext->minor |
| 1974 | */ |
| 1975 | ptp_usb->rawdevice.device_entry.device_flags |= DEVICE_FLAGS_ANDROID_BUGS; |
| 1976 | LIBMTP_INFO("Android device detected, assigning default bug flags\n"); |
| 1977 | } |
| 1978 | else if (is_sony_net_wmfu) { |
| 1979 | ptp_usb->rawdevice.device_entry.device_flags |= DEVICE_FLAGS_SONY_NWZ_BUGS; |
| 1980 | LIBMTP_INFO("SONY NWZ device detected, assigning default bug flags\n"); |
| 1981 | } |
Linus Walleij | 03f0c03 | 2011-10-22 00:01:14 +0200 | [diff] [blame] | 1982 | } |
| 1983 | |
| 1984 | /* |
Linus Walleij | a566f60 | 2010-07-24 22:46:36 +0000 | [diff] [blame] | 1985 | * If the OGG or FLAC filetypes are flagged as "unknown", check |
| 1986 | * if the firmware has been updated to actually support it. |
| 1987 | */ |
| 1988 | if (FLAG_OGG_IS_UNKNOWN(ptp_usb)) { |
| 1989 | for (i=0;i<current_params->deviceinfo.ImageFormats_len;i++) { |
| 1990 | if (current_params->deviceinfo.ImageFormats[i] == PTP_OFC_MTP_OGG) { |
| 1991 | /* This is not unknown anymore, unflag it */ |
Linus Walleij | a8b8889 | 2011-03-03 19:58:26 +0100 | [diff] [blame] | 1992 | ptp_usb->rawdevice.device_entry.device_flags &= |
Linus Walleij | a566f60 | 2010-07-24 22:46:36 +0000 | [diff] [blame] | 1993 | ~DEVICE_FLAG_OGG_IS_UNKNOWN; |
| 1994 | break; |
| 1995 | } |
| 1996 | } |
| 1997 | } |
| 1998 | if (FLAG_FLAC_IS_UNKNOWN(ptp_usb)) { |
| 1999 | for (i=0;i<current_params->deviceinfo.ImageFormats_len;i++) { |
| 2000 | if (current_params->deviceinfo.ImageFormats[i] == PTP_OFC_MTP_FLAC) { |
| 2001 | /* This is not unknown anymore, unflag it */ |
Linus Walleij | a8b8889 | 2011-03-03 19:58:26 +0100 | [diff] [blame] | 2002 | ptp_usb->rawdevice.device_entry.device_flags &= |
Linus Walleij | a566f60 | 2010-07-24 22:46:36 +0000 | [diff] [blame] | 2003 | ~DEVICE_FLAG_FLAC_IS_UNKNOWN; |
| 2004 | break; |
| 2005 | } |
| 2006 | } |
| 2007 | } |
| 2008 | |
Linus Walleij | bdb89bd | 2008-05-28 23:32:35 +0000 | [diff] [blame] | 2009 | /* Determine if the object size supported is 32 or 64 bit wide */ |
Lei Zhang | caa1bbc | 2013-02-13 15:12:02 -0800 | [diff] [blame] | 2010 | if (ptp_operation_issupported(current_params,PTP_OC_MTP_GetObjectPropsSupported)) { |
| 2011 | for (i=0;i<current_params->deviceinfo.ImageFormats_len;i++) { |
| 2012 | PTPObjectPropDesc opd; |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 2013 | |
Lei Zhang | caa1bbc | 2013-02-13 15:12:02 -0800 | [diff] [blame] | 2014 | if (ptp_mtp_getobjectpropdesc(current_params, |
| 2015 | PTP_OPC_ObjectSize, |
| 2016 | current_params->deviceinfo.ImageFormats[i], |
| 2017 | &opd) != PTP_RC_OK) { |
| 2018 | LIBMTP_ERROR("LIBMTP PANIC: " |
| 2019 | "could not inspect object property descriptions!\n"); |
Linus Walleij | bdb89bd | 2008-05-28 23:32:35 +0000 | [diff] [blame] | 2020 | } else { |
Lei Zhang | caa1bbc | 2013-02-13 15:12:02 -0800 | [diff] [blame] | 2021 | if (opd.DataType == PTP_DTC_UINT32) { |
| 2022 | if (bs == 0) { |
| 2023 | bs = 32; |
| 2024 | } else if (bs != 32) { |
| 2025 | LIBMTP_ERROR("LIBMTP PANIC: " |
| 2026 | "different objects support different object sizes!\n"); |
| 2027 | bs = 0; |
| 2028 | break; |
| 2029 | } |
| 2030 | } else if (opd.DataType == PTP_DTC_UINT64) { |
| 2031 | if (bs == 0) { |
| 2032 | bs = 64; |
| 2033 | } else if (bs != 64) { |
| 2034 | LIBMTP_ERROR("LIBMTP PANIC: " |
| 2035 | "different objects support different object sizes!\n"); |
| 2036 | bs = 0; |
| 2037 | break; |
| 2038 | } |
| 2039 | } else { |
| 2040 | // Ignore if other size. |
| 2041 | LIBMTP_ERROR("LIBMTP PANIC: " |
| 2042 | "awkward object size data type: %04x\n", opd.DataType); |
| 2043 | bs = 0; |
| 2044 | break; |
| 2045 | } |
Linus Walleij | bdb89bd | 2008-05-28 23:32:35 +0000 | [diff] [blame] | 2046 | } |
| 2047 | } |
| 2048 | } |
| 2049 | if (bs == 0) { |
| 2050 | // Could not detect object bitsize, assume 32 bits |
| 2051 | bs = 32; |
| 2052 | } |
| 2053 | mtp_device->object_bitsize = bs; |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 2054 | |
Linus Walleij | bdb89bd | 2008-05-28 23:32:35 +0000 | [diff] [blame] | 2055 | /* No Errors yet for this device */ |
| 2056 | mtp_device->errorstack = NULL; |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 2057 | |
Linus Walleij | bdb89bd | 2008-05-28 23:32:35 +0000 | [diff] [blame] | 2058 | /* Default Max Battery Level, we will adjust this if possible */ |
| 2059 | mtp_device->maximum_battery_level = 100; |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 2060 | |
Linus Walleij | bdb89bd | 2008-05-28 23:32:35 +0000 | [diff] [blame] | 2061 | /* Check if device supports reading maximum battery level */ |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 2062 | if(!FLAG_BROKEN_BATTERY_LEVEL(ptp_usb) && |
Linus Walleij | 4096c88 | 2009-03-16 23:32:34 +0000 | [diff] [blame] | 2063 | ptp_property_issupported( current_params, PTP_DPC_BatteryLevel)) { |
Linus Walleij | bdb89bd | 2008-05-28 23:32:35 +0000 | [diff] [blame] | 2064 | PTPDevicePropDesc dpd; |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 2065 | |
Linus Walleij | bdb89bd | 2008-05-28 23:32:35 +0000 | [diff] [blame] | 2066 | /* Try to read maximum battery level */ |
| 2067 | if(ptp_getdevicepropdesc(current_params, |
| 2068 | PTP_DPC_BatteryLevel, |
| 2069 | &dpd) != PTP_RC_OK) { |
| 2070 | add_error_to_errorstack(mtp_device, |
| 2071 | LIBMTP_ERROR_CONNECTING, |
| 2072 | "Unable to read Maximum Battery Level for this " |
| 2073 | "device even though the device supposedly " |
| 2074 | "supports this functionality"); |
| 2075 | } |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 2076 | |
Linus Walleij | bdb89bd | 2008-05-28 23:32:35 +0000 | [diff] [blame] | 2077 | /* TODO: is this appropriate? */ |
| 2078 | /* If max battery level is 0 then leave the default, otherwise assign */ |
| 2079 | if (dpd.FORM.Range.MaximumValue.u8 != 0) { |
| 2080 | mtp_device->maximum_battery_level = dpd.FORM.Range.MaximumValue.u8; |
| 2081 | } |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 2082 | |
Linus Walleij | bdb89bd | 2008-05-28 23:32:35 +0000 | [diff] [blame] | 2083 | ptp_free_devicepropdesc(&dpd); |
| 2084 | } |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 2085 | |
Linus Walleij | 4fc1917 | 2010-01-19 00:10:26 +0000 | [diff] [blame] | 2086 | /* Set all default folders to 0xffffffffU (root directory) */ |
| 2087 | mtp_device->default_music_folder = 0xffffffffU; |
| 2088 | mtp_device->default_playlist_folder = 0xffffffffU; |
| 2089 | mtp_device->default_picture_folder = 0xffffffffU; |
| 2090 | mtp_device->default_video_folder = 0xffffffffU; |
| 2091 | mtp_device->default_organizer_folder = 0xffffffffU; |
| 2092 | mtp_device->default_zencast_folder = 0xffffffffU; |
| 2093 | mtp_device->default_album_folder = 0xffffffffU; |
| 2094 | mtp_device->default_text_folder = 0xffffffffU; |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 2095 | |
Linus Walleij | bdb89bd | 2008-05-28 23:32:35 +0000 | [diff] [blame] | 2096 | /* Set initial storage information */ |
| 2097 | mtp_device->storage = NULL; |
| 2098 | if (LIBMTP_Get_Storage(mtp_device, LIBMTP_STORAGE_SORTBY_NOTSORTED) == -1) { |
| 2099 | add_error_to_errorstack(mtp_device, |
| 2100 | LIBMTP_ERROR_GENERAL, |
| 2101 | "Get Storage information failed."); |
| 2102 | mtp_device->storage = NULL; |
| 2103 | } |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 2104 | |
Linus Walleij | a8b8889 | 2011-03-03 19:58:26 +0100 | [diff] [blame] | 2105 | |
| 2106 | return mtp_device; |
| 2107 | } |
| 2108 | |
| 2109 | LIBMTP_mtpdevice_t *LIBMTP_Open_Raw_Device(LIBMTP_raw_device_t *rawdevice) |
| 2110 | { |
| 2111 | LIBMTP_mtpdevice_t *mtp_device = LIBMTP_Open_Raw_Device_Uncached(rawdevice); |
| 2112 | |
| 2113 | if (mtp_device == NULL) |
| 2114 | return NULL; |
| 2115 | |
Sajid Anwar | 8dca41d | 2012-08-18 20:36:13 +0200 | [diff] [blame] | 2116 | /* Check for MTPZ devices. */ |
| 2117 | if (use_mtpz) { |
| 2118 | LIBMTP_device_extension_t *tmpext = mtp_device->extensions; |
| 2119 | |
| 2120 | while (tmpext != NULL) { |
| 2121 | if (!strcmp(tmpext->name, "microsoft.com/MTPZ")) { |
| 2122 | LIBMTP_INFO("MTPZ device detected. Authenticating...\n"); |
| 2123 | if (PTP_RC_OK == ptp_mtpz_handshake(mtp_device->params)) { |
| 2124 | LIBMTP_INFO ("(MTPZ) Successfully authenticated with device.\n"); |
| 2125 | } else { |
| 2126 | LIBMTP_INFO ("(MTPZ) Failure - could not authenticate with device.\n"); |
| 2127 | } |
| 2128 | break; |
| 2129 | } |
| 2130 | tmpext = tmpext->next; |
| 2131 | } |
| 2132 | } |
| 2133 | |
Linus Walleij | a8b8889 | 2011-03-03 19:58:26 +0100 | [diff] [blame] | 2134 | // Set up this device as cached |
| 2135 | mtp_device->cached = 1; |
Linus Walleij | bdb89bd | 2008-05-28 23:32:35 +0000 | [diff] [blame] | 2136 | /* |
| 2137 | * Then get the handles and try to locate the default folders. |
| 2138 | * This has the desired side effect of caching all handles from |
| 2139 | * the device which speeds up later operations. |
| 2140 | */ |
| 2141 | flush_handles(mtp_device); |
Linus Walleij | bdb89bd | 2008-05-28 23:32:35 +0000 | [diff] [blame] | 2142 | return mtp_device; |
| 2143 | } |
| 2144 | |
| 2145 | /** |
Linus Walleij | 335b6fb | 2011-03-22 23:43:32 +0100 | [diff] [blame] | 2146 | * To read events sent by the device, repeatedly call this function from a secondary |
| 2147 | * thread until the return value is < 0. |
| 2148 | * |
| 2149 | * @param device a pointer to the MTP device to poll for events. |
| 2150 | * @param event contains a pointer to be filled in with the event retrieved if the call |
| 2151 | * is successful. |
Philip Langdale | da26879 | 2012-09-02 14:48:25 -0700 | [diff] [blame] | 2152 | * @param out1 contains the param1 value from the raw event. |
Linus Walleij | 335b6fb | 2011-03-22 23:43:32 +0100 | [diff] [blame] | 2153 | * @return 0 on success, any other value means the polling loop shall be |
| 2154 | * terminated immediately for this session. |
| 2155 | */ |
Philip Langdale | da26879 | 2012-09-02 14:48:25 -0700 | [diff] [blame] | 2156 | int LIBMTP_Read_Event(LIBMTP_mtpdevice_t *device, LIBMTP_event_t *event, uint32_t *out1) |
Linus Walleij | 335b6fb | 2011-03-22 23:43:32 +0100 | [diff] [blame] | 2157 | { |
| 2158 | /* |
| 2159 | * FIXME: Potential race-condition here, if client deallocs device |
| 2160 | * while we're *not* waiting for input. As we'll be waiting for |
| 2161 | * input most of the time, it's unlikely but still worth considering |
| 2162 | * for improvement. Also we cannot affect the state of the cache etc |
| 2163 | * unless we know we are the sole user on the device. A spinlock or |
| 2164 | * mutex in the LIBMTP_mtpdevice_t is needed for this to work. |
| 2165 | */ |
| 2166 | PTPParams *params = (PTPParams *) device->params; |
| 2167 | PTPContainer ptp_event; |
Linus Walleij | 6ba0345 | 2011-11-14 20:46:36 +0100 | [diff] [blame] | 2168 | uint16_t ret = ptp_usb_event_wait(params, &ptp_event); |
Linus Walleij | 335b6fb | 2011-03-22 23:43:32 +0100 | [diff] [blame] | 2169 | |
| 2170 | if (ret != PTP_RC_OK) { |
| 2171 | /* Device is closing down or other fatal stuff, exit thread */ |
| 2172 | return -1; |
| 2173 | } |
Philip Langdale | 0a57651 | 2016-04-09 14:22:25 -0700 | [diff] [blame] | 2174 | LIBMTP_Handle_Event(&ptp_event, event, out1); |
| 2175 | return 0; |
| 2176 | } |
| 2177 | |
| 2178 | void LIBMTP_Handle_Event(PTPContainer *ptp_event, |
| 2179 | LIBMTP_event_t *event, uint32_t *out1) { |
| 2180 | uint16_t code; |
| 2181 | uint32_t session_id; |
| 2182 | uint32_t param1; |
Linus Walleij | 335b6fb | 2011-03-22 23:43:32 +0100 | [diff] [blame] | 2183 | |
| 2184 | *event = LIBMTP_EVENT_NONE; |
| 2185 | |
| 2186 | /* Process the event */ |
Philip Langdale | 0a57651 | 2016-04-09 14:22:25 -0700 | [diff] [blame] | 2187 | code = ptp_event->Code; |
| 2188 | session_id = ptp_event->SessionID; |
| 2189 | param1 = ptp_event->Param1; |
Linus Walleij | 335b6fb | 2011-03-22 23:43:32 +0100 | [diff] [blame] | 2190 | |
| 2191 | switch(code) { |
| 2192 | case PTP_EC_Undefined: |
| 2193 | LIBMTP_INFO("Received event PTP_EC_Undefined in session %u\n", session_id); |
| 2194 | break; |
| 2195 | case PTP_EC_CancelTransaction: |
| 2196 | LIBMTP_INFO("Received event PTP_EC_CancelTransaction in session %u\n", session_id); |
| 2197 | break; |
| 2198 | case PTP_EC_ObjectAdded: |
| 2199 | LIBMTP_INFO("Received event PTP_EC_ObjectAdded in session %u\n", session_id); |
Philip Langdale | 50dd347 | 2013-03-10 12:22:51 -0700 | [diff] [blame] | 2200 | *event = LIBMTP_EVENT_OBJECT_ADDED; |
| 2201 | *out1 = param1; |
Linus Walleij | 335b6fb | 2011-03-22 23:43:32 +0100 | [diff] [blame] | 2202 | break; |
| 2203 | case PTP_EC_ObjectRemoved: |
| 2204 | LIBMTP_INFO("Received event PTP_EC_ObjectRemoved in session %u\n", session_id); |
Philip Langdale | 50dd347 | 2013-03-10 12:22:51 -0700 | [diff] [blame] | 2205 | *event = LIBMTP_EVENT_OBJECT_REMOVED; |
| 2206 | *out1 = param1; |
Linus Walleij | 335b6fb | 2011-03-22 23:43:32 +0100 | [diff] [blame] | 2207 | break; |
| 2208 | case PTP_EC_StoreAdded: |
| 2209 | LIBMTP_INFO("Received event PTP_EC_StoreAdded in session %u\n", session_id); |
| 2210 | /* TODO: rescan storages */ |
Philip Langdale | da26879 | 2012-09-02 14:48:25 -0700 | [diff] [blame] | 2211 | *event = LIBMTP_EVENT_STORE_ADDED; |
| 2212 | *out1 = param1; |
Linus Walleij | 335b6fb | 2011-03-22 23:43:32 +0100 | [diff] [blame] | 2213 | break; |
| 2214 | case PTP_EC_StoreRemoved: |
| 2215 | LIBMTP_INFO("Received event PTP_EC_StoreRemoved in session %u\n", session_id); |
| 2216 | /* TODO: rescan storages */ |
Philip Langdale | 50dd347 | 2013-03-10 12:22:51 -0700 | [diff] [blame] | 2217 | *event = LIBMTP_EVENT_STORE_REMOVED; |
| 2218 | *out1 = param1; |
Linus Walleij | 335b6fb | 2011-03-22 23:43:32 +0100 | [diff] [blame] | 2219 | break; |
| 2220 | case PTP_EC_DevicePropChanged: |
| 2221 | LIBMTP_INFO("Received event PTP_EC_DevicePropChanged in session %u\n", session_id); |
| 2222 | /* TODO: update device properties */ |
Philip Langdale | 7815a64 | 2018-03-24 08:08:43 +0100 | [diff] [blame^] | 2223 | *event = LIBMTP_EVENT_DEVICE_PROPERTY_CHANGED; |
| 2224 | *out1 = param1; |
Linus Walleij | 335b6fb | 2011-03-22 23:43:32 +0100 | [diff] [blame] | 2225 | break; |
| 2226 | case PTP_EC_ObjectInfoChanged: |
| 2227 | LIBMTP_INFO("Received event PTP_EC_ObjectInfoChanged in session %u\n", session_id); |
| 2228 | /* TODO: rescan object cache or just for this one object */ |
| 2229 | break; |
| 2230 | case PTP_EC_DeviceInfoChanged: |
| 2231 | LIBMTP_INFO("Received event PTP_EC_DeviceInfoChanged in session %u\n", session_id); |
| 2232 | /* TODO: update device info */ |
| 2233 | break; |
| 2234 | case PTP_EC_RequestObjectTransfer: |
| 2235 | LIBMTP_INFO("Received event PTP_EC_RequestObjectTransfer in session %u\n", session_id); |
| 2236 | break; |
| 2237 | case PTP_EC_StoreFull: |
| 2238 | LIBMTP_INFO("Received event PTP_EC_StoreFull in session %u\n", session_id); |
| 2239 | break; |
| 2240 | case PTP_EC_DeviceReset: |
| 2241 | LIBMTP_INFO("Received event PTP_EC_DeviceReset in session %u\n", session_id); |
| 2242 | break; |
| 2243 | case PTP_EC_StorageInfoChanged : |
| 2244 | LIBMTP_INFO( "Received event PTP_EC_StorageInfoChanged in session %u\n", session_id); |
| 2245 | /* TODO: update storage info */ |
| 2246 | break; |
| 2247 | case PTP_EC_CaptureComplete : |
| 2248 | LIBMTP_INFO( "Received event PTP_EC_CaptureComplete in session %u\n", session_id); |
| 2249 | break; |
| 2250 | case PTP_EC_UnreportedStatus : |
| 2251 | LIBMTP_INFO( "Received event PTP_EC_UnreportedStatus in session %u\n", session_id); |
| 2252 | break; |
| 2253 | default : |
| 2254 | LIBMTP_INFO( "Received unknown event in session %u\n", session_id); |
| 2255 | break; |
| 2256 | } |
Philip Langdale | 0a57651 | 2016-04-09 14:22:25 -0700 | [diff] [blame] | 2257 | } |
Linus Walleij | 335b6fb | 2011-03-22 23:43:32 +0100 | [diff] [blame] | 2258 | |
Philip Langdale | 0a57651 | 2016-04-09 14:22:25 -0700 | [diff] [blame] | 2259 | static void LIBMTP_Read_Event_Cb(PTPParams *params, uint16_t ret_code, |
| 2260 | PTPContainer *ptp_event, void *user_data) { |
| 2261 | event_cb_data_t *data = user_data; |
| 2262 | LIBMTP_event_t event = LIBMTP_EVENT_NONE; |
| 2263 | uint32_t param1 = 0; |
| 2264 | int handler_ret; |
| 2265 | |
| 2266 | switch (ret_code) { |
| 2267 | case PTP_RC_OK: |
| 2268 | handler_ret = LIBMTP_HANDLER_RETURN_OK; |
| 2269 | LIBMTP_Handle_Event(ptp_event, &event, ¶m1); |
| 2270 | break; |
| 2271 | case PTP_ERROR_CANCEL: |
| 2272 | handler_ret = LIBMTP_HANDLER_RETURN_CANCEL; |
| 2273 | break; |
| 2274 | default: |
| 2275 | handler_ret = LIBMTP_HANDLER_RETURN_ERROR; |
| 2276 | break; |
| 2277 | } |
| 2278 | |
| 2279 | data->cb(handler_ret, event, param1, data->user_data); |
| 2280 | free(data); |
| 2281 | } |
| 2282 | |
| 2283 | /** |
| 2284 | * This function reads events sent by the device, in a non-blocking manner. |
| 2285 | * The callback function will be called when an event is received, but for the function |
| 2286 | * to make progress, polling must take place, using LIBMTP_Handle_Events_Timeout_Completed. |
| 2287 | * |
| 2288 | * After an event is received, this function should be called again to listen for the next |
| 2289 | * event. |
| 2290 | * |
| 2291 | * For now, this non-blocking mechanism only works with libusb-1.0, and not any of the |
| 2292 | * other usb library backends. Attempting to call this method with another backend will |
| 2293 | * always return an error. |
| 2294 | * |
| 2295 | * @param device a pointer to the MTP device to poll for events. |
| 2296 | * @param cb a callback to be invoked when an event is received. |
| 2297 | * @param user_data arbitrary user data passed to the callback. |
| 2298 | * @return 0 on success, any other value means that the callback was not registered and |
| 2299 | * no event notification will take place. |
| 2300 | */ |
| 2301 | int LIBMTP_Read_Event_Async(LIBMTP_mtpdevice_t *device, LIBMTP_event_cb_fn cb, void *user_data) { |
| 2302 | PTPParams *params = (PTPParams *) device->params; |
| 2303 | event_cb_data_t *data = malloc(sizeof(event_cb_data_t)); |
| 2304 | uint16_t ret; |
| 2305 | |
| 2306 | data->cb = cb; |
| 2307 | data->user_data = user_data; |
| 2308 | |
| 2309 | ret = ptp_usb_event_async(params, LIBMTP_Read_Event_Cb, data); |
| 2310 | return ret == PTP_RC_OK ? 0 : -1; |
Linus Walleij | 335b6fb | 2011-03-22 23:43:32 +0100 | [diff] [blame] | 2311 | } |
| 2312 | |
| 2313 | /** |
Linus Walleij | 2d3f7b8 | 2007-02-14 09:24:20 +0000 | [diff] [blame] | 2314 | * Recursive function that adds MTP devices to a linked list |
Linus Walleij | a700d22 | 2008-05-28 23:06:14 +0000 | [diff] [blame] | 2315 | * @param devices a list of raw devices to have real devices created for. |
Linus Walleij | 2d3f7b8 | 2007-02-14 09:24:20 +0000 | [diff] [blame] | 2316 | * @return a device pointer to a newly created mtpdevice (used in linked |
Linus Walleij | b0ab548 | 2007-08-29 21:08:54 +0000 | [diff] [blame] | 2317 | * list creation). |
Linus Walleij | 2d3f7b8 | 2007-02-14 09:24:20 +0000 | [diff] [blame] | 2318 | */ |
Linus Walleij | a700d22 | 2008-05-28 23:06:14 +0000 | [diff] [blame] | 2319 | static LIBMTP_mtpdevice_t * create_usb_mtp_devices(LIBMTP_raw_device_t *devices, int numdevs) |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 2320 | { |
Linus Walleij | a700d22 | 2008-05-28 23:06:14 +0000 | [diff] [blame] | 2321 | uint8_t i; |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 2322 | LIBMTP_mtpdevice_t *mtp_device_list = NULL; |
| 2323 | LIBMTP_mtpdevice_t *current_device = NULL; |
Linus Walleij | a700d22 | 2008-05-28 23:06:14 +0000 | [diff] [blame] | 2324 | |
| 2325 | for (i=0; i < numdevs; i++) { |
Linus Walleij | 2d3f7b8 | 2007-02-14 09:24:20 +0000 | [diff] [blame] | 2326 | LIBMTP_mtpdevice_t *mtp_device; |
Linus Walleij | bdb89bd | 2008-05-28 23:32:35 +0000 | [diff] [blame] | 2327 | mtp_device = LIBMTP_Open_Raw_Device(&devices[i]); |
Linus Walleij | a700d22 | 2008-05-28 23:06:14 +0000 | [diff] [blame] | 2328 | |
Linus Walleij | bdb89bd | 2008-05-28 23:32:35 +0000 | [diff] [blame] | 2329 | /* On error, try next device */ |
| 2330 | if (mtp_device == NULL) |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 2331 | continue; |
Linus Walleij | ddaba2f | 2007-10-02 21:20:30 +0000 | [diff] [blame] | 2332 | |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 2333 | /* Add the device to the list */ |
| 2334 | mtp_device->next = NULL; |
| 2335 | if (mtp_device_list == NULL) { |
| 2336 | mtp_device_list = current_device = mtp_device; |
| 2337 | } else { |
| 2338 | current_device->next = mtp_device; |
| 2339 | current_device = mtp_device; |
| 2340 | } |
Linus Walleij | 2d3f7b8 | 2007-02-14 09:24:20 +0000 | [diff] [blame] | 2341 | } |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 2342 | return mtp_device_list; |
Linus Walleij | 2d3f7b8 | 2007-02-14 09:24:20 +0000 | [diff] [blame] | 2343 | } |
| 2344 | |
| 2345 | /** |
tedbullock | 848009b | 2007-03-03 23:20:12 +0000 | [diff] [blame] | 2346 | * Get the number of devices that are available in the listed device list |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 2347 | * @param device_list Pointer to a linked list of devices |
| 2348 | * @return Number of devices in the device list device_list |
| 2349 | * @see LIBMTP_Get_Connected_Devices() |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 2350 | */ |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 2351 | uint32_t LIBMTP_Number_Devices_In_List(LIBMTP_mtpdevice_t *device_list) |
tedbullock | 848009b | 2007-03-03 23:20:12 +0000 | [diff] [blame] | 2352 | { |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 2353 | uint32_t numdevices = 0; |
| 2354 | LIBMTP_mtpdevice_t *iter; |
| 2355 | for(iter = device_list; iter != NULL; iter = iter->next) |
| 2356 | numdevices++; |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 2357 | |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 2358 | return numdevices; |
tedbullock | 848009b | 2007-03-03 23:20:12 +0000 | [diff] [blame] | 2359 | } |
| 2360 | |
| 2361 | /** |
Linus Walleij | 2d3f7b8 | 2007-02-14 09:24:20 +0000 | [diff] [blame] | 2362 | * Get the first connected MTP device node in the linked list of devices. |
| 2363 | * Currently this only provides access to USB devices |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 2364 | * @param device_list A list of devices ready to be used by the caller. You |
| 2365 | * need to know how many there are. |
Linus Walleij | 2d3f7b8 | 2007-02-14 09:24:20 +0000 | [diff] [blame] | 2366 | * @return Any error information gathered from device connections |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 2367 | * @see LIBMTP_Number_Devices_In_List() |
Linus Walleij | 2d3f7b8 | 2007-02-14 09:24:20 +0000 | [diff] [blame] | 2368 | */ |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 2369 | LIBMTP_error_number_t LIBMTP_Get_Connected_Devices(LIBMTP_mtpdevice_t **device_list) |
Linus Walleij | 2d3f7b8 | 2007-02-14 09:24:20 +0000 | [diff] [blame] | 2370 | { |
Linus Walleij | a700d22 | 2008-05-28 23:06:14 +0000 | [diff] [blame] | 2371 | LIBMTP_raw_device_t *devices; |
| 2372 | int numdevs; |
Linus Walleij | 5fbb77d | 2007-03-07 08:40:36 +0000 | [diff] [blame] | 2373 | LIBMTP_error_number_t ret; |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 2374 | |
Linus Walleij | a700d22 | 2008-05-28 23:06:14 +0000 | [diff] [blame] | 2375 | ret = LIBMTP_Detect_Raw_Devices(&devices, &numdevs); |
Linus Walleij | 5fbb77d | 2007-03-07 08:40:36 +0000 | [diff] [blame] | 2376 | if (ret != LIBMTP_ERROR_NONE) { |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 2377 | *device_list = NULL; |
Linus Walleij | 5fbb77d | 2007-03-07 08:40:36 +0000 | [diff] [blame] | 2378 | return ret; |
Linus Walleij | 2d3f7b8 | 2007-02-14 09:24:20 +0000 | [diff] [blame] | 2379 | } |
| 2380 | |
| 2381 | /* Assign linked list of devices */ |
Linus Walleij | a700d22 | 2008-05-28 23:06:14 +0000 | [diff] [blame] | 2382 | if (devices == NULL || numdevs == 0) { |
| 2383 | *device_list = NULL; |
Marcus Meissner | b521477 | 2015-10-04 13:08:20 +0200 | [diff] [blame] | 2384 | free(devices); |
Linus Walleij | a700d22 | 2008-05-28 23:06:14 +0000 | [diff] [blame] | 2385 | return LIBMTP_ERROR_NO_DEVICE_ATTACHED; |
| 2386 | } |
| 2387 | |
| 2388 | *device_list = create_usb_mtp_devices(devices, numdevs); |
| 2389 | free(devices); |
Linus Walleij | 45a8637 | 2007-03-07 09:36:19 +0000 | [diff] [blame] | 2390 | |
Linus Walleij | 2d3f7b8 | 2007-02-14 09:24:20 +0000 | [diff] [blame] | 2391 | /* TODO: Add wifi device access here */ |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 2392 | |
Richard Low | 99a93e8 | 2007-09-29 08:16:08 +0000 | [diff] [blame] | 2393 | /* We have found some devices but create failed */ |
| 2394 | if (*device_list == NULL) |
| 2395 | return LIBMTP_ERROR_CONNECTING; |
Linus Walleij | 2d3f7b8 | 2007-02-14 09:24:20 +0000 | [diff] [blame] | 2396 | |
| 2397 | return LIBMTP_ERROR_NONE; |
| 2398 | } |
| 2399 | |
| 2400 | /** |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 2401 | * This closes and releases an allocated MTP device. |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 2402 | * @param device a pointer to the MTP device to release. |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 2403 | */ |
tedbullock | 0f033cb | 2007-02-14 20:56:54 +0000 | [diff] [blame] | 2404 | void LIBMTP_Release_Device_List(LIBMTP_mtpdevice_t *device) |
| 2405 | { |
| 2406 | if(device != NULL) |
| 2407 | { |
| 2408 | if(device->next != NULL) |
| 2409 | { |
| 2410 | LIBMTP_Release_Device_List(device->next); |
| 2411 | } |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 2412 | |
tedbullock | 0f033cb | 2007-02-14 20:56:54 +0000 | [diff] [blame] | 2413 | LIBMTP_Release_Device(device); |
| 2414 | } |
| 2415 | } |
| 2416 | |
| 2417 | /** |
| 2418 | * This closes and releases an allocated MTP device. |
| 2419 | * @param device a pointer to the MTP device to release. |
| 2420 | */ |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 2421 | void LIBMTP_Release_Device(LIBMTP_mtpdevice_t *device) |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 2422 | { |
Linus Walleij | 9b28da3 | 2006-03-16 13:47:58 +0000 | [diff] [blame] | 2423 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | 2d411db | 2006-03-22 12:13:09 +0000 | [diff] [blame] | 2424 | PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo; |
Linus Walleij | 9b28da3 | 2006-03-16 13:47:58 +0000 | [diff] [blame] | 2425 | |
Linus Walleij | b0ab548 | 2007-08-29 21:08:54 +0000 | [diff] [blame] | 2426 | close_device(ptp_usb, params); |
Linus Walleij | 2715c44 | 2007-01-20 22:35:29 +0000 | [diff] [blame] | 2427 | // Clear error stack |
| 2428 | LIBMTP_Clear_Errorstack(device); |
Linus Walleij | 3ec8631 | 2006-08-21 13:25:24 +0000 | [diff] [blame] | 2429 | // Free iconv() converters... |
Linus Walleij | a823a70 | 2006-08-27 21:27:46 +0000 | [diff] [blame] | 2430 | iconv_close(params->cd_locale_to_ucs2); |
| 2431 | iconv_close(params->cd_ucs2_to_locale); |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 2432 | free(ptp_usb); |
Linus Walleij | 073c417 | 2007-02-02 22:26:33 +0000 | [diff] [blame] | 2433 | ptp_free_params(params); |
Lei Zhang | e1082c1 | 2013-02-13 15:16:43 -0800 | [diff] [blame] | 2434 | free(params); |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 2435 | free_storage_list(device); |
Linus Walleij | 38d7ee8 | 2010-07-24 22:17:21 +0000 | [diff] [blame] | 2436 | // Free extension list... |
| 2437 | if (device->extensions != NULL) { |
| 2438 | LIBMTP_device_extension_t *tmp = device->extensions; |
| 2439 | |
| 2440 | while (tmp != NULL) { |
| 2441 | LIBMTP_device_extension_t *next = tmp->next; |
| 2442 | |
| 2443 | if (tmp->name) |
| 2444 | free(tmp->name); |
| 2445 | free(tmp); |
| 2446 | tmp = next; |
| 2447 | } |
| 2448 | } |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 2449 | free(device); |
| 2450 | } |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 2451 | |
| 2452 | /** |
Linus Walleij | 2715c44 | 2007-01-20 22:35:29 +0000 | [diff] [blame] | 2453 | * This can be used by any libmtp-intrinsic code that |
Linus Walleij | 68b19c0 | 2007-02-15 11:50:37 +0000 | [diff] [blame] | 2454 | * need to stack up an error on the stack. You are only |
| 2455 | * supposed to add errors to the error stack using this |
| 2456 | * function, do not create and reference error entries |
| 2457 | * directly. |
Linus Walleij | 2715c44 | 2007-01-20 22:35:29 +0000 | [diff] [blame] | 2458 | */ |
| 2459 | static void add_error_to_errorstack(LIBMTP_mtpdevice_t *device, |
| 2460 | LIBMTP_error_number_t errornumber, |
| 2461 | char const * const error_text) |
| 2462 | { |
| 2463 | LIBMTP_error_t *newerror; |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 2464 | |
Linus Walleij | 68b19c0 | 2007-02-15 11:50:37 +0000 | [diff] [blame] | 2465 | if (device == NULL) { |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 2466 | LIBMTP_ERROR("LIBMTP PANIC: Trying to add error to a NULL device!\n"); |
Linus Walleij | 68b19c0 | 2007-02-15 11:50:37 +0000 | [diff] [blame] | 2467 | return; |
| 2468 | } |
Linus Walleij | 2715c44 | 2007-01-20 22:35:29 +0000 | [diff] [blame] | 2469 | newerror = (LIBMTP_error_t *) malloc(sizeof(LIBMTP_error_t)); |
| 2470 | newerror->errornumber = errornumber; |
| 2471 | newerror->error_text = strdup(error_text); |
rreardon | 774503c | 2007-02-15 15:52:49 +0000 | [diff] [blame] | 2472 | newerror->next = NULL; |
Linus Walleij | 2715c44 | 2007-01-20 22:35:29 +0000 | [diff] [blame] | 2473 | if (device->errorstack == NULL) { |
| 2474 | device->errorstack = newerror; |
| 2475 | } else { |
| 2476 | LIBMTP_error_t *tmp = device->errorstack; |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 2477 | |
Linus Walleij | 2715c44 | 2007-01-20 22:35:29 +0000 | [diff] [blame] | 2478 | while (tmp->next != NULL) { |
| 2479 | tmp = tmp->next; |
| 2480 | } |
| 2481 | tmp->next = newerror; |
| 2482 | } |
| 2483 | } |
| 2484 | |
| 2485 | /** |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 2486 | * Adds an error from the PTP layer to the error stack. |
| 2487 | */ |
| 2488 | static void add_ptp_error_to_errorstack(LIBMTP_mtpdevice_t *device, |
| 2489 | uint16_t ptp_error, |
| 2490 | char const * const error_text) |
| 2491 | { |
Marcus Meissner | 3d692dc | 2016-02-21 12:34:06 +0100 | [diff] [blame] | 2492 | PTPParams *params = (PTPParams *) device->params; |
| 2493 | |
Linus Walleij | 68b19c0 | 2007-02-15 11:50:37 +0000 | [diff] [blame] | 2494 | if (device == NULL) { |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 2495 | LIBMTP_ERROR("LIBMTP PANIC: Trying to add PTP error to a NULL device!\n"); |
Linus Walleij | 68b19c0 | 2007-02-15 11:50:37 +0000 | [diff] [blame] | 2496 | return; |
| 2497 | } else { |
| 2498 | char outstr[256]; |
| 2499 | snprintf(outstr, sizeof(outstr), "PTP Layer error %04x: %s", ptp_error, error_text); |
| 2500 | outstr[sizeof(outstr)-1] = '\0'; |
| 2501 | add_error_to_errorstack(device, LIBMTP_ERROR_PTP_LAYER, outstr); |
Catalin Patulea | 73d26cf | 2012-07-03 02:16:23 -0400 | [diff] [blame] | 2502 | |
Marcus Meissner | 3d692dc | 2016-02-21 12:34:06 +0100 | [diff] [blame] | 2503 | snprintf(outstr, sizeof(outstr), "Error %04x: %s", ptp_error, ptp_strerror(ptp_error, params->deviceinfo.VendorExtensionID)); |
Catalin Patulea | 73d26cf | 2012-07-03 02:16:23 -0400 | [diff] [blame] | 2504 | outstr[sizeof(outstr)-1] = '\0'; |
| 2505 | add_error_to_errorstack(device, LIBMTP_ERROR_PTP_LAYER, outstr); |
Linus Walleij | 68b19c0 | 2007-02-15 11:50:37 +0000 | [diff] [blame] | 2506 | } |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 2507 | } |
| 2508 | |
| 2509 | /** |
Linus Walleij | 2715c44 | 2007-01-20 22:35:29 +0000 | [diff] [blame] | 2510 | * This returns the error stack for a device in case you |
| 2511 | * need to either reference the error numbers (e.g. when |
| 2512 | * creating multilingual apps with multiple-language text |
| 2513 | * representations for each error number) or when you need |
| 2514 | * to build a multi-line error text widget or something like |
| 2515 | * that. You need to call the <code>LIBMTP_Clear_Errorstack</code> |
| 2516 | * to clear it when you're finished with it. |
| 2517 | * @param device a pointer to the MTP device to get the error |
| 2518 | * stack for. |
| 2519 | * @return the error stack or NULL if there are no errors |
| 2520 | * on the stack. |
| 2521 | * @see LIBMTP_Clear_Errorstack() |
| 2522 | * @see LIBMTP_Dump_Errorstack() |
| 2523 | */ |
| 2524 | LIBMTP_error_t *LIBMTP_Get_Errorstack(LIBMTP_mtpdevice_t *device) |
| 2525 | { |
Linus Walleij | 68b19c0 | 2007-02-15 11:50:37 +0000 | [diff] [blame] | 2526 | if (device == NULL) { |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 2527 | LIBMTP_ERROR("LIBMTP PANIC: Trying to get the error stack of a NULL device!\n"); |
Linus Walleij | 5c1499e | 2009-02-21 06:54:29 +0000 | [diff] [blame] | 2528 | return NULL; |
Linus Walleij | 68b19c0 | 2007-02-15 11:50:37 +0000 | [diff] [blame] | 2529 | } |
Linus Walleij | 2715c44 | 2007-01-20 22:35:29 +0000 | [diff] [blame] | 2530 | return device->errorstack; |
| 2531 | } |
| 2532 | |
| 2533 | /** |
| 2534 | * This function clears the error stack of a device and frees |
| 2535 | * any memory used by it. Call this when you're finished with |
| 2536 | * using the errors. |
| 2537 | * @param device a pointer to the MTP device to clear the error |
| 2538 | * stack for. |
| 2539 | */ |
| 2540 | void LIBMTP_Clear_Errorstack(LIBMTP_mtpdevice_t *device) |
| 2541 | { |
Linus Walleij | 68b19c0 | 2007-02-15 11:50:37 +0000 | [diff] [blame] | 2542 | if (device == NULL) { |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 2543 | LIBMTP_ERROR("LIBMTP PANIC: Trying to clear the error stack of a NULL device!\n"); |
Linus Walleij | 68b19c0 | 2007-02-15 11:50:37 +0000 | [diff] [blame] | 2544 | } else { |
| 2545 | LIBMTP_error_t *tmp = device->errorstack; |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 2546 | |
Linus Walleij | 68b19c0 | 2007-02-15 11:50:37 +0000 | [diff] [blame] | 2547 | while (tmp != NULL) { |
| 2548 | LIBMTP_error_t *tmp2; |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 2549 | |
Linus Walleij | 68b19c0 | 2007-02-15 11:50:37 +0000 | [diff] [blame] | 2550 | if (tmp->error_text != NULL) { |
| 2551 | free(tmp->error_text); |
| 2552 | } |
| 2553 | tmp2 = tmp; |
| 2554 | tmp = tmp->next; |
| 2555 | free(tmp2); |
Linus Walleij | 2715c44 | 2007-01-20 22:35:29 +0000 | [diff] [blame] | 2556 | } |
Linus Walleij | 68b19c0 | 2007-02-15 11:50:37 +0000 | [diff] [blame] | 2557 | device->errorstack = NULL; |
Linus Walleij | 2715c44 | 2007-01-20 22:35:29 +0000 | [diff] [blame] | 2558 | } |
Linus Walleij | 2715c44 | 2007-01-20 22:35:29 +0000 | [diff] [blame] | 2559 | } |
| 2560 | |
| 2561 | /** |
| 2562 | * This function dumps the error stack to <code>stderr</code>. |
| 2563 | * (You still have to clear the stack though.) |
| 2564 | * @param device a pointer to the MTP device to dump the error |
| 2565 | * stack for. |
| 2566 | */ |
| 2567 | void LIBMTP_Dump_Errorstack(LIBMTP_mtpdevice_t *device) |
| 2568 | { |
Linus Walleij | 68b19c0 | 2007-02-15 11:50:37 +0000 | [diff] [blame] | 2569 | if (device == NULL) { |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 2570 | LIBMTP_ERROR("LIBMTP PANIC: Trying to dump the error stack of a NULL device!\n"); |
Linus Walleij | 68b19c0 | 2007-02-15 11:50:37 +0000 | [diff] [blame] | 2571 | } else { |
| 2572 | LIBMTP_error_t *tmp = device->errorstack; |
Linus Walleij | 2715c44 | 2007-01-20 22:35:29 +0000 | [diff] [blame] | 2573 | |
Linus Walleij | 68b19c0 | 2007-02-15 11:50:37 +0000 | [diff] [blame] | 2574 | while (tmp != NULL) { |
| 2575 | if (tmp->error_text != NULL) { |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 2576 | LIBMTP_ERROR("Error %d: %s\n", tmp->errornumber, tmp->error_text); |
Linus Walleij | 68b19c0 | 2007-02-15 11:50:37 +0000 | [diff] [blame] | 2577 | } else { |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 2578 | LIBMTP_ERROR("Error %d: (unknown)\n", tmp->errornumber); |
Linus Walleij | 68b19c0 | 2007-02-15 11:50:37 +0000 | [diff] [blame] | 2579 | } |
| 2580 | tmp = tmp->next; |
Linus Walleij | 2715c44 | 2007-01-20 22:35:29 +0000 | [diff] [blame] | 2581 | } |
Linus Walleij | 2715c44 | 2007-01-20 22:35:29 +0000 | [diff] [blame] | 2582 | } |
| 2583 | } |
| 2584 | |
| 2585 | /** |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 2586 | * This command gets all handles and stuff by FAST directory retrieveal |
| 2587 | * which is available by getting all metadata for object |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 2588 | * <code>0xffffffff</code> which simply means "all metadata for all objects". |
| 2589 | * This works on the vast majority of MTP devices (there ARE exceptions!) |
| 2590 | * and is quite quick. Check the error stack to see if there were |
| 2591 | * problems getting the metadata. |
Linus Walleij | 8533bf7 | 2007-08-28 10:03:59 +0000 | [diff] [blame] | 2592 | * @return 0 if all was OK, -1 on failure. |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 2593 | */ |
Catalin Patulea | a23fce3 | 2012-07-17 22:25:12 -0400 | [diff] [blame] | 2594 | static int get_all_metadata_fast(LIBMTP_mtpdevice_t *device) |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 2595 | { |
| 2596 | PTPParams *params = (PTPParams *) device->params; |
| 2597 | int cnt = 0; |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 2598 | int i, j, nrofprops; |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 2599 | uint32_t lasthandle = 0xffffffff; |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 2600 | MTPProperties *props = NULL; |
| 2601 | MTPProperties *prop; |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 2602 | uint16_t ret; |
Linus Walleij | 2f62281 | 2008-08-30 22:06:58 +0000 | [diff] [blame] | 2603 | int oldtimeout; |
| 2604 | PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo; |
| 2605 | |
Linus Walleij | e04a1b9 | 2011-03-09 18:00:24 +0100 | [diff] [blame] | 2606 | /* |
| 2607 | * The follow request causes the device to generate |
| 2608 | * a list of every file on the device and return it |
Linus Walleij | 2f62281 | 2008-08-30 22:06:58 +0000 | [diff] [blame] | 2609 | * in a single response. |
| 2610 | * |
| 2611 | * Some slow devices as well as devices with very |
| 2612 | * large file systems can easily take longer then |
| 2613 | * the standard timeout value before it is able |
| 2614 | * to return a response. |
| 2615 | * |
| 2616 | * Temporarly set timeout to allow working with |
| 2617 | * widest range of devices. |
| 2618 | */ |
| 2619 | get_usb_device_timeout(ptp_usb, &oldtimeout); |
| 2620 | set_usb_device_timeout(ptp_usb, 60000); |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 2621 | |
Linus Walleij | fcb4342 | 2008-05-23 21:53:55 +0000 | [diff] [blame] | 2622 | ret = ptp_mtp_getobjectproplist(params, 0xffffffff, &props, &nrofprops); |
Linus Walleij | 2f62281 | 2008-08-30 22:06:58 +0000 | [diff] [blame] | 2623 | set_usb_device_timeout(ptp_usb, oldtimeout); |
Linus Walleij | 8533bf7 | 2007-08-28 10:03:59 +0000 | [diff] [blame] | 2624 | |
| 2625 | if (ret == PTP_RC_MTP_Specification_By_Group_Unsupported) { |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 2626 | // What's the point in the device implementing this command if |
Linus Walleij | 8533bf7 | 2007-08-28 10:03:59 +0000 | [diff] [blame] | 2627 | // you cannot use it to get all props for AT LEAST one object? |
| 2628 | // Well, whatever... |
| 2629 | add_ptp_error_to_errorstack(device, ret, "get_all_metadata_fast(): " |
| 2630 | "cannot retrieve all metadata for an object on this device."); |
Linus Walleij | 91e9813 | 2007-08-28 10:05:11 +0000 | [diff] [blame] | 2631 | return -1; |
Linus Walleij | 8533bf7 | 2007-08-28 10:03:59 +0000 | [diff] [blame] | 2632 | } |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 2633 | if (ret != PTP_RC_OK) { |
Linus Walleij | 8533bf7 | 2007-08-28 10:03:59 +0000 | [diff] [blame] | 2634 | add_ptp_error_to_errorstack(device, ret, "get_all_metadata_fast(): " |
| 2635 | "could not get proplist of all objects."); |
| 2636 | return -1; |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 2637 | } |
Linus Walleij | fcb4342 | 2008-05-23 21:53:55 +0000 | [diff] [blame] | 2638 | if (props == NULL && nrofprops != 0) { |
| 2639 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, |
| 2640 | "get_all_metadata_fast(): " |
| 2641 | "call to ptp_mtp_getobjectproplist() returned " |
| 2642 | "inconsistent results."); |
| 2643 | return -1; |
| 2644 | } |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 2645 | /* |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 2646 | * We count the number of objects by counting the ObjectHandle |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 2647 | * references, whenever it changes we get a new object, when it's |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 2648 | * the same, it is just different properties of the same object. |
| 2649 | */ |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 2650 | prop = props; |
| 2651 | for (i=0;i<nrofprops;i++) { |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 2652 | if (lasthandle != prop->ObjectHandle) { |
| 2653 | cnt++; |
| 2654 | lasthandle = prop->ObjectHandle; |
| 2655 | } |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 2656 | prop++; |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 2657 | } |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 2658 | lasthandle = 0xffffffff; |
Marcus Meissner | b521477 | 2015-10-04 13:08:20 +0200 | [diff] [blame] | 2659 | params->objects = calloc (cnt, sizeof(PTPObject)); |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 2660 | prop = props; |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 2661 | i = -1; |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 2662 | for (j=0;j<nrofprops;j++) { |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 2663 | if (lasthandle != prop->ObjectHandle) { |
| 2664 | if (i >= 0) { |
Linus Walleij | 77127dc | 2009-08-25 16:38:42 +0000 | [diff] [blame] | 2665 | params->objects[i].flags |= PTPOBJECT_OBJECTINFO_LOADED; |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 2666 | if (!params->objects[i].oi.Filename) { |
Linus Walleij | 6bf68b4 | 2007-09-03 22:50:02 +0000 | [diff] [blame] | 2667 | /* I have one such file on my Creative (Marcus) */ |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 2668 | params->objects[i].oi.Filename = strdup("<null>"); |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 2669 | } |
| 2670 | } |
| 2671 | i++; |
| 2672 | lasthandle = prop->ObjectHandle; |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 2673 | params->objects[i].oid = prop->ObjectHandle; |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 2674 | } |
| 2675 | switch (prop->property) { |
| 2676 | case PTP_OPC_ParentObject: |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 2677 | params->objects[i].oi.ParentObject = prop->propval.u32; |
| 2678 | params->objects[i].flags |= PTPOBJECT_PARENTOBJECT_LOADED; |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 2679 | break; |
| 2680 | case PTP_OPC_ObjectFormat: |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 2681 | params->objects[i].oi.ObjectFormat = prop->propval.u16; |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 2682 | break; |
| 2683 | case PTP_OPC_ObjectSize: |
Linus Walleij | d9d28d5 | 2007-08-04 19:01:18 +0000 | [diff] [blame] | 2684 | // We loose precision here, up to 32 bits! However the commands that |
| 2685 | // retrieve metadata for files and tracks will make sure that the |
| 2686 | // PTP_OPC_ObjectSize is read in and duplicated again. |
Linus Walleij | ddaba2f | 2007-10-02 21:20:30 +0000 | [diff] [blame] | 2687 | if (device->object_bitsize == 64) { |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 2688 | params->objects[i].oi.ObjectCompressedSize = (uint32_t) prop->propval.u64; |
Linus Walleij | ddaba2f | 2007-10-02 21:20:30 +0000 | [diff] [blame] | 2689 | } else { |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 2690 | params->objects[i].oi.ObjectCompressedSize = prop->propval.u32; |
Linus Walleij | ddaba2f | 2007-10-02 21:20:30 +0000 | [diff] [blame] | 2691 | } |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 2692 | break; |
| 2693 | case PTP_OPC_StorageID: |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 2694 | params->objects[i].oi.StorageID = prop->propval.u32; |
| 2695 | params->objects[i].flags |= PTPOBJECT_STORAGEID_LOADED; |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 2696 | break; |
| 2697 | case PTP_OPC_ObjectFileName: |
Richard Low | 8e8d9d4 | 2007-07-14 10:36:50 +0000 | [diff] [blame] | 2698 | if (prop->propval.str != NULL) |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 2699 | params->objects[i].oi.Filename = strdup(prop->propval.str); |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 2700 | break; |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 2701 | default: { |
| 2702 | MTPProperties *newprops; |
| 2703 | |
| 2704 | /* Copy all of the other MTP oprierties into the per-object proplist */ |
| 2705 | if (params->objects[i].nrofmtpprops) { |
Linus Walleij | 43aba61 | 2011-11-25 09:40:34 +0100 | [diff] [blame] | 2706 | newprops = realloc(params->objects[i].mtpprops, |
| 2707 | (params->objects[i].nrofmtpprops+1)*sizeof(MTPProperties)); |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 2708 | } else { |
Marcus Meissner | b521477 | 2015-10-04 13:08:20 +0200 | [diff] [blame] | 2709 | newprops = calloc(1,sizeof(MTPProperties)); |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 2710 | } |
| 2711 | if (!newprops) return 0; /* FIXME: error handling? */ |
| 2712 | params->objects[i].mtpprops = newprops; |
Linus Walleij | 43aba61 | 2011-11-25 09:40:34 +0100 | [diff] [blame] | 2713 | memcpy(¶ms->objects[i].mtpprops[params->objects[i].nrofmtpprops], |
| 2714 | &props[j],sizeof(props[j])); |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 2715 | params->objects[i].nrofmtpprops++; |
Linus Walleij | 362d13e | 2009-08-02 19:59:21 +0000 | [diff] [blame] | 2716 | params->objects[i].flags |= PTPOBJECT_MTPPROPLIST_LOADED; |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 2717 | break; |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 2718 | } |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 2719 | } |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 2720 | prop++; |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 2721 | } |
Linus Walleij | 77127dc | 2009-08-25 16:38:42 +0000 | [diff] [blame] | 2722 | /* mark last entry also */ |
Marcus Meissner | 7e32887 | 2017-04-10 08:39:00 +0200 | [diff] [blame] | 2723 | if (i >= 0) { |
| 2724 | params->objects[i].flags |= PTPOBJECT_OBJECTINFO_LOADED; |
| 2725 | params->nrofobjects = i+1; |
| 2726 | } else { |
| 2727 | params->nrofobjects = 0; |
| 2728 | } |
Marcus Meissner | 859f9b2 | 2015-10-04 13:20:22 +0200 | [diff] [blame] | 2729 | free (props); |
Linus Walleij | 77127dc | 2009-08-25 16:38:42 +0000 | [diff] [blame] | 2730 | /* The device might not give the list in linear ascending order */ |
| 2731 | ptp_objects_sort (params); |
Linus Walleij | 8533bf7 | 2007-08-28 10:03:59 +0000 | [diff] [blame] | 2732 | return 0; |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 2733 | } |
| 2734 | |
| 2735 | /** |
| 2736 | * This function will recurse through all the directories on the device, |
| 2737 | * starting at the root directory, gathering metadata as it moves along. |
| 2738 | * It works better on some devices that will only return data for a |
| 2739 | * certain directory and does not respect the option to get all metadata |
| 2740 | * for all objects. |
| 2741 | */ |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 2742 | static void get_handles_recursively(LIBMTP_mtpdevice_t *device, |
| 2743 | PTPParams *params, |
Linus Walleij | 6bf68b4 | 2007-09-03 22:50:02 +0000 | [diff] [blame] | 2744 | uint32_t storageid, |
| 2745 | uint32_t parent) |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 2746 | { |
| 2747 | PTPObjectHandles currentHandles; |
| 2748 | int i = 0; |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 2749 | uint16_t ret = ptp_getobjecthandles(params, |
Linus Walleij | 6bf68b4 | 2007-09-03 22:50:02 +0000 | [diff] [blame] | 2750 | storageid, |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 2751 | PTP_GOH_ALL_FORMATS, |
| 2752 | parent, |
| 2753 | ¤tHandles); |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 2754 | |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 2755 | if (ret != PTP_RC_OK) { |
| 2756 | add_ptp_error_to_errorstack(device, ret, "get_handles_recursively(): could not get object handles."); |
| 2757 | return; |
| 2758 | } |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 2759 | |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 2760 | if (currentHandles.Handler == NULL || currentHandles.n == 0) |
| 2761 | return; |
| 2762 | |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 2763 | // Now descend into any subdirectories found |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 2764 | for (i = 0; i < currentHandles.n; i++) { |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 2765 | PTPObject *ob; |
Linus Walleij | 43aba61 | 2011-11-25 09:40:34 +0100 | [diff] [blame] | 2766 | ret = ptp_object_want(params,currentHandles.Handler[i], |
| 2767 | PTPOBJECT_OBJECTINFO_LOADED, &ob); |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 2768 | if (ret == PTP_RC_OK) { |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 2769 | if (ob->oi.ObjectFormat == PTP_OFC_Association) |
Linus Walleij | 43aba61 | 2011-11-25 09:40:34 +0100 | [diff] [blame] | 2770 | get_handles_recursively(device, params, |
| 2771 | storageid, currentHandles.Handler[i]); |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 2772 | } else { |
| 2773 | add_error_to_errorstack(device, |
| 2774 | LIBMTP_ERROR_CONNECTING, |
| 2775 | "Found a bad handle, trying to ignore it."); |
| 2776 | } |
| 2777 | } |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 2778 | free(currentHandles.Handler); |
| 2779 | } |
| 2780 | |
| 2781 | /** |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 2782 | * This function refresh the internal handle list whenever |
| 2783 | * the items stored inside the device is altered. On operations |
| 2784 | * that do not add or remove objects, this is typically not |
| 2785 | * called. |
| 2786 | * @param device a pointer to the MTP device to flush handles for. |
| 2787 | */ |
| 2788 | static void flush_handles(LIBMTP_mtpdevice_t *device) |
| 2789 | { |
| 2790 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 2791 | PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo; |
Linus Walleij | 8533bf7 | 2007-08-28 10:03:59 +0000 | [diff] [blame] | 2792 | int ret; |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 2793 | uint32_t i; |
| 2794 | |
Linus Walleij | a8b8889 | 2011-03-03 19:58:26 +0100 | [diff] [blame] | 2795 | if (!device->cached) { |
| 2796 | return; |
| 2797 | } |
| 2798 | |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 2799 | if (params->objects != NULL) { |
| 2800 | for (i=0;i<params->nrofobjects;i++) |
| 2801 | ptp_free_object (¶ms->objects[i]); |
| 2802 | free(params->objects); |
| 2803 | params->objects = NULL; |
| 2804 | params->nrofobjects = 0; |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 2805 | } |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 2806 | |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 2807 | if (ptp_operation_issupported(params,PTP_OC_MTP_GetObjPropList) |
Linus Walleij | fec4d56 | 2008-06-01 22:30:36 +0000 | [diff] [blame] | 2808 | && !FLAG_BROKEN_MTPGETOBJPROPLIST(ptp_usb) |
| 2809 | && !FLAG_BROKEN_MTPGETOBJPROPLIST_ALL(ptp_usb)) { |
Linus Walleij | 139455e | 2007-08-28 10:25:06 +0000 | [diff] [blame] | 2810 | // Use the fast method. Ignore return value for now. |
Catalin Patulea | a23fce3 | 2012-07-17 22:25:12 -0400 | [diff] [blame] | 2811 | ret = get_all_metadata_fast(device); |
Linus Walleij | 8533bf7 | 2007-08-28 10:03:59 +0000 | [diff] [blame] | 2812 | } |
Linus Walleij | a8b8889 | 2011-03-03 19:58:26 +0100 | [diff] [blame] | 2813 | |
Linus Walleij | 139455e | 2007-08-28 10:25:06 +0000 | [diff] [blame] | 2814 | // If the previous failed or returned no objects, use classic |
| 2815 | // methods instead. |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 2816 | if (params->nrofobjects == 0) { |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 2817 | // Get all the handles using just standard commands. |
Linus Walleij | 6bf68b4 | 2007-09-03 22:50:02 +0000 | [diff] [blame] | 2818 | if (device->storage == NULL) { |
| 2819 | get_handles_recursively(device, params, |
Linus Walleij | 6bf68b4 | 2007-09-03 22:50:02 +0000 | [diff] [blame] | 2820 | PTP_GOH_ALL_STORAGE, |
| 2821 | PTP_GOH_ROOT_PARENT); |
| 2822 | } else { |
| 2823 | // Get handles for each storage in turn. |
| 2824 | LIBMTP_devicestorage_t *storage = device->storage; |
| 2825 | while(storage != NULL) { |
| 2826 | get_handles_recursively(device, params, |
Linus Walleij | 6bf68b4 | 2007-09-03 22:50:02 +0000 | [diff] [blame] | 2827 | storage->id, |
| 2828 | PTP_GOH_ROOT_PARENT); |
| 2829 | storage = storage->next; |
| 2830 | } |
| 2831 | } |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 2832 | } |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 2833 | |
Linus Walleij | 8f7f1aa | 2008-06-15 19:00:23 +0000 | [diff] [blame] | 2834 | /* |
| 2835 | * Loop over the handles, fix up any NULL filenames or |
| 2836 | * keywords, then attempt to locate some default folders |
| 2837 | * in the root directory of the primary storage. |
| 2838 | */ |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 2839 | for(i = 0; i < params->nrofobjects; i++) { |
| 2840 | PTPObject *ob, *xob; |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 2841 | |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 2842 | ob = ¶ms->objects[i]; |
Linus Walleij | 43aba61 | 2011-11-25 09:40:34 +0100 | [diff] [blame] | 2843 | ret = ptp_object_want(params,params->objects[i].oid, |
| 2844 | PTPOBJECT_OBJECTINFO_LOADED, &xob); |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 2845 | if (ret != PTP_RC_OK) { |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 2846 | LIBMTP_ERROR("broken! %x not found\n", params->objects[i].oid); |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 2847 | } |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 2848 | if (ob->oi.Filename == NULL) |
| 2849 | ob->oi.Filename = strdup("<null>"); |
| 2850 | if (ob->oi.Keywords == NULL) |
| 2851 | ob->oi.Keywords = strdup("<null>"); |
| 2852 | |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 2853 | /* Ignore handles that point to non-folders */ |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 2854 | if(ob->oi.ObjectFormat != PTP_OFC_Association) |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 2855 | continue; |
Linus Walleij | 8f7f1aa | 2008-06-15 19:00:23 +0000 | [diff] [blame] | 2856 | /* Only look in the root folder */ |
Linus Walleij | 4fc1917 | 2010-01-19 00:10:26 +0000 | [diff] [blame] | 2857 | if (ob->oi.ParentObject == 0xffffffffU) { |
Linus Walleij | 43aba61 | 2011-11-25 09:40:34 +0100 | [diff] [blame] | 2858 | LIBMTP_ERROR("object %x has parent 0xffffffff (-1) continuing anyway\n", |
| 2859 | ob->oid); |
Linus Walleij | 4fc1917 | 2010-01-19 00:10:26 +0000 | [diff] [blame] | 2860 | } else if (ob->oi.ParentObject != 0x00000000U) |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 2861 | continue; |
Linus Walleij | c40c9bf | 2008-06-13 23:24:17 +0000 | [diff] [blame] | 2862 | /* Only look in the primary storage */ |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 2863 | if (device->storage != NULL && ob->oi.StorageID != device->storage->id) |
Linus Walleij | c40c9bf | 2008-06-13 23:24:17 +0000 | [diff] [blame] | 2864 | continue; |
Linus Walleij | 8f7f1aa | 2008-06-15 19:00:23 +0000 | [diff] [blame] | 2865 | |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 2866 | /* Is this the Music Folder */ |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 2867 | if (!strcasecmp(ob->oi.Filename, "My Music") || |
Linus Walleij | 4c181a8 | 2010-01-07 09:21:49 +0000 | [diff] [blame] | 2868 | !strcasecmp(ob->oi.Filename, "My_Music") || |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 2869 | !strcasecmp(ob->oi.Filename, "Music")) { |
| 2870 | device->default_music_folder = ob->oid; |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 2871 | } |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 2872 | else if (!strcasecmp(ob->oi.Filename, "My Playlists") || |
Linus Walleij | 4c181a8 | 2010-01-07 09:21:49 +0000 | [diff] [blame] | 2873 | !strcasecmp(ob->oi.Filename, "My_Playlists") || |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 2874 | !strcasecmp(ob->oi.Filename, "Playlists")) { |
| 2875 | device->default_playlist_folder = ob->oid; |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 2876 | } |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 2877 | else if (!strcasecmp(ob->oi.Filename, "My Pictures") || |
Linus Walleij | 4c181a8 | 2010-01-07 09:21:49 +0000 | [diff] [blame] | 2878 | !strcasecmp(ob->oi.Filename, "My_Pictures") || |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 2879 | !strcasecmp(ob->oi.Filename, "Pictures")) { |
| 2880 | device->default_picture_folder = ob->oid; |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 2881 | } |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 2882 | else if (!strcasecmp(ob->oi.Filename, "My Video") || |
Linus Walleij | 4c181a8 | 2010-01-07 09:21:49 +0000 | [diff] [blame] | 2883 | !strcasecmp(ob->oi.Filename, "My_Video") || |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 2884 | !strcasecmp(ob->oi.Filename, "Video")) { |
| 2885 | device->default_video_folder = ob->oid; |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 2886 | } |
Linus Walleij | 4c181a8 | 2010-01-07 09:21:49 +0000 | [diff] [blame] | 2887 | else if (!strcasecmp(ob->oi.Filename, "My Organizer") || |
| 2888 | !strcasecmp(ob->oi.Filename, "My_Organizer")) { |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 2889 | device->default_organizer_folder = ob->oid; |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 2890 | } |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 2891 | else if (!strcasecmp(ob->oi.Filename, "ZENcast") || |
| 2892 | !strcasecmp(ob->oi.Filename, "Datacasts")) { |
| 2893 | device->default_zencast_folder = ob->oid; |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 2894 | } |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 2895 | else if (!strcasecmp(ob->oi.Filename, "My Albums") || |
Linus Walleij | 4c181a8 | 2010-01-07 09:21:49 +0000 | [diff] [blame] | 2896 | !strcasecmp(ob->oi.Filename, "My_Albums") || |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 2897 | !strcasecmp(ob->oi.Filename, "Albums")) { |
| 2898 | device->default_album_folder = ob->oid; |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 2899 | } |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 2900 | else if (!strcasecmp(ob->oi.Filename, "Text") || |
| 2901 | !strcasecmp(ob->oi.Filename, "Texts")) { |
| 2902 | device->default_text_folder = ob->oid; |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 2903 | } |
| 2904 | } |
Richard Low | 6711f44 | 2007-05-05 19:00:59 +0000 | [diff] [blame] | 2905 | } |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 2906 | |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 2907 | /** |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 2908 | * This function traverses a devices storage list freeing up the |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 2909 | * strings and the structs. |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 2910 | * @param device a pointer to the MTP device to free the storage |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 2911 | * list for. |
| 2912 | */ |
| 2913 | static void free_storage_list(LIBMTP_mtpdevice_t *device) |
| 2914 | { |
Linus Walleij | e1ac07e | 2006-12-14 19:38:59 +0000 | [diff] [blame] | 2915 | LIBMTP_devicestorage_t *storage; |
| 2916 | LIBMTP_devicestorage_t *tmp; |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 2917 | |
Linus Walleij | e1ac07e | 2006-12-14 19:38:59 +0000 | [diff] [blame] | 2918 | storage = device->storage; |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 2919 | while(storage != NULL) { |
Linus Walleij | e1ac07e | 2006-12-14 19:38:59 +0000 | [diff] [blame] | 2920 | if (storage->StorageDescription != NULL) { |
| 2921 | free(storage->StorageDescription); |
| 2922 | } |
| 2923 | if (storage->VolumeIdentifier != NULL) { |
| 2924 | free(storage->VolumeIdentifier); |
| 2925 | } |
| 2926 | tmp = storage; |
| 2927 | storage = storage->next; |
| 2928 | free(tmp); |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 2929 | } |
| 2930 | device->storage = NULL; |
| 2931 | |
| 2932 | return; |
| 2933 | } |
| 2934 | |
| 2935 | /** |
| 2936 | * This function traverses a devices storage list freeing up the |
| 2937 | * strings and the structs. |
| 2938 | * @param device a pointer to the MTP device to free the storage |
| 2939 | * list for. |
| 2940 | */ |
| 2941 | static int sort_storage_by(LIBMTP_mtpdevice_t *device,int const sortby) |
| 2942 | { |
| 2943 | LIBMTP_devicestorage_t *oldhead, *ptr1, *ptr2, *newlist; |
| 2944 | |
| 2945 | if (device->storage == NULL) |
| 2946 | return -1; |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 2947 | if (sortby == LIBMTP_STORAGE_SORTBY_NOTSORTED) |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 2948 | return 0; |
| 2949 | |
| 2950 | oldhead = ptr1 = ptr2 = device->storage; |
| 2951 | |
| 2952 | newlist = NULL; |
| 2953 | |
| 2954 | while(oldhead != NULL) { |
| 2955 | ptr1 = ptr2 = oldhead; |
| 2956 | while(ptr1 != NULL) { |
| 2957 | |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 2958 | if (sortby == LIBMTP_STORAGE_SORTBY_FREESPACE && ptr1->FreeSpaceInBytes > ptr2->FreeSpaceInBytes) |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 2959 | ptr2 = ptr1; |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 2960 | if (sortby == LIBMTP_STORAGE_SORTBY_MAXSPACE && ptr1->FreeSpaceInBytes > ptr2->FreeSpaceInBytes) |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 2961 | ptr2 = ptr1; |
| 2962 | |
| 2963 | ptr1 = ptr1->next; |
| 2964 | } |
| 2965 | |
| 2966 | // Make our previous entries next point to our next |
| 2967 | if(ptr2->prev != NULL) { |
| 2968 | ptr1 = ptr2->prev; |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 2969 | ptr1->next = ptr2->next; |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 2970 | } else { |
| 2971 | oldhead = ptr2->next; |
| 2972 | if(oldhead != NULL) |
| 2973 | oldhead->prev = NULL; |
| 2974 | } |
| 2975 | |
| 2976 | // Make our next entries previous point to our previous |
| 2977 | ptr1 = ptr2->next; |
| 2978 | if(ptr1 != NULL) { |
| 2979 | ptr1->prev = ptr2->prev; |
| 2980 | } else { |
| 2981 | ptr1 = ptr2->prev; |
| 2982 | if(ptr1 != NULL) |
| 2983 | ptr1->next = NULL; |
| 2984 | } |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 2985 | |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 2986 | if(newlist == NULL) { |
| 2987 | newlist = ptr2; |
| 2988 | newlist->prev = NULL; |
| 2989 | } else { |
| 2990 | ptr2->prev = newlist; |
| 2991 | newlist->next = ptr2; |
| 2992 | newlist = newlist->next; |
| 2993 | } |
| 2994 | } |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 2995 | |
Linus Walleij | 5c1499e | 2009-02-21 06:54:29 +0000 | [diff] [blame] | 2996 | if (newlist != NULL) { |
| 2997 | newlist->next = NULL; |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 2998 | while(newlist->prev != NULL) |
Linus Walleij | 5c1499e | 2009-02-21 06:54:29 +0000 | [diff] [blame] | 2999 | newlist = newlist->prev; |
| 3000 | device->storage = newlist; |
| 3001 | } |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 3002 | |
| 3003 | return 0; |
| 3004 | } |
| 3005 | |
| 3006 | /** |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 3007 | * This function grabs the first writeable storageid from the |
Linus Walleij | d71d0b3 | 2008-09-22 08:21:03 +0000 | [diff] [blame] | 3008 | * device storage list. |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 3009 | * @param device a pointer to the MTP device to locate writeable |
Linus Walleij | d71d0b3 | 2008-09-22 08:21:03 +0000 | [diff] [blame] | 3010 | * storage for. |
| 3011 | * @param fitsize a file of this file must fit on the device. |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 3012 | */ |
Linus Walleij | 88babc8 | 2014-06-02 21:32:14 +0200 | [diff] [blame] | 3013 | static uint32_t get_writeable_storageid(LIBMTP_mtpdevice_t *device, |
| 3014 | uint64_t fitsize) |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 3015 | { |
Linus Walleij | 5c1499e | 2009-02-21 06:54:29 +0000 | [diff] [blame] | 3016 | LIBMTP_devicestorage_t *storage; |
Linus Walleij | fb28b63 | 2007-10-23 21:56:18 +0000 | [diff] [blame] | 3017 | uint32_t store = 0x00000000; //Should this be 0xffffffffu instead? |
Linus Walleij | d71d0b3 | 2008-09-22 08:21:03 +0000 | [diff] [blame] | 3018 | int subcall_ret; |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 3019 | |
Linus Walleij | d71d0b3 | 2008-09-22 08:21:03 +0000 | [diff] [blame] | 3020 | // See if there is some storage we can fit this file on. |
| 3021 | storage = device->storage; |
| 3022 | if (storage == NULL) { |
| 3023 | // Sometimes the storage just cannot be detected. |
| 3024 | store = 0x00000000U; |
| 3025 | } else { |
| 3026 | while(storage != NULL) { |
| 3027 | // These storages cannot be used. |
Linus Walleij | 43aba61 | 2011-11-25 09:40:34 +0100 | [diff] [blame] | 3028 | if (storage->StorageType == PTP_ST_FixedROM || |
| 3029 | storage->StorageType == PTP_ST_RemovableROM) { |
Linus Walleij | d71d0b3 | 2008-09-22 08:21:03 +0000 | [diff] [blame] | 3030 | storage = storage->next; |
| 3031 | continue; |
| 3032 | } |
| 3033 | // Storage IDs with the lower 16 bits 0x0000 are not supposed |
| 3034 | // to be writeable. |
| 3035 | if ((storage->id & 0x0000FFFFU) == 0x00000000U) { |
| 3036 | storage = storage->next; |
| 3037 | continue; |
| 3038 | } |
| 3039 | // Also check the access capability to avoid e.g. deletable only storages |
Linus Walleij | 43aba61 | 2011-11-25 09:40:34 +0100 | [diff] [blame] | 3040 | if (storage->AccessCapability == PTP_AC_ReadOnly || |
| 3041 | storage->AccessCapability == PTP_AC_ReadOnly_with_Object_Deletion) { |
Linus Walleij | d71d0b3 | 2008-09-22 08:21:03 +0000 | [diff] [blame] | 3042 | storage = storage->next; |
| 3043 | continue; |
| 3044 | } |
| 3045 | // Then see if we can fit the file. |
| 3046 | subcall_ret = check_if_file_fits(device, storage, fitsize); |
| 3047 | if (subcall_ret != 0) { |
| 3048 | storage = storage->next; |
| 3049 | } else { |
| 3050 | // We found a storage that is writable and can fit the file! |
| 3051 | break; |
| 3052 | } |
| 3053 | } |
| 3054 | if (storage == NULL) { |
Linus Walleij | 43aba61 | 2011-11-25 09:40:34 +0100 | [diff] [blame] | 3055 | add_error_to_errorstack(device, LIBMTP_ERROR_STORAGE_FULL, |
| 3056 | "get_writeable_storageid(): " |
Linus Walleij | d71d0b3 | 2008-09-22 08:21:03 +0000 | [diff] [blame] | 3057 | "all device storage is full or corrupt."); |
| 3058 | return -1; |
| 3059 | } |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 3060 | store = storage->id; |
Linus Walleij | d71d0b3 | 2008-09-22 08:21:03 +0000 | [diff] [blame] | 3061 | } |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 3062 | |
| 3063 | return store; |
| 3064 | } |
| 3065 | |
| 3066 | /** |
Linus Walleij | 88babc8 | 2014-06-02 21:32:14 +0200 | [diff] [blame] | 3067 | * Tries to suggest a storage_id of a given ID when we have a parent |
| 3068 | * @param device a pointer to the device where to search for the storage ID |
| 3069 | * @param fitsize a file of this file must fit on the device. |
| 3070 | * @param parent_id look for this ID |
| 3071 | * @ret storageID |
| 3072 | */ |
| 3073 | static int get_suggested_storage_id(LIBMTP_mtpdevice_t *device, |
| 3074 | uint64_t fitsize, |
| 3075 | uint32_t parent_id) |
| 3076 | { |
| 3077 | PTPParams *params = (PTPParams *) device->params; |
| 3078 | PTPObject *ob; |
| 3079 | uint16_t ret; |
Linus Walleij | 88babc8 | 2014-06-02 21:32:14 +0200 | [diff] [blame] | 3080 | |
| 3081 | ret = ptp_object_want(params, parent_id, PTPOBJECT_MTPPROPLIST_LOADED, &ob); |
| 3082 | if ((ret != PTP_RC_OK) || (ob->oi.StorageID == 0)) { |
| 3083 | add_ptp_error_to_errorstack(device, ret, "get_suggested_storage_id(): " |
| 3084 | "could not get storage id from parent id."); |
| 3085 | return get_writeable_storageid(device, fitsize); |
| 3086 | } else { |
| 3087 | /* OK we know the parent storage, then use that */ |
| 3088 | return ob->oi.StorageID; |
| 3089 | } |
| 3090 | } |
| 3091 | |
| 3092 | /** |
Linus Walleij | 6bf68b4 | 2007-09-03 22:50:02 +0000 | [diff] [blame] | 3093 | * This function grabs the freespace from a certain storage in |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 3094 | * device storage list. |
| 3095 | * @param device a pointer to the MTP device to free the storage |
| 3096 | * list for. |
Linus Walleij | 6bf68b4 | 2007-09-03 22:50:02 +0000 | [diff] [blame] | 3097 | * @param storageid the storage ID for the storage to flush and |
| 3098 | * get free space for. |
| 3099 | * @param freespace the free space on this storage will be returned |
| 3100 | * in this variable. |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 3101 | */ |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 3102 | static int get_storage_freespace(LIBMTP_mtpdevice_t *device, |
Linus Walleij | 6bf68b4 | 2007-09-03 22:50:02 +0000 | [diff] [blame] | 3103 | LIBMTP_devicestorage_t *storage, |
| 3104 | uint64_t *freespace) |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 3105 | { |
Linus Walleij | e1ac07e | 2006-12-14 19:38:59 +0000 | [diff] [blame] | 3106 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 3107 | |
Linus Walleij | e1ac07e | 2006-12-14 19:38:59 +0000 | [diff] [blame] | 3108 | // Always query the device about this, since some models explicitly |
Linus Walleij | 6bf68b4 | 2007-09-03 22:50:02 +0000 | [diff] [blame] | 3109 | // needs that. We flush all data on queries storage here. |
Linus Walleij | e1ac07e | 2006-12-14 19:38:59 +0000 | [diff] [blame] | 3110 | if (ptp_operation_issupported(params,PTP_OC_GetStorageInfo)) { |
| 3111 | PTPStorageInfo storageInfo; |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 3112 | uint16_t ret; |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 3113 | |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 3114 | ret = ptp_getstorageinfo(params, storage->id, &storageInfo); |
| 3115 | if (ret != PTP_RC_OK) { |
Linus Walleij | 43aba61 | 2011-11-25 09:40:34 +0100 | [diff] [blame] | 3116 | add_ptp_error_to_errorstack(device, ret, |
| 3117 | "get_storage_freespace(): could not get storage info."); |
Linus Walleij | e1ac07e | 2006-12-14 19:38:59 +0000 | [diff] [blame] | 3118 | return -1; |
| 3119 | } |
| 3120 | if (storage->StorageDescription != NULL) { |
| 3121 | free(storage->StorageDescription); |
| 3122 | } |
| 3123 | if (storage->VolumeIdentifier != NULL) { |
| 3124 | free(storage->VolumeIdentifier); |
| 3125 | } |
| 3126 | storage->StorageType = storageInfo.StorageType; |
| 3127 | storage->FilesystemType = storageInfo.FilesystemType; |
| 3128 | storage->AccessCapability = storageInfo.AccessCapability; |
| 3129 | storage->MaxCapacity = storageInfo.MaxCapability; |
| 3130 | storage->FreeSpaceInBytes = storageInfo.FreeSpaceInBytes; |
| 3131 | storage->FreeSpaceInObjects = storageInfo.FreeSpaceInImages; |
| 3132 | storage->StorageDescription = storageInfo.StorageDescription; |
| 3133 | storage->VolumeIdentifier = storageInfo.VolumeLabel; |
| 3134 | } |
| 3135 | if(storage->FreeSpaceInBytes == (uint64_t) -1) |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 3136 | return -1; |
| 3137 | *freespace = storage->FreeSpaceInBytes; |
| 3138 | return 0; |
| 3139 | } |
| 3140 | |
| 3141 | /** |
Linus Walleij | 8c45b29 | 2006-04-26 14:12:44 +0000 | [diff] [blame] | 3142 | * This function dumps out a large chunk of textual information |
| 3143 | * provided from the PTP protocol and additionally some extra |
| 3144 | * MTP-specific information where applicable. |
| 3145 | * @param device a pointer to the MTP device to report info from. |
| 3146 | */ |
| 3147 | void LIBMTP_Dump_Device_Info(LIBMTP_mtpdevice_t *device) |
| 3148 | { |
| 3149 | int i; |
| 3150 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | c6210fb | 2006-05-08 11:11:41 +0000 | [diff] [blame] | 3151 | PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo; |
Linus Walleij | e1ac07e | 2006-12-14 19:38:59 +0000 | [diff] [blame] | 3152 | LIBMTP_devicestorage_t *storage = device->storage; |
Linus Walleij | 38d7ee8 | 2010-07-24 22:17:21 +0000 | [diff] [blame] | 3153 | LIBMTP_device_extension_t *tmpext = device->extensions; |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 3154 | |
Linus Walleij | c6210fb | 2006-05-08 11:11:41 +0000 | [diff] [blame] | 3155 | printf("USB low-level info:\n"); |
| 3156 | dump_usbinfo(ptp_usb); |
Linus Walleij | 8c45b29 | 2006-04-26 14:12:44 +0000 | [diff] [blame] | 3157 | /* Print out some verbose information */ |
| 3158 | printf("Device info:\n"); |
| 3159 | printf(" Manufacturer: %s\n", params->deviceinfo.Manufacturer); |
| 3160 | printf(" Model: %s\n", params->deviceinfo.Model); |
| 3161 | printf(" Device version: %s\n", params->deviceinfo.DeviceVersion); |
| 3162 | printf(" Serial number: %s\n", params->deviceinfo.SerialNumber); |
Linus Walleij | 43aba61 | 2011-11-25 09:40:34 +0100 | [diff] [blame] | 3163 | printf(" Vendor extension ID: 0x%08x\n", |
| 3164 | params->deviceinfo.VendorExtensionID); |
| 3165 | printf(" Vendor extension description: %s\n", |
| 3166 | params->deviceinfo.VendorExtensionDesc); |
| 3167 | printf(" Detected object size: %d bits\n", |
| 3168 | device->object_bitsize); |
Linus Walleij | 38d7ee8 | 2010-07-24 22:17:21 +0000 | [diff] [blame] | 3169 | printf(" Extensions:\n"); |
| 3170 | while (tmpext != NULL) { |
| 3171 | printf(" %s: %d.%d\n", |
| 3172 | tmpext->name, |
| 3173 | tmpext->major, |
| 3174 | tmpext->minor); |
| 3175 | tmpext = tmpext->next; |
| 3176 | } |
Linus Walleij | 8c45b29 | 2006-04-26 14:12:44 +0000 | [diff] [blame] | 3177 | printf("Supported operations:\n"); |
Marcus Meissner | 5e6ef6c | 2016-11-18 21:21:15 +0100 | [diff] [blame] | 3178 | for (i=0;i<params->deviceinfo.OperationsSupported_len;i++) |
| 3179 | printf(" %04x: %s\n", params->deviceinfo.OperationsSupported[i], ptp_get_opcode_name(params, params->deviceinfo.OperationsSupported[i])); |
Linus Walleij | 8c45b29 | 2006-04-26 14:12:44 +0000 | [diff] [blame] | 3180 | printf("Events supported:\n"); |
| 3181 | if (params->deviceinfo.EventsSupported_len == 0) { |
| 3182 | printf(" None.\n"); |
| 3183 | } else { |
| 3184 | for (i=0;i<params->deviceinfo.EventsSupported_len;i++) { |
Marcus Meissner | 5e6ef6c | 2016-11-18 21:21:15 +0100 | [diff] [blame] | 3185 | printf(" 0x%04x (%s)\n", params->deviceinfo.EventsSupported[i], ptp_strerror(params->deviceinfo.EventsSupported[i], params->deviceinfo.VendorExtensionID)); |
Linus Walleij | 8c45b29 | 2006-04-26 14:12:44 +0000 | [diff] [blame] | 3186 | } |
| 3187 | } |
| 3188 | printf("Device Properties Supported:\n"); |
| 3189 | for (i=0;i<params->deviceinfo.DevicePropertiesSupported_len;i++) { |
Linus Walleij | 43aba61 | 2011-11-25 09:40:34 +0100 | [diff] [blame] | 3190 | char const *propdesc = ptp_get_property_description(params, |
| 3191 | params->deviceinfo.DevicePropertiesSupported[i]); |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 3192 | |
Linus Walleij | 545c779 | 2006-06-13 15:22:30 +0000 | [diff] [blame] | 3193 | if (propdesc != NULL) { |
Linus Walleij | 43aba61 | 2011-11-25 09:40:34 +0100 | [diff] [blame] | 3194 | printf(" 0x%04x: %s\n", |
| 3195 | params->deviceinfo.DevicePropertiesSupported[i], propdesc); |
Linus Walleij | 545c779 | 2006-06-13 15:22:30 +0000 | [diff] [blame] | 3196 | } else { |
| 3197 | uint16_t prop = params->deviceinfo.DevicePropertiesSupported[i]; |
Linus Walleij | cf223e6 | 2006-06-19 09:31:53 +0000 | [diff] [blame] | 3198 | printf(" 0x%04x: Unknown property\n", prop); |
Linus Walleij | 545c779 | 2006-06-13 15:22:30 +0000 | [diff] [blame] | 3199 | } |
Linus Walleij | 8c45b29 | 2006-04-26 14:12:44 +0000 | [diff] [blame] | 3200 | } |
Linus Walleij | 0af979a | 2006-06-19 11:49:10 +0000 | [diff] [blame] | 3201 | |
| 3202 | if (ptp_operation_issupported(params,PTP_OC_MTP_GetObjectPropsSupported)) { |
| 3203 | printf("Playable File (Object) Types and Object Properties Supported:\n"); |
| 3204 | for (i=0;i<params->deviceinfo.ImageFormats_len;i++) { |
| 3205 | char txt[256]; |
| 3206 | uint16_t ret; |
| 3207 | uint16_t *props = NULL; |
| 3208 | uint32_t propcnt = 0; |
| 3209 | int j; |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 3210 | |
Linus Walleij | 43aba61 | 2011-11-25 09:40:34 +0100 | [diff] [blame] | 3211 | (void) ptp_render_ofc (params, params->deviceinfo.ImageFormats[i], |
| 3212 | sizeof(txt), txt); |
Linus Walleij | 0af979a | 2006-06-19 11:49:10 +0000 | [diff] [blame] | 3213 | printf(" %04x: %s\n", params->deviceinfo.ImageFormats[i], txt); |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 3214 | |
Linus Walleij | 43aba61 | 2011-11-25 09:40:34 +0100 | [diff] [blame] | 3215 | ret = ptp_mtp_getobjectpropssupported (params, |
| 3216 | params->deviceinfo.ImageFormats[i], &propcnt, &props); |
Linus Walleij | 0af979a | 2006-06-19 11:49:10 +0000 | [diff] [blame] | 3217 | if (ret != PTP_RC_OK) { |
Linus Walleij | 43aba61 | 2011-11-25 09:40:34 +0100 | [diff] [blame] | 3218 | add_ptp_error_to_errorstack(device, ret, "LIBMTP_Dump_Device_Info(): " |
| 3219 | "error on query for object properties."); |
Linus Walleij | 0af979a | 2006-06-19 11:49:10 +0000 | [diff] [blame] | 3220 | } else { |
| 3221 | for (j=0;j<propcnt;j++) { |
Linus Walleij | 1483034 | 2007-03-23 13:32:00 +0000 | [diff] [blame] | 3222 | PTPObjectPropDesc opd; |
| 3223 | int k; |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 3224 | |
Linus Walleij | 43aba61 | 2011-11-25 09:40:34 +0100 | [diff] [blame] | 3225 | printf(" %04x: %s", props[j], |
| 3226 | LIBMTP_Get_Property_Description(map_ptp_property_to_libmtp_property(props[j]))); |
Linus Walleij | 1483034 | 2007-03-23 13:32:00 +0000 | [diff] [blame] | 3227 | // Get a more verbose description |
Linus Walleij | 43aba61 | 2011-11-25 09:40:34 +0100 | [diff] [blame] | 3228 | ret = ptp_mtp_getobjectpropdesc(params, props[j], |
| 3229 | params->deviceinfo.ImageFormats[i], |
| 3230 | &opd); |
Linus Walleij | 1483034 | 2007-03-23 13:32:00 +0000 | [diff] [blame] | 3231 | if (ret != PTP_RC_OK) { |
Linus Walleij | 43aba61 | 2011-11-25 09:40:34 +0100 | [diff] [blame] | 3232 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, |
| 3233 | "LIBMTP_Dump_Device_Info(): " |
Linus Walleij | 1483034 | 2007-03-23 13:32:00 +0000 | [diff] [blame] | 3234 | "could not get property description."); |
| 3235 | break; |
| 3236 | } |
| 3237 | |
| 3238 | if (opd.DataType == PTP_DTC_STR) { |
| 3239 | printf(" STRING data type"); |
Linus Walleij | 22cc487 | 2007-11-05 22:57:19 +0000 | [diff] [blame] | 3240 | switch (opd.FormFlag) { |
| 3241 | case PTP_OPFF_DateTime: |
| 3242 | printf(" DATETIME FORM"); |
| 3243 | break; |
| 3244 | case PTP_OPFF_RegularExpression: |
| 3245 | printf(" REGULAR EXPRESSION FORM"); |
| 3246 | break; |
| 3247 | case PTP_OPFF_LongString: |
| 3248 | printf(" LONG STRING FORM"); |
| 3249 | break; |
| 3250 | default: |
| 3251 | break; |
| 3252 | } |
Linus Walleij | 1483034 | 2007-03-23 13:32:00 +0000 | [diff] [blame] | 3253 | } else { |
| 3254 | if (opd.DataType & PTP_DTC_ARRAY_MASK) { |
| 3255 | printf(" array of"); |
| 3256 | } |
| 3257 | |
| 3258 | switch (opd.DataType & (~PTP_DTC_ARRAY_MASK)) { |
| 3259 | |
| 3260 | case PTP_DTC_UNDEF: |
| 3261 | printf(" UNDEFINED data type"); |
| 3262 | break; |
Linus Walleij | 1483034 | 2007-03-23 13:32:00 +0000 | [diff] [blame] | 3263 | case PTP_DTC_INT8: |
| 3264 | printf(" INT8 data type"); |
| 3265 | switch (opd.FormFlag) { |
Linus Walleij | 22cc487 | 2007-11-05 22:57:19 +0000 | [diff] [blame] | 3266 | case PTP_OPFF_Range: |
Linus Walleij | 1483034 | 2007-03-23 13:32:00 +0000 | [diff] [blame] | 3267 | printf(" range: MIN %d, MAX %d, STEP %d", |
| 3268 | opd.FORM.Range.MinimumValue.i8, |
| 3269 | opd.FORM.Range.MaximumValue.i8, |
| 3270 | opd.FORM.Range.StepSize.i8); |
| 3271 | break; |
Linus Walleij | 22cc487 | 2007-11-05 22:57:19 +0000 | [diff] [blame] | 3272 | case PTP_OPFF_Enumeration: |
Linus Walleij | 1483034 | 2007-03-23 13:32:00 +0000 | [diff] [blame] | 3273 | printf(" enumeration: "); |
| 3274 | for(k=0;k<opd.FORM.Enum.NumberOfValues;k++) { |
| 3275 | printf("%d, ", opd.FORM.Enum.SupportedValue[k].i8); |
| 3276 | } |
| 3277 | break; |
Linus Walleij | 22cc487 | 2007-11-05 22:57:19 +0000 | [diff] [blame] | 3278 | case PTP_OPFF_ByteArray: |
| 3279 | printf(" byte array: "); |
| 3280 | break; |
Linus Walleij | 1483034 | 2007-03-23 13:32:00 +0000 | [diff] [blame] | 3281 | default: |
| 3282 | printf(" ANY 8BIT VALUE form"); |
| 3283 | break; |
| 3284 | } |
| 3285 | break; |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 3286 | |
Linus Walleij | 1483034 | 2007-03-23 13:32:00 +0000 | [diff] [blame] | 3287 | case PTP_DTC_UINT8: |
| 3288 | printf(" UINT8 data type"); |
| 3289 | switch (opd.FormFlag) { |
Linus Walleij | 22cc487 | 2007-11-05 22:57:19 +0000 | [diff] [blame] | 3290 | case PTP_OPFF_Range: |
Linus Walleij | 1483034 | 2007-03-23 13:32:00 +0000 | [diff] [blame] | 3291 | printf(" range: MIN %d, MAX %d, STEP %d", |
| 3292 | opd.FORM.Range.MinimumValue.u8, |
| 3293 | opd.FORM.Range.MaximumValue.u8, |
| 3294 | opd.FORM.Range.StepSize.u8); |
| 3295 | break; |
Linus Walleij | 22cc487 | 2007-11-05 22:57:19 +0000 | [diff] [blame] | 3296 | case PTP_OPFF_Enumeration: |
Linus Walleij | 1483034 | 2007-03-23 13:32:00 +0000 | [diff] [blame] | 3297 | printf(" enumeration: "); |
| 3298 | for(k=0;k<opd.FORM.Enum.NumberOfValues;k++) { |
| 3299 | printf("%d, ", opd.FORM.Enum.SupportedValue[k].u8); |
| 3300 | } |
| 3301 | break; |
Linus Walleij | 22cc487 | 2007-11-05 22:57:19 +0000 | [diff] [blame] | 3302 | case PTP_OPFF_ByteArray: |
| 3303 | printf(" byte array: "); |
| 3304 | break; |
Linus Walleij | 1483034 | 2007-03-23 13:32:00 +0000 | [diff] [blame] | 3305 | default: |
| 3306 | printf(" ANY 8BIT VALUE form"); |
| 3307 | break; |
| 3308 | } |
| 3309 | break; |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 3310 | |
Linus Walleij | 1483034 | 2007-03-23 13:32:00 +0000 | [diff] [blame] | 3311 | case PTP_DTC_INT16: |
| 3312 | printf(" INT16 data type"); |
| 3313 | switch (opd.FormFlag) { |
Linus Walleij | 22cc487 | 2007-11-05 22:57:19 +0000 | [diff] [blame] | 3314 | case PTP_OPFF_Range: |
Linus Walleij | 1483034 | 2007-03-23 13:32:00 +0000 | [diff] [blame] | 3315 | printf(" range: MIN %d, MAX %d, STEP %d", |
| 3316 | opd.FORM.Range.MinimumValue.i16, |
| 3317 | opd.FORM.Range.MaximumValue.i16, |
| 3318 | opd.FORM.Range.StepSize.i16); |
| 3319 | break; |
Linus Walleij | 22cc487 | 2007-11-05 22:57:19 +0000 | [diff] [blame] | 3320 | case PTP_OPFF_Enumeration: |
Linus Walleij | 1483034 | 2007-03-23 13:32:00 +0000 | [diff] [blame] | 3321 | printf(" enumeration: "); |
| 3322 | for(k=0;k<opd.FORM.Enum.NumberOfValues;k++) { |
| 3323 | printf("%d, ", opd.FORM.Enum.SupportedValue[k].i16); |
| 3324 | } |
| 3325 | break; |
| 3326 | default: |
| 3327 | printf(" ANY 16BIT VALUE form"); |
| 3328 | break; |
| 3329 | } |
| 3330 | break; |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 3331 | |
Linus Walleij | 1483034 | 2007-03-23 13:32:00 +0000 | [diff] [blame] | 3332 | case PTP_DTC_UINT16: |
| 3333 | printf(" UINT16 data type"); |
| 3334 | switch (opd.FormFlag) { |
Linus Walleij | 22cc487 | 2007-11-05 22:57:19 +0000 | [diff] [blame] | 3335 | case PTP_OPFF_Range: |
Linus Walleij | 1483034 | 2007-03-23 13:32:00 +0000 | [diff] [blame] | 3336 | printf(" range: MIN %d, MAX %d, STEP %d", |
| 3337 | opd.FORM.Range.MinimumValue.u16, |
| 3338 | opd.FORM.Range.MaximumValue.u16, |
| 3339 | opd.FORM.Range.StepSize.u16); |
| 3340 | break; |
Linus Walleij | 22cc487 | 2007-11-05 22:57:19 +0000 | [diff] [blame] | 3341 | case PTP_OPFF_Enumeration: |
Linus Walleij | 1483034 | 2007-03-23 13:32:00 +0000 | [diff] [blame] | 3342 | printf(" enumeration: "); |
| 3343 | for(k=0;k<opd.FORM.Enum.NumberOfValues;k++) { |
| 3344 | printf("%d, ", opd.FORM.Enum.SupportedValue[k].u16); |
| 3345 | } |
| 3346 | break; |
| 3347 | default: |
| 3348 | printf(" ANY 16BIT VALUE form"); |
| 3349 | break; |
| 3350 | } |
| 3351 | break; |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 3352 | |
Linus Walleij | 1483034 | 2007-03-23 13:32:00 +0000 | [diff] [blame] | 3353 | case PTP_DTC_INT32: |
| 3354 | printf(" INT32 data type"); |
| 3355 | switch (opd.FormFlag) { |
Linus Walleij | 22cc487 | 2007-11-05 22:57:19 +0000 | [diff] [blame] | 3356 | case PTP_OPFF_Range: |
Linus Walleij | 1483034 | 2007-03-23 13:32:00 +0000 | [diff] [blame] | 3357 | printf(" range: MIN %d, MAX %d, STEP %d", |
| 3358 | opd.FORM.Range.MinimumValue.i32, |
| 3359 | opd.FORM.Range.MaximumValue.i32, |
| 3360 | opd.FORM.Range.StepSize.i32); |
| 3361 | break; |
Linus Walleij | 22cc487 | 2007-11-05 22:57:19 +0000 | [diff] [blame] | 3362 | case PTP_OPFF_Enumeration: |
Linus Walleij | 1483034 | 2007-03-23 13:32:00 +0000 | [diff] [blame] | 3363 | printf(" enumeration: "); |
| 3364 | for(k=0;k<opd.FORM.Enum.NumberOfValues;k++) { |
| 3365 | printf("%d, ", opd.FORM.Enum.SupportedValue[k].i32); |
| 3366 | } |
| 3367 | break; |
| 3368 | default: |
| 3369 | printf(" ANY 32BIT VALUE form"); |
| 3370 | break; |
| 3371 | } |
| 3372 | break; |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 3373 | |
Linus Walleij | 1483034 | 2007-03-23 13:32:00 +0000 | [diff] [blame] | 3374 | case PTP_DTC_UINT32: |
| 3375 | printf(" UINT32 data type"); |
| 3376 | switch (opd.FormFlag) { |
Linus Walleij | 22cc487 | 2007-11-05 22:57:19 +0000 | [diff] [blame] | 3377 | case PTP_OPFF_Range: |
Linus Walleij | 1483034 | 2007-03-23 13:32:00 +0000 | [diff] [blame] | 3378 | printf(" range: MIN %d, MAX %d, STEP %d", |
| 3379 | opd.FORM.Range.MinimumValue.u32, |
| 3380 | opd.FORM.Range.MaximumValue.u32, |
| 3381 | opd.FORM.Range.StepSize.u32); |
| 3382 | break; |
Linus Walleij | 22cc487 | 2007-11-05 22:57:19 +0000 | [diff] [blame] | 3383 | case PTP_OPFF_Enumeration: |
Linus Walleij | d71d0b3 | 2008-09-22 08:21:03 +0000 | [diff] [blame] | 3384 | // Special pretty-print for FOURCC codes |
| 3385 | if (params->deviceinfo.ImageFormats[i] == PTP_OPC_VideoFourCCCodec) { |
| 3386 | printf(" enumeration of u32 casted FOURCC: "); |
| 3387 | for (k=0;k<opd.FORM.Enum.NumberOfValues;k++) { |
| 3388 | if (opd.FORM.Enum.SupportedValue[k].u32 == 0) { |
| 3389 | printf("ANY, "); |
| 3390 | } else { |
| 3391 | char fourcc[6]; |
| 3392 | fourcc[0] = (opd.FORM.Enum.SupportedValue[k].u32 >> 24) & 0xFFU; |
| 3393 | fourcc[1] = (opd.FORM.Enum.SupportedValue[k].u32 >> 16) & 0xFFU; |
| 3394 | fourcc[2] = (opd.FORM.Enum.SupportedValue[k].u32 >> 8) & 0xFFU; |
| 3395 | fourcc[3] = opd.FORM.Enum.SupportedValue[k].u32 & 0xFFU; |
| 3396 | fourcc[4] = '\n'; |
| 3397 | fourcc[5] = '\0'; |
| 3398 | printf("\"%s\", ", fourcc); |
| 3399 | } |
| 3400 | } |
| 3401 | } else { |
| 3402 | printf(" enumeration: "); |
| 3403 | for(k=0;k<opd.FORM.Enum.NumberOfValues;k++) { |
| 3404 | printf("%d, ", opd.FORM.Enum.SupportedValue[k].u32); |
| 3405 | } |
Linus Walleij | 1483034 | 2007-03-23 13:32:00 +0000 | [diff] [blame] | 3406 | } |
| 3407 | break; |
| 3408 | default: |
| 3409 | printf(" ANY 32BIT VALUE form"); |
| 3410 | break; |
| 3411 | } |
| 3412 | break; |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 3413 | |
Linus Walleij | 1483034 | 2007-03-23 13:32:00 +0000 | [diff] [blame] | 3414 | case PTP_DTC_INT64: |
| 3415 | printf(" INT64 data type"); |
| 3416 | break; |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 3417 | |
Linus Walleij | 1483034 | 2007-03-23 13:32:00 +0000 | [diff] [blame] | 3418 | case PTP_DTC_UINT64: |
| 3419 | printf(" UINT64 data type"); |
| 3420 | break; |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 3421 | |
Linus Walleij | 1483034 | 2007-03-23 13:32:00 +0000 | [diff] [blame] | 3422 | case PTP_DTC_INT128: |
| 3423 | printf(" INT128 data type"); |
| 3424 | break; |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 3425 | |
Linus Walleij | 1483034 | 2007-03-23 13:32:00 +0000 | [diff] [blame] | 3426 | case PTP_DTC_UINT128: |
| 3427 | printf(" UINT128 data type"); |
| 3428 | break; |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 3429 | |
Linus Walleij | 1483034 | 2007-03-23 13:32:00 +0000 | [diff] [blame] | 3430 | default: |
| 3431 | printf(" UNKNOWN data type"); |
| 3432 | break; |
| 3433 | } |
| 3434 | } |
| 3435 | if (opd.GetSet) { |
| 3436 | printf(" GET/SET"); |
| 3437 | } else { |
| 3438 | printf(" READ ONLY"); |
| 3439 | } |
| 3440 | printf("\n"); |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 3441 | ptp_free_objectpropdesc(&opd); |
Linus Walleij | 0af979a | 2006-06-19 11:49:10 +0000 | [diff] [blame] | 3442 | } |
| 3443 | free(props); |
| 3444 | } |
| 3445 | } |
| 3446 | } |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 3447 | |
Linus Walleij | 43aba61 | 2011-11-25 09:40:34 +0100 | [diff] [blame] | 3448 | if(storage != NULL && |
| 3449 | ptp_operation_issupported(params,PTP_OC_GetStorageInfo)) { |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 3450 | printf("Storage Devices:\n"); |
Linus Walleij | e1ac07e | 2006-12-14 19:38:59 +0000 | [diff] [blame] | 3451 | while(storage != NULL) { |
| 3452 | printf(" StorageID: 0x%08x\n",storage->id); |
Linus Walleij | d71d0b3 | 2008-09-22 08:21:03 +0000 | [diff] [blame] | 3453 | printf(" StorageType: 0x%04x ",storage->StorageType); |
| 3454 | switch (storage->StorageType) { |
| 3455 | case PTP_ST_Undefined: |
| 3456 | printf("(undefined)\n"); |
| 3457 | break; |
| 3458 | case PTP_ST_FixedROM: |
| 3459 | printf("fixed ROM storage\n"); |
| 3460 | break; |
| 3461 | case PTP_ST_RemovableROM: |
| 3462 | printf("removable ROM storage\n"); |
| 3463 | break; |
| 3464 | case PTP_ST_FixedRAM: |
| 3465 | printf("fixed RAM storage\n"); |
| 3466 | break; |
| 3467 | case PTP_ST_RemovableRAM: |
| 3468 | printf("removable RAM storage\n"); |
| 3469 | break; |
| 3470 | default: |
| 3471 | printf("UNKNOWN storage\n"); |
| 3472 | break; |
| 3473 | } |
| 3474 | printf(" FilesystemType: 0x%04x ",storage->FilesystemType); |
| 3475 | switch(storage->FilesystemType) { |
| 3476 | case PTP_FST_Undefined: |
| 3477 | printf("(undefined)\n"); |
| 3478 | break; |
| 3479 | case PTP_FST_GenericFlat: |
| 3480 | printf("generic flat filesystem\n"); |
| 3481 | break; |
| 3482 | case PTP_FST_GenericHierarchical: |
| 3483 | printf("generic hierarchical\n"); |
| 3484 | break; |
| 3485 | case PTP_FST_DCF: |
| 3486 | printf("DCF\n"); |
| 3487 | break; |
| 3488 | default: |
| 3489 | printf("UNKNONWN filesystem type\n"); |
| 3490 | break; |
| 3491 | } |
| 3492 | printf(" AccessCapability: 0x%04x ",storage->AccessCapability); |
| 3493 | switch(storage->AccessCapability) { |
| 3494 | case PTP_AC_ReadWrite: |
| 3495 | printf("read/write\n"); |
| 3496 | break; |
| 3497 | case PTP_AC_ReadOnly: |
| 3498 | printf("read only\n"); |
| 3499 | break; |
| 3500 | case PTP_AC_ReadOnly_with_Object_Deletion: |
| 3501 | printf("read only + object deletion\n"); |
| 3502 | break; |
| 3503 | default: |
| 3504 | printf("UNKNOWN access capability\n"); |
| 3505 | break; |
| 3506 | } |
Linus Walleij | 43aba61 | 2011-11-25 09:40:34 +0100 | [diff] [blame] | 3507 | printf(" MaxCapacity: %llu\n", |
| 3508 | (long long unsigned int) storage->MaxCapacity); |
| 3509 | printf(" FreeSpaceInBytes: %llu\n", |
| 3510 | (long long unsigned int) storage->FreeSpaceInBytes); |
| 3511 | printf(" FreeSpaceInObjects: %llu\n", |
| 3512 | (long long unsigned int) storage->FreeSpaceInObjects); |
Linus Walleij | e1ac07e | 2006-12-14 19:38:59 +0000 | [diff] [blame] | 3513 | printf(" StorageDescription: %s\n",storage->StorageDescription); |
| 3514 | printf(" VolumeIdentifier: %s\n",storage->VolumeIdentifier); |
| 3515 | storage = storage->next; |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 3516 | } |
| 3517 | } |
| 3518 | |
Linus Walleij | 545c779 | 2006-06-13 15:22:30 +0000 | [diff] [blame] | 3519 | printf("Special directories:\n"); |
Linus Walleij | 43aba61 | 2011-11-25 09:40:34 +0100 | [diff] [blame] | 3520 | printf(" Default music folder: 0x%08x\n", |
| 3521 | device->default_music_folder); |
| 3522 | printf(" Default playlist folder: 0x%08x\n", |
| 3523 | device->default_playlist_folder); |
| 3524 | printf(" Default picture folder: 0x%08x\n", |
| 3525 | device->default_picture_folder); |
| 3526 | printf(" Default video folder: 0x%08x\n", |
| 3527 | device->default_video_folder); |
| 3528 | printf(" Default organizer folder: 0x%08x\n", |
| 3529 | device->default_organizer_folder); |
| 3530 | printf(" Default zencast folder: 0x%08x\n", |
| 3531 | device->default_zencast_folder); |
| 3532 | printf(" Default album folder: 0x%08x\n", |
| 3533 | device->default_album_folder); |
| 3534 | printf(" Default text folder: 0x%08x\n", |
| 3535 | device->default_text_folder); |
Linus Walleij | 8c45b29 | 2006-04-26 14:12:44 +0000 | [diff] [blame] | 3536 | } |
| 3537 | |
| 3538 | /** |
Linus Walleij | 5d533bb | 2007-07-17 21:48:57 +0000 | [diff] [blame] | 3539 | * This resets a device in case it supports the <code>PTP_OC_ResetDevice</code> |
| 3540 | * operation code (0x1010). |
| 3541 | * @param device a pointer to the device to reset. |
| 3542 | * @return 0 on success, any other value means failure. |
| 3543 | */ |
| 3544 | int LIBMTP_Reset_Device(LIBMTP_mtpdevice_t *device) |
| 3545 | { |
| 3546 | PTPParams *params = (PTPParams *) device->params; |
| 3547 | uint16_t ret; |
| 3548 | |
| 3549 | if (!ptp_operation_issupported(params,PTP_OC_ResetDevice)) { |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 3550 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, |
Linus Walleij | 43aba61 | 2011-11-25 09:40:34 +0100 | [diff] [blame] | 3551 | "LIBMTP_Reset_Device(): " |
| 3552 | "device does not support resetting."); |
Linus Walleij | 5d533bb | 2007-07-17 21:48:57 +0000 | [diff] [blame] | 3553 | return -1; |
| 3554 | } |
| 3555 | ret = ptp_resetdevice(params); |
| 3556 | if (ret != PTP_RC_OK) { |
| 3557 | add_ptp_error_to_errorstack(device, ret, "Error resetting."); |
| 3558 | return -1; |
| 3559 | } |
| 3560 | return 0; |
| 3561 | } |
| 3562 | |
| 3563 | /** |
Linus Walleij | 2350b71 | 2008-01-14 22:54:37 +0000 | [diff] [blame] | 3564 | * This retrieves the manufacturer name of an MTP device. |
| 3565 | * @param device a pointer to the device to get the manufacturer name for. |
| 3566 | * @return a newly allocated UTF-8 string representing the manufacturer name. |
| 3567 | * The string must be freed by the caller after use. If the call |
| 3568 | * was unsuccessful this will contain NULL. |
| 3569 | */ |
| 3570 | char *LIBMTP_Get_Manufacturername(LIBMTP_mtpdevice_t *device) |
| 3571 | { |
| 3572 | char *retmanuf = NULL; |
| 3573 | PTPParams *params = (PTPParams *) device->params; |
| 3574 | |
| 3575 | if (params->deviceinfo.Manufacturer != NULL) { |
| 3576 | retmanuf = strdup(params->deviceinfo.Manufacturer); |
| 3577 | } |
| 3578 | return retmanuf; |
| 3579 | } |
| 3580 | |
| 3581 | /** |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 3582 | * This retrieves the model name (often equal to product name) |
Linus Walleij | 8012406 | 2006-03-15 10:26:09 +0000 | [diff] [blame] | 3583 | * of an MTP device. |
| 3584 | * @param device a pointer to the device to get the model name for. |
| 3585 | * @return a newly allocated UTF-8 string representing the model name. |
| 3586 | * The string must be freed by the caller after use. If the call |
| 3587 | * was unsuccessful this will contain NULL. |
| 3588 | */ |
| 3589 | char *LIBMTP_Get_Modelname(LIBMTP_mtpdevice_t *device) |
| 3590 | { |
| 3591 | char *retmodel = NULL; |
Linus Walleij | 9b28da3 | 2006-03-16 13:47:58 +0000 | [diff] [blame] | 3592 | PTPParams *params = (PTPParams *) device->params; |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 3593 | |
Linus Walleij | 9b28da3 | 2006-03-16 13:47:58 +0000 | [diff] [blame] | 3594 | if (params->deviceinfo.Model != NULL) { |
| 3595 | retmodel = strdup(params->deviceinfo.Model); |
Linus Walleij | 8012406 | 2006-03-15 10:26:09 +0000 | [diff] [blame] | 3596 | } |
| 3597 | return retmodel; |
| 3598 | } |
| 3599 | |
| 3600 | /** |
| 3601 | * This retrieves the serial number of an MTP device. |
| 3602 | * @param device a pointer to the device to get the serial number for. |
| 3603 | * @return a newly allocated UTF-8 string representing the serial number. |
| 3604 | * The string must be freed by the caller after use. If the call |
| 3605 | * was unsuccessful this will contain NULL. |
| 3606 | */ |
| 3607 | char *LIBMTP_Get_Serialnumber(LIBMTP_mtpdevice_t *device) |
| 3608 | { |
| 3609 | char *retnumber = NULL; |
Linus Walleij | 9b28da3 | 2006-03-16 13:47:58 +0000 | [diff] [blame] | 3610 | PTPParams *params = (PTPParams *) device->params; |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 3611 | |
Linus Walleij | 9b28da3 | 2006-03-16 13:47:58 +0000 | [diff] [blame] | 3612 | if (params->deviceinfo.SerialNumber != NULL) { |
| 3613 | retnumber = strdup(params->deviceinfo.SerialNumber); |
Linus Walleij | 8012406 | 2006-03-15 10:26:09 +0000 | [diff] [blame] | 3614 | } |
| 3615 | return retnumber; |
| 3616 | } |
| 3617 | |
| 3618 | /** |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 3619 | * This retrieves the device version (hardware and firmware version) of an |
Linus Walleij | 8012406 | 2006-03-15 10:26:09 +0000 | [diff] [blame] | 3620 | * MTP device. |
| 3621 | * @param device a pointer to the device to get the device version for. |
| 3622 | * @return a newly allocated UTF-8 string representing the device version. |
| 3623 | * The string must be freed by the caller after use. If the call |
| 3624 | * was unsuccessful this will contain NULL. |
| 3625 | */ |
| 3626 | char *LIBMTP_Get_Deviceversion(LIBMTP_mtpdevice_t *device) |
| 3627 | { |
| 3628 | char *retversion = NULL; |
Linus Walleij | 9b28da3 | 2006-03-16 13:47:58 +0000 | [diff] [blame] | 3629 | PTPParams *params = (PTPParams *) device->params; |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 3630 | |
Linus Walleij | 9b28da3 | 2006-03-16 13:47:58 +0000 | [diff] [blame] | 3631 | if (params->deviceinfo.DeviceVersion != NULL) { |
| 3632 | retversion = strdup(params->deviceinfo.DeviceVersion); |
Linus Walleij | 8012406 | 2006-03-15 10:26:09 +0000 | [diff] [blame] | 3633 | } |
| 3634 | return retversion; |
| 3635 | } |
| 3636 | |
| 3637 | |
| 3638 | /** |
Linus Walleij | fae2748 | 2006-08-19 20:13:25 +0000 | [diff] [blame] | 3639 | * This retrieves the "friendly name" of an MTP device. Usually |
| 3640 | * this is simply the name of the owner or something like |
Linus Walleij | 3065879 | 2006-08-19 22:18:55 +0000 | [diff] [blame] | 3641 | * "John Doe's Digital Audio Player". This property should be supported |
Linus Walleij | fae2748 | 2006-08-19 20:13:25 +0000 | [diff] [blame] | 3642 | * by all MTP devices. |
| 3643 | * @param device a pointer to the device to get the friendly name for. |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 3644 | * @return a newly allocated UTF-8 string representing the friendly name. |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 3645 | * The string must be freed by the caller after use. |
Linus Walleij | 3065879 | 2006-08-19 22:18:55 +0000 | [diff] [blame] | 3646 | * @see LIBMTP_Set_Friendlyname() |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 3647 | */ |
Linus Walleij | 3065879 | 2006-08-19 22:18:55 +0000 | [diff] [blame] | 3648 | char *LIBMTP_Get_Friendlyname(LIBMTP_mtpdevice_t *device) |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 3649 | { |
Linus Walleij | b02a066 | 2006-04-25 08:05:09 +0000 | [diff] [blame] | 3650 | PTPPropertyValue propval; |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 3651 | char *retstring = NULL; |
Linus Walleij | 9b28da3 | 2006-03-16 13:47:58 +0000 | [diff] [blame] | 3652 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 3653 | uint16_t ret; |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 3654 | |
Linus Walleij | cf223e6 | 2006-06-19 09:31:53 +0000 | [diff] [blame] | 3655 | if (!ptp_property_issupported(params, PTP_DPC_MTP_DeviceFriendlyName)) { |
| 3656 | return NULL; |
| 3657 | } |
| 3658 | |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 3659 | ret = ptp_getdevicepropvalue(params, |
| 3660 | PTP_DPC_MTP_DeviceFriendlyName, |
| 3661 | &propval, |
| 3662 | PTP_DTC_STR); |
| 3663 | if (ret != PTP_RC_OK) { |
| 3664 | add_ptp_error_to_errorstack(device, ret, "Error getting friendlyname."); |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 3665 | return NULL; |
| 3666 | } |
Linus Walleij | a823a70 | 2006-08-27 21:27:46 +0000 | [diff] [blame] | 3667 | if (propval.str != NULL) { |
| 3668 | retstring = strdup(propval.str); |
| 3669 | free(propval.str); |
| 3670 | } |
Linus Walleij | fae2748 | 2006-08-19 20:13:25 +0000 | [diff] [blame] | 3671 | return retstring; |
| 3672 | } |
| 3673 | |
| 3674 | /** |
Linus Walleij | 3065879 | 2006-08-19 22:18:55 +0000 | [diff] [blame] | 3675 | * Sets the "friendly name" of an MTP device. |
| 3676 | * @param device a pointer to the device to set the friendly name for. |
| 3677 | * @param friendlyname the new friendly name for the device. |
| 3678 | * @return 0 on success, any other value means failure. |
Linus Walleij | d5b3497 | 2008-09-24 20:24:12 +0000 | [diff] [blame] | 3679 | * @see LIBMTP_Get_Friendlyname() |
Linus Walleij | 3065879 | 2006-08-19 22:18:55 +0000 | [diff] [blame] | 3680 | */ |
| 3681 | int LIBMTP_Set_Friendlyname(LIBMTP_mtpdevice_t *device, |
| 3682 | char const * const friendlyname) |
| 3683 | { |
| 3684 | PTPPropertyValue propval; |
| 3685 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 3686 | uint16_t ret; |
Linus Walleij | 3065879 | 2006-08-19 22:18:55 +0000 | [diff] [blame] | 3687 | |
| 3688 | if (!ptp_property_issupported(params, PTP_DPC_MTP_DeviceFriendlyName)) { |
| 3689 | return -1; |
| 3690 | } |
Linus Walleij | a823a70 | 2006-08-27 21:27:46 +0000 | [diff] [blame] | 3691 | propval.str = (char *) friendlyname; |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 3692 | ret = ptp_setdevicepropvalue(params, |
| 3693 | PTP_DPC_MTP_DeviceFriendlyName, |
| 3694 | &propval, |
| 3695 | PTP_DTC_STR); |
| 3696 | if (ret != PTP_RC_OK) { |
| 3697 | add_ptp_error_to_errorstack(device, ret, "Error setting friendlyname."); |
Linus Walleij | 3065879 | 2006-08-19 22:18:55 +0000 | [diff] [blame] | 3698 | return -1; |
| 3699 | } |
Linus Walleij | 3065879 | 2006-08-19 22:18:55 +0000 | [diff] [blame] | 3700 | return 0; |
| 3701 | } |
| 3702 | |
| 3703 | /** |
Linus Walleij | fae2748 | 2006-08-19 20:13:25 +0000 | [diff] [blame] | 3704 | * This retrieves the syncronization partner of an MTP device. This |
| 3705 | * property should be supported by all MTP devices. |
| 3706 | * @param device a pointer to the device to get the sync partner for. |
| 3707 | * @return a newly allocated UTF-8 string representing the synchronization |
| 3708 | * partner. The string must be freed by the caller after use. |
Linus Walleij | 3065879 | 2006-08-19 22:18:55 +0000 | [diff] [blame] | 3709 | * @see LIBMTP_Set_Syncpartner() |
Linus Walleij | fae2748 | 2006-08-19 20:13:25 +0000 | [diff] [blame] | 3710 | */ |
| 3711 | char *LIBMTP_Get_Syncpartner(LIBMTP_mtpdevice_t *device) |
| 3712 | { |
| 3713 | PTPPropertyValue propval; |
| 3714 | char *retstring = NULL; |
| 3715 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 3716 | uint16_t ret; |
Linus Walleij | fae2748 | 2006-08-19 20:13:25 +0000 | [diff] [blame] | 3717 | |
| 3718 | if (!ptp_property_issupported(params, PTP_DPC_MTP_SynchronizationPartner)) { |
| 3719 | return NULL; |
| 3720 | } |
| 3721 | |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 3722 | ret = ptp_getdevicepropvalue(params, |
| 3723 | PTP_DPC_MTP_SynchronizationPartner, |
| 3724 | &propval, |
| 3725 | PTP_DTC_STR); |
| 3726 | if (ret != PTP_RC_OK) { |
| 3727 | add_ptp_error_to_errorstack(device, ret, "Error getting syncpartner."); |
Linus Walleij | fae2748 | 2006-08-19 20:13:25 +0000 | [diff] [blame] | 3728 | return NULL; |
| 3729 | } |
Linus Walleij | a823a70 | 2006-08-27 21:27:46 +0000 | [diff] [blame] | 3730 | if (propval.str != NULL) { |
| 3731 | retstring = strdup(propval.str); |
| 3732 | free(propval.str); |
| 3733 | } |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 3734 | return retstring; |
| 3735 | } |
| 3736 | |
Linus Walleij | 3065879 | 2006-08-19 22:18:55 +0000 | [diff] [blame] | 3737 | |
| 3738 | /** |
| 3739 | * Sets the synchronization partner of an MTP device. Note that |
| 3740 | * we have no idea what the effect of setting this to "foobar" |
| 3741 | * may be. But the general idea seems to be to tell which program |
| 3742 | * shall synchronize with this device and tell others to leave |
| 3743 | * it alone. |
| 3744 | * @param device a pointer to the device to set the sync partner for. |
| 3745 | * @param syncpartner the new synchronization partner for the device. |
| 3746 | * @return 0 on success, any other value means failure. |
| 3747 | * @see LIBMTP_Get_Syncpartner() |
| 3748 | */ |
| 3749 | int LIBMTP_Set_Syncpartner(LIBMTP_mtpdevice_t *device, |
| 3750 | char const * const syncpartner) |
| 3751 | { |
| 3752 | PTPPropertyValue propval; |
| 3753 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 3754 | uint16_t ret; |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 3755 | |
Linus Walleij | 3065879 | 2006-08-19 22:18:55 +0000 | [diff] [blame] | 3756 | if (!ptp_property_issupported(params, PTP_DPC_MTP_SynchronizationPartner)) { |
| 3757 | return -1; |
| 3758 | } |
Linus Walleij | a823a70 | 2006-08-27 21:27:46 +0000 | [diff] [blame] | 3759 | propval.str = (char *) syncpartner; |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 3760 | ret = ptp_setdevicepropvalue(params, |
| 3761 | PTP_DPC_MTP_SynchronizationPartner, |
| 3762 | &propval, |
| 3763 | PTP_DTC_STR); |
| 3764 | if (ret != PTP_RC_OK) { |
| 3765 | add_ptp_error_to_errorstack(device, ret, "Error setting syncpartner."); |
Linus Walleij | 3065879 | 2006-08-19 22:18:55 +0000 | [diff] [blame] | 3766 | return -1; |
| 3767 | } |
Linus Walleij | 3065879 | 2006-08-19 22:18:55 +0000 | [diff] [blame] | 3768 | return 0; |
| 3769 | } |
| 3770 | |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 3771 | /** |
Linus Walleij | f5fcda3 | 2006-12-03 22:31:02 +0000 | [diff] [blame] | 3772 | * Checks if the device can stora a file of this size or |
| 3773 | * if it's too big. |
| 3774 | * @param device a pointer to the device. |
| 3775 | * @param filesize the size of the file to check whether it will fit. |
Linus Walleij | 6bf68b4 | 2007-09-03 22:50:02 +0000 | [diff] [blame] | 3776 | * @param storageid the ID of the storage to try to fit the file on. |
Linus Walleij | f5fcda3 | 2006-12-03 22:31:02 +0000 | [diff] [blame] | 3777 | * @return 0 if the file fits, any other value means failure. |
| 3778 | */ |
Linus Walleij | 6bf68b4 | 2007-09-03 22:50:02 +0000 | [diff] [blame] | 3779 | static int check_if_file_fits(LIBMTP_mtpdevice_t *device, |
| 3780 | LIBMTP_devicestorage_t *storage, |
| 3781 | uint64_t const filesize) { |
Linus Walleij | f5fcda3 | 2006-12-03 22:31:02 +0000 | [diff] [blame] | 3782 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | f5fcda3 | 2006-12-03 22:31:02 +0000 | [diff] [blame] | 3783 | uint64_t freebytes; |
Linus Walleij | f5fcda3 | 2006-12-03 22:31:02 +0000 | [diff] [blame] | 3784 | int ret; |
| 3785 | |
| 3786 | // If we cannot check the storage, no big deal. |
| 3787 | if (!ptp_operation_issupported(params,PTP_OC_GetStorageInfo)) { |
| 3788 | return 0; |
| 3789 | } |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 3790 | |
Linus Walleij | 6bf68b4 | 2007-09-03 22:50:02 +0000 | [diff] [blame] | 3791 | ret = get_storage_freespace(device, storage, &freebytes); |
Linus Walleij | f5fcda3 | 2006-12-03 22:31:02 +0000 | [diff] [blame] | 3792 | if (ret != 0) { |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 3793 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, |
Linus Walleij | 6bf68b4 | 2007-09-03 22:50:02 +0000 | [diff] [blame] | 3794 | "check_if_file_fits(): error checking free storage."); |
Linus Walleij | f5fcda3 | 2006-12-03 22:31:02 +0000 | [diff] [blame] | 3795 | return -1; |
| 3796 | } else { |
Linus Walleij | fb28b63 | 2007-10-23 21:56:18 +0000 | [diff] [blame] | 3797 | // See if it fits. |
Linus Walleij | f5fcda3 | 2006-12-03 22:31:02 +0000 | [diff] [blame] | 3798 | if (filesize > freebytes) { |
Linus Walleij | f5fcda3 | 2006-12-03 22:31:02 +0000 | [diff] [blame] | 3799 | return -1; |
| 3800 | } |
| 3801 | } |
| 3802 | return 0; |
| 3803 | } |
| 3804 | |
| 3805 | |
Linus Walleij | f5fcda3 | 2006-12-03 22:31:02 +0000 | [diff] [blame] | 3806 | /** |
Linus Walleij | fa1374c | 2006-02-27 07:41:46 +0000 | [diff] [blame] | 3807 | * This function retrieves the current battery level on the device. |
| 3808 | * @param device a pointer to the device to get the battery level for. |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 3809 | * @param maximum_level a pointer to a variable that will hold the |
Linus Walleij | fa1374c | 2006-02-27 07:41:46 +0000 | [diff] [blame] | 3810 | * maximum level of the battery if the call was successful. |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 3811 | * @param current_level a pointer to a variable that will hold the |
Linus Walleij | fa1374c | 2006-02-27 07:41:46 +0000 | [diff] [blame] | 3812 | * current level of the battery if the call was successful. |
Linus Walleij | 545c779 | 2006-06-13 15:22:30 +0000 | [diff] [blame] | 3813 | * A value of 0 means that the device is on external power. |
Linus Walleij | fa1374c | 2006-02-27 07:41:46 +0000 | [diff] [blame] | 3814 | * @return 0 if the storage info was successfully retrieved, any other |
Linus Walleij | 8043934 | 2006-09-12 10:42:26 +0000 | [diff] [blame] | 3815 | * means failure. A typical cause of failure is that |
Linus Walleij | 545c779 | 2006-06-13 15:22:30 +0000 | [diff] [blame] | 3816 | * the device does not support the battery level property. |
Linus Walleij | fa1374c | 2006-02-27 07:41:46 +0000 | [diff] [blame] | 3817 | */ |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 3818 | int LIBMTP_Get_Batterylevel(LIBMTP_mtpdevice_t *device, |
| 3819 | uint8_t * const maximum_level, |
Linus Walleij | fa1374c | 2006-02-27 07:41:46 +0000 | [diff] [blame] | 3820 | uint8_t * const current_level) |
| 3821 | { |
Linus Walleij | b02a066 | 2006-04-25 08:05:09 +0000 | [diff] [blame] | 3822 | PTPPropertyValue propval; |
Linus Walleij | fa1374c | 2006-02-27 07:41:46 +0000 | [diff] [blame] | 3823 | uint16_t ret; |
Linus Walleij | 9b28da3 | 2006-03-16 13:47:58 +0000 | [diff] [blame] | 3824 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | 4096c88 | 2009-03-16 23:32:34 +0000 | [diff] [blame] | 3825 | PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo; |
Linus Walleij | fa1374c | 2006-02-27 07:41:46 +0000 | [diff] [blame] | 3826 | |
Linus Walleij | 545c779 | 2006-06-13 15:22:30 +0000 | [diff] [blame] | 3827 | *maximum_level = 0; |
| 3828 | *current_level = 0; |
| 3829 | |
Linus Walleij | 4096c88 | 2009-03-16 23:32:34 +0000 | [diff] [blame] | 3830 | if (FLAG_BROKEN_BATTERY_LEVEL(ptp_usb) || |
| 3831 | !ptp_property_issupported(params, PTP_DPC_BatteryLevel)) { |
Linus Walleij | 545c779 | 2006-06-13 15:22:30 +0000 | [diff] [blame] | 3832 | return -1; |
| 3833 | } |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 3834 | |
Linus Walleij | 43aba61 | 2011-11-25 09:40:34 +0100 | [diff] [blame] | 3835 | ret = ptp_getdevicepropvalue(params, PTP_DPC_BatteryLevel, |
| 3836 | &propval, PTP_DTC_UINT8); |
Linus Walleij | b02a066 | 2006-04-25 08:05:09 +0000 | [diff] [blame] | 3837 | if (ret != PTP_RC_OK) { |
Linus Walleij | 43aba61 | 2011-11-25 09:40:34 +0100 | [diff] [blame] | 3838 | add_ptp_error_to_errorstack(device, ret, |
| 3839 | "LIBMTP_Get_Batterylevel(): " |
| 3840 | "could not get device property value."); |
Linus Walleij | fa1374c | 2006-02-27 07:41:46 +0000 | [diff] [blame] | 3841 | return -1; |
| 3842 | } |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 3843 | |
Linus Walleij | fa1374c | 2006-02-27 07:41:46 +0000 | [diff] [blame] | 3844 | *maximum_level = device->maximum_battery_level; |
Linus Walleij | b02a066 | 2006-04-25 08:05:09 +0000 | [diff] [blame] | 3845 | *current_level = propval.u8; |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 3846 | |
Linus Walleij | fa1374c | 2006-02-27 07:41:46 +0000 | [diff] [blame] | 3847 | return 0; |
| 3848 | } |
| 3849 | |
Linus Walleij | 13374a4 | 2006-09-13 11:55:30 +0000 | [diff] [blame] | 3850 | |
| 3851 | /** |
| 3852 | * Formats device storage (if the device supports the operation). |
| 3853 | * WARNING: This WILL delete all data from the device. Make sure you've |
| 3854 | * got confirmation from the user BEFORE you call this function. |
| 3855 | * |
Linus Walleij | f849191 | 2006-12-15 10:23:30 +0000 | [diff] [blame] | 3856 | * @param device a pointer to the device containing the storage to format. |
| 3857 | * @param storage the actual storage to format. |
Linus Walleij | 13374a4 | 2006-09-13 11:55:30 +0000 | [diff] [blame] | 3858 | * @return 0 on success, any other value means failure. |
| 3859 | */ |
Linus Walleij | 43aba61 | 2011-11-25 09:40:34 +0100 | [diff] [blame] | 3860 | int LIBMTP_Format_Storage(LIBMTP_mtpdevice_t *device, |
| 3861 | LIBMTP_devicestorage_t *storage) |
Linus Walleij | 13374a4 | 2006-09-13 11:55:30 +0000 | [diff] [blame] | 3862 | { |
| 3863 | uint16_t ret; |
| 3864 | PTPParams *params = (PTPParams *) device->params; |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 3865 | |
Linus Walleij | 13374a4 | 2006-09-13 11:55:30 +0000 | [diff] [blame] | 3866 | if (!ptp_operation_issupported(params,PTP_OC_FormatStore)) { |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 3867 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, |
Linus Walleij | 43aba61 | 2011-11-25 09:40:34 +0100 | [diff] [blame] | 3868 | "LIBMTP_Format_Storage(): " |
| 3869 | "device does not support formatting storage."); |
Linus Walleij | 13374a4 | 2006-09-13 11:55:30 +0000 | [diff] [blame] | 3870 | return -1; |
| 3871 | } |
Linus Walleij | f849191 | 2006-12-15 10:23:30 +0000 | [diff] [blame] | 3872 | ret = ptp_formatstore(params, storage->id); |
Linus Walleij | 13374a4 | 2006-09-13 11:55:30 +0000 | [diff] [blame] | 3873 | if (ret != PTP_RC_OK) { |
Linus Walleij | 43aba61 | 2011-11-25 09:40:34 +0100 | [diff] [blame] | 3874 | add_ptp_error_to_errorstack(device, ret, "LIBMTP_Format_Storage(): " |
| 3875 | "failed to format storage."); |
Linus Walleij | 13374a4 | 2006-09-13 11:55:30 +0000 | [diff] [blame] | 3876 | return -1; |
| 3877 | } |
| 3878 | return 0; |
| 3879 | } |
| 3880 | |
Linus Walleij | fa1374c | 2006-02-27 07:41:46 +0000 | [diff] [blame] | 3881 | /** |
Linus Walleij | 545c779 | 2006-06-13 15:22:30 +0000 | [diff] [blame] | 3882 | * Helper function to extract a unicode property off a device. |
Linus Walleij | e46f12e | 2006-06-22 17:53:25 +0000 | [diff] [blame] | 3883 | * This is the standard way of retrieveing unicode device |
| 3884 | * properties as described by the PTP spec. |
Linus Walleij | cf223e6 | 2006-06-19 09:31:53 +0000 | [diff] [blame] | 3885 | * @param device a pointer to the device to get the property from. |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 3886 | * @param unicstring a pointer to a pointer that will hold the |
Linus Walleij | cf223e6 | 2006-06-19 09:31:53 +0000 | [diff] [blame] | 3887 | * property after this call is completed. |
| 3888 | * @param property the property to retrieve. |
| 3889 | * @return 0 on success, any other value means failure. |
Linus Walleij | 545c779 | 2006-06-13 15:22:30 +0000 | [diff] [blame] | 3890 | */ |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 3891 | static int get_device_unicode_property(LIBMTP_mtpdevice_t *device, |
Linus Walleij | cf223e6 | 2006-06-19 09:31:53 +0000 | [diff] [blame] | 3892 | char **unicstring, uint16_t property) |
Linus Walleij | 545c779 | 2006-06-13 15:22:30 +0000 | [diff] [blame] | 3893 | { |
| 3894 | PTPPropertyValue propval; |
| 3895 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | 16571dc | 2006-08-17 20:27:46 +0000 | [diff] [blame] | 3896 | uint16_t *tmp; |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 3897 | uint16_t ret; |
Linus Walleij | 545c779 | 2006-06-13 15:22:30 +0000 | [diff] [blame] | 3898 | int i; |
| 3899 | |
| 3900 | if (!ptp_property_issupported(params, property)) { |
| 3901 | return -1; |
| 3902 | } |
| 3903 | |
Linus Walleij | cf223e6 | 2006-06-19 09:31:53 +0000 | [diff] [blame] | 3904 | // Unicode strings are 16bit unsigned integer arrays. |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 3905 | ret = ptp_getdevicepropvalue(params, |
| 3906 | property, |
| 3907 | &propval, |
| 3908 | PTP_DTC_AUINT16); |
| 3909 | if (ret != PTP_RC_OK) { |
| 3910 | // TODO: add a note on WHICH property that we failed to get. |
tedbullock | 4e51cb9 | 2007-02-15 11:48:34 +0000 | [diff] [blame] | 3911 | *unicstring = NULL; |
Linus Walleij | 43aba61 | 2011-11-25 09:40:34 +0100 | [diff] [blame] | 3912 | add_ptp_error_to_errorstack(device, ret, |
| 3913 | "get_device_unicode_property(): " |
| 3914 | "failed to get unicode property."); |
Linus Walleij | 545c779 | 2006-06-13 15:22:30 +0000 | [diff] [blame] | 3915 | return -1; |
| 3916 | } |
| 3917 | |
| 3918 | // Extract the actual array. |
Linus Walleij | 16571dc | 2006-08-17 20:27:46 +0000 | [diff] [blame] | 3919 | // printf("Array of %d elements\n", propval.a.count); |
| 3920 | tmp = malloc((propval.a.count + 1)*sizeof(uint16_t)); |
Linus Walleij | 545c779 | 2006-06-13 15:22:30 +0000 | [diff] [blame] | 3921 | for (i = 0; i < propval.a.count; i++) { |
Linus Walleij | 16571dc | 2006-08-17 20:27:46 +0000 | [diff] [blame] | 3922 | tmp[i] = propval.a.v[i].u16; |
| 3923 | // printf("%04x ", tmp[i]); |
Linus Walleij | 545c779 | 2006-06-13 15:22:30 +0000 | [diff] [blame] | 3924 | } |
Linus Walleij | 16571dc | 2006-08-17 20:27:46 +0000 | [diff] [blame] | 3925 | tmp[propval.a.count] = 0x0000U; |
Linus Walleij | 545c779 | 2006-06-13 15:22:30 +0000 | [diff] [blame] | 3926 | free(propval.a.v); |
| 3927 | |
Linus Walleij | 3ec8631 | 2006-08-21 13:25:24 +0000 | [diff] [blame] | 3928 | *unicstring = utf16_to_utf8(device, tmp); |
Linus Walleij | 16571dc | 2006-08-17 20:27:46 +0000 | [diff] [blame] | 3929 | |
Linus Walleij | 545c779 | 2006-06-13 15:22:30 +0000 | [diff] [blame] | 3930 | free(tmp); |
| 3931 | |
| 3932 | return 0; |
| 3933 | } |
| 3934 | |
| 3935 | /** |
| 3936 | * This function returns the secure time as an XML document string from |
| 3937 | * the device. |
| 3938 | * @param device a pointer to the device to get the secure time for. |
| 3939 | * @param sectime the secure time string as an XML document or NULL if the call |
| 3940 | * failed or the secure time property is not supported. This string |
| 3941 | * must be <code>free()</code>:ed by the caller after use. |
| 3942 | * @return 0 on success, any other value means failure. |
| 3943 | */ |
Linus Walleij | 8ab5426 | 2006-06-21 07:12:28 +0000 | [diff] [blame] | 3944 | int LIBMTP_Get_Secure_Time(LIBMTP_mtpdevice_t *device, char ** const sectime) |
Linus Walleij | 545c779 | 2006-06-13 15:22:30 +0000 | [diff] [blame] | 3945 | { |
| 3946 | return get_device_unicode_property(device, sectime, PTP_DPC_MTP_SecureTime); |
| 3947 | } |
| 3948 | |
| 3949 | /** |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 3950 | * This function returns the device (public key) certificate as an |
Linus Walleij | 545c779 | 2006-06-13 15:22:30 +0000 | [diff] [blame] | 3951 | * XML document string from the device. |
| 3952 | * @param device a pointer to the device to get the device certificate for. |
| 3953 | * @param devcert the device certificate as an XML string or NULL if the call |
| 3954 | * failed or the device certificate property is not supported. This |
| 3955 | * string must be <code>free()</code>:ed by the caller after use. |
| 3956 | * @return 0 on success, any other value means failure. |
| 3957 | */ |
Linus Walleij | 8ab5426 | 2006-06-21 07:12:28 +0000 | [diff] [blame] | 3958 | int LIBMTP_Get_Device_Certificate(LIBMTP_mtpdevice_t *device, char ** const devcert) |
Linus Walleij | 545c779 | 2006-06-13 15:22:30 +0000 | [diff] [blame] | 3959 | { |
Linus Walleij | 43aba61 | 2011-11-25 09:40:34 +0100 | [diff] [blame] | 3960 | return get_device_unicode_property(device, devcert, |
| 3961 | PTP_DPC_MTP_DeviceCertificate); |
Linus Walleij | 545c779 | 2006-06-13 15:22:30 +0000 | [diff] [blame] | 3962 | } |
| 3963 | |
| 3964 | /** |
Linus Walleij | 8ab5426 | 2006-06-21 07:12:28 +0000 | [diff] [blame] | 3965 | * This function retrieves a list of supported file types, i.e. the file |
| 3966 | * types that this device claims it supports, e.g. audio file types that |
| 3967 | * the device can play etc. This list is mitigated to |
| 3968 | * inlcude the file types that libmtp can handle, i.e. it will not list |
| 3969 | * filetypes that libmtp will handle internally like playlists and folders. |
| 3970 | * @param device a pointer to the device to get the filetype capabilities for. |
| 3971 | * @param filetypes a pointer to a pointer that will hold the list of |
| 3972 | * supported filetypes if the call was successful. This list must |
| 3973 | * be <code>free()</code>:ed by the caller after use. |
| 3974 | * @param length a pointer to a variable that will hold the length of the |
| 3975 | * list of supported filetypes if the call was successful. |
| 3976 | * @return 0 on success, any other value means failure. |
| 3977 | * @see LIBMTP_Get_Filetype_Description() |
| 3978 | */ |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 3979 | int LIBMTP_Get_Supported_Filetypes(LIBMTP_mtpdevice_t *device, uint16_t ** const filetypes, |
Linus Walleij | 8ab5426 | 2006-06-21 07:12:28 +0000 | [diff] [blame] | 3980 | uint16_t * const length) |
| 3981 | { |
| 3982 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | a3544f6 | 2007-11-30 01:20:04 +0000 | [diff] [blame] | 3983 | PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo; |
Linus Walleij | 8ab5426 | 2006-06-21 07:12:28 +0000 | [diff] [blame] | 3984 | uint16_t *localtypes; |
| 3985 | uint16_t localtypelen; |
| 3986 | uint32_t i; |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 3987 | |
Linus Walleij | 8ab5426 | 2006-06-21 07:12:28 +0000 | [diff] [blame] | 3988 | // This is more memory than needed if there are unknown types, but what the heck. |
| 3989 | localtypes = (uint16_t *) malloc(params->deviceinfo.ImageFormats_len * sizeof(uint16_t)); |
| 3990 | localtypelen = 0; |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 3991 | |
Linus Walleij | 8ab5426 | 2006-06-21 07:12:28 +0000 | [diff] [blame] | 3992 | for (i=0;i<params->deviceinfo.ImageFormats_len;i++) { |
| 3993 | uint16_t localtype = map_ptp_type_to_libmtp_type(params->deviceinfo.ImageFormats[i]); |
| 3994 | if (localtype != LIBMTP_FILETYPE_UNKNOWN) { |
| 3995 | localtypes[localtypelen] = localtype; |
| 3996 | localtypelen++; |
| 3997 | } |
| 3998 | } |
Linus Walleij | a3544f6 | 2007-11-30 01:20:04 +0000 | [diff] [blame] | 3999 | // The forgotten Ogg support on YP-10 and others... |
Linus Walleij | fec4d56 | 2008-06-01 22:30:36 +0000 | [diff] [blame] | 4000 | if (FLAG_OGG_IS_UNKNOWN(ptp_usb)) { |
Linus Walleij | 43aba61 | 2011-11-25 09:40:34 +0100 | [diff] [blame] | 4001 | localtypes = (uint16_t *) realloc(localtypes, |
| 4002 | (params->deviceinfo.ImageFormats_len+1) * sizeof(uint16_t)); |
Linus Walleij | a3544f6 | 2007-11-30 01:20:04 +0000 | [diff] [blame] | 4003 | localtypes[localtypelen] = LIBMTP_FILETYPE_OGG; |
| 4004 | localtypelen++; |
| 4005 | } |
Linus Walleij | 89bb1cd | 2009-07-24 21:03:36 +0000 | [diff] [blame] | 4006 | // The forgotten FLAC support on Cowon iAudio S9 and others... |
| 4007 | if (FLAG_FLAC_IS_UNKNOWN(ptp_usb)) { |
Linus Walleij | 43aba61 | 2011-11-25 09:40:34 +0100 | [diff] [blame] | 4008 | localtypes = (uint16_t *) realloc(localtypes, |
| 4009 | (params->deviceinfo.ImageFormats_len+1) * sizeof(uint16_t)); |
Linus Walleij | 89bb1cd | 2009-07-24 21:03:36 +0000 | [diff] [blame] | 4010 | localtypes[localtypelen] = LIBMTP_FILETYPE_FLAC; |
| 4011 | localtypelen++; |
| 4012 | } |
Linus Walleij | 8ab5426 | 2006-06-21 07:12:28 +0000 | [diff] [blame] | 4013 | |
| 4014 | *filetypes = localtypes; |
| 4015 | *length = localtypelen; |
| 4016 | |
| 4017 | return 0; |
| 4018 | } |
| 4019 | |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 4020 | /** |
Linus Walleij | a389561 | 2013-04-29 21:56:38 +0200 | [diff] [blame] | 4021 | * This function checks if the device has some specific capabilities, in |
| 4022 | * order to avoid calling APIs that may disturb the device. |
| 4023 | * |
| 4024 | * @param device a pointer to the device to check the capability on. |
| 4025 | * @param cap the capability to check. |
| 4026 | * @return 0 if not supported, any other value means the device has the |
| 4027 | * requested capability. |
| 4028 | */ |
| 4029 | int LIBMTP_Check_Capability(LIBMTP_mtpdevice_t *device, LIBMTP_devicecap_t cap) |
| 4030 | { |
| 4031 | switch (cap) { |
| 4032 | case LIBMTP_DEVICECAP_GetPartialObject: |
| 4033 | return (ptp_operation_issupported(device->params, |
| 4034 | PTP_OC_GetPartialObject) || |
| 4035 | ptp_operation_issupported(device->params, |
| 4036 | PTP_OC_ANDROID_GetPartialObject64)); |
| 4037 | case LIBMTP_DEVICECAP_SendPartialObject: |
| 4038 | return ptp_operation_issupported(device->params, |
| 4039 | PTP_OC_ANDROID_SendPartialObject); |
| 4040 | case LIBMTP_DEVICECAP_EditObjects: |
| 4041 | return (ptp_operation_issupported(device->params, |
| 4042 | PTP_OC_ANDROID_TruncateObject) && |
| 4043 | ptp_operation_issupported(device->params, |
| 4044 | PTP_OC_ANDROID_BeginEditObject) && |
| 4045 | ptp_operation_issupported(device->params, |
| 4046 | PTP_OC_ANDROID_EndEditObject)); |
Philip Langdale | 1680c39 | 2018-03-17 09:23:25 +0100 | [diff] [blame] | 4047 | case LIBMTP_DEVICECAP_MoveObject: |
| 4048 | return ptp_operation_issupported(device->params, |
| 4049 | PTP_OC_MoveObject); |
| 4050 | case LIBMTP_DEVICECAP_CopyObject: |
| 4051 | return ptp_operation_issupported(device->params, |
| 4052 | PTP_OC_CopyObject); |
Linus Walleij | a389561 | 2013-04-29 21:56:38 +0200 | [diff] [blame] | 4053 | /* |
| 4054 | * Handle other capabilities here, this is also a good place to |
| 4055 | * blacklist some advanced operations on specific devices if need |
| 4056 | * be. |
| 4057 | */ |
| 4058 | |
| 4059 | default: |
| 4060 | break; |
| 4061 | } |
| 4062 | return 0; |
| 4063 | } |
| 4064 | |
| 4065 | /** |
Linus Walleij | 5b452bd | 2007-12-28 23:34:18 +0000 | [diff] [blame] | 4066 | * This function updates all the storage id's of a device and their |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 4067 | * properties, then creates a linked list and puts the list head into |
Linus Walleij | f849191 | 2006-12-15 10:23:30 +0000 | [diff] [blame] | 4068 | * the device struct. It also optionally sorts this list. If you want |
| 4069 | * to display storage information in your application you should call |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 4070 | * this function, then dereference the device struct |
Linus Walleij | 5b452bd | 2007-12-28 23:34:18 +0000 | [diff] [blame] | 4071 | * (<code>device->storage</code>) to get out information on the storage. |
| 4072 | * |
| 4073 | * You need to call this everytime you want to update the |
| 4074 | * <code>device->storage</code> list, for example anytime you need |
| 4075 | * to check available storage somewhere. |
| 4076 | * |
| 4077 | * <b>WARNING:</b> since this list is dynamically updated, do not |
| 4078 | * reference its fields in external applications by pointer! E.g |
| 4079 | * do not put a reference to any <code>char *</code> field. instead |
| 4080 | * <code>strncpy()</code> it! |
Linus Walleij | f849191 | 2006-12-15 10:23:30 +0000 | [diff] [blame] | 4081 | * |
Linus Walleij | e1b88e8 | 2008-07-02 20:12:53 +0000 | [diff] [blame] | 4082 | * @param device a pointer to the device to get the storage for. |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 4083 | * @param sortby an integer that determines the sorting of the storage list. |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 4084 | * Valid sort methods are defined in libmtp.h with beginning with |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 4085 | * LIBMTP_STORAGE_SORTBY_. 0 or LIBMTP_STORAGE_SORTBY_NOTSORTED to not |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 4086 | * sort. |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 4087 | * @return 0 on success, 1 success but only with storage id's, storage |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 4088 | * properities could not be retrieved and -1 means failure. |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 4089 | */ |
| 4090 | int LIBMTP_Get_Storage(LIBMTP_mtpdevice_t *device, int const sortby) |
| 4091 | { |
| 4092 | uint32_t i = 0; |
| 4093 | PTPStorageInfo storageInfo; |
| 4094 | PTPParams *params = (PTPParams *) device->params; |
| 4095 | PTPStorageIDs storageIDs; |
Linus Walleij | e1ac07e | 2006-12-14 19:38:59 +0000 | [diff] [blame] | 4096 | LIBMTP_devicestorage_t *storage = NULL; |
| 4097 | LIBMTP_devicestorage_t *storageprev = NULL; |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 4098 | |
| 4099 | if (device->storage != NULL) |
| 4100 | free_storage_list(device); |
| 4101 | |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 4102 | // if (!ptp_operation_issupported(params,PTP_OC_GetStorageIDs)) |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 4103 | // return -1; |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 4104 | if (ptp_getstorageids (params, &storageIDs) != PTP_RC_OK) |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 4105 | return -1; |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 4106 | if (storageIDs.n < 1) |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 4107 | return -1; |
| 4108 | |
| 4109 | if (!ptp_operation_issupported(params,PTP_OC_GetStorageInfo)) { |
| 4110 | for (i = 0; i < storageIDs.n; i++) { |
| 4111 | |
Linus Walleij | 43aba61 | 2011-11-25 09:40:34 +0100 | [diff] [blame] | 4112 | storage = (LIBMTP_devicestorage_t *) |
| 4113 | malloc(sizeof(LIBMTP_devicestorage_t)); |
Linus Walleij | e1ac07e | 2006-12-14 19:38:59 +0000 | [diff] [blame] | 4114 | storage->prev = storageprev; |
| 4115 | if (storageprev != NULL) |
| 4116 | storageprev->next = storage; |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 4117 | if (device->storage == NULL) |
Linus Walleij | e1ac07e | 2006-12-14 19:38:59 +0000 | [diff] [blame] | 4118 | device->storage = storage; |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 4119 | |
Linus Walleij | e1ac07e | 2006-12-14 19:38:59 +0000 | [diff] [blame] | 4120 | storage->id = storageIDs.Storage[i]; |
| 4121 | storage->StorageType = PTP_ST_Undefined; |
| 4122 | storage->FilesystemType = PTP_FST_Undefined; |
| 4123 | storage->AccessCapability = PTP_AC_ReadWrite; |
| 4124 | storage->MaxCapacity = (uint64_t) -1; |
| 4125 | storage->FreeSpaceInBytes = (uint64_t) -1; |
| 4126 | storage->FreeSpaceInObjects = (uint64_t) -1; |
| 4127 | storage->StorageDescription = strdup("Unknown storage"); |
| 4128 | storage->VolumeIdentifier = strdup("Unknown volume"); |
| 4129 | storage->next = NULL; |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 4130 | |
Linus Walleij | e1ac07e | 2006-12-14 19:38:59 +0000 | [diff] [blame] | 4131 | storageprev = storage; |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 4132 | } |
| 4133 | free(storageIDs.Storage); |
| 4134 | return 1; |
| 4135 | } else { |
| 4136 | for (i = 0; i < storageIDs.n; i++) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 4137 | uint16_t ret; |
| 4138 | ret = ptp_getstorageinfo(params, storageIDs.Storage[i], &storageInfo); |
| 4139 | if (ret != PTP_RC_OK) { |
Linus Walleij | 43aba61 | 2011-11-25 09:40:34 +0100 | [diff] [blame] | 4140 | add_ptp_error_to_errorstack(device, ret, "LIBMTP_Get_Storage(): " |
| 4141 | "Could not get storage info."); |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 4142 | if (device->storage != NULL) { |
| 4143 | free_storage_list(device); |
| 4144 | } |
| 4145 | return -1; |
| 4146 | } |
| 4147 | |
Linus Walleij | 43aba61 | 2011-11-25 09:40:34 +0100 | [diff] [blame] | 4148 | storage = (LIBMTP_devicestorage_t *) |
| 4149 | malloc(sizeof(LIBMTP_devicestorage_t)); |
Linus Walleij | e1ac07e | 2006-12-14 19:38:59 +0000 | [diff] [blame] | 4150 | storage->prev = storageprev; |
| 4151 | if (storageprev != NULL) |
| 4152 | storageprev->next = storage; |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 4153 | if (device->storage == NULL) |
Linus Walleij | e1ac07e | 2006-12-14 19:38:59 +0000 | [diff] [blame] | 4154 | device->storage = storage; |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 4155 | |
Linus Walleij | e1ac07e | 2006-12-14 19:38:59 +0000 | [diff] [blame] | 4156 | storage->id = storageIDs.Storage[i]; |
| 4157 | storage->StorageType = storageInfo.StorageType; |
| 4158 | storage->FilesystemType = storageInfo.FilesystemType; |
| 4159 | storage->AccessCapability = storageInfo.AccessCapability; |
| 4160 | storage->MaxCapacity = storageInfo.MaxCapability; |
| 4161 | storage->FreeSpaceInBytes = storageInfo.FreeSpaceInBytes; |
| 4162 | storage->FreeSpaceInObjects = storageInfo.FreeSpaceInImages; |
| 4163 | storage->StorageDescription = storageInfo.StorageDescription; |
| 4164 | storage->VolumeIdentifier = storageInfo.VolumeLabel; |
| 4165 | storage->next = NULL; |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 4166 | |
Linus Walleij | e1ac07e | 2006-12-14 19:38:59 +0000 | [diff] [blame] | 4167 | storageprev = storage; |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 4168 | } |
| 4169 | |
Linus Walleij | 5c1499e | 2009-02-21 06:54:29 +0000 | [diff] [blame] | 4170 | if (storage != NULL) |
| 4171 | storage->next = NULL; |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 4172 | |
| 4173 | sort_storage_by(device,sortby); |
| 4174 | free(storageIDs.Storage); |
| 4175 | return 0; |
| 4176 | } |
| 4177 | } |
| 4178 | |
| 4179 | /** |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 4180 | * This creates a new file metadata structure and allocates memory |
| 4181 | * for it. Notice that if you add strings to this structure they |
| 4182 | * will be freed by the corresponding <code>LIBMTP_destroy_file_t</code> |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 4183 | * operation later, so be careful of using strdup() when assigning |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 4184 | * strings, e.g.: |
| 4185 | * |
| 4186 | * <pre> |
| 4187 | * LIBMTP_file_t *file = LIBMTP_new_file_t(); |
| 4188 | * file->filename = strdup(namestr); |
| 4189 | * .... |
| 4190 | * LIBMTP_destroy_file_t(file); |
| 4191 | * </pre> |
| 4192 | * |
| 4193 | * @return a pointer to the newly allocated metadata structure. |
| 4194 | * @see LIBMTP_destroy_file_t() |
| 4195 | */ |
| 4196 | LIBMTP_file_t *LIBMTP_new_file_t(void) |
| 4197 | { |
| 4198 | LIBMTP_file_t *new = (LIBMTP_file_t *) malloc(sizeof(LIBMTP_file_t)); |
| 4199 | if (new == NULL) { |
| 4200 | return NULL; |
| 4201 | } |
| 4202 | new->filename = NULL; |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 4203 | new->item_id = 0; |
| 4204 | new->parent_id = 0; |
| 4205 | new->storage_id = 0; |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 4206 | new->filesize = 0; |
Richard Low | d3b1702 | 2009-04-11 12:37:39 +0000 | [diff] [blame] | 4207 | new->modificationdate = 0; |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 4208 | new->filetype = LIBMTP_FILETYPE_UNKNOWN; |
| 4209 | new->next = NULL; |
| 4210 | return new; |
| 4211 | } |
| 4212 | |
| 4213 | /** |
| 4214 | * This destroys a file metadata structure and deallocates the memory |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 4215 | * used by it, including any strings. Never use a file metadata |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 4216 | * structure again after calling this function on it. |
| 4217 | * @param file the file metadata to destroy. |
| 4218 | * @see LIBMTP_new_file_t() |
| 4219 | */ |
| 4220 | void LIBMTP_destroy_file_t(LIBMTP_file_t *file) |
| 4221 | { |
| 4222 | if (file == NULL) { |
| 4223 | return; |
| 4224 | } |
| 4225 | if (file->filename != NULL) |
| 4226 | free(file->filename); |
| 4227 | free(file); |
| 4228 | return; |
| 4229 | } |
| 4230 | |
| 4231 | /** |
Linus Walleij | a8b8889 | 2011-03-03 19:58:26 +0100 | [diff] [blame] | 4232 | * Helper function that takes one PTP object and creates a |
| 4233 | * LIBMTP_file_t metadata entry. |
| 4234 | */ |
| 4235 | static LIBMTP_file_t *obj2file(LIBMTP_mtpdevice_t *device, PTPObject *ob) |
| 4236 | { |
| 4237 | PTPParams *params = (PTPParams *) device->params; |
| 4238 | PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo; |
| 4239 | LIBMTP_file_t *file; |
| 4240 | int i; |
| 4241 | |
| 4242 | // Allocate a new file type |
| 4243 | file = LIBMTP_new_file_t(); |
| 4244 | |
| 4245 | file->parent_id = ob->oi.ParentObject; |
| 4246 | file->storage_id = ob->oi.StorageID; |
| 4247 | |
Florent Viard | a1af189 | 2017-11-06 18:27:10 +0100 | [diff] [blame] | 4248 | if (ob->oi.Filename != NULL) { |
| 4249 | file->filename = strdup(ob->oi.Filename); |
| 4250 | } |
| 4251 | |
Linus Walleij | a8b8889 | 2011-03-03 19:58:26 +0100 | [diff] [blame] | 4252 | // Set the filetype |
| 4253 | file->filetype = map_ptp_type_to_libmtp_type(ob->oi.ObjectFormat); |
| 4254 | |
| 4255 | /* |
| 4256 | * A special quirk for devices that doesn't quite |
| 4257 | * remember that some files marked as "unknown" type are |
| 4258 | * actually OGG or FLAC files. We look at the filename extension |
| 4259 | * and see if it happens that this was atleast named "ogg" or "flac" |
| 4260 | * and fall back on this heuristic approach in that case, |
| 4261 | * for these bugged devices only. |
| 4262 | */ |
| 4263 | if (file->filetype == LIBMTP_FILETYPE_UNKNOWN) { |
| 4264 | if ((FLAG_IRIVER_OGG_ALZHEIMER(ptp_usb) || |
Florent Viard | a1af189 | 2017-11-06 18:27:10 +0100 | [diff] [blame] | 4265 | FLAG_OGG_IS_UNKNOWN(ptp_usb)) && |
Linus Walleij | a8b8889 | 2011-03-03 19:58:26 +0100 | [diff] [blame] | 4266 | has_ogg_extension(file->filename)) { |
| 4267 | file->filetype = LIBMTP_FILETYPE_OGG; |
| 4268 | } |
| 4269 | |
| 4270 | if (FLAG_FLAC_IS_UNKNOWN(ptp_usb) && has_flac_extension(file->filename)) { |
| 4271 | file->filetype = LIBMTP_FILETYPE_FLAC; |
| 4272 | } |
| 4273 | } |
| 4274 | |
| 4275 | // Set the modification date |
| 4276 | file->modificationdate = ob->oi.ModificationDate; |
| 4277 | |
| 4278 | // We only have 32-bit file size here; later we use the PTP_OPC_ObjectSize property |
| 4279 | file->filesize = ob->oi.ObjectCompressedSize; |
Linus Walleij | a8b8889 | 2011-03-03 19:58:26 +0100 | [diff] [blame] | 4280 | |
| 4281 | // This is a unique ID so we can keep track of the file. |
| 4282 | file->item_id = ob->oid; |
| 4283 | |
| 4284 | /* |
| 4285 | * If we have a cached, large set of metadata, then use it! |
| 4286 | */ |
| 4287 | if (ob->mtpprops) { |
| 4288 | MTPProperties *prop = ob->mtpprops; |
| 4289 | |
| 4290 | for (i=0; i < ob->nrofmtpprops; i++, prop++) { |
| 4291 | // Pick ObjectSize here... |
| 4292 | if (prop->property == PTP_OPC_ObjectSize) { |
| 4293 | // This may already be set, but this 64bit precision value |
| 4294 | // is better than the PTP 32bit value, so let it override. |
| 4295 | if (device->object_bitsize == 64) { |
| 4296 | file->filesize = prop->propval.u64; |
| 4297 | } else { |
| 4298 | file->filesize = prop->propval.u32; |
| 4299 | } |
| 4300 | break; |
| 4301 | } |
| 4302 | } |
Lei Zhang | caa1bbc | 2013-02-13 15:12:02 -0800 | [diff] [blame] | 4303 | } else if (ptp_operation_issupported(params,PTP_OC_MTP_GetObjectPropsSupported)) { |
Linus Walleij | a8b8889 | 2011-03-03 19:58:26 +0100 | [diff] [blame] | 4304 | uint16_t *props = NULL; |
| 4305 | uint32_t propcnt = 0; |
| 4306 | int ret; |
| 4307 | |
| 4308 | // First see which properties can be retrieved for this object format |
| 4309 | ret = ptp_mtp_getobjectpropssupported(params, map_libmtp_type_to_ptp_type(file->filetype), &propcnt, &props); |
| 4310 | if (ret != PTP_RC_OK) { |
Linus Walleij | f46d7d9 | 2011-03-04 20:17:01 +0100 | [diff] [blame] | 4311 | add_ptp_error_to_errorstack(device, ret, "obj2file: call to ptp_mtp_getobjectpropssupported() failed."); |
Linus Walleij | a8b8889 | 2011-03-03 19:58:26 +0100 | [diff] [blame] | 4312 | // Silently fall through. |
| 4313 | } else { |
| 4314 | for (i = 0; i < propcnt; i++) { |
| 4315 | switch (props[i]) { |
| 4316 | case PTP_OPC_ObjectSize: |
| 4317 | if (device->object_bitsize == 64) { |
| 4318 | file->filesize = get_u64_from_object(device, file->item_id, PTP_OPC_ObjectSize, 0); |
| 4319 | } else { |
| 4320 | file->filesize = get_u32_from_object(device, file->item_id, PTP_OPC_ObjectSize, 0); |
| 4321 | } |
| 4322 | break; |
| 4323 | default: |
| 4324 | break; |
| 4325 | } |
| 4326 | } |
| 4327 | free(props); |
| 4328 | } |
| 4329 | } |
| 4330 | |
| 4331 | return file; |
| 4332 | } |
| 4333 | |
| 4334 | |
| 4335 | /** |
| 4336 | * This function retrieves the metadata for a single file off |
| 4337 | * the device. |
| 4338 | * |
| 4339 | * Do not call this function repeatedly! The file handles are linearly |
| 4340 | * searched O(n) and the call may involve (slow) USB traffic, so use |
| 4341 | * <code>LIBMTP_Get_Filelisting()</code> and cache the file, preferably |
| 4342 | * as an efficient data structure such as a hash list. |
| 4343 | * |
| 4344 | * Incidentally this function will return metadata for |
| 4345 | * a folder (association) as well, but this is not a proper use |
| 4346 | * of it, it is intended for file manipulation, not folder manipulation. |
| 4347 | * |
| 4348 | * @param device a pointer to the device to get the file metadata from. |
| 4349 | * @param fileid the object ID of the file that you want the metadata for. |
| 4350 | * @return a metadata entry on success or NULL on failure. |
| 4351 | * @see LIBMTP_Get_Filelisting() |
| 4352 | */ |
| 4353 | LIBMTP_file_t *LIBMTP_Get_Filemetadata(LIBMTP_mtpdevice_t *device, uint32_t const fileid) |
| 4354 | { |
| 4355 | PTPParams *params = (PTPParams *) device->params; |
| 4356 | uint16_t ret; |
| 4357 | PTPObject *ob; |
| 4358 | |
| 4359 | // Get all the handles if we haven't already done that |
| 4360 | // (Only on cached devices.) |
| 4361 | if (device->cached && params->nrofobjects == 0) { |
| 4362 | flush_handles(device); |
| 4363 | } |
| 4364 | |
| 4365 | ret = ptp_object_want(params, fileid, PTPOBJECT_OBJECTINFO_LOADED|PTPOBJECT_MTPPROPLIST_LOADED, &ob); |
| 4366 | if (ret != PTP_RC_OK) |
| 4367 | return NULL; |
| 4368 | |
| 4369 | return obj2file(device, ob); |
| 4370 | } |
| 4371 | |
| 4372 | /** |
mopoke | 3136444 | 2006-11-20 04:53:04 +0000 | [diff] [blame] | 4373 | * THIS FUNCTION IS DEPRECATED. PLEASE UPDATE YOUR CODE IN ORDER |
Richard Low | dc0b6c7 | 2006-11-13 09:22:23 +0000 | [diff] [blame] | 4374 | * NOT TO USE IT. |
| 4375 | * @see LIBMTP_Get_Filelisting_With_Callback() |
| 4376 | */ |
| 4377 | LIBMTP_file_t *LIBMTP_Get_Filelisting(LIBMTP_mtpdevice_t *device) |
| 4378 | { |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 4379 | LIBMTP_INFO("WARNING: LIBMTP_Get_Filelisting() is deprecated.\n"); |
| 4380 | LIBMTP_INFO("WARNING: please update your code to use LIBMTP_Get_Filelisting_With_Callback()\n"); |
Richard Low | dc0b6c7 | 2006-11-13 09:22:23 +0000 | [diff] [blame] | 4381 | return LIBMTP_Get_Filelisting_With_Callback(device, NULL, NULL); |
| 4382 | } |
| 4383 | |
| 4384 | /** |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 4385 | * This returns a long list of all files available |
Linus Walleij | 25d5c21 | 2008-08-14 06:49:13 +0000 | [diff] [blame] | 4386 | * on the current MTP device. Folders will not be returned, but abstract |
| 4387 | * entities like playlists and albums will show up as "files". Typical usage: |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 4388 | * |
| 4389 | * <pre> |
| 4390 | * LIBMTP_file_t *filelist; |
| 4391 | * |
Richard Low | dc0b6c7 | 2006-11-13 09:22:23 +0000 | [diff] [blame] | 4392 | * filelist = LIBMTP_Get_Filelisting_With_Callback(device, callback, data); |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 4393 | * while (filelist != NULL) { |
| 4394 | * LIBMTP_file_t *tmp; |
| 4395 | * |
| 4396 | * // Do something on each element in the list here... |
| 4397 | * tmp = filelist; |
| 4398 | * filelist = filelist->next; |
| 4399 | * LIBMTP_destroy_file_t(tmp); |
| 4400 | * } |
| 4401 | * </pre> |
| 4402 | * |
Linus Walleij | 25d5c21 | 2008-08-14 06:49:13 +0000 | [diff] [blame] | 4403 | * If you want to group your file listing by storage (per storage unit) or |
| 4404 | * arrange files into folders, you must dereference the <code>storage_id</code> |
| 4405 | * and/or <code>parent_id</code> field of the returned <code>LIBMTP_file_t</code> |
| 4406 | * struct. To arrange by folders or files you typically have to create the proper |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 4407 | * trees by calls to <code>LIBMTP_Get_Storage()</code> and/or |
Linus Walleij | 25d5c21 | 2008-08-14 06:49:13 +0000 | [diff] [blame] | 4408 | * <code>LIBMTP_Get_Folder_List()</code> first. |
| 4409 | * |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 4410 | * @param device a pointer to the device to get the file listing for. |
Richard Low | dc0b6c7 | 2006-11-13 09:22:23 +0000 | [diff] [blame] | 4411 | * @param callback a function to be called during the tracklisting retrieveal |
Linus Walleij | 25d5c21 | 2008-08-14 06:49:13 +0000 | [diff] [blame] | 4412 | * for displaying progress bars etc, or NULL if you don't want |
| 4413 | * any callbacks. |
Richard Low | dc0b6c7 | 2006-11-13 09:22:23 +0000 | [diff] [blame] | 4414 | * @param data a user-defined pointer that is passed along to |
Linus Walleij | 25d5c21 | 2008-08-14 06:49:13 +0000 | [diff] [blame] | 4415 | * the <code>progress</code> function in order to |
| 4416 | * pass along some user defined data to the progress |
| 4417 | * updates. If not used, set this to NULL. |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 4418 | * @return a list of files that can be followed using the <code>next</code> |
Linus Walleij | 25d5c21 | 2008-08-14 06:49:13 +0000 | [diff] [blame] | 4419 | * field of the <code>LIBMTP_file_t</code> data structure. |
| 4420 | * Each of the metadata tags must be freed after use, and may |
| 4421 | * contain only partial metadata information, i.e. one or several |
| 4422 | * fields may be NULL or 0. |
Linus Walleij | 2e4b5f9 | 2006-06-16 14:00:49 +0000 | [diff] [blame] | 4423 | * @see LIBMTP_Get_Filemetadata() |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 4424 | */ |
Richard Low | dc0b6c7 | 2006-11-13 09:22:23 +0000 | [diff] [blame] | 4425 | LIBMTP_file_t *LIBMTP_Get_Filelisting_With_Callback(LIBMTP_mtpdevice_t *device, |
| 4426 | LIBMTP_progressfunc_t const callback, |
| 4427 | void const * const data) |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 4428 | { |
| 4429 | uint32_t i = 0; |
| 4430 | LIBMTP_file_t *retfiles = NULL; |
| 4431 | LIBMTP_file_t *curfile = NULL; |
| 4432 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 4433 | |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 4434 | // Get all the handles if we haven't already done that |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 4435 | if (params->nrofobjects == 0) { |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 4436 | flush_handles(device); |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 4437 | } |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 4438 | |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 4439 | for (i = 0; i < params->nrofobjects; i++) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 4440 | LIBMTP_file_t *file; |
Linus Walleij | a8b8889 | 2011-03-03 19:58:26 +0100 | [diff] [blame] | 4441 | PTPObject *ob; |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 4442 | |
Richard Low | dc0b6c7 | 2006-11-13 09:22:23 +0000 | [diff] [blame] | 4443 | if (callback != NULL) |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 4444 | callback(i, params->nrofobjects, data); |
mopoke | 3136444 | 2006-11-20 04:53:04 +0000 | [diff] [blame] | 4445 | |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 4446 | ob = ¶ms->objects[i]; |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 4447 | |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 4448 | if (ob->oi.ObjectFormat == PTP_OFC_Association) { |
Linus Walleij | d9d28d5 | 2007-08-04 19:01:18 +0000 | [diff] [blame] | 4449 | // MTP use this object format for folders which means |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 4450 | // these "files" will turn up on a folder listing instead. |
| 4451 | continue; |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 4452 | } |
| 4453 | |
Linus Walleij | a8b8889 | 2011-03-03 19:58:26 +0100 | [diff] [blame] | 4454 | // Look up metadata |
| 4455 | file = obj2file(device, ob); |
| 4456 | if (file == NULL) { |
| 4457 | continue; |
Linus Walleij | d9d28d5 | 2007-08-04 19:01:18 +0000 | [diff] [blame] | 4458 | } |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 4459 | |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 4460 | // Add track to a list that will be returned afterwards. |
| 4461 | if (retfiles == NULL) { |
| 4462 | retfiles = file; |
| 4463 | curfile = file; |
| 4464 | } else { |
| 4465 | curfile->next = file; |
| 4466 | curfile = file; |
| 4467 | } |
| 4468 | |
| 4469 | // Call listing callback |
| 4470 | // double progressPercent = (double)i*(double)100.0 / (double)params->handles.n; |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 4471 | |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 4472 | } // Handle counting loop |
| 4473 | return retfiles; |
| 4474 | } |
| 4475 | |
| 4476 | /** |
Linus Walleij | 71c7929 | 2011-03-05 22:08:07 +0100 | [diff] [blame] | 4477 | * This function retrieves the contents of a certain folder |
| 4478 | * with id parent on a certain storage on a certain device. |
| 4479 | * The result contains both files and folders. |
| 4480 | * The device used with this operations must have been opened with |
| 4481 | * LIBMTP_Open_Raw_Device_Uncached() or it will fail. |
| 4482 | * |
| 4483 | * NOTE: the request will always perform I/O with the device. |
| 4484 | * @param device a pointer to the MTP device to report info from. |
| 4485 | * @param storage a storage on the device to report info from. If |
Linus Walleij | 1e4dfea | 2011-03-05 22:50:39 +0100 | [diff] [blame] | 4486 | * 0 is passed in, the files for the given parent will be |
Linus Walleij | 71c7929 | 2011-03-05 22:08:07 +0100 | [diff] [blame] | 4487 | * searched across all available storages. |
| 4488 | * @param parent the parent folder id. |
Linus Walleij | 2e4b5f9 | 2006-06-16 14:00:49 +0000 | [diff] [blame] | 4489 | */ |
Linus Walleij | 71c7929 | 2011-03-05 22:08:07 +0100 | [diff] [blame] | 4490 | LIBMTP_file_t * LIBMTP_Get_Files_And_Folders(LIBMTP_mtpdevice_t *device, |
Linus Walleij | 8a19928 | 2011-03-05 22:28:41 +0100 | [diff] [blame] | 4491 | uint32_t const storage, |
Linus Walleij | 71c7929 | 2011-03-05 22:08:07 +0100 | [diff] [blame] | 4492 | uint32_t const parent) |
Linus Walleij | 2e4b5f9 | 2006-06-16 14:00:49 +0000 | [diff] [blame] | 4493 | { |
Linus Walleij | 71c7929 | 2011-03-05 22:08:07 +0100 | [diff] [blame] | 4494 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | 6ccbe96 | 2011-11-25 09:40:00 +0100 | [diff] [blame] | 4495 | PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo; |
Linus Walleij | 71c7929 | 2011-03-05 22:08:07 +0100 | [diff] [blame] | 4496 | LIBMTP_file_t *retfiles = NULL; |
| 4497 | LIBMTP_file_t *curfile = NULL; |
Linus Walleij | a8b8889 | 2011-03-03 19:58:26 +0100 | [diff] [blame] | 4498 | PTPObjectHandles currentHandles; |
Linus Walleij | 71c7929 | 2011-03-05 22:08:07 +0100 | [diff] [blame] | 4499 | uint32_t storageid; |
| 4500 | uint16_t ret; |
| 4501 | int i = 0; |
Linus Walleij | 2e4b5f9 | 2006-06-16 14:00:49 +0000 | [diff] [blame] | 4502 | |
Linus Walleij | 71c7929 | 2011-03-05 22:08:07 +0100 | [diff] [blame] | 4503 | if (device->cached) { |
| 4504 | // This function is only supposed to be used by devices |
| 4505 | // opened as uncached! |
| 4506 | LIBMTP_ERROR("tried to use %s on a cached device!\n", |
| 4507 | __func__); |
| 4508 | return NULL; |
Linus Walleij | 366dd1c | 2011-03-04 20:27:03 +0100 | [diff] [blame] | 4509 | } |
| 4510 | |
Linus Walleij | 6ccbe96 | 2011-11-25 09:40:00 +0100 | [diff] [blame] | 4511 | if (FLAG_BROKEN_GET_OBJECT_PROPVAL(ptp_usb)) { |
| 4512 | // These devices cannot handle the commands needed for |
| 4513 | // Uncached access! |
| 4514 | LIBMTP_ERROR("tried to use %s on an unsupported device, " |
| 4515 | "this command does not work on all devices " |
| 4516 | "due to missing low-level support to read " |
| 4517 | "information on individual tracks\n", |
| 4518 | __func__); |
| 4519 | return NULL; |
| 4520 | } |
| 4521 | |
Linus Walleij | 1e4dfea | 2011-03-05 22:50:39 +0100 | [diff] [blame] | 4522 | if (storage == 0) |
Linus Walleij | 71c7929 | 2011-03-05 22:08:07 +0100 | [diff] [blame] | 4523 | storageid = PTP_GOH_ALL_STORAGE; |
| 4524 | else |
Linus Walleij | 8a19928 | 2011-03-05 22:28:41 +0100 | [diff] [blame] | 4525 | storageid = storage; |
Linus Walleij | 71c7929 | 2011-03-05 22:08:07 +0100 | [diff] [blame] | 4526 | |
| 4527 | ret = ptp_getobjecthandles(params, |
| 4528 | storageid, |
| 4529 | PTP_GOH_ALL_FORMATS, |
| 4530 | parent, |
| 4531 | ¤tHandles); |
Linus Walleij | a8b8889 | 2011-03-03 19:58:26 +0100 | [diff] [blame] | 4532 | |
| 4533 | if (ret != PTP_RC_OK) { |
Linus Walleij | 71c7929 | 2011-03-05 22:08:07 +0100 | [diff] [blame] | 4534 | add_ptp_error_to_errorstack(device, ret, |
| 4535 | "LIBMTP_Get_Files_And_Folders(): could not get object handles."); |
Linus Walleij | a8b8889 | 2011-03-03 19:58:26 +0100 | [diff] [blame] | 4536 | return NULL; |
Linus Walleij | 2e4b5f9 | 2006-06-16 14:00:49 +0000 | [diff] [blame] | 4537 | } |
| 4538 | |
Linus Walleij | a8b8889 | 2011-03-03 19:58:26 +0100 | [diff] [blame] | 4539 | if (currentHandles.Handler == NULL || currentHandles.n == 0) |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 4540 | return NULL; |
Linus Walleij | 2e4b5f9 | 2006-06-16 14:00:49 +0000 | [diff] [blame] | 4541 | |
Linus Walleij | a8b8889 | 2011-03-03 19:58:26 +0100 | [diff] [blame] | 4542 | for (i = 0; i < currentHandles.n; i++) { |
| 4543 | LIBMTP_file_t *file; |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 4544 | |
Linus Walleij | a8b8889 | 2011-03-03 19:58:26 +0100 | [diff] [blame] | 4545 | // Get metadata for one file, if it fails, try next file |
| 4546 | file = LIBMTP_Get_Filemetadata(device, currentHandles.Handler[i]); |
| 4547 | if (file == NULL) |
| 4548 | continue; |
Linus Walleij | 2e4b5f9 | 2006-06-16 14:00:49 +0000 | [diff] [blame] | 4549 | |
Linus Walleij | a8b8889 | 2011-03-03 19:58:26 +0100 | [diff] [blame] | 4550 | // Add track to a list that will be returned afterwards. |
Linus Walleij | 366dd1c | 2011-03-04 20:27:03 +0100 | [diff] [blame] | 4551 | if (curfile == NULL) { |
Linus Walleij | a8b8889 | 2011-03-03 19:58:26 +0100 | [diff] [blame] | 4552 | curfile = file; |
Linus Walleij | 71c7929 | 2011-03-05 22:08:07 +0100 | [diff] [blame] | 4553 | retfiles = file; |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 4554 | } else { |
Linus Walleij | a8b8889 | 2011-03-03 19:58:26 +0100 | [diff] [blame] | 4555 | curfile->next = file; |
| 4556 | curfile = file; |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 4557 | } |
| 4558 | } |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 4559 | |
Linus Walleij | a8b8889 | 2011-03-03 19:58:26 +0100 | [diff] [blame] | 4560 | free(currentHandles.Handler); |
| 4561 | |
Linus Walleij | 366dd1c | 2011-03-04 20:27:03 +0100 | [diff] [blame] | 4562 | // Return a pointer to the original first file |
| 4563 | // in the big list. |
Linus Walleij | a8b8889 | 2011-03-03 19:58:26 +0100 | [diff] [blame] | 4564 | return retfiles; |
Linus Walleij | 2e4b5f9 | 2006-06-16 14:00:49 +0000 | [diff] [blame] | 4565 | } |
| 4566 | |
Linus Walleij | a8b8889 | 2011-03-03 19:58:26 +0100 | [diff] [blame] | 4567 | |
| 4568 | /** |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 4569 | * This creates a new track metadata structure and allocates memory |
| 4570 | * for it. Notice that if you add strings to this structure they |
| 4571 | * will be freed by the corresponding <code>LIBMTP_destroy_track_t</code> |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 4572 | * operation later, so be careful of using strdup() when assigning |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 4573 | * strings, e.g.: |
| 4574 | * |
Linus Walleij | 17e39f7 | 2006-02-23 15:54:28 +0000 | [diff] [blame] | 4575 | * <pre> |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 4576 | * LIBMTP_track_t *track = LIBMTP_new_track_t(); |
| 4577 | * track->title = strdup(titlestr); |
| 4578 | * .... |
| 4579 | * LIBMTP_destroy_track_t(track); |
Linus Walleij | 17e39f7 | 2006-02-23 15:54:28 +0000 | [diff] [blame] | 4580 | * </pre> |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 4581 | * |
| 4582 | * @return a pointer to the newly allocated metadata structure. |
| 4583 | * @see LIBMTP_destroy_track_t() |
| 4584 | */ |
| 4585 | LIBMTP_track_t *LIBMTP_new_track_t(void) |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 4586 | { |
| 4587 | LIBMTP_track_t *new = (LIBMTP_track_t *) malloc(sizeof(LIBMTP_track_t)); |
| 4588 | if (new == NULL) { |
| 4589 | return NULL; |
| 4590 | } |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 4591 | new->item_id = 0; |
| 4592 | new->parent_id = 0; |
| 4593 | new->storage_id = 0; |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 4594 | new->title = NULL; |
| 4595 | new->artist = NULL; |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 4596 | new->composer = NULL; |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 4597 | new->album = NULL; |
| 4598 | new->genre = NULL; |
| 4599 | new->date = NULL; |
| 4600 | new->filename = NULL; |
| 4601 | new->duration = 0; |
| 4602 | new->tracknumber = 0; |
| 4603 | new->filesize = 0; |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 4604 | new->filetype = LIBMTP_FILETYPE_UNKNOWN; |
Linus Walleij | cf223e6 | 2006-06-19 09:31:53 +0000 | [diff] [blame] | 4605 | new->samplerate = 0; |
| 4606 | new->nochannels = 0; |
| 4607 | new->wavecodec = 0; |
| 4608 | new->bitrate = 0; |
| 4609 | new->bitratetype = 0; |
| 4610 | new->rating = 0; |
| 4611 | new->usecount = 0; |
Richard Low | d3b1702 | 2009-04-11 12:37:39 +0000 | [diff] [blame] | 4612 | new->modificationdate = 0; |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 4613 | new->next = NULL; |
| 4614 | return new; |
| 4615 | } |
| 4616 | |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 4617 | /** |
| 4618 | * This destroys a track metadata structure and deallocates the memory |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 4619 | * used by it, including any strings. Never use a track metadata |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 4620 | * structure again after calling this function on it. |
| 4621 | * @param track the track metadata to destroy. |
| 4622 | * @see LIBMTP_new_track_t() |
| 4623 | */ |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 4624 | void LIBMTP_destroy_track_t(LIBMTP_track_t *track) |
| 4625 | { |
| 4626 | if (track == NULL) { |
| 4627 | return; |
| 4628 | } |
| 4629 | if (track->title != NULL) |
| 4630 | free(track->title); |
| 4631 | if (track->artist != NULL) |
| 4632 | free(track->artist); |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 4633 | if (track->composer != NULL) |
| 4634 | free(track->composer); |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 4635 | if (track->album != NULL) |
| 4636 | free(track->album); |
| 4637 | if (track->genre != NULL) |
| 4638 | free(track->genre); |
| 4639 | if (track->date != NULL) |
| 4640 | free(track->date); |
| 4641 | if (track->filename != NULL) |
| 4642 | free(track->filename); |
| 4643 | free(track); |
| 4644 | return; |
| 4645 | } |
| 4646 | |
| 4647 | /** |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 4648 | * This function maps and copies a property onto the track metadata if applicable. |
| 4649 | */ |
Linus Walleij | ddaba2f | 2007-10-02 21:20:30 +0000 | [diff] [blame] | 4650 | static void pick_property_to_track_metadata(LIBMTP_mtpdevice_t *device, MTPProperties *prop, LIBMTP_track_t *track) |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 4651 | { |
| 4652 | switch (prop->property) { |
| 4653 | case PTP_OPC_Name: |
| 4654 | if (prop->propval.str != NULL) |
| 4655 | track->title = strdup(prop->propval.str); |
| 4656 | else |
| 4657 | track->title = NULL; |
| 4658 | break; |
| 4659 | case PTP_OPC_Artist: |
| 4660 | if (prop->propval.str != NULL) |
| 4661 | track->artist = strdup(prop->propval.str); |
| 4662 | else |
| 4663 | track->artist = NULL; |
| 4664 | break; |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 4665 | case PTP_OPC_Composer: |
| 4666 | if (prop->propval.str != NULL) |
| 4667 | track->composer = strdup(prop->propval.str); |
| 4668 | else |
| 4669 | track->composer = NULL; |
| 4670 | break; |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 4671 | case PTP_OPC_Duration: |
| 4672 | track->duration = prop->propval.u32; |
| 4673 | break; |
| 4674 | case PTP_OPC_Track: |
| 4675 | track->tracknumber = prop->propval.u16; |
| 4676 | break; |
| 4677 | case PTP_OPC_Genre: |
| 4678 | if (prop->propval.str != NULL) |
| 4679 | track->genre = strdup(prop->propval.str); |
| 4680 | else |
| 4681 | track->genre = NULL; |
| 4682 | break; |
| 4683 | case PTP_OPC_AlbumName: |
| 4684 | if (prop->propval.str != NULL) |
| 4685 | track->album = strdup(prop->propval.str); |
| 4686 | else |
| 4687 | track->album = NULL; |
| 4688 | break; |
| 4689 | case PTP_OPC_OriginalReleaseDate: |
| 4690 | if (prop->propval.str != NULL) |
| 4691 | track->date = strdup(prop->propval.str); |
| 4692 | else |
| 4693 | track->date = NULL; |
| 4694 | break; |
| 4695 | // These are, well not so important. |
| 4696 | case PTP_OPC_SampleRate: |
| 4697 | track->samplerate = prop->propval.u32; |
| 4698 | break; |
| 4699 | case PTP_OPC_NumberOfChannels: |
| 4700 | track->nochannels = prop->propval.u16; |
| 4701 | break; |
| 4702 | case PTP_OPC_AudioWAVECodec: |
| 4703 | track->wavecodec = prop->propval.u32; |
| 4704 | break; |
| 4705 | case PTP_OPC_AudioBitRate: |
| 4706 | track->bitrate = prop->propval.u32; |
| 4707 | break; |
| 4708 | case PTP_OPC_BitRateType: |
| 4709 | track->bitratetype = prop->propval.u16; |
| 4710 | break; |
| 4711 | case PTP_OPC_Rating: |
| 4712 | track->rating = prop->propval.u16; |
| 4713 | break; |
| 4714 | case PTP_OPC_UseCount: |
| 4715 | track->usecount = prop->propval.u32; |
| 4716 | break; |
Linus Walleij | d9d28d5 | 2007-08-04 19:01:18 +0000 | [diff] [blame] | 4717 | case PTP_OPC_ObjectSize: |
Linus Walleij | ddaba2f | 2007-10-02 21:20:30 +0000 | [diff] [blame] | 4718 | if (device->object_bitsize == 64) { |
| 4719 | track->filesize = prop->propval.u64; |
| 4720 | } else { |
| 4721 | track->filesize = prop->propval.u32; |
Linus Walleij | ca2a170 | 2007-10-02 20:41:45 +0000 | [diff] [blame] | 4722 | } |
Linus Walleij | d9d28d5 | 2007-08-04 19:01:18 +0000 | [diff] [blame] | 4723 | break; |
| 4724 | default: |
| 4725 | break; |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 4726 | } |
| 4727 | } |
| 4728 | |
| 4729 | /** |
Linus Walleij | 8ab5426 | 2006-06-21 07:12:28 +0000 | [diff] [blame] | 4730 | * This function retrieves the track metadata for a track |
| 4731 | * given by a unique ID. |
| 4732 | * @param device a pointer to the device to get the track metadata off. |
| 4733 | * @param trackid the unique ID of the track. |
| 4734 | * @param objectformat the object format of this track, so we know what it supports. |
| 4735 | * @param track a metadata set to fill in. |
| 4736 | */ |
| 4737 | static void get_track_metadata(LIBMTP_mtpdevice_t *device, uint16_t objectformat, |
| 4738 | LIBMTP_track_t *track) |
| 4739 | { |
Linus Walleij | 00cf064 | 2006-07-26 20:40:59 +0000 | [diff] [blame] | 4740 | uint16_t ret; |
| 4741 | PTPParams *params = (PTPParams *) device->params; |
| 4742 | uint32_t i; |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 4743 | MTPProperties *prop; |
| 4744 | PTPObject *ob; |
Linus Walleij | 00cf064 | 2006-07-26 20:40:59 +0000 | [diff] [blame] | 4745 | |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 4746 | /* |
| 4747 | * If we have a cached, large set of metadata, then use it! |
| 4748 | */ |
Linus Walleij | 08a5fe1 | 2009-09-08 21:28:58 +0000 | [diff] [blame] | 4749 | ret = ptp_object_want(params, track->item_id, PTPOBJECT_MTPPROPLIST_LOADED, &ob); |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 4750 | if (ob->mtpprops) { |
| 4751 | prop = ob->mtpprops; |
| 4752 | for (i=0;i<ob->nrofmtpprops;i++,prop++) |
Linus Walleij | ddaba2f | 2007-10-02 21:20:30 +0000 | [diff] [blame] | 4753 | pick_property_to_track_metadata(device, prop, track); |
Linus Walleij | 3fcfea5 | 2006-11-13 07:07:36 +0000 | [diff] [blame] | 4754 | } else { |
Linus Walleij | 3fcfea5 | 2006-11-13 07:07:36 +0000 | [diff] [blame] | 4755 | uint16_t *props = NULL; |
| 4756 | uint32_t propcnt = 0; |
| 4757 | |
| 4758 | // First see which properties can be retrieved for this object format |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 4759 | ret = ptp_mtp_getobjectpropssupported(params, map_libmtp_type_to_ptp_type(track->filetype), &propcnt, &props); |
Linus Walleij | 3fcfea5 | 2006-11-13 07:07:36 +0000 | [diff] [blame] | 4760 | if (ret != PTP_RC_OK) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 4761 | add_ptp_error_to_errorstack(device, ret, "get_track_metadata(): call to ptp_mtp_getobjectpropssupported() failed."); |
Linus Walleij | 3fcfea5 | 2006-11-13 07:07:36 +0000 | [diff] [blame] | 4762 | // Just bail out for now, nothing is ever set. |
| 4763 | return; |
| 4764 | } else { |
| 4765 | for (i=0;i<propcnt;i++) { |
| 4766 | switch (props[i]) { |
| 4767 | case PTP_OPC_Name: |
Linus Walleij | 9901e22 | 2006-11-30 12:28:19 +0000 | [diff] [blame] | 4768 | track->title = get_string_from_object(device, track->item_id, PTP_OPC_Name); |
Linus Walleij | 3fcfea5 | 2006-11-13 07:07:36 +0000 | [diff] [blame] | 4769 | break; |
| 4770 | case PTP_OPC_Artist: |
Linus Walleij | 9901e22 | 2006-11-30 12:28:19 +0000 | [diff] [blame] | 4771 | track->artist = get_string_from_object(device, track->item_id, PTP_OPC_Artist); |
Linus Walleij | 3fcfea5 | 2006-11-13 07:07:36 +0000 | [diff] [blame] | 4772 | break; |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 4773 | case PTP_OPC_Composer: |
| 4774 | track->composer = get_string_from_object(device, track->item_id, PTP_OPC_Composer); |
| 4775 | break; |
Linus Walleij | 3fcfea5 | 2006-11-13 07:07:36 +0000 | [diff] [blame] | 4776 | case PTP_OPC_Duration: |
Linus Walleij | 9901e22 | 2006-11-30 12:28:19 +0000 | [diff] [blame] | 4777 | track->duration = get_u32_from_object(device, track->item_id, PTP_OPC_Duration, 0); |
Linus Walleij | 3fcfea5 | 2006-11-13 07:07:36 +0000 | [diff] [blame] | 4778 | break; |
| 4779 | case PTP_OPC_Track: |
Linus Walleij | 9901e22 | 2006-11-30 12:28:19 +0000 | [diff] [blame] | 4780 | track->tracknumber = get_u16_from_object(device, track->item_id, PTP_OPC_Track, 0); |
Linus Walleij | 3fcfea5 | 2006-11-13 07:07:36 +0000 | [diff] [blame] | 4781 | break; |
| 4782 | case PTP_OPC_Genre: |
Linus Walleij | 9901e22 | 2006-11-30 12:28:19 +0000 | [diff] [blame] | 4783 | track->genre = get_string_from_object(device, track->item_id, PTP_OPC_Genre); |
Linus Walleij | 3fcfea5 | 2006-11-13 07:07:36 +0000 | [diff] [blame] | 4784 | break; |
| 4785 | case PTP_OPC_AlbumName: |
Linus Walleij | 9901e22 | 2006-11-30 12:28:19 +0000 | [diff] [blame] | 4786 | track->album = get_string_from_object(device, track->item_id, PTP_OPC_AlbumName); |
Linus Walleij | 3fcfea5 | 2006-11-13 07:07:36 +0000 | [diff] [blame] | 4787 | break; |
| 4788 | case PTP_OPC_OriginalReleaseDate: |
Linus Walleij | 9901e22 | 2006-11-30 12:28:19 +0000 | [diff] [blame] | 4789 | track->date = get_string_from_object(device, track->item_id, PTP_OPC_OriginalReleaseDate); |
Linus Walleij | 3fcfea5 | 2006-11-13 07:07:36 +0000 | [diff] [blame] | 4790 | break; |
| 4791 | // These are, well not so important. |
| 4792 | case PTP_OPC_SampleRate: |
Linus Walleij | 9901e22 | 2006-11-30 12:28:19 +0000 | [diff] [blame] | 4793 | track->samplerate = get_u32_from_object(device, track->item_id, PTP_OPC_SampleRate, 0); |
Linus Walleij | 3fcfea5 | 2006-11-13 07:07:36 +0000 | [diff] [blame] | 4794 | break; |
| 4795 | case PTP_OPC_NumberOfChannels: |
Linus Walleij | 9901e22 | 2006-11-30 12:28:19 +0000 | [diff] [blame] | 4796 | track->nochannels = get_u16_from_object(device, track->item_id, PTP_OPC_NumberOfChannels, 0); |
Linus Walleij | 3fcfea5 | 2006-11-13 07:07:36 +0000 | [diff] [blame] | 4797 | break; |
| 4798 | case PTP_OPC_AudioWAVECodec: |
Linus Walleij | 9901e22 | 2006-11-30 12:28:19 +0000 | [diff] [blame] | 4799 | track->wavecodec = get_u32_from_object(device, track->item_id, PTP_OPC_AudioWAVECodec, 0); |
Linus Walleij | 3fcfea5 | 2006-11-13 07:07:36 +0000 | [diff] [blame] | 4800 | break; |
| 4801 | case PTP_OPC_AudioBitRate: |
Linus Walleij | 9901e22 | 2006-11-30 12:28:19 +0000 | [diff] [blame] | 4802 | track->bitrate = get_u32_from_object(device, track->item_id, PTP_OPC_AudioBitRate, 0); |
Linus Walleij | 3fcfea5 | 2006-11-13 07:07:36 +0000 | [diff] [blame] | 4803 | break; |
| 4804 | case PTP_OPC_BitRateType: |
Linus Walleij | 9901e22 | 2006-11-30 12:28:19 +0000 | [diff] [blame] | 4805 | track->bitratetype = get_u16_from_object(device, track->item_id, PTP_OPC_BitRateType, 0); |
Linus Walleij | 3fcfea5 | 2006-11-13 07:07:36 +0000 | [diff] [blame] | 4806 | break; |
| 4807 | case PTP_OPC_Rating: |
Linus Walleij | 9901e22 | 2006-11-30 12:28:19 +0000 | [diff] [blame] | 4808 | track->rating = get_u16_from_object(device, track->item_id, PTP_OPC_Rating, 0); |
Linus Walleij | 3fcfea5 | 2006-11-13 07:07:36 +0000 | [diff] [blame] | 4809 | break; |
| 4810 | case PTP_OPC_UseCount: |
Linus Walleij | 9901e22 | 2006-11-30 12:28:19 +0000 | [diff] [blame] | 4811 | track->usecount = get_u32_from_object(device, track->item_id, PTP_OPC_UseCount, 0); |
Linus Walleij | 3fcfea5 | 2006-11-13 07:07:36 +0000 | [diff] [blame] | 4812 | break; |
Linus Walleij | d9d28d5 | 2007-08-04 19:01:18 +0000 | [diff] [blame] | 4813 | case PTP_OPC_ObjectSize: |
Linus Walleij | ddaba2f | 2007-10-02 21:20:30 +0000 | [diff] [blame] | 4814 | if (device->object_bitsize == 64) { |
| 4815 | track->filesize = get_u64_from_object(device, track->item_id, PTP_OPC_ObjectSize, 0); |
| 4816 | } else { |
| 4817 | track->filesize = (uint64_t) get_u32_from_object(device, track->item_id, PTP_OPC_ObjectSize, 0); |
Linus Walleij | ca2a170 | 2007-10-02 20:41:45 +0000 | [diff] [blame] | 4818 | } |
Linus Walleij | d9d28d5 | 2007-08-04 19:01:18 +0000 | [diff] [blame] | 4819 | break; |
Linus Walleij | 3fcfea5 | 2006-11-13 07:07:36 +0000 | [diff] [blame] | 4820 | } |
| 4821 | } |
| 4822 | free(props); |
| 4823 | } |
Linus Walleij | 00cf064 | 2006-07-26 20:40:59 +0000 | [diff] [blame] | 4824 | } |
Linus Walleij | 8ab5426 | 2006-06-21 07:12:28 +0000 | [diff] [blame] | 4825 | } |
| 4826 | |
| 4827 | /** |
mopoke | 3136444 | 2006-11-20 04:53:04 +0000 | [diff] [blame] | 4828 | * THIS FUNCTION IS DEPRECATED. PLEASE UPDATE YOUR CODE IN ORDER |
Linus Walleij | 3fcfea5 | 2006-11-13 07:07:36 +0000 | [diff] [blame] | 4829 | * NOT TO USE IT. |
| 4830 | * @see LIBMTP_Get_Tracklisting_With_Callback() |
| 4831 | */ |
| 4832 | LIBMTP_track_t *LIBMTP_Get_Tracklisting(LIBMTP_mtpdevice_t *device) |
| 4833 | { |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 4834 | LIBMTP_INFO("WARNING: LIBMTP_Get_Tracklisting() is deprecated.\n"); |
| 4835 | LIBMTP_INFO("WARNING: please update your code to use LIBMTP_Get_Tracklisting_With_Callback()\n"); |
Richard Low | dc0b6c7 | 2006-11-13 09:22:23 +0000 | [diff] [blame] | 4836 | return LIBMTP_Get_Tracklisting_With_Callback(device, NULL, NULL); |
Linus Walleij | 3fcfea5 | 2006-11-13 07:07:36 +0000 | [diff] [blame] | 4837 | } |
| 4838 | |
| 4839 | /** |
Linus Walleij | 25d5c21 | 2008-08-14 06:49:13 +0000 | [diff] [blame] | 4840 | * This returns a long list of all tracks available on the current MTP device. |
| 4841 | * Tracks include multimedia objects, both music tracks and video tracks. |
| 4842 | * Typical usage: |
Linus Walleij | a498273 | 2006-02-24 15:46:02 +0000 | [diff] [blame] | 4843 | * |
| 4844 | * <pre> |
| 4845 | * LIBMTP_track_t *tracklist; |
| 4846 | * |
Richard Low | 6711f44 | 2007-05-05 19:00:59 +0000 | [diff] [blame] | 4847 | * tracklist = LIBMTP_Get_Tracklisting_With_Callback(device, callback, data); |
Linus Walleij | a498273 | 2006-02-24 15:46:02 +0000 | [diff] [blame] | 4848 | * while (tracklist != NULL) { |
| 4849 | * LIBMTP_track_t *tmp; |
| 4850 | * |
| 4851 | * // Do something on each element in the list here... |
| 4852 | * tmp = tracklist; |
| 4853 | * tracklist = tracklist->next; |
| 4854 | * LIBMTP_destroy_track_t(tmp); |
| 4855 | * } |
| 4856 | * </pre> |
| 4857 | * |
Linus Walleij | 25d5c21 | 2008-08-14 06:49:13 +0000 | [diff] [blame] | 4858 | * If you want to group your track listing by storage (per storage unit) or |
| 4859 | * arrange tracks into folders, you must dereference the <code>storage_id</code> |
| 4860 | * and/or <code>parent_id</code> field of the returned <code>LIBMTP_track_t</code> |
| 4861 | * struct. To arrange by folders or files you typically have to create the proper |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 4862 | * trees by calls to <code>LIBMTP_Get_Storage()</code> and/or |
Linus Walleij | 25d5c21 | 2008-08-14 06:49:13 +0000 | [diff] [blame] | 4863 | * <code>LIBMTP_Get_Folder_List()</code> first. |
| 4864 | * |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 4865 | * @param device a pointer to the device to get the track listing for. |
Linus Walleij | 3fcfea5 | 2006-11-13 07:07:36 +0000 | [diff] [blame] | 4866 | * @param callback a function to be called during the tracklisting retrieveal |
Linus Walleij | 25d5c21 | 2008-08-14 06:49:13 +0000 | [diff] [blame] | 4867 | * for displaying progress bars etc, or NULL if you don't want |
| 4868 | * any callbacks. |
Richard Low | dc0b6c7 | 2006-11-13 09:22:23 +0000 | [diff] [blame] | 4869 | * @param data a user-defined pointer that is passed along to |
Linus Walleij | 25d5c21 | 2008-08-14 06:49:13 +0000 | [diff] [blame] | 4870 | * the <code>progress</code> function in order to |
| 4871 | * pass along some user defined data to the progress |
| 4872 | * updates. If not used, set this to NULL. |
Linus Walleij | a498273 | 2006-02-24 15:46:02 +0000 | [diff] [blame] | 4873 | * @return a list of tracks that can be followed using the <code>next</code> |
Linus Walleij | 25d5c21 | 2008-08-14 06:49:13 +0000 | [diff] [blame] | 4874 | * field of the <code>LIBMTP_track_t</code> data structure. |
| 4875 | * Each of the metadata tags must be freed after use, and may |
| 4876 | * contain only partial metadata information, i.e. one or several |
| 4877 | * fields may be NULL or 0. |
Linus Walleij | 2e4b5f9 | 2006-06-16 14:00:49 +0000 | [diff] [blame] | 4878 | * @see LIBMTP_Get_Trackmetadata() |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 4879 | */ |
Richard Low | dc0b6c7 | 2006-11-13 09:22:23 +0000 | [diff] [blame] | 4880 | LIBMTP_track_t *LIBMTP_Get_Tracklisting_With_Callback(LIBMTP_mtpdevice_t *device, |
| 4881 | LIBMTP_progressfunc_t const callback, |
| 4882 | void const * const data) |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 4883 | { |
nicklas79 | 18b54d7 | 2009-11-07 19:54:07 +0000 | [diff] [blame] | 4884 | return LIBMTP_Get_Tracklisting_With_Callback_For_Storage(device, 0, callback, data); |
| 4885 | } |
| 4886 | |
| 4887 | |
| 4888 | /** |
| 4889 | * This returns a long list of all tracks available on the current MTP device. |
| 4890 | * Tracks include multimedia objects, both music tracks and video tracks. |
| 4891 | * Typical usage: |
| 4892 | * |
| 4893 | * <pre> |
| 4894 | * LIBMTP_track_t *tracklist; |
| 4895 | * |
| 4896 | * tracklist = LIBMTP_Get_Tracklisting_With_Callback_For_Storage(device, storage_id, callback, data); |
| 4897 | * while (tracklist != NULL) { |
| 4898 | * LIBMTP_track_t *tmp; |
| 4899 | * |
| 4900 | * // Do something on each element in the list here... |
| 4901 | * tmp = tracklist; |
| 4902 | * tracklist = tracklist->next; |
| 4903 | * LIBMTP_destroy_track_t(tmp); |
| 4904 | * } |
| 4905 | * </pre> |
| 4906 | * |
| 4907 | * If you want to group your track listing by storage (per storage unit) or |
| 4908 | * arrange tracks into folders, you must dereference the <code>storage_id</code> |
| 4909 | * and/or <code>parent_id</code> field of the returned <code>LIBMTP_track_t</code> |
| 4910 | * struct. To arrange by folders or files you typically have to create the proper |
| 4911 | * trees by calls to <code>LIBMTP_Get_Storage()</code> and/or |
| 4912 | * <code>LIBMTP_Get_Folder_List()</code> first. |
| 4913 | * |
| 4914 | * @param device a pointer to the device to get the track listing for. |
| 4915 | * @param storage_id ID of device storage (if null, no filter) |
| 4916 | * @param callback a function to be called during the tracklisting retrieveal |
| 4917 | * for displaying progress bars etc, or NULL if you don't want |
| 4918 | * any callbacks. |
| 4919 | * @param data a user-defined pointer that is passed along to |
| 4920 | * the <code>progress</code> function in order to |
| 4921 | * pass along some user defined data to the progress |
| 4922 | * updates. If not used, set this to NULL. |
| 4923 | * @return a list of tracks that can be followed using the <code>next</code> |
| 4924 | * field of the <code>LIBMTP_track_t</code> data structure. |
| 4925 | * Each of the metadata tags must be freed after use, and may |
| 4926 | * contain only partial metadata information, i.e. one or several |
| 4927 | * fields may be NULL or 0. |
| 4928 | * @see LIBMTP_Get_Trackmetadata() |
| 4929 | */ |
| 4930 | LIBMTP_track_t *LIBMTP_Get_Tracklisting_With_Callback_For_Storage(LIBMTP_mtpdevice_t *device, uint32_t const storage_id, |
| 4931 | LIBMTP_progressfunc_t const callback, |
| 4932 | void const * const data) |
| 4933 | { |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 4934 | uint32_t i = 0; |
| 4935 | LIBMTP_track_t *retracks = NULL; |
| 4936 | LIBMTP_track_t *curtrack = NULL; |
Linus Walleij | 9b28da3 | 2006-03-16 13:47:58 +0000 | [diff] [blame] | 4937 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | 2be40c7 | 2007-03-16 15:19:44 +0000 | [diff] [blame] | 4938 | PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo; |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 4939 | |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 4940 | // Get all the handles if we haven't already done that |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 4941 | if (params->nrofobjects == 0) { |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 4942 | flush_handles(device); |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 4943 | } |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 4944 | |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 4945 | for (i = 0; i < params->nrofobjects; i++) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 4946 | LIBMTP_track_t *track; |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 4947 | PTPObject *ob; |
Linus Walleij | 46651f3 | 2008-04-26 23:30:19 +0000 | [diff] [blame] | 4948 | LIBMTP_filetype_t mtptype; |
mopoke | 3136444 | 2006-11-20 04:53:04 +0000 | [diff] [blame] | 4949 | |
Richard Low | dc0b6c7 | 2006-11-13 09:22:23 +0000 | [diff] [blame] | 4950 | if (callback != NULL) |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 4951 | callback(i, params->nrofobjects, data); |
mopoke | 3136444 | 2006-11-20 04:53:04 +0000 | [diff] [blame] | 4952 | |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 4953 | ob = ¶ms->objects[i]; |
| 4954 | mtptype = map_ptp_type_to_libmtp_type(ob->oi.ObjectFormat); |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 4955 | |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 4956 | // Ignore stuff we don't know how to handle... |
| 4957 | // TODO: get this list as an intersection of the sets |
| 4958 | // supported by the device and the from the device and |
Linus Walleij | d5356e2 | 2008-11-01 22:08:59 +0000 | [diff] [blame] | 4959 | // all known track files? |
| 4960 | if (!LIBMTP_FILETYPE_IS_TRACK(mtptype) && |
Linus Walleij | 46651f3 | 2008-04-26 23:30:19 +0000 | [diff] [blame] | 4961 | // This row lets through undefined files for examination since they may be forgotten OGG files. |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 4962 | (ob->oi.ObjectFormat != PTP_OFC_Undefined || |
Richard Low | ef19731 | 2008-11-01 18:29:41 +0000 | [diff] [blame] | 4963 | (!FLAG_IRIVER_OGG_ALZHEIMER(ptp_usb) && |
Linus Walleij | 89bb1cd | 2009-07-24 21:03:36 +0000 | [diff] [blame] | 4964 | !FLAG_OGG_IS_UNKNOWN(ptp_usb) && |
| 4965 | !FLAG_FLAC_IS_UNKNOWN(ptp_usb))) |
Linus Walleij | 46651f3 | 2008-04-26 23:30:19 +0000 | [diff] [blame] | 4966 | ) { |
Linus Walleij | c8abc92 | 2008-09-28 18:38:42 +0000 | [diff] [blame] | 4967 | //printf("Not a music track (name: %s format: %d), skipping...\n", oi->Filename, oi->ObjectFormat); |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 4968 | continue; |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 4969 | } |
| 4970 | |
nicklas79 | 18b54d7 | 2009-11-07 19:54:07 +0000 | [diff] [blame] | 4971 | // Ignore stuff that isn't into the storage device |
| 4972 | if ((storage_id != 0) && (ob->oi.StorageID != storage_id )) |
| 4973 | continue; |
| 4974 | |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 4975 | // Allocate a new track type |
| 4976 | track = LIBMTP_new_track_t(); |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 4977 | |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 4978 | // This is some sort of unique ID so we can keep track of the track. |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 4979 | track->item_id = ob->oid; |
| 4980 | track->parent_id = ob->oi.ParentObject; |
| 4981 | track->storage_id = ob->oi.StorageID; |
| 4982 | track->modificationdate = ob->oi.ModificationDate; |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 4983 | |
Linus Walleij | 46651f3 | 2008-04-26 23:30:19 +0000 | [diff] [blame] | 4984 | track->filetype = mtptype; |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 4985 | |
| 4986 | // Original file-specific properties |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 4987 | track->filesize = ob->oi.ObjectCompressedSize; |
| 4988 | if (ob->oi.Filename != NULL) { |
| 4989 | track->filename = strdup(ob->oi.Filename); |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 4990 | } |
| 4991 | |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 4992 | get_track_metadata(device, ob->oi.ObjectFormat, track); |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 4993 | |
| 4994 | /* |
| 4995 | * A special quirk for iriver devices that doesn't quite |
| 4996 | * remember that some files marked as "unknown" type are |
Linus Walleij | 89bb1cd | 2009-07-24 21:03:36 +0000 | [diff] [blame] | 4997 | * actually OGG or FLAC files. We look at the filename extension |
| 4998 | * and see if it happens that this was atleast named "ogg" or "flac" |
| 4999 | * and fall back on this heuristic approach in that case, |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 5000 | * for these bugged devices only. |
| 5001 | */ |
| 5002 | if (track->filetype == LIBMTP_FILETYPE_UNKNOWN && |
Linus Walleij | 89bb1cd | 2009-07-24 21:03:36 +0000 | [diff] [blame] | 5003 | track->filename != NULL) { |
| 5004 | if ((FLAG_IRIVER_OGG_ALZHEIMER(ptp_usb) || |
| 5005 | FLAG_OGG_IS_UNKNOWN(ptp_usb)) && |
| 5006 | has_ogg_extension(track->filename)) |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 5007 | track->filetype = LIBMTP_FILETYPE_OGG; |
Linus Walleij | 89bb1cd | 2009-07-24 21:03:36 +0000 | [diff] [blame] | 5008 | else if (FLAG_FLAC_IS_UNKNOWN(ptp_usb) && |
| 5009 | has_flac_extension(track->filename)) |
| 5010 | track->filetype = LIBMTP_FILETYPE_FLAC; |
| 5011 | else { |
| 5012 | // This was not an OGG/FLAC file so discard it and continue |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 5013 | LIBMTP_destroy_track_t(track); |
| 5014 | continue; |
| 5015 | } |
| 5016 | } |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 5017 | |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 5018 | // Add track to a list that will be returned afterwards. |
| 5019 | if (retracks == NULL) { |
| 5020 | retracks = track; |
| 5021 | curtrack = track; |
| 5022 | } else { |
| 5023 | curtrack->next = track; |
| 5024 | curtrack = track; |
| 5025 | } |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 5026 | |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 5027 | // Call listing callback |
| 5028 | // double progressPercent = (double)i*(double)100.0 / (double)params->handles.n; |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 5029 | |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 5030 | } // Handle counting loop |
| 5031 | return retracks; |
| 5032 | } |
Linus Walleij | dcde608 | 2006-02-17 16:16:34 +0000 | [diff] [blame] | 5033 | |
Linus Walleij | 2e4b5f9 | 2006-06-16 14:00:49 +0000 | [diff] [blame] | 5034 | /** |
| 5035 | * This function retrieves the metadata for a single track off |
| 5036 | * the device. |
| 5037 | * |
| 5038 | * Do not call this function repeatedly! The track handles are linearly |
| 5039 | * searched O(n) and the call may involve (slow) USB traffic, so use |
| 5040 | * <code>LIBMTP_Get_Tracklisting()</code> and cache the tracks, preferably |
| 5041 | * as an efficient data structure such as a hash list. |
| 5042 | * |
| 5043 | * @param device a pointer to the device to get the track metadata from. |
| 5044 | * @param trackid the object ID of the track that you want the metadata for. |
| 5045 | * @return a track metadata entry on success or NULL on failure. |
| 5046 | * @see LIBMTP_Get_Tracklisting() |
| 5047 | */ |
| 5048 | LIBMTP_track_t *LIBMTP_Get_Trackmetadata(LIBMTP_mtpdevice_t *device, uint32_t const trackid) |
| 5049 | { |
Linus Walleij | 2e4b5f9 | 2006-06-16 14:00:49 +0000 | [diff] [blame] | 5050 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | c8abc92 | 2008-09-28 18:38:42 +0000 | [diff] [blame] | 5051 | PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo; |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 5052 | PTPObject *ob; |
| 5053 | LIBMTP_track_t *track; |
| 5054 | LIBMTP_filetype_t mtptype; |
| 5055 | uint16_t ret; |
Linus Walleij | 2e4b5f9 | 2006-06-16 14:00:49 +0000 | [diff] [blame] | 5056 | |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 5057 | // Get all the handles if we haven't already done that |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 5058 | if (params->nrofobjects == 0) |
Linus Walleij | 2e4b5f9 | 2006-06-16 14:00:49 +0000 | [diff] [blame] | 5059 | flush_handles(device); |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 5060 | |
| 5061 | ret = ptp_object_want (params, trackid, PTPOBJECT_OBJECTINFO_LOADED, &ob); |
| 5062 | if (ret != PTP_RC_OK) |
| 5063 | return NULL; |
| 5064 | |
| 5065 | mtptype = map_ptp_type_to_libmtp_type(ob->oi.ObjectFormat); |
| 5066 | |
| 5067 | // Ignore stuff we don't know how to handle... |
| 5068 | if (!LIBMTP_FILETYPE_IS_TRACK(mtptype) && |
Linus Walleij | 89bb1cd | 2009-07-24 21:03:36 +0000 | [diff] [blame] | 5069 | /* |
| 5070 | * This row lets through undefined files for examination |
| 5071 | * since they may be forgotten OGG or FLAC files. |
| 5072 | */ |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 5073 | (ob->oi.ObjectFormat != PTP_OFC_Undefined || |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 5074 | (!FLAG_IRIVER_OGG_ALZHEIMER(ptp_usb) && |
Linus Walleij | 89bb1cd | 2009-07-24 21:03:36 +0000 | [diff] [blame] | 5075 | !FLAG_OGG_IS_UNKNOWN(ptp_usb) && |
| 5076 | !FLAG_FLAC_IS_UNKNOWN(ptp_usb))) |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 5077 | ) { |
| 5078 | //printf("Not a music track (name: %s format: %d), skipping...\n", oi->Filename, oi->ObjectFormat); |
| 5079 | return NULL; |
Linus Walleij | 2e4b5f9 | 2006-06-16 14:00:49 +0000 | [diff] [blame] | 5080 | } |
| 5081 | |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 5082 | // Allocate a new track type |
| 5083 | track = LIBMTP_new_track_t(); |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 5084 | |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 5085 | // This is some sort of unique ID so we can keep track of the track. |
| 5086 | track->item_id = ob->oid; |
| 5087 | track->parent_id = ob->oi.ParentObject; |
| 5088 | track->storage_id = ob->oi.StorageID; |
| 5089 | track->modificationdate = ob->oi.ModificationDate; |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 5090 | |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 5091 | track->filetype = mtptype; |
Linus Walleij | 2e4b5f9 | 2006-06-16 14:00:49 +0000 | [diff] [blame] | 5092 | |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 5093 | // Original file-specific properties |
| 5094 | track->filesize = ob->oi.ObjectCompressedSize; |
| 5095 | if (ob->oi.Filename != NULL) { |
| 5096 | track->filename = strdup(ob->oi.Filename); |
| 5097 | } |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 5098 | |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 5099 | /* |
Linus Walleij | 89bb1cd | 2009-07-24 21:03:36 +0000 | [diff] [blame] | 5100 | * A special quirk for devices that doesn't quite |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 5101 | * remember that some files marked as "unknown" type are |
Linus Walleij | 89bb1cd | 2009-07-24 21:03:36 +0000 | [diff] [blame] | 5102 | * actually OGG or FLAC files. We look at the filename extension |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 5103 | * and see if it happens that this was atleast named "ogg" |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 5104 | * and fall back on this heuristic approach in that case, |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 5105 | * for these bugged devices only. |
| 5106 | */ |
| 5107 | if (track->filetype == LIBMTP_FILETYPE_UNKNOWN && |
Linus Walleij | 89bb1cd | 2009-07-24 21:03:36 +0000 | [diff] [blame] | 5108 | track->filename != NULL) { |
| 5109 | if ((FLAG_IRIVER_OGG_ALZHEIMER(ptp_usb) || |
| 5110 | FLAG_OGG_IS_UNKNOWN(ptp_usb)) && |
| 5111 | has_ogg_extension(track->filename)) |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 5112 | track->filetype = LIBMTP_FILETYPE_OGG; |
Linus Walleij | 89bb1cd | 2009-07-24 21:03:36 +0000 | [diff] [blame] | 5113 | else if (FLAG_FLAC_IS_UNKNOWN(ptp_usb) && |
| 5114 | has_flac_extension(track->filename)) |
| 5115 | track->filetype = LIBMTP_FILETYPE_FLAC; |
| 5116 | else { |
| 5117 | // This was not an OGG/FLAC file so discard it |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 5118 | LIBMTP_destroy_track_t(track); |
Linus Walleij | 2e4b5f9 | 2006-06-16 14:00:49 +0000 | [diff] [blame] | 5119 | return NULL; |
| 5120 | } |
Linus Walleij | 2e4b5f9 | 2006-06-16 14:00:49 +0000 | [diff] [blame] | 5121 | } |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 5122 | get_track_metadata(device, ob->oi.ObjectFormat, track); |
| 5123 | return track; |
Linus Walleij | 2e4b5f9 | 2006-06-16 14:00:49 +0000 | [diff] [blame] | 5124 | } |
| 5125 | |
Richard Low | 5b4023c | 2009-04-16 19:14:38 +0000 | [diff] [blame] | 5126 | /** |
| 5127 | * This is a manual conversion from MTPDataGetFunc to PTPDataGetFunc |
| 5128 | * to isolate the internal type. |
| 5129 | */ |
Linus Walleij | d866d24 | 2009-08-23 21:50:39 +0000 | [diff] [blame] | 5130 | static uint16_t get_func_wrapper(PTPParams* params, void* priv, unsigned long wantlen, unsigned char *data, unsigned long *gotlen) |
Richard Low | 5b4023c | 2009-04-16 19:14:38 +0000 | [diff] [blame] | 5131 | { |
Linus Walleij | d866d24 | 2009-08-23 21:50:39 +0000 | [diff] [blame] | 5132 | MTPDataHandler *handler = (MTPDataHandler *)priv; |
Richard Low | 5b4023c | 2009-04-16 19:14:38 +0000 | [diff] [blame] | 5133 | uint16_t ret; |
| 5134 | uint32_t local_gotlen = 0; |
Linus Walleij | d866d24 | 2009-08-23 21:50:39 +0000 | [diff] [blame] | 5135 | ret = handler->getfunc(params, handler->priv, wantlen, data, &local_gotlen); |
Richard Low | 5b4023c | 2009-04-16 19:14:38 +0000 | [diff] [blame] | 5136 | *gotlen = local_gotlen; |
Richard Low | 6f07084 | 2009-05-03 10:26:16 +0000 | [diff] [blame] | 5137 | switch (ret) |
| 5138 | { |
| 5139 | case LIBMTP_HANDLER_RETURN_OK: |
| 5140 | return PTP_RC_OK; |
| 5141 | case LIBMTP_HANDLER_RETURN_ERROR: |
| 5142 | return PTP_ERROR_IO; |
| 5143 | case LIBMTP_HANDLER_RETURN_CANCEL: |
| 5144 | return PTP_ERROR_CANCEL; |
| 5145 | default: |
| 5146 | return PTP_ERROR_IO; |
| 5147 | } |
Richard Low | 5b4023c | 2009-04-16 19:14:38 +0000 | [diff] [blame] | 5148 | } |
| 5149 | |
| 5150 | /** |
| 5151 | * This is a manual conversion from MTPDataPutFunc to PTPDataPutFunc |
| 5152 | * to isolate the internal type. |
| 5153 | */ |
Marcus Meissner | 3d692dc | 2016-02-21 12:34:06 +0100 | [diff] [blame] | 5154 | static uint16_t put_func_wrapper(PTPParams* params, void* priv, unsigned long sendlen, unsigned char *data) |
Richard Low | 5b4023c | 2009-04-16 19:14:38 +0000 | [diff] [blame] | 5155 | { |
Linus Walleij | d866d24 | 2009-08-23 21:50:39 +0000 | [diff] [blame] | 5156 | MTPDataHandler *handler = (MTPDataHandler *)priv; |
Richard Low | 5b4023c | 2009-04-16 19:14:38 +0000 | [diff] [blame] | 5157 | uint16_t ret; |
| 5158 | uint32_t local_putlen = 0; |
Marcus Meissner | 3d692dc | 2016-02-21 12:34:06 +0100 | [diff] [blame] | 5159 | |
Linus Walleij | d866d24 | 2009-08-23 21:50:39 +0000 | [diff] [blame] | 5160 | ret = handler->putfunc(params, handler->priv, sendlen, data, &local_putlen); |
Marcus Meissner | 3d692dc | 2016-02-21 12:34:06 +0100 | [diff] [blame] | 5161 | |
Richard Low | 75981ac | 2009-05-03 12:54:06 +0000 | [diff] [blame] | 5162 | switch (ret) |
| 5163 | { |
| 5164 | case LIBMTP_HANDLER_RETURN_OK: |
Marcus Meissner | 3d692dc | 2016-02-21 12:34:06 +0100 | [diff] [blame] | 5165 | if (local_putlen != sendlen) |
| 5166 | return PTP_ERROR_IO; |
Richard Low | 75981ac | 2009-05-03 12:54:06 +0000 | [diff] [blame] | 5167 | return PTP_RC_OK; |
| 5168 | case LIBMTP_HANDLER_RETURN_ERROR: |
| 5169 | return PTP_ERROR_IO; |
| 5170 | case LIBMTP_HANDLER_RETURN_CANCEL: |
| 5171 | return PTP_ERROR_CANCEL; |
| 5172 | default: |
| 5173 | return PTP_ERROR_IO; |
| 5174 | } |
Richard Low | 5b4023c | 2009-04-16 19:14:38 +0000 | [diff] [blame] | 5175 | } |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 5176 | |
| 5177 | /** |
| 5178 | * This gets a file off the device to a local file identified |
| 5179 | * by a filename. |
| 5180 | * @param device a pointer to the device to get the track from. |
| 5181 | * @param id the file ID of the file to retrieve. |
| 5182 | * @param path a filename to use for the retrieved file. |
| 5183 | * @param callback a progress indicator function or NULL to ignore. |
| 5184 | * @param data a user-defined pointer that is passed along to |
| 5185 | * the <code>progress</code> function in order to |
| 5186 | * pass along some user defined data to the progress |
| 5187 | * updates. If not used, set this to NULL. |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 5188 | * @return 0 if the transfer was successful, any other value means |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 5189 | * failure. |
| 5190 | * @see LIBMTP_Get_File_To_File_Descriptor() |
| 5191 | */ |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 5192 | int LIBMTP_Get_File_To_File(LIBMTP_mtpdevice_t *device, uint32_t const id, |
Linus Walleij | ee73ef2 | 2006-08-27 19:56:00 +0000 | [diff] [blame] | 5193 | char const * const path, LIBMTP_progressfunc_t const callback, |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 5194 | void const * const data) |
| 5195 | { |
| 5196 | int fd = -1; |
| 5197 | int ret; |
| 5198 | |
| 5199 | // Sanity check |
| 5200 | if (path == NULL) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 5201 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Get_File_To_File(): Bad arguments, path was NULL."); |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 5202 | return -1; |
| 5203 | } |
| 5204 | |
| 5205 | // Open file |
| 5206 | #ifdef __WIN32__ |
Linus Walleij | 7beba57 | 2006-11-29 08:56:12 +0000 | [diff] [blame] | 5207 | #ifdef USE_WINDOWS_IO_H |
| 5208 | if ( (fd = _open(path, O_RDWR|O_CREAT|O_TRUNC|O_BINARY,_S_IREAD)) == -1 ) { |
| 5209 | #else |
Linus Walleij | 5b4a4d2 | 2009-01-10 12:18:14 +0000 | [diff] [blame] | 5210 | if ( (fd = open(path, O_RDWR|O_CREAT|O_TRUNC|O_BINARY,S_IRWXU)) == -1 ) { |
Linus Walleij | 7beba57 | 2006-11-29 08:56:12 +0000 | [diff] [blame] | 5211 | #endif |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 5212 | #else |
| 5213 | if ( (fd = open(path, O_RDWR|O_CREAT|O_TRUNC,S_IRWXU|S_IRGRP)) == -1) { |
| 5214 | #endif |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 5215 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Get_File_To_File(): Could not create file."); |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 5216 | return -1; |
| 5217 | } |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 5218 | |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 5219 | ret = LIBMTP_Get_File_To_File_Descriptor(device, id, fd, callback, data); |
| 5220 | |
| 5221 | // Close file |
| 5222 | close(fd); |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 5223 | |
Linus Walleij | 25d33b5 | 2007-09-16 21:36:19 +0000 | [diff] [blame] | 5224 | // Delete partial file. |
| 5225 | if (ret == -1) { |
| 5226 | unlink(path); |
| 5227 | } |
| 5228 | |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 5229 | return ret; |
| 5230 | } |
| 5231 | |
| 5232 | /** |
| 5233 | * This gets a file off the device to a file identified |
| 5234 | * by a file descriptor. |
Linus Walleij | 0558ac5 | 2006-09-07 06:55:03 +0000 | [diff] [blame] | 5235 | * |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 5236 | * This function can potentially be used for streaming |
| 5237 | * files off the device for playback or broadcast for example, |
Linus Walleij | 0558ac5 | 2006-09-07 06:55:03 +0000 | [diff] [blame] | 5238 | * by downloading the file into a stream sink e.g. a socket. |
| 5239 | * |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 5240 | * @param device a pointer to the device to get the file from. |
| 5241 | * @param id the file ID of the file to retrieve. |
| 5242 | * @param fd a local file descriptor to write the file to. |
| 5243 | * @param callback a progress indicator function or NULL to ignore. |
| 5244 | * @param data a user-defined pointer that is passed along to |
| 5245 | * the <code>progress</code> function in order to |
| 5246 | * pass along some user defined data to the progress |
| 5247 | * updates. If not used, set this to NULL. |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 5248 | * @return 0 if the transfer was successful, any other value means |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 5249 | * failure. |
| 5250 | * @see LIBMTP_Get_File_To_File() |
| 5251 | */ |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 5252 | int LIBMTP_Get_File_To_File_Descriptor(LIBMTP_mtpdevice_t *device, |
| 5253 | uint32_t const id, |
| 5254 | int const fd, |
Linus Walleij | ee73ef2 | 2006-08-27 19:56:00 +0000 | [diff] [blame] | 5255 | LIBMTP_progressfunc_t const callback, |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 5256 | void const * const data) |
| 5257 | { |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 5258 | uint16_t ret; |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 5259 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | ee73ef2 | 2006-08-27 19:56:00 +0000 | [diff] [blame] | 5260 | PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo; |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 5261 | |
Philip Langdale | b2bcdb9 | 2018-03-12 21:32:20 +0100 | [diff] [blame] | 5262 | LIBMTP_file_t *mtpfile = LIBMTP_Get_Filemetadata(device, id); |
| 5263 | if (mtpfile == NULL) { |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 5264 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Get_File_To_File_Descriptor(): Could not get object info."); |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 5265 | return -1; |
| 5266 | } |
Philip Langdale | b2bcdb9 | 2018-03-12 21:32:20 +0100 | [diff] [blame] | 5267 | if (mtpfile->filetype == LIBMTP_FILETYPE_FOLDER) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 5268 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Get_File_To_File_Descriptor(): Bad object format."); |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 5269 | return -1; |
| 5270 | } |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 5271 | |
Linus Walleij | ee73ef2 | 2006-08-27 19:56:00 +0000 | [diff] [blame] | 5272 | // Callbacks |
| 5273 | ptp_usb->callback_active = 1; |
Philip Langdale | b2bcdb9 | 2018-03-12 21:32:20 +0100 | [diff] [blame] | 5274 | ptp_usb->current_transfer_total = mtpfile->filesize + |
Linus Walleij | 7f0c72e | 2006-09-06 13:01:58 +0000 | [diff] [blame] | 5275 | PTP_USB_BULK_HDR_LEN+sizeof(uint32_t); // Request length, one parameter |
Linus Walleij | ee73ef2 | 2006-08-27 19:56:00 +0000 | [diff] [blame] | 5276 | ptp_usb->current_transfer_complete = 0; |
| 5277 | ptp_usb->current_transfer_callback = callback; |
| 5278 | ptp_usb->current_transfer_callback_data = data; |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 5279 | |
Philip Langdale | b2bcdb9 | 2018-03-12 21:32:20 +0100 | [diff] [blame] | 5280 | // Don't need mtpfile anymore |
| 5281 | LIBMTP_destroy_file_t(mtpfile); |
| 5282 | |
Linus Walleij | 96c6243 | 2006-08-21 10:04:02 +0000 | [diff] [blame] | 5283 | ret = ptp_getobject_tofd(params, id, fd); |
Linus Walleij | ee73ef2 | 2006-08-27 19:56:00 +0000 | [diff] [blame] | 5284 | |
| 5285 | ptp_usb->callback_active = 0; |
| 5286 | ptp_usb->current_transfer_callback = NULL; |
| 5287 | ptp_usb->current_transfer_callback_data = NULL; |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 5288 | |
Linus Walleij | bd3bf9e | 2007-09-14 19:31:54 +0000 | [diff] [blame] | 5289 | if (ret == PTP_ERROR_CANCEL) { |
Linus Walleij | fb28b63 | 2007-10-23 21:56:18 +0000 | [diff] [blame] | 5290 | add_error_to_errorstack(device, LIBMTP_ERROR_CANCELLED, "LIBMTP_Get_File_From_File_Descriptor(): Cancelled transfer."); |
Linus Walleij | bd3bf9e | 2007-09-14 19:31:54 +0000 | [diff] [blame] | 5291 | return -1; |
| 5292 | } |
Linus Walleij | b02a066 | 2006-04-25 08:05:09 +0000 | [diff] [blame] | 5293 | if (ret != PTP_RC_OK) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 5294 | add_ptp_error_to_errorstack(device, ret, "LIBMTP_Get_File_To_File_Descriptor(): Could not get file from device."); |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 5295 | return -1; |
| 5296 | } |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 5297 | |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 5298 | return 0; |
| 5299 | } |
| 5300 | |
Linus Walleij | dcde608 | 2006-02-17 16:16:34 +0000 | [diff] [blame] | 5301 | /** |
Richard Low | d3b1702 | 2009-04-11 12:37:39 +0000 | [diff] [blame] | 5302 | * This gets a file off the device and calls put_func |
| 5303 | * with chunks of data |
| 5304 | * |
| 5305 | * @param device a pointer to the device to get the file from. |
| 5306 | * @param id the file ID of the file to retrieve. |
| 5307 | * @param put_func the function to call when we have data. |
| 5308 | * @param priv the user-defined pointer that is passed to |
| 5309 | * <code>put_func</code>. |
| 5310 | * @param callback a progress indicator function or NULL to ignore. |
| 5311 | * @param data a user-defined pointer that is passed along to |
| 5312 | * the <code>progress</code> function in order to |
| 5313 | * pass along some user defined data to the progress |
| 5314 | * updates. If not used, set this to NULL. |
| 5315 | * @return 0 if the transfer was successful, any other value means |
| 5316 | * failure. |
| 5317 | */ |
| 5318 | int LIBMTP_Get_File_To_Handler(LIBMTP_mtpdevice_t *device, |
| 5319 | uint32_t const id, |
| 5320 | MTPDataPutFunc put_func, |
| 5321 | void * priv, |
| 5322 | LIBMTP_progressfunc_t const callback, |
| 5323 | void const * const data) |
| 5324 | { |
Richard Low | d3b1702 | 2009-04-11 12:37:39 +0000 | [diff] [blame] | 5325 | uint16_t ret; |
| 5326 | PTPParams *params = (PTPParams *) device->params; |
| 5327 | PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo; |
| 5328 | |
Philip Langdale | b2bcdb9 | 2018-03-12 21:32:20 +0100 | [diff] [blame] | 5329 | LIBMTP_file_t *mtpfile = LIBMTP_Get_Filemetadata(device, id); |
| 5330 | if (mtpfile == NULL) { |
Richard Low | d3b1702 | 2009-04-11 12:37:39 +0000 | [diff] [blame] | 5331 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Get_File_To_File_Descriptor(): Could not get object info."); |
| 5332 | return -1; |
| 5333 | } |
Philip Langdale | b2bcdb9 | 2018-03-12 21:32:20 +0100 | [diff] [blame] | 5334 | if (mtpfile->filetype == LIBMTP_FILETYPE_FOLDER) { |
Richard Low | d3b1702 | 2009-04-11 12:37:39 +0000 | [diff] [blame] | 5335 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Get_File_To_File_Descriptor(): Bad object format."); |
| 5336 | return -1; |
| 5337 | } |
| 5338 | |
| 5339 | // Callbacks |
| 5340 | ptp_usb->callback_active = 1; |
Philip Langdale | b2bcdb9 | 2018-03-12 21:32:20 +0100 | [diff] [blame] | 5341 | ptp_usb->current_transfer_total = mtpfile->filesize + |
Richard Low | d3b1702 | 2009-04-11 12:37:39 +0000 | [diff] [blame] | 5342 | PTP_USB_BULK_HDR_LEN+sizeof(uint32_t); // Request length, one parameter |
| 5343 | ptp_usb->current_transfer_complete = 0; |
| 5344 | ptp_usb->current_transfer_callback = callback; |
| 5345 | ptp_usb->current_transfer_callback_data = data; |
| 5346 | |
Philip Langdale | b2bcdb9 | 2018-03-12 21:32:20 +0100 | [diff] [blame] | 5347 | // Don't need mtpfile anymore |
| 5348 | LIBMTP_destroy_file_t(mtpfile); |
| 5349 | |
Richard Low | 5b4023c | 2009-04-16 19:14:38 +0000 | [diff] [blame] | 5350 | MTPDataHandler mtp_handler; |
| 5351 | mtp_handler.getfunc = NULL; |
| 5352 | mtp_handler.putfunc = put_func; |
Linus Walleij | d866d24 | 2009-08-23 21:50:39 +0000 | [diff] [blame] | 5353 | mtp_handler.priv = priv; |
| 5354 | |
Richard Low | d3b1702 | 2009-04-11 12:37:39 +0000 | [diff] [blame] | 5355 | PTPDataHandler handler; |
| 5356 | handler.getfunc = NULL; |
Richard Low | 5b4023c | 2009-04-16 19:14:38 +0000 | [diff] [blame] | 5357 | handler.putfunc = put_func_wrapper; |
Linus Walleij | d866d24 | 2009-08-23 21:50:39 +0000 | [diff] [blame] | 5358 | handler.priv = &mtp_handler; |
| 5359 | |
Richard Low | d3b1702 | 2009-04-11 12:37:39 +0000 | [diff] [blame] | 5360 | ret = ptp_getobject_to_handler(params, id, &handler); |
| 5361 | |
| 5362 | ptp_usb->callback_active = 0; |
| 5363 | ptp_usb->current_transfer_callback = NULL; |
| 5364 | ptp_usb->current_transfer_callback_data = NULL; |
| 5365 | |
| 5366 | if (ret == PTP_ERROR_CANCEL) { |
| 5367 | add_error_to_errorstack(device, LIBMTP_ERROR_CANCELLED, "LIBMTP_Get_File_From_File_Descriptor(): Cancelled transfer."); |
| 5368 | return -1; |
| 5369 | } |
| 5370 | if (ret != PTP_RC_OK) { |
| 5371 | add_ptp_error_to_errorstack(device, ret, "LIBMTP_Get_File_To_File_Descriptor(): Could not get file from device."); |
| 5372 | return -1; |
| 5373 | } |
| 5374 | |
| 5375 | return 0; |
| 5376 | } |
| 5377 | |
| 5378 | |
| 5379 | /** |
Linus Walleij | dcde608 | 2006-02-17 16:16:34 +0000 | [diff] [blame] | 5380 | * This gets a track off the device to a file identified |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 5381 | * by a filename. This is actually just a wrapper for the |
| 5382 | * \c LIBMTP_Get_Track_To_File() function. |
Linus Walleij | dcde608 | 2006-02-17 16:16:34 +0000 | [diff] [blame] | 5383 | * @param device a pointer to the device to get the track from. |
| 5384 | * @param id the track ID of the track to retrieve. |
| 5385 | * @param path a filename to use for the retrieved track. |
| 5386 | * @param callback a progress indicator function or NULL to ignore. |
| 5387 | * @param data a user-defined pointer that is passed along to |
| 5388 | * the <code>progress</code> function in order to |
| 5389 | * pass along some user defined data to the progress |
| 5390 | * updates. If not used, set this to NULL. |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 5391 | * @return 0 if the transfer was successful, any other value means |
Linus Walleij | dcde608 | 2006-02-17 16:16:34 +0000 | [diff] [blame] | 5392 | * failure. |
| 5393 | * @see LIBMTP_Get_Track_To_File_Descriptor() |
| 5394 | */ |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 5395 | int LIBMTP_Get_Track_To_File(LIBMTP_mtpdevice_t *device, uint32_t const id, |
Linus Walleij | ee73ef2 | 2006-08-27 19:56:00 +0000 | [diff] [blame] | 5396 | char const * const path, LIBMTP_progressfunc_t const callback, |
Linus Walleij | 0cd8543 | 2006-02-20 14:37:50 +0000 | [diff] [blame] | 5397 | void const * const data) |
Linus Walleij | dcde608 | 2006-02-17 16:16:34 +0000 | [diff] [blame] | 5398 | { |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 5399 | // This is just a wrapper |
| 5400 | return LIBMTP_Get_File_To_File(device, id, path, callback, data); |
Linus Walleij | dcde608 | 2006-02-17 16:16:34 +0000 | [diff] [blame] | 5401 | } |
| 5402 | |
| 5403 | /** |
| 5404 | * This gets a track off the device to a file identified |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 5405 | * by a file descriptor. This is actually just a wrapper for |
| 5406 | * the \c LIBMTP_Get_File_To_File_Descriptor() function. |
Linus Walleij | dcde608 | 2006-02-17 16:16:34 +0000 | [diff] [blame] | 5407 | * @param device a pointer to the device to get the track from. |
| 5408 | * @param id the track ID of the track to retrieve. |
| 5409 | * @param fd a file descriptor to write the track to. |
| 5410 | * @param callback a progress indicator function or NULL to ignore. |
| 5411 | * @param data a user-defined pointer that is passed along to |
| 5412 | * the <code>progress</code> function in order to |
| 5413 | * pass along some user defined data to the progress |
| 5414 | * updates. If not used, set this to NULL. |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 5415 | * @return 0 if the transfer was successful, any other value means |
Linus Walleij | dcde608 | 2006-02-17 16:16:34 +0000 | [diff] [blame] | 5416 | * failure. |
| 5417 | * @see LIBMTP_Get_Track_To_File() |
| 5418 | */ |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 5419 | int LIBMTP_Get_Track_To_File_Descriptor(LIBMTP_mtpdevice_t *device, |
| 5420 | uint32_t const id, |
| 5421 | int const fd, |
Linus Walleij | ee73ef2 | 2006-08-27 19:56:00 +0000 | [diff] [blame] | 5422 | LIBMTP_progressfunc_t const callback, |
Linus Walleij | 0cd8543 | 2006-02-20 14:37:50 +0000 | [diff] [blame] | 5423 | void const * const data) |
Linus Walleij | dcde608 | 2006-02-17 16:16:34 +0000 | [diff] [blame] | 5424 | { |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 5425 | // This is just a wrapper |
| 5426 | return LIBMTP_Get_File_To_File_Descriptor(device, id, fd, callback, data); |
Linus Walleij | dcde608 | 2006-02-17 16:16:34 +0000 | [diff] [blame] | 5427 | } |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 5428 | |
| 5429 | /** |
Richard Low | d3b1702 | 2009-04-11 12:37:39 +0000 | [diff] [blame] | 5430 | * This gets a track off the device to a handler function. |
| 5431 | * This is actually just a wrapper for |
| 5432 | * the \c LIBMTP_Get_File_To_Handler() function. |
| 5433 | * @param device a pointer to the device to get the track from. |
| 5434 | * @param id the track ID of the track to retrieve. |
| 5435 | * @param put_func the function to call when we have data. |
| 5436 | * @param priv the user-defined pointer that is passed to |
| 5437 | * <code>put_func</code>. |
| 5438 | * @param callback a progress indicator function or NULL to ignore. |
| 5439 | * @param data a user-defined pointer that is passed along to |
| 5440 | * the <code>progress</code> function in order to |
| 5441 | * pass along some user defined data to the progress |
| 5442 | * updates. If not used, set this to NULL. |
| 5443 | * @return 0 if the transfer was successful, any other value means |
| 5444 | * failure. |
| 5445 | */ |
| 5446 | int LIBMTP_Get_Track_To_Handler(LIBMTP_mtpdevice_t *device, |
| 5447 | uint32_t const id, |
| 5448 | MTPDataPutFunc put_func, |
| 5449 | void * priv, |
| 5450 | LIBMTP_progressfunc_t const callback, |
| 5451 | void const * const data) |
| 5452 | { |
| 5453 | // This is just a wrapper |
| 5454 | return LIBMTP_Get_File_To_Handler(device, id, put_func, priv, callback, data); |
| 5455 | } |
| 5456 | |
| 5457 | /** |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 5458 | * This function sends a track from a local file to an |
| 5459 | * MTP device. A filename and a set of metadata must be |
| 5460 | * given as input. |
| 5461 | * @param device a pointer to the device to send the track to. |
| 5462 | * @param path the filename of a local file which will be sent. |
| 5463 | * @param metadata a track metadata set to be written along with the file. |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 5464 | * After this call the field <code>metadata->item_id</code> |
| 5465 | * will contain the new track ID. Other fields such |
| 5466 | * as the <code>metadata->filename</code>, <code>metadata->parent_id</code> |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 5467 | * or <code>metadata->storage_id</code> may also change during this |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 5468 | * operation due to device restrictions, so do not rely on the |
| 5469 | * contents of this struct to be preserved in any way. |
| 5470 | * <ul> |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 5471 | * <li><code>metadata->parent_id</code> should be set to the parent |
| 5472 | * (e.g. folder) to store this track in. Since some |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 5473 | * devices are a bit picky about where files |
| 5474 | * are placed, a default folder will be chosen if libmtp |
| 5475 | * has detected one for the current filetype and this |
| 5476 | * parameter is set to 0. If this is 0 and no default folder |
| 5477 | * can be found, the file will be stored in the root folder. |
| 5478 | * <li><code>metadata->storage_id</code> should be set to the |
| 5479 | * desired storage (e.g. memory card or whatever your device |
| 5480 | * presents) to store this track in. Setting this to 0 will store |
| 5481 | * the track on the primary storage. |
| 5482 | * </ul> |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 5483 | * @param callback a progress indicator function or NULL to ignore. |
| 5484 | * @param data a user-defined pointer that is passed along to |
| 5485 | * the <code>progress</code> function in order to |
| 5486 | * pass along some user defined data to the progress |
| 5487 | * updates. If not used, set this to NULL. |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 5488 | * @return 0 if the transfer was successful, any other value means |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 5489 | * failure. |
| 5490 | * @see LIBMTP_Send_Track_From_File_Descriptor() |
Linus Walleij | 1b87ea7 | 2007-08-28 07:53:09 +0000 | [diff] [blame] | 5491 | * @see LIBMTP_Send_File_From_File() |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 5492 | * @see LIBMTP_Delete_Object() |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 5493 | */ |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 5494 | int LIBMTP_Send_Track_From_File(LIBMTP_mtpdevice_t *device, |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 5495 | char const * const path, LIBMTP_track_t * const metadata, |
Linus Walleij | ee73ef2 | 2006-08-27 19:56:00 +0000 | [diff] [blame] | 5496 | LIBMTP_progressfunc_t const callback, |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 5497 | void const * const data) |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 5498 | { |
| 5499 | int fd; |
| 5500 | int ret; |
| 5501 | |
| 5502 | // Sanity check |
| 5503 | if (path == NULL) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 5504 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Send_Track_From_File(): Bad arguments, path was NULL."); |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 5505 | return -1; |
| 5506 | } |
| 5507 | |
| 5508 | // Open file |
| 5509 | #ifdef __WIN32__ |
Linus Walleij | 7beba57 | 2006-11-29 08:56:12 +0000 | [diff] [blame] | 5510 | #ifdef USE_WINDOWS_IO_H |
Linus Walleij | 3017c56 | 2010-10-17 21:31:36 +0000 | [diff] [blame] | 5511 | if ( (fd = _open(path, O_RDONLY|O_BINARY)) == -1 ) { |
Linus Walleij | 7beba57 | 2006-11-29 08:56:12 +0000 | [diff] [blame] | 5512 | #else |
Linus Walleij | 3017c56 | 2010-10-17 21:31:36 +0000 | [diff] [blame] | 5513 | if ( (fd = open(path, O_RDONLY|O_BINARY)) == -1 ) { |
Linus Walleij | 7beba57 | 2006-11-29 08:56:12 +0000 | [diff] [blame] | 5514 | #endif |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 5515 | #else |
| 5516 | if ( (fd = open(path, O_RDONLY)) == -1) { |
| 5517 | #endif |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 5518 | LIBMTP_ERROR("LIBMTP_Send_Track_From_File(): Could not open source file \"%s\"\n", path); |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 5519 | return -1; |
| 5520 | } |
| 5521 | |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 5522 | ret = LIBMTP_Send_Track_From_File_Descriptor(device, fd, metadata, callback, data); |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 5523 | |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 5524 | // Close file. |
Linus Walleij | 7beba57 | 2006-11-29 08:56:12 +0000 | [diff] [blame] | 5525 | #ifdef USE_WINDOWS_IO_H |
| 5526 | _close(fd); |
| 5527 | #else |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 5528 | close(fd); |
Linus Walleij | 7beba57 | 2006-11-29 08:56:12 +0000 | [diff] [blame] | 5529 | #endif |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 5530 | |
| 5531 | return ret; |
| 5532 | } |
| 5533 | |
Linus Walleij | 094b450 | 2009-09-22 22:28:33 +0000 | [diff] [blame] | 5534 | |
| 5535 | |
| 5536 | /** |
| 5537 | * This helper function checks if a filename already exists on the device |
| 5538 | * @param PTPParams* |
| 5539 | * @param string representing the filename |
| 5540 | * @return 0 if the filename doesn't exist, -1 if it does |
| 5541 | */ |
| 5542 | static int check_filename_exists(PTPParams* params, char const * const filename) |
| 5543 | { |
| 5544 | int i; |
| 5545 | |
| 5546 | for (i = 0; i < params->nrofobjects; i++) { |
| 5547 | char *fname = params->objects[i].oi.Filename; |
| 5548 | if ((fname != NULL) && (strcmp(filename, fname) == 0)) |
| 5549 | { |
| 5550 | return -1; |
| 5551 | } |
| 5552 | } |
| 5553 | |
| 5554 | return 0; |
| 5555 | } |
| 5556 | |
| 5557 | /** |
| 5558 | * This helper function returns a unique filename, with a random string before the extension |
| 5559 | * @param string representing the original filename |
| 5560 | * @return a string representing the unique filename |
| 5561 | */ |
| 5562 | static char *generate_unique_filename(PTPParams* params, char const * const filename) |
| 5563 | { |
| 5564 | int suffix; |
| 5565 | char * extension_position; |
| 5566 | |
| 5567 | if (check_filename_exists(params, filename)) |
| 5568 | { |
| 5569 | extension_position = strrchr(filename,'.'); |
| 5570 | |
| 5571 | char basename[extension_position - filename + 1]; |
| 5572 | strncpy(basename, filename, extension_position - filename); |
| 5573 | basename[extension_position - filename] = '\0'; |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 5574 | |
Linus Walleij | 094b450 | 2009-09-22 22:28:33 +0000 | [diff] [blame] | 5575 | suffix = 1; |
| 5576 | char newname[ strlen(basename) + 6 + strlen(extension_position)]; |
| 5577 | sprintf(newname, "%s_%d%s", basename, suffix, extension_position); |
| 5578 | while ((check_filename_exists(params, newname)) && (suffix < 1000000)) { |
| 5579 | suffix++; |
| 5580 | sprintf(newname, "%s_%d%s", basename, suffix, extension_position); |
| 5581 | } |
| 5582 | return strdup(newname); |
| 5583 | } |
| 5584 | else |
| 5585 | { |
| 5586 | return strdup(filename); |
| 5587 | } |
| 5588 | } |
| 5589 | |
Linus Walleij | fa1374c | 2006-02-27 07:41:46 +0000 | [diff] [blame] | 5590 | /** |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 5591 | * This function sends a track from a file descriptor to an |
| 5592 | * MTP device. A filename and a set of metadata must be |
| 5593 | * given as input. |
| 5594 | * @param device a pointer to the device to send the track to. |
| 5595 | * @param fd the filedescriptor for a local file which will be sent. |
| 5596 | * @param metadata a track metadata set to be written along with the file. |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 5597 | * After this call the field <code>metadata->item_id</code> |
| 5598 | * will contain the new track ID. Other fields such |
| 5599 | * as the <code>metadata->filename</code>, <code>metadata->parent_id</code> |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 5600 | * or <code>metadata->storage_id</code> may also change during this |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 5601 | * operation due to device restrictions, so do not rely on the |
| 5602 | * contents of this struct to be preserved in any way. |
| 5603 | * <ul> |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 5604 | * <li><code>metadata->parent_id</code> should be set to the parent |
| 5605 | * (e.g. folder) to store this track in. Since some |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 5606 | * devices are a bit picky about where files |
| 5607 | * are placed, a default folder will be chosen if libmtp |
| 5608 | * has detected one for the current filetype and this |
| 5609 | * parameter is set to 0. If this is 0 and no default folder |
| 5610 | * can be found, the file will be stored in the root folder. |
| 5611 | * <li><code>metadata->storage_id</code> should be set to the |
| 5612 | * desired storage (e.g. memory card or whatever your device |
| 5613 | * presents) to store this track in. Setting this to 0 will store |
| 5614 | * the track on the primary storage. |
| 5615 | * </ul> |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 5616 | * @param callback a progress indicator function or NULL to ignore. |
| 5617 | * @param data a user-defined pointer that is passed along to |
| 5618 | * the <code>progress</code> function in order to |
| 5619 | * pass along some user defined data to the progress |
| 5620 | * updates. If not used, set this to NULL. |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 5621 | * @return 0 if the transfer was successful, any other value means |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 5622 | * failure. |
| 5623 | * @see LIBMTP_Send_Track_From_File() |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 5624 | * @see LIBMTP_Delete_Object() |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 5625 | */ |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 5626 | int LIBMTP_Send_Track_From_File_Descriptor(LIBMTP_mtpdevice_t *device, |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 5627 | int const fd, LIBMTP_track_t * const metadata, |
Linus Walleij | ee73ef2 | 2006-08-27 19:56:00 +0000 | [diff] [blame] | 5628 | LIBMTP_progressfunc_t const callback, |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 5629 | void const * const data) |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 5630 | { |
Linus Walleij | 17e39f7 | 2006-02-23 15:54:28 +0000 | [diff] [blame] | 5631 | int subcall_ret; |
Linus Walleij | d2778d1 | 2007-08-06 19:24:58 +0000 | [diff] [blame] | 5632 | LIBMTP_file_t filedata; |
Linus Walleij | 094b450 | 2009-09-22 22:28:33 +0000 | [diff] [blame] | 5633 | PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo; |
| 5634 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | 8ae78bb | 2006-11-20 21:45:52 +0000 | [diff] [blame] | 5635 | |
Linus Walleij | 16c51f0 | 2006-05-04 13:20:22 +0000 | [diff] [blame] | 5636 | // Sanity check, is this really a track? |
Linus Walleij | 10e0ce7 | 2008-05-04 19:20:33 +0000 | [diff] [blame] | 5637 | if (!LIBMTP_FILETYPE_IS_TRACK(metadata->filetype)) { |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 5638 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, |
Linus Walleij | 46651f3 | 2008-04-26 23:30:19 +0000 | [diff] [blame] | 5639 | "LIBMTP_Send_Track_From_File_Descriptor(): " |
| 5640 | "I don't think this is actually a track, strange filetype..."); |
Linus Walleij | 99310d4 | 2006-11-01 08:29:39 +0000 | [diff] [blame] | 5641 | } |
Linus Walleij | 3725329 | 2006-10-11 08:38:14 +0000 | [diff] [blame] | 5642 | |
Linus Walleij | d2778d1 | 2007-08-06 19:24:58 +0000 | [diff] [blame] | 5643 | // Wrap around the file transfer function |
| 5644 | filedata.item_id = metadata->item_id; |
| 5645 | filedata.parent_id = metadata->parent_id; |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 5646 | filedata.storage_id = metadata->storage_id; |
Linus Walleij | 094b450 | 2009-09-22 22:28:33 +0000 | [diff] [blame] | 5647 | if FLAG_UNIQUE_FILENAMES(ptp_usb) { |
| 5648 | filedata.filename = generate_unique_filename(params, metadata->filename); |
| 5649 | } |
| 5650 | else { |
| 5651 | filedata.filename = metadata->filename; |
| 5652 | } |
Linus Walleij | d2778d1 | 2007-08-06 19:24:58 +0000 | [diff] [blame] | 5653 | filedata.filesize = metadata->filesize; |
| 5654 | filedata.filetype = metadata->filetype; |
| 5655 | filedata.next = NULL; |
Linus Walleij | 3725329 | 2006-10-11 08:38:14 +0000 | [diff] [blame] | 5656 | |
Linus Walleij | d2778d1 | 2007-08-06 19:24:58 +0000 | [diff] [blame] | 5657 | subcall_ret = LIBMTP_Send_File_From_File_Descriptor(device, |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 5658 | fd, |
Linus Walleij | d2778d1 | 2007-08-06 19:24:58 +0000 | [diff] [blame] | 5659 | &filedata, |
| 5660 | callback, |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 5661 | data); |
mopoke | 3136444 | 2006-11-20 04:53:04 +0000 | [diff] [blame] | 5662 | |
Linus Walleij | d2778d1 | 2007-08-06 19:24:58 +0000 | [diff] [blame] | 5663 | if (subcall_ret != 0) { |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 5664 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, |
Linus Walleij | 46651f3 | 2008-04-26 23:30:19 +0000 | [diff] [blame] | 5665 | "LIBMTP_Send_Track_From_File_Descriptor(): " |
Linus Walleij | d2778d1 | 2007-08-06 19:24:58 +0000 | [diff] [blame] | 5666 | "subcall to LIBMTP_Send_File_From_File_Descriptor failed."); |
| 5667 | // We used to delete the file here, but don't... It might be OK after all. |
| 5668 | // (void) LIBMTP_Delete_Object(device, metadata->item_id); |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 5669 | return -1; |
| 5670 | } |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 5671 | |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 5672 | // Pick up new item (and parent, storage) ID |
Linus Walleij | d2778d1 | 2007-08-06 19:24:58 +0000 | [diff] [blame] | 5673 | metadata->item_id = filedata.item_id; |
| 5674 | metadata->parent_id = filedata.parent_id; |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 5675 | metadata->storage_id = filedata.storage_id; |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 5676 | |
Linus Walleij | 17e39f7 | 2006-02-23 15:54:28 +0000 | [diff] [blame] | 5677 | // Set track metadata for the new fine track |
| 5678 | subcall_ret = LIBMTP_Update_Track_Metadata(device, metadata); |
| 5679 | if (subcall_ret != 0) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 5680 | // Subcall will add error to errorstack |
Linus Walleij | b275318 | 2007-02-26 08:11:38 +0000 | [diff] [blame] | 5681 | // We used to delete the file here, but don't... It might be OK after all. |
| 5682 | // (void) LIBMTP_Delete_Object(device, metadata->item_id); |
Linus Walleij | 17e39f7 | 2006-02-23 15:54:28 +0000 | [diff] [blame] | 5683 | return -1; |
| 5684 | } |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 5685 | |
Richard Low | 61edc1a | 2007-09-23 10:35:48 +0000 | [diff] [blame] | 5686 | // note we don't need to update the cache here because LIBMTP_Send_File_From_File_Descriptor |
| 5687 | // has added the object handle and LIBMTP_Update_Track_Metadata has added the metadata. |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 5688 | |
Linus Walleij | 17e39f7 | 2006-02-23 15:54:28 +0000 | [diff] [blame] | 5689 | return 0; |
| 5690 | } |
| 5691 | |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 5692 | /** |
Richard Low | d3b1702 | 2009-04-11 12:37:39 +0000 | [diff] [blame] | 5693 | * This function sends a track from a handler function to an |
| 5694 | * MTP device. A filename and a set of metadata must be |
| 5695 | * given as input. |
| 5696 | * @param device a pointer to the device to send the track to. |
| 5697 | * @param get_func the function to call when we have data. |
| 5698 | * @param priv the user-defined pointer that is passed to |
| 5699 | * <code>get_func</code>. |
| 5700 | * @param metadata a track metadata set to be written along with the file. |
| 5701 | * After this call the field <code>metadata->item_id</code> |
| 5702 | * will contain the new track ID. Other fields such |
| 5703 | * as the <code>metadata->filename</code>, <code>metadata->parent_id</code> |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 5704 | * or <code>metadata->storage_id</code> may also change during this |
Richard Low | d3b1702 | 2009-04-11 12:37:39 +0000 | [diff] [blame] | 5705 | * operation due to device restrictions, so do not rely on the |
| 5706 | * contents of this struct to be preserved in any way. |
| 5707 | * <ul> |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 5708 | * <li><code>metadata->parent_id</code> should be set to the parent |
| 5709 | * (e.g. folder) to store this track in. Since some |
Richard Low | d3b1702 | 2009-04-11 12:37:39 +0000 | [diff] [blame] | 5710 | * devices are a bit picky about where files |
| 5711 | * are placed, a default folder will be chosen if libmtp |
| 5712 | * has detected one for the current filetype and this |
| 5713 | * parameter is set to 0. If this is 0 and no default folder |
| 5714 | * can be found, the file will be stored in the root folder. |
| 5715 | * <li><code>metadata->storage_id</code> should be set to the |
| 5716 | * desired storage (e.g. memory card or whatever your device |
| 5717 | * presents) to store this track in. Setting this to 0 will store |
| 5718 | * the track on the primary storage. |
| 5719 | * </ul> |
| 5720 | * @param callback a progress indicator function or NULL to ignore. |
| 5721 | * @param data a user-defined pointer that is passed along to |
| 5722 | * the <code>progress</code> function in order to |
| 5723 | * pass along some user defined data to the progress |
| 5724 | * updates. If not used, set this to NULL. |
| 5725 | * @return 0 if the transfer was successful, any other value means |
| 5726 | * failure. |
| 5727 | * @see LIBMTP_Send_Track_From_File() |
| 5728 | * @see LIBMTP_Delete_Object() |
| 5729 | */ |
| 5730 | int LIBMTP_Send_Track_From_Handler(LIBMTP_mtpdevice_t *device, |
| 5731 | MTPDataGetFunc get_func, void * priv, LIBMTP_track_t * const metadata, |
| 5732 | LIBMTP_progressfunc_t const callback, |
| 5733 | void const * const data) |
| 5734 | { |
| 5735 | int subcall_ret; |
| 5736 | LIBMTP_file_t filedata; |
Linus Walleij | 094b450 | 2009-09-22 22:28:33 +0000 | [diff] [blame] | 5737 | PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo; |
| 5738 | PTPParams *params = (PTPParams *) device->params; |
Richard Low | d3b1702 | 2009-04-11 12:37:39 +0000 | [diff] [blame] | 5739 | |
| 5740 | // Sanity check, is this really a track? |
| 5741 | if (!LIBMTP_FILETYPE_IS_TRACK(metadata->filetype)) { |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 5742 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, |
Richard Low | d3b1702 | 2009-04-11 12:37:39 +0000 | [diff] [blame] | 5743 | "LIBMTP_Send_Track_From_Handler(): " |
| 5744 | "I don't think this is actually a track, strange filetype..."); |
| 5745 | } |
| 5746 | |
| 5747 | // Wrap around the file transfer function |
| 5748 | filedata.item_id = metadata->item_id; |
| 5749 | filedata.parent_id = metadata->parent_id; |
| 5750 | filedata.storage_id = metadata->storage_id; |
Linus Walleij | 094b450 | 2009-09-22 22:28:33 +0000 | [diff] [blame] | 5751 | if FLAG_UNIQUE_FILENAMES(ptp_usb) { |
| 5752 | filedata.filename = generate_unique_filename(params, metadata->filename); |
| 5753 | } |
| 5754 | else { |
| 5755 | filedata.filename = metadata->filename; |
| 5756 | } |
Richard Low | d3b1702 | 2009-04-11 12:37:39 +0000 | [diff] [blame] | 5757 | filedata.filesize = metadata->filesize; |
| 5758 | filedata.filetype = metadata->filetype; |
| 5759 | filedata.next = NULL; |
| 5760 | |
| 5761 | subcall_ret = LIBMTP_Send_File_From_Handler(device, |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 5762 | get_func, |
| 5763 | priv, |
| 5764 | &filedata, |
| 5765 | callback, |
| 5766 | data); |
Richard Low | d3b1702 | 2009-04-11 12:37:39 +0000 | [diff] [blame] | 5767 | |
| 5768 | if (subcall_ret != 0) { |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 5769 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, |
Richard Low | d3b1702 | 2009-04-11 12:37:39 +0000 | [diff] [blame] | 5770 | "LIBMTP_Send_Track_From_Handler(): " |
| 5771 | "subcall to LIBMTP_Send_File_From_Handler failed."); |
| 5772 | // We used to delete the file here, but don't... It might be OK after all. |
| 5773 | // (void) LIBMTP_Delete_Object(device, metadata->item_id); |
| 5774 | return -1; |
| 5775 | } |
Linus Walleij | d8fd9a5 | 2009-09-09 22:46:01 +0000 | [diff] [blame] | 5776 | |
Richard Low | d3b1702 | 2009-04-11 12:37:39 +0000 | [diff] [blame] | 5777 | // Pick up new item (and parent, storage) ID |
| 5778 | metadata->item_id = filedata.item_id; |
| 5779 | metadata->parent_id = filedata.parent_id; |
| 5780 | metadata->storage_id = filedata.storage_id; |
| 5781 | |
| 5782 | // Set track metadata for the new fine track |
| 5783 | subcall_ret = LIBMTP_Update_Track_Metadata(device, metadata); |
| 5784 | if (subcall_ret != 0) { |
| 5785 | // Subcall will add error to errorstack |
| 5786 | // We used to delete the file here, but don't... It might be OK after all. |
| 5787 | // (void) LIBMTP_Delete_Object(device, metadata->item_id); |
| 5788 | return -1; |
| 5789 | } |
| 5790 | |
| 5791 | // note we don't need to update the cache here because LIBMTP_Send_File_From_File_Descriptor |
| 5792 | // has added the object handle and LIBMTP_Update_Track_Metadata has added the metadata. |
| 5793 | |
| 5794 | return 0; |
| 5795 | } |
| 5796 | |
| 5797 | /** |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 5798 | * This function sends a local file to an MTP device. |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 5799 | * A filename and a set of metadata must be |
| 5800 | * given as input. |
| 5801 | * @param device a pointer to the device to send the track to. |
| 5802 | * @param path the filename of a local file which will be sent. |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 5803 | * @param filedata a file metadata set to be written along with the file. |
| 5804 | * After this call the field <code>filedata->item_id</code> |
| 5805 | * will contain the new file ID. Other fields such |
| 5806 | * as the <code>filedata->filename</code>, <code>filedata->parent_id</code> |
Linus Walleij | d8fd9a5 | 2009-09-09 22:46:01 +0000 | [diff] [blame] | 5807 | * or <code>filedata->storage_id</code> may also change during this |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 5808 | * operation due to device restrictions, so do not rely on the |
| 5809 | * contents of this struct to be preserved in any way. |
| 5810 | * <ul> |
Linus Walleij | d8fd9a5 | 2009-09-09 22:46:01 +0000 | [diff] [blame] | 5811 | * <li><code>filedata->parent_id</code> should be set to the parent |
| 5812 | * (e.g. folder) to store this file in. If this is 0, |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 5813 | * the file will be stored in the root folder. |
| 5814 | * <li><code>filedata->storage_id</code> should be set to the |
| 5815 | * desired storage (e.g. memory card or whatever your device |
| 5816 | * presents) to store this file in. Setting this to 0 will store |
| 5817 | * the file on the primary storage. |
| 5818 | * </ul> |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 5819 | * @param callback a progress indicator function or NULL to ignore. |
| 5820 | * @param data a user-defined pointer that is passed along to |
| 5821 | * the <code>progress</code> function in order to |
| 5822 | * pass along some user defined data to the progress |
| 5823 | * updates. If not used, set this to NULL. |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 5824 | * @return 0 if the transfer was successful, any other value means |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 5825 | * failure. |
| 5826 | * @see LIBMTP_Send_File_From_File_Descriptor() |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 5827 | * @see LIBMTP_Delete_Object() |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 5828 | */ |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 5829 | int LIBMTP_Send_File_From_File(LIBMTP_mtpdevice_t *device, |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 5830 | char const * const path, LIBMTP_file_t * const filedata, |
Linus Walleij | ee73ef2 | 2006-08-27 19:56:00 +0000 | [diff] [blame] | 5831 | LIBMTP_progressfunc_t const callback, |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 5832 | void const * const data) |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 5833 | { |
| 5834 | int fd; |
| 5835 | int ret; |
| 5836 | |
| 5837 | // Sanity check |
| 5838 | if (path == NULL) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 5839 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Send_File_From_File(): Bad arguments, path was NULL."); |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 5840 | return -1; |
| 5841 | } |
| 5842 | |
| 5843 | // Open file |
| 5844 | #ifdef __WIN32__ |
Linus Walleij | 7beba57 | 2006-11-29 08:56:12 +0000 | [diff] [blame] | 5845 | #ifdef USE_WINDOWS_IO_H |
Linus Walleij | 3017c56 | 2010-10-17 21:31:36 +0000 | [diff] [blame] | 5846 | if ( (fd = _open(path, O_RDONLY|O_BINARY)) == -1 ) { |
Linus Walleij | 7beba57 | 2006-11-29 08:56:12 +0000 | [diff] [blame] | 5847 | #else |
Linus Walleij | 3017c56 | 2010-10-17 21:31:36 +0000 | [diff] [blame] | 5848 | if ( (fd = open(path, O_RDONLY|O_BINARY)) == -1 ) { |
Linus Walleij | 7beba57 | 2006-11-29 08:56:12 +0000 | [diff] [blame] | 5849 | #endif |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 5850 | #else |
| 5851 | if ( (fd = open(path, O_RDONLY)) == -1) { |
| 5852 | #endif |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 5853 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Send_File_From_File(): Could not open source file."); |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 5854 | return -1; |
| 5855 | } |
| 5856 | |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 5857 | ret = LIBMTP_Send_File_From_File_Descriptor(device, fd, filedata, callback, data); |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 5858 | |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 5859 | // Close file. |
Linus Walleij | 7beba57 | 2006-11-29 08:56:12 +0000 | [diff] [blame] | 5860 | #ifdef USE_WINDOWS_IO_H |
| 5861 | _close(fd); |
| 5862 | #else |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 5863 | close(fd); |
Linus Walleij | 7beba57 | 2006-11-29 08:56:12 +0000 | [diff] [blame] | 5864 | #endif |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 5865 | |
| 5866 | return ret; |
| 5867 | } |
| 5868 | |
Linus Walleij | d208f9c | 2006-04-27 14:16:06 +0000 | [diff] [blame] | 5869 | /** |
| 5870 | * This function sends a generic file from a file descriptor to an |
| 5871 | * MTP device. A filename and a set of metadata must be |
| 5872 | * given as input. |
Linus Walleij | cd3eb3d | 2006-09-02 21:33:22 +0000 | [diff] [blame] | 5873 | * |
| 5874 | * This can potentially be used for sending in a stream of unknown |
Linus Walleij | d8fd9a5 | 2009-09-09 22:46:01 +0000 | [diff] [blame] | 5875 | * length. Send music files with |
Linus Walleij | 1b87ea7 | 2007-08-28 07:53:09 +0000 | [diff] [blame] | 5876 | * <code>LIBMTP_Send_Track_From_File_Descriptor()</code> |
Linus Walleij | cd3eb3d | 2006-09-02 21:33:22 +0000 | [diff] [blame] | 5877 | * |
Linus Walleij | d208f9c | 2006-04-27 14:16:06 +0000 | [diff] [blame] | 5878 | * @param device a pointer to the device to send the file to. |
| 5879 | * @param fd the filedescriptor for a local file which will be sent. |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 5880 | * @param filedata a file metadata set to be written along with the file. |
| 5881 | * After this call the field <code>filedata->item_id</code> |
| 5882 | * will contain the new file ID. Other fields such |
| 5883 | * as the <code>filedata->filename</code>, <code>filedata->parent_id</code> |
Linus Walleij | d8fd9a5 | 2009-09-09 22:46:01 +0000 | [diff] [blame] | 5884 | * or <code>filedata->storage_id</code> may also change during this |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 5885 | * operation due to device restrictions, so do not rely on the |
| 5886 | * contents of this struct to be preserved in any way. |
| 5887 | * <ul> |
Linus Walleij | d8fd9a5 | 2009-09-09 22:46:01 +0000 | [diff] [blame] | 5888 | * <li><code>filedata->parent_id</code> should be set to the parent |
| 5889 | * (e.g. folder) to store this file in. If this is 0, |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 5890 | * the file will be stored in the root folder. |
| 5891 | * <li><code>filedata->storage_id</code> should be set to the |
| 5892 | * desired storage (e.g. memory card or whatever your device |
| 5893 | * presents) to store this file in. Setting this to 0 will store |
| 5894 | * the file on the primary storage. |
| 5895 | * </ul> |
Linus Walleij | d208f9c | 2006-04-27 14:16:06 +0000 | [diff] [blame] | 5896 | * @param callback a progress indicator function or NULL to ignore. |
| 5897 | * @param data a user-defined pointer that is passed along to |
| 5898 | * the <code>progress</code> function in order to |
| 5899 | * pass along some user defined data to the progress |
| 5900 | * updates. If not used, set this to NULL. |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 5901 | * @return 0 if the transfer was successful, any other value means |
Linus Walleij | d208f9c | 2006-04-27 14:16:06 +0000 | [diff] [blame] | 5902 | * failure. |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 5903 | * @see LIBMTP_Send_File_From_File() |
Linus Walleij | 1b87ea7 | 2007-08-28 07:53:09 +0000 | [diff] [blame] | 5904 | * @see LIBMTP_Send_Track_From_File_Descriptor() |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 5905 | * @see LIBMTP_Delete_Object() |
Linus Walleij | d208f9c | 2006-04-27 14:16:06 +0000 | [diff] [blame] | 5906 | */ |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 5907 | int LIBMTP_Send_File_From_File_Descriptor(LIBMTP_mtpdevice_t *device, |
Linus Walleij | d208f9c | 2006-04-27 14:16:06 +0000 | [diff] [blame] | 5908 | int const fd, LIBMTP_file_t * const filedata, |
Linus Walleij | ee73ef2 | 2006-08-27 19:56:00 +0000 | [diff] [blame] | 5909 | LIBMTP_progressfunc_t const callback, |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 5910 | void const * const data) |
Linus Walleij | d208f9c | 2006-04-27 14:16:06 +0000 | [diff] [blame] | 5911 | { |
| 5912 | uint16_t ret; |
Linus Walleij | d208f9c | 2006-04-27 14:16:06 +0000 | [diff] [blame] | 5913 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | d214b9b | 2006-08-26 22:08:37 +0000 | [diff] [blame] | 5914 | PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo; |
Linus Walleij | 817579a | 2008-05-23 21:21:40 +0000 | [diff] [blame] | 5915 | LIBMTP_file_t *newfilemeta; |
Linus Walleij | e04a1b9 | 2011-03-09 18:00:24 +0100 | [diff] [blame] | 5916 | int oldtimeout; |
| 5917 | int timeout; |
Richard Low | ab0d22d | 2006-11-30 22:17:49 +0000 | [diff] [blame] | 5918 | |
Richard Low | d3b1702 | 2009-04-11 12:37:39 +0000 | [diff] [blame] | 5919 | if (send_file_object_info(device, filedata)) |
| 5920 | { |
| 5921 | // no need to output an error since send_file_object_info will already have done so |
Linus Walleij | 0779577 | 2007-08-06 18:44:06 +0000 | [diff] [blame] | 5922 | return -1; |
Linus Walleij | cd3eb3d | 2006-09-02 21:33:22 +0000 | [diff] [blame] | 5923 | } |
Linus Walleij | 0779577 | 2007-08-06 18:44:06 +0000 | [diff] [blame] | 5924 | |
Richard Low | d3b1702 | 2009-04-11 12:37:39 +0000 | [diff] [blame] | 5925 | // Callbacks |
| 5926 | ptp_usb->callback_active = 1; |
| 5927 | // The callback will deactivate itself after this amount of data has been sent |
| 5928 | // One BULK header for the request, one for the data phase. No parameters to the request. |
| 5929 | ptp_usb->current_transfer_total = filedata->filesize+PTP_USB_BULK_HDR_LEN*2; |
| 5930 | ptp_usb->current_transfer_complete = 0; |
| 5931 | ptp_usb->current_transfer_callback = callback; |
| 5932 | ptp_usb->current_transfer_callback_data = data; |
Linus Walleij | d8fd9a5 | 2009-09-09 22:46:01 +0000 | [diff] [blame] | 5933 | |
Linus Walleij | e04a1b9 | 2011-03-09 18:00:24 +0100 | [diff] [blame] | 5934 | /* |
| 5935 | * We might need to increase the timeout here, files can be pretty |
| 5936 | * large. Take the default timeout and add the calculated time for |
| 5937 | * this transfer |
| 5938 | */ |
| 5939 | get_usb_device_timeout(ptp_usb, &oldtimeout); |
| 5940 | timeout = oldtimeout + |
| 5941 | (ptp_usb->current_transfer_total / guess_usb_speed(ptp_usb)) * 1000; |
| 5942 | set_usb_device_timeout(ptp_usb, timeout); |
| 5943 | |
Richard Low | d3b1702 | 2009-04-11 12:37:39 +0000 | [diff] [blame] | 5944 | ret = ptp_sendobject_fromfd(params, fd, filedata->filesize); |
Linus Walleij | d8fd9a5 | 2009-09-09 22:46:01 +0000 | [diff] [blame] | 5945 | |
Richard Low | d3b1702 | 2009-04-11 12:37:39 +0000 | [diff] [blame] | 5946 | ptp_usb->callback_active = 0; |
| 5947 | ptp_usb->current_transfer_callback = NULL; |
| 5948 | ptp_usb->current_transfer_callback_data = NULL; |
Linus Walleij | e04a1b9 | 2011-03-09 18:00:24 +0100 | [diff] [blame] | 5949 | set_usb_device_timeout(ptp_usb, oldtimeout); |
Richard Low | d3b1702 | 2009-04-11 12:37:39 +0000 | [diff] [blame] | 5950 | |
| 5951 | if (ret == PTP_ERROR_CANCEL) { |
| 5952 | add_error_to_errorstack(device, LIBMTP_ERROR_CANCELLED, "LIBMTP_Send_File_From_File_Descriptor(): Cancelled transfer."); |
| 5953 | return -1; |
| 5954 | } |
| 5955 | if (ret != PTP_RC_OK) { |
| 5956 | add_ptp_error_to_errorstack(device, ret, "LIBMTP_Send_File_From_File_Descriptor(): " |
| 5957 | "Could not send object."); |
| 5958 | return -1; |
| 5959 | } |
| 5960 | |
| 5961 | add_object_to_cache(device, filedata->item_id); |
Linus Walleij | d8fd9a5 | 2009-09-09 22:46:01 +0000 | [diff] [blame] | 5962 | |
Richard Low | d3b1702 | 2009-04-11 12:37:39 +0000 | [diff] [blame] | 5963 | /* |
Linus Walleij | 4fc1917 | 2010-01-19 00:10:26 +0000 | [diff] [blame] | 5964 | * Get the device-assigned parent_id from the cache. |
Richard Low | d3b1702 | 2009-04-11 12:37:39 +0000 | [diff] [blame] | 5965 | * The operation that adds it to the cache will |
| 5966 | * look it up from the device, so we get the new |
| 5967 | * parent_id from the cache. |
| 5968 | */ |
| 5969 | newfilemeta = LIBMTP_Get_Filemetadata(device, filedata->item_id); |
| 5970 | if (newfilemeta != NULL) { |
| 5971 | filedata->parent_id = newfilemeta->parent_id; |
| 5972 | filedata->storage_id = newfilemeta->storage_id; |
| 5973 | LIBMTP_destroy_file_t(newfilemeta); |
| 5974 | } else { |
| 5975 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, |
| 5976 | "LIBMTP_Send_File_From_File_Descriptor(): " |
| 5977 | "Could not retrieve updated metadata."); |
| 5978 | return -1; |
| 5979 | } |
| 5980 | |
| 5981 | return 0; |
| 5982 | } |
| 5983 | |
| 5984 | /** |
| 5985 | * This function sends a generic file from a handler function to an |
| 5986 | * MTP device. A filename and a set of metadata must be |
| 5987 | * given as input. |
| 5988 | * |
| 5989 | * This can potentially be used for sending in a stream of unknown |
Linus Walleij | d8fd9a5 | 2009-09-09 22:46:01 +0000 | [diff] [blame] | 5990 | * length. Send music files with |
Richard Low | d3b1702 | 2009-04-11 12:37:39 +0000 | [diff] [blame] | 5991 | * <code>LIBMTP_Send_Track_From_Handler()</code> |
| 5992 | * |
| 5993 | * @param device a pointer to the device to send the file to. |
| 5994 | * @param get_func the function to call to get data to write |
| 5995 | * @param priv a user-defined pointer that is passed along to |
| 5996 | * <code>get_func</code>. If not used, this is set to NULL. |
| 5997 | * @param filedata a file metadata set to be written along with the file. |
| 5998 | * After this call the field <code>filedata->item_id</code> |
| 5999 | * will contain the new file ID. Other fields such |
| 6000 | * as the <code>filedata->filename</code>, <code>filedata->parent_id</code> |
Linus Walleij | d8fd9a5 | 2009-09-09 22:46:01 +0000 | [diff] [blame] | 6001 | * or <code>filedata->storage_id</code> may also change during this |
Richard Low | d3b1702 | 2009-04-11 12:37:39 +0000 | [diff] [blame] | 6002 | * operation due to device restrictions, so do not rely on the |
| 6003 | * contents of this struct to be preserved in any way. |
| 6004 | * <ul> |
Linus Walleij | d8fd9a5 | 2009-09-09 22:46:01 +0000 | [diff] [blame] | 6005 | * <li><code>filedata->parent_id</code> should be set to the parent |
| 6006 | * (e.g. folder) to store this file in. If this is 0, |
Richard Low | d3b1702 | 2009-04-11 12:37:39 +0000 | [diff] [blame] | 6007 | * the file will be stored in the root folder. |
| 6008 | * <li><code>filedata->storage_id</code> should be set to the |
| 6009 | * desired storage (e.g. memory card or whatever your device |
| 6010 | * presents) to store this file in. Setting this to 0 will store |
| 6011 | * the file on the primary storage. |
| 6012 | * </ul> |
| 6013 | * @param callback a progress indicator function or NULL to ignore. |
| 6014 | * @param data a user-defined pointer that is passed along to |
| 6015 | * the <code>progress</code> function in order to |
| 6016 | * pass along some user defined data to the progress |
| 6017 | * updates. If not used, set this to NULL. |
| 6018 | * @return 0 if the transfer was successful, any other value means |
| 6019 | * failure. |
| 6020 | * @see LIBMTP_Send_File_From_File() |
| 6021 | * @see LIBMTP_Send_Track_From_File_Descriptor() |
| 6022 | * @see LIBMTP_Delete_Object() |
| 6023 | */ |
| 6024 | int LIBMTP_Send_File_From_Handler(LIBMTP_mtpdevice_t *device, |
| 6025 | MTPDataGetFunc get_func, void * priv, LIBMTP_file_t * const filedata, |
| 6026 | LIBMTP_progressfunc_t const callback, void const * const data) |
| 6027 | { |
| 6028 | uint16_t ret; |
| 6029 | PTPParams *params = (PTPParams *) device->params; |
| 6030 | PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo; |
| 6031 | LIBMTP_file_t *newfilemeta; |
| 6032 | |
| 6033 | if (send_file_object_info(device, filedata)) |
| 6034 | { |
| 6035 | // no need to output an error since send_file_object_info will already have done so |
| 6036 | return -1; |
| 6037 | } |
Linus Walleij | d866d24 | 2009-08-23 21:50:39 +0000 | [diff] [blame] | 6038 | |
Richard Low | d3b1702 | 2009-04-11 12:37:39 +0000 | [diff] [blame] | 6039 | // Callbacks |
| 6040 | ptp_usb->callback_active = 1; |
| 6041 | // The callback will deactivate itself after this amount of data has been sent |
| 6042 | // One BULK header for the request, one for the data phase. No parameters to the request. |
| 6043 | ptp_usb->current_transfer_total = filedata->filesize+PTP_USB_BULK_HDR_LEN*2; |
| 6044 | ptp_usb->current_transfer_complete = 0; |
| 6045 | ptp_usb->current_transfer_callback = callback; |
| 6046 | ptp_usb->current_transfer_callback_data = data; |
Linus Walleij | d866d24 | 2009-08-23 21:50:39 +0000 | [diff] [blame] | 6047 | |
Richard Low | 5b4023c | 2009-04-16 19:14:38 +0000 | [diff] [blame] | 6048 | MTPDataHandler mtp_handler; |
| 6049 | mtp_handler.getfunc = get_func; |
| 6050 | mtp_handler.putfunc = NULL; |
Linus Walleij | d866d24 | 2009-08-23 21:50:39 +0000 | [diff] [blame] | 6051 | mtp_handler.priv = priv; |
| 6052 | |
Richard Low | d3b1702 | 2009-04-11 12:37:39 +0000 | [diff] [blame] | 6053 | PTPDataHandler handler; |
Richard Low | 5b4023c | 2009-04-16 19:14:38 +0000 | [diff] [blame] | 6054 | handler.getfunc = get_func_wrapper; |
Richard Low | d3b1702 | 2009-04-11 12:37:39 +0000 | [diff] [blame] | 6055 | handler.putfunc = NULL; |
Linus Walleij | d866d24 | 2009-08-23 21:50:39 +0000 | [diff] [blame] | 6056 | handler.priv = &mtp_handler; |
| 6057 | |
Richard Low | d3b1702 | 2009-04-11 12:37:39 +0000 | [diff] [blame] | 6058 | ret = ptp_sendobject_from_handler(params, &handler, filedata->filesize); |
Linus Walleij | d866d24 | 2009-08-23 21:50:39 +0000 | [diff] [blame] | 6059 | |
Richard Low | d3b1702 | 2009-04-11 12:37:39 +0000 | [diff] [blame] | 6060 | ptp_usb->callback_active = 0; |
| 6061 | ptp_usb->current_transfer_callback = NULL; |
| 6062 | ptp_usb->current_transfer_callback_data = NULL; |
| 6063 | |
| 6064 | if (ret == PTP_ERROR_CANCEL) { |
| 6065 | add_error_to_errorstack(device, LIBMTP_ERROR_CANCELLED, "LIBMTP_Send_File_From_Handler(): Cancelled transfer."); |
| 6066 | return -1; |
| 6067 | } |
| 6068 | if (ret != PTP_RC_OK) { |
| 6069 | add_ptp_error_to_errorstack(device, ret, "LIBMTP_Send_File_From_Handler(): " |
| 6070 | "Could not send object."); |
| 6071 | return -1; |
| 6072 | } |
| 6073 | |
| 6074 | add_object_to_cache(device, filedata->item_id); |
Linus Walleij | d866d24 | 2009-08-23 21:50:39 +0000 | [diff] [blame] | 6075 | |
Richard Low | d3b1702 | 2009-04-11 12:37:39 +0000 | [diff] [blame] | 6076 | /* |
| 6077 | * Get the device-assined parent_id from the cache. |
| 6078 | * The operation that adds it to the cache will |
| 6079 | * look it up from the device, so we get the new |
| 6080 | * parent_id from the cache. |
| 6081 | */ |
| 6082 | newfilemeta = LIBMTP_Get_Filemetadata(device, filedata->item_id); |
| 6083 | if (newfilemeta != NULL) { |
| 6084 | filedata->parent_id = newfilemeta->parent_id; |
| 6085 | filedata->storage_id = newfilemeta->storage_id; |
| 6086 | LIBMTP_destroy_file_t(newfilemeta); |
| 6087 | } else { |
| 6088 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, |
| 6089 | "LIBMTP_Send_File_From_Handler(): " |
| 6090 | "Could not retrieve updated metadata."); |
| 6091 | return -1; |
| 6092 | } |
| 6093 | |
| 6094 | return 0; |
| 6095 | } |
| 6096 | |
Linus Walleij | d866d24 | 2009-08-23 21:50:39 +0000 | [diff] [blame] | 6097 | /** |
Richard Low | d3b1702 | 2009-04-11 12:37:39 +0000 | [diff] [blame] | 6098 | * This function sends the file object info, ready for sendobject |
| 6099 | * @param device a pointer to the device to send the file to. |
| 6100 | * @param filedata a file metadata set to be written along with the file. |
| 6101 | * @return 0 if the transfer was successful, any other value means |
| 6102 | * failure. |
| 6103 | */ |
| 6104 | static int send_file_object_info(LIBMTP_mtpdevice_t *device, LIBMTP_file_t *filedata) |
| 6105 | { |
| 6106 | PTPParams *params = (PTPParams *) device->params; |
| 6107 | PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo; |
| 6108 | uint32_t store; |
| 6109 | int use_primary_storage = 1; |
| 6110 | uint16_t of = map_libmtp_type_to_ptp_type(filedata->filetype); |
| 6111 | LIBMTP_devicestorage_t *storage; |
| 6112 | uint32_t localph = filedata->parent_id; |
| 6113 | uint16_t ret; |
| 6114 | int i; |
Linus Walleij | 08a5fe1 | 2009-09-08 21:28:58 +0000 | [diff] [blame] | 6115 | |
Linus Walleij | 9014328 | 2011-03-02 00:24:15 +0100 | [diff] [blame] | 6116 | #if 0 |
| 6117 | // Sanity check: no zerolength files on some devices? |
| 6118 | // If the zerolength files cause problems on some devices, |
| 6119 | // then add a bug flag for this. |
Richard Low | d3b1702 | 2009-04-11 12:37:39 +0000 | [diff] [blame] | 6120 | if (filedata->filesize == 0) { |
| 6121 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "send_file_object_info(): " |
| 6122 | "File of zero size."); |
| 6123 | return -1; |
| 6124 | } |
Linus Walleij | 9014328 | 2011-03-02 00:24:15 +0100 | [diff] [blame] | 6125 | #endif |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 6126 | if (filedata->storage_id != 0) { |
| 6127 | store = filedata->storage_id; |
Linus Walleij | fb28b63 | 2007-10-23 21:56:18 +0000 | [diff] [blame] | 6128 | } else { |
Linus Walleij | 88babc8 | 2014-06-02 21:32:14 +0200 | [diff] [blame] | 6129 | store = get_suggested_storage_id(device, filedata->filesize, localph); |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 6130 | } |
Linus Walleij | 88babc8 | 2014-06-02 21:32:14 +0200 | [diff] [blame] | 6131 | |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 6132 | // Detect if something non-primary is in use. |
| 6133 | storage = device->storage; |
Richard Low | 4d9165f | 2008-09-23 20:13:17 +0000 | [diff] [blame] | 6134 | if (storage != NULL && store != storage->id) { |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 6135 | use_primary_storage = 0; |
Linus Walleij | 6bf68b4 | 2007-09-03 22:50:02 +0000 | [diff] [blame] | 6136 | } |
Linus Walleij | d208f9c | 2006-04-27 14:16:06 +0000 | [diff] [blame] | 6137 | |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 6138 | /* |
Linus Walleij | 05ccbe7 | 2006-06-13 07:46:58 +0000 | [diff] [blame] | 6139 | * If no destination folder was given, look up a default |
| 6140 | * folder if possible. Perhaps there is some way of retrieveing |
| 6141 | * the default folder for different forms of content, what |
| 6142 | * do I know, we use a fixed list in lack of any better method. |
| 6143 | * Some devices obviously need to have their files in certain |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 6144 | * folders in order to find/display them at all (hello Creative), |
Linus Walleij | c40c9bf | 2008-06-13 23:24:17 +0000 | [diff] [blame] | 6145 | * so we have to have a method for this. We only do this if the |
| 6146 | * primary storage is in use. |
Linus Walleij | 05ccbe7 | 2006-06-13 07:46:58 +0000 | [diff] [blame] | 6147 | */ |
| 6148 | |
Linus Walleij | c40c9bf | 2008-06-13 23:24:17 +0000 | [diff] [blame] | 6149 | if (localph == 0 && use_primary_storage) { |
Linus Walleij | 10e0ce7 | 2008-05-04 19:20:33 +0000 | [diff] [blame] | 6150 | if (LIBMTP_FILETYPE_IS_AUDIO(filedata->filetype)) { |
Linus Walleij | 05ccbe7 | 2006-06-13 07:46:58 +0000 | [diff] [blame] | 6151 | localph = device->default_music_folder; |
Linus Walleij | 10e0ce7 | 2008-05-04 19:20:33 +0000 | [diff] [blame] | 6152 | } else if (LIBMTP_FILETYPE_IS_VIDEO(filedata->filetype)) { |
Linus Walleij | 05ccbe7 | 2006-06-13 07:46:58 +0000 | [diff] [blame] | 6153 | localph = device->default_video_folder; |
| 6154 | } else if (of == PTP_OFC_EXIF_JPEG || |
Linus Walleij | 5fb4713 | 2006-12-30 15:35:48 +0000 | [diff] [blame] | 6155 | of == PTP_OFC_JP2 || |
| 6156 | of == PTP_OFC_JPX || |
Linus Walleij | 05ccbe7 | 2006-06-13 07:46:58 +0000 | [diff] [blame] | 6157 | of == PTP_OFC_JFIF || |
| 6158 | of == PTP_OFC_TIFF || |
Linus Walleij | 5fb4713 | 2006-12-30 15:35:48 +0000 | [diff] [blame] | 6159 | of == PTP_OFC_TIFF_IT || |
Linus Walleij | 05ccbe7 | 2006-06-13 07:46:58 +0000 | [diff] [blame] | 6160 | of == PTP_OFC_BMP || |
| 6161 | of == PTP_OFC_GIF || |
| 6162 | of == PTP_OFC_PICT || |
| 6163 | of == PTP_OFC_PNG || |
| 6164 | of == PTP_OFC_MTP_WindowsImageFormat) { |
| 6165 | localph = device->default_picture_folder; |
| 6166 | } else if (of == PTP_OFC_MTP_vCalendar1 || |
Linus Walleij | d7aa5b2 | 2006-09-02 11:52:31 +0000 | [diff] [blame] | 6167 | of == PTP_OFC_MTP_vCalendar2 || |
| 6168 | of == PTP_OFC_MTP_UndefinedContact || |
| 6169 | of == PTP_OFC_MTP_vCard2 || |
| 6170 | of == PTP_OFC_MTP_vCard3 || |
| 6171 | of == PTP_OFC_MTP_UndefinedCalendarItem) { |
Linus Walleij | 05ccbe7 | 2006-06-13 07:46:58 +0000 | [diff] [blame] | 6172 | localph = device->default_organizer_folder; |
Linus Walleij | 46651f3 | 2008-04-26 23:30:19 +0000 | [diff] [blame] | 6173 | } else if (of == PTP_OFC_Text) { |
Linus Walleij | 9316e65 | 2006-12-07 09:55:21 +0000 | [diff] [blame] | 6174 | localph = device->default_text_folder; |
Linus Walleij | 05ccbe7 | 2006-06-13 07:46:58 +0000 | [diff] [blame] | 6175 | } |
| 6176 | } |
mopoke | 3136444 | 2006-11-20 04:53:04 +0000 | [diff] [blame] | 6177 | |
Linus Walleij | a3544f6 | 2007-11-30 01:20:04 +0000 | [diff] [blame] | 6178 | // Here we wire the type to unknown on bugged, but |
Linus Walleij | 89bb1cd | 2009-07-24 21:03:36 +0000 | [diff] [blame] | 6179 | // Ogg or FLAC-supportive devices. |
Linus Walleij | fec4d56 | 2008-06-01 22:30:36 +0000 | [diff] [blame] | 6180 | if (FLAG_OGG_IS_UNKNOWN(ptp_usb) && of == PTP_OFC_MTP_OGG) { |
Linus Walleij | a3544f6 | 2007-11-30 01:20:04 +0000 | [diff] [blame] | 6181 | of = PTP_OFC_Undefined; |
| 6182 | } |
Linus Walleij | 89bb1cd | 2009-07-24 21:03:36 +0000 | [diff] [blame] | 6183 | if (FLAG_FLAC_IS_UNKNOWN(ptp_usb) && of == PTP_OFC_MTP_FLAC) { |
| 6184 | of = PTP_OFC_Undefined; |
| 6185 | } |
Linus Walleij | a3544f6 | 2007-11-30 01:20:04 +0000 | [diff] [blame] | 6186 | |
Linus Walleij | f67c1ad | 2009-01-14 21:39:50 +0000 | [diff] [blame] | 6187 | if (ptp_operation_issupported(params, PTP_OC_MTP_SendObjectPropList) && |
| 6188 | !FLAG_BROKEN_SEND_OBJECT_PROPLIST(ptp_usb)) { |
Linus Walleij | d2778d1 | 2007-08-06 19:24:58 +0000 | [diff] [blame] | 6189 | /* |
| 6190 | * MTP enhanched does it this way (from a sniff): |
| 6191 | * -> PTP_OC_MTP_SendObjectPropList (0x9808): |
| 6192 | * 20 00 00 00 01 00 08 98 1B 00 00 00 01 00 01 00 |
| 6193 | * FF FF FF FF 00 30 00 00 00 00 00 00 12 5E 00 00 |
| 6194 | * Length: 0x00000020 |
| 6195 | * Type: 0x0001 PTP_USB_CONTAINER_COMMAND |
| 6196 | * Code: 0x9808 |
| 6197 | * Transaction ID: 0x0000001B |
| 6198 | * Param1: 0x00010001 <- store |
| 6199 | * Param2: 0xffffffff <- parent handle (-1 ?) |
| 6200 | * Param3: 0x00003000 <- file type PTP_OFC_Undefined - we don't know about PDF files |
| 6201 | * Param4: 0x00000000 <- file length MSB (-0x0c header len) |
| 6202 | * Param5: 0x00005e12 <- file length LSB (-0x0c header len) |
| 6203 | * |
| 6204 | * -> PTP_OC_MTP_SendObjectPropList (0x9808): |
| 6205 | * 46 00 00 00 02 00 08 98 1B 00 00 00 03 00 00 00 |
| 6206 | * 00 00 00 00 07 DC FF FF 0D 4B 00 53 00 30 00 36 - dc07 = file name |
| 6207 | * 00 30 00 33 00 30 00 36 00 2E 00 70 00 64 00 66 |
| 6208 | * 00 00 00 00 00 00 00 03 DC 04 00 00 00 00 00 00 - dc03 = protection status |
| 6209 | * 00 4F DC 02 00 01 - dc4f = non consumable |
| 6210 | * Length: 0x00000046 |
| 6211 | * Type: 0x0002 PTP_USB_CONTAINER_DATA |
| 6212 | * Code: 0x9808 |
| 6213 | * Transaction ID: 0x0000001B |
| 6214 | * Metadata.... |
| 6215 | * 0x00000003 <- Number of metadata items |
| 6216 | * 0x00000000 <- Object handle, set to 0x00000000 since it is unknown! |
| 6217 | * 0xdc07 <- metadata type: file name |
| 6218 | * 0xffff <- metadata type: string |
| 6219 | * 0x0d <- number of (uint16_t) characters |
| 6220 | * 4b 53 30 36 30 33 30 36 2e 50 64 66 00 "KS060306.pdf", null terminated |
| 6221 | * 0x00000000 <- Object handle, set to 0x00000000 since it is unknown! |
| 6222 | * 0xdc03 <- metadata type: protection status |
| 6223 | * 0x0004 <- metadata type: uint16_t |
| 6224 | * 0x0000 <- not protected |
| 6225 | * 0x00000000 <- Object handle, set to 0x00000000 since it is unknown! |
| 6226 | * 0xdc4f <- non consumable |
| 6227 | * 0x0002 <- metadata type: uint8_t |
| 6228 | * 0x01 <- non-consumable (this device cannot display PDF) |
| 6229 | * |
| 6230 | * <- Read 0x18 bytes back |
| 6231 | * 18 00 00 00 03 00 01 20 1B 00 00 00 01 00 01 00 |
| 6232 | * 00 00 00 00 01 40 00 00 |
| 6233 | * Length: 0x000000018 |
| 6234 | * Type: 0x0003 PTP_USB_CONTAINER_RESPONSE |
| 6235 | * Code: 0x2001 PTP_OK |
| 6236 | * Transaction ID: 0x0000001B |
| 6237 | * Param1: 0x00010001 <- store |
| 6238 | * Param2: 0x00000000 <- parent handle |
| 6239 | * Param3: 0x00004001 <- new file/object ID |
| 6240 | * |
| 6241 | * -> PTP_OC_SendObject (0x100d) |
| 6242 | * 0C 00 00 00 01 00 0D 10 1C 00 00 00 |
| 6243 | * -> ... all the bytes ... |
| 6244 | * <- Read 0x0c bytes back |
| 6245 | * 0C 00 00 00 03 00 01 20 1C 00 00 00 |
| 6246 | * ... Then update metadata one-by one, actually (instead of sending it first!) ... |
| 6247 | */ |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 6248 | MTPProperties *props = NULL; |
| 6249 | int nrofprops = 0; |
| 6250 | MTPProperties *prop = NULL; |
| 6251 | uint16_t *properties = NULL; |
Linus Walleij | 0779577 | 2007-08-06 18:44:06 +0000 | [diff] [blame] | 6252 | uint32_t propcnt = 0; |
Linus Walleij | 08a5fe1 | 2009-09-08 21:28:58 +0000 | [diff] [blame] | 6253 | |
Linus Walleij | 9036b33 | 2008-05-23 21:01:36 +0000 | [diff] [blame] | 6254 | // default parent handle |
Linus Walleij | 0779577 | 2007-08-06 18:44:06 +0000 | [diff] [blame] | 6255 | if (localph == 0) |
| 6256 | localph = 0xFFFFFFFFU; // Set to -1 |
| 6257 | |
Richard Low | 6c0a6ce | 2006-11-26 10:42:08 +0000 | [diff] [blame] | 6258 | // Must be 0x00000000U for new objects |
| 6259 | filedata->item_id = 0x00000000U; |
Linus Walleij | 05ccbe7 | 2006-06-13 07:46:58 +0000 | [diff] [blame] | 6260 | |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 6261 | ret = ptp_mtp_getobjectpropssupported(params, of, &propcnt, &properties); |
mopoke | 3136444 | 2006-11-20 04:53:04 +0000 | [diff] [blame] | 6262 | |
rreardon | bbb4e56 | 2006-11-19 22:16:11 +0000 | [diff] [blame] | 6263 | for (i=0;i<propcnt;i++) { |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 6264 | PTPObjectPropDesc opd; |
Linus Walleij | 08a5fe1 | 2009-09-08 21:28:58 +0000 | [diff] [blame] | 6265 | |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 6266 | ret = ptp_mtp_getobjectpropdesc(params, properties[i], of, &opd); |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 6267 | if (ret != PTP_RC_OK) { |
Richard Low | d3b1702 | 2009-04-11 12:37:39 +0000 | [diff] [blame] | 6268 | add_ptp_error_to_errorstack(device, ret, "send_file_object_info(): " |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 6269 | "could not get property description."); |
| 6270 | } else if (opd.GetSet) { |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 6271 | switch (properties[i]) { |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 6272 | case PTP_OPC_ObjectFileName: |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 6273 | prop = ptp_get_new_object_prop_entry(&props,&nrofprops); |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 6274 | prop->ObjectHandle = filedata->item_id; |
| 6275 | prop->property = PTP_OPC_ObjectFileName; |
| 6276 | prop->datatype = PTP_DTC_STR; |
Linus Walleij | d3b7857 | 2007-08-24 21:28:24 +0000 | [diff] [blame] | 6277 | if (filedata->filename != NULL) { |
Linus Walleij | 0779577 | 2007-08-06 18:44:06 +0000 | [diff] [blame] | 6278 | prop->propval.str = strdup(filedata->filename); |
Linus Walleij | fec4d56 | 2008-06-01 22:30:36 +0000 | [diff] [blame] | 6279 | if (FLAG_ONLY_7BIT_FILENAMES(ptp_usb)) { |
Linus Walleij | d3b7857 | 2007-08-24 21:28:24 +0000 | [diff] [blame] | 6280 | strip_7bit_from_utf8(prop->propval.str); |
| 6281 | } |
| 6282 | } |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 6283 | break; |
| 6284 | case PTP_OPC_ProtectionStatus: |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 6285 | prop = ptp_get_new_object_prop_entry(&props,&nrofprops); |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 6286 | prop->ObjectHandle = filedata->item_id; |
| 6287 | prop->property = PTP_OPC_ProtectionStatus; |
| 6288 | prop->datatype = PTP_DTC_UINT16; |
| 6289 | prop->propval.u16 = 0x0000U; /* Not protected */ |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 6290 | break; |
| 6291 | case PTP_OPC_NonConsumable: |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 6292 | prop = ptp_get_new_object_prop_entry(&props,&nrofprops); |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 6293 | prop->ObjectHandle = filedata->item_id; |
| 6294 | prop->property = PTP_OPC_NonConsumable; |
| 6295 | prop->datatype = PTP_DTC_UINT8; |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 6296 | prop->propval.u8 = 0x00; /* It is supported, then it is consumable */ |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 6297 | break; |
| 6298 | case PTP_OPC_Name: |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 6299 | prop = ptp_get_new_object_prop_entry(&props,&nrofprops); |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 6300 | prop->ObjectHandle = filedata->item_id; |
| 6301 | prop->property = PTP_OPC_Name; |
| 6302 | prop->datatype = PTP_DTC_STR; |
Linus Walleij | 0779577 | 2007-08-06 18:44:06 +0000 | [diff] [blame] | 6303 | if (filedata->filename != NULL) |
| 6304 | prop->propval.str = strdup(filedata->filename); |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 6305 | break; |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 6306 | case PTP_OPC_DateModified: |
| 6307 | // Tag with current time if that is supported |
Linus Walleij | cf8dc2b | 2008-10-21 13:58:36 +0000 | [diff] [blame] | 6308 | if (!FLAG_CANNOT_HANDLE_DATEMODIFIED(ptp_usb)) { |
| 6309 | prop = ptp_get_new_object_prop_entry(&props,&nrofprops); |
| 6310 | prop->ObjectHandle = filedata->item_id; |
| 6311 | prop->property = PTP_OPC_DateModified; |
| 6312 | prop->datatype = PTP_DTC_STR; |
| 6313 | prop->propval.str = get_iso8601_stamp(); |
Linus Walleij | d8fd9a5 | 2009-09-09 22:46:01 +0000 | [diff] [blame] | 6314 | filedata->modificationdate = time(NULL); |
Linus Walleij | cf8dc2b | 2008-10-21 13:58:36 +0000 | [diff] [blame] | 6315 | } |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 6316 | break; |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 6317 | } |
rreardon | bbb4e56 | 2006-11-19 22:16:11 +0000 | [diff] [blame] | 6318 | } |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 6319 | ptp_free_objectpropdesc(&opd); |
Linus Walleij | 99310d4 | 2006-11-01 08:29:39 +0000 | [diff] [blame] | 6320 | } |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 6321 | free(properties); |
mopoke | 3136444 | 2006-11-20 04:53:04 +0000 | [diff] [blame] | 6322 | |
rreardon | bbb4e56 | 2006-11-19 22:16:11 +0000 | [diff] [blame] | 6323 | ret = ptp_mtp_sendobjectproplist(params, &store, &localph, &filedata->item_id, |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 6324 | of, filedata->filesize, props, nrofprops); |
mopoke | 3136444 | 2006-11-20 04:53:04 +0000 | [diff] [blame] | 6325 | |
rreardon | bbb4e56 | 2006-11-19 22:16:11 +0000 | [diff] [blame] | 6326 | /* Free property list */ |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 6327 | ptp_destroy_object_prop_list(props, nrofprops); |
mopoke | 3136444 | 2006-11-20 04:53:04 +0000 | [diff] [blame] | 6328 | |
rreardon | bbb4e56 | 2006-11-19 22:16:11 +0000 | [diff] [blame] | 6329 | if (ret != PTP_RC_OK) { |
Linus Walleij | d8fd9a5 | 2009-09-09 22:46:01 +0000 | [diff] [blame] | 6330 | add_ptp_error_to_errorstack(device, ret, "send_file_object_info():" |
Linus Walleij | 0779577 | 2007-08-06 18:44:06 +0000 | [diff] [blame] | 6331 | "Could not send object property list."); |
rreardon | bbb4e56 | 2006-11-19 22:16:11 +0000 | [diff] [blame] | 6332 | if (ret == PTP_RC_AccessDenied) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 6333 | add_ptp_error_to_errorstack(device, ret, "ACCESS DENIED."); |
rreardon | bbb4e56 | 2006-11-19 22:16:11 +0000 | [diff] [blame] | 6334 | } |
| 6335 | return -1; |
| 6336 | } |
| 6337 | } else if (ptp_operation_issupported(params,PTP_OC_SendObjectInfo)) { |
Linus Walleij | 0779577 | 2007-08-06 18:44:06 +0000 | [diff] [blame] | 6338 | PTPObjectInfo new_file; |
| 6339 | |
| 6340 | memset(&new_file, 0, sizeof(PTPObjectInfo)); |
Linus Walleij | d8fd9a5 | 2009-09-09 22:46:01 +0000 | [diff] [blame] | 6341 | |
Linus Walleij | 0779577 | 2007-08-06 18:44:06 +0000 | [diff] [blame] | 6342 | new_file.Filename = filedata->filename; |
Linus Walleij | fec4d56 | 2008-06-01 22:30:36 +0000 | [diff] [blame] | 6343 | if (FLAG_ONLY_7BIT_FILENAMES(ptp_usb)) { |
Linus Walleij | d3b7857 | 2007-08-24 21:28:24 +0000 | [diff] [blame] | 6344 | strip_7bit_from_utf8(new_file.Filename); |
| 6345 | } |
Philip Langdale | abdd59d | 2013-04-05 14:00:28 -0700 | [diff] [blame] | 6346 | if (filedata->filesize > 0xFFFFFFFFL) { |
| 6347 | // This is a kludge in the MTP standard for large files. |
| 6348 | new_file.ObjectCompressedSize = (uint32_t) 0xFFFFFFFF; |
| 6349 | } else { |
| 6350 | new_file.ObjectCompressedSize = (uint32_t) filedata->filesize; |
| 6351 | } |
Linus Walleij | 0779577 | 2007-08-06 18:44:06 +0000 | [diff] [blame] | 6352 | new_file.ObjectFormat = of; |
| 6353 | new_file.StorageID = store; |
| 6354 | new_file.ParentObject = localph; |
Richard Low | 5f6fff8 | 2009-04-11 12:45:04 +0000 | [diff] [blame] | 6355 | new_file.ModificationDate = time(NULL); |
Linus Walleij | 0779577 | 2007-08-06 18:44:06 +0000 | [diff] [blame] | 6356 | |
Linus Walleij | 9be685b | 2006-11-21 09:44:53 +0000 | [diff] [blame] | 6357 | // Create the object |
| 6358 | ret = ptp_sendobjectinfo(params, &store, &localph, &filedata->item_id, &new_file); |
Linus Walleij | 0779577 | 2007-08-06 18:44:06 +0000 | [diff] [blame] | 6359 | |
Linus Walleij | 9be685b | 2006-11-21 09:44:53 +0000 | [diff] [blame] | 6360 | if (ret != PTP_RC_OK) { |
Richard Low | d3b1702 | 2009-04-11 12:37:39 +0000 | [diff] [blame] | 6361 | add_ptp_error_to_errorstack(device, ret, "send_file_object_info(): " |
Linus Walleij | 0779577 | 2007-08-06 18:44:06 +0000 | [diff] [blame] | 6362 | "Could not send object info."); |
Linus Walleij | 9be685b | 2006-11-21 09:44:53 +0000 | [diff] [blame] | 6363 | if (ret == PTP_RC_AccessDenied) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 6364 | add_ptp_error_to_errorstack(device, ret, "ACCESS DENIED."); |
Linus Walleij | 9be685b | 2006-11-21 09:44:53 +0000 | [diff] [blame] | 6365 | } |
| 6366 | return -1; |
| 6367 | } |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 6368 | // NOTE: the char* pointers inside new_file are not copies so don't |
| 6369 | // try to destroy this objectinfo! |
rreardon | bbb4e56 | 2006-11-19 22:16:11 +0000 | [diff] [blame] | 6370 | } |
Linus Walleij | d2778d1 | 2007-08-06 19:24:58 +0000 | [diff] [blame] | 6371 | |
| 6372 | // Now there IS an object with this parent handle. |
| 6373 | filedata->parent_id = localph; |
Linus Walleij | d8fd9a5 | 2009-09-09 22:46:01 +0000 | [diff] [blame] | 6374 | |
Linus Walleij | d208f9c | 2006-04-27 14:16:06 +0000 | [diff] [blame] | 6375 | return 0; |
| 6376 | } |
| 6377 | |
Linus Walleij | 17e39f7 | 2006-02-23 15:54:28 +0000 | [diff] [blame] | 6378 | /** |
Linus Walleij | 9036b33 | 2008-05-23 21:01:36 +0000 | [diff] [blame] | 6379 | * This function updates the MTP track object metadata on a |
| 6380 | * single file identified by an object ID. |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 6381 | * @param device a pointer to the device to update the track |
Linus Walleij | 95698cd | 2006-02-24 10:40:40 +0000 | [diff] [blame] | 6382 | * metadata on. |
Linus Walleij | 17e39f7 | 2006-02-23 15:54:28 +0000 | [diff] [blame] | 6383 | * @param metadata a track metadata set to be written to the file. |
| 6384 | * notice that the <code>track_id</code> field of the |
| 6385 | * metadata structure must be correct so that the |
Linus Walleij | a498273 | 2006-02-24 15:46:02 +0000 | [diff] [blame] | 6386 | * function can update the right file. If some properties |
| 6387 | * of this metadata are set to NULL (strings) or 0 |
| 6388 | * (numerical values) they will be discarded and the |
| 6389 | * track will not be tagged with these blank values. |
Richard Low | af20b5d | 2006-12-17 18:00:59 +0000 | [diff] [blame] | 6390 | * @return 0 on success, any other value means failure. If some |
| 6391 | * or all of the properties fail to update we will still |
| 6392 | * return success. On some devices (notably iRiver T30) |
| 6393 | * properties that exist cannot be updated. |
Linus Walleij | 17e39f7 | 2006-02-23 15:54:28 +0000 | [diff] [blame] | 6394 | */ |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 6395 | int LIBMTP_Update_Track_Metadata(LIBMTP_mtpdevice_t *device, |
Linus Walleij | 17e39f7 | 2006-02-23 15:54:28 +0000 | [diff] [blame] | 6396 | LIBMTP_track_t const * const metadata) |
| 6397 | { |
Linus Walleij | 17e39f7 | 2006-02-23 15:54:28 +0000 | [diff] [blame] | 6398 | uint16_t ret; |
Linus Walleij | 00cf064 | 2006-07-26 20:40:59 +0000 | [diff] [blame] | 6399 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | f9267e9 | 2007-10-15 21:07:37 +0000 | [diff] [blame] | 6400 | PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo; |
Linus Walleij | 00cf064 | 2006-07-26 20:40:59 +0000 | [diff] [blame] | 6401 | uint32_t i; |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 6402 | uint16_t *properties = NULL; |
Linus Walleij | 00cf064 | 2006-07-26 20:40:59 +0000 | [diff] [blame] | 6403 | uint32_t propcnt = 0; |
Linus Walleij | 17e39f7 | 2006-02-23 15:54:28 +0000 | [diff] [blame] | 6404 | |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 6405 | // First see which properties can be set on this file format and apply accordingly |
Linus Walleij | 00cf064 | 2006-07-26 20:40:59 +0000 | [diff] [blame] | 6406 | // i.e only try to update this metadata for object tags that exist on the current player. |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 6407 | ret = ptp_mtp_getobjectpropssupported(params, map_libmtp_type_to_ptp_type(metadata->filetype), &propcnt, &properties); |
Linus Walleij | 00cf064 | 2006-07-26 20:40:59 +0000 | [diff] [blame] | 6408 | if (ret != PTP_RC_OK) { |
| 6409 | // Just bail out for now, nothing is ever set. |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 6410 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): " |
| 6411 | "could not retrieve supported object properties."); |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 6412 | return -1; |
Richard Low | 15731fe | 2007-03-22 20:27:20 +0000 | [diff] [blame] | 6413 | } |
Linus Walleij | f9267e9 | 2007-10-15 21:07:37 +0000 | [diff] [blame] | 6414 | if (ptp_operation_issupported(params, PTP_OC_MTP_SetObjPropList) && |
Linus Walleij | fec4d56 | 2008-06-01 22:30:36 +0000 | [diff] [blame] | 6415 | !FLAG_BROKEN_SET_OBJECT_PROPLIST(ptp_usb)) { |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 6416 | MTPProperties *props = NULL; |
| 6417 | MTPProperties *prop = NULL; |
| 6418 | int nrofprops = 0; |
Linus Walleij | 08a5fe1 | 2009-09-08 21:28:58 +0000 | [diff] [blame] | 6419 | |
Richard Low | 15731fe | 2007-03-22 20:27:20 +0000 | [diff] [blame] | 6420 | for (i=0;i<propcnt;i++) { |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 6421 | PTPObjectPropDesc opd; |
Linus Walleij | 08a5fe1 | 2009-09-08 21:28:58 +0000 | [diff] [blame] | 6422 | |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 6423 | ret = ptp_mtp_getobjectpropdesc(params, properties[i], map_libmtp_type_to_ptp_type(metadata->filetype), &opd); |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 6424 | if (ret != PTP_RC_OK) { |
| 6425 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): " |
| 6426 | "could not get property description."); |
| 6427 | } else if (opd.GetSet) { |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 6428 | switch (properties[i]) { |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 6429 | case PTP_OPC_Name: |
| 6430 | if (metadata->title == NULL) |
| 6431 | break; |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 6432 | prop = ptp_get_new_object_prop_entry(&props, &nrofprops); |
Linus Walleij | 08a5fe1 | 2009-09-08 21:28:58 +0000 | [diff] [blame] | 6433 | prop->ObjectHandle = metadata->item_id; |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 6434 | prop->property = PTP_OPC_Name; |
| 6435 | prop->datatype = PTP_DTC_STR; |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 6436 | prop->propval.str = strdup(metadata->title); |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 6437 | break; |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 6438 | case PTP_OPC_AlbumName: |
| 6439 | if (metadata->album == NULL) |
| 6440 | break; |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 6441 | prop = ptp_get_new_object_prop_entry(&props, &nrofprops); |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 6442 | prop->ObjectHandle = metadata->item_id; |
| 6443 | prop->property = PTP_OPC_AlbumName; |
| 6444 | prop->datatype = PTP_DTC_STR; |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 6445 | prop->propval.str = strdup(metadata->album); |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 6446 | break; |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 6447 | case PTP_OPC_Artist: |
| 6448 | if (metadata->artist == NULL) |
| 6449 | break; |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 6450 | prop = ptp_get_new_object_prop_entry(&props, &nrofprops); |
Linus Walleij | 08a5fe1 | 2009-09-08 21:28:58 +0000 | [diff] [blame] | 6451 | prop->ObjectHandle = metadata->item_id; |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 6452 | prop->property = PTP_OPC_Artist; |
| 6453 | prop->datatype = PTP_DTC_STR; |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 6454 | prop->propval.str = strdup(metadata->artist); |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 6455 | break; |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 6456 | case PTP_OPC_Composer: |
| 6457 | if (metadata->composer == NULL) |
| 6458 | break; |
| 6459 | prop = ptp_get_new_object_prop_entry(&props, &nrofprops); |
| 6460 | prop->ObjectHandle = metadata->item_id; |
| 6461 | prop->property = PTP_OPC_Composer; |
| 6462 | prop->datatype = PTP_DTC_STR; |
| 6463 | prop->propval.str = strdup(metadata->composer); |
| 6464 | break; |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 6465 | case PTP_OPC_Genre: |
| 6466 | if (metadata->genre == NULL) |
| 6467 | break; |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 6468 | prop = ptp_get_new_object_prop_entry(&props, &nrofprops); |
Linus Walleij | 08a5fe1 | 2009-09-08 21:28:58 +0000 | [diff] [blame] | 6469 | prop->ObjectHandle = metadata->item_id; |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 6470 | prop->property = PTP_OPC_Genre; |
| 6471 | prop->datatype = PTP_DTC_STR; |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 6472 | prop->propval.str = strdup(metadata->genre); |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 6473 | break; |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 6474 | case PTP_OPC_Duration: |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 6475 | prop = ptp_get_new_object_prop_entry(&props, &nrofprops); |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 6476 | prop->ObjectHandle = metadata->item_id; |
| 6477 | prop->property = PTP_OPC_Duration; |
| 6478 | prop->datatype = PTP_DTC_UINT32; |
Linus Walleij | 2955956 | 2007-08-22 21:38:04 +0000 | [diff] [blame] | 6479 | prop->propval.u32 = adjust_u32(metadata->duration, &opd); |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 6480 | break; |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 6481 | case PTP_OPC_Track: |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 6482 | prop = ptp_get_new_object_prop_entry(&props, &nrofprops); |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 6483 | prop->ObjectHandle = metadata->item_id; |
| 6484 | prop->property = PTP_OPC_Track; |
| 6485 | prop->datatype = PTP_DTC_UINT16; |
Linus Walleij | 2955956 | 2007-08-22 21:38:04 +0000 | [diff] [blame] | 6486 | prop->propval.u16 = adjust_u16(metadata->tracknumber, &opd); |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 6487 | break; |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 6488 | case PTP_OPC_OriginalReleaseDate: |
| 6489 | if (metadata->date == NULL) |
| 6490 | break; |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 6491 | prop = ptp_get_new_object_prop_entry(&props, &nrofprops); |
Linus Walleij | 08a5fe1 | 2009-09-08 21:28:58 +0000 | [diff] [blame] | 6492 | prop->ObjectHandle = metadata->item_id; |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 6493 | prop->property = PTP_OPC_OriginalReleaseDate; |
| 6494 | prop->datatype = PTP_DTC_STR; |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 6495 | prop->propval.str = strdup(metadata->date); |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 6496 | break; |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 6497 | case PTP_OPC_SampleRate: |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 6498 | prop = ptp_get_new_object_prop_entry(&props, &nrofprops); |
Linus Walleij | 08a5fe1 | 2009-09-08 21:28:58 +0000 | [diff] [blame] | 6499 | prop->ObjectHandle = metadata->item_id; |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 6500 | prop->property = PTP_OPC_SampleRate; |
| 6501 | prop->datatype = PTP_DTC_UINT32; |
Linus Walleij | 2955956 | 2007-08-22 21:38:04 +0000 | [diff] [blame] | 6502 | prop->propval.u32 = adjust_u32(metadata->samplerate, &opd); |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 6503 | break; |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 6504 | case PTP_OPC_NumberOfChannels: |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 6505 | prop = ptp_get_new_object_prop_entry(&props, &nrofprops); |
Linus Walleij | 08a5fe1 | 2009-09-08 21:28:58 +0000 | [diff] [blame] | 6506 | prop->ObjectHandle = metadata->item_id; |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 6507 | prop->property = PTP_OPC_NumberOfChannels; |
| 6508 | prop->datatype = PTP_DTC_UINT16; |
Linus Walleij | 2955956 | 2007-08-22 21:38:04 +0000 | [diff] [blame] | 6509 | prop->propval.u16 = adjust_u16(metadata->nochannels, &opd); |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 6510 | break; |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 6511 | case PTP_OPC_AudioWAVECodec: |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 6512 | prop = ptp_get_new_object_prop_entry(&props, &nrofprops); |
Linus Walleij | 08a5fe1 | 2009-09-08 21:28:58 +0000 | [diff] [blame] | 6513 | prop->ObjectHandle = metadata->item_id; |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 6514 | prop->property = PTP_OPC_AudioWAVECodec; |
| 6515 | prop->datatype = PTP_DTC_UINT32; |
Linus Walleij | 2955956 | 2007-08-22 21:38:04 +0000 | [diff] [blame] | 6516 | prop->propval.u32 = adjust_u32(metadata->wavecodec, &opd); |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 6517 | break; |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 6518 | case PTP_OPC_AudioBitRate: |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 6519 | prop = ptp_get_new_object_prop_entry(&props, &nrofprops); |
Linus Walleij | 08a5fe1 | 2009-09-08 21:28:58 +0000 | [diff] [blame] | 6520 | prop->ObjectHandle = metadata->item_id; |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 6521 | prop->property = PTP_OPC_AudioBitRate; |
| 6522 | prop->datatype = PTP_DTC_UINT32; |
Linus Walleij | 2955956 | 2007-08-22 21:38:04 +0000 | [diff] [blame] | 6523 | prop->propval.u32 = adjust_u32(metadata->bitrate, &opd); |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 6524 | break; |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 6525 | case PTP_OPC_BitRateType: |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 6526 | prop = ptp_get_new_object_prop_entry(&props, &nrofprops); |
Linus Walleij | 08a5fe1 | 2009-09-08 21:28:58 +0000 | [diff] [blame] | 6527 | prop->ObjectHandle = metadata->item_id; |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 6528 | prop->property = PTP_OPC_BitRateType; |
| 6529 | prop->datatype = PTP_DTC_UINT16; |
Linus Walleij | 2955956 | 2007-08-22 21:38:04 +0000 | [diff] [blame] | 6530 | prop->propval.u16 = adjust_u16(metadata->bitratetype, &opd); |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 6531 | break; |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 6532 | case PTP_OPC_Rating: |
| 6533 | // TODO: shall this be set for rating 0? |
| 6534 | if (metadata->rating == 0) |
| 6535 | break; |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 6536 | prop = ptp_get_new_object_prop_entry(&props, &nrofprops); |
Linus Walleij | 08a5fe1 | 2009-09-08 21:28:58 +0000 | [diff] [blame] | 6537 | prop->ObjectHandle = metadata->item_id; |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 6538 | prop->property = PTP_OPC_Rating; |
| 6539 | prop->datatype = PTP_DTC_UINT16; |
Linus Walleij | 2955956 | 2007-08-22 21:38:04 +0000 | [diff] [blame] | 6540 | prop->propval.u16 = adjust_u16(metadata->rating, &opd); |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 6541 | break; |
| 6542 | case PTP_OPC_UseCount: |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 6543 | prop = ptp_get_new_object_prop_entry(&props, &nrofprops); |
Linus Walleij | 08a5fe1 | 2009-09-08 21:28:58 +0000 | [diff] [blame] | 6544 | prop->ObjectHandle = metadata->item_id; |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 6545 | prop->property = PTP_OPC_UseCount; |
| 6546 | prop->datatype = PTP_DTC_UINT32; |
Linus Walleij | 2955956 | 2007-08-22 21:38:04 +0000 | [diff] [blame] | 6547 | prop->propval.u32 = adjust_u32(metadata->usecount, &opd); |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 6548 | break; |
| 6549 | case PTP_OPC_DateModified: |
Linus Walleij | cf8dc2b | 2008-10-21 13:58:36 +0000 | [diff] [blame] | 6550 | if (!FLAG_CANNOT_HANDLE_DATEMODIFIED(ptp_usb)) { |
Linus Walleij | 3758814 | 2008-10-16 19:10:47 +0000 | [diff] [blame] | 6551 | // Tag with current time if that is supported |
| 6552 | prop = ptp_get_new_object_prop_entry(&props, &nrofprops); |
| 6553 | prop->ObjectHandle = metadata->item_id; |
| 6554 | prop->property = PTP_OPC_DateModified; |
| 6555 | prop->datatype = PTP_DTC_STR; |
| 6556 | prop->propval.str = get_iso8601_stamp(); |
| 6557 | } |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 6558 | break; |
Linus Walleij | f9267e9 | 2007-10-15 21:07:37 +0000 | [diff] [blame] | 6559 | default: |
| 6560 | break; |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 6561 | } |
Linus Walleij | 304433d | 2007-02-26 08:02:47 +0000 | [diff] [blame] | 6562 | } |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 6563 | ptp_free_objectpropdesc(&opd); |
Richard Low | 15731fe | 2007-03-22 20:27:20 +0000 | [diff] [blame] | 6564 | } |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 6565 | |
Richard Low | 15731fe | 2007-03-22 20:27:20 +0000 | [diff] [blame] | 6566 | // NOTE: File size is not updated, this should not change anyway. |
| 6567 | // neither will we change the filename. |
Linus Walleij | 08a5fe1 | 2009-09-08 21:28:58 +0000 | [diff] [blame] | 6568 | |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 6569 | ret = ptp_mtp_setobjectproplist(params, props, nrofprops); |
Linus Walleij | 08a5fe1 | 2009-09-08 21:28:58 +0000 | [diff] [blame] | 6570 | |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 6571 | ptp_destroy_object_prop_list(props, nrofprops); |
Linus Walleij | 08a5fe1 | 2009-09-08 21:28:58 +0000 | [diff] [blame] | 6572 | |
Richard Low | 15731fe | 2007-03-22 20:27:20 +0000 | [diff] [blame] | 6573 | if (ret != PTP_RC_OK) { |
| 6574 | // TODO: return error of which property we couldn't set |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 6575 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): " |
| 6576 | "could not set object property list."); |
Richard Low | 016e373 | 2007-09-23 14:53:46 +0000 | [diff] [blame] | 6577 | free(properties); |
Linus Walleij | 304433d | 2007-02-26 08:02:47 +0000 | [diff] [blame] | 6578 | return -1; |
| 6579 | } |
Linus Walleij | 08a5fe1 | 2009-09-08 21:28:58 +0000 | [diff] [blame] | 6580 | |
Richard Low | 15731fe | 2007-03-22 20:27:20 +0000 | [diff] [blame] | 6581 | } else if (ptp_operation_issupported(params,PTP_OC_MTP_SetObjectPropValue)) { |
Linus Walleij | 00cf064 | 2006-07-26 20:40:59 +0000 | [diff] [blame] | 6582 | for (i=0;i<propcnt;i++) { |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 6583 | PTPObjectPropDesc opd; |
Linus Walleij | 08a5fe1 | 2009-09-08 21:28:58 +0000 | [diff] [blame] | 6584 | |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 6585 | ret = ptp_mtp_getobjectpropdesc(params, properties[i], map_libmtp_type_to_ptp_type(metadata->filetype), &opd); |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 6586 | if (ret != PTP_RC_OK) { |
| 6587 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): " |
| 6588 | "could not get property description."); |
| 6589 | } else if (opd.GetSet) { |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 6590 | switch (properties[i]) { |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 6591 | case PTP_OPC_Name: |
| 6592 | // Update title |
| 6593 | ret = set_object_string(device, metadata->item_id, PTP_OPC_Name, metadata->title); |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 6594 | if (ret != 0) { |
| 6595 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): " |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 6596 | "could not set track title."); |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 6597 | } |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 6598 | break; |
| 6599 | case PTP_OPC_AlbumName: |
| 6600 | // Update album |
| 6601 | ret = set_object_string(device, metadata->item_id, PTP_OPC_AlbumName, metadata->album); |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 6602 | if (ret != 0) { |
| 6603 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): " |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 6604 | "could not set track album name."); |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 6605 | } |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 6606 | break; |
| 6607 | case PTP_OPC_Artist: |
| 6608 | // Update artist |
| 6609 | ret = set_object_string(device, metadata->item_id, PTP_OPC_Artist, metadata->artist); |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 6610 | if (ret != 0) { |
| 6611 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): " |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 6612 | "could not set track artist name."); |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 6613 | } |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 6614 | break; |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 6615 | case PTP_OPC_Composer: |
| 6616 | // Update composer |
| 6617 | ret = set_object_string(device, metadata->item_id, PTP_OPC_Composer, metadata->composer); |
| 6618 | if (ret != 0) { |
| 6619 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): " |
| 6620 | "could not set track composer name."); |
| 6621 | } |
| 6622 | break; |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 6623 | case PTP_OPC_Genre: |
Linus Walleij | b7e8f97 | 2010-01-19 00:38:11 +0000 | [diff] [blame] | 6624 | // Update genre (but only if valid) |
| 6625 | if (metadata->genre) { |
| 6626 | ret = set_object_string(device, metadata->item_id, PTP_OPC_Genre, metadata->genre); |
| 6627 | if (ret != 0) { |
| 6628 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "update_abstract_list(): " |
| 6629 | "could not set genre."); |
| 6630 | } |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 6631 | } |
| 6632 | break; |
| 6633 | case PTP_OPC_Duration: |
| 6634 | // Update duration |
| 6635 | if (metadata->duration != 0) { |
Linus Walleij | 2955956 | 2007-08-22 21:38:04 +0000 | [diff] [blame] | 6636 | ret = set_object_u32(device, metadata->item_id, PTP_OPC_Duration, adjust_u32(metadata->duration, &opd)); |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 6637 | if (ret != 0) { |
| 6638 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): " |
| 6639 | "could not set track duration."); |
| 6640 | } |
| 6641 | } |
| 6642 | break; |
| 6643 | case PTP_OPC_Track: |
| 6644 | // Update track number. |
| 6645 | if (metadata->tracknumber != 0) { |
Linus Walleij | 2955956 | 2007-08-22 21:38:04 +0000 | [diff] [blame] | 6646 | ret = set_object_u16(device, metadata->item_id, PTP_OPC_Track, adjust_u16(metadata->tracknumber, &opd)); |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 6647 | if (ret != 0) { |
| 6648 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): " |
| 6649 | "could not set track tracknumber."); |
| 6650 | } |
| 6651 | } |
| 6652 | break; |
| 6653 | case PTP_OPC_OriginalReleaseDate: |
| 6654 | // Update creation datetime |
Linus Walleij | b7e8f97 | 2010-01-19 00:38:11 +0000 | [diff] [blame] | 6655 | // The date can be zero, but some devices do not support setting zero |
| 6656 | // dates (and it seems that a zero date should never be set anyway) |
| 6657 | if (metadata->date) { |
| 6658 | ret = set_object_string(device, metadata->item_id, PTP_OPC_OriginalReleaseDate, metadata->date); |
| 6659 | if (ret != 0) { |
| 6660 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): " |
| 6661 | "could not set track release date."); |
| 6662 | } |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 6663 | } |
| 6664 | break; |
| 6665 | // These are, well not so important. |
| 6666 | case PTP_OPC_SampleRate: |
| 6667 | // Update sample rate |
| 6668 | if (metadata->samplerate != 0) { |
Linus Walleij | 2955956 | 2007-08-22 21:38:04 +0000 | [diff] [blame] | 6669 | ret = set_object_u32(device, metadata->item_id, PTP_OPC_SampleRate, adjust_u32(metadata->samplerate, &opd)); |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 6670 | if (ret != 0) { |
| 6671 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): " |
| 6672 | "could not set samplerate."); |
| 6673 | } |
| 6674 | } |
| 6675 | break; |
| 6676 | case PTP_OPC_NumberOfChannels: |
| 6677 | // Update number of channels |
| 6678 | if (metadata->nochannels != 0) { |
Linus Walleij | 2955956 | 2007-08-22 21:38:04 +0000 | [diff] [blame] | 6679 | ret = set_object_u16(device, metadata->item_id, PTP_OPC_NumberOfChannels, adjust_u16(metadata->nochannels, &opd)); |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 6680 | if (ret != 0) { |
| 6681 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): " |
| 6682 | "could not set number of channels."); |
| 6683 | } |
| 6684 | } |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 6685 | break; |
| 6686 | case PTP_OPC_AudioWAVECodec: |
| 6687 | // Update WAVE codec |
| 6688 | if (metadata->wavecodec != 0) { |
Linus Walleij | 2955956 | 2007-08-22 21:38:04 +0000 | [diff] [blame] | 6689 | ret = set_object_u32(device, metadata->item_id, PTP_OPC_AudioWAVECodec, adjust_u32(metadata->wavecodec, &opd)); |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 6690 | if (ret != 0) { |
| 6691 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): " |
| 6692 | "could not set WAVE codec."); |
| 6693 | } |
| 6694 | } |
| 6695 | break; |
| 6696 | case PTP_OPC_AudioBitRate: |
| 6697 | // Update bitrate |
| 6698 | if (metadata->bitrate != 0) { |
Linus Walleij | 2955956 | 2007-08-22 21:38:04 +0000 | [diff] [blame] | 6699 | ret = set_object_u32(device, metadata->item_id, PTP_OPC_AudioBitRate, adjust_u32(metadata->bitrate, &opd)); |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 6700 | if (ret != 0) { |
| 6701 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): " |
| 6702 | "could not set bitrate."); |
| 6703 | } |
| 6704 | } |
| 6705 | break; |
| 6706 | case PTP_OPC_BitRateType: |
| 6707 | // Update bitrate type |
| 6708 | if (metadata->bitratetype != 0) { |
Linus Walleij | 2955956 | 2007-08-22 21:38:04 +0000 | [diff] [blame] | 6709 | ret = set_object_u16(device, metadata->item_id, PTP_OPC_BitRateType, adjust_u16(metadata->bitratetype, &opd)); |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 6710 | if (ret != 0) { |
| 6711 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): " |
| 6712 | "could not set bitratetype."); |
| 6713 | } |
| 6714 | } |
| 6715 | break; |
| 6716 | case PTP_OPC_Rating: |
| 6717 | // Update user rating |
| 6718 | // TODO: shall this be set for rating 0? |
| 6719 | if (metadata->rating != 0) { |
Linus Walleij | 2955956 | 2007-08-22 21:38:04 +0000 | [diff] [blame] | 6720 | ret = set_object_u16(device, metadata->item_id, PTP_OPC_Rating, adjust_u16(metadata->rating, &opd)); |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 6721 | if (ret != 0) { |
| 6722 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): " |
| 6723 | "could not set user rating."); |
| 6724 | } |
| 6725 | } |
| 6726 | break; |
| 6727 | case PTP_OPC_UseCount: |
| 6728 | // Update use count, set even to zero if desired. |
Linus Walleij | 2955956 | 2007-08-22 21:38:04 +0000 | [diff] [blame] | 6729 | ret = set_object_u32(device, metadata->item_id, PTP_OPC_UseCount, adjust_u32(metadata->usecount, &opd)); |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 6730 | if (ret != 0) { |
| 6731 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): " |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 6732 | "could not set use count."); |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 6733 | } |
| 6734 | break; |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 6735 | case PTP_OPC_DateModified: |
Linus Walleij | cf8dc2b | 2008-10-21 13:58:36 +0000 | [diff] [blame] | 6736 | if (!FLAG_CANNOT_HANDLE_DATEMODIFIED(ptp_usb)) { |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 6737 | // Update modification time if supported |
| 6738 | char *tmpstamp = get_iso8601_stamp(); |
| 6739 | ret = set_object_string(device, metadata->item_id, PTP_OPC_DateModified, tmpstamp); |
| 6740 | if (ret != 0) { |
| 6741 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): " |
| 6742 | "could not set modification date."); |
| 6743 | } |
| 6744 | free(tmpstamp); |
| 6745 | } |
Linus Walleij | 08a5fe1 | 2009-09-08 21:28:58 +0000 | [diff] [blame] | 6746 | break; |
| 6747 | |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 6748 | // NOTE: File size is not updated, this should not change anyway. |
| 6749 | // neither will we change the filename. |
| 6750 | default: |
| 6751 | break; |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 6752 | } |
Linus Walleij | 00cf064 | 2006-07-26 20:40:59 +0000 | [diff] [blame] | 6753 | } |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 6754 | ptp_free_objectpropdesc(&opd); |
Linus Walleij | a498273 | 2006-02-24 15:46:02 +0000 | [diff] [blame] | 6755 | } |
Richard Low | 15731fe | 2007-03-22 20:27:20 +0000 | [diff] [blame] | 6756 | } else { |
| 6757 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): " |
| 6758 | "Your device doesn't seem to support any known way of setting metadata."); |
Richard Low | 016e373 | 2007-09-23 14:53:46 +0000 | [diff] [blame] | 6759 | free(properties); |
Richard Low | 15731fe | 2007-03-22 20:27:20 +0000 | [diff] [blame] | 6760 | return -1; |
Linus Walleij | 17e39f7 | 2006-02-23 15:54:28 +0000 | [diff] [blame] | 6761 | } |
Linus Walleij | 08a5fe1 | 2009-09-08 21:28:58 +0000 | [diff] [blame] | 6762 | |
tsaarni | a3eb60a | 2007-07-06 17:41:30 +0000 | [diff] [blame] | 6763 | // update cached object properties if metadata cache exists |
Linus Walleij | 7752b95 | 2007-10-19 20:11:46 +0000 | [diff] [blame] | 6764 | update_metadata_cache(device, metadata->item_id); |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 6765 | |
Richard Low | 016e373 | 2007-09-23 14:53:46 +0000 | [diff] [blame] | 6766 | free(properties); |
Linus Walleij | 08a5fe1 | 2009-09-08 21:28:58 +0000 | [diff] [blame] | 6767 | |
Linus Walleij | f1b02f2 | 2006-12-06 09:03:23 +0000 | [diff] [blame] | 6768 | return 0; |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 6769 | } |
Linus Walleij | 95698cd | 2006-02-24 10:40:40 +0000 | [diff] [blame] | 6770 | |
| 6771 | /** |
Linus Walleij | 399807b | 2008-01-27 22:05:12 +0000 | [diff] [blame] | 6772 | * This function deletes a single file, track, playlist, folder or |
| 6773 | * any other object off the MTP device, identified by the object ID. |
| 6774 | * |
| 6775 | * If you delete a folder, there is no guarantee that the device will |
| 6776 | * really delete all the files that were in that folder, rather it is |
| 6777 | * expected that they will not be deleted, and will turn up in object |
| 6778 | * listings with parent set to a non-existant object ID. The safe way |
Linus Walleij | 08a5fe1 | 2009-09-08 21:28:58 +0000 | [diff] [blame] | 6779 | * to do this is to recursively delete all files (and folders) contained |
Linus Walleij | 399807b | 2008-01-27 22:05:12 +0000 | [diff] [blame] | 6780 | * in the folder, then the folder itself. |
| 6781 | * |
| 6782 | * @param device a pointer to the device to delete the object from. |
| 6783 | * @param object_id the object to delete. |
Linus Walleij | 95698cd | 2006-02-24 10:40:40 +0000 | [diff] [blame] | 6784 | * @return 0 on success, any other value means failure. |
| 6785 | */ |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 6786 | int LIBMTP_Delete_Object(LIBMTP_mtpdevice_t *device, |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 6787 | uint32_t object_id) |
Linus Walleij | 95698cd | 2006-02-24 10:40:40 +0000 | [diff] [blame] | 6788 | { |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 6789 | uint16_t ret; |
| 6790 | PTPParams *params = (PTPParams *) device->params; |
| 6791 | |
| 6792 | ret = ptp_deleteobject(params, object_id, 0); |
| 6793 | if (ret != PTP_RC_OK) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 6794 | add_ptp_error_to_errorstack(device, ret, "LIBMTP_Delete_Object(): could not delete object."); |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 6795 | return -1; |
| 6796 | } |
tsaarni | a3eb60a | 2007-07-06 17:41:30 +0000 | [diff] [blame] | 6797 | |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 6798 | return 0; |
Linus Walleij | 95698cd | 2006-02-24 10:40:40 +0000 | [diff] [blame] | 6799 | } |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 6800 | |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 6801 | /** |
Philip Langdale | 1680c39 | 2018-03-17 09:23:25 +0100 | [diff] [blame] | 6802 | * The function moves an object from one location on a device to another |
| 6803 | * location. |
| 6804 | * |
| 6805 | * The semantics of moving a folder are not defined in the spec, but it |
| 6806 | * appears to do the right thing when tested (but devices that implement |
| 6807 | * this operation are rare). |
| 6808 | * |
| 6809 | * Note that moving an object may take a significant amount of time, |
| 6810 | * particularly if being moved between storages. MTP does not provide |
| 6811 | * any kind of progress mechanism, so the operation will simply block |
| 6812 | * for the duration. |
| 6813 | * |
| 6814 | * @param device a pointer to the device where the object exists. |
| 6815 | * @param object_id the object to move. |
| 6816 | * @param storage_id the id of the destination storage. |
| 6817 | * @param parent_id the id of the destination parent object (folder). |
| 6818 | * If the destination is the root of the storage, pass '0'. |
| 6819 | * @return 0 on success, any other value means failure. |
| 6820 | */ |
| 6821 | int LIBMTP_Move_Object(LIBMTP_mtpdevice_t *device, |
| 6822 | uint32_t object_id, |
| 6823 | uint32_t storage_id, |
| 6824 | uint32_t parent_id) |
| 6825 | { |
| 6826 | uint16_t ret; |
| 6827 | PTPParams *params = (PTPParams *) device->params; |
| 6828 | |
| 6829 | ret = ptp_moveobject(params, object_id, storage_id, parent_id); |
| 6830 | if (ret != PTP_RC_OK) { |
| 6831 | add_ptp_error_to_errorstack(device, ret, "LIBMTP_Move_Object(): could not move object."); |
| 6832 | return -1; |
| 6833 | } |
| 6834 | |
| 6835 | return 0; |
| 6836 | } |
| 6837 | |
| 6838 | /** |
| 6839 | * The function copies an object from one location on a device to another |
| 6840 | * location. |
| 6841 | * |
| 6842 | * The semantics of copying a folder are not defined in the spec, but it |
| 6843 | * appears to do the right thing when tested (but devices that implement |
| 6844 | * this operation are rare). |
| 6845 | * |
| 6846 | * Note that copying an object may take a significant amount of time. |
| 6847 | * MTP does not provide any kind of progress mechanism, so the operation |
| 6848 | * will simply block for the duration. |
| 6849 | * |
| 6850 | * @param device a pointer to the device where the object exists. |
| 6851 | * @param object_id the object to copy. |
| 6852 | * @param storage_id the id of the destination storage. |
| 6853 | * @param parent_id the id of the destination parent object (folder). |
| 6854 | * If the destination is the root of the storage, pass '0'. |
| 6855 | * @return 0 on success, any other value means failure. |
| 6856 | */ |
| 6857 | int LIBMTP_Copy_Object(LIBMTP_mtpdevice_t *device, |
| 6858 | uint32_t object_id, |
| 6859 | uint32_t storage_id, |
| 6860 | uint32_t parent_id) |
| 6861 | { |
| 6862 | uint16_t ret; |
| 6863 | PTPParams *params = (PTPParams *) device->params; |
| 6864 | |
| 6865 | ret = ptp_copyobject(params, object_id, storage_id, parent_id); |
| 6866 | if (ret != PTP_RC_OK) { |
| 6867 | add_ptp_error_to_errorstack(device, ret, "LIBMTP_Copy_Object(): could not copy object."); |
| 6868 | return -1; |
| 6869 | } |
| 6870 | |
| 6871 | return 0; |
| 6872 | } |
| 6873 | |
| 6874 | /** |
Linus Walleij | 8d8c435 | 2008-09-23 23:04:16 +0000 | [diff] [blame] | 6875 | * Internal function to update an object filename property. |
Linus Walleij | de8193f | 2008-01-27 14:55:31 +0000 | [diff] [blame] | 6876 | */ |
Linus Walleij | 8d8c435 | 2008-09-23 23:04:16 +0000 | [diff] [blame] | 6877 | static int set_object_filename(LIBMTP_mtpdevice_t *device, |
| 6878 | uint32_t object_id, uint16_t ptp_type, |
| 6879 | const char **newname_ptr) |
Linus Walleij | de8193f | 2008-01-27 14:55:31 +0000 | [diff] [blame] | 6880 | { |
| 6881 | PTPParams *params = (PTPParams *) device->params; |
| 6882 | PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo; |
| 6883 | PTPObjectPropDesc opd; |
| 6884 | uint16_t ret; |
Linus Walleij | 8d8c435 | 2008-09-23 23:04:16 +0000 | [diff] [blame] | 6885 | char *newname; |
Linus Walleij | 8be2c03 | 2008-01-28 21:23:47 +0000 | [diff] [blame] | 6886 | |
| 6887 | // See if we can modify the filename on this kind of files. |
| 6888 | ret = ptp_mtp_getobjectpropdesc(params, PTP_OPC_ObjectFileName, ptp_type, &opd); |
Linus Walleij | de8193f | 2008-01-27 14:55:31 +0000 | [diff] [blame] | 6889 | if (ret != PTP_RC_OK) { |
Linus Walleij | 8d8c435 | 2008-09-23 23:04:16 +0000 | [diff] [blame] | 6890 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "set_object_filename(): " |
Linus Walleij | 8be2c03 | 2008-01-28 21:23:47 +0000 | [diff] [blame] | 6891 | "could not get property description."); |
Linus Walleij | de8193f | 2008-01-27 14:55:31 +0000 | [diff] [blame] | 6892 | return -1; |
| 6893 | } |
| 6894 | |
| 6895 | if (!opd.GetSet) { |
| 6896 | ptp_free_objectpropdesc(&opd); |
Linus Walleij | 8d8c435 | 2008-09-23 23:04:16 +0000 | [diff] [blame] | 6897 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "set_object_filename(): " |
| 6898 | " property is not settable."); |
Linus Walleij | 399807b | 2008-01-27 22:05:12 +0000 | [diff] [blame] | 6899 | // TODO: we COULD actually upload/download the object here, if we feel |
| 6900 | // like wasting time for the user. |
Linus Walleij | de8193f | 2008-01-27 14:55:31 +0000 | [diff] [blame] | 6901 | return -1; |
| 6902 | } |
| 6903 | |
Linus Walleij | 8d8c435 | 2008-09-23 23:04:16 +0000 | [diff] [blame] | 6904 | newname = strdup(*newname_ptr); |
| 6905 | |
Linus Walleij | fec4d56 | 2008-06-01 22:30:36 +0000 | [diff] [blame] | 6906 | if (FLAG_ONLY_7BIT_FILENAMES(ptp_usb)) { |
Linus Walleij | de8193f | 2008-01-27 14:55:31 +0000 | [diff] [blame] | 6907 | strip_7bit_from_utf8(newname); |
| 6908 | } |
| 6909 | |
| 6910 | if (ptp_operation_issupported(params, PTP_OC_MTP_SetObjPropList) && |
Linus Walleij | fec4d56 | 2008-06-01 22:30:36 +0000 | [diff] [blame] | 6911 | !FLAG_BROKEN_SET_OBJECT_PROPLIST(ptp_usb)) { |
Linus Walleij | de8193f | 2008-01-27 14:55:31 +0000 | [diff] [blame] | 6912 | MTPProperties *props = NULL; |
| 6913 | MTPProperties *prop = NULL; |
| 6914 | int nrofprops = 0; |
Linus Walleij | 8d8c435 | 2008-09-23 23:04:16 +0000 | [diff] [blame] | 6915 | |
Linus Walleij | de8193f | 2008-01-27 14:55:31 +0000 | [diff] [blame] | 6916 | prop = ptp_get_new_object_prop_entry(&props, &nrofprops); |
Linus Walleij | 08a5fe1 | 2009-09-08 21:28:58 +0000 | [diff] [blame] | 6917 | prop->ObjectHandle = object_id; |
Linus Walleij | de8193f | 2008-01-27 14:55:31 +0000 | [diff] [blame] | 6918 | prop->property = PTP_OPC_ObjectFileName; |
| 6919 | prop->datatype = PTP_DTC_STR; |
Linus Walleij | 8d8c435 | 2008-09-23 23:04:16 +0000 | [diff] [blame] | 6920 | prop->propval.str = newname; |
| 6921 | |
Linus Walleij | de8193f | 2008-01-27 14:55:31 +0000 | [diff] [blame] | 6922 | ret = ptp_mtp_setobjectproplist(params, props, nrofprops); |
Linus Walleij | 8d8c435 | 2008-09-23 23:04:16 +0000 | [diff] [blame] | 6923 | |
Linus Walleij | de8193f | 2008-01-27 14:55:31 +0000 | [diff] [blame] | 6924 | ptp_destroy_object_prop_list(props, nrofprops); |
Linus Walleij | 8d8c435 | 2008-09-23 23:04:16 +0000 | [diff] [blame] | 6925 | |
Linus Walleij | de8193f | 2008-01-27 14:55:31 +0000 | [diff] [blame] | 6926 | if (ret != PTP_RC_OK) { |
Linus Walleij | 8d8c435 | 2008-09-23 23:04:16 +0000 | [diff] [blame] | 6927 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "set_object_filename(): " |
| 6928 | " could not set object property list."); |
| 6929 | ptp_free_objectpropdesc(&opd); |
| 6930 | return -1; |
Linus Walleij | de8193f | 2008-01-27 14:55:31 +0000 | [diff] [blame] | 6931 | } |
| 6932 | } else if (ptp_operation_issupported(params, PTP_OC_MTP_SetObjectPropValue)) { |
| 6933 | ret = set_object_string(device, object_id, PTP_OPC_ObjectFileName, newname); |
| 6934 | if (ret != 0) { |
Linus Walleij | 8d8c435 | 2008-09-23 23:04:16 +0000 | [diff] [blame] | 6935 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "set_object_filename(): " |
| 6936 | " could not set object filename."); |
Linus Walleij | de8193f | 2008-01-27 14:55:31 +0000 | [diff] [blame] | 6937 | ptp_free_objectpropdesc(&opd); |
| 6938 | return -1; |
| 6939 | } |
| 6940 | } else { |
Linus Walleij | 8d8c435 | 2008-09-23 23:04:16 +0000 | [diff] [blame] | 6941 | free(newname); |
| 6942 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "set_object_filename(): " |
| 6943 | " your device doesn't seem to support any known way of setting metadata."); |
Linus Walleij | de8193f | 2008-01-27 14:55:31 +0000 | [diff] [blame] | 6944 | ptp_free_objectpropdesc(&opd); |
| 6945 | return -1; |
| 6946 | } |
Linus Walleij | 8d8c435 | 2008-09-23 23:04:16 +0000 | [diff] [blame] | 6947 | |
Linus Walleij | de8193f | 2008-01-27 14:55:31 +0000 | [diff] [blame] | 6948 | ptp_free_objectpropdesc(&opd); |
Linus Walleij | 8d8c435 | 2008-09-23 23:04:16 +0000 | [diff] [blame] | 6949 | |
Linus Walleij | de8193f | 2008-01-27 14:55:31 +0000 | [diff] [blame] | 6950 | // update cached object properties if metadata cache exists |
| 6951 | update_metadata_cache(device, object_id); |
Linus Walleij | 8d8c435 | 2008-09-23 23:04:16 +0000 | [diff] [blame] | 6952 | |
Linus Walleij | de8193f | 2008-01-27 14:55:31 +0000 | [diff] [blame] | 6953 | return 0; |
| 6954 | } |
Linus Walleij | 399807b | 2008-01-27 22:05:12 +0000 | [diff] [blame] | 6955 | |
Linus Walleij | de8193f | 2008-01-27 14:55:31 +0000 | [diff] [blame] | 6956 | /** |
Linus Walleij | 8d8c435 | 2008-09-23 23:04:16 +0000 | [diff] [blame] | 6957 | * This function renames a single file. |
| 6958 | * This simply means that the PTP_OPC_ObjectFileName property |
| 6959 | * is updated, if this is supported by the device. |
| 6960 | * |
| 6961 | * @param device a pointer to the device that contains the file. |
| 6962 | * @param file the file metadata of the file to rename. |
| 6963 | * On success, the filename member is updated. Be aware, that |
| 6964 | * this name can be different than newname depending of device restrictions. |
| 6965 | * @param newname the new filename for this object. |
| 6966 | * @return 0 on success, any other value means failure. |
| 6967 | */ |
| 6968 | int LIBMTP_Set_File_Name(LIBMTP_mtpdevice_t *device, |
| 6969 | LIBMTP_file_t *file, const char *newname) |
| 6970 | { |
| 6971 | int ret; |
| 6972 | |
| 6973 | ret = set_object_filename(device, file->item_id, |
| 6974 | map_libmtp_type_to_ptp_type(file->filetype), |
| 6975 | &newname); |
| 6976 | |
| 6977 | if (ret != 0) { |
| 6978 | return ret; |
| 6979 | } |
| 6980 | |
| 6981 | free(file->filename); |
| 6982 | file->filename = strdup(newname); |
| 6983 | return ret; |
| 6984 | } |
| 6985 | |
| 6986 | /** |
| 6987 | * This function renames a single folder. |
| 6988 | * This simply means that the PTP_OPC_ObjectFileName property |
| 6989 | * is updated, if this is supported by the device. |
| 6990 | * |
| 6991 | * @param device a pointer to the device that contains the file. |
| 6992 | * @param folder the folder metadata of the folder to rename. |
| 6993 | * On success, the name member is updated. Be aware, that |
| 6994 | * this name can be different than newname depending of device restrictions. |
| 6995 | * @param newname the new name for this object. |
| 6996 | * @return 0 on success, any other value means failure. |
| 6997 | */ |
| 6998 | int LIBMTP_Set_Folder_Name(LIBMTP_mtpdevice_t *device, |
| 6999 | LIBMTP_folder_t *folder, const char* newname) |
| 7000 | { |
| 7001 | int ret; |
| 7002 | |
| 7003 | ret = set_object_filename(device, folder->folder_id, |
Linus Walleij | fb7212f | 2008-09-24 20:30:13 +0000 | [diff] [blame] | 7004 | PTP_OFC_Association, |
Linus Walleij | 8d8c435 | 2008-09-23 23:04:16 +0000 | [diff] [blame] | 7005 | &newname); |
| 7006 | |
| 7007 | if (ret != 0) { |
| 7008 | return ret; |
| 7009 | } |
| 7010 | |
| 7011 | free(folder->name); |
| 7012 | folder->name = strdup(newname); |
| 7013 | return ret; |
| 7014 | } |
| 7015 | |
| 7016 | /** |
| 7017 | * This function renames a single track. |
| 7018 | * This simply means that the PTP_OPC_ObjectFileName property |
| 7019 | * is updated, if this is supported by the device. |
| 7020 | * |
| 7021 | * @param device a pointer to the device that contains the file. |
| 7022 | * @param track the track metadata of the track to rename. |
| 7023 | * On success, the filename member is updated. Be aware, that |
| 7024 | * this name can be different than newname depending of device restrictions. |
| 7025 | * @param newname the new filename for this object. |
| 7026 | * @return 0 on success, any other value means failure. |
| 7027 | */ |
| 7028 | int LIBMTP_Set_Track_Name(LIBMTP_mtpdevice_t *device, |
| 7029 | LIBMTP_track_t *track, const char* newname) |
| 7030 | { |
| 7031 | int ret; |
| 7032 | |
| 7033 | ret = set_object_filename(device, track->item_id, |
| 7034 | map_libmtp_type_to_ptp_type(track->filetype), |
| 7035 | &newname); |
| 7036 | |
| 7037 | if (ret != 0) { |
| 7038 | return ret; |
| 7039 | } |
| 7040 | |
| 7041 | free(track->filename); |
| 7042 | track->filename = strdup(newname); |
| 7043 | return ret; |
| 7044 | } |
| 7045 | |
| 7046 | /** |
Linus Walleij | 12bbf31 | 2009-01-26 21:46:51 +0000 | [diff] [blame] | 7047 | * This function renames a single playlist object file holder. |
| 7048 | * This simply means that the <code>PTP_OPC_ObjectFileName</code> |
| 7049 | * property is updated, if this is supported by the device. |
| 7050 | * The playlist filename should nominally end with an extension |
| 7051 | * like ".pla". |
| 7052 | * |
| 7053 | * NOTE: if you want to change the metadata the device display |
| 7054 | * about a playlist you must <i>not</i> use this function, |
| 7055 | * use <code>LIBMTP_Update_Playlist()</code> instead! |
Linus Walleij | 8d8c435 | 2008-09-23 23:04:16 +0000 | [diff] [blame] | 7056 | * |
| 7057 | * @param device a pointer to the device that contains the file. |
| 7058 | * @param playlist the playlist metadata of the playlist to rename. |
| 7059 | * On success, the name member is updated. Be aware, that |
| 7060 | * this name can be different than newname depending of device restrictions. |
| 7061 | * @param newname the new name for this object. |
| 7062 | * @return 0 on success, any other value means failure. |
Linus Walleij | 12bbf31 | 2009-01-26 21:46:51 +0000 | [diff] [blame] | 7063 | * @see LIBMTP_Update_Playlist() |
Linus Walleij | 8d8c435 | 2008-09-23 23:04:16 +0000 | [diff] [blame] | 7064 | */ |
| 7065 | int LIBMTP_Set_Playlist_Name(LIBMTP_mtpdevice_t *device, |
| 7066 | LIBMTP_playlist_t *playlist, const char* newname) |
| 7067 | { |
| 7068 | int ret; |
| 7069 | |
| 7070 | ret = set_object_filename(device, playlist->playlist_id, |
| 7071 | PTP_OFC_MTP_AbstractAudioVideoPlaylist, |
| 7072 | &newname); |
| 7073 | |
| 7074 | if (ret != 0) { |
| 7075 | return ret; |
| 7076 | } |
| 7077 | |
| 7078 | free(playlist->name); |
| 7079 | playlist->name = strdup(newname); |
| 7080 | return ret; |
| 7081 | } |
| 7082 | |
| 7083 | /** |
| 7084 | * This function renames a single album. |
Linus Walleij | 12bbf31 | 2009-01-26 21:46:51 +0000 | [diff] [blame] | 7085 | * This simply means that the <code>PTP_OPC_ObjectFileName</code> |
| 7086 | * property is updated, if this is supported by the device. |
| 7087 | * The album filename should nominally end with an extension |
| 7088 | * like ".alb". |
| 7089 | * |
| 7090 | * NOTE: if you want to change the metadata the device display |
| 7091 | * about a playlist you must <i>not</i> use this function, |
| 7092 | * use <code>LIBMTP_Update_Album()</code> instead! |
Linus Walleij | 8d8c435 | 2008-09-23 23:04:16 +0000 | [diff] [blame] | 7093 | * |
| 7094 | * @param device a pointer to the device that contains the file. |
| 7095 | * @param album the album metadata of the album to rename. |
| 7096 | * On success, the name member is updated. Be aware, that |
| 7097 | * this name can be different than newname depending of device restrictions. |
| 7098 | * @param newname the new name for this object. |
| 7099 | * @return 0 on success, any other value means failure. |
Linus Walleij | 12bbf31 | 2009-01-26 21:46:51 +0000 | [diff] [blame] | 7100 | * @see LIBMTP_Update_Album() |
Linus Walleij | 8d8c435 | 2008-09-23 23:04:16 +0000 | [diff] [blame] | 7101 | */ |
| 7102 | int LIBMTP_Set_Album_Name(LIBMTP_mtpdevice_t *device, |
| 7103 | LIBMTP_album_t *album, const char* newname) |
| 7104 | { |
| 7105 | int ret; |
| 7106 | |
| 7107 | ret = set_object_filename(device, album->album_id, |
| 7108 | PTP_OFC_MTP_AbstractAudioAlbum, |
| 7109 | &newname); |
| 7110 | |
| 7111 | if (ret != 0) { |
| 7112 | return ret; |
| 7113 | } |
| 7114 | |
| 7115 | free(album->name); |
| 7116 | album->name = strdup(newname); |
| 7117 | return ret; |
| 7118 | } |
| 7119 | |
| 7120 | /** |
| 7121 | * THIS FUNCTION IS DEPRECATED. PLEASE UPDATE YOUR CODE IN ORDER |
| 7122 | * NOT TO USE IT. |
| 7123 | * |
| 7124 | * @see LIBMTP_Set_File_Name() |
| 7125 | * @see LIBMTP_Set_Track_Name() |
| 7126 | * @see LIBMTP_Set_Folder_Name() |
| 7127 | * @see LIBMTP_Set_Playlist_Name() |
| 7128 | * @see LIBMTP_Set_Album_Name() |
| 7129 | */ |
| 7130 | int LIBMTP_Set_Object_Filename(LIBMTP_mtpdevice_t *device, |
| 7131 | uint32_t object_id, char* newname) |
| 7132 | { |
| 7133 | int ret; |
| 7134 | LIBMTP_file_t *file; |
| 7135 | |
| 7136 | file = LIBMTP_Get_Filemetadata(device, object_id); |
Linus Walleij | fb7212f | 2008-09-24 20:30:13 +0000 | [diff] [blame] | 7137 | |
| 7138 | if (file == NULL) { |
| 7139 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Set_Object_Filename(): " |
| 7140 | "could not get file metadata for target object."); |
| 7141 | return -1; |
| 7142 | } |
Linus Walleij | 08a5fe1 | 2009-09-08 21:28:58 +0000 | [diff] [blame] | 7143 | |
Linus Walleij | fb7212f | 2008-09-24 20:30:13 +0000 | [diff] [blame] | 7144 | ret = set_object_filename(device, object_id, map_libmtp_type_to_ptp_type(file->filetype), (const char **) &newname); |
Linus Walleij | 08a5fe1 | 2009-09-08 21:28:58 +0000 | [diff] [blame] | 7145 | |
Linus Walleij | 8d8c435 | 2008-09-23 23:04:16 +0000 | [diff] [blame] | 7146 | free(file); |
| 7147 | |
| 7148 | return ret; |
| 7149 | } |
| 7150 | |
| 7151 | /** |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 7152 | * Helper function. This indicates if a track exists on the device |
| 7153 | * @param device a pointer to the device to get the track from. |
| 7154 | * @param id the track ID of the track to retrieve. |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 7155 | * @return TRUE (!=0) if the track exists, FALSE (0) if not |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 7156 | */ |
| 7157 | int LIBMTP_Track_Exists(LIBMTP_mtpdevice_t *device, |
| 7158 | uint32_t const id) |
| 7159 | { |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 7160 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 7161 | uint16_t ret; |
| 7162 | PTPObject *ob; |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 7163 | |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 7164 | ret = ptp_object_want (params, id, 0, &ob); |
| 7165 | if (ret == PTP_RC_OK) |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 7166 | return -1; |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 7167 | return 0; |
| 7168 | } |
| 7169 | |
| 7170 | /** |
| 7171 | * This creates a new folder structure and allocates memory |
| 7172 | * for it. Notice that if you add strings to this structure they |
| 7173 | * will be freed by the corresponding <code>LIBMTP_folder_track_t</code> |
| 7174 | * operation later, so be careful of using strdup() when assigning |
| 7175 | * strings, e.g.: |
| 7176 | * |
| 7177 | * @return a pointer to the newly allocated folder structure. |
| 7178 | * @see LIBMTP_destroy_folder_t() |
| 7179 | */ |
| 7180 | LIBMTP_folder_t *LIBMTP_new_folder_t(void) |
| 7181 | { |
| 7182 | LIBMTP_folder_t *new = (LIBMTP_folder_t *) malloc(sizeof(LIBMTP_folder_t)); |
| 7183 | if (new == NULL) { |
| 7184 | return NULL; |
| 7185 | } |
| 7186 | new->folder_id = 0; |
| 7187 | new->parent_id = 0; |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 7188 | new->storage_id = 0; |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 7189 | new->name = NULL; |
| 7190 | new->sibling = NULL; |
| 7191 | new->child = NULL; |
| 7192 | return new; |
| 7193 | } |
| 7194 | |
| 7195 | /** |
Linus Walleij | 5573def | 2007-04-23 07:04:18 +0000 | [diff] [blame] | 7196 | * This recursively deletes the memory for a folder structure. |
| 7197 | * This shall typically be called on a top-level folder list to |
| 7198 | * detsroy the entire folder tree. |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 7199 | * |
| 7200 | * @param folder folder structure to destroy |
| 7201 | * @see LIBMTP_new_folder_t() |
| 7202 | */ |
| 7203 | void LIBMTP_destroy_folder_t(LIBMTP_folder_t *folder) |
| 7204 | { |
| 7205 | |
| 7206 | if(folder == NULL) { |
| 7207 | return; |
| 7208 | } |
| 7209 | |
| 7210 | //Destroy from the bottom up |
| 7211 | if(folder->child != NULL) { |
| 7212 | LIBMTP_destroy_folder_t(folder->child); |
| 7213 | } |
| 7214 | |
| 7215 | if(folder->sibling != NULL) { |
| 7216 | LIBMTP_destroy_folder_t(folder->sibling); |
| 7217 | } |
| 7218 | |
| 7219 | if(folder->name != NULL) { |
| 7220 | free(folder->name); |
| 7221 | } |
| 7222 | |
| 7223 | free(folder); |
| 7224 | } |
| 7225 | |
| 7226 | /** |
| 7227 | * Helper function. Returns a folder structure for a |
| 7228 | * specified id. |
| 7229 | * |
| 7230 | * @param folderlist list of folders to search |
| 7231 | * @id id of folder to look for |
| 7232 | * @return a folder or NULL if not found |
| 7233 | */ |
| 7234 | LIBMTP_folder_t *LIBMTP_Find_Folder(LIBMTP_folder_t *folderlist, uint32_t id) |
| 7235 | { |
| 7236 | LIBMTP_folder_t *ret = NULL; |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 7237 | |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 7238 | if(folderlist == NULL) { |
| 7239 | return NULL; |
| 7240 | } |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 7241 | |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 7242 | if(folderlist->folder_id == id) { |
| 7243 | return folderlist; |
| 7244 | } |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 7245 | |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 7246 | if(folderlist->sibling) { |
| 7247 | ret = LIBMTP_Find_Folder(folderlist->sibling, id); |
| 7248 | } |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 7249 | |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 7250 | if(folderlist->child && ret == NULL) { |
| 7251 | ret = LIBMTP_Find_Folder(folderlist->child, id); |
| 7252 | } |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 7253 | |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 7254 | return ret; |
| 7255 | } |
| 7256 | |
| 7257 | /** |
Linus Walleij | 4d0deea | 2007-10-19 21:27:48 +0000 | [diff] [blame] | 7258 | * Function used to recursively get subfolders from params. |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 7259 | */ |
Linus Walleij | a89a794 | 2008-12-14 23:19:34 +0000 | [diff] [blame] | 7260 | static LIBMTP_folder_t *get_subfolders_for_folder(LIBMTP_folder_t *list, uint32_t parent) |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 7261 | { |
Linus Walleij | 4fc1917 | 2010-01-19 00:10:26 +0000 | [diff] [blame] | 7262 | LIBMTP_folder_t *retfolders = NULL; |
| 7263 | LIBMTP_folder_t *children, *iter, *curr; |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 7264 | |
Linus Walleij | a89a794 | 2008-12-14 23:19:34 +0000 | [diff] [blame] | 7265 | iter = list->sibling; |
| 7266 | while(iter != list) { |
| 7267 | if (iter->parent_id != parent) { |
| 7268 | iter = iter->sibling; |
| 7269 | continue; |
| 7270 | } |
| 7271 | |
| 7272 | /* We know that iter is a child of 'parent', therefore we can safely |
| 7273 | * hold on to 'iter' locally since no one else will steal it |
| 7274 | * from the 'list' as we recurse. */ |
| 7275 | children = get_subfolders_for_folder(list, iter->folder_id); |
| 7276 | |
| 7277 | curr = iter; |
| 7278 | iter = iter->sibling; |
| 7279 | |
| 7280 | // Remove curr from the list. |
| 7281 | curr->child->sibling = curr->sibling; |
| 7282 | curr->sibling->child = curr->child; |
Linus Walleij | 08a5fe1 | 2009-09-08 21:28:58 +0000 | [diff] [blame] | 7283 | |
Linus Walleij | a89a794 | 2008-12-14 23:19:34 +0000 | [diff] [blame] | 7284 | // Attach the children to curr. |
| 7285 | curr->child = children; |
Linus Walleij | 08a5fe1 | 2009-09-08 21:28:58 +0000 | [diff] [blame] | 7286 | |
Linus Walleij | a89a794 | 2008-12-14 23:19:34 +0000 | [diff] [blame] | 7287 | // Put this folder into the list of siblings. |
| 7288 | curr->sibling = retfolders; |
| 7289 | retfolders = curr; |
| 7290 | } |
| 7291 | |
| 7292 | return retfolders; |
| 7293 | } |
| 7294 | |
| 7295 | /** |
| 7296 | * This returns a list of all folders available |
| 7297 | * on the current MTP device. |
| 7298 | * |
| 7299 | * @param device a pointer to the device to get the folder listing for. |
Linus Walleij | 1e4dfea | 2011-03-05 22:50:39 +0100 | [diff] [blame] | 7300 | * @param storage a storage ID to get the folder list from |
Linus Walleij | a89a794 | 2008-12-14 23:19:34 +0000 | [diff] [blame] | 7301 | * @return a list of folders |
| 7302 | */ |
Linus Walleij | 1e4dfea | 2011-03-05 22:50:39 +0100 | [diff] [blame] | 7303 | LIBMTP_folder_t *LIBMTP_Get_Folder_List_For_Storage(LIBMTP_mtpdevice_t *device, |
| 7304 | uint32_t const storage) |
Linus Walleij | a89a794 | 2008-12-14 23:19:34 +0000 | [diff] [blame] | 7305 | { |
| 7306 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | cdc1209 | 2010-09-18 11:28:56 +0000 | [diff] [blame] | 7307 | LIBMTP_folder_t head, *rv; |
Linus Walleij | a89a794 | 2008-12-14 23:19:34 +0000 | [diff] [blame] | 7308 | int i; |
| 7309 | |
| 7310 | // Get all the handles if we haven't already done that |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 7311 | if (params->nrofobjects == 0) { |
Linus Walleij | a89a794 | 2008-12-14 23:19:34 +0000 | [diff] [blame] | 7312 | flush_handles(device); |
| 7313 | } |
| 7314 | |
Linus Walleij | 4ab47b2 | 2008-12-14 23:24:26 +0000 | [diff] [blame] | 7315 | /* |
| 7316 | * This creates a temporary list of the folders, this is in a |
| 7317 | * reverse order and uses the Folder pointers that are already |
| 7318 | * in the Folder structure. From this we can then build up the |
| 7319 | * folder hierarchy with only looking at this temporary list, |
| 7320 | * and removing the folders from this temporary list as we go. |
| 7321 | * This significantly reduces the number of operations that we |
| 7322 | * have to do in building the folder hierarchy. Also since the |
| 7323 | * temp list is in reverse order, when we prepend to the sibling |
| 7324 | * list things are in the same order as they were originally |
| 7325 | * in the handle list. |
| 7326 | */ |
Linus Walleij | a89a794 | 2008-12-14 23:19:34 +0000 | [diff] [blame] | 7327 | head.sibling = &head; |
| 7328 | head.child = &head; |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 7329 | for (i = 0; i < params->nrofobjects; i++) { |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 7330 | LIBMTP_folder_t *folder; |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 7331 | PTPObject *ob; |
Linus Walleij | a89a794 | 2008-12-14 23:19:34 +0000 | [diff] [blame] | 7332 | |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 7333 | ob = ¶ms->objects[i]; |
| 7334 | if (ob->oi.ObjectFormat != PTP_OFC_Association) { |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 7335 | continue; |
| 7336 | } |
Linus Walleij | 1e4dfea | 2011-03-05 22:50:39 +0100 | [diff] [blame] | 7337 | |
| 7338 | if (storage != PTP_GOH_ALL_STORAGE && storage != ob->oi.StorageID) { |
| 7339 | continue; |
| 7340 | } |
| 7341 | |
Linus Walleij | 4ab47b2 | 2008-12-14 23:24:26 +0000 | [diff] [blame] | 7342 | /* |
| 7343 | * Do we know how to handle these? They are part |
| 7344 | * of the MTP 1.0 specification paragraph 3.6.4. |
Linus Walleij | 08a5fe1 | 2009-09-08 21:28:58 +0000 | [diff] [blame] | 7345 | * For AssociationDesc 0x00000001U ptp_mtp_getobjectreferences() |
| 7346 | * should be called on these to get the contained objects, but |
Linus Walleij | 4ab47b2 | 2008-12-14 23:24:26 +0000 | [diff] [blame] | 7347 | * we basically don't care. Hopefully parent_id is maintained for all |
| 7348 | * children, because we rely on that instead. |
| 7349 | */ |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 7350 | if (ob->oi.AssociationDesc != 0x00000000U) { |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 7351 | LIBMTP_INFO("MTP extended association type 0x%08x encountered\n", ob->oi.AssociationDesc); |
Linus Walleij | d71d0b3 | 2008-09-22 08:21:03 +0000 | [diff] [blame] | 7352 | } |
| 7353 | |
Linus Walleij | 4d0deea | 2007-10-19 21:27:48 +0000 | [diff] [blame] | 7354 | // Create a folder struct... |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 7355 | folder = LIBMTP_new_folder_t(); |
Linus Walleij | 4d0deea | 2007-10-19 21:27:48 +0000 | [diff] [blame] | 7356 | if (folder == NULL) { |
| 7357 | // malloc failure or so. |
| 7358 | return NULL; |
| 7359 | } |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 7360 | folder->folder_id = ob->oid; |
| 7361 | folder->parent_id = ob->oi.ParentObject; |
| 7362 | folder->storage_id = ob->oi.StorageID; |
| 7363 | folder->name = (ob->oi.Filename) ? (char *)strdup(ob->oi.Filename) : NULL; |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 7364 | |
Linus Walleij | a89a794 | 2008-12-14 23:19:34 +0000 | [diff] [blame] | 7365 | // pretend sibling says next, and child says prev. |
| 7366 | folder->sibling = head.sibling; |
| 7367 | folder->child = &head; |
| 7368 | head.sibling->child = folder; |
| 7369 | head.sibling = folder; |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 7370 | } |
Linus Walleij | 4d0deea | 2007-10-19 21:27:48 +0000 | [diff] [blame] | 7371 | |
Linus Walleij | 1e4dfea | 2011-03-05 22:50:39 +0100 | [diff] [blame] | 7372 | // We begin at the given root folder and get them all recursively |
Linus Walleij | 4fc1917 | 2010-01-19 00:10:26 +0000 | [diff] [blame] | 7373 | rv = get_subfolders_for_folder(&head, 0x00000000U); |
| 7374 | |
| 7375 | // Some buggy devices may have some files in the "root folder" |
| 7376 | // 0xffffffff so if 0x00000000 didn't return any folders, |
| 7377 | // look for children of the root 0xffffffffU |
| 7378 | if (rv == NULL) { |
| 7379 | rv = get_subfolders_for_folder(&head, 0xffffffffU); |
| 7380 | if (rv != NULL) |
| 7381 | LIBMTP_ERROR("Device have files in \"root folder\" 0xffffffffU - " |
| 7382 | "this is a firmware bug (but continuing)\n"); |
| 7383 | } |
Linus Walleij | a89a794 | 2008-12-14 23:19:34 +0000 | [diff] [blame] | 7384 | |
| 7385 | // The temp list should be empty. Clean up any orphans just in case. |
| 7386 | while(head.sibling != &head) { |
| 7387 | LIBMTP_folder_t *curr = head.sibling; |
| 7388 | |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 7389 | LIBMTP_INFO("Orphan folder with ID: 0x%08x name: \"%s\" encountered.\n", |
Linus Walleij | a89a794 | 2008-12-14 23:19:34 +0000 | [diff] [blame] | 7390 | curr->folder_id, |
| 7391 | curr->name); |
| 7392 | curr->sibling->child = curr->child; |
| 7393 | curr->child->sibling = curr->sibling; |
| 7394 | curr->child = NULL; |
| 7395 | curr->sibling = NULL; |
| 7396 | LIBMTP_destroy_folder_t(curr); |
| 7397 | } |
| 7398 | |
| 7399 | return rv; |
Linus Walleij | 4d0deea | 2007-10-19 21:27:48 +0000 | [diff] [blame] | 7400 | } |
| 7401 | |
| 7402 | /** |
Linus Walleij | 1e4dfea | 2011-03-05 22:50:39 +0100 | [diff] [blame] | 7403 | * This returns a list of all folders available |
| 7404 | * on the current MTP device. |
| 7405 | * |
| 7406 | * @param device a pointer to the device to get the folder listing for. |
| 7407 | * @return a list of folders |
| 7408 | */ |
| 7409 | LIBMTP_folder_t *LIBMTP_Get_Folder_List(LIBMTP_mtpdevice_t *device) |
| 7410 | { |
| 7411 | return LIBMTP_Get_Folder_List_For_Storage(device, PTP_GOH_ALL_STORAGE); |
| 7412 | } |
| 7413 | |
| 7414 | /** |
Linus Walleij | c86afbd | 2006-05-04 19:05:24 +0000 | [diff] [blame] | 7415 | * This create a folder on the current MTP device. The PTP name |
| 7416 | * for a folder is "association". The PTP/MTP devices does not |
| 7417 | * have an internal "folder" concept really, it contains a flat |
| 7418 | * list of all files and some file are "associations" that other |
| 7419 | * files and folders may refer to as its "parent". |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 7420 | * |
Linus Walleij | c86afbd | 2006-05-04 19:05:24 +0000 | [diff] [blame] | 7421 | * @param device a pointer to the device to create the folder on. |
Richard Low | f8cd3d7 | 2009-05-04 16:38:33 +0000 | [diff] [blame] | 7422 | * @param name the name of the new folder. Note this can be modified |
| 7423 | * if the device does not support all the characters in the |
| 7424 | * name. |
Linus Walleij | c86afbd | 2006-05-04 19:05:24 +0000 | [diff] [blame] | 7425 | * @param parent_id id of parent folder to add the new folder to, |
Reverend Homer | 8e3af29 | 2015-11-22 19:58:01 +0300 | [diff] [blame] | 7426 | * or 0xFFFFFFFF to put it in the root directory. |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 7427 | * @param storage_id id of the storage to add this new folder to. |
| 7428 | * notice that you cannot mismatch storage id and parent id: |
| 7429 | * they must both be on the same storage! Pass in 0 if you |
| 7430 | * want to create this folder on the default storage. |
Linus Walleij | c86afbd | 2006-05-04 19:05:24 +0000 | [diff] [blame] | 7431 | * @return id to new folder or 0 if an error occured |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 7432 | */ |
Richard Low | f8cd3d7 | 2009-05-04 16:38:33 +0000 | [diff] [blame] | 7433 | uint32_t LIBMTP_Create_Folder(LIBMTP_mtpdevice_t *device, char *name, |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 7434 | uint32_t parent_id, uint32_t storage_id) |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 7435 | { |
| 7436 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | d3b7857 | 2007-08-24 21:28:24 +0000 | [diff] [blame] | 7437 | PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo; |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 7438 | uint32_t parenthandle = 0; |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 7439 | uint32_t store; |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 7440 | PTPObjectInfo new_folder; |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 7441 | uint16_t ret; |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 7442 | uint32_t new_id = 0; |
| 7443 | |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 7444 | if (storage_id == 0) { |
Linus Walleij | d71d0b3 | 2008-09-22 08:21:03 +0000 | [diff] [blame] | 7445 | // I'm just guessing that a folder may require 512 bytes |
Linus Walleij | 88babc8 | 2014-06-02 21:32:14 +0200 | [diff] [blame] | 7446 | store = get_suggested_storage_id(device, 512, parent_id); |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 7447 | } else { |
| 7448 | store = storage_id; |
| 7449 | } |
| 7450 | parenthandle = parent_id; |
| 7451 | |
Richard Low | 5942604 | 2009-05-04 16:40:09 +0000 | [diff] [blame] | 7452 | memset(&new_folder, 0, sizeof(new_folder)); |
Richard Low | f8cd3d7 | 2009-05-04 16:38:33 +0000 | [diff] [blame] | 7453 | new_folder.Filename = name; |
Linus Walleij | fec4d56 | 2008-06-01 22:30:36 +0000 | [diff] [blame] | 7454 | if (FLAG_ONLY_7BIT_FILENAMES(ptp_usb)) { |
Linus Walleij | d3b7857 | 2007-08-24 21:28:24 +0000 | [diff] [blame] | 7455 | strip_7bit_from_utf8(new_folder.Filename); |
| 7456 | } |
Linus Walleij | 755c246 | 2011-03-04 20:11:01 +0100 | [diff] [blame] | 7457 | new_folder.ObjectCompressedSize = 0; |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 7458 | new_folder.ObjectFormat = PTP_OFC_Association; |
Linus Walleij | dbbfecc | 2008-11-05 09:52:47 +0000 | [diff] [blame] | 7459 | new_folder.ProtectionStatus = PTP_PS_NoProtection; |
Linus Walleij | 753a46a | 2008-11-05 09:10:52 +0000 | [diff] [blame] | 7460 | new_folder.AssociationType = PTP_AT_GenericFolder; |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 7461 | new_folder.ParentObject = parent_id; |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 7462 | new_folder.StorageID = store; |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 7463 | |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 7464 | // Create the object |
Ignacio Martinez | 5ab6747 | 2012-03-22 21:59:22 +0100 | [diff] [blame] | 7465 | if (!(params->device_flags & DEVICE_FLAG_BROKEN_SEND_OBJECT_PROPLIST) && |
| 7466 | ptp_operation_issupported(params,PTP_OC_MTP_SendObjectPropList)) { |
| 7467 | MTPProperties *props = (MTPProperties*)calloc(2,sizeof(MTPProperties)); |
| 7468 | |
| 7469 | props[0].property = PTP_OPC_ObjectFileName; |
| 7470 | props[0].datatype = PTP_DTC_STR; |
| 7471 | props[0].propval.str = name; |
| 7472 | |
| 7473 | props[1].property = PTP_OPC_Name; |
| 7474 | props[1].datatype = PTP_DTC_STR; |
| 7475 | props[1].propval.str = name; |
| 7476 | |
| 7477 | ret = ptp_mtp_sendobjectproplist(params, &store, &parenthandle, &new_id, PTP_OFC_Association, |
| 7478 | 0, props, 1); |
| 7479 | free(props); |
| 7480 | } else { |
| 7481 | ret = ptp_sendobjectinfo(params, &store, &parenthandle, &new_id, &new_folder); |
| 7482 | } |
| 7483 | |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 7484 | if (ret != PTP_RC_OK) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 7485 | add_ptp_error_to_errorstack(device, ret, "LIBMTP_Create_Folder: Could not send object info."); |
Linus Walleij | 99310d4 | 2006-11-01 08:29:39 +0000 | [diff] [blame] | 7486 | if (ret == PTP_RC_AccessDenied) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 7487 | add_ptp_error_to_errorstack(device, ret, "ACCESS DENIED."); |
Linus Walleij | 99310d4 | 2006-11-01 08:29:39 +0000 | [diff] [blame] | 7488 | } |
Linus Walleij | c86afbd | 2006-05-04 19:05:24 +0000 | [diff] [blame] | 7489 | return 0; |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 7490 | } |
Linus Walleij | 08a5fe1 | 2009-09-08 21:28:58 +0000 | [diff] [blame] | 7491 | // NOTE: don't destroy the new_folder objectinfo, because it is statically referencing |
Richard Low | f8cd3d7 | 2009-05-04 16:38:33 +0000 | [diff] [blame] | 7492 | // several strings. |
tsaarni | a3eb60a | 2007-07-06 17:41:30 +0000 | [diff] [blame] | 7493 | |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 7494 | add_object_to_cache(device, new_id); |
tsaarni | a3eb60a | 2007-07-06 17:41:30 +0000 | [diff] [blame] | 7495 | |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 7496 | return new_id; |
| 7497 | } |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 7498 | |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 7499 | /** |
| 7500 | * This creates a new playlist metadata structure and allocates memory |
| 7501 | * for it. Notice that if you add strings to this structure they |
| 7502 | * will be freed by the corresponding <code>LIBMTP_destroy_playlist_t</code> |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 7503 | * operation later, so be careful of using strdup() when assigning |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 7504 | * strings, e.g.: |
| 7505 | * |
| 7506 | * <pre> |
| 7507 | * LIBMTP_playlist_t *pl = LIBMTP_new_playlist_t(); |
| 7508 | * pl->name = strdup(str); |
| 7509 | * .... |
| 7510 | * LIBMTP_destroy_playlist_t(pl); |
| 7511 | * </pre> |
| 7512 | * |
| 7513 | * @return a pointer to the newly allocated metadata structure. |
| 7514 | * @see LIBMTP_destroy_playlist_t() |
| 7515 | */ |
| 7516 | LIBMTP_playlist_t *LIBMTP_new_playlist_t(void) |
| 7517 | { |
| 7518 | LIBMTP_playlist_t *new = (LIBMTP_playlist_t *) malloc(sizeof(LIBMTP_playlist_t)); |
| 7519 | if (new == NULL) { |
| 7520 | return NULL; |
| 7521 | } |
| 7522 | new->playlist_id = 0; |
Linus Walleij | 5ce59db | 2008-03-12 21:22:58 +0000 | [diff] [blame] | 7523 | new->parent_id = 0; |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 7524 | new->storage_id = 0; |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 7525 | new->name = NULL; |
| 7526 | new->tracks = NULL; |
| 7527 | new->no_tracks = 0; |
| 7528 | new->next = NULL; |
| 7529 | return new; |
| 7530 | } |
| 7531 | |
| 7532 | /** |
| 7533 | * This destroys a playlist metadata structure and deallocates the memory |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 7534 | * used by it, including any strings. Never use a track metadata |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 7535 | * structure again after calling this function on it. |
| 7536 | * @param playlist the playlist metadata to destroy. |
| 7537 | * @see LIBMTP_new_playlist_t() |
| 7538 | */ |
| 7539 | void LIBMTP_destroy_playlist_t(LIBMTP_playlist_t *playlist) |
| 7540 | { |
| 7541 | if (playlist == NULL) { |
| 7542 | return; |
| 7543 | } |
| 7544 | if (playlist->name != NULL) |
| 7545 | free(playlist->name); |
| 7546 | if (playlist->tracks != NULL) |
| 7547 | free(playlist->tracks); |
| 7548 | free(playlist); |
| 7549 | return; |
| 7550 | } |
| 7551 | |
| 7552 | /** |
| 7553 | * This function returns a list of the playlists available on the |
| 7554 | * device. Typical usage: |
| 7555 | * |
| 7556 | * <pre> |
| 7557 | * </pre> |
| 7558 | * |
| 7559 | * @param device a pointer to the device to get the playlist listing from. |
| 7560 | * @return a playlist list on success, else NULL. If there are no playlists |
| 7561 | * on the device, NULL will be returned as well. |
Linus Walleij | 2e4b5f9 | 2006-06-16 14:00:49 +0000 | [diff] [blame] | 7562 | * @see LIBMTP_Get_Playlist() |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 7563 | */ |
| 7564 | LIBMTP_playlist_t *LIBMTP_Get_Playlist_List(LIBMTP_mtpdevice_t *device) |
| 7565 | { |
Linus Walleij | f3c4405 | 2008-08-16 21:14:56 +0000 | [diff] [blame] | 7566 | PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo; |
| 7567 | const int REQ_SPL = FLAG_PLAYLIST_SPL(ptp_usb); |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 7568 | PTPParams *params = (PTPParams *) device->params; |
| 7569 | LIBMTP_playlist_t *retlists = NULL; |
| 7570 | LIBMTP_playlist_t *curlist = NULL; |
| 7571 | uint32_t i; |
| 7572 | |
| 7573 | // Get all the handles if we haven't already done that |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 7574 | if (params->nrofobjects == 0) { |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 7575 | flush_handles(device); |
| 7576 | } |
| 7577 | |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 7578 | for (i = 0; i < params->nrofobjects; i++) { |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 7579 | LIBMTP_playlist_t *pl; |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 7580 | PTPObject *ob; |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 7581 | uint16_t ret; |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 7582 | |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 7583 | ob = ¶ms->objects[i]; |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 7584 | |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 7585 | // Ignore stuff that isn't playlists |
Linus Walleij | f3c4405 | 2008-08-16 21:14:56 +0000 | [diff] [blame] | 7586 | |
| 7587 | // For Samsung players we must look for the .spl extension explicitly since |
| 7588 | // playlists are not stored as playlist objects. |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 7589 | if ( REQ_SPL && is_spl_playlist(&ob->oi) ) { |
Linus Walleij | f3c4405 | 2008-08-16 21:14:56 +0000 | [diff] [blame] | 7590 | // Allocate a new playlist type |
| 7591 | pl = LIBMTP_new_playlist_t(); |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 7592 | spl_to_playlist_t(device, &ob->oi, ob->oid, pl); |
Linus Walleij | f3c4405 | 2008-08-16 21:14:56 +0000 | [diff] [blame] | 7593 | } |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 7594 | else if ( ob->oi.ObjectFormat != PTP_OFC_MTP_AbstractAudioVideoPlaylist ) { |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 7595 | continue; |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 7596 | } |
Linus Walleij | f3c4405 | 2008-08-16 21:14:56 +0000 | [diff] [blame] | 7597 | else { |
| 7598 | // Allocate a new playlist type |
| 7599 | pl = LIBMTP_new_playlist_t(); |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 7600 | |
Linus Walleij | f329662 | 2008-09-04 20:53:56 +0000 | [diff] [blame] | 7601 | // Try to look up proper name, else use the oi->Filename field. |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 7602 | pl->name = get_string_from_object(device, ob->oid, PTP_OPC_Name); |
Linus Walleij | f329662 | 2008-09-04 20:53:56 +0000 | [diff] [blame] | 7603 | if (pl->name == NULL) { |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 7604 | pl->name = strdup(ob->oi.Filename); |
Linus Walleij | f329662 | 2008-09-04 20:53:56 +0000 | [diff] [blame] | 7605 | } |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 7606 | pl->playlist_id = ob->oid; |
| 7607 | pl->parent_id = ob->oi.ParentObject; |
| 7608 | pl->storage_id = ob->oi.StorageID; |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 7609 | |
Linus Walleij | f3c4405 | 2008-08-16 21:14:56 +0000 | [diff] [blame] | 7610 | // Then get the track listing for this playlist |
| 7611 | ret = ptp_mtp_getobjectreferences(params, pl->playlist_id, &pl->tracks, &pl->no_tracks); |
| 7612 | if (ret != PTP_RC_OK) { |
Linus Walleij | 8d8c435 | 2008-09-23 23:04:16 +0000 | [diff] [blame] | 7613 | add_ptp_error_to_errorstack(device, ret, "LIBMTP_Get_Playlist_List(): " |
| 7614 | "could not get object references."); |
Linus Walleij | f3c4405 | 2008-08-16 21:14:56 +0000 | [diff] [blame] | 7615 | pl->tracks = NULL; |
| 7616 | pl->no_tracks = 0; |
| 7617 | } |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 7618 | } |
Linus Walleij | 08a5fe1 | 2009-09-08 21:28:58 +0000 | [diff] [blame] | 7619 | |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 7620 | // Add playlist to a list that will be returned afterwards. |
| 7621 | if (retlists == NULL) { |
| 7622 | retlists = pl; |
| 7623 | curlist = pl; |
| 7624 | } else { |
| 7625 | curlist->next = pl; |
| 7626 | curlist = pl; |
| 7627 | } |
Linus Walleij | 08a5fe1 | 2009-09-08 21:28:58 +0000 | [diff] [blame] | 7628 | |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 7629 | // Call callback here if we decide to add that possibility... |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 7630 | } |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 7631 | return retlists; |
| 7632 | } |
| 7633 | |
Linus Walleij | 2e4b5f9 | 2006-06-16 14:00:49 +0000 | [diff] [blame] | 7634 | |
| 7635 | /** |
| 7636 | * This function retrieves an individual playlist from the device. |
| 7637 | * @param device a pointer to the device to get the playlist from. |
| 7638 | * @param plid the unique ID of the playlist to retrieve. |
| 7639 | * @return a valid playlist metadata post or NULL on failure. |
| 7640 | * @see LIBMTP_Get_Playlist_List() |
| 7641 | */ |
| 7642 | LIBMTP_playlist_t *LIBMTP_Get_Playlist(LIBMTP_mtpdevice_t *device, uint32_t const plid) |
| 7643 | { |
Linus Walleij | f3c4405 | 2008-08-16 21:14:56 +0000 | [diff] [blame] | 7644 | PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo; |
| 7645 | const int REQ_SPL = FLAG_PLAYLIST_SPL(ptp_usb); |
Linus Walleij | 2e4b5f9 | 2006-06-16 14:00:49 +0000 | [diff] [blame] | 7646 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 7647 | PTPObject *ob; |
| 7648 | LIBMTP_playlist_t *pl; |
| 7649 | uint16_t ret; |
Linus Walleij | 2e4b5f9 | 2006-06-16 14:00:49 +0000 | [diff] [blame] | 7650 | |
| 7651 | // Get all the handles if we haven't already done that |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 7652 | if (params->nrofobjects == 0) { |
Linus Walleij | 2e4b5f9 | 2006-06-16 14:00:49 +0000 | [diff] [blame] | 7653 | flush_handles(device); |
| 7654 | } |
| 7655 | |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 7656 | ret = ptp_object_want (params, plid, PTPOBJECT_OBJECTINFO_LOADED, &ob); |
| 7657 | if (ret != PTP_RC_OK) |
| 7658 | return NULL; |
Linus Walleij | 2e4b5f9 | 2006-06-16 14:00:49 +0000 | [diff] [blame] | 7659 | |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 7660 | // For Samsung players we must look for the .spl extension explicitly since |
| 7661 | // playlists are not stored as playlist objects. |
| 7662 | if ( REQ_SPL && is_spl_playlist(&ob->oi) ) { |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 7663 | // Allocate a new playlist type |
| 7664 | pl = LIBMTP_new_playlist_t(); |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 7665 | spl_to_playlist_t(device, &ob->oi, ob->oid, pl); |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 7666 | return pl; |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 7667 | } |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 7668 | |
| 7669 | // Ignore stuff that isn't playlists |
| 7670 | else if ( ob->oi.ObjectFormat != PTP_OFC_MTP_AbstractAudioVideoPlaylist ) { |
| 7671 | return NULL; |
| 7672 | } |
| 7673 | |
| 7674 | // Allocate a new playlist type |
| 7675 | pl = LIBMTP_new_playlist_t(); |
| 7676 | |
| 7677 | pl->name = get_string_from_object(device, ob->oid, PTP_OPC_Name); |
| 7678 | if (pl->name == NULL) { |
| 7679 | pl->name = strdup(ob->oi.Filename); |
| 7680 | } |
| 7681 | pl->playlist_id = ob->oid; |
| 7682 | pl->parent_id = ob->oi.ParentObject; |
| 7683 | pl->storage_id = ob->oi.StorageID; |
Linus Walleij | 08a5fe1 | 2009-09-08 21:28:58 +0000 | [diff] [blame] | 7684 | |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 7685 | // Then get the track listing for this playlist |
| 7686 | ret = ptp_mtp_getobjectreferences(params, pl->playlist_id, &pl->tracks, &pl->no_tracks); |
| 7687 | if (ret != PTP_RC_OK) { |
| 7688 | add_ptp_error_to_errorstack(device, ret, "LIBMTP_Get_Playlist(): Could not get object references."); |
| 7689 | pl->tracks = NULL; |
| 7690 | pl->no_tracks = 0; |
| 7691 | } |
| 7692 | |
| 7693 | return pl; |
Linus Walleij | 2e4b5f9 | 2006-06-16 14:00:49 +0000 | [diff] [blame] | 7694 | } |
| 7695 | |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 7696 | /** |
Linus Walleij | b7426d1 | 2006-11-25 23:19:11 +0000 | [diff] [blame] | 7697 | * This function creates a new abstract list such as a playlist |
| 7698 | * or an album. |
Linus Walleij | 08a5fe1 | 2009-09-08 21:28:58 +0000 | [diff] [blame] | 7699 | * |
Linus Walleij | b7426d1 | 2006-11-25 23:19:11 +0000 | [diff] [blame] | 7700 | * @param device a pointer to the device to create the new abstract list |
| 7701 | * on. |
| 7702 | * @param name the name of the new abstract list. |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 7703 | * @param artist the artist of the new abstract list or NULL. |
| 7704 | * @param genre the genre of the new abstract list or NULL. |
Linus Walleij | b7426d1 | 2006-11-25 23:19:11 +0000 | [diff] [blame] | 7705 | * @param parenthandle the handle of the parent or 0 for no parent |
| 7706 | * i.e. the root folder. |
| 7707 | * @param objectformat the abstract list type to create. |
| 7708 | * @param suffix the ".foo" (4 characters) suffix to use for the virtual |
| 7709 | * "file" created by this operation. |
| 7710 | * @param newid a pointer to a variable that will hold the new object |
| 7711 | * ID if this call is successful. |
| 7712 | * @param tracks an array of tracks to associate with this list. |
| 7713 | * @param no_tracks the number of tracks in the list. |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 7714 | * @return 0 on success, any other value means failure. |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 7715 | */ |
Linus Walleij | b7426d1 | 2006-11-25 23:19:11 +0000 | [diff] [blame] | 7716 | static int create_new_abstract_list(LIBMTP_mtpdevice_t *device, |
| 7717 | char const * const name, |
Linus Walleij | adce4a5 | 2007-04-23 07:28:11 +0000 | [diff] [blame] | 7718 | char const * const artist, |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 7719 | char const * const composer, |
Linus Walleij | adce4a5 | 2007-04-23 07:28:11 +0000 | [diff] [blame] | 7720 | char const * const genre, |
Linus Walleij | b7426d1 | 2006-11-25 23:19:11 +0000 | [diff] [blame] | 7721 | uint32_t const parenthandle, |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 7722 | uint32_t const storageid, |
Linus Walleij | b7426d1 | 2006-11-25 23:19:11 +0000 | [diff] [blame] | 7723 | uint16_t const objectformat, |
| 7724 | char const * const suffix, |
| 7725 | uint32_t * const newid, |
| 7726 | uint32_t const * const tracks, |
| 7727 | uint32_t const no_tracks) |
| 7728 | |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 7729 | { |
Linus Walleij | b7426d1 | 2006-11-25 23:19:11 +0000 | [diff] [blame] | 7730 | int i; |
| 7731 | int supported = 0; |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 7732 | uint16_t ret; |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 7733 | uint16_t *properties = NULL; |
rreardon | 508705f | 2006-11-19 21:27:22 +0000 | [diff] [blame] | 7734 | uint32_t propcnt = 0; |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 7735 | uint32_t store; |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 7736 | uint32_t localph = parenthandle; |
Linus Walleij | b7426d1 | 2006-11-25 23:19:11 +0000 | [diff] [blame] | 7737 | uint8_t nonconsumable = 0x00U; /* By default it is consumable */ |
| 7738 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | d3b7857 | 2007-08-24 21:28:24 +0000 | [diff] [blame] | 7739 | PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo; |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 7740 | char fname[256]; |
Linus Walleij | 71c7929 | 2011-03-05 22:08:07 +0100 | [diff] [blame] | 7741 | //uint8_t data[2]; |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 7742 | |
Linus Walleij | cdc1209 | 2010-09-18 11:28:56 +0000 | [diff] [blame] | 7743 | // NULL check |
| 7744 | if (!name) { |
| 7745 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "create_new_abstract_list(): list name was NULL, using default name \"Unknown\""); |
| 7746 | return -1; |
| 7747 | } |
| 7748 | |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 7749 | if (storageid == 0) { |
Linus Walleij | d71d0b3 | 2008-09-22 08:21:03 +0000 | [diff] [blame] | 7750 | // I'm just guessing that an abstract list may require 512 bytes |
Linus Walleij | 88babc8 | 2014-06-02 21:32:14 +0200 | [diff] [blame] | 7751 | store = get_suggested_storage_id(device, 512, localph); |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 7752 | } else { |
| 7753 | store = storageid; |
| 7754 | } |
| 7755 | |
Linus Walleij | b7426d1 | 2006-11-25 23:19:11 +0000 | [diff] [blame] | 7756 | // Check if we can create an object of this type |
| 7757 | for ( i=0; i < params->deviceinfo.ImageFormats_len; i++ ) { |
| 7758 | if (params->deviceinfo.ImageFormats[i] == objectformat) { |
| 7759 | supported = 1; |
| 7760 | break; |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 7761 | } |
| 7762 | } |
Linus Walleij | b7426d1 | 2006-11-25 23:19:11 +0000 | [diff] [blame] | 7763 | if (!supported) { |
Linus Walleij | cdc1209 | 2010-09-18 11:28:56 +0000 | [diff] [blame] | 7764 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "create_new_abstract_list(): player does not support this abstract type"); |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 7765 | LIBMTP_ERROR("Unsupported abstract list type: %04x\n", objectformat); |
Linus Walleij | b7426d1 | 2006-11-25 23:19:11 +0000 | [diff] [blame] | 7766 | return -1; |
| 7767 | } |
| 7768 | |
Richard Low | 15731fe | 2007-03-22 20:27:20 +0000 | [diff] [blame] | 7769 | // add the new suffix if it isn't there |
Linus Walleij | 629fe40 | 2006-12-12 23:39:15 +0000 | [diff] [blame] | 7770 | fname[0] = '\0'; |
Linus Walleij | b7426d1 | 2006-11-25 23:19:11 +0000 | [diff] [blame] | 7771 | if (strlen(name) > strlen(suffix)) { |
| 7772 | char const * const suff = &name[strlen(name)-strlen(suffix)]; |
| 7773 | if (!strcmp(suff, suffix)) { |
| 7774 | // Home free. |
Linus Walleij | 629fe40 | 2006-12-12 23:39:15 +0000 | [diff] [blame] | 7775 | strncpy(fname, name, sizeof(fname)); |
Linus Walleij | b7426d1 | 2006-11-25 23:19:11 +0000 | [diff] [blame] | 7776 | } |
| 7777 | } |
| 7778 | // If it didn't end with "<suffix>" then add that here. |
Linus Walleij | 629fe40 | 2006-12-12 23:39:15 +0000 | [diff] [blame] | 7779 | if (fname[0] == '\0') { |
Linus Walleij | b7426d1 | 2006-11-25 23:19:11 +0000 | [diff] [blame] | 7780 | strncpy(fname, name, sizeof(fname)-strlen(suffix)-1); |
| 7781 | strcat(fname, suffix); |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 7782 | fname[sizeof(fname)-1] = '\0'; |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 7783 | } |
| 7784 | |
Linus Walleij | f67c1ad | 2009-01-14 21:39:50 +0000 | [diff] [blame] | 7785 | if (ptp_operation_issupported(params, PTP_OC_MTP_SendObjectPropList) && |
| 7786 | !FLAG_BROKEN_SEND_OBJECT_PROPLIST(ptp_usb)) { |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 7787 | MTPProperties *props = NULL; |
| 7788 | MTPProperties *prop = NULL; |
| 7789 | int nrofprops = 0; |
Linus Walleij | 08a5fe1 | 2009-09-08 21:28:58 +0000 | [diff] [blame] | 7790 | |
Richard Low | 6c0a6ce | 2006-11-26 10:42:08 +0000 | [diff] [blame] | 7791 | *newid = 0x00000000U; |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 7792 | |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 7793 | ret = ptp_mtp_getobjectpropssupported(params, objectformat, &propcnt, &properties); |
mopoke | 3136444 | 2006-11-20 04:53:04 +0000 | [diff] [blame] | 7794 | |
rreardon | 508705f | 2006-11-19 21:27:22 +0000 | [diff] [blame] | 7795 | for (i=0;i<propcnt;i++) { |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 7796 | PTPObjectPropDesc opd; |
Linus Walleij | 08a5fe1 | 2009-09-08 21:28:58 +0000 | [diff] [blame] | 7797 | |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 7798 | ret = ptp_mtp_getobjectpropdesc(params, properties[i], objectformat, &opd); |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 7799 | if (ret != PTP_RC_OK) { |
| 7800 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "create_new_abstract_list(): " |
| 7801 | "could not get property description."); |
| 7802 | } else if (opd.GetSet) { |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 7803 | switch (properties[i]) { |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 7804 | case PTP_OPC_ObjectFileName: |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 7805 | prop = ptp_get_new_object_prop_entry(&props,&nrofprops); |
Linus Walleij | 08a5fe1 | 2009-09-08 21:28:58 +0000 | [diff] [blame] | 7806 | prop->ObjectHandle = *newid; |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 7807 | prop->property = PTP_OPC_ObjectFileName; |
| 7808 | prop->datatype = PTP_DTC_STR; |
| 7809 | prop->propval.str = strdup(fname); |
Linus Walleij | fec4d56 | 2008-06-01 22:30:36 +0000 | [diff] [blame] | 7810 | if (FLAG_ONLY_7BIT_FILENAMES(ptp_usb)) { |
Linus Walleij | d3b7857 | 2007-08-24 21:28:24 +0000 | [diff] [blame] | 7811 | strip_7bit_from_utf8(prop->propval.str); |
| 7812 | } |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 7813 | break; |
| 7814 | case PTP_OPC_ProtectionStatus: |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 7815 | prop = ptp_get_new_object_prop_entry(&props,&nrofprops); |
Linus Walleij | adce4a5 | 2007-04-23 07:28:11 +0000 | [diff] [blame] | 7816 | prop->ObjectHandle = *newid; |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 7817 | prop->property = PTP_OPC_ProtectionStatus; |
| 7818 | prop->datatype = PTP_DTC_UINT16; |
| 7819 | prop->propval.u16 = 0x0000U; /* Not protected */ |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 7820 | break; |
| 7821 | case PTP_OPC_NonConsumable: |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 7822 | prop = ptp_get_new_object_prop_entry(&props,&nrofprops); |
Linus Walleij | adce4a5 | 2007-04-23 07:28:11 +0000 | [diff] [blame] | 7823 | prop->ObjectHandle = *newid; |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 7824 | prop->property = PTP_OPC_NonConsumable; |
| 7825 | prop->datatype = PTP_DTC_UINT8; |
| 7826 | prop->propval.u8 = nonconsumable; |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 7827 | break; |
| 7828 | case PTP_OPC_Name: |
| 7829 | if (name != NULL) { |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 7830 | prop = ptp_get_new_object_prop_entry(&props,&nrofprops); |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 7831 | prop->ObjectHandle = *newid; |
| 7832 | prop->property = PTP_OPC_Name; |
| 7833 | prop->datatype = PTP_DTC_STR; |
| 7834 | prop->propval.str = strdup(name); |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 7835 | } |
| 7836 | break; |
Linus Walleij | 0b75ab6 | 2007-12-28 00:47:34 +0000 | [diff] [blame] | 7837 | case PTP_OPC_AlbumArtist: |
| 7838 | if (artist != NULL) { |
| 7839 | prop = ptp_get_new_object_prop_entry(&props,&nrofprops); |
| 7840 | prop->ObjectHandle = *newid; |
| 7841 | prop->property = PTP_OPC_AlbumArtist; |
| 7842 | prop->datatype = PTP_DTC_STR; |
| 7843 | prop->propval.str = strdup(artist); |
| 7844 | } |
| 7845 | break; |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 7846 | case PTP_OPC_Artist: |
| 7847 | if (artist != NULL) { |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 7848 | prop = ptp_get_new_object_prop_entry(&props,&nrofprops); |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 7849 | prop->ObjectHandle = *newid; |
| 7850 | prop->property = PTP_OPC_Artist; |
| 7851 | prop->datatype = PTP_DTC_STR; |
| 7852 | prop->propval.str = strdup(artist); |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 7853 | } |
| 7854 | break; |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 7855 | case PTP_OPC_Composer: |
| 7856 | if (composer != NULL) { |
| 7857 | prop = ptp_get_new_object_prop_entry(&props,&nrofprops); |
| 7858 | prop->ObjectHandle = *newid; |
| 7859 | prop->property = PTP_OPC_Composer; |
| 7860 | prop->datatype = PTP_DTC_STR; |
| 7861 | prop->propval.str = strdup(composer); |
| 7862 | } |
| 7863 | break; |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 7864 | case PTP_OPC_Genre: |
| 7865 | if (genre != NULL) { |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 7866 | prop = ptp_get_new_object_prop_entry(&props,&nrofprops); |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 7867 | prop->ObjectHandle = *newid; |
| 7868 | prop->property = PTP_OPC_Genre; |
| 7869 | prop->datatype = PTP_DTC_STR; |
| 7870 | prop->propval.str = strdup(genre); |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 7871 | } |
| 7872 | break; |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 7873 | case PTP_OPC_DateModified: |
| 7874 | // Tag with current time if that is supported |
Linus Walleij | cf8dc2b | 2008-10-21 13:58:36 +0000 | [diff] [blame] | 7875 | if (!FLAG_CANNOT_HANDLE_DATEMODIFIED(ptp_usb)) { |
| 7876 | prop = ptp_get_new_object_prop_entry(&props,&nrofprops); |
| 7877 | prop->ObjectHandle = *newid; |
| 7878 | prop->property = PTP_OPC_DateModified; |
| 7879 | prop->datatype = PTP_DTC_STR; |
| 7880 | prop->propval.str = get_iso8601_stamp(); |
| 7881 | } |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 7882 | break; |
Linus Walleij | adce4a5 | 2007-04-23 07:28:11 +0000 | [diff] [blame] | 7883 | } |
rreardon | 508705f | 2006-11-19 21:27:22 +0000 | [diff] [blame] | 7884 | } |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 7885 | ptp_free_objectpropdesc(&opd); |
rreardon | 508705f | 2006-11-19 21:27:22 +0000 | [diff] [blame] | 7886 | } |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 7887 | free(properties); |
mopoke | 3136444 | 2006-11-20 04:53:04 +0000 | [diff] [blame] | 7888 | |
Linus Walleij | b7426d1 | 2006-11-25 23:19:11 +0000 | [diff] [blame] | 7889 | ret = ptp_mtp_sendobjectproplist(params, &store, &localph, newid, |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 7890 | objectformat, 0, props, nrofprops); |
mopoke | 3136444 | 2006-11-20 04:53:04 +0000 | [diff] [blame] | 7891 | |
rreardon | 508705f | 2006-11-19 21:27:22 +0000 | [diff] [blame] | 7892 | /* Free property list */ |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 7893 | ptp_destroy_object_prop_list(props, nrofprops); |
mopoke | 3136444 | 2006-11-20 04:53:04 +0000 | [diff] [blame] | 7894 | |
rreardon | 508705f | 2006-11-19 21:27:22 +0000 | [diff] [blame] | 7895 | if (ret != PTP_RC_OK) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 7896 | add_ptp_error_to_errorstack(device, ret, "create_new_abstract_list(): Could not send object property list."); |
rreardon | 508705f | 2006-11-19 21:27:22 +0000 | [diff] [blame] | 7897 | if (ret == PTP_RC_AccessDenied) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 7898 | add_ptp_error_to_errorstack(device, ret, "ACCESS DENIED."); |
rreardon | 508705f | 2006-11-19 21:27:22 +0000 | [diff] [blame] | 7899 | } |
| 7900 | return -1; |
| 7901 | } |
Linus Walleij | 08a5fe1 | 2009-09-08 21:28:58 +0000 | [diff] [blame] | 7902 | |
Richard Low | c3f5fc3 | 2007-07-29 09:40:50 +0000 | [diff] [blame] | 7903 | // now send the blank object |
Richard Low | a6c924c | 2006-12-29 17:44:28 +0000 | [diff] [blame] | 7904 | ret = ptp_sendobject(params, NULL, 0); |
| 7905 | if (ret != PTP_RC_OK) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 7906 | add_ptp_error_to_errorstack(device, ret, "create_new_abstract_list(): Could not send blank object data."); |
Richard Low | a6c924c | 2006-12-29 17:44:28 +0000 | [diff] [blame] | 7907 | return -1; |
| 7908 | } |
Linus Walleij | 629fe40 | 2006-12-12 23:39:15 +0000 | [diff] [blame] | 7909 | |
rreardon | 508705f | 2006-11-19 21:27:22 +0000 | [diff] [blame] | 7910 | } else if (ptp_operation_issupported(params,PTP_OC_SendObjectInfo)) { |
Linus Walleij | 629fe40 | 2006-12-12 23:39:15 +0000 | [diff] [blame] | 7911 | PTPObjectInfo new_object; |
| 7912 | |
| 7913 | new_object.Filename = fname; |
Linus Walleij | fec4d56 | 2008-06-01 22:30:36 +0000 | [diff] [blame] | 7914 | if (FLAG_ONLY_7BIT_FILENAMES(ptp_usb)) { |
Linus Walleij | d3b7857 | 2007-08-24 21:28:24 +0000 | [diff] [blame] | 7915 | strip_7bit_from_utf8(new_object.Filename); |
| 7916 | } |
Linus Walleij | 755c246 | 2011-03-04 20:11:01 +0100 | [diff] [blame] | 7917 | // At one point this had to be one |
| 7918 | new_object.ObjectCompressedSize = 0; |
Linus Walleij | 629fe40 | 2006-12-12 23:39:15 +0000 | [diff] [blame] | 7919 | new_object.ObjectFormat = objectformat; |
| 7920 | |
Linus Walleij | 2069848 | 2006-11-24 20:54:21 +0000 | [diff] [blame] | 7921 | // Create the object |
Linus Walleij | b7426d1 | 2006-11-25 23:19:11 +0000 | [diff] [blame] | 7922 | ret = ptp_sendobjectinfo(params, &store, &localph, newid, &new_object); |
Linus Walleij | 2069848 | 2006-11-24 20:54:21 +0000 | [diff] [blame] | 7923 | if (ret != PTP_RC_OK) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 7924 | add_ptp_error_to_errorstack(device, ret, "create_new_abstract_list(): Could not send object info (the playlist itself)."); |
Linus Walleij | 2069848 | 2006-11-24 20:54:21 +0000 | [diff] [blame] | 7925 | if (ret == PTP_RC_AccessDenied) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 7926 | add_ptp_error_to_errorstack(device, ret, "ACCESS DENIED."); |
Linus Walleij | 2069848 | 2006-11-24 20:54:21 +0000 | [diff] [blame] | 7927 | } |
| 7928 | return -1; |
| 7929 | } |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 7930 | // NOTE: don't destroy new_object objectinfo afterwards - the strings it contains are |
| 7931 | // not copies. |
Linus Walleij | 755c246 | 2011-03-04 20:11:01 +0100 | [diff] [blame] | 7932 | |
| 7933 | #if 0 |
Richard Low | a6c924c | 2006-12-29 17:44:28 +0000 | [diff] [blame] | 7934 | /* |
Linus Walleij | 755c246 | 2011-03-04 20:11:01 +0100 | [diff] [blame] | 7935 | * At one time we had to send this one blank data byte. |
| 7936 | * If we didn't, the handle will not be created and thus there is |
| 7937 | * no playlist. Possibly this was masking some bug, so removing it |
| 7938 | * now. |
Richard Low | a6c924c | 2006-12-29 17:44:28 +0000 | [diff] [blame] | 7939 | */ |
| 7940 | data[0] = '\0'; |
| 7941 | data[1] = '\0'; |
| 7942 | ret = ptp_sendobject(params, data, 1); |
| 7943 | if (ret != PTP_RC_OK) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 7944 | add_ptp_error_to_errorstack(device, ret, "create_new_abstract_list(): Could not send blank object data."); |
Richard Low | a6c924c | 2006-12-29 17:44:28 +0000 | [diff] [blame] | 7945 | return -1; |
| 7946 | } |
Linus Walleij | 755c246 | 2011-03-04 20:11:01 +0100 | [diff] [blame] | 7947 | #endif |
Linus Walleij | 08a5fe1 | 2009-09-08 21:28:58 +0000 | [diff] [blame] | 7948 | |
Richard Low | ff16bfa | 2008-09-26 19:23:08 +0000 | [diff] [blame] | 7949 | // set the properties one by one |
| 7950 | ret = ptp_mtp_getobjectpropssupported(params, objectformat, &propcnt, &properties); |
| 7951 | |
| 7952 | for (i=0;i<propcnt;i++) { |
| 7953 | PTPObjectPropDesc opd; |
Linus Walleij | 08a5fe1 | 2009-09-08 21:28:58 +0000 | [diff] [blame] | 7954 | |
Richard Low | ff16bfa | 2008-09-26 19:23:08 +0000 | [diff] [blame] | 7955 | ret = ptp_mtp_getobjectpropdesc(params, properties[i], objectformat, &opd); |
| 7956 | if (ret != PTP_RC_OK) { |
| 7957 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "create_new_abstract_list(): " |
| 7958 | "could not get property description."); |
| 7959 | } else if (opd.GetSet) { |
| 7960 | switch (properties[i]) { |
| 7961 | case PTP_OPC_Name: |
| 7962 | if (name != NULL) { |
Linus Walleij | 3758814 | 2008-10-16 19:10:47 +0000 | [diff] [blame] | 7963 | ret = set_object_string(device, *newid, PTP_OPC_Name, name); |
| 7964 | if (ret != 0) { |
| 7965 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "create_new_abstract_list(): could not set entity name."); |
| 7966 | return -1; |
| 7967 | } |
| 7968 | } |
Richard Low | ff16bfa | 2008-09-26 19:23:08 +0000 | [diff] [blame] | 7969 | break; |
| 7970 | case PTP_OPC_AlbumArtist: |
| 7971 | if (artist != NULL) { |
Linus Walleij | 3758814 | 2008-10-16 19:10:47 +0000 | [diff] [blame] | 7972 | ret = set_object_string(device, *newid, PTP_OPC_AlbumArtist, artist); |
| 7973 | if (ret != 0) { |
| 7974 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "create_new_abstract_list(): could not set entity album artist."); |
| 7975 | return -1; |
| 7976 | } |
| 7977 | } |
Richard Low | ff16bfa | 2008-09-26 19:23:08 +0000 | [diff] [blame] | 7978 | break; |
| 7979 | case PTP_OPC_Artist: |
| 7980 | if (artist != NULL) { |
Linus Walleij | 3758814 | 2008-10-16 19:10:47 +0000 | [diff] [blame] | 7981 | ret = set_object_string(device, *newid, PTP_OPC_Artist, artist); |
| 7982 | if (ret != 0) { |
| 7983 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "create_new_abstract_list(): could not set entity artist."); |
| 7984 | return -1; |
| 7985 | } |
| 7986 | } |
Richard Low | ff16bfa | 2008-09-26 19:23:08 +0000 | [diff] [blame] | 7987 | break; |
| 7988 | case PTP_OPC_Composer: |
| 7989 | if (composer != NULL) { |
Linus Walleij | 3758814 | 2008-10-16 19:10:47 +0000 | [diff] [blame] | 7990 | ret = set_object_string(device, *newid, PTP_OPC_Composer, composer); |
| 7991 | if (ret != 0) { |
| 7992 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "create_new_abstract_list(): could not set entity composer."); |
| 7993 | return -1; |
| 7994 | } |
| 7995 | } |
Richard Low | ff16bfa | 2008-09-26 19:23:08 +0000 | [diff] [blame] | 7996 | break; |
| 7997 | case PTP_OPC_Genre: |
| 7998 | if (genre != NULL) { |
Linus Walleij | 3758814 | 2008-10-16 19:10:47 +0000 | [diff] [blame] | 7999 | ret = set_object_string(device, *newid, PTP_OPC_Genre, genre); |
| 8000 | if (ret != 0) { |
| 8001 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "create_new_abstract_list(): could not set entity genre."); |
| 8002 | return -1; |
| 8003 | } |
| 8004 | } |
Richard Low | ff16bfa | 2008-09-26 19:23:08 +0000 | [diff] [blame] | 8005 | break; |
| 8006 | case PTP_OPC_DateModified: |
Linus Walleij | cf8dc2b | 2008-10-21 13:58:36 +0000 | [diff] [blame] | 8007 | if (!FLAG_CANNOT_HANDLE_DATEMODIFIED(ptp_usb)) { |
| 8008 | ret = set_object_string(device, *newid, PTP_OPC_DateModified, get_iso8601_stamp()); |
| 8009 | if (ret != 0) { |
| 8010 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "create_new_abstract_list(): could not set date modified."); |
| 8011 | return -1; |
| 8012 | } |
Linus Walleij | 3758814 | 2008-10-16 19:10:47 +0000 | [diff] [blame] | 8013 | } |
| 8014 | break; |
Richard Low | ff16bfa | 2008-09-26 19:23:08 +0000 | [diff] [blame] | 8015 | } |
| 8016 | } |
| 8017 | ptp_free_objectpropdesc(&opd); |
| 8018 | } |
| 8019 | free(properties); |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 8020 | } |
| 8021 | |
Linus Walleij | b7426d1 | 2006-11-25 23:19:11 +0000 | [diff] [blame] | 8022 | if (no_tracks > 0) { |
Linus Walleij | 1ccd586 | 2008-11-11 12:33:01 +0000 | [diff] [blame] | 8023 | // Add tracks to the list as object references. |
Linus Walleij | b7426d1 | 2006-11-25 23:19:11 +0000 | [diff] [blame] | 8024 | ret = ptp_mtp_setobjectreferences (params, *newid, (uint32_t *) tracks, no_tracks); |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 8025 | if (ret != PTP_RC_OK) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 8026 | add_ptp_error_to_errorstack(device, ret, "create_new_abstract_list(): could not add tracks as object references."); |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 8027 | return -1; |
| 8028 | } |
| 8029 | } |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 8030 | |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 8031 | add_object_to_cache(device, *newid); |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 8032 | |
| 8033 | return 0; |
| 8034 | } |
| 8035 | |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 8036 | /** |
Linus Walleij | 08a5fe1 | 2009-09-08 21:28:58 +0000 | [diff] [blame] | 8037 | * This updates the metadata and track listing |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 8038 | * for an abstract list. |
| 8039 | * @param device a pointer to the device that the abstract list |
| 8040 | * resides on. |
| 8041 | * @param name the name of the abstract list. |
| 8042 | * @param artist the artist of the abstract list or NULL. |
| 8043 | * @param genre the genre of the abstract list or NULL. |
| 8044 | * @param objecthandle the object to be updated. |
| 8045 | * @param objectformat the abstract list type to update. |
| 8046 | * @param tracks an array of tracks to associate with this list. |
| 8047 | * @param no_tracks the number of tracks in the list. |
| 8048 | * @return 0 on success, any other value means failure. |
| 8049 | */ |
| 8050 | static int update_abstract_list(LIBMTP_mtpdevice_t *device, |
| 8051 | char const * const name, |
| 8052 | char const * const artist, |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 8053 | char const * const composer, |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 8054 | char const * const genre, |
| 8055 | uint32_t const objecthandle, |
| 8056 | uint16_t const objectformat, |
| 8057 | uint32_t const * const tracks, |
| 8058 | uint32_t const no_tracks) |
| 8059 | { |
| 8060 | uint16_t ret; |
| 8061 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | f9267e9 | 2007-10-15 21:07:37 +0000 | [diff] [blame] | 8062 | PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo; |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 8063 | uint16_t *properties = NULL; |
| 8064 | uint32_t propcnt = 0; |
| 8065 | int i; |
Linus Walleij | 08a5fe1 | 2009-09-08 21:28:58 +0000 | [diff] [blame] | 8066 | |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 8067 | // First see which properties can be set |
| 8068 | // i.e only try to update this metadata for object tags that exist on the current player. |
| 8069 | ret = ptp_mtp_getobjectpropssupported(params, objectformat, &propcnt, &properties); |
| 8070 | if (ret != PTP_RC_OK) { |
| 8071 | // Just bail out for now, nothing is ever set. |
| 8072 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "update_abstract_list(): " |
| 8073 | "could not retrieve supported object properties."); |
| 8074 | return -1; |
| 8075 | } |
Linus Walleij | f9267e9 | 2007-10-15 21:07:37 +0000 | [diff] [blame] | 8076 | if (ptp_operation_issupported(params,PTP_OC_MTP_SetObjPropList) && |
Linus Walleij | fec4d56 | 2008-06-01 22:30:36 +0000 | [diff] [blame] | 8077 | !FLAG_BROKEN_SET_OBJECT_PROPLIST(ptp_usb)) { |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 8078 | MTPProperties *props = NULL; |
| 8079 | MTPProperties *prop = NULL; |
| 8080 | int nrofprops = 0; |
Linus Walleij | 08a5fe1 | 2009-09-08 21:28:58 +0000 | [diff] [blame] | 8081 | |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 8082 | for (i=0;i<propcnt;i++) { |
| 8083 | PTPObjectPropDesc opd; |
| 8084 | |
| 8085 | ret = ptp_mtp_getobjectpropdesc(params, properties[i], objectformat, &opd); |
| 8086 | if (ret != PTP_RC_OK) { |
| 8087 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "update_abstract_list(): " |
| 8088 | "could not get property description."); |
| 8089 | } else if (opd.GetSet) { |
| 8090 | switch (properties[i]) { |
| 8091 | case PTP_OPC_Name: |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 8092 | prop = ptp_get_new_object_prop_entry(&props, &nrofprops); |
Linus Walleij | 08a5fe1 | 2009-09-08 21:28:58 +0000 | [diff] [blame] | 8093 | prop->ObjectHandle = objecthandle; |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 8094 | prop->property = PTP_OPC_Name; |
| 8095 | prop->datatype = PTP_DTC_STR; |
| 8096 | if (name != NULL) |
| 8097 | prop->propval.str = strdup(name); |
| 8098 | break; |
Linus Walleij | 0b75ab6 | 2007-12-28 00:47:34 +0000 | [diff] [blame] | 8099 | case PTP_OPC_AlbumArtist: |
| 8100 | if (artist != NULL) { |
| 8101 | prop = ptp_get_new_object_prop_entry(&props, &nrofprops); |
| 8102 | prop->ObjectHandle = objecthandle; |
| 8103 | prop->property = PTP_OPC_AlbumArtist; |
| 8104 | prop->datatype = PTP_DTC_STR; |
| 8105 | prop->propval.str = strdup(artist); |
| 8106 | } |
| 8107 | break; |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 8108 | case PTP_OPC_Artist: |
| 8109 | if (artist != NULL) { |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 8110 | prop = ptp_get_new_object_prop_entry(&props, &nrofprops); |
Linus Walleij | 0b75ab6 | 2007-12-28 00:47:34 +0000 | [diff] [blame] | 8111 | prop->ObjectHandle = objecthandle; |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 8112 | prop->property = PTP_OPC_Artist; |
| 8113 | prop->datatype = PTP_DTC_STR; |
| 8114 | prop->propval.str = strdup(artist); |
| 8115 | } |
| 8116 | break; |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 8117 | case PTP_OPC_Composer: |
| 8118 | if (composer != NULL) { |
| 8119 | prop = ptp_get_new_object_prop_entry(&props, &nrofprops); |
| 8120 | prop->ObjectHandle = objecthandle; |
| 8121 | prop->property = PTP_OPC_Composer; |
| 8122 | prop->datatype = PTP_DTC_STR; |
| 8123 | prop->propval.str = strdup(composer); |
| 8124 | } |
| 8125 | break; |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 8126 | case PTP_OPC_Genre: |
| 8127 | if (genre != NULL) { |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 8128 | prop = ptp_get_new_object_prop_entry(&props, &nrofprops); |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 8129 | prop->ObjectHandle = objecthandle; |
| 8130 | prop->property = PTP_OPC_Genre; |
| 8131 | prop->datatype = PTP_DTC_STR; |
| 8132 | prop->propval.str = strdup(genre); |
| 8133 | } |
| 8134 | break; |
| 8135 | case PTP_OPC_DateModified: |
Linus Walleij | cf8dc2b | 2008-10-21 13:58:36 +0000 | [diff] [blame] | 8136 | if (!FLAG_CANNOT_HANDLE_DATEMODIFIED(ptp_usb)) { |
Linus Walleij | 3758814 | 2008-10-16 19:10:47 +0000 | [diff] [blame] | 8137 | // Tag with current time if that is supported |
| 8138 | prop = ptp_get_new_object_prop_entry(&props, &nrofprops); |
| 8139 | prop->ObjectHandle = objecthandle; |
| 8140 | prop->property = PTP_OPC_DateModified; |
| 8141 | prop->datatype = PTP_DTC_STR; |
| 8142 | prop->propval.str = get_iso8601_stamp(); |
| 8143 | } |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 8144 | break; |
| 8145 | default: |
| 8146 | break; |
| 8147 | } |
| 8148 | } |
| 8149 | ptp_free_objectpropdesc(&opd); |
| 8150 | } |
Linus Walleij | 08a5fe1 | 2009-09-08 21:28:58 +0000 | [diff] [blame] | 8151 | |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 8152 | // proplist could be NULL if we can't write any properties |
| 8153 | if (props != NULL) { |
| 8154 | ret = ptp_mtp_setobjectproplist(params, props, nrofprops); |
| 8155 | |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 8156 | ptp_destroy_object_prop_list(props, nrofprops); |
Linus Walleij | 08a5fe1 | 2009-09-08 21:28:58 +0000 | [diff] [blame] | 8157 | |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 8158 | if (ret != PTP_RC_OK) { |
| 8159 | // TODO: return error of which property we couldn't set |
| 8160 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "update_abstract_list(): " |
| 8161 | "could not set object property list."); |
Richard Low | 016e373 | 2007-09-23 14:53:46 +0000 | [diff] [blame] | 8162 | free(properties); |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 8163 | return -1; |
| 8164 | } |
| 8165 | } |
Linus Walleij | 08a5fe1 | 2009-09-08 21:28:58 +0000 | [diff] [blame] | 8166 | |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 8167 | } else if (ptp_operation_issupported(params,PTP_OC_MTP_SetObjectPropValue)) { |
| 8168 | for (i=0;i<propcnt;i++) { |
| 8169 | switch (properties[i]) { |
| 8170 | case PTP_OPC_Name: |
| 8171 | // Update title |
| 8172 | ret = set_object_string(device, objecthandle, PTP_OPC_Name, name); |
| 8173 | if (ret != 0) { |
| 8174 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "update_abstract_list(): " |
| 8175 | "could not set title."); |
| 8176 | } |
| 8177 | break; |
Linus Walleij | 0b75ab6 | 2007-12-28 00:47:34 +0000 | [diff] [blame] | 8178 | case PTP_OPC_AlbumArtist: |
| 8179 | // Update album artist |
| 8180 | ret = set_object_string(device, objecthandle, PTP_OPC_AlbumArtist, artist); |
| 8181 | if (ret != 0) { |
| 8182 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "update_abstract_list(): " |
| 8183 | "could not set album artist name."); |
| 8184 | } |
| 8185 | break; |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 8186 | case PTP_OPC_Artist: |
| 8187 | // Update artist |
| 8188 | ret = set_object_string(device, objecthandle, PTP_OPC_Artist, artist); |
| 8189 | if (ret != 0) { |
| 8190 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "update_abstract_list(): " |
| 8191 | "could not set artist name."); |
| 8192 | } |
Marcus Meissner | 113fa29 | 2015-04-09 23:11:52 +0200 | [diff] [blame] | 8193 | break; |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 8194 | case PTP_OPC_Composer: |
| 8195 | // Update composer |
| 8196 | ret = set_object_string(device, objecthandle, PTP_OPC_Composer, composer); |
| 8197 | if (ret != 0) { |
| 8198 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "update_abstract_list(): " |
| 8199 | "could not set composer name."); |
| 8200 | } |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 8201 | break; |
| 8202 | case PTP_OPC_Genre: |
Linus Walleij | b7e8f97 | 2010-01-19 00:38:11 +0000 | [diff] [blame] | 8203 | // Update genre (but only if valid) |
| 8204 | if(genre) { |
| 8205 | ret = set_object_string(device, objecthandle, PTP_OPC_Genre, genre); |
| 8206 | if (ret != 0) { |
| 8207 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "update_abstract_list(): " |
| 8208 | "could not set genre."); |
| 8209 | } |
| 8210 | } |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 8211 | break; |
| 8212 | case PTP_OPC_DateModified: |
| 8213 | // Update date modified |
Linus Walleij | cf8dc2b | 2008-10-21 13:58:36 +0000 | [diff] [blame] | 8214 | if (!FLAG_CANNOT_HANDLE_DATEMODIFIED(ptp_usb)) { |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 8215 | char *tmpdate = get_iso8601_stamp(); |
| 8216 | ret = set_object_string(device, objecthandle, PTP_OPC_DateModified, tmpdate); |
| 8217 | if (ret != 0) { |
| 8218 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "update_abstract_list(): " |
| 8219 | "could not set modification date."); |
| 8220 | } |
| 8221 | free(tmpdate); |
| 8222 | } |
Marcus Meissner | 113fa29 | 2015-04-09 23:11:52 +0200 | [diff] [blame] | 8223 | break; |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 8224 | default: |
| 8225 | break; |
| 8226 | } |
| 8227 | } |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 8228 | } else { |
| 8229 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "update_abstract_list(): " |
| 8230 | "Your device doesn't seem to support any known way of setting metadata."); |
Richard Low | 016e373 | 2007-09-23 14:53:46 +0000 | [diff] [blame] | 8231 | free(properties); |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 8232 | return -1; |
| 8233 | } |
Linus Walleij | 08a5fe1 | 2009-09-08 21:28:58 +0000 | [diff] [blame] | 8234 | |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 8235 | // Then the object references... |
Linus Walleij | 1ccd586 | 2008-11-11 12:33:01 +0000 | [diff] [blame] | 8236 | ret = ptp_mtp_setobjectreferences (params, objecthandle, (uint32_t *) tracks, no_tracks); |
| 8237 | if (ret != PTP_RC_OK) { |
| 8238 | add_ptp_error_to_errorstack(device, ret, "update_abstract_list(): could not add tracks as object references."); |
| 8239 | free(properties); |
| 8240 | return -1; |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 8241 | } |
| 8242 | |
Richard Low | 016e373 | 2007-09-23 14:53:46 +0000 | [diff] [blame] | 8243 | free(properties); |
Linus Walleij | 7752b95 | 2007-10-19 20:11:46 +0000 | [diff] [blame] | 8244 | |
| 8245 | update_metadata_cache(device, objecthandle); |
Linus Walleij | 08a5fe1 | 2009-09-08 21:28:58 +0000 | [diff] [blame] | 8246 | |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 8247 | return 0; |
| 8248 | } |
| 8249 | |
Linus Walleij | b7426d1 | 2006-11-25 23:19:11 +0000 | [diff] [blame] | 8250 | |
| 8251 | /** |
| 8252 | * This routine creates a new playlist based on the metadata |
| 8253 | * supplied. If the <code>tracks</code> field of the metadata |
| 8254 | * contains a track listing, these tracks will be added to the |
| 8255 | * playlist. |
| 8256 | * @param device a pointer to the device to create the new playlist on. |
| 8257 | * @param metadata the metadata for the new playlist. If the function |
| 8258 | * exits with success, the <code>playlist_id</code> field of this |
| 8259 | * struct will contain the new playlist ID of the playlist. |
Linus Walleij | 2fe43e4 | 2009-01-30 20:31:26 +0000 | [diff] [blame] | 8260 | * <ul> |
| 8261 | * <li><code>metadata->parent_id</code> should be set to the parent |
| 8262 | * (e.g. folder) to store this track in. Since some |
| 8263 | * devices are a bit picky about where files |
| 8264 | * are placed, a default folder will be chosen if libmtp |
| 8265 | * has detected one for the current filetype and this |
| 8266 | * parameter is set to 0. If this is 0 and no default folder |
| 8267 | * can be found, the file will be stored in the root folder. |
| 8268 | * <li><code>metadata->storage_id</code> should be set to the |
| 8269 | * desired storage (e.g. memory card or whatever your device |
| 8270 | * presents) to store this track in. Setting this to 0 will store |
| 8271 | * the track on the primary storage. |
| 8272 | * </ul> |
Linus Walleij | b7426d1 | 2006-11-25 23:19:11 +0000 | [diff] [blame] | 8273 | * @return 0 on success, any other value means failure. |
| 8274 | * @see LIBMTP_Update_Playlist() |
| 8275 | * @see LIBMTP_Delete_Object() |
| 8276 | */ |
| 8277 | int LIBMTP_Create_New_Playlist(LIBMTP_mtpdevice_t *device, |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 8278 | LIBMTP_playlist_t * const metadata) |
Linus Walleij | b7426d1 | 2006-11-25 23:19:11 +0000 | [diff] [blame] | 8279 | { |
Linus Walleij | a4e6bdc | 2008-05-23 22:31:53 +0000 | [diff] [blame] | 8280 | PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo; |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 8281 | uint32_t localph = metadata->parent_id; |
Linus Walleij | b7426d1 | 2006-11-25 23:19:11 +0000 | [diff] [blame] | 8282 | |
| 8283 | // Use a default folder if none given |
| 8284 | if (localph == 0) { |
Linus Walleij | afac36b | 2009-01-30 20:39:13 +0000 | [diff] [blame] | 8285 | if (device->default_playlist_folder != 0) |
| 8286 | localph = device->default_playlist_folder; |
| 8287 | else |
| 8288 | localph = device->default_music_folder; |
Linus Walleij | b7426d1 | 2006-11-25 23:19:11 +0000 | [diff] [blame] | 8289 | } |
Linus Walleij | 5ce59db | 2008-03-12 21:22:58 +0000 | [diff] [blame] | 8290 | metadata->parent_id = localph; |
Linus Walleij | b7426d1 | 2006-11-25 23:19:11 +0000 | [diff] [blame] | 8291 | |
Linus Walleij | f3c4405 | 2008-08-16 21:14:56 +0000 | [diff] [blame] | 8292 | // Samsung needs its own special type of playlists |
| 8293 | if(FLAG_PLAYLIST_SPL(ptp_usb)) { |
| 8294 | return playlist_t_to_spl(device, metadata); |
| 8295 | } |
| 8296 | |
Linus Walleij | b7426d1 | 2006-11-25 23:19:11 +0000 | [diff] [blame] | 8297 | // Just create a new abstract audio/video playlist... |
| 8298 | return create_new_abstract_list(device, |
| 8299 | metadata->name, |
Linus Walleij | adce4a5 | 2007-04-23 07:28:11 +0000 | [diff] [blame] | 8300 | NULL, |
| 8301 | NULL, |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 8302 | NULL, |
Linus Walleij | b7426d1 | 2006-11-25 23:19:11 +0000 | [diff] [blame] | 8303 | localph, |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 8304 | metadata->storage_id, |
Linus Walleij | b7426d1 | 2006-11-25 23:19:11 +0000 | [diff] [blame] | 8305 | PTP_OFC_MTP_AbstractAudioVideoPlaylist, |
Linus Walleij | a4e6bdc | 2008-05-23 22:31:53 +0000 | [diff] [blame] | 8306 | get_playlist_extension(ptp_usb), |
Linus Walleij | b7426d1 | 2006-11-25 23:19:11 +0000 | [diff] [blame] | 8307 | &metadata->playlist_id, |
| 8308 | metadata->tracks, |
| 8309 | metadata->no_tracks); |
| 8310 | } |
| 8311 | |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 8312 | /** |
| 8313 | * This routine updates a playlist based on the metadata |
| 8314 | * supplied. If the <code>tracks</code> field of the metadata |
| 8315 | * contains a track listing, these tracks will be added to the |
| 8316 | * playlist in place of those already present, i.e. the |
alistair_boyle | 5e5fcb7 | 2008-11-17 14:38:19 +0000 | [diff] [blame] | 8317 | * previous track listing will be deleted. For Samsung devices the |
| 8318 | * playlist id (metadata->playlist_id) is likely to change. |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 8319 | * @param device a pointer to the device to create the new playlist on. |
| 8320 | * @param metadata the metadata for the playlist to be updated. |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 8321 | * notice that the field <code>playlist_id</code> |
alistair_boyle | 5e5fcb7 | 2008-11-17 14:38:19 +0000 | [diff] [blame] | 8322 | * must contain the apropriate playlist ID. Playlist ID |
| 8323 | * be modified to a new playlist ID by the time the |
| 8324 | * function returns since edit-in-place is not always possible. |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 8325 | * @return 0 on success, any other value means failure. |
| 8326 | * @see LIBMTP_Create_New_Playlist() |
| 8327 | * @see LIBMTP_Delete_Object() |
| 8328 | */ |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 8329 | int LIBMTP_Update_Playlist(LIBMTP_mtpdevice_t *device, |
alistair_boyle | 5e5fcb7 | 2008-11-17 14:38:19 +0000 | [diff] [blame] | 8330 | LIBMTP_playlist_t * const metadata) |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 8331 | { |
Linus Walleij | f3c4405 | 2008-08-16 21:14:56 +0000 | [diff] [blame] | 8332 | |
| 8333 | // Samsung needs its own special type of playlists |
| 8334 | PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo; |
| 8335 | if(FLAG_PLAYLIST_SPL(ptp_usb)) { |
| 8336 | return update_spl_playlist(device, metadata); |
| 8337 | } |
| 8338 | |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 8339 | return update_abstract_list(device, |
| 8340 | metadata->name, |
| 8341 | NULL, |
| 8342 | NULL, |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 8343 | NULL, |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 8344 | metadata->playlist_id, |
| 8345 | PTP_OFC_MTP_AbstractAudioVideoPlaylist, |
| 8346 | metadata->tracks, |
| 8347 | metadata->no_tracks); |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 8348 | } |
Linus Walleij | aa4b075 | 2006-07-26 22:21:04 +0000 | [diff] [blame] | 8349 | |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 8350 | /** |
| 8351 | * This creates a new album metadata structure and allocates memory |
| 8352 | * for it. Notice that if you add strings to this structure they |
| 8353 | * will be freed by the corresponding <code>LIBMTP_destroy_album_t</code> |
| 8354 | * operation later, so be careful of using strdup() when assigning |
| 8355 | * strings. |
| 8356 | * |
| 8357 | * @return a pointer to the newly allocated metadata structure. |
| 8358 | * @see LIBMTP_destroy_album_t() |
| 8359 | */ |
| 8360 | LIBMTP_album_t *LIBMTP_new_album_t(void) |
| 8361 | { |
| 8362 | LIBMTP_album_t *new = (LIBMTP_album_t *) malloc(sizeof(LIBMTP_album_t)); |
| 8363 | if (new == NULL) { |
| 8364 | return NULL; |
| 8365 | } |
| 8366 | new->album_id = 0; |
Linus Walleij | 5ce59db | 2008-03-12 21:22:58 +0000 | [diff] [blame] | 8367 | new->parent_id = 0; |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 8368 | new->storage_id = 0; |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 8369 | new->name = NULL; |
Linus Walleij | adce4a5 | 2007-04-23 07:28:11 +0000 | [diff] [blame] | 8370 | new->artist = NULL; |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 8371 | new->composer = NULL; |
Linus Walleij | adce4a5 | 2007-04-23 07:28:11 +0000 | [diff] [blame] | 8372 | new->genre = NULL; |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 8373 | new->tracks = NULL; |
| 8374 | new->no_tracks = 0; |
| 8375 | new->next = NULL; |
| 8376 | return new; |
| 8377 | } |
| 8378 | |
| 8379 | /** |
| 8380 | * This recursively deletes the memory for an album structure |
| 8381 | * |
| 8382 | * @param album structure to destroy |
| 8383 | * @see LIBMTP_new_album_t() |
| 8384 | */ |
| 8385 | void LIBMTP_destroy_album_t(LIBMTP_album_t *album) |
| 8386 | { |
| 8387 | if (album == NULL) { |
| 8388 | return; |
| 8389 | } |
| 8390 | if (album->name != NULL) |
| 8391 | free(album->name); |
Linus Walleij | adce4a5 | 2007-04-23 07:28:11 +0000 | [diff] [blame] | 8392 | if (album->artist != NULL) |
| 8393 | free(album->artist); |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 8394 | if (album->composer != NULL) |
| 8395 | free(album->composer); |
Linus Walleij | adce4a5 | 2007-04-23 07:28:11 +0000 | [diff] [blame] | 8396 | if (album->genre != NULL) |
| 8397 | free(album->genre); |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 8398 | if (album->tracks != NULL) |
| 8399 | free(album->tracks); |
| 8400 | free(album); |
| 8401 | return; |
| 8402 | } |
| 8403 | |
| 8404 | /** |
Linus Walleij | 08a5fe1 | 2009-09-08 21:28:58 +0000 | [diff] [blame] | 8405 | * This function maps and copies a property onto the album metadata if applicable. |
| 8406 | */ |
| 8407 | static void pick_property_to_album_metadata(LIBMTP_mtpdevice_t *device, |
| 8408 | MTPProperties *prop, LIBMTP_album_t *alb) |
| 8409 | { |
| 8410 | switch (prop->property) { |
| 8411 | case PTP_OPC_Name: |
| 8412 | if (prop->propval.str != NULL) |
| 8413 | alb->name = strdup(prop->propval.str); |
| 8414 | else |
| 8415 | alb->name = NULL; |
| 8416 | break; |
| 8417 | case PTP_OPC_AlbumArtist: |
| 8418 | if (prop->propval.str != NULL) { |
| 8419 | // This should take precedence over plain "Artist" |
| 8420 | if (alb->artist != NULL) |
| 8421 | free(alb->artist); |
| 8422 | alb->artist = strdup(prop->propval.str); |
| 8423 | } else |
| 8424 | alb->artist = NULL; |
| 8425 | break; |
| 8426 | case PTP_OPC_Artist: |
| 8427 | if (prop->propval.str != NULL) { |
| 8428 | // Only use of AlbumArtist is not set |
| 8429 | if (alb->artist == NULL) |
| 8430 | alb->artist = strdup(prop->propval.str); |
| 8431 | } else |
| 8432 | alb->artist = NULL; |
| 8433 | break; |
| 8434 | case PTP_OPC_Composer: |
| 8435 | if (prop->propval.str != NULL) |
| 8436 | alb->composer = strdup(prop->propval.str); |
| 8437 | else |
| 8438 | alb->composer = NULL; |
| 8439 | break; |
| 8440 | case PTP_OPC_Genre: |
| 8441 | if (prop->propval.str != NULL) |
| 8442 | alb->genre = strdup(prop->propval.str); |
| 8443 | else |
| 8444 | alb->genre = NULL; |
| 8445 | break; |
| 8446 | } |
| 8447 | } |
| 8448 | |
| 8449 | /** |
| 8450 | * This function retrieves the album metadata for an album |
| 8451 | * given by a unique ID. |
| 8452 | * @param device a pointer to the device to get the track metadata off. |
| 8453 | * @param alb an album metadata metadata set to fill in. |
| 8454 | */ |
| 8455 | static void get_album_metadata(LIBMTP_mtpdevice_t *device, |
| 8456 | LIBMTP_album_t *alb) |
| 8457 | { |
| 8458 | uint16_t ret; |
| 8459 | PTPParams *params = (PTPParams *) device->params; |
| 8460 | uint32_t i; |
| 8461 | MTPProperties *prop; |
| 8462 | PTPObject *ob; |
| 8463 | |
| 8464 | /* |
| 8465 | * If we have a cached, large set of metadata, then use it! |
| 8466 | */ |
| 8467 | ret = ptp_object_want(params, alb->album_id, PTPOBJECT_MTPPROPLIST_LOADED, &ob); |
| 8468 | if (ob->mtpprops) { |
| 8469 | prop = ob->mtpprops; |
| 8470 | for (i=0;i<ob->nrofmtpprops;i++,prop++) |
| 8471 | pick_property_to_album_metadata(device, prop, alb); |
| 8472 | } else { |
| 8473 | uint16_t *props = NULL; |
| 8474 | uint32_t propcnt = 0; |
| 8475 | |
| 8476 | // First see which properties can be retrieved for albums |
| 8477 | ret = ptp_mtp_getobjectpropssupported(params, PTP_OFC_MTP_AbstractAudioAlbum, &propcnt, &props); |
| 8478 | if (ret != PTP_RC_OK) { |
| 8479 | add_ptp_error_to_errorstack(device, ret, "get_album_metadata(): call to ptp_mtp_getobjectpropssupported() failed."); |
| 8480 | // Just bail out for now, nothing is ever set. |
| 8481 | return; |
| 8482 | } else { |
| 8483 | for (i=0;i<propcnt;i++) { |
| 8484 | switch (props[i]) { |
| 8485 | case PTP_OPC_Name: |
| 8486 | alb->name = get_string_from_object(device, ob->oid, PTP_OPC_Name); |
| 8487 | break; |
| 8488 | case PTP_OPC_AlbumArtist: |
| 8489 | if (alb->artist != NULL) |
| 8490 | free(alb->artist); |
| 8491 | alb->artist = get_string_from_object(device, ob->oid, PTP_OPC_AlbumArtist); |
| 8492 | break; |
| 8493 | case PTP_OPC_Artist: |
| 8494 | if (alb->artist == NULL) |
| 8495 | alb->artist = get_string_from_object(device, ob->oid, PTP_OPC_Artist); |
| 8496 | break; |
| 8497 | case PTP_OPC_Composer: |
| 8498 | alb->composer = get_string_from_object(device, ob->oid, PTP_OPC_Composer); |
| 8499 | break; |
| 8500 | case PTP_OPC_Genre: |
| 8501 | alb->genre = get_string_from_object(device, ob->oid, PTP_OPC_Genre); |
| 8502 | break; |
| 8503 | default: |
| 8504 | break; |
| 8505 | } |
| 8506 | } |
| 8507 | free(props); |
| 8508 | } |
| 8509 | } |
| 8510 | } |
| 8511 | |
nicklas79 | 5e0100b | 2009-11-07 19:53:46 +0000 | [diff] [blame] | 8512 | |
Linus Walleij | 08a5fe1 | 2009-09-08 21:28:58 +0000 | [diff] [blame] | 8513 | /** |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 8514 | * This function returns a list of the albums available on the |
| 8515 | * device. |
| 8516 | * |
| 8517 | * @param device a pointer to the device to get the album listing from. |
| 8518 | * @return an album list on success, else NULL. If there are no albums |
| 8519 | * on the device, NULL will be returned as well. |
| 8520 | * @see LIBMTP_Get_Album() |
| 8521 | */ |
| 8522 | LIBMTP_album_t *LIBMTP_Get_Album_List(LIBMTP_mtpdevice_t *device) |
| 8523 | { |
nicklas79 | 5e0100b | 2009-11-07 19:53:46 +0000 | [diff] [blame] | 8524 | // Read all storage devices |
| 8525 | return LIBMTP_Get_Album_List_For_Storage(device, 0); |
| 8526 | } |
| 8527 | |
| 8528 | |
| 8529 | /** |
| 8530 | * This function returns a list of the albums available on the |
| 8531 | * device. You can filter on the storage ID. |
| 8532 | * |
| 8533 | * @param device a pointer to the device to get the album listing from. |
| 8534 | * @param storage_id ID of device storage (if null, all storages) |
| 8535 | * |
| 8536 | * @return an album list on success, else NULL. If there are no albums |
| 8537 | * on the device, NULL will be returned as well. |
| 8538 | * @see LIBMTP_Get_Album() |
| 8539 | */ |
| 8540 | LIBMTP_album_t *LIBMTP_Get_Album_List_For_Storage(LIBMTP_mtpdevice_t *device, uint32_t const storage_id) |
| 8541 | { |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 8542 | PTPParams *params = (PTPParams *) device->params; |
| 8543 | LIBMTP_album_t *retalbums = NULL; |
| 8544 | LIBMTP_album_t *curalbum = NULL; |
| 8545 | uint32_t i; |
| 8546 | |
| 8547 | // Get all the handles if we haven't already done that |
Linus Walleij | 08a5fe1 | 2009-09-08 21:28:58 +0000 | [diff] [blame] | 8548 | if (params->nrofobjects == 0) |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 8549 | flush_handles(device); |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 8550 | |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 8551 | for (i = 0; i < params->nrofobjects; i++) { |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 8552 | LIBMTP_album_t *alb; |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 8553 | PTPObject *ob; |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 8554 | uint16_t ret; |
| 8555 | |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 8556 | ob = ¶ms->objects[i]; |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 8557 | |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 8558 | // Ignore stuff that isn't an album |
Linus Walleij | 08a5fe1 | 2009-09-08 21:28:58 +0000 | [diff] [blame] | 8559 | if ( ob->oi.ObjectFormat != PTP_OFC_MTP_AbstractAudioAlbum ) |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 8560 | continue; |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 8561 | |
nicklas79 | 5e0100b | 2009-11-07 19:53:46 +0000 | [diff] [blame] | 8562 | // Ignore stuff that isn't into the storage device |
| 8563 | if ((storage_id != 0) && (ob->oi.StorageID != storage_id )) |
| 8564 | continue; |
| 8565 | |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 8566 | // Allocate a new album type |
| 8567 | alb = LIBMTP_new_album_t(); |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 8568 | alb->album_id = ob->oid; |
| 8569 | alb->parent_id = ob->oi.ParentObject; |
| 8570 | alb->storage_id = ob->oi.StorageID; |
Linus Walleij | 5ce59db | 2008-03-12 21:22:58 +0000 | [diff] [blame] | 8571 | |
Linus Walleij | 08a5fe1 | 2009-09-08 21:28:58 +0000 | [diff] [blame] | 8572 | // Fetch supported metadata |
| 8573 | get_album_metadata(device, alb); |
| 8574 | |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 8575 | // Then get the track listing for this album |
| 8576 | ret = ptp_mtp_getobjectreferences(params, alb->album_id, &alb->tracks, &alb->no_tracks); |
| 8577 | if (ret != PTP_RC_OK) { |
| 8578 | add_ptp_error_to_errorstack(device, ret, "LIBMTP_Get_Album_List(): Could not get object references."); |
| 8579 | alb->tracks = NULL; |
| 8580 | alb->no_tracks = 0; |
| 8581 | } |
Linus Walleij | 08a5fe1 | 2009-09-08 21:28:58 +0000 | [diff] [blame] | 8582 | |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 8583 | // Add album to a list that will be returned afterwards. |
| 8584 | if (retalbums == NULL) { |
| 8585 | retalbums = alb; |
| 8586 | curalbum = alb; |
| 8587 | } else { |
| 8588 | curalbum->next = alb; |
| 8589 | curalbum = alb; |
| 8590 | } |
Linus Walleij | 08a5fe1 | 2009-09-08 21:28:58 +0000 | [diff] [blame] | 8591 | |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 8592 | } |
| 8593 | return retalbums; |
| 8594 | } |
| 8595 | |
| 8596 | /** |
| 8597 | * This function retrieves an individual album from the device. |
| 8598 | * @param device a pointer to the device to get the album from. |
| 8599 | * @param albid the unique ID of the album to retrieve. |
| 8600 | * @return a valid album metadata or NULL on failure. |
| 8601 | * @see LIBMTP_Get_Album_List() |
| 8602 | */ |
| 8603 | LIBMTP_album_t *LIBMTP_Get_Album(LIBMTP_mtpdevice_t *device, uint32_t const albid) |
| 8604 | { |
| 8605 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 8606 | uint16_t ret; |
| 8607 | PTPObject *ob; |
| 8608 | LIBMTP_album_t *alb; |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 8609 | |
| 8610 | // Get all the handles if we haven't already done that |
Linus Walleij | 08a5fe1 | 2009-09-08 21:28:58 +0000 | [diff] [blame] | 8611 | if (params->nrofobjects == 0) |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 8612 | flush_handles(device); |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 8613 | |
Linus Walleij | 08a5fe1 | 2009-09-08 21:28:58 +0000 | [diff] [blame] | 8614 | ret = ptp_object_want(params, albid, PTPOBJECT_OBJECTINFO_LOADED, &ob); |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 8615 | if (ret != PTP_RC_OK) |
| 8616 | return NULL; |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 8617 | |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 8618 | // Ignore stuff that isn't an album |
Linus Walleij | 08a5fe1 | 2009-09-08 21:28:58 +0000 | [diff] [blame] | 8619 | if (ob->oi.ObjectFormat != PTP_OFC_MTP_AbstractAudioAlbum) |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 8620 | return NULL; |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 8621 | |
| 8622 | // Allocate a new album type |
| 8623 | alb = LIBMTP_new_album_t(); |
| 8624 | alb->album_id = ob->oid; |
| 8625 | alb->parent_id = ob->oi.ParentObject; |
| 8626 | alb->storage_id = ob->oi.StorageID; |
| 8627 | |
Linus Walleij | 08a5fe1 | 2009-09-08 21:28:58 +0000 | [diff] [blame] | 8628 | // Fetch supported metadata |
| 8629 | get_album_metadata(device, alb); |
| 8630 | |
| 8631 | // Then get the track listing for this album |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 8632 | ret = ptp_mtp_getobjectreferences(params, alb->album_id, &alb->tracks, &alb->no_tracks); |
| 8633 | if (ret != PTP_RC_OK) { |
| 8634 | add_ptp_error_to_errorstack(device, ret, "LIBMTP_Get_Album: Could not get object references."); |
| 8635 | alb->tracks = NULL; |
| 8636 | alb->no_tracks = 0; |
| 8637 | } |
Linus Walleij | 08a5fe1 | 2009-09-08 21:28:58 +0000 | [diff] [blame] | 8638 | |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 8639 | return alb; |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 8640 | } |
| 8641 | |
| 8642 | /** |
| 8643 | * This routine creates a new album based on the metadata |
| 8644 | * supplied. If the <code>tracks</code> field of the metadata |
| 8645 | * contains a track listing, these tracks will be added to the |
| 8646 | * album. |
| 8647 | * @param device a pointer to the device to create the new album on. |
| 8648 | * @param metadata the metadata for the new album. If the function |
| 8649 | * exits with success, the <code>album_id</code> field of this |
| 8650 | * struct will contain the new ID of the album. |
Linus Walleij | 2fe43e4 | 2009-01-30 20:31:26 +0000 | [diff] [blame] | 8651 | * <ul> |
Linus Walleij | 08a5fe1 | 2009-09-08 21:28:58 +0000 | [diff] [blame] | 8652 | * <li><code>metadata->parent_id</code> should be set to the parent |
| 8653 | * (e.g. folder) to store this track in. Since some |
Linus Walleij | 2fe43e4 | 2009-01-30 20:31:26 +0000 | [diff] [blame] | 8654 | * devices are a bit picky about where files |
| 8655 | * are placed, a default folder will be chosen if libmtp |
| 8656 | * has detected one for the current filetype and this |
| 8657 | * parameter is set to 0. If this is 0 and no default folder |
| 8658 | * can be found, the file will be stored in the root folder. |
| 8659 | * <li><code>metadata->storage_id</code> should be set to the |
| 8660 | * desired storage (e.g. memory card or whatever your device |
| 8661 | * presents) to store this track in. Setting this to 0 will store |
| 8662 | * the track on the primary storage. |
| 8663 | * </ul> |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 8664 | * @return 0 on success, any other value means failure. |
| 8665 | * @see LIBMTP_Update_Album() |
| 8666 | * @see LIBMTP_Delete_Object() |
| 8667 | */ |
| 8668 | int LIBMTP_Create_New_Album(LIBMTP_mtpdevice_t *device, |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 8669 | LIBMTP_album_t * const metadata) |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 8670 | { |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 8671 | uint32_t localph = metadata->parent_id; |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 8672 | |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 8673 | // Use a default folder if none given |
| 8674 | if (localph == 0) { |
Linus Walleij | afac36b | 2009-01-30 20:39:13 +0000 | [diff] [blame] | 8675 | if (device->default_album_folder != 0) |
| 8676 | localph = device->default_album_folder; |
| 8677 | else |
| 8678 | localph = device->default_music_folder; |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 8679 | } |
Linus Walleij | 5ce59db | 2008-03-12 21:22:58 +0000 | [diff] [blame] | 8680 | metadata->parent_id = localph; |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 8681 | |
Linus Walleij | b7426d1 | 2006-11-25 23:19:11 +0000 | [diff] [blame] | 8682 | // Just create a new abstract album... |
| 8683 | return create_new_abstract_list(device, |
| 8684 | metadata->name, |
Linus Walleij | adce4a5 | 2007-04-23 07:28:11 +0000 | [diff] [blame] | 8685 | metadata->artist, |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 8686 | metadata->composer, |
Linus Walleij | adce4a5 | 2007-04-23 07:28:11 +0000 | [diff] [blame] | 8687 | metadata->genre, |
Linus Walleij | b7426d1 | 2006-11-25 23:19:11 +0000 | [diff] [blame] | 8688 | localph, |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 8689 | metadata->storage_id, |
Linus Walleij | b7426d1 | 2006-11-25 23:19:11 +0000 | [diff] [blame] | 8690 | PTP_OFC_MTP_AbstractAudioAlbum, |
| 8691 | ".alb", |
| 8692 | &metadata->album_id, |
| 8693 | metadata->tracks, |
| 8694 | metadata->no_tracks); |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 8695 | } |
| 8696 | |
| 8697 | /** |
rreardon | 5332f9c | 2006-12-05 10:18:23 +0000 | [diff] [blame] | 8698 | * This creates a new sample data metadata structure and allocates memory |
| 8699 | * for it. Notice that if you add strings to this structure they |
| 8700 | * will be freed by the corresponding <code>LIBMTP_destroy_sampledata_t</code> |
| 8701 | * operation later, so be careful of using strdup() when assigning |
| 8702 | * strings. |
| 8703 | * |
| 8704 | * @return a pointer to the newly allocated metadata structure. |
| 8705 | * @see LIBMTP_destroy_sampledata_t() |
| 8706 | */ |
| 8707 | LIBMTP_filesampledata_t *LIBMTP_new_filesampledata_t(void) |
| 8708 | { |
| 8709 | LIBMTP_filesampledata_t *new = (LIBMTP_filesampledata_t *) malloc(sizeof(LIBMTP_filesampledata_t)); |
| 8710 | if (new == NULL) { |
| 8711 | return NULL; |
| 8712 | } |
| 8713 | new->height=0; |
| 8714 | new->width = 0; |
| 8715 | new->data = NULL; |
| 8716 | new->duration = 0; |
| 8717 | new->size = 0; |
| 8718 | return new; |
| 8719 | } |
| 8720 | |
Linus Walleij | f1b02f2 | 2006-12-06 09:03:23 +0000 | [diff] [blame] | 8721 | /** |
| 8722 | * This destroys a file sample metadata type. |
| 8723 | * @param sample the file sample metadata to be destroyed. |
| 8724 | */ |
| 8725 | void LIBMTP_destroy_filesampledata_t(LIBMTP_filesampledata_t * sample) |
| 8726 | { |
| 8727 | if (sample == NULL) { |
| 8728 | return; |
| 8729 | } |
| 8730 | if (sample->data != NULL) { |
| 8731 | free(sample->data); |
| 8732 | } |
| 8733 | free(sample); |
| 8734 | } |
| 8735 | |
| 8736 | /** |
| 8737 | * This routine figures out whether a certain filetype supports |
| 8738 | * representative samples (small thumbnail images) or not. This |
| 8739 | * typically applies to JPEG files, MP3 files and Album abstract |
| 8740 | * playlists, but in theory any filetype could support representative |
| 8741 | * samples. |
| 8742 | * @param device a pointer to the device which is to be examined. |
Linus Walleij | 9ffe998 | 2008-01-20 13:42:52 +0000 | [diff] [blame] | 8743 | * @param filetype the fileype to examine, and return the representative sample |
Linus Walleij | f1b02f2 | 2006-12-06 09:03:23 +0000 | [diff] [blame] | 8744 | * properties for. |
| 8745 | * @param sample this will contain a new sample type with the fields |
| 8746 | * filled in with suitable default values. For example, the |
| 8747 | * supported sample type will be set, the supported height and |
| 8748 | * width will be set to max values if it is an image sample, |
| 8749 | * and duration will also be given some suitable default value |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 8750 | * which should not be exceeded on audio samples. If the |
Linus Walleij | f1b02f2 | 2006-12-06 09:03:23 +0000 | [diff] [blame] | 8751 | * device does not support samples for this filetype, this |
| 8752 | * pointer will be NULL. If it is not NULL, the user must |
| 8753 | * destroy this struct with <code>LIBMTP_destroy_filesampledata_t()</code> |
| 8754 | * after use. |
| 8755 | * @return 0 on success, any other value means failure. |
| 8756 | * @see LIBMTP_Send_Representative_Sample() |
| 8757 | * @see LIBMTP_Create_New_Album() |
| 8758 | */ |
| 8759 | int LIBMTP_Get_Representative_Sample_Format(LIBMTP_mtpdevice_t *device, |
| 8760 | LIBMTP_filetype_t const filetype, |
| 8761 | LIBMTP_filesampledata_t ** sample) |
| 8762 | { |
| 8763 | uint16_t ret; |
| 8764 | PTPParams *params = (PTPParams *) device->params; |
| 8765 | uint16_t *props = NULL; |
| 8766 | uint32_t propcnt = 0; |
| 8767 | int i; |
| 8768 | // TODO: Get rid of these when we can properly query the device. |
| 8769 | int support_data = 0; |
| 8770 | int support_format = 0; |
| 8771 | int support_height = 0; |
| 8772 | int support_width = 0; |
| 8773 | int support_duration = 0; |
Richard Low | 6f07084 | 2009-05-03 10:26:16 +0000 | [diff] [blame] | 8774 | int support_size = 0; |
Linus Walleij | f1b02f2 | 2006-12-06 09:03:23 +0000 | [diff] [blame] | 8775 | |
rreardon | 21a3a9d | 2007-08-15 10:56:31 +0000 | [diff] [blame] | 8776 | PTPObjectPropDesc opd_height; |
| 8777 | PTPObjectPropDesc opd_width; |
| 8778 | PTPObjectPropDesc opd_format; |
| 8779 | PTPObjectPropDesc opd_duration; |
Richard Low | 6f07084 | 2009-05-03 10:26:16 +0000 | [diff] [blame] | 8780 | PTPObjectPropDesc opd_size; |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 8781 | |
Linus Walleij | f1b02f2 | 2006-12-06 09:03:23 +0000 | [diff] [blame] | 8782 | // Default to no type supported. |
| 8783 | *sample = NULL; |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 8784 | |
Linus Walleij | f1b02f2 | 2006-12-06 09:03:23 +0000 | [diff] [blame] | 8785 | ret = ptp_mtp_getobjectpropssupported(params, map_libmtp_type_to_ptp_type(filetype), &propcnt, &props); |
| 8786 | if (ret != PTP_RC_OK) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 8787 | add_ptp_error_to_errorstack(device, ret, "LIBMTP_Get_Representative_Sample_Format(): could not get object properties."); |
Linus Walleij | f1b02f2 | 2006-12-06 09:03:23 +0000 | [diff] [blame] | 8788 | return -1; |
| 8789 | } |
| 8790 | /* |
| 8791 | * TODO: when walking through these object properties, make calls to |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 8792 | * a new function in ptp.h/ptp.c that can send the command |
Linus Walleij | f1b02f2 | 2006-12-06 09:03:23 +0000 | [diff] [blame] | 8793 | * PTP_OC_MTP_GetObjectPropDesc to get max/min values of the properties |
| 8794 | * supported. |
| 8795 | */ |
| 8796 | for (i = 0; i < propcnt; i++) { |
| 8797 | switch(props[i]) { |
| 8798 | case PTP_OPC_RepresentativeSampleData: |
| 8799 | support_data = 1; |
| 8800 | break; |
| 8801 | case PTP_OPC_RepresentativeSampleFormat: |
| 8802 | support_format = 1; |
| 8803 | break; |
| 8804 | case PTP_OPC_RepresentativeSampleSize: |
Richard Low | 6f07084 | 2009-05-03 10:26:16 +0000 | [diff] [blame] | 8805 | support_size = 1; |
Linus Walleij | f1b02f2 | 2006-12-06 09:03:23 +0000 | [diff] [blame] | 8806 | break; |
| 8807 | case PTP_OPC_RepresentativeSampleHeight: |
| 8808 | support_height = 1; |
| 8809 | break; |
| 8810 | case PTP_OPC_RepresentativeSampleWidth: |
| 8811 | support_width = 1; |
| 8812 | break; |
| 8813 | case PTP_OPC_RepresentativeSampleDuration: |
| 8814 | support_duration = 1; |
| 8815 | break; |
| 8816 | default: |
| 8817 | break; |
| 8818 | } |
| 8819 | } |
| 8820 | free(props); |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 8821 | |
Linus Walleij | f1b02f2 | 2006-12-06 09:03:23 +0000 | [diff] [blame] | 8822 | if (support_data && support_format && support_height && support_width && !support_duration) { |
| 8823 | // Something that supports height and width and not duration is likely to be JPEG |
| 8824 | LIBMTP_filesampledata_t *retsam = LIBMTP_new_filesampledata_t(); |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 8825 | /* |
| 8826 | * Populate the sample format with the first supported format |
| 8827 | * |
rreardon | 21a3a9d | 2007-08-15 10:56:31 +0000 | [diff] [blame] | 8828 | * TODO: figure out how to pass back more than one format if more are |
| 8829 | * supported by the device. |
| 8830 | */ |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 8831 | ptp_mtp_getobjectpropdesc (params, PTP_OPC_RepresentativeSampleFormat, map_libmtp_type_to_ptp_type(filetype), &opd_format); |
rreardon | 21a3a9d | 2007-08-15 10:56:31 +0000 | [diff] [blame] | 8832 | retsam->filetype = map_ptp_type_to_libmtp_type(opd_format.FORM.Enum.SupportedValue[0].u16); |
Richard Low | 6f07084 | 2009-05-03 10:26:16 +0000 | [diff] [blame] | 8833 | ptp_free_objectpropdesc(&opd_format); |
rreardon | 21a3a9d | 2007-08-15 10:56:31 +0000 | [diff] [blame] | 8834 | /* Populate the maximum image height */ |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 8835 | ptp_mtp_getobjectpropdesc (params, PTP_OPC_RepresentativeSampleWidth, map_libmtp_type_to_ptp_type(filetype), &opd_width); |
rreardon | 21a3a9d | 2007-08-15 10:56:31 +0000 | [diff] [blame] | 8836 | retsam->width = opd_width.FORM.Range.MaximumValue.u32; |
Richard Low | 6f07084 | 2009-05-03 10:26:16 +0000 | [diff] [blame] | 8837 | ptp_free_objectpropdesc(&opd_width); |
rreardon | 21a3a9d | 2007-08-15 10:56:31 +0000 | [diff] [blame] | 8838 | /* Populate the maximum image width */ |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 8839 | ptp_mtp_getobjectpropdesc (params, PTP_OPC_RepresentativeSampleHeight, map_libmtp_type_to_ptp_type(filetype), &opd_height); |
rreardon | 21a3a9d | 2007-08-15 10:56:31 +0000 | [diff] [blame] | 8840 | retsam->height = opd_height.FORM.Range.MaximumValue.u32; |
Richard Low | 6f07084 | 2009-05-03 10:26:16 +0000 | [diff] [blame] | 8841 | ptp_free_objectpropdesc(&opd_height); |
| 8842 | /* Populate the maximum size */ |
| 8843 | if (support_size) { |
| 8844 | ptp_mtp_getobjectpropdesc (params, PTP_OPC_RepresentativeSampleSize, map_libmtp_type_to_ptp_type(filetype), &opd_size); |
| 8845 | retsam->size = opd_size.FORM.Range.MaximumValue.u32; |
| 8846 | ptp_free_objectpropdesc(&opd_size); |
| 8847 | } |
Linus Walleij | f1b02f2 | 2006-12-06 09:03:23 +0000 | [diff] [blame] | 8848 | *sample = retsam; |
| 8849 | } else if (support_data && support_format && !support_height && !support_width && support_duration) { |
| 8850 | // Another qualified guess |
| 8851 | LIBMTP_filesampledata_t *retsam = LIBMTP_new_filesampledata_t(); |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 8852 | /* |
| 8853 | * Populate the sample format with the first supported format |
| 8854 | * |
rreardon | 21a3a9d | 2007-08-15 10:56:31 +0000 | [diff] [blame] | 8855 | * TODO: figure out how to pass back more than one format if more are |
| 8856 | * supported by the device. |
| 8857 | */ |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 8858 | ptp_mtp_getobjectpropdesc (params, PTP_OPC_RepresentativeSampleFormat, map_libmtp_type_to_ptp_type(filetype), &opd_format); |
rreardon | 21a3a9d | 2007-08-15 10:56:31 +0000 | [diff] [blame] | 8859 | retsam->filetype = map_ptp_type_to_libmtp_type(opd_format.FORM.Enum.SupportedValue[0].u16); |
Richard Low | 6f07084 | 2009-05-03 10:26:16 +0000 | [diff] [blame] | 8860 | ptp_free_objectpropdesc(&opd_format); |
| 8861 | /* Populate the maximum duration */ |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 8862 | ptp_mtp_getobjectpropdesc (params, PTP_OPC_RepresentativeSampleDuration, map_libmtp_type_to_ptp_type(filetype), &opd_duration); |
rreardon | 21a3a9d | 2007-08-15 10:56:31 +0000 | [diff] [blame] | 8863 | retsam->duration = opd_duration.FORM.Range.MaximumValue.u32; |
Richard Low | 6f07084 | 2009-05-03 10:26:16 +0000 | [diff] [blame] | 8864 | ptp_free_objectpropdesc(&opd_duration); |
| 8865 | /* Populate the maximum size */ |
| 8866 | if (support_size) { |
| 8867 | ptp_mtp_getobjectpropdesc (params, PTP_OPC_RepresentativeSampleSize, map_libmtp_type_to_ptp_type(filetype), &opd_size); |
| 8868 | retsam->size = opd_size.FORM.Range.MaximumValue.u32; |
| 8869 | ptp_free_objectpropdesc(&opd_size); |
| 8870 | } |
Linus Walleij | f1b02f2 | 2006-12-06 09:03:23 +0000 | [diff] [blame] | 8871 | *sample = retsam; |
| 8872 | } |
| 8873 | return 0; |
| 8874 | } |
rreardon | 5332f9c | 2006-12-05 10:18:23 +0000 | [diff] [blame] | 8875 | |
| 8876 | /** |
| 8877 | * This routine sends representative sample data for an object. |
| 8878 | * This uses the RepresentativeSampleData property of the album, |
| 8879 | * if the device supports it. The data should be of a format acceptable |
| 8880 | * to the player (for iRiver and Creative, this seems to be JPEG) and |
| 8881 | * must not be too large. (for a Creative, max seems to be about 20KB.) |
Richard Low | 6f07084 | 2009-05-03 10:26:16 +0000 | [diff] [blame] | 8882 | * Check by calling LIBMTP_Get_Representative_Sample_Format() to get |
| 8883 | * maximum size, dimensions, etc.. |
Linus Walleij | 7e3f276 | 2006-12-03 22:52:05 +0000 | [diff] [blame] | 8884 | * @param device a pointer to the device which the object is on. |
| 8885 | * @param id unique id of the object to set artwork for. |
Richard Low | 36e447c | 2008-01-20 15:24:55 +0000 | [diff] [blame] | 8886 | * @param pointer to LIBMTP_filesampledata_t struct containing data |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 8887 | * @return 0 on success, any other value means failure. |
Richard Low | 36e447c | 2008-01-20 15:24:55 +0000 | [diff] [blame] | 8888 | * @see LIBMTP_Get_Representative_Sample() |
Linus Walleij | f1b02f2 | 2006-12-06 09:03:23 +0000 | [diff] [blame] | 8889 | * @see LIBMTP_Get_Representative_Sample_Format() |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 8890 | * @see LIBMTP_Create_New_Album() |
| 8891 | */ |
Linus Walleij | 7e3f276 | 2006-12-03 22:52:05 +0000 | [diff] [blame] | 8892 | int LIBMTP_Send_Representative_Sample(LIBMTP_mtpdevice_t *device, |
rreardon | 5332f9c | 2006-12-05 10:18:23 +0000 | [diff] [blame] | 8893 | uint32_t const id, |
| 8894 | LIBMTP_filesampledata_t *sampledata) |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 8895 | { |
| 8896 | uint16_t ret; |
| 8897 | PTPParams *params = (PTPParams *) device->params; |
Richard Low | bb9fb4a | 2008-05-18 14:49:34 +0000 | [diff] [blame] | 8898 | PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo; |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 8899 | PTPPropertyValue propval; |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 8900 | PTPObject *ob; |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 8901 | uint32_t i; |
Linus Walleij | f1b02f2 | 2006-12-06 09:03:23 +0000 | [diff] [blame] | 8902 | uint16_t *props = NULL; |
| 8903 | uint32_t propcnt = 0; |
| 8904 | int supported = 0; |
rreardon | 5332f9c | 2006-12-05 10:18:23 +0000 | [diff] [blame] | 8905 | |
| 8906 | // get the file format for the object we're going to send representative data for |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 8907 | ret = ptp_object_want (params, id, PTPOBJECT_OBJECTINFO_LOADED, &ob); |
| 8908 | if (ret != PTP_RC_OK) { |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 8909 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Send_Representative_Sample(): could not get object info."); |
rreardon | 5332f9c | 2006-12-05 10:18:23 +0000 | [diff] [blame] | 8910 | return -1; |
| 8911 | } |
| 8912 | |
| 8913 | // check that we can send representative sample data for this object format |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 8914 | ret = ptp_mtp_getobjectpropssupported(params, ob->oi.ObjectFormat, &propcnt, &props); |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 8915 | if (ret != PTP_RC_OK) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 8916 | add_ptp_error_to_errorstack(device, ret, "LIBMTP_Send_Representative_Sample(): could not get object properties."); |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 8917 | return -1; |
| 8918 | } |
Linus Walleij | f1b02f2 | 2006-12-06 09:03:23 +0000 | [diff] [blame] | 8919 | |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 8920 | for (i = 0; i < propcnt; i++) { |
Linus Walleij | f1b02f2 | 2006-12-06 09:03:23 +0000 | [diff] [blame] | 8921 | if (props[i] == PTP_OPC_RepresentativeSampleData) { |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 8922 | supported = 1; |
Linus Walleij | f1b02f2 | 2006-12-06 09:03:23 +0000 | [diff] [blame] | 8923 | break; |
| 8924 | } |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 8925 | } |
| 8926 | if (!supported) { |
Linus Walleij | f1b02f2 | 2006-12-06 09:03:23 +0000 | [diff] [blame] | 8927 | free(props); |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 8928 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Send_Representative_Sample(): object type doesn't support RepresentativeSampleData."); |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 8929 | return -1; |
| 8930 | } |
Linus Walleij | f1b02f2 | 2006-12-06 09:03:23 +0000 | [diff] [blame] | 8931 | free(props); |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 8932 | |
Linus Walleij | f1b02f2 | 2006-12-06 09:03:23 +0000 | [diff] [blame] | 8933 | // Go ahead and send the data |
rreardon | 5332f9c | 2006-12-05 10:18:23 +0000 | [diff] [blame] | 8934 | propval.a.count = sampledata->size; |
| 8935 | propval.a.v = malloc(sizeof(PTPPropertyValue) * sampledata->size); |
| 8936 | for (i = 0; i < sampledata->size; i++) { |
| 8937 | propval.a.v[i].u8 = sampledata->data[i]; |
Linus Walleij | 7e3f276 | 2006-12-03 22:52:05 +0000 | [diff] [blame] | 8938 | } |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 8939 | |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 8940 | ret = ptp_mtp_setobjectpropvalue(params,id,PTP_OPC_RepresentativeSampleData, |
Linus Walleij | f1b02f2 | 2006-12-06 09:03:23 +0000 | [diff] [blame] | 8941 | &propval,PTP_DTC_AUINT8); |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 8942 | if (ret != PTP_RC_OK) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 8943 | add_ptp_error_to_errorstack(device, ret, "LIBMTP_Send_Representative_Sample(): could not send sample data."); |
Linus Walleij | 7e3f276 | 2006-12-03 22:52:05 +0000 | [diff] [blame] | 8944 | free(propval.a.v); |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 8945 | return -1; |
| 8946 | } |
Linus Walleij | 7e3f276 | 2006-12-03 22:52:05 +0000 | [diff] [blame] | 8947 | free(propval.a.v); |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 8948 | |
rreardon | d2ddb63 | 2006-12-12 12:13:21 +0000 | [diff] [blame] | 8949 | /* Set the height and width if the sample is an image, otherwise just |
| 8950 | * set the duration and size */ |
| 8951 | switch(sampledata->filetype) { |
Linus Walleij | e1ac07e | 2006-12-14 19:38:59 +0000 | [diff] [blame] | 8952 | case LIBMTP_FILETYPE_JPEG: |
| 8953 | case LIBMTP_FILETYPE_JFIF: |
| 8954 | case LIBMTP_FILETYPE_TIFF: |
| 8955 | case LIBMTP_FILETYPE_BMP: |
| 8956 | case LIBMTP_FILETYPE_GIF: |
| 8957 | case LIBMTP_FILETYPE_PICT: |
| 8958 | case LIBMTP_FILETYPE_PNG: |
Linus Walleij | fec4d56 | 2008-06-01 22:30:36 +0000 | [diff] [blame] | 8959 | if (!FLAG_BROKEN_SET_SAMPLE_DIMENSIONS(ptp_usb)) { |
Richard Low | bb9fb4a | 2008-05-18 14:49:34 +0000 | [diff] [blame] | 8960 | // For images, set the height and width |
| 8961 | set_object_u32(device, id, PTP_OPC_RepresentativeSampleHeight, sampledata->height); |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 8962 | set_object_u32(device, id, PTP_OPC_RepresentativeSampleWidth, sampledata->width); |
Richard Low | bb9fb4a | 2008-05-18 14:49:34 +0000 | [diff] [blame] | 8963 | } |
Linus Walleij | e1ac07e | 2006-12-14 19:38:59 +0000 | [diff] [blame] | 8964 | break; |
| 8965 | default: |
| 8966 | // For anything not an image, set the duration and size |
| 8967 | set_object_u32(device, id, PTP_OPC_RepresentativeSampleDuration, sampledata->duration); |
| 8968 | set_object_u32(device, id, PTP_OPC_RepresentativeSampleSize, sampledata->size); |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 8969 | break; |
rreardon | d2ddb63 | 2006-12-12 12:13:21 +0000 | [diff] [blame] | 8970 | } |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 8971 | |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 8972 | return 0; |
| 8973 | } |
| 8974 | |
| 8975 | /** |
Richard Low | 36e447c | 2008-01-20 15:24:55 +0000 | [diff] [blame] | 8976 | * This routine gets representative sample data for an object. |
| 8977 | * This uses the RepresentativeSampleData property of the album, |
| 8978 | * if the device supports it. |
| 8979 | * @param device a pointer to the device which the object is on. |
| 8980 | * @param id unique id of the object to get data for. |
| 8981 | * @param pointer to LIBMTP_filesampledata_t struct to receive data |
| 8982 | * @return 0 on success, any other value means failure. |
| 8983 | * @see LIBMTP_Send_Representative_Sample() |
| 8984 | * @see LIBMTP_Get_Representative_Sample_Format() |
| 8985 | * @see LIBMTP_Create_New_Album() |
| 8986 | */ |
| 8987 | int LIBMTP_Get_Representative_Sample(LIBMTP_mtpdevice_t *device, |
| 8988 | uint32_t const id, |
| 8989 | LIBMTP_filesampledata_t *sampledata) |
| 8990 | { |
| 8991 | uint16_t ret; |
| 8992 | PTPParams *params = (PTPParams *) device->params; |
| 8993 | PTPPropertyValue propval; |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 8994 | PTPObject *ob; |
Richard Low | 36e447c | 2008-01-20 15:24:55 +0000 | [diff] [blame] | 8995 | uint32_t i; |
| 8996 | uint16_t *props = NULL; |
| 8997 | uint32_t propcnt = 0; |
| 8998 | int supported = 0; |
| 8999 | |
| 9000 | // get the file format for the object we're going to send representative data for |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 9001 | ret = ptp_object_want (params, id, PTPOBJECT_OBJECTINFO_LOADED, &ob); |
| 9002 | if (ret != PTP_RC_OK) { |
Richard Low | 36e447c | 2008-01-20 15:24:55 +0000 | [diff] [blame] | 9003 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Get_Representative_Sample(): could not get object info."); |
| 9004 | return -1; |
| 9005 | } |
| 9006 | |
| 9007 | // check that we can store representative sample data for this object format |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 9008 | ret = ptp_mtp_getobjectpropssupported(params, ob->oi.ObjectFormat, &propcnt, &props); |
Richard Low | 36e447c | 2008-01-20 15:24:55 +0000 | [diff] [blame] | 9009 | if (ret != PTP_RC_OK) { |
| 9010 | add_ptp_error_to_errorstack(device, ret, "LIBMTP_Get_Representative_Sample(): could not get object properties."); |
| 9011 | return -1; |
| 9012 | } |
| 9013 | |
| 9014 | for (i = 0; i < propcnt; i++) { |
| 9015 | if (props[i] == PTP_OPC_RepresentativeSampleData) { |
| 9016 | supported = 1; |
| 9017 | break; |
| 9018 | } |
| 9019 | } |
| 9020 | if (!supported) { |
| 9021 | free(props); |
| 9022 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Get_Representative_Sample(): object type doesn't support RepresentativeSampleData."); |
| 9023 | return -1; |
| 9024 | } |
| 9025 | free(props); |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 9026 | |
Richard Low | 36e447c | 2008-01-20 15:24:55 +0000 | [diff] [blame] | 9027 | // Get the data |
| 9028 | ret = ptp_mtp_getobjectpropvalue(params,id,PTP_OPC_RepresentativeSampleData, |
| 9029 | &propval,PTP_DTC_AUINT8); |
| 9030 | if (ret != PTP_RC_OK) { |
| 9031 | add_ptp_error_to_errorstack(device, ret, "LIBMTP_Get_Representative_Sample(): could not get sample data."); |
| 9032 | return -1; |
| 9033 | } |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 9034 | |
Richard Low | 36e447c | 2008-01-20 15:24:55 +0000 | [diff] [blame] | 9035 | // Store it |
| 9036 | sampledata->size = propval.a.count; |
| 9037 | sampledata->data = malloc(sizeof(PTPPropertyValue) * propval.a.count); |
| 9038 | for (i = 0; i < propval.a.count; i++) { |
| 9039 | sampledata->data[i] = propval.a.v[i].u8; |
| 9040 | } |
| 9041 | free(propval.a.v); |
| 9042 | |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 9043 | // Get the other properties |
Richard Low | 36e447c | 2008-01-20 15:24:55 +0000 | [diff] [blame] | 9044 | sampledata->width = get_u32_from_object(device, id, PTP_OPC_RepresentativeSampleWidth, 0); |
| 9045 | sampledata->height = get_u32_from_object(device, id, PTP_OPC_RepresentativeSampleHeight, 0); |
| 9046 | sampledata->duration = get_u32_from_object(device, id, PTP_OPC_RepresentativeSampleDuration, 0); |
| 9047 | sampledata->filetype = map_ptp_type_to_libmtp_type( |
| 9048 | get_u16_from_object(device, id, PTP_OPC_RepresentativeSampleFormat, LIBMTP_FILETYPE_UNKNOWN)); |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 9049 | |
Richard Low | 36e447c | 2008-01-20 15:24:55 +0000 | [diff] [blame] | 9050 | return 0; |
| 9051 | } |
| 9052 | |
| 9053 | /** |
Philip Langdale | 99351a4 | 2012-08-18 15:32:12 -0700 | [diff] [blame] | 9054 | * Retrieve the thumbnail for a file. |
| 9055 | * @param device a pointer to the device to get the thumbnail from. |
| 9056 | * @param id the object ID of the file to retrieve the thumbnail for. |
| 9057 | * @return 0 on success, any other value means failure. |
| 9058 | */ |
| 9059 | int LIBMTP_Get_Thumbnail(LIBMTP_mtpdevice_t *device, uint32_t const id, |
| 9060 | unsigned char **data, unsigned int *size) |
| 9061 | { |
| 9062 | PTPParams *params = (PTPParams *) device->params; |
| 9063 | uint16_t ret; |
| 9064 | |
| 9065 | ret = ptp_getthumb(params, id, data, size); |
| 9066 | if (ret == PTP_RC_OK) |
| 9067 | return 0; |
| 9068 | return -1; |
| 9069 | } |
| 9070 | |
Philip Langdale | 31e831c | 2013-03-04 20:58:19 -0800 | [diff] [blame] | 9071 | |
| 9072 | int LIBMTP_GetPartialObject(LIBMTP_mtpdevice_t *device, uint32_t const id, |
| 9073 | uint64_t offset, uint32_t maxbytes, |
| 9074 | unsigned char **data, unsigned int *size) |
| 9075 | { |
Marcus Meissner | c13c7a9 | 2017-11-16 17:31:34 +0100 | [diff] [blame] | 9076 | PTPParams *params = (PTPParams *) device->params; |
| 9077 | uint16_t ret; |
| 9078 | LIBMTP_file_t *mtpfile = LIBMTP_Get_Filemetadata(device, id); |
| 9079 | |
| 9080 | /* Some devices do not like reading over the end and hang instead of progressing */ |
| 9081 | if (offset >= mtpfile->filesize) { |
| 9082 | *size = 0; |
| 9083 | LIBMTP_destroy_file_t (mtpfile); |
| 9084 | return 0; |
| 9085 | } |
| 9086 | if (offset + maxbytes > mtpfile->filesize) { |
| 9087 | maxbytes = mtpfile->filesize - offset; |
| 9088 | } |
Philip Langdale | 31e831c | 2013-03-04 20:58:19 -0800 | [diff] [blame] | 9089 | |
| 9090 | if (!ptp_operation_issupported(params, PTP_OC_ANDROID_GetPartialObject64)) { |
| 9091 | if (!ptp_operation_issupported(params, PTP_OC_GetPartialObject)) { |
| 9092 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, |
| 9093 | "LIBMTP_GetPartialObject: PTP_OC_GetPartialObject not supported"); |
| 9094 | return -1; |
| 9095 | } |
| 9096 | |
| 9097 | if (offset >> 32 != 0) { |
| 9098 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, |
| 9099 | "LIBMTP_GetPartialObject: PTP_OC_GetPartialObject only supports 32bit offsets"); |
| 9100 | return -1; |
| 9101 | } |
| 9102 | |
| 9103 | ret = ptp_getpartialobject(params, id, (uint32_t)offset, maxbytes, data, size); |
| 9104 | } else { |
| 9105 | ret = ptp_android_getpartialobject64(params, id, offset, maxbytes, data, size); |
| 9106 | } |
| 9107 | if (ret == PTP_RC_OK) |
| 9108 | return 0; |
| 9109 | return -1; |
| 9110 | } |
| 9111 | |
| 9112 | |
| 9113 | int LIBMTP_SendPartialObject(LIBMTP_mtpdevice_t *device, uint32_t const id, |
| 9114 | uint64_t offset, unsigned char *data, unsigned int size) |
| 9115 | { |
| 9116 | PTPParams *params = (PTPParams *) device->params; |
| 9117 | uint16_t ret; |
| 9118 | |
| 9119 | if (!ptp_operation_issupported(params, PTP_OC_ANDROID_SendPartialObject)) { |
| 9120 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, |
| 9121 | "LIBMTP_SendPartialObject: PTP_OC_ANDROID_SendPartialObject not supported"); |
| 9122 | return -1; |
| 9123 | } |
| 9124 | |
| 9125 | ret = ptp_android_sendpartialobject(params, id, offset, data, size); |
| 9126 | if (ret == PTP_RC_OK) |
| 9127 | return 0; |
| 9128 | return -1; |
| 9129 | } |
| 9130 | |
| 9131 | |
| 9132 | int LIBMTP_BeginEditObject(LIBMTP_mtpdevice_t *device, uint32_t const id) |
| 9133 | { |
| 9134 | PTPParams *params = (PTPParams *) device->params; |
| 9135 | uint16_t ret; |
| 9136 | |
| 9137 | if (!ptp_operation_issupported(params, PTP_OC_ANDROID_BeginEditObject)) { |
| 9138 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, |
| 9139 | "LIBMTP_BeginEditObject: PTP_OC_ANDROID_BeginEditObject not supported"); |
| 9140 | return -1; |
| 9141 | } |
| 9142 | |
| 9143 | ret = ptp_android_begineditobject(params, id); |
| 9144 | if (ret == PTP_RC_OK) |
| 9145 | return 0; |
| 9146 | return -1; |
| 9147 | } |
| 9148 | |
| 9149 | |
| 9150 | int LIBMTP_EndEditObject(LIBMTP_mtpdevice_t *device, uint32_t const id) |
| 9151 | { |
| 9152 | PTPParams *params = (PTPParams *) device->params; |
| 9153 | uint16_t ret; |
| 9154 | |
| 9155 | if (!ptp_operation_issupported(params, PTP_OC_ANDROID_EndEditObject)) { |
| 9156 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, |
| 9157 | "LIBMTP_EndEditObject: PTP_OC_ANDROID_EndEditObject not supported"); |
| 9158 | return -1; |
| 9159 | } |
| 9160 | |
| 9161 | ret = ptp_android_endeditobject(params, id); |
| 9162 | if (ret == PTP_RC_OK) { |
| 9163 | // update cached object properties if metadata cache exists |
| 9164 | update_metadata_cache(device, id); |
| 9165 | return 0; |
| 9166 | } |
| 9167 | return -1; |
| 9168 | } |
| 9169 | |
| 9170 | |
| 9171 | int LIBMTP_TruncateObject(LIBMTP_mtpdevice_t *device, uint32_t const id, |
| 9172 | uint64_t offset) |
| 9173 | { |
| 9174 | PTPParams *params = (PTPParams *) device->params; |
| 9175 | uint16_t ret; |
| 9176 | |
| 9177 | if (!ptp_operation_issupported(params, PTP_OC_ANDROID_TruncateObject)) { |
| 9178 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, |
| 9179 | "LIBMTP_TruncateObject: PTP_OC_ANDROID_TruncateObject not supported"); |
| 9180 | return -1; |
| 9181 | } |
| 9182 | |
| 9183 | ret = ptp_android_truncate(params, id, offset); |
| 9184 | if (ret == PTP_RC_OK) |
| 9185 | return 0; |
| 9186 | return -1; |
| 9187 | } |
| 9188 | |
| 9189 | |
Philip Langdale | 99351a4 | 2012-08-18 15:32:12 -0700 | [diff] [blame] | 9190 | /** |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 9191 | * This routine updates an album based on the metadata |
| 9192 | * supplied. If the <code>tracks</code> field of the metadata |
| 9193 | * contains a track listing, these tracks will be added to the |
| 9194 | * album in place of those already present, i.e. the |
| 9195 | * previous track listing will be deleted. |
| 9196 | * @param device a pointer to the device to create the new album on. |
| 9197 | * @param metadata the metadata for the album to be updated. |
| 9198 | * notice that the field <code>album_id</code> |
| 9199 | * must contain the apropriate album ID. |
| 9200 | * @return 0 on success, any other value means failure. |
| 9201 | * @see LIBMTP_Create_New_Album() |
| 9202 | * @see LIBMTP_Delete_Object() |
| 9203 | */ |
| 9204 | int LIBMTP_Update_Album(LIBMTP_mtpdevice_t *device, |
| 9205 | LIBMTP_album_t const * const metadata) |
| 9206 | { |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 9207 | return update_abstract_list(device, |
| 9208 | metadata->name, |
| 9209 | metadata->artist, |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 9210 | metadata->composer, |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 9211 | metadata->genre, |
| 9212 | metadata->album_id, |
| 9213 | PTP_OFC_MTP_AbstractAudioAlbum, |
| 9214 | metadata->tracks, |
| 9215 | metadata->no_tracks); |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 9216 | } |
Linus Walleij | aa4b075 | 2006-07-26 22:21:04 +0000 | [diff] [blame] | 9217 | |
| 9218 | /** |
| 9219 | * Dummy function needed to interface to upstream |
| 9220 | * ptp.c/ptp.h files. |
| 9221 | */ |
| 9222 | void ptp_nikon_getptpipguid (unsigned char* guid) { |
| 9223 | return; |
| 9224 | } |
tsaarni | a3eb60a | 2007-07-06 17:41:30 +0000 | [diff] [blame] | 9225 | |
| 9226 | /** |
Linus Walleij | 7752b95 | 2007-10-19 20:11:46 +0000 | [diff] [blame] | 9227 | * Add an object to cache. |
| 9228 | * @param device the device which may have a cache to which the object should be added. |
| 9229 | * @param object_id the object to add to the cache. |
Linus Walleij | 2c1bbd6 | 2009-11-07 15:15:03 +0000 | [diff] [blame] | 9230 | */ |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 9231 | static void add_object_to_cache(LIBMTP_mtpdevice_t *device, uint32_t object_id) |
tsaarni | a3eb60a | 2007-07-06 17:41:30 +0000 | [diff] [blame] | 9232 | { |
| 9233 | PTPParams *params = (PTPParams *)device->params; |
tsaarni | a3eb60a | 2007-07-06 17:41:30 +0000 | [diff] [blame] | 9234 | uint16_t ret; |
tsaarni | a3eb60a | 2007-07-06 17:41:30 +0000 | [diff] [blame] | 9235 | |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 9236 | ret = ptp_add_object_to_cache(params, object_id); |
| 9237 | if (ret != PTP_RC_OK) { |
| 9238 | add_ptp_error_to_errorstack(device, ret, "add_object_to_cache(): couldn't add object to cache"); |
tsaarni | a3eb60a | 2007-07-06 17:41:30 +0000 | [diff] [blame] | 9239 | } |
| 9240 | } |
| 9241 | |
| 9242 | |
| 9243 | /** |
| 9244 | * Update cache after object has been modified |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 9245 | * @param device the device which may have a cache to which the object should be updated. |
| 9246 | * @param object_id the object to update. |
tsaarni | a3eb60a | 2007-07-06 17:41:30 +0000 | [diff] [blame] | 9247 | */ |
Linus Walleij | 7752b95 | 2007-10-19 20:11:46 +0000 | [diff] [blame] | 9248 | static void update_metadata_cache(LIBMTP_mtpdevice_t *device, uint32_t object_id) |
tsaarni | a3eb60a | 2007-07-06 17:41:30 +0000 | [diff] [blame] | 9249 | { |
Linus Walleij | a3d0eaa | 2007-11-18 22:00:48 +0000 | [diff] [blame] | 9250 | PTPParams *params = (PTPParams *)device->params; |
| 9251 | |
| 9252 | ptp_remove_object_from_cache(params, object_id); |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 9253 | add_object_to_cache(device, object_id); |
tsaarni | a3eb60a | 2007-07-06 17:41:30 +0000 | [diff] [blame] | 9254 | } |
Marcin Niestroj | 718b290 | 2017-06-10 16:36:18 +0200 | [diff] [blame] | 9255 | |
| 9256 | |
| 9257 | /** |
| 9258 | * Issue custom (e.g. vendor specific) operation (without data phase) |
| 9259 | * @param device a pointer to the device to send custom operation to. |
| 9260 | * @param code operation code to send. |
| 9261 | * @param n_param number of parameters passed. |
| 9262 | * @param ... uint32_t operation specific parameters. |
| 9263 | */ |
| 9264 | int LIBMTP_Custom_Operation(LIBMTP_mtpdevice_t *device, uint16_t code, int n_param, ...) |
| 9265 | { |
| 9266 | PTPParams *params = (PTPParams *) device->params; |
| 9267 | PTPContainer ptp; |
| 9268 | va_list args; |
| 9269 | uint16_t ret; |
| 9270 | int i; |
| 9271 | |
| 9272 | ptp.Code = code; |
| 9273 | ptp.Nparam = n_param; |
| 9274 | va_start(args, n_param); |
| 9275 | for (i = 0; i < n_param; i++) |
| 9276 | (&ptp.Param1)[i] = va_arg(args, uint32_t); |
| 9277 | va_end(args); |
| 9278 | |
| 9279 | ret = ptp_transaction_new(params, &ptp, PTP_DP_NODATA, 0, NULL); |
| 9280 | |
| 9281 | if (ret != PTP_RC_OK) { |
| 9282 | add_ptp_error_to_errorstack(device, ret, "LIBMTP_Custom_Operation(): failed to execute operation."); |
| 9283 | return -1; |
| 9284 | } |
| 9285 | |
| 9286 | return 0; |
| 9287 | } |