summaryrefslogtreecommitdiff
path: root/usb-i2c-sl018
diff options
context:
space:
mode:
authorChristian Pointner <equinox@spreadspace.org>2013-01-28 02:31:02 +0000
committerChristian Pointner <equinox@spreadspace.org>2013-01-28 02:31:02 +0000
commit2fc5ae9d043a2283a6113517b352ad4bd36935ac (patch)
treeee26bee1c7a93e1c6ebdf5dc940ecdb4b6b8d714 /usb-i2c-sl018
parentrenamed usb stream to stdio (diff)
some code cleanup
git-svn-id: https://svn.spreadspace.org/avr/trunk@90 aa12f405-d877-488e-9caf-2d797e2a1cc7
Diffstat (limited to 'usb-i2c-sl018')
-rw-r--r--usb-i2c-sl018/tuer-rfid.c226
1 files changed, 101 insertions, 125 deletions
diff --git a/usb-i2c-sl018/tuer-rfid.c b/usb-i2c-sl018/tuer-rfid.c
index 492d32d..af53d41 100644
--- a/usb-i2c-sl018/tuer-rfid.c
+++ b/usb-i2c-sl018/tuer-rfid.c
@@ -20,15 +20,6 @@
* along with spreadspace avr utils. If not, see <http://www.gnu.org/licenses/>.
*/
-#define TAG_STA_PIN PINB
-#define TAG_STA_BIT 7
-// #define PIN_B_INTERRUPT 1<<TAG_STA_BIT
-//#define PIN_C_INTERRUPT 0
-//#define PIN_D_INTERRUPT 0
-// #define CARD_IN 0xe1
-// #define CARD_OUT 0xe0
-#define CARD_BOOL ( (TAG_STA_PIN >> TAG_STA_BIT) & 1)
-
#include <avr/io.h>
#include <avr/wdt.h>
#include <avr/interrupt.h>
@@ -52,13 +43,16 @@ typedef union
} keyslot_t;
keyslot_t EEMEM keyslots[EEPROM_SIZE/sizeof(keyslot_t)];
+#define TAG_STA_PIN PINB
+#define TAG_STA_BIT 7
+#define CARD_PRESENT (!((TAG_STA_PIN >> TAG_STA_BIT) & 1))
+
const uint8_t SL018_ADDR = 0xa0;
const uint8_t SL018_WRITE = 0;
const uint8_t SL018_READ = 1;
//============================================
// Command List, preamble + length + command
//============================================
-
const unsigned char SL018CMD_ComSelectCard[] ={1,1};
const unsigned char SL018CMD_ComRedLedOn[] ={2,0x40,1};
const unsigned char SL018CMD_ComRedLedOff[] ={2,0x40,0};
@@ -72,25 +66,14 @@ typedef struct __attribute__((__packed__))
uint8_t status;
unsigned char data[sizeof(twi_rcv_buff)-3];
uint8_t len;
-} SL018CMD_MESSAGE;
+} sl018_message_t;
-SL018CMD_MESSAGE * twi_message = (SL018CMD_MESSAGE *) &twi_rcv_buff;
+sl018_message_t* twi_message = (sl018_message_t *) &twi_rcv_buff;
uint8_t card_status =0;
FILE stdio_stream;
FILE * stdio = &stdio_stream;
-#define KEY_LEN_MAX (sizeof(keyslot_t) - 1)
-
-/* this generates a Fletcher8 checksum */
-/* code from: http://stackoverflow.com/questions/13491700/8-bit-fletcher-checksum-of-16-byte-data */
-unsigned char generate_csum(unsigned char* data)
-{
- uint16_t sum1 = 0xf, sum2 = 0xf, len = KEY_LEN_MAX;
- do { sum2 += ( sum1 += *data++ ); } while (--len);
- return sum2<<4 | sum1;
-}
-
/*
LUFA Library
@@ -142,71 +125,61 @@ void EVENT_USB_Device_Disconnect(void)
/* end LUFA CDC-ACM specific definitions*/
-unsigned char sl018_send_buffer(const unsigned char * buffer)
+
+bool sl018_cmd_raw(const unsigned char * buffer)
{
- uint8_t len = 0;
- uint8_t pos = 0;
- //unsigned char ret;
- len = buffer[0];
- if (TWI_StartTransmission(SL018_ADDR | SL018_WRITE,10) == TWI_ERROR_NoError)
- {
- for(pos=0; pos<=len; pos++)
- if( ! TWI_SendByte(buffer[pos]))
- {
- TWI_StopTransmission();
- return 1;
- }
- // Must stop transmission afterwards to release the bus
+ uint8_t len = 0;
+ uint8_t pos = 0;
+ //unsigned char ret;
+ len = buffer[0];
+ if (TWI_StartTransmission(SL018_ADDR | SL018_WRITE,10) == TWI_ERROR_NoError) {
+ for(pos=0; pos<=len; pos++)
+ if( ! TWI_SendByte(buffer[pos])) {
TWI_StopTransmission();
- } else {
- return 1;
- }
- _delay_ms(50);
- if (TWI_StartTransmission(SL018_ADDR | SL018_READ,10) == TWI_ERROR_NoError)
- {
- TWI_ReceiveByte(&len, 0);
- twi_rcv_buff[255]=len;
- for(pos=0; pos<len; pos++)
- if (! TWI_ReceiveByte(&twi_rcv_buff[pos], (pos == len-1) ? 1:0 ) )
- {
- TWI_StopTransmission();
- return 1;
- }
+ return 1;
+ }
// Must stop transmission afterwards to release the bus
+ TWI_StopTransmission();
+ } else
+ return 1;
+
+ _delay_ms(50);
+ if (TWI_StartTransmission(SL018_ADDR | SL018_READ,10) == TWI_ERROR_NoError) {
+ TWI_ReceiveByte(&len, 0);
+ twi_rcv_buff[255]=len;
+ for(pos=0; pos<len; pos++)
+ if (! TWI_ReceiveByte(&twi_rcv_buff[pos], (pos == len-1) ? 1:0 ) ) {
TWI_StopTransmission();
- } else {
- return 1;
- }
- return 0;
-}
+ return 1;
+ }
+ // Must stop transmission afterwards to release the bus
+ TWI_StopTransmission();
+ } else
+ return 1;
+ return 0;
+}
-bool check_card(unsigned char * uid, uint8_t uid_len)
+bool sl018_cmd(const unsigned char * buffer)
{
- keyslot_t card;
- keyslot_t eeprom_ks;
- card.uint=0;
- for (uint8_t pos=0; pos<uid_len; pos++)
- card.byte[pos]=uid[uid_len-pos-1];
- card.byte[KEY_LEN_MAX]=generate_csum(&card.byte[0]);
- keyslot_t * ks=keyslots;
- bool valid=0;
- for(uint8_t ks_num=0;ks_num<(EEPROM_SIZE/sizeof(card));ks_num++)
- {
- eeprom_read_block(&eeprom_ks,&ks[ks_num],sizeof(eeprom_ks));
- if(card.uint==eeprom_ks.uint)
- {
- valid=1;
- // break; //this breaks security
+ sl018_message_t * send_twi_message = (sl018_message_t *) (buffer+1);
+ if(sl018_cmd_raw(buffer)) {
+ fprintf(stdio, "I2C error\n\r");
+ return 0;
+ } else {
+ if(twi_message->status) {
+ fprintf(stdio,"SL018 Cmd,Error: %02X,%02X\n\r",twi_message->command,twi_message->status);
+ return 0;
+ }
+ if(send_twi_message->command != twi_message->command) {
+ fprintf(stdio,"SL018 Cmd missmatch: send, rcv: %02X,%02X\n\r",send_twi_message->command,twi_message->command);
}
}
- if (valid)
- {
- return 1;
- }
- return 0;
+ return 1;
}
+
+
void flash_eeprom_from_stdio(void)
{
keyslot_t keyslot;
@@ -214,14 +187,12 @@ void flash_eeprom_from_stdio(void)
uint8_t byte_pos=0;
fprintf(stdio,"Flashing keys:\n\r");
fflush(stdio);
- for(keyslot_pos=0;keyslot_pos<EEPROM_SIZE/sizeof(keyslot);)
- {
+ for(keyslot_pos=0;keyslot_pos<EEPROM_SIZE/sizeof(keyslot);) {
int16_t BytesReceived = CDC_Device_BytesReceived(&VirtualSerial_CDC_Interface);
while(BytesReceived >0) {
keyslot.byte[byte_pos++]=fgetc(stdio);
BytesReceived--;
- if (byte_pos == sizeof(keyslot))
- {
+ if (byte_pos == sizeof(keyslot)) {
byte_pos=0;
eeprom_update_block(&keyslot,&keyslots[keyslot_pos],sizeof(keyslot));
keyslot_pos++;
@@ -241,8 +212,7 @@ void dump_eeprom_to_stdio(void)
keyslot_t keyslot;
uint8_t keyslot_pos=0;
- for(keyslot_pos=0;keyslot_pos<EEPROM_SIZE/sizeof(keyslot);keyslot_pos++)
- {
+ for(keyslot_pos=0;keyslot_pos<EEPROM_SIZE/sizeof(keyslot);keyslot_pos++) {
eeprom_read_block(&keyslot,&keyslots[keyslot_pos],sizeof(keyslot));
for (uint8_t i=0; i< sizeof(keyslot); i++)
fprintf(stdio,"%02X",keyslot.byte[i]);
@@ -250,50 +220,29 @@ void dump_eeprom_to_stdio(void)
}
}
-bool sl018_send_buffer_check_error(const unsigned char * buffer)
-{
- SL018CMD_MESSAGE * send_twi_message = (SL018CMD_MESSAGE *) (buffer+1);
- if(sl018_send_buffer(buffer))
- {
- fprintf(stdio, "I2C error\n\r");
- return 0;
- } else {
- if(twi_message->status)
- {
- fprintf(stdio,"SL018 Cmd,Error: %02X,%02X\n\r",twi_message->command,twi_message->status);
- return 0;
- }
- if(send_twi_message->command != twi_message->command)
- {
- fprintf(stdio,"SL018 Cmd missmatch: send, rcv: %02X,%02X\n\r",send_twi_message->command,twi_message->command);
- }
- }
- return 1;
-}
-
void handle_cmd(uint8_t cmd)
{
switch(cmd) {
case 'r': reset2bootloader(); break;
case 'f': //get cardreader firmware version
- if(sl018_send_buffer_check_error(SL018CMD_ComGetFirmwareVersion))
+ if(sl018_cmd(SL018CMD_ComGetFirmwareVersion))
{
fprintf(stdio, "%s\n\r",twi_message->data);
}
break;
case '4': //turn cardreader led off
- if(sl018_send_buffer_check_error(SL018CMD_ComRedLedOff))
+ if(sl018_cmd(SL018CMD_ComRedLedOff))
fprintf(stdio, "ok\n\r");
break;
case '5': //turn cardreader led on
- if(sl018_send_buffer_check_error(SL018CMD_ComRedLedOn))
+ if(sl018_cmd(SL018CMD_ComRedLedOn))
fprintf(stdio, "ok\n\r");
break;
case 'e': //flash eeprom
flash_eeprom_from_stdio();
break;
- case 'd': //flash eeprom
+ case 'd': //dump eeprom - this breaks security!
dump_eeprom_to_stdio();
break;
case 's': //get eeprom size
@@ -303,26 +252,56 @@ void handle_cmd(uint8_t cmd)
}
}
-void card_in_action(void)
+
+
+/* this generates a Fletcher8 checksum */
+/* code from: http://stackoverflow.com/questions/13491700/8-bit-fletcher-checksum-of-16-byte-data */
+unsigned char generate_csum(unsigned char* data)
+{
+ uint16_t sum1 = 0xf, sum2 = 0xf, len = sizeof(keyslot_t) - 1;
+ do { sum2 += ( sum1 += *data++ ); } while (--len);
+ return sum2<<4 | sum1;
+}
+
+bool check_card(const unsigned char * uid, uint8_t uid_len)
+{
+ keyslot_t card;
+ keyslot_t eeprom_ks;
+ card.uint=0;
+ for (uint8_t pos=0; pos<uid_len; pos++)
+ card.byte[pos]=uid[uid_len-pos-1];
+ card.byte[sizeof(keyslot_t) - 1]=generate_csum(&card.byte[0]);
+ keyslot_t * ks=keyslots;
+ bool valid=0;
+ for(uint8_t ks_num=0;ks_num<(EEPROM_SIZE/sizeof(card));ks_num++) {
+ eeprom_read_block(&eeprom_ks,&ks[ks_num],sizeof(eeprom_ks));
+ if(card.uint==eeprom_ks.uint) { // this breaks security (not const time)
+ valid=1;
+ break; //this breaks security (not const time)
+ }
+ }
+ if(valid)
+ return 1;
+ return 0;
+}
+
+void handle_card(void)
{
uint8_t pos;
fprintf(stdio, "CARD IN: ");
- if(sl018_send_buffer_check_error(SL018CMD_ComSelectCard))
+ if(sl018_cmd(SL018CMD_ComSelectCard))
{
for (pos=twi_message->len - 4 ; pos< 255; pos--)
- {
fprintf(stdio,"%02X",twi_message->data[pos]);
- }
+
fprintf(stdio, "\n\r");
uint8_t type = twi_message->data[twi_message->len-1-sizeof(twi_message->command)-sizeof(twi_message->status)];
- if (0<type && type < 7)
- {
- if(check_card(&twi_message->data[0],twi_message->len-1-sizeof(twi_message->command)-sizeof(twi_message->status)))
- {
- sl018_send_buffer_check_error(SL018CMD_ComRedLedOn);
+ if (0<type && type < 7) {
+ if(check_card(&twi_message->data[0],twi_message->len-1-sizeof(twi_message->command)-sizeof(twi_message->status))) {
+ sl018_cmd(SL018CMD_ComRedLedOn);
fprintf(stdio,"open\n\r");
_delay_ms(255);
- sl018_send_buffer_check_error(SL018CMD_ComRedLedOff);
+ sl018_cmd(SL018CMD_ComRedLedOff);
}
} else {
fprintf(stdio,"Unknown Card Type %02x\n\r",type);
@@ -347,13 +326,10 @@ int main(void)
if(BytesReceived > 0) {
handle_cmd(fgetc(stdio));
}
- if(CARD_BOOL != card_status)
- {
- card_status = CARD_BOOL;
- if (!card_status)
- {
- card_in_action();
- }
+ if(CARD_PRESENT != card_status) {
+ card_status = CARD_PRESENT;
+ if(card_status)
+ handle_card();
}
CDC_Device_USBTask(&VirtualSerial_CDC_Interface);
USB_USBTask();