Keyple Card Calypso C++ Library 2.2.5.6
Reference Terminal Reader API for C++
CmdSamReadEventCounter.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 */
18#include "SamUtilAdapter.h"
19
20/* Keyple Core Util */
21#include "ApduUtil.h"
22#include "ByteArrayUtil.h"
23#include "IllegalArgumentException.h"
24
25namespace keyple {
26namespace card {
27namespace calypso {
28
29using namespace keyple::core::util;
30using namespace keyple::core::util::cpp::exception;
31
32const CalypsoSamCommand CmdSamReadEventCounter::mCommand = CalypsoSamCommand::READ_EVENT_COUNTER;
33
34const std::map<const int, const std::shared_ptr<StatusProperties>>
35 CmdSamReadEventCounter::STATUS_TABLE = initStatusTable();
36
37CmdSamReadEventCounter::CmdSamReadEventCounter(std::shared_ptr<CalypsoSamAdapter> calypsoSam,
38 const CounterOperationType counterOperationType,
39 const int target)
40: AbstractSamCommand(mCommand, 48, calypsoSam),
41 mCounterOperationType(counterOperationType),
42 mFirstEventCounterNumber(counterOperationType == CounterOperationType::READ_SINGLE_COUNTER ?
43 target : (target - 1) * 9)
44{
45 const uint8_t cla = SamUtilAdapter::getClassByte(calypsoSam->getProductType());
46
47 uint8_t p2;
48
49 if (counterOperationType == CounterOperationType::READ_SINGLE_COUNTER) {
50
51 p2 = static_cast<uint8_t>(0x81 + target);
52
53 } else {
54
55 p2 = static_cast<uint8_t>(0xE0 + target);
56 }
57
59 std::make_shared<ApduRequestAdapter>(
60 ApduUtil::build(cla, mCommand.getInstructionByte(), 0x00, p2, 0x00)));
61}
62
63const std::map<const int, const std::shared_ptr<StatusProperties>>
64 CmdSamReadEventCounter::initStatusTable()
65{
66 std::map<const int, const std::shared_ptr<StatusProperties>> m =
68
69 m.insert({0x6900,
70 std::make_shared<StatusProperties>("An event counter cannot be incremented.",
72 m.insert({0x6A00,
73 std::make_shared<StatusProperties>("Incorrect P2.",
74 typeid(CalypsoSamIllegalParameterException))});
75 m.insert({0x6200,
76 std::make_shared<StatusProperties>("Correct execution with warning: data not signed.",
77 typeid(nullptr))});
78
79 return m;
80}
81
82const std::map<const int, const std::shared_ptr<StatusProperties>>&
84{
85 return STATUS_TABLE;
86}
87
88void CmdSamReadEventCounter::parseApduResponse(std::shared_ptr<ApduResponseApi> apduResponse)
89{
91
92 if (isSuccessful()) {
93
94 const std::vector<uint8_t> dataOut = apduResponse->getDataOut();
95
96 if (mCounterOperationType == CounterOperationType::READ_SINGLE_COUNTER) {
97
98 getCalypsoSam()->putEventCounter(dataOut[8],
99 ByteArrayUtil::extractInt(dataOut, 9, 3, false));
100
101 } else {
102
103 for (int i = 0; i < 9; i++) {
104
105 getCalypsoSam()->putEventCounter(
106 mFirstEventCounterNumber + i,
107 ByteArrayUtil::extractInt(dataOut, 8 + (3 * i), 3, false));
108 }
109 }
110 }
111}
112
113}
114}
115}
virtual void setApduRequest(const std::shared_ptr< ApduRequestAdapter > apduRequest) final
static const std::map< const int, const std::shared_ptr< StatusProperties > > STATUS_TABLE
const std::shared_ptr< CalypsoSamAdapter > getCalypsoSam() const
void parseApduResponse(const std::shared_ptr< ApduResponseApi > apduResponse) override
static const CalypsoSamCommand READ_EVENT_COUNTER
void parseApduResponse(std::shared_ptr< ApduResponseApi > apduResponse) override
const std::map< const int, const std::shared_ptr< StatusProperties > > & getStatusTable() const override
CmdSamReadEventCounter(std::shared_ptr< CalypsoSamAdapter > calypsoSam, const CounterOperationType counterOperationType, const int target)
static uint8_t getClassByte(const ProductType productType)