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