libftdi Archives

Subject: [libftdi-1.0][PATCH 3/3] Add debug print macro: DP()

From: Yi-Shin Li <ysli@xxxxxxxxxxxxx>
To: libftdi@xxxxxxxxxxxxxxxxxxxxxxx
Date: Wed, 14 Jul 2010 11:21:27 +0800
From 1b2c9c65aa77b30f86051a711fee9a2a00e9d0b7 Mon Sep 17 00:00:00 2001
From: Yi-Shin Li <ysli@xxxxxxxxxxxxx>
Date: Mon, 12 Jul 2010 17:31:01 +0800
Subject: [PATCH 3/3] Add debug print macro: DP()

---
 src/dptrace.h |   27 ++++++++++++++++++++++++
 src/ftdi.c    |   62 +++++++++++++++++++++++++++++++++++++++++++++++++++-----
 2 files changed, 83 insertions(+), 6 deletions(-)
 create mode 100644 src/dptrace.h

diff --git a/src/dptrace.h b/src/dptrace.h
new file mode 100644
index 0000000..f15f45f
--- /dev/null
+++ b/src/dptrace.h
@@ -0,0 +1,27 @@
+#ifndef _DPTRACE_H_
+#define _DPTRACE_H_
+
+#ifndef TRACE
+#define TRACE   1      //!< 0:Trace off 1:Trace on
+#endif
+
+#if (TRACE)
+#define DP(fmt, args...)                                                \
+    do {                                                                \
+        fprintf(dptrace, "%s: (%s:%d) ",                                \
+                         __FILE__, __FUNCTION__, __LINE__ );            \
+        fprintf(dptrace, fmt, ##args);                                  \
+        fflush(dptrace);                                                \
+    } while (0)
+#define DPS(fmt, args...)                                               \
+    do {                                                                \
+        fprintf(dptrace, fmt, ##args);                                  \
+        fflush(dptrace);                                                \
+    } while (0)
+#else
+// TRACE == 0
+#define DP(fmt, args...)     do {} while(0)
+#define DPS(fmt, args...)    do {} while(0)
+#endif
+
+#endif  // _DPTRACE_H_
diff --git a/src/ftdi.c b/src/ftdi.c
index 4a57957..3b8706c 100644
--- a/src/ftdi.c
+++ b/src/ftdi.c
@@ -36,6 +36,14 @@
 
 #include "ftdi.h"
 
+// to disable DP(): #define TRACE 1
+// to dump more info: #define TRACE 2
+#define TRACE 1
+#include "dptrace.h"
+#if (TRACE!=0)
+FILE *dptrace; // dptrace = fopen("dptrace.log","w");
+#endif
+
 #define ftdi_error_return(code, str) do {  \
         ftdi->error_str = str;             \
         return code;                       \
@@ -78,6 +86,12 @@ static void ftdi_usb_close_internal (struct ftdi_context *ftdi)
 */
 int ftdi_init(struct ftdi_context *ftdi)
 {
+
+#if (TRACE!=0)
+    // dptrace = fopen("dptrace.log","w");
+    dptrace = stderr;
+#endif
+
     ftdi->usb_ctx = NULL;
     ftdi->usb_dev = NULL;
     ftdi->usb_read_timeout = 5000;
@@ -1426,12 +1440,16 @@ struct ftdi_transfer_control *ftdi_read_data_submit(struct ftdi_context *ftdi, u
     struct libusb_transfer *transfer;
     int ret;
 
-    if (ftdi == NULL || ftdi->usb_dev == NULL)
+    if (ftdi == NULL || ftdi->usb_dev == NULL) {
+        DP ("ftdi(%p) ftdi->usb_dev(%p)\n", ftdi, ftdi->usb_dev);
         return NULL;
+    }
 
     tc = (struct ftdi_transfer_control *) malloc (sizeof (*tc));
-    if (!tc)
+    if (!tc) {
+        DP ("tc(%p)\n", tc);
         return NULL;
+    }
 
     tc->ftdi = ftdi;
     tc->buf = buf;
@@ -1466,6 +1484,7 @@ struct ftdi_transfer_control *ftdi_read_data_submit(struct ftdi_context *ftdi, u
     transfer = libusb_alloc_transfer(0);
     if (!transfer)
     {
+        DP("transfer(%p)\n", transfer);
         free (tc);
         return NULL;
     }
@@ -1479,6 +1498,18 @@ struct ftdi_transfer_control *ftdi_read_data_submit(struct ftdi_context *ftdi, u
     ret = libusb_submit_transfer(transfer);
     if (ret < 0)
     {
+        DP("libusb_submit_transfer() ret(%d)\n", ret);
+        if (transfer) {
+            DP("about to libusb_cancel_transfer()...\n");
+            ret = libusb_cancel_transfer(transfer);
+            if (ret == 0) { // successfully canceled
+                DP("about to libusb_handle_events()...\n");
+        libusb_handle_events(tc->ftdi->usb_ctx);
+            } else {
+                DP("ERROR with libusb_cancel_transfer() ret(%d)\n", ret);
+            }
+        }
+        DP("about to libusb_free_transfer()...\n");
         libusb_free_transfer(transfer);
         free (tc);
         return NULL;
@@ -1505,16 +1536,23 @@ int ftdi_transfer_data_done(struct ftdi_transfer_control *tc)
 
     while (!tc->completed)
     {
+        DP ("about to libusb_handle_events()\n");
         ret = libusb_handle_events(tc->ftdi->usb_ctx);
         if (ret < 0)
         {
+            DP ("ERROR ret(%d) of libusb_handle_events()\n", ret);
             if (ret == LIBUSB_ERROR_INTERRUPTED)
                 continue;
+            DP ("about to libusb_cancel_transfer()\n");
             libusb_cancel_transfer(tc->transfer);
-            while (!tc->completed)
+            while (!tc->completed) {
+                DP ("about to libusb_handle_events()\n");
                 if (libusb_handle_events(tc->ftdi->usb_ctx) < 0)
                     break;
+            }
+            DP ("about to libusb_free_transfer()\n");
             libusb_free_transfer(tc->transfer);
+            DP ("after libusb_free_transfer()\n");
             free (tc);
             return ret;
         }
@@ -1526,9 +1564,21 @@ int ftdi_transfer_data_done(struct ftdi_transfer_control *tc)
      * at ftdi_read_data_submit(). Therefore, has to check it here.
      **/
     if (tc->transfer) {
-      if (tc->transfer->status != LIBUSB_TRANSFER_COMPLETED)
-          ret = -1;
-      libusb_free_transfer(tc->transfer);
+        if (tc->transfer->status != LIBUSB_TRANSFER_COMPLETED) {
+            DP ("ERROR transfer->status(%d)\n", tc->transfer->status);
+            DP("about to libusb_cancel_transfer()...\n");
+            ret = libusb_cancel_transfer(tc->transfer);
+            if (ret == 0) { // successfully canceled
+                DP("about to libusb_handle_events()...\n");
+                libusb_handle_events(tc->ftdi->usb_ctx);
+            } else {
+                DP("ERROR with libusb_cancel_transfer() ret(%d)n", ret);
+            }
+            ret = -1;
+            DP("about to libusb_free_transfer()...\n");
+        }
+        libusb_free_transfer(tc->transfer);
+        // DP ("after libusb_free_transfer()\n");
     }
     free(tc);
     return ret;
--
1.7.0.4



libftdi - see http://www.intra2net.com/en/developer/libftdi for details.
To unsubscribe send a mail to libftdi+unsubscribe@xxxxxxxxxxxxxxxxxxxxxxx

Current Thread
  • [libftdi-1.0][PATCH 3/3] Add debug print macro: DP(), Yi-Shin Li <=