/* * 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" FILE * * stdio_ptr = NULL; void handle_stdio(uint8_t cmd) { switch(cmd) { case 'r': reset2bootloader(); break; case 'R': if(!sl018_reset(*stdio_ptr)) fprintf(*stdio_ptr, "ok\n\r"); break; case 'f': //get cardreader firmware version do { unsigned char * firmware_str = sl018_get_firmware_version(*stdio_ptr); if(firmware_str) fprintf(*stdio_ptr, "%s\n\r",firmware_str); } while(0); break; case 'e': //flash eeprom flash_keystore_from_stdio(*stdio_ptr); break; case 'd': //dump eeprom - this breaks security! dump_keystore_to_stdio(*stdio_ptr); break; case 'o': if(start_stepper(dir_open)) fprintf(*stdio_ptr, "ok\n\r"); else fprintf(*stdio_ptr, "error: already in progress\n\r"); break; case 'c': if(start_stepper(dir_close)) fprintf(*stdio_ptr, "ok\n\r"); else fprintf(*stdio_ptr, "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: fprintf(*stdio_ptr, "error, unknown command %02X '%c'\n\r",cmd, cmd); return; } } void handle_card(void) { uid_t uid; sl018_read_card_uid(&uid,*stdio_ptr); if (uid.length) { if(keystore_check_card(uid.buffer,uid.length)) { sl018_set_led(1,*stdio_ptr); _delay_ms(255); fprintf(*stdio_ptr,"Card allowed - opening/closing door\n\r"); // TODO: open/close door! sl018_set_led(0,*stdio_ptr); } else { fprintf(*stdio_ptr,"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(); stdio_ptr = usb_serial_get_stdio_ptr(); sei(); sl018_reset(*stdio_ptr); for(;;) { usb_serial_task(); int16_t bytes_received = usb_serial_bytes_received(); if(bytes_received > 0) handle_stdio(fgetc(*stdio_ptr)); if (sl018_check_for_new_card(*stdio_ptr)) handle_card(); handle_heartbeat(); } }