diff options
author | Holger Schemel <holger.schemel@virtion.de> | 2020-07-20 11:43:45 +0200 |
---|---|---|
committer | Holger Schemel <holger.schemel@virtion.de> | 2020-07-20 11:48:42 +0200 |
commit | e4da7f9b05a37d4d3e3954d71a2758441dc03e1d (patch) | |
tree | 7e3583ef62b21b495c56ed4467ec1fd90d8760a0 | |
parent | 32b1d9f2319da6917de99d232f0064769f039d16 (diff) | |
download | libmtp-e4da7f9b05a37d4d3e3954d71a2758441dc03e1d.tar.gz |
added optional device ID parameter to 'mtp-getfile'
-rw-r--r-- | examples/connect.c | 6 | ||||
-rw-r--r-- | examples/connect.h | 1 | ||||
-rw-r--r-- | examples/getfile.c | 38 | ||||
-rw-r--r-- | src/libmtp.c | 25 | ||||
-rw-r--r-- | src/libmtp.h.in | 1 | ||||
-rw-r--r-- | src/libmtp.sym | 1 |
6 files changed, 59 insertions, 13 deletions
diff --git a/examples/connect.c b/examples/connect.c index 9155160..f326cdd 100644 --- a/examples/connect.c +++ b/examples/connect.c @@ -69,7 +69,11 @@ int main (int argc, char **argv) fprintf(stdout, "libmtp version: " LIBMTP_VERSION_STRING "\n\n"); - device = LIBMTP_Get_First_Device(); + if ((strncmp(basename(argv[0]),"mtp-getfile",11) == 0) || (strncmp(basename(argv[0]),"getfile",7) == 0)) + device = getfile_device(argc,argv); + else + device = LIBMTP_Get_First_Device(); + if (device == NULL) { printf("No devices.\n"); return 0; diff --git a/examples/connect.h b/examples/connect.h index 5fe2cd3..acd40e2 100644 --- a/examples/connect.h +++ b/examples/connect.h @@ -30,6 +30,7 @@ int sendfile_function(char *,char *); int sendfile_command(int, char **); void sendfile_usage(void); int getfile_function(char *,char *); +LIBMTP_mtpdevice_t *getfile_device(int, char **); int getfile_command(int, char **); void getfile_usage(void); int newfolder_function(char *); diff --git a/examples/getfile.c b/examples/getfile.c index 4562f87..e06aea9 100644 --- a/examples/getfile.c +++ b/examples/getfile.c @@ -33,7 +33,7 @@ extern LIBMTP_mtpdevice_t *device; void getfile_usage (void) { - fprintf(stderr, "getfile <fileid/trackid> <filename>\n"); + fprintf(stderr, "getfile [<deviceid>] <fileid/trackid> <filename>\n"); } int @@ -52,32 +52,56 @@ getfile_function(char * from_path,char * to_path) return 0; } +LIBMTP_mtpdevice_t *getfile_device(int argc, char **argv) +{ + if (argc == 3) + return LIBMTP_Get_First_Device(); + + if (argc == 4) { + uint32_t id; + char *endptr; + + // Sanity check device ID + id = strtoul(argv[1], &endptr, 10); + if ( *endptr != 0 ) { + fprintf(stderr, "illegal value %s\n", argv[1]); + return NULL; + } + + return LIBMTP_Get_Device(id); + } + + getfile_usage(); + + return NULL; +} int getfile_command(int argc, char **argv) { uint32_t id; char *endptr; char *file; + int off = (argc == 4 ? 1 : 0); int ret = 0; - // We need file ID and filename - if ( argc != 3 ) { + // We need file ID and filename (device ID is optional) + if ( argc != 3 && argc != 4 ) { getfile_usage(); return 0; } // Sanity check song ID - id = strtoul(argv[1], &endptr, 10); + id = strtoul(argv[1 + off], &endptr, 10); if ( *endptr != 0 ) { - fprintf(stderr, "illegal value %s\n", argv[1]); + fprintf(stderr, "illegal value %s\n", argv[1 + off]); return 1; } else if ( ! id ) { fprintf(stderr, "bad file/track id %u\n", id); return 1; - } + } // Filename, e.g. "foo.mp3" - file = argv[2]; + file = argv[2 + off]; printf("Getting file/track %d to local file %s\n", id, file); // This function will also work just as well for tracks. diff --git a/src/libmtp.c b/src/libmtp.c index 1326992..d6de0e4 100644 --- a/src/libmtp.c +++ b/src/libmtp.c @@ -1680,13 +1680,13 @@ static int set_object_u8(LIBMTP_mtpdevice_t *device, uint32_t const object_id, } /** - * Get the first (as in "first in the list of") connected MTP device. + * Get connected MTP device by list position. * @return a device pointer. * @see LIBMTP_Get_Connected_Devices() */ -LIBMTP_mtpdevice_t *LIBMTP_Get_First_Device(void) +LIBMTP_mtpdevice_t *LIBMTP_Get_Device(int device_nr) { - LIBMTP_mtpdevice_t *first_device = NULL; + LIBMTP_mtpdevice_t *device = NULL; LIBMTP_raw_device_t *devices; int numdevs; LIBMTP_error_number_t ret; @@ -1701,9 +1701,24 @@ LIBMTP_mtpdevice_t *LIBMTP_Get_First_Device(void) return NULL; } - first_device = LIBMTP_Open_Raw_Device(&devices[0]); + if (device_nr < 0 || device_nr >= numdevs) { + free(devices); + return NULL; + } + + device = LIBMTP_Open_Raw_Device(&devices[device_nr]); free(devices); - return first_device; + return device; +} + +/** + * Get the first (as in "first in the list of") connected MTP device. + * @return a device pointer. + * @see LIBMTP_Get_Connected_Devices() + */ +LIBMTP_mtpdevice_t *LIBMTP_Get_First_Device(void) +{ + return LIBMTP_Get_Device(0); } /** diff --git a/src/libmtp.h.in b/src/libmtp.h.in index 6376332..8df68c5 100644 --- a/src/libmtp.h.in +++ b/src/libmtp.h.in @@ -842,6 +842,7 @@ int LIBMTP_Check_Specific_Device(int busno, int devno); LIBMTP_mtpdevice_t *LIBMTP_Open_Raw_Device(LIBMTP_raw_device_t *); LIBMTP_mtpdevice_t *LIBMTP_Open_Raw_Device_Uncached(LIBMTP_raw_device_t *); /* Begin old, legacy interface */ +LIBMTP_mtpdevice_t *LIBMTP_Get_Device(int); LIBMTP_mtpdevice_t *LIBMTP_Get_First_Device(void); LIBMTP_error_number_t LIBMTP_Get_Connected_Devices(LIBMTP_mtpdevice_t **); uint32_t LIBMTP_Number_Devices_In_List(LIBMTP_mtpdevice_t *); diff --git a/src/libmtp.sym b/src/libmtp.sym index 20d9195..fec6e58 100644 --- a/src/libmtp.sym +++ b/src/libmtp.sym @@ -5,6 +5,7 @@ LIBMTP_Detect_Raw_Devices LIBMTP_Check_Specific_Device LIBMTP_Open_Raw_Device LIBMTP_Open_Raw_Device_Uncached +LIBMTP_Get_Device LIBMTP_Get_First_Device LIBMTP_Get_Connected_Devices LIBMTP_Number_Devices_In_List |