Skip to content

Commit 98a58e0

Browse files
committed
Add high level EMV library interface and refactor generated headers
* Generate emv_utils_config.h for build configuration to contain project version string, platform checks, and paths required by iso-codes and mcc-codes. This replaces isocodes_config.h and mcc_config.h. * Add emv.h, emv.c, implement emv_lib_version_string(), and update emv-decode and emv-tool accordingly.
1 parent 55ea7ed commit 98a58e0

File tree

11 files changed

+95
-36
lines changed

11 files changed

+95
-36
lines changed

src/CMakeLists.txt

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ if(NOT HAVE_TIME_H)
1414
message(FATAL_ERROR "Failed to find time.h")
1515
endif()
1616
include(CheckSymbolExists)
17-
check_symbol_exists(clock_gettime time.h HAVE_clock_gettime)
18-
if(NOT HAVE_clock_gettime)
17+
check_symbol_exists(clock_gettime time.h HAVE_CLOCK_GETTIME)
18+
if(NOT HAVE_CLOCK_GETTIME)
1919
message(FATAL_ERROR "Failed to find clock_gettime")
2020
endif()
2121

@@ -63,6 +63,10 @@ else()
6363
message(FATAL_ERROR "mcc-codes/mcc_codes.json not found")
6464
endif()
6565

