blob: eb8f663cf65d6d8ec1682f5c380ed6d704c88d6b [file] [log] [blame]
Linus Walleij10c58422006-06-02 08:51:53 +00001/**
2 * \file libmtp.c
Linus Walleij2f45d222007-02-02 22:47:39 +00003 *
Linus Walleij9ffe9982008-01-20 13:42:52 +00004 * Copyright (C) 2005-2008 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 Walleij05358382007-08-06 20:46:35 +000037#define _LARGEFILE_SOURCE
38#define _LARGEFILE64_SOURCE
Linus Walleij2f45d222007-02-02 22:47:39 +000039
Linus Walleijeb8c6fe2006-02-03 09:46:22 +000040#include "libmtp.h"
Linus Walleijb9256fd2006-02-15 09:40:43 +000041#include "unicode.h"
Linus Walleij394bbbe2006-02-22 16:10:53 +000042#include "ptp.h"
Linus Walleij15e344f2006-03-06 15:15:00 +000043#include "libusb-glue.h"
Linus Walleij3e667ae2007-10-29 23:29:39 +000044#include "device-flags.h"
Linus Walleijf3c44052008-08-16 21:14:56 +000045#include "playlist-spl.h"
Linus Walleijeb8c6fe2006-02-03 09:46:22 +000046
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 Walleijc86afbd2006-05-04 19:05:24 +000058/*
59 * This is a mapping between libmtp internal MTP filetypes and
60 * the libgphoto2/PTP equivalent defines. We need this because
61 * otherwise the libmtp.h device has to be dependent on ptp.h
62 * to be installed too, and we don't want that.
63 */
Linus Walleijcf42f452006-11-28 08:32:49 +000064//typedef struct filemap_struct filemap_t;
65typedef struct filemap_struct {
raveloxd9a28642006-05-26 23:42:22 +000066 char *description; /**< Text description for the file type */
67 LIBMTP_filetype_t id; /**< LIBMTP internal type for the file type */
68 uint16_t ptp_id; /**< PTP ID for the filetype */
Linus Walleijcf42f452006-11-28 08:32:49 +000069 struct filemap_struct *next;
70} filemap_t;
Linus Walleijc86afbd2006-05-04 19:05:24 +000071
Linus Walleijf67bca92006-05-29 09:33:39 +000072// Global variables
Linus Walleij438bd7f2006-06-08 11:35:44 +000073// This holds the global filetype mapping table
Linus Walleijcf42f452006-11-28 08:32:49 +000074static filemap_t *filemap = NULL;
Linus Walleijf67bca92006-05-29 09:33:39 +000075
Linus Walleijcf42f452006-11-28 08:32:49 +000076/*
77 * Forward declarations of local (static) functions.
78 */
79static int register_filetype(char const * const description, LIBMTP_filetype_t const id,
80 uint16_t const ptp_id);
Linus Walleij7beba572006-11-29 08:56:12 +000081static void init_filemap();
Linus Walleij2715c442007-01-20 22:35:29 +000082static void add_error_to_errorstack(LIBMTP_mtpdevice_t *device,
83 LIBMTP_error_number_t errornumber,
84 char const * const error_text);
Linus Walleij070e9b42007-01-22 08:49:28 +000085static void add_ptp_error_to_errorstack(LIBMTP_mtpdevice_t *device,
86 uint16_t ptp_error,
87 char const * const error_text);
Linus Walleij438bd7f2006-06-08 11:35:44 +000088static void flush_handles(LIBMTP_mtpdevice_t *device);
Linus Walleij6bf68b42007-09-03 22:50:02 +000089static void get_handles_recursively(LIBMTP_mtpdevice_t *device,
90 PTPParams *params,
91 PTPObjectHandles *handles,
92 uint32_t storageid,
93 uint32_t parent);
Linus Walleij9e1b0812006-12-12 19:22:02 +000094static void free_storage_list(LIBMTP_mtpdevice_t *device);
95static int sort_storage_by(LIBMTP_mtpdevice_t *device, int const sortby);
96static uint32_t get_first_storageid(LIBMTP_mtpdevice_t *device);
Linus Walleij6bf68b42007-09-03 22:50:02 +000097static int get_storage_freespace(LIBMTP_mtpdevice_t *device,
98 LIBMTP_devicestorage_t *storage,
99 uint64_t *freespace);
100static int check_if_file_fits(LIBMTP_mtpdevice_t *device,
101 LIBMTP_devicestorage_t *storage,
102 uint64_t const filesize);
Linus Walleijf67bca92006-05-29 09:33:39 +0000103static uint16_t map_libmtp_type_to_ptp_type(LIBMTP_filetype_t intype);
104static LIBMTP_filetype_t map_ptp_type_to_libmtp_type(uint16_t intype);
mopoke96143402006-10-30 04:37:26 +0000105static int get_device_unicode_property(LIBMTP_mtpdevice_t *device,
Linus Walleijcf223e62006-06-19 09:31:53 +0000106 char **unicstring, uint16_t property);
Linus Walleij29559562007-08-22 21:38:04 +0000107static uint16_t adjust_u16(uint16_t val, PTPObjectPropDesc *opd);
108static uint32_t adjust_u32(uint32_t val, PTPObjectPropDesc *opd);
Linus Walleij7783b9b2007-09-22 22:10:44 +0000109static char *get_iso8601_stamp(void);
Linus Walleij9901e222006-11-30 12:28:19 +0000110static char *get_string_from_object(LIBMTP_mtpdevice_t *device, uint32_t const object_id,
111 uint16_t const attribute_id);
Richard Lowc3f5fc32007-07-29 09:40:50 +0000112static uint64_t get_u64_from_object(LIBMTP_mtpdevice_t *device,uint32_t const object_id,
113 uint16_t const attribute_id, uint64_t const value_default);
Linus Walleij9901e222006-11-30 12:28:19 +0000114static uint32_t get_u32_from_object(LIBMTP_mtpdevice_t *device,uint32_t const object_id,
115 uint16_t const attribute_id, uint32_t const value_default);
116static uint16_t get_u16_from_object(LIBMTP_mtpdevice_t *device, uint32_t const object_id,
117 uint16_t const attribute_id, uint16_t const value_default);
118static uint8_t get_u8_from_object(LIBMTP_mtpdevice_t *device, uint32_t const object_id,
119 uint16_t const attribute_id, uint8_t const value_default);
120static int set_object_string(LIBMTP_mtpdevice_t *device, uint32_t const object_id,
121 uint16_t const attribute_id, char const * const string);
122static int set_object_u32(LIBMTP_mtpdevice_t *device, uint32_t const object_id,
123 uint16_t const attribute_id, uint32_t const value);
124static int set_object_u16(LIBMTP_mtpdevice_t *device, uint32_t const object_id,
125 uint16_t const attribute_id, uint16_t const value);
126static int set_object_u8(LIBMTP_mtpdevice_t *device, uint32_t const object_id,
127 uint16_t const attribute_id, uint8_t const value);
Linus Walleij8ab54262006-06-21 07:12:28 +0000128static void get_track_metadata(LIBMTP_mtpdevice_t *device, uint16_t objectformat,
129 LIBMTP_track_t *track);
Linus Walleij4d0deea2007-10-19 21:27:48 +0000130static LIBMTP_folder_t *get_subfolders_for_folder(PTPParams *params, uint32_t parent);
Linus Walleij7beba572006-11-29 08:56:12 +0000131static int create_new_abstract_list(LIBMTP_mtpdevice_t *device,
132 char const * const name,
Linus Walleijadce4a52007-04-23 07:28:11 +0000133 char const * const artist,
Linus Walleij31b74292008-05-02 23:29:06 +0000134 char const * const composer,
Linus Walleijadce4a52007-04-23 07:28:11 +0000135 char const * const genre,
Linus Walleij7beba572006-11-29 08:56:12 +0000136 uint32_t const parenthandle,
Linus Walleijea68f1f2008-06-22 21:54:44 +0000137 uint32_t const storageid,
Linus Walleij7beba572006-11-29 08:56:12 +0000138 uint16_t const objectformat,
139 char const * const suffix,
140 uint32_t * const newid,
141 uint32_t const * const tracks,
142 uint32_t const no_tracks);
Linus Walleij7783b9b2007-09-22 22:10:44 +0000143static int update_abstract_list(LIBMTP_mtpdevice_t *device,
144 char const * const name,
145 char const * const artist,
Linus Walleij31b74292008-05-02 23:29:06 +0000146 char const * const composer,
Linus Walleij7783b9b2007-09-22 22:10:44 +0000147 char const * const genre,
148 uint32_t const objecthandle,
149 uint16_t const objectformat,
150 uint32_t const * const tracks,
151 uint32_t const no_tracks);
Linus Walleija6d0d482007-10-31 08:54:56 +0000152static void add_object_to_cache(LIBMTP_mtpdevice_t *device, uint32_t object_id);
Linus Walleij7752b952007-10-19 20:11:46 +0000153static void update_metadata_cache(LIBMTP_mtpdevice_t *device, uint32_t object_id);
raveloxd9a28642006-05-26 23:42:22 +0000154
Linus Walleijcf42f452006-11-28 08:32:49 +0000155/**
156 * Create a new file mapping entry
157 * @return a newly allocated filemapping entry.
158 */
159static filemap_t *new_filemap_entry()
raveloxd9a28642006-05-26 23:42:22 +0000160{
Linus Walleijcf42f452006-11-28 08:32:49 +0000161 filemap_t *filemap;
mopoke96143402006-10-30 04:37:26 +0000162
Linus Walleijcf42f452006-11-28 08:32:49 +0000163 filemap = (filemap_t *)malloc(sizeof(filemap_t));
mopoke96143402006-10-30 04:37:26 +0000164
Linus Walleijf67bca92006-05-29 09:33:39 +0000165 if( filemap != NULL ) {
166 filemap->description = NULL;
167 filemap->id = LIBMTP_FILETYPE_UNKNOWN;
168 filemap->ptp_id = PTP_OFC_Undefined;
Linus Walleijf67bca92006-05-29 09:33:39 +0000169 filemap->next = NULL;
170 }
mopoke96143402006-10-30 04:37:26 +0000171
Linus Walleijf67bca92006-05-29 09:33:39 +0000172 return filemap;
raveloxd9a28642006-05-26 23:42:22 +0000173}
174
raveloxd9a28642006-05-26 23:42:22 +0000175/**
176 * Register an MTP or PTP filetype for data retrieval
177 *
178 * @param description Text description of filetype
Linus Walleijf0f3d482006-05-29 14:10:21 +0000179 * @param id libmtp internal filetype id
raveloxd9a28642006-05-26 23:42:22 +0000180 * @param ptp_id PTP filetype id
Linus Walleijf0f3d482006-05-29 14:10:21 +0000181 * @return 0 for success any other value means error.
raveloxd9a28642006-05-26 23:42:22 +0000182*/
Linus Walleijcf42f452006-11-28 08:32:49 +0000183static int register_filetype(char const * const description, LIBMTP_filetype_t const id,
184 uint16_t const ptp_id)
raveloxd9a28642006-05-26 23:42:22 +0000185{
Linus Walleijcf42f452006-11-28 08:32:49 +0000186 filemap_t *new = NULL, *current;
raveloxd9a28642006-05-26 23:42:22 +0000187
188 // Has this LIBMTP filetype been registered before ?
189 current = filemap;
190 while (current != NULL) {
Linus Walleijf67bca92006-05-29 09:33:39 +0000191 if(current->id == id) {
192 break;
193 }
Linus Walleijf0f3d482006-05-29 14:10:21 +0000194 current = current->next;
raveloxd9a28642006-05-26 23:42:22 +0000195 }
mopoke96143402006-10-30 04:37:26 +0000196
raveloxd9a28642006-05-26 23:42:22 +0000197 // Create the entry
198 if(current == NULL) {
Linus Walleijf67bca92006-05-29 09:33:39 +0000199 new = new_filemap_entry();
Linus Walleijf0f3d482006-05-29 14:10:21 +0000200 if(new == NULL) {
201 return 1;
202 }
mopoke96143402006-10-30 04:37:26 +0000203
Linus Walleijf67bca92006-05-29 09:33:39 +0000204 new->id = id;
Linus Walleij438bd7f2006-06-08 11:35:44 +0000205 if(description != NULL) {
206 new->description = strdup(description);
207 }
Linus Walleijf67bca92006-05-29 09:33:39 +0000208 new->ptp_id = ptp_id;
mopoke96143402006-10-30 04:37:26 +0000209
Linus Walleijf67bca92006-05-29 09:33:39 +0000210 // Add the entry to the list
211 if(filemap == NULL) {
212 filemap = new;
213 } else {
214 current = filemap;
215 while (current->next != NULL ) current=current->next;
216 current->next = new;
217 }
218 // Update the existing entry
raveloxd9a28642006-05-26 23:42:22 +0000219 } else {
Linus Walleij438bd7f2006-06-08 11:35:44 +0000220 if (current->description != NULL) {
221 free(current->description);
222 }
Linus Walleijf67bca92006-05-29 09:33:39 +0000223 current->description = NULL;
Linus Walleijf0f3d482006-05-29 14:10:21 +0000224 if(description != NULL) {
Linus Walleij438bd7f2006-06-08 11:35:44 +0000225 current->description = strdup(description);
Linus Walleijf0f3d482006-05-29 14:10:21 +0000226 }
Linus Walleijf67bca92006-05-29 09:33:39 +0000227 current->ptp_id = ptp_id;
raveloxd9a28642006-05-26 23:42:22 +0000228 }
229
Linus Walleijf0f3d482006-05-29 14:10:21 +0000230 return 0;
raveloxd9a28642006-05-26 23:42:22 +0000231}
232
raveloxd9a28642006-05-26 23:42:22 +0000233static void init_filemap()
234{
Linus Walleij5fb47132006-12-30 15:35:48 +0000235 register_filetype("MediaCard", LIBMTP_FILETYPE_MEDIACARD, PTP_OFC_MTP_MediaCard);
Linus Walleijcf42f452006-11-28 08:32:49 +0000236 register_filetype("RIFF WAVE file", LIBMTP_FILETYPE_WAV, PTP_OFC_WAV);
Linus Walleij5fb47132006-12-30 15:35:48 +0000237 register_filetype("ISO MPEG-1 Audio Layer 3", LIBMTP_FILETYPE_MP3, PTP_OFC_MP3);
238 register_filetype("ISO MPEG-1 Audio Layer 2", LIBMTP_FILETYPE_MP2, PTP_OFC_MTP_MP2);
Linus Walleijcf42f452006-11-28 08:32:49 +0000239 register_filetype("Microsoft Windows Media Audio", LIBMTP_FILETYPE_WMA, PTP_OFC_MTP_WMA);
240 register_filetype("Ogg container format", LIBMTP_FILETYPE_OGG, PTP_OFC_MTP_OGG);
Linus Walleij5fb47132006-12-30 15:35:48 +0000241 register_filetype("Free Lossless Audio Codec (FLAC)", LIBMTP_FILETYPE_FLAC, PTP_OFC_MTP_FLAC);
242 register_filetype("Advanced Audio Coding (AAC)/MPEG-2 Part 7/MPEG-4 Part 3", LIBMTP_FILETYPE_AAC, PTP_OFC_MTP_AAC);
243 register_filetype("MPEG-4 Part 14 Container Format (Audio Empahsis)", LIBMTP_FILETYPE_M4A, PTP_OFC_MTP_M4A);
244 register_filetype("MPEG-4 Part 14 Container Format (Audio+Video Empahsis)", LIBMTP_FILETYPE_MP4, PTP_OFC_MTP_MP4);
Linus Walleijcf42f452006-11-28 08:32:49 +0000245 register_filetype("Audible.com Audio Codec", LIBMTP_FILETYPE_AUDIBLE, PTP_OFC_MTP_AudibleCodec);
Linus Walleijcf42f452006-11-28 08:32:49 +0000246 register_filetype("Undefined audio file", LIBMTP_FILETYPE_UNDEF_AUDIO, PTP_OFC_MTP_UndefinedAudio);
247 register_filetype("Microsoft Windows Media Video", LIBMTP_FILETYPE_WMV, PTP_OFC_MTP_WMV);
248 register_filetype("Audio Video Interleave", LIBMTP_FILETYPE_AVI, PTP_OFC_AVI);
249 register_filetype("MPEG video stream", LIBMTP_FILETYPE_MPEG, PTP_OFC_MPEG);
250 register_filetype("Microsoft Advanced Systems Format", LIBMTP_FILETYPE_ASF, PTP_OFC_ASF);
251 register_filetype("Apple Quicktime container format", LIBMTP_FILETYPE_QT, PTP_OFC_QT);
252 register_filetype("Undefined video file", LIBMTP_FILETYPE_UNDEF_VIDEO, PTP_OFC_MTP_UndefinedVideo);
253 register_filetype("JPEG file", LIBMTP_FILETYPE_JPEG, PTP_OFC_EXIF_JPEG);
Linus Walleij5fb47132006-12-30 15:35:48 +0000254 register_filetype("JP2 file", LIBMTP_FILETYPE_JP2, PTP_OFC_JP2);
255 register_filetype("JPX file", LIBMTP_FILETYPE_JPX, PTP_OFC_JPX);
Linus Walleijcf42f452006-11-28 08:32:49 +0000256 register_filetype("JFIF file", LIBMTP_FILETYPE_JFIF, PTP_OFC_JFIF);
257 register_filetype("TIFF bitmap file", LIBMTP_FILETYPE_TIFF, PTP_OFC_TIFF);
258 register_filetype("BMP bitmap file", LIBMTP_FILETYPE_BMP, PTP_OFC_BMP);
259 register_filetype("GIF bitmap file", LIBMTP_FILETYPE_GIF, PTP_OFC_GIF);
260 register_filetype("PICT bitmap file", LIBMTP_FILETYPE_PICT, PTP_OFC_PICT);
261 register_filetype("Portable Network Graphics", LIBMTP_FILETYPE_PNG, PTP_OFC_PNG);
262 register_filetype("Microsoft Windows Image Format", LIBMTP_FILETYPE_WINDOWSIMAGEFORMAT, PTP_OFC_MTP_WindowsImageFormat);
263 register_filetype("VCalendar version 1", LIBMTP_FILETYPE_VCALENDAR1, PTP_OFC_MTP_vCalendar1);
264 register_filetype("VCalendar version 2", LIBMTP_FILETYPE_VCALENDAR2, PTP_OFC_MTP_vCalendar2);
265 register_filetype("VCard version 2", LIBMTP_FILETYPE_VCARD2, PTP_OFC_MTP_vCard2);
266 register_filetype("VCard version 3", LIBMTP_FILETYPE_VCARD3, PTP_OFC_MTP_vCard3);
267 register_filetype("Undefined Windows executable file", LIBMTP_FILETYPE_WINEXEC, PTP_OFC_MTP_UndefinedWindowsExecutable);
268 register_filetype("Text file", LIBMTP_FILETYPE_TEXT, PTP_OFC_Text);
269 register_filetype("HTML file", LIBMTP_FILETYPE_HTML, PTP_OFC_HTML);
Linus Walleij5fb47132006-12-30 15:35:48 +0000270 register_filetype("XML file", LIBMTP_FILETYPE_XML, PTP_OFC_MTP_XMLDocument);
271 register_filetype("DOC file", LIBMTP_FILETYPE_DOC, PTP_OFC_MTP_MSWordDocument);
272 register_filetype("XLS file", LIBMTP_FILETYPE_XLS, PTP_OFC_MTP_MSExcelSpreadsheetXLS);
273 register_filetype("PPT file", LIBMTP_FILETYPE_PPT, PTP_OFC_MTP_MSPowerpointPresentationPPT);
274 register_filetype("MHT file", LIBMTP_FILETYPE_MHT, PTP_OFC_MTP_MHTCompiledHTMLDocument);
Linus Walleija05b9802006-12-08 08:50:30 +0000275 register_filetype("Firmware file", LIBMTP_FILETYPE_FIRMWARE, PTP_OFC_MTP_Firmware);
Linus Walleijcf42f452006-11-28 08:32:49 +0000276 register_filetype("Undefined filetype", LIBMTP_FILETYPE_UNKNOWN, PTP_OFC_Undefined);
raveloxd9a28642006-05-26 23:42:22 +0000277}
Linus Walleij16c51f02006-05-04 13:20:22 +0000278
Linus Walleij16c51f02006-05-04 13:20:22 +0000279/**
280 * Returns the PTP filetype that maps to a certain libmtp internal file type.
281 * @param intype the MTP library interface type
282 * @return the PTP (libgphoto2) interface type
283 */
284static uint16_t map_libmtp_type_to_ptp_type(LIBMTP_filetype_t intype)
285{
Linus Walleijcf42f452006-11-28 08:32:49 +0000286 filemap_t *current;
Linus Walleij16c51f02006-05-04 13:20:22 +0000287
raveloxd9a28642006-05-26 23:42:22 +0000288 current = filemap;
289
290 while (current != NULL) {
Linus Walleijf0f3d482006-05-29 14:10:21 +0000291 if(current->id == intype) {
292 return current->ptp_id;
293 }
294 current = current->next;
Linus Walleij16c51f02006-05-04 13:20:22 +0000295 }
Linus Walleij1fd2d272006-05-08 09:22:01 +0000296 // printf("map_libmtp_type_to_ptp_type: unknown filetype.\n");
Linus Walleij16c51f02006-05-04 13:20:22 +0000297 return PTP_OFC_Undefined;
298}
299
300
301/**
Linus Walleij31b74292008-05-02 23:29:06 +0000302 * Returns the MTP internal interface type that maps to a certain ptp
303 * interface type.
mopoke96143402006-10-30 04:37:26 +0000304 * @param intype the PTP (libgphoto2) interface type
Linus Walleij8ab54262006-06-21 07:12:28 +0000305 * @return the MTP library interface type
Linus Walleij16c51f02006-05-04 13:20:22 +0000306 */
307static LIBMTP_filetype_t map_ptp_type_to_libmtp_type(uint16_t intype)
308{
Linus Walleijcf42f452006-11-28 08:32:49 +0000309 filemap_t *current;
Linus Walleij16c51f02006-05-04 13:20:22 +0000310
raveloxd9a28642006-05-26 23:42:22 +0000311 current = filemap;
312
313 while (current != NULL) {
Linus Walleijf0f3d482006-05-29 14:10:21 +0000314 if(current->ptp_id == intype) {
315 return current->id;
316 }
317 current = current->next;
Linus Walleij16c51f02006-05-04 13:20:22 +0000318 }
Linus Walleij1fd2d272006-05-08 09:22:01 +0000319 // printf("map_ptp_type_to_libmtp_type: unknown filetype.\n");
Linus Walleij16c51f02006-05-04 13:20:22 +0000320 return LIBMTP_FILETYPE_UNKNOWN;
321}
322
Linus Walleijfa1374c2006-02-27 07:41:46 +0000323
Linus Walleij6946ac52006-03-21 06:51:22 +0000324/**
Linus Walleijf0f3d482006-05-29 14:10:21 +0000325 * Initialize the library. You are only supposed to call this
326 * one, before using the library for the first time in a program.
327 * Never re-initialize libmtp!
328 *
raveloxd9a28642006-05-26 23:42:22 +0000329 * The only thing this does at the moment is to initialise the
330 * filetype mapping table.
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000331 */
332void LIBMTP_Init(void)
333{
raveloxd9a28642006-05-26 23:42:22 +0000334 init_filemap();
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000335 return;
336}
337
Linus Walleijcf42f452006-11-28 08:32:49 +0000338
339/**
340 * This helper function returns a textual description for a libmtp
341 * file type to be used in dialog boxes etc.
342 * @param intype the libmtp internal filetype to get a description for.
343 * @return a string representing the filetype, this must <b>NOT</b>
344 * be free():ed by the caller!
345 */
346char const * LIBMTP_Get_Filetype_Description(LIBMTP_filetype_t intype)
347{
348 filemap_t *current;
349
350 current = filemap;
351
352 while (current != NULL) {
353 if(current->id == intype) {
354 return current->description;
355 }
356 current = current->next;
357 }
358
359 return "Unknown filetype";
360}
361
Linus Walleij7783b9b2007-09-22 22:10:44 +0000362/**
363 * This function will do its best to fit a 16bit
364 * value into a PTP object property if the property
365 * is limited in range or step sizes.
366 */
Linus Walleij29559562007-08-22 21:38:04 +0000367static uint16_t adjust_u16(uint16_t val, PTPObjectPropDesc *opd)
368{
369 switch (opd->FormFlag) {
370 case PTP_DPFF_Range:
371 if (val < opd->FORM.Range.MinimumValue.u16) {
372 return opd->FORM.Range.MinimumValue.u16;
373 }
374 if (val > opd->FORM.Range.MaximumValue.u16) {
375 return opd->FORM.Range.MaximumValue.u16;
376 }
377 // Round down to last step.
378 if (val % opd->FORM.Range.StepSize.u16 != 0) {
379 return val - (val % opd->FORM.Range.StepSize.u16);
380 }
381 return val;
382 break;
383 case PTP_DPFF_Enumeration:
384 {
385 int i;
386 uint16_t bestfit = opd->FORM.Enum.SupportedValue[0].u16;
387
388 for (i=0; i<opd->FORM.Enum.NumberOfValues; i++) {
389 if (val == opd->FORM.Enum.SupportedValue[i].u16) {
390 return val;
391 }
392 // Rough guess of best fit
393 if (opd->FORM.Enum.SupportedValue[i].u16 < val) {
394 bestfit = opd->FORM.Enum.SupportedValue[i].u16;
395 }
396 }
397 // Just some default that'll work.
398 return bestfit;
399 }
400 default:
401 // Will accept any value
402 break;
403 }
404 return val;
405}
406
Linus Walleij7783b9b2007-09-22 22:10:44 +0000407/**
408 * This function will do its best to fit a 32bit
409 * value into a PTP object property if the property
410 * is limited in range or step sizes.
411 */
Linus Walleij29559562007-08-22 21:38:04 +0000412static uint32_t adjust_u32(uint32_t val, PTPObjectPropDesc *opd)
413{
414 switch (opd->FormFlag) {
415 case PTP_DPFF_Range:
416 if (val < opd->FORM.Range.MinimumValue.u32) {
417 return opd->FORM.Range.MinimumValue.u32;
418 }
419 if (val > opd->FORM.Range.MaximumValue.u32) {
420 return opd->FORM.Range.MaximumValue.u32;
421 }
422 // Round down to last step.
423 if (val % opd->FORM.Range.StepSize.u32 != 0) {
424 return val - (val % opd->FORM.Range.StepSize.u32);
425 }
426 return val;
427 break;
428 case PTP_DPFF_Enumeration:
429 {
430 int i;
431 uint32_t bestfit = opd->FORM.Enum.SupportedValue[0].u32;
432
433 for (i=0; i<opd->FORM.Enum.NumberOfValues; i++) {
434 if (val == opd->FORM.Enum.SupportedValue[i].u32) {
435 return val;
436 }
437 // Rough guess of best fit
438 if (opd->FORM.Enum.SupportedValue[i].u32 < val) {
439 bestfit = opd->FORM.Enum.SupportedValue[i].u32;
440 }
441 }
442 // Just some default that'll work.
443 return bestfit;
444 }
445 default:
446 // Will accept any value
447 break;
448 }
449 return val;
450}
451
Linus Walleij7783b9b2007-09-22 22:10:44 +0000452/**
Linus Walleij7783b9b2007-09-22 22:10:44 +0000453 * This function returns a newly created ISO 8601 timestamp with the
454 * current time in as high precision as possible. It even adds
455 * the time zone if it can.
456 */
457static char *get_iso8601_stamp(void)
458{
459 time_t curtime;
460 struct tm *loctime;
461 char tmp[64];
462
463 curtime = time(NULL);
464 loctime = localtime(&curtime);
465 strftime (tmp, sizeof(tmp), "%Y%m%dT%H%M%S.0%z", loctime);
466 return strdup(tmp);
467}
468
469/**
raveloxd9a28642006-05-26 23:42:22 +0000470 * Retrieves a string from an object
471 *
472 * @param device a pointer to an MTP device.
473 * @param object_id Object reference
474 * @param attribute_id PTP attribute ID
Linus Walleij438bd7f2006-06-08 11:35:44 +0000475 * @return valid string or NULL on failure. The returned string
476 * must bee <code>free()</code>:ed by the caller after
477 * use.
raveloxd9a28642006-05-26 23:42:22 +0000478 */
Linus Walleij9901e222006-11-30 12:28:19 +0000479static char *get_string_from_object(LIBMTP_mtpdevice_t *device, uint32_t const object_id,
Linus Walleij4ef39e62006-09-19 14:11:52 +0000480 uint16_t const attribute_id)
raveloxd9a28642006-05-26 23:42:22 +0000481{
482 PTPPropertyValue propval;
483 char *retstring = NULL;
Linus Walleijf0f3d482006-05-29 14:10:21 +0000484 PTPParams *params = (PTPParams *) device->params;
Linus Walleij438bd7f2006-06-08 11:35:44 +0000485 uint16_t ret;
mopoke96143402006-10-30 04:37:26 +0000486
Linus Walleij438bd7f2006-06-08 11:35:44 +0000487 if ( device == NULL || object_id == 0) {
Linus Walleijf0f3d482006-05-29 14:10:21 +0000488 return NULL;
489 }
Linus Walleij338ade42007-07-03 20:44:08 +0000490
491 // This O(n) search should not be used so often, since code
492 // using the cached properties don't usually call this function.
Linus Walleij1e9a0332007-09-12 19:35:56 +0000493 if (params->props) {
Linus Walleija6d0d482007-10-31 08:54:56 +0000494 MTPProperties *prop = ptp_find_object_prop_in_cache(params, object_id, attribute_id);
Linus Walleij1e9a0332007-09-12 19:35:56 +0000495 if (prop) {
Richard Low0fa83522007-07-17 20:05:08 +0000496 if (prop->propval.str != NULL)
497 return strdup(prop->propval.str);
498 else
499 return NULL;
Linus Walleij338ade42007-07-03 20:44:08 +0000500 }
501 }
mopoke96143402006-10-30 04:37:26 +0000502
Linus Walleija823a702006-08-27 21:27:46 +0000503 ret = ptp_mtp_getobjectpropvalue(params, object_id, attribute_id, &propval, PTP_DTC_STR);
raveloxd9a28642006-05-26 23:42:22 +0000504 if (ret == PTP_RC_OK) {
Linus Walleija823a702006-08-27 21:27:46 +0000505 if (propval.str != NULL) {
506 retstring = (char *) strdup(propval.str);
507 free(propval.str);
raveloxd9a28642006-05-26 23:42:22 +0000508 }
Linus Walleij070e9b42007-01-22 08:49:28 +0000509 } else {
510 add_ptp_error_to_errorstack(device, ret, "get_string_from_object(): could not get object string.");
raveloxd9a28642006-05-26 23:42:22 +0000511 }
mopoke96143402006-10-30 04:37:26 +0000512
raveloxd9a28642006-05-26 23:42:22 +0000513 return retstring;
514}
515
516/**
Richard Lowc3f5fc32007-07-29 09:40:50 +0000517* Retrieves an unsigned 64-bit integer from an object attribute
518 *
519 * @param device a pointer to an MTP device.
520 * @param object_id Object reference
521 * @param attribute_id PTP attribute ID
522 * @param value_default Default value to return on failure
523 * @return the value
524 */
525static uint64_t get_u64_from_object(LIBMTP_mtpdevice_t *device,uint32_t const object_id,
526 uint16_t const attribute_id, uint64_t const value_default)
527{
528 PTPPropertyValue propval;
529 uint64_t retval = value_default;
530 PTPParams *params = (PTPParams *) device->params;
531 uint16_t ret;
532
533 if ( device == NULL ) {
534 return value_default;
535 }
536
537 // This O(n) search should not be used so often, since code
538 // using the cached properties don't usually call this function.
Linus Walleij1e9a0332007-09-12 19:35:56 +0000539 if (params->props) {
Linus Walleija6d0d482007-10-31 08:54:56 +0000540 MTPProperties *prop = ptp_find_object_prop_in_cache(params, object_id, attribute_id);
Linus Walleij1e9a0332007-09-12 19:35:56 +0000541 if (prop)
542 return prop->propval.u64;
Richard Lowc3f5fc32007-07-29 09:40:50 +0000543 }
Linus Walleij1e9a0332007-09-12 19:35:56 +0000544
Richard Lowc3f5fc32007-07-29 09:40:50 +0000545 ret = ptp_mtp_getobjectpropvalue(params, object_id,
546 attribute_id,
547 &propval,
548 PTP_DTC_UINT64);
549 if (ret == PTP_RC_OK) {
550 retval = propval.u64;
551 } else {
552 add_ptp_error_to_errorstack(device, ret, "get_u64_from_object(): could not get unsigned 64bit integer from object.");
553 }
554
555 return retval;
556}
557
558/**
raveloxd9a28642006-05-26 23:42:22 +0000559 * Retrieves an unsigned 32-bit integer from an object attribute
560 *
561 * @param device a pointer to an MTP device.
562 * @param object_id Object reference
563 * @param attribute_id PTP attribute ID
Linus Walleij5acfa742006-05-29 14:51:59 +0000564 * @param value_default Default value to return on failure
Linus Walleijf0f3d482006-05-29 14:10:21 +0000565 * @return the value
raveloxd9a28642006-05-26 23:42:22 +0000566 */
Linus Walleij9901e222006-11-30 12:28:19 +0000567static uint32_t get_u32_from_object(LIBMTP_mtpdevice_t *device,uint32_t const object_id,
Linus Walleij4ef39e62006-09-19 14:11:52 +0000568 uint16_t const attribute_id, uint32_t const value_default)
raveloxd9a28642006-05-26 23:42:22 +0000569{
570 PTPPropertyValue propval;
571 uint32_t retval = value_default;
Linus Walleijf0f3d482006-05-29 14:10:21 +0000572 PTPParams *params = (PTPParams *) device->params;
Linus Walleij438bd7f2006-06-08 11:35:44 +0000573 uint16_t ret;
mopoke96143402006-10-30 04:37:26 +0000574
Linus Walleijf0f3d482006-05-29 14:10:21 +0000575 if ( device == NULL ) {
576 return value_default;
577 }
raveloxd9a28642006-05-26 23:42:22 +0000578
Linus Walleij338ade42007-07-03 20:44:08 +0000579 // This O(n) search should not be used so often, since code
580 // using the cached properties don't usually call this function.
Linus Walleij1e9a0332007-09-12 19:35:56 +0000581 if (params->props) {
Linus Walleija6d0d482007-10-31 08:54:56 +0000582 MTPProperties *prop = ptp_find_object_prop_in_cache(params, object_id, attribute_id);
Linus Walleij1e9a0332007-09-12 19:35:56 +0000583 if (prop)
584 return prop->propval.u32;
Linus Walleij338ade42007-07-03 20:44:08 +0000585 }
586
raveloxd9a28642006-05-26 23:42:22 +0000587 ret = ptp_mtp_getobjectpropvalue(params, object_id,
588 attribute_id,
589 &propval,
590 PTP_DTC_UINT32);
591 if (ret == PTP_RC_OK) {
592 retval = propval.u32;
Linus Walleij070e9b42007-01-22 08:49:28 +0000593 } else {
594 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 +0000595 }
596
597 return retval;
598}
599
600/**
601 * Retrieves an unsigned 16-bit integer from an object attribute
602 *
603 * @param device a pointer to an MTP device.
604 * @param object_id Object reference
605 * @param attribute_id PTP attribute ID
Linus Walleij5acfa742006-05-29 14:51:59 +0000606 * @param value_default Default value to return on failure
Linus Walleijf0f3d482006-05-29 14:10:21 +0000607 * @return a value
raveloxd9a28642006-05-26 23:42:22 +0000608 */
Linus Walleij9901e222006-11-30 12:28:19 +0000609static uint16_t get_u16_from_object(LIBMTP_mtpdevice_t *device, uint32_t const object_id,
Linus Walleij4ef39e62006-09-19 14:11:52 +0000610 uint16_t const attribute_id, uint16_t const value_default)
raveloxd9a28642006-05-26 23:42:22 +0000611{
612 PTPPropertyValue propval;
613 uint16_t retval = value_default;
Linus Walleijf0f3d482006-05-29 14:10:21 +0000614 PTPParams *params = (PTPParams *) device->params;
Linus Walleij438bd7f2006-06-08 11:35:44 +0000615 uint16_t ret;
raveloxd9a28642006-05-26 23:42:22 +0000616
Linus Walleijf0f3d482006-05-29 14:10:21 +0000617 if ( device == NULL ) {
618 return value_default;
619 }
raveloxd9a28642006-05-26 23:42:22 +0000620
Linus Walleij338ade42007-07-03 20:44:08 +0000621 // This O(n) search should not be used so often, since code
622 // using the cached properties don't usually call this function.
Linus Walleij1e9a0332007-09-12 19:35:56 +0000623 if (params->props) {
Linus Walleija6d0d482007-10-31 08:54:56 +0000624 MTPProperties *prop = ptp_find_object_prop_in_cache(params, object_id, attribute_id);
Linus Walleij1e9a0332007-09-12 19:35:56 +0000625 if (prop)
626 return prop->propval.u16;
Linus Walleij338ade42007-07-03 20:44:08 +0000627 }
628
raveloxd9a28642006-05-26 23:42:22 +0000629 ret = ptp_mtp_getobjectpropvalue(params, object_id,
630 attribute_id,
631 &propval,
632 PTP_DTC_UINT16);
633 if (ret == PTP_RC_OK) {
634 retval = propval.u16;
Linus Walleij070e9b42007-01-22 08:49:28 +0000635 } else {
636 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 +0000637 }
mopoke96143402006-10-30 04:37:26 +0000638
raveloxd9a28642006-05-26 23:42:22 +0000639 return retval;
640}
641
642/**
Linus Walleij99310d42006-11-01 08:29:39 +0000643 * Retrieves an unsigned 8-bit integer from an object attribute
644 *
645 * @param device a pointer to an MTP device.
646 * @param object_id Object reference
647 * @param attribute_id PTP attribute ID
648 * @param value_default Default value to return on failure
649 * @return a value
650 */
Linus Walleij9901e222006-11-30 12:28:19 +0000651static uint8_t get_u8_from_object(LIBMTP_mtpdevice_t *device, uint32_t const object_id,
Linus Walleij99310d42006-11-01 08:29:39 +0000652 uint16_t const attribute_id, uint8_t const value_default)
653{
654 PTPPropertyValue propval;
655 uint8_t retval = value_default;
656 PTPParams *params = (PTPParams *) device->params;
657 uint16_t ret;
658
659 if ( device == NULL ) {
660 return value_default;
661 }
662
Linus Walleij338ade42007-07-03 20:44:08 +0000663 // This O(n) search should not be used so often, since code
664 // using the cached properties don't usually call this function.
Linus Walleij1e9a0332007-09-12 19:35:56 +0000665 if (params->props) {
Linus Walleija6d0d482007-10-31 08:54:56 +0000666 MTPProperties *prop = ptp_find_object_prop_in_cache(params, object_id, attribute_id);
Linus Walleij1e9a0332007-09-12 19:35:56 +0000667 if (prop)
668 return prop->propval.u8;
Linus Walleij338ade42007-07-03 20:44:08 +0000669 }
670
Linus Walleij99310d42006-11-01 08:29:39 +0000671 ret = ptp_mtp_getobjectpropvalue(params, object_id,
672 attribute_id,
673 &propval,
674 PTP_DTC_UINT8);
675 if (ret == PTP_RC_OK) {
676 retval = propval.u8;
Linus Walleij070e9b42007-01-22 08:49:28 +0000677 } else {
678 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 +0000679 }
680
681 return retval;
682}
683
684/**
raveloxd9a28642006-05-26 23:42:22 +0000685 * Sets an object attribute from a string
686 *
687 * @param device a pointer to an MTP device.
688 * @param object_id Object reference
689 * @param attribute_id PTP attribute ID
690 * @param string string value to set
Linus Walleijf0f3d482006-05-29 14:10:21 +0000691 * @return 0 on success, any other value means failure
raveloxd9a28642006-05-26 23:42:22 +0000692 */
Linus Walleij9901e222006-11-30 12:28:19 +0000693static int set_object_string(LIBMTP_mtpdevice_t *device, uint32_t const object_id,
Linus Walleij4ef39e62006-09-19 14:11:52 +0000694 uint16_t const attribute_id, char const * const string)
raveloxd9a28642006-05-26 23:42:22 +0000695{
696 PTPPropertyValue propval;
Linus Walleijf0f3d482006-05-29 14:10:21 +0000697 PTPParams *params = (PTPParams *) device->params;
Linus Walleij438bd7f2006-06-08 11:35:44 +0000698 uint16_t ret;
raveloxd9a28642006-05-26 23:42:22 +0000699
Linus Walleijf0f3d482006-05-29 14:10:21 +0000700 if (device == NULL || string == NULL) {
Linus Walleij438bd7f2006-06-08 11:35:44 +0000701 return -1;
Linus Walleijf0f3d482006-05-29 14:10:21 +0000702 }
Linus Walleijb2753182007-02-26 08:11:38 +0000703
704 if (!ptp_operation_issupported(params,PTP_OC_MTP_SetObjectPropValue)) {
705 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "set_object_string(): could not set object string: "
706 "PTP_OC_MTP_SetObjectPropValue not supported.");
707 return -1;
708 }
Linus Walleija823a702006-08-27 21:27:46 +0000709 propval.str = (char *) string;
710 ret = ptp_mtp_setobjectpropvalue(params, object_id, attribute_id, &propval, PTP_DTC_STR);
Linus Walleijf0f3d482006-05-29 14:10:21 +0000711 if (ret != PTP_RC_OK) {
Linus Walleij070e9b42007-01-22 08:49:28 +0000712 add_ptp_error_to_errorstack(device, ret, "set_object_string(): could not set object string.");
Linus Walleij438bd7f2006-06-08 11:35:44 +0000713 return -1;
Linus Walleijf0f3d482006-05-29 14:10:21 +0000714 }
mopoke96143402006-10-30 04:37:26 +0000715
Linus Walleijf0f3d482006-05-29 14:10:21 +0000716 return 0;
raveloxd9a28642006-05-26 23:42:22 +0000717}
718
Linus Walleij29559562007-08-22 21:38:04 +0000719
raveloxd9a28642006-05-26 23:42:22 +0000720/**
721 * Sets an object attribute from an unsigned 32-bit integer
722 *
723 * @param device a pointer to an MTP device.
724 * @param object_id Object reference
725 * @param attribute_id PTP attribute ID
726 * @param value 32-bit unsigned integer to set
Linus Walleijf0f3d482006-05-29 14:10:21 +0000727 * @return 0 on success, any other value means failure
raveloxd9a28642006-05-26 23:42:22 +0000728 */
Linus Walleij9901e222006-11-30 12:28:19 +0000729static int set_object_u32(LIBMTP_mtpdevice_t *device, uint32_t const object_id,
Linus Walleij4ef39e62006-09-19 14:11:52 +0000730 uint16_t const attribute_id, uint32_t const value)
raveloxd9a28642006-05-26 23:42:22 +0000731{
732 PTPPropertyValue propval;
Linus Walleijf0f3d482006-05-29 14:10:21 +0000733 PTPParams *params = (PTPParams *) device->params;
Linus Walleij438bd7f2006-06-08 11:35:44 +0000734 uint16_t ret;
raveloxd9a28642006-05-26 23:42:22 +0000735
Linus Walleijf0f3d482006-05-29 14:10:21 +0000736 if (device == NULL) {
Linus Walleij438bd7f2006-06-08 11:35:44 +0000737 return -1;
Linus Walleijf0f3d482006-05-29 14:10:21 +0000738 }
raveloxd9a28642006-05-26 23:42:22 +0000739
Linus Walleijb2753182007-02-26 08:11:38 +0000740 if (!ptp_operation_issupported(params,PTP_OC_MTP_SetObjectPropValue)) {
741 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "set_object_u32(): could not set unsigned 32bit integer property: "
742 "PTP_OC_MTP_SetObjectPropValue not supported.");
743 return -1;
744 }
Linus Walleij29559562007-08-22 21:38:04 +0000745
raveloxd9a28642006-05-26 23:42:22 +0000746 propval.u32 = value;
747 ret = ptp_mtp_setobjectpropvalue(params, object_id, attribute_id, &propval, PTP_DTC_UINT32);
Linus Walleijf0f3d482006-05-29 14:10:21 +0000748 if (ret != PTP_RC_OK) {
Linus Walleij070e9b42007-01-22 08:49:28 +0000749 add_ptp_error_to_errorstack(device, ret, "set_object_u32(): could not set unsigned 32bit integer property.");
Linus Walleij438bd7f2006-06-08 11:35:44 +0000750 return -1;
Linus Walleijf0f3d482006-05-29 14:10:21 +0000751 }
mopoke96143402006-10-30 04:37:26 +0000752
Linus Walleijf0f3d482006-05-29 14:10:21 +0000753 return 0;
raveloxd9a28642006-05-26 23:42:22 +0000754}
755
756/**
757 * Sets an object attribute from an unsigned 16-bit integer
758 *
759 * @param device a pointer to an MTP device.
760 * @param object_id Object reference
761 * @param attribute_id PTP attribute ID
762 * @param value 16-bit unsigned integer to set
Linus Walleijf0f3d482006-05-29 14:10:21 +0000763 * @return 0 on success, any other value means failure
raveloxd9a28642006-05-26 23:42:22 +0000764 */
Linus Walleij9901e222006-11-30 12:28:19 +0000765static int set_object_u16(LIBMTP_mtpdevice_t *device, uint32_t const object_id,
Linus Walleij4ef39e62006-09-19 14:11:52 +0000766 uint16_t const attribute_id, uint16_t const value)
raveloxd9a28642006-05-26 23:42:22 +0000767{
768 PTPPropertyValue propval;
Linus Walleijf0f3d482006-05-29 14:10:21 +0000769 PTPParams *params = (PTPParams *) device->params;
Linus Walleij438bd7f2006-06-08 11:35:44 +0000770 uint16_t ret;
raveloxd9a28642006-05-26 23:42:22 +0000771
Linus Walleijf0f3d482006-05-29 14:10:21 +0000772 if (device == NULL) {
773 return 1;
774 }
raveloxd9a28642006-05-26 23:42:22 +0000775
Linus Walleijb2753182007-02-26 08:11:38 +0000776 if (!ptp_operation_issupported(params,PTP_OC_MTP_SetObjectPropValue)) {
777 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "set_object_u16(): could not set unsigned 16bit integer property: "
778 "PTP_OC_MTP_SetObjectPropValue not supported.");
779 return -1;
780 }
raveloxd9a28642006-05-26 23:42:22 +0000781 propval.u16 = value;
782 ret = ptp_mtp_setobjectpropvalue(params, object_id, attribute_id, &propval, PTP_DTC_UINT16);
Linus Walleijf0f3d482006-05-29 14:10:21 +0000783 if (ret != PTP_RC_OK) {
Linus Walleij070e9b42007-01-22 08:49:28 +0000784 add_ptp_error_to_errorstack(device, ret, "set_object_u16(): could not set unsigned 16bit integer property.");
Linus Walleijf0f3d482006-05-29 14:10:21 +0000785 return 1;
786 }
raveloxd9a28642006-05-26 23:42:22 +0000787
Linus Walleijf0f3d482006-05-29 14:10:21 +0000788 return 0;
raveloxd9a28642006-05-26 23:42:22 +0000789}
790
791/**
Linus Walleij99310d42006-11-01 08:29:39 +0000792 * Sets an object attribute from an unsigned 8-bit integer
793 *
794 * @param device a pointer to an MTP device.
795 * @param object_id Object reference
796 * @param attribute_id PTP attribute ID
797 * @param value 8-bit unsigned integer to set
798 * @return 0 on success, any other value means failure
799 */
Linus Walleij9901e222006-11-30 12:28:19 +0000800static int set_object_u8(LIBMTP_mtpdevice_t *device, uint32_t const object_id,
801 uint16_t const attribute_id, uint8_t const value)
Linus Walleij99310d42006-11-01 08:29:39 +0000802{
803 PTPPropertyValue propval;
804 PTPParams *params = (PTPParams *) device->params;
805 uint16_t ret;
806
807 if (device == NULL) {
808 return 1;
809 }
810
Linus Walleijb2753182007-02-26 08:11:38 +0000811 if (!ptp_operation_issupported(params,PTP_OC_MTP_SetObjectPropValue)) {
812 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "set_object_u8(): could not set unsigned 8bit integer property: "
813 "PTP_OC_MTP_SetObjectPropValue not supported.");
814 return -1;
815 }
Linus Walleij99310d42006-11-01 08:29:39 +0000816 propval.u8 = value;
817 ret = ptp_mtp_setobjectpropvalue(params, object_id, attribute_id, &propval, PTP_DTC_UINT8);
818 if (ret != PTP_RC_OK) {
Linus Walleij070e9b42007-01-22 08:49:28 +0000819 add_ptp_error_to_errorstack(device, ret, "set_object_u8(): could not set unsigned 8bit integer property.");
Linus Walleijf0f3d482006-05-29 14:10:21 +0000820 return 1;
raveloxd9a28642006-05-26 23:42:22 +0000821 }
mopoke96143402006-10-30 04:37:26 +0000822
Linus Walleijf0f3d482006-05-29 14:10:21 +0000823 return 0;
raveloxd9a28642006-05-26 23:42:22 +0000824}
825
raveloxd9a28642006-05-26 23:42:22 +0000826/**
Linus Walleij039d1dd2007-02-23 22:34:07 +0000827 * Get the first (as in "first in the list of") connected MTP device.
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000828 * @return a device pointer.
Linus Walleij039d1dd2007-02-23 22:34:07 +0000829 * @see LIBMTP_Get_Connected_Devices()
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000830 */
Linus Walleijb9256fd2006-02-15 09:40:43 +0000831LIBMTP_mtpdevice_t *LIBMTP_Get_First_Device(void)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000832{
tedbullocke7713642007-02-18 23:10:09 +0000833 LIBMTP_mtpdevice_t *first_device = NULL;
Linus Walleij64691b72008-05-31 22:55:18 +0000834 LIBMTP_raw_device_t *devices;
835 int numdevs;
836 LIBMTP_error_number_t ret;
tedbullocke7713642007-02-18 23:10:09 +0000837
Linus Walleij64691b72008-05-31 22:55:18 +0000838 ret = LIBMTP_Detect_Raw_Devices(&devices, &numdevs);
839 if (ret != LIBMTP_ERROR_NONE) {
Richard Low0f794762007-02-23 22:06:27 +0000840 return NULL;
Linus Walleija8a19cc2007-02-02 22:13:17 +0000841 }
tedbullock44b0ff72007-02-22 22:20:28 +0000842
Linus Walleij64691b72008-05-31 22:55:18 +0000843 if (devices == NULL || numdevs == 0) {
844 return NULL;
tedbullock44b0ff72007-02-22 22:20:28 +0000845 }
Linus Walleij64691b72008-05-31 22:55:18 +0000846
847 first_device = LIBMTP_Open_Raw_Device(&devices[0]);
848 free(devices);
tedbullocke7713642007-02-18 23:10:09 +0000849 return first_device;
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000850}
851
852/**
Linus Walleijbdb89bd2008-05-28 23:32:35 +0000853 * This function opens a device from a raw device. It is the
854 * preferred way to access devices in the new interface where
855 * several devices can come and go as the library is working
856 * on a certain device.
857 * @param rawdevice the raw device to open a "real" device for.
858 * @return an open device.
859 */
860LIBMTP_mtpdevice_t *LIBMTP_Open_Raw_Device(LIBMTP_raw_device_t *rawdevice)
861{
862 LIBMTP_mtpdevice_t *mtp_device;
863 uint8_t bs = 0;
864 PTPParams *current_params;
Linus Walleij3c643252008-05-31 23:22:28 +0000865 PTP_USB *ptp_usb;
Linus Walleijbdb89bd2008-05-28 23:32:35 +0000866 LIBMTP_error_number_t err;
867 int i;
868
869 /* Allocate dynamic space for our device */
870 mtp_device = (LIBMTP_mtpdevice_t *) malloc(sizeof(LIBMTP_mtpdevice_t));
871 /* Check if there was a memory allocation error */
872 if(mtp_device == NULL) {
873 /* There has been an memory allocation error. We are going to ignore this
874 device and attempt to continue */
875
876 /* TODO: This error statement could probably be a bit more robust */
877 fprintf(stderr, "LIBMTP PANIC: connect_usb_devices encountered a memory "
878 "allocation error with device %d on bus %d, trying to continue",
879 rawdevice->devnum, rawdevice->bus_location);
880
881 return NULL;
882 }
Linus Walleij3c643252008-05-31 23:22:28 +0000883
884 /* Create PTP params */
885 current_params = (PTPParams *) malloc(sizeof(PTPParams));
886 if (current_params == NULL) {
887 free(mtp_device);
Linus Walleijbdb89bd2008-05-28 23:32:35 +0000888 return NULL;
Linus Walleij3c643252008-05-31 23:22:28 +0000889 }
890 memset(current_params, 0, sizeof(PTPParams));
891 /* Clear all handlers */
Linus Walleijbdb89bd2008-05-28 23:32:35 +0000892 current_params->handles.Handler = NULL;
893 current_params->objectinfo = NULL;
894 current_params->props = NULL;
Linus Walleij3c643252008-05-31 23:22:28 +0000895 /* TODO: Will this always be little endian? */
896 current_params->byteorder = PTP_DL_LE;
897 current_params->cd_locale_to_ucs2 = iconv_open("UCS-2LE", "UTF-8");
898 current_params->cd_ucs2_to_locale = iconv_open("UTF-8", "UCS-2LE");
899
900 if(current_params->cd_locale_to_ucs2 == (iconv_t) -1 ||
901 current_params->cd_ucs2_to_locale == (iconv_t) -1) {
902 fprintf(stderr, "LIBMTP PANIC: Cannot open iconv() converters to/from UCS-2!\n"
903 "Too old stdlibc, glibc and libiconv?\n");
904 free(current_params);
905 free(mtp_device);
906 return NULL;
907 }
908 mtp_device->params = current_params;
909
910
911 /* Create usbinfo, this also opens the session */
912 err = configure_usb_device(rawdevice,
913 current_params,
914 &mtp_device->usbinfo);
915 if (err != LIBMTP_ERROR_NONE) {
916 free(current_params);
917 free(mtp_device);
918 return NULL;
919 }
920 ptp_usb = (PTP_USB*) mtp_device->usbinfo;
921 /* Set pointer back to params */
922 ptp_usb->params = current_params;
923
Linus Walleijbdb89bd2008-05-28 23:32:35 +0000924
925 /* Cache the device information for later use */
926 if (ptp_getdeviceinfo(current_params,
927 &current_params->deviceinfo) != PTP_RC_OK) {
928 fprintf(stderr, "LIBMTP PANIC: Unable to read device information on device "
929 "%d on bus %d, trying to continue",
930 rawdevice->devnum, rawdevice->bus_location);
931
932 /* Prevent memory leaks for this device */
933 free(mtp_device->usbinfo);
934 free(mtp_device->params);
935 current_params = NULL;
Linus Walleij3c643252008-05-31 23:22:28 +0000936 free(mtp_device);
Linus Walleijbdb89bd2008-05-28 23:32:35 +0000937 return NULL;
938 }
939
940 /* Determine if the object size supported is 32 or 64 bit wide */
941 for (i=0;i<current_params->deviceinfo.ImageFormats_len;i++) {
942 PTPObjectPropDesc opd;
943
944 if (ptp_mtp_getobjectpropdesc(current_params,
945 PTP_OPC_ObjectSize,
946 current_params->deviceinfo.ImageFormats[i],
947 &opd) != PTP_RC_OK) {
948 printf("LIBMTP PANIC: create_usb_mtp_devices(): "
949 "could not inspect object property descriptions!\n");
950 } else {
951 if (opd.DataType == PTP_DTC_UINT32) {
952 if (bs == 0) {
953 bs = 32;
954 } else if (bs != 32) {
955 printf("LIBMTP PANIC: create_usb_mtp_devices(): "
956 "different objects support different object sizes!\n");
957 bs = 0;
958 break;
959 }
960 } else if (opd.DataType == PTP_DTC_UINT64) {
961 if (bs == 0) {
962 bs = 64;
963 } else if (bs != 64) {
964 printf("LIBMTP PANIC: create_usb_mtp_devices(): "
965 "different objects support different object sizes!\n");
966 bs = 0;
967 break;
968 }
969 } else {
970 // Ignore if other size.
971 printf("LIBMTP PANIC: create_usb_mtp_devices(): "
972 "awkward object size data type: %04x\n", opd.DataType);
973 bs = 0;
974 break;
975 }
976 }
977 }
978 if (bs == 0) {
979 // Could not detect object bitsize, assume 32 bits
980 bs = 32;
981 }
982 mtp_device->object_bitsize = bs;
983
984 /* No Errors yet for this device */
985 mtp_device->errorstack = NULL;
986
987 /* Default Max Battery Level, we will adjust this if possible */
988 mtp_device->maximum_battery_level = 100;
989
990 /* Check if device supports reading maximum battery level */
991 if(ptp_property_issupported( current_params,
992 PTP_DPC_BatteryLevel)) {
993 PTPDevicePropDesc dpd;
994
995 /* Try to read maximum battery level */
996 if(ptp_getdevicepropdesc(current_params,
997 PTP_DPC_BatteryLevel,
998 &dpd) != PTP_RC_OK) {
999 add_error_to_errorstack(mtp_device,
1000 LIBMTP_ERROR_CONNECTING,
1001 "Unable to read Maximum Battery Level for this "
1002 "device even though the device supposedly "
1003 "supports this functionality");
1004 }
1005
1006 /* TODO: is this appropriate? */
1007 /* If max battery level is 0 then leave the default, otherwise assign */
1008 if (dpd.FORM.Range.MaximumValue.u8 != 0) {
1009 mtp_device->maximum_battery_level = dpd.FORM.Range.MaximumValue.u8;
1010 }
1011
1012 ptp_free_devicepropdesc(&dpd);
1013 }
1014
1015 /* Set all default folders to 0 (root directory) */
1016 mtp_device->default_music_folder = 0;
1017 mtp_device->default_playlist_folder = 0;
1018 mtp_device->default_picture_folder = 0;
1019 mtp_device->default_video_folder = 0;
1020 mtp_device->default_organizer_folder = 0;
1021 mtp_device->default_zencast_folder = 0;
1022 mtp_device->default_album_folder = 0;
1023 mtp_device->default_text_folder = 0;
1024
1025 /* Set initial storage information */
1026 mtp_device->storage = NULL;
1027 if (LIBMTP_Get_Storage(mtp_device, LIBMTP_STORAGE_SORTBY_NOTSORTED) == -1) {
1028 add_error_to_errorstack(mtp_device,
1029 LIBMTP_ERROR_GENERAL,
1030 "Get Storage information failed.");
1031 mtp_device->storage = NULL;
1032 }
1033
1034 /*
1035 * Then get the handles and try to locate the default folders.
1036 * This has the desired side effect of caching all handles from
1037 * the device which speeds up later operations.
1038 */
1039 flush_handles(mtp_device);
1040
1041 return mtp_device;
1042}
1043
1044/**
Linus Walleij2d3f7b82007-02-14 09:24:20 +00001045 * Recursive function that adds MTP devices to a linked list
Linus Walleija700d222008-05-28 23:06:14 +00001046 * @param devices a list of raw devices to have real devices created for.
Linus Walleij2d3f7b82007-02-14 09:24:20 +00001047 * @return a device pointer to a newly created mtpdevice (used in linked
Linus Walleijb0ab5482007-08-29 21:08:54 +00001048 * list creation).
Linus Walleij2d3f7b82007-02-14 09:24:20 +00001049 */
Linus Walleija700d222008-05-28 23:06:14 +00001050static LIBMTP_mtpdevice_t * create_usb_mtp_devices(LIBMTP_raw_device_t *devices, int numdevs)
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001051{
Linus Walleija700d222008-05-28 23:06:14 +00001052 uint8_t i;
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001053 LIBMTP_mtpdevice_t *mtp_device_list = NULL;
1054 LIBMTP_mtpdevice_t *current_device = NULL;
Linus Walleija700d222008-05-28 23:06:14 +00001055
1056 for (i=0; i < numdevs; i++) {
Linus Walleij2d3f7b82007-02-14 09:24:20 +00001057 LIBMTP_mtpdevice_t *mtp_device;
Linus Walleijbdb89bd2008-05-28 23:32:35 +00001058 mtp_device = LIBMTP_Open_Raw_Device(&devices[i]);
Linus Walleija700d222008-05-28 23:06:14 +00001059
Linus Walleijbdb89bd2008-05-28 23:32:35 +00001060 /* On error, try next device */
1061 if (mtp_device == NULL)
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001062 continue;
Linus Walleijddaba2f2007-10-02 21:20:30 +00001063
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001064 /* Add the device to the list */
1065 mtp_device->next = NULL;
1066 if (mtp_device_list == NULL) {
1067 mtp_device_list = current_device = mtp_device;
1068 } else {
1069 current_device->next = mtp_device;
1070 current_device = mtp_device;
1071 }
Linus Walleij2d3f7b82007-02-14 09:24:20 +00001072 }
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001073 return mtp_device_list;
Linus Walleij2d3f7b82007-02-14 09:24:20 +00001074}
1075
1076/**
tedbullock848009b2007-03-03 23:20:12 +00001077 * Get the number of devices that are available in the listed device list
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001078 * @param device_list Pointer to a linked list of devices
1079 * @return Number of devices in the device list device_list
1080 * @see LIBMTP_Get_Connected_Devices()
tedbullock848009b2007-03-03 23:20:12 +00001081 */
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001082uint32_t LIBMTP_Number_Devices_In_List(LIBMTP_mtpdevice_t *device_list)
tedbullock848009b2007-03-03 23:20:12 +00001083{
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001084 uint32_t numdevices = 0;
1085 LIBMTP_mtpdevice_t *iter;
1086 for(iter = device_list; iter != NULL; iter = iter->next)
1087 numdevices++;
1088
1089 return numdevices;
tedbullock848009b2007-03-03 23:20:12 +00001090}
1091
1092/**
Linus Walleij2d3f7b82007-02-14 09:24:20 +00001093 * Get the first connected MTP device node in the linked list of devices.
1094 * Currently this only provides access to USB devices
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001095 * @param device_list A list of devices ready to be used by the caller. You
1096 * need to know how many there are.
Linus Walleij2d3f7b82007-02-14 09:24:20 +00001097 * @return Any error information gathered from device connections
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001098 * @see LIBMTP_Number_Devices_In_List()
Linus Walleij2d3f7b82007-02-14 09:24:20 +00001099 */
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001100LIBMTP_error_number_t LIBMTP_Get_Connected_Devices(LIBMTP_mtpdevice_t **device_list)
Linus Walleij2d3f7b82007-02-14 09:24:20 +00001101{
Linus Walleija700d222008-05-28 23:06:14 +00001102 LIBMTP_raw_device_t *devices;
1103 int numdevs;
Linus Walleij5fbb77d2007-03-07 08:40:36 +00001104 LIBMTP_error_number_t ret;
Linus Walleija700d222008-05-28 23:06:14 +00001105
1106 ret = LIBMTP_Detect_Raw_Devices(&devices, &numdevs);
Linus Walleij5fbb77d2007-03-07 08:40:36 +00001107 if (ret != LIBMTP_ERROR_NONE) {
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001108 *device_list = NULL;
Linus Walleij5fbb77d2007-03-07 08:40:36 +00001109 return ret;
Linus Walleij2d3f7b82007-02-14 09:24:20 +00001110 }
1111
1112 /* Assign linked list of devices */
Linus Walleija700d222008-05-28 23:06:14 +00001113 if (devices == NULL || numdevs == 0) {
1114 *device_list = NULL;
1115 return LIBMTP_ERROR_NO_DEVICE_ATTACHED;
1116 }
1117
1118 *device_list = create_usb_mtp_devices(devices, numdevs);
1119 free(devices);
Linus Walleij45a86372007-03-07 09:36:19 +00001120
Linus Walleij2d3f7b82007-02-14 09:24:20 +00001121 /* TODO: Add wifi device access here */
Richard Low99a93e82007-09-29 08:16:08 +00001122
1123 /* We have found some devices but create failed */
1124 if (*device_list == NULL)
1125 return LIBMTP_ERROR_CONNECTING;
Linus Walleij2d3f7b82007-02-14 09:24:20 +00001126
1127 return LIBMTP_ERROR_NONE;
1128}
1129
1130/**
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001131 * This closes and releases an allocated MTP device.
Linus Walleijb9256fd2006-02-15 09:40:43 +00001132 * @param device a pointer to the MTP device to release.
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001133 */
tedbullock0f033cb2007-02-14 20:56:54 +00001134void LIBMTP_Release_Device_List(LIBMTP_mtpdevice_t *device)
1135{
1136 if(device != NULL)
1137 {
1138 if(device->next != NULL)
1139 {
1140 LIBMTP_Release_Device_List(device->next);
1141 }
1142
1143 LIBMTP_Release_Device(device);
1144 }
1145}
1146
1147/**
1148 * This closes and releases an allocated MTP device.
1149 * @param device a pointer to the MTP device to release.
1150 */
Linus Walleijb9256fd2006-02-15 09:40:43 +00001151void LIBMTP_Release_Device(LIBMTP_mtpdevice_t *device)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001152{
Linus Walleij9b28da32006-03-16 13:47:58 +00001153 PTPParams *params = (PTPParams *) device->params;
Linus Walleij2d411db2006-03-22 12:13:09 +00001154 PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo;
Linus Walleij9b28da32006-03-16 13:47:58 +00001155
Linus Walleijb0ab5482007-08-29 21:08:54 +00001156 close_device(ptp_usb, params);
Linus Walleij2715c442007-01-20 22:35:29 +00001157 // Clear error stack
1158 LIBMTP_Clear_Errorstack(device);
Linus Walleij3ec86312006-08-21 13:25:24 +00001159 // Free iconv() converters...
Linus Walleija823a702006-08-27 21:27:46 +00001160 iconv_close(params->cd_locale_to_ucs2);
1161 iconv_close(params->cd_ucs2_to_locale);
tedbullock69a445b2007-02-15 07:41:04 +00001162 free(ptp_usb);
Linus Walleij073c4172007-02-02 22:26:33 +00001163 ptp_free_params(params);
Linus Walleij9e1b0812006-12-12 19:22:02 +00001164 free_storage_list(device);
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001165 free(device);
1166}
Linus Walleijb9256fd2006-02-15 09:40:43 +00001167
1168/**
Linus Walleij2715c442007-01-20 22:35:29 +00001169 * This can be used by any libmtp-intrinsic code that
Linus Walleij68b19c02007-02-15 11:50:37 +00001170 * need to stack up an error on the stack. You are only
1171 * supposed to add errors to the error stack using this
1172 * function, do not create and reference error entries
1173 * directly.
Linus Walleij2715c442007-01-20 22:35:29 +00001174 */
1175static void add_error_to_errorstack(LIBMTP_mtpdevice_t *device,
1176 LIBMTP_error_number_t errornumber,
1177 char const * const error_text)
1178{
1179 LIBMTP_error_t *newerror;
1180
Linus Walleij68b19c02007-02-15 11:50:37 +00001181 if (device == NULL) {
1182 fprintf(stderr, "LIBMTP PANIC: Trying to add error to a NULL device!\n");
1183 return;
1184 }
Linus Walleij2715c442007-01-20 22:35:29 +00001185 newerror = (LIBMTP_error_t *) malloc(sizeof(LIBMTP_error_t));
1186 newerror->errornumber = errornumber;
1187 newerror->error_text = strdup(error_text);
rreardon774503c2007-02-15 15:52:49 +00001188 newerror->next = NULL;
Linus Walleij2715c442007-01-20 22:35:29 +00001189 if (device->errorstack == NULL) {
1190 device->errorstack = newerror;
1191 } else {
1192 LIBMTP_error_t *tmp = device->errorstack;
1193
1194 while (tmp->next != NULL) {
1195 tmp = tmp->next;
1196 }
1197 tmp->next = newerror;
1198 }
1199}
1200
1201/**
Linus Walleij070e9b42007-01-22 08:49:28 +00001202 * Adds an error from the PTP layer to the error stack.
1203 */
1204static void add_ptp_error_to_errorstack(LIBMTP_mtpdevice_t *device,
1205 uint16_t ptp_error,
1206 char const * const error_text)
1207{
Linus Walleij68b19c02007-02-15 11:50:37 +00001208 if (device == NULL) {
1209 fprintf(stderr, "LIBMTP PANIC: Trying to add PTP error to a NULL device!\n");
1210 return;
1211 } else {
1212 char outstr[256];
1213 snprintf(outstr, sizeof(outstr), "PTP Layer error %04x: %s", ptp_error, error_text);
1214 outstr[sizeof(outstr)-1] = '\0';
1215 add_error_to_errorstack(device, LIBMTP_ERROR_PTP_LAYER, outstr);
1216 add_error_to_errorstack(device, LIBMTP_ERROR_PTP_LAYER, "(Look this up in ptp.h for an explanation.)");
1217 }
Linus Walleij070e9b42007-01-22 08:49:28 +00001218}
1219
1220/**
Linus Walleij2715c442007-01-20 22:35:29 +00001221 * This returns the error stack for a device in case you
1222 * need to either reference the error numbers (e.g. when
1223 * creating multilingual apps with multiple-language text
1224 * representations for each error number) or when you need
1225 * to build a multi-line error text widget or something like
1226 * that. You need to call the <code>LIBMTP_Clear_Errorstack</code>
1227 * to clear it when you're finished with it.
1228 * @param device a pointer to the MTP device to get the error
1229 * stack for.
1230 * @return the error stack or NULL if there are no errors
1231 * on the stack.
1232 * @see LIBMTP_Clear_Errorstack()
1233 * @see LIBMTP_Dump_Errorstack()
1234 */
1235LIBMTP_error_t *LIBMTP_Get_Errorstack(LIBMTP_mtpdevice_t *device)
1236{
Linus Walleij68b19c02007-02-15 11:50:37 +00001237 if (device == NULL) {
1238 fprintf(stderr, "LIBMTP PANIC: Trying to get the error stack of a NULL device!\n");
1239 }
Linus Walleij2715c442007-01-20 22:35:29 +00001240 return device->errorstack;
1241}
1242
1243/**
1244 * This function clears the error stack of a device and frees
1245 * any memory used by it. Call this when you're finished with
1246 * using the errors.
1247 * @param device a pointer to the MTP device to clear the error
1248 * stack for.
1249 */
1250void LIBMTP_Clear_Errorstack(LIBMTP_mtpdevice_t *device)
1251{
Linus Walleij68b19c02007-02-15 11:50:37 +00001252 if (device == NULL) {
1253 fprintf(stderr, "LIBMTP PANIC: Trying to clear the error stack of a NULL device!\n");
1254 } else {
1255 LIBMTP_error_t *tmp = device->errorstack;
Linus Walleij2715c442007-01-20 22:35:29 +00001256
Linus Walleij68b19c02007-02-15 11:50:37 +00001257 while (tmp != NULL) {
1258 LIBMTP_error_t *tmp2;
1259
1260 if (tmp->error_text != NULL) {
1261 free(tmp->error_text);
1262 }
1263 tmp2 = tmp;
1264 tmp = tmp->next;
1265 free(tmp2);
Linus Walleij2715c442007-01-20 22:35:29 +00001266 }
Linus Walleij68b19c02007-02-15 11:50:37 +00001267 device->errorstack = NULL;
Linus Walleij2715c442007-01-20 22:35:29 +00001268 }
Linus Walleij2715c442007-01-20 22:35:29 +00001269}
1270
1271/**
1272 * This function dumps the error stack to <code>stderr</code>.
1273 * (You still have to clear the stack though.)
1274 * @param device a pointer to the MTP device to dump the error
1275 * stack for.
1276 */
1277void LIBMTP_Dump_Errorstack(LIBMTP_mtpdevice_t *device)
1278{
Linus Walleij68b19c02007-02-15 11:50:37 +00001279 if (device == NULL) {
1280 fprintf(stderr, "LIBMTP PANIC: Trying to dump the error stack of a NULL device!\n");
1281 } else {
1282 LIBMTP_error_t *tmp = device->errorstack;
Linus Walleij2715c442007-01-20 22:35:29 +00001283
Linus Walleij68b19c02007-02-15 11:50:37 +00001284 while (tmp != NULL) {
1285 if (tmp->error_text != NULL) {
1286 fprintf(stderr, "Error %d: %s\n", tmp->errornumber, tmp->error_text);
1287 } else {
1288 fprintf(stderr, "Error %d: (unknown)\n", tmp->errornumber);
1289 }
1290 tmp = tmp->next;
Linus Walleij2715c442007-01-20 22:35:29 +00001291 }
Linus Walleij2715c442007-01-20 22:35:29 +00001292 }
1293}
1294
1295/**
Linus Walleij338ade42007-07-03 20:44:08 +00001296 * This command gets all handles and stuff by FAST directory retrieveal
1297 * which is available by getting all metadata for object
1298 * <code>0xffffffff</code> which simply means "all metadata for all objects".
1299 * This works on the vast majority of MTP devices (there ARE exceptions!)
1300 * and is quite quick. Check the error stack to see if there were
1301 * problems getting the metadata.
Linus Walleij8533bf72007-08-28 10:03:59 +00001302 * @return 0 if all was OK, -1 on failure.
Linus Walleij338ade42007-07-03 20:44:08 +00001303 */
Linus Walleij6bf68b42007-09-03 22:50:02 +00001304static int get_all_metadata_fast(LIBMTP_mtpdevice_t *device,
1305 uint32_t storage)
Linus Walleij338ade42007-07-03 20:44:08 +00001306{
1307 PTPParams *params = (PTPParams *) device->params;
1308 int cnt = 0;
Linus Walleij1e9a0332007-09-12 19:35:56 +00001309 int i, j, nrofprops;
Linus Walleij338ade42007-07-03 20:44:08 +00001310 uint32_t lasthandle = 0xffffffff;
Linus Walleij1e9a0332007-09-12 19:35:56 +00001311 MTPProperties *props = NULL;
1312 MTPProperties *prop;
Linus Walleij338ade42007-07-03 20:44:08 +00001313 uint16_t ret;
Linus Walleij2f622812008-08-30 22:06:58 +00001314 int oldtimeout;
1315 PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo;
1316
1317 /* The follow request causes the device to generate
1318 * a list of very file on the device and return it
1319 * in a single response.
1320 *
1321 * Some slow devices as well as devices with very
1322 * large file systems can easily take longer then
1323 * the standard timeout value before it is able
1324 * to return a response.
1325 *
1326 * Temporarly set timeout to allow working with
1327 * widest range of devices.
1328 */
1329 get_usb_device_timeout(ptp_usb, &oldtimeout);
1330 set_usb_device_timeout(ptp_usb, 60000);
Linus Walleij338ade42007-07-03 20:44:08 +00001331
Linus Walleijfcb43422008-05-23 21:53:55 +00001332 ret = ptp_mtp_getobjectproplist(params, 0xffffffff, &props, &nrofprops);
Linus Walleij2f622812008-08-30 22:06:58 +00001333 set_usb_device_timeout(ptp_usb, oldtimeout);
Linus Walleij8533bf72007-08-28 10:03:59 +00001334
1335 if (ret == PTP_RC_MTP_Specification_By_Group_Unsupported) {
1336 // What's the point in the device implementing this command if
1337 // you cannot use it to get all props for AT LEAST one object?
1338 // Well, whatever...
1339 add_ptp_error_to_errorstack(device, ret, "get_all_metadata_fast(): "
1340 "cannot retrieve all metadata for an object on this device.");
Linus Walleij91e98132007-08-28 10:05:11 +00001341 return -1;
Linus Walleij8533bf72007-08-28 10:03:59 +00001342 }
Linus Walleij338ade42007-07-03 20:44:08 +00001343 if (ret != PTP_RC_OK) {
Linus Walleij8533bf72007-08-28 10:03:59 +00001344 add_ptp_error_to_errorstack(device, ret, "get_all_metadata_fast(): "
1345 "could not get proplist of all objects.");
1346 return -1;
Linus Walleij338ade42007-07-03 20:44:08 +00001347 }
Linus Walleijfcb43422008-05-23 21:53:55 +00001348 if (props == NULL && nrofprops != 0) {
1349 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL,
1350 "get_all_metadata_fast(): "
1351 "call to ptp_mtp_getobjectproplist() returned "
1352 "inconsistent results.");
1353 return -1;
1354 }
Linus Walleij1e9a0332007-09-12 19:35:56 +00001355 params->props = props; /* cache it */
1356 params->nrofprops = nrofprops; /* cache it */
Linus Walleij338ade42007-07-03 20:44:08 +00001357
1358 /*
1359 * We count the number of objects by counting the ObjectHandle
Linus Walleij31b74292008-05-02 23:29:06 +00001360 * references, whenever it changes we get a new object, when it's
Linus Walleij338ade42007-07-03 20:44:08 +00001361 * the same, it is just different properties of the same object.
1362 */
Linus Walleij1e9a0332007-09-12 19:35:56 +00001363 prop = props;
1364 for (i=0;i<nrofprops;i++) {
Linus Walleij338ade42007-07-03 20:44:08 +00001365 if (lasthandle != prop->ObjectHandle) {
1366 cnt++;
1367 lasthandle = prop->ObjectHandle;
1368 }
Linus Walleij1e9a0332007-09-12 19:35:56 +00001369 prop++;
Linus Walleij338ade42007-07-03 20:44:08 +00001370 }
Linus Walleij338ade42007-07-03 20:44:08 +00001371 lasthandle = 0xffffffff;
1372 params->objectinfo = malloc (sizeof (PTPObjectInfo) * cnt);
1373 memset (params->objectinfo, 0, sizeof(PTPObjectInfo) * cnt);
1374 params->handles.Handler = malloc (sizeof (uint32_t) * cnt);
1375 params->handles.n = cnt;
1376
Linus Walleij1e9a0332007-09-12 19:35:56 +00001377 prop = props;
Linus Walleij338ade42007-07-03 20:44:08 +00001378 i = -1;
Linus Walleij1e9a0332007-09-12 19:35:56 +00001379 for (j=0;j<nrofprops;j++) {
Linus Walleij338ade42007-07-03 20:44:08 +00001380 if (lasthandle != prop->ObjectHandle) {
1381 if (i >= 0) {
1382 if (!params->objectinfo[i].Filename) {
Linus Walleij6bf68b42007-09-03 22:50:02 +00001383 /* I have one such file on my Creative (Marcus) */
Linus Walleij338ade42007-07-03 20:44:08 +00001384 params->objectinfo[i].Filename = strdup("<null>");
1385 }
1386 }
1387 i++;
1388 lasthandle = prop->ObjectHandle;
1389 params->handles.Handler[i] = prop->ObjectHandle;
1390 }
1391 switch (prop->property) {
1392 case PTP_OPC_ParentObject:
1393 params->objectinfo[i].ParentObject = prop->propval.u32;
1394 break;
1395 case PTP_OPC_ObjectFormat:
1396 params->objectinfo[i].ObjectFormat = prop->propval.u16;
1397 break;
1398 case PTP_OPC_ObjectSize:
Linus Walleijd9d28d52007-08-04 19:01:18 +00001399 // We loose precision here, up to 32 bits! However the commands that
1400 // retrieve metadata for files and tracks will make sure that the
1401 // PTP_OPC_ObjectSize is read in and duplicated again.
Linus Walleijddaba2f2007-10-02 21:20:30 +00001402 if (device->object_bitsize == 64) {
1403 params->objectinfo[i].ObjectCompressedSize = (uint32_t) prop->propval.u64;
1404 } else {
1405 params->objectinfo[i].ObjectCompressedSize = prop->propval.u32;
1406 }
Linus Walleij338ade42007-07-03 20:44:08 +00001407 break;
1408 case PTP_OPC_StorageID:
1409 params->objectinfo[i].StorageID = prop->propval.u32;
1410 break;
1411 case PTP_OPC_ObjectFileName:
Richard Low8e8d9d42007-07-14 10:36:50 +00001412 if (prop->propval.str != NULL)
1413 params->objectinfo[i].Filename = strdup(prop->propval.str);
Linus Walleij338ade42007-07-03 20:44:08 +00001414 break;
1415 default:
1416 /*
1417 * This was in libgphoto2 no idea what it tests for...
1418 * if ((prop->property & 0xfff0) == 0xdc00)
1419 * gp_log (GP_LOG_DEBUG, "ptp2/mtpfast", "case %x type %x unhandled.\n", prop->property, prop->datatype);
1420 */
1421 break;
1422 // FIXME: the rest of the metadata is readily available right here!
1423 }
Linus Walleij1e9a0332007-09-12 19:35:56 +00001424 prop++;
Linus Walleij338ade42007-07-03 20:44:08 +00001425 }
Linus Walleij8533bf72007-08-28 10:03:59 +00001426 return 0;
Linus Walleij338ade42007-07-03 20:44:08 +00001427}
1428
1429/**
1430 * This function will recurse through all the directories on the device,
1431 * starting at the root directory, gathering metadata as it moves along.
1432 * It works better on some devices that will only return data for a
1433 * certain directory and does not respect the option to get all metadata
1434 * for all objects.
1435 */
Linus Walleij6bf68b42007-09-03 22:50:02 +00001436static void get_handles_recursively(LIBMTP_mtpdevice_t *device,
1437 PTPParams *params,
1438 PTPObjectHandles *handles,
1439 uint32_t storageid,
1440 uint32_t parent)
Linus Walleij338ade42007-07-03 20:44:08 +00001441{
1442 PTPObjectHandles currentHandles;
1443 int i = 0;
1444 uint32_t old_handles;
1445
1446 uint16_t ret = ptp_getobjecthandles(params,
Linus Walleij6bf68b42007-09-03 22:50:02 +00001447 storageid,
Linus Walleij338ade42007-07-03 20:44:08 +00001448 PTP_GOH_ALL_FORMATS,
1449 parent,
1450 &currentHandles);
1451
1452 if (ret != PTP_RC_OK) {
1453 add_ptp_error_to_errorstack(device, ret, "get_handles_recursively(): could not get object handles.");
1454 return;
1455 }
1456
1457 if (currentHandles.Handler == NULL || currentHandles.n == 0)
1458 return;
1459
1460 old_handles = handles->n;
1461
1462 // Realloc main space
1463 handles->Handler = (uint32_t *) realloc(handles->Handler,
1464 (old_handles + currentHandles.n) * sizeof(uint32_t));
1465 // Realloc object info cache
1466 params->objectinfo = (PTPObjectInfo*) realloc(params->objectinfo,
1467 (old_handles + currentHandles.n) * sizeof(PTPObjectInfo));
1468 memset(&params->objectinfo[old_handles], 0, currentHandles.n * sizeof(PTPObjectInfo));
1469
1470 // Copy new handles
Linus Walleij4dbfa762007-10-18 18:34:16 +00001471 memmove(&(handles->Handler[old_handles]), currentHandles.Handler, currentHandles.n * sizeof(uint32_t));
Linus Walleij338ade42007-07-03 20:44:08 +00001472 handles->n = old_handles + currentHandles.n;
1473
1474 // Now descend into any subdirectories found
1475 for (i = 0; i < currentHandles.n; i++) {
1476 ret = ptp_getobjectinfo(params,
1477 currentHandles.Handler[i],
1478 &params->objectinfo[old_handles + i]);
1479
1480 if (ret == PTP_RC_OK) {
1481 PTPObjectInfo *oi;
1482
1483 oi = &params->objectinfo[old_handles + i];
1484 if (oi->ObjectFormat == PTP_OFC_Association) {
Linus Walleij6bf68b42007-09-03 22:50:02 +00001485 get_handles_recursively(device, params, handles, storageid, currentHandles.Handler[i]);
Linus Walleij338ade42007-07-03 20:44:08 +00001486 }
1487 } else {
1488 add_error_to_errorstack(device,
1489 LIBMTP_ERROR_CONNECTING,
1490 "Found a bad handle, trying to ignore it.");
1491 }
1492 }
1493
1494 free(currentHandles.Handler);
1495}
1496
1497/**
Linus Walleij438bd7f2006-06-08 11:35:44 +00001498 * This function refresh the internal handle list whenever
1499 * the items stored inside the device is altered. On operations
1500 * that do not add or remove objects, this is typically not
1501 * called.
1502 * @param device a pointer to the MTP device to flush handles for.
1503 */
1504static void flush_handles(LIBMTP_mtpdevice_t *device)
1505{
1506 PTPParams *params = (PTPParams *) device->params;
Linus Walleij338ade42007-07-03 20:44:08 +00001507 PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo;
Linus Walleij8533bf72007-08-28 10:03:59 +00001508 int ret;
Linus Walleijf0bf4372007-07-01 21:47:38 +00001509 uint32_t i;
1510
Linus Walleij438bd7f2006-06-08 11:35:44 +00001511 if (params->handles.Handler != NULL) {
1512 free(params->handles.Handler);
1513 }
Linus Walleijf0bf4372007-07-01 21:47:38 +00001514 if (params->objectinfo != NULL) {
1515 for (i=0;i<params->handles.n;i++)
1516 ptp_free_objectinfo (&params->objectinfo[i]);
1517 free(params->objectinfo);
1518 }
Linus Walleij1e9a0332007-09-12 19:35:56 +00001519 if (params->props != NULL) {
Linus Walleija6d0d482007-10-31 08:54:56 +00001520 ptp_destroy_object_prop_list(params->props, params->nrofprops);
Linus Walleij338ade42007-07-03 20:44:08 +00001521 }
Richard Low6711f442007-05-05 19:00:59 +00001522
Linus Walleijf0bf4372007-07-01 21:47:38 +00001523 params->handles.n = 0;
Richard Low6711f442007-05-05 19:00:59 +00001524 params->handles.Handler = NULL;
Linus Walleijf0bf4372007-07-01 21:47:38 +00001525 params->objectinfo = NULL;
Linus Walleij1e9a0332007-09-12 19:35:56 +00001526 params->props = NULL;
1527 params->nrofprops = 0;
Linus Walleijf0bf4372007-07-01 21:47:38 +00001528
Linus Walleij338ade42007-07-03 20:44:08 +00001529 if (ptp_operation_issupported(params,PTP_OC_MTP_GetObjPropList)
Linus Walleijfec4d562008-06-01 22:30:36 +00001530 && !FLAG_BROKEN_MTPGETOBJPROPLIST(ptp_usb)
1531 && !FLAG_BROKEN_MTPGETOBJPROPLIST_ALL(ptp_usb)) {
Linus Walleij139455e2007-08-28 10:25:06 +00001532 // Use the fast method. Ignore return value for now.
Linus Walleij6bf68b42007-09-03 22:50:02 +00001533 ret = get_all_metadata_fast(device, PTP_GOH_ALL_STORAGE);
Linus Walleij8533bf72007-08-28 10:03:59 +00001534 }
Linus Walleij139455e2007-08-28 10:25:06 +00001535 // If the previous failed or returned no objects, use classic
1536 // methods instead.
Linus Walleij1e9a0332007-09-12 19:35:56 +00001537 if (params->props == NULL) {
Linus Walleij338ade42007-07-03 20:44:08 +00001538 // Get all the handles using just standard commands.
Linus Walleij6bf68b42007-09-03 22:50:02 +00001539 if (device->storage == NULL) {
1540 get_handles_recursively(device, params,
1541 &params->handles,
1542 PTP_GOH_ALL_STORAGE,
1543 PTP_GOH_ROOT_PARENT);
1544 } else {
1545 // Get handles for each storage in turn.
1546 LIBMTP_devicestorage_t *storage = device->storage;
1547 while(storage != NULL) {
1548 get_handles_recursively(device, params,
1549 &params->handles,
1550 storage->id,
1551 PTP_GOH_ROOT_PARENT);
1552 storage = storage->next;
1553 }
1554 }
Linus Walleij338ade42007-07-03 20:44:08 +00001555 }
Linus Walleij8f7f1aa2008-06-15 19:00:23 +00001556
1557 /*
1558 * Loop over the handles, fix up any NULL filenames or
1559 * keywords, then attempt to locate some default folders
1560 * in the root directory of the primary storage.
1561 */
Linus Walleijf0bf4372007-07-01 21:47:38 +00001562 for(i = 0; i < params->handles.n; i++) {
1563 PTPObjectInfo *oi;
1564
1565 oi = &params->objectinfo[i];
1566 if (oi->Filename == NULL) {
1567 oi->Filename = strdup("<null>");
1568 }
1569 if (oi->Keywords == NULL) {
1570 oi->Keywords = strdup("<null>");
1571 }
1572
1573 /* Ignore handles that point to non-folders */
1574 if(oi->ObjectFormat != PTP_OFC_Association)
1575 continue;
Linus Walleij8f7f1aa2008-06-15 19:00:23 +00001576 /* Only look in the root folder */
1577 if (oi->ParentObject != 0x00000000U)
Linus Walleijf0bf4372007-07-01 21:47:38 +00001578 continue;
Linus Walleijc40c9bf2008-06-13 23:24:17 +00001579 /* Only look in the primary storage */
1580 if (device->storage != NULL && oi->StorageID != device->storage->id)
1581 continue;
Linus Walleij8f7f1aa2008-06-15 19:00:23 +00001582
Linus Walleijf0bf4372007-07-01 21:47:38 +00001583
1584 /* Is this the Music Folder */
1585 if (!strcasecmp(oi->Filename, "My Music") ||
1586 !strcasecmp(oi->Filename, "Music")) {
1587 device->default_music_folder =
1588 params->handles.Handler[i];
1589 }
1590 else if (!strcasecmp(oi->Filename, "My Playlists") ||
1591 !strcasecmp(oi->Filename, "Playlists")) {
1592 device->default_playlist_folder =
1593 params->handles.Handler[i];
1594 }
1595 else if (!strcasecmp(oi->Filename, "My Pictures") ||
1596 !strcasecmp(oi->Filename, "Pictures")) {
1597 device->default_picture_folder =
1598 params->handles.Handler[i];
1599 }
1600 else if (!strcasecmp(oi->Filename, "My Video") ||
1601 !strcasecmp(oi->Filename, "Video")) {
1602 device->default_video_folder =
1603 params->handles.Handler[i];
1604 }
1605 else if (!strcasecmp(oi->Filename, "My Organizer")) {
1606 device->default_organizer_folder =
1607 params->handles.Handler[i];
1608 }
Linus Walleij2350b712008-01-14 22:54:37 +00001609 else if (!strcasecmp(oi->Filename, "ZENcast") ||
1610 !strcasecmp(oi->Filename, "Datacasts")) {
Linus Walleijf0bf4372007-07-01 21:47:38 +00001611 device->default_zencast_folder =
1612 params->handles.Handler[i];
1613 }
1614 else if (!strcasecmp(oi->Filename, "My Albums") ||
1615 !strcasecmp(oi->Filename, "Albums")) {
1616 device->default_album_folder =
1617 params->handles.Handler[i];
1618 }
Linus Walleij2350b712008-01-14 22:54:37 +00001619 else if (!strcasecmp(oi->Filename, "Text") ||
1620 !strcasecmp(oi->Filename, "Texts")) {
Linus Walleijf0bf4372007-07-01 21:47:38 +00001621 device->default_text_folder =
1622 params->handles.Handler[i];
1623 }
1624 }
Richard Low6711f442007-05-05 19:00:59 +00001625}
Linus Walleij438bd7f2006-06-08 11:35:44 +00001626
Linus Walleij438bd7f2006-06-08 11:35:44 +00001627/**
Linus Walleij9e1b0812006-12-12 19:22:02 +00001628 * This function traverses a devices storage list freeing up the
1629 * strings and the structs.
1630 * @param device a pointer to the MTP device to free the storage
1631 * list for.
1632 */
1633static void free_storage_list(LIBMTP_mtpdevice_t *device)
1634{
Linus Walleije1ac07e2006-12-14 19:38:59 +00001635 LIBMTP_devicestorage_t *storage;
1636 LIBMTP_devicestorage_t *tmp;
Linus Walleij9e1b0812006-12-12 19:22:02 +00001637
Linus Walleije1ac07e2006-12-14 19:38:59 +00001638 storage = device->storage;
1639 while(storage != NULL) {
1640 if (storage->StorageDescription != NULL) {
1641 free(storage->StorageDescription);
1642 }
1643 if (storage->VolumeIdentifier != NULL) {
1644 free(storage->VolumeIdentifier);
1645 }
1646 tmp = storage;
1647 storage = storage->next;
1648 free(tmp);
Linus Walleij9e1b0812006-12-12 19:22:02 +00001649 }
1650 device->storage = NULL;
1651
1652 return;
1653}
1654
1655/**
1656 * This function traverses a devices storage list freeing up the
1657 * strings and the structs.
1658 * @param device a pointer to the MTP device to free the storage
1659 * list for.
1660 */
1661static int sort_storage_by(LIBMTP_mtpdevice_t *device,int const sortby)
1662{
1663 LIBMTP_devicestorage_t *oldhead, *ptr1, *ptr2, *newlist;
1664
1665 if (device->storage == NULL)
1666 return -1;
1667 if (sortby == LIBMTP_STORAGE_SORTBY_NOTSORTED)
1668 return 0;
1669
1670 oldhead = ptr1 = ptr2 = device->storage;
1671
1672 newlist = NULL;
1673
1674 while(oldhead != NULL) {
1675 ptr1 = ptr2 = oldhead;
1676 while(ptr1 != NULL) {
1677
1678 if (sortby == LIBMTP_STORAGE_SORTBY_FREESPACE && ptr1->FreeSpaceInBytes > ptr2->FreeSpaceInBytes)
1679 ptr2 = ptr1;
1680 if (sortby == LIBMTP_STORAGE_SORTBY_MAXSPACE && ptr1->FreeSpaceInBytes > ptr2->FreeSpaceInBytes)
1681 ptr2 = ptr1;
1682
1683 ptr1 = ptr1->next;
1684 }
1685
1686 // Make our previous entries next point to our next
1687 if(ptr2->prev != NULL) {
1688 ptr1 = ptr2->prev;
1689 ptr1->next = ptr2->next;
1690 } else {
1691 oldhead = ptr2->next;
1692 if(oldhead != NULL)
1693 oldhead->prev = NULL;
1694 }
1695
1696 // Make our next entries previous point to our previous
1697 ptr1 = ptr2->next;
1698 if(ptr1 != NULL) {
1699 ptr1->prev = ptr2->prev;
1700 } else {
1701 ptr1 = ptr2->prev;
1702 if(ptr1 != NULL)
1703 ptr1->next = NULL;
1704 }
1705
1706 if(newlist == NULL) {
1707 newlist = ptr2;
1708 newlist->prev = NULL;
1709 } else {
1710 ptr2->prev = newlist;
1711 newlist->next = ptr2;
1712 newlist = newlist->next;
1713 }
1714 }
1715
1716 newlist->next = NULL;
1717 while(newlist->prev != NULL)
1718 newlist = newlist->prev;
1719
1720 device->storage = newlist;
1721
1722 return 0;
1723}
1724
1725/**
1726 * This function grabs the first storageid from the device storage
1727 * list.
1728 * @param device a pointer to the MTP device to free the storage
1729 * list for.
1730 */
1731static uint32_t get_first_storageid(LIBMTP_mtpdevice_t *device)
1732{
1733 LIBMTP_devicestorage_t *storage = device->storage;
Linus Walleijfb28b632007-10-23 21:56:18 +00001734 uint32_t store = 0x00000000; //Should this be 0xffffffffu instead?
Linus Walleij9e1b0812006-12-12 19:22:02 +00001735
1736 if(storage != NULL)
1737 store = storage->id;
1738
1739 return store;
1740}
1741
1742/**
Linus Walleij6bf68b42007-09-03 22:50:02 +00001743 * This function grabs the freespace from a certain storage in
Linus Walleij9e1b0812006-12-12 19:22:02 +00001744 * device storage list.
1745 * @param device a pointer to the MTP device to free the storage
1746 * list for.
Linus Walleij6bf68b42007-09-03 22:50:02 +00001747 * @param storageid the storage ID for the storage to flush and
1748 * get free space for.
1749 * @param freespace the free space on this storage will be returned
1750 * in this variable.
Linus Walleij9e1b0812006-12-12 19:22:02 +00001751 */
Linus Walleij6bf68b42007-09-03 22:50:02 +00001752static int get_storage_freespace(LIBMTP_mtpdevice_t *device,
1753 LIBMTP_devicestorage_t *storage,
1754 uint64_t *freespace)
Linus Walleij9e1b0812006-12-12 19:22:02 +00001755{
Linus Walleije1ac07e2006-12-14 19:38:59 +00001756 PTPParams *params = (PTPParams *) device->params;
Linus Walleij9e1b0812006-12-12 19:22:02 +00001757
Linus Walleije1ac07e2006-12-14 19:38:59 +00001758 // Always query the device about this, since some models explicitly
Linus Walleij6bf68b42007-09-03 22:50:02 +00001759 // needs that. We flush all data on queries storage here.
Linus Walleije1ac07e2006-12-14 19:38:59 +00001760 if (ptp_operation_issupported(params,PTP_OC_GetStorageInfo)) {
1761 PTPStorageInfo storageInfo;
Linus Walleij070e9b42007-01-22 08:49:28 +00001762 uint16_t ret;
Linus Walleije1ac07e2006-12-14 19:38:59 +00001763
Linus Walleij070e9b42007-01-22 08:49:28 +00001764 ret = ptp_getstorageinfo(params, storage->id, &storageInfo);
1765 if (ret != PTP_RC_OK) {
1766 add_ptp_error_to_errorstack(device, ret, "get_first_storage_freespace(): could not get storage info.");
Linus Walleije1ac07e2006-12-14 19:38:59 +00001767 return -1;
1768 }
1769 if (storage->StorageDescription != NULL) {
1770 free(storage->StorageDescription);
1771 }
1772 if (storage->VolumeIdentifier != NULL) {
1773 free(storage->VolumeIdentifier);
1774 }
1775 storage->StorageType = storageInfo.StorageType;
1776 storage->FilesystemType = storageInfo.FilesystemType;
1777 storage->AccessCapability = storageInfo.AccessCapability;
1778 storage->MaxCapacity = storageInfo.MaxCapability;
1779 storage->FreeSpaceInBytes = storageInfo.FreeSpaceInBytes;
1780 storage->FreeSpaceInObjects = storageInfo.FreeSpaceInImages;
1781 storage->StorageDescription = storageInfo.StorageDescription;
1782 storage->VolumeIdentifier = storageInfo.VolumeLabel;
1783 }
1784 if(storage->FreeSpaceInBytes == (uint64_t) -1)
Linus Walleij9e1b0812006-12-12 19:22:02 +00001785 return -1;
1786 *freespace = storage->FreeSpaceInBytes;
1787 return 0;
1788}
1789
1790/**
Linus Walleij8c45b292006-04-26 14:12:44 +00001791 * This function dumps out a large chunk of textual information
1792 * provided from the PTP protocol and additionally some extra
1793 * MTP-specific information where applicable.
1794 * @param device a pointer to the MTP device to report info from.
1795 */
1796void LIBMTP_Dump_Device_Info(LIBMTP_mtpdevice_t *device)
1797{
1798 int i;
1799 PTPParams *params = (PTPParams *) device->params;
Linus Walleijc6210fb2006-05-08 11:11:41 +00001800 PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo;
Linus Walleije1ac07e2006-12-14 19:38:59 +00001801 LIBMTP_devicestorage_t *storage = device->storage;
mopoke96143402006-10-30 04:37:26 +00001802
Linus Walleijc6210fb2006-05-08 11:11:41 +00001803 printf("USB low-level info:\n");
1804 dump_usbinfo(ptp_usb);
Linus Walleij8c45b292006-04-26 14:12:44 +00001805 /* Print out some verbose information */
1806 printf("Device info:\n");
1807 printf(" Manufacturer: %s\n", params->deviceinfo.Manufacturer);
1808 printf(" Model: %s\n", params->deviceinfo.Model);
1809 printf(" Device version: %s\n", params->deviceinfo.DeviceVersion);
1810 printf(" Serial number: %s\n", params->deviceinfo.SerialNumber);
1811 printf(" Vendor extension ID: 0x%08x\n", params->deviceinfo.VendorExtensionID);
1812 printf(" Vendor extension description: %s\n", params->deviceinfo.VendorExtensionDesc);
Linus Walleijddaba2f2007-10-02 21:20:30 +00001813 printf(" Detected object size: %d bits\n", device->object_bitsize);
Linus Walleij8c45b292006-04-26 14:12:44 +00001814 printf("Supported operations:\n");
1815 for (i=0;i<params->deviceinfo.OperationsSupported_len;i++) {
Linus Walleij4f40d112006-09-21 07:44:53 +00001816 char txt[256];
1817
1818 (void) ptp_render_opcode (params, params->deviceinfo.OperationsSupported[i], sizeof(txt), txt);
1819 printf(" %04x: %s\n", params->deviceinfo.OperationsSupported[i], txt);
Linus Walleij8c45b292006-04-26 14:12:44 +00001820 }
1821 printf("Events supported:\n");
1822 if (params->deviceinfo.EventsSupported_len == 0) {
1823 printf(" None.\n");
1824 } else {
1825 for (i=0;i<params->deviceinfo.EventsSupported_len;i++) {
1826 printf(" 0x%04x\n", params->deviceinfo.EventsSupported[i]);
1827 }
1828 }
1829 printf("Device Properties Supported:\n");
1830 for (i=0;i<params->deviceinfo.DevicePropertiesSupported_len;i++) {
Linus Walleij16c51f02006-05-04 13:20:22 +00001831 char const *propdesc = ptp_get_property_description(params, params->deviceinfo.DevicePropertiesSupported[i]);
mopoke96143402006-10-30 04:37:26 +00001832
Linus Walleij545c7792006-06-13 15:22:30 +00001833 if (propdesc != NULL) {
1834 printf(" 0x%04x: %s\n", params->deviceinfo.DevicePropertiesSupported[i], propdesc);
1835 } else {
1836 uint16_t prop = params->deviceinfo.DevicePropertiesSupported[i];
Linus Walleijcf223e62006-06-19 09:31:53 +00001837 printf(" 0x%04x: Unknown property\n", prop);
Linus Walleij545c7792006-06-13 15:22:30 +00001838 }
Linus Walleij8c45b292006-04-26 14:12:44 +00001839 }
Linus Walleij0af979a2006-06-19 11:49:10 +00001840
1841 if (ptp_operation_issupported(params,PTP_OC_MTP_GetObjectPropsSupported)) {
1842 printf("Playable File (Object) Types and Object Properties Supported:\n");
1843 for (i=0;i<params->deviceinfo.ImageFormats_len;i++) {
1844 char txt[256];
1845 uint16_t ret;
1846 uint16_t *props = NULL;
1847 uint32_t propcnt = 0;
1848 int j;
mopoke96143402006-10-30 04:37:26 +00001849
Linus Walleij0af979a2006-06-19 11:49:10 +00001850 (void) ptp_render_ofc (params, params->deviceinfo.ImageFormats[i], sizeof(txt), txt);
1851 printf(" %04x: %s\n", params->deviceinfo.ImageFormats[i], txt);
mopoke96143402006-10-30 04:37:26 +00001852
Linus Walleij0af979a2006-06-19 11:49:10 +00001853 ret = ptp_mtp_getobjectpropssupported (params, params->deviceinfo.ImageFormats[i], &propcnt, &props);
1854 if (ret != PTP_RC_OK) {
Linus Walleij070e9b42007-01-22 08:49:28 +00001855 add_ptp_error_to_errorstack(device, ret, "LIBMTP_Dump_Device_Info(): error on query for object properties.");
Linus Walleij0af979a2006-06-19 11:49:10 +00001856 } else {
1857 for (j=0;j<propcnt;j++) {
Linus Walleij14830342007-03-23 13:32:00 +00001858 PTPObjectPropDesc opd;
1859 int k;
1860
Linus Walleij0af979a2006-06-19 11:49:10 +00001861 (void) ptp_render_mtp_propname(props[j],sizeof(txt),txt);
Linus Walleij14830342007-03-23 13:32:00 +00001862 printf(" %04x: %s", props[j], txt);
1863 // Get a more verbose description
1864 ret = ptp_mtp_getobjectpropdesc(params, props[j], params->deviceinfo.ImageFormats[i], &opd);
1865 if (ret != PTP_RC_OK) {
1866 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Dump_Device_Info(): "
1867 "could not get property description.");
1868 break;
1869 }
1870
1871 if (opd.DataType == PTP_DTC_STR) {
1872 printf(" STRING data type");
Linus Walleij22cc4872007-11-05 22:57:19 +00001873 switch (opd.FormFlag) {
1874 case PTP_OPFF_DateTime:
1875 printf(" DATETIME FORM");
1876 break;
1877 case PTP_OPFF_RegularExpression:
1878 printf(" REGULAR EXPRESSION FORM");
1879 break;
1880 case PTP_OPFF_LongString:
1881 printf(" LONG STRING FORM");
1882 break;
1883 default:
1884 break;
1885 }
Linus Walleij14830342007-03-23 13:32:00 +00001886 } else {
1887 if (opd.DataType & PTP_DTC_ARRAY_MASK) {
1888 printf(" array of");
1889 }
1890
1891 switch (opd.DataType & (~PTP_DTC_ARRAY_MASK)) {
1892
1893 case PTP_DTC_UNDEF:
1894 printf(" UNDEFINED data type");
1895 break;
1896
1897 case PTP_DTC_INT8:
1898 printf(" INT8 data type");
1899 switch (opd.FormFlag) {
Linus Walleij22cc4872007-11-05 22:57:19 +00001900 case PTP_OPFF_Range:
Linus Walleij14830342007-03-23 13:32:00 +00001901 printf(" range: MIN %d, MAX %d, STEP %d",
1902 opd.FORM.Range.MinimumValue.i8,
1903 opd.FORM.Range.MaximumValue.i8,
1904 opd.FORM.Range.StepSize.i8);
1905 break;
Linus Walleij22cc4872007-11-05 22:57:19 +00001906 case PTP_OPFF_Enumeration:
Linus Walleij14830342007-03-23 13:32:00 +00001907 printf(" enumeration: ");
1908 for(k=0;k<opd.FORM.Enum.NumberOfValues;k++) {
1909 printf("%d, ", opd.FORM.Enum.SupportedValue[k].i8);
1910 }
1911 break;
Linus Walleij22cc4872007-11-05 22:57:19 +00001912 case PTP_OPFF_ByteArray:
1913 printf(" byte array: ");
1914 break;
Linus Walleij14830342007-03-23 13:32:00 +00001915 default:
1916 printf(" ANY 8BIT VALUE form");
1917 break;
1918 }
1919 break;
1920
1921 case PTP_DTC_UINT8:
1922 printf(" UINT8 data type");
1923 switch (opd.FormFlag) {
Linus Walleij22cc4872007-11-05 22:57:19 +00001924 case PTP_OPFF_Range:
Linus Walleij14830342007-03-23 13:32:00 +00001925 printf(" range: MIN %d, MAX %d, STEP %d",
1926 opd.FORM.Range.MinimumValue.u8,
1927 opd.FORM.Range.MaximumValue.u8,
1928 opd.FORM.Range.StepSize.u8);
1929 break;
Linus Walleij22cc4872007-11-05 22:57:19 +00001930 case PTP_OPFF_Enumeration:
Linus Walleij14830342007-03-23 13:32:00 +00001931 printf(" enumeration: ");
1932 for(k=0;k<opd.FORM.Enum.NumberOfValues;k++) {
1933 printf("%d, ", opd.FORM.Enum.SupportedValue[k].u8);
1934 }
1935 break;
Linus Walleij22cc4872007-11-05 22:57:19 +00001936 case PTP_OPFF_ByteArray:
1937 printf(" byte array: ");
1938 break;
Linus Walleij14830342007-03-23 13:32:00 +00001939 default:
1940 printf(" ANY 8BIT VALUE form");
1941 break;
1942 }
1943 break;
1944
1945 case PTP_DTC_INT16:
1946 printf(" INT16 data type");
1947 switch (opd.FormFlag) {
Linus Walleij22cc4872007-11-05 22:57:19 +00001948 case PTP_OPFF_Range:
Linus Walleij14830342007-03-23 13:32:00 +00001949 printf(" range: MIN %d, MAX %d, STEP %d",
1950 opd.FORM.Range.MinimumValue.i16,
1951 opd.FORM.Range.MaximumValue.i16,
1952 opd.FORM.Range.StepSize.i16);
1953 break;
Linus Walleij22cc4872007-11-05 22:57:19 +00001954 case PTP_OPFF_Enumeration:
Linus Walleij14830342007-03-23 13:32:00 +00001955 printf(" enumeration: ");
1956 for(k=0;k<opd.FORM.Enum.NumberOfValues;k++) {
1957 printf("%d, ", opd.FORM.Enum.SupportedValue[k].i16);
1958 }
1959 break;
1960 default:
1961 printf(" ANY 16BIT VALUE form");
1962 break;
1963 }
1964 break;
1965
1966 case PTP_DTC_UINT16:
1967 printf(" UINT16 data type");
1968 switch (opd.FormFlag) {
Linus Walleij22cc4872007-11-05 22:57:19 +00001969 case PTP_OPFF_Range:
Linus Walleij14830342007-03-23 13:32:00 +00001970 printf(" range: MIN %d, MAX %d, STEP %d",
1971 opd.FORM.Range.MinimumValue.u16,
1972 opd.FORM.Range.MaximumValue.u16,
1973 opd.FORM.Range.StepSize.u16);
1974 break;
Linus Walleij22cc4872007-11-05 22:57:19 +00001975 case PTP_OPFF_Enumeration:
Linus Walleij14830342007-03-23 13:32:00 +00001976 printf(" enumeration: ");
1977 for(k=0;k<opd.FORM.Enum.NumberOfValues;k++) {
1978 printf("%d, ", opd.FORM.Enum.SupportedValue[k].u16);
1979 }
1980 break;
1981 default:
1982 printf(" ANY 16BIT VALUE form");
1983 break;
1984 }
1985 break;
1986
1987 case PTP_DTC_INT32:
1988 printf(" INT32 data type");
1989 switch (opd.FormFlag) {
Linus Walleij22cc4872007-11-05 22:57:19 +00001990 case PTP_OPFF_Range:
Linus Walleij14830342007-03-23 13:32:00 +00001991 printf(" range: MIN %d, MAX %d, STEP %d",
1992 opd.FORM.Range.MinimumValue.i32,
1993 opd.FORM.Range.MaximumValue.i32,
1994 opd.FORM.Range.StepSize.i32);
1995 break;
Linus Walleij22cc4872007-11-05 22:57:19 +00001996 case PTP_OPFF_Enumeration:
Linus Walleij14830342007-03-23 13:32:00 +00001997 printf(" enumeration: ");
1998 for(k=0;k<opd.FORM.Enum.NumberOfValues;k++) {
1999 printf("%d, ", opd.FORM.Enum.SupportedValue[k].i32);
2000 }
2001 break;
2002 default:
2003 printf(" ANY 32BIT VALUE form");
2004 break;
2005 }
2006 break;
2007
2008 case PTP_DTC_UINT32:
2009 printf(" UINT32 data type");
2010 switch (opd.FormFlag) {
Linus Walleij22cc4872007-11-05 22:57:19 +00002011 case PTP_OPFF_Range:
Linus Walleij14830342007-03-23 13:32:00 +00002012 printf(" range: MIN %d, MAX %d, STEP %d",
2013 opd.FORM.Range.MinimumValue.u32,
2014 opd.FORM.Range.MaximumValue.u32,
2015 opd.FORM.Range.StepSize.u32);
2016 break;
Linus Walleij22cc4872007-11-05 22:57:19 +00002017 case PTP_OPFF_Enumeration:
Linus Walleij14830342007-03-23 13:32:00 +00002018 printf(" enumeration: ");
2019 for(k=0;k<opd.FORM.Enum.NumberOfValues;k++) {
2020 printf("%d, ", opd.FORM.Enum.SupportedValue[k].u32);
2021 }
2022 break;
2023 default:
2024 printf(" ANY 32BIT VALUE form");
2025 break;
2026 }
2027 break;
2028
2029 case PTP_DTC_INT64:
2030 printf(" INT64 data type");
2031 break;
2032
2033 case PTP_DTC_UINT64:
2034 printf(" UINT64 data type");
2035 break;
2036
2037 case PTP_DTC_INT128:
2038 printf(" INT128 data type");
2039 break;
2040
2041 case PTP_DTC_UINT128:
2042 printf(" UINT128 data type");
2043 break;
2044
2045 default:
2046 printf(" UNKNOWN data type");
2047 break;
2048 }
2049 }
2050 if (opd.GetSet) {
2051 printf(" GET/SET");
2052 } else {
2053 printf(" READ ONLY");
2054 }
2055 printf("\n");
Linus Walleijdbcc8242007-08-05 22:31:26 +00002056 ptp_free_objectpropdesc(&opd);
Linus Walleij0af979a2006-06-19 11:49:10 +00002057 }
2058 free(props);
2059 }
2060 }
2061 }
mopoke96143402006-10-30 04:37:26 +00002062
Linus Walleije1ac07e2006-12-14 19:38:59 +00002063 if(storage != NULL && ptp_operation_issupported(params,PTP_OC_GetStorageInfo)) {
Linus Walleij9e1b0812006-12-12 19:22:02 +00002064 printf("Storage Devices:\n");
Linus Walleije1ac07e2006-12-14 19:38:59 +00002065 while(storage != NULL) {
2066 printf(" StorageID: 0x%08x\n",storage->id);
2067 printf(" StorageType: 0x%04x\n",storage->StorageType);
2068 printf(" FilesystemType: 0x%04x\n",storage->FilesystemType);
2069 printf(" AccessCapability: 0x%04x\n",storage->AccessCapability);
Linus Walleijfec4d562008-06-01 22:30:36 +00002070 printf(" MaxCapacity: %llu\n", (long long unsigned int) storage->MaxCapacity);
2071 printf(" FreeSpaceInBytes: %llu\n", (long long unsigned int) storage->FreeSpaceInBytes);
2072 printf(" FreeSpaceInObjects: %llu\n", (long long unsigned int) storage->FreeSpaceInObjects);
Linus Walleije1ac07e2006-12-14 19:38:59 +00002073 printf(" StorageDescription: %s\n",storage->StorageDescription);
2074 printf(" VolumeIdentifier: %s\n",storage->VolumeIdentifier);
2075 storage = storage->next;
Linus Walleij9e1b0812006-12-12 19:22:02 +00002076 }
2077 }
2078
Linus Walleij545c7792006-06-13 15:22:30 +00002079 printf("Special directories:\n");
2080 printf(" Default music folder: 0x%08x\n", device->default_music_folder);
2081 printf(" Default playlist folder: 0x%08x\n", device->default_playlist_folder);
2082 printf(" Default picture folder: 0x%08x\n", device->default_picture_folder);
2083 printf(" Default video folder: 0x%08x\n", device->default_video_folder);
2084 printf(" Default organizer folder: 0x%08x\n", device->default_organizer_folder);
2085 printf(" Default zencast folder: 0x%08x\n", device->default_zencast_folder);
Linus Walleijccf28ce2006-11-16 16:06:38 +00002086 printf(" Default album folder: 0x%08x\n", device->default_album_folder);
Linus Walleij9316e652006-12-07 09:55:21 +00002087 printf(" Default text folder: 0x%08x\n", device->default_text_folder);
Linus Walleij8c45b292006-04-26 14:12:44 +00002088}
2089
2090/**
Linus Walleij5d533bb2007-07-17 21:48:57 +00002091 * This resets a device in case it supports the <code>PTP_OC_ResetDevice</code>
2092 * operation code (0x1010).
2093 * @param device a pointer to the device to reset.
2094 * @return 0 on success, any other value means failure.
2095 */
2096int LIBMTP_Reset_Device(LIBMTP_mtpdevice_t *device)
2097{
2098 PTPParams *params = (PTPParams *) device->params;
2099 uint16_t ret;
2100
2101 if (!ptp_operation_issupported(params,PTP_OC_ResetDevice)) {
2102 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL,
2103 "LIBMTP_Reset_Device(): device does not support resetting.");
2104 return -1;
2105 }
2106 ret = ptp_resetdevice(params);
2107 if (ret != PTP_RC_OK) {
2108 add_ptp_error_to_errorstack(device, ret, "Error resetting.");
2109 return -1;
2110 }
2111 return 0;
2112}
2113
2114/**
Linus Walleij2350b712008-01-14 22:54:37 +00002115 * This retrieves the manufacturer name of an MTP device.
2116 * @param device a pointer to the device to get the manufacturer name for.
2117 * @return a newly allocated UTF-8 string representing the manufacturer name.
2118 * The string must be freed by the caller after use. If the call
2119 * was unsuccessful this will contain NULL.
2120 */
2121char *LIBMTP_Get_Manufacturername(LIBMTP_mtpdevice_t *device)
2122{
2123 char *retmanuf = NULL;
2124 PTPParams *params = (PTPParams *) device->params;
2125
2126 if (params->deviceinfo.Manufacturer != NULL) {
2127 retmanuf = strdup(params->deviceinfo.Manufacturer);
2128 }
2129 return retmanuf;
2130}
2131
2132/**
mopoke96143402006-10-30 04:37:26 +00002133 * This retrieves the model name (often equal to product name)
Linus Walleij80124062006-03-15 10:26:09 +00002134 * of an MTP device.
2135 * @param device a pointer to the device to get the model name for.
2136 * @return a newly allocated UTF-8 string representing the model name.
2137 * The string must be freed by the caller after use. If the call
2138 * was unsuccessful this will contain NULL.
2139 */
2140char *LIBMTP_Get_Modelname(LIBMTP_mtpdevice_t *device)
2141{
2142 char *retmodel = NULL;
Linus Walleij9b28da32006-03-16 13:47:58 +00002143 PTPParams *params = (PTPParams *) device->params;
mopoke96143402006-10-30 04:37:26 +00002144
Linus Walleij9b28da32006-03-16 13:47:58 +00002145 if (params->deviceinfo.Model != NULL) {
2146 retmodel = strdup(params->deviceinfo.Model);
Linus Walleij80124062006-03-15 10:26:09 +00002147 }
2148 return retmodel;
2149}
2150
2151/**
2152 * This retrieves the serial number of an MTP device.
2153 * @param device a pointer to the device to get the serial number for.
2154 * @return a newly allocated UTF-8 string representing the serial number.
2155 * The string must be freed by the caller after use. If the call
2156 * was unsuccessful this will contain NULL.
2157 */
2158char *LIBMTP_Get_Serialnumber(LIBMTP_mtpdevice_t *device)
2159{
2160 char *retnumber = NULL;
Linus Walleij9b28da32006-03-16 13:47:58 +00002161 PTPParams *params = (PTPParams *) device->params;
mopoke96143402006-10-30 04:37:26 +00002162
Linus Walleij9b28da32006-03-16 13:47:58 +00002163 if (params->deviceinfo.SerialNumber != NULL) {
2164 retnumber = strdup(params->deviceinfo.SerialNumber);
Linus Walleij80124062006-03-15 10:26:09 +00002165 }
2166 return retnumber;
2167}
2168
2169/**
mopoke96143402006-10-30 04:37:26 +00002170 * This retrieves the device version (hardware and firmware version) of an
Linus Walleij80124062006-03-15 10:26:09 +00002171 * MTP device.
2172 * @param device a pointer to the device to get the device version for.
2173 * @return a newly allocated UTF-8 string representing the device version.
2174 * The string must be freed by the caller after use. If the call
2175 * was unsuccessful this will contain NULL.
2176 */
2177char *LIBMTP_Get_Deviceversion(LIBMTP_mtpdevice_t *device)
2178{
2179 char *retversion = NULL;
Linus Walleij9b28da32006-03-16 13:47:58 +00002180 PTPParams *params = (PTPParams *) device->params;
mopoke96143402006-10-30 04:37:26 +00002181
Linus Walleij9b28da32006-03-16 13:47:58 +00002182 if (params->deviceinfo.DeviceVersion != NULL) {
2183 retversion = strdup(params->deviceinfo.DeviceVersion);
Linus Walleij80124062006-03-15 10:26:09 +00002184 }
2185 return retversion;
2186}
2187
2188
2189/**
Linus Walleijfae27482006-08-19 20:13:25 +00002190 * This retrieves the "friendly name" of an MTP device. Usually
2191 * this is simply the name of the owner or something like
Linus Walleij30658792006-08-19 22:18:55 +00002192 * "John Doe's Digital Audio Player". This property should be supported
Linus Walleijfae27482006-08-19 20:13:25 +00002193 * by all MTP devices.
2194 * @param device a pointer to the device to get the friendly name for.
mopoke96143402006-10-30 04:37:26 +00002195 * @return a newly allocated UTF-8 string representing the friendly name.
Linus Walleijb9256fd2006-02-15 09:40:43 +00002196 * The string must be freed by the caller after use.
Linus Walleij30658792006-08-19 22:18:55 +00002197 * @see LIBMTP_Set_Friendlyname()
Linus Walleijb9256fd2006-02-15 09:40:43 +00002198 */
Linus Walleij30658792006-08-19 22:18:55 +00002199char *LIBMTP_Get_Friendlyname(LIBMTP_mtpdevice_t *device)
Linus Walleijb9256fd2006-02-15 09:40:43 +00002200{
Linus Walleijb02a0662006-04-25 08:05:09 +00002201 PTPPropertyValue propval;
Linus Walleijb9256fd2006-02-15 09:40:43 +00002202 char *retstring = NULL;
Linus Walleij9b28da32006-03-16 13:47:58 +00002203 PTPParams *params = (PTPParams *) device->params;
Linus Walleij070e9b42007-01-22 08:49:28 +00002204 uint16_t ret;
Linus Walleijb9256fd2006-02-15 09:40:43 +00002205
Linus Walleijcf223e62006-06-19 09:31:53 +00002206 if (!ptp_property_issupported(params, PTP_DPC_MTP_DeviceFriendlyName)) {
2207 return NULL;
2208 }
2209
Linus Walleij070e9b42007-01-22 08:49:28 +00002210 ret = ptp_getdevicepropvalue(params,
2211 PTP_DPC_MTP_DeviceFriendlyName,
2212 &propval,
2213 PTP_DTC_STR);
2214 if (ret != PTP_RC_OK) {
2215 add_ptp_error_to_errorstack(device, ret, "Error getting friendlyname.");
Linus Walleijb9256fd2006-02-15 09:40:43 +00002216 return NULL;
2217 }
Linus Walleija823a702006-08-27 21:27:46 +00002218 if (propval.str != NULL) {
2219 retstring = strdup(propval.str);
2220 free(propval.str);
2221 }
Linus Walleijfae27482006-08-19 20:13:25 +00002222 return retstring;
2223}
2224
2225/**
Linus Walleij30658792006-08-19 22:18:55 +00002226 * Sets the "friendly name" of an MTP device.
2227 * @param device a pointer to the device to set the friendly name for.
2228 * @param friendlyname the new friendly name for the device.
2229 * @return 0 on success, any other value means failure.
2230 * @see LIBMTP_Get_Ownername()
2231 */
2232int LIBMTP_Set_Friendlyname(LIBMTP_mtpdevice_t *device,
2233 char const * const friendlyname)
2234{
2235 PTPPropertyValue propval;
2236 PTPParams *params = (PTPParams *) device->params;
Linus Walleij070e9b42007-01-22 08:49:28 +00002237 uint16_t ret;
Linus Walleij30658792006-08-19 22:18:55 +00002238
2239 if (!ptp_property_issupported(params, PTP_DPC_MTP_DeviceFriendlyName)) {
2240 return -1;
2241 }
Linus Walleija823a702006-08-27 21:27:46 +00002242 propval.str = (char *) friendlyname;
Linus Walleij070e9b42007-01-22 08:49:28 +00002243 ret = ptp_setdevicepropvalue(params,
2244 PTP_DPC_MTP_DeviceFriendlyName,
2245 &propval,
2246 PTP_DTC_STR);
2247 if (ret != PTP_RC_OK) {
2248 add_ptp_error_to_errorstack(device, ret, "Error setting friendlyname.");
Linus Walleij30658792006-08-19 22:18:55 +00002249 return -1;
2250 }
Linus Walleij30658792006-08-19 22:18:55 +00002251 return 0;
2252}
2253
2254/**
Linus Walleijfae27482006-08-19 20:13:25 +00002255 * This retrieves the syncronization partner of an MTP device. This
2256 * property should be supported by all MTP devices.
2257 * @param device a pointer to the device to get the sync partner for.
2258 * @return a newly allocated UTF-8 string representing the synchronization
2259 * partner. The string must be freed by the caller after use.
Linus Walleij30658792006-08-19 22:18:55 +00002260 * @see LIBMTP_Set_Syncpartner()
Linus Walleijfae27482006-08-19 20:13:25 +00002261 */
2262char *LIBMTP_Get_Syncpartner(LIBMTP_mtpdevice_t *device)
2263{
2264 PTPPropertyValue propval;
2265 char *retstring = NULL;
2266 PTPParams *params = (PTPParams *) device->params;
Linus Walleij070e9b42007-01-22 08:49:28 +00002267 uint16_t ret;
Linus Walleijfae27482006-08-19 20:13:25 +00002268
2269 if (!ptp_property_issupported(params, PTP_DPC_MTP_SynchronizationPartner)) {
2270 return NULL;
2271 }
2272
Linus Walleij070e9b42007-01-22 08:49:28 +00002273 ret = ptp_getdevicepropvalue(params,
2274 PTP_DPC_MTP_SynchronizationPartner,
2275 &propval,
2276 PTP_DTC_STR);
2277 if (ret != PTP_RC_OK) {
2278 add_ptp_error_to_errorstack(device, ret, "Error getting syncpartner.");
Linus Walleijfae27482006-08-19 20:13:25 +00002279 return NULL;
2280 }
Linus Walleija823a702006-08-27 21:27:46 +00002281 if (propval.str != NULL) {
2282 retstring = strdup(propval.str);
2283 free(propval.str);
2284 }
Linus Walleijb9256fd2006-02-15 09:40:43 +00002285 return retstring;
2286}
2287
Linus Walleij30658792006-08-19 22:18:55 +00002288
2289/**
2290 * Sets the synchronization partner of an MTP device. Note that
2291 * we have no idea what the effect of setting this to "foobar"
2292 * may be. But the general idea seems to be to tell which program
2293 * shall synchronize with this device and tell others to leave
2294 * it alone.
2295 * @param device a pointer to the device to set the sync partner for.
2296 * @param syncpartner the new synchronization partner for the device.
2297 * @return 0 on success, any other value means failure.
2298 * @see LIBMTP_Get_Syncpartner()
2299 */
2300int LIBMTP_Set_Syncpartner(LIBMTP_mtpdevice_t *device,
2301 char const * const syncpartner)
2302{
2303 PTPPropertyValue propval;
2304 PTPParams *params = (PTPParams *) device->params;
Linus Walleij070e9b42007-01-22 08:49:28 +00002305 uint16_t ret;
mopoke96143402006-10-30 04:37:26 +00002306
Linus Walleij30658792006-08-19 22:18:55 +00002307 if (!ptp_property_issupported(params, PTP_DPC_MTP_SynchronizationPartner)) {
2308 return -1;
2309 }
Linus Walleija823a702006-08-27 21:27:46 +00002310 propval.str = (char *) syncpartner;
Linus Walleij070e9b42007-01-22 08:49:28 +00002311 ret = ptp_setdevicepropvalue(params,
2312 PTP_DPC_MTP_SynchronizationPartner,
2313 &propval,
2314 PTP_DTC_STR);
2315 if (ret != PTP_RC_OK) {
2316 add_ptp_error_to_errorstack(device, ret, "Error setting syncpartner.");
Linus Walleij30658792006-08-19 22:18:55 +00002317 return -1;
2318 }
Linus Walleij30658792006-08-19 22:18:55 +00002319 return 0;
2320}
2321
Linus Walleij394bbbe2006-02-22 16:10:53 +00002322/**
Linus Walleijf5fcda32006-12-03 22:31:02 +00002323 * Checks if the device can stora a file of this size or
2324 * if it's too big.
2325 * @param device a pointer to the device.
2326 * @param filesize the size of the file to check whether it will fit.
Linus Walleij6bf68b42007-09-03 22:50:02 +00002327 * @param storageid the ID of the storage to try to fit the file on.
Linus Walleijf5fcda32006-12-03 22:31:02 +00002328 * @return 0 if the file fits, any other value means failure.
2329 */
Linus Walleij6bf68b42007-09-03 22:50:02 +00002330static int check_if_file_fits(LIBMTP_mtpdevice_t *device,
2331 LIBMTP_devicestorage_t *storage,
2332 uint64_t const filesize) {
Linus Walleijf5fcda32006-12-03 22:31:02 +00002333 PTPParams *params = (PTPParams *) device->params;
Linus Walleijf5fcda32006-12-03 22:31:02 +00002334 uint64_t freebytes;
Linus Walleijf5fcda32006-12-03 22:31:02 +00002335 int ret;
2336
2337 // If we cannot check the storage, no big deal.
2338 if (!ptp_operation_issupported(params,PTP_OC_GetStorageInfo)) {
2339 return 0;
2340 }
2341
Linus Walleij6bf68b42007-09-03 22:50:02 +00002342 ret = get_storage_freespace(device, storage, &freebytes);
Linus Walleijf5fcda32006-12-03 22:31:02 +00002343 if (ret != 0) {
Linus Walleij6bf68b42007-09-03 22:50:02 +00002344 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL,
2345 "check_if_file_fits(): error checking free storage.");
Linus Walleijf5fcda32006-12-03 22:31:02 +00002346 return -1;
2347 } else {
Linus Walleijfb28b632007-10-23 21:56:18 +00002348 // Storage IDs with the lower 16 bits 0x0000 are not supposed
2349 // to be writeable.
2350 if ((storage->id & 0x0000FFFFU) == 0x00000000U) {
2351 return -1;
2352 }
2353 // See if it fits.
Linus Walleijf5fcda32006-12-03 22:31:02 +00002354 if (filesize > freebytes) {
Linus Walleijf5fcda32006-12-03 22:31:02 +00002355 return -1;
2356 }
2357 }
2358 return 0;
2359}
2360
2361
Linus Walleijf5fcda32006-12-03 22:31:02 +00002362/**
Linus Walleijfa1374c2006-02-27 07:41:46 +00002363 * This function retrieves the current battery level on the device.
2364 * @param device a pointer to the device to get the battery level for.
mopoke96143402006-10-30 04:37:26 +00002365 * @param maximum_level a pointer to a variable that will hold the
Linus Walleijfa1374c2006-02-27 07:41:46 +00002366 * maximum level of the battery if the call was successful.
mopoke96143402006-10-30 04:37:26 +00002367 * @param current_level a pointer to a variable that will hold the
Linus Walleijfa1374c2006-02-27 07:41:46 +00002368 * current level of the battery if the call was successful.
Linus Walleij545c7792006-06-13 15:22:30 +00002369 * A value of 0 means that the device is on external power.
Linus Walleijfa1374c2006-02-27 07:41:46 +00002370 * @return 0 if the storage info was successfully retrieved, any other
Linus Walleij80439342006-09-12 10:42:26 +00002371 * means failure. A typical cause of failure is that
Linus Walleij545c7792006-06-13 15:22:30 +00002372 * the device does not support the battery level property.
Linus Walleijfa1374c2006-02-27 07:41:46 +00002373 */
mopoke96143402006-10-30 04:37:26 +00002374int LIBMTP_Get_Batterylevel(LIBMTP_mtpdevice_t *device,
2375 uint8_t * const maximum_level,
Linus Walleijfa1374c2006-02-27 07:41:46 +00002376 uint8_t * const current_level)
2377{
Linus Walleijb02a0662006-04-25 08:05:09 +00002378 PTPPropertyValue propval;
Linus Walleijfa1374c2006-02-27 07:41:46 +00002379 uint16_t ret;
Linus Walleij9b28da32006-03-16 13:47:58 +00002380 PTPParams *params = (PTPParams *) device->params;
Linus Walleijfa1374c2006-02-27 07:41:46 +00002381
Linus Walleij545c7792006-06-13 15:22:30 +00002382 *maximum_level = 0;
2383 *current_level = 0;
2384
2385 if (!ptp_property_issupported(params, PTP_DPC_BatteryLevel)) {
2386 return -1;
2387 }
mopoke96143402006-10-30 04:37:26 +00002388
Linus Walleijb02a0662006-04-25 08:05:09 +00002389 ret = ptp_getdevicepropvalue(params, PTP_DPC_BatteryLevel, &propval, PTP_DTC_UINT8);
2390 if (ret != PTP_RC_OK) {
Linus Walleij070e9b42007-01-22 08:49:28 +00002391 add_ptp_error_to_errorstack(device, ret, "LIBMTP_Get_Batterylevel(): could not get device property value.");
Linus Walleijfa1374c2006-02-27 07:41:46 +00002392 return -1;
2393 }
mopoke96143402006-10-30 04:37:26 +00002394
Linus Walleijfa1374c2006-02-27 07:41:46 +00002395 *maximum_level = device->maximum_battery_level;
Linus Walleijb02a0662006-04-25 08:05:09 +00002396 *current_level = propval.u8;
mopoke96143402006-10-30 04:37:26 +00002397
Linus Walleijfa1374c2006-02-27 07:41:46 +00002398 return 0;
2399}
2400
Linus Walleij13374a42006-09-13 11:55:30 +00002401
2402/**
2403 * Formats device storage (if the device supports the operation).
2404 * WARNING: This WILL delete all data from the device. Make sure you've
2405 * got confirmation from the user BEFORE you call this function.
2406 *
Linus Walleijf8491912006-12-15 10:23:30 +00002407 * @param device a pointer to the device containing the storage to format.
2408 * @param storage the actual storage to format.
Linus Walleij13374a42006-09-13 11:55:30 +00002409 * @return 0 on success, any other value means failure.
2410 */
Linus Walleijf8491912006-12-15 10:23:30 +00002411int LIBMTP_Format_Storage(LIBMTP_mtpdevice_t *device, LIBMTP_devicestorage_t *storage)
Linus Walleij13374a42006-09-13 11:55:30 +00002412{
2413 uint16_t ret;
2414 PTPParams *params = (PTPParams *) device->params;
mopoke96143402006-10-30 04:37:26 +00002415
Linus Walleij13374a42006-09-13 11:55:30 +00002416 if (!ptp_operation_issupported(params,PTP_OC_FormatStore)) {
Linus Walleij5d533bb2007-07-17 21:48:57 +00002417 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL,
2418 "LIBMTP_Format_Storage(): device does not support formatting storage.");
Linus Walleij13374a42006-09-13 11:55:30 +00002419 return -1;
2420 }
Linus Walleijf8491912006-12-15 10:23:30 +00002421 ret = ptp_formatstore(params, storage->id);
Linus Walleij13374a42006-09-13 11:55:30 +00002422 if (ret != PTP_RC_OK) {
Linus Walleij070e9b42007-01-22 08:49:28 +00002423 add_ptp_error_to_errorstack(device, ret, "LIBMTP_Format_Storage(): failed to format storage.");
Linus Walleij13374a42006-09-13 11:55:30 +00002424 return -1;
2425 }
2426 return 0;
2427}
2428
Linus Walleijfa1374c2006-02-27 07:41:46 +00002429/**
Linus Walleij545c7792006-06-13 15:22:30 +00002430 * Helper function to extract a unicode property off a device.
Linus Walleije46f12e2006-06-22 17:53:25 +00002431 * This is the standard way of retrieveing unicode device
2432 * properties as described by the PTP spec.
Linus Walleijcf223e62006-06-19 09:31:53 +00002433 * @param device a pointer to the device to get the property from.
mopoke96143402006-10-30 04:37:26 +00002434 * @param unicstring a pointer to a pointer that will hold the
Linus Walleijcf223e62006-06-19 09:31:53 +00002435 * property after this call is completed.
2436 * @param property the property to retrieve.
2437 * @return 0 on success, any other value means failure.
Linus Walleij545c7792006-06-13 15:22:30 +00002438 */
mopoke96143402006-10-30 04:37:26 +00002439static int get_device_unicode_property(LIBMTP_mtpdevice_t *device,
Linus Walleijcf223e62006-06-19 09:31:53 +00002440 char **unicstring, uint16_t property)
Linus Walleij545c7792006-06-13 15:22:30 +00002441{
2442 PTPPropertyValue propval;
2443 PTPParams *params = (PTPParams *) device->params;
Linus Walleij16571dc2006-08-17 20:27:46 +00002444 uint16_t *tmp;
Linus Walleij070e9b42007-01-22 08:49:28 +00002445 uint16_t ret;
Linus Walleij545c7792006-06-13 15:22:30 +00002446 int i;
2447
2448 if (!ptp_property_issupported(params, property)) {
2449 return -1;
2450 }
2451
Linus Walleijcf223e62006-06-19 09:31:53 +00002452 // Unicode strings are 16bit unsigned integer arrays.
Linus Walleij070e9b42007-01-22 08:49:28 +00002453 ret = ptp_getdevicepropvalue(params,
2454 property,
2455 &propval,
2456 PTP_DTC_AUINT16);
2457 if (ret != PTP_RC_OK) {
2458 // TODO: add a note on WHICH property that we failed to get.
tedbullock4e51cb92007-02-15 11:48:34 +00002459 *unicstring = NULL;
Linus Walleij070e9b42007-01-22 08:49:28 +00002460 add_ptp_error_to_errorstack(device, ret, "get_device_unicode_property(): failed to get unicode property.");
Linus Walleij545c7792006-06-13 15:22:30 +00002461 return -1;
2462 }
2463
2464 // Extract the actual array.
Linus Walleij16571dc2006-08-17 20:27:46 +00002465 // printf("Array of %d elements\n", propval.a.count);
2466 tmp = malloc((propval.a.count + 1)*sizeof(uint16_t));
Linus Walleij545c7792006-06-13 15:22:30 +00002467 for (i = 0; i < propval.a.count; i++) {
Linus Walleij16571dc2006-08-17 20:27:46 +00002468 tmp[i] = propval.a.v[i].u16;
2469 // printf("%04x ", tmp[i]);
Linus Walleij545c7792006-06-13 15:22:30 +00002470 }
Linus Walleij16571dc2006-08-17 20:27:46 +00002471 tmp[propval.a.count] = 0x0000U;
Linus Walleij545c7792006-06-13 15:22:30 +00002472 free(propval.a.v);
2473
Linus Walleij3ec86312006-08-21 13:25:24 +00002474 *unicstring = utf16_to_utf8(device, tmp);
Linus Walleij16571dc2006-08-17 20:27:46 +00002475
Linus Walleij545c7792006-06-13 15:22:30 +00002476 free(tmp);
2477
2478 return 0;
2479}
2480
2481/**
2482 * This function returns the secure time as an XML document string from
2483 * the device.
2484 * @param device a pointer to the device to get the secure time for.
2485 * @param sectime the secure time string as an XML document or NULL if the call
2486 * failed or the secure time property is not supported. This string
2487 * must be <code>free()</code>:ed by the caller after use.
2488 * @return 0 on success, any other value means failure.
2489 */
Linus Walleij8ab54262006-06-21 07:12:28 +00002490int LIBMTP_Get_Secure_Time(LIBMTP_mtpdevice_t *device, char ** const sectime)
Linus Walleij545c7792006-06-13 15:22:30 +00002491{
2492 return get_device_unicode_property(device, sectime, PTP_DPC_MTP_SecureTime);
2493}
2494
2495/**
mopoke96143402006-10-30 04:37:26 +00002496 * This function returns the device (public key) certificate as an
Linus Walleij545c7792006-06-13 15:22:30 +00002497 * XML document string from the device.
2498 * @param device a pointer to the device to get the device certificate for.
2499 * @param devcert the device certificate as an XML string or NULL if the call
2500 * failed or the device certificate property is not supported. This
2501 * string must be <code>free()</code>:ed by the caller after use.
2502 * @return 0 on success, any other value means failure.
2503 */
Linus Walleij8ab54262006-06-21 07:12:28 +00002504int LIBMTP_Get_Device_Certificate(LIBMTP_mtpdevice_t *device, char ** const devcert)
Linus Walleij545c7792006-06-13 15:22:30 +00002505{
2506 return get_device_unicode_property(device, devcert, PTP_DPC_MTP_DeviceCertificate);
2507}
2508
2509/**
Linus Walleij8ab54262006-06-21 07:12:28 +00002510 * This function retrieves a list of supported file types, i.e. the file
2511 * types that this device claims it supports, e.g. audio file types that
2512 * the device can play etc. This list is mitigated to
2513 * inlcude the file types that libmtp can handle, i.e. it will not list
2514 * filetypes that libmtp will handle internally like playlists and folders.
2515 * @param device a pointer to the device to get the filetype capabilities for.
2516 * @param filetypes a pointer to a pointer that will hold the list of
2517 * supported filetypes if the call was successful. This list must
2518 * be <code>free()</code>:ed by the caller after use.
2519 * @param length a pointer to a variable that will hold the length of the
2520 * list of supported filetypes if the call was successful.
2521 * @return 0 on success, any other value means failure.
2522 * @see LIBMTP_Get_Filetype_Description()
2523 */
mopoke96143402006-10-30 04:37:26 +00002524int LIBMTP_Get_Supported_Filetypes(LIBMTP_mtpdevice_t *device, uint16_t ** const filetypes,
Linus Walleij8ab54262006-06-21 07:12:28 +00002525 uint16_t * const length)
2526{
2527 PTPParams *params = (PTPParams *) device->params;
Linus Walleija3544f62007-11-30 01:20:04 +00002528 PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo;
Linus Walleij8ab54262006-06-21 07:12:28 +00002529 uint16_t *localtypes;
2530 uint16_t localtypelen;
2531 uint32_t i;
mopoke96143402006-10-30 04:37:26 +00002532
Linus Walleij8ab54262006-06-21 07:12:28 +00002533 // This is more memory than needed if there are unknown types, but what the heck.
2534 localtypes = (uint16_t *) malloc(params->deviceinfo.ImageFormats_len * sizeof(uint16_t));
2535 localtypelen = 0;
mopoke96143402006-10-30 04:37:26 +00002536
Linus Walleij8ab54262006-06-21 07:12:28 +00002537 for (i=0;i<params->deviceinfo.ImageFormats_len;i++) {
2538 uint16_t localtype = map_ptp_type_to_libmtp_type(params->deviceinfo.ImageFormats[i]);
2539 if (localtype != LIBMTP_FILETYPE_UNKNOWN) {
2540 localtypes[localtypelen] = localtype;
2541 localtypelen++;
2542 }
2543 }
Linus Walleija3544f62007-11-30 01:20:04 +00002544 // The forgotten Ogg support on YP-10 and others...
Linus Walleijfec4d562008-06-01 22:30:36 +00002545 if (FLAG_OGG_IS_UNKNOWN(ptp_usb)) {
Linus Walleija3544f62007-11-30 01:20:04 +00002546 localtypes = (uint16_t *) realloc(localtypes, (params->deviceinfo.ImageFormats_len+1) * sizeof(uint16_t));
2547 localtypes[localtypelen] = LIBMTP_FILETYPE_OGG;
2548 localtypelen++;
2549 }
Linus Walleij8ab54262006-06-21 07:12:28 +00002550
2551 *filetypes = localtypes;
2552 *length = localtypelen;
2553
2554 return 0;
2555}
2556
raveloxd9a28642006-05-26 23:42:22 +00002557/**
Linus Walleij5b452bd2007-12-28 23:34:18 +00002558 * This function updates all the storage id's of a device and their
2559 * properties, then creates a linked list and puts the list head into
Linus Walleijf8491912006-12-15 10:23:30 +00002560 * the device struct. It also optionally sorts this list. If you want
2561 * to display storage information in your application you should call
Linus Walleij5b452bd2007-12-28 23:34:18 +00002562 * this function, then dereference the device struct
2563 * (<code>device-&gt;storage</code>) to get out information on the storage.
2564 *
2565 * You need to call this everytime you want to update the
2566 * <code>device-&gt;storage</code> list, for example anytime you need
2567 * to check available storage somewhere.
2568 *
2569 * <b>WARNING:</b> since this list is dynamically updated, do not
2570 * reference its fields in external applications by pointer! E.g
2571 * do not put a reference to any <code>char *</code> field. instead
2572 * <code>strncpy()</code> it!
Linus Walleijf8491912006-12-15 10:23:30 +00002573 *
Linus Walleije1b88e82008-07-02 20:12:53 +00002574 * @param device a pointer to the device to get the storage for.
Linus Walleij9e1b0812006-12-12 19:22:02 +00002575 * @param sortby an integer that determines the sorting of the storage list.
2576 * Valid sort methods are defined in libmtp.h with beginning with
2577 * LIBMTP_STORAGE_SORTBY_. 0 or LIBMTP_STORAGE_SORTBY_NOTSORTED to not
2578 * sort.
2579 * @return 0 on success, 1 success but only with storage id's, storage
2580 * properities could not be retrieved and -1 means failure.
Linus Walleij9e1b0812006-12-12 19:22:02 +00002581 */
2582int LIBMTP_Get_Storage(LIBMTP_mtpdevice_t *device, int const sortby)
2583{
2584 uint32_t i = 0;
2585 PTPStorageInfo storageInfo;
2586 PTPParams *params = (PTPParams *) device->params;
2587 PTPStorageIDs storageIDs;
Linus Walleije1ac07e2006-12-14 19:38:59 +00002588 LIBMTP_devicestorage_t *storage = NULL;
2589 LIBMTP_devicestorage_t *storageprev = NULL;
Linus Walleij9e1b0812006-12-12 19:22:02 +00002590
2591 if (device->storage != NULL)
2592 free_storage_list(device);
2593
2594 // if (!ptp_operation_issupported(params,PTP_OC_GetStorageIDs))
2595 // return -1;
Richard Lowbd14bf42007-07-21 11:25:11 +00002596 if (ptp_getstorageids (params, &storageIDs) != PTP_RC_OK)
Linus Walleij9e1b0812006-12-12 19:22:02 +00002597 return -1;
2598 if (storageIDs.n < 1)
2599 return -1;
2600
2601 if (!ptp_operation_issupported(params,PTP_OC_GetStorageInfo)) {
2602 for (i = 0; i < storageIDs.n; i++) {
2603
Linus Walleije1ac07e2006-12-14 19:38:59 +00002604 storage = (LIBMTP_devicestorage_t *) malloc(sizeof(LIBMTP_devicestorage_t));
2605 storage->prev = storageprev;
2606 if (storageprev != NULL)
2607 storageprev->next = storage;
Linus Walleij9e1b0812006-12-12 19:22:02 +00002608 if (device->storage == NULL)
Linus Walleije1ac07e2006-12-14 19:38:59 +00002609 device->storage = storage;
Linus Walleij9e1b0812006-12-12 19:22:02 +00002610
Linus Walleije1ac07e2006-12-14 19:38:59 +00002611 storage->id = storageIDs.Storage[i];
2612 storage->StorageType = PTP_ST_Undefined;
2613 storage->FilesystemType = PTP_FST_Undefined;
2614 storage->AccessCapability = PTP_AC_ReadWrite;
2615 storage->MaxCapacity = (uint64_t) -1;
2616 storage->FreeSpaceInBytes = (uint64_t) -1;
2617 storage->FreeSpaceInObjects = (uint64_t) -1;
2618 storage->StorageDescription = strdup("Unknown storage");
2619 storage->VolumeIdentifier = strdup("Unknown volume");
2620 storage->next = NULL;
Linus Walleij9e1b0812006-12-12 19:22:02 +00002621
Linus Walleije1ac07e2006-12-14 19:38:59 +00002622 storageprev = storage;
Linus Walleij9e1b0812006-12-12 19:22:02 +00002623 }
2624 free(storageIDs.Storage);
2625 return 1;
2626 } else {
2627 for (i = 0; i < storageIDs.n; i++) {
Linus Walleij070e9b42007-01-22 08:49:28 +00002628 uint16_t ret;
2629 ret = ptp_getstorageinfo(params, storageIDs.Storage[i], &storageInfo);
2630 if (ret != PTP_RC_OK) {
2631 add_ptp_error_to_errorstack(device, ret, "LIBMTP_Get_Storage(): Could not get storage info.");
Linus Walleij9e1b0812006-12-12 19:22:02 +00002632 if (device->storage != NULL) {
2633 free_storage_list(device);
2634 }
2635 return -1;
2636 }
2637
Linus Walleije1ac07e2006-12-14 19:38:59 +00002638 storage = (LIBMTP_devicestorage_t *) malloc(sizeof(LIBMTP_devicestorage_t));
2639 storage->prev = storageprev;
2640 if (storageprev != NULL)
2641 storageprev->next = storage;
Linus Walleij9e1b0812006-12-12 19:22:02 +00002642 if (device->storage == NULL)
Linus Walleije1ac07e2006-12-14 19:38:59 +00002643 device->storage = storage;
Linus Walleij9e1b0812006-12-12 19:22:02 +00002644
Linus Walleije1ac07e2006-12-14 19:38:59 +00002645 storage->id = storageIDs.Storage[i];
2646 storage->StorageType = storageInfo.StorageType;
2647 storage->FilesystemType = storageInfo.FilesystemType;
2648 storage->AccessCapability = storageInfo.AccessCapability;
2649 storage->MaxCapacity = storageInfo.MaxCapability;
2650 storage->FreeSpaceInBytes = storageInfo.FreeSpaceInBytes;
2651 storage->FreeSpaceInObjects = storageInfo.FreeSpaceInImages;
2652 storage->StorageDescription = storageInfo.StorageDescription;
2653 storage->VolumeIdentifier = storageInfo.VolumeLabel;
2654 storage->next = NULL;
Linus Walleij9e1b0812006-12-12 19:22:02 +00002655
Linus Walleije1ac07e2006-12-14 19:38:59 +00002656 storageprev = storage;
Linus Walleij9e1b0812006-12-12 19:22:02 +00002657 }
2658
Linus Walleije1ac07e2006-12-14 19:38:59 +00002659 storage->next = NULL;
Linus Walleij9e1b0812006-12-12 19:22:02 +00002660
2661 sort_storage_by(device,sortby);
2662 free(storageIDs.Storage);
2663 return 0;
2664 }
2665}
2666
2667/**
Linus Walleijf6bc1782006-03-24 15:12:47 +00002668 * This creates a new file metadata structure and allocates memory
2669 * for it. Notice that if you add strings to this structure they
2670 * will be freed by the corresponding <code>LIBMTP_destroy_file_t</code>
mopoke96143402006-10-30 04:37:26 +00002671 * operation later, so be careful of using strdup() when assigning
Linus Walleijf6bc1782006-03-24 15:12:47 +00002672 * strings, e.g.:
2673 *
2674 * <pre>
2675 * LIBMTP_file_t *file = LIBMTP_new_file_t();
2676 * file->filename = strdup(namestr);
2677 * ....
2678 * LIBMTP_destroy_file_t(file);
2679 * </pre>
2680 *
2681 * @return a pointer to the newly allocated metadata structure.
2682 * @see LIBMTP_destroy_file_t()
2683 */
2684LIBMTP_file_t *LIBMTP_new_file_t(void)
2685{
2686 LIBMTP_file_t *new = (LIBMTP_file_t *) malloc(sizeof(LIBMTP_file_t));
2687 if (new == NULL) {
2688 return NULL;
2689 }
2690 new->filename = NULL;
Linus Walleijea68f1f2008-06-22 21:54:44 +00002691 new->item_id = 0;
2692 new->parent_id = 0;
2693 new->storage_id = 0;
Linus Walleijf6bc1782006-03-24 15:12:47 +00002694 new->filesize = 0;
2695 new->filetype = LIBMTP_FILETYPE_UNKNOWN;
2696 new->next = NULL;
2697 return new;
2698}
2699
2700/**
2701 * This destroys a file metadata structure and deallocates the memory
mopoke96143402006-10-30 04:37:26 +00002702 * used by it, including any strings. Never use a file metadata
Linus Walleijf6bc1782006-03-24 15:12:47 +00002703 * structure again after calling this function on it.
2704 * @param file the file metadata to destroy.
2705 * @see LIBMTP_new_file_t()
2706 */
2707void LIBMTP_destroy_file_t(LIBMTP_file_t *file)
2708{
2709 if (file == NULL) {
2710 return;
2711 }
2712 if (file->filename != NULL)
2713 free(file->filename);
2714 free(file);
2715 return;
2716}
2717
2718/**
mopoke31364442006-11-20 04:53:04 +00002719* THIS FUNCTION IS DEPRECATED. PLEASE UPDATE YOUR CODE IN ORDER
Richard Lowdc0b6c72006-11-13 09:22:23 +00002720 * NOT TO USE IT.
2721 * @see LIBMTP_Get_Filelisting_With_Callback()
2722 */
2723LIBMTP_file_t *LIBMTP_Get_Filelisting(LIBMTP_mtpdevice_t *device)
2724{
2725 printf("WARNING: LIBMTP_Get_Filelisting() is deprecated.\n");
2726 printf("WARNING: please update your code to use LIBMTP_Get_Filelisting_With_Callback()\n");
2727 return LIBMTP_Get_Filelisting_With_Callback(device, NULL, NULL);
2728}
2729
2730/**
Linus Walleijf6bc1782006-03-24 15:12:47 +00002731 * This returns a long list of all files available
Linus Walleij25d5c212008-08-14 06:49:13 +00002732 * on the current MTP device. Folders will not be returned, but abstract
2733 * entities like playlists and albums will show up as "files". Typical usage:
Linus Walleijf6bc1782006-03-24 15:12:47 +00002734 *
2735 * <pre>
2736 * LIBMTP_file_t *filelist;
2737 *
Richard Lowdc0b6c72006-11-13 09:22:23 +00002738 * filelist = LIBMTP_Get_Filelisting_With_Callback(device, callback, data);
Linus Walleijf6bc1782006-03-24 15:12:47 +00002739 * while (filelist != NULL) {
2740 * LIBMTP_file_t *tmp;
2741 *
2742 * // Do something on each element in the list here...
2743 * tmp = filelist;
2744 * filelist = filelist->next;
2745 * LIBMTP_destroy_file_t(tmp);
2746 * }
2747 * </pre>
2748 *
Linus Walleij25d5c212008-08-14 06:49:13 +00002749 * If you want to group your file listing by storage (per storage unit) or
2750 * arrange files into folders, you must dereference the <code>storage_id</code>
2751 * and/or <code>parent_id</code> field of the returned <code>LIBMTP_file_t</code>
2752 * struct. To arrange by folders or files you typically have to create the proper
2753 * trees by calls to <code>LIBMTP_Get_Storage()</code> and/or
2754 * <code>LIBMTP_Get_Folder_List()</code> first.
2755 *
Linus Walleijf6bc1782006-03-24 15:12:47 +00002756 * @param device a pointer to the device to get the file listing for.
Richard Lowdc0b6c72006-11-13 09:22:23 +00002757 * @param callback a function to be called during the tracklisting retrieveal
Linus Walleij25d5c212008-08-14 06:49:13 +00002758 * for displaying progress bars etc, or NULL if you don't want
2759 * any callbacks.
Richard Lowdc0b6c72006-11-13 09:22:23 +00002760 * @param data a user-defined pointer that is passed along to
Linus Walleij25d5c212008-08-14 06:49:13 +00002761 * the <code>progress</code> function in order to
2762 * pass along some user defined data to the progress
2763 * updates. If not used, set this to NULL.
Linus Walleijf6bc1782006-03-24 15:12:47 +00002764 * @return a list of files that can be followed using the <code>next</code>
Linus Walleij25d5c212008-08-14 06:49:13 +00002765 * field of the <code>LIBMTP_file_t</code> data structure.
2766 * Each of the metadata tags must be freed after use, and may
2767 * contain only partial metadata information, i.e. one or several
2768 * fields may be NULL or 0.
Linus Walleij2e4b5f92006-06-16 14:00:49 +00002769 * @see LIBMTP_Get_Filemetadata()
Linus Walleijf6bc1782006-03-24 15:12:47 +00002770 */
Richard Lowdc0b6c72006-11-13 09:22:23 +00002771LIBMTP_file_t *LIBMTP_Get_Filelisting_With_Callback(LIBMTP_mtpdevice_t *device,
2772 LIBMTP_progressfunc_t const callback,
2773 void const * const data)
Linus Walleijf6bc1782006-03-24 15:12:47 +00002774{
2775 uint32_t i = 0;
2776 LIBMTP_file_t *retfiles = NULL;
2777 LIBMTP_file_t *curfile = NULL;
2778 PTPParams *params = (PTPParams *) device->params;
Linus Walleijd9d28d52007-08-04 19:01:18 +00002779 PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo;
Linus Walleijdbcc8242007-08-05 22:31:26 +00002780 uint16_t ret;
Linus Walleij438bd7f2006-06-08 11:35:44 +00002781
mopoke96143402006-10-30 04:37:26 +00002782 // Get all the handles if we haven't already done that
Linus Walleijf6bc1782006-03-24 15:12:47 +00002783 if (params->handles.Handler == NULL) {
Linus Walleij438bd7f2006-06-08 11:35:44 +00002784 flush_handles(device);
Linus Walleijf6bc1782006-03-24 15:12:47 +00002785 }
mopoke96143402006-10-30 04:37:26 +00002786
Linus Walleijf6bc1782006-03-24 15:12:47 +00002787 for (i = 0; i < params->handles.n; i++) {
Linus Walleij070e9b42007-01-22 08:49:28 +00002788 LIBMTP_file_t *file;
Linus Walleijf0bf4372007-07-01 21:47:38 +00002789 PTPObjectInfo *oi;
Linus Walleijf6bc1782006-03-24 15:12:47 +00002790
Richard Lowdc0b6c72006-11-13 09:22:23 +00002791 if (callback != NULL)
2792 callback(i, params->handles.n, data);
mopoke31364442006-11-20 04:53:04 +00002793
Linus Walleijf0bf4372007-07-01 21:47:38 +00002794 oi = &params->objectinfo[i];
Linus Walleijf6bc1782006-03-24 15:12:47 +00002795
Linus Walleijf0bf4372007-07-01 21:47:38 +00002796 if (oi->ObjectFormat == PTP_OFC_Association) {
Linus Walleijd9d28d52007-08-04 19:01:18 +00002797 // MTP use this object format for folders which means
Linus Walleijf0bf4372007-07-01 21:47:38 +00002798 // these "files" will turn up on a folder listing instead.
2799 continue;
Linus Walleijf6bc1782006-03-24 15:12:47 +00002800 }
2801
Linus Walleijf0bf4372007-07-01 21:47:38 +00002802 // Allocate a new file type
2803 file = LIBMTP_new_file_t();
2804
2805 file->parent_id = oi->ParentObject;
Linus Walleijea68f1f2008-06-22 21:54:44 +00002806 file->storage_id = oi->StorageID;
Linus Walleijf0bf4372007-07-01 21:47:38 +00002807
Linus Walleijd9d28d52007-08-04 19:01:18 +00002808 // This is some sort of unique ID so we can keep track of the track.
2809 file->item_id = params->handles.Handler[i];
2810
Linus Walleijf0bf4372007-07-01 21:47:38 +00002811 // Set the filetype
2812 file->filetype = map_ptp_type_to_libmtp_type(oi->ObjectFormat);
2813
2814 // Original file-specific properties
Linus Walleijd9d28d52007-08-04 19:01:18 +00002815 // We only have 32-bit file size here; if we find it, we use the
2816 // PTP_OPC_ObjectSize property which has 64bit precision.
Linus Walleijf0bf4372007-07-01 21:47:38 +00002817 file->filesize = oi->ObjectCompressedSize;
2818 if (oi->Filename != NULL) {
2819 file->filename = strdup(oi->Filename);
2820 }
2821
Linus Walleijd9d28d52007-08-04 19:01:18 +00002822 /*
2823 * If we have a cached, large set of metadata, then use it!
2824 */
Linus Walleij1e9a0332007-09-12 19:35:56 +00002825 if (params->props) {
2826 MTPProperties *prop = params->props;
2827 int i;
Linus Walleijd9d28d52007-08-04 19:01:18 +00002828
Linus Walleij1e9a0332007-09-12 19:35:56 +00002829 for (i=0;(i<params->nrofprops) && (prop->ObjectHandle != file->item_id);i++,prop++)
2830 /*empty*/;
2831 for (;i<params->nrofprops;i++) {
2832 if (prop->ObjectHandle != file->item_id)
2833 break;
2834
Linus Walleijd9d28d52007-08-04 19:01:18 +00002835 // Pick ObjectSize here...
2836 if (prop->property == PTP_OPC_ObjectSize) {
Linus Walleijddaba2f2007-10-02 21:20:30 +00002837 if (device->object_bitsize == 64) {
Linus Walleijca2a1702007-10-02 20:41:45 +00002838 file->filesize = prop->propval.u64;
2839 } else {
Linus Walleijddaba2f2007-10-02 21:20:30 +00002840 file->filesize = prop->propval.u32;
Linus Walleijca2a1702007-10-02 20:41:45 +00002841 }
Linus Walleijd9d28d52007-08-04 19:01:18 +00002842 break;
2843 }
Linus Walleij1e9a0332007-09-12 19:35:56 +00002844 prop ++;
Linus Walleijd9d28d52007-08-04 19:01:18 +00002845 }
2846 } else if (ptp_operation_issupported(params,PTP_OC_MTP_GetObjPropList)
Linus Walleijfec4d562008-06-01 22:30:36 +00002847 && !FLAG_BROKEN_MTPGETOBJPROPLIST(ptp_usb)) {
Linus Walleij1e9a0332007-09-12 19:35:56 +00002848 MTPProperties *props = NULL;
2849 MTPProperties *prop;
Linus Walleijca2a1702007-10-02 20:41:45 +00002850
Linus Walleij1e9a0332007-09-12 19:35:56 +00002851 int nrofprops;
Linus Walleijd9d28d52007-08-04 19:01:18 +00002852
2853 /*
2854 * This should retrieve all properties for an object, but on devices
2855 * which are inherently broken it will not, so these need the
2856 * special flag DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST.
2857 */
Linus Walleij1e9a0332007-09-12 19:35:56 +00002858 ret = ptp_mtp_getobjectproplist(params, file->item_id, &props, &nrofprops);
Linus Walleijd9d28d52007-08-04 19:01:18 +00002859 if (ret != PTP_RC_OK) {
2860 add_ptp_error_to_errorstack(device, ret, "LIBMTP_Get_Filelisting_With_Callback(): call to ptp_mtp_getobjectproplist() failed.");
2861 // Silently fall through.
2862 }
Linus Walleijfcb43422008-05-23 21:53:55 +00002863 if (props == NULL && nrofprops != 0) {
2864 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL,
2865 "LIBMTP_Get_Filelisting_With_Callback: "
2866 "call to ptp_mtp_getobjectproplist() returned "
2867 "inconsistent results.");
2868 nrofprops = 0;
2869 }
Richard Lowd7bcab52007-09-16 16:30:20 +00002870 if (props != NULL) {
2871 int i;
2872 prop = props;
2873 for (i=0;i<nrofprops;i++) {
2874 if (prop->ObjectHandle != file->item_id)
2875 break;
2876 // Pick ObjectSize here...
2877 if (prop->property == PTP_OPC_ObjectSize) {
Linus Walleijddaba2f2007-10-02 21:20:30 +00002878 if (device->object_bitsize == 64) {
Linus Walleijca2a1702007-10-02 20:41:45 +00002879 file->filesize = prop->propval.u64;
2880 } else {
Linus Walleijddaba2f2007-10-02 21:20:30 +00002881 file->filesize = prop->propval.u32;
Linus Walleijca2a1702007-10-02 20:41:45 +00002882 }
Richard Lowd7bcab52007-09-16 16:30:20 +00002883 break;
2884 }
2885 prop ++;
2886 }
Linus Walleija6d0d482007-10-31 08:54:56 +00002887 ptp_destroy_object_prop_list(props, nrofprops);
Linus Walleijd9d28d52007-08-04 19:01:18 +00002888 }
Linus Walleijd9d28d52007-08-04 19:01:18 +00002889 } else {
2890 uint16_t *props = NULL;
2891 uint32_t propcnt = 0;
2892
2893 // First see which properties can be retrieved for this object format
Linus Walleijca2a1702007-10-02 20:41:45 +00002894 ret = ptp_mtp_getobjectpropssupported(params, oi->ObjectFormat, &propcnt, &props);
Linus Walleijd9d28d52007-08-04 19:01:18 +00002895 if (ret != PTP_RC_OK) {
2896 add_ptp_error_to_errorstack(device, ret, "LIBMTP_Get_Filelisting_With_Callback(): call to ptp_mtp_getobjectpropssupported() failed.");
2897 // Silently fall through.
2898 } else {
Richard Lowd7bcab52007-09-16 16:30:20 +00002899 int i;
Linus Walleijd9d28d52007-08-04 19:01:18 +00002900 for (i=0;i<propcnt;i++) {
2901 switch (props[i]) {
2902 case PTP_OPC_ObjectSize:
Linus Walleijddaba2f2007-10-02 21:20:30 +00002903 if (device->object_bitsize == 64) {
2904 file->filesize = get_u64_from_object(device, file->item_id, PTP_OPC_ObjectSize, 0);
2905 } else {
2906 file->filesize = get_u32_from_object(device, file->item_id, PTP_OPC_ObjectSize, 0);
Linus Walleijca2a1702007-10-02 20:41:45 +00002907 }
Linus Walleijd9d28d52007-08-04 19:01:18 +00002908 break;
2909 default:
2910 break;
2911 }
2912 }
2913 free(props);
2914 }
2915 }
Linus Walleijd9d28d52007-08-04 19:01:18 +00002916
Linus Walleijf0bf4372007-07-01 21:47:38 +00002917 // Add track to a list that will be returned afterwards.
2918 if (retfiles == NULL) {
2919 retfiles = file;
2920 curfile = file;
2921 } else {
2922 curfile->next = file;
2923 curfile = file;
2924 }
2925
2926 // Call listing callback
2927 // double progressPercent = (double)i*(double)100.0 / (double)params->handles.n;
2928
Linus Walleijf6bc1782006-03-24 15:12:47 +00002929 } // Handle counting loop
2930 return retfiles;
2931}
2932
2933/**
Linus Walleij2e4b5f92006-06-16 14:00:49 +00002934 * This function retrieves the metadata for a single file off
2935 * the device.
2936 *
2937 * Do not call this function repeatedly! The file handles are linearly
2938 * searched O(n) and the call may involve (slow) USB traffic, so use
2939 * <code>LIBMTP_Get_Filelisting()</code> and cache the file, preferably
2940 * as an efficient data structure such as a hash list.
2941 *
Linus Walleij0801cd52008-01-29 22:19:15 +00002942 * Incidentally this function will return metadata for
2943 * a folder (association) as well, but this is not a proper use
2944 * of it, it is intended for file manipulation, not folder manipulation.
2945 *
Linus Walleij2e4b5f92006-06-16 14:00:49 +00002946 * @param device a pointer to the device to get the file metadata from.
2947 * @param fileid the object ID of the file that you want the metadata for.
2948 * @return a metadata entry on success or NULL on failure.
2949 * @see LIBMTP_Get_Filelisting()
2950 */
2951LIBMTP_file_t *LIBMTP_Get_Filemetadata(LIBMTP_mtpdevice_t *device, uint32_t const fileid)
2952{
2953 uint32_t i = 0;
2954 PTPParams *params = (PTPParams *) device->params;
Linus Walleijdbcc8242007-08-05 22:31:26 +00002955 PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo;
2956 uint16_t ret;
Linus Walleij2e4b5f92006-06-16 14:00:49 +00002957
mopoke96143402006-10-30 04:37:26 +00002958 // Get all the handles if we haven't already done that
Linus Walleij2e4b5f92006-06-16 14:00:49 +00002959 if (params->handles.Handler == NULL) {
2960 flush_handles(device);
2961 }
2962
2963 for (i = 0; i < params->handles.n; i++) {
2964 LIBMTP_file_t *file;
Linus Walleijf0bf4372007-07-01 21:47:38 +00002965 PTPObjectInfo *oi;
Linus Walleij2e4b5f92006-06-16 14:00:49 +00002966
2967 // Is this the file we're looking for?
2968 if (params->handles.Handler[i] != fileid) {
2969 continue;
2970 }
2971
Linus Walleijf0bf4372007-07-01 21:47:38 +00002972 oi = &params->objectinfo[i];
Linus Walleij070e9b42007-01-22 08:49:28 +00002973
Linus Walleijf0bf4372007-07-01 21:47:38 +00002974 // Allocate a new file type
2975 file = LIBMTP_new_file_t();
2976
2977 file->parent_id = oi->ParentObject;
Linus Walleijea68f1f2008-06-22 21:54:44 +00002978 file->storage_id = oi->StorageID;
Linus Walleijf0bf4372007-07-01 21:47:38 +00002979
2980 // Set the filetype
2981 file->filetype = map_ptp_type_to_libmtp_type(oi->ObjectFormat);
2982
2983 // Original file-specific properties
Richard Lowc3f5fc32007-07-29 09:40:50 +00002984
Linus Walleijdbcc8242007-08-05 22:31:26 +00002985 // We only have 32-bit file size here; later we use the PTP_OPC_ObjectSize property
Linus Walleijf0bf4372007-07-01 21:47:38 +00002986 file->filesize = oi->ObjectCompressedSize;
2987 if (oi->Filename != NULL) {
2988 file->filename = strdup(oi->Filename);
2989 }
Linus Walleijdbcc8242007-08-05 22:31:26 +00002990
2991 // This is some sort of unique ID so we can keep track of the file.
Linus Walleijf0bf4372007-07-01 21:47:38 +00002992 file->item_id = params->handles.Handler[i];
2993
Linus Walleijdbcc8242007-08-05 22:31:26 +00002994 /*
2995 * If we have a cached, large set of metadata, then use it!
2996 */
Linus Walleij1e9a0332007-09-12 19:35:56 +00002997 if (params->props) {
2998 MTPProperties *prop = params->props;
Linus Walleijdbcc8242007-08-05 22:31:26 +00002999
Linus Walleij1e9a0332007-09-12 19:35:56 +00003000 for (i=0;(i<params->nrofprops) && (prop->ObjectHandle != file->item_id);i++,prop++)
3001 /*empty*/;
3002 for (;(i<params->nrofprops) && (prop->ObjectHandle == file->item_id);i++,prop++) {
Linus Walleijdbcc8242007-08-05 22:31:26 +00003003 // Pick ObjectSize here...
3004 if (prop->property == PTP_OPC_ObjectSize) {
3005 // This may already be set, but this 64bit precision value
3006 // is better than the PTP 32bit value, so let it override.
Linus Walleijddaba2f2007-10-02 21:20:30 +00003007 if (device->object_bitsize == 64) {
3008 file->filesize = prop->propval.u64;
3009 } else {
3010 file->filesize = prop->propval.u32;
3011 }
Linus Walleijdbcc8242007-08-05 22:31:26 +00003012 break;
3013 }
Linus Walleijdbcc8242007-08-05 22:31:26 +00003014 }
3015 } else if (ptp_operation_issupported(params,PTP_OC_MTP_GetObjPropList)
Linus Walleijfec4d562008-06-01 22:30:36 +00003016 && !FLAG_BROKEN_MTPGETOBJPROPLIST(ptp_usb)) {
Linus Walleij1e9a0332007-09-12 19:35:56 +00003017 MTPProperties *props = NULL;
3018 MTPProperties *prop;
3019 int nrofprops;
Linus Walleijdbcc8242007-08-05 22:31:26 +00003020
3021 /*
3022 * This should retrieve all properties for an object, but on devices
3023 * which are inherently broken it will not, so these need the
3024 * special flag DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST.
3025 */
Linus Walleij1e9a0332007-09-12 19:35:56 +00003026 ret = ptp_mtp_getobjectproplist(params, file->item_id, &props, &nrofprops);
Linus Walleijdbcc8242007-08-05 22:31:26 +00003027 if (ret != PTP_RC_OK) {
Linus Walleijfcb43422008-05-23 21:53:55 +00003028 add_ptp_error_to_errorstack(device, ret, "LIBMTP_Get_Filelisting_With_Callback(): "
3029 "call to ptp_mtp_getobjectproplist() failed.");
Linus Walleijdbcc8242007-08-05 22:31:26 +00003030 // Silently fall through.
3031 }
Linus Walleijfcb43422008-05-23 21:53:55 +00003032 if (props == NULL && nrofprops != 0) {
3033 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL,
3034 "LIBMTP_Get_Filelisting_With_Callback(): "
3035 "call to ptp_mtp_getobjectproplist() returned "
3036 "inconsistent results.");
3037 return NULL;
3038 }
Linus Walleij1e9a0332007-09-12 19:35:56 +00003039 prop = props;
3040 for (i=0;i<nrofprops;i++) {
3041 if ((prop->ObjectHandle == file->item_id) && (prop->property == PTP_OPC_ObjectSize)) {
Linus Walleijdbcc8242007-08-05 22:31:26 +00003042 // This may already be set, but this 64bit precision value
3043 // is better than the PTP 32bit value, so let it override.
Linus Walleijddaba2f2007-10-02 21:20:30 +00003044 if (device->object_bitsize == 64) {
3045 file->filesize = prop->propval.u64;
3046 } else {
3047 file->filesize = prop->propval.u32;
3048 }
Linus Walleijdbcc8242007-08-05 22:31:26 +00003049 break;
3050 }
Linus Walleij1e9a0332007-09-12 19:35:56 +00003051 prop ++;
Linus Walleijdbcc8242007-08-05 22:31:26 +00003052 }
Linus Walleija6d0d482007-10-31 08:54:56 +00003053 ptp_destroy_object_prop_list(props, nrofprops);
Linus Walleijdbcc8242007-08-05 22:31:26 +00003054 } else {
3055 uint16_t *props = NULL;
3056 uint32_t propcnt = 0;
3057
3058 // First see which properties can be retrieved for this object format
3059 ret = ptp_mtp_getobjectpropssupported(params, map_libmtp_type_to_ptp_type(file->filetype), &propcnt, &props);
3060 if (ret != PTP_RC_OK) {
3061 add_ptp_error_to_errorstack(device, ret, "LIBMTP_Get_Filemetadata(): call to ptp_mtp_getobjectpropssupported() failed.");
3062 // Silently fall through.
3063 } else {
3064 for (i=0;i<propcnt;i++) {
3065 switch (props[i]) {
3066 case PTP_OPC_ObjectSize:
Linus Walleijddaba2f2007-10-02 21:20:30 +00003067 if (device->object_bitsize == 64) {
3068 file->filesize = get_u64_from_object(device, file->item_id, PTP_OPC_ObjectSize, 0);
3069 } else {
3070 file->filesize = get_u32_from_object(device, file->item_id, PTP_OPC_ObjectSize, 0);
3071 }
Linus Walleijdbcc8242007-08-05 22:31:26 +00003072 break;
3073 default:
3074 break;
3075 }
3076 }
3077 free(props);
3078 }
3079 }
3080
Linus Walleijf0bf4372007-07-01 21:47:38 +00003081 return file;
3082
Linus Walleij2e4b5f92006-06-16 14:00:49 +00003083 }
3084 return NULL;
3085}
3086
3087/**
Linus Walleij394bbbe2006-02-22 16:10:53 +00003088 * This creates a new track metadata structure and allocates memory
3089 * for it. Notice that if you add strings to this structure they
3090 * will be freed by the corresponding <code>LIBMTP_destroy_track_t</code>
mopoke96143402006-10-30 04:37:26 +00003091 * operation later, so be careful of using strdup() when assigning
Linus Walleij394bbbe2006-02-22 16:10:53 +00003092 * strings, e.g.:
3093 *
Linus Walleij17e39f72006-02-23 15:54:28 +00003094 * <pre>
Linus Walleij394bbbe2006-02-22 16:10:53 +00003095 * LIBMTP_track_t *track = LIBMTP_new_track_t();
3096 * track->title = strdup(titlestr);
3097 * ....
3098 * LIBMTP_destroy_track_t(track);
Linus Walleij17e39f72006-02-23 15:54:28 +00003099 * </pre>
Linus Walleij394bbbe2006-02-22 16:10:53 +00003100 *
3101 * @return a pointer to the newly allocated metadata structure.
3102 * @see LIBMTP_destroy_track_t()
3103 */
3104LIBMTP_track_t *LIBMTP_new_track_t(void)
Linus Walleijb9256fd2006-02-15 09:40:43 +00003105{
3106 LIBMTP_track_t *new = (LIBMTP_track_t *) malloc(sizeof(LIBMTP_track_t));
3107 if (new == NULL) {
3108 return NULL;
3109 }
Linus Walleijea68f1f2008-06-22 21:54:44 +00003110 new->item_id = 0;
3111 new->parent_id = 0;
3112 new->storage_id = 0;
Linus Walleijb9256fd2006-02-15 09:40:43 +00003113 new->title = NULL;
3114 new->artist = NULL;
Linus Walleij31b74292008-05-02 23:29:06 +00003115 new->composer = NULL;
Linus Walleijb9256fd2006-02-15 09:40:43 +00003116 new->album = NULL;
3117 new->genre = NULL;
3118 new->date = NULL;
3119 new->filename = NULL;
3120 new->duration = 0;
3121 new->tracknumber = 0;
3122 new->filesize = 0;
Linus Walleijf6bc1782006-03-24 15:12:47 +00003123 new->filetype = LIBMTP_FILETYPE_UNKNOWN;
Linus Walleijcf223e62006-06-19 09:31:53 +00003124 new->samplerate = 0;
3125 new->nochannels = 0;
3126 new->wavecodec = 0;
3127 new->bitrate = 0;
3128 new->bitratetype = 0;
3129 new->rating = 0;
3130 new->usecount = 0;
Linus Walleijb9256fd2006-02-15 09:40:43 +00003131 new->next = NULL;
3132 return new;
3133}
3134
Linus Walleij394bbbe2006-02-22 16:10:53 +00003135/**
3136 * This destroys a track metadata structure and deallocates the memory
mopoke96143402006-10-30 04:37:26 +00003137 * used by it, including any strings. Never use a track metadata
Linus Walleij394bbbe2006-02-22 16:10:53 +00003138 * structure again after calling this function on it.
3139 * @param track the track metadata to destroy.
3140 * @see LIBMTP_new_track_t()
3141 */
Linus Walleijb9256fd2006-02-15 09:40:43 +00003142void LIBMTP_destroy_track_t(LIBMTP_track_t *track)
3143{
3144 if (track == NULL) {
3145 return;
3146 }
3147 if (track->title != NULL)
3148 free(track->title);
3149 if (track->artist != NULL)
3150 free(track->artist);
Linus Walleij31b74292008-05-02 23:29:06 +00003151 if (track->composer != NULL)
3152 free(track->composer);
Linus Walleijb9256fd2006-02-15 09:40:43 +00003153 if (track->album != NULL)
3154 free(track->album);
3155 if (track->genre != NULL)
3156 free(track->genre);
3157 if (track->date != NULL)
3158 free(track->date);
3159 if (track->filename != NULL)
3160 free(track->filename);
3161 free(track);
3162 return;
3163}
3164
3165/**
Linus Walleij338ade42007-07-03 20:44:08 +00003166 * This function maps and copies a property onto the track metadata if applicable.
3167 */
Linus Walleijddaba2f2007-10-02 21:20:30 +00003168static void pick_property_to_track_metadata(LIBMTP_mtpdevice_t *device, MTPProperties *prop, LIBMTP_track_t *track)
Linus Walleij338ade42007-07-03 20:44:08 +00003169{
3170 switch (prop->property) {
3171 case PTP_OPC_Name:
3172 if (prop->propval.str != NULL)
3173 track->title = strdup(prop->propval.str);
3174 else
3175 track->title = NULL;
3176 break;
3177 case PTP_OPC_Artist:
3178 if (prop->propval.str != NULL)
3179 track->artist = strdup(prop->propval.str);
3180 else
3181 track->artist = NULL;
3182 break;
Linus Walleij31b74292008-05-02 23:29:06 +00003183 case PTP_OPC_Composer:
3184 if (prop->propval.str != NULL)
3185 track->composer = strdup(prop->propval.str);
3186 else
3187 track->composer = NULL;
3188 break;
Linus Walleij338ade42007-07-03 20:44:08 +00003189 case PTP_OPC_Duration:
3190 track->duration = prop->propval.u32;
3191 break;
3192 case PTP_OPC_Track:
3193 track->tracknumber = prop->propval.u16;
3194 break;
3195 case PTP_OPC_Genre:
3196 if (prop->propval.str != NULL)
3197 track->genre = strdup(prop->propval.str);
3198 else
3199 track->genre = NULL;
3200 break;
3201 case PTP_OPC_AlbumName:
3202 if (prop->propval.str != NULL)
3203 track->album = strdup(prop->propval.str);
3204 else
3205 track->album = NULL;
3206 break;
3207 case PTP_OPC_OriginalReleaseDate:
3208 if (prop->propval.str != NULL)
3209 track->date = strdup(prop->propval.str);
3210 else
3211 track->date = NULL;
3212 break;
3213 // These are, well not so important.
3214 case PTP_OPC_SampleRate:
3215 track->samplerate = prop->propval.u32;
3216 break;
3217 case PTP_OPC_NumberOfChannels:
3218 track->nochannels = prop->propval.u16;
3219 break;
3220 case PTP_OPC_AudioWAVECodec:
3221 track->wavecodec = prop->propval.u32;
3222 break;
3223 case PTP_OPC_AudioBitRate:
3224 track->bitrate = prop->propval.u32;
3225 break;
3226 case PTP_OPC_BitRateType:
3227 track->bitratetype = prop->propval.u16;
3228 break;
3229 case PTP_OPC_Rating:
3230 track->rating = prop->propval.u16;
3231 break;
3232 case PTP_OPC_UseCount:
3233 track->usecount = prop->propval.u32;
3234 break;
Linus Walleijd9d28d52007-08-04 19:01:18 +00003235 case PTP_OPC_ObjectSize:
Linus Walleijddaba2f2007-10-02 21:20:30 +00003236 if (device->object_bitsize == 64) {
3237 track->filesize = prop->propval.u64;
3238 } else {
3239 track->filesize = prop->propval.u32;
Linus Walleijca2a1702007-10-02 20:41:45 +00003240 }
Linus Walleijd9d28d52007-08-04 19:01:18 +00003241 break;
3242 default:
3243 break;
Linus Walleij338ade42007-07-03 20:44:08 +00003244 }
3245}
3246
3247/**
Linus Walleij8ab54262006-06-21 07:12:28 +00003248 * This function retrieves the track metadata for a track
3249 * given by a unique ID.
3250 * @param device a pointer to the device to get the track metadata off.
3251 * @param trackid the unique ID of the track.
3252 * @param objectformat the object format of this track, so we know what it supports.
3253 * @param track a metadata set to fill in.
3254 */
3255static void get_track_metadata(LIBMTP_mtpdevice_t *device, uint16_t objectformat,
3256 LIBMTP_track_t *track)
3257{
Linus Walleij00cf0642006-07-26 20:40:59 +00003258 uint16_t ret;
3259 PTPParams *params = (PTPParams *) device->params;
Linus Walleij1a1b2d12006-11-23 13:32:17 +00003260 PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo;
Linus Walleij00cf0642006-07-26 20:40:59 +00003261 uint32_t i;
Linus Walleij00cf0642006-07-26 20:40:59 +00003262
Linus Walleij338ade42007-07-03 20:44:08 +00003263 /*
3264 * If we have a cached, large set of metadata, then use it!
3265 */
Linus Walleij1e9a0332007-09-12 19:35:56 +00003266 if (params->props) {
3267 MTPProperties *prop = params->props;
3268
3269 for (i=0;(i<params->nrofprops) && (prop->ObjectHandle != track->item_id);i++,prop++)
3270 /*empty*/;
3271 for (i=0;(i<params->nrofprops) && (prop->ObjectHandle == track->item_id);i++,prop++) {
Linus Walleijddaba2f2007-10-02 21:20:30 +00003272 pick_property_to_track_metadata(device, prop, track);
Linus Walleij338ade42007-07-03 20:44:08 +00003273 }
3274 } else if (ptp_operation_issupported(params,PTP_OC_MTP_GetObjPropList)
Linus Walleijfec4d562008-06-01 22:30:36 +00003275 && !FLAG_BROKEN_MTPGETOBJPROPLIST(ptp_usb)) {
Linus Walleij1e9a0332007-09-12 19:35:56 +00003276 MTPProperties *props = NULL;
3277 MTPProperties *prop;
3278 int nrofprops;
mopoke31364442006-11-20 04:53:04 +00003279
Linus Walleij1a1b2d12006-11-23 13:32:17 +00003280 /*
3281 * This should retrieve all properties for an object, but on devices
3282 * which are inherently broken it will not, so these need the
3283 * special flag DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST.
3284 */
Linus Walleij1e9a0332007-09-12 19:35:56 +00003285 ret = ptp_mtp_getobjectproplist(params, track->item_id, &props, &nrofprops);
Linus Walleij070e9b42007-01-22 08:49:28 +00003286 if (ret != PTP_RC_OK) {
3287 add_ptp_error_to_errorstack(device, ret, "get_track_metadata(): call to ptp_mtp_getobjectproplist() failed.");
3288 return;
3289 }
Linus Walleijfcb43422008-05-23 21:53:55 +00003290 if (props == NULL && nrofprops != 0) {
3291 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL,
3292 "get_track_metadata(): "
3293 "call to ptp_mtp_getobjectproplist() returned "
3294 "inconsistent results.");
3295 return;
3296 }
Linus Walleij1e9a0332007-09-12 19:35:56 +00003297 prop = props;
Richard Lowd7bcab52007-09-16 16:30:20 +00003298 for (i=0;i<nrofprops;i++,prop++) {
Linus Walleij1e9a0332007-09-12 19:35:56 +00003299 if (prop->ObjectHandle == track->item_id)
Linus Walleijddaba2f2007-10-02 21:20:30 +00003300 pick_property_to_track_metadata(device, prop, track);
Linus Walleij00cf0642006-07-26 20:40:59 +00003301 }
Linus Walleija6d0d482007-10-31 08:54:56 +00003302 ptp_destroy_object_prop_list(props, nrofprops);
Linus Walleij3fcfea52006-11-13 07:07:36 +00003303 } else {
Linus Walleij3fcfea52006-11-13 07:07:36 +00003304 uint16_t *props = NULL;
3305 uint32_t propcnt = 0;
3306
3307 // First see which properties can be retrieved for this object format
Linus Walleij070e9b42007-01-22 08:49:28 +00003308 ret = ptp_mtp_getobjectpropssupported(params, map_libmtp_type_to_ptp_type(track->filetype), &propcnt, &props);
Linus Walleij3fcfea52006-11-13 07:07:36 +00003309 if (ret != PTP_RC_OK) {
Linus Walleij070e9b42007-01-22 08:49:28 +00003310 add_ptp_error_to_errorstack(device, ret, "get_track_metadata(): call to ptp_mtp_getobjectpropssupported() failed.");
Linus Walleij3fcfea52006-11-13 07:07:36 +00003311 // Just bail out for now, nothing is ever set.
3312 return;
3313 } else {
3314 for (i=0;i<propcnt;i++) {
3315 switch (props[i]) {
3316 case PTP_OPC_Name:
Linus Walleij9901e222006-11-30 12:28:19 +00003317 track->title = get_string_from_object(device, track->item_id, PTP_OPC_Name);
Linus Walleij3fcfea52006-11-13 07:07:36 +00003318 break;
3319 case PTP_OPC_Artist:
Linus Walleij9901e222006-11-30 12:28:19 +00003320 track->artist = get_string_from_object(device, track->item_id, PTP_OPC_Artist);
Linus Walleij3fcfea52006-11-13 07:07:36 +00003321 break;
Linus Walleij31b74292008-05-02 23:29:06 +00003322 case PTP_OPC_Composer:
3323 track->composer = get_string_from_object(device, track->item_id, PTP_OPC_Composer);
3324 break;
Linus Walleij3fcfea52006-11-13 07:07:36 +00003325 case PTP_OPC_Duration:
Linus Walleij9901e222006-11-30 12:28:19 +00003326 track->duration = get_u32_from_object(device, track->item_id, PTP_OPC_Duration, 0);
Linus Walleij3fcfea52006-11-13 07:07:36 +00003327 break;
3328 case PTP_OPC_Track:
Linus Walleij9901e222006-11-30 12:28:19 +00003329 track->tracknumber = get_u16_from_object(device, track->item_id, PTP_OPC_Track, 0);
Linus Walleij3fcfea52006-11-13 07:07:36 +00003330 break;
3331 case PTP_OPC_Genre:
Linus Walleij9901e222006-11-30 12:28:19 +00003332 track->genre = get_string_from_object(device, track->item_id, PTP_OPC_Genre);
Linus Walleij3fcfea52006-11-13 07:07:36 +00003333 break;
3334 case PTP_OPC_AlbumName:
Linus Walleij9901e222006-11-30 12:28:19 +00003335 track->album = get_string_from_object(device, track->item_id, PTP_OPC_AlbumName);
Linus Walleij3fcfea52006-11-13 07:07:36 +00003336 break;
3337 case PTP_OPC_OriginalReleaseDate:
Linus Walleij9901e222006-11-30 12:28:19 +00003338 track->date = get_string_from_object(device, track->item_id, PTP_OPC_OriginalReleaseDate);
Linus Walleij3fcfea52006-11-13 07:07:36 +00003339 break;
3340 // These are, well not so important.
3341 case PTP_OPC_SampleRate:
Linus Walleij9901e222006-11-30 12:28:19 +00003342 track->samplerate = get_u32_from_object(device, track->item_id, PTP_OPC_SampleRate, 0);
Linus Walleij3fcfea52006-11-13 07:07:36 +00003343 break;
3344 case PTP_OPC_NumberOfChannels:
Linus Walleij9901e222006-11-30 12:28:19 +00003345 track->nochannels = get_u16_from_object(device, track->item_id, PTP_OPC_NumberOfChannels, 0);
Linus Walleij3fcfea52006-11-13 07:07:36 +00003346 break;
3347 case PTP_OPC_AudioWAVECodec:
Linus Walleij9901e222006-11-30 12:28:19 +00003348 track->wavecodec = get_u32_from_object(device, track->item_id, PTP_OPC_AudioWAVECodec, 0);
Linus Walleij3fcfea52006-11-13 07:07:36 +00003349 break;
3350 case PTP_OPC_AudioBitRate:
Linus Walleij9901e222006-11-30 12:28:19 +00003351 track->bitrate = get_u32_from_object(device, track->item_id, PTP_OPC_AudioBitRate, 0);
Linus Walleij3fcfea52006-11-13 07:07:36 +00003352 break;
3353 case PTP_OPC_BitRateType:
Linus Walleij9901e222006-11-30 12:28:19 +00003354 track->bitratetype = get_u16_from_object(device, track->item_id, PTP_OPC_BitRateType, 0);
Linus Walleij3fcfea52006-11-13 07:07:36 +00003355 break;
3356 case PTP_OPC_Rating:
Linus Walleij9901e222006-11-30 12:28:19 +00003357 track->rating = get_u16_from_object(device, track->item_id, PTP_OPC_Rating, 0);
Linus Walleij3fcfea52006-11-13 07:07:36 +00003358 break;
3359 case PTP_OPC_UseCount:
Linus Walleij9901e222006-11-30 12:28:19 +00003360 track->usecount = get_u32_from_object(device, track->item_id, PTP_OPC_UseCount, 0);
Linus Walleij3fcfea52006-11-13 07:07:36 +00003361 break;
Linus Walleijd9d28d52007-08-04 19:01:18 +00003362 case PTP_OPC_ObjectSize:
Linus Walleijddaba2f2007-10-02 21:20:30 +00003363 if (device->object_bitsize == 64) {
3364 track->filesize = get_u64_from_object(device, track->item_id, PTP_OPC_ObjectSize, 0);
3365 } else {
3366 track->filesize = (uint64_t) get_u32_from_object(device, track->item_id, PTP_OPC_ObjectSize, 0);
Linus Walleijca2a1702007-10-02 20:41:45 +00003367 }
Linus Walleijd9d28d52007-08-04 19:01:18 +00003368 break;
Linus Walleij3fcfea52006-11-13 07:07:36 +00003369 }
3370 }
3371 free(props);
3372 }
Linus Walleij00cf0642006-07-26 20:40:59 +00003373 }
Linus Walleij8ab54262006-06-21 07:12:28 +00003374}
3375
3376/**
mopoke31364442006-11-20 04:53:04 +00003377 * THIS FUNCTION IS DEPRECATED. PLEASE UPDATE YOUR CODE IN ORDER
Linus Walleij3fcfea52006-11-13 07:07:36 +00003378 * NOT TO USE IT.
3379 * @see LIBMTP_Get_Tracklisting_With_Callback()
3380 */
3381LIBMTP_track_t *LIBMTP_Get_Tracklisting(LIBMTP_mtpdevice_t *device)
3382{
3383 printf("WARNING: LIBMTP_Get_Tracklisting() is deprecated.\n");
3384 printf("WARNING: please update your code to use LIBMTP_Get_Tracklisting_With_Callback()\n");
Richard Lowdc0b6c72006-11-13 09:22:23 +00003385 return LIBMTP_Get_Tracklisting_With_Callback(device, NULL, NULL);
Linus Walleij3fcfea52006-11-13 07:07:36 +00003386}
3387
3388/**
Linus Walleij25d5c212008-08-14 06:49:13 +00003389 * This returns a long list of all tracks available on the current MTP device.
3390 * Tracks include multimedia objects, both music tracks and video tracks.
3391 * Typical usage:
Linus Walleija4982732006-02-24 15:46:02 +00003392 *
3393 * <pre>
3394 * LIBMTP_track_t *tracklist;
3395 *
Richard Low6711f442007-05-05 19:00:59 +00003396 * tracklist = LIBMTP_Get_Tracklisting_With_Callback(device, callback, data);
Linus Walleija4982732006-02-24 15:46:02 +00003397 * while (tracklist != NULL) {
3398 * LIBMTP_track_t *tmp;
3399 *
3400 * // Do something on each element in the list here...
3401 * tmp = tracklist;
3402 * tracklist = tracklist->next;
3403 * LIBMTP_destroy_track_t(tmp);
3404 * }
3405 * </pre>
3406 *
Linus Walleij25d5c212008-08-14 06:49:13 +00003407 * If you want to group your track listing by storage (per storage unit) or
3408 * arrange tracks into folders, you must dereference the <code>storage_id</code>
3409 * and/or <code>parent_id</code> field of the returned <code>LIBMTP_track_t</code>
3410 * struct. To arrange by folders or files you typically have to create the proper
3411 * trees by calls to <code>LIBMTP_Get_Storage()</code> and/or
3412 * <code>LIBMTP_Get_Folder_List()</code> first.
3413 *
Linus Walleijb9256fd2006-02-15 09:40:43 +00003414 * @param device a pointer to the device to get the track listing for.
Linus Walleij3fcfea52006-11-13 07:07:36 +00003415 * @param callback a function to be called during the tracklisting retrieveal
Linus Walleij25d5c212008-08-14 06:49:13 +00003416 * for displaying progress bars etc, or NULL if you don't want
3417 * any callbacks.
Richard Lowdc0b6c72006-11-13 09:22:23 +00003418 * @param data a user-defined pointer that is passed along to
Linus Walleij25d5c212008-08-14 06:49:13 +00003419 * the <code>progress</code> function in order to
3420 * pass along some user defined data to the progress
3421 * updates. If not used, set this to NULL.
Linus Walleija4982732006-02-24 15:46:02 +00003422 * @return a list of tracks that can be followed using the <code>next</code>
Linus Walleij25d5c212008-08-14 06:49:13 +00003423 * field of the <code>LIBMTP_track_t</code> data structure.
3424 * Each of the metadata tags must be freed after use, and may
3425 * contain only partial metadata information, i.e. one or several
3426 * fields may be NULL or 0.
Linus Walleij2e4b5f92006-06-16 14:00:49 +00003427 * @see LIBMTP_Get_Trackmetadata()
Linus Walleijb9256fd2006-02-15 09:40:43 +00003428 */
Richard Lowdc0b6c72006-11-13 09:22:23 +00003429LIBMTP_track_t *LIBMTP_Get_Tracklisting_With_Callback(LIBMTP_mtpdevice_t *device,
3430 LIBMTP_progressfunc_t const callback,
3431 void const * const data)
Linus Walleijb9256fd2006-02-15 09:40:43 +00003432{
3433 uint32_t i = 0;
3434 LIBMTP_track_t *retracks = NULL;
3435 LIBMTP_track_t *curtrack = NULL;
Linus Walleij9b28da32006-03-16 13:47:58 +00003436 PTPParams *params = (PTPParams *) device->params;
Linus Walleij2be40c72007-03-16 15:19:44 +00003437 PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo;
Linus Walleij438bd7f2006-06-08 11:35:44 +00003438
mopoke96143402006-10-30 04:37:26 +00003439 // Get all the handles if we haven't already done that
Linus Walleij9b28da32006-03-16 13:47:58 +00003440 if (params->handles.Handler == NULL) {
Linus Walleij438bd7f2006-06-08 11:35:44 +00003441 flush_handles(device);
Linus Walleijb9256fd2006-02-15 09:40:43 +00003442 }
mopoke96143402006-10-30 04:37:26 +00003443
Linus Walleij9b28da32006-03-16 13:47:58 +00003444 for (i = 0; i < params->handles.n; i++) {
Linus Walleij070e9b42007-01-22 08:49:28 +00003445 LIBMTP_track_t *track;
Linus Walleijf0bf4372007-07-01 21:47:38 +00003446 PTPObjectInfo *oi;
Linus Walleij46651f32008-04-26 23:30:19 +00003447 LIBMTP_filetype_t mtptype;
mopoke31364442006-11-20 04:53:04 +00003448
Richard Lowdc0b6c72006-11-13 09:22:23 +00003449 if (callback != NULL)
3450 callback(i, params->handles.n, data);
mopoke31364442006-11-20 04:53:04 +00003451
Linus Walleijf0bf4372007-07-01 21:47:38 +00003452 oi = &params->objectinfo[i];
Linus Walleij46651f32008-04-26 23:30:19 +00003453 mtptype = map_ptp_type_to_libmtp_type(oi->ObjectFormat);
mopoke96143402006-10-30 04:37:26 +00003454
Linus Walleijf0bf4372007-07-01 21:47:38 +00003455 // Ignore stuff we don't know how to handle...
3456 // TODO: get this list as an intersection of the sets
3457 // supported by the device and the from the device and
3458 // all known audio track files?
Linus Walleij10e0ce72008-05-04 19:20:33 +00003459 if (!LIBMTP_FILETYPE_IS_AUDIO(mtptype) &&
3460 !LIBMTP_FILETYPE_IS_VIDEO(mtptype) &&
3461 !LIBMTP_FILETYPE_IS_AUDIOVIDEO(mtptype) &&
Linus Walleij46651f32008-04-26 23:30:19 +00003462 // This row lets through undefined files for examination since they may be forgotten OGG files.
3463 (oi->ObjectFormat != PTP_OFC_Undefined ||
Linus Walleijfec4d562008-06-01 22:30:36 +00003464 !FLAG_IRIVER_OGG_ALZHEIMER(ptp_usb) ||
3465 !FLAG_OGG_IS_UNKNOWN(ptp_usb))
Linus Walleij46651f32008-04-26 23:30:19 +00003466 ) {
Linus Walleijf0bf4372007-07-01 21:47:38 +00003467 // printf("Not a music track (format: %d), skipping...\n",oi.ObjectFormat);
3468 continue;
Linus Walleijb9256fd2006-02-15 09:40:43 +00003469 }
3470
Linus Walleijf0bf4372007-07-01 21:47:38 +00003471 // Allocate a new track type
3472 track = LIBMTP_new_track_t();
3473
3474 // This is some sort of unique ID so we can keep track of the track.
3475 track->item_id = params->handles.Handler[i];
3476 track->parent_id = oi->ParentObject;
Linus Walleijea68f1f2008-06-22 21:54:44 +00003477 track->storage_id = oi->StorageID;
Linus Walleijf0bf4372007-07-01 21:47:38 +00003478
Linus Walleij46651f32008-04-26 23:30:19 +00003479 track->filetype = mtptype;
Linus Walleijf0bf4372007-07-01 21:47:38 +00003480
3481 // Original file-specific properties
3482 track->filesize = oi->ObjectCompressedSize;
3483 if (oi->Filename != NULL) {
3484 track->filename = strdup(oi->Filename);
3485 }
3486
3487 get_track_metadata(device, oi->ObjectFormat, track);
3488
3489 /*
3490 * A special quirk for iriver devices that doesn't quite
3491 * remember that some files marked as "unknown" type are
3492 * actually OGG files. We look at the filename extension
3493 * and see if it happens that this was atleast named "ogg"
3494 * and fall back on this heuristic approach in that case,
3495 * for these bugged devices only.
3496 */
3497 if (track->filetype == LIBMTP_FILETYPE_UNKNOWN &&
Linus Walleijfec4d562008-06-01 22:30:36 +00003498 (FLAG_IRIVER_OGG_ALZHEIMER(ptp_usb) ||
3499 FLAG_OGG_IS_UNKNOWN(ptp_usb))) {
Linus Walleijf0bf4372007-07-01 21:47:38 +00003500 // Repair forgotten OGG filetype
3501 char *ptype;
3502
3503 ptype = rindex(track->filename,'.')+1;
3504 if (ptype != NULL && !strcasecmp (ptype, "ogg")) {
3505 // Fix it.
3506 track->filetype = LIBMTP_FILETYPE_OGG;
3507 } else {
3508 // This was not an OGG file so discard it and continue
3509 LIBMTP_destroy_track_t(track);
3510 continue;
3511 }
3512 }
3513
3514 // Add track to a list that will be returned afterwards.
3515 if (retracks == NULL) {
3516 retracks = track;
3517 curtrack = track;
3518 } else {
3519 curtrack->next = track;
3520 curtrack = track;
3521 }
3522
3523 // Call listing callback
3524 // double progressPercent = (double)i*(double)100.0 / (double)params->handles.n;
3525
3526
Linus Walleijb9256fd2006-02-15 09:40:43 +00003527 } // Handle counting loop
3528 return retracks;
3529}
Linus Walleijdcde6082006-02-17 16:16:34 +00003530
Linus Walleij2e4b5f92006-06-16 14:00:49 +00003531/**
3532 * This function retrieves the metadata for a single track off
3533 * the device.
3534 *
3535 * Do not call this function repeatedly! The track handles are linearly
3536 * searched O(n) and the call may involve (slow) USB traffic, so use
3537 * <code>LIBMTP_Get_Tracklisting()</code> and cache the tracks, preferably
3538 * as an efficient data structure such as a hash list.
3539 *
3540 * @param device a pointer to the device to get the track metadata from.
3541 * @param trackid the object ID of the track that you want the metadata for.
3542 * @return a track metadata entry on success or NULL on failure.
3543 * @see LIBMTP_Get_Tracklisting()
3544 */
3545LIBMTP_track_t *LIBMTP_Get_Trackmetadata(LIBMTP_mtpdevice_t *device, uint32_t const trackid)
3546{
3547 uint32_t i = 0;
3548 PTPParams *params = (PTPParams *) device->params;
3549
mopoke96143402006-10-30 04:37:26 +00003550 // Get all the handles if we haven't already done that
Linus Walleij2e4b5f92006-06-16 14:00:49 +00003551 if (params->handles.Handler == NULL) {
3552 flush_handles(device);
3553 }
3554
3555 for (i = 0; i < params->handles.n; i++) {
Linus Walleijf0bf4372007-07-01 21:47:38 +00003556 PTPObjectInfo *oi;
3557 LIBMTP_track_t *track;
Linus Walleij46651f32008-04-26 23:30:19 +00003558 LIBMTP_filetype_t mtptype;
mopoke96143402006-10-30 04:37:26 +00003559
Linus Walleij2e4b5f92006-06-16 14:00:49 +00003560 // Skip if this is not the track we want.
3561 if (params->handles.Handler[i] != trackid) {
3562 continue;
3563 }
3564
Linus Walleijf0bf4372007-07-01 21:47:38 +00003565 oi = &params->objectinfo[i];
Linus Walleij46651f32008-04-26 23:30:19 +00003566 mtptype = map_ptp_type_to_libmtp_type(oi->ObjectFormat);
mopoke96143402006-10-30 04:37:26 +00003567
Linus Walleijf0bf4372007-07-01 21:47:38 +00003568 // Ignore stuff we don't know how to handle...
Linus Walleij10e0ce72008-05-04 19:20:33 +00003569 if (!LIBMTP_FILETYPE_IS_AUDIO(mtptype) &&
3570 !LIBMTP_FILETYPE_IS_VIDEO(mtptype) &&
3571 !LIBMTP_FILETYPE_IS_AUDIOVIDEO(mtptype)) {
Linus Walleij2e4b5f92006-06-16 14:00:49 +00003572 return NULL;
3573 }
3574
Linus Walleijf0bf4372007-07-01 21:47:38 +00003575 // Allocate a new track type
3576 track = LIBMTP_new_track_t();
3577
3578 // This is some sort of unique ID so we can keep track of the track.
3579 track->item_id = params->handles.Handler[i];
3580 track->parent_id = oi->ParentObject;
Linus Walleijea68f1f2008-06-22 21:54:44 +00003581 track->storage_id = oi->StorageID;
Linus Walleijf0bf4372007-07-01 21:47:38 +00003582
Linus Walleij46651f32008-04-26 23:30:19 +00003583 track->filetype = mtptype;
Linus Walleijf0bf4372007-07-01 21:47:38 +00003584
3585 // Original file-specific properties
3586 track->filesize = oi->ObjectCompressedSize;
3587 if (oi->Filename != NULL) {
3588 track->filename = strdup(oi->Filename);
3589 }
3590
3591 get_track_metadata(device, oi->ObjectFormat, track);
3592
3593 return track;
3594
Linus Walleij2e4b5f92006-06-16 14:00:49 +00003595 }
3596 return NULL;
3597}
3598
Linus Walleijf6bc1782006-03-24 15:12:47 +00003599
3600/**
3601 * This gets a file off the device to a local file identified
3602 * by a filename.
3603 * @param device a pointer to the device to get the track from.
3604 * @param id the file ID of the file to retrieve.
3605 * @param path a filename to use for the retrieved file.
3606 * @param callback a progress indicator function or NULL to ignore.
3607 * @param data a user-defined pointer that is passed along to
3608 * the <code>progress</code> function in order to
3609 * pass along some user defined data to the progress
3610 * updates. If not used, set this to NULL.
mopoke96143402006-10-30 04:37:26 +00003611 * @return 0 if the transfer was successful, any other value means
Linus Walleijf6bc1782006-03-24 15:12:47 +00003612 * failure.
3613 * @see LIBMTP_Get_File_To_File_Descriptor()
3614 */
mopoke96143402006-10-30 04:37:26 +00003615int LIBMTP_Get_File_To_File(LIBMTP_mtpdevice_t *device, uint32_t const id,
Linus Walleijee73ef22006-08-27 19:56:00 +00003616 char const * const path, LIBMTP_progressfunc_t const callback,
Linus Walleijf6bc1782006-03-24 15:12:47 +00003617 void const * const data)
3618{
3619 int fd = -1;
3620 int ret;
3621
3622 // Sanity check
3623 if (path == NULL) {
Linus Walleij070e9b42007-01-22 08:49:28 +00003624 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 +00003625 return -1;
3626 }
3627
3628 // Open file
3629#ifdef __WIN32__
Linus Walleij7beba572006-11-29 08:56:12 +00003630#ifdef USE_WINDOWS_IO_H
3631 if ( (fd = _open(path, O_RDWR|O_CREAT|O_TRUNC|O_BINARY,_S_IREAD)) == -1 ) {
3632#else
flavienbfd80eb2006-06-01 22:41:49 +00003633 if ( (fd = open(path, O_RDWR|O_CREAT|O_TRUNC|O_BINARY,S_IRWXU|S_IRGRP)) == -1 ) {
Linus Walleij7beba572006-11-29 08:56:12 +00003634#endif
Linus Walleijf6bc1782006-03-24 15:12:47 +00003635#else
Linus Walleij05358382007-08-06 20:46:35 +00003636#ifdef __USE_LARGEFILE64
3637 if ( (fd = open64(path, O_RDWR|O_CREAT|O_TRUNC|O_LARGEFILE,S_IRWXU|S_IRGRP)) == -1) {
3638#else
Linus Walleijf6bc1782006-03-24 15:12:47 +00003639 if ( (fd = open(path, O_RDWR|O_CREAT|O_TRUNC,S_IRWXU|S_IRGRP)) == -1) {
3640#endif
Linus Walleij05358382007-08-06 20:46:35 +00003641#endif
Linus Walleij070e9b42007-01-22 08:49:28 +00003642 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Get_File_To_File(): Could not create file.");
Linus Walleijf6bc1782006-03-24 15:12:47 +00003643 return -1;
3644 }
mopoke96143402006-10-30 04:37:26 +00003645
Linus Walleijf6bc1782006-03-24 15:12:47 +00003646 ret = LIBMTP_Get_File_To_File_Descriptor(device, id, fd, callback, data);
3647
3648 // Close file
3649 close(fd);
mopoke96143402006-10-30 04:37:26 +00003650
Linus Walleij25d33b52007-09-16 21:36:19 +00003651 // Delete partial file.
3652 if (ret == -1) {
3653 unlink(path);
3654 }
3655
Linus Walleijf6bc1782006-03-24 15:12:47 +00003656 return ret;
3657}
3658
3659/**
3660 * This gets a file off the device to a file identified
3661 * by a file descriptor.
Linus Walleij0558ac52006-09-07 06:55:03 +00003662 *
mopoke96143402006-10-30 04:37:26 +00003663 * This function can potentially be used for streaming
3664 * files off the device for playback or broadcast for example,
Linus Walleij0558ac52006-09-07 06:55:03 +00003665 * by downloading the file into a stream sink e.g. a socket.
3666 *
Linus Walleijf6bc1782006-03-24 15:12:47 +00003667 * @param device a pointer to the device to get the file from.
3668 * @param id the file ID of the file to retrieve.
3669 * @param fd a local file descriptor to write the file to.
3670 * @param callback a progress indicator function or NULL to ignore.
3671 * @param data a user-defined pointer that is passed along to
3672 * the <code>progress</code> function in order to
3673 * pass along some user defined data to the progress
3674 * updates. If not used, set this to NULL.
mopoke96143402006-10-30 04:37:26 +00003675 * @return 0 if the transfer was successful, any other value means
Linus Walleijf6bc1782006-03-24 15:12:47 +00003676 * failure.
3677 * @see LIBMTP_Get_File_To_File()
3678 */
mopoke96143402006-10-30 04:37:26 +00003679int LIBMTP_Get_File_To_File_Descriptor(LIBMTP_mtpdevice_t *device,
3680 uint32_t const id,
3681 int const fd,
Linus Walleijee73ef22006-08-27 19:56:00 +00003682 LIBMTP_progressfunc_t const callback,
Linus Walleijf6bc1782006-03-24 15:12:47 +00003683 void const * const data)
3684{
Linus Walleijf0bf4372007-07-01 21:47:38 +00003685 PTPObjectInfo *oi;
3686 uint32_t i;
Linus Walleij438bd7f2006-06-08 11:35:44 +00003687 uint16_t ret;
Linus Walleijf6bc1782006-03-24 15:12:47 +00003688 PTPParams *params = (PTPParams *) device->params;
Linus Walleijee73ef22006-08-27 19:56:00 +00003689 PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo;
Linus Walleijf6bc1782006-03-24 15:12:47 +00003690
Linus Walleijf0bf4372007-07-01 21:47:38 +00003691 oi = NULL;
3692 for (i = 0; i < params->handles.n; i++) {
3693 if (params->handles.Handler[i] == id) {
3694 oi = &params->objectinfo[i];
3695 break;
3696 }
3697 }
3698 if (oi == NULL) {
3699 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Get_File_To_File_Descriptor(): Could not get object info.");
Linus Walleijf6bc1782006-03-24 15:12:47 +00003700 return -1;
3701 }
Linus Walleijf0bf4372007-07-01 21:47:38 +00003702 if (oi->ObjectFormat == PTP_OFC_Association) {
Linus Walleij070e9b42007-01-22 08:49:28 +00003703 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Get_File_To_File_Descriptor(): Bad object format.");
Linus Walleijf6bc1782006-03-24 15:12:47 +00003704 return -1;
3705 }
Linus Walleijf6bc1782006-03-24 15:12:47 +00003706
Linus Walleijee73ef22006-08-27 19:56:00 +00003707 // Callbacks
3708 ptp_usb->callback_active = 1;
Linus Walleijf0bf4372007-07-01 21:47:38 +00003709 ptp_usb->current_transfer_total = oi->ObjectCompressedSize+
Linus Walleij7f0c72e2006-09-06 13:01:58 +00003710 PTP_USB_BULK_HDR_LEN+sizeof(uint32_t); // Request length, one parameter
Linus Walleijee73ef22006-08-27 19:56:00 +00003711 ptp_usb->current_transfer_complete = 0;
3712 ptp_usb->current_transfer_callback = callback;
3713 ptp_usb->current_transfer_callback_data = data;
mopoke96143402006-10-30 04:37:26 +00003714
Linus Walleij96c62432006-08-21 10:04:02 +00003715 ret = ptp_getobject_tofd(params, id, fd);
Linus Walleijee73ef22006-08-27 19:56:00 +00003716
3717 ptp_usb->callback_active = 0;
3718 ptp_usb->current_transfer_callback = NULL;
3719 ptp_usb->current_transfer_callback_data = NULL;
mopoke96143402006-10-30 04:37:26 +00003720
Linus Walleijbd3bf9e2007-09-14 19:31:54 +00003721 if (ret == PTP_ERROR_CANCEL) {
Linus Walleijfb28b632007-10-23 21:56:18 +00003722 add_error_to_errorstack(device, LIBMTP_ERROR_CANCELLED, "LIBMTP_Get_File_From_File_Descriptor(): Cancelled transfer.");
Linus Walleijbd3bf9e2007-09-14 19:31:54 +00003723 return -1;
3724 }
Linus Walleijb02a0662006-04-25 08:05:09 +00003725 if (ret != PTP_RC_OK) {
Linus Walleij070e9b42007-01-22 08:49:28 +00003726 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 +00003727 return -1;
3728 }
mopoke96143402006-10-30 04:37:26 +00003729
Linus Walleijf6bc1782006-03-24 15:12:47 +00003730 return 0;
3731}
3732
Linus Walleijdcde6082006-02-17 16:16:34 +00003733/**
3734 * This gets a track off the device to a file identified
Linus Walleijf6bc1782006-03-24 15:12:47 +00003735 * by a filename. This is actually just a wrapper for the
3736 * \c LIBMTP_Get_Track_To_File() function.
Linus Walleijdcde6082006-02-17 16:16:34 +00003737 * @param device a pointer to the device to get the track from.
3738 * @param id the track ID of the track to retrieve.
3739 * @param path a filename to use for the retrieved track.
3740 * @param callback a progress indicator function or NULL to ignore.
3741 * @param data a user-defined pointer that is passed along to
3742 * the <code>progress</code> function in order to
3743 * pass along some user defined data to the progress
3744 * updates. If not used, set this to NULL.
mopoke96143402006-10-30 04:37:26 +00003745 * @return 0 if the transfer was successful, any other value means
Linus Walleijdcde6082006-02-17 16:16:34 +00003746 * failure.
3747 * @see LIBMTP_Get_Track_To_File_Descriptor()
3748 */
mopoke96143402006-10-30 04:37:26 +00003749int LIBMTP_Get_Track_To_File(LIBMTP_mtpdevice_t *device, uint32_t const id,
Linus Walleijee73ef22006-08-27 19:56:00 +00003750 char const * const path, LIBMTP_progressfunc_t const callback,
Linus Walleij0cd85432006-02-20 14:37:50 +00003751 void const * const data)
Linus Walleijdcde6082006-02-17 16:16:34 +00003752{
Linus Walleijf6bc1782006-03-24 15:12:47 +00003753 // This is just a wrapper
3754 return LIBMTP_Get_File_To_File(device, id, path, callback, data);
Linus Walleijdcde6082006-02-17 16:16:34 +00003755}
3756
3757/**
3758 * This gets a track off the device to a file identified
Linus Walleijf6bc1782006-03-24 15:12:47 +00003759 * by a file descriptor. This is actually just a wrapper for
3760 * the \c LIBMTP_Get_File_To_File_Descriptor() function.
Linus Walleijdcde6082006-02-17 16:16:34 +00003761 * @param device a pointer to the device to get the track from.
3762 * @param id the track ID of the track to retrieve.
3763 * @param fd a file descriptor to write the track to.
3764 * @param callback a progress indicator function or NULL to ignore.
3765 * @param data a user-defined pointer that is passed along to
3766 * the <code>progress</code> function in order to
3767 * pass along some user defined data to the progress
3768 * updates. If not used, set this to NULL.
mopoke96143402006-10-30 04:37:26 +00003769 * @return 0 if the transfer was successful, any other value means
Linus Walleijdcde6082006-02-17 16:16:34 +00003770 * failure.
3771 * @see LIBMTP_Get_Track_To_File()
3772 */
mopoke96143402006-10-30 04:37:26 +00003773int LIBMTP_Get_Track_To_File_Descriptor(LIBMTP_mtpdevice_t *device,
3774 uint32_t const id,
3775 int const fd,
Linus Walleijee73ef22006-08-27 19:56:00 +00003776 LIBMTP_progressfunc_t const callback,
Linus Walleij0cd85432006-02-20 14:37:50 +00003777 void const * const data)
Linus Walleijdcde6082006-02-17 16:16:34 +00003778{
Linus Walleijf6bc1782006-03-24 15:12:47 +00003779 // This is just a wrapper
3780 return LIBMTP_Get_File_To_File_Descriptor(device, id, fd, callback, data);
Linus Walleijdcde6082006-02-17 16:16:34 +00003781}
Linus Walleij394bbbe2006-02-22 16:10:53 +00003782
3783/**
3784 * This function sends a track from a local file to an
3785 * MTP device. A filename and a set of metadata must be
3786 * given as input.
3787 * @param device a pointer to the device to send the track to.
3788 * @param path the filename of a local file which will be sent.
3789 * @param metadata a track metadata set to be written along with the file.
Linus Walleijea68f1f2008-06-22 21:54:44 +00003790 * After this call the field <code>metadata-&gt;item_id</code>
3791 * will contain the new track ID. Other fields such
3792 * as the <code>metadata-&gt;filename</code>, <code>metadata-&gt;parent_id</code>
3793 * or <code>metadata-&gt;storage_id</code> may also change during this
3794 * operation due to device restrictions, so do not rely on the
3795 * contents of this struct to be preserved in any way.
3796 * <ul>
3797 * <li><code>metadata-&gt;parent_id</code> should be set to the parent
3798 * (e.g. folder) to store this track in. Since some
3799 * devices are a bit picky about where files
3800 * are placed, a default folder will be chosen if libmtp
3801 * has detected one for the current filetype and this
3802 * parameter is set to 0. If this is 0 and no default folder
3803 * can be found, the file will be stored in the root folder.
3804 * <li><code>metadata-&gt;storage_id</code> should be set to the
3805 * desired storage (e.g. memory card or whatever your device
3806 * presents) to store this track in. Setting this to 0 will store
3807 * the track on the primary storage.
3808 * </ul>
Linus Walleij394bbbe2006-02-22 16:10:53 +00003809 * @param callback a progress indicator function or NULL to ignore.
3810 * @param data a user-defined pointer that is passed along to
3811 * the <code>progress</code> function in order to
3812 * pass along some user defined data to the progress
3813 * updates. If not used, set this to NULL.
mopoke96143402006-10-30 04:37:26 +00003814 * @return 0 if the transfer was successful, any other value means
Linus Walleij394bbbe2006-02-22 16:10:53 +00003815 * failure.
3816 * @see LIBMTP_Send_Track_From_File_Descriptor()
Linus Walleij1b87ea72007-08-28 07:53:09 +00003817 * @see LIBMTP_Send_File_From_File()
Linus Walleij438bd7f2006-06-08 11:35:44 +00003818 * @see LIBMTP_Delete_Object()
Linus Walleij394bbbe2006-02-22 16:10:53 +00003819 */
mopoke96143402006-10-30 04:37:26 +00003820int LIBMTP_Send_Track_From_File(LIBMTP_mtpdevice_t *device,
Linus Walleij394bbbe2006-02-22 16:10:53 +00003821 char const * const path, LIBMTP_track_t * const metadata,
Linus Walleijee73ef22006-08-27 19:56:00 +00003822 LIBMTP_progressfunc_t const callback,
Linus Walleijea68f1f2008-06-22 21:54:44 +00003823 void const * const data)
Linus Walleij394bbbe2006-02-22 16:10:53 +00003824{
3825 int fd;
3826 int ret;
3827
3828 // Sanity check
3829 if (path == NULL) {
Linus Walleij070e9b42007-01-22 08:49:28 +00003830 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 +00003831 return -1;
3832 }
3833
3834 // Open file
3835#ifdef __WIN32__
Linus Walleij7beba572006-11-29 08:56:12 +00003836#ifdef USE_WINDOWS_IO_H
3837 if ( (fd = _open(path, O_RDONLY|O_BINARY) == -1 ) {
3838#else
Linus Walleij394bbbe2006-02-22 16:10:53 +00003839 if ( (fd = open(path, O_RDONLY|O_BINARY) == -1 ) {
Linus Walleij7beba572006-11-29 08:56:12 +00003840#endif
Linus Walleij394bbbe2006-02-22 16:10:53 +00003841#else
Linus Walleij05358382007-08-06 20:46:35 +00003842#ifdef __USE_LARGEFILE64
3843 if ( (fd = open64(path, O_RDONLY|O_LARGEFILE)) == -1) {
3844#else
Linus Walleij394bbbe2006-02-22 16:10:53 +00003845 if ( (fd = open(path, O_RDONLY)) == -1) {
3846#endif
Linus Walleij05358382007-08-06 20:46:35 +00003847#endif
Linus Walleijd6a49972006-05-02 08:24:54 +00003848 printf("LIBMTP_Send_Track_From_File(): Could not open source file \"%s\"\n", path);
Linus Walleij394bbbe2006-02-22 16:10:53 +00003849 return -1;
3850 }
3851
Linus Walleijea68f1f2008-06-22 21:54:44 +00003852 ret = LIBMTP_Send_Track_From_File_Descriptor(device, fd, metadata, callback, data);
mopoke96143402006-10-30 04:37:26 +00003853
Linus Walleij394bbbe2006-02-22 16:10:53 +00003854 // Close file.
Linus Walleij7beba572006-11-29 08:56:12 +00003855#ifdef USE_WINDOWS_IO_H
3856 _close(fd);
3857#else
Linus Walleij394bbbe2006-02-22 16:10:53 +00003858 close(fd);
Linus Walleij7beba572006-11-29 08:56:12 +00003859#endif
Linus Walleij394bbbe2006-02-22 16:10:53 +00003860
3861 return ret;
3862}
3863
Linus Walleijfa1374c2006-02-27 07:41:46 +00003864
Richard Low016e3732007-09-23 14:53:46 +00003865
Linus Walleijfa1374c2006-02-27 07:41:46 +00003866/**
Linus Walleij394bbbe2006-02-22 16:10:53 +00003867 * This function sends a track from a file descriptor to an
3868 * MTP device. A filename and a set of metadata must be
3869 * given as input.
3870 * @param device a pointer to the device to send the track to.
3871 * @param fd the filedescriptor for a local file which will be sent.
3872 * @param metadata a track metadata set to be written along with the file.
Linus Walleijea68f1f2008-06-22 21:54:44 +00003873 * After this call the field <code>metadata-&gt;item_id</code>
3874 * will contain the new track ID. Other fields such
3875 * as the <code>metadata-&gt;filename</code>, <code>metadata-&gt;parent_id</code>
3876 * or <code>metadata-&gt;storage_id</code> may also change during this
3877 * operation due to device restrictions, so do not rely on the
3878 * contents of this struct to be preserved in any way.
3879 * <ul>
3880 * <li><code>metadata-&gt;parent_id</code> should be set to the parent
3881 * (e.g. folder) to store this track in. Since some
3882 * devices are a bit picky about where files
3883 * are placed, a default folder will be chosen if libmtp
3884 * has detected one for the current filetype and this
3885 * parameter is set to 0. If this is 0 and no default folder
3886 * can be found, the file will be stored in the root folder.
3887 * <li><code>metadata-&gt;storage_id</code> should be set to the
3888 * desired storage (e.g. memory card or whatever your device
3889 * presents) to store this track in. Setting this to 0 will store
3890 * the track on the primary storage.
3891 * </ul>
Linus Walleij394bbbe2006-02-22 16:10:53 +00003892 * @param callback a progress indicator function or NULL to ignore.
3893 * @param data a user-defined pointer that is passed along to
3894 * the <code>progress</code> function in order to
3895 * pass along some user defined data to the progress
3896 * updates. If not used, set this to NULL.
mopoke96143402006-10-30 04:37:26 +00003897 * @return 0 if the transfer was successful, any other value means
Linus Walleij394bbbe2006-02-22 16:10:53 +00003898 * failure.
3899 * @see LIBMTP_Send_Track_From_File()
Linus Walleij438bd7f2006-06-08 11:35:44 +00003900 * @see LIBMTP_Delete_Object()
Linus Walleij394bbbe2006-02-22 16:10:53 +00003901 */
mopoke96143402006-10-30 04:37:26 +00003902int LIBMTP_Send_Track_From_File_Descriptor(LIBMTP_mtpdevice_t *device,
Linus Walleij394bbbe2006-02-22 16:10:53 +00003903 int const fd, LIBMTP_track_t * const metadata,
Linus Walleijee73ef22006-08-27 19:56:00 +00003904 LIBMTP_progressfunc_t const callback,
Linus Walleijea68f1f2008-06-22 21:54:44 +00003905 void const * const data)
Linus Walleij394bbbe2006-02-22 16:10:53 +00003906{
Linus Walleij17e39f72006-02-23 15:54:28 +00003907 int subcall_ret;
Linus Walleijd2778d12007-08-06 19:24:58 +00003908 LIBMTP_file_t filedata;
Linus Walleij8ae78bb2006-11-20 21:45:52 +00003909
Linus Walleij16c51f02006-05-04 13:20:22 +00003910 // Sanity check, is this really a track?
Linus Walleij10e0ce72008-05-04 19:20:33 +00003911 if (!LIBMTP_FILETYPE_IS_TRACK(metadata->filetype)) {
Linus Walleij46651f32008-04-26 23:30:19 +00003912 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL,
3913 "LIBMTP_Send_Track_From_File_Descriptor(): "
3914 "I don't think this is actually a track, strange filetype...");
Linus Walleij99310d42006-11-01 08:29:39 +00003915 }
Linus Walleij37253292006-10-11 08:38:14 +00003916
Linus Walleijd2778d12007-08-06 19:24:58 +00003917 // Wrap around the file transfer function
3918 filedata.item_id = metadata->item_id;
3919 filedata.parent_id = metadata->parent_id;
Linus Walleijea68f1f2008-06-22 21:54:44 +00003920 filedata.storage_id = metadata->storage_id;
Linus Walleijd2778d12007-08-06 19:24:58 +00003921 filedata.filename = metadata->filename;
3922 filedata.filesize = metadata->filesize;
3923 filedata.filetype = metadata->filetype;
3924 filedata.next = NULL;
Linus Walleij37253292006-10-11 08:38:14 +00003925
Linus Walleijd2778d12007-08-06 19:24:58 +00003926 subcall_ret = LIBMTP_Send_File_From_File_Descriptor(device,
3927 fd,
3928 &filedata,
3929 callback,
Linus Walleijea68f1f2008-06-22 21:54:44 +00003930 data);
mopoke31364442006-11-20 04:53:04 +00003931
Linus Walleijd2778d12007-08-06 19:24:58 +00003932 if (subcall_ret != 0) {
Linus Walleij46651f32008-04-26 23:30:19 +00003933 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL,
3934 "LIBMTP_Send_Track_From_File_Descriptor(): "
Linus Walleijd2778d12007-08-06 19:24:58 +00003935 "subcall to LIBMTP_Send_File_From_File_Descriptor failed.");
3936 // We used to delete the file here, but don't... It might be OK after all.
3937 // (void) LIBMTP_Delete_Object(device, metadata->item_id);
Linus Walleij394bbbe2006-02-22 16:10:53 +00003938 return -1;
3939 }
Linus Walleijd2778d12007-08-06 19:24:58 +00003940
Linus Walleijea68f1f2008-06-22 21:54:44 +00003941 // Pick up new item (and parent, storage) ID
Linus Walleijd2778d12007-08-06 19:24:58 +00003942 metadata->item_id = filedata.item_id;
3943 metadata->parent_id = filedata.parent_id;
Linus Walleijea68f1f2008-06-22 21:54:44 +00003944 metadata->storage_id = filedata.storage_id;
mopoke96143402006-10-30 04:37:26 +00003945
Linus Walleij17e39f72006-02-23 15:54:28 +00003946 // Set track metadata for the new fine track
3947 subcall_ret = LIBMTP_Update_Track_Metadata(device, metadata);
3948 if (subcall_ret != 0) {
Linus Walleij070e9b42007-01-22 08:49:28 +00003949 // Subcall will add error to errorstack
Linus Walleijb2753182007-02-26 08:11:38 +00003950 // We used to delete the file here, but don't... It might be OK after all.
3951 // (void) LIBMTP_Delete_Object(device, metadata->item_id);
Linus Walleij17e39f72006-02-23 15:54:28 +00003952 return -1;
3953 }
Linus Walleij438bd7f2006-06-08 11:35:44 +00003954
Richard Low61edc1a2007-09-23 10:35:48 +00003955 // note we don't need to update the cache here because LIBMTP_Send_File_From_File_Descriptor
3956 // has added the object handle and LIBMTP_Update_Track_Metadata has added the metadata.
mopoke96143402006-10-30 04:37:26 +00003957
Linus Walleij17e39f72006-02-23 15:54:28 +00003958 return 0;
3959}
3960
Linus Walleijd6a49972006-05-02 08:24:54 +00003961/**
mopoke96143402006-10-30 04:37:26 +00003962 * This function sends a local file to an MTP device.
Linus Walleijd6a49972006-05-02 08:24:54 +00003963 * A filename and a set of metadata must be
3964 * given as input.
3965 * @param device a pointer to the device to send the track to.
3966 * @param path the filename of a local file which will be sent.
Linus Walleijea68f1f2008-06-22 21:54:44 +00003967 * @param filedata a file metadata set to be written along with the file.
3968 * After this call the field <code>filedata-&gt;item_id</code>
3969 * will contain the new file ID. Other fields such
3970 * as the <code>filedata-&gt;filename</code>, <code>filedata-&gt;parent_id</code>
3971 * or <code>filedata-&gt;storage_id</code> may also change during this
3972 * operation due to device restrictions, so do not rely on the
3973 * contents of this struct to be preserved in any way.
3974 * <ul>
3975 * <li><code>filedata-&gt;parent_id</code> should be set to the parent
3976 * (e.g. folder) to store this file in. If this is 0,
3977 * the file will be stored in the root folder.
3978 * <li><code>filedata-&gt;storage_id</code> should be set to the
3979 * desired storage (e.g. memory card or whatever your device
3980 * presents) to store this file in. Setting this to 0 will store
3981 * the file on the primary storage.
3982 * </ul>
Linus Walleijd6a49972006-05-02 08:24:54 +00003983 * @param callback a progress indicator function or NULL to ignore.
3984 * @param data a user-defined pointer that is passed along to
3985 * the <code>progress</code> function in order to
3986 * pass along some user defined data to the progress
3987 * updates. If not used, set this to NULL.
mopoke96143402006-10-30 04:37:26 +00003988 * @return 0 if the transfer was successful, any other value means
Linus Walleijd6a49972006-05-02 08:24:54 +00003989 * failure.
3990 * @see LIBMTP_Send_File_From_File_Descriptor()
Linus Walleij438bd7f2006-06-08 11:35:44 +00003991 * @see LIBMTP_Delete_Object()
Linus Walleijd6a49972006-05-02 08:24:54 +00003992 */
mopoke96143402006-10-30 04:37:26 +00003993int LIBMTP_Send_File_From_File(LIBMTP_mtpdevice_t *device,
Linus Walleijd6a49972006-05-02 08:24:54 +00003994 char const * const path, LIBMTP_file_t * const filedata,
Linus Walleijee73ef22006-08-27 19:56:00 +00003995 LIBMTP_progressfunc_t const callback,
Linus Walleijea68f1f2008-06-22 21:54:44 +00003996 void const * const data)
Linus Walleijd6a49972006-05-02 08:24:54 +00003997{
3998 int fd;
3999 int ret;
4000
4001 // Sanity check
4002 if (path == NULL) {
Linus Walleij070e9b42007-01-22 08:49:28 +00004003 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 +00004004 return -1;
4005 }
4006
4007 // Open file
4008#ifdef __WIN32__
Linus Walleij7beba572006-11-29 08:56:12 +00004009#ifdef USE_WINDOWS_IO_H
4010 if ( (fd = _open(path, O_RDONLY|O_BINARY) == -1 ) {
4011#else
Linus Walleijd6a49972006-05-02 08:24:54 +00004012 if ( (fd = open(path, O_RDONLY|O_BINARY) == -1 ) {
Linus Walleij7beba572006-11-29 08:56:12 +00004013#endif
Linus Walleijd6a49972006-05-02 08:24:54 +00004014#else
Linus Walleij05358382007-08-06 20:46:35 +00004015#ifdef __USE_LARGEFILE64
4016 if ( (fd = open64(path, O_RDONLY|O_LARGEFILE)) == -1) {
4017#else
Linus Walleijd6a49972006-05-02 08:24:54 +00004018 if ( (fd = open(path, O_RDONLY)) == -1) {
4019#endif
Linus Walleij05358382007-08-06 20:46:35 +00004020#endif
Linus Walleij070e9b42007-01-22 08:49:28 +00004021 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 +00004022 return -1;
4023 }
4024
Linus Walleijea68f1f2008-06-22 21:54:44 +00004025 ret = LIBMTP_Send_File_From_File_Descriptor(device, fd, filedata, callback, data);
mopoke96143402006-10-30 04:37:26 +00004026
Linus Walleijd6a49972006-05-02 08:24:54 +00004027 // Close file.
Linus Walleij7beba572006-11-29 08:56:12 +00004028#ifdef USE_WINDOWS_IO_H
4029 _close(fd);
4030#else
Linus Walleijd6a49972006-05-02 08:24:54 +00004031 close(fd);
Linus Walleij7beba572006-11-29 08:56:12 +00004032#endif
Linus Walleijd6a49972006-05-02 08:24:54 +00004033
4034 return ret;
4035}
4036
Linus Walleijd208f9c2006-04-27 14:16:06 +00004037/**
4038 * This function sends a generic file from a file descriptor to an
4039 * MTP device. A filename and a set of metadata must be
4040 * given as input.
Linus Walleijcd3eb3d2006-09-02 21:33:22 +00004041 *
4042 * This can potentially be used for sending in a stream of unknown
Linus Walleij1b87ea72007-08-28 07:53:09 +00004043 * length. Send music files with
4044 * <code>LIBMTP_Send_Track_From_File_Descriptor()</code>
Linus Walleijcd3eb3d2006-09-02 21:33:22 +00004045 *
Linus Walleijd208f9c2006-04-27 14:16:06 +00004046 * @param device a pointer to the device to send the file to.
4047 * @param fd the filedescriptor for a local file which will be sent.
Linus Walleijea68f1f2008-06-22 21:54:44 +00004048 * @param filedata a file metadata set to be written along with the file.
4049 * After this call the field <code>filedata-&gt;item_id</code>
4050 * will contain the new file ID. Other fields such
4051 * as the <code>filedata-&gt;filename</code>, <code>filedata-&gt;parent_id</code>
4052 * or <code>filedata-&gt;storage_id</code> may also change during this
4053 * operation due to device restrictions, so do not rely on the
4054 * contents of this struct to be preserved in any way.
4055 * <ul>
4056 * <li><code>filedata-&gt;parent_id</code> should be set to the parent
4057 * (e.g. folder) to store this file in. If this is 0,
4058 * the file will be stored in the root folder.
4059 * <li><code>filedata-&gt;storage_id</code> should be set to the
4060 * desired storage (e.g. memory card or whatever your device
4061 * presents) to store this file in. Setting this to 0 will store
4062 * the file on the primary storage.
4063 * </ul>
Linus Walleijd208f9c2006-04-27 14:16:06 +00004064 * @param callback a progress indicator function or NULL to ignore.
4065 * @param data a user-defined pointer that is passed along to
4066 * the <code>progress</code> function in order to
4067 * pass along some user defined data to the progress
4068 * updates. If not used, set this to NULL.
mopoke96143402006-10-30 04:37:26 +00004069 * @return 0 if the transfer was successful, any other value means
Linus Walleijd208f9c2006-04-27 14:16:06 +00004070 * failure.
Linus Walleijd6a49972006-05-02 08:24:54 +00004071 * @see LIBMTP_Send_File_From_File()
Linus Walleij1b87ea72007-08-28 07:53:09 +00004072 * @see LIBMTP_Send_Track_From_File_Descriptor()
Linus Walleij438bd7f2006-06-08 11:35:44 +00004073 * @see LIBMTP_Delete_Object()
Linus Walleijd208f9c2006-04-27 14:16:06 +00004074 */
mopoke96143402006-10-30 04:37:26 +00004075int LIBMTP_Send_File_From_File_Descriptor(LIBMTP_mtpdevice_t *device,
Linus Walleijd208f9c2006-04-27 14:16:06 +00004076 int const fd, LIBMTP_file_t * const filedata,
Linus Walleijee73ef22006-08-27 19:56:00 +00004077 LIBMTP_progressfunc_t const callback,
Linus Walleijea68f1f2008-06-22 21:54:44 +00004078 void const * const data)
Linus Walleijd208f9c2006-04-27 14:16:06 +00004079{
4080 uint16_t ret;
Linus Walleij6bf68b42007-09-03 22:50:02 +00004081 uint32_t store;
Linus Walleijea68f1f2008-06-22 21:54:44 +00004082 uint32_t localph = filedata->parent_id;
4083 LIBMTP_devicestorage_t *storage;
Linus Walleijd208f9c2006-04-27 14:16:06 +00004084 PTPParams *params = (PTPParams *) device->params;
Linus Walleijd214b9b2006-08-26 22:08:37 +00004085 PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo;
rreardonbbb4e562006-11-19 22:16:11 +00004086 int i;
Linus Walleijf5fcda32006-12-03 22:31:02 +00004087 int subcall_ret;
Linus Walleij07795772007-08-06 18:44:06 +00004088 uint16_t of = map_libmtp_type_to_ptp_type(filedata->filetype);
Linus Walleij817579a2008-05-23 21:21:40 +00004089 LIBMTP_file_t *newfilemeta;
Linus Walleijc40c9bf2008-06-13 23:24:17 +00004090 int use_primary_storage = 1;
Richard Lowab0d22d2006-11-30 22:17:49 +00004091
Linus Walleij6bf68b42007-09-03 22:50:02 +00004092 // Sanity check: no zerolength files.
Linus Walleij07795772007-08-06 18:44:06 +00004093 if (filedata->filesize == 0) {
4094 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Send_File_From_File_Descriptor(): "
4095 "File of zero size.");
4096 return -1;
Linus Walleijcd3eb3d2006-09-02 21:33:22 +00004097 }
Linus Walleij07795772007-08-06 18:44:06 +00004098
Linus Walleijea68f1f2008-06-22 21:54:44 +00004099 if (filedata->storage_id != 0) {
4100 store = filedata->storage_id;
Linus Walleijfb28b632007-10-23 21:56:18 +00004101 } else {
Linus Walleijea68f1f2008-06-22 21:54:44 +00004102 // See if there is some storage we can fit this file on.
4103 storage = device->storage;
Linus Walleijfb28b632007-10-23 21:56:18 +00004104 if (storage == NULL) {
Linus Walleijea68f1f2008-06-22 21:54:44 +00004105 // Sometimes the storage just cannot be detected.
4106 store = 0x00000000U;
4107 } else {
4108 while(storage != NULL) {
4109 subcall_ret = check_if_file_fits(device, storage, filedata->filesize);
4110 if (subcall_ret != 0) {
4111 storage = storage->next;
4112 }
4113 break;
4114 }
4115 if (storage == NULL) {
4116 add_error_to_errorstack(device, LIBMTP_ERROR_STORAGE_FULL, "LIBMTP_Send_File_From_File_Descriptor(): "
4117 "all device storage is full or corrupt.");
4118 return -1;
4119 }
4120 store = storage->id;
Linus Walleijfb28b632007-10-23 21:56:18 +00004121 }
Linus Walleijea68f1f2008-06-22 21:54:44 +00004122 }
4123 // Detect if something non-primary is in use.
4124 storage = device->storage;
4125 if (store != storage->id) {
4126 use_primary_storage = 0;
Linus Walleij6bf68b42007-09-03 22:50:02 +00004127 }
Linus Walleijd208f9c2006-04-27 14:16:06 +00004128
mopoke96143402006-10-30 04:37:26 +00004129 /*
Linus Walleij05ccbe72006-06-13 07:46:58 +00004130 * If no destination folder was given, look up a default
4131 * folder if possible. Perhaps there is some way of retrieveing
4132 * the default folder for different forms of content, what
4133 * do I know, we use a fixed list in lack of any better method.
4134 * Some devices obviously need to have their files in certain
mopoke96143402006-10-30 04:37:26 +00004135 * folders in order to find/display them at all (hello Creative),
Linus Walleijc40c9bf2008-06-13 23:24:17 +00004136 * so we have to have a method for this. We only do this if the
4137 * primary storage is in use.
Linus Walleij05ccbe72006-06-13 07:46:58 +00004138 */
4139
Linus Walleijc40c9bf2008-06-13 23:24:17 +00004140 if (localph == 0 && use_primary_storage) {
Linus Walleij10e0ce72008-05-04 19:20:33 +00004141 if (LIBMTP_FILETYPE_IS_AUDIO(filedata->filetype)) {
Linus Walleij05ccbe72006-06-13 07:46:58 +00004142 localph = device->default_music_folder;
Linus Walleij10e0ce72008-05-04 19:20:33 +00004143 } else if (LIBMTP_FILETYPE_IS_VIDEO(filedata->filetype)) {
Linus Walleij05ccbe72006-06-13 07:46:58 +00004144 localph = device->default_video_folder;
4145 } else if (of == PTP_OFC_EXIF_JPEG ||
Linus Walleij5fb47132006-12-30 15:35:48 +00004146 of == PTP_OFC_JP2 ||
4147 of == PTP_OFC_JPX ||
Linus Walleij05ccbe72006-06-13 07:46:58 +00004148 of == PTP_OFC_JFIF ||
4149 of == PTP_OFC_TIFF ||
Linus Walleij5fb47132006-12-30 15:35:48 +00004150 of == PTP_OFC_TIFF_IT ||
Linus Walleij05ccbe72006-06-13 07:46:58 +00004151 of == PTP_OFC_BMP ||
4152 of == PTP_OFC_GIF ||
4153 of == PTP_OFC_PICT ||
4154 of == PTP_OFC_PNG ||
4155 of == PTP_OFC_MTP_WindowsImageFormat) {
4156 localph = device->default_picture_folder;
4157 } else if (of == PTP_OFC_MTP_vCalendar1 ||
Linus Walleijd7aa5b22006-09-02 11:52:31 +00004158 of == PTP_OFC_MTP_vCalendar2 ||
4159 of == PTP_OFC_MTP_UndefinedContact ||
4160 of == PTP_OFC_MTP_vCard2 ||
4161 of == PTP_OFC_MTP_vCard3 ||
4162 of == PTP_OFC_MTP_UndefinedCalendarItem) {
Linus Walleij05ccbe72006-06-13 07:46:58 +00004163 localph = device->default_organizer_folder;
Linus Walleij46651f32008-04-26 23:30:19 +00004164 } else if (of == PTP_OFC_Text) {
Linus Walleij9316e652006-12-07 09:55:21 +00004165 localph = device->default_text_folder;
Linus Walleij05ccbe72006-06-13 07:46:58 +00004166 }
4167 }
mopoke31364442006-11-20 04:53:04 +00004168
Linus Walleija3544f62007-11-30 01:20:04 +00004169 // Here we wire the type to unknown on bugged, but
4170 // Ogg-supportive devices.
Linus Walleijfec4d562008-06-01 22:30:36 +00004171 if (FLAG_OGG_IS_UNKNOWN(ptp_usb) && of == PTP_OFC_MTP_OGG) {
Linus Walleija3544f62007-11-30 01:20:04 +00004172 of = PTP_OFC_Undefined;
4173 }
4174
rreardonbbb4e562006-11-19 22:16:11 +00004175 if (ptp_operation_issupported(params,PTP_OC_MTP_SendObjectPropList)) {
Linus Walleijd2778d12007-08-06 19:24:58 +00004176 /*
4177 * MTP enhanched does it this way (from a sniff):
4178 * -> PTP_OC_MTP_SendObjectPropList (0x9808):
4179 * 20 00 00 00 01 00 08 98 1B 00 00 00 01 00 01 00
4180 * FF FF FF FF 00 30 00 00 00 00 00 00 12 5E 00 00
4181 * Length: 0x00000020
4182 * Type: 0x0001 PTP_USB_CONTAINER_COMMAND
4183 * Code: 0x9808
4184 * Transaction ID: 0x0000001B
4185 * Param1: 0x00010001 <- store
4186 * Param2: 0xffffffff <- parent handle (-1 ?)
4187 * Param3: 0x00003000 <- file type PTP_OFC_Undefined - we don't know about PDF files
4188 * Param4: 0x00000000 <- file length MSB (-0x0c header len)
4189 * Param5: 0x00005e12 <- file length LSB (-0x0c header len)
4190 *
4191 * -> PTP_OC_MTP_SendObjectPropList (0x9808):
4192 * 46 00 00 00 02 00 08 98 1B 00 00 00 03 00 00 00
4193 * 00 00 00 00 07 DC FF FF 0D 4B 00 53 00 30 00 36 - dc07 = file name
4194 * 00 30 00 33 00 30 00 36 00 2E 00 70 00 64 00 66
4195 * 00 00 00 00 00 00 00 03 DC 04 00 00 00 00 00 00 - dc03 = protection status
4196 * 00 4F DC 02 00 01 - dc4f = non consumable
4197 * Length: 0x00000046
4198 * Type: 0x0002 PTP_USB_CONTAINER_DATA
4199 * Code: 0x9808
4200 * Transaction ID: 0x0000001B
4201 * Metadata....
4202 * 0x00000003 <- Number of metadata items
4203 * 0x00000000 <- Object handle, set to 0x00000000 since it is unknown!
4204 * 0xdc07 <- metadata type: file name
4205 * 0xffff <- metadata type: string
4206 * 0x0d <- number of (uint16_t) characters
4207 * 4b 53 30 36 30 33 30 36 2e 50 64 66 00 "KS060306.pdf", null terminated
4208 * 0x00000000 <- Object handle, set to 0x00000000 since it is unknown!
4209 * 0xdc03 <- metadata type: protection status
4210 * 0x0004 <- metadata type: uint16_t
4211 * 0x0000 <- not protected
4212 * 0x00000000 <- Object handle, set to 0x00000000 since it is unknown!
4213 * 0xdc4f <- non consumable
4214 * 0x0002 <- metadata type: uint8_t
4215 * 0x01 <- non-consumable (this device cannot display PDF)
4216 *
4217 * <- Read 0x18 bytes back
4218 * 18 00 00 00 03 00 01 20 1B 00 00 00 01 00 01 00
4219 * 00 00 00 00 01 40 00 00
4220 * Length: 0x000000018
4221 * Type: 0x0003 PTP_USB_CONTAINER_RESPONSE
4222 * Code: 0x2001 PTP_OK
4223 * Transaction ID: 0x0000001B
4224 * Param1: 0x00010001 <- store
4225 * Param2: 0x00000000 <- parent handle
4226 * Param3: 0x00004001 <- new file/object ID
4227 *
4228 * -> PTP_OC_SendObject (0x100d)
4229 * 0C 00 00 00 01 00 0D 10 1C 00 00 00
4230 * -> ... all the bytes ...
4231 * <- Read 0x0c bytes back
4232 * 0C 00 00 00 03 00 01 20 1C 00 00 00
4233 * ... Then update metadata one-by one, actually (instead of sending it first!) ...
4234 */
Linus Walleij1e9a0332007-09-12 19:35:56 +00004235 MTPProperties *props = NULL;
4236 int nrofprops = 0;
4237 MTPProperties *prop = NULL;
4238 uint16_t *properties = NULL;
Linus Walleij07795772007-08-06 18:44:06 +00004239 uint32_t propcnt = 0;
Richard Low6c0a6ce2006-11-26 10:42:08 +00004240
Linus Walleij9036b332008-05-23 21:01:36 +00004241 // default parent handle
Linus Walleij07795772007-08-06 18:44:06 +00004242 if (localph == 0)
4243 localph = 0xFFFFFFFFU; // Set to -1
4244
Richard Low6c0a6ce2006-11-26 10:42:08 +00004245 // Must be 0x00000000U for new objects
4246 filedata->item_id = 0x00000000U;
Linus Walleij05ccbe72006-06-13 07:46:58 +00004247
Linus Walleij1e9a0332007-09-12 19:35:56 +00004248 ret = ptp_mtp_getobjectpropssupported(params, of, &propcnt, &properties);
mopoke31364442006-11-20 04:53:04 +00004249
rreardonbbb4e562006-11-19 22:16:11 +00004250 for (i=0;i<propcnt;i++) {
Linus Walleijdbcc8242007-08-05 22:31:26 +00004251 PTPObjectPropDesc opd;
4252
Linus Walleij1e9a0332007-09-12 19:35:56 +00004253 ret = ptp_mtp_getobjectpropdesc(params, properties[i], of, &opd);
Linus Walleijdbcc8242007-08-05 22:31:26 +00004254 if (ret != PTP_RC_OK) {
Linus Walleij07795772007-08-06 18:44:06 +00004255 add_ptp_error_to_errorstack(device, ret, "LIBMTP_Send_File_From_File_Descriptor(): "
Linus Walleijdbcc8242007-08-05 22:31:26 +00004256 "could not get property description.");
4257 } else if (opd.GetSet) {
Linus Walleij1e9a0332007-09-12 19:35:56 +00004258 switch (properties[i]) {
Linus Walleijdbcc8242007-08-05 22:31:26 +00004259 case PTP_OPC_ObjectFileName:
Linus Walleija6d0d482007-10-31 08:54:56 +00004260 prop = ptp_get_new_object_prop_entry(&props,&nrofprops);
Linus Walleijdbcc8242007-08-05 22:31:26 +00004261 prop->ObjectHandle = filedata->item_id;
4262 prop->property = PTP_OPC_ObjectFileName;
4263 prop->datatype = PTP_DTC_STR;
Linus Walleijd3b78572007-08-24 21:28:24 +00004264 if (filedata->filename != NULL) {
Linus Walleij07795772007-08-06 18:44:06 +00004265 prop->propval.str = strdup(filedata->filename);
Linus Walleijfec4d562008-06-01 22:30:36 +00004266 if (FLAG_ONLY_7BIT_FILENAMES(ptp_usb)) {
Linus Walleijd3b78572007-08-24 21:28:24 +00004267 strip_7bit_from_utf8(prop->propval.str);
4268 }
4269 }
Linus Walleijdbcc8242007-08-05 22:31:26 +00004270 break;
4271 case PTP_OPC_ProtectionStatus:
Linus Walleija6d0d482007-10-31 08:54:56 +00004272 prop = ptp_get_new_object_prop_entry(&props,&nrofprops);
Linus Walleijdbcc8242007-08-05 22:31:26 +00004273 prop->ObjectHandle = filedata->item_id;
4274 prop->property = PTP_OPC_ProtectionStatus;
4275 prop->datatype = PTP_DTC_UINT16;
4276 prop->propval.u16 = 0x0000U; /* Not protected */
Linus Walleijdbcc8242007-08-05 22:31:26 +00004277 break;
4278 case PTP_OPC_NonConsumable:
Linus Walleija6d0d482007-10-31 08:54:56 +00004279 prop = ptp_get_new_object_prop_entry(&props,&nrofprops);
Linus Walleijdbcc8242007-08-05 22:31:26 +00004280 prop->ObjectHandle = filedata->item_id;
4281 prop->property = PTP_OPC_NonConsumable;
4282 prop->datatype = PTP_DTC_UINT8;
Linus Walleij7783b9b2007-09-22 22:10:44 +00004283 prop->propval.u8 = 0x00; /* It is supported, then it is consumable */
Linus Walleijdbcc8242007-08-05 22:31:26 +00004284 break;
4285 case PTP_OPC_Name:
Linus Walleija6d0d482007-10-31 08:54:56 +00004286 prop = ptp_get_new_object_prop_entry(&props,&nrofprops);
Linus Walleijdbcc8242007-08-05 22:31:26 +00004287 prop->ObjectHandle = filedata->item_id;
4288 prop->property = PTP_OPC_Name;
4289 prop->datatype = PTP_DTC_STR;
Linus Walleij07795772007-08-06 18:44:06 +00004290 if (filedata->filename != NULL)
4291 prop->propval.str = strdup(filedata->filename);
Linus Walleijdbcc8242007-08-05 22:31:26 +00004292 break;
Linus Walleij7783b9b2007-09-22 22:10:44 +00004293 case PTP_OPC_DateModified:
4294 // Tag with current time if that is supported
Linus Walleija6d0d482007-10-31 08:54:56 +00004295 prop = ptp_get_new_object_prop_entry(&props,&nrofprops);
Linus Walleij7783b9b2007-09-22 22:10:44 +00004296 prop->ObjectHandle = filedata->item_id;
4297 prop->property = PTP_OPC_DateModified;
4298 prop->datatype = PTP_DTC_STR;
4299 prop->propval.str = get_iso8601_stamp();
4300 break;
Linus Walleijdbcc8242007-08-05 22:31:26 +00004301 }
rreardonbbb4e562006-11-19 22:16:11 +00004302 }
Linus Walleijdbcc8242007-08-05 22:31:26 +00004303 ptp_free_objectpropdesc(&opd);
Linus Walleij99310d42006-11-01 08:29:39 +00004304 }
Linus Walleij1e9a0332007-09-12 19:35:56 +00004305 free(properties);
mopoke31364442006-11-20 04:53:04 +00004306
rreardonbbb4e562006-11-19 22:16:11 +00004307 ret = ptp_mtp_sendobjectproplist(params, &store, &localph, &filedata->item_id,
Linus Walleij1e9a0332007-09-12 19:35:56 +00004308 of, filedata->filesize, props, nrofprops);
mopoke31364442006-11-20 04:53:04 +00004309
rreardonbbb4e562006-11-19 22:16:11 +00004310 /* Free property list */
Linus Walleija6d0d482007-10-31 08:54:56 +00004311 ptp_destroy_object_prop_list(props, nrofprops);
mopoke31364442006-11-20 04:53:04 +00004312
rreardonbbb4e562006-11-19 22:16:11 +00004313 if (ret != PTP_RC_OK) {
Linus Walleij07795772007-08-06 18:44:06 +00004314 add_ptp_error_to_errorstack(device, ret, "LIBMTP_Send_File_From_File_Descriptor():"
4315 "Could not send object property list.");
rreardonbbb4e562006-11-19 22:16:11 +00004316 if (ret == PTP_RC_AccessDenied) {
Linus Walleij070e9b42007-01-22 08:49:28 +00004317 add_ptp_error_to_errorstack(device, ret, "ACCESS DENIED.");
rreardonbbb4e562006-11-19 22:16:11 +00004318 }
4319 return -1;
4320 }
4321 } else if (ptp_operation_issupported(params,PTP_OC_SendObjectInfo)) {
Linus Walleij07795772007-08-06 18:44:06 +00004322 PTPObjectInfo new_file;
4323
4324 memset(&new_file, 0, sizeof(PTPObjectInfo));
4325
4326 new_file.Filename = filedata->filename;
Linus Walleijfec4d562008-06-01 22:30:36 +00004327 if (FLAG_ONLY_7BIT_FILENAMES(ptp_usb)) {
Linus Walleijd3b78572007-08-24 21:28:24 +00004328 strip_7bit_from_utf8(new_file.Filename);
4329 }
Linus Walleij1b87ea72007-08-28 07:53:09 +00004330 // We loose precision here.
4331 new_file.ObjectCompressedSize = (uint32_t) filedata->filesize;
Linus Walleij07795772007-08-06 18:44:06 +00004332 new_file.ObjectFormat = of;
4333 new_file.StorageID = store;
4334 new_file.ParentObject = localph;
4335
Linus Walleij9be685b2006-11-21 09:44:53 +00004336 // Create the object
4337 ret = ptp_sendobjectinfo(params, &store, &localph, &filedata->item_id, &new_file);
Linus Walleij07795772007-08-06 18:44:06 +00004338
Linus Walleij9be685b2006-11-21 09:44:53 +00004339 if (ret != PTP_RC_OK) {
Linus Walleij07795772007-08-06 18:44:06 +00004340 add_ptp_error_to_errorstack(device, ret, "LIBMTP_Send_File_From_File_Descriptor(): "
4341 "Could not send object info.");
Linus Walleij9be685b2006-11-21 09:44:53 +00004342 if (ret == PTP_RC_AccessDenied) {
Linus Walleij070e9b42007-01-22 08:49:28 +00004343 add_ptp_error_to_errorstack(device, ret, "ACCESS DENIED.");
Linus Walleij9be685b2006-11-21 09:44:53 +00004344 }
4345 return -1;
4346 }
Linus Walleijf0bf4372007-07-01 21:47:38 +00004347 // NOTE: the char* pointers inside new_file are not copies so don't
4348 // try to destroy this objectinfo!
rreardonbbb4e562006-11-19 22:16:11 +00004349 }
Linus Walleijd2778d12007-08-06 19:24:58 +00004350
4351 // Now there IS an object with this parent handle.
4352 filedata->parent_id = localph;
Linus Walleij817579a2008-05-23 21:21:40 +00004353
Linus Walleij1b87ea72007-08-28 07:53:09 +00004354 // Callbacks
4355 ptp_usb->callback_active = 1;
4356 // The callback will deactivate itself after this amount of data has been sent
4357 // One BULK header for the request, one for the data phase. No parameters to the request.
4358 ptp_usb->current_transfer_total = filedata->filesize+PTP_USB_BULK_HDR_LEN*2;
4359 ptp_usb->current_transfer_complete = 0;
4360 ptp_usb->current_transfer_callback = callback;
4361 ptp_usb->current_transfer_callback_data = data;
4362
4363 ret = ptp_sendobject_fromfd(params, fd, filedata->filesize);
4364
4365 ptp_usb->callback_active = 0;
4366 ptp_usb->current_transfer_callback = NULL;
4367 ptp_usb->current_transfer_callback_data = NULL;
Linus Walleijbd3bf9e2007-09-14 19:31:54 +00004368
4369 if (ret == PTP_ERROR_CANCEL) {
Linus Walleij25d33b52007-09-16 21:36:19 +00004370 add_error_to_errorstack(device, LIBMTP_ERROR_CANCELLED, "LIBMTP_Send_File_From_File_Descriptor(): Cancelled transfer.");
Linus Walleijbd3bf9e2007-09-14 19:31:54 +00004371 return -1;
Linus Walleij25d33b52007-09-16 21:36:19 +00004372 }
Linus Walleijee73ef22006-08-27 19:56:00 +00004373 if (ret != PTP_RC_OK) {
Linus Walleijd2778d12007-08-06 19:24:58 +00004374 add_ptp_error_to_errorstack(device, ret, "LIBMTP_Send_File_From_File_Descriptor(): "
Linus Walleij07795772007-08-06 18:44:06 +00004375 "Could not send object.");
Linus Walleijee73ef22006-08-27 19:56:00 +00004376 return -1;
4377 }
mopoke96143402006-10-30 04:37:26 +00004378
Linus Walleija6d0d482007-10-31 08:54:56 +00004379 add_object_to_cache(device, filedata->item_id);
Linus Walleij817579a2008-05-23 21:21:40 +00004380
4381 /*
4382 * Get the device-assined parent_id from the cache.
4383 * The operation that adds it to the cache will
4384 * look it up from the device, so we get the new
4385 * parent_id from the cache.
4386 */
4387 newfilemeta = LIBMTP_Get_Filemetadata(device, filedata->item_id);
4388 if (newfilemeta != NULL) {
4389 filedata->parent_id = newfilemeta->parent_id;
Linus Walleijea68f1f2008-06-22 21:54:44 +00004390 filedata->storage_id = newfilemeta->storage_id;
Linus Walleij817579a2008-05-23 21:21:40 +00004391 LIBMTP_destroy_file_t(newfilemeta);
4392 } else {
4393 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL,
4394 "LIBMTP_Send_File_From_File_Descriptor(): "
4395 "Could not retrieve updated metadata.");
4396 return -1;
4397 }
tsaarnia3eb60a2007-07-06 17:41:30 +00004398
Linus Walleijd208f9c2006-04-27 14:16:06 +00004399 return 0;
4400}
4401
Linus Walleij17e39f72006-02-23 15:54:28 +00004402/**
Linus Walleij9036b332008-05-23 21:01:36 +00004403 * This function updates the MTP track object metadata on a
4404 * single file identified by an object ID.
mopoke96143402006-10-30 04:37:26 +00004405 * @param device a pointer to the device to update the track
Linus Walleij95698cd2006-02-24 10:40:40 +00004406 * metadata on.
Linus Walleij17e39f72006-02-23 15:54:28 +00004407 * @param metadata a track metadata set to be written to the file.
4408 * notice that the <code>track_id</code> field of the
4409 * metadata structure must be correct so that the
Linus Walleija4982732006-02-24 15:46:02 +00004410 * function can update the right file. If some properties
4411 * of this metadata are set to NULL (strings) or 0
4412 * (numerical values) they will be discarded and the
4413 * track will not be tagged with these blank values.
Richard Lowaf20b5d2006-12-17 18:00:59 +00004414 * @return 0 on success, any other value means failure. If some
4415 * or all of the properties fail to update we will still
4416 * return success. On some devices (notably iRiver T30)
4417 * properties that exist cannot be updated.
Linus Walleij17e39f72006-02-23 15:54:28 +00004418 */
mopoke96143402006-10-30 04:37:26 +00004419int LIBMTP_Update_Track_Metadata(LIBMTP_mtpdevice_t *device,
Linus Walleij17e39f72006-02-23 15:54:28 +00004420 LIBMTP_track_t const * const metadata)
4421{
Linus Walleij17e39f72006-02-23 15:54:28 +00004422 uint16_t ret;
Linus Walleij00cf0642006-07-26 20:40:59 +00004423 PTPParams *params = (PTPParams *) device->params;
Linus Walleijf9267e92007-10-15 21:07:37 +00004424 PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo;
Linus Walleij00cf0642006-07-26 20:40:59 +00004425 uint32_t i;
Linus Walleij1e9a0332007-09-12 19:35:56 +00004426 uint16_t *properties = NULL;
Linus Walleij00cf0642006-07-26 20:40:59 +00004427 uint32_t propcnt = 0;
Linus Walleij17e39f72006-02-23 15:54:28 +00004428
mopoke96143402006-10-30 04:37:26 +00004429 // First see which properties can be set on this file format and apply accordingly
Linus Walleij00cf0642006-07-26 20:40:59 +00004430 // i.e only try to update this metadata for object tags that exist on the current player.
Linus Walleij1e9a0332007-09-12 19:35:56 +00004431 ret = ptp_mtp_getobjectpropssupported(params, map_libmtp_type_to_ptp_type(metadata->filetype), &propcnt, &properties);
Linus Walleij00cf0642006-07-26 20:40:59 +00004432 if (ret != PTP_RC_OK) {
4433 // Just bail out for now, nothing is ever set.
Linus Walleije7df6532007-03-23 08:20:06 +00004434 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): "
4435 "could not retrieve supported object properties.");
raveloxd9a28642006-05-26 23:42:22 +00004436 return -1;
Richard Low15731fe2007-03-22 20:27:20 +00004437 }
Linus Walleijf9267e92007-10-15 21:07:37 +00004438 if (ptp_operation_issupported(params, PTP_OC_MTP_SetObjPropList) &&
Linus Walleijfec4d562008-06-01 22:30:36 +00004439 !FLAG_BROKEN_SET_OBJECT_PROPLIST(ptp_usb)) {
Linus Walleij1e9a0332007-09-12 19:35:56 +00004440 MTPProperties *props = NULL;
4441 MTPProperties *prop = NULL;
4442 int nrofprops = 0;
Richard Low15731fe2007-03-22 20:27:20 +00004443
4444 for (i=0;i<propcnt;i++) {
Linus Walleijdbcc8242007-08-05 22:31:26 +00004445 PTPObjectPropDesc opd;
4446
Linus Walleij1e9a0332007-09-12 19:35:56 +00004447 ret = ptp_mtp_getobjectpropdesc(params, properties[i], map_libmtp_type_to_ptp_type(metadata->filetype), &opd);
Linus Walleijdbcc8242007-08-05 22:31:26 +00004448 if (ret != PTP_RC_OK) {
4449 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): "
4450 "could not get property description.");
4451 } else if (opd.GetSet) {
Linus Walleij1e9a0332007-09-12 19:35:56 +00004452 switch (properties[i]) {
Linus Walleijdbcc8242007-08-05 22:31:26 +00004453 case PTP_OPC_Name:
4454 if (metadata->title == NULL)
4455 break;
Linus Walleija6d0d482007-10-31 08:54:56 +00004456 prop = ptp_get_new_object_prop_entry(&props, &nrofprops);
Linus Walleije7df6532007-03-23 08:20:06 +00004457 prop->ObjectHandle = metadata->item_id;
4458 prop->property = PTP_OPC_Name;
4459 prop->datatype = PTP_DTC_STR;
Linus Walleijdbcc8242007-08-05 22:31:26 +00004460 prop->propval.str = strdup(metadata->title);
Linus Walleije7df6532007-03-23 08:20:06 +00004461 break;
Linus Walleijdbcc8242007-08-05 22:31:26 +00004462 case PTP_OPC_AlbumName:
4463 if (metadata->album == NULL)
4464 break;
Linus Walleija6d0d482007-10-31 08:54:56 +00004465 prop = ptp_get_new_object_prop_entry(&props, &nrofprops);
Linus Walleije7df6532007-03-23 08:20:06 +00004466 prop->ObjectHandle = metadata->item_id;
4467 prop->property = PTP_OPC_AlbumName;
4468 prop->datatype = PTP_DTC_STR;
Linus Walleijdbcc8242007-08-05 22:31:26 +00004469 prop->propval.str = strdup(metadata->album);
Linus Walleije7df6532007-03-23 08:20:06 +00004470 break;
Linus Walleijdbcc8242007-08-05 22:31:26 +00004471 case PTP_OPC_Artist:
4472 if (metadata->artist == NULL)
4473 break;
Linus Walleija6d0d482007-10-31 08:54:56 +00004474 prop = ptp_get_new_object_prop_entry(&props, &nrofprops);
Linus Walleije7df6532007-03-23 08:20:06 +00004475 prop->ObjectHandle = metadata->item_id;
4476 prop->property = PTP_OPC_Artist;
4477 prop->datatype = PTP_DTC_STR;
Linus Walleijdbcc8242007-08-05 22:31:26 +00004478 prop->propval.str = strdup(metadata->artist);
Linus Walleije7df6532007-03-23 08:20:06 +00004479 break;
Linus Walleij31b74292008-05-02 23:29:06 +00004480 case PTP_OPC_Composer:
4481 if (metadata->composer == NULL)
4482 break;
4483 prop = ptp_get_new_object_prop_entry(&props, &nrofprops);
4484 prop->ObjectHandle = metadata->item_id;
4485 prop->property = PTP_OPC_Composer;
4486 prop->datatype = PTP_DTC_STR;
4487 prop->propval.str = strdup(metadata->composer);
4488 break;
Linus Walleijdbcc8242007-08-05 22:31:26 +00004489 case PTP_OPC_Genre:
4490 if (metadata->genre == NULL)
4491 break;
Linus Walleija6d0d482007-10-31 08:54:56 +00004492 prop = ptp_get_new_object_prop_entry(&props, &nrofprops);
Linus Walleije7df6532007-03-23 08:20:06 +00004493 prop->ObjectHandle = metadata->item_id;
4494 prop->property = PTP_OPC_Genre;
4495 prop->datatype = PTP_DTC_STR;
Linus Walleijdbcc8242007-08-05 22:31:26 +00004496 prop->propval.str = strdup(metadata->genre);
Linus Walleije7df6532007-03-23 08:20:06 +00004497 break;
Linus Walleijdbcc8242007-08-05 22:31:26 +00004498 case PTP_OPC_Duration:
Linus Walleija6d0d482007-10-31 08:54:56 +00004499 prop = ptp_get_new_object_prop_entry(&props, &nrofprops);
Linus Walleije7df6532007-03-23 08:20:06 +00004500 prop->ObjectHandle = metadata->item_id;
4501 prop->property = PTP_OPC_Duration;
4502 prop->datatype = PTP_DTC_UINT32;
Linus Walleij29559562007-08-22 21:38:04 +00004503 prop->propval.u32 = adjust_u32(metadata->duration, &opd);
Linus Walleije7df6532007-03-23 08:20:06 +00004504 break;
Linus Walleijdbcc8242007-08-05 22:31:26 +00004505 case PTP_OPC_Track:
Linus Walleija6d0d482007-10-31 08:54:56 +00004506 prop = ptp_get_new_object_prop_entry(&props, &nrofprops);
Linus Walleije7df6532007-03-23 08:20:06 +00004507 prop->ObjectHandle = metadata->item_id;
4508 prop->property = PTP_OPC_Track;
4509 prop->datatype = PTP_DTC_UINT16;
Linus Walleij29559562007-08-22 21:38:04 +00004510 prop->propval.u16 = adjust_u16(metadata->tracknumber, &opd);
Linus Walleije7df6532007-03-23 08:20:06 +00004511 break;
Linus Walleijdbcc8242007-08-05 22:31:26 +00004512 case PTP_OPC_OriginalReleaseDate:
4513 if (metadata->date == NULL)
4514 break;
Linus Walleija6d0d482007-10-31 08:54:56 +00004515 prop = ptp_get_new_object_prop_entry(&props, &nrofprops);
Linus Walleije7df6532007-03-23 08:20:06 +00004516 prop->ObjectHandle = metadata->item_id;
4517 prop->property = PTP_OPC_OriginalReleaseDate;
4518 prop->datatype = PTP_DTC_STR;
Linus Walleijdbcc8242007-08-05 22:31:26 +00004519 prop->propval.str = strdup(metadata->date);
Linus Walleije7df6532007-03-23 08:20:06 +00004520 break;
Linus Walleijdbcc8242007-08-05 22:31:26 +00004521 case PTP_OPC_SampleRate:
Linus Walleija6d0d482007-10-31 08:54:56 +00004522 prop = ptp_get_new_object_prop_entry(&props, &nrofprops);
Linus Walleije7df6532007-03-23 08:20:06 +00004523 prop->ObjectHandle = metadata->item_id;
4524 prop->property = PTP_OPC_SampleRate;
4525 prop->datatype = PTP_DTC_UINT32;
Linus Walleij29559562007-08-22 21:38:04 +00004526 prop->propval.u32 = adjust_u32(metadata->samplerate, &opd);
Linus Walleije7df6532007-03-23 08:20:06 +00004527 break;
Linus Walleijdbcc8242007-08-05 22:31:26 +00004528 case PTP_OPC_NumberOfChannels:
Linus Walleija6d0d482007-10-31 08:54:56 +00004529 prop = ptp_get_new_object_prop_entry(&props, &nrofprops);
Linus Walleije7df6532007-03-23 08:20:06 +00004530 prop->ObjectHandle = metadata->item_id;
4531 prop->property = PTP_OPC_NumberOfChannels;
4532 prop->datatype = PTP_DTC_UINT16;
Linus Walleij29559562007-08-22 21:38:04 +00004533 prop->propval.u16 = adjust_u16(metadata->nochannels, &opd);
Linus Walleije7df6532007-03-23 08:20:06 +00004534 break;
Linus Walleijdbcc8242007-08-05 22:31:26 +00004535 case PTP_OPC_AudioWAVECodec:
Linus Walleija6d0d482007-10-31 08:54:56 +00004536 prop = ptp_get_new_object_prop_entry(&props, &nrofprops);
Linus Walleije7df6532007-03-23 08:20:06 +00004537 prop->ObjectHandle = metadata->item_id;
4538 prop->property = PTP_OPC_AudioWAVECodec;
4539 prop->datatype = PTP_DTC_UINT32;
Linus Walleij29559562007-08-22 21:38:04 +00004540 prop->propval.u32 = adjust_u32(metadata->wavecodec, &opd);
Linus Walleije7df6532007-03-23 08:20:06 +00004541 break;
Linus Walleijdbcc8242007-08-05 22:31:26 +00004542 case PTP_OPC_AudioBitRate:
Linus Walleija6d0d482007-10-31 08:54:56 +00004543 prop = ptp_get_new_object_prop_entry(&props, &nrofprops);
Linus Walleije7df6532007-03-23 08:20:06 +00004544 prop->ObjectHandle = metadata->item_id;
4545 prop->property = PTP_OPC_AudioBitRate;
4546 prop->datatype = PTP_DTC_UINT32;
Linus Walleij29559562007-08-22 21:38:04 +00004547 prop->propval.u32 = adjust_u32(metadata->bitrate, &opd);
Linus Walleije7df6532007-03-23 08:20:06 +00004548 break;
Linus Walleijdbcc8242007-08-05 22:31:26 +00004549 case PTP_OPC_BitRateType:
Linus Walleija6d0d482007-10-31 08:54:56 +00004550 prop = ptp_get_new_object_prop_entry(&props, &nrofprops);
Linus Walleije7df6532007-03-23 08:20:06 +00004551 prop->ObjectHandle = metadata->item_id;
4552 prop->property = PTP_OPC_BitRateType;
4553 prop->datatype = PTP_DTC_UINT16;
Linus Walleij29559562007-08-22 21:38:04 +00004554 prop->propval.u16 = adjust_u16(metadata->bitratetype, &opd);
Linus Walleije7df6532007-03-23 08:20:06 +00004555 break;
Linus Walleijdbcc8242007-08-05 22:31:26 +00004556 case PTP_OPC_Rating:
4557 // TODO: shall this be set for rating 0?
4558 if (metadata->rating == 0)
4559 break;
Linus Walleija6d0d482007-10-31 08:54:56 +00004560 prop = ptp_get_new_object_prop_entry(&props, &nrofprops);
Linus Walleijdbcc8242007-08-05 22:31:26 +00004561 prop->ObjectHandle = metadata->item_id;
4562 prop->property = PTP_OPC_Rating;
4563 prop->datatype = PTP_DTC_UINT16;
Linus Walleij29559562007-08-22 21:38:04 +00004564 prop->propval.u16 = adjust_u16(metadata->rating, &opd);
Linus Walleijdbcc8242007-08-05 22:31:26 +00004565 break;
4566 case PTP_OPC_UseCount:
Linus Walleija6d0d482007-10-31 08:54:56 +00004567 prop = ptp_get_new_object_prop_entry(&props, &nrofprops);
Linus Walleije7df6532007-03-23 08:20:06 +00004568 prop->ObjectHandle = metadata->item_id;
4569 prop->property = PTP_OPC_UseCount;
4570 prop->datatype = PTP_DTC_UINT32;
Linus Walleij29559562007-08-22 21:38:04 +00004571 prop->propval.u32 = adjust_u32(metadata->usecount, &opd);
Linus Walleij7783b9b2007-09-22 22:10:44 +00004572 break;
4573 case PTP_OPC_DateModified:
4574 // Tag with current time if that is supported
Linus Walleija6d0d482007-10-31 08:54:56 +00004575 prop = ptp_get_new_object_prop_entry(&props, &nrofprops);
Linus Walleij7783b9b2007-09-22 22:10:44 +00004576 prop->ObjectHandle = metadata->item_id;
4577 prop->property = PTP_OPC_DateModified;
4578 prop->datatype = PTP_DTC_STR;
4579 prop->propval.str = get_iso8601_stamp();
4580 break;
Linus Walleijf9267e92007-10-15 21:07:37 +00004581 default:
4582 break;
Linus Walleije7df6532007-03-23 08:20:06 +00004583 }
Linus Walleij304433d2007-02-26 08:02:47 +00004584 }
Linus Walleijdbcc8242007-08-05 22:31:26 +00004585 ptp_free_objectpropdesc(&opd);
Richard Low15731fe2007-03-22 20:27:20 +00004586 }
Linus Walleijdbcc8242007-08-05 22:31:26 +00004587
Richard Low15731fe2007-03-22 20:27:20 +00004588 // NOTE: File size is not updated, this should not change anyway.
4589 // neither will we change the filename.
Linus Walleijdbcc8242007-08-05 22:31:26 +00004590
Linus Walleij1e9a0332007-09-12 19:35:56 +00004591 ret = ptp_mtp_setobjectproplist(params, props, nrofprops);
Linus Walleijdbcc8242007-08-05 22:31:26 +00004592
Linus Walleija6d0d482007-10-31 08:54:56 +00004593 ptp_destroy_object_prop_list(props, nrofprops);
Linus Walleijdbcc8242007-08-05 22:31:26 +00004594
Richard Low15731fe2007-03-22 20:27:20 +00004595 if (ret != PTP_RC_OK) {
4596 // TODO: return error of which property we couldn't set
Linus Walleije7df6532007-03-23 08:20:06 +00004597 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): "
4598 "could not set object property list.");
Richard Low016e3732007-09-23 14:53:46 +00004599 free(properties);
Linus Walleij304433d2007-02-26 08:02:47 +00004600 return -1;
4601 }
Linus Walleijdbcc8242007-08-05 22:31:26 +00004602
Richard Low15731fe2007-03-22 20:27:20 +00004603 } else if (ptp_operation_issupported(params,PTP_OC_MTP_SetObjectPropValue)) {
Linus Walleij00cf0642006-07-26 20:40:59 +00004604 for (i=0;i<propcnt;i++) {
Linus Walleijdbcc8242007-08-05 22:31:26 +00004605 PTPObjectPropDesc opd;
4606
Linus Walleij1e9a0332007-09-12 19:35:56 +00004607 ret = ptp_mtp_getobjectpropdesc(params, properties[i], map_libmtp_type_to_ptp_type(metadata->filetype), &opd);
Linus Walleijdbcc8242007-08-05 22:31:26 +00004608 if (ret != PTP_RC_OK) {
4609 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): "
4610 "could not get property description.");
4611 } else if (opd.GetSet) {
Linus Walleij1e9a0332007-09-12 19:35:56 +00004612 switch (properties[i]) {
Linus Walleijdbcc8242007-08-05 22:31:26 +00004613 case PTP_OPC_Name:
4614 // Update title
4615 ret = set_object_string(device, metadata->item_id, PTP_OPC_Name, metadata->title);
Linus Walleije7df6532007-03-23 08:20:06 +00004616 if (ret != 0) {
4617 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): "
Linus Walleijdbcc8242007-08-05 22:31:26 +00004618 "could not set track title.");
Linus Walleije7df6532007-03-23 08:20:06 +00004619 }
Linus Walleijdbcc8242007-08-05 22:31:26 +00004620 break;
4621 case PTP_OPC_AlbumName:
4622 // Update album
4623 ret = set_object_string(device, metadata->item_id, PTP_OPC_AlbumName, metadata->album);
Linus Walleije7df6532007-03-23 08:20:06 +00004624 if (ret != 0) {
4625 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): "
Linus Walleijdbcc8242007-08-05 22:31:26 +00004626 "could not set track album name.");
Linus Walleije7df6532007-03-23 08:20:06 +00004627 }
Linus Walleijdbcc8242007-08-05 22:31:26 +00004628 break;
4629 case PTP_OPC_Artist:
4630 // Update artist
4631 ret = set_object_string(device, metadata->item_id, PTP_OPC_Artist, metadata->artist);
Linus Walleije7df6532007-03-23 08:20:06 +00004632 if (ret != 0) {
4633 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): "
Linus Walleijdbcc8242007-08-05 22:31:26 +00004634 "could not set track artist name.");
Linus Walleije7df6532007-03-23 08:20:06 +00004635 }
Linus Walleijdbcc8242007-08-05 22:31:26 +00004636 break;
Linus Walleij31b74292008-05-02 23:29:06 +00004637 case PTP_OPC_Composer:
4638 // Update composer
4639 ret = set_object_string(device, metadata->item_id, PTP_OPC_Composer, metadata->composer);
4640 if (ret != 0) {
4641 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): "
4642 "could not set track composer name.");
4643 }
4644 break;
Linus Walleijdbcc8242007-08-05 22:31:26 +00004645 case PTP_OPC_Genre:
4646 // Update genre
4647 ret = set_object_string(device, metadata->item_id, PTP_OPC_Genre, metadata->genre);
4648 if (ret != 0) {
4649 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): "
4650 "could not set track genre name.");
4651 }
4652 break;
4653 case PTP_OPC_Duration:
4654 // Update duration
4655 if (metadata->duration != 0) {
Linus Walleij29559562007-08-22 21:38:04 +00004656 ret = set_object_u32(device, metadata->item_id, PTP_OPC_Duration, adjust_u32(metadata->duration, &opd));
Linus Walleijdbcc8242007-08-05 22:31:26 +00004657 if (ret != 0) {
4658 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): "
4659 "could not set track duration.");
4660 }
4661 }
4662 break;
4663 case PTP_OPC_Track:
4664 // Update track number.
4665 if (metadata->tracknumber != 0) {
Linus Walleij29559562007-08-22 21:38:04 +00004666 ret = set_object_u16(device, metadata->item_id, PTP_OPC_Track, adjust_u16(metadata->tracknumber, &opd));
Linus Walleijdbcc8242007-08-05 22:31:26 +00004667 if (ret != 0) {
4668 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): "
4669 "could not set track tracknumber.");
4670 }
4671 }
4672 break;
4673 case PTP_OPC_OriginalReleaseDate:
4674 // Update creation datetime
4675 ret = set_object_string(device, metadata->item_id, PTP_OPC_OriginalReleaseDate, metadata->date);
4676 if (ret != 0) {
4677 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): "
4678 "could not set track release date.");
4679 }
4680 break;
4681 // These are, well not so important.
4682 case PTP_OPC_SampleRate:
4683 // Update sample rate
4684 if (metadata->samplerate != 0) {
Linus Walleij29559562007-08-22 21:38:04 +00004685 ret = set_object_u32(device, metadata->item_id, PTP_OPC_SampleRate, adjust_u32(metadata->samplerate, &opd));
Linus Walleijdbcc8242007-08-05 22:31:26 +00004686 if (ret != 0) {
4687 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): "
4688 "could not set samplerate.");
4689 }
4690 }
4691 break;
4692 case PTP_OPC_NumberOfChannels:
4693 // Update number of channels
4694 if (metadata->nochannels != 0) {
Linus Walleij29559562007-08-22 21:38:04 +00004695 ret = set_object_u16(device, metadata->item_id, PTP_OPC_NumberOfChannels, adjust_u16(metadata->nochannels, &opd));
Linus Walleije7df6532007-03-23 08:20:06 +00004696 if (ret != 0) {
4697 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): "
4698 "could not set number of channels.");
4699 }
4700 }
Linus Walleijdbcc8242007-08-05 22:31:26 +00004701 break;
4702 case PTP_OPC_AudioWAVECodec:
4703 // Update WAVE codec
4704 if (metadata->wavecodec != 0) {
Linus Walleij29559562007-08-22 21:38:04 +00004705 ret = set_object_u32(device, metadata->item_id, PTP_OPC_AudioWAVECodec, adjust_u32(metadata->wavecodec, &opd));
Linus Walleijdbcc8242007-08-05 22:31:26 +00004706 if (ret != 0) {
4707 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): "
4708 "could not set WAVE codec.");
4709 }
4710 }
4711 break;
4712 case PTP_OPC_AudioBitRate:
4713 // Update bitrate
4714 if (metadata->bitrate != 0) {
Linus Walleij29559562007-08-22 21:38:04 +00004715 ret = set_object_u32(device, metadata->item_id, PTP_OPC_AudioBitRate, adjust_u32(metadata->bitrate, &opd));
Linus Walleijdbcc8242007-08-05 22:31:26 +00004716 if (ret != 0) {
4717 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): "
4718 "could not set bitrate.");
4719 }
4720 }
4721 break;
4722 case PTP_OPC_BitRateType:
4723 // Update bitrate type
4724 if (metadata->bitratetype != 0) {
Linus Walleij29559562007-08-22 21:38:04 +00004725 ret = set_object_u16(device, metadata->item_id, PTP_OPC_BitRateType, adjust_u16(metadata->bitratetype, &opd));
Linus Walleijdbcc8242007-08-05 22:31:26 +00004726 if (ret != 0) {
4727 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): "
4728 "could not set bitratetype.");
4729 }
4730 }
4731 break;
4732 case PTP_OPC_Rating:
4733 // Update user rating
4734 // TODO: shall this be set for rating 0?
4735 if (metadata->rating != 0) {
Linus Walleij29559562007-08-22 21:38:04 +00004736 ret = set_object_u16(device, metadata->item_id, PTP_OPC_Rating, adjust_u16(metadata->rating, &opd));
Linus Walleijdbcc8242007-08-05 22:31:26 +00004737 if (ret != 0) {
4738 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): "
4739 "could not set user rating.");
4740 }
4741 }
4742 break;
4743 case PTP_OPC_UseCount:
4744 // Update use count, set even to zero if desired.
Linus Walleij29559562007-08-22 21:38:04 +00004745 ret = set_object_u32(device, metadata->item_id, PTP_OPC_UseCount, adjust_u32(metadata->usecount, &opd));
Linus Walleije7df6532007-03-23 08:20:06 +00004746 if (ret != 0) {
4747 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): "
Linus Walleije7df6532007-03-23 08:20:06 +00004748 "could not set use count.");
Linus Walleijdbcc8242007-08-05 22:31:26 +00004749 }
4750 break;
Linus Walleij7783b9b2007-09-22 22:10:44 +00004751 case PTP_OPC_DateModified:
4752 {
4753 // Update modification time if supported
4754 char *tmpstamp = get_iso8601_stamp();
4755 ret = set_object_string(device, metadata->item_id, PTP_OPC_DateModified, tmpstamp);
4756 if (ret != 0) {
4757 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): "
4758 "could not set modification date.");
4759 }
4760 free(tmpstamp);
4761 }
4762 break;
Linus Walleijdbcc8242007-08-05 22:31:26 +00004763
4764 // NOTE: File size is not updated, this should not change anyway.
4765 // neither will we change the filename.
4766 default:
4767 break;
Linus Walleije7df6532007-03-23 08:20:06 +00004768 }
Linus Walleij00cf0642006-07-26 20:40:59 +00004769 }
Linus Walleijdbcc8242007-08-05 22:31:26 +00004770 ptp_free_objectpropdesc(&opd);
Linus Walleija4982732006-02-24 15:46:02 +00004771 }
Richard Low15731fe2007-03-22 20:27:20 +00004772 } else {
4773 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Update_Track_Metadata(): "
4774 "Your device doesn't seem to support any known way of setting metadata.");
Richard Low016e3732007-09-23 14:53:46 +00004775 free(properties);
Richard Low15731fe2007-03-22 20:27:20 +00004776 return -1;
Linus Walleij17e39f72006-02-23 15:54:28 +00004777 }
Linus Walleije7df6532007-03-23 08:20:06 +00004778
tsaarnia3eb60a2007-07-06 17:41:30 +00004779 // update cached object properties if metadata cache exists
Linus Walleij7752b952007-10-19 20:11:46 +00004780 update_metadata_cache(device, metadata->item_id);
Linus Walleij338ade42007-07-03 20:44:08 +00004781
Richard Low016e3732007-09-23 14:53:46 +00004782 free(properties);
4783
Linus Walleijf1b02f22006-12-06 09:03:23 +00004784 return 0;
Linus Walleij394bbbe2006-02-22 16:10:53 +00004785}
Linus Walleij95698cd2006-02-24 10:40:40 +00004786
4787/**
Linus Walleij399807b2008-01-27 22:05:12 +00004788 * This function deletes a single file, track, playlist, folder or
4789 * any other object off the MTP device, identified by the object ID.
4790 *
4791 * If you delete a folder, there is no guarantee that the device will
4792 * really delete all the files that were in that folder, rather it is
4793 * expected that they will not be deleted, and will turn up in object
4794 * listings with parent set to a non-existant object ID. The safe way
4795 * to do this is to recursively delete all files (and folders) contained
4796 * in the folder, then the folder itself.
4797 *
4798 * @param device a pointer to the device to delete the object from.
4799 * @param object_id the object to delete.
Linus Walleij95698cd2006-02-24 10:40:40 +00004800 * @return 0 on success, any other value means failure.
4801 */
mopoke96143402006-10-30 04:37:26 +00004802int LIBMTP_Delete_Object(LIBMTP_mtpdevice_t *device,
Linus Walleij438bd7f2006-06-08 11:35:44 +00004803 uint32_t object_id)
Linus Walleij95698cd2006-02-24 10:40:40 +00004804{
Linus Walleij438bd7f2006-06-08 11:35:44 +00004805 uint16_t ret;
4806 PTPParams *params = (PTPParams *) device->params;
4807
4808 ret = ptp_deleteobject(params, object_id, 0);
4809 if (ret != PTP_RC_OK) {
Linus Walleij070e9b42007-01-22 08:49:28 +00004810 add_ptp_error_to_errorstack(device, ret, "LIBMTP_Delete_Object(): could not delete object.");
Linus Walleij438bd7f2006-06-08 11:35:44 +00004811 return -1;
4812 }
tsaarnia3eb60a2007-07-06 17:41:30 +00004813
Linus Walleij438bd7f2006-06-08 11:35:44 +00004814 return 0;
Linus Walleij95698cd2006-02-24 10:40:40 +00004815}
Linus Walleij9c6ca022006-04-21 10:24:15 +00004816
Linus Walleij9c6ca022006-04-21 10:24:15 +00004817/**
Linus Walleij399807b2008-01-27 22:05:12 +00004818 * This function renames a single file, track, playlist, folder or
4819 * any other object on the MTP device, identified by an object ID.
4820 * This simply means that the PTP_OPC_ObjectFileName property
4821 * is updated, if this is supported by the device.
4822 *
4823 * @param device a pointer to the device that contains the the file,
4824 * track, foler, playlist or other object to set the filename for.
4825 * @param object_id the ID of the object to rename.
Linus Walleij8be2c032008-01-28 21:23:47 +00004826 * @param newname the new filename for this object. You MUST assume that
4827 * this string can be modified by the call to this function, since
4828 * some devices have restrictions as to which filenames may be
4829 * used on them.
Linus Walleijde8193f2008-01-27 14:55:31 +00004830 * @return 0 on success, any other value means failure.
4831 */
4832int LIBMTP_Set_Object_Filename(LIBMTP_mtpdevice_t *device,
Linus Walleij8be2c032008-01-28 21:23:47 +00004833 uint32_t object_id, char *newname)
Linus Walleijde8193f2008-01-27 14:55:31 +00004834{
4835 PTPParams *params = (PTPParams *) device->params;
4836 PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo;
Linus Walleij8be2c032008-01-28 21:23:47 +00004837 LIBMTP_file_t *file;
4838 uint16_t ptp_type;
Linus Walleijde8193f2008-01-27 14:55:31 +00004839 PTPObjectPropDesc opd;
4840 uint16_t ret;
4841
Linus Walleij8be2c032008-01-28 21:23:47 +00004842 // Get metadata for this object.
4843 file = LIBMTP_Get_Filemetadata(device, object_id);
4844 if (file == NULL) {
4845 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Set_Object_Filename(): "
4846 "could not get file metadata for target object.");
4847 return -1;
4848 }
4849 ptp_type = map_libmtp_type_to_ptp_type(file->filetype);
4850 LIBMTP_destroy_file_t(file);
4851
4852 // See if we can modify the filename on this kind of files.
4853 ret = ptp_mtp_getobjectpropdesc(params, PTP_OPC_ObjectFileName, ptp_type, &opd);
Linus Walleijde8193f2008-01-27 14:55:31 +00004854 if (ret != PTP_RC_OK) {
Linus Walleij399807b2008-01-27 22:05:12 +00004855 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Set_Object_Filename(): "
Linus Walleij8be2c032008-01-28 21:23:47 +00004856 "could not get property description.");
Linus Walleijde8193f2008-01-27 14:55:31 +00004857 return -1;
4858 }
4859
4860 if (!opd.GetSet) {
4861 ptp_free_objectpropdesc(&opd);
Linus Walleij399807b2008-01-27 22:05:12 +00004862 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Set_Object_Filename(): "
Linus Walleij8be2c032008-01-28 21:23:47 +00004863 "property is not settable.");
Linus Walleij399807b2008-01-27 22:05:12 +00004864 // TODO: we COULD actually upload/download the object here, if we feel
4865 // like wasting time for the user.
Linus Walleijde8193f2008-01-27 14:55:31 +00004866 return -1;
4867 }
4868
Linus Walleijfec4d562008-06-01 22:30:36 +00004869 if (FLAG_ONLY_7BIT_FILENAMES(ptp_usb)) {
Linus Walleijde8193f2008-01-27 14:55:31 +00004870 strip_7bit_from_utf8(newname);
4871 }
4872
4873 if (ptp_operation_issupported(params, PTP_OC_MTP_SetObjPropList) &&
Linus Walleijfec4d562008-06-01 22:30:36 +00004874 !FLAG_BROKEN_SET_OBJECT_PROPLIST(ptp_usb)) {
Linus Walleijde8193f2008-01-27 14:55:31 +00004875 MTPProperties *props = NULL;
4876 MTPProperties *prop = NULL;
4877 int nrofprops = 0;
4878
4879 prop = ptp_get_new_object_prop_entry(&props, &nrofprops);
4880 prop->ObjectHandle = object_id;
4881 prop->property = PTP_OPC_ObjectFileName;
4882 prop->datatype = PTP_DTC_STR;
4883 prop->propval.str = strdup(newname);
4884
4885 ret = ptp_mtp_setobjectproplist(params, props, nrofprops);
4886
4887 ptp_destroy_object_prop_list(props, nrofprops);
4888
4889 if (ret != PTP_RC_OK) {
4890 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Set_Oject_Filename(): "
4891 "could not set object property list.");
4892 ptp_free_objectpropdesc(&opd);
4893 return -1;
4894 }
4895 } else if (ptp_operation_issupported(params, PTP_OC_MTP_SetObjectPropValue)) {
4896 ret = set_object_string(device, object_id, PTP_OPC_ObjectFileName, newname);
4897 if (ret != 0) {
4898 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Set_Oject_Filename(): "
4899 "could not set object filename.");
4900 ptp_free_objectpropdesc(&opd);
4901 return -1;
4902 }
4903 } else {
4904 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Set_Oject_Filename(): "
4905 "Your device doesn't seem to support any known way of setting metadata.");
4906 ptp_free_objectpropdesc(&opd);
4907 return -1;
4908 }
4909
4910 ptp_free_objectpropdesc(&opd);
4911
4912 // update cached object properties if metadata cache exists
4913 update_metadata_cache(device, object_id);
4914
4915 return 0;
4916}
Linus Walleij399807b2008-01-27 22:05:12 +00004917
Linus Walleijde8193f2008-01-27 14:55:31 +00004918/**
Linus Walleij9c6ca022006-04-21 10:24:15 +00004919 * Helper function. This indicates if a track exists on the device
4920 * @param device a pointer to the device to get the track from.
4921 * @param id the track ID of the track to retrieve.
Linus Walleij070e9b42007-01-22 08:49:28 +00004922 * @return TRUE (!=0) if the track exists, FALSE (0) if not
Linus Walleij9c6ca022006-04-21 10:24:15 +00004923 */
4924int LIBMTP_Track_Exists(LIBMTP_mtpdevice_t *device,
4925 uint32_t const id)
4926{
Linus Walleij9c6ca022006-04-21 10:24:15 +00004927 PTPParams *params = (PTPParams *) device->params;
Linus Walleijf0bf4372007-07-01 21:47:38 +00004928 uint32_t i;
Linus Walleij9c6ca022006-04-21 10:24:15 +00004929
Linus Walleijf0bf4372007-07-01 21:47:38 +00004930 for (i = 0; i < params->handles.n; i++) {
4931 if (params->handles.Handler[i] == id) {
4932 return -1;
4933 }
Linus Walleij9c6ca022006-04-21 10:24:15 +00004934 }
4935 return 0;
4936}
4937
4938/**
4939 * This creates a new folder structure and allocates memory
4940 * for it. Notice that if you add strings to this structure they
4941 * will be freed by the corresponding <code>LIBMTP_folder_track_t</code>
4942 * operation later, so be careful of using strdup() when assigning
4943 * strings, e.g.:
4944 *
4945 * @return a pointer to the newly allocated folder structure.
4946 * @see LIBMTP_destroy_folder_t()
4947 */
4948LIBMTP_folder_t *LIBMTP_new_folder_t(void)
4949{
4950 LIBMTP_folder_t *new = (LIBMTP_folder_t *) malloc(sizeof(LIBMTP_folder_t));
4951 if (new == NULL) {
4952 return NULL;
4953 }
4954 new->folder_id = 0;
4955 new->parent_id = 0;
Linus Walleijea68f1f2008-06-22 21:54:44 +00004956 new->storage_id = 0;
Linus Walleij9c6ca022006-04-21 10:24:15 +00004957 new->name = NULL;
4958 new->sibling = NULL;
4959 new->child = NULL;
4960 return new;
4961}
4962
4963/**
Linus Walleij5573def2007-04-23 07:04:18 +00004964 * This recursively deletes the memory for a folder structure.
4965 * This shall typically be called on a top-level folder list to
4966 * detsroy the entire folder tree.
Linus Walleij9c6ca022006-04-21 10:24:15 +00004967 *
4968 * @param folder folder structure to destroy
4969 * @see LIBMTP_new_folder_t()
4970 */
4971void LIBMTP_destroy_folder_t(LIBMTP_folder_t *folder)
4972{
4973
4974 if(folder == NULL) {
4975 return;
4976 }
4977
4978 //Destroy from the bottom up
4979 if(folder->child != NULL) {
4980 LIBMTP_destroy_folder_t(folder->child);
4981 }
4982
4983 if(folder->sibling != NULL) {
4984 LIBMTP_destroy_folder_t(folder->sibling);
4985 }
4986
4987 if(folder->name != NULL) {
4988 free(folder->name);
4989 }
4990
4991 free(folder);
4992}
4993
4994/**
4995 * Helper function. Returns a folder structure for a
4996 * specified id.
4997 *
4998 * @param folderlist list of folders to search
4999 * @id id of folder to look for
5000 * @return a folder or NULL if not found
5001 */
5002LIBMTP_folder_t *LIBMTP_Find_Folder(LIBMTP_folder_t *folderlist, uint32_t id)
5003{
5004 LIBMTP_folder_t *ret = NULL;
mopoke96143402006-10-30 04:37:26 +00005005
Linus Walleij9c6ca022006-04-21 10:24:15 +00005006 if(folderlist == NULL) {
5007 return NULL;
5008 }
mopoke96143402006-10-30 04:37:26 +00005009
Linus Walleij9c6ca022006-04-21 10:24:15 +00005010 if(folderlist->folder_id == id) {
5011 return folderlist;
5012 }
mopoke96143402006-10-30 04:37:26 +00005013
Linus Walleij9c6ca022006-04-21 10:24:15 +00005014 if(folderlist->sibling) {
5015 ret = LIBMTP_Find_Folder(folderlist->sibling, id);
5016 }
mopoke96143402006-10-30 04:37:26 +00005017
Linus Walleij9c6ca022006-04-21 10:24:15 +00005018 if(folderlist->child && ret == NULL) {
5019 ret = LIBMTP_Find_Folder(folderlist->child, id);
5020 }
mopoke96143402006-10-30 04:37:26 +00005021
Linus Walleij9c6ca022006-04-21 10:24:15 +00005022 return ret;
5023}
5024
5025/**
Linus Walleij4d0deea2007-10-19 21:27:48 +00005026 * Function used to recursively get subfolders from params.
Linus Walleij9c6ca022006-04-21 10:24:15 +00005027 */
Linus Walleij4d0deea2007-10-19 21:27:48 +00005028static LIBMTP_folder_t *get_subfolders_for_folder(PTPParams *params, uint32_t parent)
Linus Walleij9c6ca022006-04-21 10:24:15 +00005029{
Linus Walleij4d0deea2007-10-19 21:27:48 +00005030 uint32_t i;
Linus Walleij9c6ca022006-04-21 10:24:15 +00005031 LIBMTP_folder_t *retfolders = NULL;
mopoke96143402006-10-30 04:37:26 +00005032
Linus Walleij9c6ca022006-04-21 10:24:15 +00005033 for (i = 0; i < params->handles.n; i++) {
5034 LIBMTP_folder_t *folder;
Linus Walleijf0bf4372007-07-01 21:47:38 +00005035 PTPObjectInfo *oi;
Linus Walleij4d0deea2007-10-19 21:27:48 +00005036
Linus Walleijf0bf4372007-07-01 21:47:38 +00005037 oi = &params->objectinfo[i];
Linus Walleij4d0deea2007-10-19 21:27:48 +00005038 if (oi->ObjectFormat != PTP_OFC_Association || oi->ParentObject != parent) {
Linus Walleijf0bf4372007-07-01 21:47:38 +00005039 continue;
5040 }
Linus Walleij4d0deea2007-10-19 21:27:48 +00005041
5042 // Create a folder struct...
Linus Walleijf0bf4372007-07-01 21:47:38 +00005043 folder = LIBMTP_new_folder_t();
Linus Walleij4d0deea2007-10-19 21:27:48 +00005044 if (folder == NULL) {
5045 // malloc failure or so.
5046 return NULL;
5047 }
Linus Walleijf0bf4372007-07-01 21:47:38 +00005048 folder->folder_id = params->handles.Handler[i];
5049 folder->parent_id = oi->ParentObject;
Linus Walleijea68f1f2008-06-22 21:54:44 +00005050 folder->storage_id = oi->StorageID;
Linus Walleij6aee9a62007-10-19 20:28:48 +00005051 if (oi->Filename != NULL) {
Linus Walleijf0bf4372007-07-01 21:47:38 +00005052 folder->name = (char *)strdup(oi->Filename);
Linus Walleij6aee9a62007-10-19 20:28:48 +00005053 } else {
Linus Walleijf0bf4372007-07-01 21:47:38 +00005054 folder->name = NULL;
Linus Walleij6aee9a62007-10-19 20:28:48 +00005055 }
Linus Walleijf0bf4372007-07-01 21:47:38 +00005056
Linus Walleij4d0deea2007-10-19 21:27:48 +00005057 // Add as first returned or a sibling to current
5058 if (retfolders == NULL) {
Linus Walleijf0bf4372007-07-01 21:47:38 +00005059 retfolders = folder;
Linus Walleijf0bf4372007-07-01 21:47:38 +00005060 } else {
Linus Walleij4d0deea2007-10-19 21:27:48 +00005061 LIBMTP_folder_t *tmp = retfolders;
5062 while (tmp->sibling != NULL) {
5063 tmp = tmp->sibling;
Linus Walleij9c6ca022006-04-21 10:24:15 +00005064 }
Linus Walleij4d0deea2007-10-19 21:27:48 +00005065 tmp->sibling = folder;
Linus Walleij9c6ca022006-04-21 10:24:15 +00005066 }
Linus Walleij4d0deea2007-10-19 21:27:48 +00005067
5068 // Recursively get children for this child. Perhaps NULL.
5069 folder->child = get_subfolders_for_folder(params, folder->folder_id);
Linus Walleij9c6ca022006-04-21 10:24:15 +00005070 }
Linus Walleij4d0deea2007-10-19 21:27:48 +00005071
Linus Walleij9c6ca022006-04-21 10:24:15 +00005072 return retfolders;
5073}
5074
5075/**
Linus Walleij4d0deea2007-10-19 21:27:48 +00005076 * This returns a list of all folders available
5077 * on the current MTP device.
5078 *
5079 * @param device a pointer to the device to get the track listing for.
5080 * @return a list of folders
5081 */
5082LIBMTP_folder_t *LIBMTP_Get_Folder_List(LIBMTP_mtpdevice_t *device)
5083{
5084 PTPParams *params = (PTPParams *) device->params;
Linus Walleij4d0deea2007-10-19 21:27:48 +00005085
5086 // Get all the handles if we haven't already done that
5087 if (params->handles.Handler == NULL) {
5088 flush_handles(device);
5089 }
5090
5091 // TODO: make a temporary list of folders only to
5092 // speed up searching? Else we get O(n^2) complexity
5093 // where n is the number of handles on the device,
5094 // in the following recursive call. Making a temp
5095 // list will reduce n to the number of folders.
5096
5097 // We begin at the root folder and get them all recursively
5098 return get_subfolders_for_folder(params, 0x00000000);
5099}
5100
5101/**
Linus Walleijc86afbd2006-05-04 19:05:24 +00005102 * This create a folder on the current MTP device. The PTP name
5103 * for a folder is "association". The PTP/MTP devices does not
5104 * have an internal "folder" concept really, it contains a flat
5105 * list of all files and some file are "associations" that other
5106 * files and folders may refer to as its "parent".
Linus Walleij9c6ca022006-04-21 10:24:15 +00005107 *
Linus Walleijc86afbd2006-05-04 19:05:24 +00005108 * @param device a pointer to the device to create the folder on.
5109 * @param name the name of the new folder.
5110 * @param parent_id id of parent folder to add the new folder to,
5111 * or 0 to put it in the root directory.
Linus Walleijea68f1f2008-06-22 21:54:44 +00005112 * @param storage_id id of the storage to add this new folder to.
5113 * notice that you cannot mismatch storage id and parent id:
5114 * they must both be on the same storage! Pass in 0 if you
5115 * want to create this folder on the default storage.
Linus Walleijc86afbd2006-05-04 19:05:24 +00005116 * @return id to new folder or 0 if an error occured
Linus Walleij9c6ca022006-04-21 10:24:15 +00005117 */
Linus Walleijea68f1f2008-06-22 21:54:44 +00005118uint32_t LIBMTP_Create_Folder(LIBMTP_mtpdevice_t *device, char *name,
5119 uint32_t parent_id, uint32_t storage_id)
Linus Walleij9c6ca022006-04-21 10:24:15 +00005120{
5121 PTPParams *params = (PTPParams *) device->params;
Linus Walleijd3b78572007-08-24 21:28:24 +00005122 PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo;
Linus Walleij9c6ca022006-04-21 10:24:15 +00005123 uint32_t parenthandle = 0;
Linus Walleijea68f1f2008-06-22 21:54:44 +00005124 uint32_t store;
Linus Walleij9c6ca022006-04-21 10:24:15 +00005125 PTPObjectInfo new_folder;
Linus Walleij438bd7f2006-06-08 11:35:44 +00005126 uint16_t ret;
Linus Walleij9c6ca022006-04-21 10:24:15 +00005127 uint32_t new_id = 0;
5128
Linus Walleijea68f1f2008-06-22 21:54:44 +00005129 if (storage_id == 0) {
5130 store = get_first_storageid(device);
5131 } else {
5132 store = storage_id;
5133 }
5134 parenthandle = parent_id;
5135
Linus Walleij9c6ca022006-04-21 10:24:15 +00005136 memset(&new_folder, 0, sizeof(new_folder));
5137 new_folder.Filename = name;
Linus Walleijfec4d562008-06-01 22:30:36 +00005138 if (FLAG_ONLY_7BIT_FILENAMES(ptp_usb)) {
Linus Walleijd3b78572007-08-24 21:28:24 +00005139 strip_7bit_from_utf8(new_folder.Filename);
5140 }
Linus Walleij9c6ca022006-04-21 10:24:15 +00005141 new_folder.ObjectCompressedSize = 1;
5142 new_folder.ObjectFormat = PTP_OFC_Association;
5143 new_folder.ParentObject = parent_id;
Linus Walleijea68f1f2008-06-22 21:54:44 +00005144 new_folder.StorageID = store;
Linus Walleij9c6ca022006-04-21 10:24:15 +00005145
Linus Walleij9c6ca022006-04-21 10:24:15 +00005146 // Create the object
Linus Walleijf0bf4372007-07-01 21:47:38 +00005147 // FIXME: use send list here if available.
Linus Walleij9c6ca022006-04-21 10:24:15 +00005148 ret = ptp_sendobjectinfo(params, &store, &parenthandle, &new_id, &new_folder);
5149 if (ret != PTP_RC_OK) {
Linus Walleij070e9b42007-01-22 08:49:28 +00005150 add_ptp_error_to_errorstack(device, ret, "LIBMTP_Create_Folder: Could not send object info.");
Linus Walleij99310d42006-11-01 08:29:39 +00005151 if (ret == PTP_RC_AccessDenied) {
Linus Walleij070e9b42007-01-22 08:49:28 +00005152 add_ptp_error_to_errorstack(device, ret, "ACCESS DENIED.");
Linus Walleij99310d42006-11-01 08:29:39 +00005153 }
Linus Walleijc86afbd2006-05-04 19:05:24 +00005154 return 0;
Linus Walleij9c6ca022006-04-21 10:24:15 +00005155 }
Linus Walleijf0bf4372007-07-01 21:47:38 +00005156 // NOTE: don't destroy the new_folder objectinfo, because it is statically referencing
5157 // several strings.
tsaarnia3eb60a2007-07-06 17:41:30 +00005158
Linus Walleija6d0d482007-10-31 08:54:56 +00005159 add_object_to_cache(device, new_id);
tsaarnia3eb60a2007-07-06 17:41:30 +00005160
Linus Walleij9c6ca022006-04-21 10:24:15 +00005161 return new_id;
5162}
raveloxd9a28642006-05-26 23:42:22 +00005163
Linus Walleij438bd7f2006-06-08 11:35:44 +00005164/**
5165 * This creates a new playlist metadata structure and allocates memory
5166 * for it. Notice that if you add strings to this structure they
5167 * will be freed by the corresponding <code>LIBMTP_destroy_playlist_t</code>
mopoke96143402006-10-30 04:37:26 +00005168 * operation later, so be careful of using strdup() when assigning
Linus Walleij438bd7f2006-06-08 11:35:44 +00005169 * strings, e.g.:
5170 *
5171 * <pre>
5172 * LIBMTP_playlist_t *pl = LIBMTP_new_playlist_t();
5173 * pl->name = strdup(str);
5174 * ....
5175 * LIBMTP_destroy_playlist_t(pl);
5176 * </pre>
5177 *
5178 * @return a pointer to the newly allocated metadata structure.
5179 * @see LIBMTP_destroy_playlist_t()
5180 */
5181LIBMTP_playlist_t *LIBMTP_new_playlist_t(void)
5182{
5183 LIBMTP_playlist_t *new = (LIBMTP_playlist_t *) malloc(sizeof(LIBMTP_playlist_t));
5184 if (new == NULL) {
5185 return NULL;
5186 }
5187 new->playlist_id = 0;
Linus Walleij5ce59db2008-03-12 21:22:58 +00005188 new->parent_id = 0;
Linus Walleijea68f1f2008-06-22 21:54:44 +00005189 new->storage_id = 0;
Linus Walleij438bd7f2006-06-08 11:35:44 +00005190 new->name = NULL;
5191 new->tracks = NULL;
5192 new->no_tracks = 0;
5193 new->next = NULL;
5194 return new;
5195}
5196
5197/**
5198 * This destroys a playlist metadata structure and deallocates the memory
mopoke96143402006-10-30 04:37:26 +00005199 * used by it, including any strings. Never use a track metadata
Linus Walleij438bd7f2006-06-08 11:35:44 +00005200 * structure again after calling this function on it.
5201 * @param playlist the playlist metadata to destroy.
5202 * @see LIBMTP_new_playlist_t()
5203 */
5204void LIBMTP_destroy_playlist_t(LIBMTP_playlist_t *playlist)
5205{
5206 if (playlist == NULL) {
5207 return;
5208 }
5209 if (playlist->name != NULL)
5210 free(playlist->name);
5211 if (playlist->tracks != NULL)
5212 free(playlist->tracks);
5213 free(playlist);
5214 return;
5215}
5216
5217/**
5218 * This function returns a list of the playlists available on the
5219 * device. Typical usage:
5220 *
5221 * <pre>
5222 * </pre>
5223 *
5224 * @param device a pointer to the device to get the playlist listing from.
5225 * @return a playlist list on success, else NULL. If there are no playlists
5226 * on the device, NULL will be returned as well.
Linus Walleij2e4b5f92006-06-16 14:00:49 +00005227 * @see LIBMTP_Get_Playlist()
Linus Walleij438bd7f2006-06-08 11:35:44 +00005228 */
5229LIBMTP_playlist_t *LIBMTP_Get_Playlist_List(LIBMTP_mtpdevice_t *device)
5230{
Linus Walleijf3c44052008-08-16 21:14:56 +00005231 PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo;
5232 const int REQ_SPL = FLAG_PLAYLIST_SPL(ptp_usb);
Linus Walleij438bd7f2006-06-08 11:35:44 +00005233 PTPParams *params = (PTPParams *) device->params;
5234 LIBMTP_playlist_t *retlists = NULL;
5235 LIBMTP_playlist_t *curlist = NULL;
5236 uint32_t i;
5237
5238 // Get all the handles if we haven't already done that
5239 if (params->handles.Handler == NULL) {
5240 flush_handles(device);
5241 }
5242
5243 for (i = 0; i < params->handles.n; i++) {
5244 LIBMTP_playlist_t *pl;
Linus Walleijf0bf4372007-07-01 21:47:38 +00005245 PTPObjectInfo *oi;
Linus Walleij438bd7f2006-06-08 11:35:44 +00005246 uint16_t ret;
mopoke96143402006-10-30 04:37:26 +00005247
Linus Walleijf0bf4372007-07-01 21:47:38 +00005248 oi = &params->objectinfo[i];
mopoke96143402006-10-30 04:37:26 +00005249
Linus Walleijf0bf4372007-07-01 21:47:38 +00005250 // Ignore stuff that isn't playlists
Linus Walleijf3c44052008-08-16 21:14:56 +00005251
5252 // For Samsung players we must look for the .spl extension explicitly since
5253 // playlists are not stored as playlist objects.
5254 if ( REQ_SPL && is_spl_playlist(oi) ) {
5255 // Allocate a new playlist type
5256 pl = LIBMTP_new_playlist_t();
5257 spl_to_playlist_t(device, oi, params->handles.Handler[i], pl);
5258 }
5259 else if ( oi->ObjectFormat != PTP_OFC_MTP_AbstractAudioVideoPlaylist ) {
Linus Walleijf0bf4372007-07-01 21:47:38 +00005260 continue;
Linus Walleij438bd7f2006-06-08 11:35:44 +00005261 }
Linus Walleijf3c44052008-08-16 21:14:56 +00005262 else {
5263 // Allocate a new playlist type
5264 pl = LIBMTP_new_playlist_t();
Linus Walleijf0bf4372007-07-01 21:47:38 +00005265
Linus Walleijf3296622008-09-04 20:53:56 +00005266 // Try to look up proper name, else use the oi->Filename field.
Linus Walleijf3c44052008-08-16 21:14:56 +00005267 pl->name = get_string_from_object(device, params->handles.Handler[i], PTP_OPC_Name);
Linus Walleijf3296622008-09-04 20:53:56 +00005268 if (pl->name == NULL) {
5269 pl->name = strdup(oi->Filename);
5270 }
Linus Walleijf3c44052008-08-16 21:14:56 +00005271 pl->playlist_id = params->handles.Handler[i];
5272 pl->parent_id = oi->ParentObject;
5273 pl->storage_id = oi->StorageID;
Linus Walleijf0bf4372007-07-01 21:47:38 +00005274
Linus Walleijf3c44052008-08-16 21:14:56 +00005275 // Then get the track listing for this playlist
5276 ret = ptp_mtp_getobjectreferences(params, pl->playlist_id, &pl->tracks, &pl->no_tracks);
5277 if (ret != PTP_RC_OK) {
5278 add_ptp_error_to_errorstack(device, ret, "LIBMTP_Get_Playlist: Could not get object references.");
5279 pl->tracks = NULL;
5280 pl->no_tracks = 0;
5281 }
Linus Walleijf0bf4372007-07-01 21:47:38 +00005282 }
5283
5284 // Add playlist to a list that will be returned afterwards.
5285 if (retlists == NULL) {
5286 retlists = pl;
5287 curlist = pl;
5288 } else {
5289 curlist->next = pl;
5290 curlist = pl;
5291 }
5292
5293 // Call callback here if we decide to add that possibility...
mopoke96143402006-10-30 04:37:26 +00005294 }
Linus Walleij438bd7f2006-06-08 11:35:44 +00005295 return retlists;
5296}
5297
Linus Walleij2e4b5f92006-06-16 14:00:49 +00005298
5299/**
5300 * This function retrieves an individual playlist from the device.
5301 * @param device a pointer to the device to get the playlist from.
5302 * @param plid the unique ID of the playlist to retrieve.
5303 * @return a valid playlist metadata post or NULL on failure.
5304 * @see LIBMTP_Get_Playlist_List()
5305 */
5306LIBMTP_playlist_t *LIBMTP_Get_Playlist(LIBMTP_mtpdevice_t *device, uint32_t const plid)
5307{
Linus Walleijf3c44052008-08-16 21:14:56 +00005308 PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo;
5309 const int REQ_SPL = FLAG_PLAYLIST_SPL(ptp_usb);
Linus Walleij2e4b5f92006-06-16 14:00:49 +00005310 PTPParams *params = (PTPParams *) device->params;
5311 uint32_t i;
5312
5313 // Get all the handles if we haven't already done that
5314 if (params->handles.Handler == NULL) {
5315 flush_handles(device);
5316 }
5317
5318 for (i = 0; i < params->handles.n; i++) {
5319 LIBMTP_playlist_t *pl;
Linus Walleijf0bf4372007-07-01 21:47:38 +00005320 PTPObjectInfo *oi;
Linus Walleij2e4b5f92006-06-16 14:00:49 +00005321 uint16_t ret;
Linus Walleijf0bf4372007-07-01 21:47:38 +00005322
Linus Walleij2e4b5f92006-06-16 14:00:49 +00005323 if (params->handles.Handler[i] != plid) {
5324 continue;
5325 }
Linus Walleijf0bf4372007-07-01 21:47:38 +00005326
5327 oi = &params->objectinfo[i];
Linus Walleij2e4b5f92006-06-16 14:00:49 +00005328
Linus Walleijf3c44052008-08-16 21:14:56 +00005329 // For Samsung players we must look for the .spl extension explicitly since
5330 // playlists are not stored as playlist objects.
5331 if ( REQ_SPL && is_spl_playlist(oi) ) {
5332 // Allocate a new playlist type
5333 pl = LIBMTP_new_playlist_t();
5334 spl_to_playlist_t(device, oi, params->handles.Handler[i], pl);
5335 return pl;
5336 }
5337
Linus Walleijf0bf4372007-07-01 21:47:38 +00005338 // Ignore stuff that isn't playlists
Linus Walleijf3c44052008-08-16 21:14:56 +00005339 else if ( oi->ObjectFormat != PTP_OFC_MTP_AbstractAudioVideoPlaylist ) {
Linus Walleij2e4b5f92006-06-16 14:00:49 +00005340 return NULL;
5341 }
Linus Walleijf0bf4372007-07-01 21:47:38 +00005342
5343 // Allocate a new playlist type
5344 pl = LIBMTP_new_playlist_t();
5345
Linus Walleijf0bf4372007-07-01 21:47:38 +00005346 pl->name = get_string_from_object(device, params->handles.Handler[i], PTP_OPC_Name);
Linus Walleijf3296622008-09-04 20:53:56 +00005347 if (pl->name == NULL) {
5348 pl->name = strdup(oi->Filename);
5349 }
Linus Walleijf0bf4372007-07-01 21:47:38 +00005350 pl->playlist_id = params->handles.Handler[i];
Linus Walleij5ce59db2008-03-12 21:22:58 +00005351 pl->parent_id = oi->ParentObject;
Linus Walleijea68f1f2008-06-22 21:54:44 +00005352 pl->storage_id = oi->StorageID;
Linus Walleijf0bf4372007-07-01 21:47:38 +00005353
5354 // Then get the track listing for this playlist
5355 ret = ptp_mtp_getobjectreferences(params, pl->playlist_id, &pl->tracks, &pl->no_tracks);
5356 if (ret != PTP_RC_OK) {
5357 add_ptp_error_to_errorstack(device, ret, "LIBMTP_Get_Playlist(): Could not get object references.");
5358 pl->tracks = NULL;
5359 pl->no_tracks = 0;
5360 }
5361
5362 return pl;
mopoke96143402006-10-30 04:37:26 +00005363 }
Linus Walleij2e4b5f92006-06-16 14:00:49 +00005364 return NULL;
5365}
5366
Linus Walleij31b74292008-05-02 23:29:06 +00005367/**
Linus Walleijb7426d12006-11-25 23:19:11 +00005368 * This function creates a new abstract list such as a playlist
5369 * or an album.
5370 *
5371 * @param device a pointer to the device to create the new abstract list
5372 * on.
5373 * @param name the name of the new abstract list.
Linus Walleij7783b9b2007-09-22 22:10:44 +00005374 * @param artist the artist of the new abstract list or NULL.
5375 * @param genre the genre of the new abstract list or NULL.
Linus Walleijb7426d12006-11-25 23:19:11 +00005376 * @param parenthandle the handle of the parent or 0 for no parent
5377 * i.e. the root folder.
5378 * @param objectformat the abstract list type to create.
5379 * @param suffix the ".foo" (4 characters) suffix to use for the virtual
5380 * "file" created by this operation.
5381 * @param newid a pointer to a variable that will hold the new object
5382 * ID if this call is successful.
5383 * @param tracks an array of tracks to associate with this list.
5384 * @param no_tracks the number of tracks in the list.
Linus Walleij438bd7f2006-06-08 11:35:44 +00005385 * @return 0 on success, any other value means failure.
Linus Walleij438bd7f2006-06-08 11:35:44 +00005386 */
Linus Walleijb7426d12006-11-25 23:19:11 +00005387static int create_new_abstract_list(LIBMTP_mtpdevice_t *device,
5388 char const * const name,
Linus Walleijadce4a52007-04-23 07:28:11 +00005389 char const * const artist,
Linus Walleij31b74292008-05-02 23:29:06 +00005390 char const * const composer,
Linus Walleijadce4a52007-04-23 07:28:11 +00005391 char const * const genre,
Linus Walleijb7426d12006-11-25 23:19:11 +00005392 uint32_t const parenthandle,
Linus Walleijea68f1f2008-06-22 21:54:44 +00005393 uint32_t const storageid,
Linus Walleijb7426d12006-11-25 23:19:11 +00005394 uint16_t const objectformat,
5395 char const * const suffix,
5396 uint32_t * const newid,
5397 uint32_t const * const tracks,
5398 uint32_t const no_tracks)
5399
Linus Walleij438bd7f2006-06-08 11:35:44 +00005400{
Linus Walleijb7426d12006-11-25 23:19:11 +00005401 int i;
5402 int supported = 0;
Linus Walleij438bd7f2006-06-08 11:35:44 +00005403 uint16_t ret;
Linus Walleij1e9a0332007-09-12 19:35:56 +00005404 uint16_t *properties = NULL;
rreardon508705f2006-11-19 21:27:22 +00005405 uint32_t propcnt = 0;
Linus Walleijea68f1f2008-06-22 21:54:44 +00005406 uint32_t store;
Linus Walleij438bd7f2006-06-08 11:35:44 +00005407 uint32_t localph = parenthandle;
Linus Walleijb7426d12006-11-25 23:19:11 +00005408 uint8_t nonconsumable = 0x00U; /* By default it is consumable */
5409 PTPParams *params = (PTPParams *) device->params;
Linus Walleijd3b78572007-08-24 21:28:24 +00005410 PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo;
Linus Walleij438bd7f2006-06-08 11:35:44 +00005411 char fname[256];
Linus Walleijd14e84f2006-06-16 14:50:59 +00005412 uint8_t data[2];
Linus Walleij438bd7f2006-06-08 11:35:44 +00005413
Linus Walleijea68f1f2008-06-22 21:54:44 +00005414 if (storageid == 0) {
5415 store = get_first_storageid(device);
5416 } else {
5417 store = storageid;
5418 }
5419
Linus Walleijb7426d12006-11-25 23:19:11 +00005420 // Check if we can create an object of this type
5421 for ( i=0; i < params->deviceinfo.ImageFormats_len; i++ ) {
5422 if (params->deviceinfo.ImageFormats[i] == objectformat) {
5423 supported = 1;
5424 break;
Linus Walleij438bd7f2006-06-08 11:35:44 +00005425 }
5426 }
Linus Walleijb7426d12006-11-25 23:19:11 +00005427 if (!supported) {
Linus Walleij070e9b42007-01-22 08:49:28 +00005428 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 +00005429 printf("Unsupported abstract list type: %04x\n", objectformat);
Linus Walleijb7426d12006-11-25 23:19:11 +00005430 return -1;
5431 }
5432
Richard Low15731fe2007-03-22 20:27:20 +00005433 // add the new suffix if it isn't there
Linus Walleij629fe402006-12-12 23:39:15 +00005434 fname[0] = '\0';
Linus Walleijb7426d12006-11-25 23:19:11 +00005435 if (strlen(name) > strlen(suffix)) {
5436 char const * const suff = &name[strlen(name)-strlen(suffix)];
5437 if (!strcmp(suff, suffix)) {
5438 // Home free.
Linus Walleij629fe402006-12-12 23:39:15 +00005439 strncpy(fname, name, sizeof(fname));
Linus Walleijb7426d12006-11-25 23:19:11 +00005440 }
5441 }
5442 // If it didn't end with "<suffix>" then add that here.
Linus Walleij629fe402006-12-12 23:39:15 +00005443 if (fname[0] == '\0') {
Linus Walleijb7426d12006-11-25 23:19:11 +00005444 strncpy(fname, name, sizeof(fname)-strlen(suffix)-1);
5445 strcat(fname, suffix);
Linus Walleij438bd7f2006-06-08 11:35:44 +00005446 fname[sizeof(fname)-1] = '\0';
Linus Walleij438bd7f2006-06-08 11:35:44 +00005447 }
5448
rreardon508705f2006-11-19 21:27:22 +00005449 if (ptp_operation_issupported(params,PTP_OC_MTP_SendObjectPropList)) {
mopoke31364442006-11-20 04:53:04 +00005450
Linus Walleij1e9a0332007-09-12 19:35:56 +00005451 MTPProperties *props = NULL;
5452 MTPProperties *prop = NULL;
5453 int nrofprops = 0;
Richard Low6c0a6ce2006-11-26 10:42:08 +00005454
5455 *newid = 0x00000000U;
mopoke96143402006-10-30 04:37:26 +00005456
Linus Walleij1e9a0332007-09-12 19:35:56 +00005457 ret = ptp_mtp_getobjectpropssupported(params, objectformat, &propcnt, &properties);
mopoke31364442006-11-20 04:53:04 +00005458
rreardon508705f2006-11-19 21:27:22 +00005459 for (i=0;i<propcnt;i++) {
Linus Walleijdbcc8242007-08-05 22:31:26 +00005460 PTPObjectPropDesc opd;
5461
Linus Walleij1e9a0332007-09-12 19:35:56 +00005462 ret = ptp_mtp_getobjectpropdesc(params, properties[i], objectformat, &opd);
Linus Walleijdbcc8242007-08-05 22:31:26 +00005463 if (ret != PTP_RC_OK) {
5464 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "create_new_abstract_list(): "
5465 "could not get property description.");
5466 } else if (opd.GetSet) {
Linus Walleij1e9a0332007-09-12 19:35:56 +00005467 switch (properties[i]) {
Linus Walleijdbcc8242007-08-05 22:31:26 +00005468 case PTP_OPC_ObjectFileName:
Linus Walleija6d0d482007-10-31 08:54:56 +00005469 prop = ptp_get_new_object_prop_entry(&props,&nrofprops);
Linus Walleijdbcc8242007-08-05 22:31:26 +00005470 prop->ObjectHandle = *newid;
5471 prop->property = PTP_OPC_ObjectFileName;
5472 prop->datatype = PTP_DTC_STR;
5473 prop->propval.str = strdup(fname);
Linus Walleijfec4d562008-06-01 22:30:36 +00005474 if (FLAG_ONLY_7BIT_FILENAMES(ptp_usb)) {
Linus Walleijd3b78572007-08-24 21:28:24 +00005475 strip_7bit_from_utf8(prop->propval.str);
5476 }
Linus Walleijdbcc8242007-08-05 22:31:26 +00005477 break;
5478 case PTP_OPC_ProtectionStatus:
Linus Walleija6d0d482007-10-31 08:54:56 +00005479 prop = ptp_get_new_object_prop_entry(&props,&nrofprops);
Linus Walleijadce4a52007-04-23 07:28:11 +00005480 prop->ObjectHandle = *newid;
Linus Walleijdbcc8242007-08-05 22:31:26 +00005481 prop->property = PTP_OPC_ProtectionStatus;
5482 prop->datatype = PTP_DTC_UINT16;
5483 prop->propval.u16 = 0x0000U; /* Not protected */
Linus Walleijdbcc8242007-08-05 22:31:26 +00005484 break;
5485 case PTP_OPC_NonConsumable:
Linus Walleija6d0d482007-10-31 08:54:56 +00005486 prop = ptp_get_new_object_prop_entry(&props,&nrofprops);
Linus Walleijadce4a52007-04-23 07:28:11 +00005487 prop->ObjectHandle = *newid;
Linus Walleijdbcc8242007-08-05 22:31:26 +00005488 prop->property = PTP_OPC_NonConsumable;
5489 prop->datatype = PTP_DTC_UINT8;
5490 prop->propval.u8 = nonconsumable;
Linus Walleijdbcc8242007-08-05 22:31:26 +00005491 break;
5492 case PTP_OPC_Name:
5493 if (name != NULL) {
Linus Walleija6d0d482007-10-31 08:54:56 +00005494 prop = ptp_get_new_object_prop_entry(&props,&nrofprops);
Linus Walleijdbcc8242007-08-05 22:31:26 +00005495 prop->ObjectHandle = *newid;
5496 prop->property = PTP_OPC_Name;
5497 prop->datatype = PTP_DTC_STR;
5498 prop->propval.str = strdup(name);
Linus Walleijdbcc8242007-08-05 22:31:26 +00005499 }
5500 break;
Linus Walleij0b75ab62007-12-28 00:47:34 +00005501 case PTP_OPC_AlbumArtist:
5502 if (artist != NULL) {
5503 prop = ptp_get_new_object_prop_entry(&props,&nrofprops);
5504 prop->ObjectHandle = *newid;
5505 prop->property = PTP_OPC_AlbumArtist;
5506 prop->datatype = PTP_DTC_STR;
5507 prop->propval.str = strdup(artist);
5508 }
5509 break;
Linus Walleijdbcc8242007-08-05 22:31:26 +00005510 case PTP_OPC_Artist:
5511 if (artist != NULL) {
Linus Walleija6d0d482007-10-31 08:54:56 +00005512 prop = ptp_get_new_object_prop_entry(&props,&nrofprops);
Linus Walleijdbcc8242007-08-05 22:31:26 +00005513 prop->ObjectHandle = *newid;
5514 prop->property = PTP_OPC_Artist;
5515 prop->datatype = PTP_DTC_STR;
5516 prop->propval.str = strdup(artist);
Linus Walleijdbcc8242007-08-05 22:31:26 +00005517 }
5518 break;
Linus Walleij31b74292008-05-02 23:29:06 +00005519 case PTP_OPC_Composer:
5520 if (composer != NULL) {
5521 prop = ptp_get_new_object_prop_entry(&props,&nrofprops);
5522 prop->ObjectHandle = *newid;
5523 prop->property = PTP_OPC_Composer;
5524 prop->datatype = PTP_DTC_STR;
5525 prop->propval.str = strdup(composer);
5526 }
5527 break;
Linus Walleijdbcc8242007-08-05 22:31:26 +00005528 case PTP_OPC_Genre:
5529 if (genre != NULL) {
Linus Walleija6d0d482007-10-31 08:54:56 +00005530 prop = ptp_get_new_object_prop_entry(&props,&nrofprops);
Linus Walleijdbcc8242007-08-05 22:31:26 +00005531 prop->ObjectHandle = *newid;
5532 prop->property = PTP_OPC_Genre;
5533 prop->datatype = PTP_DTC_STR;
5534 prop->propval.str = strdup(genre);
Linus Walleijdbcc8242007-08-05 22:31:26 +00005535 }
5536 break;
Linus Walleij7783b9b2007-09-22 22:10:44 +00005537 case PTP_OPC_DateModified:
5538 // Tag with current time if that is supported
Linus Walleija6d0d482007-10-31 08:54:56 +00005539 prop = ptp_get_new_object_prop_entry(&props,&nrofprops);
Linus Walleij7783b9b2007-09-22 22:10:44 +00005540 prop->ObjectHandle = *newid;
5541 prop->property = PTP_OPC_DateModified;
5542 prop->datatype = PTP_DTC_STR;
5543 prop->propval.str = get_iso8601_stamp();
5544 break;
Linus Walleijadce4a52007-04-23 07:28:11 +00005545 }
rreardon508705f2006-11-19 21:27:22 +00005546 }
Linus Walleijdbcc8242007-08-05 22:31:26 +00005547 ptp_free_objectpropdesc(&opd);
rreardon508705f2006-11-19 21:27:22 +00005548 }
Linus Walleij1e9a0332007-09-12 19:35:56 +00005549 free(properties);
mopoke31364442006-11-20 04:53:04 +00005550
Linus Walleijb7426d12006-11-25 23:19:11 +00005551 ret = ptp_mtp_sendobjectproplist(params, &store, &localph, newid,
Linus Walleij1e9a0332007-09-12 19:35:56 +00005552 objectformat, 0, props, nrofprops);
mopoke31364442006-11-20 04:53:04 +00005553
rreardon508705f2006-11-19 21:27:22 +00005554 /* Free property list */
Linus Walleija6d0d482007-10-31 08:54:56 +00005555 ptp_destroy_object_prop_list(props, nrofprops);
mopoke31364442006-11-20 04:53:04 +00005556
rreardon508705f2006-11-19 21:27:22 +00005557 if (ret != PTP_RC_OK) {
Linus Walleij070e9b42007-01-22 08:49:28 +00005558 add_ptp_error_to_errorstack(device, ret, "create_new_abstract_list(): Could not send object property list.");
rreardon508705f2006-11-19 21:27:22 +00005559 if (ret == PTP_RC_AccessDenied) {
Linus Walleij070e9b42007-01-22 08:49:28 +00005560 add_ptp_error_to_errorstack(device, ret, "ACCESS DENIED.");
rreardon508705f2006-11-19 21:27:22 +00005561 }
5562 return -1;
5563 }
Richard Lowa6c924c2006-12-29 17:44:28 +00005564
Richard Lowc3f5fc32007-07-29 09:40:50 +00005565 // now send the blank object
Richard Lowa6c924c2006-12-29 17:44:28 +00005566 ret = ptp_sendobject(params, NULL, 0);
5567 if (ret != PTP_RC_OK) {
Linus Walleij070e9b42007-01-22 08:49:28 +00005568 add_ptp_error_to_errorstack(device, ret, "create_new_abstract_list(): Could not send blank object data.");
Richard Lowa6c924c2006-12-29 17:44:28 +00005569 return -1;
5570 }
Linus Walleij629fe402006-12-12 23:39:15 +00005571
rreardon508705f2006-11-19 21:27:22 +00005572 } else if (ptp_operation_issupported(params,PTP_OC_SendObjectInfo)) {
Linus Walleij629fe402006-12-12 23:39:15 +00005573 PTPObjectInfo new_object;
5574
5575 new_object.Filename = fname;
Linus Walleijfec4d562008-06-01 22:30:36 +00005576 if (FLAG_ONLY_7BIT_FILENAMES(ptp_usb)) {
Linus Walleijd3b78572007-08-24 21:28:24 +00005577 strip_7bit_from_utf8(new_object.Filename);
5578 }
Linus Walleij629fe402006-12-12 23:39:15 +00005579 new_object.ObjectCompressedSize = 1;
5580 new_object.ObjectFormat = objectformat;
5581
Linus Walleij20698482006-11-24 20:54:21 +00005582 // Create the object
Linus Walleijb7426d12006-11-25 23:19:11 +00005583 ret = ptp_sendobjectinfo(params, &store, &localph, newid, &new_object);
Linus Walleij20698482006-11-24 20:54:21 +00005584 if (ret != PTP_RC_OK) {
Linus Walleij070e9b42007-01-22 08:49:28 +00005585 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 +00005586 if (ret == PTP_RC_AccessDenied) {
Linus Walleij070e9b42007-01-22 08:49:28 +00005587 add_ptp_error_to_errorstack(device, ret, "ACCESS DENIED.");
Linus Walleij20698482006-11-24 20:54:21 +00005588 }
5589 return -1;
5590 }
Linus Walleijf0bf4372007-07-01 21:47:38 +00005591 // NOTE: don't destroy new_object objectinfo afterwards - the strings it contains are
5592 // not copies.
Richard Lowa6c924c2006-12-29 17:44:28 +00005593
5594 /*
5595 * We have to send this one blank data byte.
5596 * If we don't, the handle will not be created and thus there is no playlist.
5597 */
5598 data[0] = '\0';
5599 data[1] = '\0';
5600 ret = ptp_sendobject(params, data, 1);
5601 if (ret != PTP_RC_OK) {
Linus Walleij070e9b42007-01-22 08:49:28 +00005602 add_ptp_error_to_errorstack(device, ret, "create_new_abstract_list(): Could not send blank object data.");
Richard Lowa6c924c2006-12-29 17:44:28 +00005603 return -1;
5604 }
5605
5606 // Update title
Linus Walleij7783b9b2007-09-22 22:10:44 +00005607 // FIXME: check if supported
Linus Walleijadce4a52007-04-23 07:28:11 +00005608 if (name != NULL) {
5609 ret = set_object_string(device, *newid, PTP_OPC_Name, name);
5610 if (ret != 0) {
5611 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "create_new_abstract_list(): could not set entity name.");
5612 return -1;
5613 }
Richard Lowa6c924c2006-12-29 17:44:28 +00005614 }
Linus Walleijadce4a52007-04-23 07:28:11 +00005615
5616 // Update artist
Linus Walleij7783b9b2007-09-22 22:10:44 +00005617 // FIXME: check if supported
Linus Walleijadce4a52007-04-23 07:28:11 +00005618 if (artist != NULL) {
Linus Walleij0b75ab62007-12-28 00:47:34 +00005619 ret = set_object_string(device, *newid, PTP_OPC_AlbumArtist, artist);
5620 if (ret != 0) {
5621 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "create_new_abstract_list(): could not set entity album artist.");
5622 return -1;
5623 }
5624 }
5625 // Update artist
5626 // FIXME: check if supported
5627 if (artist != NULL) {
Linus Walleijadce4a52007-04-23 07:28:11 +00005628 ret = set_object_string(device, *newid, PTP_OPC_Artist, artist);
5629 if (ret != 0) {
5630 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "create_new_abstract_list(): could not set entity artist.");
5631 return -1;
5632 }
5633 }
5634
Linus Walleij31b74292008-05-02 23:29:06 +00005635 // Update composer
5636 // FIXME: check if supported
5637 if (composer != NULL) {
5638 ret = set_object_string(device, *newid, PTP_OPC_Composer, composer);
5639 if (ret != 0) {
5640 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "create_new_abstract_list(): could not set entity composer.");
5641 return -1;
5642 }
5643 }
5644
Linus Walleijadce4a52007-04-23 07:28:11 +00005645 // Update genre
Linus Walleij7783b9b2007-09-22 22:10:44 +00005646 // FIXME: check if supported
Linus Walleijadce4a52007-04-23 07:28:11 +00005647 if (genre != NULL) {
5648 ret = set_object_string(device, *newid, PTP_OPC_Genre, genre);
5649 if (ret != 0) {
5650 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "create_new_abstract_list(): could not set entity genre.");
5651 return -1;
5652 }
5653 }
Linus Walleijdbcc8242007-08-05 22:31:26 +00005654
Linus Walleij7783b9b2007-09-22 22:10:44 +00005655 // FIXME: Update date modified
5656
Linus Walleij438bd7f2006-06-08 11:35:44 +00005657 }
5658
Linus Walleijb7426d12006-11-25 23:19:11 +00005659 if (no_tracks > 0) {
Linus Walleij438bd7f2006-06-08 11:35:44 +00005660 // Add tracks to the new playlist as object references.
Linus Walleijb7426d12006-11-25 23:19:11 +00005661 ret = ptp_mtp_setobjectreferences (params, *newid, (uint32_t *) tracks, no_tracks);
Linus Walleij438bd7f2006-06-08 11:35:44 +00005662 if (ret != PTP_RC_OK) {
Linus Walleij070e9b42007-01-22 08:49:28 +00005663 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 +00005664 return -1;
5665 }
5666 }
mopoke96143402006-10-30 04:37:26 +00005667
Linus Walleija6d0d482007-10-31 08:54:56 +00005668 add_object_to_cache(device, *newid);
Linus Walleij438bd7f2006-06-08 11:35:44 +00005669
5670 return 0;
5671}
5672
Linus Walleij7783b9b2007-09-22 22:10:44 +00005673/**
5674 * This updates the metadata and track listing
5675 * for an abstract list.
5676 * @param device a pointer to the device that the abstract list
5677 * resides on.
5678 * @param name the name of the abstract list.
5679 * @param artist the artist of the abstract list or NULL.
5680 * @param genre the genre of the abstract list or NULL.
5681 * @param objecthandle the object to be updated.
5682 * @param objectformat the abstract list type to update.
5683 * @param tracks an array of tracks to associate with this list.
5684 * @param no_tracks the number of tracks in the list.
5685 * @return 0 on success, any other value means failure.
5686 */
5687static int update_abstract_list(LIBMTP_mtpdevice_t *device,
5688 char const * const name,
5689 char const * const artist,
Linus Walleij31b74292008-05-02 23:29:06 +00005690 char const * const composer,
Linus Walleij7783b9b2007-09-22 22:10:44 +00005691 char const * const genre,
5692 uint32_t const objecthandle,
5693 uint16_t const objectformat,
5694 uint32_t const * const tracks,
5695 uint32_t const no_tracks)
5696{
5697 uint16_t ret;
5698 PTPParams *params = (PTPParams *) device->params;
Linus Walleijf9267e92007-10-15 21:07:37 +00005699 PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo;
Linus Walleij7783b9b2007-09-22 22:10:44 +00005700 uint16_t *properties = NULL;
5701 uint32_t propcnt = 0;
5702 int i;
5703
5704 // First see which properties can be set
5705 // i.e only try to update this metadata for object tags that exist on the current player.
5706 ret = ptp_mtp_getobjectpropssupported(params, objectformat, &propcnt, &properties);
5707 if (ret != PTP_RC_OK) {
5708 // Just bail out for now, nothing is ever set.
5709 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "update_abstract_list(): "
5710 "could not retrieve supported object properties.");
5711 return -1;
5712 }
Linus Walleijf9267e92007-10-15 21:07:37 +00005713 if (ptp_operation_issupported(params,PTP_OC_MTP_SetObjPropList) &&
Linus Walleijfec4d562008-06-01 22:30:36 +00005714 !FLAG_BROKEN_SET_OBJECT_PROPLIST(ptp_usb)) {
Linus Walleij7783b9b2007-09-22 22:10:44 +00005715 MTPProperties *props = NULL;
5716 MTPProperties *prop = NULL;
5717 int nrofprops = 0;
5718
5719 for (i=0;i<propcnt;i++) {
5720 PTPObjectPropDesc opd;
5721
5722 ret = ptp_mtp_getobjectpropdesc(params, properties[i], objectformat, &opd);
5723 if (ret != PTP_RC_OK) {
5724 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "update_abstract_list(): "
5725 "could not get property description.");
5726 } else if (opd.GetSet) {
5727 switch (properties[i]) {
5728 case PTP_OPC_Name:
Linus Walleija6d0d482007-10-31 08:54:56 +00005729 prop = ptp_get_new_object_prop_entry(&props, &nrofprops);
Linus Walleij7783b9b2007-09-22 22:10:44 +00005730 prop->ObjectHandle = objecthandle;
5731 prop->property = PTP_OPC_Name;
5732 prop->datatype = PTP_DTC_STR;
5733 if (name != NULL)
5734 prop->propval.str = strdup(name);
5735 break;
Linus Walleij0b75ab62007-12-28 00:47:34 +00005736 case PTP_OPC_AlbumArtist:
5737 if (artist != NULL) {
5738 prop = ptp_get_new_object_prop_entry(&props, &nrofprops);
5739 prop->ObjectHandle = objecthandle;
5740 prop->property = PTP_OPC_AlbumArtist;
5741 prop->datatype = PTP_DTC_STR;
5742 prop->propval.str = strdup(artist);
5743 }
5744 break;
Linus Walleij7783b9b2007-09-22 22:10:44 +00005745 case PTP_OPC_Artist:
5746 if (artist != NULL) {
Linus Walleija6d0d482007-10-31 08:54:56 +00005747 prop = ptp_get_new_object_prop_entry(&props, &nrofprops);
Linus Walleij0b75ab62007-12-28 00:47:34 +00005748 prop->ObjectHandle = objecthandle;
Linus Walleij7783b9b2007-09-22 22:10:44 +00005749 prop->property = PTP_OPC_Artist;
5750 prop->datatype = PTP_DTC_STR;
5751 prop->propval.str = strdup(artist);
5752 }
5753 break;
Linus Walleij31b74292008-05-02 23:29:06 +00005754 case PTP_OPC_Composer:
5755 if (composer != NULL) {
5756 prop = ptp_get_new_object_prop_entry(&props, &nrofprops);
5757 prop->ObjectHandle = objecthandle;
5758 prop->property = PTP_OPC_Composer;
5759 prop->datatype = PTP_DTC_STR;
5760 prop->propval.str = strdup(composer);
5761 }
5762 break;
Linus Walleij7783b9b2007-09-22 22:10:44 +00005763 case PTP_OPC_Genre:
5764 if (genre != NULL) {
Linus Walleija6d0d482007-10-31 08:54:56 +00005765 prop = ptp_get_new_object_prop_entry(&props, &nrofprops);
Linus Walleij7783b9b2007-09-22 22:10:44 +00005766 prop->ObjectHandle = objecthandle;
5767 prop->property = PTP_OPC_Genre;
5768 prop->datatype = PTP_DTC_STR;
5769 prop->propval.str = strdup(genre);
5770 }
5771 break;
5772 case PTP_OPC_DateModified:
5773 // Tag with current time if that is supported
Linus Walleija6d0d482007-10-31 08:54:56 +00005774 prop = ptp_get_new_object_prop_entry(&props, &nrofprops);
Linus Walleij7783b9b2007-09-22 22:10:44 +00005775 prop->ObjectHandle = objecthandle;
5776 prop->property = PTP_OPC_DateModified;
5777 prop->datatype = PTP_DTC_STR;
5778 prop->propval.str = get_iso8601_stamp();
5779 break;
5780 default:
5781 break;
5782 }
5783 }
5784 ptp_free_objectpropdesc(&opd);
5785 }
5786
5787 // proplist could be NULL if we can't write any properties
5788 if (props != NULL) {
5789 ret = ptp_mtp_setobjectproplist(params, props, nrofprops);
5790
Linus Walleija6d0d482007-10-31 08:54:56 +00005791 ptp_destroy_object_prop_list(props, nrofprops);
Linus Walleij7783b9b2007-09-22 22:10:44 +00005792
5793 if (ret != PTP_RC_OK) {
5794 // TODO: return error of which property we couldn't set
5795 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "update_abstract_list(): "
5796 "could not set object property list.");
Richard Low016e3732007-09-23 14:53:46 +00005797 free(properties);
Linus Walleij7783b9b2007-09-22 22:10:44 +00005798 return -1;
5799 }
5800 }
5801
5802 } else if (ptp_operation_issupported(params,PTP_OC_MTP_SetObjectPropValue)) {
5803 for (i=0;i<propcnt;i++) {
5804 switch (properties[i]) {
5805 case PTP_OPC_Name:
5806 // Update title
5807 ret = set_object_string(device, objecthandle, PTP_OPC_Name, name);
5808 if (ret != 0) {
5809 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "update_abstract_list(): "
5810 "could not set title.");
5811 }
5812 break;
Linus Walleij0b75ab62007-12-28 00:47:34 +00005813 case PTP_OPC_AlbumArtist:
5814 // Update album artist
5815 ret = set_object_string(device, objecthandle, PTP_OPC_AlbumArtist, artist);
5816 if (ret != 0) {
5817 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "update_abstract_list(): "
5818 "could not set album artist name.");
5819 }
5820 break;
Linus Walleij7783b9b2007-09-22 22:10:44 +00005821 case PTP_OPC_Artist:
5822 // Update artist
5823 ret = set_object_string(device, objecthandle, PTP_OPC_Artist, artist);
5824 if (ret != 0) {
5825 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "update_abstract_list(): "
5826 "could not set artist name.");
5827 }
Linus Walleij31b74292008-05-02 23:29:06 +00005828 case PTP_OPC_Composer:
5829 // Update composer
5830 ret = set_object_string(device, objecthandle, PTP_OPC_Composer, composer);
5831 if (ret != 0) {
5832 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "update_abstract_list(): "
5833 "could not set composer name.");
5834 }
Linus Walleij7783b9b2007-09-22 22:10:44 +00005835 break;
5836 case PTP_OPC_Genre:
5837 // Update genre
5838 ret = set_object_string(device, objecthandle, PTP_OPC_Genre, genre);
5839 if (ret != 0) {
5840 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "update_abstract_list(): "
5841 "could not set genre.");
5842 }
5843 break;
5844 case PTP_OPC_DateModified:
5845 // Update date modified
5846 {
5847 char *tmpdate = get_iso8601_stamp();
5848 ret = set_object_string(device, objecthandle, PTP_OPC_DateModified, tmpdate);
5849 if (ret != 0) {
5850 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "update_abstract_list(): "
5851 "could not set modification date.");
5852 }
5853 free(tmpdate);
5854 }
5855 default:
5856 break;
5857 }
5858 }
Linus Walleij7783b9b2007-09-22 22:10:44 +00005859 } else {
5860 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "update_abstract_list(): "
5861 "Your device doesn't seem to support any known way of setting metadata.");
Richard Low016e3732007-09-23 14:53:46 +00005862 free(properties);
Linus Walleij7783b9b2007-09-22 22:10:44 +00005863 return -1;
5864 }
5865
5866 // Then the object references...
5867 if (no_tracks > 0) {
5868 // Add tracks to the new album as object references.
5869 ret = ptp_mtp_setobjectreferences (params, objecthandle, (uint32_t *) tracks, no_tracks);
5870 if (ret != PTP_RC_OK) {
5871 add_ptp_error_to_errorstack(device, ret, "update_abstract_list(): could not add tracks as object references.");
Richard Low016e3732007-09-23 14:53:46 +00005872 free(properties);
Linus Walleij7783b9b2007-09-22 22:10:44 +00005873 return -1;
5874 }
5875 }
5876
Richard Low016e3732007-09-23 14:53:46 +00005877 free(properties);
Linus Walleij7752b952007-10-19 20:11:46 +00005878
5879 update_metadata_cache(device, objecthandle);
Richard Low016e3732007-09-23 14:53:46 +00005880
Linus Walleij7783b9b2007-09-22 22:10:44 +00005881 return 0;
5882}
5883
Linus Walleijb7426d12006-11-25 23:19:11 +00005884
5885/**
5886 * This routine creates a new playlist based on the metadata
5887 * supplied. If the <code>tracks</code> field of the metadata
5888 * contains a track listing, these tracks will be added to the
5889 * playlist.
5890 * @param device a pointer to the device to create the new playlist on.
5891 * @param metadata the metadata for the new playlist. If the function
5892 * exits with success, the <code>playlist_id</code> field of this
5893 * struct will contain the new playlist ID of the playlist.
Linus Walleij5ce59db2008-03-12 21:22:58 +00005894 * <code>parent_id</code> will also be valid.
Linus Walleijb7426d12006-11-25 23:19:11 +00005895 * @param parenthandle the parent (e.g. folder) to store this playlist
5896 * in. Pass in 0 to put the playlist in the root directory.
5897 * @return 0 on success, any other value means failure.
5898 * @see LIBMTP_Update_Playlist()
5899 * @see LIBMTP_Delete_Object()
5900 */
5901int LIBMTP_Create_New_Playlist(LIBMTP_mtpdevice_t *device,
Linus Walleijea68f1f2008-06-22 21:54:44 +00005902 LIBMTP_playlist_t * const metadata)
Linus Walleijb7426d12006-11-25 23:19:11 +00005903{
Linus Walleija4e6bdc2008-05-23 22:31:53 +00005904 PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo;
Linus Walleijea68f1f2008-06-22 21:54:44 +00005905 uint32_t localph = metadata->parent_id;
Linus Walleijb7426d12006-11-25 23:19:11 +00005906
5907 // Use a default folder if none given
5908 if (localph == 0) {
5909 localph = device->default_playlist_folder;
5910 }
Linus Walleij5ce59db2008-03-12 21:22:58 +00005911 metadata->parent_id = localph;
Linus Walleijb7426d12006-11-25 23:19:11 +00005912
Linus Walleijf3c44052008-08-16 21:14:56 +00005913 // Samsung needs its own special type of playlists
5914 if(FLAG_PLAYLIST_SPL(ptp_usb)) {
5915 return playlist_t_to_spl(device, metadata);
5916 }
5917
Linus Walleijb7426d12006-11-25 23:19:11 +00005918 // Just create a new abstract audio/video playlist...
5919 return create_new_abstract_list(device,
5920 metadata->name,
Linus Walleijadce4a52007-04-23 07:28:11 +00005921 NULL,
5922 NULL,
Linus Walleij31b74292008-05-02 23:29:06 +00005923 NULL,
Linus Walleijb7426d12006-11-25 23:19:11 +00005924 localph,
Linus Walleijea68f1f2008-06-22 21:54:44 +00005925 metadata->storage_id,
Linus Walleijb7426d12006-11-25 23:19:11 +00005926 PTP_OFC_MTP_AbstractAudioVideoPlaylist,
Linus Walleija4e6bdc2008-05-23 22:31:53 +00005927 get_playlist_extension(ptp_usb),
Linus Walleijb7426d12006-11-25 23:19:11 +00005928 &metadata->playlist_id,
5929 metadata->tracks,
5930 metadata->no_tracks);
5931}
5932
Linus Walleij438bd7f2006-06-08 11:35:44 +00005933/**
5934 * This routine updates a playlist based on the metadata
5935 * supplied. If the <code>tracks</code> field of the metadata
5936 * contains a track listing, these tracks will be added to the
5937 * playlist in place of those already present, i.e. the
5938 * previous track listing will be deleted.
5939 * @param device a pointer to the device to create the new playlist on.
5940 * @param metadata the metadata for the playlist to be updated.
mopoke96143402006-10-30 04:37:26 +00005941 * notice that the field <code>playlist_id</code>
Linus Walleij438bd7f2006-06-08 11:35:44 +00005942 * must contain the apropriate playlist ID.
5943 * @return 0 on success, any other value means failure.
5944 * @see LIBMTP_Create_New_Playlist()
5945 * @see LIBMTP_Delete_Object()
5946 */
mopoke96143402006-10-30 04:37:26 +00005947int LIBMTP_Update_Playlist(LIBMTP_mtpdevice_t *device,
Linus Walleijf5306352006-06-08 12:00:23 +00005948 LIBMTP_playlist_t const * const metadata)
Linus Walleij438bd7f2006-06-08 11:35:44 +00005949{
Linus Walleijf3c44052008-08-16 21:14:56 +00005950
5951 // Samsung needs its own special type of playlists
5952 PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo;
5953 if(FLAG_PLAYLIST_SPL(ptp_usb)) {
5954 return update_spl_playlist(device, metadata);
5955 }
5956
Linus Walleij7783b9b2007-09-22 22:10:44 +00005957 return update_abstract_list(device,
5958 metadata->name,
5959 NULL,
5960 NULL,
Linus Walleij31b74292008-05-02 23:29:06 +00005961 NULL,
Linus Walleij7783b9b2007-09-22 22:10:44 +00005962 metadata->playlist_id,
5963 PTP_OFC_MTP_AbstractAudioVideoPlaylist,
5964 metadata->tracks,
5965 metadata->no_tracks);
Linus Walleij438bd7f2006-06-08 11:35:44 +00005966}
Linus Walleijaa4b0752006-07-26 22:21:04 +00005967
Linus Walleij0c33ec02006-10-27 10:15:40 +00005968/**
5969 * This creates a new album metadata structure and allocates memory
5970 * for it. Notice that if you add strings to this structure they
5971 * will be freed by the corresponding <code>LIBMTP_destroy_album_t</code>
5972 * operation later, so be careful of using strdup() when assigning
5973 * strings.
5974 *
5975 * @return a pointer to the newly allocated metadata structure.
5976 * @see LIBMTP_destroy_album_t()
5977 */
5978LIBMTP_album_t *LIBMTP_new_album_t(void)
5979{
5980 LIBMTP_album_t *new = (LIBMTP_album_t *) malloc(sizeof(LIBMTP_album_t));
5981 if (new == NULL) {
5982 return NULL;
5983 }
5984 new->album_id = 0;
Linus Walleij5ce59db2008-03-12 21:22:58 +00005985 new->parent_id = 0;
Linus Walleijea68f1f2008-06-22 21:54:44 +00005986 new->storage_id = 0;
Linus Walleij0c33ec02006-10-27 10:15:40 +00005987 new->name = NULL;
Linus Walleijadce4a52007-04-23 07:28:11 +00005988 new->artist = NULL;
Linus Walleij31b74292008-05-02 23:29:06 +00005989 new->composer = NULL;
Linus Walleijadce4a52007-04-23 07:28:11 +00005990 new->genre = NULL;
Linus Walleij0c33ec02006-10-27 10:15:40 +00005991 new->tracks = NULL;
5992 new->no_tracks = 0;
5993 new->next = NULL;
5994 return new;
5995}
5996
5997/**
5998 * This recursively deletes the memory for an album structure
5999 *
6000 * @param album structure to destroy
6001 * @see LIBMTP_new_album_t()
6002 */
6003void LIBMTP_destroy_album_t(LIBMTP_album_t *album)
6004{
6005 if (album == NULL) {
6006 return;
6007 }
6008 if (album->name != NULL)
6009 free(album->name);
Linus Walleijadce4a52007-04-23 07:28:11 +00006010 if (album->artist != NULL)
6011 free(album->artist);
Linus Walleij31b74292008-05-02 23:29:06 +00006012 if (album->composer != NULL)
6013 free(album->composer);
Linus Walleijadce4a52007-04-23 07:28:11 +00006014 if (album->genre != NULL)
6015 free(album->genre);
Linus Walleij0c33ec02006-10-27 10:15:40 +00006016 if (album->tracks != NULL)
6017 free(album->tracks);
6018 free(album);
6019 return;
6020}
6021
6022/**
6023 * This function returns a list of the albums available on the
6024 * device.
6025 *
6026 * @param device a pointer to the device to get the album listing from.
6027 * @return an album list on success, else NULL. If there are no albums
6028 * on the device, NULL will be returned as well.
6029 * @see LIBMTP_Get_Album()
6030 */
6031LIBMTP_album_t *LIBMTP_Get_Album_List(LIBMTP_mtpdevice_t *device)
6032{
6033 PTPParams *params = (PTPParams *) device->params;
6034 LIBMTP_album_t *retalbums = NULL;
6035 LIBMTP_album_t *curalbum = NULL;
6036 uint32_t i;
6037
6038 // Get all the handles if we haven't already done that
6039 if (params->handles.Handler == NULL) {
6040 flush_handles(device);
6041 }
6042
6043 for (i = 0; i < params->handles.n; i++) {
6044 LIBMTP_album_t *alb;
Linus Walleijf0bf4372007-07-01 21:47:38 +00006045 PTPObjectInfo *oi;
Linus Walleij0c33ec02006-10-27 10:15:40 +00006046 uint16_t ret;
6047
Linus Walleijf0bf4372007-07-01 21:47:38 +00006048 oi = &params->objectinfo[i];
Linus Walleij0c33ec02006-10-27 10:15:40 +00006049
Linus Walleijf0bf4372007-07-01 21:47:38 +00006050 // Ignore stuff that isn't an album
6051 if ( oi->ObjectFormat != PTP_OFC_MTP_AbstractAudioAlbum ) {
6052 continue;
Linus Walleij0c33ec02006-10-27 10:15:40 +00006053 }
Linus Walleijf0bf4372007-07-01 21:47:38 +00006054
6055 // Allocate a new album type
6056 alb = LIBMTP_new_album_t();
Linus Walleij5ce59db2008-03-12 21:22:58 +00006057 alb->album_id = params->handles.Handler[i];
6058 alb->parent_id = oi->ParentObject;
Linus Walleijea68f1f2008-06-22 21:54:44 +00006059 alb->storage_id = oi->StorageID;
Linus Walleij5ce59db2008-03-12 21:22:58 +00006060
Linus Walleijf0bf4372007-07-01 21:47:38 +00006061 // Get metadata for it.
6062 alb->name = get_string_from_object(device, params->handles.Handler[i], PTP_OPC_Name);
Linus Walleij0b75ab62007-12-28 00:47:34 +00006063 alb->artist = get_string_from_object(device, params->handles.Handler[i], PTP_OPC_AlbumArtist);
6064 if (alb->artist == NULL) {
6065 alb->artist = get_string_from_object(device, params->handles.Handler[i], PTP_OPC_Artist);
6066 }
Linus Walleij31b74292008-05-02 23:29:06 +00006067 alb->composer = get_string_from_object(device, params->handles.Handler[i], PTP_OPC_Composer);
Linus Walleijf0bf4372007-07-01 21:47:38 +00006068 alb->genre = get_string_from_object(device, params->handles.Handler[i], PTP_OPC_Genre);
Linus Walleijf0bf4372007-07-01 21:47:38 +00006069
6070 // Then get the track listing for this album
6071 ret = ptp_mtp_getobjectreferences(params, alb->album_id, &alb->tracks, &alb->no_tracks);
6072 if (ret != PTP_RC_OK) {
6073 add_ptp_error_to_errorstack(device, ret, "LIBMTP_Get_Album_List(): Could not get object references.");
6074 alb->tracks = NULL;
6075 alb->no_tracks = 0;
6076 }
6077
6078 // Add album to a list that will be returned afterwards.
6079 if (retalbums == NULL) {
6080 retalbums = alb;
6081 curalbum = alb;
6082 } else {
6083 curalbum->next = alb;
6084 curalbum = alb;
6085 }
6086
Linus Walleij0c33ec02006-10-27 10:15:40 +00006087 }
6088 return retalbums;
6089}
6090
6091/**
6092 * This function retrieves an individual album from the device.
6093 * @param device a pointer to the device to get the album from.
6094 * @param albid the unique ID of the album to retrieve.
6095 * @return a valid album metadata or NULL on failure.
6096 * @see LIBMTP_Get_Album_List()
6097 */
6098LIBMTP_album_t *LIBMTP_Get_Album(LIBMTP_mtpdevice_t *device, uint32_t const albid)
6099{
6100 PTPParams *params = (PTPParams *) device->params;
6101 uint32_t i;
6102
6103 // Get all the handles if we haven't already done that
6104 if (params->handles.Handler == NULL) {
6105 flush_handles(device);
6106 }
6107
6108 for (i = 0; i < params->handles.n; i++) {
6109 LIBMTP_album_t *alb;
Linus Walleijf0bf4372007-07-01 21:47:38 +00006110 PTPObjectInfo *oi;
Linus Walleij0c33ec02006-10-27 10:15:40 +00006111 uint16_t ret;
6112
6113 if (params->handles.Handler[i] != albid) {
6114 continue;
6115 }
6116
Linus Walleijf0bf4372007-07-01 21:47:38 +00006117 oi = &params->objectinfo[i];
Linus Walleij0c33ec02006-10-27 10:15:40 +00006118
Linus Walleijf0bf4372007-07-01 21:47:38 +00006119 // Ignore stuff that isn't an album
6120 if ( oi->ObjectFormat != PTP_OFC_MTP_AbstractAudioAlbum ) {
Linus Walleij0c33ec02006-10-27 10:15:40 +00006121 return NULL;
6122 }
Linus Walleijf0bf4372007-07-01 21:47:38 +00006123
6124 // Allocate a new album type
6125 alb = LIBMTP_new_album_t();
6126 alb->album_id = params->handles.Handler[i];
Linus Walleij5ce59db2008-03-12 21:22:58 +00006127 alb->parent_id = oi->ParentObject;
Linus Walleijea68f1f2008-06-22 21:54:44 +00006128 alb->storage_id = oi->StorageID;
Linus Walleij5ce59db2008-03-12 21:22:58 +00006129
6130 // Get metadata for it.
Linus Walleijf0bf4372007-07-01 21:47:38 +00006131 alb->name = get_string_from_object(device, params->handles.Handler[i], PTP_OPC_Name);
Linus Walleij0b75ab62007-12-28 00:47:34 +00006132 alb->artist = get_string_from_object(device, params->handles.Handler[i], PTP_OPC_AlbumArtist);
6133 if (alb->artist == NULL) {
6134 alb->artist = get_string_from_object(device, params->handles.Handler[i], PTP_OPC_Artist);
6135 }
Linus Walleij31b74292008-05-02 23:29:06 +00006136 alb->composer = get_string_from_object(device, params->handles.Handler[i], PTP_OPC_Composer);
Linus Walleijf0bf4372007-07-01 21:47:38 +00006137 alb->genre = get_string_from_object(device, params->handles.Handler[i], PTP_OPC_Genre);
6138 ret = ptp_mtp_getobjectreferences(params, alb->album_id, &alb->tracks, &alb->no_tracks);
6139 if (ret != PTP_RC_OK) {
6140 add_ptp_error_to_errorstack(device, ret, "LIBMTP_Get_Album: Could not get object references.");
6141 alb->tracks = NULL;
6142 alb->no_tracks = 0;
6143 }
6144
6145 return alb;
Linus Walleij0c33ec02006-10-27 10:15:40 +00006146 }
6147 return NULL;
6148}
6149
6150/**
6151 * This routine creates a new album based on the metadata
6152 * supplied. If the <code>tracks</code> field of the metadata
6153 * contains a track listing, these tracks will be added to the
6154 * album.
6155 * @param device a pointer to the device to create the new album on.
6156 * @param metadata the metadata for the new album. If the function
6157 * exits with success, the <code>album_id</code> field of this
6158 * struct will contain the new ID of the album.
Linus Walleij5ce59db2008-03-12 21:22:58 +00006159 * <code>parent_id</code> will also be valid.
Linus Walleij0c33ec02006-10-27 10:15:40 +00006160 * @param parenthandle the parent (e.g. folder) to store this album
6161 * in. Pass in 0 to put the album in the default music directory.
6162 * @return 0 on success, any other value means failure.
6163 * @see LIBMTP_Update_Album()
6164 * @see LIBMTP_Delete_Object()
6165 */
6166int LIBMTP_Create_New_Album(LIBMTP_mtpdevice_t *device,
Linus Walleijea68f1f2008-06-22 21:54:44 +00006167 LIBMTP_album_t * const metadata)
Linus Walleij0c33ec02006-10-27 10:15:40 +00006168{
Linus Walleijea68f1f2008-06-22 21:54:44 +00006169 uint32_t localph = metadata->parent_id;
mopoke96143402006-10-30 04:37:26 +00006170
Linus Walleij0c33ec02006-10-27 10:15:40 +00006171 // Use a default folder if none given
6172 if (localph == 0) {
rreardon508705f2006-11-19 21:27:22 +00006173 localph = device->default_album_folder;
Linus Walleij0c33ec02006-10-27 10:15:40 +00006174 }
Linus Walleij5ce59db2008-03-12 21:22:58 +00006175 metadata->parent_id = localph;
Linus Walleij0c33ec02006-10-27 10:15:40 +00006176
Linus Walleijb7426d12006-11-25 23:19:11 +00006177 // Just create a new abstract album...
6178 return create_new_abstract_list(device,
6179 metadata->name,
Linus Walleijadce4a52007-04-23 07:28:11 +00006180 metadata->artist,
Linus Walleij31b74292008-05-02 23:29:06 +00006181 metadata->composer,
Linus Walleijadce4a52007-04-23 07:28:11 +00006182 metadata->genre,
Linus Walleijb7426d12006-11-25 23:19:11 +00006183 localph,
Linus Walleijea68f1f2008-06-22 21:54:44 +00006184 metadata->storage_id,
Linus Walleijb7426d12006-11-25 23:19:11 +00006185 PTP_OFC_MTP_AbstractAudioAlbum,
6186 ".alb",
6187 &metadata->album_id,
6188 metadata->tracks,
6189 metadata->no_tracks);
Linus Walleij0c33ec02006-10-27 10:15:40 +00006190}
6191
6192/**
rreardon5332f9c2006-12-05 10:18:23 +00006193 * This creates a new sample data metadata structure and allocates memory
6194 * for it. Notice that if you add strings to this structure they
6195 * will be freed by the corresponding <code>LIBMTP_destroy_sampledata_t</code>
6196 * operation later, so be careful of using strdup() when assigning
6197 * strings.
6198 *
6199 * @return a pointer to the newly allocated metadata structure.
6200 * @see LIBMTP_destroy_sampledata_t()
6201 */
6202LIBMTP_filesampledata_t *LIBMTP_new_filesampledata_t(void)
6203{
6204 LIBMTP_filesampledata_t *new = (LIBMTP_filesampledata_t *) malloc(sizeof(LIBMTP_filesampledata_t));
6205 if (new == NULL) {
6206 return NULL;
6207 }
6208 new->height=0;
6209 new->width = 0;
6210 new->data = NULL;
6211 new->duration = 0;
6212 new->size = 0;
6213 return new;
6214}
6215
Linus Walleijf1b02f22006-12-06 09:03:23 +00006216/**
6217 * This destroys a file sample metadata type.
6218 * @param sample the file sample metadata to be destroyed.
6219 */
6220void LIBMTP_destroy_filesampledata_t(LIBMTP_filesampledata_t * sample)
6221{
6222 if (sample == NULL) {
6223 return;
6224 }
6225 if (sample->data != NULL) {
6226 free(sample->data);
6227 }
6228 free(sample);
6229}
6230
6231/**
6232 * This routine figures out whether a certain filetype supports
6233 * representative samples (small thumbnail images) or not. This
6234 * typically applies to JPEG files, MP3 files and Album abstract
6235 * playlists, but in theory any filetype could support representative
6236 * samples.
6237 * @param device a pointer to the device which is to be examined.
Linus Walleij9ffe9982008-01-20 13:42:52 +00006238 * @param filetype the fileype to examine, and return the representative sample
Linus Walleijf1b02f22006-12-06 09:03:23 +00006239 * properties for.
6240 * @param sample this will contain a new sample type with the fields
6241 * filled in with suitable default values. For example, the
6242 * supported sample type will be set, the supported height and
6243 * width will be set to max values if it is an image sample,
6244 * and duration will also be given some suitable default value
6245 * which should not be exceeded on audio samples. If the
6246 * device does not support samples for this filetype, this
6247 * pointer will be NULL. If it is not NULL, the user must
6248 * destroy this struct with <code>LIBMTP_destroy_filesampledata_t()</code>
6249 * after use.
6250 * @return 0 on success, any other value means failure.
6251 * @see LIBMTP_Send_Representative_Sample()
6252 * @see LIBMTP_Create_New_Album()
6253 */
6254int LIBMTP_Get_Representative_Sample_Format(LIBMTP_mtpdevice_t *device,
6255 LIBMTP_filetype_t const filetype,
6256 LIBMTP_filesampledata_t ** sample)
6257{
6258 uint16_t ret;
6259 PTPParams *params = (PTPParams *) device->params;
6260 uint16_t *props = NULL;
6261 uint32_t propcnt = 0;
6262 int i;
6263 // TODO: Get rid of these when we can properly query the device.
6264 int support_data = 0;
6265 int support_format = 0;
6266 int support_height = 0;
6267 int support_width = 0;
6268 int support_duration = 0;
6269
rreardon21a3a9d2007-08-15 10:56:31 +00006270 PTPObjectPropDesc opd_height;
6271 PTPObjectPropDesc opd_width;
6272 PTPObjectPropDesc opd_format;
6273 PTPObjectPropDesc opd_duration;
6274
Linus Walleijf1b02f22006-12-06 09:03:23 +00006275 // Default to no type supported.
6276 *sample = NULL;
6277
6278 ret = ptp_mtp_getobjectpropssupported(params, map_libmtp_type_to_ptp_type(filetype), &propcnt, &props);
6279 if (ret != PTP_RC_OK) {
Linus Walleij070e9b42007-01-22 08:49:28 +00006280 add_ptp_error_to_errorstack(device, ret, "LIBMTP_Get_Representative_Sample_Format(): could not get object properties.");
Linus Walleijf1b02f22006-12-06 09:03:23 +00006281 return -1;
6282 }
6283 /*
6284 * TODO: when walking through these object properties, make calls to
6285 * a new function in ptp.h/ptp.c that can send the command
6286 * PTP_OC_MTP_GetObjectPropDesc to get max/min values of the properties
6287 * supported.
6288 */
6289 for (i = 0; i < propcnt; i++) {
6290 switch(props[i]) {
6291 case PTP_OPC_RepresentativeSampleData:
6292 support_data = 1;
6293 break;
6294 case PTP_OPC_RepresentativeSampleFormat:
6295 support_format = 1;
6296 break;
6297 case PTP_OPC_RepresentativeSampleSize:
6298 break;
6299 case PTP_OPC_RepresentativeSampleHeight:
6300 support_height = 1;
6301 break;
6302 case PTP_OPC_RepresentativeSampleWidth:
6303 support_width = 1;
6304 break;
6305 case PTP_OPC_RepresentativeSampleDuration:
6306 support_duration = 1;
6307 break;
6308 default:
6309 break;
6310 }
6311 }
6312 free(props);
rreardon21a3a9d2007-08-15 10:56:31 +00006313
Linus Walleijf1b02f22006-12-06 09:03:23 +00006314 if (support_data && support_format && support_height && support_width && !support_duration) {
6315 // Something that supports height and width and not duration is likely to be JPEG
6316 LIBMTP_filesampledata_t *retsam = LIBMTP_new_filesampledata_t();
rreardon21a3a9d2007-08-15 10:56:31 +00006317 /*
6318 * Populate the sample format with the first supported format
6319 *
6320 * TODO: figure out how to pass back more than one format if more are
6321 * supported by the device.
6322 */
6323 ptp_mtp_getobjectpropdesc (params, PTP_OPC_RepresentativeSampleFormat, map_libmtp_type_to_ptp_type(filetype), &opd_format);
6324 retsam->filetype = map_ptp_type_to_libmtp_type(opd_format.FORM.Enum.SupportedValue[0].u16);
6325 /* Populate the maximum image height */
6326 ptp_mtp_getobjectpropdesc (params, PTP_OPC_RepresentativeSampleWidth, map_libmtp_type_to_ptp_type(filetype), &opd_width);
6327 retsam->width = opd_width.FORM.Range.MaximumValue.u32;
6328 /* Populate the maximum image width */
6329 ptp_mtp_getobjectpropdesc (params, PTP_OPC_RepresentativeSampleHeight, map_libmtp_type_to_ptp_type(filetype), &opd_height);
6330 retsam->height = opd_height.FORM.Range.MaximumValue.u32;
Linus Walleijf1b02f22006-12-06 09:03:23 +00006331 *sample = retsam;
6332 } else if (support_data && support_format && !support_height && !support_width && support_duration) {
6333 // Another qualified guess
6334 LIBMTP_filesampledata_t *retsam = LIBMTP_new_filesampledata_t();
rreardon21a3a9d2007-08-15 10:56:31 +00006335 /*
6336 * Populate the sample format with the first supported format
6337 *
6338 * TODO: figure out how to pass back more than one format if more are
6339 * supported by the device.
6340 */
6341 ptp_mtp_getobjectpropdesc (params, PTP_OPC_RepresentativeSampleFormat, map_libmtp_type_to_ptp_type(filetype), &opd_format);
6342 retsam->filetype = map_ptp_type_to_libmtp_type(opd_format.FORM.Enum.SupportedValue[0].u16);
6343 /* Populate the maximum duration */
6344 ptp_mtp_getobjectpropdesc (params, PTP_OPC_RepresentativeSampleDuration, map_libmtp_type_to_ptp_type(filetype), &opd_duration);
6345 retsam->duration = opd_duration.FORM.Range.MaximumValue.u32;
Linus Walleijf1b02f22006-12-06 09:03:23 +00006346 *sample = retsam;
6347 }
6348 return 0;
6349}
rreardon5332f9c2006-12-05 10:18:23 +00006350
6351/**
6352 * This routine sends representative sample data for an object.
6353 * This uses the RepresentativeSampleData property of the album,
6354 * if the device supports it. The data should be of a format acceptable
6355 * to the player (for iRiver and Creative, this seems to be JPEG) and
6356 * must not be too large. (for a Creative, max seems to be about 20KB.)
Linus Walleij0c33ec02006-10-27 10:15:40 +00006357 * TODO: there must be a way to find the max size for an ObjectPropertyValue.
Linus Walleij7e3f2762006-12-03 22:52:05 +00006358 * @param device a pointer to the device which the object is on.
6359 * @param id unique id of the object to set artwork for.
Richard Low36e447c2008-01-20 15:24:55 +00006360 * @param pointer to LIBMTP_filesampledata_t struct containing data
Linus Walleij0c33ec02006-10-27 10:15:40 +00006361 * @return 0 on success, any other value means failure.
Richard Low36e447c2008-01-20 15:24:55 +00006362 * @see LIBMTP_Get_Representative_Sample()
Linus Walleijf1b02f22006-12-06 09:03:23 +00006363 * @see LIBMTP_Get_Representative_Sample_Format()
Linus Walleij0c33ec02006-10-27 10:15:40 +00006364 * @see LIBMTP_Create_New_Album()
6365 */
Linus Walleij7e3f2762006-12-03 22:52:05 +00006366int LIBMTP_Send_Representative_Sample(LIBMTP_mtpdevice_t *device,
rreardon5332f9c2006-12-05 10:18:23 +00006367 uint32_t const id,
6368 LIBMTP_filesampledata_t *sampledata)
Linus Walleij0c33ec02006-10-27 10:15:40 +00006369{
6370 uint16_t ret;
6371 PTPParams *params = (PTPParams *) device->params;
Richard Lowbb9fb4a2008-05-18 14:49:34 +00006372 PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo;
Linus Walleij0c33ec02006-10-27 10:15:40 +00006373 PTPPropertyValue propval;
Linus Walleijf0bf4372007-07-01 21:47:38 +00006374 PTPObjectInfo *oi;
6375 uint32_t i;
Linus Walleijf1b02f22006-12-06 09:03:23 +00006376 uint16_t *props = NULL;
6377 uint32_t propcnt = 0;
6378 int supported = 0;
rreardon5332f9c2006-12-05 10:18:23 +00006379
6380 // get the file format for the object we're going to send representative data for
Linus Walleijf0bf4372007-07-01 21:47:38 +00006381 oi = NULL;
6382 for (i = 0; i < params->handles.n; i++) {
6383 if (params->handles.Handler[i] == id) {
6384 oi = &params->objectinfo[i];
6385 break;
6386 }
6387 }
rreardon5332f9c2006-12-05 10:18:23 +00006388
Linus Walleijf0bf4372007-07-01 21:47:38 +00006389 if (oi == NULL) {
6390 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Send_Representative_Sample(): could not get object info.");
rreardon5332f9c2006-12-05 10:18:23 +00006391 return -1;
6392 }
6393
6394 // check that we can send representative sample data for this object format
Linus Walleijf0bf4372007-07-01 21:47:38 +00006395 ret = ptp_mtp_getobjectpropssupported(params, oi->ObjectFormat, &propcnt, &props);
Linus Walleij0c33ec02006-10-27 10:15:40 +00006396 if (ret != PTP_RC_OK) {
Linus Walleij070e9b42007-01-22 08:49:28 +00006397 add_ptp_error_to_errorstack(device, ret, "LIBMTP_Send_Representative_Sample(): could not get object properties.");
Linus Walleij0c33ec02006-10-27 10:15:40 +00006398 return -1;
6399 }
Linus Walleijf1b02f22006-12-06 09:03:23 +00006400
Linus Walleij0c33ec02006-10-27 10:15:40 +00006401 for (i = 0; i < propcnt; i++) {
Linus Walleijf1b02f22006-12-06 09:03:23 +00006402 if (props[i] == PTP_OPC_RepresentativeSampleData) {
Linus Walleij0c33ec02006-10-27 10:15:40 +00006403 supported = 1;
Linus Walleijf1b02f22006-12-06 09:03:23 +00006404 break;
6405 }
Linus Walleij0c33ec02006-10-27 10:15:40 +00006406 }
6407 if (!supported) {
Linus Walleijf1b02f22006-12-06 09:03:23 +00006408 free(props);
Linus Walleij070e9b42007-01-22 08:49:28 +00006409 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 +00006410 return -1;
6411 }
Linus Walleijf1b02f22006-12-06 09:03:23 +00006412 free(props);
rreardon5332f9c2006-12-05 10:18:23 +00006413
Linus Walleijf1b02f22006-12-06 09:03:23 +00006414 // Go ahead and send the data
rreardon5332f9c2006-12-05 10:18:23 +00006415 propval.a.count = sampledata->size;
6416 propval.a.v = malloc(sizeof(PTPPropertyValue) * sampledata->size);
6417 for (i = 0; i < sampledata->size; i++) {
6418 propval.a.v[i].u8 = sampledata->data[i];
Linus Walleij7e3f2762006-12-03 22:52:05 +00006419 }
6420
Linus Walleij0c33ec02006-10-27 10:15:40 +00006421 ret = ptp_mtp_setobjectpropvalue(params,id,PTP_OPC_RepresentativeSampleData,
Linus Walleijf1b02f22006-12-06 09:03:23 +00006422 &propval,PTP_DTC_AUINT8);
Linus Walleij0c33ec02006-10-27 10:15:40 +00006423 if (ret != PTP_RC_OK) {
Linus Walleij070e9b42007-01-22 08:49:28 +00006424 add_ptp_error_to_errorstack(device, ret, "LIBMTP_Send_Representative_Sample(): could not send sample data.");
Linus Walleij7e3f2762006-12-03 22:52:05 +00006425 free(propval.a.v);
Linus Walleij0c33ec02006-10-27 10:15:40 +00006426 return -1;
6427 }
Linus Walleij7e3f2762006-12-03 22:52:05 +00006428 free(propval.a.v);
rreardond2ddb632006-12-12 12:13:21 +00006429
6430 /* Set the height and width if the sample is an image, otherwise just
6431 * set the duration and size */
6432 switch(sampledata->filetype) {
Linus Walleije1ac07e2006-12-14 19:38:59 +00006433 case LIBMTP_FILETYPE_JPEG:
6434 case LIBMTP_FILETYPE_JFIF:
6435 case LIBMTP_FILETYPE_TIFF:
6436 case LIBMTP_FILETYPE_BMP:
6437 case LIBMTP_FILETYPE_GIF:
6438 case LIBMTP_FILETYPE_PICT:
6439 case LIBMTP_FILETYPE_PNG:
Linus Walleijfec4d562008-06-01 22:30:36 +00006440 if (!FLAG_BROKEN_SET_SAMPLE_DIMENSIONS(ptp_usb)) {
Richard Lowbb9fb4a2008-05-18 14:49:34 +00006441 // For images, set the height and width
6442 set_object_u32(device, id, PTP_OPC_RepresentativeSampleHeight, sampledata->height);
6443 set_object_u32(device, id, PTP_OPC_RepresentativeSampleWidth, sampledata->width);
6444 }
Linus Walleije1ac07e2006-12-14 19:38:59 +00006445 break;
6446 default:
6447 // For anything not an image, set the duration and size
6448 set_object_u32(device, id, PTP_OPC_RepresentativeSampleDuration, sampledata->duration);
6449 set_object_u32(device, id, PTP_OPC_RepresentativeSampleSize, sampledata->size);
6450 break;
rreardond2ddb632006-12-12 12:13:21 +00006451 }
rreardond2ddb632006-12-12 12:13:21 +00006452
Linus Walleij0c33ec02006-10-27 10:15:40 +00006453 return 0;
6454}
6455
6456/**
Richard Low36e447c2008-01-20 15:24:55 +00006457 * This routine gets representative sample data for an object.
6458 * This uses the RepresentativeSampleData property of the album,
6459 * if the device supports it.
6460 * @param device a pointer to the device which the object is on.
6461 * @param id unique id of the object to get data for.
6462 * @param pointer to LIBMTP_filesampledata_t struct to receive data
6463 * @return 0 on success, any other value means failure.
6464 * @see LIBMTP_Send_Representative_Sample()
6465 * @see LIBMTP_Get_Representative_Sample_Format()
6466 * @see LIBMTP_Create_New_Album()
6467 */
6468int LIBMTP_Get_Representative_Sample(LIBMTP_mtpdevice_t *device,
6469 uint32_t const id,
6470 LIBMTP_filesampledata_t *sampledata)
6471{
6472 uint16_t ret;
6473 PTPParams *params = (PTPParams *) device->params;
6474 PTPPropertyValue propval;
6475 PTPObjectInfo *oi;
6476 uint32_t i;
6477 uint16_t *props = NULL;
6478 uint32_t propcnt = 0;
6479 int supported = 0;
6480
6481 // get the file format for the object we're going to send representative data for
6482 oi = NULL;
6483 for (i = 0; i < params->handles.n; i++) {
6484 if (params->handles.Handler[i] == id) {
6485 oi = &params->objectinfo[i];
6486 break;
6487 }
6488 }
6489
6490 if (oi == NULL) {
6491 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Get_Representative_Sample(): could not get object info.");
6492 return -1;
6493 }
6494
6495 // check that we can store representative sample data for this object format
6496 ret = ptp_mtp_getobjectpropssupported(params, oi->ObjectFormat, &propcnt, &props);
6497 if (ret != PTP_RC_OK) {
6498 add_ptp_error_to_errorstack(device, ret, "LIBMTP_Get_Representative_Sample(): could not get object properties.");
6499 return -1;
6500 }
6501
6502 for (i = 0; i < propcnt; i++) {
6503 if (props[i] == PTP_OPC_RepresentativeSampleData) {
6504 supported = 1;
6505 break;
6506 }
6507 }
6508 if (!supported) {
6509 free(props);
6510 add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Get_Representative_Sample(): object type doesn't support RepresentativeSampleData.");
6511 return -1;
6512 }
6513 free(props);
6514
6515 // Get the data
6516 ret = ptp_mtp_getobjectpropvalue(params,id,PTP_OPC_RepresentativeSampleData,
6517 &propval,PTP_DTC_AUINT8);
6518 if (ret != PTP_RC_OK) {
6519 add_ptp_error_to_errorstack(device, ret, "LIBMTP_Get_Representative_Sample(): could not get sample data.");
6520 return -1;
6521 }
6522
6523 // Store it
6524 sampledata->size = propval.a.count;
6525 sampledata->data = malloc(sizeof(PTPPropertyValue) * propval.a.count);
6526 for (i = 0; i < propval.a.count; i++) {
6527 sampledata->data[i] = propval.a.v[i].u8;
6528 }
6529 free(propval.a.v);
6530
6531 // Get the other properties
6532 sampledata->width = get_u32_from_object(device, id, PTP_OPC_RepresentativeSampleWidth, 0);
6533 sampledata->height = get_u32_from_object(device, id, PTP_OPC_RepresentativeSampleHeight, 0);
6534 sampledata->duration = get_u32_from_object(device, id, PTP_OPC_RepresentativeSampleDuration, 0);
6535 sampledata->filetype = map_ptp_type_to_libmtp_type(
6536 get_u16_from_object(device, id, PTP_OPC_RepresentativeSampleFormat, LIBMTP_FILETYPE_UNKNOWN));
6537
6538 return 0;
6539}
6540
6541/**
Linus Walleij0c33ec02006-10-27 10:15:40 +00006542 * This routine updates an album based on the metadata
6543 * supplied. If the <code>tracks</code> field of the metadata
6544 * contains a track listing, these tracks will be added to the
6545 * album in place of those already present, i.e. the
6546 * previous track listing will be deleted.
6547 * @param device a pointer to the device to create the new album on.
6548 * @param metadata the metadata for the album to be updated.
6549 * notice that the field <code>album_id</code>
6550 * must contain the apropriate album ID.
6551 * @return 0 on success, any other value means failure.
6552 * @see LIBMTP_Create_New_Album()
6553 * @see LIBMTP_Delete_Object()
6554 */
6555int LIBMTP_Update_Album(LIBMTP_mtpdevice_t *device,
6556 LIBMTP_album_t const * const metadata)
6557{
Linus Walleij7783b9b2007-09-22 22:10:44 +00006558 return update_abstract_list(device,
6559 metadata->name,
6560 metadata->artist,
Linus Walleij31b74292008-05-02 23:29:06 +00006561 metadata->composer,
Linus Walleij7783b9b2007-09-22 22:10:44 +00006562 metadata->genre,
6563 metadata->album_id,
6564 PTP_OFC_MTP_AbstractAudioAlbum,
6565 metadata->tracks,
6566 metadata->no_tracks);
Linus Walleij0c33ec02006-10-27 10:15:40 +00006567}
Linus Walleijaa4b0752006-07-26 22:21:04 +00006568
6569/**
6570 * Dummy function needed to interface to upstream
6571 * ptp.c/ptp.h files.
6572 */
6573void ptp_nikon_getptpipguid (unsigned char* guid) {
6574 return;
6575}
tsaarnia3eb60a2007-07-06 17:41:30 +00006576
6577/**
Linus Walleij7752b952007-10-19 20:11:46 +00006578 * Add an object to cache.
6579 * @param device the device which may have a cache to which the object should be added.
6580 * @param object_id the object to add to the cache.
tsaarnia3eb60a2007-07-06 17:41:30 +00006581 */
Linus Walleija6d0d482007-10-31 08:54:56 +00006582static void add_object_to_cache(LIBMTP_mtpdevice_t *device, uint32_t object_id)
tsaarnia3eb60a2007-07-06 17:41:30 +00006583{
6584 PTPParams *params = (PTPParams *)device->params;
tsaarnia3eb60a2007-07-06 17:41:30 +00006585 uint16_t ret;
tsaarnia3eb60a2007-07-06 17:41:30 +00006586
Linus Walleija6d0d482007-10-31 08:54:56 +00006587 ret = ptp_add_object_to_cache(params, object_id);
6588 if (ret != PTP_RC_OK) {
6589 add_ptp_error_to_errorstack(device, ret, "add_object_to_cache(): couldn't add object to cache");
tsaarnia3eb60a2007-07-06 17:41:30 +00006590 }
6591}
6592
6593
6594/**
6595 * Update cache after object has been modified
Linus Walleija6d0d482007-10-31 08:54:56 +00006596 * @param device the device which may have a cache to which the object should be updated.
6597 * @param object_id the object to update.
tsaarnia3eb60a2007-07-06 17:41:30 +00006598 */
Linus Walleij7752b952007-10-19 20:11:46 +00006599static void update_metadata_cache(LIBMTP_mtpdevice_t *device, uint32_t object_id)
tsaarnia3eb60a2007-07-06 17:41:30 +00006600{
Linus Walleija3d0eaa2007-11-18 22:00:48 +00006601 PTPParams *params = (PTPParams *)device->params;
6602
6603 ptp_remove_object_from_cache(params, object_id);
Linus Walleija6d0d482007-10-31 08:54:56 +00006604 add_object_to_cache(device, object_id);
tsaarnia3eb60a2007-07-06 17:41:30 +00006605}