15#include "keyple/card/calypso/CommandGetDataEfList.hpp"
21#include "keyple/card/calypso/CalypsoCardAdapter.hpp"
22#include "keyple/card/calypso/CalypsoCardConstant.hpp"
23#include "keyple/card/calypso/CardDataAccessException.hpp"
24#include "keyple/core/util/ApduUtil.hpp"
25#include "keyple/core/util/ByteArrayUtil.hpp"
26#include "keyple/core/util/cpp/Arrays.hpp"
27#include "keyple/core/util/cpp/exception/IllegalStateException.hpp"
33using keyple::core::util::ApduUtil;
34using keyple::core::util::ByteArrayUtil;
35using keyple::core::util::cpp::Arrays;
36using keyple::core::util::cpp::exception::IllegalStateException;
38const int CommandGetDataEfList::DESCRIPTORS_OFFSET = 2;
39const int CommandGetDataEfList::DESCRIPTOR_DATA_OFFSET = 2;
40const int CommandGetDataEfList::DESCRIPTOR_DATA_SFI_OFFSET = 2;
41const int CommandGetDataEfList::DESCRIPTOR_TAG_LENGTH = 8;
42const int CommandGetDataEfList::DESCRIPTOR_DATA_LENGTH = 6;
44const std::map<int, const std::shared_ptr<Command::StatusProperties>>
45 CommandGetDataEfList::STATUS_TABLE = [] {
46 std::map<int, const std::shared_ptr<Command::StatusProperties>> m(
47 Command::STATUS_TABLE);
51 std::make_shared<StatusProperties>(
52 "Data object not found (optional mode not available)",
53 typeid(CardDataAccessException))},
55 std::make_shared<StatusProperties>(
56 "P1 or P2 value not supported",
57 typeid(CardDataAccessException))}});
61CommandGetDataEfList::CommandGetDataEfList(
62 const std::shared_ptr<DtoAdapters::TransactionContextDto>&
64 const std::shared_ptr<DtoAdapters::CommandContextDto>& commandContext)
65: Command(CardCommandRef::GET_DATA, nullptr, transactionContext, commandContext)
67 const std::uint8_t cardClass
68 = transactionContext->getCard() !=
nullptr
69 ? transactionContext->getCard()->getCardClass().getValue()
70 : CalypsoCardClass::ISO.getValue();
74 std::make_shared<DtoAdapters::ApduRequestAdapter>(ApduUtil::build(
76 getCommandRef().getInstructionByte(),
77 CalypsoCardConstant::TAG_EF_LIST_MSB,
78 CalypsoCardConstant::TAG_EF_LIST_LSB,
81 addSubName(
"EF_LIST");
85CommandGetDataEfList::finalizeRequest()
91CommandGetDataEfList::isCryptoServiceRequiredToFinalizeRequest()
const
97CommandGetDataEfList::synchronizeCryptoServiceBeforeCardProcessing()
103CommandGetDataEfList::parseResponse(
104 std::shared_ptr<ApduResponseApi> apduResponse)
106 Command::setApduResponseAndCheckStatus(apduResponse);
108 const std::map<std::shared_ptr<FileHeaderAdapter>, std::uint8_t>
109 fileHeaderToSfiMap = getEfHeaders();
111 for (
const auto& entry : fileHeaderToSfiMap) {
112 getTransactionContext()->getCard()->setFileHeader(
113 entry.second, entry.first);
117const std::map<int, const std::shared_ptr<Command::StatusProperties>>&
118CommandGetDataEfList::getStatusTable()
const
123std::map<std::shared_ptr<FileHeaderAdapter>, std::uint8_t>
124CommandGetDataEfList::getEfHeaders()
126 const std::vector<std::uint8_t> rawList = getApduResponse()->getDataOut();
127 std::map<std::shared_ptr<FileHeaderAdapter>, std::uint8_t>
129 int nbFiles = rawList[1] / DESCRIPTOR_TAG_LENGTH;
131 for (
int i = 0; i < nbFiles; i++) {
132 fileHeaderToSfiMap.insert(
136 DESCRIPTORS_OFFSET + (i * DESCRIPTOR_TAG_LENGTH)
137 + DESCRIPTOR_DATA_OFFSET,
138 DESCRIPTORS_OFFSET + (i * DESCRIPTOR_TAG_LENGTH)
139 + DESCRIPTOR_DATA_OFFSET + DESCRIPTOR_DATA_LENGTH)),
141 [DESCRIPTORS_OFFSET + (i * DESCRIPTOR_TAG_LENGTH)
142 + DESCRIPTOR_DATA_OFFSET + DESCRIPTOR_DATA_SFI_OFFSET]});
145 return fileHeaderToSfiMap;
148std::shared_ptr<FileHeaderAdapter>
149CommandGetDataEfList::createFileHeader(
150 const std::vector<std::uint8_t>& efDescriptorByteArray)
152 ElementaryFile::Type efType;
154 switch (efDescriptorByteArray[3]) {
155 case CalypsoCardConstant::EF_TYPE_LINEAR:
156 efType = ElementaryFile::Type::LINEAR;
158 case CalypsoCardConstant::EF_TYPE_CYCLIC:
159 efType = ElementaryFile::Type::CYCLIC;
161 case CalypsoCardConstant::EF_TYPE_COUNTERS:
162 efType = ElementaryFile::Type::COUNTERS;
164 case CalypsoCardConstant::EF_TYPE_BINARY:
165 efType = ElementaryFile::Type::BINARY;
167 case CalypsoCardConstant::EF_TYPE_SIMULATED_COUNTERS:
168 efType = ElementaryFile::Type::SIMULATED_COUNTERS;
171 throw IllegalStateException(
172 "Unexpected EF type: " + std::to_string(efDescriptorByteArray[3]));
175 return FileHeaderAdapter::builder()
176 ->lid(ByteArrayUtil::extractShort(efDescriptorByteArray, 0))
178 .recordSize(efDescriptorByteArray[4])
179 .recordsNumber(efDescriptorByteArray[5])