Keyple Card Calypso C++ Library 2.2.5.6
Reference Terminal Reader API for C++
CmdCardReadRecords.cpp
Go to the documentation of this file.
1/**************************************************************************************************
2 * Copyright (c) 2023 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 "CmdCardReadRecords.h"
14
15#include <sstream>
16
17/* Keyple Card Calypso */
22
23/* Keyple Core Util */
24#include "ApduUtil.h"
25#include "Arrays.h"
26
27namespace keyple {
28namespace card {
29namespace calypso {
30
31using namespace keyple::core::util;
32using namespace keyple::core::util::cpp;
33
34const CalypsoCardCommand CmdCardReadRecords::mCommand = CalypsoCardCommand::READ_RECORDS;
35const std::map<const int, const std::shared_ptr<StatusProperties>>
36 CmdCardReadRecords::STATUS_TABLE = initStatusTable();
37
38CmdCardReadRecords::CmdCardReadRecords(const std::shared_ptr<CalypsoCardAdapter> calypsoCard,
39 const int sfi,
40 const int firstRecordNumber,
41 const ReadMode readMode,
42 const int expectedLength)
43: AbstractCardCommand(mCommand, expectedLength, calypsoCard),
44 mRecordSize(expectedLength)
45{
46 buildCommand(calypsoCard->getCardClass(), sfi, firstRecordNumber, readMode, expectedLength);
47}
48
50 const uint8_t sfi,
51 const uint8_t firstRecordNumber,
52 const ReadMode readMode,
53 const int expectedLength)
54: AbstractCardCommand(mCommand, expectedLength, nullptr),
55 mRecordSize(expectedLength)
56{
57 buildCommand(calypsoCardClass, sfi, firstRecordNumber, readMode, expectedLength);
58}
59
60void CmdCardReadRecords::buildCommand(const CalypsoCardClass calypsoCardClass,
61 const int sfi,
62 const int firstRecordNumber,
63 const ReadMode readMode,
64 const int expectedLength)
65{
66 mSfi = sfi;
67 mFirstRecordNumber = firstRecordNumber;
68 mReadMode = readMode;
69
70 const uint8_t p1 = static_cast<uint8_t>(firstRecordNumber);
71 uint8_t p2 = sfi == 0x00 ? 0x05 : static_cast<uint8_t>((sfi * 8) + 5);
72
73 if (readMode == ReadMode::ONE_RECORD) {
74
75 p2 -= 0x01;
76 }
77
78 const uint8_t le = expectedLength < 0 ? 0 : expectedLength;
79
80 // APDU Case 2
82 std::make_shared<ApduRequestAdapter>(
83 ApduUtil::build(
84 calypsoCardClass.getValue(), mCommand.getInstructionByte(), p1, p2, le)));
85
86
87 std::stringstream extraInfo;
88 extraInfo << "SFI: " << sfi << "h, "
89 << "REC: " << firstRecordNumber << ", "
90 << "READMODE: " << readMode << ", "
91 << "EXPECTEDLENGTH: " << expectedLength;
92 addSubName(extraInfo.str());
93}
94
96{
97 return false;
98}
99
100const std::map<const int, const std::shared_ptr<StatusProperties>>
101 CmdCardReadRecords::initStatusTable()
102{
103 std::map<const int, const std::shared_ptr<StatusProperties>> m =
105
106 m.insert({0x6981,
107 std::make_shared<StatusProperties>("Command forbidden on binary files",
108 typeid(CardDataAccessException))});
109 m.insert({0x6982,
110 std::make_shared<StatusProperties>("Security conditions not fulfilled (PIN code " \
111 "not presented, encryption required).",
112 typeid(CardSecurityContextException))});
113 m.insert({0x6985,
114 std::make_shared<StatusProperties>("Access forbidden (Never access mode, stored " \
115 "value log file and a stored value operation was" \
116 " done during the current session).",
117 typeid(CardAccessForbiddenException))});
118 m.insert({0x6986,
119 std::make_shared<StatusProperties>("Command not allowed (no current EF)",
120 typeid(CardDataAccessException))});
121 m.insert({0x6A82,
122 std::make_shared<StatusProperties>("File not found",
123 typeid(CardDataAccessException))});
124 m.insert({0x6A83,
125 std::make_shared<StatusProperties>("Record not found (record index is 0, or above " \
126 "NumRec",
127 typeid(CardDataAccessException))});
128 m.insert({0x6B00,
129 std::make_shared<StatusProperties>("P2 value not supported",
130 typeid(CardIllegalParameterException))});
131
132 return m;
133}
134
135const std::map<const int, const std::shared_ptr<StatusProperties>>&
137{
138 return STATUS_TABLE;
139}
140
141void CmdCardReadRecords::parseApduResponse(const std::shared_ptr<ApduResponseApi> apduResponse)
142{
144
146
147 getCalypsoCard()->setContent(mSfi, mFirstRecordNumber, apduResponse->getDataOut());
148
149 } else {
150
151 const std::vector<uint8_t> mApdu = apduResponse->getDataOut();
152 uint8_t apduLen = static_cast<uint8_t>(mApdu.size());
153 uint8_t index = 0;
154
155 while (apduLen > 0) {
156
157 const uint8_t recordNb = mApdu[index++];
158 const uint8_t len = mApdu[index++];
159
160 getCalypsoCard()->setContent(mSfi,
161 recordNb,
162 Arrays::copyOfRange(mApdu, index, index + len));
163
164 index = index + len;
165 apduLen -= (2 + len);
166 }
167 }
168}
169
171{
172 return mSfi;
173}
174
176{
177 return mFirstRecordNumber;
178}
179
181{
182 return mRecordSize;
183}
184
186{
187 return mReadMode;
188}
189
190std::ostream& operator<<(std::ostream& os, const CmdCardReadRecords::ReadMode rm)
191{
192 os << "READ_MODE: ";
193
194 switch (rm) {
196 os << "MULTIPLE_RECORD";
197 break;
199 os << "ONE_RECORD";
200 break;
201 default:
202 os << "UNKNOWN";
203 break;
204 }
205
206 return os;
207}
208
209}
210}
211}
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
std::shared_ptr< CalypsoCardAdapter > getCalypsoCard() const
static const CalypsoCardCommand READ_RECORDS
CmdCardReadRecords(const std::shared_ptr< CalypsoCardAdapter > calypsoCard, const int sfi, const int firstRecordNumber, const ReadMode readMode, const int expectedLength)
const std::map< const int, const std::shared_ptr< StatusProperties > > & getStatusTable() const override
void parseApduResponse(const std::shared_ptr< ApduResponseApi > apduResponse) override
std::ostream & operator<<(std::ostream &os, const std::shared_ptr< ApduRequestAdapter > ara)