master
Stefan Krulj 14 years ago
parent 5b979b8d48
commit 4e82bc31cd
  1. 63
      libcrypt/main.c
  2. 14
      libcrypt/main.h

@ -19,6 +19,7 @@
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);
@ -65,15 +66,51 @@ void buildReport(uint8_t mod, uint8_t key){
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<<PBTN))==0 ){
writeToBuffer(USB_KEY_ENTER);
inc_cnt();
device_state=DEV_STATE_CONFIRMED;
_delay_ms(100);
}
break;
case DEV_STATE_CONFIRMED:
default:
if( prev_btn!=0 && ((PIN_BTN)&(1<<PBTN))==0 ){
device_state=DEV_STATE_RESET;
_delay_ms(100);
}
break;
}
prev_btn=(PIN_BTN)&(1<<PBTN);
}
int main(void){
#ifdef CALIB
uint8_t calibrationValue;
#endif
uint8_t sec[SECLEN]=SECRET;
uint32_t cnt;
#ifdef CALIB
/* oszillator calibration */
calibrationValue = eeprom_read_byte(0); /* calibration value from last time */
@ -92,23 +129,21 @@ int main(void){
writeToBuffer(USB_KEY_NOP);
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(792,8,sec,SECLEN));
//inc_cnt();
}
/*Set button to input*/
DDR_BTN &= ~(1<<PBTN);
/*internal pull-up on*/
PORT_BTN |= 1<<PBTN;
usbInit();
sei();
for(;;){
wdt_reset();
usbPoll();
/* Main FSM*/
main_fsm();
/* Send Keystrokes, if asked to */
if(usbInterruptIsReady()){
if(!buff_empty || next_key!=0){
if(next_key==0){

@ -8,13 +8,11 @@
#include <util/delay.h>
#include <avr/io.h>
#define _PORT_BTN B
#define _NUM_BTN 3
#define PORT_BTN PORT_PORT_BTN
#define DDR_BTN DDR_PORT_BTN
#define P_BTN P_PORT_BTN_NUM_BTN
#define BTN_VECT INT_NUM_BTN_vect
#define PORT_BTN PORTB
#define PIN_BTN PINB
#define DDR_BTN DDRB
#define PBTN PB3
#define CNTPOS_PTR ((void*)1)
#define ERROR 32202
@ -23,3 +21,7 @@
#define UINT32_MAX 4294967296
#endif
#define DEV_STATE_RESET 0
#define DEV_STATE_SENT 1
#define DEV_STATE_CONFIRMED 2

Loading…
Cancel
Save