Keyple Card Calypso C++ Library - 3.2.2
Component of the Keyple C++ middleware
FreeTransactionManagerAdapter.cpp
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (c) 2025 Calypso Networks Association https://calypsonet.org/ *
3 * *
4 * See the NOTICE file(s) distributed with this work for additional *
5 * information regarding copyright ownership. *
6 * *
7 * This program and the accompanying materials are made available under the *
8 * terms of the Eclipse Public License 2.0 which is available at *
9 * http://www.eclipse.org/legal/epl-2.0 *
10 * *
11 * SPDX-License-Identifier: EPL-2.0 *
12 ******************************************************************************/
13
14#include "keyple/card/calypso/FreeTransactionManagerAdapter.hpp"
15
16#include <memory>
17#include <string>
18#include <utility>
19#include <vector>
20
21#include "keyple/card/calypso/CalypsoCardAdapter.hpp"
22#include "keyple/card/calypso/CalypsoCardConstant.hpp"
23#include "keyple/card/calypso/CommandChangePin.hpp"
24#include "keyple/card/calypso/CommandVerifyPin.hpp"
25#include "keyple/core/plugin/CardIOException.hpp"
26#include "keyple/core/util/KeypleAssert.hpp"
27#include "keyple/core/util/cpp/exception/RuntimeException.hpp"
28#include "keyple/core/util/cpp/exception/UnsupportedOperationException.hpp"
29#include "keypop/calypso/card/transaction/UnexpectedCommandStatusException.hpp"
30#include "keypop/calypso/crypto/legacysam/transaction/ReaderIOException.hpp"
31#include "keypop/reader/CardCommunicationException.hpp"
32#include "keypop/reader/ReaderCommunicationException.hpp"
33#include "keypop/reader/selection/InvalidCardResponseException.hpp"
34
35namespace keyple {
36namespace card {
37namespace calypso {
38
39using keyple::core::plugin::CardIOException;
40using keyple::core::util::Assert;
41using keyple::core::util::cpp::exception::RuntimeException;
42using keyple::core::util::cpp::exception::UnsupportedOperationException;
43using keypop::calypso::card::transaction::UnexpectedCommandStatusException;
44using keypop::calypso::crypto::legacysam::transaction::ReaderIOException;
45using keypop::reader::CardCommunicationException;
46using keypop::reader::ReaderCommunicationException;
47using keypop::reader::selection::InvalidCardResponseException;
48
49const std::string FreeTransactionManagerAdapter::MSG_PIN_NOT_AVAILABLE
50 = "PIN is not available for this card";
51
52FreeTransactionManagerAdapter::FreeTransactionManagerAdapter(
53 std::shared_ptr<ProxyReaderApi> cardReader,
54 std::shared_ptr<CalypsoCardAdapter> card)
55: TransactionManagerAdapter<FreeTransactionManager>(cardReader, card)
56, mTransactionContext(
57 std::make_shared<DtoAdapters::TransactionContextDto>(card))
58, mCommandContext(
59 std::make_shared<DtoAdapters::CommandContextDto>(false, false))
60{
61}
62
63std::shared_ptr<DtoAdapters::TransactionContextDto>
64FreeTransactionManagerAdapter::getTransactionContext() const
65{
66 return mTransactionContext;
67}
68
69std::shared_ptr<DtoAdapters::CommandContextDto>
70FreeTransactionManagerAdapter::getCommandContext() const
71{
72 return mCommandContext;
73}
74
75int
76FreeTransactionManagerAdapter::getPayloadCapacity() const
77{
78 return mCard->getPayloadCapacity();
79}
80
81void
82FreeTransactionManagerAdapter::resetTransaction()
83{
84 mCommands.clear();
85}
86
87void
88FreeTransactionManagerAdapter::prepareNewSecureSessionIfNeeded(
89 const std::shared_ptr<Command>& /*command*/)
90{
91 /* NOP */
92}
93
94bool
95FreeTransactionManagerAdapter::canConfigureReadOnOpenSecureSession() const
96{
97 return false;
98}
99
100// FreeTransactionManager&
101// FreeTransactionManagerAdapter::processCommands(
102// keypop::calypso::card::transaction::ChannelControl channelControl)
103// {
104// try {
105// return processCommands(
106// keypop::reader::valueOf(static_cast<int>(channelControl)));
107//
108// } catch (const CardCommunicationException& e) {
109// throw CardIOException(e.what(), Exception(e.what()));
110//
111// } catch (const ReaderCommunicationException& e) {
112// throw ReaderIOException(e.what(), e);
113//
114// } catch (const InvalidCardResponseException& e) {
115// throw UnexpectedCommandStatusException(e.what(), e);
116// }
117// }
118
119FreeTransactionManager&
120FreeTransactionManagerAdapter::processCommands(ChannelControl channelControl)
121{
122 if (mCommands.empty()) {
123 return *this;
124 }
125
126 try {
127 std::vector<std::shared_ptr<Command>> cardRequestCommands;
128 for (auto& command : mCommands) {
129 command->finalizeRequest();
130 cardRequestCommands.push_back(command);
131 }
132
133 executeCardCommands(cardRequestCommands, channelControl);
134
135 } catch (...) {
136 resetTransaction();
137
138 /* Finally */
139 mCommands.clear();
140
141 throw;
142 }
143
144 /* Finally */
145 mCommands.clear();
146
147 return *this;
148}
149
150FreeTransactionManager&
151FreeTransactionManagerAdapter::prepareVerifyPin(
152 const std::vector<std::uint8_t>& pin)
153{
154 try {
155 Assert::getInstance().isEqual(
156 pin.size(), CalypsoCardConstant::PIN_LENGTH, "PIN length");
157
158 if (!mCard->isPinFeatureAvailable()) {
159 throw UnsupportedOperationException(MSG_PIN_NOT_AVAILABLE);
160 }
161
162 mCommands.push_back(
163 std::make_shared<CommandVerifyPin>(
164 getTransactionContext(), getCommandContext(), pin));
165
166 } catch (...) {
167 resetTransaction();
168 throw;
169 }
170
171 return *this;
172}
173
174FreeTransactionManager&
175FreeTransactionManagerAdapter::prepareChangePin(
176 const std::vector<std::uint8_t>& newPin)
177{
178 try {
179 Assert::getInstance().isEqual(
180 newPin.size(), CalypsoCardConstant::PIN_LENGTH, "PIN length");
181 if (!mCard->isPinFeatureAvailable()) {
182 throw UnsupportedOperationException(MSG_PIN_NOT_AVAILABLE);
183 }
184
185 // CL-PIN-MENCRYPT.1
186 mCommands.push_back(
187 std::make_shared<CommandChangePin>(
188 getTransactionContext(), getCommandContext(), newPin));
189
190 } catch (...) {
191 resetTransaction();
192 throw;
193 }
194
195 return *this;
196}
197
198} /* namespace calypso */
199} /* namespace card */
200} /* namespace keyple */