Keypop Card C++ API - 2.0.1
Component of the Keypop C++ API
ApduRequestSpi.hpp
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (c) 2025 Calypso Networks Association https://calypsonet.org/ *
3 * *
4 * This program and the accompanying materials are made available under the *
5 * terms of the MIT License which is available at *
6 * https://opensource.org/licenses/MIT. *
7 * *
8 * SPDX-License-Identifier: MIT *
9 ******************************************************************************/
10
11#pragma once
12
13#include <cstdint>
14#include <iomanip>
15#include <memory>
16#include <ostream>
17#include <sstream>
18#include <string>
19#include <vector>
20
21namespace keypop {
22namespace card {
23namespace spi {
24
32public:
36 virtual ~ApduRequestSpi() = default;
37
44 virtual const std::vector<std::uint8_t>& getApdu() const = 0;
45
49 virtual void setApdu(const std::vector<std::uint8_t>& apdu) = 0;
50
58 virtual const std::vector<int>& getSuccessfulStatusWords() const = 0;
59
68 virtual const std::string& getInfo() const = 0;
69
73 friend std::ostream&
74 operator<<(std::ostream& os, const ApduRequestSpi& ars) {
75 const std::vector<uint8_t>& apdu = ars.getApdu();
76 const std::vector<int> sw = ars.getSuccessfulStatusWords();
77
78 std::stringstream ssApdu;
79 for (const auto val : apdu) {
80 ssApdu << std::uppercase << std::hex << std::setfill('0')
81 << std::setw(2) << static_cast<int>(val);
82 }
83
84 std::stringstream ssSw;
85 for (auto it = std::begin(sw); it != std::end(sw); ++it) {
86 ssSw << std::uppercase << std::hex << std::setfill('0')
87 << std::setw(4) << static_cast<int>(*it);
88 if (it != sw.end() - 1) {
89 ssSw << ", ";
90 }
91 }
92
93 os << "APDU_REQUEST_SPI: {"
94 << "APDU: " << ssApdu.str() << ", "
95 << "SUCCESSFUL_STATUS_WORD: " << ssSw.str() << ", "
96 << "INFO: " << ars.getInfo() << "}";
97
98 return os;
99 }
100
104 friend std::ostream&
105 operator<<(std::ostream& os, const std::shared_ptr<ApduRequestSpi> ars) {
106 os << *ars.get();
107
108 return os;
109 }
110
114 friend std::ostream&
116 std::ostream& os,
117 const std::vector<std::shared_ptr<ApduRequestSpi>>& arss) {
118 os << "APDU_REQUEST_SPIS: {";
119
120 for (auto it = std::begin(arss); it != std::end(arss); ++it) {
121 os << *it;
122 if (it != arss.end() - 1) {
123 os << ", ";
124 }
125 }
126
127 os << "}";
128
129 return os;
130 }
131};
132
133} /* namespace spi */
134} /* namespace card */
135} /* namespace keypop */
Data to build a single APDU command to be sent to a card.
virtual ~ApduRequestSpi()=default
Virtual destructor.
virtual const std::string & getInfo() const =0
Gets the information about this APDU request (e.g.
virtual const std::vector< int > & getSuccessfulStatusWords() const =0
Gets the list of status words that must be considered successful for the APDU.
virtual void setApdu(const std::vector< std::uint8_t > &apdu)=0
C++.
friend std::ostream & operator<<(std::ostream &os, const ApduRequestSpi &ars)
virtual const std::vector< std::uint8_t > & getApdu() const =0
Gets the APDU bytes to be sent to the card.