Keyple Card Calypso C++ Library 2.2.2
Reference Terminal Reader API for C++
CmdCardIncreaseOrDecreaseMultiple.cpp
Go to the documentation of this file.
1
2/**************************************************************************************************
3 * Copyright (c) 2021 Calypso Networks Association https://calypsonet.org/ *
4 * *
5 * See the NOTICE file(s) distributed with this work for additional information regarding *
6 * copyright ownership. *
7 * *
8 * This program and the accompanying materials are made available under the terms of the Eclipse *
9 * Public License 2.0 which is available at http://www.eclipse.org/legal/epl-2.0 *
10 * *
11 * SPDX-License-Identifier: EPL-2.0 *
12 **************************************************************************************************/
13
15
16#include <sstream>
17
18/* Keyple Core Util */
19#include "ApduUtil.h"
20#include "Arrays.h"
21
22/* Keyple Card Calypso */
28
29namespace keyple {
30namespace card {
31namespace calypso {
32
33using namespace keyple::core::util;
34using namespace keyple::core::util::cpp;
35
36const std::map<const int, const std::shared_ptr<StatusProperties>>
37 CmdCardIncreaseOrDecreaseMultiple::STATUS_TABLE = initStatusTable();
38
40 const bool isDecreaseCommand,
41 const CalypsoCardClass calypsoCardClass,
42 const uint8_t sfi,
43 const std::map<const int, const int> counterNumberToIncDecValueMap)
44: AbstractCardCommand(isDecreaseCommand ? CalypsoCardCommand::DECREASE_MULTIPLE :
45 CalypsoCardCommand::INCREASE_MULTIPLE,
46 0),
47 mSfi(sfi),
48 mCounterNumberToIncDecValueMap(counterNumberToIncDecValueMap)
49{
50 const uint8_t p1 = 0;
51 const uint8_t p2 = sfi * 8;
52 std::vector<uint8_t> dataIn(4 * counterNumberToIncDecValueMap.size());
53 int index = 0;
54
55 for (const auto& entry : counterNumberToIncDecValueMap) {
56 dataIn[index] = static_cast<uint8_t>(entry.first);
57 const int incDecValue = entry.second;
58 dataIn[index + 1] = ((incDecValue >> 16) & 0xFF);
59 dataIn[index + 2] = ((incDecValue >> 8) & 0xFF);
60 dataIn[index + 3] = (incDecValue & 0xFF);
61 index += 4;
62 }
63
65 std::make_shared<ApduRequestAdapter>(
66 ApduUtil::build(calypsoCardClass.getValue(),
67 getCommandRef().getInstructionByte(),
68 p1,
69 p2,
70 dataIn,
71 0)));
72
73 std::stringstream extraInfo;
74 extraInfo << "SFI:" << sfi << "h";
75 for (const auto& entry : counterNumberToIncDecValueMap) {
76 extraInfo << ", " << entry.first << ":" << entry.second;
77 }
78
79 addSubName(extraInfo.str());
80}
81
83{
84 return true;
85}
86
87const std::map<const int, const std::shared_ptr<StatusProperties>>
88 CmdCardIncreaseOrDecreaseMultiple::initStatusTable()
89{
90 std::map<const int, const std::shared_ptr<StatusProperties>> m =
92
93 m.insert({0x6400,
94 std::make_shared<StatusProperties>("Too many modifications in session.",
96 m.insert({0x6700,
97 std::make_shared<StatusProperties>("Lc value not supported.",
98 typeid(CardIllegalParameterException))});
99 m.insert({0x6981,
100 std::make_shared<StatusProperties>("Incorrect EF type: not a Counters EF.",
101 typeid(CardDataAccessException))});
102 m.insert({0x6982,
103 std::make_shared<StatusProperties>("Security conditions not fulfilled (no secure " \
104 "session, incorrect key, encryption required, " \
105 "PKI mode and not Always access mode).",
106 typeid(CardSecurityContextException))});
107 m.insert({0x6985,
108 std::make_shared<StatusProperties>("Access forbidden (Never access mode, DF is " \
109 "invalid, etc.).",
110 typeid(CardAccessForbiddenException))});
111 m.insert({0x6986,
112 std::make_shared<StatusProperties>("Incorrect file type: the Current File is not " \
113 "an EF. Supersedes 6981h.",
114 typeid(CardDataAccessException))});
115 m.insert({0x6A80,
116 std::make_shared<StatusProperties>("Incorrect command data (Overflow error, " \
117 "Incorrect counter number, Counter number " \
118 "present more than once).",
119 typeid(CardIllegalParameterException))});
120 m.insert({0x6A82,
121 std::make_shared<StatusProperties>("File not found.",
122 typeid(CardDataAccessException))});
123 m.insert({0x6B00,
124 std::make_shared<StatusProperties>("P1 or P2 value not supported.",
125 typeid(CardIllegalParameterException))});
126
127 return m;
128}
129
130const std::map<const int, const std::shared_ptr<StatusProperties>>&
132{
133 return STATUS_TABLE;
134}
135
137 const std::shared_ptr<ApduResponseApi> apduResponse)
138{
140
141 if (apduResponse->getDataOut().size() > 0) {
142 const std::vector<uint8_t> dataOut = apduResponse->getDataOut();
143 const int nbCounters = static_cast<int>(dataOut.size() / 4);
144 for (int i = 0; i < nbCounters; i++) {
145 mNewCounterValues.insert({dataOut[i * 4],
146 Arrays::copyOfRange(dataOut, (i * 4) + 1, (i * 4) + 4)});
147 }
148 }
149
150 return *this;
151}
152
154{
155 return mSfi;
156}
157
158const std::map<const int, const int>&
160{
161 return mCounterNumberToIncDecValueMap;
162}
163
164const std::map<const uint8_t, const std::vector<uint8_t>>&
166{
167 return mNewCounterValues;
168}
169
170}
171}
172}
static const std::map< const int, const std::shared_ptr< StatusProperties > > STATUS_TABLE
virtual void addSubName(const std::string &subName) final
virtual void setApduRequest(const std::shared_ptr< ApduRequestAdapter > apduRequest) final
const CalypsoCardCommand & getCommandRef() const override
AbstractCardCommand & setApduResponse(const std::shared_ptr< ApduResponseApi > apduResponse) override
CmdCardIncreaseOrDecreaseMultiple & setApduResponse(const std::shared_ptr< ApduResponseApi > apduResponse) override
CmdCardIncreaseOrDecreaseMultiple(const bool isDecreaseCommand, const CalypsoCardClass calypsoCardClass, const uint8_t sfi, const std::map< const int, const int > counterNumberToIncDecValueMap)
const std::map< const int, const int > & getCounterNumberToIncDecValueMap() const
const std::map< const int, const std::shared_ptr< StatusProperties > > & getStatusTable() const override
const std::map< const uint8_t, const std::vector< uint8_t > > & getNewCounterValues() const