#include #include bool openPort(); bool closePort(); bool Halt(); bool SeekAndAuthentication(int blockNum); bool ReadBlock(unsigned char* data, int block); bool WriteBlock(unsigned char* data, int block); bool writeBTData(unsigned char* payload, unsigned int length); int blockNumberIncrement(unsigned int num); HANDLE mifareHandle; void main(void) { DWORD size; openPort(); //--- change baud rate --- // set to 57600 bps /* unsigned char baud[] = {0xFF,0x00,0x02,0x94,0x03,0x99}; if( !WriteFile(mifareHandle, baud, 0x6, &size, NULL) ) { printf("error: write block output\n"); return; } Sleep(10); unsigned char res1[6]; for( int i=0; i<6; i++ ) { res1[i] = 0x00; } if( !ReadFile(mifareHandle, res1, 6, &size, NULL) ) { printf("error: write block input"); return; } return; */ printf("--- read a block ---\n"); unsigned char res[16]; ReadBlock( res, 5 ); /* for( int i=0; i<16; i++ ) { printf("%02X ", res[i]); } printf("\n"); */ printf("--- write long data ---\n"); unsigned char data[] = "0123456789ABCDEF0123456789abcdef0123456789ABCDEF"; // 48 byte writeBTData(data,48); closePort(); return; } // end of main() bool WriteBlock(unsigned char* data, int blockNum) { unsigned char packet[22]; DWORD size; int count; if( !Halt() ) { return 0; } if( !SeekAndAuthentication(blockNum) ) { return 0; } // time out roop /* count = 0; while(1) { if( Halt() ) { if( SeekAndAuthentication(blockNum) ) { break; } } count++; if( count > 30 ) { printf("%d write timeout.\n", blockNum); return 0; } } */ packet[0] = 0xFF; packet[1] = 0x00; packet[2] = 0x12; // packet length packet[3] = 0x89; // command packet[4] = blockNum; packet[21] = 0x12 + 0x89 + blockNum; // check sum; for(int i=0; i<16; i++) { // writing data packet[5+i] = data[i]; packet[21] += data[i]; } //--- if( !WriteFile(mifareHandle, packet, 0x22, &size, NULL) ) { printf("write block error\n"); return false; } Sleep(30); unsigned char res[22]; for( int i=0; i<22; i++ ) { res[i] = 0x00; } if( !ReadFile(mifareHandle, res, 22, &size, NULL) ) { printf("error: can not get WriteBlock response"); return false; } /* if( res[2]==2 ) { printf("write error"); return false; } return true; } /* * read targaet block. */ bool ReadBlock(unsigned char* data, int blockNum) { unsigned char packet[6]; DWORD size; if( !Halt() ) { return 0; } if( !SeekAndAuthentication(blockNum) ) { return 0; } packet[0] = 0xFF; packet[1] = 0x00; packet[2] = 0x02; packet[3] = 0x86; packet[4] = blockNum; packet[5] = 0x02 + 0x86 + blockNum; //--- if( !WriteFile(mifareHandle, packet, 6, &size, NULL) ) { printf("read block error\n"); return false; } Sleep(30); unsigned char res[22]; for( int i=0; i<22; i++ ) { res[i] = 0x00; } if( !ReadFile(mifareHandle, res, 22, &size, NULL) ) { printf("error: can not get ReadBlock response"); return false; } if( res[2]==2 ) { printf("error: could not read block %d\n", blockNum); return false; } for(int i=0; i<16; i++) { data[i] = res[i+5]; } return true; } // ReadBlock() /* * seek tag, and authentication. */ bool SeekAndAuthentication(int blockNum) { unsigned char packet[13]; DWORD size; //-- seek packet[0] = 0xFF; packet[1] = 0x00; packet[2] = 0x01; packet[3] = 0x82; packet[4] = 0x83; if( !WriteFile(mifareHandle, packet, 5, &size, NULL) ) { printf("seek error\n"); return false; } //Sleep(10); unsigned char res[10]; for( int i=0; i<6; i++ ) { res[i] = 0x00; } if( !ReadFile(mifareHandle, res, 6, &size, NULL) ) { printf("error: can not get Seek4Tag response1\n"); return false; } Sleep(30); for( int i=0; i<10; i++ ) { res[i] = 0x00; } if( !ReadFile(mifareHandle, res, 10, &size, NULL) ) { printf("error: can not get Seek4Tag response2\n"); return false; } //-- authentication if( size==10 ) { packet[2] = 0x09; packet[3] = 0x85; packet[4] = blockNum; packet[5] = 0xAA; for( int i=0; i<3; i++ ) { packet[i*2 +6] = 0xD3; packet[i*2+1+6] = 0xF7; } packet[12] = 0x00; // check sum for( int i=2; i<12; i++ ) { packet[12] += packet[i]; } //packet[12] = 0x9A; if( !WriteFile(mifareHandle, packet, 13, &size, NULL) ) { printf("authenticate error\n"); return false; } Sleep(30); for( int i=0; i<6; i++ ) { res[i] = 0x00; } if( !ReadFile(mifareHandle, res, 6, &size, NULL) ) { printf("error: can not get Authenticate response\n"); return false; } /* for( int i=0; i<6; i++ ) { printf("%02x ", res[i]); } printf("\n"); */ } else { printf("authentication error at %d block\n", blockNum); return false; } return true; } // seek and authenticate /** * Halt command */ bool Halt() { unsigned char packet[5]; packet[0] = 0xFF; packet[1] = 0x00; packet[2] = 0x01; packet[3] = 0x93; packet[4] = 0x94; DWORD size; if( !WriteFile(mifareHandle, packet, 5, &size, NULL) ) { printf("halt error\n"); return false; } Sleep(30); unsigned char res[6]; for( int i=0; i<6; i++ ) { res[i] = 0x00; } if( !ReadFile(mifareHandle, res, 6, &size, NULL) ) { printf("error: can not get Halt response\n"); return false; } return true; } // Halt() /*************************************************************** * write wide-byte data to Mifare 4k tag, by NDEF format * (NDEF ID is constant) ***************************************************************/ bool writeWideData(unsigned char* payload, unsigned int length) { const unsigned char NDEF_ID[] = "SML"; const unsigned int NDEF_ID_LENGTH = 3; unsigned char tmpData[16]; unsigned int i; unsigned int num; // total packet length unsigned int blockNum; // target block number num = 0; blockNum = 4; // free area of ultra light card // NDEF tmpData[0] = 0x03; tmpData[1] = 2 + 2 + NDEF_ID_LENGTH + length; tmpData[2] = 0xDD; tmpData[3] = 0x00; tmpData[4] = length; tmpData[5] = NDEF_ID_LENGTH; num = 6; //--- copy NDEF ID --- for(i=0; i