blob: a3067912cce715a33553e30a7070bca99449f127 [file] [log] [blame]
Linus Walleij10c58422006-06-02 08:51:53 +00001/**
2 * \file libmtp.c
Linus Walleij2f45d222007-02-02 22:47:39 +00003 *
Linus Walleij12bbf312009-01-26 21:46:51 +00004 * Copyright (C) 2005-2009 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 Walleij15e344f2006-03-06 15:15:00 +000041#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 Walleijeb8c6fe2006-02-03 09:46:22 +000044
Linus Walleij2eaaff02009-01-15 21:30:36 +000045#include <stdlib.h>
46#include <unistd.h>
Linus Walleij7beba572006-11-29 08:56:12 +000047#include <string.h>
48#include <sys/types.h>
49#include <sys/stat.h>
50#include <fcntl.h>
Linus Walleij4126b302007-09-22 22:13:55 +000051#include <time.h>
Linus Walleijf3c44052008-08-16 21:14:56 +000052#include <errno.h>
Linus Walleij7beba572006-11-29 08:56:12 +000053#ifdef _MSC_VER // For MSVC++
54#define USE_WINDOWS_IO_H
55#include <io.h>
56#endif
57
Linus Walleijd1e14e02008-12-14 01:07:30 +000058/* To enable PTP level debug prints (all ptp_debug(...)), switch on this */
59//#define ENABLE_PTP_DEBUG
60
Linus Walleijc86afbd2006-05-04 19:05:24 +000061/*
62 * This is a mapping between libmtp internal MTP filetypes and
63 * the libgphoto2/PTP equivalent defines. We need this because
64 * otherwise the libmtp.h device has to be dependent on ptp.h
65 * to be installed too, and we don't want that.
66 */
Linus Walleijcf42f452006-11-28 08:32:49 +000067//typedef struct filemap_struct filemap_t;
68typedef struct filemap_struct {
raveloxd9a28642006-05-26 23:42:22 +000069 char *description; /**< Text description for the file type */
70 LIBMTP_filetype_t id; /**< LIBMTP internal type for the file type */
71 uint16_t ptp_id; /**< PTP ID for the filetype */
Linus Walleijcf42f452006-11-28 08:32:49 +000072 struct filemap_struct *next;
73} filemap_t;
Linus Walleijc86afbd2006-05-04 19:05:24 +000074
Richard Low6f070842009-05-03 10:26:16 +000075/*
76 * This is a mapping between libmtp internal MTP properties and
77 * the libgphoto2/PTP equivalent defines. We need this because
78 * otherwise the libmtp.h device has to be dependent on ptp.h
79 * to be installed too, and we don't want that.
80 */
81typedef struct propertymap_struct {
82 char *description; /**< Text description for the property */
83 LIBMTP_property_t id; /**< LIBMTP internal type for the property */
84 uint16_t ptp_id; /**< PTP ID for the property */
85 struct propertymap_struct *next;
86} propertymap_t;
87
Linus Walleijf67bca92006-05-29 09:33:39 +000088// Global variables
Linus Walleij438bd7f2006-06-08 11:35:44 +000089// This holds the global filetype mapping table
Linus Walleijcf42f452006-11-28 08:32:49 +000090static filemap_t *filemap = NULL;
Richard Low6f070842009-05-03 10:26:16 +000091// This holds the global property mapping table
92static propertymap_t *propertymap = NULL;
Linus Walleijf67bca92006-05-29 09:33:39 +000093
Linus Walleijcf42f452006-11-28 08:32:49 +000094/*
95 * Forward declarations of local (static) functions.
96 */
97static int register_filetype(char const * const description, LIBMTP_filetype_t const id,
98 uint16_t const ptp_id);
Linus Walleij7beba572006-11-29 08:56:12 +000099static void init_filemap();
Richard Low6f070842009-05-03 10:26:16 +0000100static int register_property(char const * const description, LIBMTP_property_t const id,
101 uint16_t const ptp_id);
102static void init_propertymap();
Linus Walleij2715c442007-01-20 22:35:29 +0000103static void add_error_to_errorstack(LIBMTP_mtpdevice_t *device,
104 LIBMTP_error_number_t errornumber,
105 char const * const error_text);
Linus Walleij070e9b42007-01-22 08:49:28 +0000106static void add_ptp_error_to_errorstack(LIBMTP_mtpdevice_t *device,
107 uint16_t ptp_error,
108 char const * const error_text);
Linus Walleij438bd7f2006-06-08 11:35:44 +0000109static void flush_handles(LIBMTP_mtpdevice_t *device);
Linus Walleij6bf68b42007-09-03 22:50:02 +0000110static void get_handles_recursively(LIBMTP_mtpdevice_t *device,
111 PTPParams *params,
Linus Walleij6bf68b42007-09-03 22:50:02 +0000112 uint32_t storageid,
113 uint32_t parent);
Linus Walleij9e1b0812006-12-12 19:22:02 +0000114static void free_storage_list(LIBMTP_mtpdevice_t *device);
115static int sort_storage_by(LIBMTP_mtpdevice_t *device, int const sortby);
Linus Walleijd71d0b32008-09-22 08:21:03 +0000116static uint32_t get_writeable_storageid(LIBMTP_mtpdevice_t *device, uint64_t fitsize);
Linus Walleij6bf68b42007-09-03 22:50:02 +0000117static int get_storage_freespace(LIBMTP_mtpdevice_t *device,
118 LIBMTP_devicestorage_t *storage,
119 uint64_t *freespace);
120static int check_if_file_fits(LIBMTP_mtpdevice_t *device,
121 LIBMTP_devicestorage_t *storage,
122 uint64_t const filesize);
Linus Walleijf67bca92006-05-29 09:33:39 +0000123static uint16_t map_libmtp_type_to_ptp_type(LIBMTP_filetype_t intype);
124static LIBMTP_filetype_t map_ptp_type_to_libmtp_type(uint16_t intype);
Richard Low6f070842009-05-03 10:26:16 +0000125static uint16_t map_libmtp_property_to_ptp_property(LIBMTP_property_t inproperty);
126static LIBMTP_property_t map_ptp_property_to_libmtp_property(uint16_t intype);
mopoke96143402006-10-30 04:37:26 +0000127static int get_device_unicode_property(LIBMTP_mtpdevice_t *device,
Linus Walleijcf223e62006-06-19 09:31:53 +0000128 char **unicstring, uint16_t property);
Linus Walleij29559562007-08-22 21:38:04 +0000129static uint16_t adjust_u16(uint16_t val, PTPObjectPropDesc *opd);
130static uint32_t adjust_u32(uint32_t val, PTPObjectPropDesc *opd);
Linus Walleij7783b9b2007-09-22 22:10:44 +0000131static char *get_iso8601_stamp(void);
Linus Walleij9901e222006-11-30 12:28:19 +0000132static char *get_string_from_object(LIBMTP_mtpdevice_t *device, uint32_t const object_id,
133 uint16_t const attribute_id);
Richard Lowc3f5fc32007-07-29 09:40:50 +0000134static uint64_t get_u64_from_object(LIBMTP_mtpdevice_t *device,uint32_t const object_id,
135 uint16_t const attribute_id, uint64_t const value_default);
Linus Walleij9901e222006-11-30 12:28:19 +0000136static uint32_t get_u32_from_object(LIBMTP_mtpdevice_t *device,uint32_t const object_id,
137 uint16_t const attribute_id, uint32_t const value_default);
138static uint16_t get_u16_from_object(LIBMTP_mtpdevice_t *device, uint32_t const object_id,
139 uint16_t const attribute_id, uint16_t const value_default);
140static uint8_t get_u8_from_object(LIBMTP_mtpdevice_t *device, uint32_t const object_id,
141 uint16_t const attribute_id, uint8_t const value_default);
142static int set_object_string(LIBMTP_mtpdevice_t *device, uint32_t const object_id,
143 uint16_t const attribute_id, char const * const string);
144static int set_object_u32(LIBMTP_mtpdevice_t *device, uint32_t const object_id,
145 uint16_t const attribute_id, uint32_t const value);
146static int set_object_u16(LIBMTP_mtpdevice_t *device, uint32_t const object_id,
147 uint16_t const attribute_id, uint16_t const value);
148static int set_object_u8(LIBMTP_mtpdevice_t *device, uint32_t const object_id,
149 uint16_t const attribute_id, uint8_t const value);
Linus Walleij8ab54262006-06-21 07:12:28 +0000150static void get_track_metadata(LIBMTP_mtpdevice_t *device, uint16_t objectformat,
151 LIBMTP_track_t *track);
Linus Walleija89a7942008-12-14 23:19:34 +0000152static LIBMTP_folder_t *get_subfolders_for_folder(LIBMTP_folder_t *list, uint32_t parent);
Linus Walleij7beba572006-11-29 08:56:12 +0000153static int create_new_abstract_list(LIBMTP_mtpdevice_t *device,
154 char const * const name,
Linus Walleijadce4a52007-04-23 07:28:11 +0000155 char const * const artist,
Linus Walleij31b74292008-05-02 23:29:06 +0000156 char const * const composer,
Linus Walleijadce4a52007-04-23 07:28:11 +0000157 char const * const genre,
Linus Walleij7beba572006-11-29 08:56:12 +0000158 uint32_t const parenthandle,
Linus Walleijea68f1f2008-06-22 21:54:44 +0000159 uint32_t const storageid,
Linus Walleij7beba572006-11-29 08:56:12 +0000160 uint16_t const objectformat,
161 char const * const suffix,
162 uint32_t * const newid,
163 uint32_t const * const tracks,
164 uint32_t const no_tracks);
Linus Walleij7783b9b2007-09-22 22:10:44 +0000165static int update_abstract_list(LIBMTP_mtpdevice_t *device,
166 char const * const name,
167 char const * const artist,
Linus Walleij31b74292008-05-02 23:29:06 +0000168 char const * const composer,
Linus Walleij7783b9b2007-09-22 22:10:44 +0000169 char const * const genre,
170 uint32_t const objecthandle,
171 uint16_t const objectformat,
172 uint32_t const * const tracks,
173 uint32_t const no_tracks);
Richard Lowd3b17022009-04-11 12:37:39 +0000174static int send_file_object_info(LIBMTP_mtpdevice_t *device, LIBMTP_file_t *filedata);
Linus Walleija6d0d482007-10-31 08:54:56 +0000175static void add_object_to_cache(LIBMTP_mtpdevice_t *device, uint32_t object_id);
Linus Walleij7752b952007-10-19 20:11:46 +0000176static void update_metadata_cache(LIBMTP_mtpdevice_t *device, uint32_t object_id);
Linus Walleij8d8c4352008-09-23 23:04:16 +0000177static int set_object_filename(LIBMTP_mtpdevice_t *device,
178 uint32_t object_id,
179 uint16_t ptp_type,
180 const char **newname);
Richard Low5b4023c2009-04-16 19:14:38 +0000181
182/**
183 * These are to wrap the get/put handlers to convert from the MTP types to PTP types
184 * in a reliable way
185 */
186typedef struct _MTPDataHandler {
187 MTPDataGetFunc getfunc;
188 MTPDataPutFunc putfunc;
Linus Walleijd866d242009-08-23 21:50:39 +0000189 void *priv;
Richard Low5b4023c2009-04-16 19:14:38 +0000190} MTPDataHandler;
raveloxd9a28642006-05-26 23:42:22 +0000191
Linus Walleijd866d242009-08-23 21:50:39 +0000192static uint16_t get_func_wrapper(PTPParams* params, void* priv, unsigned long wantlen, unsigned char *data, unsigned long *gotlen);
193static uint16_t put_func_wrapper(PTPParams* params, void* priv, unsigned long sendlen, unsigned char *data, unsigned long *putlen);
Richard Low5b4023c2009-04-16 19:14:38 +0000194
Linus Walleijcf42f452006-11-28 08:32:49 +0000195/**
Linus Walleij01fc9c82009-03-10 23:52:09 +0000196 * Checks if a filename ends with ".ogg". Used in various
197 * situations when the device has no idea that it support
198 * OGG but still does.
Linus Walleijd98e8972009-03-08 22:57:17 +0000199 *
200 * @param name string to be checked.
201 * @return 0 if this does not end with ogg, any other
202 * value means it does.
203 */
204static int has_ogg_extension(char *name) {
205 char *ptype;
206
207 if (name == NULL)
208 return 0;
209 ptype = strrchr(name,'.');
210 if (ptype == NULL)
211 return 0;
212 if (!strcasecmp (ptype, ".ogg"))
213 return 1;
214 return 0;
215}
216
Linus Walleij89bb1cd2009-07-24 21:03:36 +0000217/**
218 * Checks if a filename ends with ".flac". Used in various
219 * situations when the device has no idea that it support
220 * FLAC but still does.
221 *
222 * @param name string to be checked.
223 * @return 0 if this does not end with flac, any other
224 * value means it does.
225 */
226static int has_flac_extension(char *name) {
227 char *ptype;
228
229 if (name == NULL)
230 return 0;
231 ptype = strrchr(name,'.');
232 if (ptype == NULL)
233 return 0;
234 if (!strcasecmp (ptype, ".flac"))
235 return 1;
236 return 0;
237}
238
239
Linus Walleijd98e8972009-03-08 22:57:17 +0000240
241/**
Linus Walleijcf42f452006-11-28 08:32:49 +0000242 * Create a new file mapping entry
243 * @return a newly allocated filemapping entry.
244 */
245static filemap_t *new_filemap_entry()
raveloxd9a28642006-05-26 23:42:22 +0000246{
Linus Walleijcf42f452006-11-28 08:32:49 +0000247 filemap_t *filemap;
mopoke96143402006-10-30 04:37:26 +0000248
Linus Walleijcf42f452006-11-28 08:32:49 +0000249 filemap = (filemap_t *)malloc(sizeof(filemap_t));
mopoke96143402006-10-30 04:37:26 +0000250
Linus Walleijf67bca92006-05-29 09:33:39 +0000251 if( filemap != NULL ) {
252 filemap->description = NULL;
253 filemap->id = LIBMTP_FILETYPE_UNKNOWN;
254 filemap->ptp_id = PTP_OFC_Undefined;
Linus Walleijf67bca92006-05-29 09:33:39 +0000255 filemap->next = NULL;
256 }
mopoke96143402006-10-30 04:37:26 +0000257
Linus Walleijf67bca92006-05-29 09:33:39 +0000258 return filemap;
raveloxd9a28642006-05-26 23:42:22 +0000259}
260
raveloxd9a28642006-05-26 23:42:22 +0000261/**
262 * Register an MTP or PTP filetype for data retrieval
263 *
264 * @param description Text description of filetype
Linus Walleijf0f3d482006-05-29 14:10:21 +0000265 * @param id libmtp internal filetype id
raveloxd9a28642006-05-26 23:42:22 +0000266 * @param ptp_id PTP filetype id
Linus Walleijf0f3d482006-05-29 14:10:21 +0000267 * @return 0 for success any other value means error.
raveloxd9a28642006-05-26 23:42:22 +0000268*/
Linus Walleijcf42f452006-11-28 08:32:49 +0000269static int register_filetype(char const * const description, LIBMTP_filetype_t const id,
270 uint16_t const ptp_id)
raveloxd9a28642006-05-26 23:42:22 +0000271{
Linus Walleijcf42f452006-11-28 08:32:49 +0000272 filemap_t *new = NULL, *current;
raveloxd9a28642006-05-26 23:42:22 +0000273
274 // Has this LIBMTP filetype been registered before ?
275 current = filemap;
276 while (current != NULL) {
Linus Walleijf67bca92006-05-29 09:33:39 +0000277 if(current->id == id) {
278 break;
279 }
Linus Walleijf0f3d482006-05-29 14:10:21 +0000280 current = current->next;
raveloxd9a28642006-05-26 23:42:22 +0000281 }
mopoke96143402006-10-30 04:37:26 +0000282
raveloxd9a28642006-05-26 23:42:22 +0000283 // Create the entry
284 if(current == NULL) {
Linus Walleijf67bca92006-05-29 09:33:39 +0000285 new = new_filemap_entry();
Linus Walleijf0f3d482006-05-29 14:10:21 +0000286 if(new == NULL) {
287 return 1;
288 }
mopoke96143402006-10-30 04:37:26 +0000289
Linus Walleijf67bca92006-05-29 09:33:39 +0000290 new->id = id;
Linus Walleij438bd7f2006-06-08 11:35:44 +0000291 if(description != NULL) {
292 new->description = strdup(description);
293 }
Linus Walleijf67bca92006-05-29 09:33:39 +0000294 new->ptp_id = ptp_id;
mopoke96143402006-10-30 04:37:26 +0000295
Linus Walleijf67bca92006-05-29 09:33:39 +0000296 // Add the entry to the list
297 if(filemap == NULL) {
298 filemap = new;
299 } else {
300 current = filemap;
301 while (current->next != NULL ) current=current->next;
302 current->next = new;
303 }
304 // Update the existing entry
raveloxd9a28642006-05-26 23:42:22 +0000305 } else {
Linus Walleij438bd7f2006-06-08 11:35:44 +0000306 if (current->description != NULL) {
307 free(current->description);
308 }
Linus Walleijf67bca92006-05-29 09:33:39 +0000309 current->description = NULL;
Linus Walleijf0f3d482006-05-29 14:10:21 +0000310 if(description != NULL) {
Linus Walleij438bd7f2006-06-08 11:35:44 +0000311 current->description = strdup(description);
Linus Walleijf0f3d482006-05-29 14:10:21 +0000312 }
Linus Walleijf67bca92006-05-29 09:33:39 +0000313 current->ptp_id = ptp_id;
raveloxd9a28642006-05-26 23:42:22 +0000314 }
315
Linus Walleijf0f3d482006-05-29 14:10:21 +0000316 return 0;
raveloxd9a28642006-05-26 23:42:22 +0000317}
318
raveloxd9a28642006-05-26 23:42:22 +0000319static void init_filemap()
320{
Linus Walleij5fb47132006-12-30 15:35:48 +0000321 register_filetype("MediaCard", LIBMTP_FILETYPE_MEDIACARD, PTP_OFC_MTP_MediaCard);
Linus Walleijcf42f452006-11-28 08:32:49 +0000322 register_filetype("RIFF WAVE file", LIBMTP_FILETYPE_WAV, PTP_OFC_WAV);
Linus Walleij5fb47132006-12-30 15:35:48 +0000323 register_filetype("ISO MPEG-1 Audio Layer 3", LIBMTP_FILETYPE_MP3, PTP_OFC_MP3);
324 register_filetype("ISO MPEG-1 Audio Layer 2", LIBMTP_FILETYPE_MP2, PTP_OFC_MTP_MP2);
Linus Walleijcf42f452006-11-28 08:32:49 +0000325 register_filetype("Microsoft Windows Media Audio", LIBMTP_FILETYPE_WMA, PTP_OFC_MTP_WMA);
326 register_filetype("Ogg container format", LIBMTP_FILETYPE_OGG, PTP_OFC_MTP_OGG);
Linus Walleij5fb47132006-12-30 15:35:48 +0000327 register_filetype("Free Lossless Audio Codec (FLAC)", LIBMTP_FILETYPE_FLAC, PTP_OFC_MTP_FLAC);
328 register_filetype("Advanced Audio Coding (AAC)/MPEG-2 Part 7/MPEG-4 Part 3", LIBMTP_FILETYPE_AAC, PTP_OFC_MTP_AAC);
Richard Lowd3b17022009-04-11 12:37:39 +0000329 register_filetype("MPEG-4 Part 14 Container Format (Audio Emphasis)", LIBMTP_FILETYPE_M4A, PTP_OFC_MTP_M4A);
330 register_filetype("MPEG-4 Part 14 Container Format (Audio+Video Emphasis)", LIBMTP_FILETYPE_MP4, PTP_OFC_MTP_MP4);
Linus Walleijcf42f452006-11-28 08:32:49 +0000331 register_filetype("Audible.com Audio Codec", LIBMTP_FILETYPE_AUDIBLE, PTP_OFC_MTP_AudibleCodec);
Linus Walleijcf42f452006-11-28 08:32:49 +0000332 register_filetype("Undefined audio file", LIBMTP_FILETYPE_UNDEF_AUDIO, PTP_OFC_MTP_UndefinedAudio);
333 register_filetype("Microsoft Windows Media Video", LIBMTP_FILETYPE_WMV, PTP_OFC_MTP_WMV);
334 register_filetype("Audio Video Interleave", LIBMTP_FILETYPE_AVI, PTP_OFC_AVI);
335 register_filetype("MPEG video stream", LIBMTP_FILETYPE_MPEG, PTP_OFC_MPEG);
336 register_filetype("Microsoft Advanced Systems Format", LIBMTP_FILETYPE_ASF, PTP_OFC_ASF);
337 register_filetype("Apple Quicktime container format", LIBMTP_FILETYPE_QT, PTP_OFC_QT);
338 register_filetype("Undefined video file", LIBMTP_FILETYPE_UNDEF_VIDEO, PTP_OFC_MTP_UndefinedVideo);
339 register_filetype("JPEG file", LIBMTP_FILETYPE_JPEG, PTP_OFC_EXIF_JPEG);
Linus Walleij5fb47132006-12-30 15:35:48 +0000340 register_filetype("JP2 file", LIBMTP_FILETYPE_JP2, PTP_OFC_JP2);
341 register_filetype("JPX file", LIBMTP_FILETYPE_JPX, PTP_OFC_JPX);
Linus Walleijcf42f452006-11-28 08:32:49 +0000342 register_filetype("JFIF file", LIBMTP_FILETYPE_JFIF, PTP_OFC_JFIF);
343 register_filetype("TIFF bitmap file", LIBMTP_FILETYPE_TIFF, PTP_OFC_TIFF);
344 register_filetype("BMP bitmap file", LIBMTP_FILETYPE_BMP, PTP_OFC_BMP);
345 register_filetype("GIF bitmap file", LIBMTP_FILETYPE_GIF, PTP_OFC_GIF);
346 register_filetype("PICT bitmap file", LIBMTP_FILETYPE_PICT, PTP_OFC_PICT);
347 register_filetype("Portable Network Graphics", LIBMTP_FILETYPE_PNG, PTP_OFC_PNG);
348 register_filetype("Microsoft Windows Image Format", LIBMTP_FILETYPE_WINDOWSIMAGEFORMAT, PTP_OFC_MTP_WindowsImageFormat);
349 register_filetype("VCalendar version 1", LIBMTP_FILETYPE_VCALENDAR1, PTP_OFC_MTP_vCalendar1);
350 register_filetype("VCalendar version 2", LIBMTP_FILETYPE_VCALENDAR2, PTP_OFC_MTP_vCalendar2);
351 register_filetype("VCard version 2", LIBMTP_FILETYPE_VCARD2, PTP_OFC_MTP_vCard2);
352 register_filetype("VCard version 3", LIBMTP_FILETYPE_VCARD3, PTP_OFC_MTP_vCard3);
353 register_filetype("Undefined Windows executable file", LIBMTP_FILETYPE_WINEXEC, PTP_OFC_MTP_UndefinedWindowsExecutable);
354 register_filetype("Text file", LIBMTP_FILETYPE_TEXT, PTP_OFC_Text);
355 register_filetype("HTML file", LIBMTP_FILETYPE_HTML, PTP_OFC_HTML);
Linus Walleij5fb47132006-12-30 15:35:48 +0000356 register_filetype("XML file", LIBMTP_FILETYPE_XML, PTP_OFC_MTP_XMLDocument);
357 register_filetype("DOC file", LIBMTP_FILETYPE_DOC, PTP_OFC_MTP_MSWordDocument);
358 register_filetype("XLS file", LIBMTP_FILETYPE_XLS, PTP_OFC_MTP_MSExcelSpreadsheetXLS);
359 register_filetype("PPT file", LIBMTP_FILETYPE_PPT, PTP_OFC_MTP_MSPowerpointPresentationPPT);
360 register_filetype("MHT file", LIBMTP_FILETYPE_MHT, PTP_OFC_MTP_MHTCompiledHTMLDocument);
Linus Walleija05b9802006-12-08 08:50:30 +0000361 register_filetype("Firmware file", LIBMTP_FILETYPE_FIRMWARE, PTP_OFC_MTP_Firmware);
Richard Lowd3b17022009-04-11 12:37:39 +0000362 register_filetype("Abstract Album file", LIBMTP_FILETYPE_ALBUM, PTP_OFC_MTP_AbstractAudioAlbum);
363 register_filetype("Abstract Playlist file", LIBMTP_FILETYPE_PLAYLIST, PTP_OFC_MTP_AbstractAudioVideoPlaylist);
Linus Walleijcf42f452006-11-28 08:32:49 +0000364 register_filetype("Undefined filetype", LIBMTP_FILETYPE_UNKNOWN, PTP_OFC_Undefined);
raveloxd9a28642006-05-26 23:42:22 +0000365}
Linus Walleij16c51f02006-05-04 13:20:22 +0000366
Linus Walleij16c51f02006-05-04 13:20:22 +0000367/**
368 * Returns the PTP filetype that maps to a certain libmtp internal file type.
369 * @param intype the MTP library interface type
370 * @return the PTP (libgphoto2) interface type
371 */
372static uint16_t map_libmtp_type_to_ptp_type(LIBMTP_filetype_t intype)
373{
Linus Walleijcf42f452006-11-28 08:32:49 +0000374 filemap_t *current;
Linus Walleij16c51f02006-05-04 13:20:22 +0000375
raveloxd9a28642006-05-26 23:42:22 +0000376 current = filemap;
377
378 while (current != NULL) {
Linus Walleijf0f3d482006-05-29 14:10:21 +0000379 if(current->id == intype) {
380 return current->ptp_id;
381 }
382 current = current->next;
Linus Walleij16c51f02006-05-04 13:20:22 +0000383 }
Linus Walleij1fd2d272006-05-08 09:22:01 +0000384 // printf("map_libmtp_type_to_ptp_type: unknown filetype.\n");
Linus Walleij16c51f02006-05-04 13:20:22 +0000385 return PTP_OFC_Undefined;
386}
387
388
389/**
Linus Walleij31b74292008-05-02 23:29:06 +0000390 * Returns the MTP internal interface type that maps to a certain ptp
391 * interface type.
mopoke96143402006-10-30 04:37:26 +0000392 * @param intype the PTP (libgphoto2) interface type
Linus Walleij8ab54262006-06-21 07:12:28 +0000393 * @return the MTP library interface type
Linus Walleij16c51f02006-05-04 13:20:22 +0000394 */
395static LIBMTP_filetype_t map_ptp_type_to_libmtp_type(uint16_t intype)
396{
Linus Walleijcf42f452006-11-28 08:32:49 +0000397 filemap_t *current;
Linus Walleij16c51f02006-05-04 13:20:22 +0000398
raveloxd9a28642006-05-26 23:42:22 +0000399 current = filemap;
400
401 while (current != NULL) {
Linus Walleijf0f3d482006-05-29 14:10:21 +0000402 if(current->ptp_id == intype) {
403 return current->id;
404 }
405 current = current->next;
Linus Walleij16c51f02006-05-04 13:20:22 +0000406 }
Linus Walleij1fd2d272006-05-08 09:22:01 +0000407 // printf("map_ptp_type_to_libmtp_type: unknown filetype.\n");
Linus Walleij16c51f02006-05-04 13:20:22 +0000408 return LIBMTP_FILETYPE_UNKNOWN;
409}
410
Richard Low6f070842009-05-03 10:26:16 +0000411/**
412 * Create a new property mapping entry
413 * @return a newly allocated propertymapping entry.
414 */
415static propertymap_t *new_propertymap_entry()
416{
417 propertymap_t *propertymap;
418
419 propertymap = (propertymap_t *)malloc(sizeof(propertymap_t));
420
421 if( propertymap != NULL ) {
422 propertymap->description = NULL;
423 propertymap->id = LIBMTP_PROPERTY_UNKNOWN;
424 propertymap->ptp_id = 0;
425 propertymap->next = NULL;
426 }
427
428 return propertymap;
429}
430
431/**
432 * Register an MTP or PTP property for data retrieval
433 *
434 * @param description Text description of property
435 * @param id libmtp internal property id
436 * @param ptp_id PTP property id
437 * @return 0 for success any other value means error.
438*/
439static int register_property(char const * const description, LIBMTP_property_t const id,
440 uint16_t const ptp_id)
441{
442 propertymap_t *new = NULL, *current;
443
444 // Has this LIBMTP propety been registered before ?
445 current = propertymap;
446 while (current != NULL) {
447 if(current->id == id) {
448 break;
449 }
450 current = current->next;
451 }
452
453 // Create the entry
454 if(current == NULL) {
455 new = new_propertymap_entry();
456 if(new == NULL) {
457 return 1;
458 }
459
460 new->id = id;
461 if(description != NULL) {
462 new->description = strdup(description);
463 }
464 new->ptp_id = ptp_id;
465
466 // Add the entry to the list
467 if(propertymap == NULL) {
468 propertymap = new;
469 } else {
470 current = propertymap;
471 while (current->next != NULL ) current=current->next;
472 current->next = new;
473 }
474 // Update the existing entry
475 } else {
476 if (current->description != NULL) {
477 free(current->description);
478 }
479 current->description = NULL;
480 if(description != NULL) {
481 current->description = strdup(description);
482 }
483 current->ptp_id = ptp_id;
484 }
485
486 return 0;
487}
488
489static void init_propertymap()
490{
491 register_property("Storage ID", LIBMTP_PROPERTY_StorageID, PTP_OPC_StorageID);
492 register_property("Object Format", LIBMTP_PROPERTY_ObjectFormat, PTP_OPC_ObjectFormat);
493 register_property("Protection Status", LIBMTP_PROPERTY_ProtectionStatus, PTP_OPC_ProtectionStatus);
494 register_property("Object Size", LIBMTP_PROPERTY_ObjectSize, PTP_OPC_ObjectSize);
495 register_property("Association Type", LIBMTP_PROPERTY_AssociationType, PTP_OPC_AssociationType);
496 register_property("Association Desc", LIBMTP_PROPERTY_AssociationDesc, PTP_OPC_AssociationDesc);
497 register_property("Object File Name", LIBMTP_PROPERTY_ObjectFileName, PTP_OPC_ObjectFileName);
498 register_property("Date Created", LIBMTP_PROPERTY_DateCreated, PTP_OPC_DateCreated);
499 register_property("Date Modified", LIBMTP_PROPERTY_DateModified, PTP_OPC_DateModified);
500 register_property("Keywords", LIBMTP_PROPERTY_Keywords, PTP_OPC_Keywords);
501 register_property("Parent Object", LIBMTP_PROPERTY_ParentObject, PTP_OPC_ParentObject);
502 register_property("Allowed Folder Contents", LIBMTP_PROPERTY_AllowedFolderContents, PTP_OPC_AllowedFolderContents);
503 register_property("Hidden", LIBMTP_PROPERTY_Hidden, PTP_OPC_Hidden);
504 register_property("System Object", LIBMTP_PROPERTY_SystemObject, PTP_OPC_SystemObject);
505 register_property("Persistant Unique Object Identifier", LIBMTP_PROPERTY_PersistantUniqueObjectIdentifier, PTP_OPC_PersistantUniqueObjectIdentifier);
506 register_property("Sync ID", LIBMTP_PROPERTY_SyncID, PTP_OPC_SyncID);
507 register_property("Property Bag", LIBMTP_PROPERTY_PropertyBag, PTP_OPC_PropertyBag);
508 register_property("Name", LIBMTP_PROPERTY_Name, PTP_OPC_Name);
509 register_property("Created By", LIBMTP_PROPERTY_CreatedBy, PTP_OPC_CreatedBy);
510 register_property("Artist", LIBMTP_PROPERTY_Artist, PTP_OPC_Artist);
511 register_property("Date Authored", LIBMTP_PROPERTY_DateAuthored, PTP_OPC_DateAuthored);
512 register_property("Description", LIBMTP_PROPERTY_Description, PTP_OPC_Description);
513 register_property("URL Reference", LIBMTP_PROPERTY_URLReference, PTP_OPC_URLReference);
514 register_property("Language Locale", LIBMTP_PROPERTY_LanguageLocale, PTP_OPC_LanguageLocale);
515 register_property("Copyright Information", LIBMTP_PROPERTY_CopyrightInformation, PTP_OPC_CopyrightInformation);
516 register_property("Source", LIBMTP_PROPERTY_Source, PTP_OPC_Source);
517 register_property("Origin Location", LIBMTP_PROPERTY_OriginLocation, PTP_OPC_OriginLocation);
518 register_property("Date Added", LIBMTP_PROPERTY_DateAdded, PTP_OPC_DateAdded);
519 register_property("Non Consumable", LIBMTP_PROPERTY_NonConsumable, PTP_OPC_NonConsumable);
520 register_property("Corrupt Or Unplayable", LIBMTP_PROPERTY_CorruptOrUnplayable, PTP_OPC_CorruptOrUnplayable);
521 register_property("Producer Serial Number", LIBMTP_PROPERTY_ProducerSerialNumber, PTP_OPC_ProducerSerialNumber);
522 register_property("Representative Sample Format", LIBMTP_PROPERTY_RepresentativeSampleFormat, PTP_OPC_RepresentativeSampleFormat);
523 register_property("Representative Sample Sise", LIBMTP_PROPERTY_RepresentativeSampleSize, PTP_OPC_RepresentativeSampleSize);
524 register_property("Representative Sample Height", LIBMTP_PROPERTY_RepresentativeSampleHeight, PTP_OPC_RepresentativeSampleHeight);
525 register_property("Representative Sample Width", LIBMTP_PROPERTY_RepresentativeSampleWidth, PTP_OPC_RepresentativeSampleWidth);
526 register_property("Representative Sample Duration", LIBMTP_PROPERTY_RepresentativeSampleDuration, PTP_OPC_RepresentativeSampleDuration);
527 register_property("Representative Sample Data", LIBMTP_PROPERTY_RepresentativeSampleData, PTP_OPC_RepresentativeSampleData);
528 register_property("Width", LIBMTP_PROPERTY_Width, PTP_OPC_Width);
529 register_property("Height", LIBMTP_PROPERTY_Height, PTP_OPC_Height);
530 register_property("Duration", LIBMTP_PROPERTY_Duration, PTP_OPC_Duration);
531 register_property("Rating", LIBMTP_PROPERTY_Rating, PTP_OPC_Rating);
532 register_property("Track", LIBMTP_PROPERTY_Track, PTP_OPC_Track);
533 register_property("Genre", LIBMTP_PROPERTY_Genre, PTP_OPC_Genre);
534 register_property("Credits", LIBMTP_PROPERTY_Credits, PTP_OPC_Credits);
535 register_property("Lyrics", LIBMTP_PROPERTY_Lyrics, PTP_OPC_Lyrics);
536 register_property("Subscription Content ID", LIBMTP_PROPERTY_SubscriptionContentID, PTP_OPC_SubscriptionContentID);
537 register_property("Produced By", LIBMTP_PROPERTY_ProducedBy, PTP_OPC_ProducedBy);
538 register_property("Use Count", LIBMTP_PROPERTY_UseCount, PTP_OPC_UseCount);
539 register_property("Skip Count", LIBMTP_PROPERTY_SkipCount, PTP_OPC_SkipCount);
540 register_property("Last Accessed", LIBMTP_PROPERTY_LastAccessed, PTP_OPC_LastAccessed);
541 register_property("Parental Rating", LIBMTP_PROPERTY_ParentalRating, PTP_OPC_ParentalRating);
542 register_property("Meta Genre", LIBMTP_PROPERTY_MetaGenre, PTP_OPC_MetaGenre);
543 register_property("Composer", LIBMTP_PROPERTY_Composer, PTP_OPC_Composer);
544 register_property("Effective Rating", LIBMTP_PROPERTY_EffectiveRating, PTP_OPC_EffectiveRating);
545 register_property("Subtitle", LIBMTP_PROPERTY_Subtitle, PTP_OPC_Subtitle);
546 register_property("Original Release Date", LIBMTP_PROPERTY_OriginalReleaseDate, PTP_OPC_OriginalReleaseDate);
547 register_property("Album Name", LIBMTP_PROPERTY_AlbumName, PTP_OPC_AlbumName);
548 register_property("Album Artist", LIBMTP_PROPERTY_AlbumArtist, PTP_OPC_AlbumArtist);
549 register_property("Mood", LIBMTP_PROPERTY_Mood, PTP_OPC_Mood);
550 register_property("DRM Status", LIBMTP_PROPERTY_DRMStatus, PTP_OPC_DRMStatus);
551 register_property("Sub Description", LIBMTP_PROPERTY_SubDescription, PTP_OPC_SubDescription);
552 register_property("Is Cropped", LIBMTP_PROPERTY_IsCropped, PTP_OPC_IsCropped);
553 register_property("Is Color Corrected", LIBMTP_PROPERTY_IsColorCorrected, PTP_OPC_IsColorCorrected);
554 register_property("Image Bit Depth", LIBMTP_PROPERTY_ImageBitDepth, PTP_OPC_ImageBitDepth);
555 register_property("f Number", LIBMTP_PROPERTY_Fnumber, PTP_OPC_Fnumber);
556 register_property("Exposure Time", LIBMTP_PROPERTY_ExposureTime, PTP_OPC_ExposureTime);
557 register_property("Exposure Index", LIBMTP_PROPERTY_ExposureIndex, PTP_OPC_ExposureIndex);
558 register_property("Display Name", LIBMTP_PROPERTY_DisplayName, PTP_OPC_DisplayName);
559 register_property("Body Text", LIBMTP_PROPERTY_BodyText, PTP_OPC_BodyText);
560 register_property("Subject", LIBMTP_PROPERTY_Subject, PTP_OPC_Subject);
561 register_property("Priority", LIBMTP_PROPERTY_Priority, PTP_OPC_Priority);
562 register_property("Given Name", LIBMTP_PROPERTY_GivenName, PTP_OPC_GivenName);
563 register_property("Middle Names", LIBMTP_PROPERTY_MiddleNames, PTP_OPC_MiddleNames);
564 register_property("Family Name", LIBMTP_PROPERTY_FamilyName, PTP_OPC_FamilyName);
565 register_property("Prefix", LIBMTP_PROPERTY_Prefix, PTP_OPC_Prefix);
566 register_property("Suffix", LIBMTP_PROPERTY_Suffix, PTP_OPC_Suffix);
567 register_property("Phonetic Given Name", LIBMTP_PROPERTY_PhoneticGivenName, PTP_OPC_PhoneticGivenName);
568 register_property("Phonetic Family Name", LIBMTP_PROPERTY_PhoneticFamilyName, PTP_OPC_PhoneticFamilyName);
569 register_property("Email: Primary", LIBMTP_PROPERTY_EmailPrimary, PTP_OPC_EmailPrimary);
570 register_property("Email: Personal 1", LIBMTP_PROPERTY_EmailPersonal1, PTP_OPC_EmailPersonal1);
571 register_property("Email: Personal 2", LIBMTP_PROPERTY_EmailPersonal2, PTP_OPC_EmailPersonal2);
572 register_property("Email: Business 1", LIBMTP_PROPERTY_EmailBusiness1, PTP_OPC_EmailBusiness1);
573 register_property("Email: Business 2", LIBMTP_PROPERTY_EmailBusiness2, PTP_OPC_EmailBusiness2);
574 register_property("Email: Others", LIBMTP_PROPERTY_EmailOthers, PTP_OPC_EmailOthers);
575 register_property("Phone Number: Primary", LIBMTP_PROPERTY_PhoneNumberPrimary, PTP_OPC_PhoneNumberPrimary);
576 register_property("Phone Number: Personal", LIBMTP_PROPERTY_PhoneNumberPersonal, PTP_OPC_PhoneNumberPersonal);
577 register_property("Phone Number: Personal 2", LIBMTP_PROPERTY_PhoneNumberPersonal2, PTP_OPC_PhoneNumberPersonal2);
578 register_property("Phone Number: Business", LIBMTP_PROPERTY_PhoneNumberBusiness, PTP_OPC_PhoneNumberBusiness);
579 register_property("Phone Number: Business 2", LIBMTP_PROPERTY_PhoneNumberBusiness2, PTP_OPC_PhoneNumberBusiness2);
580 register_property("Phone Number: Mobile", LIBMTP_PROPERTY_PhoneNumberMobile, PTP_OPC_PhoneNumberMobile);
581 register_property("Phone Number: Mobile 2", LIBMTP_PROPERTY_PhoneNumberMobile2, PTP_OPC_PhoneNumberMobile2);
582 register_property("Fax Number: Primary", LIBMTP_PROPERTY_FaxNumberPrimary, PTP_OPC_FaxNumberPrimary);
583 register_property("Fax Number: Personal", LIBMTP_PROPERTY_FaxNumberPersonal, PTP_OPC_FaxNumberPersonal);
584 register_property("Fax Number: Business", LIBMTP_PROPERTY_FaxNumberBusiness, PTP_OPC_FaxNumberBusiness);
585 register_property("Pager Number", LIBMTP_PROPERTY_PagerNumber, PTP_OPC_PagerNumber);
586 register_property("Phone Number: Others", LIBMTP_PROPERTY_PhoneNumberOthers, PTP_OPC_PhoneNumberOthers);
587 register_property("Primary Web Address", LIBMTP_PROPERTY_PrimaryWebAddress, PTP_OPC_PrimaryWebAddress);
588 register_property("Personal Web Address", LIBMTP_PROPERTY_PersonalWebAddress, PTP_OPC_PersonalWebAddress);
589 register_property("Business Web Address", LIBMTP_PROPERTY_BusinessWebAddress, PTP_OPC_BusinessWebAddress);
590 register_property("Instant Messenger Address 1", LIBMTP_PROPERTY_InstantMessengerAddress, PTP_OPC_InstantMessengerAddress);
591 register_property("Instant Messenger Address 2", LIBMTP_PROPERTY_InstantMessengerAddress2, PTP_OPC_InstantMessengerAddress2);
592 register_property("Instant Messenger Address 3", LIBMTP_PROPERTY_InstantMessengerAddress3, PTP_OPC_InstantMessengerAddress3);
593 register_property("Postal Address: Personal: Full", LIBMTP_PROPERTY_PostalAddressPersonalFull, PTP_OPC_PostalAddressPersonalFull);
594 register_property("Postal Address: Personal: Line 1", LIBMTP_PROPERTY_PostalAddressPersonalFullLine1, PTP_OPC_PostalAddressPersonalFullLine1);
595 register_property("Postal Address: Personal: Line 2", LIBMTP_PROPERTY_PostalAddressPersonalFullLine2, PTP_OPC_PostalAddressPersonalFullLine2);
596 register_property("Postal Address: Personal: City", LIBMTP_PROPERTY_PostalAddressPersonalFullCity, PTP_OPC_PostalAddressPersonalFullCity);
597 register_property("Postal Address: Personal: Region", LIBMTP_PROPERTY_PostalAddressPersonalFullRegion, PTP_OPC_PostalAddressPersonalFullRegion);
598 register_property("Postal Address: Personal: Postal Code", LIBMTP_PROPERTY_PostalAddressPersonalFullPostalCode, PTP_OPC_PostalAddressPersonalFullPostalCode);
599 register_property("Postal Address: Personal: Country", LIBMTP_PROPERTY_PostalAddressPersonalFullCountry, PTP_OPC_PostalAddressPersonalFullCountry);
600 register_property("Postal Address: Business: Full", LIBMTP_PROPERTY_PostalAddressBusinessFull, PTP_OPC_PostalAddressBusinessFull);
601 register_property("Postal Address: Business: Line 1", LIBMTP_PROPERTY_PostalAddressBusinessLine1, PTP_OPC_PostalAddressBusinessLine1);
602 register_property("Postal Address: Business: Line 2", LIBMTP_PROPERTY_PostalAddressBusinessLine2, PTP_OPC_PostalAddressBusinessLine2);
603 register_property("Postal Address: Business: City", LIBMTP_PROPERTY_PostalAddressBusinessCity, PTP_OPC_PostalAddressBusinessCity);
604 register_property("Postal Address: Business: Region", LIBMTP_PROPERTY_PostalAddressBusinessRegion, PTP_OPC_PostalAddressBusinessRegion);
605 register_property("Postal Address: Business: Postal Code", LIBMTP_PROPERTY_PostalAddressBusinessPostalCode, PTP_OPC_PostalAddressBusinessPostalCode);
606 register_property("Postal Address: Business: Country", LIBMTP_PROPERTY_PostalAddressBusinessCountry, PTP_OPC_PostalAddressBusinessCountry);
607 register_property("Postal Address: Other: Full", LIBMTP_PROPERTY_PostalAddressOtherFull, PTP_OPC_PostalAddressOtherFull);
608 register_property("Postal Address: Other: Line 1", LIBMTP_PROPERTY_PostalAddressOtherLine1, PTP_OPC_PostalAddressOtherLine1);
609 register_property("Postal Address: Other: Line 2", LIBMTP_PROPERTY_PostalAddressOtherLine2, PTP_OPC_PostalAddressOtherLine2);
610 register_property("Postal Address: Other: City", LIBMTP_PROPERTY_PostalAddressOtherCity, PTP_OPC_PostalAddressOtherCity);
611 register_property("Postal Address: Other: Region", LIBMTP_PROPERTY_PostalAddressOtherRegion, PTP_OPC_PostalAddressOtherRegion);
612 register_property("Postal Address: Other: Postal Code", LIBMTP_PROPERTY_PostalAddressOtherPostalCode, PTP_OPC_PostalAddressOtherPostalCode);
613 register_property("Postal Address: Other: Counrtry", LIBMTP_PROPERTY_PostalAddressOtherCountry, PTP_OPC_PostalAddressOtherCountry);
614 register_property("Organization Name", LIBMTP_PROPERTY_OrganizationName, PTP_OPC_OrganizationName);
615 register_property("Phonetic Organization Name", LIBMTP_PROPERTY_PhoneticOrganizationName, PTP_OPC_PhoneticOrganizationName);
616 register_property("Role", LIBMTP_PROPERTY_Role, PTP_OPC_Role);
617 register_property("Birthdate", LIBMTP_PROPERTY_Birthdate, PTP_OPC_Birthdate);
618 register_property("Message To", LIBMTP_PROPERTY_MessageTo, PTP_OPC_MessageTo);
619 register_property("Message CC", LIBMTP_PROPERTY_MessageCC, PTP_OPC_MessageCC);
620 register_property("Message BCC", LIBMTP_PROPERTY_MessageBCC, PTP_OPC_MessageBCC);
621 register_property("Message Read", LIBMTP_PROPERTY_MessageRead, PTP_OPC_MessageRead);
622 register_property("Message Received Time", LIBMTP_PROPERTY_MessageReceivedTime, PTP_OPC_MessageReceivedTime);
623 register_property("Message Sender", LIBMTP_PROPERTY_MessageSender, PTP_OPC_MessageSender);
624 register_property("Activity Begin Time", LIBMTP_PROPERTY_ActivityBeginTime, PTP_OPC_ActivityBeginTime);
625 register_property("Activity End Time", LIBMTP_PROPERTY_ActivityEndTime, PTP_OPC_ActivityEndTime);
626 register_property("Activity Location", LIBMTP_PROPERTY_ActivityLocation, PTP_OPC_ActivityLocation);
627 register_property("Activity Required Attendees", LIBMTP_PROPERTY_ActivityRequiredAttendees, PTP_OPC_ActivityRequiredAttendees);
628 register_property("Optional Attendees", LIBMTP_PROPERTY_ActivityOptionalAttendees, PTP_OPC_ActivityOptionalAttendees);
629 register_property("Activity Resources", LIBMTP_PROPERTY_ActivityResources, PTP_OPC_ActivityResources);
630 register_property("Activity Accepted", LIBMTP_PROPERTY_ActivityAccepted, PTP_OPC_ActivityAccepted);
631 register_property("Owner", LIBMTP_PROPERTY_Owner, PTP_OPC_Owner);
632 register_property("Editor", LIBMTP_PROPERTY_Editor, PTP_OPC_Editor);
633 register_property("Webmaster", LIBMTP_PROPERTY_Webmaster, PTP_OPC_Webmaster);
634 register_property("URL Source", LIBMTP_PROPERTY_URLSource, PTP_OPC_URLSource);
635 register_property("URL Destination", LIBMTP_PROPERTY_URLDestination, PTP_OPC_URLDestination);
636 register_property("Time Bookmark", LIBMTP_PROPERTY_TimeBookmark, PTP_OPC_TimeBookmark);
637 register_property("Object Bookmark", LIBMTP_PROPERTY_ObjectBookmark, PTP_OPC_ObjectBookmark);
638 register_property("Byte Bookmark", LIBMTP_PROPERTY_ByteBookmark, PTP_OPC_ByteBookmark);
639 register_property("Last Build Date", LIBMTP_PROPERTY_LastBuildDate, PTP_OPC_LastBuildDate);
640 register_property("Time To Live", LIBMTP_PROPERTY_TimetoLive, PTP_OPC_TimetoLive);
641 register_property("Media GUID", LIBMTP_PROPERTY_MediaGUID, PTP_OPC_MediaGUID);
642 register_property("Total Bit Rate", LIBMTP_PROPERTY_TotalBitRate, PTP_OPC_TotalBitRate);
643 register_property("Bit Rate Type", LIBMTP_PROPERTY_BitRateType, PTP_OPC_BitRateType);
644 register_property("Sample Rate", LIBMTP_PROPERTY_SampleRate, PTP_OPC_SampleRate);
645 register_property("Number Of Channels", LIBMTP_PROPERTY_NumberOfChannels, PTP_OPC_NumberOfChannels);
646 register_property("Audio Bit Depth", LIBMTP_PROPERTY_AudioBitDepth, PTP_OPC_AudioBitDepth);
647 register_property("Scan Depth", LIBMTP_PROPERTY_ScanDepth, PTP_OPC_ScanDepth);
648 register_property("Audio WAVE Codec", LIBMTP_PROPERTY_AudioWAVECodec, PTP_OPC_AudioWAVECodec);
649 register_property("Audio Bit Rate", LIBMTP_PROPERTY_AudioBitRate, PTP_OPC_AudioBitRate);
650 register_property("Video Four CC Codec", LIBMTP_PROPERTY_VideoFourCCCodec, PTP_OPC_VideoFourCCCodec);
651 register_property("Video Bit Rate", LIBMTP_PROPERTY_VideoBitRate, PTP_OPC_VideoBitRate);
652 register_property("Frames Per Thousand Seconds", LIBMTP_PROPERTY_FramesPerThousandSeconds, PTP_OPC_FramesPerThousandSeconds);
653 register_property("Key Frame Distance", LIBMTP_PROPERTY_KeyFrameDistance, PTP_OPC_KeyFrameDistance);
654 register_property("Buffer Size", LIBMTP_PROPERTY_BufferSize, PTP_OPC_BufferSize);
655 register_property("Encoding Quality", LIBMTP_PROPERTY_EncodingQuality, PTP_OPC_EncodingQuality);
656 register_property("Encoding Profile", LIBMTP_PROPERTY_EncodingProfile, PTP_OPC_EncodingProfile);
657 register_property("Buy flag", LIBMTP_PROPERTY_BuyFlag, PTP_OPC_BuyFlag);
658 register_property("Unknown property", LIBMTP_PROPERTY_UNKNOWN, 0);
659}
660
661/**
662 * Returns the PTP property that maps to a certain libmtp internal property type.
663 * @param inproperty the MTP library interface property
664 * @return the PTP (libgphoto2) property type
665 */
666static uint16_t map_libmtp_property_to_ptp_property(LIBMTP_property_t inproperty)
667{
668 propertymap_t *current;
669
670 current = propertymap;
671
672 while (current != NULL) {
673 if(current->id == inproperty) {
674 return current->ptp_id;
675 }
676 current = current->next;
677 }
678 return 0;
679}
680
681
682/**
683 * Returns the MTP internal interface property that maps to a certain ptp
684 * interface property.
685 * @param inproperty the PTP (libgphoto2) interface property
686 * @return the MTP library interface property
687 */
688static LIBMTP_property_t map_ptp_property_to_libmtp_property(uint16_t inproperty)
689{
690 propertymap_t *current;
691
692 current = propertymap;
693
694 while (current != NULL) {
695 if(current->ptp_id == inproperty) {
696 return current->id;
697 }
698 current = current->next;
699 }
700 // printf("map_ptp_type_to_libmtp_type: unknown filetype.\n");
701 return LIBMTP_PROPERTY_UNKNOWN;
702}
703
Linus Walleijfa1374c2006-02-27 07:41:46 +0000704
Linus Walleij6946ac52006-03-21 06:51:22 +0000705/**
Linus Walleijf0f3d482006-05-29 14:10:21 +0000706 * Initialize the library. You are only supposed to call this
707 * one, before using the library for the first time in a program.
708 * Never re-initialize libmtp!
709 *
raveloxd9a28642006-05-26 23:42:22 +0000710 * The only thing this does at the moment is to initialise the
711 * filetype mapping table.
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000712 */
713void LIBMTP_Init(void)
714{
raveloxd9a28642006-05-26 23:42:22 +0000715 init_filemap();
Richard Low6f070842009-05-03 10:26:16 +0000716 init_propertymap();
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000717 return;
718}
719
Linus Walleijcf42f452006-11-28 08:32:49 +0000720
721/**
722 * This helper function returns a textual description for a libmtp
723 * file type to be used in dialog boxes etc.
724 * @param intype the libmtp internal filetype to get a description for.
725 * @return a string representing the filetype, this must <b>NOT</b>
726 * be free():ed by the caller!
727 */
728char const * LIBMTP_Get_Filetype_Description(LIBMTP_filetype_t intype)
729{
730 filemap_t *current;
731
732 current = filemap;
733
734 while (current != NULL) {
735 if(current->id == intype) {
736 return current->description;
737 }
738 current = current->next;
739 }
740
741 return "Unknown filetype";
742}
743
Linus Walleij7783b9b2007-09-22 22:10:44 +0000744/**
Richard Low6f070842009-05-03 10:26:16 +0000745 * This helper function returns a textual description for a libmtp
746 * property to be used in dialog boxes etc.
747 * @param inproperty the libmtp internal property to get a description for.
748 * @return a string representing the filetype, this must <b>NOT</b>
749 * be free():ed by the caller!
750 */
751char const * LIBMTP_Get_Property_Description(LIBMTP_property_t inproperty)
752{
753 propertymap_t *current;
754
755 current = propertymap;
756
757 while (current != NULL) {
758 if(current->id == inproperty) {
759 return current->description;
760 }
761 current = current->next;
762 }
763
764 return "Unknown property";
765}
766
767/**
Linus Walleij7783b9b2007-09-22 22:10:44 +0000768 * This function will do its best to fit a 16bit
769 * value into a PTP object property if the property
770 * is limited in range or step sizes.
771 */
Linus Walleij29559562007-08-22 21:38:04 +0000772static uint16_t adjust_u16(uint16_t val, PTPObjectPropDesc *opd)
773{
774 switch (opd->FormFlag) {
775 case PTP_DPFF_Range:
776 if (val < opd->FORM.Range.MinimumValue.u16) {
777 return opd->FORM.Range.MinimumValue.u16;
778 }
779 if (val > opd->FORM.Range.MaximumValue.u16) {
780 return opd->FORM.Range.MaximumValue.u16;
781 }
782 // Round down to last step.
783 if (val % opd->FORM.Range.StepSize.u16 != 0) {
784 return val - (val % opd->FORM.Range.StepSize.u16);
785 }
786 return val;
787 break;
788 case PTP_DPFF_Enumeration:
789 {
790 int i;
791 uint16_t bestfit = opd->FORM.Enum.SupportedValue[0].u16;
792
793 for (i=0; i<opd->FORM.Enum.NumberOfValues; i++) {
794 if (val == opd->FORM.Enum.SupportedValue[i].u16) {
795 return val;
796 }
797 // Rough guess of best fit
798 if (opd->FORM.Enum.SupportedValue[i].u16 < val) {
799 bestfit = opd->FORM.Enum.SupportedValue[i].u16;
800 }
801 }
802 // Just some default that'll work.
803 return bestfit;
804 }
805 default:
806 // Will accept any value
807 break;
808 }
809 return val;
810}
811
Linus Walleij7783b9b2007-09-22 22:10:44 +0000812/**
813 * This function will do its best to fit a 32bit
814 * value into a PTP object property if the property
815 * is limited in range or step sizes.
816 */
Linus Walleij29559562007-08-22 21:38:04 +0000817static uint32_t adjust_u32(uint32_t val, PTPObjectPropDesc *opd)
818{
819 switch (opd->FormFlag) {
820 case PTP_DPFF_Range:
821 if (val < opd->FORM.Range.MinimumValue.u32) {
822 return opd->FORM.Range.MinimumValue.u32;
823 }
824 if (val > opd->FORM.Range.MaximumValue.u32) {
825 return opd->FORM.Range.MaximumValue.u32;
826 }
827 // Round down to last step.
828 if (val % opd->FORM.Range.StepSize.u32 != 0) {
829 return val - (val % opd->FORM.Range.StepSize.u32);
830 }
831 return val;
832 break;
833 case PTP_DPFF_Enumeration:
834 {
835 int i;
836 uint32_t bestfit = opd->FORM.Enum.SupportedValue[0].u32;
837
838 for (i=0; i<opd->FORM.Enum.NumberOfValues; i++) {
839 if (val == opd->FORM.Enum.SupportedValue[i].u32) {
840 return val;
841 }
842 // Rough guess of best fit
843 if (opd->FORM.Enum.SupportedValue[i].u32 < val) {
844 bestfit = opd->FORM.Enum.SupportedValue[i].u32;
845 }
846 }
847 // Just some default that'll work.
848 return bestfit;
849 }
850 default:
851 // Will accept any value
852 break;
853 }
854 return val;
855}
856
Linus Walleij7783b9b2007-09-22 22:10:44 +0000857/**
Linus Walleij7783b9b2007-09-22 22:10:44 +0000858 * This function returns a newly created ISO 8601 timestamp with the
859 * current time in as high precision as possible. It even adds
860 * the time zone if it can.
861 */
862static char *get_iso8601_stamp(void)
863{
864 time_t curtime;
865 struct tm *loctime;
866 char tmp[64];
867
868 curtime = time(NULL);
869 loctime = localtime(&curtime);
870 strftime (tmp, sizeof(tmp), "%Y%m%dT%H%M%S.0%z", loctime);
871 return strdup(tmp);
872}
873
874/**
Richard Low6f070842009-05-03 10:26:16 +0000875 * Gets the allowed values (range or enum) for a property
876 * @param device a pointer to an MTP device
877 * @param property the property to query
878 * @param filetype the filetype of the object you want to set values for
879 * @param allowed_vals pointer to a LIBMTP_allowed_values_t struct to
880 * receive the allowed values. Call LIBMTP_destroy_allowed_values_t
881 * on this on successful completion.
882 * @return 0 on success, any other value means failure
883 */
884int LIBMTP_Get_Allowed_Property_Values(LIBMTP_mtpdevice_t *device, LIBMTP_property_t const property,
885 LIBMTP_filetype_t const filetype, LIBMTP_allowed_values_t *allowed_vals)
886{
887 PTPObjectPropDesc opd;
888 uint16_t ret = 0;
889
890 ret = ptp_mtp_getobjectpropdesc(device->params, map_libmtp_property_to_ptp_property(property), map_libmtp_type_to_ptp_type(filetype), &opd);
891 if (ret != PTP_RC_OK) {
892 add_ptp_error_to_errorstack(device, ret, "LIBMTP_Get_Allowed_Property_Values(): could not get property description.");
893 return -1;
894 }
895
896 if (opd.FormFlag == PTP_OPFF_Enumeration) {
897 int i = 0;
898
899 allowed_vals->is_range = 0;
900 allowed_vals->num_entries = opd.FORM.Enum.NumberOfValues;
901
902 switch (opd.DataType)
903 {
904 case PTP_DTC_INT8:
905 allowed_vals->i8vals = malloc(sizeof(int8_t) * opd.FORM.Enum.NumberOfValues);
906 allowed_vals->datatype = LIBMTP_DATATYPE_INT8;
907 break;
908 case PTP_DTC_UINT8:
909 allowed_vals->u8vals = malloc(sizeof(uint8_t) * opd.FORM.Enum.NumberOfValues);
910 allowed_vals->datatype = LIBMTP_DATATYPE_UINT8;
911 break;
912 case PTP_DTC_INT16:
913 allowed_vals->i16vals = malloc(sizeof(int16_t) * opd.FORM.Enum.NumberOfValues);
914 allowed_vals->datatype = LIBMTP_DATATYPE_INT16;
915 break;
916 case PTP_DTC_UINT16:
917 allowed_vals->u16vals = malloc(sizeof(uint16_t) * opd.FORM.Enum.NumberOfValues);
918 allowed_vals->datatype = LIBMTP_DATATYPE_UINT16;
919 break;
920 case PTP_DTC_INT32:
921 allowed_vals->i32vals = malloc(sizeof(int32_t) * opd.FORM.Enum.NumberOfValues);
922 allowed_vals->datatype = LIBMTP_DATATYPE_INT32;
923 break;
924 case PTP_DTC_UINT32:
925 allowed_vals->u32vals = malloc(sizeof(uint32_t) * opd.FORM.Enum.NumberOfValues);
926 allowed_vals->datatype = LIBMTP_DATATYPE_UINT32;
927 break;
928 case PTP_DTC_INT64:
929 allowed_vals->i64vals = malloc(sizeof(int64_t) * opd.FORM.Enum.NumberOfValues);
930 allowed_vals->datatype = LIBMTP_DATATYPE_INT64;
931 break;
932 case PTP_DTC_UINT64:
933 allowed_vals->u64vals = malloc(sizeof(uint64_t) * opd.FORM.Enum.NumberOfValues);
934 allowed_vals->datatype = LIBMTP_DATATYPE_UINT64;
935 break;
936 }
937
938 for (i = 0; i < opd.FORM.Enum.NumberOfValues; i++) {
939 switch (opd.DataType)
940 {
941 case PTP_DTC_INT8:
942 allowed_vals->i8vals[i] = opd.FORM.Enum.SupportedValue[i].i8;
943 break;
944 case PTP_DTC_UINT8:
945 allowed_vals->u8vals[i] = opd.FORM.Enum.SupportedValue[i].u8;
946 break;
947 case PTP_DTC_INT16:
948 allowed_vals->i16vals[i] = opd.FORM.Enum.SupportedValue[i].i16;
949 break;
950 case PTP_DTC_UINT16:
951 allowed_vals->u16vals[i] = opd.FORM.Enum.SupportedValue[i].u16;
952 break;
953 case PTP_DTC_INT32:
954 allowed_vals->i32vals[i] = opd.FORM.Enum.SupportedValue[i].i32;
955 break;
956 case PTP_DTC_UINT32:
957 allowed_vals->u32vals[i] = opd.FORM.Enum.SupportedValue[i].u32;
958 break;
959 case PTP_DTC_INT64:
960 allowed_vals->i64vals[i] = opd.FORM.Enum.SupportedValue[i].i64;
961 break;
962 case PTP_DTC_UINT64:
963 allowed_vals->u64vals[i] = opd.FORM.Enum.SupportedValue[i].u64;
964 break;
965 }
966 }
967 ptp_free_objectpropdesc(&opd);
968 return 0;
969 } else if (opd.FormFlag == PTP_OPFF_Range) {
970 allowed_vals->is_range = 1;
971
972 switch (opd.DataType)
973 {
974 case PTP_DTC_INT8:
975 allowed_vals->i8min = opd.FORM.Range.MinimumValue.i8;
976 allowed_vals->i8max = opd.FORM.Range.MaximumValue.i8;
977 allowed_vals->i8step = opd.FORM.Range.StepSize.i8;
978 allowed_vals->datatype = LIBMTP_DATATYPE_INT8;
979 break;
980 case PTP_DTC_UINT8:
981 allowed_vals->u8min = opd.FORM.Range.MinimumValue.u8;
982 allowed_vals->u8max = opd.FORM.Range.MaximumValue.u8;
983 allowed_vals->u8step = opd.FORM.Range.StepSize.u8;
984 allowed_vals->datatype = LIBMTP_DATATYPE_UINT8;
985 break;
986 case PTP_DTC_INT16:
987 allowed_vals->i16min = opd.FORM.Range.MinimumValue.i16;
988 allowed_vals->i16max = opd.FORM.Range.MaximumValue.i16;
989 allowed_vals->i16step = opd.FORM.Range.StepSize.i16;
990 allowed_vals->datatype = LIBMTP_DATATYPE_INT16;
991 break;
992 case PTP_DTC_UINT16:
993 allowed_vals->u16min = opd.FORM.Range.MinimumValue.u16;
994 allowed_vals->u16max = opd.FORM.Range.MaximumValue.u16;
995 allowed_vals->u16step = opd.FORM.Range.StepSize.u16;
996 allowed_vals->datatype = LIBMTP_DATATYPE_UINT16;
997 break;
998 case PTP_DTC_INT32:
999 allowed_vals->i32min = opd.FORM.Range.MinimumValue.i32;
1000 allowed_vals->i32max = opd.FORM.Range.MaximumValue.i32;
1001 allowed_vals->i32step = opd.FORM.Range.StepSize.i32;
1002 allowed_vals->datatype = LIBMTP_DATATYPE_INT32;
1003 break;
1004 case PTP_DTC_UINT32:
1005 allowed_vals->u32min = opd.FORM.Range.MinimumValue.u32;
1006 allowed_vals->u32max = opd.FORM.Range.MaximumValue.u32;
1007 allowed_vals->u32step = opd.FORM.Range.StepSize.u32;
1008 allowed_vals->datatype = LIBMTP_DATATYPE_UINT32;
1009 break;
1010 case PTP_DTC_INT64:
1011 allowed_vals->i64min = opd.FORM.Range.MinimumValue.i64;
1012 allowed_vals->i64max = opd.FORM.Range.MaximumValue.i64;
1013 allowed_vals->i64step = opd.FORM.Range.StepSize.i64;
1014 allowed_vals->datatype = LIBMTP_DATATYPE_INT64;
1015 break;
1016 case PTP_DTC_UINT64:
1017 allowed_vals->u64min = opd.FORM.Range.MinimumValue.u64;
1018 allowed_vals->u64max = opd.FORM.Range.MaximumValue.u64;
1019 allowed_vals->u64step = opd.FORM.Range.StepSize.u64;
1020 allowed_vals->datatype = LIBMTP_DATATYPE_UINT64;
1021 break;
1022 }
1023 return 0;
1024 } else
1025 return -1;
1026}
1027
1028/**
1029 * Destroys a LIBMTP_allowed_values_t struct
1030 * @param allowed_vals the struct to destroy
1031 */
1032void LIBMTP_destroy_allowed_values_t(LIBMTP_allowed_values_t *allowed_vals)
1033{
1034 if (!allowed_vals->is_range)
1035 {
1036 switch (allowed_vals->datatype)
1037 {
1038 case LIBMTP_DATATYPE_INT8:
1039 if (allowed_vals->i8vals)
1040 free(allowed_vals->i8vals);
1041 break;
1042 case LIBMTP_DATATYPE_UINT8:
1043 if (allowed_vals->u8vals)
1044 free(allowed_vals->u8vals);
1045 break;
1046 case LIBMTP_DATATYPE_INT16:
1047 if (allowed_vals->i16vals)
1048 free(allowed_vals->i16vals);
1049 break;
1050 case LIBMTP_DATATYPE_UINT16:
1051 if (allowed_vals->u16vals)
1052 free(allowed_vals->u16vals);
1053 break;
1054 case LIBMTP_DATATYPE_INT32:
1055 if (allowed_vals->i32vals)
1056 free(allowed_vals->i32vals);
1057 break;
1058 case LIBMTP_DATATYPE_UINT32:
1059 if (allowed_vals->u32vals)
1060 free(allowed_vals->u32vals);
1061 break;
1062 case LIBMTP_DATATYPE_INT64:
1063 if (allowed_vals->i64vals)
1064 free(allowed_vals->i64vals);
1065 break;
1066 case LIBMTP_DATATYPE_UINT64:
1067 if (allowed_vals->u64vals)
1068 free(allowed_vals->u64vals);
1069 break;
1070 }
1071 }
1072}
1073
1074/**
1075 * Determine if a property is supported for a given file type
1076 * @param device a pointer to an MTP device
1077 * @param property the property to query
1078 * @param filetype the filetype of the object you want to set values for
1079 * @return 0 if not supported, positive if supported, negative on error
1080 */
1081int LIBMTP_Is_Property_Supported(LIBMTP_mtpdevice_t *device, LIBMTP_property_t const property,
1082 LIBMTP_filetype_t const filetype)
1083{
1084 uint16_t *props = NULL;
1085 uint32_t propcnt = 0;
1086 uint16_t ret = 0;
1087 int i = 0;
1088 int supported = 0;
1089 uint16_t ptp_prop = map_libmtp_property_to_ptp_property(property);
1090
1091 ret = ptp_mtp_getobjectpropssupported(device->params, map_libmtp_type_to_ptp_type(filetype), &propcnt, &props);
1092 if (ret != PTP_RC_OK) {
1093 add_ptp_error_to_errorstack(device, ret, "LIBMTP_Is_Property_Supported(): could not get properties supported.");
1094 return -1;
1095 }
1096
1097 for (i = 0; i < propcnt; i++) {
1098 if (props[i] == ptp_prop) {
1099 supported = 1;
1100 break;
1101 }
1102 }
1103
1104 free(props);
1105
1106 return supported;
1107}
1108
1109/**
1110 * Retrieves a string from an object
1111 *
1112 * @param device a pointer to an MTP device.
1113 * @param object_id Object reference
1114 * @param attribute_id MTP attribute ID
1115 * @return valid string or NULL on failure. The returned string
1116 * must bee <code>free()</code>:ed by the caller after
1117 * use.
1118 */
1119char *LIBMTP_Get_String_From_Object(LIBMTP_mtpdevice_t *device, uint32_t const object_id,
1120 LIBMTP_property_t const attribute_id)
1121{
1122 return get_string_from_object(device, object_id, attribute_id);
1123}
1124
1125/**
1126* Retrieves an unsigned 64-bit integer from an object attribute
1127 *
1128 * @param device a pointer to an MTP device.
1129 * @param object_id Object reference
1130 * @param attribute_id MTP attribute ID
1131 * @param value_default Default value to return on failure
1132 * @return the value
1133 */
1134uint64_t LIBMTP_Get_u64_From_Object(LIBMTP_mtpdevice_t *device,uint32_t const object_id,
1135 LIBMTP_property_t const attribute_id, uint64_t const value_default)
1136{
1137 return get_u64_from_object(device, object_id, map_libmtp_property_to_ptp_property(attribute_id), value_default);
1138}
1139
1140/**
1141 * Retrieves an unsigned 32-bit integer from an object attribute
1142 *
1143 * @param device a pointer to an MTP device.
1144 * @param object_id Object reference
1145 * @param attribute_id MTP attribute ID
1146 * @param value_default Default value to return on failure
1147 * @return the value
1148 */
1149uint32_t LIBMTP_Get_u32_From_Object(LIBMTP_mtpdevice_t *device,uint32_t const object_id,
1150 LIBMTP_property_t const attribute_id, uint32_t const value_default)
1151{
1152 return get_u32_from_object(device, object_id, map_libmtp_property_to_ptp_property(attribute_id), value_default);
1153}
1154
1155/**
1156 * Retrieves an unsigned 16-bit integer from an object attribute
1157 *
1158 * @param device a pointer to an MTP device.
1159 * @param object_id Object reference
1160 * @param attribute_id MTP attribute ID
1161 * @param value_default Default value to return on failure
1162 * @return a value
1163 */
1164uint16_t LIBMTP_Get_u16_From_Object(LIBMTP_mtpdevice_t *device, uint32_t const object_id,
1165 LIBMTP_property_t const attribute_id, uint16_t const value_default)
1166{
1167 return get_u16_from_object(device, object_id, map_libmtp_property_to_ptp_property(attribute_id), value_default);
1168}
1169
1170/**
1171 * Retrieves an unsigned 8-bit integer from an object attribute
1172 *
1173 * @param device a pointer to an MTP device.
1174 * @param object_id Object reference
1175 * @param attribute_id MTP attribute ID
1176 * @param value_default Default value to return on failure
1177 * @return a value
1178 */
1179uint8_t LIBMTP_Get_u8_From_Object(LIBMTP_mtpdevice_t *device, uint32_t const object_id,
1180 LIBMTP_property_t const attribute_id, uint8_t const value_default)
1181{
1182 return get_u8_from_object(device, object_id, map_libmtp_property_to_ptp_property(attribute_id), value_default);
1183}
1184
1185/**
1186 * Sets an object attribute from a string
1187 *
1188 * @param device a pointer to an MTP device.
1189 * @param object_id Object reference
1190 * @param attribute_id MTP attribute ID
1191 * @param string string value to set
1192 * @return 0 on success, any other value means failure
1193 */
1194int LIBMTP_Set_Object_String(LIBMTP_mtpdevice_t *device, uint32_t const object_id,
1195 LIBMTP_property_t const attribute_id, char const * const string)
1196{
1197 return set_object_string(device, object_id, map_libmtp_property_to_ptp_property(attribute_id), string);
1198}
1199
1200
1201/**
1202 * Sets an object attribute from an unsigned 32-bit integer
1203 *
1204 * @param device a pointer to an MTP device.
1205 * @param object_id Object reference
1206 * @param attribute_id MTP attribute ID
1207 * @param value 32-bit unsigned integer to set
1208 * @return 0 on success, any other value means failure
1209 */
1210int LIBMTP_Set_Object_u32(LIBMTP_mtpdevice_t *device, uint32_t const object_id,
1211 LIBMTP_property_t const attribute_id, uint32_t const value)
1212{
1213 return set_object_u32(device, object_id, map_libmtp_property_to_ptp_property(attribute_id), value);
1214}
1215
1216/**
1217 * Sets an object attribute from an unsigned 16-bit integer
1218 *
1219 * @param device a pointer to an MTP device.
1220 * @param object_id Object reference
1221 * @param attribute_id MTP attribute ID
1222 * @param value 16-bit unsigned integer to set
1223 * @return 0 on success, any other value means failure
1224 */
1225int LIBMTP_Set_Object_u16(LIBMTP_mtpdevice_t *device, uint32_t const object_id,
1226 LIBMTP_property_t const attribute_id, uint16_t const value)
1227{
1228 return set_object_u16(device, object_id, map_libmtp_property_to_ptp_property(attribute_id), value);
1229}
1230
1231/**
1232 * Sets an object attribute from an unsigned 8-bit integer
1233 *
1234 * @param device a pointer to an MTP device.
1235 * @param object_id Object reference
1236 * @param attribute_id MTP attribute ID
1237 * @param value 8-bit unsigned integer to set
1238 * @return 0 on success, any other value means failure
1239 */
1240int LIBMTP_Set_Object_u8(LIBMTP_mtpdevice_t *device, uint32_t const object_id,
1241 LIBMTP_property_t const attribute_id, uint8_t const value)
1242{
1243 return set_object_u8(device, object_id, map_libmtp_property_to_ptp_property(attribute_id), value);
1244}
1245
1246/**
raveloxd9a28642006-05-26 23:42:22 +00001247 * Retrieves a string from an object
1248 *
1249 * @param device a pointer to an MTP device.
1250 * @param object_id Object reference
1251 * @param attribute_id PTP attribute ID
Linus Walleij438bd7f2006-06-08 11:35:44 +00001252 * @return valid string or NULL on failure. The returned string
1253 * must bee <code>free()</code>:ed by the caller after
1254 * use.
raveloxd9a28642006-05-26 23:42:22 +00001255 */
Linus Walleij9901e222006-11-30 12:28:19 +00001256static char *get_string_from_object(LIBMTP_mtpdevice_t *device, uint32_t const object_id,
Linus Walleij4ef39e62006-09-19 14:11:52 +00001257 uint16_t const attribute_id)
raveloxd9a28642006-05-26 23:42:22 +00001258{
1259 PTPPropertyValue propval;
1260 char *retstring = NULL;
Linus Walleijf0f3d482006-05-29 14:10:21 +00001261 PTPParams *params = (PTPParams *) device->params;
Linus Walleij438bd7f2006-06-08 11:35:44 +00001262 uint16_t ret;
Linus Walleijd4637502009-06-14 23:03:33 +00001263 MTPProperties *prop;
mopoke96143402006-10-30 04:37:26 +00001264
Linus Walleij438bd7f2006-06-08 11:35:44 +00001265 if ( device == NULL || object_id == 0) {
Linus Walleijf0f3d482006-05-29 14:10:21 +00001266 return NULL;
1267 }
Linus Walleij338ade42007-07-03 20:44:08 +00001268
Linus Walleijd4637502009-06-14 23:03:33 +00001269 prop = ptp_find_object_prop_in_cache(params, object_id, attribute_id);
1270 if (prop) {
1271 if (prop->propval.str != NULL)
1272 return strdup(prop->propval.str);
1273 else
1274 return NULL;
Linus Walleij338ade42007-07-03 20:44:08 +00001275 }
mopoke96143402006-10-30 04:37:26 +00001276
Linus Walleija823a702006-08-27 21:27:46 +00001277 ret = ptp_mtp_getobjectpropvalue(params, object_id, attribute_id, &propval, PTP_DTC_STR);
raveloxd9a28642006-05-26 23:42:22 +00001278 if (ret == PTP_RC_OK) {
Linus Walleija823a702006-08-27 21:27:46 +00001279 if (propval.str != NULL) {
1280 retstring = (char *) strdup(propval.str);
1281 free(propval.str);
raveloxd9a28642006-05-26 23:42:22 +00001282 }
Linus Walleij070e9b42007-01-22 08:49:28 +00001283 } else {
1284 add_ptp_error_to_errorstack(device, ret, "get_string_from_object(): could not get object string.");
raveloxd9a28642006-05-26 23:42:22 +00001285 }
mopoke96143402006-10-30 04:37:26 +00001286
raveloxd9a28642006-05-26 23:42:22 +00001287 return retstring;
1288}
1289
1290/**
Richard Lowc3f5fc32007-07-29 09:40:50 +00001291* Retrieves an unsigned 64-bit integer from an object attribute
1292 *
1293 * @param device a pointer to an MTP device.
1294 * @param object_id Object reference
1295 * @param attribute_id PTP attribute ID
1296 * @param value_default Default value to return on failure
1297 * @return the value
1298 */
1299static uint64_t get_u64_from_object(LIBMTP_mtpdevice_t *device,uint32_t const object_id,
1300 uint16_t const attribute_id, uint64_t const value_default)
1301{
1302 PTPPropertyValue propval;
1303 uint64_t retval = value_default;
1304 PTPParams *params = (PTPParams *) device->params;
1305 uint16_t ret;
Linus Walleijd4637502009-06-14 23:03:33 +00001306 MTPProperties *prop;
Richard Lowc3f5fc32007-07-29 09:40:50 +00001307
1308 if ( device == NULL ) {
1309 return value_default;
1310 }
1311
Linus Walleijd4637502009-06-14 23:03:33 +00001312 prop = ptp_find_object_prop_in_cache(params, object_id, attribute_id);
1313 if (prop)
1314 return prop->propval.u64;
Linus Walleij1e9a0332007-09-12 19:35:56 +00001315
Richard Lowc3f5fc32007-07-29 09:40:50 +00001316 ret = ptp_mtp_getobjectpropvalue(params, object_id,
1317 attribute_id,
1318 &propval,
1319 PTP_DTC_UINT64);
1320 if (ret == PTP_RC_OK) {
1321 retval = propval.u64;
1322 } else {
1323 add_ptp_error_to_errorstack(device, ret, "get_u64_from_object(): could not get unsigned 64bit integer from object.");
1324 }
1325
1326 return retval;
1327}
1328
1329/**
raveloxd9a28642006-05-26 23:42:22 +00001330 * Retrieves an unsigned 32-bit integer from an object attribute
1331 *
1332 * @param device a pointer to an MTP device.
1333 * @param object_id Object reference
1334 * @param attribute_id PTP attribute ID
Linus Walleij5acfa742006-05-29 14:51:59 +00001335 * @param value_default Default value to return on failure
Linus Walleijf0f3d482006-05-29 14:10:21 +00001336 * @return the value
raveloxd9a28642006-05-26 23:42:22 +00001337 */
Linus Walleij9901e222006-11-30 12:28:19 +00001338static uint32_t get_u32_from_object(LIBMTP_mtpdevice_t *device,uint32_t const object_id,
Linus Walleij4ef39e62006-09-19 14:11:52 +00001339 uint16_t const attribute_id, uint32_t const value_default)
raveloxd9a28642006-05-26 23:42:22 +00001340{
1341 PTPPropertyValue propval;
1342 uint32_t retval = value_default;
Linus Walleijf0f3d482006-05-29 14:10:21 +00001343 PTPParams *params = (PTPParams *) device->params;
Linus Walleij438bd7f2006-06-08 11:35:44 +00001344 uint16_t ret;
Linus Walleijd4637502009-06-14 23:03:33 +00001345 MTPProperties *prop;
mopoke96143402006-10-30 04:37:26 +00001346
Linus Walleijf0f3d482006-05-29 14:10:21 +00001347 if ( device == NULL ) {
1348 return value_default;
1349 }
raveloxd9a28642006-05-26 23:42:22 +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 return prop->propval.u32;
Linus Walleij338ade42007-07-03 20:44:08 +00001354
raveloxd9a28642006-05-26 23:42:22 +00001355 ret = ptp_mtp_getobjectpropvalue(params, object_id,
1356 attribute_id,
1357 &propval,
1358 PTP_DTC_UINT32);
1359 if (ret == PTP_RC_OK) {
1360 retval = propval.u32;
Linus Walleij070e9b42007-01-22 08:49:28 +00001361 } else {
1362 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 +00001363 }
raveloxd9a28642006-05-26 23:42:22 +00001364 return retval;
1365}
1366
1367/**
1368 * Retrieves an unsigned 16-bit integer from an object attribute
1369 *
1370 * @param device a pointer to an MTP device.
1371 * @param object_id Object reference
1372 * @param attribute_id PTP attribute ID
Linus Walleij5acfa742006-05-29 14:51:59 +00001373 * @param value_default Default value to return on failure
Linus Walleijf0f3d482006-05-29 14:10:21 +00001374 * @return a value
raveloxd9a28642006-05-26 23:42:22 +00001375 */
Linus Walleij9901e222006-11-30 12:28:19 +00001376static uint16_t get_u16_from_object(LIBMTP_mtpdevice_t *device, uint32_t const object_id,
Linus Walleij4ef39e62006-09-19 14:11:52 +00001377 uint16_t const attribute_id, uint16_t const value_default)
raveloxd9a28642006-05-26 23:42:22 +00001378{
1379 PTPPropertyValue propval;
1380 uint16_t retval = value_default;
Linus Walleijf0f3d482006-05-29 14:10:21 +00001381 PTPParams *params = (PTPParams *) device->params;
Linus Walleij438bd7f2006-06-08 11:35:44 +00001382 uint16_t ret;
Linus Walleijd4637502009-06-14 23:03:33 +00001383 MTPProperties *prop;
raveloxd9a28642006-05-26 23:42:22 +00001384
Linus Walleijf0f3d482006-05-29 14:10:21 +00001385 if ( device == NULL ) {
1386 return value_default;
1387 }
raveloxd9a28642006-05-26 23:42:22 +00001388
Linus Walleij338ade42007-07-03 20:44:08 +00001389 // This O(n) search should not be used so often, since code
1390 // using the cached properties don't usually call this function.
Linus Walleijd4637502009-06-14 23:03:33 +00001391 prop = ptp_find_object_prop_in_cache(params, object_id, attribute_id);
1392 if (prop)
1393 return prop->propval.u16;
Linus Walleij338ade42007-07-03 20:44:08 +00001394
raveloxd9a28642006-05-26 23:42:22 +00001395 ret = ptp_mtp_getobjectpropvalue(params, object_id,
1396 attribute_id,
1397 &propval,
1398 PTP_DTC_UINT16);
1399 if (ret == PTP_RC_OK) {
1400 retval = propval.u16;
Linus Walleij070e9b42007-01-22 08:49:28 +00001401 } else {
1402 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 +00001403 }
mopoke96143402006-10-30 04:37:26 +00001404
raveloxd9a28642006-05-26 23:42:22 +00001405 return retval;
1406}
1407
1408/**
Linus Walleij99310d42006-11-01 08:29:39 +00001409 * Retrieves an unsigned 8-bit integer from an object attribute
1410 *
1411 * @param device a pointer to an MTP device.
1412 * @param object_id Object reference
1413 * @param attribute_id PTP attribute ID
1414 * @param value_default Default value to return on failure
1415 * @return a value
1416 */
Linus Walleij9901e222006-11-30 12:28:19 +00001417static uint8_t get_u8_from_object(LIBMTP_mtpdevice_t *device, uint32_t const object_id,
Linus Walleij99310d42006-11-01 08:29:39 +00001418 uint16_t const attribute_id, uint8_t const value_default)
1419{
1420 PTPPropertyValue propval;
1421 uint8_t retval = value_default;
1422 PTPParams *params = (PTPParams *) device->params;
1423 uint16_t ret;
Linus Walleijd4637502009-06-14 23:03:33 +00001424 MTPProperties *prop;
Linus Walleij99310d42006-11-01 08:29:39 +00001425
1426 if ( device == NULL ) {
1427 return value_default;
1428 }
1429
Linus Walleij338ade42007-07-03 20:44:08 +00001430 // This O(n) search should not be used so often, since code
1431 // using the cached properties don't usually call this function.
Linus Walleijd4637502009-06-14 23:03:33 +00001432 prop = ptp_find_object_prop_in_cache(params, object_id, attribute_id);
1433 if (prop)
1434 return prop->propval.u8;
Linus Walleij338ade42007-07-03 20:44:08 +00001435
Linus Walleij99310d42006-11-01 08:29:39 +00001436 ret = ptp_mtp_getobjectpropvalue(params, object_id,
1437 attribute_id,
1438 &propval,
1439 PTP_DTC_UINT8);
1440 if (ret == PTP_RC_OK) {
1441 retval = propval.u8;
Linus Walleij070e9b42007-01-22 08:49:28 +00001442 } else {
1443 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 +00001444 }
1445
1446 return retval;
1447}
1448
1449/**
raveloxd9a28642006-05-26 23:42:22 +00001450 * Sets an object attribute from a string
1451 *
1452 * @param device a pointer to an MTP device.
1453 * @param object_id Object reference
1454 * @param attribute_id PTP attribute ID
1455 * @param string string value to set
Linus Walleijf0f3d482006-05-29 14:10:21 +00001456 * @return 0 on success, any other value means failure
raveloxd9a28642006-05-26 23:42:22 +00001457 */
Linus Walleij9901e222006-11-30 12:28:19 +00001458static int set_object_string(LIBMTP_mtpdevice_t *device, uint32_t const object_id,
Linus Walleij4ef39e62006-09-19 14:11:52 +00001459 uint16_t const attribute_id, char const * const string)
raveloxd9a28642006-05-26 23:42:22 +00001460{
1461 PTPPropertyValue propval;
Linus Walleijf0f3d482006-05-29 14:10:21 +00001462 PTPParams *params = (PTPParams *) device->params;
Linus Walleij438bd7f2006-06-08 11:35:44 +00001463 uint16_t ret;
raveloxd9a28642006-05-26 23:42:22 +00001464
Linus Walleijf0f3d482006-05-29 14:10:21 +00001465 if (device == NULL || string == NULL) {
Linus Walleij438bd7f2006-06-08 11:35:44 +00001466 return -1;
Linus Walleijf0f3d482006-05-29 14:10:21 +00001467 }
Linus Walleijb2753182007-02-26 08:11:38 +00001468
1469 if (!ptp_operation_issupported(params,PTP_OC_MTP_SetObjectPropValue)) {
1470 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "set_object_string(): could not set object string: "
1471 "PTP_OC_MTP_SetObjectPropValue not supported.");
1472 return -1;
1473 }
Linus Walleija823a702006-08-27 21:27:46 +00001474 propval.str = (char *) string;
1475 ret = ptp_mtp_setobjectpropvalue(params, object_id, attribute_id, &propval, PTP_DTC_STR);
Linus Walleijf0f3d482006-05-29 14:10:21 +00001476 if (ret != PTP_RC_OK) {
Linus Walleij070e9b42007-01-22 08:49:28 +00001477 add_ptp_error_to_errorstack(device, ret, "set_object_string(): could not set object string.");
Linus Walleij438bd7f2006-06-08 11:35:44 +00001478 return -1;
Linus Walleijf0f3d482006-05-29 14:10:21 +00001479 }
mopoke96143402006-10-30 04:37:26 +00001480
Linus Walleijf0f3d482006-05-29 14:10:21 +00001481 return 0;
raveloxd9a28642006-05-26 23:42:22 +00001482}
1483
Linus Walleij29559562007-08-22 21:38:04 +00001484
raveloxd9a28642006-05-26 23:42:22 +00001485/**
1486 * Sets an object attribute from an unsigned 32-bit integer
1487 *
1488 * @param device a pointer to an MTP device.
1489 * @param object_id Object reference
1490 * @param attribute_id PTP attribute ID
1491 * @param value 32-bit unsigned integer to set
Linus Walleijf0f3d482006-05-29 14:10:21 +00001492 * @return 0 on success, any other value means failure
raveloxd9a28642006-05-26 23:42:22 +00001493 */
Linus Walleij9901e222006-11-30 12:28:19 +00001494static int set_object_u32(LIBMTP_mtpdevice_t *device, uint32_t const object_id,
Linus Walleij4ef39e62006-09-19 14:11:52 +00001495 uint16_t const attribute_id, uint32_t const value)
raveloxd9a28642006-05-26 23:42:22 +00001496{
1497 PTPPropertyValue propval;
Linus Walleijf0f3d482006-05-29 14:10:21 +00001498 PTPParams *params = (PTPParams *) device->params;
Linus Walleij438bd7f2006-06-08 11:35:44 +00001499 uint16_t ret;
raveloxd9a28642006-05-26 23:42:22 +00001500
Linus Walleijf0f3d482006-05-29 14:10:21 +00001501 if (device == NULL) {
Linus Walleij438bd7f2006-06-08 11:35:44 +00001502 return -1;
Linus Walleijf0f3d482006-05-29 14:10:21 +00001503 }
raveloxd9a28642006-05-26 23:42:22 +00001504
Linus Walleijb2753182007-02-26 08:11:38 +00001505 if (!ptp_operation_issupported(params,PTP_OC_MTP_SetObjectPropValue)) {
1506 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "set_object_u32(): could not set unsigned 32bit integer property: "
1507 "PTP_OC_MTP_SetObjectPropValue not supported.");
1508 return -1;
1509 }
Linus Walleij29559562007-08-22 21:38:04 +00001510
raveloxd9a28642006-05-26 23:42:22 +00001511 propval.u32 = value;
1512 ret = ptp_mtp_setobjectpropvalue(params, object_id, attribute_id, &propval, PTP_DTC_UINT32);
Linus Walleijf0f3d482006-05-29 14:10:21 +00001513 if (ret != PTP_RC_OK) {
Linus Walleij070e9b42007-01-22 08:49:28 +00001514 add_ptp_error_to_errorstack(device, ret, "set_object_u32(): could not set unsigned 32bit integer property.");
Linus Walleij438bd7f2006-06-08 11:35:44 +00001515 return -1;
Linus Walleijf0f3d482006-05-29 14:10:21 +00001516 }
mopoke96143402006-10-30 04:37:26 +00001517
Linus Walleijf0f3d482006-05-29 14:10:21 +00001518 return 0;
raveloxd9a28642006-05-26 23:42:22 +00001519}
1520
1521/**
1522 * Sets an object attribute from an unsigned 16-bit integer
1523 *
1524 * @param device a pointer to an MTP device.
1525 * @param object_id Object reference
1526 * @param attribute_id PTP attribute ID
1527 * @param value 16-bit unsigned integer to set
Linus Walleijf0f3d482006-05-29 14:10:21 +00001528 * @return 0 on success, any other value means failure
raveloxd9a28642006-05-26 23:42:22 +00001529 */
Linus Walleij9901e222006-11-30 12:28:19 +00001530static int set_object_u16(LIBMTP_mtpdevice_t *device, uint32_t const object_id,
Linus Walleij4ef39e62006-09-19 14:11:52 +00001531 uint16_t const attribute_id, uint16_t const value)
raveloxd9a28642006-05-26 23:42:22 +00001532{
1533 PTPPropertyValue propval;
Linus Walleijf0f3d482006-05-29 14:10:21 +00001534 PTPParams *params = (PTPParams *) device->params;
Linus Walleij438bd7f2006-06-08 11:35:44 +00001535 uint16_t ret;
raveloxd9a28642006-05-26 23:42:22 +00001536
Linus Walleijf0f3d482006-05-29 14:10:21 +00001537 if (device == NULL) {
1538 return 1;
1539 }
raveloxd9a28642006-05-26 23:42:22 +00001540
Linus Walleijb2753182007-02-26 08:11:38 +00001541 if (!ptp_operation_issupported(params,PTP_OC_MTP_SetObjectPropValue)) {
1542 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "set_object_u16(): could not set unsigned 16bit integer property: "
1543 "PTP_OC_MTP_SetObjectPropValue not supported.");
1544 return -1;
1545 }
raveloxd9a28642006-05-26 23:42:22 +00001546 propval.u16 = value;
1547 ret = ptp_mtp_setobjectpropvalue(params, object_id, attribute_id, &propval, PTP_DTC_UINT16);
Linus Walleijf0f3d482006-05-29 14:10:21 +00001548 if (ret != PTP_RC_OK) {
Linus Walleij070e9b42007-01-22 08:49:28 +00001549 add_ptp_error_to_errorstack(device, ret, "set_object_u16(): could not set unsigned 16bit integer property.");
Linus Walleijf0f3d482006-05-29 14:10:21 +00001550 return 1;
1551 }
raveloxd9a28642006-05-26 23:42:22 +00001552
Linus Walleijf0f3d482006-05-29 14:10:21 +00001553 return 0;
raveloxd9a28642006-05-26 23:42:22 +00001554}
1555
1556/**
Linus Walleij99310d42006-11-01 08:29:39 +00001557 * Sets an object attribute from an unsigned 8-bit integer
1558 *
1559 * @param device a pointer to an MTP device.
1560 * @param object_id Object reference
1561 * @param attribute_id PTP attribute ID
1562 * @param value 8-bit unsigned integer to set
1563 * @return 0 on success, any other value means failure
1564 */
Linus Walleij9901e222006-11-30 12:28:19 +00001565static int set_object_u8(LIBMTP_mtpdevice_t *device, uint32_t const object_id,
1566 uint16_t const attribute_id, uint8_t const value)
Linus Walleij99310d42006-11-01 08:29:39 +00001567{
1568 PTPPropertyValue propval;
1569 PTPParams *params = (PTPParams *) device->params;
1570 uint16_t ret;
1571
1572 if (device == NULL) {
1573 return 1;
1574 }
1575
Linus Walleijb2753182007-02-26 08:11:38 +00001576 if (!ptp_operation_issupported(params,PTP_OC_MTP_SetObjectPropValue)) {
1577 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "set_object_u8(): could not set unsigned 8bit integer property: "
1578 "PTP_OC_MTP_SetObjectPropValue not supported.");
1579 return -1;
1580 }
Linus Walleij99310d42006-11-01 08:29:39 +00001581 propval.u8 = value;
1582 ret = ptp_mtp_setobjectpropvalue(params, object_id, attribute_id, &propval, PTP_DTC_UINT8);
1583 if (ret != PTP_RC_OK) {
Linus Walleij070e9b42007-01-22 08:49:28 +00001584 add_ptp_error_to_errorstack(device, ret, "set_object_u8(): could not set unsigned 8bit integer property.");
Linus Walleijf0f3d482006-05-29 14:10:21 +00001585 return 1;
raveloxd9a28642006-05-26 23:42:22 +00001586 }
mopoke96143402006-10-30 04:37:26 +00001587
Linus Walleijf0f3d482006-05-29 14:10:21 +00001588 return 0;
raveloxd9a28642006-05-26 23:42:22 +00001589}
1590
raveloxd9a28642006-05-26 23:42:22 +00001591/**
Linus Walleij039d1dd2007-02-23 22:34:07 +00001592 * Get the first (as in "first in the list of") connected MTP device.
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001593 * @return a device pointer.
Linus Walleij039d1dd2007-02-23 22:34:07 +00001594 * @see LIBMTP_Get_Connected_Devices()
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001595 */
Linus Walleijb9256fd2006-02-15 09:40:43 +00001596LIBMTP_mtpdevice_t *LIBMTP_Get_First_Device(void)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001597{
tedbullocke7713642007-02-18 23:10:09 +00001598 LIBMTP_mtpdevice_t *first_device = NULL;
Linus Walleij64691b72008-05-31 22:55:18 +00001599 LIBMTP_raw_device_t *devices;
1600 int numdevs;
1601 LIBMTP_error_number_t ret;
tedbullocke7713642007-02-18 23:10:09 +00001602
Linus Walleij64691b72008-05-31 22:55:18 +00001603 ret = LIBMTP_Detect_Raw_Devices(&devices, &numdevs);
1604 if (ret != LIBMTP_ERROR_NONE) {
Richard Low0f794762007-02-23 22:06:27 +00001605 return NULL;
Linus Walleija8a19cc2007-02-02 22:13:17 +00001606 }
tedbullock44b0ff72007-02-22 22:20:28 +00001607
Linus Walleij64691b72008-05-31 22:55:18 +00001608 if (devices == NULL || numdevs == 0) {
1609 return NULL;
tedbullock44b0ff72007-02-22 22:20:28 +00001610 }
Linus Walleij64691b72008-05-31 22:55:18 +00001611
1612 first_device = LIBMTP_Open_Raw_Device(&devices[0]);
1613 free(devices);
tedbullocke7713642007-02-18 23:10:09 +00001614 return first_device;
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001615}
1616
1617/**
Linus Walleijd1e14e02008-12-14 01:07:30 +00001618 * Overriding debug function.
1619 * This way we can disable debug prints.
1620 */
1621static void
1622#ifdef __GNUC__
1623__attribute__((__format__(printf,2,0)))
1624#endif
1625LIBMTP_ptp_debug(void *data, const char *format, va_list args)
1626{
1627#ifdef ENABLE_PTP_DEBUG
1628 vfprintf (stderr, format, args);
1629 fflush (stderr);
1630#endif
1631}
1632
1633/**
1634 * Overriding error function.
1635 * This way we can capture all error etc to our errorstack.
1636 */
1637static void
1638#ifdef __GNUC__
1639__attribute__((__format__(printf,2,0)))
1640#endif
1641LIBMTP_ptp_error(void *data, const char *format, va_list args)
1642{
1643 // if (data == NULL) {
1644 vfprintf (stderr, format, args);
1645 fflush (stderr);
1646 /*
1647 FIXME: find out how we shall get the device here.
1648 } else {
1649 PTP_USB *ptp_usb = data;
1650 LIBMTP_mtpdevice_t *device = ...;
1651 char buf[2048];
1652
1653 vsnprintf (buf, sizeof (buf), format, args);
1654 add_error_to_errorstack(device,
1655 LIBMTP_ERROR_PTP_LAYER,
1656 buf);
1657 }
1658 */
1659}
1660
1661/**
Linus Walleijbdb89bd2008-05-28 23:32:35 +00001662 * This function opens a device from a raw device. It is the
1663 * preferred way to access devices in the new interface where
1664 * several devices can come and go as the library is working
1665 * on a certain device.
1666 * @param rawdevice the raw device to open a "real" device for.
1667 * @return an open device.
1668 */
1669LIBMTP_mtpdevice_t *LIBMTP_Open_Raw_Device(LIBMTP_raw_device_t *rawdevice)
1670{
1671 LIBMTP_mtpdevice_t *mtp_device;
1672 uint8_t bs = 0;
1673 PTPParams *current_params;
Linus Walleij3c643252008-05-31 23:22:28 +00001674 PTP_USB *ptp_usb;
Linus Walleijbdb89bd2008-05-28 23:32:35 +00001675 LIBMTP_error_number_t err;
1676 int i;
1677
1678 /* Allocate dynamic space for our device */
1679 mtp_device = (LIBMTP_mtpdevice_t *) malloc(sizeof(LIBMTP_mtpdevice_t));
Richard Lowef197312008-11-01 18:29:41 +00001680 memset(mtp_device, 0, sizeof(LIBMTP_mtpdevice_t));
Linus Walleijbdb89bd2008-05-28 23:32:35 +00001681 /* Check if there was a memory allocation error */
1682 if(mtp_device == NULL) {
1683 /* There has been an memory allocation error. We are going to ignore this
1684 device and attempt to continue */
1685
1686 /* TODO: This error statement could probably be a bit more robust */
1687 fprintf(stderr, "LIBMTP PANIC: connect_usb_devices encountered a memory "
1688 "allocation error with device %d on bus %d, trying to continue",
1689 rawdevice->devnum, rawdevice->bus_location);
1690
1691 return NULL;
1692 }
Linus Walleij3c643252008-05-31 23:22:28 +00001693
1694 /* Create PTP params */
1695 current_params = (PTPParams *) malloc(sizeof(PTPParams));
1696 if (current_params == NULL) {
1697 free(mtp_device);
Linus Walleijbdb89bd2008-05-28 23:32:35 +00001698 return NULL;
Linus Walleij3c643252008-05-31 23:22:28 +00001699 }
1700 memset(current_params, 0, sizeof(PTPParams));
Linus Walleijd4637502009-06-14 23:03:33 +00001701 current_params->device_flags = rawdevice->device_entry.device_flags;
1702 current_params->nrofobjects = 0;
1703 current_params->objects = NULL;
1704 current_params->response_packet_size = 0;
1705 current_params->response_packet = NULL;
Linus Walleijd1e14e02008-12-14 01:07:30 +00001706 /* This will be a pointer to PTP_USB later */
1707 current_params->data = NULL;
1708 /* Set upp local debug and error functions */
1709 current_params->debug_func = LIBMTP_ptp_debug;
1710 current_params->error_func = LIBMTP_ptp_error;
Linus Walleij3c643252008-05-31 23:22:28 +00001711 /* TODO: Will this always be little endian? */
1712 current_params->byteorder = PTP_DL_LE;
1713 current_params->cd_locale_to_ucs2 = iconv_open("UCS-2LE", "UTF-8");
1714 current_params->cd_ucs2_to_locale = iconv_open("UTF-8", "UCS-2LE");
1715
1716 if(current_params->cd_locale_to_ucs2 == (iconv_t) -1 ||
1717 current_params->cd_ucs2_to_locale == (iconv_t) -1) {
1718 fprintf(stderr, "LIBMTP PANIC: Cannot open iconv() converters to/from UCS-2!\n"
1719 "Too old stdlibc, glibc and libiconv?\n");
1720 free(current_params);
1721 free(mtp_device);
1722 return NULL;
1723 }
1724 mtp_device->params = current_params;
1725
1726
1727 /* Create usbinfo, this also opens the session */
1728 err = configure_usb_device(rawdevice,
1729 current_params,
1730 &mtp_device->usbinfo);
1731 if (err != LIBMTP_ERROR_NONE) {
1732 free(current_params);
1733 free(mtp_device);
1734 return NULL;
1735 }
1736 ptp_usb = (PTP_USB*) mtp_device->usbinfo;
1737 /* Set pointer back to params */
1738 ptp_usb->params = current_params;
1739
Linus Walleijbdb89bd2008-05-28 23:32:35 +00001740
1741 /* Cache the device information for later use */
1742 if (ptp_getdeviceinfo(current_params,
1743 &current_params->deviceinfo) != PTP_RC_OK) {
1744 fprintf(stderr, "LIBMTP PANIC: Unable to read device information on device "
1745 "%d on bus %d, trying to continue",
1746 rawdevice->devnum, rawdevice->bus_location);
1747
1748 /* Prevent memory leaks for this device */
1749 free(mtp_device->usbinfo);
1750 free(mtp_device->params);
1751 current_params = NULL;
Linus Walleij3c643252008-05-31 23:22:28 +00001752 free(mtp_device);
Linus Walleijbdb89bd2008-05-28 23:32:35 +00001753 return NULL;
1754 }
1755
1756 /* Determine if the object size supported is 32 or 64 bit wide */
1757 for (i=0;i<current_params->deviceinfo.ImageFormats_len;i++) {
1758 PTPObjectPropDesc opd;
1759
1760 if (ptp_mtp_getobjectpropdesc(current_params,
1761 PTP_OPC_ObjectSize,
1762 current_params->deviceinfo.ImageFormats[i],
1763 &opd) != PTP_RC_OK) {
Linus Walleijd1e14e02008-12-14 01:07:30 +00001764 printf("LIBMTP PANIC: "
Linus Walleijbdb89bd2008-05-28 23:32:35 +00001765 "could not inspect object property descriptions!\n");
1766 } else {
1767 if (opd.DataType == PTP_DTC_UINT32) {
1768 if (bs == 0) {
1769 bs = 32;
1770 } else if (bs != 32) {
Linus Walleijd1e14e02008-12-14 01:07:30 +00001771 printf("LIBMTP PANIC: "
Linus Walleijbdb89bd2008-05-28 23:32:35 +00001772 "different objects support different object sizes!\n");
1773 bs = 0;
1774 break;
1775 }
1776 } else if (opd.DataType == PTP_DTC_UINT64) {
1777 if (bs == 0) {
1778 bs = 64;
1779 } else if (bs != 64) {
Linus Walleijd1e14e02008-12-14 01:07:30 +00001780 printf("LIBMTP PANIC: "
Linus Walleijbdb89bd2008-05-28 23:32:35 +00001781 "different objects support different object sizes!\n");
1782 bs = 0;
1783 break;
1784 }
1785 } else {
1786 // Ignore if other size.
Linus Walleijd1e14e02008-12-14 01:07:30 +00001787 printf("LIBMTP PANIC: "
Linus Walleijbdb89bd2008-05-28 23:32:35 +00001788 "awkward object size data type: %04x\n", opd.DataType);
1789 bs = 0;
1790 break;
1791 }
1792 }
1793 }
1794 if (bs == 0) {
1795 // Could not detect object bitsize, assume 32 bits
1796 bs = 32;
1797 }
1798 mtp_device->object_bitsize = bs;
1799
1800 /* No Errors yet for this device */
1801 mtp_device->errorstack = NULL;
1802
1803 /* Default Max Battery Level, we will adjust this if possible */
1804 mtp_device->maximum_battery_level = 100;
1805
1806 /* Check if device supports reading maximum battery level */
Linus Walleij4096c882009-03-16 23:32:34 +00001807 if(!FLAG_BROKEN_BATTERY_LEVEL(ptp_usb) &&
1808 ptp_property_issupported( current_params, PTP_DPC_BatteryLevel)) {
Linus Walleijbdb89bd2008-05-28 23:32:35 +00001809 PTPDevicePropDesc dpd;
1810
1811 /* Try to read maximum battery level */
1812 if(ptp_getdevicepropdesc(current_params,
1813 PTP_DPC_BatteryLevel,
1814 &dpd) != PTP_RC_OK) {
1815 add_error_to_errorstack(mtp_device,
1816 LIBMTP_ERROR_CONNECTING,
1817 "Unable to read Maximum Battery Level for this "
1818 "device even though the device supposedly "
1819 "supports this functionality");
1820 }
1821
1822 /* TODO: is this appropriate? */
1823 /* If max battery level is 0 then leave the default, otherwise assign */
1824 if (dpd.FORM.Range.MaximumValue.u8 != 0) {
1825 mtp_device->maximum_battery_level = dpd.FORM.Range.MaximumValue.u8;
1826 }
1827
1828 ptp_free_devicepropdesc(&dpd);
1829 }
1830
1831 /* Set all default folders to 0 (root directory) */
1832 mtp_device->default_music_folder = 0;
1833 mtp_device->default_playlist_folder = 0;
1834 mtp_device->default_picture_folder = 0;
1835 mtp_device->default_video_folder = 0;
1836 mtp_device->default_organizer_folder = 0;
1837 mtp_device->default_zencast_folder = 0;
1838 mtp_device->default_album_folder = 0;
1839 mtp_device->default_text_folder = 0;
1840
1841 /* Set initial storage information */
1842 mtp_device->storage = NULL;
1843 if (LIBMTP_Get_Storage(mtp_device, LIBMTP_STORAGE_SORTBY_NOTSORTED) == -1) {
1844 add_error_to_errorstack(mtp_device,
1845 LIBMTP_ERROR_GENERAL,
1846 "Get Storage information failed.");
1847 mtp_device->storage = NULL;
1848 }
1849
1850 /*
1851 * Then get the handles and try to locate the default folders.
1852 * This has the desired side effect of caching all handles from
1853 * the device which speeds up later operations.
1854 */
1855 flush_handles(mtp_device);
1856
1857 return mtp_device;
1858}
1859
1860/**
Linus Walleij2d3f7b82007-02-14 09:24:20 +00001861 * Recursive function that adds MTP devices to a linked list
Linus Walleija700d222008-05-28 23:06:14 +00001862 * @param devices a list of raw devices to have real devices created for.
Linus Walleij2d3f7b82007-02-14 09:24:20 +00001863 * @return a device pointer to a newly created mtpdevice (used in linked
Linus Walleijb0ab5482007-08-29 21:08:54 +00001864 * list creation).
Linus Walleij2d3f7b82007-02-14 09:24:20 +00001865 */
Linus Walleija700d222008-05-28 23:06:14 +00001866static LIBMTP_mtpdevice_t * create_usb_mtp_devices(LIBMTP_raw_device_t *devices, int numdevs)
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001867{
Linus Walleija700d222008-05-28 23:06:14 +00001868 uint8_t i;
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001869 LIBMTP_mtpdevice_t *mtp_device_list = NULL;
1870 LIBMTP_mtpdevice_t *current_device = NULL;
Linus Walleija700d222008-05-28 23:06:14 +00001871
1872 for (i=0; i < numdevs; i++) {
Linus Walleij2d3f7b82007-02-14 09:24:20 +00001873 LIBMTP_mtpdevice_t *mtp_device;
Linus Walleijbdb89bd2008-05-28 23:32:35 +00001874 mtp_device = LIBMTP_Open_Raw_Device(&devices[i]);
Linus Walleija700d222008-05-28 23:06:14 +00001875
Linus Walleijbdb89bd2008-05-28 23:32:35 +00001876 /* On error, try next device */
1877 if (mtp_device == NULL)
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001878 continue;
Linus Walleijddaba2f2007-10-02 21:20:30 +00001879
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001880 /* Add the device to the list */
1881 mtp_device->next = NULL;
1882 if (mtp_device_list == NULL) {
1883 mtp_device_list = current_device = mtp_device;
1884 } else {
1885 current_device->next = mtp_device;
1886 current_device = mtp_device;
1887 }
Linus Walleij2d3f7b82007-02-14 09:24:20 +00001888 }
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001889 return mtp_device_list;
Linus Walleij2d3f7b82007-02-14 09:24:20 +00001890}
1891
1892/**
tedbullock848009b2007-03-03 23:20:12 +00001893 * Get the number of devices that are available in the listed device list
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001894 * @param device_list Pointer to a linked list of devices
1895 * @return Number of devices in the device list device_list
1896 * @see LIBMTP_Get_Connected_Devices()
tedbullock848009b2007-03-03 23:20:12 +00001897 */
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001898uint32_t LIBMTP_Number_Devices_In_List(LIBMTP_mtpdevice_t *device_list)
tedbullock848009b2007-03-03 23:20:12 +00001899{
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001900 uint32_t numdevices = 0;
1901 LIBMTP_mtpdevice_t *iter;
1902 for(iter = device_list; iter != NULL; iter = iter->next)
1903 numdevices++;
1904
1905 return numdevices;
tedbullock848009b2007-03-03 23:20:12 +00001906}
1907
1908/**
Linus Walleij2d3f7b82007-02-14 09:24:20 +00001909 * Get the first connected MTP device node in the linked list of devices.
1910 * Currently this only provides access to USB devices
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001911 * @param device_list A list of devices ready to be used by the caller. You
1912 * need to know how many there are.
Linus Walleij2d3f7b82007-02-14 09:24:20 +00001913 * @return Any error information gathered from device connections
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001914 * @see LIBMTP_Number_Devices_In_List()
Linus Walleij2d3f7b82007-02-14 09:24:20 +00001915 */
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001916LIBMTP_error_number_t LIBMTP_Get_Connected_Devices(LIBMTP_mtpdevice_t **device_list)
Linus Walleij2d3f7b82007-02-14 09:24:20 +00001917{
Linus Walleija700d222008-05-28 23:06:14 +00001918 LIBMTP_raw_device_t *devices;
1919 int numdevs;
Linus Walleij5fbb77d2007-03-07 08:40:36 +00001920 LIBMTP_error_number_t ret;
Linus Walleija700d222008-05-28 23:06:14 +00001921
1922 ret = LIBMTP_Detect_Raw_Devices(&devices, &numdevs);
Linus Walleij5fbb77d2007-03-07 08:40:36 +00001923 if (ret != LIBMTP_ERROR_NONE) {
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001924 *device_list = NULL;
Linus Walleij5fbb77d2007-03-07 08:40:36 +00001925 return ret;
Linus Walleij2d3f7b82007-02-14 09:24:20 +00001926 }
1927
1928 /* Assign linked list of devices */
Linus Walleija700d222008-05-28 23:06:14 +00001929 if (devices == NULL || numdevs == 0) {
1930 *device_list = NULL;
1931 return LIBMTP_ERROR_NO_DEVICE_ATTACHED;
1932 }
1933
1934 *device_list = create_usb_mtp_devices(devices, numdevs);
1935 free(devices);
Linus Walleij45a86372007-03-07 09:36:19 +00001936
Linus Walleij2d3f7b82007-02-14 09:24:20 +00001937 /* TODO: Add wifi device access here */
Richard Low99a93e82007-09-29 08:16:08 +00001938
1939 /* We have found some devices but create failed */
1940 if (*device_list == NULL)
1941 return LIBMTP_ERROR_CONNECTING;
Linus Walleij2d3f7b82007-02-14 09:24:20 +00001942
1943 return LIBMTP_ERROR_NONE;
1944}
1945
1946/**
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001947 * This closes and releases an allocated MTP device.
Linus Walleijb9256fd2006-02-15 09:40:43 +00001948 * @param device a pointer to the MTP device to release.
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001949 */
tedbullock0f033cb2007-02-14 20:56:54 +00001950void LIBMTP_Release_Device_List(LIBMTP_mtpdevice_t *device)
1951{
1952 if(device != NULL)
1953 {
1954 if(device->next != NULL)
1955 {
1956 LIBMTP_Release_Device_List(device->next);
1957 }
1958
1959 LIBMTP_Release_Device(device);
1960 }
1961}
1962
1963/**
1964 * This closes and releases an allocated MTP device.
1965 * @param device a pointer to the MTP device to release.
1966 */
Linus Walleijb9256fd2006-02-15 09:40:43 +00001967void LIBMTP_Release_Device(LIBMTP_mtpdevice_t *device)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001968{
Linus Walleij9b28da32006-03-16 13:47:58 +00001969 PTPParams *params = (PTPParams *) device->params;
Linus Walleij2d411db2006-03-22 12:13:09 +00001970 PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo;
Linus Walleij9b28da32006-03-16 13:47:58 +00001971
Linus Walleijb0ab5482007-08-29 21:08:54 +00001972 close_device(ptp_usb, params);
Linus Walleij2715c442007-01-20 22:35:29 +00001973 // Clear error stack
1974 LIBMTP_Clear_Errorstack(device);
Linus Walleij3ec86312006-08-21 13:25:24 +00001975 // Free iconv() converters...
Linus Walleija823a702006-08-27 21:27:46 +00001976 iconv_close(params->cd_locale_to_ucs2);
1977 iconv_close(params->cd_ucs2_to_locale);
tedbullock69a445b2007-02-15 07:41:04 +00001978 free(ptp_usb);
Linus Walleij073c4172007-02-02 22:26:33 +00001979 ptp_free_params(params);
Linus Walleij9e1b0812006-12-12 19:22:02 +00001980 free_storage_list(device);
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001981 free(device);
1982}
Linus Walleijb9256fd2006-02-15 09:40:43 +00001983
1984/**
Linus Walleij2715c442007-01-20 22:35:29 +00001985 * This can be used by any libmtp-intrinsic code that
Linus Walleij68b19c02007-02-15 11:50:37 +00001986 * need to stack up an error on the stack. You are only
1987 * supposed to add errors to the error stack using this
1988 * function, do not create and reference error entries
1989 * directly.
Linus Walleij2715c442007-01-20 22:35:29 +00001990 */
1991static void add_error_to_errorstack(LIBMTP_mtpdevice_t *device,
1992 LIBMTP_error_number_t errornumber,
1993 char const * const error_text)
1994{
1995 LIBMTP_error_t *newerror;
1996
Linus Walleij68b19c02007-02-15 11:50:37 +00001997 if (device == NULL) {
1998 fprintf(stderr, "LIBMTP PANIC: Trying to add error to a NULL device!\n");
1999 return;
2000 }
Linus Walleij2715c442007-01-20 22:35:29 +00002001 newerror = (LIBMTP_error_t *) malloc(sizeof(LIBMTP_error_t));
2002 newerror->errornumber = errornumber;
2003 newerror->error_text = strdup(error_text);
rreardon774503c2007-02-15 15:52:49 +00002004 newerror->next = NULL;
Linus Walleij2715c442007-01-20 22:35:29 +00002005 if (device->errorstack == NULL) {
2006 device->errorstack = newerror;
2007 } else {
2008 LIBMTP_error_t *tmp = device->errorstack;
2009
2010 while (tmp->next != NULL) {
2011 tmp = tmp->next;
2012 }
2013 tmp->next = newerror;
2014 }
2015}
2016
2017/**
Linus Walleij070e9b42007-01-22 08:49:28 +00002018 * Adds an error from the PTP layer to the error stack.
2019 */
2020static void add_ptp_error_to_errorstack(LIBMTP_mtpdevice_t *device,
2021 uint16_t ptp_error,
2022 char const * const error_text)
2023{
Linus Walleij68b19c02007-02-15 11:50:37 +00002024 if (device == NULL) {
2025 fprintf(stderr, "LIBMTP PANIC: Trying to add PTP error to a NULL device!\n");
2026 return;
2027 } else {
2028 char outstr[256];
2029 snprintf(outstr, sizeof(outstr), "PTP Layer error %04x: %s", ptp_error, error_text);
2030 outstr[sizeof(outstr)-1] = '\0';
2031 add_error_to_errorstack(device, LIBMTP_ERROR_PTP_LAYER, outstr);
2032 add_error_to_errorstack(device, LIBMTP_ERROR_PTP_LAYER, "(Look this up in ptp.h for an explanation.)");
2033 }
Linus Walleij070e9b42007-01-22 08:49:28 +00002034}
2035
2036/**
Linus Walleij2715c442007-01-20 22:35:29 +00002037 * This returns the error stack for a device in case you
2038 * need to either reference the error numbers (e.g. when
2039 * creating multilingual apps with multiple-language text
2040 * representations for each error number) or when you need
2041 * to build a multi-line error text widget or something like
2042 * that. You need to call the <code>LIBMTP_Clear_Errorstack</code>
2043 * to clear it when you're finished with it.
2044 * @param device a pointer to the MTP device to get the error
2045 * stack for.
2046 * @return the error stack or NULL if there are no errors
2047 * on the stack.
2048 * @see LIBMTP_Clear_Errorstack()
2049 * @see LIBMTP_Dump_Errorstack()
2050 */
2051LIBMTP_error_t *LIBMTP_Get_Errorstack(LIBMTP_mtpdevice_t *device)
2052{
Linus Walleij68b19c02007-02-15 11:50:37 +00002053 if (device == NULL) {
2054 fprintf(stderr, "LIBMTP PANIC: Trying to get the error stack of a NULL device!\n");
Linus Walleij5c1499e2009-02-21 06:54:29 +00002055 return NULL;
Linus Walleij68b19c02007-02-15 11:50:37 +00002056 }
Linus Walleij2715c442007-01-20 22:35:29 +00002057 return device->errorstack;
2058}
2059
2060/**
2061 * This function clears the error stack of a device and frees
2062 * any memory used by it. Call this when you're finished with
2063 * using the errors.
2064 * @param device a pointer to the MTP device to clear the error
2065 * stack for.
2066 */
2067void LIBMTP_Clear_Errorstack(LIBMTP_mtpdevice_t *device)
2068{
Linus Walleij68b19c02007-02-15 11:50:37 +00002069 if (device == NULL) {
2070 fprintf(stderr, "LIBMTP PANIC: Trying to clear the error stack of a NULL device!\n");
2071 } else {
2072 LIBMTP_error_t *tmp = device->errorstack;
Linus Walleij2715c442007-01-20 22:35:29 +00002073
Linus Walleij68b19c02007-02-15 11:50:37 +00002074 while (tmp != NULL) {
2075 LIBMTP_error_t *tmp2;
2076
2077 if (tmp->error_text != NULL) {
2078 free(tmp->error_text);
2079 }
2080 tmp2 = tmp;
2081 tmp = tmp->next;
2082 free(tmp2);
Linus Walleij2715c442007-01-20 22:35:29 +00002083 }
Linus Walleij68b19c02007-02-15 11:50:37 +00002084 device->errorstack = NULL;
Linus Walleij2715c442007-01-20 22:35:29 +00002085 }
Linus Walleij2715c442007-01-20 22:35:29 +00002086}
2087
2088/**
2089 * This function dumps the error stack to <code>stderr</code>.
2090 * (You still have to clear the stack though.)
2091 * @param device a pointer to the MTP device to dump the error
2092 * stack for.
2093 */
2094void LIBMTP_Dump_Errorstack(LIBMTP_mtpdevice_t *device)
2095{
Linus Walleij68b19c02007-02-15 11:50:37 +00002096 if (device == NULL) {
2097 fprintf(stderr, "LIBMTP PANIC: Trying to dump the error stack of a NULL device!\n");
2098 } else {
2099 LIBMTP_error_t *tmp = device->errorstack;
Linus Walleij2715c442007-01-20 22:35:29 +00002100
Linus Walleij68b19c02007-02-15 11:50:37 +00002101 while (tmp != NULL) {
2102 if (tmp->error_text != NULL) {
2103 fprintf(stderr, "Error %d: %s\n", tmp->errornumber, tmp->error_text);
2104 } else {
2105 fprintf(stderr, "Error %d: (unknown)\n", tmp->errornumber);
2106 }
2107 tmp = tmp->next;
Linus Walleij2715c442007-01-20 22:35:29 +00002108 }
Linus Walleij2715c442007-01-20 22:35:29 +00002109 }
2110}
2111
2112/**
Linus Walleij338ade42007-07-03 20:44:08 +00002113 * This command gets all handles and stuff by FAST directory retrieveal
2114 * which is available by getting all metadata for object
2115 * <code>0xffffffff</code> which simply means "all metadata for all objects".
2116 * This works on the vast majority of MTP devices (there ARE exceptions!)
2117 * and is quite quick. Check the error stack to see if there were
2118 * problems getting the metadata.
Linus Walleij8533bf72007-08-28 10:03:59 +00002119 * @return 0 if all was OK, -1 on failure.
Linus Walleij338ade42007-07-03 20:44:08 +00002120 */
Linus Walleij6bf68b42007-09-03 22:50:02 +00002121static int get_all_metadata_fast(LIBMTP_mtpdevice_t *device,
2122 uint32_t storage)
Linus Walleij338ade42007-07-03 20:44:08 +00002123{
2124 PTPParams *params = (PTPParams *) device->params;
2125 int cnt = 0;
Linus Walleij1e9a0332007-09-12 19:35:56 +00002126 int i, j, nrofprops;
Linus Walleij338ade42007-07-03 20:44:08 +00002127 uint32_t lasthandle = 0xffffffff;
Linus Walleij1e9a0332007-09-12 19:35:56 +00002128 MTPProperties *props = NULL;
2129 MTPProperties *prop;
Linus Walleij338ade42007-07-03 20:44:08 +00002130 uint16_t ret;
Linus Walleij2f622812008-08-30 22:06:58 +00002131 int oldtimeout;
2132 PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo;
2133
2134 /* The follow request causes the device to generate
2135 * a list of very file on the device and return it
2136 * in a single response.
2137 *
2138 * Some slow devices as well as devices with very
2139 * large file systems can easily take longer then
2140 * the standard timeout value before it is able
2141 * to return a response.
2142 *
2143 * Temporarly set timeout to allow working with
2144 * widest range of devices.
2145 */
2146 get_usb_device_timeout(ptp_usb, &oldtimeout);
2147 set_usb_device_timeout(ptp_usb, 60000);
Linus Walleij338ade42007-07-03 20:44:08 +00002148
Linus Walleijfcb43422008-05-23 21:53:55 +00002149 ret = ptp_mtp_getobjectproplist(params, 0xffffffff, &props, &nrofprops);
Linus Walleij2f622812008-08-30 22:06:58 +00002150 set_usb_device_timeout(ptp_usb, oldtimeout);
Linus Walleij8533bf72007-08-28 10:03:59 +00002151
2152 if (ret == PTP_RC_MTP_Specification_By_Group_Unsupported) {
2153 // What's the point in the device implementing this command if
2154 // you cannot use it to get all props for AT LEAST one object?
2155 // Well, whatever...
2156 add_ptp_error_to_errorstack(device, ret, "get_all_metadata_fast(): "
2157 "cannot retrieve all metadata for an object on this device.");
Linus Walleij91e98132007-08-28 10:05:11 +00002158 return -1;
Linus Walleij8533bf72007-08-28 10:03:59 +00002159 }
Linus Walleij338ade42007-07-03 20:44:08 +00002160 if (ret != PTP_RC_OK) {
Linus Walleij8533bf72007-08-28 10:03:59 +00002161 add_ptp_error_to_errorstack(device, ret, "get_all_metadata_fast(): "
2162 "could not get proplist of all objects.");
2163 return -1;
Linus Walleij338ade42007-07-03 20:44:08 +00002164 }
Linus Walleijfcb43422008-05-23 21:53:55 +00002165 if (props == NULL && nrofprops != 0) {
2166 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL,
2167 "get_all_metadata_fast(): "
2168 "call to ptp_mtp_getobjectproplist() returned "
2169 "inconsistent results.");
2170 return -1;
2171 }
Linus Walleij338ade42007-07-03 20:44:08 +00002172 /*
2173 * We count the number of objects by counting the ObjectHandle
Linus Walleij31b74292008-05-02 23:29:06 +00002174 * references, whenever it changes we get a new object, when it's
Linus Walleij338ade42007-07-03 20:44:08 +00002175 * the same, it is just different properties of the same object.
2176 */
Linus Walleij1e9a0332007-09-12 19:35:56 +00002177 prop = props;
2178 for (i=0;i<nrofprops;i++) {
Linus Walleij338ade42007-07-03 20:44:08 +00002179 if (lasthandle != prop->ObjectHandle) {
2180 cnt++;
2181 lasthandle = prop->ObjectHandle;
2182 }
Linus Walleij1e9a0332007-09-12 19:35:56 +00002183 prop++;
Linus Walleij338ade42007-07-03 20:44:08 +00002184 }
Linus Walleij338ade42007-07-03 20:44:08 +00002185 lasthandle = 0xffffffff;
Linus Walleijd4637502009-06-14 23:03:33 +00002186 params->objects = calloc (sizeof(PTPObject),cnt);
Linus Walleij1e9a0332007-09-12 19:35:56 +00002187 prop = props;
Linus Walleij338ade42007-07-03 20:44:08 +00002188 i = -1;
Linus Walleij1e9a0332007-09-12 19:35:56 +00002189 for (j=0;j<nrofprops;j++) {
Linus Walleij338ade42007-07-03 20:44:08 +00002190 if (lasthandle != prop->ObjectHandle) {
2191 if (i >= 0) {
Linus Walleij41bf72c2009-08-25 09:33:03 +00002192 params->objects[i].flags |= PTPOBJECT_MTPPROPLIST_LOADED|PTPOBJECT_OBJECTINFO_LOADED;
Linus Walleijd4637502009-06-14 23:03:33 +00002193 if (!params->objects[i].oi.Filename) {
Linus Walleij6bf68b42007-09-03 22:50:02 +00002194 /* I have one such file on my Creative (Marcus) */
Linus Walleijd4637502009-06-14 23:03:33 +00002195 params->objects[i].oi.Filename = strdup("<null>");
Linus Walleij338ade42007-07-03 20:44:08 +00002196 }
2197 }
2198 i++;
2199 lasthandle = prop->ObjectHandle;
Linus Walleijd4637502009-06-14 23:03:33 +00002200 params->objects[i].oid = prop->ObjectHandle;
Linus Walleij338ade42007-07-03 20:44:08 +00002201 }
2202 switch (prop->property) {
2203 case PTP_OPC_ParentObject:
Linus Walleijd4637502009-06-14 23:03:33 +00002204 params->objects[i].oi.ParentObject = prop->propval.u32;
2205 params->objects[i].flags |= PTPOBJECT_PARENTOBJECT_LOADED;
Linus Walleij338ade42007-07-03 20:44:08 +00002206 break;
2207 case PTP_OPC_ObjectFormat:
Linus Walleijd4637502009-06-14 23:03:33 +00002208 params->objects[i].oi.ObjectFormat = prop->propval.u16;
Linus Walleij338ade42007-07-03 20:44:08 +00002209 break;
2210 case PTP_OPC_ObjectSize:
Linus Walleijd9d28d52007-08-04 19:01:18 +00002211 // We loose precision here, up to 32 bits! However the commands that
2212 // retrieve metadata for files and tracks will make sure that the
2213 // PTP_OPC_ObjectSize is read in and duplicated again.
Linus Walleijddaba2f2007-10-02 21:20:30 +00002214 if (device->object_bitsize == 64) {
Linus Walleijd4637502009-06-14 23:03:33 +00002215 params->objects[i].oi.ObjectCompressedSize = (uint32_t) prop->propval.u64;
Linus Walleijddaba2f2007-10-02 21:20:30 +00002216 } else {
Linus Walleijd4637502009-06-14 23:03:33 +00002217 params->objects[i].oi.ObjectCompressedSize = prop->propval.u32;
Linus Walleijddaba2f2007-10-02 21:20:30 +00002218 }
Linus Walleij338ade42007-07-03 20:44:08 +00002219 break;
2220 case PTP_OPC_StorageID:
Linus Walleijd4637502009-06-14 23:03:33 +00002221 params->objects[i].oi.StorageID = prop->propval.u32;
2222 params->objects[i].flags |= PTPOBJECT_STORAGEID_LOADED;
Linus Walleij338ade42007-07-03 20:44:08 +00002223 break;
2224 case PTP_OPC_ObjectFileName:
Richard Low8e8d9d42007-07-14 10:36:50 +00002225 if (prop->propval.str != NULL)
Linus Walleijd4637502009-06-14 23:03:33 +00002226 params->objects[i].oi.Filename = strdup(prop->propval.str);
Linus Walleij338ade42007-07-03 20:44:08 +00002227 break;
Linus Walleijd4637502009-06-14 23:03:33 +00002228 default: {
2229 MTPProperties *newprops;
2230
2231 /* Copy all of the other MTP oprierties into the per-object proplist */
2232 if (params->objects[i].nrofmtpprops) {
2233 newprops = realloc(params->objects[i].mtpprops,(params->objects[i].nrofmtpprops+1)*sizeof(MTPProperties));
2234 } else {
2235 newprops = calloc(sizeof(MTPProperties),1);
2236 }
2237 if (!newprops) return 0; /* FIXME: error handling? */
2238 params->objects[i].mtpprops = newprops;
2239 memcpy(&params->objects[i].mtpprops[params->objects[i].nrofmtpprops],&props[j],sizeof(props[j]));
2240 params->objects[i].nrofmtpprops++;
Linus Walleij362d13e2009-08-02 19:59:21 +00002241 params->objects[i].flags |= PTPOBJECT_MTPPROPLIST_LOADED;
Linus Walleij338ade42007-07-03 20:44:08 +00002242 break;
Linus Walleijd4637502009-06-14 23:03:33 +00002243 }
Linus Walleij338ade42007-07-03 20:44:08 +00002244 }
Linus Walleij1e9a0332007-09-12 19:35:56 +00002245 prop++;
Linus Walleij338ade42007-07-03 20:44:08 +00002246 }
Linus Walleij8533bf72007-08-28 10:03:59 +00002247 return 0;
Linus Walleij338ade42007-07-03 20:44:08 +00002248}
2249
2250/**
2251 * This function will recurse through all the directories on the device,
2252 * starting at the root directory, gathering metadata as it moves along.
2253 * It works better on some devices that will only return data for a
2254 * certain directory and does not respect the option to get all metadata
2255 * for all objects.
2256 */
Linus Walleij6bf68b42007-09-03 22:50:02 +00002257static void get_handles_recursively(LIBMTP_mtpdevice_t *device,
2258 PTPParams *params,
Linus Walleij6bf68b42007-09-03 22:50:02 +00002259 uint32_t storageid,
2260 uint32_t parent)
Linus Walleij338ade42007-07-03 20:44:08 +00002261{
2262 PTPObjectHandles currentHandles;
2263 int i = 0;
Linus Walleij338ade42007-07-03 20:44:08 +00002264 uint16_t ret = ptp_getobjecthandles(params,
Linus Walleij6bf68b42007-09-03 22:50:02 +00002265 storageid,
Linus Walleij338ade42007-07-03 20:44:08 +00002266 PTP_GOH_ALL_FORMATS,
2267 parent,
2268 &currentHandles);
2269
2270 if (ret != PTP_RC_OK) {
2271 add_ptp_error_to_errorstack(device, ret, "get_handles_recursively(): could not get object handles.");
2272 return;
2273 }
2274
2275 if (currentHandles.Handler == NULL || currentHandles.n == 0)
2276 return;
2277
Linus Walleij338ade42007-07-03 20:44:08 +00002278 // Now descend into any subdirectories found
2279 for (i = 0; i < currentHandles.n; i++) {
Linus Walleijd4637502009-06-14 23:03:33 +00002280 PTPObject *ob;
2281 ret = ptp_object_want(params,currentHandles.Handler[i],PTPOBJECT_OBJECTINFO_LOADED, &ob);
Linus Walleij338ade42007-07-03 20:44:08 +00002282 if (ret == PTP_RC_OK) {
Linus Walleijd4637502009-06-14 23:03:33 +00002283 if (ob->oi.ObjectFormat == PTP_OFC_Association)
2284 get_handles_recursively(device, params, storageid, currentHandles.Handler[i]);
Linus Walleij338ade42007-07-03 20:44:08 +00002285 } else {
2286 add_error_to_errorstack(device,
2287 LIBMTP_ERROR_CONNECTING,
2288 "Found a bad handle, trying to ignore it.");
2289 }
2290 }
Linus Walleij338ade42007-07-03 20:44:08 +00002291 free(currentHandles.Handler);
2292}
2293
2294/**
Linus Walleij438bd7f2006-06-08 11:35:44 +00002295 * This function refresh the internal handle list whenever
2296 * the items stored inside the device is altered. On operations
2297 * that do not add or remove objects, this is typically not
2298 * called.
2299 * @param device a pointer to the MTP device to flush handles for.
2300 */
2301static void flush_handles(LIBMTP_mtpdevice_t *device)
2302{
2303 PTPParams *params = (PTPParams *) device->params;
Linus Walleij338ade42007-07-03 20:44:08 +00002304 PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo;
Linus Walleij8533bf72007-08-28 10:03:59 +00002305 int ret;
Linus Walleijf0bf4372007-07-01 21:47:38 +00002306 uint32_t i;
2307
Linus Walleijd4637502009-06-14 23:03:33 +00002308 if (params->objects != NULL) {
2309 for (i=0;i<params->nrofobjects;i++)
2310 ptp_free_object (&params->objects[i]);
2311 free(params->objects);
2312 params->objects = NULL;
2313 params->nrofobjects = 0;
Linus Walleij438bd7f2006-06-08 11:35:44 +00002314 }
Linus Walleijf0bf4372007-07-01 21:47:38 +00002315
Linus Walleij338ade42007-07-03 20:44:08 +00002316 if (ptp_operation_issupported(params,PTP_OC_MTP_GetObjPropList)
Linus Walleijfec4d562008-06-01 22:30:36 +00002317 && !FLAG_BROKEN_MTPGETOBJPROPLIST(ptp_usb)
2318 && !FLAG_BROKEN_MTPGETOBJPROPLIST_ALL(ptp_usb)) {
Linus Walleij139455e2007-08-28 10:25:06 +00002319 // Use the fast method. Ignore return value for now.
Linus Walleij6bf68b42007-09-03 22:50:02 +00002320 ret = get_all_metadata_fast(device, PTP_GOH_ALL_STORAGE);
Linus Walleij8533bf72007-08-28 10:03:59 +00002321 }
Linus Walleij139455e2007-08-28 10:25:06 +00002322 // If the previous failed or returned no objects, use classic
2323 // methods instead.
Linus Walleijd4637502009-06-14 23:03:33 +00002324 if (params->nrofobjects == 0) {
Linus Walleij338ade42007-07-03 20:44:08 +00002325 // Get all the handles using just standard commands.
Linus Walleij6bf68b42007-09-03 22:50:02 +00002326 if (device->storage == NULL) {
2327 get_handles_recursively(device, params,
Linus Walleij6bf68b42007-09-03 22:50:02 +00002328 PTP_GOH_ALL_STORAGE,
2329 PTP_GOH_ROOT_PARENT);
2330 } else {
2331 // Get handles for each storage in turn.
2332 LIBMTP_devicestorage_t *storage = device->storage;
2333 while(storage != NULL) {
2334 get_handles_recursively(device, params,
Linus Walleij6bf68b42007-09-03 22:50:02 +00002335 storage->id,
2336 PTP_GOH_ROOT_PARENT);
2337 storage = storage->next;
2338 }
2339 }
Linus Walleij338ade42007-07-03 20:44:08 +00002340 }
Linus Walleij8f7f1aa2008-06-15 19:00:23 +00002341
2342 /*
2343 * Loop over the handles, fix up any NULL filenames or
2344 * keywords, then attempt to locate some default folders
2345 * in the root directory of the primary storage.
2346 */
Linus Walleijd4637502009-06-14 23:03:33 +00002347 for(i = 0; i < params->nrofobjects; i++) {
2348 PTPObject *ob, *xob;
2349
2350 ob = &params->objects[i];
2351 ret = ptp_object_want(params,params->objects[i].oid,PTPOBJECT_OBJECTINFO_LOADED, &xob);
2352 if (ret != PTP_RC_OK) {
2353 fprintf(stderr,"broken! %x not found\n", params->objects[i].oid);
Linus Walleijf0bf4372007-07-01 21:47:38 +00002354 }
Linus Walleijd4637502009-06-14 23:03:33 +00002355 if (ob->oi.Filename == NULL)
2356 ob->oi.Filename = strdup("<null>");
2357 if (ob->oi.Keywords == NULL)
2358 ob->oi.Keywords = strdup("<null>");
2359
Linus Walleijf0bf4372007-07-01 21:47:38 +00002360 /* Ignore handles that point to non-folders */
Linus Walleijd4637502009-06-14 23:03:33 +00002361 if(ob->oi.ObjectFormat != PTP_OFC_Association)
Linus Walleijf0bf4372007-07-01 21:47:38 +00002362 continue;
Linus Walleij8f7f1aa2008-06-15 19:00:23 +00002363 /* Only look in the root folder */
Linus Walleijd4637502009-06-14 23:03:33 +00002364 if (ob->oi.ParentObject != 0x00000000U)
Linus Walleijf0bf4372007-07-01 21:47:38 +00002365 continue;
Linus Walleijc40c9bf2008-06-13 23:24:17 +00002366 /* Only look in the primary storage */
Linus Walleijd4637502009-06-14 23:03:33 +00002367 if (device->storage != NULL && ob->oi.StorageID != device->storage->id)
Linus Walleijc40c9bf2008-06-13 23:24:17 +00002368 continue;
Linus Walleij8f7f1aa2008-06-15 19:00:23 +00002369
Linus Walleijf0bf4372007-07-01 21:47:38 +00002370
2371 /* Is this the Music Folder */
Linus Walleijd4637502009-06-14 23:03:33 +00002372 if (!strcasecmp(ob->oi.Filename, "My Music") ||
2373 !strcasecmp(ob->oi.Filename, "Music")) {
2374 device->default_music_folder = ob->oid;
Linus Walleijf0bf4372007-07-01 21:47:38 +00002375 }
Linus Walleijd4637502009-06-14 23:03:33 +00002376 else if (!strcasecmp(ob->oi.Filename, "My Playlists") ||
2377 !strcasecmp(ob->oi.Filename, "Playlists")) {
2378 device->default_playlist_folder = ob->oid;
Linus Walleijf0bf4372007-07-01 21:47:38 +00002379 }
Linus Walleijd4637502009-06-14 23:03:33 +00002380 else if (!strcasecmp(ob->oi.Filename, "My Pictures") ||
2381 !strcasecmp(ob->oi.Filename, "Pictures")) {
2382 device->default_picture_folder = ob->oid;
Linus Walleijf0bf4372007-07-01 21:47:38 +00002383 }
Linus Walleijd4637502009-06-14 23:03:33 +00002384 else if (!strcasecmp(ob->oi.Filename, "My Video") ||
2385 !strcasecmp(ob->oi.Filename, "Video")) {
2386 device->default_video_folder = ob->oid;
Linus Walleijf0bf4372007-07-01 21:47:38 +00002387 }
Linus Walleijd4637502009-06-14 23:03:33 +00002388 else if (!strcasecmp(ob->oi.Filename, "My Organizer")) {
2389 device->default_organizer_folder = ob->oid;
Linus Walleijf0bf4372007-07-01 21:47:38 +00002390 }
Linus Walleijd4637502009-06-14 23:03:33 +00002391 else if (!strcasecmp(ob->oi.Filename, "ZENcast") ||
2392 !strcasecmp(ob->oi.Filename, "Datacasts")) {
2393 device->default_zencast_folder = ob->oid;
Linus Walleijf0bf4372007-07-01 21:47:38 +00002394 }
Linus Walleijd4637502009-06-14 23:03:33 +00002395 else if (!strcasecmp(ob->oi.Filename, "My Albums") ||
2396 !strcasecmp(ob->oi.Filename, "Albums")) {
2397 device->default_album_folder = ob->oid;
Linus Walleijf0bf4372007-07-01 21:47:38 +00002398 }
Linus Walleijd4637502009-06-14 23:03:33 +00002399 else if (!strcasecmp(ob->oi.Filename, "Text") ||
2400 !strcasecmp(ob->oi.Filename, "Texts")) {
2401 device->default_text_folder = ob->oid;
Linus Walleijf0bf4372007-07-01 21:47:38 +00002402 }
2403 }
Richard Low6711f442007-05-05 19:00:59 +00002404}
Linus Walleij438bd7f2006-06-08 11:35:44 +00002405
Linus Walleij438bd7f2006-06-08 11:35:44 +00002406/**
Linus Walleij9e1b0812006-12-12 19:22:02 +00002407 * This function traverses a devices storage list freeing up the
2408 * strings and the structs.
2409 * @param device a pointer to the MTP device to free the storage
2410 * list for.
2411 */
2412static void free_storage_list(LIBMTP_mtpdevice_t *device)
2413{
Linus Walleije1ac07e2006-12-14 19:38:59 +00002414 LIBMTP_devicestorage_t *storage;
2415 LIBMTP_devicestorage_t *tmp;
Linus Walleij9e1b0812006-12-12 19:22:02 +00002416
Linus Walleije1ac07e2006-12-14 19:38:59 +00002417 storage = device->storage;
2418 while(storage != NULL) {
2419 if (storage->StorageDescription != NULL) {
2420 free(storage->StorageDescription);
2421 }
2422 if (storage->VolumeIdentifier != NULL) {
2423 free(storage->VolumeIdentifier);
2424 }
2425 tmp = storage;
2426 storage = storage->next;
2427 free(tmp);
Linus Walleij9e1b0812006-12-12 19:22:02 +00002428 }
2429 device->storage = NULL;
2430
2431 return;
2432}
2433
2434/**
2435 * This function traverses a devices storage list freeing up the
2436 * strings and the structs.
2437 * @param device a pointer to the MTP device to free the storage
2438 * list for.
2439 */
2440static int sort_storage_by(LIBMTP_mtpdevice_t *device,int const sortby)
2441{
2442 LIBMTP_devicestorage_t *oldhead, *ptr1, *ptr2, *newlist;
2443
2444 if (device->storage == NULL)
2445 return -1;
2446 if (sortby == LIBMTP_STORAGE_SORTBY_NOTSORTED)
2447 return 0;
2448
2449 oldhead = ptr1 = ptr2 = device->storage;
2450
2451 newlist = NULL;
2452
2453 while(oldhead != NULL) {
2454 ptr1 = ptr2 = oldhead;
2455 while(ptr1 != NULL) {
2456
2457 if (sortby == LIBMTP_STORAGE_SORTBY_FREESPACE && ptr1->FreeSpaceInBytes > ptr2->FreeSpaceInBytes)
2458 ptr2 = ptr1;
2459 if (sortby == LIBMTP_STORAGE_SORTBY_MAXSPACE && ptr1->FreeSpaceInBytes > ptr2->FreeSpaceInBytes)
2460 ptr2 = ptr1;
2461
2462 ptr1 = ptr1->next;
2463 }
2464
2465 // Make our previous entries next point to our next
2466 if(ptr2->prev != NULL) {
2467 ptr1 = ptr2->prev;
2468 ptr1->next = ptr2->next;
2469 } else {
2470 oldhead = ptr2->next;
2471 if(oldhead != NULL)
2472 oldhead->prev = NULL;
2473 }
2474
2475 // Make our next entries previous point to our previous
2476 ptr1 = ptr2->next;
2477 if(ptr1 != NULL) {
2478 ptr1->prev = ptr2->prev;
2479 } else {
2480 ptr1 = ptr2->prev;
2481 if(ptr1 != NULL)
2482 ptr1->next = NULL;
2483 }
2484
2485 if(newlist == NULL) {
2486 newlist = ptr2;
2487 newlist->prev = NULL;
2488 } else {
2489 ptr2->prev = newlist;
2490 newlist->next = ptr2;
2491 newlist = newlist->next;
2492 }
2493 }
2494
Linus Walleij5c1499e2009-02-21 06:54:29 +00002495 if (newlist != NULL) {
2496 newlist->next = NULL;
2497 while(newlist->prev != NULL)
2498 newlist = newlist->prev;
2499 device->storage = newlist;
2500 }
Linus Walleij9e1b0812006-12-12 19:22:02 +00002501
2502 return 0;
2503}
2504
2505/**
Linus Walleijd71d0b32008-09-22 08:21:03 +00002506 * This function grabs the first writeable storageid from the
2507 * device storage list.
2508 * @param device a pointer to the MTP device to locate writeable
2509 * storage for.
2510 * @param fitsize a file of this file must fit on the device.
Linus Walleij9e1b0812006-12-12 19:22:02 +00002511 */
Linus Walleijd71d0b32008-09-22 08:21:03 +00002512static uint32_t get_writeable_storageid(LIBMTP_mtpdevice_t *device, uint64_t fitsize)
Linus Walleij9e1b0812006-12-12 19:22:02 +00002513{
Linus Walleij5c1499e2009-02-21 06:54:29 +00002514 LIBMTP_devicestorage_t *storage;
Linus Walleijfb28b632007-10-23 21:56:18 +00002515 uint32_t store = 0x00000000; //Should this be 0xffffffffu instead?
Linus Walleijd71d0b32008-09-22 08:21:03 +00002516 int subcall_ret;
Linus Walleij9e1b0812006-12-12 19:22:02 +00002517
Linus Walleijd71d0b32008-09-22 08:21:03 +00002518 // See if there is some storage we can fit this file on.
2519 storage = device->storage;
2520 if (storage == NULL) {
2521 // Sometimes the storage just cannot be detected.
2522 store = 0x00000000U;
2523 } else {
2524 while(storage != NULL) {
2525 // These storages cannot be used.
2526 if (storage->StorageType == PTP_ST_FixedROM || storage->StorageType == PTP_ST_RemovableROM) {
2527 storage = storage->next;
2528 continue;
2529 }
2530 // Storage IDs with the lower 16 bits 0x0000 are not supposed
2531 // to be writeable.
2532 if ((storage->id & 0x0000FFFFU) == 0x00000000U) {
2533 storage = storage->next;
2534 continue;
2535 }
2536 // Also check the access capability to avoid e.g. deletable only storages
2537 if (storage->AccessCapability == PTP_AC_ReadOnly || storage->AccessCapability == PTP_AC_ReadOnly_with_Object_Deletion) {
2538 storage = storage->next;
2539 continue;
2540 }
2541 // Then see if we can fit the file.
2542 subcall_ret = check_if_file_fits(device, storage, fitsize);
2543 if (subcall_ret != 0) {
2544 storage = storage->next;
2545 } else {
2546 // We found a storage that is writable and can fit the file!
2547 break;
2548 }
2549 }
2550 if (storage == NULL) {
2551 add_error_to_errorstack(device, LIBMTP_ERROR_STORAGE_FULL, "LIBMTP_Send_File_From_File_Descriptor(): "
2552 "all device storage is full or corrupt.");
2553 return -1;
2554 }
Linus Walleij9e1b0812006-12-12 19:22:02 +00002555 store = storage->id;
Linus Walleijd71d0b32008-09-22 08:21:03 +00002556 }
Linus Walleij9e1b0812006-12-12 19:22:02 +00002557
2558 return store;
2559}
2560
2561/**
Linus Walleij6bf68b42007-09-03 22:50:02 +00002562 * This function grabs the freespace from a certain storage in
Linus Walleij9e1b0812006-12-12 19:22:02 +00002563 * device storage list.
2564 * @param device a pointer to the MTP device to free the storage
2565 * list for.
Linus Walleij6bf68b42007-09-03 22:50:02 +00002566 * @param storageid the storage ID for the storage to flush and
2567 * get free space for.
2568 * @param freespace the free space on this storage will be returned
2569 * in this variable.
Linus Walleij9e1b0812006-12-12 19:22:02 +00002570 */
Linus Walleij6bf68b42007-09-03 22:50:02 +00002571static int get_storage_freespace(LIBMTP_mtpdevice_t *device,
2572 LIBMTP_devicestorage_t *storage,
2573 uint64_t *freespace)
Linus Walleij9e1b0812006-12-12 19:22:02 +00002574{
Linus Walleije1ac07e2006-12-14 19:38:59 +00002575 PTPParams *params = (PTPParams *) device->params;
Linus Walleij9e1b0812006-12-12 19:22:02 +00002576
Linus Walleije1ac07e2006-12-14 19:38:59 +00002577 // Always query the device about this, since some models explicitly
Linus Walleij6bf68b42007-09-03 22:50:02 +00002578 // needs that. We flush all data on queries storage here.
Linus Walleije1ac07e2006-12-14 19:38:59 +00002579 if (ptp_operation_issupported(params,PTP_OC_GetStorageInfo)) {
2580 PTPStorageInfo storageInfo;
Linus Walleij070e9b42007-01-22 08:49:28 +00002581 uint16_t ret;
Linus Walleije1ac07e2006-12-14 19:38:59 +00002582
Linus Walleij070e9b42007-01-22 08:49:28 +00002583 ret = ptp_getstorageinfo(params, storage->id, &storageInfo);
2584 if (ret != PTP_RC_OK) {
2585 add_ptp_error_to_errorstack(device, ret, "get_first_storage_freespace(): could not get storage info.");
Linus Walleije1ac07e2006-12-14 19:38:59 +00002586 return -1;
2587 }
2588 if (storage->StorageDescription != NULL) {
2589 free(storage->StorageDescription);
2590 }
2591 if (storage->VolumeIdentifier != NULL) {
2592 free(storage->VolumeIdentifier);
2593 }
2594 storage->StorageType = storageInfo.StorageType;
2595 storage->FilesystemType = storageInfo.FilesystemType;
2596 storage->AccessCapability = storageInfo.AccessCapability;
2597 storage->MaxCapacity = storageInfo.MaxCapability;
2598 storage->FreeSpaceInBytes = storageInfo.FreeSpaceInBytes;
2599 storage->FreeSpaceInObjects = storageInfo.FreeSpaceInImages;
2600 storage->StorageDescription = storageInfo.StorageDescription;
2601 storage->VolumeIdentifier = storageInfo.VolumeLabel;
2602 }
2603 if(storage->FreeSpaceInBytes == (uint64_t) -1)
Linus Walleij9e1b0812006-12-12 19:22:02 +00002604 return -1;
2605 *freespace = storage->FreeSpaceInBytes;
2606 return 0;
2607}
2608
2609/**
Linus Walleij8c45b292006-04-26 14:12:44 +00002610 * This function dumps out a large chunk of textual information
2611 * provided from the PTP protocol and additionally some extra
2612 * MTP-specific information where applicable.
2613 * @param device a pointer to the MTP device to report info from.
2614 */
2615void LIBMTP_Dump_Device_Info(LIBMTP_mtpdevice_t *device)
2616{
2617 int i;
2618 PTPParams *params = (PTPParams *) device->params;
Linus Walleijc6210fb2006-05-08 11:11:41 +00002619 PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo;
Linus Walleije1ac07e2006-12-14 19:38:59 +00002620 LIBMTP_devicestorage_t *storage = device->storage;
mopoke96143402006-10-30 04:37:26 +00002621
Linus Walleijc6210fb2006-05-08 11:11:41 +00002622 printf("USB low-level info:\n");
2623 dump_usbinfo(ptp_usb);
Linus Walleij8c45b292006-04-26 14:12:44 +00002624 /* Print out some verbose information */
2625 printf("Device info:\n");
2626 printf(" Manufacturer: %s\n", params->deviceinfo.Manufacturer);
2627 printf(" Model: %s\n", params->deviceinfo.Model);
2628 printf(" Device version: %s\n", params->deviceinfo.DeviceVersion);
2629 printf(" Serial number: %s\n", params->deviceinfo.SerialNumber);
2630 printf(" Vendor extension ID: 0x%08x\n", params->deviceinfo.VendorExtensionID);
2631 printf(" Vendor extension description: %s\n", params->deviceinfo.VendorExtensionDesc);
Linus Walleijddaba2f2007-10-02 21:20:30 +00002632 printf(" Detected object size: %d bits\n", device->object_bitsize);
Linus Walleij8c45b292006-04-26 14:12:44 +00002633 printf("Supported operations:\n");
2634 for (i=0;i<params->deviceinfo.OperationsSupported_len;i++) {
Linus Walleij4f40d112006-09-21 07:44:53 +00002635 char txt[256];
2636
2637 (void) ptp_render_opcode (params, params->deviceinfo.OperationsSupported[i], sizeof(txt), txt);
2638 printf(" %04x: %s\n", params->deviceinfo.OperationsSupported[i], txt);
Linus Walleij8c45b292006-04-26 14:12:44 +00002639 }
2640 printf("Events supported:\n");
2641 if (params->deviceinfo.EventsSupported_len == 0) {
2642 printf(" None.\n");
2643 } else {
2644 for (i=0;i<params->deviceinfo.EventsSupported_len;i++) {
2645 printf(" 0x%04x\n", params->deviceinfo.EventsSupported[i]);
2646 }
2647 }
2648 printf("Device Properties Supported:\n");
2649 for (i=0;i<params->deviceinfo.DevicePropertiesSupported_len;i++) {
Linus Walleij16c51f02006-05-04 13:20:22 +00002650 char const *propdesc = ptp_get_property_description(params, params->deviceinfo.DevicePropertiesSupported[i]);
mopoke96143402006-10-30 04:37:26 +00002651
Linus Walleij545c7792006-06-13 15:22:30 +00002652 if (propdesc != NULL) {
2653 printf(" 0x%04x: %s\n", params->deviceinfo.DevicePropertiesSupported[i], propdesc);
2654 } else {
2655 uint16_t prop = params->deviceinfo.DevicePropertiesSupported[i];
Linus Walleijcf223e62006-06-19 09:31:53 +00002656 printf(" 0x%04x: Unknown property\n", prop);
Linus Walleij545c7792006-06-13 15:22:30 +00002657 }
Linus Walleij8c45b292006-04-26 14:12:44 +00002658 }
Linus Walleij0af979a2006-06-19 11:49:10 +00002659
2660 if (ptp_operation_issupported(params,PTP_OC_MTP_GetObjectPropsSupported)) {
2661 printf("Playable File (Object) Types and Object Properties Supported:\n");
2662 for (i=0;i<params->deviceinfo.ImageFormats_len;i++) {
2663 char txt[256];
2664 uint16_t ret;
2665 uint16_t *props = NULL;
2666 uint32_t propcnt = 0;
2667 int j;
mopoke96143402006-10-30 04:37:26 +00002668
Linus Walleij0af979a2006-06-19 11:49:10 +00002669 (void) ptp_render_ofc (params, params->deviceinfo.ImageFormats[i], sizeof(txt), txt);
2670 printf(" %04x: %s\n", params->deviceinfo.ImageFormats[i], txt);
mopoke96143402006-10-30 04:37:26 +00002671
Linus Walleij0af979a2006-06-19 11:49:10 +00002672 ret = ptp_mtp_getobjectpropssupported (params, params->deviceinfo.ImageFormats[i], &propcnt, &props);
2673 if (ret != PTP_RC_OK) {
Linus Walleij070e9b42007-01-22 08:49:28 +00002674 add_ptp_error_to_errorstack(device, ret, "LIBMTP_Dump_Device_Info(): error on query for object properties.");
Linus Walleij0af979a2006-06-19 11:49:10 +00002675 } else {
2676 for (j=0;j<propcnt;j++) {
Linus Walleij14830342007-03-23 13:32:00 +00002677 PTPObjectPropDesc opd;
2678 int k;
2679
Richard Low6f070842009-05-03 10:26:16 +00002680 printf(" %04x: %s", props[j], LIBMTP_Get_Property_Description(map_ptp_property_to_libmtp_property(props[j])));
Linus Walleij14830342007-03-23 13:32:00 +00002681 // Get a more verbose description
2682 ret = ptp_mtp_getobjectpropdesc(params, props[j], params->deviceinfo.ImageFormats[i], &opd);
2683 if (ret != PTP_RC_OK) {
2684 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Dump_Device_Info(): "
2685 "could not get property description.");
2686 break;
2687 }
2688
2689 if (opd.DataType == PTP_DTC_STR) {
2690 printf(" STRING data type");
Linus Walleij22cc4872007-11-05 22:57:19 +00002691 switch (opd.FormFlag) {
2692 case PTP_OPFF_DateTime:
2693 printf(" DATETIME FORM");
2694 break;
2695 case PTP_OPFF_RegularExpression:
2696 printf(" REGULAR EXPRESSION FORM");
2697 break;
2698 case PTP_OPFF_LongString:
2699 printf(" LONG STRING FORM");
2700 break;
2701 default:
2702 break;
2703 }
Linus Walleij14830342007-03-23 13:32:00 +00002704 } else {
2705 if (opd.DataType & PTP_DTC_ARRAY_MASK) {
2706 printf(" array of");
2707 }
2708
2709 switch (opd.DataType & (~PTP_DTC_ARRAY_MASK)) {
2710
2711 case PTP_DTC_UNDEF:
2712 printf(" UNDEFINED data type");
2713 break;
2714
2715 case PTP_DTC_INT8:
2716 printf(" INT8 data type");
2717 switch (opd.FormFlag) {
Linus Walleij22cc4872007-11-05 22:57:19 +00002718 case PTP_OPFF_Range:
Linus Walleij14830342007-03-23 13:32:00 +00002719 printf(" range: MIN %d, MAX %d, STEP %d",
2720 opd.FORM.Range.MinimumValue.i8,
2721 opd.FORM.Range.MaximumValue.i8,
2722 opd.FORM.Range.StepSize.i8);
2723 break;
Linus Walleij22cc4872007-11-05 22:57:19 +00002724 case PTP_OPFF_Enumeration:
Linus Walleij14830342007-03-23 13:32:00 +00002725 printf(" enumeration: ");
2726 for(k=0;k<opd.FORM.Enum.NumberOfValues;k++) {
2727 printf("%d, ", opd.FORM.Enum.SupportedValue[k].i8);
2728 }
2729 break;
Linus Walleij22cc4872007-11-05 22:57:19 +00002730 case PTP_OPFF_ByteArray:
2731 printf(" byte array: ");
2732 break;
Linus Walleij14830342007-03-23 13:32:00 +00002733 default:
2734 printf(" ANY 8BIT VALUE form");
2735 break;
2736 }
2737 break;
2738
2739 case PTP_DTC_UINT8:
2740 printf(" UINT8 data type");
2741 switch (opd.FormFlag) {
Linus Walleij22cc4872007-11-05 22:57:19 +00002742 case PTP_OPFF_Range:
Linus Walleij14830342007-03-23 13:32:00 +00002743 printf(" range: MIN %d, MAX %d, STEP %d",
2744 opd.FORM.Range.MinimumValue.u8,
2745 opd.FORM.Range.MaximumValue.u8,
2746 opd.FORM.Range.StepSize.u8);
2747 break;
Linus Walleij22cc4872007-11-05 22:57:19 +00002748 case PTP_OPFF_Enumeration:
Linus Walleij14830342007-03-23 13:32:00 +00002749 printf(" enumeration: ");
2750 for(k=0;k<opd.FORM.Enum.NumberOfValues;k++) {
2751 printf("%d, ", opd.FORM.Enum.SupportedValue[k].u8);
2752 }
2753 break;
Linus Walleij22cc4872007-11-05 22:57:19 +00002754 case PTP_OPFF_ByteArray:
2755 printf(" byte array: ");
2756 break;
Linus Walleij14830342007-03-23 13:32:00 +00002757 default:
2758 printf(" ANY 8BIT VALUE form");
2759 break;
2760 }
2761 break;
2762
2763 case PTP_DTC_INT16:
2764 printf(" INT16 data type");
2765 switch (opd.FormFlag) {
Linus Walleij22cc4872007-11-05 22:57:19 +00002766 case PTP_OPFF_Range:
Linus Walleij14830342007-03-23 13:32:00 +00002767 printf(" range: MIN %d, MAX %d, STEP %d",
2768 opd.FORM.Range.MinimumValue.i16,
2769 opd.FORM.Range.MaximumValue.i16,
2770 opd.FORM.Range.StepSize.i16);
2771 break;
Linus Walleij22cc4872007-11-05 22:57:19 +00002772 case PTP_OPFF_Enumeration:
Linus Walleij14830342007-03-23 13:32:00 +00002773 printf(" enumeration: ");
2774 for(k=0;k<opd.FORM.Enum.NumberOfValues;k++) {
2775 printf("%d, ", opd.FORM.Enum.SupportedValue[k].i16);
2776 }
2777 break;
2778 default:
2779 printf(" ANY 16BIT VALUE form");
2780 break;
2781 }
2782 break;
2783
2784 case PTP_DTC_UINT16:
2785 printf(" UINT16 data type");
2786 switch (opd.FormFlag) {
Linus Walleij22cc4872007-11-05 22:57:19 +00002787 case PTP_OPFF_Range:
Linus Walleij14830342007-03-23 13:32:00 +00002788 printf(" range: MIN %d, MAX %d, STEP %d",
2789 opd.FORM.Range.MinimumValue.u16,
2790 opd.FORM.Range.MaximumValue.u16,
2791 opd.FORM.Range.StepSize.u16);
2792 break;
Linus Walleij22cc4872007-11-05 22:57:19 +00002793 case PTP_OPFF_Enumeration:
Linus Walleij14830342007-03-23 13:32:00 +00002794 printf(" enumeration: ");
2795 for(k=0;k<opd.FORM.Enum.NumberOfValues;k++) {
2796 printf("%d, ", opd.FORM.Enum.SupportedValue[k].u16);
2797 }
2798 break;
2799 default:
2800 printf(" ANY 16BIT VALUE form");
2801 break;
2802 }
2803 break;
2804
2805 case PTP_DTC_INT32:
2806 printf(" INT32 data type");
2807 switch (opd.FormFlag) {
Linus Walleij22cc4872007-11-05 22:57:19 +00002808 case PTP_OPFF_Range:
Linus Walleij14830342007-03-23 13:32:00 +00002809 printf(" range: MIN %d, MAX %d, STEP %d",
2810 opd.FORM.Range.MinimumValue.i32,
2811 opd.FORM.Range.MaximumValue.i32,
2812 opd.FORM.Range.StepSize.i32);
2813 break;
Linus Walleij22cc4872007-11-05 22:57:19 +00002814 case PTP_OPFF_Enumeration:
Linus Walleij14830342007-03-23 13:32:00 +00002815 printf(" enumeration: ");
2816 for(k=0;k<opd.FORM.Enum.NumberOfValues;k++) {
2817 printf("%d, ", opd.FORM.Enum.SupportedValue[k].i32);
2818 }
2819 break;
2820 default:
2821 printf(" ANY 32BIT VALUE form");
2822 break;
2823 }
2824 break;
2825
2826 case PTP_DTC_UINT32:
2827 printf(" UINT32 data type");
2828 switch (opd.FormFlag) {
Linus Walleij22cc4872007-11-05 22:57:19 +00002829 case PTP_OPFF_Range:
Linus Walleij14830342007-03-23 13:32:00 +00002830 printf(" range: MIN %d, MAX %d, STEP %d",
2831 opd.FORM.Range.MinimumValue.u32,
2832 opd.FORM.Range.MaximumValue.u32,
2833 opd.FORM.Range.StepSize.u32);
2834 break;
Linus Walleij22cc4872007-11-05 22:57:19 +00002835 case PTP_OPFF_Enumeration:
Linus Walleijd71d0b32008-09-22 08:21:03 +00002836 // Special pretty-print for FOURCC codes
2837 if (params->deviceinfo.ImageFormats[i] == PTP_OPC_VideoFourCCCodec) {
2838 printf(" enumeration of u32 casted FOURCC: ");
2839 for (k=0;k<opd.FORM.Enum.NumberOfValues;k++) {
2840 if (opd.FORM.Enum.SupportedValue[k].u32 == 0) {
2841 printf("ANY, ");
2842 } else {
2843 char fourcc[6];
2844 fourcc[0] = (opd.FORM.Enum.SupportedValue[k].u32 >> 24) & 0xFFU;
2845 fourcc[1] = (opd.FORM.Enum.SupportedValue[k].u32 >> 16) & 0xFFU;
2846 fourcc[2] = (opd.FORM.Enum.SupportedValue[k].u32 >> 8) & 0xFFU;
2847 fourcc[3] = opd.FORM.Enum.SupportedValue[k].u32 & 0xFFU;
2848 fourcc[4] = '\n';
2849 fourcc[5] = '\0';
2850 printf("\"%s\", ", fourcc);
2851 }
2852 }
2853 } else {
2854 printf(" enumeration: ");
2855 for(k=0;k<opd.FORM.Enum.NumberOfValues;k++) {
2856 printf("%d, ", opd.FORM.Enum.SupportedValue[k].u32);
2857 }
Linus Walleij14830342007-03-23 13:32:00 +00002858 }
2859 break;
2860 default:
2861 printf(" ANY 32BIT VALUE form");
2862 break;
2863 }
2864 break;
2865
2866 case PTP_DTC_INT64:
2867 printf(" INT64 data type");
2868 break;
2869
2870 case PTP_DTC_UINT64:
2871 printf(" UINT64 data type");
2872 break;
2873
2874 case PTP_DTC_INT128:
2875 printf(" INT128 data type");
2876 break;
2877
2878 case PTP_DTC_UINT128:
2879 printf(" UINT128 data type");
2880 break;
2881
2882 default:
2883 printf(" UNKNOWN data type");
2884 break;
2885 }
2886 }
2887 if (opd.GetSet) {
2888 printf(" GET/SET");
2889 } else {
2890 printf(" READ ONLY");
2891 }
2892 printf("\n");
Linus Walleijdbcc8242007-08-05 22:31:26 +00002893 ptp_free_objectpropdesc(&opd);
Linus Walleij0af979a2006-06-19 11:49:10 +00002894 }
2895 free(props);
2896 }
2897 }
2898 }
mopoke96143402006-10-30 04:37:26 +00002899
Linus Walleije1ac07e2006-12-14 19:38:59 +00002900 if(storage != NULL && ptp_operation_issupported(params,PTP_OC_GetStorageInfo)) {
Linus Walleij9e1b0812006-12-12 19:22:02 +00002901 printf("Storage Devices:\n");
Linus Walleije1ac07e2006-12-14 19:38:59 +00002902 while(storage != NULL) {
2903 printf(" StorageID: 0x%08x\n",storage->id);
Linus Walleijd71d0b32008-09-22 08:21:03 +00002904 printf(" StorageType: 0x%04x ",storage->StorageType);
2905 switch (storage->StorageType) {
2906 case PTP_ST_Undefined:
2907 printf("(undefined)\n");
2908 break;
2909 case PTP_ST_FixedROM:
2910 printf("fixed ROM storage\n");
2911 break;
2912 case PTP_ST_RemovableROM:
2913 printf("removable ROM storage\n");
2914 break;
2915 case PTP_ST_FixedRAM:
2916 printf("fixed RAM storage\n");
2917 break;
2918 case PTP_ST_RemovableRAM:
2919 printf("removable RAM storage\n");
2920 break;
2921 default:
2922 printf("UNKNOWN storage\n");
2923 break;
2924 }
2925 printf(" FilesystemType: 0x%04x ",storage->FilesystemType);
2926 switch(storage->FilesystemType) {
2927 case PTP_FST_Undefined:
2928 printf("(undefined)\n");
2929 break;
2930 case PTP_FST_GenericFlat:
2931 printf("generic flat filesystem\n");
2932 break;
2933 case PTP_FST_GenericHierarchical:
2934 printf("generic hierarchical\n");
2935 break;
2936 case PTP_FST_DCF:
2937 printf("DCF\n");
2938 break;
2939 default:
2940 printf("UNKNONWN filesystem type\n");
2941 break;
2942 }
2943 printf(" AccessCapability: 0x%04x ",storage->AccessCapability);
2944 switch(storage->AccessCapability) {
2945 case PTP_AC_ReadWrite:
2946 printf("read/write\n");
2947 break;
2948 case PTP_AC_ReadOnly:
2949 printf("read only\n");
2950 break;
2951 case PTP_AC_ReadOnly_with_Object_Deletion:
2952 printf("read only + object deletion\n");
2953 break;
2954 default:
2955 printf("UNKNOWN access capability\n");
2956 break;
2957 }
Linus Walleijfec4d562008-06-01 22:30:36 +00002958 printf(" MaxCapacity: %llu\n", (long long unsigned int) storage->MaxCapacity);
2959 printf(" FreeSpaceInBytes: %llu\n", (long long unsigned int) storage->FreeSpaceInBytes);
2960 printf(" FreeSpaceInObjects: %llu\n", (long long unsigned int) storage->FreeSpaceInObjects);
Linus Walleije1ac07e2006-12-14 19:38:59 +00002961 printf(" StorageDescription: %s\n",storage->StorageDescription);
2962 printf(" VolumeIdentifier: %s\n",storage->VolumeIdentifier);
2963 storage = storage->next;
Linus Walleij9e1b0812006-12-12 19:22:02 +00002964 }
2965 }
2966
Linus Walleij545c7792006-06-13 15:22:30 +00002967 printf("Special directories:\n");
2968 printf(" Default music folder: 0x%08x\n", device->default_music_folder);
2969 printf(" Default playlist folder: 0x%08x\n", device->default_playlist_folder);
2970 printf(" Default picture folder: 0x%08x\n", device->default_picture_folder);
2971 printf(" Default video folder: 0x%08x\n", device->default_video_folder);
2972 printf(" Default organizer folder: 0x%08x\n", device->default_organizer_folder);
2973 printf(" Default zencast folder: 0x%08x\n", device->default_zencast_folder);
Linus Walleijccf28ce2006-11-16 16:06:38 +00002974 printf(" Default album folder: 0x%08x\n", device->default_album_folder);
Linus Walleij9316e652006-12-07 09:55:21 +00002975 printf(" Default text folder: 0x%08x\n", device->default_text_folder);
Linus Walleij8c45b292006-04-26 14:12:44 +00002976}
2977
2978/**
Linus Walleij5d533bb2007-07-17 21:48:57 +00002979 * This resets a device in case it supports the <code>PTP_OC_ResetDevice</code>
2980 * operation code (0x1010).
2981 * @param device a pointer to the device to reset.
2982 * @return 0 on success, any other value means failure.
2983 */
2984int LIBMTP_Reset_Device(LIBMTP_mtpdevice_t *device)
2985{
2986 PTPParams *params = (PTPParams *) device->params;
2987 uint16_t ret;
2988
2989 if (!ptp_operation_issupported(params,PTP_OC_ResetDevice)) {
2990 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL,
2991 "LIBMTP_Reset_Device(): device does not support resetting.");
2992 return -1;
2993 }
2994 ret = ptp_resetdevice(params);
2995 if (ret != PTP_RC_OK) {
2996 add_ptp_error_to_errorstack(device, ret, "Error resetting.");
2997 return -1;
2998 }
2999 return 0;
3000}
3001
3002/**
Linus Walleij2350b712008-01-14 22:54:37 +00003003 * This retrieves the manufacturer name of an MTP device.
3004 * @param device a pointer to the device to get the manufacturer name for.
3005 * @return a newly allocated UTF-8 string representing the manufacturer name.
3006 * The string must be freed by the caller after use. If the call
3007 * was unsuccessful this will contain NULL.
3008 */
3009char *LIBMTP_Get_Manufacturername(LIBMTP_mtpdevice_t *device)
3010{
3011 char *retmanuf = NULL;
3012 PTPParams *params = (PTPParams *) device->params;
3013
3014 if (params->deviceinfo.Manufacturer != NULL) {
3015 retmanuf = strdup(params->deviceinfo.Manufacturer);
3016 }
3017 return retmanuf;
3018}
3019
3020/**
mopoke96143402006-10-30 04:37:26 +00003021 * This retrieves the model name (often equal to product name)
Linus Walleij80124062006-03-15 10:26:09 +00003022 * of an MTP device.
3023 * @param device a pointer to the device to get the model name for.
3024 * @return a newly allocated UTF-8 string representing the model name.
3025 * The string must be freed by the caller after use. If the call
3026 * was unsuccessful this will contain NULL.
3027 */
3028char *LIBMTP_Get_Modelname(LIBMTP_mtpdevice_t *device)
3029{
3030 char *retmodel = NULL;
Linus Walleij9b28da32006-03-16 13:47:58 +00003031 PTPParams *params = (PTPParams *) device->params;
mopoke96143402006-10-30 04:37:26 +00003032
Linus Walleij9b28da32006-03-16 13:47:58 +00003033 if (params->deviceinfo.Model != NULL) {
3034 retmodel = strdup(params->deviceinfo.Model);
Linus Walleij80124062006-03-15 10:26:09 +00003035 }
3036 return retmodel;
3037}
3038
3039/**
3040 * This retrieves the serial number of an MTP device.
3041 * @param device a pointer to the device to get the serial number for.
3042 * @return a newly allocated UTF-8 string representing the serial number.
3043 * The string must be freed by the caller after use. If the call
3044 * was unsuccessful this will contain NULL.
3045 */
3046char *LIBMTP_Get_Serialnumber(LIBMTP_mtpdevice_t *device)
3047{
3048 char *retnumber = NULL;
Linus Walleij9b28da32006-03-16 13:47:58 +00003049 PTPParams *params = (PTPParams *) device->params;
mopoke96143402006-10-30 04:37:26 +00003050
Linus Walleij9b28da32006-03-16 13:47:58 +00003051 if (params->deviceinfo.SerialNumber != NULL) {
3052 retnumber = strdup(params->deviceinfo.SerialNumber);
Linus Walleij80124062006-03-15 10:26:09 +00003053 }
3054 return retnumber;
3055}
3056
3057/**
mopoke96143402006-10-30 04:37:26 +00003058 * This retrieves the device version (hardware and firmware version) of an
Linus Walleij80124062006-03-15 10:26:09 +00003059 * MTP device.
3060 * @param device a pointer to the device to get the device version for.
3061 * @return a newly allocated UTF-8 string representing the device version.
3062 * The string must be freed by the caller after use. If the call
3063 * was unsuccessful this will contain NULL.
3064 */
3065char *LIBMTP_Get_Deviceversion(LIBMTP_mtpdevice_t *device)
3066{
3067 char *retversion = NULL;
Linus Walleij9b28da32006-03-16 13:47:58 +00003068 PTPParams *params = (PTPParams *) device->params;
mopoke96143402006-10-30 04:37:26 +00003069
Linus Walleij9b28da32006-03-16 13:47:58 +00003070 if (params->deviceinfo.DeviceVersion != NULL) {
3071 retversion = strdup(params->deviceinfo.DeviceVersion);
Linus Walleij80124062006-03-15 10:26:09 +00003072 }
3073 return retversion;
3074}
3075
3076
3077/**
Linus Walleijfae27482006-08-19 20:13:25 +00003078 * This retrieves the "friendly name" of an MTP device. Usually
3079 * this is simply the name of the owner or something like
Linus Walleij30658792006-08-19 22:18:55 +00003080 * "John Doe's Digital Audio Player". This property should be supported
Linus Walleijfae27482006-08-19 20:13:25 +00003081 * by all MTP devices.
3082 * @param device a pointer to the device to get the friendly name for.
mopoke96143402006-10-30 04:37:26 +00003083 * @return a newly allocated UTF-8 string representing the friendly name.
Linus Walleijb9256fd2006-02-15 09:40:43 +00003084 * The string must be freed by the caller after use.
Linus Walleij30658792006-08-19 22:18:55 +00003085 * @see LIBMTP_Set_Friendlyname()
Linus Walleijb9256fd2006-02-15 09:40:43 +00003086 */
Linus Walleij30658792006-08-19 22:18:55 +00003087char *LIBMTP_Get_Friendlyname(LIBMTP_mtpdevice_t *device)
Linus Walleijb9256fd2006-02-15 09:40:43 +00003088{
Linus Walleijb02a0662006-04-25 08:05:09 +00003089 PTPPropertyValue propval;
Linus Walleijb9256fd2006-02-15 09:40:43 +00003090 char *retstring = NULL;
Linus Walleij9b28da32006-03-16 13:47:58 +00003091 PTPParams *params = (PTPParams *) device->params;
Linus Walleij070e9b42007-01-22 08:49:28 +00003092 uint16_t ret;
Linus Walleijb9256fd2006-02-15 09:40:43 +00003093
Linus Walleijcf223e62006-06-19 09:31:53 +00003094 if (!ptp_property_issupported(params, PTP_DPC_MTP_DeviceFriendlyName)) {
3095 return NULL;
3096 }
3097
Linus Walleij070e9b42007-01-22 08:49:28 +00003098 ret = ptp_getdevicepropvalue(params,
3099 PTP_DPC_MTP_DeviceFriendlyName,
3100 &propval,
3101 PTP_DTC_STR);
3102 if (ret != PTP_RC_OK) {
3103 add_ptp_error_to_errorstack(device, ret, "Error getting friendlyname.");
Linus Walleijb9256fd2006-02-15 09:40:43 +00003104 return NULL;
3105 }
Linus Walleija823a702006-08-27 21:27:46 +00003106 if (propval.str != NULL) {
3107 retstring = strdup(propval.str);
3108 free(propval.str);
3109 }
Linus Walleijfae27482006-08-19 20:13:25 +00003110 return retstring;
3111}
3112
3113/**
Linus Walleij30658792006-08-19 22:18:55 +00003114 * Sets the "friendly name" of an MTP device.
3115 * @param device a pointer to the device to set the friendly name for.
3116 * @param friendlyname the new friendly name for the device.
3117 * @return 0 on success, any other value means failure.
Linus Walleijd5b34972008-09-24 20:24:12 +00003118 * @see LIBMTP_Get_Friendlyname()
Linus Walleij30658792006-08-19 22:18:55 +00003119 */
3120int LIBMTP_Set_Friendlyname(LIBMTP_mtpdevice_t *device,
3121 char const * const friendlyname)
3122{
3123 PTPPropertyValue propval;
3124 PTPParams *params = (PTPParams *) device->params;
Linus Walleij070e9b42007-01-22 08:49:28 +00003125 uint16_t ret;
Linus Walleij30658792006-08-19 22:18:55 +00003126
3127 if (!ptp_property_issupported(params, PTP_DPC_MTP_DeviceFriendlyName)) {
3128 return -1;
3129 }
Linus Walleija823a702006-08-27 21:27:46 +00003130 propval.str = (char *) friendlyname;
Linus Walleij070e9b42007-01-22 08:49:28 +00003131 ret = ptp_setdevicepropvalue(params,
3132 PTP_DPC_MTP_DeviceFriendlyName,
3133 &propval,
3134 PTP_DTC_STR);
3135 if (ret != PTP_RC_OK) {
3136 add_ptp_error_to_errorstack(device, ret, "Error setting friendlyname.");
Linus Walleij30658792006-08-19 22:18:55 +00003137 return -1;
3138 }
Linus Walleij30658792006-08-19 22:18:55 +00003139 return 0;
3140}
3141
3142/**
Linus Walleijfae27482006-08-19 20:13:25 +00003143 * This retrieves the syncronization partner of an MTP device. This
3144 * property should be supported by all MTP devices.
3145 * @param device a pointer to the device to get the sync partner for.
3146 * @return a newly allocated UTF-8 string representing the synchronization
3147 * partner. The string must be freed by the caller after use.
Linus Walleij30658792006-08-19 22:18:55 +00003148 * @see LIBMTP_Set_Syncpartner()
Linus Walleijfae27482006-08-19 20:13:25 +00003149 */
3150char *LIBMTP_Get_Syncpartner(LIBMTP_mtpdevice_t *device)
3151{
3152 PTPPropertyValue propval;
3153 char *retstring = NULL;
3154 PTPParams *params = (PTPParams *) device->params;
Linus Walleij070e9b42007-01-22 08:49:28 +00003155 uint16_t ret;
Linus Walleijfae27482006-08-19 20:13:25 +00003156
3157 if (!ptp_property_issupported(params, PTP_DPC_MTP_SynchronizationPartner)) {
3158 return NULL;
3159 }
3160
Linus Walleij070e9b42007-01-22 08:49:28 +00003161 ret = ptp_getdevicepropvalue(params,
3162 PTP_DPC_MTP_SynchronizationPartner,
3163 &propval,
3164 PTP_DTC_STR);
3165 if (ret != PTP_RC_OK) {
3166 add_ptp_error_to_errorstack(device, ret, "Error getting syncpartner.");
Linus Walleijfae27482006-08-19 20:13:25 +00003167 return NULL;
3168 }
Linus Walleija823a702006-08-27 21:27:46 +00003169 if (propval.str != NULL) {
3170 retstring = strdup(propval.str);
3171 free(propval.str);
3172 }
Linus Walleijb9256fd2006-02-15 09:40:43 +00003173 return retstring;
3174}
3175
Linus Walleij30658792006-08-19 22:18:55 +00003176
3177/**
3178 * Sets the synchronization partner of an MTP device. Note that
3179 * we have no idea what the effect of setting this to "foobar"
3180 * may be. But the general idea seems to be to tell which program
3181 * shall synchronize with this device and tell others to leave
3182 * it alone.
3183 * @param device a pointer to the device to set the sync partner for.
3184 * @param syncpartner the new synchronization partner for the device.
3185 * @return 0 on success, any other value means failure.
3186 * @see LIBMTP_Get_Syncpartner()
3187 */
3188int LIBMTP_Set_Syncpartner(LIBMTP_mtpdevice_t *device,
3189 char const * const syncpartner)
3190{
3191 PTPPropertyValue propval;
3192 PTPParams *params = (PTPParams *) device->params;
Linus Walleij070e9b42007-01-22 08:49:28 +00003193 uint16_t ret;
mopoke96143402006-10-30 04:37:26 +00003194
Linus Walleij30658792006-08-19 22:18:55 +00003195 if (!ptp_property_issupported(params, PTP_DPC_MTP_SynchronizationPartner)) {
3196 return -1;
3197 }
Linus Walleija823a702006-08-27 21:27:46 +00003198 propval.str = (char *) syncpartner;
Linus Walleij070e9b42007-01-22 08:49:28 +00003199 ret = ptp_setdevicepropvalue(params,
3200 PTP_DPC_MTP_SynchronizationPartner,
3201 &propval,
3202 PTP_DTC_STR);
3203 if (ret != PTP_RC_OK) {
3204 add_ptp_error_to_errorstack(device, ret, "Error setting syncpartner.");
Linus Walleij30658792006-08-19 22:18:55 +00003205 return -1;
3206 }
Linus Walleij30658792006-08-19 22:18:55 +00003207 return 0;
3208}
3209
Linus Walleij394bbbe2006-02-22 16:10:53 +00003210/**
Linus Walleijf5fcda32006-12-03 22:31:02 +00003211 * Checks if the device can stora a file of this size or
3212 * if it's too big.
3213 * @param device a pointer to the device.
3214 * @param filesize the size of the file to check whether it will fit.
Linus Walleij6bf68b42007-09-03 22:50:02 +00003215 * @param storageid the ID of the storage to try to fit the file on.
Linus Walleijf5fcda32006-12-03 22:31:02 +00003216 * @return 0 if the file fits, any other value means failure.
3217 */
Linus Walleij6bf68b42007-09-03 22:50:02 +00003218static int check_if_file_fits(LIBMTP_mtpdevice_t *device,
3219 LIBMTP_devicestorage_t *storage,
3220 uint64_t const filesize) {
Linus Walleijf5fcda32006-12-03 22:31:02 +00003221 PTPParams *params = (PTPParams *) device->params;
Linus Walleijf5fcda32006-12-03 22:31:02 +00003222 uint64_t freebytes;
Linus Walleijf5fcda32006-12-03 22:31:02 +00003223 int ret;
3224
3225 // If we cannot check the storage, no big deal.
3226 if (!ptp_operation_issupported(params,PTP_OC_GetStorageInfo)) {
3227 return 0;
3228 }
3229
Linus Walleij6bf68b42007-09-03 22:50:02 +00003230 ret = get_storage_freespace(device, storage, &freebytes);
Linus Walleijf5fcda32006-12-03 22:31:02 +00003231 if (ret != 0) {
Linus Walleij6bf68b42007-09-03 22:50:02 +00003232 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL,
3233 "check_if_file_fits(): error checking free storage.");
Linus Walleijf5fcda32006-12-03 22:31:02 +00003234 return -1;
3235 } else {
Linus Walleijfb28b632007-10-23 21:56:18 +00003236 // See if it fits.
Linus Walleijf5fcda32006-12-03 22:31:02 +00003237 if (filesize > freebytes) {
Linus Walleijf5fcda32006-12-03 22:31:02 +00003238 return -1;
3239 }
3240 }
3241 return 0;
3242}
3243
3244
Linus Walleijf5fcda32006-12-03 22:31:02 +00003245/**
Linus Walleijfa1374c2006-02-27 07:41:46 +00003246 * This function retrieves the current battery level on the device.
3247 * @param device a pointer to the device to get the battery level for.
mopoke96143402006-10-30 04:37:26 +00003248 * @param maximum_level a pointer to a variable that will hold the
Linus Walleijfa1374c2006-02-27 07:41:46 +00003249 * maximum level of the battery if the call was successful.
mopoke96143402006-10-30 04:37:26 +00003250 * @param current_level a pointer to a variable that will hold the
Linus Walleijfa1374c2006-02-27 07:41:46 +00003251 * current level of the battery if the call was successful.
Linus Walleij545c7792006-06-13 15:22:30 +00003252 * A value of 0 means that the device is on external power.
Linus Walleijfa1374c2006-02-27 07:41:46 +00003253 * @return 0 if the storage info was successfully retrieved, any other
Linus Walleij80439342006-09-12 10:42:26 +00003254 * means failure. A typical cause of failure is that
Linus Walleij545c7792006-06-13 15:22:30 +00003255 * the device does not support the battery level property.
Linus Walleijfa1374c2006-02-27 07:41:46 +00003256 */
mopoke96143402006-10-30 04:37:26 +00003257int LIBMTP_Get_Batterylevel(LIBMTP_mtpdevice_t *device,
3258 uint8_t * const maximum_level,
Linus Walleijfa1374c2006-02-27 07:41:46 +00003259 uint8_t * const current_level)
3260{
Linus Walleijb02a0662006-04-25 08:05:09 +00003261 PTPPropertyValue propval;
Linus Walleijfa1374c2006-02-27 07:41:46 +00003262 uint16_t ret;
Linus Walleij9b28da32006-03-16 13:47:58 +00003263 PTPParams *params = (PTPParams *) device->params;
Linus Walleij4096c882009-03-16 23:32:34 +00003264 PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo;
Linus Walleijfa1374c2006-02-27 07:41:46 +00003265
Linus Walleij545c7792006-06-13 15:22:30 +00003266 *maximum_level = 0;
3267 *current_level = 0;
3268
Linus Walleij4096c882009-03-16 23:32:34 +00003269 if (FLAG_BROKEN_BATTERY_LEVEL(ptp_usb) ||
3270 !ptp_property_issupported(params, PTP_DPC_BatteryLevel)) {
Linus Walleij545c7792006-06-13 15:22:30 +00003271 return -1;
3272 }
mopoke96143402006-10-30 04:37:26 +00003273
Linus Walleijb02a0662006-04-25 08:05:09 +00003274 ret = ptp_getdevicepropvalue(params, PTP_DPC_BatteryLevel, &propval, PTP_DTC_UINT8);
3275 if (ret != PTP_RC_OK) {
Linus Walleij070e9b42007-01-22 08:49:28 +00003276 add_ptp_error_to_errorstack(device, ret, "LIBMTP_Get_Batterylevel(): could not get device property value.");
Linus Walleijfa1374c2006-02-27 07:41:46 +00003277 return -1;
3278 }
mopoke96143402006-10-30 04:37:26 +00003279
Linus Walleijfa1374c2006-02-27 07:41:46 +00003280 *maximum_level = device->maximum_battery_level;
Linus Walleijb02a0662006-04-25 08:05:09 +00003281 *current_level = propval.u8;
mopoke96143402006-10-30 04:37:26 +00003282
Linus Walleijfa1374c2006-02-27 07:41:46 +00003283 return 0;
3284}
3285
Linus Walleij13374a42006-09-13 11:55:30 +00003286
3287/**
3288 * Formats device storage (if the device supports the operation).
3289 * WARNING: This WILL delete all data from the device. Make sure you've
3290 * got confirmation from the user BEFORE you call this function.
3291 *
Linus Walleijf8491912006-12-15 10:23:30 +00003292 * @param device a pointer to the device containing the storage to format.
3293 * @param storage the actual storage to format.
Linus Walleij13374a42006-09-13 11:55:30 +00003294 * @return 0 on success, any other value means failure.
3295 */
Linus Walleijf8491912006-12-15 10:23:30 +00003296int LIBMTP_Format_Storage(LIBMTP_mtpdevice_t *device, LIBMTP_devicestorage_t *storage)
Linus Walleij13374a42006-09-13 11:55:30 +00003297{
3298 uint16_t ret;
3299 PTPParams *params = (PTPParams *) device->params;
mopoke96143402006-10-30 04:37:26 +00003300
Linus Walleij13374a42006-09-13 11:55:30 +00003301 if (!ptp_operation_issupported(params,PTP_OC_FormatStore)) {
Linus Walleij5d533bb2007-07-17 21:48:57 +00003302 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL,
3303 "LIBMTP_Format_Storage(): device does not support formatting storage.");
Linus Walleij13374a42006-09-13 11:55:30 +00003304 return -1;
3305 }
Linus Walleijf8491912006-12-15 10:23:30 +00003306 ret = ptp_formatstore(params, storage->id);
Linus Walleij13374a42006-09-13 11:55:30 +00003307 if (ret != PTP_RC_OK) {
Linus Walleij070e9b42007-01-22 08:49:28 +00003308 add_ptp_error_to_errorstack(device, ret, "LIBMTP_Format_Storage(): failed to format storage.");
Linus Walleij13374a42006-09-13 11:55:30 +00003309 return -1;
3310 }
3311 return 0;
3312}
3313
Linus Walleijfa1374c2006-02-27 07:41:46 +00003314/**
Linus Walleij545c7792006-06-13 15:22:30 +00003315 * Helper function to extract a unicode property off a device.
Linus Walleije46f12e2006-06-22 17:53:25 +00003316 * This is the standard way of retrieveing unicode device
3317 * properties as described by the PTP spec.
Linus Walleijcf223e62006-06-19 09:31:53 +00003318 * @param device a pointer to the device to get the property from.
mopoke96143402006-10-30 04:37:26 +00003319 * @param unicstring a pointer to a pointer that will hold the
Linus Walleijcf223e62006-06-19 09:31:53 +00003320 * property after this call is completed.
3321 * @param property the property to retrieve.
3322 * @return 0 on success, any other value means failure.
Linus Walleij545c7792006-06-13 15:22:30 +00003323 */
mopoke96143402006-10-30 04:37:26 +00003324static int get_device_unicode_property(LIBMTP_mtpdevice_t *device,
Linus Walleijcf223e62006-06-19 09:31:53 +00003325 char **unicstring, uint16_t property)
Linus Walleij545c7792006-06-13 15:22:30 +00003326{
3327 PTPPropertyValue propval;
3328 PTPParams *params = (PTPParams *) device->params;
Linus Walleij16571dc2006-08-17 20:27:46 +00003329 uint16_t *tmp;
Linus Walleij070e9b42007-01-22 08:49:28 +00003330 uint16_t ret;
Linus Walleij545c7792006-06-13 15:22:30 +00003331 int i;
3332
3333 if (!ptp_property_issupported(params, property)) {
3334 return -1;
3335 }
3336
Linus Walleijcf223e62006-06-19 09:31:53 +00003337 // Unicode strings are 16bit unsigned integer arrays.
Linus Walleij070e9b42007-01-22 08:49:28 +00003338 ret = ptp_getdevicepropvalue(params,
3339 property,
3340 &propval,
3341 PTP_DTC_AUINT16);
3342 if (ret != PTP_RC_OK) {
3343 // TODO: add a note on WHICH property that we failed to get.
tedbullock4e51cb92007-02-15 11:48:34 +00003344 *unicstring = NULL;
Linus Walleij070e9b42007-01-22 08:49:28 +00003345 add_ptp_error_to_errorstack(device, ret, "get_device_unicode_property(): failed to get unicode property.");
Linus Walleij545c7792006-06-13 15:22:30 +00003346 return -1;
3347 }
3348
3349 // Extract the actual array.
Linus Walleij16571dc2006-08-17 20:27:46 +00003350 // printf("Array of %d elements\n", propval.a.count);
3351 tmp = malloc((propval.a.count + 1)*sizeof(uint16_t));
Linus Walleij545c7792006-06-13 15:22:30 +00003352 for (i = 0; i < propval.a.count; i++) {
Linus Walleij16571dc2006-08-17 20:27:46 +00003353 tmp[i] = propval.a.v[i].u16;
3354 // printf("%04x ", tmp[i]);
Linus Walleij545c7792006-06-13 15:22:30 +00003355 }
Linus Walleij16571dc2006-08-17 20:27:46 +00003356 tmp[propval.a.count] = 0x0000U;
Linus Walleij545c7792006-06-13 15:22:30 +00003357 free(propval.a.v);
3358
Linus Walleij3ec86312006-08-21 13:25:24 +00003359 *unicstring = utf16_to_utf8(device, tmp);
Linus Walleij16571dc2006-08-17 20:27:46 +00003360
Linus Walleij545c7792006-06-13 15:22:30 +00003361 free(tmp);
3362
3363 return 0;
3364}
3365
3366/**
3367 * This function returns the secure time as an XML document string from
3368 * the device.
3369 * @param device a pointer to the device to get the secure time for.
3370 * @param sectime the secure time string as an XML document or NULL if the call
3371 * failed or the secure time property is not supported. This string
3372 * must be <code>free()</code>:ed by the caller after use.
3373 * @return 0 on success, any other value means failure.
3374 */
Linus Walleij8ab54262006-06-21 07:12:28 +00003375int LIBMTP_Get_Secure_Time(LIBMTP_mtpdevice_t *device, char ** const sectime)
Linus Walleij545c7792006-06-13 15:22:30 +00003376{
3377 return get_device_unicode_property(device, sectime, PTP_DPC_MTP_SecureTime);
3378}
3379
3380/**
mopoke96143402006-10-30 04:37:26 +00003381 * This function returns the device (public key) certificate as an
Linus Walleij545c7792006-06-13 15:22:30 +00003382 * XML document string from the device.
3383 * @param device a pointer to the device to get the device certificate for.
3384 * @param devcert the device certificate as an XML string or NULL if the call
3385 * failed or the device certificate property is not supported. This
3386 * string must be <code>free()</code>:ed by the caller after use.
3387 * @return 0 on success, any other value means failure.
3388 */
Linus Walleij8ab54262006-06-21 07:12:28 +00003389int LIBMTP_Get_Device_Certificate(LIBMTP_mtpdevice_t *device, char ** const devcert)
Linus Walleij545c7792006-06-13 15:22:30 +00003390{
3391 return get_device_unicode_property(device, devcert, PTP_DPC_MTP_DeviceCertificate);
3392}
3393
3394/**
Linus Walleij8ab54262006-06-21 07:12:28 +00003395 * This function retrieves a list of supported file types, i.e. the file
3396 * types that this device claims it supports, e.g. audio file types that
3397 * the device can play etc. This list is mitigated to
3398 * inlcude the file types that libmtp can handle, i.e. it will not list
3399 * filetypes that libmtp will handle internally like playlists and folders.
3400 * @param device a pointer to the device to get the filetype capabilities for.
3401 * @param filetypes a pointer to a pointer that will hold the list of
3402 * supported filetypes if the call was successful. This list must
3403 * be <code>free()</code>:ed by the caller after use.
3404 * @param length a pointer to a variable that will hold the length of the
3405 * list of supported filetypes if the call was successful.
3406 * @return 0 on success, any other value means failure.
3407 * @see LIBMTP_Get_Filetype_Description()
3408 */
mopoke96143402006-10-30 04:37:26 +00003409int LIBMTP_Get_Supported_Filetypes(LIBMTP_mtpdevice_t *device, uint16_t ** const filetypes,
Linus Walleij8ab54262006-06-21 07:12:28 +00003410 uint16_t * const length)
3411{
3412 PTPParams *params = (PTPParams *) device->params;
Linus Walleija3544f62007-11-30 01:20:04 +00003413 PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo;
Linus Walleij8ab54262006-06-21 07:12:28 +00003414 uint16_t *localtypes;
3415 uint16_t localtypelen;
3416 uint32_t i;
mopoke96143402006-10-30 04:37:26 +00003417
Linus Walleij8ab54262006-06-21 07:12:28 +00003418 // This is more memory than needed if there are unknown types, but what the heck.
3419 localtypes = (uint16_t *) malloc(params->deviceinfo.ImageFormats_len * sizeof(uint16_t));
3420 localtypelen = 0;
mopoke96143402006-10-30 04:37:26 +00003421
Linus Walleij8ab54262006-06-21 07:12:28 +00003422 for (i=0;i<params->deviceinfo.ImageFormats_len;i++) {
3423 uint16_t localtype = map_ptp_type_to_libmtp_type(params->deviceinfo.ImageFormats[i]);
3424 if (localtype != LIBMTP_FILETYPE_UNKNOWN) {
3425 localtypes[localtypelen] = localtype;
3426 localtypelen++;
3427 }
3428 }
Linus Walleija3544f62007-11-30 01:20:04 +00003429 // The forgotten Ogg support on YP-10 and others...
Linus Walleijfec4d562008-06-01 22:30:36 +00003430 if (FLAG_OGG_IS_UNKNOWN(ptp_usb)) {
Linus Walleija3544f62007-11-30 01:20:04 +00003431 localtypes = (uint16_t *) realloc(localtypes, (params->deviceinfo.ImageFormats_len+1) * sizeof(uint16_t));
3432 localtypes[localtypelen] = LIBMTP_FILETYPE_OGG;
3433 localtypelen++;
3434 }
Linus Walleij89bb1cd2009-07-24 21:03:36 +00003435 // The forgotten FLAC support on Cowon iAudio S9 and others...
3436 if (FLAG_FLAC_IS_UNKNOWN(ptp_usb)) {
3437 localtypes = (uint16_t *) realloc(localtypes, (params->deviceinfo.ImageFormats_len+1) * sizeof(uint16_t));
3438 localtypes[localtypelen] = LIBMTP_FILETYPE_FLAC;
3439 localtypelen++;
3440 }
Linus Walleij8ab54262006-06-21 07:12:28 +00003441
3442 *filetypes = localtypes;
3443 *length = localtypelen;
3444
3445 return 0;
3446}
3447
raveloxd9a28642006-05-26 23:42:22 +00003448/**
Linus Walleij5b452bd2007-12-28 23:34:18 +00003449 * This function updates all the storage id's of a device and their
3450 * properties, then creates a linked list and puts the list head into
Linus Walleijf8491912006-12-15 10:23:30 +00003451 * the device struct. It also optionally sorts this list. If you want
3452 * to display storage information in your application you should call
Linus Walleij5b452bd2007-12-28 23:34:18 +00003453 * this function, then dereference the device struct
3454 * (<code>device-&gt;storage</code>) to get out information on the storage.
3455 *
3456 * You need to call this everytime you want to update the
3457 * <code>device-&gt;storage</code> list, for example anytime you need
3458 * to check available storage somewhere.
3459 *
3460 * <b>WARNING:</b> since this list is dynamically updated, do not
3461 * reference its fields in external applications by pointer! E.g
3462 * do not put a reference to any <code>char *</code> field. instead
3463 * <code>strncpy()</code> it!
Linus Walleijf8491912006-12-15 10:23:30 +00003464 *
Linus Walleije1b88e82008-07-02 20:12:53 +00003465 * @param device a pointer to the device to get the storage for.
Linus Walleij9e1b0812006-12-12 19:22:02 +00003466 * @param sortby an integer that determines the sorting of the storage list.
3467 * Valid sort methods are defined in libmtp.h with beginning with
3468 * LIBMTP_STORAGE_SORTBY_. 0 or LIBMTP_STORAGE_SORTBY_NOTSORTED to not
3469 * sort.
3470 * @return 0 on success, 1 success but only with storage id's, storage
3471 * properities could not be retrieved and -1 means failure.
Linus Walleij9e1b0812006-12-12 19:22:02 +00003472 */
3473int LIBMTP_Get_Storage(LIBMTP_mtpdevice_t *device, int const sortby)
3474{
3475 uint32_t i = 0;
3476 PTPStorageInfo storageInfo;
3477 PTPParams *params = (PTPParams *) device->params;
3478 PTPStorageIDs storageIDs;
Linus Walleije1ac07e2006-12-14 19:38:59 +00003479 LIBMTP_devicestorage_t *storage = NULL;
3480 LIBMTP_devicestorage_t *storageprev = NULL;
Linus Walleij9e1b0812006-12-12 19:22:02 +00003481
3482 if (device->storage != NULL)
3483 free_storage_list(device);
3484
3485 // if (!ptp_operation_issupported(params,PTP_OC_GetStorageIDs))
3486 // return -1;
Richard Lowbd14bf42007-07-21 11:25:11 +00003487 if (ptp_getstorageids (params, &storageIDs) != PTP_RC_OK)
Linus Walleij9e1b0812006-12-12 19:22:02 +00003488 return -1;
3489 if (storageIDs.n < 1)
3490 return -1;
3491
3492 if (!ptp_operation_issupported(params,PTP_OC_GetStorageInfo)) {
3493 for (i = 0; i < storageIDs.n; i++) {
3494
Linus Walleije1ac07e2006-12-14 19:38:59 +00003495 storage = (LIBMTP_devicestorage_t *) malloc(sizeof(LIBMTP_devicestorage_t));
3496 storage->prev = storageprev;
3497 if (storageprev != NULL)
3498 storageprev->next = storage;
Linus Walleij9e1b0812006-12-12 19:22:02 +00003499 if (device->storage == NULL)
Linus Walleije1ac07e2006-12-14 19:38:59 +00003500 device->storage = storage;
Linus Walleij9e1b0812006-12-12 19:22:02 +00003501
Linus Walleije1ac07e2006-12-14 19:38:59 +00003502 storage->id = storageIDs.Storage[i];
3503 storage->StorageType = PTP_ST_Undefined;
3504 storage->FilesystemType = PTP_FST_Undefined;
3505 storage->AccessCapability = PTP_AC_ReadWrite;
3506 storage->MaxCapacity = (uint64_t) -1;
3507 storage->FreeSpaceInBytes = (uint64_t) -1;
3508 storage->FreeSpaceInObjects = (uint64_t) -1;
3509 storage->StorageDescription = strdup("Unknown storage");
3510 storage->VolumeIdentifier = strdup("Unknown volume");
3511 storage->next = NULL;
Linus Walleij9e1b0812006-12-12 19:22:02 +00003512
Linus Walleije1ac07e2006-12-14 19:38:59 +00003513 storageprev = storage;
Linus Walleij9e1b0812006-12-12 19:22:02 +00003514 }
3515 free(storageIDs.Storage);
3516 return 1;
3517 } else {
3518 for (i = 0; i < storageIDs.n; i++) {
Linus Walleij070e9b42007-01-22 08:49:28 +00003519 uint16_t ret;
3520 ret = ptp_getstorageinfo(params, storageIDs.Storage[i], &storageInfo);
3521 if (ret != PTP_RC_OK) {
3522 add_ptp_error_to_errorstack(device, ret, "LIBMTP_Get_Storage(): Could not get storage info.");
Linus Walleij9e1b0812006-12-12 19:22:02 +00003523 if (device->storage != NULL) {
3524 free_storage_list(device);
3525 }
3526 return -1;
3527 }
3528
Linus Walleije1ac07e2006-12-14 19:38:59 +00003529 storage = (LIBMTP_devicestorage_t *) malloc(sizeof(LIBMTP_devicestorage_t));
3530 storage->prev = storageprev;
3531 if (storageprev != NULL)
3532 storageprev->next = storage;
Linus Walleij9e1b0812006-12-12 19:22:02 +00003533 if (device->storage == NULL)
Linus Walleije1ac07e2006-12-14 19:38:59 +00003534 device->storage = storage;
Linus Walleij9e1b0812006-12-12 19:22:02 +00003535
Linus Walleije1ac07e2006-12-14 19:38:59 +00003536 storage->id = storageIDs.Storage[i];
3537 storage->StorageType = storageInfo.StorageType;
3538 storage->FilesystemType = storageInfo.FilesystemType;
3539 storage->AccessCapability = storageInfo.AccessCapability;
3540 storage->MaxCapacity = storageInfo.MaxCapability;
3541 storage->FreeSpaceInBytes = storageInfo.FreeSpaceInBytes;
3542 storage->FreeSpaceInObjects = storageInfo.FreeSpaceInImages;
3543 storage->StorageDescription = storageInfo.StorageDescription;
3544 storage->VolumeIdentifier = storageInfo.VolumeLabel;
3545 storage->next = NULL;
Linus Walleij9e1b0812006-12-12 19:22:02 +00003546
Linus Walleije1ac07e2006-12-14 19:38:59 +00003547 storageprev = storage;
Linus Walleij9e1b0812006-12-12 19:22:02 +00003548 }
3549
Linus Walleij5c1499e2009-02-21 06:54:29 +00003550 if (storage != NULL)
3551 storage->next = NULL;
Linus Walleij9e1b0812006-12-12 19:22:02 +00003552
3553 sort_storage_by(device,sortby);
3554 free(storageIDs.Storage);
3555 return 0;
3556 }
3557}
3558
3559/**
Linus Walleijf6bc1782006-03-24 15:12:47 +00003560 * This creates a new file metadata structure and allocates memory
3561 * for it. Notice that if you add strings to this structure they
3562 * will be freed by the corresponding <code>LIBMTP_destroy_file_t</code>
mopoke96143402006-10-30 04:37:26 +00003563 * operation later, so be careful of using strdup() when assigning
Linus Walleijf6bc1782006-03-24 15:12:47 +00003564 * strings, e.g.:
3565 *
3566 * <pre>
3567 * LIBMTP_file_t *file = LIBMTP_new_file_t();
3568 * file->filename = strdup(namestr);
3569 * ....
3570 * LIBMTP_destroy_file_t(file);
3571 * </pre>
3572 *
3573 * @return a pointer to the newly allocated metadata structure.
3574 * @see LIBMTP_destroy_file_t()
3575 */
3576LIBMTP_file_t *LIBMTP_new_file_t(void)
3577{
3578 LIBMTP_file_t *new = (LIBMTP_file_t *) malloc(sizeof(LIBMTP_file_t));
3579 if (new == NULL) {
3580 return NULL;
3581 }
3582 new->filename = NULL;
Linus Walleijea68f1f2008-06-22 21:54:44 +00003583 new->item_id = 0;
3584 new->parent_id = 0;
3585 new->storage_id = 0;
Linus Walleijf6bc1782006-03-24 15:12:47 +00003586 new->filesize = 0;
Richard Lowd3b17022009-04-11 12:37:39 +00003587 new->modificationdate = 0;
Linus Walleijf6bc1782006-03-24 15:12:47 +00003588 new->filetype = LIBMTP_FILETYPE_UNKNOWN;
3589 new->next = NULL;
3590 return new;
3591}
3592
3593/**
3594 * This destroys a file metadata structure and deallocates the memory
mopoke96143402006-10-30 04:37:26 +00003595 * used by it, including any strings. Never use a file metadata
Linus Walleijf6bc1782006-03-24 15:12:47 +00003596 * structure again after calling this function on it.
3597 * @param file the file metadata to destroy.
3598 * @see LIBMTP_new_file_t()
3599 */
3600void LIBMTP_destroy_file_t(LIBMTP_file_t *file)
3601{
3602 if (file == NULL) {
3603 return;
3604 }
3605 if (file->filename != NULL)
3606 free(file->filename);
3607 free(file);
3608 return;
3609}
3610
3611/**
mopoke31364442006-11-20 04:53:04 +00003612* THIS FUNCTION IS DEPRECATED. PLEASE UPDATE YOUR CODE IN ORDER
Richard Lowdc0b6c72006-11-13 09:22:23 +00003613 * NOT TO USE IT.
3614 * @see LIBMTP_Get_Filelisting_With_Callback()
3615 */
3616LIBMTP_file_t *LIBMTP_Get_Filelisting(LIBMTP_mtpdevice_t *device)
3617{
3618 printf("WARNING: LIBMTP_Get_Filelisting() is deprecated.\n");
3619 printf("WARNING: please update your code to use LIBMTP_Get_Filelisting_With_Callback()\n");
3620 return LIBMTP_Get_Filelisting_With_Callback(device, NULL, NULL);
3621}
3622
3623/**
Linus Walleijf6bc1782006-03-24 15:12:47 +00003624 * This returns a long list of all files available
Linus Walleij25d5c212008-08-14 06:49:13 +00003625 * on the current MTP device. Folders will not be returned, but abstract
3626 * entities like playlists and albums will show up as "files". Typical usage:
Linus Walleijf6bc1782006-03-24 15:12:47 +00003627 *
3628 * <pre>
3629 * LIBMTP_file_t *filelist;
3630 *
Richard Lowdc0b6c72006-11-13 09:22:23 +00003631 * filelist = LIBMTP_Get_Filelisting_With_Callback(device, callback, data);
Linus Walleijf6bc1782006-03-24 15:12:47 +00003632 * while (filelist != NULL) {
3633 * LIBMTP_file_t *tmp;
3634 *
3635 * // Do something on each element in the list here...
3636 * tmp = filelist;
3637 * filelist = filelist->next;
3638 * LIBMTP_destroy_file_t(tmp);
3639 * }
3640 * </pre>
3641 *
Linus Walleij25d5c212008-08-14 06:49:13 +00003642 * If you want to group your file listing by storage (per storage unit) or
3643 * arrange files into folders, you must dereference the <code>storage_id</code>
3644 * and/or <code>parent_id</code> field of the returned <code>LIBMTP_file_t</code>
3645 * struct. To arrange by folders or files you typically have to create the proper
3646 * trees by calls to <code>LIBMTP_Get_Storage()</code> and/or
3647 * <code>LIBMTP_Get_Folder_List()</code> first.
3648 *
Linus Walleijf6bc1782006-03-24 15:12:47 +00003649 * @param device a pointer to the device to get the file listing for.
Richard Lowdc0b6c72006-11-13 09:22:23 +00003650 * @param callback a function to be called during the tracklisting retrieveal
Linus Walleij25d5c212008-08-14 06:49:13 +00003651 * for displaying progress bars etc, or NULL if you don't want
3652 * any callbacks.
Richard Lowdc0b6c72006-11-13 09:22:23 +00003653 * @param data a user-defined pointer that is passed along to
Linus Walleij25d5c212008-08-14 06:49:13 +00003654 * the <code>progress</code> function in order to
3655 * pass along some user defined data to the progress
3656 * updates. If not used, set this to NULL.
Linus Walleijf6bc1782006-03-24 15:12:47 +00003657 * @return a list of files that can be followed using the <code>next</code>
Linus Walleij25d5c212008-08-14 06:49:13 +00003658 * field of the <code>LIBMTP_file_t</code> data structure.
3659 * Each of the metadata tags must be freed after use, and may
3660 * contain only partial metadata information, i.e. one or several
3661 * fields may be NULL or 0.
Linus Walleij2e4b5f92006-06-16 14:00:49 +00003662 * @see LIBMTP_Get_Filemetadata()
Linus Walleijf6bc1782006-03-24 15:12:47 +00003663 */
Richard Lowdc0b6c72006-11-13 09:22:23 +00003664LIBMTP_file_t *LIBMTP_Get_Filelisting_With_Callback(LIBMTP_mtpdevice_t *device,
3665 LIBMTP_progressfunc_t const callback,
3666 void const * const data)
Linus Walleijf6bc1782006-03-24 15:12:47 +00003667{
3668 uint32_t i = 0;
3669 LIBMTP_file_t *retfiles = NULL;
3670 LIBMTP_file_t *curfile = NULL;
3671 PTPParams *params = (PTPParams *) device->params;
Linus Walleijd9d28d52007-08-04 19:01:18 +00003672 PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo;
Linus Walleijdbcc8242007-08-05 22:31:26 +00003673 uint16_t ret;
Linus Walleij438bd7f2006-06-08 11:35:44 +00003674
mopoke96143402006-10-30 04:37:26 +00003675 // Get all the handles if we haven't already done that
Linus Walleijd4637502009-06-14 23:03:33 +00003676 if (params->nrofobjects == 0) {
Linus Walleij438bd7f2006-06-08 11:35:44 +00003677 flush_handles(device);
Linus Walleijf6bc1782006-03-24 15:12:47 +00003678 }
mopoke96143402006-10-30 04:37:26 +00003679
Linus Walleijd4637502009-06-14 23:03:33 +00003680 for (i = 0; i < params->nrofobjects; i++) {
Linus Walleij070e9b42007-01-22 08:49:28 +00003681 LIBMTP_file_t *file;
Linus Walleijd4637502009-06-14 23:03:33 +00003682 PTPObject *ob, *xob;
Linus Walleijf6bc1782006-03-24 15:12:47 +00003683
Richard Lowdc0b6c72006-11-13 09:22:23 +00003684 if (callback != NULL)
Linus Walleijd4637502009-06-14 23:03:33 +00003685 callback(i, params->nrofobjects, data);
mopoke31364442006-11-20 04:53:04 +00003686
Linus Walleijd4637502009-06-14 23:03:33 +00003687 ob = &params->objects[i];
Linus Walleijf6bc1782006-03-24 15:12:47 +00003688
Linus Walleijd4637502009-06-14 23:03:33 +00003689 if (ob->oi.ObjectFormat == PTP_OFC_Association) {
Linus Walleijd9d28d52007-08-04 19:01:18 +00003690 // MTP use this object format for folders which means
Linus Walleijf0bf4372007-07-01 21:47:38 +00003691 // these "files" will turn up on a folder listing instead.
3692 continue;
Linus Walleijf6bc1782006-03-24 15:12:47 +00003693 }
3694
Linus Walleijf0bf4372007-07-01 21:47:38 +00003695 // Allocate a new file type
3696 file = LIBMTP_new_file_t();
3697
Linus Walleijd4637502009-06-14 23:03:33 +00003698 file->parent_id = ob->oi.ParentObject;
3699 file->storage_id = ob->oi.StorageID;
Linus Walleijf0bf4372007-07-01 21:47:38 +00003700
Linus Walleijd9d28d52007-08-04 19:01:18 +00003701 // This is some sort of unique ID so we can keep track of the track.
Linus Walleijd4637502009-06-14 23:03:33 +00003702 file->item_id = ob->oid;
Linus Walleijd9d28d52007-08-04 19:01:18 +00003703
Linus Walleijf0bf4372007-07-01 21:47:38 +00003704 // Set the filetype
Linus Walleijd4637502009-06-14 23:03:33 +00003705 file->filetype = map_ptp_type_to_libmtp_type(ob->oi.ObjectFormat);
Richard Lowd3b17022009-04-11 12:37:39 +00003706
3707 // Set the modification date
Linus Walleijd4637502009-06-14 23:03:33 +00003708 file->modificationdate = ob->oi.ModificationDate;
Linus Walleijf0bf4372007-07-01 21:47:38 +00003709
3710 // Original file-specific properties
Linus Walleijd9d28d52007-08-04 19:01:18 +00003711 // We only have 32-bit file size here; if we find it, we use the
3712 // PTP_OPC_ObjectSize property which has 64bit precision.
Linus Walleijd4637502009-06-14 23:03:33 +00003713 file->filesize = ob->oi.ObjectCompressedSize;
3714 if (ob->oi.Filename != NULL) {
3715 file->filename = strdup(ob->oi.Filename);
Linus Walleijf0bf4372007-07-01 21:47:38 +00003716 }
3717
Linus Walleijd9d28d52007-08-04 19:01:18 +00003718 /*
Linus Walleij89bb1cd2009-07-24 21:03:36 +00003719 * A special quirk for devices that doesn't quite
Linus Walleijc8abc922008-09-28 18:38:42 +00003720 * remember that some files marked as "unknown" type are
Linus Walleij89bb1cd2009-07-24 21:03:36 +00003721 * actually OGG or FLAC files. We look at the filename extension
3722 * and see if it happens that this was atleast named "ogg" or "flac"
Linus Walleijc8abc922008-09-28 18:38:42 +00003723 * and fall back on this heuristic approach in that case,
3724 * for these bugged devices only.
3725 */
Linus Walleij89bb1cd2009-07-24 21:03:36 +00003726 if (file->filetype == LIBMTP_FILETYPE_UNKNOWN) {
3727 if ((FLAG_IRIVER_OGG_ALZHEIMER(ptp_usb) ||
3728 FLAG_OGG_IS_UNKNOWN(ptp_usb)) &&
3729 has_ogg_extension(file->filename))
3730 file->filetype = LIBMTP_FILETYPE_OGG;
3731 if (FLAG_FLAC_IS_UNKNOWN(ptp_usb) &&
3732 has_flac_extension(file->filename))
3733 file->filetype = LIBMTP_FILETYPE_FLAC;
Linus Walleijc8abc922008-09-28 18:38:42 +00003734 }
3735
3736 /*
Linus Walleijd9d28d52007-08-04 19:01:18 +00003737 * If we have a cached, large set of metadata, then use it!
3738 */
Linus Walleijd4637502009-06-14 23:03:33 +00003739 ret = ptp_object_want (params, ob->oid, PTPOBJECT_MTPPROPLIST_LOADED, &xob);
3740 if (ob->mtpprops) {
3741 MTPProperties *prop = ob->mtpprops;
Linus Walleij1e9a0332007-09-12 19:35:56 +00003742 int i;
Linus Walleij1e9a0332007-09-12 19:35:56 +00003743
Linus Walleijd4637502009-06-14 23:03:33 +00003744 for (i=0;i<ob->nrofmtpprops;i++) {
Linus Walleijd9d28d52007-08-04 19:01:18 +00003745 // Pick ObjectSize here...
3746 if (prop->property == PTP_OPC_ObjectSize) {
Linus Walleijddaba2f2007-10-02 21:20:30 +00003747 if (device->object_bitsize == 64) {
Linus Walleijca2a1702007-10-02 20:41:45 +00003748 file->filesize = prop->propval.u64;
3749 } else {
Linus Walleijddaba2f2007-10-02 21:20:30 +00003750 file->filesize = prop->propval.u32;
Linus Walleijca2a1702007-10-02 20:41:45 +00003751 }
Linus Walleijd9d28d52007-08-04 19:01:18 +00003752 break;
3753 }
Linus Walleijd4637502009-06-14 23:03:33 +00003754 prop++;
Linus Walleijd9d28d52007-08-04 19:01:18 +00003755 }
Linus Walleijd9d28d52007-08-04 19:01:18 +00003756 } else {
3757 uint16_t *props = NULL;
3758 uint32_t propcnt = 0;
3759
3760 // First see which properties can be retrieved for this object format
Linus Walleijd4637502009-06-14 23:03:33 +00003761 ret = ptp_mtp_getobjectpropssupported(params, ob->oi.ObjectFormat, &propcnt, &props);
Linus Walleijd9d28d52007-08-04 19:01:18 +00003762 if (ret != PTP_RC_OK) {
3763 add_ptp_error_to_errorstack(device, ret, "LIBMTP_Get_Filelisting_With_Callback(): call to ptp_mtp_getobjectpropssupported() failed.");
3764 // Silently fall through.
3765 } else {
Richard Lowd7bcab52007-09-16 16:30:20 +00003766 int i;
Linus Walleijd9d28d52007-08-04 19:01:18 +00003767 for (i=0;i<propcnt;i++) {
3768 switch (props[i]) {
3769 case PTP_OPC_ObjectSize:
Linus Walleijddaba2f2007-10-02 21:20:30 +00003770 if (device->object_bitsize == 64) {
3771 file->filesize = get_u64_from_object(device, file->item_id, PTP_OPC_ObjectSize, 0);
3772 } else {
3773 file->filesize = get_u32_from_object(device, file->item_id, PTP_OPC_ObjectSize, 0);
Linus Walleijca2a1702007-10-02 20:41:45 +00003774 }
Linus Walleijd9d28d52007-08-04 19:01:18 +00003775 break;
3776 default:
3777 break;
3778 }
3779 }
3780 free(props);
3781 }
3782 }
Linus Walleijd9d28d52007-08-04 19:01:18 +00003783
Linus Walleijf0bf4372007-07-01 21:47:38 +00003784 // Add track to a list that will be returned afterwards.
3785 if (retfiles == NULL) {
3786 retfiles = file;
3787 curfile = file;
3788 } else {
3789 curfile->next = file;
3790 curfile = file;
3791 }
3792
3793 // Call listing callback
3794 // double progressPercent = (double)i*(double)100.0 / (double)params->handles.n;
3795
Linus Walleijf6bc1782006-03-24 15:12:47 +00003796 } // Handle counting loop
3797 return retfiles;
3798}
3799
3800/**
Linus Walleij2e4b5f92006-06-16 14:00:49 +00003801 * This function retrieves the metadata for a single file off
3802 * the device.
3803 *
3804 * Do not call this function repeatedly! The file handles are linearly
3805 * searched O(n) and the call may involve (slow) USB traffic, so use
3806 * <code>LIBMTP_Get_Filelisting()</code> and cache the file, preferably
3807 * as an efficient data structure such as a hash list.
3808 *
Linus Walleij0801cd52008-01-29 22:19:15 +00003809 * Incidentally this function will return metadata for
3810 * a folder (association) as well, but this is not a proper use
3811 * of it, it is intended for file manipulation, not folder manipulation.
3812 *
Linus Walleij2e4b5f92006-06-16 14:00:49 +00003813 * @param device a pointer to the device to get the file metadata from.
3814 * @param fileid the object ID of the file that you want the metadata for.
3815 * @return a metadata entry on success or NULL on failure.
3816 * @see LIBMTP_Get_Filelisting()
3817 */
3818LIBMTP_file_t *LIBMTP_Get_Filemetadata(LIBMTP_mtpdevice_t *device, uint32_t const fileid)
3819{
3820 uint32_t i = 0;
3821 PTPParams *params = (PTPParams *) device->params;
Linus Walleijdbcc8242007-08-05 22:31:26 +00003822 uint16_t ret;
Linus Walleijd4637502009-06-14 23:03:33 +00003823 PTPObject *ob;
3824 LIBMTP_file_t *file;
Linus Walleij2e4b5f92006-06-16 14:00:49 +00003825
mopoke96143402006-10-30 04:37:26 +00003826 // Get all the handles if we haven't already done that
Linus Walleijd4637502009-06-14 23:03:33 +00003827 if (params->nrofobjects == 0) {
Linus Walleij2e4b5f92006-06-16 14:00:49 +00003828 flush_handles(device);
3829 }
3830
Linus Walleijd4637502009-06-14 23:03:33 +00003831 ret = ptp_object_want (params, fileid, PTPOBJECT_OBJECTINFO_LOADED|PTPOBJECT_MTPPROPLIST_LOADED, &ob);
3832 if (ret != PTP_RC_OK)
3833 return NULL;
Linus Walleij2e4b5f92006-06-16 14:00:49 +00003834
Linus Walleijd4637502009-06-14 23:03:33 +00003835 // Allocate a new file type
3836 file = LIBMTP_new_file_t();
3837
3838 file->parent_id = ob->oi.ParentObject;
3839 file->storage_id = ob->oi.StorageID;
Linus Walleij2e4b5f92006-06-16 14:00:49 +00003840
Linus Walleijd4637502009-06-14 23:03:33 +00003841 // Set the filetype
3842 file->filetype = map_ptp_type_to_libmtp_type(ob->oi.ObjectFormat);
Linus Walleij070e9b42007-01-22 08:49:28 +00003843
Linus Walleijd4637502009-06-14 23:03:33 +00003844 // Original file-specific properties
3845
3846 // We only have 32-bit file size here; later we use the PTP_OPC_ObjectSize property
3847 file->filesize = ob->oi.ObjectCompressedSize;
3848 if (ob->oi.Filename != NULL) {
3849 file->filename = strdup(ob->oi.Filename);
Linus Walleij2e4b5f92006-06-16 14:00:49 +00003850 }
Linus Walleijd4637502009-06-14 23:03:33 +00003851
3852 // This is some sort of unique ID so we can keep track of the file.
3853 file->item_id = fileid;
3854
3855 /*
3856 * If we have a cached, large set of metadata, then use it!
3857 */
3858 if (ob->mtpprops) {
3859 MTPProperties *prop = ob->mtpprops;
3860
3861 for (i=0;i<ob->nrofmtpprops;i++,prop++) {
3862 // Pick ObjectSize here...
3863 if (prop->property == PTP_OPC_ObjectSize) {
3864 // This may already be set, but this 64bit precision value
3865 // is better than the PTP 32bit value, so let it override.
3866 if (device->object_bitsize == 64) {
3867 file->filesize = prop->propval.u64;
3868 } else {
3869 file->filesize = prop->propval.u32;
3870 }
3871 break;
3872 }
3873 }
3874 } else {
3875 uint16_t *props = NULL;
3876 uint32_t propcnt = 0;
3877
3878 // First see which properties can be retrieved for this object format
3879 ret = ptp_mtp_getobjectpropssupported(params, map_libmtp_type_to_ptp_type(file->filetype), &propcnt, &props);
3880 if (ret != PTP_RC_OK) {
3881 add_ptp_error_to_errorstack(device, ret, "LIBMTP_Get_Filemetadata(): call to ptp_mtp_getobjectpropssupported() failed.");
3882 // Silently fall through.
3883 } else {
3884 for (i=0;i<propcnt;i++) {
3885 switch (props[i]) {
3886 case PTP_OPC_ObjectSize:
3887 if (device->object_bitsize == 64) {
3888 file->filesize = get_u64_from_object(device, file->item_id, PTP_OPC_ObjectSize, 0);
3889 } else {
3890 file->filesize = get_u32_from_object(device, file->item_id, PTP_OPC_ObjectSize, 0);
3891 }
3892 break;
3893 default:
3894 break;
3895 }
3896 }
3897 free(props);
3898 }
3899 }
3900
3901 return file;
Linus Walleij2e4b5f92006-06-16 14:00:49 +00003902}
3903
3904/**
Linus Walleij394bbbe2006-02-22 16:10:53 +00003905 * This creates a new track metadata structure and allocates memory
3906 * for it. Notice that if you add strings to this structure they
3907 * will be freed by the corresponding <code>LIBMTP_destroy_track_t</code>
mopoke96143402006-10-30 04:37:26 +00003908 * operation later, so be careful of using strdup() when assigning
Linus Walleij394bbbe2006-02-22 16:10:53 +00003909 * strings, e.g.:
3910 *
Linus Walleij17e39f72006-02-23 15:54:28 +00003911 * <pre>
Linus Walleij394bbbe2006-02-22 16:10:53 +00003912 * LIBMTP_track_t *track = LIBMTP_new_track_t();
3913 * track->title = strdup(titlestr);
3914 * ....
3915 * LIBMTP_destroy_track_t(track);
Linus Walleij17e39f72006-02-23 15:54:28 +00003916 * </pre>
Linus Walleij394bbbe2006-02-22 16:10:53 +00003917 *
3918 * @return a pointer to the newly allocated metadata structure.
3919 * @see LIBMTP_destroy_track_t()
3920 */
3921LIBMTP_track_t *LIBMTP_new_track_t(void)
Linus Walleijb9256fd2006-02-15 09:40:43 +00003922{
3923 LIBMTP_track_t *new = (LIBMTP_track_t *) malloc(sizeof(LIBMTP_track_t));
3924 if (new == NULL) {
3925 return NULL;
3926 }
Linus Walleijea68f1f2008-06-22 21:54:44 +00003927 new->item_id = 0;
3928 new->parent_id = 0;
3929 new->storage_id = 0;
Linus Walleijb9256fd2006-02-15 09:40:43 +00003930 new->title = NULL;
3931 new->artist = NULL;
Linus Walleij31b74292008-05-02 23:29:06 +00003932 new->composer = NULL;
Linus Walleijb9256fd2006-02-15 09:40:43 +00003933 new->album = NULL;
3934 new->genre = NULL;
3935 new->date = NULL;
3936 new->filename = NULL;
3937 new->duration = 0;
3938 new->tracknumber = 0;
3939 new->filesize = 0;
Linus Walleijf6bc1782006-03-24 15:12:47 +00003940 new->filetype = LIBMTP_FILETYPE_UNKNOWN;
Linus Walleijcf223e62006-06-19 09:31:53 +00003941 new->samplerate = 0;
3942 new->nochannels = 0;
3943 new->wavecodec = 0;
3944 new->bitrate = 0;
3945 new->bitratetype = 0;
3946 new->rating = 0;
3947 new->usecount = 0;
Richard Lowd3b17022009-04-11 12:37:39 +00003948 new->modificationdate = 0;
Linus Walleijb9256fd2006-02-15 09:40:43 +00003949 new->next = NULL;
3950 return new;
3951}
3952
Linus Walleij394bbbe2006-02-22 16:10:53 +00003953/**
3954 * This destroys a track metadata structure and deallocates the memory
mopoke96143402006-10-30 04:37:26 +00003955 * used by it, including any strings. Never use a track metadata
Linus Walleij394bbbe2006-02-22 16:10:53 +00003956 * structure again after calling this function on it.
3957 * @param track the track metadata to destroy.
3958 * @see LIBMTP_new_track_t()
3959 */
Linus Walleijb9256fd2006-02-15 09:40:43 +00003960void LIBMTP_destroy_track_t(LIBMTP_track_t *track)
3961{
3962 if (track == NULL) {
3963 return;
3964 }
3965 if (track->title != NULL)
3966 free(track->title);
3967 if (track->artist != NULL)
3968 free(track->artist);
Linus Walleij31b74292008-05-02 23:29:06 +00003969 if (track->composer != NULL)
3970 free(track->composer);
Linus Walleijb9256fd2006-02-15 09:40:43 +00003971 if (track->album != NULL)
3972 free(track->album);
3973 if (track->genre != NULL)
3974 free(track->genre);
3975 if (track->date != NULL)
3976 free(track->date);
3977 if (track->filename != NULL)
3978 free(track->filename);
3979 free(track);
3980 return;
3981}
3982
3983/**
Linus Walleij338ade42007-07-03 20:44:08 +00003984 * This function maps and copies a property onto the track metadata if applicable.
3985 */
Linus Walleijddaba2f2007-10-02 21:20:30 +00003986static void pick_property_to_track_metadata(LIBMTP_mtpdevice_t *device, MTPProperties *prop, LIBMTP_track_t *track)
Linus Walleij338ade42007-07-03 20:44:08 +00003987{
3988 switch (prop->property) {
3989 case PTP_OPC_Name:
3990 if (prop->propval.str != NULL)
3991 track->title = strdup(prop->propval.str);
3992 else
3993 track->title = NULL;
3994 break;
3995 case PTP_OPC_Artist:
3996 if (prop->propval.str != NULL)
3997 track->artist = strdup(prop->propval.str);
3998 else
3999 track->artist = NULL;
4000 break;
Linus Walleij31b74292008-05-02 23:29:06 +00004001 case PTP_OPC_Composer:
4002 if (prop->propval.str != NULL)
4003 track->composer = strdup(prop->propval.str);
4004 else
4005 track->composer = NULL;
4006 break;
Linus Walleij338ade42007-07-03 20:44:08 +00004007 case PTP_OPC_Duration:
4008 track->duration = prop->propval.u32;
4009 break;
4010 case PTP_OPC_Track:
4011 track->tracknumber = prop->propval.u16;
4012 break;
4013 case PTP_OPC_Genre:
4014 if (prop->propval.str != NULL)
4015 track->genre = strdup(prop->propval.str);
4016 else
4017 track->genre = NULL;
4018 break;
4019 case PTP_OPC_AlbumName:
4020 if (prop->propval.str != NULL)
4021 track->album = strdup(prop->propval.str);
4022 else
4023 track->album = NULL;
4024 break;
4025 case PTP_OPC_OriginalReleaseDate:
4026 if (prop->propval.str != NULL)
4027 track->date = strdup(prop->propval.str);
4028 else
4029 track->date = NULL;
4030 break;
4031 // These are, well not so important.
4032 case PTP_OPC_SampleRate:
4033 track->samplerate = prop->propval.u32;
4034 break;
4035 case PTP_OPC_NumberOfChannels:
4036 track->nochannels = prop->propval.u16;
4037 break;
4038 case PTP_OPC_AudioWAVECodec:
4039 track->wavecodec = prop->propval.u32;
4040 break;
4041 case PTP_OPC_AudioBitRate:
4042 track->bitrate = prop->propval.u32;
4043 break;
4044 case PTP_OPC_BitRateType:
4045 track->bitratetype = prop->propval.u16;
4046 break;
4047 case PTP_OPC_Rating:
4048 track->rating = prop->propval.u16;
4049 break;
4050 case PTP_OPC_UseCount:
4051 track->usecount = prop->propval.u32;
4052 break;
Linus Walleijd9d28d52007-08-04 19:01:18 +00004053 case PTP_OPC_ObjectSize:
Linus Walleijddaba2f2007-10-02 21:20:30 +00004054 if (device->object_bitsize == 64) {
4055 track->filesize = prop->propval.u64;
4056 } else {
4057 track->filesize = prop->propval.u32;
Linus Walleijca2a1702007-10-02 20:41:45 +00004058 }
Linus Walleijd9d28d52007-08-04 19:01:18 +00004059 break;
4060 default:
4061 break;
Linus Walleij338ade42007-07-03 20:44:08 +00004062 }
4063}
4064
4065/**
Linus Walleij8ab54262006-06-21 07:12:28 +00004066 * This function retrieves the track metadata for a track
4067 * given by a unique ID.
4068 * @param device a pointer to the device to get the track metadata off.
4069 * @param trackid the unique ID of the track.
4070 * @param objectformat the object format of this track, so we know what it supports.
4071 * @param track a metadata set to fill in.
4072 */
4073static void get_track_metadata(LIBMTP_mtpdevice_t *device, uint16_t objectformat,
4074 LIBMTP_track_t *track)
4075{
Linus Walleij00cf0642006-07-26 20:40:59 +00004076 uint16_t ret;
4077 PTPParams *params = (PTPParams *) device->params;
4078 uint32_t i;
Linus Walleijd4637502009-06-14 23:03:33 +00004079 MTPProperties *prop;
4080 PTPObject *ob;
Linus Walleij00cf0642006-07-26 20:40:59 +00004081
Linus Walleij338ade42007-07-03 20:44:08 +00004082 /*
4083 * If we have a cached, large set of metadata, then use it!
4084 */
Linus Walleijd4637502009-06-14 23:03:33 +00004085 ret = ptp_object_want (params, track->item_id, PTPOBJECT_MTPPROPLIST_LOADED, &ob);
4086 if (ob->mtpprops) {
4087 prop = ob->mtpprops;
4088 for (i=0;i<ob->nrofmtpprops;i++,prop++)
Linus Walleijddaba2f2007-10-02 21:20:30 +00004089 pick_property_to_track_metadata(device, prop, track);
Linus Walleij3fcfea52006-11-13 07:07:36 +00004090 } else {
Linus Walleij3fcfea52006-11-13 07:07:36 +00004091 uint16_t *props = NULL;
4092 uint32_t propcnt = 0;
4093
4094 // First see which properties can be retrieved for this object format
Linus Walleij070e9b42007-01-22 08:49:28 +00004095 ret = ptp_mtp_getobjectpropssupported(params, map_libmtp_type_to_ptp_type(track->filetype), &propcnt, &props);
Linus Walleij3fcfea52006-11-13 07:07:36 +00004096 if (ret != PTP_RC_OK) {
Linus Walleij070e9b42007-01-22 08:49:28 +00004097 add_ptp_error_to_errorstack(device, ret, "get_track_metadata(): call to ptp_mtp_getobjectpropssupported() failed.");
Linus Walleij3fcfea52006-11-13 07:07:36 +00004098 // Just bail out for now, nothing is ever set.
4099 return;
4100 } else {
4101 for (i=0;i<propcnt;i++) {
4102 switch (props[i]) {
4103 case PTP_OPC_Name:
Linus Walleij9901e222006-11-30 12:28:19 +00004104 track->title = get_string_from_object(device, track->item_id, PTP_OPC_Name);
Linus Walleij3fcfea52006-11-13 07:07:36 +00004105 break;
4106 case PTP_OPC_Artist:
Linus Walleij9901e222006-11-30 12:28:19 +00004107 track->artist = get_string_from_object(device, track->item_id, PTP_OPC_Artist);
Linus Walleij3fcfea52006-11-13 07:07:36 +00004108 break;
Linus Walleij31b74292008-05-02 23:29:06 +00004109 case PTP_OPC_Composer:
4110 track->composer = get_string_from_object(device, track->item_id, PTP_OPC_Composer);
4111 break;
Linus Walleij3fcfea52006-11-13 07:07:36 +00004112 case PTP_OPC_Duration:
Linus Walleij9901e222006-11-30 12:28:19 +00004113 track->duration = get_u32_from_object(device, track->item_id, PTP_OPC_Duration, 0);
Linus Walleij3fcfea52006-11-13 07:07:36 +00004114 break;
4115 case PTP_OPC_Track:
Linus Walleij9901e222006-11-30 12:28:19 +00004116 track->tracknumber = get_u16_from_object(device, track->item_id, PTP_OPC_Track, 0);
Linus Walleij3fcfea52006-11-13 07:07:36 +00004117 break;
4118 case PTP_OPC_Genre:
Linus Walleij9901e222006-11-30 12:28:19 +00004119 track->genre = get_string_from_object(device, track->item_id, PTP_OPC_Genre);
Linus Walleij3fcfea52006-11-13 07:07:36 +00004120 break;
4121 case PTP_OPC_AlbumName:
Linus Walleij9901e222006-11-30 12:28:19 +00004122 track->album = get_string_from_object(device, track->item_id, PTP_OPC_AlbumName);
Linus Walleij3fcfea52006-11-13 07:07:36 +00004123 break;
4124 case PTP_OPC_OriginalReleaseDate:
Linus Walleij9901e222006-11-30 12:28:19 +00004125 track->date = get_string_from_object(device, track->item_id, PTP_OPC_OriginalReleaseDate);
Linus Walleij3fcfea52006-11-13 07:07:36 +00004126 break;
4127 // These are, well not so important.
4128 case PTP_OPC_SampleRate:
Linus Walleij9901e222006-11-30 12:28:19 +00004129 track->samplerate = get_u32_from_object(device, track->item_id, PTP_OPC_SampleRate, 0);
Linus Walleij3fcfea52006-11-13 07:07:36 +00004130 break;
4131 case PTP_OPC_NumberOfChannels:
Linus Walleij9901e222006-11-30 12:28:19 +00004132 track->nochannels = get_u16_from_object(device, track->item_id, PTP_OPC_NumberOfChannels, 0);
Linus Walleij3fcfea52006-11-13 07:07:36 +00004133 break;
4134 case PTP_OPC_AudioWAVECodec:
Linus Walleij9901e222006-11-30 12:28:19 +00004135 track->wavecodec = get_u32_from_object(device, track->item_id, PTP_OPC_AudioWAVECodec, 0);
Linus Walleij3fcfea52006-11-13 07:07:36 +00004136 break;
4137 case PTP_OPC_AudioBitRate:
Linus Walleij9901e222006-11-30 12:28:19 +00004138 track->bitrate = get_u32_from_object(device, track->item_id, PTP_OPC_AudioBitRate, 0);
Linus Walleij3fcfea52006-11-13 07:07:36 +00004139 break;
4140 case PTP_OPC_BitRateType:
Linus Walleij9901e222006-11-30 12:28:19 +00004141 track->bitratetype = get_u16_from_object(device, track->item_id, PTP_OPC_BitRateType, 0);
Linus Walleij3fcfea52006-11-13 07:07:36 +00004142 break;
4143 case PTP_OPC_Rating:
Linus Walleij9901e222006-11-30 12:28:19 +00004144 track->rating = get_u16_from_object(device, track->item_id, PTP_OPC_Rating, 0);
Linus Walleij3fcfea52006-11-13 07:07:36 +00004145 break;
4146 case PTP_OPC_UseCount:
Linus Walleij9901e222006-11-30 12:28:19 +00004147 track->usecount = get_u32_from_object(device, track->item_id, PTP_OPC_UseCount, 0);
Linus Walleij3fcfea52006-11-13 07:07:36 +00004148 break;
Linus Walleijd9d28d52007-08-04 19:01:18 +00004149 case PTP_OPC_ObjectSize:
Linus Walleijddaba2f2007-10-02 21:20:30 +00004150 if (device->object_bitsize == 64) {
4151 track->filesize = get_u64_from_object(device, track->item_id, PTP_OPC_ObjectSize, 0);
4152 } else {
4153 track->filesize = (uint64_t) get_u32_from_object(device, track->item_id, PTP_OPC_ObjectSize, 0);
Linus Walleijca2a1702007-10-02 20:41:45 +00004154 }
Linus Walleijd9d28d52007-08-04 19:01:18 +00004155 break;
Linus Walleij3fcfea52006-11-13 07:07:36 +00004156 }
4157 }
4158 free(props);
4159 }
Linus Walleij00cf0642006-07-26 20:40:59 +00004160 }
Linus Walleij8ab54262006-06-21 07:12:28 +00004161}
4162
4163/**
mopoke31364442006-11-20 04:53:04 +00004164 * THIS FUNCTION IS DEPRECATED. PLEASE UPDATE YOUR CODE IN ORDER
Linus Walleij3fcfea52006-11-13 07:07:36 +00004165 * NOT TO USE IT.
4166 * @see LIBMTP_Get_Tracklisting_With_Callback()
4167 */
4168LIBMTP_track_t *LIBMTP_Get_Tracklisting(LIBMTP_mtpdevice_t *device)
4169{
4170 printf("WARNING: LIBMTP_Get_Tracklisting() is deprecated.\n");
4171 printf("WARNING: please update your code to use LIBMTP_Get_Tracklisting_With_Callback()\n");
Richard Lowdc0b6c72006-11-13 09:22:23 +00004172 return LIBMTP_Get_Tracklisting_With_Callback(device, NULL, NULL);
Linus Walleij3fcfea52006-11-13 07:07:36 +00004173}
4174
4175/**
Linus Walleij25d5c212008-08-14 06:49:13 +00004176 * This returns a long list of all tracks available on the current MTP device.
4177 * Tracks include multimedia objects, both music tracks and video tracks.
4178 * Typical usage:
Linus Walleija4982732006-02-24 15:46:02 +00004179 *
4180 * <pre>
4181 * LIBMTP_track_t *tracklist;
4182 *
Richard Low6711f442007-05-05 19:00:59 +00004183 * tracklist = LIBMTP_Get_Tracklisting_With_Callback(device, callback, data);
Linus Walleija4982732006-02-24 15:46:02 +00004184 * while (tracklist != NULL) {
4185 * LIBMTP_track_t *tmp;
4186 *
4187 * // Do something on each element in the list here...
4188 * tmp = tracklist;
4189 * tracklist = tracklist->next;
4190 * LIBMTP_destroy_track_t(tmp);
4191 * }
4192 * </pre>
4193 *
Linus Walleij25d5c212008-08-14 06:49:13 +00004194 * If you want to group your track listing by storage (per storage unit) or
4195 * arrange tracks into folders, you must dereference the <code>storage_id</code>
4196 * and/or <code>parent_id</code> field of the returned <code>LIBMTP_track_t</code>
4197 * struct. To arrange by folders or files you typically have to create the proper
4198 * trees by calls to <code>LIBMTP_Get_Storage()</code> and/or
4199 * <code>LIBMTP_Get_Folder_List()</code> first.
4200 *
Linus Walleijb9256fd2006-02-15 09:40:43 +00004201 * @param device a pointer to the device to get the track listing for.
Linus Walleij3fcfea52006-11-13 07:07:36 +00004202 * @param callback a function to be called during the tracklisting retrieveal
Linus Walleij25d5c212008-08-14 06:49:13 +00004203 * for displaying progress bars etc, or NULL if you don't want
4204 * any callbacks.
Richard Lowdc0b6c72006-11-13 09:22:23 +00004205 * @param data a user-defined pointer that is passed along to
Linus Walleij25d5c212008-08-14 06:49:13 +00004206 * the <code>progress</code> function in order to
4207 * pass along some user defined data to the progress
4208 * updates. If not used, set this to NULL.
Linus Walleija4982732006-02-24 15:46:02 +00004209 * @return a list of tracks that can be followed using the <code>next</code>
Linus Walleij25d5c212008-08-14 06:49:13 +00004210 * field of the <code>LIBMTP_track_t</code> data structure.
4211 * Each of the metadata tags must be freed after use, and may
4212 * contain only partial metadata information, i.e. one or several
4213 * fields may be NULL or 0.
Linus Walleij2e4b5f92006-06-16 14:00:49 +00004214 * @see LIBMTP_Get_Trackmetadata()
Linus Walleijb9256fd2006-02-15 09:40:43 +00004215 */
Richard Lowdc0b6c72006-11-13 09:22:23 +00004216LIBMTP_track_t *LIBMTP_Get_Tracklisting_With_Callback(LIBMTP_mtpdevice_t *device,
4217 LIBMTP_progressfunc_t const callback,
4218 void const * const data)
Linus Walleijb9256fd2006-02-15 09:40:43 +00004219{
4220 uint32_t i = 0;
4221 LIBMTP_track_t *retracks = NULL;
4222 LIBMTP_track_t *curtrack = NULL;
Linus Walleij9b28da32006-03-16 13:47:58 +00004223 PTPParams *params = (PTPParams *) device->params;
Linus Walleij2be40c72007-03-16 15:19:44 +00004224 PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo;
Linus Walleij438bd7f2006-06-08 11:35:44 +00004225
mopoke96143402006-10-30 04:37:26 +00004226 // Get all the handles if we haven't already done that
Linus Walleijd4637502009-06-14 23:03:33 +00004227 if (params->nrofobjects == 0) {
Linus Walleij438bd7f2006-06-08 11:35:44 +00004228 flush_handles(device);
Linus Walleijb9256fd2006-02-15 09:40:43 +00004229 }
mopoke96143402006-10-30 04:37:26 +00004230
Linus Walleijd4637502009-06-14 23:03:33 +00004231 for (i = 0; i < params->nrofobjects; i++) {
Linus Walleij070e9b42007-01-22 08:49:28 +00004232 LIBMTP_track_t *track;
Linus Walleijd4637502009-06-14 23:03:33 +00004233 PTPObject *ob;
Linus Walleij46651f32008-04-26 23:30:19 +00004234 LIBMTP_filetype_t mtptype;
mopoke31364442006-11-20 04:53:04 +00004235
Richard Lowdc0b6c72006-11-13 09:22:23 +00004236 if (callback != NULL)
Linus Walleijd4637502009-06-14 23:03:33 +00004237 callback(i, params->nrofobjects, data);
mopoke31364442006-11-20 04:53:04 +00004238
Linus Walleijd4637502009-06-14 23:03:33 +00004239 ob = &params->objects[i];
4240 mtptype = map_ptp_type_to_libmtp_type(ob->oi.ObjectFormat);
mopoke96143402006-10-30 04:37:26 +00004241
Linus Walleijf0bf4372007-07-01 21:47:38 +00004242 // Ignore stuff we don't know how to handle...
4243 // TODO: get this list as an intersection of the sets
4244 // supported by the device and the from the device and
Linus Walleijd5356e22008-11-01 22:08:59 +00004245 // all known track files?
4246 if (!LIBMTP_FILETYPE_IS_TRACK(mtptype) &&
Linus Walleij46651f32008-04-26 23:30:19 +00004247 // This row lets through undefined files for examination since they may be forgotten OGG files.
Linus Walleijd4637502009-06-14 23:03:33 +00004248 (ob->oi.ObjectFormat != PTP_OFC_Undefined ||
Richard Lowef197312008-11-01 18:29:41 +00004249 (!FLAG_IRIVER_OGG_ALZHEIMER(ptp_usb) &&
Linus Walleij89bb1cd2009-07-24 21:03:36 +00004250 !FLAG_OGG_IS_UNKNOWN(ptp_usb) &&
4251 !FLAG_FLAC_IS_UNKNOWN(ptp_usb)))
Linus Walleij46651f32008-04-26 23:30:19 +00004252 ) {
Linus Walleijc8abc922008-09-28 18:38:42 +00004253 //printf("Not a music track (name: %s format: %d), skipping...\n", oi->Filename, oi->ObjectFormat);
Linus Walleijf0bf4372007-07-01 21:47:38 +00004254 continue;
Linus Walleijb9256fd2006-02-15 09:40:43 +00004255 }
4256
Linus Walleijf0bf4372007-07-01 21:47:38 +00004257 // Allocate a new track type
4258 track = LIBMTP_new_track_t();
4259
4260 // This is some sort of unique ID so we can keep track of the track.
Linus Walleijd4637502009-06-14 23:03:33 +00004261 track->item_id = ob->oid;
4262 track->parent_id = ob->oi.ParentObject;
4263 track->storage_id = ob->oi.StorageID;
4264 track->modificationdate = ob->oi.ModificationDate;
Linus Walleijf0bf4372007-07-01 21:47:38 +00004265
Linus Walleij46651f32008-04-26 23:30:19 +00004266 track->filetype = mtptype;
Linus Walleijf0bf4372007-07-01 21:47:38 +00004267
4268 // Original file-specific properties
Linus Walleijd4637502009-06-14 23:03:33 +00004269 track->filesize = ob->oi.ObjectCompressedSize;
4270 if (ob->oi.Filename != NULL) {
4271 track->filename = strdup(ob->oi.Filename);
Linus Walleijf0bf4372007-07-01 21:47:38 +00004272 }
4273
Linus Walleijd4637502009-06-14 23:03:33 +00004274 get_track_metadata(device, ob->oi.ObjectFormat, track);
Linus Walleijf0bf4372007-07-01 21:47:38 +00004275
4276 /*
4277 * A special quirk for iriver devices that doesn't quite
4278 * remember that some files marked as "unknown" type are
Linus Walleij89bb1cd2009-07-24 21:03:36 +00004279 * actually OGG or FLAC files. We look at the filename extension
4280 * and see if it happens that this was atleast named "ogg" or "flac"
4281 * and fall back on this heuristic approach in that case,
Linus Walleijf0bf4372007-07-01 21:47:38 +00004282 * for these bugged devices only.
4283 */
4284 if (track->filetype == LIBMTP_FILETYPE_UNKNOWN &&
Linus Walleij89bb1cd2009-07-24 21:03:36 +00004285 track->filename != NULL) {
4286 if ((FLAG_IRIVER_OGG_ALZHEIMER(ptp_usb) ||
4287 FLAG_OGG_IS_UNKNOWN(ptp_usb)) &&
4288 has_ogg_extension(track->filename))
Linus Walleijf0bf4372007-07-01 21:47:38 +00004289 track->filetype = LIBMTP_FILETYPE_OGG;
Linus Walleij89bb1cd2009-07-24 21:03:36 +00004290 else if (FLAG_FLAC_IS_UNKNOWN(ptp_usb) &&
4291 has_flac_extension(track->filename))
4292 track->filetype = LIBMTP_FILETYPE_FLAC;
4293 else {
4294 // This was not an OGG/FLAC file so discard it and continue
Linus Walleijf0bf4372007-07-01 21:47:38 +00004295 LIBMTP_destroy_track_t(track);
4296 continue;
4297 }
4298 }
4299
4300 // Add track to a list that will be returned afterwards.
4301 if (retracks == NULL) {
4302 retracks = track;
4303 curtrack = track;
4304 } else {
4305 curtrack->next = track;
4306 curtrack = track;
4307 }
4308
4309 // Call listing callback
4310 // double progressPercent = (double)i*(double)100.0 / (double)params->handles.n;
4311
4312
Linus Walleijb9256fd2006-02-15 09:40:43 +00004313 } // Handle counting loop
4314 return retracks;
4315}
Linus Walleijdcde6082006-02-17 16:16:34 +00004316
Linus Walleij2e4b5f92006-06-16 14:00:49 +00004317/**
4318 * This function retrieves the metadata for a single track off
4319 * the device.
4320 *
4321 * Do not call this function repeatedly! The track handles are linearly
4322 * searched O(n) and the call may involve (slow) USB traffic, so use
4323 * <code>LIBMTP_Get_Tracklisting()</code> and cache the tracks, preferably
4324 * as an efficient data structure such as a hash list.
4325 *
4326 * @param device a pointer to the device to get the track metadata from.
4327 * @param trackid the object ID of the track that you want the metadata for.
4328 * @return a track metadata entry on success or NULL on failure.
4329 * @see LIBMTP_Get_Tracklisting()
4330 */
4331LIBMTP_track_t *LIBMTP_Get_Trackmetadata(LIBMTP_mtpdevice_t *device, uint32_t const trackid)
4332{
Linus Walleij2e4b5f92006-06-16 14:00:49 +00004333 PTPParams *params = (PTPParams *) device->params;
Linus Walleijc8abc922008-09-28 18:38:42 +00004334 PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo;
Linus Walleijd4637502009-06-14 23:03:33 +00004335 PTPObject *ob;
4336 LIBMTP_track_t *track;
4337 LIBMTP_filetype_t mtptype;
4338 uint16_t ret;
Linus Walleij2e4b5f92006-06-16 14:00:49 +00004339
mopoke96143402006-10-30 04:37:26 +00004340 // Get all the handles if we haven't already done that
Linus Walleijd4637502009-06-14 23:03:33 +00004341 if (params->nrofobjects == 0)
Linus Walleij2e4b5f92006-06-16 14:00:49 +00004342 flush_handles(device);
Linus Walleijd4637502009-06-14 23:03:33 +00004343
4344 ret = ptp_object_want (params, trackid, PTPOBJECT_OBJECTINFO_LOADED, &ob);
4345 if (ret != PTP_RC_OK)
4346 return NULL;
4347
4348 mtptype = map_ptp_type_to_libmtp_type(ob->oi.ObjectFormat);
4349
4350 // Ignore stuff we don't know how to handle...
4351 if (!LIBMTP_FILETYPE_IS_TRACK(mtptype) &&
Linus Walleij89bb1cd2009-07-24 21:03:36 +00004352 /*
4353 * This row lets through undefined files for examination
4354 * since they may be forgotten OGG or FLAC files.
4355 */
Linus Walleijd4637502009-06-14 23:03:33 +00004356 (ob->oi.ObjectFormat != PTP_OFC_Undefined ||
4357 (!FLAG_IRIVER_OGG_ALZHEIMER(ptp_usb) &&
Linus Walleij89bb1cd2009-07-24 21:03:36 +00004358 !FLAG_OGG_IS_UNKNOWN(ptp_usb) &&
4359 !FLAG_FLAC_IS_UNKNOWN(ptp_usb)))
Linus Walleijd4637502009-06-14 23:03:33 +00004360 ) {
4361 //printf("Not a music track (name: %s format: %d), skipping...\n", oi->Filename, oi->ObjectFormat);
4362 return NULL;
Linus Walleij2e4b5f92006-06-16 14:00:49 +00004363 }
4364
Linus Walleijd4637502009-06-14 23:03:33 +00004365 // Allocate a new track type
4366 track = LIBMTP_new_track_t();
4367
4368 // This is some sort of unique ID so we can keep track of the track.
4369 track->item_id = ob->oid;
4370 track->parent_id = ob->oi.ParentObject;
4371 track->storage_id = ob->oi.StorageID;
4372 track->modificationdate = ob->oi.ModificationDate;
mopoke96143402006-10-30 04:37:26 +00004373
Linus Walleijd4637502009-06-14 23:03:33 +00004374 track->filetype = mtptype;
Linus Walleij2e4b5f92006-06-16 14:00:49 +00004375
Linus Walleijd4637502009-06-14 23:03:33 +00004376 // Original file-specific properties
4377 track->filesize = ob->oi.ObjectCompressedSize;
4378 if (ob->oi.Filename != NULL) {
4379 track->filename = strdup(ob->oi.Filename);
4380 }
mopoke96143402006-10-30 04:37:26 +00004381
Linus Walleijd4637502009-06-14 23:03:33 +00004382 /*
Linus Walleij89bb1cd2009-07-24 21:03:36 +00004383 * A special quirk for devices that doesn't quite
Linus Walleijd4637502009-06-14 23:03:33 +00004384 * remember that some files marked as "unknown" type are
Linus Walleij89bb1cd2009-07-24 21:03:36 +00004385 * actually OGG or FLAC files. We look at the filename extension
Linus Walleijd4637502009-06-14 23:03:33 +00004386 * and see if it happens that this was atleast named "ogg"
4387 * and fall back on this heuristic approach in that case,
4388 * for these bugged devices only.
4389 */
4390 if (track->filetype == LIBMTP_FILETYPE_UNKNOWN &&
Linus Walleij89bb1cd2009-07-24 21:03:36 +00004391 track->filename != NULL) {
4392 if ((FLAG_IRIVER_OGG_ALZHEIMER(ptp_usb) ||
4393 FLAG_OGG_IS_UNKNOWN(ptp_usb)) &&
4394 has_ogg_extension(track->filename))
Linus Walleijd4637502009-06-14 23:03:33 +00004395 track->filetype = LIBMTP_FILETYPE_OGG;
Linus Walleij89bb1cd2009-07-24 21:03:36 +00004396 else if (FLAG_FLAC_IS_UNKNOWN(ptp_usb) &&
4397 has_flac_extension(track->filename))
4398 track->filetype = LIBMTP_FILETYPE_FLAC;
4399 else {
4400 // This was not an OGG/FLAC file so discard it
Linus Walleijd4637502009-06-14 23:03:33 +00004401 LIBMTP_destroy_track_t(track);
Linus Walleij2e4b5f92006-06-16 14:00:49 +00004402 return NULL;
4403 }
Linus Walleij2e4b5f92006-06-16 14:00:49 +00004404 }
Linus Walleijd4637502009-06-14 23:03:33 +00004405 get_track_metadata(device, ob->oi.ObjectFormat, track);
4406 return track;
Linus Walleij2e4b5f92006-06-16 14:00:49 +00004407}
4408
Richard Low5b4023c2009-04-16 19:14:38 +00004409/**
4410 * This is a manual conversion from MTPDataGetFunc to PTPDataGetFunc
4411 * to isolate the internal type.
4412 */
Linus Walleijd866d242009-08-23 21:50:39 +00004413static 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 +00004414{
Linus Walleijd866d242009-08-23 21:50:39 +00004415 MTPDataHandler *handler = (MTPDataHandler *)priv;
Richard Low5b4023c2009-04-16 19:14:38 +00004416 uint16_t ret;
4417 uint32_t local_gotlen = 0;
Linus Walleijd866d242009-08-23 21:50:39 +00004418 ret = handler->getfunc(params, handler->priv, wantlen, data, &local_gotlen);
Richard Low5b4023c2009-04-16 19:14:38 +00004419 *gotlen = local_gotlen;
Richard Low6f070842009-05-03 10:26:16 +00004420 switch (ret)
4421 {
4422 case LIBMTP_HANDLER_RETURN_OK:
4423 return PTP_RC_OK;
4424 case LIBMTP_HANDLER_RETURN_ERROR:
4425 return PTP_ERROR_IO;
4426 case LIBMTP_HANDLER_RETURN_CANCEL:
4427 return PTP_ERROR_CANCEL;
4428 default:
4429 return PTP_ERROR_IO;
4430 }
Richard Low5b4023c2009-04-16 19:14:38 +00004431}
4432
4433/**
4434 * This is a manual conversion from MTPDataPutFunc to PTPDataPutFunc
4435 * to isolate the internal type.
4436 */
Linus Walleijd866d242009-08-23 21:50:39 +00004437static uint16_t put_func_wrapper(PTPParams* params, void* priv, unsigned long sendlen, unsigned char *data, unsigned long *putlen)
Richard Low5b4023c2009-04-16 19:14:38 +00004438{
Linus Walleijd866d242009-08-23 21:50:39 +00004439 MTPDataHandler *handler = (MTPDataHandler *)priv;
Richard Low5b4023c2009-04-16 19:14:38 +00004440 uint16_t ret;
4441 uint32_t local_putlen = 0;
Linus Walleijd866d242009-08-23 21:50:39 +00004442 ret = handler->putfunc(params, handler->priv, sendlen, data, &local_putlen);
Richard Low5b4023c2009-04-16 19:14:38 +00004443 *putlen = local_putlen;
Richard Low75981ac2009-05-03 12:54:06 +00004444 switch (ret)
4445 {
4446 case LIBMTP_HANDLER_RETURN_OK:
4447 return PTP_RC_OK;
4448 case LIBMTP_HANDLER_RETURN_ERROR:
4449 return PTP_ERROR_IO;
4450 case LIBMTP_HANDLER_RETURN_CANCEL:
4451 return PTP_ERROR_CANCEL;
4452 default:
4453 return PTP_ERROR_IO;
4454 }
Richard Low5b4023c2009-04-16 19:14:38 +00004455}
Linus Walleijf6bc1782006-03-24 15:12:47 +00004456
4457/**
4458 * This gets a file off the device to a local file identified
4459 * by a filename.
4460 * @param device a pointer to the device to get the track from.
4461 * @param id the file ID of the file to retrieve.
4462 * @param path a filename to use for the retrieved file.
4463 * @param callback a progress indicator function or NULL to ignore.
4464 * @param data a user-defined pointer that is passed along to
4465 * the <code>progress</code> function in order to
4466 * pass along some user defined data to the progress
4467 * updates. If not used, set this to NULL.
mopoke96143402006-10-30 04:37:26 +00004468 * @return 0 if the transfer was successful, any other value means
Linus Walleijf6bc1782006-03-24 15:12:47 +00004469 * failure.
4470 * @see LIBMTP_Get_File_To_File_Descriptor()
4471 */
mopoke96143402006-10-30 04:37:26 +00004472int LIBMTP_Get_File_To_File(LIBMTP_mtpdevice_t *device, uint32_t const id,
Linus Walleijee73ef22006-08-27 19:56:00 +00004473 char const * const path, LIBMTP_progressfunc_t const callback,
Linus Walleijf6bc1782006-03-24 15:12:47 +00004474 void const * const data)
4475{
4476 int fd = -1;
4477 int ret;
4478
4479 // Sanity check
4480 if (path == NULL) {
Linus Walleij070e9b42007-01-22 08:49:28 +00004481 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 +00004482 return -1;
4483 }
4484
4485 // Open file
4486#ifdef __WIN32__
Linus Walleij7beba572006-11-29 08:56:12 +00004487#ifdef USE_WINDOWS_IO_H
4488 if ( (fd = _open(path, O_RDWR|O_CREAT|O_TRUNC|O_BINARY,_S_IREAD)) == -1 ) {
4489#else
Linus Walleij5b4a4d22009-01-10 12:18:14 +00004490 if ( (fd = open(path, O_RDWR|O_CREAT|O_TRUNC|O_BINARY,S_IRWXU)) == -1 ) {
Linus Walleij7beba572006-11-29 08:56:12 +00004491#endif
Linus Walleijf6bc1782006-03-24 15:12:47 +00004492#else
4493 if ( (fd = open(path, O_RDWR|O_CREAT|O_TRUNC,S_IRWXU|S_IRGRP)) == -1) {
4494#endif
Linus Walleij070e9b42007-01-22 08:49:28 +00004495 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Get_File_To_File(): Could not create file.");
Linus Walleijf6bc1782006-03-24 15:12:47 +00004496 return -1;
4497 }
mopoke96143402006-10-30 04:37:26 +00004498
Linus Walleijf6bc1782006-03-24 15:12:47 +00004499 ret = LIBMTP_Get_File_To_File_Descriptor(device, id, fd, callback, data);
4500
4501 // Close file
4502 close(fd);
mopoke96143402006-10-30 04:37:26 +00004503
Linus Walleij25d33b52007-09-16 21:36:19 +00004504 // Delete partial file.
4505 if (ret == -1) {
4506 unlink(path);
4507 }
4508
Linus Walleijf6bc1782006-03-24 15:12:47 +00004509 return ret;
4510}
4511
4512/**
4513 * This gets a file off the device to a file identified
4514 * by a file descriptor.
Linus Walleij0558ac52006-09-07 06:55:03 +00004515 *
mopoke96143402006-10-30 04:37:26 +00004516 * This function can potentially be used for streaming
4517 * files off the device for playback or broadcast for example,
Linus Walleij0558ac52006-09-07 06:55:03 +00004518 * by downloading the file into a stream sink e.g. a socket.
4519 *
Linus Walleijf6bc1782006-03-24 15:12:47 +00004520 * @param device a pointer to the device to get the file from.
4521 * @param id the file ID of the file to retrieve.
4522 * @param fd a local file descriptor to write the file to.
4523 * @param callback a progress indicator function or NULL to ignore.
4524 * @param data a user-defined pointer that is passed along to
4525 * the <code>progress</code> function in order to
4526 * pass along some user defined data to the progress
4527 * updates. If not used, set this to NULL.
mopoke96143402006-10-30 04:37:26 +00004528 * @return 0 if the transfer was successful, any other value means
Linus Walleijf6bc1782006-03-24 15:12:47 +00004529 * failure.
4530 * @see LIBMTP_Get_File_To_File()
4531 */
mopoke96143402006-10-30 04:37:26 +00004532int LIBMTP_Get_File_To_File_Descriptor(LIBMTP_mtpdevice_t *device,
4533 uint32_t const id,
4534 int const fd,
Linus Walleijee73ef22006-08-27 19:56:00 +00004535 LIBMTP_progressfunc_t const callback,
Linus Walleijf6bc1782006-03-24 15:12:47 +00004536 void const * const data)
4537{
Linus Walleij438bd7f2006-06-08 11:35:44 +00004538 uint16_t ret;
Linus Walleijf6bc1782006-03-24 15:12:47 +00004539 PTPParams *params = (PTPParams *) device->params;
Linus Walleijee73ef22006-08-27 19:56:00 +00004540 PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo;
Linus Walleijd4637502009-06-14 23:03:33 +00004541 PTPObject *ob;
Linus Walleijf6bc1782006-03-24 15:12:47 +00004542
Linus Walleijd4637502009-06-14 23:03:33 +00004543 ret = ptp_object_want (params, id, PTPOBJECT_OBJECTINFO_LOADED, &ob);
4544 if (ret != PTP_RC_OK) {
Linus Walleijf0bf4372007-07-01 21:47:38 +00004545 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 +00004546 return -1;
4547 }
Linus Walleijd4637502009-06-14 23:03:33 +00004548 if (ob->oi.ObjectFormat == PTP_OFC_Association) {
Linus Walleij070e9b42007-01-22 08:49:28 +00004549 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Get_File_To_File_Descriptor(): Bad object format.");
Linus Walleijf6bc1782006-03-24 15:12:47 +00004550 return -1;
4551 }
Linus Walleijf6bc1782006-03-24 15:12:47 +00004552
Linus Walleijee73ef22006-08-27 19:56:00 +00004553 // Callbacks
4554 ptp_usb->callback_active = 1;
Linus Walleijd4637502009-06-14 23:03:33 +00004555 ptp_usb->current_transfer_total = ob->oi.ObjectCompressedSize+
Linus Walleij7f0c72e2006-09-06 13:01:58 +00004556 PTP_USB_BULK_HDR_LEN+sizeof(uint32_t); // Request length, one parameter
Linus Walleijee73ef22006-08-27 19:56:00 +00004557 ptp_usb->current_transfer_complete = 0;
4558 ptp_usb->current_transfer_callback = callback;
4559 ptp_usb->current_transfer_callback_data = data;
mopoke96143402006-10-30 04:37:26 +00004560
Linus Walleij96c62432006-08-21 10:04:02 +00004561 ret = ptp_getobject_tofd(params, id, fd);
Linus Walleijee73ef22006-08-27 19:56:00 +00004562
4563 ptp_usb->callback_active = 0;
4564 ptp_usb->current_transfer_callback = NULL;
4565 ptp_usb->current_transfer_callback_data = NULL;
mopoke96143402006-10-30 04:37:26 +00004566
Linus Walleijbd3bf9e2007-09-14 19:31:54 +00004567 if (ret == PTP_ERROR_CANCEL) {
Linus Walleijfb28b632007-10-23 21:56:18 +00004568 add_error_to_errorstack(device, LIBMTP_ERROR_CANCELLED, "LIBMTP_Get_File_From_File_Descriptor(): Cancelled transfer.");
Linus Walleijbd3bf9e2007-09-14 19:31:54 +00004569 return -1;
4570 }
Linus Walleijb02a0662006-04-25 08:05:09 +00004571 if (ret != PTP_RC_OK) {
Linus Walleij070e9b42007-01-22 08:49:28 +00004572 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 +00004573 return -1;
4574 }
mopoke96143402006-10-30 04:37:26 +00004575
Linus Walleijf6bc1782006-03-24 15:12:47 +00004576 return 0;
4577}
4578
Linus Walleijdcde6082006-02-17 16:16:34 +00004579/**
Richard Lowd3b17022009-04-11 12:37:39 +00004580 * This gets a file off the device and calls put_func
4581 * with chunks of data
4582 *
4583 * @param device a pointer to the device to get the file from.
4584 * @param id the file ID of the file to retrieve.
4585 * @param put_func the function to call when we have data.
4586 * @param priv the user-defined pointer that is passed to
4587 * <code>put_func</code>.
4588 * @param callback a progress indicator function or NULL to ignore.
4589 * @param data a user-defined pointer that is passed along to
4590 * the <code>progress</code> function in order to
4591 * pass along some user defined data to the progress
4592 * updates. If not used, set this to NULL.
4593 * @return 0 if the transfer was successful, any other value means
4594 * failure.
4595 */
4596int LIBMTP_Get_File_To_Handler(LIBMTP_mtpdevice_t *device,
4597 uint32_t const id,
4598 MTPDataPutFunc put_func,
4599 void * priv,
4600 LIBMTP_progressfunc_t const callback,
4601 void const * const data)
4602{
Linus Walleijd4637502009-06-14 23:03:33 +00004603 PTPObject *ob;
Richard Lowd3b17022009-04-11 12:37:39 +00004604 uint16_t ret;
4605 PTPParams *params = (PTPParams *) device->params;
4606 PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo;
4607
Linus Walleijd4637502009-06-14 23:03:33 +00004608 ret = ptp_object_want (params, id, PTPOBJECT_OBJECTINFO_LOADED, &ob);
4609 if (ret != PTP_RC_OK) {
Richard Lowd3b17022009-04-11 12:37:39 +00004610 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Get_File_To_File_Descriptor(): Could not get object info.");
4611 return -1;
4612 }
Linus Walleijd4637502009-06-14 23:03:33 +00004613 if (ob->oi.ObjectFormat == PTP_OFC_Association) {
Richard Lowd3b17022009-04-11 12:37:39 +00004614 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Get_File_To_File_Descriptor(): Bad object format.");
4615 return -1;
4616 }
4617
4618 // Callbacks
4619 ptp_usb->callback_active = 1;
Linus Walleijd4637502009-06-14 23:03:33 +00004620 ptp_usb->current_transfer_total = ob->oi.ObjectCompressedSize+
Richard Lowd3b17022009-04-11 12:37:39 +00004621 PTP_USB_BULK_HDR_LEN+sizeof(uint32_t); // Request length, one parameter
4622 ptp_usb->current_transfer_complete = 0;
4623 ptp_usb->current_transfer_callback = callback;
4624 ptp_usb->current_transfer_callback_data = data;
4625
Richard Low5b4023c2009-04-16 19:14:38 +00004626 MTPDataHandler mtp_handler;
4627 mtp_handler.getfunc = NULL;
4628 mtp_handler.putfunc = put_func;
Linus Walleijd866d242009-08-23 21:50:39 +00004629 mtp_handler.priv = priv;
4630
Richard Lowd3b17022009-04-11 12:37:39 +00004631 PTPDataHandler handler;
4632 handler.getfunc = NULL;
Richard Low5b4023c2009-04-16 19:14:38 +00004633 handler.putfunc = put_func_wrapper;
Linus Walleijd866d242009-08-23 21:50:39 +00004634 handler.priv = &mtp_handler;
4635
Richard Lowd3b17022009-04-11 12:37:39 +00004636 ret = ptp_getobject_to_handler(params, id, &handler);
4637
4638 ptp_usb->callback_active = 0;
4639 ptp_usb->current_transfer_callback = NULL;
4640 ptp_usb->current_transfer_callback_data = NULL;
4641
4642 if (ret == PTP_ERROR_CANCEL) {
4643 add_error_to_errorstack(device, LIBMTP_ERROR_CANCELLED, "LIBMTP_Get_File_From_File_Descriptor(): Cancelled transfer.");
4644 return -1;
4645 }
4646 if (ret != PTP_RC_OK) {
4647 add_ptp_error_to_errorstack(device, ret, "LIBMTP_Get_File_To_File_Descriptor(): Could not get file from device.");
4648 return -1;
4649 }
4650
4651 return 0;
4652}
4653
4654
4655/**
Linus Walleijdcde6082006-02-17 16:16:34 +00004656 * This gets a track off the device to a file identified
Linus Walleijf6bc1782006-03-24 15:12:47 +00004657 * by a filename. This is actually just a wrapper for the
4658 * \c LIBMTP_Get_Track_To_File() function.
Linus Walleijdcde6082006-02-17 16:16:34 +00004659 * @param device a pointer to the device to get the track from.
4660 * @param id the track ID of the track to retrieve.
4661 * @param path a filename to use for the retrieved track.
4662 * @param callback a progress indicator function or NULL to ignore.
4663 * @param data a user-defined pointer that is passed along to
4664 * the <code>progress</code> function in order to
4665 * pass along some user defined data to the progress
4666 * updates. If not used, set this to NULL.
mopoke96143402006-10-30 04:37:26 +00004667 * @return 0 if the transfer was successful, any other value means
Linus Walleijdcde6082006-02-17 16:16:34 +00004668 * failure.
4669 * @see LIBMTP_Get_Track_To_File_Descriptor()
4670 */
mopoke96143402006-10-30 04:37:26 +00004671int LIBMTP_Get_Track_To_File(LIBMTP_mtpdevice_t *device, uint32_t const id,
Linus Walleijee73ef22006-08-27 19:56:00 +00004672 char const * const path, LIBMTP_progressfunc_t const callback,
Linus Walleij0cd85432006-02-20 14:37:50 +00004673 void const * const data)
Linus Walleijdcde6082006-02-17 16:16:34 +00004674{
Linus Walleijf6bc1782006-03-24 15:12:47 +00004675 // This is just a wrapper
4676 return LIBMTP_Get_File_To_File(device, id, path, callback, data);
Linus Walleijdcde6082006-02-17 16:16:34 +00004677}
4678
4679/**
4680 * This gets a track off the device to a file identified
Linus Walleijf6bc1782006-03-24 15:12:47 +00004681 * by a file descriptor. This is actually just a wrapper for
4682 * the \c LIBMTP_Get_File_To_File_Descriptor() function.
Linus Walleijdcde6082006-02-17 16:16:34 +00004683 * @param device a pointer to the device to get the track from.
4684 * @param id the track ID of the track to retrieve.
4685 * @param fd a file descriptor to write the track to.
4686 * @param callback a progress indicator function or NULL to ignore.
4687 * @param data a user-defined pointer that is passed along to
4688 * the <code>progress</code> function in order to
4689 * pass along some user defined data to the progress
4690 * updates. If not used, set this to NULL.
mopoke96143402006-10-30 04:37:26 +00004691 * @return 0 if the transfer was successful, any other value means
Linus Walleijdcde6082006-02-17 16:16:34 +00004692 * failure.
4693 * @see LIBMTP_Get_Track_To_File()
4694 */
mopoke96143402006-10-30 04:37:26 +00004695int LIBMTP_Get_Track_To_File_Descriptor(LIBMTP_mtpdevice_t *device,
4696 uint32_t const id,
4697 int const fd,
Linus Walleijee73ef22006-08-27 19:56:00 +00004698 LIBMTP_progressfunc_t const callback,
Linus Walleij0cd85432006-02-20 14:37:50 +00004699 void const * const data)
Linus Walleijdcde6082006-02-17 16:16:34 +00004700{
Linus Walleijf6bc1782006-03-24 15:12:47 +00004701 // This is just a wrapper
4702 return LIBMTP_Get_File_To_File_Descriptor(device, id, fd, callback, data);
Linus Walleijdcde6082006-02-17 16:16:34 +00004703}
Linus Walleij394bbbe2006-02-22 16:10:53 +00004704
4705/**
Richard Lowd3b17022009-04-11 12:37:39 +00004706 * This gets a track off the device to a handler function.
4707 * This is actually just a wrapper for
4708 * the \c LIBMTP_Get_File_To_Handler() function.
4709 * @param device a pointer to the device to get the track from.
4710 * @param id the track ID of the track to retrieve.
4711 * @param put_func the function to call when we have data.
4712 * @param priv the user-defined pointer that is passed to
4713 * <code>put_func</code>.
4714 * @param callback a progress indicator function or NULL to ignore.
4715 * @param data a user-defined pointer that is passed along to
4716 * the <code>progress</code> function in order to
4717 * pass along some user defined data to the progress
4718 * updates. If not used, set this to NULL.
4719 * @return 0 if the transfer was successful, any other value means
4720 * failure.
4721 */
4722int LIBMTP_Get_Track_To_Handler(LIBMTP_mtpdevice_t *device,
4723 uint32_t const id,
4724 MTPDataPutFunc put_func,
4725 void * priv,
4726 LIBMTP_progressfunc_t const callback,
4727 void const * const data)
4728{
4729 // This is just a wrapper
4730 return LIBMTP_Get_File_To_Handler(device, id, put_func, priv, callback, data);
4731}
4732
4733/**
Linus Walleij394bbbe2006-02-22 16:10:53 +00004734 * This function sends a track from a local file to an
4735 * MTP device. A filename and a set of metadata must be
4736 * given as input.
4737 * @param device a pointer to the device to send the track to.
4738 * @param path the filename of a local file which will be sent.
4739 * @param metadata a track metadata set to be written along with the file.
Linus Walleijea68f1f2008-06-22 21:54:44 +00004740 * After this call the field <code>metadata-&gt;item_id</code>
4741 * will contain the new track ID. Other fields such
4742 * as the <code>metadata-&gt;filename</code>, <code>metadata-&gt;parent_id</code>
4743 * or <code>metadata-&gt;storage_id</code> may also change during this
4744 * operation due to device restrictions, so do not rely on the
4745 * contents of this struct to be preserved in any way.
4746 * <ul>
4747 * <li><code>metadata-&gt;parent_id</code> should be set to the parent
4748 * (e.g. folder) to store this track in. Since some
4749 * devices are a bit picky about where files
4750 * are placed, a default folder will be chosen if libmtp
4751 * has detected one for the current filetype and this
4752 * parameter is set to 0. If this is 0 and no default folder
4753 * can be found, the file will be stored in the root folder.
4754 * <li><code>metadata-&gt;storage_id</code> should be set to the
4755 * desired storage (e.g. memory card or whatever your device
4756 * presents) to store this track in. Setting this to 0 will store
4757 * the track on the primary storage.
4758 * </ul>
Linus Walleij394bbbe2006-02-22 16:10:53 +00004759 * @param callback a progress indicator function or NULL to ignore.
4760 * @param data a user-defined pointer that is passed along to
4761 * the <code>progress</code> function in order to
4762 * pass along some user defined data to the progress
4763 * updates. If not used, set this to NULL.
mopoke96143402006-10-30 04:37:26 +00004764 * @return 0 if the transfer was successful, any other value means
Linus Walleij394bbbe2006-02-22 16:10:53 +00004765 * failure.
4766 * @see LIBMTP_Send_Track_From_File_Descriptor()
Linus Walleij1b87ea72007-08-28 07:53:09 +00004767 * @see LIBMTP_Send_File_From_File()
Linus Walleij438bd7f2006-06-08 11:35:44 +00004768 * @see LIBMTP_Delete_Object()
Linus Walleij394bbbe2006-02-22 16:10:53 +00004769 */
mopoke96143402006-10-30 04:37:26 +00004770int LIBMTP_Send_Track_From_File(LIBMTP_mtpdevice_t *device,
Linus Walleij394bbbe2006-02-22 16:10:53 +00004771 char const * const path, LIBMTP_track_t * const metadata,
Linus Walleijee73ef22006-08-27 19:56:00 +00004772 LIBMTP_progressfunc_t const callback,
Linus Walleijea68f1f2008-06-22 21:54:44 +00004773 void const * const data)
Linus Walleij394bbbe2006-02-22 16:10:53 +00004774{
4775 int fd;
4776 int ret;
4777
4778 // Sanity check
4779 if (path == NULL) {
Linus Walleij070e9b42007-01-22 08:49:28 +00004780 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 +00004781 return -1;
4782 }
4783
4784 // Open file
4785#ifdef __WIN32__
Linus Walleij7beba572006-11-29 08:56:12 +00004786#ifdef USE_WINDOWS_IO_H
Linus Walleij5b4a4d22009-01-10 12:18:14 +00004787 if ( (fd = _open(path, O_RDONLY|O_BINARY) == -1) ) {
Linus Walleij7beba572006-11-29 08:56:12 +00004788#else
Linus Walleij5b4a4d22009-01-10 12:18:14 +00004789 if ( (fd = open(path, O_RDONLY|O_BINARY) == -1) ) {
Linus Walleij7beba572006-11-29 08:56:12 +00004790#endif
Linus Walleij394bbbe2006-02-22 16:10:53 +00004791#else
4792 if ( (fd = open(path, O_RDONLY)) == -1) {
4793#endif
Linus Walleijd6a49972006-05-02 08:24:54 +00004794 printf("LIBMTP_Send_Track_From_File(): Could not open source file \"%s\"\n", path);
Linus Walleij394bbbe2006-02-22 16:10:53 +00004795 return -1;
4796 }
4797
Linus Walleijea68f1f2008-06-22 21:54:44 +00004798 ret = LIBMTP_Send_Track_From_File_Descriptor(device, fd, metadata, callback, data);
mopoke96143402006-10-30 04:37:26 +00004799
Linus Walleij394bbbe2006-02-22 16:10:53 +00004800 // Close file.
Linus Walleij7beba572006-11-29 08:56:12 +00004801#ifdef USE_WINDOWS_IO_H
4802 _close(fd);
4803#else
Linus Walleij394bbbe2006-02-22 16:10:53 +00004804 close(fd);
Linus Walleij7beba572006-11-29 08:56:12 +00004805#endif
Linus Walleij394bbbe2006-02-22 16:10:53 +00004806
4807 return ret;
4808}
4809
Linus Walleijfa1374c2006-02-27 07:41:46 +00004810/**
Linus Walleij394bbbe2006-02-22 16:10:53 +00004811 * This function sends a track from a file descriptor to an
4812 * MTP device. A filename and a set of metadata must be
4813 * given as input.
4814 * @param device a pointer to the device to send the track to.
4815 * @param fd the filedescriptor for a local file which will be sent.
4816 * @param metadata a track metadata set to be written along with the file.
Linus Walleijea68f1f2008-06-22 21:54:44 +00004817 * After this call the field <code>metadata-&gt;item_id</code>
4818 * will contain the new track ID. Other fields such
4819 * as the <code>metadata-&gt;filename</code>, <code>metadata-&gt;parent_id</code>
4820 * or <code>metadata-&gt;storage_id</code> may also change during this
4821 * operation due to device restrictions, so do not rely on the
4822 * contents of this struct to be preserved in any way.
4823 * <ul>
4824 * <li><code>metadata-&gt;parent_id</code> should be set to the parent
4825 * (e.g. folder) to store this track in. Since some
4826 * devices are a bit picky about where files
4827 * are placed, a default folder will be chosen if libmtp
4828 * has detected one for the current filetype and this
4829 * parameter is set to 0. If this is 0 and no default folder
4830 * can be found, the file will be stored in the root folder.
4831 * <li><code>metadata-&gt;storage_id</code> should be set to the
4832 * desired storage (e.g. memory card or whatever your device
4833 * presents) to store this track in. Setting this to 0 will store
4834 * the track on the primary storage.
4835 * </ul>
Linus Walleij394bbbe2006-02-22 16:10:53 +00004836 * @param callback a progress indicator function or NULL to ignore.
4837 * @param data a user-defined pointer that is passed along to
4838 * the <code>progress</code> function in order to
4839 * pass along some user defined data to the progress
4840 * updates. If not used, set this to NULL.
mopoke96143402006-10-30 04:37:26 +00004841 * @return 0 if the transfer was successful, any other value means
Linus Walleij394bbbe2006-02-22 16:10:53 +00004842 * failure.
4843 * @see LIBMTP_Send_Track_From_File()
Linus Walleij438bd7f2006-06-08 11:35:44 +00004844 * @see LIBMTP_Delete_Object()
Linus Walleij394bbbe2006-02-22 16:10:53 +00004845 */
mopoke96143402006-10-30 04:37:26 +00004846int LIBMTP_Send_Track_From_File_Descriptor(LIBMTP_mtpdevice_t *device,
Linus Walleij394bbbe2006-02-22 16:10:53 +00004847 int const fd, LIBMTP_track_t * const metadata,
Linus Walleijee73ef22006-08-27 19:56:00 +00004848 LIBMTP_progressfunc_t const callback,
Linus Walleijea68f1f2008-06-22 21:54:44 +00004849 void const * const data)
Linus Walleij394bbbe2006-02-22 16:10:53 +00004850{
Linus Walleij17e39f72006-02-23 15:54:28 +00004851 int subcall_ret;
Linus Walleijd2778d12007-08-06 19:24:58 +00004852 LIBMTP_file_t filedata;
Linus Walleij8ae78bb2006-11-20 21:45:52 +00004853
Linus Walleij16c51f02006-05-04 13:20:22 +00004854 // Sanity check, is this really a track?
Linus Walleij10e0ce72008-05-04 19:20:33 +00004855 if (!LIBMTP_FILETYPE_IS_TRACK(metadata->filetype)) {
Linus Walleij46651f32008-04-26 23:30:19 +00004856 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL,
4857 "LIBMTP_Send_Track_From_File_Descriptor(): "
4858 "I don't think this is actually a track, strange filetype...");
Linus Walleij99310d42006-11-01 08:29:39 +00004859 }
Linus Walleij37253292006-10-11 08:38:14 +00004860
Linus Walleijd2778d12007-08-06 19:24:58 +00004861 // Wrap around the file transfer function
4862 filedata.item_id = metadata->item_id;
4863 filedata.parent_id = metadata->parent_id;
Linus Walleijea68f1f2008-06-22 21:54:44 +00004864 filedata.storage_id = metadata->storage_id;
Linus Walleijd2778d12007-08-06 19:24:58 +00004865 filedata.filename = metadata->filename;
4866 filedata.filesize = metadata->filesize;
4867 filedata.filetype = metadata->filetype;
4868 filedata.next = NULL;
Linus Walleij37253292006-10-11 08:38:14 +00004869
Linus Walleijd2778d12007-08-06 19:24:58 +00004870 subcall_ret = LIBMTP_Send_File_From_File_Descriptor(device,
4871 fd,
4872 &filedata,
4873 callback,
Linus Walleijea68f1f2008-06-22 21:54:44 +00004874 data);
mopoke31364442006-11-20 04:53:04 +00004875
Linus Walleijd2778d12007-08-06 19:24:58 +00004876 if (subcall_ret != 0) {
Linus Walleij46651f32008-04-26 23:30:19 +00004877 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL,
4878 "LIBMTP_Send_Track_From_File_Descriptor(): "
Linus Walleijd2778d12007-08-06 19:24:58 +00004879 "subcall to LIBMTP_Send_File_From_File_Descriptor failed.");
4880 // We used to delete the file here, but don't... It might be OK after all.
4881 // (void) LIBMTP_Delete_Object(device, metadata->item_id);
Linus Walleij394bbbe2006-02-22 16:10:53 +00004882 return -1;
4883 }
Linus Walleijd2778d12007-08-06 19:24:58 +00004884
Linus Walleijea68f1f2008-06-22 21:54:44 +00004885 // Pick up new item (and parent, storage) ID
Linus Walleijd2778d12007-08-06 19:24:58 +00004886 metadata->item_id = filedata.item_id;
4887 metadata->parent_id = filedata.parent_id;
Linus Walleijea68f1f2008-06-22 21:54:44 +00004888 metadata->storage_id = filedata.storage_id;
mopoke96143402006-10-30 04:37:26 +00004889
Linus Walleij17e39f72006-02-23 15:54:28 +00004890 // Set track metadata for the new fine track
4891 subcall_ret = LIBMTP_Update_Track_Metadata(device, metadata);
4892 if (subcall_ret != 0) {
Linus Walleij070e9b42007-01-22 08:49:28 +00004893 // Subcall will add error to errorstack
Linus Walleijb2753182007-02-26 08:11:38 +00004894 // We used to delete the file here, but don't... It might be OK after all.
4895 // (void) LIBMTP_Delete_Object(device, metadata->item_id);
Linus Walleij17e39f72006-02-23 15:54:28 +00004896 return -1;
4897 }
Linus Walleij438bd7f2006-06-08 11:35:44 +00004898
Richard Low61edc1a2007-09-23 10:35:48 +00004899 // note we don't need to update the cache here because LIBMTP_Send_File_From_File_Descriptor
4900 // has added the object handle and LIBMTP_Update_Track_Metadata has added the metadata.
mopoke96143402006-10-30 04:37:26 +00004901
Linus Walleij17e39f72006-02-23 15:54:28 +00004902 return 0;
4903}
4904
Linus Walleijd6a49972006-05-02 08:24:54 +00004905/**
Richard Lowd3b17022009-04-11 12:37:39 +00004906 * This function sends a track from a handler function to an
4907 * MTP device. A filename and a set of metadata must be
4908 * given as input.
4909 * @param device a pointer to the device to send the track to.
4910 * @param get_func the function to call when we have data.
4911 * @param priv the user-defined pointer that is passed to
4912 * <code>get_func</code>.
4913 * @param metadata a track metadata set to be written along with the file.
4914 * After this call the field <code>metadata-&gt;item_id</code>
4915 * will contain the new track ID. Other fields such
4916 * as the <code>metadata-&gt;filename</code>, <code>metadata-&gt;parent_id</code>
4917 * or <code>metadata-&gt;storage_id</code> may also change during this
4918 * operation due to device restrictions, so do not rely on the
4919 * contents of this struct to be preserved in any way.
4920 * <ul>
4921 * <li><code>metadata-&gt;parent_id</code> should be set to the parent
4922 * (e.g. folder) to store this track in. Since some
4923 * devices are a bit picky about where files
4924 * are placed, a default folder will be chosen if libmtp
4925 * has detected one for the current filetype and this
4926 * parameter is set to 0. If this is 0 and no default folder
4927 * can be found, the file will be stored in the root folder.
4928 * <li><code>metadata-&gt;storage_id</code> should be set to the
4929 * desired storage (e.g. memory card or whatever your device
4930 * presents) to store this track in. Setting this to 0 will store
4931 * the track on the primary storage.
4932 * </ul>
4933 * @param callback a progress indicator function or NULL to ignore.
4934 * @param data a user-defined pointer that is passed along to
4935 * the <code>progress</code> function in order to
4936 * pass along some user defined data to the progress
4937 * updates. If not used, set this to NULL.
4938 * @return 0 if the transfer was successful, any other value means
4939 * failure.
4940 * @see LIBMTP_Send_Track_From_File()
4941 * @see LIBMTP_Delete_Object()
4942 */
4943int LIBMTP_Send_Track_From_Handler(LIBMTP_mtpdevice_t *device,
4944 MTPDataGetFunc get_func, void * priv, LIBMTP_track_t * const metadata,
4945 LIBMTP_progressfunc_t const callback,
4946 void const * const data)
4947{
4948 int subcall_ret;
4949 LIBMTP_file_t filedata;
4950
4951 // Sanity check, is this really a track?
4952 if (!LIBMTP_FILETYPE_IS_TRACK(metadata->filetype)) {
4953 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL,
4954 "LIBMTP_Send_Track_From_Handler(): "
4955 "I don't think this is actually a track, strange filetype...");
4956 }
4957
4958 // Wrap around the file transfer function
4959 filedata.item_id = metadata->item_id;
4960 filedata.parent_id = metadata->parent_id;
4961 filedata.storage_id = metadata->storage_id;
4962 filedata.filename = metadata->filename;
4963 filedata.filesize = metadata->filesize;
4964 filedata.filetype = metadata->filetype;
4965 filedata.next = NULL;
4966
4967 subcall_ret = LIBMTP_Send_File_From_Handler(device,
4968 get_func,
4969 priv,
4970 &filedata,
4971 callback,
4972 data);
4973
4974 if (subcall_ret != 0) {
4975 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL,
4976 "LIBMTP_Send_Track_From_Handler(): "
4977 "subcall to LIBMTP_Send_File_From_Handler failed.");
4978 // We used to delete the file here, but don't... It might be OK after all.
4979 // (void) LIBMTP_Delete_Object(device, metadata->item_id);
4980 return -1;
4981 }
4982
4983 // Pick up new item (and parent, storage) ID
4984 metadata->item_id = filedata.item_id;
4985 metadata->parent_id = filedata.parent_id;
4986 metadata->storage_id = filedata.storage_id;
4987
4988 // Set track metadata for the new fine track
4989 subcall_ret = LIBMTP_Update_Track_Metadata(device, metadata);
4990 if (subcall_ret != 0) {
4991 // Subcall will add error to errorstack
4992 // We used to delete the file here, but don't... It might be OK after all.
4993 // (void) LIBMTP_Delete_Object(device, metadata->item_id);
4994 return -1;
4995 }
4996
4997 // note we don't need to update the cache here because LIBMTP_Send_File_From_File_Descriptor
4998 // has added the object handle and LIBMTP_Update_Track_Metadata has added the metadata.
4999
5000 return 0;
5001}
5002
5003/**
mopoke96143402006-10-30 04:37:26 +00005004 * This function sends a local file to an MTP device.
Linus Walleijd6a49972006-05-02 08:24:54 +00005005 * A filename and a set of metadata must be
5006 * given as input.
5007 * @param device a pointer to the device to send the track to.
5008 * @param path the filename of a local file which will be sent.
Linus Walleijea68f1f2008-06-22 21:54:44 +00005009 * @param filedata a file metadata set to be written along with the file.
5010 * After this call the field <code>filedata-&gt;item_id</code>
5011 * will contain the new file ID. Other fields such
5012 * as the <code>filedata-&gt;filename</code>, <code>filedata-&gt;parent_id</code>
5013 * or <code>filedata-&gt;storage_id</code> may also change during this
5014 * operation due to device restrictions, so do not rely on the
5015 * contents of this struct to be preserved in any way.
5016 * <ul>
5017 * <li><code>filedata-&gt;parent_id</code> should be set to the parent
5018 * (e.g. folder) to store this file in. If this is 0,
5019 * the file will be stored in the root folder.
5020 * <li><code>filedata-&gt;storage_id</code> should be set to the
5021 * desired storage (e.g. memory card or whatever your device
5022 * presents) to store this file in. Setting this to 0 will store
5023 * the file on the primary storage.
5024 * </ul>
Linus Walleijd6a49972006-05-02 08:24:54 +00005025 * @param callback a progress indicator function or NULL to ignore.
5026 * @param data a user-defined pointer that is passed along to
5027 * the <code>progress</code> function in order to
5028 * pass along some user defined data to the progress
5029 * updates. If not used, set this to NULL.
mopoke96143402006-10-30 04:37:26 +00005030 * @return 0 if the transfer was successful, any other value means
Linus Walleijd6a49972006-05-02 08:24:54 +00005031 * failure.
5032 * @see LIBMTP_Send_File_From_File_Descriptor()
Linus Walleij438bd7f2006-06-08 11:35:44 +00005033 * @see LIBMTP_Delete_Object()
Linus Walleijd6a49972006-05-02 08:24:54 +00005034 */
mopoke96143402006-10-30 04:37:26 +00005035int LIBMTP_Send_File_From_File(LIBMTP_mtpdevice_t *device,
Linus Walleijd6a49972006-05-02 08:24:54 +00005036 char const * const path, LIBMTP_file_t * const filedata,
Linus Walleijee73ef22006-08-27 19:56:00 +00005037 LIBMTP_progressfunc_t const callback,
Linus Walleijea68f1f2008-06-22 21:54:44 +00005038 void const * const data)
Linus Walleijd6a49972006-05-02 08:24:54 +00005039{
5040 int fd;
5041 int ret;
5042
5043 // Sanity check
5044 if (path == NULL) {
Linus Walleij070e9b42007-01-22 08:49:28 +00005045 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 +00005046 return -1;
5047 }
5048
5049 // Open file
5050#ifdef __WIN32__
Linus Walleij7beba572006-11-29 08:56:12 +00005051#ifdef USE_WINDOWS_IO_H
Linus Walleij5b4a4d22009-01-10 12:18:14 +00005052 if ( (fd = _open(path, O_RDONLY|O_BINARY) == -1) ) {
Linus Walleij7beba572006-11-29 08:56:12 +00005053#else
Linus Walleij5b4a4d22009-01-10 12:18:14 +00005054 if ( (fd = open(path, O_RDONLY|O_BINARY) == -1) ) {
Linus Walleij7beba572006-11-29 08:56:12 +00005055#endif
Linus Walleijd6a49972006-05-02 08:24:54 +00005056#else
5057 if ( (fd = open(path, O_RDONLY)) == -1) {
5058#endif
Linus Walleij070e9b42007-01-22 08:49:28 +00005059 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 +00005060 return -1;
5061 }
5062
Linus Walleijea68f1f2008-06-22 21:54:44 +00005063 ret = LIBMTP_Send_File_From_File_Descriptor(device, fd, filedata, callback, data);
mopoke96143402006-10-30 04:37:26 +00005064
Linus Walleijd6a49972006-05-02 08:24:54 +00005065 // Close file.
Linus Walleij7beba572006-11-29 08:56:12 +00005066#ifdef USE_WINDOWS_IO_H
5067 _close(fd);
5068#else
Linus Walleijd6a49972006-05-02 08:24:54 +00005069 close(fd);
Linus Walleij7beba572006-11-29 08:56:12 +00005070#endif
Linus Walleijd6a49972006-05-02 08:24:54 +00005071
5072 return ret;
5073}
5074
Linus Walleijd208f9c2006-04-27 14:16:06 +00005075/**
5076 * This function sends a generic file from a file descriptor to an
5077 * MTP device. A filename and a set of metadata must be
5078 * given as input.
Linus Walleijcd3eb3d2006-09-02 21:33:22 +00005079 *
5080 * This can potentially be used for sending in a stream of unknown
Linus Walleij1b87ea72007-08-28 07:53:09 +00005081 * length. Send music files with
5082 * <code>LIBMTP_Send_Track_From_File_Descriptor()</code>
Linus Walleijcd3eb3d2006-09-02 21:33:22 +00005083 *
Linus Walleijd208f9c2006-04-27 14:16:06 +00005084 * @param device a pointer to the device to send the file to.
5085 * @param fd the filedescriptor for a local file which will be sent.
Linus Walleijea68f1f2008-06-22 21:54:44 +00005086 * @param filedata a file metadata set to be written along with the file.
5087 * After this call the field <code>filedata-&gt;item_id</code>
5088 * will contain the new file ID. Other fields such
5089 * as the <code>filedata-&gt;filename</code>, <code>filedata-&gt;parent_id</code>
5090 * or <code>filedata-&gt;storage_id</code> may also change during this
5091 * operation due to device restrictions, so do not rely on the
5092 * contents of this struct to be preserved in any way.
5093 * <ul>
5094 * <li><code>filedata-&gt;parent_id</code> should be set to the parent
5095 * (e.g. folder) to store this file in. If this is 0,
5096 * the file will be stored in the root folder.
5097 * <li><code>filedata-&gt;storage_id</code> should be set to the
5098 * desired storage (e.g. memory card or whatever your device
5099 * presents) to store this file in. Setting this to 0 will store
5100 * the file on the primary storage.
5101 * </ul>
Linus Walleijd208f9c2006-04-27 14:16:06 +00005102 * @param callback a progress indicator function or NULL to ignore.
5103 * @param data a user-defined pointer that is passed along to
5104 * the <code>progress</code> function in order to
5105 * pass along some user defined data to the progress
5106 * updates. If not used, set this to NULL.
mopoke96143402006-10-30 04:37:26 +00005107 * @return 0 if the transfer was successful, any other value means
Linus Walleijd208f9c2006-04-27 14:16:06 +00005108 * failure.
Linus Walleijd6a49972006-05-02 08:24:54 +00005109 * @see LIBMTP_Send_File_From_File()
Linus Walleij1b87ea72007-08-28 07:53:09 +00005110 * @see LIBMTP_Send_Track_From_File_Descriptor()
Linus Walleij438bd7f2006-06-08 11:35:44 +00005111 * @see LIBMTP_Delete_Object()
Linus Walleijd208f9c2006-04-27 14:16:06 +00005112 */
mopoke96143402006-10-30 04:37:26 +00005113int LIBMTP_Send_File_From_File_Descriptor(LIBMTP_mtpdevice_t *device,
Linus Walleijd208f9c2006-04-27 14:16:06 +00005114 int const fd, LIBMTP_file_t * const filedata,
Linus Walleijee73ef22006-08-27 19:56:00 +00005115 LIBMTP_progressfunc_t const callback,
Linus Walleijea68f1f2008-06-22 21:54:44 +00005116 void const * const data)
Linus Walleijd208f9c2006-04-27 14:16:06 +00005117{
5118 uint16_t ret;
Linus Walleijd208f9c2006-04-27 14:16:06 +00005119 PTPParams *params = (PTPParams *) device->params;
Linus Walleijd214b9b2006-08-26 22:08:37 +00005120 PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo;
Linus Walleij817579a2008-05-23 21:21:40 +00005121 LIBMTP_file_t *newfilemeta;
Richard Lowab0d22d2006-11-30 22:17:49 +00005122
Richard Lowd3b17022009-04-11 12:37:39 +00005123 if (send_file_object_info(device, filedata))
5124 {
5125 // no need to output an error since send_file_object_info will already have done so
Linus Walleij07795772007-08-06 18:44:06 +00005126 return -1;
Linus Walleijcd3eb3d2006-09-02 21:33:22 +00005127 }
Linus Walleij07795772007-08-06 18:44:06 +00005128
Richard Lowd3b17022009-04-11 12:37:39 +00005129 // Callbacks
5130 ptp_usb->callback_active = 1;
5131 // The callback will deactivate itself after this amount of data has been sent
5132 // One BULK header for the request, one for the data phase. No parameters to the request.
5133 ptp_usb->current_transfer_total = filedata->filesize+PTP_USB_BULK_HDR_LEN*2;
5134 ptp_usb->current_transfer_complete = 0;
5135 ptp_usb->current_transfer_callback = callback;
5136 ptp_usb->current_transfer_callback_data = data;
5137
5138 ret = ptp_sendobject_fromfd(params, fd, filedata->filesize);
5139
5140 ptp_usb->callback_active = 0;
5141 ptp_usb->current_transfer_callback = NULL;
5142 ptp_usb->current_transfer_callback_data = NULL;
5143
5144 if (ret == PTP_ERROR_CANCEL) {
5145 add_error_to_errorstack(device, LIBMTP_ERROR_CANCELLED, "LIBMTP_Send_File_From_File_Descriptor(): Cancelled transfer.");
5146 return -1;
5147 }
5148 if (ret != PTP_RC_OK) {
5149 add_ptp_error_to_errorstack(device, ret, "LIBMTP_Send_File_From_File_Descriptor(): "
5150 "Could not send object.");
5151 return -1;
5152 }
5153
5154 add_object_to_cache(device, filedata->item_id);
5155
5156 /*
5157 * Get the device-assined parent_id from the cache.
5158 * The operation that adds it to the cache will
5159 * look it up from the device, so we get the new
5160 * parent_id from the cache.
5161 */
5162 newfilemeta = LIBMTP_Get_Filemetadata(device, filedata->item_id);
5163 if (newfilemeta != NULL) {
5164 filedata->parent_id = newfilemeta->parent_id;
5165 filedata->storage_id = newfilemeta->storage_id;
5166 LIBMTP_destroy_file_t(newfilemeta);
5167 } else {
5168 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL,
5169 "LIBMTP_Send_File_From_File_Descriptor(): "
5170 "Could not retrieve updated metadata.");
5171 return -1;
5172 }
5173
5174 return 0;
5175}
5176
5177/**
5178 * This function sends a generic file from a handler function to an
5179 * MTP device. A filename and a set of metadata must be
5180 * given as input.
5181 *
5182 * This can potentially be used for sending in a stream of unknown
5183 * length. Send music files with
5184 * <code>LIBMTP_Send_Track_From_Handler()</code>
5185 *
5186 * @param device a pointer to the device to send the file to.
5187 * @param get_func the function to call to get data to write
5188 * @param priv a user-defined pointer that is passed along to
5189 * <code>get_func</code>. If not used, this is set to NULL.
5190 * @param filedata a file metadata set to be written along with the file.
5191 * After this call the field <code>filedata-&gt;item_id</code>
5192 * will contain the new file ID. Other fields such
5193 * as the <code>filedata-&gt;filename</code>, <code>filedata-&gt;parent_id</code>
5194 * or <code>filedata-&gt;storage_id</code> may also change during this
5195 * operation due to device restrictions, so do not rely on the
5196 * contents of this struct to be preserved in any way.
5197 * <ul>
5198 * <li><code>filedata-&gt;parent_id</code> should be set to the parent
5199 * (e.g. folder) to store this file in. If this is 0,
5200 * the file will be stored in the root folder.
5201 * <li><code>filedata-&gt;storage_id</code> should be set to the
5202 * desired storage (e.g. memory card or whatever your device
5203 * presents) to store this file in. Setting this to 0 will store
5204 * the file on the primary storage.
5205 * </ul>
5206 * @param callback a progress indicator function or NULL to ignore.
5207 * @param data a user-defined pointer that is passed along to
5208 * the <code>progress</code> function in order to
5209 * pass along some user defined data to the progress
5210 * updates. If not used, set this to NULL.
5211 * @return 0 if the transfer was successful, any other value means
5212 * failure.
5213 * @see LIBMTP_Send_File_From_File()
5214 * @see LIBMTP_Send_Track_From_File_Descriptor()
5215 * @see LIBMTP_Delete_Object()
5216 */
5217int LIBMTP_Send_File_From_Handler(LIBMTP_mtpdevice_t *device,
5218 MTPDataGetFunc get_func, void * priv, LIBMTP_file_t * const filedata,
5219 LIBMTP_progressfunc_t const callback, void const * const data)
5220{
5221 uint16_t ret;
5222 PTPParams *params = (PTPParams *) device->params;
5223 PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo;
5224 LIBMTP_file_t *newfilemeta;
5225
5226 if (send_file_object_info(device, filedata))
5227 {
5228 // no need to output an error since send_file_object_info will already have done so
5229 return -1;
5230 }
Linus Walleijd866d242009-08-23 21:50:39 +00005231
Richard Lowd3b17022009-04-11 12:37:39 +00005232 // Callbacks
5233 ptp_usb->callback_active = 1;
5234 // The callback will deactivate itself after this amount of data has been sent
5235 // One BULK header for the request, one for the data phase. No parameters to the request.
5236 ptp_usb->current_transfer_total = filedata->filesize+PTP_USB_BULK_HDR_LEN*2;
5237 ptp_usb->current_transfer_complete = 0;
5238 ptp_usb->current_transfer_callback = callback;
5239 ptp_usb->current_transfer_callback_data = data;
Linus Walleijd866d242009-08-23 21:50:39 +00005240
Richard Low5b4023c2009-04-16 19:14:38 +00005241 MTPDataHandler mtp_handler;
5242 mtp_handler.getfunc = get_func;
5243 mtp_handler.putfunc = NULL;
Linus Walleijd866d242009-08-23 21:50:39 +00005244 mtp_handler.priv = priv;
5245
Richard Lowd3b17022009-04-11 12:37:39 +00005246 PTPDataHandler handler;
Richard Low5b4023c2009-04-16 19:14:38 +00005247 handler.getfunc = get_func_wrapper;
Richard Lowd3b17022009-04-11 12:37:39 +00005248 handler.putfunc = NULL;
Linus Walleijd866d242009-08-23 21:50:39 +00005249 handler.priv = &mtp_handler;
5250
Richard Lowd3b17022009-04-11 12:37:39 +00005251 ret = ptp_sendobject_from_handler(params, &handler, filedata->filesize);
Linus Walleijd866d242009-08-23 21:50:39 +00005252
Richard Lowd3b17022009-04-11 12:37:39 +00005253 ptp_usb->callback_active = 0;
5254 ptp_usb->current_transfer_callback = NULL;
5255 ptp_usb->current_transfer_callback_data = NULL;
5256
5257 if (ret == PTP_ERROR_CANCEL) {
5258 add_error_to_errorstack(device, LIBMTP_ERROR_CANCELLED, "LIBMTP_Send_File_From_Handler(): Cancelled transfer.");
5259 return -1;
5260 }
5261 if (ret != PTP_RC_OK) {
5262 add_ptp_error_to_errorstack(device, ret, "LIBMTP_Send_File_From_Handler(): "
5263 "Could not send object.");
5264 return -1;
5265 }
5266
5267 add_object_to_cache(device, filedata->item_id);
Linus Walleijd866d242009-08-23 21:50:39 +00005268
Richard Lowd3b17022009-04-11 12:37:39 +00005269 /*
5270 * Get the device-assined parent_id from the cache.
5271 * The operation that adds it to the cache will
5272 * look it up from the device, so we get the new
5273 * parent_id from the cache.
5274 */
5275 newfilemeta = LIBMTP_Get_Filemetadata(device, filedata->item_id);
5276 if (newfilemeta != NULL) {
5277 filedata->parent_id = newfilemeta->parent_id;
5278 filedata->storage_id = newfilemeta->storage_id;
5279 LIBMTP_destroy_file_t(newfilemeta);
5280 } else {
5281 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL,
5282 "LIBMTP_Send_File_From_Handler(): "
5283 "Could not retrieve updated metadata.");
5284 return -1;
5285 }
5286
5287 return 0;
5288}
5289
Linus Walleijd866d242009-08-23 21:50:39 +00005290/**
Richard Lowd3b17022009-04-11 12:37:39 +00005291 * This function sends the file object info, ready for sendobject
5292 * @param device a pointer to the device to send the file to.
5293 * @param filedata a file metadata set to be written along with the file.
5294 * @return 0 if the transfer was successful, any other value means
5295 * failure.
5296 */
5297static int send_file_object_info(LIBMTP_mtpdevice_t *device, LIBMTP_file_t *filedata)
5298{
5299 PTPParams *params = (PTPParams *) device->params;
5300 PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo;
5301 uint32_t store;
5302 int use_primary_storage = 1;
5303 uint16_t of = map_libmtp_type_to_ptp_type(filedata->filetype);
5304 LIBMTP_devicestorage_t *storage;
5305 uint32_t localph = filedata->parent_id;
5306 uint16_t ret;
5307 int i;
5308
5309 // Sanity check: no zerolength files.
5310 if (filedata->filesize == 0) {
5311 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "send_file_object_info(): "
5312 "File of zero size.");
5313 return -1;
5314 }
5315
Linus Walleijea68f1f2008-06-22 21:54:44 +00005316 if (filedata->storage_id != 0) {
5317 store = filedata->storage_id;
Linus Walleijfb28b632007-10-23 21:56:18 +00005318 } else {
Linus Walleijd71d0b32008-09-22 08:21:03 +00005319 store = get_writeable_storageid(device, filedata->filesize);
Linus Walleijea68f1f2008-06-22 21:54:44 +00005320 }
5321 // Detect if something non-primary is in use.
5322 storage = device->storage;
Richard Low4d9165f2008-09-23 20:13:17 +00005323 if (storage != NULL && store != storage->id) {
Linus Walleijea68f1f2008-06-22 21:54:44 +00005324 use_primary_storage = 0;
Linus Walleij6bf68b42007-09-03 22:50:02 +00005325 }
Linus Walleijd208f9c2006-04-27 14:16:06 +00005326
mopoke96143402006-10-30 04:37:26 +00005327 /*
Linus Walleij05ccbe72006-06-13 07:46:58 +00005328 * If no destination folder was given, look up a default
5329 * folder if possible. Perhaps there is some way of retrieveing
5330 * the default folder for different forms of content, what
5331 * do I know, we use a fixed list in lack of any better method.
5332 * Some devices obviously need to have their files in certain
mopoke96143402006-10-30 04:37:26 +00005333 * folders in order to find/display them at all (hello Creative),
Linus Walleijc40c9bf2008-06-13 23:24:17 +00005334 * so we have to have a method for this. We only do this if the
5335 * primary storage is in use.
Linus Walleij05ccbe72006-06-13 07:46:58 +00005336 */
5337
Linus Walleijc40c9bf2008-06-13 23:24:17 +00005338 if (localph == 0 && use_primary_storage) {
Linus Walleij10e0ce72008-05-04 19:20:33 +00005339 if (LIBMTP_FILETYPE_IS_AUDIO(filedata->filetype)) {
Linus Walleij05ccbe72006-06-13 07:46:58 +00005340 localph = device->default_music_folder;
Linus Walleij10e0ce72008-05-04 19:20:33 +00005341 } else if (LIBMTP_FILETYPE_IS_VIDEO(filedata->filetype)) {
Linus Walleij05ccbe72006-06-13 07:46:58 +00005342 localph = device->default_video_folder;
5343 } else if (of == PTP_OFC_EXIF_JPEG ||
Linus Walleij5fb47132006-12-30 15:35:48 +00005344 of == PTP_OFC_JP2 ||
5345 of == PTP_OFC_JPX ||
Linus Walleij05ccbe72006-06-13 07:46:58 +00005346 of == PTP_OFC_JFIF ||
5347 of == PTP_OFC_TIFF ||
Linus Walleij5fb47132006-12-30 15:35:48 +00005348 of == PTP_OFC_TIFF_IT ||
Linus Walleij05ccbe72006-06-13 07:46:58 +00005349 of == PTP_OFC_BMP ||
5350 of == PTP_OFC_GIF ||
5351 of == PTP_OFC_PICT ||
5352 of == PTP_OFC_PNG ||
5353 of == PTP_OFC_MTP_WindowsImageFormat) {
5354 localph = device->default_picture_folder;
5355 } else if (of == PTP_OFC_MTP_vCalendar1 ||
Linus Walleijd7aa5b22006-09-02 11:52:31 +00005356 of == PTP_OFC_MTP_vCalendar2 ||
5357 of == PTP_OFC_MTP_UndefinedContact ||
5358 of == PTP_OFC_MTP_vCard2 ||
5359 of == PTP_OFC_MTP_vCard3 ||
5360 of == PTP_OFC_MTP_UndefinedCalendarItem) {
Linus Walleij05ccbe72006-06-13 07:46:58 +00005361 localph = device->default_organizer_folder;
Linus Walleij46651f32008-04-26 23:30:19 +00005362 } else if (of == PTP_OFC_Text) {
Linus Walleij9316e652006-12-07 09:55:21 +00005363 localph = device->default_text_folder;
Linus Walleij05ccbe72006-06-13 07:46:58 +00005364 }
5365 }
mopoke31364442006-11-20 04:53:04 +00005366
Linus Walleija3544f62007-11-30 01:20:04 +00005367 // Here we wire the type to unknown on bugged, but
Linus Walleij89bb1cd2009-07-24 21:03:36 +00005368 // Ogg or FLAC-supportive devices.
Linus Walleijfec4d562008-06-01 22:30:36 +00005369 if (FLAG_OGG_IS_UNKNOWN(ptp_usb) && of == PTP_OFC_MTP_OGG) {
Linus Walleija3544f62007-11-30 01:20:04 +00005370 of = PTP_OFC_Undefined;
5371 }
Linus Walleij89bb1cd2009-07-24 21:03:36 +00005372 if (FLAG_FLAC_IS_UNKNOWN(ptp_usb) && of == PTP_OFC_MTP_FLAC) {
5373 of = PTP_OFC_Undefined;
5374 }
Linus Walleija3544f62007-11-30 01:20:04 +00005375
Linus Walleijf67c1ad2009-01-14 21:39:50 +00005376 if (ptp_operation_issupported(params, PTP_OC_MTP_SendObjectPropList) &&
5377 !FLAG_BROKEN_SEND_OBJECT_PROPLIST(ptp_usb)) {
Linus Walleijd2778d12007-08-06 19:24:58 +00005378 /*
5379 * MTP enhanched does it this way (from a sniff):
5380 * -> PTP_OC_MTP_SendObjectPropList (0x9808):
5381 * 20 00 00 00 01 00 08 98 1B 00 00 00 01 00 01 00
5382 * FF FF FF FF 00 30 00 00 00 00 00 00 12 5E 00 00
5383 * Length: 0x00000020
5384 * Type: 0x0001 PTP_USB_CONTAINER_COMMAND
5385 * Code: 0x9808
5386 * Transaction ID: 0x0000001B
5387 * Param1: 0x00010001 <- store
5388 * Param2: 0xffffffff <- parent handle (-1 ?)
5389 * Param3: 0x00003000 <- file type PTP_OFC_Undefined - we don't know about PDF files
5390 * Param4: 0x00000000 <- file length MSB (-0x0c header len)
5391 * Param5: 0x00005e12 <- file length LSB (-0x0c header len)
5392 *
5393 * -> PTP_OC_MTP_SendObjectPropList (0x9808):
5394 * 46 00 00 00 02 00 08 98 1B 00 00 00 03 00 00 00
5395 * 00 00 00 00 07 DC FF FF 0D 4B 00 53 00 30 00 36 - dc07 = file name
5396 * 00 30 00 33 00 30 00 36 00 2E 00 70 00 64 00 66
5397 * 00 00 00 00 00 00 00 03 DC 04 00 00 00 00 00 00 - dc03 = protection status
5398 * 00 4F DC 02 00 01 - dc4f = non consumable
5399 * Length: 0x00000046
5400 * Type: 0x0002 PTP_USB_CONTAINER_DATA
5401 * Code: 0x9808
5402 * Transaction ID: 0x0000001B
5403 * Metadata....
5404 * 0x00000003 <- Number of metadata items
5405 * 0x00000000 <- Object handle, set to 0x00000000 since it is unknown!
5406 * 0xdc07 <- metadata type: file name
5407 * 0xffff <- metadata type: string
5408 * 0x0d <- number of (uint16_t) characters
5409 * 4b 53 30 36 30 33 30 36 2e 50 64 66 00 "KS060306.pdf", null terminated
5410 * 0x00000000 <- Object handle, set to 0x00000000 since it is unknown!
5411 * 0xdc03 <- metadata type: protection status
5412 * 0x0004 <- metadata type: uint16_t
5413 * 0x0000 <- not protected
5414 * 0x00000000 <- Object handle, set to 0x00000000 since it is unknown!
5415 * 0xdc4f <- non consumable
5416 * 0x0002 <- metadata type: uint8_t
5417 * 0x01 <- non-consumable (this device cannot display PDF)
5418 *
5419 * <- Read 0x18 bytes back
5420 * 18 00 00 00 03 00 01 20 1B 00 00 00 01 00 01 00
5421 * 00 00 00 00 01 40 00 00
5422 * Length: 0x000000018
5423 * Type: 0x0003 PTP_USB_CONTAINER_RESPONSE
5424 * Code: 0x2001 PTP_OK
5425 * Transaction ID: 0x0000001B
5426 * Param1: 0x00010001 <- store
5427 * Param2: 0x00000000 <- parent handle
5428 * Param3: 0x00004001 <- new file/object ID
5429 *
5430 * -> PTP_OC_SendObject (0x100d)
5431 * 0C 00 00 00 01 00 0D 10 1C 00 00 00
5432 * -> ... all the bytes ...
5433 * <- Read 0x0c bytes back
5434 * 0C 00 00 00 03 00 01 20 1C 00 00 00
5435 * ... Then update metadata one-by one, actually (instead of sending it first!) ...
5436 */
Linus Walleij1e9a0332007-09-12 19:35:56 +00005437 MTPProperties *props = NULL;
5438 int nrofprops = 0;
5439 MTPProperties *prop = NULL;
5440 uint16_t *properties = NULL;
Linus Walleij07795772007-08-06 18:44:06 +00005441 uint32_t propcnt = 0;
Richard Low6c0a6ce2006-11-26 10:42:08 +00005442
Linus Walleij9036b332008-05-23 21:01:36 +00005443 // default parent handle
Linus Walleij07795772007-08-06 18:44:06 +00005444 if (localph == 0)
5445 localph = 0xFFFFFFFFU; // Set to -1
5446
Richard Low6c0a6ce2006-11-26 10:42:08 +00005447 // Must be 0x00000000U for new objects
5448 filedata->item_id = 0x00000000U;
Linus Walleij05ccbe72006-06-13 07:46:58 +00005449
Linus Walleij1e9a0332007-09-12 19:35:56 +00005450 ret = ptp_mtp_getobjectpropssupported(params, of, &propcnt, &properties);
mopoke31364442006-11-20 04:53:04 +00005451
rreardonbbb4e562006-11-19 22:16:11 +00005452 for (i=0;i<propcnt;i++) {
Linus Walleijdbcc8242007-08-05 22:31:26 +00005453 PTPObjectPropDesc opd;
5454
Linus Walleij1e9a0332007-09-12 19:35:56 +00005455 ret = ptp_mtp_getobjectpropdesc(params, properties[i], of, &opd);
Linus Walleijdbcc8242007-08-05 22:31:26 +00005456 if (ret != PTP_RC_OK) {
Richard Lowd3b17022009-04-11 12:37:39 +00005457 add_ptp_error_to_errorstack(device, ret, "send_file_object_info(): "
Linus Walleijdbcc8242007-08-05 22:31:26 +00005458 "could not get property description.");
5459 } else if (opd.GetSet) {
Linus Walleij1e9a0332007-09-12 19:35:56 +00005460 switch (properties[i]) {
Linus Walleijdbcc8242007-08-05 22:31:26 +00005461 case PTP_OPC_ObjectFileName:
Linus Walleija6d0d482007-10-31 08:54:56 +00005462 prop = ptp_get_new_object_prop_entry(&props,&nrofprops);
Linus Walleijdbcc8242007-08-05 22:31:26 +00005463 prop->ObjectHandle = filedata->item_id;
5464 prop->property = PTP_OPC_ObjectFileName;
5465 prop->datatype = PTP_DTC_STR;
Linus Walleijd3b78572007-08-24 21:28:24 +00005466 if (filedata->filename != NULL) {
Linus Walleij07795772007-08-06 18:44:06 +00005467 prop->propval.str = strdup(filedata->filename);
Linus Walleijfec4d562008-06-01 22:30:36 +00005468 if (FLAG_ONLY_7BIT_FILENAMES(ptp_usb)) {
Linus Walleijd3b78572007-08-24 21:28:24 +00005469 strip_7bit_from_utf8(prop->propval.str);
5470 }
5471 }
Linus Walleijdbcc8242007-08-05 22:31:26 +00005472 break;
5473 case PTP_OPC_ProtectionStatus:
Linus Walleija6d0d482007-10-31 08:54:56 +00005474 prop = ptp_get_new_object_prop_entry(&props,&nrofprops);
Linus Walleijdbcc8242007-08-05 22:31:26 +00005475 prop->ObjectHandle = filedata->item_id;
5476 prop->property = PTP_OPC_ProtectionStatus;
5477 prop->datatype = PTP_DTC_UINT16;
5478 prop->propval.u16 = 0x0000U; /* Not protected */
Linus Walleijdbcc8242007-08-05 22:31:26 +00005479 break;
5480 case PTP_OPC_NonConsumable:
Linus Walleija6d0d482007-10-31 08:54:56 +00005481 prop = ptp_get_new_object_prop_entry(&props,&nrofprops);
Linus Walleijdbcc8242007-08-05 22:31:26 +00005482 prop->ObjectHandle = filedata->item_id;
5483 prop->property = PTP_OPC_NonConsumable;
5484 prop->datatype = PTP_DTC_UINT8;
Linus Walleij7783b9b2007-09-22 22:10:44 +00005485 prop->propval.u8 = 0x00; /* It is supported, then it is consumable */
Linus Walleijdbcc8242007-08-05 22:31:26 +00005486 break;
5487 case PTP_OPC_Name:
Linus Walleija6d0d482007-10-31 08:54:56 +00005488 prop = ptp_get_new_object_prop_entry(&props,&nrofprops);
Linus Walleijdbcc8242007-08-05 22:31:26 +00005489 prop->ObjectHandle = filedata->item_id;
5490 prop->property = PTP_OPC_Name;
5491 prop->datatype = PTP_DTC_STR;
Linus Walleij07795772007-08-06 18:44:06 +00005492 if (filedata->filename != NULL)
5493 prop->propval.str = strdup(filedata->filename);
Linus Walleijdbcc8242007-08-05 22:31:26 +00005494 break;
Linus Walleij7783b9b2007-09-22 22:10:44 +00005495 case PTP_OPC_DateModified:
5496 // Tag with current time if that is supported
Linus Walleijcf8dc2b2008-10-21 13:58:36 +00005497 if (!FLAG_CANNOT_HANDLE_DATEMODIFIED(ptp_usb)) {
5498 prop = ptp_get_new_object_prop_entry(&props,&nrofprops);
5499 prop->ObjectHandle = filedata->item_id;
5500 prop->property = PTP_OPC_DateModified;
5501 prop->datatype = PTP_DTC_STR;
5502 prop->propval.str = get_iso8601_stamp();
Richard Lowd3b17022009-04-11 12:37:39 +00005503 filedata->modificationdate = time(NULL);
Linus Walleijcf8dc2b2008-10-21 13:58:36 +00005504 }
Linus Walleij7783b9b2007-09-22 22:10:44 +00005505 break;
Linus Walleijdbcc8242007-08-05 22:31:26 +00005506 }
rreardonbbb4e562006-11-19 22:16:11 +00005507 }
Linus Walleijdbcc8242007-08-05 22:31:26 +00005508 ptp_free_objectpropdesc(&opd);
Linus Walleij99310d42006-11-01 08:29:39 +00005509 }
Linus Walleij1e9a0332007-09-12 19:35:56 +00005510 free(properties);
mopoke31364442006-11-20 04:53:04 +00005511
rreardonbbb4e562006-11-19 22:16:11 +00005512 ret = ptp_mtp_sendobjectproplist(params, &store, &localph, &filedata->item_id,
Linus Walleij1e9a0332007-09-12 19:35:56 +00005513 of, filedata->filesize, props, nrofprops);
mopoke31364442006-11-20 04:53:04 +00005514
rreardonbbb4e562006-11-19 22:16:11 +00005515 /* Free property list */
Linus Walleija6d0d482007-10-31 08:54:56 +00005516 ptp_destroy_object_prop_list(props, nrofprops);
mopoke31364442006-11-20 04:53:04 +00005517
rreardonbbb4e562006-11-19 22:16:11 +00005518 if (ret != PTP_RC_OK) {
Richard Lowd3b17022009-04-11 12:37:39 +00005519 add_ptp_error_to_errorstack(device, ret, "send_file_object_info():"
Linus Walleij07795772007-08-06 18:44:06 +00005520 "Could not send object property list.");
rreardonbbb4e562006-11-19 22:16:11 +00005521 if (ret == PTP_RC_AccessDenied) {
Linus Walleij070e9b42007-01-22 08:49:28 +00005522 add_ptp_error_to_errorstack(device, ret, "ACCESS DENIED.");
rreardonbbb4e562006-11-19 22:16:11 +00005523 }
5524 return -1;
5525 }
5526 } else if (ptp_operation_issupported(params,PTP_OC_SendObjectInfo)) {
Linus Walleij07795772007-08-06 18:44:06 +00005527 PTPObjectInfo new_file;
5528
5529 memset(&new_file, 0, sizeof(PTPObjectInfo));
5530
5531 new_file.Filename = filedata->filename;
Linus Walleijfec4d562008-06-01 22:30:36 +00005532 if (FLAG_ONLY_7BIT_FILENAMES(ptp_usb)) {
Linus Walleijd3b78572007-08-24 21:28:24 +00005533 strip_7bit_from_utf8(new_file.Filename);
5534 }
Richard Low5f6fff82009-04-11 12:45:04 +00005535 // We lose precision here.
Linus Walleij1b87ea72007-08-28 07:53:09 +00005536 new_file.ObjectCompressedSize = (uint32_t) filedata->filesize;
Linus Walleij07795772007-08-06 18:44:06 +00005537 new_file.ObjectFormat = of;
5538 new_file.StorageID = store;
5539 new_file.ParentObject = localph;
Richard Low5f6fff82009-04-11 12:45:04 +00005540 new_file.ModificationDate = time(NULL);
Linus Walleij07795772007-08-06 18:44:06 +00005541
Linus Walleij9be685b2006-11-21 09:44:53 +00005542 // Create the object
5543 ret = ptp_sendobjectinfo(params, &store, &localph, &filedata->item_id, &new_file);
Linus Walleij07795772007-08-06 18:44:06 +00005544
Linus Walleij9be685b2006-11-21 09:44:53 +00005545 if (ret != PTP_RC_OK) {
Richard Lowd3b17022009-04-11 12:37:39 +00005546 add_ptp_error_to_errorstack(device, ret, "send_file_object_info(): "
Linus Walleij07795772007-08-06 18:44:06 +00005547 "Could not send object info.");
Linus Walleij9be685b2006-11-21 09:44:53 +00005548 if (ret == PTP_RC_AccessDenied) {
Linus Walleij070e9b42007-01-22 08:49:28 +00005549 add_ptp_error_to_errorstack(device, ret, "ACCESS DENIED.");
Linus Walleij9be685b2006-11-21 09:44:53 +00005550 }
5551 return -1;
5552 }
Linus Walleijf0bf4372007-07-01 21:47:38 +00005553 // NOTE: the char* pointers inside new_file are not copies so don't
5554 // try to destroy this objectinfo!
rreardonbbb4e562006-11-19 22:16:11 +00005555 }
Linus Walleijd2778d12007-08-06 19:24:58 +00005556
5557 // Now there IS an object with this parent handle.
5558 filedata->parent_id = localph;
Linus Walleij1b87ea72007-08-28 07:53:09 +00005559
Linus Walleijd208f9c2006-04-27 14:16:06 +00005560 return 0;
5561}
5562
Linus Walleij17e39f72006-02-23 15:54:28 +00005563/**
Linus Walleij9036b332008-05-23 21:01:36 +00005564 * This function updates the MTP track object metadata on a
5565 * single file identified by an object ID.
mopoke96143402006-10-30 04:37:26 +00005566 * @param device a pointer to the device to update the track
Linus Walleij95698cd2006-02-24 10:40:40 +00005567 * metadata on.
Linus Walleij17e39f72006-02-23 15:54:28 +00005568 * @param metadata a track metadata set to be written to the file.
5569 * notice that the <code>track_id</code> field of the
5570 * metadata structure must be correct so that the
Linus Walleija4982732006-02-24 15:46:02 +00005571 * function can update the right file. If some properties
5572 * of this metadata are set to NULL (strings) or 0
5573 * (numerical values) they will be discarded and the
5574 * track will not be tagged with these blank values.
Richard Lowaf20b5d2006-12-17 18:00:59 +00005575 * @return 0 on success, any other value means failure. If some
5576 * or all of the properties fail to update we will still
5577 * return success. On some devices (notably iRiver T30)
5578 * properties that exist cannot be updated.
Linus Walleij17e39f72006-02-23 15:54:28 +00005579 */
mopoke96143402006-10-30 04:37:26 +00005580int LIBMTP_Update_Track_Metadata(LIBMTP_mtpdevice_t *device,
Linus Walleij17e39f72006-02-23 15:54:28 +00005581 LIBMTP_track_t const * const metadata)
5582{
Linus Walleij17e39f72006-02-23 15:54:28 +00005583 uint16_t ret;
Linus Walleij00cf0642006-07-26 20:40:59 +00005584 PTPParams *params = (PTPParams *) device->params;
Linus Walleijf9267e92007-10-15 21:07:37 +00005585 PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo;
Linus Walleij00cf0642006-07-26 20:40:59 +00005586 uint32_t i;
Linus Walleij1e9a0332007-09-12 19:35:56 +00005587 uint16_t *properties = NULL;
Linus Walleij00cf0642006-07-26 20:40:59 +00005588 uint32_t propcnt = 0;
Linus Walleij17e39f72006-02-23 15:54:28 +00005589
mopoke96143402006-10-30 04:37:26 +00005590 // First see which properties can be set on this file format and apply accordingly
Linus Walleij00cf0642006-07-26 20:40:59 +00005591 // i.e only try to update this metadata for object tags that exist on the current player.
Linus Walleij1e9a0332007-09-12 19:35:56 +00005592 ret = ptp_mtp_getobjectpropssupported(params, map_libmtp_type_to_ptp_type(metadata->filetype), &propcnt, &properties);
Linus Walleij00cf0642006-07-26 20:40:59 +00005593 if (ret != PTP_RC_OK) {
5594 // Just bail out for now, nothing is ever set.
Linus Walleije7df6532007-03-23 08:20:06 +00005595 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): "
5596 "could not retrieve supported object properties.");
raveloxd9a28642006-05-26 23:42:22 +00005597 return -1;
Richard Low15731fe2007-03-22 20:27:20 +00005598 }
Linus Walleijf9267e92007-10-15 21:07:37 +00005599 if (ptp_operation_issupported(params, PTP_OC_MTP_SetObjPropList) &&
Linus Walleijfec4d562008-06-01 22:30:36 +00005600 !FLAG_BROKEN_SET_OBJECT_PROPLIST(ptp_usb)) {
Linus Walleij1e9a0332007-09-12 19:35:56 +00005601 MTPProperties *props = NULL;
5602 MTPProperties *prop = NULL;
5603 int nrofprops = 0;
Richard Low15731fe2007-03-22 20:27:20 +00005604
5605 for (i=0;i<propcnt;i++) {
Linus Walleijdbcc8242007-08-05 22:31:26 +00005606 PTPObjectPropDesc opd;
5607
Linus Walleij1e9a0332007-09-12 19:35:56 +00005608 ret = ptp_mtp_getobjectpropdesc(params, properties[i], map_libmtp_type_to_ptp_type(metadata->filetype), &opd);
Linus Walleijdbcc8242007-08-05 22:31:26 +00005609 if (ret != PTP_RC_OK) {
5610 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): "
5611 "could not get property description.");
5612 } else if (opd.GetSet) {
Linus Walleij1e9a0332007-09-12 19:35:56 +00005613 switch (properties[i]) {
Linus Walleijdbcc8242007-08-05 22:31:26 +00005614 case PTP_OPC_Name:
5615 if (metadata->title == NULL)
5616 break;
Linus Walleija6d0d482007-10-31 08:54:56 +00005617 prop = ptp_get_new_object_prop_entry(&props, &nrofprops);
Linus Walleije7df6532007-03-23 08:20:06 +00005618 prop->ObjectHandle = metadata->item_id;
5619 prop->property = PTP_OPC_Name;
5620 prop->datatype = PTP_DTC_STR;
Linus Walleijdbcc8242007-08-05 22:31:26 +00005621 prop->propval.str = strdup(metadata->title);
Linus Walleije7df6532007-03-23 08:20:06 +00005622 break;
Linus Walleijdbcc8242007-08-05 22:31:26 +00005623 case PTP_OPC_AlbumName:
5624 if (metadata->album == NULL)
5625 break;
Linus Walleija6d0d482007-10-31 08:54:56 +00005626 prop = ptp_get_new_object_prop_entry(&props, &nrofprops);
Linus Walleije7df6532007-03-23 08:20:06 +00005627 prop->ObjectHandle = metadata->item_id;
5628 prop->property = PTP_OPC_AlbumName;
5629 prop->datatype = PTP_DTC_STR;
Linus Walleijdbcc8242007-08-05 22:31:26 +00005630 prop->propval.str = strdup(metadata->album);
Linus Walleije7df6532007-03-23 08:20:06 +00005631 break;
Linus Walleijdbcc8242007-08-05 22:31:26 +00005632 case PTP_OPC_Artist:
5633 if (metadata->artist == NULL)
5634 break;
Linus Walleija6d0d482007-10-31 08:54:56 +00005635 prop = ptp_get_new_object_prop_entry(&props, &nrofprops);
Linus Walleije7df6532007-03-23 08:20:06 +00005636 prop->ObjectHandle = metadata->item_id;
5637 prop->property = PTP_OPC_Artist;
5638 prop->datatype = PTP_DTC_STR;
Linus Walleijdbcc8242007-08-05 22:31:26 +00005639 prop->propval.str = strdup(metadata->artist);
Linus Walleije7df6532007-03-23 08:20:06 +00005640 break;
Linus Walleij31b74292008-05-02 23:29:06 +00005641 case PTP_OPC_Composer:
5642 if (metadata->composer == NULL)
5643 break;
5644 prop = ptp_get_new_object_prop_entry(&props, &nrofprops);
5645 prop->ObjectHandle = metadata->item_id;
5646 prop->property = PTP_OPC_Composer;
5647 prop->datatype = PTP_DTC_STR;
5648 prop->propval.str = strdup(metadata->composer);
5649 break;
Linus Walleijdbcc8242007-08-05 22:31:26 +00005650 case PTP_OPC_Genre:
5651 if (metadata->genre == NULL)
5652 break;
Linus Walleija6d0d482007-10-31 08:54:56 +00005653 prop = ptp_get_new_object_prop_entry(&props, &nrofprops);
Linus Walleije7df6532007-03-23 08:20:06 +00005654 prop->ObjectHandle = metadata->item_id;
5655 prop->property = PTP_OPC_Genre;
5656 prop->datatype = PTP_DTC_STR;
Linus Walleijdbcc8242007-08-05 22:31:26 +00005657 prop->propval.str = strdup(metadata->genre);
Linus Walleije7df6532007-03-23 08:20:06 +00005658 break;
Linus Walleijdbcc8242007-08-05 22:31:26 +00005659 case PTP_OPC_Duration:
Linus Walleija6d0d482007-10-31 08:54:56 +00005660 prop = ptp_get_new_object_prop_entry(&props, &nrofprops);
Linus Walleije7df6532007-03-23 08:20:06 +00005661 prop->ObjectHandle = metadata->item_id;
5662 prop->property = PTP_OPC_Duration;
5663 prop->datatype = PTP_DTC_UINT32;
Linus Walleij29559562007-08-22 21:38:04 +00005664 prop->propval.u32 = adjust_u32(metadata->duration, &opd);
Linus Walleije7df6532007-03-23 08:20:06 +00005665 break;
Linus Walleijdbcc8242007-08-05 22:31:26 +00005666 case PTP_OPC_Track:
Linus Walleija6d0d482007-10-31 08:54:56 +00005667 prop = ptp_get_new_object_prop_entry(&props, &nrofprops);
Linus Walleije7df6532007-03-23 08:20:06 +00005668 prop->ObjectHandle = metadata->item_id;
5669 prop->property = PTP_OPC_Track;
5670 prop->datatype = PTP_DTC_UINT16;
Linus Walleij29559562007-08-22 21:38:04 +00005671 prop->propval.u16 = adjust_u16(metadata->tracknumber, &opd);
Linus Walleije7df6532007-03-23 08:20:06 +00005672 break;
Linus Walleijdbcc8242007-08-05 22:31:26 +00005673 case PTP_OPC_OriginalReleaseDate:
5674 if (metadata->date == NULL)
5675 break;
Linus Walleija6d0d482007-10-31 08:54:56 +00005676 prop = ptp_get_new_object_prop_entry(&props, &nrofprops);
Linus Walleije7df6532007-03-23 08:20:06 +00005677 prop->ObjectHandle = metadata->item_id;
5678 prop->property = PTP_OPC_OriginalReleaseDate;
5679 prop->datatype = PTP_DTC_STR;
Linus Walleijdbcc8242007-08-05 22:31:26 +00005680 prop->propval.str = strdup(metadata->date);
Linus Walleije7df6532007-03-23 08:20:06 +00005681 break;
Linus Walleijdbcc8242007-08-05 22:31:26 +00005682 case PTP_OPC_SampleRate:
Linus Walleija6d0d482007-10-31 08:54:56 +00005683 prop = ptp_get_new_object_prop_entry(&props, &nrofprops);
Linus Walleije7df6532007-03-23 08:20:06 +00005684 prop->ObjectHandle = metadata->item_id;
5685 prop->property = PTP_OPC_SampleRate;
5686 prop->datatype = PTP_DTC_UINT32;
Linus Walleij29559562007-08-22 21:38:04 +00005687 prop->propval.u32 = adjust_u32(metadata->samplerate, &opd);
Linus Walleije7df6532007-03-23 08:20:06 +00005688 break;
Linus Walleijdbcc8242007-08-05 22:31:26 +00005689 case PTP_OPC_NumberOfChannels:
Linus Walleija6d0d482007-10-31 08:54:56 +00005690 prop = ptp_get_new_object_prop_entry(&props, &nrofprops);
Linus Walleije7df6532007-03-23 08:20:06 +00005691 prop->ObjectHandle = metadata->item_id;
5692 prop->property = PTP_OPC_NumberOfChannels;
5693 prop->datatype = PTP_DTC_UINT16;
Linus Walleij29559562007-08-22 21:38:04 +00005694 prop->propval.u16 = adjust_u16(metadata->nochannels, &opd);
Linus Walleije7df6532007-03-23 08:20:06 +00005695 break;
Linus Walleijdbcc8242007-08-05 22:31:26 +00005696 case PTP_OPC_AudioWAVECodec:
Linus Walleija6d0d482007-10-31 08:54:56 +00005697 prop = ptp_get_new_object_prop_entry(&props, &nrofprops);
Linus Walleije7df6532007-03-23 08:20:06 +00005698 prop->ObjectHandle = metadata->item_id;
5699 prop->property = PTP_OPC_AudioWAVECodec;
5700 prop->datatype = PTP_DTC_UINT32;
Linus Walleij29559562007-08-22 21:38:04 +00005701 prop->propval.u32 = adjust_u32(metadata->wavecodec, &opd);
Linus Walleije7df6532007-03-23 08:20:06 +00005702 break;
Linus Walleijdbcc8242007-08-05 22:31:26 +00005703 case PTP_OPC_AudioBitRate:
Linus Walleija6d0d482007-10-31 08:54:56 +00005704 prop = ptp_get_new_object_prop_entry(&props, &nrofprops);
Linus Walleije7df6532007-03-23 08:20:06 +00005705 prop->ObjectHandle = metadata->item_id;
5706 prop->property = PTP_OPC_AudioBitRate;
5707 prop->datatype = PTP_DTC_UINT32;
Linus Walleij29559562007-08-22 21:38:04 +00005708 prop->propval.u32 = adjust_u32(metadata->bitrate, &opd);
Linus Walleije7df6532007-03-23 08:20:06 +00005709 break;
Linus Walleijdbcc8242007-08-05 22:31:26 +00005710 case PTP_OPC_BitRateType:
Linus Walleija6d0d482007-10-31 08:54:56 +00005711 prop = ptp_get_new_object_prop_entry(&props, &nrofprops);
Linus Walleije7df6532007-03-23 08:20:06 +00005712 prop->ObjectHandle = metadata->item_id;
5713 prop->property = PTP_OPC_BitRateType;
5714 prop->datatype = PTP_DTC_UINT16;
Linus Walleij29559562007-08-22 21:38:04 +00005715 prop->propval.u16 = adjust_u16(metadata->bitratetype, &opd);
Linus Walleije7df6532007-03-23 08:20:06 +00005716 break;
Linus Walleijdbcc8242007-08-05 22:31:26 +00005717 case PTP_OPC_Rating:
5718 // TODO: shall this be set for rating 0?
5719 if (metadata->rating == 0)
5720 break;
Linus Walleija6d0d482007-10-31 08:54:56 +00005721 prop = ptp_get_new_object_prop_entry(&props, &nrofprops);
Linus Walleijdbcc8242007-08-05 22:31:26 +00005722 prop->ObjectHandle = metadata->item_id;
5723 prop->property = PTP_OPC_Rating;
5724 prop->datatype = PTP_DTC_UINT16;
Linus Walleij29559562007-08-22 21:38:04 +00005725 prop->propval.u16 = adjust_u16(metadata->rating, &opd);
Linus Walleijdbcc8242007-08-05 22:31:26 +00005726 break;
5727 case PTP_OPC_UseCount:
Linus Walleija6d0d482007-10-31 08:54:56 +00005728 prop = ptp_get_new_object_prop_entry(&props, &nrofprops);
Linus Walleije7df6532007-03-23 08:20:06 +00005729 prop->ObjectHandle = metadata->item_id;
5730 prop->property = PTP_OPC_UseCount;
5731 prop->datatype = PTP_DTC_UINT32;
Linus Walleij29559562007-08-22 21:38:04 +00005732 prop->propval.u32 = adjust_u32(metadata->usecount, &opd);
Linus Walleij7783b9b2007-09-22 22:10:44 +00005733 break;
5734 case PTP_OPC_DateModified:
Linus Walleijcf8dc2b2008-10-21 13:58:36 +00005735 if (!FLAG_CANNOT_HANDLE_DATEMODIFIED(ptp_usb)) {
Linus Walleij37588142008-10-16 19:10:47 +00005736 // Tag with current time if that is supported
5737 prop = ptp_get_new_object_prop_entry(&props, &nrofprops);
5738 prop->ObjectHandle = metadata->item_id;
5739 prop->property = PTP_OPC_DateModified;
5740 prop->datatype = PTP_DTC_STR;
5741 prop->propval.str = get_iso8601_stamp();
5742 }
Linus Walleij7783b9b2007-09-22 22:10:44 +00005743 break;
Linus Walleijf9267e92007-10-15 21:07:37 +00005744 default:
5745 break;
Linus Walleije7df6532007-03-23 08:20:06 +00005746 }
Linus Walleij304433d2007-02-26 08:02:47 +00005747 }
Linus Walleijdbcc8242007-08-05 22:31:26 +00005748 ptp_free_objectpropdesc(&opd);
Richard Low15731fe2007-03-22 20:27:20 +00005749 }
Linus Walleijdbcc8242007-08-05 22:31:26 +00005750
Richard Low15731fe2007-03-22 20:27:20 +00005751 // NOTE: File size is not updated, this should not change anyway.
5752 // neither will we change the filename.
Linus Walleijdbcc8242007-08-05 22:31:26 +00005753
Linus Walleij1e9a0332007-09-12 19:35:56 +00005754 ret = ptp_mtp_setobjectproplist(params, props, nrofprops);
Linus Walleijdbcc8242007-08-05 22:31:26 +00005755
Linus Walleija6d0d482007-10-31 08:54:56 +00005756 ptp_destroy_object_prop_list(props, nrofprops);
Linus Walleijdbcc8242007-08-05 22:31:26 +00005757
Richard Low15731fe2007-03-22 20:27:20 +00005758 if (ret != PTP_RC_OK) {
5759 // TODO: return error of which property we couldn't set
Linus Walleije7df6532007-03-23 08:20:06 +00005760 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): "
5761 "could not set object property list.");
Richard Low016e3732007-09-23 14:53:46 +00005762 free(properties);
Linus Walleij304433d2007-02-26 08:02:47 +00005763 return -1;
5764 }
Linus Walleijdbcc8242007-08-05 22:31:26 +00005765
Richard Low15731fe2007-03-22 20:27:20 +00005766 } else if (ptp_operation_issupported(params,PTP_OC_MTP_SetObjectPropValue)) {
Linus Walleij00cf0642006-07-26 20:40:59 +00005767 for (i=0;i<propcnt;i++) {
Linus Walleijdbcc8242007-08-05 22:31:26 +00005768 PTPObjectPropDesc opd;
5769
Linus Walleij1e9a0332007-09-12 19:35:56 +00005770 ret = ptp_mtp_getobjectpropdesc(params, properties[i], map_libmtp_type_to_ptp_type(metadata->filetype), &opd);
Linus Walleijdbcc8242007-08-05 22:31:26 +00005771 if (ret != PTP_RC_OK) {
5772 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): "
5773 "could not get property description.");
5774 } else if (opd.GetSet) {
Linus Walleij1e9a0332007-09-12 19:35:56 +00005775 switch (properties[i]) {
Linus Walleijdbcc8242007-08-05 22:31:26 +00005776 case PTP_OPC_Name:
5777 // Update title
5778 ret = set_object_string(device, metadata->item_id, PTP_OPC_Name, metadata->title);
Linus Walleije7df6532007-03-23 08:20:06 +00005779 if (ret != 0) {
5780 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): "
Linus Walleijdbcc8242007-08-05 22:31:26 +00005781 "could not set track title.");
Linus Walleije7df6532007-03-23 08:20:06 +00005782 }
Linus Walleijdbcc8242007-08-05 22:31:26 +00005783 break;
5784 case PTP_OPC_AlbumName:
5785 // Update album
5786 ret = set_object_string(device, metadata->item_id, PTP_OPC_AlbumName, metadata->album);
Linus Walleije7df6532007-03-23 08:20:06 +00005787 if (ret != 0) {
5788 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): "
Linus Walleijdbcc8242007-08-05 22:31:26 +00005789 "could not set track album name.");
Linus Walleije7df6532007-03-23 08:20:06 +00005790 }
Linus Walleijdbcc8242007-08-05 22:31:26 +00005791 break;
5792 case PTP_OPC_Artist:
5793 // Update artist
5794 ret = set_object_string(device, metadata->item_id, PTP_OPC_Artist, metadata->artist);
Linus Walleije7df6532007-03-23 08:20:06 +00005795 if (ret != 0) {
5796 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): "
Linus Walleijdbcc8242007-08-05 22:31:26 +00005797 "could not set track artist name.");
Linus Walleije7df6532007-03-23 08:20:06 +00005798 }
Linus Walleijdbcc8242007-08-05 22:31:26 +00005799 break;
Linus Walleij31b74292008-05-02 23:29:06 +00005800 case PTP_OPC_Composer:
5801 // Update composer
5802 ret = set_object_string(device, metadata->item_id, PTP_OPC_Composer, metadata->composer);
5803 if (ret != 0) {
5804 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): "
5805 "could not set track composer name.");
5806 }
5807 break;
Linus Walleijdbcc8242007-08-05 22:31:26 +00005808 case PTP_OPC_Genre:
5809 // Update genre
5810 ret = set_object_string(device, metadata->item_id, PTP_OPC_Genre, metadata->genre);
5811 if (ret != 0) {
5812 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): "
5813 "could not set track genre name.");
5814 }
5815 break;
5816 case PTP_OPC_Duration:
5817 // Update duration
5818 if (metadata->duration != 0) {
Linus Walleij29559562007-08-22 21:38:04 +00005819 ret = set_object_u32(device, metadata->item_id, PTP_OPC_Duration, adjust_u32(metadata->duration, &opd));
Linus Walleijdbcc8242007-08-05 22:31:26 +00005820 if (ret != 0) {
5821 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): "
5822 "could not set track duration.");
5823 }
5824 }
5825 break;
5826 case PTP_OPC_Track:
5827 // Update track number.
5828 if (metadata->tracknumber != 0) {
Linus Walleij29559562007-08-22 21:38:04 +00005829 ret = set_object_u16(device, metadata->item_id, PTP_OPC_Track, adjust_u16(metadata->tracknumber, &opd));
Linus Walleijdbcc8242007-08-05 22:31:26 +00005830 if (ret != 0) {
5831 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): "
5832 "could not set track tracknumber.");
5833 }
5834 }
5835 break;
5836 case PTP_OPC_OriginalReleaseDate:
5837 // Update creation datetime
5838 ret = set_object_string(device, metadata->item_id, PTP_OPC_OriginalReleaseDate, metadata->date);
5839 if (ret != 0) {
5840 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): "
5841 "could not set track release date.");
5842 }
5843 break;
5844 // These are, well not so important.
5845 case PTP_OPC_SampleRate:
5846 // Update sample rate
5847 if (metadata->samplerate != 0) {
Linus Walleij29559562007-08-22 21:38:04 +00005848 ret = set_object_u32(device, metadata->item_id, PTP_OPC_SampleRate, adjust_u32(metadata->samplerate, &opd));
Linus Walleijdbcc8242007-08-05 22:31:26 +00005849 if (ret != 0) {
5850 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): "
5851 "could not set samplerate.");
5852 }
5853 }
5854 break;
5855 case PTP_OPC_NumberOfChannels:
5856 // Update number of channels
5857 if (metadata->nochannels != 0) {
Linus Walleij29559562007-08-22 21:38:04 +00005858 ret = set_object_u16(device, metadata->item_id, PTP_OPC_NumberOfChannels, adjust_u16(metadata->nochannels, &opd));
Linus Walleije7df6532007-03-23 08:20:06 +00005859 if (ret != 0) {
5860 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): "
5861 "could not set number of channels.");
5862 }
5863 }
Linus Walleijdbcc8242007-08-05 22:31:26 +00005864 break;
5865 case PTP_OPC_AudioWAVECodec:
5866 // Update WAVE codec
5867 if (metadata->wavecodec != 0) {
Linus Walleij29559562007-08-22 21:38:04 +00005868 ret = set_object_u32(device, metadata->item_id, PTP_OPC_AudioWAVECodec, adjust_u32(metadata->wavecodec, &opd));
Linus Walleijdbcc8242007-08-05 22:31:26 +00005869 if (ret != 0) {
5870 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): "
5871 "could not set WAVE codec.");
5872 }
5873 }
5874 break;
5875 case PTP_OPC_AudioBitRate:
5876 // Update bitrate
5877 if (metadata->bitrate != 0) {
Linus Walleij29559562007-08-22 21:38:04 +00005878 ret = set_object_u32(device, metadata->item_id, PTP_OPC_AudioBitRate, adjust_u32(metadata->bitrate, &opd));
Linus Walleijdbcc8242007-08-05 22:31:26 +00005879 if (ret != 0) {
5880 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): "
5881 "could not set bitrate.");
5882 }
5883 }
5884 break;
5885 case PTP_OPC_BitRateType:
5886 // Update bitrate type
5887 if (metadata->bitratetype != 0) {
Linus Walleij29559562007-08-22 21:38:04 +00005888 ret = set_object_u16(device, metadata->item_id, PTP_OPC_BitRateType, adjust_u16(metadata->bitratetype, &opd));
Linus Walleijdbcc8242007-08-05 22:31:26 +00005889 if (ret != 0) {
5890 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): "
5891 "could not set bitratetype.");
5892 }
5893 }
5894 break;
5895 case PTP_OPC_Rating:
5896 // Update user rating
5897 // TODO: shall this be set for rating 0?
5898 if (metadata->rating != 0) {
Linus Walleij29559562007-08-22 21:38:04 +00005899 ret = set_object_u16(device, metadata->item_id, PTP_OPC_Rating, adjust_u16(metadata->rating, &opd));
Linus Walleijdbcc8242007-08-05 22:31:26 +00005900 if (ret != 0) {
5901 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): "
5902 "could not set user rating.");
5903 }
5904 }
5905 break;
5906 case PTP_OPC_UseCount:
5907 // Update use count, set even to zero if desired.
Linus Walleij29559562007-08-22 21:38:04 +00005908 ret = set_object_u32(device, metadata->item_id, PTP_OPC_UseCount, adjust_u32(metadata->usecount, &opd));
Linus Walleije7df6532007-03-23 08:20:06 +00005909 if (ret != 0) {
5910 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): "
Linus Walleije7df6532007-03-23 08:20:06 +00005911 "could not set use count.");
Linus Walleijdbcc8242007-08-05 22:31:26 +00005912 }
5913 break;
Linus Walleij7783b9b2007-09-22 22:10:44 +00005914 case PTP_OPC_DateModified:
Linus Walleijcf8dc2b2008-10-21 13:58:36 +00005915 if (!FLAG_CANNOT_HANDLE_DATEMODIFIED(ptp_usb)) {
Linus Walleij7783b9b2007-09-22 22:10:44 +00005916 // Update modification time if supported
5917 char *tmpstamp = get_iso8601_stamp();
5918 ret = set_object_string(device, metadata->item_id, PTP_OPC_DateModified, tmpstamp);
5919 if (ret != 0) {
5920 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): "
5921 "could not set modification date.");
5922 }
5923 free(tmpstamp);
5924 }
5925 break;
Linus Walleijdbcc8242007-08-05 22:31:26 +00005926
5927 // NOTE: File size is not updated, this should not change anyway.
5928 // neither will we change the filename.
5929 default:
5930 break;
Linus Walleije7df6532007-03-23 08:20:06 +00005931 }
Linus Walleij00cf0642006-07-26 20:40:59 +00005932 }
Linus Walleijdbcc8242007-08-05 22:31:26 +00005933 ptp_free_objectpropdesc(&opd);
Linus Walleija4982732006-02-24 15:46:02 +00005934 }
Richard Low15731fe2007-03-22 20:27:20 +00005935 } else {
5936 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): "
5937 "Your device doesn't seem to support any known way of setting metadata.");
Richard Low016e3732007-09-23 14:53:46 +00005938 free(properties);
Richard Low15731fe2007-03-22 20:27:20 +00005939 return -1;
Linus Walleij17e39f72006-02-23 15:54:28 +00005940 }
Linus Walleije7df6532007-03-23 08:20:06 +00005941
tsaarnia3eb60a2007-07-06 17:41:30 +00005942 // update cached object properties if metadata cache exists
Linus Walleij7752b952007-10-19 20:11:46 +00005943 update_metadata_cache(device, metadata->item_id);
Linus Walleij338ade42007-07-03 20:44:08 +00005944
Richard Low016e3732007-09-23 14:53:46 +00005945 free(properties);
5946
Linus Walleijf1b02f22006-12-06 09:03:23 +00005947 return 0;
Linus Walleij394bbbe2006-02-22 16:10:53 +00005948}
Linus Walleij95698cd2006-02-24 10:40:40 +00005949
5950/**
Linus Walleij399807b2008-01-27 22:05:12 +00005951 * This function deletes a single file, track, playlist, folder or
5952 * any other object off the MTP device, identified by the object ID.
5953 *
5954 * If you delete a folder, there is no guarantee that the device will
5955 * really delete all the files that were in that folder, rather it is
5956 * expected that they will not be deleted, and will turn up in object
5957 * listings with parent set to a non-existant object ID. The safe way
5958 * to do this is to recursively delete all files (and folders) contained
5959 * in the folder, then the folder itself.
5960 *
5961 * @param device a pointer to the device to delete the object from.
5962 * @param object_id the object to delete.
Linus Walleij95698cd2006-02-24 10:40:40 +00005963 * @return 0 on success, any other value means failure.
5964 */
mopoke96143402006-10-30 04:37:26 +00005965int LIBMTP_Delete_Object(LIBMTP_mtpdevice_t *device,
Linus Walleij438bd7f2006-06-08 11:35:44 +00005966 uint32_t object_id)
Linus Walleij95698cd2006-02-24 10:40:40 +00005967{
Linus Walleij438bd7f2006-06-08 11:35:44 +00005968 uint16_t ret;
5969 PTPParams *params = (PTPParams *) device->params;
5970
5971 ret = ptp_deleteobject(params, object_id, 0);
5972 if (ret != PTP_RC_OK) {
Linus Walleij070e9b42007-01-22 08:49:28 +00005973 add_ptp_error_to_errorstack(device, ret, "LIBMTP_Delete_Object(): could not delete object.");
Linus Walleij438bd7f2006-06-08 11:35:44 +00005974 return -1;
5975 }
tsaarnia3eb60a2007-07-06 17:41:30 +00005976
Linus Walleij438bd7f2006-06-08 11:35:44 +00005977 return 0;
Linus Walleij95698cd2006-02-24 10:40:40 +00005978}
Linus Walleij9c6ca022006-04-21 10:24:15 +00005979
Linus Walleij9c6ca022006-04-21 10:24:15 +00005980/**
Linus Walleij8d8c4352008-09-23 23:04:16 +00005981 * Internal function to update an object filename property.
Linus Walleijde8193f2008-01-27 14:55:31 +00005982 */
Linus Walleij8d8c4352008-09-23 23:04:16 +00005983static int set_object_filename(LIBMTP_mtpdevice_t *device,
5984 uint32_t object_id, uint16_t ptp_type,
5985 const char **newname_ptr)
Linus Walleijde8193f2008-01-27 14:55:31 +00005986{
5987 PTPParams *params = (PTPParams *) device->params;
5988 PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo;
5989 PTPObjectPropDesc opd;
5990 uint16_t ret;
Linus Walleij8d8c4352008-09-23 23:04:16 +00005991 char *newname;
Linus Walleij8be2c032008-01-28 21:23:47 +00005992
5993 // See if we can modify the filename on this kind of files.
5994 ret = ptp_mtp_getobjectpropdesc(params, PTP_OPC_ObjectFileName, ptp_type, &opd);
Linus Walleijde8193f2008-01-27 14:55:31 +00005995 if (ret != PTP_RC_OK) {
Linus Walleij8d8c4352008-09-23 23:04:16 +00005996 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "set_object_filename(): "
Linus Walleij8be2c032008-01-28 21:23:47 +00005997 "could not get property description.");
Linus Walleijde8193f2008-01-27 14:55:31 +00005998 return -1;
5999 }
6000
6001 if (!opd.GetSet) {
6002 ptp_free_objectpropdesc(&opd);
Linus Walleij8d8c4352008-09-23 23:04:16 +00006003 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "set_object_filename(): "
6004 " property is not settable.");
Linus Walleij399807b2008-01-27 22:05:12 +00006005 // TODO: we COULD actually upload/download the object here, if we feel
6006 // like wasting time for the user.
Linus Walleijde8193f2008-01-27 14:55:31 +00006007 return -1;
6008 }
6009
Linus Walleij8d8c4352008-09-23 23:04:16 +00006010 newname = strdup(*newname_ptr);
6011
Linus Walleijfec4d562008-06-01 22:30:36 +00006012 if (FLAG_ONLY_7BIT_FILENAMES(ptp_usb)) {
Linus Walleijde8193f2008-01-27 14:55:31 +00006013 strip_7bit_from_utf8(newname);
6014 }
6015
6016 if (ptp_operation_issupported(params, PTP_OC_MTP_SetObjPropList) &&
Linus Walleijfec4d562008-06-01 22:30:36 +00006017 !FLAG_BROKEN_SET_OBJECT_PROPLIST(ptp_usb)) {
Linus Walleijde8193f2008-01-27 14:55:31 +00006018 MTPProperties *props = NULL;
6019 MTPProperties *prop = NULL;
6020 int nrofprops = 0;
Linus Walleij8d8c4352008-09-23 23:04:16 +00006021
Linus Walleijde8193f2008-01-27 14:55:31 +00006022 prop = ptp_get_new_object_prop_entry(&props, &nrofprops);
Linus Walleij8d8c4352008-09-23 23:04:16 +00006023 prop->ObjectHandle = object_id;
Linus Walleijde8193f2008-01-27 14:55:31 +00006024 prop->property = PTP_OPC_ObjectFileName;
6025 prop->datatype = PTP_DTC_STR;
Linus Walleij8d8c4352008-09-23 23:04:16 +00006026 prop->propval.str = newname;
6027
Linus Walleijde8193f2008-01-27 14:55:31 +00006028 ret = ptp_mtp_setobjectproplist(params, props, nrofprops);
Linus Walleij8d8c4352008-09-23 23:04:16 +00006029
Linus Walleijde8193f2008-01-27 14:55:31 +00006030 ptp_destroy_object_prop_list(props, nrofprops);
Linus Walleij8d8c4352008-09-23 23:04:16 +00006031
Linus Walleijde8193f2008-01-27 14:55:31 +00006032 if (ret != PTP_RC_OK) {
Linus Walleij8d8c4352008-09-23 23:04:16 +00006033 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "set_object_filename(): "
6034 " could not set object property list.");
6035 ptp_free_objectpropdesc(&opd);
6036 return -1;
Linus Walleijde8193f2008-01-27 14:55:31 +00006037 }
6038 } else if (ptp_operation_issupported(params, PTP_OC_MTP_SetObjectPropValue)) {
6039 ret = set_object_string(device, object_id, PTP_OPC_ObjectFileName, newname);
6040 if (ret != 0) {
Linus Walleij8d8c4352008-09-23 23:04:16 +00006041 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "set_object_filename(): "
6042 " could not set object filename.");
Linus Walleijde8193f2008-01-27 14:55:31 +00006043 ptp_free_objectpropdesc(&opd);
6044 return -1;
6045 }
6046 } else {
Linus Walleij8d8c4352008-09-23 23:04:16 +00006047 free(newname);
6048 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "set_object_filename(): "
6049 " your device doesn't seem to support any known way of setting metadata.");
Linus Walleijde8193f2008-01-27 14:55:31 +00006050 ptp_free_objectpropdesc(&opd);
6051 return -1;
6052 }
Linus Walleij8d8c4352008-09-23 23:04:16 +00006053
Linus Walleijde8193f2008-01-27 14:55:31 +00006054 ptp_free_objectpropdesc(&opd);
Linus Walleij8d8c4352008-09-23 23:04:16 +00006055
Linus Walleijde8193f2008-01-27 14:55:31 +00006056 // update cached object properties if metadata cache exists
6057 update_metadata_cache(device, object_id);
Linus Walleij8d8c4352008-09-23 23:04:16 +00006058
Linus Walleijde8193f2008-01-27 14:55:31 +00006059 return 0;
6060}
Linus Walleij399807b2008-01-27 22:05:12 +00006061
Linus Walleijde8193f2008-01-27 14:55:31 +00006062/**
Linus Walleij8d8c4352008-09-23 23:04:16 +00006063 * This function renames a single file.
6064 * This simply means that the PTP_OPC_ObjectFileName property
6065 * is updated, if this is supported by the device.
6066 *
6067 * @param device a pointer to the device that contains the file.
6068 * @param file the file metadata of the file to rename.
6069 * On success, the filename member is updated. Be aware, that
6070 * this name can be different than newname depending of device restrictions.
6071 * @param newname the new filename for this object.
6072 * @return 0 on success, any other value means failure.
6073 */
6074int LIBMTP_Set_File_Name(LIBMTP_mtpdevice_t *device,
6075 LIBMTP_file_t *file, const char *newname)
6076{
6077 int ret;
6078
6079 ret = set_object_filename(device, file->item_id,
6080 map_libmtp_type_to_ptp_type(file->filetype),
6081 &newname);
6082
6083 if (ret != 0) {
6084 return ret;
6085 }
6086
6087 free(file->filename);
6088 file->filename = strdup(newname);
6089 return ret;
6090}
6091
6092/**
6093 * This function renames a single folder.
6094 * This simply means that the PTP_OPC_ObjectFileName property
6095 * is updated, if this is supported by the device.
6096 *
6097 * @param device a pointer to the device that contains the file.
6098 * @param folder the folder metadata of the folder to rename.
6099 * On success, the name member is updated. Be aware, that
6100 * this name can be different than newname depending of device restrictions.
6101 * @param newname the new name for this object.
6102 * @return 0 on success, any other value means failure.
6103 */
6104int LIBMTP_Set_Folder_Name(LIBMTP_mtpdevice_t *device,
6105 LIBMTP_folder_t *folder, const char* newname)
6106{
6107 int ret;
6108
6109 ret = set_object_filename(device, folder->folder_id,
Linus Walleijfb7212f2008-09-24 20:30:13 +00006110 PTP_OFC_Association,
Linus Walleij8d8c4352008-09-23 23:04:16 +00006111 &newname);
6112
6113 if (ret != 0) {
6114 return ret;
6115 }
6116
6117 free(folder->name);
6118 folder->name = strdup(newname);
6119 return ret;
6120}
6121
6122/**
6123 * This function renames a single track.
6124 * This simply means that the PTP_OPC_ObjectFileName property
6125 * is updated, if this is supported by the device.
6126 *
6127 * @param device a pointer to the device that contains the file.
6128 * @param track the track metadata of the track to rename.
6129 * On success, the filename member is updated. Be aware, that
6130 * this name can be different than newname depending of device restrictions.
6131 * @param newname the new filename for this object.
6132 * @return 0 on success, any other value means failure.
6133 */
6134int LIBMTP_Set_Track_Name(LIBMTP_mtpdevice_t *device,
6135 LIBMTP_track_t *track, const char* newname)
6136{
6137 int ret;
6138
6139 ret = set_object_filename(device, track->item_id,
6140 map_libmtp_type_to_ptp_type(track->filetype),
6141 &newname);
6142
6143 if (ret != 0) {
6144 return ret;
6145 }
6146
6147 free(track->filename);
6148 track->filename = strdup(newname);
6149 return ret;
6150}
6151
6152/**
Linus Walleij12bbf312009-01-26 21:46:51 +00006153 * This function renames a single playlist object file holder.
6154 * This simply means that the <code>PTP_OPC_ObjectFileName</code>
6155 * property is updated, if this is supported by the device.
6156 * The playlist filename should nominally end with an extension
6157 * like ".pla".
6158 *
6159 * NOTE: if you want to change the metadata the device display
6160 * about a playlist you must <i>not</i> use this function,
6161 * use <code>LIBMTP_Update_Playlist()</code> instead!
Linus Walleij8d8c4352008-09-23 23:04:16 +00006162 *
6163 * @param device a pointer to the device that contains the file.
6164 * @param playlist the playlist metadata of the playlist to rename.
6165 * On success, the name member is updated. Be aware, that
6166 * this name can be different than newname depending of device restrictions.
6167 * @param newname the new name for this object.
6168 * @return 0 on success, any other value means failure.
Linus Walleij12bbf312009-01-26 21:46:51 +00006169 * @see LIBMTP_Update_Playlist()
Linus Walleij8d8c4352008-09-23 23:04:16 +00006170 */
6171int LIBMTP_Set_Playlist_Name(LIBMTP_mtpdevice_t *device,
6172 LIBMTP_playlist_t *playlist, const char* newname)
6173{
6174 int ret;
6175
6176 ret = set_object_filename(device, playlist->playlist_id,
6177 PTP_OFC_MTP_AbstractAudioVideoPlaylist,
6178 &newname);
6179
6180 if (ret != 0) {
6181 return ret;
6182 }
6183
6184 free(playlist->name);
6185 playlist->name = strdup(newname);
6186 return ret;
6187}
6188
6189/**
6190 * This function renames a single album.
Linus Walleij12bbf312009-01-26 21:46:51 +00006191 * This simply means that the <code>PTP_OPC_ObjectFileName</code>
6192 * property is updated, if this is supported by the device.
6193 * The album filename should nominally end with an extension
6194 * like ".alb".
6195 *
6196 * NOTE: if you want to change the metadata the device display
6197 * about a playlist you must <i>not</i> use this function,
6198 * use <code>LIBMTP_Update_Album()</code> instead!
Linus Walleij8d8c4352008-09-23 23:04:16 +00006199 *
6200 * @param device a pointer to the device that contains the file.
6201 * @param album the album metadata of the album to rename.
6202 * On success, the name member is updated. Be aware, that
6203 * this name can be different than newname depending of device restrictions.
6204 * @param newname the new name for this object.
6205 * @return 0 on success, any other value means failure.
Linus Walleij12bbf312009-01-26 21:46:51 +00006206 * @see LIBMTP_Update_Album()
Linus Walleij8d8c4352008-09-23 23:04:16 +00006207 */
6208int LIBMTP_Set_Album_Name(LIBMTP_mtpdevice_t *device,
6209 LIBMTP_album_t *album, const char* newname)
6210{
6211 int ret;
6212
6213 ret = set_object_filename(device, album->album_id,
6214 PTP_OFC_MTP_AbstractAudioAlbum,
6215 &newname);
6216
6217 if (ret != 0) {
6218 return ret;
6219 }
6220
6221 free(album->name);
6222 album->name = strdup(newname);
6223 return ret;
6224}
6225
6226/**
6227 * THIS FUNCTION IS DEPRECATED. PLEASE UPDATE YOUR CODE IN ORDER
6228 * NOT TO USE IT.
6229 *
6230 * @see LIBMTP_Set_File_Name()
6231 * @see LIBMTP_Set_Track_Name()
6232 * @see LIBMTP_Set_Folder_Name()
6233 * @see LIBMTP_Set_Playlist_Name()
6234 * @see LIBMTP_Set_Album_Name()
6235 */
6236int LIBMTP_Set_Object_Filename(LIBMTP_mtpdevice_t *device,
6237 uint32_t object_id, char* newname)
6238{
6239 int ret;
6240 LIBMTP_file_t *file;
6241
6242 file = LIBMTP_Get_Filemetadata(device, object_id);
Linus Walleijfb7212f2008-09-24 20:30:13 +00006243
6244 if (file == NULL) {
6245 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Set_Object_Filename(): "
6246 "could not get file metadata for target object.");
6247 return -1;
6248 }
Linus Walleij8d8c4352008-09-23 23:04:16 +00006249
Linus Walleijfb7212f2008-09-24 20:30:13 +00006250 ret = set_object_filename(device, object_id, map_libmtp_type_to_ptp_type(file->filetype), (const char **) &newname);
Linus Walleij8d8c4352008-09-23 23:04:16 +00006251
6252 free(file);
6253
6254 return ret;
6255}
6256
6257/**
Linus Walleij9c6ca022006-04-21 10:24:15 +00006258 * Helper function. This indicates if a track exists on the device
6259 * @param device a pointer to the device to get the track from.
6260 * @param id the track ID of the track to retrieve.
Linus Walleij070e9b42007-01-22 08:49:28 +00006261 * @return TRUE (!=0) if the track exists, FALSE (0) if not
Linus Walleij9c6ca022006-04-21 10:24:15 +00006262 */
6263int LIBMTP_Track_Exists(LIBMTP_mtpdevice_t *device,
6264 uint32_t const id)
6265{
Linus Walleij9c6ca022006-04-21 10:24:15 +00006266 PTPParams *params = (PTPParams *) device->params;
Linus Walleijd4637502009-06-14 23:03:33 +00006267 uint16_t ret;
6268 PTPObject *ob;
Linus Walleij9c6ca022006-04-21 10:24:15 +00006269
Linus Walleijd4637502009-06-14 23:03:33 +00006270 ret = ptp_object_want (params, id, 0, &ob);
6271 if (ret == PTP_RC_OK)
Linus Walleijf0bf4372007-07-01 21:47:38 +00006272 return -1;
Linus Walleij9c6ca022006-04-21 10:24:15 +00006273 return 0;
6274}
6275
6276/**
6277 * This creates a new folder structure and allocates memory
6278 * for it. Notice that if you add strings to this structure they
6279 * will be freed by the corresponding <code>LIBMTP_folder_track_t</code>
6280 * operation later, so be careful of using strdup() when assigning
6281 * strings, e.g.:
6282 *
6283 * @return a pointer to the newly allocated folder structure.
6284 * @see LIBMTP_destroy_folder_t()
6285 */
6286LIBMTP_folder_t *LIBMTP_new_folder_t(void)
6287{
6288 LIBMTP_folder_t *new = (LIBMTP_folder_t *) malloc(sizeof(LIBMTP_folder_t));
6289 if (new == NULL) {
6290 return NULL;
6291 }
6292 new->folder_id = 0;
6293 new->parent_id = 0;
Linus Walleijea68f1f2008-06-22 21:54:44 +00006294 new->storage_id = 0;
Linus Walleij9c6ca022006-04-21 10:24:15 +00006295 new->name = NULL;
6296 new->sibling = NULL;
6297 new->child = NULL;
6298 return new;
6299}
6300
6301/**
Linus Walleij5573def2007-04-23 07:04:18 +00006302 * This recursively deletes the memory for a folder structure.
6303 * This shall typically be called on a top-level folder list to
6304 * detsroy the entire folder tree.
Linus Walleij9c6ca022006-04-21 10:24:15 +00006305 *
6306 * @param folder folder structure to destroy
6307 * @see LIBMTP_new_folder_t()
6308 */
6309void LIBMTP_destroy_folder_t(LIBMTP_folder_t *folder)
6310{
6311
6312 if(folder == NULL) {
6313 return;
6314 }
6315
6316 //Destroy from the bottom up
6317 if(folder->child != NULL) {
6318 LIBMTP_destroy_folder_t(folder->child);
6319 }
6320
6321 if(folder->sibling != NULL) {
6322 LIBMTP_destroy_folder_t(folder->sibling);
6323 }
6324
6325 if(folder->name != NULL) {
6326 free(folder->name);
6327 }
6328
6329 free(folder);
6330}
6331
6332/**
6333 * Helper function. Returns a folder structure for a
6334 * specified id.
6335 *
6336 * @param folderlist list of folders to search
6337 * @id id of folder to look for
6338 * @return a folder or NULL if not found
6339 */
6340LIBMTP_folder_t *LIBMTP_Find_Folder(LIBMTP_folder_t *folderlist, uint32_t id)
6341{
6342 LIBMTP_folder_t *ret = NULL;
mopoke96143402006-10-30 04:37:26 +00006343
Linus Walleij9c6ca022006-04-21 10:24:15 +00006344 if(folderlist == NULL) {
6345 return NULL;
6346 }
mopoke96143402006-10-30 04:37:26 +00006347
Linus Walleij9c6ca022006-04-21 10:24:15 +00006348 if(folderlist->folder_id == id) {
6349 return folderlist;
6350 }
mopoke96143402006-10-30 04:37:26 +00006351
Linus Walleij9c6ca022006-04-21 10:24:15 +00006352 if(folderlist->sibling) {
6353 ret = LIBMTP_Find_Folder(folderlist->sibling, id);
6354 }
mopoke96143402006-10-30 04:37:26 +00006355
Linus Walleij9c6ca022006-04-21 10:24:15 +00006356 if(folderlist->child && ret == NULL) {
6357 ret = LIBMTP_Find_Folder(folderlist->child, id);
6358 }
mopoke96143402006-10-30 04:37:26 +00006359
Linus Walleij9c6ca022006-04-21 10:24:15 +00006360 return ret;
6361}
6362
6363/**
Linus Walleij4d0deea2007-10-19 21:27:48 +00006364 * Function used to recursively get subfolders from params.
Linus Walleij9c6ca022006-04-21 10:24:15 +00006365 */
Linus Walleija89a7942008-12-14 23:19:34 +00006366static LIBMTP_folder_t *get_subfolders_for_folder(LIBMTP_folder_t *list, uint32_t parent)
Linus Walleij9c6ca022006-04-21 10:24:15 +00006367{
Linus Walleija89a7942008-12-14 23:19:34 +00006368 LIBMTP_folder_t *retfolders = NULL, *children, *iter, *curr;
mopoke96143402006-10-30 04:37:26 +00006369
Linus Walleija89a7942008-12-14 23:19:34 +00006370 iter = list->sibling;
6371 while(iter != list) {
6372 if (iter->parent_id != parent) {
6373 iter = iter->sibling;
6374 continue;
6375 }
6376
6377 /* We know that iter is a child of 'parent', therefore we can safely
6378 * hold on to 'iter' locally since no one else will steal it
6379 * from the 'list' as we recurse. */
6380 children = get_subfolders_for_folder(list, iter->folder_id);
6381
6382 curr = iter;
6383 iter = iter->sibling;
6384
6385 // Remove curr from the list.
6386 curr->child->sibling = curr->sibling;
6387 curr->sibling->child = curr->child;
6388
6389 // Attach the children to curr.
6390 curr->child = children;
6391
6392 // Put this folder into the list of siblings.
6393 curr->sibling = retfolders;
6394 retfolders = curr;
6395 }
6396
6397 return retfolders;
6398}
6399
6400/**
6401 * This returns a list of all folders available
6402 * on the current MTP device.
6403 *
6404 * @param device a pointer to the device to get the folder listing for.
6405 * @return a list of folders
6406 */
6407LIBMTP_folder_t *LIBMTP_Get_Folder_List(LIBMTP_mtpdevice_t *device)
6408{
6409 PTPParams *params = (PTPParams *) device->params;
6410 LIBMTP_folder_t head, *rv;
6411 int i;
6412
6413 // Get all the handles if we haven't already done that
Linus Walleijd4637502009-06-14 23:03:33 +00006414 if (params->nrofobjects == 0) {
Linus Walleija89a7942008-12-14 23:19:34 +00006415 flush_handles(device);
6416 }
6417
Linus Walleij4ab47b22008-12-14 23:24:26 +00006418 /*
6419 * This creates a temporary list of the folders, this is in a
6420 * reverse order and uses the Folder pointers that are already
6421 * in the Folder structure. From this we can then build up the
6422 * folder hierarchy with only looking at this temporary list,
6423 * and removing the folders from this temporary list as we go.
6424 * This significantly reduces the number of operations that we
6425 * have to do in building the folder hierarchy. Also since the
6426 * temp list is in reverse order, when we prepend to the sibling
6427 * list things are in the same order as they were originally
6428 * in the handle list.
6429 */
Linus Walleija89a7942008-12-14 23:19:34 +00006430 head.sibling = &head;
6431 head.child = &head;
Linus Walleijd4637502009-06-14 23:03:33 +00006432 for (i = 0; i < params->nrofobjects; i++) {
Linus Walleij9c6ca022006-04-21 10:24:15 +00006433 LIBMTP_folder_t *folder;
Linus Walleijd4637502009-06-14 23:03:33 +00006434 PTPObject *ob;
Linus Walleija89a7942008-12-14 23:19:34 +00006435
Linus Walleijd4637502009-06-14 23:03:33 +00006436 ob = &params->objects[i];
6437 if (ob->oi.ObjectFormat != PTP_OFC_Association) {
Linus Walleijf0bf4372007-07-01 21:47:38 +00006438 continue;
6439 }
Linus Walleij4ab47b22008-12-14 23:24:26 +00006440 /*
6441 * Do we know how to handle these? They are part
6442 * of the MTP 1.0 specification paragraph 3.6.4.
6443 * For AssociationDesc 0x00000001U ptp_mtp_getobjectreferences()
6444 * should be called on these to get the contained objects, but
6445 * we basically don't care. Hopefully parent_id is maintained for all
6446 * children, because we rely on that instead.
6447 */
Linus Walleijd4637502009-06-14 23:03:33 +00006448 if (ob->oi.AssociationDesc != 0x00000000U) {
6449 printf("MTP extended association type 0x%08x encountered\n", ob->oi.AssociationDesc);
Linus Walleijd71d0b32008-09-22 08:21:03 +00006450 }
6451
Linus Walleij4d0deea2007-10-19 21:27:48 +00006452 // Create a folder struct...
Linus Walleijf0bf4372007-07-01 21:47:38 +00006453 folder = LIBMTP_new_folder_t();
Linus Walleij4d0deea2007-10-19 21:27:48 +00006454 if (folder == NULL) {
6455 // malloc failure or so.
6456 return NULL;
6457 }
Linus Walleijd4637502009-06-14 23:03:33 +00006458 folder->folder_id = ob->oid;
6459 folder->parent_id = ob->oi.ParentObject;
6460 folder->storage_id = ob->oi.StorageID;
6461 folder->name = (ob->oi.Filename) ? (char *)strdup(ob->oi.Filename) : NULL;
Linus Walleijf0bf4372007-07-01 21:47:38 +00006462
Linus Walleija89a7942008-12-14 23:19:34 +00006463 // pretend sibling says next, and child says prev.
6464 folder->sibling = head.sibling;
6465 folder->child = &head;
6466 head.sibling->child = folder;
6467 head.sibling = folder;
Linus Walleij9c6ca022006-04-21 10:24:15 +00006468 }
Linus Walleij4d0deea2007-10-19 21:27:48 +00006469
Linus Walleij4d0deea2007-10-19 21:27:48 +00006470 // We begin at the root folder and get them all recursively
Linus Walleija89a7942008-12-14 23:19:34 +00006471 rv = get_subfolders_for_folder(&head, 0x00000000);
6472
6473 // The temp list should be empty. Clean up any orphans just in case.
6474 while(head.sibling != &head) {
6475 LIBMTP_folder_t *curr = head.sibling;
6476
6477 printf("Orphan folder with ID: 0x%08x name: \"%s\" encountered.\n",
6478 curr->folder_id,
6479 curr->name);
6480 curr->sibling->child = curr->child;
6481 curr->child->sibling = curr->sibling;
6482 curr->child = NULL;
6483 curr->sibling = NULL;
6484 LIBMTP_destroy_folder_t(curr);
6485 }
6486
6487 return rv;
Linus Walleij4d0deea2007-10-19 21:27:48 +00006488}
6489
6490/**
Linus Walleijc86afbd2006-05-04 19:05:24 +00006491 * This create a folder on the current MTP device. The PTP name
6492 * for a folder is "association". The PTP/MTP devices does not
6493 * have an internal "folder" concept really, it contains a flat
6494 * list of all files and some file are "associations" that other
6495 * files and folders may refer to as its "parent".
Linus Walleij9c6ca022006-04-21 10:24:15 +00006496 *
Linus Walleijc86afbd2006-05-04 19:05:24 +00006497 * @param device a pointer to the device to create the folder on.
Richard Lowf8cd3d72009-05-04 16:38:33 +00006498 * @param name the name of the new folder. Note this can be modified
6499 * if the device does not support all the characters in the
6500 * name.
Linus Walleijc86afbd2006-05-04 19:05:24 +00006501 * @param parent_id id of parent folder to add the new folder to,
6502 * or 0 to put it in the root directory.
Linus Walleijea68f1f2008-06-22 21:54:44 +00006503 * @param storage_id id of the storage to add this new folder to.
6504 * notice that you cannot mismatch storage id and parent id:
6505 * they must both be on the same storage! Pass in 0 if you
6506 * want to create this folder on the default storage.
Linus Walleijc86afbd2006-05-04 19:05:24 +00006507 * @return id to new folder or 0 if an error occured
Linus Walleij9c6ca022006-04-21 10:24:15 +00006508 */
Richard Lowf8cd3d72009-05-04 16:38:33 +00006509uint32_t LIBMTP_Create_Folder(LIBMTP_mtpdevice_t *device, char *name,
Linus Walleijea68f1f2008-06-22 21:54:44 +00006510 uint32_t parent_id, uint32_t storage_id)
Linus Walleij9c6ca022006-04-21 10:24:15 +00006511{
6512 PTPParams *params = (PTPParams *) device->params;
Linus Walleijd3b78572007-08-24 21:28:24 +00006513 PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo;
Linus Walleij9c6ca022006-04-21 10:24:15 +00006514 uint32_t parenthandle = 0;
Linus Walleijea68f1f2008-06-22 21:54:44 +00006515 uint32_t store;
Linus Walleij9c6ca022006-04-21 10:24:15 +00006516 PTPObjectInfo new_folder;
Linus Walleij438bd7f2006-06-08 11:35:44 +00006517 uint16_t ret;
Linus Walleij9c6ca022006-04-21 10:24:15 +00006518 uint32_t new_id = 0;
6519
Linus Walleijea68f1f2008-06-22 21:54:44 +00006520 if (storage_id == 0) {
Linus Walleijd71d0b32008-09-22 08:21:03 +00006521 // I'm just guessing that a folder may require 512 bytes
6522 store = get_writeable_storageid(device, 512);
Linus Walleijea68f1f2008-06-22 21:54:44 +00006523 } else {
6524 store = storage_id;
6525 }
6526 parenthandle = parent_id;
6527
Richard Low59426042009-05-04 16:40:09 +00006528 memset(&new_folder, 0, sizeof(new_folder));
Richard Lowf8cd3d72009-05-04 16:38:33 +00006529 new_folder.Filename = name;
Linus Walleijfec4d562008-06-01 22:30:36 +00006530 if (FLAG_ONLY_7BIT_FILENAMES(ptp_usb)) {
Linus Walleijd3b78572007-08-24 21:28:24 +00006531 strip_7bit_from_utf8(new_folder.Filename);
6532 }
Linus Walleij9c6ca022006-04-21 10:24:15 +00006533 new_folder.ObjectCompressedSize = 1;
6534 new_folder.ObjectFormat = PTP_OFC_Association;
Linus Walleijdbbfecc2008-11-05 09:52:47 +00006535 new_folder.ProtectionStatus = PTP_PS_NoProtection;
Linus Walleij753a46a2008-11-05 09:10:52 +00006536 new_folder.AssociationType = PTP_AT_GenericFolder;
Linus Walleij9c6ca022006-04-21 10:24:15 +00006537 new_folder.ParentObject = parent_id;
Linus Walleijea68f1f2008-06-22 21:54:44 +00006538 new_folder.StorageID = store;
Linus Walleij9c6ca022006-04-21 10:24:15 +00006539
Linus Walleij9c6ca022006-04-21 10:24:15 +00006540 // Create the object
Linus Walleijf0bf4372007-07-01 21:47:38 +00006541 // FIXME: use send list here if available.
Linus Walleij9c6ca022006-04-21 10:24:15 +00006542 ret = ptp_sendobjectinfo(params, &store, &parenthandle, &new_id, &new_folder);
6543 if (ret != PTP_RC_OK) {
Linus Walleij070e9b42007-01-22 08:49:28 +00006544 add_ptp_error_to_errorstack(device, ret, "LIBMTP_Create_Folder: Could not send object info.");
Linus Walleij99310d42006-11-01 08:29:39 +00006545 if (ret == PTP_RC_AccessDenied) {
Linus Walleij070e9b42007-01-22 08:49:28 +00006546 add_ptp_error_to_errorstack(device, ret, "ACCESS DENIED.");
Linus Walleij99310d42006-11-01 08:29:39 +00006547 }
Linus Walleijc86afbd2006-05-04 19:05:24 +00006548 return 0;
Linus Walleij9c6ca022006-04-21 10:24:15 +00006549 }
Richard Lowf8cd3d72009-05-04 16:38:33 +00006550 // NOTE: don't destroy the new_folder objectinfo, because it is statically referencing
6551 // several strings.
tsaarnia3eb60a2007-07-06 17:41:30 +00006552
Linus Walleija6d0d482007-10-31 08:54:56 +00006553 add_object_to_cache(device, new_id);
tsaarnia3eb60a2007-07-06 17:41:30 +00006554
Linus Walleij9c6ca022006-04-21 10:24:15 +00006555 return new_id;
6556}
raveloxd9a28642006-05-26 23:42:22 +00006557
Linus Walleij438bd7f2006-06-08 11:35:44 +00006558/**
6559 * This creates a new playlist metadata structure and allocates memory
6560 * for it. Notice that if you add strings to this structure they
6561 * will be freed by the corresponding <code>LIBMTP_destroy_playlist_t</code>
mopoke96143402006-10-30 04:37:26 +00006562 * operation later, so be careful of using strdup() when assigning
Linus Walleij438bd7f2006-06-08 11:35:44 +00006563 * strings, e.g.:
6564 *
6565 * <pre>
6566 * LIBMTP_playlist_t *pl = LIBMTP_new_playlist_t();
6567 * pl->name = strdup(str);
6568 * ....
6569 * LIBMTP_destroy_playlist_t(pl);
6570 * </pre>
6571 *
6572 * @return a pointer to the newly allocated metadata structure.
6573 * @see LIBMTP_destroy_playlist_t()
6574 */
6575LIBMTP_playlist_t *LIBMTP_new_playlist_t(void)
6576{
6577 LIBMTP_playlist_t *new = (LIBMTP_playlist_t *) malloc(sizeof(LIBMTP_playlist_t));
6578 if (new == NULL) {
6579 return NULL;
6580 }
6581 new->playlist_id = 0;
Linus Walleij5ce59db2008-03-12 21:22:58 +00006582 new->parent_id = 0;
Linus Walleijea68f1f2008-06-22 21:54:44 +00006583 new->storage_id = 0;
Linus Walleij438bd7f2006-06-08 11:35:44 +00006584 new->name = NULL;
6585 new->tracks = NULL;
6586 new->no_tracks = 0;
6587 new->next = NULL;
6588 return new;
6589}
6590
6591/**
6592 * This destroys a playlist metadata structure and deallocates the memory
mopoke96143402006-10-30 04:37:26 +00006593 * used by it, including any strings. Never use a track metadata
Linus Walleij438bd7f2006-06-08 11:35:44 +00006594 * structure again after calling this function on it.
6595 * @param playlist the playlist metadata to destroy.
6596 * @see LIBMTP_new_playlist_t()
6597 */
6598void LIBMTP_destroy_playlist_t(LIBMTP_playlist_t *playlist)
6599{
6600 if (playlist == NULL) {
6601 return;
6602 }
6603 if (playlist->name != NULL)
6604 free(playlist->name);
6605 if (playlist->tracks != NULL)
6606 free(playlist->tracks);
6607 free(playlist);
6608 return;
6609}
6610
6611/**
6612 * This function returns a list of the playlists available on the
6613 * device. Typical usage:
6614 *
6615 * <pre>
6616 * </pre>
6617 *
6618 * @param device a pointer to the device to get the playlist listing from.
6619 * @return a playlist list on success, else NULL. If there are no playlists
6620 * on the device, NULL will be returned as well.
Linus Walleij2e4b5f92006-06-16 14:00:49 +00006621 * @see LIBMTP_Get_Playlist()
Linus Walleij438bd7f2006-06-08 11:35:44 +00006622 */
6623LIBMTP_playlist_t *LIBMTP_Get_Playlist_List(LIBMTP_mtpdevice_t *device)
6624{
Linus Walleijf3c44052008-08-16 21:14:56 +00006625 PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo;
6626 const int REQ_SPL = FLAG_PLAYLIST_SPL(ptp_usb);
Linus Walleij438bd7f2006-06-08 11:35:44 +00006627 PTPParams *params = (PTPParams *) device->params;
6628 LIBMTP_playlist_t *retlists = NULL;
6629 LIBMTP_playlist_t *curlist = NULL;
6630 uint32_t i;
6631
6632 // Get all the handles if we haven't already done that
Linus Walleijd4637502009-06-14 23:03:33 +00006633 if (params->nrofobjects == 0) {
Linus Walleij438bd7f2006-06-08 11:35:44 +00006634 flush_handles(device);
6635 }
6636
Linus Walleijd4637502009-06-14 23:03:33 +00006637 for (i = 0; i < params->nrofobjects; i++) {
Linus Walleij438bd7f2006-06-08 11:35:44 +00006638 LIBMTP_playlist_t *pl;
Linus Walleijd4637502009-06-14 23:03:33 +00006639 PTPObject *ob;
Linus Walleij438bd7f2006-06-08 11:35:44 +00006640 uint16_t ret;
mopoke96143402006-10-30 04:37:26 +00006641
Linus Walleijd4637502009-06-14 23:03:33 +00006642 ob = &params->objects[i];
mopoke96143402006-10-30 04:37:26 +00006643
Linus Walleijf0bf4372007-07-01 21:47:38 +00006644 // Ignore stuff that isn't playlists
Linus Walleijf3c44052008-08-16 21:14:56 +00006645
6646 // For Samsung players we must look for the .spl extension explicitly since
6647 // playlists are not stored as playlist objects.
Linus Walleijd4637502009-06-14 23:03:33 +00006648 if ( REQ_SPL && is_spl_playlist(&ob->oi) ) {
Linus Walleijf3c44052008-08-16 21:14:56 +00006649 // Allocate a new playlist type
6650 pl = LIBMTP_new_playlist_t();
Linus Walleijd4637502009-06-14 23:03:33 +00006651 spl_to_playlist_t(device, &ob->oi, ob->oid, pl);
Linus Walleijf3c44052008-08-16 21:14:56 +00006652 }
Linus Walleijd4637502009-06-14 23:03:33 +00006653 else if ( ob->oi.ObjectFormat != PTP_OFC_MTP_AbstractAudioVideoPlaylist ) {
Linus Walleijf0bf4372007-07-01 21:47:38 +00006654 continue;
Linus Walleij438bd7f2006-06-08 11:35:44 +00006655 }
Linus Walleijf3c44052008-08-16 21:14:56 +00006656 else {
6657 // Allocate a new playlist type
6658 pl = LIBMTP_new_playlist_t();
Linus Walleijf0bf4372007-07-01 21:47:38 +00006659
Linus Walleijf3296622008-09-04 20:53:56 +00006660 // Try to look up proper name, else use the oi->Filename field.
Linus Walleijd4637502009-06-14 23:03:33 +00006661 pl->name = get_string_from_object(device, ob->oid, PTP_OPC_Name);
Linus Walleijf3296622008-09-04 20:53:56 +00006662 if (pl->name == NULL) {
Linus Walleijd4637502009-06-14 23:03:33 +00006663 pl->name = strdup(ob->oi.Filename);
Linus Walleijf3296622008-09-04 20:53:56 +00006664 }
Linus Walleijd4637502009-06-14 23:03:33 +00006665 pl->playlist_id = ob->oid;
6666 pl->parent_id = ob->oi.ParentObject;
6667 pl->storage_id = ob->oi.StorageID;
Linus Walleijf0bf4372007-07-01 21:47:38 +00006668
Linus Walleijf3c44052008-08-16 21:14:56 +00006669 // Then get the track listing for this playlist
6670 ret = ptp_mtp_getobjectreferences(params, pl->playlist_id, &pl->tracks, &pl->no_tracks);
6671 if (ret != PTP_RC_OK) {
Linus Walleij8d8c4352008-09-23 23:04:16 +00006672 add_ptp_error_to_errorstack(device, ret, "LIBMTP_Get_Playlist_List(): "
6673 "could not get object references.");
Linus Walleijf3c44052008-08-16 21:14:56 +00006674 pl->tracks = NULL;
6675 pl->no_tracks = 0;
6676 }
Linus Walleijf0bf4372007-07-01 21:47:38 +00006677 }
6678
6679 // Add playlist to a list that will be returned afterwards.
6680 if (retlists == NULL) {
6681 retlists = pl;
6682 curlist = pl;
6683 } else {
6684 curlist->next = pl;
6685 curlist = pl;
6686 }
6687
6688 // Call callback here if we decide to add that possibility...
mopoke96143402006-10-30 04:37:26 +00006689 }
Linus Walleij438bd7f2006-06-08 11:35:44 +00006690 return retlists;
6691}
6692
Linus Walleij2e4b5f92006-06-16 14:00:49 +00006693
6694/**
6695 * This function retrieves an individual playlist from the device.
6696 * @param device a pointer to the device to get the playlist from.
6697 * @param plid the unique ID of the playlist to retrieve.
6698 * @return a valid playlist metadata post or NULL on failure.
6699 * @see LIBMTP_Get_Playlist_List()
6700 */
6701LIBMTP_playlist_t *LIBMTP_Get_Playlist(LIBMTP_mtpdevice_t *device, uint32_t const plid)
6702{
Linus Walleijf3c44052008-08-16 21:14:56 +00006703 PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo;
6704 const int REQ_SPL = FLAG_PLAYLIST_SPL(ptp_usb);
Linus Walleij2e4b5f92006-06-16 14:00:49 +00006705 PTPParams *params = (PTPParams *) device->params;
Linus Walleijd4637502009-06-14 23:03:33 +00006706 PTPObject *ob;
6707 LIBMTP_playlist_t *pl;
6708 uint16_t ret;
Linus Walleij2e4b5f92006-06-16 14:00:49 +00006709
6710 // Get all the handles if we haven't already done that
Linus Walleijd4637502009-06-14 23:03:33 +00006711 if (params->nrofobjects == 0) {
Linus Walleij2e4b5f92006-06-16 14:00:49 +00006712 flush_handles(device);
6713 }
6714
Linus Walleijd4637502009-06-14 23:03:33 +00006715 ret = ptp_object_want (params, plid, PTPOBJECT_OBJECTINFO_LOADED, &ob);
6716 if (ret != PTP_RC_OK)
6717 return NULL;
Linus Walleij2e4b5f92006-06-16 14:00:49 +00006718
Linus Walleijd4637502009-06-14 23:03:33 +00006719 // For Samsung players we must look for the .spl extension explicitly since
6720 // playlists are not stored as playlist objects.
6721 if ( REQ_SPL && is_spl_playlist(&ob->oi) ) {
Linus Walleijf0bf4372007-07-01 21:47:38 +00006722 // Allocate a new playlist type
6723 pl = LIBMTP_new_playlist_t();
Linus Walleijd4637502009-06-14 23:03:33 +00006724 spl_to_playlist_t(device, &ob->oi, ob->oid, pl);
Linus Walleijf0bf4372007-07-01 21:47:38 +00006725 return pl;
mopoke96143402006-10-30 04:37:26 +00006726 }
Linus Walleijd4637502009-06-14 23:03:33 +00006727
6728 // Ignore stuff that isn't playlists
6729 else if ( ob->oi.ObjectFormat != PTP_OFC_MTP_AbstractAudioVideoPlaylist ) {
6730 return NULL;
6731 }
6732
6733 // Allocate a new playlist type
6734 pl = LIBMTP_new_playlist_t();
6735
6736 pl->name = get_string_from_object(device, ob->oid, PTP_OPC_Name);
6737 if (pl->name == NULL) {
6738 pl->name = strdup(ob->oi.Filename);
6739 }
6740 pl->playlist_id = ob->oid;
6741 pl->parent_id = ob->oi.ParentObject;
6742 pl->storage_id = ob->oi.StorageID;
6743
6744 // Then get the track listing for this playlist
6745 ret = ptp_mtp_getobjectreferences(params, pl->playlist_id, &pl->tracks, &pl->no_tracks);
6746 if (ret != PTP_RC_OK) {
6747 add_ptp_error_to_errorstack(device, ret, "LIBMTP_Get_Playlist(): Could not get object references.");
6748 pl->tracks = NULL;
6749 pl->no_tracks = 0;
6750 }
6751
6752 return pl;
Linus Walleij2e4b5f92006-06-16 14:00:49 +00006753}
6754
Linus Walleij31b74292008-05-02 23:29:06 +00006755/**
Linus Walleijb7426d12006-11-25 23:19:11 +00006756 * This function creates a new abstract list such as a playlist
6757 * or an album.
6758 *
6759 * @param device a pointer to the device to create the new abstract list
6760 * on.
6761 * @param name the name of the new abstract list.
Linus Walleij7783b9b2007-09-22 22:10:44 +00006762 * @param artist the artist of the new abstract list or NULL.
6763 * @param genre the genre of the new abstract list or NULL.
Linus Walleijb7426d12006-11-25 23:19:11 +00006764 * @param parenthandle the handle of the parent or 0 for no parent
6765 * i.e. the root folder.
6766 * @param objectformat the abstract list type to create.
6767 * @param suffix the ".foo" (4 characters) suffix to use for the virtual
6768 * "file" created by this operation.
6769 * @param newid a pointer to a variable that will hold the new object
6770 * ID if this call is successful.
6771 * @param tracks an array of tracks to associate with this list.
6772 * @param no_tracks the number of tracks in the list.
Linus Walleij438bd7f2006-06-08 11:35:44 +00006773 * @return 0 on success, any other value means failure.
Linus Walleij438bd7f2006-06-08 11:35:44 +00006774 */
Linus Walleijb7426d12006-11-25 23:19:11 +00006775static int create_new_abstract_list(LIBMTP_mtpdevice_t *device,
6776 char const * const name,
Linus Walleijadce4a52007-04-23 07:28:11 +00006777 char const * const artist,
Linus Walleij31b74292008-05-02 23:29:06 +00006778 char const * const composer,
Linus Walleijadce4a52007-04-23 07:28:11 +00006779 char const * const genre,
Linus Walleijb7426d12006-11-25 23:19:11 +00006780 uint32_t const parenthandle,
Linus Walleijea68f1f2008-06-22 21:54:44 +00006781 uint32_t const storageid,
Linus Walleijb7426d12006-11-25 23:19:11 +00006782 uint16_t const objectformat,
6783 char const * const suffix,
6784 uint32_t * const newid,
6785 uint32_t const * const tracks,
6786 uint32_t const no_tracks)
6787
Linus Walleij438bd7f2006-06-08 11:35:44 +00006788{
Linus Walleijb7426d12006-11-25 23:19:11 +00006789 int i;
6790 int supported = 0;
Linus Walleij438bd7f2006-06-08 11:35:44 +00006791 uint16_t ret;
Linus Walleij1e9a0332007-09-12 19:35:56 +00006792 uint16_t *properties = NULL;
rreardon508705f2006-11-19 21:27:22 +00006793 uint32_t propcnt = 0;
Linus Walleijea68f1f2008-06-22 21:54:44 +00006794 uint32_t store;
Linus Walleij438bd7f2006-06-08 11:35:44 +00006795 uint32_t localph = parenthandle;
Linus Walleijb7426d12006-11-25 23:19:11 +00006796 uint8_t nonconsumable = 0x00U; /* By default it is consumable */
6797 PTPParams *params = (PTPParams *) device->params;
Linus Walleijd3b78572007-08-24 21:28:24 +00006798 PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo;
Linus Walleij438bd7f2006-06-08 11:35:44 +00006799 char fname[256];
Linus Walleijd14e84f2006-06-16 14:50:59 +00006800 uint8_t data[2];
Linus Walleij438bd7f2006-06-08 11:35:44 +00006801
Linus Walleijea68f1f2008-06-22 21:54:44 +00006802 if (storageid == 0) {
Linus Walleijd71d0b32008-09-22 08:21:03 +00006803 // I'm just guessing that an abstract list may require 512 bytes
6804 store = get_writeable_storageid(device, 512);
Linus Walleijea68f1f2008-06-22 21:54:44 +00006805 } else {
6806 store = storageid;
6807 }
6808
Linus Walleijb7426d12006-11-25 23:19:11 +00006809 // Check if we can create an object of this type
6810 for ( i=0; i < params->deviceinfo.ImageFormats_len; i++ ) {
6811 if (params->deviceinfo.ImageFormats[i] == objectformat) {
6812 supported = 1;
6813 break;
Linus Walleij438bd7f2006-06-08 11:35:44 +00006814 }
6815 }
Linus Walleijb7426d12006-11-25 23:19:11 +00006816 if (!supported) {
Linus Walleij070e9b42007-01-22 08:49:28 +00006817 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "create_new_abstract_list(): player does not support this abstract type.");
Linus Walleijf3296622008-09-04 20:53:56 +00006818 printf("Unsupported abstract list type: %04x\n", objectformat);
Linus Walleijb7426d12006-11-25 23:19:11 +00006819 return -1;
6820 }
6821
Richard Low15731fe2007-03-22 20:27:20 +00006822 // add the new suffix if it isn't there
Linus Walleij629fe402006-12-12 23:39:15 +00006823 fname[0] = '\0';
Linus Walleijb7426d12006-11-25 23:19:11 +00006824 if (strlen(name) > strlen(suffix)) {
6825 char const * const suff = &name[strlen(name)-strlen(suffix)];
6826 if (!strcmp(suff, suffix)) {
6827 // Home free.
Linus Walleij629fe402006-12-12 23:39:15 +00006828 strncpy(fname, name, sizeof(fname));
Linus Walleijb7426d12006-11-25 23:19:11 +00006829 }
6830 }
6831 // If it didn't end with "<suffix>" then add that here.
Linus Walleij629fe402006-12-12 23:39:15 +00006832 if (fname[0] == '\0') {
Linus Walleijb7426d12006-11-25 23:19:11 +00006833 strncpy(fname, name, sizeof(fname)-strlen(suffix)-1);
6834 strcat(fname, suffix);
Linus Walleij438bd7f2006-06-08 11:35:44 +00006835 fname[sizeof(fname)-1] = '\0';
Linus Walleij438bd7f2006-06-08 11:35:44 +00006836 }
6837
Linus Walleijf67c1ad2009-01-14 21:39:50 +00006838 if (ptp_operation_issupported(params, PTP_OC_MTP_SendObjectPropList) &&
6839 !FLAG_BROKEN_SEND_OBJECT_PROPLIST(ptp_usb)) {
Linus Walleij1e9a0332007-09-12 19:35:56 +00006840 MTPProperties *props = NULL;
6841 MTPProperties *prop = NULL;
6842 int nrofprops = 0;
Richard Low6c0a6ce2006-11-26 10:42:08 +00006843
6844 *newid = 0x00000000U;
mopoke96143402006-10-30 04:37:26 +00006845
Linus Walleij1e9a0332007-09-12 19:35:56 +00006846 ret = ptp_mtp_getobjectpropssupported(params, objectformat, &propcnt, &properties);
mopoke31364442006-11-20 04:53:04 +00006847
rreardon508705f2006-11-19 21:27:22 +00006848 for (i=0;i<propcnt;i++) {
Linus Walleijdbcc8242007-08-05 22:31:26 +00006849 PTPObjectPropDesc opd;
6850
Linus Walleij1e9a0332007-09-12 19:35:56 +00006851 ret = ptp_mtp_getobjectpropdesc(params, properties[i], objectformat, &opd);
Linus Walleijdbcc8242007-08-05 22:31:26 +00006852 if (ret != PTP_RC_OK) {
6853 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "create_new_abstract_list(): "
6854 "could not get property description.");
6855 } else if (opd.GetSet) {
Linus Walleij1e9a0332007-09-12 19:35:56 +00006856 switch (properties[i]) {
Linus Walleijdbcc8242007-08-05 22:31:26 +00006857 case PTP_OPC_ObjectFileName:
Linus Walleija6d0d482007-10-31 08:54:56 +00006858 prop = ptp_get_new_object_prop_entry(&props,&nrofprops);
Linus Walleijdbcc8242007-08-05 22:31:26 +00006859 prop->ObjectHandle = *newid;
6860 prop->property = PTP_OPC_ObjectFileName;
6861 prop->datatype = PTP_DTC_STR;
6862 prop->propval.str = strdup(fname);
Linus Walleijfec4d562008-06-01 22:30:36 +00006863 if (FLAG_ONLY_7BIT_FILENAMES(ptp_usb)) {
Linus Walleijd3b78572007-08-24 21:28:24 +00006864 strip_7bit_from_utf8(prop->propval.str);
6865 }
Linus Walleijdbcc8242007-08-05 22:31:26 +00006866 break;
6867 case PTP_OPC_ProtectionStatus:
Linus Walleija6d0d482007-10-31 08:54:56 +00006868 prop = ptp_get_new_object_prop_entry(&props,&nrofprops);
Linus Walleijadce4a52007-04-23 07:28:11 +00006869 prop->ObjectHandle = *newid;
Linus Walleijdbcc8242007-08-05 22:31:26 +00006870 prop->property = PTP_OPC_ProtectionStatus;
6871 prop->datatype = PTP_DTC_UINT16;
6872 prop->propval.u16 = 0x0000U; /* Not protected */
Linus Walleijdbcc8242007-08-05 22:31:26 +00006873 break;
6874 case PTP_OPC_NonConsumable:
Linus Walleija6d0d482007-10-31 08:54:56 +00006875 prop = ptp_get_new_object_prop_entry(&props,&nrofprops);
Linus Walleijadce4a52007-04-23 07:28:11 +00006876 prop->ObjectHandle = *newid;
Linus Walleijdbcc8242007-08-05 22:31:26 +00006877 prop->property = PTP_OPC_NonConsumable;
6878 prop->datatype = PTP_DTC_UINT8;
6879 prop->propval.u8 = nonconsumable;
Linus Walleijdbcc8242007-08-05 22:31:26 +00006880 break;
6881 case PTP_OPC_Name:
6882 if (name != NULL) {
Linus Walleija6d0d482007-10-31 08:54:56 +00006883 prop = ptp_get_new_object_prop_entry(&props,&nrofprops);
Linus Walleijdbcc8242007-08-05 22:31:26 +00006884 prop->ObjectHandle = *newid;
6885 prop->property = PTP_OPC_Name;
6886 prop->datatype = PTP_DTC_STR;
6887 prop->propval.str = strdup(name);
Linus Walleijdbcc8242007-08-05 22:31:26 +00006888 }
6889 break;
Linus Walleij0b75ab62007-12-28 00:47:34 +00006890 case PTP_OPC_AlbumArtist:
6891 if (artist != NULL) {
6892 prop = ptp_get_new_object_prop_entry(&props,&nrofprops);
6893 prop->ObjectHandle = *newid;
6894 prop->property = PTP_OPC_AlbumArtist;
6895 prop->datatype = PTP_DTC_STR;
6896 prop->propval.str = strdup(artist);
6897 }
6898 break;
Linus Walleijdbcc8242007-08-05 22:31:26 +00006899 case PTP_OPC_Artist:
6900 if (artist != NULL) {
Linus Walleija6d0d482007-10-31 08:54:56 +00006901 prop = ptp_get_new_object_prop_entry(&props,&nrofprops);
Linus Walleijdbcc8242007-08-05 22:31:26 +00006902 prop->ObjectHandle = *newid;
6903 prop->property = PTP_OPC_Artist;
6904 prop->datatype = PTP_DTC_STR;
6905 prop->propval.str = strdup(artist);
Linus Walleijdbcc8242007-08-05 22:31:26 +00006906 }
6907 break;
Linus Walleij31b74292008-05-02 23:29:06 +00006908 case PTP_OPC_Composer:
6909 if (composer != NULL) {
6910 prop = ptp_get_new_object_prop_entry(&props,&nrofprops);
6911 prop->ObjectHandle = *newid;
6912 prop->property = PTP_OPC_Composer;
6913 prop->datatype = PTP_DTC_STR;
6914 prop->propval.str = strdup(composer);
6915 }
6916 break;
Linus Walleijdbcc8242007-08-05 22:31:26 +00006917 case PTP_OPC_Genre:
6918 if (genre != NULL) {
Linus Walleija6d0d482007-10-31 08:54:56 +00006919 prop = ptp_get_new_object_prop_entry(&props,&nrofprops);
Linus Walleijdbcc8242007-08-05 22:31:26 +00006920 prop->ObjectHandle = *newid;
6921 prop->property = PTP_OPC_Genre;
6922 prop->datatype = PTP_DTC_STR;
6923 prop->propval.str = strdup(genre);
Linus Walleijdbcc8242007-08-05 22:31:26 +00006924 }
6925 break;
Linus Walleij7783b9b2007-09-22 22:10:44 +00006926 case PTP_OPC_DateModified:
6927 // Tag with current time if that is supported
Linus Walleijcf8dc2b2008-10-21 13:58:36 +00006928 if (!FLAG_CANNOT_HANDLE_DATEMODIFIED(ptp_usb)) {
6929 prop = ptp_get_new_object_prop_entry(&props,&nrofprops);
6930 prop->ObjectHandle = *newid;
6931 prop->property = PTP_OPC_DateModified;
6932 prop->datatype = PTP_DTC_STR;
6933 prop->propval.str = get_iso8601_stamp();
6934 }
Linus Walleij7783b9b2007-09-22 22:10:44 +00006935 break;
Linus Walleijadce4a52007-04-23 07:28:11 +00006936 }
rreardon508705f2006-11-19 21:27:22 +00006937 }
Linus Walleijdbcc8242007-08-05 22:31:26 +00006938 ptp_free_objectpropdesc(&opd);
rreardon508705f2006-11-19 21:27:22 +00006939 }
Linus Walleij1e9a0332007-09-12 19:35:56 +00006940 free(properties);
mopoke31364442006-11-20 04:53:04 +00006941
Linus Walleijb7426d12006-11-25 23:19:11 +00006942 ret = ptp_mtp_sendobjectproplist(params, &store, &localph, newid,
Linus Walleij1e9a0332007-09-12 19:35:56 +00006943 objectformat, 0, props, nrofprops);
mopoke31364442006-11-20 04:53:04 +00006944
rreardon508705f2006-11-19 21:27:22 +00006945 /* Free property list */
Linus Walleija6d0d482007-10-31 08:54:56 +00006946 ptp_destroy_object_prop_list(props, nrofprops);
mopoke31364442006-11-20 04:53:04 +00006947
rreardon508705f2006-11-19 21:27:22 +00006948 if (ret != PTP_RC_OK) {
Linus Walleij070e9b42007-01-22 08:49:28 +00006949 add_ptp_error_to_errorstack(device, ret, "create_new_abstract_list(): Could not send object property list.");
rreardon508705f2006-11-19 21:27:22 +00006950 if (ret == PTP_RC_AccessDenied) {
Linus Walleij070e9b42007-01-22 08:49:28 +00006951 add_ptp_error_to_errorstack(device, ret, "ACCESS DENIED.");
rreardon508705f2006-11-19 21:27:22 +00006952 }
6953 return -1;
6954 }
Richard Lowa6c924c2006-12-29 17:44:28 +00006955
Richard Lowc3f5fc32007-07-29 09:40:50 +00006956 // now send the blank object
Richard Lowa6c924c2006-12-29 17:44:28 +00006957 ret = ptp_sendobject(params, NULL, 0);
6958 if (ret != PTP_RC_OK) {
Linus Walleij070e9b42007-01-22 08:49:28 +00006959 add_ptp_error_to_errorstack(device, ret, "create_new_abstract_list(): Could not send blank object data.");
Richard Lowa6c924c2006-12-29 17:44:28 +00006960 return -1;
6961 }
Linus Walleij629fe402006-12-12 23:39:15 +00006962
rreardon508705f2006-11-19 21:27:22 +00006963 } else if (ptp_operation_issupported(params,PTP_OC_SendObjectInfo)) {
Linus Walleij629fe402006-12-12 23:39:15 +00006964 PTPObjectInfo new_object;
6965
6966 new_object.Filename = fname;
Linus Walleijfec4d562008-06-01 22:30:36 +00006967 if (FLAG_ONLY_7BIT_FILENAMES(ptp_usb)) {
Linus Walleijd3b78572007-08-24 21:28:24 +00006968 strip_7bit_from_utf8(new_object.Filename);
6969 }
Linus Walleij629fe402006-12-12 23:39:15 +00006970 new_object.ObjectCompressedSize = 1;
6971 new_object.ObjectFormat = objectformat;
6972
Linus Walleij20698482006-11-24 20:54:21 +00006973 // Create the object
Linus Walleijb7426d12006-11-25 23:19:11 +00006974 ret = ptp_sendobjectinfo(params, &store, &localph, newid, &new_object);
Linus Walleij20698482006-11-24 20:54:21 +00006975 if (ret != PTP_RC_OK) {
Linus Walleij070e9b42007-01-22 08:49:28 +00006976 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 +00006977 if (ret == PTP_RC_AccessDenied) {
Linus Walleij070e9b42007-01-22 08:49:28 +00006978 add_ptp_error_to_errorstack(device, ret, "ACCESS DENIED.");
Linus Walleij20698482006-11-24 20:54:21 +00006979 }
6980 return -1;
6981 }
Linus Walleijf0bf4372007-07-01 21:47:38 +00006982 // NOTE: don't destroy new_object objectinfo afterwards - the strings it contains are
6983 // not copies.
Richard Lowa6c924c2006-12-29 17:44:28 +00006984
6985 /*
6986 * We have to send this one blank data byte.
6987 * If we don't, the handle will not be created and thus there is no playlist.
6988 */
6989 data[0] = '\0';
6990 data[1] = '\0';
6991 ret = ptp_sendobject(params, data, 1);
6992 if (ret != PTP_RC_OK) {
Linus Walleij070e9b42007-01-22 08:49:28 +00006993 add_ptp_error_to_errorstack(device, ret, "create_new_abstract_list(): Could not send blank object data.");
Richard Lowa6c924c2006-12-29 17:44:28 +00006994 return -1;
6995 }
Richard Lowff16bfa2008-09-26 19:23:08 +00006996
6997 // set the properties one by one
6998 ret = ptp_mtp_getobjectpropssupported(params, objectformat, &propcnt, &properties);
6999
7000 for (i=0;i<propcnt;i++) {
7001 PTPObjectPropDesc opd;
7002
7003 ret = ptp_mtp_getobjectpropdesc(params, properties[i], objectformat, &opd);
7004 if (ret != PTP_RC_OK) {
7005 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "create_new_abstract_list(): "
7006 "could not get property description.");
7007 } else if (opd.GetSet) {
7008 switch (properties[i]) {
7009 case PTP_OPC_Name:
7010 if (name != NULL) {
Linus Walleij37588142008-10-16 19:10:47 +00007011 ret = set_object_string(device, *newid, PTP_OPC_Name, name);
7012 if (ret != 0) {
7013 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "create_new_abstract_list(): could not set entity name.");
7014 return -1;
7015 }
7016 }
Richard Lowff16bfa2008-09-26 19:23:08 +00007017 break;
7018 case PTP_OPC_AlbumArtist:
7019 if (artist != NULL) {
Linus Walleij37588142008-10-16 19:10:47 +00007020 ret = set_object_string(device, *newid, PTP_OPC_AlbumArtist, artist);
7021 if (ret != 0) {
7022 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "create_new_abstract_list(): could not set entity album artist.");
7023 return -1;
7024 }
7025 }
Richard Lowff16bfa2008-09-26 19:23:08 +00007026 break;
7027 case PTP_OPC_Artist:
7028 if (artist != NULL) {
Linus Walleij37588142008-10-16 19:10:47 +00007029 ret = set_object_string(device, *newid, PTP_OPC_Artist, artist);
7030 if (ret != 0) {
7031 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "create_new_abstract_list(): could not set entity artist.");
7032 return -1;
7033 }
7034 }
Richard Lowff16bfa2008-09-26 19:23:08 +00007035 break;
7036 case PTP_OPC_Composer:
7037 if (composer != NULL) {
Linus Walleij37588142008-10-16 19:10:47 +00007038 ret = set_object_string(device, *newid, PTP_OPC_Composer, composer);
7039 if (ret != 0) {
7040 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "create_new_abstract_list(): could not set entity composer.");
7041 return -1;
7042 }
7043 }
Richard Lowff16bfa2008-09-26 19:23:08 +00007044 break;
7045 case PTP_OPC_Genre:
7046 if (genre != NULL) {
Linus Walleij37588142008-10-16 19:10:47 +00007047 ret = set_object_string(device, *newid, PTP_OPC_Genre, genre);
7048 if (ret != 0) {
7049 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "create_new_abstract_list(): could not set entity genre.");
7050 return -1;
7051 }
7052 }
Richard Lowff16bfa2008-09-26 19:23:08 +00007053 break;
7054 case PTP_OPC_DateModified:
Linus Walleijcf8dc2b2008-10-21 13:58:36 +00007055 if (!FLAG_CANNOT_HANDLE_DATEMODIFIED(ptp_usb)) {
7056 ret = set_object_string(device, *newid, PTP_OPC_DateModified, get_iso8601_stamp());
7057 if (ret != 0) {
7058 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "create_new_abstract_list(): could not set date modified.");
7059 return -1;
7060 }
Linus Walleij37588142008-10-16 19:10:47 +00007061 }
7062 break;
Richard Lowff16bfa2008-09-26 19:23:08 +00007063 }
7064 }
7065 ptp_free_objectpropdesc(&opd);
7066 }
7067 free(properties);
Linus Walleij438bd7f2006-06-08 11:35:44 +00007068 }
7069
Linus Walleijb7426d12006-11-25 23:19:11 +00007070 if (no_tracks > 0) {
Linus Walleij1ccd5862008-11-11 12:33:01 +00007071 // Add tracks to the list as object references.
Linus Walleijb7426d12006-11-25 23:19:11 +00007072 ret = ptp_mtp_setobjectreferences (params, *newid, (uint32_t *) tracks, no_tracks);
Linus Walleij438bd7f2006-06-08 11:35:44 +00007073 if (ret != PTP_RC_OK) {
Linus Walleij070e9b42007-01-22 08:49:28 +00007074 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 +00007075 return -1;
7076 }
7077 }
mopoke96143402006-10-30 04:37:26 +00007078
Linus Walleija6d0d482007-10-31 08:54:56 +00007079 add_object_to_cache(device, *newid);
Linus Walleij438bd7f2006-06-08 11:35:44 +00007080
7081 return 0;
7082}
7083
Linus Walleij7783b9b2007-09-22 22:10:44 +00007084/**
7085 * This updates the metadata and track listing
7086 * for an abstract list.
7087 * @param device a pointer to the device that the abstract list
7088 * resides on.
7089 * @param name the name of the abstract list.
7090 * @param artist the artist of the abstract list or NULL.
7091 * @param genre the genre of the abstract list or NULL.
7092 * @param objecthandle the object to be updated.
7093 * @param objectformat the abstract list type to update.
7094 * @param tracks an array of tracks to associate with this list.
7095 * @param no_tracks the number of tracks in the list.
7096 * @return 0 on success, any other value means failure.
7097 */
7098static int update_abstract_list(LIBMTP_mtpdevice_t *device,
7099 char const * const name,
7100 char const * const artist,
Linus Walleij31b74292008-05-02 23:29:06 +00007101 char const * const composer,
Linus Walleij7783b9b2007-09-22 22:10:44 +00007102 char const * const genre,
7103 uint32_t const objecthandle,
7104 uint16_t const objectformat,
7105 uint32_t const * const tracks,
7106 uint32_t const no_tracks)
7107{
7108 uint16_t ret;
7109 PTPParams *params = (PTPParams *) device->params;
Linus Walleijf9267e92007-10-15 21:07:37 +00007110 PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo;
Linus Walleij7783b9b2007-09-22 22:10:44 +00007111 uint16_t *properties = NULL;
7112 uint32_t propcnt = 0;
7113 int i;
7114
7115 // First see which properties can be set
7116 // i.e only try to update this metadata for object tags that exist on the current player.
7117 ret = ptp_mtp_getobjectpropssupported(params, objectformat, &propcnt, &properties);
7118 if (ret != PTP_RC_OK) {
7119 // Just bail out for now, nothing is ever set.
7120 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "update_abstract_list(): "
7121 "could not retrieve supported object properties.");
7122 return -1;
7123 }
Linus Walleijf9267e92007-10-15 21:07:37 +00007124 if (ptp_operation_issupported(params,PTP_OC_MTP_SetObjPropList) &&
Linus Walleijfec4d562008-06-01 22:30:36 +00007125 !FLAG_BROKEN_SET_OBJECT_PROPLIST(ptp_usb)) {
Linus Walleij7783b9b2007-09-22 22:10:44 +00007126 MTPProperties *props = NULL;
7127 MTPProperties *prop = NULL;
7128 int nrofprops = 0;
7129
7130 for (i=0;i<propcnt;i++) {
7131 PTPObjectPropDesc opd;
7132
7133 ret = ptp_mtp_getobjectpropdesc(params, properties[i], objectformat, &opd);
7134 if (ret != PTP_RC_OK) {
7135 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "update_abstract_list(): "
7136 "could not get property description.");
7137 } else if (opd.GetSet) {
7138 switch (properties[i]) {
7139 case PTP_OPC_Name:
Linus Walleija6d0d482007-10-31 08:54:56 +00007140 prop = ptp_get_new_object_prop_entry(&props, &nrofprops);
Linus Walleij7783b9b2007-09-22 22:10:44 +00007141 prop->ObjectHandle = objecthandle;
7142 prop->property = PTP_OPC_Name;
7143 prop->datatype = PTP_DTC_STR;
7144 if (name != NULL)
7145 prop->propval.str = strdup(name);
7146 break;
Linus Walleij0b75ab62007-12-28 00:47:34 +00007147 case PTP_OPC_AlbumArtist:
7148 if (artist != NULL) {
7149 prop = ptp_get_new_object_prop_entry(&props, &nrofprops);
7150 prop->ObjectHandle = objecthandle;
7151 prop->property = PTP_OPC_AlbumArtist;
7152 prop->datatype = PTP_DTC_STR;
7153 prop->propval.str = strdup(artist);
7154 }
7155 break;
Linus Walleij7783b9b2007-09-22 22:10:44 +00007156 case PTP_OPC_Artist:
7157 if (artist != NULL) {
Linus Walleija6d0d482007-10-31 08:54:56 +00007158 prop = ptp_get_new_object_prop_entry(&props, &nrofprops);
Linus Walleij0b75ab62007-12-28 00:47:34 +00007159 prop->ObjectHandle = objecthandle;
Linus Walleij7783b9b2007-09-22 22:10:44 +00007160 prop->property = PTP_OPC_Artist;
7161 prop->datatype = PTP_DTC_STR;
7162 prop->propval.str = strdup(artist);
7163 }
7164 break;
Linus Walleij31b74292008-05-02 23:29:06 +00007165 case PTP_OPC_Composer:
7166 if (composer != NULL) {
7167 prop = ptp_get_new_object_prop_entry(&props, &nrofprops);
7168 prop->ObjectHandle = objecthandle;
7169 prop->property = PTP_OPC_Composer;
7170 prop->datatype = PTP_DTC_STR;
7171 prop->propval.str = strdup(composer);
7172 }
7173 break;
Linus Walleij7783b9b2007-09-22 22:10:44 +00007174 case PTP_OPC_Genre:
7175 if (genre != NULL) {
Linus Walleija6d0d482007-10-31 08:54:56 +00007176 prop = ptp_get_new_object_prop_entry(&props, &nrofprops);
Linus Walleij7783b9b2007-09-22 22:10:44 +00007177 prop->ObjectHandle = objecthandle;
7178 prop->property = PTP_OPC_Genre;
7179 prop->datatype = PTP_DTC_STR;
7180 prop->propval.str = strdup(genre);
7181 }
7182 break;
7183 case PTP_OPC_DateModified:
Linus Walleijcf8dc2b2008-10-21 13:58:36 +00007184 if (!FLAG_CANNOT_HANDLE_DATEMODIFIED(ptp_usb)) {
Linus Walleij37588142008-10-16 19:10:47 +00007185 // Tag with current time if that is supported
7186 prop = ptp_get_new_object_prop_entry(&props, &nrofprops);
7187 prop->ObjectHandle = objecthandle;
7188 prop->property = PTP_OPC_DateModified;
7189 prop->datatype = PTP_DTC_STR;
7190 prop->propval.str = get_iso8601_stamp();
7191 }
Linus Walleij7783b9b2007-09-22 22:10:44 +00007192 break;
7193 default:
7194 break;
7195 }
7196 }
7197 ptp_free_objectpropdesc(&opd);
7198 }
7199
7200 // proplist could be NULL if we can't write any properties
7201 if (props != NULL) {
7202 ret = ptp_mtp_setobjectproplist(params, props, nrofprops);
7203
Linus Walleija6d0d482007-10-31 08:54:56 +00007204 ptp_destroy_object_prop_list(props, nrofprops);
Linus Walleij7783b9b2007-09-22 22:10:44 +00007205
7206 if (ret != PTP_RC_OK) {
7207 // TODO: return error of which property we couldn't set
7208 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "update_abstract_list(): "
7209 "could not set object property list.");
Richard Low016e3732007-09-23 14:53:46 +00007210 free(properties);
Linus Walleij7783b9b2007-09-22 22:10:44 +00007211 return -1;
7212 }
7213 }
7214
7215 } else if (ptp_operation_issupported(params,PTP_OC_MTP_SetObjectPropValue)) {
7216 for (i=0;i<propcnt;i++) {
7217 switch (properties[i]) {
7218 case PTP_OPC_Name:
7219 // Update title
7220 ret = set_object_string(device, objecthandle, PTP_OPC_Name, name);
7221 if (ret != 0) {
7222 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "update_abstract_list(): "
7223 "could not set title.");
7224 }
7225 break;
Linus Walleij0b75ab62007-12-28 00:47:34 +00007226 case PTP_OPC_AlbumArtist:
7227 // Update album artist
7228 ret = set_object_string(device, objecthandle, PTP_OPC_AlbumArtist, artist);
7229 if (ret != 0) {
7230 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "update_abstract_list(): "
7231 "could not set album artist name.");
7232 }
7233 break;
Linus Walleij7783b9b2007-09-22 22:10:44 +00007234 case PTP_OPC_Artist:
7235 // Update artist
7236 ret = set_object_string(device, objecthandle, PTP_OPC_Artist, artist);
7237 if (ret != 0) {
7238 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "update_abstract_list(): "
7239 "could not set artist name.");
7240 }
Linus Walleij31b74292008-05-02 23:29:06 +00007241 case PTP_OPC_Composer:
7242 // Update composer
7243 ret = set_object_string(device, objecthandle, PTP_OPC_Composer, composer);
7244 if (ret != 0) {
7245 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "update_abstract_list(): "
7246 "could not set composer name.");
7247 }
Linus Walleij7783b9b2007-09-22 22:10:44 +00007248 break;
7249 case PTP_OPC_Genre:
7250 // Update genre
7251 ret = set_object_string(device, objecthandle, PTP_OPC_Genre, genre);
7252 if (ret != 0) {
7253 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "update_abstract_list(): "
7254 "could not set genre.");
7255 }
7256 break;
7257 case PTP_OPC_DateModified:
7258 // Update date modified
Linus Walleijcf8dc2b2008-10-21 13:58:36 +00007259 if (!FLAG_CANNOT_HANDLE_DATEMODIFIED(ptp_usb)) {
Linus Walleij7783b9b2007-09-22 22:10:44 +00007260 char *tmpdate = get_iso8601_stamp();
7261 ret = set_object_string(device, objecthandle, PTP_OPC_DateModified, tmpdate);
7262 if (ret != 0) {
7263 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "update_abstract_list(): "
7264 "could not set modification date.");
7265 }
7266 free(tmpdate);
7267 }
7268 default:
7269 break;
7270 }
7271 }
Linus Walleij7783b9b2007-09-22 22:10:44 +00007272 } else {
7273 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "update_abstract_list(): "
7274 "Your device doesn't seem to support any known way of setting metadata.");
Richard Low016e3732007-09-23 14:53:46 +00007275 free(properties);
Linus Walleij7783b9b2007-09-22 22:10:44 +00007276 return -1;
7277 }
7278
7279 // Then the object references...
Linus Walleij1ccd5862008-11-11 12:33:01 +00007280 ret = ptp_mtp_setobjectreferences (params, objecthandle, (uint32_t *) tracks, no_tracks);
7281 if (ret != PTP_RC_OK) {
7282 add_ptp_error_to_errorstack(device, ret, "update_abstract_list(): could not add tracks as object references.");
7283 free(properties);
7284 return -1;
Linus Walleij7783b9b2007-09-22 22:10:44 +00007285 }
7286
Richard Low016e3732007-09-23 14:53:46 +00007287 free(properties);
Linus Walleij7752b952007-10-19 20:11:46 +00007288
7289 update_metadata_cache(device, objecthandle);
Richard Low016e3732007-09-23 14:53:46 +00007290
Linus Walleij7783b9b2007-09-22 22:10:44 +00007291 return 0;
7292}
7293
Linus Walleijb7426d12006-11-25 23:19:11 +00007294
7295/**
7296 * This routine creates a new playlist based on the metadata
7297 * supplied. If the <code>tracks</code> field of the metadata
7298 * contains a track listing, these tracks will be added to the
7299 * playlist.
7300 * @param device a pointer to the device to create the new playlist on.
7301 * @param metadata the metadata for the new playlist. If the function
7302 * exits with success, the <code>playlist_id</code> field of this
7303 * struct will contain the new playlist ID of the playlist.
Linus Walleij2fe43e42009-01-30 20:31:26 +00007304 * <ul>
7305 * <li><code>metadata-&gt;parent_id</code> should be set to the parent
7306 * (e.g. folder) to store this track in. Since some
7307 * devices are a bit picky about where files
7308 * are placed, a default folder will be chosen if libmtp
7309 * has detected one for the current filetype and this
7310 * parameter is set to 0. If this is 0 and no default folder
7311 * can be found, the file will be stored in the root folder.
7312 * <li><code>metadata-&gt;storage_id</code> should be set to the
7313 * desired storage (e.g. memory card or whatever your device
7314 * presents) to store this track in. Setting this to 0 will store
7315 * the track on the primary storage.
7316 * </ul>
Linus Walleijb7426d12006-11-25 23:19:11 +00007317 * @return 0 on success, any other value means failure.
7318 * @see LIBMTP_Update_Playlist()
7319 * @see LIBMTP_Delete_Object()
7320 */
7321int LIBMTP_Create_New_Playlist(LIBMTP_mtpdevice_t *device,
Linus Walleijea68f1f2008-06-22 21:54:44 +00007322 LIBMTP_playlist_t * const metadata)
Linus Walleijb7426d12006-11-25 23:19:11 +00007323{
Linus Walleija4e6bdc2008-05-23 22:31:53 +00007324 PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo;
Linus Walleijea68f1f2008-06-22 21:54:44 +00007325 uint32_t localph = metadata->parent_id;
Linus Walleijb7426d12006-11-25 23:19:11 +00007326
7327 // Use a default folder if none given
7328 if (localph == 0) {
Linus Walleijafac36b2009-01-30 20:39:13 +00007329 if (device->default_playlist_folder != 0)
7330 localph = device->default_playlist_folder;
7331 else
7332 localph = device->default_music_folder;
Linus Walleijb7426d12006-11-25 23:19:11 +00007333 }
Linus Walleij5ce59db2008-03-12 21:22:58 +00007334 metadata->parent_id = localph;
Linus Walleijb7426d12006-11-25 23:19:11 +00007335
Linus Walleijf3c44052008-08-16 21:14:56 +00007336 // Samsung needs its own special type of playlists
7337 if(FLAG_PLAYLIST_SPL(ptp_usb)) {
7338 return playlist_t_to_spl(device, metadata);
7339 }
7340
Linus Walleijb7426d12006-11-25 23:19:11 +00007341 // Just create a new abstract audio/video playlist...
7342 return create_new_abstract_list(device,
7343 metadata->name,
Linus Walleijadce4a52007-04-23 07:28:11 +00007344 NULL,
7345 NULL,
Linus Walleij31b74292008-05-02 23:29:06 +00007346 NULL,
Linus Walleijb7426d12006-11-25 23:19:11 +00007347 localph,
Linus Walleijea68f1f2008-06-22 21:54:44 +00007348 metadata->storage_id,
Linus Walleijb7426d12006-11-25 23:19:11 +00007349 PTP_OFC_MTP_AbstractAudioVideoPlaylist,
Linus Walleija4e6bdc2008-05-23 22:31:53 +00007350 get_playlist_extension(ptp_usb),
Linus Walleijb7426d12006-11-25 23:19:11 +00007351 &metadata->playlist_id,
7352 metadata->tracks,
7353 metadata->no_tracks);
7354}
7355
Linus Walleij438bd7f2006-06-08 11:35:44 +00007356/**
7357 * This routine updates a playlist based on the metadata
7358 * supplied. If the <code>tracks</code> field of the metadata
7359 * contains a track listing, these tracks will be added to the
7360 * playlist in place of those already present, i.e. the
alistair_boyle5e5fcb72008-11-17 14:38:19 +00007361 * previous track listing will be deleted. For Samsung devices the
7362 * playlist id (metadata->playlist_id) is likely to change.
Linus Walleij438bd7f2006-06-08 11:35:44 +00007363 * @param device a pointer to the device to create the new playlist on.
7364 * @param metadata the metadata for the playlist to be updated.
mopoke96143402006-10-30 04:37:26 +00007365 * notice that the field <code>playlist_id</code>
alistair_boyle5e5fcb72008-11-17 14:38:19 +00007366 * must contain the apropriate playlist ID. Playlist ID
7367 * be modified to a new playlist ID by the time the
7368 * function returns since edit-in-place is not always possible.
Linus Walleij438bd7f2006-06-08 11:35:44 +00007369 * @return 0 on success, any other value means failure.
7370 * @see LIBMTP_Create_New_Playlist()
7371 * @see LIBMTP_Delete_Object()
7372 */
mopoke96143402006-10-30 04:37:26 +00007373int LIBMTP_Update_Playlist(LIBMTP_mtpdevice_t *device,
alistair_boyle5e5fcb72008-11-17 14:38:19 +00007374 LIBMTP_playlist_t * const metadata)
Linus Walleij438bd7f2006-06-08 11:35:44 +00007375{
Linus Walleijf3c44052008-08-16 21:14:56 +00007376
7377 // Samsung needs its own special type of playlists
7378 PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo;
7379 if(FLAG_PLAYLIST_SPL(ptp_usb)) {
7380 return update_spl_playlist(device, metadata);
7381 }
7382
Linus Walleij7783b9b2007-09-22 22:10:44 +00007383 return update_abstract_list(device,
7384 metadata->name,
7385 NULL,
7386 NULL,
Linus Walleij31b74292008-05-02 23:29:06 +00007387 NULL,
Linus Walleij7783b9b2007-09-22 22:10:44 +00007388 metadata->playlist_id,
7389 PTP_OFC_MTP_AbstractAudioVideoPlaylist,
7390 metadata->tracks,
7391 metadata->no_tracks);
Linus Walleij438bd7f2006-06-08 11:35:44 +00007392}
Linus Walleijaa4b0752006-07-26 22:21:04 +00007393
Linus Walleij0c33ec02006-10-27 10:15:40 +00007394/**
7395 * This creates a new album metadata structure and allocates memory
7396 * for it. Notice that if you add strings to this structure they
7397 * will be freed by the corresponding <code>LIBMTP_destroy_album_t</code>
7398 * operation later, so be careful of using strdup() when assigning
7399 * strings.
7400 *
7401 * @return a pointer to the newly allocated metadata structure.
7402 * @see LIBMTP_destroy_album_t()
7403 */
7404LIBMTP_album_t *LIBMTP_new_album_t(void)
7405{
7406 LIBMTP_album_t *new = (LIBMTP_album_t *) malloc(sizeof(LIBMTP_album_t));
7407 if (new == NULL) {
7408 return NULL;
7409 }
7410 new->album_id = 0;
Linus Walleij5ce59db2008-03-12 21:22:58 +00007411 new->parent_id = 0;
Linus Walleijea68f1f2008-06-22 21:54:44 +00007412 new->storage_id = 0;
Linus Walleij0c33ec02006-10-27 10:15:40 +00007413 new->name = NULL;
Linus Walleijadce4a52007-04-23 07:28:11 +00007414 new->artist = NULL;
Linus Walleij31b74292008-05-02 23:29:06 +00007415 new->composer = NULL;
Linus Walleijadce4a52007-04-23 07:28:11 +00007416 new->genre = NULL;
Linus Walleij0c33ec02006-10-27 10:15:40 +00007417 new->tracks = NULL;
7418 new->no_tracks = 0;
7419 new->next = NULL;
7420 return new;
7421}
7422
7423/**
7424 * This recursively deletes the memory for an album structure
7425 *
7426 * @param album structure to destroy
7427 * @see LIBMTP_new_album_t()
7428 */
7429void LIBMTP_destroy_album_t(LIBMTP_album_t *album)
7430{
7431 if (album == NULL) {
7432 return;
7433 }
7434 if (album->name != NULL)
7435 free(album->name);
Linus Walleijadce4a52007-04-23 07:28:11 +00007436 if (album->artist != NULL)
7437 free(album->artist);
Linus Walleij31b74292008-05-02 23:29:06 +00007438 if (album->composer != NULL)
7439 free(album->composer);
Linus Walleijadce4a52007-04-23 07:28:11 +00007440 if (album->genre != NULL)
7441 free(album->genre);
Linus Walleij0c33ec02006-10-27 10:15:40 +00007442 if (album->tracks != NULL)
7443 free(album->tracks);
7444 free(album);
7445 return;
7446}
7447
7448/**
7449 * This function returns a list of the albums available on the
7450 * device.
7451 *
7452 * @param device a pointer to the device to get the album listing from.
7453 * @return an album list on success, else NULL. If there are no albums
7454 * on the device, NULL will be returned as well.
7455 * @see LIBMTP_Get_Album()
7456 */
7457LIBMTP_album_t *LIBMTP_Get_Album_List(LIBMTP_mtpdevice_t *device)
7458{
7459 PTPParams *params = (PTPParams *) device->params;
7460 LIBMTP_album_t *retalbums = NULL;
7461 LIBMTP_album_t *curalbum = NULL;
7462 uint32_t i;
7463
7464 // Get all the handles if we haven't already done that
Linus Walleijd4637502009-06-14 23:03:33 +00007465 if (params->nrofobjects == 0) {
Linus Walleij0c33ec02006-10-27 10:15:40 +00007466 flush_handles(device);
7467 }
7468
Linus Walleijd4637502009-06-14 23:03:33 +00007469 for (i = 0; i < params->nrofobjects; i++) {
Linus Walleij0c33ec02006-10-27 10:15:40 +00007470 LIBMTP_album_t *alb;
Linus Walleijd4637502009-06-14 23:03:33 +00007471 PTPObject *ob;
Linus Walleij0c33ec02006-10-27 10:15:40 +00007472 uint16_t ret;
7473
Linus Walleijd4637502009-06-14 23:03:33 +00007474 ob = &params->objects[i];
Linus Walleij0c33ec02006-10-27 10:15:40 +00007475
Linus Walleijf0bf4372007-07-01 21:47:38 +00007476 // Ignore stuff that isn't an album
Linus Walleijd4637502009-06-14 23:03:33 +00007477 if ( ob->oi.ObjectFormat != PTP_OFC_MTP_AbstractAudioAlbum ) {
Linus Walleijf0bf4372007-07-01 21:47:38 +00007478 continue;
Linus Walleij0c33ec02006-10-27 10:15:40 +00007479 }
Linus Walleijf0bf4372007-07-01 21:47:38 +00007480
7481 // Allocate a new album type
7482 alb = LIBMTP_new_album_t();
Linus Walleijd4637502009-06-14 23:03:33 +00007483 alb->album_id = ob->oid;
7484 alb->parent_id = ob->oi.ParentObject;
7485 alb->storage_id = ob->oi.StorageID;
Linus Walleij5ce59db2008-03-12 21:22:58 +00007486
Linus Walleijf0bf4372007-07-01 21:47:38 +00007487 // Get metadata for it.
Linus Walleijd4637502009-06-14 23:03:33 +00007488 alb->name = get_string_from_object(device, ob->oid, PTP_OPC_Name);
7489 alb->artist = get_string_from_object(device, ob->oid, PTP_OPC_AlbumArtist);
Linus Walleij0b75ab62007-12-28 00:47:34 +00007490 if (alb->artist == NULL) {
Linus Walleijd4637502009-06-14 23:03:33 +00007491 alb->artist = get_string_from_object(device, ob->oid, PTP_OPC_Artist);
Linus Walleij0b75ab62007-12-28 00:47:34 +00007492 }
Linus Walleijd4637502009-06-14 23:03:33 +00007493 alb->composer = get_string_from_object(device, ob->oid, PTP_OPC_Composer);
7494 alb->genre = get_string_from_object(device, ob->oid, PTP_OPC_Genre);
Linus Walleijf0bf4372007-07-01 21:47:38 +00007495
7496 // Then get the track listing for this album
7497 ret = ptp_mtp_getobjectreferences(params, alb->album_id, &alb->tracks, &alb->no_tracks);
7498 if (ret != PTP_RC_OK) {
7499 add_ptp_error_to_errorstack(device, ret, "LIBMTP_Get_Album_List(): Could not get object references.");
7500 alb->tracks = NULL;
7501 alb->no_tracks = 0;
7502 }
7503
7504 // Add album to a list that will be returned afterwards.
7505 if (retalbums == NULL) {
7506 retalbums = alb;
7507 curalbum = alb;
7508 } else {
7509 curalbum->next = alb;
7510 curalbum = alb;
7511 }
7512
Linus Walleij0c33ec02006-10-27 10:15:40 +00007513 }
7514 return retalbums;
7515}
7516
7517/**
7518 * This function retrieves an individual album from the device.
7519 * @param device a pointer to the device to get the album from.
7520 * @param albid the unique ID of the album to retrieve.
7521 * @return a valid album metadata or NULL on failure.
7522 * @see LIBMTP_Get_Album_List()
7523 */
7524LIBMTP_album_t *LIBMTP_Get_Album(LIBMTP_mtpdevice_t *device, uint32_t const albid)
7525{
7526 PTPParams *params = (PTPParams *) device->params;
Linus Walleijd4637502009-06-14 23:03:33 +00007527 uint16_t ret;
7528 PTPObject *ob;
7529 LIBMTP_album_t *alb;
Linus Walleij0c33ec02006-10-27 10:15:40 +00007530
7531 // Get all the handles if we haven't already done that
Linus Walleijd4637502009-06-14 23:03:33 +00007532 if (params->nrofobjects == 0) {
Linus Walleij0c33ec02006-10-27 10:15:40 +00007533 flush_handles(device);
7534 }
7535
Linus Walleijd4637502009-06-14 23:03:33 +00007536 ret = ptp_object_want (params, albid, PTPOBJECT_OBJECTINFO_LOADED, &ob);
7537 if (ret != PTP_RC_OK)
7538 return NULL;
Linus Walleij0c33ec02006-10-27 10:15:40 +00007539
Linus Walleijd4637502009-06-14 23:03:33 +00007540 // Ignore stuff that isn't an album
7541 if ( ob->oi.ObjectFormat != PTP_OFC_MTP_AbstractAudioAlbum ) {
7542 return NULL;
Linus Walleij0c33ec02006-10-27 10:15:40 +00007543 }
Linus Walleijd4637502009-06-14 23:03:33 +00007544
7545 // Allocate a new album type
7546 alb = LIBMTP_new_album_t();
7547 alb->album_id = ob->oid;
7548 alb->parent_id = ob->oi.ParentObject;
7549 alb->storage_id = ob->oi.StorageID;
7550
7551 // Get metadata for it.
7552 alb->name = get_string_from_object(device, ob->oid, PTP_OPC_Name);
7553 alb->artist = get_string_from_object(device, ob->oid, PTP_OPC_AlbumArtist);
7554 if (alb->artist == NULL) {
7555 alb->artist = get_string_from_object(device, ob->oid, PTP_OPC_Artist);
7556 }
7557 alb->composer = get_string_from_object(device, ob->oid, PTP_OPC_Composer);
7558 alb->genre = get_string_from_object(device, ob->oid, PTP_OPC_Genre);
7559 ret = ptp_mtp_getobjectreferences(params, alb->album_id, &alb->tracks, &alb->no_tracks);
7560 if (ret != PTP_RC_OK) {
7561 add_ptp_error_to_errorstack(device, ret, "LIBMTP_Get_Album: Could not get object references.");
7562 alb->tracks = NULL;
7563 alb->no_tracks = 0;
7564 }
7565
7566 return alb;
Linus Walleij0c33ec02006-10-27 10:15:40 +00007567}
7568
7569/**
7570 * This routine creates a new album based on the metadata
7571 * supplied. If the <code>tracks</code> field of the metadata
7572 * contains a track listing, these tracks will be added to the
7573 * album.
7574 * @param device a pointer to the device to create the new album on.
7575 * @param metadata the metadata for the new album. If the function
7576 * exits with success, the <code>album_id</code> field of this
7577 * struct will contain the new ID of the album.
Linus Walleij2fe43e42009-01-30 20:31:26 +00007578 * <ul>
7579 * <li><code>metadata-&gt;parent_id</code> should be set to the parent
7580 * (e.g. folder) to store this track in. Since some
7581 * devices are a bit picky about where files
7582 * are placed, a default folder will be chosen if libmtp
7583 * has detected one for the current filetype and this
7584 * parameter is set to 0. If this is 0 and no default folder
7585 * can be found, the file will be stored in the root folder.
7586 * <li><code>metadata-&gt;storage_id</code> should be set to the
7587 * desired storage (e.g. memory card or whatever your device
7588 * presents) to store this track in. Setting this to 0 will store
7589 * the track on the primary storage.
7590 * </ul>
Linus Walleij0c33ec02006-10-27 10:15:40 +00007591 * @return 0 on success, any other value means failure.
7592 * @see LIBMTP_Update_Album()
7593 * @see LIBMTP_Delete_Object()
7594 */
7595int LIBMTP_Create_New_Album(LIBMTP_mtpdevice_t *device,
Linus Walleijea68f1f2008-06-22 21:54:44 +00007596 LIBMTP_album_t * const metadata)
Linus Walleij0c33ec02006-10-27 10:15:40 +00007597{
Linus Walleijea68f1f2008-06-22 21:54:44 +00007598 uint32_t localph = metadata->parent_id;
mopoke96143402006-10-30 04:37:26 +00007599
Linus Walleij0c33ec02006-10-27 10:15:40 +00007600 // Use a default folder if none given
7601 if (localph == 0) {
Linus Walleijafac36b2009-01-30 20:39:13 +00007602 if (device->default_album_folder != 0)
7603 localph = device->default_album_folder;
7604 else
7605 localph = device->default_music_folder;
Linus Walleij0c33ec02006-10-27 10:15:40 +00007606 }
Linus Walleij5ce59db2008-03-12 21:22:58 +00007607 metadata->parent_id = localph;
Linus Walleij0c33ec02006-10-27 10:15:40 +00007608
Linus Walleijb7426d12006-11-25 23:19:11 +00007609 // Just create a new abstract album...
7610 return create_new_abstract_list(device,
7611 metadata->name,
Linus Walleijadce4a52007-04-23 07:28:11 +00007612 metadata->artist,
Linus Walleij31b74292008-05-02 23:29:06 +00007613 metadata->composer,
Linus Walleijadce4a52007-04-23 07:28:11 +00007614 metadata->genre,
Linus Walleijb7426d12006-11-25 23:19:11 +00007615 localph,
Linus Walleijea68f1f2008-06-22 21:54:44 +00007616 metadata->storage_id,
Linus Walleijb7426d12006-11-25 23:19:11 +00007617 PTP_OFC_MTP_AbstractAudioAlbum,
7618 ".alb",
7619 &metadata->album_id,
7620 metadata->tracks,
7621 metadata->no_tracks);
Linus Walleij0c33ec02006-10-27 10:15:40 +00007622}
7623
7624/**
rreardon5332f9c2006-12-05 10:18:23 +00007625 * This creates a new sample data metadata structure and allocates memory
7626 * for it. Notice that if you add strings to this structure they
7627 * will be freed by the corresponding <code>LIBMTP_destroy_sampledata_t</code>
7628 * operation later, so be careful of using strdup() when assigning
7629 * strings.
7630 *
7631 * @return a pointer to the newly allocated metadata structure.
7632 * @see LIBMTP_destroy_sampledata_t()
7633 */
7634LIBMTP_filesampledata_t *LIBMTP_new_filesampledata_t(void)
7635{
7636 LIBMTP_filesampledata_t *new = (LIBMTP_filesampledata_t *) malloc(sizeof(LIBMTP_filesampledata_t));
7637 if (new == NULL) {
7638 return NULL;
7639 }
7640 new->height=0;
7641 new->width = 0;
7642 new->data = NULL;
7643 new->duration = 0;
7644 new->size = 0;
7645 return new;
7646}
7647
Linus Walleijf1b02f22006-12-06 09:03:23 +00007648/**
7649 * This destroys a file sample metadata type.
7650 * @param sample the file sample metadata to be destroyed.
7651 */
7652void LIBMTP_destroy_filesampledata_t(LIBMTP_filesampledata_t * sample)
7653{
7654 if (sample == NULL) {
7655 return;
7656 }
7657 if (sample->data != NULL) {
7658 free(sample->data);
7659 }
7660 free(sample);
7661}
7662
7663/**
7664 * This routine figures out whether a certain filetype supports
7665 * representative samples (small thumbnail images) or not. This
7666 * typically applies to JPEG files, MP3 files and Album abstract
7667 * playlists, but in theory any filetype could support representative
7668 * samples.
7669 * @param device a pointer to the device which is to be examined.
Linus Walleij9ffe9982008-01-20 13:42:52 +00007670 * @param filetype the fileype to examine, and return the representative sample
Linus Walleijf1b02f22006-12-06 09:03:23 +00007671 * properties for.
7672 * @param sample this will contain a new sample type with the fields
7673 * filled in with suitable default values. For example, the
7674 * supported sample type will be set, the supported height and
7675 * width will be set to max values if it is an image sample,
7676 * and duration will also be given some suitable default value
7677 * which should not be exceeded on audio samples. If the
7678 * device does not support samples for this filetype, this
7679 * pointer will be NULL. If it is not NULL, the user must
7680 * destroy this struct with <code>LIBMTP_destroy_filesampledata_t()</code>
7681 * after use.
7682 * @return 0 on success, any other value means failure.
7683 * @see LIBMTP_Send_Representative_Sample()
7684 * @see LIBMTP_Create_New_Album()
7685 */
7686int LIBMTP_Get_Representative_Sample_Format(LIBMTP_mtpdevice_t *device,
7687 LIBMTP_filetype_t const filetype,
7688 LIBMTP_filesampledata_t ** sample)
7689{
7690 uint16_t ret;
7691 PTPParams *params = (PTPParams *) device->params;
7692 uint16_t *props = NULL;
7693 uint32_t propcnt = 0;
7694 int i;
7695 // TODO: Get rid of these when we can properly query the device.
7696 int support_data = 0;
7697 int support_format = 0;
7698 int support_height = 0;
7699 int support_width = 0;
7700 int support_duration = 0;
Richard Low6f070842009-05-03 10:26:16 +00007701 int support_size = 0;
Linus Walleijf1b02f22006-12-06 09:03:23 +00007702
rreardon21a3a9d2007-08-15 10:56:31 +00007703 PTPObjectPropDesc opd_height;
7704 PTPObjectPropDesc opd_width;
7705 PTPObjectPropDesc opd_format;
7706 PTPObjectPropDesc opd_duration;
Richard Low6f070842009-05-03 10:26:16 +00007707 PTPObjectPropDesc opd_size;
rreardon21a3a9d2007-08-15 10:56:31 +00007708
Linus Walleijf1b02f22006-12-06 09:03:23 +00007709 // Default to no type supported.
7710 *sample = NULL;
7711
7712 ret = ptp_mtp_getobjectpropssupported(params, map_libmtp_type_to_ptp_type(filetype), &propcnt, &props);
7713 if (ret != PTP_RC_OK) {
Linus Walleij070e9b42007-01-22 08:49:28 +00007714 add_ptp_error_to_errorstack(device, ret, "LIBMTP_Get_Representative_Sample_Format(): could not get object properties.");
Linus Walleijf1b02f22006-12-06 09:03:23 +00007715 return -1;
7716 }
7717 /*
7718 * TODO: when walking through these object properties, make calls to
7719 * a new function in ptp.h/ptp.c that can send the command
7720 * PTP_OC_MTP_GetObjectPropDesc to get max/min values of the properties
7721 * supported.
7722 */
7723 for (i = 0; i < propcnt; i++) {
7724 switch(props[i]) {
7725 case PTP_OPC_RepresentativeSampleData:
7726 support_data = 1;
7727 break;
7728 case PTP_OPC_RepresentativeSampleFormat:
7729 support_format = 1;
7730 break;
7731 case PTP_OPC_RepresentativeSampleSize:
Richard Low6f070842009-05-03 10:26:16 +00007732 support_size = 1;
Linus Walleijf1b02f22006-12-06 09:03:23 +00007733 break;
7734 case PTP_OPC_RepresentativeSampleHeight:
7735 support_height = 1;
7736 break;
7737 case PTP_OPC_RepresentativeSampleWidth:
7738 support_width = 1;
7739 break;
7740 case PTP_OPC_RepresentativeSampleDuration:
7741 support_duration = 1;
7742 break;
7743 default:
7744 break;
7745 }
7746 }
7747 free(props);
Richard Low6f070842009-05-03 10:26:16 +00007748
Linus Walleijf1b02f22006-12-06 09:03:23 +00007749 if (support_data && support_format && support_height && support_width && !support_duration) {
7750 // Something that supports height and width and not duration is likely to be JPEG
7751 LIBMTP_filesampledata_t *retsam = LIBMTP_new_filesampledata_t();
rreardon21a3a9d2007-08-15 10:56:31 +00007752 /*
7753 * Populate the sample format with the first supported format
7754 *
7755 * TODO: figure out how to pass back more than one format if more are
7756 * supported by the device.
7757 */
7758 ptp_mtp_getobjectpropdesc (params, PTP_OPC_RepresentativeSampleFormat, map_libmtp_type_to_ptp_type(filetype), &opd_format);
7759 retsam->filetype = map_ptp_type_to_libmtp_type(opd_format.FORM.Enum.SupportedValue[0].u16);
Richard Low6f070842009-05-03 10:26:16 +00007760 ptp_free_objectpropdesc(&opd_format);
rreardon21a3a9d2007-08-15 10:56:31 +00007761 /* Populate the maximum image height */
7762 ptp_mtp_getobjectpropdesc (params, PTP_OPC_RepresentativeSampleWidth, map_libmtp_type_to_ptp_type(filetype), &opd_width);
7763 retsam->width = opd_width.FORM.Range.MaximumValue.u32;
Richard Low6f070842009-05-03 10:26:16 +00007764 ptp_free_objectpropdesc(&opd_width);
rreardon21a3a9d2007-08-15 10:56:31 +00007765 /* Populate the maximum image width */
7766 ptp_mtp_getobjectpropdesc (params, PTP_OPC_RepresentativeSampleHeight, map_libmtp_type_to_ptp_type(filetype), &opd_height);
7767 retsam->height = opd_height.FORM.Range.MaximumValue.u32;
Richard Low6f070842009-05-03 10:26:16 +00007768 ptp_free_objectpropdesc(&opd_height);
7769 /* Populate the maximum size */
7770 if (support_size) {
7771 ptp_mtp_getobjectpropdesc (params, PTP_OPC_RepresentativeSampleSize, map_libmtp_type_to_ptp_type(filetype), &opd_size);
7772 retsam->size = opd_size.FORM.Range.MaximumValue.u32;
7773 ptp_free_objectpropdesc(&opd_size);
7774 }
Linus Walleijf1b02f22006-12-06 09:03:23 +00007775 *sample = retsam;
7776 } else if (support_data && support_format && !support_height && !support_width && support_duration) {
7777 // Another qualified guess
7778 LIBMTP_filesampledata_t *retsam = LIBMTP_new_filesampledata_t();
rreardon21a3a9d2007-08-15 10:56:31 +00007779 /*
7780 * Populate the sample format with the first supported format
7781 *
7782 * TODO: figure out how to pass back more than one format if more are
7783 * supported by the device.
7784 */
7785 ptp_mtp_getobjectpropdesc (params, PTP_OPC_RepresentativeSampleFormat, map_libmtp_type_to_ptp_type(filetype), &opd_format);
7786 retsam->filetype = map_ptp_type_to_libmtp_type(opd_format.FORM.Enum.SupportedValue[0].u16);
Richard Low6f070842009-05-03 10:26:16 +00007787 ptp_free_objectpropdesc(&opd_format);
7788 /* Populate the maximum duration */
rreardon21a3a9d2007-08-15 10:56:31 +00007789 ptp_mtp_getobjectpropdesc (params, PTP_OPC_RepresentativeSampleDuration, map_libmtp_type_to_ptp_type(filetype), &opd_duration);
7790 retsam->duration = opd_duration.FORM.Range.MaximumValue.u32;
Richard Low6f070842009-05-03 10:26:16 +00007791 ptp_free_objectpropdesc(&opd_duration);
7792 /* Populate the maximum size */
7793 if (support_size) {
7794 ptp_mtp_getobjectpropdesc (params, PTP_OPC_RepresentativeSampleSize, map_libmtp_type_to_ptp_type(filetype), &opd_size);
7795 retsam->size = opd_size.FORM.Range.MaximumValue.u32;
7796 ptp_free_objectpropdesc(&opd_size);
7797 }
Linus Walleijf1b02f22006-12-06 09:03:23 +00007798 *sample = retsam;
7799 }
7800 return 0;
7801}
rreardon5332f9c2006-12-05 10:18:23 +00007802
7803/**
7804 * This routine sends representative sample data for an object.
7805 * This uses the RepresentativeSampleData property of the album,
7806 * if the device supports it. The data should be of a format acceptable
7807 * to the player (for iRiver and Creative, this seems to be JPEG) and
7808 * must not be too large. (for a Creative, max seems to be about 20KB.)
Richard Low6f070842009-05-03 10:26:16 +00007809 * Check by calling LIBMTP_Get_Representative_Sample_Format() to get
7810 * maximum size, dimensions, etc..
Linus Walleij7e3f2762006-12-03 22:52:05 +00007811 * @param device a pointer to the device which the object is on.
7812 * @param id unique id of the object to set artwork for.
Richard Low36e447c2008-01-20 15:24:55 +00007813 * @param pointer to LIBMTP_filesampledata_t struct containing data
Linus Walleij0c33ec02006-10-27 10:15:40 +00007814 * @return 0 on success, any other value means failure.
Richard Low36e447c2008-01-20 15:24:55 +00007815 * @see LIBMTP_Get_Representative_Sample()
Linus Walleijf1b02f22006-12-06 09:03:23 +00007816 * @see LIBMTP_Get_Representative_Sample_Format()
Linus Walleij0c33ec02006-10-27 10:15:40 +00007817 * @see LIBMTP_Create_New_Album()
7818 */
Linus Walleij7e3f2762006-12-03 22:52:05 +00007819int LIBMTP_Send_Representative_Sample(LIBMTP_mtpdevice_t *device,
rreardon5332f9c2006-12-05 10:18:23 +00007820 uint32_t const id,
7821 LIBMTP_filesampledata_t *sampledata)
Linus Walleij0c33ec02006-10-27 10:15:40 +00007822{
7823 uint16_t ret;
7824 PTPParams *params = (PTPParams *) device->params;
Richard Lowbb9fb4a2008-05-18 14:49:34 +00007825 PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo;
Linus Walleij0c33ec02006-10-27 10:15:40 +00007826 PTPPropertyValue propval;
Linus Walleijd4637502009-06-14 23:03:33 +00007827 PTPObject *ob;
Linus Walleijf0bf4372007-07-01 21:47:38 +00007828 uint32_t i;
Linus Walleijf1b02f22006-12-06 09:03:23 +00007829 uint16_t *props = NULL;
7830 uint32_t propcnt = 0;
7831 int supported = 0;
rreardon5332f9c2006-12-05 10:18:23 +00007832
7833 // get the file format for the object we're going to send representative data for
Linus Walleijd4637502009-06-14 23:03:33 +00007834 ret = ptp_object_want (params, id, PTPOBJECT_OBJECTINFO_LOADED, &ob);
7835 if (ret != PTP_RC_OK) {
Linus Walleijf0bf4372007-07-01 21:47:38 +00007836 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Send_Representative_Sample(): could not get object info.");
rreardon5332f9c2006-12-05 10:18:23 +00007837 return -1;
7838 }
7839
7840 // check that we can send representative sample data for this object format
Linus Walleijd4637502009-06-14 23:03:33 +00007841 ret = ptp_mtp_getobjectpropssupported(params, ob->oi.ObjectFormat, &propcnt, &props);
Linus Walleij0c33ec02006-10-27 10:15:40 +00007842 if (ret != PTP_RC_OK) {
Linus Walleij070e9b42007-01-22 08:49:28 +00007843 add_ptp_error_to_errorstack(device, ret, "LIBMTP_Send_Representative_Sample(): could not get object properties.");
Linus Walleij0c33ec02006-10-27 10:15:40 +00007844 return -1;
7845 }
Linus Walleijf1b02f22006-12-06 09:03:23 +00007846
Linus Walleij0c33ec02006-10-27 10:15:40 +00007847 for (i = 0; i < propcnt; i++) {
Linus Walleijf1b02f22006-12-06 09:03:23 +00007848 if (props[i] == PTP_OPC_RepresentativeSampleData) {
Linus Walleij0c33ec02006-10-27 10:15:40 +00007849 supported = 1;
Linus Walleijf1b02f22006-12-06 09:03:23 +00007850 break;
7851 }
Linus Walleij0c33ec02006-10-27 10:15:40 +00007852 }
7853 if (!supported) {
Linus Walleijf1b02f22006-12-06 09:03:23 +00007854 free(props);
Linus Walleij070e9b42007-01-22 08:49:28 +00007855 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 +00007856 return -1;
7857 }
Linus Walleijf1b02f22006-12-06 09:03:23 +00007858 free(props);
rreardon5332f9c2006-12-05 10:18:23 +00007859
Linus Walleijf1b02f22006-12-06 09:03:23 +00007860 // Go ahead and send the data
rreardon5332f9c2006-12-05 10:18:23 +00007861 propval.a.count = sampledata->size;
7862 propval.a.v = malloc(sizeof(PTPPropertyValue) * sampledata->size);
7863 for (i = 0; i < sampledata->size; i++) {
7864 propval.a.v[i].u8 = sampledata->data[i];
Linus Walleij7e3f2762006-12-03 22:52:05 +00007865 }
7866
Linus Walleij0c33ec02006-10-27 10:15:40 +00007867 ret = ptp_mtp_setobjectpropvalue(params,id,PTP_OPC_RepresentativeSampleData,
Linus Walleijf1b02f22006-12-06 09:03:23 +00007868 &propval,PTP_DTC_AUINT8);
Linus Walleij0c33ec02006-10-27 10:15:40 +00007869 if (ret != PTP_RC_OK) {
Linus Walleij070e9b42007-01-22 08:49:28 +00007870 add_ptp_error_to_errorstack(device, ret, "LIBMTP_Send_Representative_Sample(): could not send sample data.");
Linus Walleij7e3f2762006-12-03 22:52:05 +00007871 free(propval.a.v);
Linus Walleij0c33ec02006-10-27 10:15:40 +00007872 return -1;
7873 }
Linus Walleij7e3f2762006-12-03 22:52:05 +00007874 free(propval.a.v);
rreardond2ddb632006-12-12 12:13:21 +00007875
7876 /* Set the height and width if the sample is an image, otherwise just
7877 * set the duration and size */
7878 switch(sampledata->filetype) {
Linus Walleije1ac07e2006-12-14 19:38:59 +00007879 case LIBMTP_FILETYPE_JPEG:
7880 case LIBMTP_FILETYPE_JFIF:
7881 case LIBMTP_FILETYPE_TIFF:
7882 case LIBMTP_FILETYPE_BMP:
7883 case LIBMTP_FILETYPE_GIF:
7884 case LIBMTP_FILETYPE_PICT:
7885 case LIBMTP_FILETYPE_PNG:
Linus Walleijfec4d562008-06-01 22:30:36 +00007886 if (!FLAG_BROKEN_SET_SAMPLE_DIMENSIONS(ptp_usb)) {
Richard Lowbb9fb4a2008-05-18 14:49:34 +00007887 // For images, set the height and width
7888 set_object_u32(device, id, PTP_OPC_RepresentativeSampleHeight, sampledata->height);
7889 set_object_u32(device, id, PTP_OPC_RepresentativeSampleWidth, sampledata->width);
7890 }
Linus Walleije1ac07e2006-12-14 19:38:59 +00007891 break;
7892 default:
7893 // For anything not an image, set the duration and size
7894 set_object_u32(device, id, PTP_OPC_RepresentativeSampleDuration, sampledata->duration);
7895 set_object_u32(device, id, PTP_OPC_RepresentativeSampleSize, sampledata->size);
7896 break;
rreardond2ddb632006-12-12 12:13:21 +00007897 }
rreardond2ddb632006-12-12 12:13:21 +00007898
Linus Walleij0c33ec02006-10-27 10:15:40 +00007899 return 0;
7900}
7901
7902/**
Richard Low36e447c2008-01-20 15:24:55 +00007903 * This routine gets representative sample data for an object.
7904 * This uses the RepresentativeSampleData property of the album,
7905 * if the device supports it.
7906 * @param device a pointer to the device which the object is on.
7907 * @param id unique id of the object to get data for.
7908 * @param pointer to LIBMTP_filesampledata_t struct to receive data
7909 * @return 0 on success, any other value means failure.
7910 * @see LIBMTP_Send_Representative_Sample()
7911 * @see LIBMTP_Get_Representative_Sample_Format()
7912 * @see LIBMTP_Create_New_Album()
7913 */
7914int LIBMTP_Get_Representative_Sample(LIBMTP_mtpdevice_t *device,
7915 uint32_t const id,
7916 LIBMTP_filesampledata_t *sampledata)
7917{
7918 uint16_t ret;
7919 PTPParams *params = (PTPParams *) device->params;
7920 PTPPropertyValue propval;
Linus Walleijd4637502009-06-14 23:03:33 +00007921 PTPObject *ob;
Richard Low36e447c2008-01-20 15:24:55 +00007922 uint32_t i;
7923 uint16_t *props = NULL;
7924 uint32_t propcnt = 0;
7925 int supported = 0;
7926
7927 // get the file format for the object we're going to send representative data for
Linus Walleijd4637502009-06-14 23:03:33 +00007928 ret = ptp_object_want (params, id, PTPOBJECT_OBJECTINFO_LOADED, &ob);
7929 if (ret != PTP_RC_OK) {
Richard Low36e447c2008-01-20 15:24:55 +00007930 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Get_Representative_Sample(): could not get object info.");
7931 return -1;
7932 }
7933
7934 // check that we can store representative sample data for this object format
Linus Walleijd4637502009-06-14 23:03:33 +00007935 ret = ptp_mtp_getobjectpropssupported(params, ob->oi.ObjectFormat, &propcnt, &props);
Richard Low36e447c2008-01-20 15:24:55 +00007936 if (ret != PTP_RC_OK) {
7937 add_ptp_error_to_errorstack(device, ret, "LIBMTP_Get_Representative_Sample(): could not get object properties.");
7938 return -1;
7939 }
7940
7941 for (i = 0; i < propcnt; i++) {
7942 if (props[i] == PTP_OPC_RepresentativeSampleData) {
7943 supported = 1;
7944 break;
7945 }
7946 }
7947 if (!supported) {
7948 free(props);
7949 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Get_Representative_Sample(): object type doesn't support RepresentativeSampleData.");
7950 return -1;
7951 }
7952 free(props);
7953
7954 // Get the data
7955 ret = ptp_mtp_getobjectpropvalue(params,id,PTP_OPC_RepresentativeSampleData,
7956 &propval,PTP_DTC_AUINT8);
7957 if (ret != PTP_RC_OK) {
7958 add_ptp_error_to_errorstack(device, ret, "LIBMTP_Get_Representative_Sample(): could not get sample data.");
7959 return -1;
7960 }
7961
7962 // Store it
7963 sampledata->size = propval.a.count;
7964 sampledata->data = malloc(sizeof(PTPPropertyValue) * propval.a.count);
7965 for (i = 0; i < propval.a.count; i++) {
7966 sampledata->data[i] = propval.a.v[i].u8;
7967 }
7968 free(propval.a.v);
7969
7970 // Get the other properties
7971 sampledata->width = get_u32_from_object(device, id, PTP_OPC_RepresentativeSampleWidth, 0);
7972 sampledata->height = get_u32_from_object(device, id, PTP_OPC_RepresentativeSampleHeight, 0);
7973 sampledata->duration = get_u32_from_object(device, id, PTP_OPC_RepresentativeSampleDuration, 0);
7974 sampledata->filetype = map_ptp_type_to_libmtp_type(
7975 get_u16_from_object(device, id, PTP_OPC_RepresentativeSampleFormat, LIBMTP_FILETYPE_UNKNOWN));
7976
7977 return 0;
7978}
7979
7980/**
Linus Walleij0c33ec02006-10-27 10:15:40 +00007981 * This routine updates an album based on the metadata
7982 * supplied. If the <code>tracks</code> field of the metadata
7983 * contains a track listing, these tracks will be added to the
7984 * album in place of those already present, i.e. the
7985 * previous track listing will be deleted.
7986 * @param device a pointer to the device to create the new album on.
7987 * @param metadata the metadata for the album to be updated.
7988 * notice that the field <code>album_id</code>
7989 * must contain the apropriate album ID.
7990 * @return 0 on success, any other value means failure.
7991 * @see LIBMTP_Create_New_Album()
7992 * @see LIBMTP_Delete_Object()
7993 */
7994int LIBMTP_Update_Album(LIBMTP_mtpdevice_t *device,
7995 LIBMTP_album_t const * const metadata)
7996{
Linus Walleij7783b9b2007-09-22 22:10:44 +00007997 return update_abstract_list(device,
7998 metadata->name,
7999 metadata->artist,
Linus Walleij31b74292008-05-02 23:29:06 +00008000 metadata->composer,
Linus Walleij7783b9b2007-09-22 22:10:44 +00008001 metadata->genre,
8002 metadata->album_id,
8003 PTP_OFC_MTP_AbstractAudioAlbum,
8004 metadata->tracks,
8005 metadata->no_tracks);
Linus Walleij0c33ec02006-10-27 10:15:40 +00008006}
Linus Walleijaa4b0752006-07-26 22:21:04 +00008007
8008/**
8009 * Dummy function needed to interface to upstream
8010 * ptp.c/ptp.h files.
8011 */
8012void ptp_nikon_getptpipguid (unsigned char* guid) {
8013 return;
8014}
tsaarnia3eb60a2007-07-06 17:41:30 +00008015
8016/**
Linus Walleij7752b952007-10-19 20:11:46 +00008017 * Add an object to cache.
8018 * @param device the device which may have a cache to which the object should be added.
8019 * @param object_id the object to add to the cache.
tsaarnia3eb60a2007-07-06 17:41:30 +00008020 */
Linus Walleija6d0d482007-10-31 08:54:56 +00008021static void add_object_to_cache(LIBMTP_mtpdevice_t *device, uint32_t object_id)
tsaarnia3eb60a2007-07-06 17:41:30 +00008022{
8023 PTPParams *params = (PTPParams *)device->params;
tsaarnia3eb60a2007-07-06 17:41:30 +00008024 uint16_t ret;
tsaarnia3eb60a2007-07-06 17:41:30 +00008025
Linus Walleija6d0d482007-10-31 08:54:56 +00008026 ret = ptp_add_object_to_cache(params, object_id);
8027 if (ret != PTP_RC_OK) {
8028 add_ptp_error_to_errorstack(device, ret, "add_object_to_cache(): couldn't add object to cache");
tsaarnia3eb60a2007-07-06 17:41:30 +00008029 }
8030}
8031
8032
8033/**
8034 * Update cache after object has been modified
Linus Walleija6d0d482007-10-31 08:54:56 +00008035 * @param device the device which may have a cache to which the object should be updated.
8036 * @param object_id the object to update.
tsaarnia3eb60a2007-07-06 17:41:30 +00008037 */
Linus Walleij7752b952007-10-19 20:11:46 +00008038static void update_metadata_cache(LIBMTP_mtpdevice_t *device, uint32_t object_id)
tsaarnia3eb60a2007-07-06 17:41:30 +00008039{
Linus Walleija3d0eaa2007-11-18 22:00:48 +00008040 PTPParams *params = (PTPParams *)device->params;
8041
8042 ptp_remove_object_from_cache(params, object_id);
Linus Walleija6d0d482007-10-31 08:54:56 +00008043 add_object_to_cache(device, object_id);
tsaarnia3eb60a2007-07-06 17:41:30 +00008044}