rfid_driver_wrapper.cpp
Go to the documentation of this file.
00001 
00026 #include "maggie_rfid_drivers/rfid_driver_wrapper.h"
00027 
00029 
00030 RfidDriverWrapper::RfidDriverWrapper()
00031 {
00032     _snr = "0";
00033     _DSFID = "0";
00034     _num_devices = 0;
00035     _device_handle = 0;
00036     _reader_handle = 0;
00037     _lenght = 0;
00038     _id_device = 0;
00039     _num_max_labels = 0;
00040 
00041     // clean previous list of devices, if exists
00042     FEUSB_ClearScanList();
00043 }
00044 
00046 
00047 RfidDriverWrapper::~RfidDriverWrapper()
00048 {
00049 }
00050 
00052 
00053 int RfidDriverWrapper::open_device(long num_device)
00054 {
00055     char cDeviceID[16];
00056     char cError[256];
00057     char* pEnd;
00058     bool encontrado = false;
00059     char cPara[16] = "Device-ID";
00060     int error = 0;
00061 
00062     // scan everything and return the rumber of devices found
00063     int index = FEUSB_Scan(FEUSB_SCAN_ALL, NULL);
00064     if (index < 0) {
00065         FEUSB_GetErrorText(index, cError);
00066         ROS_ERROR("[RFID_DRIVER] Error: %s", cError);
00067     }
00068     else {
00069         _num_devices = FEUSB_GetScanListSize();
00070 
00071         if (_num_devices < 0) {
00072             ROS_ERROR("[RFID_DRIVER] Error: opening USB devices");
00073         }
00074         else {
00075             for (int i = 0; i < _num_devices; i++) {
00076                 if (FEUSB_GetScanListPara(i, cPara, cDeviceID) == 0) {
00077                     // convert the ID from decimal to hexadecimal
00078                     _id_device = strtol(cDeviceID, &pEnd, 16);
00079 
00080                     if (num_device == _id_device) {
00081                         encontrado = true;
00082                         _device_handle = FEUSB_OpenDevice(_id_device);
00083 
00084                         if (_device_handle < 0) {
00085                             ROS_ERROR("[RFID_DRIVER] Error: opening USB devices");
00086                             error = -1;
00087                         }
00088                         else {
00089                             set_type_reader(i);
00090 
00091                             _reader_handle = FEISC_NewReader(_device_handle);
00092 
00093                             if (_reader_handle < 0) {
00094                                 ROS_ERROR("[RFID_DRIVER] Error: opening USB devices");
00095                                 error = -1;
00096                             }
00097                             else {
00098                                 set_port_hand(num_device);
00099                                 _rfid_reader.SetReaderHnd(_reader_handle);
00100                                 ROS_DEBUG("[RFID_DRIVER] Device %ld enabled", num_device);
00101                                 i = _num_devices;
00102                             }
00103                         }
00104                     }
00105                 }
00106             }
00107         }
00108     }
00109 
00110     if (!encontrado) {
00111         error = -1;
00112     }
00113 
00114     return error;
00115 }
00116 
00118 
00119 int RfidDriverWrapper::close_device()
00120 {
00121     int error = 0;
00122 
00123     if (_reader_handle < 0) {
00124         error = -1;
00125     }
00126     else {
00127         FEISC_DeleteReader(_reader_handle);
00128         if (_device_handle < 0) {
00129             error = -1;
00130         }
00131         else {
00132             int iErr = FEUSB_CloseDevice(_device_handle);
00133             if (iErr == 0) {
00134                 ROS_DEBUG("[RFID_DRIVER] USB device %d closed", iErr);
00135             }
00136             else {
00137                 ROS_ERROR("[RFID_DRIVER] Error: closing the device");
00138                 error = -1;
00139             }
00140         }
00141     }
00142 
00143     return error;
00144 }
00145 
00147 
00148 std::string RfidDriverWrapper::get_uid_label()
00149 {
00150     return _snr;
00151 }
00152 
00154 
00155 std::string* RfidDriverWrapper::get_tags()
00156 {
00157     std::string* detected = &_labels[0];
00158 
00159     return detected;
00160 }
00161 
00163 
00164 std::string RfidDriverWrapper::get_type_reader()
00165 {
00166     std::string result = "";
00167 
00168     if (strcmp(_device_name.c_str(), HF_NAME) == 0) {
00169         result = HF_NAME;
00170     }
00171 
00172     return result;
00173 }
00174 
00176 
00177 int RfidDriverWrapper::get_num_labels()
00178 {
00179     return _lenght;
00180 }
00181 
00183 
00184 void RfidDriverWrapper::set_num_labels(int num_labels)
00185 {
00186     _num_max_labels = num_labels;
00187 }
00188 
00190 
00191 void RfidDriverWrapper::read_hf(unsigned char data[BLOCK_SIZE_HF], unsigned char uc_db_address)
00192 {
00193     // buffer for a block of data (block size: 4 bytes)
00194     unsigned char ucDB[BLOCK_SIZE_HF];
00195 
00196     unsigned char ucBlockSize;
00197     int iErr, iIdx, i;
00198     char cError[256];
00199 
00200     // fix the serial number (UID) of the label
00201     _rfid_reader.SetData(FEDM_ISC_TMP_B0_REQ_UID, _snr);
00202     // command to write data in the label
00203     _rfid_reader.SetData(FEDM_ISC_TMP_B0_CMD, (unsigned char) 0x23);
00204     _rfid_reader.SetData(FEDM_ISC_TMP_B0_MODE, (unsigned char) 0x00);
00205     // directional mode
00206     _rfid_reader.SetData(FEDM_ISC_TMP_B0_MODE_ADR, (unsigned char) 0x01);
00207     // fix the direction of the block
00208     _rfid_reader.SetData(FEDM_ISC_TMP_B0_REQ_DB_ADR, uc_db_address);
00209     // write 1 block of data (size 4 bytes)
00210     _rfid_reader.SetData(FEDM_ISC_TMP_B0_REQ_DBN, (unsigned char) 0x01);
00211 
00212     // communication
00213     iErr = _rfid_reader.SendProtocol(0xB0);
00214     _rfid_reader.GetErrorText(cError, iErr);
00215 
00216     // find the position in the table of the label with UID = _snr
00217     iIdx = _rfid_reader.FindTableIndex(0, FEDM_ISC_ISO_TABLE, FEDM_ISC_DATA_SNR, _snr);
00218 
00219     // get the size of the block of data, for HF_NAME is 4 bytes
00220     _rfid_reader.GetTableData(iIdx, FEDM_ISC_ISO_TABLE, FEDM_ISC_DATA_BLOCK_SIZE, &ucBlockSize);
00221 
00222     // get a block of data
00223     _rfid_reader.GetTableData(iIdx, FEDM_ISC_ISO_TABLE, FEDM_ISC_DATA_RxDB, uc_db_address, ucDB, ucBlockSize);
00224 
00225     // get a block of data
00226     for (i = 0; i < BLOCK_SIZE_HF; i++) {
00227         data[i] = ucDB[i];
00228     }
00229 }
00230 
00232 
00233 void RfidDriverWrapper::write_hf(unsigned char data[BLOCK_SIZE_HF], unsigned char uc_db_address, std::string uid)
00234 {
00235     _snr = uid;
00236     char cError[256];
00237 
00238     // find the position in the table of the label with UID = _snr
00239     int iIdx = _rfid_reader.FindTableIndex(0, FEDM_ISC_ISO_TABLE, FEDM_ISC_DATA_SNR, _snr);
00240 
00241     // fix the serial number (UID) of the label
00242     _rfid_reader.SetData(FEDM_ISC_TMP_B0_REQ_UID, _snr);
00243     // command to write data in the label
00244     _rfid_reader.SetData(FEDM_ISC_TMP_B0_CMD, (unsigned char) 0x24);
00245     _rfid_reader.SetData(FEDM_ISC_TMP_B0_MODE, (unsigned char) 0x00);
00246     // directional mode
00247     _rfid_reader.SetData(FEDM_ISC_TMP_B0_MODE_ADR, (unsigned char) 0x01);
00248     // write 6 blocks of data (size 2 bytes) fix the direction of the block
00249     _rfid_reader.SetData(FEDM_ISC_TMP_B0_REQ_DB_ADR, uc_db_address);
00250     // write 6 blocks of data (size 4 bytes)
00251     _rfid_reader.SetData(FEDM_ISC_TMP_B0_REQ_DBN, (unsigned char) 0x01);
00252     // size of the block for the tags HF_NAME: 4
00253     _rfid_reader.SetTableData(iIdx, FEDM_ISC_ISO_TABLE, FEDM_ISC_DATA_BLOCK_SIZE, (unsigned char) BLOCK_SIZE_HF);
00254     // write the blocks of data in the table
00255     _rfid_reader.SetTableData(iIdx, FEDM_ISC_ISO_TABLE, FEDM_ISC_DATA_TxDB, uc_db_address, data, BLOCK_SIZE_HF);
00256 
00257     // communication
00258     int iErr = _rfid_reader.SendProtocol(0xB0);
00259     _rfid_reader.GetErrorText(cError, iErr);
00260 }
00261 
00263 
00264 int RfidDriverWrapper::write_tag_epc(unsigned char* data, std::string epc, unsigned int mem_bank, unsigned int address,
00265                                      unsigned int num_blocks)
00266 {
00267     _snr = epc;
00268     FedmIscTagHandler* pTagHandler = NULL;
00269 
00270     pTagHandler = _rfid_reader.GetTagHandler(_snr);
00271     FedmIscTagHandler_EPC_Class1_Gen2* pEPC = (FedmIscTagHandler_EPC_Class1_Gen2*) pTagHandler;
00272     int iErr = pEPC->WriteMultipleBlocks(mem_bank, address, num_blocks, "", data);
00273     ROS_ERROR("[RFID_DRIVER] iErr %d", iErr);
00274 
00275     return iErr;
00276 }
00277 
00279 
00280 int RfidDriverWrapper::inventory()
00281 {
00282     char cError[256];
00283     int i;
00284     unsigned char ucTrType = 0;
00285 
00286     // clean the vector of labels
00287     _labels.clear();
00288 
00289     // indicate the maximum number of labels to detect
00290     _rfid_reader.SetTableSize(FEDM_ISC_ISO_TABLE, _num_max_labels);
00291 
00292     _rfid_reader.ResetTable(FEDM_ISC_ISO_TABLE, FEDM_ISC_DATA_ALL);
00293 
00294     // inventory
00295     _rfid_reader.SetData(FEDM_ISC_TMP_B0_CMD, (unsigned char) 0x01);
00296     _rfid_reader.SetData(FEDM_ISC_TMP_B0_MODE, (unsigned char) 0x00);
00297     int iErr = _rfid_reader.SendProtocol(0xB0);
00298 
00299     if (iErr < 0) {
00300         _rfid_reader.GetErrorText(cError, iErr);
00301         ROS_ERROR("[RFID_DRIVER] Error: %d in inventory. %s", iErr, cError);
00302         return iErr;
00303     }
00304 
00305     _lenght = _rfid_reader.GetTableLength(FEDM_ISC_ISO_TABLE);
00306     if (_lenght == 0) {
00307         // reader: no transponder in reader field
00308         FEISC_GetLastState(_rfid_reader.GetReaderHnd(), cError);
00309 
00310         return _lenght;
00311     }
00312 
00313     for (i = 0; i < _lenght; i++) {
00314         _rfid_reader.GetTableData(i, FEDM_ISC_ISO_TABLE, FEDM_ISC_DATA_TRTYPE, &ucTrType);
00315 
00316         // 0x03:  ISO15693
00317         // 0x84:  EPC Class1 Gen2
00318         _rfid_reader.GetTableData(i, FEDM_ISC_ISO_TABLE, FEDM_ISC_DATA_SNR, _snr);
00319         _labels.push_back(_snr);
00320     }
00321 
00322     return _lenght;
00323 }
00324 
00326 
00327 int RfidDriverWrapper::set_type_reader(int index)
00328 {
00329     std::string name = "DeviceName";
00330     int error = 0;
00331 
00332     if (FEUSB_GetScanListPara(index, (char*) name.c_str(), (char*) _device_name.c_str()) == 0) {
00333         if (strcmp(_device_name.c_str(), HF_NAME) == 0) {
00334             _rfid_reader.SetReaderType(FEDM_ISC_TYPE_ISCMR101_U);
00335         }
00336         else {
00337             ROS_ERROR("[RFID_DRIVER] Error: unknown RFID reader type");
00338             error = -1;
00339         }
00340     }
00341     else {
00342         error = -1;
00343     }
00344 
00345     return error;
00346 }
00347 
00349 
00350 int RfidDriverWrapper::set_port_hand(long num_device)
00351 {
00352     int error = 0;
00353 
00354     if (num_device == HEAD_READER_ID) {
00355         _rfid_reader.SetPortHnd(HEAD_READER_PORT);
00356     }
00357     else if (num_device == BASE_READER_ID) {
00358         _rfid_reader.SetPortHnd(BASE_READER_PORT);
00359     }
00360     else {
00361         ROS_DEBUG("[RFID_DRIVER] Port by default");
00362         error = -1;
00363     }
00364 
00365     return error;
00366 }
00367 


maggie_rfid_drivers
Author(s): Raul Perula-Martinez
autogenerated on Mon Sep 14 2015 03:05:31