Fixed various trivial cppcheck 1.80 warnings
[platform/upstream/libusb.git] / examples / dpfp.c
index 70944f8..77f9476 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * libusb example program to manipulate U.are.U 4000B fingerprint scanner.
- * Copyright (C) 2007 Daniel Drake <dsd@gentoo.org>
+ * Copyright © 2007 Daniel Drake <dsd@gentoo.org>
  *
  * Basic image capture program only, does not consider the powerup quirks or
  * the fact that image encryption may be enabled. Not expected to work
 #include <stdio.h>
 #include <stdlib.h>
 
-#include <libusb/libusb.h>
+#include "libusb.h"
 
 #define EP_INTR                        (1 | LIBUSB_ENDPOINT_IN)
 #define EP_DATA                        (2 | LIBUSB_ENDPOINT_IN)
-#define CTRL_IN                        (LIBUSB_TYPE_VENDOR | LIBUSB_ENDPOINT_IN)
-#define CTRL_OUT               (LIBUSB_TYPE_VENDOR | LIBUSB_ENDPOINT_OUT)
+#define CTRL_IN                        (LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_IN)
+#define CTRL_OUT               (LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_OUT)
 #define USB_RQ                 0x04
 #define INTR_LENGTH            64
 
@@ -46,8 +46,6 @@ enum {
 };
 
 static int next_state(void);
-static int submit_irq_transfer(void);
-static int submit_img_transfer(void);
 
 enum {
        STATE_AWAIT_MODE_CHANGE_AWAIT_FINGER_ON = 1,
@@ -69,7 +67,7 @@ static int do_exit = 0;
 
 static int find_dpfp_device(void)
 {
-       devh = libusb_open_device_with_vid_pid(0x05ba, 0x000a);
+       devh = libusb_open_device_with_vid_pid(NULL, 0x05ba, 0x000a);
        return devh ? 0 : -EIO;
 }
 
@@ -151,7 +149,7 @@ static int set_mode(unsigned char data)
        return 0;
 }
 
-static void cb_mode_changed(struct libusb_transfer *transfer)
+static void LIBUSB_CALL cb_mode_changed(struct libusb_transfer *transfer)
 {
        if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
                fprintf(stderr, "mode change transfer not completed!\n");
@@ -166,13 +164,13 @@ static void cb_mode_changed(struct libusb_transfer *transfer)
 
 static int set_mode_async(unsigned char data)
 {
-       unsigned char *buf = malloc(LIBUSB_CONTROL_SETUP_SIZE + 1);
+       unsigned char *buf = (unsigned char*) malloc(LIBUSB_CONTROL_SETUP_SIZE + 1);
        struct libusb_transfer *transfer;
 
        if (!buf)
                return -ENOMEM;
-       
-       transfer = libusb_alloc_transfer();
+
+       transfer = libusb_alloc_transfer(0);
        if (!transfer) {
                free(buf);
                return -ENOMEM;
@@ -210,7 +208,7 @@ static int do_sync_intr(unsigned char *data)
 }
 
 static int sync_intr(unsigned char type)
-{      
+{
        int r;
        unsigned char data[INTR_LENGTH];
 
@@ -228,13 +226,13 @@ static int save_to_file(unsigned char *data)
        FILE *fd;
        char filename[64];
 
-       sprintf(filename, "finger%d.pgm", img_idx++);
+       snprintf(filename, sizeof(filename), "finger%d.pgm", img_idx++);
        fd = fopen(filename, "w");
        if (!fd)
                return -1;
 
        fputs("P5 384 289 255 ", fd);
-       fwrite(data + 64, 1, 384*289, fd);
+       (void) fwrite(data + 64, 1, 384*289, fd);
        fclose(fd);
        printf("saved image to %s\n", filename);
        return 0;
@@ -270,7 +268,7 @@ static int next_state(void)
                printf("unrecognised state %d\n", state);
        }
        if (r < 0) {
-               fprintf(stderr, "error detected changing state");
+               fprintf(stderr, "error detected changing state\n");
                return r;
        }
 
@@ -278,13 +276,15 @@ static int next_state(void)
        return 0;
 }
 
-static void cb_irq(struct libusb_transfer *transfer)
+static void LIBUSB_CALL cb_irq(struct libusb_transfer *transfer)
 {
        unsigned char irqtype = transfer->buffer[0];
 
        if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
                fprintf(stderr, "irq transfer status %d?\n", transfer->status);
                do_exit = 2;
+               libusb_free_transfer(transfer);
+               irq_transfer = NULL;
                return;
        }
 
@@ -311,15 +311,17 @@ static void cb_irq(struct libusb_transfer *transfer)
                }
                break;
        }
