123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676 |
- /*
- * Module_RFID.c
- *
- * Created on: 2019|~10?e28?e
- * Author: Eason Yang
- * Version: D0.01
- */
- #include "Module_RFID.h"
- //==================================
- // PRINT OUT LOG FORMAT
- //==================================
- #define SystemLogMessage
- #define DEBUG_INFO_1(format, args...) StoreLogMessage("[%s:%d][%s][Info] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
- #define DEBUG_WARN_1(format, args...) StoreLogMessage("[%s:%d][%s][Warn] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
- #define DEBUG_ERROR_1(format, args...) StoreLogMessage("[%s:%d][%s][Error] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
- //==================================
- // SYSTEM CONSTANT
- //==================================
- #define Debug
- #define ARRAY_SIZE(A) (sizeof(A) / sizeof(A[0]))
- #define PASS 1
- #define FAIL -1
- //==================================
- // RFID CMD CONSTANT
- //==================================
- unsigned int RFID_CMD_ISO1443A_REQUEST = 0x20;
- unsigned int RFID_CMD_ISO1443B_REQUEST = 0x60;
- unsigned int RFID_CMD_FELICA_POLLING_REQUEST = 0x2F;
- unsigned int RFID_CMD_HALT_14443A = 0x28;
- unsigned int RFID_CMD_BLOCK_READ = 0x21;
- unsigned int RFID_CMD_BLOCK_WRITE = 0X22;
- unsigned int RFID_CMD_BUZZER_SET = 0X14;
- //==================================
- // RFID MODE CONSTANT
- //==================================
- unsigned char WUPA = 0;
- unsigned char REQA = 1;
- unsigned char AFI = 0;
- //==================================
- // MIFARE CARD LENGTH
- //==================================
- unsigned int LENGTH_0 = 0;
- unsigned int LENGTH_4 = 4;
- unsigned int LENGTH_6 = 6;
- unsigned int LENGTH_7 = 7;
- unsigned int LENGTH_10 = 10;
- //==================================
- // MIFARE SECTOR SPACE
- //==================================
- #define ROW 6
- #define COLUMN 16
- unsigned char sectorKeyA[COLUMN][ROW] = { {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
- {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
- {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
- {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
- {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
- {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
- {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
- {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
- {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
- {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
- {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
- {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
- {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
- {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
- {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
- {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}
- };
- unsigned char serialNumber[32];
- unsigned int cardLength;
- //==================================
- // SystemLog message
- //==================================
- #ifdef SystemLogMessage
- int StoreLogMessage(const char *fmt, ...)
- {
- char Buf[4096 + 256];
- char buffer[4096];
- time_t CurrentTime;
- struct tm *tm;
- va_list args;
- va_start(args, fmt);
- int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
- va_end(args);
- memset(Buf, 0, sizeof(Buf));
- CurrentTime = time(NULL);
- tm = localtime(&CurrentTime);
- sprintf(Buf,
- "echo \"[%04d.%02d.%02d %02d:%02d:%02d] - %s\" >> /Storage/SystemLog/[%04d.%02d]RFID_SystemLog",
- tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour,
- tm->tm_min, tm->tm_sec, buffer, tm->tm_year + 1900, tm->tm_mon + 1);
- system(Buf);
- #ifdef Debug
- printf("[%04d.%02d.%02d %02d:%02d:%02d] - %s", tm->tm_year + 1900,
- tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec,
- buffer);
- #endif
- return rc;
- }
- #endif
- //==========================================
- // Module's command send/receive
- //==========================================
- int system_command(int uart, unsigned char* cmd,int length, unsigned char* rx)
- {
- int len;
- //sleep(2); //required to make flush work, for some reason
- tcflush(uart,TCIOFLUSH);
- if(write(uart, cmd, length) >= 0)
- {
- usleep(100000);
- len = read(uart, rx, 256);
- }
- else
- {
- #ifdef SystemLogMessage
- DEBUG_ERROR_1("system command %s response fail.\n", cmd);
- #endif
- }
- return len;
- }
- //==========================================
- // Calculation checksum function
- //==========================================
- char ClaCheckSum(unsigned char *buffer, int len)
- {
- int idx;
- char chksum = 0;
- for(idx = 0 ; idx < len-1 ; idx++)
- {
- chksum ^= *(buffer+idx);
- }
- return chksum;
- }
- bool getRequestCardSN(int Fd, int moduleType, RFID* rfid)
- {
- bool isSuccess = false;
- if(ISO14443A_REQUEST_SN(Fd,moduleType,serialNumber) == true)
- {
- rfid->cardType = ISO14443A;
- if(cardLength == LENGTH_4)
- {
- rfid->snType = RFID_SN_TYPE_4BYTE;
- memcpy(rfid->currentCard, &serialNumber, 32);
- }
- else if(cardLength == LENGTH_7)
- {
- rfid->snType = RFID_SN_TYPE_7BYTE;
- memcpy(rfid->currentCard, &serialNumber, 32);
- }
- isSuccess = true;
- }
- else if(ISO14443B_REQUEST_SN(Fd,moduleType,serialNumber) == true)
- {
- rfid->cardType = IS014443B;
- rfid->snType = RFID_SN_TYPE_4BYTE;
- memcpy(rfid->currentCard, &serialNumber, 32);
- sleep(2);
- isSuccess = true;
- }
- else if(FELICA_REQUEST_SN(Fd,moduleType,serialNumber) == true)
- {
- rfid->cardType = FELICA;
- rfid->snType = RFID_SN_TYPE_6BYTE;
- memcpy(rfid->currentCard, &serialNumber, 32);
- sleep(2);
- isSuccess = true;
- }
- else
- {}
- return isSuccess;
- }
- //==========================================
- // ISO14443A Request SN function
- //==========================================
- bool ISO14443A_REQUEST_SN(int Fd, int moduleType, unsigned char *data)
- {
- bool isSuccess = false;
- int tx_len = 4;
- unsigned char txByte[tx_len];
- unsigned char rxByte[254];
- unsigned char tmp[254];
- switch(moduleType)
- {
- case MODULE_EWT:
- default:
- //===============================
- // Command
- //===============================
- txByte[0] = tx_len-1;
- txByte[1] = RFID_CMD_ISO1443A_REQUEST;
- txByte[2] = REQA;
- txByte[ARRAY_SIZE(txByte)-1] = ClaCheckSum(txByte,ARRAY_SIZE(txByte));
- if(Fd > 0)
- {
- memset(rxByte, 0, sizeof (rxByte));
- if(system_command(Fd, txByte, tx_len, rxByte) > 0)
- {
- memset(tmp, 0, sizeof tmp);
- memcpy(tmp, rxByte, sizeof(rxByte));
- if(tmp[1] == RFID_CMD_ISO1443A_REQUEST)
- {
- if(tmp[0] == 0x09)
- {
- #ifdef SystemLogMessage
- DEBUG_INFO_1("MIFARE CLASSIC.\n");
- #endif
- cardLength = LENGTH_4;
- memcpy(data, rxByte+2, cardLength);
- isSuccess = true;
- }
- else if(tmp[0] == 0x0C)
- {
- #ifdef SystemLogMessage
- DEBUG_INFO_1("MIFARE PLUS.\n");
- #endif
- cardLength = LENGTH_7;
- memcpy(data, rxByte+2, cardLength);
- isSuccess = true;
- }
- else
- {}
- }
- else if (tmp[1] == 0xDF)
- {
- cardLength = LENGTH_0;
- isSuccess = false;
- }
- }
- else
- {}
- }
- else
- {}
- break;
- }
- return isSuccess;
- }
- //==========================================
- // ISO14443B Request SN function
- //==========================================
- bool ISO14443B_REQUEST_SN(int Fd, int moduleType, unsigned char *data)
- {
- bool isSuccess = false;
- int cardLength;
- int tx_len = 5;
- unsigned char txByte[tx_len];
- unsigned char rxByte[254];
- unsigned char tmp[254];
- switch(moduleType)
- {
- case MODULE_EWT:
- default:
- //===============================
- // Command
- //===============================
- txByte[0] = tx_len-1;
- txByte[1] = RFID_CMD_ISO1443B_REQUEST;
- txByte[2] = WUPA;
- txByte[3] = AFI;
- txByte[ARRAY_SIZE(txByte)-1] = ClaCheckSum(txByte,ARRAY_SIZE(txByte));
- if(Fd > 0)
- {
- memset(rxByte, 0, sizeof (rxByte));
- if(system_command(Fd, txByte, tx_len, rxByte) > 0)
- {
- memset(tmp, 0, sizeof tmp);
- memcpy(tmp, rxByte, sizeof(rxByte));
- if(tmp[1] == RFID_CMD_ISO1443B_REQUEST)
- {
- #ifdef SystemLogMessage
- DEBUG_INFO_1("ISO14443 TYPE B.\n");
- #endif
- cardLength = LENGTH_4;
- memcpy(data, rxByte+3, cardLength);
- isSuccess = true;
- }
- else if(tmp[1] == 0x9F)
- {
- cardLength = LENGTH_0;
- isSuccess = false;
- }
- }
- else
- {}
- }
- else
- {}
- break;
- }
- return isSuccess;
- }
- //==========================================
- // FELICA Request SN function
- //==========================================
- bool FELICA_REQUEST_SN(int Fd, int moduleType, unsigned char *data)
- {
- bool isSuccess = false;
- int cardLength;
- int tx_len = 9;
- unsigned char txByte[tx_len];
- unsigned char rxByte[254];
- unsigned char tmp[254];
- switch(moduleType)
- {
- case MODULE_EWT:
- default:
- //===============================
- // Command
- //===============================
- txByte[0] = tx_len-1;
- txByte[1] = RFID_CMD_FELICA_POLLING_REQUEST;
- txByte[2] = 0x06;
- txByte[3] = 0x00;
- txByte[4] = 0xFF;
- txByte[5] = 0xFF;
- txByte[6] = 0x01;
- txByte[7] = 0x00;
- txByte[ARRAY_SIZE(txByte)-1] = ClaCheckSum(txByte,ARRAY_SIZE(txByte));
- if(Fd > 0)
- {
- memset(rxByte, 0, sizeof (rxByte));
- if(system_command(Fd, txByte, tx_len, rxByte) > 0)
- {
- memset(tmp, 0, sizeof tmp);
- memcpy(tmp, rxByte, sizeof(rxByte));
- if(tmp[1] == RFID_CMD_FELICA_POLLING_REQUEST)
- {
- #ifdef SystemLogMessage
- DEBUG_INFO_1("FELICA.\n");
- #endif
- cardLength = LENGTH_6;
- memcpy(data, rxByte+6, cardLength);
- isSuccess = true;
- }
- else if(tmp[1] == 0xD0)
- {
- cardLength = LENGTH_0;
- isSuccess = false;
- }
- }
- else
- {}
- }
- else
- {}
- break;
- }
- return isSuccess;
- }
- //==========================================
- // Read block data from RFID card
- //==========================================
- bool getBlockRead(int Fd, int moduleType, int block, unsigned char keyId, unsigned char *data)
- {
- bool isSuccess = false;
- int i;
- int j = 0;
- int len = 11;
- unsigned char txByte[len];
- unsigned char rxByte[254];
- unsigned char tmp[254];
- switch(moduleType)
- {
- case MODULE_EWT:
- default:
- //===============================
- // Command
- //===============================
- txByte[0] = 0x0A;
- txByte[1] = RFID_CMD_BLOCK_READ;
- txByte[2] = keyId;
- txByte[3] = block;
- for(i = 4; i < 10; i++)
- {
- if( j < sizeof (sectorKeyA[0]))
- {
- txByte[i] = sectorKeyA[(int)(block/4)][j];
- j++;
- }
- }
- txByte[ARRAY_SIZE(txByte)-1] = ClaCheckSum(txByte,ARRAY_SIZE(txByte));
- if(Fd > 0)
- {
- memset(rxByte, 0, sizeof(rxByte));
- if(system_command(Fd, txByte, len, rxByte) > 0)
- {
- memset(tmp, 0, sizeof tmp);
- memcpy(tmp, rxByte, sizeof (rxByte));
- if((tmp[1] == RFID_CMD_BLOCK_READ))
- {
- #ifdef SystemLogMessage
- DEBUG_INFO_1("Read block ok.\n");
- #endif
- memcpy(data, rxByte+2, 16);
- isSuccess = true;
- }
- else if (tmp[1] == 0xDE)
- {
- #ifdef SystemLogMessage
- DEBUG_INFO_1("Read block failed.\n");
- #endif
- }
- else
- {}
- }
- else
- {}
- }
- else
- {}
- break;
- }
- return isSuccess;
- }
- //==========================================
- // Write data into RFID card block
- //==========================================
- bool setBlockWrite(int Fd, int moduleType, int block, unsigned char keyid, unsigned char *data)
- {
- bool isSuccess = false;
- int i;
- int j = 0;
- int len = 27;
- unsigned char txByte[len];
- unsigned char rxByte[254];
- unsigned char tmp[254];
- switch(moduleType)
- {
- case MODULE_EWT:
- default:
- //===============================
- // Command
- //===============================
- txByte[0] = 0x1A;
- txByte[1] = RFID_CMD_BLOCK_WRITE;
- txByte[2] = keyid;
- txByte[3] = block;
- for(i = 4; i < 10; i++)
- {
- if( j < sizeof (sectorKeyA[0]))
- {
- txByte[i] = sectorKeyA[(int)(block/4)][j];
- j++;
- }
- }
- memcpy(txByte+10, data, 16);
- txByte[ARRAY_SIZE(txByte)-1] = ClaCheckSum(txByte,(ARRAY_SIZE(txByte)));
- if(Fd > 0)
- {
- memset(rxByte, 0, sizeof(rxByte));
- if(system_command(Fd, txByte, len, rxByte) > 0)
- {
- memset(tmp, 0, sizeof tmp);
- memcpy(tmp, rxByte, sizeof (rxByte));
- if(tmp[1] == RFID_CMD_BLOCK_WRITE)
- {
- #ifdef SystemLogMessage
- DEBUG_INFO_1("Write block ok.\n");
- #endif
- isSuccess = true;
- }
- else if(tmp[1] == 0xDD)
- {
- #ifdef SystemLogMessage
- DEBUG_INFO_1("Write block failed.\n");
- #endif
- isSuccess = false;
- }
- }
- else
- {}
- }
- else
- {}
- break;
- }
- return isSuccess;
- }
- //==========================================
- // Buzzer set (EWT Module)
- //==========================================
- void setBuzzer(int Fd, int moduleType, unsigned char time)
- {
- int len = 4;
- unsigned char txByte[len];
- unsigned char rxByte[254];
- unsigned char tmp[254];
- switch(moduleType)
- {
- case MODULE_EWT:
- default:
- //===============================
- // Command
- //===============================
- txByte[0] = 0x03;
- txByte[1] = RFID_CMD_BUZZER_SET;
- if(time > 0x0A)
- {
- time = 0x05;
- txByte[2] = time;
- #ifdef SystemLogMessage
- DEBUG_WARN_1("Value is out of range.\n");
- #endif
- }
- else
- {
- txByte[2] = time;
- }
- txByte[ARRAY_SIZE(txByte)-1] = ClaCheckSum(txByte,ARRAY_SIZE(txByte));
- if(Fd > 0)
- {
- memset(rxByte, 0, sizeof(rxByte));
- if(system_command(Fd, txByte, len, rxByte) > 0)
- {
- memset(tmp, 0, sizeof tmp);
- memcpy(tmp, rxByte, sizeof (rxByte));
- if(tmp[1] == 0x14)
- {
- #ifdef SystemLogMessage
- DEBUG_INFO_1("Set Buzzer ok.\n");
- DEBUG_INFO_1("Set Buzzer %d ms.\n",time);
- #endif
- }
- else if(tmp[1] == 0xEC)
- {
- #ifdef SystemLogMessage
- DEBUG_INFO_1("Set Buzzer fail.\n");
- #endif
- }
- }
- else
- {}
- }
- else
- {}
- break;
- }
- }
- //==========================================
- // Halt RFID card (EWT)
- //==========================================
- void sethaltCard(int Fd, int moduleType)
- {
- int len = 3;
- unsigned char txByte[len];
- unsigned char rxByte[254];
- unsigned char tmp[254];
- switch(moduleType)
- {
- case MODULE_EWT:
- default:
- //===============================
- // Command
- //===============================
- txByte[0] = 0x02;
- txByte[1] = RFID_CMD_HALT_14443A;
- txByte[ARRAY_SIZE(txByte)-1] = ClaCheckSum(txByte,ARRAY_SIZE(txByte));
- if(Fd > 0)
- {
- memset(rxByte, 0, sizeof(rxByte));
- if(system_command(Fd, txByte, len, rxByte) > 0)
- {
- memset(tmp, 0, sizeof tmp);
- memcpy(tmp, rxByte, sizeof (rxByte));
- if(tmp[1] == 0x28)
- {
- #ifdef SystemLogMessage
- DEBUG_INFO_1("Halt card pass.\n");
- #endif
- }
- else if(tmp[1] == 0xD7)
- {
- #ifdef SystemLogMessage
- DEBUG_INFO_1("Halt card fail.\n");
- #endif
- }
- else
- {}
- }
- else
- {}
- }
- else
- {}
- break;
- }
- }
|