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 | 9ffe998 | 2008-01-20 13:42:52 +0000 | [diff] [blame] | 4 | * Copyright (C) 2005-2008 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 | 0535838 | 2007-08-06 20:46:35 +0000 | [diff] [blame] | 37 | #define _LARGEFILE_SOURCE |
| 38 | #define _LARGEFILE64_SOURCE |
Linus Walleij | 2f45d22 | 2007-02-02 22:47:39 +0000 | [diff] [blame] | 39 | |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 40 | #include "libmtp.h" |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 41 | #include "unicode.h" |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 42 | #include "ptp.h" |
Linus Walleij | 15e344f | 2006-03-06 15:15:00 +0000 | [diff] [blame] | 43 | #include "libusb-glue.h" |
Linus Walleij | 3e667ae | 2007-10-29 23:29:39 +0000 | [diff] [blame] | 44 | #include "device-flags.h" |
Linus Walleij | f3c4405 | 2008-08-16 21:14:56 +0000 | [diff] [blame] | 45 | #include "playlist-spl.h" |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 46 | |
Linus Walleij | 7beba57 | 2006-11-29 08:56:12 +0000 | [diff] [blame] | 47 | #include <string.h> |
| 48 | #include <sys/types.h> |
| 49 | #include <sys/stat.h> |
| 50 | #include <fcntl.h> |
Linus Walleij | 4126b30 | 2007-09-22 22:13:55 +0000 | [diff] [blame] | 51 | #include <time.h> |
Linus Walleij | f3c4405 | 2008-08-16 21:14:56 +0000 | [diff] [blame] | 52 | #include <errno.h> |
Linus Walleij | 7beba57 | 2006-11-29 08:56:12 +0000 | [diff] [blame] | 53 | #ifdef _MSC_VER // For MSVC++ |
| 54 | #define USE_WINDOWS_IO_H |
| 55 | #include <io.h> |
| 56 | #endif |
| 57 | |
Linus Walleij | c86afbd | 2006-05-04 19:05:24 +0000 | [diff] [blame] | 58 | /* |
| 59 | * This is a mapping between libmtp internal MTP filetypes and |
| 60 | * the libgphoto2/PTP equivalent defines. We need this because |
| 61 | * otherwise the libmtp.h device has to be dependent on ptp.h |
| 62 | * to be installed too, and we don't want that. |
| 63 | */ |
Linus Walleij | cf42f45 | 2006-11-28 08:32:49 +0000 | [diff] [blame] | 64 | //typedef struct filemap_struct filemap_t; |
| 65 | typedef struct filemap_struct { |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 66 | char *description; /**< Text description for the file type */ |
| 67 | LIBMTP_filetype_t id; /**< LIBMTP internal type for the file type */ |
| 68 | uint16_t ptp_id; /**< PTP ID for the filetype */ |
Linus Walleij | cf42f45 | 2006-11-28 08:32:49 +0000 | [diff] [blame] | 69 | struct filemap_struct *next; |
| 70 | } filemap_t; |
Linus Walleij | c86afbd | 2006-05-04 19:05:24 +0000 | [diff] [blame] | 71 | |
Linus Walleij | f67bca9 | 2006-05-29 09:33:39 +0000 | [diff] [blame] | 72 | // Global variables |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 73 | // This holds the global filetype mapping table |
Linus Walleij | cf42f45 | 2006-11-28 08:32:49 +0000 | [diff] [blame] | 74 | static filemap_t *filemap = NULL; |
Linus Walleij | f67bca9 | 2006-05-29 09:33:39 +0000 | [diff] [blame] | 75 | |
Linus Walleij | cf42f45 | 2006-11-28 08:32:49 +0000 | [diff] [blame] | 76 | /* |
| 77 | * Forward declarations of local (static) functions. |
| 78 | */ |
| 79 | static int register_filetype(char const * const description, LIBMTP_filetype_t const id, |
| 80 | uint16_t const ptp_id); |
Linus Walleij | 7beba57 | 2006-11-29 08:56:12 +0000 | [diff] [blame] | 81 | static void init_filemap(); |
Linus Walleij | 2715c44 | 2007-01-20 22:35:29 +0000 | [diff] [blame] | 82 | static void add_error_to_errorstack(LIBMTP_mtpdevice_t *device, |
| 83 | LIBMTP_error_number_t errornumber, |
| 84 | char const * const error_text); |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 85 | static void add_ptp_error_to_errorstack(LIBMTP_mtpdevice_t *device, |
| 86 | uint16_t ptp_error, |
| 87 | char const * const error_text); |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 88 | static void flush_handles(LIBMTP_mtpdevice_t *device); |
Linus Walleij | 6bf68b4 | 2007-09-03 22:50:02 +0000 | [diff] [blame] | 89 | static void get_handles_recursively(LIBMTP_mtpdevice_t *device, |
| 90 | PTPParams *params, |
| 91 | PTPObjectHandles *handles, |
| 92 | uint32_t storageid, |
| 93 | uint32_t parent); |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 94 | static void free_storage_list(LIBMTP_mtpdevice_t *device); |
| 95 | static int sort_storage_by(LIBMTP_mtpdevice_t *device, int const sortby); |
| 96 | static uint32_t get_first_storageid(LIBMTP_mtpdevice_t *device); |
Linus Walleij | 6bf68b4 | 2007-09-03 22:50:02 +0000 | [diff] [blame] | 97 | static int get_storage_freespace(LIBMTP_mtpdevice_t *device, |
| 98 | LIBMTP_devicestorage_t *storage, |
| 99 | uint64_t *freespace); |
| 100 | static int check_if_file_fits(LIBMTP_mtpdevice_t *device, |
| 101 | LIBMTP_devicestorage_t *storage, |
| 102 | uint64_t const filesize); |
Linus Walleij | f67bca9 | 2006-05-29 09:33:39 +0000 | [diff] [blame] | 103 | static uint16_t map_libmtp_type_to_ptp_type(LIBMTP_filetype_t intype); |
| 104 | static LIBMTP_filetype_t map_ptp_type_to_libmtp_type(uint16_t intype); |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 105 | static int get_device_unicode_property(LIBMTP_mtpdevice_t *device, |
Linus Walleij | cf223e6 | 2006-06-19 09:31:53 +0000 | [diff] [blame] | 106 | char **unicstring, uint16_t property); |
Linus Walleij | 2955956 | 2007-08-22 21:38:04 +0000 | [diff] [blame] | 107 | static uint16_t adjust_u16(uint16_t val, PTPObjectPropDesc *opd); |
| 108 | static uint32_t adjust_u32(uint32_t val, PTPObjectPropDesc *opd); |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 109 | static char *get_iso8601_stamp(void); |
Linus Walleij | 9901e22 | 2006-11-30 12:28:19 +0000 | [diff] [blame] | 110 | static char *get_string_from_object(LIBMTP_mtpdevice_t *device, uint32_t const object_id, |
| 111 | uint16_t const attribute_id); |
Richard Low | c3f5fc3 | 2007-07-29 09:40:50 +0000 | [diff] [blame] | 112 | static uint64_t get_u64_from_object(LIBMTP_mtpdevice_t *device,uint32_t const object_id, |
| 113 | uint16_t const attribute_id, uint64_t const value_default); |
Linus Walleij | 9901e22 | 2006-11-30 12:28:19 +0000 | [diff] [blame] | 114 | static uint32_t get_u32_from_object(LIBMTP_mtpdevice_t *device,uint32_t const object_id, |
| 115 | uint16_t const attribute_id, uint32_t const value_default); |
| 116 | static uint16_t get_u16_from_object(LIBMTP_mtpdevice_t *device, uint32_t const object_id, |
| 117 | uint16_t const attribute_id, uint16_t const value_default); |
| 118 | static uint8_t get_u8_from_object(LIBMTP_mtpdevice_t *device, uint32_t const object_id, |
| 119 | uint16_t const attribute_id, uint8_t const value_default); |
| 120 | static int set_object_string(LIBMTP_mtpdevice_t *device, uint32_t const object_id, |
| 121 | uint16_t const attribute_id, char const * const string); |
| 122 | static int set_object_u32(LIBMTP_mtpdevice_t *device, uint32_t const object_id, |
| 123 | uint16_t const attribute_id, uint32_t const value); |
| 124 | static int set_object_u16(LIBMTP_mtpdevice_t *device, uint32_t const object_id, |
| 125 | uint16_t const attribute_id, uint16_t const value); |
| 126 | static int set_object_u8(LIBMTP_mtpdevice_t *device, uint32_t const object_id, |
| 127 | uint16_t const attribute_id, uint8_t const value); |
Linus Walleij | 8ab5426 | 2006-06-21 07:12:28 +0000 | [diff] [blame] | 128 | static void get_track_metadata(LIBMTP_mtpdevice_t *device, uint16_t objectformat, |
| 129 | LIBMTP_track_t *track); |
Linus Walleij | 4d0deea | 2007-10-19 21:27:48 +0000 | [diff] [blame] | 130 | static LIBMTP_folder_t *get_subfolders_for_folder(PTPParams *params, uint32_t parent); |
Linus Walleij | 7beba57 | 2006-11-29 08:56:12 +0000 | [diff] [blame] | 131 | static int create_new_abstract_list(LIBMTP_mtpdevice_t *device, |
| 132 | char const * const name, |
Linus Walleij | adce4a5 | 2007-04-23 07:28:11 +0000 | [diff] [blame] | 133 | char const * const artist, |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 134 | char const * const composer, |
Linus Walleij | adce4a5 | 2007-04-23 07:28:11 +0000 | [diff] [blame] | 135 | char const * const genre, |
Linus Walleij | 7beba57 | 2006-11-29 08:56:12 +0000 | [diff] [blame] | 136 | uint32_t const parenthandle, |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 137 | uint32_t const storageid, |
Linus Walleij | 7beba57 | 2006-11-29 08:56:12 +0000 | [diff] [blame] | 138 | uint16_t const objectformat, |
| 139 | char const * const suffix, |
| 140 | uint32_t * const newid, |
| 141 | uint32_t const * const tracks, |
| 142 | uint32_t const no_tracks); |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 143 | static int update_abstract_list(LIBMTP_mtpdevice_t *device, |
| 144 | char const * const name, |
| 145 | char const * const artist, |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 146 | char const * const composer, |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 147 | char const * const genre, |
| 148 | uint32_t const objecthandle, |
| 149 | uint16_t const objectformat, |
| 150 | uint32_t const * const tracks, |
| 151 | uint32_t const no_tracks); |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 152 | 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] | 153 | static void update_metadata_cache(LIBMTP_mtpdevice_t *device, uint32_t object_id); |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 154 | |
Linus Walleij | cf42f45 | 2006-11-28 08:32:49 +0000 | [diff] [blame] | 155 | /** |
| 156 | * Create a new file mapping entry |
| 157 | * @return a newly allocated filemapping entry. |
| 158 | */ |
| 159 | static filemap_t *new_filemap_entry() |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 160 | { |
Linus Walleij | cf42f45 | 2006-11-28 08:32:49 +0000 | [diff] [blame] | 161 | filemap_t *filemap; |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 162 | |
Linus Walleij | cf42f45 | 2006-11-28 08:32:49 +0000 | [diff] [blame] | 163 | filemap = (filemap_t *)malloc(sizeof(filemap_t)); |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 164 | |
Linus Walleij | f67bca9 | 2006-05-29 09:33:39 +0000 | [diff] [blame] | 165 | if( filemap != NULL ) { |
| 166 | filemap->description = NULL; |
| 167 | filemap->id = LIBMTP_FILETYPE_UNKNOWN; |
| 168 | filemap->ptp_id = PTP_OFC_Undefined; |
Linus Walleij | f67bca9 | 2006-05-29 09:33:39 +0000 | [diff] [blame] | 169 | filemap->next = NULL; |
| 170 | } |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 171 | |
Linus Walleij | f67bca9 | 2006-05-29 09:33:39 +0000 | [diff] [blame] | 172 | return filemap; |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 173 | } |
| 174 | |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 175 | /** |
| 176 | * Register an MTP or PTP filetype for data retrieval |
| 177 | * |
| 178 | * @param description Text description of filetype |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 179 | * @param id libmtp internal filetype id |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 180 | * @param ptp_id PTP filetype id |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 181 | * @return 0 for success any other value means error. |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 182 | */ |
Linus Walleij | cf42f45 | 2006-11-28 08:32:49 +0000 | [diff] [blame] | 183 | static int register_filetype(char const * const description, LIBMTP_filetype_t const id, |
| 184 | uint16_t const ptp_id) |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 185 | { |
Linus Walleij | cf42f45 | 2006-11-28 08:32:49 +0000 | [diff] [blame] | 186 | filemap_t *new = NULL, *current; |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 187 | |
| 188 | // Has this LIBMTP filetype been registered before ? |
| 189 | current = filemap; |
| 190 | while (current != NULL) { |
Linus Walleij | f67bca9 | 2006-05-29 09:33:39 +0000 | [diff] [blame] | 191 | if(current->id == id) { |
| 192 | break; |
| 193 | } |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 194 | current = current->next; |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 195 | } |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 196 | |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 197 | // Create the entry |
| 198 | if(current == NULL) { |
Linus Walleij | f67bca9 | 2006-05-29 09:33:39 +0000 | [diff] [blame] | 199 | new = new_filemap_entry(); |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 200 | if(new == NULL) { |
| 201 | return 1; |
| 202 | } |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 203 | |
Linus Walleij | f67bca9 | 2006-05-29 09:33:39 +0000 | [diff] [blame] | 204 | new->id = id; |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 205 | if(description != NULL) { |
| 206 | new->description = strdup(description); |
| 207 | } |
Linus Walleij | f67bca9 | 2006-05-29 09:33:39 +0000 | [diff] [blame] | 208 | new->ptp_id = ptp_id; |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 209 | |
Linus Walleij | f67bca9 | 2006-05-29 09:33:39 +0000 | [diff] [blame] | 210 | // Add the entry to the list |
| 211 | if(filemap == NULL) { |
| 212 | filemap = new; |
| 213 | } else { |
| 214 | current = filemap; |
| 215 | while (current->next != NULL ) current=current->next; |
| 216 | current->next = new; |
| 217 | } |
| 218 | // Update the existing entry |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 219 | } else { |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 220 | if (current->description != NULL) { |
| 221 | free(current->description); |
| 222 | } |
Linus Walleij | f67bca9 | 2006-05-29 09:33:39 +0000 | [diff] [blame] | 223 | current->description = NULL; |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 224 | if(description != NULL) { |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 225 | current->description = strdup(description); |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 226 | } |
Linus Walleij | f67bca9 | 2006-05-29 09:33:39 +0000 | [diff] [blame] | 227 | current->ptp_id = ptp_id; |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 228 | } |
| 229 | |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 230 | return 0; |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 231 | } |
| 232 | |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 233 | static void init_filemap() |
| 234 | { |
Linus Walleij | 5fb4713 | 2006-12-30 15:35:48 +0000 | [diff] [blame] | 235 | register_filetype("MediaCard", LIBMTP_FILETYPE_MEDIACARD, PTP_OFC_MTP_MediaCard); |
Linus Walleij | cf42f45 | 2006-11-28 08:32:49 +0000 | [diff] [blame] | 236 | register_filetype("RIFF WAVE file", LIBMTP_FILETYPE_WAV, PTP_OFC_WAV); |
Linus Walleij | 5fb4713 | 2006-12-30 15:35:48 +0000 | [diff] [blame] | 237 | register_filetype("ISO MPEG-1 Audio Layer 3", LIBMTP_FILETYPE_MP3, PTP_OFC_MP3); |
| 238 | 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] | 239 | register_filetype("Microsoft Windows Media Audio", LIBMTP_FILETYPE_WMA, PTP_OFC_MTP_WMA); |
| 240 | register_filetype("Ogg container format", LIBMTP_FILETYPE_OGG, PTP_OFC_MTP_OGG); |
Linus Walleij | 5fb4713 | 2006-12-30 15:35:48 +0000 | [diff] [blame] | 241 | register_filetype("Free Lossless Audio Codec (FLAC)", LIBMTP_FILETYPE_FLAC, PTP_OFC_MTP_FLAC); |
| 242 | register_filetype("Advanced Audio Coding (AAC)/MPEG-2 Part 7/MPEG-4 Part 3", LIBMTP_FILETYPE_AAC, PTP_OFC_MTP_AAC); |
| 243 | register_filetype("MPEG-4 Part 14 Container Format (Audio Empahsis)", LIBMTP_FILETYPE_M4A, PTP_OFC_MTP_M4A); |
| 244 | register_filetype("MPEG-4 Part 14 Container Format (Audio+Video Empahsis)", LIBMTP_FILETYPE_MP4, PTP_OFC_MTP_MP4); |
Linus Walleij | cf42f45 | 2006-11-28 08:32:49 +0000 | [diff] [blame] | 245 | 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] | 246 | register_filetype("Undefined audio file", LIBMTP_FILETYPE_UNDEF_AUDIO, PTP_OFC_MTP_UndefinedAudio); |
| 247 | register_filetype("Microsoft Windows Media Video", LIBMTP_FILETYPE_WMV, PTP_OFC_MTP_WMV); |
| 248 | register_filetype("Audio Video Interleave", LIBMTP_FILETYPE_AVI, PTP_OFC_AVI); |
| 249 | register_filetype("MPEG video stream", LIBMTP_FILETYPE_MPEG, PTP_OFC_MPEG); |
| 250 | register_filetype("Microsoft Advanced Systems Format", LIBMTP_FILETYPE_ASF, PTP_OFC_ASF); |
| 251 | register_filetype("Apple Quicktime container format", LIBMTP_FILETYPE_QT, PTP_OFC_QT); |
| 252 | register_filetype("Undefined video file", LIBMTP_FILETYPE_UNDEF_VIDEO, PTP_OFC_MTP_UndefinedVideo); |
| 253 | register_filetype("JPEG file", LIBMTP_FILETYPE_JPEG, PTP_OFC_EXIF_JPEG); |
Linus Walleij | 5fb4713 | 2006-12-30 15:35:48 +0000 | [diff] [blame] | 254 | register_filetype("JP2 file", LIBMTP_FILETYPE_JP2, PTP_OFC_JP2); |
| 255 | register_filetype("JPX file", LIBMTP_FILETYPE_JPX, PTP_OFC_JPX); |
Linus Walleij | cf42f45 | 2006-11-28 08:32:49 +0000 | [diff] [blame] | 256 | register_filetype("JFIF file", LIBMTP_FILETYPE_JFIF, PTP_OFC_JFIF); |
| 257 | register_filetype("TIFF bitmap file", LIBMTP_FILETYPE_TIFF, PTP_OFC_TIFF); |
| 258 | register_filetype("BMP bitmap file", LIBMTP_FILETYPE_BMP, PTP_OFC_BMP); |
| 259 | register_filetype("GIF bitmap file", LIBMTP_FILETYPE_GIF, PTP_OFC_GIF); |
| 260 | register_filetype("PICT bitmap file", LIBMTP_FILETYPE_PICT, PTP_OFC_PICT); |
| 261 | register_filetype("Portable Network Graphics", LIBMTP_FILETYPE_PNG, PTP_OFC_PNG); |
| 262 | register_filetype("Microsoft Windows Image Format", LIBMTP_FILETYPE_WINDOWSIMAGEFORMAT, PTP_OFC_MTP_WindowsImageFormat); |
| 263 | register_filetype("VCalendar version 1", LIBMTP_FILETYPE_VCALENDAR1, PTP_OFC_MTP_vCalendar1); |
| 264 | register_filetype("VCalendar version 2", LIBMTP_FILETYPE_VCALENDAR2, PTP_OFC_MTP_vCalendar2); |
| 265 | register_filetype("VCard version 2", LIBMTP_FILETYPE_VCARD2, PTP_OFC_MTP_vCard2); |
| 266 | register_filetype("VCard version 3", LIBMTP_FILETYPE_VCARD3, PTP_OFC_MTP_vCard3); |
| 267 | register_filetype("Undefined Windows executable file", LIBMTP_FILETYPE_WINEXEC, PTP_OFC_MTP_UndefinedWindowsExecutable); |
| 268 | register_filetype("Text file", LIBMTP_FILETYPE_TEXT, PTP_OFC_Text); |
| 269 | register_filetype("HTML file", LIBMTP_FILETYPE_HTML, PTP_OFC_HTML); |
Linus Walleij | 5fb4713 | 2006-12-30 15:35:48 +0000 | [diff] [blame] | 270 | register_filetype("XML file", LIBMTP_FILETYPE_XML, PTP_OFC_MTP_XMLDocument); |
| 271 | register_filetype("DOC file", LIBMTP_FILETYPE_DOC, PTP_OFC_MTP_MSWordDocument); |
| 272 | register_filetype("XLS file", LIBMTP_FILETYPE_XLS, PTP_OFC_MTP_MSExcelSpreadsheetXLS); |
| 273 | register_filetype("PPT file", LIBMTP_FILETYPE_PPT, PTP_OFC_MTP_MSPowerpointPresentationPPT); |
| 274 | register_filetype("MHT file", LIBMTP_FILETYPE_MHT, PTP_OFC_MTP_MHTCompiledHTMLDocument); |
Linus Walleij | a05b980 | 2006-12-08 08:50:30 +0000 | [diff] [blame] | 275 | register_filetype("Firmware file", LIBMTP_FILETYPE_FIRMWARE, PTP_OFC_MTP_Firmware); |
Linus Walleij | cf42f45 | 2006-11-28 08:32:49 +0000 | [diff] [blame] | 276 | register_filetype("Undefined filetype", LIBMTP_FILETYPE_UNKNOWN, PTP_OFC_Undefined); |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 277 | } |
Linus Walleij | 16c51f0 | 2006-05-04 13:20:22 +0000 | [diff] [blame] | 278 | |
Linus Walleij | 16c51f0 | 2006-05-04 13:20:22 +0000 | [diff] [blame] | 279 | /** |
| 280 | * Returns the PTP filetype that maps to a certain libmtp internal file type. |
| 281 | * @param intype the MTP library interface type |
| 282 | * @return the PTP (libgphoto2) interface type |
| 283 | */ |
| 284 | static uint16_t map_libmtp_type_to_ptp_type(LIBMTP_filetype_t intype) |
| 285 | { |
Linus Walleij | cf42f45 | 2006-11-28 08:32:49 +0000 | [diff] [blame] | 286 | filemap_t *current; |
Linus Walleij | 16c51f0 | 2006-05-04 13:20:22 +0000 | [diff] [blame] | 287 | |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 288 | current = filemap; |
| 289 | |
| 290 | while (current != NULL) { |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 291 | if(current->id == intype) { |
| 292 | return current->ptp_id; |
| 293 | } |
| 294 | current = current->next; |
Linus Walleij | 16c51f0 | 2006-05-04 13:20:22 +0000 | [diff] [blame] | 295 | } |
Linus Walleij | 1fd2d27 | 2006-05-08 09:22:01 +0000 | [diff] [blame] | 296 | // printf("map_libmtp_type_to_ptp_type: unknown filetype.\n"); |
Linus Walleij | 16c51f0 | 2006-05-04 13:20:22 +0000 | [diff] [blame] | 297 | return PTP_OFC_Undefined; |
| 298 | } |
| 299 | |
| 300 | |
| 301 | /** |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 302 | * Returns the MTP internal interface type that maps to a certain ptp |
| 303 | * interface type. |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 304 | * @param intype the PTP (libgphoto2) interface type |
Linus Walleij | 8ab5426 | 2006-06-21 07:12:28 +0000 | [diff] [blame] | 305 | * @return the MTP library interface type |
Linus Walleij | 16c51f0 | 2006-05-04 13:20:22 +0000 | [diff] [blame] | 306 | */ |
| 307 | static LIBMTP_filetype_t map_ptp_type_to_libmtp_type(uint16_t intype) |
| 308 | { |
Linus Walleij | cf42f45 | 2006-11-28 08:32:49 +0000 | [diff] [blame] | 309 | filemap_t *current; |
Linus Walleij | 16c51f0 | 2006-05-04 13:20:22 +0000 | [diff] [blame] | 310 | |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 311 | current = filemap; |
| 312 | |
| 313 | while (current != NULL) { |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 314 | if(current->ptp_id == intype) { |
| 315 | return current->id; |
| 316 | } |
| 317 | current = current->next; |
Linus Walleij | 16c51f0 | 2006-05-04 13:20:22 +0000 | [diff] [blame] | 318 | } |
Linus Walleij | 1fd2d27 | 2006-05-08 09:22:01 +0000 | [diff] [blame] | 319 | // printf("map_ptp_type_to_libmtp_type: unknown filetype.\n"); |
Linus Walleij | 16c51f0 | 2006-05-04 13:20:22 +0000 | [diff] [blame] | 320 | return LIBMTP_FILETYPE_UNKNOWN; |
| 321 | } |
| 322 | |
Linus Walleij | fa1374c | 2006-02-27 07:41:46 +0000 | [diff] [blame] | 323 | |
Linus Walleij | 6946ac5 | 2006-03-21 06:51:22 +0000 | [diff] [blame] | 324 | /** |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 325 | * Initialize the library. You are only supposed to call this |
| 326 | * one, before using the library for the first time in a program. |
| 327 | * Never re-initialize libmtp! |
| 328 | * |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 329 | * The only thing this does at the moment is to initialise the |
| 330 | * filetype mapping table. |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 331 | */ |
| 332 | void LIBMTP_Init(void) |
| 333 | { |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 334 | init_filemap(); |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 335 | return; |
| 336 | } |
| 337 | |
Linus Walleij | cf42f45 | 2006-11-28 08:32:49 +0000 | [diff] [blame] | 338 | |
| 339 | /** |
| 340 | * This helper function returns a textual description for a libmtp |
| 341 | * file type to be used in dialog boxes etc. |
| 342 | * @param intype the libmtp internal filetype to get a description for. |
| 343 | * @return a string representing the filetype, this must <b>NOT</b> |
| 344 | * be free():ed by the caller! |
| 345 | */ |
| 346 | char const * LIBMTP_Get_Filetype_Description(LIBMTP_filetype_t intype) |
| 347 | { |
| 348 | filemap_t *current; |
| 349 | |
| 350 | current = filemap; |
| 351 | |
| 352 | while (current != NULL) { |
| 353 | if(current->id == intype) { |
| 354 | return current->description; |
| 355 | } |
| 356 | current = current->next; |
| 357 | } |
| 358 | |
| 359 | return "Unknown filetype"; |
| 360 | } |
| 361 | |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 362 | /** |
| 363 | * This function will do its best to fit a 16bit |
| 364 | * value into a PTP object property if the property |
| 365 | * is limited in range or step sizes. |
| 366 | */ |
Linus Walleij | 2955956 | 2007-08-22 21:38:04 +0000 | [diff] [blame] | 367 | static uint16_t adjust_u16(uint16_t val, PTPObjectPropDesc *opd) |
| 368 | { |
| 369 | switch (opd->FormFlag) { |
| 370 | case PTP_DPFF_Range: |
| 371 | if (val < opd->FORM.Range.MinimumValue.u16) { |
| 372 | return opd->FORM.Range.MinimumValue.u16; |
| 373 | } |
| 374 | if (val > opd->FORM.Range.MaximumValue.u16) { |
| 375 | return opd->FORM.Range.MaximumValue.u16; |
| 376 | } |
| 377 | // Round down to last step. |
| 378 | if (val % opd->FORM.Range.StepSize.u16 != 0) { |
| 379 | return val - (val % opd->FORM.Range.StepSize.u16); |
| 380 | } |
| 381 | return val; |
| 382 | break; |
| 383 | case PTP_DPFF_Enumeration: |
| 384 | { |
| 385 | int i; |
| 386 | uint16_t bestfit = opd->FORM.Enum.SupportedValue[0].u16; |
| 387 | |
| 388 | for (i=0; i<opd->FORM.Enum.NumberOfValues; i++) { |
| 389 | if (val == opd->FORM.Enum.SupportedValue[i].u16) { |
| 390 | return val; |
| 391 | } |
| 392 | // Rough guess of best fit |
| 393 | if (opd->FORM.Enum.SupportedValue[i].u16 < val) { |
| 394 | bestfit = opd->FORM.Enum.SupportedValue[i].u16; |
| 395 | } |
| 396 | } |
| 397 | // Just some default that'll work. |
| 398 | return bestfit; |
| 399 | } |
| 400 | default: |
| 401 | // Will accept any value |
| 402 | break; |
| 403 | } |
| 404 | return val; |
| 405 | } |
| 406 | |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 407 | /** |
| 408 | * This function will do its best to fit a 32bit |
| 409 | * value into a PTP object property if the property |
| 410 | * is limited in range or step sizes. |
| 411 | */ |
Linus Walleij | 2955956 | 2007-08-22 21:38:04 +0000 | [diff] [blame] | 412 | static uint32_t adjust_u32(uint32_t val, PTPObjectPropDesc *opd) |
| 413 | { |
| 414 | switch (opd->FormFlag) { |
| 415 | case PTP_DPFF_Range: |
| 416 | if (val < opd->FORM.Range.MinimumValue.u32) { |
| 417 | return opd->FORM.Range.MinimumValue.u32; |
| 418 | } |
| 419 | if (val > opd->FORM.Range.MaximumValue.u32) { |
| 420 | return opd->FORM.Range.MaximumValue.u32; |
| 421 | } |
| 422 | // Round down to last step. |
| 423 | if (val % opd->FORM.Range.StepSize.u32 != 0) { |
| 424 | return val - (val % opd->FORM.Range.StepSize.u32); |
| 425 | } |
| 426 | return val; |
| 427 | break; |
| 428 | case PTP_DPFF_Enumeration: |
| 429 | { |
| 430 | int i; |
| 431 | uint32_t bestfit = opd->FORM.Enum.SupportedValue[0].u32; |
| 432 | |
| 433 | for (i=0; i<opd->FORM.Enum.NumberOfValues; i++) { |
| 434 | if (val == opd->FORM.Enum.SupportedValue[i].u32) { |
| 435 | return val; |
| 436 | } |
| 437 | // Rough guess of best fit |
| 438 | if (opd->FORM.Enum.SupportedValue[i].u32 < val) { |
| 439 | bestfit = opd->FORM.Enum.SupportedValue[i].u32; |
| 440 | } |
| 441 | } |
| 442 | // Just some default that'll work. |
| 443 | return bestfit; |
| 444 | } |
| 445 | default: |
| 446 | // Will accept any value |
| 447 | break; |
| 448 | } |
| 449 | return val; |
| 450 | } |
| 451 | |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 452 | /** |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 453 | * This function returns a newly created ISO 8601 timestamp with the |
| 454 | * current time in as high precision as possible. It even adds |
| 455 | * the time zone if it can. |
| 456 | */ |
| 457 | static char *get_iso8601_stamp(void) |
| 458 | { |
| 459 | time_t curtime; |
| 460 | struct tm *loctime; |
| 461 | char tmp[64]; |
| 462 | |
| 463 | curtime = time(NULL); |
| 464 | loctime = localtime(&curtime); |
| 465 | strftime (tmp, sizeof(tmp), "%Y%m%dT%H%M%S.0%z", loctime); |
| 466 | return strdup(tmp); |
| 467 | } |
| 468 | |
| 469 | /** |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 470 | * Retrieves a string from an object |
| 471 | * |
| 472 | * @param device a pointer to an MTP device. |
| 473 | * @param object_id Object reference |
| 474 | * @param attribute_id PTP attribute ID |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 475 | * @return valid string or NULL on failure. The returned string |
| 476 | * must bee <code>free()</code>:ed by the caller after |
| 477 | * use. |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 478 | */ |
Linus Walleij | 9901e22 | 2006-11-30 12:28:19 +0000 | [diff] [blame] | 479 | 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] | 480 | uint16_t const attribute_id) |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 481 | { |
| 482 | PTPPropertyValue propval; |
| 483 | char *retstring = NULL; |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 484 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 485 | uint16_t ret; |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 486 | |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 487 | if ( device == NULL || object_id == 0) { |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 488 | return NULL; |
| 489 | } |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 490 | |
| 491 | // This O(n) search should not be used so often, since code |
| 492 | // using the cached properties don't usually call this function. |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 493 | if (params->props) { |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 494 | MTPProperties *prop = ptp_find_object_prop_in_cache(params, object_id, attribute_id); |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 495 | if (prop) { |
Richard Low | 0fa8352 | 2007-07-17 20:05:08 +0000 | [diff] [blame] | 496 | if (prop->propval.str != NULL) |
| 497 | return strdup(prop->propval.str); |
| 498 | else |
| 499 | return NULL; |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 500 | } |
| 501 | } |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 502 | |
Linus Walleij | a823a70 | 2006-08-27 21:27:46 +0000 | [diff] [blame] | 503 | ret = ptp_mtp_getobjectpropvalue(params, object_id, attribute_id, &propval, PTP_DTC_STR); |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 504 | if (ret == PTP_RC_OK) { |
Linus Walleij | a823a70 | 2006-08-27 21:27:46 +0000 | [diff] [blame] | 505 | if (propval.str != NULL) { |
| 506 | retstring = (char *) strdup(propval.str); |
| 507 | free(propval.str); |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 508 | } |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 509 | } else { |
| 510 | 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] | 511 | } |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 512 | |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 513 | return retstring; |
| 514 | } |
| 515 | |
| 516 | /** |
Richard Low | c3f5fc3 | 2007-07-29 09:40:50 +0000 | [diff] [blame] | 517 | * Retrieves an unsigned 64-bit integer from an object attribute |
| 518 | * |
| 519 | * @param device a pointer to an MTP device. |
| 520 | * @param object_id Object reference |
| 521 | * @param attribute_id PTP attribute ID |
| 522 | * @param value_default Default value to return on failure |
| 523 | * @return the value |
| 524 | */ |
| 525 | static uint64_t get_u64_from_object(LIBMTP_mtpdevice_t *device,uint32_t const object_id, |
| 526 | uint16_t const attribute_id, uint64_t const value_default) |
| 527 | { |
| 528 | PTPPropertyValue propval; |
| 529 | uint64_t retval = value_default; |
| 530 | PTPParams *params = (PTPParams *) device->params; |
| 531 | uint16_t ret; |
| 532 | |
| 533 | if ( device == NULL ) { |
| 534 | return value_default; |
| 535 | } |
| 536 | |
| 537 | // This O(n) search should not be used so often, since code |
| 538 | // using the cached properties don't usually call this function. |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 539 | if (params->props) { |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 540 | MTPProperties *prop = ptp_find_object_prop_in_cache(params, object_id, attribute_id); |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 541 | if (prop) |
| 542 | return prop->propval.u64; |
Richard Low | c3f5fc3 | 2007-07-29 09:40:50 +0000 | [diff] [blame] | 543 | } |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 544 | |
Richard Low | c3f5fc3 | 2007-07-29 09:40:50 +0000 | [diff] [blame] | 545 | ret = ptp_mtp_getobjectpropvalue(params, object_id, |
| 546 | attribute_id, |
| 547 | &propval, |
| 548 | PTP_DTC_UINT64); |
| 549 | if (ret == PTP_RC_OK) { |
| 550 | retval = propval.u64; |
| 551 | } else { |
| 552 | add_ptp_error_to_errorstack(device, ret, "get_u64_from_object(): could not get unsigned 64bit integer from object."); |
| 553 | } |
| 554 | |
| 555 | return retval; |
| 556 | } |
| 557 | |
| 558 | /** |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 559 | * Retrieves an unsigned 32-bit integer from an object attribute |
| 560 | * |
| 561 | * @param device a pointer to an MTP device. |
| 562 | * @param object_id Object reference |
| 563 | * @param attribute_id PTP attribute ID |
Linus Walleij | 5acfa74 | 2006-05-29 14:51:59 +0000 | [diff] [blame] | 564 | * @param value_default Default value to return on failure |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 565 | * @return the value |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 566 | */ |
Linus Walleij | 9901e22 | 2006-11-30 12:28:19 +0000 | [diff] [blame] | 567 | 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] | 568 | uint16_t const attribute_id, uint32_t const value_default) |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 569 | { |
| 570 | PTPPropertyValue propval; |
| 571 | uint32_t retval = value_default; |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 572 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 573 | uint16_t ret; |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 574 | |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 575 | if ( device == NULL ) { |
| 576 | return value_default; |
| 577 | } |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 578 | |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 579 | // This O(n) search should not be used so often, since code |
| 580 | // using the cached properties don't usually call this function. |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 581 | if (params->props) { |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 582 | MTPProperties *prop = ptp_find_object_prop_in_cache(params, object_id, attribute_id); |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 583 | if (prop) |
| 584 | return prop->propval.u32; |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 585 | } |
| 586 | |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 587 | ret = ptp_mtp_getobjectpropvalue(params, object_id, |
| 588 | attribute_id, |
| 589 | &propval, |
| 590 | PTP_DTC_UINT32); |
| 591 | if (ret == PTP_RC_OK) { |
| 592 | retval = propval.u32; |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 593 | } else { |
| 594 | 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] | 595 | } |
| 596 | |
| 597 | return retval; |
| 598 | } |
| 599 | |
| 600 | /** |
| 601 | * Retrieves an unsigned 16-bit integer from an object attribute |
| 602 | * |
| 603 | * @param device a pointer to an MTP device. |
| 604 | * @param object_id Object reference |
| 605 | * @param attribute_id PTP attribute ID |
Linus Walleij | 5acfa74 | 2006-05-29 14:51:59 +0000 | [diff] [blame] | 606 | * @param value_default Default value to return on failure |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 607 | * @return a value |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 608 | */ |
Linus Walleij | 9901e22 | 2006-11-30 12:28:19 +0000 | [diff] [blame] | 609 | 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] | 610 | uint16_t const attribute_id, uint16_t const value_default) |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 611 | { |
| 612 | PTPPropertyValue propval; |
| 613 | uint16_t retval = value_default; |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 614 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 615 | uint16_t ret; |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 616 | |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 617 | if ( device == NULL ) { |
| 618 | return value_default; |
| 619 | } |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 620 | |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 621 | // This O(n) search should not be used so often, since code |
| 622 | // using the cached properties don't usually call this function. |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 623 | if (params->props) { |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 624 | MTPProperties *prop = ptp_find_object_prop_in_cache(params, object_id, attribute_id); |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 625 | if (prop) |
| 626 | return prop->propval.u16; |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 627 | } |
| 628 | |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 629 | ret = ptp_mtp_getobjectpropvalue(params, object_id, |
| 630 | attribute_id, |
| 631 | &propval, |
| 632 | PTP_DTC_UINT16); |
| 633 | if (ret == PTP_RC_OK) { |
| 634 | retval = propval.u16; |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 635 | } else { |
| 636 | 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] | 637 | } |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 638 | |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 639 | return retval; |
| 640 | } |
| 641 | |
| 642 | /** |
Linus Walleij | 99310d4 | 2006-11-01 08:29:39 +0000 | [diff] [blame] | 643 | * Retrieves an unsigned 8-bit integer from an object attribute |
| 644 | * |
| 645 | * @param device a pointer to an MTP device. |
| 646 | * @param object_id Object reference |
| 647 | * @param attribute_id PTP attribute ID |
| 648 | * @param value_default Default value to return on failure |
| 649 | * @return a value |
| 650 | */ |
Linus Walleij | 9901e22 | 2006-11-30 12:28:19 +0000 | [diff] [blame] | 651 | 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] | 652 | uint16_t const attribute_id, uint8_t const value_default) |
| 653 | { |
| 654 | PTPPropertyValue propval; |
| 655 | uint8_t retval = value_default; |
| 656 | PTPParams *params = (PTPParams *) device->params; |
| 657 | uint16_t ret; |
| 658 | |
| 659 | if ( device == NULL ) { |
| 660 | return value_default; |
| 661 | } |
| 662 | |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 663 | // This O(n) search should not be used so often, since code |
| 664 | // using the cached properties don't usually call this function. |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 665 | if (params->props) { |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 666 | MTPProperties *prop = ptp_find_object_prop_in_cache(params, object_id, attribute_id); |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 667 | if (prop) |
| 668 | return prop->propval.u8; |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 669 | } |
| 670 | |
Linus Walleij | 99310d4 | 2006-11-01 08:29:39 +0000 | [diff] [blame] | 671 | ret = ptp_mtp_getobjectpropvalue(params, object_id, |
| 672 | attribute_id, |
| 673 | &propval, |
| 674 | PTP_DTC_UINT8); |
| 675 | if (ret == PTP_RC_OK) { |
| 676 | retval = propval.u8; |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 677 | } else { |
| 678 | 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] | 679 | } |
| 680 | |
| 681 | return retval; |
| 682 | } |
| 683 | |
| 684 | /** |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 685 | * Sets an object attribute from a string |
| 686 | * |
| 687 | * @param device a pointer to an MTP device. |
| 688 | * @param object_id Object reference |
| 689 | * @param attribute_id PTP attribute ID |
| 690 | * @param string string value to set |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 691 | * @return 0 on success, any other value means failure |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 692 | */ |
Linus Walleij | 9901e22 | 2006-11-30 12:28:19 +0000 | [diff] [blame] | 693 | 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] | 694 | uint16_t const attribute_id, char const * const string) |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 695 | { |
| 696 | PTPPropertyValue propval; |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 697 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 698 | uint16_t ret; |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 699 | |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 700 | if (device == NULL || string == NULL) { |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 701 | return -1; |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 702 | } |
Linus Walleij | b275318 | 2007-02-26 08:11:38 +0000 | [diff] [blame] | 703 | |
| 704 | if (!ptp_operation_issupported(params,PTP_OC_MTP_SetObjectPropValue)) { |
| 705 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "set_object_string(): could not set object string: " |
| 706 | "PTP_OC_MTP_SetObjectPropValue not supported."); |
| 707 | return -1; |
| 708 | } |
Linus Walleij | a823a70 | 2006-08-27 21:27:46 +0000 | [diff] [blame] | 709 | propval.str = (char *) string; |
| 710 | 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] | 711 | if (ret != PTP_RC_OK) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 712 | 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] | 713 | return -1; |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 714 | } |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 715 | |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 716 | return 0; |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 717 | } |
| 718 | |
Linus Walleij | 2955956 | 2007-08-22 21:38:04 +0000 | [diff] [blame] | 719 | |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 720 | /** |
| 721 | * Sets an object attribute from an unsigned 32-bit integer |
| 722 | * |
| 723 | * @param device a pointer to an MTP device. |
| 724 | * @param object_id Object reference |
| 725 | * @param attribute_id PTP attribute ID |
| 726 | * @param value 32-bit unsigned integer to set |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 727 | * @return 0 on success, any other value means failure |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 728 | */ |
Linus Walleij | 9901e22 | 2006-11-30 12:28:19 +0000 | [diff] [blame] | 729 | 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] | 730 | uint16_t const attribute_id, uint32_t const value) |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 731 | { |
| 732 | PTPPropertyValue propval; |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 733 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 734 | uint16_t ret; |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 735 | |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 736 | if (device == NULL) { |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 737 | return -1; |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 738 | } |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 739 | |
Linus Walleij | b275318 | 2007-02-26 08:11:38 +0000 | [diff] [blame] | 740 | if (!ptp_operation_issupported(params,PTP_OC_MTP_SetObjectPropValue)) { |
| 741 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "set_object_u32(): could not set unsigned 32bit integer property: " |
| 742 | "PTP_OC_MTP_SetObjectPropValue not supported."); |
| 743 | return -1; |
| 744 | } |
Linus Walleij | 2955956 | 2007-08-22 21:38:04 +0000 | [diff] [blame] | 745 | |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 746 | propval.u32 = value; |
| 747 | 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] | 748 | if (ret != PTP_RC_OK) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 749 | 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] | 750 | return -1; |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 751 | } |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 752 | |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 753 | return 0; |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 754 | } |
| 755 | |
| 756 | /** |
| 757 | * Sets an object attribute from an unsigned 16-bit integer |
| 758 | * |
| 759 | * @param device a pointer to an MTP device. |
| 760 | * @param object_id Object reference |
| 761 | * @param attribute_id PTP attribute ID |
| 762 | * @param value 16-bit unsigned integer to set |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 763 | * @return 0 on success, any other value means failure |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 764 | */ |
Linus Walleij | 9901e22 | 2006-11-30 12:28:19 +0000 | [diff] [blame] | 765 | 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] | 766 | uint16_t const attribute_id, uint16_t const value) |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 767 | { |
| 768 | PTPPropertyValue propval; |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 769 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 770 | uint16_t ret; |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 771 | |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 772 | if (device == NULL) { |
| 773 | return 1; |
| 774 | } |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 775 | |
Linus Walleij | b275318 | 2007-02-26 08:11:38 +0000 | [diff] [blame] | 776 | if (!ptp_operation_issupported(params,PTP_OC_MTP_SetObjectPropValue)) { |
| 777 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "set_object_u16(): could not set unsigned 16bit integer property: " |
| 778 | "PTP_OC_MTP_SetObjectPropValue not supported."); |
| 779 | return -1; |
| 780 | } |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 781 | propval.u16 = value; |
| 782 | 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] | 783 | if (ret != PTP_RC_OK) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 784 | 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] | 785 | return 1; |
| 786 | } |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 787 | |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 788 | return 0; |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 789 | } |
| 790 | |
| 791 | /** |
Linus Walleij | 99310d4 | 2006-11-01 08:29:39 +0000 | [diff] [blame] | 792 | * Sets an object attribute from an unsigned 8-bit integer |
| 793 | * |
| 794 | * @param device a pointer to an MTP device. |
| 795 | * @param object_id Object reference |
| 796 | * @param attribute_id PTP attribute ID |
| 797 | * @param value 8-bit unsigned integer to set |
| 798 | * @return 0 on success, any other value means failure |
| 799 | */ |
Linus Walleij | 9901e22 | 2006-11-30 12:28:19 +0000 | [diff] [blame] | 800 | static int set_object_u8(LIBMTP_mtpdevice_t *device, uint32_t const object_id, |
| 801 | uint16_t const attribute_id, uint8_t const value) |
Linus Walleij | 99310d4 | 2006-11-01 08:29:39 +0000 | [diff] [blame] | 802 | { |
| 803 | PTPPropertyValue propval; |
| 804 | PTPParams *params = (PTPParams *) device->params; |
| 805 | uint16_t ret; |
| 806 | |
| 807 | if (device == NULL) { |
| 808 | return 1; |
| 809 | } |
| 810 | |
Linus Walleij | b275318 | 2007-02-26 08:11:38 +0000 | [diff] [blame] | 811 | if (!ptp_operation_issupported(params,PTP_OC_MTP_SetObjectPropValue)) { |
| 812 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "set_object_u8(): could not set unsigned 8bit integer property: " |
| 813 | "PTP_OC_MTP_SetObjectPropValue not supported."); |
| 814 | return -1; |
| 815 | } |
Linus Walleij | 99310d4 | 2006-11-01 08:29:39 +0000 | [diff] [blame] | 816 | propval.u8 = value; |
| 817 | ret = ptp_mtp_setobjectpropvalue(params, object_id, attribute_id, &propval, PTP_DTC_UINT8); |
| 818 | if (ret != PTP_RC_OK) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 819 | 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] | 820 | return 1; |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 821 | } |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 822 | |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 823 | return 0; |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 824 | } |
| 825 | |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 826 | /** |
Linus Walleij | 039d1dd | 2007-02-23 22:34:07 +0000 | [diff] [blame] | 827 | * 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] | 828 | * @return a device pointer. |
Linus Walleij | 039d1dd | 2007-02-23 22:34:07 +0000 | [diff] [blame] | 829 | * @see LIBMTP_Get_Connected_Devices() |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 830 | */ |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 831 | LIBMTP_mtpdevice_t *LIBMTP_Get_First_Device(void) |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 832 | { |
tedbullock | e771364 | 2007-02-18 23:10:09 +0000 | [diff] [blame] | 833 | LIBMTP_mtpdevice_t *first_device = NULL; |
Linus Walleij | 64691b7 | 2008-05-31 22:55:18 +0000 | [diff] [blame] | 834 | LIBMTP_raw_device_t *devices; |
| 835 | int numdevs; |
| 836 | LIBMTP_error_number_t ret; |
tedbullock | e771364 | 2007-02-18 23:10:09 +0000 | [diff] [blame] | 837 | |
Linus Walleij | 64691b7 | 2008-05-31 22:55:18 +0000 | [diff] [blame] | 838 | ret = LIBMTP_Detect_Raw_Devices(&devices, &numdevs); |
| 839 | if (ret != LIBMTP_ERROR_NONE) { |
Richard Low | 0f79476 | 2007-02-23 22:06:27 +0000 | [diff] [blame] | 840 | return NULL; |
Linus Walleij | a8a19cc | 2007-02-02 22:13:17 +0000 | [diff] [blame] | 841 | } |
tedbullock | 44b0ff7 | 2007-02-22 22:20:28 +0000 | [diff] [blame] | 842 | |
Linus Walleij | 64691b7 | 2008-05-31 22:55:18 +0000 | [diff] [blame] | 843 | if (devices == NULL || numdevs == 0) { |
| 844 | return NULL; |
tedbullock | 44b0ff7 | 2007-02-22 22:20:28 +0000 | [diff] [blame] | 845 | } |
Linus Walleij | 64691b7 | 2008-05-31 22:55:18 +0000 | [diff] [blame] | 846 | |
| 847 | first_device = LIBMTP_Open_Raw_Device(&devices[0]); |
| 848 | free(devices); |
tedbullock | e771364 | 2007-02-18 23:10:09 +0000 | [diff] [blame] | 849 | return first_device; |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 850 | } |
| 851 | |
| 852 | /** |
Linus Walleij | bdb89bd | 2008-05-28 23:32:35 +0000 | [diff] [blame] | 853 | * This function opens a device from a raw device. It is the |
| 854 | * preferred way to access devices in the new interface where |
| 855 | * several devices can come and go as the library is working |
| 856 | * on a certain device. |
| 857 | * @param rawdevice the raw device to open a "real" device for. |
| 858 | * @return an open device. |
| 859 | */ |
| 860 | LIBMTP_mtpdevice_t *LIBMTP_Open_Raw_Device(LIBMTP_raw_device_t *rawdevice) |
| 861 | { |
| 862 | LIBMTP_mtpdevice_t *mtp_device; |
| 863 | uint8_t bs = 0; |
| 864 | PTPParams *current_params; |
Linus Walleij | 3c64325 | 2008-05-31 23:22:28 +0000 | [diff] [blame] | 865 | PTP_USB *ptp_usb; |
Linus Walleij | bdb89bd | 2008-05-28 23:32:35 +0000 | [diff] [blame] | 866 | LIBMTP_error_number_t err; |
| 867 | int i; |
| 868 | |
| 869 | /* Allocate dynamic space for our device */ |
| 870 | mtp_device = (LIBMTP_mtpdevice_t *) malloc(sizeof(LIBMTP_mtpdevice_t)); |
| 871 | /* Check if there was a memory allocation error */ |
| 872 | if(mtp_device == NULL) { |
| 873 | /* There has been an memory allocation error. We are going to ignore this |
| 874 | device and attempt to continue */ |
| 875 | |
| 876 | /* TODO: This error statement could probably be a bit more robust */ |
| 877 | fprintf(stderr, "LIBMTP PANIC: connect_usb_devices encountered a memory " |
| 878 | "allocation error with device %d on bus %d, trying to continue", |
| 879 | rawdevice->devnum, rawdevice->bus_location); |
| 880 | |
| 881 | return NULL; |
| 882 | } |
Linus Walleij | 3c64325 | 2008-05-31 23:22:28 +0000 | [diff] [blame] | 883 | |
| 884 | /* Create PTP params */ |
| 885 | current_params = (PTPParams *) malloc(sizeof(PTPParams)); |
| 886 | if (current_params == NULL) { |
| 887 | free(mtp_device); |
Linus Walleij | bdb89bd | 2008-05-28 23:32:35 +0000 | [diff] [blame] | 888 | return NULL; |
Linus Walleij | 3c64325 | 2008-05-31 23:22:28 +0000 | [diff] [blame] | 889 | } |
| 890 | memset(current_params, 0, sizeof(PTPParams)); |
| 891 | /* Clear all handlers */ |
Linus Walleij | bdb89bd | 2008-05-28 23:32:35 +0000 | [diff] [blame] | 892 | current_params->handles.Handler = NULL; |
| 893 | current_params->objectinfo = NULL; |
| 894 | current_params->props = NULL; |
Linus Walleij | 3c64325 | 2008-05-31 23:22:28 +0000 | [diff] [blame] | 895 | /* TODO: Will this always be little endian? */ |
| 896 | current_params->byteorder = PTP_DL_LE; |
| 897 | current_params->cd_locale_to_ucs2 = iconv_open("UCS-2LE", "UTF-8"); |
| 898 | current_params->cd_ucs2_to_locale = iconv_open("UTF-8", "UCS-2LE"); |
| 899 | |
| 900 | if(current_params->cd_locale_to_ucs2 == (iconv_t) -1 || |
| 901 | current_params->cd_ucs2_to_locale == (iconv_t) -1) { |
| 902 | fprintf(stderr, "LIBMTP PANIC: Cannot open iconv() converters to/from UCS-2!\n" |
| 903 | "Too old stdlibc, glibc and libiconv?\n"); |
| 904 | free(current_params); |
| 905 | free(mtp_device); |
| 906 | return NULL; |
| 907 | } |
| 908 | mtp_device->params = current_params; |
| 909 | |
| 910 | |
| 911 | /* Create usbinfo, this also opens the session */ |
| 912 | err = configure_usb_device(rawdevice, |
| 913 | current_params, |
| 914 | &mtp_device->usbinfo); |
| 915 | if (err != LIBMTP_ERROR_NONE) { |
| 916 | free(current_params); |
| 917 | free(mtp_device); |
| 918 | return NULL; |
| 919 | } |
| 920 | ptp_usb = (PTP_USB*) mtp_device->usbinfo; |
| 921 | /* Set pointer back to params */ |
| 922 | ptp_usb->params = current_params; |
| 923 | |
Linus Walleij | bdb89bd | 2008-05-28 23:32:35 +0000 | [diff] [blame] | 924 | |
| 925 | /* Cache the device information for later use */ |
| 926 | if (ptp_getdeviceinfo(current_params, |
| 927 | ¤t_params->deviceinfo) != PTP_RC_OK) { |
| 928 | fprintf(stderr, "LIBMTP PANIC: Unable to read device information on device " |
| 929 | "%d on bus %d, trying to continue", |
| 930 | rawdevice->devnum, rawdevice->bus_location); |
| 931 | |
| 932 | /* Prevent memory leaks for this device */ |
| 933 | free(mtp_device->usbinfo); |
| 934 | free(mtp_device->params); |
| 935 | current_params = NULL; |
Linus Walleij | 3c64325 | 2008-05-31 23:22:28 +0000 | [diff] [blame] | 936 | free(mtp_device); |
Linus Walleij | bdb89bd | 2008-05-28 23:32:35 +0000 | [diff] [blame] | 937 | return NULL; |
| 938 | } |
| 939 | |
| 940 | /* Determine if the object size supported is 32 or 64 bit wide */ |
| 941 | for (i=0;i<current_params->deviceinfo.ImageFormats_len;i++) { |
| 942 | PTPObjectPropDesc opd; |
| 943 | |
| 944 | if (ptp_mtp_getobjectpropdesc(current_params, |
| 945 | PTP_OPC_ObjectSize, |
| 946 | current_params->deviceinfo.ImageFormats[i], |
| 947 | &opd) != PTP_RC_OK) { |
| 948 | printf("LIBMTP PANIC: create_usb_mtp_devices(): " |
| 949 | "could not inspect object property descriptions!\n"); |
| 950 | } else { |
| 951 | if (opd.DataType == PTP_DTC_UINT32) { |
| 952 | if (bs == 0) { |
| 953 | bs = 32; |
| 954 | } else if (bs != 32) { |
| 955 | printf("LIBMTP PANIC: create_usb_mtp_devices(): " |
| 956 | "different objects support different object sizes!\n"); |
| 957 | bs = 0; |
| 958 | break; |
| 959 | } |
| 960 | } else if (opd.DataType == PTP_DTC_UINT64) { |
| 961 | if (bs == 0) { |
| 962 | bs = 64; |
| 963 | } else if (bs != 64) { |
| 964 | printf("LIBMTP PANIC: create_usb_mtp_devices(): " |
| 965 | "different objects support different object sizes!\n"); |
| 966 | bs = 0; |
| 967 | break; |
| 968 | } |
| 969 | } else { |
| 970 | // Ignore if other size. |
| 971 | printf("LIBMTP PANIC: create_usb_mtp_devices(): " |
| 972 | "awkward object size data type: %04x\n", opd.DataType); |
| 973 | bs = 0; |
| 974 | break; |
| 975 | } |
| 976 | } |
| 977 | } |
| 978 | if (bs == 0) { |
| 979 | // Could not detect object bitsize, assume 32 bits |
| 980 | bs = 32; |
| 981 | } |
| 982 | mtp_device->object_bitsize = bs; |
| 983 | |
| 984 | /* No Errors yet for this device */ |
| 985 | mtp_device->errorstack = NULL; |
| 986 | |
| 987 | /* Default Max Battery Level, we will adjust this if possible */ |
| 988 | mtp_device->maximum_battery_level = 100; |
| 989 | |
| 990 | /* Check if device supports reading maximum battery level */ |
| 991 | if(ptp_property_issupported( current_params, |
| 992 | PTP_DPC_BatteryLevel)) { |
| 993 | PTPDevicePropDesc dpd; |
| 994 | |
| 995 | /* Try to read maximum battery level */ |
| 996 | if(ptp_getdevicepropdesc(current_params, |
| 997 | PTP_DPC_BatteryLevel, |
| 998 | &dpd) != PTP_RC_OK) { |
| 999 | add_error_to_errorstack(mtp_device, |
| 1000 | LIBMTP_ERROR_CONNECTING, |
| 1001 | "Unable to read Maximum Battery Level for this " |
| 1002 | "device even though the device supposedly " |
| 1003 | "supports this functionality"); |
| 1004 | } |
| 1005 | |
| 1006 | /* TODO: is this appropriate? */ |
| 1007 | /* If max battery level is 0 then leave the default, otherwise assign */ |
| 1008 | if (dpd.FORM.Range.MaximumValue.u8 != 0) { |
| 1009 | mtp_device->maximum_battery_level = dpd.FORM.Range.MaximumValue.u8; |
| 1010 | } |
| 1011 | |
| 1012 | ptp_free_devicepropdesc(&dpd); |
| 1013 | } |
| 1014 | |
| 1015 | /* Set all default folders to 0 (root directory) */ |
| 1016 | mtp_device->default_music_folder = 0; |
| 1017 | mtp_device->default_playlist_folder = 0; |
| 1018 | mtp_device->default_picture_folder = 0; |
| 1019 | mtp_device->default_video_folder = 0; |
| 1020 | mtp_device->default_organizer_folder = 0; |
| 1021 | mtp_device->default_zencast_folder = 0; |
| 1022 | mtp_device->default_album_folder = 0; |
| 1023 | mtp_device->default_text_folder = 0; |
| 1024 | |
| 1025 | /* Set initial storage information */ |
| 1026 | mtp_device->storage = NULL; |
| 1027 | if (LIBMTP_Get_Storage(mtp_device, LIBMTP_STORAGE_SORTBY_NOTSORTED) == -1) { |
| 1028 | add_error_to_errorstack(mtp_device, |
| 1029 | LIBMTP_ERROR_GENERAL, |
| 1030 | "Get Storage information failed."); |
| 1031 | mtp_device->storage = NULL; |
| 1032 | } |
| 1033 | |
| 1034 | /* |
| 1035 | * Then get the handles and try to locate the default folders. |
| 1036 | * This has the desired side effect of caching all handles from |
| 1037 | * the device which speeds up later operations. |
| 1038 | */ |
| 1039 | flush_handles(mtp_device); |
| 1040 | |
| 1041 | return mtp_device; |
| 1042 | } |
| 1043 | |
| 1044 | /** |
Linus Walleij | 2d3f7b8 | 2007-02-14 09:24:20 +0000 | [diff] [blame] | 1045 | * Recursive function that adds MTP devices to a linked list |
Linus Walleij | a700d22 | 2008-05-28 23:06:14 +0000 | [diff] [blame] | 1046 | * @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] | 1047 | * @return a device pointer to a newly created mtpdevice (used in linked |
Linus Walleij | b0ab548 | 2007-08-29 21:08:54 +0000 | [diff] [blame] | 1048 | * list creation). |
Linus Walleij | 2d3f7b8 | 2007-02-14 09:24:20 +0000 | [diff] [blame] | 1049 | */ |
Linus Walleij | a700d22 | 2008-05-28 23:06:14 +0000 | [diff] [blame] | 1050 | 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] | 1051 | { |
Linus Walleij | a700d22 | 2008-05-28 23:06:14 +0000 | [diff] [blame] | 1052 | uint8_t i; |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 1053 | LIBMTP_mtpdevice_t *mtp_device_list = NULL; |
| 1054 | LIBMTP_mtpdevice_t *current_device = NULL; |
Linus Walleij | a700d22 | 2008-05-28 23:06:14 +0000 | [diff] [blame] | 1055 | |
| 1056 | for (i=0; i < numdevs; i++) { |
Linus Walleij | 2d3f7b8 | 2007-02-14 09:24:20 +0000 | [diff] [blame] | 1057 | LIBMTP_mtpdevice_t *mtp_device; |
Linus Walleij | bdb89bd | 2008-05-28 23:32:35 +0000 | [diff] [blame] | 1058 | mtp_device = LIBMTP_Open_Raw_Device(&devices[i]); |
Linus Walleij | a700d22 | 2008-05-28 23:06:14 +0000 | [diff] [blame] | 1059 | |
Linus Walleij | bdb89bd | 2008-05-28 23:32:35 +0000 | [diff] [blame] | 1060 | /* On error, try next device */ |
| 1061 | if (mtp_device == NULL) |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 1062 | continue; |
Linus Walleij | ddaba2f | 2007-10-02 21:20:30 +0000 | [diff] [blame] | 1063 | |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 1064 | /* Add the device to the list */ |
| 1065 | mtp_device->next = NULL; |
| 1066 | if (mtp_device_list == NULL) { |
| 1067 | mtp_device_list = current_device = mtp_device; |
| 1068 | } else { |
| 1069 | current_device->next = mtp_device; |
| 1070 | current_device = mtp_device; |
| 1071 | } |
Linus Walleij | 2d3f7b8 | 2007-02-14 09:24:20 +0000 | [diff] [blame] | 1072 | } |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 1073 | return mtp_device_list; |
Linus Walleij | 2d3f7b8 | 2007-02-14 09:24:20 +0000 | [diff] [blame] | 1074 | } |
| 1075 | |
| 1076 | /** |
tedbullock | 848009b | 2007-03-03 23:20:12 +0000 | [diff] [blame] | 1077 | * 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] | 1078 | * @param device_list Pointer to a linked list of devices |
| 1079 | * @return Number of devices in the device list device_list |
| 1080 | * @see LIBMTP_Get_Connected_Devices() |
tedbullock | 848009b | 2007-03-03 23:20:12 +0000 | [diff] [blame] | 1081 | */ |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 1082 | uint32_t LIBMTP_Number_Devices_In_List(LIBMTP_mtpdevice_t *device_list) |
tedbullock | 848009b | 2007-03-03 23:20:12 +0000 | [diff] [blame] | 1083 | { |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 1084 | uint32_t numdevices = 0; |
| 1085 | LIBMTP_mtpdevice_t *iter; |
| 1086 | for(iter = device_list; iter != NULL; iter = iter->next) |
| 1087 | numdevices++; |
| 1088 | |
| 1089 | return numdevices; |
tedbullock | 848009b | 2007-03-03 23:20:12 +0000 | [diff] [blame] | 1090 | } |
| 1091 | |
| 1092 | /** |
Linus Walleij | 2d3f7b8 | 2007-02-14 09:24:20 +0000 | [diff] [blame] | 1093 | * Get the first connected MTP device node in the linked list of devices. |
| 1094 | * Currently this only provides access to USB devices |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 1095 | * @param device_list A list of devices ready to be used by the caller. You |
| 1096 | * need to know how many there are. |
Linus Walleij | 2d3f7b8 | 2007-02-14 09:24:20 +0000 | [diff] [blame] | 1097 | * @return Any error information gathered from device connections |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 1098 | * @see LIBMTP_Number_Devices_In_List() |
Linus Walleij | 2d3f7b8 | 2007-02-14 09:24:20 +0000 | [diff] [blame] | 1099 | */ |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 1100 | 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] | 1101 | { |
Linus Walleij | a700d22 | 2008-05-28 23:06:14 +0000 | [diff] [blame] | 1102 | LIBMTP_raw_device_t *devices; |
| 1103 | int numdevs; |
Linus Walleij | 5fbb77d | 2007-03-07 08:40:36 +0000 | [diff] [blame] | 1104 | LIBMTP_error_number_t ret; |
Linus Walleij | a700d22 | 2008-05-28 23:06:14 +0000 | [diff] [blame] | 1105 | |
| 1106 | ret = LIBMTP_Detect_Raw_Devices(&devices, &numdevs); |
Linus Walleij | 5fbb77d | 2007-03-07 08:40:36 +0000 | [diff] [blame] | 1107 | if (ret != LIBMTP_ERROR_NONE) { |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 1108 | *device_list = NULL; |
Linus Walleij | 5fbb77d | 2007-03-07 08:40:36 +0000 | [diff] [blame] | 1109 | return ret; |
Linus Walleij | 2d3f7b8 | 2007-02-14 09:24:20 +0000 | [diff] [blame] | 1110 | } |
| 1111 | |
| 1112 | /* Assign linked list of devices */ |
Linus Walleij | a700d22 | 2008-05-28 23:06:14 +0000 | [diff] [blame] | 1113 | if (devices == NULL || numdevs == 0) { |
| 1114 | *device_list = NULL; |
| 1115 | return LIBMTP_ERROR_NO_DEVICE_ATTACHED; |
| 1116 | } |
| 1117 | |
| 1118 | *device_list = create_usb_mtp_devices(devices, numdevs); |
| 1119 | free(devices); |
Linus Walleij | 45a8637 | 2007-03-07 09:36:19 +0000 | [diff] [blame] | 1120 | |
Linus Walleij | 2d3f7b8 | 2007-02-14 09:24:20 +0000 | [diff] [blame] | 1121 | /* TODO: Add wifi device access here */ |
Richard Low | 99a93e8 | 2007-09-29 08:16:08 +0000 | [diff] [blame] | 1122 | |
| 1123 | /* We have found some devices but create failed */ |
| 1124 | if (*device_list == NULL) |
| 1125 | return LIBMTP_ERROR_CONNECTING; |
Linus Walleij | 2d3f7b8 | 2007-02-14 09:24:20 +0000 | [diff] [blame] | 1126 | |
| 1127 | return LIBMTP_ERROR_NONE; |
| 1128 | } |
| 1129 | |
| 1130 | /** |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 1131 | * This closes and releases an allocated MTP device. |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 1132 | * @param device a pointer to the MTP device to release. |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 1133 | */ |
tedbullock | 0f033cb | 2007-02-14 20:56:54 +0000 | [diff] [blame] | 1134 | void LIBMTP_Release_Device_List(LIBMTP_mtpdevice_t *device) |
| 1135 | { |
| 1136 | if(device != NULL) |
| 1137 | { |
| 1138 | if(device->next != NULL) |
| 1139 | { |
| 1140 | LIBMTP_Release_Device_List(device->next); |
| 1141 | } |
| 1142 | |
| 1143 | LIBMTP_Release_Device(device); |
| 1144 | } |
| 1145 | } |
| 1146 | |
| 1147 | /** |
| 1148 | * This closes and releases an allocated MTP device. |
| 1149 | * @param device a pointer to the MTP device to release. |
| 1150 | */ |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 1151 | void LIBMTP_Release_Device(LIBMTP_mtpdevice_t *device) |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 1152 | { |
Linus Walleij | 9b28da3 | 2006-03-16 13:47:58 +0000 | [diff] [blame] | 1153 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | 2d411db | 2006-03-22 12:13:09 +0000 | [diff] [blame] | 1154 | PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo; |
Linus Walleij | 9b28da3 | 2006-03-16 13:47:58 +0000 | [diff] [blame] | 1155 | |
Linus Walleij | b0ab548 | 2007-08-29 21:08:54 +0000 | [diff] [blame] | 1156 | close_device(ptp_usb, params); |
Linus Walleij | 2715c44 | 2007-01-20 22:35:29 +0000 | [diff] [blame] | 1157 | // Clear error stack |
| 1158 | LIBMTP_Clear_Errorstack(device); |
Linus Walleij | 3ec8631 | 2006-08-21 13:25:24 +0000 | [diff] [blame] | 1159 | // Free iconv() converters... |
Linus Walleij | a823a70 | 2006-08-27 21:27:46 +0000 | [diff] [blame] | 1160 | iconv_close(params->cd_locale_to_ucs2); |
| 1161 | iconv_close(params->cd_ucs2_to_locale); |
tedbullock | 69a445b | 2007-02-15 07:41:04 +0000 | [diff] [blame] | 1162 | free(ptp_usb); |
Linus Walleij | 073c417 | 2007-02-02 22:26:33 +0000 | [diff] [blame] | 1163 | ptp_free_params(params); |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 1164 | free_storage_list(device); |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 1165 | free(device); |
| 1166 | } |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 1167 | |
| 1168 | /** |
Linus Walleij | 2715c44 | 2007-01-20 22:35:29 +0000 | [diff] [blame] | 1169 | * This can be used by any libmtp-intrinsic code that |
Linus Walleij | 68b19c0 | 2007-02-15 11:50:37 +0000 | [diff] [blame] | 1170 | * need to stack up an error on the stack. You are only |
| 1171 | * supposed to add errors to the error stack using this |
| 1172 | * function, do not create and reference error entries |
| 1173 | * directly. |
Linus Walleij | 2715c44 | 2007-01-20 22:35:29 +0000 | [diff] [blame] | 1174 | */ |
| 1175 | static void add_error_to_errorstack(LIBMTP_mtpdevice_t *device, |
| 1176 | LIBMTP_error_number_t errornumber, |
| 1177 | char const * const error_text) |
| 1178 | { |
| 1179 | LIBMTP_error_t *newerror; |
| 1180 | |
Linus Walleij | 68b19c0 | 2007-02-15 11:50:37 +0000 | [diff] [blame] | 1181 | if (device == NULL) { |
| 1182 | fprintf(stderr, "LIBMTP PANIC: Trying to add error to a NULL device!\n"); |
| 1183 | return; |
| 1184 | } |
Linus Walleij | 2715c44 | 2007-01-20 22:35:29 +0000 | [diff] [blame] | 1185 | newerror = (LIBMTP_error_t *) malloc(sizeof(LIBMTP_error_t)); |
| 1186 | newerror->errornumber = errornumber; |
| 1187 | newerror->error_text = strdup(error_text); |
rreardon | 774503c | 2007-02-15 15:52:49 +0000 | [diff] [blame] | 1188 | newerror->next = NULL; |
Linus Walleij | 2715c44 | 2007-01-20 22:35:29 +0000 | [diff] [blame] | 1189 | if (device->errorstack == NULL) { |
| 1190 | device->errorstack = newerror; |
| 1191 | } else { |
| 1192 | LIBMTP_error_t *tmp = device->errorstack; |
| 1193 | |
| 1194 | while (tmp->next != NULL) { |
| 1195 | tmp = tmp->next; |
| 1196 | } |
| 1197 | tmp->next = newerror; |
| 1198 | } |
| 1199 | } |
| 1200 | |
| 1201 | /** |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 1202 | * Adds an error from the PTP layer to the error stack. |
| 1203 | */ |
| 1204 | static void add_ptp_error_to_errorstack(LIBMTP_mtpdevice_t *device, |
| 1205 | uint16_t ptp_error, |
| 1206 | char const * const error_text) |
| 1207 | { |
Linus Walleij | 68b19c0 | 2007-02-15 11:50:37 +0000 | [diff] [blame] | 1208 | if (device == NULL) { |
| 1209 | fprintf(stderr, "LIBMTP PANIC: Trying to add PTP error to a NULL device!\n"); |
| 1210 | return; |
| 1211 | } else { |
| 1212 | char outstr[256]; |
| 1213 | snprintf(outstr, sizeof(outstr), "PTP Layer error %04x: %s", ptp_error, error_text); |
| 1214 | outstr[sizeof(outstr)-1] = '\0'; |
| 1215 | add_error_to_errorstack(device, LIBMTP_ERROR_PTP_LAYER, outstr); |
| 1216 | add_error_to_errorstack(device, LIBMTP_ERROR_PTP_LAYER, "(Look this up in ptp.h for an explanation.)"); |
| 1217 | } |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 1218 | } |
| 1219 | |
| 1220 | /** |
Linus Walleij | 2715c44 | 2007-01-20 22:35:29 +0000 | [diff] [blame] | 1221 | * This returns the error stack for a device in case you |
| 1222 | * need to either reference the error numbers (e.g. when |
| 1223 | * creating multilingual apps with multiple-language text |
| 1224 | * representations for each error number) or when you need |
| 1225 | * to build a multi-line error text widget or something like |
| 1226 | * that. You need to call the <code>LIBMTP_Clear_Errorstack</code> |
| 1227 | * to clear it when you're finished with it. |
| 1228 | * @param device a pointer to the MTP device to get the error |
| 1229 | * stack for. |
| 1230 | * @return the error stack or NULL if there are no errors |
| 1231 | * on the stack. |
| 1232 | * @see LIBMTP_Clear_Errorstack() |
| 1233 | * @see LIBMTP_Dump_Errorstack() |
| 1234 | */ |
| 1235 | LIBMTP_error_t *LIBMTP_Get_Errorstack(LIBMTP_mtpdevice_t *device) |
| 1236 | { |
Linus Walleij | 68b19c0 | 2007-02-15 11:50:37 +0000 | [diff] [blame] | 1237 | if (device == NULL) { |
| 1238 | fprintf(stderr, "LIBMTP PANIC: Trying to get the error stack of a NULL device!\n"); |
| 1239 | } |
Linus Walleij | 2715c44 | 2007-01-20 22:35:29 +0000 | [diff] [blame] | 1240 | return device->errorstack; |
| 1241 | } |
| 1242 | |
| 1243 | /** |
| 1244 | * This function clears the error stack of a device and frees |
| 1245 | * any memory used by it. Call this when you're finished with |
| 1246 | * using the errors. |
| 1247 | * @param device a pointer to the MTP device to clear the error |
| 1248 | * stack for. |
| 1249 | */ |
| 1250 | void LIBMTP_Clear_Errorstack(LIBMTP_mtpdevice_t *device) |
| 1251 | { |
Linus Walleij | 68b19c0 | 2007-02-15 11:50:37 +0000 | [diff] [blame] | 1252 | if (device == NULL) { |
| 1253 | fprintf(stderr, "LIBMTP PANIC: Trying to clear the error stack of a NULL device!\n"); |
| 1254 | } else { |
| 1255 | LIBMTP_error_t *tmp = device->errorstack; |
Linus Walleij | 2715c44 | 2007-01-20 22:35:29 +0000 | [diff] [blame] | 1256 | |
Linus Walleij | 68b19c0 | 2007-02-15 11:50:37 +0000 | [diff] [blame] | 1257 | while (tmp != NULL) { |
| 1258 | LIBMTP_error_t *tmp2; |
| 1259 | |
| 1260 | if (tmp->error_text != NULL) { |
| 1261 | free(tmp->error_text); |
| 1262 | } |
| 1263 | tmp2 = tmp; |
| 1264 | tmp = tmp->next; |
| 1265 | free(tmp2); |
Linus Walleij | 2715c44 | 2007-01-20 22:35:29 +0000 | [diff] [blame] | 1266 | } |
Linus Walleij | 68b19c0 | 2007-02-15 11:50:37 +0000 | [diff] [blame] | 1267 | device->errorstack = NULL; |
Linus Walleij | 2715c44 | 2007-01-20 22:35:29 +0000 | [diff] [blame] | 1268 | } |
Linus Walleij | 2715c44 | 2007-01-20 22:35:29 +0000 | [diff] [blame] | 1269 | } |
| 1270 | |
| 1271 | /** |
| 1272 | * This function dumps the error stack to <code>stderr</code>. |
| 1273 | * (You still have to clear the stack though.) |
| 1274 | * @param device a pointer to the MTP device to dump the error |
| 1275 | * stack for. |
| 1276 | */ |
| 1277 | void LIBMTP_Dump_Errorstack(LIBMTP_mtpdevice_t *device) |
| 1278 | { |
Linus Walleij | 68b19c0 | 2007-02-15 11:50:37 +0000 | [diff] [blame] | 1279 | if (device == NULL) { |
| 1280 | fprintf(stderr, "LIBMTP PANIC: Trying to dump the error stack of a NULL device!\n"); |
| 1281 | } else { |
| 1282 | LIBMTP_error_t *tmp = device->errorstack; |
Linus Walleij | 2715c44 | 2007-01-20 22:35:29 +0000 | [diff] [blame] | 1283 | |
Linus Walleij | 68b19c0 | 2007-02-15 11:50:37 +0000 | [diff] [blame] | 1284 | while (tmp != NULL) { |
| 1285 | if (tmp->error_text != NULL) { |
| 1286 | fprintf(stderr, "Error %d: %s\n", tmp->errornumber, tmp->error_text); |
| 1287 | } else { |
| 1288 | fprintf(stderr, "Error %d: (unknown)\n", tmp->errornumber); |
| 1289 | } |
| 1290 | tmp = tmp->next; |
Linus Walleij | 2715c44 | 2007-01-20 22:35:29 +0000 | [diff] [blame] | 1291 | } |
Linus Walleij | 2715c44 | 2007-01-20 22:35:29 +0000 | [diff] [blame] | 1292 | } |
| 1293 | } |
| 1294 | |
| 1295 | /** |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 1296 | * This command gets all handles and stuff by FAST directory retrieveal |
| 1297 | * which is available by getting all metadata for object |
| 1298 | * <code>0xffffffff</code> which simply means "all metadata for all objects". |
| 1299 | * This works on the vast majority of MTP devices (there ARE exceptions!) |
| 1300 | * and is quite quick. Check the error stack to see if there were |
| 1301 | * problems getting the metadata. |
Linus Walleij | 8533bf7 | 2007-08-28 10:03:59 +0000 | [diff] [blame] | 1302 | * @return 0 if all was OK, -1 on failure. |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 1303 | */ |
Linus Walleij | 6bf68b4 | 2007-09-03 22:50:02 +0000 | [diff] [blame] | 1304 | static int get_all_metadata_fast(LIBMTP_mtpdevice_t *device, |
| 1305 | uint32_t storage) |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 1306 | { |
| 1307 | PTPParams *params = (PTPParams *) device->params; |
| 1308 | int cnt = 0; |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 1309 | int i, j, nrofprops; |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 1310 | uint32_t lasthandle = 0xffffffff; |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 1311 | MTPProperties *props = NULL; |
| 1312 | MTPProperties *prop; |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 1313 | uint16_t ret; |
Linus Walleij | 2f62281 | 2008-08-30 22:06:58 +0000 | [diff] [blame] | 1314 | int oldtimeout; |
| 1315 | PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo; |
| 1316 | |
| 1317 | /* The follow request causes the device to generate |
| 1318 | * a list of very file on the device and return it |
| 1319 | * in a single response. |
| 1320 | * |
| 1321 | * Some slow devices as well as devices with very |
| 1322 | * large file systems can easily take longer then |
| 1323 | * the standard timeout value before it is able |
| 1324 | * to return a response. |
| 1325 | * |
| 1326 | * Temporarly set timeout to allow working with |
| 1327 | * widest range of devices. |
| 1328 | */ |
| 1329 | get_usb_device_timeout(ptp_usb, &oldtimeout); |
| 1330 | set_usb_device_timeout(ptp_usb, 60000); |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 1331 | |
Linus Walleij | fcb4342 | 2008-05-23 21:53:55 +0000 | [diff] [blame] | 1332 | ret = ptp_mtp_getobjectproplist(params, 0xffffffff, &props, &nrofprops); |
Linus Walleij | 2f62281 | 2008-08-30 22:06:58 +0000 | [diff] [blame] | 1333 | set_usb_device_timeout(ptp_usb, oldtimeout); |
Linus Walleij | 8533bf7 | 2007-08-28 10:03:59 +0000 | [diff] [blame] | 1334 | |
| 1335 | if (ret == PTP_RC_MTP_Specification_By_Group_Unsupported) { |
| 1336 | // What's the point in the device implementing this command if |
| 1337 | // you cannot use it to get all props for AT LEAST one object? |
| 1338 | // Well, whatever... |
| 1339 | add_ptp_error_to_errorstack(device, ret, "get_all_metadata_fast(): " |
| 1340 | "cannot retrieve all metadata for an object on this device."); |
Linus Walleij | 91e9813 | 2007-08-28 10:05:11 +0000 | [diff] [blame] | 1341 | return -1; |
Linus Walleij | 8533bf7 | 2007-08-28 10:03:59 +0000 | [diff] [blame] | 1342 | } |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 1343 | if (ret != PTP_RC_OK) { |
Linus Walleij | 8533bf7 | 2007-08-28 10:03:59 +0000 | [diff] [blame] | 1344 | add_ptp_error_to_errorstack(device, ret, "get_all_metadata_fast(): " |
| 1345 | "could not get proplist of all objects."); |
| 1346 | return -1; |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 1347 | } |
Linus Walleij | fcb4342 | 2008-05-23 21:53:55 +0000 | [diff] [blame] | 1348 | if (props == NULL && nrofprops != 0) { |
| 1349 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, |
| 1350 | "get_all_metadata_fast(): " |
| 1351 | "call to ptp_mtp_getobjectproplist() returned " |
| 1352 | "inconsistent results."); |
| 1353 | return -1; |
| 1354 | } |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 1355 | params->props = props; /* cache it */ |
| 1356 | params->nrofprops = nrofprops; /* cache it */ |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 1357 | |
| 1358 | /* |
| 1359 | * We count the number of objects by counting the ObjectHandle |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 1360 | * references, whenever it changes we get a new object, when it's |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 1361 | * the same, it is just different properties of the same object. |
| 1362 | */ |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 1363 | prop = props; |
| 1364 | for (i=0;i<nrofprops;i++) { |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 1365 | if (lasthandle != prop->ObjectHandle) { |
| 1366 | cnt++; |
| 1367 | lasthandle = prop->ObjectHandle; |
| 1368 | } |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 1369 | prop++; |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 1370 | } |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 1371 | lasthandle = 0xffffffff; |
| 1372 | params->objectinfo = malloc (sizeof (PTPObjectInfo) * cnt); |
| 1373 | memset (params->objectinfo, 0, sizeof(PTPObjectInfo) * cnt); |
| 1374 | params->handles.Handler = malloc (sizeof (uint32_t) * cnt); |
| 1375 | params->handles.n = cnt; |
| 1376 | |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 1377 | prop = props; |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 1378 | i = -1; |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 1379 | for (j=0;j<nrofprops;j++) { |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 1380 | if (lasthandle != prop->ObjectHandle) { |
| 1381 | if (i >= 0) { |
| 1382 | if (!params->objectinfo[i].Filename) { |
Linus Walleij | 6bf68b4 | 2007-09-03 22:50:02 +0000 | [diff] [blame] | 1383 | /* I have one such file on my Creative (Marcus) */ |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 1384 | params->objectinfo[i].Filename = strdup("<null>"); |
| 1385 | } |
| 1386 | } |
| 1387 | i++; |
| 1388 | lasthandle = prop->ObjectHandle; |
| 1389 | params->handles.Handler[i] = prop->ObjectHandle; |
| 1390 | } |
| 1391 | switch (prop->property) { |
| 1392 | case PTP_OPC_ParentObject: |
| 1393 | params->objectinfo[i].ParentObject = prop->propval.u32; |
| 1394 | break; |
| 1395 | case PTP_OPC_ObjectFormat: |
| 1396 | params->objectinfo[i].ObjectFormat = prop->propval.u16; |
| 1397 | break; |
| 1398 | case PTP_OPC_ObjectSize: |
Linus Walleij | d9d28d5 | 2007-08-04 19:01:18 +0000 | [diff] [blame] | 1399 | // We loose precision here, up to 32 bits! However the commands that |
| 1400 | // retrieve metadata for files and tracks will make sure that the |
| 1401 | // PTP_OPC_ObjectSize is read in and duplicated again. |
Linus Walleij | ddaba2f | 2007-10-02 21:20:30 +0000 | [diff] [blame] | 1402 | if (device->object_bitsize == 64) { |
| 1403 | params->objectinfo[i].ObjectCompressedSize = (uint32_t) prop->propval.u64; |
| 1404 | } else { |
| 1405 | params->objectinfo[i].ObjectCompressedSize = prop->propval.u32; |
| 1406 | } |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 1407 | break; |
| 1408 | case PTP_OPC_StorageID: |
| 1409 | params->objectinfo[i].StorageID = prop->propval.u32; |
| 1410 | break; |
| 1411 | case PTP_OPC_ObjectFileName: |
Richard Low | 8e8d9d4 | 2007-07-14 10:36:50 +0000 | [diff] [blame] | 1412 | if (prop->propval.str != NULL) |
| 1413 | params->objectinfo[i].Filename = strdup(prop->propval.str); |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 1414 | break; |
| 1415 | default: |
| 1416 | /* |
| 1417 | * This was in libgphoto2 no idea what it tests for... |
| 1418 | * if ((prop->property & 0xfff0) == 0xdc00) |
| 1419 | * gp_log (GP_LOG_DEBUG, "ptp2/mtpfast", "case %x type %x unhandled.\n", prop->property, prop->datatype); |
| 1420 | */ |
| 1421 | break; |
| 1422 | // FIXME: the rest of the metadata is readily available right here! |
| 1423 | } |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 1424 | prop++; |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 1425 | } |
Linus Walleij | 8533bf7 | 2007-08-28 10:03:59 +0000 | [diff] [blame] | 1426 | return 0; |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 1427 | } |
| 1428 | |
| 1429 | /** |
| 1430 | * This function will recurse through all the directories on the device, |
| 1431 | * starting at the root directory, gathering metadata as it moves along. |
| 1432 | * It works better on some devices that will only return data for a |
| 1433 | * certain directory and does not respect the option to get all metadata |
| 1434 | * for all objects. |
| 1435 | */ |
Linus Walleij | 6bf68b4 | 2007-09-03 22:50:02 +0000 | [diff] [blame] | 1436 | static void get_handles_recursively(LIBMTP_mtpdevice_t *device, |
| 1437 | PTPParams *params, |
| 1438 | PTPObjectHandles *handles, |
| 1439 | uint32_t storageid, |
| 1440 | uint32_t parent) |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 1441 | { |
| 1442 | PTPObjectHandles currentHandles; |
| 1443 | int i = 0; |
| 1444 | uint32_t old_handles; |
| 1445 | |
| 1446 | uint16_t ret = ptp_getobjecthandles(params, |
Linus Walleij | 6bf68b4 | 2007-09-03 22:50:02 +0000 | [diff] [blame] | 1447 | storageid, |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 1448 | PTP_GOH_ALL_FORMATS, |
| 1449 | parent, |
| 1450 | ¤tHandles); |
| 1451 | |
| 1452 | if (ret != PTP_RC_OK) { |
| 1453 | add_ptp_error_to_errorstack(device, ret, "get_handles_recursively(): could not get object handles."); |
| 1454 | return; |
| 1455 | } |
| 1456 | |
| 1457 | if (currentHandles.Handler == NULL || currentHandles.n == 0) |
| 1458 | return; |
| 1459 | |
| 1460 | old_handles = handles->n; |
| 1461 | |
| 1462 | // Realloc main space |
| 1463 | handles->Handler = (uint32_t *) realloc(handles->Handler, |
| 1464 | (old_handles + currentHandles.n) * sizeof(uint32_t)); |
| 1465 | // Realloc object info cache |
| 1466 | params->objectinfo = (PTPObjectInfo*) realloc(params->objectinfo, |
| 1467 | (old_handles + currentHandles.n) * sizeof(PTPObjectInfo)); |
| 1468 | memset(¶ms->objectinfo[old_handles], 0, currentHandles.n * sizeof(PTPObjectInfo)); |
| 1469 | |
| 1470 | // Copy new handles |
Linus Walleij | 4dbfa76 | 2007-10-18 18:34:16 +0000 | [diff] [blame] | 1471 | memmove(&(handles->Handler[old_handles]), currentHandles.Handler, currentHandles.n * sizeof(uint32_t)); |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 1472 | handles->n = old_handles + currentHandles.n; |
| 1473 | |
| 1474 | // Now descend into any subdirectories found |
| 1475 | for (i = 0; i < currentHandles.n; i++) { |
| 1476 | ret = ptp_getobjectinfo(params, |
| 1477 | currentHandles.Handler[i], |
| 1478 | ¶ms->objectinfo[old_handles + i]); |
| 1479 | |
| 1480 | if (ret == PTP_RC_OK) { |
| 1481 | PTPObjectInfo *oi; |
| 1482 | |
| 1483 | oi = ¶ms->objectinfo[old_handles + i]; |
| 1484 | if (oi->ObjectFormat == PTP_OFC_Association) { |
Linus Walleij | 6bf68b4 | 2007-09-03 22:50:02 +0000 | [diff] [blame] | 1485 | get_handles_recursively(device, params, handles, storageid, currentHandles.Handler[i]); |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 1486 | } |
| 1487 | } else { |
| 1488 | add_error_to_errorstack(device, |
| 1489 | LIBMTP_ERROR_CONNECTING, |
| 1490 | "Found a bad handle, trying to ignore it."); |
| 1491 | } |
| 1492 | } |
| 1493 | |
| 1494 | free(currentHandles.Handler); |
| 1495 | } |
| 1496 | |
| 1497 | /** |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 1498 | * This function refresh the internal handle list whenever |
| 1499 | * the items stored inside the device is altered. On operations |
| 1500 | * that do not add or remove objects, this is typically not |
| 1501 | * called. |
| 1502 | * @param device a pointer to the MTP device to flush handles for. |
| 1503 | */ |
| 1504 | static void flush_handles(LIBMTP_mtpdevice_t *device) |
| 1505 | { |
| 1506 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 1507 | PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo; |
Linus Walleij | 8533bf7 | 2007-08-28 10:03:59 +0000 | [diff] [blame] | 1508 | int ret; |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 1509 | uint32_t i; |
| 1510 | |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 1511 | if (params->handles.Handler != NULL) { |
| 1512 | free(params->handles.Handler); |
| 1513 | } |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 1514 | if (params->objectinfo != NULL) { |
| 1515 | for (i=0;i<params->handles.n;i++) |
| 1516 | ptp_free_objectinfo (¶ms->objectinfo[i]); |
| 1517 | free(params->objectinfo); |
| 1518 | } |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 1519 | if (params->props != NULL) { |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 1520 | ptp_destroy_object_prop_list(params->props, params->nrofprops); |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 1521 | } |
Richard Low | 6711f44 | 2007-05-05 19:00:59 +0000 | [diff] [blame] | 1522 | |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 1523 | params->handles.n = 0; |
Richard Low | 6711f44 | 2007-05-05 19:00:59 +0000 | [diff] [blame] | 1524 | params->handles.Handler = NULL; |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 1525 | params->objectinfo = NULL; |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 1526 | params->props = NULL; |
| 1527 | params->nrofprops = 0; |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 1528 | |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 1529 | if (ptp_operation_issupported(params,PTP_OC_MTP_GetObjPropList) |
Linus Walleij | fec4d56 | 2008-06-01 22:30:36 +0000 | [diff] [blame] | 1530 | && !FLAG_BROKEN_MTPGETOBJPROPLIST(ptp_usb) |
| 1531 | && !FLAG_BROKEN_MTPGETOBJPROPLIST_ALL(ptp_usb)) { |
Linus Walleij | 139455e | 2007-08-28 10:25:06 +0000 | [diff] [blame] | 1532 | // Use the fast method. Ignore return value for now. |
Linus Walleij | 6bf68b4 | 2007-09-03 22:50:02 +0000 | [diff] [blame] | 1533 | ret = get_all_metadata_fast(device, PTP_GOH_ALL_STORAGE); |
Linus Walleij | 8533bf7 | 2007-08-28 10:03:59 +0000 | [diff] [blame] | 1534 | } |
Linus Walleij | 139455e | 2007-08-28 10:25:06 +0000 | [diff] [blame] | 1535 | // If the previous failed or returned no objects, use classic |
| 1536 | // methods instead. |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 1537 | if (params->props == NULL) { |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 1538 | // Get all the handles using just standard commands. |
Linus Walleij | 6bf68b4 | 2007-09-03 22:50:02 +0000 | [diff] [blame] | 1539 | if (device->storage == NULL) { |
| 1540 | get_handles_recursively(device, params, |
| 1541 | ¶ms->handles, |
| 1542 | PTP_GOH_ALL_STORAGE, |
| 1543 | PTP_GOH_ROOT_PARENT); |
| 1544 | } else { |
| 1545 | // Get handles for each storage in turn. |
| 1546 | LIBMTP_devicestorage_t *storage = device->storage; |
| 1547 | while(storage != NULL) { |
| 1548 | get_handles_recursively(device, params, |
| 1549 | ¶ms->handles, |
| 1550 | storage->id, |
| 1551 | PTP_GOH_ROOT_PARENT); |
| 1552 | storage = storage->next; |
| 1553 | } |
| 1554 | } |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 1555 | } |
Linus Walleij | 8f7f1aa | 2008-06-15 19:00:23 +0000 | [diff] [blame] | 1556 | |
| 1557 | /* |
| 1558 | * Loop over the handles, fix up any NULL filenames or |
| 1559 | * keywords, then attempt to locate some default folders |
| 1560 | * in the root directory of the primary storage. |
| 1561 | */ |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 1562 | for(i = 0; i < params->handles.n; i++) { |
| 1563 | PTPObjectInfo *oi; |
| 1564 | |
| 1565 | oi = ¶ms->objectinfo[i]; |
| 1566 | if (oi->Filename == NULL) { |
| 1567 | oi->Filename = strdup("<null>"); |
| 1568 | } |
| 1569 | if (oi->Keywords == NULL) { |
| 1570 | oi->Keywords = strdup("<null>"); |
| 1571 | } |
| 1572 | |
| 1573 | /* Ignore handles that point to non-folders */ |
| 1574 | if(oi->ObjectFormat != PTP_OFC_Association) |
| 1575 | continue; |
Linus Walleij | 8f7f1aa | 2008-06-15 19:00:23 +0000 | [diff] [blame] | 1576 | /* Only look in the root folder */ |
| 1577 | if (oi->ParentObject != 0x00000000U) |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 1578 | continue; |
Linus Walleij | c40c9bf | 2008-06-13 23:24:17 +0000 | [diff] [blame] | 1579 | /* Only look in the primary storage */ |
| 1580 | if (device->storage != NULL && oi->StorageID != device->storage->id) |
| 1581 | continue; |
Linus Walleij | 8f7f1aa | 2008-06-15 19:00:23 +0000 | [diff] [blame] | 1582 | |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 1583 | |
| 1584 | /* Is this the Music Folder */ |
| 1585 | if (!strcasecmp(oi->Filename, "My Music") || |
| 1586 | !strcasecmp(oi->Filename, "Music")) { |
| 1587 | device->default_music_folder = |
| 1588 | params->handles.Handler[i]; |
| 1589 | } |
| 1590 | else if (!strcasecmp(oi->Filename, "My Playlists") || |
| 1591 | !strcasecmp(oi->Filename, "Playlists")) { |
| 1592 | device->default_playlist_folder = |
| 1593 | params->handles.Handler[i]; |
| 1594 | } |
| 1595 | else if (!strcasecmp(oi->Filename, "My Pictures") || |
| 1596 | !strcasecmp(oi->Filename, "Pictures")) { |
| 1597 | device->default_picture_folder = |
| 1598 | params->handles.Handler[i]; |
| 1599 | } |
| 1600 | else if (!strcasecmp(oi->Filename, "My Video") || |
| 1601 | !strcasecmp(oi->Filename, "Video")) { |
| 1602 | device->default_video_folder = |
| 1603 | params->handles.Handler[i]; |
| 1604 | } |
| 1605 | else if (!strcasecmp(oi->Filename, "My Organizer")) { |
| 1606 | device->default_organizer_folder = |
| 1607 | params->handles.Handler[i]; |
| 1608 | } |
Linus Walleij | 2350b71 | 2008-01-14 22:54:37 +0000 | [diff] [blame] | 1609 | else if (!strcasecmp(oi->Filename, "ZENcast") || |
| 1610 | !strcasecmp(oi->Filename, "Datacasts")) { |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 1611 | device->default_zencast_folder = |
| 1612 | params->handles.Handler[i]; |
| 1613 | } |
| 1614 | else if (!strcasecmp(oi->Filename, "My Albums") || |
| 1615 | !strcasecmp(oi->Filename, "Albums")) { |
| 1616 | device->default_album_folder = |
| 1617 | params->handles.Handler[i]; |
| 1618 | } |
Linus Walleij | 2350b71 | 2008-01-14 22:54:37 +0000 | [diff] [blame] | 1619 | else if (!strcasecmp(oi->Filename, "Text") || |
| 1620 | !strcasecmp(oi->Filename, "Texts")) { |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 1621 | device->default_text_folder = |
| 1622 | params->handles.Handler[i]; |
| 1623 | } |
| 1624 | } |
Richard Low | 6711f44 | 2007-05-05 19:00:59 +0000 | [diff] [blame] | 1625 | } |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 1626 | |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 1627 | /** |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 1628 | * This function traverses a devices storage list freeing up the |
| 1629 | * strings and the structs. |
| 1630 | * @param device a pointer to the MTP device to free the storage |
| 1631 | * list for. |
| 1632 | */ |
| 1633 | static void free_storage_list(LIBMTP_mtpdevice_t *device) |
| 1634 | { |
Linus Walleij | e1ac07e | 2006-12-14 19:38:59 +0000 | [diff] [blame] | 1635 | LIBMTP_devicestorage_t *storage; |
| 1636 | LIBMTP_devicestorage_t *tmp; |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 1637 | |
Linus Walleij | e1ac07e | 2006-12-14 19:38:59 +0000 | [diff] [blame] | 1638 | storage = device->storage; |
| 1639 | while(storage != NULL) { |
| 1640 | if (storage->StorageDescription != NULL) { |
| 1641 | free(storage->StorageDescription); |
| 1642 | } |
| 1643 | if (storage->VolumeIdentifier != NULL) { |
| 1644 | free(storage->VolumeIdentifier); |
| 1645 | } |
| 1646 | tmp = storage; |
| 1647 | storage = storage->next; |
| 1648 | free(tmp); |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 1649 | } |
| 1650 | device->storage = NULL; |
| 1651 | |
| 1652 | return; |
| 1653 | } |
| 1654 | |
| 1655 | /** |
| 1656 | * This function traverses a devices storage list freeing up the |
| 1657 | * strings and the structs. |
| 1658 | * @param device a pointer to the MTP device to free the storage |
| 1659 | * list for. |
| 1660 | */ |
| 1661 | static int sort_storage_by(LIBMTP_mtpdevice_t *device,int const sortby) |
| 1662 | { |
| 1663 | LIBMTP_devicestorage_t *oldhead, *ptr1, *ptr2, *newlist; |
| 1664 | |
| 1665 | if (device->storage == NULL) |
| 1666 | return -1; |
| 1667 | if (sortby == LIBMTP_STORAGE_SORTBY_NOTSORTED) |
| 1668 | return 0; |
| 1669 | |
| 1670 | oldhead = ptr1 = ptr2 = device->storage; |
| 1671 | |
| 1672 | newlist = NULL; |
| 1673 | |
| 1674 | while(oldhead != NULL) { |
| 1675 | ptr1 = ptr2 = oldhead; |
| 1676 | while(ptr1 != NULL) { |
| 1677 | |
| 1678 | if (sortby == LIBMTP_STORAGE_SORTBY_FREESPACE && ptr1->FreeSpaceInBytes > ptr2->FreeSpaceInBytes) |
| 1679 | ptr2 = ptr1; |
| 1680 | if (sortby == LIBMTP_STORAGE_SORTBY_MAXSPACE && ptr1->FreeSpaceInBytes > ptr2->FreeSpaceInBytes) |
| 1681 | ptr2 = ptr1; |
| 1682 | |
| 1683 | ptr1 = ptr1->next; |
| 1684 | } |
| 1685 | |
| 1686 | // Make our previous entries next point to our next |
| 1687 | if(ptr2->prev != NULL) { |
| 1688 | ptr1 = ptr2->prev; |
| 1689 | ptr1->next = ptr2->next; |
| 1690 | } else { |
| 1691 | oldhead = ptr2->next; |
| 1692 | if(oldhead != NULL) |
| 1693 | oldhead->prev = NULL; |
| 1694 | } |
| 1695 | |
| 1696 | // Make our next entries previous point to our previous |
| 1697 | ptr1 = ptr2->next; |
| 1698 | if(ptr1 != NULL) { |
| 1699 | ptr1->prev = ptr2->prev; |
| 1700 | } else { |
| 1701 | ptr1 = ptr2->prev; |
| 1702 | if(ptr1 != NULL) |
| 1703 | ptr1->next = NULL; |
| 1704 | } |
| 1705 | |
| 1706 | if(newlist == NULL) { |
| 1707 | newlist = ptr2; |
| 1708 | newlist->prev = NULL; |
| 1709 | } else { |
| 1710 | ptr2->prev = newlist; |
| 1711 | newlist->next = ptr2; |
| 1712 | newlist = newlist->next; |
| 1713 | } |
| 1714 | } |
| 1715 | |
| 1716 | newlist->next = NULL; |
| 1717 | while(newlist->prev != NULL) |
| 1718 | newlist = newlist->prev; |
| 1719 | |
| 1720 | device->storage = newlist; |
| 1721 | |
| 1722 | return 0; |
| 1723 | } |
| 1724 | |
| 1725 | /** |
| 1726 | * This function grabs the first storageid from the device storage |
| 1727 | * list. |
| 1728 | * @param device a pointer to the MTP device to free the storage |
| 1729 | * list for. |
| 1730 | */ |
| 1731 | static uint32_t get_first_storageid(LIBMTP_mtpdevice_t *device) |
| 1732 | { |
| 1733 | LIBMTP_devicestorage_t *storage = device->storage; |
Linus Walleij | fb28b63 | 2007-10-23 21:56:18 +0000 | [diff] [blame] | 1734 | uint32_t store = 0x00000000; //Should this be 0xffffffffu instead? |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 1735 | |
| 1736 | if(storage != NULL) |
| 1737 | store = storage->id; |
| 1738 | |
| 1739 | return store; |
| 1740 | } |
| 1741 | |
| 1742 | /** |
Linus Walleij | 6bf68b4 | 2007-09-03 22:50:02 +0000 | [diff] [blame] | 1743 | * This function grabs the freespace from a certain storage in |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 1744 | * device storage list. |
| 1745 | * @param device a pointer to the MTP device to free the storage |
| 1746 | * list for. |
Linus Walleij | 6bf68b4 | 2007-09-03 22:50:02 +0000 | [diff] [blame] | 1747 | * @param storageid the storage ID for the storage to flush and |
| 1748 | * get free space for. |
| 1749 | * @param freespace the free space on this storage will be returned |
| 1750 | * in this variable. |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 1751 | */ |
Linus Walleij | 6bf68b4 | 2007-09-03 22:50:02 +0000 | [diff] [blame] | 1752 | static int get_storage_freespace(LIBMTP_mtpdevice_t *device, |
| 1753 | LIBMTP_devicestorage_t *storage, |
| 1754 | uint64_t *freespace) |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 1755 | { |
Linus Walleij | e1ac07e | 2006-12-14 19:38:59 +0000 | [diff] [blame] | 1756 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 1757 | |
Linus Walleij | e1ac07e | 2006-12-14 19:38:59 +0000 | [diff] [blame] | 1758 | // Always query the device about this, since some models explicitly |
Linus Walleij | 6bf68b4 | 2007-09-03 22:50:02 +0000 | [diff] [blame] | 1759 | // needs that. We flush all data on queries storage here. |
Linus Walleij | e1ac07e | 2006-12-14 19:38:59 +0000 | [diff] [blame] | 1760 | if (ptp_operation_issupported(params,PTP_OC_GetStorageInfo)) { |
| 1761 | PTPStorageInfo storageInfo; |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 1762 | uint16_t ret; |
Linus Walleij | e1ac07e | 2006-12-14 19:38:59 +0000 | [diff] [blame] | 1763 | |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 1764 | ret = ptp_getstorageinfo(params, storage->id, &storageInfo); |
| 1765 | if (ret != PTP_RC_OK) { |
| 1766 | add_ptp_error_to_errorstack(device, ret, "get_first_storage_freespace(): could not get storage info."); |
Linus Walleij | e1ac07e | 2006-12-14 19:38:59 +0000 | [diff] [blame] | 1767 | return -1; |
| 1768 | } |
| 1769 | if (storage->StorageDescription != NULL) { |
| 1770 | free(storage->StorageDescription); |
| 1771 | } |
| 1772 | if (storage->VolumeIdentifier != NULL) { |
| 1773 | free(storage->VolumeIdentifier); |
| 1774 | } |
| 1775 | storage->StorageType = storageInfo.StorageType; |
| 1776 | storage->FilesystemType = storageInfo.FilesystemType; |
| 1777 | storage->AccessCapability = storageInfo.AccessCapability; |
| 1778 | storage->MaxCapacity = storageInfo.MaxCapability; |
| 1779 | storage->FreeSpaceInBytes = storageInfo.FreeSpaceInBytes; |
| 1780 | storage->FreeSpaceInObjects = storageInfo.FreeSpaceInImages; |
| 1781 | storage->StorageDescription = storageInfo.StorageDescription; |
| 1782 | storage->VolumeIdentifier = storageInfo.VolumeLabel; |
| 1783 | } |
| 1784 | if(storage->FreeSpaceInBytes == (uint64_t) -1) |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 1785 | return -1; |
| 1786 | *freespace = storage->FreeSpaceInBytes; |
| 1787 | return 0; |
| 1788 | } |
| 1789 | |
| 1790 | /** |
Linus Walleij | 8c45b29 | 2006-04-26 14:12:44 +0000 | [diff] [blame] | 1791 | * This function dumps out a large chunk of textual information |
| 1792 | * provided from the PTP protocol and additionally some extra |
| 1793 | * MTP-specific information where applicable. |
| 1794 | * @param device a pointer to the MTP device to report info from. |
| 1795 | */ |
| 1796 | void LIBMTP_Dump_Device_Info(LIBMTP_mtpdevice_t *device) |
| 1797 | { |
| 1798 | int i; |
| 1799 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | c6210fb | 2006-05-08 11:11:41 +0000 | [diff] [blame] | 1800 | PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo; |
Linus Walleij | e1ac07e | 2006-12-14 19:38:59 +0000 | [diff] [blame] | 1801 | LIBMTP_devicestorage_t *storage = device->storage; |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 1802 | |
Linus Walleij | c6210fb | 2006-05-08 11:11:41 +0000 | [diff] [blame] | 1803 | printf("USB low-level info:\n"); |
| 1804 | dump_usbinfo(ptp_usb); |
Linus Walleij | 8c45b29 | 2006-04-26 14:12:44 +0000 | [diff] [blame] | 1805 | /* Print out some verbose information */ |
| 1806 | printf("Device info:\n"); |
| 1807 | printf(" Manufacturer: %s\n", params->deviceinfo.Manufacturer); |
| 1808 | printf(" Model: %s\n", params->deviceinfo.Model); |
| 1809 | printf(" Device version: %s\n", params->deviceinfo.DeviceVersion); |
| 1810 | printf(" Serial number: %s\n", params->deviceinfo.SerialNumber); |
| 1811 | printf(" Vendor extension ID: 0x%08x\n", params->deviceinfo.VendorExtensionID); |
| 1812 | printf(" Vendor extension description: %s\n", params->deviceinfo.VendorExtensionDesc); |
Linus Walleij | ddaba2f | 2007-10-02 21:20:30 +0000 | [diff] [blame] | 1813 | printf(" Detected object size: %d bits\n", device->object_bitsize); |
Linus Walleij | 8c45b29 | 2006-04-26 14:12:44 +0000 | [diff] [blame] | 1814 | printf("Supported operations:\n"); |
| 1815 | for (i=0;i<params->deviceinfo.OperationsSupported_len;i++) { |
Linus Walleij | 4f40d11 | 2006-09-21 07:44:53 +0000 | [diff] [blame] | 1816 | char txt[256]; |
| 1817 | |
| 1818 | (void) ptp_render_opcode (params, params->deviceinfo.OperationsSupported[i], sizeof(txt), txt); |
| 1819 | printf(" %04x: %s\n", params->deviceinfo.OperationsSupported[i], txt); |
Linus Walleij | 8c45b29 | 2006-04-26 14:12:44 +0000 | [diff] [blame] | 1820 | } |
| 1821 | printf("Events supported:\n"); |
| 1822 | if (params->deviceinfo.EventsSupported_len == 0) { |
| 1823 | printf(" None.\n"); |
| 1824 | } else { |
| 1825 | for (i=0;i<params->deviceinfo.EventsSupported_len;i++) { |
| 1826 | printf(" 0x%04x\n", params->deviceinfo.EventsSupported[i]); |
| 1827 | } |
| 1828 | } |
| 1829 | printf("Device Properties Supported:\n"); |
| 1830 | for (i=0;i<params->deviceinfo.DevicePropertiesSupported_len;i++) { |
Linus Walleij | 16c51f0 | 2006-05-04 13:20:22 +0000 | [diff] [blame] | 1831 | char const *propdesc = ptp_get_property_description(params, params->deviceinfo.DevicePropertiesSupported[i]); |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 1832 | |
Linus Walleij | 545c779 | 2006-06-13 15:22:30 +0000 | [diff] [blame] | 1833 | if (propdesc != NULL) { |
| 1834 | printf(" 0x%04x: %s\n", params->deviceinfo.DevicePropertiesSupported[i], propdesc); |
| 1835 | } else { |
| 1836 | uint16_t prop = params->deviceinfo.DevicePropertiesSupported[i]; |
Linus Walleij | cf223e6 | 2006-06-19 09:31:53 +0000 | [diff] [blame] | 1837 | printf(" 0x%04x: Unknown property\n", prop); |
Linus Walleij | 545c779 | 2006-06-13 15:22:30 +0000 | [diff] [blame] | 1838 | } |
Linus Walleij | 8c45b29 | 2006-04-26 14:12:44 +0000 | [diff] [blame] | 1839 | } |
Linus Walleij | 0af979a | 2006-06-19 11:49:10 +0000 | [diff] [blame] | 1840 | |
| 1841 | if (ptp_operation_issupported(params,PTP_OC_MTP_GetObjectPropsSupported)) { |
| 1842 | printf("Playable File (Object) Types and Object Properties Supported:\n"); |
| 1843 | for (i=0;i<params->deviceinfo.ImageFormats_len;i++) { |
| 1844 | char txt[256]; |
| 1845 | uint16_t ret; |
| 1846 | uint16_t *props = NULL; |
| 1847 | uint32_t propcnt = 0; |
| 1848 | int j; |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 1849 | |
Linus Walleij | 0af979a | 2006-06-19 11:49:10 +0000 | [diff] [blame] | 1850 | (void) ptp_render_ofc (params, params->deviceinfo.ImageFormats[i], sizeof(txt), txt); |
| 1851 | printf(" %04x: %s\n", params->deviceinfo.ImageFormats[i], txt); |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 1852 | |
Linus Walleij | 0af979a | 2006-06-19 11:49:10 +0000 | [diff] [blame] | 1853 | ret = ptp_mtp_getobjectpropssupported (params, params->deviceinfo.ImageFormats[i], &propcnt, &props); |
| 1854 | if (ret != PTP_RC_OK) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 1855 | add_ptp_error_to_errorstack(device, ret, "LIBMTP_Dump_Device_Info(): error on query for object properties."); |
Linus Walleij | 0af979a | 2006-06-19 11:49:10 +0000 | [diff] [blame] | 1856 | } else { |
| 1857 | for (j=0;j<propcnt;j++) { |
Linus Walleij | 1483034 | 2007-03-23 13:32:00 +0000 | [diff] [blame] | 1858 | PTPObjectPropDesc opd; |
| 1859 | int k; |
| 1860 | |
Linus Walleij | 0af979a | 2006-06-19 11:49:10 +0000 | [diff] [blame] | 1861 | (void) ptp_render_mtp_propname(props[j],sizeof(txt),txt); |
Linus Walleij | 1483034 | 2007-03-23 13:32:00 +0000 | [diff] [blame] | 1862 | printf(" %04x: %s", props[j], txt); |
| 1863 | // Get a more verbose description |
| 1864 | ret = ptp_mtp_getobjectpropdesc(params, props[j], params->deviceinfo.ImageFormats[i], &opd); |
| 1865 | if (ret != PTP_RC_OK) { |
| 1866 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Dump_Device_Info(): " |
| 1867 | "could not get property description."); |
| 1868 | break; |
| 1869 | } |
| 1870 | |
| 1871 | if (opd.DataType == PTP_DTC_STR) { |
| 1872 | printf(" STRING data type"); |
Linus Walleij | 22cc487 | 2007-11-05 22:57:19 +0000 | [diff] [blame] | 1873 | switch (opd.FormFlag) { |
| 1874 | case PTP_OPFF_DateTime: |
| 1875 | printf(" DATETIME FORM"); |
| 1876 | break; |
| 1877 | case PTP_OPFF_RegularExpression: |
| 1878 | printf(" REGULAR EXPRESSION FORM"); |
| 1879 | break; |
| 1880 | case PTP_OPFF_LongString: |
| 1881 | printf(" LONG STRING FORM"); |
| 1882 | break; |
| 1883 | default: |
| 1884 | break; |
| 1885 | } |
Linus Walleij | 1483034 | 2007-03-23 13:32:00 +0000 | [diff] [blame] | 1886 | } else { |
| 1887 | if (opd.DataType & PTP_DTC_ARRAY_MASK) { |
| 1888 | printf(" array of"); |
| 1889 | } |
| 1890 | |
| 1891 | switch (opd.DataType & (~PTP_DTC_ARRAY_MASK)) { |
| 1892 | |
| 1893 | case PTP_DTC_UNDEF: |
| 1894 | printf(" UNDEFINED data type"); |
| 1895 | break; |
| 1896 | |
| 1897 | case PTP_DTC_INT8: |
| 1898 | printf(" INT8 data type"); |
| 1899 | switch (opd.FormFlag) { |
Linus Walleij | 22cc487 | 2007-11-05 22:57:19 +0000 | [diff] [blame] | 1900 | case PTP_OPFF_Range: |
Linus Walleij | 1483034 | 2007-03-23 13:32:00 +0000 | [diff] [blame] | 1901 | printf(" range: MIN %d, MAX %d, STEP %d", |
| 1902 | opd.FORM.Range.MinimumValue.i8, |
| 1903 | opd.FORM.Range.MaximumValue.i8, |
| 1904 | opd.FORM.Range.StepSize.i8); |
| 1905 | break; |
Linus Walleij | 22cc487 | 2007-11-05 22:57:19 +0000 | [diff] [blame] | 1906 | case PTP_OPFF_Enumeration: |
Linus Walleij | 1483034 | 2007-03-23 13:32:00 +0000 | [diff] [blame] | 1907 | printf(" enumeration: "); |
| 1908 | for(k=0;k<opd.FORM.Enum.NumberOfValues;k++) { |
| 1909 | printf("%d, ", opd.FORM.Enum.SupportedValue[k].i8); |
| 1910 | } |
| 1911 | break; |
Linus Walleij | 22cc487 | 2007-11-05 22:57:19 +0000 | [diff] [blame] | 1912 | case PTP_OPFF_ByteArray: |
| 1913 | printf(" byte array: "); |
| 1914 | break; |
Linus Walleij | 1483034 | 2007-03-23 13:32:00 +0000 | [diff] [blame] | 1915 | default: |
| 1916 | printf(" ANY 8BIT VALUE form"); |
| 1917 | break; |
| 1918 | } |
| 1919 | break; |
| 1920 | |
| 1921 | case PTP_DTC_UINT8: |
| 1922 | printf(" UINT8 data type"); |
| 1923 | switch (opd.FormFlag) { |
Linus Walleij | 22cc487 | 2007-11-05 22:57:19 +0000 | [diff] [blame] | 1924 | case PTP_OPFF_Range: |
Linus Walleij | 1483034 | 2007-03-23 13:32:00 +0000 | [diff] [blame] | 1925 | printf(" range: MIN %d, MAX %d, STEP %d", |
| 1926 | opd.FORM.Range.MinimumValue.u8, |
| 1927 | opd.FORM.Range.MaximumValue.u8, |
| 1928 | opd.FORM.Range.StepSize.u8); |
| 1929 | break; |
Linus Walleij | 22cc487 | 2007-11-05 22:57:19 +0000 | [diff] [blame] | 1930 | case PTP_OPFF_Enumeration: |
Linus Walleij | 1483034 | 2007-03-23 13:32:00 +0000 | [diff] [blame] | 1931 | printf(" enumeration: "); |
| 1932 | for(k=0;k<opd.FORM.Enum.NumberOfValues;k++) { |
| 1933 | printf("%d, ", opd.FORM.Enum.SupportedValue[k].u8); |
| 1934 | } |
| 1935 | break; |
Linus Walleij | 22cc487 | 2007-11-05 22:57:19 +0000 | [diff] [blame] | 1936 | case PTP_OPFF_ByteArray: |
| 1937 | printf(" byte array: "); |
| 1938 | break; |
Linus Walleij | 1483034 | 2007-03-23 13:32:00 +0000 | [diff] [blame] | 1939 | default: |
| 1940 | printf(" ANY 8BIT VALUE form"); |
| 1941 | break; |
| 1942 | } |
| 1943 | break; |
| 1944 | |
| 1945 | case PTP_DTC_INT16: |
| 1946 | printf(" INT16 data type"); |
| 1947 | switch (opd.FormFlag) { |
Linus Walleij | 22cc487 | 2007-11-05 22:57:19 +0000 | [diff] [blame] | 1948 | case PTP_OPFF_Range: |
Linus Walleij | 1483034 | 2007-03-23 13:32:00 +0000 | [diff] [blame] | 1949 | printf(" range: MIN %d, MAX %d, STEP %d", |
| 1950 | opd.FORM.Range.MinimumValue.i16, |
| 1951 | opd.FORM.Range.MaximumValue.i16, |
| 1952 | opd.FORM.Range.StepSize.i16); |
| 1953 | break; |
Linus Walleij | 22cc487 | 2007-11-05 22:57:19 +0000 | [diff] [blame] | 1954 | case PTP_OPFF_Enumeration: |
Linus Walleij | 1483034 | 2007-03-23 13:32:00 +0000 | [diff] [blame] | 1955 | printf(" enumeration: "); |
| 1956 | for(k=0;k<opd.FORM.Enum.NumberOfValues;k++) { |
| 1957 | printf("%d, ", opd.FORM.Enum.SupportedValue[k].i16); |
| 1958 | } |
| 1959 | break; |
| 1960 | default: |
| 1961 | printf(" ANY 16BIT VALUE form"); |
| 1962 | break; |
| 1963 | } |
| 1964 | break; |
| 1965 | |
| 1966 | case PTP_DTC_UINT16: |
| 1967 | printf(" UINT16 data type"); |
| 1968 | switch (opd.FormFlag) { |
Linus Walleij | 22cc487 | 2007-11-05 22:57:19 +0000 | [diff] [blame] | 1969 | case PTP_OPFF_Range: |
Linus Walleij | 1483034 | 2007-03-23 13:32:00 +0000 | [diff] [blame] | 1970 | printf(" range: MIN %d, MAX %d, STEP %d", |
| 1971 | opd.FORM.Range.MinimumValue.u16, |
| 1972 | opd.FORM.Range.MaximumValue.u16, |
| 1973 | opd.FORM.Range.StepSize.u16); |
| 1974 | break; |
Linus Walleij | 22cc487 | 2007-11-05 22:57:19 +0000 | [diff] [blame] | 1975 | case PTP_OPFF_Enumeration: |
Linus Walleij | 1483034 | 2007-03-23 13:32:00 +0000 | [diff] [blame] | 1976 | printf(" enumeration: "); |
| 1977 | for(k=0;k<opd.FORM.Enum.NumberOfValues;k++) { |
| 1978 | printf("%d, ", opd.FORM.Enum.SupportedValue[k].u16); |
| 1979 | } |
| 1980 | break; |
| 1981 | default: |
| 1982 | printf(" ANY 16BIT VALUE form"); |
| 1983 | break; |
| 1984 | } |
| 1985 | break; |
| 1986 | |
| 1987 | case PTP_DTC_INT32: |
| 1988 | printf(" INT32 data type"); |
| 1989 | switch (opd.FormFlag) { |
Linus Walleij | 22cc487 | 2007-11-05 22:57:19 +0000 | [diff] [blame] | 1990 | case PTP_OPFF_Range: |
Linus Walleij | 1483034 | 2007-03-23 13:32:00 +0000 | [diff] [blame] | 1991 | printf(" range: MIN %d, MAX %d, STEP %d", |
| 1992 | opd.FORM.Range.MinimumValue.i32, |
| 1993 | opd.FORM.Range.MaximumValue.i32, |
| 1994 | opd.FORM.Range.StepSize.i32); |
| 1995 | break; |
Linus Walleij | 22cc487 | 2007-11-05 22:57:19 +0000 | [diff] [blame] | 1996 | case PTP_OPFF_Enumeration: |
Linus Walleij | 1483034 | 2007-03-23 13:32:00 +0000 | [diff] [blame] | 1997 | printf(" enumeration: "); |
| 1998 | for(k=0;k<opd.FORM.Enum.NumberOfValues;k++) { |
| 1999 | printf("%d, ", opd.FORM.Enum.SupportedValue[k].i32); |
| 2000 | } |
| 2001 | break; |
| 2002 | default: |
| 2003 | printf(" ANY 32BIT VALUE form"); |
| 2004 | break; |
| 2005 | } |
| 2006 | break; |
| 2007 | |
| 2008 | case PTP_DTC_UINT32: |
| 2009 | printf(" UINT32 data type"); |
| 2010 | switch (opd.FormFlag) { |
Linus Walleij | 22cc487 | 2007-11-05 22:57:19 +0000 | [diff] [blame] | 2011 | case PTP_OPFF_Range: |
Linus Walleij | 1483034 | 2007-03-23 13:32:00 +0000 | [diff] [blame] | 2012 | printf(" range: MIN %d, MAX %d, STEP %d", |
| 2013 | opd.FORM.Range.MinimumValue.u32, |
| 2014 | opd.FORM.Range.MaximumValue.u32, |
| 2015 | opd.FORM.Range.StepSize.u32); |
| 2016 | break; |
Linus Walleij | 22cc487 | 2007-11-05 22:57:19 +0000 | [diff] [blame] | 2017 | case PTP_OPFF_Enumeration: |
Linus Walleij | 1483034 | 2007-03-23 13:32:00 +0000 | [diff] [blame] | 2018 | printf(" enumeration: "); |
| 2019 | for(k=0;k<opd.FORM.Enum.NumberOfValues;k++) { |
| 2020 | printf("%d, ", opd.FORM.Enum.SupportedValue[k].u32); |
| 2021 | } |
| 2022 | break; |
| 2023 | default: |
| 2024 | printf(" ANY 32BIT VALUE form"); |
| 2025 | break; |
| 2026 | } |
| 2027 | break; |
| 2028 | |
| 2029 | case PTP_DTC_INT64: |
| 2030 | printf(" INT64 data type"); |
| 2031 | break; |
| 2032 | |
| 2033 | case PTP_DTC_UINT64: |
| 2034 | printf(" UINT64 data type"); |
| 2035 | break; |
| 2036 | |
| 2037 | case PTP_DTC_INT128: |
| 2038 | printf(" INT128 data type"); |
| 2039 | break; |
| 2040 | |
| 2041 | case PTP_DTC_UINT128: |
| 2042 | printf(" UINT128 data type"); |
| 2043 | break; |
| 2044 | |
| 2045 | default: |
| 2046 | printf(" UNKNOWN data type"); |
| 2047 | break; |
| 2048 | } |
| 2049 | } |
| 2050 | if (opd.GetSet) { |
| 2051 | printf(" GET/SET"); |
| 2052 | } else { |
| 2053 | printf(" READ ONLY"); |
| 2054 | } |
| 2055 | printf("\n"); |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 2056 | ptp_free_objectpropdesc(&opd); |
Linus Walleij | 0af979a | 2006-06-19 11:49:10 +0000 | [diff] [blame] | 2057 | } |
| 2058 | free(props); |
| 2059 | } |
| 2060 | } |
| 2061 | } |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 2062 | |
Linus Walleij | e1ac07e | 2006-12-14 19:38:59 +0000 | [diff] [blame] | 2063 | if(storage != NULL && ptp_operation_issupported(params,PTP_OC_GetStorageInfo)) { |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 2064 | printf("Storage Devices:\n"); |
Linus Walleij | e1ac07e | 2006-12-14 19:38:59 +0000 | [diff] [blame] | 2065 | while(storage != NULL) { |
| 2066 | printf(" StorageID: 0x%08x\n",storage->id); |
| 2067 | printf(" StorageType: 0x%04x\n",storage->StorageType); |
| 2068 | printf(" FilesystemType: 0x%04x\n",storage->FilesystemType); |
| 2069 | printf(" AccessCapability: 0x%04x\n",storage->AccessCapability); |
Linus Walleij | fec4d56 | 2008-06-01 22:30:36 +0000 | [diff] [blame] | 2070 | printf(" MaxCapacity: %llu\n", (long long unsigned int) storage->MaxCapacity); |
| 2071 | printf(" FreeSpaceInBytes: %llu\n", (long long unsigned int) storage->FreeSpaceInBytes); |
| 2072 | printf(" FreeSpaceInObjects: %llu\n", (long long unsigned int) storage->FreeSpaceInObjects); |
Linus Walleij | e1ac07e | 2006-12-14 19:38:59 +0000 | [diff] [blame] | 2073 | printf(" StorageDescription: %s\n",storage->StorageDescription); |
| 2074 | printf(" VolumeIdentifier: %s\n",storage->VolumeIdentifier); |
| 2075 | storage = storage->next; |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 2076 | } |
| 2077 | } |
| 2078 | |
Linus Walleij | 545c779 | 2006-06-13 15:22:30 +0000 | [diff] [blame] | 2079 | printf("Special directories:\n"); |
| 2080 | printf(" Default music folder: 0x%08x\n", device->default_music_folder); |
| 2081 | printf(" Default playlist folder: 0x%08x\n", device->default_playlist_folder); |
| 2082 | printf(" Default picture folder: 0x%08x\n", device->default_picture_folder); |
| 2083 | printf(" Default video folder: 0x%08x\n", device->default_video_folder); |
| 2084 | printf(" Default organizer folder: 0x%08x\n", device->default_organizer_folder); |
| 2085 | printf(" Default zencast folder: 0x%08x\n", device->default_zencast_folder); |
Linus Walleij | ccf28ce | 2006-11-16 16:06:38 +0000 | [diff] [blame] | 2086 | printf(" Default album folder: 0x%08x\n", device->default_album_folder); |
Linus Walleij | 9316e65 | 2006-12-07 09:55:21 +0000 | [diff] [blame] | 2087 | printf(" Default text folder: 0x%08x\n", device->default_text_folder); |
Linus Walleij | 8c45b29 | 2006-04-26 14:12:44 +0000 | [diff] [blame] | 2088 | } |
| 2089 | |
| 2090 | /** |
Linus Walleij | 5d533bb | 2007-07-17 21:48:57 +0000 | [diff] [blame] | 2091 | * This resets a device in case it supports the <code>PTP_OC_ResetDevice</code> |
| 2092 | * operation code (0x1010). |
| 2093 | * @param device a pointer to the device to reset. |
| 2094 | * @return 0 on success, any other value means failure. |
| 2095 | */ |
| 2096 | int LIBMTP_Reset_Device(LIBMTP_mtpdevice_t *device) |
| 2097 | { |
| 2098 | PTPParams *params = (PTPParams *) device->params; |
| 2099 | uint16_t ret; |
| 2100 | |
| 2101 | if (!ptp_operation_issupported(params,PTP_OC_ResetDevice)) { |
| 2102 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, |
| 2103 | "LIBMTP_Reset_Device(): device does not support resetting."); |
| 2104 | return -1; |
| 2105 | } |
| 2106 | ret = ptp_resetdevice(params); |
| 2107 | if (ret != PTP_RC_OK) { |
| 2108 | add_ptp_error_to_errorstack(device, ret, "Error resetting."); |
| 2109 | return -1; |
| 2110 | } |
| 2111 | return 0; |
| 2112 | } |
| 2113 | |
| 2114 | /** |
Linus Walleij | 2350b71 | 2008-01-14 22:54:37 +0000 | [diff] [blame] | 2115 | * This retrieves the manufacturer name of an MTP device. |
| 2116 | * @param device a pointer to the device to get the manufacturer name for. |
| 2117 | * @return a newly allocated UTF-8 string representing the manufacturer name. |
| 2118 | * The string must be freed by the caller after use. If the call |
| 2119 | * was unsuccessful this will contain NULL. |
| 2120 | */ |
| 2121 | char *LIBMTP_Get_Manufacturername(LIBMTP_mtpdevice_t *device) |
| 2122 | { |
| 2123 | char *retmanuf = NULL; |
| 2124 | PTPParams *params = (PTPParams *) device->params; |
| 2125 | |
| 2126 | if (params->deviceinfo.Manufacturer != NULL) { |
| 2127 | retmanuf = strdup(params->deviceinfo.Manufacturer); |
| 2128 | } |
| 2129 | return retmanuf; |
| 2130 | } |
| 2131 | |
| 2132 | /** |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 2133 | * This retrieves the model name (often equal to product name) |
Linus Walleij | 8012406 | 2006-03-15 10:26:09 +0000 | [diff] [blame] | 2134 | * of an MTP device. |
| 2135 | * @param device a pointer to the device to get the model name for. |
| 2136 | * @return a newly allocated UTF-8 string representing the model name. |
| 2137 | * The string must be freed by the caller after use. If the call |
| 2138 | * was unsuccessful this will contain NULL. |
| 2139 | */ |
| 2140 | char *LIBMTP_Get_Modelname(LIBMTP_mtpdevice_t *device) |
| 2141 | { |
| 2142 | char *retmodel = NULL; |
Linus Walleij | 9b28da3 | 2006-03-16 13:47:58 +0000 | [diff] [blame] | 2143 | PTPParams *params = (PTPParams *) device->params; |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 2144 | |
Linus Walleij | 9b28da3 | 2006-03-16 13:47:58 +0000 | [diff] [blame] | 2145 | if (params->deviceinfo.Model != NULL) { |
| 2146 | retmodel = strdup(params->deviceinfo.Model); |
Linus Walleij | 8012406 | 2006-03-15 10:26:09 +0000 | [diff] [blame] | 2147 | } |
| 2148 | return retmodel; |
| 2149 | } |
| 2150 | |
| 2151 | /** |
| 2152 | * This retrieves the serial number of an MTP device. |
| 2153 | * @param device a pointer to the device to get the serial number for. |
| 2154 | * @return a newly allocated UTF-8 string representing the serial number. |
| 2155 | * The string must be freed by the caller after use. If the call |
| 2156 | * was unsuccessful this will contain NULL. |
| 2157 | */ |
| 2158 | char *LIBMTP_Get_Serialnumber(LIBMTP_mtpdevice_t *device) |
| 2159 | { |
| 2160 | char *retnumber = NULL; |
Linus Walleij | 9b28da3 | 2006-03-16 13:47:58 +0000 | [diff] [blame] | 2161 | PTPParams *params = (PTPParams *) device->params; |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 2162 | |
Linus Walleij | 9b28da3 | 2006-03-16 13:47:58 +0000 | [diff] [blame] | 2163 | if (params->deviceinfo.SerialNumber != NULL) { |
| 2164 | retnumber = strdup(params->deviceinfo.SerialNumber); |
Linus Walleij | 8012406 | 2006-03-15 10:26:09 +0000 | [diff] [blame] | 2165 | } |
| 2166 | return retnumber; |
| 2167 | } |
| 2168 | |
| 2169 | /** |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 2170 | * This retrieves the device version (hardware and firmware version) of an |
Linus Walleij | 8012406 | 2006-03-15 10:26:09 +0000 | [diff] [blame] | 2171 | * MTP device. |
| 2172 | * @param device a pointer to the device to get the device version for. |
| 2173 | * @return a newly allocated UTF-8 string representing the device version. |
| 2174 | * The string must be freed by the caller after use. If the call |
| 2175 | * was unsuccessful this will contain NULL. |
| 2176 | */ |
| 2177 | char *LIBMTP_Get_Deviceversion(LIBMTP_mtpdevice_t *device) |
| 2178 | { |
| 2179 | char *retversion = NULL; |
Linus Walleij | 9b28da3 | 2006-03-16 13:47:58 +0000 | [diff] [blame] | 2180 | PTPParams *params = (PTPParams *) device->params; |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 2181 | |
Linus Walleij | 9b28da3 | 2006-03-16 13:47:58 +0000 | [diff] [blame] | 2182 | if (params->deviceinfo.DeviceVersion != NULL) { |
| 2183 | retversion = strdup(params->deviceinfo.DeviceVersion); |
Linus Walleij | 8012406 | 2006-03-15 10:26:09 +0000 | [diff] [blame] | 2184 | } |
| 2185 | return retversion; |
| 2186 | } |
| 2187 | |
| 2188 | |
| 2189 | /** |
Linus Walleij | fae2748 | 2006-08-19 20:13:25 +0000 | [diff] [blame] | 2190 | * This retrieves the "friendly name" of an MTP device. Usually |
| 2191 | * this is simply the name of the owner or something like |
Linus Walleij | 3065879 | 2006-08-19 22:18:55 +0000 | [diff] [blame] | 2192 | * "John Doe's Digital Audio Player". This property should be supported |
Linus Walleij | fae2748 | 2006-08-19 20:13:25 +0000 | [diff] [blame] | 2193 | * by all MTP devices. |
| 2194 | * @param device a pointer to the device to get the friendly name for. |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 2195 | * @return a newly allocated UTF-8 string representing the friendly name. |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 2196 | * The string must be freed by the caller after use. |
Linus Walleij | 3065879 | 2006-08-19 22:18:55 +0000 | [diff] [blame] | 2197 | * @see LIBMTP_Set_Friendlyname() |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 2198 | */ |
Linus Walleij | 3065879 | 2006-08-19 22:18:55 +0000 | [diff] [blame] | 2199 | char *LIBMTP_Get_Friendlyname(LIBMTP_mtpdevice_t *device) |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 2200 | { |
Linus Walleij | b02a066 | 2006-04-25 08:05:09 +0000 | [diff] [blame] | 2201 | PTPPropertyValue propval; |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 2202 | char *retstring = NULL; |
Linus Walleij | 9b28da3 | 2006-03-16 13:47:58 +0000 | [diff] [blame] | 2203 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 2204 | uint16_t ret; |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 2205 | |
Linus Walleij | cf223e6 | 2006-06-19 09:31:53 +0000 | [diff] [blame] | 2206 | if (!ptp_property_issupported(params, PTP_DPC_MTP_DeviceFriendlyName)) { |
| 2207 | return NULL; |
| 2208 | } |
| 2209 | |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 2210 | ret = ptp_getdevicepropvalue(params, |
| 2211 | PTP_DPC_MTP_DeviceFriendlyName, |
| 2212 | &propval, |
| 2213 | PTP_DTC_STR); |
| 2214 | if (ret != PTP_RC_OK) { |
| 2215 | add_ptp_error_to_errorstack(device, ret, "Error getting friendlyname."); |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 2216 | return NULL; |
| 2217 | } |
Linus Walleij | a823a70 | 2006-08-27 21:27:46 +0000 | [diff] [blame] | 2218 | if (propval.str != NULL) { |
| 2219 | retstring = strdup(propval.str); |
| 2220 | free(propval.str); |
| 2221 | } |
Linus Walleij | fae2748 | 2006-08-19 20:13:25 +0000 | [diff] [blame] | 2222 | return retstring; |
| 2223 | } |
| 2224 | |
| 2225 | /** |
Linus Walleij | 3065879 | 2006-08-19 22:18:55 +0000 | [diff] [blame] | 2226 | * Sets the "friendly name" of an MTP device. |
| 2227 | * @param device a pointer to the device to set the friendly name for. |
| 2228 | * @param friendlyname the new friendly name for the device. |
| 2229 | * @return 0 on success, any other value means failure. |
| 2230 | * @see LIBMTP_Get_Ownername() |
| 2231 | */ |
| 2232 | int LIBMTP_Set_Friendlyname(LIBMTP_mtpdevice_t *device, |
| 2233 | char const * const friendlyname) |
| 2234 | { |
| 2235 | PTPPropertyValue propval; |
| 2236 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 2237 | uint16_t ret; |
Linus Walleij | 3065879 | 2006-08-19 22:18:55 +0000 | [diff] [blame] | 2238 | |
| 2239 | if (!ptp_property_issupported(params, PTP_DPC_MTP_DeviceFriendlyName)) { |
| 2240 | return -1; |
| 2241 | } |
Linus Walleij | a823a70 | 2006-08-27 21:27:46 +0000 | [diff] [blame] | 2242 | propval.str = (char *) friendlyname; |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 2243 | ret = ptp_setdevicepropvalue(params, |
| 2244 | PTP_DPC_MTP_DeviceFriendlyName, |
| 2245 | &propval, |
| 2246 | PTP_DTC_STR); |
| 2247 | if (ret != PTP_RC_OK) { |
| 2248 | add_ptp_error_to_errorstack(device, ret, "Error setting friendlyname."); |
Linus Walleij | 3065879 | 2006-08-19 22:18:55 +0000 | [diff] [blame] | 2249 | return -1; |
| 2250 | } |
Linus Walleij | 3065879 | 2006-08-19 22:18:55 +0000 | [diff] [blame] | 2251 | return 0; |
| 2252 | } |
| 2253 | |
| 2254 | /** |
Linus Walleij | fae2748 | 2006-08-19 20:13:25 +0000 | [diff] [blame] | 2255 | * This retrieves the syncronization partner of an MTP device. This |
| 2256 | * property should be supported by all MTP devices. |
| 2257 | * @param device a pointer to the device to get the sync partner for. |
| 2258 | * @return a newly allocated UTF-8 string representing the synchronization |
| 2259 | * partner. The string must be freed by the caller after use. |
Linus Walleij | 3065879 | 2006-08-19 22:18:55 +0000 | [diff] [blame] | 2260 | * @see LIBMTP_Set_Syncpartner() |
Linus Walleij | fae2748 | 2006-08-19 20:13:25 +0000 | [diff] [blame] | 2261 | */ |
| 2262 | char *LIBMTP_Get_Syncpartner(LIBMTP_mtpdevice_t *device) |
| 2263 | { |
| 2264 | PTPPropertyValue propval; |
| 2265 | char *retstring = NULL; |
| 2266 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 2267 | uint16_t ret; |
Linus Walleij | fae2748 | 2006-08-19 20:13:25 +0000 | [diff] [blame] | 2268 | |
| 2269 | if (!ptp_property_issupported(params, PTP_DPC_MTP_SynchronizationPartner)) { |
| 2270 | return NULL; |
| 2271 | } |
| 2272 | |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 2273 | ret = ptp_getdevicepropvalue(params, |
| 2274 | PTP_DPC_MTP_SynchronizationPartner, |
| 2275 | &propval, |
| 2276 | PTP_DTC_STR); |
| 2277 | if (ret != PTP_RC_OK) { |
| 2278 | add_ptp_error_to_errorstack(device, ret, "Error getting syncpartner."); |
Linus Walleij | fae2748 | 2006-08-19 20:13:25 +0000 | [diff] [blame] | 2279 | return NULL; |
| 2280 | } |
Linus Walleij | a823a70 | 2006-08-27 21:27:46 +0000 | [diff] [blame] | 2281 | if (propval.str != NULL) { |
| 2282 | retstring = strdup(propval.str); |
| 2283 | free(propval.str); |
| 2284 | } |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 2285 | return retstring; |
| 2286 | } |
| 2287 | |
Linus Walleij | 3065879 | 2006-08-19 22:18:55 +0000 | [diff] [blame] | 2288 | |
| 2289 | /** |
| 2290 | * Sets the synchronization partner of an MTP device. Note that |
| 2291 | * we have no idea what the effect of setting this to "foobar" |
| 2292 | * may be. But the general idea seems to be to tell which program |
| 2293 | * shall synchronize with this device and tell others to leave |
| 2294 | * it alone. |
| 2295 | * @param device a pointer to the device to set the sync partner for. |
| 2296 | * @param syncpartner the new synchronization partner for the device. |
| 2297 | * @return 0 on success, any other value means failure. |
| 2298 | * @see LIBMTP_Get_Syncpartner() |
| 2299 | */ |
| 2300 | int LIBMTP_Set_Syncpartner(LIBMTP_mtpdevice_t *device, |
| 2301 | char const * const syncpartner) |
| 2302 | { |
| 2303 | PTPPropertyValue propval; |
| 2304 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 2305 | uint16_t ret; |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 2306 | |
Linus Walleij | 3065879 | 2006-08-19 22:18:55 +0000 | [diff] [blame] | 2307 | if (!ptp_property_issupported(params, PTP_DPC_MTP_SynchronizationPartner)) { |
| 2308 | return -1; |
| 2309 | } |
Linus Walleij | a823a70 | 2006-08-27 21:27:46 +0000 | [diff] [blame] | 2310 | propval.str = (char *) syncpartner; |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 2311 | ret = ptp_setdevicepropvalue(params, |
| 2312 | PTP_DPC_MTP_SynchronizationPartner, |
| 2313 | &propval, |
| 2314 | PTP_DTC_STR); |
| 2315 | if (ret != PTP_RC_OK) { |
| 2316 | add_ptp_error_to_errorstack(device, ret, "Error setting syncpartner."); |
Linus Walleij | 3065879 | 2006-08-19 22:18:55 +0000 | [diff] [blame] | 2317 | return -1; |
| 2318 | } |
Linus Walleij | 3065879 | 2006-08-19 22:18:55 +0000 | [diff] [blame] | 2319 | return 0; |
| 2320 | } |
| 2321 | |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 2322 | /** |
Linus Walleij | f5fcda3 | 2006-12-03 22:31:02 +0000 | [diff] [blame] | 2323 | * Checks if the device can stora a file of this size or |
| 2324 | * if it's too big. |
| 2325 | * @param device a pointer to the device. |
| 2326 | * @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] | 2327 | * @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] | 2328 | * @return 0 if the file fits, any other value means failure. |
| 2329 | */ |
Linus Walleij | 6bf68b4 | 2007-09-03 22:50:02 +0000 | [diff] [blame] | 2330 | static int check_if_file_fits(LIBMTP_mtpdevice_t *device, |
| 2331 | LIBMTP_devicestorage_t *storage, |
| 2332 | uint64_t const filesize) { |
Linus Walleij | f5fcda3 | 2006-12-03 22:31:02 +0000 | [diff] [blame] | 2333 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | f5fcda3 | 2006-12-03 22:31:02 +0000 | [diff] [blame] | 2334 | uint64_t freebytes; |
Linus Walleij | f5fcda3 | 2006-12-03 22:31:02 +0000 | [diff] [blame] | 2335 | int ret; |
| 2336 | |
| 2337 | // If we cannot check the storage, no big deal. |
| 2338 | if (!ptp_operation_issupported(params,PTP_OC_GetStorageInfo)) { |
| 2339 | return 0; |
| 2340 | } |
| 2341 | |
Linus Walleij | 6bf68b4 | 2007-09-03 22:50:02 +0000 | [diff] [blame] | 2342 | ret = get_storage_freespace(device, storage, &freebytes); |
Linus Walleij | f5fcda3 | 2006-12-03 22:31:02 +0000 | [diff] [blame] | 2343 | if (ret != 0) { |
Linus Walleij | 6bf68b4 | 2007-09-03 22:50:02 +0000 | [diff] [blame] | 2344 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, |
| 2345 | "check_if_file_fits(): error checking free storage."); |
Linus Walleij | f5fcda3 | 2006-12-03 22:31:02 +0000 | [diff] [blame] | 2346 | return -1; |
| 2347 | } else { |
Linus Walleij | fb28b63 | 2007-10-23 21:56:18 +0000 | [diff] [blame] | 2348 | // Storage IDs with the lower 16 bits 0x0000 are not supposed |
| 2349 | // to be writeable. |
| 2350 | if ((storage->id & 0x0000FFFFU) == 0x00000000U) { |
| 2351 | return -1; |
| 2352 | } |
| 2353 | // See if it fits. |
Linus Walleij | f5fcda3 | 2006-12-03 22:31:02 +0000 | [diff] [blame] | 2354 | if (filesize > freebytes) { |
Linus Walleij | f5fcda3 | 2006-12-03 22:31:02 +0000 | [diff] [blame] | 2355 | return -1; |
| 2356 | } |
| 2357 | } |
| 2358 | return 0; |
| 2359 | } |
| 2360 | |
| 2361 | |
Linus Walleij | f5fcda3 | 2006-12-03 22:31:02 +0000 | [diff] [blame] | 2362 | /** |
Linus Walleij | fa1374c | 2006-02-27 07:41:46 +0000 | [diff] [blame] | 2363 | * This function retrieves the current battery level on the device. |
| 2364 | * @param device a pointer to the device to get the battery level for. |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 2365 | * @param maximum_level a pointer to a variable that will hold the |
Linus Walleij | fa1374c | 2006-02-27 07:41:46 +0000 | [diff] [blame] | 2366 | * maximum level of the battery if the call was successful. |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 2367 | * @param current_level a pointer to a variable that will hold the |
Linus Walleij | fa1374c | 2006-02-27 07:41:46 +0000 | [diff] [blame] | 2368 | * current level of the battery if the call was successful. |
Linus Walleij | 545c779 | 2006-06-13 15:22:30 +0000 | [diff] [blame] | 2369 | * A value of 0 means that the device is on external power. |
Linus Walleij | fa1374c | 2006-02-27 07:41:46 +0000 | [diff] [blame] | 2370 | * @return 0 if the storage info was successfully retrieved, any other |
Linus Walleij | 8043934 | 2006-09-12 10:42:26 +0000 | [diff] [blame] | 2371 | * means failure. A typical cause of failure is that |
Linus Walleij | 545c779 | 2006-06-13 15:22:30 +0000 | [diff] [blame] | 2372 | * the device does not support the battery level property. |
Linus Walleij | fa1374c | 2006-02-27 07:41:46 +0000 | [diff] [blame] | 2373 | */ |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 2374 | int LIBMTP_Get_Batterylevel(LIBMTP_mtpdevice_t *device, |
| 2375 | uint8_t * const maximum_level, |
Linus Walleij | fa1374c | 2006-02-27 07:41:46 +0000 | [diff] [blame] | 2376 | uint8_t * const current_level) |
| 2377 | { |
Linus Walleij | b02a066 | 2006-04-25 08:05:09 +0000 | [diff] [blame] | 2378 | PTPPropertyValue propval; |
Linus Walleij | fa1374c | 2006-02-27 07:41:46 +0000 | [diff] [blame] | 2379 | uint16_t ret; |
Linus Walleij | 9b28da3 | 2006-03-16 13:47:58 +0000 | [diff] [blame] | 2380 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | fa1374c | 2006-02-27 07:41:46 +0000 | [diff] [blame] | 2381 | |
Linus Walleij | 545c779 | 2006-06-13 15:22:30 +0000 | [diff] [blame] | 2382 | *maximum_level = 0; |
| 2383 | *current_level = 0; |
| 2384 | |
| 2385 | if (!ptp_property_issupported(params, PTP_DPC_BatteryLevel)) { |
| 2386 | return -1; |
| 2387 | } |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 2388 | |
Linus Walleij | b02a066 | 2006-04-25 08:05:09 +0000 | [diff] [blame] | 2389 | ret = ptp_getdevicepropvalue(params, PTP_DPC_BatteryLevel, &propval, PTP_DTC_UINT8); |
| 2390 | if (ret != PTP_RC_OK) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 2391 | add_ptp_error_to_errorstack(device, ret, "LIBMTP_Get_Batterylevel(): could not get device property value."); |
Linus Walleij | fa1374c | 2006-02-27 07:41:46 +0000 | [diff] [blame] | 2392 | return -1; |
| 2393 | } |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 2394 | |
Linus Walleij | fa1374c | 2006-02-27 07:41:46 +0000 | [diff] [blame] | 2395 | *maximum_level = device->maximum_battery_level; |
Linus Walleij | b02a066 | 2006-04-25 08:05:09 +0000 | [diff] [blame] | 2396 | *current_level = propval.u8; |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 2397 | |
Linus Walleij | fa1374c | 2006-02-27 07:41:46 +0000 | [diff] [blame] | 2398 | return 0; |
| 2399 | } |
| 2400 | |
Linus Walleij | 13374a4 | 2006-09-13 11:55:30 +0000 | [diff] [blame] | 2401 | |
| 2402 | /** |
| 2403 | * Formats device storage (if the device supports the operation). |
| 2404 | * WARNING: This WILL delete all data from the device. Make sure you've |
| 2405 | * got confirmation from the user BEFORE you call this function. |
| 2406 | * |
Linus Walleij | f849191 | 2006-12-15 10:23:30 +0000 | [diff] [blame] | 2407 | * @param device a pointer to the device containing the storage to format. |
| 2408 | * @param storage the actual storage to format. |
Linus Walleij | 13374a4 | 2006-09-13 11:55:30 +0000 | [diff] [blame] | 2409 | * @return 0 on success, any other value means failure. |
| 2410 | */ |
Linus Walleij | f849191 | 2006-12-15 10:23:30 +0000 | [diff] [blame] | 2411 | int LIBMTP_Format_Storage(LIBMTP_mtpdevice_t *device, LIBMTP_devicestorage_t *storage) |
Linus Walleij | 13374a4 | 2006-09-13 11:55:30 +0000 | [diff] [blame] | 2412 | { |
| 2413 | uint16_t ret; |
| 2414 | PTPParams *params = (PTPParams *) device->params; |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 2415 | |
Linus Walleij | 13374a4 | 2006-09-13 11:55:30 +0000 | [diff] [blame] | 2416 | if (!ptp_operation_issupported(params,PTP_OC_FormatStore)) { |
Linus Walleij | 5d533bb | 2007-07-17 21:48:57 +0000 | [diff] [blame] | 2417 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, |
| 2418 | "LIBMTP_Format_Storage(): device does not support formatting storage."); |
Linus Walleij | 13374a4 | 2006-09-13 11:55:30 +0000 | [diff] [blame] | 2419 | return -1; |
| 2420 | } |
Linus Walleij | f849191 | 2006-12-15 10:23:30 +0000 | [diff] [blame] | 2421 | ret = ptp_formatstore(params, storage->id); |
Linus Walleij | 13374a4 | 2006-09-13 11:55:30 +0000 | [diff] [blame] | 2422 | if (ret != PTP_RC_OK) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 2423 | add_ptp_error_to_errorstack(device, ret, "LIBMTP_Format_Storage(): failed to format storage."); |
Linus Walleij | 13374a4 | 2006-09-13 11:55:30 +0000 | [diff] [blame] | 2424 | return -1; |
| 2425 | } |
| 2426 | return 0; |
| 2427 | } |
| 2428 | |
Linus Walleij | fa1374c | 2006-02-27 07:41:46 +0000 | [diff] [blame] | 2429 | /** |
Linus Walleij | 545c779 | 2006-06-13 15:22:30 +0000 | [diff] [blame] | 2430 | * Helper function to extract a unicode property off a device. |
Linus Walleij | e46f12e | 2006-06-22 17:53:25 +0000 | [diff] [blame] | 2431 | * This is the standard way of retrieveing unicode device |
| 2432 | * properties as described by the PTP spec. |
Linus Walleij | cf223e6 | 2006-06-19 09:31:53 +0000 | [diff] [blame] | 2433 | * @param device a pointer to the device to get the property from. |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 2434 | * @param unicstring a pointer to a pointer that will hold the |
Linus Walleij | cf223e6 | 2006-06-19 09:31:53 +0000 | [diff] [blame] | 2435 | * property after this call is completed. |
| 2436 | * @param property the property to retrieve. |
| 2437 | * @return 0 on success, any other value means failure. |
Linus Walleij | 545c779 | 2006-06-13 15:22:30 +0000 | [diff] [blame] | 2438 | */ |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 2439 | static int get_device_unicode_property(LIBMTP_mtpdevice_t *device, |
Linus Walleij | cf223e6 | 2006-06-19 09:31:53 +0000 | [diff] [blame] | 2440 | char **unicstring, uint16_t property) |
Linus Walleij | 545c779 | 2006-06-13 15:22:30 +0000 | [diff] [blame] | 2441 | { |
| 2442 | PTPPropertyValue propval; |
| 2443 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | 16571dc | 2006-08-17 20:27:46 +0000 | [diff] [blame] | 2444 | uint16_t *tmp; |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 2445 | uint16_t ret; |
Linus Walleij | 545c779 | 2006-06-13 15:22:30 +0000 | [diff] [blame] | 2446 | int i; |
| 2447 | |
| 2448 | if (!ptp_property_issupported(params, property)) { |
| 2449 | return -1; |
| 2450 | } |
| 2451 | |
Linus Walleij | cf223e6 | 2006-06-19 09:31:53 +0000 | [diff] [blame] | 2452 | // Unicode strings are 16bit unsigned integer arrays. |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 2453 | ret = ptp_getdevicepropvalue(params, |
| 2454 | property, |
| 2455 | &propval, |
| 2456 | PTP_DTC_AUINT16); |
| 2457 | if (ret != PTP_RC_OK) { |
| 2458 | // TODO: add a note on WHICH property that we failed to get. |
tedbullock | 4e51cb9 | 2007-02-15 11:48:34 +0000 | [diff] [blame] | 2459 | *unicstring = NULL; |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 2460 | add_ptp_error_to_errorstack(device, ret, "get_device_unicode_property(): failed to get unicode property."); |
Linus Walleij | 545c779 | 2006-06-13 15:22:30 +0000 | [diff] [blame] | 2461 | return -1; |
| 2462 | } |
| 2463 | |
| 2464 | // Extract the actual array. |
Linus Walleij | 16571dc | 2006-08-17 20:27:46 +0000 | [diff] [blame] | 2465 | // printf("Array of %d elements\n", propval.a.count); |
| 2466 | tmp = malloc((propval.a.count + 1)*sizeof(uint16_t)); |
Linus Walleij | 545c779 | 2006-06-13 15:22:30 +0000 | [diff] [blame] | 2467 | for (i = 0; i < propval.a.count; i++) { |
Linus Walleij | 16571dc | 2006-08-17 20:27:46 +0000 | [diff] [blame] | 2468 | tmp[i] = propval.a.v[i].u16; |
| 2469 | // printf("%04x ", tmp[i]); |
Linus Walleij | 545c779 | 2006-06-13 15:22:30 +0000 | [diff] [blame] | 2470 | } |
Linus Walleij | 16571dc | 2006-08-17 20:27:46 +0000 | [diff] [blame] | 2471 | tmp[propval.a.count] = 0x0000U; |
Linus Walleij | 545c779 | 2006-06-13 15:22:30 +0000 | [diff] [blame] | 2472 | free(propval.a.v); |
| 2473 | |
Linus Walleij | 3ec8631 | 2006-08-21 13:25:24 +0000 | [diff] [blame] | 2474 | *unicstring = utf16_to_utf8(device, tmp); |
Linus Walleij | 16571dc | 2006-08-17 20:27:46 +0000 | [diff] [blame] | 2475 | |
Linus Walleij | 545c779 | 2006-06-13 15:22:30 +0000 | [diff] [blame] | 2476 | free(tmp); |
| 2477 | |
| 2478 | return 0; |
| 2479 | } |
| 2480 | |
| 2481 | /** |
| 2482 | * This function returns the secure time as an XML document string from |
| 2483 | * the device. |
| 2484 | * @param device a pointer to the device to get the secure time for. |
| 2485 | * @param sectime the secure time string as an XML document or NULL if the call |
| 2486 | * failed or the secure time property is not supported. This string |
| 2487 | * must be <code>free()</code>:ed by the caller after use. |
| 2488 | * @return 0 on success, any other value means failure. |
| 2489 | */ |
Linus Walleij | 8ab5426 | 2006-06-21 07:12:28 +0000 | [diff] [blame] | 2490 | int LIBMTP_Get_Secure_Time(LIBMTP_mtpdevice_t *device, char ** const sectime) |
Linus Walleij | 545c779 | 2006-06-13 15:22:30 +0000 | [diff] [blame] | 2491 | { |
| 2492 | return get_device_unicode_property(device, sectime, PTP_DPC_MTP_SecureTime); |
| 2493 | } |
| 2494 | |
| 2495 | /** |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 2496 | * This function returns the device (public key) certificate as an |
Linus Walleij | 545c779 | 2006-06-13 15:22:30 +0000 | [diff] [blame] | 2497 | * XML document string from the device. |
| 2498 | * @param device a pointer to the device to get the device certificate for. |
| 2499 | * @param devcert the device certificate as an XML string or NULL if the call |
| 2500 | * failed or the device certificate property is not supported. This |
| 2501 | * string must be <code>free()</code>:ed by the caller after use. |
| 2502 | * @return 0 on success, any other value means failure. |
| 2503 | */ |
Linus Walleij | 8ab5426 | 2006-06-21 07:12:28 +0000 | [diff] [blame] | 2504 | int LIBMTP_Get_Device_Certificate(LIBMTP_mtpdevice_t *device, char ** const devcert) |
Linus Walleij | 545c779 | 2006-06-13 15:22:30 +0000 | [diff] [blame] | 2505 | { |
| 2506 | return get_device_unicode_property(device, devcert, PTP_DPC_MTP_DeviceCertificate); |
| 2507 | } |
| 2508 | |
| 2509 | /** |
Linus Walleij | 8ab5426 | 2006-06-21 07:12:28 +0000 | [diff] [blame] | 2510 | * This function retrieves a list of supported file types, i.e. the file |
| 2511 | * types that this device claims it supports, e.g. audio file types that |
| 2512 | * the device can play etc. This list is mitigated to |
| 2513 | * inlcude the file types that libmtp can handle, i.e. it will not list |
| 2514 | * filetypes that libmtp will handle internally like playlists and folders. |
| 2515 | * @param device a pointer to the device to get the filetype capabilities for. |
| 2516 | * @param filetypes a pointer to a pointer that will hold the list of |
| 2517 | * supported filetypes if the call was successful. This list must |
| 2518 | * be <code>free()</code>:ed by the caller after use. |
| 2519 | * @param length a pointer to a variable that will hold the length of the |
| 2520 | * list of supported filetypes if the call was successful. |
| 2521 | * @return 0 on success, any other value means failure. |
| 2522 | * @see LIBMTP_Get_Filetype_Description() |
| 2523 | */ |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 2524 | 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] | 2525 | uint16_t * const length) |
| 2526 | { |
| 2527 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | a3544f6 | 2007-11-30 01:20:04 +0000 | [diff] [blame] | 2528 | PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo; |
Linus Walleij | 8ab5426 | 2006-06-21 07:12:28 +0000 | [diff] [blame] | 2529 | uint16_t *localtypes; |
| 2530 | uint16_t localtypelen; |
| 2531 | uint32_t i; |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 2532 | |
Linus Walleij | 8ab5426 | 2006-06-21 07:12:28 +0000 | [diff] [blame] | 2533 | // This is more memory than needed if there are unknown types, but what the heck. |
| 2534 | localtypes = (uint16_t *) malloc(params->deviceinfo.ImageFormats_len * sizeof(uint16_t)); |
| 2535 | localtypelen = 0; |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 2536 | |
Linus Walleij | 8ab5426 | 2006-06-21 07:12:28 +0000 | [diff] [blame] | 2537 | for (i=0;i<params->deviceinfo.ImageFormats_len;i++) { |
| 2538 | uint16_t localtype = map_ptp_type_to_libmtp_type(params->deviceinfo.ImageFormats[i]); |
| 2539 | if (localtype != LIBMTP_FILETYPE_UNKNOWN) { |
| 2540 | localtypes[localtypelen] = localtype; |
| 2541 | localtypelen++; |
| 2542 | } |
| 2543 | } |
Linus Walleij | a3544f6 | 2007-11-30 01:20:04 +0000 | [diff] [blame] | 2544 | // The forgotten Ogg support on YP-10 and others... |
Linus Walleij | fec4d56 | 2008-06-01 22:30:36 +0000 | [diff] [blame] | 2545 | if (FLAG_OGG_IS_UNKNOWN(ptp_usb)) { |
Linus Walleij | a3544f6 | 2007-11-30 01:20:04 +0000 | [diff] [blame] | 2546 | localtypes = (uint16_t *) realloc(localtypes, (params->deviceinfo.ImageFormats_len+1) * sizeof(uint16_t)); |
| 2547 | localtypes[localtypelen] = LIBMTP_FILETYPE_OGG; |
| 2548 | localtypelen++; |
| 2549 | } |
Linus Walleij | 8ab5426 | 2006-06-21 07:12:28 +0000 | [diff] [blame] | 2550 | |
| 2551 | *filetypes = localtypes; |
| 2552 | *length = localtypelen; |
| 2553 | |
| 2554 | return 0; |
| 2555 | } |
| 2556 | |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 2557 | /** |
Linus Walleij | 5b452bd | 2007-12-28 23:34:18 +0000 | [diff] [blame] | 2558 | * This function updates all the storage id's of a device and their |
| 2559 | * properties, then creates a linked list and puts the list head into |
Linus Walleij | f849191 | 2006-12-15 10:23:30 +0000 | [diff] [blame] | 2560 | * the device struct. It also optionally sorts this list. If you want |
| 2561 | * to display storage information in your application you should call |
Linus Walleij | 5b452bd | 2007-12-28 23:34:18 +0000 | [diff] [blame] | 2562 | * this function, then dereference the device struct |
| 2563 | * (<code>device->storage</code>) to get out information on the storage. |
| 2564 | * |
| 2565 | * You need to call this everytime you want to update the |
| 2566 | * <code>device->storage</code> list, for example anytime you need |
| 2567 | * to check available storage somewhere. |
| 2568 | * |
| 2569 | * <b>WARNING:</b> since this list is dynamically updated, do not |
| 2570 | * reference its fields in external applications by pointer! E.g |
| 2571 | * do not put a reference to any <code>char *</code> field. instead |
| 2572 | * <code>strncpy()</code> it! |
Linus Walleij | f849191 | 2006-12-15 10:23:30 +0000 | [diff] [blame] | 2573 | * |
Linus Walleij | e1b88e8 | 2008-07-02 20:12:53 +0000 | [diff] [blame] | 2574 | * @param device a pointer to the device to get the storage for. |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 2575 | * @param sortby an integer that determines the sorting of the storage list. |
| 2576 | * Valid sort methods are defined in libmtp.h with beginning with |
| 2577 | * LIBMTP_STORAGE_SORTBY_. 0 or LIBMTP_STORAGE_SORTBY_NOTSORTED to not |
| 2578 | * sort. |
| 2579 | * @return 0 on success, 1 success but only with storage id's, storage |
| 2580 | * properities could not be retrieved and -1 means failure. |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 2581 | */ |
| 2582 | int LIBMTP_Get_Storage(LIBMTP_mtpdevice_t *device, int const sortby) |
| 2583 | { |
| 2584 | uint32_t i = 0; |
| 2585 | PTPStorageInfo storageInfo; |
| 2586 | PTPParams *params = (PTPParams *) device->params; |
| 2587 | PTPStorageIDs storageIDs; |
Linus Walleij | e1ac07e | 2006-12-14 19:38:59 +0000 | [diff] [blame] | 2588 | LIBMTP_devicestorage_t *storage = NULL; |
| 2589 | LIBMTP_devicestorage_t *storageprev = NULL; |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 2590 | |
| 2591 | if (device->storage != NULL) |
| 2592 | free_storage_list(device); |
| 2593 | |
| 2594 | // if (!ptp_operation_issupported(params,PTP_OC_GetStorageIDs)) |
| 2595 | // return -1; |
Richard Low | bd14bf4 | 2007-07-21 11:25:11 +0000 | [diff] [blame] | 2596 | if (ptp_getstorageids (params, &storageIDs) != PTP_RC_OK) |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 2597 | return -1; |
| 2598 | if (storageIDs.n < 1) |
| 2599 | return -1; |
| 2600 | |
| 2601 | if (!ptp_operation_issupported(params,PTP_OC_GetStorageInfo)) { |
| 2602 | for (i = 0; i < storageIDs.n; i++) { |
| 2603 | |
Linus Walleij | e1ac07e | 2006-12-14 19:38:59 +0000 | [diff] [blame] | 2604 | storage = (LIBMTP_devicestorage_t *) malloc(sizeof(LIBMTP_devicestorage_t)); |
| 2605 | storage->prev = storageprev; |
| 2606 | if (storageprev != NULL) |
| 2607 | storageprev->next = storage; |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 2608 | if (device->storage == NULL) |
Linus Walleij | e1ac07e | 2006-12-14 19:38:59 +0000 | [diff] [blame] | 2609 | device->storage = storage; |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 2610 | |
Linus Walleij | e1ac07e | 2006-12-14 19:38:59 +0000 | [diff] [blame] | 2611 | storage->id = storageIDs.Storage[i]; |
| 2612 | storage->StorageType = PTP_ST_Undefined; |
| 2613 | storage->FilesystemType = PTP_FST_Undefined; |
| 2614 | storage->AccessCapability = PTP_AC_ReadWrite; |
| 2615 | storage->MaxCapacity = (uint64_t) -1; |
| 2616 | storage->FreeSpaceInBytes = (uint64_t) -1; |
| 2617 | storage->FreeSpaceInObjects = (uint64_t) -1; |
| 2618 | storage->StorageDescription = strdup("Unknown storage"); |
| 2619 | storage->VolumeIdentifier = strdup("Unknown volume"); |
| 2620 | storage->next = NULL; |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 2621 | |
Linus Walleij | e1ac07e | 2006-12-14 19:38:59 +0000 | [diff] [blame] | 2622 | storageprev = storage; |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 2623 | } |
| 2624 | free(storageIDs.Storage); |
| 2625 | return 1; |
| 2626 | } else { |
| 2627 | for (i = 0; i < storageIDs.n; i++) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 2628 | uint16_t ret; |
| 2629 | ret = ptp_getstorageinfo(params, storageIDs.Storage[i], &storageInfo); |
| 2630 | if (ret != PTP_RC_OK) { |
| 2631 | add_ptp_error_to_errorstack(device, ret, "LIBMTP_Get_Storage(): Could not get storage info."); |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 2632 | if (device->storage != NULL) { |
| 2633 | free_storage_list(device); |
| 2634 | } |
| 2635 | return -1; |
| 2636 | } |
| 2637 | |
Linus Walleij | e1ac07e | 2006-12-14 19:38:59 +0000 | [diff] [blame] | 2638 | storage = (LIBMTP_devicestorage_t *) malloc(sizeof(LIBMTP_devicestorage_t)); |
| 2639 | storage->prev = storageprev; |
| 2640 | if (storageprev != NULL) |
| 2641 | storageprev->next = storage; |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 2642 | if (device->storage == NULL) |
Linus Walleij | e1ac07e | 2006-12-14 19:38:59 +0000 | [diff] [blame] | 2643 | device->storage = storage; |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 2644 | |
Linus Walleij | e1ac07e | 2006-12-14 19:38:59 +0000 | [diff] [blame] | 2645 | storage->id = storageIDs.Storage[i]; |
| 2646 | storage->StorageType = storageInfo.StorageType; |
| 2647 | storage->FilesystemType = storageInfo.FilesystemType; |
| 2648 | storage->AccessCapability = storageInfo.AccessCapability; |
| 2649 | storage->MaxCapacity = storageInfo.MaxCapability; |
| 2650 | storage->FreeSpaceInBytes = storageInfo.FreeSpaceInBytes; |
| 2651 | storage->FreeSpaceInObjects = storageInfo.FreeSpaceInImages; |
| 2652 | storage->StorageDescription = storageInfo.StorageDescription; |
| 2653 | storage->VolumeIdentifier = storageInfo.VolumeLabel; |
| 2654 | storage->next = NULL; |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 2655 | |
Linus Walleij | e1ac07e | 2006-12-14 19:38:59 +0000 | [diff] [blame] | 2656 | storageprev = storage; |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 2657 | } |
| 2658 | |
Linus Walleij | e1ac07e | 2006-12-14 19:38:59 +0000 | [diff] [blame] | 2659 | storage->next = NULL; |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 2660 | |
| 2661 | sort_storage_by(device,sortby); |
| 2662 | free(storageIDs.Storage); |
| 2663 | return 0; |
| 2664 | } |
| 2665 | } |
| 2666 | |
| 2667 | /** |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 2668 | * This creates a new file metadata structure and allocates memory |
| 2669 | * for it. Notice that if you add strings to this structure they |
| 2670 | * will be freed by the corresponding <code>LIBMTP_destroy_file_t</code> |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 2671 | * operation later, so be careful of using strdup() when assigning |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 2672 | * strings, e.g.: |
| 2673 | * |
| 2674 | * <pre> |
| 2675 | * LIBMTP_file_t *file = LIBMTP_new_file_t(); |
| 2676 | * file->filename = strdup(namestr); |
| 2677 | * .... |
| 2678 | * LIBMTP_destroy_file_t(file); |
| 2679 | * </pre> |
| 2680 | * |
| 2681 | * @return a pointer to the newly allocated metadata structure. |
| 2682 | * @see LIBMTP_destroy_file_t() |
| 2683 | */ |
| 2684 | LIBMTP_file_t *LIBMTP_new_file_t(void) |
| 2685 | { |
| 2686 | LIBMTP_file_t *new = (LIBMTP_file_t *) malloc(sizeof(LIBMTP_file_t)); |
| 2687 | if (new == NULL) { |
| 2688 | return NULL; |
| 2689 | } |
| 2690 | new->filename = NULL; |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 2691 | new->item_id = 0; |
| 2692 | new->parent_id = 0; |
| 2693 | new->storage_id = 0; |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 2694 | new->filesize = 0; |
| 2695 | new->filetype = LIBMTP_FILETYPE_UNKNOWN; |
| 2696 | new->next = NULL; |
| 2697 | return new; |
| 2698 | } |
| 2699 | |
| 2700 | /** |
| 2701 | * This destroys a file metadata structure and deallocates the memory |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 2702 | * used by it, including any strings. Never use a file metadata |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 2703 | * structure again after calling this function on it. |
| 2704 | * @param file the file metadata to destroy. |
| 2705 | * @see LIBMTP_new_file_t() |
| 2706 | */ |
| 2707 | void LIBMTP_destroy_file_t(LIBMTP_file_t *file) |
| 2708 | { |
| 2709 | if (file == NULL) { |
| 2710 | return; |
| 2711 | } |
| 2712 | if (file->filename != NULL) |
| 2713 | free(file->filename); |
| 2714 | free(file); |
| 2715 | return; |
| 2716 | } |
| 2717 | |
| 2718 | /** |
mopoke | 3136444 | 2006-11-20 04:53:04 +0000 | [diff] [blame] | 2719 | * THIS FUNCTION IS DEPRECATED. PLEASE UPDATE YOUR CODE IN ORDER |
Richard Low | dc0b6c7 | 2006-11-13 09:22:23 +0000 | [diff] [blame] | 2720 | * NOT TO USE IT. |
| 2721 | * @see LIBMTP_Get_Filelisting_With_Callback() |
| 2722 | */ |
| 2723 | LIBMTP_file_t *LIBMTP_Get_Filelisting(LIBMTP_mtpdevice_t *device) |
| 2724 | { |
| 2725 | printf("WARNING: LIBMTP_Get_Filelisting() is deprecated.\n"); |
| 2726 | printf("WARNING: please update your code to use LIBMTP_Get_Filelisting_With_Callback()\n"); |
| 2727 | return LIBMTP_Get_Filelisting_With_Callback(device, NULL, NULL); |
| 2728 | } |
| 2729 | |
| 2730 | /** |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 2731 | * This returns a long list of all files available |
Linus Walleij | 25d5c21 | 2008-08-14 06:49:13 +0000 | [diff] [blame] | 2732 | * on the current MTP device. Folders will not be returned, but abstract |
| 2733 | * entities like playlists and albums will show up as "files". Typical usage: |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 2734 | * |
| 2735 | * <pre> |
| 2736 | * LIBMTP_file_t *filelist; |
| 2737 | * |
Richard Low | dc0b6c7 | 2006-11-13 09:22:23 +0000 | [diff] [blame] | 2738 | * filelist = LIBMTP_Get_Filelisting_With_Callback(device, callback, data); |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 2739 | * while (filelist != NULL) { |
| 2740 | * LIBMTP_file_t *tmp; |
| 2741 | * |
| 2742 | * // Do something on each element in the list here... |
| 2743 | * tmp = filelist; |
| 2744 | * filelist = filelist->next; |
| 2745 | * LIBMTP_destroy_file_t(tmp); |
| 2746 | * } |
| 2747 | * </pre> |
| 2748 | * |
Linus Walleij | 25d5c21 | 2008-08-14 06:49:13 +0000 | [diff] [blame] | 2749 | * If you want to group your file listing by storage (per storage unit) or |
| 2750 | * arrange files into folders, you must dereference the <code>storage_id</code> |
| 2751 | * and/or <code>parent_id</code> field of the returned <code>LIBMTP_file_t</code> |
| 2752 | * struct. To arrange by folders or files you typically have to create the proper |
| 2753 | * trees by calls to <code>LIBMTP_Get_Storage()</code> and/or |
| 2754 | * <code>LIBMTP_Get_Folder_List()</code> first. |
| 2755 | * |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 2756 | * @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] | 2757 | * @param callback a function to be called during the tracklisting retrieveal |
Linus Walleij | 25d5c21 | 2008-08-14 06:49:13 +0000 | [diff] [blame] | 2758 | * for displaying progress bars etc, or NULL if you don't want |
| 2759 | * any callbacks. |
Richard Low | dc0b6c7 | 2006-11-13 09:22:23 +0000 | [diff] [blame] | 2760 | * @param data a user-defined pointer that is passed along to |
Linus Walleij | 25d5c21 | 2008-08-14 06:49:13 +0000 | [diff] [blame] | 2761 | * the <code>progress</code> function in order to |
| 2762 | * pass along some user defined data to the progress |
| 2763 | * updates. If not used, set this to NULL. |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 2764 | * @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] | 2765 | * field of the <code>LIBMTP_file_t</code> data structure. |
| 2766 | * Each of the metadata tags must be freed after use, and may |
| 2767 | * contain only partial metadata information, i.e. one or several |
| 2768 | * fields may be NULL or 0. |
Linus Walleij | 2e4b5f9 | 2006-06-16 14:00:49 +0000 | [diff] [blame] | 2769 | * @see LIBMTP_Get_Filemetadata() |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 2770 | */ |
Richard Low | dc0b6c7 | 2006-11-13 09:22:23 +0000 | [diff] [blame] | 2771 | LIBMTP_file_t *LIBMTP_Get_Filelisting_With_Callback(LIBMTP_mtpdevice_t *device, |
| 2772 | LIBMTP_progressfunc_t const callback, |
| 2773 | void const * const data) |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 2774 | { |
| 2775 | uint32_t i = 0; |
| 2776 | LIBMTP_file_t *retfiles = NULL; |
| 2777 | LIBMTP_file_t *curfile = NULL; |
| 2778 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | d9d28d5 | 2007-08-04 19:01:18 +0000 | [diff] [blame] | 2779 | PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo; |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 2780 | uint16_t ret; |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 2781 | |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 2782 | // Get all the handles if we haven't already done that |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 2783 | if (params->handles.Handler == NULL) { |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 2784 | flush_handles(device); |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 2785 | } |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 2786 | |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 2787 | for (i = 0; i < params->handles.n; i++) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 2788 | LIBMTP_file_t *file; |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 2789 | PTPObjectInfo *oi; |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 2790 | |
Richard Low | dc0b6c7 | 2006-11-13 09:22:23 +0000 | [diff] [blame] | 2791 | if (callback != NULL) |
| 2792 | callback(i, params->handles.n, data); |
mopoke | 3136444 | 2006-11-20 04:53:04 +0000 | [diff] [blame] | 2793 | |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 2794 | oi = ¶ms->objectinfo[i]; |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 2795 | |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 2796 | if (oi->ObjectFormat == PTP_OFC_Association) { |
Linus Walleij | d9d28d5 | 2007-08-04 19:01:18 +0000 | [diff] [blame] | 2797 | // MTP use this object format for folders which means |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 2798 | // these "files" will turn up on a folder listing instead. |
| 2799 | continue; |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 2800 | } |
| 2801 | |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 2802 | // Allocate a new file type |
| 2803 | file = LIBMTP_new_file_t(); |
| 2804 | |
| 2805 | file->parent_id = oi->ParentObject; |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 2806 | file->storage_id = oi->StorageID; |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 2807 | |
Linus Walleij | d9d28d5 | 2007-08-04 19:01:18 +0000 | [diff] [blame] | 2808 | // This is some sort of unique ID so we can keep track of the track. |
| 2809 | file->item_id = params->handles.Handler[i]; |
| 2810 | |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 2811 | // Set the filetype |
| 2812 | file->filetype = map_ptp_type_to_libmtp_type(oi->ObjectFormat); |
| 2813 | |
| 2814 | // Original file-specific properties |
Linus Walleij | d9d28d5 | 2007-08-04 19:01:18 +0000 | [diff] [blame] | 2815 | // We only have 32-bit file size here; if we find it, we use the |
| 2816 | // PTP_OPC_ObjectSize property which has 64bit precision. |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 2817 | file->filesize = oi->ObjectCompressedSize; |
| 2818 | if (oi->Filename != NULL) { |
| 2819 | file->filename = strdup(oi->Filename); |
| 2820 | } |
| 2821 | |
Linus Walleij | d9d28d5 | 2007-08-04 19:01:18 +0000 | [diff] [blame] | 2822 | /* |
| 2823 | * If we have a cached, large set of metadata, then use it! |
| 2824 | */ |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 2825 | if (params->props) { |
| 2826 | MTPProperties *prop = params->props; |
| 2827 | int i; |
Linus Walleij | d9d28d5 | 2007-08-04 19:01:18 +0000 | [diff] [blame] | 2828 | |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 2829 | for (i=0;(i<params->nrofprops) && (prop->ObjectHandle != file->item_id);i++,prop++) |
| 2830 | /*empty*/; |
| 2831 | for (;i<params->nrofprops;i++) { |
| 2832 | if (prop->ObjectHandle != file->item_id) |
| 2833 | break; |
| 2834 | |
Linus Walleij | d9d28d5 | 2007-08-04 19:01:18 +0000 | [diff] [blame] | 2835 | // Pick ObjectSize here... |
| 2836 | if (prop->property == PTP_OPC_ObjectSize) { |
Linus Walleij | ddaba2f | 2007-10-02 21:20:30 +0000 | [diff] [blame] | 2837 | if (device->object_bitsize == 64) { |
Linus Walleij | ca2a170 | 2007-10-02 20:41:45 +0000 | [diff] [blame] | 2838 | file->filesize = prop->propval.u64; |
| 2839 | } else { |
Linus Walleij | ddaba2f | 2007-10-02 21:20:30 +0000 | [diff] [blame] | 2840 | file->filesize = prop->propval.u32; |
Linus Walleij | ca2a170 | 2007-10-02 20:41:45 +0000 | [diff] [blame] | 2841 | } |
Linus Walleij | d9d28d5 | 2007-08-04 19:01:18 +0000 | [diff] [blame] | 2842 | break; |
| 2843 | } |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 2844 | prop ++; |
Linus Walleij | d9d28d5 | 2007-08-04 19:01:18 +0000 | [diff] [blame] | 2845 | } |
| 2846 | } else if (ptp_operation_issupported(params,PTP_OC_MTP_GetObjPropList) |
Linus Walleij | fec4d56 | 2008-06-01 22:30:36 +0000 | [diff] [blame] | 2847 | && !FLAG_BROKEN_MTPGETOBJPROPLIST(ptp_usb)) { |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 2848 | MTPProperties *props = NULL; |
| 2849 | MTPProperties *prop; |
Linus Walleij | ca2a170 | 2007-10-02 20:41:45 +0000 | [diff] [blame] | 2850 | |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 2851 | int nrofprops; |
Linus Walleij | d9d28d5 | 2007-08-04 19:01:18 +0000 | [diff] [blame] | 2852 | |
| 2853 | /* |
| 2854 | * This should retrieve all properties for an object, but on devices |
| 2855 | * which are inherently broken it will not, so these need the |
| 2856 | * special flag DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST. |
| 2857 | */ |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 2858 | ret = ptp_mtp_getobjectproplist(params, file->item_id, &props, &nrofprops); |
Linus Walleij | d9d28d5 | 2007-08-04 19:01:18 +0000 | [diff] [blame] | 2859 | if (ret != PTP_RC_OK) { |
| 2860 | add_ptp_error_to_errorstack(device, ret, "LIBMTP_Get_Filelisting_With_Callback(): call to ptp_mtp_getobjectproplist() failed."); |
| 2861 | // Silently fall through. |
| 2862 | } |
Linus Walleij | fcb4342 | 2008-05-23 21:53:55 +0000 | [diff] [blame] | 2863 | if (props == NULL && nrofprops != 0) { |
| 2864 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, |
| 2865 | "LIBMTP_Get_Filelisting_With_Callback: " |
| 2866 | "call to ptp_mtp_getobjectproplist() returned " |
| 2867 | "inconsistent results."); |
| 2868 | nrofprops = 0; |
| 2869 | } |
Richard Low | d7bcab5 | 2007-09-16 16:30:20 +0000 | [diff] [blame] | 2870 | if (props != NULL) { |
| 2871 | int i; |
| 2872 | prop = props; |
| 2873 | for (i=0;i<nrofprops;i++) { |
| 2874 | if (prop->ObjectHandle != file->item_id) |
| 2875 | break; |
| 2876 | // Pick ObjectSize here... |
| 2877 | if (prop->property == PTP_OPC_ObjectSize) { |
Linus Walleij | ddaba2f | 2007-10-02 21:20:30 +0000 | [diff] [blame] | 2878 | if (device->object_bitsize == 64) { |
Linus Walleij | ca2a170 | 2007-10-02 20:41:45 +0000 | [diff] [blame] | 2879 | file->filesize = prop->propval.u64; |
| 2880 | } else { |
Linus Walleij | ddaba2f | 2007-10-02 21:20:30 +0000 | [diff] [blame] | 2881 | file->filesize = prop->propval.u32; |
Linus Walleij | ca2a170 | 2007-10-02 20:41:45 +0000 | [diff] [blame] | 2882 | } |
Richard Low | d7bcab5 | 2007-09-16 16:30:20 +0000 | [diff] [blame] | 2883 | break; |
| 2884 | } |
| 2885 | prop ++; |
| 2886 | } |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 2887 | ptp_destroy_object_prop_list(props, nrofprops); |
Linus Walleij | d9d28d5 | 2007-08-04 19:01:18 +0000 | [diff] [blame] | 2888 | } |
Linus Walleij | d9d28d5 | 2007-08-04 19:01:18 +0000 | [diff] [blame] | 2889 | } else { |
| 2890 | uint16_t *props = NULL; |
| 2891 | uint32_t propcnt = 0; |
| 2892 | |
| 2893 | // First see which properties can be retrieved for this object format |
Linus Walleij | ca2a170 | 2007-10-02 20:41:45 +0000 | [diff] [blame] | 2894 | ret = ptp_mtp_getobjectpropssupported(params, oi->ObjectFormat, &propcnt, &props); |
Linus Walleij | d9d28d5 | 2007-08-04 19:01:18 +0000 | [diff] [blame] | 2895 | if (ret != PTP_RC_OK) { |
| 2896 | add_ptp_error_to_errorstack(device, ret, "LIBMTP_Get_Filelisting_With_Callback(): call to ptp_mtp_getobjectpropssupported() failed."); |
| 2897 | // Silently fall through. |
| 2898 | } else { |
Richard Low | d7bcab5 | 2007-09-16 16:30:20 +0000 | [diff] [blame] | 2899 | int i; |
Linus Walleij | d9d28d5 | 2007-08-04 19:01:18 +0000 | [diff] [blame] | 2900 | for (i=0;i<propcnt;i++) { |
| 2901 | switch (props[i]) { |
| 2902 | case PTP_OPC_ObjectSize: |
Linus Walleij | ddaba2f | 2007-10-02 21:20:30 +0000 | [diff] [blame] | 2903 | if (device->object_bitsize == 64) { |
| 2904 | file->filesize = get_u64_from_object(device, file->item_id, PTP_OPC_ObjectSize, 0); |
| 2905 | } else { |
| 2906 | file->filesize = get_u32_from_object(device, file->item_id, PTP_OPC_ObjectSize, 0); |
Linus Walleij | ca2a170 | 2007-10-02 20:41:45 +0000 | [diff] [blame] | 2907 | } |
Linus Walleij | d9d28d5 | 2007-08-04 19:01:18 +0000 | [diff] [blame] | 2908 | break; |
| 2909 | default: |
| 2910 | break; |
| 2911 | } |
| 2912 | } |
| 2913 | free(props); |
| 2914 | } |
| 2915 | } |
Linus Walleij | d9d28d5 | 2007-08-04 19:01:18 +0000 | [diff] [blame] | 2916 | |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 2917 | // Add track to a list that will be returned afterwards. |
| 2918 | if (retfiles == NULL) { |
| 2919 | retfiles = file; |
| 2920 | curfile = file; |
| 2921 | } else { |
| 2922 | curfile->next = file; |
| 2923 | curfile = file; |
| 2924 | } |
| 2925 | |
| 2926 | // Call listing callback |
| 2927 | // double progressPercent = (double)i*(double)100.0 / (double)params->handles.n; |
| 2928 | |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 2929 | } // Handle counting loop |
| 2930 | return retfiles; |
| 2931 | } |
| 2932 | |
| 2933 | /** |
Linus Walleij | 2e4b5f9 | 2006-06-16 14:00:49 +0000 | [diff] [blame] | 2934 | * This function retrieves the metadata for a single file off |
| 2935 | * the device. |
| 2936 | * |
| 2937 | * Do not call this function repeatedly! The file handles are linearly |
| 2938 | * searched O(n) and the call may involve (slow) USB traffic, so use |
| 2939 | * <code>LIBMTP_Get_Filelisting()</code> and cache the file, preferably |
| 2940 | * as an efficient data structure such as a hash list. |
| 2941 | * |
Linus Walleij | 0801cd5 | 2008-01-29 22:19:15 +0000 | [diff] [blame] | 2942 | * Incidentally this function will return metadata for |
| 2943 | * a folder (association) as well, but this is not a proper use |
| 2944 | * of it, it is intended for file manipulation, not folder manipulation. |
| 2945 | * |
Linus Walleij | 2e4b5f9 | 2006-06-16 14:00:49 +0000 | [diff] [blame] | 2946 | * @param device a pointer to the device to get the file metadata from. |
| 2947 | * @param fileid the object ID of the file that you want the metadata for. |
| 2948 | * @return a metadata entry on success or NULL on failure. |
| 2949 | * @see LIBMTP_Get_Filelisting() |
| 2950 | */ |
| 2951 | LIBMTP_file_t *LIBMTP_Get_Filemetadata(LIBMTP_mtpdevice_t *device, uint32_t const fileid) |
| 2952 | { |
| 2953 | uint32_t i = 0; |
| 2954 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 2955 | PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo; |
| 2956 | uint16_t ret; |
Linus Walleij | 2e4b5f9 | 2006-06-16 14:00:49 +0000 | [diff] [blame] | 2957 | |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 2958 | // Get all the handles if we haven't already done that |
Linus Walleij | 2e4b5f9 | 2006-06-16 14:00:49 +0000 | [diff] [blame] | 2959 | if (params->handles.Handler == NULL) { |
| 2960 | flush_handles(device); |
| 2961 | } |
| 2962 | |
| 2963 | for (i = 0; i < params->handles.n; i++) { |
| 2964 | LIBMTP_file_t *file; |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 2965 | PTPObjectInfo *oi; |
Linus Walleij | 2e4b5f9 | 2006-06-16 14:00:49 +0000 | [diff] [blame] | 2966 | |
| 2967 | // Is this the file we're looking for? |
| 2968 | if (params->handles.Handler[i] != fileid) { |
| 2969 | continue; |
| 2970 | } |
| 2971 | |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 2972 | oi = ¶ms->objectinfo[i]; |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 2973 | |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 2974 | // Allocate a new file type |
| 2975 | file = LIBMTP_new_file_t(); |
| 2976 | |
| 2977 | file->parent_id = oi->ParentObject; |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 2978 | file->storage_id = oi->StorageID; |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 2979 | |
| 2980 | // Set the filetype |
| 2981 | file->filetype = map_ptp_type_to_libmtp_type(oi->ObjectFormat); |
| 2982 | |
| 2983 | // Original file-specific properties |
Richard Low | c3f5fc3 | 2007-07-29 09:40:50 +0000 | [diff] [blame] | 2984 | |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 2985 | // We only have 32-bit file size here; later we use the PTP_OPC_ObjectSize property |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 2986 | file->filesize = oi->ObjectCompressedSize; |
| 2987 | if (oi->Filename != NULL) { |
| 2988 | file->filename = strdup(oi->Filename); |
| 2989 | } |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 2990 | |
| 2991 | // This is some sort of unique ID so we can keep track of the file. |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 2992 | file->item_id = params->handles.Handler[i]; |
| 2993 | |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 2994 | /* |
| 2995 | * If we have a cached, large set of metadata, then use it! |
| 2996 | */ |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 2997 | if (params->props) { |
| 2998 | MTPProperties *prop = params->props; |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 2999 | |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 3000 | for (i=0;(i<params->nrofprops) && (prop->ObjectHandle != file->item_id);i++,prop++) |
| 3001 | /*empty*/; |
| 3002 | for (;(i<params->nrofprops) && (prop->ObjectHandle == file->item_id);i++,prop++) { |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 3003 | // Pick ObjectSize here... |
| 3004 | if (prop->property == PTP_OPC_ObjectSize) { |
| 3005 | // This may already be set, but this 64bit precision value |
| 3006 | // is better than the PTP 32bit value, so let it override. |
Linus Walleij | ddaba2f | 2007-10-02 21:20:30 +0000 | [diff] [blame] | 3007 | if (device->object_bitsize == 64) { |
| 3008 | file->filesize = prop->propval.u64; |
| 3009 | } else { |
| 3010 | file->filesize = prop->propval.u32; |
| 3011 | } |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 3012 | break; |
| 3013 | } |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 3014 | } |
| 3015 | } else if (ptp_operation_issupported(params,PTP_OC_MTP_GetObjPropList) |
Linus Walleij | fec4d56 | 2008-06-01 22:30:36 +0000 | [diff] [blame] | 3016 | && !FLAG_BROKEN_MTPGETOBJPROPLIST(ptp_usb)) { |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 3017 | MTPProperties *props = NULL; |
| 3018 | MTPProperties *prop; |
| 3019 | int nrofprops; |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 3020 | |
| 3021 | /* |
| 3022 | * This should retrieve all properties for an object, but on devices |
| 3023 | * which are inherently broken it will not, so these need the |
| 3024 | * special flag DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST. |
| 3025 | */ |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 3026 | ret = ptp_mtp_getobjectproplist(params, file->item_id, &props, &nrofprops); |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 3027 | if (ret != PTP_RC_OK) { |
Linus Walleij | fcb4342 | 2008-05-23 21:53:55 +0000 | [diff] [blame] | 3028 | add_ptp_error_to_errorstack(device, ret, "LIBMTP_Get_Filelisting_With_Callback(): " |
| 3029 | "call to ptp_mtp_getobjectproplist() failed."); |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 3030 | // Silently fall through. |
| 3031 | } |
Linus Walleij | fcb4342 | 2008-05-23 21:53:55 +0000 | [diff] [blame] | 3032 | if (props == NULL && nrofprops != 0) { |
| 3033 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, |
| 3034 | "LIBMTP_Get_Filelisting_With_Callback(): " |
| 3035 | "call to ptp_mtp_getobjectproplist() returned " |
| 3036 | "inconsistent results."); |
| 3037 | return NULL; |
| 3038 | } |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 3039 | prop = props; |
| 3040 | for (i=0;i<nrofprops;i++) { |
| 3041 | if ((prop->ObjectHandle == file->item_id) && (prop->property == PTP_OPC_ObjectSize)) { |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 3042 | // This may already be set, but this 64bit precision value |
| 3043 | // is better than the PTP 32bit value, so let it override. |
Linus Walleij | ddaba2f | 2007-10-02 21:20:30 +0000 | [diff] [blame] | 3044 | if (device->object_bitsize == 64) { |
| 3045 | file->filesize = prop->propval.u64; |
| 3046 | } else { |
| 3047 | file->filesize = prop->propval.u32; |
| 3048 | } |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 3049 | break; |
| 3050 | } |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 3051 | prop ++; |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 3052 | } |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 3053 | ptp_destroy_object_prop_list(props, nrofprops); |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 3054 | } else { |
| 3055 | uint16_t *props = NULL; |
| 3056 | uint32_t propcnt = 0; |
| 3057 | |
| 3058 | // First see which properties can be retrieved for this object format |
| 3059 | ret = ptp_mtp_getobjectpropssupported(params, map_libmtp_type_to_ptp_type(file->filetype), &propcnt, &props); |
| 3060 | if (ret != PTP_RC_OK) { |
| 3061 | add_ptp_error_to_errorstack(device, ret, "LIBMTP_Get_Filemetadata(): call to ptp_mtp_getobjectpropssupported() failed."); |
| 3062 | // Silently fall through. |
| 3063 | } else { |
| 3064 | for (i=0;i<propcnt;i++) { |
| 3065 | switch (props[i]) { |
| 3066 | case PTP_OPC_ObjectSize: |
Linus Walleij | ddaba2f | 2007-10-02 21:20:30 +0000 | [diff] [blame] | 3067 | if (device->object_bitsize == 64) { |
| 3068 | file->filesize = get_u64_from_object(device, file->item_id, PTP_OPC_ObjectSize, 0); |
| 3069 | } else { |
| 3070 | file->filesize = get_u32_from_object(device, file->item_id, PTP_OPC_ObjectSize, 0); |
| 3071 | } |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 3072 | break; |
| 3073 | default: |
| 3074 | break; |
| 3075 | } |
| 3076 | } |
| 3077 | free(props); |
| 3078 | } |
| 3079 | } |
| 3080 | |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 3081 | return file; |
| 3082 | |
Linus Walleij | 2e4b5f9 | 2006-06-16 14:00:49 +0000 | [diff] [blame] | 3083 | } |
| 3084 | return NULL; |
| 3085 | } |
| 3086 | |
| 3087 | /** |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 3088 | * This creates a new track metadata structure and allocates memory |
| 3089 | * for it. Notice that if you add strings to this structure they |
| 3090 | * will be freed by the corresponding <code>LIBMTP_destroy_track_t</code> |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 3091 | * operation later, so be careful of using strdup() when assigning |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 3092 | * strings, e.g.: |
| 3093 | * |
Linus Walleij | 17e39f7 | 2006-02-23 15:54:28 +0000 | [diff] [blame] | 3094 | * <pre> |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 3095 | * LIBMTP_track_t *track = LIBMTP_new_track_t(); |
| 3096 | * track->title = strdup(titlestr); |
| 3097 | * .... |
| 3098 | * LIBMTP_destroy_track_t(track); |
Linus Walleij | 17e39f7 | 2006-02-23 15:54:28 +0000 | [diff] [blame] | 3099 | * </pre> |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 3100 | * |
| 3101 | * @return a pointer to the newly allocated metadata structure. |
| 3102 | * @see LIBMTP_destroy_track_t() |
| 3103 | */ |
| 3104 | LIBMTP_track_t *LIBMTP_new_track_t(void) |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 3105 | { |
| 3106 | LIBMTP_track_t *new = (LIBMTP_track_t *) malloc(sizeof(LIBMTP_track_t)); |
| 3107 | if (new == NULL) { |
| 3108 | return NULL; |
| 3109 | } |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 3110 | new->item_id = 0; |
| 3111 | new->parent_id = 0; |
| 3112 | new->storage_id = 0; |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 3113 | new->title = NULL; |
| 3114 | new->artist = NULL; |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 3115 | new->composer = NULL; |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 3116 | new->album = NULL; |
| 3117 | new->genre = NULL; |
| 3118 | new->date = NULL; |
| 3119 | new->filename = NULL; |
| 3120 | new->duration = 0; |
| 3121 | new->tracknumber = 0; |
| 3122 | new->filesize = 0; |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 3123 | new->filetype = LIBMTP_FILETYPE_UNKNOWN; |
Linus Walleij | cf223e6 | 2006-06-19 09:31:53 +0000 | [diff] [blame] | 3124 | new->samplerate = 0; |
| 3125 | new->nochannels = 0; |
| 3126 | new->wavecodec = 0; |
| 3127 | new->bitrate = 0; |
| 3128 | new->bitratetype = 0; |
| 3129 | new->rating = 0; |
| 3130 | new->usecount = 0; |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 3131 | new->next = NULL; |
| 3132 | return new; |
| 3133 | } |
| 3134 | |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 3135 | /** |
| 3136 | * This destroys a track metadata structure and deallocates the memory |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 3137 | * used by it, including any strings. Never use a track metadata |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 3138 | * structure again after calling this function on it. |
| 3139 | * @param track the track metadata to destroy. |
| 3140 | * @see LIBMTP_new_track_t() |
| 3141 | */ |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 3142 | void LIBMTP_destroy_track_t(LIBMTP_track_t *track) |
| 3143 | { |
| 3144 | if (track == NULL) { |
| 3145 | return; |
| 3146 | } |
| 3147 | if (track->title != NULL) |
| 3148 | free(track->title); |
| 3149 | if (track->artist != NULL) |
| 3150 | free(track->artist); |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 3151 | if (track->composer != NULL) |
| 3152 | free(track->composer); |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 3153 | if (track->album != NULL) |
| 3154 | free(track->album); |
| 3155 | if (track->genre != NULL) |
| 3156 | free(track->genre); |
| 3157 | if (track->date != NULL) |
| 3158 | free(track->date); |
| 3159 | if (track->filename != NULL) |
| 3160 | free(track->filename); |
| 3161 | free(track); |
| 3162 | return; |
| 3163 | } |
| 3164 | |
| 3165 | /** |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 3166 | * This function maps and copies a property onto the track metadata if applicable. |
| 3167 | */ |
Linus Walleij | ddaba2f | 2007-10-02 21:20:30 +0000 | [diff] [blame] | 3168 | 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] | 3169 | { |
| 3170 | switch (prop->property) { |
| 3171 | case PTP_OPC_Name: |
| 3172 | if (prop->propval.str != NULL) |
| 3173 | track->title = strdup(prop->propval.str); |
| 3174 | else |
| 3175 | track->title = NULL; |
| 3176 | break; |
| 3177 | case PTP_OPC_Artist: |
| 3178 | if (prop->propval.str != NULL) |
| 3179 | track->artist = strdup(prop->propval.str); |
| 3180 | else |
| 3181 | track->artist = NULL; |
| 3182 | break; |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 3183 | case PTP_OPC_Composer: |
| 3184 | if (prop->propval.str != NULL) |
| 3185 | track->composer = strdup(prop->propval.str); |
| 3186 | else |
| 3187 | track->composer = NULL; |
| 3188 | break; |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 3189 | case PTP_OPC_Duration: |
| 3190 | track->duration = prop->propval.u32; |
| 3191 | break; |
| 3192 | case PTP_OPC_Track: |
| 3193 | track->tracknumber = prop->propval.u16; |
| 3194 | break; |
| 3195 | case PTP_OPC_Genre: |
| 3196 | if (prop->propval.str != NULL) |
| 3197 | track->genre = strdup(prop->propval.str); |
| 3198 | else |
| 3199 | track->genre = NULL; |
| 3200 | break; |
| 3201 | case PTP_OPC_AlbumName: |
| 3202 | if (prop->propval.str != NULL) |
| 3203 | track->album = strdup(prop->propval.str); |
| 3204 | else |
| 3205 | track->album = NULL; |
| 3206 | break; |
| 3207 | case PTP_OPC_OriginalReleaseDate: |
| 3208 | if (prop->propval.str != NULL) |
| 3209 | track->date = strdup(prop->propval.str); |
| 3210 | else |
| 3211 | track->date = NULL; |
| 3212 | break; |
| 3213 | // These are, well not so important. |
| 3214 | case PTP_OPC_SampleRate: |
| 3215 | track->samplerate = prop->propval.u32; |
| 3216 | break; |
| 3217 | case PTP_OPC_NumberOfChannels: |
| 3218 | track->nochannels = prop->propval.u16; |
| 3219 | break; |
| 3220 | case PTP_OPC_AudioWAVECodec: |
| 3221 | track->wavecodec = prop->propval.u32; |
| 3222 | break; |
| 3223 | case PTP_OPC_AudioBitRate: |
| 3224 | track->bitrate = prop->propval.u32; |
| 3225 | break; |
| 3226 | case PTP_OPC_BitRateType: |
| 3227 | track->bitratetype = prop->propval.u16; |
| 3228 | break; |
| 3229 | case PTP_OPC_Rating: |
| 3230 | track->rating = prop->propval.u16; |
| 3231 | break; |
| 3232 | case PTP_OPC_UseCount: |
| 3233 | track->usecount = prop->propval.u32; |
| 3234 | break; |
Linus Walleij | d9d28d5 | 2007-08-04 19:01:18 +0000 | [diff] [blame] | 3235 | case PTP_OPC_ObjectSize: |
Linus Walleij | ddaba2f | 2007-10-02 21:20:30 +0000 | [diff] [blame] | 3236 | if (device->object_bitsize == 64) { |
| 3237 | track->filesize = prop->propval.u64; |
| 3238 | } else { |
| 3239 | track->filesize = prop->propval.u32; |
Linus Walleij | ca2a170 | 2007-10-02 20:41:45 +0000 | [diff] [blame] | 3240 | } |
Linus Walleij | d9d28d5 | 2007-08-04 19:01:18 +0000 | [diff] [blame] | 3241 | break; |
| 3242 | default: |
| 3243 | break; |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 3244 | } |
| 3245 | } |
| 3246 | |
| 3247 | /** |
Linus Walleij | 8ab5426 | 2006-06-21 07:12:28 +0000 | [diff] [blame] | 3248 | * This function retrieves the track metadata for a track |
| 3249 | * given by a unique ID. |
| 3250 | * @param device a pointer to the device to get the track metadata off. |
| 3251 | * @param trackid the unique ID of the track. |
| 3252 | * @param objectformat the object format of this track, so we know what it supports. |
| 3253 | * @param track a metadata set to fill in. |
| 3254 | */ |
| 3255 | static void get_track_metadata(LIBMTP_mtpdevice_t *device, uint16_t objectformat, |
| 3256 | LIBMTP_track_t *track) |
| 3257 | { |
Linus Walleij | 00cf064 | 2006-07-26 20:40:59 +0000 | [diff] [blame] | 3258 | uint16_t ret; |
| 3259 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | 1a1b2d1 | 2006-11-23 13:32:17 +0000 | [diff] [blame] | 3260 | PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo; |
Linus Walleij | 00cf064 | 2006-07-26 20:40:59 +0000 | [diff] [blame] | 3261 | uint32_t i; |
Linus Walleij | 00cf064 | 2006-07-26 20:40:59 +0000 | [diff] [blame] | 3262 | |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 3263 | /* |
| 3264 | * If we have a cached, large set of metadata, then use it! |
| 3265 | */ |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 3266 | if (params->props) { |
| 3267 | MTPProperties *prop = params->props; |
| 3268 | |
| 3269 | for (i=0;(i<params->nrofprops) && (prop->ObjectHandle != track->item_id);i++,prop++) |
| 3270 | /*empty*/; |
| 3271 | for (i=0;(i<params->nrofprops) && (prop->ObjectHandle == track->item_id);i++,prop++) { |
Linus Walleij | ddaba2f | 2007-10-02 21:20:30 +0000 | [diff] [blame] | 3272 | pick_property_to_track_metadata(device, prop, track); |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 3273 | } |
| 3274 | } else if (ptp_operation_issupported(params,PTP_OC_MTP_GetObjPropList) |
Linus Walleij | fec4d56 | 2008-06-01 22:30:36 +0000 | [diff] [blame] | 3275 | && !FLAG_BROKEN_MTPGETOBJPROPLIST(ptp_usb)) { |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 3276 | MTPProperties *props = NULL; |
| 3277 | MTPProperties *prop; |
| 3278 | int nrofprops; |
mopoke | 3136444 | 2006-11-20 04:53:04 +0000 | [diff] [blame] | 3279 | |
Linus Walleij | 1a1b2d1 | 2006-11-23 13:32:17 +0000 | [diff] [blame] | 3280 | /* |
| 3281 | * This should retrieve all properties for an object, but on devices |
| 3282 | * which are inherently broken it will not, so these need the |
| 3283 | * special flag DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST. |
| 3284 | */ |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 3285 | ret = ptp_mtp_getobjectproplist(params, track->item_id, &props, &nrofprops); |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 3286 | if (ret != PTP_RC_OK) { |
| 3287 | add_ptp_error_to_errorstack(device, ret, "get_track_metadata(): call to ptp_mtp_getobjectproplist() failed."); |
| 3288 | return; |
| 3289 | } |
Linus Walleij | fcb4342 | 2008-05-23 21:53:55 +0000 | [diff] [blame] | 3290 | if (props == NULL && nrofprops != 0) { |
| 3291 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, |
| 3292 | "get_track_metadata(): " |
| 3293 | "call to ptp_mtp_getobjectproplist() returned " |
| 3294 | "inconsistent results."); |
| 3295 | return; |
| 3296 | } |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 3297 | prop = props; |
Richard Low | d7bcab5 | 2007-09-16 16:30:20 +0000 | [diff] [blame] | 3298 | for (i=0;i<nrofprops;i++,prop++) { |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 3299 | if (prop->ObjectHandle == track->item_id) |
Linus Walleij | ddaba2f | 2007-10-02 21:20:30 +0000 | [diff] [blame] | 3300 | pick_property_to_track_metadata(device, prop, track); |
Linus Walleij | 00cf064 | 2006-07-26 20:40:59 +0000 | [diff] [blame] | 3301 | } |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 3302 | ptp_destroy_object_prop_list(props, nrofprops); |
Linus Walleij | 3fcfea5 | 2006-11-13 07:07:36 +0000 | [diff] [blame] | 3303 | } else { |
Linus Walleij | 3fcfea5 | 2006-11-13 07:07:36 +0000 | [diff] [blame] | 3304 | uint16_t *props = NULL; |
| 3305 | uint32_t propcnt = 0; |
| 3306 | |
| 3307 | // First see which properties can be retrieved for this object format |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 3308 | 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] | 3309 | if (ret != PTP_RC_OK) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 3310 | 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] | 3311 | // Just bail out for now, nothing is ever set. |
| 3312 | return; |
| 3313 | } else { |
| 3314 | for (i=0;i<propcnt;i++) { |
| 3315 | switch (props[i]) { |
| 3316 | case PTP_OPC_Name: |
Linus Walleij | 9901e22 | 2006-11-30 12:28:19 +0000 | [diff] [blame] | 3317 | 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] | 3318 | break; |
| 3319 | case PTP_OPC_Artist: |
Linus Walleij | 9901e22 | 2006-11-30 12:28:19 +0000 | [diff] [blame] | 3320 | 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] | 3321 | break; |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 3322 | case PTP_OPC_Composer: |
| 3323 | track->composer = get_string_from_object(device, track->item_id, PTP_OPC_Composer); |
| 3324 | break; |
Linus Walleij | 3fcfea5 | 2006-11-13 07:07:36 +0000 | [diff] [blame] | 3325 | case PTP_OPC_Duration: |
Linus Walleij | 9901e22 | 2006-11-30 12:28:19 +0000 | [diff] [blame] | 3326 | 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] | 3327 | break; |
| 3328 | case PTP_OPC_Track: |
Linus Walleij | 9901e22 | 2006-11-30 12:28:19 +0000 | [diff] [blame] | 3329 | 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] | 3330 | break; |
| 3331 | case PTP_OPC_Genre: |
Linus Walleij | 9901e22 | 2006-11-30 12:28:19 +0000 | [diff] [blame] | 3332 | 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] | 3333 | break; |
| 3334 | case PTP_OPC_AlbumName: |
Linus Walleij | 9901e22 | 2006-11-30 12:28:19 +0000 | [diff] [blame] | 3335 | 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] | 3336 | break; |
| 3337 | case PTP_OPC_OriginalReleaseDate: |
Linus Walleij | 9901e22 | 2006-11-30 12:28:19 +0000 | [diff] [blame] | 3338 | 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] | 3339 | break; |
| 3340 | // These are, well not so important. |
| 3341 | case PTP_OPC_SampleRate: |
Linus Walleij | 9901e22 | 2006-11-30 12:28:19 +0000 | [diff] [blame] | 3342 | 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] | 3343 | break; |
| 3344 | case PTP_OPC_NumberOfChannels: |
Linus Walleij | 9901e22 | 2006-11-30 12:28:19 +0000 | [diff] [blame] | 3345 | 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] | 3346 | break; |
| 3347 | case PTP_OPC_AudioWAVECodec: |
Linus Walleij | 9901e22 | 2006-11-30 12:28:19 +0000 | [diff] [blame] | 3348 | 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] | 3349 | break; |
| 3350 | case PTP_OPC_AudioBitRate: |
Linus Walleij | 9901e22 | 2006-11-30 12:28:19 +0000 | [diff] [blame] | 3351 | 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] | 3352 | break; |
| 3353 | case PTP_OPC_BitRateType: |
Linus Walleij | 9901e22 | 2006-11-30 12:28:19 +0000 | [diff] [blame] | 3354 | 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] | 3355 | break; |
| 3356 | case PTP_OPC_Rating: |
Linus Walleij | 9901e22 | 2006-11-30 12:28:19 +0000 | [diff] [blame] | 3357 | 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] | 3358 | break; |
| 3359 | case PTP_OPC_UseCount: |
Linus Walleij | 9901e22 | 2006-11-30 12:28:19 +0000 | [diff] [blame] | 3360 | 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] | 3361 | break; |
Linus Walleij | d9d28d5 | 2007-08-04 19:01:18 +0000 | [diff] [blame] | 3362 | case PTP_OPC_ObjectSize: |
Linus Walleij | ddaba2f | 2007-10-02 21:20:30 +0000 | [diff] [blame] | 3363 | if (device->object_bitsize == 64) { |
| 3364 | track->filesize = get_u64_from_object(device, track->item_id, PTP_OPC_ObjectSize, 0); |
| 3365 | } else { |
| 3366 | 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] | 3367 | } |
Linus Walleij | d9d28d5 | 2007-08-04 19:01:18 +0000 | [diff] [blame] | 3368 | break; |
Linus Walleij | 3fcfea5 | 2006-11-13 07:07:36 +0000 | [diff] [blame] | 3369 | } |
| 3370 | } |
| 3371 | free(props); |
| 3372 | } |
Linus Walleij | 00cf064 | 2006-07-26 20:40:59 +0000 | [diff] [blame] | 3373 | } |
Linus Walleij | 8ab5426 | 2006-06-21 07:12:28 +0000 | [diff] [blame] | 3374 | } |
| 3375 | |
| 3376 | /** |
mopoke | 3136444 | 2006-11-20 04:53:04 +0000 | [diff] [blame] | 3377 | * THIS FUNCTION IS DEPRECATED. PLEASE UPDATE YOUR CODE IN ORDER |
Linus Walleij | 3fcfea5 | 2006-11-13 07:07:36 +0000 | [diff] [blame] | 3378 | * NOT TO USE IT. |
| 3379 | * @see LIBMTP_Get_Tracklisting_With_Callback() |
| 3380 | */ |
| 3381 | LIBMTP_track_t *LIBMTP_Get_Tracklisting(LIBMTP_mtpdevice_t *device) |
| 3382 | { |
| 3383 | printf("WARNING: LIBMTP_Get_Tracklisting() is deprecated.\n"); |
| 3384 | printf("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] | 3385 | return LIBMTP_Get_Tracklisting_With_Callback(device, NULL, NULL); |
Linus Walleij | 3fcfea5 | 2006-11-13 07:07:36 +0000 | [diff] [blame] | 3386 | } |
| 3387 | |
| 3388 | /** |
Linus Walleij | 25d5c21 | 2008-08-14 06:49:13 +0000 | [diff] [blame] | 3389 | * This returns a long list of all tracks available on the current MTP device. |
| 3390 | * Tracks include multimedia objects, both music tracks and video tracks. |
| 3391 | * Typical usage: |
Linus Walleij | a498273 | 2006-02-24 15:46:02 +0000 | [diff] [blame] | 3392 | * |
| 3393 | * <pre> |
| 3394 | * LIBMTP_track_t *tracklist; |
| 3395 | * |
Richard Low | 6711f44 | 2007-05-05 19:00:59 +0000 | [diff] [blame] | 3396 | * tracklist = LIBMTP_Get_Tracklisting_With_Callback(device, callback, data); |
Linus Walleij | a498273 | 2006-02-24 15:46:02 +0000 | [diff] [blame] | 3397 | * while (tracklist != NULL) { |
| 3398 | * LIBMTP_track_t *tmp; |
| 3399 | * |
| 3400 | * // Do something on each element in the list here... |
| 3401 | * tmp = tracklist; |
| 3402 | * tracklist = tracklist->next; |
| 3403 | * LIBMTP_destroy_track_t(tmp); |
| 3404 | * } |
| 3405 | * </pre> |
| 3406 | * |
Linus Walleij | 25d5c21 | 2008-08-14 06:49:13 +0000 | [diff] [blame] | 3407 | * If you want to group your track listing by storage (per storage unit) or |
| 3408 | * arrange tracks into folders, you must dereference the <code>storage_id</code> |
| 3409 | * and/or <code>parent_id</code> field of the returned <code>LIBMTP_track_t</code> |
| 3410 | * struct. To arrange by folders or files you typically have to create the proper |
| 3411 | * trees by calls to <code>LIBMTP_Get_Storage()</code> and/or |
| 3412 | * <code>LIBMTP_Get_Folder_List()</code> first. |
| 3413 | * |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 3414 | * @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] | 3415 | * @param callback a function to be called during the tracklisting retrieveal |
Linus Walleij | 25d5c21 | 2008-08-14 06:49:13 +0000 | [diff] [blame] | 3416 | * for displaying progress bars etc, or NULL if you don't want |
| 3417 | * any callbacks. |
Richard Low | dc0b6c7 | 2006-11-13 09:22:23 +0000 | [diff] [blame] | 3418 | * @param data a user-defined pointer that is passed along to |
Linus Walleij | 25d5c21 | 2008-08-14 06:49:13 +0000 | [diff] [blame] | 3419 | * the <code>progress</code> function in order to |
| 3420 | * pass along some user defined data to the progress |
| 3421 | * updates. If not used, set this to NULL. |
Linus Walleij | a498273 | 2006-02-24 15:46:02 +0000 | [diff] [blame] | 3422 | * @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] | 3423 | * field of the <code>LIBMTP_track_t</code> data structure. |
| 3424 | * Each of the metadata tags must be freed after use, and may |
| 3425 | * contain only partial metadata information, i.e. one or several |
| 3426 | * fields may be NULL or 0. |
Linus Walleij | 2e4b5f9 | 2006-06-16 14:00:49 +0000 | [diff] [blame] | 3427 | * @see LIBMTP_Get_Trackmetadata() |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 3428 | */ |
Richard Low | dc0b6c7 | 2006-11-13 09:22:23 +0000 | [diff] [blame] | 3429 | LIBMTP_track_t *LIBMTP_Get_Tracklisting_With_Callback(LIBMTP_mtpdevice_t *device, |
| 3430 | LIBMTP_progressfunc_t const callback, |
| 3431 | void const * const data) |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 3432 | { |
| 3433 | uint32_t i = 0; |
| 3434 | LIBMTP_track_t *retracks = NULL; |
| 3435 | LIBMTP_track_t *curtrack = NULL; |
Linus Walleij | 9b28da3 | 2006-03-16 13:47:58 +0000 | [diff] [blame] | 3436 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | 2be40c7 | 2007-03-16 15:19:44 +0000 | [diff] [blame] | 3437 | PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo; |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 3438 | |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 3439 | // Get all the handles if we haven't already done that |
Linus Walleij | 9b28da3 | 2006-03-16 13:47:58 +0000 | [diff] [blame] | 3440 | if (params->handles.Handler == NULL) { |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 3441 | flush_handles(device); |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 3442 | } |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 3443 | |
Linus Walleij | 9b28da3 | 2006-03-16 13:47:58 +0000 | [diff] [blame] | 3444 | for (i = 0; i < params->handles.n; i++) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 3445 | LIBMTP_track_t *track; |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 3446 | PTPObjectInfo *oi; |
Linus Walleij | 46651f3 | 2008-04-26 23:30:19 +0000 | [diff] [blame] | 3447 | LIBMTP_filetype_t mtptype; |
mopoke | 3136444 | 2006-11-20 04:53:04 +0000 | [diff] [blame] | 3448 | |
Richard Low | dc0b6c7 | 2006-11-13 09:22:23 +0000 | [diff] [blame] | 3449 | if (callback != NULL) |
| 3450 | callback(i, params->handles.n, data); |
mopoke | 3136444 | 2006-11-20 04:53:04 +0000 | [diff] [blame] | 3451 | |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 3452 | oi = ¶ms->objectinfo[i]; |
Linus Walleij | 46651f3 | 2008-04-26 23:30:19 +0000 | [diff] [blame] | 3453 | mtptype = map_ptp_type_to_libmtp_type(oi->ObjectFormat); |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 3454 | |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 3455 | // Ignore stuff we don't know how to handle... |
| 3456 | // TODO: get this list as an intersection of the sets |
| 3457 | // supported by the device and the from the device and |
| 3458 | // all known audio track files? |
Linus Walleij | 10e0ce7 | 2008-05-04 19:20:33 +0000 | [diff] [blame] | 3459 | if (!LIBMTP_FILETYPE_IS_AUDIO(mtptype) && |
| 3460 | !LIBMTP_FILETYPE_IS_VIDEO(mtptype) && |
| 3461 | !LIBMTP_FILETYPE_IS_AUDIOVIDEO(mtptype) && |
Linus Walleij | 46651f3 | 2008-04-26 23:30:19 +0000 | [diff] [blame] | 3462 | // This row lets through undefined files for examination since they may be forgotten OGG files. |
| 3463 | (oi->ObjectFormat != PTP_OFC_Undefined || |
Linus Walleij | fec4d56 | 2008-06-01 22:30:36 +0000 | [diff] [blame] | 3464 | !FLAG_IRIVER_OGG_ALZHEIMER(ptp_usb) || |
| 3465 | !FLAG_OGG_IS_UNKNOWN(ptp_usb)) |
Linus Walleij | 46651f3 | 2008-04-26 23:30:19 +0000 | [diff] [blame] | 3466 | ) { |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 3467 | // printf("Not a music track (format: %d), skipping...\n",oi.ObjectFormat); |
| 3468 | continue; |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 3469 | } |
| 3470 | |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 3471 | // Allocate a new track type |
| 3472 | track = LIBMTP_new_track_t(); |
| 3473 | |
| 3474 | // This is some sort of unique ID so we can keep track of the track. |
| 3475 | track->item_id = params->handles.Handler[i]; |
| 3476 | track->parent_id = oi->ParentObject; |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 3477 | track->storage_id = oi->StorageID; |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 3478 | |
Linus Walleij | 46651f3 | 2008-04-26 23:30:19 +0000 | [diff] [blame] | 3479 | track->filetype = mtptype; |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 3480 | |
| 3481 | // Original file-specific properties |
| 3482 | track->filesize = oi->ObjectCompressedSize; |
| 3483 | if (oi->Filename != NULL) { |
| 3484 | track->filename = strdup(oi->Filename); |
| 3485 | } |
| 3486 | |
| 3487 | get_track_metadata(device, oi->ObjectFormat, track); |
| 3488 | |
| 3489 | /* |
| 3490 | * A special quirk for iriver devices that doesn't quite |
| 3491 | * remember that some files marked as "unknown" type are |
| 3492 | * actually OGG files. We look at the filename extension |
| 3493 | * and see if it happens that this was atleast named "ogg" |
| 3494 | * and fall back on this heuristic approach in that case, |
| 3495 | * for these bugged devices only. |
| 3496 | */ |
| 3497 | if (track->filetype == LIBMTP_FILETYPE_UNKNOWN && |
Linus Walleij | fec4d56 | 2008-06-01 22:30:36 +0000 | [diff] [blame] | 3498 | (FLAG_IRIVER_OGG_ALZHEIMER(ptp_usb) || |
| 3499 | FLAG_OGG_IS_UNKNOWN(ptp_usb))) { |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 3500 | // Repair forgotten OGG filetype |
| 3501 | char *ptype; |
| 3502 | |
| 3503 | ptype = rindex(track->filename,'.')+1; |
| 3504 | if (ptype != NULL && !strcasecmp (ptype, "ogg")) { |
| 3505 | // Fix it. |
| 3506 | track->filetype = LIBMTP_FILETYPE_OGG; |
| 3507 | } else { |
| 3508 | // This was not an OGG file so discard it and continue |
| 3509 | LIBMTP_destroy_track_t(track); |
| 3510 | continue; |
| 3511 | } |
| 3512 | } |
| 3513 | |
| 3514 | // Add track to a list that will be returned afterwards. |
| 3515 | if (retracks == NULL) { |
| 3516 | retracks = track; |
| 3517 | curtrack = track; |
| 3518 | } else { |
| 3519 | curtrack->next = track; |
| 3520 | curtrack = track; |
| 3521 | } |
| 3522 | |
| 3523 | // Call listing callback |
| 3524 | // double progressPercent = (double)i*(double)100.0 / (double)params->handles.n; |
| 3525 | |
| 3526 | |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 3527 | } // Handle counting loop |
| 3528 | return retracks; |
| 3529 | } |
Linus Walleij | dcde608 | 2006-02-17 16:16:34 +0000 | [diff] [blame] | 3530 | |
Linus Walleij | 2e4b5f9 | 2006-06-16 14:00:49 +0000 | [diff] [blame] | 3531 | /** |
| 3532 | * This function retrieves the metadata for a single track off |
| 3533 | * the device. |
| 3534 | * |
| 3535 | * Do not call this function repeatedly! The track handles are linearly |
| 3536 | * searched O(n) and the call may involve (slow) USB traffic, so use |
| 3537 | * <code>LIBMTP_Get_Tracklisting()</code> and cache the tracks, preferably |
| 3538 | * as an efficient data structure such as a hash list. |
| 3539 | * |
| 3540 | * @param device a pointer to the device to get the track metadata from. |
| 3541 | * @param trackid the object ID of the track that you want the metadata for. |
| 3542 | * @return a track metadata entry on success or NULL on failure. |
| 3543 | * @see LIBMTP_Get_Tracklisting() |
| 3544 | */ |
| 3545 | LIBMTP_track_t *LIBMTP_Get_Trackmetadata(LIBMTP_mtpdevice_t *device, uint32_t const trackid) |
| 3546 | { |
| 3547 | uint32_t i = 0; |
| 3548 | PTPParams *params = (PTPParams *) device->params; |
| 3549 | |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 3550 | // Get all the handles if we haven't already done that |
Linus Walleij | 2e4b5f9 | 2006-06-16 14:00:49 +0000 | [diff] [blame] | 3551 | if (params->handles.Handler == NULL) { |
| 3552 | flush_handles(device); |
| 3553 | } |
| 3554 | |
| 3555 | for (i = 0; i < params->handles.n; i++) { |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 3556 | PTPObjectInfo *oi; |
| 3557 | LIBMTP_track_t *track; |
Linus Walleij | 46651f3 | 2008-04-26 23:30:19 +0000 | [diff] [blame] | 3558 | LIBMTP_filetype_t mtptype; |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 3559 | |
Linus Walleij | 2e4b5f9 | 2006-06-16 14:00:49 +0000 | [diff] [blame] | 3560 | // Skip if this is not the track we want. |
| 3561 | if (params->handles.Handler[i] != trackid) { |
| 3562 | continue; |
| 3563 | } |
| 3564 | |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 3565 | oi = ¶ms->objectinfo[i]; |
Linus Walleij | 46651f3 | 2008-04-26 23:30:19 +0000 | [diff] [blame] | 3566 | mtptype = map_ptp_type_to_libmtp_type(oi->ObjectFormat); |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 3567 | |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 3568 | // Ignore stuff we don't know how to handle... |
Linus Walleij | 10e0ce7 | 2008-05-04 19:20:33 +0000 | [diff] [blame] | 3569 | if (!LIBMTP_FILETYPE_IS_AUDIO(mtptype) && |
| 3570 | !LIBMTP_FILETYPE_IS_VIDEO(mtptype) && |
| 3571 | !LIBMTP_FILETYPE_IS_AUDIOVIDEO(mtptype)) { |
Linus Walleij | 2e4b5f9 | 2006-06-16 14:00:49 +0000 | [diff] [blame] | 3572 | return NULL; |
| 3573 | } |
| 3574 | |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 3575 | // Allocate a new track type |
| 3576 | track = LIBMTP_new_track_t(); |
| 3577 | |
| 3578 | // This is some sort of unique ID so we can keep track of the track. |
| 3579 | track->item_id = params->handles.Handler[i]; |
| 3580 | track->parent_id = oi->ParentObject; |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 3581 | track->storage_id = oi->StorageID; |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 3582 | |
Linus Walleij | 46651f3 | 2008-04-26 23:30:19 +0000 | [diff] [blame] | 3583 | track->filetype = mtptype; |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 3584 | |
| 3585 | // Original file-specific properties |
| 3586 | track->filesize = oi->ObjectCompressedSize; |
| 3587 | if (oi->Filename != NULL) { |
| 3588 | track->filename = strdup(oi->Filename); |
| 3589 | } |
| 3590 | |
| 3591 | get_track_metadata(device, oi->ObjectFormat, track); |
| 3592 | |
| 3593 | return track; |
| 3594 | |
Linus Walleij | 2e4b5f9 | 2006-06-16 14:00:49 +0000 | [diff] [blame] | 3595 | } |
| 3596 | return NULL; |
| 3597 | } |
| 3598 | |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 3599 | |
| 3600 | /** |
| 3601 | * This gets a file off the device to a local file identified |
| 3602 | * by a filename. |
| 3603 | * @param device a pointer to the device to get the track from. |
| 3604 | * @param id the file ID of the file to retrieve. |
| 3605 | * @param path a filename to use for the retrieved file. |
| 3606 | * @param callback a progress indicator function or NULL to ignore. |
| 3607 | * @param data a user-defined pointer that is passed along to |
| 3608 | * the <code>progress</code> function in order to |
| 3609 | * pass along some user defined data to the progress |
| 3610 | * updates. If not used, set this to NULL. |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 3611 | * @return 0 if the transfer was successful, any other value means |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 3612 | * failure. |
| 3613 | * @see LIBMTP_Get_File_To_File_Descriptor() |
| 3614 | */ |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 3615 | 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] | 3616 | char const * const path, LIBMTP_progressfunc_t const callback, |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 3617 | void const * const data) |
| 3618 | { |
| 3619 | int fd = -1; |
| 3620 | int ret; |
| 3621 | |
| 3622 | // Sanity check |
| 3623 | if (path == NULL) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 3624 | 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] | 3625 | return -1; |
| 3626 | } |
| 3627 | |
| 3628 | // Open file |
| 3629 | #ifdef __WIN32__ |
Linus Walleij | 7beba57 | 2006-11-29 08:56:12 +0000 | [diff] [blame] | 3630 | #ifdef USE_WINDOWS_IO_H |
| 3631 | if ( (fd = _open(path, O_RDWR|O_CREAT|O_TRUNC|O_BINARY,_S_IREAD)) == -1 ) { |
| 3632 | #else |
flavien | bfd80eb | 2006-06-01 22:41:49 +0000 | [diff] [blame] | 3633 | if ( (fd = open(path, O_RDWR|O_CREAT|O_TRUNC|O_BINARY,S_IRWXU|S_IRGRP)) == -1 ) { |
Linus Walleij | 7beba57 | 2006-11-29 08:56:12 +0000 | [diff] [blame] | 3634 | #endif |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 3635 | #else |
Linus Walleij | 0535838 | 2007-08-06 20:46:35 +0000 | [diff] [blame] | 3636 | #ifdef __USE_LARGEFILE64 |
| 3637 | if ( (fd = open64(path, O_RDWR|O_CREAT|O_TRUNC|O_LARGEFILE,S_IRWXU|S_IRGRP)) == -1) { |
| 3638 | #else |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 3639 | if ( (fd = open(path, O_RDWR|O_CREAT|O_TRUNC,S_IRWXU|S_IRGRP)) == -1) { |
| 3640 | #endif |
Linus Walleij | 0535838 | 2007-08-06 20:46:35 +0000 | [diff] [blame] | 3641 | #endif |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 3642 | 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] | 3643 | return -1; |
| 3644 | } |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 3645 | |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 3646 | ret = LIBMTP_Get_File_To_File_Descriptor(device, id, fd, callback, data); |
| 3647 | |
| 3648 | // Close file |
| 3649 | close(fd); |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 3650 | |
Linus Walleij | 25d33b5 | 2007-09-16 21:36:19 +0000 | [diff] [blame] | 3651 | // Delete partial file. |
| 3652 | if (ret == -1) { |
| 3653 | unlink(path); |
| 3654 | } |
| 3655 | |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 3656 | return ret; |
| 3657 | } |
| 3658 | |
| 3659 | /** |
| 3660 | * This gets a file off the device to a file identified |
| 3661 | * by a file descriptor. |
Linus Walleij | 0558ac5 | 2006-09-07 06:55:03 +0000 | [diff] [blame] | 3662 | * |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 3663 | * This function can potentially be used for streaming |
| 3664 | * files off the device for playback or broadcast for example, |
Linus Walleij | 0558ac5 | 2006-09-07 06:55:03 +0000 | [diff] [blame] | 3665 | * by downloading the file into a stream sink e.g. a socket. |
| 3666 | * |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 3667 | * @param device a pointer to the device to get the file from. |
| 3668 | * @param id the file ID of the file to retrieve. |
| 3669 | * @param fd a local file descriptor to write the file to. |
| 3670 | * @param callback a progress indicator function or NULL to ignore. |
| 3671 | * @param data a user-defined pointer that is passed along to |
| 3672 | * the <code>progress</code> function in order to |
| 3673 | * pass along some user defined data to the progress |
| 3674 | * updates. If not used, set this to NULL. |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 3675 | * @return 0 if the transfer was successful, any other value means |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 3676 | * failure. |
| 3677 | * @see LIBMTP_Get_File_To_File() |
| 3678 | */ |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 3679 | int LIBMTP_Get_File_To_File_Descriptor(LIBMTP_mtpdevice_t *device, |
| 3680 | uint32_t const id, |
| 3681 | int const fd, |
Linus Walleij | ee73ef2 | 2006-08-27 19:56:00 +0000 | [diff] [blame] | 3682 | LIBMTP_progressfunc_t const callback, |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 3683 | void const * const data) |
| 3684 | { |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 3685 | PTPObjectInfo *oi; |
| 3686 | uint32_t i; |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 3687 | uint16_t ret; |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 3688 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | ee73ef2 | 2006-08-27 19:56:00 +0000 | [diff] [blame] | 3689 | PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo; |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 3690 | |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 3691 | oi = NULL; |
| 3692 | for (i = 0; i < params->handles.n; i++) { |
| 3693 | if (params->handles.Handler[i] == id) { |
| 3694 | oi = ¶ms->objectinfo[i]; |
| 3695 | break; |
| 3696 | } |
| 3697 | } |
| 3698 | if (oi == NULL) { |
| 3699 | 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] | 3700 | return -1; |
| 3701 | } |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 3702 | if (oi->ObjectFormat == PTP_OFC_Association) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 3703 | 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] | 3704 | return -1; |
| 3705 | } |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 3706 | |
Linus Walleij | ee73ef2 | 2006-08-27 19:56:00 +0000 | [diff] [blame] | 3707 | // Callbacks |
| 3708 | ptp_usb->callback_active = 1; |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 3709 | ptp_usb->current_transfer_total = oi->ObjectCompressedSize+ |
Linus Walleij | 7f0c72e | 2006-09-06 13:01:58 +0000 | [diff] [blame] | 3710 | PTP_USB_BULK_HDR_LEN+sizeof(uint32_t); // Request length, one parameter |
Linus Walleij | ee73ef2 | 2006-08-27 19:56:00 +0000 | [diff] [blame] | 3711 | ptp_usb->current_transfer_complete = 0; |
| 3712 | ptp_usb->current_transfer_callback = callback; |
| 3713 | ptp_usb->current_transfer_callback_data = data; |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 3714 | |
Linus Walleij | 96c6243 | 2006-08-21 10:04:02 +0000 | [diff] [blame] | 3715 | ret = ptp_getobject_tofd(params, id, fd); |
Linus Walleij | ee73ef2 | 2006-08-27 19:56:00 +0000 | [diff] [blame] | 3716 | |
| 3717 | ptp_usb->callback_active = 0; |
| 3718 | ptp_usb->current_transfer_callback = NULL; |
| 3719 | ptp_usb->current_transfer_callback_data = NULL; |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 3720 | |
Linus Walleij | bd3bf9e | 2007-09-14 19:31:54 +0000 | [diff] [blame] | 3721 | if (ret == PTP_ERROR_CANCEL) { |
Linus Walleij | fb28b63 | 2007-10-23 21:56:18 +0000 | [diff] [blame] | 3722 | 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] | 3723 | return -1; |
| 3724 | } |
Linus Walleij | b02a066 | 2006-04-25 08:05:09 +0000 | [diff] [blame] | 3725 | if (ret != PTP_RC_OK) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 3726 | 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] | 3727 | return -1; |
| 3728 | } |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 3729 | |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 3730 | return 0; |
| 3731 | } |
| 3732 | |
Linus Walleij | dcde608 | 2006-02-17 16:16:34 +0000 | [diff] [blame] | 3733 | /** |
| 3734 | * This gets a track off the device to a file identified |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 3735 | * by a filename. This is actually just a wrapper for the |
| 3736 | * \c LIBMTP_Get_Track_To_File() function. |
Linus Walleij | dcde608 | 2006-02-17 16:16:34 +0000 | [diff] [blame] | 3737 | * @param device a pointer to the device to get the track from. |
| 3738 | * @param id the track ID of the track to retrieve. |
| 3739 | * @param path a filename to use for the retrieved track. |
| 3740 | * @param callback a progress indicator function or NULL to ignore. |
| 3741 | * @param data a user-defined pointer that is passed along to |
| 3742 | * the <code>progress</code> function in order to |
| 3743 | * pass along some user defined data to the progress |
| 3744 | * updates. If not used, set this to NULL. |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 3745 | * @return 0 if the transfer was successful, any other value means |
Linus Walleij | dcde608 | 2006-02-17 16:16:34 +0000 | [diff] [blame] | 3746 | * failure. |
| 3747 | * @see LIBMTP_Get_Track_To_File_Descriptor() |
| 3748 | */ |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 3749 | 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] | 3750 | char const * const path, LIBMTP_progressfunc_t const callback, |
Linus Walleij | 0cd8543 | 2006-02-20 14:37:50 +0000 | [diff] [blame] | 3751 | void const * const data) |
Linus Walleij | dcde608 | 2006-02-17 16:16:34 +0000 | [diff] [blame] | 3752 | { |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 3753 | // This is just a wrapper |
| 3754 | return LIBMTP_Get_File_To_File(device, id, path, callback, data); |
Linus Walleij | dcde608 | 2006-02-17 16:16:34 +0000 | [diff] [blame] | 3755 | } |
| 3756 | |
| 3757 | /** |
| 3758 | * This gets a track off the device to a file identified |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 3759 | * by a file descriptor. This is actually just a wrapper for |
| 3760 | * the \c LIBMTP_Get_File_To_File_Descriptor() function. |
Linus Walleij | dcde608 | 2006-02-17 16:16:34 +0000 | [diff] [blame] | 3761 | * @param device a pointer to the device to get the track from. |
| 3762 | * @param id the track ID of the track to retrieve. |
| 3763 | * @param fd a file descriptor to write the track to. |
| 3764 | * @param callback a progress indicator function or NULL to ignore. |
| 3765 | * @param data a user-defined pointer that is passed along to |
| 3766 | * the <code>progress</code> function in order to |
| 3767 | * pass along some user defined data to the progress |
| 3768 | * updates. If not used, set this to NULL. |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 3769 | * @return 0 if the transfer was successful, any other value means |
Linus Walleij | dcde608 | 2006-02-17 16:16:34 +0000 | [diff] [blame] | 3770 | * failure. |
| 3771 | * @see LIBMTP_Get_Track_To_File() |
| 3772 | */ |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 3773 | int LIBMTP_Get_Track_To_File_Descriptor(LIBMTP_mtpdevice_t *device, |
| 3774 | uint32_t const id, |
| 3775 | int const fd, |
Linus Walleij | ee73ef2 | 2006-08-27 19:56:00 +0000 | [diff] [blame] | 3776 | LIBMTP_progressfunc_t const callback, |
Linus Walleij | 0cd8543 | 2006-02-20 14:37:50 +0000 | [diff] [blame] | 3777 | void const * const data) |
Linus Walleij | dcde608 | 2006-02-17 16:16:34 +0000 | [diff] [blame] | 3778 | { |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 3779 | // This is just a wrapper |
| 3780 | return LIBMTP_Get_File_To_File_Descriptor(device, id, fd, callback, data); |
Linus Walleij | dcde608 | 2006-02-17 16:16:34 +0000 | [diff] [blame] | 3781 | } |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 3782 | |
| 3783 | /** |
| 3784 | * This function sends a track from a local file to an |
| 3785 | * MTP device. A filename and a set of metadata must be |
| 3786 | * given as input. |
| 3787 | * @param device a pointer to the device to send the track to. |
| 3788 | * @param path the filename of a local file which will be sent. |
| 3789 | * @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] | 3790 | * After this call the field <code>metadata->item_id</code> |
| 3791 | * will contain the new track ID. Other fields such |
| 3792 | * as the <code>metadata->filename</code>, <code>metadata->parent_id</code> |
| 3793 | * or <code>metadata->storage_id</code> may also change during this |
| 3794 | * operation due to device restrictions, so do not rely on the |
| 3795 | * contents of this struct to be preserved in any way. |
| 3796 | * <ul> |
| 3797 | * <li><code>metadata->parent_id</code> should be set to the parent |
| 3798 | * (e.g. folder) to store this track in. Since some |
| 3799 | * devices are a bit picky about where files |
| 3800 | * are placed, a default folder will be chosen if libmtp |
| 3801 | * has detected one for the current filetype and this |
| 3802 | * parameter is set to 0. If this is 0 and no default folder |
| 3803 | * can be found, the file will be stored in the root folder. |
| 3804 | * <li><code>metadata->storage_id</code> should be set to the |
| 3805 | * desired storage (e.g. memory card or whatever your device |
| 3806 | * presents) to store this track in. Setting this to 0 will store |
| 3807 | * the track on the primary storage. |
| 3808 | * </ul> |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 3809 | * @param callback a progress indicator function or NULL to ignore. |
| 3810 | * @param data a user-defined pointer that is passed along to |
| 3811 | * the <code>progress</code> function in order to |
| 3812 | * pass along some user defined data to the progress |
| 3813 | * updates. If not used, set this to NULL. |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 3814 | * @return 0 if the transfer was successful, any other value means |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 3815 | * failure. |
| 3816 | * @see LIBMTP_Send_Track_From_File_Descriptor() |
Linus Walleij | 1b87ea7 | 2007-08-28 07:53:09 +0000 | [diff] [blame] | 3817 | * @see LIBMTP_Send_File_From_File() |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 3818 | * @see LIBMTP_Delete_Object() |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 3819 | */ |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 3820 | int LIBMTP_Send_Track_From_File(LIBMTP_mtpdevice_t *device, |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 3821 | char const * const path, LIBMTP_track_t * const metadata, |
Linus Walleij | ee73ef2 | 2006-08-27 19:56:00 +0000 | [diff] [blame] | 3822 | LIBMTP_progressfunc_t const callback, |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 3823 | void const * const data) |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 3824 | { |
| 3825 | int fd; |
| 3826 | int ret; |
| 3827 | |
| 3828 | // Sanity check |
| 3829 | if (path == NULL) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 3830 | 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] | 3831 | return -1; |
| 3832 | } |
| 3833 | |
| 3834 | // Open file |
| 3835 | #ifdef __WIN32__ |
Linus Walleij | 7beba57 | 2006-11-29 08:56:12 +0000 | [diff] [blame] | 3836 | #ifdef USE_WINDOWS_IO_H |
| 3837 | if ( (fd = _open(path, O_RDONLY|O_BINARY) == -1 ) { |
| 3838 | #else |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 3839 | if ( (fd = open(path, O_RDONLY|O_BINARY) == -1 ) { |
Linus Walleij | 7beba57 | 2006-11-29 08:56:12 +0000 | [diff] [blame] | 3840 | #endif |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 3841 | #else |
Linus Walleij | 0535838 | 2007-08-06 20:46:35 +0000 | [diff] [blame] | 3842 | #ifdef __USE_LARGEFILE64 |
| 3843 | if ( (fd = open64(path, O_RDONLY|O_LARGEFILE)) == -1) { |
| 3844 | #else |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 3845 | if ( (fd = open(path, O_RDONLY)) == -1) { |
| 3846 | #endif |
Linus Walleij | 0535838 | 2007-08-06 20:46:35 +0000 | [diff] [blame] | 3847 | #endif |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 3848 | printf("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] | 3849 | return -1; |
| 3850 | } |
| 3851 | |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 3852 | ret = LIBMTP_Send_Track_From_File_Descriptor(device, fd, metadata, callback, data); |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 3853 | |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 3854 | // Close file. |
Linus Walleij | 7beba57 | 2006-11-29 08:56:12 +0000 | [diff] [blame] | 3855 | #ifdef USE_WINDOWS_IO_H |
| 3856 | _close(fd); |
| 3857 | #else |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 3858 | close(fd); |
Linus Walleij | 7beba57 | 2006-11-29 08:56:12 +0000 | [diff] [blame] | 3859 | #endif |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 3860 | |
| 3861 | return ret; |
| 3862 | } |
| 3863 | |
Linus Walleij | fa1374c | 2006-02-27 07:41:46 +0000 | [diff] [blame] | 3864 | |
Richard Low | 016e373 | 2007-09-23 14:53:46 +0000 | [diff] [blame] | 3865 | |
Linus Walleij | fa1374c | 2006-02-27 07:41:46 +0000 | [diff] [blame] | 3866 | /** |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 3867 | * This function sends a track from a file descriptor to an |
| 3868 | * MTP device. A filename and a set of metadata must be |
| 3869 | * given as input. |
| 3870 | * @param device a pointer to the device to send the track to. |
| 3871 | * @param fd the filedescriptor for a local file which will be sent. |
| 3872 | * @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] | 3873 | * After this call the field <code>metadata->item_id</code> |
| 3874 | * will contain the new track ID. Other fields such |
| 3875 | * as the <code>metadata->filename</code>, <code>metadata->parent_id</code> |
| 3876 | * or <code>metadata->storage_id</code> may also change during this |
| 3877 | * operation due to device restrictions, so do not rely on the |
| 3878 | * contents of this struct to be preserved in any way. |
| 3879 | * <ul> |
| 3880 | * <li><code>metadata->parent_id</code> should be set to the parent |
| 3881 | * (e.g. folder) to store this track in. Since some |
| 3882 | * devices are a bit picky about where files |
| 3883 | * are placed, a default folder will be chosen if libmtp |
| 3884 | * has detected one for the current filetype and this |
| 3885 | * parameter is set to 0. If this is 0 and no default folder |
| 3886 | * can be found, the file will be stored in the root folder. |
| 3887 | * <li><code>metadata->storage_id</code> should be set to the |
| 3888 | * desired storage (e.g. memory card or whatever your device |
| 3889 | * presents) to store this track in. Setting this to 0 will store |
| 3890 | * the track on the primary storage. |
| 3891 | * </ul> |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 3892 | * @param callback a progress indicator function or NULL to ignore. |
| 3893 | * @param data a user-defined pointer that is passed along to |
| 3894 | * the <code>progress</code> function in order to |
| 3895 | * pass along some user defined data to the progress |
| 3896 | * updates. If not used, set this to NULL. |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 3897 | * @return 0 if the transfer was successful, any other value means |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 3898 | * failure. |
| 3899 | * @see LIBMTP_Send_Track_From_File() |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 3900 | * @see LIBMTP_Delete_Object() |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 3901 | */ |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 3902 | int LIBMTP_Send_Track_From_File_Descriptor(LIBMTP_mtpdevice_t *device, |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 3903 | int const fd, LIBMTP_track_t * const metadata, |
Linus Walleij | ee73ef2 | 2006-08-27 19:56:00 +0000 | [diff] [blame] | 3904 | LIBMTP_progressfunc_t const callback, |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 3905 | void const * const data) |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 3906 | { |
Linus Walleij | 17e39f7 | 2006-02-23 15:54:28 +0000 | [diff] [blame] | 3907 | int subcall_ret; |
Linus Walleij | d2778d1 | 2007-08-06 19:24:58 +0000 | [diff] [blame] | 3908 | LIBMTP_file_t filedata; |
Linus Walleij | 8ae78bb | 2006-11-20 21:45:52 +0000 | [diff] [blame] | 3909 | |
Linus Walleij | 16c51f0 | 2006-05-04 13:20:22 +0000 | [diff] [blame] | 3910 | // Sanity check, is this really a track? |
Linus Walleij | 10e0ce7 | 2008-05-04 19:20:33 +0000 | [diff] [blame] | 3911 | if (!LIBMTP_FILETYPE_IS_TRACK(metadata->filetype)) { |
Linus Walleij | 46651f3 | 2008-04-26 23:30:19 +0000 | [diff] [blame] | 3912 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, |
| 3913 | "LIBMTP_Send_Track_From_File_Descriptor(): " |
| 3914 | "I don't think this is actually a track, strange filetype..."); |
Linus Walleij | 99310d4 | 2006-11-01 08:29:39 +0000 | [diff] [blame] | 3915 | } |
Linus Walleij | 3725329 | 2006-10-11 08:38:14 +0000 | [diff] [blame] | 3916 | |
Linus Walleij | d2778d1 | 2007-08-06 19:24:58 +0000 | [diff] [blame] | 3917 | // Wrap around the file transfer function |
| 3918 | filedata.item_id = metadata->item_id; |
| 3919 | filedata.parent_id = metadata->parent_id; |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 3920 | filedata.storage_id = metadata->storage_id; |
Linus Walleij | d2778d1 | 2007-08-06 19:24:58 +0000 | [diff] [blame] | 3921 | filedata.filename = metadata->filename; |
| 3922 | filedata.filesize = metadata->filesize; |
| 3923 | filedata.filetype = metadata->filetype; |
| 3924 | filedata.next = NULL; |
Linus Walleij | 3725329 | 2006-10-11 08:38:14 +0000 | [diff] [blame] | 3925 | |
Linus Walleij | d2778d1 | 2007-08-06 19:24:58 +0000 | [diff] [blame] | 3926 | subcall_ret = LIBMTP_Send_File_From_File_Descriptor(device, |
| 3927 | fd, |
| 3928 | &filedata, |
| 3929 | callback, |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 3930 | data); |
mopoke | 3136444 | 2006-11-20 04:53:04 +0000 | [diff] [blame] | 3931 | |
Linus Walleij | d2778d1 | 2007-08-06 19:24:58 +0000 | [diff] [blame] | 3932 | if (subcall_ret != 0) { |
Linus Walleij | 46651f3 | 2008-04-26 23:30:19 +0000 | [diff] [blame] | 3933 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, |
| 3934 | "LIBMTP_Send_Track_From_File_Descriptor(): " |
Linus Walleij | d2778d1 | 2007-08-06 19:24:58 +0000 | [diff] [blame] | 3935 | "subcall to LIBMTP_Send_File_From_File_Descriptor failed."); |
| 3936 | // We used to delete the file here, but don't... It might be OK after all. |
| 3937 | // (void) LIBMTP_Delete_Object(device, metadata->item_id); |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 3938 | return -1; |
| 3939 | } |
Linus Walleij | d2778d1 | 2007-08-06 19:24:58 +0000 | [diff] [blame] | 3940 | |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 3941 | // Pick up new item (and parent, storage) ID |
Linus Walleij | d2778d1 | 2007-08-06 19:24:58 +0000 | [diff] [blame] | 3942 | metadata->item_id = filedata.item_id; |
| 3943 | metadata->parent_id = filedata.parent_id; |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 3944 | metadata->storage_id = filedata.storage_id; |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 3945 | |
Linus Walleij | 17e39f7 | 2006-02-23 15:54:28 +0000 | [diff] [blame] | 3946 | // Set track metadata for the new fine track |
| 3947 | subcall_ret = LIBMTP_Update_Track_Metadata(device, metadata); |
| 3948 | if (subcall_ret != 0) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 3949 | // Subcall will add error to errorstack |
Linus Walleij | b275318 | 2007-02-26 08:11:38 +0000 | [diff] [blame] | 3950 | // We used to delete the file here, but don't... It might be OK after all. |
| 3951 | // (void) LIBMTP_Delete_Object(device, metadata->item_id); |
Linus Walleij | 17e39f7 | 2006-02-23 15:54:28 +0000 | [diff] [blame] | 3952 | return -1; |
| 3953 | } |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 3954 | |
Richard Low | 61edc1a | 2007-09-23 10:35:48 +0000 | [diff] [blame] | 3955 | // note we don't need to update the cache here because LIBMTP_Send_File_From_File_Descriptor |
| 3956 | // 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] | 3957 | |
Linus Walleij | 17e39f7 | 2006-02-23 15:54:28 +0000 | [diff] [blame] | 3958 | return 0; |
| 3959 | } |
| 3960 | |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 3961 | /** |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 3962 | * This function sends a local file to an MTP device. |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 3963 | * A filename and a set of metadata must be |
| 3964 | * given as input. |
| 3965 | * @param device a pointer to the device to send the track to. |
| 3966 | * @param path the filename of a local file which will be sent. |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 3967 | * @param filedata a file metadata set to be written along with the file. |
| 3968 | * After this call the field <code>filedata->item_id</code> |
| 3969 | * will contain the new file ID. Other fields such |
| 3970 | * as the <code>filedata->filename</code>, <code>filedata->parent_id</code> |
| 3971 | * or <code>filedata->storage_id</code> may also change during this |
| 3972 | * operation due to device restrictions, so do not rely on the |
| 3973 | * contents of this struct to be preserved in any way. |
| 3974 | * <ul> |
| 3975 | * <li><code>filedata->parent_id</code> should be set to the parent |
| 3976 | * (e.g. folder) to store this file in. If this is 0, |
| 3977 | * the file will be stored in the root folder. |
| 3978 | * <li><code>filedata->storage_id</code> should be set to the |
| 3979 | * desired storage (e.g. memory card or whatever your device |
| 3980 | * presents) to store this file in. Setting this to 0 will store |
| 3981 | * the file on the primary storage. |
| 3982 | * </ul> |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 3983 | * @param callback a progress indicator function or NULL to ignore. |
| 3984 | * @param data a user-defined pointer that is passed along to |
| 3985 | * the <code>progress</code> function in order to |
| 3986 | * pass along some user defined data to the progress |
| 3987 | * updates. If not used, set this to NULL. |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 3988 | * @return 0 if the transfer was successful, any other value means |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 3989 | * failure. |
| 3990 | * @see LIBMTP_Send_File_From_File_Descriptor() |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 3991 | * @see LIBMTP_Delete_Object() |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 3992 | */ |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 3993 | int LIBMTP_Send_File_From_File(LIBMTP_mtpdevice_t *device, |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 3994 | char const * const path, LIBMTP_file_t * const filedata, |
Linus Walleij | ee73ef2 | 2006-08-27 19:56:00 +0000 | [diff] [blame] | 3995 | LIBMTP_progressfunc_t const callback, |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 3996 | void const * const data) |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 3997 | { |
| 3998 | int fd; |
| 3999 | int ret; |
| 4000 | |
| 4001 | // Sanity check |
| 4002 | if (path == NULL) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 4003 | 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] | 4004 | return -1; |
| 4005 | } |
| 4006 | |
| 4007 | // Open file |
| 4008 | #ifdef __WIN32__ |
Linus Walleij | 7beba57 | 2006-11-29 08:56:12 +0000 | [diff] [blame] | 4009 | #ifdef USE_WINDOWS_IO_H |
| 4010 | if ( (fd = _open(path, O_RDONLY|O_BINARY) == -1 ) { |
| 4011 | #else |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 4012 | if ( (fd = open(path, O_RDONLY|O_BINARY) == -1 ) { |
Linus Walleij | 7beba57 | 2006-11-29 08:56:12 +0000 | [diff] [blame] | 4013 | #endif |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 4014 | #else |
Linus Walleij | 0535838 | 2007-08-06 20:46:35 +0000 | [diff] [blame] | 4015 | #ifdef __USE_LARGEFILE64 |
| 4016 | if ( (fd = open64(path, O_RDONLY|O_LARGEFILE)) == -1) { |
| 4017 | #else |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 4018 | if ( (fd = open(path, O_RDONLY)) == -1) { |
| 4019 | #endif |
Linus Walleij | 0535838 | 2007-08-06 20:46:35 +0000 | [diff] [blame] | 4020 | #endif |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 4021 | 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] | 4022 | return -1; |
| 4023 | } |
| 4024 | |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 4025 | ret = LIBMTP_Send_File_From_File_Descriptor(device, fd, filedata, callback, data); |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 4026 | |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 4027 | // Close file. |
Linus Walleij | 7beba57 | 2006-11-29 08:56:12 +0000 | [diff] [blame] | 4028 | #ifdef USE_WINDOWS_IO_H |
| 4029 | _close(fd); |
| 4030 | #else |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 4031 | close(fd); |
Linus Walleij | 7beba57 | 2006-11-29 08:56:12 +0000 | [diff] [blame] | 4032 | #endif |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 4033 | |
| 4034 | return ret; |
| 4035 | } |
| 4036 | |
Linus Walleij | d208f9c | 2006-04-27 14:16:06 +0000 | [diff] [blame] | 4037 | /** |
| 4038 | * This function sends a generic file from a file descriptor to an |
| 4039 | * MTP device. A filename and a set of metadata must be |
| 4040 | * given as input. |
Linus Walleij | cd3eb3d | 2006-09-02 21:33:22 +0000 | [diff] [blame] | 4041 | * |
| 4042 | * This can potentially be used for sending in a stream of unknown |
Linus Walleij | 1b87ea7 | 2007-08-28 07:53:09 +0000 | [diff] [blame] | 4043 | * length. Send music files with |
| 4044 | * <code>LIBMTP_Send_Track_From_File_Descriptor()</code> |
Linus Walleij | cd3eb3d | 2006-09-02 21:33:22 +0000 | [diff] [blame] | 4045 | * |
Linus Walleij | d208f9c | 2006-04-27 14:16:06 +0000 | [diff] [blame] | 4046 | * @param device a pointer to the device to send the file to. |
| 4047 | * @param fd the filedescriptor for a local file which will be sent. |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 4048 | * @param filedata a file metadata set to be written along with the file. |
| 4049 | * After this call the field <code>filedata->item_id</code> |
| 4050 | * will contain the new file ID. Other fields such |
| 4051 | * as the <code>filedata->filename</code>, <code>filedata->parent_id</code> |
| 4052 | * or <code>filedata->storage_id</code> may also change during this |
| 4053 | * operation due to device restrictions, so do not rely on the |
| 4054 | * contents of this struct to be preserved in any way. |
| 4055 | * <ul> |
| 4056 | * <li><code>filedata->parent_id</code> should be set to the parent |
| 4057 | * (e.g. folder) to store this file in. If this is 0, |
| 4058 | * the file will be stored in the root folder. |
| 4059 | * <li><code>filedata->storage_id</code> should be set to the |
| 4060 | * desired storage (e.g. memory card or whatever your device |
| 4061 | * presents) to store this file in. Setting this to 0 will store |
| 4062 | * the file on the primary storage. |
| 4063 | * </ul> |
Linus Walleij | d208f9c | 2006-04-27 14:16:06 +0000 | [diff] [blame] | 4064 | * @param callback a progress indicator function or NULL to ignore. |
| 4065 | * @param data a user-defined pointer that is passed along to |
| 4066 | * the <code>progress</code> function in order to |
| 4067 | * pass along some user defined data to the progress |
| 4068 | * updates. If not used, set this to NULL. |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 4069 | * @return 0 if the transfer was successful, any other value means |
Linus Walleij | d208f9c | 2006-04-27 14:16:06 +0000 | [diff] [blame] | 4070 | * failure. |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 4071 | * @see LIBMTP_Send_File_From_File() |
Linus Walleij | 1b87ea7 | 2007-08-28 07:53:09 +0000 | [diff] [blame] | 4072 | * @see LIBMTP_Send_Track_From_File_Descriptor() |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 4073 | * @see LIBMTP_Delete_Object() |
Linus Walleij | d208f9c | 2006-04-27 14:16:06 +0000 | [diff] [blame] | 4074 | */ |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 4075 | int LIBMTP_Send_File_From_File_Descriptor(LIBMTP_mtpdevice_t *device, |
Linus Walleij | d208f9c | 2006-04-27 14:16:06 +0000 | [diff] [blame] | 4076 | int const fd, LIBMTP_file_t * const filedata, |
Linus Walleij | ee73ef2 | 2006-08-27 19:56:00 +0000 | [diff] [blame] | 4077 | LIBMTP_progressfunc_t const callback, |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 4078 | void const * const data) |
Linus Walleij | d208f9c | 2006-04-27 14:16:06 +0000 | [diff] [blame] | 4079 | { |
| 4080 | uint16_t ret; |
Linus Walleij | 6bf68b4 | 2007-09-03 22:50:02 +0000 | [diff] [blame] | 4081 | uint32_t store; |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 4082 | uint32_t localph = filedata->parent_id; |
| 4083 | LIBMTP_devicestorage_t *storage; |
Linus Walleij | d208f9c | 2006-04-27 14:16:06 +0000 | [diff] [blame] | 4084 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | d214b9b | 2006-08-26 22:08:37 +0000 | [diff] [blame] | 4085 | PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo; |
rreardon | bbb4e56 | 2006-11-19 22:16:11 +0000 | [diff] [blame] | 4086 | int i; |
Linus Walleij | f5fcda3 | 2006-12-03 22:31:02 +0000 | [diff] [blame] | 4087 | int subcall_ret; |
Linus Walleij | 0779577 | 2007-08-06 18:44:06 +0000 | [diff] [blame] | 4088 | uint16_t of = map_libmtp_type_to_ptp_type(filedata->filetype); |
Linus Walleij | 817579a | 2008-05-23 21:21:40 +0000 | [diff] [blame] | 4089 | LIBMTP_file_t *newfilemeta; |
Linus Walleij | c40c9bf | 2008-06-13 23:24:17 +0000 | [diff] [blame] | 4090 | int use_primary_storage = 1; |
Richard Low | ab0d22d | 2006-11-30 22:17:49 +0000 | [diff] [blame] | 4091 | |
Linus Walleij | 6bf68b4 | 2007-09-03 22:50:02 +0000 | [diff] [blame] | 4092 | // Sanity check: no zerolength files. |
Linus Walleij | 0779577 | 2007-08-06 18:44:06 +0000 | [diff] [blame] | 4093 | if (filedata->filesize == 0) { |
| 4094 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Send_File_From_File_Descriptor(): " |
| 4095 | "File of zero size."); |
| 4096 | return -1; |
Linus Walleij | cd3eb3d | 2006-09-02 21:33:22 +0000 | [diff] [blame] | 4097 | } |
Linus Walleij | 0779577 | 2007-08-06 18:44:06 +0000 | [diff] [blame] | 4098 | |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 4099 | if (filedata->storage_id != 0) { |
| 4100 | store = filedata->storage_id; |
Linus Walleij | fb28b63 | 2007-10-23 21:56:18 +0000 | [diff] [blame] | 4101 | } else { |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 4102 | // See if there is some storage we can fit this file on. |
| 4103 | storage = device->storage; |
Linus Walleij | fb28b63 | 2007-10-23 21:56:18 +0000 | [diff] [blame] | 4104 | if (storage == NULL) { |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 4105 | // Sometimes the storage just cannot be detected. |
| 4106 | store = 0x00000000U; |
| 4107 | } else { |
| 4108 | while(storage != NULL) { |
| 4109 | subcall_ret = check_if_file_fits(device, storage, filedata->filesize); |
| 4110 | if (subcall_ret != 0) { |
| 4111 | storage = storage->next; |
| 4112 | } |
| 4113 | break; |
| 4114 | } |
| 4115 | if (storage == NULL) { |
| 4116 | add_error_to_errorstack(device, LIBMTP_ERROR_STORAGE_FULL, "LIBMTP_Send_File_From_File_Descriptor(): " |
| 4117 | "all device storage is full or corrupt."); |
| 4118 | return -1; |
| 4119 | } |
| 4120 | store = storage->id; |
Linus Walleij | fb28b63 | 2007-10-23 21:56:18 +0000 | [diff] [blame] | 4121 | } |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 4122 | } |
| 4123 | // Detect if something non-primary is in use. |
| 4124 | storage = device->storage; |
| 4125 | if (store != storage->id) { |
| 4126 | use_primary_storage = 0; |
Linus Walleij | 6bf68b4 | 2007-09-03 22:50:02 +0000 | [diff] [blame] | 4127 | } |
Linus Walleij | d208f9c | 2006-04-27 14:16:06 +0000 | [diff] [blame] | 4128 | |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 4129 | /* |
Linus Walleij | 05ccbe7 | 2006-06-13 07:46:58 +0000 | [diff] [blame] | 4130 | * If no destination folder was given, look up a default |
| 4131 | * folder if possible. Perhaps there is some way of retrieveing |
| 4132 | * the default folder for different forms of content, what |
| 4133 | * do I know, we use a fixed list in lack of any better method. |
| 4134 | * Some devices obviously need to have their files in certain |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 4135 | * folders in order to find/display them at all (hello Creative), |
Linus Walleij | c40c9bf | 2008-06-13 23:24:17 +0000 | [diff] [blame] | 4136 | * so we have to have a method for this. We only do this if the |
| 4137 | * primary storage is in use. |
Linus Walleij | 05ccbe7 | 2006-06-13 07:46:58 +0000 | [diff] [blame] | 4138 | */ |
| 4139 | |
Linus Walleij | c40c9bf | 2008-06-13 23:24:17 +0000 | [diff] [blame] | 4140 | if (localph == 0 && use_primary_storage) { |
Linus Walleij | 10e0ce7 | 2008-05-04 19:20:33 +0000 | [diff] [blame] | 4141 | if (LIBMTP_FILETYPE_IS_AUDIO(filedata->filetype)) { |
Linus Walleij | 05ccbe7 | 2006-06-13 07:46:58 +0000 | [diff] [blame] | 4142 | localph = device->default_music_folder; |
Linus Walleij | 10e0ce7 | 2008-05-04 19:20:33 +0000 | [diff] [blame] | 4143 | } else if (LIBMTP_FILETYPE_IS_VIDEO(filedata->filetype)) { |
Linus Walleij | 05ccbe7 | 2006-06-13 07:46:58 +0000 | [diff] [blame] | 4144 | localph = device->default_video_folder; |
| 4145 | } else if (of == PTP_OFC_EXIF_JPEG || |
Linus Walleij | 5fb4713 | 2006-12-30 15:35:48 +0000 | [diff] [blame] | 4146 | of == PTP_OFC_JP2 || |
| 4147 | of == PTP_OFC_JPX || |
Linus Walleij | 05ccbe7 | 2006-06-13 07:46:58 +0000 | [diff] [blame] | 4148 | of == PTP_OFC_JFIF || |
| 4149 | of == PTP_OFC_TIFF || |
Linus Walleij | 5fb4713 | 2006-12-30 15:35:48 +0000 | [diff] [blame] | 4150 | of == PTP_OFC_TIFF_IT || |
Linus Walleij | 05ccbe7 | 2006-06-13 07:46:58 +0000 | [diff] [blame] | 4151 | of == PTP_OFC_BMP || |
| 4152 | of == PTP_OFC_GIF || |
| 4153 | of == PTP_OFC_PICT || |
| 4154 | of == PTP_OFC_PNG || |
| 4155 | of == PTP_OFC_MTP_WindowsImageFormat) { |
| 4156 | localph = device->default_picture_folder; |
| 4157 | } else if (of == PTP_OFC_MTP_vCalendar1 || |
Linus Walleij | d7aa5b2 | 2006-09-02 11:52:31 +0000 | [diff] [blame] | 4158 | of == PTP_OFC_MTP_vCalendar2 || |
| 4159 | of == PTP_OFC_MTP_UndefinedContact || |
| 4160 | of == PTP_OFC_MTP_vCard2 || |
| 4161 | of == PTP_OFC_MTP_vCard3 || |
| 4162 | of == PTP_OFC_MTP_UndefinedCalendarItem) { |
Linus Walleij | 05ccbe7 | 2006-06-13 07:46:58 +0000 | [diff] [blame] | 4163 | localph = device->default_organizer_folder; |
Linus Walleij | 46651f3 | 2008-04-26 23:30:19 +0000 | [diff] [blame] | 4164 | } else if (of == PTP_OFC_Text) { |
Linus Walleij | 9316e65 | 2006-12-07 09:55:21 +0000 | [diff] [blame] | 4165 | localph = device->default_text_folder; |
Linus Walleij | 05ccbe7 | 2006-06-13 07:46:58 +0000 | [diff] [blame] | 4166 | } |
| 4167 | } |
mopoke | 3136444 | 2006-11-20 04:53:04 +0000 | [diff] [blame] | 4168 | |
Linus Walleij | a3544f6 | 2007-11-30 01:20:04 +0000 | [diff] [blame] | 4169 | // Here we wire the type to unknown on bugged, but |
| 4170 | // Ogg-supportive devices. |
Linus Walleij | fec4d56 | 2008-06-01 22:30:36 +0000 | [diff] [blame] | 4171 | if (FLAG_OGG_IS_UNKNOWN(ptp_usb) && of == PTP_OFC_MTP_OGG) { |
Linus Walleij | a3544f6 | 2007-11-30 01:20:04 +0000 | [diff] [blame] | 4172 | of = PTP_OFC_Undefined; |
| 4173 | } |
| 4174 | |
rreardon | bbb4e56 | 2006-11-19 22:16:11 +0000 | [diff] [blame] | 4175 | if (ptp_operation_issupported(params,PTP_OC_MTP_SendObjectPropList)) { |
Linus Walleij | d2778d1 | 2007-08-06 19:24:58 +0000 | [diff] [blame] | 4176 | /* |
| 4177 | * MTP enhanched does it this way (from a sniff): |
| 4178 | * -> PTP_OC_MTP_SendObjectPropList (0x9808): |
| 4179 | * 20 00 00 00 01 00 08 98 1B 00 00 00 01 00 01 00 |
| 4180 | * FF FF FF FF 00 30 00 00 00 00 00 00 12 5E 00 00 |
| 4181 | * Length: 0x00000020 |
| 4182 | * Type: 0x0001 PTP_USB_CONTAINER_COMMAND |
| 4183 | * Code: 0x9808 |
| 4184 | * Transaction ID: 0x0000001B |
| 4185 | * Param1: 0x00010001 <- store |
| 4186 | * Param2: 0xffffffff <- parent handle (-1 ?) |
| 4187 | * Param3: 0x00003000 <- file type PTP_OFC_Undefined - we don't know about PDF files |
| 4188 | * Param4: 0x00000000 <- file length MSB (-0x0c header len) |
| 4189 | * Param5: 0x00005e12 <- file length LSB (-0x0c header len) |
| 4190 | * |
| 4191 | * -> PTP_OC_MTP_SendObjectPropList (0x9808): |
| 4192 | * 46 00 00 00 02 00 08 98 1B 00 00 00 03 00 00 00 |
| 4193 | * 00 00 00 00 07 DC FF FF 0D 4B 00 53 00 30 00 36 - dc07 = file name |
| 4194 | * 00 30 00 33 00 30 00 36 00 2E 00 70 00 64 00 66 |
| 4195 | * 00 00 00 00 00 00 00 03 DC 04 00 00 00 00 00 00 - dc03 = protection status |
| 4196 | * 00 4F DC 02 00 01 - dc4f = non consumable |
| 4197 | * Length: 0x00000046 |
| 4198 | * Type: 0x0002 PTP_USB_CONTAINER_DATA |
| 4199 | * Code: 0x9808 |
| 4200 | * Transaction ID: 0x0000001B |
| 4201 | * Metadata.... |
| 4202 | * 0x00000003 <- Number of metadata items |
| 4203 | * 0x00000000 <- Object handle, set to 0x00000000 since it is unknown! |
| 4204 | * 0xdc07 <- metadata type: file name |
| 4205 | * 0xffff <- metadata type: string |
| 4206 | * 0x0d <- number of (uint16_t) characters |
| 4207 | * 4b 53 30 36 30 33 30 36 2e 50 64 66 00 "KS060306.pdf", null terminated |
| 4208 | * 0x00000000 <- Object handle, set to 0x00000000 since it is unknown! |
| 4209 | * 0xdc03 <- metadata type: protection status |
| 4210 | * 0x0004 <- metadata type: uint16_t |
| 4211 | * 0x0000 <- not protected |
| 4212 | * 0x00000000 <- Object handle, set to 0x00000000 since it is unknown! |
| 4213 | * 0xdc4f <- non consumable |
| 4214 | * 0x0002 <- metadata type: uint8_t |
| 4215 | * 0x01 <- non-consumable (this device cannot display PDF) |
| 4216 | * |
| 4217 | * <- Read 0x18 bytes back |
| 4218 | * 18 00 00 00 03 00 01 20 1B 00 00 00 01 00 01 00 |
| 4219 | * 00 00 00 00 01 40 00 00 |
| 4220 | * Length: 0x000000018 |
| 4221 | * Type: 0x0003 PTP_USB_CONTAINER_RESPONSE |
| 4222 | * Code: 0x2001 PTP_OK |
| 4223 | * Transaction ID: 0x0000001B |
| 4224 | * Param1: 0x00010001 <- store |
| 4225 | * Param2: 0x00000000 <- parent handle |
| 4226 | * Param3: 0x00004001 <- new file/object ID |
| 4227 | * |
| 4228 | * -> PTP_OC_SendObject (0x100d) |
| 4229 | * 0C 00 00 00 01 00 0D 10 1C 00 00 00 |
| 4230 | * -> ... all the bytes ... |
| 4231 | * <- Read 0x0c bytes back |
| 4232 | * 0C 00 00 00 03 00 01 20 1C 00 00 00 |
| 4233 | * ... Then update metadata one-by one, actually (instead of sending it first!) ... |
| 4234 | */ |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 4235 | MTPProperties *props = NULL; |
| 4236 | int nrofprops = 0; |
| 4237 | MTPProperties *prop = NULL; |
| 4238 | uint16_t *properties = NULL; |
Linus Walleij | 0779577 | 2007-08-06 18:44:06 +0000 | [diff] [blame] | 4239 | uint32_t propcnt = 0; |
Richard Low | 6c0a6ce | 2006-11-26 10:42:08 +0000 | [diff] [blame] | 4240 | |
Linus Walleij | 9036b33 | 2008-05-23 21:01:36 +0000 | [diff] [blame] | 4241 | // default parent handle |
Linus Walleij | 0779577 | 2007-08-06 18:44:06 +0000 | [diff] [blame] | 4242 | if (localph == 0) |
| 4243 | localph = 0xFFFFFFFFU; // Set to -1 |
| 4244 | |
Richard Low | 6c0a6ce | 2006-11-26 10:42:08 +0000 | [diff] [blame] | 4245 | // Must be 0x00000000U for new objects |
| 4246 | filedata->item_id = 0x00000000U; |
Linus Walleij | 05ccbe7 | 2006-06-13 07:46:58 +0000 | [diff] [blame] | 4247 | |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 4248 | ret = ptp_mtp_getobjectpropssupported(params, of, &propcnt, &properties); |
mopoke | 3136444 | 2006-11-20 04:53:04 +0000 | [diff] [blame] | 4249 | |
rreardon | bbb4e56 | 2006-11-19 22:16:11 +0000 | [diff] [blame] | 4250 | for (i=0;i<propcnt;i++) { |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 4251 | PTPObjectPropDesc opd; |
| 4252 | |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 4253 | ret = ptp_mtp_getobjectpropdesc(params, properties[i], of, &opd); |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 4254 | if (ret != PTP_RC_OK) { |
Linus Walleij | 0779577 | 2007-08-06 18:44:06 +0000 | [diff] [blame] | 4255 | add_ptp_error_to_errorstack(device, ret, "LIBMTP_Send_File_From_File_Descriptor(): " |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 4256 | "could not get property description."); |
| 4257 | } else if (opd.GetSet) { |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 4258 | switch (properties[i]) { |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 4259 | case PTP_OPC_ObjectFileName: |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 4260 | prop = ptp_get_new_object_prop_entry(&props,&nrofprops); |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 4261 | prop->ObjectHandle = filedata->item_id; |
| 4262 | prop->property = PTP_OPC_ObjectFileName; |
| 4263 | prop->datatype = PTP_DTC_STR; |
Linus Walleij | d3b7857 | 2007-08-24 21:28:24 +0000 | [diff] [blame] | 4264 | if (filedata->filename != NULL) { |
Linus Walleij | 0779577 | 2007-08-06 18:44:06 +0000 | [diff] [blame] | 4265 | prop->propval.str = strdup(filedata->filename); |
Linus Walleij | fec4d56 | 2008-06-01 22:30:36 +0000 | [diff] [blame] | 4266 | if (FLAG_ONLY_7BIT_FILENAMES(ptp_usb)) { |
Linus Walleij | d3b7857 | 2007-08-24 21:28:24 +0000 | [diff] [blame] | 4267 | strip_7bit_from_utf8(prop->propval.str); |
| 4268 | } |
| 4269 | } |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 4270 | break; |
| 4271 | case PTP_OPC_ProtectionStatus: |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 4272 | prop = ptp_get_new_object_prop_entry(&props,&nrofprops); |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 4273 | prop->ObjectHandle = filedata->item_id; |
| 4274 | prop->property = PTP_OPC_ProtectionStatus; |
| 4275 | prop->datatype = PTP_DTC_UINT16; |
| 4276 | prop->propval.u16 = 0x0000U; /* Not protected */ |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 4277 | break; |
| 4278 | case PTP_OPC_NonConsumable: |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 4279 | prop = ptp_get_new_object_prop_entry(&props,&nrofprops); |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 4280 | prop->ObjectHandle = filedata->item_id; |
| 4281 | prop->property = PTP_OPC_NonConsumable; |
| 4282 | prop->datatype = PTP_DTC_UINT8; |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 4283 | prop->propval.u8 = 0x00; /* It is supported, then it is consumable */ |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 4284 | break; |
| 4285 | case PTP_OPC_Name: |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 4286 | prop = ptp_get_new_object_prop_entry(&props,&nrofprops); |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 4287 | prop->ObjectHandle = filedata->item_id; |
| 4288 | prop->property = PTP_OPC_Name; |
| 4289 | prop->datatype = PTP_DTC_STR; |
Linus Walleij | 0779577 | 2007-08-06 18:44:06 +0000 | [diff] [blame] | 4290 | if (filedata->filename != NULL) |
| 4291 | prop->propval.str = strdup(filedata->filename); |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 4292 | break; |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 4293 | case PTP_OPC_DateModified: |
| 4294 | // Tag with current time if that is supported |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 4295 | prop = ptp_get_new_object_prop_entry(&props,&nrofprops); |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 4296 | prop->ObjectHandle = filedata->item_id; |
| 4297 | prop->property = PTP_OPC_DateModified; |
| 4298 | prop->datatype = PTP_DTC_STR; |
| 4299 | prop->propval.str = get_iso8601_stamp(); |
| 4300 | break; |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 4301 | } |
rreardon | bbb4e56 | 2006-11-19 22:16:11 +0000 | [diff] [blame] | 4302 | } |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 4303 | ptp_free_objectpropdesc(&opd); |
Linus Walleij | 99310d4 | 2006-11-01 08:29:39 +0000 | [diff] [blame] | 4304 | } |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 4305 | free(properties); |
mopoke | 3136444 | 2006-11-20 04:53:04 +0000 | [diff] [blame] | 4306 | |
rreardon | bbb4e56 | 2006-11-19 22:16:11 +0000 | [diff] [blame] | 4307 | ret = ptp_mtp_sendobjectproplist(params, &store, &localph, &filedata->item_id, |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 4308 | of, filedata->filesize, props, nrofprops); |
mopoke | 3136444 | 2006-11-20 04:53:04 +0000 | [diff] [blame] | 4309 | |
rreardon | bbb4e56 | 2006-11-19 22:16:11 +0000 | [diff] [blame] | 4310 | /* Free property list */ |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 4311 | ptp_destroy_object_prop_list(props, nrofprops); |
mopoke | 3136444 | 2006-11-20 04:53:04 +0000 | [diff] [blame] | 4312 | |
rreardon | bbb4e56 | 2006-11-19 22:16:11 +0000 | [diff] [blame] | 4313 | if (ret != PTP_RC_OK) { |
Linus Walleij | 0779577 | 2007-08-06 18:44:06 +0000 | [diff] [blame] | 4314 | add_ptp_error_to_errorstack(device, ret, "LIBMTP_Send_File_From_File_Descriptor():" |
| 4315 | "Could not send object property list."); |
rreardon | bbb4e56 | 2006-11-19 22:16:11 +0000 | [diff] [blame] | 4316 | if (ret == PTP_RC_AccessDenied) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 4317 | add_ptp_error_to_errorstack(device, ret, "ACCESS DENIED."); |
rreardon | bbb4e56 | 2006-11-19 22:16:11 +0000 | [diff] [blame] | 4318 | } |
| 4319 | return -1; |
| 4320 | } |
| 4321 | } else if (ptp_operation_issupported(params,PTP_OC_SendObjectInfo)) { |
Linus Walleij | 0779577 | 2007-08-06 18:44:06 +0000 | [diff] [blame] | 4322 | PTPObjectInfo new_file; |
| 4323 | |
| 4324 | memset(&new_file, 0, sizeof(PTPObjectInfo)); |
| 4325 | |
| 4326 | new_file.Filename = filedata->filename; |
Linus Walleij | fec4d56 | 2008-06-01 22:30:36 +0000 | [diff] [blame] | 4327 | if (FLAG_ONLY_7BIT_FILENAMES(ptp_usb)) { |
Linus Walleij | d3b7857 | 2007-08-24 21:28:24 +0000 | [diff] [blame] | 4328 | strip_7bit_from_utf8(new_file.Filename); |
| 4329 | } |
Linus Walleij | 1b87ea7 | 2007-08-28 07:53:09 +0000 | [diff] [blame] | 4330 | // We loose precision here. |
| 4331 | new_file.ObjectCompressedSize = (uint32_t) filedata->filesize; |
Linus Walleij | 0779577 | 2007-08-06 18:44:06 +0000 | [diff] [blame] | 4332 | new_file.ObjectFormat = of; |
| 4333 | new_file.StorageID = store; |
| 4334 | new_file.ParentObject = localph; |
| 4335 | |
Linus Walleij | 9be685b | 2006-11-21 09:44:53 +0000 | [diff] [blame] | 4336 | // Create the object |
| 4337 | ret = ptp_sendobjectinfo(params, &store, &localph, &filedata->item_id, &new_file); |
Linus Walleij | 0779577 | 2007-08-06 18:44:06 +0000 | [diff] [blame] | 4338 | |
Linus Walleij | 9be685b | 2006-11-21 09:44:53 +0000 | [diff] [blame] | 4339 | if (ret != PTP_RC_OK) { |
Linus Walleij | 0779577 | 2007-08-06 18:44:06 +0000 | [diff] [blame] | 4340 | add_ptp_error_to_errorstack(device, ret, "LIBMTP_Send_File_From_File_Descriptor(): " |
| 4341 | "Could not send object info."); |
Linus Walleij | 9be685b | 2006-11-21 09:44:53 +0000 | [diff] [blame] | 4342 | if (ret == PTP_RC_AccessDenied) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 4343 | add_ptp_error_to_errorstack(device, ret, "ACCESS DENIED."); |
Linus Walleij | 9be685b | 2006-11-21 09:44:53 +0000 | [diff] [blame] | 4344 | } |
| 4345 | return -1; |
| 4346 | } |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 4347 | // NOTE: the char* pointers inside new_file are not copies so don't |
| 4348 | // try to destroy this objectinfo! |
rreardon | bbb4e56 | 2006-11-19 22:16:11 +0000 | [diff] [blame] | 4349 | } |
Linus Walleij | d2778d1 | 2007-08-06 19:24:58 +0000 | [diff] [blame] | 4350 | |
| 4351 | // Now there IS an object with this parent handle. |
| 4352 | filedata->parent_id = localph; |
Linus Walleij | 817579a | 2008-05-23 21:21:40 +0000 | [diff] [blame] | 4353 | |
Linus Walleij | 1b87ea7 | 2007-08-28 07:53:09 +0000 | [diff] [blame] | 4354 | // Callbacks |
| 4355 | ptp_usb->callback_active = 1; |
| 4356 | // The callback will deactivate itself after this amount of data has been sent |
| 4357 | // One BULK header for the request, one for the data phase. No parameters to the request. |
| 4358 | ptp_usb->current_transfer_total = filedata->filesize+PTP_USB_BULK_HDR_LEN*2; |
| 4359 | ptp_usb->current_transfer_complete = 0; |
| 4360 | ptp_usb->current_transfer_callback = callback; |
| 4361 | ptp_usb->current_transfer_callback_data = data; |
| 4362 | |
| 4363 | ret = ptp_sendobject_fromfd(params, fd, filedata->filesize); |
| 4364 | |
| 4365 | ptp_usb->callback_active = 0; |
| 4366 | ptp_usb->current_transfer_callback = NULL; |
| 4367 | ptp_usb->current_transfer_callback_data = NULL; |
Linus Walleij | bd3bf9e | 2007-09-14 19:31:54 +0000 | [diff] [blame] | 4368 | |
| 4369 | if (ret == PTP_ERROR_CANCEL) { |
Linus Walleij | 25d33b5 | 2007-09-16 21:36:19 +0000 | [diff] [blame] | 4370 | add_error_to_errorstack(device, LIBMTP_ERROR_CANCELLED, "LIBMTP_Send_File_From_File_Descriptor(): Cancelled transfer."); |
Linus Walleij | bd3bf9e | 2007-09-14 19:31:54 +0000 | [diff] [blame] | 4371 | return -1; |
Linus Walleij | 25d33b5 | 2007-09-16 21:36:19 +0000 | [diff] [blame] | 4372 | } |
Linus Walleij | ee73ef2 | 2006-08-27 19:56:00 +0000 | [diff] [blame] | 4373 | if (ret != PTP_RC_OK) { |
Linus Walleij | d2778d1 | 2007-08-06 19:24:58 +0000 | [diff] [blame] | 4374 | add_ptp_error_to_errorstack(device, ret, "LIBMTP_Send_File_From_File_Descriptor(): " |
Linus Walleij | 0779577 | 2007-08-06 18:44:06 +0000 | [diff] [blame] | 4375 | "Could not send object."); |
Linus Walleij | ee73ef2 | 2006-08-27 19:56:00 +0000 | [diff] [blame] | 4376 | return -1; |
| 4377 | } |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 4378 | |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 4379 | add_object_to_cache(device, filedata->item_id); |
Linus Walleij | 817579a | 2008-05-23 21:21:40 +0000 | [diff] [blame] | 4380 | |
| 4381 | /* |
| 4382 | * Get the device-assined parent_id from the cache. |
| 4383 | * The operation that adds it to the cache will |
| 4384 | * look it up from the device, so we get the new |
| 4385 | * parent_id from the cache. |
| 4386 | */ |
| 4387 | newfilemeta = LIBMTP_Get_Filemetadata(device, filedata->item_id); |
| 4388 | if (newfilemeta != NULL) { |
| 4389 | filedata->parent_id = newfilemeta->parent_id; |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 4390 | filedata->storage_id = newfilemeta->storage_id; |
Linus Walleij | 817579a | 2008-05-23 21:21:40 +0000 | [diff] [blame] | 4391 | LIBMTP_destroy_file_t(newfilemeta); |
| 4392 | } else { |
| 4393 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, |
| 4394 | "LIBMTP_Send_File_From_File_Descriptor(): " |
| 4395 | "Could not retrieve updated metadata."); |
| 4396 | return -1; |
| 4397 | } |
tsaarni | a3eb60a | 2007-07-06 17:41:30 +0000 | [diff] [blame] | 4398 | |
Linus Walleij | d208f9c | 2006-04-27 14:16:06 +0000 | [diff] [blame] | 4399 | return 0; |
| 4400 | } |
| 4401 | |
Linus Walleij | 17e39f7 | 2006-02-23 15:54:28 +0000 | [diff] [blame] | 4402 | /** |
Linus Walleij | 9036b33 | 2008-05-23 21:01:36 +0000 | [diff] [blame] | 4403 | * This function updates the MTP track object metadata on a |
| 4404 | * single file identified by an object ID. |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 4405 | * @param device a pointer to the device to update the track |
Linus Walleij | 95698cd | 2006-02-24 10:40:40 +0000 | [diff] [blame] | 4406 | * metadata on. |
Linus Walleij | 17e39f7 | 2006-02-23 15:54:28 +0000 | [diff] [blame] | 4407 | * @param metadata a track metadata set to be written to the file. |
| 4408 | * notice that the <code>track_id</code> field of the |
| 4409 | * metadata structure must be correct so that the |
Linus Walleij | a498273 | 2006-02-24 15:46:02 +0000 | [diff] [blame] | 4410 | * function can update the right file. If some properties |
| 4411 | * of this metadata are set to NULL (strings) or 0 |
| 4412 | * (numerical values) they will be discarded and the |
| 4413 | * track will not be tagged with these blank values. |
Richard Low | af20b5d | 2006-12-17 18:00:59 +0000 | [diff] [blame] | 4414 | * @return 0 on success, any other value means failure. If some |
| 4415 | * or all of the properties fail to update we will still |
| 4416 | * return success. On some devices (notably iRiver T30) |
| 4417 | * properties that exist cannot be updated. |
Linus Walleij | 17e39f7 | 2006-02-23 15:54:28 +0000 | [diff] [blame] | 4418 | */ |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 4419 | int LIBMTP_Update_Track_Metadata(LIBMTP_mtpdevice_t *device, |
Linus Walleij | 17e39f7 | 2006-02-23 15:54:28 +0000 | [diff] [blame] | 4420 | LIBMTP_track_t const * const metadata) |
| 4421 | { |
Linus Walleij | 17e39f7 | 2006-02-23 15:54:28 +0000 | [diff] [blame] | 4422 | uint16_t ret; |
Linus Walleij | 00cf064 | 2006-07-26 20:40:59 +0000 | [diff] [blame] | 4423 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | f9267e9 | 2007-10-15 21:07:37 +0000 | [diff] [blame] | 4424 | PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo; |
Linus Walleij | 00cf064 | 2006-07-26 20:40:59 +0000 | [diff] [blame] | 4425 | uint32_t i; |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 4426 | uint16_t *properties = NULL; |
Linus Walleij | 00cf064 | 2006-07-26 20:40:59 +0000 | [diff] [blame] | 4427 | uint32_t propcnt = 0; |
Linus Walleij | 17e39f7 | 2006-02-23 15:54:28 +0000 | [diff] [blame] | 4428 | |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 4429 | // 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] | 4430 | // 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] | 4431 | 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] | 4432 | if (ret != PTP_RC_OK) { |
| 4433 | // Just bail out for now, nothing is ever set. |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 4434 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): " |
| 4435 | "could not retrieve supported object properties."); |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 4436 | return -1; |
Richard Low | 15731fe | 2007-03-22 20:27:20 +0000 | [diff] [blame] | 4437 | } |
Linus Walleij | f9267e9 | 2007-10-15 21:07:37 +0000 | [diff] [blame] | 4438 | if (ptp_operation_issupported(params, PTP_OC_MTP_SetObjPropList) && |
Linus Walleij | fec4d56 | 2008-06-01 22:30:36 +0000 | [diff] [blame] | 4439 | !FLAG_BROKEN_SET_OBJECT_PROPLIST(ptp_usb)) { |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 4440 | MTPProperties *props = NULL; |
| 4441 | MTPProperties *prop = NULL; |
| 4442 | int nrofprops = 0; |
Richard Low | 15731fe | 2007-03-22 20:27:20 +0000 | [diff] [blame] | 4443 | |
| 4444 | for (i=0;i<propcnt;i++) { |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 4445 | PTPObjectPropDesc opd; |
| 4446 | |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 4447 | 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] | 4448 | if (ret != PTP_RC_OK) { |
| 4449 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): " |
| 4450 | "could not get property description."); |
| 4451 | } else if (opd.GetSet) { |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 4452 | switch (properties[i]) { |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 4453 | case PTP_OPC_Name: |
| 4454 | if (metadata->title == NULL) |
| 4455 | break; |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 4456 | prop = ptp_get_new_object_prop_entry(&props, &nrofprops); |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 4457 | prop->ObjectHandle = metadata->item_id; |
| 4458 | prop->property = PTP_OPC_Name; |
| 4459 | prop->datatype = PTP_DTC_STR; |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 4460 | prop->propval.str = strdup(metadata->title); |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 4461 | break; |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 4462 | case PTP_OPC_AlbumName: |
| 4463 | if (metadata->album == NULL) |
| 4464 | break; |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 4465 | prop = ptp_get_new_object_prop_entry(&props, &nrofprops); |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 4466 | prop->ObjectHandle = metadata->item_id; |
| 4467 | prop->property = PTP_OPC_AlbumName; |
| 4468 | prop->datatype = PTP_DTC_STR; |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 4469 | prop->propval.str = strdup(metadata->album); |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 4470 | break; |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 4471 | case PTP_OPC_Artist: |
| 4472 | if (metadata->artist == NULL) |
| 4473 | break; |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 4474 | prop = ptp_get_new_object_prop_entry(&props, &nrofprops); |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 4475 | prop->ObjectHandle = metadata->item_id; |
| 4476 | prop->property = PTP_OPC_Artist; |
| 4477 | prop->datatype = PTP_DTC_STR; |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 4478 | prop->propval.str = strdup(metadata->artist); |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 4479 | break; |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 4480 | case PTP_OPC_Composer: |
| 4481 | if (metadata->composer == NULL) |
| 4482 | break; |
| 4483 | prop = ptp_get_new_object_prop_entry(&props, &nrofprops); |
| 4484 | prop->ObjectHandle = metadata->item_id; |
| 4485 | prop->property = PTP_OPC_Composer; |
| 4486 | prop->datatype = PTP_DTC_STR; |
| 4487 | prop->propval.str = strdup(metadata->composer); |
| 4488 | break; |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 4489 | case PTP_OPC_Genre: |
| 4490 | if (metadata->genre == NULL) |
| 4491 | break; |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 4492 | prop = ptp_get_new_object_prop_entry(&props, &nrofprops); |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 4493 | prop->ObjectHandle = metadata->item_id; |
| 4494 | prop->property = PTP_OPC_Genre; |
| 4495 | prop->datatype = PTP_DTC_STR; |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 4496 | prop->propval.str = strdup(metadata->genre); |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 4497 | break; |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 4498 | case PTP_OPC_Duration: |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 4499 | prop = ptp_get_new_object_prop_entry(&props, &nrofprops); |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 4500 | prop->ObjectHandle = metadata->item_id; |
| 4501 | prop->property = PTP_OPC_Duration; |
| 4502 | prop->datatype = PTP_DTC_UINT32; |
Linus Walleij | 2955956 | 2007-08-22 21:38:04 +0000 | [diff] [blame] | 4503 | prop->propval.u32 = adjust_u32(metadata->duration, &opd); |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 4504 | break; |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 4505 | case PTP_OPC_Track: |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 4506 | prop = ptp_get_new_object_prop_entry(&props, &nrofprops); |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 4507 | prop->ObjectHandle = metadata->item_id; |
| 4508 | prop->property = PTP_OPC_Track; |
| 4509 | prop->datatype = PTP_DTC_UINT16; |
Linus Walleij | 2955956 | 2007-08-22 21:38:04 +0000 | [diff] [blame] | 4510 | prop->propval.u16 = adjust_u16(metadata->tracknumber, &opd); |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 4511 | break; |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 4512 | case PTP_OPC_OriginalReleaseDate: |
| 4513 | if (metadata->date == NULL) |
| 4514 | break; |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 4515 | prop = ptp_get_new_object_prop_entry(&props, &nrofprops); |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 4516 | prop->ObjectHandle = metadata->item_id; |
| 4517 | prop->property = PTP_OPC_OriginalReleaseDate; |
| 4518 | prop->datatype = PTP_DTC_STR; |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 4519 | prop->propval.str = strdup(metadata->date); |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 4520 | break; |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 4521 | case PTP_OPC_SampleRate: |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 4522 | prop = ptp_get_new_object_prop_entry(&props, &nrofprops); |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 4523 | prop->ObjectHandle = metadata->item_id; |
| 4524 | prop->property = PTP_OPC_SampleRate; |
| 4525 | prop->datatype = PTP_DTC_UINT32; |
Linus Walleij | 2955956 | 2007-08-22 21:38:04 +0000 | [diff] [blame] | 4526 | prop->propval.u32 = adjust_u32(metadata->samplerate, &opd); |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 4527 | break; |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 4528 | case PTP_OPC_NumberOfChannels: |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 4529 | prop = ptp_get_new_object_prop_entry(&props, &nrofprops); |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 4530 | prop->ObjectHandle = metadata->item_id; |
| 4531 | prop->property = PTP_OPC_NumberOfChannels; |
| 4532 | prop->datatype = PTP_DTC_UINT16; |
Linus Walleij | 2955956 | 2007-08-22 21:38:04 +0000 | [diff] [blame] | 4533 | prop->propval.u16 = adjust_u16(metadata->nochannels, &opd); |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 4534 | break; |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 4535 | case PTP_OPC_AudioWAVECodec: |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 4536 | prop = ptp_get_new_object_prop_entry(&props, &nrofprops); |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 4537 | prop->ObjectHandle = metadata->item_id; |
| 4538 | prop->property = PTP_OPC_AudioWAVECodec; |
| 4539 | prop->datatype = PTP_DTC_UINT32; |
Linus Walleij | 2955956 | 2007-08-22 21:38:04 +0000 | [diff] [blame] | 4540 | prop->propval.u32 = adjust_u32(metadata->wavecodec, &opd); |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 4541 | break; |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 4542 | case PTP_OPC_AudioBitRate: |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 4543 | prop = ptp_get_new_object_prop_entry(&props, &nrofprops); |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 4544 | prop->ObjectHandle = metadata->item_id; |
| 4545 | prop->property = PTP_OPC_AudioBitRate; |
| 4546 | prop->datatype = PTP_DTC_UINT32; |
Linus Walleij | 2955956 | 2007-08-22 21:38:04 +0000 | [diff] [blame] | 4547 | prop->propval.u32 = adjust_u32(metadata->bitrate, &opd); |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 4548 | break; |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 4549 | case PTP_OPC_BitRateType: |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 4550 | prop = ptp_get_new_object_prop_entry(&props, &nrofprops); |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 4551 | prop->ObjectHandle = metadata->item_id; |
| 4552 | prop->property = PTP_OPC_BitRateType; |
| 4553 | prop->datatype = PTP_DTC_UINT16; |
Linus Walleij | 2955956 | 2007-08-22 21:38:04 +0000 | [diff] [blame] | 4554 | prop->propval.u16 = adjust_u16(metadata->bitratetype, &opd); |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 4555 | break; |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 4556 | case PTP_OPC_Rating: |
| 4557 | // TODO: shall this be set for rating 0? |
| 4558 | if (metadata->rating == 0) |
| 4559 | break; |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 4560 | prop = ptp_get_new_object_prop_entry(&props, &nrofprops); |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 4561 | prop->ObjectHandle = metadata->item_id; |
| 4562 | prop->property = PTP_OPC_Rating; |
| 4563 | prop->datatype = PTP_DTC_UINT16; |
Linus Walleij | 2955956 | 2007-08-22 21:38:04 +0000 | [diff] [blame] | 4564 | prop->propval.u16 = adjust_u16(metadata->rating, &opd); |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 4565 | break; |
| 4566 | case PTP_OPC_UseCount: |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 4567 | prop = ptp_get_new_object_prop_entry(&props, &nrofprops); |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 4568 | prop->ObjectHandle = metadata->item_id; |
| 4569 | prop->property = PTP_OPC_UseCount; |
| 4570 | prop->datatype = PTP_DTC_UINT32; |
Linus Walleij | 2955956 | 2007-08-22 21:38:04 +0000 | [diff] [blame] | 4571 | prop->propval.u32 = adjust_u32(metadata->usecount, &opd); |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 4572 | break; |
| 4573 | case PTP_OPC_DateModified: |
| 4574 | // Tag with current time if that is supported |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 4575 | prop = ptp_get_new_object_prop_entry(&props, &nrofprops); |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 4576 | prop->ObjectHandle = metadata->item_id; |
| 4577 | prop->property = PTP_OPC_DateModified; |
| 4578 | prop->datatype = PTP_DTC_STR; |
| 4579 | prop->propval.str = get_iso8601_stamp(); |
| 4580 | break; |
Linus Walleij | f9267e9 | 2007-10-15 21:07:37 +0000 | [diff] [blame] | 4581 | default: |
| 4582 | break; |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 4583 | } |
Linus Walleij | 304433d | 2007-02-26 08:02:47 +0000 | [diff] [blame] | 4584 | } |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 4585 | ptp_free_objectpropdesc(&opd); |
Richard Low | 15731fe | 2007-03-22 20:27:20 +0000 | [diff] [blame] | 4586 | } |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 4587 | |
Richard Low | 15731fe | 2007-03-22 20:27:20 +0000 | [diff] [blame] | 4588 | // NOTE: File size is not updated, this should not change anyway. |
| 4589 | // neither will we change the filename. |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 4590 | |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 4591 | ret = ptp_mtp_setobjectproplist(params, props, nrofprops); |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 4592 | |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 4593 | ptp_destroy_object_prop_list(props, nrofprops); |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 4594 | |
Richard Low | 15731fe | 2007-03-22 20:27:20 +0000 | [diff] [blame] | 4595 | if (ret != PTP_RC_OK) { |
| 4596 | // TODO: return error of which property we couldn't set |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 4597 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): " |
| 4598 | "could not set object property list."); |
Richard Low | 016e373 | 2007-09-23 14:53:46 +0000 | [diff] [blame] | 4599 | free(properties); |
Linus Walleij | 304433d | 2007-02-26 08:02:47 +0000 | [diff] [blame] | 4600 | return -1; |
| 4601 | } |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 4602 | |
Richard Low | 15731fe | 2007-03-22 20:27:20 +0000 | [diff] [blame] | 4603 | } else if (ptp_operation_issupported(params,PTP_OC_MTP_SetObjectPropValue)) { |
Linus Walleij | 00cf064 | 2006-07-26 20:40:59 +0000 | [diff] [blame] | 4604 | for (i=0;i<propcnt;i++) { |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 4605 | PTPObjectPropDesc opd; |
| 4606 | |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 4607 | 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] | 4608 | if (ret != PTP_RC_OK) { |
| 4609 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): " |
| 4610 | "could not get property description."); |
| 4611 | } else if (opd.GetSet) { |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 4612 | switch (properties[i]) { |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 4613 | case PTP_OPC_Name: |
| 4614 | // Update title |
| 4615 | 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] | 4616 | if (ret != 0) { |
| 4617 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): " |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 4618 | "could not set track title."); |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 4619 | } |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 4620 | break; |
| 4621 | case PTP_OPC_AlbumName: |
| 4622 | // Update album |
| 4623 | 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] | 4624 | if (ret != 0) { |
| 4625 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): " |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 4626 | "could not set track album name."); |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 4627 | } |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 4628 | break; |
| 4629 | case PTP_OPC_Artist: |
| 4630 | // Update artist |
| 4631 | 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] | 4632 | if (ret != 0) { |
| 4633 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): " |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 4634 | "could not set track artist name."); |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 4635 | } |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 4636 | break; |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 4637 | case PTP_OPC_Composer: |
| 4638 | // Update composer |
| 4639 | ret = set_object_string(device, metadata->item_id, PTP_OPC_Composer, metadata->composer); |
| 4640 | if (ret != 0) { |
| 4641 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): " |
| 4642 | "could not set track composer name."); |
| 4643 | } |
| 4644 | break; |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 4645 | case PTP_OPC_Genre: |
| 4646 | // Update genre |
| 4647 | ret = set_object_string(device, metadata->item_id, PTP_OPC_Genre, metadata->genre); |
| 4648 | if (ret != 0) { |
| 4649 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): " |
| 4650 | "could not set track genre name."); |
| 4651 | } |
| 4652 | break; |
| 4653 | case PTP_OPC_Duration: |
| 4654 | // Update duration |
| 4655 | if (metadata->duration != 0) { |
Linus Walleij | 2955956 | 2007-08-22 21:38:04 +0000 | [diff] [blame] | 4656 | 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] | 4657 | if (ret != 0) { |
| 4658 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): " |
| 4659 | "could not set track duration."); |
| 4660 | } |
| 4661 | } |
| 4662 | break; |
| 4663 | case PTP_OPC_Track: |
| 4664 | // Update track number. |
| 4665 | if (metadata->tracknumber != 0) { |
Linus Walleij | 2955956 | 2007-08-22 21:38:04 +0000 | [diff] [blame] | 4666 | 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] | 4667 | if (ret != 0) { |
| 4668 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): " |
| 4669 | "could not set track tracknumber."); |
| 4670 | } |
| 4671 | } |
| 4672 | break; |
| 4673 | case PTP_OPC_OriginalReleaseDate: |
| 4674 | // Update creation datetime |
| 4675 | ret = set_object_string(device, metadata->item_id, PTP_OPC_OriginalReleaseDate, metadata->date); |
| 4676 | if (ret != 0) { |
| 4677 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): " |
| 4678 | "could not set track release date."); |
| 4679 | } |
| 4680 | break; |
| 4681 | // These are, well not so important. |
| 4682 | case PTP_OPC_SampleRate: |
| 4683 | // Update sample rate |
| 4684 | if (metadata->samplerate != 0) { |
Linus Walleij | 2955956 | 2007-08-22 21:38:04 +0000 | [diff] [blame] | 4685 | 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] | 4686 | if (ret != 0) { |
| 4687 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): " |
| 4688 | "could not set samplerate."); |
| 4689 | } |
| 4690 | } |
| 4691 | break; |
| 4692 | case PTP_OPC_NumberOfChannels: |
| 4693 | // Update number of channels |
| 4694 | if (metadata->nochannels != 0) { |
Linus Walleij | 2955956 | 2007-08-22 21:38:04 +0000 | [diff] [blame] | 4695 | 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] | 4696 | if (ret != 0) { |
| 4697 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): " |
| 4698 | "could not set number of channels."); |
| 4699 | } |
| 4700 | } |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 4701 | break; |
| 4702 | case PTP_OPC_AudioWAVECodec: |
| 4703 | // Update WAVE codec |
| 4704 | if (metadata->wavecodec != 0) { |
Linus Walleij | 2955956 | 2007-08-22 21:38:04 +0000 | [diff] [blame] | 4705 | 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] | 4706 | if (ret != 0) { |
| 4707 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): " |
| 4708 | "could not set WAVE codec."); |
| 4709 | } |
| 4710 | } |
| 4711 | break; |
| 4712 | case PTP_OPC_AudioBitRate: |
| 4713 | // Update bitrate |
| 4714 | if (metadata->bitrate != 0) { |
Linus Walleij | 2955956 | 2007-08-22 21:38:04 +0000 | [diff] [blame] | 4715 | 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] | 4716 | if (ret != 0) { |
| 4717 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): " |
| 4718 | "could not set bitrate."); |
| 4719 | } |
| 4720 | } |
| 4721 | break; |
| 4722 | case PTP_OPC_BitRateType: |
| 4723 | // Update bitrate type |
| 4724 | if (metadata->bitratetype != 0) { |
Linus Walleij | 2955956 | 2007-08-22 21:38:04 +0000 | [diff] [blame] | 4725 | 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] | 4726 | if (ret != 0) { |
| 4727 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): " |
| 4728 | "could not set bitratetype."); |
| 4729 | } |
| 4730 | } |
| 4731 | break; |
| 4732 | case PTP_OPC_Rating: |
| 4733 | // Update user rating |
| 4734 | // TODO: shall this be set for rating 0? |
| 4735 | if (metadata->rating != 0) { |
Linus Walleij | 2955956 | 2007-08-22 21:38:04 +0000 | [diff] [blame] | 4736 | 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] | 4737 | if (ret != 0) { |
| 4738 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): " |
| 4739 | "could not set user rating."); |
| 4740 | } |
| 4741 | } |
| 4742 | break; |
| 4743 | case PTP_OPC_UseCount: |
| 4744 | // Update use count, set even to zero if desired. |
Linus Walleij | 2955956 | 2007-08-22 21:38:04 +0000 | [diff] [blame] | 4745 | 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] | 4746 | if (ret != 0) { |
| 4747 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): " |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 4748 | "could not set use count."); |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 4749 | } |
| 4750 | break; |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 4751 | case PTP_OPC_DateModified: |
| 4752 | { |
| 4753 | // Update modification time if supported |
| 4754 | char *tmpstamp = get_iso8601_stamp(); |
| 4755 | ret = set_object_string(device, metadata->item_id, PTP_OPC_DateModified, tmpstamp); |
| 4756 | if (ret != 0) { |
| 4757 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): " |
| 4758 | "could not set modification date."); |
| 4759 | } |
| 4760 | free(tmpstamp); |
| 4761 | } |
| 4762 | break; |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 4763 | |
| 4764 | // NOTE: File size is not updated, this should not change anyway. |
| 4765 | // neither will we change the filename. |
| 4766 | default: |
| 4767 | break; |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 4768 | } |
Linus Walleij | 00cf064 | 2006-07-26 20:40:59 +0000 | [diff] [blame] | 4769 | } |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 4770 | ptp_free_objectpropdesc(&opd); |
Linus Walleij | a498273 | 2006-02-24 15:46:02 +0000 | [diff] [blame] | 4771 | } |
Richard Low | 15731fe | 2007-03-22 20:27:20 +0000 | [diff] [blame] | 4772 | } else { |
| 4773 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): " |
| 4774 | "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] | 4775 | free(properties); |
Richard Low | 15731fe | 2007-03-22 20:27:20 +0000 | [diff] [blame] | 4776 | return -1; |
Linus Walleij | 17e39f7 | 2006-02-23 15:54:28 +0000 | [diff] [blame] | 4777 | } |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 4778 | |
tsaarni | a3eb60a | 2007-07-06 17:41:30 +0000 | [diff] [blame] | 4779 | // update cached object properties if metadata cache exists |
Linus Walleij | 7752b95 | 2007-10-19 20:11:46 +0000 | [diff] [blame] | 4780 | update_metadata_cache(device, metadata->item_id); |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 4781 | |
Richard Low | 016e373 | 2007-09-23 14:53:46 +0000 | [diff] [blame] | 4782 | free(properties); |
| 4783 | |
Linus Walleij | f1b02f2 | 2006-12-06 09:03:23 +0000 | [diff] [blame] | 4784 | return 0; |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 4785 | } |
Linus Walleij | 95698cd | 2006-02-24 10:40:40 +0000 | [diff] [blame] | 4786 | |
| 4787 | /** |
Linus Walleij | 399807b | 2008-01-27 22:05:12 +0000 | [diff] [blame] | 4788 | * This function deletes a single file, track, playlist, folder or |
| 4789 | * any other object off the MTP device, identified by the object ID. |
| 4790 | * |
| 4791 | * If you delete a folder, there is no guarantee that the device will |
| 4792 | * really delete all the files that were in that folder, rather it is |
| 4793 | * expected that they will not be deleted, and will turn up in object |
| 4794 | * listings with parent set to a non-existant object ID. The safe way |
| 4795 | * to do this is to recursively delete all files (and folders) contained |
| 4796 | * in the folder, then the folder itself. |
| 4797 | * |
| 4798 | * @param device a pointer to the device to delete the object from. |
| 4799 | * @param object_id the object to delete. |
Linus Walleij | 95698cd | 2006-02-24 10:40:40 +0000 | [diff] [blame] | 4800 | * @return 0 on success, any other value means failure. |
| 4801 | */ |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 4802 | int LIBMTP_Delete_Object(LIBMTP_mtpdevice_t *device, |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 4803 | uint32_t object_id) |
Linus Walleij | 95698cd | 2006-02-24 10:40:40 +0000 | [diff] [blame] | 4804 | { |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 4805 | uint16_t ret; |
| 4806 | PTPParams *params = (PTPParams *) device->params; |
| 4807 | |
| 4808 | ret = ptp_deleteobject(params, object_id, 0); |
| 4809 | if (ret != PTP_RC_OK) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 4810 | 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] | 4811 | return -1; |
| 4812 | } |
tsaarni | a3eb60a | 2007-07-06 17:41:30 +0000 | [diff] [blame] | 4813 | |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 4814 | return 0; |
Linus Walleij | 95698cd | 2006-02-24 10:40:40 +0000 | [diff] [blame] | 4815 | } |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 4816 | |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 4817 | /** |
Linus Walleij | 399807b | 2008-01-27 22:05:12 +0000 | [diff] [blame] | 4818 | * This function renames a single file, track, playlist, folder or |
| 4819 | * any other object on the MTP device, identified by an object ID. |
| 4820 | * This simply means that the PTP_OPC_ObjectFileName property |
| 4821 | * is updated, if this is supported by the device. |
| 4822 | * |
| 4823 | * @param device a pointer to the device that contains the the file, |
| 4824 | * track, foler, playlist or other object to set the filename for. |
| 4825 | * @param object_id the ID of the object to rename. |
Linus Walleij | 8be2c03 | 2008-01-28 21:23:47 +0000 | [diff] [blame] | 4826 | * @param newname the new filename for this object. You MUST assume that |
| 4827 | * this string can be modified by the call to this function, since |
| 4828 | * some devices have restrictions as to which filenames may be |
| 4829 | * used on them. |
Linus Walleij | de8193f | 2008-01-27 14:55:31 +0000 | [diff] [blame] | 4830 | * @return 0 on success, any other value means failure. |
| 4831 | */ |
| 4832 | int LIBMTP_Set_Object_Filename(LIBMTP_mtpdevice_t *device, |
Linus Walleij | 8be2c03 | 2008-01-28 21:23:47 +0000 | [diff] [blame] | 4833 | uint32_t object_id, char *newname) |
Linus Walleij | de8193f | 2008-01-27 14:55:31 +0000 | [diff] [blame] | 4834 | { |
| 4835 | PTPParams *params = (PTPParams *) device->params; |
| 4836 | PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo; |
Linus Walleij | 8be2c03 | 2008-01-28 21:23:47 +0000 | [diff] [blame] | 4837 | LIBMTP_file_t *file; |
| 4838 | uint16_t ptp_type; |
Linus Walleij | de8193f | 2008-01-27 14:55:31 +0000 | [diff] [blame] | 4839 | PTPObjectPropDesc opd; |
| 4840 | uint16_t ret; |
| 4841 | |
Linus Walleij | 8be2c03 | 2008-01-28 21:23:47 +0000 | [diff] [blame] | 4842 | // Get metadata for this object. |
| 4843 | file = LIBMTP_Get_Filemetadata(device, object_id); |
| 4844 | if (file == NULL) { |
| 4845 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Set_Object_Filename(): " |
| 4846 | "could not get file metadata for target object."); |
| 4847 | return -1; |
| 4848 | } |
| 4849 | ptp_type = map_libmtp_type_to_ptp_type(file->filetype); |
| 4850 | LIBMTP_destroy_file_t(file); |
| 4851 | |
| 4852 | // See if we can modify the filename on this kind of files. |
| 4853 | ret = ptp_mtp_getobjectpropdesc(params, PTP_OPC_ObjectFileName, ptp_type, &opd); |
Linus Walleij | de8193f | 2008-01-27 14:55:31 +0000 | [diff] [blame] | 4854 | if (ret != PTP_RC_OK) { |
Linus Walleij | 399807b | 2008-01-27 22:05:12 +0000 | [diff] [blame] | 4855 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Set_Object_Filename(): " |
Linus Walleij | 8be2c03 | 2008-01-28 21:23:47 +0000 | [diff] [blame] | 4856 | "could not get property description."); |
Linus Walleij | de8193f | 2008-01-27 14:55:31 +0000 | [diff] [blame] | 4857 | return -1; |
| 4858 | } |
| 4859 | |
| 4860 | if (!opd.GetSet) { |
| 4861 | ptp_free_objectpropdesc(&opd); |
Linus Walleij | 399807b | 2008-01-27 22:05:12 +0000 | [diff] [blame] | 4862 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Set_Object_Filename(): " |
Linus Walleij | 8be2c03 | 2008-01-28 21:23:47 +0000 | [diff] [blame] | 4863 | "property is not settable."); |
Linus Walleij | 399807b | 2008-01-27 22:05:12 +0000 | [diff] [blame] | 4864 | // TODO: we COULD actually upload/download the object here, if we feel |
| 4865 | // like wasting time for the user. |
Linus Walleij | de8193f | 2008-01-27 14:55:31 +0000 | [diff] [blame] | 4866 | return -1; |
| 4867 | } |
| 4868 | |
Linus Walleij | fec4d56 | 2008-06-01 22:30:36 +0000 | [diff] [blame] | 4869 | if (FLAG_ONLY_7BIT_FILENAMES(ptp_usb)) { |
Linus Walleij | de8193f | 2008-01-27 14:55:31 +0000 | [diff] [blame] | 4870 | strip_7bit_from_utf8(newname); |
| 4871 | } |
| 4872 | |
| 4873 | if (ptp_operation_issupported(params, PTP_OC_MTP_SetObjPropList) && |
Linus Walleij | fec4d56 | 2008-06-01 22:30:36 +0000 | [diff] [blame] | 4874 | !FLAG_BROKEN_SET_OBJECT_PROPLIST(ptp_usb)) { |
Linus Walleij | de8193f | 2008-01-27 14:55:31 +0000 | [diff] [blame] | 4875 | MTPProperties *props = NULL; |
| 4876 | MTPProperties *prop = NULL; |
| 4877 | int nrofprops = 0; |
| 4878 | |
| 4879 | prop = ptp_get_new_object_prop_entry(&props, &nrofprops); |
| 4880 | prop->ObjectHandle = object_id; |
| 4881 | prop->property = PTP_OPC_ObjectFileName; |
| 4882 | prop->datatype = PTP_DTC_STR; |
| 4883 | prop->propval.str = strdup(newname); |
| 4884 | |
| 4885 | ret = ptp_mtp_setobjectproplist(params, props, nrofprops); |
| 4886 | |
| 4887 | ptp_destroy_object_prop_list(props, nrofprops); |
| 4888 | |
| 4889 | if (ret != PTP_RC_OK) { |
| 4890 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Set_Oject_Filename(): " |
| 4891 | "could not set object property list."); |
| 4892 | ptp_free_objectpropdesc(&opd); |
| 4893 | return -1; |
| 4894 | } |
| 4895 | } else if (ptp_operation_issupported(params, PTP_OC_MTP_SetObjectPropValue)) { |
| 4896 | ret = set_object_string(device, object_id, PTP_OPC_ObjectFileName, newname); |
| 4897 | if (ret != 0) { |
| 4898 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Set_Oject_Filename(): " |
| 4899 | "could not set object filename."); |
| 4900 | ptp_free_objectpropdesc(&opd); |
| 4901 | return -1; |
| 4902 | } |
| 4903 | } else { |
| 4904 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Set_Oject_Filename(): " |
| 4905 | "Your device doesn't seem to support any known way of setting metadata."); |
| 4906 | ptp_free_objectpropdesc(&opd); |
| 4907 | return -1; |
| 4908 | } |
| 4909 | |
| 4910 | ptp_free_objectpropdesc(&opd); |
| 4911 | |
| 4912 | // update cached object properties if metadata cache exists |
| 4913 | update_metadata_cache(device, object_id); |
| 4914 | |
| 4915 | return 0; |
| 4916 | } |
Linus Walleij | 399807b | 2008-01-27 22:05:12 +0000 | [diff] [blame] | 4917 | |
Linus Walleij | de8193f | 2008-01-27 14:55:31 +0000 | [diff] [blame] | 4918 | /** |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 4919 | * Helper function. This indicates if a track exists on the device |
| 4920 | * @param device a pointer to the device to get the track from. |
| 4921 | * @param id the track ID of the track to retrieve. |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 4922 | * @return TRUE (!=0) if the track exists, FALSE (0) if not |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 4923 | */ |
| 4924 | int LIBMTP_Track_Exists(LIBMTP_mtpdevice_t *device, |
| 4925 | uint32_t const id) |
| 4926 | { |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 4927 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 4928 | uint32_t i; |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 4929 | |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 4930 | for (i = 0; i < params->handles.n; i++) { |
| 4931 | if (params->handles.Handler[i] == id) { |
| 4932 | return -1; |
| 4933 | } |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 4934 | } |
| 4935 | return 0; |
| 4936 | } |
| 4937 | |
| 4938 | /** |
| 4939 | * This creates a new folder structure and allocates memory |
| 4940 | * for it. Notice that if you add strings to this structure they |
| 4941 | * will be freed by the corresponding <code>LIBMTP_folder_track_t</code> |
| 4942 | * operation later, so be careful of using strdup() when assigning |
| 4943 | * strings, e.g.: |
| 4944 | * |
| 4945 | * @return a pointer to the newly allocated folder structure. |
| 4946 | * @see LIBMTP_destroy_folder_t() |
| 4947 | */ |
| 4948 | LIBMTP_folder_t *LIBMTP_new_folder_t(void) |
| 4949 | { |
| 4950 | LIBMTP_folder_t *new = (LIBMTP_folder_t *) malloc(sizeof(LIBMTP_folder_t)); |
| 4951 | if (new == NULL) { |
| 4952 | return NULL; |
| 4953 | } |
| 4954 | new->folder_id = 0; |
| 4955 | new->parent_id = 0; |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 4956 | new->storage_id = 0; |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 4957 | new->name = NULL; |
| 4958 | new->sibling = NULL; |
| 4959 | new->child = NULL; |
| 4960 | return new; |
| 4961 | } |
| 4962 | |
| 4963 | /** |
Linus Walleij | 5573def | 2007-04-23 07:04:18 +0000 | [diff] [blame] | 4964 | * This recursively deletes the memory for a folder structure. |
| 4965 | * This shall typically be called on a top-level folder list to |
| 4966 | * detsroy the entire folder tree. |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 4967 | * |
| 4968 | * @param folder folder structure to destroy |
| 4969 | * @see LIBMTP_new_folder_t() |
| 4970 | */ |
| 4971 | void LIBMTP_destroy_folder_t(LIBMTP_folder_t *folder) |
| 4972 | { |
| 4973 | |
| 4974 | if(folder == NULL) { |
| 4975 | return; |
| 4976 | } |
| 4977 | |
| 4978 | //Destroy from the bottom up |
| 4979 | if(folder->child != NULL) { |
| 4980 | LIBMTP_destroy_folder_t(folder->child); |
| 4981 | } |
| 4982 | |
| 4983 | if(folder->sibling != NULL) { |
| 4984 | LIBMTP_destroy_folder_t(folder->sibling); |
| 4985 | } |
| 4986 | |
| 4987 | if(folder->name != NULL) { |
| 4988 | free(folder->name); |
| 4989 | } |
| 4990 | |
| 4991 | free(folder); |
| 4992 | } |
| 4993 | |
| 4994 | /** |
| 4995 | * Helper function. Returns a folder structure for a |
| 4996 | * specified id. |
| 4997 | * |
| 4998 | * @param folderlist list of folders to search |
| 4999 | * @id id of folder to look for |
| 5000 | * @return a folder or NULL if not found |
| 5001 | */ |
| 5002 | LIBMTP_folder_t *LIBMTP_Find_Folder(LIBMTP_folder_t *folderlist, uint32_t id) |
| 5003 | { |
| 5004 | LIBMTP_folder_t *ret = NULL; |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 5005 | |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 5006 | if(folderlist == NULL) { |
| 5007 | return NULL; |
| 5008 | } |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 5009 | |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 5010 | if(folderlist->folder_id == id) { |
| 5011 | return folderlist; |
| 5012 | } |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 5013 | |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 5014 | if(folderlist->sibling) { |
| 5015 | ret = LIBMTP_Find_Folder(folderlist->sibling, id); |
| 5016 | } |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 5017 | |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 5018 | if(folderlist->child && ret == NULL) { |
| 5019 | ret = LIBMTP_Find_Folder(folderlist->child, id); |
| 5020 | } |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 5021 | |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 5022 | return ret; |
| 5023 | } |
| 5024 | |
| 5025 | /** |
Linus Walleij | 4d0deea | 2007-10-19 21:27:48 +0000 | [diff] [blame] | 5026 | * Function used to recursively get subfolders from params. |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 5027 | */ |
Linus Walleij | 4d0deea | 2007-10-19 21:27:48 +0000 | [diff] [blame] | 5028 | static LIBMTP_folder_t *get_subfolders_for_folder(PTPParams *params, uint32_t parent) |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 5029 | { |
Linus Walleij | 4d0deea | 2007-10-19 21:27:48 +0000 | [diff] [blame] | 5030 | uint32_t i; |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 5031 | LIBMTP_folder_t *retfolders = NULL; |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 5032 | |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 5033 | for (i = 0; i < params->handles.n; i++) { |
| 5034 | LIBMTP_folder_t *folder; |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 5035 | PTPObjectInfo *oi; |
Linus Walleij | 4d0deea | 2007-10-19 21:27:48 +0000 | [diff] [blame] | 5036 | |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 5037 | oi = ¶ms->objectinfo[i]; |
Linus Walleij | 4d0deea | 2007-10-19 21:27:48 +0000 | [diff] [blame] | 5038 | if (oi->ObjectFormat != PTP_OFC_Association || oi->ParentObject != parent) { |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 5039 | continue; |
| 5040 | } |
Linus Walleij | 4d0deea | 2007-10-19 21:27:48 +0000 | [diff] [blame] | 5041 | |
| 5042 | // Create a folder struct... |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 5043 | folder = LIBMTP_new_folder_t(); |
Linus Walleij | 4d0deea | 2007-10-19 21:27:48 +0000 | [diff] [blame] | 5044 | if (folder == NULL) { |
| 5045 | // malloc failure or so. |
| 5046 | return NULL; |
| 5047 | } |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 5048 | folder->folder_id = params->handles.Handler[i]; |
| 5049 | folder->parent_id = oi->ParentObject; |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 5050 | folder->storage_id = oi->StorageID; |
Linus Walleij | 6aee9a6 | 2007-10-19 20:28:48 +0000 | [diff] [blame] | 5051 | if (oi->Filename != NULL) { |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 5052 | folder->name = (char *)strdup(oi->Filename); |
Linus Walleij | 6aee9a6 | 2007-10-19 20:28:48 +0000 | [diff] [blame] | 5053 | } else { |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 5054 | folder->name = NULL; |
Linus Walleij | 6aee9a6 | 2007-10-19 20:28:48 +0000 | [diff] [blame] | 5055 | } |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 5056 | |
Linus Walleij | 4d0deea | 2007-10-19 21:27:48 +0000 | [diff] [blame] | 5057 | // Add as first returned or a sibling to current |
| 5058 | if (retfolders == NULL) { |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 5059 | retfolders = folder; |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 5060 | } else { |
Linus Walleij | 4d0deea | 2007-10-19 21:27:48 +0000 | [diff] [blame] | 5061 | LIBMTP_folder_t *tmp = retfolders; |
| 5062 | while (tmp->sibling != NULL) { |
| 5063 | tmp = tmp->sibling; |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 5064 | } |
Linus Walleij | 4d0deea | 2007-10-19 21:27:48 +0000 | [diff] [blame] | 5065 | tmp->sibling = folder; |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 5066 | } |
Linus Walleij | 4d0deea | 2007-10-19 21:27:48 +0000 | [diff] [blame] | 5067 | |
| 5068 | // Recursively get children for this child. Perhaps NULL. |
| 5069 | folder->child = get_subfolders_for_folder(params, folder->folder_id); |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 5070 | } |
Linus Walleij | 4d0deea | 2007-10-19 21:27:48 +0000 | [diff] [blame] | 5071 | |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 5072 | return retfolders; |
| 5073 | } |
| 5074 | |
| 5075 | /** |
Linus Walleij | 4d0deea | 2007-10-19 21:27:48 +0000 | [diff] [blame] | 5076 | * This returns a list of all folders available |
| 5077 | * on the current MTP device. |
| 5078 | * |
| 5079 | * @param device a pointer to the device to get the track listing for. |
| 5080 | * @return a list of folders |
| 5081 | */ |
| 5082 | LIBMTP_folder_t *LIBMTP_Get_Folder_List(LIBMTP_mtpdevice_t *device) |
| 5083 | { |
| 5084 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | 4d0deea | 2007-10-19 21:27:48 +0000 | [diff] [blame] | 5085 | |
| 5086 | // Get all the handles if we haven't already done that |
| 5087 | if (params->handles.Handler == NULL) { |
| 5088 | flush_handles(device); |
| 5089 | } |
| 5090 | |
| 5091 | // TODO: make a temporary list of folders only to |
| 5092 | // speed up searching? Else we get O(n^2) complexity |
| 5093 | // where n is the number of handles on the device, |
| 5094 | // in the following recursive call. Making a temp |
| 5095 | // list will reduce n to the number of folders. |
| 5096 | |
| 5097 | // We begin at the root folder and get them all recursively |
| 5098 | return get_subfolders_for_folder(params, 0x00000000); |
| 5099 | } |
| 5100 | |
| 5101 | /** |
Linus Walleij | c86afbd | 2006-05-04 19:05:24 +0000 | [diff] [blame] | 5102 | * This create a folder on the current MTP device. The PTP name |
| 5103 | * for a folder is "association". The PTP/MTP devices does not |
| 5104 | * have an internal "folder" concept really, it contains a flat |
| 5105 | * list of all files and some file are "associations" that other |
| 5106 | * files and folders may refer to as its "parent". |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 5107 | * |
Linus Walleij | c86afbd | 2006-05-04 19:05:24 +0000 | [diff] [blame] | 5108 | * @param device a pointer to the device to create the folder on. |
| 5109 | * @param name the name of the new folder. |
| 5110 | * @param parent_id id of parent folder to add the new folder to, |
| 5111 | * or 0 to put it in the root directory. |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 5112 | * @param storage_id id of the storage to add this new folder to. |
| 5113 | * notice that you cannot mismatch storage id and parent id: |
| 5114 | * they must both be on the same storage! Pass in 0 if you |
| 5115 | * want to create this folder on the default storage. |
Linus Walleij | c86afbd | 2006-05-04 19:05:24 +0000 | [diff] [blame] | 5116 | * @return id to new folder or 0 if an error occured |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 5117 | */ |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 5118 | uint32_t LIBMTP_Create_Folder(LIBMTP_mtpdevice_t *device, char *name, |
| 5119 | uint32_t parent_id, uint32_t storage_id) |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 5120 | { |
| 5121 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | d3b7857 | 2007-08-24 21:28:24 +0000 | [diff] [blame] | 5122 | PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo; |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 5123 | uint32_t parenthandle = 0; |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 5124 | uint32_t store; |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 5125 | PTPObjectInfo new_folder; |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 5126 | uint16_t ret; |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 5127 | uint32_t new_id = 0; |
| 5128 | |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 5129 | if (storage_id == 0) { |
| 5130 | store = get_first_storageid(device); |
| 5131 | } else { |
| 5132 | store = storage_id; |
| 5133 | } |
| 5134 | parenthandle = parent_id; |
| 5135 | |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 5136 | memset(&new_folder, 0, sizeof(new_folder)); |
| 5137 | new_folder.Filename = name; |
Linus Walleij | fec4d56 | 2008-06-01 22:30:36 +0000 | [diff] [blame] | 5138 | if (FLAG_ONLY_7BIT_FILENAMES(ptp_usb)) { |
Linus Walleij | d3b7857 | 2007-08-24 21:28:24 +0000 | [diff] [blame] | 5139 | strip_7bit_from_utf8(new_folder.Filename); |
| 5140 | } |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 5141 | new_folder.ObjectCompressedSize = 1; |
| 5142 | new_folder.ObjectFormat = PTP_OFC_Association; |
| 5143 | new_folder.ParentObject = parent_id; |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 5144 | new_folder.StorageID = store; |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 5145 | |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 5146 | // Create the object |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 5147 | // FIXME: use send list here if available. |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 5148 | ret = ptp_sendobjectinfo(params, &store, &parenthandle, &new_id, &new_folder); |
| 5149 | if (ret != PTP_RC_OK) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 5150 | 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] | 5151 | if (ret == PTP_RC_AccessDenied) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 5152 | add_ptp_error_to_errorstack(device, ret, "ACCESS DENIED."); |
Linus Walleij | 99310d4 | 2006-11-01 08:29:39 +0000 | [diff] [blame] | 5153 | } |
Linus Walleij | c86afbd | 2006-05-04 19:05:24 +0000 | [diff] [blame] | 5154 | return 0; |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 5155 | } |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 5156 | // NOTE: don't destroy the new_folder objectinfo, because it is statically referencing |
| 5157 | // several strings. |
tsaarni | a3eb60a | 2007-07-06 17:41:30 +0000 | [diff] [blame] | 5158 | |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 5159 | add_object_to_cache(device, new_id); |
tsaarni | a3eb60a | 2007-07-06 17:41:30 +0000 | [diff] [blame] | 5160 | |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 5161 | return new_id; |
| 5162 | } |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 5163 | |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 5164 | /** |
| 5165 | * This creates a new playlist metadata structure and allocates memory |
| 5166 | * for it. Notice that if you add strings to this structure they |
| 5167 | * will be freed by the corresponding <code>LIBMTP_destroy_playlist_t</code> |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 5168 | * operation later, so be careful of using strdup() when assigning |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 5169 | * strings, e.g.: |
| 5170 | * |
| 5171 | * <pre> |
| 5172 | * LIBMTP_playlist_t *pl = LIBMTP_new_playlist_t(); |
| 5173 | * pl->name = strdup(str); |
| 5174 | * .... |
| 5175 | * LIBMTP_destroy_playlist_t(pl); |
| 5176 | * </pre> |
| 5177 | * |
| 5178 | * @return a pointer to the newly allocated metadata structure. |
| 5179 | * @see LIBMTP_destroy_playlist_t() |
| 5180 | */ |
| 5181 | LIBMTP_playlist_t *LIBMTP_new_playlist_t(void) |
| 5182 | { |
| 5183 | LIBMTP_playlist_t *new = (LIBMTP_playlist_t *) malloc(sizeof(LIBMTP_playlist_t)); |
| 5184 | if (new == NULL) { |
| 5185 | return NULL; |
| 5186 | } |
| 5187 | new->playlist_id = 0; |
Linus Walleij | 5ce59db | 2008-03-12 21:22:58 +0000 | [diff] [blame] | 5188 | new->parent_id = 0; |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 5189 | new->storage_id = 0; |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 5190 | new->name = NULL; |
| 5191 | new->tracks = NULL; |
| 5192 | new->no_tracks = 0; |
| 5193 | new->next = NULL; |
| 5194 | return new; |
| 5195 | } |
| 5196 | |
| 5197 | /** |
| 5198 | * This destroys a playlist metadata structure and deallocates the memory |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 5199 | * used by it, including any strings. Never use a track metadata |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 5200 | * structure again after calling this function on it. |
| 5201 | * @param playlist the playlist metadata to destroy. |
| 5202 | * @see LIBMTP_new_playlist_t() |
| 5203 | */ |
| 5204 | void LIBMTP_destroy_playlist_t(LIBMTP_playlist_t *playlist) |
| 5205 | { |
| 5206 | if (playlist == NULL) { |
| 5207 | return; |
| 5208 | } |
| 5209 | if (playlist->name != NULL) |
| 5210 | free(playlist->name); |
| 5211 | if (playlist->tracks != NULL) |
| 5212 | free(playlist->tracks); |
| 5213 | free(playlist); |
| 5214 | return; |
| 5215 | } |
| 5216 | |
| 5217 | /** |
| 5218 | * This function returns a list of the playlists available on the |
| 5219 | * device. Typical usage: |
| 5220 | * |
| 5221 | * <pre> |
| 5222 | * </pre> |
| 5223 | * |
| 5224 | * @param device a pointer to the device to get the playlist listing from. |
| 5225 | * @return a playlist list on success, else NULL. If there are no playlists |
| 5226 | * on the device, NULL will be returned as well. |
Linus Walleij | 2e4b5f9 | 2006-06-16 14:00:49 +0000 | [diff] [blame] | 5227 | * @see LIBMTP_Get_Playlist() |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 5228 | */ |
| 5229 | LIBMTP_playlist_t *LIBMTP_Get_Playlist_List(LIBMTP_mtpdevice_t *device) |
| 5230 | { |
Linus Walleij | f3c4405 | 2008-08-16 21:14:56 +0000 | [diff] [blame] | 5231 | PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo; |
| 5232 | const int REQ_SPL = FLAG_PLAYLIST_SPL(ptp_usb); |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 5233 | PTPParams *params = (PTPParams *) device->params; |
| 5234 | LIBMTP_playlist_t *retlists = NULL; |
| 5235 | LIBMTP_playlist_t *curlist = NULL; |
| 5236 | uint32_t i; |
| 5237 | |
| 5238 | // Get all the handles if we haven't already done that |
| 5239 | if (params->handles.Handler == NULL) { |
| 5240 | flush_handles(device); |
| 5241 | } |
| 5242 | |
| 5243 | for (i = 0; i < params->handles.n; i++) { |
| 5244 | LIBMTP_playlist_t *pl; |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 5245 | PTPObjectInfo *oi; |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 5246 | uint16_t ret; |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 5247 | |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 5248 | oi = ¶ms->objectinfo[i]; |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 5249 | |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 5250 | // Ignore stuff that isn't playlists |
Linus Walleij | f3c4405 | 2008-08-16 21:14:56 +0000 | [diff] [blame] | 5251 | |
| 5252 | // For Samsung players we must look for the .spl extension explicitly since |
| 5253 | // playlists are not stored as playlist objects. |
| 5254 | if ( REQ_SPL && is_spl_playlist(oi) ) { |
| 5255 | // Allocate a new playlist type |
| 5256 | pl = LIBMTP_new_playlist_t(); |
| 5257 | spl_to_playlist_t(device, oi, params->handles.Handler[i], pl); |
| 5258 | } |
| 5259 | else if ( oi->ObjectFormat != PTP_OFC_MTP_AbstractAudioVideoPlaylist ) { |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 5260 | continue; |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 5261 | } |
Linus Walleij | f3c4405 | 2008-08-16 21:14:56 +0000 | [diff] [blame] | 5262 | else { |
| 5263 | // Allocate a new playlist type |
| 5264 | pl = LIBMTP_new_playlist_t(); |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 5265 | |
Linus Walleij | f3c4405 | 2008-08-16 21:14:56 +0000 | [diff] [blame] | 5266 | // Ignoring the oi->Filename field. |
| 5267 | pl->name = get_string_from_object(device, params->handles.Handler[i], PTP_OPC_Name); |
| 5268 | pl->playlist_id = params->handles.Handler[i]; |
| 5269 | pl->parent_id = oi->ParentObject; |
| 5270 | pl->storage_id = oi->StorageID; |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 5271 | |
Linus Walleij | f3c4405 | 2008-08-16 21:14:56 +0000 | [diff] [blame] | 5272 | // Then get the track listing for this playlist |
| 5273 | ret = ptp_mtp_getobjectreferences(params, pl->playlist_id, &pl->tracks, &pl->no_tracks); |
| 5274 | if (ret != PTP_RC_OK) { |
| 5275 | add_ptp_error_to_errorstack(device, ret, "LIBMTP_Get_Playlist: Could not get object references."); |
| 5276 | pl->tracks = NULL; |
| 5277 | pl->no_tracks = 0; |
| 5278 | } |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 5279 | } |
| 5280 | |
| 5281 | // Add playlist to a list that will be returned afterwards. |
| 5282 | if (retlists == NULL) { |
| 5283 | retlists = pl; |
| 5284 | curlist = pl; |
| 5285 | } else { |
| 5286 | curlist->next = pl; |
| 5287 | curlist = pl; |
| 5288 | } |
| 5289 | |
| 5290 | // Call callback here if we decide to add that possibility... |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 5291 | } |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 5292 | return retlists; |
| 5293 | } |
| 5294 | |
Linus Walleij | 2e4b5f9 | 2006-06-16 14:00:49 +0000 | [diff] [blame] | 5295 | |
| 5296 | /** |
| 5297 | * This function retrieves an individual playlist from the device. |
| 5298 | * @param device a pointer to the device to get the playlist from. |
| 5299 | * @param plid the unique ID of the playlist to retrieve. |
| 5300 | * @return a valid playlist metadata post or NULL on failure. |
| 5301 | * @see LIBMTP_Get_Playlist_List() |
| 5302 | */ |
| 5303 | LIBMTP_playlist_t *LIBMTP_Get_Playlist(LIBMTP_mtpdevice_t *device, uint32_t const plid) |
| 5304 | { |
Linus Walleij | f3c4405 | 2008-08-16 21:14:56 +0000 | [diff] [blame] | 5305 | PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo; |
| 5306 | const int REQ_SPL = FLAG_PLAYLIST_SPL(ptp_usb); |
Linus Walleij | 2e4b5f9 | 2006-06-16 14:00:49 +0000 | [diff] [blame] | 5307 | PTPParams *params = (PTPParams *) device->params; |
| 5308 | uint32_t i; |
| 5309 | |
| 5310 | // Get all the handles if we haven't already done that |
| 5311 | if (params->handles.Handler == NULL) { |
| 5312 | flush_handles(device); |
| 5313 | } |
| 5314 | |
| 5315 | for (i = 0; i < params->handles.n; i++) { |
| 5316 | LIBMTP_playlist_t *pl; |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 5317 | PTPObjectInfo *oi; |
Linus Walleij | 2e4b5f9 | 2006-06-16 14:00:49 +0000 | [diff] [blame] | 5318 | uint16_t ret; |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 5319 | |
Linus Walleij | 2e4b5f9 | 2006-06-16 14:00:49 +0000 | [diff] [blame] | 5320 | if (params->handles.Handler[i] != plid) { |
| 5321 | continue; |
| 5322 | } |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 5323 | |
| 5324 | oi = ¶ms->objectinfo[i]; |
Linus Walleij | 2e4b5f9 | 2006-06-16 14:00:49 +0000 | [diff] [blame] | 5325 | |
Linus Walleij | f3c4405 | 2008-08-16 21:14:56 +0000 | [diff] [blame] | 5326 | // For Samsung players we must look for the .spl extension explicitly since |
| 5327 | // playlists are not stored as playlist objects. |
| 5328 | if ( REQ_SPL && is_spl_playlist(oi) ) { |
| 5329 | // Allocate a new playlist type |
| 5330 | pl = LIBMTP_new_playlist_t(); |
| 5331 | spl_to_playlist_t(device, oi, params->handles.Handler[i], pl); |
| 5332 | return pl; |
| 5333 | } |
| 5334 | |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 5335 | // Ignore stuff that isn't playlists |
Linus Walleij | f3c4405 | 2008-08-16 21:14:56 +0000 | [diff] [blame] | 5336 | else if ( oi->ObjectFormat != PTP_OFC_MTP_AbstractAudioVideoPlaylist ) { |
Linus Walleij | 2e4b5f9 | 2006-06-16 14:00:49 +0000 | [diff] [blame] | 5337 | return NULL; |
| 5338 | } |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 5339 | |
| 5340 | // Allocate a new playlist type |
| 5341 | pl = LIBMTP_new_playlist_t(); |
| 5342 | |
| 5343 | // Ignoring the io.Filename field. |
| 5344 | pl->name = get_string_from_object(device, params->handles.Handler[i], PTP_OPC_Name); |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 5345 | pl->playlist_id = params->handles.Handler[i]; |
Linus Walleij | 5ce59db | 2008-03-12 21:22:58 +0000 | [diff] [blame] | 5346 | pl->parent_id = oi->ParentObject; |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 5347 | pl->storage_id = oi->StorageID; |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 5348 | |
| 5349 | // Then get the track listing for this playlist |
| 5350 | ret = ptp_mtp_getobjectreferences(params, pl->playlist_id, &pl->tracks, &pl->no_tracks); |
| 5351 | if (ret != PTP_RC_OK) { |
| 5352 | add_ptp_error_to_errorstack(device, ret, "LIBMTP_Get_Playlist(): Could not get object references."); |
| 5353 | pl->tracks = NULL; |
| 5354 | pl->no_tracks = 0; |
| 5355 | } |
| 5356 | |
| 5357 | return pl; |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 5358 | } |
Linus Walleij | 2e4b5f9 | 2006-06-16 14:00:49 +0000 | [diff] [blame] | 5359 | return NULL; |
| 5360 | } |
| 5361 | |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 5362 | /** |
Linus Walleij | b7426d1 | 2006-11-25 23:19:11 +0000 | [diff] [blame] | 5363 | * This function creates a new abstract list such as a playlist |
| 5364 | * or an album. |
| 5365 | * |
| 5366 | * @param device a pointer to the device to create the new abstract list |
| 5367 | * on. |
| 5368 | * @param name the name of the new abstract list. |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 5369 | * @param artist the artist of the new abstract list or NULL. |
| 5370 | * @param genre the genre of the new abstract list or NULL. |
Linus Walleij | b7426d1 | 2006-11-25 23:19:11 +0000 | [diff] [blame] | 5371 | * @param parenthandle the handle of the parent or 0 for no parent |
| 5372 | * i.e. the root folder. |
| 5373 | * @param objectformat the abstract list type to create. |
| 5374 | * @param suffix the ".foo" (4 characters) suffix to use for the virtual |
| 5375 | * "file" created by this operation. |
| 5376 | * @param newid a pointer to a variable that will hold the new object |
| 5377 | * ID if this call is successful. |
| 5378 | * @param tracks an array of tracks to associate with this list. |
| 5379 | * @param no_tracks the number of tracks in the list. |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 5380 | * @return 0 on success, any other value means failure. |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 5381 | */ |
Linus Walleij | b7426d1 | 2006-11-25 23:19:11 +0000 | [diff] [blame] | 5382 | static int create_new_abstract_list(LIBMTP_mtpdevice_t *device, |
| 5383 | char const * const name, |
Linus Walleij | adce4a5 | 2007-04-23 07:28:11 +0000 | [diff] [blame] | 5384 | char const * const artist, |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 5385 | char const * const composer, |
Linus Walleij | adce4a5 | 2007-04-23 07:28:11 +0000 | [diff] [blame] | 5386 | char const * const genre, |
Linus Walleij | b7426d1 | 2006-11-25 23:19:11 +0000 | [diff] [blame] | 5387 | uint32_t const parenthandle, |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 5388 | uint32_t const storageid, |
Linus Walleij | b7426d1 | 2006-11-25 23:19:11 +0000 | [diff] [blame] | 5389 | uint16_t const objectformat, |
| 5390 | char const * const suffix, |
| 5391 | uint32_t * const newid, |
| 5392 | uint32_t const * const tracks, |
| 5393 | uint32_t const no_tracks) |
| 5394 | |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 5395 | { |
Linus Walleij | b7426d1 | 2006-11-25 23:19:11 +0000 | [diff] [blame] | 5396 | int i; |
| 5397 | int supported = 0; |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 5398 | uint16_t ret; |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 5399 | uint16_t *properties = NULL; |
rreardon | 508705f | 2006-11-19 21:27:22 +0000 | [diff] [blame] | 5400 | uint32_t propcnt = 0; |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 5401 | uint32_t store; |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 5402 | uint32_t localph = parenthandle; |
Linus Walleij | b7426d1 | 2006-11-25 23:19:11 +0000 | [diff] [blame] | 5403 | uint8_t nonconsumable = 0x00U; /* By default it is consumable */ |
| 5404 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | d3b7857 | 2007-08-24 21:28:24 +0000 | [diff] [blame] | 5405 | PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo; |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 5406 | char fname[256]; |
Linus Walleij | d14e84f | 2006-06-16 14:50:59 +0000 | [diff] [blame] | 5407 | uint8_t data[2]; |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 5408 | |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 5409 | if (storageid == 0) { |
| 5410 | store = get_first_storageid(device); |
| 5411 | } else { |
| 5412 | store = storageid; |
| 5413 | } |
| 5414 | |
Linus Walleij | b7426d1 | 2006-11-25 23:19:11 +0000 | [diff] [blame] | 5415 | // Check if we can create an object of this type |
| 5416 | for ( i=0; i < params->deviceinfo.ImageFormats_len; i++ ) { |
| 5417 | if (params->deviceinfo.ImageFormats[i] == objectformat) { |
| 5418 | supported = 1; |
| 5419 | break; |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 5420 | } |
| 5421 | } |
Linus Walleij | b7426d1 | 2006-11-25 23:19:11 +0000 | [diff] [blame] | 5422 | if (!supported) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 5423 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "create_new_abstract_list(): player does not support this abstract type."); |
| 5424 | printf("Unsupported type: %04x\n", objectformat); |
Linus Walleij | b7426d1 | 2006-11-25 23:19:11 +0000 | [diff] [blame] | 5425 | return -1; |
| 5426 | } |
| 5427 | |
Richard Low | 15731fe | 2007-03-22 20:27:20 +0000 | [diff] [blame] | 5428 | // add the new suffix if it isn't there |
Linus Walleij | 629fe40 | 2006-12-12 23:39:15 +0000 | [diff] [blame] | 5429 | fname[0] = '\0'; |
Linus Walleij | b7426d1 | 2006-11-25 23:19:11 +0000 | [diff] [blame] | 5430 | if (strlen(name) > strlen(suffix)) { |
| 5431 | char const * const suff = &name[strlen(name)-strlen(suffix)]; |
| 5432 | if (!strcmp(suff, suffix)) { |
| 5433 | // Home free. |
Linus Walleij | 629fe40 | 2006-12-12 23:39:15 +0000 | [diff] [blame] | 5434 | strncpy(fname, name, sizeof(fname)); |
Linus Walleij | b7426d1 | 2006-11-25 23:19:11 +0000 | [diff] [blame] | 5435 | } |
| 5436 | } |
| 5437 | // If it didn't end with "<suffix>" then add that here. |
Linus Walleij | 629fe40 | 2006-12-12 23:39:15 +0000 | [diff] [blame] | 5438 | if (fname[0] == '\0') { |
Linus Walleij | b7426d1 | 2006-11-25 23:19:11 +0000 | [diff] [blame] | 5439 | strncpy(fname, name, sizeof(fname)-strlen(suffix)-1); |
| 5440 | strcat(fname, suffix); |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 5441 | fname[sizeof(fname)-1] = '\0'; |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 5442 | } |
| 5443 | |
rreardon | 508705f | 2006-11-19 21:27:22 +0000 | [diff] [blame] | 5444 | if (ptp_operation_issupported(params,PTP_OC_MTP_SendObjectPropList)) { |
mopoke | 3136444 | 2006-11-20 04:53:04 +0000 | [diff] [blame] | 5445 | |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 5446 | MTPProperties *props = NULL; |
| 5447 | MTPProperties *prop = NULL; |
| 5448 | int nrofprops = 0; |
Richard Low | 6c0a6ce | 2006-11-26 10:42:08 +0000 | [diff] [blame] | 5449 | |
| 5450 | *newid = 0x00000000U; |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 5451 | |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 5452 | ret = ptp_mtp_getobjectpropssupported(params, objectformat, &propcnt, &properties); |
mopoke | 3136444 | 2006-11-20 04:53:04 +0000 | [diff] [blame] | 5453 | |
rreardon | 508705f | 2006-11-19 21:27:22 +0000 | [diff] [blame] | 5454 | for (i=0;i<propcnt;i++) { |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 5455 | PTPObjectPropDesc opd; |
| 5456 | |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 5457 | ret = ptp_mtp_getobjectpropdesc(params, properties[i], objectformat, &opd); |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 5458 | if (ret != PTP_RC_OK) { |
| 5459 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "create_new_abstract_list(): " |
| 5460 | "could not get property description."); |
| 5461 | } else if (opd.GetSet) { |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 5462 | switch (properties[i]) { |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 5463 | case PTP_OPC_ObjectFileName: |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 5464 | prop = ptp_get_new_object_prop_entry(&props,&nrofprops); |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 5465 | prop->ObjectHandle = *newid; |
| 5466 | prop->property = PTP_OPC_ObjectFileName; |
| 5467 | prop->datatype = PTP_DTC_STR; |
| 5468 | prop->propval.str = strdup(fname); |
Linus Walleij | fec4d56 | 2008-06-01 22:30:36 +0000 | [diff] [blame] | 5469 | if (FLAG_ONLY_7BIT_FILENAMES(ptp_usb)) { |
Linus Walleij | d3b7857 | 2007-08-24 21:28:24 +0000 | [diff] [blame] | 5470 | strip_7bit_from_utf8(prop->propval.str); |
| 5471 | } |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 5472 | break; |
| 5473 | case PTP_OPC_ProtectionStatus: |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 5474 | prop = ptp_get_new_object_prop_entry(&props,&nrofprops); |
Linus Walleij | adce4a5 | 2007-04-23 07:28:11 +0000 | [diff] [blame] | 5475 | prop->ObjectHandle = *newid; |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 5476 | prop->property = PTP_OPC_ProtectionStatus; |
| 5477 | prop->datatype = PTP_DTC_UINT16; |
| 5478 | prop->propval.u16 = 0x0000U; /* Not protected */ |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 5479 | break; |
| 5480 | case PTP_OPC_NonConsumable: |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 5481 | prop = ptp_get_new_object_prop_entry(&props,&nrofprops); |
Linus Walleij | adce4a5 | 2007-04-23 07:28:11 +0000 | [diff] [blame] | 5482 | prop->ObjectHandle = *newid; |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 5483 | prop->property = PTP_OPC_NonConsumable; |
| 5484 | prop->datatype = PTP_DTC_UINT8; |
| 5485 | prop->propval.u8 = nonconsumable; |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 5486 | break; |
| 5487 | case PTP_OPC_Name: |
| 5488 | if (name != NULL) { |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 5489 | prop = ptp_get_new_object_prop_entry(&props,&nrofprops); |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 5490 | prop->ObjectHandle = *newid; |
| 5491 | prop->property = PTP_OPC_Name; |
| 5492 | prop->datatype = PTP_DTC_STR; |
| 5493 | prop->propval.str = strdup(name); |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 5494 | } |
| 5495 | break; |
Linus Walleij | 0b75ab6 | 2007-12-28 00:47:34 +0000 | [diff] [blame] | 5496 | case PTP_OPC_AlbumArtist: |
| 5497 | if (artist != NULL) { |
| 5498 | prop = ptp_get_new_object_prop_entry(&props,&nrofprops); |
| 5499 | prop->ObjectHandle = *newid; |
| 5500 | prop->property = PTP_OPC_AlbumArtist; |
| 5501 | prop->datatype = PTP_DTC_STR; |
| 5502 | prop->propval.str = strdup(artist); |
| 5503 | } |
| 5504 | break; |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 5505 | case PTP_OPC_Artist: |
| 5506 | if (artist != NULL) { |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 5507 | prop = ptp_get_new_object_prop_entry(&props,&nrofprops); |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 5508 | prop->ObjectHandle = *newid; |
| 5509 | prop->property = PTP_OPC_Artist; |
| 5510 | prop->datatype = PTP_DTC_STR; |
| 5511 | prop->propval.str = strdup(artist); |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 5512 | } |
| 5513 | break; |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 5514 | case PTP_OPC_Composer: |
| 5515 | if (composer != NULL) { |
| 5516 | prop = ptp_get_new_object_prop_entry(&props,&nrofprops); |
| 5517 | prop->ObjectHandle = *newid; |
| 5518 | prop->property = PTP_OPC_Composer; |
| 5519 | prop->datatype = PTP_DTC_STR; |
| 5520 | prop->propval.str = strdup(composer); |
| 5521 | } |
| 5522 | break; |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 5523 | case PTP_OPC_Genre: |
| 5524 | if (genre != NULL) { |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 5525 | prop = ptp_get_new_object_prop_entry(&props,&nrofprops); |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 5526 | prop->ObjectHandle = *newid; |
| 5527 | prop->property = PTP_OPC_Genre; |
| 5528 | prop->datatype = PTP_DTC_STR; |
| 5529 | prop->propval.str = strdup(genre); |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 5530 | } |
| 5531 | break; |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 5532 | case PTP_OPC_DateModified: |
| 5533 | // Tag with current time if that is supported |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 5534 | prop = ptp_get_new_object_prop_entry(&props,&nrofprops); |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 5535 | prop->ObjectHandle = *newid; |
| 5536 | prop->property = PTP_OPC_DateModified; |
| 5537 | prop->datatype = PTP_DTC_STR; |
| 5538 | prop->propval.str = get_iso8601_stamp(); |
| 5539 | break; |
Linus Walleij | adce4a5 | 2007-04-23 07:28:11 +0000 | [diff] [blame] | 5540 | } |
rreardon | 508705f | 2006-11-19 21:27:22 +0000 | [diff] [blame] | 5541 | } |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 5542 | ptp_free_objectpropdesc(&opd); |
rreardon | 508705f | 2006-11-19 21:27:22 +0000 | [diff] [blame] | 5543 | } |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 5544 | free(properties); |
mopoke | 3136444 | 2006-11-20 04:53:04 +0000 | [diff] [blame] | 5545 | |
Linus Walleij | b7426d1 | 2006-11-25 23:19:11 +0000 | [diff] [blame] | 5546 | ret = ptp_mtp_sendobjectproplist(params, &store, &localph, newid, |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 5547 | objectformat, 0, props, nrofprops); |
mopoke | 3136444 | 2006-11-20 04:53:04 +0000 | [diff] [blame] | 5548 | |
rreardon | 508705f | 2006-11-19 21:27:22 +0000 | [diff] [blame] | 5549 | /* Free property list */ |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 5550 | ptp_destroy_object_prop_list(props, nrofprops); |
mopoke | 3136444 | 2006-11-20 04:53:04 +0000 | [diff] [blame] | 5551 | |
rreardon | 508705f | 2006-11-19 21:27:22 +0000 | [diff] [blame] | 5552 | if (ret != PTP_RC_OK) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 5553 | 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] | 5554 | if (ret == PTP_RC_AccessDenied) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 5555 | add_ptp_error_to_errorstack(device, ret, "ACCESS DENIED."); |
rreardon | 508705f | 2006-11-19 21:27:22 +0000 | [diff] [blame] | 5556 | } |
| 5557 | return -1; |
| 5558 | } |
Richard Low | a6c924c | 2006-12-29 17:44:28 +0000 | [diff] [blame] | 5559 | |
Richard Low | c3f5fc3 | 2007-07-29 09:40:50 +0000 | [diff] [blame] | 5560 | // now send the blank object |
Richard Low | a6c924c | 2006-12-29 17:44:28 +0000 | [diff] [blame] | 5561 | ret = ptp_sendobject(params, NULL, 0); |
| 5562 | if (ret != PTP_RC_OK) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 5563 | 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] | 5564 | return -1; |
| 5565 | } |
Linus Walleij | 629fe40 | 2006-12-12 23:39:15 +0000 | [diff] [blame] | 5566 | |
rreardon | 508705f | 2006-11-19 21:27:22 +0000 | [diff] [blame] | 5567 | } else if (ptp_operation_issupported(params,PTP_OC_SendObjectInfo)) { |
Linus Walleij | 629fe40 | 2006-12-12 23:39:15 +0000 | [diff] [blame] | 5568 | PTPObjectInfo new_object; |
| 5569 | |
| 5570 | new_object.Filename = fname; |
Linus Walleij | fec4d56 | 2008-06-01 22:30:36 +0000 | [diff] [blame] | 5571 | if (FLAG_ONLY_7BIT_FILENAMES(ptp_usb)) { |
Linus Walleij | d3b7857 | 2007-08-24 21:28:24 +0000 | [diff] [blame] | 5572 | strip_7bit_from_utf8(new_object.Filename); |
| 5573 | } |
Linus Walleij | 629fe40 | 2006-12-12 23:39:15 +0000 | [diff] [blame] | 5574 | new_object.ObjectCompressedSize = 1; |
| 5575 | new_object.ObjectFormat = objectformat; |
| 5576 | |
Linus Walleij | 2069848 | 2006-11-24 20:54:21 +0000 | [diff] [blame] | 5577 | // Create the object |
Linus Walleij | b7426d1 | 2006-11-25 23:19:11 +0000 | [diff] [blame] | 5578 | ret = ptp_sendobjectinfo(params, &store, &localph, newid, &new_object); |
Linus Walleij | 2069848 | 2006-11-24 20:54:21 +0000 | [diff] [blame] | 5579 | if (ret != PTP_RC_OK) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 5580 | 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] | 5581 | if (ret == PTP_RC_AccessDenied) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 5582 | add_ptp_error_to_errorstack(device, ret, "ACCESS DENIED."); |
Linus Walleij | 2069848 | 2006-11-24 20:54:21 +0000 | [diff] [blame] | 5583 | } |
| 5584 | return -1; |
| 5585 | } |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 5586 | // NOTE: don't destroy new_object objectinfo afterwards - the strings it contains are |
| 5587 | // not copies. |
Richard Low | a6c924c | 2006-12-29 17:44:28 +0000 | [diff] [blame] | 5588 | |
| 5589 | /* |
| 5590 | * We have to send this one blank data byte. |
| 5591 | * If we don't, the handle will not be created and thus there is no playlist. |
| 5592 | */ |
| 5593 | data[0] = '\0'; |
| 5594 | data[1] = '\0'; |
| 5595 | ret = ptp_sendobject(params, data, 1); |
| 5596 | if (ret != PTP_RC_OK) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 5597 | 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] | 5598 | return -1; |
| 5599 | } |
| 5600 | |
| 5601 | // Update title |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 5602 | // FIXME: check if supported |
Linus Walleij | adce4a5 | 2007-04-23 07:28:11 +0000 | [diff] [blame] | 5603 | if (name != NULL) { |
| 5604 | ret = set_object_string(device, *newid, PTP_OPC_Name, name); |
| 5605 | if (ret != 0) { |
| 5606 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "create_new_abstract_list(): could not set entity name."); |
| 5607 | return -1; |
| 5608 | } |
Richard Low | a6c924c | 2006-12-29 17:44:28 +0000 | [diff] [blame] | 5609 | } |
Linus Walleij | adce4a5 | 2007-04-23 07:28:11 +0000 | [diff] [blame] | 5610 | |
| 5611 | // Update artist |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 5612 | // FIXME: check if supported |
Linus Walleij | adce4a5 | 2007-04-23 07:28:11 +0000 | [diff] [blame] | 5613 | if (artist != NULL) { |
Linus Walleij | 0b75ab6 | 2007-12-28 00:47:34 +0000 | [diff] [blame] | 5614 | ret = set_object_string(device, *newid, PTP_OPC_AlbumArtist, artist); |
| 5615 | if (ret != 0) { |
| 5616 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "create_new_abstract_list(): could not set entity album artist."); |
| 5617 | return -1; |
| 5618 | } |
| 5619 | } |
| 5620 | // Update artist |
| 5621 | // FIXME: check if supported |
| 5622 | if (artist != NULL) { |
Linus Walleij | adce4a5 | 2007-04-23 07:28:11 +0000 | [diff] [blame] | 5623 | ret = set_object_string(device, *newid, PTP_OPC_Artist, artist); |
| 5624 | if (ret != 0) { |
| 5625 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "create_new_abstract_list(): could not set entity artist."); |
| 5626 | return -1; |
| 5627 | } |
| 5628 | } |
| 5629 | |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 5630 | // Update composer |
| 5631 | // FIXME: check if supported |
| 5632 | if (composer != NULL) { |
| 5633 | ret = set_object_string(device, *newid, PTP_OPC_Composer, composer); |
| 5634 | if (ret != 0) { |
| 5635 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "create_new_abstract_list(): could not set entity composer."); |
| 5636 | return -1; |
| 5637 | } |
| 5638 | } |
| 5639 | |
Linus Walleij | adce4a5 | 2007-04-23 07:28:11 +0000 | [diff] [blame] | 5640 | // Update genre |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 5641 | // FIXME: check if supported |
Linus Walleij | adce4a5 | 2007-04-23 07:28:11 +0000 | [diff] [blame] | 5642 | if (genre != NULL) { |
| 5643 | ret = set_object_string(device, *newid, PTP_OPC_Genre, genre); |
| 5644 | if (ret != 0) { |
| 5645 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "create_new_abstract_list(): could not set entity genre."); |
| 5646 | return -1; |
| 5647 | } |
| 5648 | } |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 5649 | |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 5650 | // FIXME: Update date modified |
| 5651 | |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 5652 | } |
| 5653 | |
Linus Walleij | b7426d1 | 2006-11-25 23:19:11 +0000 | [diff] [blame] | 5654 | if (no_tracks > 0) { |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 5655 | // Add tracks to the new playlist as object references. |
Linus Walleij | b7426d1 | 2006-11-25 23:19:11 +0000 | [diff] [blame] | 5656 | ret = ptp_mtp_setobjectreferences (params, *newid, (uint32_t *) tracks, no_tracks); |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 5657 | if (ret != PTP_RC_OK) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 5658 | 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] | 5659 | return -1; |
| 5660 | } |
| 5661 | } |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 5662 | |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 5663 | add_object_to_cache(device, *newid); |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 5664 | |
| 5665 | return 0; |
| 5666 | } |
| 5667 | |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 5668 | /** |
| 5669 | * This updates the metadata and track listing |
| 5670 | * for an abstract list. |
| 5671 | * @param device a pointer to the device that the abstract list |
| 5672 | * resides on. |
| 5673 | * @param name the name of the abstract list. |
| 5674 | * @param artist the artist of the abstract list or NULL. |
| 5675 | * @param genre the genre of the abstract list or NULL. |
| 5676 | * @param objecthandle the object to be updated. |
| 5677 | * @param objectformat the abstract list type to update. |
| 5678 | * @param tracks an array of tracks to associate with this list. |
| 5679 | * @param no_tracks the number of tracks in the list. |
| 5680 | * @return 0 on success, any other value means failure. |
| 5681 | */ |
| 5682 | static int update_abstract_list(LIBMTP_mtpdevice_t *device, |
| 5683 | char const * const name, |
| 5684 | char const * const artist, |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 5685 | char const * const composer, |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 5686 | char const * const genre, |
| 5687 | uint32_t const objecthandle, |
| 5688 | uint16_t const objectformat, |
| 5689 | uint32_t const * const tracks, |
| 5690 | uint32_t const no_tracks) |
| 5691 | { |
| 5692 | uint16_t ret; |
| 5693 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | f9267e9 | 2007-10-15 21:07:37 +0000 | [diff] [blame] | 5694 | PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo; |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 5695 | uint16_t *properties = NULL; |
| 5696 | uint32_t propcnt = 0; |
| 5697 | int i; |
| 5698 | |
| 5699 | // First see which properties can be set |
| 5700 | // i.e only try to update this metadata for object tags that exist on the current player. |
| 5701 | ret = ptp_mtp_getobjectpropssupported(params, objectformat, &propcnt, &properties); |
| 5702 | if (ret != PTP_RC_OK) { |
| 5703 | // Just bail out for now, nothing is ever set. |
| 5704 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "update_abstract_list(): " |
| 5705 | "could not retrieve supported object properties."); |
| 5706 | return -1; |
| 5707 | } |
Linus Walleij | f9267e9 | 2007-10-15 21:07:37 +0000 | [diff] [blame] | 5708 | if (ptp_operation_issupported(params,PTP_OC_MTP_SetObjPropList) && |
Linus Walleij | fec4d56 | 2008-06-01 22:30:36 +0000 | [diff] [blame] | 5709 | !FLAG_BROKEN_SET_OBJECT_PROPLIST(ptp_usb)) { |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 5710 | MTPProperties *props = NULL; |
| 5711 | MTPProperties *prop = NULL; |
| 5712 | int nrofprops = 0; |
| 5713 | |
| 5714 | for (i=0;i<propcnt;i++) { |
| 5715 | PTPObjectPropDesc opd; |
| 5716 | |
| 5717 | ret = ptp_mtp_getobjectpropdesc(params, properties[i], objectformat, &opd); |
| 5718 | if (ret != PTP_RC_OK) { |
| 5719 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "update_abstract_list(): " |
| 5720 | "could not get property description."); |
| 5721 | } else if (opd.GetSet) { |
| 5722 | switch (properties[i]) { |
| 5723 | case PTP_OPC_Name: |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 5724 | prop = ptp_get_new_object_prop_entry(&props, &nrofprops); |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 5725 | prop->ObjectHandle = objecthandle; |
| 5726 | prop->property = PTP_OPC_Name; |
| 5727 | prop->datatype = PTP_DTC_STR; |
| 5728 | if (name != NULL) |
| 5729 | prop->propval.str = strdup(name); |
| 5730 | break; |
Linus Walleij | 0b75ab6 | 2007-12-28 00:47:34 +0000 | [diff] [blame] | 5731 | case PTP_OPC_AlbumArtist: |
| 5732 | if (artist != NULL) { |
| 5733 | prop = ptp_get_new_object_prop_entry(&props, &nrofprops); |
| 5734 | prop->ObjectHandle = objecthandle; |
| 5735 | prop->property = PTP_OPC_AlbumArtist; |
| 5736 | prop->datatype = PTP_DTC_STR; |
| 5737 | prop->propval.str = strdup(artist); |
| 5738 | } |
| 5739 | break; |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 5740 | case PTP_OPC_Artist: |
| 5741 | if (artist != NULL) { |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 5742 | prop = ptp_get_new_object_prop_entry(&props, &nrofprops); |
Linus Walleij | 0b75ab6 | 2007-12-28 00:47:34 +0000 | [diff] [blame] | 5743 | prop->ObjectHandle = objecthandle; |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 5744 | prop->property = PTP_OPC_Artist; |
| 5745 | prop->datatype = PTP_DTC_STR; |
| 5746 | prop->propval.str = strdup(artist); |
| 5747 | } |
| 5748 | break; |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 5749 | case PTP_OPC_Composer: |
| 5750 | if (composer != NULL) { |
| 5751 | prop = ptp_get_new_object_prop_entry(&props, &nrofprops); |
| 5752 | prop->ObjectHandle = objecthandle; |
| 5753 | prop->property = PTP_OPC_Composer; |
| 5754 | prop->datatype = PTP_DTC_STR; |
| 5755 | prop->propval.str = strdup(composer); |
| 5756 | } |
| 5757 | break; |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 5758 | case PTP_OPC_Genre: |
| 5759 | if (genre != NULL) { |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 5760 | prop = ptp_get_new_object_prop_entry(&props, &nrofprops); |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 5761 | prop->ObjectHandle = objecthandle; |
| 5762 | prop->property = PTP_OPC_Genre; |
| 5763 | prop->datatype = PTP_DTC_STR; |
| 5764 | prop->propval.str = strdup(genre); |
| 5765 | } |
| 5766 | break; |
| 5767 | case PTP_OPC_DateModified: |
| 5768 | // Tag with current time if that is supported |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 5769 | prop = ptp_get_new_object_prop_entry(&props, &nrofprops); |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 5770 | prop->ObjectHandle = objecthandle; |
| 5771 | prop->property = PTP_OPC_DateModified; |
| 5772 | prop->datatype = PTP_DTC_STR; |
| 5773 | prop->propval.str = get_iso8601_stamp(); |
| 5774 | break; |
| 5775 | default: |
| 5776 | break; |
| 5777 | } |
| 5778 | } |
| 5779 | ptp_free_objectpropdesc(&opd); |
| 5780 | } |
| 5781 | |
| 5782 | // proplist could be NULL if we can't write any properties |
| 5783 | if (props != NULL) { |
| 5784 | ret = ptp_mtp_setobjectproplist(params, props, nrofprops); |
| 5785 | |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 5786 | ptp_destroy_object_prop_list(props, nrofprops); |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 5787 | |
| 5788 | if (ret != PTP_RC_OK) { |
| 5789 | // TODO: return error of which property we couldn't set |
| 5790 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "update_abstract_list(): " |
| 5791 | "could not set object property list."); |
Richard Low | 016e373 | 2007-09-23 14:53:46 +0000 | [diff] [blame] | 5792 | free(properties); |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 5793 | return -1; |
| 5794 | } |
| 5795 | } |
| 5796 | |
| 5797 | } else if (ptp_operation_issupported(params,PTP_OC_MTP_SetObjectPropValue)) { |
| 5798 | for (i=0;i<propcnt;i++) { |
| 5799 | switch (properties[i]) { |
| 5800 | case PTP_OPC_Name: |
| 5801 | // Update title |
| 5802 | ret = set_object_string(device, objecthandle, PTP_OPC_Name, name); |
| 5803 | if (ret != 0) { |
| 5804 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "update_abstract_list(): " |
| 5805 | "could not set title."); |
| 5806 | } |
| 5807 | break; |
Linus Walleij | 0b75ab6 | 2007-12-28 00:47:34 +0000 | [diff] [blame] | 5808 | case PTP_OPC_AlbumArtist: |
| 5809 | // Update album artist |
| 5810 | ret = set_object_string(device, objecthandle, PTP_OPC_AlbumArtist, artist); |
| 5811 | if (ret != 0) { |
| 5812 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "update_abstract_list(): " |
| 5813 | "could not set album artist name."); |
| 5814 | } |
| 5815 | break; |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 5816 | case PTP_OPC_Artist: |
| 5817 | // Update artist |
| 5818 | ret = set_object_string(device, objecthandle, PTP_OPC_Artist, artist); |
| 5819 | if (ret != 0) { |
| 5820 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "update_abstract_list(): " |
| 5821 | "could not set artist name."); |
| 5822 | } |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 5823 | case PTP_OPC_Composer: |
| 5824 | // Update composer |
| 5825 | ret = set_object_string(device, objecthandle, PTP_OPC_Composer, composer); |
| 5826 | if (ret != 0) { |
| 5827 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "update_abstract_list(): " |
| 5828 | "could not set composer name."); |
| 5829 | } |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 5830 | break; |
| 5831 | case PTP_OPC_Genre: |
| 5832 | // Update genre |
| 5833 | ret = set_object_string(device, objecthandle, PTP_OPC_Genre, genre); |
| 5834 | if (ret != 0) { |
| 5835 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "update_abstract_list(): " |
| 5836 | "could not set genre."); |
| 5837 | } |
| 5838 | break; |
| 5839 | case PTP_OPC_DateModified: |
| 5840 | // Update date modified |
| 5841 | { |
| 5842 | char *tmpdate = get_iso8601_stamp(); |
| 5843 | ret = set_object_string(device, objecthandle, PTP_OPC_DateModified, tmpdate); |
| 5844 | if (ret != 0) { |
| 5845 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "update_abstract_list(): " |
| 5846 | "could not set modification date."); |
| 5847 | } |
| 5848 | free(tmpdate); |
| 5849 | } |
| 5850 | default: |
| 5851 | break; |
| 5852 | } |
| 5853 | } |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 5854 | } else { |
| 5855 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "update_abstract_list(): " |
| 5856 | "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] | 5857 | free(properties); |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 5858 | return -1; |
| 5859 | } |
| 5860 | |
| 5861 | // Then the object references... |
| 5862 | if (no_tracks > 0) { |
| 5863 | // Add tracks to the new album as object references. |
| 5864 | ret = ptp_mtp_setobjectreferences (params, objecthandle, (uint32_t *) tracks, no_tracks); |
| 5865 | if (ret != PTP_RC_OK) { |
| 5866 | add_ptp_error_to_errorstack(device, ret, "update_abstract_list(): could not add tracks as object references."); |
Richard Low | 016e373 | 2007-09-23 14:53:46 +0000 | [diff] [blame] | 5867 | free(properties); |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 5868 | return -1; |
| 5869 | } |
| 5870 | } |
| 5871 | |
Richard Low | 016e373 | 2007-09-23 14:53:46 +0000 | [diff] [blame] | 5872 | free(properties); |
Linus Walleij | 7752b95 | 2007-10-19 20:11:46 +0000 | [diff] [blame] | 5873 | |
| 5874 | update_metadata_cache(device, objecthandle); |
Richard Low | 016e373 | 2007-09-23 14:53:46 +0000 | [diff] [blame] | 5875 | |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 5876 | return 0; |
| 5877 | } |
| 5878 | |
Linus Walleij | b7426d1 | 2006-11-25 23:19:11 +0000 | [diff] [blame] | 5879 | |
| 5880 | /** |
| 5881 | * This routine creates a new playlist based on the metadata |
| 5882 | * supplied. If the <code>tracks</code> field of the metadata |
| 5883 | * contains a track listing, these tracks will be added to the |
| 5884 | * playlist. |
| 5885 | * @param device a pointer to the device to create the new playlist on. |
| 5886 | * @param metadata the metadata for the new playlist. If the function |
| 5887 | * exits with success, the <code>playlist_id</code> field of this |
| 5888 | * struct will contain the new playlist ID of the playlist. |
Linus Walleij | 5ce59db | 2008-03-12 21:22:58 +0000 | [diff] [blame] | 5889 | * <code>parent_id</code> will also be valid. |
Linus Walleij | b7426d1 | 2006-11-25 23:19:11 +0000 | [diff] [blame] | 5890 | * @param parenthandle the parent (e.g. folder) to store this playlist |
| 5891 | * in. Pass in 0 to put the playlist in the root directory. |
| 5892 | * @return 0 on success, any other value means failure. |
| 5893 | * @see LIBMTP_Update_Playlist() |
| 5894 | * @see LIBMTP_Delete_Object() |
| 5895 | */ |
| 5896 | int LIBMTP_Create_New_Playlist(LIBMTP_mtpdevice_t *device, |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 5897 | LIBMTP_playlist_t * const metadata) |
Linus Walleij | b7426d1 | 2006-11-25 23:19:11 +0000 | [diff] [blame] | 5898 | { |
Linus Walleij | a4e6bdc | 2008-05-23 22:31:53 +0000 | [diff] [blame] | 5899 | PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo; |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 5900 | uint32_t localph = metadata->parent_id; |
Linus Walleij | b7426d1 | 2006-11-25 23:19:11 +0000 | [diff] [blame] | 5901 | |
| 5902 | // Use a default folder if none given |
| 5903 | if (localph == 0) { |
| 5904 | localph = device->default_playlist_folder; |
| 5905 | } |
Linus Walleij | 5ce59db | 2008-03-12 21:22:58 +0000 | [diff] [blame] | 5906 | metadata->parent_id = localph; |
Linus Walleij | b7426d1 | 2006-11-25 23:19:11 +0000 | [diff] [blame] | 5907 | |
Linus Walleij | f3c4405 | 2008-08-16 21:14:56 +0000 | [diff] [blame] | 5908 | // Samsung needs its own special type of playlists |
| 5909 | if(FLAG_PLAYLIST_SPL(ptp_usb)) { |
| 5910 | return playlist_t_to_spl(device, metadata); |
| 5911 | } |
| 5912 | |
Linus Walleij | b7426d1 | 2006-11-25 23:19:11 +0000 | [diff] [blame] | 5913 | // Just create a new abstract audio/video playlist... |
| 5914 | return create_new_abstract_list(device, |
| 5915 | metadata->name, |
Linus Walleij | adce4a5 | 2007-04-23 07:28:11 +0000 | [diff] [blame] | 5916 | NULL, |
| 5917 | NULL, |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 5918 | NULL, |
Linus Walleij | b7426d1 | 2006-11-25 23:19:11 +0000 | [diff] [blame] | 5919 | localph, |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 5920 | metadata->storage_id, |
Linus Walleij | b7426d1 | 2006-11-25 23:19:11 +0000 | [diff] [blame] | 5921 | PTP_OFC_MTP_AbstractAudioVideoPlaylist, |
Linus Walleij | a4e6bdc | 2008-05-23 22:31:53 +0000 | [diff] [blame] | 5922 | get_playlist_extension(ptp_usb), |
Linus Walleij | b7426d1 | 2006-11-25 23:19:11 +0000 | [diff] [blame] | 5923 | &metadata->playlist_id, |
| 5924 | metadata->tracks, |
| 5925 | metadata->no_tracks); |
| 5926 | } |
| 5927 | |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 5928 | /** |
| 5929 | * This routine updates a playlist based on the metadata |
| 5930 | * supplied. If the <code>tracks</code> field of the metadata |
| 5931 | * contains a track listing, these tracks will be added to the |
| 5932 | * playlist in place of those already present, i.e. the |
| 5933 | * previous track listing will be deleted. |
| 5934 | * @param device a pointer to the device to create the new playlist on. |
| 5935 | * @param metadata the metadata for the playlist to be updated. |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 5936 | * notice that the field <code>playlist_id</code> |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 5937 | * must contain the apropriate playlist ID. |
| 5938 | * @return 0 on success, any other value means failure. |
| 5939 | * @see LIBMTP_Create_New_Playlist() |
| 5940 | * @see LIBMTP_Delete_Object() |
| 5941 | */ |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 5942 | int LIBMTP_Update_Playlist(LIBMTP_mtpdevice_t *device, |
Linus Walleij | f530635 | 2006-06-08 12:00:23 +0000 | [diff] [blame] | 5943 | LIBMTP_playlist_t const * const metadata) |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 5944 | { |
Linus Walleij | f3c4405 | 2008-08-16 21:14:56 +0000 | [diff] [blame] | 5945 | |
| 5946 | // Samsung needs its own special type of playlists |
| 5947 | PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo; |
| 5948 | if(FLAG_PLAYLIST_SPL(ptp_usb)) { |
| 5949 | return update_spl_playlist(device, metadata); |
| 5950 | } |
| 5951 | |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 5952 | return update_abstract_list(device, |
| 5953 | metadata->name, |
| 5954 | NULL, |
| 5955 | NULL, |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 5956 | NULL, |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 5957 | metadata->playlist_id, |
| 5958 | PTP_OFC_MTP_AbstractAudioVideoPlaylist, |
| 5959 | metadata->tracks, |
| 5960 | metadata->no_tracks); |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 5961 | } |
Linus Walleij | aa4b075 | 2006-07-26 22:21:04 +0000 | [diff] [blame] | 5962 | |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 5963 | /** |
| 5964 | * This creates a new album metadata structure and allocates memory |
| 5965 | * for it. Notice that if you add strings to this structure they |
| 5966 | * will be freed by the corresponding <code>LIBMTP_destroy_album_t</code> |
| 5967 | * operation later, so be careful of using strdup() when assigning |
| 5968 | * strings. |
| 5969 | * |
| 5970 | * @return a pointer to the newly allocated metadata structure. |
| 5971 | * @see LIBMTP_destroy_album_t() |
| 5972 | */ |
| 5973 | LIBMTP_album_t *LIBMTP_new_album_t(void) |
| 5974 | { |
| 5975 | LIBMTP_album_t *new = (LIBMTP_album_t *) malloc(sizeof(LIBMTP_album_t)); |
| 5976 | if (new == NULL) { |
| 5977 | return NULL; |
| 5978 | } |
| 5979 | new->album_id = 0; |
Linus Walleij | 5ce59db | 2008-03-12 21:22:58 +0000 | [diff] [blame] | 5980 | new->parent_id = 0; |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 5981 | new->storage_id = 0; |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 5982 | new->name = NULL; |
Linus Walleij | adce4a5 | 2007-04-23 07:28:11 +0000 | [diff] [blame] | 5983 | new->artist = NULL; |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 5984 | new->composer = NULL; |
Linus Walleij | adce4a5 | 2007-04-23 07:28:11 +0000 | [diff] [blame] | 5985 | new->genre = NULL; |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 5986 | new->tracks = NULL; |
| 5987 | new->no_tracks = 0; |
| 5988 | new->next = NULL; |
| 5989 | return new; |
| 5990 | } |
| 5991 | |
| 5992 | /** |
| 5993 | * This recursively deletes the memory for an album structure |
| 5994 | * |
| 5995 | * @param album structure to destroy |
| 5996 | * @see LIBMTP_new_album_t() |
| 5997 | */ |
| 5998 | void LIBMTP_destroy_album_t(LIBMTP_album_t *album) |
| 5999 | { |
| 6000 | if (album == NULL) { |
| 6001 | return; |
| 6002 | } |
| 6003 | if (album->name != NULL) |
| 6004 | free(album->name); |
Linus Walleij | adce4a5 | 2007-04-23 07:28:11 +0000 | [diff] [blame] | 6005 | if (album->artist != NULL) |
| 6006 | free(album->artist); |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 6007 | if (album->composer != NULL) |
| 6008 | free(album->composer); |
Linus Walleij | adce4a5 | 2007-04-23 07:28:11 +0000 | [diff] [blame] | 6009 | if (album->genre != NULL) |
| 6010 | free(album->genre); |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 6011 | if (album->tracks != NULL) |
| 6012 | free(album->tracks); |
| 6013 | free(album); |
| 6014 | return; |
| 6015 | } |
| 6016 | |
| 6017 | /** |
| 6018 | * This function returns a list of the albums available on the |
| 6019 | * device. |
| 6020 | * |
| 6021 | * @param device a pointer to the device to get the album listing from. |
| 6022 | * @return an album list on success, else NULL. If there are no albums |
| 6023 | * on the device, NULL will be returned as well. |
| 6024 | * @see LIBMTP_Get_Album() |
| 6025 | */ |
| 6026 | LIBMTP_album_t *LIBMTP_Get_Album_List(LIBMTP_mtpdevice_t *device) |
| 6027 | { |
| 6028 | PTPParams *params = (PTPParams *) device->params; |
| 6029 | LIBMTP_album_t *retalbums = NULL; |
| 6030 | LIBMTP_album_t *curalbum = NULL; |
| 6031 | uint32_t i; |
| 6032 | |
| 6033 | // Get all the handles if we haven't already done that |
| 6034 | if (params->handles.Handler == NULL) { |
| 6035 | flush_handles(device); |
| 6036 | } |
| 6037 | |
| 6038 | for (i = 0; i < params->handles.n; i++) { |
| 6039 | LIBMTP_album_t *alb; |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 6040 | PTPObjectInfo *oi; |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 6041 | uint16_t ret; |
| 6042 | |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 6043 | oi = ¶ms->objectinfo[i]; |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 6044 | |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 6045 | // Ignore stuff that isn't an album |
| 6046 | if ( oi->ObjectFormat != PTP_OFC_MTP_AbstractAudioAlbum ) { |
| 6047 | continue; |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 6048 | } |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 6049 | |
| 6050 | // Allocate a new album type |
| 6051 | alb = LIBMTP_new_album_t(); |
Linus Walleij | 5ce59db | 2008-03-12 21:22:58 +0000 | [diff] [blame] | 6052 | alb->album_id = params->handles.Handler[i]; |
| 6053 | alb->parent_id = oi->ParentObject; |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 6054 | alb->storage_id = oi->StorageID; |
Linus Walleij | 5ce59db | 2008-03-12 21:22:58 +0000 | [diff] [blame] | 6055 | |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 6056 | // Get metadata for it. |
| 6057 | alb->name = get_string_from_object(device, params->handles.Handler[i], PTP_OPC_Name); |
Linus Walleij | 0b75ab6 | 2007-12-28 00:47:34 +0000 | [diff] [blame] | 6058 | alb->artist = get_string_from_object(device, params->handles.Handler[i], PTP_OPC_AlbumArtist); |
| 6059 | if (alb->artist == NULL) { |
| 6060 | alb->artist = get_string_from_object(device, params->handles.Handler[i], PTP_OPC_Artist); |
| 6061 | } |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 6062 | alb->composer = get_string_from_object(device, params->handles.Handler[i], PTP_OPC_Composer); |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 6063 | alb->genre = get_string_from_object(device, params->handles.Handler[i], PTP_OPC_Genre); |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 6064 | |
| 6065 | // Then get the track listing for this album |
| 6066 | ret = ptp_mtp_getobjectreferences(params, alb->album_id, &alb->tracks, &alb->no_tracks); |
| 6067 | if (ret != PTP_RC_OK) { |
| 6068 | add_ptp_error_to_errorstack(device, ret, "LIBMTP_Get_Album_List(): Could not get object references."); |
| 6069 | alb->tracks = NULL; |
| 6070 | alb->no_tracks = 0; |
| 6071 | } |
| 6072 | |
| 6073 | // Add album to a list that will be returned afterwards. |
| 6074 | if (retalbums == NULL) { |
| 6075 | retalbums = alb; |
| 6076 | curalbum = alb; |
| 6077 | } else { |
| 6078 | curalbum->next = alb; |
| 6079 | curalbum = alb; |
| 6080 | } |
| 6081 | |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 6082 | } |
| 6083 | return retalbums; |
| 6084 | } |
| 6085 | |
| 6086 | /** |
| 6087 | * This function retrieves an individual album from the device. |
| 6088 | * @param device a pointer to the device to get the album from. |
| 6089 | * @param albid the unique ID of the album to retrieve. |
| 6090 | * @return a valid album metadata or NULL on failure. |
| 6091 | * @see LIBMTP_Get_Album_List() |
| 6092 | */ |
| 6093 | LIBMTP_album_t *LIBMTP_Get_Album(LIBMTP_mtpdevice_t *device, uint32_t const albid) |
| 6094 | { |
| 6095 | PTPParams *params = (PTPParams *) device->params; |
| 6096 | uint32_t i; |
| 6097 | |
| 6098 | // Get all the handles if we haven't already done that |
| 6099 | if (params->handles.Handler == NULL) { |
| 6100 | flush_handles(device); |
| 6101 | } |
| 6102 | |
| 6103 | for (i = 0; i < params->handles.n; i++) { |
| 6104 | LIBMTP_album_t *alb; |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 6105 | PTPObjectInfo *oi; |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 6106 | uint16_t ret; |
| 6107 | |
| 6108 | if (params->handles.Handler[i] != albid) { |
| 6109 | continue; |
| 6110 | } |
| 6111 | |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 6112 | oi = ¶ms->objectinfo[i]; |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 6113 | |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 6114 | // Ignore stuff that isn't an album |
| 6115 | if ( oi->ObjectFormat != PTP_OFC_MTP_AbstractAudioAlbum ) { |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 6116 | return NULL; |
| 6117 | } |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 6118 | |
| 6119 | // Allocate a new album type |
| 6120 | alb = LIBMTP_new_album_t(); |
| 6121 | alb->album_id = params->handles.Handler[i]; |
Linus Walleij | 5ce59db | 2008-03-12 21:22:58 +0000 | [diff] [blame] | 6122 | alb->parent_id = oi->ParentObject; |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 6123 | alb->storage_id = oi->StorageID; |
Linus Walleij | 5ce59db | 2008-03-12 21:22:58 +0000 | [diff] [blame] | 6124 | |
| 6125 | // Get metadata for it. |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 6126 | alb->name = get_string_from_object(device, params->handles.Handler[i], PTP_OPC_Name); |
Linus Walleij | 0b75ab6 | 2007-12-28 00:47:34 +0000 | [diff] [blame] | 6127 | alb->artist = get_string_from_object(device, params->handles.Handler[i], PTP_OPC_AlbumArtist); |
| 6128 | if (alb->artist == NULL) { |
| 6129 | alb->artist = get_string_from_object(device, params->handles.Handler[i], PTP_OPC_Artist); |
| 6130 | } |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 6131 | alb->composer = get_string_from_object(device, params->handles.Handler[i], PTP_OPC_Composer); |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 6132 | alb->genre = get_string_from_object(device, params->handles.Handler[i], PTP_OPC_Genre); |
| 6133 | ret = ptp_mtp_getobjectreferences(params, alb->album_id, &alb->tracks, &alb->no_tracks); |
| 6134 | if (ret != PTP_RC_OK) { |
| 6135 | add_ptp_error_to_errorstack(device, ret, "LIBMTP_Get_Album: Could not get object references."); |
| 6136 | alb->tracks = NULL; |
| 6137 | alb->no_tracks = 0; |
| 6138 | } |
| 6139 | |
| 6140 | return alb; |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 6141 | } |
| 6142 | return NULL; |
| 6143 | } |
| 6144 | |
| 6145 | /** |
| 6146 | * This routine creates a new album based on the metadata |
| 6147 | * supplied. If the <code>tracks</code> field of the metadata |
| 6148 | * contains a track listing, these tracks will be added to the |
| 6149 | * album. |
| 6150 | * @param device a pointer to the device to create the new album on. |
| 6151 | * @param metadata the metadata for the new album. If the function |
| 6152 | * exits with success, the <code>album_id</code> field of this |
| 6153 | * struct will contain the new ID of the album. |
Linus Walleij | 5ce59db | 2008-03-12 21:22:58 +0000 | [diff] [blame] | 6154 | * <code>parent_id</code> will also be valid. |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 6155 | * @param parenthandle the parent (e.g. folder) to store this album |
| 6156 | * in. Pass in 0 to put the album in the default music directory. |
| 6157 | * @return 0 on success, any other value means failure. |
| 6158 | * @see LIBMTP_Update_Album() |
| 6159 | * @see LIBMTP_Delete_Object() |
| 6160 | */ |
| 6161 | int LIBMTP_Create_New_Album(LIBMTP_mtpdevice_t *device, |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 6162 | LIBMTP_album_t * const metadata) |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 6163 | { |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 6164 | uint32_t localph = metadata->parent_id; |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 6165 | |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 6166 | // Use a default folder if none given |
| 6167 | if (localph == 0) { |
rreardon | 508705f | 2006-11-19 21:27:22 +0000 | [diff] [blame] | 6168 | localph = device->default_album_folder; |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 6169 | } |
Linus Walleij | 5ce59db | 2008-03-12 21:22:58 +0000 | [diff] [blame] | 6170 | metadata->parent_id = localph; |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 6171 | |
Linus Walleij | b7426d1 | 2006-11-25 23:19:11 +0000 | [diff] [blame] | 6172 | // Just create a new abstract album... |
| 6173 | return create_new_abstract_list(device, |
| 6174 | metadata->name, |
Linus Walleij | adce4a5 | 2007-04-23 07:28:11 +0000 | [diff] [blame] | 6175 | metadata->artist, |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 6176 | metadata->composer, |
Linus Walleij | adce4a5 | 2007-04-23 07:28:11 +0000 | [diff] [blame] | 6177 | metadata->genre, |
Linus Walleij | b7426d1 | 2006-11-25 23:19:11 +0000 | [diff] [blame] | 6178 | localph, |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 6179 | metadata->storage_id, |
Linus Walleij | b7426d1 | 2006-11-25 23:19:11 +0000 | [diff] [blame] | 6180 | PTP_OFC_MTP_AbstractAudioAlbum, |
| 6181 | ".alb", |
| 6182 | &metadata->album_id, |
| 6183 | metadata->tracks, |
| 6184 | metadata->no_tracks); |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 6185 | } |
| 6186 | |
| 6187 | /** |
rreardon | 5332f9c | 2006-12-05 10:18:23 +0000 | [diff] [blame] | 6188 | * This creates a new sample data metadata structure and allocates memory |
| 6189 | * for it. Notice that if you add strings to this structure they |
| 6190 | * will be freed by the corresponding <code>LIBMTP_destroy_sampledata_t</code> |
| 6191 | * operation later, so be careful of using strdup() when assigning |
| 6192 | * strings. |
| 6193 | * |
| 6194 | * @return a pointer to the newly allocated metadata structure. |
| 6195 | * @see LIBMTP_destroy_sampledata_t() |
| 6196 | */ |
| 6197 | LIBMTP_filesampledata_t *LIBMTP_new_filesampledata_t(void) |
| 6198 | { |
| 6199 | LIBMTP_filesampledata_t *new = (LIBMTP_filesampledata_t *) malloc(sizeof(LIBMTP_filesampledata_t)); |
| 6200 | if (new == NULL) { |
| 6201 | return NULL; |
| 6202 | } |
| 6203 | new->height=0; |
| 6204 | new->width = 0; |
| 6205 | new->data = NULL; |
| 6206 | new->duration = 0; |
| 6207 | new->size = 0; |
| 6208 | return new; |
| 6209 | } |
| 6210 | |
Linus Walleij | f1b02f2 | 2006-12-06 09:03:23 +0000 | [diff] [blame] | 6211 | /** |
| 6212 | * This destroys a file sample metadata type. |
| 6213 | * @param sample the file sample metadata to be destroyed. |
| 6214 | */ |
| 6215 | void LIBMTP_destroy_filesampledata_t(LIBMTP_filesampledata_t * sample) |
| 6216 | { |
| 6217 | if (sample == NULL) { |
| 6218 | return; |
| 6219 | } |
| 6220 | if (sample->data != NULL) { |
| 6221 | free(sample->data); |
| 6222 | } |
| 6223 | free(sample); |
| 6224 | } |
| 6225 | |
| 6226 | /** |
| 6227 | * This routine figures out whether a certain filetype supports |
| 6228 | * representative samples (small thumbnail images) or not. This |
| 6229 | * typically applies to JPEG files, MP3 files and Album abstract |
| 6230 | * playlists, but in theory any filetype could support representative |
| 6231 | * samples. |
| 6232 | * @param device a pointer to the device which is to be examined. |
Linus Walleij | 9ffe998 | 2008-01-20 13:42:52 +0000 | [diff] [blame] | 6233 | * @param filetype the fileype to examine, and return the representative sample |
Linus Walleij | f1b02f2 | 2006-12-06 09:03:23 +0000 | [diff] [blame] | 6234 | * properties for. |
| 6235 | * @param sample this will contain a new sample type with the fields |
| 6236 | * filled in with suitable default values. For example, the |
| 6237 | * supported sample type will be set, the supported height and |
| 6238 | * width will be set to max values if it is an image sample, |
| 6239 | * and duration will also be given some suitable default value |
| 6240 | * which should not be exceeded on audio samples. If the |
| 6241 | * device does not support samples for this filetype, this |
| 6242 | * pointer will be NULL. If it is not NULL, the user must |
| 6243 | * destroy this struct with <code>LIBMTP_destroy_filesampledata_t()</code> |
| 6244 | * after use. |
| 6245 | * @return 0 on success, any other value means failure. |
| 6246 | * @see LIBMTP_Send_Representative_Sample() |
| 6247 | * @see LIBMTP_Create_New_Album() |
| 6248 | */ |
| 6249 | int LIBMTP_Get_Representative_Sample_Format(LIBMTP_mtpdevice_t *device, |
| 6250 | LIBMTP_filetype_t const filetype, |
| 6251 | LIBMTP_filesampledata_t ** sample) |
| 6252 | { |
| 6253 | uint16_t ret; |
| 6254 | PTPParams *params = (PTPParams *) device->params; |
| 6255 | uint16_t *props = NULL; |
| 6256 | uint32_t propcnt = 0; |
| 6257 | int i; |
| 6258 | // TODO: Get rid of these when we can properly query the device. |
| 6259 | int support_data = 0; |
| 6260 | int support_format = 0; |
| 6261 | int support_height = 0; |
| 6262 | int support_width = 0; |
| 6263 | int support_duration = 0; |
| 6264 | |
rreardon | 21a3a9d | 2007-08-15 10:56:31 +0000 | [diff] [blame] | 6265 | PTPObjectPropDesc opd_height; |
| 6266 | PTPObjectPropDesc opd_width; |
| 6267 | PTPObjectPropDesc opd_format; |
| 6268 | PTPObjectPropDesc opd_duration; |
| 6269 | |
Linus Walleij | f1b02f2 | 2006-12-06 09:03:23 +0000 | [diff] [blame] | 6270 | // Default to no type supported. |
| 6271 | *sample = NULL; |
| 6272 | |
| 6273 | ret = ptp_mtp_getobjectpropssupported(params, map_libmtp_type_to_ptp_type(filetype), &propcnt, &props); |
| 6274 | if (ret != PTP_RC_OK) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 6275 | 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] | 6276 | return -1; |
| 6277 | } |
| 6278 | /* |
| 6279 | * TODO: when walking through these object properties, make calls to |
| 6280 | * a new function in ptp.h/ptp.c that can send the command |
| 6281 | * PTP_OC_MTP_GetObjectPropDesc to get max/min values of the properties |
| 6282 | * supported. |
| 6283 | */ |
| 6284 | for (i = 0; i < propcnt; i++) { |
| 6285 | switch(props[i]) { |
| 6286 | case PTP_OPC_RepresentativeSampleData: |
| 6287 | support_data = 1; |
| 6288 | break; |
| 6289 | case PTP_OPC_RepresentativeSampleFormat: |
| 6290 | support_format = 1; |
| 6291 | break; |
| 6292 | case PTP_OPC_RepresentativeSampleSize: |
| 6293 | break; |
| 6294 | case PTP_OPC_RepresentativeSampleHeight: |
| 6295 | support_height = 1; |
| 6296 | break; |
| 6297 | case PTP_OPC_RepresentativeSampleWidth: |
| 6298 | support_width = 1; |
| 6299 | break; |
| 6300 | case PTP_OPC_RepresentativeSampleDuration: |
| 6301 | support_duration = 1; |
| 6302 | break; |
| 6303 | default: |
| 6304 | break; |
| 6305 | } |
| 6306 | } |
| 6307 | free(props); |
rreardon | 21a3a9d | 2007-08-15 10:56:31 +0000 | [diff] [blame] | 6308 | |
Linus Walleij | f1b02f2 | 2006-12-06 09:03:23 +0000 | [diff] [blame] | 6309 | if (support_data && support_format && support_height && support_width && !support_duration) { |
| 6310 | // Something that supports height and width and not duration is likely to be JPEG |
| 6311 | LIBMTP_filesampledata_t *retsam = LIBMTP_new_filesampledata_t(); |
rreardon | 21a3a9d | 2007-08-15 10:56:31 +0000 | [diff] [blame] | 6312 | /* |
| 6313 | * Populate the sample format with the first supported format |
| 6314 | * |
| 6315 | * TODO: figure out how to pass back more than one format if more are |
| 6316 | * supported by the device. |
| 6317 | */ |
| 6318 | ptp_mtp_getobjectpropdesc (params, PTP_OPC_RepresentativeSampleFormat, map_libmtp_type_to_ptp_type(filetype), &opd_format); |
| 6319 | retsam->filetype = map_ptp_type_to_libmtp_type(opd_format.FORM.Enum.SupportedValue[0].u16); |
| 6320 | /* Populate the maximum image height */ |
| 6321 | ptp_mtp_getobjectpropdesc (params, PTP_OPC_RepresentativeSampleWidth, map_libmtp_type_to_ptp_type(filetype), &opd_width); |
| 6322 | retsam->width = opd_width.FORM.Range.MaximumValue.u32; |
| 6323 | /* Populate the maximum image width */ |
| 6324 | ptp_mtp_getobjectpropdesc (params, PTP_OPC_RepresentativeSampleHeight, map_libmtp_type_to_ptp_type(filetype), &opd_height); |
| 6325 | retsam->height = opd_height.FORM.Range.MaximumValue.u32; |
Linus Walleij | f1b02f2 | 2006-12-06 09:03:23 +0000 | [diff] [blame] | 6326 | *sample = retsam; |
| 6327 | } else if (support_data && support_format && !support_height && !support_width && support_duration) { |
| 6328 | // Another qualified guess |
| 6329 | LIBMTP_filesampledata_t *retsam = LIBMTP_new_filesampledata_t(); |
rreardon | 21a3a9d | 2007-08-15 10:56:31 +0000 | [diff] [blame] | 6330 | /* |
| 6331 | * Populate the sample format with the first supported format |
| 6332 | * |
| 6333 | * TODO: figure out how to pass back more than one format if more are |
| 6334 | * supported by the device. |
| 6335 | */ |
| 6336 | ptp_mtp_getobjectpropdesc (params, PTP_OPC_RepresentativeSampleFormat, map_libmtp_type_to_ptp_type(filetype), &opd_format); |
| 6337 | retsam->filetype = map_ptp_type_to_libmtp_type(opd_format.FORM.Enum.SupportedValue[0].u16); |
| 6338 | /* Populate the maximum duration */ |
| 6339 | ptp_mtp_getobjectpropdesc (params, PTP_OPC_RepresentativeSampleDuration, map_libmtp_type_to_ptp_type(filetype), &opd_duration); |
| 6340 | retsam->duration = opd_duration.FORM.Range.MaximumValue.u32; |
Linus Walleij | f1b02f2 | 2006-12-06 09:03:23 +0000 | [diff] [blame] | 6341 | *sample = retsam; |
| 6342 | } |
| 6343 | return 0; |
| 6344 | } |
rreardon | 5332f9c | 2006-12-05 10:18:23 +0000 | [diff] [blame] | 6345 | |
| 6346 | /** |
| 6347 | * This routine sends representative sample data for an object. |
| 6348 | * This uses the RepresentativeSampleData property of the album, |
| 6349 | * if the device supports it. The data should be of a format acceptable |
| 6350 | * to the player (for iRiver and Creative, this seems to be JPEG) and |
| 6351 | * must not be too large. (for a Creative, max seems to be about 20KB.) |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 6352 | * TODO: there must be a way to find the max size for an ObjectPropertyValue. |
Linus Walleij | 7e3f276 | 2006-12-03 22:52:05 +0000 | [diff] [blame] | 6353 | * @param device a pointer to the device which the object is on. |
| 6354 | * @param id unique id of the object to set artwork for. |
Richard Low | 36e447c | 2008-01-20 15:24:55 +0000 | [diff] [blame] | 6355 | * @param pointer to LIBMTP_filesampledata_t struct containing data |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 6356 | * @return 0 on success, any other value means failure. |
Richard Low | 36e447c | 2008-01-20 15:24:55 +0000 | [diff] [blame] | 6357 | * @see LIBMTP_Get_Representative_Sample() |
Linus Walleij | f1b02f2 | 2006-12-06 09:03:23 +0000 | [diff] [blame] | 6358 | * @see LIBMTP_Get_Representative_Sample_Format() |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 6359 | * @see LIBMTP_Create_New_Album() |
| 6360 | */ |
Linus Walleij | 7e3f276 | 2006-12-03 22:52:05 +0000 | [diff] [blame] | 6361 | int LIBMTP_Send_Representative_Sample(LIBMTP_mtpdevice_t *device, |
rreardon | 5332f9c | 2006-12-05 10:18:23 +0000 | [diff] [blame] | 6362 | uint32_t const id, |
| 6363 | LIBMTP_filesampledata_t *sampledata) |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 6364 | { |
| 6365 | uint16_t ret; |
| 6366 | PTPParams *params = (PTPParams *) device->params; |
Richard Low | bb9fb4a | 2008-05-18 14:49:34 +0000 | [diff] [blame] | 6367 | PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo; |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 6368 | PTPPropertyValue propval; |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 6369 | PTPObjectInfo *oi; |
| 6370 | uint32_t i; |
Linus Walleij | f1b02f2 | 2006-12-06 09:03:23 +0000 | [diff] [blame] | 6371 | uint16_t *props = NULL; |
| 6372 | uint32_t propcnt = 0; |
| 6373 | int supported = 0; |
rreardon | 5332f9c | 2006-12-05 10:18:23 +0000 | [diff] [blame] | 6374 | |
| 6375 | // get the file format for the object we're going to send representative data for |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 6376 | oi = NULL; |
| 6377 | for (i = 0; i < params->handles.n; i++) { |
| 6378 | if (params->handles.Handler[i] == id) { |
| 6379 | oi = ¶ms->objectinfo[i]; |
| 6380 | break; |
| 6381 | } |
| 6382 | } |
rreardon | 5332f9c | 2006-12-05 10:18:23 +0000 | [diff] [blame] | 6383 | |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 6384 | if (oi == NULL) { |
| 6385 | 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] | 6386 | return -1; |
| 6387 | } |
| 6388 | |
| 6389 | // check that we can send representative sample data for this object format |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 6390 | ret = ptp_mtp_getobjectpropssupported(params, oi->ObjectFormat, &propcnt, &props); |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 6391 | if (ret != PTP_RC_OK) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 6392 | 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] | 6393 | return -1; |
| 6394 | } |
Linus Walleij | f1b02f2 | 2006-12-06 09:03:23 +0000 | [diff] [blame] | 6395 | |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 6396 | for (i = 0; i < propcnt; i++) { |
Linus Walleij | f1b02f2 | 2006-12-06 09:03:23 +0000 | [diff] [blame] | 6397 | if (props[i] == PTP_OPC_RepresentativeSampleData) { |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 6398 | supported = 1; |
Linus Walleij | f1b02f2 | 2006-12-06 09:03:23 +0000 | [diff] [blame] | 6399 | break; |
| 6400 | } |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 6401 | } |
| 6402 | if (!supported) { |
Linus Walleij | f1b02f2 | 2006-12-06 09:03:23 +0000 | [diff] [blame] | 6403 | free(props); |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 6404 | 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] | 6405 | return -1; |
| 6406 | } |
Linus Walleij | f1b02f2 | 2006-12-06 09:03:23 +0000 | [diff] [blame] | 6407 | free(props); |
rreardon | 5332f9c | 2006-12-05 10:18:23 +0000 | [diff] [blame] | 6408 | |
Linus Walleij | f1b02f2 | 2006-12-06 09:03:23 +0000 | [diff] [blame] | 6409 | // Go ahead and send the data |
rreardon | 5332f9c | 2006-12-05 10:18:23 +0000 | [diff] [blame] | 6410 | propval.a.count = sampledata->size; |
| 6411 | propval.a.v = malloc(sizeof(PTPPropertyValue) * sampledata->size); |
| 6412 | for (i = 0; i < sampledata->size; i++) { |
| 6413 | propval.a.v[i].u8 = sampledata->data[i]; |
Linus Walleij | 7e3f276 | 2006-12-03 22:52:05 +0000 | [diff] [blame] | 6414 | } |
| 6415 | |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 6416 | ret = ptp_mtp_setobjectpropvalue(params,id,PTP_OPC_RepresentativeSampleData, |
Linus Walleij | f1b02f2 | 2006-12-06 09:03:23 +0000 | [diff] [blame] | 6417 | &propval,PTP_DTC_AUINT8); |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 6418 | if (ret != PTP_RC_OK) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 6419 | 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] | 6420 | free(propval.a.v); |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 6421 | return -1; |
| 6422 | } |
Linus Walleij | 7e3f276 | 2006-12-03 22:52:05 +0000 | [diff] [blame] | 6423 | free(propval.a.v); |
rreardon | d2ddb63 | 2006-12-12 12:13:21 +0000 | [diff] [blame] | 6424 | |
| 6425 | /* Set the height and width if the sample is an image, otherwise just |
| 6426 | * set the duration and size */ |
| 6427 | switch(sampledata->filetype) { |
Linus Walleij | e1ac07e | 2006-12-14 19:38:59 +0000 | [diff] [blame] | 6428 | case LIBMTP_FILETYPE_JPEG: |
| 6429 | case LIBMTP_FILETYPE_JFIF: |
| 6430 | case LIBMTP_FILETYPE_TIFF: |
| 6431 | case LIBMTP_FILETYPE_BMP: |
| 6432 | case LIBMTP_FILETYPE_GIF: |
| 6433 | case LIBMTP_FILETYPE_PICT: |
| 6434 | case LIBMTP_FILETYPE_PNG: |
Linus Walleij | fec4d56 | 2008-06-01 22:30:36 +0000 | [diff] [blame] | 6435 | if (!FLAG_BROKEN_SET_SAMPLE_DIMENSIONS(ptp_usb)) { |
Richard Low | bb9fb4a | 2008-05-18 14:49:34 +0000 | [diff] [blame] | 6436 | // For images, set the height and width |
| 6437 | set_object_u32(device, id, PTP_OPC_RepresentativeSampleHeight, sampledata->height); |
| 6438 | set_object_u32(device, id, PTP_OPC_RepresentativeSampleWidth, sampledata->width); |
| 6439 | } |
Linus Walleij | e1ac07e | 2006-12-14 19:38:59 +0000 | [diff] [blame] | 6440 | break; |
| 6441 | default: |
| 6442 | // For anything not an image, set the duration and size |
| 6443 | set_object_u32(device, id, PTP_OPC_RepresentativeSampleDuration, sampledata->duration); |
| 6444 | set_object_u32(device, id, PTP_OPC_RepresentativeSampleSize, sampledata->size); |
| 6445 | break; |
rreardon | d2ddb63 | 2006-12-12 12:13:21 +0000 | [diff] [blame] | 6446 | } |
rreardon | d2ddb63 | 2006-12-12 12:13:21 +0000 | [diff] [blame] | 6447 | |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 6448 | return 0; |
| 6449 | } |
| 6450 | |
| 6451 | /** |
Richard Low | 36e447c | 2008-01-20 15:24:55 +0000 | [diff] [blame] | 6452 | * This routine gets representative sample data for an object. |
| 6453 | * This uses the RepresentativeSampleData property of the album, |
| 6454 | * if the device supports it. |
| 6455 | * @param device a pointer to the device which the object is on. |
| 6456 | * @param id unique id of the object to get data for. |
| 6457 | * @param pointer to LIBMTP_filesampledata_t struct to receive data |
| 6458 | * @return 0 on success, any other value means failure. |
| 6459 | * @see LIBMTP_Send_Representative_Sample() |
| 6460 | * @see LIBMTP_Get_Representative_Sample_Format() |
| 6461 | * @see LIBMTP_Create_New_Album() |
| 6462 | */ |
| 6463 | int LIBMTP_Get_Representative_Sample(LIBMTP_mtpdevice_t *device, |
| 6464 | uint32_t const id, |
| 6465 | LIBMTP_filesampledata_t *sampledata) |
| 6466 | { |
| 6467 | uint16_t ret; |
| 6468 | PTPParams *params = (PTPParams *) device->params; |
| 6469 | PTPPropertyValue propval; |
| 6470 | PTPObjectInfo *oi; |
| 6471 | uint32_t i; |
| 6472 | uint16_t *props = NULL; |
| 6473 | uint32_t propcnt = 0; |
| 6474 | int supported = 0; |
| 6475 | |
| 6476 | // get the file format for the object we're going to send representative data for |
| 6477 | oi = NULL; |
| 6478 | for (i = 0; i < params->handles.n; i++) { |
| 6479 | if (params->handles.Handler[i] == id) { |
| 6480 | oi = ¶ms->objectinfo[i]; |
| 6481 | break; |
| 6482 | } |
| 6483 | } |
| 6484 | |
| 6485 | if (oi == NULL) { |
| 6486 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Get_Representative_Sample(): could not get object info."); |
| 6487 | return -1; |
| 6488 | } |
| 6489 | |
| 6490 | // check that we can store representative sample data for this object format |
| 6491 | ret = ptp_mtp_getobjectpropssupported(params, oi->ObjectFormat, &propcnt, &props); |
| 6492 | if (ret != PTP_RC_OK) { |
| 6493 | add_ptp_error_to_errorstack(device, ret, "LIBMTP_Get_Representative_Sample(): could not get object properties."); |
| 6494 | return -1; |
| 6495 | } |
| 6496 | |
| 6497 | for (i = 0; i < propcnt; i++) { |
| 6498 | if (props[i] == PTP_OPC_RepresentativeSampleData) { |
| 6499 | supported = 1; |
| 6500 | break; |
| 6501 | } |
| 6502 | } |
| 6503 | if (!supported) { |
| 6504 | free(props); |
| 6505 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Get_Representative_Sample(): object type doesn't support RepresentativeSampleData."); |
| 6506 | return -1; |
| 6507 | } |
| 6508 | free(props); |
| 6509 | |
| 6510 | // Get the data |
| 6511 | ret = ptp_mtp_getobjectpropvalue(params,id,PTP_OPC_RepresentativeSampleData, |
| 6512 | &propval,PTP_DTC_AUINT8); |
| 6513 | if (ret != PTP_RC_OK) { |
| 6514 | add_ptp_error_to_errorstack(device, ret, "LIBMTP_Get_Representative_Sample(): could not get sample data."); |
| 6515 | return -1; |
| 6516 | } |
| 6517 | |
| 6518 | // Store it |
| 6519 | sampledata->size = propval.a.count; |
| 6520 | sampledata->data = malloc(sizeof(PTPPropertyValue) * propval.a.count); |
| 6521 | for (i = 0; i < propval.a.count; i++) { |
| 6522 | sampledata->data[i] = propval.a.v[i].u8; |
| 6523 | } |
| 6524 | free(propval.a.v); |
| 6525 | |
| 6526 | // Get the other properties |
| 6527 | sampledata->width = get_u32_from_object(device, id, PTP_OPC_RepresentativeSampleWidth, 0); |
| 6528 | sampledata->height = get_u32_from_object(device, id, PTP_OPC_RepresentativeSampleHeight, 0); |
| 6529 | sampledata->duration = get_u32_from_object(device, id, PTP_OPC_RepresentativeSampleDuration, 0); |
| 6530 | sampledata->filetype = map_ptp_type_to_libmtp_type( |
| 6531 | get_u16_from_object(device, id, PTP_OPC_RepresentativeSampleFormat, LIBMTP_FILETYPE_UNKNOWN)); |
| 6532 | |
| 6533 | return 0; |
| 6534 | } |
| 6535 | |
| 6536 | /** |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 6537 | * This routine updates an album based on the metadata |
| 6538 | * supplied. If the <code>tracks</code> field of the metadata |
| 6539 | * contains a track listing, these tracks will be added to the |
| 6540 | * album in place of those already present, i.e. the |
| 6541 | * previous track listing will be deleted. |
| 6542 | * @param device a pointer to the device to create the new album on. |
| 6543 | * @param metadata the metadata for the album to be updated. |
| 6544 | * notice that the field <code>album_id</code> |
| 6545 | * must contain the apropriate album ID. |
| 6546 | * @return 0 on success, any other value means failure. |
| 6547 | * @see LIBMTP_Create_New_Album() |
| 6548 | * @see LIBMTP_Delete_Object() |
| 6549 | */ |
| 6550 | int LIBMTP_Update_Album(LIBMTP_mtpdevice_t *device, |
| 6551 | LIBMTP_album_t const * const metadata) |
| 6552 | { |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 6553 | return update_abstract_list(device, |
| 6554 | metadata->name, |
| 6555 | metadata->artist, |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 6556 | metadata->composer, |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 6557 | metadata->genre, |
| 6558 | metadata->album_id, |
| 6559 | PTP_OFC_MTP_AbstractAudioAlbum, |
| 6560 | metadata->tracks, |
| 6561 | metadata->no_tracks); |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 6562 | } |
Linus Walleij | aa4b075 | 2006-07-26 22:21:04 +0000 | [diff] [blame] | 6563 | |
| 6564 | /** |
| 6565 | * Dummy function needed to interface to upstream |
| 6566 | * ptp.c/ptp.h files. |
| 6567 | */ |
| 6568 | void ptp_nikon_getptpipguid (unsigned char* guid) { |
| 6569 | return; |
| 6570 | } |
tsaarni | a3eb60a | 2007-07-06 17:41:30 +0000 | [diff] [blame] | 6571 | |
| 6572 | /** |
Linus Walleij | 7752b95 | 2007-10-19 20:11:46 +0000 | [diff] [blame] | 6573 | * Add an object to cache. |
| 6574 | * @param device the device which may have a cache to which the object should be added. |
| 6575 | * @param object_id the object to add to the cache. |
tsaarni | a3eb60a | 2007-07-06 17:41:30 +0000 | [diff] [blame] | 6576 | */ |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 6577 | 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] | 6578 | { |
| 6579 | PTPParams *params = (PTPParams *)device->params; |
tsaarni | a3eb60a | 2007-07-06 17:41:30 +0000 | [diff] [blame] | 6580 | uint16_t ret; |
tsaarni | a3eb60a | 2007-07-06 17:41:30 +0000 | [diff] [blame] | 6581 | |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 6582 | ret = ptp_add_object_to_cache(params, object_id); |
| 6583 | if (ret != PTP_RC_OK) { |
| 6584 | 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] | 6585 | } |
| 6586 | } |
| 6587 | |
| 6588 | |
| 6589 | /** |
| 6590 | * Update cache after object has been modified |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 6591 | * @param device the device which may have a cache to which the object should be updated. |
| 6592 | * @param object_id the object to update. |
tsaarni | a3eb60a | 2007-07-06 17:41:30 +0000 | [diff] [blame] | 6593 | */ |
Linus Walleij | 7752b95 | 2007-10-19 20:11:46 +0000 | [diff] [blame] | 6594 | static void update_metadata_cache(LIBMTP_mtpdevice_t *device, uint32_t object_id) |
tsaarni | a3eb60a | 2007-07-06 17:41:30 +0000 | [diff] [blame] | 6595 | { |
Linus Walleij | a3d0eaa | 2007-11-18 22:00:48 +0000 | [diff] [blame] | 6596 | PTPParams *params = (PTPParams *)device->params; |
| 6597 | |
| 6598 | ptp_remove_object_from_cache(params, object_id); |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 6599 | add_object_to_cache(device, object_id); |
tsaarni | a3eb60a | 2007-07-06 17:41:30 +0000 | [diff] [blame] | 6600 | } |