123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846 |
- #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
- //==================================
- // MODULE CONSTANT
- //==================================
- #define NO_MODULE -1
- #define MODULE_EWT 0
- #define MODULE_ELATEC 1
- //==================================
- // ACTION CONSTANT
- //==================================
- #define SEARCH 1
- #define READ 2
- #define WRITE 3
- //==================================
- // RFID MODE CONSTANT
- //==================================
- unsigned char WUPA = 0;
- unsigned char REQA = 1;
- unsigned int STATUS = 0;
- //==================================
- // 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}
- };
- //==================================
- // RFID CMD CONSTANT
- //==================================
- unsigned int RFID_CMD_REQUEST_SN = 0x20;
- 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;
- //==================================
- // 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]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
- //=================================
- // Common routine
- //=================================
- char* getTimeString(void)
- {
- char *result=malloc(21);
- time_t timep;
- struct tm *p;
- time(&timep);
- p=gmtime(&timep);
- sprintf(result, "[%04d-%02d-%02d %02d:%02d:%02d]", (1900+p->tm_year), (1+p->tm_mon), p->tm_mday, p->tm_hour, p->tm_hour, p->tm_sec);
- return result;
- }
- //==========================================
- // 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(500000);
- 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;
- }
- //==========================================
- // Read RFID card's serial number
- //==========================================
- bool getRequestCardSN(int Fd, int moduleType, unsigned char *serialNumber)
- {
- bool isSuccess = false;
- int cardLength;
- 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_REQUEST_SN;
- txByte[2] = REQA;
- txByte[ARRAY_SIZE(txByte)-1] = ClaCheckSum(txByte,ARRAY_SIZE(txByte));
- if(Fd > 0)
- {
- memset(rxByte, 0, sizeof (rxByte));
- // Send commands
- if(system_command(Fd, txByte, len, rxByte) > 0)
- {
- memset(tmp, 0, sizeof tmp);
- memcpy(tmp, rxByte, sizeof(rxByte));
- if(tmp[1] == RFID_CMD_REQUEST_SN)
- {
- if(tmp[0] == 0x09)
- {
- #ifdef SystemLogMessage
- DEBUG_INFO_1("Card type : MIFARE classic.\n");
- #endif
- cardLength = 4;
- *(serialNumber) = cardLength;
- memcpy(serialNumber+1, rxByte+2, cardLength);
- isSuccess = true;
- }
- else if(tmp[0] == 0x0C)
- {
- #ifdef SystemLogMessage
- DEBUG_INFO_1("Card type : Plus, UltraLight or DESFire.\n");
- #endif
- cardLength = 7;
- *(serialNumber) = cardLength;
- memcpy(serialNumber+1, rxByte+2, cardLength);
- isSuccess = true;
- }
- else
- {
- #ifdef SystemLogMessage
- DEBUG_INFO_1("Please try again.\n");
- #endif
- isSuccess = false;
- }
- }
- else if (tmp[1] == 0xDF)
- {
- cardLength = 0;
- isSuccess = false;
- }
- }
- else
- {}
- }
- else
- {}
- break;
- case MODULE_ELATEC:
- 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[19];
- unsigned char tmp[19];
- 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));
- // Send commands
- 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;
- case MODULE_ELATEC:
- 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[3];
- unsigned char tmp[3];
- 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));
- // Send commands
- 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;
- case MODULE_ELATEC:
- 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[3];
- unsigned char tmp[3];
- 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));
- // Send commands
- 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;
- case MODULE_ELATEC:
- break;
- }
- }
- //==========================================
- // Halt RFID card (EWT)
- //==========================================
- void sethaltCard(int Fd, int moduleType)
- {
- int len = 3;
- unsigned char txByte[len];
- unsigned char rxByte[3];
- unsigned char tmp[3];
- 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));
- // Send commands
- 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;
- case MODULE_ELATEC:
- break;
- }
- }
- //==========================================
- // Searchtag function (ELATEC Module)
- //==========================================
- bool searchTag(int Fd, int moduleType, unsigned char *serialNumber, int status)
- {
- bool isSuccess = false;
- int cardLength;
- int len = 5;
- unsigned char txByte[len];
- unsigned char rxByte[254];
- unsigned char tmp[254];
- // Command
- txByte[0] = 0x03;
- txByte[1] = 0x00;
- txByte[2] = 0x05;
- txByte[3] = 0x00;
- txByte[4] = 0x10;
- // Open port
- if(Fd > 0)
- {
- memset(rxByte, 0, sizeof (rxByte));
- // Send commands
- if(system_command(Fd, txByte, len, rxByte) > 0)
- {
- memset(tmp, 0, sizeof tmp);
- memcpy(tmp, rxByte, sizeof(rxByte));
- if(tmp[3] == 0x01)
- {
- if(tmp[6] == 0x04)
- {
- if(status == SEARCH)
- {
- cardLength = 4;
- *(serialNumber) = cardLength;
- memcpy(serialNumber+1, rxByte+7, cardLength);
- }
- #ifdef SystemLogMessage
- DEBUG_INFO_1("Card type: MIFARE classic.\n");
- #endif
- }
- else if (tmp[6] == 0x07)
- {
- cardLength = 7;
- *(serialNumber) = cardLength;
- memcpy(serialNumber+1, rxByte+7, cardLength);
- #ifdef SystemLogMessage
- DEBUG_INFO_1("Card type: Plus, UltraLight or DESFire.\n");
- #endif
- }
- isSuccess = true;
- }
- else if(tmp[3] == 0x00)
- {
- cardLength = 0;
- isSuccess = false;
- }
- }
- else
- {}
- }
- else
- {}
- return isSuccess;
- }
- //==========================================
- // MifareClassic_Login function (ELATEC Module)
- //==========================================
- bool MifareClassic_Login(int Fd, int moduleType, int block, unsigned char keyid)
- {
- bool isSuccess = false;
- int i;
- int j = 0;
- int len = 12;
- unsigned char txByte[len];
- unsigned char rxByte[254];
- unsigned char tmp[254];
- // Command
- txByte[0] = 0x0A;
- txByte[1] = 0x00;
- txByte[2] = 0x0B;
- txByte[3] = 0x00;
- for(i = 4; i < 10; i++)
- {
- if( j < sizeof (sectorKeyA[0]))
- {
- txByte[i] = sectorKeyA[(int)(block/4)][j];
- j++;
- }
- }
- txByte[10] = keyid;
- txByte[11] = block/4;
- if(Fd > 0)
- {
- memset(rxByte, 0, sizeof (rxByte));
- // Send commands
- if(system_command(Fd, txByte, len, rxByte) > 0)
- {
- memset(tmp, 0, sizeof tmp);
- memcpy(tmp, rxByte, sizeof(rxByte));
- if(tmp[3] == 0x01)
- {
- #ifdef SystemLogMessage
- DEBUG_INFO_1("MIFARE classic login pass.\n");
- #endif
- isSuccess = true;
- }
- else if(tmp[3] == 0x00)
- {
- #ifdef SystemLogMessage
- DEBUG_INFO_1("MIFARE classic login fail.\n");
- #endif
- isSuccess = false;
- }
- else
- {
- #ifdef SystemLogMessage
- DEBUG_WARN_1("Please try again.\n");
- #endif
- isSuccess = false;
- }
- }
- else
- {}
- }
- else
- {}
- return isSuccess;
- }
- //==========================================
- // MifareClassic_ReadBlock (ELATEC Module)
- //==========================================
- bool MifareClassic_ReadBlock(int Fd, int moduleType, int block,unsigned char *data)
- {
- bool isSuccess = false;
- int len = 5;
- unsigned char txByte[len];
- unsigned char rxByte[254];
- unsigned char tmp[254];
- // Command
- txByte[0] = 0x03;
- txByte[1] = 0x00;
- txByte[2] = 0x0B;
- txByte[3] = 0x01;
- txByte[4] = block;
- if(Fd > 0)
- {
- memset(rxByte, 0, sizeof (rxByte));
- // Send commands
- if(system_command(Fd, txByte, len, rxByte) > 0)
- {
- memset(tmp, 0, sizeof tmp);
- memcpy(tmp, rxByte, sizeof(rxByte));
- if(tmp[3] == 0x01)
- {
- #ifdef SystemLogMessage
- DEBUG_INFO_1("Read block successfully.\n");
- #endif
- memcpy(data, rxByte+4, 16);
- isSuccess = true;
- }
- else if(tmp[3] == 0x00)
- {
- #ifdef SystemLogMessage
- DEBUG_INFO_1("Unsuccessful to read block.\n");
- #endif
- isSuccess = false;
- }
- else
- {
- #ifdef SystemLogMessage
- DEBUG_WARN_1("Please try again.\n");
- #endif
- isSuccess = false;
- }
- }
- else
- {}
- }
- else
- {}
- return isSuccess;
- }
- //==========================================
- // MifareClassic_WriteBlock (ELATEC Module)
- //==========================================
- bool MifareClassic_WriteBlock(int Fd, int moduleType, int block, unsigned char *data)
- {
- bool isSuccess = false;
- int len = 21;
- unsigned char txByte[len];
- unsigned char rxByte[254];
- unsigned char tmp[254];
- // Command
- txByte[0] = 0x13;
- txByte[1] = 0x00;
- txByte[2] = 0x0B;
- txByte[3] = 0x02;
- txByte[4] = block;
- memcpy(txByte+5, data, 16);
- if(Fd > 0)
- {
- memset(rxByte, 0, sizeof (rxByte));
- // Send command
- if(system_command(Fd, txByte, len, rxByte) > 0)
- {
- memset(tmp, 0, sizeof tmp);
- memcpy(tmp, rxByte, sizeof(rxByte));
- if(tmp[3] == 0x01)
- {
- #ifdef SystemLogMessage
- DEBUG_INFO_1("Write block successfully.\n");
- #endif
- memcpy(data, rxByte+4, 16);
- isSuccess = true;
- }
- else if(tmp[3] == 0x00)
- {
- #ifdef SystemLogMessage
- DEBUG_INFO_1("Unsuccessful to Write block.\n");
- #endif
- isSuccess = false;
- }
- else
- {
- #ifdef SystemLogMessage
- DEBUG_WARN_1("Please try again.\n");
- #endif
- isSuccess = false;
- }
- }
- else
- {}
- }
- else
- {}
- return isSuccess;
- }
- //==========================================
- // Beep (ELATEC Module)
- //==========================================
- bool SetBeep(int Fd, int moduleType, unsigned char sound)
- {
- bool isSuccess = false;
- int len = 11;
- unsigned char txByte[len];
- unsigned char rxByte[254];
- unsigned char tmp[254];
- // Command
- txByte[0] = 0x09;
- txByte[1] = 0x00;
- txByte[2] = 0x04;
- txByte[3] = 0x07;
- txByte[4] = sound;
- txByte[5] = 0x60;
- txByte[6] = 0x09;
- txByte[7] = 0xF4;
- txByte[8] = 0x01;
- txByte[9] = 0xF4;
- txByte[10] = 0x01;
- if(Fd > 0)
- {
- memset(rxByte, 0, sizeof(rxByte));
- // Send command
- if(system_command(Fd, txByte, len, rxByte) > 0)
- {
- memset(tmp, 0, sizeof tmp);
- memcpy(tmp, rxByte, sizeof(rxByte));
- isSuccess = true;
- }
- else
- {
- isSuccess = true;
- }
- }
- else
- {}
- return isSuccess;
- }
|