Esempio n. 1
0
void loop_clone_rfid(uint8_t *menu, uint8_t *opmode)
{
    uint8_t data[80];
    uint8_t keyindex = 0;
    uint8_t block = 0;
    uint8_t tries = 0;
    int res, oid;

	get_firmware_version();

    while (block < BLOCKS) {
        if ( READ != *menu) { break; }
        res = mifare_reader_init(data, sizeof(data));

        if (tries >= KEYS) {
            tries = 0;
            block += 1;
        }

        if (res >= 0) {
            res = initiator_init(data, sizeof(data));

            if (res >= 11) {
                if (0x00 == data[3] && data[6] >= 0x04) {
                    memcpy(&oid, &data[7], sizeof(oid));
                    if (0x00 == block) {
                        debug_printf("MIFARE_CARD_ID:");
                        rfid_hexdump(&oid, sizeof(oid));
                    }

                    set_uid(data, oid);
                    set_key(data, keyindex);

                    res = mifare_authenticate_block(data, sizeof(data), block);

/*
                    debug_printf("res:");
                    rfid_hexdump(&res, sizeof(res));

                    debug_printf("data:");
                    rfid_hexdump(&data[0], sizeof(data));
*/

                    if (0x41 == data[0] && 0x00 == data[1]) {
                        debug_printf("Auth Succeeded.\n");
                        tries = 0;

                        switch (*opmode) {
                            case READ:
                                res = mifare_read_block(data, sizeof(data), block);

                                if (res == 18) {
                                    debug_printf("Block:");
                                    rfid_hexdump(&block, sizeof(block));
                                    debug_printf("Data:");
                                    rfid_hexdump(&data[2], BLOCK_SIZE);
                                    debug_printf("Key:");
                                    rfid_hexdump(&default_keys[keyindex], MIFARE_KEY_SIZE);

                                    memcpy(&mifare_card[block*BLOCK_SIZE], &data[2], BLOCK_SIZE);
                                    if (0x00 == (block+1) % 4) {
                                        memcpy(&mifare_card[block*BLOCK_SIZE], &default_keys[keyindex], MIFARE_KEY_SIZE);
                                        memcpy(&mifare_card[block*BLOCK_SIZE+6], &access_bytes[0], ACCESS_BYTES);
                                        memcpy(&mifare_card[block*BLOCK_SIZE+10], &key_b[0], MIFARE_KEY_SIZE);
                                    }
                                }
                            break;
                            case WRITE:
                                memcpy(&data[4], &mifare_card[block*BLOCK_SIZE], BLOCK_SIZE);
                                res = mifare_write_block(data, sizeof(data), block);
                                debug_printf("res:");
                                rfid_hexdump(&res, sizeof(res));
                            break;
                        }
                        block += 1;
                    } else if (0x41 == data[0] && 0x14 == data[1]) {
                        debug_printf("Auth Failed.\n");
                        keyindex = (keyindex + 1) % KEYS;
                        tries += 1;
                    }
                }
            }
        } else {
            turn_rf_off(data, sizeof(data));
        }
    }
    *menu = LIBNFC;
}
Esempio n. 2
0
//**********************************************************************************************
//**********************************************************************************************
//**********************************************************************************************
int main(void)
{
	volatile unsigned char data, pos;

	WDTCTL = WDTPW + WDTHOLD;             // Stop watchdog timer


	//if (CALBC1_12MHZ==0xFF || CALDCO_12MHZ == 0xFF)
	 //   while(1);                               // If calibration constants erased do not load - trap CPU
	BCSCTL1 = CALBC1_12MHZ;	// Set DCO
	DCOCTL = CALDCO_12MHZ;


	//**********************************************
	//DO NOT CHANGE THE ORDER OF THE FOLLOWING CODE!
	port_init();

	initLCD();
	P4OUT |= BIT1; //LCD backlight on
	writecom(0x01, 0);//mode=0 instruction/command,    mode=1 data  clear display
	writecom(0x02, 0);//mode=0 instruction/command,    mode=1 data  position cursor home home
	write_string_to_LCD("DLP Design      ", 16);
	writecom(0xC0, 0);//mode=0 instruction/command,    mode=1 data  position cursor to start of second row
	write_string_to_LCD("DLP-RFID2 Demo  ", 16);

	keep_local=0;//if high, keep data returned to demo board (from reader) local instead of forwarding to host PC via USB.  if low, send to host PC.
	rx_index=0;//init the receive buffer index
	run_mode=0;//disable inventories
	blkaddr=0;
	currentantswstate = ANTSWINT;//init to internal

	/*	Code showing how to use J2, "SEL"  User Defined Jumper
	//pullup resistor enabled for P6.7 in port_init();
	data = P6IN;	    //read port 6
	data = data & 0x80;	//mask off other port bits
	if(data>0)          //if P6.7 is high (default)
	*/

	uart_init();

	setup_interrupts();
	//ping reader twice to sync and stop transmitting 'D's
	short_dly(50000);//pause for remainder of packet
	ping_reader();//ping for presence of reader and update display
	short_dly(50000);//pause for remainder of packet
	ping_reader();//ping for presence of reader and update display
	short_dly(50000);//pause for remainder of packet
	//**********************************************

	set_output_mode();


	for (;;)
	{
		if(light_pressed())
			P4OUT ^= 0x02; //toggle P4.1
		if(ping_pressed())
			ping_reader();//ping for presence of reader and update display
		if(antsw_pressed())
			toggle_antenna_switch();//toggle between internal and external antenna
		if(run_pressed())
			enter_run_mode();//setup for reading UIDs and set run flag for continuous inventories
		if(stop_pressed())
			exit_run_mode();//setup for reading UIDs and set run flag for continuous inventories
		if(rfoff_pressed())
			turn_rf_off();
		if(rdblk_pressed())
			read_block();

		if(run_mode==1)
			single_slot_inventory();
	}
}