proxmark3/client/proxusb.c
izsh.f0f 7fe9b0b742 Client cleanup and restructuring. Stage 1...
Next Step is refactoring some of the giant functions which are
just copy/paste of some other ones with just a few line changes,
removing unnecessary 'goto' etc.

The MS Windows version is broken with this commit but will be fixed
soon. Everything can't be done all at once :P

The commands are now hierarchical, for example:
"hf 14a read" vs. "hf 14b read".
You can also request help:
"hf help", "data help", "hf 15 help" etc.

Indents are now space-based, not tab-based anymore. Hopefully
no one will be trolling about it, considering the suicide-prone work
being done here ;)

client/cmdhw.c, client/proxusb.c, client/cmdhw.h, client/proxusb.h,
client/cmdmain.c, client/cmdlfhid.c, client/cmdmain.h, client/cmdlfhid.h,
client/data.c, client/data.h, client/cmdhf.c, client/cmdlf.c,
client/cmdhf.h, client/cmdhf15.c, client/cmdhf14b.c, client/cmdlf.h,
client/cmdhf15.h, client/cmdhf14b.h, client/cmddata.c, client/cmddata.h,
client/ui.c, client/cmdparser.c, client/cmdlfti.c, client/ui.h,
client/cmdlfem4x.c, client/cmdparser.h, client/cmdlfti.h, client/cmdlfem4x.h,
client/graph.c, client/graph.h, client/cmdhf14a.c, client/cmdhf14a.h,
client/cmdhflegic.c, client/cmdhflegic.c: New files.

client/cli.c, client/flasher.c, client/snooper.c, client/proxmark3.c,
client/proxmark3.h, client/Makefile: Update accordingly.

client/flash.h, client/flash.c, client/proxgui.cpp: Cosmetic changes.

client/translate.h, client/command.c, client/gui.c,
client/usb.c, client/prox.h: Remove.

include/usb_cmd.h (CMD_ACQUIRE_RAW_ADC_SAMPLES_ISO_14443_SIM): Remove dead cmd.

common/crc16.h: New file.
common/crc16.c: Modify accordingly.
common/iso14443crc.h: New file.
common/iso14443_crc.c: Rename to
common/iso14443crc.c: and modify accordingly.

armsrc/lfops.c, armsrc/iso14443.c,
armsrc/iso14443a.c: include .h files from
the common directory instead of including the c files.

common/Makefile.common, armsrc/Makefile: Modify accordingly.
2010-02-04 01:27:07 +00:00

169 lines
3.6 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <usb.h>
#include <strings.h>
#include <errno.h>
#include "proxusb.h"
#include "proxmark3.h"
#include "usb_cmd.h"
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)
{
int ret;
#if 0
printf("Sending %d bytes\n", sizeof(UsbCommand));
#endif
current_command = c->cmd;
ret = usb_bulk_write(devh, 0x01, (char*)c, sizeof(UsbCommand), 1000);
if (ret<0) {
error_occured = 1;
if (return_on_error)
return;
fprintf(stderr, "write failed: %s!\nTrying to reopen device...\n",
usb_strerror());
if (devh) {
usb_close(devh);
devh = NULL;
}
while(!(devh=OpenProxmark(0))) { sleep(1); }
printf(PROXPROMPT);
fflush(NULL);
return;
}
}
bool ReceiveCommandPoll(UsbCommand *c)
{
int ret;
bzero(c, sizeof(UsbCommand));
ret = usb_bulk_read(devh, 0x82, (char*)c, sizeof(UsbCommand), 500);
if (ret<0) {
if (ret != -ETIMEDOUT) {
error_occured = 1;
if (return_on_error)
return false;
fprintf(stderr, "read failed: %s(%d)!\nTrying to reopen device...\n",
usb_strerror(), ret);
if (devh) {
usb_close(devh);
devh = NULL;
}
while(!(devh=OpenProxmark(0))) { sleep(1); }
printf(PROXPROMPT);
fflush(NULL);
return false;
}
} else {
if (ret && (ret < sizeof(UsbCommand))) {
fprintf(stderr, "Read only %d instead of requested %d bytes!\n",
ret, (int)sizeof(UsbCommand));
}
}
return ret > 0;
}
void ReceiveCommand(UsbCommand *c)
{
// printf("%s()\n", __FUNCTION__);
int retval = 0;
do {
retval = ReceiveCommandPoll(c);
if (retval != 1) printf("ReceiveCommandPoll returned %d\n", retval);
} while(retval<0);
// printf("recv %x\n", c->cmd);
}
usb_dev_handle* findProxmark(int verbose, unsigned int *iface)
{
struct usb_bus *busses, *bus;
usb_dev_handle *handle = NULL;
usb_find_busses();
usb_find_devices();
busses = usb_get_busses();
for (bus = busses; bus; bus = bus->next) {
struct usb_device *dev;
for (dev = bus->devices; dev; dev = dev->next) {
struct usb_device_descriptor *desc = &(dev->descriptor);
if ((desc->idProduct == 0x4b8f) && (desc->idVendor == 0x9ac4)) {
handle = usb_open(dev);
if (!handle) {
if (verbose)
fprintf(stderr, "open failed: %s!\n", usb_strerror());
return NULL;
}
*iface = dev->config[0].interface[0].altsetting[0].bInterfaceNumber;
return handle;
}
}
}
return NULL;
}
usb_dev_handle* OpenProxmark(int verbose)
{
int ret;
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__
/* 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
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;
}
void CloseProxmark(void)
{
usb_release_interface(devh, claimed_iface);
usb_close(devh);
}