Keyple Card Calypso C++ Library 2.1.0
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 mSfi(sfi),
47 mCounterNumberToIncDecValueMap(counterNumberToIncDecValueMap)
48{
49 const uint8_t p1 = 0;
50 const uint8_t p2 = sfi * 8;
51 std::vector<uint8_t> dataIn(4 * counterNumberToIncDecValueMap.size());
52 int index = 0;
53
54 for (const auto& entry : counterNumberToIncDecValueMap) {
55 dataIn[index] = static_cast<uint8_t>(entry.first);
56 const int incDecValue = entry.second;
57 dataIn[index + 1] = ((incDecValue >> 16) & 0xFF);
58 dataIn[index + 2] = ((incDecValue >> 8) & 0xFF);
59 dataIn[index + 3] = (incDecValue & 0xFF);
60 index += 4;
61 }
62
64 std::make_shared<ApduRequestAdapter>(
65 ApduUtil::build(calypsoCardClass.getValue(),
66 getCommandRef().getInstructionByte(),
67 p1,
68 p2,
69 dataIn,
70 0)));
71
72 std::stringstream extraInfo;
73 extraInfo << "SFI:" << sfi << "h";
74 for (const auto& entry : counterNumberToIncDecValueMap) {
75 extraInfo << ", " << entry.first << ":" << entry.second;
76 }
77
78 addSubName(extraInfo.str());
79}
80
82{
83 return true;
84}
85
86const std::map<const int, const std::shared_ptr<StatusProperties>>
87 CmdCardIncreaseOrDecreaseMultiple::initStatusTable()
88{
89 std::map<const int, const std::shared_ptr<StatusProperties>> m =
91
92 m.insert({0x6400,
93 std::make_shared<StatusProperties>("Too many modifications in session.",
95 m.insert({0x6700,
96 std::make_shared<StatusProperties>("Lc value not supported.",
97 typeid(CardIllegalParameterException))});
98 m.insert({0x6981,
99 std::make_shared<StatusProperties>("Incorrect EF type: not a Counters EF.",
100 typeid(CardDataAccessException))});
101 m.insert({0x6982,
102 std::make_shared<StatusProperties>("Security conditions not fulfilled (no secure " \
103 "session, incorrect key, encryption required, " \
104 "PKI mode and not Always access mode).",
105 typeid(CardSecurityContextException))});
106 m.insert({0x6985,
107 std::make_shared<StatusProperties>("Access forbidden (Never access mode, DF is " \
108 "invalid, etc.).",
109 typeid(CardAccessForbiddenException))});
110 m.insert({0x6986,
111 std::make_shared<StatusProperties>("Incorrect file type: the Current File is not " \
112 "an EF. Supersedes 6981h.",
113 typeid(CardDataAccessException))});
114 m.insert({0x6A80,
115 std::make_shared<StatusProperties>("Incorrect command data (Overflow error, " \
116 "Incorrect counter number, Counter number " \
117 "present more than once).",
118 typeid(CardIllegalParameterException))});
119 m.insert({0x6A82,
120 std::make_shared<StatusProperties>("File not found.",
121 typeid(CardDataAccessException))});
122 m.insert({0x6B00,
123 std::make_shared<StatusProperties>("P1 or P2 value not supported.",
124 typeid(CardIllegalParameterException))});
125
126 return m;
127}
128
129const std::map<const int, const std::shared_ptr<StatusProperties>>&
131{
132 return STATUS_TABLE;
133}
134
136 const std::shared_ptr<ApduResponseApi> apduResponse)
137{
139
140 if (apduResponse->getDataOut().size() > 0) {
141 const std::vector<uint8_t> dataOut = apduResponse->getDataOut();
142 const int nbCounters = static_cast<int>(dataOut.size() / 4);
143 for (int i = 0; i < nbCounters; i++) {
144 mNewCounterValues.insert({dataOut[i * 4],
145 Arrays::copyOfRange(dataOut, (i * 4) + 1, (i * 4) + 4)});
146 }
147 }
148
149 return *this;
150}
151
153{
154 return mSfi;
155}
156
157const std::map<const int, const int>&
159{
160 return mCounterNumberToIncDecValueMap;
161}
162
163const std::map<const uint8_t, const std::vector<uint8_t>>&
165{
166 return mNewCounterValues;
167}
168
169}
170}
171}
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