Keyple Card Calypso C++ Library 2.2.2
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 Core Util */
18#include "StringUtils.h"
19
20namespace keyple {
21namespace card {
22namespace calypso {
23
24using namespace keyple::core::util::cpp;
25
27
28/* STATUS PROPERTIES ---------------------------------------------------------------------------- */
29
30StatusProperties::StatusProperties(const std::string& information)
31: mInformation(information), mSuccessful(true), mExceptionClass(typeid(nullptr)) {}
32
34 const std::string& information, const std::type_info& exceptionClass)
35: mInformation(information),
36 mSuccessful(exceptionClass == typeid(nullptr)),
37 mExceptionClass(exceptionClass) {}
38
39const std::string& StatusProperties::getInformation() const
40{
41 return mInformation;
42}
43
45{
46 return mSuccessful;
47}
48
49const std::type_info& StatusProperties::getExceptionClass() const
50{
51 return mExceptionClass;
52}
53
54/* ABSTRACT APDU COMMAND ------------------------------------------------------------------------ */
55
56const std::map<const int, const std::shared_ptr<StatusProperties>>
58 {0x9000, std::make_shared<StatusProperties>("Success")},
59};
60
62: mCommandRef(commandRef), mLe(le), mName(commandRef.getName()) {}
63
64void AbstractApduCommand::addSubName(const std::string& subName)
65{
66 mName.append("-").append(subName);
67 mApduRequest->setInfo(mName);
68}
69
71{
72 return mCommandRef;
73}
74
75const std::string& AbstractApduCommand::getName() const
76{
77 return mName;
78}
79
80void AbstractApduCommand::setApduRequest(const std::shared_ptr<ApduRequestAdapter> apduRequest)
81{
82 mApduRequest = apduRequest;
83 mApduRequest->setInfo(mName);
84}
85
86const std::shared_ptr<ApduRequestAdapter> AbstractApduCommand::getApduRequest() const
87{
88 return mApduRequest;
89}
90
92 const std::shared_ptr<ApduResponseApi> apduResponse)
93{
94 mApduResponse = apduResponse;
95
96 return *this;
97}
98
99const std::shared_ptr<ApduResponseApi> AbstractApduCommand::getApduResponse() const
100{
101 return mApduResponse;
102}
103
104const std::map<const int, const std::shared_ptr<StatusProperties>>&
106{
107 return STATUS_TABLE;
108}
109
110const std::shared_ptr<StatusProperties> AbstractApduCommand::getStatusWordProperties() const
111{
112 const std::map<const int, const std::shared_ptr<StatusProperties>>& table = getStatusTable();
113
114 const auto it = getStatusTable().find(mApduResponse->getStatusWord());
115
116 return it != table.end() ? it->second : nullptr;
117}
118
120{
121 const std::shared_ptr<StatusProperties> props = getStatusWordProperties();
122
123 return props != nullptr &&
124 props->isSuccessful() &&
125 /* CL-CSS-RESPLE.1 */
126 (mLe == 0 || static_cast<int>(mApduResponse->getDataOut().size()) == mLe);
127}
128
130{
131 const std::shared_ptr<StatusProperties> props = getStatusWordProperties();
132 if (props != nullptr && props->isSuccessful()) {
133
134 /* SW is successful, then check the response length (CL-CSS-RESPLE.1) */
135 if (mLe != 0 && static_cast<int>(mApduResponse->getDataOut().size()) != mLe) {
137 StringUtils::format("Incorrect APDU response length (expected: %d, actual: %d)",
138 mLe,
139 mApduResponse->getDataOut().size()));
140 }
141
142 /* SW and response length are correct */
143 return;
144 }
145
146 /* Status word is not referenced, or not successful */
147
148 /* Exception class */
149 const std::type_info& exceptionClass = props != nullptr ? props->getExceptionClass()
150 : typeid(nullptr);
151
152 /* Message */
153 const std::string message = props != nullptr ? props->getInformation() : "Unknown status";
154
155 /* Throw the exception */
156 throw buildCommandException(exceptionClass, message);
157}
158
160{
161 const std::shared_ptr<StatusProperties> props = getStatusWordProperties();
162
163 return props != nullptr ? props->getInformation() : "";
164}
165
166}
167}
168}
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
AbstractApduCommand(const CardCommand &commandRef, const int le)
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 buildUnexpectedResponseLengthException(const std::string &message) const =0
virtual const CalypsoApduCommandException buildCommandException(const std::type_info &exceptionClass, const std::string &message) const =0
AbstractApduCommand::StatusProperties StatusProperties