14#include "keyple/card/calypso/crypto/legacysam/CommandReadCounter.hpp"
20#include "keyple/card/calypso/crypto/legacysam/CounterOverflowException.hpp"
21#include "keyple/card/calypso/crypto/legacysam/IllegalParameterException.hpp"
22#include "keyple/core/util/ApduUtil.hpp"
23#include "keyple/core/util/ByteArrayUtil.hpp"
31using keyple::core::util::ApduUtil;
32using keyple::core::util::ByteArrayUtil;
34const int CommandReadCounter::SW_DATA_NOT_SIGNED_WARNING = 0x6200;
36const std::map<int, const std::shared_ptr<StatusProperties>>
37 CommandReadCounter::STATUS_TABLE = [] {
38 std::map<int, const std::shared_ptr<StatusProperties>> m(
39 Command::STATUS_TABLE);
43 std::make_shared<StatusProperties>(
44 "An event counter cannot be incremented",
45 typeid(CounterOverflowException))},
47 std::make_shared<StatusProperties>(
48 "Incorrect P2",
typeid(IllegalParameterException))},
50 std::make_shared<StatusProperties>(
51 "Correct execution with warning: data not signed")}});
55CommandReadCounter::CommandReadCounter(
56 std::shared_ptr<DtoAdapters::CommandContextDto> context,
57 int counterFileRecordNumber)
58: Command(CommandRef::READ_EVENT_COUNTER, 48, context)
59, mCounterFileRecordNumber(counterFileRecordNumber)
61 const uint8_t cla = context->getTargetSam()->getClassByte();
62 const uint8_t p2 =
static_cast<uint8_t
>(0xE1 + counterFileRecordNumber);
64 auto adapter = std::make_shared<ApduRequestAdapter>(
65 ApduUtil::build(cla, getCommandRef().getInstructionByte(), 0x00, p2));
66 adapter->addSuccessfulStatusWord(SW_DATA_NOT_SIGNED_WARNING);
67 setApduRequest(adapter);
71CommandReadCounter::getCounterFileRecordNumber()
const
73 return mCounterFileRecordNumber;
77CommandReadCounter::finalizeRequest()
83CommandReadCounter::isControlSamRequiredToFinalizeRequest()
const
89CommandReadCounter::parseResponse(std::shared_ptr<ApduResponseApi> apduResponse)
91 setResponseAndCheckStatus(apduResponse);
93 const std::vector<uint8_t>& dataOut = apduResponse->getDataOut();
94 for (
int i = 0; i < 9; i++) {
95 getContext()->getTargetSam()->putCounterValue(
96 (mCounterFileRecordNumber * 9) + i,
98 ByteArrayUtil::extractInt(dataOut, 8 + (3 * i), 3,
false)));
102const std::map<int, const std::shared_ptr<StatusProperties>>&
103CommandReadCounter::getStatusTable()
const