Linus Walleij | 10c5842 | 2006-06-02 08:51:53 +0000 | [diff] [blame] | 1 | /** |
| 2 | * \file libmtp.c |
| 3 | * This file provides an interface "glue" to the underlying |
| 4 | * PTP implementation from libgphoto2. It uses some local |
| 5 | * code to convert from/to UTF-8 (stored in unicode.c/.h) |
| 6 | * and some small utility functions, mainly for debugging |
| 7 | * (stored in util.c/.h). |
| 8 | * |
| 9 | * The three PTP files (ptp.c, ptp.h and ptp-pack.c) are |
| 10 | * plain copied from the libhphoto2 codebase. |
| 11 | * |
| 12 | * The files libusb-glue.c/.h are just what they say: an |
| 13 | * interface to libusb for the actual, physical USB traffic. |
| 14 | */ |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 15 | #include <string.h> |
Linus Walleij | dcde608 | 2006-02-17 16:16:34 +0000 | [diff] [blame] | 16 | #include <sys/types.h> |
Linus Walleij | 3ff2cf8 | 2006-02-20 15:03:26 +0000 | [diff] [blame] | 17 | #include <sys/stat.h> |
Linus Walleij | dcde608 | 2006-02-17 16:16:34 +0000 | [diff] [blame] | 18 | #include <fcntl.h> |
Linus Walleij | 2eb884b | 2006-08-04 19:17:36 +0000 | [diff] [blame] | 19 | #include <sys/mman.h> |
| 20 | |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 21 | #include "libmtp.h" |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 22 | #include "unicode.h" |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 23 | #include "ptp.h" |
Linus Walleij | 15e344f | 2006-03-06 15:15:00 +0000 | [diff] [blame] | 24 | #include "libusb-glue.h" |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 25 | |
Linus Walleij | c86afbd | 2006-05-04 19:05:24 +0000 | [diff] [blame] | 26 | /* |
Linus Walleij | f82dcea | 2006-09-11 11:23:47 +0000 | [diff] [blame] | 27 | * On MacOS (Darwin) and *BSD we're not using glibc, but libiconv. |
| 28 | * glibc knows that UCS-2 is to be in the local machine endianness, |
| 29 | * whereas libiconv does not. So we construct this macro to get |
| 30 | * things right. Reportedly, glibc 2.1.3 has a bug so that UCS-2 |
| 31 | * is always bigendian though, we would need to work around that |
| 32 | * too... |
Linus Walleij | d5d51c8 | 2006-09-11 06:57:50 +0000 | [diff] [blame] | 33 | */ |
| 34 | #ifndef __GLIBC__ |
Linus Walleij | f82dcea | 2006-09-11 11:23:47 +0000 | [diff] [blame] | 35 | #define UCS_2_INTERNAL "UCS-2-INTERNAL" |
Linus Walleij | d5d51c8 | 2006-09-11 06:57:50 +0000 | [diff] [blame] | 36 | #else |
Linus Walleij | f82dcea | 2006-09-11 11:23:47 +0000 | [diff] [blame] | 37 | #if (__GLIBC__ == 2 && __GLIBC_MINOR__ <= 1 ) |
| 38 | #error "Too old glibc. This versions iconv() implementation cannot be trusted." |
| 39 | #endif |
| 40 | #define UCS_2_INTERNAL "UCS-2" |
Linus Walleij | d5d51c8 | 2006-09-11 06:57:50 +0000 | [diff] [blame] | 41 | #endif |
| 42 | |
| 43 | /* |
Linus Walleij | c86afbd | 2006-05-04 19:05:24 +0000 | [diff] [blame] | 44 | * This is a mapping between libmtp internal MTP filetypes and |
| 45 | * the libgphoto2/PTP equivalent defines. We need this because |
| 46 | * otherwise the libmtp.h device has to be dependent on ptp.h |
| 47 | * to be installed too, and we don't want that. |
| 48 | */ |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 49 | typedef struct filemap_t LIBMTP_filemap_t; |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 50 | struct filemap_t { |
| 51 | char *description; /**< Text description for the file type */ |
| 52 | LIBMTP_filetype_t id; /**< LIBMTP internal type for the file type */ |
| 53 | uint16_t ptp_id; /**< PTP ID for the filetype */ |
| 54 | void *constructor; /**< Function to create the data structure for this file type */ |
| 55 | void *destructor; /**< Function to destroy the data structure for this file type */ |
| 56 | void *datafunc; /**< Function to fill in the data for this file type */ |
| 57 | LIBMTP_filemap_t *next; |
Linus Walleij | 16c51f0 | 2006-05-04 13:20:22 +0000 | [diff] [blame] | 58 | }; |
Linus Walleij | c86afbd | 2006-05-04 19:05:24 +0000 | [diff] [blame] | 59 | |
Linus Walleij | f67bca9 | 2006-05-29 09:33:39 +0000 | [diff] [blame] | 60 | // Global variables |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 61 | // This holds the global filetype mapping table |
Linus Walleij | f67bca9 | 2006-05-29 09:33:39 +0000 | [diff] [blame] | 62 | static LIBMTP_filemap_t *filemap = NULL; |
| 63 | |
| 64 | // Forward declarations of local functions |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 65 | static void flush_handles(LIBMTP_mtpdevice_t *device); |
Linus Walleij | f67bca9 | 2006-05-29 09:33:39 +0000 | [diff] [blame] | 66 | static uint16_t map_libmtp_type_to_ptp_type(LIBMTP_filetype_t intype); |
| 67 | static LIBMTP_filetype_t map_ptp_type_to_libmtp_type(uint16_t intype); |
Linus Walleij | cf223e6 | 2006-06-19 09:31:53 +0000 | [diff] [blame] | 68 | static int get_device_unicode_property(LIBMTP_mtpdevice_t *device, |
| 69 | char **unicstring, uint16_t property); |
Linus Walleij | 8ab5426 | 2006-06-21 07:12:28 +0000 | [diff] [blame] | 70 | static void get_track_metadata(LIBMTP_mtpdevice_t *device, uint16_t objectformat, |
| 71 | LIBMTP_track_t *track); |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 72 | |
| 73 | static LIBMTP_filemap_t *new_filemap_entry() |
| 74 | { |
Linus Walleij | f67bca9 | 2006-05-29 09:33:39 +0000 | [diff] [blame] | 75 | LIBMTP_filemap_t *filemap; |
| 76 | |
| 77 | filemap = (LIBMTP_filemap_t *)malloc(sizeof(LIBMTP_filemap_t)); |
| 78 | |
| 79 | if( filemap != NULL ) { |
| 80 | filemap->description = NULL; |
| 81 | filemap->id = LIBMTP_FILETYPE_UNKNOWN; |
| 82 | filemap->ptp_id = PTP_OFC_Undefined; |
| 83 | filemap->constructor = NULL; |
| 84 | filemap->destructor = NULL; |
| 85 | filemap->datafunc = NULL; |
| 86 | filemap->next = NULL; |
| 87 | } |
| 88 | |
| 89 | return filemap; |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 90 | } |
| 91 | |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 92 | /** |
| 93 | * Register an MTP or PTP filetype for data retrieval |
| 94 | * |
| 95 | * @param description Text description of filetype |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 96 | * @param id libmtp internal filetype id |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 97 | * @param ptp_id PTP filetype id |
| 98 | * @param constructor Pointer to function to create data structure for filetype |
| 99 | * @param destructor Pointer to function to destroy data structure for filetype |
| 100 | * @param datafunc Pointer to function to fill data structure |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 101 | * @return 0 for success any other value means error. |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 102 | */ |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 103 | int LIBMTP_Register_Filetype(char const * const description, LIBMTP_filetype_t const id, |
| 104 | uint16_t const ptp_id, void const * const constructor, |
| 105 | void const * const destructor, void const * const datafunc) |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 106 | { |
| 107 | LIBMTP_filemap_t *new = NULL, *current; |
| 108 | |
| 109 | // Has this LIBMTP filetype been registered before ? |
| 110 | current = filemap; |
| 111 | while (current != NULL) { |
Linus Walleij | f67bca9 | 2006-05-29 09:33:39 +0000 | [diff] [blame] | 112 | if(current->id == id) { |
| 113 | break; |
| 114 | } |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 115 | current = current->next; |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 116 | } |
Linus Walleij | f67bca9 | 2006-05-29 09:33:39 +0000 | [diff] [blame] | 117 | |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 118 | // Create the entry |
| 119 | if(current == NULL) { |
Linus Walleij | f67bca9 | 2006-05-29 09:33:39 +0000 | [diff] [blame] | 120 | new = new_filemap_entry(); |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 121 | if(new == NULL) { |
| 122 | return 1; |
| 123 | } |
Linus Walleij | f67bca9 | 2006-05-29 09:33:39 +0000 | [diff] [blame] | 124 | |
| 125 | new->id = id; |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 126 | if(description != NULL) { |
| 127 | new->description = strdup(description); |
| 128 | } |
Linus Walleij | f67bca9 | 2006-05-29 09:33:39 +0000 | [diff] [blame] | 129 | new->ptp_id = ptp_id; |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 130 | new->constructor = (void*) constructor; |
| 131 | new->destructor = (void*) destructor; |
| 132 | new->datafunc = (void*) datafunc; |
Linus Walleij | f67bca9 | 2006-05-29 09:33:39 +0000 | [diff] [blame] | 133 | |
| 134 | // Add the entry to the list |
| 135 | if(filemap == NULL) { |
| 136 | filemap = new; |
| 137 | } else { |
| 138 | current = filemap; |
| 139 | while (current->next != NULL ) current=current->next; |
| 140 | current->next = new; |
| 141 | } |
| 142 | // Update the existing entry |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 143 | } else { |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 144 | if (current->description != NULL) { |
| 145 | free(current->description); |
| 146 | } |
Linus Walleij | f67bca9 | 2006-05-29 09:33:39 +0000 | [diff] [blame] | 147 | current->description = NULL; |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 148 | if(description != NULL) { |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 149 | current->description = strdup(description); |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 150 | } |
Linus Walleij | f67bca9 | 2006-05-29 09:33:39 +0000 | [diff] [blame] | 151 | current->ptp_id = ptp_id; |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 152 | current->constructor = (void*) constructor; |
| 153 | current->destructor = (void*) destructor; |
| 154 | current->datafunc = (void*) datafunc; |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 157 | return 0; |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Set the description for a MTP filetype |
| 162 | * |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 163 | * @param id libmtp internal filetype id |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 164 | * @param description Text description of filetype |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 165 | * @return 0 on success, any other value means error. |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 166 | */ |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 167 | int LIBMTP_Set_Filetype_Description(LIBMTP_filetype_t const id, char const * const description) |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 168 | { |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 169 | LIBMTP_filemap_t *current; |
Linus Walleij | f67bca9 | 2006-05-29 09:33:39 +0000 | [diff] [blame] | 170 | |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 171 | if (filemap == NULL) { |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 172 | return -1; |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 173 | } |
Linus Walleij | f67bca9 | 2006-05-29 09:33:39 +0000 | [diff] [blame] | 174 | |
| 175 | // Go through the filemap until an entry is found |
| 176 | current = filemap; |
| 177 | |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 178 | while(current != NULL) { |
| 179 | if(current->id == id) { |
Linus Walleij | f67bca9 | 2006-05-29 09:33:39 +0000 | [diff] [blame] | 180 | break; |
| 181 | } |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 182 | current = current->next; |
Linus Walleij | f67bca9 | 2006-05-29 09:33:39 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 185 | if(current == NULL) { |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 186 | return -1; |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | if (current->description != NULL) { |
| 190 | free(current->description); |
| 191 | current->description = NULL; |
| 192 | } |
| 193 | if(description != NULL) { |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 194 | current->description = strdup(description); |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 195 | } |
| 196 | return 0; |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Set the constructor for a MTP filetype |
| 201 | * |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 202 | * @param id libmtp internal filetype id |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 203 | * @param constructor Pointer to a constructor function |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 204 | * @return 0 on success, any other value means failure |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 205 | */ |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 206 | int LIBMTP_Set_Constructor(LIBMTP_filetype_t const id, void const * const constructor) |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 207 | { |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 208 | LIBMTP_filemap_t *current; |
Linus Walleij | f67bca9 | 2006-05-29 09:33:39 +0000 | [diff] [blame] | 209 | |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 210 | if (filemap == NULL) { |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 211 | return -1; |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 212 | } |
| 213 | |
Linus Walleij | f67bca9 | 2006-05-29 09:33:39 +0000 | [diff] [blame] | 214 | // Go through the filemap until an entry is found |
| 215 | current = filemap; |
| 216 | |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 217 | while(current != NULL) { |
| 218 | if(current->id == id) { |
Linus Walleij | f67bca9 | 2006-05-29 09:33:39 +0000 | [diff] [blame] | 219 | break; |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 220 | } |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 221 | current = current->next; |
Linus Walleij | f67bca9 | 2006-05-29 09:33:39 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 224 | if (current == NULL) { |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 225 | return -1; |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 226 | } |
Linus Walleij | f67bca9 | 2006-05-29 09:33:39 +0000 | [diff] [blame] | 227 | |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 228 | current->constructor = (void*) constructor; |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 229 | return 0; |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Set the destructor for a MTP filetype |
| 234 | * |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 235 | * @param id libmtp internal filetype id |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 236 | * @param destructor Pointer to a destructor function |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 237 | * @return 0 on success, any other value means failure |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 238 | */ |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 239 | int LIBMTP_Set_Destructor(LIBMTP_filetype_t const id, void const * const destructor) |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 240 | { |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 241 | LIBMTP_filemap_t *current; |
Linus Walleij | f67bca9 | 2006-05-29 09:33:39 +0000 | [diff] [blame] | 242 | |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 243 | if (filemap == NULL) { |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 244 | return -1; |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 245 | } |
Linus Walleij | f67bca9 | 2006-05-29 09:33:39 +0000 | [diff] [blame] | 246 | |
| 247 | // Go through the filemap until an entry is found |
| 248 | current = filemap; |
| 249 | |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 250 | while(current != NULL) { |
| 251 | if(current->id == id) { |
Linus Walleij | f67bca9 | 2006-05-29 09:33:39 +0000 | [diff] [blame] | 252 | break; |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 253 | } |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 254 | current = current->next; |
Linus Walleij | f67bca9 | 2006-05-29 09:33:39 +0000 | [diff] [blame] | 255 | } |
| 256 | |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 257 | if(current == NULL) { |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 258 | return -1; |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 259 | } |
Linus Walleij | f67bca9 | 2006-05-29 09:33:39 +0000 | [diff] [blame] | 260 | |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 261 | current->destructor = (void *) destructor; |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 262 | return 0; |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | /** |
| 266 | * Set the datafunc for a MTP filetype |
| 267 | * |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 268 | * @param id libmtp internal filetype id |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 269 | * @param datafunc Pointer to a data function |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 270 | * @return 0 on success, any other value means failure |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 271 | */ |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 272 | int LIBMTP_Set_Datafunc(LIBMTP_filetype_t const id, void const * const datafunc) |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 273 | { |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 274 | LIBMTP_filemap_t *current; |
Linus Walleij | f67bca9 | 2006-05-29 09:33:39 +0000 | [diff] [blame] | 275 | |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 276 | if (filemap == NULL) { |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 277 | return -1; |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 278 | } |
Linus Walleij | f67bca9 | 2006-05-29 09:33:39 +0000 | [diff] [blame] | 279 | |
| 280 | // Go through the filemap until an entry is found |
| 281 | current = filemap; |
| 282 | |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 283 | while(current != NULL) { |
| 284 | if(current->id == id) { |
Linus Walleij | f67bca9 | 2006-05-29 09:33:39 +0000 | [diff] [blame] | 285 | break; |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 286 | } |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 287 | current = current->next; |
Linus Walleij | f67bca9 | 2006-05-29 09:33:39 +0000 | [diff] [blame] | 288 | } |
| 289 | |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 290 | if(current == NULL) { |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 291 | return -1; |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 292 | } |
Linus Walleij | f67bca9 | 2006-05-29 09:33:39 +0000 | [diff] [blame] | 293 | |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 294 | current->datafunc = (void *) datafunc; |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 295 | return 0; |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | static void init_filemap() |
| 299 | { |
Linus Walleij | f67bca9 | 2006-05-29 09:33:39 +0000 | [diff] [blame] | 300 | LIBMTP_Register_Filetype("RIFF WAVE file", LIBMTP_FILETYPE_WAV, PTP_OFC_WAV,LIBMTP_new_track_t,LIBMTP_destroy_track_t,NULL); |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 301 | LIBMTP_Register_Filetype("ISO MPEG Audio Layer 3", LIBMTP_FILETYPE_MP3, PTP_OFC_MP3,LIBMTP_new_track_t,LIBMTP_destroy_track_t,NULL); |
Linus Walleij | f67bca9 | 2006-05-29 09:33:39 +0000 | [diff] [blame] | 302 | LIBMTP_Register_Filetype("Microsoft Windows Media Audio", LIBMTP_FILETYPE_WMA, PTP_OFC_MTP_WMA,LIBMTP_new_track_t,LIBMTP_destroy_track_t,NULL); |
| 303 | LIBMTP_Register_Filetype("Ogg container format", LIBMTP_FILETYPE_OGG, PTP_OFC_MTP_OGG,LIBMTP_new_track_t,LIBMTP_destroy_track_t,NULL); |
Linus Walleij | e46f12e | 2006-06-22 17:53:25 +0000 | [diff] [blame] | 304 | LIBMTP_Register_Filetype("Audible.com Audio Codec", LIBMTP_FILETYPE_AUDIBLE, PTP_OFC_MTP_AudibleCodec,LIBMTP_new_track_t,LIBMTP_destroy_track_t,NULL); |
Linus Walleij | f67bca9 | 2006-05-29 09:33:39 +0000 | [diff] [blame] | 305 | LIBMTP_Register_Filetype("Advanced Acoustic Coding", LIBMTP_FILETYPE_MP4, PTP_OFC_MTP_MP4,LIBMTP_new_track_t,LIBMTP_destroy_track_t,NULL); |
| 306 | LIBMTP_Register_Filetype("Undefined audio file", LIBMTP_FILETYPE_UNDEF_AUDIO, PTP_OFC_MTP_UndefinedAudio,LIBMTP_new_track_t,LIBMTP_destroy_track_t,NULL); |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 307 | LIBMTP_Register_Filetype("Microsoft Windows Media Video", LIBMTP_FILETYPE_WMV, PTP_OFC_MTP_WMV,NULL,NULL,NULL); |
| 308 | LIBMTP_Register_Filetype("Audio Video Interleave", LIBMTP_FILETYPE_AVI, PTP_OFC_AVI,NULL,NULL,NULL); |
| 309 | LIBMTP_Register_Filetype("MPEG video stream", LIBMTP_FILETYPE_MPEG, PTP_OFC_MPEG,NULL,NULL,NULL); |
| 310 | LIBMTP_Register_Filetype("Microsoft Advanced Systems Format", LIBMTP_FILETYPE_ASF, PTP_OFC_ASF,NULL,NULL,NULL); |
| 311 | LIBMTP_Register_Filetype("Apple Quicktime container format", LIBMTP_FILETYPE_QT, PTP_OFC_QT,NULL,NULL,NULL); |
| 312 | LIBMTP_Register_Filetype("Undefined video file", LIBMTP_FILETYPE_UNDEF_VIDEO, PTP_OFC_MTP_UndefinedVideo,NULL,NULL,NULL); |
Linus Walleij | 83f57eb | 2006-05-31 19:57:56 +0000 | [diff] [blame] | 313 | LIBMTP_Register_Filetype("JPEG file", LIBMTP_FILETYPE_JPEG, PTP_OFC_EXIF_JPEG,NULL,NULL,NULL); |
| 314 | LIBMTP_Register_Filetype("JFIF file", LIBMTP_FILETYPE_JFIF, PTP_OFC_JFIF,NULL,NULL,NULL); |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 315 | LIBMTP_Register_Filetype("TIFF bitmap file", LIBMTP_FILETYPE_TIFF, PTP_OFC_TIFF,NULL,NULL,NULL); |
| 316 | LIBMTP_Register_Filetype("BMP bitmap file", LIBMTP_FILETYPE_BMP, PTP_OFC_BMP,NULL,NULL,NULL); |
| 317 | LIBMTP_Register_Filetype("GIF bitmap file", LIBMTP_FILETYPE_GIF, PTP_OFC_GIF,NULL,NULL,NULL); |
| 318 | LIBMTP_Register_Filetype("PICT bitmap file", LIBMTP_FILETYPE_PICT, PTP_OFC_PICT,NULL,NULL,NULL); |
| 319 | LIBMTP_Register_Filetype("Portable Network Graphics", LIBMTP_FILETYPE_PNG, PTP_OFC_PNG,NULL,NULL,NULL); |
| 320 | LIBMTP_Register_Filetype("Microsoft Windows Image Format", LIBMTP_FILETYPE_WINDOWSIMAGEFORMAT, PTP_OFC_MTP_WindowsImageFormat,NULL,NULL,NULL); |
| 321 | LIBMTP_Register_Filetype("VCalendar version 1", LIBMTP_FILETYPE_VCALENDAR1, PTP_OFC_MTP_vCalendar1,NULL,NULL,NULL); |
| 322 | LIBMTP_Register_Filetype("VCalendar version 2", LIBMTP_FILETYPE_VCALENDAR2, PTP_OFC_MTP_vCalendar2,NULL,NULL,NULL); |
| 323 | LIBMTP_Register_Filetype("VCard version 2", LIBMTP_FILETYPE_VCARD2, PTP_OFC_MTP_vCard2,NULL,NULL,NULL); |
| 324 | LIBMTP_Register_Filetype("VCard version 3", LIBMTP_FILETYPE_VCARD3, PTP_OFC_MTP_vCard3,NULL,NULL,NULL); |
| 325 | LIBMTP_Register_Filetype("Undefined Windows executable file", LIBMTP_FILETYPE_WINEXEC, PTP_OFC_MTP_UndefinedWindowsExecutable,NULL,NULL,NULL); |
| 326 | LIBMTP_Register_Filetype("Text file", LIBMTP_FILETYPE_TEXT, PTP_OFC_Text,NULL,NULL,NULL); |
| 327 | LIBMTP_Register_Filetype("HTML file", LIBMTP_FILETYPE_HTML, PTP_OFC_HTML,NULL,NULL,NULL); |
| 328 | LIBMTP_Register_Filetype("Undefined filetype", LIBMTP_FILETYPE_UNKNOWN, PTP_OFC_Undefined, NULL, NULL, NULL); |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 329 | } |
Linus Walleij | 16c51f0 | 2006-05-04 13:20:22 +0000 | [diff] [blame] | 330 | |
Linus Walleij | 16c51f0 | 2006-05-04 13:20:22 +0000 | [diff] [blame] | 331 | /** |
| 332 | * Returns the PTP filetype that maps to a certain libmtp internal file type. |
| 333 | * @param intype the MTP library interface type |
| 334 | * @return the PTP (libgphoto2) interface type |
| 335 | */ |
| 336 | static uint16_t map_libmtp_type_to_ptp_type(LIBMTP_filetype_t intype) |
| 337 | { |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 338 | LIBMTP_filemap_t *current; |
Linus Walleij | 16c51f0 | 2006-05-04 13:20:22 +0000 | [diff] [blame] | 339 | |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 340 | current = filemap; |
| 341 | |
| 342 | while (current != NULL) { |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 343 | if(current->id == intype) { |
| 344 | return current->ptp_id; |
| 345 | } |
| 346 | current = current->next; |
Linus Walleij | 16c51f0 | 2006-05-04 13:20:22 +0000 | [diff] [blame] | 347 | } |
Linus Walleij | 1fd2d27 | 2006-05-08 09:22:01 +0000 | [diff] [blame] | 348 | // printf("map_libmtp_type_to_ptp_type: unknown filetype.\n"); |
Linus Walleij | 16c51f0 | 2006-05-04 13:20:22 +0000 | [diff] [blame] | 349 | return PTP_OFC_Undefined; |
| 350 | } |
| 351 | |
| 352 | |
| 353 | /** |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 354 | * Returns the PTP internal filetype that maps to a certain libmtp |
| 355 | * interface file type. |
Linus Walleij | 8ab5426 | 2006-06-21 07:12:28 +0000 | [diff] [blame] | 356 | * @param intype the PTP (libgphoto2) interface type |
| 357 | * @return the MTP library interface type |
Linus Walleij | 16c51f0 | 2006-05-04 13:20:22 +0000 | [diff] [blame] | 358 | */ |
| 359 | static LIBMTP_filetype_t map_ptp_type_to_libmtp_type(uint16_t intype) |
| 360 | { |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 361 | LIBMTP_filemap_t *current; |
Linus Walleij | 16c51f0 | 2006-05-04 13:20:22 +0000 | [diff] [blame] | 362 | |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 363 | current = filemap; |
| 364 | |
| 365 | while (current != NULL) { |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 366 | if(current->ptp_id == intype) { |
| 367 | return current->id; |
| 368 | } |
| 369 | current = current->next; |
Linus Walleij | 16c51f0 | 2006-05-04 13:20:22 +0000 | [diff] [blame] | 370 | } |
Linus Walleij | 1fd2d27 | 2006-05-08 09:22:01 +0000 | [diff] [blame] | 371 | // printf("map_ptp_type_to_libmtp_type: unknown filetype.\n"); |
Linus Walleij | 16c51f0 | 2006-05-04 13:20:22 +0000 | [diff] [blame] | 372 | return LIBMTP_FILETYPE_UNKNOWN; |
| 373 | } |
| 374 | |
| 375 | /** |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 376 | * Returns the data function for the file type |
| 377 | * @param intype the PTP library interface |
| 378 | * @return pointer to the data function |
| 379 | */ |
| 380 | static void *get_datafunc(uint16_t intype) |
| 381 | { |
| 382 | LIBMTP_filemap_t *current; |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 383 | |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 384 | current = filemap; |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 385 | |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 386 | while (current != NULL) { |
| 387 | if(current->ptp_id == intype) { |
| 388 | return current->datafunc; |
| 389 | } |
| 390 | current = current->next; |
| 391 | } |
| 392 | return NULL; |
| 393 | } |
| 394 | |
| 395 | |
| 396 | /** |
| 397 | * Returns the constructor for that file type data |
| 398 | * @param intype the PTP library interface type |
| 399 | * @return pointer to the constructor |
| 400 | */ |
| 401 | static void *get_constructor(uint16_t intype) |
| 402 | { |
| 403 | LIBMTP_filemap_t *current; |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 404 | |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 405 | current = filemap; |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 406 | |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 407 | while (current != NULL) { |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 408 | if(current->ptp_id == intype) { |
| 409 | return current->constructor; |
| 410 | } |
| 411 | current = current->next; |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 412 | } |
| 413 | return NULL; |
| 414 | } |
| 415 | |
| 416 | /** |
| 417 | * Returns the destructor for that file type data |
| 418 | * @param intype the PTP library interface type |
| 419 | * @return pointer to the destructor |
| 420 | */ |
| 421 | static void *get_destructor(uint16_t intype) |
| 422 | { |
| 423 | LIBMTP_filemap_t *current; |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 424 | |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 425 | current = filemap; |
| 426 | |
| 427 | while (current != NULL) { |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 428 | if(current->ptp_id == intype) { |
| 429 | return current->destructor; |
| 430 | } |
| 431 | current = current->next; |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 432 | } |
| 433 | return NULL; |
| 434 | } |
| 435 | |
| 436 | /** |
Linus Walleij | 16c51f0 | 2006-05-04 13:20:22 +0000 | [diff] [blame] | 437 | * This helper function returns a textual description for a libmtp |
| 438 | * file type to be used in dialog boxes etc. |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 439 | * @param intype the libmtp internal filetype to get a description for. |
Linus Walleij | 16c51f0 | 2006-05-04 13:20:22 +0000 | [diff] [blame] | 440 | * @return a string representing the filetype, this must <b>NOT</b> |
| 441 | * be free():ed by the caller! |
| 442 | */ |
| 443 | char const * LIBMTP_Get_Filetype_Description(LIBMTP_filetype_t intype) |
| 444 | { |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 445 | LIBMTP_filemap_t *current; |
Linus Walleij | 16c51f0 | 2006-05-04 13:20:22 +0000 | [diff] [blame] | 446 | |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 447 | current = filemap; |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 448 | |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 449 | while (current != NULL) { |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 450 | if(current->id == intype) { |
| 451 | return current->description; |
| 452 | } |
| 453 | current = current->next; |
Linus Walleij | 16c51f0 | 2006-05-04 13:20:22 +0000 | [diff] [blame] | 454 | } |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 455 | |
Linus Walleij | 16c51f0 | 2006-05-04 13:20:22 +0000 | [diff] [blame] | 456 | return "Unknown filetype"; |
| 457 | } |
Linus Walleij | fa1374c | 2006-02-27 07:41:46 +0000 | [diff] [blame] | 458 | |
Linus Walleij | 6946ac5 | 2006-03-21 06:51:22 +0000 | [diff] [blame] | 459 | /** |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 460 | * Initialize the library. You are only supposed to call this |
| 461 | * one, before using the library for the first time in a program. |
| 462 | * Never re-initialize libmtp! |
| 463 | * |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 464 | * The only thing this does at the moment is to initialise the |
| 465 | * filetype mapping table. |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 466 | */ |
| 467 | void LIBMTP_Init(void) |
| 468 | { |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 469 | init_filemap(); |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 470 | return; |
| 471 | } |
| 472 | |
| 473 | /** |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 474 | * Retrieves a string from an object |
| 475 | * |
| 476 | * @param device a pointer to an MTP device. |
| 477 | * @param object_id Object reference |
| 478 | * @param attribute_id PTP attribute ID |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 479 | * @return valid string or NULL on failure. The returned string |
| 480 | * must bee <code>free()</code>:ed by the caller after |
| 481 | * use. |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 482 | */ |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 483 | char *LIBMTP_Get_String_From_Object(LIBMTP_mtpdevice_t *device, uint32_t const object_id, |
Linus Walleij | a823a70 | 2006-08-27 21:27:46 +0000 | [diff] [blame] | 484 | uint32_t const attribute_id) |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 485 | { |
| 486 | PTPPropertyValue propval; |
| 487 | char *retstring = NULL; |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 488 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 489 | uint16_t ret; |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 490 | |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 491 | if ( device == NULL || object_id == 0) { |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 492 | return NULL; |
| 493 | } |
| 494 | |
Linus Walleij | a823a70 | 2006-08-27 21:27:46 +0000 | [diff] [blame] | 495 | ret = ptp_mtp_getobjectpropvalue(params, object_id, attribute_id, &propval, PTP_DTC_STR); |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 496 | if (ret == PTP_RC_OK) { |
Linus Walleij | a823a70 | 2006-08-27 21:27:46 +0000 | [diff] [blame] | 497 | if (propval.str != NULL) { |
| 498 | retstring = (char *) strdup(propval.str); |
| 499 | free(propval.str); |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 500 | } |
| 501 | } |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 502 | |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 503 | return retstring; |
| 504 | } |
| 505 | |
| 506 | /** |
| 507 | * Retrieves an unsigned 32-bit integer from an object attribute |
| 508 | * |
| 509 | * @param device a pointer to an MTP device. |
| 510 | * @param object_id Object reference |
| 511 | * @param attribute_id PTP attribute ID |
Linus Walleij | 5acfa74 | 2006-05-29 14:51:59 +0000 | [diff] [blame] | 512 | * @param value_default Default value to return on failure |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 513 | * @return the value |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 514 | */ |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 515 | uint32_t LIBMTP_Get_U32_From_Object(LIBMTP_mtpdevice_t *device,uint32_t const object_id, |
| 516 | uint32_t const attribute_id, uint32_t const value_default) |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 517 | { |
| 518 | PTPPropertyValue propval; |
| 519 | uint32_t retval = value_default; |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 520 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 521 | uint16_t ret; |
| 522 | |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 523 | if ( device == NULL ) { |
| 524 | return value_default; |
| 525 | } |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 526 | |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 527 | ret = ptp_mtp_getobjectpropvalue(params, object_id, |
| 528 | attribute_id, |
| 529 | &propval, |
| 530 | PTP_DTC_UINT32); |
| 531 | if (ret == PTP_RC_OK) { |
| 532 | retval = propval.u32; |
| 533 | } |
| 534 | |
| 535 | return retval; |
| 536 | } |
| 537 | |
| 538 | /** |
| 539 | * Retrieves an unsigned 16-bit integer from an object attribute |
| 540 | * |
| 541 | * @param device a pointer to an MTP device. |
| 542 | * @param object_id Object reference |
| 543 | * @param attribute_id PTP attribute ID |
Linus Walleij | 5acfa74 | 2006-05-29 14:51:59 +0000 | [diff] [blame] | 544 | * @param value_default Default value to return on failure |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 545 | * @return a value |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 546 | */ |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 547 | uint16_t LIBMTP_Get_U16_From_Object(LIBMTP_mtpdevice_t *device, uint32_t const object_id, |
| 548 | uint32_t const attribute_id, uint16_t const value_default) |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 549 | { |
| 550 | PTPPropertyValue propval; |
| 551 | uint16_t retval = value_default; |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 552 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 553 | uint16_t ret; |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 554 | |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 555 | if ( device == NULL ) { |
| 556 | return value_default; |
| 557 | } |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 558 | |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 559 | ret = ptp_mtp_getobjectpropvalue(params, object_id, |
| 560 | attribute_id, |
| 561 | &propval, |
| 562 | PTP_DTC_UINT16); |
| 563 | if (ret == PTP_RC_OK) { |
| 564 | retval = propval.u16; |
| 565 | } |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 566 | |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 567 | return retval; |
| 568 | } |
| 569 | |
| 570 | /** |
| 571 | * Sets an object attribute from a string |
| 572 | * |
| 573 | * @param device a pointer to an MTP device. |
| 574 | * @param object_id Object reference |
| 575 | * @param attribute_id PTP attribute ID |
| 576 | * @param string string value to set |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 577 | * @return 0 on success, any other value means failure |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 578 | */ |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 579 | int LIBMTP_Set_Object_String(LIBMTP_mtpdevice_t *device, uint32_t const object_id, |
Linus Walleij | a823a70 | 2006-08-27 21:27:46 +0000 | [diff] [blame] | 580 | uint32_t const attribute_id, char const * const string) |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 581 | { |
| 582 | PTPPropertyValue propval; |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 583 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 584 | uint16_t ret; |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 585 | |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 586 | if (device == NULL || string == NULL) { |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 587 | return -1; |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 588 | } |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 589 | |
Linus Walleij | a823a70 | 2006-08-27 21:27:46 +0000 | [diff] [blame] | 590 | propval.str = (char *) string; |
| 591 | ret = ptp_mtp_setobjectpropvalue(params, object_id, attribute_id, &propval, PTP_DTC_STR); |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 592 | if (ret != PTP_RC_OK) { |
Linus Walleij | da9500d | 2006-08-30 13:17:06 +0000 | [diff] [blame] | 593 | printf("LIBMTP_Set_Object_String(): could not set object string.\n"); |
| 594 | printf("Return code: 0x%04x (look this up in ptp.h for an explanation).\n", ret); |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 595 | return -1; |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 596 | } |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 597 | |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 598 | return 0; |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | /** |
| 602 | * Sets an object attribute from an unsigned 32-bit integer |
| 603 | * |
| 604 | * @param device a pointer to an MTP device. |
| 605 | * @param object_id Object reference |
| 606 | * @param attribute_id PTP attribute ID |
| 607 | * @param value 32-bit unsigned integer to set |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 608 | * @return 0 on success, any other value means failure |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 609 | */ |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 610 | int LIBMTP_Set_Object_U32(LIBMTP_mtpdevice_t *device, uint32_t const object_id, |
| 611 | uint32_t const attribute_id, uint32_t const value) |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 612 | { |
| 613 | PTPPropertyValue propval; |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 614 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 615 | uint16_t ret; |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 616 | |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 617 | if (device == NULL) { |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 618 | return -1; |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 619 | } |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 620 | |
| 621 | propval.u32 = value; |
| 622 | ret = ptp_mtp_setobjectpropvalue(params, object_id, attribute_id, &propval, PTP_DTC_UINT32); |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 623 | if (ret != PTP_RC_OK) { |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 624 | return -1; |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | return 0; |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | /** |
| 631 | * Sets an object attribute from an unsigned 16-bit integer |
| 632 | * |
| 633 | * @param device a pointer to an MTP device. |
| 634 | * @param object_id Object reference |
| 635 | * @param attribute_id PTP attribute ID |
| 636 | * @param value 16-bit unsigned integer to set |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 637 | * @return 0 on success, any other value means failure |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 638 | */ |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 639 | int LIBMTP_Set_Object_U16(LIBMTP_mtpdevice_t *device, uint32_t const object_id, |
| 640 | uint32_t const attribute_id, uint16_t const value) |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 641 | { |
| 642 | PTPPropertyValue propval; |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 643 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 644 | uint16_t ret; |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 645 | |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 646 | if (device == NULL) { |
| 647 | return 1; |
| 648 | } |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 649 | |
| 650 | propval.u16 = value; |
| 651 | ret = ptp_mtp_setobjectpropvalue(params, object_id, attribute_id, &propval, PTP_DTC_UINT16); |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 652 | if (ret != PTP_RC_OK) { |
| 653 | return 1; |
| 654 | } |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 655 | |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 656 | return 0; |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | /** |
| 660 | * Gets an array of object ids associated with a specified object |
| 661 | * |
| 662 | * @param device a pointer to an MTP device. |
| 663 | * @param object_id Object reference |
| 664 | * @param items array of unsigned 32-bit integers |
| 665 | * @param len length of array |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 666 | * @return 0 on success, any other value means failure |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 667 | */ |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 668 | int LIBMTP_Get_Object_References(LIBMTP_mtpdevice_t *device, uint32_t const object_id, |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 669 | uint32_t **items, uint32_t *len) |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 670 | { |
| 671 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 672 | uint16_t ret; |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 673 | |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 674 | // A device must be attached |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 675 | if (device == NULL ) { |
Linus Walleij | f67bca9 | 2006-05-29 09:33:39 +0000 | [diff] [blame] | 676 | *items = NULL; |
| 677 | *len = 0; |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 678 | return 1; |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 679 | } |
Linus Walleij | f67bca9 | 2006-05-29 09:33:39 +0000 | [diff] [blame] | 680 | |
| 681 | ret = ptp_mtp_getobjectreferences (params, object_id, items, len); |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 682 | if (ret != PTP_RC_OK) { |
| 683 | ptp_perror(params, ret); |
| 684 | printf("LIBMTP_Get_Object_References: Could not get object references\n"); |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 685 | return 1; |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 686 | } |
| 687 | |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 688 | return 0; |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 689 | } |
| 690 | |
| 691 | /** |
| 692 | * Sets an array of object ids associated with a specified object |
| 693 | * |
| 694 | * @param device a pointer to an MTP device. |
| 695 | * @param object_id Object reference |
| 696 | * @param items array of unsigned 32-bit integers |
| 697 | * @param len length of array |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 698 | * @return 0 on success, any other value means failure |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 699 | */ |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 700 | int LIBMTP_Set_Object_References(LIBMTP_mtpdevice_t *device, uint32_t const object_id, |
| 701 | uint32_t const * const items, uint32_t const len) |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 702 | { |
| 703 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 704 | uint16_t ret; |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 705 | |
| 706 | if (device == NULL || items == NULL) { |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 707 | return 1; |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 708 | } |
Linus Walleij | f67bca9 | 2006-05-29 09:33:39 +0000 | [diff] [blame] | 709 | |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 710 | ret = ptp_mtp_setobjectreferences (params, object_id, (uint32_t *) items, len); |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 711 | if (ret != PTP_RC_OK) { |
| 712 | ptp_perror(params, ret); |
| 713 | printf("LIBMTP_Set_Object_References: Could not set object references\n"); |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 714 | return 1; |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 715 | } |
Linus Walleij | f67bca9 | 2006-05-29 09:33:39 +0000 | [diff] [blame] | 716 | |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 717 | return 0; |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 718 | } |
| 719 | |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 720 | /** |
Linus Walleij | 6fd2f08 | 2006-03-28 07:19:22 +0000 | [diff] [blame] | 721 | * Get a list of the supported devices. |
| 722 | * |
Linus Walleij | c86afbd | 2006-05-04 19:05:24 +0000 | [diff] [blame] | 723 | * The developers depend on users of this library to constantly |
| 724 | * add in to the list of supported devices. What we need is the |
| 725 | * device name, USB Vendor ID (VID) and USB Product ID (PID). |
| 726 | * put this into a bug ticket at the project homepage, please. |
| 727 | * The VID/PID is used to let e.g. udev lift the device to |
| 728 | * console userspace access when it's plugged in. |
| 729 | * |
Linus Walleij | 6fd2f08 | 2006-03-28 07:19:22 +0000 | [diff] [blame] | 730 | * @param devices a pointer to a pointer that will hold a device |
| 731 | * list after the call to this function, if it was |
| 732 | * successful. |
| 733 | * @param numdevs a pointer to an integer that will hold the number |
| 734 | * of devices in the device list if the call was successful. |
| 735 | * @return 0 if the list was successfull retrieved, any other |
| 736 | * value means failure. |
| 737 | */ |
| 738 | int LIBMTP_Get_Supported_Devices_List(LIBMTP_device_entry_t ** const devices, int * const numdevs) |
| 739 | { |
| 740 | // Just dispatch to libusb glue file... |
| 741 | return get_device_list(devices, numdevs); |
| 742 | } |
| 743 | |
| 744 | /** |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 745 | * Get the first connected MTP device. There is currently no API for |
| 746 | * retrieveing multiple devices. |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 747 | * @return a device pointer. |
| 748 | */ |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 749 | LIBMTP_mtpdevice_t *LIBMTP_Get_First_Device(void) |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 750 | { |
| 751 | uint8_t interface_number; |
Linus Walleij | 56d3e18 | 2006-02-10 15:46:54 +0000 | [diff] [blame] | 752 | PTPParams *params; |
| 753 | PTP_USB *ptp_usb; |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 754 | PTPStorageIDs storageIDs; |
| 755 | unsigned storageID = 0; |
| 756 | PTPDevicePropDesc dpd; |
Linus Walleij | d31e619 | 2006-09-12 07:55:27 +0000 | [diff] [blame] | 757 | uint8_t batteryLevelMax = 100; // Some default |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 758 | uint16_t ret; |
Linus Walleij | 05ccbe7 | 2006-06-13 07:46:58 +0000 | [diff] [blame] | 759 | uint32_t i; |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 760 | LIBMTP_mtpdevice_t *tmpdevice; |
Richard Low | 43fdb88 | 2006-09-06 16:19:05 +0000 | [diff] [blame] | 761 | uint8_t remaining_directories; |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 762 | |
Linus Walleij | 56d3e18 | 2006-02-10 15:46:54 +0000 | [diff] [blame] | 763 | // Allocate a parameter block |
| 764 | params = (PTPParams *) malloc(sizeof(PTPParams)); |
Linus Walleij | d5d51c8 | 2006-09-11 06:57:50 +0000 | [diff] [blame] | 765 | params->cd_locale_to_ucs2 = iconv_open(UCS_2_INTERNAL, "UTF-8"); |
| 766 | params->cd_ucs2_to_locale = iconv_open("UTF-8", UCS_2_INTERNAL); |
Linus Walleij | a823a70 | 2006-08-27 21:27:46 +0000 | [diff] [blame] | 767 | if (params->cd_locale_to_ucs2 == (iconv_t) -1 || params->cd_ucs2_to_locale == (iconv_t) -1) { |
Linus Walleij | d5d51c8 | 2006-09-11 06:57:50 +0000 | [diff] [blame] | 768 | printf("LIBMTP panic: could not open iconv() converters to/from UCS-2!\n"); |
Linus Walleij | a823a70 | 2006-08-27 21:27:46 +0000 | [diff] [blame] | 769 | return NULL; |
| 770 | } |
| 771 | |
Linus Walleij | 56d3e18 | 2006-02-10 15:46:54 +0000 | [diff] [blame] | 772 | ptp_usb = (PTP_USB *) malloc(sizeof(PTP_USB)); |
Linus Walleij | d214b9b | 2006-08-26 22:08:37 +0000 | [diff] [blame] | 773 | // Callbacks and stuff |
| 774 | ptp_usb->callback_active = 0; |
| 775 | ptp_usb->current_transfer_total = 0; |
| 776 | ptp_usb->current_transfer_complete = 0; |
| 777 | ptp_usb->current_transfer_callback = NULL; |
| 778 | |
| 779 | // get storage ID |
Linus Walleij | 56d3e18 | 2006-02-10 15:46:54 +0000 | [diff] [blame] | 780 | ret = connect_first_device(params, ptp_usb, &interface_number); |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 781 | |
| 782 | switch (ret) |
| 783 | { |
| 784 | case PTP_CD_RC_CONNECTED: |
| 785 | printf("Connected to MTP device.\n"); |
| 786 | break; |
| 787 | case PTP_CD_RC_NO_DEVICES: |
| 788 | printf("No MTP devices.\n"); |
| 789 | return NULL; |
| 790 | case PTP_CD_RC_ERROR_CONNECTING: |
| 791 | printf("Connection error.\n"); |
| 792 | return NULL; |
| 793 | } |
| 794 | |
| 795 | // get storage ID |
Linus Walleij | 56d3e18 | 2006-02-10 15:46:54 +0000 | [diff] [blame] | 796 | if (ptp_getstorageids (params, &storageIDs) == PTP_RC_OK) { |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 797 | if (storageIDs.n > 0) |
| 798 | storageID = storageIDs.Storage[0]; |
| 799 | free(storageIDs.Storage); |
| 800 | } |
| 801 | |
| 802 | // Make sure there are no handlers |
Linus Walleij | 56d3e18 | 2006-02-10 15:46:54 +0000 | [diff] [blame] | 803 | params->handles.Handler = NULL; |
Linus Walleij | b02a066 | 2006-04-25 08:05:09 +0000 | [diff] [blame] | 804 | |
Linus Walleij | 8c45b29 | 2006-04-26 14:12:44 +0000 | [diff] [blame] | 805 | // Just cache the device information for any later use. |
| 806 | if (ptp_getdeviceinfo(params, ¶ms->deviceinfo) != PTP_RC_OK) { |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 807 | goto error_handler; |
| 808 | } |
| 809 | |
| 810 | // Get battery maximum level |
Linus Walleij | d31e619 | 2006-09-12 07:55:27 +0000 | [diff] [blame] | 811 | if (ptp_property_issupported(params, PTP_DPC_BatteryLevel)) { |
| 812 | if (ptp_getdevicepropdesc(params, PTP_DPC_BatteryLevel, &dpd) != PTP_RC_OK) { |
| 813 | printf("LIBMTP_Get_First_Device(): Unable to retrieve battery max level.\n"); |
| 814 | goto error_handler; |
| 815 | } |
| 816 | // if is NULL, just leave as default |
| 817 | if (dpd.FORM.Range.MaximumValue.u8 != 0) { |
| 818 | batteryLevelMax = dpd.FORM.Range.MaximumValue.u8; |
| 819 | } |
| 820 | ptp_free_devicepropdesc(&dpd); |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 821 | } |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 822 | |
| 823 | // OK everything got this far, so it is time to create a device struct! |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 824 | tmpdevice = (LIBMTP_mtpdevice_t *) malloc(sizeof(LIBMTP_mtpdevice_t)); |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 825 | tmpdevice->interface_number = interface_number; |
Linus Walleij | 9b28da3 | 2006-03-16 13:47:58 +0000 | [diff] [blame] | 826 | tmpdevice->params = (void *) params; |
Linus Walleij | 2d411db | 2006-03-22 12:13:09 +0000 | [diff] [blame] | 827 | tmpdevice->usbinfo = (void *) ptp_usb; |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 828 | tmpdevice->storage_id = storageID; |
| 829 | tmpdevice->maximum_battery_level = batteryLevelMax; |
| 830 | |
Linus Walleij | 05ccbe7 | 2006-06-13 07:46:58 +0000 | [diff] [blame] | 831 | // Set all default folders to 0 == root directory |
| 832 | tmpdevice->default_music_folder = 0; |
| 833 | tmpdevice->default_playlist_folder = 0; |
| 834 | tmpdevice->default_picture_folder = 0; |
| 835 | tmpdevice->default_video_folder = 0; |
| 836 | tmpdevice->default_organizer_folder = 0; |
| 837 | tmpdevice->default_zencast_folder = 0; |
| 838 | |
| 839 | /* |
| 840 | * Then get the handles and try to locate the default folders. |
| 841 | * This has the desired side effect of cacheing all handles from |
| 842 | * the device which speeds up later operations. |
| 843 | */ |
| 844 | flush_handles(tmpdevice); |
Linus Walleij | 0558ac5 | 2006-09-07 06:55:03 +0000 | [diff] [blame] | 845 | /* |
| 846 | * Remaining directories to get the handles to. |
| 847 | * We can stop when done this to save time |
| 848 | */ |
| 849 | remaining_directories = 6; |
Richard Low | 43fdb88 | 2006-09-06 16:19:05 +0000 | [diff] [blame] | 850 | for (i = 0; i < params->handles.n && remaining_directories > 0; i++) { |
Linus Walleij | 05ccbe7 | 2006-06-13 07:46:58 +0000 | [diff] [blame] | 851 | PTPObjectInfo oi; |
| 852 | if (ptp_getobjectinfo(params, params->handles.Handler[i], &oi) == PTP_RC_OK) { |
| 853 | // Ignore non-folders |
| 854 | if ( oi.ObjectFormat != PTP_OFC_Association ) |
| 855 | continue; |
Linus Walleij | 97c7a34 | 2006-09-11 07:03:03 +0000 | [diff] [blame] | 856 | if ( oi.Filename == NULL) |
| 857 | continue; |
Linus Walleij | 05ccbe7 | 2006-06-13 07:46:58 +0000 | [diff] [blame] | 858 | if (!strcmp(oi.Filename, "Music")) { |
| 859 | tmpdevice->default_music_folder = params->handles.Handler[i]; |
Linus Walleij | 0558ac5 | 2006-09-07 06:55:03 +0000 | [diff] [blame] | 860 | remaining_directories--; |
Linus Walleij | 05ccbe7 | 2006-06-13 07:46:58 +0000 | [diff] [blame] | 861 | continue; |
| 862 | } else if (!strcmp(oi.Filename, "My Playlists")) { |
| 863 | tmpdevice->default_playlist_folder = params->handles.Handler[i]; |
Richard Low | 43fdb88 | 2006-09-06 16:19:05 +0000 | [diff] [blame] | 864 | remaining_directories--; |
Linus Walleij | 05ccbe7 | 2006-06-13 07:46:58 +0000 | [diff] [blame] | 865 | continue; |
| 866 | } else if (!strcmp(oi.Filename, "Pictures")) { |
| 867 | tmpdevice->default_picture_folder = params->handles.Handler[i]; |
Richard Low | 43fdb88 | 2006-09-06 16:19:05 +0000 | [diff] [blame] | 868 | remaining_directories--; |
Linus Walleij | 05ccbe7 | 2006-06-13 07:46:58 +0000 | [diff] [blame] | 869 | continue; |
| 870 | } else if (!strcmp(oi.Filename, "Video")) { |
| 871 | tmpdevice->default_video_folder = params->handles.Handler[i]; |
Richard Low | 43fdb88 | 2006-09-06 16:19:05 +0000 | [diff] [blame] | 872 | remaining_directories--; |
Linus Walleij | 05ccbe7 | 2006-06-13 07:46:58 +0000 | [diff] [blame] | 873 | continue; |
| 874 | } else if (!strcmp(oi.Filename, "My Organizer")) { |
| 875 | tmpdevice->default_organizer_folder = params->handles.Handler[i]; |
Richard Low | 43fdb88 | 2006-09-06 16:19:05 +0000 | [diff] [blame] | 876 | remaining_directories--; |
Linus Walleij | 05ccbe7 | 2006-06-13 07:46:58 +0000 | [diff] [blame] | 877 | continue; |
| 878 | } else if (!strcmp(oi.Filename, "ZENcast")) { |
| 879 | tmpdevice->default_zencast_folder = params->handles.Handler[i]; |
Richard Low | 43fdb88 | 2006-09-06 16:19:05 +0000 | [diff] [blame] | 880 | remaining_directories--; |
Linus Walleij | 05ccbe7 | 2006-06-13 07:46:58 +0000 | [diff] [blame] | 881 | continue; |
| 882 | } |
| 883 | } else { |
| 884 | printf("LIBMTP panic: Found a bad handle, trying to ignore it.\n"); |
| 885 | } |
| 886 | } |
| 887 | |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 888 | return tmpdevice; |
| 889 | |
| 890 | // Then close it again. |
| 891 | error_handler: |
Linus Walleij | 56d3e18 | 2006-02-10 15:46:54 +0000 | [diff] [blame] | 892 | close_device(ptp_usb, params, interface_number); |
Linus Walleij | b02a066 | 2006-04-25 08:05:09 +0000 | [diff] [blame] | 893 | // TODO: libgphoto2 does not seem to be able to free the deviceinfo |
| 894 | // ptp_free_deviceinfo(¶ms->deviceinfo); |
Linus Walleij | 56d3e18 | 2006-02-10 15:46:54 +0000 | [diff] [blame] | 895 | if (params->handles.Handler != NULL) { |
| 896 | free(params->handles.Handler); |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 897 | } |
| 898 | return NULL; |
| 899 | } |
| 900 | |
| 901 | /** |
| 902 | * This closes and releases an allocated MTP device. |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 903 | * @param device a pointer to the MTP device to release. |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 904 | */ |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 905 | void LIBMTP_Release_Device(LIBMTP_mtpdevice_t *device) |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 906 | { |
Linus Walleij | 9b28da3 | 2006-03-16 13:47:58 +0000 | [diff] [blame] | 907 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | 2d411db | 2006-03-22 12:13:09 +0000 | [diff] [blame] | 908 | PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo; |
Linus Walleij | 9b28da3 | 2006-03-16 13:47:58 +0000 | [diff] [blame] | 909 | |
Linus Walleij | 2d411db | 2006-03-22 12:13:09 +0000 | [diff] [blame] | 910 | close_device(ptp_usb, params, device->interface_number); |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 911 | // Free the device info and any handler |
Linus Walleij | b02a066 | 2006-04-25 08:05:09 +0000 | [diff] [blame] | 912 | // TODO: libgphoto2 does not seem to be able to free the deviceinfo |
| 913 | // ptp_free_deviceinfo(¶ms->deviceinfo); |
Linus Walleij | 9b28da3 | 2006-03-16 13:47:58 +0000 | [diff] [blame] | 914 | if (params->handles.Handler != NULL) { |
| 915 | free(params->handles.Handler); |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 916 | params->handles.Handler = NULL; |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 917 | } |
Linus Walleij | 3ec8631 | 2006-08-21 13:25:24 +0000 | [diff] [blame] | 918 | // Free iconv() converters... |
Linus Walleij | a823a70 | 2006-08-27 21:27:46 +0000 | [diff] [blame] | 919 | iconv_close(params->cd_locale_to_ucs2); |
| 920 | iconv_close(params->cd_ucs2_to_locale); |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 921 | free(device); |
| 922 | } |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 923 | |
| 924 | /** |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 925 | * This function refresh the internal handle list whenever |
| 926 | * the items stored inside the device is altered. On operations |
| 927 | * that do not add or remove objects, this is typically not |
| 928 | * called. |
| 929 | * @param device a pointer to the MTP device to flush handles for. |
| 930 | */ |
| 931 | static void flush_handles(LIBMTP_mtpdevice_t *device) |
| 932 | { |
| 933 | PTPParams *params = (PTPParams *) device->params; |
| 934 | uint16_t ret; |
| 935 | |
| 936 | if (params->handles.Handler != NULL) { |
| 937 | free(params->handles.Handler); |
| 938 | } |
| 939 | |
| 940 | // Get all the handles if we haven't already done that |
| 941 | ret = ptp_getobjecthandles(params, |
| 942 | PTP_GOH_ALL_STORAGE, |
| 943 | PTP_GOH_ALL_FORMATS, |
| 944 | PTP_GOH_ALL_ASSOCS, |
| 945 | ¶ms->handles); |
| 946 | if (ret != PTP_RC_OK) { |
| 947 | printf("flush_handles(): LIBMTP panic: Could not get object handles...\n"); |
Linus Walleij | da9500d | 2006-08-30 13:17:06 +0000 | [diff] [blame] | 948 | printf("Return code: 0x%04x (look this up in ptp.h for an explanation).\n", ret); |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 949 | } |
| 950 | |
| 951 | return; |
| 952 | } |
| 953 | |
| 954 | /** |
Linus Walleij | 8c45b29 | 2006-04-26 14:12:44 +0000 | [diff] [blame] | 955 | * This function dumps out a large chunk of textual information |
| 956 | * provided from the PTP protocol and additionally some extra |
| 957 | * MTP-specific information where applicable. |
| 958 | * @param device a pointer to the MTP device to report info from. |
| 959 | */ |
| 960 | void LIBMTP_Dump_Device_Info(LIBMTP_mtpdevice_t *device) |
| 961 | { |
| 962 | int i; |
| 963 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | c6210fb | 2006-05-08 11:11:41 +0000 | [diff] [blame] | 964 | PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo; |
| 965 | |
| 966 | printf("USB low-level info:\n"); |
| 967 | dump_usbinfo(ptp_usb); |
Linus Walleij | 8c45b29 | 2006-04-26 14:12:44 +0000 | [diff] [blame] | 968 | /* Print out some verbose information */ |
| 969 | printf("Device info:\n"); |
| 970 | printf(" Manufacturer: %s\n", params->deviceinfo.Manufacturer); |
| 971 | printf(" Model: %s\n", params->deviceinfo.Model); |
| 972 | printf(" Device version: %s\n", params->deviceinfo.DeviceVersion); |
| 973 | printf(" Serial number: %s\n", params->deviceinfo.SerialNumber); |
| 974 | printf(" Vendor extension ID: 0x%08x\n", params->deviceinfo.VendorExtensionID); |
| 975 | printf(" Vendor extension description: %s\n", params->deviceinfo.VendorExtensionDesc); |
| 976 | printf("Supported operations:\n"); |
| 977 | for (i=0;i<params->deviceinfo.OperationsSupported_len;i++) { |
| 978 | printf(" 0x%04x\n", params->deviceinfo.OperationsSupported[i]); |
| 979 | } |
| 980 | printf("Events supported:\n"); |
| 981 | if (params->deviceinfo.EventsSupported_len == 0) { |
| 982 | printf(" None.\n"); |
| 983 | } else { |
| 984 | for (i=0;i<params->deviceinfo.EventsSupported_len;i++) { |
| 985 | printf(" 0x%04x\n", params->deviceinfo.EventsSupported[i]); |
| 986 | } |
| 987 | } |
| 988 | printf("Device Properties Supported:\n"); |
| 989 | for (i=0;i<params->deviceinfo.DevicePropertiesSupported_len;i++) { |
Linus Walleij | 16c51f0 | 2006-05-04 13:20:22 +0000 | [diff] [blame] | 990 | char const *propdesc = ptp_get_property_description(params, params->deviceinfo.DevicePropertiesSupported[i]); |
Linus Walleij | 545c779 | 2006-06-13 15:22:30 +0000 | [diff] [blame] | 991 | |
| 992 | if (propdesc != NULL) { |
| 993 | printf(" 0x%04x: %s\n", params->deviceinfo.DevicePropertiesSupported[i], propdesc); |
| 994 | } else { |
| 995 | uint16_t prop = params->deviceinfo.DevicePropertiesSupported[i]; |
Linus Walleij | cf223e6 | 2006-06-19 09:31:53 +0000 | [diff] [blame] | 996 | printf(" 0x%04x: Unknown property\n", prop); |
Linus Walleij | 545c779 | 2006-06-13 15:22:30 +0000 | [diff] [blame] | 997 | } |
Linus Walleij | 8c45b29 | 2006-04-26 14:12:44 +0000 | [diff] [blame] | 998 | } |
Linus Walleij | 0af979a | 2006-06-19 11:49:10 +0000 | [diff] [blame] | 999 | |
| 1000 | if (ptp_operation_issupported(params,PTP_OC_MTP_GetObjectPropsSupported)) { |
| 1001 | printf("Playable File (Object) Types and Object Properties Supported:\n"); |
| 1002 | for (i=0;i<params->deviceinfo.ImageFormats_len;i++) { |
| 1003 | char txt[256]; |
| 1004 | uint16_t ret; |
| 1005 | uint16_t *props = NULL; |
| 1006 | uint32_t propcnt = 0; |
| 1007 | int j; |
| 1008 | |
| 1009 | (void) ptp_render_ofc (params, params->deviceinfo.ImageFormats[i], sizeof(txt), txt); |
| 1010 | printf(" %04x: %s\n", params->deviceinfo.ImageFormats[i], txt); |
| 1011 | |
| 1012 | ret = ptp_mtp_getobjectpropssupported (params, params->deviceinfo.ImageFormats[i], &propcnt, &props); |
| 1013 | if (ret != PTP_RC_OK) { |
| 1014 | printf(" Error on query for object properties.\n"); |
Linus Walleij | da9500d | 2006-08-30 13:17:06 +0000 | [diff] [blame] | 1015 | printf(" Return code: 0x%04x (look this up in ptp.h for an explanation).\n", ret); |
Linus Walleij | 0af979a | 2006-06-19 11:49:10 +0000 | [diff] [blame] | 1016 | } else { |
| 1017 | for (j=0;j<propcnt;j++) { |
| 1018 | (void) ptp_render_mtp_propname(props[j],sizeof(txt),txt); |
| 1019 | printf(" %04x: %s\n", props[j], txt); |
| 1020 | } |
| 1021 | free(props); |
| 1022 | } |
| 1023 | } |
| 1024 | } |
Linus Walleij | 545c779 | 2006-06-13 15:22:30 +0000 | [diff] [blame] | 1025 | |
| 1026 | printf("Special directories:\n"); |
| 1027 | printf(" Default music folder: 0x%08x\n", device->default_music_folder); |
| 1028 | printf(" Default playlist folder: 0x%08x\n", device->default_playlist_folder); |
| 1029 | printf(" Default picture folder: 0x%08x\n", device->default_picture_folder); |
| 1030 | printf(" Default video folder: 0x%08x\n", device->default_video_folder); |
| 1031 | printf(" Default organizer folder: 0x%08x\n", device->default_organizer_folder); |
| 1032 | printf(" Default zencast folder: 0x%08x\n", device->default_zencast_folder); |
Linus Walleij | 8c45b29 | 2006-04-26 14:12:44 +0000 | [diff] [blame] | 1033 | } |
| 1034 | |
| 1035 | /** |
Linus Walleij | 8012406 | 2006-03-15 10:26:09 +0000 | [diff] [blame] | 1036 | * This retrieves the model name (often equal to product name) |
| 1037 | * of an MTP device. |
| 1038 | * @param device a pointer to the device to get the model name for. |
| 1039 | * @return a newly allocated UTF-8 string representing the model name. |
| 1040 | * The string must be freed by the caller after use. If the call |
| 1041 | * was unsuccessful this will contain NULL. |
| 1042 | */ |
| 1043 | char *LIBMTP_Get_Modelname(LIBMTP_mtpdevice_t *device) |
| 1044 | { |
| 1045 | char *retmodel = NULL; |
Linus Walleij | 9b28da3 | 2006-03-16 13:47:58 +0000 | [diff] [blame] | 1046 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | 8012406 | 2006-03-15 10:26:09 +0000 | [diff] [blame] | 1047 | |
Linus Walleij | 9b28da3 | 2006-03-16 13:47:58 +0000 | [diff] [blame] | 1048 | if (params->deviceinfo.Model != NULL) { |
| 1049 | retmodel = strdup(params->deviceinfo.Model); |
Linus Walleij | 8012406 | 2006-03-15 10:26:09 +0000 | [diff] [blame] | 1050 | } |
| 1051 | return retmodel; |
| 1052 | } |
| 1053 | |
| 1054 | /** |
| 1055 | * This retrieves the serial number of an MTP device. |
| 1056 | * @param device a pointer to the device to get the serial number for. |
| 1057 | * @return a newly allocated UTF-8 string representing the serial number. |
| 1058 | * The string must be freed by the caller after use. If the call |
| 1059 | * was unsuccessful this will contain NULL. |
| 1060 | */ |
| 1061 | char *LIBMTP_Get_Serialnumber(LIBMTP_mtpdevice_t *device) |
| 1062 | { |
| 1063 | char *retnumber = NULL; |
Linus Walleij | 9b28da3 | 2006-03-16 13:47:58 +0000 | [diff] [blame] | 1064 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | 8012406 | 2006-03-15 10:26:09 +0000 | [diff] [blame] | 1065 | |
Linus Walleij | 9b28da3 | 2006-03-16 13:47:58 +0000 | [diff] [blame] | 1066 | if (params->deviceinfo.SerialNumber != NULL) { |
| 1067 | retnumber = strdup(params->deviceinfo.SerialNumber); |
Linus Walleij | 8012406 | 2006-03-15 10:26:09 +0000 | [diff] [blame] | 1068 | } |
| 1069 | return retnumber; |
| 1070 | } |
| 1071 | |
| 1072 | /** |
| 1073 | * This retrieves the device version (hardware and firmware version) of an |
| 1074 | * MTP device. |
| 1075 | * @param device a pointer to the device to get the device version for. |
| 1076 | * @return a newly allocated UTF-8 string representing the device version. |
| 1077 | * The string must be freed by the caller after use. If the call |
| 1078 | * was unsuccessful this will contain NULL. |
| 1079 | */ |
| 1080 | char *LIBMTP_Get_Deviceversion(LIBMTP_mtpdevice_t *device) |
| 1081 | { |
| 1082 | char *retversion = NULL; |
Linus Walleij | 9b28da3 | 2006-03-16 13:47:58 +0000 | [diff] [blame] | 1083 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | 8012406 | 2006-03-15 10:26:09 +0000 | [diff] [blame] | 1084 | |
Linus Walleij | 9b28da3 | 2006-03-16 13:47:58 +0000 | [diff] [blame] | 1085 | if (params->deviceinfo.DeviceVersion != NULL) { |
| 1086 | retversion = strdup(params->deviceinfo.DeviceVersion); |
Linus Walleij | 8012406 | 2006-03-15 10:26:09 +0000 | [diff] [blame] | 1087 | } |
| 1088 | return retversion; |
| 1089 | } |
| 1090 | |
| 1091 | |
| 1092 | /** |
Linus Walleij | fae2748 | 2006-08-19 20:13:25 +0000 | [diff] [blame] | 1093 | * This retrieves the "friendly name" of an MTP device. Usually |
| 1094 | * this is simply the name of the owner or something like |
Linus Walleij | 3065879 | 2006-08-19 22:18:55 +0000 | [diff] [blame] | 1095 | * "John Doe's Digital Audio Player". This property should be supported |
Linus Walleij | fae2748 | 2006-08-19 20:13:25 +0000 | [diff] [blame] | 1096 | * by all MTP devices. |
| 1097 | * @param device a pointer to the device to get the friendly name for. |
| 1098 | * @return a newly allocated UTF-8 string representing the friendly name. |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 1099 | * The string must be freed by the caller after use. |
Linus Walleij | 3065879 | 2006-08-19 22:18:55 +0000 | [diff] [blame] | 1100 | * @see LIBMTP_Set_Friendlyname() |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 1101 | */ |
Linus Walleij | 3065879 | 2006-08-19 22:18:55 +0000 | [diff] [blame] | 1102 | char *LIBMTP_Get_Friendlyname(LIBMTP_mtpdevice_t *device) |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 1103 | { |
Linus Walleij | b02a066 | 2006-04-25 08:05:09 +0000 | [diff] [blame] | 1104 | PTPPropertyValue propval; |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 1105 | char *retstring = NULL; |
Linus Walleij | 9b28da3 | 2006-03-16 13:47:58 +0000 | [diff] [blame] | 1106 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 1107 | |
Linus Walleij | cf223e6 | 2006-06-19 09:31:53 +0000 | [diff] [blame] | 1108 | if (!ptp_property_issupported(params, PTP_DPC_MTP_DeviceFriendlyName)) { |
| 1109 | return NULL; |
| 1110 | } |
| 1111 | |
Linus Walleij | 9b28da3 | 2006-03-16 13:47:58 +0000 | [diff] [blame] | 1112 | if (ptp_getdevicepropvalue(params, |
Linus Walleij | 545c779 | 2006-06-13 15:22:30 +0000 | [diff] [blame] | 1113 | PTP_DPC_MTP_DeviceFriendlyName, |
Linus Walleij | b02a066 | 2006-04-25 08:05:09 +0000 | [diff] [blame] | 1114 | &propval, |
Linus Walleij | a823a70 | 2006-08-27 21:27:46 +0000 | [diff] [blame] | 1115 | PTP_DTC_STR) != PTP_RC_OK) { |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 1116 | return NULL; |
| 1117 | } |
Linus Walleij | a823a70 | 2006-08-27 21:27:46 +0000 | [diff] [blame] | 1118 | if (propval.str != NULL) { |
| 1119 | retstring = strdup(propval.str); |
| 1120 | free(propval.str); |
| 1121 | } |
Linus Walleij | fae2748 | 2006-08-19 20:13:25 +0000 | [diff] [blame] | 1122 | return retstring; |
| 1123 | } |
| 1124 | |
| 1125 | /** |
Linus Walleij | 3065879 | 2006-08-19 22:18:55 +0000 | [diff] [blame] | 1126 | * Sets the "friendly name" of an MTP device. |
| 1127 | * @param device a pointer to the device to set the friendly name for. |
| 1128 | * @param friendlyname the new friendly name for the device. |
| 1129 | * @return 0 on success, any other value means failure. |
| 1130 | * @see LIBMTP_Get_Ownername() |
| 1131 | */ |
| 1132 | int LIBMTP_Set_Friendlyname(LIBMTP_mtpdevice_t *device, |
| 1133 | char const * const friendlyname) |
| 1134 | { |
| 1135 | PTPPropertyValue propval; |
| 1136 | PTPParams *params = (PTPParams *) device->params; |
| 1137 | |
| 1138 | if (!ptp_property_issupported(params, PTP_DPC_MTP_DeviceFriendlyName)) { |
| 1139 | return -1; |
| 1140 | } |
Linus Walleij | a823a70 | 2006-08-27 21:27:46 +0000 | [diff] [blame] | 1141 | propval.str = (char *) friendlyname; |
Linus Walleij | 3065879 | 2006-08-19 22:18:55 +0000 | [diff] [blame] | 1142 | if (ptp_setdevicepropvalue(params, |
| 1143 | PTP_DPC_MTP_DeviceFriendlyName, |
| 1144 | &propval, |
Linus Walleij | a823a70 | 2006-08-27 21:27:46 +0000 | [diff] [blame] | 1145 | PTP_DTC_STR) != PTP_RC_OK) { |
Linus Walleij | 3065879 | 2006-08-19 22:18:55 +0000 | [diff] [blame] | 1146 | return -1; |
| 1147 | } |
Linus Walleij | 3065879 | 2006-08-19 22:18:55 +0000 | [diff] [blame] | 1148 | return 0; |
| 1149 | } |
| 1150 | |
| 1151 | /** |
Linus Walleij | fae2748 | 2006-08-19 20:13:25 +0000 | [diff] [blame] | 1152 | * This retrieves the syncronization partner of an MTP device. This |
| 1153 | * property should be supported by all MTP devices. |
| 1154 | * @param device a pointer to the device to get the sync partner for. |
| 1155 | * @return a newly allocated UTF-8 string representing the synchronization |
| 1156 | * partner. The string must be freed by the caller after use. |
Linus Walleij | 3065879 | 2006-08-19 22:18:55 +0000 | [diff] [blame] | 1157 | * @see LIBMTP_Set_Syncpartner() |
Linus Walleij | fae2748 | 2006-08-19 20:13:25 +0000 | [diff] [blame] | 1158 | */ |
| 1159 | char *LIBMTP_Get_Syncpartner(LIBMTP_mtpdevice_t *device) |
| 1160 | { |
| 1161 | PTPPropertyValue propval; |
| 1162 | char *retstring = NULL; |
| 1163 | PTPParams *params = (PTPParams *) device->params; |
| 1164 | |
| 1165 | if (!ptp_property_issupported(params, PTP_DPC_MTP_SynchronizationPartner)) { |
| 1166 | return NULL; |
| 1167 | } |
| 1168 | |
| 1169 | if (ptp_getdevicepropvalue(params, |
| 1170 | PTP_DPC_MTP_SynchronizationPartner, |
| 1171 | &propval, |
Linus Walleij | a823a70 | 2006-08-27 21:27:46 +0000 | [diff] [blame] | 1172 | PTP_DTC_STR) != PTP_RC_OK) { |
Linus Walleij | fae2748 | 2006-08-19 20:13:25 +0000 | [diff] [blame] | 1173 | return NULL; |
| 1174 | } |
Linus Walleij | a823a70 | 2006-08-27 21:27:46 +0000 | [diff] [blame] | 1175 | if (propval.str != NULL) { |
| 1176 | retstring = strdup(propval.str); |
| 1177 | free(propval.str); |
| 1178 | } |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 1179 | return retstring; |
| 1180 | } |
| 1181 | |
Linus Walleij | 3065879 | 2006-08-19 22:18:55 +0000 | [diff] [blame] | 1182 | |
| 1183 | /** |
| 1184 | * Sets the synchronization partner of an MTP device. Note that |
| 1185 | * we have no idea what the effect of setting this to "foobar" |
| 1186 | * may be. But the general idea seems to be to tell which program |
| 1187 | * shall synchronize with this device and tell others to leave |
| 1188 | * it alone. |
| 1189 | * @param device a pointer to the device to set the sync partner for. |
| 1190 | * @param syncpartner the new synchronization partner for the device. |
| 1191 | * @return 0 on success, any other value means failure. |
| 1192 | * @see LIBMTP_Get_Syncpartner() |
| 1193 | */ |
| 1194 | int LIBMTP_Set_Syncpartner(LIBMTP_mtpdevice_t *device, |
| 1195 | char const * const syncpartner) |
| 1196 | { |
| 1197 | PTPPropertyValue propval; |
| 1198 | PTPParams *params = (PTPParams *) device->params; |
| 1199 | |
| 1200 | if (!ptp_property_issupported(params, PTP_DPC_MTP_SynchronizationPartner)) { |
| 1201 | return -1; |
| 1202 | } |
Linus Walleij | a823a70 | 2006-08-27 21:27:46 +0000 | [diff] [blame] | 1203 | propval.str = (char *) syncpartner; |
Linus Walleij | 3065879 | 2006-08-19 22:18:55 +0000 | [diff] [blame] | 1204 | if (ptp_setdevicepropvalue(params, |
| 1205 | PTP_DPC_MTP_SynchronizationPartner, |
| 1206 | &propval, |
Linus Walleij | a823a70 | 2006-08-27 21:27:46 +0000 | [diff] [blame] | 1207 | PTP_DTC_STR) != PTP_RC_OK) { |
Linus Walleij | 3065879 | 2006-08-19 22:18:55 +0000 | [diff] [blame] | 1208 | return -1; |
| 1209 | } |
Linus Walleij | 3065879 | 2006-08-19 22:18:55 +0000 | [diff] [blame] | 1210 | return 0; |
| 1211 | } |
| 1212 | |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 1213 | /** |
Linus Walleij | fa1374c | 2006-02-27 07:41:46 +0000 | [diff] [blame] | 1214 | * This function finds out how much storage space is currently used |
| 1215 | * and any additional storage information. Storage may be a hard disk |
| 1216 | * or flash memory or whatever. |
| 1217 | * @param device a pointer to the device to get the storage info for. |
| 1218 | * @param total a pointer to a variable that will hold the |
| 1219 | * total the total number of bytes available on this volume after |
| 1220 | * the call. |
| 1221 | * @param free a pointer to a variable that will hold the number of |
| 1222 | * free bytes available on this volume right now after the call. |
| 1223 | * @param storage_description a description of the storage. This may |
| 1224 | * be NULL after the call even if it succeeded. If it is not NULL, |
| 1225 | * it must be freed by the callee after use. |
| 1226 | * @param volume_label a volume label or similar. This may be NULL after the |
| 1227 | * call even if it succeeded. If it is not NULL, it must be |
| 1228 | * freed by the callee after use. |
| 1229 | * @return 0 if the storage info was successfully retrieved, any other |
| 1230 | * value means failure. |
| 1231 | */ |
| 1232 | int LIBMTP_Get_Storageinfo(LIBMTP_mtpdevice_t *device, uint64_t * const total, |
| 1233 | uint64_t * const free, char ** const storage_description, |
| 1234 | char ** const volume_label) |
| 1235 | { |
| 1236 | PTPStorageInfo storageInfo; |
Linus Walleij | 9b28da3 | 2006-03-16 13:47:58 +0000 | [diff] [blame] | 1237 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | fa1374c | 2006-02-27 07:41:46 +0000 | [diff] [blame] | 1238 | |
Linus Walleij | 9b28da3 | 2006-03-16 13:47:58 +0000 | [diff] [blame] | 1239 | if (ptp_getstorageinfo(params, device->storage_id, &storageInfo) != PTP_RC_OK) { |
Linus Walleij | fa1374c | 2006-02-27 07:41:46 +0000 | [diff] [blame] | 1240 | printf("LIBMTP_Get_Diskinfo(): failed to get disk info\n"); |
| 1241 | *total = 0; |
| 1242 | *free = 0; |
| 1243 | *storage_description = NULL; |
| 1244 | *volume_label = NULL; |
| 1245 | return -1; |
| 1246 | } |
| 1247 | *total = storageInfo.MaxCapability; |
| 1248 | *free = storageInfo.FreeSpaceInBytes; |
| 1249 | *storage_description = storageInfo.StorageDescription; |
| 1250 | *volume_label = storageInfo.VolumeLabel; |
| 1251 | |
| 1252 | return 0; |
| 1253 | } |
| 1254 | |
| 1255 | |
| 1256 | /** |
| 1257 | * This function retrieves the current battery level on the device. |
| 1258 | * @param device a pointer to the device to get the battery level for. |
| 1259 | * @param maximum_level a pointer to a variable that will hold the |
| 1260 | * maximum level of the battery if the call was successful. |
| 1261 | * @param current_level a pointer to a variable that will hold the |
| 1262 | * current level of the battery if the call was successful. |
Linus Walleij | 545c779 | 2006-06-13 15:22:30 +0000 | [diff] [blame] | 1263 | * A value of 0 means that the device is on external power. |
Linus Walleij | fa1374c | 2006-02-27 07:41:46 +0000 | [diff] [blame] | 1264 | * @return 0 if the storage info was successfully retrieved, any other |
Linus Walleij | 8043934 | 2006-09-12 10:42:26 +0000 | [diff] [blame^] | 1265 | * means failure. A typical cause of failure is that |
Linus Walleij | 545c779 | 2006-06-13 15:22:30 +0000 | [diff] [blame] | 1266 | * the device does not support the battery level property. |
Linus Walleij | fa1374c | 2006-02-27 07:41:46 +0000 | [diff] [blame] | 1267 | */ |
| 1268 | int LIBMTP_Get_Batterylevel(LIBMTP_mtpdevice_t *device, |
| 1269 | uint8_t * const maximum_level, |
| 1270 | uint8_t * const current_level) |
| 1271 | { |
Linus Walleij | b02a066 | 2006-04-25 08:05:09 +0000 | [diff] [blame] | 1272 | PTPPropertyValue propval; |
Linus Walleij | fa1374c | 2006-02-27 07:41:46 +0000 | [diff] [blame] | 1273 | uint16_t ret; |
Linus Walleij | 9b28da3 | 2006-03-16 13:47:58 +0000 | [diff] [blame] | 1274 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | fa1374c | 2006-02-27 07:41:46 +0000 | [diff] [blame] | 1275 | |
Linus Walleij | 545c779 | 2006-06-13 15:22:30 +0000 | [diff] [blame] | 1276 | *maximum_level = 0; |
| 1277 | *current_level = 0; |
| 1278 | |
| 1279 | if (!ptp_property_issupported(params, PTP_DPC_BatteryLevel)) { |
| 1280 | return -1; |
| 1281 | } |
| 1282 | |
Linus Walleij | b02a066 | 2006-04-25 08:05:09 +0000 | [diff] [blame] | 1283 | ret = ptp_getdevicepropvalue(params, PTP_DPC_BatteryLevel, &propval, PTP_DTC_UINT8); |
| 1284 | if (ret != PTP_RC_OK) { |
Linus Walleij | da9500d | 2006-08-30 13:17:06 +0000 | [diff] [blame] | 1285 | printf("LIBMTP_Get_Batterylevel(): could not get devcie property value.\n"); |
| 1286 | printf("Return code: 0x%04x (look this up in ptp.h for an explanation).\n", ret); |
Linus Walleij | fa1374c | 2006-02-27 07:41:46 +0000 | [diff] [blame] | 1287 | return -1; |
| 1288 | } |
| 1289 | |
| 1290 | *maximum_level = device->maximum_battery_level; |
Linus Walleij | b02a066 | 2006-04-25 08:05:09 +0000 | [diff] [blame] | 1291 | *current_level = propval.u8; |
Linus Walleij | fa1374c | 2006-02-27 07:41:46 +0000 | [diff] [blame] | 1292 | |
| 1293 | return 0; |
| 1294 | } |
| 1295 | |
| 1296 | /** |
Linus Walleij | 545c779 | 2006-06-13 15:22:30 +0000 | [diff] [blame] | 1297 | * Helper function to extract a unicode property off a device. |
Linus Walleij | e46f12e | 2006-06-22 17:53:25 +0000 | [diff] [blame] | 1298 | * This is the standard way of retrieveing unicode device |
| 1299 | * properties as described by the PTP spec. |
Linus Walleij | cf223e6 | 2006-06-19 09:31:53 +0000 | [diff] [blame] | 1300 | * @param device a pointer to the device to get the property from. |
| 1301 | * @param unicstring a pointer to a pointer that will hold the |
| 1302 | * property after this call is completed. |
| 1303 | * @param property the property to retrieve. |
| 1304 | * @return 0 on success, any other value means failure. |
Linus Walleij | 545c779 | 2006-06-13 15:22:30 +0000 | [diff] [blame] | 1305 | */ |
Linus Walleij | cf223e6 | 2006-06-19 09:31:53 +0000 | [diff] [blame] | 1306 | static int get_device_unicode_property(LIBMTP_mtpdevice_t *device, |
| 1307 | char **unicstring, uint16_t property) |
Linus Walleij | 545c779 | 2006-06-13 15:22:30 +0000 | [diff] [blame] | 1308 | { |
| 1309 | PTPPropertyValue propval; |
| 1310 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | 16571dc | 2006-08-17 20:27:46 +0000 | [diff] [blame] | 1311 | uint16_t *tmp; |
Linus Walleij | 545c779 | 2006-06-13 15:22:30 +0000 | [diff] [blame] | 1312 | int i; |
| 1313 | |
| 1314 | if (!ptp_property_issupported(params, property)) { |
| 1315 | return -1; |
| 1316 | } |
| 1317 | |
Linus Walleij | cf223e6 | 2006-06-19 09:31:53 +0000 | [diff] [blame] | 1318 | // Unicode strings are 16bit unsigned integer arrays. |
Linus Walleij | 545c779 | 2006-06-13 15:22:30 +0000 | [diff] [blame] | 1319 | if (ptp_getdevicepropvalue(params, |
| 1320 | property, |
| 1321 | &propval, |
| 1322 | PTP_DTC_AUINT16) != PTP_RC_OK) { |
| 1323 | return -1; |
| 1324 | } |
| 1325 | |
| 1326 | // Extract the actual array. |
Linus Walleij | 16571dc | 2006-08-17 20:27:46 +0000 | [diff] [blame] | 1327 | // printf("Array of %d elements\n", propval.a.count); |
| 1328 | tmp = malloc((propval.a.count + 1)*sizeof(uint16_t)); |
Linus Walleij | 545c779 | 2006-06-13 15:22:30 +0000 | [diff] [blame] | 1329 | for (i = 0; i < propval.a.count; i++) { |
Linus Walleij | 16571dc | 2006-08-17 20:27:46 +0000 | [diff] [blame] | 1330 | tmp[i] = propval.a.v[i].u16; |
| 1331 | // printf("%04x ", tmp[i]); |
Linus Walleij | 545c779 | 2006-06-13 15:22:30 +0000 | [diff] [blame] | 1332 | } |
Linus Walleij | 16571dc | 2006-08-17 20:27:46 +0000 | [diff] [blame] | 1333 | tmp[propval.a.count] = 0x0000U; |
Linus Walleij | 545c779 | 2006-06-13 15:22:30 +0000 | [diff] [blame] | 1334 | free(propval.a.v); |
| 1335 | |
Linus Walleij | 3ec8631 | 2006-08-21 13:25:24 +0000 | [diff] [blame] | 1336 | *unicstring = utf16_to_utf8(device, tmp); |
Linus Walleij | 16571dc | 2006-08-17 20:27:46 +0000 | [diff] [blame] | 1337 | |
Linus Walleij | 545c779 | 2006-06-13 15:22:30 +0000 | [diff] [blame] | 1338 | free(tmp); |
| 1339 | |
| 1340 | return 0; |
| 1341 | } |
| 1342 | |
| 1343 | /** |
| 1344 | * This function returns the secure time as an XML document string from |
| 1345 | * the device. |
| 1346 | * @param device a pointer to the device to get the secure time for. |
| 1347 | * @param sectime the secure time string as an XML document or NULL if the call |
| 1348 | * failed or the secure time property is not supported. This string |
| 1349 | * must be <code>free()</code>:ed by the caller after use. |
| 1350 | * @return 0 on success, any other value means failure. |
| 1351 | */ |
Linus Walleij | 8ab5426 | 2006-06-21 07:12:28 +0000 | [diff] [blame] | 1352 | int LIBMTP_Get_Secure_Time(LIBMTP_mtpdevice_t *device, char ** const sectime) |
Linus Walleij | 545c779 | 2006-06-13 15:22:30 +0000 | [diff] [blame] | 1353 | { |
| 1354 | return get_device_unicode_property(device, sectime, PTP_DPC_MTP_SecureTime); |
| 1355 | } |
| 1356 | |
| 1357 | /** |
| 1358 | * This function returns the device (public key) certificate as an |
| 1359 | * XML document string from the device. |
| 1360 | * @param device a pointer to the device to get the device certificate for. |
| 1361 | * @param devcert the device certificate as an XML string or NULL if the call |
| 1362 | * failed or the device certificate property is not supported. This |
| 1363 | * string must be <code>free()</code>:ed by the caller after use. |
| 1364 | * @return 0 on success, any other value means failure. |
| 1365 | */ |
Linus Walleij | 8ab5426 | 2006-06-21 07:12:28 +0000 | [diff] [blame] | 1366 | int LIBMTP_Get_Device_Certificate(LIBMTP_mtpdevice_t *device, char ** const devcert) |
Linus Walleij | 545c779 | 2006-06-13 15:22:30 +0000 | [diff] [blame] | 1367 | { |
| 1368 | return get_device_unicode_property(device, devcert, PTP_DPC_MTP_DeviceCertificate); |
| 1369 | } |
| 1370 | |
| 1371 | /** |
Linus Walleij | 8ab5426 | 2006-06-21 07:12:28 +0000 | [diff] [blame] | 1372 | * This function retrieves a list of supported file types, i.e. the file |
| 1373 | * types that this device claims it supports, e.g. audio file types that |
| 1374 | * the device can play etc. This list is mitigated to |
| 1375 | * inlcude the file types that libmtp can handle, i.e. it will not list |
| 1376 | * filetypes that libmtp will handle internally like playlists and folders. |
| 1377 | * @param device a pointer to the device to get the filetype capabilities for. |
| 1378 | * @param filetypes a pointer to a pointer that will hold the list of |
| 1379 | * supported filetypes if the call was successful. This list must |
| 1380 | * be <code>free()</code>:ed by the caller after use. |
| 1381 | * @param length a pointer to a variable that will hold the length of the |
| 1382 | * list of supported filetypes if the call was successful. |
| 1383 | * @return 0 on success, any other value means failure. |
| 1384 | * @see LIBMTP_Get_Filetype_Description() |
| 1385 | */ |
| 1386 | int LIBMTP_Get_Supported_Filetypes(LIBMTP_mtpdevice_t *device, uint16_t ** const filetypes, |
| 1387 | uint16_t * const length) |
| 1388 | { |
| 1389 | PTPParams *params = (PTPParams *) device->params; |
| 1390 | uint16_t *localtypes; |
| 1391 | uint16_t localtypelen; |
| 1392 | uint32_t i; |
| 1393 | |
| 1394 | // This is more memory than needed if there are unknown types, but what the heck. |
| 1395 | localtypes = (uint16_t *) malloc(params->deviceinfo.ImageFormats_len * sizeof(uint16_t)); |
| 1396 | localtypelen = 0; |
| 1397 | |
| 1398 | for (i=0;i<params->deviceinfo.ImageFormats_len;i++) { |
| 1399 | uint16_t localtype = map_ptp_type_to_libmtp_type(params->deviceinfo.ImageFormats[i]); |
| 1400 | if (localtype != LIBMTP_FILETYPE_UNKNOWN) { |
| 1401 | localtypes[localtypelen] = localtype; |
| 1402 | localtypelen++; |
| 1403 | } |
| 1404 | } |
| 1405 | |
| 1406 | *filetypes = localtypes; |
| 1407 | *length = localtypelen; |
| 1408 | |
| 1409 | return 0; |
| 1410 | } |
| 1411 | |
| 1412 | |
| 1413 | /** |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 1414 | * This creates a new MTP object structure and allocates memory |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 1415 | * for it. Notice that if you add strings to this structure they |
| 1416 | * will be freed by the corresponding <code>LIBMTP_destroy_object_t</code> |
| 1417 | * operation later, so be careful of using strdup() when assigning |
| 1418 | * strings, e.g.: |
| 1419 | * |
| 1420 | * <pre> |
| 1421 | * LIBMTP_object_t *object = LIBMTP_new_object_t(); |
| 1422 | * object->name = strdup(namestr); |
| 1423 | * .... |
| 1424 | * LIBMTP_destroy_object_t(file); |
| 1425 | * </pre> |
| 1426 | * |
| 1427 | * @return a pointer to the newly allocated structure. |
| 1428 | * @see LIBMTP_destroy_object_t() |
| 1429 | */ |
| 1430 | LIBMTP_object_t *LIBMTP_new_object_t(void) |
| 1431 | { |
| 1432 | LIBMTP_object_t *new = (LIBMTP_object_t *) malloc(sizeof(LIBMTP_object_t)); |
| 1433 | if (new == NULL) { |
| 1434 | return NULL; |
| 1435 | } |
| 1436 | |
| 1437 | new->id = 0; |
| 1438 | new->parent = 0; |
| 1439 | new->type = LIBMTP_FILETYPE_UNKNOWN; |
| 1440 | new->size = 0; |
| 1441 | new->name = NULL; |
| 1442 | new->data = NULL; |
| 1443 | new->sibling = NULL; |
| 1444 | new->child = NULL; |
| 1445 | |
| 1446 | return new; |
| 1447 | } |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 1448 | |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 1449 | /** |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 1450 | * This creates a new file metadata structure and allocates memory |
| 1451 | * for it. Notice that if you add strings to this structure they |
| 1452 | * will be freed by the corresponding <code>LIBMTP_destroy_file_t</code> |
| 1453 | * operation later, so be careful of using strdup() when assigning |
| 1454 | * strings, e.g.: |
| 1455 | * |
| 1456 | * <pre> |
| 1457 | * LIBMTP_file_t *file = LIBMTP_new_file_t(); |
| 1458 | * file->filename = strdup(namestr); |
| 1459 | * .... |
| 1460 | * LIBMTP_destroy_file_t(file); |
| 1461 | * </pre> |
| 1462 | * |
| 1463 | * @return a pointer to the newly allocated metadata structure. |
| 1464 | * @see LIBMTP_destroy_file_t() |
| 1465 | */ |
| 1466 | LIBMTP_file_t *LIBMTP_new_file_t(void) |
| 1467 | { |
| 1468 | LIBMTP_file_t *new = (LIBMTP_file_t *) malloc(sizeof(LIBMTP_file_t)); |
| 1469 | if (new == NULL) { |
| 1470 | return NULL; |
| 1471 | } |
| 1472 | new->filename = NULL; |
| 1473 | new->filesize = 0; |
| 1474 | new->filetype = LIBMTP_FILETYPE_UNKNOWN; |
| 1475 | new->next = NULL; |
| 1476 | return new; |
| 1477 | } |
| 1478 | |
| 1479 | /** |
| 1480 | * This destroys a file metadata structure and deallocates the memory |
| 1481 | * used by it, including any strings. Never use a file metadata |
| 1482 | * structure again after calling this function on it. |
| 1483 | * @param file the file metadata to destroy. |
| 1484 | * @see LIBMTP_new_file_t() |
| 1485 | */ |
| 1486 | void LIBMTP_destroy_file_t(LIBMTP_file_t *file) |
| 1487 | { |
| 1488 | if (file == NULL) { |
| 1489 | return; |
| 1490 | } |
| 1491 | if (file->filename != NULL) |
| 1492 | free(file->filename); |
| 1493 | free(file); |
| 1494 | return; |
| 1495 | } |
| 1496 | |
| 1497 | /** |
| 1498 | * This returns a long list of all files available |
| 1499 | * on the current MTP device. Typical usage: |
| 1500 | * |
| 1501 | * <pre> |
| 1502 | * LIBMTP_file_t *filelist; |
| 1503 | * |
| 1504 | * filelist = LIBMTP_Get_Filelisting(device); |
| 1505 | * while (filelist != NULL) { |
| 1506 | * LIBMTP_file_t *tmp; |
| 1507 | * |
| 1508 | * // Do something on each element in the list here... |
| 1509 | * tmp = filelist; |
| 1510 | * filelist = filelist->next; |
| 1511 | * LIBMTP_destroy_file_t(tmp); |
| 1512 | * } |
| 1513 | * </pre> |
| 1514 | * |
| 1515 | * @param device a pointer to the device to get the file listing for. |
| 1516 | * @return a list of files that can be followed using the <code>next</code> |
| 1517 | * field of the <code>LIBMTP_file_t</code> data structure. |
| 1518 | * Each of the metadata tags must be freed after use, and may |
| 1519 | * contain only partial metadata information, i.e. one or several |
| 1520 | * fields may be NULL or 0. |
Linus Walleij | 2e4b5f9 | 2006-06-16 14:00:49 +0000 | [diff] [blame] | 1521 | * @see LIBMTP_Get_Filemetadata() |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 1522 | */ |
| 1523 | LIBMTP_file_t *LIBMTP_Get_Filelisting(LIBMTP_mtpdevice_t *device) |
| 1524 | { |
| 1525 | uint32_t i = 0; |
| 1526 | LIBMTP_file_t *retfiles = NULL; |
| 1527 | LIBMTP_file_t *curfile = NULL; |
| 1528 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 1529 | |
| 1530 | // Get all the handles if we haven't already done that |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 1531 | if (params->handles.Handler == NULL) { |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 1532 | flush_handles(device); |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 1533 | } |
| 1534 | |
| 1535 | for (i = 0; i < params->handles.n; i++) { |
| 1536 | |
| 1537 | LIBMTP_file_t *file; |
| 1538 | PTPObjectInfo oi; |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 1539 | |
| 1540 | if (ptp_getobjectinfo(params, params->handles.Handler[i], &oi) == PTP_RC_OK) { |
Linus Walleij | 16c51f0 | 2006-05-04 13:20:22 +0000 | [diff] [blame] | 1541 | |
Linus Walleij | 9140559 | 2006-05-05 14:22:51 +0000 | [diff] [blame] | 1542 | if (oi.ObjectFormat == PTP_OFC_Association) { |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 1543 | // MTP use thesis object format for folders which means |
Linus Walleij | 16c51f0 | 2006-05-04 13:20:22 +0000 | [diff] [blame] | 1544 | // these "files" will turn up on a folder listing instead. |
| 1545 | continue; |
| 1546 | } |
| 1547 | |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 1548 | // Allocate a new file type |
| 1549 | file = LIBMTP_new_file_t(); |
Linus Walleij | b02a066 | 2006-04-25 08:05:09 +0000 | [diff] [blame] | 1550 | |
Linus Walleij | d208f9c | 2006-04-27 14:16:06 +0000 | [diff] [blame] | 1551 | file->parent_id = oi.ParentObject; |
Linus Walleij | 16c51f0 | 2006-05-04 13:20:22 +0000 | [diff] [blame] | 1552 | |
| 1553 | // Set the filetype |
| 1554 | file->filetype = map_ptp_type_to_libmtp_type(oi.ObjectFormat); |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 1555 | |
| 1556 | // Original file-specific properties |
| 1557 | file->filesize = oi.ObjectCompressedSize; |
| 1558 | if (oi.Filename != NULL) { |
| 1559 | file->filename = strdup(oi.Filename); |
| 1560 | } |
| 1561 | |
| 1562 | // This is some sort of unique ID so we can keep track of the track. |
| 1563 | file->item_id = params->handles.Handler[i]; |
| 1564 | |
| 1565 | // Add track to a list that will be returned afterwards. |
| 1566 | if (retfiles == NULL) { |
| 1567 | retfiles = file; |
| 1568 | curfile = file; |
| 1569 | } else { |
| 1570 | curfile->next = file; |
| 1571 | curfile = file; |
| 1572 | } |
| 1573 | |
| 1574 | // Call listing callback |
| 1575 | // double progressPercent = (double)i*(double)100.0 / (double)params->handles.n; |
| 1576 | |
| 1577 | } else { |
| 1578 | printf("LIBMTP panic: Found a bad handle, trying to ignore it.\n"); |
| 1579 | } |
| 1580 | |
| 1581 | } // Handle counting loop |
| 1582 | return retfiles; |
| 1583 | } |
| 1584 | |
| 1585 | /** |
Linus Walleij | 2e4b5f9 | 2006-06-16 14:00:49 +0000 | [diff] [blame] | 1586 | * This function retrieves the metadata for a single file off |
| 1587 | * the device. |
| 1588 | * |
| 1589 | * Do not call this function repeatedly! The file handles are linearly |
| 1590 | * searched O(n) and the call may involve (slow) USB traffic, so use |
| 1591 | * <code>LIBMTP_Get_Filelisting()</code> and cache the file, preferably |
| 1592 | * as an efficient data structure such as a hash list. |
| 1593 | * |
| 1594 | * @param device a pointer to the device to get the file metadata from. |
| 1595 | * @param fileid the object ID of the file that you want the metadata for. |
| 1596 | * @return a metadata entry on success or NULL on failure. |
| 1597 | * @see LIBMTP_Get_Filelisting() |
| 1598 | */ |
| 1599 | LIBMTP_file_t *LIBMTP_Get_Filemetadata(LIBMTP_mtpdevice_t *device, uint32_t const fileid) |
| 1600 | { |
| 1601 | uint32_t i = 0; |
| 1602 | PTPParams *params = (PTPParams *) device->params; |
| 1603 | |
| 1604 | // Get all the handles if we haven't already done that |
| 1605 | if (params->handles.Handler == NULL) { |
| 1606 | flush_handles(device); |
| 1607 | } |
| 1608 | |
| 1609 | for (i = 0; i < params->handles.n; i++) { |
| 1610 | LIBMTP_file_t *file; |
| 1611 | PTPObjectInfo oi; |
| 1612 | |
| 1613 | // Is this the file we're looking for? |
| 1614 | if (params->handles.Handler[i] != fileid) { |
| 1615 | continue; |
| 1616 | } |
| 1617 | |
| 1618 | if (ptp_getobjectinfo(params, params->handles.Handler[i], &oi) == PTP_RC_OK) { |
| 1619 | |
| 1620 | if (oi.ObjectFormat == PTP_OFC_Association) { |
| 1621 | // MTP use thesis object format for folders which means |
| 1622 | // these "files" will turn up on a folder listing instead. |
| 1623 | return NULL; |
| 1624 | } |
| 1625 | |
| 1626 | // Allocate a new file type |
| 1627 | file = LIBMTP_new_file_t(); |
| 1628 | |
| 1629 | file->parent_id = oi.ParentObject; |
| 1630 | |
| 1631 | // Set the filetype |
| 1632 | file->filetype = map_ptp_type_to_libmtp_type(oi.ObjectFormat); |
| 1633 | |
| 1634 | // Original file-specific properties |
| 1635 | file->filesize = oi.ObjectCompressedSize; |
| 1636 | if (oi.Filename != NULL) { |
| 1637 | file->filename = strdup(oi.Filename); |
| 1638 | } |
| 1639 | |
| 1640 | // This is some sort of unique ID so we can keep track of the track. |
| 1641 | file->item_id = params->handles.Handler[i]; |
| 1642 | |
| 1643 | return file; |
| 1644 | } else { |
| 1645 | return NULL; |
| 1646 | } |
| 1647 | |
| 1648 | } |
| 1649 | return NULL; |
| 1650 | } |
| 1651 | |
| 1652 | /** |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 1653 | * This creates a new track metadata structure and allocates memory |
| 1654 | * for it. Notice that if you add strings to this structure they |
| 1655 | * will be freed by the corresponding <code>LIBMTP_destroy_track_t</code> |
| 1656 | * operation later, so be careful of using strdup() when assigning |
| 1657 | * strings, e.g.: |
| 1658 | * |
Linus Walleij | 17e39f7 | 2006-02-23 15:54:28 +0000 | [diff] [blame] | 1659 | * <pre> |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 1660 | * LIBMTP_track_t *track = LIBMTP_new_track_t(); |
| 1661 | * track->title = strdup(titlestr); |
| 1662 | * .... |
| 1663 | * LIBMTP_destroy_track_t(track); |
Linus Walleij | 17e39f7 | 2006-02-23 15:54:28 +0000 | [diff] [blame] | 1664 | * </pre> |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 1665 | * |
| 1666 | * @return a pointer to the newly allocated metadata structure. |
| 1667 | * @see LIBMTP_destroy_track_t() |
| 1668 | */ |
| 1669 | LIBMTP_track_t *LIBMTP_new_track_t(void) |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 1670 | { |
| 1671 | LIBMTP_track_t *new = (LIBMTP_track_t *) malloc(sizeof(LIBMTP_track_t)); |
| 1672 | if (new == NULL) { |
| 1673 | return NULL; |
| 1674 | } |
| 1675 | new->title = NULL; |
| 1676 | new->artist = NULL; |
| 1677 | new->album = NULL; |
| 1678 | new->genre = NULL; |
| 1679 | new->date = NULL; |
| 1680 | new->filename = NULL; |
| 1681 | new->duration = 0; |
| 1682 | new->tracknumber = 0; |
| 1683 | new->filesize = 0; |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 1684 | new->filetype = LIBMTP_FILETYPE_UNKNOWN; |
Linus Walleij | cf223e6 | 2006-06-19 09:31:53 +0000 | [diff] [blame] | 1685 | new->samplerate = 0; |
| 1686 | new->nochannels = 0; |
| 1687 | new->wavecodec = 0; |
| 1688 | new->bitrate = 0; |
| 1689 | new->bitratetype = 0; |
| 1690 | new->rating = 0; |
| 1691 | new->usecount = 0; |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 1692 | new->next = NULL; |
| 1693 | return new; |
| 1694 | } |
| 1695 | |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 1696 | /** |
| 1697 | * This destroys a track metadata structure and deallocates the memory |
| 1698 | * used by it, including any strings. Never use a track metadata |
| 1699 | * structure again after calling this function on it. |
| 1700 | * @param track the track metadata to destroy. |
| 1701 | * @see LIBMTP_new_track_t() |
| 1702 | */ |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 1703 | void LIBMTP_destroy_track_t(LIBMTP_track_t *track) |
| 1704 | { |
| 1705 | if (track == NULL) { |
| 1706 | return; |
| 1707 | } |
| 1708 | if (track->title != NULL) |
| 1709 | free(track->title); |
| 1710 | if (track->artist != NULL) |
| 1711 | free(track->artist); |
| 1712 | if (track->album != NULL) |
| 1713 | free(track->album); |
| 1714 | if (track->genre != NULL) |
| 1715 | free(track->genre); |
| 1716 | if (track->date != NULL) |
| 1717 | free(track->date); |
| 1718 | if (track->filename != NULL) |
| 1719 | free(track->filename); |
| 1720 | free(track); |
| 1721 | return; |
| 1722 | } |
| 1723 | |
| 1724 | /** |
Linus Walleij | 8ab5426 | 2006-06-21 07:12:28 +0000 | [diff] [blame] | 1725 | * This function retrieves the track metadata for a track |
| 1726 | * given by a unique ID. |
| 1727 | * @param device a pointer to the device to get the track metadata off. |
| 1728 | * @param trackid the unique ID of the track. |
| 1729 | * @param objectformat the object format of this track, so we know what it supports. |
| 1730 | * @param track a metadata set to fill in. |
| 1731 | */ |
| 1732 | static void get_track_metadata(LIBMTP_mtpdevice_t *device, uint16_t objectformat, |
| 1733 | LIBMTP_track_t *track) |
| 1734 | { |
Linus Walleij | 00cf064 | 2006-07-26 20:40:59 +0000 | [diff] [blame] | 1735 | uint16_t ret; |
| 1736 | PTPParams *params = (PTPParams *) device->params; |
| 1737 | uint32_t i; |
| 1738 | uint16_t *props = NULL; |
| 1739 | uint32_t propcnt = 0; |
| 1740 | |
| 1741 | // First see which properties can be retrieved for this object format |
| 1742 | ret = ptp_mtp_getobjectpropssupported (params, map_libmtp_type_to_ptp_type(track->filetype), &propcnt, &props); |
| 1743 | if (ret != PTP_RC_OK) { |
| 1744 | // Just bail out for now, nothing is ever set. |
| 1745 | return; |
| 1746 | } else { |
| 1747 | for (i=0;i<propcnt;i++) { |
| 1748 | switch (props[i]) { |
| 1749 | case PTP_OPC_Name: |
Linus Walleij | a823a70 | 2006-08-27 21:27:46 +0000 | [diff] [blame] | 1750 | track->title = LIBMTP_Get_String_From_Object(device, track->item_id, PTP_OPC_Name); |
Linus Walleij | 00cf064 | 2006-07-26 20:40:59 +0000 | [diff] [blame] | 1751 | break; |
| 1752 | case PTP_OPC_Artist: |
Linus Walleij | a823a70 | 2006-08-27 21:27:46 +0000 | [diff] [blame] | 1753 | track->artist = LIBMTP_Get_String_From_Object(device, track->item_id, PTP_OPC_Artist); |
Linus Walleij | 00cf064 | 2006-07-26 20:40:59 +0000 | [diff] [blame] | 1754 | break; |
| 1755 | case PTP_OPC_Duration: |
| 1756 | track->duration = LIBMTP_Get_U32_From_Object(device, track->item_id, PTP_OPC_Duration, 0); |
| 1757 | break; |
| 1758 | case PTP_OPC_Track: |
| 1759 | track->tracknumber = LIBMTP_Get_U16_From_Object(device, track->item_id, PTP_OPC_Track, 0); |
| 1760 | break; |
| 1761 | case PTP_OPC_Genre: |
Linus Walleij | a823a70 | 2006-08-27 21:27:46 +0000 | [diff] [blame] | 1762 | track->genre = LIBMTP_Get_String_From_Object(device, track->item_id, PTP_OPC_Genre); |
Linus Walleij | 00cf064 | 2006-07-26 20:40:59 +0000 | [diff] [blame] | 1763 | break; |
| 1764 | case PTP_OPC_AlbumName: |
Linus Walleij | a823a70 | 2006-08-27 21:27:46 +0000 | [diff] [blame] | 1765 | track->album = LIBMTP_Get_String_From_Object(device, track->item_id, PTP_OPC_AlbumName); |
Linus Walleij | 00cf064 | 2006-07-26 20:40:59 +0000 | [diff] [blame] | 1766 | break; |
| 1767 | case PTP_OPC_OriginalReleaseDate: |
Linus Walleij | a823a70 | 2006-08-27 21:27:46 +0000 | [diff] [blame] | 1768 | track->date = LIBMTP_Get_String_From_Object(device, track->item_id, PTP_OPC_OriginalReleaseDate); |
Linus Walleij | 00cf064 | 2006-07-26 20:40:59 +0000 | [diff] [blame] | 1769 | break; |
| 1770 | // These are, well not so important. |
| 1771 | case PTP_OPC_SampleRate: |
| 1772 | track->samplerate = LIBMTP_Get_U32_From_Object(device, track->item_id, PTP_OPC_SampleRate, 0); |
| 1773 | break; |
| 1774 | case PTP_OPC_NumberOfChannels: |
| 1775 | track->nochannels = LIBMTP_Get_U16_From_Object(device, track->item_id, PTP_OPC_NumberOfChannels, 0); |
| 1776 | break; |
| 1777 | case PTP_OPC_AudioWAVECodec: |
| 1778 | track->wavecodec = LIBMTP_Get_U32_From_Object(device, track->item_id, PTP_OPC_AudioWAVECodec, 0); |
| 1779 | break; |
| 1780 | case PTP_OPC_AudioBitRate: |
| 1781 | track->bitrate = LIBMTP_Get_U32_From_Object(device, track->item_id, PTP_OPC_AudioBitRate, 0); |
| 1782 | break; |
| 1783 | case PTP_OPC_BitRateType: |
| 1784 | track->bitratetype = LIBMTP_Get_U16_From_Object(device, track->item_id, PTP_OPC_BitRateType, 0); |
| 1785 | break; |
| 1786 | case PTP_OPC_Rating: |
| 1787 | track->rating = LIBMTP_Get_U16_From_Object(device, track->item_id, PTP_OPC_Rating, 0); |
| 1788 | break; |
| 1789 | case PTP_OPC_UseCount: |
| 1790 | track->usecount = LIBMTP_Get_U32_From_Object(device, track->item_id, PTP_OPC_UseCount, 0); |
| 1791 | break; |
| 1792 | } |
| 1793 | } |
| 1794 | free(props); |
| 1795 | } |
Linus Walleij | 8ab5426 | 2006-06-21 07:12:28 +0000 | [diff] [blame] | 1796 | } |
| 1797 | |
| 1798 | /** |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 1799 | * This returns a long list of all tracks available |
Linus Walleij | a498273 | 2006-02-24 15:46:02 +0000 | [diff] [blame] | 1800 | * on the current MTP device. Typical usage: |
| 1801 | * |
| 1802 | * <pre> |
| 1803 | * LIBMTP_track_t *tracklist; |
| 1804 | * |
| 1805 | * tracklist = LIBMTP_Get_Tracklisting(device); |
| 1806 | * while (tracklist != NULL) { |
| 1807 | * LIBMTP_track_t *tmp; |
| 1808 | * |
| 1809 | * // Do something on each element in the list here... |
| 1810 | * tmp = tracklist; |
| 1811 | * tracklist = tracklist->next; |
| 1812 | * LIBMTP_destroy_track_t(tmp); |
| 1813 | * } |
| 1814 | * </pre> |
| 1815 | * |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 1816 | * @param device a pointer to the device to get the track listing for. |
Linus Walleij | a498273 | 2006-02-24 15:46:02 +0000 | [diff] [blame] | 1817 | * @return a list of tracks that can be followed using the <code>next</code> |
| 1818 | * field of the <code>LIBMTP_track_t</code> data structure. |
| 1819 | * Each of the metadata tags must be freed after use, and may |
| 1820 | * contain only partial metadata information, i.e. one or several |
| 1821 | * fields may be NULL or 0. |
Linus Walleij | 2e4b5f9 | 2006-06-16 14:00:49 +0000 | [diff] [blame] | 1822 | * @see LIBMTP_Get_Trackmetadata() |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 1823 | */ |
| 1824 | LIBMTP_track_t *LIBMTP_Get_Tracklisting(LIBMTP_mtpdevice_t *device) |
| 1825 | { |
| 1826 | uint32_t i = 0; |
| 1827 | LIBMTP_track_t *retracks = NULL; |
| 1828 | LIBMTP_track_t *curtrack = NULL; |
Linus Walleij | 9b28da3 | 2006-03-16 13:47:58 +0000 | [diff] [blame] | 1829 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 1830 | |
| 1831 | // Get all the handles if we haven't already done that |
Linus Walleij | 9b28da3 | 2006-03-16 13:47:58 +0000 | [diff] [blame] | 1832 | if (params->handles.Handler == NULL) { |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 1833 | flush_handles(device); |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 1834 | } |
| 1835 | |
Linus Walleij | 9b28da3 | 2006-03-16 13:47:58 +0000 | [diff] [blame] | 1836 | for (i = 0; i < params->handles.n; i++) { |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 1837 | LIBMTP_track_t *track; |
| 1838 | PTPObjectInfo oi; |
Linus Walleij | 5acfa74 | 2006-05-29 14:51:59 +0000 | [diff] [blame] | 1839 | |
Linus Walleij | 9b28da3 | 2006-03-16 13:47:58 +0000 | [diff] [blame] | 1840 | if (ptp_getobjectinfo(params, params->handles.Handler[i], &oi) == PTP_RC_OK) { |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 1841 | |
| 1842 | // Ignore stuff we don't know how to handle... |
Linus Walleij | 8ab5426 | 2006-06-21 07:12:28 +0000 | [diff] [blame] | 1843 | // TODO: get this list as an intersection of the sets |
| 1844 | // supported by the device and the from the device and |
| 1845 | // all known audio track files? |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 1846 | if ( oi.ObjectFormat != PTP_OFC_WAV && |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 1847 | oi.ObjectFormat != PTP_OFC_MP3 && |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 1848 | oi.ObjectFormat != PTP_OFC_MTP_WMA && |
| 1849 | oi.ObjectFormat != PTP_OFC_MTP_OGG && |
| 1850 | oi.ObjectFormat != PTP_OFC_MTP_MP4 && |
| 1851 | oi.ObjectFormat != PTP_OFC_MTP_UndefinedAudio ) { |
Linus Walleij | afe6143 | 2006-05-05 13:51:34 +0000 | [diff] [blame] | 1852 | // printf("Not a music track (format: %d), skipping...\n",oi.ObjectFormat); |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 1853 | continue; |
| 1854 | } |
| 1855 | |
| 1856 | // Allocate a new track type |
| 1857 | track = LIBMTP_new_track_t(); |
Linus Walleij | 8ab5426 | 2006-06-21 07:12:28 +0000 | [diff] [blame] | 1858 | |
| 1859 | // This is some sort of unique ID so we can keep track of the track. |
| 1860 | track->item_id = params->handles.Handler[i]; |
Linus Walleij | 5acfa74 | 2006-05-29 14:51:59 +0000 | [diff] [blame] | 1861 | |
Linus Walleij | 16c51f0 | 2006-05-04 13:20:22 +0000 | [diff] [blame] | 1862 | track->filetype = map_ptp_type_to_libmtp_type(oi.ObjectFormat); |
Linus Walleij | 5acfa74 | 2006-05-29 14:51:59 +0000 | [diff] [blame] | 1863 | |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 1864 | // Original file-specific properties |
| 1865 | track->filesize = oi.ObjectCompressedSize; |
| 1866 | if (oi.Filename != NULL) { |
| 1867 | track->filename = strdup(oi.Filename); |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 1868 | } |
Linus Walleij | 8ab5426 | 2006-06-21 07:12:28 +0000 | [diff] [blame] | 1869 | |
| 1870 | get_track_metadata(device, oi.ObjectFormat, track); |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 1871 | |
| 1872 | // Add track to a list that will be returned afterwards. |
| 1873 | if (retracks == NULL) { |
| 1874 | retracks = track; |
| 1875 | curtrack = track; |
| 1876 | } else { |
| 1877 | curtrack->next = track; |
| 1878 | curtrack = track; |
| 1879 | } |
| 1880 | |
| 1881 | // Call listing callback |
Linus Walleij | 9b28da3 | 2006-03-16 13:47:58 +0000 | [diff] [blame] | 1882 | // double progressPercent = (double)i*(double)100.0 / (double)params->handles.n; |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 1883 | |
| 1884 | } else { |
| 1885 | printf("LIBMTP panic: Found a bad handle, trying to ignore it.\n"); |
| 1886 | } |
| 1887 | |
| 1888 | } // Handle counting loop |
| 1889 | return retracks; |
| 1890 | } |
Linus Walleij | dcde608 | 2006-02-17 16:16:34 +0000 | [diff] [blame] | 1891 | |
Linus Walleij | 2e4b5f9 | 2006-06-16 14:00:49 +0000 | [diff] [blame] | 1892 | /** |
| 1893 | * This function retrieves the metadata for a single track off |
| 1894 | * the device. |
| 1895 | * |
| 1896 | * Do not call this function repeatedly! The track handles are linearly |
| 1897 | * searched O(n) and the call may involve (slow) USB traffic, so use |
| 1898 | * <code>LIBMTP_Get_Tracklisting()</code> and cache the tracks, preferably |
| 1899 | * as an efficient data structure such as a hash list. |
| 1900 | * |
| 1901 | * @param device a pointer to the device to get the track metadata from. |
| 1902 | * @param trackid the object ID of the track that you want the metadata for. |
| 1903 | * @return a track metadata entry on success or NULL on failure. |
| 1904 | * @see LIBMTP_Get_Tracklisting() |
| 1905 | */ |
| 1906 | LIBMTP_track_t *LIBMTP_Get_Trackmetadata(LIBMTP_mtpdevice_t *device, uint32_t const trackid) |
| 1907 | { |
| 1908 | uint32_t i = 0; |
| 1909 | PTPParams *params = (PTPParams *) device->params; |
| 1910 | |
| 1911 | // Get all the handles if we haven't already done that |
| 1912 | if (params->handles.Handler == NULL) { |
| 1913 | flush_handles(device); |
| 1914 | } |
| 1915 | |
| 1916 | for (i = 0; i < params->handles.n; i++) { |
| 1917 | PTPObjectInfo oi; |
| 1918 | |
| 1919 | // Skip if this is not the track we want. |
| 1920 | if (params->handles.Handler[i] != trackid) { |
| 1921 | continue; |
| 1922 | } |
| 1923 | |
| 1924 | if (ptp_getobjectinfo(params, params->handles.Handler[i], &oi) == PTP_RC_OK) { |
| 1925 | LIBMTP_track_t *track; |
| 1926 | |
| 1927 | // Ignore stuff we don't know how to handle... |
| 1928 | if ( oi.ObjectFormat != PTP_OFC_WAV && |
| 1929 | oi.ObjectFormat != PTP_OFC_MP3 && |
| 1930 | oi.ObjectFormat != PTP_OFC_MTP_WMA && |
| 1931 | oi.ObjectFormat != PTP_OFC_MTP_OGG && |
| 1932 | oi.ObjectFormat != PTP_OFC_MTP_MP4 && |
| 1933 | oi.ObjectFormat != PTP_OFC_MTP_UndefinedAudio ) { |
| 1934 | return NULL; |
| 1935 | } |
| 1936 | |
| 1937 | // Allocate a new track type |
| 1938 | track = LIBMTP_new_track_t(); |
Linus Walleij | 8ab5426 | 2006-06-21 07:12:28 +0000 | [diff] [blame] | 1939 | |
| 1940 | // This is some sort of unique ID so we can keep track of the track. |
| 1941 | track->item_id = params->handles.Handler[i]; |
Linus Walleij | 2e4b5f9 | 2006-06-16 14:00:49 +0000 | [diff] [blame] | 1942 | |
| 1943 | track->filetype = map_ptp_type_to_libmtp_type(oi.ObjectFormat); |
| 1944 | |
| 1945 | // Original file-specific properties |
| 1946 | track->filesize = oi.ObjectCompressedSize; |
| 1947 | if (oi.Filename != NULL) { |
| 1948 | track->filename = strdup(oi.Filename); |
| 1949 | } |
Linus Walleij | 8ab5426 | 2006-06-21 07:12:28 +0000 | [diff] [blame] | 1950 | |
| 1951 | get_track_metadata(device, oi.ObjectFormat, track); |
| 1952 | |
Linus Walleij | 2e4b5f9 | 2006-06-16 14:00:49 +0000 | [diff] [blame] | 1953 | return track; |
| 1954 | |
| 1955 | } else { |
| 1956 | return NULL; |
| 1957 | } |
| 1958 | |
| 1959 | } |
| 1960 | return NULL; |
| 1961 | } |
| 1962 | |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 1963 | |
| 1964 | /** |
| 1965 | * This gets a file off the device to a local file identified |
| 1966 | * by a filename. |
| 1967 | * @param device a pointer to the device to get the track from. |
| 1968 | * @param id the file ID of the file to retrieve. |
| 1969 | * @param path a filename to use for the retrieved file. |
| 1970 | * @param callback a progress indicator function or NULL to ignore. |
| 1971 | * @param data a user-defined pointer that is passed along to |
| 1972 | * the <code>progress</code> function in order to |
| 1973 | * pass along some user defined data to the progress |
| 1974 | * updates. If not used, set this to NULL. |
| 1975 | * @return 0 if the transfer was successful, any other value means |
| 1976 | * failure. |
| 1977 | * @see LIBMTP_Get_File_To_File_Descriptor() |
| 1978 | */ |
| 1979 | int LIBMTP_Get_File_To_File(LIBMTP_mtpdevice_t *device, uint32_t const id, |
Linus Walleij | ee73ef2 | 2006-08-27 19:56:00 +0000 | [diff] [blame] | 1980 | char const * const path, LIBMTP_progressfunc_t const callback, |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 1981 | void const * const data) |
| 1982 | { |
| 1983 | int fd = -1; |
| 1984 | int ret; |
| 1985 | |
| 1986 | // Sanity check |
| 1987 | if (path == NULL) { |
| 1988 | printf("LIBMTP_Get_File_To_File(): Bad arguments, path was NULL\n"); |
| 1989 | return -1; |
| 1990 | } |
| 1991 | |
| 1992 | // Open file |
| 1993 | #ifdef __WIN32__ |
flavien | bfd80eb | 2006-06-01 22:41:49 +0000 | [diff] [blame] | 1994 | if ( (fd = open(path, O_RDWR|O_CREAT|O_TRUNC|O_BINARY,S_IRWXU|S_IRGRP)) == -1 ) { |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 1995 | #else |
| 1996 | if ( (fd = open(path, O_RDWR|O_CREAT|O_TRUNC,S_IRWXU|S_IRGRP)) == -1) { |
| 1997 | #endif |
| 1998 | printf("LIBMTP_Get_File_To_File(): Could not create file \"%s\"\n", path); |
| 1999 | return -1; |
| 2000 | } |
| 2001 | |
| 2002 | ret = LIBMTP_Get_File_To_File_Descriptor(device, id, fd, callback, data); |
| 2003 | |
| 2004 | // Close file |
| 2005 | close(fd); |
| 2006 | |
| 2007 | return ret; |
| 2008 | } |
| 2009 | |
| 2010 | /** |
| 2011 | * This gets a file off the device to a file identified |
| 2012 | * by a file descriptor. |
Linus Walleij | 0558ac5 | 2006-09-07 06:55:03 +0000 | [diff] [blame] | 2013 | * |
| 2014 | * This function can potentially be used for streaming |
| 2015 | * files off the device for playback or broadcast for example, |
| 2016 | * by downloading the file into a stream sink e.g. a socket. |
| 2017 | * |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 2018 | * @param device a pointer to the device to get the file from. |
| 2019 | * @param id the file ID of the file to retrieve. |
| 2020 | * @param fd a local file descriptor to write the file to. |
| 2021 | * @param callback a progress indicator function or NULL to ignore. |
| 2022 | * @param data a user-defined pointer that is passed along to |
| 2023 | * the <code>progress</code> function in order to |
| 2024 | * pass along some user defined data to the progress |
| 2025 | * updates. If not used, set this to NULL. |
| 2026 | * @return 0 if the transfer was successful, any other value means |
| 2027 | * failure. |
| 2028 | * @see LIBMTP_Get_File_To_File() |
| 2029 | */ |
| 2030 | int LIBMTP_Get_File_To_File_Descriptor(LIBMTP_mtpdevice_t *device, |
| 2031 | uint32_t const id, |
| 2032 | int const fd, |
Linus Walleij | ee73ef2 | 2006-08-27 19:56:00 +0000 | [diff] [blame] | 2033 | LIBMTP_progressfunc_t const callback, |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 2034 | void const * const data) |
| 2035 | { |
| 2036 | PTPObjectInfo oi; |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 2037 | uint16_t ret; |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 2038 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | ee73ef2 | 2006-08-27 19:56:00 +0000 | [diff] [blame] | 2039 | PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo; |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 2040 | |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 2041 | if (ptp_getobjectinfo(params, id, &oi) != PTP_RC_OK) { |
| 2042 | printf("LIBMTP_Get_File_To_File_Descriptor(): Could not get object info\n"); |
| 2043 | return -1; |
| 2044 | } |
| 2045 | if (oi.ObjectFormat == PTP_OFC_Association) { |
| 2046 | printf("LIBMTP_Get_File_To_File_Descriptor(): Bad object format\n"); |
| 2047 | return -1; |
| 2048 | } |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 2049 | |
Linus Walleij | ee73ef2 | 2006-08-27 19:56:00 +0000 | [diff] [blame] | 2050 | // Callbacks |
| 2051 | ptp_usb->callback_active = 1; |
Linus Walleij | 7f0c72e | 2006-09-06 13:01:58 +0000 | [diff] [blame] | 2052 | ptp_usb->current_transfer_total = oi.ObjectCompressedSize+ |
| 2053 | PTP_USB_BULK_HDR_LEN+sizeof(uint32_t); // Request length, one parameter |
Linus Walleij | ee73ef2 | 2006-08-27 19:56:00 +0000 | [diff] [blame] | 2054 | ptp_usb->current_transfer_complete = 0; |
| 2055 | ptp_usb->current_transfer_callback = callback; |
| 2056 | ptp_usb->current_transfer_callback_data = data; |
| 2057 | |
Linus Walleij | 96c6243 | 2006-08-21 10:04:02 +0000 | [diff] [blame] | 2058 | // This now exist in upstream |
| 2059 | ret = ptp_getobject_tofd(params, id, fd); |
Linus Walleij | ee73ef2 | 2006-08-27 19:56:00 +0000 | [diff] [blame] | 2060 | |
| 2061 | ptp_usb->callback_active = 0; |
| 2062 | ptp_usb->current_transfer_callback = NULL; |
| 2063 | ptp_usb->current_transfer_callback_data = NULL; |
Linus Walleij | c3ea3e5 | 2006-08-20 22:13:32 +0000 | [diff] [blame] | 2064 | |
Linus Walleij | b02a066 | 2006-04-25 08:05:09 +0000 | [diff] [blame] | 2065 | if (ret != PTP_RC_OK) { |
| 2066 | printf("LIBMTP_Get_File_To_File_Descriptor(): Could not get file from device (%d)\n", ret); |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 2067 | return -1; |
| 2068 | } |
Linus Walleij | c3ea3e5 | 2006-08-20 22:13:32 +0000 | [diff] [blame] | 2069 | |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 2070 | return 0; |
| 2071 | } |
| 2072 | |
Linus Walleij | dcde608 | 2006-02-17 16:16:34 +0000 | [diff] [blame] | 2073 | /** |
| 2074 | * This gets a track off the device to a file identified |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 2075 | * by a filename. This is actually just a wrapper for the |
| 2076 | * \c LIBMTP_Get_Track_To_File() function. |
Linus Walleij | dcde608 | 2006-02-17 16:16:34 +0000 | [diff] [blame] | 2077 | * @param device a pointer to the device to get the track from. |
| 2078 | * @param id the track ID of the track to retrieve. |
| 2079 | * @param path a filename to use for the retrieved track. |
| 2080 | * @param callback a progress indicator function or NULL to ignore. |
| 2081 | * @param data a user-defined pointer that is passed along to |
| 2082 | * the <code>progress</code> function in order to |
| 2083 | * pass along some user defined data to the progress |
| 2084 | * updates. If not used, set this to NULL. |
| 2085 | * @return 0 if the transfer was successful, any other value means |
| 2086 | * failure. |
| 2087 | * @see LIBMTP_Get_Track_To_File_Descriptor() |
| 2088 | */ |
Linus Walleij | 0cd8543 | 2006-02-20 14:37:50 +0000 | [diff] [blame] | 2089 | int LIBMTP_Get_Track_To_File(LIBMTP_mtpdevice_t *device, uint32_t const id, |
Linus Walleij | ee73ef2 | 2006-08-27 19:56:00 +0000 | [diff] [blame] | 2090 | char const * const path, LIBMTP_progressfunc_t const callback, |
Linus Walleij | 0cd8543 | 2006-02-20 14:37:50 +0000 | [diff] [blame] | 2091 | void const * const data) |
Linus Walleij | dcde608 | 2006-02-17 16:16:34 +0000 | [diff] [blame] | 2092 | { |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 2093 | // This is just a wrapper |
| 2094 | return LIBMTP_Get_File_To_File(device, id, path, callback, data); |
Linus Walleij | dcde608 | 2006-02-17 16:16:34 +0000 | [diff] [blame] | 2095 | } |
| 2096 | |
| 2097 | /** |
| 2098 | * This gets a track off the device to a file identified |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 2099 | * by a file descriptor. This is actually just a wrapper for |
| 2100 | * the \c LIBMTP_Get_File_To_File_Descriptor() function. |
Linus Walleij | dcde608 | 2006-02-17 16:16:34 +0000 | [diff] [blame] | 2101 | * @param device a pointer to the device to get the track from. |
| 2102 | * @param id the track ID of the track to retrieve. |
| 2103 | * @param fd a file descriptor to write the track to. |
| 2104 | * @param callback a progress indicator function or NULL to ignore. |
| 2105 | * @param data a user-defined pointer that is passed along to |
| 2106 | * the <code>progress</code> function in order to |
| 2107 | * pass along some user defined data to the progress |
| 2108 | * updates. If not used, set this to NULL. |
| 2109 | * @return 0 if the transfer was successful, any other value means |
| 2110 | * failure. |
| 2111 | * @see LIBMTP_Get_Track_To_File() |
| 2112 | */ |
| 2113 | int LIBMTP_Get_Track_To_File_Descriptor(LIBMTP_mtpdevice_t *device, |
Linus Walleij | 0cd8543 | 2006-02-20 14:37:50 +0000 | [diff] [blame] | 2114 | uint32_t const id, |
| 2115 | int const fd, |
Linus Walleij | ee73ef2 | 2006-08-27 19:56:00 +0000 | [diff] [blame] | 2116 | LIBMTP_progressfunc_t const callback, |
Linus Walleij | 0cd8543 | 2006-02-20 14:37:50 +0000 | [diff] [blame] | 2117 | void const * const data) |
Linus Walleij | dcde608 | 2006-02-17 16:16:34 +0000 | [diff] [blame] | 2118 | { |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 2119 | // This is just a wrapper |
| 2120 | return LIBMTP_Get_File_To_File_Descriptor(device, id, fd, callback, data); |
Linus Walleij | dcde608 | 2006-02-17 16:16:34 +0000 | [diff] [blame] | 2121 | } |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 2122 | |
| 2123 | /** |
| 2124 | * This function sends a track from a local file to an |
| 2125 | * MTP device. A filename and a set of metadata must be |
| 2126 | * given as input. |
| 2127 | * @param device a pointer to the device to send the track to. |
| 2128 | * @param path the filename of a local file which will be sent. |
| 2129 | * @param metadata a track metadata set to be written along with the file. |
| 2130 | * @param callback a progress indicator function or NULL to ignore. |
| 2131 | * @param data a user-defined pointer that is passed along to |
| 2132 | * the <code>progress</code> function in order to |
| 2133 | * pass along some user defined data to the progress |
| 2134 | * updates. If not used, set this to NULL. |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 2135 | * @param parenthandle the parent (e.g. folder) to store this file |
Linus Walleij | d7aa5b2 | 2006-09-02 11:52:31 +0000 | [diff] [blame] | 2136 | * in. Since some devices are a bit picky about where files |
| 2137 | * are placed, a default folder will be chosen if libmtp |
| 2138 | * has detected one for the current filetype and this |
| 2139 | * parameter is set to 0. If this is 0 and no default folder |
| 2140 | * can be found, the file will be stored in the root folder. |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 2141 | * @return 0 if the transfer was successful, any other value means |
| 2142 | * failure. |
| 2143 | * @see LIBMTP_Send_Track_From_File_Descriptor() |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 2144 | * @see LIBMTP_Delete_Object() |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 2145 | */ |
| 2146 | int LIBMTP_Send_Track_From_File(LIBMTP_mtpdevice_t *device, |
| 2147 | char const * const path, LIBMTP_track_t * const metadata, |
Linus Walleij | ee73ef2 | 2006-08-27 19:56:00 +0000 | [diff] [blame] | 2148 | LIBMTP_progressfunc_t const callback, |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 2149 | void const * const data, uint32_t const parenthandle) |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 2150 | { |
| 2151 | int fd; |
| 2152 | int ret; |
| 2153 | |
| 2154 | // Sanity check |
| 2155 | if (path == NULL) { |
| 2156 | printf("LIBMTP_Send_Track_From_File(): Bad arguments, path was NULL\n"); |
| 2157 | return -1; |
| 2158 | } |
| 2159 | |
| 2160 | // Open file |
| 2161 | #ifdef __WIN32__ |
| 2162 | if ( (fd = open(path, O_RDONLY|O_BINARY) == -1 ) { |
| 2163 | #else |
| 2164 | if ( (fd = open(path, O_RDONLY)) == -1) { |
| 2165 | #endif |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 2166 | printf("LIBMTP_Send_Track_From_File(): Could not open source file \"%s\"\n", path); |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 2167 | return -1; |
| 2168 | } |
| 2169 | |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 2170 | ret = LIBMTP_Send_Track_From_File_Descriptor(device, fd, metadata, callback, data, parenthandle); |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 2171 | |
| 2172 | // Close file. |
| 2173 | close(fd); |
| 2174 | |
| 2175 | return ret; |
| 2176 | } |
| 2177 | |
Linus Walleij | fa1374c | 2006-02-27 07:41:46 +0000 | [diff] [blame] | 2178 | |
| 2179 | /** |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 2180 | * This function sends a track from a file descriptor to an |
| 2181 | * MTP device. A filename and a set of metadata must be |
| 2182 | * given as input. |
| 2183 | * @param device a pointer to the device to send the track to. |
| 2184 | * @param fd the filedescriptor for a local file which will be sent. |
| 2185 | * @param metadata a track metadata set to be written along with the file. |
| 2186 | * After this call the field <code>item_id</code> |
| 2187 | * will contain the new track ID. |
| 2188 | * @param callback a progress indicator function or NULL to ignore. |
| 2189 | * @param data a user-defined pointer that is passed along to |
| 2190 | * the <code>progress</code> function in order to |
| 2191 | * pass along some user defined data to the progress |
| 2192 | * updates. If not used, set this to NULL. |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 2193 | * @param parenthandle the parent (e.g. folder) to store this file |
Linus Walleij | d7aa5b2 | 2006-09-02 11:52:31 +0000 | [diff] [blame] | 2194 | * in. Since some devices are a bit picky about where files |
| 2195 | * are placed, a default folder will be chosen if libmtp |
| 2196 | * has detected one for the current filetype and this |
| 2197 | * parameter is set to 0. If this is 0 and no default folder |
| 2198 | * can be found, the file will be stored in the root folder. |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 2199 | * @return 0 if the transfer was successful, any other value means |
| 2200 | * failure. |
| 2201 | * @see LIBMTP_Send_Track_From_File() |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 2202 | * @see LIBMTP_Delete_Object() |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 2203 | */ |
| 2204 | int LIBMTP_Send_Track_From_File_Descriptor(LIBMTP_mtpdevice_t *device, |
| 2205 | int const fd, LIBMTP_track_t * const metadata, |
Linus Walleij | ee73ef2 | 2006-08-27 19:56:00 +0000 | [diff] [blame] | 2206 | LIBMTP_progressfunc_t const callback, |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 2207 | void const * const data, uint32_t const parenthandle) |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 2208 | { |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 2209 | uint16_t ret; |
| 2210 | uint32_t store = 0; |
Linus Walleij | 17e39f7 | 2006-02-23 15:54:28 +0000 | [diff] [blame] | 2211 | int subcall_ret; |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 2212 | PTPObjectInfo new_track; |
Linus Walleij | 9b28da3 | 2006-03-16 13:47:58 +0000 | [diff] [blame] | 2213 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 2214 | uint32_t localph = parenthandle; |
Linus Walleij | d214b9b | 2006-08-26 22:08:37 +0000 | [diff] [blame] | 2215 | PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo; |
Linus Walleij | 16c51f0 | 2006-05-04 13:20:22 +0000 | [diff] [blame] | 2216 | |
Linus Walleij | 05ccbe7 | 2006-06-13 07:46:58 +0000 | [diff] [blame] | 2217 | if (localph == 0) { |
| 2218 | localph = device->default_music_folder; |
| 2219 | } |
| 2220 | |
Linus Walleij | 16c51f0 | 2006-05-04 13:20:22 +0000 | [diff] [blame] | 2221 | // Sanity check, is this really a track? |
| 2222 | if (metadata->filetype != LIBMTP_FILETYPE_WAV && |
| 2223 | metadata->filetype != LIBMTP_FILETYPE_MP3 && |
| 2224 | metadata->filetype != LIBMTP_FILETYPE_WMA && |
| 2225 | metadata->filetype != LIBMTP_FILETYPE_OGG && |
| 2226 | metadata->filetype != LIBMTP_FILETYPE_MP4 && |
| 2227 | metadata->filetype != LIBMTP_FILETYPE_UNDEF_AUDIO) { |
| 2228 | printf("LIBMTP_Send_Track_From_File_Descriptor: I don't think this is actually a track, strange filetype...\n"); |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 2229 | } |
Linus Walleij | 16c51f0 | 2006-05-04 13:20:22 +0000 | [diff] [blame] | 2230 | |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 2231 | new_track.Filename = metadata->filename; |
| 2232 | new_track.ObjectCompressedSize = metadata->filesize; |
Linus Walleij | ab1827c | 2006-06-01 20:17:11 +0000 | [diff] [blame] | 2233 | new_track.ObjectFormat = map_libmtp_type_to_ptp_type(metadata->filetype); |
Linus Walleij | fcf8891 | 2006-06-05 13:23:33 +0000 | [diff] [blame] | 2234 | |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 2235 | // Create the object |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 2236 | ret = ptp_sendobjectinfo(params, &store, &localph, &metadata->item_id, &new_track); |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 2237 | if (ret != PTP_RC_OK) { |
Linus Walleij | 9b28da3 | 2006-03-16 13:47:58 +0000 | [diff] [blame] | 2238 | ptp_perror(params, ret); |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 2239 | printf("LIBMTP_Send_Track_From_File_Descriptor: Could not send object info\n"); |
Linus Walleij | da9500d | 2006-08-30 13:17:06 +0000 | [diff] [blame] | 2240 | printf("Return code: 0x%04x (look this up in ptp.h for an explanation).\n", ret); |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 2241 | return -1; |
| 2242 | } |
| 2243 | |
Linus Walleij | ee73ef2 | 2006-08-27 19:56:00 +0000 | [diff] [blame] | 2244 | // Callbacks |
Linus Walleij | d214b9b | 2006-08-26 22:08:37 +0000 | [diff] [blame] | 2245 | ptp_usb->callback_active = 1; |
Linus Walleij | 7f0c72e | 2006-09-06 13:01:58 +0000 | [diff] [blame] | 2246 | // The callback will deactivate itself after this amount of data has been sent |
| 2247 | // One BULK header for the request, one for the data phase. No parameters to the request. |
| 2248 | ptp_usb->current_transfer_total = metadata->filesize+PTP_USB_BULK_HDR_LEN*2; |
Linus Walleij | d214b9b | 2006-08-26 22:08:37 +0000 | [diff] [blame] | 2249 | ptp_usb->current_transfer_complete = 0; |
| 2250 | ptp_usb->current_transfer_callback = callback; |
| 2251 | ptp_usb->current_transfer_callback_data = data; |
| 2252 | |
Linus Walleij | e7f44be | 2006-08-25 19:32:29 +0000 | [diff] [blame] | 2253 | ret = ptp_sendobject_fromfd(params, fd, metadata->filesize); |
Linus Walleij | ee73ef2 | 2006-08-27 19:56:00 +0000 | [diff] [blame] | 2254 | |
| 2255 | ptp_usb->callback_active = 0; |
| 2256 | ptp_usb->current_transfer_callback = NULL; |
| 2257 | ptp_usb->current_transfer_callback_data = NULL; |
| 2258 | |
Linus Walleij | e7f44be | 2006-08-25 19:32:29 +0000 | [diff] [blame] | 2259 | if (ret != PTP_RC_OK) { |
| 2260 | ptp_perror(params, ret); |
| 2261 | printf("LIBMTP_Send_Track_From_File_Descriptor: Could not send object\n"); |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 2262 | return -1; |
| 2263 | } |
Linus Walleij | ee73ef2 | 2006-08-27 19:56:00 +0000 | [diff] [blame] | 2264 | |
Linus Walleij | 17e39f7 | 2006-02-23 15:54:28 +0000 | [diff] [blame] | 2265 | // Set track metadata for the new fine track |
| 2266 | subcall_ret = LIBMTP_Update_Track_Metadata(device, metadata); |
| 2267 | if (subcall_ret != 0) { |
| 2268 | printf("LIBMTP_Send_Track_From_File_Descriptor: error setting metadata for new track\n"); |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 2269 | (void) LIBMTP_Delete_Object(device, metadata->item_id); |
Linus Walleij | 17e39f7 | 2006-02-23 15:54:28 +0000 | [diff] [blame] | 2270 | return -1; |
| 2271 | } |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 2272 | |
| 2273 | // Added object so flush handles |
| 2274 | flush_handles(device); |
Linus Walleij | 17e39f7 | 2006-02-23 15:54:28 +0000 | [diff] [blame] | 2275 | |
| 2276 | return 0; |
| 2277 | } |
| 2278 | |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 2279 | /** |
| 2280 | * This function sends a local file to an MTP device. |
| 2281 | * A filename and a set of metadata must be |
| 2282 | * given as input. |
| 2283 | * @param device a pointer to the device to send the track to. |
| 2284 | * @param path the filename of a local file which will be sent. |
| 2285 | * @param filedata a file strtuct to pass in info about the file. |
| 2286 | * After this call the field <code>item_id</code> |
| 2287 | * will contain the new file ID. |
| 2288 | * @param callback a progress indicator function or NULL to ignore. |
| 2289 | * @param data a user-defined pointer that is passed along to |
| 2290 | * the <code>progress</code> function in order to |
| 2291 | * pass along some user defined data to the progress |
| 2292 | * updates. If not used, set this to NULL. |
| 2293 | * @param parenthandle the parent (e.g. folder) to store this file |
Linus Walleij | d7aa5b2 | 2006-09-02 11:52:31 +0000 | [diff] [blame] | 2294 | * in. Since some devices are a bit picky about where files |
| 2295 | * are placed, a default folder will be chosen if libmtp |
| 2296 | * has detected one for the current filetype and this |
| 2297 | * parameter is set to 0. If this is 0 and no default folder |
| 2298 | * can be found, the file will be stored in the root folder. |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 2299 | * @return 0 if the transfer was successful, any other value means |
| 2300 | * failure. |
| 2301 | * @see LIBMTP_Send_File_From_File_Descriptor() |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 2302 | * @see LIBMTP_Delete_Object() |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 2303 | */ |
| 2304 | int LIBMTP_Send_File_From_File(LIBMTP_mtpdevice_t *device, |
| 2305 | char const * const path, LIBMTP_file_t * const filedata, |
Linus Walleij | ee73ef2 | 2006-08-27 19:56:00 +0000 | [diff] [blame] | 2306 | LIBMTP_progressfunc_t const callback, |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 2307 | void const * const data, uint32_t const parenthandle) |
| 2308 | { |
| 2309 | int fd; |
| 2310 | int ret; |
| 2311 | |
| 2312 | // Sanity check |
| 2313 | if (path == NULL) { |
| 2314 | printf("LIBMTP_Send_File_From_File(): Bad arguments, path was NULL\n"); |
| 2315 | return -1; |
| 2316 | } |
| 2317 | |
| 2318 | // Open file |
| 2319 | #ifdef __WIN32__ |
| 2320 | if ( (fd = open(path, O_RDONLY|O_BINARY) == -1 ) { |
| 2321 | #else |
| 2322 | if ( (fd = open(path, O_RDONLY)) == -1) { |
| 2323 | #endif |
| 2324 | printf("LIBMTP_Send_File_From_File(): Could not open source file \"%s\"\n", path); |
| 2325 | return -1; |
| 2326 | } |
| 2327 | |
| 2328 | ret = LIBMTP_Send_File_From_File_Descriptor(device, fd, filedata, callback, data, parenthandle); |
| 2329 | |
| 2330 | // Close file. |
| 2331 | close(fd); |
| 2332 | |
| 2333 | return ret; |
| 2334 | } |
| 2335 | |
Linus Walleij | d208f9c | 2006-04-27 14:16:06 +0000 | [diff] [blame] | 2336 | /** |
| 2337 | * This function sends a generic file from a file descriptor to an |
| 2338 | * MTP device. A filename and a set of metadata must be |
| 2339 | * given as input. |
Linus Walleij | cd3eb3d | 2006-09-02 21:33:22 +0000 | [diff] [blame] | 2340 | * |
| 2341 | * This can potentially be used for sending in a stream of unknown |
| 2342 | * length. Set <code>filedata->filesize = (uint64_t) -1</code> to |
| 2343 | * make libmtp send some dummy length to the device and just |
| 2344 | * accept a stream up to some device-determined max length. There |
| 2345 | * is not guarantee this will work on all devices... Remember to |
| 2346 | * set correct metadata for the track with |
| 2347 | * <code>LIBMTP_Update_Track_Metadata()</code> afterwards if it's |
| 2348 | * a music file. (This doesn't seem to work very well right now.) |
| 2349 | * |
Linus Walleij | d208f9c | 2006-04-27 14:16:06 +0000 | [diff] [blame] | 2350 | * @param device a pointer to the device to send the file to. |
| 2351 | * @param fd the filedescriptor for a local file which will be sent. |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 2352 | * @param filedata a file strtuct to pass in info about the file. |
Linus Walleij | d208f9c | 2006-04-27 14:16:06 +0000 | [diff] [blame] | 2353 | * After this call the field <code>item_id</code> |
| 2354 | * will contain the new track ID. |
| 2355 | * @param callback a progress indicator function or NULL to ignore. |
| 2356 | * @param data a user-defined pointer that is passed along to |
| 2357 | * the <code>progress</code> function in order to |
| 2358 | * pass along some user defined data to the progress |
| 2359 | * updates. If not used, set this to NULL. |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 2360 | * @param parenthandle the parent (e.g. folder) to store this file |
Linus Walleij | d7aa5b2 | 2006-09-02 11:52:31 +0000 | [diff] [blame] | 2361 | * in. Since some devices are a bit picky about where files |
| 2362 | * are placed, a default folder will be chosen if libmtp |
| 2363 | * has detected one for the current filetype and this |
| 2364 | * parameter is set to 0. If this is 0 and no default folder |
| 2365 | * can be found, the file will be stored in the root folder. |
Linus Walleij | d208f9c | 2006-04-27 14:16:06 +0000 | [diff] [blame] | 2366 | * @return 0 if the transfer was successful, any other value means |
| 2367 | * failure. |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 2368 | * @see LIBMTP_Send_File_From_File() |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 2369 | * @see LIBMTP_Delete_Object() |
Linus Walleij | d208f9c | 2006-04-27 14:16:06 +0000 | [diff] [blame] | 2370 | */ |
| 2371 | int LIBMTP_Send_File_From_File_Descriptor(LIBMTP_mtpdevice_t *device, |
| 2372 | int const fd, LIBMTP_file_t * const filedata, |
Linus Walleij | ee73ef2 | 2006-08-27 19:56:00 +0000 | [diff] [blame] | 2373 | LIBMTP_progressfunc_t const callback, |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 2374 | void const * const data, uint32_t const parenthandle) |
Linus Walleij | d208f9c | 2006-04-27 14:16:06 +0000 | [diff] [blame] | 2375 | { |
| 2376 | uint16_t ret; |
| 2377 | uint32_t store = 0; |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 2378 | uint32_t localph = parenthandle; |
Linus Walleij | d208f9c | 2006-04-27 14:16:06 +0000 | [diff] [blame] | 2379 | PTPObjectInfo new_file; |
| 2380 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | d214b9b | 2006-08-26 22:08:37 +0000 | [diff] [blame] | 2381 | PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo; |
Linus Walleij | 05ccbe7 | 2006-06-13 07:46:58 +0000 | [diff] [blame] | 2382 | |
Linus Walleij | d208f9c | 2006-04-27 14:16:06 +0000 | [diff] [blame] | 2383 | new_file.Filename = filedata->filename; |
Linus Walleij | cd3eb3d | 2006-09-02 21:33:22 +0000 | [diff] [blame] | 2384 | if (filedata->filesize == (uint64_t) -1) { |
| 2385 | // This is a stream. Set a dummy length... |
| 2386 | new_file.ObjectCompressedSize = 1; |
| 2387 | } else { |
| 2388 | new_file.ObjectCompressedSize = filedata->filesize; |
| 2389 | } |
Linus Walleij | 16c51f0 | 2006-05-04 13:20:22 +0000 | [diff] [blame] | 2390 | new_file.ObjectFormat = map_libmtp_type_to_ptp_type(filedata->filetype); |
Linus Walleij | d208f9c | 2006-04-27 14:16:06 +0000 | [diff] [blame] | 2391 | |
Linus Walleij | 05ccbe7 | 2006-06-13 07:46:58 +0000 | [diff] [blame] | 2392 | /* |
| 2393 | * If no destination folder was given, look up a default |
| 2394 | * folder if possible. Perhaps there is some way of retrieveing |
| 2395 | * the default folder for different forms of content, what |
| 2396 | * do I know, we use a fixed list in lack of any better method. |
| 2397 | * Some devices obviously need to have their files in certain |
| 2398 | * folders in order to find/display them at all (hello Creative), |
| 2399 | * so we have to have a method for this. |
| 2400 | */ |
| 2401 | |
| 2402 | if (localph == 0) { |
| 2403 | uint16_t of = new_file.ObjectFormat; |
| 2404 | if (of == PTP_OFC_WAV || |
| 2405 | of == PTP_OFC_MP3 || |
| 2406 | of == PTP_OFC_MTP_WMA || |
| 2407 | of == PTP_OFC_MTP_OGG || |
| 2408 | of == PTP_OFC_MTP_MP4 || |
| 2409 | of == PTP_OFC_MTP_UndefinedAudio) { |
| 2410 | localph = device->default_music_folder; |
| 2411 | } else if (of == PTP_OFC_MTP_WMV || |
| 2412 | of == PTP_OFC_AVI || |
| 2413 | of == PTP_OFC_MPEG || |
| 2414 | of == PTP_OFC_ASF || |
| 2415 | of == PTP_OFC_QT || |
| 2416 | of == PTP_OFC_MTP_UndefinedVideo) { |
| 2417 | localph = device->default_video_folder; |
| 2418 | } else if (of == PTP_OFC_EXIF_JPEG || |
| 2419 | of == PTP_OFC_JFIF || |
| 2420 | of == PTP_OFC_TIFF || |
| 2421 | of == PTP_OFC_BMP || |
| 2422 | of == PTP_OFC_GIF || |
| 2423 | of == PTP_OFC_PICT || |
| 2424 | of == PTP_OFC_PNG || |
| 2425 | of == PTP_OFC_MTP_WindowsImageFormat) { |
| 2426 | localph = device->default_picture_folder; |
| 2427 | } else if (of == PTP_OFC_MTP_vCalendar1 || |
Linus Walleij | d7aa5b2 | 2006-09-02 11:52:31 +0000 | [diff] [blame] | 2428 | of == PTP_OFC_MTP_vCalendar2 || |
| 2429 | of == PTP_OFC_MTP_UndefinedContact || |
| 2430 | of == PTP_OFC_MTP_vCard2 || |
| 2431 | of == PTP_OFC_MTP_vCard3 || |
| 2432 | of == PTP_OFC_MTP_UndefinedCalendarItem) { |
Linus Walleij | 05ccbe7 | 2006-06-13 07:46:58 +0000 | [diff] [blame] | 2433 | localph = device->default_organizer_folder; |
| 2434 | } |
| 2435 | } |
| 2436 | |
Linus Walleij | d208f9c | 2006-04-27 14:16:06 +0000 | [diff] [blame] | 2437 | // Create the object |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 2438 | ret = ptp_sendobjectinfo(params, &store, &localph, &filedata->item_id, &new_file); |
Linus Walleij | d208f9c | 2006-04-27 14:16:06 +0000 | [diff] [blame] | 2439 | if (ret != PTP_RC_OK) { |
| 2440 | ptp_perror(params, ret); |
| 2441 | printf("LIBMTP_Send_File_From_File_Descriptor: Could not send object info\n"); |
Linus Walleij | da9500d | 2006-08-30 13:17:06 +0000 | [diff] [blame] | 2442 | printf("Return code: 0x%04x (look this up in ptp.h for an explanation).\n", ret); |
Linus Walleij | d208f9c | 2006-04-27 14:16:06 +0000 | [diff] [blame] | 2443 | return -1; |
| 2444 | } |
Linus Walleij | ee73ef2 | 2006-08-27 19:56:00 +0000 | [diff] [blame] | 2445 | |
Linus Walleij | cd3eb3d | 2006-09-02 21:33:22 +0000 | [diff] [blame] | 2446 | if (filedata->filesize != (uint64_t) -1) { |
| 2447 | // Callbacks |
| 2448 | ptp_usb->callback_active = 1; |
Linus Walleij | 7f0c72e | 2006-09-06 13:01:58 +0000 | [diff] [blame] | 2449 | // The callback will deactivate itself after this amount of data has been sent |
| 2450 | // One BULK header for the request, one for the data phase. No parameters to the request. |
| 2451 | ptp_usb->current_transfer_total = filedata->filesize+PTP_USB_BULK_HDR_LEN*2; |
Linus Walleij | cd3eb3d | 2006-09-02 21:33:22 +0000 | [diff] [blame] | 2452 | ptp_usb->current_transfer_complete = 0; |
| 2453 | ptp_usb->current_transfer_callback = callback; |
| 2454 | ptp_usb->current_transfer_callback_data = data; |
| 2455 | |
| 2456 | ret = ptp_sendobject_fromfd(params, fd, filedata->filesize); |
Linus Walleij | d214b9b | 2006-08-26 22:08:37 +0000 | [diff] [blame] | 2457 | |
Linus Walleij | cd3eb3d | 2006-09-02 21:33:22 +0000 | [diff] [blame] | 2458 | ptp_usb->callback_active = 0; |
| 2459 | ptp_usb->current_transfer_callback = NULL; |
| 2460 | ptp_usb->current_transfer_callback_data = NULL; |
| 2461 | } else { |
| 2462 | // This is a stream.. |
Linus Walleij | a9310fa | 2006-09-04 06:47:42 +0000 | [diff] [blame] | 2463 | ret = ptp_sendobject_fromfd(params, fd, 0xFFFFFFFFU-PTP_USB_BULK_HDR_LEN); |
Linus Walleij | cd3eb3d | 2006-09-02 21:33:22 +0000 | [diff] [blame] | 2464 | if (ret == PTP_ERROR_IO) { |
| 2465 | // That's expected. The stream ends, simply... |
| 2466 | ret = PTP_RC_OK; |
| 2467 | } else { |
| 2468 | printf("LIBMTP_Send_File_From_File_Descriptor: Error while sending stream.\n"); |
| 2469 | printf("Return code: 0x%04x (look this up in ptp.h for an explanation).\n", ret); |
| 2470 | } |
| 2471 | } |
Linus Walleij | ee73ef2 | 2006-08-27 19:56:00 +0000 | [diff] [blame] | 2472 | |
| 2473 | if (ret != PTP_RC_OK) { |
| 2474 | ptp_perror(params, ret); |
| 2475 | printf("LIBMTP_Send_File_From_File_Descriptor: Could not send object\n"); |
| 2476 | return -1; |
| 2477 | } |
Linus Walleij | d214b9b | 2006-08-26 22:08:37 +0000 | [diff] [blame] | 2478 | |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 2479 | // Added object so flush handles. |
| 2480 | flush_handles(device); |
Linus Walleij | d208f9c | 2006-04-27 14:16:06 +0000 | [diff] [blame] | 2481 | return 0; |
| 2482 | } |
| 2483 | |
Linus Walleij | 17e39f7 | 2006-02-23 15:54:28 +0000 | [diff] [blame] | 2484 | /** |
| 2485 | * This function updates the MTP object metadata on a single file |
| 2486 | * identified by an object ID. |
Linus Walleij | 95698cd | 2006-02-24 10:40:40 +0000 | [diff] [blame] | 2487 | * @param device a pointer to the device to update the track |
| 2488 | * metadata on. |
Linus Walleij | 17e39f7 | 2006-02-23 15:54:28 +0000 | [diff] [blame] | 2489 | * @param metadata a track metadata set to be written to the file. |
| 2490 | * notice that the <code>track_id</code> field of the |
| 2491 | * metadata structure must be correct so that the |
Linus Walleij | a498273 | 2006-02-24 15:46:02 +0000 | [diff] [blame] | 2492 | * function can update the right file. If some properties |
| 2493 | * of this metadata are set to NULL (strings) or 0 |
| 2494 | * (numerical values) they will be discarded and the |
| 2495 | * track will not be tagged with these blank values. |
Linus Walleij | 17e39f7 | 2006-02-23 15:54:28 +0000 | [diff] [blame] | 2496 | * @return 0 on success, any other value means failure. |
| 2497 | */ |
| 2498 | int LIBMTP_Update_Track_Metadata(LIBMTP_mtpdevice_t *device, |
| 2499 | LIBMTP_track_t const * const metadata) |
| 2500 | { |
Linus Walleij | 17e39f7 | 2006-02-23 15:54:28 +0000 | [diff] [blame] | 2501 | uint16_t ret; |
Linus Walleij | 00cf064 | 2006-07-26 20:40:59 +0000 | [diff] [blame] | 2502 | PTPParams *params = (PTPParams *) device->params; |
| 2503 | uint32_t i; |
| 2504 | uint16_t *props = NULL; |
| 2505 | uint32_t propcnt = 0; |
Linus Walleij | 17e39f7 | 2006-02-23 15:54:28 +0000 | [diff] [blame] | 2506 | |
Linus Walleij | 00cf064 | 2006-07-26 20:40:59 +0000 | [diff] [blame] | 2507 | // First see which properties can be set on this file format and apply accordingly |
| 2508 | // i.e only try to update this metadata for object tags that exist on the current player. |
| 2509 | ret = ptp_mtp_getobjectpropssupported (params, map_libmtp_type_to_ptp_type(metadata->filetype), &propcnt, &props); |
| 2510 | if (ret != PTP_RC_OK) { |
| 2511 | // Just bail out for now, nothing is ever set. |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 2512 | return -1; |
Linus Walleij | 00cf064 | 2006-07-26 20:40:59 +0000 | [diff] [blame] | 2513 | } else { |
| 2514 | for (i=0;i<propcnt;i++) { |
| 2515 | switch (props[i]) { |
| 2516 | case PTP_OPC_Name: |
| 2517 | // Update title |
Linus Walleij | a823a70 | 2006-08-27 21:27:46 +0000 | [diff] [blame] | 2518 | ret = LIBMTP_Set_Object_String(device, metadata->item_id, PTP_OPC_Name, metadata->title); |
Linus Walleij | 00cf064 | 2006-07-26 20:40:59 +0000 | [diff] [blame] | 2519 | if (ret != 0) { |
| 2520 | printf("LIBMTP_Update_Track_Metadata(): could not set track title\n"); |
| 2521 | return -1; |
| 2522 | } |
| 2523 | break; |
| 2524 | case PTP_OPC_AlbumName: |
| 2525 | // Update album |
Linus Walleij | a823a70 | 2006-08-27 21:27:46 +0000 | [diff] [blame] | 2526 | ret = LIBMTP_Set_Object_String(device, metadata->item_id, PTP_OPC_AlbumName, metadata->album); |
Linus Walleij | 00cf064 | 2006-07-26 20:40:59 +0000 | [diff] [blame] | 2527 | if (ret != 0) { |
| 2528 | printf("LIBMTP_Update_Track_Metadata(): could not set track album name\n"); |
| 2529 | return -1; |
| 2530 | } |
| 2531 | break; |
| 2532 | case PTP_OPC_Artist: |
| 2533 | // Update artist |
Linus Walleij | a823a70 | 2006-08-27 21:27:46 +0000 | [diff] [blame] | 2534 | ret = LIBMTP_Set_Object_String(device, metadata->item_id, PTP_OPC_Artist, metadata->artist); |
Linus Walleij | 00cf064 | 2006-07-26 20:40:59 +0000 | [diff] [blame] | 2535 | if (ret != 0) { |
| 2536 | printf("LIBMTP_Update_Track_Metadata(): could not set track artist name\n"); |
| 2537 | return -1; |
| 2538 | } |
| 2539 | break; |
| 2540 | case PTP_OPC_Genre: |
| 2541 | // Update genre |
Linus Walleij | a823a70 | 2006-08-27 21:27:46 +0000 | [diff] [blame] | 2542 | ret = LIBMTP_Set_Object_String(device, metadata->item_id, PTP_OPC_Genre, metadata->genre); |
Linus Walleij | 00cf064 | 2006-07-26 20:40:59 +0000 | [diff] [blame] | 2543 | if (ret != 0) { |
| 2544 | printf("LIBMTP_Update_Track_Metadata(): could not set track genre name\n"); |
| 2545 | return -1; |
| 2546 | } |
| 2547 | break; |
| 2548 | case PTP_OPC_Duration: |
| 2549 | // Update duration |
| 2550 | if (metadata->duration != 0) { |
| 2551 | ret = LIBMTP_Set_Object_U32(device, metadata->item_id, PTP_OPC_Duration, metadata->duration); |
| 2552 | if (ret != 0) { |
| 2553 | printf("LIBMTP_Update_Track_Metadata(): could not set track duration\n"); |
| 2554 | return -1; |
| 2555 | } |
| 2556 | } |
| 2557 | break; |
| 2558 | case PTP_OPC_Track: |
| 2559 | // Update track number. |
| 2560 | if (metadata->tracknumber != 0) { |
| 2561 | ret = LIBMTP_Set_Object_U16(device, metadata->item_id, PTP_OPC_Track, metadata->tracknumber); |
| 2562 | if (ret != 0) { |
| 2563 | printf("LIBMTP_Update_Track_Metadata(): could not set track tracknumber\n"); |
| 2564 | return -1; |
| 2565 | } |
| 2566 | } |
| 2567 | break; |
| 2568 | case PTP_OPC_OriginalReleaseDate: |
| 2569 | // Update creation datetime |
Linus Walleij | a823a70 | 2006-08-27 21:27:46 +0000 | [diff] [blame] | 2570 | ret = LIBMTP_Set_Object_String(device, metadata->item_id, PTP_OPC_OriginalReleaseDate, metadata->date); |
Linus Walleij | 00cf064 | 2006-07-26 20:40:59 +0000 | [diff] [blame] | 2571 | if (ret != 0) { |
| 2572 | printf("LIBMTP_Update_Track_Metadata(): could not set track release date\n"); |
| 2573 | return -1; |
| 2574 | } |
| 2575 | break; |
| 2576 | // These are, well not so important. |
| 2577 | case PTP_OPC_SampleRate: |
| 2578 | // Update sample rate |
| 2579 | if (metadata->samplerate != 0) { |
| 2580 | ret = LIBMTP_Set_Object_U32(device, metadata->item_id, PTP_OPC_SampleRate, metadata->samplerate); |
| 2581 | if (ret != 0) { |
| 2582 | printf("LIBMTP_Update_Track_Metadata(): could not set samplerate\n"); |
| 2583 | return -1; |
| 2584 | } |
| 2585 | } |
| 2586 | break; |
| 2587 | case PTP_OPC_NumberOfChannels: |
| 2588 | // Update number of channels |
| 2589 | if (metadata->nochannels != 0) { |
| 2590 | ret = LIBMTP_Set_Object_U16(device, metadata->item_id, PTP_OPC_NumberOfChannels, metadata->nochannels); |
| 2591 | if (ret != 0) { |
| 2592 | printf("LIBMTP_Update_Track_Metadata(): could not set number of channels\n"); |
| 2593 | return -1; |
| 2594 | } |
| 2595 | } |
| 2596 | break; |
| 2597 | case PTP_OPC_AudioWAVECodec: |
| 2598 | // Update WAVE codec |
| 2599 | if (metadata->wavecodec != 0) { |
| 2600 | ret = LIBMTP_Set_Object_U32(device, metadata->item_id, PTP_OPC_AudioWAVECodec, metadata->wavecodec); |
| 2601 | if (ret != 0) { |
| 2602 | printf("LIBMTP_Update_Track_Metadata(): could not set WAVE codec\n"); |
| 2603 | return -1; |
| 2604 | } |
| 2605 | } |
| 2606 | break; |
| 2607 | case PTP_OPC_AudioBitRate: |
| 2608 | // Update bitrate |
| 2609 | if (metadata->bitrate != 0) { |
| 2610 | ret = LIBMTP_Set_Object_U32(device, metadata->item_id, PTP_OPC_AudioBitRate, metadata->bitrate); |
| 2611 | if (ret != 0) { |
| 2612 | printf("LIBMTP_Update_Track_Metadata(): could not set bitrate\n"); |
| 2613 | return -1; |
| 2614 | } |
| 2615 | } |
| 2616 | break; |
| 2617 | case PTP_OPC_BitRateType: |
| 2618 | // Update bitrate type |
| 2619 | if (metadata->bitratetype != 0) { |
| 2620 | ret = LIBMTP_Set_Object_U16(device, metadata->item_id, PTP_OPC_BitRateType, metadata->bitratetype); |
| 2621 | if (ret != 0) { |
| 2622 | printf("LIBMTP_Update_Track_Metadata(): could not set bitratetype\n"); |
| 2623 | return -1; |
| 2624 | } |
| 2625 | } |
| 2626 | break; |
| 2627 | case PTP_OPC_Rating: |
| 2628 | // Update user rating |
| 2629 | // TODO: shall this be set for rating 0? |
| 2630 | if (metadata->rating != 0) { |
| 2631 | ret = LIBMTP_Set_Object_U16(device, metadata->item_id, PTP_OPC_Rating, metadata->rating); |
| 2632 | if (ret != 0) { |
| 2633 | printf("LIBMTP_Update_Track_Metadata(): could not set user rating\n"); |
| 2634 | return -1; |
| 2635 | } |
| 2636 | } |
| 2637 | break; |
| 2638 | case PTP_OPC_UseCount: |
| 2639 | // Update use count, set even to zero if desired. |
| 2640 | ret = LIBMTP_Set_Object_U32(device, metadata->item_id, PTP_OPC_UseCount, metadata->usecount); |
| 2641 | if (ret != 0) { |
| 2642 | printf("LIBMTP_Update_Track_Metadata(): could not set use count\n"); |
| 2643 | return -1; |
| 2644 | } |
| 2645 | break; |
Linus Walleij | 17e39f7 | 2006-02-23 15:54:28 +0000 | [diff] [blame] | 2646 | |
Linus Walleij | 00cf064 | 2006-07-26 20:40:59 +0000 | [diff] [blame] | 2647 | // NOTE: File size is not updated, this should not change anyway. |
| 2648 | // neither will we change the filename. |
| 2649 | } |
Linus Walleij | a498273 | 2006-02-24 15:46:02 +0000 | [diff] [blame] | 2650 | } |
Linus Walleij | 00cf064 | 2006-07-26 20:40:59 +0000 | [diff] [blame] | 2651 | return 0; |
Linus Walleij | 17e39f7 | 2006-02-23 15:54:28 +0000 | [diff] [blame] | 2652 | } |
Linus Walleij | 394bbbe | 2006-02-22 16:10:53 +0000 | [diff] [blame] | 2653 | } |
Linus Walleij | 95698cd | 2006-02-24 10:40:40 +0000 | [diff] [blame] | 2654 | |
| 2655 | /** |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 2656 | * This function deletes a single file, track, playlist or |
| 2657 | * any other object off the MTP device, |
Linus Walleij | 95698cd | 2006-02-24 10:40:40 +0000 | [diff] [blame] | 2658 | * identified by an object ID. |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 2659 | * @param device a pointer to the device to delete the file or track from. |
Linus Walleij | 95698cd | 2006-02-24 10:40:40 +0000 | [diff] [blame] | 2660 | * @param item_id the item to delete. |
| 2661 | * @return 0 on success, any other value means failure. |
| 2662 | */ |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 2663 | int LIBMTP_Delete_Object(LIBMTP_mtpdevice_t *device, |
| 2664 | uint32_t object_id) |
Linus Walleij | 95698cd | 2006-02-24 10:40:40 +0000 | [diff] [blame] | 2665 | { |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 2666 | uint16_t ret; |
| 2667 | PTPParams *params = (PTPParams *) device->params; |
| 2668 | |
| 2669 | ret = ptp_deleteobject(params, object_id, 0); |
| 2670 | if (ret != PTP_RC_OK) { |
| 2671 | ptp_perror(params, ret); |
| 2672 | printf("LIBMTP_Delete_Object(): could not delete object\n"); |
| 2673 | return -1; |
| 2674 | } |
| 2675 | // Removed object so flush handles. |
| 2676 | flush_handles(device); |
| 2677 | return 0; |
Linus Walleij | 95698cd | 2006-02-24 10:40:40 +0000 | [diff] [blame] | 2678 | } |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 2679 | |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 2680 | /** |
| 2681 | * Helper function. This indicates if a track exists on the device |
| 2682 | * @param device a pointer to the device to get the track from. |
| 2683 | * @param id the track ID of the track to retrieve. |
| 2684 | * @return TRUE (1) if the track exists, FALSE (0) if not |
| 2685 | */ |
| 2686 | int LIBMTP_Track_Exists(LIBMTP_mtpdevice_t *device, |
| 2687 | uint32_t const id) |
| 2688 | { |
| 2689 | PTPObjectInfo oi; |
| 2690 | PTPParams *params = (PTPParams *) device->params; |
| 2691 | |
| 2692 | if (ptp_getobjectinfo(params, id, &oi) == PTP_RC_OK) { |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 2693 | return -1; |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 2694 | } |
| 2695 | return 0; |
| 2696 | } |
| 2697 | |
| 2698 | /** |
| 2699 | * This creates a new folder structure and allocates memory |
| 2700 | * for it. Notice that if you add strings to this structure they |
| 2701 | * will be freed by the corresponding <code>LIBMTP_folder_track_t</code> |
| 2702 | * operation later, so be careful of using strdup() when assigning |
| 2703 | * strings, e.g.: |
| 2704 | * |
| 2705 | * @return a pointer to the newly allocated folder structure. |
| 2706 | * @see LIBMTP_destroy_folder_t() |
| 2707 | */ |
| 2708 | LIBMTP_folder_t *LIBMTP_new_folder_t(void) |
| 2709 | { |
| 2710 | LIBMTP_folder_t *new = (LIBMTP_folder_t *) malloc(sizeof(LIBMTP_folder_t)); |
| 2711 | if (new == NULL) { |
| 2712 | return NULL; |
| 2713 | } |
| 2714 | new->folder_id = 0; |
| 2715 | new->parent_id = 0; |
| 2716 | new->name = NULL; |
| 2717 | new->sibling = NULL; |
| 2718 | new->child = NULL; |
| 2719 | return new; |
| 2720 | } |
| 2721 | |
| 2722 | /** |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 2723 | * |
| 2724 | * This deletes the memory for an object structure |
| 2725 | * and makes use of the registered destructor for the object |
| 2726 | * type data. |
| 2727 | * |
| 2728 | * @param object object structure to destroy |
| 2729 | * @param recurse indicate if the call should recursively delete |
| 2730 | * the object. Specify 1 for recursion. |
| 2731 | * @see LIBMTP_new_object_t() |
| 2732 | */ |
| 2733 | void LIBMTP_destroy_object_t(LIBMTP_object_t *object, uint32_t recursive) |
| 2734 | { |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 2735 | if(object == NULL) { |
| 2736 | return; |
| 2737 | } |
| 2738 | |
| 2739 | //Destroy from the bottom up |
| 2740 | if(recursive==1) { |
| 2741 | LIBMTP_destroy_object_t(object->child, recursive); |
| 2742 | object->child = NULL; |
| 2743 | LIBMTP_destroy_object_t(object->sibling, recursive); |
| 2744 | object->sibling = NULL; |
| 2745 | } |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 2746 | |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 2747 | if(object->name != NULL) free(object->name); |
| 2748 | |
| 2749 | //Use the data type destructor |
| 2750 | if(object->data != NULL) { |
| 2751 | void (*destructor)(void *); |
| 2752 | |
| 2753 | destructor = get_destructor(object->type); |
| 2754 | |
| 2755 | if(destructor != NULL) { |
| 2756 | (*destructor)(object->data); |
| 2757 | } |
| 2758 | object->data = NULL; |
| 2759 | } |
| 2760 | |
| 2761 | free(object); |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 2762 | } |
| 2763 | |
| 2764 | /** |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 2765 | * This recursively deletes the memory for a folder structure |
| 2766 | * |
| 2767 | * @param folder folder structure to destroy |
| 2768 | * @see LIBMTP_new_folder_t() |
| 2769 | */ |
| 2770 | void LIBMTP_destroy_folder_t(LIBMTP_folder_t *folder) |
| 2771 | { |
| 2772 | |
| 2773 | if(folder == NULL) { |
| 2774 | return; |
| 2775 | } |
| 2776 | |
| 2777 | //Destroy from the bottom up |
| 2778 | if(folder->child != NULL) { |
| 2779 | LIBMTP_destroy_folder_t(folder->child); |
| 2780 | } |
| 2781 | |
| 2782 | if(folder->sibling != NULL) { |
| 2783 | LIBMTP_destroy_folder_t(folder->sibling); |
| 2784 | } |
| 2785 | |
| 2786 | if(folder->name != NULL) { |
| 2787 | free(folder->name); |
| 2788 | } |
| 2789 | |
| 2790 | free(folder); |
| 2791 | } |
| 2792 | |
| 2793 | /** |
| 2794 | * Helper function. Returns a folder structure for a |
| 2795 | * specified id. |
| 2796 | * |
| 2797 | * @param folderlist list of folders to search |
| 2798 | * @id id of folder to look for |
| 2799 | * @return a folder or NULL if not found |
| 2800 | */ |
| 2801 | LIBMTP_folder_t *LIBMTP_Find_Folder(LIBMTP_folder_t *folderlist, uint32_t id) |
| 2802 | { |
| 2803 | LIBMTP_folder_t *ret = NULL; |
| 2804 | |
| 2805 | if(folderlist == NULL) { |
| 2806 | return NULL; |
| 2807 | } |
| 2808 | |
| 2809 | if(folderlist->folder_id == id) { |
| 2810 | return folderlist; |
| 2811 | } |
| 2812 | |
| 2813 | if(folderlist->sibling) { |
| 2814 | ret = LIBMTP_Find_Folder(folderlist->sibling, id); |
| 2815 | } |
| 2816 | |
| 2817 | if(folderlist->child && ret == NULL) { |
| 2818 | ret = LIBMTP_Find_Folder(folderlist->child, id); |
| 2819 | } |
| 2820 | |
| 2821 | return ret; |
| 2822 | } |
| 2823 | |
| 2824 | /** |
| 2825 | * This returns a list of all folders available |
| 2826 | * on the current MTP device. |
| 2827 | * |
| 2828 | * @param device a pointer to the device to get the track listing for. |
| 2829 | * @return a list of folders |
| 2830 | */ |
| 2831 | LIBMTP_folder_t *LIBMTP_Get_Folder_List(LIBMTP_mtpdevice_t *device) |
| 2832 | { |
| 2833 | uint32_t i = 0; |
| 2834 | LIBMTP_folder_t *retfolders = NULL; |
| 2835 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 2836 | |
| 2837 | // Get all the handles if we haven't already done that |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 2838 | if (params->handles.Handler == NULL) { |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 2839 | flush_handles(device); |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 2840 | } |
| 2841 | |
| 2842 | for (i = 0; i < params->handles.n; i++) { |
| 2843 | LIBMTP_folder_t *folder; |
| 2844 | PTPObjectInfo oi; |
| 2845 | |
| 2846 | if (ptp_getobjectinfo(params, params->handles.Handler[i], &oi) == PTP_RC_OK) { |
| 2847 | if (oi.ObjectFormat != PTP_OFC_Association) { |
| 2848 | continue; |
| 2849 | } |
| 2850 | folder = LIBMTP_new_folder_t(); |
| 2851 | folder->folder_id = params->handles.Handler[i]; |
| 2852 | folder->parent_id = oi.ParentObject; |
| 2853 | folder->name = (char *)strdup(oi.Filename); |
| 2854 | |
| 2855 | // Work out where to put this new item |
| 2856 | if(retfolders == NULL) { |
| 2857 | retfolders = folder; |
| 2858 | continue; |
| 2859 | } else { |
| 2860 | LIBMTP_folder_t *parent_folder; |
| 2861 | LIBMTP_folder_t *current_folder; |
| 2862 | |
| 2863 | parent_folder = LIBMTP_Find_Folder(retfolders, folder->parent_id); |
| 2864 | |
| 2865 | if(parent_folder == NULL) { |
| 2866 | current_folder = retfolders; |
| 2867 | } else { |
| 2868 | if(parent_folder->child == NULL) { |
| 2869 | parent_folder->child = folder; |
| 2870 | continue; |
| 2871 | } else { |
| 2872 | current_folder = parent_folder->child; |
| 2873 | } |
| 2874 | } |
| 2875 | |
| 2876 | while(current_folder->sibling != NULL) { |
| 2877 | current_folder=current_folder->sibling; |
| 2878 | } |
| 2879 | |
| 2880 | current_folder->sibling = folder; |
| 2881 | } |
| 2882 | } |
| 2883 | } |
| 2884 | return retfolders; |
| 2885 | } |
| 2886 | |
| 2887 | /** |
Linus Walleij | c86afbd | 2006-05-04 19:05:24 +0000 | [diff] [blame] | 2888 | * This create a folder on the current MTP device. The PTP name |
| 2889 | * for a folder is "association". The PTP/MTP devices does not |
| 2890 | * have an internal "folder" concept really, it contains a flat |
| 2891 | * list of all files and some file are "associations" that other |
| 2892 | * files and folders may refer to as its "parent". |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 2893 | * |
Linus Walleij | c86afbd | 2006-05-04 19:05:24 +0000 | [diff] [blame] | 2894 | * @param device a pointer to the device to create the folder on. |
| 2895 | * @param name the name of the new folder. |
| 2896 | * @param parent_id id of parent folder to add the new folder to, |
| 2897 | * or 0 to put it in the root directory. |
| 2898 | * @return id to new folder or 0 if an error occured |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 2899 | */ |
| 2900 | uint32_t LIBMTP_Create_Folder(LIBMTP_mtpdevice_t *device, char *name, uint32_t parent_id) |
| 2901 | { |
| 2902 | PTPParams *params = (PTPParams *) device->params; |
| 2903 | uint32_t parenthandle = 0; |
| 2904 | uint32_t store = 0; |
| 2905 | PTPObjectInfo new_folder; |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 2906 | uint16_t ret; |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 2907 | uint32_t new_id = 0; |
| 2908 | |
| 2909 | memset(&new_folder, 0, sizeof(new_folder)); |
| 2910 | new_folder.Filename = name; |
| 2911 | new_folder.ObjectCompressedSize = 1; |
| 2912 | new_folder.ObjectFormat = PTP_OFC_Association; |
| 2913 | new_folder.ParentObject = parent_id; |
| 2914 | |
| 2915 | parenthandle = parent_id; |
| 2916 | // Create the object |
| 2917 | ret = ptp_sendobjectinfo(params, &store, &parenthandle, &new_id, &new_folder); |
| 2918 | if (ret != PTP_RC_OK) { |
| 2919 | ptp_perror(params, ret); |
| 2920 | printf("LIBMTP_Create_Folder: Could not send object info\n"); |
Linus Walleij | da9500d | 2006-08-30 13:17:06 +0000 | [diff] [blame] | 2921 | printf("Return code: 0x%04x (look this up in ptp.h for an explanation).\n", ret); |
Linus Walleij | c86afbd | 2006-05-04 19:05:24 +0000 | [diff] [blame] | 2922 | return 0; |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 2923 | } |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 2924 | // Created new object so flush handles |
| 2925 | flush_handles(device); |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 2926 | return new_id; |
| 2927 | } |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 2928 | |
| 2929 | |
| 2930 | /** |
| 2931 | * Helper function. Returns a folder structure for a |
| 2932 | * specified id. |
| 2933 | * |
| 2934 | * @param objectlist list of objects to search |
| 2935 | * @id id of object to look for |
| 2936 | * @return a object or NULL if not found |
| 2937 | */ |
| 2938 | LIBMTP_object_t *LIBMTP_Find_Object(LIBMTP_object_t *objectlist, uint32_t id) |
| 2939 | { |
| 2940 | LIBMTP_object_t *ret = NULL; |
| 2941 | |
| 2942 | if(objectlist == NULL) { |
| 2943 | return NULL; |
| 2944 | } |
| 2945 | |
| 2946 | if(objectlist->id == id) { |
| 2947 | return objectlist; |
| 2948 | } |
| 2949 | |
| 2950 | if(objectlist->sibling) { |
| 2951 | ret = LIBMTP_Find_Object(objectlist->sibling, id); |
| 2952 | } |
| 2953 | |
| 2954 | if(objectlist->child && ret == NULL) { |
| 2955 | ret = LIBMTP_Find_Object(objectlist->child, id); |
| 2956 | } |
| 2957 | |
| 2958 | return ret; |
| 2959 | } |
| 2960 | |
| 2961 | /** |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 2962 | * This returns a list of objects on the current MTP device, |
| 2963 | * selected by a filter based on PTP object ID:s. |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 2964 | * |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 2965 | * @param device a pointer to the device to get the object listing for. |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 2966 | * @param filter array of unsigned 32-bit integers specifying which types |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 2967 | * to include in the list |
| 2968 | * @param filter_len length of filter array in 32-bit words |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 2969 | * @param exclusions array of unsigned 32-bit integers specifying which types |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 2970 | * to exclude from the list |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 2971 | * @param exclusion_len length of exclusion array |
| 2972 | * @return a list of objects |
| 2973 | * @see LIBMTP_destroy_object_t() |
| 2974 | */ |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 2975 | LIBMTP_object_t *LIBMTP_Make_List(LIBMTP_mtpdevice_t *device, uint32_t *filter, |
| 2976 | uint32_t filter_len, uint32_t *exclusions, uint32_t exclusion_len) |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 2977 | { |
| 2978 | uint32_t i = 0; |
| 2979 | LIBMTP_object_t *objectlist = NULL; |
| 2980 | PTPParams *params = (PTPParams *) device->params; |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 2981 | uint32_t max_exclusions = 0; |
| 2982 | uint32_t max_filter = 0; |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 2983 | |
| 2984 | // Get all the handles if we haven't already done that |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 2985 | if (params->handles.Handler == NULL) { |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 2986 | flush_handles(device); |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 2987 | } |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 2988 | |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 2989 | if(filter != NULL) max_filter = filter_len; |
| 2990 | if(exclusions != NULL) max_exclusions = exclusion_len; |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 2991 | |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 2992 | for (i = 0; i < params->handles.n; i++) { |
| 2993 | LIBMTP_object_t *object; |
| 2994 | PTPObjectInfo oi; |
| 2995 | |
| 2996 | if (ptp_getobjectinfo(params, params->handles.Handler[i], &oi) == PTP_RC_OK) { |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 2997 | uint32_t x = 0; |
| 2998 | uint32_t exclude = 0, filter_allow = 0; |
| 2999 | void (*datafunc)(LIBMTP_mtpdevice_t *, uint32_t, void *); |
| 3000 | void *(*constructor)(void); |
| 3001 | |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 3002 | // Is the ObjectFormat in the list of exclusions ? |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 3003 | for(x = 0; x < max_exclusions; x++) { |
| 3004 | if (oi.ObjectFormat == exclusions[x]) { |
| 3005 | exclude = 1; |
| 3006 | break; |
| 3007 | } |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 3008 | } |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 3009 | if(exclude == 1) { |
| 3010 | continue; |
| 3011 | } |
| 3012 | |
| 3013 | // Is the ObjectFormat in the filter ? |
| 3014 | for(x = 0; x < max_filter; x++) { |
| 3015 | if (oi.ObjectFormat == filter[x]) { |
| 3016 | filter_allow = 1; |
| 3017 | break; |
| 3018 | } |
| 3019 | } |
| 3020 | if(filter_allow == 0) { |
| 3021 | continue; |
| 3022 | } |
| 3023 | |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 3024 | object = LIBMTP_new_object_t(); |
| 3025 | object->id = params->handles.Handler[i]; |
| 3026 | object->parent = oi.ParentObject; |
| 3027 | object->name = (char *)strdup(oi.Filename); |
| 3028 | object->size = oi.ObjectCompressedSize; |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 3029 | object->type = oi.ObjectFormat; |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 3030 | |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 3031 | // Get the function pointers for the constructor and datafunc |
| 3032 | constructor = get_constructor(oi.ObjectFormat); |
| 3033 | datafunc = get_datafunc(oi.ObjectFormat); |
| 3034 | |
| 3035 | if(constructor != NULL) { |
| 3036 | object->data = (*constructor)(); |
| 3037 | if(datafunc != NULL) { |
| 3038 | (*datafunc)(device, object->id, object->data); |
| 3039 | } |
| 3040 | } |
| 3041 | |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 3042 | // Work out where to put this new item |
| 3043 | if(objectlist == NULL) { |
| 3044 | objectlist = object; |
| 3045 | continue; |
| 3046 | } else { |
| 3047 | LIBMTP_object_t *parent_object; |
| 3048 | LIBMTP_object_t *current_object; |
| 3049 | |
| 3050 | parent_object = LIBMTP_Find_Object(objectlist, object->parent); |
| 3051 | |
| 3052 | if(parent_object == NULL) { |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 3053 | current_object = objectlist; |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 3054 | } else { |
| 3055 | if(parent_object->child == NULL) { |
| 3056 | parent_object->child = object; |
| 3057 | continue; |
| 3058 | } else { |
| 3059 | current_object = parent_object->child; |
| 3060 | } |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 3061 | } |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 3062 | |
| 3063 | while(current_object->sibling != NULL) { |
| 3064 | current_object=current_object->sibling; |
| 3065 | } |
| 3066 | current_object->sibling = object; |
| 3067 | } |
| 3068 | } |
| 3069 | } |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 3070 | |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 3071 | return objectlist; |
| 3072 | } |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 3073 | |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 3074 | /** |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 3075 | * Debug function that dumps out some textual representation |
| 3076 | * of an object list. |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 3077 | * |
| 3078 | * @param list object list returned from LIBMTP_Make_List |
| 3079 | * |
| 3080 | * @see LIBMTP_Make_List() |
| 3081 | */ |
| 3082 | void LIBMTP_Dump_List(LIBMTP_object_t *list) |
| 3083 | { |
Linus Walleij | f0f3d48 | 2006-05-29 14:10:21 +0000 | [diff] [blame] | 3084 | if(list == NULL) return; |
| 3085 | |
| 3086 | printf("Id : %u\n", list->id); |
| 3087 | printf("Parent: %u\n", list->parent); |
| 3088 | printf("Size : %u\n", list->size); |
| 3089 | printf("Name : %s\n", (list->name ? list->name : "")); |
| 3090 | printf("Type : 0x%04x\n", list->type); |
| 3091 | printf("--\n"); |
| 3092 | |
| 3093 | LIBMTP_Dump_List(list->child); |
| 3094 | LIBMTP_Dump_List(list->sibling); |
ravelox | d9a2864 | 2006-05-26 23:42:22 +0000 | [diff] [blame] | 3095 | } |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 3096 | |
| 3097 | /** |
| 3098 | * This creates a new playlist metadata structure and allocates memory |
| 3099 | * for it. Notice that if you add strings to this structure they |
| 3100 | * will be freed by the corresponding <code>LIBMTP_destroy_playlist_t</code> |
| 3101 | * operation later, so be careful of using strdup() when assigning |
| 3102 | * strings, e.g.: |
| 3103 | * |
| 3104 | * <pre> |
| 3105 | * LIBMTP_playlist_t *pl = LIBMTP_new_playlist_t(); |
| 3106 | * pl->name = strdup(str); |
| 3107 | * .... |
| 3108 | * LIBMTP_destroy_playlist_t(pl); |
| 3109 | * </pre> |
| 3110 | * |
| 3111 | * @return a pointer to the newly allocated metadata structure. |
| 3112 | * @see LIBMTP_destroy_playlist_t() |
| 3113 | */ |
| 3114 | LIBMTP_playlist_t *LIBMTP_new_playlist_t(void) |
| 3115 | { |
| 3116 | LIBMTP_playlist_t *new = (LIBMTP_playlist_t *) malloc(sizeof(LIBMTP_playlist_t)); |
| 3117 | if (new == NULL) { |
| 3118 | return NULL; |
| 3119 | } |
| 3120 | new->playlist_id = 0; |
| 3121 | new->name = NULL; |
| 3122 | new->tracks = NULL; |
| 3123 | new->no_tracks = 0; |
| 3124 | new->next = NULL; |
| 3125 | return new; |
| 3126 | } |
| 3127 | |
| 3128 | /** |
| 3129 | * This destroys a playlist metadata structure and deallocates the memory |
| 3130 | * used by it, including any strings. Never use a track metadata |
| 3131 | * structure again after calling this function on it. |
| 3132 | * @param playlist the playlist metadata to destroy. |
| 3133 | * @see LIBMTP_new_playlist_t() |
| 3134 | */ |
| 3135 | void LIBMTP_destroy_playlist_t(LIBMTP_playlist_t *playlist) |
| 3136 | { |
| 3137 | if (playlist == NULL) { |
| 3138 | return; |
| 3139 | } |
| 3140 | if (playlist->name != NULL) |
| 3141 | free(playlist->name); |
| 3142 | if (playlist->tracks != NULL) |
| 3143 | free(playlist->tracks); |
| 3144 | free(playlist); |
| 3145 | return; |
| 3146 | } |
| 3147 | |
| 3148 | /** |
| 3149 | * This function returns a list of the playlists available on the |
| 3150 | * device. Typical usage: |
| 3151 | * |
| 3152 | * <pre> |
| 3153 | * </pre> |
| 3154 | * |
| 3155 | * @param device a pointer to the device to get the playlist listing from. |
| 3156 | * @return a playlist list on success, else NULL. If there are no playlists |
| 3157 | * on the device, NULL will be returned as well. |
Linus Walleij | 2e4b5f9 | 2006-06-16 14:00:49 +0000 | [diff] [blame] | 3158 | * @see LIBMTP_Get_Playlist() |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 3159 | */ |
| 3160 | LIBMTP_playlist_t *LIBMTP_Get_Playlist_List(LIBMTP_mtpdevice_t *device) |
| 3161 | { |
| 3162 | PTPParams *params = (PTPParams *) device->params; |
| 3163 | LIBMTP_playlist_t *retlists = NULL; |
| 3164 | LIBMTP_playlist_t *curlist = NULL; |
| 3165 | uint32_t i; |
| 3166 | |
| 3167 | // Get all the handles if we haven't already done that |
| 3168 | if (params->handles.Handler == NULL) { |
| 3169 | flush_handles(device); |
| 3170 | } |
| 3171 | |
| 3172 | for (i = 0; i < params->handles.n; i++) { |
| 3173 | LIBMTP_playlist_t *pl; |
| 3174 | PTPObjectInfo oi; |
| 3175 | uint16_t ret; |
| 3176 | |
| 3177 | ret = ptp_getobjectinfo(params, params->handles.Handler[i], &oi); |
| 3178 | if ( ret == PTP_RC_OK) { |
| 3179 | |
| 3180 | // Ignore stuff that isn't playlists |
| 3181 | if ( oi.ObjectFormat != PTP_OFC_MTP_AbstractAudioVideoPlaylist ) { |
| 3182 | continue; |
| 3183 | } |
| 3184 | |
| 3185 | // Allocate a new playlist type |
| 3186 | pl = LIBMTP_new_playlist_t(); |
| 3187 | |
| 3188 | // Ignoring the io.Filename field. |
Linus Walleij | a823a70 | 2006-08-27 21:27:46 +0000 | [diff] [blame] | 3189 | pl->name = LIBMTP_Get_String_From_Object(device, params->handles.Handler[i], PTP_OPC_Name); |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 3190 | |
| 3191 | // This is some sort of unique playlist ID so we can keep track of it |
| 3192 | pl->playlist_id = params->handles.Handler[i]; |
| 3193 | |
| 3194 | // Then get the track listing for this playlist |
| 3195 | ret = ptp_mtp_getobjectreferences(params, pl->playlist_id, &pl->tracks, &pl->no_tracks); |
| 3196 | if (ret != PTP_RC_OK) { |
| 3197 | printf("LIBMTP_Get_Playlist: Could not get object references\n"); |
| 3198 | pl->tracks = NULL; |
| 3199 | pl->no_tracks = 0; |
| 3200 | } |
| 3201 | |
| 3202 | // Add playlist to a list that will be returned afterwards. |
| 3203 | if (retlists == NULL) { |
| 3204 | retlists = pl; |
| 3205 | curlist = pl; |
| 3206 | } else { |
| 3207 | curlist->next = pl; |
| 3208 | curlist = pl; |
| 3209 | } |
| 3210 | |
| 3211 | // Call callback here if we decide to add that possibility... |
| 3212 | |
| 3213 | } else { |
| 3214 | printf("LIBMTP panic: Found a bad handle, trying to ignore it.\n"); |
| 3215 | } |
| 3216 | } |
| 3217 | return retlists; |
| 3218 | } |
| 3219 | |
Linus Walleij | 2e4b5f9 | 2006-06-16 14:00:49 +0000 | [diff] [blame] | 3220 | |
| 3221 | /** |
| 3222 | * This function retrieves an individual playlist from the device. |
| 3223 | * @param device a pointer to the device to get the playlist from. |
| 3224 | * @param plid the unique ID of the playlist to retrieve. |
| 3225 | * @return a valid playlist metadata post or NULL on failure. |
| 3226 | * @see LIBMTP_Get_Playlist_List() |
| 3227 | */ |
| 3228 | LIBMTP_playlist_t *LIBMTP_Get_Playlist(LIBMTP_mtpdevice_t *device, uint32_t const plid) |
| 3229 | { |
| 3230 | PTPParams *params = (PTPParams *) device->params; |
| 3231 | uint32_t i; |
| 3232 | |
| 3233 | // Get all the handles if we haven't already done that |
| 3234 | if (params->handles.Handler == NULL) { |
| 3235 | flush_handles(device); |
| 3236 | } |
| 3237 | |
| 3238 | for (i = 0; i < params->handles.n; i++) { |
| 3239 | LIBMTP_playlist_t *pl; |
| 3240 | PTPObjectInfo oi; |
| 3241 | uint16_t ret; |
| 3242 | |
| 3243 | if (params->handles.Handler[i] != plid) { |
| 3244 | continue; |
| 3245 | } |
| 3246 | |
| 3247 | ret = ptp_getobjectinfo(params, params->handles.Handler[i], &oi); |
| 3248 | if ( ret == PTP_RC_OK) { |
| 3249 | |
| 3250 | // Ignore stuff that isn't playlists |
| 3251 | if ( oi.ObjectFormat != PTP_OFC_MTP_AbstractAudioVideoPlaylist ) { |
| 3252 | return NULL; |
| 3253 | } |
| 3254 | |
| 3255 | // Allocate a new playlist type |
| 3256 | pl = LIBMTP_new_playlist_t(); |
| 3257 | |
| 3258 | // Ignoring the io.Filename field. |
Linus Walleij | a823a70 | 2006-08-27 21:27:46 +0000 | [diff] [blame] | 3259 | pl->name = LIBMTP_Get_String_From_Object(device, params->handles.Handler[i], PTP_OPC_Name); |
Linus Walleij | 2e4b5f9 | 2006-06-16 14:00:49 +0000 | [diff] [blame] | 3260 | |
| 3261 | // This is some sort of unique playlist ID so we can keep track of it |
| 3262 | pl->playlist_id = params->handles.Handler[i]; |
| 3263 | |
| 3264 | // Then get the track listing for this playlist |
| 3265 | ret = ptp_mtp_getobjectreferences(params, pl->playlist_id, &pl->tracks, &pl->no_tracks); |
| 3266 | if (ret != PTP_RC_OK) { |
| 3267 | printf("LIBMTP_Get_Playlist: Could not get object references\n"); |
| 3268 | pl->tracks = NULL; |
| 3269 | pl->no_tracks = 0; |
| 3270 | } |
| 3271 | |
| 3272 | return pl; |
| 3273 | } else { |
| 3274 | return NULL; |
| 3275 | } |
| 3276 | } |
| 3277 | return NULL; |
| 3278 | } |
| 3279 | |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 3280 | /** |
| 3281 | * This routine creates a new playlist based on the metadata |
| 3282 | * supplied. If the <code>tracks</code> field of the metadata |
| 3283 | * contains a track listing, these tracks will be added to the |
| 3284 | * playlist. |
| 3285 | * @param device a pointer to the device to create the new playlist on. |
| 3286 | * @param metadata the metadata for the new playlist. If the function |
| 3287 | * exits with success, the <code>playlist_id</code> field of this |
| 3288 | * struct will contain the new playlist ID of the playlist. |
| 3289 | * @param parenthandle the parent (e.g. folder) to store this playlist |
| 3290 | * in. Pass in 0 to put the playlist in the root directory. |
| 3291 | * @return 0 on success, any other value means failure. |
| 3292 | * @see LIBMTP_Update_Playlist() |
| 3293 | * @see LIBMTP_Delete_Object() |
| 3294 | */ |
| 3295 | int LIBMTP_Create_New_Playlist(LIBMTP_mtpdevice_t *device, |
| 3296 | LIBMTP_playlist_t * const metadata, |
| 3297 | uint32_t const parenthandle) |
| 3298 | { |
| 3299 | uint16_t ret; |
| 3300 | uint32_t store = 0; |
| 3301 | PTPObjectInfo new_pl; |
| 3302 | PTPParams *params = (PTPParams *) device->params; |
| 3303 | uint32_t localph = parenthandle; |
| 3304 | char fname[256]; |
Linus Walleij | d14e84f | 2006-06-16 14:50:59 +0000 | [diff] [blame] | 3305 | uint8_t data[2]; |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 3306 | |
Linus Walleij | 05ccbe7 | 2006-06-13 07:46:58 +0000 | [diff] [blame] | 3307 | // Use a default folder if none given |
| 3308 | if (localph == 0) { |
| 3309 | localph = device->default_playlist_folder; |
| 3310 | } |
| 3311 | |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 3312 | // .zpl is the "abstract audio/video playlist "file" suffix |
| 3313 | new_pl.Filename = NULL; |
| 3314 | if (strlen(metadata->name) > 4) { |
| 3315 | char *suff = &metadata->name[strlen(metadata->name)-4]; |
| 3316 | if (!strcmp(suff, ".zpl")) { |
| 3317 | // Home free. |
| 3318 | new_pl.Filename = metadata->name; |
| 3319 | } |
| 3320 | } |
| 3321 | // If it didn't end with ".zpl" then add that here. |
| 3322 | if (new_pl.Filename == NULL) { |
| 3323 | strncpy(fname, metadata->name, sizeof(fname)-5); |
| 3324 | strcat(fname, ".zpl"); |
| 3325 | fname[sizeof(fname)-1] = '\0'; |
| 3326 | new_pl.Filename = fname; |
| 3327 | } |
| 3328 | |
Linus Walleij | cd3eb3d | 2006-09-02 21:33:22 +0000 | [diff] [blame] | 3329 | // Playlists created on device have size (uint32_t) -1 = 0xFFFFFFFFU, but setting: |
| 3330 | // new_pl.ObjectCompressedSize = 0; <- DOES NOT WORK! (return PTP_RC_GeneralError) |
| 3331 | // new_pl.ObjectCompressedSize = (uint32_t) -1; <- DOES NOT WORK! (return PTP_RC_MTP_Object_Too_Large) |
Linus Walleij | d14e84f | 2006-06-16 14:50:59 +0000 | [diff] [blame] | 3332 | new_pl.ObjectCompressedSize = 1; |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 3333 | new_pl.ObjectFormat = PTP_OFC_MTP_AbstractAudioVideoPlaylist; |
| 3334 | |
| 3335 | // Create the object |
| 3336 | ret = ptp_sendobjectinfo(params, &store, &localph, &metadata->playlist_id, &new_pl); |
| 3337 | if (ret != PTP_RC_OK) { |
| 3338 | ptp_perror(params, ret); |
| 3339 | printf("LIBMTP_New_Playlist(): Could not send object info (the playlist itself)\n"); |
Linus Walleij | da9500d | 2006-08-30 13:17:06 +0000 | [diff] [blame] | 3340 | printf("Return code: 0x%04x (look this up in ptp.h for an explanation).\n", ret); |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 3341 | return -1; |
| 3342 | } |
| 3343 | |
| 3344 | /* |
Linus Walleij | cd3eb3d | 2006-09-02 21:33:22 +0000 | [diff] [blame] | 3345 | * We have to send this one blank data byte. |
| 3346 | * If we don't, the handle will not be created and thus there is no playlist. |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 3347 | */ |
Linus Walleij | d14e84f | 2006-06-16 14:50:59 +0000 | [diff] [blame] | 3348 | data[0] = '\0'; |
| 3349 | data[1] = '\0'; |
| 3350 | ret = ptp_sendobject(params, data, 1); |
| 3351 | if (ret != PTP_RC_OK) { |
| 3352 | ptp_perror(params, ret); |
| 3353 | printf("LIBMTP_New_Playlist(): Could not send blank object data\n"); |
Linus Walleij | cd3eb3d | 2006-09-02 21:33:22 +0000 | [diff] [blame] | 3354 | printf("Return code: 0x%04x (look this up in ptp.h for an explanation).\n", ret); |
Linus Walleij | d14e84f | 2006-06-16 14:50:59 +0000 | [diff] [blame] | 3355 | return -1; |
| 3356 | } |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 3357 | |
| 3358 | // Update title |
Linus Walleij | a823a70 | 2006-08-27 21:27:46 +0000 | [diff] [blame] | 3359 | ret = LIBMTP_Set_Object_String(device, metadata->playlist_id, PTP_OPC_Name, metadata->name); |
Linus Walleij | d14e84f | 2006-06-16 14:50:59 +0000 | [diff] [blame] | 3360 | if (ret != 0) { |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 3361 | printf("LIBMTP_New_Playlist(): could not set playlist name\n"); |
| 3362 | return -1; |
| 3363 | } |
| 3364 | |
| 3365 | if (metadata->no_tracks > 0) { |
| 3366 | // Add tracks to the new playlist as object references. |
| 3367 | ret = ptp_mtp_setobjectreferences (params, metadata->playlist_id, metadata->tracks, metadata->no_tracks); |
| 3368 | if (ret != PTP_RC_OK) { |
| 3369 | printf("LIBMTP_New_Playlist(): could not add tracks as object references\n"); |
Linus Walleij | cd3eb3d | 2006-09-02 21:33:22 +0000 | [diff] [blame] | 3370 | printf("Return code: 0x%04x (look this up in ptp.h for an explanation).\n", ret); |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 3371 | return -1; |
| 3372 | } |
| 3373 | } |
| 3374 | |
| 3375 | // Created new item, so flush handles |
| 3376 | flush_handles(device); |
| 3377 | |
| 3378 | return 0; |
| 3379 | } |
| 3380 | |
| 3381 | /** |
| 3382 | * This routine updates a playlist based on the metadata |
| 3383 | * supplied. If the <code>tracks</code> field of the metadata |
| 3384 | * contains a track listing, these tracks will be added to the |
| 3385 | * playlist in place of those already present, i.e. the |
| 3386 | * previous track listing will be deleted. |
| 3387 | * @param device a pointer to the device to create the new playlist on. |
| 3388 | * @param metadata the metadata for the playlist to be updated. |
| 3389 | * notice that the field <code>playlist_id</code> |
| 3390 | * must contain the apropriate playlist ID. |
| 3391 | * @return 0 on success, any other value means failure. |
| 3392 | * @see LIBMTP_Create_New_Playlist() |
| 3393 | * @see LIBMTP_Delete_Object() |
| 3394 | */ |
| 3395 | int LIBMTP_Update_Playlist(LIBMTP_mtpdevice_t *device, |
Linus Walleij | f530635 | 2006-06-08 12:00:23 +0000 | [diff] [blame] | 3396 | LIBMTP_playlist_t const * const metadata) |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 3397 | { |
| 3398 | uint16_t ret; |
Linus Walleij | f530635 | 2006-06-08 12:00:23 +0000 | [diff] [blame] | 3399 | PTPParams *params = (PTPParams *) device->params; |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 3400 | |
| 3401 | // Update title |
Linus Walleij | a823a70 | 2006-08-27 21:27:46 +0000 | [diff] [blame] | 3402 | ret = LIBMTP_Set_Object_String(device, metadata->playlist_id, PTP_OPC_Name, metadata->name); |
Linus Walleij | d14e84f | 2006-06-16 14:50:59 +0000 | [diff] [blame] | 3403 | if (ret != 0) { |
Linus Walleij | 438bd7f | 2006-06-08 11:35:44 +0000 | [diff] [blame] | 3404 | printf("LIBMTP_Update_Playlist(): could not set playlist name\n"); |
| 3405 | return -1; |
| 3406 | } |
| 3407 | |
| 3408 | if (metadata->no_tracks > 0) { |
| 3409 | // Add tracks to the new playlist as object references. |
| 3410 | ret = ptp_mtp_setobjectreferences (params, metadata->playlist_id, metadata->tracks, metadata->no_tracks); |
| 3411 | if (ret != PTP_RC_OK) { |
| 3412 | printf("LIBMTP_Update_Playlist(): could not add tracks as object references\n"); |
| 3413 | return -1; |
| 3414 | } |
| 3415 | } |
| 3416 | return 0; |
| 3417 | } |
Linus Walleij | aa4b075 | 2006-07-26 22:21:04 +0000 | [diff] [blame] | 3418 | |
| 3419 | |
| 3420 | /** |
| 3421 | * Dummy function needed to interface to upstream |
| 3422 | * ptp.c/ptp.h files. |
| 3423 | */ |
| 3424 | void ptp_nikon_getptpipguid (unsigned char* guid) { |
| 3425 | return; |
| 3426 | } |