Skip to content
This repository has been archived by the owner on May 16, 2024. It is now read-only.

Commit

Permalink
refactor(SmartCard): add null object constructor
Browse files Browse the repository at this point in the history
WE2-716

Signed-off-by: Mart Somermaa <[email protected]>
  • Loading branch information
mrts committed Nov 4, 2022
1 parent d85c1f3 commit e7d702b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
7 changes: 7 additions & 0 deletions include/pcsc-cpp/pcsc-cpp-utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,10 @@ inline std::string removeAbsolutePathPrefix(const std::string& filePath)

#define THROW(ExceptionType, message) \
THROW_WITH_CALLER_INFO(ExceptionType, message, __FILE__, __LINE__, __func__)

#define REQUIRE_NON_NULL(val) \
if (!val) { \
throw std::logic_error("Null " + std::string(#val) + " in " \
+ pcsc_cpp::removeAbsolutePathPrefix(__FILE__) + ':' \
+ std::to_string(__LINE__) + ':' + __func__); \
}
3 changes: 2 additions & 1 deletion include/pcsc-cpp/pcsc-cpp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ constexpr uint8_t PIN_PAD_PIN_ENTRY_TIMEOUT = 90; // 1 minute, 30 seconds
class SmartCard
{
public:
enum class Protocol { T0, T1 }; // AUTO = T0 | T1
enum class Protocol { UNDEFINED, T0, T1 }; // AUTO = T0 | T1

using ptr = std::unique_ptr<SmartCard>;

Expand All @@ -229,6 +229,7 @@ class SmartCard
};

SmartCard(const ContextPtr& context, const string_t& readerName, byte_vector atr);
SmartCard(); // Null object constructor.
~SmartCard();

// The rule of five.
Expand Down
6 changes: 5 additions & 1 deletion src/SmartCard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -273,20 +273,23 @@ SmartCard::SmartCard(const ContextPtr& contex, const string_t& readerName, byte_
// TODO: debug("Card ATR -> " + bytes2hexstr(atr));
}

SmartCard::SmartCard() = default;
SmartCard::~SmartCard() = default;

SmartCard::TransactionGuard SmartCard::beginTransaction()
{
REQUIRE_NON_NULL(card);
return SmartCard::TransactionGuard {*card, transactionInProgress};
}

bool SmartCard::readerHasPinPad() const
{
return card->readerHasPinPad();
return card ? card->readerHasPinPad() : false;
}

ResponseApdu SmartCard::transmit(const CommandApdu& command) const
{
REQUIRE_NON_NULL(card);
if (!transactionInProgress) {
THROW(std::logic_error, "Call SmartCard::transmit() inside a transaction");
}
Expand All @@ -296,6 +299,7 @@ ResponseApdu SmartCard::transmit(const CommandApdu& command) const

ResponseApdu SmartCard::transmitCTL(const CommandApdu& command, uint16_t lang, uint8_t minlen) const
{
REQUIRE_NON_NULL(card);
if (!transactionInProgress) {
THROW(std::logic_error, "Call SmartCard::transmit() inside a transaction");
}
Expand Down

0 comments on commit e7d702b

Please sign in to comment.