Keyple Service C++ Library - 3.4.1
Component of the Keyple C++ middleware
InternalLegacyDto.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/InternalLegacyDto.hpp"
15
16#include <iomanip>
17#include <memory>
18#include <sstream>
19#include <vector>
20
21#include "keyple/core/service/InternalCardSelector.hpp"
22
23namespace keyple {
24namespace core {
25namespace service {
26
27InternalLegacyDto::InternalLegacyDto()
28{
29}
30
31const std::vector<std::shared_ptr<LegacyCardSelectionRequestV0>>
32InternalLegacyDto::mapToLegacyCardSelectionRequestsV0(
33 const std::vector<std::shared_ptr<CardSelector>>& cardSelectors,
34 const std::vector<std::shared_ptr<CardSelectionRequestSpi>>&
35 cardSelectionRequests)
36{
37 std::vector<std::shared_ptr<LegacyCardSelectionRequestV0>> result;
38
39 for (int i = 0; i < cardSelectors.size(); i++) {
40 result.push_back(mapToLegacyCardSelectionRequestV0(
41 cardSelectors[i], cardSelectionRequests[i]));
42 }
43
44 return result;
45}
46
47const std::vector<std::shared_ptr<LegacyCardSelectionRequestV1>>
48InternalLegacyDto::mapToLegacyCardSelectionRequestsV1(
49 const std::vector<std::shared_ptr<CardSelector>>& cardSelectors,
50 const std::vector<std::shared_ptr<CardSelectionRequestSpi>>&
51 cardSelectionRequests)
52{
53 std::vector<std::shared_ptr<LegacyCardSelectionRequestV1>> result;
54
55 for (int i = 0; i < cardSelectors.size(); i++) {
56 result.push_back(mapToLegacyCardSelectionRequestV1(
57 cardSelectors[i], cardSelectionRequests[i]));
58 }
59
60 return result;
61}
62
63std::shared_ptr<LegacyCardSelectionRequestV0>
64InternalLegacyDto::mapToLegacyCardSelectionRequestV0(
65 std::shared_ptr<CardSelector> cardSelector,
66 std::shared_ptr<CardSelectionRequestSpi> cardSelectionRequestSpi)
67{
68 auto result = std::make_shared<LegacyCardSelectionRequestV0>();
69
70 result->mCardRequest = cardSelectionRequestSpi->getCardRequest() != nullptr
71 ? mapToLegacyCardRequestV0(
72 cardSelectionRequestSpi->getCardRequest())
73 : nullptr;
74
75 result->mCardSelector
76 = mapToLegacyCardSelector(cardSelector, cardSelectionRequestSpi);
77
78 return result;
79}
80
81std::shared_ptr<LegacyCardSelectionRequestV1>
82InternalLegacyDto::mapToLegacyCardSelectionRequestV1(
83 std::shared_ptr<CardSelector> cardSelector,
84 std::shared_ptr<CardSelectionRequestSpi> cardSelectionRequestSpi)
85{
86 auto result = std::make_shared<LegacyCardSelectionRequestV1>();
87
88 result->mCardRequest = cardSelectionRequestSpi->getCardRequest() != nullptr
89 ? mapToLegacyCardRequestV1(
90 cardSelectionRequestSpi->getCardRequest())
91 : nullptr;
92
93 result->mCardSelector
94 = mapToLegacyCardSelector(cardSelector, cardSelectionRequestSpi);
95
96 return result;
97}
98
99std::shared_ptr<LegacyCardSelector>
100InternalLegacyDto::mapToLegacyCardSelector(
101 std::shared_ptr<CardSelector> cardSelector,
102 std::shared_ptr<CardSelectionRequestSpi> cardSelectionRequestSpi)
103{
104 auto result = std::make_shared<LegacyCardSelector>();
105 auto basicCardSelector
106 = std::dynamic_pointer_cast<InternalCardSelector>(cardSelector);
107
108 result->mCardProtocol = basicCardSelector->getLogicalProtocolName();
109 result->mPowerOnDataRegex = basicCardSelector->getPowerOnDataRegex();
110
111 const auto isoCardSelector
112 = std::dynamic_pointer_cast<InternalIsoCardSelector>(cardSelector);
113 if (isoCardSelector) {
114 result->mAid = isoCardSelector->getAid();
115 result->mFileOccurrence = isoCardSelector->getFileOccurrence();
116 result->mFileControlInformation
117 = isoCardSelector->getFileControlInformation();
118 } else {
119 result->mAid = std::vector<uint8_t>(0);
120 result->mFileOccurrence = CommonIsoCardSelector::FileOccurrence::FIRST;
121 result->mFileControlInformation
122 = CommonIsoCardSelector::FileControlInformation::FCI;
123 }
124 result->mSuccessfulSelectionStatusWords
125 = cardSelectionRequestSpi->getSuccessfulSelectionStatusWords();
126
127 return result;
128}
129
130std::shared_ptr<LegacyCardRequest>
131InternalLegacyDto::mapToLegacyCardRequestV0(
132 const std::shared_ptr<CardRequestSpi> cardRequest)
133{
134 auto result = std::make_shared<LegacyCardRequestV0>();
135
136 result->mApduRequests
137 = mapToLegacyApduRequests(cardRequest->getApduRequests());
138 result->mIsStatusCodesVerificationEnabled
139 = cardRequest->stopOnUnsuccessfulStatusWord();
140
141 return result;
142}
143
144std::shared_ptr<LegacyCardRequest>
145InternalLegacyDto::mapToLegacyCardRequestV1(
146 const std::shared_ptr<CardRequestSpi> cardRequest)
147{
148 auto result = std::make_shared<LegacyCardRequestV1>();
149
150 result->mApduRequests
151 = mapToLegacyApduRequests(cardRequest->getApduRequests());
152 result->mStopOnUnsuccessfulStatusWord
153 = cardRequest->stopOnUnsuccessfulStatusWord();
154
155 return result;
156}
157
158const std::vector<std::shared_ptr<LegacyApduRequest>>
159InternalLegacyDto::mapToLegacyApduRequests(
160 const std::vector<std::shared_ptr<ApduRequestSpi>>& apduRequests)
161{
162 std::vector<std::shared_ptr<LegacyApduRequest>> result;
163
164 for (const auto& apduRequestSpi : apduRequests) {
165 result.push_back(mapToLegacyApduRequest(apduRequestSpi));
166 }
167
168 return result;
169}
170
171std::shared_ptr<LegacyApduRequest>
172InternalLegacyDto::mapToLegacyApduRequest(
173 const std::shared_ptr<ApduRequestSpi> apduRequestSpi)
174{
175 auto result = std::make_shared<LegacyApduRequest>();
176
177 result->mApdu = apduRequestSpi->getApdu();
178 result->mInfo = apduRequestSpi->getInfo();
179 result->mSuccessfulStatusWords = apduRequestSpi->getSuccessfulStatusWords();
180
181 return result;
182}
183
184std::ostream&
185operator<<(std::ostream& os, const InternalLegacyDto::LegacyCardSelector& lcs)
186{
187 std::stringstream ssAid;
188 for (const auto val : lcs.mAid) {
189 ssAid << std::uppercase << std::hex << std::setfill('0') << std::setw(2)
190 << static_cast<int>(val);
191 }
192
193 std::stringstream ssSw;
194 for (auto it = std::begin(lcs.mSuccessfulSelectionStatusWords);
195 it != std::end(lcs.mSuccessfulSelectionStatusWords);
196 ++it) {
197 ssSw << std::uppercase << std::hex << std::setfill('0') << std::setw(4)
198 << static_cast<int>(*it);
199 if (it != lcs.mSuccessfulSelectionStatusWords.end() - 1) {
200 ssSw << ", ";
201 }
202 }
203
204 os << "LEGACY_CARD_SELECTOR: {"
205 << "CARD_PROTOCOL: " << lcs.mCardProtocol << ", "
206 << "POWER_ON_DATA_REGEX: " << lcs.mPowerOnDataRegex << ", "
207 << "AID: " << ssAid.str() << ", "
208 << "FILE_OCCURRENCE: " << lcs.mFileOccurrence << ", "
209 << "FILE_CONTROL_INFORMATION: " << lcs.mFileControlInformation << ", "
210 << "SUCCESSFUL_SELECTION_STATUS_WORDS: " << ssSw.str() << "}";
211
212 return os;
213}
214
215std::ostream&
217 std::ostream& os,
218 const std::shared_ptr<InternalLegacyDto::LegacyCardSelector> lcs)
219{
220 if (lcs == nullptr) {
221 os << "LEGACY_CARD_SELECTOR: null";
222 } else {
223 os << *lcs;
224 }
225
226 return os;
227}
228
229std::ostream&
230operator<<(std::ostream& os, const InternalLegacyDto::LegacyApduRequest& lar)
231{
232 std::stringstream ssApdu;
233 for (const auto val : lar.mApdu) {
234 ssApdu << std::uppercase << std::hex << std::setfill('0')
235 << std::setw(2) << static_cast<int>(val);
236 }
237
238 std::stringstream ssSw;
239 for (auto it = std::begin(lar.mSuccessfulStatusWords);
240 it != std::end(lar.mSuccessfulStatusWords);
241 ++it) {
242 ssSw << std::uppercase << std::hex << std::setfill('0') << std::setw(4)
243 << static_cast<int>(*it);
244 if (it != lar.mSuccessfulStatusWords.end() - 1) {
245 ssSw << ", ";
246 }
247 }
248
249 os << "LEGACY_APDU_REQUEST: {"
250 << "APDU: " << ssApdu.str() << ", "
251 << "SUCCESSFUL_STATUS_WORD: " << ssSw.str() << ", "
252 << "INFO: " << lar.mInfo << "}";
253
254 return os;
255}
256
257std::ostream&
259 std::ostream& os,
260 const std::shared_ptr<InternalLegacyDto::LegacyApduRequest> lar)
261{
262 if (lar == nullptr) {
263 os << "LEGACY_APDU_REQUEST: null";
264 } else {
265 os << *lar;
266 }
267
268 return os;
269}
270
271std::ostream&
272operator<<(std::ostream& os, const InternalLegacyDto::LegacyCardRequestV0& lcr)
273{
274 os << "LEGACY_CARD_REQUEST_V0: {"
275 << "APDU_REQUESTS: {";
276
277 for (auto it = std::begin(lcr.mApduRequests);
278 it != std::end(lcr.mApduRequests);
279 ++it) {
280 os << **it;
281 if (it != lcr.mApduRequests.end() - 1) {
282 os << ", ";
283 }
284 }
285
286 os << "}, "
287 << "IS_STATUS_CODES_VERIFICATION_ENABLED: "
288 << lcr.mIsStatusCodesVerificationEnabled << "}";
289
290 return os;
291}
292
293std::ostream&
295 std::ostream& os,
296 const std::shared_ptr<InternalLegacyDto::LegacyCardRequestV0> lcr)
297{
298 if (lcr == nullptr) {
299 os << "LEGACY_CARD_REQUEST_V0: null";
300 } else {
301 os << *lcr;
302 }
303
304 return os;
305}
306
307std::ostream&
308operator<<(std::ostream& os, const InternalLegacyDto::LegacyCardRequestV1& lcr)
309{
310 os << "LEGACY_CARD_REQUEST_V1: {"
311 << "APDU_REQUESTS: {";
312
313 for (auto it = std::begin(lcr.mApduRequests);
314 it != std::end(lcr.mApduRequests);
315 ++it) {
316 os << **it;
317 if (it != lcr.mApduRequests.end() - 1) {
318 os << ", ";
319 }
320 }
321
322 os << "}, "
323 << "STOP_ON_UNSUCCESSFUL_STATUS_WORD: "
324 << lcr.mStopOnUnsuccessfulStatusWord << "}";
325
326 return os;
327}
328
329std::ostream&
331 std::ostream& os,
332 const std::shared_ptr<InternalLegacyDto::LegacyCardRequestV1> lcr)
333{
334 if (lcr == nullptr) {
335 os << "LEGACY_CARD_REQUEST_V1: null";
336 } else {
337 os << *lcr;
338 }
339
340 return os;
341}
342
343std::ostream&
345 std::ostream& os,
346 const InternalLegacyDto::LegacyCardSelectionRequestV0& lcsr)
347{
348 os << "LEGACY_CARD_SELECTION_REQUEST_V0: {"
349 << "CARD_SELECTOR: ";
350
351 if (lcsr.mCardSelector == nullptr) {
352 os << "null";
353 } else {
354 os << *lcsr.mCardSelector;
355 }
356
357 os << ", "
358 << "CARD_REQUEST: ";
359
360 if (lcsr.mCardRequest == nullptr) {
361 os << "null";
362 } else {
363 os << *lcsr.mCardRequest;
364 }
365
366 os << "}";
367
368 return os;
369}
370
371std::ostream&
373 std::ostream& os,
374 const std::shared_ptr<InternalLegacyDto::LegacyCardSelectionRequestV0> lcsr)
375{
376 if (lcsr == nullptr) {
377 os << "LEGACY_CARD_SELECTION_REQUEST_V0: null";
378 } else {
379 os << *lcsr;
380 }
381
382 return os;
383}
384
385std::ostream&
387 std::ostream& os,
388 const InternalLegacyDto::LegacyCardSelectionRequestV1& lcsr)
389{
390 os << "LEGACY_CARD_SELECTION_REQUEST_V1: {"
391 << "CARD_SELECTOR: ";
392
393 if (lcsr.mCardSelector == nullptr) {
394 os << "null";
395 } else {
396 os << *lcsr.mCardSelector;
397 }
398
399 os << ", "
400 << "CARD_REQUEST: ";
401
402 if (lcsr.mCardRequest == nullptr) {
403 os << "null";
404 } else {
405 os << *lcsr.mCardRequest;
406 }
407
408 os << "}";
409
410 return os;
411}
412
413std::ostream&
415 std::ostream& os,
416 const std::shared_ptr<InternalLegacyDto::LegacyCardSelectionRequestV1> lcsr)
417{
418 if (lcsr == nullptr) {
419 os << "LEGACY_CARD_SELECTION_REQUEST_V1: null";
420 } else {
421 os << *lcsr;
422 }
423
424 return os;
425}
426
427} /* namespace service */
428} /* namespace core */
429} /* namespace keyple */
std::ostream & operator<<(std::ostream &os, const ApduResponseAdapter &ara)