Keyple Card Generic C++ Library - 3.1.2
Component of the Keyple C++ middleware
GenericCardResourceProfileExtensionAdapter.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/generic/GenericCardResourceProfileExtensionAdapter.hpp"
15
16#include "keyple/core/util/KeypleAssert.hpp"
17#include "keyple/core/util/cpp/exception/Exception.hpp"
18#include "keypop/reader/selection/CardSelectionManager.hpp"
19#include "keypop/reader/selection/CardSelectionResult.hpp"
20#include "keypop/reader/selection/spi/IsoSmartCard.hpp"
21
22namespace keyple {
23namespace card {
24namespace generic {
25
26using keyple::core::util::Assert;
27using keyple::core::util::cpp::exception::Exception;
28using keypop::reader::selection::CardSelectionManager;
29using keypop::reader::selection::CardSelectionResult;
30using keypop::reader::selection::spi::IsoSmartCard;
31
32GenericCardResourceProfileExtensionAdapter::
33 GenericCardResourceProfileExtensionAdapter(
34 std::shared_ptr<IsoCardSelector> cardSelector,
35 std::shared_ptr<GenericCardSelectionExtension>
36 genericCardSelectionExtension)
37: mGenericCardSelection(
38 std::dynamic_pointer_cast<GenericCardSelectionExtensionAdapter>(
39 genericCardSelectionExtension))
40, mCardSelector(cardSelector)
41{
42 Assert::getInstance().notNull(
43 genericCardSelectionExtension, "genericCardSelectionExtension");
44}
45
46std::shared_ptr<SmartCard>
47GenericCardResourceProfileExtensionAdapter::matches(
48 std::shared_ptr<CardReader> reader,
49 std::shared_ptr<ReaderApiFactory> readerApiFactory)
50{
51 if (!reader->isCardPresent()) {
52 return nullptr;
53 }
54
55 std::shared_ptr<CardSelectionManager> genericCardSelectionManager(
56 readerApiFactory->createCardSelectionManager());
57 genericCardSelectionManager->prepareSelection(
58 mCardSelector, mGenericCardSelection);
59 std::shared_ptr<CardSelectionResult> genericCardSelectionResult = nullptr;
60
61 try {
62 genericCardSelectionResult
63 = genericCardSelectionManager->processCardSelectionScenario(reader);
64 } catch (const Exception& e) {
65 mLogger->error("Card selection failed: %\n", e.getMessage(), e);
66 }
67
68 if (genericCardSelectionResult != nullptr) {
69 return genericCardSelectionResult->getActiveSmartCard();
70 }
71
72 return nullptr;
73}
74
75std::shared_ptr<SmartCard>
76GenericCardResourceProfileExtensionAdapter::matches(
77 std::shared_ptr<SmartCard> smartCard)
78{
79 if (std::dynamic_pointer_cast<IsoSmartCard>(smartCard) == nullptr) {
80 return nullptr;
81 }
82
83 return smartCard;
84}
85
86} /* namespace generic */
87} /* namespace card */
88} /* namespace keyple */