66+
# Generate emv-utils build configuration for internal use only
67+
# This file should NOT be installed or used by an installed header
68+
configure_file(emv_utils_config.h.in emv_utils_config.h)
69+
6670
# ISO 7816 library
6771
add_library(iso7816
6872
iso7816.c
@@ -175,6 +179,7 @@ install(
175179

176180
# EMV library
177181
add_library(emv
182+
emv.c
178183
emv_fields.c
179184
emv_tlv.c
180185
emv_dol.c
@@ -184,6 +189,7 @@ add_library(emv
184189
emv_tal.c
185190
)
186191
set(emv_HEADERS # PUBLIC_HEADER property requires a list instead of individual entries
192+
emv.h
187193
emv_tags.h
188194
emv_fields.h
189195
emv_tlv.h
@@ -208,9 +214,12 @@ set_target_properties(emv
208214
VERSION ${CMAKE_PROJECT_VERSION}
209215
SOVERSION ${CMAKE_PROJECT_VERSION_MAJOR}.${CMAKE_PROJECT_VERSION_MINOR}
210216
)
211-
target_include_directories(emv INTERFACE
212-
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
213-
$<INSTALL_INTERFACE:include/emv>
217+
target_include_directories(emv
218+
PRIVATE
219+
"${CMAKE_CURRENT_BINARY_DIR}" # For generated headers
220+
INTERFACE
221+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
222+
$<INSTALL_INTERFACE:include/emv>
214223
)
215224
install(
216225
TARGETS
@@ -251,8 +260,6 @@ set_target_properties(emv_strings
251260
VERSION ${CMAKE_PROJECT_VERSION}
252261
SOVERSION ${CMAKE_PROJECT_VERSION_MAJOR}.${CMAKE_PROJECT_VERSION_MINOR}
253262
)
254-
configure_file(isocodes_config.h.in isocodes_config.h)
255-
configure_file(mcc_config.h.in mcc_config.h)
256263
target_include_directories(emv_strings
257264
PRIVATE
258265
"${CMAKE_CURRENT_BINARY_DIR}" # For generated headers

src/mcc_config.h.in renamed to src/emv.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
2-
* @file mcc_config.h
3-
* @brief Definitions related to the configuration of mcc-codes
2+
* @file emv.c
3+
* @brief High level EMV library interface
44
*
55
* Copyright (c) 2023 Leon Lynch
66
*
@@ -19,10 +19,10 @@
1919
* <https://www.gnu.org/licenses/>.
2020
*/
2121

22-
#ifndef MCC_CONFIG_H
23-
#define MCC_CONFIG_H
22+
#include "emv.h"
23+
#include "emv_utils_config.h"
2424

25-
#define MCC_JSON_BUILD_PATH "@MCC_JSON_BUILD_PATH@"
26-
#define MCC_JSON_INSTALL_PATH "@MCC_JSON_INSTALL_PATH@"
27-
28-
#endif
25+
const char* emv_lib_version_string(void)
26+
{
27+
return EMV_UTILS_VERSION_STRING;
28+
}

src/isocodes_config.h.in renamed to src/emv.h

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/**
2-
* @file isocodes_config.h
3-
* @brief Definitions related to the configuration of the iso-codes package
2+
* @file emv.h
3+
* @brief High level EMV library interface
44
*
5-
* Copyright (c) 2021 Leon Lynch
5+
* Copyright (c) 2023 Leon Lynch
66
*
77
* This library is free software; you can redistribute it and/or
88
* modify it under the terms of the GNU Lesser General Public
@@ -19,10 +19,19 @@
1919
* <https://www.gnu.org/licenses/>.
2020
*/
2121

22-
#ifndef ISOCODES_CONFIG_H
23-
#define ISOCODES_CONFIG_H
22+
#ifndef EMV_H
23+
#define EMV_H
2424

25-
#define ISOCODES_PREFIX "@IsoCodes_PREFIX@"
26-
#define ISOCODES_JSON_PATH "@IsoCodes_JSON_PATH@"
25+
#include <sys/cdefs.h>
26+
27+
__BEGIN_DECLS
28+
29+
/**
30+
* Retrieve EMV library version string
31+
* @return Pointer to null-terminated string. Do not free.
32+
*/
33+
const char* emv_lib_version_string(void);
34+
35+
__END_DECLS
2736

2837
#endif

src/emv_strings.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
#include "emv_fields.h"
2626
#include "isocodes_lookup.h"
2727
#include "mcc_lookup.h"
28-
#include "mcc_config.h"
2928

3029
#include <string.h>
3130
#include <stdarg.h>

src/emv_utils_config.h.in

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* @file emv_utils_config.h
3+
* @brief Definitions related to emv-utils build configuration
4+
*
5+
* Copyright (c) 2023 Leon Lynch
6+
*
7+
* This library is free software; you can redistribute it and/or
8+
* modify it under the terms of the GNU Lesser General Public
9+
* License as published by the Free Software Foundation; either
10+
* version 2.1 of the License, or (at your option) any later version.
11+
*
12+
* This library is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
* Lesser General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Lesser General Public
18+
* License along with this library. If not, see
19+
* <https://www.gnu.org/licenses/>.
20+
*/
21+
22+
#ifndef EMV_UTILS_CONFIG_H
23+
#define EMV_UTILS_CONFIG_H
24+
25+
#cmakedefine EMV_UTILS_VERSION_STRING "@EMV_UTILS_VERSION_STRING@"
26+
#cmakedefine HAVE_TIME_H
27+
#cmakedefine HAVE_CLOCK_GETTIME
28+
29+
// For iso-codes
30+
#define ISOCODES_JSON_PATH "@IsoCodes_JSON_PATH@"
31+
32+
// For mcc-codes
33+
#cmakedefine MCC_JSON_BUILD_PATH "@MCC_JSON_BUILD_PATH@"
34+
#cmakedefine MCC_JSON_INSTALL_PATH "@MCC_JSON_INSTALL_PATH@"
35+
36+
#endif

src/isocodes_lookup.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
*/
2121

2222
#include "isocodes_lookup.h"
23-
#include "isocodes_config.h"
23+
#include "emv_utils_config.h"
2424

2525
#include <string>
2626
#include <vector>

src/mcc_lookup.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
*/
2121

2222
#include "mcc_lookup.h"
23-
#include "mcc_config.h"
23+
#include "emv_utils_config.h"
2424

2525
#include <string>
2626
#include <map>

tests/mcc_test.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
*/
2121

2222
#include "mcc_lookup.h"
23-
#include "mcc_config.h"
23+
#include "emv_utils_config.h"
2424

2525
#include <stdio.h>
2626
#include <string.h>

tools/CMakeLists.txt

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,6 @@ target_link_libraries(print_helpers PRIVATE iso7816 emv emv_strings)
5757
# EMV decode command line tool
5858
if(BUILD_EMV_DECODE)
5959
add_executable(emv-decode emv-decode.c)
60-
set_source_files_properties(emv-decode.c
61-
PROPERTIES
62-
COMPILE_DEFINITIONS EMV_UTILS_VERSION_STRING="${EMV_UTILS_VERSION_STRING}"
63-
)
6460
target_link_libraries(emv-decode PRIVATE print_helpers iso7816 emv emv_strings iso8859)
6561
if(TARGET libargp::argp)
6662
target_link_libraries(emv-decode PRIVATE libargp::argp)
@@ -85,10 +81,6 @@ if(BUILD_EMV_TOOL)
8581
endif()
8682

8783
add_executable(emv-tool emv-tool.c ../src/pcsc.c)
88-
set_source_files_properties(emv-tool.c
89-
PROPERTIES
90-
COMPILE_DEFINITIONS EMV_UTILS_VERSION_STRING="${EMV_UTILS_VERSION_STRING}"
91-
)
9284
target_link_libraries(emv-tool PRIVATE print_helpers iso7816 emv emv_strings)
9385
if(TARGET libargp::argp)
9486
target_link_libraries(emv-tool PRIVATE libargp::argp)

tools/emv-decode.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
* <https://www.gnu.org/licenses/>.
2020
*/
2121

22+
#include "emv.h"
2223
#include "iso7816.h"
2324
#include "print_helpers.h"
2425
#include "emv_strings.h"
@@ -286,7 +287,14 @@ static error_t argp_parser_helper(int key, char* arg, struct argp_state* state)
286287
}
287288

288289
case EMV_DECODE_VERSION: {
289-
printf("%s\n", EMV_UTILS_VERSION_STRING);
290+
const char* version;
291+
292+
version = emv_lib_version_string();
293+
if (version) {
294+
printf("%s\n", version);
295+
} else {
296+
printf("Unknown\n");
297+
}
290298
exit(EXIT_SUCCESS);
291299
return 0;
292300
}

tools/emv-tool.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
* <https://www.gnu.org/licenses/>.
2020
*/
2121

22+
#include "emv.h"
2223
#include "pcsc.h"
2324
#include "iso7816.h"
2425
#include "print_helpers.h"
@@ -255,7 +256,14 @@ static error_t argp_parser_helper(int key, char* arg, struct argp_state* state)
255256
}
256257

257258
case EMV_TOOL_VERSION: {
258-
printf("%s\n", EMV_UTILS_VERSION_STRING);
259+
const char* version;
260+
261+
version = emv_lib_version_string();
262+
if (version) {
263+
printf("%s\n", version);
264+
} else {
265+
printf("Unknown\n");
266+
}
259267
exit(EXIT_SUCCESS);
260268
return 0;
261269
}

0 commit comments

Comments
 (0)