X-Git-Url: https://git.cryptolib.org/?p=labortage2013badge.git;a=blobdiff_plain;f=firmware%2Fmain.c;h=53898b05ea0f5ccef6e9bfe7a1a71c36973b0533;hp=20bf8c09e592f664893c6b0b9a42b27de48427db;hb=bb8b3d49a5a36eb1e52b79429fe7513425d02fc3;hpb=06016b0f48e8407fdc6000735d310b84fd1fd0bc diff --git a/firmware/main.c b/firmware/main.c index 20bf8c0..53898b0 100644 --- a/firmware/main.c +++ b/firmware/main.c @@ -38,41 +38,51 @@ different port or bit, change the macros below: #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" +#include "percnt2.h" /* ------------------------------------------------------------------------- */ /* ----------------------------- USB interface ----------------------------- */ /* ------------------------------------------------------------------------- */ -const PROGMEM char usbHidReportDescriptor[35] = { /* USB report descriptor */ +PROGMEM const char usbHidReportDescriptor[USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH] = { 0x05, 0x01, // USAGE_PAGE (Generic Desktop) 0x09, 0x06, // USAGE (Keyboard) 0xa1, 0x01, // COLLECTION (Application) - 0x05, 0x07, // USAGE_PAGE (Keyboard) - 0x19, 0xe0, // USAGE_MINIMUM (Keyboard LeftControl) - 0x29, 0xe7, // USAGE_MAXIMUM (Keyboard Right GUI) + 0x75, 0x01, // REPORT_SIZE (1) + 0x95, 0x08, // REPORT_COUNT (8) + 0x05, 0x07, // USAGE_PAGE (Keyboard)(Key Codes) + 0x19, 0xe0, // USAGE_MINIMUM (Keyboard LeftControl)(224) + 0x29, 0xe7, // USAGE_MAXIMUM (Keyboard Right GUI)(231) 0x15, 0x00, // LOGICAL_MINIMUM (0) 0x25, 0x01, // LOGICAL_MAXIMUM (1) + 0x81, 0x02, // INPUT (Data,Var,Abs) ; Modifier byte + 0x95, 0x01, // REPORT_COUNT (1) + 0x75, 0x08, // REPORT_SIZE (8) + 0x81, 0x03, // INPUT (Cnst,Var,Abs) ; Reserved byte + 0x95, 0x05, // REPORT_COUNT (5) 0x75, 0x01, // REPORT_SIZE (1) - 0x95, 0x08, // REPORT_COUNT (8) - 0x81, 0x02, // INPUT (Data,Var,Abs) + 0x05, 0x08, // USAGE_PAGE (LEDs) + 0x19, 0x01, // USAGE_MINIMUM (Num Lock) + 0x29, 0x05, // USAGE_MAXIMUM (Kana) + 0x91, 0x02, // OUTPUT (Data,Var,Abs) ; LED report 0x95, 0x01, // REPORT_COUNT (1) + 0x75, 0x03, // REPORT_SIZE (3) + 0x91, 0x03, // OUTPUT (Cnst,Var,Abs) ; LED report padding + 0x95, 0x06, // REPORT_COUNT (6) 0x75, 0x08, // REPORT_SIZE (8) + 0x15, 0x00, // LOGICAL_MINIMUM (0) 0x25, 0x65, // LOGICAL_MAXIMUM (101) - 0x19, 0x00, // USAGE_MINIMUM (Reserved (no event indicated)) - 0x29, 0x65, // USAGE_MAXIMUM (Keyboard Application) + 0x05, 0x07, // USAGE_PAGE (Keyboard)(Key Codes) + 0x19, 0x00, // USAGE_MINIMUM (Reserved (no event indicated))(0) + 0x29, 0x65, // USAGE_MAXIMUM (Keyboard Application)(101) 0x81, 0x00, // INPUT (Data,Ary,Abs) 0xc0 // END_COLLECTION }; -/* We use a simplifed keyboard report descriptor which does not support the - * boot protocol. We don't allow setting status LEDs and we only allow one - * simultaneous key press (except modifiers). We can therefore use short - * 2 byte input reports. - * The report descriptor has been created with usb.org's "HID Descriptor Tool" - * which can be downloaded from http://www.usb.org/developers/hidpage/. - * Redundant entries (such as LOGICAL_MINIMUM and USAGE_PAGE) have been omitted - * for the second INPUT item. - */ + +uint16_t secret_length_ee EEMEM = 0; +uint8_t secret_ee[32] EEMEM; +uint8_t reset_counter_ee EEMEM = 0; +uint8_t digits_ee EEMEM = 8; /* Keyboard usage values, see usb.org's HID-usage-tables document, chapter * 10 Keyboard/Keypad Page for more codes. @@ -136,16 +146,18 @@ const PROGMEM char usbHidReportDescriptor[35] = { /* USB report descriptor */ #define KEY_F11 68 #define KEY_F12 69 -union { - struct { - uint16_t red; - uint16_t green; - uint16_t blue; - } name; - uint16_t idx[3]; -} color; +#define NUM_LOCK 1 +#define CAPS_LOCK 2 +#define SCROLL_LOCK 4 + +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 + +#define UNI_BUFFER_SIZE 36 static union { uint8_t w8[UNI_BUFFER_SIZE]; @@ -157,26 +169,118 @@ static union { static uint8_t uni_buffer_fill; static uint8_t current_command; -static uchar reportBuffer[2]; /* buffer for HID reports */ -static uchar idleRate; /* in 4 ms units */ +typedef struct { + uint8_t modifier; + uint8_t reserved; + uint8_t keycode[6]; +} keyboard_report_t; + +#define STATE_WAIT 0 +#define STATE_SEND_KEY 1 +#define STATE_RELEASE_KEY 2 +#define STATE_NEXT 3 + -static bool keyDidChange = false; +static keyboard_report_t keyboard_report; // sent to PC +static uchar idleRate; /* in 4 ms units */ +static uchar key_state = STATE_WAIT; +volatile static uchar LED_state = 0xff; // received from PC /* ------------------------------------------------------------------------- */ +void memory_clean(void) { + memset(secret, 0, 32); + secret_length_b = 0; +} + +uint8_t secret_set(void){ + uint8_t r; + union { + uint8_t w8[32]; + uint16_t w16[16]; + } read_back; + const uint8_t length_B = (secret_length_b + 7) / 8; + + eeprom_busy_wait(); + eeprom_write_block(secret, secret_ee, length_B); + 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; + } + eeprom_busy_wait(); + eeprom_write_word(&secret_length_ee, secret_length_b); + eeprom_busy_wait(); + r = eeprom_read_word(&secret_length_ee) == secret_length_b; + memory_clean(); + *read_back.w16 = 0; + if (!r) { + return 1; + } + return 0; +} + +void token_generate(void) { + percnt_inc(0); + eeprom_busy_wait(); + eeprom_read_block(secret, secret_ee, 32); + eeprom_busy_wait(); + hotp(token, secret, eeprom_read_word(&secret_length_ee), percnt_get(0), eeprom_read_byte(&digits_ee)); + memory_clean(); +} + +void counter_reset(void) { + uint8_t reset_counter; + eeprom_busy_wait(); + reset_counter = eeprom_read_byte(&reset_counter_ee); + percnt_reset(0); + eeprom_busy_wait(); + eeprom_write_byte(&reset_counter_ee, reset_counter + 1); +} + +void counter_init(void) { + eeprom_busy_wait(); + if (eeprom_read_byte(&reset_counter_ee) == 0) { + counter_reset(); + } + percnt_init(0); +} + +void buildReport(uchar send_key) { + keyboard_report.modifier = 0; + + switch (send_key) { + case 'A' ... 'Z': + keyboard_report.modifier = MOD_SHIFT_LEFT; + keyboard_report.keycode[0] = KEY_A + (send_key-'A'); + break; + case 'a' ... 'z': + keyboard_report.keycode[0] = KEY_A + (send_key-'a'); + break; + 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; + } +} -uint8_t read_button(void){ - uint8_t t,v=0; - t = DDRB; - DDRB &= ~(1<>= BUTTON_PIN; - v &= 1; - v ^= 1; - return v; +static inline +int8_t button_get_debounced(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; } void init_temperature_sensor(void){ @@ -191,64 +295,88 @@ uint16_t read_temperture_sensor(void){ return ADC; } -#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 */ - } - return 0; -} -#endif - 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; - if (rq->bRequest == USBRQ_HID_GET_REPORT){ /* wValue: ReportType (highbyte), ReportID (lowbyte) */ - /* we only have one report type, so don't look at wValue */ - if (color.name.red == 133) { - color.name.red = 23; - usbMsgPtr = reportBuffer; - reportBuffer[0] = 0; - reportBuffer[1] = KEY_X; + switch(rq->bRequest) { + case USBRQ_HID_GET_REPORT: // send "no keys pressed" if asked here + // wValue: ReportType (highbyte), ReportID (lowbyte) + usbMsgPtr = (void *)&keyboard_report; // we only have this one + keyboard_report.modifier = 0; + keyboard_report.keycode[0] = 0; + return sizeof(keyboard_report); + case USBRQ_HID_SET_REPORT: // if wLength == 1, should be LED state + if (rq->wLength.word == 1) { + current_command = LED_WRITE; + return USB_NO_MSG; } - return sizeof(reportBuffer); - } else if (rq->bRequest == USBRQ_HID_GET_IDLE) { + return 0; + case USBRQ_HID_GET_IDLE: // send idle rate to PC as required by spec usbMsgPtr = &idleRate; return 1; - }else if (rq->bRequest == USBRQ_HID_SET_IDLE) { - usbMsgPtr = reportBuffer; + case USBRQ_HID_SET_IDLE: // save idle rate as required by spec idleRate = rq->wValue.bytes[1]; + return 0; } } if ((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_VENDOR) { 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: + percnt_inc(0); + return 0; + case CUSTOM_RQ_GET_COUNTER: + uni_buffer.w32[0] = percnt_get(0); + 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: @@ -256,19 +384,20 @@ usbMsgLen_t usbFunctionSetup(uchar data[8]) return rq->wLength.word; case CUSTOM_RQ_WRITE_MEM: case CUSTOM_RQ_EXEC_SPM: - uni_buffer_fill = 4; +/* 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: +*/ case CUSTOM_RQ_READ_FLASH: uni_buffer.w16[0] = rq->wValue.word; uni_buffer.w16[1] = rq->wLength.word; + uni_buffer_fill = 4; 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: @@ -281,23 +410,40 @@ usbMsgLen_t usbFunctionSetup(uchar data[8]) return 0; /* default for not implemented requests: return no data back to host */ } + uchar usbFunctionWrite(uchar *data, uchar len) { switch(current_command){ - case CUSTOM_RQ_SET_RGB: - if(len!=6){ - return 1; + + case LED_WRITE: + if (data[0] != LED_state) + LED_state = data[0]; + return 1; // Data read, not expecting more + 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); - keyDidChange = true; + 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(uni_buffer_fill < 8){ + uint8_t l = 8 - uni_buffer_fill; if(len8){ + if (uni_buffer.w16[1] > 8) { memcpy(uni_buffer.ptr[0], data, len); uni_buffer.w16[0] += len; return 0; - }else{ + } 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; @@ -321,7 +467,7 @@ uchar usbFunctionWrite(uchar *data, uchar len) return 0; } uchar usbFunctionRead(uchar *data, uchar len){ - uchar ret=len; + uchar ret = len; switch(current_command){ case CUSTOM_RQ_READ_FLASH: while(len--){ @@ -341,22 +487,22 @@ uchar trialValue = 0, optimumValue; int x, optimumDev, targetValue = (unsigned)(1499 * (double)F_CPU / 10.5e6 + 0.5); /* do a binary search: */ - do{ + do { OSCCAL = trialValue + step; x = usbMeasureFrameLength(); // proportional to current real frequency if(x < targetValue) // frequency still too low trialValue += step; step >>= 1; - }while(step > 0); + } while(step > 0); /* We have a precision of +/- 1 for optimum OSCCAL here */ /* now do a neighborhood search for optimum value */ optimumValue = trialValue; optimumDev = x; // this is certainly far away from optimum - for(OSCCAL = trialValue - 1; OSCCAL <= trialValue + 1; OSCCAL++){ + for (OSCCAL = trialValue - 1; OSCCAL <= trialValue + 1; OSCCAL++){ x = usbMeasureFrameLength() - targetValue; - if(x < 0) + if (x < 0) x = -x; - if(x < optimumDev){ + if (x < optimumDev) { optimumDev = x; optimumValue = OSCCAL; } @@ -378,7 +524,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, @@ -389,33 +536,55 @@ int main(void) * additional hardware initialization. */ + DDRB &= ~_BV(BUTTON_PIN); /* make button pin input */ + PORTB |= _BV(BUTTON_PIN); /* turn on pull-up resistor */ init_temperature_sensor(); usbInit(); usbDeviceDisconnect(); /* enforce re-enumeration, do this while interrupts are disabled! */ - i = 0; - while(--i){ /* fake USB disconnect for > 250 ms */ + while(--i){ /* fake USB disconnect for ~512 ms */ wdt_reset(); - _delay_ms(1); + _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(keyDidChange && usbInterruptIsReady()){ - keyDidChange = 0; - color.name.red = 42; - /* use last key and not current key status in order to avoid lost - changes in key status. */ - reportBuffer[0] = 0; - reportBuffer[1] = KEY_Y; - usbSetInterrupt(reportBuffer, sizeof(reportBuffer)); + + 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){ + switch(key_state) { + case STATE_SEND_KEY: + buildReport(token[idx]); + key_state = STATE_RELEASE_KEY; // release next + break; + case STATE_RELEASE_KEY: + 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)); + + } + } return 0; }