]> git.zerfleddert.de Git - proxmark3-svn/blobdiff - client/proxusb.c
fixed USB GPIO bug reported by gregy, and fixed 'hf 14a reader' command
[proxmark3-svn] / client / proxusb.c
index be37fc69cca16719186e21be93c980d81abae62a..2f152ace25dfe5f2f49e48fee3e11ed4b609c3e4 100644 (file)
@@ -1,30 +1,48 @@
+//-----------------------------------------------------------------------------
+// Copyright (C) 2009 Michael Gernoth <michael at gernoth.net>
+// Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
+//
+// This code is licensed to you under the terms of the GNU GPL, version 2 or,
+// at your option, any later version. See the LICENSE.txt file for the text of
+// the license.
+//-----------------------------------------------------------------------------
+// USB utilities
+//-----------------------------------------------------------------------------
+
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <stdbool.h>
 #include <unistd.h>
 #include <usb.h>
 #include <strings.h>
 #include <errno.h>
 
+#include "sleep.h"
 #include "proxusb.h"
 #include "proxmark3.h"
 #include "usb_cmd.h"
 
+// It seems to be missing for mingw
+#ifndef ETIMEDOUT
+#define ETIMEDOUT 116
+#endif
+
 usb_dev_handle *devh = NULL;
 static unsigned int claimed_iface = 0;
 unsigned char return_on_error = 0;
 unsigned char error_occured = 0;
 extern unsigned int current_command;
 
-void SendCommand(UsbCommand *c)
+void SendCommand_(HidCommand *c)
 {
   int ret;
 
 #if 0
-  printf("Sending %d bytes\n", sizeof(UsbCommand));
+  printf("Sending %d bytes\n", sizeof(HidCommand));
 #endif
   current_command = c->cmd;
-  ret = usb_bulk_write(devh, 0x01, (char*)c, sizeof(UsbCommand), 1000);
+  ret = usb_bulk_write(devh, 0x01, (char*)c, sizeof(HidCommand), 1000);
   if (ret<0) {
     error_occured = 1;
     if (return_on_error)
@@ -37,7 +55,7 @@ void SendCommand(UsbCommand *c)
       usb_close(devh);
       devh = NULL;
     }
-    while(!(devh=OpenProxmark(0))) { sleep(1); }
+    while(!OpenProxmark(0)) { sleep(1); }
     printf(PROXPROMPT);
     fflush(NULL);
 
@@ -45,12 +63,12 @@ void SendCommand(UsbCommand *c)
   }
 }
 
-bool ReceiveCommandPoll(UsbCommand *c)
+bool ReceiveCommandPoll(HidCommand *c)
 {
   int ret;
 
-  bzero(c, sizeof(UsbCommand));
-  ret = usb_bulk_read(devh, 0x82, (char*)c, sizeof(UsbCommand), 500);
+  memset(c, 0, sizeof (HidCommand));
+  ret = usb_bulk_read(devh, 0x82, (char*)c, sizeof(HidCommand), 500);
   if (ret<0) {
     if (ret != -ETIMEDOUT) {
       error_occured = 1;
@@ -64,23 +82,23 @@ bool ReceiveCommandPoll(UsbCommand *c)
         usb_close(devh);
         devh = NULL;
       }
-      while(!(devh=OpenProxmark(0))) { sleep(1); }
+      while(!OpenProxmark(0)) { sleep(1); }
       printf(PROXPROMPT);
       fflush(NULL);
 
       return false;
     }
   } else {
-    if (ret && (ret < sizeof(UsbCommand))) {
+    if (ret && (ret < sizeof(HidCommand))) {
       fprintf(stderr, "Read only %d instead of requested %d bytes!\n",
-        ret, (int)sizeof(UsbCommand));
+        ret, (int)sizeof(HidCommand));
     }
   }
 
   return ret > 0;
 }
 
