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

Commit 5064e12

Browse files
committed
Initial commit
Signed-off-by: Mart Somermaa <[email protected]>
1 parent 0edd30e commit 5064e12

23 files changed

+2612
-0
lines changed

.clang-format

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
BasedOnStyle: WebKit
2+
3+
Standard: Cpp11
4+
ColumnLimit: 100
5+
6+
# Disable reflow of qdoc comments: indentation rules are different.
7+
# Translation comments are also excluded
8+
CommentPragmas: "^!|^:"
9+
10+
# We want & and * to be attached to the type
11+
PointerBindsToType: true
12+
13+
# We want long lines to break before the operators, but not before a '='
14+
BreakBeforeBinaryOperators: NonAssignment
15+
16+
# Braces are usually attached, but not after functions or classes declaration
17+
BreakBeforeBraces: Custom
18+
BraceWrapping:
19+
AfterClass: true
20+
AfterControlStatement: false
21+
AfterEnum: false
22+
AfterFunction: true
23+
AfterNamespace: true
24+
AfterObjCDeclaration: false
25+
AfterStruct: true
26+
AfterUnion: false
27+
BeforeCatch: false
28+
BeforeElse: false
29+
IndentBraces: false
30+
31+
AlignAfterOpenBracket: true
32+
AlwaysBreakTemplateDeclarations: true
33+
AllowShortFunctionsOnASingleLine: Inline
34+
SpaceInEmptyBlock: false
35+
FixNamespaceComments: true
36+
Cpp11BracedListStyle: true
37+
BreakConstructorInitializers: AfterColon
38+
39+
SortIncludes: false
40+
41+
ForEachMacros: [ foreach, Q_FOREACH, BOOST_FOREACH, forever, Q_FOREVER, QBENCHMARK, QBENCHMARK_ONCE ]

.gitattributes

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Assures that line endings are consistently normalized.
2+
#
3+
# Comments:
4+
# - use filter='...' to pass through external filters, e.g. clang-format
5+
6+
# --- Source files
7+
*.c text
8+
*.cc text
9+
*.cpp text
10+
*.cxx text
11+
*.h text
12+
*.hh text
13+
*.hpp text
14+
*.ipp text
15+
16+
*.js text
17+
*.pl text
18+
*.py text
19+
20+
# --- Resource files
21+
*.rc text eol=crlf
22+
*.rc2 text eol=crlf
23+
24+
# --- Windows and VS-specific files
25+
*.bat text eol=crlf
26+
*.cmd text eol=crlf
27+
*.cpy text eol=crlf
28+
*.ddf text eol=crlf
29+
*.def text eol=crlf
30+
*.inf text eol=crlf
31+
*.msbuild text eol=crlf
32+
*.sln text eol=crlf
33+
*.vcproj text eol=crlf
34+
*.vcxproj text eol=crlf
35+
36+
# --- Text files
37+
*.htm text
38+
*.html text
39+
*.md text
40+
*.properties text
41+
*.rst text
42+
*.txt text
43+
*.xml text
44+
45+
# --- Binary files
46+
*.bmp binary
47+
*.dll binary
48+
*.exe binary
49+
*.gif binary
50+
*.ico binary
51+
*.jpg binary
52+
*.lib binary
53+
*.png binary
54+
55+
# --- git files
56+
.gitignore text eol=lf
57+
.gitattributes text eol=lf
58+
.clang-format text eol=lf
59+
60+
# --- Makefiles
61+
Makefile text eol=lf
62+
Makefile.dep text eol=lf
63+
64+
# --- CMake files
65+
CMakeLists.txt text
66+
67+
# --- Shell scripts, git hooks
68+
*.sh text eol=lf
69+
commit-msg text eol=lf
70+
pre-commit text eol=lf

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
*.swp
2+
tags
3+
build/
4+
.vscode/

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "tests/lib/libpcsc-mock"]
2+
path = tests/lib/libpcsc-mock
3+
url = https://github.com/web-eid/libpcsc-mock

