18#include "StringUtils.h"
50using namespace keyple::core::util::cpp;
57: mInformation(information), mSuccessful(true), mExceptionClass(typeid(nullptr)) {}
60 const std::string& information,
const std::type_info& exceptionClass)
61: mInformation(information),
62 mSuccessful(exceptionClass == typeid(nullptr)),
63 mExceptionClass(exceptionClass) {}
77 return mExceptionClass;
82const std::map<const int, const std::shared_ptr<StatusProperties>>
84 {0x9000, std::make_shared<StatusProperties>(
"Success")},
88: mCommandRef(commandRef), mExpectedResponseLength(expectedResponseLength), mName(commandRef.
getName()) {}
92 mName.append(
"-").append(subName);
93 mApduRequest->setInfo(mName);
108 mExpectedResponseLength = expectedResponseLength;
113 mApduRequest = apduRequest;
114 mApduRequest->setInfo(mName);
124 mApduResponse = apduResponse;
131 return mApduResponse;
134const std::map<const int, const std::shared_ptr<StatusProperties>>&
140const std::shared_ptr<StatusProperties> AbstractApduCommand::getStatusWordProperties()
const
142 const std::map<const int, const std::shared_ptr<StatusProperties>>& table =
getStatusTable();
144 const auto it =
getStatusTable().find(mApduResponse->getStatusWord());
146 return it != table.end() ? it->second :
nullptr;
151 const std::shared_ptr<StatusProperties> props = getStatusWordProperties();
153 return props !=
nullptr &&
154 props->isSuccessful() &&
156 (mExpectedResponseLength == -1 ||
static_cast<int>(mApduResponse->getDataOut().size()) == mExpectedResponseLength);
159void AbstractApduCommand::checkStatus()
161 const std::shared_ptr<StatusProperties> props = getStatusWordProperties();
162 if (props !=
nullptr && props->isSuccessful()) {
165 if (mExpectedResponseLength != -1 &&
static_cast<int>(mApduResponse->getDataOut().size()) != mExpectedResponseLength) {
181 StringUtils::format(
"Incorrect APDU response length (expected: %d, " \
183 mExpectedResponseLength,
184 mApduResponse->getDataOut().size()));
188 }
catch (
const std::bad_cast& e) {
191 CalypsoApduCommandException ex =
193 StringUtils::format(
"Incorrect APDU response length (expected: %d, " \
195 mExpectedResponseLength,
196 mApduResponse->getDataOut().size()));
198 throw static_cast<const CalypsoSamUnexpectedResponseLengthException&
>(ex);
209 const std::type_info& exceptionClass = props !=
nullptr ? props->getExceptionClass()
213 const std::string message = props !=
nullptr ? props->getInformation() :
"Unknown status";
227 const auto& command =
dynamic_cast<const CalypsoCardCommand&
>(
getCommandRef());
228 const auto statusWord = std::make_shared<int>(
getApduResponse()->getStatusWord());
230 if (exceptionClass ==
typeid(CardAccessForbiddenException)) {
232 throw CardAccessForbiddenException(message, command, statusWord);
234 }
else if (exceptionClass ==
typeid(CardDataAccessException)) {
236 throw CardDataAccessException(message, command, statusWord);
238 }
else if (exceptionClass ==
typeid(CardDataOutOfBoundsException)) {
240 throw CardDataOutOfBoundsException(message, command, statusWord);
242 }
else if (exceptionClass ==
typeid(CardIllegalArgumentException)) {
244 throw CardIllegalArgumentException(message, command);
246 }
else if (exceptionClass ==
typeid(CardIllegalParameterException)) {
248 throw CardIllegalParameterException(message, command, statusWord);
250 }
else if (exceptionClass ==
typeid(CardPinException)) {
252 throw CardPinException(message, command, statusWord);
254 }
else if (exceptionClass ==
typeid(CardSecurityContextException)) {
256 throw CardSecurityContextException(message, command, statusWord);
258 }
else if (exceptionClass ==
typeid(CardSecurityDataException)) {
260 throw CardSecurityDataException(message, command, statusWord);
262 }
else if (exceptionClass ==
typeid(CardSessionBufferOverflowException)) {
264 throw CardSessionBufferOverflowException(message, command, statusWord);
266 }
else if (exceptionClass ==
typeid(CardTerminatedException)) {
268 throw CardTerminatedException(message, command, statusWord);
272 throw CardUnknownStatusException(message, command, statusWord);
275 }
catch (
const std::bad_cast& e) {
278 const auto& command =
dynamic_cast<const CalypsoSamCommand&
>(
getCommandRef());
279 const auto statusWord = std::make_shared<int>(
getApduResponse()->getStatusWord());
281 if (exceptionClass ==
typeid(CalypsoSamAccessForbiddenException)) {
283 throw CalypsoSamAccessForbiddenException(message, command, statusWord);
285 }
else if (exceptionClass ==
typeid(CalypsoSamCounterOverflowException)) {
287 throw CalypsoSamCounterOverflowException(message, command, statusWord);
289 }
else if (exceptionClass ==
typeid(CalypsoSamDataAccessException)) {
291 throw CalypsoSamDataAccessException(message, command, statusWord);
293 }
else if (exceptionClass ==
typeid(CalypsoSamIllegalArgumentException)) {
295 throw CalypsoSamIllegalArgumentException(message, command);
297 }
else if (exceptionClass ==
typeid(CalypsoSamIllegalParameterException)) {
299 throw CalypsoSamIllegalParameterException(message, command, statusWord);
301 }
else if (exceptionClass ==
typeid(CalypsoSamIncorrectInputDataException)) {
303 throw CalypsoSamIncorrectInputDataException(message, command, statusWord);
305 }
else if (exceptionClass ==
typeid(CalypsoSamSecurityDataException)) {
307 throw CalypsoSamSecurityDataException(message, command, statusWord);
311 throw CalypsoSamUnknownStatusException(message, command, statusWord);
318 const std::shared_ptr<StatusProperties> props = getStatusWordProperties();
320 return props !=
nullptr ? props->getInformation() :
"";
StatusProperties(const std::string &information)
const std::string & getInformation() const
const std::type_info & getExceptionClass() const
bool isSuccessful() const
virtual const std::string & getName() const final
static const std::map< const int, const std::shared_ptr< StatusProperties > > STATUS_TABLE
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 bool isSuccessful() 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
AbstractApduCommand(const CardCommand &commandRef, const int expectedResponseLength)
virtual void setExpectedResponseLength(const int expectedResponseLength) final
virtual void parseApduResponse(const std::shared_ptr< ApduResponseApi > apduResponse)
AbstractApduCommand::StatusProperties StatusProperties