Keyple Plugin PC/SC C++ Library - 2.5.2
Component of the Keyple C++ middleware
Card.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/plugin/pcsc/cpp/Card.hpp"
15
16#include "PcscUtils.hpp"
17
18#include "keyple/plugin/pcsc/cpp/exception/CardException.hpp"
19
20namespace keyple {
21namespace plugin {
22namespace pcsc {
23namespace cpp {
24
25using keyple::plugin::pcsc::cpp::exception::CardException;
26
27Card::Card(
28 const std::shared_ptr<CardTerminal> cardTerminal,
29 const SCARDHANDLE handle,
30 const std::vector<uint8_t> atr,
31 const DWORD protocol,
32 const SCARD_IO_REQUEST ioRequest)
33: mProtocol(protocol)
34, mIORequest(ioRequest)
35, mHandle(handle)
36, mAtr(atr)
37, mCardTerminal(cardTerminal)
38{
39
40}
41
42const std::vector<uint8_t>&
43Card::getATR() const
44{
45 return mAtr;
46}
47
48void
49Card::beginExclusive()
50{
51 SCardBeginTransaction(mHandle);
52}
53
54void
55Card::endExclusive()
56{
57 SCardEndTransaction(mHandle, SCARD_LEAVE_CARD);
58}
59
60void
61Card::disconnect(const bool reset)
62{
63 SCardDisconnect(mHandle, reset ? SCARD_RESET_CARD : SCARD_LEAVE_CARD);
64}
65
66const std::string
67Card::getProtocol() const
68{
69 switch (mProtocol) {
70 case SCARD_PROTOCOL_T0:
71 return "T=0";
72 case SCARD_PROTOCOL_T1:
73 return "T=1";
74 default:
75 return "DIRECT";
76 }
77}
78
79std::shared_ptr<CardChannel>
80Card::getBasicChannel()
81{
82 return std::make_shared<CardChannel>(shared_from_this(), 0);
83}
84
85const std::vector<uint8_t>
86Card::transmitControlCommand(
87 const int commandId, const std::vector<uint8_t>& command)
88{
89 char r_apdu[261];
90 DWORD dwRecv = sizeof(r_apdu);
91
92 LONG rv = SCardControl(
93 mHandle,
94 (DWORD)commandId,
95 (LPCBYTE)command.data(),
96 (DWORD)command.size(),
97 (LPBYTE)r_apdu,
98 (DWORD)sizeof(r_apdu),
99 &dwRecv);
100 if (rv != SCARD_S_SUCCESS) {
101 mLogger->error(
102 "SCardControl failed with error: %\n",
103 std::string(pcsc_stringify_error(rv)));
104 throw CardException("SCardControl failed");
105 }
106
107 std::vector<uint8_t> response(r_apdu, r_apdu + dwRecv);
108
109 return response;
110}
111
112} /* namespace cpp */
113} /* namespace pcsc */
114} /* namespace plugin */
115} /* namespace keyple */
Definition: Card.cpp:20