libusbi.h: Add IS_EPIN() IS_EPOUT() IS_XFERIN() and IS_XFEROUT() macros
authorPeter Stuge <peter@stuge.se>
Thu, 22 Mar 2012 00:52:59 +0000 (01:52 +0100)
committerPeter Stuge <peter@stuge.se>
Thu, 22 Mar 2012 00:57:25 +0000 (01:57 +0100)
The macros make backend code a little simpler to read, since the
transfer and endpoint direction is checked in several places across
the supported platforms.

libusb/libusbi.h
libusb/os/darwin_usb.c
libusb/os/openbsd_usb.c
libusb/os/windows_usb.c

index 676b866..335df11 100644 (file)
@@ -189,6 +189,11 @@ static inline void usbi_dbg(const char *format, ...)
 #define ITRANSFER_CTX(transfer) \
        (TRANSFER_CTX(USBI_TRANSFER_TO_LIBUSB_TRANSFER(transfer)))
 
+#define IS_EPIN(ep) (0 != ((ep) & LIBUSB_ENDPOINT_IN))
+#define IS_EPOUT(ep) (!IS_EPIN(ep))
+#define IS_XFERIN(xfer) (0 != ((xfer)->endpoint & LIBUSB_ENDPOINT_IN))
+#define IS_XFEROUT(xfer) (!IS_XFERIN(xfer))
+
 /* Internal abstractions for thread synchronization and poll */
 #if defined(THREADS_POSIX)
 #include <os/threads_posix.h>
