📄 simureader.cpp
字号:
/* ============================================================================ Project Name : jayaCard Module Name : inkit - IP Reader Version : $Id: SimuReader.cpp,v 1.33 2003/11/28 22:46:27 dgil Exp $ Description: Connects to an simulator or remote reader using sockets/=============================================================================== The Original Code is inKit code, the contactless library of the jayacard project (http://www.jayacard.org). The contents of this file are subject to the Mozilla Public License Version 1.1 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.mozilla.org/MPL/ Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the specific language governing rights and limitations under the License. The Initial Developer of the Original Code is inSeal SAS and the authors Gilles Dumortier and Philippe Fremy. Portions created by the Initial Developer are Copyright (C) 1996-2003 the Initial Developer. All Rights Reserved. Alternatively, the contents of this file may be used under the terms of either the GNU General Public License Version 2 or later (the "GPL"), or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), in which case the provisions of the GPL or the LGPL are applicable instead of those above. If you wish to allow use of your version of this file only under the terms of either the GPL or the LGPL, and not to allow others to use your version of this file under the terms of the MPL, indicate your decision by deleting the provisions above and replace them with the notice and other provisions required by the GPL or the LGPL. If you do not delete the provisions above, a recipient may use your version of this file under the terms of any one of the MPL, the GPL or the LGPL. inKit is also available under commercial license. For pricing and ordering information, send an email to sales@inseal.com History Rev Description 020403 phf wrote it from scratch ============================================================================*/#include "common/portability.h"//#include <strstream.h>#include <sstream>#include "SimuReader.h"#include "inkit/jaya_reader/ContactlessBinder.h"#include "inkit/jaya_reader/SimuReader/SimuReaderFactory.h"namespace Jayacard {/* ========================================================================= */void * SimuReader::operator new( size_t tSize ){ return malloc( tSize );}void SimuReader::operator delete( void *p ){ free(p);}SimuReader::SimuReader( jresult * result, string type, string configString ) : ContactlessReader( result, type ){ if (type == SIMU_READER_TYPEB) { setComProtocol( ProtocolTypeB ); } else { setComProtocol( ProtocolTypeA ); } _capabilities.maxBaudrate = speed_848; _socket = 0L; init_socket(); *result = open( configString );}jresult SimuReader::open( string configString ){ parseConfigString( configString ); _lastResult = connect_socket( (char *) _host.c_str(), _port, &_socket ); if (_lastResult != JY_OK) { jlog("SimuReader", "Unable to connect socket: %s", jerror_msg( _lastResult ) ); } jlog_disable_component( "SIMUMSG" ); return _lastResult;}SimuReader::~SimuReader(){ jlog("destructors", "SimuReader ~destructor()" ); close();}void SimuReader::_close(){ jlog("destructors", "SimuReader _close()" ); _msg = new_simumsg( SIMUMSG_CLOSE, SIMUMSG_CLOSE_AND_WAIT); _lastResult = send_simumsg( _socket, _msg ); free_simumsg( _msg ); close_socket( _socket ); }void SimuReader::parseConfigString( string configString ){ string::size_type pos; _host = "localhost"; _port = 2000; if (configString.empty()) return; pos = configString.find(":"); if (pos == string::npos ) { _host = configString; } else { _host = configString.substr( 0, pos ); _port = atoi( configString.substr(pos+1).c_str() ); } return;}bool SimuReader::isComProtocolSupported( ComProtocol protocol ){ // only supports type A at the beginning if (_type == SIMU_READER_TYPEA && protocol == ProtocolTypeA) { return true; } if (_type == SIMU_READER_TYPEB && protocol == ProtocolTypeB) { return true; } if (_type == SIMU_READER) { if (protocol == ProtocolTypeA) return true; if (protocol == ProtocolTypeB) return true; } return false;}void SimuReader::_setComProtocol( ComProtocol protocol ){ // nothing special to do}jresult SimuReader::_setTimeout( long timeoutUs ){ // nothing special to do now // Future: set the timeout on the socket ? return JY_OK;}jresult SimuReader::_rfOn(){ jlog( "SimuReader", "rfOn on interface %d", _comProtocol ); _msg = new_simumsg( SIMUMSG_POWERON, (jbyte) _comProtocol ); _lastResult = send_simumsg( _socket, _msg ); free_simumsg( _msg ); return _lastResult;}jresult SimuReader::_rfOff(){ _msg = new_simumsg( SIMUMSG_POWEROFF, 0 ); _lastResult = send_simumsg( _socket, _msg ); free_simumsg( _msg ); return _lastResult;}jresult SimuReader::_exchangeBlock( Block sendBlock, Block & recvBlock ){ _msg = new_simumsg( SIMUMSG_TCL_FRAME, 0 ); _msg->buf = sendBlock.bytes(); _msg->h.param = sendBlock.len(); _lastResult = send_simumsg( _socket, _msg ); _msg->buf = NULL; _msg->h.param = 0; free_simumsg( _msg ); if (_lastResult != JY_OK) { jlog("SimuReader", "exchangeBlock: send_simu_msg returns '%s'", jerror_msg( _lastResult ) ); return _lastResult; } _lastResult = receive_simumsg( _socket, &_msg ); if (_lastResult != JY_OK) { jlog("SimuReader", "exchangeBlock, receive_simumsg returns: '%s'", jerror_msg( _lastResult ) ); return _lastResult; } if (_msg->h.kindof != SIMUMSG_TCL_FRAME) { jlog("SimuReader", "exchangeBlock, expecting TCL_BLOCK, got '%s'", kindof2str( _msg->h.kindof ) ); return _lastResult = JY_PROTOCOL_ERROR; } recvBlock.set( _msg->buf, _msg->h.param ); return _lastResult;}jresult SimuReader::getSerialNumber(string & serialNumber){ ostringstream buf; buf << _type << " (" << _host << ":" << _port << ")"; serialNumber = buf.str(); return _lastResult = JY_OK;}jresult SimuReader::_setBaudrate( ComBaudrate toCard, ComBaudrate fromCard ){ int param; param = (toCard << 4) + fromCard; jlog("SimuReader", "_setBaudrate : param = %02X", param ); _msg = new_simumsg( SIMUMSG_BAUDRATE, param ); _lastResult = send_simumsg( _socket, _msg ); free_simumsg( _msg ); if (_lastResult != JY_OK) { jlog("SimuReader", "_setBaudrate: send_simu_msg returns '%s'", jerror_msg( _lastResult ) ); return _lastResult; } return JY_OK;}// =================================================================//// TypeA Specific //// =================================================================jresult SimuReader::_typeAWakupAll( jword * atqa ){ return typeARequestA( atqa, 0x52 );}jresult SimuReader::typeARequestIdle(jword * atqa ){ return typeARequestA( atqa, 0x26 );}jresult SimuReader::typeARequestA( jword * atqa, jbyte short_frame ){ _msg = new_simumsg( SIMUMSG_TCL_FRAME, 3 ); _msg->buf[0] = short_frame; // add CRC because our simulator likes TCL_FRAME with CRC ContactlessBinder::addCrcA( _msg->buf, 1 ); _lastResult = send_simumsg( _socket, _msg ); free_simumsg( _msg ); if (_lastResult != JY_OK) { jlog("SimuReader", "can not send REQA : %s", jerror_msg( _lastResult ) ); return _lastResult; } _lastResult = receive_simumsg( _socket, &_msg ); if (_lastResult != JY_OK) { jlog("SimuReader", "receiving ATQA: '%s'", jerror_msg( _lastResult ) ); free_simumsg( _msg ); return _lastResult; } if (_msg->h.kindof != SIMUMSG_TCL_FRAME) { jlog("SimuReader", "expecting ATQA, receive: '%s'", kindof2str( _msg->h.kindof ) ); free_simumsg( _msg ); return _lastResult = JY_PROTOCOL_ERROR; } *atqa = _msg->buf[0] + (_msg->buf[1] << 8); free_simumsg( _msg ); return _lastResult = JY_OK;}jresult SimuReader::_typeASelect( bool wupAll, jword * atqa, jbyte * uid, int * uidLen, jbyte * sak ){ if (wupAll) { typeAWakupAll( atqa ); } else { typeARequestIdle( atqa ); } if (_lastResult != JY_OK) return _lastResult; // send ANTICOL jlog("SimuReader", "send ANTICOL" ); _msg = new_simumsg( SIMUMSG_TCL_FRAME, 4 ); _msg->buf[0] = 0x93; _msg->buf[1] = 0x20; ContactlessBinder::addCrcA( _msg->buf, 2 ); _lastResult = send_simumsg( _socket, _msg ); free_simumsg( _msg ); if (_lastResult != JY_OK) { jlog("SimuReader", "can not send ANTICOL : %s", jerror_msg( _lastResult ) ); return _lastResult; } jlog("SimuReader", "recv UUID" ); // recv UUID _lastResult = receive_simumsg( _socket, &_msg ); if (_lastResult != JY_OK) { jlog("SimuReader", "receive UUID returns: '%s'", jerror_msg( _lastResult ) ); return _lastResult; } if (_msg->h.kindof != SIMUMSG_TCL_FRAME) { jlog("SimuReader", "expecting UUID, receive : '%s'", kindof2str( _msg->h.kindof ) ); return _lastResult = JY_PROTOCOL_ERROR; } *uidLen = _msg->h.param-2; memcpy( uid, _msg->buf, *uidLen ); free_simumsg( _msg ); jlog("SimuReader", "UUID=%s", hex2str( uid, *uidLen) ); jlog("SimuReader", "Send Select" ); // send Select _msg = new_simumsg( SIMUMSG_TCL_FRAME, 4 + *uidLen ); _msg->buf[0] = 0x93; _msg->buf[1] = 0x70; memcpy( _msg->buf+2, uid, *uidLen ); ContactlessBinder::addCrcA( _msg->buf, *uidLen + 2 ); _lastResult = send_simumsg( _socket, _msg ); free_simumsg( _msg ); if (_lastResult != JY_OK) { jlog("SimuReader", "can not send Select : %s", jerror_msg( _lastResult ) ); return _lastResult; } jlog("SimuReader", "Receive SAK" ); // recv SAK _lastResult = receive_simumsg( _socket, &_msg ); if (_lastResult != JY_OK) { jlog("SimuReader", "receive SAK returns: '%s'", jerror_msg( _lastResult ) ); return _lastResult; } if (_msg->h.kindof != SIMUMSG_TCL_FRAME) { jlog("SimuReader", "expecting SAK, got : '%s'", kindof2str( _msg->h.kindof ) ); return _lastResult = JY_PROTOCOL_ERROR; } sak[0] = _msg->buf[0]; free_simumsg( _msg ); jlog("SimuReader", "SAK = %.2X", sak[0] ); return _lastResult = JY_OK;}jresult SimuReader::_typeAHalt(){ _msg = new_simumsg( SIMUMSG_TCL_FRAME, 4 ); ContactlessBinder::buildHltaBlock( _msg->buf ); _lastResult = send_simumsg( _socket, _msg ); free_simumsg( _msg ); return _lastResult;}// =================================================================//// TypeB Specific //// =================================================================// nothing to reimplement, all methods use _exchangeBlock// =================================================================//// Other//// =================================================================string SimuReader::_extraCommand( string cmd, string arg ){ const char * msg; jbyte param = 0; jbyte interf = 0x01; /* Contactless TypeA */ if (cmd.find(' ') != string::npos) { param = atoi( cmd.c_str() + cmd.find(' ') ); cmd[ cmd.find(' ') ] = '\0'; } msg = cmd.c_str(); SIMUMSG* pmsg; jresult res; if (strcmp(msg,"POWERON")==0) { pmsg = new_simumsg(SIMUMSG_POWERON,interf);send: res = send_simumsg(_socket,pmsg); free_simumsg(pmsg); return "Ok"; } else if (strcmp(msg,"POWEROFF")==0) { pmsg = new_simumsg(SIMUMSG_POWEROFF,interf); goto send; } else if (strcmp(msg,"STOP_SIMULATOR")==0) { pmsg = new_simumsg(SIMUMSG_CLOSE, SIMUMSG_CLOSE_AND_WAIT); goto send; } else if (strcmp(msg,"EXIT_SIMULATOR")==0) { pmsg = new_simumsg(SIMUMSG_CLOSE, SIMUMSG_CLOSE_AND_EXIT); goto send; } else if (strcmp(msg,"UNLOCK_BOOTSTRAP")==0) { pmsg = new_simumsg(SIMUMSG_UNLOCK,0x00); goto send; } else if (strcmp(msg,"UNLOCK_INIT")==0) { pmsg = new_simumsg(SIMUMSG_UNLOCK,0x01); goto send; } else if (strcmp(msg,"UNLOCK_PERSO")==0) { pmsg = new_simumsg(SIMUMSG_UNLOCK,0x02); goto send; } else if (strcmp(msg,"UNLOCK_BLOCKED")==0) { /* unlock the BLOCKED lock */ pmsg = new_simumsg(SIMUMSG_UNLOCK,0x03); goto send; } else if (strcmp(msg,"SAVE_EEPROM")==0) { /* save the eeprom content to file now */ pmsg = new_simumsg(SIMUMSG_SAVE_EEPROM,0x00); pmsg->h.param = param; goto send; } else if (strcmp(msg,"LOAD_EEPROM")==0) { /* load the eeprom content from file now */ pmsg = new_simumsg(SIMUMSG_LOAD_EEPROM,0x00); pmsg->h.param = param; goto send; } else if (strcmp(msg,"CLEAR_EEPROM")==0) { /* clear the eeprom content now */ pmsg = new_simumsg(SIMUMSG_CLEAR_EEPROM,0x00); goto send; } /* otherwise */ return "";}// ======================================================================} // namespace Jayacard
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -