Keyple Card Calypso C++ Library 2.2.5.6
Reference Terminal Reader API for C++
CmdCardIncreaseOrDecreaseMultiple.cpp
Go to the documentation of this file.
1
2/**************************************************************************************************
3 * Copyright (c) 2023 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#include "ByteArrayUtil.h"
22
23/* Keyple Card Calypso */
29
30namespace keyple {
31namespace card {
32namespace calypso {
33
34using namespace keyple::core::util;
35using namespace keyple::core::util::cpp;
36
37const std::map<const int, const std::shared_ptr<StatusProperties>>
38 CmdCardIncreaseOrDecreaseMultiple::STATUS_TABLE = initStatusTable();
39
41 const bool isDecreaseCommand,
42 const std::shared_ptr<CalypsoCardAdapter> calypsoCard,
43 const uint8_t sfi,
44 const std::map<const int, const int> counterNumberToIncDecValueMap)
45: AbstractCardCommand(isDecreaseCommand ? CalypsoCardCommand::DECREASE_MULTIPLE :
46 CalypsoCardCommand::INCREASE_MULTIPLE,
47 counterNumberToIncDecValueMap.size() * 4,
48 calypsoCard),
49 mSfi(sfi),
50 mCounterNumberToIncDecValueMap(counterNumberToIncDecValueMap)
51{
52 const uint8_t p1 = 0;
53 const uint8_t p2 = sfi * 8;
54 std::vector<uint8_t> dataIn(4 * counterNumberToIncDecValueMap.size());
55 int index = 0;
56
57 for (const auto& entry : counterNumberToIncDecValueMap) {
58
59 dataIn[index] = static_cast<uint8_t>(entry.first);
60 const int incDecValue = entry.second;
61 ByteArrayUtil::copyBytes(incDecValue, dataIn, index + 1, 3);
62 index += 4;
63 }
64
65 // APDU Case 4
67 std::make_shared<ApduRequestAdapter>(
68 ApduUtil::build(calypsoCard->getCardClass().getValue(),
69 getCommandRef().getInstructionByte(),
70 p1,
71 p2,
72 dataIn,
73 0)));
74
75 std::stringstream extraInfo;
76 extraInfo << "SFI:" << sfi << "h";
77
78 for (const auto& entry : counterNumberToIncDecValueMap) {
79
80 extraInfo << ", " << entry.first << ":" << entry.second;
81 }
82
83 addSubName(extraInfo.str());
84}
85
87{
88 return true;
89}
90
91const std::map<const int, const std::shared_ptr<StatusProperties>>
92 CmdCardIncreaseOrDecreaseMultiple::initStatusTable()
93{
94 std::map<const int, const std::shared_ptr<StatusProperties>> m =
96
97 m.insert({0x6400,
98 std::make_shared<StatusProperties>("Too many modifications in session.",
100 m.insert({0x6700,
101 std::make_shared<StatusProperties>("Lc value not supported.",
102 typeid(CardIllegalParameterException))});
103 m.insert({0x6981,
104 std::make_shared<StatusProperties>("Incorrect EF type: not a Counters EF.",
105 typeid(CardDataAccessException))});
106 m.insert({0x6982,
107 std::make_shared<StatusProperties>("Security conditions not fulfilled (no secure " \
108 "session, incorrect key, encryption required, " \
109 "PKI mode and not Always access mode).",
110 typeid(CardSecurityContextException))});
111 m.insert({0x6985,
112 std::make_shared<StatusProperties>("Access forbidden (Never access mode, DF is " \
113 "invalid, etc.).",
114 typeid(CardAccessForbiddenException))});
115 m.insert({0x6986,
116 std::make_shared<StatusProperties>("Incorrect file type: the Current File is not " \
117 "an EF. Supersedes 6981h.",
118 typeid(CardDataAccessException))});
119 m.insert({0x6A80,
120 std::make_shared<StatusProperties>("Incorrect command data (Overflow error, " \
121 "Incorrect counter number, Counter number " \
122 "present more than once).",
123 typeid(CardIllegalParameterException))});
124 m.insert({0x6A82,
125 std::make_shared<StatusProperties>("File not found.",
126 typeid(CardDataAccessException))});
127 m.insert({0x6B00,
128 std::make_shared<StatusProperties>("P1 or P2 value not supported.",
129 typeid(CardIllegalParameterException))});
130
131 return m;
132}
133
134const std::map<const int, const std::shared_ptr<StatusProperties>>&
136{
137 return STATUS_TABLE;
138}
139
141 const std::shared_ptr<ApduResponseApi> apduResponse)
142{
144
145 if (apduResponse->getDataOut().size() > 0) {
146
147 const std::vector<uint8_t> dataOut = apduResponse->getDataOut();
148 const int nbCounters = static_cast<int>(dataOut.size() / 4);
149
150 for (int i = 0; i < nbCounters; i++) {
151
152 getCalypsoCard()->setCounter(mSfi,
153 dataOut[i * 4] & 0xFF,
154 Arrays::copyOfRange(dataOut, (i * 4) + 1, (i * 4) + 4));
155 }
156 }
157}
158
160{
161 return mSfi;
162}
163
164const std::map<const int, const int>&
166{
167 return mCounterNumberToIncDecValueMap;
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
void parseApduResponse(const std::shared_ptr< ApduResponseApi > apduResponse) override
const CalypsoCardCommand & getCommandRef() const override
std::shared_ptr< CalypsoCardAdapter > getCalypsoCard() const
const std::map< const int, const int > & getCounterNumberToIncDecValueMap() const
const std::map< const int, const std::shared_ptr< StatusProperties > > & getStatusTable() const override
void parseApduResponse(const std::shared_ptr< ApduResponseApi > apduResponse) override
CmdCardIncreaseOrDecreaseMultiple(const bool isDecreaseCommand, const std::shared_ptr< CalypsoCardAdapter > calypsoCard, const uint8_t sfi, const std::map< const int, const int > counterNumberToIncDecValueMap)