Keyple Plugin Stub C++ Library - 2.2.2
Component of the Keyple C++ middleware
StubReaderAdapter.cpp
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (c) 2025 Calypso Networks Association https://calypsonet.org/ *
3 * *
4 * This program and the accompanying materials are made available under the *
5 * terms of the MIT License which is available at *
6 * https://opensource.org/licenses/MIT. *
7 * *
8 * SPDX-License-Identifier: MIT *
9 ******************************************************************************/
10
11#include "keyple/plugin/stub/StubReaderAdapter.hpp"
12
13#include "keyple/core/plugin/CardIOException.hpp"
14#include "keyple/core/plugin/TaskCanceledException.hpp"
15#include "keyple/core/util/HexUtil.hpp"
16#include "keyple/core/util/KeypleAssert.hpp"
17#include "keyple/core/util/cpp/Any.hpp"
18#include "keyple/core/util/cpp/Arrays.hpp"
19#include "keyple/core/util/cpp/Thread.hpp"
20#include "keyple/core/util/cpp/exception/InterruptedException.hpp"
21
22namespace keyple {
23namespace plugin {
24namespace stub {
25
26using keyple::core::plugin::CardIOException;
27using keyple::core::plugin::TaskCanceledException;
28using keyple::core::util::Assert;
29using keyple::core::util::HexUtil;
30using keyple::core::util::cpp::any;
31using keyple::core::util::cpp::Arrays;
32using keyple::core::util::cpp::Thread;
33using keyple::core::util::cpp::exception::InterruptedException;
34
35StubReaderAdapter::StubReaderAdapter(
36 const std::string& name,
37 const bool isContactLess,
38 std::shared_ptr<StubSmartCard> card)
39: mName(name)
40, mIsContactLess(isContactLess)
41, mSmartCard(card)
42, mContinueWaitForCardRemovalTask(false)
43{
44}
45
46void
47StubReaderAdapter::onStartDetection()
48{
49 // NOP
50}
51
52void
53StubReaderAdapter::onStopDetection()
54{
55 // NOP
56}
57
58const std::string&
59StubReaderAdapter::getName() const
60{
61 return mName;
62}
63
64bool
65StubReaderAdapter::isProtocolSupported(const std::string& readerProtocol) const
66{
67 (void)readerProtocol;
68
69 /* Do not block any protocol */
70 return true;
71}
72
73void
74StubReaderAdapter::activateProtocol(const std::string& readerProtocol)
75{
76 mActivatedProtocols.push_back(readerProtocol);
77}
78
79void
80StubReaderAdapter::deactivateProtocol(const std::string& readerProtocol)
81{
82 const auto it = std::find(
83 mActivatedProtocols.begin(), mActivatedProtocols.end(), readerProtocol);
84 if (it != mActivatedProtocols.end()) {
85 mActivatedProtocols.erase(it);
86 }
87}
88
89bool
90StubReaderAdapter::isCurrentProtocol(const std::string& readerProtocol) const
91{
92 if (mSmartCard != nullptr && mSmartCard->getCardProtocol() != "") {
93 return mSmartCard->getCardProtocol() == readerProtocol;
94 } else {
95 return false;
96 }
97}
98
99void
100StubReaderAdapter::openPhysicalChannel()
101{
102 if (mSmartCard != nullptr) {
103 mSmartCard->openPhysicalChannel();
104 }
105}
106
107void
108StubReaderAdapter::closePhysicalChannel()
109{
110 if (mSmartCard != nullptr) {
111 mSmartCard->closePhysicalChannel();
112 }
113}
114
115bool
116StubReaderAdapter::isPhysicalChannelOpen() const
117{
118 return mSmartCard != nullptr && mSmartCard->isPhysicalChannelOpen();
119}
120
121bool
122StubReaderAdapter::checkCardPresence()
123{
124 return mSmartCard != nullptr;
125}
126
127const std::string
128StubReaderAdapter::getPowerOnData() const
129{
130 return HexUtil::toHex(mSmartCard->getPowerOnData());
131}
132
133const std::vector<uint8_t>
134StubReaderAdapter::transmitApdu(const std::vector<uint8_t>& apduIn)
135{
136 if (mSmartCard == nullptr) {
137 throw CardIOException("No card is available");
138 }
139
140 return mSmartCard->processApdu(apduIn);
141}
142
143bool
144StubReaderAdapter::isContactless()
145{
146 return mIsContactLess;
147}
148
149void
150StubReaderAdapter::onUnregister()
151{
152 /* NO-OP */
153}
154
155void
156StubReaderAdapter::insertCard(std::shared_ptr<StubSmartCard> smartCard)
157{
158 Assert::getInstance().notNull(smartCard, "smart card");
159
160 if (checkCardPresence()) {
161 mLogger->warn(
162 "[readerExt=%] A card is already inserted. First remove the " \
163 "inserted card before inserting a new one\n",
164 getName());
165 return;
166 }
167
168 const std::string protocol = smartCard->getCardProtocol();
169 if (!Arrays::contains(mActivatedProtocols, protocol)) {
170 mLogger->trace(
171 "[readerExt=%] Inserted card protocol does not match any " \
172 "activated protocol. Please use 'activateProtocol()' method first" \
173 "[cardProtocol=%]\n",
174 getName(),
175 protocol);
176
177 return;
178 }
179
180 mLogger->trace(
181 "[readerExt=%] Card inserted [smartCard=%]\n", getName(), smartCard);
182
183 mSmartCard = smartCard;
184}
185
186void
187StubReaderAdapter::removeCard()
188{
189 if (mSmartCard != nullptr) {
190 mLogger->trace(
191 "[readerExt=%] Card removed [smartCard=%]\n",
192 getName(),
193 mSmartCard);
194 closePhysicalChannel();
195 mSmartCard = nullptr;
196 }
197}
198
199std::shared_ptr<StubSmartCard>
200StubReaderAdapter::getSmartcard()
201{
202 return mSmartCard;
203}
204
205std::shared_ptr<any>
206StubReaderAdapter::getSelectedSmartCard() const
207{
208 return nullptr;
209}
210
211int
212StubReaderAdapter::getCardInsertionMonitoringSleepDuration() const
213{
214 return 0;
215}
216
217int
218StubReaderAdapter::getCardRemovalMonitoringSleepDuration() const
219{
220 return 0;
221}
222
223} /* namespace stub */
224} /* namespace plugin */
225} /* namespace keyple */