-       if (submit_irq_transfer() < 0)
+       if (libusb_submit_transfer(irq_transfer) < 0)
                do_exit = 2;
 }
 
-static void cb_img(struct libusb_transfer *transfer)
+static void LIBUSB_CALL cb_img(struct libusb_transfer *transfer)
 {
        if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
                fprintf(stderr, "img transfer status %d?\n", transfer->status);
                do_exit = 2;
+               libusb_free_transfer(transfer);
+               img_transfer = NULL;
                return;
        }
 
@@ -329,31 +331,24 @@ static void cb_img(struct libusb_transfer *transfer)
                do_exit = 2;
                return;
        }
-       if (submit_img_transfer() < 0)
+       if (libusb_submit_transfer(img_transfer) < 0)
                do_exit = 2;
 }
 
-static int submit_irq_transfer(void)
-{
-       return libusb_submit_transfer(irq_transfer);
-}
-
-static int submit_img_transfer(void)
-{
-       return libusb_submit_transfer(img_transfer);
-}
-
 static int init_capture(void)
 {
        int r;
 
-       r = submit_irq_transfer();
+       r = libusb_submit_transfer(irq_transfer);
        if (r < 0)
                return r;
 
-       r = submit_img_transfer();
+       r = libusb_submit_transfer(img_transfer);
        if (r < 0) {
-               libusb_cancel_transfer_sync(img_transfer);
+               libusb_cancel_transfer(irq_transfer);
+               while (irq_transfer)
+                       if (libusb_handle_events(NULL) < 0)
+                               break;
                return r;
        }
 
@@ -398,11 +393,11 @@ static int do_init(void)
 
 static int alloc_transfers(void)
 {
-       img_transfer = libusb_alloc_transfer();
+       img_transfer = libusb_alloc_transfer(0);
        if (!img_transfer)
                return -ENOMEM;
-       
-       irq_transfer = libusb_alloc_transfer();
+
+       irq_transfer = libusb_alloc_transfer(0);
        if (!irq_transfer)
                return -ENOMEM;
 
@@ -416,15 +411,17 @@ static int alloc_transfers(void)
 
 static void sighandler(int signum)
 {
-       do_exit = 1;    
+       (void)signum;
+
+       do_exit = 1;
 }
 
 int main(void)
 {
        struct sigaction sigact;
-       int r = 1;
+       int r;
 
-       r = libusb_init();
+       r = libusb_init(NULL);
        if (r < 0) {
                fprintf(stderr, "failed to initialise libusb\n");
                exit(1);
@@ -438,7 +435,7 @@ int main(void)
 
        r = libusb_claim_interface(devh, 0);
        if (r < 0) {
-               fprintf(stderr, "usb_claim_interface error %d %s\n", r, strerror(-r));
+               fprintf(stderr, "usb_claim_interface error %d\n", r);
                goto out;
        }
        printf("claimed interface\n");
@@ -469,21 +466,29 @@ int main(void)
        sigaction(SIGQUIT, &sigact, NULL);
 
        while (!do_exit) {
-               r = libusb_poll();
+               r = libusb_handle_events(NULL);
                if (r < 0)
                        goto out_deinit;
        }
 
        printf("shutting down...\n");
 
-       r = libusb_cancel_transfer_sync(irq_transfer);
-       if (r < 0)
-               goto out_deinit;
+       if (irq_transfer) {
+               r = libusb_cancel_transfer(irq_transfer);
+               if (r < 0)
+                       goto out_deinit;
+       }
+
+       if (img_transfer) {
+               r = libusb_cancel_transfer(img_transfer);
+               if (r < 0)
+                       goto out_deinit;
+       }
+
+       while (irq_transfer || img_transfer)
+               if (libusb_handle_events(NULL) < 0)
+                       break;
 
-       r = libusb_cancel_transfer_sync(img_transfer);
-       if (r < 0)
-               goto out_deinit;
-       
        if (do_exit == 1)
                r = 0;
        else
@@ -498,7 +503,6 @@ out_release:
        libusb_release_interface(devh, 0);
 out:
        libusb_close(devh);
-       libusb_exit();
+       libusb_exit(NULL);
        return r >= 0 ? r : -r;
 }
-