Keyple Card Calypso C++ Library 2.1.0
Reference Terminal Reader API for C++
AbstractApduCommand.cpp
Go to the documentation of this file.
1/**************************************************************************************************
2 * Copyright (c) 2022 Calypso Networks Association https://calypsonet.org/ *
3 * *
4 * See the NOTICE file(s) distributed with this work for additional information regarding *
5 * copyright ownership. *
6 * *
7 * This program and the accompanying materials are made available under the terms of the Eclipse *
8 * Public License 2.0 which is available at http://www.eclipse.org/legal/epl-2.0 *
9 * *
10 * SPDX-License-Identifier: EPL-2.0 *
11 **************************************************************************************************/
12
13#include "AbstractApduCommand.h"
14
15#include <typeinfo>
16
17/* Keyple Card Calypso */
19
20namespace keyple {
21namespace card {
22namespace calypso {
23
25
26/* STATUS PROPERTIES ---------------------------------------------------------------------------- */
27
28StatusProperties::StatusProperties(const std::string& information)
29: mInformation(information), mSuccessful(true), mExceptionClass(typeid(nullptr)) {}
30
32 const std::string& information, const std::type_info& exceptionClass)
33: mInformation(information),
34 mSuccessful(exceptionClass == typeid(nullptr)),
35 mExceptionClass(exceptionClass) {}
36
37const std::string& StatusProperties::getInformation() const
38{
39 return mInformation;
40}
41
43{
44 return mSuccessful;
45}
46
47const std::type_info& StatusProperties::getExceptionClass() const
48{
49 return mExceptionClass;
50}
51
52/* ABSTRACT APDU COMMAND ------------------------------------------------------------------------ */
53
54const std::map<const int, const std::shared_ptr<StatusProperties>>
56 {0x9000, std::make_shared<StatusProperties>("Success")},
57};
58
60: mCommandRef(commandRef), mName(commandRef.getName()) {}
61
62void AbstractApduCommand::addSubName(const std::string& subName)
63{
64 mName.append("-").append(subName);
65 mApduRequest->setInfo(mName);
66}
67
69{
70 return mCommandRef;
71}
72
73const std::string& AbstractApduCommand::getName() const
74{
75 return mName;
76}
77
78void AbstractApduCommand::setApduRequest(const std::shared_ptr<ApduRequestAdapter> apduRequest)
79{
80 mApduRequest = apduRequest;
81 mApduRequest->setInfo(mName);
82}
83
84const std::shared_ptr<ApduRequestAdapter> AbstractApduCommand::getApduRequest() const
85{
86 return mApduRequest;
87}
88
90 const std::shared_ptr<ApduResponseApi> apduResponse)
91{
92 mApduResponse = apduResponse;
93
94 return *this;
95}
96
97const std::shared_ptr<ApduResponseApi> AbstractApduCommand::getApduResponse() const
98{
99 return mApduResponse;
100}
101
102const std::map<const int, const std::shared_ptr<StatusProperties>>&
104{
105 return STATUS_TABLE;
106}
107
109 const std::type_info& exceptionClass,
110 const std::string& message,
111 const CardCommand& commandRef,
112 const int statusWord) const
113{
114 (void)exceptionClass;
115
116 const auto sw = std::make_shared<int>(statusWord);
117
118 return CardCommandUnknownStatusException(message, commandRef, sw);
119}
120
121const std::shared_ptr<StatusProperties> AbstractApduCommand::getStatusWordProperties() const
122{
123 const std::map<const int, const std::shared_ptr<StatusProperties>>& table = getStatusTable();
124
125 const auto it = getStatusTable().find(mApduResponse->getStatusWord());
126
127 return it != table.end() ? it->second : nullptr;
128}
129
131{
132 const std::shared_ptr<StatusProperties> props = getStatusWordProperties();
133
134 return props != nullptr && props->isSuccessful();
135}
136
138{
139 const std::shared_ptr<StatusProperties> props = getStatusWordProperties();
140 if (props != nullptr && props->isSuccessful()) {
141 return;
142 }
143
144 /* Status word is not referenced, or not successful */
145
146 /* Exception class */
147 const std::type_info& exceptionClass = props != nullptr ? props->getExceptionClass()
148 : typeid(nullptr);
149
150 /* Message */
151 const std::string message = props != nullptr ? props->getInformation() : "Unknown status";
152
153 /* Status word */
154 const int statusWord = mApduResponse->getStatusWord();
155
156 /* Throw the exception */
157 throw buildCommandException(exceptionClass, message, mCommandRef, statusWord);
158}
159
161{
162 const std::shared_ptr<StatusProperties> props = getStatusWordProperties();
163
164 return props != nullptr ? props->getInformation() : "";
165}
166
167}
168}
169}
virtual const std::string & getName() const final
static const std::map< const int, const std::shared_ptr< StatusProperties > > STATUS_TABLE
virtual AbstractApduCommand & setApduResponse(const std::shared_ptr< ApduResponseApi > apduResponse)
virtual void addSubName(const std::string &subName) final
virtual const std::map< const int, const std::shared_ptr< StatusProperties > > & getStatusTable() const
virtual const std::string getStatusInformation() const final
virtual const std::shared_ptr< ApduResponseApi > getApduResponse() const final
virtual const std::shared_ptr< ApduRequestAdapter > getApduRequest() const final
virtual const CardCommand & getCommandRef() const
virtual void setApduRequest(const std::shared_ptr< ApduRequestAdapter > apduRequest) final
virtual const CalypsoApduCommandException buildCommandException(const std::type_info &exceptionClass, const std::string &message, const CardCommand &commandRef, const int statusWord) const
AbstractApduCommand(const CardCommand &commandRef)
AbstractApduCommand::StatusProperties StatusProperties