summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornicklas79 <nicklas79>2009-09-28 18:19:34 +0000
committernicklas79 <nicklas79>2009-09-28 18:19:34 +0000
commitdaadbf2e26eca84c9d250f0e09d9efbe2c70d77a (patch)
treee38fea9dffb0cc3dbfc999253d156cf73ca74762
parentaac4729a1797831465f3d45b840e22749fbf1095 (diff)
downloadlibmtp-daadbf2e26eca84c9d250f0e09d9efbe2c70d77a.tar.gz
Add a new function to API to enable debug mode
To avoid build and build only to enable (or disable) debug mode, we work now as the lib libusb :) As libusb, you can do : LIBMTP_Set_Debug(flags); or use an environnement variable : export LIBMTP_DEBUG=flags flags value details : * 0x00 [0000 0000] : no debug (default) * 0x01 [0000 0001] : PTP debug * 0x02 [0000 0010] : Playlist debug * 0x04 [0000 0100] : USB debug * 0x08 [0000 1000] : USB data debug
-rw-r--r--examples/albumart.c7
-rw-r--r--examples/albums.c17
-rw-r--r--examples/detect.c15
-rw-r--r--src/libmtp.c88
-rw-r--r--src/libmtp.h.in26
-rw-r--r--src/libmtp.sym1
-rw-r--r--src/libusb-glue.c221
-rw-r--r--src/libusb-glue.h16
-rw-r--r--src/playlist-spl.c119
9 files changed, 299 insertions, 211 deletions
diff --git a/examples/albumart.c b/examples/albumart.c
index 51ab400..7fe9077 100644
--- a/examples/albumart.c
+++ b/examples/albumart.c
@@ -34,7 +34,7 @@
#endif
static void usage(void) {
- printf("Usage: albumart -i <fileid/trackid> -n <albumname> -s <storage_id> -p <parent_id> <imagefile>\n");
+ printf("Usage: albumart -d -i <fileid/trackid> -n <albumname> -s <storage_id> -p <parent_id> <imagefile>\n");
exit(0);
}
@@ -58,10 +58,13 @@ int main (int argc, char **argv) {
fprintf(stdout, "libmtp version: " LIBMTP_VERSION_STRING "\n\n");
- while ( (opt = getopt(argc, argv, "hn:i:s:p:")) != -1 ) {
+ while ( (opt = getopt(argc, argv, "dhn:i:s:p:")) != -1 ) {
switch (opt) {
case 'h':
usage();
+ case 'd':
+ LIBMTP_Set_Debug(9);
+ break;
case 'i':
idcount++;
if ((tmp = realloc(ids, sizeof(uint32_t) * (idcount))) == NULL) {
diff --git a/examples/albums.c b/examples/albums.c
index a653126..8296e35 100644
--- a/examples/albums.c
+++ b/examples/albums.c
@@ -34,9 +34,24 @@ static void dump_albuminfo(LIBMTP_album_t *album)
printf(" Tracks: %d\n\n",album->no_tracks);
}
-int main () {
+int main (int argc, char *argv[]) {
LIBMTP_mtpdevice_t *device_list, *iter;
+ int opt;
+ extern int optind;
+ extern char *optarg;
+
+ while ((opt = getopt(argc, argv, "d")) != -1 ) {
+ switch (opt) {
+ case 'd':
+ LIBMTP_Set_Debug(9);
+ break;
+ }
+ }
+
+ argc -= optind;
+ argv += optind;
+
LIBMTP_Init();
fprintf(stdout, "libmtp version: " LIBMTP_VERSION_STRING "\n\n");
diff --git a/examples/detect.c b/examples/detect.c
index 393fd2c..8dba1b4 100644
--- a/examples/detect.c
+++ b/examples/detect.c
@@ -64,6 +64,21 @@ int main (int argc, char **argv)
LIBMTP_error_number_t err;
int i;
+ int opt;
+ extern int optind;
+ extern char *optarg;
+
+ while ((opt = getopt(argc, argv, "d")) != -1 ) {
+ switch (opt) {
+ case 'd':
+ LIBMTP_Set_Debug(9);
+ break;
+ }
+ }
+
+ argc -= optind;
+ argv += optind;
+
LIBMTP_Init();
fprintf(stdout, "libmtp version: " LIBMTP_VERSION_STRING "\n\n");
diff --git a/src/libmtp.c b/src/libmtp.c
index f142a34..e2d0408 100644
--- a/src/libmtp.c
+++ b/src/libmtp.c
@@ -55,8 +55,20 @@
#include <io.h>
#endif
-/* To enable PTP level debug prints (all ptp_debug(...)), switch on this */
-//#define ENABLE_PTP_DEBUG
+
+/**
+ * Global debug level
+ * We use a flag system to enable a part of logs.
+ * To choose a particular flag, you have to use LIBMTP_DEBUG env variable.
+ * Indeed, the option '-d' enables all logs.
+ * 0x00 [0000 0000] : no debug (default)
+ * 0x01 [0000 0001] : PTP debug
+ * 0x02 [0000 0010] : Playlist debug
+ * 0x04 [0000 0100] : USB debug
+ * 0x08 [0000 1000] : USB data debug
+ */
+int LIBMTP_debug = 0;
+
/*
* This is a mapping between libmtp internal MTP filetypes and
@@ -705,6 +717,21 @@ static LIBMTP_property_t map_ptp_property_to_libmtp_property(uint16_t inproperty
/**
+ * Set the debug level.
+ *
+ * By default, the debug level is set to '0' (disable).
+ */
+void LIBMTP_Set_Debug(int level)
+{
+ if (LIBMTP_debug || level)
+ LIBMTP_ERROR("LIBMTP_Set_Debug: Setting debugging level to %d (%s)\n",
+ level, level ? "on" : "off");
+
+ LIBMTP_debug = level;
+}
+
+
+/**
* Initialize the library. You are only supposed to call this
* one, before using the library for the first time in a program.
* Never re-initialize libmtp!
@@ -714,6 +741,9 @@ static LIBMTP_property_t map_ptp_property_to_libmtp_property(uint16_t inproperty
*/
void LIBMTP_Init(void)
{
+ if (getenv("LIBMTP_DEBUG"))
+ LIBMTP_Set_Debug(atoi(getenv("LIBMTP_DEBUG")));
+
init_filemap();
init_propertymap();
return;
@@ -1626,10 +1656,10 @@ __attribute__((__format__(printf,2,0)))
#endif
LIBMTP_ptp_debug(void *data, const char *format, va_list args)
{
-#ifdef ENABLE_PTP_DEBUG
- vfprintf (stderr, format, args);
- fflush (stderr);
-#endif
+ if ((LIBMTP_debug & 0x01) != 0) {
+ vfprintf (stderr, format, args);
+ fflush (stderr);
+ }
}
/**
@@ -1686,7 +1716,7 @@ LIBMTP_mtpdevice_t *LIBMTP_Open_Raw_Device(LIBMTP_raw_device_t *rawdevice)
device and attempt to continue */
/* TODO: This error statement could probably be a bit more robust */
- fprintf(stderr, "LIBMTP PANIC: connect_usb_devices encountered a memory "
+ LIBMTP_ERROR("LIBMTP PANIC: connect_usb_devices encountered a memory "
"allocation error with device %d on bus %d, trying to continue",
rawdevice->devnum, rawdevice->bus_location);
@@ -1717,7 +1747,7 @@ LIBMTP_mtpdevice_t *LIBMTP_Open_Raw_Device(LIBMTP_raw_device_t *rawdevice)
if(current_params->cd_locale_to_ucs2 == (iconv_t) -1 ||
current_params->cd_ucs2_to_locale == (iconv_t) -1) {
- fprintf(stderr, "LIBMTP PANIC: Cannot open iconv() converters to/from UCS-2!\n"
+ LIBMTP_ERROR("LIBMTP PANIC: Cannot open iconv() converters to/from UCS-2!\n"
"Too old stdlibc, glibc and libiconv?\n");
free(current_params);
free(mtp_device);
@@ -1743,7 +1773,7 @@ LIBMTP_mtpdevice_t *LIBMTP_Open_Raw_Device(LIBMTP_raw_device_t *rawdevice)
/* Cache the device information for later use */
if (ptp_getdeviceinfo(current_params,
&current_params->deviceinfo) != PTP_RC_OK) {
- fprintf(stderr, "LIBMTP PANIC: Unable to read device information on device "
+ LIBMTP_ERROR("LIBMTP PANIC: Unable to read device information on device "
"%d on bus %d, trying to continue",
rawdevice->devnum, rawdevice->bus_location);
@@ -1763,14 +1793,14 @@ LIBMTP_mtpdevice_t *LIBMTP_Open_Raw_Device(LIBMTP_raw_device_t *rawdevice)
PTP_OPC_ObjectSize,
current_params->deviceinfo.ImageFormats[i],
&opd) != PTP_RC_OK) {
- printf("LIBMTP PANIC: "
+ LIBMTP_ERROR("LIBMTP PANIC: "
"could not inspect object property descriptions!\n");
} else {
if (opd.DataType == PTP_DTC_UINT32) {
if (bs == 0) {
bs = 32;
} else if (bs != 32) {
- printf("LIBMTP PANIC: "
+ LIBMTP_ERROR("LIBMTP PANIC: "
"different objects support different object sizes!\n");
bs = 0;
break;
@@ -1779,14 +1809,14 @@ LIBMTP_mtpdevice_t *LIBMTP_Open_Raw_Device(LIBMTP_raw_device_t *rawdevice)
if (bs == 0) {
bs = 64;
} else if (bs != 64) {
- printf("LIBMTP PANIC: "
+ LIBMTP_ERROR("LIBMTP PANIC: "
"different objects support different object sizes!\n");
bs = 0;
break;
}
} else {
// Ignore if other size.
- printf("LIBMTP PANIC: "
+ LIBMTP_ERROR("LIBMTP PANIC: "
"awkward object size data type: %04x\n", opd.DataType);
bs = 0;
break;
@@ -1997,7 +2027,7 @@ static void add_error_to_errorstack(LIBMTP_mtpdevice_t *device,
LIBMTP_error_t *newerror;
if (device == NULL) {
- fprintf(stderr, "LIBMTP PANIC: Trying to add error to a NULL device!\n");
+ LIBMTP_ERROR("LIBMTP PANIC: Trying to add error to a NULL device!\n");
return;
}
newerror = (LIBMTP_error_t *) malloc(sizeof(LIBMTP_error_t));
@@ -2024,7 +2054,7 @@ static void add_ptp_error_to_errorstack(LIBMTP_mtpdevice_t *device,
char const * const error_text)
{
if (device == NULL) {
- fprintf(stderr, "LIBMTP PANIC: Trying to add PTP error to a NULL device!\n");
+ LIBMTP_ERROR("LIBMTP PANIC: Trying to add PTP error to a NULL device!\n");
return;
} else {
char outstr[256];
@@ -2053,7 +2083,7 @@ static void add_ptp_error_to_errorstack(LIBMTP_mtpdevice_t *device,
LIBMTP_error_t *LIBMTP_Get_Errorstack(LIBMTP_mtpdevice_t *device)
{
if (device == NULL) {
- fprintf(stderr, "LIBMTP PANIC: Trying to get the error stack of a NULL device!\n");
+ LIBMTP_ERROR("LIBMTP PANIC: Trying to get the error stack of a NULL device!\n");
return NULL;
}
return device->errorstack;
@@ -2069,7 +2099,7 @@ LIBMTP_error_t *LIBMTP_Get_Errorstack(LIBMTP_mtpdevice_t *device)
void LIBMTP_Clear_Errorstack(LIBMTP_mtpdevice_t *device)
{
if (device == NULL) {
- fprintf(stderr, "LIBMTP PANIC: Trying to clear the error stack of a NULL device!\n");
+ LIBMTP_ERROR("LIBMTP PANIC: Trying to clear the error stack of a NULL device!\n");
} else {
LIBMTP_error_t *tmp = device->errorstack;
@@ -2096,15 +2126,15 @@ void LIBMTP_Clear_Errorstack(LIBMTP_mtpdevice_t *device)
void LIBMTP_Dump_Errorstack(LIBMTP_mtpdevice_t *device)
{
if (device == NULL) {
- fprintf(stderr, "LIBMTP PANIC: Trying to dump the error stack of a NULL device!\n");
+ LIBMTP_ERROR("LIBMTP PANIC: Trying to dump the error stack of a NULL device!\n");
} else {
LIBMTP_error_t *tmp = device->errorstack;
while (tmp != NULL) {
if (tmp->error_text != NULL) {
- fprintf(stderr, "Error %d: %s\n", tmp->errornumber, tmp->error_text);
+ LIBMTP_ERROR("Error %d: %s\n", tmp->errornumber, tmp->error_text);
} else {
- fprintf(stderr, "Error %d: (unknown)\n", tmp->errornumber);
+ LIBMTP_ERROR("Error %d: (unknown)\n", tmp->errornumber);
}
tmp = tmp->next;
}
@@ -2357,7 +2387,7 @@ static void flush_handles(LIBMTP_mtpdevice_t *device)
ob = &params->objects[i];
ret = ptp_object_want(params,params->objects[i].oid,PTPOBJECT_OBJECTINFO_LOADED, &xob);
if (ret != PTP_RC_OK) {
- fprintf(stderr,"broken! %x not found\n", params->objects[i].oid);
+ LIBMTP_ERROR("broken! %x not found\n", params->objects[i].oid);
}
if (ob->oi.Filename == NULL)
ob->oi.Filename = strdup("<null>");
@@ -3622,8 +3652,8 @@ void LIBMTP_destroy_file_t(LIBMTP_file_t *file)
*/
LIBMTP_file_t *LIBMTP_Get_Filelisting(LIBMTP_mtpdevice_t *device)
{
- printf("WARNING: LIBMTP_Get_Filelisting() is deprecated.\n");
- printf("WARNING: please update your code to use LIBMTP_Get_Filelisting_With_Callback()\n");
+ LIBMTP_INFO("WARNING: LIBMTP_Get_Filelisting() is deprecated.\n");
+ LIBMTP_INFO("WARNING: please update your code to use LIBMTP_Get_Filelisting_With_Callback()\n");
return LIBMTP_Get_Filelisting_With_Callback(device, NULL, NULL);
}
@@ -4174,8 +4204,8 @@ static void get_track_metadata(LIBMTP_mtpdevice_t *device, uint16_t objectformat
*/
LIBMTP_track_t *LIBMTP_Get_Tracklisting(LIBMTP_mtpdevice_t *device)
{
- printf("WARNING: LIBMTP_Get_Tracklisting() is deprecated.\n");
- printf("WARNING: please update your code to use LIBMTP_Get_Tracklisting_With_Callback()\n");
+ LIBMTP_INFO("WARNING: LIBMTP_Get_Tracklisting() is deprecated.\n");
+ LIBMTP_INFO("WARNING: please update your code to use LIBMTP_Get_Tracklisting_With_Callback()\n");
return LIBMTP_Get_Tracklisting_With_Callback(device, NULL, NULL);
}
@@ -4798,7 +4828,7 @@ int LIBMTP_Send_Track_From_File(LIBMTP_mtpdevice_t *device,
#else
if ( (fd = open(path, O_RDONLY)) == -1) {
#endif
- printf("LIBMTP_Send_Track_From_File(): Could not open source file \"%s\"\n", path);
+ LIBMTP_ERROR("LIBMTP_Send_Track_From_File(): Could not open source file \"%s\"\n", path);
return -1;
}
@@ -6523,7 +6553,7 @@ LIBMTP_folder_t *LIBMTP_Get_Folder_List(LIBMTP_mtpdevice_t *device)
* children, because we rely on that instead.
*/
if (ob->oi.AssociationDesc != 0x00000000U) {
- printf("MTP extended association type 0x%08x encountered\n", ob->oi.AssociationDesc);
+ LIBMTP_INFO("MTP extended association type 0x%08x encountered\n", ob->oi.AssociationDesc);
}
// Create a folder struct...
@@ -6551,7 +6581,7 @@ LIBMTP_folder_t *LIBMTP_Get_Folder_List(LIBMTP_mtpdevice_t *device)
while(head.sibling != &head) {
LIBMTP_folder_t *curr = head.sibling;
- printf("Orphan folder with ID: 0x%08x name: \"%s\" encountered.\n",
+ LIBMTP_INFO("Orphan folder with ID: 0x%08x name: \"%s\" encountered.\n",
curr->folder_id,
curr->name);
curr->sibling->child = curr->child;
@@ -6892,7 +6922,7 @@ static int create_new_abstract_list(LIBMTP_mtpdevice_t *device,
}
if (!supported) {
add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "create_new_abstract_list(): player does not support this abstract type.");
- printf("Unsupported abstract list type: %04x\n", objectformat);
+ LIBMTP_ERROR("Unsupported abstract list type: %04x\n", objectformat);
return -1;
}
diff --git a/src/libmtp.h.in b/src/libmtp.h.in
index 12f657d..f2f61f2 100644
--- a/src/libmtp.h.in
+++ b/src/libmtp.h.in
@@ -56,6 +56,29 @@ typedef unsigned __int64 uint64_t;
#include <usb.h>
#include <stdint.h>
+
+/**
+ * Info macro
+ */
+#define LIBMTP_INFO(format, args...) \
+ do { \
+ if (LIBMTP_debug != 0) \
+ fprintf(stdout, "LIBMTP %s[%d]: " format, __FUNCTION__, __LINE__, ##args); \
+ else \
+ fprintf(stdout, format, ##args); \
+ } while (0)
+
+/**
+ * Error macro
+ */
+#define LIBMTP_ERROR(format, args...) \
+ do { \
+ if (LIBMTP_debug != 0) \
+ fprintf(stderr, "LIBMTP %s[%d]: " format, __FUNCTION__, __LINE__, ##args); \
+ else \
+ fprintf(stderr, format, ##args); \
+ } while (0)
+
/**
* @defgroup types libmtp global type definitions
* @{
@@ -724,10 +747,13 @@ struct LIBMTP_devicestorage_struct {
extern "C" {
#endif
+extern int LIBMTP_debug;
+
/**
* @defgroup internals The libmtp internals API.
* @{
*/
+void LIBMTP_Set_Debug(int);
void LIBMTP_Init(void);
int LIBMTP_Get_Supported_Devices_List(LIBMTP_device_entry_t ** const, int * const);
/**
diff --git a/src/libmtp.sym b/src/libmtp.sym
index 8a649a8..68ef83c 100644
--- a/src/libmtp.sym
+++ b/src/libmtp.sym
@@ -1,3 +1,4 @@
+LIBMTP_Set_Debug
LIBMTP_Init
LIBMTP_Get_Supported_Devices_List
LIBMTP_Detect_Raw_Devices
diff --git a/src/libusb-glue.c b/src/libusb-glue.c
index 71847e6..6fdb0e7 100644
--- a/src/libusb-glue.c
+++ b/src/libusb-glue.c
@@ -49,9 +49,6 @@
#define USB_CLASS_PTP 6
#endif
-/* To enable debug prints for USB stuff, switch on this */
-//#define ENABLE_USB_BULK_DEBUG
-
/* Default USB timeout length. This can be overridden as needed
* but should start with a reasonable value so most common
* requests can be completed. The original value of 4000 was
@@ -135,10 +132,12 @@ int LIBMTP_Get_Supported_Devices_List(LIBMTP_device_entry_t ** const devices, in
static struct usb_bus* init_usb()
{
- /* Some additional libusb debugging please */
-#ifdef ENABLE_USB_BULK_DEBUG
- usb_set_debug(9);
-#endif
+ /*
+ * Some additional libusb debugging please.
+ * We use the same level debug between MTP and USB.
+ */
+ usb_set_debug(LIBMTP_debug);
+
usb_init();
usb_find_busses();
usb_find_devices();
@@ -303,7 +302,7 @@ static int probe_device_descriptor(struct usb_device *dev, FILE *dumpfile)
devname,
sizeof(devname));
if (devname[0] != '\0' && strcmp(devname, "usb-storage")) {
- printf("avoid probing device using kernel interface \"%s\"\n", devname);
+ LIBMTP_INFO("avoid probing device using kernel interface \"%s\"\n", devname);
return 0;
}
}
@@ -313,7 +312,7 @@ static int probe_device_descriptor(struct usb_device *dev, FILE *dumpfile)
}
} else {
if (dev->descriptor.bNumConfigurations)
- printf("dev->config is NULL in probe_device_descriptor yet dev->descriptor.bNumConfigurations > 0\n");
+ LIBMTP_INFO("dev->config is NULL in probe_device_descriptor yet dev->descriptor.bNumConfigurations > 0\n");
}
/* Read the special descriptor */
@@ -392,7 +391,7 @@ static int probe_device_descriptor(struct usb_device *dev, FILE *dumpfile)
if (ret == -1) {
/* TODO: Implement callback function to let managing program know there
was a problem, along with description of the problem */
- fprintf(stderr, "Potential MTP Device with VendorID:%04x and "
+ LIBMTP_ERROR("Potential MTP Device with VendorID:%04x and "
"ProductID:%04x encountered an error responding to "
"control message 2.\n"
"Problems may arrise but continuing\n",
@@ -400,7 +399,7 @@ static int probe_device_descriptor(struct usb_device *dev, FILE *dumpfile)
} else if (ret <= 0x15) {
/* TODO: Implement callback function to let managing program know there
was a problem, along with description of the problem */
- fprintf(stderr, "Potential MTP Device with VendorID:%04x and "
+ LIBMTP_ERROR("Potential MTP Device with VendorID:%04x and "
"ProductID:%04x responded to control message 2 with a "
"response that was too short. Problems may arrise but "
"continuing\n",
@@ -408,7 +407,7 @@ static int probe_device_descriptor(struct usb_device *dev, FILE *dumpfile)
} else if ((buf[0x12] != 'M') || (buf[0x13] != 'T') || (buf[0x14] != 'P')) {
/* TODO: Implement callback function to let managing program know there
was a problem, along with description of the problem */
- fprintf(stderr, "Potential MTP Device with VendorID:%04x and "
+ LIBMTP_ERROR("Potential MTP Device with VendorID:%04x and "
"ProductID:%04x encountered an error responding to "
"control message 2\n"
"Problems may arrise but continuing\n",
@@ -520,7 +519,7 @@ LIBMTP_error_number_t LIBMTP_Detect_Raw_Devices(LIBMTP_raw_device_t ** devices,
*numdevs = 0;
return ret;
} else if (ret != LIBMTP_ERROR_NONE) {
- fprintf(stderr, "LIBMTP PANIC: get_mtp_usb_device_list() "
+ LIBMTP_ERROR("LIBMTP PANIC: get_mtp_usb_device_list() "
"error code: %d on line %d\n", ret, __LINE__);
return ret;
}
@@ -563,25 +562,24 @@ LIBMTP_error_number_t LIBMTP_Detect_Raw_Devices(LIBMTP_raw_device_t ** devices,
retdevs[i].device_entry.vendor = mtp_device_table[j].vendor;
retdevs[i].device_entry.product = mtp_device_table[j].product;
retdevs[i].device_entry.device_flags = mtp_device_table[j].device_flags;
-#ifdef ENABLE_USB_BULK_DEBUG
+
// This device is known to the developers
- fprintf(stderr, "Device %d (VID=%04x and PID=%04x) is a %s %s.\n",
+ LIBMTP_ERROR("Device %d (VID=%04x and PID=%04x) is a %s %s.\n",
i,
dev->libusb_device->descriptor.idVendor,
dev->libusb_device->descriptor.idProduct,
mtp_device_table[j].vendor,
mtp_device_table[j].product);
-#endif
break;
}
}
if (!device_known) {
// This device is unknown to the developers
- fprintf(stderr, "Device %d (VID=%04x and PID=%04x) is UNKNOWN.\n",
+ LIBMTP_ERROR("Device %d (VID=%04x and PID=%04x) is UNKNOWN.\n",
i,
dev->libusb_device->descriptor.idVendor,
dev->libusb_device->descriptor.idProduct);
- fprintf(stderr, "Please report this VID/PID and the device model to the "
+ LIBMTP_ERROR("Please report this VID/PID and the device model to the "
"libmtp development team\n");
/*
* Trying to get iManufacturer or iProduct from the device at this
@@ -617,27 +615,27 @@ void dump_usbinfo(PTP_USB *ptp_usb)
devname[0] = '\0';
res = usb_get_driver_np(ptp_usb->handle, (int) ptp_usb->interface, devname, sizeof(devname));
if (devname[0] != '\0') {
- printf(" Using kernel interface \"%s\"\n", devname);
+ LIBMTP_INFO(" Using kernel interface \"%s\"\n", devname);
}
#endif
dev = usb_device(ptp_usb->handle);
- printf(" bcdUSB: %d\n", dev->descriptor.bcdUSB);
- printf(" bDeviceClass: %d\n", dev->descriptor.bDeviceClass);
- printf(" bDeviceSubClass: %d\n", dev->descriptor.bDeviceSubClass);
- printf(" bDeviceProtocol: %d\n", dev->descriptor.bDeviceProtocol);
- printf(" idVendor: %04x\n", dev->descriptor.idVendor);
- printf(" idProduct: %04x\n", dev->descriptor.idProduct);
- printf(" IN endpoint maxpacket: %d bytes\n", ptp_usb->inep_maxpacket);
- printf(" OUT endpoint maxpacket: %d bytes\n", ptp_usb->outep_maxpacket);
- printf(" Raw device info:\n");
- printf(" Bus location: %d\n", ptp_usb->rawdevice.bus_location);
- printf(" Device number: %d\n", ptp_usb->rawdevice.devnum);
- printf(" Device entry info:\n");
- printf(" Vendor: %s\n", ptp_usb->rawdevice.device_entry.vendor);
- printf(" Vendor id: 0x%04x\n", ptp_usb->rawdevice.device_entry.vendor_id);
- printf(" Product: %s\n", ptp_usb->rawdevice.device_entry.product);
- printf(" Vendor id: 0x%04x\n", ptp_usb->rawdevice.device_entry.product_id);
- printf(" Device flags: 0x%08x\n", ptp_usb->rawdevice.device_entry.device_flags);
+ LIBMTP_INFO(" bcdUSB: %d\n", dev->descriptor.bcdUSB);
+ LIBMTP_INFO(" bDeviceClass: %d\n", dev->descriptor.bDeviceClass);
+ LIBMTP_INFO(" bDeviceSubClass: %d\n", dev->descriptor.bDeviceSubClass);
+ LIBMTP_INFO(" bDeviceProtocol: %d\n", dev->descriptor.bDeviceProtocol);
+ LIBMTP_INFO(" idVendor: %04x\n", dev->descriptor.idVendor);
+ LIBMTP_INFO(" idProduct: %04x\n", dev->descriptor.idProduct);
+ LIBMTP_INFO(" IN endpoint maxpacket: %d bytes\n", ptp_usb->inep_maxpacket);
+ LIBMTP_INFO(" OUT endpoint maxpacket: %d bytes\n", ptp_usb->outep_maxpacket);
+ LIBMTP_INFO(" Raw device info:\n");
+ LIBMTP_INFO(" Bus location: %d\n", ptp_usb->rawdevice.bus_location);
+ LIBMTP_INFO(" Device number: %d\n", ptp_usb->rawdevice.devnum);
+ LIBMTP_INFO(" Device entry info:\n");
+ LIBMTP_INFO(" Vendor: %s\n", ptp_usb->rawdevice.device_entry.vendor);
+ LIBMTP_INFO(" Vendor id: 0x%04x\n", ptp_usb->rawdevice.device_entry.vendor_id);
+ LIBMTP_INFO(" Product: %s\n", ptp_usb->rawdevice.device_entry.product);
+ LIBMTP_INFO(" Vendor id: 0x%04x\n", ptp_usb->rawdevice.device_entry.product_id);
+ LIBMTP_INFO(" Device flags: 0x%08x\n", ptp_usb->rawdevice.device_entry.device_flags);
(void) probe_device_descriptor(dev, stdout);
}
@@ -739,9 +737,9 @@ ptp_read_func (
bytes = malloc(CONTEXT_BLOCK_SIZE);
while (curread < size) {
-#ifdef ENABLE_USB_BULK_DEBUG
- printf("Remaining size to read: 0x%04lx bytes\n", size - curread);
-#endif
+
+ LIBMTP_USB_DEBUG("Remaining size to read: 0x%04lx bytes\n", size - curread);
+
// check equal to condition here
if (size - curread < CONTEXT_BLOCK_SIZE)
{
@@ -761,34 +759,30 @@ ptp_read_func (
else if (toread == CONTEXT_BLOCK_SIZE_2)
toread = CONTEXT_BLOCK_SIZE_1;
else
- printf("unexpected toread size 0x%04x, 0x%04x remaining bytes\n",
+ LIBMTP_INFO("unexpected toread size 0x%04x, 0x%04x remaining bytes\n",
(unsigned int) toread, (unsigned int) (size-curread));
-#ifdef ENABLE_USB_BULK_DEBUG
- printf("Reading in 0x%04lx bytes\n", toread);
-#endif
+ LIBMTP_USB_DEBUG("Reading in 0x%04lx bytes\n", toread);
+
result = USB_BULK_READ(ptp_usb->handle, ptp_usb->inep, (char*)bytes, toread, ptp_usb->timeout);
-#ifdef ENABLE_USB_BULK_DEBUG
- printf("Result of read: 0x%04x\n", result);
-#endif
+
+ LIBMTP_USB_DEBUG("Result of read: 0x%04x\n", result);
if (result < 0) {
return PTP_ERROR_IO;
}
-#ifdef ENABLE_USB_BULK_DEBUG
- printf("<==USB IN\n");
+
+ LIBMTP_USB_DEBUG("<==USB IN\n");
if (result == 0)
- printf("Zero Read\n");
+ LIBMTP_USB_DEBUG("Zero Read\n");
else
- data_dump_ascii (stdout,bytes,result,16);
-#endif
+ LIBMTP_USB_DATA(bytes, result, 16);
// want to discard extra byte
if (expect_terminator_byte && result == toread)
{
-#ifdef ENABLE_USB_BULK_DEBUG
- printf("<==USB IN\nDiscarding extra byte\n");
-#endif
+ LIBMTP_USB_DEBUG("<==USB IN\nDiscarding extra byte\n");
+
result--;
}
@@ -830,13 +824,12 @@ ptp_read_func (
char temp;
int zeroresult = 0;
-#ifdef ENABLE_USB_BULK_DEBUG
- printf("<==USB IN\n");
- printf("Zero Read\n");
-#endif
+ LIBMTP_USB_DEBUG("<==USB IN\n");
+ LIBMTP_USB_DEBUG("Zero Read\n");
+
zeroresult = USB_BULK_READ(ptp_usb->handle, ptp_usb->inep, &temp, 0, ptp_usb->timeout);
if (zeroresult != 0)
- printf("LIBMTP panic: unable to read in zero packet, response 0x%04x", zeroresult);
+ LIBMTP_INFO("LIBMTP panic: unable to read in zero packet, response 0x%04x", zeroresult);
}
return PTP_RC_OK;
@@ -876,10 +869,10 @@ ptp_write_func (
return getfunc_ret;
while (usbwritten < towrite) {
result = USB_BULK_WRITE(ptp_usb->handle,ptp_usb->outep,((char*)bytes+usbwritten),towrite-usbwritten,ptp_usb->timeout);
-#ifdef ENABLE_USB_BULK_DEBUG
- printf("USB OUT==>\n");
- data_dump_ascii (stdout,bytes+usbwritten,result,16);
-#endif
+
+ LIBMTP_USB_DEBUG("USB OUT==>\n");
+ LIBMTP_USB_DATA(bytes+usbwritten, result, 16);
+
if (result < 0) {
return PTP_ERROR_IO;
}
@@ -918,10 +911,10 @@ ptp_write_func (
// If this is the last transfer send a zero write if required
if (ptp_usb->current_transfer_complete >= ptp_usb->current_transfer_total) {
if ((towrite % ptp_usb->outep_maxpacket) == 0) {
-#ifdef ENABLE_USB_BULK_DEBUG
- printf("USB OUT==>\n");
- printf("Zero Write\n");
-#endif
+
+ LIBMTP_USB_DEBUG("USB OUT==>\n");
+ LIBMTP_USB_DEBUG("Zero Write\n");
+
result=USB_BULK_WRITE(ptp_usb->handle,ptp_usb->outep,(char *)"x",0,ptp_usb->timeout);
}
}
@@ -1035,12 +1028,12 @@ ptp_usb_sendreq (PTPParams* params, PTPContainer* req)
PTPDataHandler memhandler;
unsigned long written = 0;
unsigned long towrite;
-#ifdef ENABLE_USB_BULK_DEBUG
+
char txt[256];
(void) ptp_render_opcode (params, req->Code, sizeof(txt), txt);
- printf("REQUEST: 0x%04x, %s\n", req->Code, txt);
-#endif
+ LIBMTP_USB_DEBUG("REQUEST: 0x%04x, %s\n", req->Code, txt);
+
/* build appropriate USB container */
usbreq.length=htod32(PTP_USB_BULK_REQ_LEN-
(sizeof(uint32_t)*(5-req->Nparam)));
@@ -1086,9 +1079,9 @@ ptp_usb_senddata (PTPParams* params, PTPContainer* ptp,
uint32_t bytes_left_to_transfer;
PTPDataHandler memhandler;
-#ifdef ENABLE_USB_BULK_DEBUG
- printf("SEND DATA PHASE\n");
-#endif
+
+ LIBMTP_USB_DEBUG("SEND DATA PHASE\n");
+
/* build appropriate USB container */
usbdata.length = htod32(PTP_USB_BULK_HDR_LEN+size);
usbdata.type = htod16(PTP_USB_CONTAINER_DATA);
@@ -1175,9 +1168,9 @@ ptp_usb_getdata (PTPParams* params, PTPContainer* ptp, PTPDataHandler *handler)
unsigned long written;
PTP_USB *ptp_usb = (PTP_USB *) params->data;
-#ifdef ENABLE_USB_BULK_DEBUG
- printf("GET DATA PHASE\n");
-#endif
+
+ LIBMTP_USB_DEBUG("GET DATA PHASE\n");
+
memset(&usbdata,0,sizeof(usbdata));
do {
unsigned long len, rlen;
@@ -1295,27 +1288,27 @@ ptp_usb_getdata (PTPParams* params, PTPContainer* ptp, PTPDataHandler *handler)
if (FLAG_NO_ZERO_READS(ptp_usb) &&
len+PTP_USB_BULK_HDR_LEN == PTP_USB_BULK_HS_MAX_PACKET_LEN_READ) {
-#ifdef ENABLE_USB_BULK_DEBUG
- printf("Reading in extra terminating byte\n");
-#endif
+
+ LIBMTP_USB_DEBUG("Reading in extra terminating byte\n");
+
// need to read in extra byte and discard it
int result = 0;
char byte = 0;
result = USB_BULK_READ(ptp_usb->handle, ptp_usb->inep, &byte, 1, ptp_usb->timeout);
if (result != 1)
- printf("Could not read in extra byte for PTP_USB_BULK_HS_MAX_PACKET_LEN_READ long file, return value 0x%04x\n", result);
+ LIBMTP_INFO("Could not read in extra byte for PTP_USB_BULK_HS_MAX_PACKET_LEN_READ long file, return value 0x%04x\n", result);
} else if (len+PTP_USB_BULK_HDR_LEN == PTP_USB_BULK_HS_MAX_PACKET_LEN_READ && params->split_header_data == 0) {
int zeroresult = 0;
char zerobyte = 0;
-#ifdef ENABLE_USB_BULK_DEBUG
- printf("Reading in zero packet after header\n");
-#endif
- zeroresult = USB_BULK_READ(ptp_usb->handle, ptp_usb->inep, &zerobyte, 0, ptp_usb->timeout);
+
+ LIBMTP_INFO("Reading in zero packet after header\n");
+
+ zeroresult = USB_BULK_READ(ptp_usb->handle, ptp_usb->inep, &zerobyte, 0, ptp_usb->timeout);
if (zeroresult != 0)
- printf("LIBMTP panic: unable to read in zero packet, response 0x%04x", zeroresult);
+ LIBMTP_INFO("LIBMTP panic: unable to read in zero packet, response 0x%04x", zeroresult);
}
/* Is that all of data? */
@@ -1342,9 +1335,9 @@ ptp_usb_getresp (PTPParams* params, PTPContainer* resp)
PTPUSBBulkContainer usbresp;
PTP_USB *ptp_usb = (PTP_USB *)(params->data);
-#ifdef ENABLE_USB_BULK_DEBUG
- printf("RESPONSE: ");
-#endif
+
+ LIBMTP_USB_DEBUG("RESPONSE: ");
+
memset(&usbresp,0,sizeof(usbresp));
/* read response, it should never be longer than sizeof(usbresp) */
ret = ptp_usb_getpacket(params, &usbresp, &rlen);
@@ -1369,9 +1362,9 @@ ptp_usb_getresp (PTPParams* params, PTPContainer* resp)
if (dtoh16(usbresp.code)!=resp->Code) {
ret = dtoh16(usbresp.code);
}
-#ifdef ENABLE_USB_BULK_DEBUG
- printf("%04x\n", ret);
-#endif
+
+ LIBMTP_USB_DEBUG("%04x\n", ret);
+
if (ret!=PTP_RC_OK) {
/* libusb_glue_error (params,
"PTP: request code 0x%04x getting resp error 0x%04x",
@@ -1558,37 +1551,33 @@ static int init_ptp_usb (PTPParams* params, PTP_USB* ptp_usb, struct usb_device*
ret = usb_control_msg(device_handle,
USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
0xaa, 0x00, 0x04, buf, 0x40, 1000);
-#ifdef ENABLE_USB_BULK_DEBUG
- fprintf(stdout, "BlackBerry magic part 1:\n");
- data_dump_ascii(stdout, buf, ret, 16);
-#endif
+ LIBMTP_USB_DEBUG("BlackBerry magic part 1:\n");
+ LIBMTP_USB_DATA(buf, ret, 16);
+
usleep(1000);
// This control message is unnecessary
ret = usb_control_msg(device_handle,
USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
0xa5, 0x00, 0x01, buf, 0x02, 1000);
-#ifdef ENABLE_USB_BULK_DEBUG
- fprintf(stdout, "BlackBerry magic part 2:\n");
- data_dump_ascii(stdout, buf, ret, 16);
-#endif
+ LIBMTP_USB_DEBUG("BlackBerry magic part 2:\n");
+ LIBMTP_USB_DATA(buf, ret, 16);
+
usleep(1000);
// This control message is unnecessary
ret = usb_control_msg(device_handle,
USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
0xa8, 0x00, 0x01, buf, 0x05, 1000);
-#ifdef ENABLE_USB_BULK_DEBUG
- fprintf(stdout, "BlackBerry magic part 3:\n");
- data_dump_ascii(stdout, buf, ret, 16);
-#endif
+ LIBMTP_USB_DEBUG("BlackBerry magic part 3:\n");
+ LIBMTP_USB_DATA(buf, ret, 16);
+
usleep(1000);
// This control message is unnecessary
ret = usb_control_msg(device_handle,
USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
0xa8, 0x00, 0x01, buf, 0x11, 1000);
-#ifdef ENABLE_USB_BULK_DEBUG
- fprintf(stdout, "BlackBerry magic part 4:\n");
- data_dump_ascii(stdout, buf, ret, 16);
-#endif
+ LIBMTP_USB_DEBUG("BlackBerry magic part 4:\n");
+ LIBMTP_USB_DATA(buf, ret, 16);
+
usleep(1000);
}
}
@@ -1606,7 +1595,7 @@ static void clear_stall(PTP_USB* ptp_usb)
if (ret<0) {
perror ("inep: usb_get_endpoint_status()");
} else if (status) {
- printf("Clearing stall on IN endpoint\n");
+ LIBMTP_INFO("Clearing stall on IN endpoint\n");
ret = usb_clear_stall_feature(ptp_usb,ptp_usb->inep);
if (ret<0) {
perror ("usb_clear_stall_feature()");
@@ -1619,7 +1608,7 @@ static void clear_stall(PTP_USB* ptp_usb)
if (ret<0) {
perror("outep: usb_get_endpoint_status()");
} else if (status) {
- printf("Clearing stall on OUT endpoint\n");
+ LIBMTP_INFO("Clearing stall on OUT endpoint\n");
ret = usb_clear_stall_feature(ptp_usb,ptp_usb->outep);
if (ret<0) {
perror("usb_clear_stall_feature()");
@@ -1819,13 +1808,13 @@ LIBMTP_error_number_t configure_usb_device(LIBMTP_raw_device_t *device,
&ptp_usb->intep);
if (err) {
- fprintf(stderr, "LIBMTP PANIC: Unable to find interface & endpoints of device\n");
+ LIBMTP_ERROR("LIBMTP PANIC: Unable to find interface & endpoints of device\n");
return LIBMTP_ERROR_CONNECTING;
}
/* Attempt to initialize this device */
if (init_ptp_usb(params, ptp_usb, libusb_device) < 0) {
- fprintf(stderr, "LIBMTP PANIC: Unable to initialize device\n");
+ LIBMTP_ERROR("LIBMTP PANIC: Unable to initialize device\n");
return LIBMTP_ERROR_CONNECTING;
}
@@ -1834,11 +1823,11 @@ LIBMTP_error_number_t configure_usb_device(LIBMTP_raw_device_t *device,
* have not used LIBMTP_Release_Device on exit
*/
if ((ret = ptp_opensession(params, 1)) == PTP_ERROR_IO) {
- fprintf(stderr, "PTP_ERROR_IO: Trying again after re-initializing USB interface\n");
+ LIBMTP_ERROR("PTP_ERROR_IO: Trying again after re-initializing USB interface\n");
close_usb(ptp_usb);
if(init_ptp_usb(params, ptp_usb, libusb_device) <0) {
- fprintf(stderr, "LIBMTP PANIC: Could not open session on device\n");
+ LIBMTP_ERROR("LIBMTP PANIC: Could not open session on device\n");
return LIBMTP_ERROR_CONNECTING;
}
@@ -1848,13 +1837,13 @@ LIBMTP_error_number_t configure_usb_device(LIBMTP_raw_device_t *device,
/* Was the transaction id invalid? Try again */
if (ret == PTP_RC_InvalidTransactionID) {
- fprintf(stderr, "LIBMTP WARNING: Transaction ID was invalid, increment and try again\n");
+ LIBMTP_ERROR("LIBMTP WARNING: Transaction ID was invalid, increment and try again\n");
params->transaction_id += 10;
ret = ptp_opensession(params, 1);
}
if (ret != PTP_RC_SessionAlreadyOpened && ret != PTP_RC_OK) {
- fprintf(stderr, "LIBMTP PANIC: Could not open session! "
+ LIBMTP_ERROR("LIBMTP PANIC: Could not open session! "
"(Return code %d)\n Try to reset the device.\n",
ret);
usb_release_interface(ptp_usb->handle,
@@ -1871,7 +1860,7 @@ LIBMTP_error_number_t configure_usb_device(LIBMTP_raw_device_t *device,
void close_device (PTP_USB *ptp_usb, PTPParams *params)
{
if (ptp_closesession(params)!=PTP_RC_OK)
- fprintf(stderr,"ERROR: Could not close session!\n");
+ LIBMTP_ERROR("ERROR: Could not close session!\n");
close_usb(ptp_usb);
}
diff --git a/src/libusb-glue.h b/src/libusb-glue.h
index 7bf2e7f..862f72a 100644
--- a/src/libusb-glue.h
+++ b/src/libusb-glue.h
@@ -40,6 +40,22 @@
extern "C" {
#endif /* __cplusplus */
+/**
+ * Debug macro
+ */
+#define LIBMTP_USB_DEBUG(format, args...) \
+ do { \
+ if ((LIBMTP_debug & 0x04) != 0) \
+ fprintf(stdout, "LIBMTP %s[%d]: " format, __FUNCTION__, __LINE__, ##args); \
+ } while (0)
+
+#define LIBMTP_USB_DATA(buffer, length, base) \
+ do { \
+ if ((LIBMTP_debug & 0x08) != 0) \
+ data_dump_ascii (stdout, buffer, length, base); \
+ } while (0)
+
+
#define USB_BULK_READ usb_bulk_read
#define USB_BULK_WRITE usb_bulk_write
diff --git a/src/playlist-spl.c b/src/playlist-spl.c
index 95844d0..e6e736c 100644
--- a/src/playlist-spl.c
+++ b/src/playlist-spl.c
@@ -43,15 +43,15 @@
#include "playlist-spl.h"
-// set this to 1 to add lots of messy debug output to the playlist code
-#define DEBUG_ENABLED 0
+/**
+ * Debug macro
+ */
+#define LIBMTP_PLST_DEBUG(format, args...) \
+ do { \
+ if ((LIBMTP_debug & 0x02) != 0) \
+ fprintf(stdout, "LIBMTP %s[%d]: " format, __FUNCTION__, __LINE__, ##args); \
+ } while (0)
-// debug macro
-// d = indenting depth
-#define IF_DEBUG() if(DEBUG_ENABLED) {\
- printf("%s:%u:%s(): ", __FILE__, __LINE__, __func__); \
- } \
- if(DEBUG_ENABLED)
// Internal singly linked list of strings
// used to hold .spl playlist in memory
@@ -129,23 +129,23 @@ void spl_to_playlist_t(LIBMTP_mtpdevice_t* device, PTPObjectInfo *oi,
pl->tracks = NULL;
pl->no_tracks = 0;
- IF_DEBUG() printf("pl->name='%s'\n",pl->name);
+ LIBMTP_PLST_DEBUG("pl->name='%s'\n", pl->name);
// open a temporary file
char tmpname[] = "/tmp/mtp-spl2pl-XXXXXX";
int fd = mkstemp(tmpname);
if(fd < 0) {
- printf("failed to make temp file for %s.spl -> %s, errno=%s\n", pl->name, tmpname, strerror(errno));
+ LIBMTP_ERROR("failed to make temp file for %s.spl -> %s, errno=%s\n", pl->name, tmpname, strerror(errno));
return;
}
// make sure the file will be deleted afterwards
if(unlink(tmpname) < 0)
- printf("failed to delete temp file for %s.spl -> %s, errno=%s\n", pl->name, tmpname, strerror(errno));
+ LIBMTP_ERROR("failed to delete temp file for %s.spl -> %s, errno=%s\n", pl->name, tmpname, strerror(errno));
int ret = LIBMTP_Get_File_To_File_Descriptor(device, pl->playlist_id, fd, NULL, NULL);
if( ret < 0 ) {
// FIXME add_ptp_error_to_errorstack(device, ret, "LIBMTP_Get_Playlist: Could not get .spl playlist file.");
close(fd);
- printf("FIXME closed\n");
+ LIBMTP_INFO("FIXME closed\n");
}
text_t* p = read_into_spl_text_t(device, fd);
@@ -159,14 +159,14 @@ void spl_to_playlist_t(LIBMTP_mtpdevice_t* device, PTPObjectInfo *oi,
// convert the playlist listing to track ids
pl->no_tracks = trackno_spl_text_t(p);
- IF_DEBUG() printf("%u track%s found\n", pl->no_tracks, pl->no_tracks==1?"":"s");
+ LIBMTP_PLST_DEBUG("%u track%s found\n", pl->no_tracks, pl->no_tracks==1?"":"s");
pl->tracks = malloc(sizeof(uint32_t)*(pl->no_tracks));
tracks_from_spl_text_t(p, pl->tracks, folders, files);
free_spl_text_t(p);
// debug: add a break since this is the top level function call
- IF_DEBUG() printf("------------\n\n");
+ LIBMTP_PLST_DEBUG("------------\n\n");
}
@@ -189,17 +189,17 @@ int playlist_t_to_spl(LIBMTP_mtpdevice_t *device,
char tmpname[] = "/tmp/mtp-spl2pl-XXXXXX"; // must be a var since mkstemp modifies it
- IF_DEBUG() printf("pl->name='%s'\n",pl->name);
+ LIBMTP_PLST_DEBUG("pl->name='%s'\n",pl->name);
// open a file descriptor
int fd = mkstemp(tmpname);
if(fd < 0) {
- printf("failed to make temp file for %s.spl -> %s, errno=%s\n", pl->name, tmpname, strerror(errno));
+ LIBMTP_ERROR("failed to make temp file for %s.spl -> %s, errno=%s\n", pl->name, tmpname, strerror(errno));
return -1;
}
// make sure the file will be deleted afterwards
if(unlink(tmpname) < 0)
- printf("failed to delete temp file for %s.spl -> %s, errno=%s\n", pl->name, tmpname, strerror(errno));
+ LIBMTP_ERROR("failed to delete temp file for %s.spl -> %s, errno=%s\n", pl->name, tmpname, strerror(errno));
// decide on which version of the .spl format to use
uint32_t ver_major;
@@ -208,8 +208,8 @@ int playlist_t_to_spl(LIBMTP_mtpdevice_t *device,
if(FLAG_PLAYLIST_SPL_V2(ptp_usb)) ver_major = 2;
else ver_major = 1; // FLAG_PLAYLIST_SPL_V1()
- IF_DEBUG() printf("%u track%s\n", pl->no_tracks, pl->no_tracks==1?"":"s");
- IF_DEBUG() printf(".spl version %d.%02d\n", ver_major, ver_minor);
+ LIBMTP_PLST_DEBUG("%u track%s\n", pl->no_tracks, pl->no_tracks==1?"":"s");
+ LIBMTP_PLST_DEBUG(".spl version %d.%02d\n", ver_major, ver_minor);
// create the text for the playlist
spl_text_t_from_tracks(&t, pl->tracks, pl->no_tracks, ver_major, ver_minor, NULL, folders, files);
@@ -228,7 +228,7 @@ int playlist_t_to_spl(LIBMTP_mtpdevice_t *device,
f->filetype = LIBMTP_FILETYPE_UNKNOWN;
f->next = NULL;
- IF_DEBUG() printf("%s is %dB\n", f->filename, (int)f->filesize);
+ LIBMTP_PLST_DEBUG("%s is %dB\n", f->filename, (int)f->filesize);
// push the playlist to the device
lseek(fd, 0, SEEK_SET); // reset file desc. to start of file
@@ -240,7 +240,7 @@ int playlist_t_to_spl(LIBMTP_mtpdevice_t *device,
// release the memory when we're done with it
close(fd);
// debug: add a break since this is the top level function call
- IF_DEBUG() printf("------------\n\n");
+ LIBMTP_PLST_DEBUG("------------\n\n");
return ret;
}
@@ -262,7 +262,7 @@ int playlist_t_to_spl(LIBMTP_mtpdevice_t *device,
int update_spl_playlist(LIBMTP_mtpdevice_t *device,
LIBMTP_playlist_t * const newlist)
{
- IF_DEBUG() printf("pl->name='%s'\n",newlist->name);
+ LIBMTP_PLST_DEBUG("pl->name='%s'\n",newlist->name);
// read in the playlist of interest
LIBMTP_playlist_t * old = LIBMTP_Get_Playlist(device, newlist->playlist_id);
@@ -283,18 +283,16 @@ int update_spl_playlist(LIBMTP_mtpdevice_t *device,
// if not, kill the playlist and replace it
if(delta) {
- IF_DEBUG() printf("new tracks detected:\n");
- IF_DEBUG() printf("delete old playlist and build a new one\n");
- IF_DEBUG() printf(" NOTE: new playlist_id will result!\n");
+ LIBMTP_PLST_DEBUG("new tracks detected:\n");
+ LIBMTP_PLST_DEBUG("delete old playlist and build a new one\n");
+ LIBMTP_PLST_DEBUG(" NOTE: new playlist_id will result!\n");
if(LIBMTP_Delete_Object(device, old->playlist_id) != 0)
return -1;
- IF_DEBUG() {
- if(strcmp(old->name,newlist->name) == 0)
- printf("name unchanged\n");
- else
- printf("name is changing too -> %s\n",newlist->name);
- }
+ if(strcmp(old->name,newlist->name) == 0)
+ LIBMTP_PLST_DEBUG("name unchanged\n");
+ else
+ LIBMTP_PLST_DEBUG("name is changing too -> %s\n",newlist->name);
return LIBMTP_Create_New_Playlist(device, newlist);
}
@@ -302,8 +300,8 @@ int update_spl_playlist(LIBMTP_mtpdevice_t *device,
// update the name only
if(strcmp(old->name,newlist->name) != 0) {
- IF_DEBUG() printf("ONLY name is changing -> %s\n",newlist->name);
- IF_DEBUG() printf("playlist_id will remain unchanged\n");
+ LIBMTP_PLST_DEBUG("ONLY name is changing -> %s\n",newlist->name);
+ LIBMTP_PLST_DEBUG("playlist_id will remain unchanged\n");
char* s = malloc(sizeof(char)*(strlen(newlist->name)+5));
strcpy(s, newlist->name);
strcat(s,".spl"); // FIXME check for success
@@ -312,7 +310,7 @@ int update_spl_playlist(LIBMTP_mtpdevice_t *device,
return ret;
}
- IF_DEBUG() printf("no change\n");
+ LIBMTP_PLST_DEBUG("no change\n");
return 0; // nothing to be done, success
}
@@ -356,16 +354,16 @@ static text_t* read_into_spl_text_t(LIBMTP_mtpdevice_t *device, const int fd)
it = t; // set ptr to start of buffer
rdcnt = read(fd, it, sizeof(char)*MAXREAD);
if(rdcnt < 0)
- printf("load_spl_fd read err %s\n", strerror(errno));
+ LIBMTP_INFO("load_spl_fd read err %s\n", strerror(errno));
else if(rdcnt == 0) { // for EOF, fix rdcnt
if(it-t == MAXREAD)
- printf("error -- buffer too small to read in .spl playlist entry\n");
+ LIBMTP_ERROR("error -- buffer too small to read in .spl playlist entry\n");
rdcnt = lseek(fd, 0, SEEK_CUR) - offcnt;
eof = 1;
}
- IF_DEBUG() printf("read buff= {%dB new, %dB old/left-over}%s\n",(int)rdcnt, (int)(iw-w), eof?", EOF":"");
+ LIBMTP_PLST_DEBUG("read buff= {%dB new, %dB old/left-over}%s\n",(int)rdcnt, (int)(iw-w), eof?", EOF":"");
// while more input bytes
char* it_end = t + rdcnt;
@@ -407,7 +405,7 @@ static text_t* read_into_spl_text_t(LIBMTP_mtpdevice_t *device, const int fd)
tail->text = utf16_to_utf8(device, (uint16_t*) w);
iw = w; // start again
- IF_DEBUG() printf("line: %s\n", tail->text);
+ LIBMTP_PLST_DEBUG("line: %s\n", tail->text);
}
// prevent buffer overflow
@@ -416,7 +414,7 @@ static text_t* read_into_spl_text_t(LIBMTP_mtpdevice_t *device, const int fd)
// we are dropping all the processed bytes for this line and
// proceeding on as if everything is okay, probably losing a track
// from the playlist
- printf("ERROR %s:%u:%s(): buffer overflow! .spl line too long @ %zuB\n",
+ LIBMTP_ERROR("ERROR %s:%u:%s(): buffer overflow! .spl line too long @ %zuB\n",
__FILE__, __LINE__, __func__, WSIZE);
iw = w; // reset buffer
}
@@ -461,16 +459,14 @@ static void write_from_spl_text_t(LIBMTP_mtpdevice_t *device,
const size_t len = ucs2_strlen((uint16_t*)t)*sizeof(uint16_t);
int i;
- IF_DEBUG() {
- printf("\nutf8=%s ",p->text);
- for(i=0;i<strlen(p->text);i++)
- printf("%02x ", p->text[i] & 0xff);
- printf("\n");
- printf("ucs2=");
- for(i=0;i<ucs2_strlen((uint16_t*)t)*sizeof(uint16_t);i++)
- printf("%02x ", t[i] & 0xff);
- printf("\n");
- }
+ LIBMTP_PLST_DEBUG("\nutf8=%s ",p->text);
+ for(i=0;i<strlen(p->text);i++)
+ LIBMTP_PLST_DEBUG("%02x ", p->text[i] & 0xff);
+ LIBMTP_PLST_DEBUG("\n");
+ LIBMTP_PLST_DEBUG("ucs2=");
+ for(i=0;i<ucs2_strlen((uint16_t*)t)*sizeof(uint16_t);i++)
+ LIBMTP_PLST_DEBUG("%02x ", t[i] & 0xff);
+ LIBMTP_PLST_DEBUG("\n");
// write: utf8 -> utf16
ret += write(fd, t, len);
@@ -480,16 +476,16 @@ static void write_from_spl_text_t(LIBMTP_mtpdevice_t *device,
// check for failures
if(ret < 0)
- printf("write spl file failed: %s\n", strerror(errno));
+ LIBMTP_ERROR("write spl file failed: %s\n", strerror(errno));
else if(ret != len +2)
- printf("write spl file wrong number of bytes ret=%d len=%d '%s'\n", (int)ret, (int)len, p->text);
+ LIBMTP_ERROR("write spl file wrong number of bytes ret=%d len=%d '%s'\n", (int)ret, (int)len, p->text);
// write carriage return, line feed in ucs2
ret = write(fd, "\r\0\n\0", 4);
if(ret < 0)
- printf("write spl file failed: %s\n", strerror(errno));
+ LIBMTP_ERROR("write spl file failed: %s\n", strerror(errno));
else if(ret != 4)
- printf("failed to write the correct number of bytes '\\n'!\n");
+ LIBMTP_ERROR("failed to write the correct number of bytes '\\n'!\n");
// fake out count (first time through has two extra bytes from BOM)
ret = 2;
@@ -519,13 +515,14 @@ static void free_spl_text_t(text_t* p)
/**
* Print a linked-list of strings to stdout.
+ * Used to debug.
*
* @param p the list to print
*/
static void print_spl_text_t(text_t* p)
{
while(p != NULL) {
- printf("%s\n",p->text);
+ LIBMTP_PLST_DEBUG("%s\n",p->text);
p = p->next;
}
}
@@ -569,8 +566,7 @@ static void tracks_from_spl_text_t(text_t* p,
while(p != NULL) {
if(p->text[0] == '\\' ) {
tracks[c] = discover_id_from_filepath(p->text, folders, files);
- IF_DEBUG()
- printf("track %d = %s (%u)\n", c+1, p->text, tracks[c]);
+ LIBMTP_PLST_DEBUG("track %d = %s (%u)\n", c+1, p->text, tracks[c]);
c++;
}
p = p->next;
@@ -617,11 +613,10 @@ static void spl_text_t_from_tracks(text_t** p,
if(f != NULL) {
append_text_t(&c, f);
- IF_DEBUG()
- printf("track %d = %s (%u)\n", i+1, f, tracks[i]);
+ LIBMTP_PLST_DEBUG("track %d = %s (%u)\n", i+1, f, tracks[i]);
}
else
- printf("failed to find filepath for track=%d\n", tracks[i]);
+ LIBMTP_ERROR("failed to find filepath for track=%d\n", tracks[i]);
}
// FOOTER
@@ -643,10 +638,8 @@ static void spl_text_t_from_tracks(text_t** p,
c->next = NULL;
// debug
- IF_DEBUG() {
- printf(".spl playlist:\n");
- print_spl_text_t(*p);
- }
+ LIBMTP_PLST_DEBUG(".spl playlist:\n");
+ print_spl_text_t(*p);
}