14#include "keyple/plugin/pcsc/cpp/CardChannel.hpp"
18#include "keyple/core/util/cpp/KeypleStd.hpp"
19#include "keyple/core/util/cpp/exception/IllegalArgumentException.hpp"
20#include "keyple/plugin/pcsc/cpp/Card.hpp"
21#include "keyple/plugin/pcsc/cpp/exception/CardException.hpp"
28using keyple::core::util::cpp::exception::IllegalArgumentException;
29using keyple::plugin::pcsc::cpp::exception::CardException;
31CardChannel::CardChannel(
const std::shared_ptr<Card> card,
const int channel)
40CardChannel::getCard()
const
46CardChannel::transmit(
const std::vector<uint8_t>& apduIn)
48 if (apduIn.size() == 0)
49 throw IllegalArgumentException(
"command cannot be empty");
52 std::vector<uint8_t> _apduIn = apduIn;
55 bool t0GetResponse =
true;
56 bool t1GetResponse =
true;
62 int n =
static_cast<int>(_apduIn.size());
63 bool t0 = mCard->mProtocol == SCARD_PROTOCOL_T0;
64 bool t1 = mCard->mProtocol == SCARD_PROTOCOL_T1;
66 if (t0 && (n >= 7) && (_apduIn[4] == 0))
67 throw CardException(
"Extended len. not supported for T=0");
69 if ((t0 || t1) && (n >= 7)) {
70 int lc = _apduIn[4] & 0xff;
76 lc = ((_apduIn[5] & 0xff) << 8) | (_apduIn[6] & 0xff);
83 bool getresponse = (t0 && t0GetResponse) || (t1 && t1GetResponse);
85 std::vector<uint8_t> result;
89 throw CardException(
"Could not obtain response");
93 DWORD dwRecv =
sizeof(r_apdu);
96 mLogger->debug(
"transmitApdu - c-apdu >> %\n", _apduIn);
101 (LPCBYTE)_apduIn.data(),
102 static_cast<DWORD
>(_apduIn.size()),
106 if (rv != SCARD_S_SUCCESS) {
108 "SCardTransmit failed with error: %\n",
109 std::string(pcsc_stringify_error(rv)));
112 throw CardException(
"ScardTransmit failed (CARD)");
114 throw CardException(
"ScardTransmit failed (READER)");
118 std::vector<uint8_t> response(r_apdu, r_apdu + dwRecv);
120 mLogger->debug(
"transmitApdu - r-apdu << %\n", response);
122 int rn =
static_cast<int>(response.size());
123 if (getresponse && (rn >= 2)) {
125 if ((rn == 2) && (response[0] == 0x6c)) {
127 _apduIn[n - 1] = response[1];
131 if (response[rn - 2] == 0x61) {
138 response.begin() + rn - 2);
140 std::vector<uint8_t> getResponse(5);
141 getResponse[0] = _apduIn[0];
142 getResponse[1] = 0xC0;
145 getResponse[4] = response[rn - 1];
147 _apduIn.swap(getResponse);
152 result.insert(result.end(), response.begin(), response.begin() + rn);
bool isCardCommunicationError(uint64_t rv)