Sending APDUs

This commit is contained in:
Ondrej Mikle 2014-07-21 17:29:45 +02:00
parent 703c214421
commit 6ae565373a
3 changed files with 127 additions and 1 deletions

View file

@ -9,9 +9,23 @@
using namespace std;
ResponseAPDU::ResponseAPDU(const string &data)
{
size_t len = data.size();
_valid = len >= 2;
if (!_valid) {
return;
}
_sw = (uint8_t(data[len-2]) << 8) | uint8_t(data[len-1]);
_data = data.substr(0, len-2);
}
NFCDevice::NFCDevice() throw(NFCError):
pollNr(20),
pollPeriod(2),
apduTimeout(500),
_nfcContext(NULL),
_nfcDevice(NULL),
_opened(false),
@ -105,6 +119,36 @@ std::string NFCDevice::scanUID() throw(NFCError)
return uid;
}
void NFCDevice::selectPassiveTarget() throw(NFCError)
{
nfc_target nt;
while (nfc_initiator_select_passive_target(_nfcDevice, _modulations[0], NULL, 0, &nt) <= 0);
}
ResponseAPDU NFCDevice::sendAPDU(const string &apdu) throw(NFCError)
{
int res;
uint8_t rapdu[512];
if ((res = nfc_initiator_transceive_bytes(_nfcDevice, (uint8_t*)apdu.data(), apdu.size(),
rapdu, 512, apduTimeout)) < 0) {
if (res == NFC_EOVFLOW) {
throw NFCError("Response APDU too long");
}
throw NFCError("Failed to transceive APDU");
} else {
string rapduData((char *)rapdu, res);
ResponseAPDU responseApdu(rapduData);
if (!responseApdu.valid()) {
throw NFCError("Invalid response APDU was received");
}
return responseApdu;
}
}
const nfc_modulation NFCDevice::_modulations[5] = {
{ /*.nmt = */ NMT_ISO14443A, /* .nbr = */ NBR_106 }
//{ /*.nmt = */ NMT_ISO14443B, /* .nbr = */ NBR_106 },