From 95ca3f5bd696b5450820929e1b1a444d02f0bd1e Mon Sep 17 00:00:00 2001 From: Christian Starkjohann Date: Thu, 17 Apr 2008 19:00:20 +0000 Subject: - imported new files into project --- examples/hid-data/commandline/Makefile | 42 ++++++++ examples/hid-data/commandline/Makefile.windows | 18 ++++ examples/hid-data/commandline/hidtool.c | 127 +++++++++++++++++++++++++ 3 files changed, 187 insertions(+) create mode 100644 examples/hid-data/commandline/Makefile create mode 100644 examples/hid-data/commandline/Makefile.windows create mode 100644 examples/hid-data/commandline/hidtool.c (limited to 'examples/hid-data/commandline') diff --git a/examples/hid-data/commandline/Makefile b/examples/hid-data/commandline/Makefile new file mode 100644 index 0000000..97c6b06 --- /dev/null +++ b/examples/hid-data/commandline/Makefile @@ -0,0 +1,42 @@ +# Name: Makefile +# Project: hid-data example +# Author: Christian Starkjohann +# Creation Date: 2008-04-11 +# Tabsize: 4 +# Copyright: (c) 2008 by OBJECTIVE DEVELOPMENT Software GmbH +# License: GNU GPL v2 (see License.txt) or proprietary (CommercialLicense.txt) +# This Revision: $Id$ + +# Please read the definitions below and edit them as appropriate for your +# system: + +# Use the following 3 lines on Unix and Mac OS X: +USBFLAGS= `libusb-config --cflags` +USBLIBS= `libusb-config --libs` +EXE_SUFFIX= + +# Use the following 3 lines on Windows and comment out the 3 above: +#USBFLAGS= +#USBLIBS= -lhid -lusb -lsetupapi +#EXE_SUFFIX= .exe + +CC= gcc +CFLAGS= -O -Wall $(USBFLAGS) +LIBS= $(USBLIBS) + +OBJ= hidtool.o hiddata.o +PROGRAM= hidtool$(EXE_SUFFIX) + +all: $(PROGRAM) + +$(PROGRAM): $(OBJ) + $(CC) -o $(PROGRAM) $(OBJ) $(LIBS) + +strip: $(PROGRAM) + strip $(PROGRAM) + +clean: + rm -f $(OBJ) $(PROGRAM) + +.c.o: + $(CC) $(ARCH_COMPILE) $(CFLAGS) -c $*.c -o $*.o diff --git a/examples/hid-data/commandline/Makefile.windows b/examples/hid-data/commandline/Makefile.windows new file mode 100644 index 0000000..f0e6cfa --- /dev/null +++ b/examples/hid-data/commandline/Makefile.windows @@ -0,0 +1,18 @@ +# Name: Makefile.windows +# Project: hid-data example +# Author: Christian Starkjohann +# Creation Date: 2008-04-11 +# Tabsize: 4 +# Copyright: (c) 2008 by OBJECTIVE DEVELOPMENT Software GmbH +# License: GNU GPL v2 (see License.txt) or proprietary (CommercialLicense.txt) +# This Revision: $Id$ + +# You may use this file with +# make -f Makefile.windows +# on Windows with MinGW instead of editing the main Makefile. + +include Makefile + +USBFLAGS= +USBLIBS= -lhid -lusb -lsetupapi +EXE_SUFFIX= .exe diff --git a/examples/hid-data/commandline/hidtool.c b/examples/hid-data/commandline/hidtool.c new file mode 100644 index 0000000..d03bca8 --- /dev/null +++ b/examples/hid-data/commandline/hidtool.c @@ -0,0 +1,127 @@ +/* Name: hidtool.c + * Project: hid-data example + * Author: Christian Starkjohann + * Creation Date: 2008-04-11 + * Tabsize: 4 + * Copyright: (c) 2008 by OBJECTIVE DEVELOPMENT Software GmbH + * License: GNU GPL v2 (see License.txt) or proprietary (CommercialLicense.txt) + * This Revision: $Id$ + */ + +#include +#include +#include +#include "hiddata.h" +#include "../firmware/usbconfig.h" /* for device VID, PID, vendor name and product name */ + +/* ------------------------------------------------------------------------- */ + +static char *usbErrorMessage(int errCode) +{ +static char buffer[80]; + + switch(errCode){ + case USBOPEN_ERR_ACCESS: return "Access to device denied"; + case USBOPEN_ERR_NOTFOUND: return "The specified device was not found"; + case USBOPEN_ERR_IO: return "Communication error with device"; + default: + sprintf(buffer, "Unknown USB error %d", errCode); + return buffer; + } + return NULL; /* not reached */ +} + +static usbDevice_t *openDevice(void) +{ +usbDevice_t *dev = NULL; +unsigned char rawVid[2] = {USB_CFG_VENDOR_ID}, rawPid[2] = {USB_CFG_DEVICE_ID}; +char vendorName[] = {USB_CFG_VENDOR_NAME, 0}, productName[] = {USB_CFG_DEVICE_NAME, 0}; +int vid = rawVid[0] + 256 * rawVid[1]; +int pid = rawPid[0] + 256 * rawPid[1]; +int err; + + if((err = usbhidOpenDevice(&dev, vid, vendorName, pid, productName, 0)) != 0){ + fprintf(stderr, "error finding %s: %s\n", productName, usbErrorMessage(err)); + return NULL; + } + return dev; +} + +/* ------------------------------------------------------------------------- */ + +static void hexdump(char *buffer, int len) +{ +int i; +FILE *fp = stdout; + + for(i = 0; i < len; i++){ + if(i != 0){ + if(i % 16 == 0){ + fprintf(fp, "\n"); + }else{ + fprintf(fp, " "); + } + } + fprintf(fp, "0x%02x", buffer[i] & 0xff); + } + if(i != 0) + fprintf(fp, "\n"); +} + +static int hexread(char *buffer, char *string, int buflen) +{ +char *s; +int pos = 0; + + while((s = strtok(string, ", ")) != NULL && pos < buflen){ + string = NULL; + buffer[pos++] = (char)strtol(s, NULL, 0); + } + return pos; +} + +/* ------------------------------------------------------------------------- */ + +static void usage(char *myName) +{ + fprintf(stderr, "usage:\n"); + fprintf(stderr, " %s read\n", myName); + fprintf(stderr, " %s write \n", myName); +} + +int main(int argc, char **argv) +{ +usbDevice_t *dev; +char buffer[129]; /* room for dummy report ID */ +int err; + + if(argc < 2){ + usage(argv[0]); + exit(1); + } + if((dev = openDevice()) == NULL) + exit(1); + if(strcasecmp(argv[1], "read") == 0){ + int len = sizeof(buffer); + if((err = usbhidGetReport(dev, 0, buffer, &len)) != 0){ + fprintf(stderr, "error reading data: %s\n", usbErrorMessage(err)); + }else{ + hexdump(buffer + 1, sizeof(buffer) - 1); + } + }else if(strcasecmp(argv[1], "write") == 0){ + int i, pos; + bzero(buffer, sizeof(buffer)); + for(pos = 1, i = 2; i < argc && pos < sizeof(buffer); i++){ + pos += hexread(buffer + pos, argv[i], sizeof(buffer) - pos); + } + if((err = usbhidSetReport(dev, buffer, sizeof(buffer))) != 0) /* add a dummy report ID */ + fprintf(stderr, "error writing data: %s\n", usbErrorMessage(err)); + }else{ + usage(argv[0]); + exit(1); + } + usbhidCloseDevice(dev); + return 0; +} + +/* ------------------------------------------------------------------------- */ -- cgit v1.2.3