keyple-card-calypso-crypto-legacysam-cpp-lib - 1.0.2
Component of the Keyple C++ middleware
AsyncTransactionCreatorManagerAdapter.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/crypto/legacysam/AsyncTransactionCreatorManagerAdapter.hpp"
15
16#include <memory>
17#include <string>
18#include <vector>
19
20#include "keyple/card/calypso/crypto/legacysam/Command.hpp"
21#include "keyple/card/calypso/crypto/legacysam/CommandWriteCeilings.hpp"
22#include "keyple/card/calypso/crypto/legacysam/LegacySamConstants.hpp"
23#include "keyple/card/calypso/crypto/legacysam/SecuritySettingAdapter.hpp"
24#include "keyple/core/util/KeypleAssert.hpp"
25#include "keyple/core/util/cpp/exception/UnsupportedOperationException.hpp"
26
27namespace keyple {
28namespace card {
29namespace calypso {
30namespace crypto {
31namespace legacysam {
32
33using keyple::core::util::Assert;
34using keyple::core::util::cpp::exception::UnsupportedOperationException;
35
36AsyncTransactionCreatorManagerAdapter::AsyncTransactionCreatorManagerAdapter(
37 const std::string& /* targetSamContextJson* */,
38 const std::shared_ptr<SecuritySetting> securitySetting)
39: CommonTransactionManagerAdapter(
40 nullptr,
41 nullptr,
42 std::dynamic_pointer_cast<SecuritySettingAdapter>(securitySetting)
43 ->getControlSamReader(),
44 std::dynamic_pointer_cast<SecuritySettingAdapter>(securitySetting)
45 ->getControlSam())
46{
47}
48
49AsyncTransactionCreatorManager&
50AsyncTransactionCreatorManagerAdapter::prepareWriteCounterCeiling(
51 const int counterNumber, const int ceilingValue)
52{
53 Assert::getInstance()
54 .isInRange(
55 counterNumber,
56 LegacySamConstants::MIN_COUNTER_CEILING_NUMBER,
57 LegacySamConstants::MAX_COUNTER_CEILING_NUMBER,
58 "counterNumber")
59 .isInRange(
60 ceilingValue,
61 LegacySamConstants::MIN_COUNTER_CEILING_VALUE,
62 LegacySamConstants::MAX_COUNTER_CEILING_VALUE,
63 "ceilingValue");
64
65 addTargetSamCommand(
66 std::make_shared<CommandWriteCeilings>(
67 getContext(), mTargetSamContext, counterNumber, ceilingValue));
68
69 return *this;
70}
71
72AsyncTransactionCreatorManager&
73AsyncTransactionCreatorManagerAdapter::prepareWriteCounterConfiguration(
74 const int counterNumber,
75 const int ceilingValue,
76 const CounterIncrementAccess counterIncrementAccess)
77{
78 Assert::getInstance()
79 .isInRange(
80 counterNumber,
81 LegacySamConstants::MIN_COUNTER_CEILING_NUMBER,
82 LegacySamConstants::MAX_COUNTER_CEILING_NUMBER,
83 "counterNumber")
84 .isInRange(
85 ceilingValue,
86 LegacySamConstants::MIN_COUNTER_CEILING_VALUE,
87 LegacySamConstants::MAX_COUNTER_CEILING_VALUE,
88 "ceilingValue");
89
90 for (const auto& command : getTargetSamCommands()) {
91 auto commandWriteCeilings
92 = std::dynamic_pointer_cast<CommandWriteCeilings>(command);
93 if (commandWriteCeilings
94 && commandWriteCeilings->getCounterFileRecordNumber()
95 == LegacySamConstants::COUNTER_TO_RECORD_LOOKUP
96 [counterNumber]) {
97 commandWriteCeilings->addCounter(
98 counterNumber, ceilingValue, counterIncrementAccess);
99 return *this;
100 }
101 }
102
103 addTargetSamCommand(
104 std::make_shared<CommandWriteCeilings>(
105 getContext(),
106 mTargetSamContext,
107 counterNumber,
108 ceilingValue,
109 counterIncrementAccess));
110
111 return *this;
112}
113
114std::string
115AsyncTransactionCreatorManagerAdapter::exportCommands() const
116{
117 const std::vector<std::shared_ptr<Command>> commands
118 = getTargetSamCommands();
119
120 for (const auto& command : commands) {
121 command->finalizeRequest();
122 }
123
124 // JsonObject jsonObject = new JsonObject();
125
126 std::vector<std::string> cardCommandTypes;
127
128 for (const auto& command : commands) {
129 cardCommandTypes.push_back(typeid(command).name());
130 }
131
132 // jsonObject.add(SAM_COMMANDS_TYPES,
133 // JsonUtil.getParser().toJsonTree(cardCommandTypes));
134 // jsonObject.add(SAM_COMMANDS, JsonUtil.getParser().toJsonTree(commands));
135
136 return ""; // jsonObject.toString();
137}
138
139AsyncTransactionCreatorManager&
140AsyncTransactionCreatorManagerAdapter::processCommands()
141{
142 throw UnsupportedOperationException(
143 "'processCommands' is not allowed during the creation of an "
144 "asynchronous transaction");
145}
146
147AsyncTransactionCreatorManager&
148AsyncTransactionCreatorManagerAdapter::processCommands(
149 ChannelControl /*channelControl*/)
150{
151 throw UnsupportedOperationException(
152 "'processCommands' is not allowed during the creation of an "
153 "asynchronous transaction");
154}
155
156} /* namespace legacysam */
157} /* namespace crypto */
158} /* namespace calypso */
159} /* namespace card */
160} /* namespace keyple */