/* * spreadspace avr utils * * * Copyright (C) 2013 Christian Pointner * Othmar Gsenger * * This file is part of spreadspace avr utils. * * spreadspace avr utils is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * any later version. * * spreadspace avr utils is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with spreadspace avr utils. If not, see . */ #include #include #include #include #include #include "LUFA/Drivers/Peripheral/TWI.h" #include #include #include "util.h" #include "led.h" #include "heartbeat.h" #include "stepper.h" #include "ledmatrix.h" #include "sl018.h" #include "keystore.h" #include "usb_serial.h" #include "statemachine.h" void handle_stdio(uint8_t cmd) { switch(cmd) { case 'r': reset2bootloader(); break; case 'R': if(!sl018_reset()) printf("ok\n\r"); break; case 'f': //get cardreader firmware version do { unsigned char * firmware_str = sl018_get_firmware_version(); if(firmware_str) printf("%s\n\r",firmware_str); } while(0); break; case 'e': //flash eeprom flash_keystore_from_stdio(); break; case 'd': //dump eeprom - this breaks security! dump_keystore_to_stdio(); break; case 'o': if(start_stepper(dir_open)) printf("ok\n\r"); else printf("error: already in progress\n\r"); break; case 'c': if(start_stepper(dir_close)) printf("ok\n\r"); else printf("error: already in progress\n\r"); break; case '0': ledmatrix(off); break; case '1': ledmatrix(red); break; case '2': ledmatrix(red_moving); break; case '3': ledmatrix(red_blink); break; case '4': ledmatrix(green); break; case '5': ledmatrix(green_moving); break; case '6': ledmatrix(green_blink); break; case '7': ledmatrix(rg_moving); break; case '8': ledmatrix(rg_blink); break; default: printf("error, unknown command %02X '%c'\n\r",cmd, cmd); return; } } void handle_card(void) { uid_t uid; sl018_read_card_uid(&uid); if (uid.length) { if(keystore_check_card(uid.buffer,uid.length)) { sl018_set_led(1); _delay_ms(255); printf("Card allowed - opening/closing door\n\r"); // TODO: open/close door! sl018_set_led(0); } else { printf("Card not found in Database\n\r"); } } } int main(void) { MCUSR &= ~(1 << WDRF); wdt_disable(); cpu_init(); led_init(); usb_serial_init(); TWI_Init(TWI_BIT_PRESCALE_1, TWI_BITLENGTH_FROM_FREQ(1, 200000)); init_heartbeat(); init_stepper(); init_ledmatrix(); sei(); sl018_reset(); for(;;) { statemachine_task(); usb_serial_task(); int16_t bytes_received = usb_serial_bytes_received(); if(bytes_received > 0) handle_stdio(fgetc(stdin)); if (sl018_check_for_new_card()) handle_card(); handle_heartbeat(); } }