summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarcus Meissner <marcus@jet.franken.de>2022-09-28 08:52:19 +0200
committerGitHub <noreply@github.com>2022-09-28 08:52:19 +0200
commitea0efee159beaf5e8cf5cd6a8b05cd0ccafa3d87 (patch)
tree9c5cd34d72f6fbd57bae71efa7c67bb2ea4828c1
parentc364dae7fe02a23f1e5e8dec5138b98b061f4ebe (diff)
parentea7c9fd99c1460d0fac275c137c644dd91dc8e58 (diff)
downloadlibmtp-ea0efee159beaf5e8cf5cd6a8b05cd0ccafa3d87.tar.gz
Merge pull request #58 from virtion/master
Device serial number parameter (optional) for "mtp-getfile" and "mtp-delfile"
-rw-r--r--examples/delfile.c17
-rw-r--r--examples/files.c9
-rw-r--r--examples/getfile.c17
-rw-r--r--src/libmtp.c74
-rw-r--r--src/libmtp.h.in2
-rw-r--r--src/libmtp.sym2
6 files changed, 91 insertions, 30 deletions
diff --git a/examples/delfile.c b/examples/delfile.c
index 59d7625..8f01d6d 100644
--- a/examples/delfile.c
+++ b/examples/delfile.c
@@ -34,7 +34,7 @@ extern LIBMTP_file_t *files;
void delfile_usage(void)
{
- printf("Usage: delfile [<deviceid>] -n <fileid/trackid> | -f <filename> ...\n");
+ printf("Usage: delfile [<deviceid> | SN:<serialnumber>] -n <fileid/trackid> | -f <filename> ...\n");
}
int
@@ -61,19 +61,8 @@ LIBMTP_mtpdevice_t *delfile_device(int argc, char **argv)
if (argc >= 3 && argv[1][0] == '-')
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);
- }
+ if (argc >= 4)
+ return LIBMTP_Get_Device_By_ID(argv[1]);
delfile_usage();
diff --git a/examples/files.c b/examples/files.c
index 6af3b7b..3ee0846 100644
--- a/examples/files.c
+++ b/examples/files.c
@@ -21,6 +21,7 @@
* Boston, MA 02111-1307, USA.
*/
#include "common.h"
+#include "ptp.h"
#include <stdlib.h>
static void dump_fileinfo(LIBMTP_file_t *file)
@@ -117,6 +118,7 @@ int main(int argc, char **argv)
for (i = 0; i < numrawdevices; i++) {
LIBMTP_mtpdevice_t *device;
LIBMTP_devicestorage_t *storage;
+ PTPParams *params;
char *friendlyname;
device = LIBMTP_Open_Raw_Device_Uncached(&rawdevices[i]);
@@ -127,10 +129,13 @@ int main(int argc, char **argv)
/* Echo the friendly name so we know which device we are working with */
friendlyname = LIBMTP_Get_Friendlyname(device);
+ params = (PTPParams *) device->params;
if (friendlyname == NULL) {
- printf("Listing File Information on Device with name: (NULL)\n");
+ printf("Listing File Information on Device with name: (NULL) [SN:%s]\n",
+ params->deviceinfo.SerialNumber);
} else {
- printf("Listing File Information on Device with name: %s\n", friendlyname);
+ printf("Listing File Information on Device with name: %s [SN:%s]\n",
+ friendlyname, params->deviceinfo.SerialNumber);
free(friendlyname);
}
diff --git a/examples/getfile.c b/examples/getfile.c
index e06aea9..532c7e3 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 [<deviceid>] <fileid/trackid> <filename>\n");
+ fprintf(stderr, "getfile [<deviceid> | SN:<serialnumber>] <fileid/trackid> <filename>\n");
}
int
@@ -57,19 +57,8 @@ 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);
- }
+ if (argc == 4)
+ return LIBMTP_Get_Device_By_ID(argv[1]);
getfile_usage();
diff --git a/src/libmtp.c b/src/libmtp.c
index 0f348ef..624d688 100644
--- a/src/libmtp.c
+++ b/src/libmtp.c
@@ -1722,6 +1722,80 @@ LIBMTP_mtpdevice_t *LIBMTP_Get_First_Device(void)
}
/**
+ * Get connected MTP device by serial number.
+ * @return a device pointer.
+ * @see LIBMTP_Get_Connected_Devices()
+ */
+LIBMTP_mtpdevice_t *LIBMTP_Get_Device_By_SerialNumber(char *serial_number)
+{
+ LIBMTP_mtpdevice_t *device = NULL;
+ LIBMTP_raw_device_t *devices;
+ int numdevs;
+ LIBMTP_error_number_t ret;
+ PTPParams *params;
+ int found_device = 0;
+ int i;
+
+ if (serial_number == NULL || *serial_number == '\0')
+ return NULL;
+
+ ret = LIBMTP_Detect_Raw_Devices(&devices, &numdevs);
+ if (ret != LIBMTP_ERROR_NONE)
+ return NULL;
+
+ if (devices == NULL || numdevs == 0) {
+ free(devices);
+ return NULL;
+ }
+
+ for (i = 0; i < numdevs; i++) {
+ device = LIBMTP_Open_Raw_Device(&devices[i]);
+ if (device == NULL)
+ continue;
+
+ params = (PTPParams *) device->params;
+ if (strcmp(params->deviceinfo.SerialNumber, serial_number) == 0) {
+ found_device = 1;
+ break;
+ }
+
+ LIBMTP_Release_Device(device);
+ }
+
+ free(devices);
+
+ if (!found_device)
+ return NULL;
+
+ return device;
+}
+
+/**
+ * Get connected MTP device by list position or serial number.
+ * @return a device pointer.
+ * @see LIBMTP_Get_Connected_Devices()
+ */
+LIBMTP_mtpdevice_t *LIBMTP_Get_Device_By_ID(char *device_id)
+{
+ uint32_t device_nr;
+ char *endptr;
+
+ if (device_id == NULL || *device_id == '\0')
+ return NULL;
+
+ // 1st try: serial number
+ if (strlen(device_id) > 3 && strncmp(device_id, "SN:", 3) == 0)
+ return LIBMTP_Get_Device_By_SerialNumber(device_id + 3);
+
+ // 2nd try: device number
+ device_nr = strtoul(device_id, &endptr, 10);
+ if (*endptr == '\0')
+ return LIBMTP_Get_Device(device_nr);
+
+ return NULL;
+}
+
+/**
* Overriding debug function.
* This way we can disable debug prints.
*/
diff --git a/src/libmtp.h.in b/src/libmtp.h.in
index fa5cfb8..8be84f1 100644
--- a/src/libmtp.h.in
+++ b/src/libmtp.h.in
@@ -843,6 +843,8 @@ 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_mtpdevice_t *LIBMTP_Get_Device_By_SerialNumber(char *);
+LIBMTP_mtpdevice_t *LIBMTP_Get_Device_By_ID(char *);
LIBMTP_error_number_t LIBMTP_Get_Connected_Devices(LIBMTP_mtpdevice_t **);
uint32_t LIBMTP_Number_Devices_In_List(LIBMTP_mtpdevice_t *);
void LIBMTP_Release_Device_List(LIBMTP_mtpdevice_t*);
diff --git a/src/libmtp.sym b/src/libmtp.sym
index fec6e58..4bece67 100644
--- a/src/libmtp.sym
+++ b/src/libmtp.sym
@@ -7,6 +7,8 @@ LIBMTP_Open_Raw_Device
LIBMTP_Open_Raw_Device_Uncached
LIBMTP_Get_Device
LIBMTP_Get_First_Device
+LIBMTP_Get_Device_By_SerialNumber
+LIBMTP_Get_Device_By_ID
LIBMTP_Get_Connected_Devices
LIBMTP_Number_Devices_In_List
LIBMTP_Release_Device_List