-void ReceiveCommand(UsbCommand *c)
+void ReceiveCommand(HidCommand *c)
 {
 //  printf("%s()\n", __FUNCTION__);
   int retval = 0;
@@ -95,6 +113,8 @@ usb_dev_handle* findProxmark(int verbose, unsigned int *iface)
 {
   struct usb_bus *busses, *bus;
   usb_dev_handle *handle = NULL;
+  struct prox_unit units[50];
+  int iUnit = 0;
 
   usb_find_busses();
   usb_find_devices();
@@ -111,14 +131,47 @@ usb_dev_handle* findProxmark(int verbose, unsigned int *iface)
         handle = usb_open(dev);
         if (!handle) {
           if (verbose)
-            fprintf(stderr, "open failed: %s!\n", usb_strerror());
-          return NULL;
+            fprintf(stderr, "open fabiled: %s!\n", usb_strerror());
+          //return NULL;
+          continue;
         }
         *iface = dev->config[0].interface[0].altsetting[0].bInterfaceNumber;
-        return handle;
+
+        struct prox_unit unit = {handle, {0}};
+        usb_get_string_simple(handle, desc->iSerialNumber, unit.serial_number, sizeof(unit.serial_number));
+        units[iUnit++] = unit;
+
+        //return handle;
       }
+    }
+  }
 
+  if (iUnit > 0) {
+    int iSelection = 0;
+
+    fprintf(stdout, "\nConnected units:\n");
+
+    for (int i = 0; i < iUnit; i++) {
+      struct usb_device * dev = usb_device(units[i].handle);
+      fprintf(stdout, "\t%d. SN: %s [%s/%s]\n", i+1, units[i].serial_number, dev->bus->dirname, dev->filename);
+    }
+    if (iUnit > 1) {
+      while (iSelection < 1 || iSelection > iUnit) {
+        fprintf(stdout, "Which unit do you want to connect to? ");
+        fscanf(stdin, "%d", &iSelection);
+        }
+      }
+    else
+      iSelection = 1;
+    iSelection --;
+
+    for (int i = 0; i < iUnit; i++) {
+      if (iSelection == i) continue;
+      usb_close(units[i].handle);
+      units[i].handle = NULL;
     }
+
+    return units[iSelection].handle;
   }
 
   return NULL;
@@ -130,33 +183,32 @@ usb_dev_handle* OpenProxmark(int verbose)
   usb_dev_handle *handle = NULL;
   unsigned int iface;
 
-#ifndef __APPLE__
   handle = findProxmark(verbose, &iface);
   if (!handle)
     return NULL;
 
-  /* Whatever... */
-  usb_reset(handle);
-#endif
-
-  handle = findProxmark(verbose, &iface);
-  if (!handle)
-    return NULL;
-
-#ifndef __APPLE__
+#ifdef __linux__
   /* detach kernel driver first */
   ret = usb_detach_kernel_driver_np(handle, iface);
   /* don't complain if no driver attached */
   if (ret<0 && ret != -61 && verbose)
     fprintf(stderr, "detach kernel driver failed: (%d) %s!\n", ret, usb_strerror());
 #endif
+
+  // Needed for Windows. Optional for Mac OS and Linux
+  ret = usb_set_configuration(handle, 1);
+  if (ret < 0) {
+    if (verbose)
+      fprintf(stderr, "configuration set failed: %s!\n", usb_strerror());
+    return NULL;
+  }
+
   ret = usb_claim_interface(handle, iface);
   if (ret < 0) {
     if (verbose)
       fprintf(stderr, "claim failed: %s!\n", usb_strerror());
     return NULL;
   }
-
   claimed_iface = iface;
   devh = handle;
   return handle;
@@ -166,4 +218,5 @@ void CloseProxmark(void)
 {
   usb_release_interface(devh, claimed_iface);
   usb_close(devh);
+  devh = NULL;
 }
Impressum, Datenschutz