keyple-card-calypso-crypto-legacysam-cpp-lib - 1.0.2
Component of the Keyple C++ middleware
CommandReadCeilings.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/CommandReadCeilings.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 CommandReadCeilings::SW_DATA_NOT_SIGNED_WARNING = 0x6200;
35
36const std::map<int, const std::shared_ptr<StatusProperties>>
37 CommandReadCeilings::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 P1 or P2", typeid(IllegalParameterException))},
49 {0x6200,
50 std::make_shared<StatusProperties>(
51 "Correct execution with warning: data not signed")}});
52 return m;
53 }();
54
55CommandReadCeilings::CommandReadCeilings(
56 std::shared_ptr<DtoAdapters::CommandContextDto> context,
57 int ceilingFileRecordNumber)
58: Command(CommandRef::READ_CEILINGS, 48, context)
59, mCeilingFileRecordNumber(ceilingFileRecordNumber)
60{
61 const std::uint8_t cla = context->getTargetSam()->getClassByte();
62 const std::uint8_t p1 = 0x00;
63 const std::uint8_t p2
64 = static_cast<std::uint8_t>(0xB1 + ceilingFileRecordNumber);
65
66 auto adapter = std::make_shared<ApduRequestAdapter>(
67 ApduUtil::build(cla, getCommandRef().getInstructionByte(), p1, p2));
68 adapter->addSuccessfulStatusWord(SW_DATA_NOT_SIGNED_WARNING);
69 setApduRequest(adapter);
70}
71
72void
73CommandReadCeilings::finalizeRequest()
74{
75 /* nothing to do */
76}
77
78bool
79CommandReadCeilings::isControlSamRequiredToFinalizeRequest() const
80{
81 return false;
82}
83
84void
85CommandReadCeilings::parseResponse(
86 std::shared_ptr<ApduResponseApi> apduResponse)
87{
88 setResponseAndCheckStatus(apduResponse);
89
90 const std::vector<std::uint8_t> dataOut = apduResponse->getDataOut();
91 std::shared_ptr<LegacySamAdapter> targetSam = getContext()->getTargetSam();
92 const std::uint16_t counterIncrementConfig = static_cast<std::uint16_t>(
93 ByteArrayUtil::extractInt(dataOut, 27, 2, false));
94
95 for (int i = 0; i < 9; i++) {
96 targetSam->putCounterCeilingValue(
97 (mCeilingFileRecordNumber * 9) + i,
98 ByteArrayUtil::extractInt(dataOut, 8 + (3 * i), 3, false));
99
100 targetSam->putCounterIncrementConfiguration(
101 (mCeilingFileRecordNumber * 9) + i,
102 (counterIncrementConfig & (1 << i)) != 0
103 ? CounterIncrementAccess::FREE_COUNTING_ENABLED
104 : CounterIncrementAccess::FREE_COUNTING_DISABLED);
105 }
106}
107
108const std::map<int, const std::shared_ptr<StatusProperties>>&
109CommandReadCeilings::getStatusTable() const
110{
111 return STATUS_TABLE;
112}
113
114} /* namespace legacysam */
115} /* namespace crypto */
116} /* namespace calypso */
117} /* namespace card */
118} /* namespace keyple */