X-Git-Url: https://git.cryptolib.org/?p=labortage2013badge.git;a=blobdiff_plain;f=hostware%2Fcommandline%2Fmain.c;fp=hostware%2Fcommandline%2Fmain.c;h=71d9b076fefa12b9eb03e5706b1fc37439871ad1;hp=0000000000000000000000000000000000000000;hb=14a4df0d4392a668439c879c89e135d5ac345688;hpb=2da43c43d4818a4f2e3af7e8ec138c1f98019d9e diff --git a/hostware/commandline/main.c b/hostware/commandline/main.c new file mode 100644 index 0000000..71d9b07 --- /dev/null +++ b/hostware/commandline/main.c @@ -0,0 +1,377 @@ +/* Name: set-led.c + * Project: hid-custom-rq example + * Author: Christian Starkjohann + * Creation Date: 2008-04-10 + * Tabsize: 4 + * Copyright: (c) 2008 by OBJECTIVE DEVELOPMENT Software GmbH + * License: GNU GPL v2 (see License.txt), GNU GPL v3 or proprietary (CommercialLicense.txt) + * This Revision: $Id: set-led.c 692 2008-11-07 15:07:40Z cs $ + */ + +/* +General Description: +This is the host-side driver for the custom-class example device. It searches +the USB for the LEDControl device and sends the requests understood by this +device. +This program must be linked with libusb on Unix and libusb-win32 on Windows. +See http://libusb.sourceforge.net/ or http://libusb-win32.sourceforge.net/ +respectively. +*/ + +#include +#include +#include +#include +#include +#include /* this is libusb */ +#include +#include "hexdump.h" +#include "opendevice.h" /* common code moved to separate module */ + +#include "../../firmware/requests.h" /* custom request numbers */ +#include "../../firmware/usbconfig.h" /* device's VID/PID and names */ + +int safety_question_override=0; +int pad=-1; + +char* fname; +usb_dev_handle *handle = NULL; + +void set_rgb(char* color){ + uint16_t buffer[3] = {0, 0, 0}; + sscanf(color, "%hi:%hi:%hi", &(buffer[0]), &(buffer[1]), &(buffer[2])); + buffer[0] &= (1<<10)-1; + buffer[1] &= (1<<10)-1; + buffer[2] &= (1<<10)-1; + usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_OUT, CUSTOM_RQ_SET_RGB, 0, 0, (char*)buffer, 6, 5000); +} + +void get_rgb(char* param){ + uint16_t buffer[3]; + int cnt; + cnt = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, CUSTOM_RQ_GET_RGB, 0, 0, (char*)buffer, 6, 5000); + if(cnt!=6){ + fprintf(stderr, "ERROR: received %d bytes from device while expecting %d bytes\n", cnt, 6); + exit(1); + } + printf("red: %5hu\ngreen: %5hu\nblue: %5u\n", buffer[0], buffer[1], buffer[2]); +} + +void read_mem(char* param){ + int length=0; + uint8_t *buffer, *addr; + int cnt; + FILE* f=NULL; + if(fname){ + f = fopen(fname, "wb"); + if(!f){ + fprintf(stderr, "ERROR: could not open %s for writing\n", fname); + exit(1); + } + } + sscanf(param, "%i:%i", (int*)&addr, &length); + if(length<=0){ + return; + } + buffer = malloc(length); + if(!buffer){ + if(f) + fclose(f); + fprintf(stderr, "ERROR: out of memory\n"); + exit(1); + } + cnt = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, CUSTOM_RQ_READ_MEM, (int)((unsigned)addr), 0, (char*)buffer, length, 5000); + if(cnt!=length){ + if(f) + fclose(f); + fprintf(stderr, "ERROR: received %d bytes from device while expecting %d bytes\n", cnt, length); + exit(1); + } + if(f){ + cnt = fwrite(buffer, 1, length, f); + fclose(f); + if(cnt!=length){ + fprintf(stderr, "ERROR: could write only %d bytes out of %d bytes\n", cnt, length); + exit(1); + } + + }else{ + hexdump_block(stdout, buffer, addr, length, 8); + } +} + +void write_mem(char* param){ + int length; + uint8_t *addr, *buffer, *data=NULL; + int cnt=0; + FILE* f=NULL; + + if(fname){ + f = fopen(fname, "rb"); + if(!f){ + fprintf(stderr, "ERROR: could not open %s for writing\n", fname); + exit(1); + } + } + sscanf(param, "%i:%i:%n", (int*)&addr, &length, &cnt); + data += cnt; + if(length<=0){ + return; + } + buffer = malloc(length); + if(!buffer){ + if(f) + fclose(f); + fprintf(stderr, "ERROR: out of memory\n"); + exit(1); + } + memset(buffer, (uint8_t)pad, length); + if(!data && !f && length==0){ + fprintf(stderr, "ERROR: no data to write\n"); + exit(1); + } + if(f){ + cnt = fread(buffer, 1, length, f); + fclose(f); + if(cnt!=length && pad==-1){ + fprintf(stderr, "Warning: could ony read %d bytes from file; will only write these bytes", cnt); + } + }else if(data){ + char xbuffer[3]= {0, 0, 0}; + uint8_t fill=0; + unsigned idx=0; + while(*data && idx] \n" + " where