index 8815f26..e112cf5 100644 (file)
@@ -1267,7 +1267,6 @@ static int submit_bulk_transfer(struct usbi_transfer *itransfer) {
   struct darwin_device_handle_priv *priv = (struct darwin_device_handle_priv *)transfer->dev_handle->os_priv;
 
   IOReturn               ret;
-  uint8_t                is_read; /* 0 = we're reading, 1 = we're writing */
   uint8_t                transferType;
   /* None of the values below are used in libusb for bulk transfers */
   uint8_t                direction, number, interval, pipeRef, iface;
@@ -1275,10 +1274,7 @@ static int submit_bulk_transfer(struct usbi_transfer *itransfer) {
 
   struct darwin_interface *cInterface;
 
-  /* are we reading or writing? */
-  is_read = transfer->endpoint & LIBUSB_ENDPOINT_IN;
-
-  if (!is_read && transfer->flags & LIBUSB_TRANSFER_ADD_ZERO_PACKET)
+  if (IS_XFEROUT(transfer) && transfer->flags & LIBUSB_TRANSFER_ADD_ZERO_PACKET)
     return LIBUSB_ERROR_NOT_SUPPORTED;
 
   if (ep_to_pipeRef (transfer->dev_handle, transfer->endpoint, &pipeRef, &iface) != 0) {
@@ -1295,7 +1291,7 @@ static int submit_bulk_transfer(struct usbi_transfer *itransfer) {
   /* submit the request */
   /* timeouts are unavailable on interrupt endpoints */
   if (transferType == kUSBInterrupt) {
-    if (is_read)
+    if (IS_XFERIN(transfer))
       ret = (*(cInterface->interface))->ReadPipeAsync(cInterface->interface, pipeRef, transfer->buffer,
                                                      transfer->length, darwin_async_io_callback, itransfer);
     else
@@ -1304,7 +1300,7 @@ static int submit_bulk_transfer(struct usbi_transfer *itransfer) {
   } else {
     itransfer->flags |= USBI_TRANSFER_OS_HANDLES_TIMEOUT;
 
-    if (is_read)
+    if (IS_XFERIN(transfer))
       ret = (*(cInterface->interface))->ReadPipeAsyncTO(cInterface->interface, pipeRef, transfer->buffer,
                                                        transfer->length, transfer->timeout, transfer->timeout,
                                                        darwin_async_io_callback, (void *)itransfer);
@@ -1315,7 +1311,7 @@ static int submit_bulk_transfer(struct usbi_transfer *itransfer) {
   }
 
   if (ret)
-    usbi_err (TRANSFER_CTX (transfer), "bulk transfer failed (dir = %s): %s (code = 0x%08x)", is_read ? "In" : "Out",
+    usbi_err (TRANSFER_CTX (transfer), "bulk transfer failed (dir = %s): %s (code = 0x%08x)", IS_XFERIN(transfer) ? "In" : "Out",
               darwin_error_str(ret), ret);
 
   return darwin_to_libusb (ret);
@@ -1327,7 +1323,6 @@ static int submit_iso_transfer(struct usbi_transfer *itransfer) {
   struct darwin_device_handle_priv *priv = (struct darwin_device_handle_priv *)transfer->dev_handle->os_priv;
 
   IOReturn                kresult;
-  uint8_t                 is_read; /* 0 = we're writing, 1 = we're reading */
   uint8_t                 pipeRef, iface;
   UInt64                  frame;
   AbsoluteTime            atTime;
@@ -1335,9 +1330,6 @@ static int submit_iso_transfer(struct usbi_transfer *itransfer) {
 
   struct darwin_interface *cInterface;
 
-  /* are we reading or writing? */
-  is_read = transfer->endpoint & LIBUSB_ENDPOINT_IN;
-
   /* construct an array of IOUSBIsocFrames, reuse the old one if possible */
   if (tpriv->isoc_framelist && tpriv->num_iso_packets != transfer->num_iso_packets) {
     free(tpriv->isoc_framelist);
@@ -1381,7 +1373,7 @@ static int submit_iso_transfer(struct usbi_transfer *itransfer) {
     frame = cInterface->frames[transfer->endpoint];
 
   /* submit the request */
-  if (is_read)
+  if (IS_XFERIN(transfer))
     kresult = (*(cInterface->interface))->ReadIsochPipeAsync(cInterface->interface, pipeRef, transfer->buffer, frame,
                                                             transfer->num_iso_packets, tpriv->isoc_framelist, darwin_async_io_callback,
                                                             itransfer);
@@ -1393,7 +1385,7 @@ static int submit_iso_transfer(struct usbi_transfer *itransfer) {
   cInterface->frames[transfer->endpoint] = frame + transfer->num_iso_packets / 8;
 
   if (kresult != kIOReturnSuccess) {
-    usbi_err (TRANSFER_CTX (transfer), "isochronous transfer failed (dir: %s): %s", is_read ? "In" : "Out",
+    usbi_err (TRANSFER_CTX (transfer), "isochronous transfer failed (dir: %s): %s", IS_XFERIN(transfer) ? "In" : "Out",
               darwin_error_str(kresult));
     free (tpriv->isoc_framelist);
     tpriv->isoc_framelist = NULL;
index 9b03277..bbafa82 100644 (file)
@@ -436,7 +436,7 @@ obsd_submit_transfer(struct usbi_transfer *itransfer)
                err = _sync_control_transfer(itransfer);
                break;
        case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
-               if (0 == transfer->endpoint & LIBUSB_ENDPOINT_IN) {
+               if (IS_XFEROUT(transfer)) {
                        /* Isochronous write is not supported */
                        err = LIBUSB_ERROR_NOT_SUPPORTED;
                        break;
@@ -445,7 +445,7 @@ obsd_submit_transfer(struct usbi_transfer *itransfer)
                break;
        case LIBUSB_TRANSFER_TYPE_BULK:
        case LIBUSB_TRANSFER_TYPE_INTERRUPT:
-               if (0 == transfer->endpoint & LIBUSB_ENDPOINT_IN &&
+               if (IS_XFEROUT(transfer) &&
                    transfer->flags & LIBUSB_TRANSFER_ADD_ZERO_PACKET) {
                        err = LIBUSB_ERROR_NOT_SUPPORTED;
                        break;
@@ -659,7 +659,7 @@ _access_endpoint(struct libusb_transfer *transfer)
        dpriv = (struct device_priv *)transfer->dev_handle->dev->os_priv;
 
        endpt = UE_GET_ADDR(transfer->endpoint);
-       mode = (transfer->endpoint & LIBUSB_ENDPOINT_IN) ? O_RDONLY : O_WRONLY;
+       mode = IS_XFERIN(transfer) ? O_RDONLY : O_WRONLY;
 
        usbi_dbg("endpoint %d mode %d", endpt, mode);
 
@@ -698,7 +698,7 @@ _sync_gen_transfer(struct usbi_transfer *itransfer)
        if ((ioctl(fd, USB_SET_TIMEOUT, &transfer->timeout)) < 0)
                return _errno_to_libusb(errno);
 
-       if (transfer->endpoint & LIBUSB_ENDPOINT_IN) {
+       if (IS_XFERIN(transfer)) {
                if ((transfer->flags & LIBUSB_TRANSFER_SHORT_NOT_OK) == 0)
                        if ((ioctl(fd, USB_SET_SHORT_XFER, &nr)) < 0)
                                return _errno_to_libusb(errno);
index e7da2ee..0ae66e9 100644 (file)
@@ -1785,7 +1785,7 @@ static int submit_bulk_transfer(struct usbi_transfer *itransfer)
        }
 
        usbi_add_pollfd(ctx, transfer_priv->pollable_fd.fd,
-               (short)((transfer->endpoint & LIBUSB_ENDPOINT_IN)?POLLIN:POLLOUT));
+               (short)(IS_XFERIN(transfer) ? POLLIN : POLLOUT));
 #if !defined(DYNAMIC_FDS)
        usbi_fd_notification(ctx);
 #endif
@@ -1807,7 +1807,7 @@ static int submit_iso_transfer(struct usbi_transfer *itransfer)
        }
 
        usbi_add_pollfd(ctx, transfer_priv->pollable_fd.fd,
-               (short)((transfer->endpoint & LIBUSB_ENDPOINT_IN)?POLLIN:POLLOUT));
+               (short)(IS_XFERIN(transfer) ? POLLIN : POLLOUT));
 #if !defined(DYNAMIC_FDS)
        usbi_fd_notification(ctx);
 #endif
@@ -1846,7 +1846,7 @@ static int windows_submit_transfer(struct usbi_transfer *itransfer)
                return submit_control_transfer(itransfer);
        case LIBUSB_TRANSFER_TYPE_BULK:
        case LIBUSB_TRANSFER_TYPE_INTERRUPT:
-               if (0 == transfer->endpoint & LIBUSB_ENDPOINT_IN &&
+               if (IS_XFEROUT(transfer) &&
                    transfer->flags & LIBUSB_TRANSFER_ADD_ZERO_PACKET)
                        return LIBUSB_ERROR_NOT_SUPPORTED;
                return submit_bulk_transfer(itransfer);
@@ -2687,7 +2687,7 @@ static int winusb_submit_bulk_transfer(struct usbi_transfer *itransfer)
        struct windows_device_handle_priv *handle_priv = _device_handle_priv(transfer->dev_handle);
        struct windows_device_priv *priv = _device_priv(transfer->dev_handle->dev);
        HANDLE winusb_handle;
-       bool direction_in, ret;
+       bool ret;
        int current_interface;
        struct winfd wfd;
 
@@ -2704,15 +2704,14 @@ static int winusb_submit_bulk_transfer(struct usbi_transfer *itransfer)
        usbi_dbg("matched endpoint %02X with interface %d", transfer->endpoint, current_interface);
 
        winusb_handle = handle_priv->interface_handle[current_interface].api_handle;
-       direction_in = transfer->endpoint & LIBUSB_ENDPOINT_IN;
 
-       wfd = usbi_create_fd(winusb_handle, direction_in?_O_RDONLY:_O_WRONLY);
+       wfd = usbi_create_fd(winusb_handle, IS_XFERIN(transfer) ? _O_RDONLY : _O_WRONLY);
        // Always use the handle returned from usbi_create_fd (wfd.handle)
        if (wfd.fd < 0) {
                return LIBUSB_ERROR_NO_MEM;
        }
 
-       if (direction_in) {
+       if (IS_XFERIN(transfer)) {
                usbi_dbg("reading %d bytes", transfer->length);
                ret = WinUsb_ReadPipe(wfd.handle, transfer->endpoint, transfer->buffer, transfer->length, NULL, wfd.overlapped);
        } else {
@@ -2842,7 +2841,7 @@ static int winusb_reset_device(struct libusb_device_handle *dev_handle)
                                                priv->usb_interface[i].endpoint[j], windows_error_str(0));
                                }
                                // FlushPipe seems to fail on OUT pipes
-                               if ( (priv->usb_interface[i].endpoint[j] & LIBUSB_ENDPOINT_IN)
+                               if (IS_EPIN(priv->usb_interface[i].endpoint[j])
                                  && (!WinUsb_FlushPipe(winusb_handle, priv->usb_interface[i].endpoint[j])) ) {
                                        usbi_err(ctx, "WinUsb_FlushPipe (pipe address %02X) failed: %s",
                                                priv->usb_interface[i].endpoint[j], windows_error_str(0));