Keyple Card Calypso C++ Library 2.2.5.6
Reference Terminal Reader API for C++
CmdCardUpdateOrWriteBinary.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#include <sstream>
16
17/* Keyple Core Util */
18#include "ApduUtil.h"
19#include "Arrays.h"
20#include "System.h"
21
22/* Keyple Card Calypso */
29
30namespace keyple {
31namespace card {
32namespace calypso {
33
34using namespace keyple::core::util;
35using namespace keyple::core::util::cpp;
36
37const std::map<const int, const std::shared_ptr<StatusProperties>>
38 CmdCardUpdateOrWriteBinary::STATUS_TABLE = initStatusTable();
39
41 const bool isUpdateCommand,
42 const std::shared_ptr<CalypsoCardAdapter> calypsoCard,
43 const uint8_t sfi,
44 const int offset,
45 const std::vector<uint8_t>& data)
46: AbstractCardCommand(isUpdateCommand ? CalypsoCardCommand::UPDATE_BINARY :
47 CalypsoCardCommand::WRITE_BINARY,
48 0,
49 calypsoCard),
50 mSfi(sfi),
51 mOffset(offset),
52 mData(data)
53{
54 const uint8_t msb = (offset & 0xFF00) >> 8;
55 const uint8_t lsb = (offset & 0xFF);
56
57 /*
58 * 100xxxxx : 'xxxxx' = SFI of the EF to select.
59 * 0xxxxxxx : 'xxxxxxx' = MSB of the offset of the first byte.
60 */
61 const uint8_t p1 = msb > 0 ? msb : 0x80 + sfi;
62
63 // APDU Case 3
65 std::make_shared<ApduRequestAdapter>(
66 ApduUtil::build(calypsoCard->getCardClass().getValue(),
67 getCommandRef().getInstructionByte(),
68 p1,
69 lsb,
70 data)));
71
72
73
74 std::stringstream extraInfo;
75 extraInfo << "SFI:" << sfi << "h, "
76 << "OFFSET:" << offset;
77
78 addSubName(extraInfo.str());
79}
80
81void CmdCardUpdateOrWriteBinary::parseApduResponse(std::shared_ptr<ApduResponseApi> apduResponse)
82{
84
86
87 getCalypsoCard()->setContent(mSfi, 1, mData, mOffset);
88
89 } else {
90
91 getCalypsoCard()->fillContent(mSfi, 1, mData, mOffset);
92 }
93}
94
96{
97 return true;
98}
99
100const std::map<const int, const std::shared_ptr<StatusProperties>>
101 CmdCardUpdateOrWriteBinary::initStatusTable()
102{
103 std::map<const int, const std::shared_ptr<StatusProperties>> m =
105
106 m.insert({0x6400,
107 std::make_shared<StatusProperties>("Too many modifications in session",
109 m.insert({0x6700,
110 std::make_shared<StatusProperties>("Lc value not supported, or Offset+Lc > file size",
111 typeid(CardDataAccessException))});
112 m.insert({0x6981,
113 std::make_shared<StatusProperties>("Incorrect EF type: not a Binary EF",
114 typeid(CardDataAccessException))});
115 m.insert({0x6982,
116 std::make_shared<StatusProperties>("Security conditions not fulfilled (no secure " \
117 "session, incorrect key, encryption required, " \
118 "PKI mode and not Always access mode)",
119 typeid(CardSecurityContextException))});
120 m.insert({0x6985,
121 std::make_shared<StatusProperties>("Access forbidden (Never access mode, DF is " \
122 "invalidated, etc..)",
123 typeid(CardAccessForbiddenException))});
124 m.insert({0x6986,
125 std::make_shared<StatusProperties>("Incorrect file type: the Current File is not " \
126 "an EF. Supersedes 6981h",
127 typeid(CardDataAccessException))});
128 m.insert({0x6A82,
129 std::make_shared<StatusProperties>("File not found",
130 typeid(CardDataAccessException))});
131 m.insert({0x6A83,
132 std::make_shared<StatusProperties>("Offset not in the file (offset overflow)",
133 typeid(CardDataAccessException))});
134 m.insert({0x6B00,
135 std::make_shared<StatusProperties>("P1 value not supported",
136 typeid(CardIllegalParameterException))});
137
138 return m;
139}
140
141const std::map<const int, const std::shared_ptr<StatusProperties>>&
143{
144 return STATUS_TABLE;
145}
146
147}
148}
149}
static const std::map< const int, const std::shared_ptr< StatusProperties > > STATUS_TABLE
virtual void addSubName(const std::string &subName) final
virtual void setApduRequest(const std::shared_ptr< ApduRequestAdapter > apduRequest) final
void parseApduResponse(const std::shared_ptr< ApduResponseApi > apduResponse) override
const CalypsoCardCommand & getCommandRef() const override
std::shared_ptr< CalypsoCardAdapter > getCalypsoCard() const
static const CalypsoCardCommand UPDATE_BINARY
void parseApduResponse(std::shared_ptr< ApduResponseApi > apduResponse) override
CmdCardUpdateOrWriteBinary(const bool isUpdateCommand, const std::shared_ptr< CalypsoCardAdapter > calypsoCard, const uint8_t sfi, const int offset, const std::vector< uint8_t > &data)
const std::map< const int, const std::shared_ptr< StatusProperties > > & getStatusTable() const override