]> git.cryptolib.org Git - labortage2013badge.git/blobdiff - firmware/main.c
removing unneeded character entering capability
[labortage2013badge.git] / firmware / main.c
index cdbc69dba5384106ce5d51f87c62db7ea23da595..75f5e463b1bab740845d80fb7d0cf0167366e713 100644 (file)
@@ -16,13 +16,10 @@ at least be connected to INT0 as well.
 We assume that an LED is connected to port B bit 0. If you connect it to a
 different port or bit, change the macros below:
 */
-#define LED_PORT_DDR        DDRB
-#define LED_PORT_OUTPUT     PORTB
-#define R_BIT            4
-#define G_BIT            3
-#define B_BIT            1
 #define BUTTON_PIN 4
 
+#define SIMPLE_COUNTER 1
+
 #include <stdint.h>
 #include <string.h>
 #include <stdbool.h>
@@ -37,13 +34,21 @@ different port or bit, change the macros below:
 #include "usbdrv.h"
 #include "oddebug.h"        /* This is also an example for using debug macros */
 #include "requests.h"       /* The custom request numbers we use */
-#include "special_functions.h"
-
-void update_pwm(void);
+#include "hotp.h"
+#if !SIMPLE_COUNTER
+#include "percnt2.h"
+#endif
+#include "usb_keyboard_codes.h"
 
 /* ------------------------------------------------------------------------- */
 /* ----------------------------- USB interface ----------------------------- */
 /* ------------------------------------------------------------------------- */
+
+#define STATE_WAIT 0
+#define STATE_SEND_KEY 1
+#define STATE_RELEASE_KEY 2
+#define STATE_NEXT 3
+
 PROGMEM const char usbHidReportDescriptor[USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH] = {
     0x05, 0x01,                    // USAGE_PAGE (Generic Desktop)
     0x09, 0x06,                    // USAGE (Keyboard)
@@ -79,84 +84,23 @@ PROGMEM const char usbHidReportDescriptor[USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH]
     0xc0                           // END_COLLECTION
 };
 
-/* Keyboard usage values, see usb.org's HID-usage-tables document, chapter
- * 10 Keyboard/Keypad Page for more codes.
- */
-#define MOD_CONTROL_LEFT    (1<<0)
-#define MOD_SHIFT_LEFT      (1<<1)
-#define MOD_ALT_LEFT        (1<<2)
-#define MOD_GUI_LEFT        (1<<3)
-#define MOD_CONTROL_RIGHT   (1<<4)
-#define MOD_SHIFT_RIGHT     (1<<5)
-#define MOD_ALT_RIGHT       (1<<6)
-#define MOD_GUI_RIGHT       (1<<7)
-
-#define KEY_A       4
-#define KEY_B       5
-#define KEY_C       6
-#define KEY_D       7
-#define KEY_E       8
-#define KEY_F       9
-#define KEY_G       10
-#define KEY_H       11
-#define KEY_I       12
-#define KEY_J       13
-#define KEY_K       14
-#define KEY_L       15
-#define KEY_M       16
-#define KEY_N       17
-#define KEY_O       18
-#define KEY_P       19
-#define KEY_Q       20
-#define KEY_R       21
-#define KEY_S       22
-#define KEY_T       23
-#define KEY_U       24
-#define KEY_V       25
-#define KEY_W       26
-#define KEY_X       27
-#define KEY_Y       28
-#define KEY_Z       29
-#define KEY_1       30
-#define KEY_2       31
-#define KEY_3       32
-#define KEY_4       33
-#define KEY_5       34
-#define KEY_6       35
-#define KEY_7       36
-#define KEY_8       37
-#define KEY_9       38
-#define KEY_0       39
-
-#define KEY_F1      58
-#define KEY_F2      59
-#define KEY_F3      60
-#define KEY_F4      61
-#define KEY_F5      62
-#define KEY_F6      63
-#define KEY_F7      64
-#define KEY_F8      65
-#define KEY_F9      66
-#define KEY_F10     67
-#define KEY_F11     68
-#define KEY_F12     69
-
-#define NUM_LOCK 1
-#define CAPS_LOCK 2
-#define SCROLL_LOCK 4
-
-union {
-       struct {
-               uint16_t red;
-               uint16_t green;
-               uint16_t blue;
-       } name;
-       uint16_t idx[3];
-} color;
+static uint16_t secret_length_ee EEMEM = 0;
+static uint8_t  secret_ee[32] EEMEM;
+static uint8_t  reset_counter_ee EEMEM = 0;
+static uint8_t  digits_ee EEMEM = 8;
+
+#if SIMPLE_COUNTER
+static uint32_t counter_ee EEMEM = 0;
+#endif
+
+static uint8_t dbg_buffer[8];
+static uint8_t secret[32];
+static uint16_t secret_length_b;
+static char token[10];
 
 #define UNI_BUFFER_SIZE 16
 
