Keyple Card Calypso C++ Library 2.2.5.6
Reference Terminal Reader API for C++
CmdCardGetDataEfList.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
14
15/* Keyple Card Calypso */
16#include "ApduRequestAdapter.h"
17#include "CalypsoCardConstant.h"
19#include "FileHeaderAdapter.h"
20
21/* Keyple Core Util */
22#include "ApduUtil.h"
23#include "Arrays.h"
24#include "ByteArrayUtil.h"
25#include "IllegalStateException.h"
26
27namespace keyple {
28namespace card {
29namespace calypso {
30
31using namespace keyple::core::util;
32using namespace keyple::core::util::cpp;
33
34const int CmdCardGetDataEfList::DESCRIPTORS_OFFSET = 2;
35const int CmdCardGetDataEfList::DESCRIPTOR_DATA_OFFSET = 2;
36const int CmdCardGetDataEfList::DESCRIPTOR_DATA_SFI_OFFSET = 2;
37const int CmdCardGetDataEfList::DESCRIPTOR_TAG_LENGTH = 8;
38const int CmdCardGetDataEfList::DESCRIPTOR_DATA_LENGTH = 6;
39const CalypsoCardCommand CmdCardGetDataEfList::mCommand = CalypsoCardCommand::GET_DATA;
40const std::map<const int, const std::shared_ptr<StatusProperties>>
41 CmdCardGetDataEfList::STATUS_TABLE = initStatusTable();
42
44: AbstractCardCommand(mCommand, -1, nullptr)
45{
46 buildCommand(calypsoCardClass);
47}
48
49CmdCardGetDataEfList::CmdCardGetDataEfList(const std::shared_ptr<CalypsoCardAdapter> calypsoCard)
50: AbstractCardCommand(mCommand, -1, calypsoCard)
51{
52 buildCommand(calypsoCard->getCardClass());
53}
54
55void CmdCardGetDataEfList::buildCommand(const CalypsoCardClass calypsoCardClass)
56{
57 // APDU Case 2 - always outside secure session
59 std::make_shared<ApduRequestAdapter>(
60 ApduUtil::build(calypsoCardClass.getValue(),
61 mCommand.getInstructionByte(),
62 0x00,
63 0xC0,
64 0x00)));
65}
66
67void CmdCardGetDataEfList::parseApduResponse(const std::shared_ptr<ApduResponseApi> apduResponse)
68{
70
71 const std::map<const std::shared_ptr<FileHeaderAdapter>, const uint8_t> fileHeaderToSfiMap =
73
74 for (const auto& entry : fileHeaderToSfiMap) {
75
76 getCalypsoCard()->setFileHeader(entry.second, entry.first);
77 }
78}
79
81{
82 return false;
83}
84
85const std::map<const int, const std::shared_ptr<StatusProperties>>
86 CmdCardGetDataEfList::initStatusTable()
87{
88 std::map<const int, const std::shared_ptr<StatusProperties>> m =
90
91 m.insert({0x6A88,
92 std::make_shared<StatusProperties>("Data object not found (optional mode not " \
93 "available).",
94 typeid(CardDataAccessException))});
95 m.insert({0x6B00,
96 std::make_shared<StatusProperties>("P1 or P2 value not supported.",
97 typeid(CardDataAccessException))});
98
99 return m;
100}
101
102const std::map<const int, const std::shared_ptr<StatusProperties>>&
104{
105 return STATUS_TABLE;
106}
107
108const std::map<const std::shared_ptr<FileHeaderAdapter>, const uint8_t>
110{
111 const std::vector<uint8_t> rawList = getApduResponse()->getDataOut();
112 std::map<const std::shared_ptr<FileHeaderAdapter>, const uint8_t> fileHeaderToSfiMap;
113 const int nbFiles = rawList[1] / DESCRIPTOR_TAG_LENGTH;
114
115 for (int i = 0; i < nbFiles; i++) {
116 fileHeaderToSfiMap.insert({
117 createFileHeader(
118 Arrays::copyOfRange(
119 rawList,
120 DESCRIPTORS_OFFSET + (i * DESCRIPTOR_TAG_LENGTH) + DESCRIPTOR_DATA_OFFSET,
121 DESCRIPTORS_OFFSET
122 + (i * DESCRIPTOR_TAG_LENGTH)
123 + DESCRIPTOR_DATA_OFFSET
124 + DESCRIPTOR_DATA_LENGTH)),
125 rawList[
126 DESCRIPTORS_OFFSET
127 + (i * DESCRIPTOR_TAG_LENGTH)
128 + DESCRIPTOR_DATA_OFFSET
129 + DESCRIPTOR_DATA_SFI_OFFSET]});
130 }
131
132 return fileHeaderToSfiMap;
133}
134
135const std::shared_ptr<FileHeaderAdapter> CmdCardGetDataEfList::createFileHeader(
136 const std::vector<uint8_t>& efDescriptorByteArray) const
137{
138 ElementaryFile::Type efType;
139
140 if (efDescriptorByteArray[3] == CalypsoCardConstant::EF_TYPE_LINEAR) {
141
142 efType = ElementaryFile::Type::LINEAR;
143
144 } else if (efDescriptorByteArray[3] == CalypsoCardConstant::EF_TYPE_CYCLIC) {
145
146 efType = ElementaryFile::Type::CYCLIC;
147
148 } else if (efDescriptorByteArray[3] == CalypsoCardConstant::EF_TYPE_COUNTERS) {
149
150 efType = ElementaryFile::Type::COUNTERS;
151
152 } else if (efDescriptorByteArray[3] == CalypsoCardConstant::EF_TYPE_BINARY) {
153
154 efType = ElementaryFile::Type::BINARY;
155
156 } else if (efDescriptorByteArray[3] == CalypsoCardConstant::EF_TYPE_SIMULATED_COUNTERS) {
157
158 efType = ElementaryFile::Type::SIMULATED_COUNTERS;
159
160 } else {
161
162 throw IllegalStateException("Unexpected EF type");
163 }
164
166 ->lid(ByteArrayUtil::extractShort(efDescriptorByteArray, 0))
167 .type(efType)
168 .recordSize(efDescriptorByteArray[4])
169 .recordsNumber(efDescriptorByteArray[5])
170 .build();
171}
172
173}
174}
175}
static const std::map< const int, const std::shared_ptr< StatusProperties > > STATUS_TABLE
virtual const std::shared_ptr< ApduResponseApi > getApduResponse() const 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 GET_DATA
CmdCardGetDataEfList(const CalypsoCardClass calypsoCardClass)
void parseApduResponse(const std::shared_ptr< ApduResponseApi > apduResponse) override
const std::map< const int, const std::shared_ptr< StatusProperties > > & getStatusTable() const override
const std::map< const std::shared_ptr< FileHeaderAdapter >, const uint8_t > getEfHeaders() const
static std::shared_ptr< FileHeaderBuilder > builder()