Keyple Service C++ Library - 3.4.1
Component of the Keyple C++ middleware
InternalDto.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/core/service/InternalDto.hpp"
15
16#include <iomanip>
17#include <memory>
18#include <sstream>
19#include <string>
20#include <utility>
21#include <vector>
22
23#include "keyple/core/util/cpp/exception/UnsupportedOperationException.hpp"
24
25namespace keyple {
26namespace core {
27namespace service {
28
29using keyple::core::util::cpp::exception::UnsupportedOperationException;
30
31/* CARD SELECTION ADAPTER
32 * ----------------------------------------------------------------------- */
33
34InternalDto::CardSelectionAdapter::CardSelectionAdapter(
35 const std::shared_ptr<CardSelectionExtensionSpi> src)
36: mCardSelectionRequest(
37 std::unique_ptr<CardSelectionRequest>(
38 new CardSelectionRequest(src->getCardSelectionRequest())))
39{
40}
41
42std::unique_ptr<CardSelectionRequestSpi>
43InternalDto::CardSelectionAdapter::getCardSelectionRequest()
44{
45 return std::move(mCardSelectionRequest);
46}
47
48std::shared_ptr<SmartCardSpi>
49InternalDto::CardSelectionAdapter::parse(
50 const std::shared_ptr<
51 CardSelectionResponseApi>& /* cardSelectionResponseApi */)
52{
53 throw UnsupportedOperationException(
54 "Method not supported for internal DTO");
55}
56
57std::ostream&
58operator<<(std::ostream& os, const InternalDto::CardSelectionAdapter& csa)
59{
60 os << "CARD_SELECTION_ADAPTER: {"
61 << "CARD_SELECTION_REQUEST: ";
62
63 if (csa.mCardSelectionRequest == nullptr) {
64 os << "null";
65 } else {
66 os << *csa.mCardSelectionRequest;
67 }
68
69 os << "}";
70
71 return os;
72}
73
74std::ostream&
76 std::ostream& os,
77 const std::shared_ptr<InternalDto::CardSelectionAdapter> csa)
78{
79 if (csa == nullptr) {
80 os << "CARD_SELECTION_ADAPTER: null";
81 } else {
82 os << *csa;
83 }
84
85 return os;
86}
87
88/* CARD SELECTION REQUEST
89 * ----------------------------------------------------------------------- */
90
91InternalDto::CardSelectionRequest::CardSelectionRequest(
92 const std::shared_ptr<CardSelectionRequestSpi> src)
93{
94 if (src->getCardRequest() != nullptr) {
95 mCardRequest = std::make_shared<CardRequest>(src->getCardRequest());
96 }
97
98 mSuccessfulSelectionStatusWords = src->getSuccessfulSelectionStatusWords();
99}
100
101const std::vector<int>&
102InternalDto::CardSelectionRequest::getSuccessfulSelectionStatusWords() const
103{
104 return mSuccessfulSelectionStatusWords;
105}
106
107const std::shared_ptr<CardRequestSpi>
108InternalDto::CardSelectionRequest::getCardRequest() const
109{
110 return mCardRequest;
111}
112
113std::ostream&
114operator<<(std::ostream& os, const InternalDto::CardSelectionRequest& csr)
115{
116 std::stringstream ssSw;
117 for (auto it = std::begin(csr.mSuccessfulSelectionStatusWords);
118 it != std::end(csr.mSuccessfulSelectionStatusWords);
119 ++it) {
120 ssSw << std::uppercase << std::hex << std::setfill('0') << std::setw(4)
121 << static_cast<int>(*it);
122 if (it != csr.mSuccessfulSelectionStatusWords.end() - 1) {
123 ssSw << ", ";
124 }
125 }
126
127 os << "CARD_SELECTION_REQUEST: {"
128 << "CARD_REQUEST: ";
129
130 if (csr.mCardRequest == nullptr) {
131 os << "null";
132 } else {
133 os << *csr.mCardRequest;
134 }
135
136 os << ", "
137 << "SUCCESSFUL_SELECTION_STATUS_WORDS: " << ssSw.str() << "}";
138
139 return os;
140}
141
142std::ostream&
144 std::ostream& os,
145 const std::shared_ptr<InternalDto::CardSelectionRequest> csr)
146{
147 if (csr == nullptr) {
148 os << "CARD_SELECTION_REQUEST: null";
149 } else {
150 os << *csr;
151 }
152
153 return os;
154}
155
156/* CARD REQUEST
157 * ----------------------------------------------------------------------- */
158
159InternalDto::CardRequest::CardRequest(const std::shared_ptr<CardRequestSpi> src)
160{
161 for (const auto& apduRequestSpi : src->getApduRequests()) {
162 mApduRequests.push_back(std::make_shared<ApduRequest>(apduRequestSpi));
163 }
164
165 mStopOnUnsuccessfulStatusWord = src->stopOnUnsuccessfulStatusWord();
166}
167
168const std::vector<std::shared_ptr<ApduRequestSpi>>&
169InternalDto::CardRequest::getApduRequests() const
170{
171 return mApduRequests;
172}
173
174bool
175InternalDto::CardRequest::stopOnUnsuccessfulStatusWord() const
176{
177 return mStopOnUnsuccessfulStatusWord;
178}
179
180std::ostream&
181operator<<(std::ostream& os, const InternalDto::CardRequest& cr)
182{
183 os << "CARD_REQUEST: {"
184 << "APDU_REQUESTS: " << cr.mApduRequests << ", "
185 << "STOP_ON_UNSUCCESSFUL_STATUS_WORD: "
186 << cr.mStopOnUnsuccessfulStatusWord << "}";
187
188 return os;
189}
190
191std::ostream&
192operator<<(std::ostream& os, const std::shared_ptr<InternalDto::CardRequest> cr)
193{
194 if (cr == nullptr) {
195 os << "CARD_REQUEST: null";
196 } else {
197 os << *cr;
198 }
199
200 return os;
201}
202
203/* APDU REQUEST
204 * ----------------------------------------------------------------------- */
205
206InternalDto::ApduRequest::ApduRequest(const std::shared_ptr<ApduRequestSpi> src)
207: mApdu(src->getApdu())
208, mSuccessfulStatusWords(src->getSuccessfulStatusWords())
209, mInfo(src->getInfo())
210{
211}
212
213const std::vector<uint8_t>&
214InternalDto::ApduRequest::getApdu() const
215{
216 return mApdu;
217}
218
219void
220InternalDto::ApduRequest::setApdu(const std::vector<uint8_t>& apdu)
221{
222 mApdu = apdu;
223}
224
225const std::vector<int>&
226InternalDto::ApduRequest::getSuccessfulStatusWords() const
227{
228 return mSuccessfulStatusWords;
229}
230
231const std::string&
232InternalDto::ApduRequest::getInfo() const
233{
234 return mInfo;
235}
236
237std::ostream&
238operator<<(std::ostream& os, const InternalDto::ApduRequest& ar)
239{
240 std::stringstream ssApdu;
241 for (const auto val : ar.mApdu) {
242 ssApdu << std::uppercase << std::hex << std::setfill('0')
243 << std::setw(2) << static_cast<int>(val);
244 }
245
246 std::stringstream ssSw;
247 for (auto it = std::begin(ar.mSuccessfulStatusWords);
248 it != std::end(ar.mSuccessfulStatusWords);
249 ++it) {
250 ssSw << std::uppercase << std::hex << std::setfill('0') << std::setw(4)
251 << static_cast<int>(*it);
252 if (it != ar.mSuccessfulStatusWords.end() - 1) {
253 ssSw << ", ";
254 }
255 }
256
257 os << "APDU_REQUEST: {"
258 << "APDU: " << ssApdu.str() << ", "
259 << "SUCCESSFUL_STATUS_WORD: " << ssSw.str() << ", "
260 << "INFO: " << ar.mInfo << "}";
261
262 return os;
263}
264
265std::ostream&
266operator<<(std::ostream& os, const std::shared_ptr<InternalDto::ApduRequest> ar)
267{
268 if (ar == nullptr) {
269 os << "APDU_REQUEST: null";
270 } else {
271 os << *ar;
272 }
273
274 return os;
275}
276
277} /* namespace service */
278} /* namespace core */
279} /* namespace keyple */
std::ostream & operator<<(std::ostream &os, const std::shared_ptr< InternalDto::ApduRequest > ar)