Keyple Card Calypso C++ Library 2.1.0
Reference Terminal Reader API for C++
CalypsoSamAdapter.cpp
Go to the documentation of this file.
1/**************************************************************************************************
2 * Copyright (c) 2021 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
13#include "CalypsoSamAdapter.h"
14
15#include <sstream>
16
17/* Keyple Core Util */
18#include "ByteArrayUtil.h"
19#include "IllegalStateException.h"
20#include "Pattern.h"
21#include "System.h"
22
23namespace keyple {
24namespace card {
25namespace calypso {
26
27using namespace keyple::core::util;
28using namespace keyple::core::util::cpp;
29using namespace keyple::core::util::cpp::exception;
30
32 const std::shared_ptr<CardSelectionResponseApi> cardSelectionResponse)
33{
34 /* In the case of a SAM, the power-on data corresponds to the ATR of the card */
35 mPowerOnData = cardSelectionResponse->getPowerOnData();
36 if (mPowerOnData.empty()) {
37 throw IllegalStateException("ATR should not be empty.");
38 }
39
40 mSerialNumber = std::vector<uint8_t>(4);
41
42 /*
43 * Extract the historical bytes from T3 to T12
44 * CL-SAM-ATR.1
45 */
46 const std::string extractRegex = "3B(.{6}|.{10})805A(.{20})829000";
47 std::unique_ptr<Pattern> pattern = Pattern::compile(extractRegex);
48
49 /* To use */
50 std::unique_ptr<Matcher> matcher = pattern->matcher(mPowerOnData);
51 if (matcher->find(0)) {
52 const std::vector<uint8_t> atrSubElements = ByteArrayUtil::fromHex(matcher->group(2));
53 mPlatform = atrSubElements[0];
54 mApplicationType = atrSubElements[1];
55 mApplicationSubType = atrSubElements[2];
56
57 /* Determine SAM product type from Application Subtype */
58 switch (mApplicationSubType) {
59 case 0xC1:
60 mSamProductType = ProductType::SAM_C1;
61 break;
62 case 0xD0:
63 case 0xD1:
64 case 0xD2:
65 mSamProductType = ProductType::SAM_S1DX;
66 break;
67 case 0xE1:
68 mSamProductType = ProductType::SAM_S1E1;
69 break;
70 default:
71 mSamProductType = ProductType::UNKNOWN;
72 break;
73 }
74
75 mSoftwareIssuer = atrSubElements[3];
76 mSoftwareVersion = atrSubElements[4];
77 mSoftwareRevision = atrSubElements[5];
78 System::arraycopy(atrSubElements, 6, mSerialNumber, 0, 4);
79
80 std::stringstream ss;
81 ss << "SAM " << mSamProductType
82 << "PLATFORM = " << mPlatform << ", "
83 << "APPTYPE = " << mApplicationType << "h, "
84 << "APPSUBTYPE = " << mApplicationSubType << "h, "
85 << "SWISSUER = " << mSoftwareIssuer << "h, "
86 << "SWVERSION = " << mSoftwareVersion << "h, "
87 << "SWREVISION = " << mSoftwareRevision;
88 mLogger->trace("%\n", ss.str());
89 mLogger->trace("SAM SERIALNUMBER = %\n", ByteArrayUtil::toHex(mSerialNumber));
90
91 } else {
92 mSamProductType = ProductType::UNKNOWN;
93 mPlatform = 0;
94 mApplicationType = 0;
95 mApplicationSubType = 0;
96 mSoftwareIssuer = 0;
97 mSoftwareVersion = 0;
98 mSoftwareRevision = 0;
99 }
100}
101
103{
104 /* CL-CLA-SAM.1 */
105 if (type == CalypsoSam::ProductType::SAM_S1DX ||
106 type == CalypsoSam::ProductType::CSAM_F) {
107 return 0x94;
108 }
109
110 return 0x80;
111}
112
114{
115 return getClassByte(mSamProductType);
116}
117
119{
120 switch (mSamProductType) {
121 case CalypsoSam::ProductType::SAM_C1:
122 return 255;
123 case CalypsoSam::ProductType::SAM_S1DX:
124 return 70;
125 case CalypsoSam::ProductType::SAM_S1E1:
126 return 240;
127 case CalypsoSam::ProductType::CSAM_F:
128 return 247;
129 default:
130 return 0;
131 }
132}
133
134const std::vector<uint8_t> CalypsoSamAdapter::getSelectApplicationResponse() const
135{
136 return std::vector<uint8_t>(0);
137}
138
139const std::string& CalypsoSamAdapter::getPowerOnData() const
140{
141 return mPowerOnData;
142}
143
145{
146 return mSamProductType;
147}
148
149const std::string CalypsoSamAdapter::getProductInfo() const
150{
151 std::stringstream ss;
152 ss << "Type: " << getProductType() << ", S/N: " << ByteArrayUtil::toHex(getSerialNumber());
153
154 return ss.str();
155}
156
157const std::vector<uint8_t>& CalypsoSamAdapter::getSerialNumber() const
158{
159 return mSerialNumber;
160}
161
163{
164 return mPlatform;
165}
166
168{
169 return mApplicationType;
170}
171
173{
174 return mApplicationSubType;
175}
176
178{
179 return mSoftwareIssuer;
180}
181
183{
184 return mSoftwareVersion;
185}
186
188{
189 return mSoftwareRevision;
190}
191
192}
193}
194}
const std::string & getPowerOnData() const override
CalypsoSamAdapter(const std::shared_ptr< CardSelectionResponseApi > cardSelectionResponse)
const std::vector< uint8_t > getSelectApplicationResponse() const override
const std::string getProductInfo() const override
const std::vector< uint8_t > & getSerialNumber() const final
CalypsoSam::ProductType getProductType() const final
CalypsoSam::ProductType ProductType