blob: 0f091388c559418fd71834beb0a46247c8cf0d35 [file] [log] [blame]
Linus Walleij10c58422006-06-02 08:51:53 +00001/**
2 * \file libmtp.c
Linus Walleij2f45d222007-02-02 22:47:39 +00003 *
Linus Walleija8b88892011-03-03 19:58:26 +01004 * Copyright (C) 2005-2011 Linus Walleij <triad@df.lth.se>
Richard Low36e447c2008-01-20 15:24:55 +00005 * Copyright (C) 2005-2008 Richard A. Low <richard@wentnet.com>
Linus Walleij9521e2b2007-07-10 21:50:42 +00006 * Copyright (C) 2007 Ted Bullock <tbullock@canada.com>
7 * Copyright (C) 2007 Tero Saarni <tero.saarni@gmail.com>
Linus Walleijde8193f2008-01-27 14:55:31 +00008 * Copyright (C) 2008 Florent Mertens <flomertens@gmail.com>
Linus Walleij2f45d222007-02-02 22:47:39 +00009 *
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 Walleij10c58422006-06-02 08:51:53 +000025 * 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 *
mopoke96143402006-10-30 04:37:26 +000031 * The three PTP files (ptp.c, ptp.h and ptp-pack.c) are
Linus Walleij10c58422006-06-02 08:51:53 +000032 * 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 Walleijc6d7c982009-05-10 13:24:36 +000037#include "config.h"
Linus Walleijeb8c6fe2006-02-03 09:46:22 +000038#include "libmtp.h"
Linus Walleijb9256fd2006-02-15 09:40:43 +000039#include "unicode.h"
Linus Walleij394bbbe2006-02-22 16:10:53 +000040#include "ptp.h"
Linus Walleij7cf02642011-11-16 23:44:58 +010041#include "libusb-glue.h"
Linus Walleij3e667ae2007-10-29 23:29:39 +000042#include "device-flags.h"
Linus Walleijf3c44052008-08-16 21:14:56 +000043#include "playlist-spl.h"
Linus Walleij8f6e0d92009-10-10 21:39:44 +000044#include "util.h"
Linus Walleijeb8c6fe2006-02-03 09:46:22 +000045
Sajid Anwar8dca41d2012-08-18 20:36:13 +020046#include "mtpz.h"
47
Marcin Niestroj718b2902017-06-10 16:36:18 +020048#include <stdarg.h>
Linus Walleij2eaaff02009-01-15 21:30:36 +000049#include <stdlib.h>
Catalin Patuleada25de02012-07-19 00:57:42 -040050#include <limits.h>
Linus Walleij2eaaff02009-01-15 21:30:36 +000051#include <unistd.h>
Linus Walleij7beba572006-11-29 08:56:12 +000052#include <string.h>
53#include <sys/types.h>
54#include <sys/stat.h>
55#include <fcntl.h>
Linus Walleij4126b302007-09-22 22:13:55 +000056#include <time.h>
Linus Walleijf3c44052008-08-16 21:14:56 +000057#include <errno.h>
Linus Walleij7beba572006-11-29 08:56:12 +000058#ifdef _MSC_VER // For MSVC++
59#define USE_WINDOWS_IO_H
60#include <io.h>
61#endif
62
Linus Walleij7cf02642011-11-16 23:44:58 +010063
nicklas79daadbf22009-09-28 18:19:34 +000064/**
65 * Global debug level
66 * We use a flag system to enable a part of logs.
Catalin Patuleada25de02012-07-19 00:57:42 -040067 *
68 * The LIBMTP_DEBUG environment variable sets the debug flags for any binary
69 * that uses libmtp and calls LIBMTP_Init. The value can be given in decimal
70 * (must not start with "0" or it will be interpreted in octal), or in
71 * hexadecimal (must start with "0x").
72 *
73 * The value "-1" enables all debug flags.
74 *
75 * Some of the utilities in examples/ also take a command-line flag "-d" that
Catalin Patulea406d5f92012-07-20 19:00:24 -040076 * enables LIBMTP_DEBUG_PTP and LIBMTP_DEBUG_DATA (same as setting
77 * LIBMTP_DEBUG=9).
Catalin Patuleada25de02012-07-19 00:57:42 -040078 *
79 * Flags (combine by adding the hex values):
Catalin Patulea406d5f92012-07-20 19:00:24 -040080 * 0x00 [0000 0000] : LIBMTP_DEBUG_NONE : no debug (default)
81 * 0x01 [0000 0001] : LIBMTP_DEBUG_PTP : PTP debug
82 * 0x02 [0000 0010] : LIBMTP_DEBUG_PLST : Playlist debug
83 * 0x04 [0000 0100] : LIBMTP_DEBUG_USB : USB debug
84 * 0x08 [0000 1000] : LIBMTP_DEBUG_DATA : USB data debug
85 *
86 * (Please keep this list in sync with libmtp.h.)
nicklas79daadbf22009-09-28 18:19:34 +000087 */
nicklas7903584082009-09-28 18:20:16 +000088int LIBMTP_debug = LIBMTP_DEBUG_NONE;
nicklas79daadbf22009-09-28 18:19:34 +000089
Linus Walleijd1e14e02008-12-14 01:07:30 +000090
Linus Walleijc86afbd2006-05-04 19:05:24 +000091/*
92 * This is a mapping between libmtp internal MTP filetypes and
93 * the libgphoto2/PTP equivalent defines. We need this because
94 * otherwise the libmtp.h device has to be dependent on ptp.h
95 * to be installed too, and we don't want that.
96 */
Linus Walleijcf42f452006-11-28 08:32:49 +000097//typedef struct filemap_struct filemap_t;
98typedef struct filemap_struct {
raveloxd9a28642006-05-26 23:42:22 +000099 char *description; /**< Text description for the file type */
100 LIBMTP_filetype_t id; /**< LIBMTP internal type for the file type */
101 uint16_t ptp_id; /**< PTP ID for the filetype */
Linus Walleijcf42f452006-11-28 08:32:49 +0000102 struct filemap_struct *next;
103} filemap_t;
Linus Walleijc86afbd2006-05-04 19:05:24 +0000104
Richard Low6f070842009-05-03 10:26:16 +0000105/*
106 * This is a mapping between libmtp internal MTP properties and
107 * the libgphoto2/PTP equivalent defines. We need this because
108 * otherwise the libmtp.h device has to be dependent on ptp.h
109 * to be installed too, and we don't want that.
110 */
111typedef struct propertymap_struct {
112 char *description; /**< Text description for the property */
113 LIBMTP_property_t id; /**< LIBMTP internal type for the property */
114 uint16_t ptp_id; /**< PTP ID for the property */
115 struct propertymap_struct *next;
116} propertymap_t;
117
Philip Langdale0a576512016-04-09 14:22:25 -0700118/*
119 * This is a simple container for holding our callback and user_data
120 * for parsing onwards to the usb_event_async function.
121 */
122typedef struct event_cb_data_struct {
123 LIBMTP_event_cb_fn cb;
124 void *user_data;
125} event_cb_data_t;
126
Linus Walleijf67bca92006-05-29 09:33:39 +0000127// Global variables
Linus Walleij438bd7f2006-06-08 11:35:44 +0000128// This holds the global filetype mapping table
Lei Zhang81732132013-09-04 17:27:35 -0700129static filemap_t *g_filemap = NULL;
Richard Low6f070842009-05-03 10:26:16 +0000130// This holds the global property mapping table
Lei Zhang81732132013-09-04 17:27:35 -0700131static propertymap_t *g_propertymap = NULL;
Linus Walleijf67bca92006-05-29 09:33:39 +0000132
Linus Walleijcf42f452006-11-28 08:32:49 +0000133/*
134 * Forward declarations of local (static) functions.
135 */
136static int register_filetype(char const * const description, LIBMTP_filetype_t const id,
137 uint16_t const ptp_id);
Linus Walleij7beba572006-11-29 08:56:12 +0000138static void init_filemap();
Richard Low6f070842009-05-03 10:26:16 +0000139static int register_property(char const * const description, LIBMTP_property_t const id,
140 uint16_t const ptp_id);
141static void init_propertymap();
Linus Walleij2715c442007-01-20 22:35:29 +0000142static void add_error_to_errorstack(LIBMTP_mtpdevice_t *device,
143 LIBMTP_error_number_t errornumber,
144 char const * const error_text);
Linus Walleij070e9b42007-01-22 08:49:28 +0000145static void add_ptp_error_to_errorstack(LIBMTP_mtpdevice_t *device,
146 uint16_t ptp_error,
147 char const * const error_text);
Linus Walleij438bd7f2006-06-08 11:35:44 +0000148static void flush_handles(LIBMTP_mtpdevice_t *device);
Linus Walleij2c1bbd62009-11-07 15:15:03 +0000149static void get_handles_recursively(LIBMTP_mtpdevice_t *device,
150 PTPParams *params,
Linus Walleij6bf68b42007-09-03 22:50:02 +0000151 uint32_t storageid,
152 uint32_t parent);
Linus Walleij9e1b0812006-12-12 19:22:02 +0000153static void free_storage_list(LIBMTP_mtpdevice_t *device);
154static int sort_storage_by(LIBMTP_mtpdevice_t *device, int const sortby);
Linus Walleij88babc82014-06-02 21:32:14 +0200155static uint32_t get_writeable_storageid(LIBMTP_mtpdevice_t *device,
156 uint64_t fitsize);
Linus Walleij6bf68b42007-09-03 22:50:02 +0000157static int get_storage_freespace(LIBMTP_mtpdevice_t *device,
158 LIBMTP_devicestorage_t *storage,
159 uint64_t *freespace);
Linus Walleij2c1bbd62009-11-07 15:15:03 +0000160static int check_if_file_fits(LIBMTP_mtpdevice_t *device,
Linus Walleij6bf68b42007-09-03 22:50:02 +0000161 LIBMTP_devicestorage_t *storage,
162 uint64_t const filesize);
Linus Walleijf67bca92006-05-29 09:33:39 +0000163static uint16_t map_libmtp_type_to_ptp_type(LIBMTP_filetype_t intype);
164static LIBMTP_filetype_t map_ptp_type_to_libmtp_type(uint16_t intype);
Richard Low6f070842009-05-03 10:26:16 +0000165static uint16_t map_libmtp_property_to_ptp_property(LIBMTP_property_t inproperty);
166static LIBMTP_property_t map_ptp_property_to_libmtp_property(uint16_t intype);
mopoke96143402006-10-30 04:37:26 +0000167static int get_device_unicode_property(LIBMTP_mtpdevice_t *device,
Linus Walleijcf223e62006-06-19 09:31:53 +0000168 char **unicstring, uint16_t property);
Linus Walleij29559562007-08-22 21:38:04 +0000169static uint16_t adjust_u16(uint16_t val, PTPObjectPropDesc *opd);
170static uint32_t adjust_u32(uint32_t val, PTPObjectPropDesc *opd);
Linus Walleij7783b9b2007-09-22 22:10:44 +0000171static char *get_iso8601_stamp(void);
Linus Walleij9901e222006-11-30 12:28:19 +0000172static char *get_string_from_object(LIBMTP_mtpdevice_t *device, uint32_t const object_id,
173 uint16_t const attribute_id);
Richard Lowc3f5fc32007-07-29 09:40:50 +0000174static uint64_t get_u64_from_object(LIBMTP_mtpdevice_t *device,uint32_t const object_id,
175 uint16_t const attribute_id, uint64_t const value_default);
Linus Walleij9901e222006-11-30 12:28:19 +0000176static uint32_t get_u32_from_object(LIBMTP_mtpdevice_t *device,uint32_t const object_id,
177 uint16_t const attribute_id, uint32_t const value_default);
178static uint16_t get_u16_from_object(LIBMTP_mtpdevice_t *device, uint32_t const object_id,
179 uint16_t const attribute_id, uint16_t const value_default);
180static uint8_t get_u8_from_object(LIBMTP_mtpdevice_t *device, uint32_t const object_id,
181 uint16_t const attribute_id, uint8_t const value_default);
182static int set_object_string(LIBMTP_mtpdevice_t *device, uint32_t const object_id,
183 uint16_t const attribute_id, char const * const string);
184static int set_object_u32(LIBMTP_mtpdevice_t *device, uint32_t const object_id,
185 uint16_t const attribute_id, uint32_t const value);
186static int set_object_u16(LIBMTP_mtpdevice_t *device, uint32_t const object_id,
187 uint16_t const attribute_id, uint16_t const value);
188static int set_object_u8(LIBMTP_mtpdevice_t *device, uint32_t const object_id,
189 uint16_t const attribute_id, uint8_t const value);
Linus Walleij8ab54262006-06-21 07:12:28 +0000190static void get_track_metadata(LIBMTP_mtpdevice_t *device, uint16_t objectformat,
191 LIBMTP_track_t *track);
Linus Walleija89a7942008-12-14 23:19:34 +0000192static LIBMTP_folder_t *get_subfolders_for_folder(LIBMTP_folder_t *list, uint32_t parent);
Linus Walleij7beba572006-11-29 08:56:12 +0000193static int create_new_abstract_list(LIBMTP_mtpdevice_t *device,
194 char const * const name,
Linus Walleijadce4a52007-04-23 07:28:11 +0000195 char const * const artist,
Linus Walleij31b74292008-05-02 23:29:06 +0000196 char const * const composer,
Linus Walleijadce4a52007-04-23 07:28:11 +0000197 char const * const genre,
Linus Walleij7beba572006-11-29 08:56:12 +0000198 uint32_t const parenthandle,
Linus Walleijea68f1f2008-06-22 21:54:44 +0000199 uint32_t const storageid,
Linus Walleij7beba572006-11-29 08:56:12 +0000200 uint16_t const objectformat,
201 char const * const suffix,
202 uint32_t * const newid,
203 uint32_t const * const tracks,
204 uint32_t const no_tracks);
Linus Walleij7783b9b2007-09-22 22:10:44 +0000205static int update_abstract_list(LIBMTP_mtpdevice_t *device,
206 char const * const name,
207 char const * const artist,
Linus Walleij31b74292008-05-02 23:29:06 +0000208 char const * const composer,
Linus Walleij7783b9b2007-09-22 22:10:44 +0000209 char const * const genre,
210 uint32_t const objecthandle,
211 uint16_t const objectformat,
212 uint32_t const * const tracks,
213 uint32_t const no_tracks);
Richard Lowd3b17022009-04-11 12:37:39 +0000214static int send_file_object_info(LIBMTP_mtpdevice_t *device, LIBMTP_file_t *filedata);
Linus Walleija6d0d482007-10-31 08:54:56 +0000215static void add_object_to_cache(LIBMTP_mtpdevice_t *device, uint32_t object_id);
Linus Walleij7752b952007-10-19 20:11:46 +0000216static void update_metadata_cache(LIBMTP_mtpdevice_t *device, uint32_t object_id);
Linus Walleij8d8c4352008-09-23 23:04:16 +0000217static int set_object_filename(LIBMTP_mtpdevice_t *device,
218 uint32_t object_id,
219 uint16_t ptp_type,
220 const char **newname);
Linus Walleij094b4502009-09-22 22:28:33 +0000221static char *generate_unique_filename(PTPParams* params, char const * const filename);
222static int check_filename_exists(PTPParams* params, char const * const filename);
Philip Langdale0a576512016-04-09 14:22:25 -0700223static void LIBMTP_Handle_Event(PTPContainer *ptp_event,
224 LIBMTP_event_t *event, uint32_t *out1);
Linus Walleij2c1bbd62009-11-07 15:15:03 +0000225
Richard Low5b4023c2009-04-16 19:14:38 +0000226/**
227 * These are to wrap the get/put handlers to convert from the MTP types to PTP types
228 * in a reliable way
229 */
230typedef struct _MTPDataHandler {
231 MTPDataGetFunc getfunc;
232 MTPDataPutFunc putfunc;
Linus Walleijd866d242009-08-23 21:50:39 +0000233 void *priv;
Richard Low5b4023c2009-04-16 19:14:38 +0000234} MTPDataHandler;
raveloxd9a28642006-05-26 23:42:22 +0000235
Linus Walleijd866d242009-08-23 21:50:39 +0000236static uint16_t get_func_wrapper(PTPParams* params, void* priv, unsigned long wantlen, unsigned char *data, unsigned long *gotlen);
Marcus Meissner3d692dc2016-02-21 12:34:06 +0100237static uint16_t put_func_wrapper(PTPParams* params, void* priv, unsigned long sendlen, unsigned char *data);
Linus Walleij2c1bbd62009-11-07 15:15:03 +0000238
Linus Walleijcf42f452006-11-28 08:32:49 +0000239/**
Linus Walleij01fc9c82009-03-10 23:52:09 +0000240 * Checks if a filename ends with ".ogg". Used in various
241 * situations when the device has no idea that it support
242 * OGG but still does.
Linus Walleijd98e8972009-03-08 22:57:17 +0000243 *
244 * @param name string to be checked.
245 * @return 0 if this does not end with ogg, any other
246 * value means it does.
247 */
248static int has_ogg_extension(char *name) {
249 char *ptype;
250
251 if (name == NULL)
252 return 0;
253 ptype = strrchr(name,'.');
254 if (ptype == NULL)
255 return 0;
256 if (!strcasecmp (ptype, ".ogg"))
257 return 1;
258 return 0;
259}
260
Linus Walleij89bb1cd2009-07-24 21:03:36 +0000261/**
262 * Checks if a filename ends with ".flac". Used in various
263 * situations when the device has no idea that it support
264 * FLAC but still does.
265 *
266 * @param name string to be checked.
267 * @return 0 if this does not end with flac, any other
268 * value means it does.
269 */
270static int has_flac_extension(char *name) {
271 char *ptype;
272
273 if (name == NULL)
274 return 0;
275 ptype = strrchr(name,'.');
276 if (ptype == NULL)
277 return 0;
278 if (!strcasecmp (ptype, ".flac"))
279 return 1;
280 return 0;
281}
282
283
Linus Walleijd98e8972009-03-08 22:57:17 +0000284
285/**
Linus Walleijcf42f452006-11-28 08:32:49 +0000286 * Create a new file mapping entry
287 * @return a newly allocated filemapping entry.
288 */
289static filemap_t *new_filemap_entry()
raveloxd9a28642006-05-26 23:42:22 +0000290{
Linus Walleijcf42f452006-11-28 08:32:49 +0000291 filemap_t *filemap;
mopoke96143402006-10-30 04:37:26 +0000292
Linus Walleijcf42f452006-11-28 08:32:49 +0000293 filemap = (filemap_t *)malloc(sizeof(filemap_t));
mopoke96143402006-10-30 04:37:26 +0000294
Linus Walleijf67bca92006-05-29 09:33:39 +0000295 if( filemap != NULL ) {
296 filemap->description = NULL;
297 filemap->id = LIBMTP_FILETYPE_UNKNOWN;
298 filemap->ptp_id = PTP_OFC_Undefined;
Linus Walleijf67bca92006-05-29 09:33:39 +0000299 filemap->next = NULL;
300 }
mopoke96143402006-10-30 04:37:26 +0000301
Linus Walleijf67bca92006-05-29 09:33:39 +0000302 return filemap;
raveloxd9a28642006-05-26 23:42:22 +0000303}
304
raveloxd9a28642006-05-26 23:42:22 +0000305/**
306 * Register an MTP or PTP filetype for data retrieval
307 *
308 * @param description Text description of filetype
Linus Walleijf0f3d482006-05-29 14:10:21 +0000309 * @param id libmtp internal filetype id
raveloxd9a28642006-05-26 23:42:22 +0000310 * @param ptp_id PTP filetype id
Linus Walleijf0f3d482006-05-29 14:10:21 +0000311 * @return 0 for success any other value means error.
raveloxd9a28642006-05-26 23:42:22 +0000312*/
Linus Walleijcf42f452006-11-28 08:32:49 +0000313static int register_filetype(char const * const description, LIBMTP_filetype_t const id,
314 uint16_t const ptp_id)
raveloxd9a28642006-05-26 23:42:22 +0000315{
Linus Walleijcf42f452006-11-28 08:32:49 +0000316 filemap_t *new = NULL, *current;
raveloxd9a28642006-05-26 23:42:22 +0000317
318 // Has this LIBMTP filetype been registered before ?
Lei Zhang81732132013-09-04 17:27:35 -0700319 current = g_filemap;
raveloxd9a28642006-05-26 23:42:22 +0000320 while (current != NULL) {
Linus Walleijf67bca92006-05-29 09:33:39 +0000321 if(current->id == id) {
322 break;
323 }
Linus Walleijf0f3d482006-05-29 14:10:21 +0000324 current = current->next;
raveloxd9a28642006-05-26 23:42:22 +0000325 }
mopoke96143402006-10-30 04:37:26 +0000326
raveloxd9a28642006-05-26 23:42:22 +0000327 // Create the entry
328 if(current == NULL) {
Linus Walleijf67bca92006-05-29 09:33:39 +0000329 new = new_filemap_entry();
Linus Walleijf0f3d482006-05-29 14:10:21 +0000330 if(new == NULL) {
331 return 1;
332 }
mopoke96143402006-10-30 04:37:26 +0000333
Linus Walleijf67bca92006-05-29 09:33:39 +0000334 new->id = id;
Linus Walleij438bd7f2006-06-08 11:35:44 +0000335 if(description != NULL) {
336 new->description = strdup(description);
337 }
Linus Walleijf67bca92006-05-29 09:33:39 +0000338 new->ptp_id = ptp_id;
mopoke96143402006-10-30 04:37:26 +0000339
Linus Walleijf67bca92006-05-29 09:33:39 +0000340 // Add the entry to the list
Lei Zhang81732132013-09-04 17:27:35 -0700341 if(g_filemap == NULL) {
342 g_filemap = new;
Linus Walleijf67bca92006-05-29 09:33:39 +0000343 } else {
Lei Zhang81732132013-09-04 17:27:35 -0700344 current = g_filemap;
Linus Walleijf67bca92006-05-29 09:33:39 +0000345 while (current->next != NULL ) current=current->next;
346 current->next = new;
347 }
348 // Update the existing entry
raveloxd9a28642006-05-26 23:42:22 +0000349 } else {
Linus Walleij438bd7f2006-06-08 11:35:44 +0000350 if (current->description != NULL) {
351 free(current->description);
352 }
Linus Walleijf67bca92006-05-29 09:33:39 +0000353 current->description = NULL;
Linus Walleijf0f3d482006-05-29 14:10:21 +0000354 if(description != NULL) {
Linus Walleij438bd7f2006-06-08 11:35:44 +0000355 current->description = strdup(description);
Linus Walleijf0f3d482006-05-29 14:10:21 +0000356 }
Linus Walleijf67bca92006-05-29 09:33:39 +0000357 current->ptp_id = ptp_id;
raveloxd9a28642006-05-26 23:42:22 +0000358 }
359
Linus Walleijf0f3d482006-05-29 14:10:21 +0000360 return 0;
raveloxd9a28642006-05-26 23:42:22 +0000361}
362
raveloxd9a28642006-05-26 23:42:22 +0000363static void init_filemap()
364{
Linus Walleija8b88892011-03-03 19:58:26 +0100365 register_filetype("Folder", LIBMTP_FILETYPE_FOLDER, PTP_OFC_Association);
Linus Walleij5fb47132006-12-30 15:35:48 +0000366 register_filetype("MediaCard", LIBMTP_FILETYPE_MEDIACARD, PTP_OFC_MTP_MediaCard);
Linus Walleijcf42f452006-11-28 08:32:49 +0000367 register_filetype("RIFF WAVE file", LIBMTP_FILETYPE_WAV, PTP_OFC_WAV);
Linus Walleij5fb47132006-12-30 15:35:48 +0000368 register_filetype("ISO MPEG-1 Audio Layer 3", LIBMTP_FILETYPE_MP3, PTP_OFC_MP3);
369 register_filetype("ISO MPEG-1 Audio Layer 2", LIBMTP_FILETYPE_MP2, PTP_OFC_MTP_MP2);
Linus Walleijcf42f452006-11-28 08:32:49 +0000370 register_filetype("Microsoft Windows Media Audio", LIBMTP_FILETYPE_WMA, PTP_OFC_MTP_WMA);
371 register_filetype("Ogg container format", LIBMTP_FILETYPE_OGG, PTP_OFC_MTP_OGG);
Linus Walleij5fb47132006-12-30 15:35:48 +0000372 register_filetype("Free Lossless Audio Codec (FLAC)", LIBMTP_FILETYPE_FLAC, PTP_OFC_MTP_FLAC);
373 register_filetype("Advanced Audio Coding (AAC)/MPEG-2 Part 7/MPEG-4 Part 3", LIBMTP_FILETYPE_AAC, PTP_OFC_MTP_AAC);
Richard Lowd3b17022009-04-11 12:37:39 +0000374 register_filetype("MPEG-4 Part 14 Container Format (Audio Emphasis)", LIBMTP_FILETYPE_M4A, PTP_OFC_MTP_M4A);
375 register_filetype("MPEG-4 Part 14 Container Format (Audio+Video Emphasis)", LIBMTP_FILETYPE_MP4, PTP_OFC_MTP_MP4);
Linus Walleijcf42f452006-11-28 08:32:49 +0000376 register_filetype("Audible.com Audio Codec", LIBMTP_FILETYPE_AUDIBLE, PTP_OFC_MTP_AudibleCodec);
Linus Walleijcf42f452006-11-28 08:32:49 +0000377 register_filetype("Undefined audio file", LIBMTP_FILETYPE_UNDEF_AUDIO, PTP_OFC_MTP_UndefinedAudio);
378 register_filetype("Microsoft Windows Media Video", LIBMTP_FILETYPE_WMV, PTP_OFC_MTP_WMV);
379 register_filetype("Audio Video Interleave", LIBMTP_FILETYPE_AVI, PTP_OFC_AVI);
380 register_filetype("MPEG video stream", LIBMTP_FILETYPE_MPEG, PTP_OFC_MPEG);
381 register_filetype("Microsoft Advanced Systems Format", LIBMTP_FILETYPE_ASF, PTP_OFC_ASF);
382 register_filetype("Apple Quicktime container format", LIBMTP_FILETYPE_QT, PTP_OFC_QT);
383 register_filetype("Undefined video file", LIBMTP_FILETYPE_UNDEF_VIDEO, PTP_OFC_MTP_UndefinedVideo);
384 register_filetype("JPEG file", LIBMTP_FILETYPE_JPEG, PTP_OFC_EXIF_JPEG);
Linus Walleij5fb47132006-12-30 15:35:48 +0000385 register_filetype("JP2 file", LIBMTP_FILETYPE_JP2, PTP_OFC_JP2);
386 register_filetype("JPX file", LIBMTP_FILETYPE_JPX, PTP_OFC_JPX);
Linus Walleijcf42f452006-11-28 08:32:49 +0000387 register_filetype("JFIF file", LIBMTP_FILETYPE_JFIF, PTP_OFC_JFIF);
388 register_filetype("TIFF bitmap file", LIBMTP_FILETYPE_TIFF, PTP_OFC_TIFF);
389 register_filetype("BMP bitmap file", LIBMTP_FILETYPE_BMP, PTP_OFC_BMP);
390 register_filetype("GIF bitmap file", LIBMTP_FILETYPE_GIF, PTP_OFC_GIF);
391 register_filetype("PICT bitmap file", LIBMTP_FILETYPE_PICT, PTP_OFC_PICT);
392 register_filetype("Portable Network Graphics", LIBMTP_FILETYPE_PNG, PTP_OFC_PNG);
393 register_filetype("Microsoft Windows Image Format", LIBMTP_FILETYPE_WINDOWSIMAGEFORMAT, PTP_OFC_MTP_WindowsImageFormat);
394 register_filetype("VCalendar version 1", LIBMTP_FILETYPE_VCALENDAR1, PTP_OFC_MTP_vCalendar1);
395 register_filetype("VCalendar version 2", LIBMTP_FILETYPE_VCALENDAR2, PTP_OFC_MTP_vCalendar2);
396 register_filetype("VCard version 2", LIBMTP_FILETYPE_VCARD2, PTP_OFC_MTP_vCard2);
397 register_filetype("VCard version 3", LIBMTP_FILETYPE_VCARD3, PTP_OFC_MTP_vCard3);
398 register_filetype("Undefined Windows executable file", LIBMTP_FILETYPE_WINEXEC, PTP_OFC_MTP_UndefinedWindowsExecutable);
399 register_filetype("Text file", LIBMTP_FILETYPE_TEXT, PTP_OFC_Text);
400 register_filetype("HTML file", LIBMTP_FILETYPE_HTML, PTP_OFC_HTML);
Linus Walleij5fb47132006-12-30 15:35:48 +0000401 register_filetype("XML file", LIBMTP_FILETYPE_XML, PTP_OFC_MTP_XMLDocument);
402 register_filetype("DOC file", LIBMTP_FILETYPE_DOC, PTP_OFC_MTP_MSWordDocument);
403 register_filetype("XLS file", LIBMTP_FILETYPE_XLS, PTP_OFC_MTP_MSExcelSpreadsheetXLS);
404 register_filetype("PPT file", LIBMTP_FILETYPE_PPT, PTP_OFC_MTP_MSPowerpointPresentationPPT);
405 register_filetype("MHT file", LIBMTP_FILETYPE_MHT, PTP_OFC_MTP_MHTCompiledHTMLDocument);
Linus Walleija05b9802006-12-08 08:50:30 +0000406 register_filetype("Firmware file", LIBMTP_FILETYPE_FIRMWARE, PTP_OFC_MTP_Firmware);
Richard Lowd3b17022009-04-11 12:37:39 +0000407 register_filetype("Abstract Album file", LIBMTP_FILETYPE_ALBUM, PTP_OFC_MTP_AbstractAudioAlbum);
408 register_filetype("Abstract Playlist file", LIBMTP_FILETYPE_PLAYLIST, PTP_OFC_MTP_AbstractAudioVideoPlaylist);
Linus Walleijcf42f452006-11-28 08:32:49 +0000409 register_filetype("Undefined filetype", LIBMTP_FILETYPE_UNKNOWN, PTP_OFC_Undefined);
raveloxd9a28642006-05-26 23:42:22 +0000410}
Linus Walleij16c51f02006-05-04 13:20:22 +0000411
Linus Walleij16c51f02006-05-04 13:20:22 +0000412/**
413 * Returns the PTP filetype that maps to a certain libmtp internal file type.
414 * @param intype the MTP library interface type
415 * @return the PTP (libgphoto2) interface type
416 */
417static uint16_t map_libmtp_type_to_ptp_type(LIBMTP_filetype_t intype)
418{
Linus Walleijcf42f452006-11-28 08:32:49 +0000419 filemap_t *current;
Linus Walleij16c51f02006-05-04 13:20:22 +0000420
Lei Zhang81732132013-09-04 17:27:35 -0700421 current = g_filemap;
raveloxd9a28642006-05-26 23:42:22 +0000422
423 while (current != NULL) {
Linus Walleijf0f3d482006-05-29 14:10:21 +0000424 if(current->id == intype) {
425 return current->ptp_id;
426 }
427 current = current->next;
Linus Walleij16c51f02006-05-04 13:20:22 +0000428 }
Linus Walleij1fd2d272006-05-08 09:22:01 +0000429 // printf("map_libmtp_type_to_ptp_type: unknown filetype.\n");
Linus Walleij16c51f02006-05-04 13:20:22 +0000430 return PTP_OFC_Undefined;
431}
432
433
434/**
Linus Walleij31b74292008-05-02 23:29:06 +0000435 * Returns the MTP internal interface type that maps to a certain ptp
436 * interface type.
mopoke96143402006-10-30 04:37:26 +0000437 * @param intype the PTP (libgphoto2) interface type
Linus Walleij8ab54262006-06-21 07:12:28 +0000438 * @return the MTP library interface type
Linus Walleij16c51f02006-05-04 13:20:22 +0000439 */
440static LIBMTP_filetype_t map_ptp_type_to_libmtp_type(uint16_t intype)
441{
Linus Walleijcf42f452006-11-28 08:32:49 +0000442 filemap_t *current;
Linus Walleij16c51f02006-05-04 13:20:22 +0000443
Lei Zhang81732132013-09-04 17:27:35 -0700444 current = g_filemap;
raveloxd9a28642006-05-26 23:42:22 +0000445
446 while (current != NULL) {
Linus Walleijf0f3d482006-05-29 14:10:21 +0000447 if(current->ptp_id == intype) {
448 return current->id;
449 }
450 current = current->next;
Linus Walleij16c51f02006-05-04 13:20:22 +0000451 }
Linus Walleij1fd2d272006-05-08 09:22:01 +0000452 // printf("map_ptp_type_to_libmtp_type: unknown filetype.\n");
Linus Walleij16c51f02006-05-04 13:20:22 +0000453 return LIBMTP_FILETYPE_UNKNOWN;
454}
455
Richard Low6f070842009-05-03 10:26:16 +0000456/**
457 * Create a new property mapping entry
458 * @return a newly allocated propertymapping entry.
459 */
460static propertymap_t *new_propertymap_entry()
461{
462 propertymap_t *propertymap;
463
464 propertymap = (propertymap_t *)malloc(sizeof(propertymap_t));
465
466 if( propertymap != NULL ) {
467 propertymap->description = NULL;
468 propertymap->id = LIBMTP_PROPERTY_UNKNOWN;
469 propertymap->ptp_id = 0;
470 propertymap->next = NULL;
471 }
472
473 return propertymap;
474}
475
476/**
477 * Register an MTP or PTP property for data retrieval
478 *
479 * @param description Text description of property
480 * @param id libmtp internal property id
481 * @param ptp_id PTP property id
482 * @return 0 for success any other value means error.
483*/
484static int register_property(char const * const description, LIBMTP_property_t const id,
485 uint16_t const ptp_id)
486{
487 propertymap_t *new = NULL, *current;
488
489 // Has this LIBMTP propety been registered before ?
Lei Zhang81732132013-09-04 17:27:35 -0700490 current = g_propertymap;
Richard Low6f070842009-05-03 10:26:16 +0000491 while (current != NULL) {
492 if(current->id == id) {
493 break;
494 }
495 current = current->next;
496 }
497
498 // Create the entry
499 if(current == NULL) {
500 new = new_propertymap_entry();
501 if(new == NULL) {
502 return 1;
503 }
504
505 new->id = id;
506 if(description != NULL) {
507 new->description = strdup(description);
508 }
509 new->ptp_id = ptp_id;
510
511 // Add the entry to the list
Lei Zhang81732132013-09-04 17:27:35 -0700512 if(g_propertymap == NULL) {
513 g_propertymap = new;
Richard Low6f070842009-05-03 10:26:16 +0000514 } else {
Lei Zhang81732132013-09-04 17:27:35 -0700515 current = g_propertymap;
Richard Low6f070842009-05-03 10:26:16 +0000516 while (current->next != NULL ) current=current->next;
517 current->next = new;
518 }
519 // Update the existing entry
520 } else {
521 if (current->description != NULL) {
522 free(current->description);
523 }
524 current->description = NULL;
525 if(description != NULL) {
526 current->description = strdup(description);
527 }
528 current->ptp_id = ptp_id;
529 }
530
531 return 0;
532}
533
534static void init_propertymap()
535{
536 register_property("Storage ID", LIBMTP_PROPERTY_StorageID, PTP_OPC_StorageID);
537 register_property("Object Format", LIBMTP_PROPERTY_ObjectFormat, PTP_OPC_ObjectFormat);
538 register_property("Protection Status", LIBMTP_PROPERTY_ProtectionStatus, PTP_OPC_ProtectionStatus);
539 register_property("Object Size", LIBMTP_PROPERTY_ObjectSize, PTP_OPC_ObjectSize);
540 register_property("Association Type", LIBMTP_PROPERTY_AssociationType, PTP_OPC_AssociationType);
541 register_property("Association Desc", LIBMTP_PROPERTY_AssociationDesc, PTP_OPC_AssociationDesc);
542 register_property("Object File Name", LIBMTP_PROPERTY_ObjectFileName, PTP_OPC_ObjectFileName);
543 register_property("Date Created", LIBMTP_PROPERTY_DateCreated, PTP_OPC_DateCreated);
544 register_property("Date Modified", LIBMTP_PROPERTY_DateModified, PTP_OPC_DateModified);
545 register_property("Keywords", LIBMTP_PROPERTY_Keywords, PTP_OPC_Keywords);
546 register_property("Parent Object", LIBMTP_PROPERTY_ParentObject, PTP_OPC_ParentObject);
547 register_property("Allowed Folder Contents", LIBMTP_PROPERTY_AllowedFolderContents, PTP_OPC_AllowedFolderContents);
548 register_property("Hidden", LIBMTP_PROPERTY_Hidden, PTP_OPC_Hidden);
549 register_property("System Object", LIBMTP_PROPERTY_SystemObject, PTP_OPC_SystemObject);
550 register_property("Persistant Unique Object Identifier", LIBMTP_PROPERTY_PersistantUniqueObjectIdentifier, PTP_OPC_PersistantUniqueObjectIdentifier);
551 register_property("Sync ID", LIBMTP_PROPERTY_SyncID, PTP_OPC_SyncID);
552 register_property("Property Bag", LIBMTP_PROPERTY_PropertyBag, PTP_OPC_PropertyBag);
553 register_property("Name", LIBMTP_PROPERTY_Name, PTP_OPC_Name);
554 register_property("Created By", LIBMTP_PROPERTY_CreatedBy, PTP_OPC_CreatedBy);
555 register_property("Artist", LIBMTP_PROPERTY_Artist, PTP_OPC_Artist);
556 register_property("Date Authored", LIBMTP_PROPERTY_DateAuthored, PTP_OPC_DateAuthored);
557 register_property("Description", LIBMTP_PROPERTY_Description, PTP_OPC_Description);
558 register_property("URL Reference", LIBMTP_PROPERTY_URLReference, PTP_OPC_URLReference);
559 register_property("Language Locale", LIBMTP_PROPERTY_LanguageLocale, PTP_OPC_LanguageLocale);
560 register_property("Copyright Information", LIBMTP_PROPERTY_CopyrightInformation, PTP_OPC_CopyrightInformation);
561 register_property("Source", LIBMTP_PROPERTY_Source, PTP_OPC_Source);
562 register_property("Origin Location", LIBMTP_PROPERTY_OriginLocation, PTP_OPC_OriginLocation);
563 register_property("Date Added", LIBMTP_PROPERTY_DateAdded, PTP_OPC_DateAdded);
564 register_property("Non Consumable", LIBMTP_PROPERTY_NonConsumable, PTP_OPC_NonConsumable);
565 register_property("Corrupt Or Unplayable", LIBMTP_PROPERTY_CorruptOrUnplayable, PTP_OPC_CorruptOrUnplayable);
566 register_property("Producer Serial Number", LIBMTP_PROPERTY_ProducerSerialNumber, PTP_OPC_ProducerSerialNumber);
567 register_property("Representative Sample Format", LIBMTP_PROPERTY_RepresentativeSampleFormat, PTP_OPC_RepresentativeSampleFormat);
568 register_property("Representative Sample Sise", LIBMTP_PROPERTY_RepresentativeSampleSize, PTP_OPC_RepresentativeSampleSize);
569 register_property("Representative Sample Height", LIBMTP_PROPERTY_RepresentativeSampleHeight, PTP_OPC_RepresentativeSampleHeight);
570 register_property("Representative Sample Width", LIBMTP_PROPERTY_RepresentativeSampleWidth, PTP_OPC_RepresentativeSampleWidth);
571 register_property("Representative Sample Duration", LIBMTP_PROPERTY_RepresentativeSampleDuration, PTP_OPC_RepresentativeSampleDuration);
572 register_property("Representative Sample Data", LIBMTP_PROPERTY_RepresentativeSampleData, PTP_OPC_RepresentativeSampleData);
573 register_property("Width", LIBMTP_PROPERTY_Width, PTP_OPC_Width);
574 register_property("Height", LIBMTP_PROPERTY_Height, PTP_OPC_Height);
575 register_property("Duration", LIBMTP_PROPERTY_Duration, PTP_OPC_Duration);
576 register_property("Rating", LIBMTP_PROPERTY_Rating, PTP_OPC_Rating);
577 register_property("Track", LIBMTP_PROPERTY_Track, PTP_OPC_Track);
578 register_property("Genre", LIBMTP_PROPERTY_Genre, PTP_OPC_Genre);
579 register_property("Credits", LIBMTP_PROPERTY_Credits, PTP_OPC_Credits);
580 register_property("Lyrics", LIBMTP_PROPERTY_Lyrics, PTP_OPC_Lyrics);
581 register_property("Subscription Content ID", LIBMTP_PROPERTY_SubscriptionContentID, PTP_OPC_SubscriptionContentID);
582 register_property("Produced By", LIBMTP_PROPERTY_ProducedBy, PTP_OPC_ProducedBy);
583 register_property("Use Count", LIBMTP_PROPERTY_UseCount, PTP_OPC_UseCount);
584 register_property("Skip Count", LIBMTP_PROPERTY_SkipCount, PTP_OPC_SkipCount);
585 register_property("Last Accessed", LIBMTP_PROPERTY_LastAccessed, PTP_OPC_LastAccessed);
586 register_property("Parental Rating", LIBMTP_PROPERTY_ParentalRating, PTP_OPC_ParentalRating);
587 register_property("Meta Genre", LIBMTP_PROPERTY_MetaGenre, PTP_OPC_MetaGenre);
588 register_property("Composer", LIBMTP_PROPERTY_Composer, PTP_OPC_Composer);
589 register_property("Effective Rating", LIBMTP_PROPERTY_EffectiveRating, PTP_OPC_EffectiveRating);
590 register_property("Subtitle", LIBMTP_PROPERTY_Subtitle, PTP_OPC_Subtitle);
591 register_property("Original Release Date", LIBMTP_PROPERTY_OriginalReleaseDate, PTP_OPC_OriginalReleaseDate);
592 register_property("Album Name", LIBMTP_PROPERTY_AlbumName, PTP_OPC_AlbumName);
593 register_property("Album Artist", LIBMTP_PROPERTY_AlbumArtist, PTP_OPC_AlbumArtist);
594 register_property("Mood", LIBMTP_PROPERTY_Mood, PTP_OPC_Mood);
595 register_property("DRM Status", LIBMTP_PROPERTY_DRMStatus, PTP_OPC_DRMStatus);
596 register_property("Sub Description", LIBMTP_PROPERTY_SubDescription, PTP_OPC_SubDescription);
597 register_property("Is Cropped", LIBMTP_PROPERTY_IsCropped, PTP_OPC_IsCropped);
598 register_property("Is Color Corrected", LIBMTP_PROPERTY_IsColorCorrected, PTP_OPC_IsColorCorrected);
599 register_property("Image Bit Depth", LIBMTP_PROPERTY_ImageBitDepth, PTP_OPC_ImageBitDepth);
600 register_property("f Number", LIBMTP_PROPERTY_Fnumber, PTP_OPC_Fnumber);
601 register_property("Exposure Time", LIBMTP_PROPERTY_ExposureTime, PTP_OPC_ExposureTime);
602 register_property("Exposure Index", LIBMTP_PROPERTY_ExposureIndex, PTP_OPC_ExposureIndex);
603 register_property("Display Name", LIBMTP_PROPERTY_DisplayName, PTP_OPC_DisplayName);
604 register_property("Body Text", LIBMTP_PROPERTY_BodyText, PTP_OPC_BodyText);
605 register_property("Subject", LIBMTP_PROPERTY_Subject, PTP_OPC_Subject);
606 register_property("Priority", LIBMTP_PROPERTY_Priority, PTP_OPC_Priority);
607 register_property("Given Name", LIBMTP_PROPERTY_GivenName, PTP_OPC_GivenName);
608 register_property("Middle Names", LIBMTP_PROPERTY_MiddleNames, PTP_OPC_MiddleNames);
609 register_property("Family Name", LIBMTP_PROPERTY_FamilyName, PTP_OPC_FamilyName);
610 register_property("Prefix", LIBMTP_PROPERTY_Prefix, PTP_OPC_Prefix);
611 register_property("Suffix", LIBMTP_PROPERTY_Suffix, PTP_OPC_Suffix);
612 register_property("Phonetic Given Name", LIBMTP_PROPERTY_PhoneticGivenName, PTP_OPC_PhoneticGivenName);
613 register_property("Phonetic Family Name", LIBMTP_PROPERTY_PhoneticFamilyName, PTP_OPC_PhoneticFamilyName);
614 register_property("Email: Primary", LIBMTP_PROPERTY_EmailPrimary, PTP_OPC_EmailPrimary);
615 register_property("Email: Personal 1", LIBMTP_PROPERTY_EmailPersonal1, PTP_OPC_EmailPersonal1);
616 register_property("Email: Personal 2", LIBMTP_PROPERTY_EmailPersonal2, PTP_OPC_EmailPersonal2);
617 register_property("Email: Business 1", LIBMTP_PROPERTY_EmailBusiness1, PTP_OPC_EmailBusiness1);
618 register_property("Email: Business 2", LIBMTP_PROPERTY_EmailBusiness2, PTP_OPC_EmailBusiness2);
619 register_property("Email: Others", LIBMTP_PROPERTY_EmailOthers, PTP_OPC_EmailOthers);
620 register_property("Phone Number: Primary", LIBMTP_PROPERTY_PhoneNumberPrimary, PTP_OPC_PhoneNumberPrimary);
621 register_property("Phone Number: Personal", LIBMTP_PROPERTY_PhoneNumberPersonal, PTP_OPC_PhoneNumberPersonal);
622 register_property("Phone Number: Personal 2", LIBMTP_PROPERTY_PhoneNumberPersonal2, PTP_OPC_PhoneNumberPersonal2);
623 register_property("Phone Number: Business", LIBMTP_PROPERTY_PhoneNumberBusiness, PTP_OPC_PhoneNumberBusiness);
624 register_property("Phone Number: Business 2", LIBMTP_PROPERTY_PhoneNumberBusiness2, PTP_OPC_PhoneNumberBusiness2);
625 register_property("Phone Number: Mobile", LIBMTP_PROPERTY_PhoneNumberMobile, PTP_OPC_PhoneNumberMobile);
626 register_property("Phone Number: Mobile 2", LIBMTP_PROPERTY_PhoneNumberMobile2, PTP_OPC_PhoneNumberMobile2);
627 register_property("Fax Number: Primary", LIBMTP_PROPERTY_FaxNumberPrimary, PTP_OPC_FaxNumberPrimary);
628 register_property("Fax Number: Personal", LIBMTP_PROPERTY_FaxNumberPersonal, PTP_OPC_FaxNumberPersonal);
629 register_property("Fax Number: Business", LIBMTP_PROPERTY_FaxNumberBusiness, PTP_OPC_FaxNumberBusiness);
630 register_property("Pager Number", LIBMTP_PROPERTY_PagerNumber, PTP_OPC_PagerNumber);
631 register_property("Phone Number: Others", LIBMTP_PROPERTY_PhoneNumberOthers, PTP_OPC_PhoneNumberOthers);
632 register_property("Primary Web Address", LIBMTP_PROPERTY_PrimaryWebAddress, PTP_OPC_PrimaryWebAddress);
633 register_property("Personal Web Address", LIBMTP_PROPERTY_PersonalWebAddress, PTP_OPC_PersonalWebAddress);
634 register_property("Business Web Address", LIBMTP_PROPERTY_BusinessWebAddress, PTP_OPC_BusinessWebAddress);
635 register_property("Instant Messenger Address 1", LIBMTP_PROPERTY_InstantMessengerAddress, PTP_OPC_InstantMessengerAddress);
636 register_property("Instant Messenger Address 2", LIBMTP_PROPERTY_InstantMessengerAddress2, PTP_OPC_InstantMessengerAddress2);
637 register_property("Instant Messenger Address 3", LIBMTP_PROPERTY_InstantMessengerAddress3, PTP_OPC_InstantMessengerAddress3);
638 register_property("Postal Address: Personal: Full", LIBMTP_PROPERTY_PostalAddressPersonalFull, PTP_OPC_PostalAddressPersonalFull);
639 register_property("Postal Address: Personal: Line 1", LIBMTP_PROPERTY_PostalAddressPersonalFullLine1, PTP_OPC_PostalAddressPersonalFullLine1);
640 register_property("Postal Address: Personal: Line 2", LIBMTP_PROPERTY_PostalAddressPersonalFullLine2, PTP_OPC_PostalAddressPersonalFullLine2);
641 register_property("Postal Address: Personal: City", LIBMTP_PROPERTY_PostalAddressPersonalFullCity, PTP_OPC_PostalAddressPersonalFullCity);
642 register_property("Postal Address: Personal: Region", LIBMTP_PROPERTY_PostalAddressPersonalFullRegion, PTP_OPC_PostalAddressPersonalFullRegion);
643 register_property("Postal Address: Personal: Postal Code", LIBMTP_PROPERTY_PostalAddressPersonalFullPostalCode, PTP_OPC_PostalAddressPersonalFullPostalCode);
644 register_property("Postal Address: Personal: Country", LIBMTP_PROPERTY_PostalAddressPersonalFullCountry, PTP_OPC_PostalAddressPersonalFullCountry);
645 register_property("Postal Address: Business: Full", LIBMTP_PROPERTY_PostalAddressBusinessFull, PTP_OPC_PostalAddressBusinessFull);
646 register_property("Postal Address: Business: Line 1", LIBMTP_PROPERTY_PostalAddressBusinessLine1, PTP_OPC_PostalAddressBusinessLine1);
647 register_property("Postal Address: Business: Line 2", LIBMTP_PROPERTY_PostalAddressBusinessLine2, PTP_OPC_PostalAddressBusinessLine2);
648 register_property("Postal Address: Business: City", LIBMTP_PROPERTY_PostalAddressBusinessCity, PTP_OPC_PostalAddressBusinessCity);
649 register_property("Postal Address: Business: Region", LIBMTP_PROPERTY_PostalAddressBusinessRegion, PTP_OPC_PostalAddressBusinessRegion);
650 register_property("Postal Address: Business: Postal Code", LIBMTP_PROPERTY_PostalAddressBusinessPostalCode, PTP_OPC_PostalAddressBusinessPostalCode);
651 register_property("Postal Address: Business: Country", LIBMTP_PROPERTY_PostalAddressBusinessCountry, PTP_OPC_PostalAddressBusinessCountry);
652 register_property("Postal Address: Other: Full", LIBMTP_PROPERTY_PostalAddressOtherFull, PTP_OPC_PostalAddressOtherFull);
653 register_property("Postal Address: Other: Line 1", LIBMTP_PROPERTY_PostalAddressOtherLine1, PTP_OPC_PostalAddressOtherLine1);
654 register_property("Postal Address: Other: Line 2", LIBMTP_PROPERTY_PostalAddressOtherLine2, PTP_OPC_PostalAddressOtherLine2);
655 register_property("Postal Address: Other: City", LIBMTP_PROPERTY_PostalAddressOtherCity, PTP_OPC_PostalAddressOtherCity);
656 register_property("Postal Address: Other: Region", LIBMTP_PROPERTY_PostalAddressOtherRegion, PTP_OPC_PostalAddressOtherRegion);
657 register_property("Postal Address: Other: Postal Code", LIBMTP_PROPERTY_PostalAddressOtherPostalCode, PTP_OPC_PostalAddressOtherPostalCode);
658 register_property("Postal Address: Other: Counrtry", LIBMTP_PROPERTY_PostalAddressOtherCountry, PTP_OPC_PostalAddressOtherCountry);
659 register_property("Organization Name", LIBMTP_PROPERTY_OrganizationName, PTP_OPC_OrganizationName);
660 register_property("Phonetic Organization Name", LIBMTP_PROPERTY_PhoneticOrganizationName, PTP_OPC_PhoneticOrganizationName);
661 register_property("Role", LIBMTP_PROPERTY_Role, PTP_OPC_Role);
662 register_property("Birthdate", LIBMTP_PROPERTY_Birthdate, PTP_OPC_Birthdate);
663 register_property("Message To", LIBMTP_PROPERTY_MessageTo, PTP_OPC_MessageTo);
664 register_property("Message CC", LIBMTP_PROPERTY_MessageCC, PTP_OPC_MessageCC);
665 register_property("Message BCC", LIBMTP_PROPERTY_MessageBCC, PTP_OPC_MessageBCC);
666 register_property("Message Read", LIBMTP_PROPERTY_MessageRead, PTP_OPC_MessageRead);
667 register_property("Message Received Time", LIBMTP_PROPERTY_MessageReceivedTime, PTP_OPC_MessageReceivedTime);
668 register_property("Message Sender", LIBMTP_PROPERTY_MessageSender, PTP_OPC_MessageSender);
669 register_property("Activity Begin Time", LIBMTP_PROPERTY_ActivityBeginTime, PTP_OPC_ActivityBeginTime);
670 register_property("Activity End Time", LIBMTP_PROPERTY_ActivityEndTime, PTP_OPC_ActivityEndTime);
671 register_property("Activity Location", LIBMTP_PROPERTY_ActivityLocation, PTP_OPC_ActivityLocation);
672 register_property("Activity Required Attendees", LIBMTP_PROPERTY_ActivityRequiredAttendees, PTP_OPC_ActivityRequiredAttendees);
673 register_property("Optional Attendees", LIBMTP_PROPERTY_ActivityOptionalAttendees, PTP_OPC_ActivityOptionalAttendees);
674 register_property("Activity Resources", LIBMTP_PROPERTY_ActivityResources, PTP_OPC_ActivityResources);
675 register_property("Activity Accepted", LIBMTP_PROPERTY_ActivityAccepted, PTP_OPC_ActivityAccepted);
676 register_property("Owner", LIBMTP_PROPERTY_Owner, PTP_OPC_Owner);
677 register_property("Editor", LIBMTP_PROPERTY_Editor, PTP_OPC_Editor);
678 register_property("Webmaster", LIBMTP_PROPERTY_Webmaster, PTP_OPC_Webmaster);
679 register_property("URL Source", LIBMTP_PROPERTY_URLSource, PTP_OPC_URLSource);
680 register_property("URL Destination", LIBMTP_PROPERTY_URLDestination, PTP_OPC_URLDestination);
681 register_property("Time Bookmark", LIBMTP_PROPERTY_TimeBookmark, PTP_OPC_TimeBookmark);
682 register_property("Object Bookmark", LIBMTP_PROPERTY_ObjectBookmark, PTP_OPC_ObjectBookmark);
683 register_property("Byte Bookmark", LIBMTP_PROPERTY_ByteBookmark, PTP_OPC_ByteBookmark);
684 register_property("Last Build Date", LIBMTP_PROPERTY_LastBuildDate, PTP_OPC_LastBuildDate);
685 register_property("Time To Live", LIBMTP_PROPERTY_TimetoLive, PTP_OPC_TimetoLive);
686 register_property("Media GUID", LIBMTP_PROPERTY_MediaGUID, PTP_OPC_MediaGUID);
687 register_property("Total Bit Rate", LIBMTP_PROPERTY_TotalBitRate, PTP_OPC_TotalBitRate);
688 register_property("Bit Rate Type", LIBMTP_PROPERTY_BitRateType, PTP_OPC_BitRateType);
689 register_property("Sample Rate", LIBMTP_PROPERTY_SampleRate, PTP_OPC_SampleRate);
690 register_property("Number Of Channels", LIBMTP_PROPERTY_NumberOfChannels, PTP_OPC_NumberOfChannels);
691 register_property("Audio Bit Depth", LIBMTP_PROPERTY_AudioBitDepth, PTP_OPC_AudioBitDepth);
692 register_property("Scan Depth", LIBMTP_PROPERTY_ScanDepth, PTP_OPC_ScanDepth);
693 register_property("Audio WAVE Codec", LIBMTP_PROPERTY_AudioWAVECodec, PTP_OPC_AudioWAVECodec);
694 register_property("Audio Bit Rate", LIBMTP_PROPERTY_AudioBitRate, PTP_OPC_AudioBitRate);
695 register_property("Video Four CC Codec", LIBMTP_PROPERTY_VideoFourCCCodec, PTP_OPC_VideoFourCCCodec);
696 register_property("Video Bit Rate", LIBMTP_PROPERTY_VideoBitRate, PTP_OPC_VideoBitRate);
697 register_property("Frames Per Thousand Seconds", LIBMTP_PROPERTY_FramesPerThousandSeconds, PTP_OPC_FramesPerThousandSeconds);
698 register_property("Key Frame Distance", LIBMTP_PROPERTY_KeyFrameDistance, PTP_OPC_KeyFrameDistance);
699 register_property("Buffer Size", LIBMTP_PROPERTY_BufferSize, PTP_OPC_BufferSize);
700 register_property("Encoding Quality", LIBMTP_PROPERTY_EncodingQuality, PTP_OPC_EncodingQuality);
701 register_property("Encoding Profile", LIBMTP_PROPERTY_EncodingProfile, PTP_OPC_EncodingProfile);
702 register_property("Buy flag", LIBMTP_PROPERTY_BuyFlag, PTP_OPC_BuyFlag);
703 register_property("Unknown property", LIBMTP_PROPERTY_UNKNOWN, 0);
704}
705
706/**
707 * Returns the PTP property that maps to a certain libmtp internal property type.
708 * @param inproperty the MTP library interface property
709 * @return the PTP (libgphoto2) property type
710 */
711static uint16_t map_libmtp_property_to_ptp_property(LIBMTP_property_t inproperty)
712{
713 propertymap_t *current;
714
Lei Zhang81732132013-09-04 17:27:35 -0700715 current = g_propertymap;
Richard Low6f070842009-05-03 10:26:16 +0000716
717 while (current != NULL) {
718 if(current->id == inproperty) {
719 return current->ptp_id;
720 }
721 current = current->next;
722 }
723 return 0;
724}
725
726
727/**
728 * Returns the MTP internal interface property that maps to a certain ptp
729 * interface property.
730 * @param inproperty the PTP (libgphoto2) interface property
731 * @return the MTP library interface property
732 */
733static LIBMTP_property_t map_ptp_property_to_libmtp_property(uint16_t inproperty)
734{
735 propertymap_t *current;
736
Lei Zhang81732132013-09-04 17:27:35 -0700737 current = g_propertymap;
Richard Low6f070842009-05-03 10:26:16 +0000738
739 while (current != NULL) {
740 if(current->ptp_id == inproperty) {
741 return current->id;
742 }
743 current = current->next;
744 }
745 // printf("map_ptp_type_to_libmtp_type: unknown filetype.\n");
746 return LIBMTP_PROPERTY_UNKNOWN;
747}
748
Linus Walleijfa1374c2006-02-27 07:41:46 +0000749
Linus Walleij6946ac52006-03-21 06:51:22 +0000750/**
nicklas79daadbf22009-09-28 18:19:34 +0000751 * Set the debug level.
752 *
753 * By default, the debug level is set to '0' (disable).
754 */
755void LIBMTP_Set_Debug(int level)
756{
757 if (LIBMTP_debug || level)
Catalin Patuleada25de02012-07-19 00:57:42 -0400758 LIBMTP_ERROR("LIBMTP_Set_Debug: Setting debugging level to %d (0x%02x) "
759 "(%s)\n", level, level, level ? "on" : "off");
nicklas79daadbf22009-09-28 18:19:34 +0000760
761 LIBMTP_debug = level;
762}
763
764
765/**
Linus Walleijf0f3d482006-05-29 14:10:21 +0000766 * Initialize the library. You are only supposed to call this
767 * one, before using the library for the first time in a program.
768 * Never re-initialize libmtp!
769 *
raveloxd9a28642006-05-26 23:42:22 +0000770 * The only thing this does at the moment is to initialise the
Sajid Anwar8dca41d2012-08-18 20:36:13 +0200771 * filetype mapping table, as well as load MTPZ data if necessary.
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000772 */
773void LIBMTP_Init(void)
774{
Catalin Patuleada25de02012-07-19 00:57:42 -0400775 const char *env_debug = getenv("LIBMTP_DEBUG");
776 if (env_debug) {
777 const long debug_flags = strtol(env_debug, NULL, 0);
778 if (debug_flags != LONG_MIN && debug_flags != LONG_MAX &&
779 INT_MIN <= debug_flags && debug_flags <= INT_MAX) {
780 LIBMTP_Set_Debug(debug_flags);
781 } else {
782 fprintf(stderr, "LIBMTP_Init: error setting debug flags from environment "
783 "value \"%s\"\n", env_debug);
784 }
785 }
nicklas79daadbf22009-09-28 18:19:34 +0000786
raveloxd9a28642006-05-26 23:42:22 +0000787 init_filemap();
Richard Low6f070842009-05-03 10:26:16 +0000788 init_propertymap();
Sajid Anwar8dca41d2012-08-18 20:36:13 +0200789
790 if (mtpz_loaddata() == -1)
791 use_mtpz = 0;
792 else
793 use_mtpz = 1;
794
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000795 return;
796}
797
Linus Walleijcf42f452006-11-28 08:32:49 +0000798
799/**
800 * This helper function returns a textual description for a libmtp
801 * file type to be used in dialog boxes etc.
802 * @param intype the libmtp internal filetype to get a description for.
803 * @return a string representing the filetype, this must <b>NOT</b>
804 * be free():ed by the caller!
805 */
806char const * LIBMTP_Get_Filetype_Description(LIBMTP_filetype_t intype)
807{
808 filemap_t *current;
809
Lei Zhang81732132013-09-04 17:27:35 -0700810 current = g_filemap;
Linus Walleijcf42f452006-11-28 08:32:49 +0000811
812 while (current != NULL) {
813 if(current->id == intype) {
814 return current->description;
815 }
816 current = current->next;
817 }
818
819 return "Unknown filetype";
820}
821
Linus Walleij7783b9b2007-09-22 22:10:44 +0000822/**
Richard Low6f070842009-05-03 10:26:16 +0000823 * This helper function returns a textual description for a libmtp
824 * property to be used in dialog boxes etc.
825 * @param inproperty the libmtp internal property to get a description for.
826 * @return a string representing the filetype, this must <b>NOT</b>
827 * be free():ed by the caller!
828 */
829char const * LIBMTP_Get_Property_Description(LIBMTP_property_t inproperty)
830{
831 propertymap_t *current;
832
Lei Zhang81732132013-09-04 17:27:35 -0700833 current = g_propertymap;
Richard Low6f070842009-05-03 10:26:16 +0000834
835 while (current != NULL) {
836 if(current->id == inproperty) {
837 return current->description;
838 }
839 current = current->next;
840 }
841
842 return "Unknown property";
843}
844
845/**
Linus Walleij7783b9b2007-09-22 22:10:44 +0000846 * This function will do its best to fit a 16bit
847 * value into a PTP object property if the property
848 * is limited in range or step sizes.
849 */
Linus Walleij29559562007-08-22 21:38:04 +0000850static uint16_t adjust_u16(uint16_t val, PTPObjectPropDesc *opd)
851{
852 switch (opd->FormFlag) {
853 case PTP_DPFF_Range:
854 if (val < opd->FORM.Range.MinimumValue.u16) {
855 return opd->FORM.Range.MinimumValue.u16;
856 }
857 if (val > opd->FORM.Range.MaximumValue.u16) {
858 return opd->FORM.Range.MaximumValue.u16;
859 }
860 // Round down to last step.
861 if (val % opd->FORM.Range.StepSize.u16 != 0) {
862 return val - (val % opd->FORM.Range.StepSize.u16);
863 }
864 return val;
865 break;
866 case PTP_DPFF_Enumeration:
867 {
868 int i;
869 uint16_t bestfit = opd->FORM.Enum.SupportedValue[0].u16;
Linus Walleij2c1bbd62009-11-07 15:15:03 +0000870
Linus Walleij29559562007-08-22 21:38:04 +0000871 for (i=0; i<opd->FORM.Enum.NumberOfValues; i++) {
872 if (val == opd->FORM.Enum.SupportedValue[i].u16) {
873 return val;
874 }
875 // Rough guess of best fit
876 if (opd->FORM.Enum.SupportedValue[i].u16 < val) {
877 bestfit = opd->FORM.Enum.SupportedValue[i].u16;
878 }
879 }
880 // Just some default that'll work.
881 return bestfit;
882 }
883 default:
884 // Will accept any value
885 break;
886 }
887 return val;
888}
889
Linus Walleij7783b9b2007-09-22 22:10:44 +0000890/**
891 * This function will do its best to fit a 32bit
892 * value into a PTP object property if the property
893 * is limited in range or step sizes.
894 */
Linus Walleij29559562007-08-22 21:38:04 +0000895static uint32_t adjust_u32(uint32_t val, PTPObjectPropDesc *opd)
896{
897 switch (opd->FormFlag) {
898 case PTP_DPFF_Range:
899 if (val < opd->FORM.Range.MinimumValue.u32) {
900 return opd->FORM.Range.MinimumValue.u32;
901 }
902 if (val > opd->FORM.Range.MaximumValue.u32) {
903 return opd->FORM.Range.MaximumValue.u32;
904 }
905 // Round down to last step.
906 if (val % opd->FORM.Range.StepSize.u32 != 0) {
907 return val - (val % opd->FORM.Range.StepSize.u32);
908 }
909 return val;
910 break;
911 case PTP_DPFF_Enumeration:
912 {
913 int i;
914 uint32_t bestfit = opd->FORM.Enum.SupportedValue[0].u32;
915
916 for (i=0; i<opd->FORM.Enum.NumberOfValues; i++) {
917 if (val == opd->FORM.Enum.SupportedValue[i].u32) {
918 return val;
919 }
920 // Rough guess of best fit
921 if (opd->FORM.Enum.SupportedValue[i].u32 < val) {
922 bestfit = opd->FORM.Enum.SupportedValue[i].u32;
923 }
924 }
925 // Just some default that'll work.
926 return bestfit;
927 }
928 default:
929 // Will accept any value
930 break;
931 }
932 return val;
933}
934
Linus Walleij7783b9b2007-09-22 22:10:44 +0000935/**
Linus Walleij7783b9b2007-09-22 22:10:44 +0000936 * This function returns a newly created ISO 8601 timestamp with the
937 * current time in as high precision as possible. It even adds
938 * the time zone if it can.
939 */
940static char *get_iso8601_stamp(void)
941{
942 time_t curtime;
943 struct tm *loctime;
944 char tmp[64];
945
946 curtime = time(NULL);
947 loctime = localtime(&curtime);
948 strftime (tmp, sizeof(tmp), "%Y%m%dT%H%M%S.0%z", loctime);
949 return strdup(tmp);
950}
951
952/**
Richard Low6f070842009-05-03 10:26:16 +0000953 * Gets the allowed values (range or enum) for a property
954 * @param device a pointer to an MTP device
955 * @param property the property to query
956 * @param filetype the filetype of the object you want to set values for
957 * @param allowed_vals pointer to a LIBMTP_allowed_values_t struct to
958 * receive the allowed values. Call LIBMTP_destroy_allowed_values_t
959 * on this on successful completion.
960 * @return 0 on success, any other value means failure
961 */
962int LIBMTP_Get_Allowed_Property_Values(LIBMTP_mtpdevice_t *device, LIBMTP_property_t const property,
963 LIBMTP_filetype_t const filetype, LIBMTP_allowed_values_t *allowed_vals)
964{
965 PTPObjectPropDesc opd;
966 uint16_t ret = 0;
Linus Walleij2c1bbd62009-11-07 15:15:03 +0000967
Richard Low6f070842009-05-03 10:26:16 +0000968 ret = ptp_mtp_getobjectpropdesc(device->params, map_libmtp_property_to_ptp_property(property), map_libmtp_type_to_ptp_type(filetype), &opd);
969 if (ret != PTP_RC_OK) {
970 add_ptp_error_to_errorstack(device, ret, "LIBMTP_Get_Allowed_Property_Values(): could not get property description.");
971 return -1;
972 }
973
974 if (opd.FormFlag == PTP_OPFF_Enumeration) {
975 int i = 0;
Linus Walleij2c1bbd62009-11-07 15:15:03 +0000976
Richard Low6f070842009-05-03 10:26:16 +0000977 allowed_vals->is_range = 0;
978 allowed_vals->num_entries = opd.FORM.Enum.NumberOfValues;
979
980 switch (opd.DataType)
981 {
982 case PTP_DTC_INT8:
983 allowed_vals->i8vals = malloc(sizeof(int8_t) * opd.FORM.Enum.NumberOfValues);
984 allowed_vals->datatype = LIBMTP_DATATYPE_INT8;
985 break;
986 case PTP_DTC_UINT8:
987 allowed_vals->u8vals = malloc(sizeof(uint8_t) * opd.FORM.Enum.NumberOfValues);
988 allowed_vals->datatype = LIBMTP_DATATYPE_UINT8;
989 break;
990 case PTP_DTC_INT16:
991 allowed_vals->i16vals = malloc(sizeof(int16_t) * opd.FORM.Enum.NumberOfValues);
992 allowed_vals->datatype = LIBMTP_DATATYPE_INT16;
993 break;
994 case PTP_DTC_UINT16:
995 allowed_vals->u16vals = malloc(sizeof(uint16_t) * opd.FORM.Enum.NumberOfValues);
996 allowed_vals->datatype = LIBMTP_DATATYPE_UINT16;
997 break;
998 case PTP_DTC_INT32:
999 allowed_vals->i32vals = malloc(sizeof(int32_t) * opd.FORM.Enum.NumberOfValues);
1000 allowed_vals->datatype = LIBMTP_DATATYPE_INT32;
1001 break;
1002 case PTP_DTC_UINT32:
1003 allowed_vals->u32vals = malloc(sizeof(uint32_t) * opd.FORM.Enum.NumberOfValues);
1004 allowed_vals->datatype = LIBMTP_DATATYPE_UINT32;
1005 break;
1006 case PTP_DTC_INT64:
1007 allowed_vals->i64vals = malloc(sizeof(int64_t) * opd.FORM.Enum.NumberOfValues);
1008 allowed_vals->datatype = LIBMTP_DATATYPE_INT64;
1009 break;
1010 case PTP_DTC_UINT64:
1011 allowed_vals->u64vals = malloc(sizeof(uint64_t) * opd.FORM.Enum.NumberOfValues);
1012 allowed_vals->datatype = LIBMTP_DATATYPE_UINT64;
1013 break;
1014 }
Linus Walleij2c1bbd62009-11-07 15:15:03 +00001015
Richard Low6f070842009-05-03 10:26:16 +00001016 for (i = 0; i < opd.FORM.Enum.NumberOfValues; i++) {
1017 switch (opd.DataType)
1018 {
1019 case PTP_DTC_INT8:
1020 allowed_vals->i8vals[i] = opd.FORM.Enum.SupportedValue[i].i8;
1021 break;
1022 case PTP_DTC_UINT8:
1023 allowed_vals->u8vals[i] = opd.FORM.Enum.SupportedValue[i].u8;
1024 break;
1025 case PTP_DTC_INT16:
1026 allowed_vals->i16vals[i] = opd.FORM.Enum.SupportedValue[i].i16;
1027 break;
1028 case PTP_DTC_UINT16:
1029 allowed_vals->u16vals[i] = opd.FORM.Enum.SupportedValue[i].u16;
1030 break;
1031 case PTP_DTC_INT32:
1032 allowed_vals->i32vals[i] = opd.FORM.Enum.SupportedValue[i].i32;
1033 break;
1034 case PTP_DTC_UINT32:
1035 allowed_vals->u32vals[i] = opd.FORM.Enum.SupportedValue[i].u32;
1036 break;
1037 case PTP_DTC_INT64:
1038 allowed_vals->i64vals[i] = opd.FORM.Enum.SupportedValue[i].i64;
1039 break;
1040 case PTP_DTC_UINT64:
1041 allowed_vals->u64vals[i] = opd.FORM.Enum.SupportedValue[i].u64;
1042 break;
1043 }
1044 }
1045 ptp_free_objectpropdesc(&opd);
1046 return 0;
1047 } else if (opd.FormFlag == PTP_OPFF_Range) {
1048 allowed_vals->is_range = 1;
Linus Walleij2c1bbd62009-11-07 15:15:03 +00001049
Richard Low6f070842009-05-03 10:26:16 +00001050 switch (opd.DataType)
1051 {
1052 case PTP_DTC_INT8:
1053 allowed_vals->i8min = opd.FORM.Range.MinimumValue.i8;
1054 allowed_vals->i8max = opd.FORM.Range.MaximumValue.i8;
1055 allowed_vals->i8step = opd.FORM.Range.StepSize.i8;
1056 allowed_vals->datatype = LIBMTP_DATATYPE_INT8;
1057 break;
1058 case PTP_DTC_UINT8:
1059 allowed_vals->u8min = opd.FORM.Range.MinimumValue.u8;
1060 allowed_vals->u8max = opd.FORM.Range.MaximumValue.u8;
1061 allowed_vals->u8step = opd.FORM.Range.StepSize.u8;
1062 allowed_vals->datatype = LIBMTP_DATATYPE_UINT8;
1063 break;
1064 case PTP_DTC_INT16:
1065 allowed_vals->i16min = opd.FORM.Range.MinimumValue.i16;
1066 allowed_vals->i16max = opd.FORM.Range.MaximumValue.i16;
1067 allowed_vals->i16step = opd.FORM.Range.StepSize.i16;
1068 allowed_vals->datatype = LIBMTP_DATATYPE_INT16;
1069 break;
1070 case PTP_DTC_UINT16:
1071 allowed_vals->u16min = opd.FORM.Range.MinimumValue.u16;
1072 allowed_vals->u16max = opd.FORM.Range.MaximumValue.u16;
1073 allowed_vals->u16step = opd.FORM.Range.StepSize.u16;
1074 allowed_vals->datatype = LIBMTP_DATATYPE_UINT16;
1075 break;
1076 case PTP_DTC_INT32:
1077 allowed_vals->i32min = opd.FORM.Range.MinimumValue.i32;
1078 allowed_vals->i32max = opd.FORM.Range.MaximumValue.i32;
1079 allowed_vals->i32step = opd.FORM.Range.StepSize.i32;
1080 allowed_vals->datatype = LIBMTP_DATATYPE_INT32;
1081 break;
1082 case PTP_DTC_UINT32:
1083 allowed_vals->u32min = opd.FORM.Range.MinimumValue.u32;
1084 allowed_vals->u32max = opd.FORM.Range.MaximumValue.u32;
1085 allowed_vals->u32step = opd.FORM.Range.StepSize.u32;
1086 allowed_vals->datatype = LIBMTP_DATATYPE_UINT32;
1087 break;
1088 case PTP_DTC_INT64:
1089 allowed_vals->i64min = opd.FORM.Range.MinimumValue.i64;
1090 allowed_vals->i64max = opd.FORM.Range.MaximumValue.i64;
1091 allowed_vals->i64step = opd.FORM.Range.StepSize.i64;
1092 allowed_vals->datatype = LIBMTP_DATATYPE_INT64;
1093 break;
1094 case PTP_DTC_UINT64:
1095 allowed_vals->u64min = opd.FORM.Range.MinimumValue.u64;
1096 allowed_vals->u64max = opd.FORM.Range.MaximumValue.u64;
1097 allowed_vals->u64step = opd.FORM.Range.StepSize.u64;
1098 allowed_vals->datatype = LIBMTP_DATATYPE_UINT64;
1099 break;
1100 }
Linus Walleij2c1bbd62009-11-07 15:15:03 +00001101 return 0;
Richard Low6f070842009-05-03 10:26:16 +00001102 } else
1103 return -1;
1104}
1105
1106/**
1107 * Destroys a LIBMTP_allowed_values_t struct
1108 * @param allowed_vals the struct to destroy
1109 */
1110void LIBMTP_destroy_allowed_values_t(LIBMTP_allowed_values_t *allowed_vals)
1111{
1112 if (!allowed_vals->is_range)
1113 {
1114 switch (allowed_vals->datatype)
1115 {
1116 case LIBMTP_DATATYPE_INT8:
1117 if (allowed_vals->i8vals)
1118 free(allowed_vals->i8vals);
1119 break;
1120 case LIBMTP_DATATYPE_UINT8:
1121 if (allowed_vals->u8vals)
1122 free(allowed_vals->u8vals);
1123 break;
1124 case LIBMTP_DATATYPE_INT16:
1125 if (allowed_vals->i16vals)
1126 free(allowed_vals->i16vals);
1127 break;
1128 case LIBMTP_DATATYPE_UINT16:
1129 if (allowed_vals->u16vals)
1130 free(allowed_vals->u16vals);
1131 break;
1132 case LIBMTP_DATATYPE_INT32:
1133 if (allowed_vals->i32vals)
1134 free(allowed_vals->i32vals);
1135 break;
1136 case LIBMTP_DATATYPE_UINT32:
1137 if (allowed_vals->u32vals)
1138 free(allowed_vals->u32vals);
1139 break;
1140 case LIBMTP_DATATYPE_INT64:
1141 if (allowed_vals->i64vals)
1142 free(allowed_vals->i64vals);
1143 break;
1144 case LIBMTP_DATATYPE_UINT64:
1145 if (allowed_vals->u64vals)
1146 free(allowed_vals->u64vals);
1147 break;
1148 }
1149 }
1150}
1151
1152/**
1153 * Determine if a property is supported for a given file type
1154 * @param device a pointer to an MTP device
1155 * @param property the property to query
1156 * @param filetype the filetype of the object you want to set values for
1157 * @return 0 if not supported, positive if supported, negative on error
1158 */
1159int LIBMTP_Is_Property_Supported(LIBMTP_mtpdevice_t *device, LIBMTP_property_t const property,
1160 LIBMTP_filetype_t const filetype)
1161{
1162 uint16_t *props = NULL;
1163 uint32_t propcnt = 0;
1164 uint16_t ret = 0;
1165 int i = 0;
1166 int supported = 0;
1167 uint16_t ptp_prop = map_libmtp_property_to_ptp_property(property);
Linus Walleij2c1bbd62009-11-07 15:15:03 +00001168
Lei Zhangcaa1bbc2013-02-13 15:12:02 -08001169 if (!ptp_operation_issupported(device->params, PTP_OC_MTP_GetObjectPropsSupported))
1170 return 0;
1171
Richard Low6f070842009-05-03 10:26:16 +00001172 ret = ptp_mtp_getobjectpropssupported(device->params, map_libmtp_type_to_ptp_type(filetype), &propcnt, &props);
1173 if (ret != PTP_RC_OK) {
1174 add_ptp_error_to_errorstack(device, ret, "LIBMTP_Is_Property_Supported(): could not get properties supported.");
1175 return -1;
1176 }
1177
1178 for (i = 0; i < propcnt; i++) {
1179 if (props[i] == ptp_prop) {
1180 supported = 1;
1181 break;
1182 }
1183 }
Linus Walleij2c1bbd62009-11-07 15:15:03 +00001184
Richard Low6f070842009-05-03 10:26:16 +00001185 free(props);
Linus Walleij2c1bbd62009-11-07 15:15:03 +00001186
Richard Low6f070842009-05-03 10:26:16 +00001187 return supported;
1188}
1189
1190/**
1191 * Retrieves a string from an object
1192 *
1193 * @param device a pointer to an MTP device.
1194 * @param object_id Object reference
1195 * @param attribute_id MTP attribute ID
1196 * @return valid string or NULL on failure. The returned string
1197 * must bee <code>free()</code>:ed by the caller after
1198 * use.
1199 */
1200char *LIBMTP_Get_String_From_Object(LIBMTP_mtpdevice_t *device, uint32_t const object_id,
1201 LIBMTP_property_t const attribute_id)
1202{
1203 return get_string_from_object(device, object_id, attribute_id);
1204}
1205
1206/**
1207* Retrieves an unsigned 64-bit integer from an object attribute
1208 *
1209 * @param device a pointer to an MTP device.
1210 * @param object_id Object reference
1211 * @param attribute_id MTP attribute ID
1212 * @param value_default Default value to return on failure
1213 * @return the value
1214 */
1215uint64_t LIBMTP_Get_u64_From_Object(LIBMTP_mtpdevice_t *device,uint32_t const object_id,
1216 LIBMTP_property_t const attribute_id, uint64_t const value_default)
1217{
1218 return get_u64_from_object(device, object_id, map_libmtp_property_to_ptp_property(attribute_id), value_default);
1219}
1220
1221/**
1222 * Retrieves an unsigned 32-bit integer from an object attribute
1223 *
1224 * @param device a pointer to an MTP device.
1225 * @param object_id Object reference
1226 * @param attribute_id MTP attribute ID
1227 * @param value_default Default value to return on failure
1228 * @return the value
1229 */
1230uint32_t LIBMTP_Get_u32_From_Object(LIBMTP_mtpdevice_t *device,uint32_t const object_id,
1231 LIBMTP_property_t const attribute_id, uint32_t const value_default)
1232{
1233 return get_u32_from_object(device, object_id, map_libmtp_property_to_ptp_property(attribute_id), value_default);
1234}
1235
1236/**
1237 * Retrieves an unsigned 16-bit integer from an object attribute
1238 *
1239 * @param device a pointer to an MTP device.
1240 * @param object_id Object reference
1241 * @param attribute_id MTP attribute ID
1242 * @param value_default Default value to return on failure
1243 * @return a value
1244 */
1245uint16_t LIBMTP_Get_u16_From_Object(LIBMTP_mtpdevice_t *device, uint32_t const object_id,
1246 LIBMTP_property_t const attribute_id, uint16_t const value_default)
1247{
1248 return get_u16_from_object(device, object_id, map_libmtp_property_to_ptp_property(attribute_id), value_default);
1249}
1250
1251/**
1252 * Retrieves an unsigned 8-bit integer from an object attribute
1253 *
1254 * @param device a pointer to an MTP device.
1255 * @param object_id Object reference
1256 * @param attribute_id MTP attribute ID
1257 * @param value_default Default value to return on failure
1258 * @return a value
1259 */
1260uint8_t LIBMTP_Get_u8_From_Object(LIBMTP_mtpdevice_t *device, uint32_t const object_id,
1261 LIBMTP_property_t const attribute_id, uint8_t const value_default)
1262{
1263 return get_u8_from_object(device, object_id, map_libmtp_property_to_ptp_property(attribute_id), value_default);
1264}
1265
1266/**
1267 * Sets an object attribute from a string
1268 *
1269 * @param device a pointer to an MTP device.
1270 * @param object_id Object reference
1271 * @param attribute_id MTP attribute ID
1272 * @param string string value to set
1273 * @return 0 on success, any other value means failure
1274 */
1275int LIBMTP_Set_Object_String(LIBMTP_mtpdevice_t *device, uint32_t const object_id,
1276 LIBMTP_property_t const attribute_id, char const * const string)
1277{
1278 return set_object_string(device, object_id, map_libmtp_property_to_ptp_property(attribute_id), string);
1279}
1280
1281
1282/**
1283 * Sets an object attribute from an unsigned 32-bit integer
1284 *
1285 * @param device a pointer to an MTP device.
1286 * @param object_id Object reference
1287 * @param attribute_id MTP attribute ID
1288 * @param value 32-bit unsigned integer to set
1289 * @return 0 on success, any other value means failure
1290 */
1291int LIBMTP_Set_Object_u32(LIBMTP_mtpdevice_t *device, uint32_t const object_id,
1292 LIBMTP_property_t const attribute_id, uint32_t const value)
1293{
1294 return set_object_u32(device, object_id, map_libmtp_property_to_ptp_property(attribute_id), value);
1295}
1296
1297/**
1298 * Sets an object attribute from an unsigned 16-bit integer
1299 *
1300 * @param device a pointer to an MTP device.
1301 * @param object_id Object reference
1302 * @param attribute_id MTP attribute ID
1303 * @param value 16-bit unsigned integer to set
1304 * @return 0 on success, any other value means failure
1305 */
1306int LIBMTP_Set_Object_u16(LIBMTP_mtpdevice_t *device, uint32_t const object_id,
1307 LIBMTP_property_t const attribute_id, uint16_t const value)
1308{
1309 return set_object_u16(device, object_id, map_libmtp_property_to_ptp_property(attribute_id), value);
1310}
1311
1312/**
1313 * Sets an object attribute from an unsigned 8-bit integer
1314 *
1315 * @param device a pointer to an MTP device.
1316 * @param object_id Object reference
1317 * @param attribute_id MTP attribute ID
1318 * @param value 8-bit unsigned integer to set
1319 * @return 0 on success, any other value means failure
1320 */
1321int LIBMTP_Set_Object_u8(LIBMTP_mtpdevice_t *device, uint32_t const object_id,
1322 LIBMTP_property_t const attribute_id, uint8_t const value)
1323{
1324 return set_object_u8(device, object_id, map_libmtp_property_to_ptp_property(attribute_id), value);
1325}
1326
1327/**
raveloxd9a28642006-05-26 23:42:22 +00001328 * Retrieves a string from an object
1329 *
1330 * @param device a pointer to an MTP device.
1331 * @param object_id Object reference
1332 * @param attribute_id PTP attribute ID
Linus Walleij438bd7f2006-06-08 11:35:44 +00001333 * @return valid string or NULL on failure. The returned string
1334 * must bee <code>free()</code>:ed by the caller after
1335 * use.
raveloxd9a28642006-05-26 23:42:22 +00001336 */
Linus Walleij9901e222006-11-30 12:28:19 +00001337static char *get_string_from_object(LIBMTP_mtpdevice_t *device, uint32_t const object_id,
Linus Walleij4ef39e62006-09-19 14:11:52 +00001338 uint16_t const attribute_id)
raveloxd9a28642006-05-26 23:42:22 +00001339{
1340 PTPPropertyValue propval;
1341 char *retstring = NULL;
Marcus Meissner9d404be2015-03-22 14:37:54 +01001342 PTPParams *params;
Linus Walleij438bd7f2006-06-08 11:35:44 +00001343 uint16_t ret;
Linus Walleijd4637502009-06-14 23:03:33 +00001344 MTPProperties *prop;
mopoke96143402006-10-30 04:37:26 +00001345
Marcus Meissner9d404be2015-03-22 14:37:54 +01001346 if (!device || !object_id)
Linus Walleijf0f3d482006-05-29 14:10:21 +00001347 return NULL;
Marcus Meissner9d404be2015-03-22 14:37:54 +01001348
1349 params = (PTPParams *) device->params;
Linus Walleij2c1bbd62009-11-07 15:15:03 +00001350
Linus Walleijd4637502009-06-14 23:03:33 +00001351 prop = ptp_find_object_prop_in_cache(params, object_id, attribute_id);
1352 if (prop) {
1353 if (prop->propval.str != NULL)
1354 return strdup(prop->propval.str);
1355 else
1356 return NULL;
Linus Walleij338ade42007-07-03 20:44:08 +00001357 }
mopoke96143402006-10-30 04:37:26 +00001358
Linus Walleija823a702006-08-27 21:27:46 +00001359 ret = ptp_mtp_getobjectpropvalue(params, object_id, attribute_id, &propval, PTP_DTC_STR);
raveloxd9a28642006-05-26 23:42:22 +00001360 if (ret == PTP_RC_OK) {
Linus Walleija823a702006-08-27 21:27:46 +00001361 if (propval.str != NULL) {
1362 retstring = (char *) strdup(propval.str);
1363 free(propval.str);
raveloxd9a28642006-05-26 23:42:22 +00001364 }
Linus Walleij070e9b42007-01-22 08:49:28 +00001365 } else {
1366 add_ptp_error_to_errorstack(device, ret, "get_string_from_object(): could not get object string.");
raveloxd9a28642006-05-26 23:42:22 +00001367 }
mopoke96143402006-10-30 04:37:26 +00001368
raveloxd9a28642006-05-26 23:42:22 +00001369 return retstring;
1370}
1371
1372/**
Richard Lowc3f5fc32007-07-29 09:40:50 +00001373* Retrieves an unsigned 64-bit integer from an object attribute
1374 *
1375 * @param device a pointer to an MTP device.
1376 * @param object_id Object reference
1377 * @param attribute_id PTP attribute ID
1378 * @param value_default Default value to return on failure
1379 * @return the value
1380 */
1381static uint64_t get_u64_from_object(LIBMTP_mtpdevice_t *device,uint32_t const object_id,
1382 uint16_t const attribute_id, uint64_t const value_default)
1383{
1384 PTPPropertyValue propval;
1385 uint64_t retval = value_default;
Marcus Meissner9d404be2015-03-22 14:37:54 +01001386 PTPParams *params;
Richard Lowc3f5fc32007-07-29 09:40:50 +00001387 uint16_t ret;
Linus Walleijd4637502009-06-14 23:03:33 +00001388 MTPProperties *prop;
Linus Walleij2c1bbd62009-11-07 15:15:03 +00001389
Marcus Meissner9d404be2015-03-22 14:37:54 +01001390 if (!device)
Richard Lowc3f5fc32007-07-29 09:40:50 +00001391 return value_default;
Marcus Meissner9d404be2015-03-22 14:37:54 +01001392
1393 params = (PTPParams *) device->params;
Linus Walleij2c1bbd62009-11-07 15:15:03 +00001394
Linus Walleijd4637502009-06-14 23:03:33 +00001395 prop = ptp_find_object_prop_in_cache(params, object_id, attribute_id);
1396 if (prop)
1397 return prop->propval.u64;
Linus Walleij1e9a0332007-09-12 19:35:56 +00001398
Richard Lowc3f5fc32007-07-29 09:40:50 +00001399 ret = ptp_mtp_getobjectpropvalue(params, object_id,
1400 attribute_id,
1401 &propval,
1402 PTP_DTC_UINT64);
1403 if (ret == PTP_RC_OK) {
1404 retval = propval.u64;
1405 } else {
1406 add_ptp_error_to_errorstack(device, ret, "get_u64_from_object(): could not get unsigned 64bit integer from object.");
1407 }
Linus Walleij2c1bbd62009-11-07 15:15:03 +00001408
Richard Lowc3f5fc32007-07-29 09:40:50 +00001409 return retval;
1410}
1411
1412/**
raveloxd9a28642006-05-26 23:42:22 +00001413 * Retrieves an unsigned 32-bit integer from an object attribute
1414 *
1415 * @param device a pointer to an MTP device.
1416 * @param object_id Object reference
1417 * @param attribute_id PTP attribute ID
Linus Walleij5acfa742006-05-29 14:51:59 +00001418 * @param value_default Default value to return on failure
Linus Walleijf0f3d482006-05-29 14:10:21 +00001419 * @return the value
raveloxd9a28642006-05-26 23:42:22 +00001420 */
Linus Walleij9901e222006-11-30 12:28:19 +00001421static uint32_t get_u32_from_object(LIBMTP_mtpdevice_t *device,uint32_t const object_id,
Linus Walleij4ef39e62006-09-19 14:11:52 +00001422 uint16_t const attribute_id, uint32_t const value_default)
raveloxd9a28642006-05-26 23:42:22 +00001423{
1424 PTPPropertyValue propval;
1425 uint32_t retval = value_default;
Marcus Meissner9d404be2015-03-22 14:37:54 +01001426 PTPParams *params;
Linus Walleij438bd7f2006-06-08 11:35:44 +00001427 uint16_t ret;
Linus Walleijd4637502009-06-14 23:03:33 +00001428 MTPProperties *prop;
mopoke96143402006-10-30 04:37:26 +00001429
Marcus Meissner9d404be2015-03-22 14:37:54 +01001430 if (!device)
Linus Walleijf0f3d482006-05-29 14:10:21 +00001431 return value_default;
Marcus Meissner9d404be2015-03-22 14:37:54 +01001432
1433 params = (PTPParams *) device->params;
raveloxd9a28642006-05-26 23:42:22 +00001434
Linus Walleijd4637502009-06-14 23:03:33 +00001435 prop = ptp_find_object_prop_in_cache(params, object_id, attribute_id);
1436 if (prop)
1437 return prop->propval.u32;
Linus Walleij338ade42007-07-03 20:44:08 +00001438
raveloxd9a28642006-05-26 23:42:22 +00001439 ret = ptp_mtp_getobjectpropvalue(params, object_id,
1440 attribute_id,
1441 &propval,
1442 PTP_DTC_UINT32);
1443 if (ret == PTP_RC_OK) {
1444 retval = propval.u32;
Linus Walleij070e9b42007-01-22 08:49:28 +00001445 } else {
1446 add_ptp_error_to_errorstack(device, ret, "get_u32_from_object(): could not get unsigned 32bit integer from object.");
raveloxd9a28642006-05-26 23:42:22 +00001447 }
raveloxd9a28642006-05-26 23:42:22 +00001448 return retval;
1449}
1450
1451/**
1452 * Retrieves an unsigned 16-bit integer from an object attribute
1453 *
1454 * @param device a pointer to an MTP device.
1455 * @param object_id Object reference
1456 * @param attribute_id PTP attribute ID
Linus Walleij5acfa742006-05-29 14:51:59 +00001457 * @param value_default Default value to return on failure
Linus Walleijf0f3d482006-05-29 14:10:21 +00001458 * @return a value
raveloxd9a28642006-05-26 23:42:22 +00001459 */
Linus Walleij9901e222006-11-30 12:28:19 +00001460static uint16_t get_u16_from_object(LIBMTP_mtpdevice_t *device, uint32_t const object_id,
Linus Walleij4ef39e62006-09-19 14:11:52 +00001461 uint16_t const attribute_id, uint16_t const value_default)
raveloxd9a28642006-05-26 23:42:22 +00001462{
1463 PTPPropertyValue propval;
1464 uint16_t retval = value_default;
Marcus Meissner9d404be2015-03-22 14:37:54 +01001465 PTPParams *params;
Linus Walleij438bd7f2006-06-08 11:35:44 +00001466 uint16_t ret;
Linus Walleijd4637502009-06-14 23:03:33 +00001467 MTPProperties *prop;
raveloxd9a28642006-05-26 23:42:22 +00001468
Marcus Meissner9d404be2015-03-22 14:37:54 +01001469 if (!device)
Linus Walleijf0f3d482006-05-29 14:10:21 +00001470 return value_default;
Marcus Meissner9d404be2015-03-22 14:37:54 +01001471
1472 params = (PTPParams *) device->params;
raveloxd9a28642006-05-26 23:42:22 +00001473
Linus Walleij338ade42007-07-03 20:44:08 +00001474 // This O(n) search should not be used so often, since code
1475 // using the cached properties don't usually call this function.
Linus Walleijd4637502009-06-14 23:03:33 +00001476 prop = ptp_find_object_prop_in_cache(params, object_id, attribute_id);
1477 if (prop)
1478 return prop->propval.u16;
Linus Walleij338ade42007-07-03 20:44:08 +00001479
raveloxd9a28642006-05-26 23:42:22 +00001480 ret = ptp_mtp_getobjectpropvalue(params, object_id,
1481 attribute_id,
1482 &propval,
1483 PTP_DTC_UINT16);
1484 if (ret == PTP_RC_OK) {
1485 retval = propval.u16;
Linus Walleij070e9b42007-01-22 08:49:28 +00001486 } else {
1487 add_ptp_error_to_errorstack(device, ret, "get_u16_from_object(): could not get unsigned 16bit integer from object.");
raveloxd9a28642006-05-26 23:42:22 +00001488 }
mopoke96143402006-10-30 04:37:26 +00001489
raveloxd9a28642006-05-26 23:42:22 +00001490 return retval;
1491}
1492
1493/**
Linus Walleij99310d42006-11-01 08:29:39 +00001494 * Retrieves an unsigned 8-bit integer from an object attribute
1495 *
1496 * @param device a pointer to an MTP device.
1497 * @param object_id Object reference
1498 * @param attribute_id PTP attribute ID
1499 * @param value_default Default value to return on failure
1500 * @return a value
1501 */
Linus Walleij9901e222006-11-30 12:28:19 +00001502static uint8_t get_u8_from_object(LIBMTP_mtpdevice_t *device, uint32_t const object_id,
Linus Walleij99310d42006-11-01 08:29:39 +00001503 uint16_t const attribute_id, uint8_t const value_default)
1504{
1505 PTPPropertyValue propval;
1506 uint8_t retval = value_default;
Marcus Meissner9d404be2015-03-22 14:37:54 +01001507 PTPParams *params;
Linus Walleij99310d42006-11-01 08:29:39 +00001508 uint16_t ret;
Linus Walleijd4637502009-06-14 23:03:33 +00001509 MTPProperties *prop;
Linus Walleij99310d42006-11-01 08:29:39 +00001510
Marcus Meissner9d404be2015-03-22 14:37:54 +01001511 if (!device)
Linus Walleij99310d42006-11-01 08:29:39 +00001512 return value_default;
Marcus Meissner9d404be2015-03-22 14:37:54 +01001513
1514 params = (PTPParams *) device->params;
Linus Walleij99310d42006-11-01 08:29:39 +00001515
Linus Walleij338ade42007-07-03 20:44:08 +00001516 // This O(n) search should not be used so often, since code
1517 // using the cached properties don't usually call this function.
Linus Walleijd4637502009-06-14 23:03:33 +00001518 prop = ptp_find_object_prop_in_cache(params, object_id, attribute_id);
1519 if (prop)
1520 return prop->propval.u8;
Linus Walleij338ade42007-07-03 20:44:08 +00001521
Linus Walleij99310d42006-11-01 08:29:39 +00001522 ret = ptp_mtp_getobjectpropvalue(params, object_id,
1523 attribute_id,
1524 &propval,
1525 PTP_DTC_UINT8);
1526 if (ret == PTP_RC_OK) {
1527 retval = propval.u8;
Linus Walleij070e9b42007-01-22 08:49:28 +00001528 } else {
1529 add_ptp_error_to_errorstack(device, ret, "get_u8_from_object(): could not get unsigned 8bit integer from object.");
Linus Walleij99310d42006-11-01 08:29:39 +00001530 }
1531
1532 return retval;
1533}
1534
1535/**
raveloxd9a28642006-05-26 23:42:22 +00001536 * Sets an object attribute from a string
1537 *
1538 * @param device a pointer to an MTP device.
1539 * @param object_id Object reference
1540 * @param attribute_id PTP attribute ID
1541 * @param string string value to set
Linus Walleijf0f3d482006-05-29 14:10:21 +00001542 * @return 0 on success, any other value means failure
raveloxd9a28642006-05-26 23:42:22 +00001543 */
Linus Walleij9901e222006-11-30 12:28:19 +00001544static int set_object_string(LIBMTP_mtpdevice_t *device, uint32_t const object_id,
Linus Walleij4ef39e62006-09-19 14:11:52 +00001545 uint16_t const attribute_id, char const * const string)
raveloxd9a28642006-05-26 23:42:22 +00001546{
1547 PTPPropertyValue propval;
Marcus Meissner9d404be2015-03-22 14:37:54 +01001548 PTPParams *params;
Linus Walleij438bd7f2006-06-08 11:35:44 +00001549 uint16_t ret;
raveloxd9a28642006-05-26 23:42:22 +00001550
Marcus Meissner9d404be2015-03-22 14:37:54 +01001551 if (!device || !string)
Linus Walleij438bd7f2006-06-08 11:35:44 +00001552 return -1;
Marcus Meissner9d404be2015-03-22 14:37:54 +01001553
1554 params = (PTPParams *) device->params;
Linus Walleij2c1bbd62009-11-07 15:15:03 +00001555
Linus Walleijb2753182007-02-26 08:11:38 +00001556 if (!ptp_operation_issupported(params,PTP_OC_MTP_SetObjectPropValue)) {
1557 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "set_object_string(): could not set object string: "
1558 "PTP_OC_MTP_SetObjectPropValue not supported.");
1559 return -1;
1560 }
Linus Walleija823a702006-08-27 21:27:46 +00001561 propval.str = (char *) string;
1562 ret = ptp_mtp_setobjectpropvalue(params, object_id, attribute_id, &propval, PTP_DTC_STR);
Linus Walleijf0f3d482006-05-29 14:10:21 +00001563 if (ret != PTP_RC_OK) {
Linus Walleij070e9b42007-01-22 08:49:28 +00001564 add_ptp_error_to_errorstack(device, ret, "set_object_string(): could not set object string.");
Linus Walleij438bd7f2006-06-08 11:35:44 +00001565 return -1;
Linus Walleijf0f3d482006-05-29 14:10:21 +00001566 }
mopoke96143402006-10-30 04:37:26 +00001567
Linus Walleijf0f3d482006-05-29 14:10:21 +00001568 return 0;
raveloxd9a28642006-05-26 23:42:22 +00001569}
1570
Linus Walleij29559562007-08-22 21:38:04 +00001571
raveloxd9a28642006-05-26 23:42:22 +00001572/**
1573 * Sets an object attribute from an unsigned 32-bit integer
1574 *
1575 * @param device a pointer to an MTP device.
1576 * @param object_id Object reference
1577 * @param attribute_id PTP attribute ID
1578 * @param value 32-bit unsigned integer to set
Linus Walleijf0f3d482006-05-29 14:10:21 +00001579 * @return 0 on success, any other value means failure
raveloxd9a28642006-05-26 23:42:22 +00001580 */
Linus Walleij9901e222006-11-30 12:28:19 +00001581static int set_object_u32(LIBMTP_mtpdevice_t *device, uint32_t const object_id,
Linus Walleij4ef39e62006-09-19 14:11:52 +00001582 uint16_t const attribute_id, uint32_t const value)
raveloxd9a28642006-05-26 23:42:22 +00001583{
1584 PTPPropertyValue propval;
Marcus Meissner9d404be2015-03-22 14:37:54 +01001585 PTPParams *params;
Linus Walleij438bd7f2006-06-08 11:35:44 +00001586 uint16_t ret;
raveloxd9a28642006-05-26 23:42:22 +00001587
Marcus Meissner9d404be2015-03-22 14:37:54 +01001588 if (!device)
Linus Walleij438bd7f2006-06-08 11:35:44 +00001589 return -1;
Marcus Meissner9d404be2015-03-22 14:37:54 +01001590
1591 params = (PTPParams *) device->params;
raveloxd9a28642006-05-26 23:42:22 +00001592
Linus Walleijb2753182007-02-26 08:11:38 +00001593 if (!ptp_operation_issupported(params,PTP_OC_MTP_SetObjectPropValue)) {
1594 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "set_object_u32(): could not set unsigned 32bit integer property: "
1595 "PTP_OC_MTP_SetObjectPropValue not supported.");
1596 return -1;
1597 }
Linus Walleij2c1bbd62009-11-07 15:15:03 +00001598
raveloxd9a28642006-05-26 23:42:22 +00001599 propval.u32 = value;
1600 ret = ptp_mtp_setobjectpropvalue(params, object_id, attribute_id, &propval, PTP_DTC_UINT32);
Linus Walleijf0f3d482006-05-29 14:10:21 +00001601 if (ret != PTP_RC_OK) {
Linus Walleij070e9b42007-01-22 08:49:28 +00001602 add_ptp_error_to_errorstack(device, ret, "set_object_u32(): could not set unsigned 32bit integer property.");
Linus Walleij438bd7f2006-06-08 11:35:44 +00001603 return -1;
Linus Walleijf0f3d482006-05-29 14:10:21 +00001604 }
mopoke96143402006-10-30 04:37:26 +00001605
Linus Walleijf0f3d482006-05-29 14:10:21 +00001606 return 0;
raveloxd9a28642006-05-26 23:42:22 +00001607}
1608
1609/**
1610 * Sets an object attribute from an unsigned 16-bit integer
1611 *
1612 * @param device a pointer to an MTP device.
1613 * @param object_id Object reference
1614 * @param attribute_id PTP attribute ID
1615 * @param value 16-bit unsigned integer to set
Linus Walleijf0f3d482006-05-29 14:10:21 +00001616 * @return 0 on success, any other value means failure
raveloxd9a28642006-05-26 23:42:22 +00001617 */
Linus Walleij9901e222006-11-30 12:28:19 +00001618static int set_object_u16(LIBMTP_mtpdevice_t *device, uint32_t const object_id,
Linus Walleij4ef39e62006-09-19 14:11:52 +00001619 uint16_t const attribute_id, uint16_t const value)
raveloxd9a28642006-05-26 23:42:22 +00001620{
1621 PTPPropertyValue propval;
Marcus Meissner9d404be2015-03-22 14:37:54 +01001622 PTPParams *params;
Linus Walleij438bd7f2006-06-08 11:35:44 +00001623 uint16_t ret;
raveloxd9a28642006-05-26 23:42:22 +00001624
Marcus Meissner9d404be2015-03-22 14:37:54 +01001625 if (!device)
Linus Walleijf0f3d482006-05-29 14:10:21 +00001626 return 1;
Marcus Meissner9d404be2015-03-22 14:37:54 +01001627
1628 params = (PTPParams *) device->params;
raveloxd9a28642006-05-26 23:42:22 +00001629
Linus Walleijb2753182007-02-26 08:11:38 +00001630 if (!ptp_operation_issupported(params,PTP_OC_MTP_SetObjectPropValue)) {
1631 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "set_object_u16(): could not set unsigned 16bit integer property: "
1632 "PTP_OC_MTP_SetObjectPropValue not supported.");
1633 return -1;
1634 }
raveloxd9a28642006-05-26 23:42:22 +00001635 propval.u16 = value;
1636 ret = ptp_mtp_setobjectpropvalue(params, object_id, attribute_id, &propval, PTP_DTC_UINT16);
Linus Walleijf0f3d482006-05-29 14:10:21 +00001637 if (ret != PTP_RC_OK) {
Linus Walleij070e9b42007-01-22 08:49:28 +00001638 add_ptp_error_to_errorstack(device, ret, "set_object_u16(): could not set unsigned 16bit integer property.");
Linus Walleijf0f3d482006-05-29 14:10:21 +00001639 return 1;
1640 }
raveloxd9a28642006-05-26 23:42:22 +00001641
Linus Walleijf0f3d482006-05-29 14:10:21 +00001642 return 0;
raveloxd9a28642006-05-26 23:42:22 +00001643}
1644
1645/**
Linus Walleij99310d42006-11-01 08:29:39 +00001646 * Sets an object attribute from an unsigned 8-bit integer
1647 *
1648 * @param device a pointer to an MTP device.
1649 * @param object_id Object reference
1650 * @param attribute_id PTP attribute ID
1651 * @param value 8-bit unsigned integer to set
1652 * @return 0 on success, any other value means failure
1653 */
Linus Walleij9901e222006-11-30 12:28:19 +00001654static int set_object_u8(LIBMTP_mtpdevice_t *device, uint32_t const object_id,
1655 uint16_t const attribute_id, uint8_t const value)
Linus Walleij99310d42006-11-01 08:29:39 +00001656{
1657 PTPPropertyValue propval;
Marcus Meissner9d404be2015-03-22 14:37:54 +01001658 PTPParams *params;
Linus Walleij99310d42006-11-01 08:29:39 +00001659 uint16_t ret;
1660
Marcus Meissner9d404be2015-03-22 14:37:54 +01001661 if (!device)
Linus Walleij99310d42006-11-01 08:29:39 +00001662 return 1;
Marcus Meissner9d404be2015-03-22 14:37:54 +01001663
1664 params = (PTPParams *) device->params;
Linus Walleij99310d42006-11-01 08:29:39 +00001665
Linus Walleijb2753182007-02-26 08:11:38 +00001666 if (!ptp_operation_issupported(params,PTP_OC_MTP_SetObjectPropValue)) {
1667 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "set_object_u8(): could not set unsigned 8bit integer property: "
1668 "PTP_OC_MTP_SetObjectPropValue not supported.");
1669 return -1;
1670 }
Linus Walleij99310d42006-11-01 08:29:39 +00001671 propval.u8 = value;
1672 ret = ptp_mtp_setobjectpropvalue(params, object_id, attribute_id, &propval, PTP_DTC_UINT8);
1673 if (ret != PTP_RC_OK) {
Linus Walleij070e9b42007-01-22 08:49:28 +00001674 add_ptp_error_to_errorstack(device, ret, "set_object_u8(): could not set unsigned 8bit integer property.");
Linus Walleijf0f3d482006-05-29 14:10:21 +00001675 return 1;
raveloxd9a28642006-05-26 23:42:22 +00001676 }
mopoke96143402006-10-30 04:37:26 +00001677
Linus Walleijf0f3d482006-05-29 14:10:21 +00001678 return 0;
raveloxd9a28642006-05-26 23:42:22 +00001679}
1680
raveloxd9a28642006-05-26 23:42:22 +00001681/**
Linus Walleij039d1dd2007-02-23 22:34:07 +00001682 * Get the first (as in "first in the list of") connected MTP device.
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001683 * @return a device pointer.
Linus Walleij039d1dd2007-02-23 22:34:07 +00001684 * @see LIBMTP_Get_Connected_Devices()
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001685 */
Linus Walleijb9256fd2006-02-15 09:40:43 +00001686LIBMTP_mtpdevice_t *LIBMTP_Get_First_Device(void)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001687{
tedbullocke7713642007-02-18 23:10:09 +00001688 LIBMTP_mtpdevice_t *first_device = NULL;
Linus Walleij64691b72008-05-31 22:55:18 +00001689 LIBMTP_raw_device_t *devices;
1690 int numdevs;
1691 LIBMTP_error_number_t ret;
Linus Walleij2c1bbd62009-11-07 15:15:03 +00001692
Linus Walleij64691b72008-05-31 22:55:18 +00001693 ret = LIBMTP_Detect_Raw_Devices(&devices, &numdevs);
1694 if (ret != LIBMTP_ERROR_NONE) {
Richard Low0f794762007-02-23 22:06:27 +00001695 return NULL;
Linus Walleija8a19cc2007-02-02 22:13:17 +00001696 }
tedbullock44b0ff72007-02-22 22:20:28 +00001697
Linus Walleij64691b72008-05-31 22:55:18 +00001698 if (devices == NULL || numdevs == 0) {
Marcus Meissnerb5214772015-10-04 13:08:20 +02001699 free(devices);
Linus Walleij64691b72008-05-31 22:55:18 +00001700 return NULL;
tedbullock44b0ff72007-02-22 22:20:28 +00001701 }
Linus Walleij64691b72008-05-31 22:55:18 +00001702
1703 first_device = LIBMTP_Open_Raw_Device(&devices[0]);
1704 free(devices);
tedbullocke7713642007-02-18 23:10:09 +00001705 return first_device;
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001706}
1707
1708/**
Linus Walleijd1e14e02008-12-14 01:07:30 +00001709 * Overriding debug function.
1710 * This way we can disable debug prints.
1711 */
1712static void
1713#ifdef __GNUC__
1714__attribute__((__format__(printf,2,0)))
1715#endif
1716LIBMTP_ptp_debug(void *data, const char *format, va_list args)
1717{
nicklas7903584082009-09-28 18:20:16 +00001718 if ((LIBMTP_debug & LIBMTP_DEBUG_PTP) != 0) {
nicklas79daadbf22009-09-28 18:19:34 +00001719 vfprintf (stderr, format, args);
Catalin Patuleada25de02012-07-19 00:57:42 -04001720 fprintf (stderr, "\n");
nicklas79daadbf22009-09-28 18:19:34 +00001721 fflush (stderr);
1722 }
Linus Walleijd1e14e02008-12-14 01:07:30 +00001723}
1724
1725/**
1726 * Overriding error function.
1727 * This way we can capture all error etc to our errorstack.
1728 */
1729static void
1730#ifdef __GNUC__
1731__attribute__((__format__(printf,2,0)))
1732#endif
1733LIBMTP_ptp_error(void *data, const char *format, va_list args)
1734{
1735 // if (data == NULL) {
1736 vfprintf (stderr, format, args);
1737 fflush (stderr);
1738 /*
1739 FIXME: find out how we shall get the device here.
1740 } else {
1741 PTP_USB *ptp_usb = data;
1742 LIBMTP_mtpdevice_t *device = ...;
1743 char buf[2048];
1744
1745 vsnprintf (buf, sizeof (buf), format, args);
1746 add_error_to_errorstack(device,
1747 LIBMTP_ERROR_PTP_LAYER,
1748 buf);
1749 }
1750 */
1751}
1752
1753/**
Linus Walleij38d7ee82010-07-24 22:17:21 +00001754 * Parses the extension descriptor, there may be stuff in
1755 * this that we want to know about.
1756 */
1757static void parse_extension_descriptor(LIBMTP_mtpdevice_t *mtpdevice,
1758 char *desc)
1759{
1760 int start = 0;
1761 int end = 0;
1762
Lei Zhang766bcb82013-02-13 15:12:30 -08001763 /* NULL on Canon A70 */
1764 if (!desc)
1765 return;
1766
Linus Walleij38d7ee82010-07-24 22:17:21 +00001767 /* descriptors are divided by semicolons */
1768 while (end < strlen(desc)) {
Linus Walleij03f0c032011-10-22 00:01:14 +02001769 /* Skip past initial whitespace */
Marcus Meissner1f1d8652015-04-07 23:30:53 +02001770 while ((end < strlen(desc)) && (desc[start] == ' ' )) {
Linus Walleij03f0c032011-10-22 00:01:14 +02001771 start++;
1772 end++;
1773 }
1774 /* Detect extension */
Marcus Meissner1f1d8652015-04-07 23:30:53 +02001775 while ((end < strlen(desc)) && (desc[end] != ';'))
Linus Walleij38d7ee82010-07-24 22:17:21 +00001776 end++;
1777 if (end < strlen(desc)) {
1778 char *element = strndup(desc + start, end-start);
1779 if (element) {
1780 int i = 0;
1781 // printf(" Element: \"%s\"\n", element);
1782
1783 /* Parse for an extension */
Marcus Meissner1f1d8652015-04-07 23:30:53 +02001784 while ((i < strlen(element)) && (element[i] != ':'))
Linus Walleij38d7ee82010-07-24 22:17:21 +00001785 i++;
1786 if (i < strlen(element)) {
1787 char *name = strndup(element, i);
Marcus Meissnerf93179d2016-10-24 22:42:38 +02001788 int major = 0, minor = 0;
Linus Walleij38d7ee82010-07-24 22:17:21 +00001789
Marcus Meissnerf93179d2016-10-24 22:42:38 +02001790 /* extension versions have to be MAJOR.MINOR, but Samsung has one
1791 * with just 0, so just cope with those cases too */
1792 if ( (2 == sscanf(element+i+1,"%d.%d",&major,&minor)) ||
1793 (1 == sscanf(element+i+1,"%d",&major))
1794 ) {
Linus Walleij38d7ee82010-07-24 22:17:21 +00001795 LIBMTP_device_extension_t *extension;
Marcus Meissnerf93179d2016-10-24 22:42:38 +02001796
Linus Walleij38d7ee82010-07-24 22:17:21 +00001797 extension = malloc(sizeof(LIBMTP_device_extension_t));
1798 extension->name = name;
1799 extension->major = major;
1800 extension->minor = minor;
1801 extension->next = NULL;
1802 if (mtpdevice->extensions == NULL) {
1803 mtpdevice->extensions = extension;
1804 } else {
1805 LIBMTP_device_extension_t *tmp = mtpdevice->extensions;
1806 while (tmp->next != NULL)
1807 tmp = tmp->next;
1808 tmp->next = extension;
1809 }
Linus Walleij38d7ee82010-07-24 22:17:21 +00001810 } else {
1811 LIBMTP_ERROR("LIBMTP ERROR: couldnt parse extension %s\n",
1812 element);
1813 }
1814 }
1815 free(element);
1816 }
1817 }
1818 end++;
1819 start = end;
1820 }
1821}
1822
1823/**
Linus Walleijbdb89bd2008-05-28 23:32:35 +00001824 * This function opens a device from a raw device. It is the
1825 * preferred way to access devices in the new interface where
1826 * several devices can come and go as the library is working
1827 * on a certain device.
1828 * @param rawdevice the raw device to open a "real" device for.
1829 * @return an open device.
1830 */
Linus Walleija8b88892011-03-03 19:58:26 +01001831LIBMTP_mtpdevice_t *LIBMTP_Open_Raw_Device_Uncached(LIBMTP_raw_device_t *rawdevice)
Linus Walleijbdb89bd2008-05-28 23:32:35 +00001832{
1833 LIBMTP_mtpdevice_t *mtp_device;
1834 uint8_t bs = 0;
1835 PTPParams *current_params;
Linus Walleij3c643252008-05-31 23:22:28 +00001836 PTP_USB *ptp_usb;
Linus Walleijbdb89bd2008-05-28 23:32:35 +00001837 LIBMTP_error_number_t err;
1838 int i;
1839
1840 /* Allocate dynamic space for our device */
1841 mtp_device = (LIBMTP_mtpdevice_t *) malloc(sizeof(LIBMTP_mtpdevice_t));
1842 /* Check if there was a memory allocation error */
1843 if(mtp_device == NULL) {
1844 /* There has been an memory allocation error. We are going to ignore this
1845 device and attempt to continue */
Linus Walleij2c1bbd62009-11-07 15:15:03 +00001846
Linus Walleijbdb89bd2008-05-28 23:32:35 +00001847 /* TODO: This error statement could probably be a bit more robust */
nicklas79daadbf22009-09-28 18:19:34 +00001848 LIBMTP_ERROR("LIBMTP PANIC: connect_usb_devices encountered a memory "
Linus Walleijbdb89bd2008-05-28 23:32:35 +00001849 "allocation error with device %d on bus %d, trying to continue",
1850 rawdevice->devnum, rawdevice->bus_location);
Linus Walleij2c1bbd62009-11-07 15:15:03 +00001851
Linus Walleijbdb89bd2008-05-28 23:32:35 +00001852 return NULL;
1853 }
Marcus Meissner097a0522015-03-22 14:30:36 +01001854 memset(mtp_device, 0, sizeof(LIBMTP_mtpdevice_t));
Linus Walleija8b88892011-03-03 19:58:26 +01001855 // Non-cached by default
1856 mtp_device->cached = 0;
Linus Walleij3c643252008-05-31 23:22:28 +00001857
1858 /* Create PTP params */
1859 current_params = (PTPParams *) malloc(sizeof(PTPParams));
1860 if (current_params == NULL) {
1861 free(mtp_device);
Linus Walleijbdb89bd2008-05-28 23:32:35 +00001862 return NULL;
Linus Walleij3c643252008-05-31 23:22:28 +00001863 }
1864 memset(current_params, 0, sizeof(PTPParams));
Linus Walleijd4637502009-06-14 23:03:33 +00001865 current_params->device_flags = rawdevice->device_entry.device_flags;
1866 current_params->nrofobjects = 0;
Marcus Meissneraa7d91a2017-03-16 15:59:48 +01001867 current_params->cachetime = 2;
Linus Walleijd4637502009-06-14 23:03:33 +00001868 current_params->objects = NULL;
1869 current_params->response_packet_size = 0;
1870 current_params->response_packet = NULL;
Linus Walleijd1e14e02008-12-14 01:07:30 +00001871 /* This will be a pointer to PTP_USB later */
1872 current_params->data = NULL;
1873 /* Set upp local debug and error functions */
1874 current_params->debug_func = LIBMTP_ptp_debug;
1875 current_params->error_func = LIBMTP_ptp_error;
Linus Walleij3c643252008-05-31 23:22:28 +00001876 /* TODO: Will this always be little endian? */
1877 current_params->byteorder = PTP_DL_LE;
1878 current_params->cd_locale_to_ucs2 = iconv_open("UCS-2LE", "UTF-8");
1879 current_params->cd_ucs2_to_locale = iconv_open("UTF-8", "UCS-2LE");
Linus Walleij2c1bbd62009-11-07 15:15:03 +00001880
Linus Walleij3c643252008-05-31 23:22:28 +00001881 if(current_params->cd_locale_to_ucs2 == (iconv_t) -1 ||
1882 current_params->cd_ucs2_to_locale == (iconv_t) -1) {
nicklas79daadbf22009-09-28 18:19:34 +00001883 LIBMTP_ERROR("LIBMTP PANIC: Cannot open iconv() converters to/from UCS-2!\n"
Linus Walleij3c643252008-05-31 23:22:28 +00001884 "Too old stdlibc, glibc and libiconv?\n");
1885 free(current_params);
1886 free(mtp_device);
1887 return NULL;
1888 }
1889 mtp_device->params = current_params;
1890
Linus Walleij3c643252008-05-31 23:22:28 +00001891 /* Create usbinfo, this also opens the session */
1892 err = configure_usb_device(rawdevice,
1893 current_params,
1894 &mtp_device->usbinfo);
1895 if (err != LIBMTP_ERROR_NONE) {
1896 free(current_params);
1897 free(mtp_device);
1898 return NULL;
1899 }
1900 ptp_usb = (PTP_USB*) mtp_device->usbinfo;
1901 /* Set pointer back to params */
1902 ptp_usb->params = current_params;
1903
Linus Walleijbdb89bd2008-05-28 23:32:35 +00001904 /* Cache the device information for later use */
1905 if (ptp_getdeviceinfo(current_params,
1906 &current_params->deviceinfo) != PTP_RC_OK) {
nicklas79daadbf22009-09-28 18:19:34 +00001907 LIBMTP_ERROR("LIBMTP PANIC: Unable to read device information on device "
Linus Walleijbdb89bd2008-05-28 23:32:35 +00001908 "%d on bus %d, trying to continue",
1909 rawdevice->devnum, rawdevice->bus_location);
Linus Walleij2c1bbd62009-11-07 15:15:03 +00001910
Linus Walleijbdb89bd2008-05-28 23:32:35 +00001911 /* Prevent memory leaks for this device */
1912 free(mtp_device->usbinfo);
1913 free(mtp_device->params);
1914 current_params = NULL;
Linus Walleij2c1bbd62009-11-07 15:15:03 +00001915 free(mtp_device);
Linus Walleijbdb89bd2008-05-28 23:32:35 +00001916 return NULL;
1917 }
Linus Walleij2c1bbd62009-11-07 15:15:03 +00001918
Linus Walleij14735952010-04-25 04:56:49 +00001919 /* Check: if this is a PTP device, is it really tagged as MTP? */
1920 if (current_params->deviceinfo.VendorExtensionID != 0x00000006) {
1921 LIBMTP_ERROR("LIBMTP WARNING: no MTP vendor extension on device "
1922 "%d on bus %d",
1923 rawdevice->devnum, rawdevice->bus_location);
1924 LIBMTP_ERROR("LIBMTP WARNING: VendorExtensionID: %08x",
1925 current_params->deviceinfo.VendorExtensionID);
1926 LIBMTP_ERROR("LIBMTP WARNING: VendorExtensionDesc: %s",
1927 current_params->deviceinfo.VendorExtensionDesc);
1928 LIBMTP_ERROR("LIBMTP WARNING: this typically means the device is PTP "
1929 "(i.e. a camera) but not an MTP device at all. "
1930 "Trying to continue anyway.");
1931 }
1932
Linus Walleij38d7ee82010-07-24 22:17:21 +00001933 parse_extension_descriptor(mtp_device,
1934 current_params->deviceinfo.VendorExtensionDesc);
1935
Linus Walleija566f602010-07-24 22:46:36 +00001936 /*
Linus Walleij03f0c032011-10-22 00:01:14 +02001937 * Android has a number of bugs, force-assign these bug flags
Linus Walleij15c7d1b2012-01-07 01:37:31 +01001938 * if Android is encountered. Same thing for devices we detect
1939 * as SONY NWZ Walkmen. I have no clue what "sony.net/WMFU" means
1940 * I just know only NWZs have it.
Linus Walleij03f0c032011-10-22 00:01:14 +02001941 */
1942 {
1943 LIBMTP_device_extension_t *tmpext = mtp_device->extensions;
Linus Walleij4c570a92012-07-17 22:33:58 +02001944 int is_microsoft_com_wpdna = 0;
1945 int is_android = 0;
1946 int is_sony_net_wmfu = 0;
1947 int is_sonyericsson_com_se = 0;
Linus Walleij03f0c032011-10-22 00:01:14 +02001948
Linus Walleij4c570a92012-07-17 22:33:58 +02001949 /* Loop over extensions and set flags */
Linus Walleij03f0c032011-10-22 00:01:14 +02001950 while (tmpext != NULL) {
Linus Walleij4c570a92012-07-17 22:33:58 +02001951 if (!strcmp(tmpext->name, "microsoft.com/WPDNA"))
1952 is_microsoft_com_wpdna = 1;
1953 if (!strcmp(tmpext->name, "android.com"))
1954 is_android = 1;
1955 if (!strcmp(tmpext->name, "sony.net/WMFU"))
1956 is_sony_net_wmfu = 1;
1957 if (!strcmp(tmpext->name, "sonyericsson.com/SE"))
1958 is_sonyericsson_com_se = 1;
Linus Walleij03f0c032011-10-22 00:01:14 +02001959 tmpext = tmpext->next;
1960 }
Linus Walleij4c570a92012-07-17 22:33:58 +02001961
1962 /* Check for specific stacks */
1963 if (is_microsoft_com_wpdna && is_sonyericsson_com_se && !is_android) {
1964 /*
1965 * The Aricent stack seems to be detected by providing WPDNA, the SonyEricsson
1966 * extension and NO Android extension.
1967 */
1968 ptp_usb->rawdevice.device_entry.device_flags |= DEVICE_FLAGS_ARICENT_BUGS;
1969 LIBMTP_INFO("Aricent MTP stack device detected, assigning default bug flags\n");
1970 }
1971 else if (is_android) {
1972 /*
1973 * If bugs are fixed in later versions, test on tmpext->major, tmpext->minor
1974 */
1975 ptp_usb->rawdevice.device_entry.device_flags |= DEVICE_FLAGS_ANDROID_BUGS;
1976 LIBMTP_INFO("Android device detected, assigning default bug flags\n");
1977 }
1978 else if (is_sony_net_wmfu) {
1979 ptp_usb->rawdevice.device_entry.device_flags |= DEVICE_FLAGS_SONY_NWZ_BUGS;
1980 LIBMTP_INFO("SONY NWZ device detected, assigning default bug flags\n");
1981 }
Linus Walleij03f0c032011-10-22 00:01:14 +02001982 }
1983
1984 /*
Linus Walleija566f602010-07-24 22:46:36 +00001985 * If the OGG or FLAC filetypes are flagged as "unknown", check
1986 * if the firmware has been updated to actually support it.
1987 */
1988 if (FLAG_OGG_IS_UNKNOWN(ptp_usb)) {
1989 for (i=0;i<current_params->deviceinfo.ImageFormats_len;i++) {
1990 if (current_params->deviceinfo.ImageFormats[i] == PTP_OFC_MTP_OGG) {
1991 /* This is not unknown anymore, unflag it */
Linus Walleija8b88892011-03-03 19:58:26 +01001992 ptp_usb->rawdevice.device_entry.device_flags &=
Linus Walleija566f602010-07-24 22:46:36 +00001993 ~DEVICE_FLAG_OGG_IS_UNKNOWN;
1994 break;
1995 }
1996 }
1997 }
1998 if (FLAG_FLAC_IS_UNKNOWN(ptp_usb)) {
1999 for (i=0;i<current_params->deviceinfo.ImageFormats_len;i++) {
2000 if (current_params->deviceinfo.ImageFormats[i] == PTP_OFC_MTP_FLAC) {
2001 /* This is not unknown anymore, unflag it */
Linus Walleija8b88892011-03-03 19:58:26 +01002002 ptp_usb->rawdevice.device_entry.device_flags &=
Linus Walleija566f602010-07-24 22:46:36 +00002003 ~DEVICE_FLAG_FLAC_IS_UNKNOWN;
2004 break;
2005 }
2006 }
2007 }
2008
Linus Walleijbdb89bd2008-05-28 23:32:35 +00002009 /* Determine if the object size supported is 32 or 64 bit wide */
Lei Zhangcaa1bbc2013-02-13 15:12:02 -08002010 if (ptp_operation_issupported(current_params,PTP_OC_MTP_GetObjectPropsSupported)) {
2011 for (i=0;i<current_params->deviceinfo.ImageFormats_len;i++) {
2012 PTPObjectPropDesc opd;
Linus Walleij2c1bbd62009-11-07 15:15:03 +00002013
Lei Zhangcaa1bbc2013-02-13 15:12:02 -08002014 if (ptp_mtp_getobjectpropdesc(current_params,
2015 PTP_OPC_ObjectSize,
2016 current_params->deviceinfo.ImageFormats[i],
2017 &opd) != PTP_RC_OK) {
2018 LIBMTP_ERROR("LIBMTP PANIC: "
2019 "could not inspect object property descriptions!\n");
Linus Walleijbdb89bd2008-05-28 23:32:35 +00002020 } else {
Lei Zhangcaa1bbc2013-02-13 15:12:02 -08002021 if (opd.DataType == PTP_DTC_UINT32) {
2022 if (bs == 0) {
2023 bs = 32;
2024 } else if (bs != 32) {
2025 LIBMTP_ERROR("LIBMTP PANIC: "
2026 "different objects support different object sizes!\n");
2027 bs = 0;
2028 break;
2029 }
2030 } else if (opd.DataType == PTP_DTC_UINT64) {
2031 if (bs == 0) {
2032 bs = 64;
2033 } else if (bs != 64) {
2034 LIBMTP_ERROR("LIBMTP PANIC: "
2035 "different objects support different object sizes!\n");
2036 bs = 0;
2037 break;
2038 }
2039 } else {
2040 // Ignore if other size.
2041 LIBMTP_ERROR("LIBMTP PANIC: "
2042 "awkward object size data type: %04x\n", opd.DataType);
2043 bs = 0;
2044 break;
2045 }
Linus Walleijbdb89bd2008-05-28 23:32:35 +00002046 }
2047 }
2048 }
2049 if (bs == 0) {
2050 // Could not detect object bitsize, assume 32 bits
2051 bs = 32;
2052 }
2053 mtp_device->object_bitsize = bs;
Linus Walleij2c1bbd62009-11-07 15:15:03 +00002054
Linus Walleijbdb89bd2008-05-28 23:32:35 +00002055 /* No Errors yet for this device */
2056 mtp_device->errorstack = NULL;
Linus Walleij2c1bbd62009-11-07 15:15:03 +00002057
Linus Walleijbdb89bd2008-05-28 23:32:35 +00002058 /* Default Max Battery Level, we will adjust this if possible */
2059 mtp_device->maximum_battery_level = 100;
Linus Walleij2c1bbd62009-11-07 15:15:03 +00002060
Linus Walleijbdb89bd2008-05-28 23:32:35 +00002061 /* Check if device supports reading maximum battery level */
Linus Walleij2c1bbd62009-11-07 15:15:03 +00002062 if(!FLAG_BROKEN_BATTERY_LEVEL(ptp_usb) &&
Linus Walleij4096c882009-03-16 23:32:34 +00002063 ptp_property_issupported( current_params, PTP_DPC_BatteryLevel)) {
Linus Walleijbdb89bd2008-05-28 23:32:35 +00002064 PTPDevicePropDesc dpd;
Linus Walleij2c1bbd62009-11-07 15:15:03 +00002065
Linus Walleijbdb89bd2008-05-28 23:32:35 +00002066 /* Try to read maximum battery level */
2067 if(ptp_getdevicepropdesc(current_params,
2068 PTP_DPC_BatteryLevel,
2069 &dpd) != PTP_RC_OK) {
2070 add_error_to_errorstack(mtp_device,
2071 LIBMTP_ERROR_CONNECTING,
2072 "Unable to read Maximum Battery Level for this "
2073 "device even though the device supposedly "
2074 "supports this functionality");
2075 }
Linus Walleij2c1bbd62009-11-07 15:15:03 +00002076
Linus Walleijbdb89bd2008-05-28 23:32:35 +00002077 /* TODO: is this appropriate? */
2078 /* If max battery level is 0 then leave the default, otherwise assign */
2079 if (dpd.FORM.Range.MaximumValue.u8 != 0) {
2080 mtp_device->maximum_battery_level = dpd.FORM.Range.MaximumValue.u8;
2081 }
Linus Walleij2c1bbd62009-11-07 15:15:03 +00002082
Linus Walleijbdb89bd2008-05-28 23:32:35 +00002083 ptp_free_devicepropdesc(&dpd);
2084 }
Linus Walleij2c1bbd62009-11-07 15:15:03 +00002085
Linus Walleij4fc19172010-01-19 00:10:26 +00002086 /* Set all default folders to 0xffffffffU (root directory) */
2087 mtp_device->default_music_folder = 0xffffffffU;
2088 mtp_device->default_playlist_folder = 0xffffffffU;
2089 mtp_device->default_picture_folder = 0xffffffffU;
2090 mtp_device->default_video_folder = 0xffffffffU;
2091 mtp_device->default_organizer_folder = 0xffffffffU;
2092 mtp_device->default_zencast_folder = 0xffffffffU;
2093 mtp_device->default_album_folder = 0xffffffffU;
2094 mtp_device->default_text_folder = 0xffffffffU;
Linus Walleij2c1bbd62009-11-07 15:15:03 +00002095
Linus Walleijbdb89bd2008-05-28 23:32:35 +00002096 /* Set initial storage information */
2097 mtp_device->storage = NULL;
2098 if (LIBMTP_Get_Storage(mtp_device, LIBMTP_STORAGE_SORTBY_NOTSORTED) == -1) {
2099 add_error_to_errorstack(mtp_device,
2100 LIBMTP_ERROR_GENERAL,
2101 "Get Storage information failed.");
2102 mtp_device->storage = NULL;
2103 }
Linus Walleij2c1bbd62009-11-07 15:15:03 +00002104
Linus Walleija8b88892011-03-03 19:58:26 +01002105
2106 return mtp_device;
2107}
2108
2109LIBMTP_mtpdevice_t *LIBMTP_Open_Raw_Device(LIBMTP_raw_device_t *rawdevice)
2110{
2111 LIBMTP_mtpdevice_t *mtp_device = LIBMTP_Open_Raw_Device_Uncached(rawdevice);
2112
2113 if (mtp_device == NULL)
2114 return NULL;
2115
Sajid Anwar8dca41d2012-08-18 20:36:13 +02002116 /* Check for MTPZ devices. */
2117 if (use_mtpz) {
2118 LIBMTP_device_extension_t *tmpext = mtp_device->extensions;
2119
2120 while (tmpext != NULL) {
2121 if (!strcmp(tmpext->name, "microsoft.com/MTPZ")) {
2122 LIBMTP_INFO("MTPZ device detected. Authenticating...\n");
2123 if (PTP_RC_OK == ptp_mtpz_handshake(mtp_device->params)) {
2124 LIBMTP_INFO ("(MTPZ) Successfully authenticated with device.\n");
2125 } else {
2126 LIBMTP_INFO ("(MTPZ) Failure - could not authenticate with device.\n");
2127 }
2128 break;
2129 }
2130 tmpext = tmpext->next;
2131 }
2132 }
2133
Linus Walleija8b88892011-03-03 19:58:26 +01002134 // Set up this device as cached
2135 mtp_device->cached = 1;
Linus Walleijbdb89bd2008-05-28 23:32:35 +00002136 /*
2137 * Then get the handles and try to locate the default folders.
2138 * This has the desired side effect of caching all handles from
2139 * the device which speeds up later operations.
2140 */
2141 flush_handles(mtp_device);
Linus Walleijbdb89bd2008-05-28 23:32:35 +00002142 return mtp_device;
2143}
2144
2145/**
Linus Walleij335b6fb2011-03-22 23:43:32 +01002146 * To read events sent by the device, repeatedly call this function from a secondary
2147 * thread until the return value is < 0.
2148 *
2149 * @param device a pointer to the MTP device to poll for events.
2150 * @param event contains a pointer to be filled in with the event retrieved if the call
2151 * is successful.
Philip Langdaleda268792012-09-02 14:48:25 -07002152 * @param out1 contains the param1 value from the raw event.
Linus Walleij335b6fb2011-03-22 23:43:32 +01002153 * @return 0 on success, any other value means the polling loop shall be
2154 * terminated immediately for this session.
2155 */
Philip Langdaleda268792012-09-02 14:48:25 -07002156int LIBMTP_Read_Event(LIBMTP_mtpdevice_t *device, LIBMTP_event_t *event, uint32_t *out1)
Linus Walleij335b6fb2011-03-22 23:43:32 +01002157{
2158 /*
2159 * FIXME: Potential race-condition here, if client deallocs device
2160 * while we're *not* waiting for input. As we'll be waiting for
2161 * input most of the time, it's unlikely but still worth considering
2162 * for improvement. Also we cannot affect the state of the cache etc
2163 * unless we know we are the sole user on the device. A spinlock or
2164 * mutex in the LIBMTP_mtpdevice_t is needed for this to work.
2165 */
2166 PTPParams *params = (PTPParams *) device->params;
2167 PTPContainer ptp_event;
Linus Walleij6ba03452011-11-14 20:46:36 +01002168 uint16_t ret = ptp_usb_event_wait(params, &ptp_event);
Linus Walleij335b6fb2011-03-22 23:43:32 +01002169
2170 if (ret != PTP_RC_OK) {
2171 /* Device is closing down or other fatal stuff, exit thread */
2172 return -1;
2173 }
Philip Langdale0a576512016-04-09 14:22:25 -07002174 LIBMTP_Handle_Event(&ptp_event, event, out1);
2175 return 0;
2176}
2177
2178void LIBMTP_Handle_Event(PTPContainer *ptp_event,
2179 LIBMTP_event_t *event, uint32_t *out1) {
2180 uint16_t code;
2181 uint32_t session_id;
2182 uint32_t param1;
Linus Walleij335b6fb2011-03-22 23:43:32 +01002183
2184 *event = LIBMTP_EVENT_NONE;
2185
2186 /* Process the event */
Philip Langdale0a576512016-04-09 14:22:25 -07002187 code = ptp_event->Code;
2188 session_id = ptp_event->SessionID;
2189 param1 = ptp_event->Param1;
Linus Walleij335b6fb2011-03-22 23:43:32 +01002190
2191 switch(code) {
2192 case PTP_EC_Undefined:
2193 LIBMTP_INFO("Received event PTP_EC_Undefined in session %u\n", session_id);
2194 break;
2195 case PTP_EC_CancelTransaction:
2196 LIBMTP_INFO("Received event PTP_EC_CancelTransaction in session %u\n", session_id);
2197 break;
2198 case PTP_EC_ObjectAdded:
2199 LIBMTP_INFO("Received event PTP_EC_ObjectAdded in session %u\n", session_id);
Philip Langdale50dd3472013-03-10 12:22:51 -07002200 *event = LIBMTP_EVENT_OBJECT_ADDED;
2201 *out1 = param1;
Linus Walleij335b6fb2011-03-22 23:43:32 +01002202 break;
2203 case PTP_EC_ObjectRemoved:
2204 LIBMTP_INFO("Received event PTP_EC_ObjectRemoved in session %u\n", session_id);
Philip Langdale50dd3472013-03-10 12:22:51 -07002205 *event = LIBMTP_EVENT_OBJECT_REMOVED;
2206 *out1 = param1;
Linus Walleij335b6fb2011-03-22 23:43:32 +01002207 break;
2208 case PTP_EC_StoreAdded:
2209 LIBMTP_INFO("Received event PTP_EC_StoreAdded in session %u\n", session_id);
2210 /* TODO: rescan storages */
Philip Langdaleda268792012-09-02 14:48:25 -07002211 *event = LIBMTP_EVENT_STORE_ADDED;
2212 *out1 = param1;
Linus Walleij335b6fb2011-03-22 23:43:32 +01002213 break;
2214 case PTP_EC_StoreRemoved:
2215 LIBMTP_INFO("Received event PTP_EC_StoreRemoved in session %u\n", session_id);
2216 /* TODO: rescan storages */
Philip Langdale50dd3472013-03-10 12:22:51 -07002217 *event = LIBMTP_EVENT_STORE_REMOVED;
2218 *out1 = param1;
Linus Walleij335b6fb2011-03-22 23:43:32 +01002219 break;
2220 case PTP_EC_DevicePropChanged:
2221 LIBMTP_INFO("Received event PTP_EC_DevicePropChanged in session %u\n", session_id);
2222 /* TODO: update device properties */
Philip Langdale7815a642018-03-24 08:08:43 +01002223 *event = LIBMTP_EVENT_DEVICE_PROPERTY_CHANGED;
2224 *out1 = param1;
Linus Walleij335b6fb2011-03-22 23:43:32 +01002225 break;
2226 case PTP_EC_ObjectInfoChanged:
2227 LIBMTP_INFO("Received event PTP_EC_ObjectInfoChanged in session %u\n", session_id);
2228 /* TODO: rescan object cache or just for this one object */
2229 break;
2230 case PTP_EC_DeviceInfoChanged:
2231 LIBMTP_INFO("Received event PTP_EC_DeviceInfoChanged in session %u\n", session_id);
2232 /* TODO: update device info */
2233 break;
2234 case PTP_EC_RequestObjectTransfer:
2235 LIBMTP_INFO("Received event PTP_EC_RequestObjectTransfer in session %u\n", session_id);
2236 break;
2237 case PTP_EC_StoreFull:
2238 LIBMTP_INFO("Received event PTP_EC_StoreFull in session %u\n", session_id);
2239 break;
2240 case PTP_EC_DeviceReset:
2241 LIBMTP_INFO("Received event PTP_EC_DeviceReset in session %u\n", session_id);
2242 break;
2243 case PTP_EC_StorageInfoChanged :
2244 LIBMTP_INFO( "Received event PTP_EC_StorageInfoChanged in session %u\n", session_id);
2245 /* TODO: update storage info */
2246 break;
2247 case PTP_EC_CaptureComplete :
2248 LIBMTP_INFO( "Received event PTP_EC_CaptureComplete in session %u\n", session_id);
2249 break;
2250 case PTP_EC_UnreportedStatus :
2251 LIBMTP_INFO( "Received event PTP_EC_UnreportedStatus in session %u\n", session_id);
2252 break;
2253 default :
2254 LIBMTP_INFO( "Received unknown event in session %u\n", session_id);
2255 break;
2256 }
Philip Langdale0a576512016-04-09 14:22:25 -07002257}
Linus Walleij335b6fb2011-03-22 23:43:32 +01002258
Philip Langdale0a576512016-04-09 14:22:25 -07002259static void LIBMTP_Read_Event_Cb(PTPParams *params, uint16_t ret_code,
2260 PTPContainer *ptp_event, void *user_data) {
2261 event_cb_data_t *data = user_data;
2262 LIBMTP_event_t event = LIBMTP_EVENT_NONE;
2263 uint32_t param1 = 0;
2264 int handler_ret;
2265
2266 switch (ret_code) {
2267 case PTP_RC_OK:
2268 handler_ret = LIBMTP_HANDLER_RETURN_OK;
2269 LIBMTP_Handle_Event(ptp_event, &event, &param1);
2270 break;
2271 case PTP_ERROR_CANCEL:
2272 handler_ret = LIBMTP_HANDLER_RETURN_CANCEL;
2273 break;
2274 default:
2275 handler_ret = LIBMTP_HANDLER_RETURN_ERROR;
2276 break;
2277 }
2278
2279 data->cb(handler_ret, event, param1, data->user_data);
2280 free(data);
2281}
2282
2283/**
2284 * This function reads events sent by the device, in a non-blocking manner.
2285 * The callback function will be called when an event is received, but for the function
2286 * to make progress, polling must take place, using LIBMTP_Handle_Events_Timeout_Completed.
2287 *
2288 * After an event is received, this function should be called again to listen for the next
2289 * event.
2290 *
2291 * For now, this non-blocking mechanism only works with libusb-1.0, and not any of the
2292 * other usb library backends. Attempting to call this method with another backend will
2293 * always return an error.
2294 *
2295 * @param device a pointer to the MTP device to poll for events.
2296 * @param cb a callback to be invoked when an event is received.
2297 * @param user_data arbitrary user data passed to the callback.
2298 * @return 0 on success, any other value means that the callback was not registered and
2299 * no event notification will take place.
2300 */
2301int LIBMTP_Read_Event_Async(LIBMTP_mtpdevice_t *device, LIBMTP_event_cb_fn cb, void *user_data) {
2302 PTPParams *params = (PTPParams *) device->params;
2303 event_cb_data_t *data = malloc(sizeof(event_cb_data_t));
2304 uint16_t ret;
2305
2306 data->cb = cb;
2307 data->user_data = user_data;
2308
2309 ret = ptp_usb_event_async(params, LIBMTP_Read_Event_Cb, data);
2310 return ret == PTP_RC_OK ? 0 : -1;
Linus Walleij335b6fb2011-03-22 23:43:32 +01002311}
2312
2313/**
Linus Walleij2d3f7b82007-02-14 09:24:20 +00002314 * Recursive function that adds MTP devices to a linked list
Linus Walleija700d222008-05-28 23:06:14 +00002315 * @param devices a list of raw devices to have real devices created for.
Linus Walleij2d3f7b82007-02-14 09:24:20 +00002316 * @return a device pointer to a newly created mtpdevice (used in linked
Linus Walleijb0ab5482007-08-29 21:08:54 +00002317 * list creation).
Linus Walleij2d3f7b82007-02-14 09:24:20 +00002318 */
Linus Walleija700d222008-05-28 23:06:14 +00002319static LIBMTP_mtpdevice_t * create_usb_mtp_devices(LIBMTP_raw_device_t *devices, int numdevs)
Linus Walleijf27d1cd2007-03-05 14:23:00 +00002320{
Linus Walleija700d222008-05-28 23:06:14 +00002321 uint8_t i;
Linus Walleijf27d1cd2007-03-05 14:23:00 +00002322 LIBMTP_mtpdevice_t *mtp_device_list = NULL;
2323 LIBMTP_mtpdevice_t *current_device = NULL;
Linus Walleija700d222008-05-28 23:06:14 +00002324
2325 for (i=0; i < numdevs; i++) {
Linus Walleij2d3f7b82007-02-14 09:24:20 +00002326 LIBMTP_mtpdevice_t *mtp_device;
Linus Walleijbdb89bd2008-05-28 23:32:35 +00002327 mtp_device = LIBMTP_Open_Raw_Device(&devices[i]);
Linus Walleija700d222008-05-28 23:06:14 +00002328
Linus Walleijbdb89bd2008-05-28 23:32:35 +00002329 /* On error, try next device */
2330 if (mtp_device == NULL)
Linus Walleijf27d1cd2007-03-05 14:23:00 +00002331 continue;
Linus Walleijddaba2f2007-10-02 21:20:30 +00002332
Linus Walleijf27d1cd2007-03-05 14:23:00 +00002333 /* Add the device to the list */
2334 mtp_device->next = NULL;
2335 if (mtp_device_list == NULL) {
2336 mtp_device_list = current_device = mtp_device;
2337 } else {
2338 current_device->next = mtp_device;
2339 current_device = mtp_device;
2340 }
Linus Walleij2d3f7b82007-02-14 09:24:20 +00002341 }
Linus Walleijf27d1cd2007-03-05 14:23:00 +00002342 return mtp_device_list;
Linus Walleij2d3f7b82007-02-14 09:24:20 +00002343}
2344
2345/**
tedbullock848009b2007-03-03 23:20:12 +00002346 * Get the number of devices that are available in the listed device list
Linus Walleijf27d1cd2007-03-05 14:23:00 +00002347 * @param device_list Pointer to a linked list of devices
2348 * @return Number of devices in the device list device_list
2349 * @see LIBMTP_Get_Connected_Devices()
Linus Walleij2c1bbd62009-11-07 15:15:03 +00002350 */
Linus Walleijf27d1cd2007-03-05 14:23:00 +00002351uint32_t LIBMTP_Number_Devices_In_List(LIBMTP_mtpdevice_t *device_list)
tedbullock848009b2007-03-03 23:20:12 +00002352{
Linus Walleijf27d1cd2007-03-05 14:23:00 +00002353 uint32_t numdevices = 0;
2354 LIBMTP_mtpdevice_t *iter;
2355 for(iter = device_list; iter != NULL; iter = iter->next)
2356 numdevices++;
Linus Walleij2c1bbd62009-11-07 15:15:03 +00002357
Linus Walleijf27d1cd2007-03-05 14:23:00 +00002358 return numdevices;
tedbullock848009b2007-03-03 23:20:12 +00002359}
2360
2361/**
Linus Walleij2d3f7b82007-02-14 09:24:20 +00002362 * Get the first connected MTP device node in the linked list of devices.
2363 * Currently this only provides access to USB devices
Linus Walleijf27d1cd2007-03-05 14:23:00 +00002364 * @param device_list A list of devices ready to be used by the caller. You
2365 * need to know how many there are.
Linus Walleij2d3f7b82007-02-14 09:24:20 +00002366 * @return Any error information gathered from device connections
Linus Walleijf27d1cd2007-03-05 14:23:00 +00002367 * @see LIBMTP_Number_Devices_In_List()
Linus Walleij2d3f7b82007-02-14 09:24:20 +00002368 */
Linus Walleijf27d1cd2007-03-05 14:23:00 +00002369LIBMTP_error_number_t LIBMTP_Get_Connected_Devices(LIBMTP_mtpdevice_t **device_list)
Linus Walleij2d3f7b82007-02-14 09:24:20 +00002370{
Linus Walleija700d222008-05-28 23:06:14 +00002371 LIBMTP_raw_device_t *devices;
2372 int numdevs;
Linus Walleij5fbb77d2007-03-07 08:40:36 +00002373 LIBMTP_error_number_t ret;
Linus Walleij2c1bbd62009-11-07 15:15:03 +00002374
Linus Walleija700d222008-05-28 23:06:14 +00002375 ret = LIBMTP_Detect_Raw_Devices(&devices, &numdevs);
Linus Walleij5fbb77d2007-03-07 08:40:36 +00002376 if (ret != LIBMTP_ERROR_NONE) {
Linus Walleijf27d1cd2007-03-05 14:23:00 +00002377 *device_list = NULL;
Linus Walleij5fbb77d2007-03-07 08:40:36 +00002378 return ret;
Linus Walleij2d3f7b82007-02-14 09:24:20 +00002379 }
2380
2381 /* Assign linked list of devices */
Linus Walleija700d222008-05-28 23:06:14 +00002382 if (devices == NULL || numdevs == 0) {
2383 *device_list = NULL;
Marcus Meissnerb5214772015-10-04 13:08:20 +02002384 free(devices);
Linus Walleija700d222008-05-28 23:06:14 +00002385 return LIBMTP_ERROR_NO_DEVICE_ATTACHED;
2386 }
2387
2388 *device_list = create_usb_mtp_devices(devices, numdevs);
2389 free(devices);
Linus Walleij45a86372007-03-07 09:36:19 +00002390
Linus Walleij2d3f7b82007-02-14 09:24:20 +00002391 /* TODO: Add wifi device access here */
Linus Walleij2c1bbd62009-11-07 15:15:03 +00002392
Richard Low99a93e82007-09-29 08:16:08 +00002393 /* We have found some devices but create failed */
2394 if (*device_list == NULL)
2395 return LIBMTP_ERROR_CONNECTING;
Linus Walleij2d3f7b82007-02-14 09:24:20 +00002396
2397 return LIBMTP_ERROR_NONE;
2398}
2399
2400/**
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00002401 * This closes and releases an allocated MTP device.
Linus Walleijb9256fd2006-02-15 09:40:43 +00002402 * @param device a pointer to the MTP device to release.
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00002403 */
tedbullock0f033cb2007-02-14 20:56:54 +00002404void LIBMTP_Release_Device_List(LIBMTP_mtpdevice_t *device)
2405{
2406 if(device != NULL)
2407 {
2408 if(device->next != NULL)
2409 {
2410 LIBMTP_Release_Device_List(device->next);
2411 }
Linus Walleij2c1bbd62009-11-07 15:15:03 +00002412
tedbullock0f033cb2007-02-14 20:56:54 +00002413 LIBMTP_Release_Device(device);
2414 }
2415}
2416
2417/**
2418 * This closes and releases an allocated MTP device.
2419 * @param device a pointer to the MTP device to release.
2420 */
Linus Walleijb9256fd2006-02-15 09:40:43 +00002421void LIBMTP_Release_Device(LIBMTP_mtpdevice_t *device)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00002422{
Linus Walleij9b28da32006-03-16 13:47:58 +00002423 PTPParams *params = (PTPParams *) device->params;
Linus Walleij2d411db2006-03-22 12:13:09 +00002424 PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo;
Linus Walleij9b28da32006-03-16 13:47:58 +00002425
Linus Walleijb0ab5482007-08-29 21:08:54 +00002426 close_device(ptp_usb, params);
Linus Walleij2715c442007-01-20 22:35:29 +00002427 // Clear error stack
2428 LIBMTP_Clear_Errorstack(device);
Linus Walleij3ec86312006-08-21 13:25:24 +00002429 // Free iconv() converters...
Linus Walleija823a702006-08-27 21:27:46 +00002430 iconv_close(params->cd_locale_to_ucs2);
2431 iconv_close(params->cd_ucs2_to_locale);
Linus Walleij2c1bbd62009-11-07 15:15:03 +00002432 free(ptp_usb);
Linus Walleij073c4172007-02-02 22:26:33 +00002433 ptp_free_params(params);
Lei Zhange1082c12013-02-13 15:16:43 -08002434 free(params);
Linus Walleij9e1b0812006-12-12 19:22:02 +00002435 free_storage_list(device);
Linus Walleij38d7ee82010-07-24 22:17:21 +00002436 // Free extension list...
2437 if (device->extensions != NULL) {
2438 LIBMTP_device_extension_t *tmp = device->extensions;
2439
2440 while (tmp != NULL) {
2441 LIBMTP_device_extension_t *next = tmp->next;
2442
2443 if (tmp->name)
2444 free(tmp->name);
2445 free(tmp);
2446 tmp = next;
2447 }
2448 }
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00002449 free(device);
2450}
Linus Walleijb9256fd2006-02-15 09:40:43 +00002451
2452/**
Linus Walleij2715c442007-01-20 22:35:29 +00002453 * This can be used by any libmtp-intrinsic code that
Linus Walleij68b19c02007-02-15 11:50:37 +00002454 * need to stack up an error on the stack. You are only
2455 * supposed to add errors to the error stack using this
2456 * function, do not create and reference error entries
2457 * directly.
Linus Walleij2715c442007-01-20 22:35:29 +00002458 */
2459static void add_error_to_errorstack(LIBMTP_mtpdevice_t *device,
2460 LIBMTP_error_number_t errornumber,
2461 char const * const error_text)
2462{
2463 LIBMTP_error_t *newerror;
Linus Walleij2c1bbd62009-11-07 15:15:03 +00002464
Linus Walleij68b19c02007-02-15 11:50:37 +00002465 if (device == NULL) {
nicklas79daadbf22009-09-28 18:19:34 +00002466 LIBMTP_ERROR("LIBMTP PANIC: Trying to add error to a NULL device!\n");
Linus Walleij68b19c02007-02-15 11:50:37 +00002467 return;
2468 }
Linus Walleij2715c442007-01-20 22:35:29 +00002469 newerror = (LIBMTP_error_t *) malloc(sizeof(LIBMTP_error_t));
2470 newerror->errornumber = errornumber;
2471 newerror->error_text = strdup(error_text);
rreardon774503c2007-02-15 15:52:49 +00002472 newerror->next = NULL;
Linus Walleij2715c442007-01-20 22:35:29 +00002473 if (device->errorstack == NULL) {
2474 device->errorstack = newerror;
2475 } else {
2476 LIBMTP_error_t *tmp = device->errorstack;
Linus Walleij2c1bbd62009-11-07 15:15:03 +00002477
Linus Walleij2715c442007-01-20 22:35:29 +00002478 while (tmp->next != NULL) {
2479 tmp = tmp->next;
2480 }
2481 tmp->next = newerror;
2482 }
2483}
2484
2485/**
Linus Walleij070e9b42007-01-22 08:49:28 +00002486 * Adds an error from the PTP layer to the error stack.
2487 */
2488static void add_ptp_error_to_errorstack(LIBMTP_mtpdevice_t *device,
2489 uint16_t ptp_error,
2490 char const * const error_text)
2491{
Marcus Meissner3d692dc2016-02-21 12:34:06 +01002492 PTPParams *params = (PTPParams *) device->params;
2493
Linus Walleij68b19c02007-02-15 11:50:37 +00002494 if (device == NULL) {
nicklas79daadbf22009-09-28 18:19:34 +00002495 LIBMTP_ERROR("LIBMTP PANIC: Trying to add PTP error to a NULL device!\n");
Linus Walleij68b19c02007-02-15 11:50:37 +00002496 return;
2497 } else {
2498 char outstr[256];
2499 snprintf(outstr, sizeof(outstr), "PTP Layer error %04x: %s", ptp_error, error_text);
2500 outstr[sizeof(outstr)-1] = '\0';
2501 add_error_to_errorstack(device, LIBMTP_ERROR_PTP_LAYER, outstr);
Catalin Patulea73d26cf2012-07-03 02:16:23 -04002502
Marcus Meissner3d692dc2016-02-21 12:34:06 +01002503 snprintf(outstr, sizeof(outstr), "Error %04x: %s", ptp_error, ptp_strerror(ptp_error, params->deviceinfo.VendorExtensionID));
Catalin Patulea73d26cf2012-07-03 02:16:23 -04002504 outstr[sizeof(outstr)-1] = '\0';
2505 add_error_to_errorstack(device, LIBMTP_ERROR_PTP_LAYER, outstr);
Linus Walleij68b19c02007-02-15 11:50:37 +00002506 }
Linus Walleij070e9b42007-01-22 08:49:28 +00002507}
2508
2509/**
Linus Walleij2715c442007-01-20 22:35:29 +00002510 * This returns the error stack for a device in case you
2511 * need to either reference the error numbers (e.g. when
2512 * creating multilingual apps with multiple-language text
2513 * representations for each error number) or when you need
2514 * to build a multi-line error text widget or something like
2515 * that. You need to call the <code>LIBMTP_Clear_Errorstack</code>
2516 * to clear it when you're finished with it.
2517 * @param device a pointer to the MTP device to get the error
2518 * stack for.
2519 * @return the error stack or NULL if there are no errors
2520 * on the stack.
2521 * @see LIBMTP_Clear_Errorstack()
2522 * @see LIBMTP_Dump_Errorstack()
2523 */
2524LIBMTP_error_t *LIBMTP_Get_Errorstack(LIBMTP_mtpdevice_t *device)
2525{
Linus Walleij68b19c02007-02-15 11:50:37 +00002526 if (device == NULL) {
nicklas79daadbf22009-09-28 18:19:34 +00002527 LIBMTP_ERROR("LIBMTP PANIC: Trying to get the error stack of a NULL device!\n");
Linus Walleij5c1499e2009-02-21 06:54:29 +00002528 return NULL;
Linus Walleij68b19c02007-02-15 11:50:37 +00002529 }
Linus Walleij2715c442007-01-20 22:35:29 +00002530 return device->errorstack;
2531}
2532
2533/**
2534 * This function clears the error stack of a device and frees
2535 * any memory used by it. Call this when you're finished with
2536 * using the errors.
2537 * @param device a pointer to the MTP device to clear the error
2538 * stack for.
2539 */
2540void LIBMTP_Clear_Errorstack(LIBMTP_mtpdevice_t *device)
2541{
Linus Walleij68b19c02007-02-15 11:50:37 +00002542 if (device == NULL) {
nicklas79daadbf22009-09-28 18:19:34 +00002543 LIBMTP_ERROR("LIBMTP PANIC: Trying to clear the error stack of a NULL device!\n");
Linus Walleij68b19c02007-02-15 11:50:37 +00002544 } else {
2545 LIBMTP_error_t *tmp = device->errorstack;
Linus Walleij2c1bbd62009-11-07 15:15:03 +00002546
Linus Walleij68b19c02007-02-15 11:50:37 +00002547 while (tmp != NULL) {
2548 LIBMTP_error_t *tmp2;
Linus Walleij2c1bbd62009-11-07 15:15:03 +00002549
Linus Walleij68b19c02007-02-15 11:50:37 +00002550 if (tmp->error_text != NULL) {
2551 free(tmp->error_text);
2552 }
2553 tmp2 = tmp;
2554 tmp = tmp->next;
2555 free(tmp2);
Linus Walleij2715c442007-01-20 22:35:29 +00002556 }
Linus Walleij68b19c02007-02-15 11:50:37 +00002557 device->errorstack = NULL;
Linus Walleij2715c442007-01-20 22:35:29 +00002558 }
Linus Walleij2715c442007-01-20 22:35:29 +00002559}
2560
2561/**
2562 * This function dumps the error stack to <code>stderr</code>.
2563 * (You still have to clear the stack though.)
2564 * @param device a pointer to the MTP device to dump the error
2565 * stack for.
2566 */
2567void LIBMTP_Dump_Errorstack(LIBMTP_mtpdevice_t *device)
2568{
Linus Walleij68b19c02007-02-15 11:50:37 +00002569 if (device == NULL) {
nicklas79daadbf22009-09-28 18:19:34 +00002570 LIBMTP_ERROR("LIBMTP PANIC: Trying to dump the error stack of a NULL device!\n");
Linus Walleij68b19c02007-02-15 11:50:37 +00002571 } else {
2572 LIBMTP_error_t *tmp = device->errorstack;
Linus Walleij2715c442007-01-20 22:35:29 +00002573
Linus Walleij68b19c02007-02-15 11:50:37 +00002574 while (tmp != NULL) {
2575 if (tmp->error_text != NULL) {
nicklas79daadbf22009-09-28 18:19:34 +00002576 LIBMTP_ERROR("Error %d: %s\n", tmp->errornumber, tmp->error_text);
Linus Walleij68b19c02007-02-15 11:50:37 +00002577 } else {
nicklas79daadbf22009-09-28 18:19:34 +00002578 LIBMTP_ERROR("Error %d: (unknown)\n", tmp->errornumber);
Linus Walleij68b19c02007-02-15 11:50:37 +00002579 }
2580 tmp = tmp->next;
Linus Walleij2715c442007-01-20 22:35:29 +00002581 }
Linus Walleij2715c442007-01-20 22:35:29 +00002582 }
2583}
2584
2585/**
Linus Walleij2c1bbd62009-11-07 15:15:03 +00002586 * This command gets all handles and stuff by FAST directory retrieveal
2587 * which is available by getting all metadata for object
Linus Walleij338ade42007-07-03 20:44:08 +00002588 * <code>0xffffffff</code> which simply means "all metadata for all objects".
2589 * This works on the vast majority of MTP devices (there ARE exceptions!)
2590 * and is quite quick. Check the error stack to see if there were
2591 * problems getting the metadata.
Linus Walleij8533bf72007-08-28 10:03:59 +00002592 * @return 0 if all was OK, -1 on failure.
Linus Walleij338ade42007-07-03 20:44:08 +00002593 */
Catalin Patuleaa23fce32012-07-17 22:25:12 -04002594static int get_all_metadata_fast(LIBMTP_mtpdevice_t *device)
Linus Walleij338ade42007-07-03 20:44:08 +00002595{
2596 PTPParams *params = (PTPParams *) device->params;
2597 int cnt = 0;
Linus Walleij1e9a0332007-09-12 19:35:56 +00002598 int i, j, nrofprops;
Linus Walleij338ade42007-07-03 20:44:08 +00002599 uint32_t lasthandle = 0xffffffff;
Linus Walleij1e9a0332007-09-12 19:35:56 +00002600 MTPProperties *props = NULL;
2601 MTPProperties *prop;
Linus Walleij338ade42007-07-03 20:44:08 +00002602 uint16_t ret;
Linus Walleij2f622812008-08-30 22:06:58 +00002603 int oldtimeout;
2604 PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo;
2605
Linus Walleije04a1b92011-03-09 18:00:24 +01002606 /*
2607 * The follow request causes the device to generate
2608 * a list of every file on the device and return it
Linus Walleij2f622812008-08-30 22:06:58 +00002609 * in a single response.
2610 *
2611 * Some slow devices as well as devices with very
2612 * large file systems can easily take longer then
2613 * the standard timeout value before it is able
2614 * to return a response.
2615 *
2616 * Temporarly set timeout to allow working with
2617 * widest range of devices.
2618 */
2619 get_usb_device_timeout(ptp_usb, &oldtimeout);
2620 set_usb_device_timeout(ptp_usb, 60000);
Linus Walleij2c1bbd62009-11-07 15:15:03 +00002621
Linus Walleijfcb43422008-05-23 21:53:55 +00002622 ret = ptp_mtp_getobjectproplist(params, 0xffffffff, &props, &nrofprops);
Linus Walleij2f622812008-08-30 22:06:58 +00002623 set_usb_device_timeout(ptp_usb, oldtimeout);
Linus Walleij8533bf72007-08-28 10:03:59 +00002624
2625 if (ret == PTP_RC_MTP_Specification_By_Group_Unsupported) {
Linus Walleij2c1bbd62009-11-07 15:15:03 +00002626 // What's the point in the device implementing this command if
Linus Walleij8533bf72007-08-28 10:03:59 +00002627 // you cannot use it to get all props for AT LEAST one object?
2628 // Well, whatever...
2629 add_ptp_error_to_errorstack(device, ret, "get_all_metadata_fast(): "
2630 "cannot retrieve all metadata for an object on this device.");
Linus Walleij91e98132007-08-28 10:05:11 +00002631 return -1;
Linus Walleij8533bf72007-08-28 10:03:59 +00002632 }
Linus Walleij338ade42007-07-03 20:44:08 +00002633 if (ret != PTP_RC_OK) {
Linus Walleij8533bf72007-08-28 10:03:59 +00002634 add_ptp_error_to_errorstack(device, ret, "get_all_metadata_fast(): "
2635 "could not get proplist of all objects.");
2636 return -1;
Linus Walleij338ade42007-07-03 20:44:08 +00002637 }
Linus Walleijfcb43422008-05-23 21:53:55 +00002638 if (props == NULL && nrofprops != 0) {
2639 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL,
2640 "get_all_metadata_fast(): "
2641 "call to ptp_mtp_getobjectproplist() returned "
2642 "inconsistent results.");
2643 return -1;
2644 }
Linus Walleij2c1bbd62009-11-07 15:15:03 +00002645 /*
Linus Walleij338ade42007-07-03 20:44:08 +00002646 * We count the number of objects by counting the ObjectHandle
Linus Walleij31b74292008-05-02 23:29:06 +00002647 * references, whenever it changes we get a new object, when it's
Linus Walleij338ade42007-07-03 20:44:08 +00002648 * the same, it is just different properties of the same object.
2649 */
Linus Walleij1e9a0332007-09-12 19:35:56 +00002650 prop = props;
2651 for (i=0;i<nrofprops;i++) {
Linus Walleij338ade42007-07-03 20:44:08 +00002652 if (lasthandle != prop->ObjectHandle) {
2653 cnt++;
2654 lasthandle = prop->ObjectHandle;
2655 }
Linus Walleij1e9a0332007-09-12 19:35:56 +00002656 prop++;
Linus Walleij338ade42007-07-03 20:44:08 +00002657 }
Linus Walleij338ade42007-07-03 20:44:08 +00002658 lasthandle = 0xffffffff;
Marcus Meissnerb5214772015-10-04 13:08:20 +02002659 params->objects = calloc (cnt, sizeof(PTPObject));
Linus Walleij1e9a0332007-09-12 19:35:56 +00002660 prop = props;
Linus Walleij338ade42007-07-03 20:44:08 +00002661 i = -1;
Linus Walleij1e9a0332007-09-12 19:35:56 +00002662 for (j=0;j<nrofprops;j++) {
Linus Walleij338ade42007-07-03 20:44:08 +00002663 if (lasthandle != prop->ObjectHandle) {
2664 if (i >= 0) {
Linus Walleij77127dc2009-08-25 16:38:42 +00002665 params->objects[i].flags |= PTPOBJECT_OBJECTINFO_LOADED;
Linus Walleijd4637502009-06-14 23:03:33 +00002666 if (!params->objects[i].oi.Filename) {
Linus Walleij6bf68b42007-09-03 22:50:02 +00002667 /* I have one such file on my Creative (Marcus) */
Linus Walleijd4637502009-06-14 23:03:33 +00002668 params->objects[i].oi.Filename = strdup("<null>");
Linus Walleij338ade42007-07-03 20:44:08 +00002669 }
2670 }
2671 i++;
2672 lasthandle = prop->ObjectHandle;
Linus Walleijd4637502009-06-14 23:03:33 +00002673 params->objects[i].oid = prop->ObjectHandle;
Linus Walleij338ade42007-07-03 20:44:08 +00002674 }
2675 switch (prop->property) {
2676 case PTP_OPC_ParentObject:
Linus Walleijd4637502009-06-14 23:03:33 +00002677 params->objects[i].oi.ParentObject = prop->propval.u32;
2678 params->objects[i].flags |= PTPOBJECT_PARENTOBJECT_LOADED;
Linus Walleij338ade42007-07-03 20:44:08 +00002679 break;
2680 case PTP_OPC_ObjectFormat:
Linus Walleijd4637502009-06-14 23:03:33 +00002681 params->objects[i].oi.ObjectFormat = prop->propval.u16;
Linus Walleij338ade42007-07-03 20:44:08 +00002682 break;
2683 case PTP_OPC_ObjectSize:
Linus Walleijd9d28d52007-08-04 19:01:18 +00002684 // We loose precision here, up to 32 bits! However the commands that
2685 // retrieve metadata for files and tracks will make sure that the
2686 // PTP_OPC_ObjectSize is read in and duplicated again.
Linus Walleijddaba2f2007-10-02 21:20:30 +00002687 if (device->object_bitsize == 64) {
Linus Walleijd4637502009-06-14 23:03:33 +00002688 params->objects[i].oi.ObjectCompressedSize = (uint32_t) prop->propval.u64;
Linus Walleijddaba2f2007-10-02 21:20:30 +00002689 } else {
Linus Walleijd4637502009-06-14 23:03:33 +00002690 params->objects[i].oi.ObjectCompressedSize = prop->propval.u32;
Linus Walleijddaba2f2007-10-02 21:20:30 +00002691 }
Linus Walleij338ade42007-07-03 20:44:08 +00002692 break;
2693 case PTP_OPC_StorageID:
Linus Walleijd4637502009-06-14 23:03:33 +00002694 params->objects[i].oi.StorageID = prop->propval.u32;
2695 params->objects[i].flags |= PTPOBJECT_STORAGEID_LOADED;
Linus Walleij338ade42007-07-03 20:44:08 +00002696 break;
2697 case PTP_OPC_ObjectFileName:
Richard Low8e8d9d42007-07-14 10:36:50 +00002698 if (prop->propval.str != NULL)
Linus Walleijd4637502009-06-14 23:03:33 +00002699 params->objects[i].oi.Filename = strdup(prop->propval.str);
Linus Walleij338ade42007-07-03 20:44:08 +00002700 break;
Linus Walleijd4637502009-06-14 23:03:33 +00002701 default: {
2702 MTPProperties *newprops;
2703
2704 /* Copy all of the other MTP oprierties into the per-object proplist */
2705 if (params->objects[i].nrofmtpprops) {
Linus Walleij43aba612011-11-25 09:40:34 +01002706 newprops = realloc(params->objects[i].mtpprops,
2707 (params->objects[i].nrofmtpprops+1)*sizeof(MTPProperties));
Linus Walleijd4637502009-06-14 23:03:33 +00002708 } else {
Marcus Meissnerb5214772015-10-04 13:08:20 +02002709 newprops = calloc(1,sizeof(MTPProperties));
Linus Walleijd4637502009-06-14 23:03:33 +00002710 }
2711 if (!newprops) return 0; /* FIXME: error handling? */
2712 params->objects[i].mtpprops = newprops;
Linus Walleij43aba612011-11-25 09:40:34 +01002713 memcpy(&params->objects[i].mtpprops[params->objects[i].nrofmtpprops],
2714 &props[j],sizeof(props[j]));
Linus Walleijd4637502009-06-14 23:03:33 +00002715 params->objects[i].nrofmtpprops++;
Linus Walleij362d13e2009-08-02 19:59:21 +00002716 params->objects[i].flags |= PTPOBJECT_MTPPROPLIST_LOADED;
Linus Walleij338ade42007-07-03 20:44:08 +00002717 break;
Linus Walleijd4637502009-06-14 23:03:33 +00002718 }
Linus Walleij338ade42007-07-03 20:44:08 +00002719 }
Linus Walleij1e9a0332007-09-12 19:35:56 +00002720 prop++;
Linus Walleij338ade42007-07-03 20:44:08 +00002721 }
Linus Walleij77127dc2009-08-25 16:38:42 +00002722 /* mark last entry also */
Marcus Meissner7e328872017-04-10 08:39:00 +02002723 if (i >= 0) {
2724 params->objects[i].flags |= PTPOBJECT_OBJECTINFO_LOADED;
2725 params->nrofobjects = i+1;
2726 } else {
2727 params->nrofobjects = 0;
2728 }
Marcus Meissner859f9b22015-10-04 13:20:22 +02002729 free (props);
Linus Walleij77127dc2009-08-25 16:38:42 +00002730 /* The device might not give the list in linear ascending order */
2731 ptp_objects_sort (params);
Linus Walleij8533bf72007-08-28 10:03:59 +00002732 return 0;
Linus Walleij338ade42007-07-03 20:44:08 +00002733}
2734
2735/**
2736 * This function will recurse through all the directories on the device,
2737 * starting at the root directory, gathering metadata as it moves along.
2738 * It works better on some devices that will only return data for a
2739 * certain directory and does not respect the option to get all metadata
2740 * for all objects.
2741 */
Linus Walleij2c1bbd62009-11-07 15:15:03 +00002742static void get_handles_recursively(LIBMTP_mtpdevice_t *device,
2743 PTPParams *params,
Linus Walleij6bf68b42007-09-03 22:50:02 +00002744 uint32_t storageid,
2745 uint32_t parent)
Linus Walleij338ade42007-07-03 20:44:08 +00002746{
2747 PTPObjectHandles currentHandles;
2748 int i = 0;
Linus Walleij338ade42007-07-03 20:44:08 +00002749 uint16_t ret = ptp_getobjecthandles(params,
Linus Walleij6bf68b42007-09-03 22:50:02 +00002750 storageid,
Linus Walleij338ade42007-07-03 20:44:08 +00002751 PTP_GOH_ALL_FORMATS,
2752 parent,
2753 &currentHandles);
Linus Walleij2c1bbd62009-11-07 15:15:03 +00002754
Linus Walleij338ade42007-07-03 20:44:08 +00002755 if (ret != PTP_RC_OK) {
2756 add_ptp_error_to_errorstack(device, ret, "get_handles_recursively(): could not get object handles.");
2757 return;
2758 }
Linus Walleij2c1bbd62009-11-07 15:15:03 +00002759
Linus Walleij338ade42007-07-03 20:44:08 +00002760 if (currentHandles.Handler == NULL || currentHandles.n == 0)
2761 return;
2762
Linus Walleij338ade42007-07-03 20:44:08 +00002763 // Now descend into any subdirectories found
Linus Walleij2c1bbd62009-11-07 15:15:03 +00002764 for (i = 0; i < currentHandles.n; i++) {
Linus Walleijd4637502009-06-14 23:03:33 +00002765 PTPObject *ob;
Linus Walleij43aba612011-11-25 09:40:34 +01002766 ret = ptp_object_want(params,currentHandles.Handler[i],
2767 PTPOBJECT_OBJECTINFO_LOADED, &ob);
Linus Walleij338ade42007-07-03 20:44:08 +00002768 if (ret == PTP_RC_OK) {
Linus Walleijd4637502009-06-14 23:03:33 +00002769 if (ob->oi.ObjectFormat == PTP_OFC_Association)
Linus Walleij43aba612011-11-25 09:40:34 +01002770 get_handles_recursively(device, params,
2771 storageid, currentHandles.Handler[i]);
Linus Walleij338ade42007-07-03 20:44:08 +00002772 } else {
2773 add_error_to_errorstack(device,
2774 LIBMTP_ERROR_CONNECTING,
2775 "Found a bad handle, trying to ignore it.");
2776 }
2777 }
Linus Walleij338ade42007-07-03 20:44:08 +00002778 free(currentHandles.Handler);
2779}
2780
2781/**
Linus Walleij438bd7f2006-06-08 11:35:44 +00002782 * This function refresh the internal handle list whenever
2783 * the items stored inside the device is altered. On operations
2784 * that do not add or remove objects, this is typically not
2785 * called.
2786 * @param device a pointer to the MTP device to flush handles for.
2787 */
2788static void flush_handles(LIBMTP_mtpdevice_t *device)
2789{
2790 PTPParams *params = (PTPParams *) device->params;
Linus Walleij338ade42007-07-03 20:44:08 +00002791 PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo;
Linus Walleij8533bf72007-08-28 10:03:59 +00002792 int ret;
Linus Walleijf0bf4372007-07-01 21:47:38 +00002793 uint32_t i;
2794
Linus Walleija8b88892011-03-03 19:58:26 +01002795 if (!device->cached) {
2796 return;
2797 }
2798
Linus Walleijd4637502009-06-14 23:03:33 +00002799 if (params->objects != NULL) {
2800 for (i=0;i<params->nrofobjects;i++)
2801 ptp_free_object (&params->objects[i]);
2802 free(params->objects);
2803 params->objects = NULL;
2804 params->nrofobjects = 0;
Linus Walleij438bd7f2006-06-08 11:35:44 +00002805 }
Linus Walleijf0bf4372007-07-01 21:47:38 +00002806
Linus Walleij338ade42007-07-03 20:44:08 +00002807 if (ptp_operation_issupported(params,PTP_OC_MTP_GetObjPropList)
Linus Walleijfec4d562008-06-01 22:30:36 +00002808 && !FLAG_BROKEN_MTPGETOBJPROPLIST(ptp_usb)
2809 && !FLAG_BROKEN_MTPGETOBJPROPLIST_ALL(ptp_usb)) {
Linus Walleij139455e2007-08-28 10:25:06 +00002810 // Use the fast method. Ignore return value for now.
Catalin Patuleaa23fce32012-07-17 22:25:12 -04002811 ret = get_all_metadata_fast(device);
Linus Walleij8533bf72007-08-28 10:03:59 +00002812 }
Linus Walleija8b88892011-03-03 19:58:26 +01002813
Linus Walleij139455e2007-08-28 10:25:06 +00002814 // If the previous failed or returned no objects, use classic
2815 // methods instead.
Linus Walleijd4637502009-06-14 23:03:33 +00002816 if (params->nrofobjects == 0) {
Linus Walleij338ade42007-07-03 20:44:08 +00002817 // Get all the handles using just standard commands.
Linus Walleij6bf68b42007-09-03 22:50:02 +00002818 if (device->storage == NULL) {
2819 get_handles_recursively(device, params,
Linus Walleij6bf68b42007-09-03 22:50:02 +00002820 PTP_GOH_ALL_STORAGE,
2821 PTP_GOH_ROOT_PARENT);
2822 } else {
2823 // Get handles for each storage in turn.
2824 LIBMTP_devicestorage_t *storage = device->storage;
2825 while(storage != NULL) {
2826 get_handles_recursively(device, params,
Linus Walleij6bf68b42007-09-03 22:50:02 +00002827 storage->id,
2828 PTP_GOH_ROOT_PARENT);
2829 storage = storage->next;
2830 }
2831 }
Linus Walleij338ade42007-07-03 20:44:08 +00002832 }
Linus Walleij2c1bbd62009-11-07 15:15:03 +00002833
Linus Walleij8f7f1aa2008-06-15 19:00:23 +00002834 /*
2835 * Loop over the handles, fix up any NULL filenames or
2836 * keywords, then attempt to locate some default folders
2837 * in the root directory of the primary storage.
2838 */
Linus Walleijd4637502009-06-14 23:03:33 +00002839 for(i = 0; i < params->nrofobjects; i++) {
2840 PTPObject *ob, *xob;
Linus Walleij2c1bbd62009-11-07 15:15:03 +00002841
Linus Walleijd4637502009-06-14 23:03:33 +00002842 ob = &params->objects[i];
Linus Walleij43aba612011-11-25 09:40:34 +01002843 ret = ptp_object_want(params,params->objects[i].oid,
2844 PTPOBJECT_OBJECTINFO_LOADED, &xob);
Linus Walleijd4637502009-06-14 23:03:33 +00002845 if (ret != PTP_RC_OK) {
nicklas79daadbf22009-09-28 18:19:34 +00002846 LIBMTP_ERROR("broken! %x not found\n", params->objects[i].oid);
Linus Walleijf0bf4372007-07-01 21:47:38 +00002847 }
Linus Walleijd4637502009-06-14 23:03:33 +00002848 if (ob->oi.Filename == NULL)
2849 ob->oi.Filename = strdup("<null>");
2850 if (ob->oi.Keywords == NULL)
2851 ob->oi.Keywords = strdup("<null>");
2852
Linus Walleijf0bf4372007-07-01 21:47:38 +00002853 /* Ignore handles that point to non-folders */
Linus Walleijd4637502009-06-14 23:03:33 +00002854 if(ob->oi.ObjectFormat != PTP_OFC_Association)
Linus Walleijf0bf4372007-07-01 21:47:38 +00002855 continue;
Linus Walleij8f7f1aa2008-06-15 19:00:23 +00002856 /* Only look in the root folder */
Linus Walleij4fc19172010-01-19 00:10:26 +00002857 if (ob->oi.ParentObject == 0xffffffffU) {
Linus Walleij43aba612011-11-25 09:40:34 +01002858 LIBMTP_ERROR("object %x has parent 0xffffffff (-1) continuing anyway\n",
2859 ob->oid);
Linus Walleij4fc19172010-01-19 00:10:26 +00002860 } else if (ob->oi.ParentObject != 0x00000000U)
Linus Walleijf0bf4372007-07-01 21:47:38 +00002861 continue;
Linus Walleijc40c9bf2008-06-13 23:24:17 +00002862 /* Only look in the primary storage */
Linus Walleijd4637502009-06-14 23:03:33 +00002863 if (device->storage != NULL && ob->oi.StorageID != device->storage->id)
Linus Walleijc40c9bf2008-06-13 23:24:17 +00002864 continue;
Linus Walleij8f7f1aa2008-06-15 19:00:23 +00002865
Linus Walleijf0bf4372007-07-01 21:47:38 +00002866 /* Is this the Music Folder */
Linus Walleijd4637502009-06-14 23:03:33 +00002867 if (!strcasecmp(ob->oi.Filename, "My Music") ||
Linus Walleij4c181a82010-01-07 09:21:49 +00002868 !strcasecmp(ob->oi.Filename, "My_Music") ||
Linus Walleijd4637502009-06-14 23:03:33 +00002869 !strcasecmp(ob->oi.Filename, "Music")) {
2870 device->default_music_folder = ob->oid;
Linus Walleijf0bf4372007-07-01 21:47:38 +00002871 }
Linus Walleijd4637502009-06-14 23:03:33 +00002872 else if (!strcasecmp(ob->oi.Filename, "My Playlists") ||
Linus Walleij4c181a82010-01-07 09:21:49 +00002873 !strcasecmp(ob->oi.Filename, "My_Playlists") ||
Linus Walleijd4637502009-06-14 23:03:33 +00002874 !strcasecmp(ob->oi.Filename, "Playlists")) {
2875 device->default_playlist_folder = ob->oid;
Linus Walleijf0bf4372007-07-01 21:47:38 +00002876 }
Linus Walleijd4637502009-06-14 23:03:33 +00002877 else if (!strcasecmp(ob->oi.Filename, "My Pictures") ||
Linus Walleij4c181a82010-01-07 09:21:49 +00002878 !strcasecmp(ob->oi.Filename, "My_Pictures") ||
Linus Walleijd4637502009-06-14 23:03:33 +00002879 !strcasecmp(ob->oi.Filename, "Pictures")) {
2880 device->default_picture_folder = ob->oid;
Linus Walleijf0bf4372007-07-01 21:47:38 +00002881 }
Linus Walleijd4637502009-06-14 23:03:33 +00002882 else if (!strcasecmp(ob->oi.Filename, "My Video") ||
Linus Walleij4c181a82010-01-07 09:21:49 +00002883 !strcasecmp(ob->oi.Filename, "My_Video") ||
Linus Walleijd4637502009-06-14 23:03:33 +00002884 !strcasecmp(ob->oi.Filename, "Video")) {
2885 device->default_video_folder = ob->oid;
Linus Walleijf0bf4372007-07-01 21:47:38 +00002886 }
Linus Walleij4c181a82010-01-07 09:21:49 +00002887 else if (!strcasecmp(ob->oi.Filename, "My Organizer") ||
2888 !strcasecmp(ob->oi.Filename, "My_Organizer")) {
Linus Walleijd4637502009-06-14 23:03:33 +00002889 device->default_organizer_folder = ob->oid;
Linus Walleijf0bf4372007-07-01 21:47:38 +00002890 }
Linus Walleijd4637502009-06-14 23:03:33 +00002891 else if (!strcasecmp(ob->oi.Filename, "ZENcast") ||
2892 !strcasecmp(ob->oi.Filename, "Datacasts")) {
2893 device->default_zencast_folder = ob->oid;
Linus Walleijf0bf4372007-07-01 21:47:38 +00002894 }
Linus Walleijd4637502009-06-14 23:03:33 +00002895 else if (!strcasecmp(ob->oi.Filename, "My Albums") ||
Linus Walleij4c181a82010-01-07 09:21:49 +00002896 !strcasecmp(ob->oi.Filename, "My_Albums") ||
Linus Walleijd4637502009-06-14 23:03:33 +00002897 !strcasecmp(ob->oi.Filename, "Albums")) {
2898 device->default_album_folder = ob->oid;
Linus Walleijf0bf4372007-07-01 21:47:38 +00002899 }
Linus Walleijd4637502009-06-14 23:03:33 +00002900 else if (!strcasecmp(ob->oi.Filename, "Text") ||
2901 !strcasecmp(ob->oi.Filename, "Texts")) {
2902 device->default_text_folder = ob->oid;
Linus Walleijf0bf4372007-07-01 21:47:38 +00002903 }
2904 }
Richard Low6711f442007-05-05 19:00:59 +00002905}
Linus Walleij438bd7f2006-06-08 11:35:44 +00002906
Linus Walleij438bd7f2006-06-08 11:35:44 +00002907/**
Linus Walleij2c1bbd62009-11-07 15:15:03 +00002908 * This function traverses a devices storage list freeing up the
Linus Walleij9e1b0812006-12-12 19:22:02 +00002909 * strings and the structs.
Linus Walleij2c1bbd62009-11-07 15:15:03 +00002910 * @param device a pointer to the MTP device to free the storage
Linus Walleij9e1b0812006-12-12 19:22:02 +00002911 * list for.
2912 */
2913static void free_storage_list(LIBMTP_mtpdevice_t *device)
2914{
Linus Walleije1ac07e2006-12-14 19:38:59 +00002915 LIBMTP_devicestorage_t *storage;
2916 LIBMTP_devicestorage_t *tmp;
Linus Walleij9e1b0812006-12-12 19:22:02 +00002917
Linus Walleije1ac07e2006-12-14 19:38:59 +00002918 storage = device->storage;
Linus Walleij2c1bbd62009-11-07 15:15:03 +00002919 while(storage != NULL) {
Linus Walleije1ac07e2006-12-14 19:38:59 +00002920 if (storage->StorageDescription != NULL) {
2921 free(storage->StorageDescription);
2922 }
2923 if (storage->VolumeIdentifier != NULL) {
2924 free(storage->VolumeIdentifier);
2925 }
2926 tmp = storage;
2927 storage = storage->next;
2928 free(tmp);
Linus Walleij9e1b0812006-12-12 19:22:02 +00002929 }
2930 device->storage = NULL;
2931
2932 return;
2933}
2934
2935/**
2936 * This function traverses a devices storage list freeing up the
2937 * strings and the structs.
2938 * @param device a pointer to the MTP device to free the storage
2939 * list for.
2940 */
2941static int sort_storage_by(LIBMTP_mtpdevice_t *device,int const sortby)
2942{
2943 LIBMTP_devicestorage_t *oldhead, *ptr1, *ptr2, *newlist;
2944
2945 if (device->storage == NULL)
2946 return -1;
Linus Walleij2c1bbd62009-11-07 15:15:03 +00002947 if (sortby == LIBMTP_STORAGE_SORTBY_NOTSORTED)
Linus Walleij9e1b0812006-12-12 19:22:02 +00002948 return 0;
2949
2950 oldhead = ptr1 = ptr2 = device->storage;
2951
2952 newlist = NULL;
2953
2954 while(oldhead != NULL) {
2955 ptr1 = ptr2 = oldhead;
2956 while(ptr1 != NULL) {
2957
Linus Walleij2c1bbd62009-11-07 15:15:03 +00002958 if (sortby == LIBMTP_STORAGE_SORTBY_FREESPACE && ptr1->FreeSpaceInBytes > ptr2->FreeSpaceInBytes)
Linus Walleij9e1b0812006-12-12 19:22:02 +00002959 ptr2 = ptr1;
Linus Walleij2c1bbd62009-11-07 15:15:03 +00002960 if (sortby == LIBMTP_STORAGE_SORTBY_MAXSPACE && ptr1->FreeSpaceInBytes > ptr2->FreeSpaceInBytes)
Linus Walleij9e1b0812006-12-12 19:22:02 +00002961 ptr2 = ptr1;
2962
2963 ptr1 = ptr1->next;
2964 }
2965
2966 // Make our previous entries next point to our next
2967 if(ptr2->prev != NULL) {
2968 ptr1 = ptr2->prev;
Linus Walleij2c1bbd62009-11-07 15:15:03 +00002969 ptr1->next = ptr2->next;
Linus Walleij9e1b0812006-12-12 19:22:02 +00002970 } else {
2971 oldhead = ptr2->next;
2972 if(oldhead != NULL)
2973 oldhead->prev = NULL;
2974 }
2975
2976 // Make our next entries previous point to our previous
2977 ptr1 = ptr2->next;
2978 if(ptr1 != NULL) {
2979 ptr1->prev = ptr2->prev;
2980 } else {
2981 ptr1 = ptr2->prev;
2982 if(ptr1 != NULL)
2983 ptr1->next = NULL;
2984 }
Linus Walleij2c1bbd62009-11-07 15:15:03 +00002985
Linus Walleij9e1b0812006-12-12 19:22:02 +00002986 if(newlist == NULL) {
2987 newlist = ptr2;
2988 newlist->prev = NULL;
2989 } else {
2990 ptr2->prev = newlist;
2991 newlist->next = ptr2;
2992 newlist = newlist->next;
2993 }
2994 }
Linus Walleij2c1bbd62009-11-07 15:15:03 +00002995
Linus Walleij5c1499e2009-02-21 06:54:29 +00002996 if (newlist != NULL) {
2997 newlist->next = NULL;
Linus Walleij2c1bbd62009-11-07 15:15:03 +00002998 while(newlist->prev != NULL)
Linus Walleij5c1499e2009-02-21 06:54:29 +00002999 newlist = newlist->prev;
3000 device->storage = newlist;
3001 }
Linus Walleij9e1b0812006-12-12 19:22:02 +00003002
3003 return 0;
3004}
3005
3006/**
Linus Walleij2c1bbd62009-11-07 15:15:03 +00003007 * This function grabs the first writeable storageid from the
Linus Walleijd71d0b32008-09-22 08:21:03 +00003008 * device storage list.
Linus Walleij2c1bbd62009-11-07 15:15:03 +00003009 * @param device a pointer to the MTP device to locate writeable
Linus Walleijd71d0b32008-09-22 08:21:03 +00003010 * storage for.
3011 * @param fitsize a file of this file must fit on the device.
Linus Walleij9e1b0812006-12-12 19:22:02 +00003012 */
Linus Walleij88babc82014-06-02 21:32:14 +02003013static uint32_t get_writeable_storageid(LIBMTP_mtpdevice_t *device,
3014 uint64_t fitsize)
Linus Walleij9e1b0812006-12-12 19:22:02 +00003015{
Linus Walleij5c1499e2009-02-21 06:54:29 +00003016 LIBMTP_devicestorage_t *storage;
Linus Walleijfb28b632007-10-23 21:56:18 +00003017 uint32_t store = 0x00000000; //Should this be 0xffffffffu instead?
Linus Walleijd71d0b32008-09-22 08:21:03 +00003018 int subcall_ret;
Linus Walleij9e1b0812006-12-12 19:22:02 +00003019
Linus Walleijd71d0b32008-09-22 08:21:03 +00003020 // See if there is some storage we can fit this file on.
3021 storage = device->storage;
3022 if (storage == NULL) {
3023 // Sometimes the storage just cannot be detected.
3024 store = 0x00000000U;
3025 } else {
3026 while(storage != NULL) {
3027 // These storages cannot be used.
Linus Walleij43aba612011-11-25 09:40:34 +01003028 if (storage->StorageType == PTP_ST_FixedROM ||
3029 storage->StorageType == PTP_ST_RemovableROM) {
Linus Walleijd71d0b32008-09-22 08:21:03 +00003030 storage = storage->next;
3031 continue;
3032 }
3033 // Storage IDs with the lower 16 bits 0x0000 are not supposed
3034 // to be writeable.
3035 if ((storage->id & 0x0000FFFFU) == 0x00000000U) {
3036 storage = storage->next;
3037 continue;
3038 }
3039 // Also check the access capability to avoid e.g. deletable only storages
Linus Walleij43aba612011-11-25 09:40:34 +01003040 if (storage->AccessCapability == PTP_AC_ReadOnly ||
3041 storage->AccessCapability == PTP_AC_ReadOnly_with_Object_Deletion) {
Linus Walleijd71d0b32008-09-22 08:21:03 +00003042 storage = storage->next;
3043 continue;
3044 }
3045 // Then see if we can fit the file.
3046 subcall_ret = check_if_file_fits(device, storage, fitsize);
3047 if (subcall_ret != 0) {
3048 storage = storage->next;
3049 } else {
3050 // We found a storage that is writable and can fit the file!
3051 break;
3052 }
3053 }
3054 if (storage == NULL) {
Linus Walleij43aba612011-11-25 09:40:34 +01003055 add_error_to_errorstack(device, LIBMTP_ERROR_STORAGE_FULL,
3056 "get_writeable_storageid(): "
Linus Walleijd71d0b32008-09-22 08:21:03 +00003057 "all device storage is full or corrupt.");
3058 return -1;
3059 }
Linus Walleij9e1b0812006-12-12 19:22:02 +00003060 store = storage->id;
Linus Walleijd71d0b32008-09-22 08:21:03 +00003061 }
Linus Walleij9e1b0812006-12-12 19:22:02 +00003062
3063 return store;
3064}
3065
3066/**
Linus Walleij88babc82014-06-02 21:32:14 +02003067 * Tries to suggest a storage_id of a given ID when we have a parent
3068 * @param device a pointer to the device where to search for the storage ID
3069 * @param fitsize a file of this file must fit on the device.
3070 * @param parent_id look for this ID
3071 * @ret storageID
3072 */
3073static int get_suggested_storage_id(LIBMTP_mtpdevice_t *device,
3074 uint64_t fitsize,
3075 uint32_t parent_id)
3076{
3077 PTPParams *params = (PTPParams *) device->params;
3078 PTPObject *ob;
3079 uint16_t ret;
Linus Walleij88babc82014-06-02 21:32:14 +02003080
3081 ret = ptp_object_want(params, parent_id, PTPOBJECT_MTPPROPLIST_LOADED, &ob);
3082 if ((ret != PTP_RC_OK) || (ob->oi.StorageID == 0)) {
3083 add_ptp_error_to_errorstack(device, ret, "get_suggested_storage_id(): "
3084 "could not get storage id from parent id.");
3085 return get_writeable_storageid(device, fitsize);
3086 } else {
3087 /* OK we know the parent storage, then use that */
3088 return ob->oi.StorageID;
3089 }
3090}
3091
3092/**
Linus Walleij6bf68b42007-09-03 22:50:02 +00003093 * This function grabs the freespace from a certain storage in
Linus Walleij9e1b0812006-12-12 19:22:02 +00003094 * device storage list.
3095 * @param device a pointer to the MTP device to free the storage
3096 * list for.
Linus Walleij6bf68b42007-09-03 22:50:02 +00003097 * @param storageid the storage ID for the storage to flush and
3098 * get free space for.
3099 * @param freespace the free space on this storage will be returned
3100 * in this variable.
Linus Walleij9e1b0812006-12-12 19:22:02 +00003101 */
Linus Walleij2c1bbd62009-11-07 15:15:03 +00003102static int get_storage_freespace(LIBMTP_mtpdevice_t *device,
Linus Walleij6bf68b42007-09-03 22:50:02 +00003103 LIBMTP_devicestorage_t *storage,
3104 uint64_t *freespace)
Linus Walleij9e1b0812006-12-12 19:22:02 +00003105{
Linus Walleije1ac07e2006-12-14 19:38:59 +00003106 PTPParams *params = (PTPParams *) device->params;
Linus Walleij9e1b0812006-12-12 19:22:02 +00003107
Linus Walleije1ac07e2006-12-14 19:38:59 +00003108 // Always query the device about this, since some models explicitly
Linus Walleij6bf68b42007-09-03 22:50:02 +00003109 // needs that. We flush all data on queries storage here.
Linus Walleije1ac07e2006-12-14 19:38:59 +00003110 if (ptp_operation_issupported(params,PTP_OC_GetStorageInfo)) {
3111 PTPStorageInfo storageInfo;
Linus Walleij070e9b42007-01-22 08:49:28 +00003112 uint16_t ret;
Linus Walleij2c1bbd62009-11-07 15:15:03 +00003113
Linus Walleij070e9b42007-01-22 08:49:28 +00003114 ret = ptp_getstorageinfo(params, storage->id, &storageInfo);
3115 if (ret != PTP_RC_OK) {
Linus Walleij43aba612011-11-25 09:40:34 +01003116 add_ptp_error_to_errorstack(device, ret,
3117 "get_storage_freespace(): could not get storage info.");
Linus Walleije1ac07e2006-12-14 19:38:59 +00003118 return -1;
3119 }
3120 if (storage->StorageDescription != NULL) {
3121 free(storage->StorageDescription);
3122 }
3123 if (storage->VolumeIdentifier != NULL) {
3124 free(storage->VolumeIdentifier);
3125 }
3126 storage->StorageType = storageInfo.StorageType;
3127 storage->FilesystemType = storageInfo.FilesystemType;
3128 storage->AccessCapability = storageInfo.AccessCapability;
3129 storage->MaxCapacity = storageInfo.MaxCapability;
3130 storage->FreeSpaceInBytes = storageInfo.FreeSpaceInBytes;
3131 storage->FreeSpaceInObjects = storageInfo.FreeSpaceInImages;
3132 storage->StorageDescription = storageInfo.StorageDescription;
3133 storage->VolumeIdentifier = storageInfo.VolumeLabel;
3134 }
3135 if(storage->FreeSpaceInBytes == (uint64_t) -1)
Linus Walleij9e1b0812006-12-12 19:22:02 +00003136 return -1;
3137 *freespace = storage->FreeSpaceInBytes;
3138 return 0;
3139}
3140
3141/**
Linus Walleij8c45b292006-04-26 14:12:44 +00003142 * This function dumps out a large chunk of textual information
3143 * provided from the PTP protocol and additionally some extra
3144 * MTP-specific information where applicable.
3145 * @param device a pointer to the MTP device to report info from.
3146 */
3147void LIBMTP_Dump_Device_Info(LIBMTP_mtpdevice_t *device)
3148{
3149 int i;
3150 PTPParams *params = (PTPParams *) device->params;
Linus Walleijc6210fb2006-05-08 11:11:41 +00003151 PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo;
Linus Walleije1ac07e2006-12-14 19:38:59 +00003152 LIBMTP_devicestorage_t *storage = device->storage;
Linus Walleij38d7ee82010-07-24 22:17:21 +00003153 LIBMTP_device_extension_t *tmpext = device->extensions;
mopoke96143402006-10-30 04:37:26 +00003154
Linus Walleijc6210fb2006-05-08 11:11:41 +00003155 printf("USB low-level info:\n");
3156 dump_usbinfo(ptp_usb);
Linus Walleij8c45b292006-04-26 14:12:44 +00003157 /* Print out some verbose information */
3158 printf("Device info:\n");
3159 printf(" Manufacturer: %s\n", params->deviceinfo.Manufacturer);
3160 printf(" Model: %s\n", params->deviceinfo.Model);
3161 printf(" Device version: %s\n", params->deviceinfo.DeviceVersion);
3162 printf(" Serial number: %s\n", params->deviceinfo.SerialNumber);
Linus Walleij43aba612011-11-25 09:40:34 +01003163 printf(" Vendor extension ID: 0x%08x\n",
3164 params->deviceinfo.VendorExtensionID);
3165 printf(" Vendor extension description: %s\n",
3166 params->deviceinfo.VendorExtensionDesc);
3167 printf(" Detected object size: %d bits\n",
3168 device->object_bitsize);
Linus Walleij38d7ee82010-07-24 22:17:21 +00003169 printf(" Extensions:\n");
3170 while (tmpext != NULL) {
3171 printf(" %s: %d.%d\n",
3172 tmpext->name,
3173 tmpext->major,
3174 tmpext->minor);
3175 tmpext = tmpext->next;
3176 }
Linus Walleij8c45b292006-04-26 14:12:44 +00003177 printf("Supported operations:\n");
Marcus Meissner5e6ef6c2016-11-18 21:21:15 +01003178 for (i=0;i<params->deviceinfo.OperationsSupported_len;i++)
3179 printf(" %04x: %s\n", params->deviceinfo.OperationsSupported[i], ptp_get_opcode_name(params, params->deviceinfo.OperationsSupported[i]));
Linus Walleij8c45b292006-04-26 14:12:44 +00003180 printf("Events supported:\n");
3181 if (params->deviceinfo.EventsSupported_len == 0) {
3182 printf(" None.\n");
3183 } else {
3184 for (i=0;i<params->deviceinfo.EventsSupported_len;i++) {
Marcus Meissner5e6ef6c2016-11-18 21:21:15 +01003185 printf(" 0x%04x (%s)\n", params->deviceinfo.EventsSupported[i], ptp_strerror(params->deviceinfo.EventsSupported[i], params->deviceinfo.VendorExtensionID));
Linus Walleij8c45b292006-04-26 14:12:44 +00003186 }
3187 }
3188 printf("Device Properties Supported:\n");
3189 for (i=0;i<params->deviceinfo.DevicePropertiesSupported_len;i++) {
Linus Walleij43aba612011-11-25 09:40:34 +01003190 char const *propdesc = ptp_get_property_description(params,
3191 params->deviceinfo.DevicePropertiesSupported[i]);
mopoke96143402006-10-30 04:37:26 +00003192
Linus Walleij545c7792006-06-13 15:22:30 +00003193 if (propdesc != NULL) {
Linus Walleij43aba612011-11-25 09:40:34 +01003194 printf(" 0x%04x: %s\n",
3195 params->deviceinfo.DevicePropertiesSupported[i], propdesc);
Linus Walleij545c7792006-06-13 15:22:30 +00003196 } else {
3197 uint16_t prop = params->deviceinfo.DevicePropertiesSupported[i];
Linus Walleijcf223e62006-06-19 09:31:53 +00003198 printf(" 0x%04x: Unknown property\n", prop);
Linus Walleij545c7792006-06-13 15:22:30 +00003199 }
Linus Walleij8c45b292006-04-26 14:12:44 +00003200 }
Linus Walleij0af979a2006-06-19 11:49:10 +00003201
3202 if (ptp_operation_issupported(params,PTP_OC_MTP_GetObjectPropsSupported)) {
3203 printf("Playable File (Object) Types and Object Properties Supported:\n");
3204 for (i=0;i<params->deviceinfo.ImageFormats_len;i++) {
3205 char txt[256];
3206 uint16_t ret;
3207 uint16_t *props = NULL;
3208 uint32_t propcnt = 0;
3209 int j;
mopoke96143402006-10-30 04:37:26 +00003210
Linus Walleij43aba612011-11-25 09:40:34 +01003211 (void) ptp_render_ofc (params, params->deviceinfo.ImageFormats[i],
3212 sizeof(txt), txt);
Linus Walleij0af979a2006-06-19 11:49:10 +00003213 printf(" %04x: %s\n", params->deviceinfo.ImageFormats[i], txt);
mopoke96143402006-10-30 04:37:26 +00003214
Linus Walleij43aba612011-11-25 09:40:34 +01003215 ret = ptp_mtp_getobjectpropssupported (params,
3216 params->deviceinfo.ImageFormats[i], &propcnt, &props);
Linus Walleij0af979a2006-06-19 11:49:10 +00003217 if (ret != PTP_RC_OK) {
Linus Walleij43aba612011-11-25 09:40:34 +01003218 add_ptp_error_to_errorstack(device, ret, "LIBMTP_Dump_Device_Info(): "
3219 "error on query for object properties.");
Linus Walleij0af979a2006-06-19 11:49:10 +00003220 } else {
3221 for (j=0;j<propcnt;j++) {
Linus Walleij14830342007-03-23 13:32:00 +00003222 PTPObjectPropDesc opd;
3223 int k;
Linus Walleij2c1bbd62009-11-07 15:15:03 +00003224
Linus Walleij43aba612011-11-25 09:40:34 +01003225 printf(" %04x: %s", props[j],
3226 LIBMTP_Get_Property_Description(map_ptp_property_to_libmtp_property(props[j])));
Linus Walleij14830342007-03-23 13:32:00 +00003227 // Get a more verbose description
Linus Walleij43aba612011-11-25 09:40:34 +01003228 ret = ptp_mtp_getobjectpropdesc(params, props[j],
3229 params->deviceinfo.ImageFormats[i],
3230 &opd);
Linus Walleij14830342007-03-23 13:32:00 +00003231 if (ret != PTP_RC_OK) {
Linus Walleij43aba612011-11-25 09:40:34 +01003232 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL,
3233 "LIBMTP_Dump_Device_Info(): "
Linus Walleij14830342007-03-23 13:32:00 +00003234 "could not get property description.");
3235 break;
3236 }
3237
3238 if (opd.DataType == PTP_DTC_STR) {
3239 printf(" STRING data type");
Linus Walleij22cc4872007-11-05 22:57:19 +00003240 switch (opd.FormFlag) {
3241 case PTP_OPFF_DateTime:
3242 printf(" DATETIME FORM");
3243 break;
3244 case PTP_OPFF_RegularExpression:
3245 printf(" REGULAR EXPRESSION FORM");
3246 break;
3247 case PTP_OPFF_LongString:
3248 printf(" LONG STRING FORM");
3249 break;
3250 default:
3251 break;
3252 }
Linus Walleij14830342007-03-23 13:32:00 +00003253 } else {
3254 if (opd.DataType & PTP_DTC_ARRAY_MASK) {
3255 printf(" array of");
3256 }
3257
3258 switch (opd.DataType & (~PTP_DTC_ARRAY_MASK)) {
3259
3260 case PTP_DTC_UNDEF:
3261 printf(" UNDEFINED data type");
3262 break;
Linus Walleij14830342007-03-23 13:32:00 +00003263 case PTP_DTC_INT8:
3264 printf(" INT8 data type");
3265 switch (opd.FormFlag) {
Linus Walleij22cc4872007-11-05 22:57:19 +00003266 case PTP_OPFF_Range:
Linus Walleij14830342007-03-23 13:32:00 +00003267 printf(" range: MIN %d, MAX %d, STEP %d",
3268 opd.FORM.Range.MinimumValue.i8,
3269 opd.FORM.Range.MaximumValue.i8,
3270 opd.FORM.Range.StepSize.i8);
3271 break;
Linus Walleij22cc4872007-11-05 22:57:19 +00003272 case PTP_OPFF_Enumeration:
Linus Walleij14830342007-03-23 13:32:00 +00003273 printf(" enumeration: ");
3274 for(k=0;k<opd.FORM.Enum.NumberOfValues;k++) {
3275 printf("%d, ", opd.FORM.Enum.SupportedValue[k].i8);
3276 }
3277 break;
Linus Walleij22cc4872007-11-05 22:57:19 +00003278 case PTP_OPFF_ByteArray:
3279 printf(" byte array: ");
3280 break;
Linus Walleij14830342007-03-23 13:32:00 +00003281 default:
3282 printf(" ANY 8BIT VALUE form");
3283 break;
3284 }
3285 break;
Linus Walleij2c1bbd62009-11-07 15:15:03 +00003286
Linus Walleij14830342007-03-23 13:32:00 +00003287 case PTP_DTC_UINT8:
3288 printf(" UINT8 data type");
3289 switch (opd.FormFlag) {
Linus Walleij22cc4872007-11-05 22:57:19 +00003290 case PTP_OPFF_Range:
Linus Walleij14830342007-03-23 13:32:00 +00003291 printf(" range: MIN %d, MAX %d, STEP %d",
3292 opd.FORM.Range.MinimumValue.u8,
3293 opd.FORM.Range.MaximumValue.u8,
3294 opd.FORM.Range.StepSize.u8);
3295 break;
Linus Walleij22cc4872007-11-05 22:57:19 +00003296 case PTP_OPFF_Enumeration:
Linus Walleij14830342007-03-23 13:32:00 +00003297 printf(" enumeration: ");
3298 for(k=0;k<opd.FORM.Enum.NumberOfValues;k++) {
3299 printf("%d, ", opd.FORM.Enum.SupportedValue[k].u8);
3300 }
3301 break;
Linus Walleij22cc4872007-11-05 22:57:19 +00003302 case PTP_OPFF_ByteArray:
3303 printf(" byte array: ");
3304 break;
Linus Walleij14830342007-03-23 13:32:00 +00003305 default:
3306 printf(" ANY 8BIT VALUE form");
3307 break;
3308 }
3309 break;
Linus Walleij2c1bbd62009-11-07 15:15:03 +00003310
Linus Walleij14830342007-03-23 13:32:00 +00003311 case PTP_DTC_INT16:
3312 printf(" INT16 data type");
3313 switch (opd.FormFlag) {
Linus Walleij22cc4872007-11-05 22:57:19 +00003314 case PTP_OPFF_Range:
Linus Walleij14830342007-03-23 13:32:00 +00003315 printf(" range: MIN %d, MAX %d, STEP %d",
3316 opd.FORM.Range.MinimumValue.i16,
3317 opd.FORM.Range.MaximumValue.i16,
3318 opd.FORM.Range.StepSize.i16);
3319 break;
Linus Walleij22cc4872007-11-05 22:57:19 +00003320 case PTP_OPFF_Enumeration:
Linus Walleij14830342007-03-23 13:32:00 +00003321 printf(" enumeration: ");
3322 for(k=0;k<opd.FORM.Enum.NumberOfValues;k++) {
3323 printf("%d, ", opd.FORM.Enum.SupportedValue[k].i16);
3324 }
3325 break;
3326 default:
3327 printf(" ANY 16BIT VALUE form");
3328 break;
3329 }
3330 break;
Linus Walleij2c1bbd62009-11-07 15:15:03 +00003331
Linus Walleij14830342007-03-23 13:32:00 +00003332 case PTP_DTC_UINT16:
3333 printf(" UINT16 data type");
3334 switch (opd.FormFlag) {
Linus Walleij22cc4872007-11-05 22:57:19 +00003335 case PTP_OPFF_Range:
Linus Walleij14830342007-03-23 13:32:00 +00003336 printf(" range: MIN %d, MAX %d, STEP %d",
3337 opd.FORM.Range.MinimumValue.u16,
3338 opd.FORM.Range.MaximumValue.u16,
3339 opd.FORM.Range.StepSize.u16);
3340 break;
Linus Walleij22cc4872007-11-05 22:57:19 +00003341 case PTP_OPFF_Enumeration:
Linus Walleij14830342007-03-23 13:32:00 +00003342 printf(" enumeration: ");
3343 for(k=0;k<opd.FORM.Enum.NumberOfValues;k++) {
3344 printf("%d, ", opd.FORM.Enum.SupportedValue[k].u16);
3345 }
3346 break;
3347 default:
3348 printf(" ANY 16BIT VALUE form");
3349 break;
3350 }
3351 break;
Linus Walleij2c1bbd62009-11-07 15:15:03 +00003352
Linus Walleij14830342007-03-23 13:32:00 +00003353 case PTP_DTC_INT32:
3354 printf(" INT32 data type");
3355 switch (opd.FormFlag) {
Linus Walleij22cc4872007-11-05 22:57:19 +00003356 case PTP_OPFF_Range:
Linus Walleij14830342007-03-23 13:32:00 +00003357 printf(" range: MIN %d, MAX %d, STEP %d",
3358 opd.FORM.Range.MinimumValue.i32,
3359 opd.FORM.Range.MaximumValue.i32,
3360 opd.FORM.Range.StepSize.i32);
3361 break;
Linus Walleij22cc4872007-11-05 22:57:19 +00003362 case PTP_OPFF_Enumeration:
Linus Walleij14830342007-03-23 13:32:00 +00003363 printf(" enumeration: ");
3364 for(k=0;k<opd.FORM.Enum.NumberOfValues;k++) {
3365 printf("%d, ", opd.FORM.Enum.SupportedValue[k].i32);
3366 }
3367 break;
3368 default:
3369 printf(" ANY 32BIT VALUE form");
3370 break;
3371 }
3372 break;
Linus Walleij2c1bbd62009-11-07 15:15:03 +00003373
Linus Walleij14830342007-03-23 13:32:00 +00003374 case PTP_DTC_UINT32:
3375 printf(" UINT32 data type");
3376 switch (opd.FormFlag) {
Linus Walleij22cc4872007-11-05 22:57:19 +00003377 case PTP_OPFF_Range:
Linus Walleij14830342007-03-23 13:32:00 +00003378 printf(" range: MIN %d, MAX %d, STEP %d",
3379 opd.FORM.Range.MinimumValue.u32,
3380 opd.FORM.Range.MaximumValue.u32,
3381 opd.FORM.Range.StepSize.u32);
3382 break;
Linus Walleij22cc4872007-11-05 22:57:19 +00003383 case PTP_OPFF_Enumeration:
Linus Walleijd71d0b32008-09-22 08:21:03 +00003384 // Special pretty-print for FOURCC codes
3385 if (params->deviceinfo.ImageFormats[i] == PTP_OPC_VideoFourCCCodec) {
3386 printf(" enumeration of u32 casted FOURCC: ");
3387 for (k=0;k<opd.FORM.Enum.NumberOfValues;k++) {
3388 if (opd.FORM.Enum.SupportedValue[k].u32 == 0) {
3389 printf("ANY, ");
3390 } else {
3391 char fourcc[6];
3392 fourcc[0] = (opd.FORM.Enum.SupportedValue[k].u32 >> 24) & 0xFFU;
3393 fourcc[1] = (opd.FORM.Enum.SupportedValue[k].u32 >> 16) & 0xFFU;
3394 fourcc[2] = (opd.FORM.Enum.SupportedValue[k].u32 >> 8) & 0xFFU;
3395 fourcc[3] = opd.FORM.Enum.SupportedValue[k].u32 & 0xFFU;
3396 fourcc[4] = '\n';
3397 fourcc[5] = '\0';
3398 printf("\"%s\", ", fourcc);
3399 }
3400 }
3401 } else {
3402 printf(" enumeration: ");
3403 for(k=0;k<opd.FORM.Enum.NumberOfValues;k++) {
3404 printf("%d, ", opd.FORM.Enum.SupportedValue[k].u32);
3405 }
Linus Walleij14830342007-03-23 13:32:00 +00003406 }
3407 break;
3408 default:
3409 printf(" ANY 32BIT VALUE form");
3410 break;
3411 }
3412 break;
Linus Walleij2c1bbd62009-11-07 15:15:03 +00003413
Linus Walleij14830342007-03-23 13:32:00 +00003414 case PTP_DTC_INT64:
3415 printf(" INT64 data type");
3416 break;
Linus Walleij2c1bbd62009-11-07 15:15:03 +00003417
Linus Walleij14830342007-03-23 13:32:00 +00003418 case PTP_DTC_UINT64:
3419 printf(" UINT64 data type");
3420 break;
Linus Walleij2c1bbd62009-11-07 15:15:03 +00003421
Linus Walleij14830342007-03-23 13:32:00 +00003422 case PTP_DTC_INT128:
3423 printf(" INT128 data type");
3424 break;
Linus Walleij2c1bbd62009-11-07 15:15:03 +00003425
Linus Walleij14830342007-03-23 13:32:00 +00003426 case PTP_DTC_UINT128:
3427 printf(" UINT128 data type");
3428 break;
Linus Walleij2c1bbd62009-11-07 15:15:03 +00003429
Linus Walleij14830342007-03-23 13:32:00 +00003430 default:
3431 printf(" UNKNOWN data type");
3432 break;
3433 }
3434 }
3435 if (opd.GetSet) {
3436 printf(" GET/SET");
3437 } else {
3438 printf(" READ ONLY");
3439 }
3440 printf("\n");
Linus Walleijdbcc8242007-08-05 22:31:26 +00003441 ptp_free_objectpropdesc(&opd);
Linus Walleij0af979a2006-06-19 11:49:10 +00003442 }
3443 free(props);
3444 }
3445 }
3446 }
mopoke96143402006-10-30 04:37:26 +00003447
Linus Walleij43aba612011-11-25 09:40:34 +01003448 if(storage != NULL &&
3449 ptp_operation_issupported(params,PTP_OC_GetStorageInfo)) {
Linus Walleij9e1b0812006-12-12 19:22:02 +00003450 printf("Storage Devices:\n");
Linus Walleije1ac07e2006-12-14 19:38:59 +00003451 while(storage != NULL) {
3452 printf(" StorageID: 0x%08x\n",storage->id);
Linus Walleijd71d0b32008-09-22 08:21:03 +00003453 printf(" StorageType: 0x%04x ",storage->StorageType);
3454 switch (storage->StorageType) {
3455 case PTP_ST_Undefined:
3456 printf("(undefined)\n");
3457 break;
3458 case PTP_ST_FixedROM:
3459 printf("fixed ROM storage\n");
3460 break;
3461 case PTP_ST_RemovableROM:
3462 printf("removable ROM storage\n");
3463 break;
3464 case PTP_ST_FixedRAM:
3465 printf("fixed RAM storage\n");
3466 break;
3467 case PTP_ST_RemovableRAM:
3468 printf("removable RAM storage\n");
3469 break;
3470 default:
3471 printf("UNKNOWN storage\n");
3472 break;
3473 }
3474 printf(" FilesystemType: 0x%04x ",storage->FilesystemType);
3475 switch(storage->FilesystemType) {
3476 case PTP_FST_Undefined:
3477 printf("(undefined)\n");
3478 break;
3479 case PTP_FST_GenericFlat:
3480 printf("generic flat filesystem\n");
3481 break;
3482 case PTP_FST_GenericHierarchical:
3483 printf("generic hierarchical\n");
3484 break;
3485 case PTP_FST_DCF:
3486 printf("DCF\n");
3487 break;
3488 default:
3489 printf("UNKNONWN filesystem type\n");
3490 break;
3491 }
3492 printf(" AccessCapability: 0x%04x ",storage->AccessCapability);
3493 switch(storage->AccessCapability) {
3494 case PTP_AC_ReadWrite:
3495 printf("read/write\n");
3496 break;
3497 case PTP_AC_ReadOnly:
3498 printf("read only\n");
3499 break;
3500 case PTP_AC_ReadOnly_with_Object_Deletion:
3501 printf("read only + object deletion\n");
3502 break;
3503 default:
3504 printf("UNKNOWN access capability\n");
3505 break;
3506 }
Linus Walleij43aba612011-11-25 09:40:34 +01003507 printf(" MaxCapacity: %llu\n",
3508 (long long unsigned int) storage->MaxCapacity);
3509 printf(" FreeSpaceInBytes: %llu\n",
3510 (long long unsigned int) storage->FreeSpaceInBytes);
3511 printf(" FreeSpaceInObjects: %llu\n",
3512 (long long unsigned int) storage->FreeSpaceInObjects);
Linus Walleije1ac07e2006-12-14 19:38:59 +00003513 printf(" StorageDescription: %s\n",storage->StorageDescription);
3514 printf(" VolumeIdentifier: %s\n",storage->VolumeIdentifier);
3515 storage = storage->next;
Linus Walleij9e1b0812006-12-12 19:22:02 +00003516 }
3517 }
3518
Linus Walleij545c7792006-06-13 15:22:30 +00003519 printf("Special directories:\n");
Linus Walleij43aba612011-11-25 09:40:34 +01003520 printf(" Default music folder: 0x%08x\n",
3521 device->default_music_folder);
3522 printf(" Default playlist folder: 0x%08x\n",
3523 device->default_playlist_folder);
3524 printf(" Default picture folder: 0x%08x\n",
3525 device->default_picture_folder);
3526 printf(" Default video folder: 0x%08x\n",
3527 device->default_video_folder);
3528 printf(" Default organizer folder: 0x%08x\n",
3529 device->default_organizer_folder);
3530 printf(" Default zencast folder: 0x%08x\n",
3531 device->default_zencast_folder);
3532 printf(" Default album folder: 0x%08x\n",
3533 device->default_album_folder);
3534 printf(" Default text folder: 0x%08x\n",
3535 device->default_text_folder);
Linus Walleij8c45b292006-04-26 14:12:44 +00003536}
3537
3538/**
Linus Walleij5d533bb2007-07-17 21:48:57 +00003539 * This resets a device in case it supports the <code>PTP_OC_ResetDevice</code>
3540 * operation code (0x1010).
3541 * @param device a pointer to the device to reset.
3542 * @return 0 on success, any other value means failure.
3543 */
3544int LIBMTP_Reset_Device(LIBMTP_mtpdevice_t *device)
3545{
3546 PTPParams *params = (PTPParams *) device->params;
3547 uint16_t ret;
3548
3549 if (!ptp_operation_issupported(params,PTP_OC_ResetDevice)) {
Linus Walleij2c1bbd62009-11-07 15:15:03 +00003550 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL,
Linus Walleij43aba612011-11-25 09:40:34 +01003551 "LIBMTP_Reset_Device(): "
3552 "device does not support resetting.");
Linus Walleij5d533bb2007-07-17 21:48:57 +00003553 return -1;
3554 }
3555 ret = ptp_resetdevice(params);
3556 if (ret != PTP_RC_OK) {
3557 add_ptp_error_to_errorstack(device, ret, "Error resetting.");
3558 return -1;
3559 }
3560 return 0;
3561}
3562
3563/**
Linus Walleij2350b712008-01-14 22:54:37 +00003564 * This retrieves the manufacturer name of an MTP device.
3565 * @param device a pointer to the device to get the manufacturer name for.
3566 * @return a newly allocated UTF-8 string representing the manufacturer name.
3567 * The string must be freed by the caller after use. If the call
3568 * was unsuccessful this will contain NULL.
3569 */
3570char *LIBMTP_Get_Manufacturername(LIBMTP_mtpdevice_t *device)
3571{
3572 char *retmanuf = NULL;
3573 PTPParams *params = (PTPParams *) device->params;
3574
3575 if (params->deviceinfo.Manufacturer != NULL) {
3576 retmanuf = strdup(params->deviceinfo.Manufacturer);
3577 }
3578 return retmanuf;
3579}
3580
3581/**
mopoke96143402006-10-30 04:37:26 +00003582 * This retrieves the model name (often equal to product name)
Linus Walleij80124062006-03-15 10:26:09 +00003583 * of an MTP device.
3584 * @param device a pointer to the device to get the model name for.
3585 * @return a newly allocated UTF-8 string representing the model name.
3586 * The string must be freed by the caller after use. If the call
3587 * was unsuccessful this will contain NULL.
3588 */
3589char *LIBMTP_Get_Modelname(LIBMTP_mtpdevice_t *device)
3590{
3591 char *retmodel = NULL;
Linus Walleij9b28da32006-03-16 13:47:58 +00003592 PTPParams *params = (PTPParams *) device->params;
mopoke96143402006-10-30 04:37:26 +00003593
Linus Walleij9b28da32006-03-16 13:47:58 +00003594 if (params->deviceinfo.Model != NULL) {
3595 retmodel = strdup(params->deviceinfo.Model);
Linus Walleij80124062006-03-15 10:26:09 +00003596 }
3597 return retmodel;
3598}
3599
3600/**
3601 * This retrieves the serial number of an MTP device.
3602 * @param device a pointer to the device to get the serial number for.
3603 * @return a newly allocated UTF-8 string representing the serial number.
3604 * The string must be freed by the caller after use. If the call
3605 * was unsuccessful this will contain NULL.
3606 */
3607char *LIBMTP_Get_Serialnumber(LIBMTP_mtpdevice_t *device)
3608{
3609 char *retnumber = NULL;
Linus Walleij9b28da32006-03-16 13:47:58 +00003610 PTPParams *params = (PTPParams *) device->params;
mopoke96143402006-10-30 04:37:26 +00003611
Linus Walleij9b28da32006-03-16 13:47:58 +00003612 if (params->deviceinfo.SerialNumber != NULL) {
3613 retnumber = strdup(params->deviceinfo.SerialNumber);
Linus Walleij80124062006-03-15 10:26:09 +00003614 }
3615 return retnumber;
3616}
3617
3618/**
mopoke96143402006-10-30 04:37:26 +00003619 * This retrieves the device version (hardware and firmware version) of an
Linus Walleij80124062006-03-15 10:26:09 +00003620 * MTP device.
3621 * @param device a pointer to the device to get the device version for.
3622 * @return a newly allocated UTF-8 string representing the device version.
3623 * The string must be freed by the caller after use. If the call
3624 * was unsuccessful this will contain NULL.
3625 */
3626char *LIBMTP_Get_Deviceversion(LIBMTP_mtpdevice_t *device)
3627{
3628 char *retversion = NULL;
Linus Walleij9b28da32006-03-16 13:47:58 +00003629 PTPParams *params = (PTPParams *) device->params;
mopoke96143402006-10-30 04:37:26 +00003630
Linus Walleij9b28da32006-03-16 13:47:58 +00003631 if (params->deviceinfo.DeviceVersion != NULL) {
3632 retversion = strdup(params->deviceinfo.DeviceVersion);
Linus Walleij80124062006-03-15 10:26:09 +00003633 }
3634 return retversion;
3635}
3636
3637
3638/**
Linus Walleijfae27482006-08-19 20:13:25 +00003639 * This retrieves the "friendly name" of an MTP device. Usually
3640 * this is simply the name of the owner or something like
Linus Walleij30658792006-08-19 22:18:55 +00003641 * "John Doe's Digital Audio Player". This property should be supported
Linus Walleijfae27482006-08-19 20:13:25 +00003642 * by all MTP devices.
3643 * @param device a pointer to the device to get the friendly name for.
mopoke96143402006-10-30 04:37:26 +00003644 * @return a newly allocated UTF-8 string representing the friendly name.
Linus Walleijb9256fd2006-02-15 09:40:43 +00003645 * The string must be freed by the caller after use.
Linus Walleij30658792006-08-19 22:18:55 +00003646 * @see LIBMTP_Set_Friendlyname()
Linus Walleijb9256fd2006-02-15 09:40:43 +00003647 */
Linus Walleij30658792006-08-19 22:18:55 +00003648char *LIBMTP_Get_Friendlyname(LIBMTP_mtpdevice_t *device)
Linus Walleijb9256fd2006-02-15 09:40:43 +00003649{
Linus Walleijb02a0662006-04-25 08:05:09 +00003650 PTPPropertyValue propval;
Linus Walleijb9256fd2006-02-15 09:40:43 +00003651 char *retstring = NULL;
Linus Walleij9b28da32006-03-16 13:47:58 +00003652 PTPParams *params = (PTPParams *) device->params;
Linus Walleij070e9b42007-01-22 08:49:28 +00003653 uint16_t ret;
Linus Walleijb9256fd2006-02-15 09:40:43 +00003654
Linus Walleijcf223e62006-06-19 09:31:53 +00003655 if (!ptp_property_issupported(params, PTP_DPC_MTP_DeviceFriendlyName)) {
3656 return NULL;
3657 }
3658
Linus Walleij070e9b42007-01-22 08:49:28 +00003659 ret = ptp_getdevicepropvalue(params,
3660 PTP_DPC_MTP_DeviceFriendlyName,
3661 &propval,
3662 PTP_DTC_STR);
3663 if (ret != PTP_RC_OK) {
3664 add_ptp_error_to_errorstack(device, ret, "Error getting friendlyname.");
Linus Walleijb9256fd2006-02-15 09:40:43 +00003665 return NULL;
3666 }
Linus Walleija823a702006-08-27 21:27:46 +00003667 if (propval.str != NULL) {
3668 retstring = strdup(propval.str);
3669 free(propval.str);
3670 }
Linus Walleijfae27482006-08-19 20:13:25 +00003671 return retstring;
3672}
3673
3674/**
Linus Walleij30658792006-08-19 22:18:55 +00003675 * Sets the "friendly name" of an MTP device.
3676 * @param device a pointer to the device to set the friendly name for.
3677 * @param friendlyname the new friendly name for the device.
3678 * @return 0 on success, any other value means failure.
Linus Walleijd5b34972008-09-24 20:24:12 +00003679 * @see LIBMTP_Get_Friendlyname()
Linus Walleij30658792006-08-19 22:18:55 +00003680 */
3681int LIBMTP_Set_Friendlyname(LIBMTP_mtpdevice_t *device,
3682 char const * const friendlyname)
3683{
3684 PTPPropertyValue propval;
3685 PTPParams *params = (PTPParams *) device->params;
Linus Walleij070e9b42007-01-22 08:49:28 +00003686 uint16_t ret;
Linus Walleij30658792006-08-19 22:18:55 +00003687
3688 if (!ptp_property_issupported(params, PTP_DPC_MTP_DeviceFriendlyName)) {
3689 return -1;
3690 }
Linus Walleija823a702006-08-27 21:27:46 +00003691 propval.str = (char *) friendlyname;
Linus Walleij070e9b42007-01-22 08:49:28 +00003692 ret = ptp_setdevicepropvalue(params,
3693 PTP_DPC_MTP_DeviceFriendlyName,
3694 &propval,
3695 PTP_DTC_STR);
3696 if (ret != PTP_RC_OK) {
3697 add_ptp_error_to_errorstack(device, ret, "Error setting friendlyname.");
Linus Walleij30658792006-08-19 22:18:55 +00003698 return -1;
3699 }
Linus Walleij30658792006-08-19 22:18:55 +00003700 return 0;
3701}
3702
3703/**
Linus Walleijfae27482006-08-19 20:13:25 +00003704 * This retrieves the syncronization partner of an MTP device. This
3705 * property should be supported by all MTP devices.
3706 * @param device a pointer to the device to get the sync partner for.
3707 * @return a newly allocated UTF-8 string representing the synchronization
3708 * partner. The string must be freed by the caller after use.
Linus Walleij30658792006-08-19 22:18:55 +00003709 * @see LIBMTP_Set_Syncpartner()
Linus Walleijfae27482006-08-19 20:13:25 +00003710 */
3711char *LIBMTP_Get_Syncpartner(LIBMTP_mtpdevice_t *device)
3712{
3713 PTPPropertyValue propval;
3714 char *retstring = NULL;
3715 PTPParams *params = (PTPParams *) device->params;
Linus Walleij070e9b42007-01-22 08:49:28 +00003716 uint16_t ret;
Linus Walleijfae27482006-08-19 20:13:25 +00003717
3718 if (!ptp_property_issupported(params, PTP_DPC_MTP_SynchronizationPartner)) {
3719 return NULL;
3720 }
3721
Linus Walleij070e9b42007-01-22 08:49:28 +00003722 ret = ptp_getdevicepropvalue(params,
3723 PTP_DPC_MTP_SynchronizationPartner,
3724 &propval,
3725 PTP_DTC_STR);
3726 if (ret != PTP_RC_OK) {
3727 add_ptp_error_to_errorstack(device, ret, "Error getting syncpartner.");
Linus Walleijfae27482006-08-19 20:13:25 +00003728 return NULL;
3729 }
Linus Walleija823a702006-08-27 21:27:46 +00003730 if (propval.str != NULL) {
3731 retstring = strdup(propval.str);
3732 free(propval.str);
3733 }
Linus Walleijb9256fd2006-02-15 09:40:43 +00003734 return retstring;
3735}
3736
Linus Walleij30658792006-08-19 22:18:55 +00003737
3738/**
3739 * Sets the synchronization partner of an MTP device. Note that
3740 * we have no idea what the effect of setting this to "foobar"
3741 * may be. But the general idea seems to be to tell which program
3742 * shall synchronize with this device and tell others to leave
3743 * it alone.
3744 * @param device a pointer to the device to set the sync partner for.
3745 * @param syncpartner the new synchronization partner for the device.
3746 * @return 0 on success, any other value means failure.
3747 * @see LIBMTP_Get_Syncpartner()
3748 */
3749int LIBMTP_Set_Syncpartner(LIBMTP_mtpdevice_t *device,
3750 char const * const syncpartner)
3751{
3752 PTPPropertyValue propval;
3753 PTPParams *params = (PTPParams *) device->params;
Linus Walleij070e9b42007-01-22 08:49:28 +00003754 uint16_t ret;
mopoke96143402006-10-30 04:37:26 +00003755
Linus Walleij30658792006-08-19 22:18:55 +00003756 if (!ptp_property_issupported(params, PTP_DPC_MTP_SynchronizationPartner)) {
3757 return -1;
3758 }
Linus Walleija823a702006-08-27 21:27:46 +00003759 propval.str = (char *) syncpartner;
Linus Walleij070e9b42007-01-22 08:49:28 +00003760 ret = ptp_setdevicepropvalue(params,
3761 PTP_DPC_MTP_SynchronizationPartner,
3762 &propval,
3763 PTP_DTC_STR);
3764 if (ret != PTP_RC_OK) {
3765 add_ptp_error_to_errorstack(device, ret, "Error setting syncpartner.");
Linus Walleij30658792006-08-19 22:18:55 +00003766 return -1;
3767 }
Linus Walleij30658792006-08-19 22:18:55 +00003768 return 0;
3769}
3770
Linus Walleij394bbbe2006-02-22 16:10:53 +00003771/**
Linus Walleijf5fcda32006-12-03 22:31:02 +00003772 * Checks if the device can stora a file of this size or
3773 * if it's too big.
3774 * @param device a pointer to the device.
3775 * @param filesize the size of the file to check whether it will fit.
Linus Walleij6bf68b42007-09-03 22:50:02 +00003776 * @param storageid the ID of the storage to try to fit the file on.
Linus Walleijf5fcda32006-12-03 22:31:02 +00003777 * @return 0 if the file fits, any other value means failure.
3778 */
Linus Walleij6bf68b42007-09-03 22:50:02 +00003779static int check_if_file_fits(LIBMTP_mtpdevice_t *device,
3780 LIBMTP_devicestorage_t *storage,
3781 uint64_t const filesize) {
Linus Walleijf5fcda32006-12-03 22:31:02 +00003782 PTPParams *params = (PTPParams *) device->params;
Linus Walleijf5fcda32006-12-03 22:31:02 +00003783 uint64_t freebytes;
Linus Walleijf5fcda32006-12-03 22:31:02 +00003784 int ret;
3785
3786 // If we cannot check the storage, no big deal.
3787 if (!ptp_operation_issupported(params,PTP_OC_GetStorageInfo)) {
3788 return 0;
3789 }
Linus Walleij2c1bbd62009-11-07 15:15:03 +00003790
Linus Walleij6bf68b42007-09-03 22:50:02 +00003791 ret = get_storage_freespace(device, storage, &freebytes);
Linus Walleijf5fcda32006-12-03 22:31:02 +00003792 if (ret != 0) {
Linus Walleij2c1bbd62009-11-07 15:15:03 +00003793 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL,
Linus Walleij6bf68b42007-09-03 22:50:02 +00003794 "check_if_file_fits(): error checking free storage.");
Linus Walleijf5fcda32006-12-03 22:31:02 +00003795 return -1;
3796 } else {
Linus Walleijfb28b632007-10-23 21:56:18 +00003797 // See if it fits.
Linus Walleijf5fcda32006-12-03 22:31:02 +00003798 if (filesize > freebytes) {
Linus Walleijf5fcda32006-12-03 22:31:02 +00003799 return -1;
3800 }
3801 }
3802 return 0;
3803}
3804
3805
Linus Walleijf5fcda32006-12-03 22:31:02 +00003806/**
Linus Walleijfa1374c2006-02-27 07:41:46 +00003807 * This function retrieves the current battery level on the device.
3808 * @param device a pointer to the device to get the battery level for.
mopoke96143402006-10-30 04:37:26 +00003809 * @param maximum_level a pointer to a variable that will hold the
Linus Walleijfa1374c2006-02-27 07:41:46 +00003810 * maximum level of the battery if the call was successful.
mopoke96143402006-10-30 04:37:26 +00003811 * @param current_level a pointer to a variable that will hold the
Linus Walleijfa1374c2006-02-27 07:41:46 +00003812 * current level of the battery if the call was successful.
Linus Walleij545c7792006-06-13 15:22:30 +00003813 * A value of 0 means that the device is on external power.
Linus Walleijfa1374c2006-02-27 07:41:46 +00003814 * @return 0 if the storage info was successfully retrieved, any other
Linus Walleij80439342006-09-12 10:42:26 +00003815 * means failure. A typical cause of failure is that
Linus Walleij545c7792006-06-13 15:22:30 +00003816 * the device does not support the battery level property.
Linus Walleijfa1374c2006-02-27 07:41:46 +00003817 */
mopoke96143402006-10-30 04:37:26 +00003818int LIBMTP_Get_Batterylevel(LIBMTP_mtpdevice_t *device,
3819 uint8_t * const maximum_level,
Linus Walleijfa1374c2006-02-27 07:41:46 +00003820 uint8_t * const current_level)
3821{
Linus Walleijb02a0662006-04-25 08:05:09 +00003822 PTPPropertyValue propval;
Linus Walleijfa1374c2006-02-27 07:41:46 +00003823 uint16_t ret;
Linus Walleij9b28da32006-03-16 13:47:58 +00003824 PTPParams *params = (PTPParams *) device->params;
Linus Walleij4096c882009-03-16 23:32:34 +00003825 PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo;
Linus Walleijfa1374c2006-02-27 07:41:46 +00003826
Linus Walleij545c7792006-06-13 15:22:30 +00003827 *maximum_level = 0;
3828 *current_level = 0;
3829
Linus Walleij4096c882009-03-16 23:32:34 +00003830 if (FLAG_BROKEN_BATTERY_LEVEL(ptp_usb) ||
3831 !ptp_property_issupported(params, PTP_DPC_BatteryLevel)) {
Linus Walleij545c7792006-06-13 15:22:30 +00003832 return -1;
3833 }
mopoke96143402006-10-30 04:37:26 +00003834
Linus Walleij43aba612011-11-25 09:40:34 +01003835 ret = ptp_getdevicepropvalue(params, PTP_DPC_BatteryLevel,
3836 &propval, PTP_DTC_UINT8);
Linus Walleijb02a0662006-04-25 08:05:09 +00003837 if (ret != PTP_RC_OK) {
Linus Walleij43aba612011-11-25 09:40:34 +01003838 add_ptp_error_to_errorstack(device, ret,
3839 "LIBMTP_Get_Batterylevel(): "
3840 "could not get device property value.");
Linus Walleijfa1374c2006-02-27 07:41:46 +00003841 return -1;
3842 }
mopoke96143402006-10-30 04:37:26 +00003843
Linus Walleijfa1374c2006-02-27 07:41:46 +00003844 *maximum_level = device->maximum_battery_level;
Linus Walleijb02a0662006-04-25 08:05:09 +00003845 *current_level = propval.u8;
mopoke96143402006-10-30 04:37:26 +00003846
Linus Walleijfa1374c2006-02-27 07:41:46 +00003847 return 0;
3848}
3849
Linus Walleij13374a42006-09-13 11:55:30 +00003850
3851/**
3852 * Formats device storage (if the device supports the operation).
3853 * WARNING: This WILL delete all data from the device. Make sure you've
3854 * got confirmation from the user BEFORE you call this function.
3855 *
Linus Walleijf8491912006-12-15 10:23:30 +00003856 * @param device a pointer to the device containing the storage to format.
3857 * @param storage the actual storage to format.
Linus Walleij13374a42006-09-13 11:55:30 +00003858 * @return 0 on success, any other value means failure.
3859 */
Linus Walleij43aba612011-11-25 09:40:34 +01003860int LIBMTP_Format_Storage(LIBMTP_mtpdevice_t *device,
3861 LIBMTP_devicestorage_t *storage)
Linus Walleij13374a42006-09-13 11:55:30 +00003862{
3863 uint16_t ret;
3864 PTPParams *params = (PTPParams *) device->params;
mopoke96143402006-10-30 04:37:26 +00003865
Linus Walleij13374a42006-09-13 11:55:30 +00003866 if (!ptp_operation_issupported(params,PTP_OC_FormatStore)) {
Linus Walleij2c1bbd62009-11-07 15:15:03 +00003867 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL,
Linus Walleij43aba612011-11-25 09:40:34 +01003868 "LIBMTP_Format_Storage(): "
3869 "device does not support formatting storage.");
Linus Walleij13374a42006-09-13 11:55:30 +00003870 return -1;
3871 }
Linus Walleijf8491912006-12-15 10:23:30 +00003872 ret = ptp_formatstore(params, storage->id);
Linus Walleij13374a42006-09-13 11:55:30 +00003873 if (ret != PTP_RC_OK) {
Linus Walleij43aba612011-11-25 09:40:34 +01003874 add_ptp_error_to_errorstack(device, ret, "LIBMTP_Format_Storage(): "
3875 "failed to format storage.");
Linus Walleij13374a42006-09-13 11:55:30 +00003876 return -1;
3877 }
3878 return 0;
3879}
3880
Linus Walleijfa1374c2006-02-27 07:41:46 +00003881/**
Linus Walleij545c7792006-06-13 15:22:30 +00003882 * Helper function to extract a unicode property off a device.
Linus Walleije46f12e2006-06-22 17:53:25 +00003883 * This is the standard way of retrieveing unicode device
3884 * properties as described by the PTP spec.
Linus Walleijcf223e62006-06-19 09:31:53 +00003885 * @param device a pointer to the device to get the property from.
mopoke96143402006-10-30 04:37:26 +00003886 * @param unicstring a pointer to a pointer that will hold the
Linus Walleijcf223e62006-06-19 09:31:53 +00003887 * property after this call is completed.
3888 * @param property the property to retrieve.
3889 * @return 0 on success, any other value means failure.
Linus Walleij545c7792006-06-13 15:22:30 +00003890 */
mopoke96143402006-10-30 04:37:26 +00003891static int get_device_unicode_property(LIBMTP_mtpdevice_t *device,
Linus Walleijcf223e62006-06-19 09:31:53 +00003892 char **unicstring, uint16_t property)
Linus Walleij545c7792006-06-13 15:22:30 +00003893{
3894 PTPPropertyValue propval;
3895 PTPParams *params = (PTPParams *) device->params;
Linus Walleij16571dc2006-08-17 20:27:46 +00003896 uint16_t *tmp;
Linus Walleij070e9b42007-01-22 08:49:28 +00003897 uint16_t ret;
Linus Walleij545c7792006-06-13 15:22:30 +00003898 int i;
3899
3900 if (!ptp_property_issupported(params, property)) {
3901 return -1;
3902 }
3903
Linus Walleijcf223e62006-06-19 09:31:53 +00003904 // Unicode strings are 16bit unsigned integer arrays.
Linus Walleij070e9b42007-01-22 08:49:28 +00003905 ret = ptp_getdevicepropvalue(params,
3906 property,
3907 &propval,
3908 PTP_DTC_AUINT16);
3909 if (ret != PTP_RC_OK) {
3910 // TODO: add a note on WHICH property that we failed to get.
tedbullock4e51cb92007-02-15 11:48:34 +00003911 *unicstring = NULL;
Linus Walleij43aba612011-11-25 09:40:34 +01003912 add_ptp_error_to_errorstack(device, ret,
3913 "get_device_unicode_property(): "
3914 "failed to get unicode property.");
Linus Walleij545c7792006-06-13 15:22:30 +00003915 return -1;
3916 }
3917
3918 // Extract the actual array.
Linus Walleij16571dc2006-08-17 20:27:46 +00003919 // printf("Array of %d elements\n", propval.a.count);
3920 tmp = malloc((propval.a.count + 1)*sizeof(uint16_t));
Linus Walleij545c7792006-06-13 15:22:30 +00003921 for (i = 0; i < propval.a.count; i++) {
Linus Walleij16571dc2006-08-17 20:27:46 +00003922 tmp[i] = propval.a.v[i].u16;
3923 // printf("%04x ", tmp[i]);
Linus Walleij545c7792006-06-13 15:22:30 +00003924 }
Linus Walleij16571dc2006-08-17 20:27:46 +00003925 tmp[propval.a.count] = 0x0000U;
Linus Walleij545c7792006-06-13 15:22:30 +00003926 free(propval.a.v);
3927
Linus Walleij3ec86312006-08-21 13:25:24 +00003928 *unicstring = utf16_to_utf8(device, tmp);
Linus Walleij16571dc2006-08-17 20:27:46 +00003929
Linus Walleij545c7792006-06-13 15:22:30 +00003930 free(tmp);
3931
3932 return 0;
3933}
3934
3935/**
3936 * This function returns the secure time as an XML document string from
3937 * the device.
3938 * @param device a pointer to the device to get the secure time for.
3939 * @param sectime the secure time string as an XML document or NULL if the call
3940 * failed or the secure time property is not supported. This string
3941 * must be <code>free()</code>:ed by the caller after use.
3942 * @return 0 on success, any other value means failure.
3943 */
Linus Walleij8ab54262006-06-21 07:12:28 +00003944int LIBMTP_Get_Secure_Time(LIBMTP_mtpdevice_t *device, char ** const sectime)
Linus Walleij545c7792006-06-13 15:22:30 +00003945{
3946 return get_device_unicode_property(device, sectime, PTP_DPC_MTP_SecureTime);
3947}
3948
3949/**
mopoke96143402006-10-30 04:37:26 +00003950 * This function returns the device (public key) certificate as an
Linus Walleij545c7792006-06-13 15:22:30 +00003951 * XML document string from the device.
3952 * @param device a pointer to the device to get the device certificate for.
3953 * @param devcert the device certificate as an XML string or NULL if the call
3954 * failed or the device certificate property is not supported. This
3955 * string must be <code>free()</code>:ed by the caller after use.
3956 * @return 0 on success, any other value means failure.
3957 */
Linus Walleij8ab54262006-06-21 07:12:28 +00003958int LIBMTP_Get_Device_Certificate(LIBMTP_mtpdevice_t *device, char ** const devcert)
Linus Walleij545c7792006-06-13 15:22:30 +00003959{
Linus Walleij43aba612011-11-25 09:40:34 +01003960 return get_device_unicode_property(device, devcert,
3961 PTP_DPC_MTP_DeviceCertificate);
Linus Walleij545c7792006-06-13 15:22:30 +00003962}
3963
3964/**
Linus Walleij8ab54262006-06-21 07:12:28 +00003965 * This function retrieves a list of supported file types, i.e. the file
3966 * types that this device claims it supports, e.g. audio file types that
3967 * the device can play etc. This list is mitigated to
3968 * inlcude the file types that libmtp can handle, i.e. it will not list
3969 * filetypes that libmtp will handle internally like playlists and folders.
3970 * @param device a pointer to the device to get the filetype capabilities for.
3971 * @param filetypes a pointer to a pointer that will hold the list of
3972 * supported filetypes if the call was successful. This list must
3973 * be <code>free()</code>:ed by the caller after use.
3974 * @param length a pointer to a variable that will hold the length of the
3975 * list of supported filetypes if the call was successful.
3976 * @return 0 on success, any other value means failure.
3977 * @see LIBMTP_Get_Filetype_Description()
3978 */
mopoke96143402006-10-30 04:37:26 +00003979int LIBMTP_Get_Supported_Filetypes(LIBMTP_mtpdevice_t *device, uint16_t ** const filetypes,
Linus Walleij8ab54262006-06-21 07:12:28 +00003980 uint16_t * const length)
3981{
3982 PTPParams *params = (PTPParams *) device->params;
Linus Walleija3544f62007-11-30 01:20:04 +00003983 PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo;
Linus Walleij8ab54262006-06-21 07:12:28 +00003984 uint16_t *localtypes;
3985 uint16_t localtypelen;
3986 uint32_t i;
mopoke96143402006-10-30 04:37:26 +00003987
Linus Walleij8ab54262006-06-21 07:12:28 +00003988 // This is more memory than needed if there are unknown types, but what the heck.
3989 localtypes = (uint16_t *) malloc(params->deviceinfo.ImageFormats_len * sizeof(uint16_t));
3990 localtypelen = 0;
mopoke96143402006-10-30 04:37:26 +00003991
Linus Walleij8ab54262006-06-21 07:12:28 +00003992 for (i=0;i<params->deviceinfo.ImageFormats_len;i++) {
3993 uint16_t localtype = map_ptp_type_to_libmtp_type(params->deviceinfo.ImageFormats[i]);
3994 if (localtype != LIBMTP_FILETYPE_UNKNOWN) {
3995 localtypes[localtypelen] = localtype;
3996 localtypelen++;
3997 }
3998 }
Linus Walleija3544f62007-11-30 01:20:04 +00003999 // The forgotten Ogg support on YP-10 and others...
Linus Walleijfec4d562008-06-01 22:30:36 +00004000 if (FLAG_OGG_IS_UNKNOWN(ptp_usb)) {
Linus Walleij43aba612011-11-25 09:40:34 +01004001 localtypes = (uint16_t *) realloc(localtypes,
4002 (params->deviceinfo.ImageFormats_len+1) * sizeof(uint16_t));
Linus Walleija3544f62007-11-30 01:20:04 +00004003 localtypes[localtypelen] = LIBMTP_FILETYPE_OGG;
4004 localtypelen++;
4005 }
Linus Walleij89bb1cd2009-07-24 21:03:36 +00004006 // The forgotten FLAC support on Cowon iAudio S9 and others...
4007 if (FLAG_FLAC_IS_UNKNOWN(ptp_usb)) {
Linus Walleij43aba612011-11-25 09:40:34 +01004008 localtypes = (uint16_t *) realloc(localtypes,
4009 (params->deviceinfo.ImageFormats_len+1) * sizeof(uint16_t));
Linus Walleij89bb1cd2009-07-24 21:03:36 +00004010 localtypes[localtypelen] = LIBMTP_FILETYPE_FLAC;
4011 localtypelen++;
4012 }
Linus Walleij8ab54262006-06-21 07:12:28 +00004013
4014 *filetypes = localtypes;
4015 *length = localtypelen;
4016
4017 return 0;
4018}
4019
raveloxd9a28642006-05-26 23:42:22 +00004020/**
Linus Walleija3895612013-04-29 21:56:38 +02004021 * This function checks if the device has some specific capabilities, in
4022 * order to avoid calling APIs that may disturb the device.
4023 *
4024 * @param device a pointer to the device to check the capability on.
4025 * @param cap the capability to check.
4026 * @return 0 if not supported, any other value means the device has the
4027 * requested capability.
4028 */
4029int LIBMTP_Check_Capability(LIBMTP_mtpdevice_t *device, LIBMTP_devicecap_t cap)
4030{
4031 switch (cap) {
4032 case LIBMTP_DEVICECAP_GetPartialObject:
4033 return (ptp_operation_issupported(device->params,
4034 PTP_OC_GetPartialObject) ||
4035 ptp_operation_issupported(device->params,
4036 PTP_OC_ANDROID_GetPartialObject64));
4037 case LIBMTP_DEVICECAP_SendPartialObject:
4038 return ptp_operation_issupported(device->params,
4039 PTP_OC_ANDROID_SendPartialObject);
4040 case LIBMTP_DEVICECAP_EditObjects:
4041 return (ptp_operation_issupported(device->params,
4042 PTP_OC_ANDROID_TruncateObject) &&
4043 ptp_operation_issupported(device->params,
4044 PTP_OC_ANDROID_BeginEditObject) &&
4045 ptp_operation_issupported(device->params,
4046 PTP_OC_ANDROID_EndEditObject));
Philip Langdale1680c392018-03-17 09:23:25 +01004047 case LIBMTP_DEVICECAP_MoveObject:
4048 return ptp_operation_issupported(device->params,
4049 PTP_OC_MoveObject);
4050 case LIBMTP_DEVICECAP_CopyObject:
4051 return ptp_operation_issupported(device->params,
4052 PTP_OC_CopyObject);
Linus Walleija3895612013-04-29 21:56:38 +02004053 /*
4054 * Handle other capabilities here, this is also a good place to
4055 * blacklist some advanced operations on specific devices if need
4056 * be.
4057 */
4058
4059 default:
4060 break;
4061 }
4062 return 0;
4063}
4064
4065/**
Linus Walleij5b452bd2007-12-28 23:34:18 +00004066 * This function updates all the storage id's of a device and their
Linus Walleij2c1bbd62009-11-07 15:15:03 +00004067 * properties, then creates a linked list and puts the list head into
Linus Walleijf8491912006-12-15 10:23:30 +00004068 * the device struct. It also optionally sorts this list. If you want
4069 * to display storage information in your application you should call
Linus Walleij2c1bbd62009-11-07 15:15:03 +00004070 * this function, then dereference the device struct
Linus Walleij5b452bd2007-12-28 23:34:18 +00004071 * (<code>device-&gt;storage</code>) to get out information on the storage.
4072 *
4073 * You need to call this everytime you want to update the
4074 * <code>device-&gt;storage</code> list, for example anytime you need
4075 * to check available storage somewhere.
4076 *
4077 * <b>WARNING:</b> since this list is dynamically updated, do not
4078 * reference its fields in external applications by pointer! E.g
4079 * do not put a reference to any <code>char *</code> field. instead
4080 * <code>strncpy()</code> it!
Linus Walleijf8491912006-12-15 10:23:30 +00004081 *
Linus Walleije1b88e82008-07-02 20:12:53 +00004082 * @param device a pointer to the device to get the storage for.
Linus Walleij2c1bbd62009-11-07 15:15:03 +00004083 * @param sortby an integer that determines the sorting of the storage list.
Linus Walleij9e1b0812006-12-12 19:22:02 +00004084 * Valid sort methods are defined in libmtp.h with beginning with
Linus Walleij2c1bbd62009-11-07 15:15:03 +00004085 * LIBMTP_STORAGE_SORTBY_. 0 or LIBMTP_STORAGE_SORTBY_NOTSORTED to not
Linus Walleij9e1b0812006-12-12 19:22:02 +00004086 * sort.
Linus Walleij2c1bbd62009-11-07 15:15:03 +00004087 * @return 0 on success, 1 success but only with storage id's, storage
Linus Walleij9e1b0812006-12-12 19:22:02 +00004088 * properities could not be retrieved and -1 means failure.
Linus Walleij9e1b0812006-12-12 19:22:02 +00004089 */
4090int LIBMTP_Get_Storage(LIBMTP_mtpdevice_t *device, int const sortby)
4091{
4092 uint32_t i = 0;
4093 PTPStorageInfo storageInfo;
4094 PTPParams *params = (PTPParams *) device->params;
4095 PTPStorageIDs storageIDs;
Linus Walleije1ac07e2006-12-14 19:38:59 +00004096 LIBMTP_devicestorage_t *storage = NULL;
4097 LIBMTP_devicestorage_t *storageprev = NULL;
Linus Walleij9e1b0812006-12-12 19:22:02 +00004098
4099 if (device->storage != NULL)
4100 free_storage_list(device);
4101
Linus Walleij2c1bbd62009-11-07 15:15:03 +00004102 // if (!ptp_operation_issupported(params,PTP_OC_GetStorageIDs))
Linus Walleij9e1b0812006-12-12 19:22:02 +00004103 // return -1;
Linus Walleij2c1bbd62009-11-07 15:15:03 +00004104 if (ptp_getstorageids (params, &storageIDs) != PTP_RC_OK)
Linus Walleij9e1b0812006-12-12 19:22:02 +00004105 return -1;
Linus Walleij2c1bbd62009-11-07 15:15:03 +00004106 if (storageIDs.n < 1)
Linus Walleij9e1b0812006-12-12 19:22:02 +00004107 return -1;
4108
4109 if (!ptp_operation_issupported(params,PTP_OC_GetStorageInfo)) {
4110 for (i = 0; i < storageIDs.n; i++) {
4111
Linus Walleij43aba612011-11-25 09:40:34 +01004112 storage = (LIBMTP_devicestorage_t *)
4113 malloc(sizeof(LIBMTP_devicestorage_t));
Linus Walleije1ac07e2006-12-14 19:38:59 +00004114 storage->prev = storageprev;
4115 if (storageprev != NULL)
4116 storageprev->next = storage;
Linus Walleij2c1bbd62009-11-07 15:15:03 +00004117 if (device->storage == NULL)
Linus Walleije1ac07e2006-12-14 19:38:59 +00004118 device->storage = storage;
Linus Walleij9e1b0812006-12-12 19:22:02 +00004119
Linus Walleije1ac07e2006-12-14 19:38:59 +00004120 storage->id = storageIDs.Storage[i];
4121 storage->StorageType = PTP_ST_Undefined;
4122 storage->FilesystemType = PTP_FST_Undefined;
4123 storage->AccessCapability = PTP_AC_ReadWrite;
4124 storage->MaxCapacity = (uint64_t) -1;
4125 storage->FreeSpaceInBytes = (uint64_t) -1;
4126 storage->FreeSpaceInObjects = (uint64_t) -1;
4127 storage->StorageDescription = strdup("Unknown storage");
4128 storage->VolumeIdentifier = strdup("Unknown volume");
4129 storage->next = NULL;
Linus Walleij9e1b0812006-12-12 19:22:02 +00004130
Linus Walleije1ac07e2006-12-14 19:38:59 +00004131 storageprev = storage;
Linus Walleij9e1b0812006-12-12 19:22:02 +00004132 }
4133 free(storageIDs.Storage);
4134 return 1;
4135 } else {
4136 for (i = 0; i < storageIDs.n; i++) {
Linus Walleij070e9b42007-01-22 08:49:28 +00004137 uint16_t ret;
4138 ret = ptp_getstorageinfo(params, storageIDs.Storage[i], &storageInfo);
4139 if (ret != PTP_RC_OK) {
Linus Walleij43aba612011-11-25 09:40:34 +01004140 add_ptp_error_to_errorstack(device, ret, "LIBMTP_Get_Storage(): "
4141 "Could not get storage info.");
Linus Walleij9e1b0812006-12-12 19:22:02 +00004142 if (device->storage != NULL) {
4143 free_storage_list(device);
4144 }
4145 return -1;
4146 }
4147
Linus Walleij43aba612011-11-25 09:40:34 +01004148 storage = (LIBMTP_devicestorage_t *)
4149 malloc(sizeof(LIBMTP_devicestorage_t));
Linus Walleije1ac07e2006-12-14 19:38:59 +00004150 storage->prev = storageprev;
4151 if (storageprev != NULL)
4152 storageprev->next = storage;
Linus Walleij9e1b0812006-12-12 19:22:02 +00004153 if (device->storage == NULL)
Linus Walleije1ac07e2006-12-14 19:38:59 +00004154 device->storage = storage;
Linus Walleij9e1b0812006-12-12 19:22:02 +00004155
Linus Walleije1ac07e2006-12-14 19:38:59 +00004156 storage->id = storageIDs.Storage[i];
4157 storage->StorageType = storageInfo.StorageType;
4158 storage->FilesystemType = storageInfo.FilesystemType;
4159 storage->AccessCapability = storageInfo.AccessCapability;
4160 storage->MaxCapacity = storageInfo.MaxCapability;
4161 storage->FreeSpaceInBytes = storageInfo.FreeSpaceInBytes;
4162 storage->FreeSpaceInObjects = storageInfo.FreeSpaceInImages;
4163 storage->StorageDescription = storageInfo.StorageDescription;
4164 storage->VolumeIdentifier = storageInfo.VolumeLabel;
4165 storage->next = NULL;
Linus Walleij9e1b0812006-12-12 19:22:02 +00004166
Linus Walleije1ac07e2006-12-14 19:38:59 +00004167 storageprev = storage;
Linus Walleij9e1b0812006-12-12 19:22:02 +00004168 }
4169
Linus Walleij5c1499e2009-02-21 06:54:29 +00004170 if (storage != NULL)
4171 storage->next = NULL;
Linus Walleij9e1b0812006-12-12 19:22:02 +00004172
4173 sort_storage_by(device,sortby);
4174 free(storageIDs.Storage);
4175 return 0;
4176 }
4177}
4178
4179/**
Linus Walleijf6bc1782006-03-24 15:12:47 +00004180 * This creates a new file metadata structure and allocates memory
4181 * for it. Notice that if you add strings to this structure they
4182 * will be freed by the corresponding <code>LIBMTP_destroy_file_t</code>
mopoke96143402006-10-30 04:37:26 +00004183 * operation later, so be careful of using strdup() when assigning
Linus Walleijf6bc1782006-03-24 15:12:47 +00004184 * strings, e.g.:
4185 *
4186 * <pre>
4187 * LIBMTP_file_t *file = LIBMTP_new_file_t();
4188 * file->filename = strdup(namestr);
4189 * ....
4190 * LIBMTP_destroy_file_t(file);
4191 * </pre>
4192 *
4193 * @return a pointer to the newly allocated metadata structure.
4194 * @see LIBMTP_destroy_file_t()
4195 */
4196LIBMTP_file_t *LIBMTP_new_file_t(void)
4197{
4198 LIBMTP_file_t *new = (LIBMTP_file_t *) malloc(sizeof(LIBMTP_file_t));
4199 if (new == NULL) {
4200 return NULL;
4201 }
4202 new->filename = NULL;
Linus Walleijea68f1f2008-06-22 21:54:44 +00004203 new->item_id = 0;
4204 new->parent_id = 0;
4205 new->storage_id = 0;
Linus Walleijf6bc1782006-03-24 15:12:47 +00004206 new->filesize = 0;
Richard Lowd3b17022009-04-11 12:37:39 +00004207 new->modificationdate = 0;
Linus Walleijf6bc1782006-03-24 15:12:47 +00004208 new->filetype = LIBMTP_FILETYPE_UNKNOWN;
4209 new->next = NULL;
4210 return new;
4211}
4212
4213/**
4214 * This destroys a file metadata structure and deallocates the memory
mopoke96143402006-10-30 04:37:26 +00004215 * used by it, including any strings. Never use a file metadata
Linus Walleijf6bc1782006-03-24 15:12:47 +00004216 * structure again after calling this function on it.
4217 * @param file the file metadata to destroy.
4218 * @see LIBMTP_new_file_t()
4219 */
4220void LIBMTP_destroy_file_t(LIBMTP_file_t *file)
4221{
4222 if (file == NULL) {
4223 return;
4224 }
4225 if (file->filename != NULL)
4226 free(file->filename);
4227 free(file);
4228 return;
4229}
4230
4231/**
Linus Walleija8b88892011-03-03 19:58:26 +01004232 * Helper function that takes one PTP object and creates a
4233 * LIBMTP_file_t metadata entry.
4234 */
4235static LIBMTP_file_t *obj2file(LIBMTP_mtpdevice_t *device, PTPObject *ob)
4236{
4237 PTPParams *params = (PTPParams *) device->params;
4238 PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo;
4239 LIBMTP_file_t *file;
4240 int i;
4241
4242 // Allocate a new file type
4243 file = LIBMTP_new_file_t();
4244
4245 file->parent_id = ob->oi.ParentObject;
4246 file->storage_id = ob->oi.StorageID;
4247
Florent Viarda1af1892017-11-06 18:27:10 +01004248 if (ob->oi.Filename != NULL) {
4249 file->filename = strdup(ob->oi.Filename);
4250 }
4251
Linus Walleija8b88892011-03-03 19:58:26 +01004252 // Set the filetype
4253 file->filetype = map_ptp_type_to_libmtp_type(ob->oi.ObjectFormat);
4254
4255 /*
4256 * A special quirk for devices that doesn't quite
4257 * remember that some files marked as "unknown" type are
4258 * actually OGG or FLAC files. We look at the filename extension
4259 * and see if it happens that this was atleast named "ogg" or "flac"
4260 * and fall back on this heuristic approach in that case,
4261 * for these bugged devices only.
4262 */
4263 if (file->filetype == LIBMTP_FILETYPE_UNKNOWN) {
4264 if ((FLAG_IRIVER_OGG_ALZHEIMER(ptp_usb) ||
Florent Viarda1af1892017-11-06 18:27:10 +01004265 FLAG_OGG_IS_UNKNOWN(ptp_usb)) &&
Linus Walleija8b88892011-03-03 19:58:26 +01004266 has_ogg_extension(file->filename)) {
4267 file->filetype = LIBMTP_FILETYPE_OGG;
4268 }
4269
4270 if (FLAG_FLAC_IS_UNKNOWN(ptp_usb) && has_flac_extension(file->filename)) {
4271 file->filetype = LIBMTP_FILETYPE_FLAC;
4272 }
4273 }
4274
4275 // Set the modification date
4276 file->modificationdate = ob->oi.ModificationDate;
4277
4278 // We only have 32-bit file size here; later we use the PTP_OPC_ObjectSize property
4279 file->filesize = ob->oi.ObjectCompressedSize;
Linus Walleija8b88892011-03-03 19:58:26 +01004280
4281 // This is a unique ID so we can keep track of the file.
4282 file->item_id = ob->oid;
4283
4284 /*
4285 * If we have a cached, large set of metadata, then use it!
4286 */
4287 if (ob->mtpprops) {
4288 MTPProperties *prop = ob->mtpprops;
4289
4290 for (i=0; i < ob->nrofmtpprops; i++, prop++) {
4291 // Pick ObjectSize here...
4292 if (prop->property == PTP_OPC_ObjectSize) {
4293 // This may already be set, but this 64bit precision value
4294 // is better than the PTP 32bit value, so let it override.
4295 if (device->object_bitsize == 64) {
4296 file->filesize = prop->propval.u64;
4297 } else {
4298 file->filesize = prop->propval.u32;
4299 }
4300 break;
4301 }
4302 }
Lei Zhangcaa1bbc2013-02-13 15:12:02 -08004303 } else if (ptp_operation_issupported(params,PTP_OC_MTP_GetObjectPropsSupported)) {
Linus Walleija8b88892011-03-03 19:58:26 +01004304 uint16_t *props = NULL;
4305 uint32_t propcnt = 0;
4306 int ret;
4307
4308 // First see which properties can be retrieved for this object format
4309 ret = ptp_mtp_getobjectpropssupported(params, map_libmtp_type_to_ptp_type(file->filetype), &propcnt, &props);
4310 if (ret != PTP_RC_OK) {
Linus Walleijf46d7d92011-03-04 20:17:01 +01004311 add_ptp_error_to_errorstack(device, ret, "obj2file: call to ptp_mtp_getobjectpropssupported() failed.");
Linus Walleija8b88892011-03-03 19:58:26 +01004312 // Silently fall through.
4313 } else {
4314 for (i = 0; i < propcnt; i++) {
4315 switch (props[i]) {
4316 case PTP_OPC_ObjectSize:
4317 if (device->object_bitsize == 64) {
4318 file->filesize = get_u64_from_object(device, file->item_id, PTP_OPC_ObjectSize, 0);
4319 } else {
4320 file->filesize = get_u32_from_object(device, file->item_id, PTP_OPC_ObjectSize, 0);
4321 }
4322 break;
4323 default:
4324 break;
4325 }
4326 }
4327 free(props);
4328 }
4329 }
4330
4331 return file;
4332}
4333
4334
4335/**
4336 * This function retrieves the metadata for a single file off
4337 * the device.
4338 *
4339 * Do not call this function repeatedly! The file handles are linearly
4340 * searched O(n) and the call may involve (slow) USB traffic, so use
4341 * <code>LIBMTP_Get_Filelisting()</code> and cache the file, preferably
4342 * as an efficient data structure such as a hash list.
4343 *
4344 * Incidentally this function will return metadata for
4345 * a folder (association) as well, but this is not a proper use
4346 * of it, it is intended for file manipulation, not folder manipulation.
4347 *
4348 * @param device a pointer to the device to get the file metadata from.
4349 * @param fileid the object ID of the file that you want the metadata for.
4350 * @return a metadata entry on success or NULL on failure.
4351 * @see LIBMTP_Get_Filelisting()
4352 */
4353LIBMTP_file_t *LIBMTP_Get_Filemetadata(LIBMTP_mtpdevice_t *device, uint32_t const fileid)
4354{
4355 PTPParams *params = (PTPParams *) device->params;
4356 uint16_t ret;
4357 PTPObject *ob;
4358
4359 // Get all the handles if we haven't already done that
4360 // (Only on cached devices.)
4361 if (device->cached && params->nrofobjects == 0) {
4362 flush_handles(device);
4363 }
4364
4365 ret = ptp_object_want(params, fileid, PTPOBJECT_OBJECTINFO_LOADED|PTPOBJECT_MTPPROPLIST_LOADED, &ob);
4366 if (ret != PTP_RC_OK)
4367 return NULL;
4368
4369 return obj2file(device, ob);
4370}
4371
4372/**
mopoke31364442006-11-20 04:53:04 +00004373* THIS FUNCTION IS DEPRECATED. PLEASE UPDATE YOUR CODE IN ORDER
Richard Lowdc0b6c72006-11-13 09:22:23 +00004374 * NOT TO USE IT.
4375 * @see LIBMTP_Get_Filelisting_With_Callback()
4376 */
4377LIBMTP_file_t *LIBMTP_Get_Filelisting(LIBMTP_mtpdevice_t *device)
4378{
nicklas79daadbf22009-09-28 18:19:34 +00004379 LIBMTP_INFO("WARNING: LIBMTP_Get_Filelisting() is deprecated.\n");
4380 LIBMTP_INFO("WARNING: please update your code to use LIBMTP_Get_Filelisting_With_Callback()\n");
Richard Lowdc0b6c72006-11-13 09:22:23 +00004381 return LIBMTP_Get_Filelisting_With_Callback(device, NULL, NULL);
4382}
4383
4384/**
Linus Walleijf6bc1782006-03-24 15:12:47 +00004385 * This returns a long list of all files available
Linus Walleij25d5c212008-08-14 06:49:13 +00004386 * on the current MTP device. Folders will not be returned, but abstract
4387 * entities like playlists and albums will show up as "files". Typical usage:
Linus Walleijf6bc1782006-03-24 15:12:47 +00004388 *
4389 * <pre>
4390 * LIBMTP_file_t *filelist;
4391 *
Richard Lowdc0b6c72006-11-13 09:22:23 +00004392 * filelist = LIBMTP_Get_Filelisting_With_Callback(device, callback, data);
Linus Walleijf6bc1782006-03-24 15:12:47 +00004393 * while (filelist != NULL) {
4394 * LIBMTP_file_t *tmp;
4395 *
4396 * // Do something on each element in the list here...
4397 * tmp = filelist;
4398 * filelist = filelist->next;
4399 * LIBMTP_destroy_file_t(tmp);
4400 * }
4401 * </pre>
4402 *
Linus Walleij25d5c212008-08-14 06:49:13 +00004403 * If you want to group your file listing by storage (per storage unit) or
4404 * arrange files into folders, you must dereference the <code>storage_id</code>
4405 * and/or <code>parent_id</code> field of the returned <code>LIBMTP_file_t</code>
4406 * struct. To arrange by folders or files you typically have to create the proper
Linus Walleij2c1bbd62009-11-07 15:15:03 +00004407 * trees by calls to <code>LIBMTP_Get_Storage()</code> and/or
Linus Walleij25d5c212008-08-14 06:49:13 +00004408 * <code>LIBMTP_Get_Folder_List()</code> first.
4409 *
Linus Walleijf6bc1782006-03-24 15:12:47 +00004410 * @param device a pointer to the device to get the file listing for.
Richard Lowdc0b6c72006-11-13 09:22:23 +00004411 * @param callback a function to be called during the tracklisting retrieveal
Linus Walleij25d5c212008-08-14 06:49:13 +00004412 * for displaying progress bars etc, or NULL if you don't want
4413 * any callbacks.
Richard Lowdc0b6c72006-11-13 09:22:23 +00004414 * @param data a user-defined pointer that is passed along to
Linus Walleij25d5c212008-08-14 06:49:13 +00004415 * the <code>progress</code> function in order to
4416 * pass along some user defined data to the progress
4417 * updates. If not used, set this to NULL.
Linus Walleijf6bc1782006-03-24 15:12:47 +00004418 * @return a list of files that can be followed using the <code>next</code>
Linus Walleij25d5c212008-08-14 06:49:13 +00004419 * field of the <code>LIBMTP_file_t</code> data structure.
4420 * Each of the metadata tags must be freed after use, and may
4421 * contain only partial metadata information, i.e. one or several
4422 * fields may be NULL or 0.
Linus Walleij2e4b5f92006-06-16 14:00:49 +00004423 * @see LIBMTP_Get_Filemetadata()
Linus Walleijf6bc1782006-03-24 15:12:47 +00004424 */
Richard Lowdc0b6c72006-11-13 09:22:23 +00004425LIBMTP_file_t *LIBMTP_Get_Filelisting_With_Callback(LIBMTP_mtpdevice_t *device,
4426 LIBMTP_progressfunc_t const callback,
4427 void const * const data)
Linus Walleijf6bc1782006-03-24 15:12:47 +00004428{
4429 uint32_t i = 0;
4430 LIBMTP_file_t *retfiles = NULL;
4431 LIBMTP_file_t *curfile = NULL;
4432 PTPParams *params = (PTPParams *) device->params;
Linus Walleij438bd7f2006-06-08 11:35:44 +00004433
mopoke96143402006-10-30 04:37:26 +00004434 // Get all the handles if we haven't already done that
Linus Walleijd4637502009-06-14 23:03:33 +00004435 if (params->nrofobjects == 0) {
Linus Walleij438bd7f2006-06-08 11:35:44 +00004436 flush_handles(device);
Linus Walleijf6bc1782006-03-24 15:12:47 +00004437 }
mopoke96143402006-10-30 04:37:26 +00004438
Linus Walleijd4637502009-06-14 23:03:33 +00004439 for (i = 0; i < params->nrofobjects; i++) {
Linus Walleij070e9b42007-01-22 08:49:28 +00004440 LIBMTP_file_t *file;
Linus Walleija8b88892011-03-03 19:58:26 +01004441 PTPObject *ob;
Linus Walleijf6bc1782006-03-24 15:12:47 +00004442
Richard Lowdc0b6c72006-11-13 09:22:23 +00004443 if (callback != NULL)
Linus Walleijd4637502009-06-14 23:03:33 +00004444 callback(i, params->nrofobjects, data);
mopoke31364442006-11-20 04:53:04 +00004445
Linus Walleijd4637502009-06-14 23:03:33 +00004446 ob = &params->objects[i];
Linus Walleijf6bc1782006-03-24 15:12:47 +00004447
Linus Walleijd4637502009-06-14 23:03:33 +00004448 if (ob->oi.ObjectFormat == PTP_OFC_Association) {
Linus Walleijd9d28d52007-08-04 19:01:18 +00004449 // MTP use this object format for folders which means
Linus Walleijf0bf4372007-07-01 21:47:38 +00004450 // these "files" will turn up on a folder listing instead.
4451 continue;
Linus Walleijf6bc1782006-03-24 15:12:47 +00004452 }
4453
Linus Walleija8b88892011-03-03 19:58:26 +01004454 // Look up metadata
4455 file = obj2file(device, ob);
4456 if (file == NULL) {
4457 continue;
Linus Walleijd9d28d52007-08-04 19:01:18 +00004458 }
Linus Walleij2c1bbd62009-11-07 15:15:03 +00004459
Linus Walleijf0bf4372007-07-01 21:47:38 +00004460 // Add track to a list that will be returned afterwards.
4461 if (retfiles == NULL) {
4462 retfiles = file;
4463 curfile = file;
4464 } else {
4465 curfile->next = file;
4466 curfile = file;
4467 }
4468
4469 // Call listing callback
4470 // double progressPercent = (double)i*(double)100.0 / (double)params->handles.n;
Linus Walleij2c1bbd62009-11-07 15:15:03 +00004471
Linus Walleijf6bc1782006-03-24 15:12:47 +00004472 } // Handle counting loop
4473 return retfiles;
4474}
4475
4476/**
Linus Walleij71c79292011-03-05 22:08:07 +01004477 * This function retrieves the contents of a certain folder
4478 * with id parent on a certain storage on a certain device.
4479 * The result contains both files and folders.
4480 * The device used with this operations must have been opened with
4481 * LIBMTP_Open_Raw_Device_Uncached() or it will fail.
4482 *
4483 * NOTE: the request will always perform I/O with the device.
4484 * @param device a pointer to the MTP device to report info from.
4485 * @param storage a storage on the device to report info from. If
Linus Walleij1e4dfea2011-03-05 22:50:39 +01004486 * 0 is passed in, the files for the given parent will be
Linus Walleij71c79292011-03-05 22:08:07 +01004487 * searched across all available storages.
4488 * @param parent the parent folder id.
Linus Walleij2e4b5f92006-06-16 14:00:49 +00004489 */
Linus Walleij71c79292011-03-05 22:08:07 +01004490LIBMTP_file_t * LIBMTP_Get_Files_And_Folders(LIBMTP_mtpdevice_t *device,
Linus Walleij8a199282011-03-05 22:28:41 +01004491 uint32_t const storage,
Linus Walleij71c79292011-03-05 22:08:07 +01004492 uint32_t const parent)
Linus Walleij2e4b5f92006-06-16 14:00:49 +00004493{
Linus Walleij71c79292011-03-05 22:08:07 +01004494 PTPParams *params = (PTPParams *) device->params;
Linus Walleij6ccbe962011-11-25 09:40:00 +01004495 PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo;
Linus Walleij71c79292011-03-05 22:08:07 +01004496 LIBMTP_file_t *retfiles = NULL;
4497 LIBMTP_file_t *curfile = NULL;
Linus Walleija8b88892011-03-03 19:58:26 +01004498 PTPObjectHandles currentHandles;
Linus Walleij71c79292011-03-05 22:08:07 +01004499 uint32_t storageid;
4500 uint16_t ret;
4501 int i = 0;
Linus Walleij2e4b5f92006-06-16 14:00:49 +00004502
Linus Walleij71c79292011-03-05 22:08:07 +01004503 if (device->cached) {
4504 // This function is only supposed to be used by devices
4505 // opened as uncached!
4506 LIBMTP_ERROR("tried to use %s on a cached device!\n",
4507 __func__);
4508 return NULL;
Linus Walleij366dd1c2011-03-04 20:27:03 +01004509 }
4510
Linus Walleij6ccbe962011-11-25 09:40:00 +01004511 if (FLAG_BROKEN_GET_OBJECT_PROPVAL(ptp_usb)) {
4512 // These devices cannot handle the commands needed for
4513 // Uncached access!
4514 LIBMTP_ERROR("tried to use %s on an unsupported device, "
4515 "this command does not work on all devices "
4516 "due to missing low-level support to read "
4517 "information on individual tracks\n",
4518 __func__);
4519 return NULL;
4520 }
4521
Linus Walleij1e4dfea2011-03-05 22:50:39 +01004522 if (storage == 0)
Linus Walleij71c79292011-03-05 22:08:07 +01004523 storageid = PTP_GOH_ALL_STORAGE;
4524 else
Linus Walleij8a199282011-03-05 22:28:41 +01004525 storageid = storage;
Linus Walleij71c79292011-03-05 22:08:07 +01004526
4527 ret = ptp_getobjecthandles(params,
4528 storageid,
4529 PTP_GOH_ALL_FORMATS,
4530 parent,
4531 &currentHandles);
Linus Walleija8b88892011-03-03 19:58:26 +01004532
4533 if (ret != PTP_RC_OK) {
Linus Walleij71c79292011-03-05 22:08:07 +01004534 add_ptp_error_to_errorstack(device, ret,
4535 "LIBMTP_Get_Files_And_Folders(): could not get object handles.");
Linus Walleija8b88892011-03-03 19:58:26 +01004536 return NULL;
Linus Walleij2e4b5f92006-06-16 14:00:49 +00004537 }
4538
Linus Walleija8b88892011-03-03 19:58:26 +01004539 if (currentHandles.Handler == NULL || currentHandles.n == 0)
Linus Walleijd4637502009-06-14 23:03:33 +00004540 return NULL;
Linus Walleij2e4b5f92006-06-16 14:00:49 +00004541
Linus Walleija8b88892011-03-03 19:58:26 +01004542 for (i = 0; i < currentHandles.n; i++) {
4543 LIBMTP_file_t *file;
Linus Walleij2c1bbd62009-11-07 15:15:03 +00004544
Linus Walleija8b88892011-03-03 19:58:26 +01004545 // Get metadata for one file, if it fails, try next file
4546 file = LIBMTP_Get_Filemetadata(device, currentHandles.Handler[i]);
4547 if (file == NULL)
4548 continue;
Linus Walleij2e4b5f92006-06-16 14:00:49 +00004549
Linus Walleija8b88892011-03-03 19:58:26 +01004550 // Add track to a list that will be returned afterwards.
Linus Walleij366dd1c2011-03-04 20:27:03 +01004551 if (curfile == NULL) {
Linus Walleija8b88892011-03-03 19:58:26 +01004552 curfile = file;
Linus Walleij71c79292011-03-05 22:08:07 +01004553 retfiles = file;
Linus Walleijd4637502009-06-14 23:03:33 +00004554 } else {
Linus Walleija8b88892011-03-03 19:58:26 +01004555 curfile->next = file;
4556 curfile = file;
Linus Walleijd4637502009-06-14 23:03:33 +00004557 }
4558 }
Linus Walleij2c1bbd62009-11-07 15:15:03 +00004559
Linus Walleija8b88892011-03-03 19:58:26 +01004560 free(currentHandles.Handler);
4561
Linus Walleij366dd1c2011-03-04 20:27:03 +01004562 // Return a pointer to the original first file
4563 // in the big list.
Linus Walleija8b88892011-03-03 19:58:26 +01004564 return retfiles;
Linus Walleij2e4b5f92006-06-16 14:00:49 +00004565}
4566
Linus Walleija8b88892011-03-03 19:58:26 +01004567
4568/**
Linus Walleij394bbbe2006-02-22 16:10:53 +00004569 * This creates a new track metadata structure and allocates memory
4570 * for it. Notice that if you add strings to this structure they
4571 * will be freed by the corresponding <code>LIBMTP_destroy_track_t</code>
mopoke96143402006-10-30 04:37:26 +00004572 * operation later, so be careful of using strdup() when assigning
Linus Walleij394bbbe2006-02-22 16:10:53 +00004573 * strings, e.g.:
4574 *
Linus Walleij17e39f72006-02-23 15:54:28 +00004575 * <pre>
Linus Walleij394bbbe2006-02-22 16:10:53 +00004576 * LIBMTP_track_t *track = LIBMTP_new_track_t();
4577 * track->title = strdup(titlestr);
4578 * ....
4579 * LIBMTP_destroy_track_t(track);
Linus Walleij17e39f72006-02-23 15:54:28 +00004580 * </pre>
Linus Walleij394bbbe2006-02-22 16:10:53 +00004581 *
4582 * @return a pointer to the newly allocated metadata structure.
4583 * @see LIBMTP_destroy_track_t()
4584 */
4585LIBMTP_track_t *LIBMTP_new_track_t(void)
Linus Walleijb9256fd2006-02-15 09:40:43 +00004586{
4587 LIBMTP_track_t *new = (LIBMTP_track_t *) malloc(sizeof(LIBMTP_track_t));
4588 if (new == NULL) {
4589 return NULL;
4590 }
Linus Walleijea68f1f2008-06-22 21:54:44 +00004591 new->item_id = 0;
4592 new->parent_id = 0;
4593 new->storage_id = 0;
Linus Walleijb9256fd2006-02-15 09:40:43 +00004594 new->title = NULL;
4595 new->artist = NULL;
Linus Walleij31b74292008-05-02 23:29:06 +00004596 new->composer = NULL;
Linus Walleijb9256fd2006-02-15 09:40:43 +00004597 new->album = NULL;
4598 new->genre = NULL;
4599 new->date = NULL;
4600 new->filename = NULL;
4601 new->duration = 0;
4602 new->tracknumber = 0;
4603 new->filesize = 0;
Linus Walleijf6bc1782006-03-24 15:12:47 +00004604 new->filetype = LIBMTP_FILETYPE_UNKNOWN;
Linus Walleijcf223e62006-06-19 09:31:53 +00004605 new->samplerate = 0;
4606 new->nochannels = 0;
4607 new->wavecodec = 0;
4608 new->bitrate = 0;
4609 new->bitratetype = 0;
4610 new->rating = 0;
4611 new->usecount = 0;
Richard Lowd3b17022009-04-11 12:37:39 +00004612 new->modificationdate = 0;
Linus Walleijb9256fd2006-02-15 09:40:43 +00004613 new->next = NULL;
4614 return new;
4615}
4616
Linus Walleij394bbbe2006-02-22 16:10:53 +00004617/**
4618 * This destroys a track metadata structure and deallocates the memory
mopoke96143402006-10-30 04:37:26 +00004619 * used by it, including any strings. Never use a track metadata
Linus Walleij394bbbe2006-02-22 16:10:53 +00004620 * structure again after calling this function on it.
4621 * @param track the track metadata to destroy.
4622 * @see LIBMTP_new_track_t()
4623 */
Linus Walleijb9256fd2006-02-15 09:40:43 +00004624void LIBMTP_destroy_track_t(LIBMTP_track_t *track)
4625{
4626 if (track == NULL) {
4627 return;
4628 }
4629 if (track->title != NULL)
4630 free(track->title);
4631 if (track->artist != NULL)
4632 free(track->artist);
Linus Walleij31b74292008-05-02 23:29:06 +00004633 if (track->composer != NULL)
4634 free(track->composer);
Linus Walleijb9256fd2006-02-15 09:40:43 +00004635 if (track->album != NULL)
4636 free(track->album);
4637 if (track->genre != NULL)
4638 free(track->genre);
4639 if (track->date != NULL)
4640 free(track->date);
4641 if (track->filename != NULL)
4642 free(track->filename);
4643 free(track);
4644 return;
4645}
4646
4647/**
Linus Walleij338ade42007-07-03 20:44:08 +00004648 * This function maps and copies a property onto the track metadata if applicable.
4649 */
Linus Walleijddaba2f2007-10-02 21:20:30 +00004650static void pick_property_to_track_metadata(LIBMTP_mtpdevice_t *device, MTPProperties *prop, LIBMTP_track_t *track)
Linus Walleij338ade42007-07-03 20:44:08 +00004651{
4652 switch (prop->property) {
4653 case PTP_OPC_Name:
4654 if (prop->propval.str != NULL)
4655 track->title = strdup(prop->propval.str);
4656 else
4657 track->title = NULL;
4658 break;
4659 case PTP_OPC_Artist:
4660 if (prop->propval.str != NULL)
4661 track->artist = strdup(prop->propval.str);
4662 else
4663 track->artist = NULL;
4664 break;
Linus Walleij31b74292008-05-02 23:29:06 +00004665 case PTP_OPC_Composer:
4666 if (prop->propval.str != NULL)
4667 track->composer = strdup(prop->propval.str);
4668 else
4669 track->composer = NULL;
4670 break;
Linus Walleij338ade42007-07-03 20:44:08 +00004671 case PTP_OPC_Duration:
4672 track->duration = prop->propval.u32;
4673 break;
4674 case PTP_OPC_Track:
4675 track->tracknumber = prop->propval.u16;
4676 break;
4677 case PTP_OPC_Genre:
4678 if (prop->propval.str != NULL)
4679 track->genre = strdup(prop->propval.str);
4680 else
4681 track->genre = NULL;
4682 break;
4683 case PTP_OPC_AlbumName:
4684 if (prop->propval.str != NULL)
4685 track->album = strdup(prop->propval.str);
4686 else
4687 track->album = NULL;
4688 break;
4689 case PTP_OPC_OriginalReleaseDate:
4690 if (prop->propval.str != NULL)
4691 track->date = strdup(prop->propval.str);
4692 else
4693 track->date = NULL;
4694 break;
4695 // These are, well not so important.
4696 case PTP_OPC_SampleRate:
4697 track->samplerate = prop->propval.u32;
4698 break;
4699 case PTP_OPC_NumberOfChannels:
4700 track->nochannels = prop->propval.u16;
4701 break;
4702 case PTP_OPC_AudioWAVECodec:
4703 track->wavecodec = prop->propval.u32;
4704 break;
4705 case PTP_OPC_AudioBitRate:
4706 track->bitrate = prop->propval.u32;
4707 break;
4708 case PTP_OPC_BitRateType:
4709 track->bitratetype = prop->propval.u16;
4710 break;
4711 case PTP_OPC_Rating:
4712 track->rating = prop->propval.u16;
4713 break;
4714 case PTP_OPC_UseCount:
4715 track->usecount = prop->propval.u32;
4716 break;
Linus Walleijd9d28d52007-08-04 19:01:18 +00004717 case PTP_OPC_ObjectSize:
Linus Walleijddaba2f2007-10-02 21:20:30 +00004718 if (device->object_bitsize == 64) {
4719 track->filesize = prop->propval.u64;
4720 } else {
4721 track->filesize = prop->propval.u32;
Linus Walleijca2a1702007-10-02 20:41:45 +00004722 }
Linus Walleijd9d28d52007-08-04 19:01:18 +00004723 break;
4724 default:
4725 break;
Linus Walleij338ade42007-07-03 20:44:08 +00004726 }
4727}
4728
4729/**
Linus Walleij8ab54262006-06-21 07:12:28 +00004730 * This function retrieves the track metadata for a track
4731 * given by a unique ID.
4732 * @param device a pointer to the device to get the track metadata off.
4733 * @param trackid the unique ID of the track.
4734 * @param objectformat the object format of this track, so we know what it supports.
4735 * @param track a metadata set to fill in.
4736 */
4737static void get_track_metadata(LIBMTP_mtpdevice_t *device, uint16_t objectformat,
4738 LIBMTP_track_t *track)
4739{
Linus Walleij00cf0642006-07-26 20:40:59 +00004740 uint16_t ret;
4741 PTPParams *params = (PTPParams *) device->params;
4742 uint32_t i;
Linus Walleijd4637502009-06-14 23:03:33 +00004743 MTPProperties *prop;
4744 PTPObject *ob;
Linus Walleij00cf0642006-07-26 20:40:59 +00004745
Linus Walleij338ade42007-07-03 20:44:08 +00004746 /*
4747 * If we have a cached, large set of metadata, then use it!
4748 */
Linus Walleij08a5fe12009-09-08 21:28:58 +00004749 ret = ptp_object_want(params, track->item_id, PTPOBJECT_MTPPROPLIST_LOADED, &ob);
Linus Walleijd4637502009-06-14 23:03:33 +00004750 if (ob->mtpprops) {
4751 prop = ob->mtpprops;
4752 for (i=0;i<ob->nrofmtpprops;i++,prop++)
Linus Walleijddaba2f2007-10-02 21:20:30 +00004753 pick_property_to_track_metadata(device, prop, track);
Linus Walleij3fcfea52006-11-13 07:07:36 +00004754 } else {
Linus Walleij3fcfea52006-11-13 07:07:36 +00004755 uint16_t *props = NULL;
4756 uint32_t propcnt = 0;
4757
4758 // First see which properties can be retrieved for this object format
Linus Walleij070e9b42007-01-22 08:49:28 +00004759 ret = ptp_mtp_getobjectpropssupported(params, map_libmtp_type_to_ptp_type(track->filetype), &propcnt, &props);
Linus Walleij3fcfea52006-11-13 07:07:36 +00004760 if (ret != PTP_RC_OK) {
Linus Walleij070e9b42007-01-22 08:49:28 +00004761 add_ptp_error_to_errorstack(device, ret, "get_track_metadata(): call to ptp_mtp_getobjectpropssupported() failed.");
Linus Walleij3fcfea52006-11-13 07:07:36 +00004762 // Just bail out for now, nothing is ever set.
4763 return;
4764 } else {
4765 for (i=0;i<propcnt;i++) {
4766 switch (props[i]) {
4767 case PTP_OPC_Name:
Linus Walleij9901e222006-11-30 12:28:19 +00004768 track->title = get_string_from_object(device, track->item_id, PTP_OPC_Name);
Linus Walleij3fcfea52006-11-13 07:07:36 +00004769 break;
4770 case PTP_OPC_Artist:
Linus Walleij9901e222006-11-30 12:28:19 +00004771 track->artist = get_string_from_object(device, track->item_id, PTP_OPC_Artist);
Linus Walleij3fcfea52006-11-13 07:07:36 +00004772 break;
Linus Walleij31b74292008-05-02 23:29:06 +00004773 case PTP_OPC_Composer:
4774 track->composer = get_string_from_object(device, track->item_id, PTP_OPC_Composer);
4775 break;
Linus Walleij3fcfea52006-11-13 07:07:36 +00004776 case PTP_OPC_Duration:
Linus Walleij9901e222006-11-30 12:28:19 +00004777 track->duration = get_u32_from_object(device, track->item_id, PTP_OPC_Duration, 0);
Linus Walleij3fcfea52006-11-13 07:07:36 +00004778 break;
4779 case PTP_OPC_Track:
Linus Walleij9901e222006-11-30 12:28:19 +00004780 track->tracknumber = get_u16_from_object(device, track->item_id, PTP_OPC_Track, 0);
Linus Walleij3fcfea52006-11-13 07:07:36 +00004781 break;
4782 case PTP_OPC_Genre:
Linus Walleij9901e222006-11-30 12:28:19 +00004783 track->genre = get_string_from_object(device, track->item_id, PTP_OPC_Genre);
Linus Walleij3fcfea52006-11-13 07:07:36 +00004784 break;
4785 case PTP_OPC_AlbumName:
Linus Walleij9901e222006-11-30 12:28:19 +00004786 track->album = get_string_from_object(device, track->item_id, PTP_OPC_AlbumName);
Linus Walleij3fcfea52006-11-13 07:07:36 +00004787 break;
4788 case PTP_OPC_OriginalReleaseDate:
Linus Walleij9901e222006-11-30 12:28:19 +00004789 track->date = get_string_from_object(device, track->item_id, PTP_OPC_OriginalReleaseDate);
Linus Walleij3fcfea52006-11-13 07:07:36 +00004790 break;
4791 // These are, well not so important.
4792 case PTP_OPC_SampleRate:
Linus Walleij9901e222006-11-30 12:28:19 +00004793 track->samplerate = get_u32_from_object(device, track->item_id, PTP_OPC_SampleRate, 0);
Linus Walleij3fcfea52006-11-13 07:07:36 +00004794 break;
4795 case PTP_OPC_NumberOfChannels:
Linus Walleij9901e222006-11-30 12:28:19 +00004796 track->nochannels = get_u16_from_object(device, track->item_id, PTP_OPC_NumberOfChannels, 0);
Linus Walleij3fcfea52006-11-13 07:07:36 +00004797 break;
4798 case PTP_OPC_AudioWAVECodec:
Linus Walleij9901e222006-11-30 12:28:19 +00004799 track->wavecodec = get_u32_from_object(device, track->item_id, PTP_OPC_AudioWAVECodec, 0);
Linus Walleij3fcfea52006-11-13 07:07:36 +00004800 break;
4801 case PTP_OPC_AudioBitRate:
Linus Walleij9901e222006-11-30 12:28:19 +00004802 track->bitrate = get_u32_from_object(device, track->item_id, PTP_OPC_AudioBitRate, 0);
Linus Walleij3fcfea52006-11-13 07:07:36 +00004803 break;
4804 case PTP_OPC_BitRateType:
Linus Walleij9901e222006-11-30 12:28:19 +00004805 track->bitratetype = get_u16_from_object(device, track->item_id, PTP_OPC_BitRateType, 0);
Linus Walleij3fcfea52006-11-13 07:07:36 +00004806 break;
4807 case PTP_OPC_Rating:
Linus Walleij9901e222006-11-30 12:28:19 +00004808 track->rating = get_u16_from_object(device, track->item_id, PTP_OPC_Rating, 0);
Linus Walleij3fcfea52006-11-13 07:07:36 +00004809 break;
4810 case PTP_OPC_UseCount:
Linus Walleij9901e222006-11-30 12:28:19 +00004811 track->usecount = get_u32_from_object(device, track->item_id, PTP_OPC_UseCount, 0);
Linus Walleij3fcfea52006-11-13 07:07:36 +00004812 break;
Linus Walleijd9d28d52007-08-04 19:01:18 +00004813 case PTP_OPC_ObjectSize:
Linus Walleijddaba2f2007-10-02 21:20:30 +00004814 if (device->object_bitsize == 64) {
4815 track->filesize = get_u64_from_object(device, track->item_id, PTP_OPC_ObjectSize, 0);
4816 } else {
4817 track->filesize = (uint64_t) get_u32_from_object(device, track->item_id, PTP_OPC_ObjectSize, 0);
Linus Walleijca2a1702007-10-02 20:41:45 +00004818 }
Linus Walleijd9d28d52007-08-04 19:01:18 +00004819 break;
Linus Walleij3fcfea52006-11-13 07:07:36 +00004820 }
4821 }
4822 free(props);
4823 }
Linus Walleij00cf0642006-07-26 20:40:59 +00004824 }
Linus Walleij8ab54262006-06-21 07:12:28 +00004825}
4826
4827/**
mopoke31364442006-11-20 04:53:04 +00004828 * THIS FUNCTION IS DEPRECATED. PLEASE UPDATE YOUR CODE IN ORDER
Linus Walleij3fcfea52006-11-13 07:07:36 +00004829 * NOT TO USE IT.
4830 * @see LIBMTP_Get_Tracklisting_With_Callback()
4831 */
4832LIBMTP_track_t *LIBMTP_Get_Tracklisting(LIBMTP_mtpdevice_t *device)
4833{
nicklas79daadbf22009-09-28 18:19:34 +00004834 LIBMTP_INFO("WARNING: LIBMTP_Get_Tracklisting() is deprecated.\n");
4835 LIBMTP_INFO("WARNING: please update your code to use LIBMTP_Get_Tracklisting_With_Callback()\n");
Richard Lowdc0b6c72006-11-13 09:22:23 +00004836 return LIBMTP_Get_Tracklisting_With_Callback(device, NULL, NULL);
Linus Walleij3fcfea52006-11-13 07:07:36 +00004837}
4838
4839/**
Linus Walleij25d5c212008-08-14 06:49:13 +00004840 * This returns a long list of all tracks available on the current MTP device.
4841 * Tracks include multimedia objects, both music tracks and video tracks.
4842 * Typical usage:
Linus Walleija4982732006-02-24 15:46:02 +00004843 *
4844 * <pre>
4845 * LIBMTP_track_t *tracklist;
4846 *
Richard Low6711f442007-05-05 19:00:59 +00004847 * tracklist = LIBMTP_Get_Tracklisting_With_Callback(device, callback, data);
Linus Walleija4982732006-02-24 15:46:02 +00004848 * while (tracklist != NULL) {
4849 * LIBMTP_track_t *tmp;
4850 *
4851 * // Do something on each element in the list here...
4852 * tmp = tracklist;
4853 * tracklist = tracklist->next;
4854 * LIBMTP_destroy_track_t(tmp);
4855 * }
4856 * </pre>
4857 *
Linus Walleij25d5c212008-08-14 06:49:13 +00004858 * If you want to group your track listing by storage (per storage unit) or
4859 * arrange tracks into folders, you must dereference the <code>storage_id</code>
4860 * and/or <code>parent_id</code> field of the returned <code>LIBMTP_track_t</code>
4861 * struct. To arrange by folders or files you typically have to create the proper
Linus Walleij2c1bbd62009-11-07 15:15:03 +00004862 * trees by calls to <code>LIBMTP_Get_Storage()</code> and/or
Linus Walleij25d5c212008-08-14 06:49:13 +00004863 * <code>LIBMTP_Get_Folder_List()</code> first.
4864 *
Linus Walleijb9256fd2006-02-15 09:40:43 +00004865 * @param device a pointer to the device to get the track listing for.
Linus Walleij3fcfea52006-11-13 07:07:36 +00004866 * @param callback a function to be called during the tracklisting retrieveal
Linus Walleij25d5c212008-08-14 06:49:13 +00004867 * for displaying progress bars etc, or NULL if you don't want
4868 * any callbacks.
Richard Lowdc0b6c72006-11-13 09:22:23 +00004869 * @param data a user-defined pointer that is passed along to
Linus Walleij25d5c212008-08-14 06:49:13 +00004870 * the <code>progress</code> function in order to
4871 * pass along some user defined data to the progress
4872 * updates. If not used, set this to NULL.
Linus Walleija4982732006-02-24 15:46:02 +00004873 * @return a list of tracks that can be followed using the <code>next</code>
Linus Walleij25d5c212008-08-14 06:49:13 +00004874 * field of the <code>LIBMTP_track_t</code> data structure.
4875 * Each of the metadata tags must be freed after use, and may
4876 * contain only partial metadata information, i.e. one or several
4877 * fields may be NULL or 0.
Linus Walleij2e4b5f92006-06-16 14:00:49 +00004878 * @see LIBMTP_Get_Trackmetadata()
Linus Walleijb9256fd2006-02-15 09:40:43 +00004879 */
Richard Lowdc0b6c72006-11-13 09:22:23 +00004880LIBMTP_track_t *LIBMTP_Get_Tracklisting_With_Callback(LIBMTP_mtpdevice_t *device,
4881 LIBMTP_progressfunc_t const callback,
4882 void const * const data)
Linus Walleijb9256fd2006-02-15 09:40:43 +00004883{
nicklas7918b54d72009-11-07 19:54:07 +00004884 return LIBMTP_Get_Tracklisting_With_Callback_For_Storage(device, 0, callback, data);
4885}
4886
4887
4888/**
4889 * This returns a long list of all tracks available on the current MTP device.
4890 * Tracks include multimedia objects, both music tracks and video tracks.
4891 * Typical usage:
4892 *
4893 * <pre>
4894 * LIBMTP_track_t *tracklist;
4895 *
4896 * tracklist = LIBMTP_Get_Tracklisting_With_Callback_For_Storage(device, storage_id, callback, data);
4897 * while (tracklist != NULL) {
4898 * LIBMTP_track_t *tmp;
4899 *
4900 * // Do something on each element in the list here...
4901 * tmp = tracklist;
4902 * tracklist = tracklist->next;
4903 * LIBMTP_destroy_track_t(tmp);
4904 * }
4905 * </pre>
4906 *
4907 * If you want to group your track listing by storage (per storage unit) or
4908 * arrange tracks into folders, you must dereference the <code>storage_id</code>
4909 * and/or <code>parent_id</code> field of the returned <code>LIBMTP_track_t</code>
4910 * struct. To arrange by folders or files you typically have to create the proper
4911 * trees by calls to <code>LIBMTP_Get_Storage()</code> and/or
4912 * <code>LIBMTP_Get_Folder_List()</code> first.
4913 *
4914 * @param device a pointer to the device to get the track listing for.
4915 * @param storage_id ID of device storage (if null, no filter)
4916 * @param callback a function to be called during the tracklisting retrieveal
4917 * for displaying progress bars etc, or NULL if you don't want
4918 * any callbacks.
4919 * @param data a user-defined pointer that is passed along to
4920 * the <code>progress</code> function in order to
4921 * pass along some user defined data to the progress
4922 * updates. If not used, set this to NULL.
4923 * @return a list of tracks that can be followed using the <code>next</code>
4924 * field of the <code>LIBMTP_track_t</code> data structure.
4925 * Each of the metadata tags must be freed after use, and may
4926 * contain only partial metadata information, i.e. one or several
4927 * fields may be NULL or 0.
4928 * @see LIBMTP_Get_Trackmetadata()
4929 */
4930LIBMTP_track_t *LIBMTP_Get_Tracklisting_With_Callback_For_Storage(LIBMTP_mtpdevice_t *device, uint32_t const storage_id,
4931 LIBMTP_progressfunc_t const callback,
4932 void const * const data)
4933{
Linus Walleijb9256fd2006-02-15 09:40:43 +00004934 uint32_t i = 0;
4935 LIBMTP_track_t *retracks = NULL;
4936 LIBMTP_track_t *curtrack = NULL;
Linus Walleij9b28da32006-03-16 13:47:58 +00004937 PTPParams *params = (PTPParams *) device->params;
Linus Walleij2be40c72007-03-16 15:19:44 +00004938 PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo;
Linus Walleij438bd7f2006-06-08 11:35:44 +00004939
mopoke96143402006-10-30 04:37:26 +00004940 // Get all the handles if we haven't already done that
Linus Walleijd4637502009-06-14 23:03:33 +00004941 if (params->nrofobjects == 0) {
Linus Walleij438bd7f2006-06-08 11:35:44 +00004942 flush_handles(device);
Linus Walleijb9256fd2006-02-15 09:40:43 +00004943 }
mopoke96143402006-10-30 04:37:26 +00004944
Linus Walleijd4637502009-06-14 23:03:33 +00004945 for (i = 0; i < params->nrofobjects; i++) {
Linus Walleij070e9b42007-01-22 08:49:28 +00004946 LIBMTP_track_t *track;
Linus Walleijd4637502009-06-14 23:03:33 +00004947 PTPObject *ob;
Linus Walleij46651f32008-04-26 23:30:19 +00004948 LIBMTP_filetype_t mtptype;
mopoke31364442006-11-20 04:53:04 +00004949
Richard Lowdc0b6c72006-11-13 09:22:23 +00004950 if (callback != NULL)
Linus Walleijd4637502009-06-14 23:03:33 +00004951 callback(i, params->nrofobjects, data);
mopoke31364442006-11-20 04:53:04 +00004952
Linus Walleijd4637502009-06-14 23:03:33 +00004953 ob = &params->objects[i];
4954 mtptype = map_ptp_type_to_libmtp_type(ob->oi.ObjectFormat);
mopoke96143402006-10-30 04:37:26 +00004955
Linus Walleijf0bf4372007-07-01 21:47:38 +00004956 // Ignore stuff we don't know how to handle...
4957 // TODO: get this list as an intersection of the sets
4958 // supported by the device and the from the device and
Linus Walleijd5356e22008-11-01 22:08:59 +00004959 // all known track files?
4960 if (!LIBMTP_FILETYPE_IS_TRACK(mtptype) &&
Linus Walleij46651f32008-04-26 23:30:19 +00004961 // This row lets through undefined files for examination since they may be forgotten OGG files.
Linus Walleij2c1bbd62009-11-07 15:15:03 +00004962 (ob->oi.ObjectFormat != PTP_OFC_Undefined ||
Richard Lowef197312008-11-01 18:29:41 +00004963 (!FLAG_IRIVER_OGG_ALZHEIMER(ptp_usb) &&
Linus Walleij89bb1cd2009-07-24 21:03:36 +00004964 !FLAG_OGG_IS_UNKNOWN(ptp_usb) &&
4965 !FLAG_FLAC_IS_UNKNOWN(ptp_usb)))
Linus Walleij46651f32008-04-26 23:30:19 +00004966 ) {
Linus Walleijc8abc922008-09-28 18:38:42 +00004967 //printf("Not a music track (name: %s format: %d), skipping...\n", oi->Filename, oi->ObjectFormat);
Linus Walleijf0bf4372007-07-01 21:47:38 +00004968 continue;
Linus Walleijb9256fd2006-02-15 09:40:43 +00004969 }
4970
nicklas7918b54d72009-11-07 19:54:07 +00004971 // Ignore stuff that isn't into the storage device
4972 if ((storage_id != 0) && (ob->oi.StorageID != storage_id ))
4973 continue;
4974
Linus Walleijf0bf4372007-07-01 21:47:38 +00004975 // Allocate a new track type
4976 track = LIBMTP_new_track_t();
Linus Walleij2c1bbd62009-11-07 15:15:03 +00004977
Linus Walleijf0bf4372007-07-01 21:47:38 +00004978 // This is some sort of unique ID so we can keep track of the track.
Linus Walleijd4637502009-06-14 23:03:33 +00004979 track->item_id = ob->oid;
4980 track->parent_id = ob->oi.ParentObject;
4981 track->storage_id = ob->oi.StorageID;
4982 track->modificationdate = ob->oi.ModificationDate;
Linus Walleijf0bf4372007-07-01 21:47:38 +00004983
Linus Walleij46651f32008-04-26 23:30:19 +00004984 track->filetype = mtptype;
Linus Walleijf0bf4372007-07-01 21:47:38 +00004985
4986 // Original file-specific properties
Linus Walleijd4637502009-06-14 23:03:33 +00004987 track->filesize = ob->oi.ObjectCompressedSize;
4988 if (ob->oi.Filename != NULL) {
4989 track->filename = strdup(ob->oi.Filename);
Linus Walleijf0bf4372007-07-01 21:47:38 +00004990 }
4991
Linus Walleijd4637502009-06-14 23:03:33 +00004992 get_track_metadata(device, ob->oi.ObjectFormat, track);
Linus Walleijf0bf4372007-07-01 21:47:38 +00004993
4994 /*
4995 * A special quirk for iriver devices that doesn't quite
4996 * remember that some files marked as "unknown" type are
Linus Walleij89bb1cd2009-07-24 21:03:36 +00004997 * actually OGG or FLAC files. We look at the filename extension
4998 * and see if it happens that this was atleast named "ogg" or "flac"
4999 * and fall back on this heuristic approach in that case,
Linus Walleijf0bf4372007-07-01 21:47:38 +00005000 * for these bugged devices only.
5001 */
5002 if (track->filetype == LIBMTP_FILETYPE_UNKNOWN &&
Linus Walleij89bb1cd2009-07-24 21:03:36 +00005003 track->filename != NULL) {
5004 if ((FLAG_IRIVER_OGG_ALZHEIMER(ptp_usb) ||
5005 FLAG_OGG_IS_UNKNOWN(ptp_usb)) &&
5006 has_ogg_extension(track->filename))
Linus Walleijf0bf4372007-07-01 21:47:38 +00005007 track->filetype = LIBMTP_FILETYPE_OGG;
Linus Walleij89bb1cd2009-07-24 21:03:36 +00005008 else if (FLAG_FLAC_IS_UNKNOWN(ptp_usb) &&
5009 has_flac_extension(track->filename))
5010 track->filetype = LIBMTP_FILETYPE_FLAC;
5011 else {
5012 // This was not an OGG/FLAC file so discard it and continue
Linus Walleijf0bf4372007-07-01 21:47:38 +00005013 LIBMTP_destroy_track_t(track);
5014 continue;
5015 }
5016 }
Linus Walleij2c1bbd62009-11-07 15:15:03 +00005017
Linus Walleijf0bf4372007-07-01 21:47:38 +00005018 // Add track to a list that will be returned afterwards.
5019 if (retracks == NULL) {
5020 retracks = track;
5021 curtrack = track;
5022 } else {
5023 curtrack->next = track;
5024 curtrack = track;
5025 }
Linus Walleij2c1bbd62009-11-07 15:15:03 +00005026
Linus Walleijf0bf4372007-07-01 21:47:38 +00005027 // Call listing callback
5028 // double progressPercent = (double)i*(double)100.0 / (double)params->handles.n;
Linus Walleij2c1bbd62009-11-07 15:15:03 +00005029
Linus Walleijb9256fd2006-02-15 09:40:43 +00005030 } // Handle counting loop
5031 return retracks;
5032}
Linus Walleijdcde6082006-02-17 16:16:34 +00005033
Linus Walleij2e4b5f92006-06-16 14:00:49 +00005034/**
5035 * This function retrieves the metadata for a single track off
5036 * the device.
5037 *
5038 * Do not call this function repeatedly! The track handles are linearly
5039 * searched O(n) and the call may involve (slow) USB traffic, so use
5040 * <code>LIBMTP_Get_Tracklisting()</code> and cache the tracks, preferably
5041 * as an efficient data structure such as a hash list.
5042 *
5043 * @param device a pointer to the device to get the track metadata from.
5044 * @param trackid the object ID of the track that you want the metadata for.
5045 * @return a track metadata entry on success or NULL on failure.
5046 * @see LIBMTP_Get_Tracklisting()
5047 */
5048LIBMTP_track_t *LIBMTP_Get_Trackmetadata(LIBMTP_mtpdevice_t *device, uint32_t const trackid)
5049{
Linus Walleij2e4b5f92006-06-16 14:00:49 +00005050 PTPParams *params = (PTPParams *) device->params;
Linus Walleijc8abc922008-09-28 18:38:42 +00005051 PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo;
Linus Walleijd4637502009-06-14 23:03:33 +00005052 PTPObject *ob;
5053 LIBMTP_track_t *track;
5054 LIBMTP_filetype_t mtptype;
5055 uint16_t ret;
Linus Walleij2e4b5f92006-06-16 14:00:49 +00005056
mopoke96143402006-10-30 04:37:26 +00005057 // Get all the handles if we haven't already done that
Linus Walleijd4637502009-06-14 23:03:33 +00005058 if (params->nrofobjects == 0)
Linus Walleij2e4b5f92006-06-16 14:00:49 +00005059 flush_handles(device);
Linus Walleijd4637502009-06-14 23:03:33 +00005060
5061 ret = ptp_object_want (params, trackid, PTPOBJECT_OBJECTINFO_LOADED, &ob);
5062 if (ret != PTP_RC_OK)
5063 return NULL;
5064
5065 mtptype = map_ptp_type_to_libmtp_type(ob->oi.ObjectFormat);
5066
5067 // Ignore stuff we don't know how to handle...
5068 if (!LIBMTP_FILETYPE_IS_TRACK(mtptype) &&
Linus Walleij89bb1cd2009-07-24 21:03:36 +00005069 /*
5070 * This row lets through undefined files for examination
5071 * since they may be forgotten OGG or FLAC files.
5072 */
Linus Walleij2c1bbd62009-11-07 15:15:03 +00005073 (ob->oi.ObjectFormat != PTP_OFC_Undefined ||
Linus Walleijd4637502009-06-14 23:03:33 +00005074 (!FLAG_IRIVER_OGG_ALZHEIMER(ptp_usb) &&
Linus Walleij89bb1cd2009-07-24 21:03:36 +00005075 !FLAG_OGG_IS_UNKNOWN(ptp_usb) &&
5076 !FLAG_FLAC_IS_UNKNOWN(ptp_usb)))
Linus Walleijd4637502009-06-14 23:03:33 +00005077 ) {
5078 //printf("Not a music track (name: %s format: %d), skipping...\n", oi->Filename, oi->ObjectFormat);
5079 return NULL;
Linus Walleij2e4b5f92006-06-16 14:00:49 +00005080 }
5081
Linus Walleijd4637502009-06-14 23:03:33 +00005082 // Allocate a new track type
5083 track = LIBMTP_new_track_t();
Linus Walleij2c1bbd62009-11-07 15:15:03 +00005084
Linus Walleijd4637502009-06-14 23:03:33 +00005085 // This is some sort of unique ID so we can keep track of the track.
5086 track->item_id = ob->oid;
5087 track->parent_id = ob->oi.ParentObject;
5088 track->storage_id = ob->oi.StorageID;
5089 track->modificationdate = ob->oi.ModificationDate;
mopoke96143402006-10-30 04:37:26 +00005090
Linus Walleijd4637502009-06-14 23:03:33 +00005091 track->filetype = mtptype;
Linus Walleij2e4b5f92006-06-16 14:00:49 +00005092
Linus Walleijd4637502009-06-14 23:03:33 +00005093 // Original file-specific properties
5094 track->filesize = ob->oi.ObjectCompressedSize;
5095 if (ob->oi.Filename != NULL) {
5096 track->filename = strdup(ob->oi.Filename);
5097 }
mopoke96143402006-10-30 04:37:26 +00005098
Linus Walleijd4637502009-06-14 23:03:33 +00005099 /*
Linus Walleij89bb1cd2009-07-24 21:03:36 +00005100 * A special quirk for devices that doesn't quite
Linus Walleijd4637502009-06-14 23:03:33 +00005101 * remember that some files marked as "unknown" type are
Linus Walleij89bb1cd2009-07-24 21:03:36 +00005102 * actually OGG or FLAC files. We look at the filename extension
Linus Walleijd4637502009-06-14 23:03:33 +00005103 * and see if it happens that this was atleast named "ogg"
Linus Walleij2c1bbd62009-11-07 15:15:03 +00005104 * and fall back on this heuristic approach in that case,
Linus Walleijd4637502009-06-14 23:03:33 +00005105 * for these bugged devices only.
5106 */
5107 if (track->filetype == LIBMTP_FILETYPE_UNKNOWN &&
Linus Walleij89bb1cd2009-07-24 21:03:36 +00005108 track->filename != NULL) {
5109 if ((FLAG_IRIVER_OGG_ALZHEIMER(ptp_usb) ||
5110 FLAG_OGG_IS_UNKNOWN(ptp_usb)) &&
5111 has_ogg_extension(track->filename))
Linus Walleijd4637502009-06-14 23:03:33 +00005112 track->filetype = LIBMTP_FILETYPE_OGG;
Linus Walleij89bb1cd2009-07-24 21:03:36 +00005113 else if (FLAG_FLAC_IS_UNKNOWN(ptp_usb) &&
5114 has_flac_extension(track->filename))
5115 track->filetype = LIBMTP_FILETYPE_FLAC;
5116 else {
5117 // This was not an OGG/FLAC file so discard it
Linus Walleijd4637502009-06-14 23:03:33 +00005118 LIBMTP_destroy_track_t(track);
Linus Walleij2e4b5f92006-06-16 14:00:49 +00005119 return NULL;
5120 }
Linus Walleij2e4b5f92006-06-16 14:00:49 +00005121 }
Linus Walleijd4637502009-06-14 23:03:33 +00005122 get_track_metadata(device, ob->oi.ObjectFormat, track);
5123 return track;
Linus Walleij2e4b5f92006-06-16 14:00:49 +00005124}
5125
Richard Low5b4023c2009-04-16 19:14:38 +00005126/**
5127 * This is a manual conversion from MTPDataGetFunc to PTPDataGetFunc
5128 * to isolate the internal type.
5129 */
Linus Walleijd866d242009-08-23 21:50:39 +00005130static uint16_t get_func_wrapper(PTPParams* params, void* priv, unsigned long wantlen, unsigned char *data, unsigned long *gotlen)
Richard Low5b4023c2009-04-16 19:14:38 +00005131{
Linus Walleijd866d242009-08-23 21:50:39 +00005132 MTPDataHandler *handler = (MTPDataHandler *)priv;
Richard Low5b4023c2009-04-16 19:14:38 +00005133 uint16_t ret;
5134 uint32_t local_gotlen = 0;
Linus Walleijd866d242009-08-23 21:50:39 +00005135 ret = handler->getfunc(params, handler->priv, wantlen, data, &local_gotlen);
Richard Low5b4023c2009-04-16 19:14:38 +00005136 *gotlen = local_gotlen;
Richard Low6f070842009-05-03 10:26:16 +00005137 switch (ret)
5138 {
5139 case LIBMTP_HANDLER_RETURN_OK:
5140 return PTP_RC_OK;
5141 case LIBMTP_HANDLER_RETURN_ERROR:
5142 return PTP_ERROR_IO;
5143 case LIBMTP_HANDLER_RETURN_CANCEL:
5144 return PTP_ERROR_CANCEL;
5145 default:
5146 return PTP_ERROR_IO;
5147 }
Richard Low5b4023c2009-04-16 19:14:38 +00005148}
5149
5150/**
5151 * This is a manual conversion from MTPDataPutFunc to PTPDataPutFunc
5152 * to isolate the internal type.
5153 */
Marcus Meissner3d692dc2016-02-21 12:34:06 +01005154static uint16_t put_func_wrapper(PTPParams* params, void* priv, unsigned long sendlen, unsigned char *data)
Richard Low5b4023c2009-04-16 19:14:38 +00005155{
Linus Walleijd866d242009-08-23 21:50:39 +00005156 MTPDataHandler *handler = (MTPDataHandler *)priv;
Richard Low5b4023c2009-04-16 19:14:38 +00005157 uint16_t ret;
5158 uint32_t local_putlen = 0;
Marcus Meissner3d692dc2016-02-21 12:34:06 +01005159
Linus Walleijd866d242009-08-23 21:50:39 +00005160 ret = handler->putfunc(params, handler->priv, sendlen, data, &local_putlen);
Marcus Meissner3d692dc2016-02-21 12:34:06 +01005161
Richard Low75981ac2009-05-03 12:54:06 +00005162 switch (ret)
5163 {
5164 case LIBMTP_HANDLER_RETURN_OK:
Marcus Meissner3d692dc2016-02-21 12:34:06 +01005165 if (local_putlen != sendlen)
5166 return PTP_ERROR_IO;
Richard Low75981ac2009-05-03 12:54:06 +00005167 return PTP_RC_OK;
5168 case LIBMTP_HANDLER_RETURN_ERROR:
5169 return PTP_ERROR_IO;
5170 case LIBMTP_HANDLER_RETURN_CANCEL:
5171 return PTP_ERROR_CANCEL;
5172 default:
5173 return PTP_ERROR_IO;
5174 }
Richard Low5b4023c2009-04-16 19:14:38 +00005175}
Linus Walleijf6bc1782006-03-24 15:12:47 +00005176
5177/**
5178 * This gets a file off the device to a local file identified
5179 * by a filename.
5180 * @param device a pointer to the device to get the track from.
5181 * @param id the file ID of the file to retrieve.
5182 * @param path a filename to use for the retrieved file.
5183 * @param callback a progress indicator function or NULL to ignore.
5184 * @param data a user-defined pointer that is passed along to
5185 * the <code>progress</code> function in order to
5186 * pass along some user defined data to the progress
5187 * updates. If not used, set this to NULL.
mopoke96143402006-10-30 04:37:26 +00005188 * @return 0 if the transfer was successful, any other value means
Linus Walleijf6bc1782006-03-24 15:12:47 +00005189 * failure.
5190 * @see LIBMTP_Get_File_To_File_Descriptor()
5191 */
mopoke96143402006-10-30 04:37:26 +00005192int LIBMTP_Get_File_To_File(LIBMTP_mtpdevice_t *device, uint32_t const id,
Linus Walleijee73ef22006-08-27 19:56:00 +00005193 char const * const path, LIBMTP_progressfunc_t const callback,
Linus Walleijf6bc1782006-03-24 15:12:47 +00005194 void const * const data)
5195{
5196 int fd = -1;
5197 int ret;
5198
5199 // Sanity check
5200 if (path == NULL) {
Linus Walleij070e9b42007-01-22 08:49:28 +00005201 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Get_File_To_File(): Bad arguments, path was NULL.");
Linus Walleijf6bc1782006-03-24 15:12:47 +00005202 return -1;
5203 }
5204
5205 // Open file
5206#ifdef __WIN32__
Linus Walleij7beba572006-11-29 08:56:12 +00005207#ifdef USE_WINDOWS_IO_H
5208 if ( (fd = _open(path, O_RDWR|O_CREAT|O_TRUNC|O_BINARY,_S_IREAD)) == -1 ) {
5209#else
Linus Walleij5b4a4d22009-01-10 12:18:14 +00005210 if ( (fd = open(path, O_RDWR|O_CREAT|O_TRUNC|O_BINARY,S_IRWXU)) == -1 ) {
Linus Walleij7beba572006-11-29 08:56:12 +00005211#endif
Linus Walleijf6bc1782006-03-24 15:12:47 +00005212#else
5213 if ( (fd = open(path, O_RDWR|O_CREAT|O_TRUNC,S_IRWXU|S_IRGRP)) == -1) {
5214#endif
Linus Walleij070e9b42007-01-22 08:49:28 +00005215 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Get_File_To_File(): Could not create file.");
Linus Walleijf6bc1782006-03-24 15:12:47 +00005216 return -1;
5217 }
mopoke96143402006-10-30 04:37:26 +00005218
Linus Walleijf6bc1782006-03-24 15:12:47 +00005219 ret = LIBMTP_Get_File_To_File_Descriptor(device, id, fd, callback, data);
5220
5221 // Close file
5222 close(fd);
mopoke96143402006-10-30 04:37:26 +00005223
Linus Walleij25d33b52007-09-16 21:36:19 +00005224 // Delete partial file.
5225 if (ret == -1) {
5226 unlink(path);
5227 }
5228
Linus Walleijf6bc1782006-03-24 15:12:47 +00005229 return ret;
5230}
5231
5232/**
5233 * This gets a file off the device to a file identified
5234 * by a file descriptor.
Linus Walleij0558ac52006-09-07 06:55:03 +00005235 *
mopoke96143402006-10-30 04:37:26 +00005236 * This function can potentially be used for streaming
5237 * files off the device for playback or broadcast for example,
Linus Walleij0558ac52006-09-07 06:55:03 +00005238 * by downloading the file into a stream sink e.g. a socket.
5239 *
Linus Walleijf6bc1782006-03-24 15:12:47 +00005240 * @param device a pointer to the device to get the file from.
5241 * @param id the file ID of the file to retrieve.
5242 * @param fd a local file descriptor to write the file to.
5243 * @param callback a progress indicator function or NULL to ignore.
5244 * @param data a user-defined pointer that is passed along to
5245 * the <code>progress</code> function in order to
5246 * pass along some user defined data to the progress
5247 * updates. If not used, set this to NULL.
mopoke96143402006-10-30 04:37:26 +00005248 * @return 0 if the transfer was successful, any other value means
Linus Walleijf6bc1782006-03-24 15:12:47 +00005249 * failure.
5250 * @see LIBMTP_Get_File_To_File()
5251 */
mopoke96143402006-10-30 04:37:26 +00005252int LIBMTP_Get_File_To_File_Descriptor(LIBMTP_mtpdevice_t *device,
5253 uint32_t const id,
5254 int const fd,
Linus Walleijee73ef22006-08-27 19:56:00 +00005255 LIBMTP_progressfunc_t const callback,
Linus Walleijf6bc1782006-03-24 15:12:47 +00005256 void const * const data)
5257{
Linus Walleij438bd7f2006-06-08 11:35:44 +00005258 uint16_t ret;
Linus Walleijf6bc1782006-03-24 15:12:47 +00005259 PTPParams *params = (PTPParams *) device->params;
Linus Walleijee73ef22006-08-27 19:56:00 +00005260 PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo;
Linus Walleijf6bc1782006-03-24 15:12:47 +00005261
Philip Langdaleb2bcdb92018-03-12 21:32:20 +01005262 LIBMTP_file_t *mtpfile = LIBMTP_Get_Filemetadata(device, id);
5263 if (mtpfile == NULL) {
Linus Walleijf0bf4372007-07-01 21:47:38 +00005264 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Get_File_To_File_Descriptor(): Could not get object info.");
Linus Walleijf6bc1782006-03-24 15:12:47 +00005265 return -1;
5266 }
Philip Langdaleb2bcdb92018-03-12 21:32:20 +01005267 if (mtpfile->filetype == LIBMTP_FILETYPE_FOLDER) {
Linus Walleij070e9b42007-01-22 08:49:28 +00005268 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Get_File_To_File_Descriptor(): Bad object format.");
Linus Walleijf6bc1782006-03-24 15:12:47 +00005269 return -1;
5270 }
Linus Walleijf6bc1782006-03-24 15:12:47 +00005271
Linus Walleijee73ef22006-08-27 19:56:00 +00005272 // Callbacks
5273 ptp_usb->callback_active = 1;
Philip Langdaleb2bcdb92018-03-12 21:32:20 +01005274 ptp_usb->current_transfer_total = mtpfile->filesize +
Linus Walleij7f0c72e2006-09-06 13:01:58 +00005275 PTP_USB_BULK_HDR_LEN+sizeof(uint32_t); // Request length, one parameter
Linus Walleijee73ef22006-08-27 19:56:00 +00005276 ptp_usb->current_transfer_complete = 0;
5277 ptp_usb->current_transfer_callback = callback;
5278 ptp_usb->current_transfer_callback_data = data;
mopoke96143402006-10-30 04:37:26 +00005279
Philip Langdaleb2bcdb92018-03-12 21:32:20 +01005280 // Don't need mtpfile anymore
5281 LIBMTP_destroy_file_t(mtpfile);
5282
Linus Walleij96c62432006-08-21 10:04:02 +00005283 ret = ptp_getobject_tofd(params, id, fd);
Linus Walleijee73ef22006-08-27 19:56:00 +00005284
5285 ptp_usb->callback_active = 0;
5286 ptp_usb->current_transfer_callback = NULL;
5287 ptp_usb->current_transfer_callback_data = NULL;
mopoke96143402006-10-30 04:37:26 +00005288
Linus Walleijbd3bf9e2007-09-14 19:31:54 +00005289 if (ret == PTP_ERROR_CANCEL) {
Linus Walleijfb28b632007-10-23 21:56:18 +00005290 add_error_to_errorstack(device, LIBMTP_ERROR_CANCELLED, "LIBMTP_Get_File_From_File_Descriptor(): Cancelled transfer.");
Linus Walleijbd3bf9e2007-09-14 19:31:54 +00005291 return -1;
5292 }
Linus Walleijb02a0662006-04-25 08:05:09 +00005293 if (ret != PTP_RC_OK) {
Linus Walleij070e9b42007-01-22 08:49:28 +00005294 add_ptp_error_to_errorstack(device, ret, "LIBMTP_Get_File_To_File_Descriptor(): Could not get file from device.");
Linus Walleijf6bc1782006-03-24 15:12:47 +00005295 return -1;
5296 }
mopoke96143402006-10-30 04:37:26 +00005297
Linus Walleijf6bc1782006-03-24 15:12:47 +00005298 return 0;
5299}
5300
Linus Walleijdcde6082006-02-17 16:16:34 +00005301/**
Richard Lowd3b17022009-04-11 12:37:39 +00005302 * This gets a file off the device and calls put_func
5303 * with chunks of data
5304 *
5305 * @param device a pointer to the device to get the file from.
5306 * @param id the file ID of the file to retrieve.
5307 * @param put_func the function to call when we have data.
5308 * @param priv the user-defined pointer that is passed to
5309 * <code>put_func</code>.
5310 * @param callback a progress indicator function or NULL to ignore.
5311 * @param data a user-defined pointer that is passed along to
5312 * the <code>progress</code> function in order to
5313 * pass along some user defined data to the progress
5314 * updates. If not used, set this to NULL.
5315 * @return 0 if the transfer was successful, any other value means
5316 * failure.
5317 */
5318int LIBMTP_Get_File_To_Handler(LIBMTP_mtpdevice_t *device,
5319 uint32_t const id,
5320 MTPDataPutFunc put_func,
5321 void * priv,
5322 LIBMTP_progressfunc_t const callback,
5323 void const * const data)
5324{
Richard Lowd3b17022009-04-11 12:37:39 +00005325 uint16_t ret;
5326 PTPParams *params = (PTPParams *) device->params;
5327 PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo;
5328
Philip Langdaleb2bcdb92018-03-12 21:32:20 +01005329 LIBMTP_file_t *mtpfile = LIBMTP_Get_Filemetadata(device, id);
5330 if (mtpfile == NULL) {
Richard Lowd3b17022009-04-11 12:37:39 +00005331 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Get_File_To_File_Descriptor(): Could not get object info.");
5332 return -1;
5333 }
Philip Langdaleb2bcdb92018-03-12 21:32:20 +01005334 if (mtpfile->filetype == LIBMTP_FILETYPE_FOLDER) {
Richard Lowd3b17022009-04-11 12:37:39 +00005335 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Get_File_To_File_Descriptor(): Bad object format.");
5336 return -1;
5337 }
5338
5339 // Callbacks
5340 ptp_usb->callback_active = 1;
Philip Langdaleb2bcdb92018-03-12 21:32:20 +01005341 ptp_usb->current_transfer_total = mtpfile->filesize +
Richard Lowd3b17022009-04-11 12:37:39 +00005342 PTP_USB_BULK_HDR_LEN+sizeof(uint32_t); // Request length, one parameter
5343 ptp_usb->current_transfer_complete = 0;
5344 ptp_usb->current_transfer_callback = callback;
5345 ptp_usb->current_transfer_callback_data = data;
5346
Philip Langdaleb2bcdb92018-03-12 21:32:20 +01005347 // Don't need mtpfile anymore
5348 LIBMTP_destroy_file_t(mtpfile);
5349
Richard Low5b4023c2009-04-16 19:14:38 +00005350 MTPDataHandler mtp_handler;
5351 mtp_handler.getfunc = NULL;
5352 mtp_handler.putfunc = put_func;
Linus Walleijd866d242009-08-23 21:50:39 +00005353 mtp_handler.priv = priv;
5354
Richard Lowd3b17022009-04-11 12:37:39 +00005355 PTPDataHandler handler;
5356 handler.getfunc = NULL;
Richard Low5b4023c2009-04-16 19:14:38 +00005357 handler.putfunc = put_func_wrapper;
Linus Walleijd866d242009-08-23 21:50:39 +00005358 handler.priv = &mtp_handler;
5359
Richard Lowd3b17022009-04-11 12:37:39 +00005360 ret = ptp_getobject_to_handler(params, id, &handler);
5361
5362 ptp_usb->callback_active = 0;
5363 ptp_usb->current_transfer_callback = NULL;
5364 ptp_usb->current_transfer_callback_data = NULL;
5365
5366 if (ret == PTP_ERROR_CANCEL) {
5367 add_error_to_errorstack(device, LIBMTP_ERROR_CANCELLED, "LIBMTP_Get_File_From_File_Descriptor(): Cancelled transfer.");
5368 return -1;
5369 }
5370 if (ret != PTP_RC_OK) {
5371 add_ptp_error_to_errorstack(device, ret, "LIBMTP_Get_File_To_File_Descriptor(): Could not get file from device.");
5372 return -1;
5373 }
5374
5375 return 0;
5376}
5377
5378
5379/**
Linus Walleijdcde6082006-02-17 16:16:34 +00005380 * This gets a track off the device to a file identified
Linus Walleijf6bc1782006-03-24 15:12:47 +00005381 * by a filename. This is actually just a wrapper for the
5382 * \c LIBMTP_Get_Track_To_File() function.
Linus Walleijdcde6082006-02-17 16:16:34 +00005383 * @param device a pointer to the device to get the track from.
5384 * @param id the track ID of the track to retrieve.
5385 * @param path a filename to use for the retrieved track.
5386 * @param callback a progress indicator function or NULL to ignore.
5387 * @param data a user-defined pointer that is passed along to
5388 * the <code>progress</code> function in order to
5389 * pass along some user defined data to the progress
5390 * updates. If not used, set this to NULL.
mopoke96143402006-10-30 04:37:26 +00005391 * @return 0 if the transfer was successful, any other value means
Linus Walleijdcde6082006-02-17 16:16:34 +00005392 * failure.
5393 * @see LIBMTP_Get_Track_To_File_Descriptor()
5394 */
mopoke96143402006-10-30 04:37:26 +00005395int LIBMTP_Get_Track_To_File(LIBMTP_mtpdevice_t *device, uint32_t const id,
Linus Walleijee73ef22006-08-27 19:56:00 +00005396 char const * const path, LIBMTP_progressfunc_t const callback,
Linus Walleij0cd85432006-02-20 14:37:50 +00005397 void const * const data)
Linus Walleijdcde6082006-02-17 16:16:34 +00005398{
Linus Walleijf6bc1782006-03-24 15:12:47 +00005399 // This is just a wrapper
5400 return LIBMTP_Get_File_To_File(device, id, path, callback, data);
Linus Walleijdcde6082006-02-17 16:16:34 +00005401}
5402
5403/**
5404 * This gets a track off the device to a file identified
Linus Walleijf6bc1782006-03-24 15:12:47 +00005405 * by a file descriptor. This is actually just a wrapper for
5406 * the \c LIBMTP_Get_File_To_File_Descriptor() function.
Linus Walleijdcde6082006-02-17 16:16:34 +00005407 * @param device a pointer to the device to get the track from.
5408 * @param id the track ID of the track to retrieve.
5409 * @param fd a file descriptor to write the track to.
5410 * @param callback a progress indicator function or NULL to ignore.
5411 * @param data a user-defined pointer that is passed along to
5412 * the <code>progress</code> function in order to
5413 * pass along some user defined data to the progress
5414 * updates. If not used, set this to NULL.
mopoke96143402006-10-30 04:37:26 +00005415 * @return 0 if the transfer was successful, any other value means
Linus Walleijdcde6082006-02-17 16:16:34 +00005416 * failure.
5417 * @see LIBMTP_Get_Track_To_File()
5418 */
mopoke96143402006-10-30 04:37:26 +00005419int LIBMTP_Get_Track_To_File_Descriptor(LIBMTP_mtpdevice_t *device,
5420 uint32_t const id,
5421 int const fd,
Linus Walleijee73ef22006-08-27 19:56:00 +00005422 LIBMTP_progressfunc_t const callback,
Linus Walleij0cd85432006-02-20 14:37:50 +00005423 void const * const data)
Linus Walleijdcde6082006-02-17 16:16:34 +00005424{
Linus Walleijf6bc1782006-03-24 15:12:47 +00005425 // This is just a wrapper
5426 return LIBMTP_Get_File_To_File_Descriptor(device, id, fd, callback, data);
Linus Walleijdcde6082006-02-17 16:16:34 +00005427}
Linus Walleij394bbbe2006-02-22 16:10:53 +00005428
5429/**
Richard Lowd3b17022009-04-11 12:37:39 +00005430 * This gets a track off the device to a handler function.
5431 * This is actually just a wrapper for
5432 * the \c LIBMTP_Get_File_To_Handler() function.
5433 * @param device a pointer to the device to get the track from.
5434 * @param id the track ID of the track to retrieve.
5435 * @param put_func the function to call when we have data.
5436 * @param priv the user-defined pointer that is passed to
5437 * <code>put_func</code>.
5438 * @param callback a progress indicator function or NULL to ignore.
5439 * @param data a user-defined pointer that is passed along to
5440 * the <code>progress</code> function in order to
5441 * pass along some user defined data to the progress
5442 * updates. If not used, set this to NULL.
5443 * @return 0 if the transfer was successful, any other value means
5444 * failure.
5445 */
5446int LIBMTP_Get_Track_To_Handler(LIBMTP_mtpdevice_t *device,
5447 uint32_t const id,
5448 MTPDataPutFunc put_func,
5449 void * priv,
5450 LIBMTP_progressfunc_t const callback,
5451 void const * const data)
5452{
5453 // This is just a wrapper
5454 return LIBMTP_Get_File_To_Handler(device, id, put_func, priv, callback, data);
5455}
5456
5457/**
Linus Walleij394bbbe2006-02-22 16:10:53 +00005458 * This function sends a track from a local file to an
5459 * MTP device. A filename and a set of metadata must be
5460 * given as input.
5461 * @param device a pointer to the device to send the track to.
5462 * @param path the filename of a local file which will be sent.
5463 * @param metadata a track metadata set to be written along with the file.
Linus Walleijea68f1f2008-06-22 21:54:44 +00005464 * After this call the field <code>metadata-&gt;item_id</code>
5465 * will contain the new track ID. Other fields such
5466 * as the <code>metadata-&gt;filename</code>, <code>metadata-&gt;parent_id</code>
Linus Walleij2c1bbd62009-11-07 15:15:03 +00005467 * or <code>metadata-&gt;storage_id</code> may also change during this
Linus Walleijea68f1f2008-06-22 21:54:44 +00005468 * operation due to device restrictions, so do not rely on the
5469 * contents of this struct to be preserved in any way.
5470 * <ul>
Linus Walleij2c1bbd62009-11-07 15:15:03 +00005471 * <li><code>metadata-&gt;parent_id</code> should be set to the parent
5472 * (e.g. folder) to store this track in. Since some
Linus Walleijea68f1f2008-06-22 21:54:44 +00005473 * devices are a bit picky about where files
5474 * are placed, a default folder will be chosen if libmtp
5475 * has detected one for the current filetype and this
5476 * parameter is set to 0. If this is 0 and no default folder
5477 * can be found, the file will be stored in the root folder.
5478 * <li><code>metadata-&gt;storage_id</code> should be set to the
5479 * desired storage (e.g. memory card or whatever your device
5480 * presents) to store this track in. Setting this to 0 will store
5481 * the track on the primary storage.
5482 * </ul>
Linus Walleij394bbbe2006-02-22 16:10:53 +00005483 * @param callback a progress indicator function or NULL to ignore.
5484 * @param data a user-defined pointer that is passed along to
5485 * the <code>progress</code> function in order to
5486 * pass along some user defined data to the progress
5487 * updates. If not used, set this to NULL.
mopoke96143402006-10-30 04:37:26 +00005488 * @return 0 if the transfer was successful, any other value means
Linus Walleij394bbbe2006-02-22 16:10:53 +00005489 * failure.
5490 * @see LIBMTP_Send_Track_From_File_Descriptor()
Linus Walleij1b87ea72007-08-28 07:53:09 +00005491 * @see LIBMTP_Send_File_From_File()
Linus Walleij438bd7f2006-06-08 11:35:44 +00005492 * @see LIBMTP_Delete_Object()
Linus Walleij394bbbe2006-02-22 16:10:53 +00005493 */
mopoke96143402006-10-30 04:37:26 +00005494int LIBMTP_Send_Track_From_File(LIBMTP_mtpdevice_t *device,
Linus Walleij394bbbe2006-02-22 16:10:53 +00005495 char const * const path, LIBMTP_track_t * const metadata,
Linus Walleijee73ef22006-08-27 19:56:00 +00005496 LIBMTP_progressfunc_t const callback,
Linus Walleijea68f1f2008-06-22 21:54:44 +00005497 void const * const data)
Linus Walleij394bbbe2006-02-22 16:10:53 +00005498{
5499 int fd;
5500 int ret;
5501
5502 // Sanity check
5503 if (path == NULL) {
Linus Walleij070e9b42007-01-22 08:49:28 +00005504 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Send_Track_From_File(): Bad arguments, path was NULL.");
Linus Walleij394bbbe2006-02-22 16:10:53 +00005505 return -1;
5506 }
5507
5508 // Open file
5509#ifdef __WIN32__
Linus Walleij7beba572006-11-29 08:56:12 +00005510#ifdef USE_WINDOWS_IO_H
Linus Walleij3017c562010-10-17 21:31:36 +00005511 if ( (fd = _open(path, O_RDONLY|O_BINARY)) == -1 ) {
Linus Walleij7beba572006-11-29 08:56:12 +00005512#else
Linus Walleij3017c562010-10-17 21:31:36 +00005513 if ( (fd = open(path, O_RDONLY|O_BINARY)) == -1 ) {
Linus Walleij7beba572006-11-29 08:56:12 +00005514#endif
Linus Walleij394bbbe2006-02-22 16:10:53 +00005515#else
5516 if ( (fd = open(path, O_RDONLY)) == -1) {
5517#endif
nicklas79daadbf22009-09-28 18:19:34 +00005518 LIBMTP_ERROR("LIBMTP_Send_Track_From_File(): Could not open source file \"%s\"\n", path);
Linus Walleij394bbbe2006-02-22 16:10:53 +00005519 return -1;
5520 }
5521
Linus Walleijea68f1f2008-06-22 21:54:44 +00005522 ret = LIBMTP_Send_Track_From_File_Descriptor(device, fd, metadata, callback, data);
mopoke96143402006-10-30 04:37:26 +00005523
Linus Walleij394bbbe2006-02-22 16:10:53 +00005524 // Close file.
Linus Walleij7beba572006-11-29 08:56:12 +00005525#ifdef USE_WINDOWS_IO_H
5526 _close(fd);
5527#else
Linus Walleij394bbbe2006-02-22 16:10:53 +00005528 close(fd);
Linus Walleij7beba572006-11-29 08:56:12 +00005529#endif
Linus Walleij394bbbe2006-02-22 16:10:53 +00005530
5531 return ret;
5532}
5533
Linus Walleij094b4502009-09-22 22:28:33 +00005534
5535
5536/**
5537 * This helper function checks if a filename already exists on the device
5538 * @param PTPParams*
5539 * @param string representing the filename
5540 * @return 0 if the filename doesn't exist, -1 if it does
5541 */
5542static int check_filename_exists(PTPParams* params, char const * const filename)
5543{
5544 int i;
5545
5546 for (i = 0; i < params->nrofobjects; i++) {
5547 char *fname = params->objects[i].oi.Filename;
5548 if ((fname != NULL) && (strcmp(filename, fname) == 0))
5549 {
5550 return -1;
5551 }
5552 }
5553
5554 return 0;
5555}
5556
5557/**
5558 * This helper function returns a unique filename, with a random string before the extension
5559 * @param string representing the original filename
5560 * @return a string representing the unique filename
5561 */
5562static char *generate_unique_filename(PTPParams* params, char const * const filename)
5563{
5564 int suffix;
5565 char * extension_position;
5566
5567 if (check_filename_exists(params, filename))
5568 {
5569 extension_position = strrchr(filename,'.');
5570
5571 char basename[extension_position - filename + 1];
5572 strncpy(basename, filename, extension_position - filename);
5573 basename[extension_position - filename] = '\0';
Linus Walleij2c1bbd62009-11-07 15:15:03 +00005574
Linus Walleij094b4502009-09-22 22:28:33 +00005575 suffix = 1;
5576 char newname[ strlen(basename) + 6 + strlen(extension_position)];
5577 sprintf(newname, "%s_%d%s", basename, suffix, extension_position);
5578 while ((check_filename_exists(params, newname)) && (suffix < 1000000)) {
5579 suffix++;
5580 sprintf(newname, "%s_%d%s", basename, suffix, extension_position);
5581 }
5582 return strdup(newname);
5583 }
5584 else
5585 {
5586 return strdup(filename);
5587 }
5588}
5589
Linus Walleijfa1374c2006-02-27 07:41:46 +00005590/**
Linus Walleij394bbbe2006-02-22 16:10:53 +00005591 * This function sends a track from a file descriptor to an
5592 * MTP device. A filename and a set of metadata must be
5593 * given as input.
5594 * @param device a pointer to the device to send the track to.
5595 * @param fd the filedescriptor for a local file which will be sent.
5596 * @param metadata a track metadata set to be written along with the file.
Linus Walleijea68f1f2008-06-22 21:54:44 +00005597 * After this call the field <code>metadata-&gt;item_id</code>
5598 * will contain the new track ID. Other fields such
5599 * as the <code>metadata-&gt;filename</code>, <code>metadata-&gt;parent_id</code>
Linus Walleij2c1bbd62009-11-07 15:15:03 +00005600 * or <code>metadata-&gt;storage_id</code> may also change during this
Linus Walleijea68f1f2008-06-22 21:54:44 +00005601 * operation due to device restrictions, so do not rely on the
5602 * contents of this struct to be preserved in any way.
5603 * <ul>
Linus Walleij2c1bbd62009-11-07 15:15:03 +00005604 * <li><code>metadata-&gt;parent_id</code> should be set to the parent
5605 * (e.g. folder) to store this track in. Since some
Linus Walleijea68f1f2008-06-22 21:54:44 +00005606 * devices are a bit picky about where files
5607 * are placed, a default folder will be chosen if libmtp
5608 * has detected one for the current filetype and this
5609 * parameter is set to 0. If this is 0 and no default folder
5610 * can be found, the file will be stored in the root folder.
5611 * <li><code>metadata-&gt;storage_id</code> should be set to the
5612 * desired storage (e.g. memory card or whatever your device
5613 * presents) to store this track in. Setting this to 0 will store
5614 * the track on the primary storage.
5615 * </ul>
Linus Walleij394bbbe2006-02-22 16:10:53 +00005616 * @param callback a progress indicator function or NULL to ignore.
5617 * @param data a user-defined pointer that is passed along to
5618 * the <code>progress</code> function in order to
5619 * pass along some user defined data to the progress
5620 * updates. If not used, set this to NULL.
mopoke96143402006-10-30 04:37:26 +00005621 * @return 0 if the transfer was successful, any other value means
Linus Walleij394bbbe2006-02-22 16:10:53 +00005622 * failure.
5623 * @see LIBMTP_Send_Track_From_File()
Linus Walleij438bd7f2006-06-08 11:35:44 +00005624 * @see LIBMTP_Delete_Object()
Linus Walleij394bbbe2006-02-22 16:10:53 +00005625 */
mopoke96143402006-10-30 04:37:26 +00005626int LIBMTP_Send_Track_From_File_Descriptor(LIBMTP_mtpdevice_t *device,
Linus Walleij394bbbe2006-02-22 16:10:53 +00005627 int const fd, LIBMTP_track_t * const metadata,
Linus Walleijee73ef22006-08-27 19:56:00 +00005628 LIBMTP_progressfunc_t const callback,
Linus Walleijea68f1f2008-06-22 21:54:44 +00005629 void const * const data)
Linus Walleij394bbbe2006-02-22 16:10:53 +00005630{
Linus Walleij17e39f72006-02-23 15:54:28 +00005631 int subcall_ret;
Linus Walleijd2778d12007-08-06 19:24:58 +00005632 LIBMTP_file_t filedata;
Linus Walleij094b4502009-09-22 22:28:33 +00005633 PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo;
5634 PTPParams *params = (PTPParams *) device->params;
Linus Walleij8ae78bb2006-11-20 21:45:52 +00005635
Linus Walleij16c51f02006-05-04 13:20:22 +00005636 // Sanity check, is this really a track?
Linus Walleij10e0ce72008-05-04 19:20:33 +00005637 if (!LIBMTP_FILETYPE_IS_TRACK(metadata->filetype)) {
Linus Walleij2c1bbd62009-11-07 15:15:03 +00005638 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL,
Linus Walleij46651f32008-04-26 23:30:19 +00005639 "LIBMTP_Send_Track_From_File_Descriptor(): "
5640 "I don't think this is actually a track, strange filetype...");
Linus Walleij99310d42006-11-01 08:29:39 +00005641 }
Linus Walleij37253292006-10-11 08:38:14 +00005642
Linus Walleijd2778d12007-08-06 19:24:58 +00005643 // Wrap around the file transfer function
5644 filedata.item_id = metadata->item_id;
5645 filedata.parent_id = metadata->parent_id;
Linus Walleijea68f1f2008-06-22 21:54:44 +00005646 filedata.storage_id = metadata->storage_id;
Linus Walleij094b4502009-09-22 22:28:33 +00005647 if FLAG_UNIQUE_FILENAMES(ptp_usb) {
5648 filedata.filename = generate_unique_filename(params, metadata->filename);
5649 }
5650 else {
5651 filedata.filename = metadata->filename;
5652 }
Linus Walleijd2778d12007-08-06 19:24:58 +00005653 filedata.filesize = metadata->filesize;
5654 filedata.filetype = metadata->filetype;
5655 filedata.next = NULL;
Linus Walleij37253292006-10-11 08:38:14 +00005656
Linus Walleijd2778d12007-08-06 19:24:58 +00005657 subcall_ret = LIBMTP_Send_File_From_File_Descriptor(device,
Linus Walleij2c1bbd62009-11-07 15:15:03 +00005658 fd,
Linus Walleijd2778d12007-08-06 19:24:58 +00005659 &filedata,
5660 callback,
Linus Walleijea68f1f2008-06-22 21:54:44 +00005661 data);
mopoke31364442006-11-20 04:53:04 +00005662
Linus Walleijd2778d12007-08-06 19:24:58 +00005663 if (subcall_ret != 0) {
Linus Walleij2c1bbd62009-11-07 15:15:03 +00005664 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL,
Linus Walleij46651f32008-04-26 23:30:19 +00005665 "LIBMTP_Send_Track_From_File_Descriptor(): "
Linus Walleijd2778d12007-08-06 19:24:58 +00005666 "subcall to LIBMTP_Send_File_From_File_Descriptor failed.");
5667 // We used to delete the file here, but don't... It might be OK after all.
5668 // (void) LIBMTP_Delete_Object(device, metadata->item_id);
Linus Walleij394bbbe2006-02-22 16:10:53 +00005669 return -1;
5670 }
Linus Walleij2c1bbd62009-11-07 15:15:03 +00005671
Linus Walleijea68f1f2008-06-22 21:54:44 +00005672 // Pick up new item (and parent, storage) ID
Linus Walleijd2778d12007-08-06 19:24:58 +00005673 metadata->item_id = filedata.item_id;
5674 metadata->parent_id = filedata.parent_id;
Linus Walleijea68f1f2008-06-22 21:54:44 +00005675 metadata->storage_id = filedata.storage_id;
mopoke96143402006-10-30 04:37:26 +00005676
Linus Walleij17e39f72006-02-23 15:54:28 +00005677 // Set track metadata for the new fine track
5678 subcall_ret = LIBMTP_Update_Track_Metadata(device, metadata);
5679 if (subcall_ret != 0) {
Linus Walleij070e9b42007-01-22 08:49:28 +00005680 // Subcall will add error to errorstack
Linus Walleijb2753182007-02-26 08:11:38 +00005681 // We used to delete the file here, but don't... It might be OK after all.
5682 // (void) LIBMTP_Delete_Object(device, metadata->item_id);
Linus Walleij17e39f72006-02-23 15:54:28 +00005683 return -1;
5684 }
Linus Walleij438bd7f2006-06-08 11:35:44 +00005685
Richard Low61edc1a2007-09-23 10:35:48 +00005686 // note we don't need to update the cache here because LIBMTP_Send_File_From_File_Descriptor
5687 // has added the object handle and LIBMTP_Update_Track_Metadata has added the metadata.
mopoke96143402006-10-30 04:37:26 +00005688
Linus Walleij17e39f72006-02-23 15:54:28 +00005689 return 0;
5690}
5691
Linus Walleijd6a49972006-05-02 08:24:54 +00005692/**
Richard Lowd3b17022009-04-11 12:37:39 +00005693 * This function sends a track from a handler function to an
5694 * MTP device. A filename and a set of metadata must be
5695 * given as input.
5696 * @param device a pointer to the device to send the track to.
5697 * @param get_func the function to call when we have data.
5698 * @param priv the user-defined pointer that is passed to
5699 * <code>get_func</code>.
5700 * @param metadata a track metadata set to be written along with the file.
5701 * After this call the field <code>metadata-&gt;item_id</code>
5702 * will contain the new track ID. Other fields such
5703 * as the <code>metadata-&gt;filename</code>, <code>metadata-&gt;parent_id</code>
Linus Walleij2c1bbd62009-11-07 15:15:03 +00005704 * or <code>metadata-&gt;storage_id</code> may also change during this
Richard Lowd3b17022009-04-11 12:37:39 +00005705 * operation due to device restrictions, so do not rely on the
5706 * contents of this struct to be preserved in any way.
5707 * <ul>
Linus Walleij2c1bbd62009-11-07 15:15:03 +00005708 * <li><code>metadata-&gt;parent_id</code> should be set to the parent
5709 * (e.g. folder) to store this track in. Since some
Richard Lowd3b17022009-04-11 12:37:39 +00005710 * devices are a bit picky about where files
5711 * are placed, a default folder will be chosen if libmtp
5712 * has detected one for the current filetype and this
5713 * parameter is set to 0. If this is 0 and no default folder
5714 * can be found, the file will be stored in the root folder.
5715 * <li><code>metadata-&gt;storage_id</code> should be set to the
5716 * desired storage (e.g. memory card or whatever your device
5717 * presents) to store this track in. Setting this to 0 will store
5718 * the track on the primary storage.
5719 * </ul>
5720 * @param callback a progress indicator function or NULL to ignore.
5721 * @param data a user-defined pointer that is passed along to
5722 * the <code>progress</code> function in order to
5723 * pass along some user defined data to the progress
5724 * updates. If not used, set this to NULL.
5725 * @return 0 if the transfer was successful, any other value means
5726 * failure.
5727 * @see LIBMTP_Send_Track_From_File()
5728 * @see LIBMTP_Delete_Object()
5729 */
5730int LIBMTP_Send_Track_From_Handler(LIBMTP_mtpdevice_t *device,
5731 MTPDataGetFunc get_func, void * priv, LIBMTP_track_t * const metadata,
5732 LIBMTP_progressfunc_t const callback,
5733 void const * const data)
5734{
5735 int subcall_ret;
5736 LIBMTP_file_t filedata;
Linus Walleij094b4502009-09-22 22:28:33 +00005737 PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo;
5738 PTPParams *params = (PTPParams *) device->params;
Richard Lowd3b17022009-04-11 12:37:39 +00005739
5740 // Sanity check, is this really a track?
5741 if (!LIBMTP_FILETYPE_IS_TRACK(metadata->filetype)) {
Linus Walleij2c1bbd62009-11-07 15:15:03 +00005742 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL,
Richard Lowd3b17022009-04-11 12:37:39 +00005743 "LIBMTP_Send_Track_From_Handler(): "
5744 "I don't think this is actually a track, strange filetype...");
5745 }
5746
5747 // Wrap around the file transfer function
5748 filedata.item_id = metadata->item_id;
5749 filedata.parent_id = metadata->parent_id;
5750 filedata.storage_id = metadata->storage_id;
Linus Walleij094b4502009-09-22 22:28:33 +00005751 if FLAG_UNIQUE_FILENAMES(ptp_usb) {
5752 filedata.filename = generate_unique_filename(params, metadata->filename);
5753 }
5754 else {
5755 filedata.filename = metadata->filename;
5756 }
Richard Lowd3b17022009-04-11 12:37:39 +00005757 filedata.filesize = metadata->filesize;
5758 filedata.filetype = metadata->filetype;
5759 filedata.next = NULL;
5760
5761 subcall_ret = LIBMTP_Send_File_From_Handler(device,
Linus Walleij2c1bbd62009-11-07 15:15:03 +00005762 get_func,
5763 priv,
5764 &filedata,
5765 callback,
5766 data);
Richard Lowd3b17022009-04-11 12:37:39 +00005767
5768 if (subcall_ret != 0) {
Linus Walleij2c1bbd62009-11-07 15:15:03 +00005769 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL,
Richard Lowd3b17022009-04-11 12:37:39 +00005770 "LIBMTP_Send_Track_From_Handler(): "
5771 "subcall to LIBMTP_Send_File_From_Handler failed.");
5772 // We used to delete the file here, but don't... It might be OK after all.
5773 // (void) LIBMTP_Delete_Object(device, metadata->item_id);
5774 return -1;
5775 }
Linus Walleijd8fd9a52009-09-09 22:46:01 +00005776
Richard Lowd3b17022009-04-11 12:37:39 +00005777 // Pick up new item (and parent, storage) ID
5778 metadata->item_id = filedata.item_id;
5779 metadata->parent_id = filedata.parent_id;
5780 metadata->storage_id = filedata.storage_id;
5781
5782 // Set track metadata for the new fine track
5783 subcall_ret = LIBMTP_Update_Track_Metadata(device, metadata);
5784 if (subcall_ret != 0) {
5785 // Subcall will add error to errorstack
5786 // We used to delete the file here, but don't... It might be OK after all.
5787 // (void) LIBMTP_Delete_Object(device, metadata->item_id);
5788 return -1;
5789 }
5790
5791 // note we don't need to update the cache here because LIBMTP_Send_File_From_File_Descriptor
5792 // has added the object handle and LIBMTP_Update_Track_Metadata has added the metadata.
5793
5794 return 0;
5795}
5796
5797/**
mopoke96143402006-10-30 04:37:26 +00005798 * This function sends a local file to an MTP device.
Linus Walleijd6a49972006-05-02 08:24:54 +00005799 * A filename and a set of metadata must be
5800 * given as input.
5801 * @param device a pointer to the device to send the track to.
5802 * @param path the filename of a local file which will be sent.
Linus Walleijea68f1f2008-06-22 21:54:44 +00005803 * @param filedata a file metadata set to be written along with the file.
5804 * After this call the field <code>filedata-&gt;item_id</code>
5805 * will contain the new file ID. Other fields such
5806 * as the <code>filedata-&gt;filename</code>, <code>filedata-&gt;parent_id</code>
Linus Walleijd8fd9a52009-09-09 22:46:01 +00005807 * or <code>filedata-&gt;storage_id</code> may also change during this
Linus Walleijea68f1f2008-06-22 21:54:44 +00005808 * operation due to device restrictions, so do not rely on the
5809 * contents of this struct to be preserved in any way.
5810 * <ul>
Linus Walleijd8fd9a52009-09-09 22:46:01 +00005811 * <li><code>filedata-&gt;parent_id</code> should be set to the parent
5812 * (e.g. folder) to store this file in. If this is 0,
Linus Walleijea68f1f2008-06-22 21:54:44 +00005813 * the file will be stored in the root folder.
5814 * <li><code>filedata-&gt;storage_id</code> should be set to the
5815 * desired storage (e.g. memory card or whatever your device
5816 * presents) to store this file in. Setting this to 0 will store
5817 * the file on the primary storage.
5818 * </ul>
Linus Walleijd6a49972006-05-02 08:24:54 +00005819 * @param callback a progress indicator function or NULL to ignore.
5820 * @param data a user-defined pointer that is passed along to
5821 * the <code>progress</code> function in order to
5822 * pass along some user defined data to the progress
5823 * updates. If not used, set this to NULL.
mopoke96143402006-10-30 04:37:26 +00005824 * @return 0 if the transfer was successful, any other value means
Linus Walleijd6a49972006-05-02 08:24:54 +00005825 * failure.
5826 * @see LIBMTP_Send_File_From_File_Descriptor()
Linus Walleij438bd7f2006-06-08 11:35:44 +00005827 * @see LIBMTP_Delete_Object()
Linus Walleijd6a49972006-05-02 08:24:54 +00005828 */
mopoke96143402006-10-30 04:37:26 +00005829int LIBMTP_Send_File_From_File(LIBMTP_mtpdevice_t *device,
Linus Walleijd6a49972006-05-02 08:24:54 +00005830 char const * const path, LIBMTP_file_t * const filedata,
Linus Walleijee73ef22006-08-27 19:56:00 +00005831 LIBMTP_progressfunc_t const callback,
Linus Walleijea68f1f2008-06-22 21:54:44 +00005832 void const * const data)
Linus Walleijd6a49972006-05-02 08:24:54 +00005833{
5834 int fd;
5835 int ret;
5836
5837 // Sanity check
5838 if (path == NULL) {
Linus Walleij070e9b42007-01-22 08:49:28 +00005839 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Send_File_From_File(): Bad arguments, path was NULL.");
Linus Walleijd6a49972006-05-02 08:24:54 +00005840 return -1;
5841 }
5842
5843 // Open file
5844#ifdef __WIN32__
Linus Walleij7beba572006-11-29 08:56:12 +00005845#ifdef USE_WINDOWS_IO_H
Linus Walleij3017c562010-10-17 21:31:36 +00005846 if ( (fd = _open(path, O_RDONLY|O_BINARY)) == -1 ) {
Linus Walleij7beba572006-11-29 08:56:12 +00005847#else
Linus Walleij3017c562010-10-17 21:31:36 +00005848 if ( (fd = open(path, O_RDONLY|O_BINARY)) == -1 ) {
Linus Walleij7beba572006-11-29 08:56:12 +00005849#endif
Linus Walleijd6a49972006-05-02 08:24:54 +00005850#else
5851 if ( (fd = open(path, O_RDONLY)) == -1) {
5852#endif
Linus Walleij070e9b42007-01-22 08:49:28 +00005853 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Send_File_From_File(): Could not open source file.");
Linus Walleijd6a49972006-05-02 08:24:54 +00005854 return -1;
5855 }
5856
Linus Walleijea68f1f2008-06-22 21:54:44 +00005857 ret = LIBMTP_Send_File_From_File_Descriptor(device, fd, filedata, callback, data);
mopoke96143402006-10-30 04:37:26 +00005858
Linus Walleijd6a49972006-05-02 08:24:54 +00005859 // Close file.
Linus Walleij7beba572006-11-29 08:56:12 +00005860#ifdef USE_WINDOWS_IO_H
5861 _close(fd);
5862#else
Linus Walleijd6a49972006-05-02 08:24:54 +00005863 close(fd);
Linus Walleij7beba572006-11-29 08:56:12 +00005864#endif
Linus Walleijd6a49972006-05-02 08:24:54 +00005865
5866 return ret;
5867}
5868
Linus Walleijd208f9c2006-04-27 14:16:06 +00005869/**
5870 * This function sends a generic file from a file descriptor to an
5871 * MTP device. A filename and a set of metadata must be
5872 * given as input.
Linus Walleijcd3eb3d2006-09-02 21:33:22 +00005873 *
5874 * This can potentially be used for sending in a stream of unknown
Linus Walleijd8fd9a52009-09-09 22:46:01 +00005875 * length. Send music files with
Linus Walleij1b87ea72007-08-28 07:53:09 +00005876 * <code>LIBMTP_Send_Track_From_File_Descriptor()</code>
Linus Walleijcd3eb3d2006-09-02 21:33:22 +00005877 *
Linus Walleijd208f9c2006-04-27 14:16:06 +00005878 * @param device a pointer to the device to send the file to.
5879 * @param fd the filedescriptor for a local file which will be sent.
Linus Walleijea68f1f2008-06-22 21:54:44 +00005880 * @param filedata a file metadata set to be written along with the file.
5881 * After this call the field <code>filedata-&gt;item_id</code>
5882 * will contain the new file ID. Other fields such
5883 * as the <code>filedata-&gt;filename</code>, <code>filedata-&gt;parent_id</code>
Linus Walleijd8fd9a52009-09-09 22:46:01 +00005884 * or <code>filedata-&gt;storage_id</code> may also change during this
Linus Walleijea68f1f2008-06-22 21:54:44 +00005885 * operation due to device restrictions, so do not rely on the
5886 * contents of this struct to be preserved in any way.
5887 * <ul>
Linus Walleijd8fd9a52009-09-09 22:46:01 +00005888 * <li><code>filedata-&gt;parent_id</code> should be set to the parent
5889 * (e.g. folder) to store this file in. If this is 0,
Linus Walleijea68f1f2008-06-22 21:54:44 +00005890 * the file will be stored in the root folder.
5891 * <li><code>filedata-&gt;storage_id</code> should be set to the
5892 * desired storage (e.g. memory card or whatever your device
5893 * presents) to store this file in. Setting this to 0 will store
5894 * the file on the primary storage.
5895 * </ul>
Linus Walleijd208f9c2006-04-27 14:16:06 +00005896 * @param callback a progress indicator function or NULL to ignore.
5897 * @param data a user-defined pointer that is passed along to
5898 * the <code>progress</code> function in order to
5899 * pass along some user defined data to the progress
5900 * updates. If not used, set this to NULL.
mopoke96143402006-10-30 04:37:26 +00005901 * @return 0 if the transfer was successful, any other value means
Linus Walleijd208f9c2006-04-27 14:16:06 +00005902 * failure.
Linus Walleijd6a49972006-05-02 08:24:54 +00005903 * @see LIBMTP_Send_File_From_File()
Linus Walleij1b87ea72007-08-28 07:53:09 +00005904 * @see LIBMTP_Send_Track_From_File_Descriptor()
Linus Walleij438bd7f2006-06-08 11:35:44 +00005905 * @see LIBMTP_Delete_Object()
Linus Walleijd208f9c2006-04-27 14:16:06 +00005906 */
mopoke96143402006-10-30 04:37:26 +00005907int LIBMTP_Send_File_From_File_Descriptor(LIBMTP_mtpdevice_t *device,
Linus Walleijd208f9c2006-04-27 14:16:06 +00005908 int const fd, LIBMTP_file_t * const filedata,
Linus Walleijee73ef22006-08-27 19:56:00 +00005909 LIBMTP_progressfunc_t const callback,
Linus Walleijea68f1f2008-06-22 21:54:44 +00005910 void const * const data)
Linus Walleijd208f9c2006-04-27 14:16:06 +00005911{
5912 uint16_t ret;
Linus Walleijd208f9c2006-04-27 14:16:06 +00005913 PTPParams *params = (PTPParams *) device->params;
Linus Walleijd214b9b2006-08-26 22:08:37 +00005914 PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo;
Linus Walleij817579a2008-05-23 21:21:40 +00005915 LIBMTP_file_t *newfilemeta;
Linus Walleije04a1b92011-03-09 18:00:24 +01005916 int oldtimeout;
5917 int timeout;
Richard Lowab0d22d2006-11-30 22:17:49 +00005918
Richard Lowd3b17022009-04-11 12:37:39 +00005919 if (send_file_object_info(device, filedata))
5920 {
5921 // no need to output an error since send_file_object_info will already have done so
Linus Walleij07795772007-08-06 18:44:06 +00005922 return -1;
Linus Walleijcd3eb3d2006-09-02 21:33:22 +00005923 }
Linus Walleij07795772007-08-06 18:44:06 +00005924
Richard Lowd3b17022009-04-11 12:37:39 +00005925 // Callbacks
5926 ptp_usb->callback_active = 1;
5927 // The callback will deactivate itself after this amount of data has been sent
5928 // One BULK header for the request, one for the data phase. No parameters to the request.
5929 ptp_usb->current_transfer_total = filedata->filesize+PTP_USB_BULK_HDR_LEN*2;
5930 ptp_usb->current_transfer_complete = 0;
5931 ptp_usb->current_transfer_callback = callback;
5932 ptp_usb->current_transfer_callback_data = data;
Linus Walleijd8fd9a52009-09-09 22:46:01 +00005933
Linus Walleije04a1b92011-03-09 18:00:24 +01005934 /*
5935 * We might need to increase the timeout here, files can be pretty
5936 * large. Take the default timeout and add the calculated time for
5937 * this transfer
5938 */
5939 get_usb_device_timeout(ptp_usb, &oldtimeout);
5940 timeout = oldtimeout +
5941 (ptp_usb->current_transfer_total / guess_usb_speed(ptp_usb)) * 1000;
5942 set_usb_device_timeout(ptp_usb, timeout);
5943
Richard Lowd3b17022009-04-11 12:37:39 +00005944 ret = ptp_sendobject_fromfd(params, fd, filedata->filesize);
Linus Walleijd8fd9a52009-09-09 22:46:01 +00005945
Richard Lowd3b17022009-04-11 12:37:39 +00005946 ptp_usb->callback_active = 0;
5947 ptp_usb->current_transfer_callback = NULL;
5948 ptp_usb->current_transfer_callback_data = NULL;
Linus Walleije04a1b92011-03-09 18:00:24 +01005949 set_usb_device_timeout(ptp_usb, oldtimeout);
Richard Lowd3b17022009-04-11 12:37:39 +00005950
5951 if (ret == PTP_ERROR_CANCEL) {
5952 add_error_to_errorstack(device, LIBMTP_ERROR_CANCELLED, "LIBMTP_Send_File_From_File_Descriptor(): Cancelled transfer.");
5953 return -1;
5954 }
5955 if (ret != PTP_RC_OK) {
5956 add_ptp_error_to_errorstack(device, ret, "LIBMTP_Send_File_From_File_Descriptor(): "
5957 "Could not send object.");
5958 return -1;
5959 }
5960
5961 add_object_to_cache(device, filedata->item_id);
Linus Walleijd8fd9a52009-09-09 22:46:01 +00005962
Richard Lowd3b17022009-04-11 12:37:39 +00005963 /*
Linus Walleij4fc19172010-01-19 00:10:26 +00005964 * Get the device-assigned parent_id from the cache.
Richard Lowd3b17022009-04-11 12:37:39 +00005965 * The operation that adds it to the cache will
5966 * look it up from the device, so we get the new
5967 * parent_id from the cache.
5968 */
5969 newfilemeta = LIBMTP_Get_Filemetadata(device, filedata->item_id);
5970 if (newfilemeta != NULL) {
5971 filedata->parent_id = newfilemeta->parent_id;
5972 filedata->storage_id = newfilemeta->storage_id;
5973 LIBMTP_destroy_file_t(newfilemeta);
5974 } else {
5975 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL,
5976 "LIBMTP_Send_File_From_File_Descriptor(): "
5977 "Could not retrieve updated metadata.");
5978 return -1;
5979 }
5980
5981 return 0;
5982}
5983
5984/**
5985 * This function sends a generic file from a handler function to an
5986 * MTP device. A filename and a set of metadata must be
5987 * given as input.
5988 *
5989 * This can potentially be used for sending in a stream of unknown
Linus Walleijd8fd9a52009-09-09 22:46:01 +00005990 * length. Send music files with
Richard Lowd3b17022009-04-11 12:37:39 +00005991 * <code>LIBMTP_Send_Track_From_Handler()</code>
5992 *
5993 * @param device a pointer to the device to send the file to.
5994 * @param get_func the function to call to get data to write
5995 * @param priv a user-defined pointer that is passed along to
5996 * <code>get_func</code>. If not used, this is set to NULL.
5997 * @param filedata a file metadata set to be written along with the file.
5998 * After this call the field <code>filedata-&gt;item_id</code>
5999 * will contain the new file ID. Other fields such
6000 * as the <code>filedata-&gt;filename</code>, <code>filedata-&gt;parent_id</code>
Linus Walleijd8fd9a52009-09-09 22:46:01 +00006001 * or <code>filedata-&gt;storage_id</code> may also change during this
Richard Lowd3b17022009-04-11 12:37:39 +00006002 * operation due to device restrictions, so do not rely on the
6003 * contents of this struct to be preserved in any way.
6004 * <ul>
Linus Walleijd8fd9a52009-09-09 22:46:01 +00006005 * <li><code>filedata-&gt;parent_id</code> should be set to the parent
6006 * (e.g. folder) to store this file in. If this is 0,
Richard Lowd3b17022009-04-11 12:37:39 +00006007 * the file will be stored in the root folder.
6008 * <li><code>filedata-&gt;storage_id</code> should be set to the
6009 * desired storage (e.g. memory card or whatever your device
6010 * presents) to store this file in. Setting this to 0 will store
6011 * the file on the primary storage.
6012 * </ul>
6013 * @param callback a progress indicator function or NULL to ignore.
6014 * @param data a user-defined pointer that is passed along to
6015 * the <code>progress</code> function in order to
6016 * pass along some user defined data to the progress
6017 * updates. If not used, set this to NULL.
6018 * @return 0 if the transfer was successful, any other value means
6019 * failure.
6020 * @see LIBMTP_Send_File_From_File()
6021 * @see LIBMTP_Send_Track_From_File_Descriptor()
6022 * @see LIBMTP_Delete_Object()
6023 */
6024int LIBMTP_Send_File_From_Handler(LIBMTP_mtpdevice_t *device,
6025 MTPDataGetFunc get_func, void * priv, LIBMTP_file_t * const filedata,
6026 LIBMTP_progressfunc_t const callback, void const * const data)
6027{
6028 uint16_t ret;
6029 PTPParams *params = (PTPParams *) device->params;
6030 PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo;
6031 LIBMTP_file_t *newfilemeta;
6032
6033 if (send_file_object_info(device, filedata))
6034 {
6035 // no need to output an error since send_file_object_info will already have done so
6036 return -1;
6037 }
Linus Walleijd866d242009-08-23 21:50:39 +00006038
Richard Lowd3b17022009-04-11 12:37:39 +00006039 // Callbacks
6040 ptp_usb->callback_active = 1;
6041 // The callback will deactivate itself after this amount of data has been sent
6042 // One BULK header for the request, one for the data phase. No parameters to the request.
6043 ptp_usb->current_transfer_total = filedata->filesize+PTP_USB_BULK_HDR_LEN*2;
6044 ptp_usb->current_transfer_complete = 0;
6045 ptp_usb->current_transfer_callback = callback;
6046 ptp_usb->current_transfer_callback_data = data;
Linus Walleijd866d242009-08-23 21:50:39 +00006047
Richard Low5b4023c2009-04-16 19:14:38 +00006048 MTPDataHandler mtp_handler;
6049 mtp_handler.getfunc = get_func;
6050 mtp_handler.putfunc = NULL;
Linus Walleijd866d242009-08-23 21:50:39 +00006051 mtp_handler.priv = priv;
6052
Richard Lowd3b17022009-04-11 12:37:39 +00006053 PTPDataHandler handler;
Richard Low5b4023c2009-04-16 19:14:38 +00006054 handler.getfunc = get_func_wrapper;
Richard Lowd3b17022009-04-11 12:37:39 +00006055 handler.putfunc = NULL;
Linus Walleijd866d242009-08-23 21:50:39 +00006056 handler.priv = &mtp_handler;
6057
Richard Lowd3b17022009-04-11 12:37:39 +00006058 ret = ptp_sendobject_from_handler(params, &handler, filedata->filesize);
Linus Walleijd866d242009-08-23 21:50:39 +00006059
Richard Lowd3b17022009-04-11 12:37:39 +00006060 ptp_usb->callback_active = 0;
6061 ptp_usb->current_transfer_callback = NULL;
6062 ptp_usb->current_transfer_callback_data = NULL;
6063
6064 if (ret == PTP_ERROR_CANCEL) {
6065 add_error_to_errorstack(device, LIBMTP_ERROR_CANCELLED, "LIBMTP_Send_File_From_Handler(): Cancelled transfer.");
6066 return -1;
6067 }
6068 if (ret != PTP_RC_OK) {
6069 add_ptp_error_to_errorstack(device, ret, "LIBMTP_Send_File_From_Handler(): "
6070 "Could not send object.");
6071 return -1;
6072 }
6073
6074 add_object_to_cache(device, filedata->item_id);
Linus Walleijd866d242009-08-23 21:50:39 +00006075
Richard Lowd3b17022009-04-11 12:37:39 +00006076 /*
6077 * Get the device-assined parent_id from the cache.
6078 * The operation that adds it to the cache will
6079 * look it up from the device, so we get the new
6080 * parent_id from the cache.
6081 */
6082 newfilemeta = LIBMTP_Get_Filemetadata(device, filedata->item_id);
6083 if (newfilemeta != NULL) {
6084 filedata->parent_id = newfilemeta->parent_id;
6085 filedata->storage_id = newfilemeta->storage_id;
6086 LIBMTP_destroy_file_t(newfilemeta);
6087 } else {
6088 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL,
6089 "LIBMTP_Send_File_From_Handler(): "
6090 "Could not retrieve updated metadata.");
6091 return -1;
6092 }
6093
6094 return 0;
6095}
6096
Linus Walleijd866d242009-08-23 21:50:39 +00006097/**
Richard Lowd3b17022009-04-11 12:37:39 +00006098 * This function sends the file object info, ready for sendobject
6099 * @param device a pointer to the device to send the file to.
6100 * @param filedata a file metadata set to be written along with the file.
6101 * @return 0 if the transfer was successful, any other value means
6102 * failure.
6103 */
6104static int send_file_object_info(LIBMTP_mtpdevice_t *device, LIBMTP_file_t *filedata)
6105{
6106 PTPParams *params = (PTPParams *) device->params;
6107 PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo;
6108 uint32_t store;
6109 int use_primary_storage = 1;
6110 uint16_t of = map_libmtp_type_to_ptp_type(filedata->filetype);
6111 LIBMTP_devicestorage_t *storage;
6112 uint32_t localph = filedata->parent_id;
6113 uint16_t ret;
6114 int i;
Linus Walleij08a5fe12009-09-08 21:28:58 +00006115
Linus Walleij90143282011-03-02 00:24:15 +01006116#if 0
6117 // Sanity check: no zerolength files on some devices?
6118 // If the zerolength files cause problems on some devices,
6119 // then add a bug flag for this.
Richard Lowd3b17022009-04-11 12:37:39 +00006120 if (filedata->filesize == 0) {
6121 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "send_file_object_info(): "
6122 "File of zero size.");
6123 return -1;
6124 }
Linus Walleij90143282011-03-02 00:24:15 +01006125#endif
Linus Walleijea68f1f2008-06-22 21:54:44 +00006126 if (filedata->storage_id != 0) {
6127 store = filedata->storage_id;
Linus Walleijfb28b632007-10-23 21:56:18 +00006128 } else {
Linus Walleij88babc82014-06-02 21:32:14 +02006129 store = get_suggested_storage_id(device, filedata->filesize, localph);
Linus Walleijea68f1f2008-06-22 21:54:44 +00006130 }
Linus Walleij88babc82014-06-02 21:32:14 +02006131
Linus Walleijea68f1f2008-06-22 21:54:44 +00006132 // Detect if something non-primary is in use.
6133 storage = device->storage;
Richard Low4d9165f2008-09-23 20:13:17 +00006134 if (storage != NULL && store != storage->id) {
Linus Walleijea68f1f2008-06-22 21:54:44 +00006135 use_primary_storage = 0;
Linus Walleij6bf68b42007-09-03 22:50:02 +00006136 }
Linus Walleijd208f9c2006-04-27 14:16:06 +00006137
mopoke96143402006-10-30 04:37:26 +00006138 /*
Linus Walleij05ccbe72006-06-13 07:46:58 +00006139 * If no destination folder was given, look up a default
6140 * folder if possible. Perhaps there is some way of retrieveing
6141 * the default folder for different forms of content, what
6142 * do I know, we use a fixed list in lack of any better method.
6143 * Some devices obviously need to have their files in certain
mopoke96143402006-10-30 04:37:26 +00006144 * folders in order to find/display them at all (hello Creative),
Linus Walleijc40c9bf2008-06-13 23:24:17 +00006145 * so we have to have a method for this. We only do this if the
6146 * primary storage is in use.
Linus Walleij05ccbe72006-06-13 07:46:58 +00006147 */
6148
Linus Walleijc40c9bf2008-06-13 23:24:17 +00006149 if (localph == 0 && use_primary_storage) {
Linus Walleij10e0ce72008-05-04 19:20:33 +00006150 if (LIBMTP_FILETYPE_IS_AUDIO(filedata->filetype)) {
Linus Walleij05ccbe72006-06-13 07:46:58 +00006151 localph = device->default_music_folder;
Linus Walleij10e0ce72008-05-04 19:20:33 +00006152 } else if (LIBMTP_FILETYPE_IS_VIDEO(filedata->filetype)) {
Linus Walleij05ccbe72006-06-13 07:46:58 +00006153 localph = device->default_video_folder;
6154 } else if (of == PTP_OFC_EXIF_JPEG ||
Linus Walleij5fb47132006-12-30 15:35:48 +00006155 of == PTP_OFC_JP2 ||
6156 of == PTP_OFC_JPX ||
Linus Walleij05ccbe72006-06-13 07:46:58 +00006157 of == PTP_OFC_JFIF ||
6158 of == PTP_OFC_TIFF ||
Linus Walleij5fb47132006-12-30 15:35:48 +00006159 of == PTP_OFC_TIFF_IT ||
Linus Walleij05ccbe72006-06-13 07:46:58 +00006160 of == PTP_OFC_BMP ||
6161 of == PTP_OFC_GIF ||
6162 of == PTP_OFC_PICT ||
6163 of == PTP_OFC_PNG ||
6164 of == PTP_OFC_MTP_WindowsImageFormat) {
6165 localph = device->default_picture_folder;
6166 } else if (of == PTP_OFC_MTP_vCalendar1 ||
Linus Walleijd7aa5b22006-09-02 11:52:31 +00006167 of == PTP_OFC_MTP_vCalendar2 ||
6168 of == PTP_OFC_MTP_UndefinedContact ||
6169 of == PTP_OFC_MTP_vCard2 ||
6170 of == PTP_OFC_MTP_vCard3 ||
6171 of == PTP_OFC_MTP_UndefinedCalendarItem) {
Linus Walleij05ccbe72006-06-13 07:46:58 +00006172 localph = device->default_organizer_folder;
Linus Walleij46651f32008-04-26 23:30:19 +00006173 } else if (of == PTP_OFC_Text) {
Linus Walleij9316e652006-12-07 09:55:21 +00006174 localph = device->default_text_folder;
Linus Walleij05ccbe72006-06-13 07:46:58 +00006175 }
6176 }
mopoke31364442006-11-20 04:53:04 +00006177
Linus Walleija3544f62007-11-30 01:20:04 +00006178 // Here we wire the type to unknown on bugged, but
Linus Walleij89bb1cd2009-07-24 21:03:36 +00006179 // Ogg or FLAC-supportive devices.
Linus Walleijfec4d562008-06-01 22:30:36 +00006180 if (FLAG_OGG_IS_UNKNOWN(ptp_usb) && of == PTP_OFC_MTP_OGG) {
Linus Walleija3544f62007-11-30 01:20:04 +00006181 of = PTP_OFC_Undefined;
6182 }
Linus Walleij89bb1cd2009-07-24 21:03:36 +00006183 if (FLAG_FLAC_IS_UNKNOWN(ptp_usb) && of == PTP_OFC_MTP_FLAC) {
6184 of = PTP_OFC_Undefined;
6185 }
Linus Walleija3544f62007-11-30 01:20:04 +00006186
Linus Walleijf67c1ad2009-01-14 21:39:50 +00006187 if (ptp_operation_issupported(params, PTP_OC_MTP_SendObjectPropList) &&
6188 !FLAG_BROKEN_SEND_OBJECT_PROPLIST(ptp_usb)) {
Linus Walleijd2778d12007-08-06 19:24:58 +00006189 /*
6190 * MTP enhanched does it this way (from a sniff):
6191 * -> PTP_OC_MTP_SendObjectPropList (0x9808):
6192 * 20 00 00 00 01 00 08 98 1B 00 00 00 01 00 01 00
6193 * FF FF FF FF 00 30 00 00 00 00 00 00 12 5E 00 00
6194 * Length: 0x00000020
6195 * Type: 0x0001 PTP_USB_CONTAINER_COMMAND
6196 * Code: 0x9808
6197 * Transaction ID: 0x0000001B
6198 * Param1: 0x00010001 <- store
6199 * Param2: 0xffffffff <- parent handle (-1 ?)
6200 * Param3: 0x00003000 <- file type PTP_OFC_Undefined - we don't know about PDF files
6201 * Param4: 0x00000000 <- file length MSB (-0x0c header len)
6202 * Param5: 0x00005e12 <- file length LSB (-0x0c header len)
6203 *
6204 * -> PTP_OC_MTP_SendObjectPropList (0x9808):
6205 * 46 00 00 00 02 00 08 98 1B 00 00 00 03 00 00 00
6206 * 00 00 00 00 07 DC FF FF 0D 4B 00 53 00 30 00 36 - dc07 = file name
6207 * 00 30 00 33 00 30 00 36 00 2E 00 70 00 64 00 66
6208 * 00 00 00 00 00 00 00 03 DC 04 00 00 00 00 00 00 - dc03 = protection status
6209 * 00 4F DC 02 00 01 - dc4f = non consumable
6210 * Length: 0x00000046
6211 * Type: 0x0002 PTP_USB_CONTAINER_DATA
6212 * Code: 0x9808
6213 * Transaction ID: 0x0000001B
6214 * Metadata....
6215 * 0x00000003 <- Number of metadata items
6216 * 0x00000000 <- Object handle, set to 0x00000000 since it is unknown!
6217 * 0xdc07 <- metadata type: file name
6218 * 0xffff <- metadata type: string
6219 * 0x0d <- number of (uint16_t) characters
6220 * 4b 53 30 36 30 33 30 36 2e 50 64 66 00 "KS060306.pdf", null terminated
6221 * 0x00000000 <- Object handle, set to 0x00000000 since it is unknown!
6222 * 0xdc03 <- metadata type: protection status
6223 * 0x0004 <- metadata type: uint16_t
6224 * 0x0000 <- not protected
6225 * 0x00000000 <- Object handle, set to 0x00000000 since it is unknown!
6226 * 0xdc4f <- non consumable
6227 * 0x0002 <- metadata type: uint8_t
6228 * 0x01 <- non-consumable (this device cannot display PDF)
6229 *
6230 * <- Read 0x18 bytes back
6231 * 18 00 00 00 03 00 01 20 1B 00 00 00 01 00 01 00
6232 * 00 00 00 00 01 40 00 00
6233 * Length: 0x000000018
6234 * Type: 0x0003 PTP_USB_CONTAINER_RESPONSE
6235 * Code: 0x2001 PTP_OK
6236 * Transaction ID: 0x0000001B
6237 * Param1: 0x00010001 <- store
6238 * Param2: 0x00000000 <- parent handle
6239 * Param3: 0x00004001 <- new file/object ID
6240 *
6241 * -> PTP_OC_SendObject (0x100d)
6242 * 0C 00 00 00 01 00 0D 10 1C 00 00 00
6243 * -> ... all the bytes ...
6244 * <- Read 0x0c bytes back
6245 * 0C 00 00 00 03 00 01 20 1C 00 00 00
6246 * ... Then update metadata one-by one, actually (instead of sending it first!) ...
6247 */
Linus Walleij1e9a0332007-09-12 19:35:56 +00006248 MTPProperties *props = NULL;
6249 int nrofprops = 0;
6250 MTPProperties *prop = NULL;
6251 uint16_t *properties = NULL;
Linus Walleij07795772007-08-06 18:44:06 +00006252 uint32_t propcnt = 0;
Linus Walleij08a5fe12009-09-08 21:28:58 +00006253
Linus Walleij9036b332008-05-23 21:01:36 +00006254 // default parent handle
Linus Walleij07795772007-08-06 18:44:06 +00006255 if (localph == 0)
6256 localph = 0xFFFFFFFFU; // Set to -1
6257
Richard Low6c0a6ce2006-11-26 10:42:08 +00006258 // Must be 0x00000000U for new objects
6259 filedata->item_id = 0x00000000U;
Linus Walleij05ccbe72006-06-13 07:46:58 +00006260
Linus Walleij1e9a0332007-09-12 19:35:56 +00006261 ret = ptp_mtp_getobjectpropssupported(params, of, &propcnt, &properties);
mopoke31364442006-11-20 04:53:04 +00006262
rreardonbbb4e562006-11-19 22:16:11 +00006263 for (i=0;i<propcnt;i++) {
Linus Walleijdbcc8242007-08-05 22:31:26 +00006264 PTPObjectPropDesc opd;
Linus Walleij08a5fe12009-09-08 21:28:58 +00006265
Linus Walleij1e9a0332007-09-12 19:35:56 +00006266 ret = ptp_mtp_getobjectpropdesc(params, properties[i], of, &opd);
Linus Walleijdbcc8242007-08-05 22:31:26 +00006267 if (ret != PTP_RC_OK) {
Richard Lowd3b17022009-04-11 12:37:39 +00006268 add_ptp_error_to_errorstack(device, ret, "send_file_object_info(): "
Linus Walleijdbcc8242007-08-05 22:31:26 +00006269 "could not get property description.");
6270 } else if (opd.GetSet) {
Linus Walleij1e9a0332007-09-12 19:35:56 +00006271 switch (properties[i]) {
Linus Walleijdbcc8242007-08-05 22:31:26 +00006272 case PTP_OPC_ObjectFileName:
Linus Walleija6d0d482007-10-31 08:54:56 +00006273 prop = ptp_get_new_object_prop_entry(&props,&nrofprops);
Linus Walleijdbcc8242007-08-05 22:31:26 +00006274 prop->ObjectHandle = filedata->item_id;
6275 prop->property = PTP_OPC_ObjectFileName;
6276 prop->datatype = PTP_DTC_STR;
Linus Walleijd3b78572007-08-24 21:28:24 +00006277 if (filedata->filename != NULL) {
Linus Walleij07795772007-08-06 18:44:06 +00006278 prop->propval.str = strdup(filedata->filename);
Linus Walleijfec4d562008-06-01 22:30:36 +00006279 if (FLAG_ONLY_7BIT_FILENAMES(ptp_usb)) {
Linus Walleijd3b78572007-08-24 21:28:24 +00006280 strip_7bit_from_utf8(prop->propval.str);
6281 }
6282 }
Linus Walleijdbcc8242007-08-05 22:31:26 +00006283 break;
6284 case PTP_OPC_ProtectionStatus:
Linus Walleija6d0d482007-10-31 08:54:56 +00006285 prop = ptp_get_new_object_prop_entry(&props,&nrofprops);
Linus Walleijdbcc8242007-08-05 22:31:26 +00006286 prop->ObjectHandle = filedata->item_id;
6287 prop->property = PTP_OPC_ProtectionStatus;
6288 prop->datatype = PTP_DTC_UINT16;
6289 prop->propval.u16 = 0x0000U; /* Not protected */
Linus Walleijdbcc8242007-08-05 22:31:26 +00006290 break;
6291 case PTP_OPC_NonConsumable:
Linus Walleija6d0d482007-10-31 08:54:56 +00006292 prop = ptp_get_new_object_prop_entry(&props,&nrofprops);
Linus Walleijdbcc8242007-08-05 22:31:26 +00006293 prop->ObjectHandle = filedata->item_id;
6294 prop->property = PTP_OPC_NonConsumable;
6295 prop->datatype = PTP_DTC_UINT8;
Linus Walleij7783b9b2007-09-22 22:10:44 +00006296 prop->propval.u8 = 0x00; /* It is supported, then it is consumable */
Linus Walleijdbcc8242007-08-05 22:31:26 +00006297 break;
6298 case PTP_OPC_Name:
Linus Walleija6d0d482007-10-31 08:54:56 +00006299 prop = ptp_get_new_object_prop_entry(&props,&nrofprops);
Linus Walleijdbcc8242007-08-05 22:31:26 +00006300 prop->ObjectHandle = filedata->item_id;
6301 prop->property = PTP_OPC_Name;
6302 prop->datatype = PTP_DTC_STR;
Linus Walleij07795772007-08-06 18:44:06 +00006303 if (filedata->filename != NULL)
6304 prop->propval.str = strdup(filedata->filename);
Linus Walleijdbcc8242007-08-05 22:31:26 +00006305 break;
Linus Walleij7783b9b2007-09-22 22:10:44 +00006306 case PTP_OPC_DateModified:
6307 // Tag with current time if that is supported
Linus Walleijcf8dc2b2008-10-21 13:58:36 +00006308 if (!FLAG_CANNOT_HANDLE_DATEMODIFIED(ptp_usb)) {
6309 prop = ptp_get_new_object_prop_entry(&props,&nrofprops);
6310 prop->ObjectHandle = filedata->item_id;
6311 prop->property = PTP_OPC_DateModified;
6312 prop->datatype = PTP_DTC_STR;
6313 prop->propval.str = get_iso8601_stamp();
Linus Walleijd8fd9a52009-09-09 22:46:01 +00006314 filedata->modificationdate = time(NULL);
Linus Walleijcf8dc2b2008-10-21 13:58:36 +00006315 }
Linus Walleij7783b9b2007-09-22 22:10:44 +00006316 break;
Linus Walleijdbcc8242007-08-05 22:31:26 +00006317 }
rreardonbbb4e562006-11-19 22:16:11 +00006318 }
Linus Walleijdbcc8242007-08-05 22:31:26 +00006319 ptp_free_objectpropdesc(&opd);
Linus Walleij99310d42006-11-01 08:29:39 +00006320 }
Linus Walleij1e9a0332007-09-12 19:35:56 +00006321 free(properties);
mopoke31364442006-11-20 04:53:04 +00006322
rreardonbbb4e562006-11-19 22:16:11 +00006323 ret = ptp_mtp_sendobjectproplist(params, &store, &localph, &filedata->item_id,
Linus Walleij1e9a0332007-09-12 19:35:56 +00006324 of, filedata->filesize, props, nrofprops);
mopoke31364442006-11-20 04:53:04 +00006325
rreardonbbb4e562006-11-19 22:16:11 +00006326 /* Free property list */
Linus Walleija6d0d482007-10-31 08:54:56 +00006327 ptp_destroy_object_prop_list(props, nrofprops);
mopoke31364442006-11-20 04:53:04 +00006328
rreardonbbb4e562006-11-19 22:16:11 +00006329 if (ret != PTP_RC_OK) {
Linus Walleijd8fd9a52009-09-09 22:46:01 +00006330 add_ptp_error_to_errorstack(device, ret, "send_file_object_info():"
Linus Walleij07795772007-08-06 18:44:06 +00006331 "Could not send object property list.");
rreardonbbb4e562006-11-19 22:16:11 +00006332 if (ret == PTP_RC_AccessDenied) {
Linus Walleij070e9b42007-01-22 08:49:28 +00006333 add_ptp_error_to_errorstack(device, ret, "ACCESS DENIED.");
rreardonbbb4e562006-11-19 22:16:11 +00006334 }
6335 return -1;
6336 }
6337 } else if (ptp_operation_issupported(params,PTP_OC_SendObjectInfo)) {
Linus Walleij07795772007-08-06 18:44:06 +00006338 PTPObjectInfo new_file;
6339
6340 memset(&new_file, 0, sizeof(PTPObjectInfo));
Linus Walleijd8fd9a52009-09-09 22:46:01 +00006341
Linus Walleij07795772007-08-06 18:44:06 +00006342 new_file.Filename = filedata->filename;
Linus Walleijfec4d562008-06-01 22:30:36 +00006343 if (FLAG_ONLY_7BIT_FILENAMES(ptp_usb)) {
Linus Walleijd3b78572007-08-24 21:28:24 +00006344 strip_7bit_from_utf8(new_file.Filename);
6345 }
Philip Langdaleabdd59d2013-04-05 14:00:28 -07006346 if (filedata->filesize > 0xFFFFFFFFL) {
6347 // This is a kludge in the MTP standard for large files.
6348 new_file.ObjectCompressedSize = (uint32_t) 0xFFFFFFFF;
6349 } else {
6350 new_file.ObjectCompressedSize = (uint32_t) filedata->filesize;
6351 }
Linus Walleij07795772007-08-06 18:44:06 +00006352 new_file.ObjectFormat = of;
6353 new_file.StorageID = store;
6354 new_file.ParentObject = localph;
Richard Low5f6fff82009-04-11 12:45:04 +00006355 new_file.ModificationDate = time(NULL);
Linus Walleij07795772007-08-06 18:44:06 +00006356
Linus Walleij9be685b2006-11-21 09:44:53 +00006357 // Create the object
6358 ret = ptp_sendobjectinfo(params, &store, &localph, &filedata->item_id, &new_file);
Linus Walleij07795772007-08-06 18:44:06 +00006359
Linus Walleij9be685b2006-11-21 09:44:53 +00006360 if (ret != PTP_RC_OK) {
Richard Lowd3b17022009-04-11 12:37:39 +00006361 add_ptp_error_to_errorstack(device, ret, "send_file_object_info(): "
Linus Walleij07795772007-08-06 18:44:06 +00006362 "Could not send object info.");
Linus Walleij9be685b2006-11-21 09:44:53 +00006363 if (ret == PTP_RC_AccessDenied) {
Linus Walleij070e9b42007-01-22 08:49:28 +00006364 add_ptp_error_to_errorstack(device, ret, "ACCESS DENIED.");
Linus Walleij9be685b2006-11-21 09:44:53 +00006365 }
6366 return -1;
6367 }
Linus Walleijf0bf4372007-07-01 21:47:38 +00006368 // NOTE: the char* pointers inside new_file are not copies so don't
6369 // try to destroy this objectinfo!
rreardonbbb4e562006-11-19 22:16:11 +00006370 }
Linus Walleijd2778d12007-08-06 19:24:58 +00006371
6372 // Now there IS an object with this parent handle.
6373 filedata->parent_id = localph;
Linus Walleijd8fd9a52009-09-09 22:46:01 +00006374
Linus Walleijd208f9c2006-04-27 14:16:06 +00006375 return 0;
6376}
6377
Linus Walleij17e39f72006-02-23 15:54:28 +00006378/**
Linus Walleij9036b332008-05-23 21:01:36 +00006379 * This function updates the MTP track object metadata on a
6380 * single file identified by an object ID.
mopoke96143402006-10-30 04:37:26 +00006381 * @param device a pointer to the device to update the track
Linus Walleij95698cd2006-02-24 10:40:40 +00006382 * metadata on.
Linus Walleij17e39f72006-02-23 15:54:28 +00006383 * @param metadata a track metadata set to be written to the file.
6384 * notice that the <code>track_id</code> field of the
6385 * metadata structure must be correct so that the
Linus Walleija4982732006-02-24 15:46:02 +00006386 * function can update the right file. If some properties
6387 * of this metadata are set to NULL (strings) or 0
6388 * (numerical values) they will be discarded and the
6389 * track will not be tagged with these blank values.
Richard Lowaf20b5d2006-12-17 18:00:59 +00006390 * @return 0 on success, any other value means failure. If some
6391 * or all of the properties fail to update we will still
6392 * return success. On some devices (notably iRiver T30)
6393 * properties that exist cannot be updated.
Linus Walleij17e39f72006-02-23 15:54:28 +00006394 */
mopoke96143402006-10-30 04:37:26 +00006395int LIBMTP_Update_Track_Metadata(LIBMTP_mtpdevice_t *device,
Linus Walleij17e39f72006-02-23 15:54:28 +00006396 LIBMTP_track_t const * const metadata)
6397{
Linus Walleij17e39f72006-02-23 15:54:28 +00006398 uint16_t ret;
Linus Walleij00cf0642006-07-26 20:40:59 +00006399 PTPParams *params = (PTPParams *) device->params;
Linus Walleijf9267e92007-10-15 21:07:37 +00006400 PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo;
Linus Walleij00cf0642006-07-26 20:40:59 +00006401 uint32_t i;
Linus Walleij1e9a0332007-09-12 19:35:56 +00006402 uint16_t *properties = NULL;
Linus Walleij00cf0642006-07-26 20:40:59 +00006403 uint32_t propcnt = 0;
Linus Walleij17e39f72006-02-23 15:54:28 +00006404
mopoke96143402006-10-30 04:37:26 +00006405 // First see which properties can be set on this file format and apply accordingly
Linus Walleij00cf0642006-07-26 20:40:59 +00006406 // i.e only try to update this metadata for object tags that exist on the current player.
Linus Walleij1e9a0332007-09-12 19:35:56 +00006407 ret = ptp_mtp_getobjectpropssupported(params, map_libmtp_type_to_ptp_type(metadata->filetype), &propcnt, &properties);
Linus Walleij00cf0642006-07-26 20:40:59 +00006408 if (ret != PTP_RC_OK) {
6409 // Just bail out for now, nothing is ever set.
Linus Walleije7df6532007-03-23 08:20:06 +00006410 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): "
6411 "could not retrieve supported object properties.");
raveloxd9a28642006-05-26 23:42:22 +00006412 return -1;
Richard Low15731fe2007-03-22 20:27:20 +00006413 }
Linus Walleijf9267e92007-10-15 21:07:37 +00006414 if (ptp_operation_issupported(params, PTP_OC_MTP_SetObjPropList) &&
Linus Walleijfec4d562008-06-01 22:30:36 +00006415 !FLAG_BROKEN_SET_OBJECT_PROPLIST(ptp_usb)) {
Linus Walleij1e9a0332007-09-12 19:35:56 +00006416 MTPProperties *props = NULL;
6417 MTPProperties *prop = NULL;
6418 int nrofprops = 0;
Linus Walleij08a5fe12009-09-08 21:28:58 +00006419
Richard Low15731fe2007-03-22 20:27:20 +00006420 for (i=0;i<propcnt;i++) {
Linus Walleijdbcc8242007-08-05 22:31:26 +00006421 PTPObjectPropDesc opd;
Linus Walleij08a5fe12009-09-08 21:28:58 +00006422
Linus Walleij1e9a0332007-09-12 19:35:56 +00006423 ret = ptp_mtp_getobjectpropdesc(params, properties[i], map_libmtp_type_to_ptp_type(metadata->filetype), &opd);
Linus Walleijdbcc8242007-08-05 22:31:26 +00006424 if (ret != PTP_RC_OK) {
6425 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): "
6426 "could not get property description.");
6427 } else if (opd.GetSet) {
Linus Walleij1e9a0332007-09-12 19:35:56 +00006428 switch (properties[i]) {
Linus Walleijdbcc8242007-08-05 22:31:26 +00006429 case PTP_OPC_Name:
6430 if (metadata->title == NULL)
6431 break;
Linus Walleija6d0d482007-10-31 08:54:56 +00006432 prop = ptp_get_new_object_prop_entry(&props, &nrofprops);
Linus Walleij08a5fe12009-09-08 21:28:58 +00006433 prop->ObjectHandle = metadata->item_id;
Linus Walleije7df6532007-03-23 08:20:06 +00006434 prop->property = PTP_OPC_Name;
6435 prop->datatype = PTP_DTC_STR;
Linus Walleijdbcc8242007-08-05 22:31:26 +00006436 prop->propval.str = strdup(metadata->title);
Linus Walleije7df6532007-03-23 08:20:06 +00006437 break;
Linus Walleijdbcc8242007-08-05 22:31:26 +00006438 case PTP_OPC_AlbumName:
6439 if (metadata->album == NULL)
6440 break;
Linus Walleija6d0d482007-10-31 08:54:56 +00006441 prop = ptp_get_new_object_prop_entry(&props, &nrofprops);
Linus Walleije7df6532007-03-23 08:20:06 +00006442 prop->ObjectHandle = metadata->item_id;
6443 prop->property = PTP_OPC_AlbumName;
6444 prop->datatype = PTP_DTC_STR;
Linus Walleijdbcc8242007-08-05 22:31:26 +00006445 prop->propval.str = strdup(metadata->album);
Linus Walleije7df6532007-03-23 08:20:06 +00006446 break;
Linus Walleijdbcc8242007-08-05 22:31:26 +00006447 case PTP_OPC_Artist:
6448 if (metadata->artist == NULL)
6449 break;
Linus Walleija6d0d482007-10-31 08:54:56 +00006450 prop = ptp_get_new_object_prop_entry(&props, &nrofprops);
Linus Walleij08a5fe12009-09-08 21:28:58 +00006451 prop->ObjectHandle = metadata->item_id;
Linus Walleije7df6532007-03-23 08:20:06 +00006452 prop->property = PTP_OPC_Artist;
6453 prop->datatype = PTP_DTC_STR;
Linus Walleijdbcc8242007-08-05 22:31:26 +00006454 prop->propval.str = strdup(metadata->artist);
Linus Walleije7df6532007-03-23 08:20:06 +00006455 break;
Linus Walleij31b74292008-05-02 23:29:06 +00006456 case PTP_OPC_Composer:
6457 if (metadata->composer == NULL)
6458 break;
6459 prop = ptp_get_new_object_prop_entry(&props, &nrofprops);
6460 prop->ObjectHandle = metadata->item_id;
6461 prop->property = PTP_OPC_Composer;
6462 prop->datatype = PTP_DTC_STR;
6463 prop->propval.str = strdup(metadata->composer);
6464 break;
Linus Walleijdbcc8242007-08-05 22:31:26 +00006465 case PTP_OPC_Genre:
6466 if (metadata->genre == NULL)
6467 break;
Linus Walleija6d0d482007-10-31 08:54:56 +00006468 prop = ptp_get_new_object_prop_entry(&props, &nrofprops);
Linus Walleij08a5fe12009-09-08 21:28:58 +00006469 prop->ObjectHandle = metadata->item_id;
Linus Walleije7df6532007-03-23 08:20:06 +00006470 prop->property = PTP_OPC_Genre;
6471 prop->datatype = PTP_DTC_STR;
Linus Walleijdbcc8242007-08-05 22:31:26 +00006472 prop->propval.str = strdup(metadata->genre);
Linus Walleije7df6532007-03-23 08:20:06 +00006473 break;
Linus Walleijdbcc8242007-08-05 22:31:26 +00006474 case PTP_OPC_Duration:
Linus Walleija6d0d482007-10-31 08:54:56 +00006475 prop = ptp_get_new_object_prop_entry(&props, &nrofprops);
Linus Walleije7df6532007-03-23 08:20:06 +00006476 prop->ObjectHandle = metadata->item_id;
6477 prop->property = PTP_OPC_Duration;
6478 prop->datatype = PTP_DTC_UINT32;
Linus Walleij29559562007-08-22 21:38:04 +00006479 prop->propval.u32 = adjust_u32(metadata->duration, &opd);
Linus Walleije7df6532007-03-23 08:20:06 +00006480 break;
Linus Walleijdbcc8242007-08-05 22:31:26 +00006481 case PTP_OPC_Track:
Linus Walleija6d0d482007-10-31 08:54:56 +00006482 prop = ptp_get_new_object_prop_entry(&props, &nrofprops);
Linus Walleije7df6532007-03-23 08:20:06 +00006483 prop->ObjectHandle = metadata->item_id;
6484 prop->property = PTP_OPC_Track;
6485 prop->datatype = PTP_DTC_UINT16;
Linus Walleij29559562007-08-22 21:38:04 +00006486 prop->propval.u16 = adjust_u16(metadata->tracknumber, &opd);
Linus Walleije7df6532007-03-23 08:20:06 +00006487 break;
Linus Walleijdbcc8242007-08-05 22:31:26 +00006488 case PTP_OPC_OriginalReleaseDate:
6489 if (metadata->date == NULL)
6490 break;
Linus Walleija6d0d482007-10-31 08:54:56 +00006491 prop = ptp_get_new_object_prop_entry(&props, &nrofprops);
Linus Walleij08a5fe12009-09-08 21:28:58 +00006492 prop->ObjectHandle = metadata->item_id;
Linus Walleije7df6532007-03-23 08:20:06 +00006493 prop->property = PTP_OPC_OriginalReleaseDate;
6494 prop->datatype = PTP_DTC_STR;
Linus Walleijdbcc8242007-08-05 22:31:26 +00006495 prop->propval.str = strdup(metadata->date);
Linus Walleije7df6532007-03-23 08:20:06 +00006496 break;
Linus Walleijdbcc8242007-08-05 22:31:26 +00006497 case PTP_OPC_SampleRate:
Linus Walleija6d0d482007-10-31 08:54:56 +00006498 prop = ptp_get_new_object_prop_entry(&props, &nrofprops);
Linus Walleij08a5fe12009-09-08 21:28:58 +00006499 prop->ObjectHandle = metadata->item_id;
Linus Walleije7df6532007-03-23 08:20:06 +00006500 prop->property = PTP_OPC_SampleRate;
6501 prop->datatype = PTP_DTC_UINT32;
Linus Walleij29559562007-08-22 21:38:04 +00006502 prop->propval.u32 = adjust_u32(metadata->samplerate, &opd);
Linus Walleije7df6532007-03-23 08:20:06 +00006503 break;
Linus Walleijdbcc8242007-08-05 22:31:26 +00006504 case PTP_OPC_NumberOfChannels:
Linus Walleija6d0d482007-10-31 08:54:56 +00006505 prop = ptp_get_new_object_prop_entry(&props, &nrofprops);
Linus Walleij08a5fe12009-09-08 21:28:58 +00006506 prop->ObjectHandle = metadata->item_id;
Linus Walleije7df6532007-03-23 08:20:06 +00006507 prop->property = PTP_OPC_NumberOfChannels;
6508 prop->datatype = PTP_DTC_UINT16;
Linus Walleij29559562007-08-22 21:38:04 +00006509 prop->propval.u16 = adjust_u16(metadata->nochannels, &opd);
Linus Walleije7df6532007-03-23 08:20:06 +00006510 break;
Linus Walleijdbcc8242007-08-05 22:31:26 +00006511 case PTP_OPC_AudioWAVECodec:
Linus Walleija6d0d482007-10-31 08:54:56 +00006512 prop = ptp_get_new_object_prop_entry(&props, &nrofprops);
Linus Walleij08a5fe12009-09-08 21:28:58 +00006513 prop->ObjectHandle = metadata->item_id;
Linus Walleije7df6532007-03-23 08:20:06 +00006514 prop->property = PTP_OPC_AudioWAVECodec;
6515 prop->datatype = PTP_DTC_UINT32;
Linus Walleij29559562007-08-22 21:38:04 +00006516 prop->propval.u32 = adjust_u32(metadata->wavecodec, &opd);
Linus Walleije7df6532007-03-23 08:20:06 +00006517 break;
Linus Walleijdbcc8242007-08-05 22:31:26 +00006518 case PTP_OPC_AudioBitRate:
Linus Walleija6d0d482007-10-31 08:54:56 +00006519 prop = ptp_get_new_object_prop_entry(&props, &nrofprops);
Linus Walleij08a5fe12009-09-08 21:28:58 +00006520 prop->ObjectHandle = metadata->item_id;
Linus Walleije7df6532007-03-23 08:20:06 +00006521 prop->property = PTP_OPC_AudioBitRate;
6522 prop->datatype = PTP_DTC_UINT32;
Linus Walleij29559562007-08-22 21:38:04 +00006523 prop->propval.u32 = adjust_u32(metadata->bitrate, &opd);
Linus Walleije7df6532007-03-23 08:20:06 +00006524 break;
Linus Walleijdbcc8242007-08-05 22:31:26 +00006525 case PTP_OPC_BitRateType:
Linus Walleija6d0d482007-10-31 08:54:56 +00006526 prop = ptp_get_new_object_prop_entry(&props, &nrofprops);
Linus Walleij08a5fe12009-09-08 21:28:58 +00006527 prop->ObjectHandle = metadata->item_id;
Linus Walleije7df6532007-03-23 08:20:06 +00006528 prop->property = PTP_OPC_BitRateType;
6529 prop->datatype = PTP_DTC_UINT16;
Linus Walleij29559562007-08-22 21:38:04 +00006530 prop->propval.u16 = adjust_u16(metadata->bitratetype, &opd);
Linus Walleije7df6532007-03-23 08:20:06 +00006531 break;
Linus Walleijdbcc8242007-08-05 22:31:26 +00006532 case PTP_OPC_Rating:
6533 // TODO: shall this be set for rating 0?
6534 if (metadata->rating == 0)
6535 break;
Linus Walleija6d0d482007-10-31 08:54:56 +00006536 prop = ptp_get_new_object_prop_entry(&props, &nrofprops);
Linus Walleij08a5fe12009-09-08 21:28:58 +00006537 prop->ObjectHandle = metadata->item_id;
Linus Walleijdbcc8242007-08-05 22:31:26 +00006538 prop->property = PTP_OPC_Rating;
6539 prop->datatype = PTP_DTC_UINT16;
Linus Walleij29559562007-08-22 21:38:04 +00006540 prop->propval.u16 = adjust_u16(metadata->rating, &opd);
Linus Walleijdbcc8242007-08-05 22:31:26 +00006541 break;
6542 case PTP_OPC_UseCount:
Linus Walleija6d0d482007-10-31 08:54:56 +00006543 prop = ptp_get_new_object_prop_entry(&props, &nrofprops);
Linus Walleij08a5fe12009-09-08 21:28:58 +00006544 prop->ObjectHandle = metadata->item_id;
Linus Walleije7df6532007-03-23 08:20:06 +00006545 prop->property = PTP_OPC_UseCount;
6546 prop->datatype = PTP_DTC_UINT32;
Linus Walleij29559562007-08-22 21:38:04 +00006547 prop->propval.u32 = adjust_u32(metadata->usecount, &opd);
Linus Walleij7783b9b2007-09-22 22:10:44 +00006548 break;
6549 case PTP_OPC_DateModified:
Linus Walleijcf8dc2b2008-10-21 13:58:36 +00006550 if (!FLAG_CANNOT_HANDLE_DATEMODIFIED(ptp_usb)) {
Linus Walleij37588142008-10-16 19:10:47 +00006551 // Tag with current time if that is supported
6552 prop = ptp_get_new_object_prop_entry(&props, &nrofprops);
6553 prop->ObjectHandle = metadata->item_id;
6554 prop->property = PTP_OPC_DateModified;
6555 prop->datatype = PTP_DTC_STR;
6556 prop->propval.str = get_iso8601_stamp();
6557 }
Linus Walleij7783b9b2007-09-22 22:10:44 +00006558 break;
Linus Walleijf9267e92007-10-15 21:07:37 +00006559 default:
6560 break;
Linus Walleije7df6532007-03-23 08:20:06 +00006561 }
Linus Walleij304433d2007-02-26 08:02:47 +00006562 }
Linus Walleijdbcc8242007-08-05 22:31:26 +00006563 ptp_free_objectpropdesc(&opd);
Richard Low15731fe2007-03-22 20:27:20 +00006564 }
Linus Walleijdbcc8242007-08-05 22:31:26 +00006565
Richard Low15731fe2007-03-22 20:27:20 +00006566 // NOTE: File size is not updated, this should not change anyway.
6567 // neither will we change the filename.
Linus Walleij08a5fe12009-09-08 21:28:58 +00006568
Linus Walleij1e9a0332007-09-12 19:35:56 +00006569 ret = ptp_mtp_setobjectproplist(params, props, nrofprops);
Linus Walleij08a5fe12009-09-08 21:28:58 +00006570
Linus Walleija6d0d482007-10-31 08:54:56 +00006571 ptp_destroy_object_prop_list(props, nrofprops);
Linus Walleij08a5fe12009-09-08 21:28:58 +00006572
Richard Low15731fe2007-03-22 20:27:20 +00006573 if (ret != PTP_RC_OK) {
6574 // TODO: return error of which property we couldn't set
Linus Walleije7df6532007-03-23 08:20:06 +00006575 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): "
6576 "could not set object property list.");
Richard Low016e3732007-09-23 14:53:46 +00006577 free(properties);
Linus Walleij304433d2007-02-26 08:02:47 +00006578 return -1;
6579 }
Linus Walleij08a5fe12009-09-08 21:28:58 +00006580
Richard Low15731fe2007-03-22 20:27:20 +00006581 } else if (ptp_operation_issupported(params,PTP_OC_MTP_SetObjectPropValue)) {
Linus Walleij00cf0642006-07-26 20:40:59 +00006582 for (i=0;i<propcnt;i++) {
Linus Walleijdbcc8242007-08-05 22:31:26 +00006583 PTPObjectPropDesc opd;
Linus Walleij08a5fe12009-09-08 21:28:58 +00006584
Linus Walleij1e9a0332007-09-12 19:35:56 +00006585 ret = ptp_mtp_getobjectpropdesc(params, properties[i], map_libmtp_type_to_ptp_type(metadata->filetype), &opd);
Linus Walleijdbcc8242007-08-05 22:31:26 +00006586 if (ret != PTP_RC_OK) {
6587 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): "
6588 "could not get property description.");
6589 } else if (opd.GetSet) {
Linus Walleij1e9a0332007-09-12 19:35:56 +00006590 switch (properties[i]) {
Linus Walleijdbcc8242007-08-05 22:31:26 +00006591 case PTP_OPC_Name:
6592 // Update title
6593 ret = set_object_string(device, metadata->item_id, PTP_OPC_Name, metadata->title);
Linus Walleije7df6532007-03-23 08:20:06 +00006594 if (ret != 0) {
6595 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): "
Linus Walleijdbcc8242007-08-05 22:31:26 +00006596 "could not set track title.");
Linus Walleije7df6532007-03-23 08:20:06 +00006597 }
Linus Walleijdbcc8242007-08-05 22:31:26 +00006598 break;
6599 case PTP_OPC_AlbumName:
6600 // Update album
6601 ret = set_object_string(device, metadata->item_id, PTP_OPC_AlbumName, metadata->album);
Linus Walleije7df6532007-03-23 08:20:06 +00006602 if (ret != 0) {
6603 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): "
Linus Walleijdbcc8242007-08-05 22:31:26 +00006604 "could not set track album name.");
Linus Walleije7df6532007-03-23 08:20:06 +00006605 }
Linus Walleijdbcc8242007-08-05 22:31:26 +00006606 break;
6607 case PTP_OPC_Artist:
6608 // Update artist
6609 ret = set_object_string(device, metadata->item_id, PTP_OPC_Artist, metadata->artist);
Linus Walleije7df6532007-03-23 08:20:06 +00006610 if (ret != 0) {
6611 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): "
Linus Walleijdbcc8242007-08-05 22:31:26 +00006612 "could not set track artist name.");
Linus Walleije7df6532007-03-23 08:20:06 +00006613 }
Linus Walleijdbcc8242007-08-05 22:31:26 +00006614 break;
Linus Walleij31b74292008-05-02 23:29:06 +00006615 case PTP_OPC_Composer:
6616 // Update composer
6617 ret = set_object_string(device, metadata->item_id, PTP_OPC_Composer, metadata->composer);
6618 if (ret != 0) {
6619 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): "
6620 "could not set track composer name.");
6621 }
6622 break;
Linus Walleijdbcc8242007-08-05 22:31:26 +00006623 case PTP_OPC_Genre:
Linus Walleijb7e8f972010-01-19 00:38:11 +00006624 // Update genre (but only if valid)
6625 if (metadata->genre) {
6626 ret = set_object_string(device, metadata->item_id, PTP_OPC_Genre, metadata->genre);
6627 if (ret != 0) {
6628 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "update_abstract_list(): "
6629 "could not set genre.");
6630 }
Linus Walleijdbcc8242007-08-05 22:31:26 +00006631 }
6632 break;
6633 case PTP_OPC_Duration:
6634 // Update duration
6635 if (metadata->duration != 0) {
Linus Walleij29559562007-08-22 21:38:04 +00006636 ret = set_object_u32(device, metadata->item_id, PTP_OPC_Duration, adjust_u32(metadata->duration, &opd));
Linus Walleijdbcc8242007-08-05 22:31:26 +00006637 if (ret != 0) {
6638 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): "
6639 "could not set track duration.");
6640 }
6641 }
6642 break;
6643 case PTP_OPC_Track:
6644 // Update track number.
6645 if (metadata->tracknumber != 0) {
Linus Walleij29559562007-08-22 21:38:04 +00006646 ret = set_object_u16(device, metadata->item_id, PTP_OPC_Track, adjust_u16(metadata->tracknumber, &opd));
Linus Walleijdbcc8242007-08-05 22:31:26 +00006647 if (ret != 0) {
6648 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): "
6649 "could not set track tracknumber.");
6650 }
6651 }
6652 break;
6653 case PTP_OPC_OriginalReleaseDate:
6654 // Update creation datetime
Linus Walleijb7e8f972010-01-19 00:38:11 +00006655 // The date can be zero, but some devices do not support setting zero
6656 // dates (and it seems that a zero date should never be set anyway)
6657 if (metadata->date) {
6658 ret = set_object_string(device, metadata->item_id, PTP_OPC_OriginalReleaseDate, metadata->date);
6659 if (ret != 0) {
6660 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): "
6661 "could not set track release date.");
6662 }
Linus Walleijdbcc8242007-08-05 22:31:26 +00006663 }
6664 break;
6665 // These are, well not so important.
6666 case PTP_OPC_SampleRate:
6667 // Update sample rate
6668 if (metadata->samplerate != 0) {
Linus Walleij29559562007-08-22 21:38:04 +00006669 ret = set_object_u32(device, metadata->item_id, PTP_OPC_SampleRate, adjust_u32(metadata->samplerate, &opd));
Linus Walleijdbcc8242007-08-05 22:31:26 +00006670 if (ret != 0) {
6671 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): "
6672 "could not set samplerate.");
6673 }
6674 }
6675 break;
6676 case PTP_OPC_NumberOfChannels:
6677 // Update number of channels
6678 if (metadata->nochannels != 0) {
Linus Walleij29559562007-08-22 21:38:04 +00006679 ret = set_object_u16(device, metadata->item_id, PTP_OPC_NumberOfChannels, adjust_u16(metadata->nochannels, &opd));
Linus Walleije7df6532007-03-23 08:20:06 +00006680 if (ret != 0) {
6681 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): "
6682 "could not set number of channels.");
6683 }
6684 }
Linus Walleijdbcc8242007-08-05 22:31:26 +00006685 break;
6686 case PTP_OPC_AudioWAVECodec:
6687 // Update WAVE codec
6688 if (metadata->wavecodec != 0) {
Linus Walleij29559562007-08-22 21:38:04 +00006689 ret = set_object_u32(device, metadata->item_id, PTP_OPC_AudioWAVECodec, adjust_u32(metadata->wavecodec, &opd));
Linus Walleijdbcc8242007-08-05 22:31:26 +00006690 if (ret != 0) {
6691 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): "
6692 "could not set WAVE codec.");
6693 }
6694 }
6695 break;
6696 case PTP_OPC_AudioBitRate:
6697 // Update bitrate
6698 if (metadata->bitrate != 0) {
Linus Walleij29559562007-08-22 21:38:04 +00006699 ret = set_object_u32(device, metadata->item_id, PTP_OPC_AudioBitRate, adjust_u32(metadata->bitrate, &opd));
Linus Walleijdbcc8242007-08-05 22:31:26 +00006700 if (ret != 0) {
6701 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): "
6702 "could not set bitrate.");
6703 }
6704 }
6705 break;
6706 case PTP_OPC_BitRateType:
6707 // Update bitrate type
6708 if (metadata->bitratetype != 0) {
Linus Walleij29559562007-08-22 21:38:04 +00006709 ret = set_object_u16(device, metadata->item_id, PTP_OPC_BitRateType, adjust_u16(metadata->bitratetype, &opd));
Linus Walleijdbcc8242007-08-05 22:31:26 +00006710 if (ret != 0) {
6711 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): "
6712 "could not set bitratetype.");
6713 }
6714 }
6715 break;
6716 case PTP_OPC_Rating:
6717 // Update user rating
6718 // TODO: shall this be set for rating 0?
6719 if (metadata->rating != 0) {
Linus Walleij29559562007-08-22 21:38:04 +00006720 ret = set_object_u16(device, metadata->item_id, PTP_OPC_Rating, adjust_u16(metadata->rating, &opd));
Linus Walleijdbcc8242007-08-05 22:31:26 +00006721 if (ret != 0) {
6722 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): "
6723 "could not set user rating.");
6724 }
6725 }
6726 break;
6727 case PTP_OPC_UseCount:
6728 // Update use count, set even to zero if desired.
Linus Walleij29559562007-08-22 21:38:04 +00006729 ret = set_object_u32(device, metadata->item_id, PTP_OPC_UseCount, adjust_u32(metadata->usecount, &opd));
Linus Walleije7df6532007-03-23 08:20:06 +00006730 if (ret != 0) {
6731 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): "
Linus Walleije7df6532007-03-23 08:20:06 +00006732 "could not set use count.");
Linus Walleijdbcc8242007-08-05 22:31:26 +00006733 }
6734 break;
Linus Walleij7783b9b2007-09-22 22:10:44 +00006735 case PTP_OPC_DateModified:
Linus Walleijcf8dc2b2008-10-21 13:58:36 +00006736 if (!FLAG_CANNOT_HANDLE_DATEMODIFIED(ptp_usb)) {
Linus Walleij7783b9b2007-09-22 22:10:44 +00006737 // Update modification time if supported
6738 char *tmpstamp = get_iso8601_stamp();
6739 ret = set_object_string(device, metadata->item_id, PTP_OPC_DateModified, tmpstamp);
6740 if (ret != 0) {
6741 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): "
6742 "could not set modification date.");
6743 }
6744 free(tmpstamp);
6745 }
Linus Walleij08a5fe12009-09-08 21:28:58 +00006746 break;
6747
Linus Walleijdbcc8242007-08-05 22:31:26 +00006748 // NOTE: File size is not updated, this should not change anyway.
6749 // neither will we change the filename.
6750 default:
6751 break;
Linus Walleije7df6532007-03-23 08:20:06 +00006752 }
Linus Walleij00cf0642006-07-26 20:40:59 +00006753 }
Linus Walleijdbcc8242007-08-05 22:31:26 +00006754 ptp_free_objectpropdesc(&opd);
Linus Walleija4982732006-02-24 15:46:02 +00006755 }
Richard Low15731fe2007-03-22 20:27:20 +00006756 } else {
6757 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): "
6758 "Your device doesn't seem to support any known way of setting metadata.");
Richard Low016e3732007-09-23 14:53:46 +00006759 free(properties);
Richard Low15731fe2007-03-22 20:27:20 +00006760 return -1;
Linus Walleij17e39f72006-02-23 15:54:28 +00006761 }
Linus Walleij08a5fe12009-09-08 21:28:58 +00006762
tsaarnia3eb60a2007-07-06 17:41:30 +00006763 // update cached object properties if metadata cache exists
Linus Walleij7752b952007-10-19 20:11:46 +00006764 update_metadata_cache(device, metadata->item_id);
Linus Walleij338ade42007-07-03 20:44:08 +00006765
Richard Low016e3732007-09-23 14:53:46 +00006766 free(properties);
Linus Walleij08a5fe12009-09-08 21:28:58 +00006767
Linus Walleijf1b02f22006-12-06 09:03:23 +00006768 return 0;
Linus Walleij394bbbe2006-02-22 16:10:53 +00006769}
Linus Walleij95698cd2006-02-24 10:40:40 +00006770
6771/**
Linus Walleij399807b2008-01-27 22:05:12 +00006772 * This function deletes a single file, track, playlist, folder or
6773 * any other object off the MTP device, identified by the object ID.
6774 *
6775 * If you delete a folder, there is no guarantee that the device will
6776 * really delete all the files that were in that folder, rather it is
6777 * expected that they will not be deleted, and will turn up in object
6778 * listings with parent set to a non-existant object ID. The safe way
Linus Walleij08a5fe12009-09-08 21:28:58 +00006779 * to do this is to recursively delete all files (and folders) contained
Linus Walleij399807b2008-01-27 22:05:12 +00006780 * in the folder, then the folder itself.
6781 *
6782 * @param device a pointer to the device to delete the object from.
6783 * @param object_id the object to delete.
Linus Walleij95698cd2006-02-24 10:40:40 +00006784 * @return 0 on success, any other value means failure.
6785 */
mopoke96143402006-10-30 04:37:26 +00006786int LIBMTP_Delete_Object(LIBMTP_mtpdevice_t *device,
Linus Walleij438bd7f2006-06-08 11:35:44 +00006787 uint32_t object_id)
Linus Walleij95698cd2006-02-24 10:40:40 +00006788{
Linus Walleij438bd7f2006-06-08 11:35:44 +00006789 uint16_t ret;
6790 PTPParams *params = (PTPParams *) device->params;
6791
6792 ret = ptp_deleteobject(params, object_id, 0);
6793 if (ret != PTP_RC_OK) {
Linus Walleij070e9b42007-01-22 08:49:28 +00006794 add_ptp_error_to_errorstack(device, ret, "LIBMTP_Delete_Object(): could not delete object.");
Linus Walleij438bd7f2006-06-08 11:35:44 +00006795 return -1;
6796 }
tsaarnia3eb60a2007-07-06 17:41:30 +00006797
Linus Walleij438bd7f2006-06-08 11:35:44 +00006798 return 0;
Linus Walleij95698cd2006-02-24 10:40:40 +00006799}
Linus Walleij9c6ca022006-04-21 10:24:15 +00006800
Linus Walleij9c6ca022006-04-21 10:24:15 +00006801/**
Philip Langdale1680c392018-03-17 09:23:25 +01006802 * The function moves an object from one location on a device to another
6803 * location.
6804 *
6805 * The semantics of moving a folder are not defined in the spec, but it
6806 * appears to do the right thing when tested (but devices that implement
6807 * this operation are rare).
6808 *
6809 * Note that moving an object may take a significant amount of time,
6810 * particularly if being moved between storages. MTP does not provide
6811 * any kind of progress mechanism, so the operation will simply block
6812 * for the duration.
6813 *
6814 * @param device a pointer to the device where the object exists.
6815 * @param object_id the object to move.
6816 * @param storage_id the id of the destination storage.
6817 * @param parent_id the id of the destination parent object (folder).
6818 * If the destination is the root of the storage, pass '0'.
6819 * @return 0 on success, any other value means failure.
6820 */
6821int LIBMTP_Move_Object(LIBMTP_mtpdevice_t *device,
6822 uint32_t object_id,
6823 uint32_t storage_id,
6824 uint32_t parent_id)
6825{
6826 uint16_t ret;
6827 PTPParams *params = (PTPParams *) device->params;
6828
6829 ret = ptp_moveobject(params, object_id, storage_id, parent_id);
6830 if (ret != PTP_RC_OK) {
6831 add_ptp_error_to_errorstack(device, ret, "LIBMTP_Move_Object(): could not move object.");
6832 return -1;
6833 }
6834
6835 return 0;
6836}
6837
6838/**
6839 * The function copies an object from one location on a device to another
6840 * location.
6841 *
6842 * The semantics of copying a folder are not defined in the spec, but it
6843 * appears to do the right thing when tested (but devices that implement
6844 * this operation are rare).
6845 *
6846 * Note that copying an object may take a significant amount of time.
6847 * MTP does not provide any kind of progress mechanism, so the operation
6848 * will simply block for the duration.
6849 *
6850 * @param device a pointer to the device where the object exists.
6851 * @param object_id the object to copy.
6852 * @param storage_id the id of the destination storage.
6853 * @param parent_id the id of the destination parent object (folder).
6854 * If the destination is the root of the storage, pass '0'.
6855 * @return 0 on success, any other value means failure.
6856 */
6857int LIBMTP_Copy_Object(LIBMTP_mtpdevice_t *device,
6858 uint32_t object_id,
6859 uint32_t storage_id,
6860 uint32_t parent_id)
6861{
6862 uint16_t ret;
6863 PTPParams *params = (PTPParams *) device->params;
6864
6865 ret = ptp_copyobject(params, object_id, storage_id, parent_id);
6866 if (ret != PTP_RC_OK) {
6867 add_ptp_error_to_errorstack(device, ret, "LIBMTP_Copy_Object(): could not copy object.");
6868 return -1;
6869 }
6870
6871 return 0;
6872}
6873
6874/**
Linus Walleij8d8c4352008-09-23 23:04:16 +00006875 * Internal function to update an object filename property.
Linus Walleijde8193f2008-01-27 14:55:31 +00006876 */
Linus Walleij8d8c4352008-09-23 23:04:16 +00006877static int set_object_filename(LIBMTP_mtpdevice_t *device,
6878 uint32_t object_id, uint16_t ptp_type,
6879 const char **newname_ptr)
Linus Walleijde8193f2008-01-27 14:55:31 +00006880{
6881 PTPParams *params = (PTPParams *) device->params;
6882 PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo;
6883 PTPObjectPropDesc opd;
6884 uint16_t ret;
Linus Walleij8d8c4352008-09-23 23:04:16 +00006885 char *newname;
Linus Walleij8be2c032008-01-28 21:23:47 +00006886
6887 // See if we can modify the filename on this kind of files.
6888 ret = ptp_mtp_getobjectpropdesc(params, PTP_OPC_ObjectFileName, ptp_type, &opd);
Linus Walleijde8193f2008-01-27 14:55:31 +00006889 if (ret != PTP_RC_OK) {
Linus Walleij8d8c4352008-09-23 23:04:16 +00006890 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "set_object_filename(): "
Linus Walleij8be2c032008-01-28 21:23:47 +00006891 "could not get property description.");
Linus Walleijde8193f2008-01-27 14:55:31 +00006892 return -1;
6893 }
6894
6895 if (!opd.GetSet) {
6896 ptp_free_objectpropdesc(&opd);
Linus Walleij8d8c4352008-09-23 23:04:16 +00006897 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "set_object_filename(): "
6898 " property is not settable.");
Linus Walleij399807b2008-01-27 22:05:12 +00006899 // TODO: we COULD actually upload/download the object here, if we feel
6900 // like wasting time for the user.
Linus Walleijde8193f2008-01-27 14:55:31 +00006901 return -1;
6902 }
6903
Linus Walleij8d8c4352008-09-23 23:04:16 +00006904 newname = strdup(*newname_ptr);
6905
Linus Walleijfec4d562008-06-01 22:30:36 +00006906 if (FLAG_ONLY_7BIT_FILENAMES(ptp_usb)) {
Linus Walleijde8193f2008-01-27 14:55:31 +00006907 strip_7bit_from_utf8(newname);
6908 }
6909
6910 if (ptp_operation_issupported(params, PTP_OC_MTP_SetObjPropList) &&
Linus Walleijfec4d562008-06-01 22:30:36 +00006911 !FLAG_BROKEN_SET_OBJECT_PROPLIST(ptp_usb)) {
Linus Walleijde8193f2008-01-27 14:55:31 +00006912 MTPProperties *props = NULL;
6913 MTPProperties *prop = NULL;
6914 int nrofprops = 0;
Linus Walleij8d8c4352008-09-23 23:04:16 +00006915
Linus Walleijde8193f2008-01-27 14:55:31 +00006916 prop = ptp_get_new_object_prop_entry(&props, &nrofprops);
Linus Walleij08a5fe12009-09-08 21:28:58 +00006917 prop->ObjectHandle = object_id;
Linus Walleijde8193f2008-01-27 14:55:31 +00006918 prop->property = PTP_OPC_ObjectFileName;
6919 prop->datatype = PTP_DTC_STR;
Linus Walleij8d8c4352008-09-23 23:04:16 +00006920 prop->propval.str = newname;
6921
Linus Walleijde8193f2008-01-27 14:55:31 +00006922 ret = ptp_mtp_setobjectproplist(params, props, nrofprops);
Linus Walleij8d8c4352008-09-23 23:04:16 +00006923
Linus Walleijde8193f2008-01-27 14:55:31 +00006924 ptp_destroy_object_prop_list(props, nrofprops);
Linus Walleij8d8c4352008-09-23 23:04:16 +00006925
Linus Walleijde8193f2008-01-27 14:55:31 +00006926 if (ret != PTP_RC_OK) {
Linus Walleij8d8c4352008-09-23 23:04:16 +00006927 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "set_object_filename(): "
6928 " could not set object property list.");
6929 ptp_free_objectpropdesc(&opd);
6930 return -1;
Linus Walleijde8193f2008-01-27 14:55:31 +00006931 }
6932 } else if (ptp_operation_issupported(params, PTP_OC_MTP_SetObjectPropValue)) {
6933 ret = set_object_string(device, object_id, PTP_OPC_ObjectFileName, newname);
6934 if (ret != 0) {
Linus Walleij8d8c4352008-09-23 23:04:16 +00006935 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "set_object_filename(): "
6936 " could not set object filename.");
Linus Walleijde8193f2008-01-27 14:55:31 +00006937 ptp_free_objectpropdesc(&opd);
6938 return -1;
6939 }
6940 } else {
Linus Walleij8d8c4352008-09-23 23:04:16 +00006941 free(newname);
6942 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "set_object_filename(): "
6943 " your device doesn't seem to support any known way of setting metadata.");
Linus Walleijde8193f2008-01-27 14:55:31 +00006944 ptp_free_objectpropdesc(&opd);
6945 return -1;
6946 }
Linus Walleij8d8c4352008-09-23 23:04:16 +00006947
Linus Walleijde8193f2008-01-27 14:55:31 +00006948 ptp_free_objectpropdesc(&opd);
Linus Walleij8d8c4352008-09-23 23:04:16 +00006949
Linus Walleijde8193f2008-01-27 14:55:31 +00006950 // update cached object properties if metadata cache exists
6951 update_metadata_cache(device, object_id);
Linus Walleij8d8c4352008-09-23 23:04:16 +00006952
Linus Walleijde8193f2008-01-27 14:55:31 +00006953 return 0;
6954}
Linus Walleij399807b2008-01-27 22:05:12 +00006955
Linus Walleijde8193f2008-01-27 14:55:31 +00006956/**
Linus Walleij8d8c4352008-09-23 23:04:16 +00006957 * This function renames a single file.
6958 * This simply means that the PTP_OPC_ObjectFileName property
6959 * is updated, if this is supported by the device.
6960 *
6961 * @param device a pointer to the device that contains the file.
6962 * @param file the file metadata of the file to rename.
6963 * On success, the filename member is updated. Be aware, that
6964 * this name can be different than newname depending of device restrictions.
6965 * @param newname the new filename for this object.
6966 * @return 0 on success, any other value means failure.
6967 */
6968int LIBMTP_Set_File_Name(LIBMTP_mtpdevice_t *device,
6969 LIBMTP_file_t *file, const char *newname)
6970{
6971 int ret;
6972
6973 ret = set_object_filename(device, file->item_id,
6974 map_libmtp_type_to_ptp_type(file->filetype),
6975 &newname);
6976
6977 if (ret != 0) {
6978 return ret;
6979 }
6980
6981 free(file->filename);
6982 file->filename = strdup(newname);
6983 return ret;
6984}
6985
6986/**
6987 * This function renames a single folder.
6988 * This simply means that the PTP_OPC_ObjectFileName property
6989 * is updated, if this is supported by the device.
6990 *
6991 * @param device a pointer to the device that contains the file.
6992 * @param folder the folder metadata of the folder to rename.
6993 * On success, the name member is updated. Be aware, that
6994 * this name can be different than newname depending of device restrictions.
6995 * @param newname the new name for this object.
6996 * @return 0 on success, any other value means failure.
6997 */
6998int LIBMTP_Set_Folder_Name(LIBMTP_mtpdevice_t *device,
6999 LIBMTP_folder_t *folder, const char* newname)
7000{
7001 int ret;
7002
7003 ret = set_object_filename(device, folder->folder_id,
Linus Walleijfb7212f2008-09-24 20:30:13 +00007004 PTP_OFC_Association,
Linus Walleij8d8c4352008-09-23 23:04:16 +00007005 &newname);
7006
7007 if (ret != 0) {
7008 return ret;
7009 }
7010
7011 free(folder->name);
7012 folder->name = strdup(newname);
7013 return ret;
7014}
7015
7016/**
7017 * This function renames a single track.
7018 * This simply means that the PTP_OPC_ObjectFileName property
7019 * is updated, if this is supported by the device.
7020 *
7021 * @param device a pointer to the device that contains the file.
7022 * @param track the track metadata of the track to rename.
7023 * On success, the filename member is updated. Be aware, that
7024 * this name can be different than newname depending of device restrictions.
7025 * @param newname the new filename for this object.
7026 * @return 0 on success, any other value means failure.
7027 */
7028int LIBMTP_Set_Track_Name(LIBMTP_mtpdevice_t *device,
7029 LIBMTP_track_t *track, const char* newname)
7030{
7031 int ret;
7032
7033 ret = set_object_filename(device, track->item_id,
7034 map_libmtp_type_to_ptp_type(track->filetype),
7035 &newname);
7036
7037 if (ret != 0) {
7038 return ret;
7039 }
7040
7041 free(track->filename);
7042 track->filename = strdup(newname);
7043 return ret;
7044}
7045
7046/**
Linus Walleij12bbf312009-01-26 21:46:51 +00007047 * This function renames a single playlist object file holder.
7048 * This simply means that the <code>PTP_OPC_ObjectFileName</code>
7049 * property is updated, if this is supported by the device.
7050 * The playlist filename should nominally end with an extension
7051 * like ".pla".
7052 *
7053 * NOTE: if you want to change the metadata the device display
7054 * about a playlist you must <i>not</i> use this function,
7055 * use <code>LIBMTP_Update_Playlist()</code> instead!
Linus Walleij8d8c4352008-09-23 23:04:16 +00007056 *
7057 * @param device a pointer to the device that contains the file.
7058 * @param playlist the playlist metadata of the playlist to rename.
7059 * On success, the name member is updated. Be aware, that
7060 * this name can be different than newname depending of device restrictions.
7061 * @param newname the new name for this object.
7062 * @return 0 on success, any other value means failure.
Linus Walleij12bbf312009-01-26 21:46:51 +00007063 * @see LIBMTP_Update_Playlist()
Linus Walleij8d8c4352008-09-23 23:04:16 +00007064 */
7065int LIBMTP_Set_Playlist_Name(LIBMTP_mtpdevice_t *device,
7066 LIBMTP_playlist_t *playlist, const char* newname)
7067{
7068 int ret;
7069
7070 ret = set_object_filename(device, playlist->playlist_id,
7071 PTP_OFC_MTP_AbstractAudioVideoPlaylist,
7072 &newname);
7073
7074 if (ret != 0) {
7075 return ret;
7076 }
7077
7078 free(playlist->name);
7079 playlist->name = strdup(newname);
7080 return ret;
7081}
7082
7083/**
7084 * This function renames a single album.
Linus Walleij12bbf312009-01-26 21:46:51 +00007085 * This simply means that the <code>PTP_OPC_ObjectFileName</code>
7086 * property is updated, if this is supported by the device.
7087 * The album filename should nominally end with an extension
7088 * like ".alb".
7089 *
7090 * NOTE: if you want to change the metadata the device display
7091 * about a playlist you must <i>not</i> use this function,
7092 * use <code>LIBMTP_Update_Album()</code> instead!
Linus Walleij8d8c4352008-09-23 23:04:16 +00007093 *
7094 * @param device a pointer to the device that contains the file.
7095 * @param album the album metadata of the album to rename.
7096 * On success, the name member is updated. Be aware, that
7097 * this name can be different than newname depending of device restrictions.
7098 * @param newname the new name for this object.
7099 * @return 0 on success, any other value means failure.
Linus Walleij12bbf312009-01-26 21:46:51 +00007100 * @see LIBMTP_Update_Album()
Linus Walleij8d8c4352008-09-23 23:04:16 +00007101 */
7102int LIBMTP_Set_Album_Name(LIBMTP_mtpdevice_t *device,
7103 LIBMTP_album_t *album, const char* newname)
7104{
7105 int ret;
7106
7107 ret = set_object_filename(device, album->album_id,
7108 PTP_OFC_MTP_AbstractAudioAlbum,
7109 &newname);
7110
7111 if (ret != 0) {
7112 return ret;
7113 }
7114
7115 free(album->name);
7116 album->name = strdup(newname);
7117 return ret;
7118}
7119
7120/**
7121 * THIS FUNCTION IS DEPRECATED. PLEASE UPDATE YOUR CODE IN ORDER
7122 * NOT TO USE IT.
7123 *
7124 * @see LIBMTP_Set_File_Name()
7125 * @see LIBMTP_Set_Track_Name()
7126 * @see LIBMTP_Set_Folder_Name()
7127 * @see LIBMTP_Set_Playlist_Name()
7128 * @see LIBMTP_Set_Album_Name()
7129 */
7130int LIBMTP_Set_Object_Filename(LIBMTP_mtpdevice_t *device,
7131 uint32_t object_id, char* newname)
7132{
7133 int ret;
7134 LIBMTP_file_t *file;
7135
7136 file = LIBMTP_Get_Filemetadata(device, object_id);
Linus Walleijfb7212f2008-09-24 20:30:13 +00007137
7138 if (file == NULL) {
7139 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Set_Object_Filename(): "
7140 "could not get file metadata for target object.");
7141 return -1;
7142 }
Linus Walleij08a5fe12009-09-08 21:28:58 +00007143
Linus Walleijfb7212f2008-09-24 20:30:13 +00007144 ret = set_object_filename(device, object_id, map_libmtp_type_to_ptp_type(file->filetype), (const char **) &newname);
Linus Walleij08a5fe12009-09-08 21:28:58 +00007145
Linus Walleij8d8c4352008-09-23 23:04:16 +00007146 free(file);
7147
7148 return ret;
7149}
7150
7151/**
Linus Walleij9c6ca022006-04-21 10:24:15 +00007152 * Helper function. This indicates if a track exists on the device
7153 * @param device a pointer to the device to get the track from.
7154 * @param id the track ID of the track to retrieve.
Linus Walleij070e9b42007-01-22 08:49:28 +00007155 * @return TRUE (!=0) if the track exists, FALSE (0) if not
Linus Walleij9c6ca022006-04-21 10:24:15 +00007156 */
7157int LIBMTP_Track_Exists(LIBMTP_mtpdevice_t *device,
7158 uint32_t const id)
7159{
Linus Walleij9c6ca022006-04-21 10:24:15 +00007160 PTPParams *params = (PTPParams *) device->params;
Linus Walleijd4637502009-06-14 23:03:33 +00007161 uint16_t ret;
7162 PTPObject *ob;
Linus Walleij9c6ca022006-04-21 10:24:15 +00007163
Linus Walleijd4637502009-06-14 23:03:33 +00007164 ret = ptp_object_want (params, id, 0, &ob);
7165 if (ret == PTP_RC_OK)
Linus Walleijf0bf4372007-07-01 21:47:38 +00007166 return -1;
Linus Walleij9c6ca022006-04-21 10:24:15 +00007167 return 0;
7168}
7169
7170/**
7171 * This creates a new folder structure and allocates memory
7172 * for it. Notice that if you add strings to this structure they
7173 * will be freed by the corresponding <code>LIBMTP_folder_track_t</code>
7174 * operation later, so be careful of using strdup() when assigning
7175 * strings, e.g.:
7176 *
7177 * @return a pointer to the newly allocated folder structure.
7178 * @see LIBMTP_destroy_folder_t()
7179 */
7180LIBMTP_folder_t *LIBMTP_new_folder_t(void)
7181{
7182 LIBMTP_folder_t *new = (LIBMTP_folder_t *) malloc(sizeof(LIBMTP_folder_t));
7183 if (new == NULL) {
7184 return NULL;
7185 }
7186 new->folder_id = 0;
7187 new->parent_id = 0;
Linus Walleijea68f1f2008-06-22 21:54:44 +00007188 new->storage_id = 0;
Linus Walleij9c6ca022006-04-21 10:24:15 +00007189 new->name = NULL;
7190 new->sibling = NULL;
7191 new->child = NULL;
7192 return new;
7193}
7194
7195/**
Linus Walleij5573def2007-04-23 07:04:18 +00007196 * This recursively deletes the memory for a folder structure.
7197 * This shall typically be called on a top-level folder list to
7198 * detsroy the entire folder tree.
Linus Walleij9c6ca022006-04-21 10:24:15 +00007199 *
7200 * @param folder folder structure to destroy
7201 * @see LIBMTP_new_folder_t()
7202 */
7203void LIBMTP_destroy_folder_t(LIBMTP_folder_t *folder)
7204{
7205
7206 if(folder == NULL) {
7207 return;
7208 }
7209
7210 //Destroy from the bottom up
7211 if(folder->child != NULL) {
7212 LIBMTP_destroy_folder_t(folder->child);
7213 }
7214
7215 if(folder->sibling != NULL) {
7216 LIBMTP_destroy_folder_t(folder->sibling);
7217 }
7218
7219 if(folder->name != NULL) {
7220 free(folder->name);
7221 }
7222
7223 free(folder);
7224}
7225
7226/**
7227 * Helper function. Returns a folder structure for a
7228 * specified id.
7229 *
7230 * @param folderlist list of folders to search
7231 * @id id of folder to look for
7232 * @return a folder or NULL if not found
7233 */
7234LIBMTP_folder_t *LIBMTP_Find_Folder(LIBMTP_folder_t *folderlist, uint32_t id)
7235{
7236 LIBMTP_folder_t *ret = NULL;
mopoke96143402006-10-30 04:37:26 +00007237
Linus Walleij9c6ca022006-04-21 10:24:15 +00007238 if(folderlist == NULL) {
7239 return NULL;
7240 }
mopoke96143402006-10-30 04:37:26 +00007241
Linus Walleij9c6ca022006-04-21 10:24:15 +00007242 if(folderlist->folder_id == id) {
7243 return folderlist;
7244 }
mopoke96143402006-10-30 04:37:26 +00007245
Linus Walleij9c6ca022006-04-21 10:24:15 +00007246 if(folderlist->sibling) {
7247 ret = LIBMTP_Find_Folder(folderlist->sibling, id);
7248 }
mopoke96143402006-10-30 04:37:26 +00007249
Linus Walleij9c6ca022006-04-21 10:24:15 +00007250 if(folderlist->child && ret == NULL) {
7251 ret = LIBMTP_Find_Folder(folderlist->child, id);
7252 }
mopoke96143402006-10-30 04:37:26 +00007253
Linus Walleij9c6ca022006-04-21 10:24:15 +00007254 return ret;
7255}
7256
7257/**
Linus Walleij4d0deea2007-10-19 21:27:48 +00007258 * Function used to recursively get subfolders from params.
Linus Walleij9c6ca022006-04-21 10:24:15 +00007259 */
Linus Walleija89a7942008-12-14 23:19:34 +00007260static LIBMTP_folder_t *get_subfolders_for_folder(LIBMTP_folder_t *list, uint32_t parent)
Linus Walleij9c6ca022006-04-21 10:24:15 +00007261{
Linus Walleij4fc19172010-01-19 00:10:26 +00007262 LIBMTP_folder_t *retfolders = NULL;
7263 LIBMTP_folder_t *children, *iter, *curr;
mopoke96143402006-10-30 04:37:26 +00007264
Linus Walleija89a7942008-12-14 23:19:34 +00007265 iter = list->sibling;
7266 while(iter != list) {
7267 if (iter->parent_id != parent) {
7268 iter = iter->sibling;
7269 continue;
7270 }
7271
7272 /* We know that iter is a child of 'parent', therefore we can safely
7273 * hold on to 'iter' locally since no one else will steal it
7274 * from the 'list' as we recurse. */
7275 children = get_subfolders_for_folder(list, iter->folder_id);
7276
7277 curr = iter;
7278 iter = iter->sibling;
7279
7280 // Remove curr from the list.
7281 curr->child->sibling = curr->sibling;
7282 curr->sibling->child = curr->child;
Linus Walleij08a5fe12009-09-08 21:28:58 +00007283
Linus Walleija89a7942008-12-14 23:19:34 +00007284 // Attach the children to curr.
7285 curr->child = children;
Linus Walleij08a5fe12009-09-08 21:28:58 +00007286
Linus Walleija89a7942008-12-14 23:19:34 +00007287 // Put this folder into the list of siblings.
7288 curr->sibling = retfolders;
7289 retfolders = curr;
7290 }
7291
7292 return retfolders;
7293}
7294
7295/**
7296 * This returns a list of all folders available
7297 * on the current MTP device.
7298 *
7299 * @param device a pointer to the device to get the folder listing for.
Linus Walleij1e4dfea2011-03-05 22:50:39 +01007300 * @param storage a storage ID to get the folder list from
Linus Walleija89a7942008-12-14 23:19:34 +00007301 * @return a list of folders
7302 */
Linus Walleij1e4dfea2011-03-05 22:50:39 +01007303 LIBMTP_folder_t *LIBMTP_Get_Folder_List_For_Storage(LIBMTP_mtpdevice_t *device,
7304 uint32_t const storage)
Linus Walleija89a7942008-12-14 23:19:34 +00007305{
7306 PTPParams *params = (PTPParams *) device->params;
Linus Walleijcdc12092010-09-18 11:28:56 +00007307 LIBMTP_folder_t head, *rv;
Linus Walleija89a7942008-12-14 23:19:34 +00007308 int i;
7309
7310 // Get all the handles if we haven't already done that
Linus Walleijd4637502009-06-14 23:03:33 +00007311 if (params->nrofobjects == 0) {
Linus Walleija89a7942008-12-14 23:19:34 +00007312 flush_handles(device);
7313 }
7314
Linus Walleij4ab47b22008-12-14 23:24:26 +00007315 /*
7316 * This creates a temporary list of the folders, this is in a
7317 * reverse order and uses the Folder pointers that are already
7318 * in the Folder structure. From this we can then build up the
7319 * folder hierarchy with only looking at this temporary list,
7320 * and removing the folders from this temporary list as we go.
7321 * This significantly reduces the number of operations that we
7322 * have to do in building the folder hierarchy. Also since the
7323 * temp list is in reverse order, when we prepend to the sibling
7324 * list things are in the same order as they were originally
7325 * in the handle list.
7326 */
Linus Walleija89a7942008-12-14 23:19:34 +00007327 head.sibling = &head;
7328 head.child = &head;
Linus Walleijd4637502009-06-14 23:03:33 +00007329 for (i = 0; i < params->nrofobjects; i++) {
Linus Walleij9c6ca022006-04-21 10:24:15 +00007330 LIBMTP_folder_t *folder;
Linus Walleijd4637502009-06-14 23:03:33 +00007331 PTPObject *ob;
Linus Walleija89a7942008-12-14 23:19:34 +00007332
Linus Walleijd4637502009-06-14 23:03:33 +00007333 ob = &params->objects[i];
7334 if (ob->oi.ObjectFormat != PTP_OFC_Association) {
Linus Walleijf0bf4372007-07-01 21:47:38 +00007335 continue;
7336 }
Linus Walleij1e4dfea2011-03-05 22:50:39 +01007337
7338 if (storage != PTP_GOH_ALL_STORAGE && storage != ob->oi.StorageID) {
7339 continue;
7340 }
7341
Linus Walleij4ab47b22008-12-14 23:24:26 +00007342 /*
7343 * Do we know how to handle these? They are part
7344 * of the MTP 1.0 specification paragraph 3.6.4.
Linus Walleij08a5fe12009-09-08 21:28:58 +00007345 * For AssociationDesc 0x00000001U ptp_mtp_getobjectreferences()
7346 * should be called on these to get the contained objects, but
Linus Walleij4ab47b22008-12-14 23:24:26 +00007347 * we basically don't care. Hopefully parent_id is maintained for all
7348 * children, because we rely on that instead.
7349 */
Linus Walleijd4637502009-06-14 23:03:33 +00007350 if (ob->oi.AssociationDesc != 0x00000000U) {
nicklas79daadbf22009-09-28 18:19:34 +00007351 LIBMTP_INFO("MTP extended association type 0x%08x encountered\n", ob->oi.AssociationDesc);
Linus Walleijd71d0b32008-09-22 08:21:03 +00007352 }
7353
Linus Walleij4d0deea2007-10-19 21:27:48 +00007354 // Create a folder struct...
Linus Walleijf0bf4372007-07-01 21:47:38 +00007355 folder = LIBMTP_new_folder_t();
Linus Walleij4d0deea2007-10-19 21:27:48 +00007356 if (folder == NULL) {
7357 // malloc failure or so.
7358 return NULL;
7359 }
Linus Walleijd4637502009-06-14 23:03:33 +00007360 folder->folder_id = ob->oid;
7361 folder->parent_id = ob->oi.ParentObject;
7362 folder->storage_id = ob->oi.StorageID;
7363 folder->name = (ob->oi.Filename) ? (char *)strdup(ob->oi.Filename) : NULL;
Linus Walleijf0bf4372007-07-01 21:47:38 +00007364
Linus Walleija89a7942008-12-14 23:19:34 +00007365 // pretend sibling says next, and child says prev.
7366 folder->sibling = head.sibling;
7367 folder->child = &head;
7368 head.sibling->child = folder;
7369 head.sibling = folder;
Linus Walleij9c6ca022006-04-21 10:24:15 +00007370 }
Linus Walleij4d0deea2007-10-19 21:27:48 +00007371
Linus Walleij1e4dfea2011-03-05 22:50:39 +01007372 // We begin at the given root folder and get them all recursively
Linus Walleij4fc19172010-01-19 00:10:26 +00007373 rv = get_subfolders_for_folder(&head, 0x00000000U);
7374
7375 // Some buggy devices may have some files in the "root folder"
7376 // 0xffffffff so if 0x00000000 didn't return any folders,
7377 // look for children of the root 0xffffffffU
7378 if (rv == NULL) {
7379 rv = get_subfolders_for_folder(&head, 0xffffffffU);
7380 if (rv != NULL)
7381 LIBMTP_ERROR("Device have files in \"root folder\" 0xffffffffU - "
7382 "this is a firmware bug (but continuing)\n");
7383 }
Linus Walleija89a7942008-12-14 23:19:34 +00007384
7385 // The temp list should be empty. Clean up any orphans just in case.
7386 while(head.sibling != &head) {
7387 LIBMTP_folder_t *curr = head.sibling;
7388
nicklas79daadbf22009-09-28 18:19:34 +00007389 LIBMTP_INFO("Orphan folder with ID: 0x%08x name: \"%s\" encountered.\n",
Linus Walleija89a7942008-12-14 23:19:34 +00007390 curr->folder_id,
7391 curr->name);
7392 curr->sibling->child = curr->child;
7393 curr->child->sibling = curr->sibling;
7394 curr->child = NULL;
7395 curr->sibling = NULL;
7396 LIBMTP_destroy_folder_t(curr);
7397 }
7398
7399 return rv;
Linus Walleij4d0deea2007-10-19 21:27:48 +00007400}
7401
7402/**
Linus Walleij1e4dfea2011-03-05 22:50:39 +01007403 * This returns a list of all folders available
7404 * on the current MTP device.
7405 *
7406 * @param device a pointer to the device to get the folder listing for.
7407 * @return a list of folders
7408 */
7409LIBMTP_folder_t *LIBMTP_Get_Folder_List(LIBMTP_mtpdevice_t *device)
7410{
7411 return LIBMTP_Get_Folder_List_For_Storage(device, PTP_GOH_ALL_STORAGE);
7412}
7413
7414/**
Linus Walleijc86afbd2006-05-04 19:05:24 +00007415 * This create a folder on the current MTP device. The PTP name
7416 * for a folder is "association". The PTP/MTP devices does not
7417 * have an internal "folder" concept really, it contains a flat
7418 * list of all files and some file are "associations" that other
7419 * files and folders may refer to as its "parent".
Linus Walleij9c6ca022006-04-21 10:24:15 +00007420 *
Linus Walleijc86afbd2006-05-04 19:05:24 +00007421 * @param device a pointer to the device to create the folder on.
Richard Lowf8cd3d72009-05-04 16:38:33 +00007422 * @param name the name of the new folder. Note this can be modified
7423 * if the device does not support all the characters in the
7424 * name.
Linus Walleijc86afbd2006-05-04 19:05:24 +00007425 * @param parent_id id of parent folder to add the new folder to,
Reverend Homer8e3af292015-11-22 19:58:01 +03007426 * or 0xFFFFFFFF to put it in the root directory.
Linus Walleijea68f1f2008-06-22 21:54:44 +00007427 * @param storage_id id of the storage to add this new folder to.
7428 * notice that you cannot mismatch storage id and parent id:
7429 * they must both be on the same storage! Pass in 0 if you
7430 * want to create this folder on the default storage.
Linus Walleijc86afbd2006-05-04 19:05:24 +00007431 * @return id to new folder or 0 if an error occured
Linus Walleij9c6ca022006-04-21 10:24:15 +00007432 */
Richard Lowf8cd3d72009-05-04 16:38:33 +00007433uint32_t LIBMTP_Create_Folder(LIBMTP_mtpdevice_t *device, char *name,
Linus Walleijea68f1f2008-06-22 21:54:44 +00007434 uint32_t parent_id, uint32_t storage_id)
Linus Walleij9c6ca022006-04-21 10:24:15 +00007435{
7436 PTPParams *params = (PTPParams *) device->params;
Linus Walleijd3b78572007-08-24 21:28:24 +00007437 PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo;
Linus Walleij9c6ca022006-04-21 10:24:15 +00007438 uint32_t parenthandle = 0;
Linus Walleijea68f1f2008-06-22 21:54:44 +00007439 uint32_t store;
Linus Walleij9c6ca022006-04-21 10:24:15 +00007440 PTPObjectInfo new_folder;
Linus Walleij438bd7f2006-06-08 11:35:44 +00007441 uint16_t ret;
Linus Walleij9c6ca022006-04-21 10:24:15 +00007442 uint32_t new_id = 0;
7443
Linus Walleijea68f1f2008-06-22 21:54:44 +00007444 if (storage_id == 0) {
Linus Walleijd71d0b32008-09-22 08:21:03 +00007445 // I'm just guessing that a folder may require 512 bytes
Linus Walleij88babc82014-06-02 21:32:14 +02007446 store = get_suggested_storage_id(device, 512, parent_id);
Linus Walleijea68f1f2008-06-22 21:54:44 +00007447 } else {
7448 store = storage_id;
7449 }
7450 parenthandle = parent_id;
7451
Richard Low59426042009-05-04 16:40:09 +00007452 memset(&new_folder, 0, sizeof(new_folder));
Richard Lowf8cd3d72009-05-04 16:38:33 +00007453 new_folder.Filename = name;
Linus Walleijfec4d562008-06-01 22:30:36 +00007454 if (FLAG_ONLY_7BIT_FILENAMES(ptp_usb)) {
Linus Walleijd3b78572007-08-24 21:28:24 +00007455 strip_7bit_from_utf8(new_folder.Filename);
7456 }
Linus Walleij755c2462011-03-04 20:11:01 +01007457 new_folder.ObjectCompressedSize = 0;
Linus Walleij9c6ca022006-04-21 10:24:15 +00007458 new_folder.ObjectFormat = PTP_OFC_Association;
Linus Walleijdbbfecc2008-11-05 09:52:47 +00007459 new_folder.ProtectionStatus = PTP_PS_NoProtection;
Linus Walleij753a46a2008-11-05 09:10:52 +00007460 new_folder.AssociationType = PTP_AT_GenericFolder;
Linus Walleij9c6ca022006-04-21 10:24:15 +00007461 new_folder.ParentObject = parent_id;
Linus Walleijea68f1f2008-06-22 21:54:44 +00007462 new_folder.StorageID = store;
Linus Walleij9c6ca022006-04-21 10:24:15 +00007463
Linus Walleij9c6ca022006-04-21 10:24:15 +00007464 // Create the object
Ignacio Martinez5ab67472012-03-22 21:59:22 +01007465 if (!(params->device_flags & DEVICE_FLAG_BROKEN_SEND_OBJECT_PROPLIST) &&
7466 ptp_operation_issupported(params,PTP_OC_MTP_SendObjectPropList)) {
7467 MTPProperties *props = (MTPProperties*)calloc(2,sizeof(MTPProperties));
7468
7469 props[0].property = PTP_OPC_ObjectFileName;
7470 props[0].datatype = PTP_DTC_STR;
7471 props[0].propval.str = name;
7472
7473 props[1].property = PTP_OPC_Name;
7474 props[1].datatype = PTP_DTC_STR;
7475 props[1].propval.str = name;
7476
7477 ret = ptp_mtp_sendobjectproplist(params, &store, &parenthandle, &new_id, PTP_OFC_Association,
7478 0, props, 1);
7479 free(props);
7480 } else {
7481 ret = ptp_sendobjectinfo(params, &store, &parenthandle, &new_id, &new_folder);
7482 }
7483
Linus Walleij9c6ca022006-04-21 10:24:15 +00007484 if (ret != PTP_RC_OK) {
Linus Walleij070e9b42007-01-22 08:49:28 +00007485 add_ptp_error_to_errorstack(device, ret, "LIBMTP_Create_Folder: Could not send object info.");
Linus Walleij99310d42006-11-01 08:29:39 +00007486 if (ret == PTP_RC_AccessDenied) {
Linus Walleij070e9b42007-01-22 08:49:28 +00007487 add_ptp_error_to_errorstack(device, ret, "ACCESS DENIED.");
Linus Walleij99310d42006-11-01 08:29:39 +00007488 }
Linus Walleijc86afbd2006-05-04 19:05:24 +00007489 return 0;
Linus Walleij9c6ca022006-04-21 10:24:15 +00007490 }
Linus Walleij08a5fe12009-09-08 21:28:58 +00007491 // NOTE: don't destroy the new_folder objectinfo, because it is statically referencing
Richard Lowf8cd3d72009-05-04 16:38:33 +00007492 // several strings.
tsaarnia3eb60a2007-07-06 17:41:30 +00007493
Linus Walleija6d0d482007-10-31 08:54:56 +00007494 add_object_to_cache(device, new_id);
tsaarnia3eb60a2007-07-06 17:41:30 +00007495
Linus Walleij9c6ca022006-04-21 10:24:15 +00007496 return new_id;
7497}
raveloxd9a28642006-05-26 23:42:22 +00007498
Linus Walleij438bd7f2006-06-08 11:35:44 +00007499/**
7500 * This creates a new playlist metadata structure and allocates memory
7501 * for it. Notice that if you add strings to this structure they
7502 * will be freed by the corresponding <code>LIBMTP_destroy_playlist_t</code>
mopoke96143402006-10-30 04:37:26 +00007503 * operation later, so be careful of using strdup() when assigning
Linus Walleij438bd7f2006-06-08 11:35:44 +00007504 * strings, e.g.:
7505 *
7506 * <pre>
7507 * LIBMTP_playlist_t *pl = LIBMTP_new_playlist_t();
7508 * pl->name = strdup(str);
7509 * ....
7510 * LIBMTP_destroy_playlist_t(pl);
7511 * </pre>
7512 *
7513 * @return a pointer to the newly allocated metadata structure.
7514 * @see LIBMTP_destroy_playlist_t()
7515 */
7516LIBMTP_playlist_t *LIBMTP_new_playlist_t(void)
7517{
7518 LIBMTP_playlist_t *new = (LIBMTP_playlist_t *) malloc(sizeof(LIBMTP_playlist_t));
7519 if (new == NULL) {
7520 return NULL;
7521 }
7522 new->playlist_id = 0;
Linus Walleij5ce59db2008-03-12 21:22:58 +00007523 new->parent_id = 0;
Linus Walleijea68f1f2008-06-22 21:54:44 +00007524 new->storage_id = 0;
Linus Walleij438bd7f2006-06-08 11:35:44 +00007525 new->name = NULL;
7526 new->tracks = NULL;
7527 new->no_tracks = 0;
7528 new->next = NULL;
7529 return new;
7530}
7531
7532/**
7533 * This destroys a playlist metadata structure and deallocates the memory
mopoke96143402006-10-30 04:37:26 +00007534 * used by it, including any strings. Never use a track metadata
Linus Walleij438bd7f2006-06-08 11:35:44 +00007535 * structure again after calling this function on it.
7536 * @param playlist the playlist metadata to destroy.
7537 * @see LIBMTP_new_playlist_t()
7538 */
7539void LIBMTP_destroy_playlist_t(LIBMTP_playlist_t *playlist)
7540{
7541 if (playlist == NULL) {
7542 return;
7543 }
7544 if (playlist->name != NULL)
7545 free(playlist->name);
7546 if (playlist->tracks != NULL)
7547 free(playlist->tracks);
7548 free(playlist);
7549 return;
7550}
7551
7552/**
7553 * This function returns a list of the playlists available on the
7554 * device. Typical usage:
7555 *
7556 * <pre>
7557 * </pre>
7558 *
7559 * @param device a pointer to the device to get the playlist listing from.
7560 * @return a playlist list on success, else NULL. If there are no playlists
7561 * on the device, NULL will be returned as well.
Linus Walleij2e4b5f92006-06-16 14:00:49 +00007562 * @see LIBMTP_Get_Playlist()
Linus Walleij438bd7f2006-06-08 11:35:44 +00007563 */
7564LIBMTP_playlist_t *LIBMTP_Get_Playlist_List(LIBMTP_mtpdevice_t *device)
7565{
Linus Walleijf3c44052008-08-16 21:14:56 +00007566 PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo;
7567 const int REQ_SPL = FLAG_PLAYLIST_SPL(ptp_usb);
Linus Walleij438bd7f2006-06-08 11:35:44 +00007568 PTPParams *params = (PTPParams *) device->params;
7569 LIBMTP_playlist_t *retlists = NULL;
7570 LIBMTP_playlist_t *curlist = NULL;
7571 uint32_t i;
7572
7573 // Get all the handles if we haven't already done that
Linus Walleijd4637502009-06-14 23:03:33 +00007574 if (params->nrofobjects == 0) {
Linus Walleij438bd7f2006-06-08 11:35:44 +00007575 flush_handles(device);
7576 }
7577
Linus Walleijd4637502009-06-14 23:03:33 +00007578 for (i = 0; i < params->nrofobjects; i++) {
Linus Walleij438bd7f2006-06-08 11:35:44 +00007579 LIBMTP_playlist_t *pl;
Linus Walleijd4637502009-06-14 23:03:33 +00007580 PTPObject *ob;
Linus Walleij438bd7f2006-06-08 11:35:44 +00007581 uint16_t ret;
mopoke96143402006-10-30 04:37:26 +00007582
Linus Walleijd4637502009-06-14 23:03:33 +00007583 ob = &params->objects[i];
mopoke96143402006-10-30 04:37:26 +00007584
Linus Walleijf0bf4372007-07-01 21:47:38 +00007585 // Ignore stuff that isn't playlists
Linus Walleijf3c44052008-08-16 21:14:56 +00007586
7587 // For Samsung players we must look for the .spl extension explicitly since
7588 // playlists are not stored as playlist objects.
Linus Walleijd4637502009-06-14 23:03:33 +00007589 if ( REQ_SPL && is_spl_playlist(&ob->oi) ) {
Linus Walleijf3c44052008-08-16 21:14:56 +00007590 // Allocate a new playlist type
7591 pl = LIBMTP_new_playlist_t();
Linus Walleijd4637502009-06-14 23:03:33 +00007592 spl_to_playlist_t(device, &ob->oi, ob->oid, pl);
Linus Walleijf3c44052008-08-16 21:14:56 +00007593 }
Linus Walleijd4637502009-06-14 23:03:33 +00007594 else if ( ob->oi.ObjectFormat != PTP_OFC_MTP_AbstractAudioVideoPlaylist ) {
Linus Walleijf0bf4372007-07-01 21:47:38 +00007595 continue;
Linus Walleij438bd7f2006-06-08 11:35:44 +00007596 }
Linus Walleijf3c44052008-08-16 21:14:56 +00007597 else {
7598 // Allocate a new playlist type
7599 pl = LIBMTP_new_playlist_t();
Linus Walleijf0bf4372007-07-01 21:47:38 +00007600
Linus Walleijf3296622008-09-04 20:53:56 +00007601 // Try to look up proper name, else use the oi->Filename field.
Linus Walleijd4637502009-06-14 23:03:33 +00007602 pl->name = get_string_from_object(device, ob->oid, PTP_OPC_Name);
Linus Walleijf3296622008-09-04 20:53:56 +00007603 if (pl->name == NULL) {
Linus Walleijd4637502009-06-14 23:03:33 +00007604 pl->name = strdup(ob->oi.Filename);
Linus Walleijf3296622008-09-04 20:53:56 +00007605 }
Linus Walleijd4637502009-06-14 23:03:33 +00007606 pl->playlist_id = ob->oid;
7607 pl->parent_id = ob->oi.ParentObject;
7608 pl->storage_id = ob->oi.StorageID;
Linus Walleijf0bf4372007-07-01 21:47:38 +00007609
Linus Walleijf3c44052008-08-16 21:14:56 +00007610 // Then get the track listing for this playlist
7611 ret = ptp_mtp_getobjectreferences(params, pl->playlist_id, &pl->tracks, &pl->no_tracks);
7612 if (ret != PTP_RC_OK) {
Linus Walleij8d8c4352008-09-23 23:04:16 +00007613 add_ptp_error_to_errorstack(device, ret, "LIBMTP_Get_Playlist_List(): "
7614 "could not get object references.");
Linus Walleijf3c44052008-08-16 21:14:56 +00007615 pl->tracks = NULL;
7616 pl->no_tracks = 0;
7617 }
Linus Walleijf0bf4372007-07-01 21:47:38 +00007618 }
Linus Walleij08a5fe12009-09-08 21:28:58 +00007619
Linus Walleijf0bf4372007-07-01 21:47:38 +00007620 // Add playlist to a list that will be returned afterwards.
7621 if (retlists == NULL) {
7622 retlists = pl;
7623 curlist = pl;
7624 } else {
7625 curlist->next = pl;
7626 curlist = pl;
7627 }
Linus Walleij08a5fe12009-09-08 21:28:58 +00007628
Linus Walleijf0bf4372007-07-01 21:47:38 +00007629 // Call callback here if we decide to add that possibility...
mopoke96143402006-10-30 04:37:26 +00007630 }
Linus Walleij438bd7f2006-06-08 11:35:44 +00007631 return retlists;
7632}
7633
Linus Walleij2e4b5f92006-06-16 14:00:49 +00007634
7635/**
7636 * This function retrieves an individual playlist from the device.
7637 * @param device a pointer to the device to get the playlist from.
7638 * @param plid the unique ID of the playlist to retrieve.
7639 * @return a valid playlist metadata post or NULL on failure.
7640 * @see LIBMTP_Get_Playlist_List()
7641 */
7642LIBMTP_playlist_t *LIBMTP_Get_Playlist(LIBMTP_mtpdevice_t *device, uint32_t const plid)
7643{
Linus Walleijf3c44052008-08-16 21:14:56 +00007644 PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo;
7645 const int REQ_SPL = FLAG_PLAYLIST_SPL(ptp_usb);
Linus Walleij2e4b5f92006-06-16 14:00:49 +00007646 PTPParams *params = (PTPParams *) device->params;
Linus Walleijd4637502009-06-14 23:03:33 +00007647 PTPObject *ob;
7648 LIBMTP_playlist_t *pl;
7649 uint16_t ret;
Linus Walleij2e4b5f92006-06-16 14:00:49 +00007650
7651 // Get all the handles if we haven't already done that
Linus Walleijd4637502009-06-14 23:03:33 +00007652 if (params->nrofobjects == 0) {
Linus Walleij2e4b5f92006-06-16 14:00:49 +00007653 flush_handles(device);
7654 }
7655
Linus Walleijd4637502009-06-14 23:03:33 +00007656 ret = ptp_object_want (params, plid, PTPOBJECT_OBJECTINFO_LOADED, &ob);
7657 if (ret != PTP_RC_OK)
7658 return NULL;
Linus Walleij2e4b5f92006-06-16 14:00:49 +00007659
Linus Walleijd4637502009-06-14 23:03:33 +00007660 // For Samsung players we must look for the .spl extension explicitly since
7661 // playlists are not stored as playlist objects.
7662 if ( REQ_SPL && is_spl_playlist(&ob->oi) ) {
Linus Walleijf0bf4372007-07-01 21:47:38 +00007663 // Allocate a new playlist type
7664 pl = LIBMTP_new_playlist_t();
Linus Walleijd4637502009-06-14 23:03:33 +00007665 spl_to_playlist_t(device, &ob->oi, ob->oid, pl);
Linus Walleijf0bf4372007-07-01 21:47:38 +00007666 return pl;
mopoke96143402006-10-30 04:37:26 +00007667 }
Linus Walleijd4637502009-06-14 23:03:33 +00007668
7669 // Ignore stuff that isn't playlists
7670 else if ( ob->oi.ObjectFormat != PTP_OFC_MTP_AbstractAudioVideoPlaylist ) {
7671 return NULL;
7672 }
7673
7674 // Allocate a new playlist type
7675 pl = LIBMTP_new_playlist_t();
7676
7677 pl->name = get_string_from_object(device, ob->oid, PTP_OPC_Name);
7678 if (pl->name == NULL) {
7679 pl->name = strdup(ob->oi.Filename);
7680 }
7681 pl->playlist_id = ob->oid;
7682 pl->parent_id = ob->oi.ParentObject;
7683 pl->storage_id = ob->oi.StorageID;
Linus Walleij08a5fe12009-09-08 21:28:58 +00007684
Linus Walleijd4637502009-06-14 23:03:33 +00007685 // Then get the track listing for this playlist
7686 ret = ptp_mtp_getobjectreferences(params, pl->playlist_id, &pl->tracks, &pl->no_tracks);
7687 if (ret != PTP_RC_OK) {
7688 add_ptp_error_to_errorstack(device, ret, "LIBMTP_Get_Playlist(): Could not get object references.");
7689 pl->tracks = NULL;
7690 pl->no_tracks = 0;
7691 }
7692
7693 return pl;
Linus Walleij2e4b5f92006-06-16 14:00:49 +00007694}
7695
Linus Walleij31b74292008-05-02 23:29:06 +00007696/**
Linus Walleijb7426d12006-11-25 23:19:11 +00007697 * This function creates a new abstract list such as a playlist
7698 * or an album.
Linus Walleij08a5fe12009-09-08 21:28:58 +00007699 *
Linus Walleijb7426d12006-11-25 23:19:11 +00007700 * @param device a pointer to the device to create the new abstract list
7701 * on.
7702 * @param name the name of the new abstract list.
Linus Walleij7783b9b2007-09-22 22:10:44 +00007703 * @param artist the artist of the new abstract list or NULL.
7704 * @param genre the genre of the new abstract list or NULL.
Linus Walleijb7426d12006-11-25 23:19:11 +00007705 * @param parenthandle the handle of the parent or 0 for no parent
7706 * i.e. the root folder.
7707 * @param objectformat the abstract list type to create.
7708 * @param suffix the ".foo" (4 characters) suffix to use for the virtual
7709 * "file" created by this operation.
7710 * @param newid a pointer to a variable that will hold the new object
7711 * ID if this call is successful.
7712 * @param tracks an array of tracks to associate with this list.
7713 * @param no_tracks the number of tracks in the list.
Linus Walleij438bd7f2006-06-08 11:35:44 +00007714 * @return 0 on success, any other value means failure.
Linus Walleij438bd7f2006-06-08 11:35:44 +00007715 */
Linus Walleijb7426d12006-11-25 23:19:11 +00007716static int create_new_abstract_list(LIBMTP_mtpdevice_t *device,
7717 char const * const name,
Linus Walleijadce4a52007-04-23 07:28:11 +00007718 char const * const artist,
Linus Walleij31b74292008-05-02 23:29:06 +00007719 char const * const composer,
Linus Walleijadce4a52007-04-23 07:28:11 +00007720 char const * const genre,
Linus Walleijb7426d12006-11-25 23:19:11 +00007721 uint32_t const parenthandle,
Linus Walleijea68f1f2008-06-22 21:54:44 +00007722 uint32_t const storageid,
Linus Walleijb7426d12006-11-25 23:19:11 +00007723 uint16_t const objectformat,
7724 char const * const suffix,
7725 uint32_t * const newid,
7726 uint32_t const * const tracks,
7727 uint32_t const no_tracks)
7728
Linus Walleij438bd7f2006-06-08 11:35:44 +00007729{
Linus Walleijb7426d12006-11-25 23:19:11 +00007730 int i;
7731 int supported = 0;
Linus Walleij438bd7f2006-06-08 11:35:44 +00007732 uint16_t ret;
Linus Walleij1e9a0332007-09-12 19:35:56 +00007733 uint16_t *properties = NULL;
rreardon508705f2006-11-19 21:27:22 +00007734 uint32_t propcnt = 0;
Linus Walleijea68f1f2008-06-22 21:54:44 +00007735 uint32_t store;
Linus Walleij438bd7f2006-06-08 11:35:44 +00007736 uint32_t localph = parenthandle;
Linus Walleijb7426d12006-11-25 23:19:11 +00007737 uint8_t nonconsumable = 0x00U; /* By default it is consumable */
7738 PTPParams *params = (PTPParams *) device->params;
Linus Walleijd3b78572007-08-24 21:28:24 +00007739 PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo;
Linus Walleij438bd7f2006-06-08 11:35:44 +00007740 char fname[256];
Linus Walleij71c79292011-03-05 22:08:07 +01007741 //uint8_t data[2];
Linus Walleij438bd7f2006-06-08 11:35:44 +00007742
Linus Walleijcdc12092010-09-18 11:28:56 +00007743 // NULL check
7744 if (!name) {
7745 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "create_new_abstract_list(): list name was NULL, using default name \"Unknown\"");
7746 return -1;
7747 }
7748
Linus Walleijea68f1f2008-06-22 21:54:44 +00007749 if (storageid == 0) {
Linus Walleijd71d0b32008-09-22 08:21:03 +00007750 // I'm just guessing that an abstract list may require 512 bytes
Linus Walleij88babc82014-06-02 21:32:14 +02007751 store = get_suggested_storage_id(device, 512, localph);
Linus Walleijea68f1f2008-06-22 21:54:44 +00007752 } else {
7753 store = storageid;
7754 }
7755
Linus Walleijb7426d12006-11-25 23:19:11 +00007756 // Check if we can create an object of this type
7757 for ( i=0; i < params->deviceinfo.ImageFormats_len; i++ ) {
7758 if (params->deviceinfo.ImageFormats[i] == objectformat) {
7759 supported = 1;
7760 break;
Linus Walleij438bd7f2006-06-08 11:35:44 +00007761 }
7762 }
Linus Walleijb7426d12006-11-25 23:19:11 +00007763 if (!supported) {
Linus Walleijcdc12092010-09-18 11:28:56 +00007764 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "create_new_abstract_list(): player does not support this abstract type");
nicklas79daadbf22009-09-28 18:19:34 +00007765 LIBMTP_ERROR("Unsupported abstract list type: %04x\n", objectformat);
Linus Walleijb7426d12006-11-25 23:19:11 +00007766 return -1;
7767 }
7768
Richard Low15731fe2007-03-22 20:27:20 +00007769 // add the new suffix if it isn't there
Linus Walleij629fe402006-12-12 23:39:15 +00007770 fname[0] = '\0';
Linus Walleijb7426d12006-11-25 23:19:11 +00007771 if (strlen(name) > strlen(suffix)) {
7772 char const * const suff = &name[strlen(name)-strlen(suffix)];
7773 if (!strcmp(suff, suffix)) {
7774 // Home free.
Linus Walleij629fe402006-12-12 23:39:15 +00007775 strncpy(fname, name, sizeof(fname));
Linus Walleijb7426d12006-11-25 23:19:11 +00007776 }
7777 }
7778 // If it didn't end with "<suffix>" then add that here.
Linus Walleij629fe402006-12-12 23:39:15 +00007779 if (fname[0] == '\0') {
Linus Walleijb7426d12006-11-25 23:19:11 +00007780 strncpy(fname, name, sizeof(fname)-strlen(suffix)-1);
7781 strcat(fname, suffix);
Linus Walleij438bd7f2006-06-08 11:35:44 +00007782 fname[sizeof(fname)-1] = '\0';
Linus Walleij438bd7f2006-06-08 11:35:44 +00007783 }
7784
Linus Walleijf67c1ad2009-01-14 21:39:50 +00007785 if (ptp_operation_issupported(params, PTP_OC_MTP_SendObjectPropList) &&
7786 !FLAG_BROKEN_SEND_OBJECT_PROPLIST(ptp_usb)) {
Linus Walleij1e9a0332007-09-12 19:35:56 +00007787 MTPProperties *props = NULL;
7788 MTPProperties *prop = NULL;
7789 int nrofprops = 0;
Linus Walleij08a5fe12009-09-08 21:28:58 +00007790
Richard Low6c0a6ce2006-11-26 10:42:08 +00007791 *newid = 0x00000000U;
mopoke96143402006-10-30 04:37:26 +00007792
Linus Walleij1e9a0332007-09-12 19:35:56 +00007793 ret = ptp_mtp_getobjectpropssupported(params, objectformat, &propcnt, &properties);
mopoke31364442006-11-20 04:53:04 +00007794
rreardon508705f2006-11-19 21:27:22 +00007795 for (i=0;i<propcnt;i++) {
Linus Walleijdbcc8242007-08-05 22:31:26 +00007796 PTPObjectPropDesc opd;
Linus Walleij08a5fe12009-09-08 21:28:58 +00007797
Linus Walleij1e9a0332007-09-12 19:35:56 +00007798 ret = ptp_mtp_getobjectpropdesc(params, properties[i], objectformat, &opd);
Linus Walleijdbcc8242007-08-05 22:31:26 +00007799 if (ret != PTP_RC_OK) {
7800 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "create_new_abstract_list(): "
7801 "could not get property description.");
7802 } else if (opd.GetSet) {
Linus Walleij1e9a0332007-09-12 19:35:56 +00007803 switch (properties[i]) {
Linus Walleijdbcc8242007-08-05 22:31:26 +00007804 case PTP_OPC_ObjectFileName:
Linus Walleija6d0d482007-10-31 08:54:56 +00007805 prop = ptp_get_new_object_prop_entry(&props,&nrofprops);
Linus Walleij08a5fe12009-09-08 21:28:58 +00007806 prop->ObjectHandle = *newid;
Linus Walleijdbcc8242007-08-05 22:31:26 +00007807 prop->property = PTP_OPC_ObjectFileName;
7808 prop->datatype = PTP_DTC_STR;
7809 prop->propval.str = strdup(fname);
Linus Walleijfec4d562008-06-01 22:30:36 +00007810 if (FLAG_ONLY_7BIT_FILENAMES(ptp_usb)) {
Linus Walleijd3b78572007-08-24 21:28:24 +00007811 strip_7bit_from_utf8(prop->propval.str);
7812 }
Linus Walleijdbcc8242007-08-05 22:31:26 +00007813 break;
7814 case PTP_OPC_ProtectionStatus:
Linus Walleija6d0d482007-10-31 08:54:56 +00007815 prop = ptp_get_new_object_prop_entry(&props,&nrofprops);
Linus Walleijadce4a52007-04-23 07:28:11 +00007816 prop->ObjectHandle = *newid;
Linus Walleijdbcc8242007-08-05 22:31:26 +00007817 prop->property = PTP_OPC_ProtectionStatus;
7818 prop->datatype = PTP_DTC_UINT16;
7819 prop->propval.u16 = 0x0000U; /* Not protected */
Linus Walleijdbcc8242007-08-05 22:31:26 +00007820 break;
7821 case PTP_OPC_NonConsumable:
Linus Walleija6d0d482007-10-31 08:54:56 +00007822 prop = ptp_get_new_object_prop_entry(&props,&nrofprops);
Linus Walleijadce4a52007-04-23 07:28:11 +00007823 prop->ObjectHandle = *newid;
Linus Walleijdbcc8242007-08-05 22:31:26 +00007824 prop->property = PTP_OPC_NonConsumable;
7825 prop->datatype = PTP_DTC_UINT8;
7826 prop->propval.u8 = nonconsumable;
Linus Walleijdbcc8242007-08-05 22:31:26 +00007827 break;
7828 case PTP_OPC_Name:
7829 if (name != NULL) {
Linus Walleija6d0d482007-10-31 08:54:56 +00007830 prop = ptp_get_new_object_prop_entry(&props,&nrofprops);
Linus Walleijdbcc8242007-08-05 22:31:26 +00007831 prop->ObjectHandle = *newid;
7832 prop->property = PTP_OPC_Name;
7833 prop->datatype = PTP_DTC_STR;
7834 prop->propval.str = strdup(name);
Linus Walleijdbcc8242007-08-05 22:31:26 +00007835 }
7836 break;
Linus Walleij0b75ab62007-12-28 00:47:34 +00007837 case PTP_OPC_AlbumArtist:
7838 if (artist != NULL) {
7839 prop = ptp_get_new_object_prop_entry(&props,&nrofprops);
7840 prop->ObjectHandle = *newid;
7841 prop->property = PTP_OPC_AlbumArtist;
7842 prop->datatype = PTP_DTC_STR;
7843 prop->propval.str = strdup(artist);
7844 }
7845 break;
Linus Walleijdbcc8242007-08-05 22:31:26 +00007846 case PTP_OPC_Artist:
7847 if (artist != NULL) {
Linus Walleija6d0d482007-10-31 08:54:56 +00007848 prop = ptp_get_new_object_prop_entry(&props,&nrofprops);
Linus Walleijdbcc8242007-08-05 22:31:26 +00007849 prop->ObjectHandle = *newid;
7850 prop->property = PTP_OPC_Artist;
7851 prop->datatype = PTP_DTC_STR;
7852 prop->propval.str = strdup(artist);
Linus Walleijdbcc8242007-08-05 22:31:26 +00007853 }
7854 break;
Linus Walleij31b74292008-05-02 23:29:06 +00007855 case PTP_OPC_Composer:
7856 if (composer != NULL) {
7857 prop = ptp_get_new_object_prop_entry(&props,&nrofprops);
7858 prop->ObjectHandle = *newid;
7859 prop->property = PTP_OPC_Composer;
7860 prop->datatype = PTP_DTC_STR;
7861 prop->propval.str = strdup(composer);
7862 }
7863 break;
Linus Walleijdbcc8242007-08-05 22:31:26 +00007864 case PTP_OPC_Genre:
7865 if (genre != NULL) {
Linus Walleija6d0d482007-10-31 08:54:56 +00007866 prop = ptp_get_new_object_prop_entry(&props,&nrofprops);
Linus Walleijdbcc8242007-08-05 22:31:26 +00007867 prop->ObjectHandle = *newid;
7868 prop->property = PTP_OPC_Genre;
7869 prop->datatype = PTP_DTC_STR;
7870 prop->propval.str = strdup(genre);
Linus Walleijdbcc8242007-08-05 22:31:26 +00007871 }
7872 break;
Linus Walleij7783b9b2007-09-22 22:10:44 +00007873 case PTP_OPC_DateModified:
7874 // Tag with current time if that is supported
Linus Walleijcf8dc2b2008-10-21 13:58:36 +00007875 if (!FLAG_CANNOT_HANDLE_DATEMODIFIED(ptp_usb)) {
7876 prop = ptp_get_new_object_prop_entry(&props,&nrofprops);
7877 prop->ObjectHandle = *newid;
7878 prop->property = PTP_OPC_DateModified;
7879 prop->datatype = PTP_DTC_STR;
7880 prop->propval.str = get_iso8601_stamp();
7881 }
Linus Walleij7783b9b2007-09-22 22:10:44 +00007882 break;
Linus Walleijadce4a52007-04-23 07:28:11 +00007883 }
rreardon508705f2006-11-19 21:27:22 +00007884 }
Linus Walleijdbcc8242007-08-05 22:31:26 +00007885 ptp_free_objectpropdesc(&opd);
rreardon508705f2006-11-19 21:27:22 +00007886 }
Linus Walleij1e9a0332007-09-12 19:35:56 +00007887 free(properties);
mopoke31364442006-11-20 04:53:04 +00007888
Linus Walleijb7426d12006-11-25 23:19:11 +00007889 ret = ptp_mtp_sendobjectproplist(params, &store, &localph, newid,
Linus Walleij1e9a0332007-09-12 19:35:56 +00007890 objectformat, 0, props, nrofprops);
mopoke31364442006-11-20 04:53:04 +00007891
rreardon508705f2006-11-19 21:27:22 +00007892 /* Free property list */
Linus Walleija6d0d482007-10-31 08:54:56 +00007893 ptp_destroy_object_prop_list(props, nrofprops);
mopoke31364442006-11-20 04:53:04 +00007894
rreardon508705f2006-11-19 21:27:22 +00007895 if (ret != PTP_RC_OK) {
Linus Walleij070e9b42007-01-22 08:49:28 +00007896 add_ptp_error_to_errorstack(device, ret, "create_new_abstract_list(): Could not send object property list.");
rreardon508705f2006-11-19 21:27:22 +00007897 if (ret == PTP_RC_AccessDenied) {
Linus Walleij070e9b42007-01-22 08:49:28 +00007898 add_ptp_error_to_errorstack(device, ret, "ACCESS DENIED.");
rreardon508705f2006-11-19 21:27:22 +00007899 }
7900 return -1;
7901 }
Linus Walleij08a5fe12009-09-08 21:28:58 +00007902
Richard Lowc3f5fc32007-07-29 09:40:50 +00007903 // now send the blank object
Richard Lowa6c924c2006-12-29 17:44:28 +00007904 ret = ptp_sendobject(params, NULL, 0);
7905 if (ret != PTP_RC_OK) {
Linus Walleij070e9b42007-01-22 08:49:28 +00007906 add_ptp_error_to_errorstack(device, ret, "create_new_abstract_list(): Could not send blank object data.");
Richard Lowa6c924c2006-12-29 17:44:28 +00007907 return -1;
7908 }
Linus Walleij629fe402006-12-12 23:39:15 +00007909
rreardon508705f2006-11-19 21:27:22 +00007910 } else if (ptp_operation_issupported(params,PTP_OC_SendObjectInfo)) {
Linus Walleij629fe402006-12-12 23:39:15 +00007911 PTPObjectInfo new_object;
7912
7913 new_object.Filename = fname;
Linus Walleijfec4d562008-06-01 22:30:36 +00007914 if (FLAG_ONLY_7BIT_FILENAMES(ptp_usb)) {
Linus Walleijd3b78572007-08-24 21:28:24 +00007915 strip_7bit_from_utf8(new_object.Filename);
7916 }
Linus Walleij755c2462011-03-04 20:11:01 +01007917 // At one point this had to be one
7918 new_object.ObjectCompressedSize = 0;
Linus Walleij629fe402006-12-12 23:39:15 +00007919 new_object.ObjectFormat = objectformat;
7920
Linus Walleij20698482006-11-24 20:54:21 +00007921 // Create the object
Linus Walleijb7426d12006-11-25 23:19:11 +00007922 ret = ptp_sendobjectinfo(params, &store, &localph, newid, &new_object);
Linus Walleij20698482006-11-24 20:54:21 +00007923 if (ret != PTP_RC_OK) {
Linus Walleij070e9b42007-01-22 08:49:28 +00007924 add_ptp_error_to_errorstack(device, ret, "create_new_abstract_list(): Could not send object info (the playlist itself).");
Linus Walleij20698482006-11-24 20:54:21 +00007925 if (ret == PTP_RC_AccessDenied) {
Linus Walleij070e9b42007-01-22 08:49:28 +00007926 add_ptp_error_to_errorstack(device, ret, "ACCESS DENIED.");
Linus Walleij20698482006-11-24 20:54:21 +00007927 }
7928 return -1;
7929 }
Linus Walleijf0bf4372007-07-01 21:47:38 +00007930 // NOTE: don't destroy new_object objectinfo afterwards - the strings it contains are
7931 // not copies.
Linus Walleij755c2462011-03-04 20:11:01 +01007932
7933#if 0
Richard Lowa6c924c2006-12-29 17:44:28 +00007934 /*
Linus Walleij755c2462011-03-04 20:11:01 +01007935 * At one time we had to send this one blank data byte.
7936 * If we didn't, the handle will not be created and thus there is
7937 * no playlist. Possibly this was masking some bug, so removing it
7938 * now.
Richard Lowa6c924c2006-12-29 17:44:28 +00007939 */
7940 data[0] = '\0';
7941 data[1] = '\0';
7942 ret = ptp_sendobject(params, data, 1);
7943 if (ret != PTP_RC_OK) {
Linus Walleij070e9b42007-01-22 08:49:28 +00007944 add_ptp_error_to_errorstack(device, ret, "create_new_abstract_list(): Could not send blank object data.");
Richard Lowa6c924c2006-12-29 17:44:28 +00007945 return -1;
7946 }
Linus Walleij755c2462011-03-04 20:11:01 +01007947#endif
Linus Walleij08a5fe12009-09-08 21:28:58 +00007948
Richard Lowff16bfa2008-09-26 19:23:08 +00007949 // set the properties one by one
7950 ret = ptp_mtp_getobjectpropssupported(params, objectformat, &propcnt, &properties);
7951
7952 for (i=0;i<propcnt;i++) {
7953 PTPObjectPropDesc opd;
Linus Walleij08a5fe12009-09-08 21:28:58 +00007954
Richard Lowff16bfa2008-09-26 19:23:08 +00007955 ret = ptp_mtp_getobjectpropdesc(params, properties[i], objectformat, &opd);
7956 if (ret != PTP_RC_OK) {
7957 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "create_new_abstract_list(): "
7958 "could not get property description.");
7959 } else if (opd.GetSet) {
7960 switch (properties[i]) {
7961 case PTP_OPC_Name:
7962 if (name != NULL) {
Linus Walleij37588142008-10-16 19:10:47 +00007963 ret = set_object_string(device, *newid, PTP_OPC_Name, name);
7964 if (ret != 0) {
7965 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "create_new_abstract_list(): could not set entity name.");
7966 return -1;
7967 }
7968 }
Richard Lowff16bfa2008-09-26 19:23:08 +00007969 break;
7970 case PTP_OPC_AlbumArtist:
7971 if (artist != NULL) {
Linus Walleij37588142008-10-16 19:10:47 +00007972 ret = set_object_string(device, *newid, PTP_OPC_AlbumArtist, artist);
7973 if (ret != 0) {
7974 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "create_new_abstract_list(): could not set entity album artist.");
7975 return -1;
7976 }
7977 }
Richard Lowff16bfa2008-09-26 19:23:08 +00007978 break;
7979 case PTP_OPC_Artist:
7980 if (artist != NULL) {
Linus Walleij37588142008-10-16 19:10:47 +00007981 ret = set_object_string(device, *newid, PTP_OPC_Artist, artist);
7982 if (ret != 0) {
7983 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "create_new_abstract_list(): could not set entity artist.");
7984 return -1;
7985 }
7986 }
Richard Lowff16bfa2008-09-26 19:23:08 +00007987 break;
7988 case PTP_OPC_Composer:
7989 if (composer != NULL) {
Linus Walleij37588142008-10-16 19:10:47 +00007990 ret = set_object_string(device, *newid, PTP_OPC_Composer, composer);
7991 if (ret != 0) {
7992 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "create_new_abstract_list(): could not set entity composer.");
7993 return -1;
7994 }
7995 }
Richard Lowff16bfa2008-09-26 19:23:08 +00007996 break;
7997 case PTP_OPC_Genre:
7998 if (genre != NULL) {
Linus Walleij37588142008-10-16 19:10:47 +00007999 ret = set_object_string(device, *newid, PTP_OPC_Genre, genre);
8000 if (ret != 0) {
8001 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "create_new_abstract_list(): could not set entity genre.");
8002 return -1;
8003 }
8004 }
Richard Lowff16bfa2008-09-26 19:23:08 +00008005 break;
8006 case PTP_OPC_DateModified:
Linus Walleijcf8dc2b2008-10-21 13:58:36 +00008007 if (!FLAG_CANNOT_HANDLE_DATEMODIFIED(ptp_usb)) {
8008 ret = set_object_string(device, *newid, PTP_OPC_DateModified, get_iso8601_stamp());
8009 if (ret != 0) {
8010 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "create_new_abstract_list(): could not set date modified.");
8011 return -1;
8012 }
Linus Walleij37588142008-10-16 19:10:47 +00008013 }
8014 break;
Richard Lowff16bfa2008-09-26 19:23:08 +00008015 }
8016 }
8017 ptp_free_objectpropdesc(&opd);
8018 }
8019 free(properties);
Linus Walleij438bd7f2006-06-08 11:35:44 +00008020 }
8021
Linus Walleijb7426d12006-11-25 23:19:11 +00008022 if (no_tracks > 0) {
Linus Walleij1ccd5862008-11-11 12:33:01 +00008023 // Add tracks to the list as object references.
Linus Walleijb7426d12006-11-25 23:19:11 +00008024 ret = ptp_mtp_setobjectreferences (params, *newid, (uint32_t *) tracks, no_tracks);
Linus Walleij438bd7f2006-06-08 11:35:44 +00008025 if (ret != PTP_RC_OK) {
Linus Walleij070e9b42007-01-22 08:49:28 +00008026 add_ptp_error_to_errorstack(device, ret, "create_new_abstract_list(): could not add tracks as object references.");
Linus Walleij438bd7f2006-06-08 11:35:44 +00008027 return -1;
8028 }
8029 }
mopoke96143402006-10-30 04:37:26 +00008030
Linus Walleija6d0d482007-10-31 08:54:56 +00008031 add_object_to_cache(device, *newid);
Linus Walleij438bd7f2006-06-08 11:35:44 +00008032
8033 return 0;
8034}
8035
Linus Walleij7783b9b2007-09-22 22:10:44 +00008036/**
Linus Walleij08a5fe12009-09-08 21:28:58 +00008037 * This updates the metadata and track listing
Linus Walleij7783b9b2007-09-22 22:10:44 +00008038 * for an abstract list.
8039 * @param device a pointer to the device that the abstract list
8040 * resides on.
8041 * @param name the name of the abstract list.
8042 * @param artist the artist of the abstract list or NULL.
8043 * @param genre the genre of the abstract list or NULL.
8044 * @param objecthandle the object to be updated.
8045 * @param objectformat the abstract list type to update.
8046 * @param tracks an array of tracks to associate with this list.
8047 * @param no_tracks the number of tracks in the list.
8048 * @return 0 on success, any other value means failure.
8049 */
8050static int update_abstract_list(LIBMTP_mtpdevice_t *device,
8051 char const * const name,
8052 char const * const artist,
Linus Walleij31b74292008-05-02 23:29:06 +00008053 char const * const composer,
Linus Walleij7783b9b2007-09-22 22:10:44 +00008054 char const * const genre,
8055 uint32_t const objecthandle,
8056 uint16_t const objectformat,
8057 uint32_t const * const tracks,
8058 uint32_t const no_tracks)
8059{
8060 uint16_t ret;
8061 PTPParams *params = (PTPParams *) device->params;
Linus Walleijf9267e92007-10-15 21:07:37 +00008062 PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo;
Linus Walleij7783b9b2007-09-22 22:10:44 +00008063 uint16_t *properties = NULL;
8064 uint32_t propcnt = 0;
8065 int i;
Linus Walleij08a5fe12009-09-08 21:28:58 +00008066
Linus Walleij7783b9b2007-09-22 22:10:44 +00008067 // First see which properties can be set
8068 // i.e only try to update this metadata for object tags that exist on the current player.
8069 ret = ptp_mtp_getobjectpropssupported(params, objectformat, &propcnt, &properties);
8070 if (ret != PTP_RC_OK) {
8071 // Just bail out for now, nothing is ever set.
8072 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "update_abstract_list(): "
8073 "could not retrieve supported object properties.");
8074 return -1;
8075 }
Linus Walleijf9267e92007-10-15 21:07:37 +00008076 if (ptp_operation_issupported(params,PTP_OC_MTP_SetObjPropList) &&
Linus Walleijfec4d562008-06-01 22:30:36 +00008077 !FLAG_BROKEN_SET_OBJECT_PROPLIST(ptp_usb)) {
Linus Walleij7783b9b2007-09-22 22:10:44 +00008078 MTPProperties *props = NULL;
8079 MTPProperties *prop = NULL;
8080 int nrofprops = 0;
Linus Walleij08a5fe12009-09-08 21:28:58 +00008081
Linus Walleij7783b9b2007-09-22 22:10:44 +00008082 for (i=0;i<propcnt;i++) {
8083 PTPObjectPropDesc opd;
8084
8085 ret = ptp_mtp_getobjectpropdesc(params, properties[i], objectformat, &opd);
8086 if (ret != PTP_RC_OK) {
8087 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "update_abstract_list(): "
8088 "could not get property description.");
8089 } else if (opd.GetSet) {
8090 switch (properties[i]) {
8091 case PTP_OPC_Name:
Linus Walleija6d0d482007-10-31 08:54:56 +00008092 prop = ptp_get_new_object_prop_entry(&props, &nrofprops);
Linus Walleij08a5fe12009-09-08 21:28:58 +00008093 prop->ObjectHandle = objecthandle;
Linus Walleij7783b9b2007-09-22 22:10:44 +00008094 prop->property = PTP_OPC_Name;
8095 prop->datatype = PTP_DTC_STR;
8096 if (name != NULL)
8097 prop->propval.str = strdup(name);
8098 break;
Linus Walleij0b75ab62007-12-28 00:47:34 +00008099 case PTP_OPC_AlbumArtist:
8100 if (artist != NULL) {
8101 prop = ptp_get_new_object_prop_entry(&props, &nrofprops);
8102 prop->ObjectHandle = objecthandle;
8103 prop->property = PTP_OPC_AlbumArtist;
8104 prop->datatype = PTP_DTC_STR;
8105 prop->propval.str = strdup(artist);
8106 }
8107 break;
Linus Walleij7783b9b2007-09-22 22:10:44 +00008108 case PTP_OPC_Artist:
8109 if (artist != NULL) {
Linus Walleija6d0d482007-10-31 08:54:56 +00008110 prop = ptp_get_new_object_prop_entry(&props, &nrofprops);
Linus Walleij0b75ab62007-12-28 00:47:34 +00008111 prop->ObjectHandle = objecthandle;
Linus Walleij7783b9b2007-09-22 22:10:44 +00008112 prop->property = PTP_OPC_Artist;
8113 prop->datatype = PTP_DTC_STR;
8114 prop->propval.str = strdup(artist);
8115 }
8116 break;
Linus Walleij31b74292008-05-02 23:29:06 +00008117 case PTP_OPC_Composer:
8118 if (composer != NULL) {
8119 prop = ptp_get_new_object_prop_entry(&props, &nrofprops);
8120 prop->ObjectHandle = objecthandle;
8121 prop->property = PTP_OPC_Composer;
8122 prop->datatype = PTP_DTC_STR;
8123 prop->propval.str = strdup(composer);
8124 }
8125 break;
Linus Walleij7783b9b2007-09-22 22:10:44 +00008126 case PTP_OPC_Genre:
8127 if (genre != NULL) {
Linus Walleija6d0d482007-10-31 08:54:56 +00008128 prop = ptp_get_new_object_prop_entry(&props, &nrofprops);
Linus Walleij7783b9b2007-09-22 22:10:44 +00008129 prop->ObjectHandle = objecthandle;
8130 prop->property = PTP_OPC_Genre;
8131 prop->datatype = PTP_DTC_STR;
8132 prop->propval.str = strdup(genre);
8133 }
8134 break;
8135 case PTP_OPC_DateModified:
Linus Walleijcf8dc2b2008-10-21 13:58:36 +00008136 if (!FLAG_CANNOT_HANDLE_DATEMODIFIED(ptp_usb)) {
Linus Walleij37588142008-10-16 19:10:47 +00008137 // Tag with current time if that is supported
8138 prop = ptp_get_new_object_prop_entry(&props, &nrofprops);
8139 prop->ObjectHandle = objecthandle;
8140 prop->property = PTP_OPC_DateModified;
8141 prop->datatype = PTP_DTC_STR;
8142 prop->propval.str = get_iso8601_stamp();
8143 }
Linus Walleij7783b9b2007-09-22 22:10:44 +00008144 break;
8145 default:
8146 break;
8147 }
8148 }
8149 ptp_free_objectpropdesc(&opd);
8150 }
Linus Walleij08a5fe12009-09-08 21:28:58 +00008151
Linus Walleij7783b9b2007-09-22 22:10:44 +00008152 // proplist could be NULL if we can't write any properties
8153 if (props != NULL) {
8154 ret = ptp_mtp_setobjectproplist(params, props, nrofprops);
8155
Linus Walleija6d0d482007-10-31 08:54:56 +00008156 ptp_destroy_object_prop_list(props, nrofprops);
Linus Walleij08a5fe12009-09-08 21:28:58 +00008157
Linus Walleij7783b9b2007-09-22 22:10:44 +00008158 if (ret != PTP_RC_OK) {
8159 // TODO: return error of which property we couldn't set
8160 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "update_abstract_list(): "
8161 "could not set object property list.");
Richard Low016e3732007-09-23 14:53:46 +00008162 free(properties);
Linus Walleij7783b9b2007-09-22 22:10:44 +00008163 return -1;
8164 }
8165 }
Linus Walleij08a5fe12009-09-08 21:28:58 +00008166
Linus Walleij7783b9b2007-09-22 22:10:44 +00008167 } else if (ptp_operation_issupported(params,PTP_OC_MTP_SetObjectPropValue)) {
8168 for (i=0;i<propcnt;i++) {
8169 switch (properties[i]) {
8170 case PTP_OPC_Name:
8171 // Update title
8172 ret = set_object_string(device, objecthandle, PTP_OPC_Name, name);
8173 if (ret != 0) {
8174 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "update_abstract_list(): "
8175 "could not set title.");
8176 }
8177 break;
Linus Walleij0b75ab62007-12-28 00:47:34 +00008178 case PTP_OPC_AlbumArtist:
8179 // Update album artist
8180 ret = set_object_string(device, objecthandle, PTP_OPC_AlbumArtist, artist);
8181 if (ret != 0) {
8182 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "update_abstract_list(): "
8183 "could not set album artist name.");
8184 }
8185 break;
Linus Walleij7783b9b2007-09-22 22:10:44 +00008186 case PTP_OPC_Artist:
8187 // Update artist
8188 ret = set_object_string(device, objecthandle, PTP_OPC_Artist, artist);
8189 if (ret != 0) {
8190 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "update_abstract_list(): "
8191 "could not set artist name.");
8192 }
Marcus Meissner113fa292015-04-09 23:11:52 +02008193 break;
Linus Walleij31b74292008-05-02 23:29:06 +00008194 case PTP_OPC_Composer:
8195 // Update composer
8196 ret = set_object_string(device, objecthandle, PTP_OPC_Composer, composer);
8197 if (ret != 0) {
8198 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "update_abstract_list(): "
8199 "could not set composer name.");
8200 }
Linus Walleij7783b9b2007-09-22 22:10:44 +00008201 break;
8202 case PTP_OPC_Genre:
Linus Walleijb7e8f972010-01-19 00:38:11 +00008203 // Update genre (but only if valid)
8204 if(genre) {
8205 ret = set_object_string(device, objecthandle, PTP_OPC_Genre, genre);
8206 if (ret != 0) {
8207 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "update_abstract_list(): "
8208 "could not set genre.");
8209 }
8210 }
Linus Walleij7783b9b2007-09-22 22:10:44 +00008211 break;
8212 case PTP_OPC_DateModified:
8213 // Update date modified
Linus Walleijcf8dc2b2008-10-21 13:58:36 +00008214 if (!FLAG_CANNOT_HANDLE_DATEMODIFIED(ptp_usb)) {
Linus Walleij7783b9b2007-09-22 22:10:44 +00008215 char *tmpdate = get_iso8601_stamp();
8216 ret = set_object_string(device, objecthandle, PTP_OPC_DateModified, tmpdate);
8217 if (ret != 0) {
8218 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "update_abstract_list(): "
8219 "could not set modification date.");
8220 }
8221 free(tmpdate);
8222 }
Marcus Meissner113fa292015-04-09 23:11:52 +02008223 break;
Linus Walleij7783b9b2007-09-22 22:10:44 +00008224 default:
8225 break;
8226 }
8227 }
Linus Walleij7783b9b2007-09-22 22:10:44 +00008228 } else {
8229 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "update_abstract_list(): "
8230 "Your device doesn't seem to support any known way of setting metadata.");
Richard Low016e3732007-09-23 14:53:46 +00008231 free(properties);
Linus Walleij7783b9b2007-09-22 22:10:44 +00008232 return -1;
8233 }
Linus Walleij08a5fe12009-09-08 21:28:58 +00008234
Linus Walleij7783b9b2007-09-22 22:10:44 +00008235 // Then the object references...
Linus Walleij1ccd5862008-11-11 12:33:01 +00008236 ret = ptp_mtp_setobjectreferences (params, objecthandle, (uint32_t *) tracks, no_tracks);
8237 if (ret != PTP_RC_OK) {
8238 add_ptp_error_to_errorstack(device, ret, "update_abstract_list(): could not add tracks as object references.");
8239 free(properties);
8240 return -1;
Linus Walleij7783b9b2007-09-22 22:10:44 +00008241 }
8242
Richard Low016e3732007-09-23 14:53:46 +00008243 free(properties);
Linus Walleij7752b952007-10-19 20:11:46 +00008244
8245 update_metadata_cache(device, objecthandle);
Linus Walleij08a5fe12009-09-08 21:28:58 +00008246
Linus Walleij7783b9b2007-09-22 22:10:44 +00008247 return 0;
8248}
8249
Linus Walleijb7426d12006-11-25 23:19:11 +00008250
8251/**
8252 * This routine creates a new playlist based on the metadata
8253 * supplied. If the <code>tracks</code> field of the metadata
8254 * contains a track listing, these tracks will be added to the
8255 * playlist.
8256 * @param device a pointer to the device to create the new playlist on.
8257 * @param metadata the metadata for the new playlist. If the function
8258 * exits with success, the <code>playlist_id</code> field of this
8259 * struct will contain the new playlist ID of the playlist.
Linus Walleij2fe43e42009-01-30 20:31:26 +00008260 * <ul>
8261 * <li><code>metadata-&gt;parent_id</code> should be set to the parent
8262 * (e.g. folder) to store this track in. Since some
8263 * devices are a bit picky about where files
8264 * are placed, a default folder will be chosen if libmtp
8265 * has detected one for the current filetype and this
8266 * parameter is set to 0. If this is 0 and no default folder
8267 * can be found, the file will be stored in the root folder.
8268 * <li><code>metadata-&gt;storage_id</code> should be set to the
8269 * desired storage (e.g. memory card or whatever your device
8270 * presents) to store this track in. Setting this to 0 will store
8271 * the track on the primary storage.
8272 * </ul>
Linus Walleijb7426d12006-11-25 23:19:11 +00008273 * @return 0 on success, any other value means failure.
8274 * @see LIBMTP_Update_Playlist()
8275 * @see LIBMTP_Delete_Object()
8276 */
8277int LIBMTP_Create_New_Playlist(LIBMTP_mtpdevice_t *device,
Linus Walleijea68f1f2008-06-22 21:54:44 +00008278 LIBMTP_playlist_t * const metadata)
Linus Walleijb7426d12006-11-25 23:19:11 +00008279{
Linus Walleija4e6bdc2008-05-23 22:31:53 +00008280 PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo;
Linus Walleijea68f1f2008-06-22 21:54:44 +00008281 uint32_t localph = metadata->parent_id;
Linus Walleijb7426d12006-11-25 23:19:11 +00008282
8283 // Use a default folder if none given
8284 if (localph == 0) {
Linus Walleijafac36b2009-01-30 20:39:13 +00008285 if (device->default_playlist_folder != 0)
8286 localph = device->default_playlist_folder;
8287 else
8288 localph = device->default_music_folder;
Linus Walleijb7426d12006-11-25 23:19:11 +00008289 }
Linus Walleij5ce59db2008-03-12 21:22:58 +00008290 metadata->parent_id = localph;
Linus Walleijb7426d12006-11-25 23:19:11 +00008291
Linus Walleijf3c44052008-08-16 21:14:56 +00008292 // Samsung needs its own special type of playlists
8293 if(FLAG_PLAYLIST_SPL(ptp_usb)) {
8294 return playlist_t_to_spl(device, metadata);
8295 }
8296
Linus Walleijb7426d12006-11-25 23:19:11 +00008297 // Just create a new abstract audio/video playlist...
8298 return create_new_abstract_list(device,
8299 metadata->name,
Linus Walleijadce4a52007-04-23 07:28:11 +00008300 NULL,
8301 NULL,
Linus Walleij31b74292008-05-02 23:29:06 +00008302 NULL,
Linus Walleijb7426d12006-11-25 23:19:11 +00008303 localph,
Linus Walleijea68f1f2008-06-22 21:54:44 +00008304 metadata->storage_id,
Linus Walleijb7426d12006-11-25 23:19:11 +00008305 PTP_OFC_MTP_AbstractAudioVideoPlaylist,
Linus Walleija4e6bdc2008-05-23 22:31:53 +00008306 get_playlist_extension(ptp_usb),
Linus Walleijb7426d12006-11-25 23:19:11 +00008307 &metadata->playlist_id,
8308 metadata->tracks,
8309 metadata->no_tracks);
8310}
8311
Linus Walleij438bd7f2006-06-08 11:35:44 +00008312/**
8313 * This routine updates a playlist based on the metadata
8314 * supplied. If the <code>tracks</code> field of the metadata
8315 * contains a track listing, these tracks will be added to the
8316 * playlist in place of those already present, i.e. the
alistair_boyle5e5fcb72008-11-17 14:38:19 +00008317 * previous track listing will be deleted. For Samsung devices the
8318 * playlist id (metadata->playlist_id) is likely to change.
Linus Walleij438bd7f2006-06-08 11:35:44 +00008319 * @param device a pointer to the device to create the new playlist on.
8320 * @param metadata the metadata for the playlist to be updated.
mopoke96143402006-10-30 04:37:26 +00008321 * notice that the field <code>playlist_id</code>
alistair_boyle5e5fcb72008-11-17 14:38:19 +00008322 * must contain the apropriate playlist ID. Playlist ID
8323 * be modified to a new playlist ID by the time the
8324 * function returns since edit-in-place is not always possible.
Linus Walleij438bd7f2006-06-08 11:35:44 +00008325 * @return 0 on success, any other value means failure.
8326 * @see LIBMTP_Create_New_Playlist()
8327 * @see LIBMTP_Delete_Object()
8328 */
mopoke96143402006-10-30 04:37:26 +00008329int LIBMTP_Update_Playlist(LIBMTP_mtpdevice_t *device,
alistair_boyle5e5fcb72008-11-17 14:38:19 +00008330 LIBMTP_playlist_t * const metadata)
Linus Walleij438bd7f2006-06-08 11:35:44 +00008331{
Linus Walleijf3c44052008-08-16 21:14:56 +00008332
8333 // Samsung needs its own special type of playlists
8334 PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo;
8335 if(FLAG_PLAYLIST_SPL(ptp_usb)) {
8336 return update_spl_playlist(device, metadata);
8337 }
8338
Linus Walleij7783b9b2007-09-22 22:10:44 +00008339 return update_abstract_list(device,
8340 metadata->name,
8341 NULL,
8342 NULL,
Linus Walleij31b74292008-05-02 23:29:06 +00008343 NULL,
Linus Walleij7783b9b2007-09-22 22:10:44 +00008344 metadata->playlist_id,
8345 PTP_OFC_MTP_AbstractAudioVideoPlaylist,
8346 metadata->tracks,
8347 metadata->no_tracks);
Linus Walleij438bd7f2006-06-08 11:35:44 +00008348}
Linus Walleijaa4b0752006-07-26 22:21:04 +00008349
Linus Walleij0c33ec02006-10-27 10:15:40 +00008350/**
8351 * This creates a new album metadata structure and allocates memory
8352 * for it. Notice that if you add strings to this structure they
8353 * will be freed by the corresponding <code>LIBMTP_destroy_album_t</code>
8354 * operation later, so be careful of using strdup() when assigning
8355 * strings.
8356 *
8357 * @return a pointer to the newly allocated metadata structure.
8358 * @see LIBMTP_destroy_album_t()
8359 */
8360LIBMTP_album_t *LIBMTP_new_album_t(void)
8361{
8362 LIBMTP_album_t *new = (LIBMTP_album_t *) malloc(sizeof(LIBMTP_album_t));
8363 if (new == NULL) {
8364 return NULL;
8365 }
8366 new->album_id = 0;
Linus Walleij5ce59db2008-03-12 21:22:58 +00008367 new->parent_id = 0;
Linus Walleijea68f1f2008-06-22 21:54:44 +00008368 new->storage_id = 0;
Linus Walleij0c33ec02006-10-27 10:15:40 +00008369 new->name = NULL;
Linus Walleijadce4a52007-04-23 07:28:11 +00008370 new->artist = NULL;
Linus Walleij31b74292008-05-02 23:29:06 +00008371 new->composer = NULL;
Linus Walleijadce4a52007-04-23 07:28:11 +00008372 new->genre = NULL;
Linus Walleij0c33ec02006-10-27 10:15:40 +00008373 new->tracks = NULL;
8374 new->no_tracks = 0;
8375 new->next = NULL;
8376 return new;
8377}
8378
8379/**
8380 * This recursively deletes the memory for an album structure
8381 *
8382 * @param album structure to destroy
8383 * @see LIBMTP_new_album_t()
8384 */
8385void LIBMTP_destroy_album_t(LIBMTP_album_t *album)
8386{
8387 if (album == NULL) {
8388 return;
8389 }
8390 if (album->name != NULL)
8391 free(album->name);
Linus Walleijadce4a52007-04-23 07:28:11 +00008392 if (album->artist != NULL)
8393 free(album->artist);
Linus Walleij31b74292008-05-02 23:29:06 +00008394 if (album->composer != NULL)
8395 free(album->composer);
Linus Walleijadce4a52007-04-23 07:28:11 +00008396 if (album->genre != NULL)
8397 free(album->genre);
Linus Walleij0c33ec02006-10-27 10:15:40 +00008398 if (album->tracks != NULL)
8399 free(album->tracks);
8400 free(album);
8401 return;
8402}
8403
8404/**
Linus Walleij08a5fe12009-09-08 21:28:58 +00008405 * This function maps and copies a property onto the album metadata if applicable.
8406 */
8407static void pick_property_to_album_metadata(LIBMTP_mtpdevice_t *device,
8408 MTPProperties *prop, LIBMTP_album_t *alb)
8409{
8410 switch (prop->property) {
8411 case PTP_OPC_Name:
8412 if (prop->propval.str != NULL)
8413 alb->name = strdup(prop->propval.str);
8414 else
8415 alb->name = NULL;
8416 break;
8417 case PTP_OPC_AlbumArtist:
8418 if (prop->propval.str != NULL) {
8419 // This should take precedence over plain "Artist"
8420 if (alb->artist != NULL)
8421 free(alb->artist);
8422 alb->artist = strdup(prop->propval.str);
8423 } else
8424 alb->artist = NULL;
8425 break;
8426 case PTP_OPC_Artist:
8427 if (prop->propval.str != NULL) {
8428 // Only use of AlbumArtist is not set
8429 if (alb->artist == NULL)
8430 alb->artist = strdup(prop->propval.str);
8431 } else
8432 alb->artist = NULL;
8433 break;
8434 case PTP_OPC_Composer:
8435 if (prop->propval.str != NULL)
8436 alb->composer = strdup(prop->propval.str);
8437 else
8438 alb->composer = NULL;
8439 break;
8440 case PTP_OPC_Genre:
8441 if (prop->propval.str != NULL)
8442 alb->genre = strdup(prop->propval.str);
8443 else
8444 alb->genre = NULL;
8445 break;
8446 }
8447}
8448
8449/**
8450 * This function retrieves the album metadata for an album
8451 * given by a unique ID.
8452 * @param device a pointer to the device to get the track metadata off.
8453 * @param alb an album metadata metadata set to fill in.
8454 */
8455static void get_album_metadata(LIBMTP_mtpdevice_t *device,
8456 LIBMTP_album_t *alb)
8457{
8458 uint16_t ret;
8459 PTPParams *params = (PTPParams *) device->params;
8460 uint32_t i;
8461 MTPProperties *prop;
8462 PTPObject *ob;
8463
8464 /*
8465 * If we have a cached, large set of metadata, then use it!
8466 */
8467 ret = ptp_object_want(params, alb->album_id, PTPOBJECT_MTPPROPLIST_LOADED, &ob);
8468 if (ob->mtpprops) {
8469 prop = ob->mtpprops;
8470 for (i=0;i<ob->nrofmtpprops;i++,prop++)
8471 pick_property_to_album_metadata(device, prop, alb);
8472 } else {
8473 uint16_t *props = NULL;
8474 uint32_t propcnt = 0;
8475
8476 // First see which properties can be retrieved for albums
8477 ret = ptp_mtp_getobjectpropssupported(params, PTP_OFC_MTP_AbstractAudioAlbum, &propcnt, &props);
8478 if (ret != PTP_RC_OK) {
8479 add_ptp_error_to_errorstack(device, ret, "get_album_metadata(): call to ptp_mtp_getobjectpropssupported() failed.");
8480 // Just bail out for now, nothing is ever set.
8481 return;
8482 } else {
8483 for (i=0;i<propcnt;i++) {
8484 switch (props[i]) {
8485 case PTP_OPC_Name:
8486 alb->name = get_string_from_object(device, ob->oid, PTP_OPC_Name);
8487 break;
8488 case PTP_OPC_AlbumArtist:
8489 if (alb->artist != NULL)
8490 free(alb->artist);
8491 alb->artist = get_string_from_object(device, ob->oid, PTP_OPC_AlbumArtist);
8492 break;
8493 case PTP_OPC_Artist:
8494 if (alb->artist == NULL)
8495 alb->artist = get_string_from_object(device, ob->oid, PTP_OPC_Artist);
8496 break;
8497 case PTP_OPC_Composer:
8498 alb->composer = get_string_from_object(device, ob->oid, PTP_OPC_Composer);
8499 break;
8500 case PTP_OPC_Genre:
8501 alb->genre = get_string_from_object(device, ob->oid, PTP_OPC_Genre);
8502 break;
8503 default:
8504 break;
8505 }
8506 }
8507 free(props);
8508 }
8509 }
8510}
8511
nicklas795e0100b2009-11-07 19:53:46 +00008512
Linus Walleij08a5fe12009-09-08 21:28:58 +00008513/**
Linus Walleij0c33ec02006-10-27 10:15:40 +00008514 * This function returns a list of the albums available on the
8515 * device.
8516 *
8517 * @param device a pointer to the device to get the album listing from.
8518 * @return an album list on success, else NULL. If there are no albums
8519 * on the device, NULL will be returned as well.
8520 * @see LIBMTP_Get_Album()
8521 */
8522LIBMTP_album_t *LIBMTP_Get_Album_List(LIBMTP_mtpdevice_t *device)
8523{
nicklas795e0100b2009-11-07 19:53:46 +00008524 // Read all storage devices
8525 return LIBMTP_Get_Album_List_For_Storage(device, 0);
8526}
8527
8528
8529/**
8530 * This function returns a list of the albums available on the
8531 * device. You can filter on the storage ID.
8532 *
8533 * @param device a pointer to the device to get the album listing from.
8534 * @param storage_id ID of device storage (if null, all storages)
8535 *
8536 * @return an album list on success, else NULL. If there are no albums
8537 * on the device, NULL will be returned as well.
8538 * @see LIBMTP_Get_Album()
8539 */
8540LIBMTP_album_t *LIBMTP_Get_Album_List_For_Storage(LIBMTP_mtpdevice_t *device, uint32_t const storage_id)
8541{
Linus Walleij0c33ec02006-10-27 10:15:40 +00008542 PTPParams *params = (PTPParams *) device->params;
8543 LIBMTP_album_t *retalbums = NULL;
8544 LIBMTP_album_t *curalbum = NULL;
8545 uint32_t i;
8546
8547 // Get all the handles if we haven't already done that
Linus Walleij08a5fe12009-09-08 21:28:58 +00008548 if (params->nrofobjects == 0)
Linus Walleij0c33ec02006-10-27 10:15:40 +00008549 flush_handles(device);
Linus Walleij0c33ec02006-10-27 10:15:40 +00008550
Linus Walleijd4637502009-06-14 23:03:33 +00008551 for (i = 0; i < params->nrofobjects; i++) {
Linus Walleij0c33ec02006-10-27 10:15:40 +00008552 LIBMTP_album_t *alb;
Linus Walleijd4637502009-06-14 23:03:33 +00008553 PTPObject *ob;
Linus Walleij0c33ec02006-10-27 10:15:40 +00008554 uint16_t ret;
8555
Linus Walleijd4637502009-06-14 23:03:33 +00008556 ob = &params->objects[i];
Linus Walleij0c33ec02006-10-27 10:15:40 +00008557
Linus Walleijf0bf4372007-07-01 21:47:38 +00008558 // Ignore stuff that isn't an album
Linus Walleij08a5fe12009-09-08 21:28:58 +00008559 if ( ob->oi.ObjectFormat != PTP_OFC_MTP_AbstractAudioAlbum )
Linus Walleijf0bf4372007-07-01 21:47:38 +00008560 continue;
Linus Walleijf0bf4372007-07-01 21:47:38 +00008561
nicklas795e0100b2009-11-07 19:53:46 +00008562 // Ignore stuff that isn't into the storage device
8563 if ((storage_id != 0) && (ob->oi.StorageID != storage_id ))
8564 continue;
8565
Linus Walleijf0bf4372007-07-01 21:47:38 +00008566 // Allocate a new album type
8567 alb = LIBMTP_new_album_t();
Linus Walleijd4637502009-06-14 23:03:33 +00008568 alb->album_id = ob->oid;
8569 alb->parent_id = ob->oi.ParentObject;
8570 alb->storage_id = ob->oi.StorageID;
Linus Walleij5ce59db2008-03-12 21:22:58 +00008571
Linus Walleij08a5fe12009-09-08 21:28:58 +00008572 // Fetch supported metadata
8573 get_album_metadata(device, alb);
8574
Linus Walleijf0bf4372007-07-01 21:47:38 +00008575 // Then get the track listing for this album
8576 ret = ptp_mtp_getobjectreferences(params, alb->album_id, &alb->tracks, &alb->no_tracks);
8577 if (ret != PTP_RC_OK) {
8578 add_ptp_error_to_errorstack(device, ret, "LIBMTP_Get_Album_List(): Could not get object references.");
8579 alb->tracks = NULL;
8580 alb->no_tracks = 0;
8581 }
Linus Walleij08a5fe12009-09-08 21:28:58 +00008582
Linus Walleijf0bf4372007-07-01 21:47:38 +00008583 // Add album to a list that will be returned afterwards.
8584 if (retalbums == NULL) {
8585 retalbums = alb;
8586 curalbum = alb;
8587 } else {
8588 curalbum->next = alb;
8589 curalbum = alb;
8590 }
Linus Walleij08a5fe12009-09-08 21:28:58 +00008591
Linus Walleij0c33ec02006-10-27 10:15:40 +00008592 }
8593 return retalbums;
8594}
8595
8596/**
8597 * This function retrieves an individual album from the device.
8598 * @param device a pointer to the device to get the album from.
8599 * @param albid the unique ID of the album to retrieve.
8600 * @return a valid album metadata or NULL on failure.
8601 * @see LIBMTP_Get_Album_List()
8602 */
8603LIBMTP_album_t *LIBMTP_Get_Album(LIBMTP_mtpdevice_t *device, uint32_t const albid)
8604{
8605 PTPParams *params = (PTPParams *) device->params;
Linus Walleijd4637502009-06-14 23:03:33 +00008606 uint16_t ret;
8607 PTPObject *ob;
8608 LIBMTP_album_t *alb;
Linus Walleij0c33ec02006-10-27 10:15:40 +00008609
8610 // Get all the handles if we haven't already done that
Linus Walleij08a5fe12009-09-08 21:28:58 +00008611 if (params->nrofobjects == 0)
Linus Walleij0c33ec02006-10-27 10:15:40 +00008612 flush_handles(device);
Linus Walleij0c33ec02006-10-27 10:15:40 +00008613
Linus Walleij08a5fe12009-09-08 21:28:58 +00008614 ret = ptp_object_want(params, albid, PTPOBJECT_OBJECTINFO_LOADED, &ob);
Linus Walleijd4637502009-06-14 23:03:33 +00008615 if (ret != PTP_RC_OK)
8616 return NULL;
Linus Walleij0c33ec02006-10-27 10:15:40 +00008617
Linus Walleijd4637502009-06-14 23:03:33 +00008618 // Ignore stuff that isn't an album
Linus Walleij08a5fe12009-09-08 21:28:58 +00008619 if (ob->oi.ObjectFormat != PTP_OFC_MTP_AbstractAudioAlbum)
Linus Walleijd4637502009-06-14 23:03:33 +00008620 return NULL;
Linus Walleijd4637502009-06-14 23:03:33 +00008621
8622 // Allocate a new album type
8623 alb = LIBMTP_new_album_t();
8624 alb->album_id = ob->oid;
8625 alb->parent_id = ob->oi.ParentObject;
8626 alb->storage_id = ob->oi.StorageID;
8627
Linus Walleij08a5fe12009-09-08 21:28:58 +00008628 // Fetch supported metadata
8629 get_album_metadata(device, alb);
8630
8631 // Then get the track listing for this album
Linus Walleijd4637502009-06-14 23:03:33 +00008632 ret = ptp_mtp_getobjectreferences(params, alb->album_id, &alb->tracks, &alb->no_tracks);
8633 if (ret != PTP_RC_OK) {
8634 add_ptp_error_to_errorstack(device, ret, "LIBMTP_Get_Album: Could not get object references.");
8635 alb->tracks = NULL;
8636 alb->no_tracks = 0;
8637 }
Linus Walleij08a5fe12009-09-08 21:28:58 +00008638
Linus Walleijd4637502009-06-14 23:03:33 +00008639 return alb;
Linus Walleij0c33ec02006-10-27 10:15:40 +00008640}
8641
8642/**
8643 * This routine creates a new album based on the metadata
8644 * supplied. If the <code>tracks</code> field of the metadata
8645 * contains a track listing, these tracks will be added to the
8646 * album.
8647 * @param device a pointer to the device to create the new album on.
8648 * @param metadata the metadata for the new album. If the function
8649 * exits with success, the <code>album_id</code> field of this
8650 * struct will contain the new ID of the album.
Linus Walleij2fe43e42009-01-30 20:31:26 +00008651 * <ul>
Linus Walleij08a5fe12009-09-08 21:28:58 +00008652 * <li><code>metadata-&gt;parent_id</code> should be set to the parent
8653 * (e.g. folder) to store this track in. Since some
Linus Walleij2fe43e42009-01-30 20:31:26 +00008654 * devices are a bit picky about where files
8655 * are placed, a default folder will be chosen if libmtp
8656 * has detected one for the current filetype and this
8657 * parameter is set to 0. If this is 0 and no default folder
8658 * can be found, the file will be stored in the root folder.
8659 * <li><code>metadata-&gt;storage_id</code> should be set to the
8660 * desired storage (e.g. memory card or whatever your device
8661 * presents) to store this track in. Setting this to 0 will store
8662 * the track on the primary storage.
8663 * </ul>
Linus Walleij0c33ec02006-10-27 10:15:40 +00008664 * @return 0 on success, any other value means failure.
8665 * @see LIBMTP_Update_Album()
8666 * @see LIBMTP_Delete_Object()
8667 */
8668int LIBMTP_Create_New_Album(LIBMTP_mtpdevice_t *device,
Linus Walleijea68f1f2008-06-22 21:54:44 +00008669 LIBMTP_album_t * const metadata)
Linus Walleij0c33ec02006-10-27 10:15:40 +00008670{
Linus Walleijea68f1f2008-06-22 21:54:44 +00008671 uint32_t localph = metadata->parent_id;
mopoke96143402006-10-30 04:37:26 +00008672
Linus Walleij0c33ec02006-10-27 10:15:40 +00008673 // Use a default folder if none given
8674 if (localph == 0) {
Linus Walleijafac36b2009-01-30 20:39:13 +00008675 if (device->default_album_folder != 0)
8676 localph = device->default_album_folder;
8677 else
8678 localph = device->default_music_folder;
Linus Walleij0c33ec02006-10-27 10:15:40 +00008679 }
Linus Walleij5ce59db2008-03-12 21:22:58 +00008680 metadata->parent_id = localph;
Linus Walleij0c33ec02006-10-27 10:15:40 +00008681
Linus Walleijb7426d12006-11-25 23:19:11 +00008682 // Just create a new abstract album...
8683 return create_new_abstract_list(device,
8684 metadata->name,
Linus Walleijadce4a52007-04-23 07:28:11 +00008685 metadata->artist,
Linus Walleij31b74292008-05-02 23:29:06 +00008686 metadata->composer,
Linus Walleijadce4a52007-04-23 07:28:11 +00008687 metadata->genre,
Linus Walleijb7426d12006-11-25 23:19:11 +00008688 localph,
Linus Walleijea68f1f2008-06-22 21:54:44 +00008689 metadata->storage_id,
Linus Walleijb7426d12006-11-25 23:19:11 +00008690 PTP_OFC_MTP_AbstractAudioAlbum,
8691 ".alb",
8692 &metadata->album_id,
8693 metadata->tracks,
8694 metadata->no_tracks);
Linus Walleij0c33ec02006-10-27 10:15:40 +00008695}
8696
8697/**
rreardon5332f9c2006-12-05 10:18:23 +00008698 * This creates a new sample data metadata structure and allocates memory
8699 * for it. Notice that if you add strings to this structure they
8700 * will be freed by the corresponding <code>LIBMTP_destroy_sampledata_t</code>
8701 * operation later, so be careful of using strdup() when assigning
8702 * strings.
8703 *
8704 * @return a pointer to the newly allocated metadata structure.
8705 * @see LIBMTP_destroy_sampledata_t()
8706 */
8707LIBMTP_filesampledata_t *LIBMTP_new_filesampledata_t(void)
8708{
8709 LIBMTP_filesampledata_t *new = (LIBMTP_filesampledata_t *) malloc(sizeof(LIBMTP_filesampledata_t));
8710 if (new == NULL) {
8711 return NULL;
8712 }
8713 new->height=0;
8714 new->width = 0;
8715 new->data = NULL;
8716 new->duration = 0;
8717 new->size = 0;
8718 return new;
8719}
8720
Linus Walleijf1b02f22006-12-06 09:03:23 +00008721/**
8722 * This destroys a file sample metadata type.
8723 * @param sample the file sample metadata to be destroyed.
8724 */
8725void LIBMTP_destroy_filesampledata_t(LIBMTP_filesampledata_t * sample)
8726{
8727 if (sample == NULL) {
8728 return;
8729 }
8730 if (sample->data != NULL) {
8731 free(sample->data);
8732 }
8733 free(sample);
8734}
8735
8736/**
8737 * This routine figures out whether a certain filetype supports
8738 * representative samples (small thumbnail images) or not. This
8739 * typically applies to JPEG files, MP3 files and Album abstract
8740 * playlists, but in theory any filetype could support representative
8741 * samples.
8742 * @param device a pointer to the device which is to be examined.
Linus Walleij9ffe9982008-01-20 13:42:52 +00008743 * @param filetype the fileype to examine, and return the representative sample
Linus Walleijf1b02f22006-12-06 09:03:23 +00008744 * properties for.
8745 * @param sample this will contain a new sample type with the fields
8746 * filled in with suitable default values. For example, the
8747 * supported sample type will be set, the supported height and
8748 * width will be set to max values if it is an image sample,
8749 * and duration will also be given some suitable default value
Linus Walleij2c1bbd62009-11-07 15:15:03 +00008750 * which should not be exceeded on audio samples. If the
Linus Walleijf1b02f22006-12-06 09:03:23 +00008751 * device does not support samples for this filetype, this
8752 * pointer will be NULL. If it is not NULL, the user must
8753 * destroy this struct with <code>LIBMTP_destroy_filesampledata_t()</code>
8754 * after use.
8755 * @return 0 on success, any other value means failure.
8756 * @see LIBMTP_Send_Representative_Sample()
8757 * @see LIBMTP_Create_New_Album()
8758 */
8759int LIBMTP_Get_Representative_Sample_Format(LIBMTP_mtpdevice_t *device,
8760 LIBMTP_filetype_t const filetype,
8761 LIBMTP_filesampledata_t ** sample)
8762{
8763 uint16_t ret;
8764 PTPParams *params = (PTPParams *) device->params;
8765 uint16_t *props = NULL;
8766 uint32_t propcnt = 0;
8767 int i;
8768 // TODO: Get rid of these when we can properly query the device.
8769 int support_data = 0;
8770 int support_format = 0;
8771 int support_height = 0;
8772 int support_width = 0;
8773 int support_duration = 0;
Richard Low6f070842009-05-03 10:26:16 +00008774 int support_size = 0;
Linus Walleijf1b02f22006-12-06 09:03:23 +00008775
rreardon21a3a9d2007-08-15 10:56:31 +00008776 PTPObjectPropDesc opd_height;
8777 PTPObjectPropDesc opd_width;
8778 PTPObjectPropDesc opd_format;
8779 PTPObjectPropDesc opd_duration;
Richard Low6f070842009-05-03 10:26:16 +00008780 PTPObjectPropDesc opd_size;
Linus Walleij2c1bbd62009-11-07 15:15:03 +00008781
Linus Walleijf1b02f22006-12-06 09:03:23 +00008782 // Default to no type supported.
8783 *sample = NULL;
Linus Walleij2c1bbd62009-11-07 15:15:03 +00008784
Linus Walleijf1b02f22006-12-06 09:03:23 +00008785 ret = ptp_mtp_getobjectpropssupported(params, map_libmtp_type_to_ptp_type(filetype), &propcnt, &props);
8786 if (ret != PTP_RC_OK) {
Linus Walleij070e9b42007-01-22 08:49:28 +00008787 add_ptp_error_to_errorstack(device, ret, "LIBMTP_Get_Representative_Sample_Format(): could not get object properties.");
Linus Walleijf1b02f22006-12-06 09:03:23 +00008788 return -1;
8789 }
8790 /*
8791 * TODO: when walking through these object properties, make calls to
Linus Walleij2c1bbd62009-11-07 15:15:03 +00008792 * a new function in ptp.h/ptp.c that can send the command
Linus Walleijf1b02f22006-12-06 09:03:23 +00008793 * PTP_OC_MTP_GetObjectPropDesc to get max/min values of the properties
8794 * supported.
8795 */
8796 for (i = 0; i < propcnt; i++) {
8797 switch(props[i]) {
8798 case PTP_OPC_RepresentativeSampleData:
8799 support_data = 1;
8800 break;
8801 case PTP_OPC_RepresentativeSampleFormat:
8802 support_format = 1;
8803 break;
8804 case PTP_OPC_RepresentativeSampleSize:
Richard Low6f070842009-05-03 10:26:16 +00008805 support_size = 1;
Linus Walleijf1b02f22006-12-06 09:03:23 +00008806 break;
8807 case PTP_OPC_RepresentativeSampleHeight:
8808 support_height = 1;
8809 break;
8810 case PTP_OPC_RepresentativeSampleWidth:
8811 support_width = 1;
8812 break;
8813 case PTP_OPC_RepresentativeSampleDuration:
8814 support_duration = 1;
8815 break;
8816 default:
8817 break;
8818 }
8819 }
8820 free(props);
Linus Walleij2c1bbd62009-11-07 15:15:03 +00008821
Linus Walleijf1b02f22006-12-06 09:03:23 +00008822 if (support_data && support_format && support_height && support_width && !support_duration) {
8823 // Something that supports height and width and not duration is likely to be JPEG
8824 LIBMTP_filesampledata_t *retsam = LIBMTP_new_filesampledata_t();
Linus Walleij2c1bbd62009-11-07 15:15:03 +00008825 /*
8826 * Populate the sample format with the first supported format
8827 *
rreardon21a3a9d2007-08-15 10:56:31 +00008828 * TODO: figure out how to pass back more than one format if more are
8829 * supported by the device.
8830 */
Linus Walleij2c1bbd62009-11-07 15:15:03 +00008831 ptp_mtp_getobjectpropdesc (params, PTP_OPC_RepresentativeSampleFormat, map_libmtp_type_to_ptp_type(filetype), &opd_format);
rreardon21a3a9d2007-08-15 10:56:31 +00008832 retsam->filetype = map_ptp_type_to_libmtp_type(opd_format.FORM.Enum.SupportedValue[0].u16);
Richard Low6f070842009-05-03 10:26:16 +00008833 ptp_free_objectpropdesc(&opd_format);
rreardon21a3a9d2007-08-15 10:56:31 +00008834 /* Populate the maximum image height */
Linus Walleij2c1bbd62009-11-07 15:15:03 +00008835 ptp_mtp_getobjectpropdesc (params, PTP_OPC_RepresentativeSampleWidth, map_libmtp_type_to_ptp_type(filetype), &opd_width);
rreardon21a3a9d2007-08-15 10:56:31 +00008836 retsam->width = opd_width.FORM.Range.MaximumValue.u32;
Richard Low6f070842009-05-03 10:26:16 +00008837 ptp_free_objectpropdesc(&opd_width);
rreardon21a3a9d2007-08-15 10:56:31 +00008838 /* Populate the maximum image width */
Linus Walleij2c1bbd62009-11-07 15:15:03 +00008839 ptp_mtp_getobjectpropdesc (params, PTP_OPC_RepresentativeSampleHeight, map_libmtp_type_to_ptp_type(filetype), &opd_height);
rreardon21a3a9d2007-08-15 10:56:31 +00008840 retsam->height = opd_height.FORM.Range.MaximumValue.u32;
Richard Low6f070842009-05-03 10:26:16 +00008841 ptp_free_objectpropdesc(&opd_height);
8842 /* Populate the maximum size */
8843 if (support_size) {
8844 ptp_mtp_getobjectpropdesc (params, PTP_OPC_RepresentativeSampleSize, map_libmtp_type_to_ptp_type(filetype), &opd_size);
8845 retsam->size = opd_size.FORM.Range.MaximumValue.u32;
8846 ptp_free_objectpropdesc(&opd_size);
8847 }
Linus Walleijf1b02f22006-12-06 09:03:23 +00008848 *sample = retsam;
8849 } else if (support_data && support_format && !support_height && !support_width && support_duration) {
8850 // Another qualified guess
8851 LIBMTP_filesampledata_t *retsam = LIBMTP_new_filesampledata_t();
Linus Walleij2c1bbd62009-11-07 15:15:03 +00008852 /*
8853 * Populate the sample format with the first supported format
8854 *
rreardon21a3a9d2007-08-15 10:56:31 +00008855 * TODO: figure out how to pass back more than one format if more are
8856 * supported by the device.
8857 */
Linus Walleij2c1bbd62009-11-07 15:15:03 +00008858 ptp_mtp_getobjectpropdesc (params, PTP_OPC_RepresentativeSampleFormat, map_libmtp_type_to_ptp_type(filetype), &opd_format);
rreardon21a3a9d2007-08-15 10:56:31 +00008859 retsam->filetype = map_ptp_type_to_libmtp_type(opd_format.FORM.Enum.SupportedValue[0].u16);
Richard Low6f070842009-05-03 10:26:16 +00008860 ptp_free_objectpropdesc(&opd_format);
8861 /* Populate the maximum duration */
Linus Walleij2c1bbd62009-11-07 15:15:03 +00008862 ptp_mtp_getobjectpropdesc (params, PTP_OPC_RepresentativeSampleDuration, map_libmtp_type_to_ptp_type(filetype), &opd_duration);
rreardon21a3a9d2007-08-15 10:56:31 +00008863 retsam->duration = opd_duration.FORM.Range.MaximumValue.u32;
Richard Low6f070842009-05-03 10:26:16 +00008864 ptp_free_objectpropdesc(&opd_duration);
8865 /* Populate the maximum size */
8866 if (support_size) {
8867 ptp_mtp_getobjectpropdesc (params, PTP_OPC_RepresentativeSampleSize, map_libmtp_type_to_ptp_type(filetype), &opd_size);
8868 retsam->size = opd_size.FORM.Range.MaximumValue.u32;
8869 ptp_free_objectpropdesc(&opd_size);
8870 }
Linus Walleijf1b02f22006-12-06 09:03:23 +00008871 *sample = retsam;
8872 }
8873 return 0;
8874}
rreardon5332f9c2006-12-05 10:18:23 +00008875
8876/**
8877 * This routine sends representative sample data for an object.
8878 * This uses the RepresentativeSampleData property of the album,
8879 * if the device supports it. The data should be of a format acceptable
8880 * to the player (for iRiver and Creative, this seems to be JPEG) and
8881 * must not be too large. (for a Creative, max seems to be about 20KB.)
Richard Low6f070842009-05-03 10:26:16 +00008882 * Check by calling LIBMTP_Get_Representative_Sample_Format() to get
8883 * maximum size, dimensions, etc..
Linus Walleij7e3f2762006-12-03 22:52:05 +00008884 * @param device a pointer to the device which the object is on.
8885 * @param id unique id of the object to set artwork for.
Richard Low36e447c2008-01-20 15:24:55 +00008886 * @param pointer to LIBMTP_filesampledata_t struct containing data
Linus Walleij0c33ec02006-10-27 10:15:40 +00008887 * @return 0 on success, any other value means failure.
Richard Low36e447c2008-01-20 15:24:55 +00008888 * @see LIBMTP_Get_Representative_Sample()
Linus Walleijf1b02f22006-12-06 09:03:23 +00008889 * @see LIBMTP_Get_Representative_Sample_Format()
Linus Walleij0c33ec02006-10-27 10:15:40 +00008890 * @see LIBMTP_Create_New_Album()
8891 */
Linus Walleij7e3f2762006-12-03 22:52:05 +00008892int LIBMTP_Send_Representative_Sample(LIBMTP_mtpdevice_t *device,
rreardon5332f9c2006-12-05 10:18:23 +00008893 uint32_t const id,
8894 LIBMTP_filesampledata_t *sampledata)
Linus Walleij0c33ec02006-10-27 10:15:40 +00008895{
8896 uint16_t ret;
8897 PTPParams *params = (PTPParams *) device->params;
Richard Lowbb9fb4a2008-05-18 14:49:34 +00008898 PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo;
Linus Walleij0c33ec02006-10-27 10:15:40 +00008899 PTPPropertyValue propval;
Linus Walleijd4637502009-06-14 23:03:33 +00008900 PTPObject *ob;
Linus Walleijf0bf4372007-07-01 21:47:38 +00008901 uint32_t i;
Linus Walleijf1b02f22006-12-06 09:03:23 +00008902 uint16_t *props = NULL;
8903 uint32_t propcnt = 0;
8904 int supported = 0;
rreardon5332f9c2006-12-05 10:18:23 +00008905
8906 // get the file format for the object we're going to send representative data for
Linus Walleijd4637502009-06-14 23:03:33 +00008907 ret = ptp_object_want (params, id, PTPOBJECT_OBJECTINFO_LOADED, &ob);
8908 if (ret != PTP_RC_OK) {
Linus Walleijf0bf4372007-07-01 21:47:38 +00008909 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Send_Representative_Sample(): could not get object info.");
rreardon5332f9c2006-12-05 10:18:23 +00008910 return -1;
8911 }
8912
8913 // check that we can send representative sample data for this object format
Linus Walleijd4637502009-06-14 23:03:33 +00008914 ret = ptp_mtp_getobjectpropssupported(params, ob->oi.ObjectFormat, &propcnt, &props);
Linus Walleij0c33ec02006-10-27 10:15:40 +00008915 if (ret != PTP_RC_OK) {
Linus Walleij070e9b42007-01-22 08:49:28 +00008916 add_ptp_error_to_errorstack(device, ret, "LIBMTP_Send_Representative_Sample(): could not get object properties.");
Linus Walleij0c33ec02006-10-27 10:15:40 +00008917 return -1;
8918 }
Linus Walleijf1b02f22006-12-06 09:03:23 +00008919
Linus Walleij0c33ec02006-10-27 10:15:40 +00008920 for (i = 0; i < propcnt; i++) {
Linus Walleijf1b02f22006-12-06 09:03:23 +00008921 if (props[i] == PTP_OPC_RepresentativeSampleData) {
Linus Walleij0c33ec02006-10-27 10:15:40 +00008922 supported = 1;
Linus Walleijf1b02f22006-12-06 09:03:23 +00008923 break;
8924 }
Linus Walleij0c33ec02006-10-27 10:15:40 +00008925 }
8926 if (!supported) {
Linus Walleijf1b02f22006-12-06 09:03:23 +00008927 free(props);
Linus Walleij070e9b42007-01-22 08:49:28 +00008928 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Send_Representative_Sample(): object type doesn't support RepresentativeSampleData.");
Linus Walleij0c33ec02006-10-27 10:15:40 +00008929 return -1;
8930 }
Linus Walleijf1b02f22006-12-06 09:03:23 +00008931 free(props);
Linus Walleij2c1bbd62009-11-07 15:15:03 +00008932
Linus Walleijf1b02f22006-12-06 09:03:23 +00008933 // Go ahead and send the data
rreardon5332f9c2006-12-05 10:18:23 +00008934 propval.a.count = sampledata->size;
8935 propval.a.v = malloc(sizeof(PTPPropertyValue) * sampledata->size);
8936 for (i = 0; i < sampledata->size; i++) {
8937 propval.a.v[i].u8 = sampledata->data[i];
Linus Walleij7e3f2762006-12-03 22:52:05 +00008938 }
Linus Walleij2c1bbd62009-11-07 15:15:03 +00008939
Linus Walleij0c33ec02006-10-27 10:15:40 +00008940 ret = ptp_mtp_setobjectpropvalue(params,id,PTP_OPC_RepresentativeSampleData,
Linus Walleijf1b02f22006-12-06 09:03:23 +00008941 &propval,PTP_DTC_AUINT8);
Linus Walleij0c33ec02006-10-27 10:15:40 +00008942 if (ret != PTP_RC_OK) {
Linus Walleij070e9b42007-01-22 08:49:28 +00008943 add_ptp_error_to_errorstack(device, ret, "LIBMTP_Send_Representative_Sample(): could not send sample data.");
Linus Walleij7e3f2762006-12-03 22:52:05 +00008944 free(propval.a.v);
Linus Walleij0c33ec02006-10-27 10:15:40 +00008945 return -1;
8946 }
Linus Walleij7e3f2762006-12-03 22:52:05 +00008947 free(propval.a.v);
Linus Walleij2c1bbd62009-11-07 15:15:03 +00008948
rreardond2ddb632006-12-12 12:13:21 +00008949 /* Set the height and width if the sample is an image, otherwise just
8950 * set the duration and size */
8951 switch(sampledata->filetype) {
Linus Walleije1ac07e2006-12-14 19:38:59 +00008952 case LIBMTP_FILETYPE_JPEG:
8953 case LIBMTP_FILETYPE_JFIF:
8954 case LIBMTP_FILETYPE_TIFF:
8955 case LIBMTP_FILETYPE_BMP:
8956 case LIBMTP_FILETYPE_GIF:
8957 case LIBMTP_FILETYPE_PICT:
8958 case LIBMTP_FILETYPE_PNG:
Linus Walleijfec4d562008-06-01 22:30:36 +00008959 if (!FLAG_BROKEN_SET_SAMPLE_DIMENSIONS(ptp_usb)) {
Richard Lowbb9fb4a2008-05-18 14:49:34 +00008960 // For images, set the height and width
8961 set_object_u32(device, id, PTP_OPC_RepresentativeSampleHeight, sampledata->height);
Linus Walleij2c1bbd62009-11-07 15:15:03 +00008962 set_object_u32(device, id, PTP_OPC_RepresentativeSampleWidth, sampledata->width);
Richard Lowbb9fb4a2008-05-18 14:49:34 +00008963 }
Linus Walleije1ac07e2006-12-14 19:38:59 +00008964 break;
8965 default:
8966 // For anything not an image, set the duration and size
8967 set_object_u32(device, id, PTP_OPC_RepresentativeSampleDuration, sampledata->duration);
8968 set_object_u32(device, id, PTP_OPC_RepresentativeSampleSize, sampledata->size);
Linus Walleij2c1bbd62009-11-07 15:15:03 +00008969 break;
rreardond2ddb632006-12-12 12:13:21 +00008970 }
Linus Walleij2c1bbd62009-11-07 15:15:03 +00008971
Linus Walleij0c33ec02006-10-27 10:15:40 +00008972 return 0;
8973}
8974
8975/**
Richard Low36e447c2008-01-20 15:24:55 +00008976 * This routine gets representative sample data for an object.
8977 * This uses the RepresentativeSampleData property of the album,
8978 * if the device supports it.
8979 * @param device a pointer to the device which the object is on.
8980 * @param id unique id of the object to get data for.
8981 * @param pointer to LIBMTP_filesampledata_t struct to receive data
8982 * @return 0 on success, any other value means failure.
8983 * @see LIBMTP_Send_Representative_Sample()
8984 * @see LIBMTP_Get_Representative_Sample_Format()
8985 * @see LIBMTP_Create_New_Album()
8986 */
8987int LIBMTP_Get_Representative_Sample(LIBMTP_mtpdevice_t *device,
8988 uint32_t const id,
8989 LIBMTP_filesampledata_t *sampledata)
8990{
8991 uint16_t ret;
8992 PTPParams *params = (PTPParams *) device->params;
8993 PTPPropertyValue propval;
Linus Walleijd4637502009-06-14 23:03:33 +00008994 PTPObject *ob;
Richard Low36e447c2008-01-20 15:24:55 +00008995 uint32_t i;
8996 uint16_t *props = NULL;
8997 uint32_t propcnt = 0;
8998 int supported = 0;
8999
9000 // get the file format for the object we're going to send representative data for
Linus Walleijd4637502009-06-14 23:03:33 +00009001 ret = ptp_object_want (params, id, PTPOBJECT_OBJECTINFO_LOADED, &ob);
9002 if (ret != PTP_RC_OK) {
Richard Low36e447c2008-01-20 15:24:55 +00009003 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Get_Representative_Sample(): could not get object info.");
9004 return -1;
9005 }
9006
9007 // check that we can store representative sample data for this object format
Linus Walleijd4637502009-06-14 23:03:33 +00009008 ret = ptp_mtp_getobjectpropssupported(params, ob->oi.ObjectFormat, &propcnt, &props);
Richard Low36e447c2008-01-20 15:24:55 +00009009 if (ret != PTP_RC_OK) {
9010 add_ptp_error_to_errorstack(device, ret, "LIBMTP_Get_Representative_Sample(): could not get object properties.");
9011 return -1;
9012 }
9013
9014 for (i = 0; i < propcnt; i++) {
9015 if (props[i] == PTP_OPC_RepresentativeSampleData) {
9016 supported = 1;
9017 break;
9018 }
9019 }
9020 if (!supported) {
9021 free(props);
9022 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Get_Representative_Sample(): object type doesn't support RepresentativeSampleData.");
9023 return -1;
9024 }
9025 free(props);
Linus Walleij2c1bbd62009-11-07 15:15:03 +00009026
Richard Low36e447c2008-01-20 15:24:55 +00009027 // Get the data
9028 ret = ptp_mtp_getobjectpropvalue(params,id,PTP_OPC_RepresentativeSampleData,
9029 &propval,PTP_DTC_AUINT8);
9030 if (ret != PTP_RC_OK) {
9031 add_ptp_error_to_errorstack(device, ret, "LIBMTP_Get_Representative_Sample(): could not get sample data.");
9032 return -1;
9033 }
Linus Walleij2c1bbd62009-11-07 15:15:03 +00009034
Richard Low36e447c2008-01-20 15:24:55 +00009035 // Store it
9036 sampledata->size = propval.a.count;
9037 sampledata->data = malloc(sizeof(PTPPropertyValue) * propval.a.count);
9038 for (i = 0; i < propval.a.count; i++) {
9039 sampledata->data[i] = propval.a.v[i].u8;
9040 }
9041 free(propval.a.v);
9042
Linus Walleij2c1bbd62009-11-07 15:15:03 +00009043 // Get the other properties
Richard Low36e447c2008-01-20 15:24:55 +00009044 sampledata->width = get_u32_from_object(device, id, PTP_OPC_RepresentativeSampleWidth, 0);
9045 sampledata->height = get_u32_from_object(device, id, PTP_OPC_RepresentativeSampleHeight, 0);
9046 sampledata->duration = get_u32_from_object(device, id, PTP_OPC_RepresentativeSampleDuration, 0);
9047 sampledata->filetype = map_ptp_type_to_libmtp_type(
9048 get_u16_from_object(device, id, PTP_OPC_RepresentativeSampleFormat, LIBMTP_FILETYPE_UNKNOWN));
Linus Walleij2c1bbd62009-11-07 15:15:03 +00009049
Richard Low36e447c2008-01-20 15:24:55 +00009050 return 0;
9051}
9052
9053/**
Philip Langdale99351a42012-08-18 15:32:12 -07009054 * Retrieve the thumbnail for a file.
9055 * @param device a pointer to the device to get the thumbnail from.
9056 * @param id the object ID of the file to retrieve the thumbnail for.
9057 * @return 0 on success, any other value means failure.
9058 */
9059int LIBMTP_Get_Thumbnail(LIBMTP_mtpdevice_t *device, uint32_t const id,
9060 unsigned char **data, unsigned int *size)
9061{
9062 PTPParams *params = (PTPParams *) device->params;
9063 uint16_t ret;
9064
9065 ret = ptp_getthumb(params, id, data, size);
9066 if (ret == PTP_RC_OK)
9067 return 0;
9068 return -1;
9069}
9070
Philip Langdale31e831c2013-03-04 20:58:19 -08009071
9072int LIBMTP_GetPartialObject(LIBMTP_mtpdevice_t *device, uint32_t const id,
9073 uint64_t offset, uint32_t maxbytes,
9074 unsigned char **data, unsigned int *size)
9075{
Marcus Meissnerc13c7a92017-11-16 17:31:34 +01009076 PTPParams *params = (PTPParams *) device->params;
9077 uint16_t ret;
9078 LIBMTP_file_t *mtpfile = LIBMTP_Get_Filemetadata(device, id);
9079
9080 /* Some devices do not like reading over the end and hang instead of progressing */
9081 if (offset >= mtpfile->filesize) {
9082 *size = 0;
9083 LIBMTP_destroy_file_t (mtpfile);
9084 return 0;
9085 }
9086 if (offset + maxbytes > mtpfile->filesize) {
9087 maxbytes = mtpfile->filesize - offset;
9088 }
Philip Langdale31e831c2013-03-04 20:58:19 -08009089
9090 if (!ptp_operation_issupported(params, PTP_OC_ANDROID_GetPartialObject64)) {
9091 if (!ptp_operation_issupported(params, PTP_OC_GetPartialObject)) {
9092 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL,
9093 "LIBMTP_GetPartialObject: PTP_OC_GetPartialObject not supported");
9094 return -1;
9095 }
9096
9097 if (offset >> 32 != 0) {
9098 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL,
9099 "LIBMTP_GetPartialObject: PTP_OC_GetPartialObject only supports 32bit offsets");
9100 return -1;
9101 }
9102
9103 ret = ptp_getpartialobject(params, id, (uint32_t)offset, maxbytes, data, size);
9104 } else {
9105 ret = ptp_android_getpartialobject64(params, id, offset, maxbytes, data, size);
9106 }
9107 if (ret == PTP_RC_OK)
9108 return 0;
9109 return -1;
9110}
9111
9112
9113int LIBMTP_SendPartialObject(LIBMTP_mtpdevice_t *device, uint32_t const id,
9114 uint64_t offset, unsigned char *data, unsigned int size)
9115{
9116 PTPParams *params = (PTPParams *) device->params;
9117 uint16_t ret;
9118
9119 if (!ptp_operation_issupported(params, PTP_OC_ANDROID_SendPartialObject)) {
9120 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL,
9121 "LIBMTP_SendPartialObject: PTP_OC_ANDROID_SendPartialObject not supported");
9122 return -1;
9123 }
9124
9125 ret = ptp_android_sendpartialobject(params, id, offset, data, size);
9126 if (ret == PTP_RC_OK)
9127 return 0;
9128 return -1;
9129}
9130
9131
9132int LIBMTP_BeginEditObject(LIBMTP_mtpdevice_t *device, uint32_t const id)
9133{
9134 PTPParams *params = (PTPParams *) device->params;
9135 uint16_t ret;
9136
9137 if (!ptp_operation_issupported(params, PTP_OC_ANDROID_BeginEditObject)) {
9138 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL,
9139 "LIBMTP_BeginEditObject: PTP_OC_ANDROID_BeginEditObject not supported");
9140 return -1;
9141 }
9142
9143 ret = ptp_android_begineditobject(params, id);
9144 if (ret == PTP_RC_OK)
9145 return 0;
9146 return -1;
9147}
9148
9149
9150int LIBMTP_EndEditObject(LIBMTP_mtpdevice_t *device, uint32_t const id)
9151{
9152 PTPParams *params = (PTPParams *) device->params;
9153 uint16_t ret;
9154
9155 if (!ptp_operation_issupported(params, PTP_OC_ANDROID_EndEditObject)) {
9156 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL,
9157 "LIBMTP_EndEditObject: PTP_OC_ANDROID_EndEditObject not supported");
9158 return -1;
9159 }
9160
9161 ret = ptp_android_endeditobject(params, id);
9162 if (ret == PTP_RC_OK) {
9163 // update cached object properties if metadata cache exists
9164 update_metadata_cache(device, id);
9165 return 0;
9166 }
9167 return -1;
9168}
9169
9170
9171int LIBMTP_TruncateObject(LIBMTP_mtpdevice_t *device, uint32_t const id,
9172 uint64_t offset)
9173{
9174 PTPParams *params = (PTPParams *) device->params;
9175 uint16_t ret;
9176
9177 if (!ptp_operation_issupported(params, PTP_OC_ANDROID_TruncateObject)) {
9178 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL,
9179 "LIBMTP_TruncateObject: PTP_OC_ANDROID_TruncateObject not supported");
9180 return -1;
9181 }
9182
9183 ret = ptp_android_truncate(params, id, offset);
9184 if (ret == PTP_RC_OK)
9185 return 0;
9186 return -1;
9187}
9188
9189
Philip Langdale99351a42012-08-18 15:32:12 -07009190/**
Linus Walleij0c33ec02006-10-27 10:15:40 +00009191 * This routine updates an album based on the metadata
9192 * supplied. If the <code>tracks</code> field of the metadata
9193 * contains a track listing, these tracks will be added to the
9194 * album in place of those already present, i.e. the
9195 * previous track listing will be deleted.
9196 * @param device a pointer to the device to create the new album on.
9197 * @param metadata the metadata for the album to be updated.
9198 * notice that the field <code>album_id</code>
9199 * must contain the apropriate album ID.
9200 * @return 0 on success, any other value means failure.
9201 * @see LIBMTP_Create_New_Album()
9202 * @see LIBMTP_Delete_Object()
9203 */
9204int LIBMTP_Update_Album(LIBMTP_mtpdevice_t *device,
9205 LIBMTP_album_t const * const metadata)
9206{
Linus Walleij7783b9b2007-09-22 22:10:44 +00009207 return update_abstract_list(device,
9208 metadata->name,
9209 metadata->artist,
Linus Walleij31b74292008-05-02 23:29:06 +00009210 metadata->composer,
Linus Walleij7783b9b2007-09-22 22:10:44 +00009211 metadata->genre,
9212 metadata->album_id,
9213 PTP_OFC_MTP_AbstractAudioAlbum,
9214 metadata->tracks,
9215 metadata->no_tracks);
Linus Walleij0c33ec02006-10-27 10:15:40 +00009216}
Linus Walleijaa4b0752006-07-26 22:21:04 +00009217
9218/**
9219 * Dummy function needed to interface to upstream
9220 * ptp.c/ptp.h files.
9221 */
9222void ptp_nikon_getptpipguid (unsigned char* guid) {
9223 return;
9224}
tsaarnia3eb60a2007-07-06 17:41:30 +00009225
9226/**
Linus Walleij7752b952007-10-19 20:11:46 +00009227 * Add an object to cache.
9228 * @param device the device which may have a cache to which the object should be added.
9229 * @param object_id the object to add to the cache.
Linus Walleij2c1bbd62009-11-07 15:15:03 +00009230 */
Linus Walleija6d0d482007-10-31 08:54:56 +00009231static void add_object_to_cache(LIBMTP_mtpdevice_t *device, uint32_t object_id)
tsaarnia3eb60a2007-07-06 17:41:30 +00009232{
9233 PTPParams *params = (PTPParams *)device->params;
tsaarnia3eb60a2007-07-06 17:41:30 +00009234 uint16_t ret;
tsaarnia3eb60a2007-07-06 17:41:30 +00009235
Linus Walleija6d0d482007-10-31 08:54:56 +00009236 ret = ptp_add_object_to_cache(params, object_id);
9237 if (ret != PTP_RC_OK) {
9238 add_ptp_error_to_errorstack(device, ret, "add_object_to_cache(): couldn't add object to cache");
tsaarnia3eb60a2007-07-06 17:41:30 +00009239 }
9240}
9241
9242
9243/**
9244 * Update cache after object has been modified
Linus Walleija6d0d482007-10-31 08:54:56 +00009245 * @param device the device which may have a cache to which the object should be updated.
9246 * @param object_id the object to update.
tsaarnia3eb60a2007-07-06 17:41:30 +00009247 */
Linus Walleij7752b952007-10-19 20:11:46 +00009248static void update_metadata_cache(LIBMTP_mtpdevice_t *device, uint32_t object_id)
tsaarnia3eb60a2007-07-06 17:41:30 +00009249{
Linus Walleija3d0eaa2007-11-18 22:00:48 +00009250 PTPParams *params = (PTPParams *)device->params;
9251
9252 ptp_remove_object_from_cache(params, object_id);
Linus Walleija6d0d482007-10-31 08:54:56 +00009253 add_object_to_cache(device, object_id);
tsaarnia3eb60a2007-07-06 17:41:30 +00009254}
Marcin Niestroj718b2902017-06-10 16:36:18 +02009255
9256
9257/**
9258 * Issue custom (e.g. vendor specific) operation (without data phase)
9259 * @param device a pointer to the device to send custom operation to.
9260 * @param code operation code to send.
9261 * @param n_param number of parameters passed.
9262 * @param ... uint32_t operation specific parameters.
9263 */
9264int LIBMTP_Custom_Operation(LIBMTP_mtpdevice_t *device, uint16_t code, int n_param, ...)
9265{
9266 PTPParams *params = (PTPParams *) device->params;
9267 PTPContainer ptp;
9268 va_list args;
9269 uint16_t ret;
9270 int i;
9271
9272 ptp.Code = code;
9273 ptp.Nparam = n_param;
9274 va_start(args, n_param);
9275 for (i = 0; i < n_param; i++)
9276 (&ptp.Param1)[i] = va_arg(args, uint32_t);
9277 va_end(args);
9278
9279 ret = ptp_transaction_new(params, &ptp, PTP_DP_NODATA, 0, NULL);
9280
9281 if (ret != PTP_RC_OK) {
9282 add_ptp_error_to_errorstack(device, ret, "LIBMTP_Custom_Operation(): failed to execute operation.");
9283 return -1;
9284 }
9285
9286 return 0;
9287}