11#include "keyple/plugin/stub/StubSmartCard.hpp"
13#include "keyple/core/plugin/CardIOException.hpp"
14#include "keyple/core/util/HexUtil.hpp"
15#include "keyple/core/util/cpp/KeypleStd.hpp"
16#include "keyple/core/util/cpp/Pattern.hpp"
22using keyple::core::plugin::CardIOException;
23using keyple::core::util::HexUtil;
24using keyple::core::util::cpp::Pattern;
32StubSmartCard::SimulatedCommandStep&
33StubSmartCard::Builder::withSimulatedCommand(
34 const std::string& command,
const std::string& response)
37 std::string cmd = command;
38 std::string resp = response;
40 mHexCommands.insert({std::trim(cmd), std::trim(resp)});
45std::shared_ptr<StubSmartCard>
46StubSmartCard::Builder::build()
48 return std::shared_ptr<StubSmartCard>(
new StubSmartCard(
49 mPowerOnData, mCardProtocol, mHexCommands, mApduResponseProvider));
52StubSmartCard::ProtocolStep&
53StubSmartCard::Builder::withPowerOnData(
const std::vector<uint8_t>& powerOnData)
55 mPowerOnData = powerOnData;
60StubSmartCard::CommandStep&
61StubSmartCard::Builder::withProtocol(
const std::string& protocol)
63 mCardProtocol = protocol;
68StubSmartCard::BuildStep&
69StubSmartCard::Builder::withApduResponseProvider(
70 const std::shared_ptr<ApduResponseProviderSpi> apduResponseProvider)
72 mApduResponseProvider = apduResponseProvider;
80StubSmartCard::getCardProtocol()
const
85const std::vector<uint8_t>&
86StubSmartCard::getPowerOnData()
const
92StubSmartCard::isPhysicalChannelOpen()
const
94 return mIsPhysicalChannelOpen;
98StubSmartCard::openPhysicalChannel()
100 mIsPhysicalChannelOpen =
true;
104StubSmartCard::closePhysicalChannel()
106 mIsPhysicalChannelOpen =
false;
109const std::vector<uint8_t>
110StubSmartCard::processApdu(
const std::vector<uint8_t>& apduIn)
112 if (apduIn.size() == 0) {
113 return std::vector<uint8_t>();
117 const std::string hexApdu = HexUtil::toHex(apduIn);
119 if (mApduResponseProvider !=
nullptr) {
120 const std::string responseFromRequest
121 = mApduResponseProvider->getResponseFromRequest(hexApdu);
122 if (responseFromRequest !=
"") {
123 return HexUtil::toByteArray(responseFromRequest);
126 }
else if (!mHexCommands.empty()) {
129 for (
const auto& hexCommand : mHexCommands) {
130 std::shared_ptr<Pattern> p = Pattern::compile(hexCommand.first);
131 if (p->matcher(hexApdu)->matches()) {
132 return HexUtil::toByteArray(hexCommand.second);
138 throw CardIOException(
"No response is available for request: " + hexApdu);
142operator<<(std::ostream& os,
const std::shared_ptr<StubSmartCard> ssc)
144 os <<
"STUB_SMART_CARD: {"
145 <<
"POWER_ON_DATA = " << HexUtil::toHex(ssc->mPowerOnData) <<
", "
146 <<
"CARD_PROTOCOL = " << ssc->mCardProtocol <<
", "
147 <<
"IS_PHYSICAL_CHANNEL_OPEN = " << ssc->mIsPhysicalChannelOpen <<
", "
148 <<
"HEX_COMMANDS(#) = " << ssc->mHexCommands.size() <<
"}";
153std::unique_ptr<StubSmartCard::PowerOnDataStep>
154StubSmartCard::builder()
156 return std::unique_ptr<Builder>(
new Builder());
159StubSmartCard::StubSmartCard(
160 const std::vector<uint8_t>& powerOnData,
161 const std::string& cardProtocol,
162 const std::map<std::string, std::string>& hexCommands,
163 const std::shared_ptr<ApduResponseProviderSpi> apduResponseProvider)
164: mPowerOnData(powerOnData)
165, mCardProtocol(cardProtocol)
166, mIsPhysicalChannelOpen(false)
167, mHexCommands(hexCommands)
168, mApduResponseProvider(apduResponseProvider)
std::ostream & operator<<(std::ostream &os, const std::shared_ptr< StubSmartCard > ssc)
StubPluginFactoryBuilder::Builder Builder