X-Git-Url: https://git.cryptolib.org/?p=labortage2013badge.git;a=blobdiff_plain;f=hostware%2Fcommandline%2Fmain.c;h=202dbeea6e92390411421fade9c54251631a3281;hp=ffffc606a87de162ec86737bc7031f631e7e53ac;hb=48646597db7ce609b6a3e0ba3d44a4723587cc0c;hpb=ffbec2d5023ce1b2e8816249ff2ae364028b55bd diff --git a/hostware/commandline/main.c b/hostware/commandline/main.c index ffffc60..202dbee 100644 --- a/hostware/commandline/main.c +++ b/hostware/commandline/main.c @@ -21,6 +21,7 @@ respectively. #include #include #include +#include #include #include #include /* this is libusb */ @@ -50,7 +51,7 @@ int8_t hex_to_int(char c) { return r; } -void press_button(char* param){ +void press_button(char *param){ usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_OUT, CUSTOM_RQ_PRESS_BUTTON, 0, 0, NULL, 0, 5000); } @@ -76,7 +77,7 @@ void set_dbg(char *hex_string){ usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_OUT, CUSTOM_RQ_SET_DBG, 0, 0, (char*)buffer, length, 5000); } -void get_dbg(char* param){ +void get_dbg(char *param){ uint16_t buffer[256]; int cnt; cnt = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, CUSTOM_RQ_GET_DBG, 0, 0, (char*)buffer, 256, 5000); @@ -84,157 +85,99 @@ void get_dbg(char* param){ hexdump_block(stdout, buffer, 0, cnt, 16); } -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 clr_dbg(char *param){ + usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, CUSTOM_RQ_CLR_DBG, 0, 0, NULL, 0, 5000); } -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 9 || d < 6) { + fprintf(stderr, "Error: must be in range 6, 7, 8 or 9\n"); + return; + } + usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, CUSTOM_RQ_SET_DIGITS, d, 0, NULL, 0, 5000); +} + +void get_token(char *param){ + char token[10]; + int cnt; + cnt = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, CUSTOM_RQ_GET_TOKEN, 0, 0, token, 9, 5000); + if (cnt < 9 ) { + token[cnt] = '\0'; + printf("token = %s\n", token); + } else { + fprintf(stderr, "Error: reading %d bytes for token, expecting max. 9\n", cnt); + } } void soft_reset(char* param){ - unsigned delay=0; - if(param){ + unsigned delay = 0; + if (param) { sscanf(param, "%i", &delay); } delay &= 0xf; @@ -242,53 +185,39 @@ void soft_reset(char* param){ } void read_button(char* param){ - uint8_t v; + int8_t v; usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, CUSTOM_RQ_READ_BUTTON, 0, 0, (char*)&v, 1, 5000); - printf("button is %s\n",v?"on":"off"); + printf("button is %s (%"PRId8")\n", v ? "on" : "off", v); } -void wait_for_button(char* param){ - volatile uint8_t v=0, x=1; - if(param){ - printf("DBG: having param: %s\n", param); - if(!(strcmp(param,"off") && strcmp(param,"0"))){ - x = 0; - } - } - do{ - usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, CUSTOM_RQ_READ_BUTTON, 0, 0, (char*)&v, 1, 5000); - }while(x!=v); - printf("button is %s\n",v?"on":"off"); -} - -void read_temperature(char* param){ - uint16_t v; - int cnt; - cnt = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, CUSTOM_RQ_READ_TMPSENS, 0, 0, (char*)&v, 2, 5000); - printf("temperature raw value: %hd 0x%hx\n", v, v); +void get_secret(char *param){ + uint16_t buffer[256]; + int cnt; + cnt = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, CUSTOM_RQ_GET_SECRET, 0, 0, (char*)buffer, 256, 5000); + printf("Secret:\n"); + hexdump_block(stdout, buffer, 0, cnt, 16); } static struct option long_options[] = { - /* These options set a flag. */ - {"i-am-sure", no_argument, &safety_question_override, 1}, - {"pad", optional_argument, 0, 'p'}, /* These options don't set a flag. We distinguish them by their indices. */ - {"set-rgb", required_argument, 0, 's'}, - {"get-rgb", no_argument, 0, 'g'}, - {"read-mem", required_argument, 0, 'r'}, - {"write-mem", required_argument, 0, 'w'}, - {"read-flash", required_argument, 0, 'z'}, - {"exec-spm", required_argument, 0, 'x'}, - {"read-adc", required_argument, 0, 'a'}, - {"reset", optional_argument, 0, 'q'}, - {"read-button", required_argument, 0, 'b'}, - {"wait-for-button", optional_argument, 0, 'k'}, - {"read-temperature", no_argument, 0, 't'}, - {"file", required_argument, 0, 'f'}, - {"loop", required_argument, 0, 'l'}, + {"set-secret", required_argument, NULL, 's'}, + {"inc-counter", no_argument, NULL, 'i'}, + {"get-counter", no_argument, NULL, 'c'}, + {"reset-counter", no_argument, NULL, 'r'}, + {"get-reset-counter", no_argument, NULL, 'q'}, + {"get-digits", no_argument, NULL, 'D'}, + {"set-digits", required_argument, NULL, 'd'}, + {"reset", optional_argument, NULL, 'R'}, + {"get-token", no_argument, NULL, 't'}, + {"read-button", no_argument, NULL, 'b'}, + {"press-button", no_argument, NULL, 'B'}, + {"get-dbg", no_argument, NULL, 'x'}, + {"set-dbg", required_argument, NULL, 'y'}, + {"clr-dbg", no_argument, NULL, 'z'}, + {"get-secret", no_argument, NULL, 'S'}, {0, 0, 0, 0} }; @@ -296,29 +225,28 @@ static void usage(char *name) { char *usage_str = "usage:\n" - " %s [