CMakeLists.txt

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
cmake_minimum_required(VERSION 3.8.0)
2+
if(POLICY CMP0092)
3+
cmake_policy(SET CMP0092 NEW)
4+
endif()
5+
6+
project(pcsc-cpp)
7+
8+
# The libpcsc-cpp library itself.
9+
10+
add_library(${PROJECT_NAME}
11+
STATIC
12+
include/${PROJECT_NAME}/${PROJECT_NAME}.hpp
13+
include/${PROJECT_NAME}/comp_winscard.hpp
14+
include/flag-set-cpp/flag_set.hpp
15+
include/magic_enum/magic_enum.hpp
16+
src/Context.hpp
17+
src/Reader.cpp
18+
src/SCardCall.hpp
19+
src/SmartCard.cpp
20+
src/listReaders.cpp
21+
src/utils.cpp
22+
)
23+
24+
target_include_directories(${PROJECT_NAME}
25+
PUBLIC
26+
include
27+
)
28+
29+
target_compile_features(${PROJECT_NAME}
30+
PUBLIC
31+
cxx_std_17
32+
)
33+
34+
target_compile_options(${PROJECT_NAME} PUBLIC
35+
$<$<CXX_COMPILER_ID:MSVC>:/W4 /WX>
36+
$<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-Wall -Wextra -pedantic -Werror>
37+
)
38+
39+
target_compile_definitions(${PROJECT_NAME} PUBLIC
40+
$<$<CXX_COMPILER_ID:MSVC>:WIN32_LEAN_AND_MEAN;UNICODE>
41+
)
42+
43+
target_link_libraries(${PROJECT_NAME} PRIVATE
44+
$<$<CXX_COMPILER_ID:MSVC>:Ws2_32>
45+
)
46+
47+
# Common compile options.
48+
49+
set(CMAKE_CXX_EXTENSIONS OFF)
50+
51+
# PC/SC API dependencies.
52+
53+
add_library(pcsc INTERFACE)
54+
if(MSVC)
55+
# no extra includes required
56+
target_link_libraries(pcsc INTERFACE winscard)
57+
elseif(APPLE)
58+
# no extra includes required
59+
target_link_libraries(pcsc INTERFACE "-framework PCSC")
60+
else()
61+
find_package(PkgConfig)
62+
pkg_check_modules(PCSC libpcsclite)
63+
target_include_directories(${PROJECT_NAME} PRIVATE ${PCSC_INCLUDE_DIRS})
64+
target_link_libraries(pcsc INTERFACE ${PCSC_LIBRARIES})
65+
endif()
66+
67+
# Common testing options.
68+
69+
enable_testing()
70+
71+
find_package(GTest REQUIRED)
72+
73+
# Mock tests that use libpcsc-mock to mock PC/SC API calls.
74+
75+
set(MOCK_TEST_EXE lib${PROJECT_NAME}-test-mock)
76+
77+
add_subdirectory(tests/lib/libpcsc-mock)
78+
79+
add_executable(${MOCK_TEST_EXE}
80+
tests/mock/test-select-card-reader-and-card.cpp
81+
tests/mock/test-connect-to-card-transmit-apdus.cpp
82+
)
83+
84+
target_link_libraries(${MOCK_TEST_EXE}
85+
${PROJECT_NAME}
86+
pcsc-mock
87+
GTest::Main
88+
)
89+
90+
add_test(${MOCK_TEST_EXE} ${MOCK_TEST_EXE})
91+
92+
# Integration tests that use the real operating system PC/SC service.
93+
94+
set(INTEGRATION_TEST_EXE lib${PROJECT_NAME}-test-integration)
95+
96+
add_executable(${INTEGRATION_TEST_EXE}
97+
tests/integration/test-${PROJECT_NAME}.cpp
98+
)
99+
100+
target_link_libraries(${INTEGRATION_TEST_EXE}
101+
${PROJECT_NAME}
102+
pcsc
103+
GTest::Main
104+
)

README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# libpcsc-cpp
2+
3+
C++ library for accessing smart cards using the PC/SC API.
4+
5+
## Usage
6+
7+
Example how to list available readers, connect to the smart card in first
8+
reader and transmit an APDU:
9+
10+
auto readers = listReaders();
11+
auto card = readers[0].connectToCard();
12+
auto command = CommandApdu::fromBytes({0x2, 0x1, 0x3, 0x4});
13+
14+
auto transactionGuard = card->beginTransaction();
15+
auto response = card->transmit(command);
16+
17+
See more examples in [tests](tests).
18+
19+
## Building
20+
21+
In Ubuntu:
22+
23+
apt install build-essential pkg-config cmake libgtest-dev valgrind libpcsclite-dev
24+
sudo bash -c 'cd /usr/src/googletest && cmake . && cmake --build . --target install'
25+
26+
cd build
27+
cmake .. # optionally with -DCMAKE_BUILD_TYPE=Debug
28+
cmake --build . # optionally with VERBOSE=1
29+
30+
## Testing
31+
32+
Build as described above, then, inside the `build` directory, run:
33+
34+
ctest # or 'valgrind --leak-check=full ctest'
35+
36+
`ctest` runs tests that use the _libscard-mock_ library to mock PC/SC API calls.
37+
38+
There are also integration tests that use the real operating system PC/SC
39+
service, run them inside `build` directory with:
40+
41+
./libpcsc-cpp-test-integration
42+
43+
## Development guidelines
44+
45+
- Format code with `scripts/clang-format.sh` before committing
46+
- See [docs/DEVELOPMENT.md](docs/DEVELOPMENT.md)

docs/DEVELOPMENT.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
TODO:
2+
3+
- coding conventions - Qt coding style and conventions
4+
- gtest and winscard-mock usage
5+
- API
6+
- architecture
7+
- technical design
8+
- describe PIMPL (we don't want to leak `winscard.h` to clients)

0 commit comments

Comments
 (0)