keyple-card-calypso-crypto-legacysam-cpp-lib - 1.0.2
Component of the Keyple C++ middleware
CommandReadCounter.cpp
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (c) 2025 Calypso Networks Association https://calypsonet.org/ *
3 * *
4 * See the NOTICE file(s) distributed with this work for additional *
5 * information regarding copyright ownership. *
6 * *
7 * This program and the accompanying materials are made available under the *
8 * terms of the Eclipse Public License 2.0 which is available at *
9 * http://www.eclipse.org/legal/epl-2.0 *
10 * *
11 * SPDX-License-Identifier: EPL-2.0 *
12 ******************************************************************************/
13
14#include "keyple/card/calypso/crypto/legacysam/CommandReadCounter.hpp"
15
16#include <map>
17#include <memory>
18#include <vector>
19
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"
24
25namespace keyple {
26namespace card {
27namespace calypso {
28namespace crypto {
29namespace legacysam {
30
31using keyple::core::util::ApduUtil;
32using keyple::core::util::ByteArrayUtil;
33
34const int CommandReadCounter::SW_DATA_NOT_SIGNED_WARNING = 0x6200;
35
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);
40
41 m.insert(
42 {{0x6900,
43 std::make_shared<StatusProperties>(
44 "An event counter cannot be incremented",
45 typeid(CounterOverflowException))},
46 {0x6A00,
47 std::make_shared<StatusProperties>(
48 "Incorrect P2", typeid(IllegalParameterException))},
49 {0x6200,
50 std::make_shared<StatusProperties>(
51 "Correct execution with warning: data not signed")}});
52 return m;
53 }();
54
55CommandReadCounter::CommandReadCounter(
56 std::shared_ptr<DtoAdapters::CommandContextDto> context,
57 int counterFileRecordNumber)
58: Command(CommandRef::READ_EVENT_COUNTER, 48, context)
59, mCounterFileRecordNumber(counterFileRecordNumber)
60{
61 const uint8_t cla = context->getTargetSam()->getClassByte();
62 const uint8_t p2 = static_cast<uint8_t>(0xE1 + counterFileRecordNumber);
63
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);
68}
69
70int
71CommandReadCounter::getCounterFileRecordNumber() const
72{
73 return mCounterFileRecordNumber;
74}
75
76void
77CommandReadCounter::finalizeRequest()
78{
79 /* nothing to do */
80}
81
82bool
83CommandReadCounter::isControlSamRequiredToFinalizeRequest() const
84{
85 return false;
86}
87
88void
89CommandReadCounter::parseResponse(std::shared_ptr<ApduResponseApi> apduResponse)
90{
91 setResponseAndCheckStatus(apduResponse);
92
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,
97 static_cast<int>(
98 ByteArrayUtil::extractInt(dataOut, 8 + (3 * i), 3, false)));
99 }
100}
101
102const std::map<int, const std::shared_ptr<StatusProperties>>&
103CommandReadCounter::getStatusTable() const
104{
105 return STATUS_TABLE;
106}
107
108} /* namespace legacysam */
109} /* namespace crypto */
110} /* namespace calypso */
111} /* namespace card */
112} /* namespace keyple */