-static union {
+static union __attribute__((packed)) {
        uint8_t  w8[UNI_BUFFER_SIZE];
        uint16_t w16[UNI_BUFFER_SIZE/2];
        uint32_t w32[UNI_BUFFER_SIZE/4];
@@ -172,82 +116,147 @@ typedef struct {
     uint8_t keycode[6];
 } keyboard_report_t;
 
-#define STATE_WAIT 0
-#define STATE_SEND_KEY 1
-#define STATE_RELEASE_KEY 2
-
-
-static keyboard_report_t keyboard_report; // sent to PC
-static uchar idleRate;           /* in 4 ms units */
+static keyboard_report_t keyboard_report; /* report sent to the host */
+static uchar idleRate;  /* in 4 ms units */
 static uchar key_state = STATE_WAIT;
-volatile static uchar LED_state = 0xff; // received from PC
+volatile static uchar LED_state = 0xff;
 /* ------------------------------------------------------------------------- */
 
-void buildReport(uchar send_key) {
-    keyboard_report.modifier = 0;
-
-    if(send_key >= 'a' && send_key <= 'z')
-        keyboard_report.keycode[0] = 4 + (send_key - 'a');
-    else
-        keyboard_report.keycode[0] = 0;
+static
+void memory_clean(void) {
+    memset(secret, 0, 32);
+    secret_length_b = 0;
 }
 
-uint8_t read_button(void){
-       uint8_t t,v=0;
-       t = DDRB;
-       DDRB &= ~(1<<BUTTON_PIN);
-       PORTB |= 1<<BUTTON_PIN;
-       PORTB &= ~(1<<BUTTON_PIN);
-       v |= PINB;
-       DDRB |= t&(1<<BUTTON_PIN);
-       PORTB &= ~(t&(1<<BUTTON_PIN));
-       v >>= BUTTON_PIN;
-       v &= 1;
-       v ^= 1;
-       return v;
+#define NO_CHECK 1
+
+static
+uint8_t secret_set(void){
+#if !NO_CHECK
+    uint8_t r;
+    union {
+        uint8_t w8[32];
+        uint16_t w16[16];
+    } read_back;
+#endif
+    const uint8_t length_B = (secret_length_b + 7) / 8;
+
+    eeprom_busy_wait();
+    eeprom_write_block(secret, secret_ee, length_B);
+#if !NO_CHECK
+    eeprom_busy_wait();
+    eeprom_read_block(read_back.w8, secret_ee, length_B);
+    r = memcmp(secret, read_back.w8, length_B);
+    memory_clean();
+    memset(read_back.w8, 0, 32);
+    if (r) {
+        return 1;
+    }
+#endif
+    eeprom_busy_wait();
+    eeprom_write_word(&secret_length_ee, secret_length_b);
+#if !NO_CHECK
+    eeprom_busy_wait();
+    r = eeprom_read_word(&secret_length_ee) == secret_length_b;
+    memory_clean();
+    *read_back.w16 = 0;
+    if (!r) {
+        return 1;
+    }
+#else
+    memory_clean();
+#endif
+
+    return 0;
 }
 
-void init_temperature_sensor(void){
-       ADMUX = 0x8F;
-       ADCSRA = 0x87;
+static
+void counter_inc(void){
+#if SIMPLE_COUNTER
+    uint32_t t;
+    eeprom_busy_wait();
+    t = eeprom_read_dword(&counter_ee);
+    eeprom_busy_wait();
+    eeprom_write_dword(&counter_ee, t + 1);
+#else
+    percnt_inc(0);
+#endif
 }
 
-uint16_t read_temperture_sensor(void){
-       ADCSRA |= 0x40;
-       while(ADCSRA & 0x40)
-               ;
-       return ADC;
+static
+void counter_reset(void) {
+    uint8_t reset_counter;
+    eeprom_busy_wait();
+    reset_counter = eeprom_read_byte(&reset_counter_ee);
+#if SIMPLE_COUNTER
+    eeprom_busy_wait();
+    eeprom_write_dword(&counter_ee, 0);
+#else
+    percnt_reset(0);
+#endif
+    eeprom_busy_wait();
+    eeprom_write_byte(&reset_counter_ee, reset_counter + 1);
 }
 
-#if 0
-uchar   usbFunctionSetup(uchar data[8])
-{
-usbRequest_t    *rq = (void *)data;
-
-    usbMsgPtr = reportBuffer;
-    if((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS){    /* class request type */
-        if(rq->bRequest == USBRQ_HID_GET_REPORT){  /* wValue: ReportType (highbyte), ReportID (lowbyte) */
-            /* we only have one report type, so don't look at wValue */
-            buildReport(keyPressed());
-            return sizeof(reportBuffer);
-        }else if(rq->bRequest == USBRQ_HID_GET_IDLE){
-            usbMsgPtr = &idleRate;
-            return 1;
-        }else if(rq->bRequest == USBRQ_HID_SET_IDLE){
-            idleRate = rq->wValue.bytes[1];
-        }
-    }else{
-        /* no vendor specific requests implemented */
+static
+void counter_init(void) {
+#if !SIMPLE_COUNTER
+    eeprom_busy_wait();
+    if (eeprom_read_byte(&reset_counter_ee) == 0) {
+        counter_reset();
     }
-    return 0;
+    percnt_init(0);
+#endif
 }
+
+static
+void token_generate(void) {
+    counter_inc();
+    eeprom_busy_wait();
+    eeprom_read_block(secret, secret_ee, 32);
+    eeprom_busy_wait();
+#if SIMPLE_COUNTER
+    hotp(token, secret, eeprom_read_word(&secret_length_ee), eeprom_read_dword(&counter_ee), eeprom_read_byte(&digits_ee));
+#else
+    hotp(token, secret, eeprom_read_word(&secret_length_ee), percnt_get(0), eeprom_read_byte(&digits_ee));
 #endif
+    memory_clean();
+}
+
+
+static
+void buildReport(uchar send_key) {
+    keyboard_report.modifier = 0;
+
+    switch (send_key) {
+    case '1' ... '9':
+        keyboard_report.keycode[0] = KEY_1 + (send_key-'1');
+        break;
+    case '0':
+        keyboard_report.keycode[0] = KEY_0;
+        break;
+    default:
+        keyboard_report.keycode[0] = 0;
+    }
+}
+
+static
+int8_t button_get_debounced(volatile uint8_t debounce_count) {
+    uint8_t v;
+    v = PINB & _BV(BUTTON_PIN);
+    while (debounce_count-- && v == (PINB & _BV(BUTTON_PIN))) {
+        ;
+    }
+    if (debounce_count) {
+        return -1;
+    }
+    return v ? 0 : 1;
+}
 
 usbMsgLen_t usbFunctionSetup(uchar data[8])
 {
        usbRequest_t    *rq = (usbRequest_t *)data;
        if ((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS) {    /* class request type */
-           color.name.red = 13;
            switch(rq->bRequest) {
         case USBRQ_HID_GET_REPORT: // send "no keys pressed" if asked here
             // wValue: ReportType (highbyte), ReportID (lowbyte)
@@ -273,40 +282,73 @@ usbMsgLen_t usbFunctionSetup(uchar data[8])
                current_command = rq->bRequest;
        switch(rq->bRequest)
                {
-               case CUSTOM_RQ_SET_RGB:
+       case CUSTOM_RQ_SET_SECRET:
+           secret_length_b = rq->wValue.word;
+           if (secret_length_b > 256) {
+               secret_length_b = 256;
+           }
+           uni_buffer.w8[0] = 0;
+           return USB_NO_MSG;
+       case CUSTOM_RQ_INC_COUNTER:
+           counter_inc();
+           return 0;
+       case CUSTOM_RQ_GET_COUNTER:
+#if SIMPLE_COUNTER
+           eeprom_busy_wait();
+           uni_buffer.w32[0] = eeprom_read_dword(&counter_ee);
+#else
+           uni_buffer.w32[0] = percnt_get(0);
+#endif
+           usbMsgPtr = (usbMsgPtr_t)uni_buffer.w32;
+           return 4;
+       case CUSTOM_RQ_RESET_COUNTER:
+           counter_reset();
+           return 0;
+        case CUSTOM_RQ_GET_RESET_COUNTER:
+           eeprom_busy_wait();
+            uni_buffer.w8[0] = eeprom_read_byte(&reset_counter_ee);
+            usbMsgPtr = uni_buffer.w8;
+            return 1;
+       case CUSTOM_RQ_SET_DIGITS:
+           if (rq->wValue.bytes[0] > 9) {
+               rq->wValue.bytes[0] = 9;
+           }
+           eeprom_busy_wait();
+           eeprom_write_byte(&digits_ee, rq->wValue.bytes[0]);
+           return 0;
+       case CUSTOM_RQ_GET_DIGITS:
+           eeprom_busy_wait();
+            uni_buffer.w8[0] = eeprom_read_byte(&digits_ee);
+            usbMsgPtr = uni_buffer.w8;
+            return 1;
+       case CUSTOM_RQ_GET_TOKEN:
+           token_generate();
+           usbMsgPtr = (usbMsgPtr_t)token;
+           return strlen(token);
+
+       case CUSTOM_RQ_PRESS_BUTTON:
+           key_state = STATE_SEND_KEY;
+           return 0;
+       case CUSTOM_RQ_CLR_DBG:
+            memset(dbg_buffer, 0, sizeof(dbg_buffer));
+            return 0;
+               case CUSTOM_RQ_SET_DBG:
                        return USB_NO_MSG;
-               case CUSTOM_RQ_GET_RGB:{
-                       usbMsgLen_t len = 6;
-                       if(len>rq->wLength.word){
+               case CUSTOM_RQ_GET_DBG:{
+                       usbMsgLen_t len = 8;
+                       if(len > rq->wLength.word){
                                len = rq->wLength.word;
                        }
-                       usbMsgPtr = (uchar*)color.idx;
+                       usbMsgPtr = dbg_buffer;
                        return len;
                }
-               case CUSTOM_RQ_READ_MEM:
-                       usbMsgPtr = (uchar*)rq->wValue.word;
-                       return rq->wLength.word;
-               case CUSTOM_RQ_WRITE_MEM:
-               case CUSTOM_RQ_EXEC_SPM:
-                       uni_buffer_fill = 4;
-                       uni_buffer.w16[0] = rq->wValue.word;
-                       uni_buffer.w16[1] = rq->wLength.word;
-                       return USB_NO_MSG;
-               case CUSTOM_RQ_READ_FLASH:
-                       uni_buffer.w16[0] = rq->wValue.word;
-                       uni_buffer.w16[1] = rq->wLength.word;
-                       return USB_NO_MSG;
                case CUSTOM_RQ_RESET:
                        soft_reset((uint8_t)(rq->wValue.word));
                        break;
                case CUSTOM_RQ_READ_BUTTON:
-                       uni_buffer.w8[0] = read_button();
+                       uni_buffer.w8[0] = button_get_debounced(25);
                        usbMsgPtr = uni_buffer.w8;
                        return 1;
-               case CUSTOM_RQ_READ_TMPSENS:
-                       uni_buffer.w16[0] = read_temperture_sensor();
-                       usbMsgPtr = uni_buffer.w8;
-                       return 2;
                }
     }
 
@@ -322,53 +364,30 @@ uchar usbFunctionWrite(uchar *data, uchar len)
            if (data[0] != LED_state)
                LED_state = data[0];
            return 1; // Data read, not expecting more
-       case CUSTOM_RQ_SET_RGB:
-               if(len != 6){
-                       return 1;
+       case CUSTOM_RQ_SET_SECRET:
+        {
+            if (uni_buffer.w8[0] < (secret_length_b + 7) / 8) {
+                memcpy(&secret[uni_buffer.w8[0]], data, len);
+                uni_buffer.w8[0] += len;
+            }
+            if (uni_buffer.w8[0] >= (secret_length_b + 7) / 8) {
+                secret_set();
+                return 1;
+            }
+            return 0;
+        }
+       case CUSTOM_RQ_SET_DBG:
+               if(len > sizeof(dbg_buffer)){
+                       len = sizeof(dbg_buffer);
                }
-               memcpy(color.idx, data, 6);
-        key_state = STATE_SEND_KEY;
+               memcpy(dbg_buffer, data, len);
                return 1;
-       case CUSTOM_RQ_WRITE_MEM:
-               memcpy(uni_buffer.ptr[0], data, len);
-               uni_buffer.w16[0] += len;
-               return !(uni_buffer.w16[1] -= len);
-       case CUSTOM_RQ_EXEC_SPM:
-               if(uni_buffer_fill < 8){
-                       uint8_t l = 8 - uni_buffer_fill;
-                       if(len<l){
-                               len = l;
-                       }
-                       memcpy(&(uni_buffer.w8[uni_buffer_fill]), data, len);
-                       uni_buffer_fill += len;
-                       return 0;
-               }
-               uni_buffer.w16[1] -= len;
-               if (uni_buffer.w16[1] > 8) {
-                       memcpy(uni_buffer.ptr[0], data, len);
-                       uni_buffer.w16[0] += len;
-                       return 0;
-               } else {
-                       memcpy(&(uni_buffer.w8[uni_buffer_fill]), data, len);
-                       exec_spm(uni_buffer.w16[2], uni_buffer.w16[3], uni_buffer.ptr[0], data, len);
-                       return 1;
-               }
        default:
                return 1;
        }
        return 0;
 }
 uchar usbFunctionRead(uchar *data, uchar len){
-       uchar ret = len;
-       switch(current_command){
-       case CUSTOM_RQ_READ_FLASH:
-               while(len--){
-                       *data++ = pgm_read_byte((uni_buffer.w16[0])++);
-               }
-               return ret;
-       default:
-               break;
-       }
        return 0;
 }
 
@@ -416,7 +435,8 @@ void usbEventResetReady(void)
 
 int main(void)
 {
-       uchar   i;
+       size_t idx = 0;
+       int8_t i = 0, last_stable_button_state = 0;
 
     wdt_enable(WDTO_1S);
     /* Even if you don't use the watchdog, turn it off here. On newer devices,
@@ -427,50 +447,52 @@ int main(void)
      * additional hardware initialization.
      */
 
-    memset(&keyboard_report, 0, sizeof(keyboard_report));
-
-    init_temperature_sensor();
+    DDRB &= ~_BV(BUTTON_PIN); /* make button pin input */
+    PORTB |= _BV(BUTTON_PIN); /* turn on pull-up resistor */
+    counter_init();
     usbInit();
     usbDeviceDisconnect();  /* enforce re-enumeration, do this while interrupts are disabled! */
-    i = 0;
     while(--i){             /* fake USB disconnect for ~512 ms */
         wdt_reset();
         _delay_ms(2);
     }
     usbDeviceConnect();
-    LED_PORT_DDR |= _BV(R_BIT) | _BV(G_BIT) | _BV(B_BIT);   /* make the LED bit an output */
-
        
     sei();
 
     for(;;){                /* main event loop */
-       //      update_pwm();
-               
         wdt_reset();
         usbPoll();
 
-        if(usbInterruptIsReady())
-            color.name.green = 0x10 | key_state;
-        else
-            color.name.green = 0;
+        i = button_get_debounced(25);
+        if (i != -1) {
+            if (last_stable_button_state == 0 && i == 1) {
+                key_state = STATE_SEND_KEY;
+            }
+            last_stable_button_state = i;
+        }
 
         if(usbInterruptIsReady() && key_state != STATE_WAIT){
-            color.name.red = 16;
             switch(key_state) {
             case STATE_SEND_KEY:
-                color.name.red = 17;
-                buildReport('x');
+                buildReport(token[idx]);
                 key_state = STATE_RELEASE_KEY; // release next
                 break;
             case STATE_RELEASE_KEY:
-                color.name.red = 18;
                 buildReport(0);
+                ++idx;
+                if (token[idx] == '\0') {
+                    idx = 0;
+                    key_state = STATE_WAIT;
+                } else {
+                    key_state = STATE_SEND_KEY;
+                }
+                break;
             default:
                 key_state = STATE_WAIT; // should not happen
             }
                         // start sending
             usbSetInterrupt((void *)&keyboard_report, sizeof(keyboard_report));
-            color.name.red |= 0x40;
 
         }