-void read_flash(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_FLASH, (intptr_t)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 inc_counter(char *param){
+ usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, CUSTOM_RQ_INC_COUNTER, 0, 0, NULL, 0, 5000);
+}
+
+void get_counter(char *param){
+ uint32_t counter;
+ int cnt;
+ cnt = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, CUSTOM_RQ_GET_COUNTER, 0, 0, (char*)&counter, 4, 5000);
+ if (cnt == 4) {
+ printf("internal counter = %"PRId32"\n", counter);
+ } else {
+ fprintf(stderr, "Error: reading %d bytes for counter, expecting 4\n", cnt);
+ }
+}
+
+void reset_counter(char *param){
+ usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, CUSTOM_RQ_RESET_COUNTER, 0, 0, NULL, 0, 5000);
+}
+
+void get_reset_counter (char *param){
+ uint8_t counter;
+ int cnt;
+ cnt = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, CUSTOM_RQ_GET_RESET_COUNTER, 0, 0, (char*)&counter, 1, 5000);
+ if (cnt == 1) {
+ printf("internal reset counter = %"PRId8"\n", counter);
+ } else {
+ fprintf(stderr, "Error: reading %d bytes for reset counter, expecting 1\n", cnt);
+ }
+}
+
+void get_digits(char *param){
+ uint8_t digits;
+ int cnt;
+ cnt = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, CUSTOM_RQ_GET_DIGITS, 0, 0, (char*)&digits, 1, 5000);
+ if (cnt == 1) {
+ printf("digits = %"PRId8"\n", digits);
+ } else {
+ fprintf(stderr, "Error: reading %d bytes for reset counter, expecting 1\n", cnt);
+ }
+}
+
+void set_digits(char *param){
+ long d;
+ d = strtol(param, NULL, 10);
+ if (d > 9 || d < 6) {
+ fprintf(stderr, "Error: <digits> 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);
+ }