Keyple Card Calypso C++ Library 2.2.2
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 "IllegalArgumentException.h"
23
24namespace keyple {
25namespace card {
26namespace calypso {
27
28using namespace keyple::core::util;
29using namespace keyple::core::util::cpp::exception;
30
31const CalypsoSamCommand CmdSamReadEventCounter::mCommand = CalypsoSamCommand::READ_EVENT_COUNTER;
32const int CmdSamReadEventCounter::MAX_COUNTER_NUMB = 26;
33const int CmdSamReadEventCounter::MAX_COUNTER_REC_NUMB = 3;
34
35const std::map<const int, const std::shared_ptr<StatusProperties>>
36 CmdSamReadEventCounter::STATUS_TABLE = initStatusTable();
37
39 const SamEventCounterOperationType operationType,
40 const int index)
41: AbstractSamCommand(mCommand, 0)
42{
43 const uint8_t cla = SamUtilAdapter::getClassByte(productType);
44
45 uint8_t p2;
46
47 if (operationType == SamEventCounterOperationType::COUNTER_RECORD) {
48 if (index < 1 || index > MAX_COUNTER_REC_NUMB) {
49 throw IllegalArgumentException("Record Number must be between 1 and " +
50 std::to_string(MAX_COUNTER_REC_NUMB) +
51 ".");
52 }
53
54 p2 = static_cast<uint8_t>(0xE0 + index);
55
56 } else {
57 /* SINGLE_COUNTER */
58 if (index < 0 || index > MAX_COUNTER_NUMB) {
59 throw IllegalArgumentException("Counter Number must be between 0 and " +
60 std::to_string(MAX_COUNTER_NUMB) +
61 ".");
62 }
63
64 p2 = static_cast<uint8_t>(0x80 + index);
65 }
66
68 std::make_shared<ApduRequestAdapter>(
69 ApduUtil::build(cla, mCommand.getInstructionByte(), 0x00, p2, 0x00)));
70}
71
72const std::map<const int, const std::shared_ptr<StatusProperties>>
73 CmdSamReadEventCounter::initStatusTable()
74{
75 std::map<const int, const std::shared_ptr<StatusProperties>> m =
77
78 m.insert({0x6900,
79 std::make_shared<StatusProperties>("An event counter cannot be incremented.",
81 m.insert({0x6A00,
82 std::make_shared<StatusProperties>("Incorrect P2.",
83 typeid(CalypsoSamIllegalParameterException))});
84 m.insert({0x6200,
85 std::make_shared<StatusProperties>("Correct execution with warning: data not signed.",
86 typeid(nullptr))});
87
88 return m;
89}
90
91const std::vector<uint8_t> CmdSamReadEventCounter::getCounterData() const
92{
93 return isSuccessful() ? getApduResponse()->getDataOut() : std::vector<uint8_t>();
94}
95
96const std::map<const int, const std::shared_ptr<StatusProperties>>&
98{
99 return STATUS_TABLE;
100}
101
102}
103}
104}
virtual const std::shared_ptr< ApduResponseApi > getApduResponse() const final
virtual void setApduRequest(const std::shared_ptr< ApduRequestAdapter > apduRequest) final
static const std::map< const int, const std::shared_ptr< StatusProperties > > STATUS_TABLE
static const CalypsoSamCommand READ_EVENT_COUNTER
const std::map< const int, const std::shared_ptr< StatusProperties > > & getStatusTable() const override
CmdSamReadEventCounter(const CalypsoSam::ProductType productType, const SamEventCounterOperationType operationType, const int index)
const std::vector< uint8_t > getCounterData() const
static uint8_t getClassByte(const ProductType productType)
CalypsoSam::ProductType ProductType