/* * This code is based on seveal VUSB-Examples the * HID was taken from the CapsLocker code from macetech * http://macetech.com/blog/?q=node/46 * */ #include "main.h" #define MSG "what do ya want for nothing?" #include #include #include #include #include #include #include uint8_t reportBuffer[2]; /* buffer for HID reports */ static uint8_t next_key=0, prev_key=0xFF; static uint8_t sec[SECLEN]=SECRET; uint32_t get_cnt(){ void* p=(void*)eeprom_read_word(CNTPOS_PTR); return eeprom_read_dword(p); } uint8_t inc_cnt(){ uint32_t cnt; void* p=(void*)eeprom_read_word(CNTPOS_PTR); cnt=eeprom_read_dword(p); if(cnt==UINT32_MAX){ eeprom_write_word(CNTPOS_PTR,ERROR); return 0; } eeprom_write_dword(p,cnt+1); retry: if(eeprom_read_dword(p)!=cnt+1){ eeprom_write_dword(p,cnt+1); if(eeprom_read_dword(p)!=cnt+1){ /*Probabbly the EEPROM position is bad, try an other*/ p+=4; if(p+4>(void*)EEPROM_SIZE){ eeprom_write_word(CNTPOS_PTR,ERROR); return 0; } eeprom_write_word(CNTPOS_PTR,(uint16_t)p); /* I know some people don't like goto because of dijkstra's legendary article "goto considered harmfull". The truth is he didn't want us to stop using goto at all, but to think if it is appropriate. The alternative here would be a loop around this whole block and some well-placed "break" and "continue" kbd_event_statements - I think that would be even worse than the goto*/ goto retry; } } return 1; } void buildReport(uint8_t mod, uint8_t key){ reportBuffer[0] = mod; reportBuffer[1] = key; } void main_fsm(){ static uint8_t device_state=0; static uint8_t prev_btn=0; switch(device_state){ case DEV_STATE_RESET: { uint32_t cnt; if( eeprom_read_word(CNTPOS_PTR)==ERROR ){ writeToBuffer(USB_KEY_E); writeToBuffer(USB_KEY_R); writeToBuffer(USB_KEY_R); writeToBuffer(USB_KEY_O); writeToBuffer(USB_KEY_R); }else{ cnt = get_cnt(); buffer_unum(get_otp_from_cnt(cnt,8,sec,SECLEN)); } device_state=DEV_STATE_SENT; break; } case DEV_STATE_SENT: if( prev_btn!=0 && ((PIN_BTN)&(1<