]> git.cryptolib.org Git - labortage2013badge.git/commitdiff
removing color stuff; introducing debug stuff (cmd)
authorbg <daniel.otte@rub.de>
Wed, 16 Oct 2013 12:02:30 +0000 (14:02 +0200)
committerbg <daniel.otte@rub.de>
Wed, 16 Oct 2013 12:02:30 +0000 (14:02 +0200)
hostware/commandline/hexdump.c
hostware/commandline/main.c

index 6fb3742855cbabf8cb3850f0ee4f6c9b2be5c828..42ac6a0e4974b3c492dc0bce75dba1f5e4651c2b 100644 (file)
 #include <stdio.h>
 #include <string.h>
 #include <ctype.h>
-#include <stdint.h>
+#include <inttypes.h>
 
 static
 void int_hexdump_line_buffer(FILE* stream, uint8_t* buffer, unsigned width, unsigned fill){
        unsigned i;
        for(i=0; i<width; ++i){
                if(i<fill){
-                       fprintf(stream, "%2.2x ", buffer[i]);
+                       fprintf(stream, "%2.2"PRIx8" ", buffer[i]);
                }else{
                        fputs("   ", stream);
                }
@@ -54,7 +54,7 @@ void hexdump_block(FILE* stream, void* block, void* print_addr, unsigned length,
        while(length>width){
                memcpy(buffer, block, width);
                if(print_addr){
-                       fprintf(stream, "%8.8x ", (uint32_t)print_addr);
+                       fprintf(stream, "%p ", print_addr);
                        print_addr = (uint8_t*)print_addr + width;
                }
                fprintf(stream, "<%4.4x>: ", index);
@@ -66,7 +66,7 @@ void hexdump_block(FILE* stream, void* block, void* print_addr, unsigned length,
        }
        memcpy(buffer, block, length);
        if(print_addr){
-               fprintf(stream, "%8.8x ", (uint32_t)print_addr);
+               fprintf(stream, "%p ", print_addr);
        }
        fprintf(stream, "<%4.4x>: ", index);
        int_hexdump_line_buffer(stream, buffer, width, length);
index 71d9b076fefa12b9eb03e5706b1fc37439871ad1..ffffc606a87de162ec86737bc7031f631e7e53ac 100644 (file)
@@ -37,24 +37,51 @@ 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);
+int8_t hex_to_int(char c) {
+    int8_t r = -1;
+    if (c >= '0' && c <= '9') {
+        r = c - '0';
+    } else {
+        c |= 'a' ^ 'A';
+        if (c >= 'a' && c <= 'f') {
+            r = 10 + c - 'a';
+        }
+    }
+    return r;
 }
 
-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);
+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);
+}
+
+void set_dbg(char *hex_string){
+       uint8_t buffer[(strlen(hex_string) + 1) / 2];
+       size_t i = 0, length = 0;
+       int8_t t;
+
+       memset(buffer, 0, (strlen(hex_string) + 1) / 2);
+
+       while (hex_string[i]) {
+           t = hex_to_int(hex_string[i]);
+           if (t == -1){
+               break;
+           }
+           if (i & 1) {
+               buffer[length++] |= t;
+           } else {
+               buffer[length] |= t << 4;
+           }
        }
-       printf("red:   %5hu\ngreen: %5hu\nblue:  %5u\n", buffer[0], buffer[1], buffer[2]);
+
+       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){
+       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);
+       printf("DBG-Buffer:\n");
+       hexdump_block(stdout, buffer, 0, cnt, 16);
 }
 
 void read_mem(char* param){
@@ -323,25 +350,25 @@ int main(int argc, char **argv)
     }
 
     for(;;){
-       c = getopt_long(argc, argv, "s:gr:z:w:x:a:f:p::q::bk::tl:",
+       c = getopt_long(argc, argv, "s:gr:z:w:x:a:f:p::q::bk::tl:e",
                 long_options, &option_index);
        if(c == -1){
                break;
        }
 
-       if(action_fn && strchr("sgrzwxaqbkt", c)){
+       if(action_fn && strchr("sgrzwxaqbkte", c)){
                /* action given while already having an action */
                usage(argv[0]);
                exit(1);
        }
 
-       if(strchr("sgrzwxaqkt", c)){
+       if(strchr("sgrzwxaqkte", c)){
                main_arg = optarg;
        }
 
        switch(c){
-       case 's': action_fn = set_rgb; break;
-       case 'g': action_fn = get_rgb; break;
+       case 's': action_fn = set_dbg; break;
+       case 'g': action_fn = get_dbg; break;
        case 'r': action_fn = read_mem; break;
        case 'z': action_fn = read_flash; break;
        case 'w': action_fn = write_mem; break;
@@ -352,8 +379,9 @@ int main(int argc, char **argv)
        case 'f': fname = optarg; break;
        case 'p': pad = 0; if(optarg) pad=strtoul(optarg, NULL, 0); break;
                case 'l': exec_loops = strtoul(optarg, NULL, 0); break;
+        case 'e': action_fn = press_button; break;
         case 'x':
-       case 'a':
+        case 'a':
        default:
                break;
        }