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 | 12bbf31 | 2009-01-26 21:46:51 +0000 | [diff] [blame] | 4 | * Copyright (C) 2005-2009 Linus Walleij <triad@df.lth.se> |
Richard Low | 36e447c | 2008-01-20 15:24:55 +0000 | [diff] [blame] | 5 | * Copyright (C) 2005-2008 Richard A. Low <richard@wentnet.com> |
Linus Walleij | 9521e2b | 2007-07-10 21:50:42 +0000 | [diff] [blame] | 6 | * Copyright (C) 2007 Ted Bullock <tbullock@canada.com> |
| 7 | * Copyright (C) 2007 Tero Saarni <tero.saarni@gmail.com> |
Linus Walleij | de8193f | 2008-01-27 14:55:31 +0000 | [diff] [blame] | 8 | * Copyright (C) 2008 Florent Mertens <flomertens@gmail.com> |
Linus Walleij | 2f45d22 | 2007-02-02 22:47:39 +0000 | [diff] [blame] | 9 | * |
| 10 | * This library is free software; you can redistribute it and/or |
| 11 | * modify it under the terms of the GNU Lesser General Public |
| 12 | * License as published by the Free Software Foundation; either |
| 13 | * version 2 of the License, or (at your option) any later version. |
| 14 | * |
| 15 | * This library is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 18 | * Lesser General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU Lesser General Public |
| 21 | * License along with this library; if not, write to the |
| 22 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 23 | * Boston, MA 02111-1307, USA. |
| 24 | * |
Linus Walleij | 10c5842 | 2006-06-02 08:51:53 +0000 | [diff] [blame] | 25 | * This file provides an interface "glue" to the underlying |
| 26 | * PTP implementation from libgphoto2. It uses some local |
| 27 | * code to convert from/to UTF-8 (stored in unicode.c/.h) |
| 28 | * and some small utility functions, mainly for debugging |
| 29 | * (stored in util.c/.h). |
| 30 | * |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 31 | * The three PTP files (ptp.c, ptp.h and ptp-pack.c) are |
Linus Walleij | 10c5842 | 2006-06-02 08:51:53 +0000 | [diff] [blame] | 32 | * plain copied from the libhphoto2 codebase. |
| 33 | * |
| 34 | * The files libusb-glue.c/.h are just what they say: an |
| 35 | * interface to libusb for the actual, physical USB traffic. |
| 36 | */ |
Linus Walleij | c6d7c98 | 2009-05-10 13:24:36 +0000 | [diff] [blame] | 37 | #include "config.h" |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 38 | #include "libmtp.h" |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 39 | #include "unicode.h" |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 40 | #include "ptp.h" |
Linus Walleij | 15e344f | 2006-03-06 15:15:00 +0000 | [diff] [blame] | 41 | #include "libusb-glue.h" |
Linus Walleij | 3e667ae | 2007-10-29 23:29:39 +0000 | [diff] [blame] | 42 | #include "device-flags.h" |
Linus Walleij | f3c4405 | 2008-08-16 21:14:56 +0000 | [diff] [blame] | 43 | #include "playlist-spl.h" |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 44 | |
Linus Walleij | 2eaaff0 | 2009-01-15 21:30:36 +0000 | [diff] [blame] | 45 | #include <stdlib.h> |
| 46 | #include <unistd.h> |
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 | d1e14e0 | 2008-12-14 01:07:30 +0000 | [diff] [blame] | 58 | /* To enable PTP level debug prints (all ptp_debug(...)), switch on this */ |
| 59 | //#define ENABLE_PTP_DEBUG |
| 60 | |
Linus Walleij | c86afbd | 2006-05-04 19:05:24 +0000 | [diff] [blame] | 61 | /* |
| 62 | * This is a mapping between libmtp internal MTP filetypes and |
| 63 | * the libgphoto2/PTP equivalent defines. We need this because |
| 64 | * otherwise the libmtp.h device has to be dependent on ptp.h |
| 65 | * to be installed too, and we don't want that. |
| 66 | */ |
Linus Walleij | cf42f45 | 2006-11-28 08:32:49 +0000 | [diff] [blame] | 67 | //typedef struct filemap_struct filemap_t; |
| 68 | typedef struct filemap_struct { |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 69 | char *description; /**< Text description for the file type */ |
| 70 | LIBMTP_filetype_t id; /**< LIBMTP internal type for the file type */ |
| 71 | uint16_t ptp_id; /**< PTP ID for the filetype */ |
Linus Walleij | cf42f45 | 2006-11-28 08:32:49 +0000 | [diff] [blame] | 72 | struct filemap_struct *next; |
| 73 | } filemap_t; |
Linus Walleij | c86afbd | 2006-05-04 19:05:24 +0000 | [diff] [blame] | 74 | |
Richard Low | 6f07084 | 2009-05-03 10:26:16 +0000 | [diff] [blame] | 75 | /* |
| 76 | * This is a mapping between libmtp internal MTP properties and |
| 77 | * the libgphoto2/PTP equivalent defines. We need this because |
| 78 | * otherwise the libmtp.h device has to be dependent on ptp.h |
| 79 | * to be installed too, and we don't want that. |
| 80 | */ |
| 81 | typedef struct propertymap_struct { |
| 82 | char *description; /**< Text description for the property */ |
| 83 | LIBMTP_property_t id; /**< LIBMTP internal type for the property */ |
| 84 | uint16_t ptp_id; /**< PTP ID for the property */ |
| 85 | struct propertymap_struct *next; |
| 86 | } propertymap_t; |
| 87 | |
Linus Walleij | f67bca9 | 2006-05-29 09:33:39 +0000 | [diff] [blame] | 88 | // Global variables |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 89 | // This holds the global filetype mapping table |
Linus Walleij | cf42f45 | 2006-11-28 08:32:49 +0000 | [diff] [blame] | 90 | static filemap_t *filemap = NULL; |
Richard Low | 6f07084 | 2009-05-03 10:26:16 +0000 | [diff] [blame] | 91 | // This holds the global property mapping table |
| 92 | static propertymap_t *propertymap = NULL; |
Linus Walleij | f67bca9 | 2006-05-29 09:33:39 +0000 | [diff] [blame] | 93 | |
Linus Walleij | cf42f45 | 2006-11-28 08:32:49 +0000 | [diff] [blame] | 94 | /* |
| 95 | * Forward declarations of local (static) functions. |
| 96 | */ |
| 97 | static int register_filetype(char const * const description, LIBMTP_filetype_t const id, |
| 98 | uint16_t const ptp_id); |
Linus Walleij | 7beba57 | 2006-11-29 08:56:12 +0000 | [diff] [blame] | 99 | static void init_filemap(); |
Richard Low | 6f07084 | 2009-05-03 10:26:16 +0000 | [diff] [blame] | 100 | static int register_property(char const * const description, LIBMTP_property_t const id, |
| 101 | uint16_t const ptp_id); |
| 102 | static void init_propertymap(); |
Linus Walleij | 2715c44 | 2007-01-20 22:35:29 +0000 | [diff] [blame] | 103 | static void add_error_to_errorstack(LIBMTP_mtpdevice_t *device, |
| 104 | LIBMTP_error_number_t errornumber, |
| 105 | char const * const error_text); |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 106 | static void add_ptp_error_to_errorstack(LIBMTP_mtpdevice_t *device, |
| 107 | uint16_t ptp_error, |
| 108 | char const * const error_text); |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 109 | static void flush_handles(LIBMTP_mtpdevice_t *device); |
Linus Walleij | 6bf68b4 | 2007-09-03 22:50:02 +0000 | [diff] [blame] | 110 | static void get_handles_recursively(LIBMTP_mtpdevice_t *device, |
| 111 | PTPParams *params, |
Linus Walleij | 6bf68b4 | 2007-09-03 22:50:02 +0000 | [diff] [blame] | 112 | uint32_t storageid, |
| 113 | uint32_t parent); |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 114 | static void free_storage_list(LIBMTP_mtpdevice_t *device); |
| 115 | static int sort_storage_by(LIBMTP_mtpdevice_t *device, int const sortby); |
Linus Walleij | d71d0b3 | 2008-09-22 08:21:03 +0000 | [diff] [blame] | 116 | static uint32_t get_writeable_storageid(LIBMTP_mtpdevice_t *device, uint64_t fitsize); |
Linus Walleij | 6bf68b4 | 2007-09-03 22:50:02 +0000 | [diff] [blame] | 117 | static int get_storage_freespace(LIBMTP_mtpdevice_t *device, |
| 118 | LIBMTP_devicestorage_t *storage, |
| 119 | uint64_t *freespace); |
| 120 | static int check_if_file_fits(LIBMTP_mtpdevice_t *device, |
| 121 | LIBMTP_devicestorage_t *storage, |
| 122 | uint64_t const filesize); |
Linus Walleij | f67bca9 | 2006-05-29 09:33:39 +0000 | [diff] [blame] | 123 | static uint16_t map_libmtp_type_to_ptp_type(LIBMTP_filetype_t intype); |
| 124 | static LIBMTP_filetype_t map_ptp_type_to_libmtp_type(uint16_t intype); |
Richard Low | 6f07084 | 2009-05-03 10:26:16 +0000 | [diff] [blame] | 125 | static uint16_t map_libmtp_property_to_ptp_property(LIBMTP_property_t inproperty); |
| 126 | static LIBMTP_property_t map_ptp_property_to_libmtp_property(uint16_t intype); |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 127 | static int get_device_unicode_property(LIBMTP_mtpdevice_t *device, |
Linus Walleij | cf223e6 | 2006-06-19 09:31:53 +0000 | [diff] [blame] | 128 | char **unicstring, uint16_t property); |
Linus Walleij | 2955956 | 2007-08-22 21:38:04 +0000 | [diff] [blame] | 129 | static uint16_t adjust_u16(uint16_t val, PTPObjectPropDesc *opd); |
| 130 | static uint32_t adjust_u32(uint32_t val, PTPObjectPropDesc *opd); |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 131 | static char *get_iso8601_stamp(void); |
Linus Walleij | 9901e22 | 2006-11-30 12:28:19 +0000 | [diff] [blame] | 132 | static char *get_string_from_object(LIBMTP_mtpdevice_t *device, uint32_t const object_id, |
| 133 | uint16_t const attribute_id); |
Richard Low | c3f5fc3 | 2007-07-29 09:40:50 +0000 | [diff] [blame] | 134 | static uint64_t get_u64_from_object(LIBMTP_mtpdevice_t *device,uint32_t const object_id, |
| 135 | uint16_t const attribute_id, uint64_t const value_default); |
Linus Walleij | 9901e22 | 2006-11-30 12:28:19 +0000 | [diff] [blame] | 136 | static uint32_t get_u32_from_object(LIBMTP_mtpdevice_t *device,uint32_t const object_id, |
| 137 | uint16_t const attribute_id, uint32_t const value_default); |
| 138 | static uint16_t get_u16_from_object(LIBMTP_mtpdevice_t *device, uint32_t const object_id, |
| 139 | uint16_t const attribute_id, uint16_t const value_default); |
| 140 | static uint8_t get_u8_from_object(LIBMTP_mtpdevice_t *device, uint32_t const object_id, |
| 141 | uint16_t const attribute_id, uint8_t const value_default); |
| 142 | static int set_object_string(LIBMTP_mtpdevice_t *device, uint32_t const object_id, |
| 143 | uint16_t const attribute_id, char const * const string); |
| 144 | static int set_object_u32(LIBMTP_mtpdevice_t *device, uint32_t const object_id, |
| 145 | uint16_t const attribute_id, uint32_t const value); |
| 146 | static int set_object_u16(LIBMTP_mtpdevice_t *device, uint32_t const object_id, |
| 147 | uint16_t const attribute_id, uint16_t const value); |
| 148 | static int set_object_u8(LIBMTP_mtpdevice_t *device, uint32_t const object_id, |
| 149 | uint16_t const attribute_id, uint8_t const value); |
Linus Walleij | 8ab5426 | 2006-06-21 07:12:28 +0000 | [diff] [blame] | 150 | static void get_track_metadata(LIBMTP_mtpdevice_t *device, uint16_t objectformat, |
| 151 | LIBMTP_track_t *track); |
Linus Walleij | a89a794 | 2008-12-14 23:19:34 +0000 | [diff] [blame] | 152 | static LIBMTP_folder_t *get_subfolders_for_folder(LIBMTP_folder_t *list, uint32_t parent); |
Linus Walleij | 7beba57 | 2006-11-29 08:56:12 +0000 | [diff] [blame] | 153 | static int create_new_abstract_list(LIBMTP_mtpdevice_t *device, |
| 154 | char const * const name, |
Linus Walleij | adce4a5 | 2007-04-23 07:28:11 +0000 | [diff] [blame] | 155 | char const * const artist, |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 156 | char const * const composer, |
Linus Walleij | adce4a5 | 2007-04-23 07:28:11 +0000 | [diff] [blame] | 157 | char const * const genre, |
Linus Walleij | 7beba57 | 2006-11-29 08:56:12 +0000 | [diff] [blame] | 158 | uint32_t const parenthandle, |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 159 | uint32_t const storageid, |
Linus Walleij | 7beba57 | 2006-11-29 08:56:12 +0000 | [diff] [blame] | 160 | uint16_t const objectformat, |
| 161 | char const * const suffix, |
| 162 | uint32_t * const newid, |
| 163 | uint32_t const * const tracks, |
| 164 | uint32_t const no_tracks); |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 165 | static int update_abstract_list(LIBMTP_mtpdevice_t *device, |
| 166 | char const * const name, |
| 167 | char const * const artist, |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 168 | char const * const composer, |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 169 | char const * const genre, |
| 170 | uint32_t const objecthandle, |
| 171 | uint16_t const objectformat, |
| 172 | uint32_t const * const tracks, |
| 173 | uint32_t const no_tracks); |
Richard Low | d3b1702 | 2009-04-11 12:37:39 +0000 | [diff] [blame] | 174 | static int send_file_object_info(LIBMTP_mtpdevice_t *device, LIBMTP_file_t *filedata); |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 175 | 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] | 176 | static void update_metadata_cache(LIBMTP_mtpdevice_t *device, uint32_t object_id); |
Linus Walleij | 8d8c435 | 2008-09-23 23:04:16 +0000 | [diff] [blame] | 177 | static int set_object_filename(LIBMTP_mtpdevice_t *device, |
| 178 | uint32_t object_id, |
| 179 | uint16_t ptp_type, |
| 180 | const char **newname); |
Richard Low | 5b4023c | 2009-04-16 19:14:38 +0000 | [diff] [blame] | 181 | |
| 182 | /** |
| 183 | * These are to wrap the get/put handlers to convert from the MTP types to PTP types |
| 184 | * in a reliable way |
| 185 | */ |
| 186 | typedef struct _MTPDataHandler { |
| 187 | MTPDataGetFunc getfunc; |
| 188 | MTPDataPutFunc putfunc; |
Linus Walleij | d866d24 | 2009-08-23 21:50:39 +0000 | [diff] [blame] | 189 | void *priv; |
Richard Low | 5b4023c | 2009-04-16 19:14:38 +0000 | [diff] [blame] | 190 | } MTPDataHandler; |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 191 | |
Linus Walleij | d866d24 | 2009-08-23 21:50:39 +0000 | [diff] [blame] | 192 | static uint16_t get_func_wrapper(PTPParams* params, void* priv, unsigned long wantlen, unsigned char *data, unsigned long *gotlen); |
| 193 | static uint16_t put_func_wrapper(PTPParams* params, void* priv, unsigned long sendlen, unsigned char *data, unsigned long *putlen); |
Richard Low | 5b4023c | 2009-04-16 19:14:38 +0000 | [diff] [blame] | 194 | |
Linus Walleij | cf42f45 | 2006-11-28 08:32:49 +0000 | [diff] [blame] | 195 | /** |
Linus Walleij | 01fc9c8 | 2009-03-10 23:52:09 +0000 | [diff] [blame] | 196 | * Checks if a filename ends with ".ogg". Used in various |
| 197 | * situations when the device has no idea that it support |
| 198 | * OGG but still does. |
Linus Walleij | d98e897 | 2009-03-08 22:57:17 +0000 | [diff] [blame] | 199 | * |
| 200 | * @param name string to be checked. |
| 201 | * @return 0 if this does not end with ogg, any other |
| 202 | * value means it does. |
| 203 | */ |
| 204 | static int has_ogg_extension(char *name) { |
| 205 | char *ptype; |
| 206 | |
| 207 | if (name == NULL) |
| 208 | return 0; |
| 209 | ptype = strrchr(name,'.'); |
| 210 | if (ptype == NULL) |
| 211 | return 0; |
| 212 | if (!strcasecmp (ptype, ".ogg")) |
| 213 | return 1; |
| 214 | return 0; |
| 215 | } |
| 216 | |
Linus Walleij | 89bb1cd | 2009-07-24 21:03:36 +0000 | [diff] [blame] | 217 | /** |
| 218 | * Checks if a filename ends with ".flac". Used in various |
| 219 | * situations when the device has no idea that it support |
| 220 | * FLAC but still does. |
| 221 | * |
| 222 | * @param name string to be checked. |
| 223 | * @return 0 if this does not end with flac, any other |
| 224 | * value means it does. |
| 225 | */ |
| 226 | static int has_flac_extension(char *name) { |
| 227 | char *ptype; |
| 228 | |
| 229 | if (name == NULL) |
| 230 | return 0; |
| 231 | ptype = strrchr(name,'.'); |
| 232 | if (ptype == NULL) |
| 233 | return 0; |
| 234 | if (!strcasecmp (ptype, ".flac")) |
| 235 | return 1; |
| 236 | return 0; |
| 237 | } |
| 238 | |
| 239 | |
Linus Walleij | d98e897 | 2009-03-08 22:57:17 +0000 | [diff] [blame] | 240 | |
| 241 | /** |
Linus Walleij | cf42f45 | 2006-11-28 08:32:49 +0000 | [diff] [blame] | 242 | * Create a new file mapping entry |
| 243 | * @return a newly allocated filemapping entry. |
| 244 | */ |
| 245 | static filemap_t *new_filemap_entry() |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 246 | { |
Linus Walleij | cf42f45 | 2006-11-28 08:32:49 +0000 | [diff] [blame] | 247 | filemap_t *filemap; |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 248 | |
Linus Walleij | cf42f45 | 2006-11-28 08:32:49 +0000 | [diff] [blame] | 249 | filemap = (filemap_t *)malloc(sizeof(filemap_t)); |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 250 | |
Linus Walleij | f67bca9 | 2006-05-29 09:33:39 +0000 | [diff] [blame] | 251 | if( filemap != NULL ) { |
| 252 | filemap->description = NULL; |
| 253 | filemap->id = LIBMTP_FILETYPE_UNKNOWN; |
| 254 | filemap->ptp_id = PTP_OFC_Undefined; |
Linus Walleij | f67bca9 | 2006-05-29 09:33:39 +0000 | [diff] [blame] | 255 | filemap->next = NULL; |
| 256 | } |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 257 | |
Linus Walleij | f67bca9 | 2006-05-29 09:33:39 +0000 | [diff] [blame] | 258 | return filemap; |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 259 | } |
| 260 | |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 261 | /** |
| 262 | * Register an MTP or PTP filetype for data retrieval |
| 263 | * |
| 264 | * @param description Text description of filetype |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 265 | * @param id libmtp internal filetype id |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 266 | * @param ptp_id PTP filetype id |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 267 | * @return 0 for success any other value means error. |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 268 | */ |
Linus Walleij | cf42f45 | 2006-11-28 08:32:49 +0000 | [diff] [blame] | 269 | static int register_filetype(char const * const description, LIBMTP_filetype_t const id, |
| 270 | uint16_t const ptp_id) |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 271 | { |
Linus Walleij | cf42f45 | 2006-11-28 08:32:49 +0000 | [diff] [blame] | 272 | filemap_t *new = NULL, *current; |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 273 | |
| 274 | // Has this LIBMTP filetype been registered before ? |
| 275 | current = filemap; |
| 276 | while (current != NULL) { |
Linus Walleij | f67bca9 | 2006-05-29 09:33:39 +0000 | [diff] [blame] | 277 | if(current->id == id) { |
| 278 | break; |
| 279 | } |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 280 | current = current->next; |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 281 | } |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 282 | |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 283 | // Create the entry |
| 284 | if(current == NULL) { |
Linus Walleij | f67bca9 | 2006-05-29 09:33:39 +0000 | [diff] [blame] | 285 | new = new_filemap_entry(); |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 286 | if(new == NULL) { |
| 287 | return 1; |
| 288 | } |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 289 | |
Linus Walleij | f67bca9 | 2006-05-29 09:33:39 +0000 | [diff] [blame] | 290 | new->id = id; |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 291 | if(description != NULL) { |
| 292 | new->description = strdup(description); |
| 293 | } |
Linus Walleij | f67bca9 | 2006-05-29 09:33:39 +0000 | [diff] [blame] | 294 | new->ptp_id = ptp_id; |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 295 | |
Linus Walleij | f67bca9 | 2006-05-29 09:33:39 +0000 | [diff] [blame] | 296 | // Add the entry to the list |
| 297 | if(filemap == NULL) { |
| 298 | filemap = new; |
| 299 | } else { |
| 300 | current = filemap; |
| 301 | while (current->next != NULL ) current=current->next; |
| 302 | current->next = new; |
| 303 | } |
| 304 | // Update the existing entry |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 305 | } else { |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 306 | if (current->description != NULL) { |
| 307 | free(current->description); |
| 308 | } |
Linus Walleij | f67bca9 | 2006-05-29 09:33:39 +0000 | [diff] [blame] | 309 | current->description = NULL; |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 310 | if(description != NULL) { |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 311 | current->description = strdup(description); |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 312 | } |
Linus Walleij | f67bca9 | 2006-05-29 09:33:39 +0000 | [diff] [blame] | 313 | current->ptp_id = ptp_id; |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 314 | } |
| 315 | |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 316 | return 0; |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 317 | } |
| 318 | |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 319 | static void init_filemap() |
| 320 | { |
Linus Walleij | 5fb4713 | 2006-12-30 15:35:48 +0000 | [diff] [blame] | 321 | register_filetype("MediaCard", LIBMTP_FILETYPE_MEDIACARD, PTP_OFC_MTP_MediaCard); |
Linus Walleij | cf42f45 | 2006-11-28 08:32:49 +0000 | [diff] [blame] | 322 | register_filetype("RIFF WAVE file", LIBMTP_FILETYPE_WAV, PTP_OFC_WAV); |
Linus Walleij | 5fb4713 | 2006-12-30 15:35:48 +0000 | [diff] [blame] | 323 | register_filetype("ISO MPEG-1 Audio Layer 3", LIBMTP_FILETYPE_MP3, PTP_OFC_MP3); |
| 324 | 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] | 325 | register_filetype("Microsoft Windows Media Audio", LIBMTP_FILETYPE_WMA, PTP_OFC_MTP_WMA); |
| 326 | register_filetype("Ogg container format", LIBMTP_FILETYPE_OGG, PTP_OFC_MTP_OGG); |
Linus Walleij | 5fb4713 | 2006-12-30 15:35:48 +0000 | [diff] [blame] | 327 | register_filetype("Free Lossless Audio Codec (FLAC)", LIBMTP_FILETYPE_FLAC, PTP_OFC_MTP_FLAC); |
| 328 | register_filetype("Advanced Audio Coding (AAC)/MPEG-2 Part 7/MPEG-4 Part 3", LIBMTP_FILETYPE_AAC, PTP_OFC_MTP_AAC); |
Richard Low | d3b1702 | 2009-04-11 12:37:39 +0000 | [diff] [blame] | 329 | register_filetype("MPEG-4 Part 14 Container Format (Audio Emphasis)", LIBMTP_FILETYPE_M4A, PTP_OFC_MTP_M4A); |
| 330 | register_filetype("MPEG-4 Part 14 Container Format (Audio+Video Emphasis)", LIBMTP_FILETYPE_MP4, PTP_OFC_MTP_MP4); |
Linus Walleij | cf42f45 | 2006-11-28 08:32:49 +0000 | [diff] [blame] | 331 | 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] | 332 | register_filetype("Undefined audio file", LIBMTP_FILETYPE_UNDEF_AUDIO, PTP_OFC_MTP_UndefinedAudio); |
| 333 | register_filetype("Microsoft Windows Media Video", LIBMTP_FILETYPE_WMV, PTP_OFC_MTP_WMV); |
| 334 | register_filetype("Audio Video Interleave", LIBMTP_FILETYPE_AVI, PTP_OFC_AVI); |
| 335 | register_filetype("MPEG video stream", LIBMTP_FILETYPE_MPEG, PTP_OFC_MPEG); |
| 336 | register_filetype("Microsoft Advanced Systems Format", LIBMTP_FILETYPE_ASF, PTP_OFC_ASF); |
| 337 | register_filetype("Apple Quicktime container format", LIBMTP_FILETYPE_QT, PTP_OFC_QT); |
| 338 | register_filetype("Undefined video file", LIBMTP_FILETYPE_UNDEF_VIDEO, PTP_OFC_MTP_UndefinedVideo); |
| 339 | register_filetype("JPEG file", LIBMTP_FILETYPE_JPEG, PTP_OFC_EXIF_JPEG); |
Linus Walleij | 5fb4713 | 2006-12-30 15:35:48 +0000 | [diff] [blame] | 340 | register_filetype("JP2 file", LIBMTP_FILETYPE_JP2, PTP_OFC_JP2); |
| 341 | register_filetype("JPX file", LIBMTP_FILETYPE_JPX, PTP_OFC_JPX); |
Linus Walleij | cf42f45 | 2006-11-28 08:32:49 +0000 | [diff] [blame] | 342 | register_filetype("JFIF file", LIBMTP_FILETYPE_JFIF, PTP_OFC_JFIF); |
| 343 | register_filetype("TIFF bitmap file", LIBMTP_FILETYPE_TIFF, PTP_OFC_TIFF); |
| 344 | register_filetype("BMP bitmap file", LIBMTP_FILETYPE_BMP, PTP_OFC_BMP); |
| 345 | register_filetype("GIF bitmap file", LIBMTP_FILETYPE_GIF, PTP_OFC_GIF); |
| 346 | register_filetype("PICT bitmap file", LIBMTP_FILETYPE_PICT, PTP_OFC_PICT); |
| 347 | register_filetype("Portable Network Graphics", LIBMTP_FILETYPE_PNG, PTP_OFC_PNG); |
| 348 | register_filetype("Microsoft Windows Image Format", LIBMTP_FILETYPE_WINDOWSIMAGEFORMAT, PTP_OFC_MTP_WindowsImageFormat); |
| 349 | register_filetype("VCalendar version 1", LIBMTP_FILETYPE_VCALENDAR1, PTP_OFC_MTP_vCalendar1); |
| 350 | register_filetype("VCalendar version 2", LIBMTP_FILETYPE_VCALENDAR2, PTP_OFC_MTP_vCalendar2); |
| 351 | register_filetype("VCard version 2", LIBMTP_FILETYPE_VCARD2, PTP_OFC_MTP_vCard2); |
| 352 | register_filetype("VCard version 3", LIBMTP_FILETYPE_VCARD3, PTP_OFC_MTP_vCard3); |
| 353 | register_filetype("Undefined Windows executable file", LIBMTP_FILETYPE_WINEXEC, PTP_OFC_MTP_UndefinedWindowsExecutable); |
| 354 | register_filetype("Text file", LIBMTP_FILETYPE_TEXT, PTP_OFC_Text); |
| 355 | register_filetype("HTML file", LIBMTP_FILETYPE_HTML, PTP_OFC_HTML); |
Linus Walleij | 5fb4713 | 2006-12-30 15:35:48 +0000 | [diff] [blame] | 356 | register_filetype("XML file", LIBMTP_FILETYPE_XML, PTP_OFC_MTP_XMLDocument); |
| 357 | register_filetype("DOC file", LIBMTP_FILETYPE_DOC, PTP_OFC_MTP_MSWordDocument); |
| 358 | register_filetype("XLS file", LIBMTP_FILETYPE_XLS, PTP_OFC_MTP_MSExcelSpreadsheetXLS); |
| 359 | register_filetype("PPT file", LIBMTP_FILETYPE_PPT, PTP_OFC_MTP_MSPowerpointPresentationPPT); |
| 360 | register_filetype("MHT file", LIBMTP_FILETYPE_MHT, PTP_OFC_MTP_MHTCompiledHTMLDocument); |
Linus Walleij | a05b980 | 2006-12-08 08:50:30 +0000 | [diff] [blame] | 361 | register_filetype("Firmware file", LIBMTP_FILETYPE_FIRMWARE, PTP_OFC_MTP_Firmware); |
Richard Low | d3b1702 | 2009-04-11 12:37:39 +0000 | [diff] [blame] | 362 | register_filetype("Abstract Album file", LIBMTP_FILETYPE_ALBUM, PTP_OFC_MTP_AbstractAudioAlbum); |
| 363 | register_filetype("Abstract Playlist file", LIBMTP_FILETYPE_PLAYLIST, PTP_OFC_MTP_AbstractAudioVideoPlaylist); |
Linus Walleij | cf42f45 | 2006-11-28 08:32:49 +0000 | [diff] [blame] | 364 | register_filetype("Undefined filetype", LIBMTP_FILETYPE_UNKNOWN, PTP_OFC_Undefined); |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 365 | } |
Linus Walleij | 16c51f0 | 2006-05-04 13:20:22 +0000 | [diff] [blame] | 366 | |
Linus Walleij | 16c51f0 | 2006-05-04 13:20:22 +0000 | [diff] [blame] | 367 | /** |
| 368 | * Returns the PTP filetype that maps to a certain libmtp internal file type. |
| 369 | * @param intype the MTP library interface type |
| 370 | * @return the PTP (libgphoto2) interface type |
| 371 | */ |
| 372 | static uint16_t map_libmtp_type_to_ptp_type(LIBMTP_filetype_t intype) |
| 373 | { |
Linus Walleij | cf42f45 | 2006-11-28 08:32:49 +0000 | [diff] [blame] | 374 | filemap_t *current; |
Linus Walleij | 16c51f0 | 2006-05-04 13:20:22 +0000 | [diff] [blame] | 375 | |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 376 | current = filemap; |
| 377 | |
| 378 | while (current != NULL) { |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 379 | if(current->id == intype) { |
| 380 | return current->ptp_id; |
| 381 | } |
| 382 | current = current->next; |
Linus Walleij | 16c51f0 | 2006-05-04 13:20:22 +0000 | [diff] [blame] | 383 | } |
Linus Walleij | 1fd2d27 | 2006-05-08 09:22:01 +0000 | [diff] [blame] | 384 | // printf("map_libmtp_type_to_ptp_type: unknown filetype.\n"); |
Linus Walleij | 16c51f0 | 2006-05-04 13:20:22 +0000 | [diff] [blame] | 385 | return PTP_OFC_Undefined; |
| 386 | } |
| 387 | |
| 388 | |
| 389 | /** |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 390 | * Returns the MTP internal interface type that maps to a certain ptp |
| 391 | * interface type. |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 392 | * @param intype the PTP (libgphoto2) interface type |
Linus Walleij | 8ab5426 | 2006-06-21 07:12:28 +0000 | [diff] [blame] | 393 | * @return the MTP library interface type |
Linus Walleij | 16c51f0 | 2006-05-04 13:20:22 +0000 | [diff] [blame] | 394 | */ |
| 395 | static LIBMTP_filetype_t map_ptp_type_to_libmtp_type(uint16_t intype) |
| 396 | { |
Linus Walleij | cf42f45 | 2006-11-28 08:32:49 +0000 | [diff] [blame] | 397 | filemap_t *current; |
Linus Walleij | 16c51f0 | 2006-05-04 13:20:22 +0000 | [diff] [blame] | 398 | |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 399 | current = filemap; |
| 400 | |
| 401 | while (current != NULL) { |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 402 | if(current->ptp_id == intype) { |
| 403 | return current->id; |
| 404 | } |
| 405 | current = current->next; |
Linus Walleij | 16c51f0 | 2006-05-04 13:20:22 +0000 | [diff] [blame] | 406 | } |
Linus Walleij | 1fd2d27 | 2006-05-08 09:22:01 +0000 | [diff] [blame] | 407 | // printf("map_ptp_type_to_libmtp_type: unknown filetype.\n"); |
Linus Walleij | 16c51f0 | 2006-05-04 13:20:22 +0000 | [diff] [blame] | 408 | return LIBMTP_FILETYPE_UNKNOWN; |
| 409 | } |
| 410 | |
Richard Low | 6f07084 | 2009-05-03 10:26:16 +0000 | [diff] [blame] | 411 | /** |
| 412 | * Create a new property mapping entry |
| 413 | * @return a newly allocated propertymapping entry. |
| 414 | */ |
| 415 | static propertymap_t *new_propertymap_entry() |
| 416 | { |
| 417 | propertymap_t *propertymap; |
| 418 | |
| 419 | propertymap = (propertymap_t *)malloc(sizeof(propertymap_t)); |
| 420 | |
| 421 | if( propertymap != NULL ) { |
| 422 | propertymap->description = NULL; |
| 423 | propertymap->id = LIBMTP_PROPERTY_UNKNOWN; |
| 424 | propertymap->ptp_id = 0; |
| 425 | propertymap->next = NULL; |
| 426 | } |
| 427 | |
| 428 | return propertymap; |
| 429 | } |
| 430 | |
| 431 | /** |
| 432 | * Register an MTP or PTP property for data retrieval |
| 433 | * |
| 434 | * @param description Text description of property |
| 435 | * @param id libmtp internal property id |
| 436 | * @param ptp_id PTP property id |
| 437 | * @return 0 for success any other value means error. |
| 438 | */ |
| 439 | static int register_property(char const * const description, LIBMTP_property_t const id, |
| 440 | uint16_t const ptp_id) |
| 441 | { |
| 442 | propertymap_t *new = NULL, *current; |
| 443 | |
| 444 | // Has this LIBMTP propety been registered before ? |
| 445 | current = propertymap; |
| 446 | while (current != NULL) { |
| 447 | if(current->id == id) { |
| 448 | break; |
| 449 | } |
| 450 | current = current->next; |
| 451 | } |
| 452 | |
| 453 | // Create the entry |
| 454 | if(current == NULL) { |
| 455 | new = new_propertymap_entry(); |
| 456 | if(new == NULL) { |
| 457 | return 1; |
| 458 | } |
| 459 | |
| 460 | new->id = id; |
| 461 | if(description != NULL) { |
| 462 | new->description = strdup(description); |
| 463 | } |
| 464 | new->ptp_id = ptp_id; |
| 465 | |
| 466 | // Add the entry to the list |
| 467 | if(propertymap == NULL) { |
| 468 | propertymap = new; |
| 469 | } else { |
| 470 | current = propertymap; |
| 471 | while (current->next != NULL ) current=current->next; |
| 472 | current->next = new; |
| 473 | } |
| 474 | // Update the existing entry |
| 475 | } else { |
| 476 | if (current->description != NULL) { |
| 477 | free(current->description); |
| 478 | } |
| 479 | current->description = NULL; |
| 480 | if(description != NULL) { |
| 481 | current->description = strdup(description); |
| 482 | } |
| 483 | current->ptp_id = ptp_id; |
| 484 | } |
| 485 | |
| 486 | return 0; |
| 487 | } |
| 488 | |
| 489 | static void init_propertymap() |
| 490 | { |
| 491 | register_property("Storage ID", LIBMTP_PROPERTY_StorageID, PTP_OPC_StorageID); |
| 492 | register_property("Object Format", LIBMTP_PROPERTY_ObjectFormat, PTP_OPC_ObjectFormat); |
| 493 | register_property("Protection Status", LIBMTP_PROPERTY_ProtectionStatus, PTP_OPC_ProtectionStatus); |
| 494 | register_property("Object Size", LIBMTP_PROPERTY_ObjectSize, PTP_OPC_ObjectSize); |
| 495 | register_property("Association Type", LIBMTP_PROPERTY_AssociationType, PTP_OPC_AssociationType); |
| 496 | register_property("Association Desc", LIBMTP_PROPERTY_AssociationDesc, PTP_OPC_AssociationDesc); |
| 497 | register_property("Object File Name", LIBMTP_PROPERTY_ObjectFileName, PTP_OPC_ObjectFileName); |
| 498 | register_property("Date Created", LIBMTP_PROPERTY_DateCreated, PTP_OPC_DateCreated); |
| 499 | register_property("Date Modified", LIBMTP_PROPERTY_DateModified, PTP_OPC_DateModified); |
| 500 | register_property("Keywords", LIBMTP_PROPERTY_Keywords, PTP_OPC_Keywords); |
| 501 | register_property("Parent Object", LIBMTP_PROPERTY_ParentObject, PTP_OPC_ParentObject); |
| 502 | register_property("Allowed Folder Contents", LIBMTP_PROPERTY_AllowedFolderContents, PTP_OPC_AllowedFolderContents); |
| 503 | register_property("Hidden", LIBMTP_PROPERTY_Hidden, PTP_OPC_Hidden); |
| 504 | register_property("System Object", LIBMTP_PROPERTY_SystemObject, PTP_OPC_SystemObject); |
| 505 | register_property("Persistant Unique Object Identifier", LIBMTP_PROPERTY_PersistantUniqueObjectIdentifier, PTP_OPC_PersistantUniqueObjectIdentifier); |
| 506 | register_property("Sync ID", LIBMTP_PROPERTY_SyncID, PTP_OPC_SyncID); |
| 507 | register_property("Property Bag", LIBMTP_PROPERTY_PropertyBag, PTP_OPC_PropertyBag); |
| 508 | register_property("Name", LIBMTP_PROPERTY_Name, PTP_OPC_Name); |
| 509 | register_property("Created By", LIBMTP_PROPERTY_CreatedBy, PTP_OPC_CreatedBy); |
| 510 | register_property("Artist", LIBMTP_PROPERTY_Artist, PTP_OPC_Artist); |
| 511 | register_property("Date Authored", LIBMTP_PROPERTY_DateAuthored, PTP_OPC_DateAuthored); |
| 512 | register_property("Description", LIBMTP_PROPERTY_Description, PTP_OPC_Description); |
| 513 | register_property("URL Reference", LIBMTP_PROPERTY_URLReference, PTP_OPC_URLReference); |
| 514 | register_property("Language Locale", LIBMTP_PROPERTY_LanguageLocale, PTP_OPC_LanguageLocale); |
| 515 | register_property("Copyright Information", LIBMTP_PROPERTY_CopyrightInformation, PTP_OPC_CopyrightInformation); |
| 516 | register_property("Source", LIBMTP_PROPERTY_Source, PTP_OPC_Source); |
| 517 | register_property("Origin Location", LIBMTP_PROPERTY_OriginLocation, PTP_OPC_OriginLocation); |
| 518 | register_property("Date Added", LIBMTP_PROPERTY_DateAdded, PTP_OPC_DateAdded); |
| 519 | register_property("Non Consumable", LIBMTP_PROPERTY_NonConsumable, PTP_OPC_NonConsumable); |
| 520 | register_property("Corrupt Or Unplayable", LIBMTP_PROPERTY_CorruptOrUnplayable, PTP_OPC_CorruptOrUnplayable); |
| 521 | register_property("Producer Serial Number", LIBMTP_PROPERTY_ProducerSerialNumber, PTP_OPC_ProducerSerialNumber); |
| 522 | register_property("Representative Sample Format", LIBMTP_PROPERTY_RepresentativeSampleFormat, PTP_OPC_RepresentativeSampleFormat); |
| 523 | register_property("Representative Sample Sise", LIBMTP_PROPERTY_RepresentativeSampleSize, PTP_OPC_RepresentativeSampleSize); |
| 524 | register_property("Representative Sample Height", LIBMTP_PROPERTY_RepresentativeSampleHeight, PTP_OPC_RepresentativeSampleHeight); |
| 525 | register_property("Representative Sample Width", LIBMTP_PROPERTY_RepresentativeSampleWidth, PTP_OPC_RepresentativeSampleWidth); |
| 526 | register_property("Representative Sample Duration", LIBMTP_PROPERTY_RepresentativeSampleDuration, PTP_OPC_RepresentativeSampleDuration); |
| 527 | register_property("Representative Sample Data", LIBMTP_PROPERTY_RepresentativeSampleData, PTP_OPC_RepresentativeSampleData); |
| 528 | register_property("Width", LIBMTP_PROPERTY_Width, PTP_OPC_Width); |
| 529 | register_property("Height", LIBMTP_PROPERTY_Height, PTP_OPC_Height); |
| 530 | register_property("Duration", LIBMTP_PROPERTY_Duration, PTP_OPC_Duration); |
| 531 | register_property("Rating", LIBMTP_PROPERTY_Rating, PTP_OPC_Rating); |
| 532 | register_property("Track", LIBMTP_PROPERTY_Track, PTP_OPC_Track); |
| 533 | register_property("Genre", LIBMTP_PROPERTY_Genre, PTP_OPC_Genre); |
| 534 | register_property("Credits", LIBMTP_PROPERTY_Credits, PTP_OPC_Credits); |
| 535 | register_property("Lyrics", LIBMTP_PROPERTY_Lyrics, PTP_OPC_Lyrics); |
| 536 | register_property("Subscription Content ID", LIBMTP_PROPERTY_SubscriptionContentID, PTP_OPC_SubscriptionContentID); |
| 537 | register_property("Produced By", LIBMTP_PROPERTY_ProducedBy, PTP_OPC_ProducedBy); |
| 538 | register_property("Use Count", LIBMTP_PROPERTY_UseCount, PTP_OPC_UseCount); |
| 539 | register_property("Skip Count", LIBMTP_PROPERTY_SkipCount, PTP_OPC_SkipCount); |
| 540 | register_property("Last Accessed", LIBMTP_PROPERTY_LastAccessed, PTP_OPC_LastAccessed); |
| 541 | register_property("Parental Rating", LIBMTP_PROPERTY_ParentalRating, PTP_OPC_ParentalRating); |
| 542 | register_property("Meta Genre", LIBMTP_PROPERTY_MetaGenre, PTP_OPC_MetaGenre); |
| 543 | register_property("Composer", LIBMTP_PROPERTY_Composer, PTP_OPC_Composer); |
| 544 | register_property("Effective Rating", LIBMTP_PROPERTY_EffectiveRating, PTP_OPC_EffectiveRating); |
| 545 | register_property("Subtitle", LIBMTP_PROPERTY_Subtitle, PTP_OPC_Subtitle); |
| 546 | register_property("Original Release Date", LIBMTP_PROPERTY_OriginalReleaseDate, PTP_OPC_OriginalReleaseDate); |
| 547 | register_property("Album Name", LIBMTP_PROPERTY_AlbumName, PTP_OPC_AlbumName); |
| 548 | register_property("Album Artist", LIBMTP_PROPERTY_AlbumArtist, PTP_OPC_AlbumArtist); |
| 549 | register_property("Mood", LIBMTP_PROPERTY_Mood, PTP_OPC_Mood); |
| 550 | register_property("DRM Status", LIBMTP_PROPERTY_DRMStatus, PTP_OPC_DRMStatus); |
| 551 | register_property("Sub Description", LIBMTP_PROPERTY_SubDescription, PTP_OPC_SubDescription); |
| 552 | register_property("Is Cropped", LIBMTP_PROPERTY_IsCropped, PTP_OPC_IsCropped); |
| 553 | register_property("Is Color Corrected", LIBMTP_PROPERTY_IsColorCorrected, PTP_OPC_IsColorCorrected); |
| 554 | register_property("Image Bit Depth", LIBMTP_PROPERTY_ImageBitDepth, PTP_OPC_ImageBitDepth); |
| 555 | register_property("f Number", LIBMTP_PROPERTY_Fnumber, PTP_OPC_Fnumber); |
| 556 | register_property("Exposure Time", LIBMTP_PROPERTY_ExposureTime, PTP_OPC_ExposureTime); |
| 557 | register_property("Exposure Index", LIBMTP_PROPERTY_ExposureIndex, PTP_OPC_ExposureIndex); |
| 558 | register_property("Display Name", LIBMTP_PROPERTY_DisplayName, PTP_OPC_DisplayName); |
| 559 | register_property("Body Text", LIBMTP_PROPERTY_BodyText, PTP_OPC_BodyText); |
| 560 | register_property("Subject", LIBMTP_PROPERTY_Subject, PTP_OPC_Subject); |
| 561 | register_property("Priority", LIBMTP_PROPERTY_Priority, PTP_OPC_Priority); |
| 562 | register_property("Given Name", LIBMTP_PROPERTY_GivenName, PTP_OPC_GivenName); |
| 563 | register_property("Middle Names", LIBMTP_PROPERTY_MiddleNames, PTP_OPC_MiddleNames); |
| 564 | register_property("Family Name", LIBMTP_PROPERTY_FamilyName, PTP_OPC_FamilyName); |
| 565 | register_property("Prefix", LIBMTP_PROPERTY_Prefix, PTP_OPC_Prefix); |
| 566 | register_property("Suffix", LIBMTP_PROPERTY_Suffix, PTP_OPC_Suffix); |
| 567 | register_property("Phonetic Given Name", LIBMTP_PROPERTY_PhoneticGivenName, PTP_OPC_PhoneticGivenName); |
| 568 | register_property("Phonetic Family Name", LIBMTP_PROPERTY_PhoneticFamilyName, PTP_OPC_PhoneticFamilyName); |
| 569 | register_property("Email: Primary", LIBMTP_PROPERTY_EmailPrimary, PTP_OPC_EmailPrimary); |
| 570 | register_property("Email: Personal 1", LIBMTP_PROPERTY_EmailPersonal1, PTP_OPC_EmailPersonal1); |
| 571 | register_property("Email: Personal 2", LIBMTP_PROPERTY_EmailPersonal2, PTP_OPC_EmailPersonal2); |
| 572 | register_property("Email: Business 1", LIBMTP_PROPERTY_EmailBusiness1, PTP_OPC_EmailBusiness1); |
| 573 | register_property("Email: Business 2", LIBMTP_PROPERTY_EmailBusiness2, PTP_OPC_EmailBusiness2); |
| 574 | register_property("Email: Others", LIBMTP_PROPERTY_EmailOthers, PTP_OPC_EmailOthers); |
| 575 | register_property("Phone Number: Primary", LIBMTP_PROPERTY_PhoneNumberPrimary, PTP_OPC_PhoneNumberPrimary); |
| 576 | register_property("Phone Number: Personal", LIBMTP_PROPERTY_PhoneNumberPersonal, PTP_OPC_PhoneNumberPersonal); |
| 577 | register_property("Phone Number: Personal 2", LIBMTP_PROPERTY_PhoneNumberPersonal2, PTP_OPC_PhoneNumberPersonal2); |
| 578 | register_property("Phone Number: Business", LIBMTP_PROPERTY_PhoneNumberBusiness, PTP_OPC_PhoneNumberBusiness); |
| 579 | register_property("Phone Number: Business 2", LIBMTP_PROPERTY_PhoneNumberBusiness2, PTP_OPC_PhoneNumberBusiness2); |
| 580 | register_property("Phone Number: Mobile", LIBMTP_PROPERTY_PhoneNumberMobile, PTP_OPC_PhoneNumberMobile); |
| 581 | register_property("Phone Number: Mobile 2", LIBMTP_PROPERTY_PhoneNumberMobile2, PTP_OPC_PhoneNumberMobile2); |
| 582 | register_property("Fax Number: Primary", LIBMTP_PROPERTY_FaxNumberPrimary, PTP_OPC_FaxNumberPrimary); |
| 583 | register_property("Fax Number: Personal", LIBMTP_PROPERTY_FaxNumberPersonal, PTP_OPC_FaxNumberPersonal); |
| 584 | register_property("Fax Number: Business", LIBMTP_PROPERTY_FaxNumberBusiness, PTP_OPC_FaxNumberBusiness); |
| 585 | register_property("Pager Number", LIBMTP_PROPERTY_PagerNumber, PTP_OPC_PagerNumber); |
| 586 | register_property("Phone Number: Others", LIBMTP_PROPERTY_PhoneNumberOthers, PTP_OPC_PhoneNumberOthers); |
| 587 | register_property("Primary Web Address", LIBMTP_PROPERTY_PrimaryWebAddress, PTP_OPC_PrimaryWebAddress); |
| 588 | register_property("Personal Web Address", LIBMTP_PROPERTY_PersonalWebAddress, PTP_OPC_PersonalWebAddress); |
| 589 | register_property("Business Web Address", LIBMTP_PROPERTY_BusinessWebAddress, PTP_OPC_BusinessWebAddress); |
| 590 | register_property("Instant Messenger Address 1", LIBMTP_PROPERTY_InstantMessengerAddress, PTP_OPC_InstantMessengerAddress); |
| 591 | register_property("Instant Messenger Address 2", LIBMTP_PROPERTY_InstantMessengerAddress2, PTP_OPC_InstantMessengerAddress2); |
| 592 | register_property("Instant Messenger Address 3", LIBMTP_PROPERTY_InstantMessengerAddress3, PTP_OPC_InstantMessengerAddress3); |
| 593 | register_property("Postal Address: Personal: Full", LIBMTP_PROPERTY_PostalAddressPersonalFull, PTP_OPC_PostalAddressPersonalFull); |
| 594 | register_property("Postal Address: Personal: Line 1", LIBMTP_PROPERTY_PostalAddressPersonalFullLine1, PTP_OPC_PostalAddressPersonalFullLine1); |
| 595 | register_property("Postal Address: Personal: Line 2", LIBMTP_PROPERTY_PostalAddressPersonalFullLine2, PTP_OPC_PostalAddressPersonalFullLine2); |
| 596 | register_property("Postal Address: Personal: City", LIBMTP_PROPERTY_PostalAddressPersonalFullCity, PTP_OPC_PostalAddressPersonalFullCity); |
| 597 | register_property("Postal Address: Personal: Region", LIBMTP_PROPERTY_PostalAddressPersonalFullRegion, PTP_OPC_PostalAddressPersonalFullRegion); |
| 598 | register_property("Postal Address: Personal: Postal Code", LIBMTP_PROPERTY_PostalAddressPersonalFullPostalCode, PTP_OPC_PostalAddressPersonalFullPostalCode); |
| 599 | register_property("Postal Address: Personal: Country", LIBMTP_PROPERTY_PostalAddressPersonalFullCountry, PTP_OPC_PostalAddressPersonalFullCountry); |
| 600 | register_property("Postal Address: Business: Full", LIBMTP_PROPERTY_PostalAddressBusinessFull, PTP_OPC_PostalAddressBusinessFull); |
| 601 | register_property("Postal Address: Business: Line 1", LIBMTP_PROPERTY_PostalAddressBusinessLine1, PTP_OPC_PostalAddressBusinessLine1); |
| 602 | register_property("Postal Address: Business: Line 2", LIBMTP_PROPERTY_PostalAddressBusinessLine2, PTP_OPC_PostalAddressBusinessLine2); |
| 603 | register_property("Postal Address: Business: City", LIBMTP_PROPERTY_PostalAddressBusinessCity, PTP_OPC_PostalAddressBusinessCity); |
| 604 | register_property("Postal Address: Business: Region", LIBMTP_PROPERTY_PostalAddressBusinessRegion, PTP_OPC_PostalAddressBusinessRegion); |
| 605 | register_property("Postal Address: Business: Postal Code", LIBMTP_PROPERTY_PostalAddressBusinessPostalCode, PTP_OPC_PostalAddressBusinessPostalCode); |
| 606 | register_property("Postal Address: Business: Country", LIBMTP_PROPERTY_PostalAddressBusinessCountry, PTP_OPC_PostalAddressBusinessCountry); |
| 607 | register_property("Postal Address: Other: Full", LIBMTP_PROPERTY_PostalAddressOtherFull, PTP_OPC_PostalAddressOtherFull); |
| 608 | register_property("Postal Address: Other: Line 1", LIBMTP_PROPERTY_PostalAddressOtherLine1, PTP_OPC_PostalAddressOtherLine1); |
| 609 | register_property("Postal Address: Other: Line 2", LIBMTP_PROPERTY_PostalAddressOtherLine2, PTP_OPC_PostalAddressOtherLine2); |
| 610 | register_property("Postal Address: Other: City", LIBMTP_PROPERTY_PostalAddressOtherCity, PTP_OPC_PostalAddressOtherCity); |
| 611 | register_property("Postal Address: Other: Region", LIBMTP_PROPERTY_PostalAddressOtherRegion, PTP_OPC_PostalAddressOtherRegion); |
| 612 | register_property("Postal Address: Other: Postal Code", LIBMTP_PROPERTY_PostalAddressOtherPostalCode, PTP_OPC_PostalAddressOtherPostalCode); |
| 613 | register_property("Postal Address: Other: Counrtry", LIBMTP_PROPERTY_PostalAddressOtherCountry, PTP_OPC_PostalAddressOtherCountry); |
| 614 | register_property("Organization Name", LIBMTP_PROPERTY_OrganizationName, PTP_OPC_OrganizationName); |
| 615 | register_property("Phonetic Organization Name", LIBMTP_PROPERTY_PhoneticOrganizationName, PTP_OPC_PhoneticOrganizationName); |
| 616 | register_property("Role", LIBMTP_PROPERTY_Role, PTP_OPC_Role); |
| 617 | register_property("Birthdate", LIBMTP_PROPERTY_Birthdate, PTP_OPC_Birthdate); |
| 618 | register_property("Message To", LIBMTP_PROPERTY_MessageTo, PTP_OPC_MessageTo); |
| 619 | register_property("Message CC", LIBMTP_PROPERTY_MessageCC, PTP_OPC_MessageCC); |
| 620 | register_property("Message BCC", LIBMTP_PROPERTY_MessageBCC, PTP_OPC_MessageBCC); |
| 621 | register_property("Message Read", LIBMTP_PROPERTY_MessageRead, PTP_OPC_MessageRead); |
| 622 | register_property("Message Received Time", LIBMTP_PROPERTY_MessageReceivedTime, PTP_OPC_MessageReceivedTime); |
| 623 | register_property("Message Sender", LIBMTP_PROPERTY_MessageSender, PTP_OPC_MessageSender); |
| 624 | register_property("Activity Begin Time", LIBMTP_PROPERTY_ActivityBeginTime, PTP_OPC_ActivityBeginTime); |
| 625 | register_property("Activity End Time", LIBMTP_PROPERTY_ActivityEndTime, PTP_OPC_ActivityEndTime); |
| 626 | register_property("Activity Location", LIBMTP_PROPERTY_ActivityLocation, PTP_OPC_ActivityLocation); |
| 627 | register_property("Activity Required Attendees", LIBMTP_PROPERTY_ActivityRequiredAttendees, PTP_OPC_ActivityRequiredAttendees); |
| 628 | register_property("Optional Attendees", LIBMTP_PROPERTY_ActivityOptionalAttendees, PTP_OPC_ActivityOptionalAttendees); |
| 629 | register_property("Activity Resources", LIBMTP_PROPERTY_ActivityResources, PTP_OPC_ActivityResources); |
| 630 | register_property("Activity Accepted", LIBMTP_PROPERTY_ActivityAccepted, PTP_OPC_ActivityAccepted); |
| 631 | register_property("Owner", LIBMTP_PROPERTY_Owner, PTP_OPC_Owner); |
| 632 | register_property("Editor", LIBMTP_PROPERTY_Editor, PTP_OPC_Editor); |
| 633 | register_property("Webmaster", LIBMTP_PROPERTY_Webmaster, PTP_OPC_Webmaster); |
| 634 | register_property("URL Source", LIBMTP_PROPERTY_URLSource, PTP_OPC_URLSource); |
| 635 | register_property("URL Destination", LIBMTP_PROPERTY_URLDestination, PTP_OPC_URLDestination); |
| 636 | register_property("Time Bookmark", LIBMTP_PROPERTY_TimeBookmark, PTP_OPC_TimeBookmark); |
| 637 | register_property("Object Bookmark", LIBMTP_PROPERTY_ObjectBookmark, PTP_OPC_ObjectBookmark); |
| 638 | register_property("Byte Bookmark", LIBMTP_PROPERTY_ByteBookmark, PTP_OPC_ByteBookmark); |
| 639 | register_property("Last Build Date", LIBMTP_PROPERTY_LastBuildDate, PTP_OPC_LastBuildDate); |
| 640 | register_property("Time To Live", LIBMTP_PROPERTY_TimetoLive, PTP_OPC_TimetoLive); |
| 641 | register_property("Media GUID", LIBMTP_PROPERTY_MediaGUID, PTP_OPC_MediaGUID); |
| 642 | register_property("Total Bit Rate", LIBMTP_PROPERTY_TotalBitRate, PTP_OPC_TotalBitRate); |
| 643 | register_property("Bit Rate Type", LIBMTP_PROPERTY_BitRateType, PTP_OPC_BitRateType); |
| 644 | register_property("Sample Rate", LIBMTP_PROPERTY_SampleRate, PTP_OPC_SampleRate); |
| 645 | register_property("Number Of Channels", LIBMTP_PROPERTY_NumberOfChannels, PTP_OPC_NumberOfChannels); |
| 646 | register_property("Audio Bit Depth", LIBMTP_PROPERTY_AudioBitDepth, PTP_OPC_AudioBitDepth); |
| 647 | register_property("Scan Depth", LIBMTP_PROPERTY_ScanDepth, PTP_OPC_ScanDepth); |
| 648 | register_property("Audio WAVE Codec", LIBMTP_PROPERTY_AudioWAVECodec, PTP_OPC_AudioWAVECodec); |
| 649 | register_property("Audio Bit Rate", LIBMTP_PROPERTY_AudioBitRate, PTP_OPC_AudioBitRate); |
| 650 | register_property("Video Four CC Codec", LIBMTP_PROPERTY_VideoFourCCCodec, PTP_OPC_VideoFourCCCodec); |
| 651 | register_property("Video Bit Rate", LIBMTP_PROPERTY_VideoBitRate, PTP_OPC_VideoBitRate); |
| 652 | register_property("Frames Per Thousand Seconds", LIBMTP_PROPERTY_FramesPerThousandSeconds, PTP_OPC_FramesPerThousandSeconds); |
| 653 | register_property("Key Frame Distance", LIBMTP_PROPERTY_KeyFrameDistance, PTP_OPC_KeyFrameDistance); |
| 654 | register_property("Buffer Size", LIBMTP_PROPERTY_BufferSize, PTP_OPC_BufferSize); |
| 655 | register_property("Encoding Quality", LIBMTP_PROPERTY_EncodingQuality, PTP_OPC_EncodingQuality); |
| 656 | register_property("Encoding Profile", LIBMTP_PROPERTY_EncodingProfile, PTP_OPC_EncodingProfile); |
| 657 | register_property("Buy flag", LIBMTP_PROPERTY_BuyFlag, PTP_OPC_BuyFlag); |
| 658 | register_property("Unknown property", LIBMTP_PROPERTY_UNKNOWN, 0); |
| 659 | } |
| 660 | |
| 661 | /** |
| 662 | * Returns the PTP property that maps to a certain libmtp internal property type. |
| 663 | * @param inproperty the MTP library interface property |
| 664 | * @return the PTP (libgphoto2) property type |
| 665 | */ |
| 666 | static uint16_t map_libmtp_property_to_ptp_property(LIBMTP_property_t inproperty) |
| 667 | { |
| 668 | propertymap_t *current; |
| 669 | |
| 670 | current = propertymap; |
| 671 | |
| 672 | while (current != NULL) { |
| 673 | if(current->id == inproperty) { |
| 674 | return current->ptp_id; |
| 675 | } |
| 676 | current = current->next; |
| 677 | } |
| 678 | return 0; |
| 679 | } |
| 680 | |
| 681 | |
| 682 | /** |
| 683 | * Returns the MTP internal interface property that maps to a certain ptp |
| 684 | * interface property. |
| 685 | * @param inproperty the PTP (libgphoto2) interface property |
| 686 | * @return the MTP library interface property |
| 687 | */ |
| 688 | static LIBMTP_property_t map_ptp_property_to_libmtp_property(uint16_t inproperty) |
| 689 | { |
| 690 | propertymap_t *current; |
| 691 | |
| 692 | current = propertymap; |
| 693 | |
| 694 | while (current != NULL) { |
| 695 | if(current->ptp_id == inproperty) { |
| 696 | return current->id; |
| 697 | } |
| 698 | current = current->next; |
| 699 | } |
| 700 | // printf("map_ptp_type_to_libmtp_type: unknown filetype.\n"); |
| 701 | return LIBMTP_PROPERTY_UNKNOWN; |
| 702 | } |
| 703 | |
Linus Walleij | fa1374c | 2006-02-27 07:41:46 +0000 | [diff] [blame] | 704 | |
Linus Walleij | 6946ac5 | 2006-03-21 06:51:22 +0000 | [diff] [blame] | 705 | /** |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 706 | * Initialize the library. You are only supposed to call this |
| 707 | * one, before using the library for the first time in a program. |
| 708 | * Never re-initialize libmtp! |
| 709 | * |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 710 | * The only thing this does at the moment is to initialise the |
| 711 | * filetype mapping table. |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 712 | */ |
| 713 | void LIBMTP_Init(void) |
| 714 | { |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 715 | init_filemap(); |
Richard Low | 6f07084 | 2009-05-03 10:26:16 +0000 | [diff] [blame] | 716 | init_propertymap(); |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 717 | return; |
| 718 | } |
| 719 | |
Linus Walleij | cf42f45 | 2006-11-28 08:32:49 +0000 | [diff] [blame] | 720 | |
| 721 | /** |
| 722 | * This helper function returns a textual description for a libmtp |
| 723 | * file type to be used in dialog boxes etc. |
| 724 | * @param intype the libmtp internal filetype to get a description for. |
| 725 | * @return a string representing the filetype, this must <b>NOT</b> |
| 726 | * be free():ed by the caller! |
| 727 | */ |
| 728 | char const * LIBMTP_Get_Filetype_Description(LIBMTP_filetype_t intype) |
| 729 | { |
| 730 | filemap_t *current; |
| 731 | |
| 732 | current = filemap; |
| 733 | |
| 734 | while (current != NULL) { |
| 735 | if(current->id == intype) { |
| 736 | return current->description; |
| 737 | } |
| 738 | current = current->next; |
| 739 | } |
| 740 | |
| 741 | return "Unknown filetype"; |
| 742 | } |
| 743 | |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 744 | /** |
Richard Low | 6f07084 | 2009-05-03 10:26:16 +0000 | [diff] [blame] | 745 | * This helper function returns a textual description for a libmtp |
| 746 | * property to be used in dialog boxes etc. |
| 747 | * @param inproperty the libmtp internal property to get a description for. |
| 748 | * @return a string representing the filetype, this must <b>NOT</b> |
| 749 | * be free():ed by the caller! |
| 750 | */ |
| 751 | char const * LIBMTP_Get_Property_Description(LIBMTP_property_t inproperty) |
| 752 | { |
| 753 | propertymap_t *current; |
| 754 | |
| 755 | current = propertymap; |
| 756 | |
| 757 | while (current != NULL) { |
| 758 | if(current->id == inproperty) { |
| 759 | return current->description; |
| 760 | } |
| 761 | current = current->next; |
| 762 | } |
| 763 | |
| 764 | return "Unknown property"; |
| 765 | } |
| 766 | |
| 767 | /** |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 768 | * This function will do its best to fit a 16bit |
| 769 | * value into a PTP object property if the property |
| 770 | * is limited in range or step sizes. |
| 771 | */ |
Linus Walleij | 2955956 | 2007-08-22 21:38:04 +0000 | [diff] [blame] | 772 | static uint16_t adjust_u16(uint16_t val, PTPObjectPropDesc *opd) |
| 773 | { |
| 774 | switch (opd->FormFlag) { |
| 775 | case PTP_DPFF_Range: |
| 776 | if (val < opd->FORM.Range.MinimumValue.u16) { |
| 777 | return opd->FORM.Range.MinimumValue.u16; |
| 778 | } |
| 779 | if (val > opd->FORM.Range.MaximumValue.u16) { |
| 780 | return opd->FORM.Range.MaximumValue.u16; |
| 781 | } |
| 782 | // Round down to last step. |
| 783 | if (val % opd->FORM.Range.StepSize.u16 != 0) { |
| 784 | return val - (val % opd->FORM.Range.StepSize.u16); |
| 785 | } |
| 786 | return val; |
| 787 | break; |
| 788 | case PTP_DPFF_Enumeration: |
| 789 | { |
| 790 | int i; |
| 791 | uint16_t bestfit = opd->FORM.Enum.SupportedValue[0].u16; |
| 792 | |
| 793 | for (i=0; i<opd->FORM.Enum.NumberOfValues; i++) { |
| 794 | if (val == opd->FORM.Enum.SupportedValue[i].u16) { |
| 795 | return val; |
| 796 | } |
| 797 | // Rough guess of best fit |
| 798 | if (opd->FORM.Enum.SupportedValue[i].u16 < val) { |
| 799 | bestfit = opd->FORM.Enum.SupportedValue[i].u16; |
| 800 | } |
| 801 | } |
| 802 | // Just some default that'll work. |
| 803 | return bestfit; |
| 804 | } |
| 805 | default: |
| 806 | // Will accept any value |
| 807 | break; |
| 808 | } |
| 809 | return val; |
| 810 | } |
| 811 | |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 812 | /** |
| 813 | * This function will do its best to fit a 32bit |
| 814 | * value into a PTP object property if the property |
| 815 | * is limited in range or step sizes. |
| 816 | */ |
Linus Walleij | 2955956 | 2007-08-22 21:38:04 +0000 | [diff] [blame] | 817 | static uint32_t adjust_u32(uint32_t val, PTPObjectPropDesc *opd) |
| 818 | { |
| 819 | switch (opd->FormFlag) { |
| 820 | case PTP_DPFF_Range: |
| 821 | if (val < opd->FORM.Range.MinimumValue.u32) { |
| 822 | return opd->FORM.Range.MinimumValue.u32; |
| 823 | } |
| 824 | if (val > opd->FORM.Range.MaximumValue.u32) { |
| 825 | return opd->FORM.Range.MaximumValue.u32; |
| 826 | } |
| 827 | // Round down to last step. |
| 828 | if (val % opd->FORM.Range.StepSize.u32 != 0) { |
| 829 | return val - (val % opd->FORM.Range.StepSize.u32); |
| 830 | } |
| 831 | return val; |
| 832 | break; |
| 833 | case PTP_DPFF_Enumeration: |
| 834 | { |
| 835 | int i; |
| 836 | uint32_t bestfit = opd->FORM.Enum.SupportedValue[0].u32; |
| 837 | |
| 838 | for (i=0; i<opd->FORM.Enum.NumberOfValues; i++) { |
| 839 | if (val == opd->FORM.Enum.SupportedValue[i].u32) { |
| 840 | return val; |
| 841 | } |
| 842 | // Rough guess of best fit |
| 843 | if (opd->FORM.Enum.SupportedValue[i].u32 < val) { |
| 844 | bestfit = opd->FORM.Enum.SupportedValue[i].u32; |
| 845 | } |
| 846 | } |
| 847 | // Just some default that'll work. |
| 848 | return bestfit; |
| 849 | } |
| 850 | default: |
| 851 | // Will accept any value |
| 852 | break; |
| 853 | } |
| 854 | return val; |
| 855 | } |
| 856 | |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 857 | /** |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 858 | * This function returns a newly created ISO 8601 timestamp with the |
| 859 | * current time in as high precision as possible. It even adds |
| 860 | * the time zone if it can. |
| 861 | */ |
| 862 | static char *get_iso8601_stamp(void) |
| 863 | { |
| 864 | time_t curtime; |
| 865 | struct tm *loctime; |
| 866 | char tmp[64]; |
| 867 | |
| 868 | curtime = time(NULL); |
| 869 | loctime = localtime(&curtime); |
| 870 | strftime (tmp, sizeof(tmp), "%Y%m%dT%H%M%S.0%z", loctime); |
| 871 | return strdup(tmp); |
| 872 | } |
| 873 | |
| 874 | /** |
Richard Low | 6f07084 | 2009-05-03 10:26:16 +0000 | [diff] [blame] | 875 | * Gets the allowed values (range or enum) for a property |
| 876 | * @param device a pointer to an MTP device |
| 877 | * @param property the property to query |
| 878 | * @param filetype the filetype of the object you want to set values for |
| 879 | * @param allowed_vals pointer to a LIBMTP_allowed_values_t struct to |
| 880 | * receive the allowed values. Call LIBMTP_destroy_allowed_values_t |
| 881 | * on this on successful completion. |
| 882 | * @return 0 on success, any other value means failure |
| 883 | */ |
| 884 | int LIBMTP_Get_Allowed_Property_Values(LIBMTP_mtpdevice_t *device, LIBMTP_property_t const property, |
| 885 | LIBMTP_filetype_t const filetype, LIBMTP_allowed_values_t *allowed_vals) |
| 886 | { |
| 887 | PTPObjectPropDesc opd; |
| 888 | uint16_t ret = 0; |
| 889 | |
| 890 | ret = ptp_mtp_getobjectpropdesc(device->params, map_libmtp_property_to_ptp_property(property), map_libmtp_type_to_ptp_type(filetype), &opd); |
| 891 | if (ret != PTP_RC_OK) { |
| 892 | add_ptp_error_to_errorstack(device, ret, "LIBMTP_Get_Allowed_Property_Values(): could not get property description."); |
| 893 | return -1; |
| 894 | } |
| 895 | |
| 896 | if (opd.FormFlag == PTP_OPFF_Enumeration) { |
| 897 | int i = 0; |
| 898 | |
| 899 | allowed_vals->is_range = 0; |
| 900 | allowed_vals->num_entries = opd.FORM.Enum.NumberOfValues; |
| 901 | |
| 902 | switch (opd.DataType) |
| 903 | { |
| 904 | case PTP_DTC_INT8: |
| 905 | allowed_vals->i8vals = malloc(sizeof(int8_t) * opd.FORM.Enum.NumberOfValues); |
| 906 | allowed_vals->datatype = LIBMTP_DATATYPE_INT8; |
| 907 | break; |
| 908 | case PTP_DTC_UINT8: |
| 909 | allowed_vals->u8vals = malloc(sizeof(uint8_t) * opd.FORM.Enum.NumberOfValues); |
| 910 | allowed_vals->datatype = LIBMTP_DATATYPE_UINT8; |
| 911 | break; |
| 912 | case PTP_DTC_INT16: |
| 913 | allowed_vals->i16vals = malloc(sizeof(int16_t) * opd.FORM.Enum.NumberOfValues); |
| 914 | allowed_vals->datatype = LIBMTP_DATATYPE_INT16; |
| 915 | break; |
| 916 | case PTP_DTC_UINT16: |
| 917 | allowed_vals->u16vals = malloc(sizeof(uint16_t) * opd.FORM.Enum.NumberOfValues); |
| 918 | allowed_vals->datatype = LIBMTP_DATATYPE_UINT16; |
| 919 | break; |
| 920 | case PTP_DTC_INT32: |
| 921 | allowed_vals->i32vals = malloc(sizeof(int32_t) * opd.FORM.Enum.NumberOfValues); |
| 922 | allowed_vals->datatype = LIBMTP_DATATYPE_INT32; |
| 923 | break; |
| 924 | case PTP_DTC_UINT32: |
| 925 | allowed_vals->u32vals = malloc(sizeof(uint32_t) * opd.FORM.Enum.NumberOfValues); |
| 926 | allowed_vals->datatype = LIBMTP_DATATYPE_UINT32; |
| 927 | break; |
| 928 | case PTP_DTC_INT64: |
| 929 | allowed_vals->i64vals = malloc(sizeof(int64_t) * opd.FORM.Enum.NumberOfValues); |
| 930 | allowed_vals->datatype = LIBMTP_DATATYPE_INT64; |
| 931 | break; |
| 932 | case PTP_DTC_UINT64: |
| 933 | allowed_vals->u64vals = malloc(sizeof(uint64_t) * opd.FORM.Enum.NumberOfValues); |
| 934 | allowed_vals->datatype = LIBMTP_DATATYPE_UINT64; |
| 935 | break; |
| 936 | } |
| 937 | |
| 938 | for (i = 0; i < opd.FORM.Enum.NumberOfValues; i++) { |
| 939 | switch (opd.DataType) |
| 940 | { |
| 941 | case PTP_DTC_INT8: |
| 942 | allowed_vals->i8vals[i] = opd.FORM.Enum.SupportedValue[i].i8; |
| 943 | break; |
| 944 | case PTP_DTC_UINT8: |
| 945 | allowed_vals->u8vals[i] = opd.FORM.Enum.SupportedValue[i].u8; |
| 946 | break; |
| 947 | case PTP_DTC_INT16: |
| 948 | allowed_vals->i16vals[i] = opd.FORM.Enum.SupportedValue[i].i16; |
| 949 | break; |
| 950 | case PTP_DTC_UINT16: |
| 951 | allowed_vals->u16vals[i] = opd.FORM.Enum.SupportedValue[i].u16; |
| 952 | break; |
| 953 | case PTP_DTC_INT32: |
| 954 | allowed_vals->i32vals[i] = opd.FORM.Enum.SupportedValue[i].i32; |
| 955 | break; |
| 956 | case PTP_DTC_UINT32: |
| 957 | allowed_vals->u32vals[i] = opd.FORM.Enum.SupportedValue[i].u32; |
| 958 | break; |
| 959 | case PTP_DTC_INT64: |
| 960 | allowed_vals->i64vals[i] = opd.FORM.Enum.SupportedValue[i].i64; |
| 961 | break; |
| 962 | case PTP_DTC_UINT64: |
| 963 | allowed_vals->u64vals[i] = opd.FORM.Enum.SupportedValue[i].u64; |
| 964 | break; |
| 965 | } |
| 966 | } |
| 967 | ptp_free_objectpropdesc(&opd); |
| 968 | return 0; |
| 969 | } else if (opd.FormFlag == PTP_OPFF_Range) { |
| 970 | allowed_vals->is_range = 1; |
| 971 | |
| 972 | switch (opd.DataType) |
| 973 | { |
| 974 | case PTP_DTC_INT8: |
| 975 | allowed_vals->i8min = opd.FORM.Range.MinimumValue.i8; |
| 976 | allowed_vals->i8max = opd.FORM.Range.MaximumValue.i8; |
| 977 | allowed_vals->i8step = opd.FORM.Range.StepSize.i8; |
| 978 | allowed_vals->datatype = LIBMTP_DATATYPE_INT8; |
| 979 | break; |
| 980 | case PTP_DTC_UINT8: |
| 981 | allowed_vals->u8min = opd.FORM.Range.MinimumValue.u8; |
| 982 | allowed_vals->u8max = opd.FORM.Range.MaximumValue.u8; |
| 983 | allowed_vals->u8step = opd.FORM.Range.StepSize.u8; |
| 984 | allowed_vals->datatype = LIBMTP_DATATYPE_UINT8; |
| 985 | break; |
| 986 | case PTP_DTC_INT16: |
| 987 | allowed_vals->i16min = opd.FORM.Range.MinimumValue.i16; |
| 988 | allowed_vals->i16max = opd.FORM.Range.MaximumValue.i16; |
| 989 | allowed_vals->i16step = opd.FORM.Range.StepSize.i16; |
| 990 | allowed_vals->datatype = LIBMTP_DATATYPE_INT16; |
| 991 | break; |
| 992 | case PTP_DTC_UINT16: |
| 993 | allowed_vals->u16min = opd.FORM.Range.MinimumValue.u16; |
| 994 | allowed_vals->u16max = opd.FORM.Range.MaximumValue.u16; |
| 995 | allowed_vals->u16step = opd.FORM.Range.StepSize.u16; |
| 996 | allowed_vals->datatype = LIBMTP_DATATYPE_UINT16; |
| 997 | break; |
| 998 | case PTP_DTC_INT32: |
| 999 | allowed_vals->i32min = opd.FORM.Range.MinimumValue.i32; |
| 1000 | allowed_vals->i32max = opd.FORM.Range.MaximumValue.i32; |
| 1001 | allowed_vals->i32step = opd.FORM.Range.StepSize.i32; |
| 1002 | allowed_vals->datatype = LIBMTP_DATATYPE_INT32; |
| 1003 | break; |
| 1004 | case PTP_DTC_UINT32: |
| 1005 | allowed_vals->u32min = opd.FORM.Range.MinimumValue.u32; |
| 1006 | allowed_vals->u32max = opd.FORM.Range.MaximumValue.u32; |
| 1007 | allowed_vals->u32step = opd.FORM.Range.StepSize.u32; |
| 1008 | allowed_vals->datatype = LIBMTP_DATATYPE_UINT32; |
| 1009 | break; |
| 1010 | case PTP_DTC_INT64: |
| 1011 | allowed_vals->i64min = opd.FORM.Range.MinimumValue.i64; |
| 1012 | allowed_vals->i64max = opd.FORM.Range.MaximumValue.i64; |
| 1013 | allowed_vals->i64step = opd.FORM.Range.StepSize.i64; |
| 1014 | allowed_vals->datatype = LIBMTP_DATATYPE_INT64; |
| 1015 | break; |
| 1016 | case PTP_DTC_UINT64: |
| 1017 | allowed_vals->u64min = opd.FORM.Range.MinimumValue.u64; |
| 1018 | allowed_vals->u64max = opd.FORM.Range.MaximumValue.u64; |
| 1019 | allowed_vals->u64step = opd.FORM.Range.StepSize.u64; |
| 1020 | allowed_vals->datatype = LIBMTP_DATATYPE_UINT64; |
| 1021 | break; |
| 1022 | } |
| 1023 | return 0; |
| 1024 | } else |
| 1025 | return -1; |
| 1026 | } |
| 1027 | |
| 1028 | /** |
| 1029 | * Destroys a LIBMTP_allowed_values_t struct |
| 1030 | * @param allowed_vals the struct to destroy |
| 1031 | */ |
| 1032 | void LIBMTP_destroy_allowed_values_t(LIBMTP_allowed_values_t *allowed_vals) |
| 1033 | { |
| 1034 | if (!allowed_vals->is_range) |
| 1035 | { |
| 1036 | switch (allowed_vals->datatype) |
| 1037 | { |
| 1038 | case LIBMTP_DATATYPE_INT8: |
| 1039 | if (allowed_vals->i8vals) |
| 1040 | free(allowed_vals->i8vals); |
| 1041 | break; |
| 1042 | case LIBMTP_DATATYPE_UINT8: |
| 1043 | if (allowed_vals->u8vals) |
| 1044 | free(allowed_vals->u8vals); |
| 1045 | break; |
| 1046 | case LIBMTP_DATATYPE_INT16: |
| 1047 | if (allowed_vals->i16vals) |
| 1048 | free(allowed_vals->i16vals); |
| 1049 | break; |
| 1050 | case LIBMTP_DATATYPE_UINT16: |
| 1051 | if (allowed_vals->u16vals) |
| 1052 | free(allowed_vals->u16vals); |
| 1053 | break; |
| 1054 | case LIBMTP_DATATYPE_INT32: |
| 1055 | if (allowed_vals->i32vals) |
| 1056 | free(allowed_vals->i32vals); |
| 1057 | break; |
| 1058 | case LIBMTP_DATATYPE_UINT32: |
| 1059 | if (allowed_vals->u32vals) |
| 1060 | free(allowed_vals->u32vals); |
| 1061 | break; |
| 1062 | case LIBMTP_DATATYPE_INT64: |
| 1063 | if (allowed_vals->i64vals) |
| 1064 | free(allowed_vals->i64vals); |
| 1065 | break; |
| 1066 | case LIBMTP_DATATYPE_UINT64: |
| 1067 | if (allowed_vals->u64vals) |
| 1068 | free(allowed_vals->u64vals); |
| 1069 | break; |
| 1070 | } |
| 1071 | } |
| 1072 | } |
| 1073 | |
| 1074 | /** |
| 1075 | * Determine if a property is supported for a given file type |
| 1076 | * @param device a pointer to an MTP device |
| 1077 | * @param property the property to query |
| 1078 | * @param filetype the filetype of the object you want to set values for |
| 1079 | * @return 0 if not supported, positive if supported, negative on error |
| 1080 | */ |
| 1081 | int LIBMTP_Is_Property_Supported(LIBMTP_mtpdevice_t *device, LIBMTP_property_t const property, |
| 1082 | LIBMTP_filetype_t const filetype) |
| 1083 | { |
| 1084 | uint16_t *props = NULL; |
| 1085 | uint32_t propcnt = 0; |
| 1086 | uint16_t ret = 0; |
| 1087 | int i = 0; |
| 1088 | int supported = 0; |
| 1089 | uint16_t ptp_prop = map_libmtp_property_to_ptp_property(property); |
| 1090 | |
| 1091 | ret = ptp_mtp_getobjectpropssupported(device->params, map_libmtp_type_to_ptp_type(filetype), &propcnt, &props); |
| 1092 | if (ret != PTP_RC_OK) { |
| 1093 | add_ptp_error_to_errorstack(device, ret, "LIBMTP_Is_Property_Supported(): could not get properties supported."); |
| 1094 | return -1; |
| 1095 | } |
| 1096 | |
| 1097 | for (i = 0; i < propcnt; i++) { |
| 1098 | if (props[i] == ptp_prop) { |
| 1099 | supported = 1; |
| 1100 | break; |
| 1101 | } |
| 1102 | } |
| 1103 | |
| 1104 | free(props); |
| 1105 | |
| 1106 | return supported; |
| 1107 | } |
| 1108 | |
| 1109 | /** |
| 1110 | * Retrieves a string from an object |
| 1111 | * |
| 1112 | * @param device a pointer to an MTP device. |
| 1113 | * @param object_id Object reference |
| 1114 | * @param attribute_id MTP attribute ID |
| 1115 | * @return valid string or NULL on failure. The returned string |
| 1116 | * must bee <code>free()</code>:ed by the caller after |
| 1117 | * use. |
| 1118 | */ |
| 1119 | char *LIBMTP_Get_String_From_Object(LIBMTP_mtpdevice_t *device, uint32_t const object_id, |
| 1120 | LIBMTP_property_t const attribute_id) |
| 1121 | { |
| 1122 | return get_string_from_object(device, object_id, attribute_id); |
| 1123 | } |
| 1124 | |
| 1125 | /** |
| 1126 | * Retrieves an unsigned 64-bit integer from an object attribute |
| 1127 | * |
| 1128 | * @param device a pointer to an MTP device. |
| 1129 | * @param object_id Object reference |
| 1130 | * @param attribute_id MTP attribute ID |
| 1131 | * @param value_default Default value to return on failure |
| 1132 | * @return the value |
| 1133 | */ |
| 1134 | uint64_t LIBMTP_Get_u64_From_Object(LIBMTP_mtpdevice_t *device,uint32_t const object_id, |
| 1135 | LIBMTP_property_t const attribute_id, uint64_t const value_default) |
| 1136 | { |
| 1137 | return get_u64_from_object(device, object_id, map_libmtp_property_to_ptp_property(attribute_id), value_default); |
| 1138 | } |
| 1139 | |
| 1140 | /** |
| 1141 | * Retrieves an unsigned 32-bit integer from an object attribute |
| 1142 | * |
| 1143 | * @param device a pointer to an MTP device. |
| 1144 | * @param object_id Object reference |
| 1145 | * @param attribute_id MTP attribute ID |
| 1146 | * @param value_default Default value to return on failure |
| 1147 | * @return the value |
| 1148 | */ |
| 1149 | uint32_t LIBMTP_Get_u32_From_Object(LIBMTP_mtpdevice_t *device,uint32_t const object_id, |
| 1150 | LIBMTP_property_t const attribute_id, uint32_t const value_default) |
| 1151 | { |
| 1152 | return get_u32_from_object(device, object_id, map_libmtp_property_to_ptp_property(attribute_id), value_default); |
| 1153 | } |
| 1154 | |
| 1155 | /** |
| 1156 | * Retrieves an unsigned 16-bit integer from an object attribute |
| 1157 | * |
| 1158 | * @param device a pointer to an MTP device. |
| 1159 | * @param object_id Object reference |
| 1160 | * @param attribute_id MTP attribute ID |
| 1161 | * @param value_default Default value to return on failure |
| 1162 | * @return a value |
| 1163 | */ |
| 1164 | uint16_t LIBMTP_Get_u16_From_Object(LIBMTP_mtpdevice_t *device, uint32_t const object_id, |
| 1165 | LIBMTP_property_t const attribute_id, uint16_t const value_default) |
| 1166 | { |
| 1167 | return get_u16_from_object(device, object_id, map_libmtp_property_to_ptp_property(attribute_id), value_default); |
| 1168 | } |
| 1169 | |
| 1170 | /** |
| 1171 | * Retrieves an unsigned 8-bit integer from an object attribute |
| 1172 | * |
| 1173 | * @param device a pointer to an MTP device. |
| 1174 | * @param object_id Object reference |
| 1175 | * @param attribute_id MTP attribute ID |
| 1176 | * @param value_default Default value to return on failure |
| 1177 | * @return a value |
| 1178 | */ |
| 1179 | uint8_t LIBMTP_Get_u8_From_Object(LIBMTP_mtpdevice_t *device, uint32_t const object_id, |
| 1180 | LIBMTP_property_t const attribute_id, uint8_t const value_default) |
| 1181 | { |
| 1182 | return get_u8_from_object(device, object_id, map_libmtp_property_to_ptp_property(attribute_id), value_default); |
| 1183 | } |
| 1184 | |
| 1185 | /** |
| 1186 | * Sets an object attribute from a string |
| 1187 | * |
| 1188 | * @param device a pointer to an MTP device. |
| 1189 | * @param object_id Object reference |
| 1190 | * @param attribute_id MTP attribute ID |
| 1191 | * @param string string value to set |
| 1192 | * @return 0 on success, any other value means failure |
| 1193 | */ |
| 1194 | int LIBMTP_Set_Object_String(LIBMTP_mtpdevice_t *device, uint32_t const object_id, |
| 1195 | LIBMTP_property_t const attribute_id, char const * const string) |
| 1196 | { |
| 1197 | return set_object_string(device, object_id, map_libmtp_property_to_ptp_property(attribute_id), string); |
| 1198 | } |
| 1199 | |
| 1200 | |
| 1201 | /** |
| 1202 | * Sets an object attribute from an unsigned 32-bit integer |
| 1203 | * |
| 1204 | * @param device a pointer to an MTP device. |
| 1205 | * @param object_id Object reference |
| 1206 | * @param attribute_id MTP attribute ID |
| 1207 | * @param value 32-bit unsigned integer to set |
| 1208 | * @return 0 on success, any other value means failure |
| 1209 | */ |
| 1210 | int LIBMTP_Set_Object_u32(LIBMTP_mtpdevice_t *device, uint32_t const object_id, |
| 1211 | LIBMTP_property_t const attribute_id, uint32_t const value) |
| 1212 | { |
| 1213 | return set_object_u32(device, object_id, map_libmtp_property_to_ptp_property(attribute_id), value); |
| 1214 | } |
| 1215 | |
| 1216 | /** |
| 1217 | * Sets an object attribute from an unsigned 16-bit integer |
| 1218 | * |
| 1219 | * @param device a pointer to an MTP device. |
| 1220 | * @param object_id Object reference |
| 1221 | * @param attribute_id MTP attribute ID |
| 1222 | * @param value 16-bit unsigned integer to set |
| 1223 | * @return 0 on success, any other value means failure |
| 1224 | */ |
| 1225 | int LIBMTP_Set_Object_u16(LIBMTP_mtpdevice_t *device, uint32_t const object_id, |
| 1226 | LIBMTP_property_t const attribute_id, uint16_t const value) |
| 1227 | { |
| 1228 | return set_object_u16(device, object_id, map_libmtp_property_to_ptp_property(attribute_id), value); |
| 1229 | } |
| 1230 | |
| 1231 | /** |
| 1232 | * Sets an object attribute from an unsigned 8-bit integer |
| 1233 | * |
| 1234 | * @param device a pointer to an MTP device. |
| 1235 | * @param object_id Object reference |
| 1236 | * @param attribute_id MTP attribute ID |
| 1237 | * @param value 8-bit unsigned integer to set |
| 1238 | * @return 0 on success, any other value means failure |
| 1239 | */ |
| 1240 | int LIBMTP_Set_Object_u8(LIBMTP_mtpdevice_t *device, uint32_t const object_id, |
| 1241 | LIBMTP_property_t const attribute_id, uint8_t const value) |
| 1242 | { |
| 1243 | return set_object_u8(device, object_id, map_libmtp_property_to_ptp_property(attribute_id), value); |
| 1244 | } |
| 1245 | |
| 1246 | /** |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 1247 | * Retrieves a string from an object |
| 1248 | * |
| 1249 | * @param device a pointer to an MTP device. |
| 1250 | * @param object_id Object reference |
| 1251 | * @param attribute_id PTP attribute ID |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 1252 | * @return valid string or NULL on failure. The returned string |
| 1253 | * must bee <code>free()</code>:ed by the caller after |
| 1254 | * use. |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 1255 | */ |
Linus Walleij | 9901e22 | 2006-11-30 12:28:19 +0000 | [diff] [blame] | 1256 | 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] | 1257 | uint16_t const attribute_id) |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 1258 | { |
| 1259 | PTPPropertyValue propval; |
| 1260 | char *retstring = NULL; |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 1261 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 1262 | uint16_t ret; |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 1263 | MTPProperties *prop; |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 1264 | |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 1265 | if ( device == NULL || object_id == 0) { |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 1266 | return NULL; |
| 1267 | } |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 1268 | |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 1269 | prop = ptp_find_object_prop_in_cache(params, object_id, attribute_id); |
| 1270 | if (prop) { |
| 1271 | if (prop->propval.str != NULL) |
| 1272 | return strdup(prop->propval.str); |
| 1273 | else |
| 1274 | return NULL; |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 1275 | } |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 1276 | |
Linus Walleij | a823a70 | 2006-08-27 21:27:46 +0000 | [diff] [blame] | 1277 | ret = ptp_mtp_getobjectpropvalue(params, object_id, attribute_id, &propval, PTP_DTC_STR); |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 1278 | if (ret == PTP_RC_OK) { |
Linus Walleij | a823a70 | 2006-08-27 21:27:46 +0000 | [diff] [blame] | 1279 | if (propval.str != NULL) { |
| 1280 | retstring = (char *) strdup(propval.str); |
| 1281 | free(propval.str); |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 1282 | } |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 1283 | } else { |
| 1284 | 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] | 1285 | } |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 1286 | |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 1287 | return retstring; |
| 1288 | } |
| 1289 | |
| 1290 | /** |
Richard Low | c3f5fc3 | 2007-07-29 09:40:50 +0000 | [diff] [blame] | 1291 | * Retrieves an unsigned 64-bit integer from an object attribute |
| 1292 | * |
| 1293 | * @param device a pointer to an MTP device. |
| 1294 | * @param object_id Object reference |
| 1295 | * @param attribute_id PTP attribute ID |
| 1296 | * @param value_default Default value to return on failure |
| 1297 | * @return the value |
| 1298 | */ |
| 1299 | static uint64_t get_u64_from_object(LIBMTP_mtpdevice_t *device,uint32_t const object_id, |
| 1300 | uint16_t const attribute_id, uint64_t const value_default) |
| 1301 | { |
| 1302 | PTPPropertyValue propval; |
| 1303 | uint64_t retval = value_default; |
| 1304 | PTPParams *params = (PTPParams *) device->params; |
| 1305 | uint16_t ret; |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 1306 | MTPProperties *prop; |
Richard Low | c3f5fc3 | 2007-07-29 09:40:50 +0000 | [diff] [blame] | 1307 | |
| 1308 | if ( device == NULL ) { |
| 1309 | return value_default; |
| 1310 | } |
| 1311 | |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 1312 | prop = ptp_find_object_prop_in_cache(params, object_id, attribute_id); |
| 1313 | if (prop) |
| 1314 | return prop->propval.u64; |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 1315 | |
Richard Low | c3f5fc3 | 2007-07-29 09:40:50 +0000 | [diff] [blame] | 1316 | ret = ptp_mtp_getobjectpropvalue(params, object_id, |
| 1317 | attribute_id, |
| 1318 | &propval, |
| 1319 | PTP_DTC_UINT64); |
| 1320 | if (ret == PTP_RC_OK) { |
| 1321 | retval = propval.u64; |
| 1322 | } else { |
| 1323 | add_ptp_error_to_errorstack(device, ret, "get_u64_from_object(): could not get unsigned 64bit integer from object."); |
| 1324 | } |
| 1325 | |
| 1326 | return retval; |
| 1327 | } |
| 1328 | |
| 1329 | /** |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 1330 | * Retrieves an unsigned 32-bit integer from an object attribute |
| 1331 | * |
| 1332 | * @param device a pointer to an MTP device. |
| 1333 | * @param object_id Object reference |
| 1334 | * @param attribute_id PTP attribute ID |
Linus Walleij | 5acfa74 | 2006-05-29 14:51:59 +0000 | [diff] [blame] | 1335 | * @param value_default Default value to return on failure |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 1336 | * @return the value |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 1337 | */ |
Linus Walleij | 9901e22 | 2006-11-30 12:28:19 +0000 | [diff] [blame] | 1338 | 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] | 1339 | uint16_t const attribute_id, uint32_t const value_default) |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 1340 | { |
| 1341 | PTPPropertyValue propval; |
| 1342 | uint32_t retval = value_default; |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 1343 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 1344 | uint16_t ret; |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 1345 | MTPProperties *prop; |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 1346 | |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 1347 | if ( device == NULL ) { |
| 1348 | return value_default; |
| 1349 | } |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 1350 | |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 1351 | prop = ptp_find_object_prop_in_cache(params, object_id, attribute_id); |
| 1352 | if (prop) |
| 1353 | return prop->propval.u32; |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 1354 | |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 1355 | ret = ptp_mtp_getobjectpropvalue(params, object_id, |
| 1356 | attribute_id, |
| 1357 | &propval, |
| 1358 | PTP_DTC_UINT32); |
| 1359 | if (ret == PTP_RC_OK) { |
| 1360 | retval = propval.u32; |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 1361 | } else { |
| 1362 | 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] | 1363 | } |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 1364 | return retval; |
| 1365 | } |
| 1366 | |
| 1367 | /** |
| 1368 | * Retrieves an unsigned 16-bit integer from an object attribute |
| 1369 | * |
| 1370 | * @param device a pointer to an MTP device. |
| 1371 | * @param object_id Object reference |
| 1372 | * @param attribute_id PTP attribute ID |
Linus Walleij | 5acfa74 | 2006-05-29 14:51:59 +0000 | [diff] [blame] | 1373 | * @param value_default Default value to return on failure |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 1374 | * @return a value |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 1375 | */ |
Linus Walleij | 9901e22 | 2006-11-30 12:28:19 +0000 | [diff] [blame] | 1376 | 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] | 1377 | uint16_t const attribute_id, uint16_t const value_default) |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 1378 | { |
| 1379 | PTPPropertyValue propval; |
| 1380 | uint16_t retval = value_default; |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 1381 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 1382 | uint16_t ret; |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 1383 | MTPProperties *prop; |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 1384 | |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 1385 | if ( device == NULL ) { |
| 1386 | return value_default; |
| 1387 | } |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 1388 | |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 1389 | // This O(n) search should not be used so often, since code |
| 1390 | // using the cached properties don't usually call this function. |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 1391 | prop = ptp_find_object_prop_in_cache(params, object_id, attribute_id); |
| 1392 | if (prop) |
| 1393 | return prop->propval.u16; |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 1394 | |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 1395 | ret = ptp_mtp_getobjectpropvalue(params, object_id, |
| 1396 | attribute_id, |
| 1397 | &propval, |
| 1398 | PTP_DTC_UINT16); |
| 1399 | if (ret == PTP_RC_OK) { |
| 1400 | retval = propval.u16; |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 1401 | } else { |
| 1402 | 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] | 1403 | } |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 1404 | |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 1405 | return retval; |
| 1406 | } |
| 1407 | |
| 1408 | /** |
Linus Walleij | 99310d4 | 2006-11-01 08:29:39 +0000 | [diff] [blame] | 1409 | * Retrieves an unsigned 8-bit integer from an object attribute |
| 1410 | * |
| 1411 | * @param device a pointer to an MTP device. |
| 1412 | * @param object_id Object reference |
| 1413 | * @param attribute_id PTP attribute ID |
| 1414 | * @param value_default Default value to return on failure |
| 1415 | * @return a value |
| 1416 | */ |
Linus Walleij | 9901e22 | 2006-11-30 12:28:19 +0000 | [diff] [blame] | 1417 | 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] | 1418 | uint16_t const attribute_id, uint8_t const value_default) |
| 1419 | { |
| 1420 | PTPPropertyValue propval; |
| 1421 | uint8_t retval = value_default; |
| 1422 | PTPParams *params = (PTPParams *) device->params; |
| 1423 | uint16_t ret; |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 1424 | MTPProperties *prop; |
Linus Walleij | 99310d4 | 2006-11-01 08:29:39 +0000 | [diff] [blame] | 1425 | |
| 1426 | if ( device == NULL ) { |
| 1427 | return value_default; |
| 1428 | } |
| 1429 | |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 1430 | // This O(n) search should not be used so often, since code |
| 1431 | // using the cached properties don't usually call this function. |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 1432 | prop = ptp_find_object_prop_in_cache(params, object_id, attribute_id); |
| 1433 | if (prop) |
| 1434 | return prop->propval.u8; |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 1435 | |
Linus Walleij | 99310d4 | 2006-11-01 08:29:39 +0000 | [diff] [blame] | 1436 | ret = ptp_mtp_getobjectpropvalue(params, object_id, |
| 1437 | attribute_id, |
| 1438 | &propval, |
| 1439 | PTP_DTC_UINT8); |
| 1440 | if (ret == PTP_RC_OK) { |
| 1441 | retval = propval.u8; |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 1442 | } else { |
| 1443 | 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] | 1444 | } |
| 1445 | |
| 1446 | return retval; |
| 1447 | } |
| 1448 | |
| 1449 | /** |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 1450 | * Sets an object attribute from a string |
| 1451 | * |
| 1452 | * @param device a pointer to an MTP device. |
| 1453 | * @param object_id Object reference |
| 1454 | * @param attribute_id PTP attribute ID |
| 1455 | * @param string string value to set |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 1456 | * @return 0 on success, any other value means failure |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 1457 | */ |
Linus Walleij | 9901e22 | 2006-11-30 12:28:19 +0000 | [diff] [blame] | 1458 | 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] | 1459 | uint16_t const attribute_id, char const * const string) |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 1460 | { |
| 1461 | PTPPropertyValue propval; |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 1462 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 1463 | uint16_t ret; |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 1464 | |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 1465 | if (device == NULL || string == NULL) { |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 1466 | return -1; |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 1467 | } |
Linus Walleij | b275318 | 2007-02-26 08:11:38 +0000 | [diff] [blame] | 1468 | |
| 1469 | if (!ptp_operation_issupported(params,PTP_OC_MTP_SetObjectPropValue)) { |
| 1470 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "set_object_string(): could not set object string: " |
| 1471 | "PTP_OC_MTP_SetObjectPropValue not supported."); |
| 1472 | return -1; |
| 1473 | } |
Linus Walleij | a823a70 | 2006-08-27 21:27:46 +0000 | [diff] [blame] | 1474 | propval.str = (char *) string; |
| 1475 | 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] | 1476 | if (ret != PTP_RC_OK) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 1477 | 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] | 1478 | return -1; |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 1479 | } |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 1480 | |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 1481 | return 0; |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 1482 | } |
| 1483 | |
Linus Walleij | 2955956 | 2007-08-22 21:38:04 +0000 | [diff] [blame] | 1484 | |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 1485 | /** |
| 1486 | * Sets an object attribute from an unsigned 32-bit integer |
| 1487 | * |
| 1488 | * @param device a pointer to an MTP device. |
| 1489 | * @param object_id Object reference |
| 1490 | * @param attribute_id PTP attribute ID |
| 1491 | * @param value 32-bit unsigned integer to set |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 1492 | * @return 0 on success, any other value means failure |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 1493 | */ |
Linus Walleij | 9901e22 | 2006-11-30 12:28:19 +0000 | [diff] [blame] | 1494 | 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] | 1495 | uint16_t const attribute_id, uint32_t const value) |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 1496 | { |
| 1497 | PTPPropertyValue propval; |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 1498 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 1499 | uint16_t ret; |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 1500 | |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 1501 | if (device == NULL) { |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 1502 | return -1; |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 1503 | } |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 1504 | |
Linus Walleij | b275318 | 2007-02-26 08:11:38 +0000 | [diff] [blame] | 1505 | if (!ptp_operation_issupported(params,PTP_OC_MTP_SetObjectPropValue)) { |
| 1506 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "set_object_u32(): could not set unsigned 32bit integer property: " |
| 1507 | "PTP_OC_MTP_SetObjectPropValue not supported."); |
| 1508 | return -1; |
| 1509 | } |
Linus Walleij | 2955956 | 2007-08-22 21:38:04 +0000 | [diff] [blame] | 1510 | |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 1511 | propval.u32 = value; |
| 1512 | 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] | 1513 | if (ret != PTP_RC_OK) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 1514 | 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] | 1515 | return -1; |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 1516 | } |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 1517 | |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 1518 | return 0; |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 1519 | } |
| 1520 | |
| 1521 | /** |
| 1522 | * Sets an object attribute from an unsigned 16-bit integer |
| 1523 | * |
| 1524 | * @param device a pointer to an MTP device. |
| 1525 | * @param object_id Object reference |
| 1526 | * @param attribute_id PTP attribute ID |
| 1527 | * @param value 16-bit unsigned integer to set |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 1528 | * @return 0 on success, any other value means failure |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 1529 | */ |
Linus Walleij | 9901e22 | 2006-11-30 12:28:19 +0000 | [diff] [blame] | 1530 | 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] | 1531 | uint16_t const attribute_id, uint16_t const value) |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 1532 | { |
| 1533 | PTPPropertyValue propval; |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 1534 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 1535 | uint16_t ret; |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 1536 | |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 1537 | if (device == NULL) { |
| 1538 | return 1; |
| 1539 | } |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 1540 | |
Linus Walleij | b275318 | 2007-02-26 08:11:38 +0000 | [diff] [blame] | 1541 | if (!ptp_operation_issupported(params,PTP_OC_MTP_SetObjectPropValue)) { |
| 1542 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "set_object_u16(): could not set unsigned 16bit integer property: " |
| 1543 | "PTP_OC_MTP_SetObjectPropValue not supported."); |
| 1544 | return -1; |
| 1545 | } |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 1546 | propval.u16 = value; |
| 1547 | 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] | 1548 | if (ret != PTP_RC_OK) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 1549 | 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] | 1550 | return 1; |
| 1551 | } |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 1552 | |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 1553 | return 0; |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 1554 | } |
| 1555 | |
| 1556 | /** |
Linus Walleij | 99310d4 | 2006-11-01 08:29:39 +0000 | [diff] [blame] | 1557 | * Sets an object attribute from an unsigned 8-bit integer |
| 1558 | * |
| 1559 | * @param device a pointer to an MTP device. |
| 1560 | * @param object_id Object reference |
| 1561 | * @param attribute_id PTP attribute ID |
| 1562 | * @param value 8-bit unsigned integer to set |
| 1563 | * @return 0 on success, any other value means failure |
| 1564 | */ |
Linus Walleij | 9901e22 | 2006-11-30 12:28:19 +0000 | [diff] [blame] | 1565 | static int set_object_u8(LIBMTP_mtpdevice_t *device, uint32_t const object_id, |
| 1566 | uint16_t const attribute_id, uint8_t const value) |
Linus Walleij | 99310d4 | 2006-11-01 08:29:39 +0000 | [diff] [blame] | 1567 | { |
| 1568 | PTPPropertyValue propval; |
| 1569 | PTPParams *params = (PTPParams *) device->params; |
| 1570 | uint16_t ret; |
| 1571 | |
| 1572 | if (device == NULL) { |
| 1573 | return 1; |
| 1574 | } |
| 1575 | |
Linus Walleij | b275318 | 2007-02-26 08:11:38 +0000 | [diff] [blame] | 1576 | if (!ptp_operation_issupported(params,PTP_OC_MTP_SetObjectPropValue)) { |
| 1577 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "set_object_u8(): could not set unsigned 8bit integer property: " |
| 1578 | "PTP_OC_MTP_SetObjectPropValue not supported."); |
| 1579 | return -1; |
| 1580 | } |
Linus Walleij | 99310d4 | 2006-11-01 08:29:39 +0000 | [diff] [blame] | 1581 | propval.u8 = value; |
| 1582 | ret = ptp_mtp_setobjectpropvalue(params, object_id, attribute_id, &propval, PTP_DTC_UINT8); |
| 1583 | if (ret != PTP_RC_OK) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 1584 | 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] | 1585 | return 1; |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 1586 | } |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 1587 | |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 1588 | return 0; |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 1589 | } |
| 1590 | |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 1591 | /** |
Linus Walleij | 039d1dd | 2007-02-23 22:34:07 +0000 | [diff] [blame] | 1592 | * 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] | 1593 | * @return a device pointer. |
Linus Walleij | 039d1dd | 2007-02-23 22:34:07 +0000 | [diff] [blame] | 1594 | * @see LIBMTP_Get_Connected_Devices() |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 1595 | */ |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 1596 | LIBMTP_mtpdevice_t *LIBMTP_Get_First_Device(void) |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 1597 | { |
tedbullock | e771364 | 2007-02-18 23:10:09 +0000 | [diff] [blame] | 1598 | LIBMTP_mtpdevice_t *first_device = NULL; |
Linus Walleij | 64691b7 | 2008-05-31 22:55:18 +0000 | [diff] [blame] | 1599 | LIBMTP_raw_device_t *devices; |
| 1600 | int numdevs; |
| 1601 | LIBMTP_error_number_t ret; |
tedbullock | e771364 | 2007-02-18 23:10:09 +0000 | [diff] [blame] | 1602 | |
Linus Walleij | 64691b7 | 2008-05-31 22:55:18 +0000 | [diff] [blame] | 1603 | ret = LIBMTP_Detect_Raw_Devices(&devices, &numdevs); |
| 1604 | if (ret != LIBMTP_ERROR_NONE) { |
Richard Low | 0f79476 | 2007-02-23 22:06:27 +0000 | [diff] [blame] | 1605 | return NULL; |
Linus Walleij | a8a19cc | 2007-02-02 22:13:17 +0000 | [diff] [blame] | 1606 | } |
tedbullock | 44b0ff7 | 2007-02-22 22:20:28 +0000 | [diff] [blame] | 1607 | |
Linus Walleij | 64691b7 | 2008-05-31 22:55:18 +0000 | [diff] [blame] | 1608 | if (devices == NULL || numdevs == 0) { |
| 1609 | return NULL; |
tedbullock | 44b0ff7 | 2007-02-22 22:20:28 +0000 | [diff] [blame] | 1610 | } |
Linus Walleij | 64691b7 | 2008-05-31 22:55:18 +0000 | [diff] [blame] | 1611 | |
| 1612 | first_device = LIBMTP_Open_Raw_Device(&devices[0]); |
| 1613 | free(devices); |
tedbullock | e771364 | 2007-02-18 23:10:09 +0000 | [diff] [blame] | 1614 | return first_device; |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 1615 | } |
| 1616 | |
| 1617 | /** |
Linus Walleij | d1e14e0 | 2008-12-14 01:07:30 +0000 | [diff] [blame] | 1618 | * Overriding debug function. |
| 1619 | * This way we can disable debug prints. |
| 1620 | */ |
| 1621 | static void |
| 1622 | #ifdef __GNUC__ |
| 1623 | __attribute__((__format__(printf,2,0))) |
| 1624 | #endif |
| 1625 | LIBMTP_ptp_debug(void *data, const char *format, va_list args) |
| 1626 | { |
| 1627 | #ifdef ENABLE_PTP_DEBUG |
| 1628 | vfprintf (stderr, format, args); |
| 1629 | fflush (stderr); |
| 1630 | #endif |
| 1631 | } |
| 1632 | |
| 1633 | /** |
| 1634 | * Overriding error function. |
| 1635 | * This way we can capture all error etc to our errorstack. |
| 1636 | */ |
| 1637 | static void |
| 1638 | #ifdef __GNUC__ |
| 1639 | __attribute__((__format__(printf,2,0))) |
| 1640 | #endif |
| 1641 | LIBMTP_ptp_error(void *data, const char *format, va_list args) |
| 1642 | { |
| 1643 | // if (data == NULL) { |
| 1644 | vfprintf (stderr, format, args); |
| 1645 | fflush (stderr); |
| 1646 | /* |
| 1647 | FIXME: find out how we shall get the device here. |
| 1648 | } else { |
| 1649 | PTP_USB *ptp_usb = data; |
| 1650 | LIBMTP_mtpdevice_t *device = ...; |
| 1651 | char buf[2048]; |
| 1652 | |
| 1653 | vsnprintf (buf, sizeof (buf), format, args); |
| 1654 | add_error_to_errorstack(device, |
| 1655 | LIBMTP_ERROR_PTP_LAYER, |
| 1656 | buf); |
| 1657 | } |
| 1658 | */ |
| 1659 | } |
| 1660 | |
| 1661 | /** |
Linus Walleij | bdb89bd | 2008-05-28 23:32:35 +0000 | [diff] [blame] | 1662 | * This function opens a device from a raw device. It is the |
| 1663 | * preferred way to access devices in the new interface where |
| 1664 | * several devices can come and go as the library is working |
| 1665 | * on a certain device. |
| 1666 | * @param rawdevice the raw device to open a "real" device for. |
| 1667 | * @return an open device. |
| 1668 | */ |
| 1669 | LIBMTP_mtpdevice_t *LIBMTP_Open_Raw_Device(LIBMTP_raw_device_t *rawdevice) |
| 1670 | { |
| 1671 | LIBMTP_mtpdevice_t *mtp_device; |
| 1672 | uint8_t bs = 0; |
| 1673 | PTPParams *current_params; |
Linus Walleij | 3c64325 | 2008-05-31 23:22:28 +0000 | [diff] [blame] | 1674 | PTP_USB *ptp_usb; |
Linus Walleij | bdb89bd | 2008-05-28 23:32:35 +0000 | [diff] [blame] | 1675 | LIBMTP_error_number_t err; |
| 1676 | int i; |
| 1677 | |
| 1678 | /* Allocate dynamic space for our device */ |
| 1679 | mtp_device = (LIBMTP_mtpdevice_t *) malloc(sizeof(LIBMTP_mtpdevice_t)); |
Richard Low | ef19731 | 2008-11-01 18:29:41 +0000 | [diff] [blame] | 1680 | memset(mtp_device, 0, sizeof(LIBMTP_mtpdevice_t)); |
Linus Walleij | bdb89bd | 2008-05-28 23:32:35 +0000 | [diff] [blame] | 1681 | /* Check if there was a memory allocation error */ |
| 1682 | if(mtp_device == NULL) { |
| 1683 | /* There has been an memory allocation error. We are going to ignore this |
| 1684 | device and attempt to continue */ |
| 1685 | |
| 1686 | /* TODO: This error statement could probably be a bit more robust */ |
| 1687 | fprintf(stderr, "LIBMTP PANIC: connect_usb_devices encountered a memory " |
| 1688 | "allocation error with device %d on bus %d, trying to continue", |
| 1689 | rawdevice->devnum, rawdevice->bus_location); |
| 1690 | |
| 1691 | return NULL; |
| 1692 | } |
Linus Walleij | 3c64325 | 2008-05-31 23:22:28 +0000 | [diff] [blame] | 1693 | |
| 1694 | /* Create PTP params */ |
| 1695 | current_params = (PTPParams *) malloc(sizeof(PTPParams)); |
| 1696 | if (current_params == NULL) { |
| 1697 | free(mtp_device); |
Linus Walleij | bdb89bd | 2008-05-28 23:32:35 +0000 | [diff] [blame] | 1698 | return NULL; |
Linus Walleij | 3c64325 | 2008-05-31 23:22:28 +0000 | [diff] [blame] | 1699 | } |
| 1700 | memset(current_params, 0, sizeof(PTPParams)); |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 1701 | current_params->device_flags = rawdevice->device_entry.device_flags; |
| 1702 | current_params->nrofobjects = 0; |
| 1703 | current_params->objects = NULL; |
| 1704 | current_params->response_packet_size = 0; |
| 1705 | current_params->response_packet = NULL; |
Linus Walleij | d1e14e0 | 2008-12-14 01:07:30 +0000 | [diff] [blame] | 1706 | /* This will be a pointer to PTP_USB later */ |
| 1707 | current_params->data = NULL; |
| 1708 | /* Set upp local debug and error functions */ |
| 1709 | current_params->debug_func = LIBMTP_ptp_debug; |
| 1710 | current_params->error_func = LIBMTP_ptp_error; |
Linus Walleij | 3c64325 | 2008-05-31 23:22:28 +0000 | [diff] [blame] | 1711 | /* TODO: Will this always be little endian? */ |
| 1712 | current_params->byteorder = PTP_DL_LE; |
| 1713 | current_params->cd_locale_to_ucs2 = iconv_open("UCS-2LE", "UTF-8"); |
| 1714 | current_params->cd_ucs2_to_locale = iconv_open("UTF-8", "UCS-2LE"); |
| 1715 | |
| 1716 | if(current_params->cd_locale_to_ucs2 == (iconv_t) -1 || |
| 1717 | current_params->cd_ucs2_to_locale == (iconv_t) -1) { |
| 1718 | fprintf(stderr, "LIBMTP PANIC: Cannot open iconv() converters to/from UCS-2!\n" |
| 1719 | "Too old stdlibc, glibc and libiconv?\n"); |
| 1720 | free(current_params); |
| 1721 | free(mtp_device); |
| 1722 | return NULL; |
| 1723 | } |
| 1724 | mtp_device->params = current_params; |
| 1725 | |
| 1726 | |
| 1727 | /* Create usbinfo, this also opens the session */ |
| 1728 | err = configure_usb_device(rawdevice, |
| 1729 | current_params, |
| 1730 | &mtp_device->usbinfo); |
| 1731 | if (err != LIBMTP_ERROR_NONE) { |
| 1732 | free(current_params); |
| 1733 | free(mtp_device); |
| 1734 | return NULL; |
| 1735 | } |
| 1736 | ptp_usb = (PTP_USB*) mtp_device->usbinfo; |
| 1737 | /* Set pointer back to params */ |
| 1738 | ptp_usb->params = current_params; |
| 1739 | |
Linus Walleij | bdb89bd | 2008-05-28 23:32:35 +0000 | [diff] [blame] | 1740 | |
| 1741 | /* Cache the device information for later use */ |
| 1742 | if (ptp_getdeviceinfo(current_params, |
| 1743 | ¤t_params->deviceinfo) != PTP_RC_OK) { |
| 1744 | fprintf(stderr, "LIBMTP PANIC: Unable to read device information on device " |
| 1745 | "%d on bus %d, trying to continue", |
| 1746 | rawdevice->devnum, rawdevice->bus_location); |
| 1747 | |
| 1748 | /* Prevent memory leaks for this device */ |
| 1749 | free(mtp_device->usbinfo); |
| 1750 | free(mtp_device->params); |
| 1751 | current_params = NULL; |
Linus Walleij | 3c64325 | 2008-05-31 23:22:28 +0000 | [diff] [blame] | 1752 | free(mtp_device); |
Linus Walleij | bdb89bd | 2008-05-28 23:32:35 +0000 | [diff] [blame] | 1753 | return NULL; |
| 1754 | } |
| 1755 | |
| 1756 | /* Determine if the object size supported is 32 or 64 bit wide */ |
| 1757 | for (i=0;i<current_params->deviceinfo.ImageFormats_len;i++) { |
| 1758 | PTPObjectPropDesc opd; |
| 1759 | |
| 1760 | if (ptp_mtp_getobjectpropdesc(current_params, |
| 1761 | PTP_OPC_ObjectSize, |
| 1762 | current_params->deviceinfo.ImageFormats[i], |
| 1763 | &opd) != PTP_RC_OK) { |
Linus Walleij | d1e14e0 | 2008-12-14 01:07:30 +0000 | [diff] [blame] | 1764 | printf("LIBMTP PANIC: " |
Linus Walleij | bdb89bd | 2008-05-28 23:32:35 +0000 | [diff] [blame] | 1765 | "could not inspect object property descriptions!\n"); |
| 1766 | } else { |
| 1767 | if (opd.DataType == PTP_DTC_UINT32) { |
| 1768 | if (bs == 0) { |
| 1769 | bs = 32; |
| 1770 | } else if (bs != 32) { |
Linus Walleij | d1e14e0 | 2008-12-14 01:07:30 +0000 | [diff] [blame] | 1771 | printf("LIBMTP PANIC: " |
Linus Walleij | bdb89bd | 2008-05-28 23:32:35 +0000 | [diff] [blame] | 1772 | "different objects support different object sizes!\n"); |
| 1773 | bs = 0; |
| 1774 | break; |
| 1775 | } |
| 1776 | } else if (opd.DataType == PTP_DTC_UINT64) { |
| 1777 | if (bs == 0) { |
| 1778 | bs = 64; |
| 1779 | } else if (bs != 64) { |
Linus Walleij | d1e14e0 | 2008-12-14 01:07:30 +0000 | [diff] [blame] | 1780 | printf("LIBMTP PANIC: " |
Linus Walleij | bdb89bd | 2008-05-28 23:32:35 +0000 | [diff] [blame] | 1781 | "different objects support different object sizes!\n"); |
| 1782 | bs = 0; |
| 1783 | break; |
| 1784 | } |
| 1785 | } else { |
| 1786 | // Ignore if other size. |
Linus Walleij | d1e14e0 | 2008-12-14 01:07:30 +0000 | [diff] [blame] | 1787 | printf("LIBMTP PANIC: " |
Linus Walleij | bdb89bd | 2008-05-28 23:32:35 +0000 | [diff] [blame] | 1788 | "awkward object size data type: %04x\n", opd.DataType); |
| 1789 | bs = 0; |
| 1790 | break; |
| 1791 | } |
| 1792 | } |
| 1793 | } |
| 1794 | if (bs == 0) { |
| 1795 | // Could not detect object bitsize, assume 32 bits |
| 1796 | bs = 32; |
| 1797 | } |
| 1798 | mtp_device->object_bitsize = bs; |
| 1799 | |
| 1800 | /* No Errors yet for this device */ |
| 1801 | mtp_device->errorstack = NULL; |
| 1802 | |
| 1803 | /* Default Max Battery Level, we will adjust this if possible */ |
| 1804 | mtp_device->maximum_battery_level = 100; |
| 1805 | |
| 1806 | /* Check if device supports reading maximum battery level */ |
Linus Walleij | 4096c88 | 2009-03-16 23:32:34 +0000 | [diff] [blame] | 1807 | if(!FLAG_BROKEN_BATTERY_LEVEL(ptp_usb) && |
| 1808 | ptp_property_issupported( current_params, PTP_DPC_BatteryLevel)) { |
Linus Walleij | bdb89bd | 2008-05-28 23:32:35 +0000 | [diff] [blame] | 1809 | PTPDevicePropDesc dpd; |
| 1810 | |
| 1811 | /* Try to read maximum battery level */ |
| 1812 | if(ptp_getdevicepropdesc(current_params, |
| 1813 | PTP_DPC_BatteryLevel, |
| 1814 | &dpd) != PTP_RC_OK) { |
| 1815 | add_error_to_errorstack(mtp_device, |
| 1816 | LIBMTP_ERROR_CONNECTING, |
| 1817 | "Unable to read Maximum Battery Level for this " |
| 1818 | "device even though the device supposedly " |
| 1819 | "supports this functionality"); |
| 1820 | } |
| 1821 | |
| 1822 | /* TODO: is this appropriate? */ |
| 1823 | /* If max battery level is 0 then leave the default, otherwise assign */ |
| 1824 | if (dpd.FORM.Range.MaximumValue.u8 != 0) { |
| 1825 | mtp_device->maximum_battery_level = dpd.FORM.Range.MaximumValue.u8; |
| 1826 | } |
| 1827 | |
| 1828 | ptp_free_devicepropdesc(&dpd); |
| 1829 | } |
| 1830 | |
| 1831 | /* Set all default folders to 0 (root directory) */ |
| 1832 | mtp_device->default_music_folder = 0; |
| 1833 | mtp_device->default_playlist_folder = 0; |
| 1834 | mtp_device->default_picture_folder = 0; |
| 1835 | mtp_device->default_video_folder = 0; |
| 1836 | mtp_device->default_organizer_folder = 0; |
| 1837 | mtp_device->default_zencast_folder = 0; |
| 1838 | mtp_device->default_album_folder = 0; |
| 1839 | mtp_device->default_text_folder = 0; |
| 1840 | |
| 1841 | /* Set initial storage information */ |
| 1842 | mtp_device->storage = NULL; |
| 1843 | if (LIBMTP_Get_Storage(mtp_device, LIBMTP_STORAGE_SORTBY_NOTSORTED) == -1) { |
| 1844 | add_error_to_errorstack(mtp_device, |
| 1845 | LIBMTP_ERROR_GENERAL, |
| 1846 | "Get Storage information failed."); |
| 1847 | mtp_device->storage = NULL; |
| 1848 | } |
| 1849 | |
| 1850 | /* |
| 1851 | * Then get the handles and try to locate the default folders. |
| 1852 | * This has the desired side effect of caching all handles from |
| 1853 | * the device which speeds up later operations. |
| 1854 | */ |
| 1855 | flush_handles(mtp_device); |
| 1856 | |
| 1857 | return mtp_device; |
| 1858 | } |
| 1859 | |
| 1860 | /** |
Linus Walleij | 2d3f7b8 | 2007-02-14 09:24:20 +0000 | [diff] [blame] | 1861 | * Recursive function that adds MTP devices to a linked list |
Linus Walleij | a700d22 | 2008-05-28 23:06:14 +0000 | [diff] [blame] | 1862 | * @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] | 1863 | * @return a device pointer to a newly created mtpdevice (used in linked |
Linus Walleij | b0ab548 | 2007-08-29 21:08:54 +0000 | [diff] [blame] | 1864 | * list creation). |
Linus Walleij | 2d3f7b8 | 2007-02-14 09:24:20 +0000 | [diff] [blame] | 1865 | */ |
Linus Walleij | a700d22 | 2008-05-28 23:06:14 +0000 | [diff] [blame] | 1866 | 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] | 1867 | { |
Linus Walleij | a700d22 | 2008-05-28 23:06:14 +0000 | [diff] [blame] | 1868 | uint8_t i; |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 1869 | LIBMTP_mtpdevice_t *mtp_device_list = NULL; |
| 1870 | LIBMTP_mtpdevice_t *current_device = NULL; |
Linus Walleij | a700d22 | 2008-05-28 23:06:14 +0000 | [diff] [blame] | 1871 | |
| 1872 | for (i=0; i < numdevs; i++) { |
Linus Walleij | 2d3f7b8 | 2007-02-14 09:24:20 +0000 | [diff] [blame] | 1873 | LIBMTP_mtpdevice_t *mtp_device; |
Linus Walleij | bdb89bd | 2008-05-28 23:32:35 +0000 | [diff] [blame] | 1874 | mtp_device = LIBMTP_Open_Raw_Device(&devices[i]); |
Linus Walleij | a700d22 | 2008-05-28 23:06:14 +0000 | [diff] [blame] | 1875 | |
Linus Walleij | bdb89bd | 2008-05-28 23:32:35 +0000 | [diff] [blame] | 1876 | /* On error, try next device */ |
| 1877 | if (mtp_device == NULL) |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 1878 | continue; |
Linus Walleij | ddaba2f | 2007-10-02 21:20:30 +0000 | [diff] [blame] | 1879 | |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 1880 | /* Add the device to the list */ |
| 1881 | mtp_device->next = NULL; |
| 1882 | if (mtp_device_list == NULL) { |
| 1883 | mtp_device_list = current_device = mtp_device; |
| 1884 | } else { |
| 1885 | current_device->next = mtp_device; |
| 1886 | current_device = mtp_device; |
| 1887 | } |
Linus Walleij | 2d3f7b8 | 2007-02-14 09:24:20 +0000 | [diff] [blame] | 1888 | } |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 1889 | return mtp_device_list; |
Linus Walleij | 2d3f7b8 | 2007-02-14 09:24:20 +0000 | [diff] [blame] | 1890 | } |
| 1891 | |
| 1892 | /** |
tedbullock | 848009b | 2007-03-03 23:20:12 +0000 | [diff] [blame] | 1893 | * 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] | 1894 | * @param device_list Pointer to a linked list of devices |
| 1895 | * @return Number of devices in the device list device_list |
| 1896 | * @see LIBMTP_Get_Connected_Devices() |
tedbullock | 848009b | 2007-03-03 23:20:12 +0000 | [diff] [blame] | 1897 | */ |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 1898 | uint32_t LIBMTP_Number_Devices_In_List(LIBMTP_mtpdevice_t *device_list) |
tedbullock | 848009b | 2007-03-03 23:20:12 +0000 | [diff] [blame] | 1899 | { |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 1900 | uint32_t numdevices = 0; |
| 1901 | LIBMTP_mtpdevice_t *iter; |
| 1902 | for(iter = device_list; iter != NULL; iter = iter->next) |
| 1903 | numdevices++; |
| 1904 | |
| 1905 | return numdevices; |
tedbullock | 848009b | 2007-03-03 23:20:12 +0000 | [diff] [blame] | 1906 | } |
| 1907 | |
| 1908 | /** |
Linus Walleij | 2d3f7b8 | 2007-02-14 09:24:20 +0000 | [diff] [blame] | 1909 | * Get the first connected MTP device node in the linked list of devices. |
| 1910 | * Currently this only provides access to USB devices |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 1911 | * @param device_list A list of devices ready to be used by the caller. You |
| 1912 | * need to know how many there are. |
Linus Walleij | 2d3f7b8 | 2007-02-14 09:24:20 +0000 | [diff] [blame] | 1913 | * @return Any error information gathered from device connections |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 1914 | * @see LIBMTP_Number_Devices_In_List() |
Linus Walleij | 2d3f7b8 | 2007-02-14 09:24:20 +0000 | [diff] [blame] | 1915 | */ |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 1916 | 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] | 1917 | { |
Linus Walleij | a700d22 | 2008-05-28 23:06:14 +0000 | [diff] [blame] | 1918 | LIBMTP_raw_device_t *devices; |
| 1919 | int numdevs; |
Linus Walleij | 5fbb77d | 2007-03-07 08:40:36 +0000 | [diff] [blame] | 1920 | LIBMTP_error_number_t ret; |
Linus Walleij | a700d22 | 2008-05-28 23:06:14 +0000 | [diff] [blame] | 1921 | |
| 1922 | ret = LIBMTP_Detect_Raw_Devices(&devices, &numdevs); |
Linus Walleij | 5fbb77d | 2007-03-07 08:40:36 +0000 | [diff] [blame] | 1923 | if (ret != LIBMTP_ERROR_NONE) { |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 1924 | *device_list = NULL; |
Linus Walleij | 5fbb77d | 2007-03-07 08:40:36 +0000 | [diff] [blame] | 1925 | return ret; |
Linus Walleij | 2d3f7b8 | 2007-02-14 09:24:20 +0000 | [diff] [blame] | 1926 | } |
| 1927 | |
| 1928 | /* Assign linked list of devices */ |
Linus Walleij | a700d22 | 2008-05-28 23:06:14 +0000 | [diff] [blame] | 1929 | if (devices == NULL || numdevs == 0) { |
| 1930 | *device_list = NULL; |
| 1931 | return LIBMTP_ERROR_NO_DEVICE_ATTACHED; |
| 1932 | } |
| 1933 | |
| 1934 | *device_list = create_usb_mtp_devices(devices, numdevs); |
| 1935 | free(devices); |
Linus Walleij | 45a8637 | 2007-03-07 09:36:19 +0000 | [diff] [blame] | 1936 | |
Linus Walleij | 2d3f7b8 | 2007-02-14 09:24:20 +0000 | [diff] [blame] | 1937 | /* TODO: Add wifi device access here */ |
Richard Low | 99a93e8 | 2007-09-29 08:16:08 +0000 | [diff] [blame] | 1938 | |
| 1939 | /* We have found some devices but create failed */ |
| 1940 | if (*device_list == NULL) |
| 1941 | return LIBMTP_ERROR_CONNECTING; |
Linus Walleij | 2d3f7b8 | 2007-02-14 09:24:20 +0000 | [diff] [blame] | 1942 | |
| 1943 | return LIBMTP_ERROR_NONE; |
| 1944 | } |
| 1945 | |
| 1946 | /** |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 1947 | * This closes and releases an allocated MTP device. |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 1948 | * @param device a pointer to the MTP device to release. |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 1949 | */ |
tedbullock | 0f033cb | 2007-02-14 20:56:54 +0000 | [diff] [blame] | 1950 | void LIBMTP_Release_Device_List(LIBMTP_mtpdevice_t *device) |
| 1951 | { |
| 1952 | if(device != NULL) |
| 1953 | { |
| 1954 | if(device->next != NULL) |
| 1955 | { |
| 1956 | LIBMTP_Release_Device_List(device->next); |
| 1957 | } |
| 1958 | |
| 1959 | LIBMTP_Release_Device(device); |
| 1960 | } |
| 1961 | } |
| 1962 | |
| 1963 | /** |
| 1964 | * This closes and releases an allocated MTP device. |
| 1965 | * @param device a pointer to the MTP device to release. |
| 1966 | */ |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 1967 | void LIBMTP_Release_Device(LIBMTP_mtpdevice_t *device) |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 1968 | { |
Linus Walleij | 9b28da3 | 2006-03-16 13:47:58 +0000 | [diff] [blame] | 1969 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | 2d411db | 2006-03-22 12:13:09 +0000 | [diff] [blame] | 1970 | PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo; |
Linus Walleij | 9b28da3 | 2006-03-16 13:47:58 +0000 | [diff] [blame] | 1971 | |
Linus Walleij | b0ab548 | 2007-08-29 21:08:54 +0000 | [diff] [blame] | 1972 | close_device(ptp_usb, params); |
Linus Walleij | 2715c44 | 2007-01-20 22:35:29 +0000 | [diff] [blame] | 1973 | // Clear error stack |
| 1974 | LIBMTP_Clear_Errorstack(device); |
Linus Walleij | 3ec8631 | 2006-08-21 13:25:24 +0000 | [diff] [blame] | 1975 | // Free iconv() converters... |
Linus Walleij | a823a70 | 2006-08-27 21:27:46 +0000 | [diff] [blame] | 1976 | iconv_close(params->cd_locale_to_ucs2); |
| 1977 | iconv_close(params->cd_ucs2_to_locale); |
tedbullock | 69a445b | 2007-02-15 07:41:04 +0000 | [diff] [blame] | 1978 | free(ptp_usb); |
Linus Walleij | 073c417 | 2007-02-02 22:26:33 +0000 | [diff] [blame] | 1979 | ptp_free_params(params); |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 1980 | free_storage_list(device); |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 1981 | free(device); |
| 1982 | } |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 1983 | |
| 1984 | /** |
Linus Walleij | 2715c44 | 2007-01-20 22:35:29 +0000 | [diff] [blame] | 1985 | * This can be used by any libmtp-intrinsic code that |
Linus Walleij | 68b19c0 | 2007-02-15 11:50:37 +0000 | [diff] [blame] | 1986 | * need to stack up an error on the stack. You are only |
| 1987 | * supposed to add errors to the error stack using this |
| 1988 | * function, do not create and reference error entries |
| 1989 | * directly. |
Linus Walleij | 2715c44 | 2007-01-20 22:35:29 +0000 | [diff] [blame] | 1990 | */ |
| 1991 | static void add_error_to_errorstack(LIBMTP_mtpdevice_t *device, |
| 1992 | LIBMTP_error_number_t errornumber, |
| 1993 | char const * const error_text) |
| 1994 | { |
| 1995 | LIBMTP_error_t *newerror; |
| 1996 | |
Linus Walleij | 68b19c0 | 2007-02-15 11:50:37 +0000 | [diff] [blame] | 1997 | if (device == NULL) { |
| 1998 | fprintf(stderr, "LIBMTP PANIC: Trying to add error to a NULL device!\n"); |
| 1999 | return; |
| 2000 | } |
Linus Walleij | 2715c44 | 2007-01-20 22:35:29 +0000 | [diff] [blame] | 2001 | newerror = (LIBMTP_error_t *) malloc(sizeof(LIBMTP_error_t)); |
| 2002 | newerror->errornumber = errornumber; |
| 2003 | newerror->error_text = strdup(error_text); |
rreardon | 774503c | 2007-02-15 15:52:49 +0000 | [diff] [blame] | 2004 | newerror->next = NULL; |
Linus Walleij | 2715c44 | 2007-01-20 22:35:29 +0000 | [diff] [blame] | 2005 | if (device->errorstack == NULL) { |
| 2006 | device->errorstack = newerror; |
| 2007 | } else { |
| 2008 | LIBMTP_error_t *tmp = device->errorstack; |
| 2009 | |
| 2010 | while (tmp->next != NULL) { |
| 2011 | tmp = tmp->next; |
| 2012 | } |
| 2013 | tmp->next = newerror; |
| 2014 | } |
| 2015 | } |
| 2016 | |
| 2017 | /** |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 2018 | * Adds an error from the PTP layer to the error stack. |
| 2019 | */ |
| 2020 | static void add_ptp_error_to_errorstack(LIBMTP_mtpdevice_t *device, |
| 2021 | uint16_t ptp_error, |
| 2022 | char const * const error_text) |
| 2023 | { |
Linus Walleij | 68b19c0 | 2007-02-15 11:50:37 +0000 | [diff] [blame] | 2024 | if (device == NULL) { |
| 2025 | fprintf(stderr, "LIBMTP PANIC: Trying to add PTP error to a NULL device!\n"); |
| 2026 | return; |
| 2027 | } else { |
| 2028 | char outstr[256]; |
| 2029 | snprintf(outstr, sizeof(outstr), "PTP Layer error %04x: %s", ptp_error, error_text); |
| 2030 | outstr[sizeof(outstr)-1] = '\0'; |
| 2031 | add_error_to_errorstack(device, LIBMTP_ERROR_PTP_LAYER, outstr); |
| 2032 | add_error_to_errorstack(device, LIBMTP_ERROR_PTP_LAYER, "(Look this up in ptp.h for an explanation.)"); |
| 2033 | } |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 2034 | } |
| 2035 | |
| 2036 | /** |
Linus Walleij | 2715c44 | 2007-01-20 22:35:29 +0000 | [diff] [blame] | 2037 | * This returns the error stack for a device in case you |
| 2038 | * need to either reference the error numbers (e.g. when |
| 2039 | * creating multilingual apps with multiple-language text |
| 2040 | * representations for each error number) or when you need |
| 2041 | * to build a multi-line error text widget or something like |
| 2042 | * that. You need to call the <code>LIBMTP_Clear_Errorstack</code> |
| 2043 | * to clear it when you're finished with it. |
| 2044 | * @param device a pointer to the MTP device to get the error |
| 2045 | * stack for. |
| 2046 | * @return the error stack or NULL if there are no errors |
| 2047 | * on the stack. |
| 2048 | * @see LIBMTP_Clear_Errorstack() |
| 2049 | * @see LIBMTP_Dump_Errorstack() |
| 2050 | */ |
| 2051 | LIBMTP_error_t *LIBMTP_Get_Errorstack(LIBMTP_mtpdevice_t *device) |
| 2052 | { |
Linus Walleij | 68b19c0 | 2007-02-15 11:50:37 +0000 | [diff] [blame] | 2053 | if (device == NULL) { |
| 2054 | fprintf(stderr, "LIBMTP PANIC: Trying to get the error stack of a NULL device!\n"); |
Linus Walleij | 5c1499e | 2009-02-21 06:54:29 +0000 | [diff] [blame] | 2055 | return NULL; |
Linus Walleij | 68b19c0 | 2007-02-15 11:50:37 +0000 | [diff] [blame] | 2056 | } |
Linus Walleij | 2715c44 | 2007-01-20 22:35:29 +0000 | [diff] [blame] | 2057 | return device->errorstack; |
| 2058 | } |
| 2059 | |
| 2060 | /** |
| 2061 | * This function clears the error stack of a device and frees |
| 2062 | * any memory used by it. Call this when you're finished with |
| 2063 | * using the errors. |
| 2064 | * @param device a pointer to the MTP device to clear the error |
| 2065 | * stack for. |
| 2066 | */ |
| 2067 | void LIBMTP_Clear_Errorstack(LIBMTP_mtpdevice_t *device) |
| 2068 | { |
Linus Walleij | 68b19c0 | 2007-02-15 11:50:37 +0000 | [diff] [blame] | 2069 | if (device == NULL) { |
| 2070 | fprintf(stderr, "LIBMTP PANIC: Trying to clear the error stack of a NULL device!\n"); |
| 2071 | } else { |
| 2072 | LIBMTP_error_t *tmp = device->errorstack; |
Linus Walleij | 2715c44 | 2007-01-20 22:35:29 +0000 | [diff] [blame] | 2073 | |
Linus Walleij | 68b19c0 | 2007-02-15 11:50:37 +0000 | [diff] [blame] | 2074 | while (tmp != NULL) { |
| 2075 | LIBMTP_error_t *tmp2; |
| 2076 | |
| 2077 | if (tmp->error_text != NULL) { |
| 2078 | free(tmp->error_text); |
| 2079 | } |
| 2080 | tmp2 = tmp; |
| 2081 | tmp = tmp->next; |
| 2082 | free(tmp2); |
Linus Walleij | 2715c44 | 2007-01-20 22:35:29 +0000 | [diff] [blame] | 2083 | } |
Linus Walleij | 68b19c0 | 2007-02-15 11:50:37 +0000 | [diff] [blame] | 2084 | device->errorstack = NULL; |
Linus Walleij | 2715c44 | 2007-01-20 22:35:29 +0000 | [diff] [blame] | 2085 | } |
Linus Walleij | 2715c44 | 2007-01-20 22:35:29 +0000 | [diff] [blame] | 2086 | } |
| 2087 | |
| 2088 | /** |
| 2089 | * This function dumps the error stack to <code>stderr</code>. |
| 2090 | * (You still have to clear the stack though.) |
| 2091 | * @param device a pointer to the MTP device to dump the error |
| 2092 | * stack for. |
| 2093 | */ |
| 2094 | void LIBMTP_Dump_Errorstack(LIBMTP_mtpdevice_t *device) |
| 2095 | { |
Linus Walleij | 68b19c0 | 2007-02-15 11:50:37 +0000 | [diff] [blame] | 2096 | if (device == NULL) { |
| 2097 | fprintf(stderr, "LIBMTP PANIC: Trying to dump the error stack of a NULL device!\n"); |
| 2098 | } else { |
| 2099 | LIBMTP_error_t *tmp = device->errorstack; |
Linus Walleij | 2715c44 | 2007-01-20 22:35:29 +0000 | [diff] [blame] | 2100 | |
Linus Walleij | 68b19c0 | 2007-02-15 11:50:37 +0000 | [diff] [blame] | 2101 | while (tmp != NULL) { |
| 2102 | if (tmp->error_text != NULL) { |
| 2103 | fprintf(stderr, "Error %d: %s\n", tmp->errornumber, tmp->error_text); |
| 2104 | } else { |
| 2105 | fprintf(stderr, "Error %d: (unknown)\n", tmp->errornumber); |
| 2106 | } |
| 2107 | tmp = tmp->next; |
Linus Walleij | 2715c44 | 2007-01-20 22:35:29 +0000 | [diff] [blame] | 2108 | } |
Linus Walleij | 2715c44 | 2007-01-20 22:35:29 +0000 | [diff] [blame] | 2109 | } |
| 2110 | } |
| 2111 | |
| 2112 | /** |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 2113 | * This command gets all handles and stuff by FAST directory retrieveal |
| 2114 | * which is available by getting all metadata for object |
| 2115 | * <code>0xffffffff</code> which simply means "all metadata for all objects". |
| 2116 | * This works on the vast majority of MTP devices (there ARE exceptions!) |
| 2117 | * and is quite quick. Check the error stack to see if there were |
| 2118 | * problems getting the metadata. |
Linus Walleij | 8533bf7 | 2007-08-28 10:03:59 +0000 | [diff] [blame] | 2119 | * @return 0 if all was OK, -1 on failure. |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 2120 | */ |
Linus Walleij | 6bf68b4 | 2007-09-03 22:50:02 +0000 | [diff] [blame] | 2121 | static int get_all_metadata_fast(LIBMTP_mtpdevice_t *device, |
| 2122 | uint32_t storage) |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 2123 | { |
| 2124 | PTPParams *params = (PTPParams *) device->params; |
| 2125 | int cnt = 0; |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 2126 | int i, j, nrofprops; |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 2127 | uint32_t lasthandle = 0xffffffff; |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 2128 | MTPProperties *props = NULL; |
| 2129 | MTPProperties *prop; |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 2130 | uint16_t ret; |
Linus Walleij | 2f62281 | 2008-08-30 22:06:58 +0000 | [diff] [blame] | 2131 | int oldtimeout; |
| 2132 | PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo; |
| 2133 | |
| 2134 | /* The follow request causes the device to generate |
| 2135 | * a list of very file on the device and return it |
| 2136 | * in a single response. |
| 2137 | * |
| 2138 | * Some slow devices as well as devices with very |
| 2139 | * large file systems can easily take longer then |
| 2140 | * the standard timeout value before it is able |
| 2141 | * to return a response. |
| 2142 | * |
| 2143 | * Temporarly set timeout to allow working with |
| 2144 | * widest range of devices. |
| 2145 | */ |
| 2146 | get_usb_device_timeout(ptp_usb, &oldtimeout); |
| 2147 | set_usb_device_timeout(ptp_usb, 60000); |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 2148 | |
Linus Walleij | fcb4342 | 2008-05-23 21:53:55 +0000 | [diff] [blame] | 2149 | ret = ptp_mtp_getobjectproplist(params, 0xffffffff, &props, &nrofprops); |
Linus Walleij | 2f62281 | 2008-08-30 22:06:58 +0000 | [diff] [blame] | 2150 | set_usb_device_timeout(ptp_usb, oldtimeout); |
Linus Walleij | 8533bf7 | 2007-08-28 10:03:59 +0000 | [diff] [blame] | 2151 | |
| 2152 | if (ret == PTP_RC_MTP_Specification_By_Group_Unsupported) { |
| 2153 | // What's the point in the device implementing this command if |
| 2154 | // you cannot use it to get all props for AT LEAST one object? |
| 2155 | // Well, whatever... |
| 2156 | add_ptp_error_to_errorstack(device, ret, "get_all_metadata_fast(): " |
| 2157 | "cannot retrieve all metadata for an object on this device."); |
Linus Walleij | 91e9813 | 2007-08-28 10:05:11 +0000 | [diff] [blame] | 2158 | return -1; |
Linus Walleij | 8533bf7 | 2007-08-28 10:03:59 +0000 | [diff] [blame] | 2159 | } |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 2160 | if (ret != PTP_RC_OK) { |
Linus Walleij | 8533bf7 | 2007-08-28 10:03:59 +0000 | [diff] [blame] | 2161 | add_ptp_error_to_errorstack(device, ret, "get_all_metadata_fast(): " |
| 2162 | "could not get proplist of all objects."); |
| 2163 | return -1; |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 2164 | } |
Linus Walleij | fcb4342 | 2008-05-23 21:53:55 +0000 | [diff] [blame] | 2165 | if (props == NULL && nrofprops != 0) { |
| 2166 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, |
| 2167 | "get_all_metadata_fast(): " |
| 2168 | "call to ptp_mtp_getobjectproplist() returned " |
| 2169 | "inconsistent results."); |
| 2170 | return -1; |
| 2171 | } |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 2172 | /* |
| 2173 | * We count the number of objects by counting the ObjectHandle |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 2174 | * references, whenever it changes we get a new object, when it's |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 2175 | * the same, it is just different properties of the same object. |
| 2176 | */ |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 2177 | prop = props; |
| 2178 | for (i=0;i<nrofprops;i++) { |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 2179 | if (lasthandle != prop->ObjectHandle) { |
| 2180 | cnt++; |
| 2181 | lasthandle = prop->ObjectHandle; |
| 2182 | } |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 2183 | prop++; |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 2184 | } |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 2185 | lasthandle = 0xffffffff; |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 2186 | params->objects = calloc (sizeof(PTPObject),cnt); |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 2187 | prop = props; |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 2188 | i = -1; |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 2189 | for (j=0;j<nrofprops;j++) { |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 2190 | if (lasthandle != prop->ObjectHandle) { |
| 2191 | if (i >= 0) { |
Linus Walleij | 41bf72c | 2009-08-25 09:33:03 +0000 | [diff] [blame^] | 2192 | params->objects[i].flags |= PTPOBJECT_MTPPROPLIST_LOADED|PTPOBJECT_OBJECTINFO_LOADED; |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 2193 | if (!params->objects[i].oi.Filename) { |
Linus Walleij | 6bf68b4 | 2007-09-03 22:50:02 +0000 | [diff] [blame] | 2194 | /* I have one such file on my Creative (Marcus) */ |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 2195 | params->objects[i].oi.Filename = strdup("<null>"); |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 2196 | } |
| 2197 | } |
| 2198 | i++; |
| 2199 | lasthandle = prop->ObjectHandle; |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 2200 | params->objects[i].oid = prop->ObjectHandle; |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 2201 | } |
| 2202 | switch (prop->property) { |
| 2203 | case PTP_OPC_ParentObject: |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 2204 | params->objects[i].oi.ParentObject = prop->propval.u32; |
| 2205 | params->objects[i].flags |= PTPOBJECT_PARENTOBJECT_LOADED; |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 2206 | break; |
| 2207 | case PTP_OPC_ObjectFormat: |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 2208 | params->objects[i].oi.ObjectFormat = prop->propval.u16; |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 2209 | break; |
| 2210 | case PTP_OPC_ObjectSize: |
Linus Walleij | d9d28d5 | 2007-08-04 19:01:18 +0000 | [diff] [blame] | 2211 | // We loose precision here, up to 32 bits! However the commands that |
| 2212 | // retrieve metadata for files and tracks will make sure that the |
| 2213 | // PTP_OPC_ObjectSize is read in and duplicated again. |
Linus Walleij | ddaba2f | 2007-10-02 21:20:30 +0000 | [diff] [blame] | 2214 | if (device->object_bitsize == 64) { |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 2215 | params->objects[i].oi.ObjectCompressedSize = (uint32_t) prop->propval.u64; |
Linus Walleij | ddaba2f | 2007-10-02 21:20:30 +0000 | [diff] [blame] | 2216 | } else { |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 2217 | params->objects[i].oi.ObjectCompressedSize = prop->propval.u32; |
Linus Walleij | ddaba2f | 2007-10-02 21:20:30 +0000 | [diff] [blame] | 2218 | } |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 2219 | break; |
| 2220 | case PTP_OPC_StorageID: |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 2221 | params->objects[i].oi.StorageID = prop->propval.u32; |
| 2222 | params->objects[i].flags |= PTPOBJECT_STORAGEID_LOADED; |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 2223 | break; |
| 2224 | case PTP_OPC_ObjectFileName: |
Richard Low | 8e8d9d4 | 2007-07-14 10:36:50 +0000 | [diff] [blame] | 2225 | if (prop->propval.str != NULL) |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 2226 | params->objects[i].oi.Filename = strdup(prop->propval.str); |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 2227 | break; |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 2228 | default: { |
| 2229 | MTPProperties *newprops; |
| 2230 | |
| 2231 | /* Copy all of the other MTP oprierties into the per-object proplist */ |
| 2232 | if (params->objects[i].nrofmtpprops) { |
| 2233 | newprops = realloc(params->objects[i].mtpprops,(params->objects[i].nrofmtpprops+1)*sizeof(MTPProperties)); |
| 2234 | } else { |
| 2235 | newprops = calloc(sizeof(MTPProperties),1); |
| 2236 | } |
| 2237 | if (!newprops) return 0; /* FIXME: error handling? */ |
| 2238 | params->objects[i].mtpprops = newprops; |
| 2239 | memcpy(¶ms->objects[i].mtpprops[params->objects[i].nrofmtpprops],&props[j],sizeof(props[j])); |
| 2240 | params->objects[i].nrofmtpprops++; |
Linus Walleij | 362d13e | 2009-08-02 19:59:21 +0000 | [diff] [blame] | 2241 | params->objects[i].flags |= PTPOBJECT_MTPPROPLIST_LOADED; |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 2242 | break; |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 2243 | } |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 2244 | } |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 2245 | prop++; |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 2246 | } |
Linus Walleij | 8533bf7 | 2007-08-28 10:03:59 +0000 | [diff] [blame] | 2247 | return 0; |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 2248 | } |
| 2249 | |
| 2250 | /** |
| 2251 | * This function will recurse through all the directories on the device, |
| 2252 | * starting at the root directory, gathering metadata as it moves along. |
| 2253 | * It works better on some devices that will only return data for a |
| 2254 | * certain directory and does not respect the option to get all metadata |
| 2255 | * for all objects. |
| 2256 | */ |
Linus Walleij | 6bf68b4 | 2007-09-03 22:50:02 +0000 | [diff] [blame] | 2257 | static void get_handles_recursively(LIBMTP_mtpdevice_t *device, |
| 2258 | PTPParams *params, |
Linus Walleij | 6bf68b4 | 2007-09-03 22:50:02 +0000 | [diff] [blame] | 2259 | uint32_t storageid, |
| 2260 | uint32_t parent) |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 2261 | { |
| 2262 | PTPObjectHandles currentHandles; |
| 2263 | int i = 0; |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 2264 | uint16_t ret = ptp_getobjecthandles(params, |
Linus Walleij | 6bf68b4 | 2007-09-03 22:50:02 +0000 | [diff] [blame] | 2265 | storageid, |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 2266 | PTP_GOH_ALL_FORMATS, |
| 2267 | parent, |
| 2268 | ¤tHandles); |
| 2269 | |
| 2270 | if (ret != PTP_RC_OK) { |
| 2271 | add_ptp_error_to_errorstack(device, ret, "get_handles_recursively(): could not get object handles."); |
| 2272 | return; |
| 2273 | } |
| 2274 | |
| 2275 | if (currentHandles.Handler == NULL || currentHandles.n == 0) |
| 2276 | return; |
| 2277 | |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 2278 | // Now descend into any subdirectories found |
| 2279 | for (i = 0; i < currentHandles.n; i++) { |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 2280 | PTPObject *ob; |
| 2281 | ret = ptp_object_want(params,currentHandles.Handler[i],PTPOBJECT_OBJECTINFO_LOADED, &ob); |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 2282 | if (ret == PTP_RC_OK) { |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 2283 | if (ob->oi.ObjectFormat == PTP_OFC_Association) |
| 2284 | get_handles_recursively(device, params, storageid, currentHandles.Handler[i]); |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 2285 | } else { |
| 2286 | add_error_to_errorstack(device, |
| 2287 | LIBMTP_ERROR_CONNECTING, |
| 2288 | "Found a bad handle, trying to ignore it."); |
| 2289 | } |
| 2290 | } |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 2291 | free(currentHandles.Handler); |
| 2292 | } |
| 2293 | |
| 2294 | /** |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 2295 | * This function refresh the internal handle list whenever |
| 2296 | * the items stored inside the device is altered. On operations |
| 2297 | * that do not add or remove objects, this is typically not |
| 2298 | * called. |
| 2299 | * @param device a pointer to the MTP device to flush handles for. |
| 2300 | */ |
| 2301 | static void flush_handles(LIBMTP_mtpdevice_t *device) |
| 2302 | { |
| 2303 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 2304 | PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo; |
Linus Walleij | 8533bf7 | 2007-08-28 10:03:59 +0000 | [diff] [blame] | 2305 | int ret; |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 2306 | uint32_t i; |
| 2307 | |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 2308 | if (params->objects != NULL) { |
| 2309 | for (i=0;i<params->nrofobjects;i++) |
| 2310 | ptp_free_object (¶ms->objects[i]); |
| 2311 | free(params->objects); |
| 2312 | params->objects = NULL; |
| 2313 | params->nrofobjects = 0; |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 2314 | } |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 2315 | |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 2316 | if (ptp_operation_issupported(params,PTP_OC_MTP_GetObjPropList) |
Linus Walleij | fec4d56 | 2008-06-01 22:30:36 +0000 | [diff] [blame] | 2317 | && !FLAG_BROKEN_MTPGETOBJPROPLIST(ptp_usb) |
| 2318 | && !FLAG_BROKEN_MTPGETOBJPROPLIST_ALL(ptp_usb)) { |
Linus Walleij | 139455e | 2007-08-28 10:25:06 +0000 | [diff] [blame] | 2319 | // Use the fast method. Ignore return value for now. |
Linus Walleij | 6bf68b4 | 2007-09-03 22:50:02 +0000 | [diff] [blame] | 2320 | ret = get_all_metadata_fast(device, PTP_GOH_ALL_STORAGE); |
Linus Walleij | 8533bf7 | 2007-08-28 10:03:59 +0000 | [diff] [blame] | 2321 | } |
Linus Walleij | 139455e | 2007-08-28 10:25:06 +0000 | [diff] [blame] | 2322 | // If the previous failed or returned no objects, use classic |
| 2323 | // methods instead. |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 2324 | if (params->nrofobjects == 0) { |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 2325 | // Get all the handles using just standard commands. |
Linus Walleij | 6bf68b4 | 2007-09-03 22:50:02 +0000 | [diff] [blame] | 2326 | if (device->storage == NULL) { |
| 2327 | get_handles_recursively(device, params, |
Linus Walleij | 6bf68b4 | 2007-09-03 22:50:02 +0000 | [diff] [blame] | 2328 | PTP_GOH_ALL_STORAGE, |
| 2329 | PTP_GOH_ROOT_PARENT); |
| 2330 | } else { |
| 2331 | // Get handles for each storage in turn. |
| 2332 | LIBMTP_devicestorage_t *storage = device->storage; |
| 2333 | while(storage != NULL) { |
| 2334 | get_handles_recursively(device, params, |
Linus Walleij | 6bf68b4 | 2007-09-03 22:50:02 +0000 | [diff] [blame] | 2335 | storage->id, |
| 2336 | PTP_GOH_ROOT_PARENT); |
| 2337 | storage = storage->next; |
| 2338 | } |
| 2339 | } |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 2340 | } |
Linus Walleij | 8f7f1aa | 2008-06-15 19:00:23 +0000 | [diff] [blame] | 2341 | |
| 2342 | /* |
| 2343 | * Loop over the handles, fix up any NULL filenames or |
| 2344 | * keywords, then attempt to locate some default folders |
| 2345 | * in the root directory of the primary storage. |
| 2346 | */ |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 2347 | for(i = 0; i < params->nrofobjects; i++) { |
| 2348 | PTPObject *ob, *xob; |
| 2349 | |
| 2350 | ob = ¶ms->objects[i]; |
| 2351 | ret = ptp_object_want(params,params->objects[i].oid,PTPOBJECT_OBJECTINFO_LOADED, &xob); |
| 2352 | if (ret != PTP_RC_OK) { |
| 2353 | fprintf(stderr,"broken! %x not found\n", params->objects[i].oid); |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 2354 | } |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 2355 | if (ob->oi.Filename == NULL) |
| 2356 | ob->oi.Filename = strdup("<null>"); |
| 2357 | if (ob->oi.Keywords == NULL) |
| 2358 | ob->oi.Keywords = strdup("<null>"); |
| 2359 | |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 2360 | /* Ignore handles that point to non-folders */ |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 2361 | if(ob->oi.ObjectFormat != PTP_OFC_Association) |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 2362 | continue; |
Linus Walleij | 8f7f1aa | 2008-06-15 19:00:23 +0000 | [diff] [blame] | 2363 | /* Only look in the root folder */ |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 2364 | if (ob->oi.ParentObject != 0x00000000U) |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 2365 | continue; |
Linus Walleij | c40c9bf | 2008-06-13 23:24:17 +0000 | [diff] [blame] | 2366 | /* Only look in the primary storage */ |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 2367 | if (device->storage != NULL && ob->oi.StorageID != device->storage->id) |
Linus Walleij | c40c9bf | 2008-06-13 23:24:17 +0000 | [diff] [blame] | 2368 | continue; |
Linus Walleij | 8f7f1aa | 2008-06-15 19:00:23 +0000 | [diff] [blame] | 2369 | |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 2370 | |
| 2371 | /* Is this the Music Folder */ |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 2372 | if (!strcasecmp(ob->oi.Filename, "My Music") || |
| 2373 | !strcasecmp(ob->oi.Filename, "Music")) { |
| 2374 | device->default_music_folder = ob->oid; |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 2375 | } |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 2376 | else if (!strcasecmp(ob->oi.Filename, "My Playlists") || |
| 2377 | !strcasecmp(ob->oi.Filename, "Playlists")) { |
| 2378 | device->default_playlist_folder = ob->oid; |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 2379 | } |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 2380 | else if (!strcasecmp(ob->oi.Filename, "My Pictures") || |
| 2381 | !strcasecmp(ob->oi.Filename, "Pictures")) { |
| 2382 | device->default_picture_folder = ob->oid; |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 2383 | } |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 2384 | else if (!strcasecmp(ob->oi.Filename, "My Video") || |
| 2385 | !strcasecmp(ob->oi.Filename, "Video")) { |
| 2386 | device->default_video_folder = ob->oid; |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 2387 | } |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 2388 | else if (!strcasecmp(ob->oi.Filename, "My Organizer")) { |
| 2389 | device->default_organizer_folder = ob->oid; |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 2390 | } |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 2391 | else if (!strcasecmp(ob->oi.Filename, "ZENcast") || |
| 2392 | !strcasecmp(ob->oi.Filename, "Datacasts")) { |
| 2393 | device->default_zencast_folder = ob->oid; |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 2394 | } |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 2395 | else if (!strcasecmp(ob->oi.Filename, "My Albums") || |
| 2396 | !strcasecmp(ob->oi.Filename, "Albums")) { |
| 2397 | device->default_album_folder = ob->oid; |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 2398 | } |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 2399 | else if (!strcasecmp(ob->oi.Filename, "Text") || |
| 2400 | !strcasecmp(ob->oi.Filename, "Texts")) { |
| 2401 | device->default_text_folder = ob->oid; |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 2402 | } |
| 2403 | } |
Richard Low | 6711f44 | 2007-05-05 19:00:59 +0000 | [diff] [blame] | 2404 | } |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 2405 | |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 2406 | /** |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 2407 | * This function traverses a devices storage list freeing up the |
| 2408 | * strings and the structs. |
| 2409 | * @param device a pointer to the MTP device to free the storage |
| 2410 | * list for. |
| 2411 | */ |
| 2412 | static void free_storage_list(LIBMTP_mtpdevice_t *device) |
| 2413 | { |
Linus Walleij | e1ac07e | 2006-12-14 19:38:59 +0000 | [diff] [blame] | 2414 | LIBMTP_devicestorage_t *storage; |
| 2415 | LIBMTP_devicestorage_t *tmp; |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 2416 | |
Linus Walleij | e1ac07e | 2006-12-14 19:38:59 +0000 | [diff] [blame] | 2417 | storage = device->storage; |
| 2418 | while(storage != NULL) { |
| 2419 | if (storage->StorageDescription != NULL) { |
| 2420 | free(storage->StorageDescription); |
| 2421 | } |
| 2422 | if (storage->VolumeIdentifier != NULL) { |
| 2423 | free(storage->VolumeIdentifier); |
| 2424 | } |
| 2425 | tmp = storage; |
| 2426 | storage = storage->next; |
| 2427 | free(tmp); |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 2428 | } |
| 2429 | device->storage = NULL; |
| 2430 | |
| 2431 | return; |
| 2432 | } |
| 2433 | |
| 2434 | /** |
| 2435 | * This function traverses a devices storage list freeing up the |
| 2436 | * strings and the structs. |
| 2437 | * @param device a pointer to the MTP device to free the storage |
| 2438 | * list for. |
| 2439 | */ |
| 2440 | static int sort_storage_by(LIBMTP_mtpdevice_t *device,int const sortby) |
| 2441 | { |
| 2442 | LIBMTP_devicestorage_t *oldhead, *ptr1, *ptr2, *newlist; |
| 2443 | |
| 2444 | if (device->storage == NULL) |
| 2445 | return -1; |
| 2446 | if (sortby == LIBMTP_STORAGE_SORTBY_NOTSORTED) |
| 2447 | return 0; |
| 2448 | |
| 2449 | oldhead = ptr1 = ptr2 = device->storage; |
| 2450 | |
| 2451 | newlist = NULL; |
| 2452 | |
| 2453 | while(oldhead != NULL) { |
| 2454 | ptr1 = ptr2 = oldhead; |
| 2455 | while(ptr1 != NULL) { |
| 2456 | |
| 2457 | if (sortby == LIBMTP_STORAGE_SORTBY_FREESPACE && ptr1->FreeSpaceInBytes > ptr2->FreeSpaceInBytes) |
| 2458 | ptr2 = ptr1; |
| 2459 | if (sortby == LIBMTP_STORAGE_SORTBY_MAXSPACE && ptr1->FreeSpaceInBytes > ptr2->FreeSpaceInBytes) |
| 2460 | ptr2 = ptr1; |
| 2461 | |
| 2462 | ptr1 = ptr1->next; |
| 2463 | } |
| 2464 | |
| 2465 | // Make our previous entries next point to our next |
| 2466 | if(ptr2->prev != NULL) { |
| 2467 | ptr1 = ptr2->prev; |
| 2468 | ptr1->next = ptr2->next; |
| 2469 | } else { |
| 2470 | oldhead = ptr2->next; |
| 2471 | if(oldhead != NULL) |
| 2472 | oldhead->prev = NULL; |
| 2473 | } |
| 2474 | |
| 2475 | // Make our next entries previous point to our previous |
| 2476 | ptr1 = ptr2->next; |
| 2477 | if(ptr1 != NULL) { |
| 2478 | ptr1->prev = ptr2->prev; |
| 2479 | } else { |
| 2480 | ptr1 = ptr2->prev; |
| 2481 | if(ptr1 != NULL) |
| 2482 | ptr1->next = NULL; |
| 2483 | } |
| 2484 | |
| 2485 | if(newlist == NULL) { |
| 2486 | newlist = ptr2; |
| 2487 | newlist->prev = NULL; |
| 2488 | } else { |
| 2489 | ptr2->prev = newlist; |
| 2490 | newlist->next = ptr2; |
| 2491 | newlist = newlist->next; |
| 2492 | } |
| 2493 | } |
| 2494 | |
Linus Walleij | 5c1499e | 2009-02-21 06:54:29 +0000 | [diff] [blame] | 2495 | if (newlist != NULL) { |
| 2496 | newlist->next = NULL; |
| 2497 | while(newlist->prev != NULL) |
| 2498 | newlist = newlist->prev; |
| 2499 | device->storage = newlist; |
| 2500 | } |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 2501 | |
| 2502 | return 0; |
| 2503 | } |
| 2504 | |
| 2505 | /** |
Linus Walleij | d71d0b3 | 2008-09-22 08:21:03 +0000 | [diff] [blame] | 2506 | * This function grabs the first writeable storageid from the |
| 2507 | * device storage list. |
| 2508 | * @param device a pointer to the MTP device to locate writeable |
| 2509 | * storage for. |
| 2510 | * @param fitsize a file of this file must fit on the device. |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 2511 | */ |
Linus Walleij | d71d0b3 | 2008-09-22 08:21:03 +0000 | [diff] [blame] | 2512 | static uint32_t get_writeable_storageid(LIBMTP_mtpdevice_t *device, uint64_t fitsize) |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 2513 | { |
Linus Walleij | 5c1499e | 2009-02-21 06:54:29 +0000 | [diff] [blame] | 2514 | LIBMTP_devicestorage_t *storage; |
Linus Walleij | fb28b63 | 2007-10-23 21:56:18 +0000 | [diff] [blame] | 2515 | uint32_t store = 0x00000000; //Should this be 0xffffffffu instead? |
Linus Walleij | d71d0b3 | 2008-09-22 08:21:03 +0000 | [diff] [blame] | 2516 | int subcall_ret; |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 2517 | |
Linus Walleij | d71d0b3 | 2008-09-22 08:21:03 +0000 | [diff] [blame] | 2518 | // See if there is some storage we can fit this file on. |
| 2519 | storage = device->storage; |
| 2520 | if (storage == NULL) { |
| 2521 | // Sometimes the storage just cannot be detected. |
| 2522 | store = 0x00000000U; |
| 2523 | } else { |
| 2524 | while(storage != NULL) { |
| 2525 | // These storages cannot be used. |
| 2526 | if (storage->StorageType == PTP_ST_FixedROM || storage->StorageType == PTP_ST_RemovableROM) { |
| 2527 | storage = storage->next; |
| 2528 | continue; |
| 2529 | } |
| 2530 | // Storage IDs with the lower 16 bits 0x0000 are not supposed |
| 2531 | // to be writeable. |
| 2532 | if ((storage->id & 0x0000FFFFU) == 0x00000000U) { |
| 2533 | storage = storage->next; |
| 2534 | continue; |
| 2535 | } |
| 2536 | // Also check the access capability to avoid e.g. deletable only storages |
| 2537 | if (storage->AccessCapability == PTP_AC_ReadOnly || storage->AccessCapability == PTP_AC_ReadOnly_with_Object_Deletion) { |
| 2538 | storage = storage->next; |
| 2539 | continue; |
| 2540 | } |
| 2541 | // Then see if we can fit the file. |
| 2542 | subcall_ret = check_if_file_fits(device, storage, fitsize); |
| 2543 | if (subcall_ret != 0) { |
| 2544 | storage = storage->next; |
| 2545 | } else { |
| 2546 | // We found a storage that is writable and can fit the file! |
| 2547 | break; |
| 2548 | } |
| 2549 | } |
| 2550 | if (storage == NULL) { |
| 2551 | add_error_to_errorstack(device, LIBMTP_ERROR_STORAGE_FULL, "LIBMTP_Send_File_From_File_Descriptor(): " |
| 2552 | "all device storage is full or corrupt."); |
| 2553 | return -1; |
| 2554 | } |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 2555 | store = storage->id; |
Linus Walleij | d71d0b3 | 2008-09-22 08:21:03 +0000 | [diff] [blame] | 2556 | } |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 2557 | |
| 2558 | return store; |
| 2559 | } |
| 2560 | |
| 2561 | /** |
Linus Walleij | 6bf68b4 | 2007-09-03 22:50:02 +0000 | [diff] [blame] | 2562 | * This function grabs the freespace from a certain storage in |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 2563 | * device storage list. |
| 2564 | * @param device a pointer to the MTP device to free the storage |
| 2565 | * list for. |
Linus Walleij | 6bf68b4 | 2007-09-03 22:50:02 +0000 | [diff] [blame] | 2566 | * @param storageid the storage ID for the storage to flush and |
| 2567 | * get free space for. |
| 2568 | * @param freespace the free space on this storage will be returned |
| 2569 | * in this variable. |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 2570 | */ |
Linus Walleij | 6bf68b4 | 2007-09-03 22:50:02 +0000 | [diff] [blame] | 2571 | static int get_storage_freespace(LIBMTP_mtpdevice_t *device, |
| 2572 | LIBMTP_devicestorage_t *storage, |
| 2573 | uint64_t *freespace) |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 2574 | { |
Linus Walleij | e1ac07e | 2006-12-14 19:38:59 +0000 | [diff] [blame] | 2575 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 2576 | |
Linus Walleij | e1ac07e | 2006-12-14 19:38:59 +0000 | [diff] [blame] | 2577 | // Always query the device about this, since some models explicitly |
Linus Walleij | 6bf68b4 | 2007-09-03 22:50:02 +0000 | [diff] [blame] | 2578 | // needs that. We flush all data on queries storage here. |
Linus Walleij | e1ac07e | 2006-12-14 19:38:59 +0000 | [diff] [blame] | 2579 | if (ptp_operation_issupported(params,PTP_OC_GetStorageInfo)) { |
| 2580 | PTPStorageInfo storageInfo; |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 2581 | uint16_t ret; |
Linus Walleij | e1ac07e | 2006-12-14 19:38:59 +0000 | [diff] [blame] | 2582 | |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 2583 | ret = ptp_getstorageinfo(params, storage->id, &storageInfo); |
| 2584 | if (ret != PTP_RC_OK) { |
| 2585 | 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] | 2586 | return -1; |
| 2587 | } |
| 2588 | if (storage->StorageDescription != NULL) { |
| 2589 | free(storage->StorageDescription); |
| 2590 | } |
| 2591 | if (storage->VolumeIdentifier != NULL) { |
| 2592 | free(storage->VolumeIdentifier); |
| 2593 | } |
| 2594 | storage->StorageType = storageInfo.StorageType; |
| 2595 | storage->FilesystemType = storageInfo.FilesystemType; |
| 2596 | storage->AccessCapability = storageInfo.AccessCapability; |
| 2597 | storage->MaxCapacity = storageInfo.MaxCapability; |
| 2598 | storage->FreeSpaceInBytes = storageInfo.FreeSpaceInBytes; |
| 2599 | storage->FreeSpaceInObjects = storageInfo.FreeSpaceInImages; |
| 2600 | storage->StorageDescription = storageInfo.StorageDescription; |
| 2601 | storage->VolumeIdentifier = storageInfo.VolumeLabel; |
| 2602 | } |
| 2603 | if(storage->FreeSpaceInBytes == (uint64_t) -1) |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 2604 | return -1; |
| 2605 | *freespace = storage->FreeSpaceInBytes; |
| 2606 | return 0; |
| 2607 | } |
| 2608 | |
| 2609 | /** |
Linus Walleij | 8c45b29 | 2006-04-26 14:12:44 +0000 | [diff] [blame] | 2610 | * This function dumps out a large chunk of textual information |
| 2611 | * provided from the PTP protocol and additionally some extra |
| 2612 | * MTP-specific information where applicable. |
| 2613 | * @param device a pointer to the MTP device to report info from. |
| 2614 | */ |
| 2615 | void LIBMTP_Dump_Device_Info(LIBMTP_mtpdevice_t *device) |
| 2616 | { |
| 2617 | int i; |
| 2618 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | c6210fb | 2006-05-08 11:11:41 +0000 | [diff] [blame] | 2619 | PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo; |
Linus Walleij | e1ac07e | 2006-12-14 19:38:59 +0000 | [diff] [blame] | 2620 | LIBMTP_devicestorage_t *storage = device->storage; |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 2621 | |
Linus Walleij | c6210fb | 2006-05-08 11:11:41 +0000 | [diff] [blame] | 2622 | printf("USB low-level info:\n"); |
| 2623 | dump_usbinfo(ptp_usb); |
Linus Walleij | 8c45b29 | 2006-04-26 14:12:44 +0000 | [diff] [blame] | 2624 | /* Print out some verbose information */ |
| 2625 | printf("Device info:\n"); |
| 2626 | printf(" Manufacturer: %s\n", params->deviceinfo.Manufacturer); |
| 2627 | printf(" Model: %s\n", params->deviceinfo.Model); |
| 2628 | printf(" Device version: %s\n", params->deviceinfo.DeviceVersion); |
| 2629 | printf(" Serial number: %s\n", params->deviceinfo.SerialNumber); |
| 2630 | printf(" Vendor extension ID: 0x%08x\n", params->deviceinfo.VendorExtensionID); |
| 2631 | printf(" Vendor extension description: %s\n", params->deviceinfo.VendorExtensionDesc); |
Linus Walleij | ddaba2f | 2007-10-02 21:20:30 +0000 | [diff] [blame] | 2632 | printf(" Detected object size: %d bits\n", device->object_bitsize); |
Linus Walleij | 8c45b29 | 2006-04-26 14:12:44 +0000 | [diff] [blame] | 2633 | printf("Supported operations:\n"); |
| 2634 | for (i=0;i<params->deviceinfo.OperationsSupported_len;i++) { |
Linus Walleij | 4f40d11 | 2006-09-21 07:44:53 +0000 | [diff] [blame] | 2635 | char txt[256]; |
| 2636 | |
| 2637 | (void) ptp_render_opcode (params, params->deviceinfo.OperationsSupported[i], sizeof(txt), txt); |
| 2638 | printf(" %04x: %s\n", params->deviceinfo.OperationsSupported[i], txt); |
Linus Walleij | 8c45b29 | 2006-04-26 14:12:44 +0000 | [diff] [blame] | 2639 | } |
| 2640 | printf("Events supported:\n"); |
| 2641 | if (params->deviceinfo.EventsSupported_len == 0) { |
| 2642 | printf(" None.\n"); |
| 2643 | } else { |
| 2644 | for (i=0;i<params->deviceinfo.EventsSupported_len;i++) { |
| 2645 | printf(" 0x%04x\n", params->deviceinfo.EventsSupported[i]); |
| 2646 | } |
| 2647 | } |
| 2648 | printf("Device Properties Supported:\n"); |
| 2649 | for (i=0;i<params->deviceinfo.DevicePropertiesSupported_len;i++) { |
Linus Walleij | 16c51f0 | 2006-05-04 13:20:22 +0000 | [diff] [blame] | 2650 | char const *propdesc = ptp_get_property_description(params, params->deviceinfo.DevicePropertiesSupported[i]); |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 2651 | |
Linus Walleij | 545c779 | 2006-06-13 15:22:30 +0000 | [diff] [blame] | 2652 | if (propdesc != NULL) { |
| 2653 | printf(" 0x%04x: %s\n", params->deviceinfo.DevicePropertiesSupported[i], propdesc); |
| 2654 | } else { |
| 2655 | uint16_t prop = params->deviceinfo.DevicePropertiesSupported[i]; |
Linus Walleij | cf223e6 | 2006-06-19 09:31:53 +0000 | [diff] [blame] | 2656 | printf(" 0x%04x: Unknown property\n", prop); |
Linus Walleij | 545c779 | 2006-06-13 15:22:30 +0000 | [diff] [blame] | 2657 | } |
Linus Walleij | 8c45b29 | 2006-04-26 14:12:44 +0000 | [diff] [blame] | 2658 | } |
Linus Walleij | 0af979a | 2006-06-19 11:49:10 +0000 | [diff] [blame] | 2659 | |
| 2660 | if (ptp_operation_issupported(params,PTP_OC_MTP_GetObjectPropsSupported)) { |
| 2661 | printf("Playable File (Object) Types and Object Properties Supported:\n"); |
| 2662 | for (i=0;i<params->deviceinfo.ImageFormats_len;i++) { |
| 2663 | char txt[256]; |
| 2664 | uint16_t ret; |
| 2665 | uint16_t *props = NULL; |
| 2666 | uint32_t propcnt = 0; |
| 2667 | int j; |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 2668 | |
Linus Walleij | 0af979a | 2006-06-19 11:49:10 +0000 | [diff] [blame] | 2669 | (void) ptp_render_ofc (params, params->deviceinfo.ImageFormats[i], sizeof(txt), txt); |
| 2670 | printf(" %04x: %s\n", params->deviceinfo.ImageFormats[i], txt); |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 2671 | |
Linus Walleij | 0af979a | 2006-06-19 11:49:10 +0000 | [diff] [blame] | 2672 | ret = ptp_mtp_getobjectpropssupported (params, params->deviceinfo.ImageFormats[i], &propcnt, &props); |
| 2673 | if (ret != PTP_RC_OK) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 2674 | 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] | 2675 | } else { |
| 2676 | for (j=0;j<propcnt;j++) { |
Linus Walleij | 1483034 | 2007-03-23 13:32:00 +0000 | [diff] [blame] | 2677 | PTPObjectPropDesc opd; |
| 2678 | int k; |
| 2679 | |
Richard Low | 6f07084 | 2009-05-03 10:26:16 +0000 | [diff] [blame] | 2680 | printf(" %04x: %s", props[j], LIBMTP_Get_Property_Description(map_ptp_property_to_libmtp_property(props[j]))); |
Linus Walleij | 1483034 | 2007-03-23 13:32:00 +0000 | [diff] [blame] | 2681 | // Get a more verbose description |
| 2682 | ret = ptp_mtp_getobjectpropdesc(params, props[j], params->deviceinfo.ImageFormats[i], &opd); |
| 2683 | if (ret != PTP_RC_OK) { |
| 2684 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Dump_Device_Info(): " |
| 2685 | "could not get property description."); |
| 2686 | break; |
| 2687 | } |
| 2688 | |
| 2689 | if (opd.DataType == PTP_DTC_STR) { |
| 2690 | printf(" STRING data type"); |
Linus Walleij | 22cc487 | 2007-11-05 22:57:19 +0000 | [diff] [blame] | 2691 | switch (opd.FormFlag) { |
| 2692 | case PTP_OPFF_DateTime: |
| 2693 | printf(" DATETIME FORM"); |
| 2694 | break; |
| 2695 | case PTP_OPFF_RegularExpression: |
| 2696 | printf(" REGULAR EXPRESSION FORM"); |
| 2697 | break; |
| 2698 | case PTP_OPFF_LongString: |
| 2699 | printf(" LONG STRING FORM"); |
| 2700 | break; |
| 2701 | default: |
| 2702 | break; |
| 2703 | } |
Linus Walleij | 1483034 | 2007-03-23 13:32:00 +0000 | [diff] [blame] | 2704 | } else { |
| 2705 | if (opd.DataType & PTP_DTC_ARRAY_MASK) { |
| 2706 | printf(" array of"); |
| 2707 | } |
| 2708 | |
| 2709 | switch (opd.DataType & (~PTP_DTC_ARRAY_MASK)) { |
| 2710 | |
| 2711 | case PTP_DTC_UNDEF: |
| 2712 | printf(" UNDEFINED data type"); |
| 2713 | break; |
| 2714 | |
| 2715 | case PTP_DTC_INT8: |
| 2716 | printf(" INT8 data type"); |
| 2717 | switch (opd.FormFlag) { |
Linus Walleij | 22cc487 | 2007-11-05 22:57:19 +0000 | [diff] [blame] | 2718 | case PTP_OPFF_Range: |
Linus Walleij | 1483034 | 2007-03-23 13:32:00 +0000 | [diff] [blame] | 2719 | printf(" range: MIN %d, MAX %d, STEP %d", |
| 2720 | opd.FORM.Range.MinimumValue.i8, |
| 2721 | opd.FORM.Range.MaximumValue.i8, |
| 2722 | opd.FORM.Range.StepSize.i8); |
| 2723 | break; |
Linus Walleij | 22cc487 | 2007-11-05 22:57:19 +0000 | [diff] [blame] | 2724 | case PTP_OPFF_Enumeration: |
Linus Walleij | 1483034 | 2007-03-23 13:32:00 +0000 | [diff] [blame] | 2725 | printf(" enumeration: "); |
| 2726 | for(k=0;k<opd.FORM.Enum.NumberOfValues;k++) { |
| 2727 | printf("%d, ", opd.FORM.Enum.SupportedValue[k].i8); |
| 2728 | } |
| 2729 | break; |
Linus Walleij | 22cc487 | 2007-11-05 22:57:19 +0000 | [diff] [blame] | 2730 | case PTP_OPFF_ByteArray: |
| 2731 | printf(" byte array: "); |
| 2732 | break; |
Linus Walleij | 1483034 | 2007-03-23 13:32:00 +0000 | [diff] [blame] | 2733 | default: |
| 2734 | printf(" ANY 8BIT VALUE form"); |
| 2735 | break; |
| 2736 | } |
| 2737 | break; |
| 2738 | |
| 2739 | case PTP_DTC_UINT8: |
| 2740 | printf(" UINT8 data type"); |
| 2741 | switch (opd.FormFlag) { |
Linus Walleij | 22cc487 | 2007-11-05 22:57:19 +0000 | [diff] [blame] | 2742 | case PTP_OPFF_Range: |
Linus Walleij | 1483034 | 2007-03-23 13:32:00 +0000 | [diff] [blame] | 2743 | printf(" range: MIN %d, MAX %d, STEP %d", |
| 2744 | opd.FORM.Range.MinimumValue.u8, |
| 2745 | opd.FORM.Range.MaximumValue.u8, |
| 2746 | opd.FORM.Range.StepSize.u8); |
| 2747 | break; |
Linus Walleij | 22cc487 | 2007-11-05 22:57:19 +0000 | [diff] [blame] | 2748 | case PTP_OPFF_Enumeration: |
Linus Walleij | 1483034 | 2007-03-23 13:32:00 +0000 | [diff] [blame] | 2749 | printf(" enumeration: "); |
| 2750 | for(k=0;k<opd.FORM.Enum.NumberOfValues;k++) { |
| 2751 | printf("%d, ", opd.FORM.Enum.SupportedValue[k].u8); |
| 2752 | } |
| 2753 | break; |
Linus Walleij | 22cc487 | 2007-11-05 22:57:19 +0000 | [diff] [blame] | 2754 | case PTP_OPFF_ByteArray: |
| 2755 | printf(" byte array: "); |
| 2756 | break; |
Linus Walleij | 1483034 | 2007-03-23 13:32:00 +0000 | [diff] [blame] | 2757 | default: |
| 2758 | printf(" ANY 8BIT VALUE form"); |
| 2759 | break; |
| 2760 | } |
| 2761 | break; |
| 2762 | |
| 2763 | case PTP_DTC_INT16: |
| 2764 | printf(" INT16 data type"); |
| 2765 | switch (opd.FormFlag) { |
Linus Walleij | 22cc487 | 2007-11-05 22:57:19 +0000 | [diff] [blame] | 2766 | case PTP_OPFF_Range: |
Linus Walleij | 1483034 | 2007-03-23 13:32:00 +0000 | [diff] [blame] | 2767 | printf(" range: MIN %d, MAX %d, STEP %d", |
| 2768 | opd.FORM.Range.MinimumValue.i16, |
| 2769 | opd.FORM.Range.MaximumValue.i16, |
| 2770 | opd.FORM.Range.StepSize.i16); |
| 2771 | break; |
Linus Walleij | 22cc487 | 2007-11-05 22:57:19 +0000 | [diff] [blame] | 2772 | case PTP_OPFF_Enumeration: |
Linus Walleij | 1483034 | 2007-03-23 13:32:00 +0000 | [diff] [blame] | 2773 | printf(" enumeration: "); |
| 2774 | for(k=0;k<opd.FORM.Enum.NumberOfValues;k++) { |
| 2775 | printf("%d, ", opd.FORM.Enum.SupportedValue[k].i16); |
| 2776 | } |
| 2777 | break; |
| 2778 | default: |
| 2779 | printf(" ANY 16BIT VALUE form"); |
| 2780 | break; |
| 2781 | } |
| 2782 | break; |
| 2783 | |
| 2784 | case PTP_DTC_UINT16: |
| 2785 | printf(" UINT16 data type"); |
| 2786 | switch (opd.FormFlag) { |
Linus Walleij | 22cc487 | 2007-11-05 22:57:19 +0000 | [diff] [blame] | 2787 | case PTP_OPFF_Range: |
Linus Walleij | 1483034 | 2007-03-23 13:32:00 +0000 | [diff] [blame] | 2788 | printf(" range: MIN %d, MAX %d, STEP %d", |
| 2789 | opd.FORM.Range.MinimumValue.u16, |
| 2790 | opd.FORM.Range.MaximumValue.u16, |
| 2791 | opd.FORM.Range.StepSize.u16); |
| 2792 | break; |
Linus Walleij | 22cc487 | 2007-11-05 22:57:19 +0000 | [diff] [blame] | 2793 | case PTP_OPFF_Enumeration: |
Linus Walleij | 1483034 | 2007-03-23 13:32:00 +0000 | [diff] [blame] | 2794 | printf(" enumeration: "); |
| 2795 | for(k=0;k<opd.FORM.Enum.NumberOfValues;k++) { |
| 2796 | printf("%d, ", opd.FORM.Enum.SupportedValue[k].u16); |
| 2797 | } |
| 2798 | break; |
| 2799 | default: |
| 2800 | printf(" ANY 16BIT VALUE form"); |
| 2801 | break; |
| 2802 | } |
| 2803 | break; |
| 2804 | |
| 2805 | case PTP_DTC_INT32: |
| 2806 | printf(" INT32 data type"); |
| 2807 | switch (opd.FormFlag) { |
Linus Walleij | 22cc487 | 2007-11-05 22:57:19 +0000 | [diff] [blame] | 2808 | case PTP_OPFF_Range: |
Linus Walleij | 1483034 | 2007-03-23 13:32:00 +0000 | [diff] [blame] | 2809 | printf(" range: MIN %d, MAX %d, STEP %d", |
| 2810 | opd.FORM.Range.MinimumValue.i32, |
| 2811 | opd.FORM.Range.MaximumValue.i32, |
| 2812 | opd.FORM.Range.StepSize.i32); |
| 2813 | break; |
Linus Walleij | 22cc487 | 2007-11-05 22:57:19 +0000 | [diff] [blame] | 2814 | case PTP_OPFF_Enumeration: |
Linus Walleij | 1483034 | 2007-03-23 13:32:00 +0000 | [diff] [blame] | 2815 | printf(" enumeration: "); |
| 2816 | for(k=0;k<opd.FORM.Enum.NumberOfValues;k++) { |
| 2817 | printf("%d, ", opd.FORM.Enum.SupportedValue[k].i32); |
| 2818 | } |
| 2819 | break; |
| 2820 | default: |
| 2821 | printf(" ANY 32BIT VALUE form"); |
| 2822 | break; |
| 2823 | } |
| 2824 | break; |
| 2825 | |
| 2826 | case PTP_DTC_UINT32: |
| 2827 | printf(" UINT32 data type"); |
| 2828 | switch (opd.FormFlag) { |
Linus Walleij | 22cc487 | 2007-11-05 22:57:19 +0000 | [diff] [blame] | 2829 | case PTP_OPFF_Range: |
Linus Walleij | 1483034 | 2007-03-23 13:32:00 +0000 | [diff] [blame] | 2830 | printf(" range: MIN %d, MAX %d, STEP %d", |
| 2831 | opd.FORM.Range.MinimumValue.u32, |
| 2832 | opd.FORM.Range.MaximumValue.u32, |
| 2833 | opd.FORM.Range.StepSize.u32); |
| 2834 | break; |
Linus Walleij | 22cc487 | 2007-11-05 22:57:19 +0000 | [diff] [blame] | 2835 | case PTP_OPFF_Enumeration: |
Linus Walleij | d71d0b3 | 2008-09-22 08:21:03 +0000 | [diff] [blame] | 2836 | // Special pretty-print for FOURCC codes |
| 2837 | if (params->deviceinfo.ImageFormats[i] == PTP_OPC_VideoFourCCCodec) { |
| 2838 | printf(" enumeration of u32 casted FOURCC: "); |
| 2839 | for (k=0;k<opd.FORM.Enum.NumberOfValues;k++) { |
| 2840 | if (opd.FORM.Enum.SupportedValue[k].u32 == 0) { |
| 2841 | printf("ANY, "); |
| 2842 | } else { |
| 2843 | char fourcc[6]; |
| 2844 | fourcc[0] = (opd.FORM.Enum.SupportedValue[k].u32 >> 24) & 0xFFU; |
| 2845 | fourcc[1] = (opd.FORM.Enum.SupportedValue[k].u32 >> 16) & 0xFFU; |
| 2846 | fourcc[2] = (opd.FORM.Enum.SupportedValue[k].u32 >> 8) & 0xFFU; |
| 2847 | fourcc[3] = opd.FORM.Enum.SupportedValue[k].u32 & 0xFFU; |
| 2848 | fourcc[4] = '\n'; |
| 2849 | fourcc[5] = '\0'; |
| 2850 | printf("\"%s\", ", fourcc); |
| 2851 | } |
| 2852 | } |
| 2853 | } else { |
| 2854 | printf(" enumeration: "); |
| 2855 | for(k=0;k<opd.FORM.Enum.NumberOfValues;k++) { |
| 2856 | printf("%d, ", opd.FORM.Enum.SupportedValue[k].u32); |
| 2857 | } |
Linus Walleij | 1483034 | 2007-03-23 13:32:00 +0000 | [diff] [blame] | 2858 | } |
| 2859 | break; |
| 2860 | default: |
| 2861 | printf(" ANY 32BIT VALUE form"); |
| 2862 | break; |
| 2863 | } |
| 2864 | break; |
| 2865 | |
| 2866 | case PTP_DTC_INT64: |
| 2867 | printf(" INT64 data type"); |
| 2868 | break; |
| 2869 | |
| 2870 | case PTP_DTC_UINT64: |
| 2871 | printf(" UINT64 data type"); |
| 2872 | break; |
| 2873 | |
| 2874 | case PTP_DTC_INT128: |
| 2875 | printf(" INT128 data type"); |
| 2876 | break; |
| 2877 | |
| 2878 | case PTP_DTC_UINT128: |
| 2879 | printf(" UINT128 data type"); |
| 2880 | break; |
| 2881 | |
| 2882 | default: |
| 2883 | printf(" UNKNOWN data type"); |
| 2884 | break; |
| 2885 | } |
| 2886 | } |
| 2887 | if (opd.GetSet) { |
| 2888 | printf(" GET/SET"); |
| 2889 | } else { |
| 2890 | printf(" READ ONLY"); |
| 2891 | } |
| 2892 | printf("\n"); |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 2893 | ptp_free_objectpropdesc(&opd); |
Linus Walleij | 0af979a | 2006-06-19 11:49:10 +0000 | [diff] [blame] | 2894 | } |
| 2895 | free(props); |
| 2896 | } |
| 2897 | } |
| 2898 | } |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 2899 | |
Linus Walleij | e1ac07e | 2006-12-14 19:38:59 +0000 | [diff] [blame] | 2900 | if(storage != NULL && ptp_operation_issupported(params,PTP_OC_GetStorageInfo)) { |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 2901 | printf("Storage Devices:\n"); |
Linus Walleij | e1ac07e | 2006-12-14 19:38:59 +0000 | [diff] [blame] | 2902 | while(storage != NULL) { |
| 2903 | printf(" StorageID: 0x%08x\n",storage->id); |
Linus Walleij | d71d0b3 | 2008-09-22 08:21:03 +0000 | [diff] [blame] | 2904 | printf(" StorageType: 0x%04x ",storage->StorageType); |
| 2905 | switch (storage->StorageType) { |
| 2906 | case PTP_ST_Undefined: |
| 2907 | printf("(undefined)\n"); |
| 2908 | break; |
| 2909 | case PTP_ST_FixedROM: |
| 2910 | printf("fixed ROM storage\n"); |
| 2911 | break; |
| 2912 | case PTP_ST_RemovableROM: |
| 2913 | printf("removable ROM storage\n"); |
| 2914 | break; |
| 2915 | case PTP_ST_FixedRAM: |
| 2916 | printf("fixed RAM storage\n"); |
| 2917 | break; |
| 2918 | case PTP_ST_RemovableRAM: |
| 2919 | printf("removable RAM storage\n"); |
| 2920 | break; |
| 2921 | default: |
| 2922 | printf("UNKNOWN storage\n"); |
| 2923 | break; |
| 2924 | } |
| 2925 | printf(" FilesystemType: 0x%04x ",storage->FilesystemType); |
| 2926 | switch(storage->FilesystemType) { |
| 2927 | case PTP_FST_Undefined: |
| 2928 | printf("(undefined)\n"); |
| 2929 | break; |
| 2930 | case PTP_FST_GenericFlat: |
| 2931 | printf("generic flat filesystem\n"); |
| 2932 | break; |
| 2933 | case PTP_FST_GenericHierarchical: |
| 2934 | printf("generic hierarchical\n"); |
| 2935 | break; |
| 2936 | case PTP_FST_DCF: |
| 2937 | printf("DCF\n"); |
| 2938 | break; |
| 2939 | default: |
| 2940 | printf("UNKNONWN filesystem type\n"); |
| 2941 | break; |
| 2942 | } |
| 2943 | printf(" AccessCapability: 0x%04x ",storage->AccessCapability); |
| 2944 | switch(storage->AccessCapability) { |
| 2945 | case PTP_AC_ReadWrite: |
| 2946 | printf("read/write\n"); |
| 2947 | break; |
| 2948 | case PTP_AC_ReadOnly: |
| 2949 | printf("read only\n"); |
| 2950 | break; |
| 2951 | case PTP_AC_ReadOnly_with_Object_Deletion: |
| 2952 | printf("read only + object deletion\n"); |
| 2953 | break; |
| 2954 | default: |
| 2955 | printf("UNKNOWN access capability\n"); |
| 2956 | break; |
| 2957 | } |
Linus Walleij | fec4d56 | 2008-06-01 22:30:36 +0000 | [diff] [blame] | 2958 | printf(" MaxCapacity: %llu\n", (long long unsigned int) storage->MaxCapacity); |
| 2959 | printf(" FreeSpaceInBytes: %llu\n", (long long unsigned int) storage->FreeSpaceInBytes); |
| 2960 | printf(" FreeSpaceInObjects: %llu\n", (long long unsigned int) storage->FreeSpaceInObjects); |
Linus Walleij | e1ac07e | 2006-12-14 19:38:59 +0000 | [diff] [blame] | 2961 | printf(" StorageDescription: %s\n",storage->StorageDescription); |
| 2962 | printf(" VolumeIdentifier: %s\n",storage->VolumeIdentifier); |
| 2963 | storage = storage->next; |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 2964 | } |
| 2965 | } |
| 2966 | |
Linus Walleij | 545c779 | 2006-06-13 15:22:30 +0000 | [diff] [blame] | 2967 | printf("Special directories:\n"); |
| 2968 | printf(" Default music folder: 0x%08x\n", device->default_music_folder); |
| 2969 | printf(" Default playlist folder: 0x%08x\n", device->default_playlist_folder); |
| 2970 | printf(" Default picture folder: 0x%08x\n", device->default_picture_folder); |
| 2971 | printf(" Default video folder: 0x%08x\n", device->default_video_folder); |
| 2972 | printf(" Default organizer folder: 0x%08x\n", device->default_organizer_folder); |
| 2973 | printf(" Default zencast folder: 0x%08x\n", device->default_zencast_folder); |
Linus Walleij | ccf28ce | 2006-11-16 16:06:38 +0000 | [diff] [blame] | 2974 | printf(" Default album folder: 0x%08x\n", device->default_album_folder); |
Linus Walleij | 9316e65 | 2006-12-07 09:55:21 +0000 | [diff] [blame] | 2975 | printf(" Default text folder: 0x%08x\n", device->default_text_folder); |
Linus Walleij | 8c45b29 | 2006-04-26 14:12:44 +0000 | [diff] [blame] | 2976 | } |
| 2977 | |
| 2978 | /** |
Linus Walleij | 5d533bb | 2007-07-17 21:48:57 +0000 | [diff] [blame] | 2979 | * This resets a device in case it supports the <code>PTP_OC_ResetDevice</code> |
| 2980 | * operation code (0x1010). |
| 2981 | * @param device a pointer to the device to reset. |
| 2982 | * @return 0 on success, any other value means failure. |
| 2983 | */ |
| 2984 | int LIBMTP_Reset_Device(LIBMTP_mtpdevice_t *device) |
| 2985 | { |
| 2986 | PTPParams *params = (PTPParams *) device->params; |
| 2987 | uint16_t ret; |
| 2988 | |
| 2989 | if (!ptp_operation_issupported(params,PTP_OC_ResetDevice)) { |
| 2990 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, |
| 2991 | "LIBMTP_Reset_Device(): device does not support resetting."); |
| 2992 | return -1; |
| 2993 | } |
| 2994 | ret = ptp_resetdevice(params); |
| 2995 | if (ret != PTP_RC_OK) { |
| 2996 | add_ptp_error_to_errorstack(device, ret, "Error resetting."); |
| 2997 | return -1; |
| 2998 | } |
| 2999 | return 0; |
| 3000 | } |
| 3001 | |
| 3002 | /** |
Linus Walleij | 2350b71 | 2008-01-14 22:54:37 +0000 | [diff] [blame] | 3003 | * This retrieves the manufacturer name of an MTP device. |
| 3004 | * @param device a pointer to the device to get the manufacturer name for. |
| 3005 | * @return a newly allocated UTF-8 string representing the manufacturer name. |
| 3006 | * The string must be freed by the caller after use. If the call |
| 3007 | * was unsuccessful this will contain NULL. |
| 3008 | */ |
| 3009 | char *LIBMTP_Get_Manufacturername(LIBMTP_mtpdevice_t *device) |
| 3010 | { |
| 3011 | char *retmanuf = NULL; |
| 3012 | PTPParams *params = (PTPParams *) device->params; |
| 3013 | |
| 3014 | if (params->deviceinfo.Manufacturer != NULL) { |
| 3015 | retmanuf = strdup(params->deviceinfo.Manufacturer); |
| 3016 | } |
| 3017 | return retmanuf; |
| 3018 | } |
| 3019 | |
| 3020 | /** |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 3021 | * This retrieves the model name (often equal to product name) |
Linus Walleij | 8012406 | 2006-03-15 10:26:09 +0000 | [diff] [blame] | 3022 | * of an MTP device. |
| 3023 | * @param device a pointer to the device to get the model name for. |
| 3024 | * @return a newly allocated UTF-8 string representing the model name. |
| 3025 | * The string must be freed by the caller after use. If the call |
| 3026 | * was unsuccessful this will contain NULL. |
| 3027 | */ |
| 3028 | char *LIBMTP_Get_Modelname(LIBMTP_mtpdevice_t *device) |
| 3029 | { |
| 3030 | char *retmodel = NULL; |
Linus Walleij | 9b28da3 | 2006-03-16 13:47:58 +0000 | [diff] [blame] | 3031 | PTPParams *params = (PTPParams *) device->params; |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 3032 | |
Linus Walleij | 9b28da3 | 2006-03-16 13:47:58 +0000 | [diff] [blame] | 3033 | if (params->deviceinfo.Model != NULL) { |
| 3034 | retmodel = strdup(params->deviceinfo.Model); |
Linus Walleij | 8012406 | 2006-03-15 10:26:09 +0000 | [diff] [blame] | 3035 | } |
| 3036 | return retmodel; |
| 3037 | } |
| 3038 | |
| 3039 | /** |
| 3040 | * This retrieves the serial number of an MTP device. |
| 3041 | * @param device a pointer to the device to get the serial number for. |
| 3042 | * @return a newly allocated UTF-8 string representing the serial number. |
| 3043 | * The string must be freed by the caller after use. If the call |
| 3044 | * was unsuccessful this will contain NULL. |
| 3045 | */ |
| 3046 | char *LIBMTP_Get_Serialnumber(LIBMTP_mtpdevice_t *device) |
| 3047 | { |
| 3048 | char *retnumber = NULL; |
Linus Walleij | 9b28da3 | 2006-03-16 13:47:58 +0000 | [diff] [blame] | 3049 | PTPParams *params = (PTPParams *) device->params; |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 3050 | |
Linus Walleij | 9b28da3 | 2006-03-16 13:47:58 +0000 | [diff] [blame] | 3051 | if (params->deviceinfo.SerialNumber != NULL) { |
| 3052 | retnumber = strdup(params->deviceinfo.SerialNumber); |
Linus Walleij | 8012406 | 2006-03-15 10:26:09 +0000 | [diff] [blame] | 3053 | } |
| 3054 | return retnumber; |
| 3055 | } |
| 3056 | |
| 3057 | /** |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 3058 | * This retrieves the device version (hardware and firmware version) of an |
Linus Walleij | 8012406 | 2006-03-15 10:26:09 +0000 | [diff] [blame] | 3059 | * MTP device. |
| 3060 | * @param device a pointer to the device to get the device version for. |
| 3061 | * @return a newly allocated UTF-8 string representing the device version. |
| 3062 | * The string must be freed by the caller after use. If the call |
| 3063 | * was unsuccessful this will contain NULL. |
| 3064 | */ |
| 3065 | char *LIBMTP_Get_Deviceversion(LIBMTP_mtpdevice_t *device) |
| 3066 | { |
| 3067 | char *retversion = NULL; |
Linus Walleij | 9b28da3 | 2006-03-16 13:47:58 +0000 | [diff] [blame] | 3068 | PTPParams *params = (PTPParams *) device->params; |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 3069 | |
Linus Walleij | 9b28da3 | 2006-03-16 13:47:58 +0000 | [diff] [blame] | 3070 | if (params->deviceinfo.DeviceVersion != NULL) { |
| 3071 | retversion = strdup(params->deviceinfo.DeviceVersion); |
Linus Walleij | 8012406 | 2006-03-15 10:26:09 +0000 | [diff] [blame] | 3072 | } |
| 3073 | return retversion; |
| 3074 | } |
| 3075 | |
| 3076 | |
| 3077 | /** |
Linus Walleij | fae2748 | 2006-08-19 20:13:25 +0000 | [diff] [blame] | 3078 | * This retrieves the "friendly name" of an MTP device. Usually |
| 3079 | * this is simply the name of the owner or something like |
Linus Walleij | 3065879 | 2006-08-19 22:18:55 +0000 | [diff] [blame] | 3080 | * "John Doe's Digital Audio Player". This property should be supported |
Linus Walleij | fae2748 | 2006-08-19 20:13:25 +0000 | [diff] [blame] | 3081 | * by all MTP devices. |
| 3082 | * @param device a pointer to the device to get the friendly name for. |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 3083 | * @return a newly allocated UTF-8 string representing the friendly name. |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 3084 | * The string must be freed by the caller after use. |
Linus Walleij | 3065879 | 2006-08-19 22:18:55 +0000 | [diff] [blame] | 3085 | * @see LIBMTP_Set_Friendlyname() |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 3086 | */ |
Linus Walleij | 3065879 | 2006-08-19 22:18:55 +0000 | [diff] [blame] | 3087 | char *LIBMTP_Get_Friendlyname(LIBMTP_mtpdevice_t *device) |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 3088 | { |
Linus Walleij | b02a066 | 2006-04-25 08:05:09 +0000 | [diff] [blame] | 3089 | PTPPropertyValue propval; |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 3090 | char *retstring = NULL; |
Linus Walleij | 9b28da3 | 2006-03-16 13:47:58 +0000 | [diff] [blame] | 3091 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 3092 | uint16_t ret; |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 3093 | |
Linus Walleij | cf223e6 | 2006-06-19 09:31:53 +0000 | [diff] [blame] | 3094 | if (!ptp_property_issupported(params, PTP_DPC_MTP_DeviceFriendlyName)) { |
| 3095 | return NULL; |
| 3096 | } |
| 3097 | |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 3098 | ret = ptp_getdevicepropvalue(params, |
| 3099 | PTP_DPC_MTP_DeviceFriendlyName, |
| 3100 | &propval, |
| 3101 | PTP_DTC_STR); |
| 3102 | if (ret != PTP_RC_OK) { |
| 3103 | add_ptp_error_to_errorstack(device, ret, "Error getting friendlyname."); |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 3104 | return NULL; |
| 3105 | } |
Linus Walleij | a823a70 | 2006-08-27 21:27:46 +0000 | [diff] [blame] | 3106 | if (propval.str != NULL) { |
| 3107 | retstring = strdup(propval.str); |
| 3108 | free(propval.str); |
| 3109 | } |
Linus Walleij | fae2748 | 2006-08-19 20:13:25 +0000 | [diff] [blame] | 3110 | return retstring; |
| 3111 | } |
| 3112 | |
| 3113 | /** |
Linus Walleij | 3065879 | 2006-08-19 22:18:55 +0000 | [diff] [blame] | 3114 | * Sets the "friendly name" of an MTP device. |
| 3115 | * @param device a pointer to the device to set the friendly name for. |
| 3116 | * @param friendlyname the new friendly name for the device. |
| 3117 | * @return 0 on success, any other value means failure. |
Linus Walleij | d5b3497 | 2008-09-24 20:24:12 +0000 | [diff] [blame] | 3118 | * @see LIBMTP_Get_Friendlyname() |
Linus Walleij | 3065879 | 2006-08-19 22:18:55 +0000 | [diff] [blame] | 3119 | */ |
| 3120 | int LIBMTP_Set_Friendlyname(LIBMTP_mtpdevice_t *device, |
| 3121 | char const * const friendlyname) |
| 3122 | { |
| 3123 | PTPPropertyValue propval; |
| 3124 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 3125 | uint16_t ret; |
Linus Walleij | 3065879 | 2006-08-19 22:18:55 +0000 | [diff] [blame] | 3126 | |
| 3127 | if (!ptp_property_issupported(params, PTP_DPC_MTP_DeviceFriendlyName)) { |
| 3128 | return -1; |
| 3129 | } |
Linus Walleij | a823a70 | 2006-08-27 21:27:46 +0000 | [diff] [blame] | 3130 | propval.str = (char *) friendlyname; |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 3131 | ret = ptp_setdevicepropvalue(params, |
| 3132 | PTP_DPC_MTP_DeviceFriendlyName, |
| 3133 | &propval, |
| 3134 | PTP_DTC_STR); |
| 3135 | if (ret != PTP_RC_OK) { |
| 3136 | add_ptp_error_to_errorstack(device, ret, "Error setting friendlyname."); |
Linus Walleij | 3065879 | 2006-08-19 22:18:55 +0000 | [diff] [blame] | 3137 | return -1; |
| 3138 | } |
Linus Walleij | 3065879 | 2006-08-19 22:18:55 +0000 | [diff] [blame] | 3139 | return 0; |
| 3140 | } |
| 3141 | |
| 3142 | /** |
Linus Walleij | fae2748 | 2006-08-19 20:13:25 +0000 | [diff] [blame] | 3143 | * This retrieves the syncronization partner of an MTP device. This |
| 3144 | * property should be supported by all MTP devices. |
| 3145 | * @param device a pointer to the device to get the sync partner for. |
| 3146 | * @return a newly allocated UTF-8 string representing the synchronization |
| 3147 | * partner. The string must be freed by the caller after use. |
Linus Walleij | 3065879 | 2006-08-19 22:18:55 +0000 | [diff] [blame] | 3148 | * @see LIBMTP_Set_Syncpartner() |
Linus Walleij | fae2748 | 2006-08-19 20:13:25 +0000 | [diff] [blame] | 3149 | */ |
| 3150 | char *LIBMTP_Get_Syncpartner(LIBMTP_mtpdevice_t *device) |
| 3151 | { |
| 3152 | PTPPropertyValue propval; |
| 3153 | char *retstring = NULL; |
| 3154 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 3155 | uint16_t ret; |
Linus Walleij | fae2748 | 2006-08-19 20:13:25 +0000 | [diff] [blame] | 3156 | |
| 3157 | if (!ptp_property_issupported(params, PTP_DPC_MTP_SynchronizationPartner)) { |
| 3158 | return NULL; |
| 3159 | } |
| 3160 | |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 3161 | ret = ptp_getdevicepropvalue(params, |
| 3162 | PTP_DPC_MTP_SynchronizationPartner, |
| 3163 | &propval, |
| 3164 | PTP_DTC_STR); |
| 3165 | if (ret != PTP_RC_OK) { |
| 3166 | add_ptp_error_to_errorstack(device, ret, "Error getting syncpartner."); |
Linus Walleij | fae2748 | 2006-08-19 20:13:25 +0000 | [diff] [blame] | 3167 | return NULL; |
| 3168 | } |
Linus Walleij | a823a70 | 2006-08-27 21:27:46 +0000 | [diff] [blame] | 3169 | if (propval.str != NULL) { |
| 3170 | retstring = strdup(propval.str); |
| 3171 | free(propval.str); |
| 3172 | } |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 3173 | return retstring; |
| 3174 | } |
| 3175 | |
Linus Walleij | 3065879 | 2006-08-19 22:18:55 +0000 | [diff] [blame] | 3176 | |
| 3177 | /** |
| 3178 | * Sets the synchronization partner of an MTP device. Note that |
| 3179 | * we have no idea what the effect of setting this to "foobar" |
| 3180 | * may be. But the general idea seems to be to tell which program |
| 3181 | * shall synchronize with this device and tell others to leave |
| 3182 | * it alone. |
| 3183 | * @param device a pointer to the device to set the sync partner for. |
| 3184 | * @param syncpartner the new synchronization partner for the device. |
| 3185 | * @return 0 on success, any other value means failure. |
| 3186 | * @see LIBMTP_Get_Syncpartner() |
| 3187 | */ |
| 3188 | int LIBMTP_Set_Syncpartner(LIBMTP_mtpdevice_t *device, |
| 3189 | char const * const syncpartner) |
| 3190 | { |
| 3191 | PTPPropertyValue propval; |
| 3192 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 3193 | uint16_t ret; |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 3194 | |
Linus Walleij | 3065879 | 2006-08-19 22:18:55 +0000 | [diff] [blame] | 3195 | if (!ptp_property_issupported(params, PTP_DPC_MTP_SynchronizationPartner)) { |
| 3196 | return -1; |
| 3197 | } |
Linus Walleij | a823a70 | 2006-08-27 21:27:46 +0000 | [diff] [blame] | 3198 | propval.str = (char *) syncpartner; |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 3199 | ret = ptp_setdevicepropvalue(params, |
| 3200 | PTP_DPC_MTP_SynchronizationPartner, |
| 3201 | &propval, |
| 3202 | PTP_DTC_STR); |
| 3203 | if (ret != PTP_RC_OK) { |
| 3204 | add_ptp_error_to_errorstack(device, ret, "Error setting syncpartner."); |
Linus Walleij | 3065879 | 2006-08-19 22:18:55 +0000 | [diff] [blame] | 3205 | return -1; |
| 3206 | } |
Linus Walleij | 3065879 | 2006-08-19 22:18:55 +0000 | [diff] [blame] | 3207 | return 0; |
| 3208 | } |
| 3209 | |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 3210 | /** |
Linus Walleij | f5fcda3 | 2006-12-03 22:31:02 +0000 | [diff] [blame] | 3211 | * Checks if the device can stora a file of this size or |
| 3212 | * if it's too big. |
| 3213 | * @param device a pointer to the device. |
| 3214 | * @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] | 3215 | * @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] | 3216 | * @return 0 if the file fits, any other value means failure. |
| 3217 | */ |
Linus Walleij | 6bf68b4 | 2007-09-03 22:50:02 +0000 | [diff] [blame] | 3218 | static int check_if_file_fits(LIBMTP_mtpdevice_t *device, |
| 3219 | LIBMTP_devicestorage_t *storage, |
| 3220 | uint64_t const filesize) { |
Linus Walleij | f5fcda3 | 2006-12-03 22:31:02 +0000 | [diff] [blame] | 3221 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | f5fcda3 | 2006-12-03 22:31:02 +0000 | [diff] [blame] | 3222 | uint64_t freebytes; |
Linus Walleij | f5fcda3 | 2006-12-03 22:31:02 +0000 | [diff] [blame] | 3223 | int ret; |
| 3224 | |
| 3225 | // If we cannot check the storage, no big deal. |
| 3226 | if (!ptp_operation_issupported(params,PTP_OC_GetStorageInfo)) { |
| 3227 | return 0; |
| 3228 | } |
| 3229 | |
Linus Walleij | 6bf68b4 | 2007-09-03 22:50:02 +0000 | [diff] [blame] | 3230 | ret = get_storage_freespace(device, storage, &freebytes); |
Linus Walleij | f5fcda3 | 2006-12-03 22:31:02 +0000 | [diff] [blame] | 3231 | if (ret != 0) { |
Linus Walleij | 6bf68b4 | 2007-09-03 22:50:02 +0000 | [diff] [blame] | 3232 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, |
| 3233 | "check_if_file_fits(): error checking free storage."); |
Linus Walleij | f5fcda3 | 2006-12-03 22:31:02 +0000 | [diff] [blame] | 3234 | return -1; |
| 3235 | } else { |
Linus Walleij | fb28b63 | 2007-10-23 21:56:18 +0000 | [diff] [blame] | 3236 | // See if it fits. |
Linus Walleij | f5fcda3 | 2006-12-03 22:31:02 +0000 | [diff] [blame] | 3237 | if (filesize > freebytes) { |
Linus Walleij | f5fcda3 | 2006-12-03 22:31:02 +0000 | [diff] [blame] | 3238 | return -1; |
| 3239 | } |
| 3240 | } |
| 3241 | return 0; |
| 3242 | } |
| 3243 | |
| 3244 | |
Linus Walleij | f5fcda3 | 2006-12-03 22:31:02 +0000 | [diff] [blame] | 3245 | /** |
Linus Walleij | fa1374c | 2006-02-27 07:41:46 +0000 | [diff] [blame] | 3246 | * This function retrieves the current battery level on the device. |
| 3247 | * @param device a pointer to the device to get the battery level for. |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 3248 | * @param maximum_level a pointer to a variable that will hold the |
Linus Walleij | fa1374c | 2006-02-27 07:41:46 +0000 | [diff] [blame] | 3249 | * maximum level of the battery if the call was successful. |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 3250 | * @param current_level a pointer to a variable that will hold the |
Linus Walleij | fa1374c | 2006-02-27 07:41:46 +0000 | [diff] [blame] | 3251 | * current level of the battery if the call was successful. |
Linus Walleij | 545c779 | 2006-06-13 15:22:30 +0000 | [diff] [blame] | 3252 | * A value of 0 means that the device is on external power. |
Linus Walleij | fa1374c | 2006-02-27 07:41:46 +0000 | [diff] [blame] | 3253 | * @return 0 if the storage info was successfully retrieved, any other |
Linus Walleij | 8043934 | 2006-09-12 10:42:26 +0000 | [diff] [blame] | 3254 | * means failure. A typical cause of failure is that |
Linus Walleij | 545c779 | 2006-06-13 15:22:30 +0000 | [diff] [blame] | 3255 | * the device does not support the battery level property. |
Linus Walleij | fa1374c | 2006-02-27 07:41:46 +0000 | [diff] [blame] | 3256 | */ |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 3257 | int LIBMTP_Get_Batterylevel(LIBMTP_mtpdevice_t *device, |
| 3258 | uint8_t * const maximum_level, |
Linus Walleij | fa1374c | 2006-02-27 07:41:46 +0000 | [diff] [blame] | 3259 | uint8_t * const current_level) |
| 3260 | { |
Linus Walleij | b02a066 | 2006-04-25 08:05:09 +0000 | [diff] [blame] | 3261 | PTPPropertyValue propval; |
Linus Walleij | fa1374c | 2006-02-27 07:41:46 +0000 | [diff] [blame] | 3262 | uint16_t ret; |
Linus Walleij | 9b28da3 | 2006-03-16 13:47:58 +0000 | [diff] [blame] | 3263 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | 4096c88 | 2009-03-16 23:32:34 +0000 | [diff] [blame] | 3264 | PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo; |
Linus Walleij | fa1374c | 2006-02-27 07:41:46 +0000 | [diff] [blame] | 3265 | |
Linus Walleij | 545c779 | 2006-06-13 15:22:30 +0000 | [diff] [blame] | 3266 | *maximum_level = 0; |
| 3267 | *current_level = 0; |
| 3268 | |
Linus Walleij | 4096c88 | 2009-03-16 23:32:34 +0000 | [diff] [blame] | 3269 | if (FLAG_BROKEN_BATTERY_LEVEL(ptp_usb) || |
| 3270 | !ptp_property_issupported(params, PTP_DPC_BatteryLevel)) { |
Linus Walleij | 545c779 | 2006-06-13 15:22:30 +0000 | [diff] [blame] | 3271 | return -1; |
| 3272 | } |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 3273 | |
Linus Walleij | b02a066 | 2006-04-25 08:05:09 +0000 | [diff] [blame] | 3274 | ret = ptp_getdevicepropvalue(params, PTP_DPC_BatteryLevel, &propval, PTP_DTC_UINT8); |
| 3275 | if (ret != PTP_RC_OK) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 3276 | 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] | 3277 | return -1; |
| 3278 | } |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 3279 | |
Linus Walleij | fa1374c | 2006-02-27 07:41:46 +0000 | [diff] [blame] | 3280 | *maximum_level = device->maximum_battery_level; |
Linus Walleij | b02a066 | 2006-04-25 08:05:09 +0000 | [diff] [blame] | 3281 | *current_level = propval.u8; |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 3282 | |
Linus Walleij | fa1374c | 2006-02-27 07:41:46 +0000 | [diff] [blame] | 3283 | return 0; |
| 3284 | } |
| 3285 | |
Linus Walleij | 13374a4 | 2006-09-13 11:55:30 +0000 | [diff] [blame] | 3286 | |
| 3287 | /** |
| 3288 | * Formats device storage (if the device supports the operation). |
| 3289 | * WARNING: This WILL delete all data from the device. Make sure you've |
| 3290 | * got confirmation from the user BEFORE you call this function. |
| 3291 | * |
Linus Walleij | f849191 | 2006-12-15 10:23:30 +0000 | [diff] [blame] | 3292 | * @param device a pointer to the device containing the storage to format. |
| 3293 | * @param storage the actual storage to format. |
Linus Walleij | 13374a4 | 2006-09-13 11:55:30 +0000 | [diff] [blame] | 3294 | * @return 0 on success, any other value means failure. |
| 3295 | */ |
Linus Walleij | f849191 | 2006-12-15 10:23:30 +0000 | [diff] [blame] | 3296 | int LIBMTP_Format_Storage(LIBMTP_mtpdevice_t *device, LIBMTP_devicestorage_t *storage) |
Linus Walleij | 13374a4 | 2006-09-13 11:55:30 +0000 | [diff] [blame] | 3297 | { |
| 3298 | uint16_t ret; |
| 3299 | PTPParams *params = (PTPParams *) device->params; |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 3300 | |
Linus Walleij | 13374a4 | 2006-09-13 11:55:30 +0000 | [diff] [blame] | 3301 | if (!ptp_operation_issupported(params,PTP_OC_FormatStore)) { |
Linus Walleij | 5d533bb | 2007-07-17 21:48:57 +0000 | [diff] [blame] | 3302 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, |
| 3303 | "LIBMTP_Format_Storage(): device does not support formatting storage."); |
Linus Walleij | 13374a4 | 2006-09-13 11:55:30 +0000 | [diff] [blame] | 3304 | return -1; |
| 3305 | } |
Linus Walleij | f849191 | 2006-12-15 10:23:30 +0000 | [diff] [blame] | 3306 | ret = ptp_formatstore(params, storage->id); |
Linus Walleij | 13374a4 | 2006-09-13 11:55:30 +0000 | [diff] [blame] | 3307 | if (ret != PTP_RC_OK) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 3308 | 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] | 3309 | return -1; |
| 3310 | } |
| 3311 | return 0; |
| 3312 | } |
| 3313 | |
Linus Walleij | fa1374c | 2006-02-27 07:41:46 +0000 | [diff] [blame] | 3314 | /** |
Linus Walleij | 545c779 | 2006-06-13 15:22:30 +0000 | [diff] [blame] | 3315 | * Helper function to extract a unicode property off a device. |
Linus Walleij | e46f12e | 2006-06-22 17:53:25 +0000 | [diff] [blame] | 3316 | * This is the standard way of retrieveing unicode device |
| 3317 | * properties as described by the PTP spec. |
Linus Walleij | cf223e6 | 2006-06-19 09:31:53 +0000 | [diff] [blame] | 3318 | * @param device a pointer to the device to get the property from. |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 3319 | * @param unicstring a pointer to a pointer that will hold the |
Linus Walleij | cf223e6 | 2006-06-19 09:31:53 +0000 | [diff] [blame] | 3320 | * property after this call is completed. |
| 3321 | * @param property the property to retrieve. |
| 3322 | * @return 0 on success, any other value means failure. |
Linus Walleij | 545c779 | 2006-06-13 15:22:30 +0000 | [diff] [blame] | 3323 | */ |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 3324 | static int get_device_unicode_property(LIBMTP_mtpdevice_t *device, |
Linus Walleij | cf223e6 | 2006-06-19 09:31:53 +0000 | [diff] [blame] | 3325 | char **unicstring, uint16_t property) |
Linus Walleij | 545c779 | 2006-06-13 15:22:30 +0000 | [diff] [blame] | 3326 | { |
| 3327 | PTPPropertyValue propval; |
| 3328 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | 16571dc | 2006-08-17 20:27:46 +0000 | [diff] [blame] | 3329 | uint16_t *tmp; |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 3330 | uint16_t ret; |
Linus Walleij | 545c779 | 2006-06-13 15:22:30 +0000 | [diff] [blame] | 3331 | int i; |
| 3332 | |
| 3333 | if (!ptp_property_issupported(params, property)) { |
| 3334 | return -1; |
| 3335 | } |
| 3336 | |
Linus Walleij | cf223e6 | 2006-06-19 09:31:53 +0000 | [diff] [blame] | 3337 | // Unicode strings are 16bit unsigned integer arrays. |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 3338 | ret = ptp_getdevicepropvalue(params, |
| 3339 | property, |
| 3340 | &propval, |
| 3341 | PTP_DTC_AUINT16); |
| 3342 | if (ret != PTP_RC_OK) { |
| 3343 | // TODO: add a note on WHICH property that we failed to get. |
tedbullock | 4e51cb9 | 2007-02-15 11:48:34 +0000 | [diff] [blame] | 3344 | *unicstring = NULL; |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 3345 | 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] | 3346 | return -1; |
| 3347 | } |
| 3348 | |
| 3349 | // Extract the actual array. |
Linus Walleij | 16571dc | 2006-08-17 20:27:46 +0000 | [diff] [blame] | 3350 | // printf("Array of %d elements\n", propval.a.count); |
| 3351 | tmp = malloc((propval.a.count + 1)*sizeof(uint16_t)); |
Linus Walleij | 545c779 | 2006-06-13 15:22:30 +0000 | [diff] [blame] | 3352 | for (i = 0; i < propval.a.count; i++) { |
Linus Walleij | 16571dc | 2006-08-17 20:27:46 +0000 | [diff] [blame] | 3353 | tmp[i] = propval.a.v[i].u16; |
| 3354 | // printf("%04x ", tmp[i]); |
Linus Walleij | 545c779 | 2006-06-13 15:22:30 +0000 | [diff] [blame] | 3355 | } |
Linus Walleij | 16571dc | 2006-08-17 20:27:46 +0000 | [diff] [blame] | 3356 | tmp[propval.a.count] = 0x0000U; |
Linus Walleij | 545c779 | 2006-06-13 15:22:30 +0000 | [diff] [blame] | 3357 | free(propval.a.v); |
| 3358 | |
Linus Walleij | 3ec8631 | 2006-08-21 13:25:24 +0000 | [diff] [blame] | 3359 | *unicstring = utf16_to_utf8(device, tmp); |
Linus Walleij | 16571dc | 2006-08-17 20:27:46 +0000 | [diff] [blame] | 3360 | |
Linus Walleij | 545c779 | 2006-06-13 15:22:30 +0000 | [diff] [blame] | 3361 | free(tmp); |
| 3362 | |
| 3363 | return 0; |
| 3364 | } |
| 3365 | |
| 3366 | /** |
| 3367 | * This function returns the secure time as an XML document string from |
| 3368 | * the device. |
| 3369 | * @param device a pointer to the device to get the secure time for. |
| 3370 | * @param sectime the secure time string as an XML document or NULL if the call |
| 3371 | * failed or the secure time property is not supported. This string |
| 3372 | * must be <code>free()</code>:ed by the caller after use. |
| 3373 | * @return 0 on success, any other value means failure. |
| 3374 | */ |
Linus Walleij | 8ab5426 | 2006-06-21 07:12:28 +0000 | [diff] [blame] | 3375 | int LIBMTP_Get_Secure_Time(LIBMTP_mtpdevice_t *device, char ** const sectime) |
Linus Walleij | 545c779 | 2006-06-13 15:22:30 +0000 | [diff] [blame] | 3376 | { |
| 3377 | return get_device_unicode_property(device, sectime, PTP_DPC_MTP_SecureTime); |
| 3378 | } |
| 3379 | |
| 3380 | /** |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 3381 | * This function returns the device (public key) certificate as an |
Linus Walleij | 545c779 | 2006-06-13 15:22:30 +0000 | [diff] [blame] | 3382 | * XML document string from the device. |
| 3383 | * @param device a pointer to the device to get the device certificate for. |
| 3384 | * @param devcert the device certificate as an XML string or NULL if the call |
| 3385 | * failed or the device certificate property is not supported. This |
| 3386 | * string must be <code>free()</code>:ed by the caller after use. |
| 3387 | * @return 0 on success, any other value means failure. |
| 3388 | */ |
Linus Walleij | 8ab5426 | 2006-06-21 07:12:28 +0000 | [diff] [blame] | 3389 | int LIBMTP_Get_Device_Certificate(LIBMTP_mtpdevice_t *device, char ** const devcert) |
Linus Walleij | 545c779 | 2006-06-13 15:22:30 +0000 | [diff] [blame] | 3390 | { |
| 3391 | return get_device_unicode_property(device, devcert, PTP_DPC_MTP_DeviceCertificate); |
| 3392 | } |
| 3393 | |
| 3394 | /** |
Linus Walleij | 8ab5426 | 2006-06-21 07:12:28 +0000 | [diff] [blame] | 3395 | * This function retrieves a list of supported file types, i.e. the file |
| 3396 | * types that this device claims it supports, e.g. audio file types that |
| 3397 | * the device can play etc. This list is mitigated to |
| 3398 | * inlcude the file types that libmtp can handle, i.e. it will not list |
| 3399 | * filetypes that libmtp will handle internally like playlists and folders. |
| 3400 | * @param device a pointer to the device to get the filetype capabilities for. |
| 3401 | * @param filetypes a pointer to a pointer that will hold the list of |
| 3402 | * supported filetypes if the call was successful. This list must |
| 3403 | * be <code>free()</code>:ed by the caller after use. |
| 3404 | * @param length a pointer to a variable that will hold the length of the |
| 3405 | * list of supported filetypes if the call was successful. |
| 3406 | * @return 0 on success, any other value means failure. |
| 3407 | * @see LIBMTP_Get_Filetype_Description() |
| 3408 | */ |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 3409 | 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] | 3410 | uint16_t * const length) |
| 3411 | { |
| 3412 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | a3544f6 | 2007-11-30 01:20:04 +0000 | [diff] [blame] | 3413 | PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo; |
Linus Walleij | 8ab5426 | 2006-06-21 07:12:28 +0000 | [diff] [blame] | 3414 | uint16_t *localtypes; |
| 3415 | uint16_t localtypelen; |
| 3416 | uint32_t i; |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 3417 | |
Linus Walleij | 8ab5426 | 2006-06-21 07:12:28 +0000 | [diff] [blame] | 3418 | // This is more memory than needed if there are unknown types, but what the heck. |
| 3419 | localtypes = (uint16_t *) malloc(params->deviceinfo.ImageFormats_len * sizeof(uint16_t)); |
| 3420 | localtypelen = 0; |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 3421 | |
Linus Walleij | 8ab5426 | 2006-06-21 07:12:28 +0000 | [diff] [blame] | 3422 | for (i=0;i<params->deviceinfo.ImageFormats_len;i++) { |
| 3423 | uint16_t localtype = map_ptp_type_to_libmtp_type(params->deviceinfo.ImageFormats[i]); |
| 3424 | if (localtype != LIBMTP_FILETYPE_UNKNOWN) { |
| 3425 | localtypes[localtypelen] = localtype; |
| 3426 | localtypelen++; |
| 3427 | } |
| 3428 | } |
Linus Walleij | a3544f6 | 2007-11-30 01:20:04 +0000 | [diff] [blame] | 3429 | // The forgotten Ogg support on YP-10 and others... |
Linus Walleij | fec4d56 | 2008-06-01 22:30:36 +0000 | [diff] [blame] | 3430 | if (FLAG_OGG_IS_UNKNOWN(ptp_usb)) { |
Linus Walleij | a3544f6 | 2007-11-30 01:20:04 +0000 | [diff] [blame] | 3431 | localtypes = (uint16_t *) realloc(localtypes, (params->deviceinfo.ImageFormats_len+1) * sizeof(uint16_t)); |
| 3432 | localtypes[localtypelen] = LIBMTP_FILETYPE_OGG; |
| 3433 | localtypelen++; |
| 3434 | } |
Linus Walleij | 89bb1cd | 2009-07-24 21:03:36 +0000 | [diff] [blame] | 3435 | // The forgotten FLAC support on Cowon iAudio S9 and others... |
| 3436 | if (FLAG_FLAC_IS_UNKNOWN(ptp_usb)) { |
| 3437 | localtypes = (uint16_t *) realloc(localtypes, (params->deviceinfo.ImageFormats_len+1) * sizeof(uint16_t)); |
| 3438 | localtypes[localtypelen] = LIBMTP_FILETYPE_FLAC; |
| 3439 | localtypelen++; |
| 3440 | } |
Linus Walleij | 8ab5426 | 2006-06-21 07:12:28 +0000 | [diff] [blame] | 3441 | |
| 3442 | *filetypes = localtypes; |
| 3443 | *length = localtypelen; |
| 3444 | |
| 3445 | return 0; |
| 3446 | } |
| 3447 | |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 3448 | /** |
Linus Walleij | 5b452bd | 2007-12-28 23:34:18 +0000 | [diff] [blame] | 3449 | * This function updates all the storage id's of a device and their |
| 3450 | * properties, then creates a linked list and puts the list head into |
Linus Walleij | f849191 | 2006-12-15 10:23:30 +0000 | [diff] [blame] | 3451 | * the device struct. It also optionally sorts this list. If you want |
| 3452 | * to display storage information in your application you should call |
Linus Walleij | 5b452bd | 2007-12-28 23:34:18 +0000 | [diff] [blame] | 3453 | * this function, then dereference the device struct |
| 3454 | * (<code>device->storage</code>) to get out information on the storage. |
| 3455 | * |
| 3456 | * You need to call this everytime you want to update the |
| 3457 | * <code>device->storage</code> list, for example anytime you need |
| 3458 | * to check available storage somewhere. |
| 3459 | * |
| 3460 | * <b>WARNING:</b> since this list is dynamically updated, do not |
| 3461 | * reference its fields in external applications by pointer! E.g |
| 3462 | * do not put a reference to any <code>char *</code> field. instead |
| 3463 | * <code>strncpy()</code> it! |
Linus Walleij | f849191 | 2006-12-15 10:23:30 +0000 | [diff] [blame] | 3464 | * |
Linus Walleij | e1b88e8 | 2008-07-02 20:12:53 +0000 | [diff] [blame] | 3465 | * @param device a pointer to the device to get the storage for. |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 3466 | * @param sortby an integer that determines the sorting of the storage list. |
| 3467 | * Valid sort methods are defined in libmtp.h with beginning with |
| 3468 | * LIBMTP_STORAGE_SORTBY_. 0 or LIBMTP_STORAGE_SORTBY_NOTSORTED to not |
| 3469 | * sort. |
| 3470 | * @return 0 on success, 1 success but only with storage id's, storage |
| 3471 | * properities could not be retrieved and -1 means failure. |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 3472 | */ |
| 3473 | int LIBMTP_Get_Storage(LIBMTP_mtpdevice_t *device, int const sortby) |
| 3474 | { |
| 3475 | uint32_t i = 0; |
| 3476 | PTPStorageInfo storageInfo; |
| 3477 | PTPParams *params = (PTPParams *) device->params; |
| 3478 | PTPStorageIDs storageIDs; |
Linus Walleij | e1ac07e | 2006-12-14 19:38:59 +0000 | [diff] [blame] | 3479 | LIBMTP_devicestorage_t *storage = NULL; |
| 3480 | LIBMTP_devicestorage_t *storageprev = NULL; |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 3481 | |
| 3482 | if (device->storage != NULL) |
| 3483 | free_storage_list(device); |
| 3484 | |
| 3485 | // if (!ptp_operation_issupported(params,PTP_OC_GetStorageIDs)) |
| 3486 | // return -1; |
Richard Low | bd14bf4 | 2007-07-21 11:25:11 +0000 | [diff] [blame] | 3487 | if (ptp_getstorageids (params, &storageIDs) != PTP_RC_OK) |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 3488 | return -1; |
| 3489 | if (storageIDs.n < 1) |
| 3490 | return -1; |
| 3491 | |
| 3492 | if (!ptp_operation_issupported(params,PTP_OC_GetStorageInfo)) { |
| 3493 | for (i = 0; i < storageIDs.n; i++) { |
| 3494 | |
Linus Walleij | e1ac07e | 2006-12-14 19:38:59 +0000 | [diff] [blame] | 3495 | storage = (LIBMTP_devicestorage_t *) malloc(sizeof(LIBMTP_devicestorage_t)); |
| 3496 | storage->prev = storageprev; |
| 3497 | if (storageprev != NULL) |
| 3498 | storageprev->next = storage; |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 3499 | if (device->storage == NULL) |
Linus Walleij | e1ac07e | 2006-12-14 19:38:59 +0000 | [diff] [blame] | 3500 | device->storage = storage; |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 3501 | |
Linus Walleij | e1ac07e | 2006-12-14 19:38:59 +0000 | [diff] [blame] | 3502 | storage->id = storageIDs.Storage[i]; |
| 3503 | storage->StorageType = PTP_ST_Undefined; |
| 3504 | storage->FilesystemType = PTP_FST_Undefined; |
| 3505 | storage->AccessCapability = PTP_AC_ReadWrite; |
| 3506 | storage->MaxCapacity = (uint64_t) -1; |
| 3507 | storage->FreeSpaceInBytes = (uint64_t) -1; |
| 3508 | storage->FreeSpaceInObjects = (uint64_t) -1; |
| 3509 | storage->StorageDescription = strdup("Unknown storage"); |
| 3510 | storage->VolumeIdentifier = strdup("Unknown volume"); |
| 3511 | storage->next = NULL; |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 3512 | |
Linus Walleij | e1ac07e | 2006-12-14 19:38:59 +0000 | [diff] [blame] | 3513 | storageprev = storage; |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 3514 | } |
| 3515 | free(storageIDs.Storage); |
| 3516 | return 1; |
| 3517 | } else { |
| 3518 | for (i = 0; i < storageIDs.n; i++) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 3519 | uint16_t ret; |
| 3520 | ret = ptp_getstorageinfo(params, storageIDs.Storage[i], &storageInfo); |
| 3521 | if (ret != PTP_RC_OK) { |
| 3522 | 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] | 3523 | if (device->storage != NULL) { |
| 3524 | free_storage_list(device); |
| 3525 | } |
| 3526 | return -1; |
| 3527 | } |
| 3528 | |
Linus Walleij | e1ac07e | 2006-12-14 19:38:59 +0000 | [diff] [blame] | 3529 | storage = (LIBMTP_devicestorage_t *) malloc(sizeof(LIBMTP_devicestorage_t)); |
| 3530 | storage->prev = storageprev; |
| 3531 | if (storageprev != NULL) |
| 3532 | storageprev->next = storage; |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 3533 | if (device->storage == NULL) |
Linus Walleij | e1ac07e | 2006-12-14 19:38:59 +0000 | [diff] [blame] | 3534 | device->storage = storage; |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 3535 | |
Linus Walleij | e1ac07e | 2006-12-14 19:38:59 +0000 | [diff] [blame] | 3536 | storage->id = storageIDs.Storage[i]; |
| 3537 | storage->StorageType = storageInfo.StorageType; |
| 3538 | storage->FilesystemType = storageInfo.FilesystemType; |
| 3539 | storage->AccessCapability = storageInfo.AccessCapability; |
| 3540 | storage->MaxCapacity = storageInfo.MaxCapability; |
| 3541 | storage->FreeSpaceInBytes = storageInfo.FreeSpaceInBytes; |
| 3542 | storage->FreeSpaceInObjects = storageInfo.FreeSpaceInImages; |
| 3543 | storage->StorageDescription = storageInfo.StorageDescription; |
| 3544 | storage->VolumeIdentifier = storageInfo.VolumeLabel; |
| 3545 | storage->next = NULL; |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 3546 | |
Linus Walleij | e1ac07e | 2006-12-14 19:38:59 +0000 | [diff] [blame] | 3547 | storageprev = storage; |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 3548 | } |
| 3549 | |
Linus Walleij | 5c1499e | 2009-02-21 06:54:29 +0000 | [diff] [blame] | 3550 | if (storage != NULL) |
| 3551 | storage->next = NULL; |
Linus Walleij | 9e1b081 | 2006-12-12 19:22:02 +0000 | [diff] [blame] | 3552 | |
| 3553 | sort_storage_by(device,sortby); |
| 3554 | free(storageIDs.Storage); |
| 3555 | return 0; |
| 3556 | } |
| 3557 | } |
| 3558 | |
| 3559 | /** |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 3560 | * This creates a new file metadata structure and allocates memory |
| 3561 | * for it. Notice that if you add strings to this structure they |
| 3562 | * will be freed by the corresponding <code>LIBMTP_destroy_file_t</code> |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 3563 | * operation later, so be careful of using strdup() when assigning |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 3564 | * strings, e.g.: |
| 3565 | * |
| 3566 | * <pre> |
| 3567 | * LIBMTP_file_t *file = LIBMTP_new_file_t(); |
| 3568 | * file->filename = strdup(namestr); |
| 3569 | * .... |
| 3570 | * LIBMTP_destroy_file_t(file); |
| 3571 | * </pre> |
| 3572 | * |
| 3573 | * @return a pointer to the newly allocated metadata structure. |
| 3574 | * @see LIBMTP_destroy_file_t() |
| 3575 | */ |
| 3576 | LIBMTP_file_t *LIBMTP_new_file_t(void) |
| 3577 | { |
| 3578 | LIBMTP_file_t *new = (LIBMTP_file_t *) malloc(sizeof(LIBMTP_file_t)); |
| 3579 | if (new == NULL) { |
| 3580 | return NULL; |
| 3581 | } |
| 3582 | new->filename = NULL; |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 3583 | new->item_id = 0; |
| 3584 | new->parent_id = 0; |
| 3585 | new->storage_id = 0; |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 3586 | new->filesize = 0; |
Richard Low | d3b1702 | 2009-04-11 12:37:39 +0000 | [diff] [blame] | 3587 | new->modificationdate = 0; |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 3588 | new->filetype = LIBMTP_FILETYPE_UNKNOWN; |
| 3589 | new->next = NULL; |
| 3590 | return new; |
| 3591 | } |
| 3592 | |
| 3593 | /** |
| 3594 | * This destroys a file metadata structure and deallocates the memory |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 3595 | * used by it, including any strings. Never use a file metadata |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 3596 | * structure again after calling this function on it. |
| 3597 | * @param file the file metadata to destroy. |
| 3598 | * @see LIBMTP_new_file_t() |
| 3599 | */ |
| 3600 | void LIBMTP_destroy_file_t(LIBMTP_file_t *file) |
| 3601 | { |
| 3602 | if (file == NULL) { |
| 3603 | return; |
| 3604 | } |
| 3605 | if (file->filename != NULL) |
| 3606 | free(file->filename); |
| 3607 | free(file); |
| 3608 | return; |
| 3609 | } |
| 3610 | |
| 3611 | /** |
mopoke | 3136444 | 2006-11-20 04:53:04 +0000 | [diff] [blame] | 3612 | * THIS FUNCTION IS DEPRECATED. PLEASE UPDATE YOUR CODE IN ORDER |
Richard Low | dc0b6c7 | 2006-11-13 09:22:23 +0000 | [diff] [blame] | 3613 | * NOT TO USE IT. |
| 3614 | * @see LIBMTP_Get_Filelisting_With_Callback() |
| 3615 | */ |
| 3616 | LIBMTP_file_t *LIBMTP_Get_Filelisting(LIBMTP_mtpdevice_t *device) |
| 3617 | { |
| 3618 | printf("WARNING: LIBMTP_Get_Filelisting() is deprecated.\n"); |
| 3619 | printf("WARNING: please update your code to use LIBMTP_Get_Filelisting_With_Callback()\n"); |
| 3620 | return LIBMTP_Get_Filelisting_With_Callback(device, NULL, NULL); |
| 3621 | } |
| 3622 | |
| 3623 | /** |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 3624 | * This returns a long list of all files available |
Linus Walleij | 25d5c21 | 2008-08-14 06:49:13 +0000 | [diff] [blame] | 3625 | * on the current MTP device. Folders will not be returned, but abstract |
| 3626 | * entities like playlists and albums will show up as "files". Typical usage: |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 3627 | * |
| 3628 | * <pre> |
| 3629 | * LIBMTP_file_t *filelist; |
| 3630 | * |
Richard Low | dc0b6c7 | 2006-11-13 09:22:23 +0000 | [diff] [blame] | 3631 | * filelist = LIBMTP_Get_Filelisting_With_Callback(device, callback, data); |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 3632 | * while (filelist != NULL) { |
| 3633 | * LIBMTP_file_t *tmp; |
| 3634 | * |
| 3635 | * // Do something on each element in the list here... |
| 3636 | * tmp = filelist; |
| 3637 | * filelist = filelist->next; |
| 3638 | * LIBMTP_destroy_file_t(tmp); |
| 3639 | * } |
| 3640 | * </pre> |
| 3641 | * |
Linus Walleij | 25d5c21 | 2008-08-14 06:49:13 +0000 | [diff] [blame] | 3642 | * If you want to group your file listing by storage (per storage unit) or |
| 3643 | * arrange files into folders, you must dereference the <code>storage_id</code> |
| 3644 | * and/or <code>parent_id</code> field of the returned <code>LIBMTP_file_t</code> |
| 3645 | * struct. To arrange by folders or files you typically have to create the proper |
| 3646 | * trees by calls to <code>LIBMTP_Get_Storage()</code> and/or |
| 3647 | * <code>LIBMTP_Get_Folder_List()</code> first. |
| 3648 | * |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 3649 | * @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] | 3650 | * @param callback a function to be called during the tracklisting retrieveal |
Linus Walleij | 25d5c21 | 2008-08-14 06:49:13 +0000 | [diff] [blame] | 3651 | * for displaying progress bars etc, or NULL if you don't want |
| 3652 | * any callbacks. |
Richard Low | dc0b6c7 | 2006-11-13 09:22:23 +0000 | [diff] [blame] | 3653 | * @param data a user-defined pointer that is passed along to |
Linus Walleij | 25d5c21 | 2008-08-14 06:49:13 +0000 | [diff] [blame] | 3654 | * the <code>progress</code> function in order to |
| 3655 | * pass along some user defined data to the progress |
| 3656 | * updates. If not used, set this to NULL. |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 3657 | * @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] | 3658 | * field of the <code>LIBMTP_file_t</code> data structure. |
| 3659 | * Each of the metadata tags must be freed after use, and may |
| 3660 | * contain only partial metadata information, i.e. one or several |
| 3661 | * fields may be NULL or 0. |
Linus Walleij | 2e4b5f9 | 2006-06-16 14:00:49 +0000 | [diff] [blame] | 3662 | * @see LIBMTP_Get_Filemetadata() |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 3663 | */ |
Richard Low | dc0b6c7 | 2006-11-13 09:22:23 +0000 | [diff] [blame] | 3664 | LIBMTP_file_t *LIBMTP_Get_Filelisting_With_Callback(LIBMTP_mtpdevice_t *device, |
| 3665 | LIBMTP_progressfunc_t const callback, |
| 3666 | void const * const data) |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 3667 | { |
| 3668 | uint32_t i = 0; |
| 3669 | LIBMTP_file_t *retfiles = NULL; |
| 3670 | LIBMTP_file_t *curfile = NULL; |
| 3671 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | d9d28d5 | 2007-08-04 19:01:18 +0000 | [diff] [blame] | 3672 | PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo; |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 3673 | uint16_t ret; |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 3674 | |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 3675 | // Get all the handles if we haven't already done that |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 3676 | if (params->nrofobjects == 0) { |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 3677 | flush_handles(device); |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 3678 | } |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 3679 | |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 3680 | for (i = 0; i < params->nrofobjects; i++) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 3681 | LIBMTP_file_t *file; |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 3682 | PTPObject *ob, *xob; |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 3683 | |
Richard Low | dc0b6c7 | 2006-11-13 09:22:23 +0000 | [diff] [blame] | 3684 | if (callback != NULL) |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 3685 | callback(i, params->nrofobjects, data); |
mopoke | 3136444 | 2006-11-20 04:53:04 +0000 | [diff] [blame] | 3686 | |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 3687 | ob = ¶ms->objects[i]; |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 3688 | |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 3689 | if (ob->oi.ObjectFormat == PTP_OFC_Association) { |
Linus Walleij | d9d28d5 | 2007-08-04 19:01:18 +0000 | [diff] [blame] | 3690 | // MTP use this object format for folders which means |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 3691 | // these "files" will turn up on a folder listing instead. |
| 3692 | continue; |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 3693 | } |
| 3694 | |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 3695 | // Allocate a new file type |
| 3696 | file = LIBMTP_new_file_t(); |
| 3697 | |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 3698 | file->parent_id = ob->oi.ParentObject; |
| 3699 | file->storage_id = ob->oi.StorageID; |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 3700 | |
Linus Walleij | d9d28d5 | 2007-08-04 19:01:18 +0000 | [diff] [blame] | 3701 | // This is some sort of unique ID so we can keep track of the track. |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 3702 | file->item_id = ob->oid; |
Linus Walleij | d9d28d5 | 2007-08-04 19:01:18 +0000 | [diff] [blame] | 3703 | |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 3704 | // Set the filetype |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 3705 | file->filetype = map_ptp_type_to_libmtp_type(ob->oi.ObjectFormat); |
Richard Low | d3b1702 | 2009-04-11 12:37:39 +0000 | [diff] [blame] | 3706 | |
| 3707 | // Set the modification date |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 3708 | file->modificationdate = ob->oi.ModificationDate; |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 3709 | |
| 3710 | // Original file-specific properties |
Linus Walleij | d9d28d5 | 2007-08-04 19:01:18 +0000 | [diff] [blame] | 3711 | // We only have 32-bit file size here; if we find it, we use the |
| 3712 | // PTP_OPC_ObjectSize property which has 64bit precision. |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 3713 | file->filesize = ob->oi.ObjectCompressedSize; |
| 3714 | if (ob->oi.Filename != NULL) { |
| 3715 | file->filename = strdup(ob->oi.Filename); |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 3716 | } |
| 3717 | |
Linus Walleij | d9d28d5 | 2007-08-04 19:01:18 +0000 | [diff] [blame] | 3718 | /* |
Linus Walleij | 89bb1cd | 2009-07-24 21:03:36 +0000 | [diff] [blame] | 3719 | * A special quirk for devices that doesn't quite |
Linus Walleij | c8abc92 | 2008-09-28 18:38:42 +0000 | [diff] [blame] | 3720 | * remember that some files marked as "unknown" type are |
Linus Walleij | 89bb1cd | 2009-07-24 21:03:36 +0000 | [diff] [blame] | 3721 | * actually OGG or FLAC files. We look at the filename extension |
| 3722 | * and see if it happens that this was atleast named "ogg" or "flac" |
Linus Walleij | c8abc92 | 2008-09-28 18:38:42 +0000 | [diff] [blame] | 3723 | * and fall back on this heuristic approach in that case, |
| 3724 | * for these bugged devices only. |
| 3725 | */ |
Linus Walleij | 89bb1cd | 2009-07-24 21:03:36 +0000 | [diff] [blame] | 3726 | if (file->filetype == LIBMTP_FILETYPE_UNKNOWN) { |
| 3727 | if ((FLAG_IRIVER_OGG_ALZHEIMER(ptp_usb) || |
| 3728 | FLAG_OGG_IS_UNKNOWN(ptp_usb)) && |
| 3729 | has_ogg_extension(file->filename)) |
| 3730 | file->filetype = LIBMTP_FILETYPE_OGG; |
| 3731 | if (FLAG_FLAC_IS_UNKNOWN(ptp_usb) && |
| 3732 | has_flac_extension(file->filename)) |
| 3733 | file->filetype = LIBMTP_FILETYPE_FLAC; |
Linus Walleij | c8abc92 | 2008-09-28 18:38:42 +0000 | [diff] [blame] | 3734 | } |
| 3735 | |
| 3736 | /* |
Linus Walleij | d9d28d5 | 2007-08-04 19:01:18 +0000 | [diff] [blame] | 3737 | * If we have a cached, large set of metadata, then use it! |
| 3738 | */ |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 3739 | ret = ptp_object_want (params, ob->oid, PTPOBJECT_MTPPROPLIST_LOADED, &xob); |
| 3740 | if (ob->mtpprops) { |
| 3741 | MTPProperties *prop = ob->mtpprops; |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 3742 | int i; |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 3743 | |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 3744 | for (i=0;i<ob->nrofmtpprops;i++) { |
Linus Walleij | d9d28d5 | 2007-08-04 19:01:18 +0000 | [diff] [blame] | 3745 | // Pick ObjectSize here... |
| 3746 | if (prop->property == PTP_OPC_ObjectSize) { |
Linus Walleij | ddaba2f | 2007-10-02 21:20:30 +0000 | [diff] [blame] | 3747 | if (device->object_bitsize == 64) { |
Linus Walleij | ca2a170 | 2007-10-02 20:41:45 +0000 | [diff] [blame] | 3748 | file->filesize = prop->propval.u64; |
| 3749 | } else { |
Linus Walleij | ddaba2f | 2007-10-02 21:20:30 +0000 | [diff] [blame] | 3750 | file->filesize = prop->propval.u32; |
Linus Walleij | ca2a170 | 2007-10-02 20:41:45 +0000 | [diff] [blame] | 3751 | } |
Linus Walleij | d9d28d5 | 2007-08-04 19:01:18 +0000 | [diff] [blame] | 3752 | break; |
| 3753 | } |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 3754 | prop++; |
Linus Walleij | d9d28d5 | 2007-08-04 19:01:18 +0000 | [diff] [blame] | 3755 | } |
Linus Walleij | d9d28d5 | 2007-08-04 19:01:18 +0000 | [diff] [blame] | 3756 | } else { |
| 3757 | uint16_t *props = NULL; |
| 3758 | uint32_t propcnt = 0; |
| 3759 | |
| 3760 | // First see which properties can be retrieved for this object format |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 3761 | ret = ptp_mtp_getobjectpropssupported(params, ob->oi.ObjectFormat, &propcnt, &props); |
Linus Walleij | d9d28d5 | 2007-08-04 19:01:18 +0000 | [diff] [blame] | 3762 | if (ret != PTP_RC_OK) { |
| 3763 | add_ptp_error_to_errorstack(device, ret, "LIBMTP_Get_Filelisting_With_Callback(): call to ptp_mtp_getobjectpropssupported() failed."); |
| 3764 | // Silently fall through. |
| 3765 | } else { |
Richard Low | d7bcab5 | 2007-09-16 16:30:20 +0000 | [diff] [blame] | 3766 | int i; |
Linus Walleij | d9d28d5 | 2007-08-04 19:01:18 +0000 | [diff] [blame] | 3767 | for (i=0;i<propcnt;i++) { |
| 3768 | switch (props[i]) { |
| 3769 | case PTP_OPC_ObjectSize: |
Linus Walleij | ddaba2f | 2007-10-02 21:20:30 +0000 | [diff] [blame] | 3770 | if (device->object_bitsize == 64) { |
| 3771 | file->filesize = get_u64_from_object(device, file->item_id, PTP_OPC_ObjectSize, 0); |
| 3772 | } else { |
| 3773 | 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] | 3774 | } |
Linus Walleij | d9d28d5 | 2007-08-04 19:01:18 +0000 | [diff] [blame] | 3775 | break; |
| 3776 | default: |
| 3777 | break; |
| 3778 | } |
| 3779 | } |
| 3780 | free(props); |
| 3781 | } |
| 3782 | } |
Linus Walleij | d9d28d5 | 2007-08-04 19:01:18 +0000 | [diff] [blame] | 3783 | |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 3784 | // Add track to a list that will be returned afterwards. |
| 3785 | if (retfiles == NULL) { |
| 3786 | retfiles = file; |
| 3787 | curfile = file; |
| 3788 | } else { |
| 3789 | curfile->next = file; |
| 3790 | curfile = file; |
| 3791 | } |
| 3792 | |
| 3793 | // Call listing callback |
| 3794 | // double progressPercent = (double)i*(double)100.0 / (double)params->handles.n; |
| 3795 | |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 3796 | } // Handle counting loop |
| 3797 | return retfiles; |
| 3798 | } |
| 3799 | |
| 3800 | /** |
Linus Walleij | 2e4b5f9 | 2006-06-16 14:00:49 +0000 | [diff] [blame] | 3801 | * This function retrieves the metadata for a single file off |
| 3802 | * the device. |
| 3803 | * |
| 3804 | * Do not call this function repeatedly! The file handles are linearly |
| 3805 | * searched O(n) and the call may involve (slow) USB traffic, so use |
| 3806 | * <code>LIBMTP_Get_Filelisting()</code> and cache the file, preferably |
| 3807 | * as an efficient data structure such as a hash list. |
| 3808 | * |
Linus Walleij | 0801cd5 | 2008-01-29 22:19:15 +0000 | [diff] [blame] | 3809 | * Incidentally this function will return metadata for |
| 3810 | * a folder (association) as well, but this is not a proper use |
| 3811 | * of it, it is intended for file manipulation, not folder manipulation. |
| 3812 | * |
Linus Walleij | 2e4b5f9 | 2006-06-16 14:00:49 +0000 | [diff] [blame] | 3813 | * @param device a pointer to the device to get the file metadata from. |
| 3814 | * @param fileid the object ID of the file that you want the metadata for. |
| 3815 | * @return a metadata entry on success or NULL on failure. |
| 3816 | * @see LIBMTP_Get_Filelisting() |
| 3817 | */ |
| 3818 | LIBMTP_file_t *LIBMTP_Get_Filemetadata(LIBMTP_mtpdevice_t *device, uint32_t const fileid) |
| 3819 | { |
| 3820 | uint32_t i = 0; |
| 3821 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 3822 | uint16_t ret; |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 3823 | PTPObject *ob; |
| 3824 | LIBMTP_file_t *file; |
Linus Walleij | 2e4b5f9 | 2006-06-16 14:00:49 +0000 | [diff] [blame] | 3825 | |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 3826 | // Get all the handles if we haven't already done that |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 3827 | if (params->nrofobjects == 0) { |
Linus Walleij | 2e4b5f9 | 2006-06-16 14:00:49 +0000 | [diff] [blame] | 3828 | flush_handles(device); |
| 3829 | } |
| 3830 | |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 3831 | ret = ptp_object_want (params, fileid, PTPOBJECT_OBJECTINFO_LOADED|PTPOBJECT_MTPPROPLIST_LOADED, &ob); |
| 3832 | if (ret != PTP_RC_OK) |
| 3833 | return NULL; |
Linus Walleij | 2e4b5f9 | 2006-06-16 14:00:49 +0000 | [diff] [blame] | 3834 | |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 3835 | // Allocate a new file type |
| 3836 | file = LIBMTP_new_file_t(); |
| 3837 | |
| 3838 | file->parent_id = ob->oi.ParentObject; |
| 3839 | file->storage_id = ob->oi.StorageID; |
Linus Walleij | 2e4b5f9 | 2006-06-16 14:00:49 +0000 | [diff] [blame] | 3840 | |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 3841 | // Set the filetype |
| 3842 | file->filetype = map_ptp_type_to_libmtp_type(ob->oi.ObjectFormat); |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 3843 | |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 3844 | // Original file-specific properties |
| 3845 | |
| 3846 | // We only have 32-bit file size here; later we use the PTP_OPC_ObjectSize property |
| 3847 | file->filesize = ob->oi.ObjectCompressedSize; |
| 3848 | if (ob->oi.Filename != NULL) { |
| 3849 | file->filename = strdup(ob->oi.Filename); |
Linus Walleij | 2e4b5f9 | 2006-06-16 14:00:49 +0000 | [diff] [blame] | 3850 | } |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 3851 | |
| 3852 | // This is some sort of unique ID so we can keep track of the file. |
| 3853 | file->item_id = fileid; |
| 3854 | |
| 3855 | /* |
| 3856 | * If we have a cached, large set of metadata, then use it! |
| 3857 | */ |
| 3858 | if (ob->mtpprops) { |
| 3859 | MTPProperties *prop = ob->mtpprops; |
| 3860 | |
| 3861 | for (i=0;i<ob->nrofmtpprops;i++,prop++) { |
| 3862 | // Pick ObjectSize here... |
| 3863 | if (prop->property == PTP_OPC_ObjectSize) { |
| 3864 | // This may already be set, but this 64bit precision value |
| 3865 | // is better than the PTP 32bit value, so let it override. |
| 3866 | if (device->object_bitsize == 64) { |
| 3867 | file->filesize = prop->propval.u64; |
| 3868 | } else { |
| 3869 | file->filesize = prop->propval.u32; |
| 3870 | } |
| 3871 | break; |
| 3872 | } |
| 3873 | } |
| 3874 | } else { |
| 3875 | uint16_t *props = NULL; |
| 3876 | uint32_t propcnt = 0; |
| 3877 | |
| 3878 | // First see which properties can be retrieved for this object format |
| 3879 | ret = ptp_mtp_getobjectpropssupported(params, map_libmtp_type_to_ptp_type(file->filetype), &propcnt, &props); |
| 3880 | if (ret != PTP_RC_OK) { |
| 3881 | add_ptp_error_to_errorstack(device, ret, "LIBMTP_Get_Filemetadata(): call to ptp_mtp_getobjectpropssupported() failed."); |
| 3882 | // Silently fall through. |
| 3883 | } else { |
| 3884 | for (i=0;i<propcnt;i++) { |
| 3885 | switch (props[i]) { |
| 3886 | case PTP_OPC_ObjectSize: |
| 3887 | if (device->object_bitsize == 64) { |
| 3888 | file->filesize = get_u64_from_object(device, file->item_id, PTP_OPC_ObjectSize, 0); |
| 3889 | } else { |
| 3890 | file->filesize = get_u32_from_object(device, file->item_id, PTP_OPC_ObjectSize, 0); |
| 3891 | } |
| 3892 | break; |
| 3893 | default: |
| 3894 | break; |
| 3895 | } |
| 3896 | } |
| 3897 | free(props); |
| 3898 | } |
| 3899 | } |
| 3900 | |
| 3901 | return file; |
Linus Walleij | 2e4b5f9 | 2006-06-16 14:00:49 +0000 | [diff] [blame] | 3902 | } |
| 3903 | |
| 3904 | /** |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 3905 | * This creates a new track metadata structure and allocates memory |
| 3906 | * for it. Notice that if you add strings to this structure they |
| 3907 | * will be freed by the corresponding <code>LIBMTP_destroy_track_t</code> |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 3908 | * operation later, so be careful of using strdup() when assigning |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 3909 | * strings, e.g.: |
| 3910 | * |
Linus Walleij | 17e39f7 | 2006-02-23 15:54:28 +0000 | [diff] [blame] | 3911 | * <pre> |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 3912 | * LIBMTP_track_t *track = LIBMTP_new_track_t(); |
| 3913 | * track->title = strdup(titlestr); |
| 3914 | * .... |
| 3915 | * LIBMTP_destroy_track_t(track); |
Linus Walleij | 17e39f7 | 2006-02-23 15:54:28 +0000 | [diff] [blame] | 3916 | * </pre> |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 3917 | * |
| 3918 | * @return a pointer to the newly allocated metadata structure. |
| 3919 | * @see LIBMTP_destroy_track_t() |
| 3920 | */ |
| 3921 | LIBMTP_track_t *LIBMTP_new_track_t(void) |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 3922 | { |
| 3923 | LIBMTP_track_t *new = (LIBMTP_track_t *) malloc(sizeof(LIBMTP_track_t)); |
| 3924 | if (new == NULL) { |
| 3925 | return NULL; |
| 3926 | } |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 3927 | new->item_id = 0; |
| 3928 | new->parent_id = 0; |
| 3929 | new->storage_id = 0; |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 3930 | new->title = NULL; |
| 3931 | new->artist = NULL; |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 3932 | new->composer = NULL; |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 3933 | new->album = NULL; |
| 3934 | new->genre = NULL; |
| 3935 | new->date = NULL; |
| 3936 | new->filename = NULL; |
| 3937 | new->duration = 0; |
| 3938 | new->tracknumber = 0; |
| 3939 | new->filesize = 0; |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 3940 | new->filetype = LIBMTP_FILETYPE_UNKNOWN; |
Linus Walleij | cf223e6 | 2006-06-19 09:31:53 +0000 | [diff] [blame] | 3941 | new->samplerate = 0; |
| 3942 | new->nochannels = 0; |
| 3943 | new->wavecodec = 0; |
| 3944 | new->bitrate = 0; |
| 3945 | new->bitratetype = 0; |
| 3946 | new->rating = 0; |
| 3947 | new->usecount = 0; |
Richard Low | d3b1702 | 2009-04-11 12:37:39 +0000 | [diff] [blame] | 3948 | new->modificationdate = 0; |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 3949 | new->next = NULL; |
| 3950 | return new; |
| 3951 | } |
| 3952 | |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 3953 | /** |
| 3954 | * This destroys a track metadata structure and deallocates the memory |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 3955 | * used by it, including any strings. Never use a track metadata |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 3956 | * structure again after calling this function on it. |
| 3957 | * @param track the track metadata to destroy. |
| 3958 | * @see LIBMTP_new_track_t() |
| 3959 | */ |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 3960 | void LIBMTP_destroy_track_t(LIBMTP_track_t *track) |
| 3961 | { |
| 3962 | if (track == NULL) { |
| 3963 | return; |
| 3964 | } |
| 3965 | if (track->title != NULL) |
| 3966 | free(track->title); |
| 3967 | if (track->artist != NULL) |
| 3968 | free(track->artist); |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 3969 | if (track->composer != NULL) |
| 3970 | free(track->composer); |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 3971 | if (track->album != NULL) |
| 3972 | free(track->album); |
| 3973 | if (track->genre != NULL) |
| 3974 | free(track->genre); |
| 3975 | if (track->date != NULL) |
| 3976 | free(track->date); |
| 3977 | if (track->filename != NULL) |
| 3978 | free(track->filename); |
| 3979 | free(track); |
| 3980 | return; |
| 3981 | } |
| 3982 | |
| 3983 | /** |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 3984 | * This function maps and copies a property onto the track metadata if applicable. |
| 3985 | */ |
Linus Walleij | ddaba2f | 2007-10-02 21:20:30 +0000 | [diff] [blame] | 3986 | 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] | 3987 | { |
| 3988 | switch (prop->property) { |
| 3989 | case PTP_OPC_Name: |
| 3990 | if (prop->propval.str != NULL) |
| 3991 | track->title = strdup(prop->propval.str); |
| 3992 | else |
| 3993 | track->title = NULL; |
| 3994 | break; |
| 3995 | case PTP_OPC_Artist: |
| 3996 | if (prop->propval.str != NULL) |
| 3997 | track->artist = strdup(prop->propval.str); |
| 3998 | else |
| 3999 | track->artist = NULL; |
| 4000 | break; |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 4001 | case PTP_OPC_Composer: |
| 4002 | if (prop->propval.str != NULL) |
| 4003 | track->composer = strdup(prop->propval.str); |
| 4004 | else |
| 4005 | track->composer = NULL; |
| 4006 | break; |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 4007 | case PTP_OPC_Duration: |
| 4008 | track->duration = prop->propval.u32; |
| 4009 | break; |
| 4010 | case PTP_OPC_Track: |
| 4011 | track->tracknumber = prop->propval.u16; |
| 4012 | break; |
| 4013 | case PTP_OPC_Genre: |
| 4014 | if (prop->propval.str != NULL) |
| 4015 | track->genre = strdup(prop->propval.str); |
| 4016 | else |
| 4017 | track->genre = NULL; |
| 4018 | break; |
| 4019 | case PTP_OPC_AlbumName: |
| 4020 | if (prop->propval.str != NULL) |
| 4021 | track->album = strdup(prop->propval.str); |
| 4022 | else |
| 4023 | track->album = NULL; |
| 4024 | break; |
| 4025 | case PTP_OPC_OriginalReleaseDate: |
| 4026 | if (prop->propval.str != NULL) |
| 4027 | track->date = strdup(prop->propval.str); |
| 4028 | else |
| 4029 | track->date = NULL; |
| 4030 | break; |
| 4031 | // These are, well not so important. |
| 4032 | case PTP_OPC_SampleRate: |
| 4033 | track->samplerate = prop->propval.u32; |
| 4034 | break; |
| 4035 | case PTP_OPC_NumberOfChannels: |
| 4036 | track->nochannels = prop->propval.u16; |
| 4037 | break; |
| 4038 | case PTP_OPC_AudioWAVECodec: |
| 4039 | track->wavecodec = prop->propval.u32; |
| 4040 | break; |
| 4041 | case PTP_OPC_AudioBitRate: |
| 4042 | track->bitrate = prop->propval.u32; |
| 4043 | break; |
| 4044 | case PTP_OPC_BitRateType: |
| 4045 | track->bitratetype = prop->propval.u16; |
| 4046 | break; |
| 4047 | case PTP_OPC_Rating: |
| 4048 | track->rating = prop->propval.u16; |
| 4049 | break; |
| 4050 | case PTP_OPC_UseCount: |
| 4051 | track->usecount = prop->propval.u32; |
| 4052 | break; |
Linus Walleij | d9d28d5 | 2007-08-04 19:01:18 +0000 | [diff] [blame] | 4053 | case PTP_OPC_ObjectSize: |
Linus Walleij | ddaba2f | 2007-10-02 21:20:30 +0000 | [diff] [blame] | 4054 | if (device->object_bitsize == 64) { |
| 4055 | track->filesize = prop->propval.u64; |
| 4056 | } else { |
| 4057 | track->filesize = prop->propval.u32; |
Linus Walleij | ca2a170 | 2007-10-02 20:41:45 +0000 | [diff] [blame] | 4058 | } |
Linus Walleij | d9d28d5 | 2007-08-04 19:01:18 +0000 | [diff] [blame] | 4059 | break; |
| 4060 | default: |
| 4061 | break; |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 4062 | } |
| 4063 | } |
| 4064 | |
| 4065 | /** |
Linus Walleij | 8ab5426 | 2006-06-21 07:12:28 +0000 | [diff] [blame] | 4066 | * This function retrieves the track metadata for a track |
| 4067 | * given by a unique ID. |
| 4068 | * @param device a pointer to the device to get the track metadata off. |
| 4069 | * @param trackid the unique ID of the track. |
| 4070 | * @param objectformat the object format of this track, so we know what it supports. |
| 4071 | * @param track a metadata set to fill in. |
| 4072 | */ |
| 4073 | static void get_track_metadata(LIBMTP_mtpdevice_t *device, uint16_t objectformat, |
| 4074 | LIBMTP_track_t *track) |
| 4075 | { |
Linus Walleij | 00cf064 | 2006-07-26 20:40:59 +0000 | [diff] [blame] | 4076 | uint16_t ret; |
| 4077 | PTPParams *params = (PTPParams *) device->params; |
| 4078 | uint32_t i; |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 4079 | MTPProperties *prop; |
| 4080 | PTPObject *ob; |
Linus Walleij | 00cf064 | 2006-07-26 20:40:59 +0000 | [diff] [blame] | 4081 | |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 4082 | /* |
| 4083 | * If we have a cached, large set of metadata, then use it! |
| 4084 | */ |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 4085 | ret = ptp_object_want (params, track->item_id, PTPOBJECT_MTPPROPLIST_LOADED, &ob); |
| 4086 | if (ob->mtpprops) { |
| 4087 | prop = ob->mtpprops; |
| 4088 | for (i=0;i<ob->nrofmtpprops;i++,prop++) |
Linus Walleij | ddaba2f | 2007-10-02 21:20:30 +0000 | [diff] [blame] | 4089 | pick_property_to_track_metadata(device, prop, track); |
Linus Walleij | 3fcfea5 | 2006-11-13 07:07:36 +0000 | [diff] [blame] | 4090 | } else { |
Linus Walleij | 3fcfea5 | 2006-11-13 07:07:36 +0000 | [diff] [blame] | 4091 | uint16_t *props = NULL; |
| 4092 | uint32_t propcnt = 0; |
| 4093 | |
| 4094 | // First see which properties can be retrieved for this object format |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 4095 | 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] | 4096 | if (ret != PTP_RC_OK) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 4097 | 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] | 4098 | // Just bail out for now, nothing is ever set. |
| 4099 | return; |
| 4100 | } else { |
| 4101 | for (i=0;i<propcnt;i++) { |
| 4102 | switch (props[i]) { |
| 4103 | case PTP_OPC_Name: |
Linus Walleij | 9901e22 | 2006-11-30 12:28:19 +0000 | [diff] [blame] | 4104 | 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] | 4105 | break; |
| 4106 | case PTP_OPC_Artist: |
Linus Walleij | 9901e22 | 2006-11-30 12:28:19 +0000 | [diff] [blame] | 4107 | 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] | 4108 | break; |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 4109 | case PTP_OPC_Composer: |
| 4110 | track->composer = get_string_from_object(device, track->item_id, PTP_OPC_Composer); |
| 4111 | break; |
Linus Walleij | 3fcfea5 | 2006-11-13 07:07:36 +0000 | [diff] [blame] | 4112 | case PTP_OPC_Duration: |
Linus Walleij | 9901e22 | 2006-11-30 12:28:19 +0000 | [diff] [blame] | 4113 | 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] | 4114 | break; |
| 4115 | case PTP_OPC_Track: |
Linus Walleij | 9901e22 | 2006-11-30 12:28:19 +0000 | [diff] [blame] | 4116 | 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] | 4117 | break; |
| 4118 | case PTP_OPC_Genre: |
Linus Walleij | 9901e22 | 2006-11-30 12:28:19 +0000 | [diff] [blame] | 4119 | 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] | 4120 | break; |
| 4121 | case PTP_OPC_AlbumName: |
Linus Walleij | 9901e22 | 2006-11-30 12:28:19 +0000 | [diff] [blame] | 4122 | 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] | 4123 | break; |
| 4124 | case PTP_OPC_OriginalReleaseDate: |
Linus Walleij | 9901e22 | 2006-11-30 12:28:19 +0000 | [diff] [blame] | 4125 | 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] | 4126 | break; |
| 4127 | // These are, well not so important. |
| 4128 | case PTP_OPC_SampleRate: |
Linus Walleij | 9901e22 | 2006-11-30 12:28:19 +0000 | [diff] [blame] | 4129 | 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] | 4130 | break; |
| 4131 | case PTP_OPC_NumberOfChannels: |
Linus Walleij | 9901e22 | 2006-11-30 12:28:19 +0000 | [diff] [blame] | 4132 | 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] | 4133 | break; |
| 4134 | case PTP_OPC_AudioWAVECodec: |
Linus Walleij | 9901e22 | 2006-11-30 12:28:19 +0000 | [diff] [blame] | 4135 | 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] | 4136 | break; |
| 4137 | case PTP_OPC_AudioBitRate: |
Linus Walleij | 9901e22 | 2006-11-30 12:28:19 +0000 | [diff] [blame] | 4138 | 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] | 4139 | break; |
| 4140 | case PTP_OPC_BitRateType: |
Linus Walleij | 9901e22 | 2006-11-30 12:28:19 +0000 | [diff] [blame] | 4141 | 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] | 4142 | break; |
| 4143 | case PTP_OPC_Rating: |
Linus Walleij | 9901e22 | 2006-11-30 12:28:19 +0000 | [diff] [blame] | 4144 | 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] | 4145 | break; |
| 4146 | case PTP_OPC_UseCount: |
Linus Walleij | 9901e22 | 2006-11-30 12:28:19 +0000 | [diff] [blame] | 4147 | 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] | 4148 | break; |
Linus Walleij | d9d28d5 | 2007-08-04 19:01:18 +0000 | [diff] [blame] | 4149 | case PTP_OPC_ObjectSize: |
Linus Walleij | ddaba2f | 2007-10-02 21:20:30 +0000 | [diff] [blame] | 4150 | if (device->object_bitsize == 64) { |
| 4151 | track->filesize = get_u64_from_object(device, track->item_id, PTP_OPC_ObjectSize, 0); |
| 4152 | } else { |
| 4153 | 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] | 4154 | } |
Linus Walleij | d9d28d5 | 2007-08-04 19:01:18 +0000 | [diff] [blame] | 4155 | break; |
Linus Walleij | 3fcfea5 | 2006-11-13 07:07:36 +0000 | [diff] [blame] | 4156 | } |
| 4157 | } |
| 4158 | free(props); |
| 4159 | } |
Linus Walleij | 00cf064 | 2006-07-26 20:40:59 +0000 | [diff] [blame] | 4160 | } |
Linus Walleij | 8ab5426 | 2006-06-21 07:12:28 +0000 | [diff] [blame] | 4161 | } |
| 4162 | |
| 4163 | /** |
mopoke | 3136444 | 2006-11-20 04:53:04 +0000 | [diff] [blame] | 4164 | * THIS FUNCTION IS DEPRECATED. PLEASE UPDATE YOUR CODE IN ORDER |
Linus Walleij | 3fcfea5 | 2006-11-13 07:07:36 +0000 | [diff] [blame] | 4165 | * NOT TO USE IT. |
| 4166 | * @see LIBMTP_Get_Tracklisting_With_Callback() |
| 4167 | */ |
| 4168 | LIBMTP_track_t *LIBMTP_Get_Tracklisting(LIBMTP_mtpdevice_t *device) |
| 4169 | { |
| 4170 | printf("WARNING: LIBMTP_Get_Tracklisting() is deprecated.\n"); |
| 4171 | 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] | 4172 | return LIBMTP_Get_Tracklisting_With_Callback(device, NULL, NULL); |
Linus Walleij | 3fcfea5 | 2006-11-13 07:07:36 +0000 | [diff] [blame] | 4173 | } |
| 4174 | |
| 4175 | /** |
Linus Walleij | 25d5c21 | 2008-08-14 06:49:13 +0000 | [diff] [blame] | 4176 | * This returns a long list of all tracks available on the current MTP device. |
| 4177 | * Tracks include multimedia objects, both music tracks and video tracks. |
| 4178 | * Typical usage: |
Linus Walleij | a498273 | 2006-02-24 15:46:02 +0000 | [diff] [blame] | 4179 | * |
| 4180 | * <pre> |
| 4181 | * LIBMTP_track_t *tracklist; |
| 4182 | * |
Richard Low | 6711f44 | 2007-05-05 19:00:59 +0000 | [diff] [blame] | 4183 | * tracklist = LIBMTP_Get_Tracklisting_With_Callback(device, callback, data); |
Linus Walleij | a498273 | 2006-02-24 15:46:02 +0000 | [diff] [blame] | 4184 | * while (tracklist != NULL) { |
| 4185 | * LIBMTP_track_t *tmp; |
| 4186 | * |
| 4187 | * // Do something on each element in the list here... |
| 4188 | * tmp = tracklist; |
| 4189 | * tracklist = tracklist->next; |
| 4190 | * LIBMTP_destroy_track_t(tmp); |
| 4191 | * } |
| 4192 | * </pre> |
| 4193 | * |
Linus Walleij | 25d5c21 | 2008-08-14 06:49:13 +0000 | [diff] [blame] | 4194 | * If you want to group your track listing by storage (per storage unit) or |
| 4195 | * arrange tracks into folders, you must dereference the <code>storage_id</code> |
| 4196 | * and/or <code>parent_id</code> field of the returned <code>LIBMTP_track_t</code> |
| 4197 | * struct. To arrange by folders or files you typically have to create the proper |
| 4198 | * trees by calls to <code>LIBMTP_Get_Storage()</code> and/or |
| 4199 | * <code>LIBMTP_Get_Folder_List()</code> first. |
| 4200 | * |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 4201 | * @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] | 4202 | * @param callback a function to be called during the tracklisting retrieveal |
Linus Walleij | 25d5c21 | 2008-08-14 06:49:13 +0000 | [diff] [blame] | 4203 | * for displaying progress bars etc, or NULL if you don't want |
| 4204 | * any callbacks. |
Richard Low | dc0b6c7 | 2006-11-13 09:22:23 +0000 | [diff] [blame] | 4205 | * @param data a user-defined pointer that is passed along to |
Linus Walleij | 25d5c21 | 2008-08-14 06:49:13 +0000 | [diff] [blame] | 4206 | * the <code>progress</code> function in order to |
| 4207 | * pass along some user defined data to the progress |
| 4208 | * updates. If not used, set this to NULL. |
Linus Walleij | a498273 | 2006-02-24 15:46:02 +0000 | [diff] [blame] | 4209 | * @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] | 4210 | * field of the <code>LIBMTP_track_t</code> data structure. |
| 4211 | * Each of the metadata tags must be freed after use, and may |
| 4212 | * contain only partial metadata information, i.e. one or several |
| 4213 | * fields may be NULL or 0. |
Linus Walleij | 2e4b5f9 | 2006-06-16 14:00:49 +0000 | [diff] [blame] | 4214 | * @see LIBMTP_Get_Trackmetadata() |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 4215 | */ |
Richard Low | dc0b6c7 | 2006-11-13 09:22:23 +0000 | [diff] [blame] | 4216 | LIBMTP_track_t *LIBMTP_Get_Tracklisting_With_Callback(LIBMTP_mtpdevice_t *device, |
| 4217 | LIBMTP_progressfunc_t const callback, |
| 4218 | void const * const data) |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 4219 | { |
| 4220 | uint32_t i = 0; |
| 4221 | LIBMTP_track_t *retracks = NULL; |
| 4222 | LIBMTP_track_t *curtrack = NULL; |
Linus Walleij | 9b28da3 | 2006-03-16 13:47:58 +0000 | [diff] [blame] | 4223 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | 2be40c7 | 2007-03-16 15:19:44 +0000 | [diff] [blame] | 4224 | PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo; |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 4225 | |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 4226 | // Get all the handles if we haven't already done that |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 4227 | if (params->nrofobjects == 0) { |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 4228 | flush_handles(device); |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 4229 | } |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 4230 | |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 4231 | for (i = 0; i < params->nrofobjects; i++) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 4232 | LIBMTP_track_t *track; |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 4233 | PTPObject *ob; |
Linus Walleij | 46651f3 | 2008-04-26 23:30:19 +0000 | [diff] [blame] | 4234 | LIBMTP_filetype_t mtptype; |
mopoke | 3136444 | 2006-11-20 04:53:04 +0000 | [diff] [blame] | 4235 | |
Richard Low | dc0b6c7 | 2006-11-13 09:22:23 +0000 | [diff] [blame] | 4236 | if (callback != NULL) |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 4237 | callback(i, params->nrofobjects, data); |
mopoke | 3136444 | 2006-11-20 04:53:04 +0000 | [diff] [blame] | 4238 | |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 4239 | ob = ¶ms->objects[i]; |
| 4240 | mtptype = map_ptp_type_to_libmtp_type(ob->oi.ObjectFormat); |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 4241 | |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 4242 | // Ignore stuff we don't know how to handle... |
| 4243 | // TODO: get this list as an intersection of the sets |
| 4244 | // supported by the device and the from the device and |
Linus Walleij | d5356e2 | 2008-11-01 22:08:59 +0000 | [diff] [blame] | 4245 | // all known track files? |
| 4246 | if (!LIBMTP_FILETYPE_IS_TRACK(mtptype) && |
Linus Walleij | 46651f3 | 2008-04-26 23:30:19 +0000 | [diff] [blame] | 4247 | // This row lets through undefined files for examination since they may be forgotten OGG files. |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 4248 | (ob->oi.ObjectFormat != PTP_OFC_Undefined || |
Richard Low | ef19731 | 2008-11-01 18:29:41 +0000 | [diff] [blame] | 4249 | (!FLAG_IRIVER_OGG_ALZHEIMER(ptp_usb) && |
Linus Walleij | 89bb1cd | 2009-07-24 21:03:36 +0000 | [diff] [blame] | 4250 | !FLAG_OGG_IS_UNKNOWN(ptp_usb) && |
| 4251 | !FLAG_FLAC_IS_UNKNOWN(ptp_usb))) |
Linus Walleij | 46651f3 | 2008-04-26 23:30:19 +0000 | [diff] [blame] | 4252 | ) { |
Linus Walleij | c8abc92 | 2008-09-28 18:38:42 +0000 | [diff] [blame] | 4253 | //printf("Not a music track (name: %s format: %d), skipping...\n", oi->Filename, oi->ObjectFormat); |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 4254 | continue; |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 4255 | } |
| 4256 | |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 4257 | // Allocate a new track type |
| 4258 | track = LIBMTP_new_track_t(); |
| 4259 | |
| 4260 | // This is some sort of unique ID so we can keep track of the track. |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 4261 | track->item_id = ob->oid; |
| 4262 | track->parent_id = ob->oi.ParentObject; |
| 4263 | track->storage_id = ob->oi.StorageID; |
| 4264 | track->modificationdate = ob->oi.ModificationDate; |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 4265 | |
Linus Walleij | 46651f3 | 2008-04-26 23:30:19 +0000 | [diff] [blame] | 4266 | track->filetype = mtptype; |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 4267 | |
| 4268 | // Original file-specific properties |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 4269 | track->filesize = ob->oi.ObjectCompressedSize; |
| 4270 | if (ob->oi.Filename != NULL) { |
| 4271 | track->filename = strdup(ob->oi.Filename); |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 4272 | } |
| 4273 | |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 4274 | get_track_metadata(device, ob->oi.ObjectFormat, track); |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 4275 | |
| 4276 | /* |
| 4277 | * A special quirk for iriver devices that doesn't quite |
| 4278 | * remember that some files marked as "unknown" type are |
Linus Walleij | 89bb1cd | 2009-07-24 21:03:36 +0000 | [diff] [blame] | 4279 | * actually OGG or FLAC files. We look at the filename extension |
| 4280 | * and see if it happens that this was atleast named "ogg" or "flac" |
| 4281 | * and fall back on this heuristic approach in that case, |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 4282 | * for these bugged devices only. |
| 4283 | */ |
| 4284 | if (track->filetype == LIBMTP_FILETYPE_UNKNOWN && |
Linus Walleij | 89bb1cd | 2009-07-24 21:03:36 +0000 | [diff] [blame] | 4285 | track->filename != NULL) { |
| 4286 | if ((FLAG_IRIVER_OGG_ALZHEIMER(ptp_usb) || |
| 4287 | FLAG_OGG_IS_UNKNOWN(ptp_usb)) && |
| 4288 | has_ogg_extension(track->filename)) |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 4289 | track->filetype = LIBMTP_FILETYPE_OGG; |
Linus Walleij | 89bb1cd | 2009-07-24 21:03:36 +0000 | [diff] [blame] | 4290 | else if (FLAG_FLAC_IS_UNKNOWN(ptp_usb) && |
| 4291 | has_flac_extension(track->filename)) |
| 4292 | track->filetype = LIBMTP_FILETYPE_FLAC; |
| 4293 | else { |
| 4294 | // This was not an OGG/FLAC file so discard it and continue |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 4295 | LIBMTP_destroy_track_t(track); |
| 4296 | continue; |
| 4297 | } |
| 4298 | } |
| 4299 | |
| 4300 | // Add track to a list that will be returned afterwards. |
| 4301 | if (retracks == NULL) { |
| 4302 | retracks = track; |
| 4303 | curtrack = track; |
| 4304 | } else { |
| 4305 | curtrack->next = track; |
| 4306 | curtrack = track; |
| 4307 | } |
| 4308 | |
| 4309 | // Call listing callback |
| 4310 | // double progressPercent = (double)i*(double)100.0 / (double)params->handles.n; |
| 4311 | |
| 4312 | |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 4313 | } // Handle counting loop |
| 4314 | return retracks; |
| 4315 | } |
Linus Walleij | dcde608 | 2006-02-17 16:16:34 +0000 | [diff] [blame] | 4316 | |
Linus Walleij | 2e4b5f9 | 2006-06-16 14:00:49 +0000 | [diff] [blame] | 4317 | /** |
| 4318 | * This function retrieves the metadata for a single track off |
| 4319 | * the device. |
| 4320 | * |
| 4321 | * Do not call this function repeatedly! The track handles are linearly |
| 4322 | * searched O(n) and the call may involve (slow) USB traffic, so use |
| 4323 | * <code>LIBMTP_Get_Tracklisting()</code> and cache the tracks, preferably |
| 4324 | * as an efficient data structure such as a hash list. |
| 4325 | * |
| 4326 | * @param device a pointer to the device to get the track metadata from. |
| 4327 | * @param trackid the object ID of the track that you want the metadata for. |
| 4328 | * @return a track metadata entry on success or NULL on failure. |
| 4329 | * @see LIBMTP_Get_Tracklisting() |
| 4330 | */ |
| 4331 | LIBMTP_track_t *LIBMTP_Get_Trackmetadata(LIBMTP_mtpdevice_t *device, uint32_t const trackid) |
| 4332 | { |
Linus Walleij | 2e4b5f9 | 2006-06-16 14:00:49 +0000 | [diff] [blame] | 4333 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | c8abc92 | 2008-09-28 18:38:42 +0000 | [diff] [blame] | 4334 | PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo; |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 4335 | PTPObject *ob; |
| 4336 | LIBMTP_track_t *track; |
| 4337 | LIBMTP_filetype_t mtptype; |
| 4338 | uint16_t ret; |
Linus Walleij | 2e4b5f9 | 2006-06-16 14:00:49 +0000 | [diff] [blame] | 4339 | |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 4340 | // Get all the handles if we haven't already done that |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 4341 | if (params->nrofobjects == 0) |
Linus Walleij | 2e4b5f9 | 2006-06-16 14:00:49 +0000 | [diff] [blame] | 4342 | flush_handles(device); |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 4343 | |
| 4344 | ret = ptp_object_want (params, trackid, PTPOBJECT_OBJECTINFO_LOADED, &ob); |
| 4345 | if (ret != PTP_RC_OK) |
| 4346 | return NULL; |
| 4347 | |
| 4348 | mtptype = map_ptp_type_to_libmtp_type(ob->oi.ObjectFormat); |
| 4349 | |
| 4350 | // Ignore stuff we don't know how to handle... |
| 4351 | if (!LIBMTP_FILETYPE_IS_TRACK(mtptype) && |
Linus Walleij | 89bb1cd | 2009-07-24 21:03:36 +0000 | [diff] [blame] | 4352 | /* |
| 4353 | * This row lets through undefined files for examination |
| 4354 | * since they may be forgotten OGG or FLAC files. |
| 4355 | */ |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 4356 | (ob->oi.ObjectFormat != PTP_OFC_Undefined || |
| 4357 | (!FLAG_IRIVER_OGG_ALZHEIMER(ptp_usb) && |
Linus Walleij | 89bb1cd | 2009-07-24 21:03:36 +0000 | [diff] [blame] | 4358 | !FLAG_OGG_IS_UNKNOWN(ptp_usb) && |
| 4359 | !FLAG_FLAC_IS_UNKNOWN(ptp_usb))) |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 4360 | ) { |
| 4361 | //printf("Not a music track (name: %s format: %d), skipping...\n", oi->Filename, oi->ObjectFormat); |
| 4362 | return NULL; |
Linus Walleij | 2e4b5f9 | 2006-06-16 14:00:49 +0000 | [diff] [blame] | 4363 | } |
| 4364 | |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 4365 | // Allocate a new track type |
| 4366 | track = LIBMTP_new_track_t(); |
| 4367 | |
| 4368 | // This is some sort of unique ID so we can keep track of the track. |
| 4369 | track->item_id = ob->oid; |
| 4370 | track->parent_id = ob->oi.ParentObject; |
| 4371 | track->storage_id = ob->oi.StorageID; |
| 4372 | track->modificationdate = ob->oi.ModificationDate; |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 4373 | |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 4374 | track->filetype = mtptype; |
Linus Walleij | 2e4b5f9 | 2006-06-16 14:00:49 +0000 | [diff] [blame] | 4375 | |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 4376 | // Original file-specific properties |
| 4377 | track->filesize = ob->oi.ObjectCompressedSize; |
| 4378 | if (ob->oi.Filename != NULL) { |
| 4379 | track->filename = strdup(ob->oi.Filename); |
| 4380 | } |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 4381 | |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 4382 | /* |
Linus Walleij | 89bb1cd | 2009-07-24 21:03:36 +0000 | [diff] [blame] | 4383 | * A special quirk for devices that doesn't quite |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 4384 | * remember that some files marked as "unknown" type are |
Linus Walleij | 89bb1cd | 2009-07-24 21:03:36 +0000 | [diff] [blame] | 4385 | * actually OGG or FLAC files. We look at the filename extension |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 4386 | * and see if it happens that this was atleast named "ogg" |
| 4387 | * and fall back on this heuristic approach in that case, |
| 4388 | * for these bugged devices only. |
| 4389 | */ |
| 4390 | if (track->filetype == LIBMTP_FILETYPE_UNKNOWN && |
Linus Walleij | 89bb1cd | 2009-07-24 21:03:36 +0000 | [diff] [blame] | 4391 | track->filename != NULL) { |
| 4392 | if ((FLAG_IRIVER_OGG_ALZHEIMER(ptp_usb) || |
| 4393 | FLAG_OGG_IS_UNKNOWN(ptp_usb)) && |
| 4394 | has_ogg_extension(track->filename)) |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 4395 | track->filetype = LIBMTP_FILETYPE_OGG; |
Linus Walleij | 89bb1cd | 2009-07-24 21:03:36 +0000 | [diff] [blame] | 4396 | else if (FLAG_FLAC_IS_UNKNOWN(ptp_usb) && |
| 4397 | has_flac_extension(track->filename)) |
| 4398 | track->filetype = LIBMTP_FILETYPE_FLAC; |
| 4399 | else { |
| 4400 | // This was not an OGG/FLAC file so discard it |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 4401 | LIBMTP_destroy_track_t(track); |
Linus Walleij | 2e4b5f9 | 2006-06-16 14:00:49 +0000 | [diff] [blame] | 4402 | return NULL; |
| 4403 | } |
Linus Walleij | 2e4b5f9 | 2006-06-16 14:00:49 +0000 | [diff] [blame] | 4404 | } |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 4405 | get_track_metadata(device, ob->oi.ObjectFormat, track); |
| 4406 | return track; |
Linus Walleij | 2e4b5f9 | 2006-06-16 14:00:49 +0000 | [diff] [blame] | 4407 | } |
| 4408 | |
Richard Low | 5b4023c | 2009-04-16 19:14:38 +0000 | [diff] [blame] | 4409 | /** |
| 4410 | * This is a manual conversion from MTPDataGetFunc to PTPDataGetFunc |
| 4411 | * to isolate the internal type. |
| 4412 | */ |
Linus Walleij | d866d24 | 2009-08-23 21:50:39 +0000 | [diff] [blame] | 4413 | static uint16_t get_func_wrapper(PTPParams* params, void* priv, unsigned long wantlen, unsigned char *data, unsigned long *gotlen) |
Richard Low | 5b4023c | 2009-04-16 19:14:38 +0000 | [diff] [blame] | 4414 | { |
Linus Walleij | d866d24 | 2009-08-23 21:50:39 +0000 | [diff] [blame] | 4415 | MTPDataHandler *handler = (MTPDataHandler *)priv; |
Richard Low | 5b4023c | 2009-04-16 19:14:38 +0000 | [diff] [blame] | 4416 | uint16_t ret; |
| 4417 | uint32_t local_gotlen = 0; |
Linus Walleij | d866d24 | 2009-08-23 21:50:39 +0000 | [diff] [blame] | 4418 | ret = handler->getfunc(params, handler->priv, wantlen, data, &local_gotlen); |
Richard Low | 5b4023c | 2009-04-16 19:14:38 +0000 | [diff] [blame] | 4419 | *gotlen = local_gotlen; |
Richard Low | 6f07084 | 2009-05-03 10:26:16 +0000 | [diff] [blame] | 4420 | switch (ret) |
| 4421 | { |
| 4422 | case LIBMTP_HANDLER_RETURN_OK: |
| 4423 | return PTP_RC_OK; |
| 4424 | case LIBMTP_HANDLER_RETURN_ERROR: |
| 4425 | return PTP_ERROR_IO; |
| 4426 | case LIBMTP_HANDLER_RETURN_CANCEL: |
| 4427 | return PTP_ERROR_CANCEL; |
| 4428 | default: |
| 4429 | return PTP_ERROR_IO; |
| 4430 | } |
Richard Low | 5b4023c | 2009-04-16 19:14:38 +0000 | [diff] [blame] | 4431 | } |
| 4432 | |
| 4433 | /** |
| 4434 | * This is a manual conversion from MTPDataPutFunc to PTPDataPutFunc |
| 4435 | * to isolate the internal type. |
| 4436 | */ |
Linus Walleij | d866d24 | 2009-08-23 21:50:39 +0000 | [diff] [blame] | 4437 | static uint16_t put_func_wrapper(PTPParams* params, void* priv, unsigned long sendlen, unsigned char *data, unsigned long *putlen) |
Richard Low | 5b4023c | 2009-04-16 19:14:38 +0000 | [diff] [blame] | 4438 | { |
Linus Walleij | d866d24 | 2009-08-23 21:50:39 +0000 | [diff] [blame] | 4439 | MTPDataHandler *handler = (MTPDataHandler *)priv; |
Richard Low | 5b4023c | 2009-04-16 19:14:38 +0000 | [diff] [blame] | 4440 | uint16_t ret; |
| 4441 | uint32_t local_putlen = 0; |
Linus Walleij | d866d24 | 2009-08-23 21:50:39 +0000 | [diff] [blame] | 4442 | ret = handler->putfunc(params, handler->priv, sendlen, data, &local_putlen); |
Richard Low | 5b4023c | 2009-04-16 19:14:38 +0000 | [diff] [blame] | 4443 | *putlen = local_putlen; |
Richard Low | 75981ac | 2009-05-03 12:54:06 +0000 | [diff] [blame] | 4444 | switch (ret) |
| 4445 | { |
| 4446 | case LIBMTP_HANDLER_RETURN_OK: |
| 4447 | return PTP_RC_OK; |
| 4448 | case LIBMTP_HANDLER_RETURN_ERROR: |
| 4449 | return PTP_ERROR_IO; |
| 4450 | case LIBMTP_HANDLER_RETURN_CANCEL: |
| 4451 | return PTP_ERROR_CANCEL; |
| 4452 | default: |
| 4453 | return PTP_ERROR_IO; |
| 4454 | } |
Richard Low | 5b4023c | 2009-04-16 19:14:38 +0000 | [diff] [blame] | 4455 | } |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 4456 | |
| 4457 | /** |
| 4458 | * This gets a file off the device to a local file identified |
| 4459 | * by a filename. |
| 4460 | * @param device a pointer to the device to get the track from. |
| 4461 | * @param id the file ID of the file to retrieve. |
| 4462 | * @param path a filename to use for the retrieved file. |
| 4463 | * @param callback a progress indicator function or NULL to ignore. |
| 4464 | * @param data a user-defined pointer that is passed along to |
| 4465 | * the <code>progress</code> function in order to |
| 4466 | * pass along some user defined data to the progress |
| 4467 | * updates. If not used, set this to NULL. |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 4468 | * @return 0 if the transfer was successful, any other value means |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 4469 | * failure. |
| 4470 | * @see LIBMTP_Get_File_To_File_Descriptor() |
| 4471 | */ |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 4472 | 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] | 4473 | char const * const path, LIBMTP_progressfunc_t const callback, |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 4474 | void const * const data) |
| 4475 | { |
| 4476 | int fd = -1; |
| 4477 | int ret; |
| 4478 | |
| 4479 | // Sanity check |
| 4480 | if (path == NULL) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 4481 | 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] | 4482 | return -1; |
| 4483 | } |
| 4484 | |
| 4485 | // Open file |
| 4486 | #ifdef __WIN32__ |
Linus Walleij | 7beba57 | 2006-11-29 08:56:12 +0000 | [diff] [blame] | 4487 | #ifdef USE_WINDOWS_IO_H |
| 4488 | if ( (fd = _open(path, O_RDWR|O_CREAT|O_TRUNC|O_BINARY,_S_IREAD)) == -1 ) { |
| 4489 | #else |
Linus Walleij | 5b4a4d2 | 2009-01-10 12:18:14 +0000 | [diff] [blame] | 4490 | if ( (fd = open(path, O_RDWR|O_CREAT|O_TRUNC|O_BINARY,S_IRWXU)) == -1 ) { |
Linus Walleij | 7beba57 | 2006-11-29 08:56:12 +0000 | [diff] [blame] | 4491 | #endif |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 4492 | #else |
| 4493 | if ( (fd = open(path, O_RDWR|O_CREAT|O_TRUNC,S_IRWXU|S_IRGRP)) == -1) { |
| 4494 | #endif |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 4495 | 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] | 4496 | return -1; |
| 4497 | } |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 4498 | |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 4499 | ret = LIBMTP_Get_File_To_File_Descriptor(device, id, fd, callback, data); |
| 4500 | |
| 4501 | // Close file |
| 4502 | close(fd); |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 4503 | |
Linus Walleij | 25d33b5 | 2007-09-16 21:36:19 +0000 | [diff] [blame] | 4504 | // Delete partial file. |
| 4505 | if (ret == -1) { |
| 4506 | unlink(path); |
| 4507 | } |
| 4508 | |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 4509 | return ret; |
| 4510 | } |
| 4511 | |
| 4512 | /** |
| 4513 | * This gets a file off the device to a file identified |
| 4514 | * by a file descriptor. |
Linus Walleij | 0558ac5 | 2006-09-07 06:55:03 +0000 | [diff] [blame] | 4515 | * |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 4516 | * This function can potentially be used for streaming |
| 4517 | * files off the device for playback or broadcast for example, |
Linus Walleij | 0558ac5 | 2006-09-07 06:55:03 +0000 | [diff] [blame] | 4518 | * by downloading the file into a stream sink e.g. a socket. |
| 4519 | * |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 4520 | * @param device a pointer to the device to get the file from. |
| 4521 | * @param id the file ID of the file to retrieve. |
| 4522 | * @param fd a local file descriptor to write the file to. |
| 4523 | * @param callback a progress indicator function or NULL to ignore. |
| 4524 | * @param data a user-defined pointer that is passed along to |
| 4525 | * the <code>progress</code> function in order to |
| 4526 | * pass along some user defined data to the progress |
| 4527 | * updates. If not used, set this to NULL. |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 4528 | * @return 0 if the transfer was successful, any other value means |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 4529 | * failure. |
| 4530 | * @see LIBMTP_Get_File_To_File() |
| 4531 | */ |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 4532 | int LIBMTP_Get_File_To_File_Descriptor(LIBMTP_mtpdevice_t *device, |
| 4533 | uint32_t const id, |
| 4534 | int const fd, |
Linus Walleij | ee73ef2 | 2006-08-27 19:56:00 +0000 | [diff] [blame] | 4535 | LIBMTP_progressfunc_t const callback, |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 4536 | void const * const data) |
| 4537 | { |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 4538 | uint16_t ret; |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 4539 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | ee73ef2 | 2006-08-27 19:56:00 +0000 | [diff] [blame] | 4540 | PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo; |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 4541 | PTPObject *ob; |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 4542 | |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 4543 | ret = ptp_object_want (params, id, PTPOBJECT_OBJECTINFO_LOADED, &ob); |
| 4544 | if (ret != PTP_RC_OK) { |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 4545 | 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] | 4546 | return -1; |
| 4547 | } |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 4548 | if (ob->oi.ObjectFormat == PTP_OFC_Association) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 4549 | 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] | 4550 | return -1; |
| 4551 | } |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 4552 | |
Linus Walleij | ee73ef2 | 2006-08-27 19:56:00 +0000 | [diff] [blame] | 4553 | // Callbacks |
| 4554 | ptp_usb->callback_active = 1; |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 4555 | ptp_usb->current_transfer_total = ob->oi.ObjectCompressedSize+ |
Linus Walleij | 7f0c72e | 2006-09-06 13:01:58 +0000 | [diff] [blame] | 4556 | PTP_USB_BULK_HDR_LEN+sizeof(uint32_t); // Request length, one parameter |
Linus Walleij | ee73ef2 | 2006-08-27 19:56:00 +0000 | [diff] [blame] | 4557 | ptp_usb->current_transfer_complete = 0; |
| 4558 | ptp_usb->current_transfer_callback = callback; |
| 4559 | ptp_usb->current_transfer_callback_data = data; |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 4560 | |
Linus Walleij | 96c6243 | 2006-08-21 10:04:02 +0000 | [diff] [blame] | 4561 | ret = ptp_getobject_tofd(params, id, fd); |
Linus Walleij | ee73ef2 | 2006-08-27 19:56:00 +0000 | [diff] [blame] | 4562 | |
| 4563 | ptp_usb->callback_active = 0; |
| 4564 | ptp_usb->current_transfer_callback = NULL; |
| 4565 | ptp_usb->current_transfer_callback_data = NULL; |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 4566 | |
Linus Walleij | bd3bf9e | 2007-09-14 19:31:54 +0000 | [diff] [blame] | 4567 | if (ret == PTP_ERROR_CANCEL) { |
Linus Walleij | fb28b63 | 2007-10-23 21:56:18 +0000 | [diff] [blame] | 4568 | 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] | 4569 | return -1; |
| 4570 | } |
Linus Walleij | b02a066 | 2006-04-25 08:05:09 +0000 | [diff] [blame] | 4571 | if (ret != PTP_RC_OK) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 4572 | 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] | 4573 | return -1; |
| 4574 | } |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 4575 | |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 4576 | return 0; |
| 4577 | } |
| 4578 | |
Linus Walleij | dcde608 | 2006-02-17 16:16:34 +0000 | [diff] [blame] | 4579 | /** |
Richard Low | d3b1702 | 2009-04-11 12:37:39 +0000 | [diff] [blame] | 4580 | * This gets a file off the device and calls put_func |
| 4581 | * with chunks of data |
| 4582 | * |
| 4583 | * @param device a pointer to the device to get the file from. |
| 4584 | * @param id the file ID of the file to retrieve. |
| 4585 | * @param put_func the function to call when we have data. |
| 4586 | * @param priv the user-defined pointer that is passed to |
| 4587 | * <code>put_func</code>. |
| 4588 | * @param callback a progress indicator function or NULL to ignore. |
| 4589 | * @param data a user-defined pointer that is passed along to |
| 4590 | * the <code>progress</code> function in order to |
| 4591 | * pass along some user defined data to the progress |
| 4592 | * updates. If not used, set this to NULL. |
| 4593 | * @return 0 if the transfer was successful, any other value means |
| 4594 | * failure. |
| 4595 | */ |
| 4596 | int LIBMTP_Get_File_To_Handler(LIBMTP_mtpdevice_t *device, |
| 4597 | uint32_t const id, |
| 4598 | MTPDataPutFunc put_func, |
| 4599 | void * priv, |
| 4600 | LIBMTP_progressfunc_t const callback, |
| 4601 | void const * const data) |
| 4602 | { |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 4603 | PTPObject *ob; |
Richard Low | d3b1702 | 2009-04-11 12:37:39 +0000 | [diff] [blame] | 4604 | uint16_t ret; |
| 4605 | PTPParams *params = (PTPParams *) device->params; |
| 4606 | PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo; |
| 4607 | |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 4608 | ret = ptp_object_want (params, id, PTPOBJECT_OBJECTINFO_LOADED, &ob); |
| 4609 | if (ret != PTP_RC_OK) { |
Richard Low | d3b1702 | 2009-04-11 12:37:39 +0000 | [diff] [blame] | 4610 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Get_File_To_File_Descriptor(): Could not get object info."); |
| 4611 | return -1; |
| 4612 | } |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 4613 | if (ob->oi.ObjectFormat == PTP_OFC_Association) { |
Richard Low | d3b1702 | 2009-04-11 12:37:39 +0000 | [diff] [blame] | 4614 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Get_File_To_File_Descriptor(): Bad object format."); |
| 4615 | return -1; |
| 4616 | } |
| 4617 | |
| 4618 | // Callbacks |
| 4619 | ptp_usb->callback_active = 1; |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 4620 | ptp_usb->current_transfer_total = ob->oi.ObjectCompressedSize+ |
Richard Low | d3b1702 | 2009-04-11 12:37:39 +0000 | [diff] [blame] | 4621 | PTP_USB_BULK_HDR_LEN+sizeof(uint32_t); // Request length, one parameter |
| 4622 | ptp_usb->current_transfer_complete = 0; |
| 4623 | ptp_usb->current_transfer_callback = callback; |
| 4624 | ptp_usb->current_transfer_callback_data = data; |
| 4625 | |
Richard Low | 5b4023c | 2009-04-16 19:14:38 +0000 | [diff] [blame] | 4626 | MTPDataHandler mtp_handler; |
| 4627 | mtp_handler.getfunc = NULL; |
| 4628 | mtp_handler.putfunc = put_func; |
Linus Walleij | d866d24 | 2009-08-23 21:50:39 +0000 | [diff] [blame] | 4629 | mtp_handler.priv = priv; |
| 4630 | |
Richard Low | d3b1702 | 2009-04-11 12:37:39 +0000 | [diff] [blame] | 4631 | PTPDataHandler handler; |
| 4632 | handler.getfunc = NULL; |
Richard Low | 5b4023c | 2009-04-16 19:14:38 +0000 | [diff] [blame] | 4633 | handler.putfunc = put_func_wrapper; |
Linus Walleij | d866d24 | 2009-08-23 21:50:39 +0000 | [diff] [blame] | 4634 | handler.priv = &mtp_handler; |
| 4635 | |
Richard Low | d3b1702 | 2009-04-11 12:37:39 +0000 | [diff] [blame] | 4636 | ret = ptp_getobject_to_handler(params, id, &handler); |
| 4637 | |
| 4638 | ptp_usb->callback_active = 0; |
| 4639 | ptp_usb->current_transfer_callback = NULL; |
| 4640 | ptp_usb->current_transfer_callback_data = NULL; |
| 4641 | |
| 4642 | if (ret == PTP_ERROR_CANCEL) { |
| 4643 | add_error_to_errorstack(device, LIBMTP_ERROR_CANCELLED, "LIBMTP_Get_File_From_File_Descriptor(): Cancelled transfer."); |
| 4644 | return -1; |
| 4645 | } |
| 4646 | if (ret != PTP_RC_OK) { |
| 4647 | add_ptp_error_to_errorstack(device, ret, "LIBMTP_Get_File_To_File_Descriptor(): Could not get file from device."); |
| 4648 | return -1; |
| 4649 | } |
| 4650 | |
| 4651 | return 0; |
| 4652 | } |
| 4653 | |
| 4654 | |
| 4655 | /** |
Linus Walleij | dcde608 | 2006-02-17 16:16:34 +0000 | [diff] [blame] | 4656 | * This gets a track off the device to a file identified |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 4657 | * by a filename. This is actually just a wrapper for the |
| 4658 | * \c LIBMTP_Get_Track_To_File() function. |
Linus Walleij | dcde608 | 2006-02-17 16:16:34 +0000 | [diff] [blame] | 4659 | * @param device a pointer to the device to get the track from. |
| 4660 | * @param id the track ID of the track to retrieve. |
| 4661 | * @param path a filename to use for the retrieved track. |
| 4662 | * @param callback a progress indicator function or NULL to ignore. |
| 4663 | * @param data a user-defined pointer that is passed along to |
| 4664 | * the <code>progress</code> function in order to |
| 4665 | * pass along some user defined data to the progress |
| 4666 | * updates. If not used, set this to NULL. |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 4667 | * @return 0 if the transfer was successful, any other value means |
Linus Walleij | dcde608 | 2006-02-17 16:16:34 +0000 | [diff] [blame] | 4668 | * failure. |
| 4669 | * @see LIBMTP_Get_Track_To_File_Descriptor() |
| 4670 | */ |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 4671 | 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] | 4672 | char const * const path, LIBMTP_progressfunc_t const callback, |
Linus Walleij | 0cd8543 | 2006-02-20 14:37:50 +0000 | [diff] [blame] | 4673 | void const * const data) |
Linus Walleij | dcde608 | 2006-02-17 16:16:34 +0000 | [diff] [blame] | 4674 | { |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 4675 | // This is just a wrapper |
| 4676 | return LIBMTP_Get_File_To_File(device, id, path, callback, data); |
Linus Walleij | dcde608 | 2006-02-17 16:16:34 +0000 | [diff] [blame] | 4677 | } |
| 4678 | |
| 4679 | /** |
| 4680 | * This gets a track off the device to a file identified |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 4681 | * by a file descriptor. This is actually just a wrapper for |
| 4682 | * the \c LIBMTP_Get_File_To_File_Descriptor() function. |
Linus Walleij | dcde608 | 2006-02-17 16:16:34 +0000 | [diff] [blame] | 4683 | * @param device a pointer to the device to get the track from. |
| 4684 | * @param id the track ID of the track to retrieve. |
| 4685 | * @param fd a file descriptor to write the track to. |
| 4686 | * @param callback a progress indicator function or NULL to ignore. |
| 4687 | * @param data a user-defined pointer that is passed along to |
| 4688 | * the <code>progress</code> function in order to |
| 4689 | * pass along some user defined data to the progress |
| 4690 | * updates. If not used, set this to NULL. |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 4691 | * @return 0 if the transfer was successful, any other value means |
Linus Walleij | dcde608 | 2006-02-17 16:16:34 +0000 | [diff] [blame] | 4692 | * failure. |
| 4693 | * @see LIBMTP_Get_Track_To_File() |
| 4694 | */ |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 4695 | int LIBMTP_Get_Track_To_File_Descriptor(LIBMTP_mtpdevice_t *device, |
| 4696 | uint32_t const id, |
| 4697 | int const fd, |
Linus Walleij | ee73ef2 | 2006-08-27 19:56:00 +0000 | [diff] [blame] | 4698 | LIBMTP_progressfunc_t const callback, |
Linus Walleij | 0cd8543 | 2006-02-20 14:37:50 +0000 | [diff] [blame] | 4699 | void const * const data) |
Linus Walleij | dcde608 | 2006-02-17 16:16:34 +0000 | [diff] [blame] | 4700 | { |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 4701 | // This is just a wrapper |
| 4702 | return LIBMTP_Get_File_To_File_Descriptor(device, id, fd, callback, data); |
Linus Walleij | dcde608 | 2006-02-17 16:16:34 +0000 | [diff] [blame] | 4703 | } |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 4704 | |
| 4705 | /** |
Richard Low | d3b1702 | 2009-04-11 12:37:39 +0000 | [diff] [blame] | 4706 | * This gets a track off the device to a handler function. |
| 4707 | * This is actually just a wrapper for |
| 4708 | * the \c LIBMTP_Get_File_To_Handler() function. |
| 4709 | * @param device a pointer to the device to get the track from. |
| 4710 | * @param id the track ID of the track to retrieve. |
| 4711 | * @param put_func the function to call when we have data. |
| 4712 | * @param priv the user-defined pointer that is passed to |
| 4713 | * <code>put_func</code>. |
| 4714 | * @param callback a progress indicator function or NULL to ignore. |
| 4715 | * @param data a user-defined pointer that is passed along to |
| 4716 | * the <code>progress</code> function in order to |
| 4717 | * pass along some user defined data to the progress |
| 4718 | * updates. If not used, set this to NULL. |
| 4719 | * @return 0 if the transfer was successful, any other value means |
| 4720 | * failure. |
| 4721 | */ |
| 4722 | int LIBMTP_Get_Track_To_Handler(LIBMTP_mtpdevice_t *device, |
| 4723 | uint32_t const id, |
| 4724 | MTPDataPutFunc put_func, |
| 4725 | void * priv, |
| 4726 | LIBMTP_progressfunc_t const callback, |
| 4727 | void const * const data) |
| 4728 | { |
| 4729 | // This is just a wrapper |
| 4730 | return LIBMTP_Get_File_To_Handler(device, id, put_func, priv, callback, data); |
| 4731 | } |
| 4732 | |
| 4733 | /** |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 4734 | * This function sends a track from a local file to an |
| 4735 | * MTP device. A filename and a set of metadata must be |
| 4736 | * given as input. |
| 4737 | * @param device a pointer to the device to send the track to. |
| 4738 | * @param path the filename of a local file which will be sent. |
| 4739 | * @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] | 4740 | * After this call the field <code>metadata->item_id</code> |
| 4741 | * will contain the new track ID. Other fields such |
| 4742 | * as the <code>metadata->filename</code>, <code>metadata->parent_id</code> |
| 4743 | * or <code>metadata->storage_id</code> may also change during this |
| 4744 | * operation due to device restrictions, so do not rely on the |
| 4745 | * contents of this struct to be preserved in any way. |
| 4746 | * <ul> |
| 4747 | * <li><code>metadata->parent_id</code> should be set to the parent |
| 4748 | * (e.g. folder) to store this track in. Since some |
| 4749 | * devices are a bit picky about where files |
| 4750 | * are placed, a default folder will be chosen if libmtp |
| 4751 | * has detected one for the current filetype and this |
| 4752 | * parameter is set to 0. If this is 0 and no default folder |
| 4753 | * can be found, the file will be stored in the root folder. |
| 4754 | * <li><code>metadata->storage_id</code> should be set to the |
| 4755 | * desired storage (e.g. memory card or whatever your device |
| 4756 | * presents) to store this track in. Setting this to 0 will store |
| 4757 | * the track on the primary storage. |
| 4758 | * </ul> |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 4759 | * @param callback a progress indicator function or NULL to ignore. |
| 4760 | * @param data a user-defined pointer that is passed along to |
| 4761 | * the <code>progress</code> function in order to |
| 4762 | * pass along some user defined data to the progress |
| 4763 | * updates. If not used, set this to NULL. |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 4764 | * @return 0 if the transfer was successful, any other value means |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 4765 | * failure. |
| 4766 | * @see LIBMTP_Send_Track_From_File_Descriptor() |
Linus Walleij | 1b87ea7 | 2007-08-28 07:53:09 +0000 | [diff] [blame] | 4767 | * @see LIBMTP_Send_File_From_File() |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 4768 | * @see LIBMTP_Delete_Object() |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 4769 | */ |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 4770 | int LIBMTP_Send_Track_From_File(LIBMTP_mtpdevice_t *device, |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 4771 | char const * const path, LIBMTP_track_t * const metadata, |
Linus Walleij | ee73ef2 | 2006-08-27 19:56:00 +0000 | [diff] [blame] | 4772 | LIBMTP_progressfunc_t const callback, |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 4773 | void const * const data) |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 4774 | { |
| 4775 | int fd; |
| 4776 | int ret; |
| 4777 | |
| 4778 | // Sanity check |
| 4779 | if (path == NULL) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 4780 | 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] | 4781 | return -1; |
| 4782 | } |
| 4783 | |
| 4784 | // Open file |
| 4785 | #ifdef __WIN32__ |
Linus Walleij | 7beba57 | 2006-11-29 08:56:12 +0000 | [diff] [blame] | 4786 | #ifdef USE_WINDOWS_IO_H |
Linus Walleij | 5b4a4d2 | 2009-01-10 12:18:14 +0000 | [diff] [blame] | 4787 | if ( (fd = _open(path, O_RDONLY|O_BINARY) == -1) ) { |
Linus Walleij | 7beba57 | 2006-11-29 08:56:12 +0000 | [diff] [blame] | 4788 | #else |
Linus Walleij | 5b4a4d2 | 2009-01-10 12:18:14 +0000 | [diff] [blame] | 4789 | if ( (fd = open(path, O_RDONLY|O_BINARY) == -1) ) { |
Linus Walleij | 7beba57 | 2006-11-29 08:56:12 +0000 | [diff] [blame] | 4790 | #endif |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 4791 | #else |
| 4792 | if ( (fd = open(path, O_RDONLY)) == -1) { |
| 4793 | #endif |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 4794 | 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] | 4795 | return -1; |
| 4796 | } |
| 4797 | |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 4798 | ret = LIBMTP_Send_Track_From_File_Descriptor(device, fd, metadata, callback, data); |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 4799 | |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 4800 | // Close file. |
Linus Walleij | 7beba57 | 2006-11-29 08:56:12 +0000 | [diff] [blame] | 4801 | #ifdef USE_WINDOWS_IO_H |
| 4802 | _close(fd); |
| 4803 | #else |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 4804 | close(fd); |
Linus Walleij | 7beba57 | 2006-11-29 08:56:12 +0000 | [diff] [blame] | 4805 | #endif |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 4806 | |
| 4807 | return ret; |
| 4808 | } |
| 4809 | |
Linus Walleij | fa1374c | 2006-02-27 07:41:46 +0000 | [diff] [blame] | 4810 | /** |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 4811 | * This function sends a track from a file descriptor to an |
| 4812 | * MTP device. A filename and a set of metadata must be |
| 4813 | * given as input. |
| 4814 | * @param device a pointer to the device to send the track to. |
| 4815 | * @param fd the filedescriptor for a local file which will be sent. |
| 4816 | * @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] | 4817 | * After this call the field <code>metadata->item_id</code> |
| 4818 | * will contain the new track ID. Other fields such |
| 4819 | * as the <code>metadata->filename</code>, <code>metadata->parent_id</code> |
| 4820 | * or <code>metadata->storage_id</code> may also change during this |
| 4821 | * operation due to device restrictions, so do not rely on the |
| 4822 | * contents of this struct to be preserved in any way. |
| 4823 | * <ul> |
| 4824 | * <li><code>metadata->parent_id</code> should be set to the parent |
| 4825 | * (e.g. folder) to store this track in. Since some |
| 4826 | * devices are a bit picky about where files |
| 4827 | * are placed, a default folder will be chosen if libmtp |
| 4828 | * has detected one for the current filetype and this |
| 4829 | * parameter is set to 0. If this is 0 and no default folder |
| 4830 | * can be found, the file will be stored in the root folder. |
| 4831 | * <li><code>metadata->storage_id</code> should be set to the |
| 4832 | * desired storage (e.g. memory card or whatever your device |
| 4833 | * presents) to store this track in. Setting this to 0 will store |
| 4834 | * the track on the primary storage. |
| 4835 | * </ul> |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 4836 | * @param callback a progress indicator function or NULL to ignore. |
| 4837 | * @param data a user-defined pointer that is passed along to |
| 4838 | * the <code>progress</code> function in order to |
| 4839 | * pass along some user defined data to the progress |
| 4840 | * updates. If not used, set this to NULL. |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 4841 | * @return 0 if the transfer was successful, any other value means |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 4842 | * failure. |
| 4843 | * @see LIBMTP_Send_Track_From_File() |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 4844 | * @see LIBMTP_Delete_Object() |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 4845 | */ |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 4846 | int LIBMTP_Send_Track_From_File_Descriptor(LIBMTP_mtpdevice_t *device, |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 4847 | int const fd, LIBMTP_track_t * const metadata, |
Linus Walleij | ee73ef2 | 2006-08-27 19:56:00 +0000 | [diff] [blame] | 4848 | LIBMTP_progressfunc_t const callback, |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 4849 | void const * const data) |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 4850 | { |
Linus Walleij | 17e39f7 | 2006-02-23 15:54:28 +0000 | [diff] [blame] | 4851 | int subcall_ret; |
Linus Walleij | d2778d1 | 2007-08-06 19:24:58 +0000 | [diff] [blame] | 4852 | LIBMTP_file_t filedata; |
Linus Walleij | 8ae78bb | 2006-11-20 21:45:52 +0000 | [diff] [blame] | 4853 | |
Linus Walleij | 16c51f0 | 2006-05-04 13:20:22 +0000 | [diff] [blame] | 4854 | // Sanity check, is this really a track? |
Linus Walleij | 10e0ce7 | 2008-05-04 19:20:33 +0000 | [diff] [blame] | 4855 | if (!LIBMTP_FILETYPE_IS_TRACK(metadata->filetype)) { |
Linus Walleij | 46651f3 | 2008-04-26 23:30:19 +0000 | [diff] [blame] | 4856 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, |
| 4857 | "LIBMTP_Send_Track_From_File_Descriptor(): " |
| 4858 | "I don't think this is actually a track, strange filetype..."); |
Linus Walleij | 99310d4 | 2006-11-01 08:29:39 +0000 | [diff] [blame] | 4859 | } |
Linus Walleij | 3725329 | 2006-10-11 08:38:14 +0000 | [diff] [blame] | 4860 | |
Linus Walleij | d2778d1 | 2007-08-06 19:24:58 +0000 | [diff] [blame] | 4861 | // Wrap around the file transfer function |
| 4862 | filedata.item_id = metadata->item_id; |
| 4863 | filedata.parent_id = metadata->parent_id; |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 4864 | filedata.storage_id = metadata->storage_id; |
Linus Walleij | d2778d1 | 2007-08-06 19:24:58 +0000 | [diff] [blame] | 4865 | filedata.filename = metadata->filename; |
| 4866 | filedata.filesize = metadata->filesize; |
| 4867 | filedata.filetype = metadata->filetype; |
| 4868 | filedata.next = NULL; |
Linus Walleij | 3725329 | 2006-10-11 08:38:14 +0000 | [diff] [blame] | 4869 | |
Linus Walleij | d2778d1 | 2007-08-06 19:24:58 +0000 | [diff] [blame] | 4870 | subcall_ret = LIBMTP_Send_File_From_File_Descriptor(device, |
| 4871 | fd, |
| 4872 | &filedata, |
| 4873 | callback, |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 4874 | data); |
mopoke | 3136444 | 2006-11-20 04:53:04 +0000 | [diff] [blame] | 4875 | |
Linus Walleij | d2778d1 | 2007-08-06 19:24:58 +0000 | [diff] [blame] | 4876 | if (subcall_ret != 0) { |
Linus Walleij | 46651f3 | 2008-04-26 23:30:19 +0000 | [diff] [blame] | 4877 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, |
| 4878 | "LIBMTP_Send_Track_From_File_Descriptor(): " |
Linus Walleij | d2778d1 | 2007-08-06 19:24:58 +0000 | [diff] [blame] | 4879 | "subcall to LIBMTP_Send_File_From_File_Descriptor failed."); |
| 4880 | // We used to delete the file here, but don't... It might be OK after all. |
| 4881 | // (void) LIBMTP_Delete_Object(device, metadata->item_id); |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 4882 | return -1; |
| 4883 | } |
Linus Walleij | d2778d1 | 2007-08-06 19:24:58 +0000 | [diff] [blame] | 4884 | |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 4885 | // Pick up new item (and parent, storage) ID |
Linus Walleij | d2778d1 | 2007-08-06 19:24:58 +0000 | [diff] [blame] | 4886 | metadata->item_id = filedata.item_id; |
| 4887 | metadata->parent_id = filedata.parent_id; |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 4888 | metadata->storage_id = filedata.storage_id; |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 4889 | |
Linus Walleij | 17e39f7 | 2006-02-23 15:54:28 +0000 | [diff] [blame] | 4890 | // Set track metadata for the new fine track |
| 4891 | subcall_ret = LIBMTP_Update_Track_Metadata(device, metadata); |
| 4892 | if (subcall_ret != 0) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 4893 | // Subcall will add error to errorstack |
Linus Walleij | b275318 | 2007-02-26 08:11:38 +0000 | [diff] [blame] | 4894 | // We used to delete the file here, but don't... It might be OK after all. |
| 4895 | // (void) LIBMTP_Delete_Object(device, metadata->item_id); |
Linus Walleij | 17e39f7 | 2006-02-23 15:54:28 +0000 | [diff] [blame] | 4896 | return -1; |
| 4897 | } |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 4898 | |
Richard Low | 61edc1a | 2007-09-23 10:35:48 +0000 | [diff] [blame] | 4899 | // note we don't need to update the cache here because LIBMTP_Send_File_From_File_Descriptor |
| 4900 | // 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] | 4901 | |
Linus Walleij | 17e39f7 | 2006-02-23 15:54:28 +0000 | [diff] [blame] | 4902 | return 0; |
| 4903 | } |
| 4904 | |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 4905 | /** |
Richard Low | d3b1702 | 2009-04-11 12:37:39 +0000 | [diff] [blame] | 4906 | * This function sends a track from a handler function to an |
| 4907 | * MTP device. A filename and a set of metadata must be |
| 4908 | * given as input. |
| 4909 | * @param device a pointer to the device to send the track to. |
| 4910 | * @param get_func the function to call when we have data. |
| 4911 | * @param priv the user-defined pointer that is passed to |
| 4912 | * <code>get_func</code>. |
| 4913 | * @param metadata a track metadata set to be written along with the file. |
| 4914 | * After this call the field <code>metadata->item_id</code> |
| 4915 | * will contain the new track ID. Other fields such |
| 4916 | * as the <code>metadata->filename</code>, <code>metadata->parent_id</code> |
| 4917 | * or <code>metadata->storage_id</code> may also change during this |
| 4918 | * operation due to device restrictions, so do not rely on the |
| 4919 | * contents of this struct to be preserved in any way. |
| 4920 | * <ul> |
| 4921 | * <li><code>metadata->parent_id</code> should be set to the parent |
| 4922 | * (e.g. folder) to store this track in. Since some |
| 4923 | * devices are a bit picky about where files |
| 4924 | * are placed, a default folder will be chosen if libmtp |
| 4925 | * has detected one for the current filetype and this |
| 4926 | * parameter is set to 0. If this is 0 and no default folder |
| 4927 | * can be found, the file will be stored in the root folder. |
| 4928 | * <li><code>metadata->storage_id</code> should be set to the |
| 4929 | * desired storage (e.g. memory card or whatever your device |
| 4930 | * presents) to store this track in. Setting this to 0 will store |
| 4931 | * the track on the primary storage. |
| 4932 | * </ul> |
| 4933 | * @param callback a progress indicator function or NULL to ignore. |
| 4934 | * @param data a user-defined pointer that is passed along to |
| 4935 | * the <code>progress</code> function in order to |
| 4936 | * pass along some user defined data to the progress |
| 4937 | * updates. If not used, set this to NULL. |
| 4938 | * @return 0 if the transfer was successful, any other value means |
| 4939 | * failure. |
| 4940 | * @see LIBMTP_Send_Track_From_File() |
| 4941 | * @see LIBMTP_Delete_Object() |
| 4942 | */ |
| 4943 | int LIBMTP_Send_Track_From_Handler(LIBMTP_mtpdevice_t *device, |
| 4944 | MTPDataGetFunc get_func, void * priv, LIBMTP_track_t * const metadata, |
| 4945 | LIBMTP_progressfunc_t const callback, |
| 4946 | void const * const data) |
| 4947 | { |
| 4948 | int subcall_ret; |
| 4949 | LIBMTP_file_t filedata; |
| 4950 | |
| 4951 | // Sanity check, is this really a track? |
| 4952 | if (!LIBMTP_FILETYPE_IS_TRACK(metadata->filetype)) { |
| 4953 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, |
| 4954 | "LIBMTP_Send_Track_From_Handler(): " |
| 4955 | "I don't think this is actually a track, strange filetype..."); |
| 4956 | } |
| 4957 | |
| 4958 | // Wrap around the file transfer function |
| 4959 | filedata.item_id = metadata->item_id; |
| 4960 | filedata.parent_id = metadata->parent_id; |
| 4961 | filedata.storage_id = metadata->storage_id; |
| 4962 | filedata.filename = metadata->filename; |
| 4963 | filedata.filesize = metadata->filesize; |
| 4964 | filedata.filetype = metadata->filetype; |
| 4965 | filedata.next = NULL; |
| 4966 | |
| 4967 | subcall_ret = LIBMTP_Send_File_From_Handler(device, |
| 4968 | get_func, |
| 4969 | priv, |
| 4970 | &filedata, |
| 4971 | callback, |
| 4972 | data); |
| 4973 | |
| 4974 | if (subcall_ret != 0) { |
| 4975 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, |
| 4976 | "LIBMTP_Send_Track_From_Handler(): " |
| 4977 | "subcall to LIBMTP_Send_File_From_Handler failed."); |
| 4978 | // We used to delete the file here, but don't... It might be OK after all. |
| 4979 | // (void) LIBMTP_Delete_Object(device, metadata->item_id); |
| 4980 | return -1; |
| 4981 | } |
| 4982 | |
| 4983 | // Pick up new item (and parent, storage) ID |
| 4984 | metadata->item_id = filedata.item_id; |
| 4985 | metadata->parent_id = filedata.parent_id; |
| 4986 | metadata->storage_id = filedata.storage_id; |
| 4987 | |
| 4988 | // Set track metadata for the new fine track |
| 4989 | subcall_ret = LIBMTP_Update_Track_Metadata(device, metadata); |
| 4990 | if (subcall_ret != 0) { |
| 4991 | // Subcall will add error to errorstack |
| 4992 | // We used to delete the file here, but don't... It might be OK after all. |
| 4993 | // (void) LIBMTP_Delete_Object(device, metadata->item_id); |
| 4994 | return -1; |
| 4995 | } |
| 4996 | |
| 4997 | // note we don't need to update the cache here because LIBMTP_Send_File_From_File_Descriptor |
| 4998 | // has added the object handle and LIBMTP_Update_Track_Metadata has added the metadata. |
| 4999 | |
| 5000 | return 0; |
| 5001 | } |
| 5002 | |
| 5003 | /** |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 5004 | * This function sends a local file to an MTP device. |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 5005 | * A filename and a set of metadata must be |
| 5006 | * given as input. |
| 5007 | * @param device a pointer to the device to send the track to. |
| 5008 | * @param path the filename of a local file which will be sent. |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 5009 | * @param filedata a file metadata set to be written along with the file. |
| 5010 | * After this call the field <code>filedata->item_id</code> |
| 5011 | * will contain the new file ID. Other fields such |
| 5012 | * as the <code>filedata->filename</code>, <code>filedata->parent_id</code> |
| 5013 | * or <code>filedata->storage_id</code> may also change during this |
| 5014 | * operation due to device restrictions, so do not rely on the |
| 5015 | * contents of this struct to be preserved in any way. |
| 5016 | * <ul> |
| 5017 | * <li><code>filedata->parent_id</code> should be set to the parent |
| 5018 | * (e.g. folder) to store this file in. If this is 0, |
| 5019 | * the file will be stored in the root folder. |
| 5020 | * <li><code>filedata->storage_id</code> should be set to the |
| 5021 | * desired storage (e.g. memory card or whatever your device |
| 5022 | * presents) to store this file in. Setting this to 0 will store |
| 5023 | * the file on the primary storage. |
| 5024 | * </ul> |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 5025 | * @param callback a progress indicator function or NULL to ignore. |
| 5026 | * @param data a user-defined pointer that is passed along to |
| 5027 | * the <code>progress</code> function in order to |
| 5028 | * pass along some user defined data to the progress |
| 5029 | * updates. If not used, set this to NULL. |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 5030 | * @return 0 if the transfer was successful, any other value means |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 5031 | * failure. |
| 5032 | * @see LIBMTP_Send_File_From_File_Descriptor() |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 5033 | * @see LIBMTP_Delete_Object() |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 5034 | */ |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 5035 | int LIBMTP_Send_File_From_File(LIBMTP_mtpdevice_t *device, |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 5036 | char const * const path, LIBMTP_file_t * const filedata, |
Linus Walleij | ee73ef2 | 2006-08-27 19:56:00 +0000 | [diff] [blame] | 5037 | LIBMTP_progressfunc_t const callback, |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 5038 | void const * const data) |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 5039 | { |
| 5040 | int fd; |
| 5041 | int ret; |
| 5042 | |
| 5043 | // Sanity check |
| 5044 | if (path == NULL) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 5045 | 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] | 5046 | return -1; |
| 5047 | } |
| 5048 | |
| 5049 | // Open file |
| 5050 | #ifdef __WIN32__ |
Linus Walleij | 7beba57 | 2006-11-29 08:56:12 +0000 | [diff] [blame] | 5051 | #ifdef USE_WINDOWS_IO_H |
Linus Walleij | 5b4a4d2 | 2009-01-10 12:18:14 +0000 | [diff] [blame] | 5052 | if ( (fd = _open(path, O_RDONLY|O_BINARY) == -1) ) { |
Linus Walleij | 7beba57 | 2006-11-29 08:56:12 +0000 | [diff] [blame] | 5053 | #else |
Linus Walleij | 5b4a4d2 | 2009-01-10 12:18:14 +0000 | [diff] [blame] | 5054 | if ( (fd = open(path, O_RDONLY|O_BINARY) == -1) ) { |
Linus Walleij | 7beba57 | 2006-11-29 08:56:12 +0000 | [diff] [blame] | 5055 | #endif |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 5056 | #else |
| 5057 | if ( (fd = open(path, O_RDONLY)) == -1) { |
| 5058 | #endif |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 5059 | 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] | 5060 | return -1; |
| 5061 | } |
| 5062 | |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 5063 | ret = LIBMTP_Send_File_From_File_Descriptor(device, fd, filedata, callback, data); |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 5064 | |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 5065 | // Close file. |
Linus Walleij | 7beba57 | 2006-11-29 08:56:12 +0000 | [diff] [blame] | 5066 | #ifdef USE_WINDOWS_IO_H |
| 5067 | _close(fd); |
| 5068 | #else |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 5069 | close(fd); |
Linus Walleij | 7beba57 | 2006-11-29 08:56:12 +0000 | [diff] [blame] | 5070 | #endif |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 5071 | |
| 5072 | return ret; |
| 5073 | } |
| 5074 | |
Linus Walleij | d208f9c | 2006-04-27 14:16:06 +0000 | [diff] [blame] | 5075 | /** |
| 5076 | * This function sends a generic file from a file descriptor to an |
| 5077 | * MTP device. A filename and a set of metadata must be |
| 5078 | * given as input. |
Linus Walleij | cd3eb3d | 2006-09-02 21:33:22 +0000 | [diff] [blame] | 5079 | * |
| 5080 | * This can potentially be used for sending in a stream of unknown |
Linus Walleij | 1b87ea7 | 2007-08-28 07:53:09 +0000 | [diff] [blame] | 5081 | * length. Send music files with |
| 5082 | * <code>LIBMTP_Send_Track_From_File_Descriptor()</code> |
Linus Walleij | cd3eb3d | 2006-09-02 21:33:22 +0000 | [diff] [blame] | 5083 | * |
Linus Walleij | d208f9c | 2006-04-27 14:16:06 +0000 | [diff] [blame] | 5084 | * @param device a pointer to the device to send the file to. |
| 5085 | * @param fd the filedescriptor for a local file which will be sent. |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 5086 | * @param filedata a file metadata set to be written along with the file. |
| 5087 | * After this call the field <code>filedata->item_id</code> |
| 5088 | * will contain the new file ID. Other fields such |
| 5089 | * as the <code>filedata->filename</code>, <code>filedata->parent_id</code> |
| 5090 | * or <code>filedata->storage_id</code> may also change during this |
| 5091 | * operation due to device restrictions, so do not rely on the |
| 5092 | * contents of this struct to be preserved in any way. |
| 5093 | * <ul> |
| 5094 | * <li><code>filedata->parent_id</code> should be set to the parent |
| 5095 | * (e.g. folder) to store this file in. If this is 0, |
| 5096 | * the file will be stored in the root folder. |
| 5097 | * <li><code>filedata->storage_id</code> should be set to the |
| 5098 | * desired storage (e.g. memory card or whatever your device |
| 5099 | * presents) to store this file in. Setting this to 0 will store |
| 5100 | * the file on the primary storage. |
| 5101 | * </ul> |
Linus Walleij | d208f9c | 2006-04-27 14:16:06 +0000 | [diff] [blame] | 5102 | * @param callback a progress indicator function or NULL to ignore. |
| 5103 | * @param data a user-defined pointer that is passed along to |
| 5104 | * the <code>progress</code> function in order to |
| 5105 | * pass along some user defined data to the progress |
| 5106 | * updates. If not used, set this to NULL. |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 5107 | * @return 0 if the transfer was successful, any other value means |
Linus Walleij | d208f9c | 2006-04-27 14:16:06 +0000 | [diff] [blame] | 5108 | * failure. |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 5109 | * @see LIBMTP_Send_File_From_File() |
Linus Walleij | 1b87ea7 | 2007-08-28 07:53:09 +0000 | [diff] [blame] | 5110 | * @see LIBMTP_Send_Track_From_File_Descriptor() |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 5111 | * @see LIBMTP_Delete_Object() |
Linus Walleij | d208f9c | 2006-04-27 14:16:06 +0000 | [diff] [blame] | 5112 | */ |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 5113 | int LIBMTP_Send_File_From_File_Descriptor(LIBMTP_mtpdevice_t *device, |
Linus Walleij | d208f9c | 2006-04-27 14:16:06 +0000 | [diff] [blame] | 5114 | int const fd, LIBMTP_file_t * const filedata, |
Linus Walleij | ee73ef2 | 2006-08-27 19:56:00 +0000 | [diff] [blame] | 5115 | LIBMTP_progressfunc_t const callback, |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 5116 | void const * const data) |
Linus Walleij | d208f9c | 2006-04-27 14:16:06 +0000 | [diff] [blame] | 5117 | { |
| 5118 | uint16_t ret; |
Linus Walleij | d208f9c | 2006-04-27 14:16:06 +0000 | [diff] [blame] | 5119 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | d214b9b | 2006-08-26 22:08:37 +0000 | [diff] [blame] | 5120 | PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo; |
Linus Walleij | 817579a | 2008-05-23 21:21:40 +0000 | [diff] [blame] | 5121 | LIBMTP_file_t *newfilemeta; |
Richard Low | ab0d22d | 2006-11-30 22:17:49 +0000 | [diff] [blame] | 5122 | |
Richard Low | d3b1702 | 2009-04-11 12:37:39 +0000 | [diff] [blame] | 5123 | if (send_file_object_info(device, filedata)) |
| 5124 | { |
| 5125 | // no need to output an error since send_file_object_info will already have done so |
Linus Walleij | 0779577 | 2007-08-06 18:44:06 +0000 | [diff] [blame] | 5126 | return -1; |
Linus Walleij | cd3eb3d | 2006-09-02 21:33:22 +0000 | [diff] [blame] | 5127 | } |
Linus Walleij | 0779577 | 2007-08-06 18:44:06 +0000 | [diff] [blame] | 5128 | |
Richard Low | d3b1702 | 2009-04-11 12:37:39 +0000 | [diff] [blame] | 5129 | // Callbacks |
| 5130 | ptp_usb->callback_active = 1; |
| 5131 | // The callback will deactivate itself after this amount of data has been sent |
| 5132 | // One BULK header for the request, one for the data phase. No parameters to the request. |
| 5133 | ptp_usb->current_transfer_total = filedata->filesize+PTP_USB_BULK_HDR_LEN*2; |
| 5134 | ptp_usb->current_transfer_complete = 0; |
| 5135 | ptp_usb->current_transfer_callback = callback; |
| 5136 | ptp_usb->current_transfer_callback_data = data; |
| 5137 | |
| 5138 | ret = ptp_sendobject_fromfd(params, fd, filedata->filesize); |
| 5139 | |
| 5140 | ptp_usb->callback_active = 0; |
| 5141 | ptp_usb->current_transfer_callback = NULL; |
| 5142 | ptp_usb->current_transfer_callback_data = NULL; |
| 5143 | |
| 5144 | if (ret == PTP_ERROR_CANCEL) { |
| 5145 | add_error_to_errorstack(device, LIBMTP_ERROR_CANCELLED, "LIBMTP_Send_File_From_File_Descriptor(): Cancelled transfer."); |
| 5146 | return -1; |
| 5147 | } |
| 5148 | if (ret != PTP_RC_OK) { |
| 5149 | add_ptp_error_to_errorstack(device, ret, "LIBMTP_Send_File_From_File_Descriptor(): " |
| 5150 | "Could not send object."); |
| 5151 | return -1; |
| 5152 | } |
| 5153 | |
| 5154 | add_object_to_cache(device, filedata->item_id); |
| 5155 | |
| 5156 | /* |
| 5157 | * Get the device-assined parent_id from the cache. |
| 5158 | * The operation that adds it to the cache will |
| 5159 | * look it up from the device, so we get the new |
| 5160 | * parent_id from the cache. |
| 5161 | */ |
| 5162 | newfilemeta = LIBMTP_Get_Filemetadata(device, filedata->item_id); |
| 5163 | if (newfilemeta != NULL) { |
| 5164 | filedata->parent_id = newfilemeta->parent_id; |
| 5165 | filedata->storage_id = newfilemeta->storage_id; |
| 5166 | LIBMTP_destroy_file_t(newfilemeta); |
| 5167 | } else { |
| 5168 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, |
| 5169 | "LIBMTP_Send_File_From_File_Descriptor(): " |
| 5170 | "Could not retrieve updated metadata."); |
| 5171 | return -1; |
| 5172 | } |
| 5173 | |
| 5174 | return 0; |
| 5175 | } |
| 5176 | |
| 5177 | /** |
| 5178 | * This function sends a generic file from a handler function to an |
| 5179 | * MTP device. A filename and a set of metadata must be |
| 5180 | * given as input. |
| 5181 | * |
| 5182 | * This can potentially be used for sending in a stream of unknown |
| 5183 | * length. Send music files with |
| 5184 | * <code>LIBMTP_Send_Track_From_Handler()</code> |
| 5185 | * |
| 5186 | * @param device a pointer to the device to send the file to. |
| 5187 | * @param get_func the function to call to get data to write |
| 5188 | * @param priv a user-defined pointer that is passed along to |
| 5189 | * <code>get_func</code>. If not used, this is set to NULL. |
| 5190 | * @param filedata a file metadata set to be written along with the file. |
| 5191 | * After this call the field <code>filedata->item_id</code> |
| 5192 | * will contain the new file ID. Other fields such |
| 5193 | * as the <code>filedata->filename</code>, <code>filedata->parent_id</code> |
| 5194 | * or <code>filedata->storage_id</code> may also change during this |
| 5195 | * operation due to device restrictions, so do not rely on the |
| 5196 | * contents of this struct to be preserved in any way. |
| 5197 | * <ul> |
| 5198 | * <li><code>filedata->parent_id</code> should be set to the parent |
| 5199 | * (e.g. folder) to store this file in. If this is 0, |
| 5200 | * the file will be stored in the root folder. |
| 5201 | * <li><code>filedata->storage_id</code> should be set to the |
| 5202 | * desired storage (e.g. memory card or whatever your device |
| 5203 | * presents) to store this file in. Setting this to 0 will store |
| 5204 | * the file on the primary storage. |
| 5205 | * </ul> |
| 5206 | * @param callback a progress indicator function or NULL to ignore. |
| 5207 | * @param data a user-defined pointer that is passed along to |
| 5208 | * the <code>progress</code> function in order to |
| 5209 | * pass along some user defined data to the progress |
| 5210 | * updates. If not used, set this to NULL. |
| 5211 | * @return 0 if the transfer was successful, any other value means |
| 5212 | * failure. |
| 5213 | * @see LIBMTP_Send_File_From_File() |
| 5214 | * @see LIBMTP_Send_Track_From_File_Descriptor() |
| 5215 | * @see LIBMTP_Delete_Object() |
| 5216 | */ |
| 5217 | int LIBMTP_Send_File_From_Handler(LIBMTP_mtpdevice_t *device, |
| 5218 | MTPDataGetFunc get_func, void * priv, LIBMTP_file_t * const filedata, |
| 5219 | LIBMTP_progressfunc_t const callback, void const * const data) |
| 5220 | { |
| 5221 | uint16_t ret; |
| 5222 | PTPParams *params = (PTPParams *) device->params; |
| 5223 | PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo; |
| 5224 | LIBMTP_file_t *newfilemeta; |
| 5225 | |
| 5226 | if (send_file_object_info(device, filedata)) |
| 5227 | { |
| 5228 | // no need to output an error since send_file_object_info will already have done so |
| 5229 | return -1; |
| 5230 | } |
Linus Walleij | d866d24 | 2009-08-23 21:50:39 +0000 | [diff] [blame] | 5231 | |
Richard Low | d3b1702 | 2009-04-11 12:37:39 +0000 | [diff] [blame] | 5232 | // Callbacks |
| 5233 | ptp_usb->callback_active = 1; |
| 5234 | // The callback will deactivate itself after this amount of data has been sent |
| 5235 | // One BULK header for the request, one for the data phase. No parameters to the request. |
| 5236 | ptp_usb->current_transfer_total = filedata->filesize+PTP_USB_BULK_HDR_LEN*2; |
| 5237 | ptp_usb->current_transfer_complete = 0; |
| 5238 | ptp_usb->current_transfer_callback = callback; |
| 5239 | ptp_usb->current_transfer_callback_data = data; |
Linus Walleij | d866d24 | 2009-08-23 21:50:39 +0000 | [diff] [blame] | 5240 | |
Richard Low | 5b4023c | 2009-04-16 19:14:38 +0000 | [diff] [blame] | 5241 | MTPDataHandler mtp_handler; |
| 5242 | mtp_handler.getfunc = get_func; |
| 5243 | mtp_handler.putfunc = NULL; |
Linus Walleij | d866d24 | 2009-08-23 21:50:39 +0000 | [diff] [blame] | 5244 | mtp_handler.priv = priv; |
| 5245 | |
Richard Low | d3b1702 | 2009-04-11 12:37:39 +0000 | [diff] [blame] | 5246 | PTPDataHandler handler; |
Richard Low | 5b4023c | 2009-04-16 19:14:38 +0000 | [diff] [blame] | 5247 | handler.getfunc = get_func_wrapper; |
Richard Low | d3b1702 | 2009-04-11 12:37:39 +0000 | [diff] [blame] | 5248 | handler.putfunc = NULL; |
Linus Walleij | d866d24 | 2009-08-23 21:50:39 +0000 | [diff] [blame] | 5249 | handler.priv = &mtp_handler; |
| 5250 | |
Richard Low | d3b1702 | 2009-04-11 12:37:39 +0000 | [diff] [blame] | 5251 | ret = ptp_sendobject_from_handler(params, &handler, filedata->filesize); |
Linus Walleij | d866d24 | 2009-08-23 21:50:39 +0000 | [diff] [blame] | 5252 | |
Richard Low | d3b1702 | 2009-04-11 12:37:39 +0000 | [diff] [blame] | 5253 | ptp_usb->callback_active = 0; |
| 5254 | ptp_usb->current_transfer_callback = NULL; |
| 5255 | ptp_usb->current_transfer_callback_data = NULL; |
| 5256 | |
| 5257 | if (ret == PTP_ERROR_CANCEL) { |
| 5258 | add_error_to_errorstack(device, LIBMTP_ERROR_CANCELLED, "LIBMTP_Send_File_From_Handler(): Cancelled transfer."); |
| 5259 | return -1; |
| 5260 | } |
| 5261 | if (ret != PTP_RC_OK) { |
| 5262 | add_ptp_error_to_errorstack(device, ret, "LIBMTP_Send_File_From_Handler(): " |
| 5263 | "Could not send object."); |
| 5264 | return -1; |
| 5265 | } |
| 5266 | |
| 5267 | add_object_to_cache(device, filedata->item_id); |
Linus Walleij | d866d24 | 2009-08-23 21:50:39 +0000 | [diff] [blame] | 5268 | |
Richard Low | d3b1702 | 2009-04-11 12:37:39 +0000 | [diff] [blame] | 5269 | /* |
| 5270 | * Get the device-assined parent_id from the cache. |
| 5271 | * The operation that adds it to the cache will |
| 5272 | * look it up from the device, so we get the new |
| 5273 | * parent_id from the cache. |
| 5274 | */ |
| 5275 | newfilemeta = LIBMTP_Get_Filemetadata(device, filedata->item_id); |
| 5276 | if (newfilemeta != NULL) { |
| 5277 | filedata->parent_id = newfilemeta->parent_id; |
| 5278 | filedata->storage_id = newfilemeta->storage_id; |
| 5279 | LIBMTP_destroy_file_t(newfilemeta); |
| 5280 | } else { |
| 5281 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, |
| 5282 | "LIBMTP_Send_File_From_Handler(): " |
| 5283 | "Could not retrieve updated metadata."); |
| 5284 | return -1; |
| 5285 | } |
| 5286 | |
| 5287 | return 0; |
| 5288 | } |
| 5289 | |
Linus Walleij | d866d24 | 2009-08-23 21:50:39 +0000 | [diff] [blame] | 5290 | /** |
Richard Low | d3b1702 | 2009-04-11 12:37:39 +0000 | [diff] [blame] | 5291 | * This function sends the file object info, ready for sendobject |
| 5292 | * @param device a pointer to the device to send the file to. |
| 5293 | * @param filedata a file metadata set to be written along with the file. |
| 5294 | * @return 0 if the transfer was successful, any other value means |
| 5295 | * failure. |
| 5296 | */ |
| 5297 | static int send_file_object_info(LIBMTP_mtpdevice_t *device, LIBMTP_file_t *filedata) |
| 5298 | { |
| 5299 | PTPParams *params = (PTPParams *) device->params; |
| 5300 | PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo; |
| 5301 | uint32_t store; |
| 5302 | int use_primary_storage = 1; |
| 5303 | uint16_t of = map_libmtp_type_to_ptp_type(filedata->filetype); |
| 5304 | LIBMTP_devicestorage_t *storage; |
| 5305 | uint32_t localph = filedata->parent_id; |
| 5306 | uint16_t ret; |
| 5307 | int i; |
| 5308 | |
| 5309 | // Sanity check: no zerolength files. |
| 5310 | if (filedata->filesize == 0) { |
| 5311 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "send_file_object_info(): " |
| 5312 | "File of zero size."); |
| 5313 | return -1; |
| 5314 | } |
| 5315 | |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 5316 | if (filedata->storage_id != 0) { |
| 5317 | store = filedata->storage_id; |
Linus Walleij | fb28b63 | 2007-10-23 21:56:18 +0000 | [diff] [blame] | 5318 | } else { |
Linus Walleij | d71d0b3 | 2008-09-22 08:21:03 +0000 | [diff] [blame] | 5319 | store = get_writeable_storageid(device, filedata->filesize); |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 5320 | } |
| 5321 | // Detect if something non-primary is in use. |
| 5322 | storage = device->storage; |
Richard Low | 4d9165f | 2008-09-23 20:13:17 +0000 | [diff] [blame] | 5323 | if (storage != NULL && store != storage->id) { |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 5324 | use_primary_storage = 0; |
Linus Walleij | 6bf68b4 | 2007-09-03 22:50:02 +0000 | [diff] [blame] | 5325 | } |
Linus Walleij | d208f9c | 2006-04-27 14:16:06 +0000 | [diff] [blame] | 5326 | |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 5327 | /* |
Linus Walleij | 05ccbe7 | 2006-06-13 07:46:58 +0000 | [diff] [blame] | 5328 | * If no destination folder was given, look up a default |
| 5329 | * folder if possible. Perhaps there is some way of retrieveing |
| 5330 | * the default folder for different forms of content, what |
| 5331 | * do I know, we use a fixed list in lack of any better method. |
| 5332 | * Some devices obviously need to have their files in certain |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 5333 | * folders in order to find/display them at all (hello Creative), |
Linus Walleij | c40c9bf | 2008-06-13 23:24:17 +0000 | [diff] [blame] | 5334 | * so we have to have a method for this. We only do this if the |
| 5335 | * primary storage is in use. |
Linus Walleij | 05ccbe7 | 2006-06-13 07:46:58 +0000 | [diff] [blame] | 5336 | */ |
| 5337 | |
Linus Walleij | c40c9bf | 2008-06-13 23:24:17 +0000 | [diff] [blame] | 5338 | if (localph == 0 && use_primary_storage) { |
Linus Walleij | 10e0ce7 | 2008-05-04 19:20:33 +0000 | [diff] [blame] | 5339 | if (LIBMTP_FILETYPE_IS_AUDIO(filedata->filetype)) { |
Linus Walleij | 05ccbe7 | 2006-06-13 07:46:58 +0000 | [diff] [blame] | 5340 | localph = device->default_music_folder; |
Linus Walleij | 10e0ce7 | 2008-05-04 19:20:33 +0000 | [diff] [blame] | 5341 | } else if (LIBMTP_FILETYPE_IS_VIDEO(filedata->filetype)) { |
Linus Walleij | 05ccbe7 | 2006-06-13 07:46:58 +0000 | [diff] [blame] | 5342 | localph = device->default_video_folder; |
| 5343 | } else if (of == PTP_OFC_EXIF_JPEG || |
Linus Walleij | 5fb4713 | 2006-12-30 15:35:48 +0000 | [diff] [blame] | 5344 | of == PTP_OFC_JP2 || |
| 5345 | of == PTP_OFC_JPX || |
Linus Walleij | 05ccbe7 | 2006-06-13 07:46:58 +0000 | [diff] [blame] | 5346 | of == PTP_OFC_JFIF || |
| 5347 | of == PTP_OFC_TIFF || |
Linus Walleij | 5fb4713 | 2006-12-30 15:35:48 +0000 | [diff] [blame] | 5348 | of == PTP_OFC_TIFF_IT || |
Linus Walleij | 05ccbe7 | 2006-06-13 07:46:58 +0000 | [diff] [blame] | 5349 | of == PTP_OFC_BMP || |
| 5350 | of == PTP_OFC_GIF || |
| 5351 | of == PTP_OFC_PICT || |
| 5352 | of == PTP_OFC_PNG || |
| 5353 | of == PTP_OFC_MTP_WindowsImageFormat) { |
| 5354 | localph = device->default_picture_folder; |
| 5355 | } else if (of == PTP_OFC_MTP_vCalendar1 || |
Linus Walleij | d7aa5b2 | 2006-09-02 11:52:31 +0000 | [diff] [blame] | 5356 | of == PTP_OFC_MTP_vCalendar2 || |
| 5357 | of == PTP_OFC_MTP_UndefinedContact || |
| 5358 | of == PTP_OFC_MTP_vCard2 || |
| 5359 | of == PTP_OFC_MTP_vCard3 || |
| 5360 | of == PTP_OFC_MTP_UndefinedCalendarItem) { |
Linus Walleij | 05ccbe7 | 2006-06-13 07:46:58 +0000 | [diff] [blame] | 5361 | localph = device->default_organizer_folder; |
Linus Walleij | 46651f3 | 2008-04-26 23:30:19 +0000 | [diff] [blame] | 5362 | } else if (of == PTP_OFC_Text) { |
Linus Walleij | 9316e65 | 2006-12-07 09:55:21 +0000 | [diff] [blame] | 5363 | localph = device->default_text_folder; |
Linus Walleij | 05ccbe7 | 2006-06-13 07:46:58 +0000 | [diff] [blame] | 5364 | } |
| 5365 | } |
mopoke | 3136444 | 2006-11-20 04:53:04 +0000 | [diff] [blame] | 5366 | |
Linus Walleij | a3544f6 | 2007-11-30 01:20:04 +0000 | [diff] [blame] | 5367 | // Here we wire the type to unknown on bugged, but |
Linus Walleij | 89bb1cd | 2009-07-24 21:03:36 +0000 | [diff] [blame] | 5368 | // Ogg or FLAC-supportive devices. |
Linus Walleij | fec4d56 | 2008-06-01 22:30:36 +0000 | [diff] [blame] | 5369 | if (FLAG_OGG_IS_UNKNOWN(ptp_usb) && of == PTP_OFC_MTP_OGG) { |
Linus Walleij | a3544f6 | 2007-11-30 01:20:04 +0000 | [diff] [blame] | 5370 | of = PTP_OFC_Undefined; |
| 5371 | } |
Linus Walleij | 89bb1cd | 2009-07-24 21:03:36 +0000 | [diff] [blame] | 5372 | if (FLAG_FLAC_IS_UNKNOWN(ptp_usb) && of == PTP_OFC_MTP_FLAC) { |
| 5373 | of = PTP_OFC_Undefined; |
| 5374 | } |
Linus Walleij | a3544f6 | 2007-11-30 01:20:04 +0000 | [diff] [blame] | 5375 | |
Linus Walleij | f67c1ad | 2009-01-14 21:39:50 +0000 | [diff] [blame] | 5376 | if (ptp_operation_issupported(params, PTP_OC_MTP_SendObjectPropList) && |
| 5377 | !FLAG_BROKEN_SEND_OBJECT_PROPLIST(ptp_usb)) { |
Linus Walleij | d2778d1 | 2007-08-06 19:24:58 +0000 | [diff] [blame] | 5378 | /* |
| 5379 | * MTP enhanched does it this way (from a sniff): |
| 5380 | * -> PTP_OC_MTP_SendObjectPropList (0x9808): |
| 5381 | * 20 00 00 00 01 00 08 98 1B 00 00 00 01 00 01 00 |
| 5382 | * FF FF FF FF 00 30 00 00 00 00 00 00 12 5E 00 00 |
| 5383 | * Length: 0x00000020 |
| 5384 | * Type: 0x0001 PTP_USB_CONTAINER_COMMAND |
| 5385 | * Code: 0x9808 |
| 5386 | * Transaction ID: 0x0000001B |
| 5387 | * Param1: 0x00010001 <- store |
| 5388 | * Param2: 0xffffffff <- parent handle (-1 ?) |
| 5389 | * Param3: 0x00003000 <- file type PTP_OFC_Undefined - we don't know about PDF files |
| 5390 | * Param4: 0x00000000 <- file length MSB (-0x0c header len) |
| 5391 | * Param5: 0x00005e12 <- file length LSB (-0x0c header len) |
| 5392 | * |
| 5393 | * -> PTP_OC_MTP_SendObjectPropList (0x9808): |
| 5394 | * 46 00 00 00 02 00 08 98 1B 00 00 00 03 00 00 00 |
| 5395 | * 00 00 00 00 07 DC FF FF 0D 4B 00 53 00 30 00 36 - dc07 = file name |
| 5396 | * 00 30 00 33 00 30 00 36 00 2E 00 70 00 64 00 66 |
| 5397 | * 00 00 00 00 00 00 00 03 DC 04 00 00 00 00 00 00 - dc03 = protection status |
| 5398 | * 00 4F DC 02 00 01 - dc4f = non consumable |
| 5399 | * Length: 0x00000046 |
| 5400 | * Type: 0x0002 PTP_USB_CONTAINER_DATA |
| 5401 | * Code: 0x9808 |
| 5402 | * Transaction ID: 0x0000001B |
| 5403 | * Metadata.... |
| 5404 | * 0x00000003 <- Number of metadata items |
| 5405 | * 0x00000000 <- Object handle, set to 0x00000000 since it is unknown! |
| 5406 | * 0xdc07 <- metadata type: file name |
| 5407 | * 0xffff <- metadata type: string |
| 5408 | * 0x0d <- number of (uint16_t) characters |
| 5409 | * 4b 53 30 36 30 33 30 36 2e 50 64 66 00 "KS060306.pdf", null terminated |
| 5410 | * 0x00000000 <- Object handle, set to 0x00000000 since it is unknown! |
| 5411 | * 0xdc03 <- metadata type: protection status |
| 5412 | * 0x0004 <- metadata type: uint16_t |
| 5413 | * 0x0000 <- not protected |
| 5414 | * 0x00000000 <- Object handle, set to 0x00000000 since it is unknown! |
| 5415 | * 0xdc4f <- non consumable |
| 5416 | * 0x0002 <- metadata type: uint8_t |
| 5417 | * 0x01 <- non-consumable (this device cannot display PDF) |
| 5418 | * |
| 5419 | * <- Read 0x18 bytes back |
| 5420 | * 18 00 00 00 03 00 01 20 1B 00 00 00 01 00 01 00 |
| 5421 | * 00 00 00 00 01 40 00 00 |
| 5422 | * Length: 0x000000018 |
| 5423 | * Type: 0x0003 PTP_USB_CONTAINER_RESPONSE |
| 5424 | * Code: 0x2001 PTP_OK |
| 5425 | * Transaction ID: 0x0000001B |
| 5426 | * Param1: 0x00010001 <- store |
| 5427 | * Param2: 0x00000000 <- parent handle |
| 5428 | * Param3: 0x00004001 <- new file/object ID |
| 5429 | * |
| 5430 | * -> PTP_OC_SendObject (0x100d) |
| 5431 | * 0C 00 00 00 01 00 0D 10 1C 00 00 00 |
| 5432 | * -> ... all the bytes ... |
| 5433 | * <- Read 0x0c bytes back |
| 5434 | * 0C 00 00 00 03 00 01 20 1C 00 00 00 |
| 5435 | * ... Then update metadata one-by one, actually (instead of sending it first!) ... |
| 5436 | */ |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 5437 | MTPProperties *props = NULL; |
| 5438 | int nrofprops = 0; |
| 5439 | MTPProperties *prop = NULL; |
| 5440 | uint16_t *properties = NULL; |
Linus Walleij | 0779577 | 2007-08-06 18:44:06 +0000 | [diff] [blame] | 5441 | uint32_t propcnt = 0; |
Richard Low | 6c0a6ce | 2006-11-26 10:42:08 +0000 | [diff] [blame] | 5442 | |
Linus Walleij | 9036b33 | 2008-05-23 21:01:36 +0000 | [diff] [blame] | 5443 | // default parent handle |
Linus Walleij | 0779577 | 2007-08-06 18:44:06 +0000 | [diff] [blame] | 5444 | if (localph == 0) |
| 5445 | localph = 0xFFFFFFFFU; // Set to -1 |
| 5446 | |
Richard Low | 6c0a6ce | 2006-11-26 10:42:08 +0000 | [diff] [blame] | 5447 | // Must be 0x00000000U for new objects |
| 5448 | filedata->item_id = 0x00000000U; |
Linus Walleij | 05ccbe7 | 2006-06-13 07:46:58 +0000 | [diff] [blame] | 5449 | |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 5450 | ret = ptp_mtp_getobjectpropssupported(params, of, &propcnt, &properties); |
mopoke | 3136444 | 2006-11-20 04:53:04 +0000 | [diff] [blame] | 5451 | |
rreardon | bbb4e56 | 2006-11-19 22:16:11 +0000 | [diff] [blame] | 5452 | for (i=0;i<propcnt;i++) { |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 5453 | PTPObjectPropDesc opd; |
| 5454 | |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 5455 | ret = ptp_mtp_getobjectpropdesc(params, properties[i], of, &opd); |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 5456 | if (ret != PTP_RC_OK) { |
Richard Low | d3b1702 | 2009-04-11 12:37:39 +0000 | [diff] [blame] | 5457 | add_ptp_error_to_errorstack(device, ret, "send_file_object_info(): " |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 5458 | "could not get property description."); |
| 5459 | } else if (opd.GetSet) { |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 5460 | switch (properties[i]) { |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 5461 | case PTP_OPC_ObjectFileName: |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 5462 | prop = ptp_get_new_object_prop_entry(&props,&nrofprops); |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 5463 | prop->ObjectHandle = filedata->item_id; |
| 5464 | prop->property = PTP_OPC_ObjectFileName; |
| 5465 | prop->datatype = PTP_DTC_STR; |
Linus Walleij | d3b7857 | 2007-08-24 21:28:24 +0000 | [diff] [blame] | 5466 | if (filedata->filename != NULL) { |
Linus Walleij | 0779577 | 2007-08-06 18:44:06 +0000 | [diff] [blame] | 5467 | prop->propval.str = strdup(filedata->filename); |
Linus Walleij | fec4d56 | 2008-06-01 22:30:36 +0000 | [diff] [blame] | 5468 | if (FLAG_ONLY_7BIT_FILENAMES(ptp_usb)) { |
Linus Walleij | d3b7857 | 2007-08-24 21:28:24 +0000 | [diff] [blame] | 5469 | strip_7bit_from_utf8(prop->propval.str); |
| 5470 | } |
| 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 | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 5475 | prop->ObjectHandle = filedata->item_id; |
| 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 | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 5482 | prop->ObjectHandle = filedata->item_id; |
| 5483 | prop->property = PTP_OPC_NonConsumable; |
| 5484 | prop->datatype = PTP_DTC_UINT8; |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 5485 | prop->propval.u8 = 0x00; /* It is supported, then it is consumable */ |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 5486 | break; |
| 5487 | case PTP_OPC_Name: |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 5488 | prop = ptp_get_new_object_prop_entry(&props,&nrofprops); |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 5489 | prop->ObjectHandle = filedata->item_id; |
| 5490 | prop->property = PTP_OPC_Name; |
| 5491 | prop->datatype = PTP_DTC_STR; |
Linus Walleij | 0779577 | 2007-08-06 18:44:06 +0000 | [diff] [blame] | 5492 | if (filedata->filename != NULL) |
| 5493 | prop->propval.str = strdup(filedata->filename); |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 5494 | break; |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 5495 | case PTP_OPC_DateModified: |
| 5496 | // Tag with current time if that is supported |
Linus Walleij | cf8dc2b | 2008-10-21 13:58:36 +0000 | [diff] [blame] | 5497 | if (!FLAG_CANNOT_HANDLE_DATEMODIFIED(ptp_usb)) { |
| 5498 | prop = ptp_get_new_object_prop_entry(&props,&nrofprops); |
| 5499 | prop->ObjectHandle = filedata->item_id; |
| 5500 | prop->property = PTP_OPC_DateModified; |
| 5501 | prop->datatype = PTP_DTC_STR; |
| 5502 | prop->propval.str = get_iso8601_stamp(); |
Richard Low | d3b1702 | 2009-04-11 12:37:39 +0000 | [diff] [blame] | 5503 | filedata->modificationdate = time(NULL); |
Linus Walleij | cf8dc2b | 2008-10-21 13:58:36 +0000 | [diff] [blame] | 5504 | } |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 5505 | break; |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 5506 | } |
rreardon | bbb4e56 | 2006-11-19 22:16:11 +0000 | [diff] [blame] | 5507 | } |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 5508 | ptp_free_objectpropdesc(&opd); |
Linus Walleij | 99310d4 | 2006-11-01 08:29:39 +0000 | [diff] [blame] | 5509 | } |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 5510 | free(properties); |
mopoke | 3136444 | 2006-11-20 04:53:04 +0000 | [diff] [blame] | 5511 | |
rreardon | bbb4e56 | 2006-11-19 22:16:11 +0000 | [diff] [blame] | 5512 | ret = ptp_mtp_sendobjectproplist(params, &store, &localph, &filedata->item_id, |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 5513 | of, filedata->filesize, props, nrofprops); |
mopoke | 3136444 | 2006-11-20 04:53:04 +0000 | [diff] [blame] | 5514 | |
rreardon | bbb4e56 | 2006-11-19 22:16:11 +0000 | [diff] [blame] | 5515 | /* Free property list */ |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 5516 | ptp_destroy_object_prop_list(props, nrofprops); |
mopoke | 3136444 | 2006-11-20 04:53:04 +0000 | [diff] [blame] | 5517 | |
rreardon | bbb4e56 | 2006-11-19 22:16:11 +0000 | [diff] [blame] | 5518 | if (ret != PTP_RC_OK) { |
Richard Low | d3b1702 | 2009-04-11 12:37:39 +0000 | [diff] [blame] | 5519 | add_ptp_error_to_errorstack(device, ret, "send_file_object_info():" |
Linus Walleij | 0779577 | 2007-08-06 18:44:06 +0000 | [diff] [blame] | 5520 | "Could not send object property list."); |
rreardon | bbb4e56 | 2006-11-19 22:16:11 +0000 | [diff] [blame] | 5521 | if (ret == PTP_RC_AccessDenied) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 5522 | add_ptp_error_to_errorstack(device, ret, "ACCESS DENIED."); |
rreardon | bbb4e56 | 2006-11-19 22:16:11 +0000 | [diff] [blame] | 5523 | } |
| 5524 | return -1; |
| 5525 | } |
| 5526 | } else if (ptp_operation_issupported(params,PTP_OC_SendObjectInfo)) { |
Linus Walleij | 0779577 | 2007-08-06 18:44:06 +0000 | [diff] [blame] | 5527 | PTPObjectInfo new_file; |
| 5528 | |
| 5529 | memset(&new_file, 0, sizeof(PTPObjectInfo)); |
| 5530 | |
| 5531 | new_file.Filename = filedata->filename; |
Linus Walleij | fec4d56 | 2008-06-01 22:30:36 +0000 | [diff] [blame] | 5532 | if (FLAG_ONLY_7BIT_FILENAMES(ptp_usb)) { |
Linus Walleij | d3b7857 | 2007-08-24 21:28:24 +0000 | [diff] [blame] | 5533 | strip_7bit_from_utf8(new_file.Filename); |
| 5534 | } |
Richard Low | 5f6fff8 | 2009-04-11 12:45:04 +0000 | [diff] [blame] | 5535 | // We lose precision here. |
Linus Walleij | 1b87ea7 | 2007-08-28 07:53:09 +0000 | [diff] [blame] | 5536 | new_file.ObjectCompressedSize = (uint32_t) filedata->filesize; |
Linus Walleij | 0779577 | 2007-08-06 18:44:06 +0000 | [diff] [blame] | 5537 | new_file.ObjectFormat = of; |
| 5538 | new_file.StorageID = store; |
| 5539 | new_file.ParentObject = localph; |
Richard Low | 5f6fff8 | 2009-04-11 12:45:04 +0000 | [diff] [blame] | 5540 | new_file.ModificationDate = time(NULL); |
Linus Walleij | 0779577 | 2007-08-06 18:44:06 +0000 | [diff] [blame] | 5541 | |
Linus Walleij | 9be685b | 2006-11-21 09:44:53 +0000 | [diff] [blame] | 5542 | // Create the object |
| 5543 | ret = ptp_sendobjectinfo(params, &store, &localph, &filedata->item_id, &new_file); |
Linus Walleij | 0779577 | 2007-08-06 18:44:06 +0000 | [diff] [blame] | 5544 | |
Linus Walleij | 9be685b | 2006-11-21 09:44:53 +0000 | [diff] [blame] | 5545 | if (ret != PTP_RC_OK) { |
Richard Low | d3b1702 | 2009-04-11 12:37:39 +0000 | [diff] [blame] | 5546 | add_ptp_error_to_errorstack(device, ret, "send_file_object_info(): " |
Linus Walleij | 0779577 | 2007-08-06 18:44:06 +0000 | [diff] [blame] | 5547 | "Could not send object info."); |
Linus Walleij | 9be685b | 2006-11-21 09:44:53 +0000 | [diff] [blame] | 5548 | if (ret == PTP_RC_AccessDenied) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 5549 | add_ptp_error_to_errorstack(device, ret, "ACCESS DENIED."); |
Linus Walleij | 9be685b | 2006-11-21 09:44:53 +0000 | [diff] [blame] | 5550 | } |
| 5551 | return -1; |
| 5552 | } |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 5553 | // NOTE: the char* pointers inside new_file are not copies so don't |
| 5554 | // try to destroy this objectinfo! |
rreardon | bbb4e56 | 2006-11-19 22:16:11 +0000 | [diff] [blame] | 5555 | } |
Linus Walleij | d2778d1 | 2007-08-06 19:24:58 +0000 | [diff] [blame] | 5556 | |
| 5557 | // Now there IS an object with this parent handle. |
| 5558 | filedata->parent_id = localph; |
Linus Walleij | 1b87ea7 | 2007-08-28 07:53:09 +0000 | [diff] [blame] | 5559 | |
Linus Walleij | d208f9c | 2006-04-27 14:16:06 +0000 | [diff] [blame] | 5560 | return 0; |
| 5561 | } |
| 5562 | |
Linus Walleij | 17e39f7 | 2006-02-23 15:54:28 +0000 | [diff] [blame] | 5563 | /** |
Linus Walleij | 9036b33 | 2008-05-23 21:01:36 +0000 | [diff] [blame] | 5564 | * This function updates the MTP track object metadata on a |
| 5565 | * single file identified by an object ID. |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 5566 | * @param device a pointer to the device to update the track |
Linus Walleij | 95698cd | 2006-02-24 10:40:40 +0000 | [diff] [blame] | 5567 | * metadata on. |
Linus Walleij | 17e39f7 | 2006-02-23 15:54:28 +0000 | [diff] [blame] | 5568 | * @param metadata a track metadata set to be written to the file. |
| 5569 | * notice that the <code>track_id</code> field of the |
| 5570 | * metadata structure must be correct so that the |
Linus Walleij | a498273 | 2006-02-24 15:46:02 +0000 | [diff] [blame] | 5571 | * function can update the right file. If some properties |
| 5572 | * of this metadata are set to NULL (strings) or 0 |
| 5573 | * (numerical values) they will be discarded and the |
| 5574 | * track will not be tagged with these blank values. |
Richard Low | af20b5d | 2006-12-17 18:00:59 +0000 | [diff] [blame] | 5575 | * @return 0 on success, any other value means failure. If some |
| 5576 | * or all of the properties fail to update we will still |
| 5577 | * return success. On some devices (notably iRiver T30) |
| 5578 | * properties that exist cannot be updated. |
Linus Walleij | 17e39f7 | 2006-02-23 15:54:28 +0000 | [diff] [blame] | 5579 | */ |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 5580 | int LIBMTP_Update_Track_Metadata(LIBMTP_mtpdevice_t *device, |
Linus Walleij | 17e39f7 | 2006-02-23 15:54:28 +0000 | [diff] [blame] | 5581 | LIBMTP_track_t const * const metadata) |
| 5582 | { |
Linus Walleij | 17e39f7 | 2006-02-23 15:54:28 +0000 | [diff] [blame] | 5583 | uint16_t ret; |
Linus Walleij | 00cf064 | 2006-07-26 20:40:59 +0000 | [diff] [blame] | 5584 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | f9267e9 | 2007-10-15 21:07:37 +0000 | [diff] [blame] | 5585 | PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo; |
Linus Walleij | 00cf064 | 2006-07-26 20:40:59 +0000 | [diff] [blame] | 5586 | uint32_t i; |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 5587 | uint16_t *properties = NULL; |
Linus Walleij | 00cf064 | 2006-07-26 20:40:59 +0000 | [diff] [blame] | 5588 | uint32_t propcnt = 0; |
Linus Walleij | 17e39f7 | 2006-02-23 15:54:28 +0000 | [diff] [blame] | 5589 | |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 5590 | // 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] | 5591 | // 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] | 5592 | 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] | 5593 | if (ret != PTP_RC_OK) { |
| 5594 | // Just bail out for now, nothing is ever set. |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 5595 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): " |
| 5596 | "could not retrieve supported object properties."); |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 5597 | return -1; |
Richard Low | 15731fe | 2007-03-22 20:27:20 +0000 | [diff] [blame] | 5598 | } |
Linus Walleij | f9267e9 | 2007-10-15 21:07:37 +0000 | [diff] [blame] | 5599 | if (ptp_operation_issupported(params, PTP_OC_MTP_SetObjPropList) && |
Linus Walleij | fec4d56 | 2008-06-01 22:30:36 +0000 | [diff] [blame] | 5600 | !FLAG_BROKEN_SET_OBJECT_PROPLIST(ptp_usb)) { |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 5601 | MTPProperties *props = NULL; |
| 5602 | MTPProperties *prop = NULL; |
| 5603 | int nrofprops = 0; |
Richard Low | 15731fe | 2007-03-22 20:27:20 +0000 | [diff] [blame] | 5604 | |
| 5605 | for (i=0;i<propcnt;i++) { |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 5606 | PTPObjectPropDesc opd; |
| 5607 | |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 5608 | 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] | 5609 | if (ret != PTP_RC_OK) { |
| 5610 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): " |
| 5611 | "could not get property description."); |
| 5612 | } else if (opd.GetSet) { |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 5613 | switch (properties[i]) { |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 5614 | case PTP_OPC_Name: |
| 5615 | if (metadata->title == NULL) |
| 5616 | break; |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 5617 | prop = ptp_get_new_object_prop_entry(&props, &nrofprops); |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 5618 | prop->ObjectHandle = metadata->item_id; |
| 5619 | prop->property = PTP_OPC_Name; |
| 5620 | prop->datatype = PTP_DTC_STR; |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 5621 | prop->propval.str = strdup(metadata->title); |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 5622 | break; |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 5623 | case PTP_OPC_AlbumName: |
| 5624 | if (metadata->album == NULL) |
| 5625 | break; |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 5626 | prop = ptp_get_new_object_prop_entry(&props, &nrofprops); |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 5627 | prop->ObjectHandle = metadata->item_id; |
| 5628 | prop->property = PTP_OPC_AlbumName; |
| 5629 | prop->datatype = PTP_DTC_STR; |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 5630 | prop->propval.str = strdup(metadata->album); |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 5631 | break; |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 5632 | case PTP_OPC_Artist: |
| 5633 | if (metadata->artist == NULL) |
| 5634 | break; |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 5635 | prop = ptp_get_new_object_prop_entry(&props, &nrofprops); |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 5636 | prop->ObjectHandle = metadata->item_id; |
| 5637 | prop->property = PTP_OPC_Artist; |
| 5638 | prop->datatype = PTP_DTC_STR; |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 5639 | prop->propval.str = strdup(metadata->artist); |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 5640 | break; |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 5641 | case PTP_OPC_Composer: |
| 5642 | if (metadata->composer == NULL) |
| 5643 | break; |
| 5644 | prop = ptp_get_new_object_prop_entry(&props, &nrofprops); |
| 5645 | prop->ObjectHandle = metadata->item_id; |
| 5646 | prop->property = PTP_OPC_Composer; |
| 5647 | prop->datatype = PTP_DTC_STR; |
| 5648 | prop->propval.str = strdup(metadata->composer); |
| 5649 | break; |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 5650 | case PTP_OPC_Genre: |
| 5651 | if (metadata->genre == NULL) |
| 5652 | break; |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 5653 | prop = ptp_get_new_object_prop_entry(&props, &nrofprops); |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 5654 | prop->ObjectHandle = metadata->item_id; |
| 5655 | prop->property = PTP_OPC_Genre; |
| 5656 | prop->datatype = PTP_DTC_STR; |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 5657 | prop->propval.str = strdup(metadata->genre); |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 5658 | break; |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 5659 | case PTP_OPC_Duration: |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 5660 | prop = ptp_get_new_object_prop_entry(&props, &nrofprops); |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 5661 | prop->ObjectHandle = metadata->item_id; |
| 5662 | prop->property = PTP_OPC_Duration; |
| 5663 | prop->datatype = PTP_DTC_UINT32; |
Linus Walleij | 2955956 | 2007-08-22 21:38:04 +0000 | [diff] [blame] | 5664 | prop->propval.u32 = adjust_u32(metadata->duration, &opd); |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 5665 | break; |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 5666 | case PTP_OPC_Track: |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 5667 | prop = ptp_get_new_object_prop_entry(&props, &nrofprops); |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 5668 | prop->ObjectHandle = metadata->item_id; |
| 5669 | prop->property = PTP_OPC_Track; |
| 5670 | prop->datatype = PTP_DTC_UINT16; |
Linus Walleij | 2955956 | 2007-08-22 21:38:04 +0000 | [diff] [blame] | 5671 | prop->propval.u16 = adjust_u16(metadata->tracknumber, &opd); |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 5672 | break; |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 5673 | case PTP_OPC_OriginalReleaseDate: |
| 5674 | if (metadata->date == NULL) |
| 5675 | break; |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 5676 | prop = ptp_get_new_object_prop_entry(&props, &nrofprops); |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 5677 | prop->ObjectHandle = metadata->item_id; |
| 5678 | prop->property = PTP_OPC_OriginalReleaseDate; |
| 5679 | prop->datatype = PTP_DTC_STR; |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 5680 | prop->propval.str = strdup(metadata->date); |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 5681 | break; |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 5682 | case PTP_OPC_SampleRate: |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 5683 | prop = ptp_get_new_object_prop_entry(&props, &nrofprops); |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 5684 | prop->ObjectHandle = metadata->item_id; |
| 5685 | prop->property = PTP_OPC_SampleRate; |
| 5686 | prop->datatype = PTP_DTC_UINT32; |
Linus Walleij | 2955956 | 2007-08-22 21:38:04 +0000 | [diff] [blame] | 5687 | prop->propval.u32 = adjust_u32(metadata->samplerate, &opd); |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 5688 | break; |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 5689 | case PTP_OPC_NumberOfChannels: |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 5690 | prop = ptp_get_new_object_prop_entry(&props, &nrofprops); |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 5691 | prop->ObjectHandle = metadata->item_id; |
| 5692 | prop->property = PTP_OPC_NumberOfChannels; |
| 5693 | prop->datatype = PTP_DTC_UINT16; |
Linus Walleij | 2955956 | 2007-08-22 21:38:04 +0000 | [diff] [blame] | 5694 | prop->propval.u16 = adjust_u16(metadata->nochannels, &opd); |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 5695 | break; |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 5696 | case PTP_OPC_AudioWAVECodec: |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 5697 | prop = ptp_get_new_object_prop_entry(&props, &nrofprops); |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 5698 | prop->ObjectHandle = metadata->item_id; |
| 5699 | prop->property = PTP_OPC_AudioWAVECodec; |
| 5700 | prop->datatype = PTP_DTC_UINT32; |
Linus Walleij | 2955956 | 2007-08-22 21:38:04 +0000 | [diff] [blame] | 5701 | prop->propval.u32 = adjust_u32(metadata->wavecodec, &opd); |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 5702 | break; |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 5703 | case PTP_OPC_AudioBitRate: |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 5704 | prop = ptp_get_new_object_prop_entry(&props, &nrofprops); |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 5705 | prop->ObjectHandle = metadata->item_id; |
| 5706 | prop->property = PTP_OPC_AudioBitRate; |
| 5707 | prop->datatype = PTP_DTC_UINT32; |
Linus Walleij | 2955956 | 2007-08-22 21:38:04 +0000 | [diff] [blame] | 5708 | prop->propval.u32 = adjust_u32(metadata->bitrate, &opd); |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 5709 | break; |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 5710 | case PTP_OPC_BitRateType: |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 5711 | prop = ptp_get_new_object_prop_entry(&props, &nrofprops); |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 5712 | prop->ObjectHandle = metadata->item_id; |
| 5713 | prop->property = PTP_OPC_BitRateType; |
| 5714 | prop->datatype = PTP_DTC_UINT16; |
Linus Walleij | 2955956 | 2007-08-22 21:38:04 +0000 | [diff] [blame] | 5715 | prop->propval.u16 = adjust_u16(metadata->bitratetype, &opd); |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 5716 | break; |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 5717 | case PTP_OPC_Rating: |
| 5718 | // TODO: shall this be set for rating 0? |
| 5719 | if (metadata->rating == 0) |
| 5720 | break; |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 5721 | prop = ptp_get_new_object_prop_entry(&props, &nrofprops); |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 5722 | prop->ObjectHandle = metadata->item_id; |
| 5723 | prop->property = PTP_OPC_Rating; |
| 5724 | prop->datatype = PTP_DTC_UINT16; |
Linus Walleij | 2955956 | 2007-08-22 21:38:04 +0000 | [diff] [blame] | 5725 | prop->propval.u16 = adjust_u16(metadata->rating, &opd); |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 5726 | break; |
| 5727 | case PTP_OPC_UseCount: |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 5728 | prop = ptp_get_new_object_prop_entry(&props, &nrofprops); |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 5729 | prop->ObjectHandle = metadata->item_id; |
| 5730 | prop->property = PTP_OPC_UseCount; |
| 5731 | prop->datatype = PTP_DTC_UINT32; |
Linus Walleij | 2955956 | 2007-08-22 21:38:04 +0000 | [diff] [blame] | 5732 | prop->propval.u32 = adjust_u32(metadata->usecount, &opd); |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 5733 | break; |
| 5734 | case PTP_OPC_DateModified: |
Linus Walleij | cf8dc2b | 2008-10-21 13:58:36 +0000 | [diff] [blame] | 5735 | if (!FLAG_CANNOT_HANDLE_DATEMODIFIED(ptp_usb)) { |
Linus Walleij | 3758814 | 2008-10-16 19:10:47 +0000 | [diff] [blame] | 5736 | // Tag with current time if that is supported |
| 5737 | prop = ptp_get_new_object_prop_entry(&props, &nrofprops); |
| 5738 | prop->ObjectHandle = metadata->item_id; |
| 5739 | prop->property = PTP_OPC_DateModified; |
| 5740 | prop->datatype = PTP_DTC_STR; |
| 5741 | prop->propval.str = get_iso8601_stamp(); |
| 5742 | } |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 5743 | break; |
Linus Walleij | f9267e9 | 2007-10-15 21:07:37 +0000 | [diff] [blame] | 5744 | default: |
| 5745 | break; |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 5746 | } |
Linus Walleij | 304433d | 2007-02-26 08:02:47 +0000 | [diff] [blame] | 5747 | } |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 5748 | ptp_free_objectpropdesc(&opd); |
Richard Low | 15731fe | 2007-03-22 20:27:20 +0000 | [diff] [blame] | 5749 | } |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 5750 | |
Richard Low | 15731fe | 2007-03-22 20:27:20 +0000 | [diff] [blame] | 5751 | // NOTE: File size is not updated, this should not change anyway. |
| 5752 | // neither will we change the filename. |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 5753 | |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 5754 | ret = ptp_mtp_setobjectproplist(params, props, nrofprops); |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 5755 | |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 5756 | ptp_destroy_object_prop_list(props, nrofprops); |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 5757 | |
Richard Low | 15731fe | 2007-03-22 20:27:20 +0000 | [diff] [blame] | 5758 | if (ret != PTP_RC_OK) { |
| 5759 | // TODO: return error of which property we couldn't set |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 5760 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): " |
| 5761 | "could not set object property list."); |
Richard Low | 016e373 | 2007-09-23 14:53:46 +0000 | [diff] [blame] | 5762 | free(properties); |
Linus Walleij | 304433d | 2007-02-26 08:02:47 +0000 | [diff] [blame] | 5763 | return -1; |
| 5764 | } |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 5765 | |
Richard Low | 15731fe | 2007-03-22 20:27:20 +0000 | [diff] [blame] | 5766 | } else if (ptp_operation_issupported(params,PTP_OC_MTP_SetObjectPropValue)) { |
Linus Walleij | 00cf064 | 2006-07-26 20:40:59 +0000 | [diff] [blame] | 5767 | for (i=0;i<propcnt;i++) { |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 5768 | PTPObjectPropDesc opd; |
| 5769 | |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 5770 | 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] | 5771 | if (ret != PTP_RC_OK) { |
| 5772 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): " |
| 5773 | "could not get property description."); |
| 5774 | } else if (opd.GetSet) { |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 5775 | switch (properties[i]) { |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 5776 | case PTP_OPC_Name: |
| 5777 | // Update title |
| 5778 | 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] | 5779 | if (ret != 0) { |
| 5780 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): " |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 5781 | "could not set track title."); |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 5782 | } |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 5783 | break; |
| 5784 | case PTP_OPC_AlbumName: |
| 5785 | // Update album |
| 5786 | 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] | 5787 | if (ret != 0) { |
| 5788 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): " |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 5789 | "could not set track album name."); |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 5790 | } |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 5791 | break; |
| 5792 | case PTP_OPC_Artist: |
| 5793 | // Update artist |
| 5794 | 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] | 5795 | if (ret != 0) { |
| 5796 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): " |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 5797 | "could not set track artist name."); |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 5798 | } |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 5799 | break; |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 5800 | case PTP_OPC_Composer: |
| 5801 | // Update composer |
| 5802 | ret = set_object_string(device, metadata->item_id, PTP_OPC_Composer, metadata->composer); |
| 5803 | if (ret != 0) { |
| 5804 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): " |
| 5805 | "could not set track composer name."); |
| 5806 | } |
| 5807 | break; |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 5808 | case PTP_OPC_Genre: |
| 5809 | // Update genre |
| 5810 | ret = set_object_string(device, metadata->item_id, PTP_OPC_Genre, metadata->genre); |
| 5811 | if (ret != 0) { |
| 5812 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): " |
| 5813 | "could not set track genre name."); |
| 5814 | } |
| 5815 | break; |
| 5816 | case PTP_OPC_Duration: |
| 5817 | // Update duration |
| 5818 | if (metadata->duration != 0) { |
Linus Walleij | 2955956 | 2007-08-22 21:38:04 +0000 | [diff] [blame] | 5819 | 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] | 5820 | if (ret != 0) { |
| 5821 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): " |
| 5822 | "could not set track duration."); |
| 5823 | } |
| 5824 | } |
| 5825 | break; |
| 5826 | case PTP_OPC_Track: |
| 5827 | // Update track number. |
| 5828 | if (metadata->tracknumber != 0) { |
Linus Walleij | 2955956 | 2007-08-22 21:38:04 +0000 | [diff] [blame] | 5829 | 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] | 5830 | if (ret != 0) { |
| 5831 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): " |
| 5832 | "could not set track tracknumber."); |
| 5833 | } |
| 5834 | } |
| 5835 | break; |
| 5836 | case PTP_OPC_OriginalReleaseDate: |
| 5837 | // Update creation datetime |
| 5838 | ret = set_object_string(device, metadata->item_id, PTP_OPC_OriginalReleaseDate, metadata->date); |
| 5839 | if (ret != 0) { |
| 5840 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): " |
| 5841 | "could not set track release date."); |
| 5842 | } |
| 5843 | break; |
| 5844 | // These are, well not so important. |
| 5845 | case PTP_OPC_SampleRate: |
| 5846 | // Update sample rate |
| 5847 | if (metadata->samplerate != 0) { |
Linus Walleij | 2955956 | 2007-08-22 21:38:04 +0000 | [diff] [blame] | 5848 | 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] | 5849 | if (ret != 0) { |
| 5850 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): " |
| 5851 | "could not set samplerate."); |
| 5852 | } |
| 5853 | } |
| 5854 | break; |
| 5855 | case PTP_OPC_NumberOfChannels: |
| 5856 | // Update number of channels |
| 5857 | if (metadata->nochannels != 0) { |
Linus Walleij | 2955956 | 2007-08-22 21:38:04 +0000 | [diff] [blame] | 5858 | 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] | 5859 | if (ret != 0) { |
| 5860 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): " |
| 5861 | "could not set number of channels."); |
| 5862 | } |
| 5863 | } |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 5864 | break; |
| 5865 | case PTP_OPC_AudioWAVECodec: |
| 5866 | // Update WAVE codec |
| 5867 | if (metadata->wavecodec != 0) { |
Linus Walleij | 2955956 | 2007-08-22 21:38:04 +0000 | [diff] [blame] | 5868 | 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] | 5869 | if (ret != 0) { |
| 5870 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): " |
| 5871 | "could not set WAVE codec."); |
| 5872 | } |
| 5873 | } |
| 5874 | break; |
| 5875 | case PTP_OPC_AudioBitRate: |
| 5876 | // Update bitrate |
| 5877 | if (metadata->bitrate != 0) { |
Linus Walleij | 2955956 | 2007-08-22 21:38:04 +0000 | [diff] [blame] | 5878 | 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] | 5879 | if (ret != 0) { |
| 5880 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): " |
| 5881 | "could not set bitrate."); |
| 5882 | } |
| 5883 | } |
| 5884 | break; |
| 5885 | case PTP_OPC_BitRateType: |
| 5886 | // Update bitrate type |
| 5887 | if (metadata->bitratetype != 0) { |
Linus Walleij | 2955956 | 2007-08-22 21:38:04 +0000 | [diff] [blame] | 5888 | 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] | 5889 | if (ret != 0) { |
| 5890 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): " |
| 5891 | "could not set bitratetype."); |
| 5892 | } |
| 5893 | } |
| 5894 | break; |
| 5895 | case PTP_OPC_Rating: |
| 5896 | // Update user rating |
| 5897 | // TODO: shall this be set for rating 0? |
| 5898 | if (metadata->rating != 0) { |
Linus Walleij | 2955956 | 2007-08-22 21:38:04 +0000 | [diff] [blame] | 5899 | 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] | 5900 | if (ret != 0) { |
| 5901 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): " |
| 5902 | "could not set user rating."); |
| 5903 | } |
| 5904 | } |
| 5905 | break; |
| 5906 | case PTP_OPC_UseCount: |
| 5907 | // Update use count, set even to zero if desired. |
Linus Walleij | 2955956 | 2007-08-22 21:38:04 +0000 | [diff] [blame] | 5908 | 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] | 5909 | if (ret != 0) { |
| 5910 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): " |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 5911 | "could not set use count."); |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 5912 | } |
| 5913 | break; |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 5914 | case PTP_OPC_DateModified: |
Linus Walleij | cf8dc2b | 2008-10-21 13:58:36 +0000 | [diff] [blame] | 5915 | if (!FLAG_CANNOT_HANDLE_DATEMODIFIED(ptp_usb)) { |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 5916 | // Update modification time if supported |
| 5917 | char *tmpstamp = get_iso8601_stamp(); |
| 5918 | ret = set_object_string(device, metadata->item_id, PTP_OPC_DateModified, tmpstamp); |
| 5919 | if (ret != 0) { |
| 5920 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): " |
| 5921 | "could not set modification date."); |
| 5922 | } |
| 5923 | free(tmpstamp); |
| 5924 | } |
| 5925 | break; |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 5926 | |
| 5927 | // NOTE: File size is not updated, this should not change anyway. |
| 5928 | // neither will we change the filename. |
| 5929 | default: |
| 5930 | break; |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 5931 | } |
Linus Walleij | 00cf064 | 2006-07-26 20:40:59 +0000 | [diff] [blame] | 5932 | } |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 5933 | ptp_free_objectpropdesc(&opd); |
Linus Walleij | a498273 | 2006-02-24 15:46:02 +0000 | [diff] [blame] | 5934 | } |
Richard Low | 15731fe | 2007-03-22 20:27:20 +0000 | [diff] [blame] | 5935 | } else { |
| 5936 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): " |
| 5937 | "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] | 5938 | free(properties); |
Richard Low | 15731fe | 2007-03-22 20:27:20 +0000 | [diff] [blame] | 5939 | return -1; |
Linus Walleij | 17e39f7 | 2006-02-23 15:54:28 +0000 | [diff] [blame] | 5940 | } |
Linus Walleij | e7df653 | 2007-03-23 08:20:06 +0000 | [diff] [blame] | 5941 | |
tsaarni | a3eb60a | 2007-07-06 17:41:30 +0000 | [diff] [blame] | 5942 | // update cached object properties if metadata cache exists |
Linus Walleij | 7752b95 | 2007-10-19 20:11:46 +0000 | [diff] [blame] | 5943 | update_metadata_cache(device, metadata->item_id); |
Linus Walleij | 338ade4 | 2007-07-03 20:44:08 +0000 | [diff] [blame] | 5944 | |
Richard Low | 016e373 | 2007-09-23 14:53:46 +0000 | [diff] [blame] | 5945 | free(properties); |
| 5946 | |
Linus Walleij | f1b02f2 | 2006-12-06 09:03:23 +0000 | [diff] [blame] | 5947 | return 0; |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 5948 | } |
Linus Walleij | 95698cd | 2006-02-24 10:40:40 +0000 | [diff] [blame] | 5949 | |
| 5950 | /** |
Linus Walleij | 399807b | 2008-01-27 22:05:12 +0000 | [diff] [blame] | 5951 | * This function deletes a single file, track, playlist, folder or |
| 5952 | * any other object off the MTP device, identified by the object ID. |
| 5953 | * |
| 5954 | * If you delete a folder, there is no guarantee that the device will |
| 5955 | * really delete all the files that were in that folder, rather it is |
| 5956 | * expected that they will not be deleted, and will turn up in object |
| 5957 | * listings with parent set to a non-existant object ID. The safe way |
| 5958 | * to do this is to recursively delete all files (and folders) contained |
| 5959 | * in the folder, then the folder itself. |
| 5960 | * |
| 5961 | * @param device a pointer to the device to delete the object from. |
| 5962 | * @param object_id the object to delete. |
Linus Walleij | 95698cd | 2006-02-24 10:40:40 +0000 | [diff] [blame] | 5963 | * @return 0 on success, any other value means failure. |
| 5964 | */ |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 5965 | int LIBMTP_Delete_Object(LIBMTP_mtpdevice_t *device, |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 5966 | uint32_t object_id) |
Linus Walleij | 95698cd | 2006-02-24 10:40:40 +0000 | [diff] [blame] | 5967 | { |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 5968 | uint16_t ret; |
| 5969 | PTPParams *params = (PTPParams *) device->params; |
| 5970 | |
| 5971 | ret = ptp_deleteobject(params, object_id, 0); |
| 5972 | if (ret != PTP_RC_OK) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 5973 | 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] | 5974 | return -1; |
| 5975 | } |
tsaarni | a3eb60a | 2007-07-06 17:41:30 +0000 | [diff] [blame] | 5976 | |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 5977 | return 0; |
Linus Walleij | 95698cd | 2006-02-24 10:40:40 +0000 | [diff] [blame] | 5978 | } |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 5979 | |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 5980 | /** |
Linus Walleij | 8d8c435 | 2008-09-23 23:04:16 +0000 | [diff] [blame] | 5981 | * Internal function to update an object filename property. |
Linus Walleij | de8193f | 2008-01-27 14:55:31 +0000 | [diff] [blame] | 5982 | */ |
Linus Walleij | 8d8c435 | 2008-09-23 23:04:16 +0000 | [diff] [blame] | 5983 | static int set_object_filename(LIBMTP_mtpdevice_t *device, |
| 5984 | uint32_t object_id, uint16_t ptp_type, |
| 5985 | const char **newname_ptr) |
Linus Walleij | de8193f | 2008-01-27 14:55:31 +0000 | [diff] [blame] | 5986 | { |
| 5987 | PTPParams *params = (PTPParams *) device->params; |
| 5988 | PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo; |
| 5989 | PTPObjectPropDesc opd; |
| 5990 | uint16_t ret; |
Linus Walleij | 8d8c435 | 2008-09-23 23:04:16 +0000 | [diff] [blame] | 5991 | char *newname; |
Linus Walleij | 8be2c03 | 2008-01-28 21:23:47 +0000 | [diff] [blame] | 5992 | |
| 5993 | // See if we can modify the filename on this kind of files. |
| 5994 | ret = ptp_mtp_getobjectpropdesc(params, PTP_OPC_ObjectFileName, ptp_type, &opd); |
Linus Walleij | de8193f | 2008-01-27 14:55:31 +0000 | [diff] [blame] | 5995 | if (ret != PTP_RC_OK) { |
Linus Walleij | 8d8c435 | 2008-09-23 23:04:16 +0000 | [diff] [blame] | 5996 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "set_object_filename(): " |
Linus Walleij | 8be2c03 | 2008-01-28 21:23:47 +0000 | [diff] [blame] | 5997 | "could not get property description."); |
Linus Walleij | de8193f | 2008-01-27 14:55:31 +0000 | [diff] [blame] | 5998 | return -1; |
| 5999 | } |
| 6000 | |
| 6001 | if (!opd.GetSet) { |
| 6002 | ptp_free_objectpropdesc(&opd); |
Linus Walleij | 8d8c435 | 2008-09-23 23:04:16 +0000 | [diff] [blame] | 6003 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "set_object_filename(): " |
| 6004 | " property is not settable."); |
Linus Walleij | 399807b | 2008-01-27 22:05:12 +0000 | [diff] [blame] | 6005 | // TODO: we COULD actually upload/download the object here, if we feel |
| 6006 | // like wasting time for the user. |
Linus Walleij | de8193f | 2008-01-27 14:55:31 +0000 | [diff] [blame] | 6007 | return -1; |
| 6008 | } |
| 6009 | |
Linus Walleij | 8d8c435 | 2008-09-23 23:04:16 +0000 | [diff] [blame] | 6010 | newname = strdup(*newname_ptr); |
| 6011 | |
Linus Walleij | fec4d56 | 2008-06-01 22:30:36 +0000 | [diff] [blame] | 6012 | if (FLAG_ONLY_7BIT_FILENAMES(ptp_usb)) { |
Linus Walleij | de8193f | 2008-01-27 14:55:31 +0000 | [diff] [blame] | 6013 | strip_7bit_from_utf8(newname); |
| 6014 | } |
| 6015 | |
| 6016 | if (ptp_operation_issupported(params, PTP_OC_MTP_SetObjPropList) && |
Linus Walleij | fec4d56 | 2008-06-01 22:30:36 +0000 | [diff] [blame] | 6017 | !FLAG_BROKEN_SET_OBJECT_PROPLIST(ptp_usb)) { |
Linus Walleij | de8193f | 2008-01-27 14:55:31 +0000 | [diff] [blame] | 6018 | MTPProperties *props = NULL; |
| 6019 | MTPProperties *prop = NULL; |
| 6020 | int nrofprops = 0; |
Linus Walleij | 8d8c435 | 2008-09-23 23:04:16 +0000 | [diff] [blame] | 6021 | |
Linus Walleij | de8193f | 2008-01-27 14:55:31 +0000 | [diff] [blame] | 6022 | prop = ptp_get_new_object_prop_entry(&props, &nrofprops); |
Linus Walleij | 8d8c435 | 2008-09-23 23:04:16 +0000 | [diff] [blame] | 6023 | prop->ObjectHandle = object_id; |
Linus Walleij | de8193f | 2008-01-27 14:55:31 +0000 | [diff] [blame] | 6024 | prop->property = PTP_OPC_ObjectFileName; |
| 6025 | prop->datatype = PTP_DTC_STR; |
Linus Walleij | 8d8c435 | 2008-09-23 23:04:16 +0000 | [diff] [blame] | 6026 | prop->propval.str = newname; |
| 6027 | |
Linus Walleij | de8193f | 2008-01-27 14:55:31 +0000 | [diff] [blame] | 6028 | ret = ptp_mtp_setobjectproplist(params, props, nrofprops); |
Linus Walleij | 8d8c435 | 2008-09-23 23:04:16 +0000 | [diff] [blame] | 6029 | |
Linus Walleij | de8193f | 2008-01-27 14:55:31 +0000 | [diff] [blame] | 6030 | ptp_destroy_object_prop_list(props, nrofprops); |
Linus Walleij | 8d8c435 | 2008-09-23 23:04:16 +0000 | [diff] [blame] | 6031 | |
Linus Walleij | de8193f | 2008-01-27 14:55:31 +0000 | [diff] [blame] | 6032 | if (ret != PTP_RC_OK) { |
Linus Walleij | 8d8c435 | 2008-09-23 23:04:16 +0000 | [diff] [blame] | 6033 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "set_object_filename(): " |
| 6034 | " could not set object property list."); |
| 6035 | ptp_free_objectpropdesc(&opd); |
| 6036 | return -1; |
Linus Walleij | de8193f | 2008-01-27 14:55:31 +0000 | [diff] [blame] | 6037 | } |
| 6038 | } else if (ptp_operation_issupported(params, PTP_OC_MTP_SetObjectPropValue)) { |
| 6039 | ret = set_object_string(device, object_id, PTP_OPC_ObjectFileName, newname); |
| 6040 | if (ret != 0) { |
Linus Walleij | 8d8c435 | 2008-09-23 23:04:16 +0000 | [diff] [blame] | 6041 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "set_object_filename(): " |
| 6042 | " could not set object filename."); |
Linus Walleij | de8193f | 2008-01-27 14:55:31 +0000 | [diff] [blame] | 6043 | ptp_free_objectpropdesc(&opd); |
| 6044 | return -1; |
| 6045 | } |
| 6046 | } else { |
Linus Walleij | 8d8c435 | 2008-09-23 23:04:16 +0000 | [diff] [blame] | 6047 | free(newname); |
| 6048 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "set_object_filename(): " |
| 6049 | " your device doesn't seem to support any known way of setting metadata."); |
Linus Walleij | de8193f | 2008-01-27 14:55:31 +0000 | [diff] [blame] | 6050 | ptp_free_objectpropdesc(&opd); |
| 6051 | return -1; |
| 6052 | } |
Linus Walleij | 8d8c435 | 2008-09-23 23:04:16 +0000 | [diff] [blame] | 6053 | |
Linus Walleij | de8193f | 2008-01-27 14:55:31 +0000 | [diff] [blame] | 6054 | ptp_free_objectpropdesc(&opd); |
Linus Walleij | 8d8c435 | 2008-09-23 23:04:16 +0000 | [diff] [blame] | 6055 | |
Linus Walleij | de8193f | 2008-01-27 14:55:31 +0000 | [diff] [blame] | 6056 | // update cached object properties if metadata cache exists |
| 6057 | update_metadata_cache(device, object_id); |
Linus Walleij | 8d8c435 | 2008-09-23 23:04:16 +0000 | [diff] [blame] | 6058 | |
Linus Walleij | de8193f | 2008-01-27 14:55:31 +0000 | [diff] [blame] | 6059 | return 0; |
| 6060 | } |
Linus Walleij | 399807b | 2008-01-27 22:05:12 +0000 | [diff] [blame] | 6061 | |
Linus Walleij | de8193f | 2008-01-27 14:55:31 +0000 | [diff] [blame] | 6062 | /** |
Linus Walleij | 8d8c435 | 2008-09-23 23:04:16 +0000 | [diff] [blame] | 6063 | * This function renames a single file. |
| 6064 | * This simply means that the PTP_OPC_ObjectFileName property |
| 6065 | * is updated, if this is supported by the device. |
| 6066 | * |
| 6067 | * @param device a pointer to the device that contains the file. |
| 6068 | * @param file the file metadata of the file to rename. |
| 6069 | * On success, the filename member is updated. Be aware, that |
| 6070 | * this name can be different than newname depending of device restrictions. |
| 6071 | * @param newname the new filename for this object. |
| 6072 | * @return 0 on success, any other value means failure. |
| 6073 | */ |
| 6074 | int LIBMTP_Set_File_Name(LIBMTP_mtpdevice_t *device, |
| 6075 | LIBMTP_file_t *file, const char *newname) |
| 6076 | { |
| 6077 | int ret; |
| 6078 | |
| 6079 | ret = set_object_filename(device, file->item_id, |
| 6080 | map_libmtp_type_to_ptp_type(file->filetype), |
| 6081 | &newname); |
| 6082 | |
| 6083 | if (ret != 0) { |
| 6084 | return ret; |
| 6085 | } |
| 6086 | |
| 6087 | free(file->filename); |
| 6088 | file->filename = strdup(newname); |
| 6089 | return ret; |
| 6090 | } |
| 6091 | |
| 6092 | /** |
| 6093 | * This function renames a single folder. |
| 6094 | * This simply means that the PTP_OPC_ObjectFileName property |
| 6095 | * is updated, if this is supported by the device. |
| 6096 | * |
| 6097 | * @param device a pointer to the device that contains the file. |
| 6098 | * @param folder the folder metadata of the folder to rename. |
| 6099 | * On success, the name member is updated. Be aware, that |
| 6100 | * this name can be different than newname depending of device restrictions. |
| 6101 | * @param newname the new name for this object. |
| 6102 | * @return 0 on success, any other value means failure. |
| 6103 | */ |
| 6104 | int LIBMTP_Set_Folder_Name(LIBMTP_mtpdevice_t *device, |
| 6105 | LIBMTP_folder_t *folder, const char* newname) |
| 6106 | { |
| 6107 | int ret; |
| 6108 | |
| 6109 | ret = set_object_filename(device, folder->folder_id, |
Linus Walleij | fb7212f | 2008-09-24 20:30:13 +0000 | [diff] [blame] | 6110 | PTP_OFC_Association, |
Linus Walleij | 8d8c435 | 2008-09-23 23:04:16 +0000 | [diff] [blame] | 6111 | &newname); |
| 6112 | |
| 6113 | if (ret != 0) { |
| 6114 | return ret; |
| 6115 | } |
| 6116 | |
| 6117 | free(folder->name); |
| 6118 | folder->name = strdup(newname); |
| 6119 | return ret; |
| 6120 | } |
| 6121 | |
| 6122 | /** |
| 6123 | * This function renames a single track. |
| 6124 | * This simply means that the PTP_OPC_ObjectFileName property |
| 6125 | * is updated, if this is supported by the device. |
| 6126 | * |
| 6127 | * @param device a pointer to the device that contains the file. |
| 6128 | * @param track the track metadata of the track to rename. |
| 6129 | * On success, the filename member is updated. Be aware, that |
| 6130 | * this name can be different than newname depending of device restrictions. |
| 6131 | * @param newname the new filename for this object. |
| 6132 | * @return 0 on success, any other value means failure. |
| 6133 | */ |
| 6134 | int LIBMTP_Set_Track_Name(LIBMTP_mtpdevice_t *device, |
| 6135 | LIBMTP_track_t *track, const char* newname) |
| 6136 | { |
| 6137 | int ret; |
| 6138 | |
| 6139 | ret = set_object_filename(device, track->item_id, |
| 6140 | map_libmtp_type_to_ptp_type(track->filetype), |
| 6141 | &newname); |
| 6142 | |
| 6143 | if (ret != 0) { |
| 6144 | return ret; |
| 6145 | } |
| 6146 | |
| 6147 | free(track->filename); |
| 6148 | track->filename = strdup(newname); |
| 6149 | return ret; |
| 6150 | } |
| 6151 | |
| 6152 | /** |
Linus Walleij | 12bbf31 | 2009-01-26 21:46:51 +0000 | [diff] [blame] | 6153 | * This function renames a single playlist object file holder. |
| 6154 | * This simply means that the <code>PTP_OPC_ObjectFileName</code> |
| 6155 | * property is updated, if this is supported by the device. |
| 6156 | * The playlist filename should nominally end with an extension |
| 6157 | * like ".pla". |
| 6158 | * |
| 6159 | * NOTE: if you want to change the metadata the device display |
| 6160 | * about a playlist you must <i>not</i> use this function, |
| 6161 | * use <code>LIBMTP_Update_Playlist()</code> instead! |
Linus Walleij | 8d8c435 | 2008-09-23 23:04:16 +0000 | [diff] [blame] | 6162 | * |
| 6163 | * @param device a pointer to the device that contains the file. |
| 6164 | * @param playlist the playlist metadata of the playlist to rename. |
| 6165 | * On success, the name member is updated. Be aware, that |
| 6166 | * this name can be different than newname depending of device restrictions. |
| 6167 | * @param newname the new name for this object. |
| 6168 | * @return 0 on success, any other value means failure. |
Linus Walleij | 12bbf31 | 2009-01-26 21:46:51 +0000 | [diff] [blame] | 6169 | * @see LIBMTP_Update_Playlist() |
Linus Walleij | 8d8c435 | 2008-09-23 23:04:16 +0000 | [diff] [blame] | 6170 | */ |
| 6171 | int LIBMTP_Set_Playlist_Name(LIBMTP_mtpdevice_t *device, |
| 6172 | LIBMTP_playlist_t *playlist, const char* newname) |
| 6173 | { |
| 6174 | int ret; |
| 6175 | |
| 6176 | ret = set_object_filename(device, playlist->playlist_id, |
| 6177 | PTP_OFC_MTP_AbstractAudioVideoPlaylist, |
| 6178 | &newname); |
| 6179 | |
| 6180 | if (ret != 0) { |
| 6181 | return ret; |
| 6182 | } |
| 6183 | |
| 6184 | free(playlist->name); |
| 6185 | playlist->name = strdup(newname); |
| 6186 | return ret; |
| 6187 | } |
| 6188 | |
| 6189 | /** |
| 6190 | * This function renames a single album. |
Linus Walleij | 12bbf31 | 2009-01-26 21:46:51 +0000 | [diff] [blame] | 6191 | * This simply means that the <code>PTP_OPC_ObjectFileName</code> |
| 6192 | * property is updated, if this is supported by the device. |
| 6193 | * The album filename should nominally end with an extension |
| 6194 | * like ".alb". |
| 6195 | * |
| 6196 | * NOTE: if you want to change the metadata the device display |
| 6197 | * about a playlist you must <i>not</i> use this function, |
| 6198 | * use <code>LIBMTP_Update_Album()</code> instead! |
Linus Walleij | 8d8c435 | 2008-09-23 23:04:16 +0000 | [diff] [blame] | 6199 | * |
| 6200 | * @param device a pointer to the device that contains the file. |
| 6201 | * @param album the album metadata of the album to rename. |
| 6202 | * On success, the name member is updated. Be aware, that |
| 6203 | * this name can be different than newname depending of device restrictions. |
| 6204 | * @param newname the new name for this object. |
| 6205 | * @return 0 on success, any other value means failure. |
Linus Walleij | 12bbf31 | 2009-01-26 21:46:51 +0000 | [diff] [blame] | 6206 | * @see LIBMTP_Update_Album() |
Linus Walleij | 8d8c435 | 2008-09-23 23:04:16 +0000 | [diff] [blame] | 6207 | */ |
| 6208 | int LIBMTP_Set_Album_Name(LIBMTP_mtpdevice_t *device, |
| 6209 | LIBMTP_album_t *album, const char* newname) |
| 6210 | { |
| 6211 | int ret; |
| 6212 | |
| 6213 | ret = set_object_filename(device, album->album_id, |
| 6214 | PTP_OFC_MTP_AbstractAudioAlbum, |
| 6215 | &newname); |
| 6216 | |
| 6217 | if (ret != 0) { |
| 6218 | return ret; |
| 6219 | } |
| 6220 | |
| 6221 | free(album->name); |
| 6222 | album->name = strdup(newname); |
| 6223 | return ret; |
| 6224 | } |
| 6225 | |
| 6226 | /** |
| 6227 | * THIS FUNCTION IS DEPRECATED. PLEASE UPDATE YOUR CODE IN ORDER |
| 6228 | * NOT TO USE IT. |
| 6229 | * |
| 6230 | * @see LIBMTP_Set_File_Name() |
| 6231 | * @see LIBMTP_Set_Track_Name() |
| 6232 | * @see LIBMTP_Set_Folder_Name() |
| 6233 | * @see LIBMTP_Set_Playlist_Name() |
| 6234 | * @see LIBMTP_Set_Album_Name() |
| 6235 | */ |
| 6236 | int LIBMTP_Set_Object_Filename(LIBMTP_mtpdevice_t *device, |
| 6237 | uint32_t object_id, char* newname) |
| 6238 | { |
| 6239 | int ret; |
| 6240 | LIBMTP_file_t *file; |
| 6241 | |
| 6242 | file = LIBMTP_Get_Filemetadata(device, object_id); |
Linus Walleij | fb7212f | 2008-09-24 20:30:13 +0000 | [diff] [blame] | 6243 | |
| 6244 | if (file == NULL) { |
| 6245 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Set_Object_Filename(): " |
| 6246 | "could not get file metadata for target object."); |
| 6247 | return -1; |
| 6248 | } |
Linus Walleij | 8d8c435 | 2008-09-23 23:04:16 +0000 | [diff] [blame] | 6249 | |
Linus Walleij | fb7212f | 2008-09-24 20:30:13 +0000 | [diff] [blame] | 6250 | ret = set_object_filename(device, object_id, map_libmtp_type_to_ptp_type(file->filetype), (const char **) &newname); |
Linus Walleij | 8d8c435 | 2008-09-23 23:04:16 +0000 | [diff] [blame] | 6251 | |
| 6252 | free(file); |
| 6253 | |
| 6254 | return ret; |
| 6255 | } |
| 6256 | |
| 6257 | /** |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 6258 | * Helper function. This indicates if a track exists on the device |
| 6259 | * @param device a pointer to the device to get the track from. |
| 6260 | * @param id the track ID of the track to retrieve. |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 6261 | * @return TRUE (!=0) if the track exists, FALSE (0) if not |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 6262 | */ |
| 6263 | int LIBMTP_Track_Exists(LIBMTP_mtpdevice_t *device, |
| 6264 | uint32_t const id) |
| 6265 | { |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 6266 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 6267 | uint16_t ret; |
| 6268 | PTPObject *ob; |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 6269 | |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 6270 | ret = ptp_object_want (params, id, 0, &ob); |
| 6271 | if (ret == PTP_RC_OK) |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 6272 | return -1; |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 6273 | return 0; |
| 6274 | } |
| 6275 | |
| 6276 | /** |
| 6277 | * This creates a new folder structure and allocates memory |
| 6278 | * for it. Notice that if you add strings to this structure they |
| 6279 | * will be freed by the corresponding <code>LIBMTP_folder_track_t</code> |
| 6280 | * operation later, so be careful of using strdup() when assigning |
| 6281 | * strings, e.g.: |
| 6282 | * |
| 6283 | * @return a pointer to the newly allocated folder structure. |
| 6284 | * @see LIBMTP_destroy_folder_t() |
| 6285 | */ |
| 6286 | LIBMTP_folder_t *LIBMTP_new_folder_t(void) |
| 6287 | { |
| 6288 | LIBMTP_folder_t *new = (LIBMTP_folder_t *) malloc(sizeof(LIBMTP_folder_t)); |
| 6289 | if (new == NULL) { |
| 6290 | return NULL; |
| 6291 | } |
| 6292 | new->folder_id = 0; |
| 6293 | new->parent_id = 0; |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 6294 | new->storage_id = 0; |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 6295 | new->name = NULL; |
| 6296 | new->sibling = NULL; |
| 6297 | new->child = NULL; |
| 6298 | return new; |
| 6299 | } |
| 6300 | |
| 6301 | /** |
Linus Walleij | 5573def | 2007-04-23 07:04:18 +0000 | [diff] [blame] | 6302 | * This recursively deletes the memory for a folder structure. |
| 6303 | * This shall typically be called on a top-level folder list to |
| 6304 | * detsroy the entire folder tree. |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 6305 | * |
| 6306 | * @param folder folder structure to destroy |
| 6307 | * @see LIBMTP_new_folder_t() |
| 6308 | */ |
| 6309 | void LIBMTP_destroy_folder_t(LIBMTP_folder_t *folder) |
| 6310 | { |
| 6311 | |
| 6312 | if(folder == NULL) { |
| 6313 | return; |
| 6314 | } |
| 6315 | |
| 6316 | //Destroy from the bottom up |
| 6317 | if(folder->child != NULL) { |
| 6318 | LIBMTP_destroy_folder_t(folder->child); |
| 6319 | } |
| 6320 | |
| 6321 | if(folder->sibling != NULL) { |
| 6322 | LIBMTP_destroy_folder_t(folder->sibling); |
| 6323 | } |
| 6324 | |
| 6325 | if(folder->name != NULL) { |
| 6326 | free(folder->name); |
| 6327 | } |
| 6328 | |
| 6329 | free(folder); |
| 6330 | } |
| 6331 | |
| 6332 | /** |
| 6333 | * Helper function. Returns a folder structure for a |
| 6334 | * specified id. |
| 6335 | * |
| 6336 | * @param folderlist list of folders to search |
| 6337 | * @id id of folder to look for |
| 6338 | * @return a folder or NULL if not found |
| 6339 | */ |
| 6340 | LIBMTP_folder_t *LIBMTP_Find_Folder(LIBMTP_folder_t *folderlist, uint32_t id) |
| 6341 | { |
| 6342 | LIBMTP_folder_t *ret = NULL; |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 6343 | |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 6344 | if(folderlist == NULL) { |
| 6345 | return NULL; |
| 6346 | } |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 6347 | |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 6348 | if(folderlist->folder_id == id) { |
| 6349 | return folderlist; |
| 6350 | } |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 6351 | |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 6352 | if(folderlist->sibling) { |
| 6353 | ret = LIBMTP_Find_Folder(folderlist->sibling, id); |
| 6354 | } |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 6355 | |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 6356 | if(folderlist->child && ret == NULL) { |
| 6357 | ret = LIBMTP_Find_Folder(folderlist->child, id); |
| 6358 | } |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 6359 | |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 6360 | return ret; |
| 6361 | } |
| 6362 | |
| 6363 | /** |
Linus Walleij | 4d0deea | 2007-10-19 21:27:48 +0000 | [diff] [blame] | 6364 | * Function used to recursively get subfolders from params. |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 6365 | */ |
Linus Walleij | a89a794 | 2008-12-14 23:19:34 +0000 | [diff] [blame] | 6366 | static LIBMTP_folder_t *get_subfolders_for_folder(LIBMTP_folder_t *list, uint32_t parent) |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 6367 | { |
Linus Walleij | a89a794 | 2008-12-14 23:19:34 +0000 | [diff] [blame] | 6368 | LIBMTP_folder_t *retfolders = NULL, *children, *iter, *curr; |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 6369 | |
Linus Walleij | a89a794 | 2008-12-14 23:19:34 +0000 | [diff] [blame] | 6370 | iter = list->sibling; |
| 6371 | while(iter != list) { |
| 6372 | if (iter->parent_id != parent) { |
| 6373 | iter = iter->sibling; |
| 6374 | continue; |
| 6375 | } |
| 6376 | |
| 6377 | /* We know that iter is a child of 'parent', therefore we can safely |
| 6378 | * hold on to 'iter' locally since no one else will steal it |
| 6379 | * from the 'list' as we recurse. */ |
| 6380 | children = get_subfolders_for_folder(list, iter->folder_id); |
| 6381 | |
| 6382 | curr = iter; |
| 6383 | iter = iter->sibling; |
| 6384 | |
| 6385 | // Remove curr from the list. |
| 6386 | curr->child->sibling = curr->sibling; |
| 6387 | curr->sibling->child = curr->child; |
| 6388 | |
| 6389 | // Attach the children to curr. |
| 6390 | curr->child = children; |
| 6391 | |
| 6392 | // Put this folder into the list of siblings. |
| 6393 | curr->sibling = retfolders; |
| 6394 | retfolders = curr; |
| 6395 | } |
| 6396 | |
| 6397 | return retfolders; |
| 6398 | } |
| 6399 | |
| 6400 | /** |
| 6401 | * This returns a list of all folders available |
| 6402 | * on the current MTP device. |
| 6403 | * |
| 6404 | * @param device a pointer to the device to get the folder listing for. |
| 6405 | * @return a list of folders |
| 6406 | */ |
| 6407 | LIBMTP_folder_t *LIBMTP_Get_Folder_List(LIBMTP_mtpdevice_t *device) |
| 6408 | { |
| 6409 | PTPParams *params = (PTPParams *) device->params; |
| 6410 | LIBMTP_folder_t head, *rv; |
| 6411 | int i; |
| 6412 | |
| 6413 | // Get all the handles if we haven't already done that |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 6414 | if (params->nrofobjects == 0) { |
Linus Walleij | a89a794 | 2008-12-14 23:19:34 +0000 | [diff] [blame] | 6415 | flush_handles(device); |
| 6416 | } |
| 6417 | |
Linus Walleij | 4ab47b2 | 2008-12-14 23:24:26 +0000 | [diff] [blame] | 6418 | /* |
| 6419 | * This creates a temporary list of the folders, this is in a |
| 6420 | * reverse order and uses the Folder pointers that are already |
| 6421 | * in the Folder structure. From this we can then build up the |
| 6422 | * folder hierarchy with only looking at this temporary list, |
| 6423 | * and removing the folders from this temporary list as we go. |
| 6424 | * This significantly reduces the number of operations that we |
| 6425 | * have to do in building the folder hierarchy. Also since the |
| 6426 | * temp list is in reverse order, when we prepend to the sibling |
| 6427 | * list things are in the same order as they were originally |
| 6428 | * in the handle list. |
| 6429 | */ |
Linus Walleij | a89a794 | 2008-12-14 23:19:34 +0000 | [diff] [blame] | 6430 | head.sibling = &head; |
| 6431 | head.child = &head; |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 6432 | for (i = 0; i < params->nrofobjects; i++) { |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 6433 | LIBMTP_folder_t *folder; |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 6434 | PTPObject *ob; |
Linus Walleij | a89a794 | 2008-12-14 23:19:34 +0000 | [diff] [blame] | 6435 | |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 6436 | ob = ¶ms->objects[i]; |
| 6437 | if (ob->oi.ObjectFormat != PTP_OFC_Association) { |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 6438 | continue; |
| 6439 | } |
Linus Walleij | 4ab47b2 | 2008-12-14 23:24:26 +0000 | [diff] [blame] | 6440 | /* |
| 6441 | * Do we know how to handle these? They are part |
| 6442 | * of the MTP 1.0 specification paragraph 3.6.4. |
| 6443 | * For AssociationDesc 0x00000001U ptp_mtp_getobjectreferences() |
| 6444 | * should be called on these to get the contained objects, but |
| 6445 | * we basically don't care. Hopefully parent_id is maintained for all |
| 6446 | * children, because we rely on that instead. |
| 6447 | */ |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 6448 | if (ob->oi.AssociationDesc != 0x00000000U) { |
| 6449 | printf("MTP extended association type 0x%08x encountered\n", ob->oi.AssociationDesc); |
Linus Walleij | d71d0b3 | 2008-09-22 08:21:03 +0000 | [diff] [blame] | 6450 | } |
| 6451 | |
Linus Walleij | 4d0deea | 2007-10-19 21:27:48 +0000 | [diff] [blame] | 6452 | // Create a folder struct... |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 6453 | folder = LIBMTP_new_folder_t(); |
Linus Walleij | 4d0deea | 2007-10-19 21:27:48 +0000 | [diff] [blame] | 6454 | if (folder == NULL) { |
| 6455 | // malloc failure or so. |
| 6456 | return NULL; |
| 6457 | } |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 6458 | folder->folder_id = ob->oid; |
| 6459 | folder->parent_id = ob->oi.ParentObject; |
| 6460 | folder->storage_id = ob->oi.StorageID; |
| 6461 | folder->name = (ob->oi.Filename) ? (char *)strdup(ob->oi.Filename) : NULL; |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 6462 | |
Linus Walleij | a89a794 | 2008-12-14 23:19:34 +0000 | [diff] [blame] | 6463 | // pretend sibling says next, and child says prev. |
| 6464 | folder->sibling = head.sibling; |
| 6465 | folder->child = &head; |
| 6466 | head.sibling->child = folder; |
| 6467 | head.sibling = folder; |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 6468 | } |
Linus Walleij | 4d0deea | 2007-10-19 21:27:48 +0000 | [diff] [blame] | 6469 | |
Linus Walleij | 4d0deea | 2007-10-19 21:27:48 +0000 | [diff] [blame] | 6470 | // We begin at the root folder and get them all recursively |
Linus Walleij | a89a794 | 2008-12-14 23:19:34 +0000 | [diff] [blame] | 6471 | rv = get_subfolders_for_folder(&head, 0x00000000); |
| 6472 | |
| 6473 | // The temp list should be empty. Clean up any orphans just in case. |
| 6474 | while(head.sibling != &head) { |
| 6475 | LIBMTP_folder_t *curr = head.sibling; |
| 6476 | |
| 6477 | printf("Orphan folder with ID: 0x%08x name: \"%s\" encountered.\n", |
| 6478 | curr->folder_id, |
| 6479 | curr->name); |
| 6480 | curr->sibling->child = curr->child; |
| 6481 | curr->child->sibling = curr->sibling; |
| 6482 | curr->child = NULL; |
| 6483 | curr->sibling = NULL; |
| 6484 | LIBMTP_destroy_folder_t(curr); |
| 6485 | } |
| 6486 | |
| 6487 | return rv; |
Linus Walleij | 4d0deea | 2007-10-19 21:27:48 +0000 | [diff] [blame] | 6488 | } |
| 6489 | |
| 6490 | /** |
Linus Walleij | c86afbd | 2006-05-04 19:05:24 +0000 | [diff] [blame] | 6491 | * This create a folder on the current MTP device. The PTP name |
| 6492 | * for a folder is "association". The PTP/MTP devices does not |
| 6493 | * have an internal "folder" concept really, it contains a flat |
| 6494 | * list of all files and some file are "associations" that other |
| 6495 | * files and folders may refer to as its "parent". |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 6496 | * |
Linus Walleij | c86afbd | 2006-05-04 19:05:24 +0000 | [diff] [blame] | 6497 | * @param device a pointer to the device to create the folder on. |
Richard Low | f8cd3d7 | 2009-05-04 16:38:33 +0000 | [diff] [blame] | 6498 | * @param name the name of the new folder. Note this can be modified |
| 6499 | * if the device does not support all the characters in the |
| 6500 | * name. |
Linus Walleij | c86afbd | 2006-05-04 19:05:24 +0000 | [diff] [blame] | 6501 | * @param parent_id id of parent folder to add the new folder to, |
| 6502 | * or 0 to put it in the root directory. |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 6503 | * @param storage_id id of the storage to add this new folder to. |
| 6504 | * notice that you cannot mismatch storage id and parent id: |
| 6505 | * they must both be on the same storage! Pass in 0 if you |
| 6506 | * want to create this folder on the default storage. |
Linus Walleij | c86afbd | 2006-05-04 19:05:24 +0000 | [diff] [blame] | 6507 | * @return id to new folder or 0 if an error occured |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 6508 | */ |
Richard Low | f8cd3d7 | 2009-05-04 16:38:33 +0000 | [diff] [blame] | 6509 | uint32_t LIBMTP_Create_Folder(LIBMTP_mtpdevice_t *device, char *name, |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 6510 | uint32_t parent_id, uint32_t storage_id) |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 6511 | { |
| 6512 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | d3b7857 | 2007-08-24 21:28:24 +0000 | [diff] [blame] | 6513 | PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo; |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 6514 | uint32_t parenthandle = 0; |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 6515 | uint32_t store; |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 6516 | PTPObjectInfo new_folder; |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 6517 | uint16_t ret; |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 6518 | uint32_t new_id = 0; |
| 6519 | |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 6520 | if (storage_id == 0) { |
Linus Walleij | d71d0b3 | 2008-09-22 08:21:03 +0000 | [diff] [blame] | 6521 | // I'm just guessing that a folder may require 512 bytes |
| 6522 | store = get_writeable_storageid(device, 512); |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 6523 | } else { |
| 6524 | store = storage_id; |
| 6525 | } |
| 6526 | parenthandle = parent_id; |
| 6527 | |
Richard Low | 5942604 | 2009-05-04 16:40:09 +0000 | [diff] [blame] | 6528 | memset(&new_folder, 0, sizeof(new_folder)); |
Richard Low | f8cd3d7 | 2009-05-04 16:38:33 +0000 | [diff] [blame] | 6529 | new_folder.Filename = name; |
Linus Walleij | fec4d56 | 2008-06-01 22:30:36 +0000 | [diff] [blame] | 6530 | if (FLAG_ONLY_7BIT_FILENAMES(ptp_usb)) { |
Linus Walleij | d3b7857 | 2007-08-24 21:28:24 +0000 | [diff] [blame] | 6531 | strip_7bit_from_utf8(new_folder.Filename); |
| 6532 | } |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 6533 | new_folder.ObjectCompressedSize = 1; |
| 6534 | new_folder.ObjectFormat = PTP_OFC_Association; |
Linus Walleij | dbbfecc | 2008-11-05 09:52:47 +0000 | [diff] [blame] | 6535 | new_folder.ProtectionStatus = PTP_PS_NoProtection; |
Linus Walleij | 753a46a | 2008-11-05 09:10:52 +0000 | [diff] [blame] | 6536 | new_folder.AssociationType = PTP_AT_GenericFolder; |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 6537 | new_folder.ParentObject = parent_id; |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 6538 | new_folder.StorageID = store; |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 6539 | |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 6540 | // Create the object |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 6541 | // FIXME: use send list here if available. |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 6542 | ret = ptp_sendobjectinfo(params, &store, &parenthandle, &new_id, &new_folder); |
| 6543 | if (ret != PTP_RC_OK) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 6544 | 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] | 6545 | if (ret == PTP_RC_AccessDenied) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 6546 | add_ptp_error_to_errorstack(device, ret, "ACCESS DENIED."); |
Linus Walleij | 99310d4 | 2006-11-01 08:29:39 +0000 | [diff] [blame] | 6547 | } |
Linus Walleij | c86afbd | 2006-05-04 19:05:24 +0000 | [diff] [blame] | 6548 | return 0; |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 6549 | } |
Richard Low | f8cd3d7 | 2009-05-04 16:38:33 +0000 | [diff] [blame] | 6550 | // NOTE: don't destroy the new_folder objectinfo, because it is statically referencing |
| 6551 | // several strings. |
tsaarni | a3eb60a | 2007-07-06 17:41:30 +0000 | [diff] [blame] | 6552 | |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 6553 | add_object_to_cache(device, new_id); |
tsaarni | a3eb60a | 2007-07-06 17:41:30 +0000 | [diff] [blame] | 6554 | |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 6555 | return new_id; |
| 6556 | } |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 6557 | |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 6558 | /** |
| 6559 | * This creates a new playlist metadata structure and allocates memory |
| 6560 | * for it. Notice that if you add strings to this structure they |
| 6561 | * will be freed by the corresponding <code>LIBMTP_destroy_playlist_t</code> |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 6562 | * operation later, so be careful of using strdup() when assigning |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 6563 | * strings, e.g.: |
| 6564 | * |
| 6565 | * <pre> |
| 6566 | * LIBMTP_playlist_t *pl = LIBMTP_new_playlist_t(); |
| 6567 | * pl->name = strdup(str); |
| 6568 | * .... |
| 6569 | * LIBMTP_destroy_playlist_t(pl); |
| 6570 | * </pre> |
| 6571 | * |
| 6572 | * @return a pointer to the newly allocated metadata structure. |
| 6573 | * @see LIBMTP_destroy_playlist_t() |
| 6574 | */ |
| 6575 | LIBMTP_playlist_t *LIBMTP_new_playlist_t(void) |
| 6576 | { |
| 6577 | LIBMTP_playlist_t *new = (LIBMTP_playlist_t *) malloc(sizeof(LIBMTP_playlist_t)); |
| 6578 | if (new == NULL) { |
| 6579 | return NULL; |
| 6580 | } |
| 6581 | new->playlist_id = 0; |
Linus Walleij | 5ce59db | 2008-03-12 21:22:58 +0000 | [diff] [blame] | 6582 | new->parent_id = 0; |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 6583 | new->storage_id = 0; |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 6584 | new->name = NULL; |
| 6585 | new->tracks = NULL; |
| 6586 | new->no_tracks = 0; |
| 6587 | new->next = NULL; |
| 6588 | return new; |
| 6589 | } |
| 6590 | |
| 6591 | /** |
| 6592 | * This destroys a playlist metadata structure and deallocates the memory |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 6593 | * used by it, including any strings. Never use a track metadata |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 6594 | * structure again after calling this function on it. |
| 6595 | * @param playlist the playlist metadata to destroy. |
| 6596 | * @see LIBMTP_new_playlist_t() |
| 6597 | */ |
| 6598 | void LIBMTP_destroy_playlist_t(LIBMTP_playlist_t *playlist) |
| 6599 | { |
| 6600 | if (playlist == NULL) { |
| 6601 | return; |
| 6602 | } |
| 6603 | if (playlist->name != NULL) |
| 6604 | free(playlist->name); |
| 6605 | if (playlist->tracks != NULL) |
| 6606 | free(playlist->tracks); |
| 6607 | free(playlist); |
| 6608 | return; |
| 6609 | } |
| 6610 | |
| 6611 | /** |
| 6612 | * This function returns a list of the playlists available on the |
| 6613 | * device. Typical usage: |
| 6614 | * |
| 6615 | * <pre> |
| 6616 | * </pre> |
| 6617 | * |
| 6618 | * @param device a pointer to the device to get the playlist listing from. |
| 6619 | * @return a playlist list on success, else NULL. If there are no playlists |
| 6620 | * on the device, NULL will be returned as well. |
Linus Walleij | 2e4b5f9 | 2006-06-16 14:00:49 +0000 | [diff] [blame] | 6621 | * @see LIBMTP_Get_Playlist() |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 6622 | */ |
| 6623 | LIBMTP_playlist_t *LIBMTP_Get_Playlist_List(LIBMTP_mtpdevice_t *device) |
| 6624 | { |
Linus Walleij | f3c4405 | 2008-08-16 21:14:56 +0000 | [diff] [blame] | 6625 | PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo; |
| 6626 | const int REQ_SPL = FLAG_PLAYLIST_SPL(ptp_usb); |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 6627 | PTPParams *params = (PTPParams *) device->params; |
| 6628 | LIBMTP_playlist_t *retlists = NULL; |
| 6629 | LIBMTP_playlist_t *curlist = NULL; |
| 6630 | uint32_t i; |
| 6631 | |
| 6632 | // Get all the handles if we haven't already done that |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 6633 | if (params->nrofobjects == 0) { |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 6634 | flush_handles(device); |
| 6635 | } |
| 6636 | |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 6637 | for (i = 0; i < params->nrofobjects; i++) { |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 6638 | LIBMTP_playlist_t *pl; |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 6639 | PTPObject *ob; |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 6640 | uint16_t ret; |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 6641 | |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 6642 | ob = ¶ms->objects[i]; |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 6643 | |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 6644 | // Ignore stuff that isn't playlists |
Linus Walleij | f3c4405 | 2008-08-16 21:14:56 +0000 | [diff] [blame] | 6645 | |
| 6646 | // For Samsung players we must look for the .spl extension explicitly since |
| 6647 | // playlists are not stored as playlist objects. |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 6648 | if ( REQ_SPL && is_spl_playlist(&ob->oi) ) { |
Linus Walleij | f3c4405 | 2008-08-16 21:14:56 +0000 | [diff] [blame] | 6649 | // Allocate a new playlist type |
| 6650 | pl = LIBMTP_new_playlist_t(); |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 6651 | spl_to_playlist_t(device, &ob->oi, ob->oid, pl); |
Linus Walleij | f3c4405 | 2008-08-16 21:14:56 +0000 | [diff] [blame] | 6652 | } |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 6653 | else if ( ob->oi.ObjectFormat != PTP_OFC_MTP_AbstractAudioVideoPlaylist ) { |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 6654 | continue; |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 6655 | } |
Linus Walleij | f3c4405 | 2008-08-16 21:14:56 +0000 | [diff] [blame] | 6656 | else { |
| 6657 | // Allocate a new playlist type |
| 6658 | pl = LIBMTP_new_playlist_t(); |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 6659 | |
Linus Walleij | f329662 | 2008-09-04 20:53:56 +0000 | [diff] [blame] | 6660 | // Try to look up proper name, else use the oi->Filename field. |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 6661 | pl->name = get_string_from_object(device, ob->oid, PTP_OPC_Name); |
Linus Walleij | f329662 | 2008-09-04 20:53:56 +0000 | [diff] [blame] | 6662 | if (pl->name == NULL) { |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 6663 | pl->name = strdup(ob->oi.Filename); |
Linus Walleij | f329662 | 2008-09-04 20:53:56 +0000 | [diff] [blame] | 6664 | } |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 6665 | pl->playlist_id = ob->oid; |
| 6666 | pl->parent_id = ob->oi.ParentObject; |
| 6667 | pl->storage_id = ob->oi.StorageID; |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 6668 | |
Linus Walleij | f3c4405 | 2008-08-16 21:14:56 +0000 | [diff] [blame] | 6669 | // Then get the track listing for this playlist |
| 6670 | ret = ptp_mtp_getobjectreferences(params, pl->playlist_id, &pl->tracks, &pl->no_tracks); |
| 6671 | if (ret != PTP_RC_OK) { |
Linus Walleij | 8d8c435 | 2008-09-23 23:04:16 +0000 | [diff] [blame] | 6672 | add_ptp_error_to_errorstack(device, ret, "LIBMTP_Get_Playlist_List(): " |
| 6673 | "could not get object references."); |
Linus Walleij | f3c4405 | 2008-08-16 21:14:56 +0000 | [diff] [blame] | 6674 | pl->tracks = NULL; |
| 6675 | pl->no_tracks = 0; |
| 6676 | } |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 6677 | } |
| 6678 | |
| 6679 | // Add playlist to a list that will be returned afterwards. |
| 6680 | if (retlists == NULL) { |
| 6681 | retlists = pl; |
| 6682 | curlist = pl; |
| 6683 | } else { |
| 6684 | curlist->next = pl; |
| 6685 | curlist = pl; |
| 6686 | } |
| 6687 | |
| 6688 | // Call callback here if we decide to add that possibility... |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 6689 | } |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 6690 | return retlists; |
| 6691 | } |
| 6692 | |
Linus Walleij | 2e4b5f9 | 2006-06-16 14:00:49 +0000 | [diff] [blame] | 6693 | |
| 6694 | /** |
| 6695 | * This function retrieves an individual playlist from the device. |
| 6696 | * @param device a pointer to the device to get the playlist from. |
| 6697 | * @param plid the unique ID of the playlist to retrieve. |
| 6698 | * @return a valid playlist metadata post or NULL on failure. |
| 6699 | * @see LIBMTP_Get_Playlist_List() |
| 6700 | */ |
| 6701 | LIBMTP_playlist_t *LIBMTP_Get_Playlist(LIBMTP_mtpdevice_t *device, uint32_t const plid) |
| 6702 | { |
Linus Walleij | f3c4405 | 2008-08-16 21:14:56 +0000 | [diff] [blame] | 6703 | PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo; |
| 6704 | const int REQ_SPL = FLAG_PLAYLIST_SPL(ptp_usb); |
Linus Walleij | 2e4b5f9 | 2006-06-16 14:00:49 +0000 | [diff] [blame] | 6705 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 6706 | PTPObject *ob; |
| 6707 | LIBMTP_playlist_t *pl; |
| 6708 | uint16_t ret; |
Linus Walleij | 2e4b5f9 | 2006-06-16 14:00:49 +0000 | [diff] [blame] | 6709 | |
| 6710 | // Get all the handles if we haven't already done that |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 6711 | if (params->nrofobjects == 0) { |
Linus Walleij | 2e4b5f9 | 2006-06-16 14:00:49 +0000 | [diff] [blame] | 6712 | flush_handles(device); |
| 6713 | } |
| 6714 | |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 6715 | ret = ptp_object_want (params, plid, PTPOBJECT_OBJECTINFO_LOADED, &ob); |
| 6716 | if (ret != PTP_RC_OK) |
| 6717 | return NULL; |
Linus Walleij | 2e4b5f9 | 2006-06-16 14:00:49 +0000 | [diff] [blame] | 6718 | |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 6719 | // For Samsung players we must look for the .spl extension explicitly since |
| 6720 | // playlists are not stored as playlist objects. |
| 6721 | if ( REQ_SPL && is_spl_playlist(&ob->oi) ) { |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 6722 | // Allocate a new playlist type |
| 6723 | pl = LIBMTP_new_playlist_t(); |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 6724 | spl_to_playlist_t(device, &ob->oi, ob->oid, pl); |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 6725 | return pl; |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 6726 | } |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 6727 | |
| 6728 | // Ignore stuff that isn't playlists |
| 6729 | else if ( ob->oi.ObjectFormat != PTP_OFC_MTP_AbstractAudioVideoPlaylist ) { |
| 6730 | return NULL; |
| 6731 | } |
| 6732 | |
| 6733 | // Allocate a new playlist type |
| 6734 | pl = LIBMTP_new_playlist_t(); |
| 6735 | |
| 6736 | pl->name = get_string_from_object(device, ob->oid, PTP_OPC_Name); |
| 6737 | if (pl->name == NULL) { |
| 6738 | pl->name = strdup(ob->oi.Filename); |
| 6739 | } |
| 6740 | pl->playlist_id = ob->oid; |
| 6741 | pl->parent_id = ob->oi.ParentObject; |
| 6742 | pl->storage_id = ob->oi.StorageID; |
| 6743 | |
| 6744 | // Then get the track listing for this playlist |
| 6745 | ret = ptp_mtp_getobjectreferences(params, pl->playlist_id, &pl->tracks, &pl->no_tracks); |
| 6746 | if (ret != PTP_RC_OK) { |
| 6747 | add_ptp_error_to_errorstack(device, ret, "LIBMTP_Get_Playlist(): Could not get object references."); |
| 6748 | pl->tracks = NULL; |
| 6749 | pl->no_tracks = 0; |
| 6750 | } |
| 6751 | |
| 6752 | return pl; |
Linus Walleij | 2e4b5f9 | 2006-06-16 14:00:49 +0000 | [diff] [blame] | 6753 | } |
| 6754 | |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 6755 | /** |
Linus Walleij | b7426d1 | 2006-11-25 23:19:11 +0000 | [diff] [blame] | 6756 | * This function creates a new abstract list such as a playlist |
| 6757 | * or an album. |
| 6758 | * |
| 6759 | * @param device a pointer to the device to create the new abstract list |
| 6760 | * on. |
| 6761 | * @param name the name of the new abstract list. |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 6762 | * @param artist the artist of the new abstract list or NULL. |
| 6763 | * @param genre the genre of the new abstract list or NULL. |
Linus Walleij | b7426d1 | 2006-11-25 23:19:11 +0000 | [diff] [blame] | 6764 | * @param parenthandle the handle of the parent or 0 for no parent |
| 6765 | * i.e. the root folder. |
| 6766 | * @param objectformat the abstract list type to create. |
| 6767 | * @param suffix the ".foo" (4 characters) suffix to use for the virtual |
| 6768 | * "file" created by this operation. |
| 6769 | * @param newid a pointer to a variable that will hold the new object |
| 6770 | * ID if this call is successful. |
| 6771 | * @param tracks an array of tracks to associate with this list. |
| 6772 | * @param no_tracks the number of tracks in the list. |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 6773 | * @return 0 on success, any other value means failure. |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 6774 | */ |
Linus Walleij | b7426d1 | 2006-11-25 23:19:11 +0000 | [diff] [blame] | 6775 | static int create_new_abstract_list(LIBMTP_mtpdevice_t *device, |
| 6776 | char const * const name, |
Linus Walleij | adce4a5 | 2007-04-23 07:28:11 +0000 | [diff] [blame] | 6777 | char const * const artist, |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 6778 | char const * const composer, |
Linus Walleij | adce4a5 | 2007-04-23 07:28:11 +0000 | [diff] [blame] | 6779 | char const * const genre, |
Linus Walleij | b7426d1 | 2006-11-25 23:19:11 +0000 | [diff] [blame] | 6780 | uint32_t const parenthandle, |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 6781 | uint32_t const storageid, |
Linus Walleij | b7426d1 | 2006-11-25 23:19:11 +0000 | [diff] [blame] | 6782 | uint16_t const objectformat, |
| 6783 | char const * const suffix, |
| 6784 | uint32_t * const newid, |
| 6785 | uint32_t const * const tracks, |
| 6786 | uint32_t const no_tracks) |
| 6787 | |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 6788 | { |
Linus Walleij | b7426d1 | 2006-11-25 23:19:11 +0000 | [diff] [blame] | 6789 | int i; |
| 6790 | int supported = 0; |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 6791 | uint16_t ret; |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 6792 | uint16_t *properties = NULL; |
rreardon | 508705f | 2006-11-19 21:27:22 +0000 | [diff] [blame] | 6793 | uint32_t propcnt = 0; |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 6794 | uint32_t store; |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 6795 | uint32_t localph = parenthandle; |
Linus Walleij | b7426d1 | 2006-11-25 23:19:11 +0000 | [diff] [blame] | 6796 | uint8_t nonconsumable = 0x00U; /* By default it is consumable */ |
| 6797 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | d3b7857 | 2007-08-24 21:28:24 +0000 | [diff] [blame] | 6798 | PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo; |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 6799 | char fname[256]; |
Linus Walleij | d14e84f | 2006-06-16 14:50:59 +0000 | [diff] [blame] | 6800 | uint8_t data[2]; |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 6801 | |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 6802 | if (storageid == 0) { |
Linus Walleij | d71d0b3 | 2008-09-22 08:21:03 +0000 | [diff] [blame] | 6803 | // I'm just guessing that an abstract list may require 512 bytes |
| 6804 | store = get_writeable_storageid(device, 512); |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 6805 | } else { |
| 6806 | store = storageid; |
| 6807 | } |
| 6808 | |
Linus Walleij | b7426d1 | 2006-11-25 23:19:11 +0000 | [diff] [blame] | 6809 | // Check if we can create an object of this type |
| 6810 | for ( i=0; i < params->deviceinfo.ImageFormats_len; i++ ) { |
| 6811 | if (params->deviceinfo.ImageFormats[i] == objectformat) { |
| 6812 | supported = 1; |
| 6813 | break; |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 6814 | } |
| 6815 | } |
Linus Walleij | b7426d1 | 2006-11-25 23:19:11 +0000 | [diff] [blame] | 6816 | if (!supported) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 6817 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "create_new_abstract_list(): player does not support this abstract type."); |
Linus Walleij | f329662 | 2008-09-04 20:53:56 +0000 | [diff] [blame] | 6818 | printf("Unsupported abstract list type: %04x\n", objectformat); |
Linus Walleij | b7426d1 | 2006-11-25 23:19:11 +0000 | [diff] [blame] | 6819 | return -1; |
| 6820 | } |
| 6821 | |
Richard Low | 15731fe | 2007-03-22 20:27:20 +0000 | [diff] [blame] | 6822 | // add the new suffix if it isn't there |
Linus Walleij | 629fe40 | 2006-12-12 23:39:15 +0000 | [diff] [blame] | 6823 | fname[0] = '\0'; |
Linus Walleij | b7426d1 | 2006-11-25 23:19:11 +0000 | [diff] [blame] | 6824 | if (strlen(name) > strlen(suffix)) { |
| 6825 | char const * const suff = &name[strlen(name)-strlen(suffix)]; |
| 6826 | if (!strcmp(suff, suffix)) { |
| 6827 | // Home free. |
Linus Walleij | 629fe40 | 2006-12-12 23:39:15 +0000 | [diff] [blame] | 6828 | strncpy(fname, name, sizeof(fname)); |
Linus Walleij | b7426d1 | 2006-11-25 23:19:11 +0000 | [diff] [blame] | 6829 | } |
| 6830 | } |
| 6831 | // If it didn't end with "<suffix>" then add that here. |
Linus Walleij | 629fe40 | 2006-12-12 23:39:15 +0000 | [diff] [blame] | 6832 | if (fname[0] == '\0') { |
Linus Walleij | b7426d1 | 2006-11-25 23:19:11 +0000 | [diff] [blame] | 6833 | strncpy(fname, name, sizeof(fname)-strlen(suffix)-1); |
| 6834 | strcat(fname, suffix); |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 6835 | fname[sizeof(fname)-1] = '\0'; |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 6836 | } |
| 6837 | |
Linus Walleij | f67c1ad | 2009-01-14 21:39:50 +0000 | [diff] [blame] | 6838 | if (ptp_operation_issupported(params, PTP_OC_MTP_SendObjectPropList) && |
| 6839 | !FLAG_BROKEN_SEND_OBJECT_PROPLIST(ptp_usb)) { |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 6840 | MTPProperties *props = NULL; |
| 6841 | MTPProperties *prop = NULL; |
| 6842 | int nrofprops = 0; |
Richard Low | 6c0a6ce | 2006-11-26 10:42:08 +0000 | [diff] [blame] | 6843 | |
| 6844 | *newid = 0x00000000U; |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 6845 | |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 6846 | ret = ptp_mtp_getobjectpropssupported(params, objectformat, &propcnt, &properties); |
mopoke | 3136444 | 2006-11-20 04:53:04 +0000 | [diff] [blame] | 6847 | |
rreardon | 508705f | 2006-11-19 21:27:22 +0000 | [diff] [blame] | 6848 | for (i=0;i<propcnt;i++) { |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 6849 | PTPObjectPropDesc opd; |
| 6850 | |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 6851 | ret = ptp_mtp_getobjectpropdesc(params, properties[i], objectformat, &opd); |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 6852 | if (ret != PTP_RC_OK) { |
| 6853 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "create_new_abstract_list(): " |
| 6854 | "could not get property description."); |
| 6855 | } else if (opd.GetSet) { |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 6856 | switch (properties[i]) { |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 6857 | case PTP_OPC_ObjectFileName: |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 6858 | prop = ptp_get_new_object_prop_entry(&props,&nrofprops); |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 6859 | prop->ObjectHandle = *newid; |
| 6860 | prop->property = PTP_OPC_ObjectFileName; |
| 6861 | prop->datatype = PTP_DTC_STR; |
| 6862 | prop->propval.str = strdup(fname); |
Linus Walleij | fec4d56 | 2008-06-01 22:30:36 +0000 | [diff] [blame] | 6863 | if (FLAG_ONLY_7BIT_FILENAMES(ptp_usb)) { |
Linus Walleij | d3b7857 | 2007-08-24 21:28:24 +0000 | [diff] [blame] | 6864 | strip_7bit_from_utf8(prop->propval.str); |
| 6865 | } |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 6866 | break; |
| 6867 | case PTP_OPC_ProtectionStatus: |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 6868 | prop = ptp_get_new_object_prop_entry(&props,&nrofprops); |
Linus Walleij | adce4a5 | 2007-04-23 07:28:11 +0000 | [diff] [blame] | 6869 | prop->ObjectHandle = *newid; |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 6870 | prop->property = PTP_OPC_ProtectionStatus; |
| 6871 | prop->datatype = PTP_DTC_UINT16; |
| 6872 | prop->propval.u16 = 0x0000U; /* Not protected */ |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 6873 | break; |
| 6874 | case PTP_OPC_NonConsumable: |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 6875 | prop = ptp_get_new_object_prop_entry(&props,&nrofprops); |
Linus Walleij | adce4a5 | 2007-04-23 07:28:11 +0000 | [diff] [blame] | 6876 | prop->ObjectHandle = *newid; |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 6877 | prop->property = PTP_OPC_NonConsumable; |
| 6878 | prop->datatype = PTP_DTC_UINT8; |
| 6879 | prop->propval.u8 = nonconsumable; |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 6880 | break; |
| 6881 | case PTP_OPC_Name: |
| 6882 | if (name != NULL) { |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 6883 | prop = ptp_get_new_object_prop_entry(&props,&nrofprops); |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 6884 | prop->ObjectHandle = *newid; |
| 6885 | prop->property = PTP_OPC_Name; |
| 6886 | prop->datatype = PTP_DTC_STR; |
| 6887 | prop->propval.str = strdup(name); |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 6888 | } |
| 6889 | break; |
Linus Walleij | 0b75ab6 | 2007-12-28 00:47:34 +0000 | [diff] [blame] | 6890 | case PTP_OPC_AlbumArtist: |
| 6891 | if (artist != NULL) { |
| 6892 | prop = ptp_get_new_object_prop_entry(&props,&nrofprops); |
| 6893 | prop->ObjectHandle = *newid; |
| 6894 | prop->property = PTP_OPC_AlbumArtist; |
| 6895 | prop->datatype = PTP_DTC_STR; |
| 6896 | prop->propval.str = strdup(artist); |
| 6897 | } |
| 6898 | break; |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 6899 | case PTP_OPC_Artist: |
| 6900 | if (artist != NULL) { |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 6901 | prop = ptp_get_new_object_prop_entry(&props,&nrofprops); |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 6902 | prop->ObjectHandle = *newid; |
| 6903 | prop->property = PTP_OPC_Artist; |
| 6904 | prop->datatype = PTP_DTC_STR; |
| 6905 | prop->propval.str = strdup(artist); |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 6906 | } |
| 6907 | break; |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 6908 | case PTP_OPC_Composer: |
| 6909 | if (composer != NULL) { |
| 6910 | prop = ptp_get_new_object_prop_entry(&props,&nrofprops); |
| 6911 | prop->ObjectHandle = *newid; |
| 6912 | prop->property = PTP_OPC_Composer; |
| 6913 | prop->datatype = PTP_DTC_STR; |
| 6914 | prop->propval.str = strdup(composer); |
| 6915 | } |
| 6916 | break; |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 6917 | case PTP_OPC_Genre: |
| 6918 | if (genre != NULL) { |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 6919 | prop = ptp_get_new_object_prop_entry(&props,&nrofprops); |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 6920 | prop->ObjectHandle = *newid; |
| 6921 | prop->property = PTP_OPC_Genre; |
| 6922 | prop->datatype = PTP_DTC_STR; |
| 6923 | prop->propval.str = strdup(genre); |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 6924 | } |
| 6925 | break; |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 6926 | case PTP_OPC_DateModified: |
| 6927 | // Tag with current time if that is supported |
Linus Walleij | cf8dc2b | 2008-10-21 13:58:36 +0000 | [diff] [blame] | 6928 | if (!FLAG_CANNOT_HANDLE_DATEMODIFIED(ptp_usb)) { |
| 6929 | prop = ptp_get_new_object_prop_entry(&props,&nrofprops); |
| 6930 | prop->ObjectHandle = *newid; |
| 6931 | prop->property = PTP_OPC_DateModified; |
| 6932 | prop->datatype = PTP_DTC_STR; |
| 6933 | prop->propval.str = get_iso8601_stamp(); |
| 6934 | } |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 6935 | break; |
Linus Walleij | adce4a5 | 2007-04-23 07:28:11 +0000 | [diff] [blame] | 6936 | } |
rreardon | 508705f | 2006-11-19 21:27:22 +0000 | [diff] [blame] | 6937 | } |
Linus Walleij | dbcc824 | 2007-08-05 22:31:26 +0000 | [diff] [blame] | 6938 | ptp_free_objectpropdesc(&opd); |
rreardon | 508705f | 2006-11-19 21:27:22 +0000 | [diff] [blame] | 6939 | } |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 6940 | free(properties); |
mopoke | 3136444 | 2006-11-20 04:53:04 +0000 | [diff] [blame] | 6941 | |
Linus Walleij | b7426d1 | 2006-11-25 23:19:11 +0000 | [diff] [blame] | 6942 | ret = ptp_mtp_sendobjectproplist(params, &store, &localph, newid, |
Linus Walleij | 1e9a033 | 2007-09-12 19:35:56 +0000 | [diff] [blame] | 6943 | objectformat, 0, props, nrofprops); |
mopoke | 3136444 | 2006-11-20 04:53:04 +0000 | [diff] [blame] | 6944 | |
rreardon | 508705f | 2006-11-19 21:27:22 +0000 | [diff] [blame] | 6945 | /* Free property list */ |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 6946 | ptp_destroy_object_prop_list(props, nrofprops); |
mopoke | 3136444 | 2006-11-20 04:53:04 +0000 | [diff] [blame] | 6947 | |
rreardon | 508705f | 2006-11-19 21:27:22 +0000 | [diff] [blame] | 6948 | if (ret != PTP_RC_OK) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 6949 | 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] | 6950 | if (ret == PTP_RC_AccessDenied) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 6951 | add_ptp_error_to_errorstack(device, ret, "ACCESS DENIED."); |
rreardon | 508705f | 2006-11-19 21:27:22 +0000 | [diff] [blame] | 6952 | } |
| 6953 | return -1; |
| 6954 | } |
Richard Low | a6c924c | 2006-12-29 17:44:28 +0000 | [diff] [blame] | 6955 | |
Richard Low | c3f5fc3 | 2007-07-29 09:40:50 +0000 | [diff] [blame] | 6956 | // now send the blank object |
Richard Low | a6c924c | 2006-12-29 17:44:28 +0000 | [diff] [blame] | 6957 | ret = ptp_sendobject(params, NULL, 0); |
| 6958 | if (ret != PTP_RC_OK) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 6959 | 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] | 6960 | return -1; |
| 6961 | } |
Linus Walleij | 629fe40 | 2006-12-12 23:39:15 +0000 | [diff] [blame] | 6962 | |
rreardon | 508705f | 2006-11-19 21:27:22 +0000 | [diff] [blame] | 6963 | } else if (ptp_operation_issupported(params,PTP_OC_SendObjectInfo)) { |
Linus Walleij | 629fe40 | 2006-12-12 23:39:15 +0000 | [diff] [blame] | 6964 | PTPObjectInfo new_object; |
| 6965 | |
| 6966 | new_object.Filename = fname; |
Linus Walleij | fec4d56 | 2008-06-01 22:30:36 +0000 | [diff] [blame] | 6967 | if (FLAG_ONLY_7BIT_FILENAMES(ptp_usb)) { |
Linus Walleij | d3b7857 | 2007-08-24 21:28:24 +0000 | [diff] [blame] | 6968 | strip_7bit_from_utf8(new_object.Filename); |
| 6969 | } |
Linus Walleij | 629fe40 | 2006-12-12 23:39:15 +0000 | [diff] [blame] | 6970 | new_object.ObjectCompressedSize = 1; |
| 6971 | new_object.ObjectFormat = objectformat; |
| 6972 | |
Linus Walleij | 2069848 | 2006-11-24 20:54:21 +0000 | [diff] [blame] | 6973 | // Create the object |
Linus Walleij | b7426d1 | 2006-11-25 23:19:11 +0000 | [diff] [blame] | 6974 | ret = ptp_sendobjectinfo(params, &store, &localph, newid, &new_object); |
Linus Walleij | 2069848 | 2006-11-24 20:54:21 +0000 | [diff] [blame] | 6975 | if (ret != PTP_RC_OK) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 6976 | 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] | 6977 | if (ret == PTP_RC_AccessDenied) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 6978 | add_ptp_error_to_errorstack(device, ret, "ACCESS DENIED."); |
Linus Walleij | 2069848 | 2006-11-24 20:54:21 +0000 | [diff] [blame] | 6979 | } |
| 6980 | return -1; |
| 6981 | } |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 6982 | // NOTE: don't destroy new_object objectinfo afterwards - the strings it contains are |
| 6983 | // not copies. |
Richard Low | a6c924c | 2006-12-29 17:44:28 +0000 | [diff] [blame] | 6984 | |
| 6985 | /* |
| 6986 | * We have to send this one blank data byte. |
| 6987 | * If we don't, the handle will not be created and thus there is no playlist. |
| 6988 | */ |
| 6989 | data[0] = '\0'; |
| 6990 | data[1] = '\0'; |
| 6991 | ret = ptp_sendobject(params, data, 1); |
| 6992 | if (ret != PTP_RC_OK) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 6993 | 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] | 6994 | return -1; |
| 6995 | } |
Richard Low | ff16bfa | 2008-09-26 19:23:08 +0000 | [diff] [blame] | 6996 | |
| 6997 | // set the properties one by one |
| 6998 | ret = ptp_mtp_getobjectpropssupported(params, objectformat, &propcnt, &properties); |
| 6999 | |
| 7000 | for (i=0;i<propcnt;i++) { |
| 7001 | PTPObjectPropDesc opd; |
| 7002 | |
| 7003 | ret = ptp_mtp_getobjectpropdesc(params, properties[i], objectformat, &opd); |
| 7004 | if (ret != PTP_RC_OK) { |
| 7005 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "create_new_abstract_list(): " |
| 7006 | "could not get property description."); |
| 7007 | } else if (opd.GetSet) { |
| 7008 | switch (properties[i]) { |
| 7009 | case PTP_OPC_Name: |
| 7010 | if (name != NULL) { |
Linus Walleij | 3758814 | 2008-10-16 19:10:47 +0000 | [diff] [blame] | 7011 | ret = set_object_string(device, *newid, PTP_OPC_Name, name); |
| 7012 | if (ret != 0) { |
| 7013 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "create_new_abstract_list(): could not set entity name."); |
| 7014 | return -1; |
| 7015 | } |
| 7016 | } |
Richard Low | ff16bfa | 2008-09-26 19:23:08 +0000 | [diff] [blame] | 7017 | break; |
| 7018 | case PTP_OPC_AlbumArtist: |
| 7019 | if (artist != NULL) { |
Linus Walleij | 3758814 | 2008-10-16 19:10:47 +0000 | [diff] [blame] | 7020 | ret = set_object_string(device, *newid, PTP_OPC_AlbumArtist, artist); |
| 7021 | if (ret != 0) { |
| 7022 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "create_new_abstract_list(): could not set entity album artist."); |
| 7023 | return -1; |
| 7024 | } |
| 7025 | } |
Richard Low | ff16bfa | 2008-09-26 19:23:08 +0000 | [diff] [blame] | 7026 | break; |
| 7027 | case PTP_OPC_Artist: |
| 7028 | if (artist != NULL) { |
Linus Walleij | 3758814 | 2008-10-16 19:10:47 +0000 | [diff] [blame] | 7029 | ret = set_object_string(device, *newid, PTP_OPC_Artist, artist); |
| 7030 | if (ret != 0) { |
| 7031 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "create_new_abstract_list(): could not set entity artist."); |
| 7032 | return -1; |
| 7033 | } |
| 7034 | } |
Richard Low | ff16bfa | 2008-09-26 19:23:08 +0000 | [diff] [blame] | 7035 | break; |
| 7036 | case PTP_OPC_Composer: |
| 7037 | if (composer != NULL) { |
Linus Walleij | 3758814 | 2008-10-16 19:10:47 +0000 | [diff] [blame] | 7038 | ret = set_object_string(device, *newid, PTP_OPC_Composer, composer); |
| 7039 | if (ret != 0) { |
| 7040 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "create_new_abstract_list(): could not set entity composer."); |
| 7041 | return -1; |
| 7042 | } |
| 7043 | } |
Richard Low | ff16bfa | 2008-09-26 19:23:08 +0000 | [diff] [blame] | 7044 | break; |
| 7045 | case PTP_OPC_Genre: |
| 7046 | if (genre != NULL) { |
Linus Walleij | 3758814 | 2008-10-16 19:10:47 +0000 | [diff] [blame] | 7047 | ret = set_object_string(device, *newid, PTP_OPC_Genre, genre); |
| 7048 | if (ret != 0) { |
| 7049 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "create_new_abstract_list(): could not set entity genre."); |
| 7050 | return -1; |
| 7051 | } |
| 7052 | } |
Richard Low | ff16bfa | 2008-09-26 19:23:08 +0000 | [diff] [blame] | 7053 | break; |
| 7054 | case PTP_OPC_DateModified: |
Linus Walleij | cf8dc2b | 2008-10-21 13:58:36 +0000 | [diff] [blame] | 7055 | if (!FLAG_CANNOT_HANDLE_DATEMODIFIED(ptp_usb)) { |
| 7056 | ret = set_object_string(device, *newid, PTP_OPC_DateModified, get_iso8601_stamp()); |
| 7057 | if (ret != 0) { |
| 7058 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "create_new_abstract_list(): could not set date modified."); |
| 7059 | return -1; |
| 7060 | } |
Linus Walleij | 3758814 | 2008-10-16 19:10:47 +0000 | [diff] [blame] | 7061 | } |
| 7062 | break; |
Richard Low | ff16bfa | 2008-09-26 19:23:08 +0000 | [diff] [blame] | 7063 | } |
| 7064 | } |
| 7065 | ptp_free_objectpropdesc(&opd); |
| 7066 | } |
| 7067 | free(properties); |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 7068 | } |
| 7069 | |
Linus Walleij | b7426d1 | 2006-11-25 23:19:11 +0000 | [diff] [blame] | 7070 | if (no_tracks > 0) { |
Linus Walleij | 1ccd586 | 2008-11-11 12:33:01 +0000 | [diff] [blame] | 7071 | // Add tracks to the list as object references. |
Linus Walleij | b7426d1 | 2006-11-25 23:19:11 +0000 | [diff] [blame] | 7072 | ret = ptp_mtp_setobjectreferences (params, *newid, (uint32_t *) tracks, no_tracks); |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 7073 | if (ret != PTP_RC_OK) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 7074 | 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] | 7075 | return -1; |
| 7076 | } |
| 7077 | } |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 7078 | |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 7079 | add_object_to_cache(device, *newid); |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 7080 | |
| 7081 | return 0; |
| 7082 | } |
| 7083 | |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 7084 | /** |
| 7085 | * This updates the metadata and track listing |
| 7086 | * for an abstract list. |
| 7087 | * @param device a pointer to the device that the abstract list |
| 7088 | * resides on. |
| 7089 | * @param name the name of the abstract list. |
| 7090 | * @param artist the artist of the abstract list or NULL. |
| 7091 | * @param genre the genre of the abstract list or NULL. |
| 7092 | * @param objecthandle the object to be updated. |
| 7093 | * @param objectformat the abstract list type to update. |
| 7094 | * @param tracks an array of tracks to associate with this list. |
| 7095 | * @param no_tracks the number of tracks in the list. |
| 7096 | * @return 0 on success, any other value means failure. |
| 7097 | */ |
| 7098 | static int update_abstract_list(LIBMTP_mtpdevice_t *device, |
| 7099 | char const * const name, |
| 7100 | char const * const artist, |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 7101 | char const * const composer, |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 7102 | char const * const genre, |
| 7103 | uint32_t const objecthandle, |
| 7104 | uint16_t const objectformat, |
| 7105 | uint32_t const * const tracks, |
| 7106 | uint32_t const no_tracks) |
| 7107 | { |
| 7108 | uint16_t ret; |
| 7109 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | f9267e9 | 2007-10-15 21:07:37 +0000 | [diff] [blame] | 7110 | PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo; |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 7111 | uint16_t *properties = NULL; |
| 7112 | uint32_t propcnt = 0; |
| 7113 | int i; |
| 7114 | |
| 7115 | // First see which properties can be set |
| 7116 | // i.e only try to update this metadata for object tags that exist on the current player. |
| 7117 | ret = ptp_mtp_getobjectpropssupported(params, objectformat, &propcnt, &properties); |
| 7118 | if (ret != PTP_RC_OK) { |
| 7119 | // Just bail out for now, nothing is ever set. |
| 7120 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "update_abstract_list(): " |
| 7121 | "could not retrieve supported object properties."); |
| 7122 | return -1; |
| 7123 | } |
Linus Walleij | f9267e9 | 2007-10-15 21:07:37 +0000 | [diff] [blame] | 7124 | if (ptp_operation_issupported(params,PTP_OC_MTP_SetObjPropList) && |
Linus Walleij | fec4d56 | 2008-06-01 22:30:36 +0000 | [diff] [blame] | 7125 | !FLAG_BROKEN_SET_OBJECT_PROPLIST(ptp_usb)) { |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 7126 | MTPProperties *props = NULL; |
| 7127 | MTPProperties *prop = NULL; |
| 7128 | int nrofprops = 0; |
| 7129 | |
| 7130 | for (i=0;i<propcnt;i++) { |
| 7131 | PTPObjectPropDesc opd; |
| 7132 | |
| 7133 | ret = ptp_mtp_getobjectpropdesc(params, properties[i], objectformat, &opd); |
| 7134 | if (ret != PTP_RC_OK) { |
| 7135 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "update_abstract_list(): " |
| 7136 | "could not get property description."); |
| 7137 | } else if (opd.GetSet) { |
| 7138 | switch (properties[i]) { |
| 7139 | case PTP_OPC_Name: |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 7140 | prop = ptp_get_new_object_prop_entry(&props, &nrofprops); |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 7141 | prop->ObjectHandle = objecthandle; |
| 7142 | prop->property = PTP_OPC_Name; |
| 7143 | prop->datatype = PTP_DTC_STR; |
| 7144 | if (name != NULL) |
| 7145 | prop->propval.str = strdup(name); |
| 7146 | break; |
Linus Walleij | 0b75ab6 | 2007-12-28 00:47:34 +0000 | [diff] [blame] | 7147 | case PTP_OPC_AlbumArtist: |
| 7148 | if (artist != NULL) { |
| 7149 | prop = ptp_get_new_object_prop_entry(&props, &nrofprops); |
| 7150 | prop->ObjectHandle = objecthandle; |
| 7151 | prop->property = PTP_OPC_AlbumArtist; |
| 7152 | prop->datatype = PTP_DTC_STR; |
| 7153 | prop->propval.str = strdup(artist); |
| 7154 | } |
| 7155 | break; |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 7156 | case PTP_OPC_Artist: |
| 7157 | if (artist != NULL) { |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 7158 | prop = ptp_get_new_object_prop_entry(&props, &nrofprops); |
Linus Walleij | 0b75ab6 | 2007-12-28 00:47:34 +0000 | [diff] [blame] | 7159 | prop->ObjectHandle = objecthandle; |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 7160 | prop->property = PTP_OPC_Artist; |
| 7161 | prop->datatype = PTP_DTC_STR; |
| 7162 | prop->propval.str = strdup(artist); |
| 7163 | } |
| 7164 | break; |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 7165 | case PTP_OPC_Composer: |
| 7166 | if (composer != NULL) { |
| 7167 | prop = ptp_get_new_object_prop_entry(&props, &nrofprops); |
| 7168 | prop->ObjectHandle = objecthandle; |
| 7169 | prop->property = PTP_OPC_Composer; |
| 7170 | prop->datatype = PTP_DTC_STR; |
| 7171 | prop->propval.str = strdup(composer); |
| 7172 | } |
| 7173 | break; |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 7174 | case PTP_OPC_Genre: |
| 7175 | if (genre != NULL) { |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 7176 | prop = ptp_get_new_object_prop_entry(&props, &nrofprops); |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 7177 | prop->ObjectHandle = objecthandle; |
| 7178 | prop->property = PTP_OPC_Genre; |
| 7179 | prop->datatype = PTP_DTC_STR; |
| 7180 | prop->propval.str = strdup(genre); |
| 7181 | } |
| 7182 | break; |
| 7183 | case PTP_OPC_DateModified: |
Linus Walleij | cf8dc2b | 2008-10-21 13:58:36 +0000 | [diff] [blame] | 7184 | if (!FLAG_CANNOT_HANDLE_DATEMODIFIED(ptp_usb)) { |
Linus Walleij | 3758814 | 2008-10-16 19:10:47 +0000 | [diff] [blame] | 7185 | // Tag with current time if that is supported |
| 7186 | prop = ptp_get_new_object_prop_entry(&props, &nrofprops); |
| 7187 | prop->ObjectHandle = objecthandle; |
| 7188 | prop->property = PTP_OPC_DateModified; |
| 7189 | prop->datatype = PTP_DTC_STR; |
| 7190 | prop->propval.str = get_iso8601_stamp(); |
| 7191 | } |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 7192 | break; |
| 7193 | default: |
| 7194 | break; |
| 7195 | } |
| 7196 | } |
| 7197 | ptp_free_objectpropdesc(&opd); |
| 7198 | } |
| 7199 | |
| 7200 | // proplist could be NULL if we can't write any properties |
| 7201 | if (props != NULL) { |
| 7202 | ret = ptp_mtp_setobjectproplist(params, props, nrofprops); |
| 7203 | |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 7204 | ptp_destroy_object_prop_list(props, nrofprops); |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 7205 | |
| 7206 | if (ret != PTP_RC_OK) { |
| 7207 | // TODO: return error of which property we couldn't set |
| 7208 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "update_abstract_list(): " |
| 7209 | "could not set object property list."); |
Richard Low | 016e373 | 2007-09-23 14:53:46 +0000 | [diff] [blame] | 7210 | free(properties); |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 7211 | return -1; |
| 7212 | } |
| 7213 | } |
| 7214 | |
| 7215 | } else if (ptp_operation_issupported(params,PTP_OC_MTP_SetObjectPropValue)) { |
| 7216 | for (i=0;i<propcnt;i++) { |
| 7217 | switch (properties[i]) { |
| 7218 | case PTP_OPC_Name: |
| 7219 | // Update title |
| 7220 | ret = set_object_string(device, objecthandle, PTP_OPC_Name, name); |
| 7221 | if (ret != 0) { |
| 7222 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "update_abstract_list(): " |
| 7223 | "could not set title."); |
| 7224 | } |
| 7225 | break; |
Linus Walleij | 0b75ab6 | 2007-12-28 00:47:34 +0000 | [diff] [blame] | 7226 | case PTP_OPC_AlbumArtist: |
| 7227 | // Update album artist |
| 7228 | ret = set_object_string(device, objecthandle, PTP_OPC_AlbumArtist, artist); |
| 7229 | if (ret != 0) { |
| 7230 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "update_abstract_list(): " |
| 7231 | "could not set album artist name."); |
| 7232 | } |
| 7233 | break; |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 7234 | case PTP_OPC_Artist: |
| 7235 | // Update artist |
| 7236 | ret = set_object_string(device, objecthandle, PTP_OPC_Artist, artist); |
| 7237 | if (ret != 0) { |
| 7238 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "update_abstract_list(): " |
| 7239 | "could not set artist name."); |
| 7240 | } |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 7241 | case PTP_OPC_Composer: |
| 7242 | // Update composer |
| 7243 | ret = set_object_string(device, objecthandle, PTP_OPC_Composer, composer); |
| 7244 | if (ret != 0) { |
| 7245 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "update_abstract_list(): " |
| 7246 | "could not set composer name."); |
| 7247 | } |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 7248 | break; |
| 7249 | case PTP_OPC_Genre: |
| 7250 | // Update genre |
| 7251 | ret = set_object_string(device, objecthandle, PTP_OPC_Genre, genre); |
| 7252 | if (ret != 0) { |
| 7253 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "update_abstract_list(): " |
| 7254 | "could not set genre."); |
| 7255 | } |
| 7256 | break; |
| 7257 | case PTP_OPC_DateModified: |
| 7258 | // Update date modified |
Linus Walleij | cf8dc2b | 2008-10-21 13:58:36 +0000 | [diff] [blame] | 7259 | if (!FLAG_CANNOT_HANDLE_DATEMODIFIED(ptp_usb)) { |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 7260 | char *tmpdate = get_iso8601_stamp(); |
| 7261 | ret = set_object_string(device, objecthandle, PTP_OPC_DateModified, tmpdate); |
| 7262 | if (ret != 0) { |
| 7263 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "update_abstract_list(): " |
| 7264 | "could not set modification date."); |
| 7265 | } |
| 7266 | free(tmpdate); |
| 7267 | } |
| 7268 | default: |
| 7269 | break; |
| 7270 | } |
| 7271 | } |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 7272 | } else { |
| 7273 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "update_abstract_list(): " |
| 7274 | "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] | 7275 | free(properties); |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 7276 | return -1; |
| 7277 | } |
| 7278 | |
| 7279 | // Then the object references... |
Linus Walleij | 1ccd586 | 2008-11-11 12:33:01 +0000 | [diff] [blame] | 7280 | ret = ptp_mtp_setobjectreferences (params, objecthandle, (uint32_t *) tracks, no_tracks); |
| 7281 | if (ret != PTP_RC_OK) { |
| 7282 | add_ptp_error_to_errorstack(device, ret, "update_abstract_list(): could not add tracks as object references."); |
| 7283 | free(properties); |
| 7284 | return -1; |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 7285 | } |
| 7286 | |
Richard Low | 016e373 | 2007-09-23 14:53:46 +0000 | [diff] [blame] | 7287 | free(properties); |
Linus Walleij | 7752b95 | 2007-10-19 20:11:46 +0000 | [diff] [blame] | 7288 | |
| 7289 | update_metadata_cache(device, objecthandle); |
Richard Low | 016e373 | 2007-09-23 14:53:46 +0000 | [diff] [blame] | 7290 | |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 7291 | return 0; |
| 7292 | } |
| 7293 | |
Linus Walleij | b7426d1 | 2006-11-25 23:19:11 +0000 | [diff] [blame] | 7294 | |
| 7295 | /** |
| 7296 | * This routine creates a new playlist based on the metadata |
| 7297 | * supplied. If the <code>tracks</code> field of the metadata |
| 7298 | * contains a track listing, these tracks will be added to the |
| 7299 | * playlist. |
| 7300 | * @param device a pointer to the device to create the new playlist on. |
| 7301 | * @param metadata the metadata for the new playlist. If the function |
| 7302 | * exits with success, the <code>playlist_id</code> field of this |
| 7303 | * struct will contain the new playlist ID of the playlist. |
Linus Walleij | 2fe43e4 | 2009-01-30 20:31:26 +0000 | [diff] [blame] | 7304 | * <ul> |
| 7305 | * <li><code>metadata->parent_id</code> should be set to the parent |
| 7306 | * (e.g. folder) to store this track in. Since some |
| 7307 | * devices are a bit picky about where files |
| 7308 | * are placed, a default folder will be chosen if libmtp |
| 7309 | * has detected one for the current filetype and this |
| 7310 | * parameter is set to 0. If this is 0 and no default folder |
| 7311 | * can be found, the file will be stored in the root folder. |
| 7312 | * <li><code>metadata->storage_id</code> should be set to the |
| 7313 | * desired storage (e.g. memory card or whatever your device |
| 7314 | * presents) to store this track in. Setting this to 0 will store |
| 7315 | * the track on the primary storage. |
| 7316 | * </ul> |
Linus Walleij | b7426d1 | 2006-11-25 23:19:11 +0000 | [diff] [blame] | 7317 | * @return 0 on success, any other value means failure. |
| 7318 | * @see LIBMTP_Update_Playlist() |
| 7319 | * @see LIBMTP_Delete_Object() |
| 7320 | */ |
| 7321 | int LIBMTP_Create_New_Playlist(LIBMTP_mtpdevice_t *device, |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 7322 | LIBMTP_playlist_t * const metadata) |
Linus Walleij | b7426d1 | 2006-11-25 23:19:11 +0000 | [diff] [blame] | 7323 | { |
Linus Walleij | a4e6bdc | 2008-05-23 22:31:53 +0000 | [diff] [blame] | 7324 | PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo; |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 7325 | uint32_t localph = metadata->parent_id; |
Linus Walleij | b7426d1 | 2006-11-25 23:19:11 +0000 | [diff] [blame] | 7326 | |
| 7327 | // Use a default folder if none given |
| 7328 | if (localph == 0) { |
Linus Walleij | afac36b | 2009-01-30 20:39:13 +0000 | [diff] [blame] | 7329 | if (device->default_playlist_folder != 0) |
| 7330 | localph = device->default_playlist_folder; |
| 7331 | else |
| 7332 | localph = device->default_music_folder; |
Linus Walleij | b7426d1 | 2006-11-25 23:19:11 +0000 | [diff] [blame] | 7333 | } |
Linus Walleij | 5ce59db | 2008-03-12 21:22:58 +0000 | [diff] [blame] | 7334 | metadata->parent_id = localph; |
Linus Walleij | b7426d1 | 2006-11-25 23:19:11 +0000 | [diff] [blame] | 7335 | |
Linus Walleij | f3c4405 | 2008-08-16 21:14:56 +0000 | [diff] [blame] | 7336 | // Samsung needs its own special type of playlists |
| 7337 | if(FLAG_PLAYLIST_SPL(ptp_usb)) { |
| 7338 | return playlist_t_to_spl(device, metadata); |
| 7339 | } |
| 7340 | |
Linus Walleij | b7426d1 | 2006-11-25 23:19:11 +0000 | [diff] [blame] | 7341 | // Just create a new abstract audio/video playlist... |
| 7342 | return create_new_abstract_list(device, |
| 7343 | metadata->name, |
Linus Walleij | adce4a5 | 2007-04-23 07:28:11 +0000 | [diff] [blame] | 7344 | NULL, |
| 7345 | NULL, |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 7346 | NULL, |
Linus Walleij | b7426d1 | 2006-11-25 23:19:11 +0000 | [diff] [blame] | 7347 | localph, |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 7348 | metadata->storage_id, |
Linus Walleij | b7426d1 | 2006-11-25 23:19:11 +0000 | [diff] [blame] | 7349 | PTP_OFC_MTP_AbstractAudioVideoPlaylist, |
Linus Walleij | a4e6bdc | 2008-05-23 22:31:53 +0000 | [diff] [blame] | 7350 | get_playlist_extension(ptp_usb), |
Linus Walleij | b7426d1 | 2006-11-25 23:19:11 +0000 | [diff] [blame] | 7351 | &metadata->playlist_id, |
| 7352 | metadata->tracks, |
| 7353 | metadata->no_tracks); |
| 7354 | } |
| 7355 | |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 7356 | /** |
| 7357 | * This routine updates a playlist based on the metadata |
| 7358 | * supplied. If the <code>tracks</code> field of the metadata |
| 7359 | * contains a track listing, these tracks will be added to the |
| 7360 | * playlist in place of those already present, i.e. the |
alistair_boyle | 5e5fcb7 | 2008-11-17 14:38:19 +0000 | [diff] [blame] | 7361 | * previous track listing will be deleted. For Samsung devices the |
| 7362 | * playlist id (metadata->playlist_id) is likely to change. |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 7363 | * @param device a pointer to the device to create the new playlist on. |
| 7364 | * @param metadata the metadata for the playlist to be updated. |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 7365 | * notice that the field <code>playlist_id</code> |
alistair_boyle | 5e5fcb7 | 2008-11-17 14:38:19 +0000 | [diff] [blame] | 7366 | * must contain the apropriate playlist ID. Playlist ID |
| 7367 | * be modified to a new playlist ID by the time the |
| 7368 | * function returns since edit-in-place is not always possible. |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 7369 | * @return 0 on success, any other value means failure. |
| 7370 | * @see LIBMTP_Create_New_Playlist() |
| 7371 | * @see LIBMTP_Delete_Object() |
| 7372 | */ |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 7373 | int LIBMTP_Update_Playlist(LIBMTP_mtpdevice_t *device, |
alistair_boyle | 5e5fcb7 | 2008-11-17 14:38:19 +0000 | [diff] [blame] | 7374 | LIBMTP_playlist_t * const metadata) |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 7375 | { |
Linus Walleij | f3c4405 | 2008-08-16 21:14:56 +0000 | [diff] [blame] | 7376 | |
| 7377 | // Samsung needs its own special type of playlists |
| 7378 | PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo; |
| 7379 | if(FLAG_PLAYLIST_SPL(ptp_usb)) { |
| 7380 | return update_spl_playlist(device, metadata); |
| 7381 | } |
| 7382 | |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 7383 | return update_abstract_list(device, |
| 7384 | metadata->name, |
| 7385 | NULL, |
| 7386 | NULL, |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 7387 | NULL, |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 7388 | metadata->playlist_id, |
| 7389 | PTP_OFC_MTP_AbstractAudioVideoPlaylist, |
| 7390 | metadata->tracks, |
| 7391 | metadata->no_tracks); |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 7392 | } |
Linus Walleij | aa4b075 | 2006-07-26 22:21:04 +0000 | [diff] [blame] | 7393 | |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 7394 | /** |
| 7395 | * This creates a new album metadata structure and allocates memory |
| 7396 | * for it. Notice that if you add strings to this structure they |
| 7397 | * will be freed by the corresponding <code>LIBMTP_destroy_album_t</code> |
| 7398 | * operation later, so be careful of using strdup() when assigning |
| 7399 | * strings. |
| 7400 | * |
| 7401 | * @return a pointer to the newly allocated metadata structure. |
| 7402 | * @see LIBMTP_destroy_album_t() |
| 7403 | */ |
| 7404 | LIBMTP_album_t *LIBMTP_new_album_t(void) |
| 7405 | { |
| 7406 | LIBMTP_album_t *new = (LIBMTP_album_t *) malloc(sizeof(LIBMTP_album_t)); |
| 7407 | if (new == NULL) { |
| 7408 | return NULL; |
| 7409 | } |
| 7410 | new->album_id = 0; |
Linus Walleij | 5ce59db | 2008-03-12 21:22:58 +0000 | [diff] [blame] | 7411 | new->parent_id = 0; |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 7412 | new->storage_id = 0; |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 7413 | new->name = NULL; |
Linus Walleij | adce4a5 | 2007-04-23 07:28:11 +0000 | [diff] [blame] | 7414 | new->artist = NULL; |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 7415 | new->composer = NULL; |
Linus Walleij | adce4a5 | 2007-04-23 07:28:11 +0000 | [diff] [blame] | 7416 | new->genre = NULL; |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 7417 | new->tracks = NULL; |
| 7418 | new->no_tracks = 0; |
| 7419 | new->next = NULL; |
| 7420 | return new; |
| 7421 | } |
| 7422 | |
| 7423 | /** |
| 7424 | * This recursively deletes the memory for an album structure |
| 7425 | * |
| 7426 | * @param album structure to destroy |
| 7427 | * @see LIBMTP_new_album_t() |
| 7428 | */ |
| 7429 | void LIBMTP_destroy_album_t(LIBMTP_album_t *album) |
| 7430 | { |
| 7431 | if (album == NULL) { |
| 7432 | return; |
| 7433 | } |
| 7434 | if (album->name != NULL) |
| 7435 | free(album->name); |
Linus Walleij | adce4a5 | 2007-04-23 07:28:11 +0000 | [diff] [blame] | 7436 | if (album->artist != NULL) |
| 7437 | free(album->artist); |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 7438 | if (album->composer != NULL) |
| 7439 | free(album->composer); |
Linus Walleij | adce4a5 | 2007-04-23 07:28:11 +0000 | [diff] [blame] | 7440 | if (album->genre != NULL) |
| 7441 | free(album->genre); |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 7442 | if (album->tracks != NULL) |
| 7443 | free(album->tracks); |
| 7444 | free(album); |
| 7445 | return; |
| 7446 | } |
| 7447 | |
| 7448 | /** |
| 7449 | * This function returns a list of the albums available on the |
| 7450 | * device. |
| 7451 | * |
| 7452 | * @param device a pointer to the device to get the album listing from. |
| 7453 | * @return an album list on success, else NULL. If there are no albums |
| 7454 | * on the device, NULL will be returned as well. |
| 7455 | * @see LIBMTP_Get_Album() |
| 7456 | */ |
| 7457 | LIBMTP_album_t *LIBMTP_Get_Album_List(LIBMTP_mtpdevice_t *device) |
| 7458 | { |
| 7459 | PTPParams *params = (PTPParams *) device->params; |
| 7460 | LIBMTP_album_t *retalbums = NULL; |
| 7461 | LIBMTP_album_t *curalbum = NULL; |
| 7462 | uint32_t i; |
| 7463 | |
| 7464 | // Get all the handles if we haven't already done that |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 7465 | if (params->nrofobjects == 0) { |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 7466 | flush_handles(device); |
| 7467 | } |
| 7468 | |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 7469 | for (i = 0; i < params->nrofobjects; i++) { |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 7470 | LIBMTP_album_t *alb; |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 7471 | PTPObject *ob; |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 7472 | uint16_t ret; |
| 7473 | |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 7474 | ob = ¶ms->objects[i]; |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 7475 | |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 7476 | // Ignore stuff that isn't an album |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 7477 | if ( ob->oi.ObjectFormat != PTP_OFC_MTP_AbstractAudioAlbum ) { |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 7478 | continue; |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 7479 | } |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 7480 | |
| 7481 | // Allocate a new album type |
| 7482 | alb = LIBMTP_new_album_t(); |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 7483 | alb->album_id = ob->oid; |
| 7484 | alb->parent_id = ob->oi.ParentObject; |
| 7485 | alb->storage_id = ob->oi.StorageID; |
Linus Walleij | 5ce59db | 2008-03-12 21:22:58 +0000 | [diff] [blame] | 7486 | |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 7487 | // Get metadata for it. |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 7488 | alb->name = get_string_from_object(device, ob->oid, PTP_OPC_Name); |
| 7489 | alb->artist = get_string_from_object(device, ob->oid, PTP_OPC_AlbumArtist); |
Linus Walleij | 0b75ab6 | 2007-12-28 00:47:34 +0000 | [diff] [blame] | 7490 | if (alb->artist == NULL) { |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 7491 | alb->artist = get_string_from_object(device, ob->oid, PTP_OPC_Artist); |
Linus Walleij | 0b75ab6 | 2007-12-28 00:47:34 +0000 | [diff] [blame] | 7492 | } |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 7493 | alb->composer = get_string_from_object(device, ob->oid, PTP_OPC_Composer); |
| 7494 | alb->genre = get_string_from_object(device, ob->oid, PTP_OPC_Genre); |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 7495 | |
| 7496 | // Then get the track listing for this album |
| 7497 | ret = ptp_mtp_getobjectreferences(params, alb->album_id, &alb->tracks, &alb->no_tracks); |
| 7498 | if (ret != PTP_RC_OK) { |
| 7499 | add_ptp_error_to_errorstack(device, ret, "LIBMTP_Get_Album_List(): Could not get object references."); |
| 7500 | alb->tracks = NULL; |
| 7501 | alb->no_tracks = 0; |
| 7502 | } |
| 7503 | |
| 7504 | // Add album to a list that will be returned afterwards. |
| 7505 | if (retalbums == NULL) { |
| 7506 | retalbums = alb; |
| 7507 | curalbum = alb; |
| 7508 | } else { |
| 7509 | curalbum->next = alb; |
| 7510 | curalbum = alb; |
| 7511 | } |
| 7512 | |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 7513 | } |
| 7514 | return retalbums; |
| 7515 | } |
| 7516 | |
| 7517 | /** |
| 7518 | * This function retrieves an individual album from the device. |
| 7519 | * @param device a pointer to the device to get the album from. |
| 7520 | * @param albid the unique ID of the album to retrieve. |
| 7521 | * @return a valid album metadata or NULL on failure. |
| 7522 | * @see LIBMTP_Get_Album_List() |
| 7523 | */ |
| 7524 | LIBMTP_album_t *LIBMTP_Get_Album(LIBMTP_mtpdevice_t *device, uint32_t const albid) |
| 7525 | { |
| 7526 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 7527 | uint16_t ret; |
| 7528 | PTPObject *ob; |
| 7529 | LIBMTP_album_t *alb; |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 7530 | |
| 7531 | // Get all the handles if we haven't already done that |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 7532 | if (params->nrofobjects == 0) { |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 7533 | flush_handles(device); |
| 7534 | } |
| 7535 | |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 7536 | ret = ptp_object_want (params, albid, PTPOBJECT_OBJECTINFO_LOADED, &ob); |
| 7537 | if (ret != PTP_RC_OK) |
| 7538 | return NULL; |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 7539 | |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 7540 | // Ignore stuff that isn't an album |
| 7541 | if ( ob->oi.ObjectFormat != PTP_OFC_MTP_AbstractAudioAlbum ) { |
| 7542 | return NULL; |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 7543 | } |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 7544 | |
| 7545 | // Allocate a new album type |
| 7546 | alb = LIBMTP_new_album_t(); |
| 7547 | alb->album_id = ob->oid; |
| 7548 | alb->parent_id = ob->oi.ParentObject; |
| 7549 | alb->storage_id = ob->oi.StorageID; |
| 7550 | |
| 7551 | // Get metadata for it. |
| 7552 | alb->name = get_string_from_object(device, ob->oid, PTP_OPC_Name); |
| 7553 | alb->artist = get_string_from_object(device, ob->oid, PTP_OPC_AlbumArtist); |
| 7554 | if (alb->artist == NULL) { |
| 7555 | alb->artist = get_string_from_object(device, ob->oid, PTP_OPC_Artist); |
| 7556 | } |
| 7557 | alb->composer = get_string_from_object(device, ob->oid, PTP_OPC_Composer); |
| 7558 | alb->genre = get_string_from_object(device, ob->oid, PTP_OPC_Genre); |
| 7559 | ret = ptp_mtp_getobjectreferences(params, alb->album_id, &alb->tracks, &alb->no_tracks); |
| 7560 | if (ret != PTP_RC_OK) { |
| 7561 | add_ptp_error_to_errorstack(device, ret, "LIBMTP_Get_Album: Could not get object references."); |
| 7562 | alb->tracks = NULL; |
| 7563 | alb->no_tracks = 0; |
| 7564 | } |
| 7565 | |
| 7566 | return alb; |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 7567 | } |
| 7568 | |
| 7569 | /** |
| 7570 | * This routine creates a new album based on the metadata |
| 7571 | * supplied. If the <code>tracks</code> field of the metadata |
| 7572 | * contains a track listing, these tracks will be added to the |
| 7573 | * album. |
| 7574 | * @param device a pointer to the device to create the new album on. |
| 7575 | * @param metadata the metadata for the new album. If the function |
| 7576 | * exits with success, the <code>album_id</code> field of this |
| 7577 | * struct will contain the new ID of the album. |
Linus Walleij | 2fe43e4 | 2009-01-30 20:31:26 +0000 | [diff] [blame] | 7578 | * <ul> |
| 7579 | * <li><code>metadata->parent_id</code> should be set to the parent |
| 7580 | * (e.g. folder) to store this track in. Since some |
| 7581 | * devices are a bit picky about where files |
| 7582 | * are placed, a default folder will be chosen if libmtp |
| 7583 | * has detected one for the current filetype and this |
| 7584 | * parameter is set to 0. If this is 0 and no default folder |
| 7585 | * can be found, the file will be stored in the root folder. |
| 7586 | * <li><code>metadata->storage_id</code> should be set to the |
| 7587 | * desired storage (e.g. memory card or whatever your device |
| 7588 | * presents) to store this track in. Setting this to 0 will store |
| 7589 | * the track on the primary storage. |
| 7590 | * </ul> |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 7591 | * @return 0 on success, any other value means failure. |
| 7592 | * @see LIBMTP_Update_Album() |
| 7593 | * @see LIBMTP_Delete_Object() |
| 7594 | */ |
| 7595 | int LIBMTP_Create_New_Album(LIBMTP_mtpdevice_t *device, |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 7596 | LIBMTP_album_t * const metadata) |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 7597 | { |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 7598 | uint32_t localph = metadata->parent_id; |
mopoke | 9614340 | 2006-10-30 04:37:26 +0000 | [diff] [blame] | 7599 | |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 7600 | // Use a default folder if none given |
| 7601 | if (localph == 0) { |
Linus Walleij | afac36b | 2009-01-30 20:39:13 +0000 | [diff] [blame] | 7602 | if (device->default_album_folder != 0) |
| 7603 | localph = device->default_album_folder; |
| 7604 | else |
| 7605 | localph = device->default_music_folder; |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 7606 | } |
Linus Walleij | 5ce59db | 2008-03-12 21:22:58 +0000 | [diff] [blame] | 7607 | metadata->parent_id = localph; |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 7608 | |
Linus Walleij | b7426d1 | 2006-11-25 23:19:11 +0000 | [diff] [blame] | 7609 | // Just create a new abstract album... |
| 7610 | return create_new_abstract_list(device, |
| 7611 | metadata->name, |
Linus Walleij | adce4a5 | 2007-04-23 07:28:11 +0000 | [diff] [blame] | 7612 | metadata->artist, |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 7613 | metadata->composer, |
Linus Walleij | adce4a5 | 2007-04-23 07:28:11 +0000 | [diff] [blame] | 7614 | metadata->genre, |
Linus Walleij | b7426d1 | 2006-11-25 23:19:11 +0000 | [diff] [blame] | 7615 | localph, |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 7616 | metadata->storage_id, |
Linus Walleij | b7426d1 | 2006-11-25 23:19:11 +0000 | [diff] [blame] | 7617 | PTP_OFC_MTP_AbstractAudioAlbum, |
| 7618 | ".alb", |
| 7619 | &metadata->album_id, |
| 7620 | metadata->tracks, |
| 7621 | metadata->no_tracks); |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 7622 | } |
| 7623 | |
| 7624 | /** |
rreardon | 5332f9c | 2006-12-05 10:18:23 +0000 | [diff] [blame] | 7625 | * This creates a new sample data metadata structure and allocates memory |
| 7626 | * for it. Notice that if you add strings to this structure they |
| 7627 | * will be freed by the corresponding <code>LIBMTP_destroy_sampledata_t</code> |
| 7628 | * operation later, so be careful of using strdup() when assigning |
| 7629 | * strings. |
| 7630 | * |
| 7631 | * @return a pointer to the newly allocated metadata structure. |
| 7632 | * @see LIBMTP_destroy_sampledata_t() |
| 7633 | */ |
| 7634 | LIBMTP_filesampledata_t *LIBMTP_new_filesampledata_t(void) |
| 7635 | { |
| 7636 | LIBMTP_filesampledata_t *new = (LIBMTP_filesampledata_t *) malloc(sizeof(LIBMTP_filesampledata_t)); |
| 7637 | if (new == NULL) { |
| 7638 | return NULL; |
| 7639 | } |
| 7640 | new->height=0; |
| 7641 | new->width = 0; |
| 7642 | new->data = NULL; |
| 7643 | new->duration = 0; |
| 7644 | new->size = 0; |
| 7645 | return new; |
| 7646 | } |
| 7647 | |
Linus Walleij | f1b02f2 | 2006-12-06 09:03:23 +0000 | [diff] [blame] | 7648 | /** |
| 7649 | * This destroys a file sample metadata type. |
| 7650 | * @param sample the file sample metadata to be destroyed. |
| 7651 | */ |
| 7652 | void LIBMTP_destroy_filesampledata_t(LIBMTP_filesampledata_t * sample) |
| 7653 | { |
| 7654 | if (sample == NULL) { |
| 7655 | return; |
| 7656 | } |
| 7657 | if (sample->data != NULL) { |
| 7658 | free(sample->data); |
| 7659 | } |
| 7660 | free(sample); |
| 7661 | } |
| 7662 | |
| 7663 | /** |
| 7664 | * This routine figures out whether a certain filetype supports |
| 7665 | * representative samples (small thumbnail images) or not. This |
| 7666 | * typically applies to JPEG files, MP3 files and Album abstract |
| 7667 | * playlists, but in theory any filetype could support representative |
| 7668 | * samples. |
| 7669 | * @param device a pointer to the device which is to be examined. |
Linus Walleij | 9ffe998 | 2008-01-20 13:42:52 +0000 | [diff] [blame] | 7670 | * @param filetype the fileype to examine, and return the representative sample |
Linus Walleij | f1b02f2 | 2006-12-06 09:03:23 +0000 | [diff] [blame] | 7671 | * properties for. |
| 7672 | * @param sample this will contain a new sample type with the fields |
| 7673 | * filled in with suitable default values. For example, the |
| 7674 | * supported sample type will be set, the supported height and |
| 7675 | * width will be set to max values if it is an image sample, |
| 7676 | * and duration will also be given some suitable default value |
| 7677 | * which should not be exceeded on audio samples. If the |
| 7678 | * device does not support samples for this filetype, this |
| 7679 | * pointer will be NULL. If it is not NULL, the user must |
| 7680 | * destroy this struct with <code>LIBMTP_destroy_filesampledata_t()</code> |
| 7681 | * after use. |
| 7682 | * @return 0 on success, any other value means failure. |
| 7683 | * @see LIBMTP_Send_Representative_Sample() |
| 7684 | * @see LIBMTP_Create_New_Album() |
| 7685 | */ |
| 7686 | int LIBMTP_Get_Representative_Sample_Format(LIBMTP_mtpdevice_t *device, |
| 7687 | LIBMTP_filetype_t const filetype, |
| 7688 | LIBMTP_filesampledata_t ** sample) |
| 7689 | { |
| 7690 | uint16_t ret; |
| 7691 | PTPParams *params = (PTPParams *) device->params; |
| 7692 | uint16_t *props = NULL; |
| 7693 | uint32_t propcnt = 0; |
| 7694 | int i; |
| 7695 | // TODO: Get rid of these when we can properly query the device. |
| 7696 | int support_data = 0; |
| 7697 | int support_format = 0; |
| 7698 | int support_height = 0; |
| 7699 | int support_width = 0; |
| 7700 | int support_duration = 0; |
Richard Low | 6f07084 | 2009-05-03 10:26:16 +0000 | [diff] [blame] | 7701 | int support_size = 0; |
Linus Walleij | f1b02f2 | 2006-12-06 09:03:23 +0000 | [diff] [blame] | 7702 | |
rreardon | 21a3a9d | 2007-08-15 10:56:31 +0000 | [diff] [blame] | 7703 | PTPObjectPropDesc opd_height; |
| 7704 | PTPObjectPropDesc opd_width; |
| 7705 | PTPObjectPropDesc opd_format; |
| 7706 | PTPObjectPropDesc opd_duration; |
Richard Low | 6f07084 | 2009-05-03 10:26:16 +0000 | [diff] [blame] | 7707 | PTPObjectPropDesc opd_size; |
rreardon | 21a3a9d | 2007-08-15 10:56:31 +0000 | [diff] [blame] | 7708 | |
Linus Walleij | f1b02f2 | 2006-12-06 09:03:23 +0000 | [diff] [blame] | 7709 | // Default to no type supported. |
| 7710 | *sample = NULL; |
| 7711 | |
| 7712 | ret = ptp_mtp_getobjectpropssupported(params, map_libmtp_type_to_ptp_type(filetype), &propcnt, &props); |
| 7713 | if (ret != PTP_RC_OK) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 7714 | 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] | 7715 | return -1; |
| 7716 | } |
| 7717 | /* |
| 7718 | * TODO: when walking through these object properties, make calls to |
| 7719 | * a new function in ptp.h/ptp.c that can send the command |
| 7720 | * PTP_OC_MTP_GetObjectPropDesc to get max/min values of the properties |
| 7721 | * supported. |
| 7722 | */ |
| 7723 | for (i = 0; i < propcnt; i++) { |
| 7724 | switch(props[i]) { |
| 7725 | case PTP_OPC_RepresentativeSampleData: |
| 7726 | support_data = 1; |
| 7727 | break; |
| 7728 | case PTP_OPC_RepresentativeSampleFormat: |
| 7729 | support_format = 1; |
| 7730 | break; |
| 7731 | case PTP_OPC_RepresentativeSampleSize: |
Richard Low | 6f07084 | 2009-05-03 10:26:16 +0000 | [diff] [blame] | 7732 | support_size = 1; |
Linus Walleij | f1b02f2 | 2006-12-06 09:03:23 +0000 | [diff] [blame] | 7733 | break; |
| 7734 | case PTP_OPC_RepresentativeSampleHeight: |
| 7735 | support_height = 1; |
| 7736 | break; |
| 7737 | case PTP_OPC_RepresentativeSampleWidth: |
| 7738 | support_width = 1; |
| 7739 | break; |
| 7740 | case PTP_OPC_RepresentativeSampleDuration: |
| 7741 | support_duration = 1; |
| 7742 | break; |
| 7743 | default: |
| 7744 | break; |
| 7745 | } |
| 7746 | } |
| 7747 | free(props); |
Richard Low | 6f07084 | 2009-05-03 10:26:16 +0000 | [diff] [blame] | 7748 | |
Linus Walleij | f1b02f2 | 2006-12-06 09:03:23 +0000 | [diff] [blame] | 7749 | if (support_data && support_format && support_height && support_width && !support_duration) { |
| 7750 | // Something that supports height and width and not duration is likely to be JPEG |
| 7751 | LIBMTP_filesampledata_t *retsam = LIBMTP_new_filesampledata_t(); |
rreardon | 21a3a9d | 2007-08-15 10:56:31 +0000 | [diff] [blame] | 7752 | /* |
| 7753 | * Populate the sample format with the first supported format |
| 7754 | * |
| 7755 | * TODO: figure out how to pass back more than one format if more are |
| 7756 | * supported by the device. |
| 7757 | */ |
| 7758 | ptp_mtp_getobjectpropdesc (params, PTP_OPC_RepresentativeSampleFormat, map_libmtp_type_to_ptp_type(filetype), &opd_format); |
| 7759 | retsam->filetype = map_ptp_type_to_libmtp_type(opd_format.FORM.Enum.SupportedValue[0].u16); |
Richard Low | 6f07084 | 2009-05-03 10:26:16 +0000 | [diff] [blame] | 7760 | ptp_free_objectpropdesc(&opd_format); |
rreardon | 21a3a9d | 2007-08-15 10:56:31 +0000 | [diff] [blame] | 7761 | /* Populate the maximum image height */ |
| 7762 | ptp_mtp_getobjectpropdesc (params, PTP_OPC_RepresentativeSampleWidth, map_libmtp_type_to_ptp_type(filetype), &opd_width); |
| 7763 | retsam->width = opd_width.FORM.Range.MaximumValue.u32; |
Richard Low | 6f07084 | 2009-05-03 10:26:16 +0000 | [diff] [blame] | 7764 | ptp_free_objectpropdesc(&opd_width); |
rreardon | 21a3a9d | 2007-08-15 10:56:31 +0000 | [diff] [blame] | 7765 | /* Populate the maximum image width */ |
| 7766 | ptp_mtp_getobjectpropdesc (params, PTP_OPC_RepresentativeSampleHeight, map_libmtp_type_to_ptp_type(filetype), &opd_height); |
| 7767 | retsam->height = opd_height.FORM.Range.MaximumValue.u32; |
Richard Low | 6f07084 | 2009-05-03 10:26:16 +0000 | [diff] [blame] | 7768 | ptp_free_objectpropdesc(&opd_height); |
| 7769 | /* Populate the maximum size */ |
| 7770 | if (support_size) { |
| 7771 | ptp_mtp_getobjectpropdesc (params, PTP_OPC_RepresentativeSampleSize, map_libmtp_type_to_ptp_type(filetype), &opd_size); |
| 7772 | retsam->size = opd_size.FORM.Range.MaximumValue.u32; |
| 7773 | ptp_free_objectpropdesc(&opd_size); |
| 7774 | } |
Linus Walleij | f1b02f2 | 2006-12-06 09:03:23 +0000 | [diff] [blame] | 7775 | *sample = retsam; |
| 7776 | } else if (support_data && support_format && !support_height && !support_width && support_duration) { |
| 7777 | // Another qualified guess |
| 7778 | LIBMTP_filesampledata_t *retsam = LIBMTP_new_filesampledata_t(); |
rreardon | 21a3a9d | 2007-08-15 10:56:31 +0000 | [diff] [blame] | 7779 | /* |
| 7780 | * Populate the sample format with the first supported format |
| 7781 | * |
| 7782 | * TODO: figure out how to pass back more than one format if more are |
| 7783 | * supported by the device. |
| 7784 | */ |
| 7785 | ptp_mtp_getobjectpropdesc (params, PTP_OPC_RepresentativeSampleFormat, map_libmtp_type_to_ptp_type(filetype), &opd_format); |
| 7786 | retsam->filetype = map_ptp_type_to_libmtp_type(opd_format.FORM.Enum.SupportedValue[0].u16); |
Richard Low | 6f07084 | 2009-05-03 10:26:16 +0000 | [diff] [blame] | 7787 | ptp_free_objectpropdesc(&opd_format); |
| 7788 | /* Populate the maximum duration */ |
rreardon | 21a3a9d | 2007-08-15 10:56:31 +0000 | [diff] [blame] | 7789 | ptp_mtp_getobjectpropdesc (params, PTP_OPC_RepresentativeSampleDuration, map_libmtp_type_to_ptp_type(filetype), &opd_duration); |
| 7790 | retsam->duration = opd_duration.FORM.Range.MaximumValue.u32; |
Richard Low | 6f07084 | 2009-05-03 10:26:16 +0000 | [diff] [blame] | 7791 | ptp_free_objectpropdesc(&opd_duration); |
| 7792 | /* Populate the maximum size */ |
| 7793 | if (support_size) { |
| 7794 | ptp_mtp_getobjectpropdesc (params, PTP_OPC_RepresentativeSampleSize, map_libmtp_type_to_ptp_type(filetype), &opd_size); |
| 7795 | retsam->size = opd_size.FORM.Range.MaximumValue.u32; |
| 7796 | ptp_free_objectpropdesc(&opd_size); |
| 7797 | } |
Linus Walleij | f1b02f2 | 2006-12-06 09:03:23 +0000 | [diff] [blame] | 7798 | *sample = retsam; |
| 7799 | } |
| 7800 | return 0; |
| 7801 | } |
rreardon | 5332f9c | 2006-12-05 10:18:23 +0000 | [diff] [blame] | 7802 | |
| 7803 | /** |
| 7804 | * This routine sends representative sample data for an object. |
| 7805 | * This uses the RepresentativeSampleData property of the album, |
| 7806 | * if the device supports it. The data should be of a format acceptable |
| 7807 | * to the player (for iRiver and Creative, this seems to be JPEG) and |
| 7808 | * must not be too large. (for a Creative, max seems to be about 20KB.) |
Richard Low | 6f07084 | 2009-05-03 10:26:16 +0000 | [diff] [blame] | 7809 | * Check by calling LIBMTP_Get_Representative_Sample_Format() to get |
| 7810 | * maximum size, dimensions, etc.. |
Linus Walleij | 7e3f276 | 2006-12-03 22:52:05 +0000 | [diff] [blame] | 7811 | * @param device a pointer to the device which the object is on. |
| 7812 | * @param id unique id of the object to set artwork for. |
Richard Low | 36e447c | 2008-01-20 15:24:55 +0000 | [diff] [blame] | 7813 | * @param pointer to LIBMTP_filesampledata_t struct containing data |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 7814 | * @return 0 on success, any other value means failure. |
Richard Low | 36e447c | 2008-01-20 15:24:55 +0000 | [diff] [blame] | 7815 | * @see LIBMTP_Get_Representative_Sample() |
Linus Walleij | f1b02f2 | 2006-12-06 09:03:23 +0000 | [diff] [blame] | 7816 | * @see LIBMTP_Get_Representative_Sample_Format() |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 7817 | * @see LIBMTP_Create_New_Album() |
| 7818 | */ |
Linus Walleij | 7e3f276 | 2006-12-03 22:52:05 +0000 | [diff] [blame] | 7819 | int LIBMTP_Send_Representative_Sample(LIBMTP_mtpdevice_t *device, |
rreardon | 5332f9c | 2006-12-05 10:18:23 +0000 | [diff] [blame] | 7820 | uint32_t const id, |
| 7821 | LIBMTP_filesampledata_t *sampledata) |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 7822 | { |
| 7823 | uint16_t ret; |
| 7824 | PTPParams *params = (PTPParams *) device->params; |
Richard Low | bb9fb4a | 2008-05-18 14:49:34 +0000 | [diff] [blame] | 7825 | PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo; |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 7826 | PTPPropertyValue propval; |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 7827 | PTPObject *ob; |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 7828 | uint32_t i; |
Linus Walleij | f1b02f2 | 2006-12-06 09:03:23 +0000 | [diff] [blame] | 7829 | uint16_t *props = NULL; |
| 7830 | uint32_t propcnt = 0; |
| 7831 | int supported = 0; |
rreardon | 5332f9c | 2006-12-05 10:18:23 +0000 | [diff] [blame] | 7832 | |
| 7833 | // get the file format for the object we're going to send representative data for |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 7834 | ret = ptp_object_want (params, id, PTPOBJECT_OBJECTINFO_LOADED, &ob); |
| 7835 | if (ret != PTP_RC_OK) { |
Linus Walleij | f0bf437 | 2007-07-01 21:47:38 +0000 | [diff] [blame] | 7836 | 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] | 7837 | return -1; |
| 7838 | } |
| 7839 | |
| 7840 | // check that we can send representative sample data for this object format |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 7841 | ret = ptp_mtp_getobjectpropssupported(params, ob->oi.ObjectFormat, &propcnt, &props); |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 7842 | if (ret != PTP_RC_OK) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 7843 | 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] | 7844 | return -1; |
| 7845 | } |
Linus Walleij | f1b02f2 | 2006-12-06 09:03:23 +0000 | [diff] [blame] | 7846 | |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 7847 | for (i = 0; i < propcnt; i++) { |
Linus Walleij | f1b02f2 | 2006-12-06 09:03:23 +0000 | [diff] [blame] | 7848 | if (props[i] == PTP_OPC_RepresentativeSampleData) { |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 7849 | supported = 1; |
Linus Walleij | f1b02f2 | 2006-12-06 09:03:23 +0000 | [diff] [blame] | 7850 | break; |
| 7851 | } |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 7852 | } |
| 7853 | if (!supported) { |
Linus Walleij | f1b02f2 | 2006-12-06 09:03:23 +0000 | [diff] [blame] | 7854 | free(props); |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 7855 | 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] | 7856 | return -1; |
| 7857 | } |
Linus Walleij | f1b02f2 | 2006-12-06 09:03:23 +0000 | [diff] [blame] | 7858 | free(props); |
rreardon | 5332f9c | 2006-12-05 10:18:23 +0000 | [diff] [blame] | 7859 | |
Linus Walleij | f1b02f2 | 2006-12-06 09:03:23 +0000 | [diff] [blame] | 7860 | // Go ahead and send the data |
rreardon | 5332f9c | 2006-12-05 10:18:23 +0000 | [diff] [blame] | 7861 | propval.a.count = sampledata->size; |
| 7862 | propval.a.v = malloc(sizeof(PTPPropertyValue) * sampledata->size); |
| 7863 | for (i = 0; i < sampledata->size; i++) { |
| 7864 | propval.a.v[i].u8 = sampledata->data[i]; |
Linus Walleij | 7e3f276 | 2006-12-03 22:52:05 +0000 | [diff] [blame] | 7865 | } |
| 7866 | |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 7867 | ret = ptp_mtp_setobjectpropvalue(params,id,PTP_OPC_RepresentativeSampleData, |
Linus Walleij | f1b02f2 | 2006-12-06 09:03:23 +0000 | [diff] [blame] | 7868 | &propval,PTP_DTC_AUINT8); |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 7869 | if (ret != PTP_RC_OK) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 7870 | 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] | 7871 | free(propval.a.v); |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 7872 | return -1; |
| 7873 | } |
Linus Walleij | 7e3f276 | 2006-12-03 22:52:05 +0000 | [diff] [blame] | 7874 | free(propval.a.v); |
rreardon | d2ddb63 | 2006-12-12 12:13:21 +0000 | [diff] [blame] | 7875 | |
| 7876 | /* Set the height and width if the sample is an image, otherwise just |
| 7877 | * set the duration and size */ |
| 7878 | switch(sampledata->filetype) { |
Linus Walleij | e1ac07e | 2006-12-14 19:38:59 +0000 | [diff] [blame] | 7879 | case LIBMTP_FILETYPE_JPEG: |
| 7880 | case LIBMTP_FILETYPE_JFIF: |
| 7881 | case LIBMTP_FILETYPE_TIFF: |
| 7882 | case LIBMTP_FILETYPE_BMP: |
| 7883 | case LIBMTP_FILETYPE_GIF: |
| 7884 | case LIBMTP_FILETYPE_PICT: |
| 7885 | case LIBMTP_FILETYPE_PNG: |
Linus Walleij | fec4d56 | 2008-06-01 22:30:36 +0000 | [diff] [blame] | 7886 | if (!FLAG_BROKEN_SET_SAMPLE_DIMENSIONS(ptp_usb)) { |
Richard Low | bb9fb4a | 2008-05-18 14:49:34 +0000 | [diff] [blame] | 7887 | // For images, set the height and width |
| 7888 | set_object_u32(device, id, PTP_OPC_RepresentativeSampleHeight, sampledata->height); |
| 7889 | set_object_u32(device, id, PTP_OPC_RepresentativeSampleWidth, sampledata->width); |
| 7890 | } |
Linus Walleij | e1ac07e | 2006-12-14 19:38:59 +0000 | [diff] [blame] | 7891 | break; |
| 7892 | default: |
| 7893 | // For anything not an image, set the duration and size |
| 7894 | set_object_u32(device, id, PTP_OPC_RepresentativeSampleDuration, sampledata->duration); |
| 7895 | set_object_u32(device, id, PTP_OPC_RepresentativeSampleSize, sampledata->size); |
| 7896 | break; |
rreardon | d2ddb63 | 2006-12-12 12:13:21 +0000 | [diff] [blame] | 7897 | } |
rreardon | d2ddb63 | 2006-12-12 12:13:21 +0000 | [diff] [blame] | 7898 | |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 7899 | return 0; |
| 7900 | } |
| 7901 | |
| 7902 | /** |
Richard Low | 36e447c | 2008-01-20 15:24:55 +0000 | [diff] [blame] | 7903 | * This routine gets representative sample data for an object. |
| 7904 | * This uses the RepresentativeSampleData property of the album, |
| 7905 | * if the device supports it. |
| 7906 | * @param device a pointer to the device which the object is on. |
| 7907 | * @param id unique id of the object to get data for. |
| 7908 | * @param pointer to LIBMTP_filesampledata_t struct to receive data |
| 7909 | * @return 0 on success, any other value means failure. |
| 7910 | * @see LIBMTP_Send_Representative_Sample() |
| 7911 | * @see LIBMTP_Get_Representative_Sample_Format() |
| 7912 | * @see LIBMTP_Create_New_Album() |
| 7913 | */ |
| 7914 | int LIBMTP_Get_Representative_Sample(LIBMTP_mtpdevice_t *device, |
| 7915 | uint32_t const id, |
| 7916 | LIBMTP_filesampledata_t *sampledata) |
| 7917 | { |
| 7918 | uint16_t ret; |
| 7919 | PTPParams *params = (PTPParams *) device->params; |
| 7920 | PTPPropertyValue propval; |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 7921 | PTPObject *ob; |
Richard Low | 36e447c | 2008-01-20 15:24:55 +0000 | [diff] [blame] | 7922 | uint32_t i; |
| 7923 | uint16_t *props = NULL; |
| 7924 | uint32_t propcnt = 0; |
| 7925 | int supported = 0; |
| 7926 | |
| 7927 | // get the file format for the object we're going to send representative data for |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 7928 | ret = ptp_object_want (params, id, PTPOBJECT_OBJECTINFO_LOADED, &ob); |
| 7929 | if (ret != PTP_RC_OK) { |
Richard Low | 36e447c | 2008-01-20 15:24:55 +0000 | [diff] [blame] | 7930 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Get_Representative_Sample(): could not get object info."); |
| 7931 | return -1; |
| 7932 | } |
| 7933 | |
| 7934 | // check that we can store representative sample data for this object format |
Linus Walleij | d463750 | 2009-06-14 23:03:33 +0000 | [diff] [blame] | 7935 | ret = ptp_mtp_getobjectpropssupported(params, ob->oi.ObjectFormat, &propcnt, &props); |
Richard Low | 36e447c | 2008-01-20 15:24:55 +0000 | [diff] [blame] | 7936 | if (ret != PTP_RC_OK) { |
| 7937 | add_ptp_error_to_errorstack(device, ret, "LIBMTP_Get_Representative_Sample(): could not get object properties."); |
| 7938 | return -1; |
| 7939 | } |
| 7940 | |
| 7941 | for (i = 0; i < propcnt; i++) { |
| 7942 | if (props[i] == PTP_OPC_RepresentativeSampleData) { |
| 7943 | supported = 1; |
| 7944 | break; |
| 7945 | } |
| 7946 | } |
| 7947 | if (!supported) { |
| 7948 | free(props); |
| 7949 | add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Get_Representative_Sample(): object type doesn't support RepresentativeSampleData."); |
| 7950 | return -1; |
| 7951 | } |
| 7952 | free(props); |
| 7953 | |
| 7954 | // Get the data |
| 7955 | ret = ptp_mtp_getobjectpropvalue(params,id,PTP_OPC_RepresentativeSampleData, |
| 7956 | &propval,PTP_DTC_AUINT8); |
| 7957 | if (ret != PTP_RC_OK) { |
| 7958 | add_ptp_error_to_errorstack(device, ret, "LIBMTP_Get_Representative_Sample(): could not get sample data."); |
| 7959 | return -1; |
| 7960 | } |
| 7961 | |
| 7962 | // Store it |
| 7963 | sampledata->size = propval.a.count; |
| 7964 | sampledata->data = malloc(sizeof(PTPPropertyValue) * propval.a.count); |
| 7965 | for (i = 0; i < propval.a.count; i++) { |
| 7966 | sampledata->data[i] = propval.a.v[i].u8; |
| 7967 | } |
| 7968 | free(propval.a.v); |
| 7969 | |
| 7970 | // Get the other properties |
| 7971 | sampledata->width = get_u32_from_object(device, id, PTP_OPC_RepresentativeSampleWidth, 0); |
| 7972 | sampledata->height = get_u32_from_object(device, id, PTP_OPC_RepresentativeSampleHeight, 0); |
| 7973 | sampledata->duration = get_u32_from_object(device, id, PTP_OPC_RepresentativeSampleDuration, 0); |
| 7974 | sampledata->filetype = map_ptp_type_to_libmtp_type( |
| 7975 | get_u16_from_object(device, id, PTP_OPC_RepresentativeSampleFormat, LIBMTP_FILETYPE_UNKNOWN)); |
| 7976 | |
| 7977 | return 0; |
| 7978 | } |
| 7979 | |
| 7980 | /** |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 7981 | * This routine updates an album based on the metadata |
| 7982 | * supplied. If the <code>tracks</code> field of the metadata |
| 7983 | * contains a track listing, these tracks will be added to the |
| 7984 | * album in place of those already present, i.e. the |
| 7985 | * previous track listing will be deleted. |
| 7986 | * @param device a pointer to the device to create the new album on. |
| 7987 | * @param metadata the metadata for the album to be updated. |
| 7988 | * notice that the field <code>album_id</code> |
| 7989 | * must contain the apropriate album ID. |
| 7990 | * @return 0 on success, any other value means failure. |
| 7991 | * @see LIBMTP_Create_New_Album() |
| 7992 | * @see LIBMTP_Delete_Object() |
| 7993 | */ |
| 7994 | int LIBMTP_Update_Album(LIBMTP_mtpdevice_t *device, |
| 7995 | LIBMTP_album_t const * const metadata) |
| 7996 | { |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 7997 | return update_abstract_list(device, |
| 7998 | metadata->name, |
| 7999 | metadata->artist, |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 8000 | metadata->composer, |
Linus Walleij | 7783b9b | 2007-09-22 22:10:44 +0000 | [diff] [blame] | 8001 | metadata->genre, |
| 8002 | metadata->album_id, |
| 8003 | PTP_OFC_MTP_AbstractAudioAlbum, |
| 8004 | metadata->tracks, |
| 8005 | metadata->no_tracks); |
Linus Walleij | 0c33ec0 | 2006-10-27 10:15:40 +0000 | [diff] [blame] | 8006 | } |
Linus Walleij | aa4b075 | 2006-07-26 22:21:04 +0000 | [diff] [blame] | 8007 | |
| 8008 | /** |
| 8009 | * Dummy function needed to interface to upstream |
| 8010 | * ptp.c/ptp.h files. |
| 8011 | */ |
| 8012 | void ptp_nikon_getptpipguid (unsigned char* guid) { |
| 8013 | return; |
| 8014 | } |
tsaarni | a3eb60a | 2007-07-06 17:41:30 +0000 | [diff] [blame] | 8015 | |
| 8016 | /** |
Linus Walleij | 7752b95 | 2007-10-19 20:11:46 +0000 | [diff] [blame] | 8017 | * Add an object to cache. |
| 8018 | * @param device the device which may have a cache to which the object should be added. |
| 8019 | * @param object_id the object to add to the cache. |
tsaarni | a3eb60a | 2007-07-06 17:41:30 +0000 | [diff] [blame] | 8020 | */ |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 8021 | 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] | 8022 | { |
| 8023 | PTPParams *params = (PTPParams *)device->params; |
tsaarni | a3eb60a | 2007-07-06 17:41:30 +0000 | [diff] [blame] | 8024 | uint16_t ret; |
tsaarni | a3eb60a | 2007-07-06 17:41:30 +0000 | [diff] [blame] | 8025 | |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 8026 | ret = ptp_add_object_to_cache(params, object_id); |
| 8027 | if (ret != PTP_RC_OK) { |
| 8028 | 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] | 8029 | } |
| 8030 | } |
| 8031 | |
| 8032 | |
| 8033 | /** |
| 8034 | * Update cache after object has been modified |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 8035 | * @param device the device which may have a cache to which the object should be updated. |
| 8036 | * @param object_id the object to update. |
tsaarni | a3eb60a | 2007-07-06 17:41:30 +0000 | [diff] [blame] | 8037 | */ |
Linus Walleij | 7752b95 | 2007-10-19 20:11:46 +0000 | [diff] [blame] | 8038 | static void update_metadata_cache(LIBMTP_mtpdevice_t *device, uint32_t object_id) |
tsaarni | a3eb60a | 2007-07-06 17:41:30 +0000 | [diff] [blame] | 8039 | { |
Linus Walleij | a3d0eaa | 2007-11-18 22:00:48 +0000 | [diff] [blame] | 8040 | PTPParams *params = (PTPParams *)device->params; |
| 8041 | |
| 8042 | ptp_remove_object_from_cache(params, object_id); |
Linus Walleij | a6d0d48 | 2007-10-31 08:54:56 +0000 | [diff] [blame] | 8043 | add_object_to_cache(device, object_id); |
tsaarni | a3eb60a | 2007-07-06 17:41:30 +0000 | [diff] [blame] | 8044 | } |