Skip to content

Commit

Permalink
Add high level EMV library interface and refactor generated headers
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
leonlynch committed Mar 15, 2023
1 parent 55ea7ed commit 98a58e0
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 36 deletions.
21 changes: 14 additions & 7 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ if(NOT HAVE_TIME_H)
message(FATAL_ERROR "Failed to find time.h")
endif()
include(CheckSymbolExists)
check_symbol_exists(clock_gettime time.h HAVE_clock_gettime)
if(NOT HAVE_clock_gettime)
check_symbol_exists(clock_gettime time.h HAVE_CLOCK_GETTIME)
if(NOT HAVE_CLOCK_GETTIME)
message(FATAL_ERROR "Failed to find clock_gettime")
endif()

Expand Down Expand Up @@ -63,6 +63,10 @@ else()
message(FATAL_ERROR "mcc-codes/mcc_codes.json not found")
endif()

# Generate emv-utils build configuration for internal use only
# This file should NOT be installed or used by an installed header
configure_file(emv_utils_config.h.in emv_utils_config.h)

# ISO 7816 library
add_library(iso7816
iso7816.c
Expand Down Expand Up @@ -175,6 +179,7 @@ install(

# EMV library
add_library(emv
emv.c
emv_fields.c
emv_tlv.c
emv_dol.c
Expand All @@ -184,6 +189,7 @@ add_library(emv
emv_tal.c
)
set(emv_HEADERS # PUBLIC_HEADER property requires a list instead of individual entries
emv.h
emv_tags.h
emv_fields.h
emv_tlv.h
Expand All @@ -208,9 +214,12 @@ set_target_properties(emv
VERSION ${CMAKE_PROJECT_VERSION}
SOVERSION ${CMAKE_PROJECT_VERSION_MAJOR}.${CMAKE_PROJECT_VERSION_MINOR}
)
target_include_directories(emv INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
$<INSTALL_INTERFACE:include/emv>
target_include_directories(emv
PRIVATE
"${CMAKE_CURRENT_BINARY_DIR}" # For generated headers
INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
$<INSTALL_INTERFACE:include/emv>
)
install(
TARGETS
Expand Down Expand Up @@ -251,8 +260,6 @@ set_target_properties(emv_strings
VERSION ${CMAKE_PROJECT_VERSION}
SOVERSION ${CMAKE_PROJECT_VERSION_MAJOR}.${CMAKE_PROJECT_VERSION_MINOR}
)
configure_file(isocodes_config.h.in isocodes_config.h)
configure_file(mcc_config.h.in mcc_config.h)
target_include_directories(emv_strings
PRIVATE
"${CMAKE_CURRENT_BINARY_DIR}" # For generated headers
Expand Down
16 changes: 8 additions & 8 deletions src/mcc_config.h.in → src/emv.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* @file mcc_config.h
* @brief Definitions related to the configuration of mcc-codes
* @file emv.c
* @brief High level EMV library interface
*
* Copyright (c) 2023 Leon Lynch
*
Expand All @@ -19,10 +19,10 @@
* <https://www.gnu.org/licenses/>.
*/

#ifndef MCC_CONFIG_H
#define MCC_CONFIG_H
#include "emv.h"
#include "emv_utils_config.h"

#define MCC_JSON_BUILD_PATH "@MCC_JSON_BUILD_PATH@"
#define MCC_JSON_INSTALL_PATH "@MCC_JSON_INSTALL_PATH@"

#endif
const char* emv_lib_version_string(void)
{
return EMV_UTILS_VERSION_STRING;
}
23 changes: 16 additions & 7 deletions src/isocodes_config.h.in → src/emv.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/**
* @file isocodes_config.h
* @brief Definitions related to the configuration of the iso-codes package
* @file emv.h
* @brief High level EMV library interface
*
* Copyright (c) 2021 Leon Lynch
* Copyright (c) 2023 Leon Lynch
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
Expand All @@ -19,10 +19,19 @@
* <https://www.gnu.org/licenses/>.
*/

#ifndef ISOCODES_CONFIG_H
#define ISOCODES_CONFIG_H
#ifndef EMV_H
#define EMV_H

#define ISOCODES_PREFIX "@IsoCodes_PREFIX@"
#define ISOCODES_JSON_PATH "@IsoCodes_JSON_PATH@"
#include <sys/cdefs.h>

__BEGIN_DECLS

/**
* Retrieve EMV library version string
* @return Pointer to null-terminated string. Do not free.
*/
const char* emv_lib_version_string(void);

__END_DECLS

#endif
1 change: 0 additions & 1 deletion src/emv_strings.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
#include "emv_fields.h"
#include "isocodes_lookup.h"
#include "mcc_lookup.h"
#include "mcc_config.h"

#include <string.h>
#include <stdarg.h>
Expand Down
36 changes: 36 additions & 0 deletions src/emv_utils_config.h.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* @file emv_utils_config.h
* @brief Definitions related to emv-utils build configuration
*
* Copyright (c) 2023 Leon Lynch
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see
* <https://www.gnu.org/licenses/>.
*/

#ifndef EMV_UTILS_CONFIG_H
#define EMV_UTILS_CONFIG_H

#cmakedefine EMV_UTILS_VERSION_STRING "@EMV_UTILS_VERSION_STRING@"
#cmakedefine HAVE_TIME_H
#cmakedefine HAVE_CLOCK_GETTIME

// For iso-codes
#define ISOCODES_JSON_PATH "@IsoCodes_JSON_PATH@"

// For mcc-codes
#cmakedefine MCC_JSON_BUILD_PATH "@MCC_JSON_BUILD_PATH@"
#cmakedefine MCC_JSON_INSTALL_PATH "@MCC_JSON_INSTALL_PATH@"

#endif
2 changes: 1 addition & 1 deletion src/isocodes_lookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*/

#include "isocodes_lookup.h"
#include "isocodes_config.h"
#include "emv_utils_config.h"

#include <string>
#include <vector>
Expand Down
2 changes: 1 addition & 1 deletion src/mcc_lookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*/

#include "mcc_lookup.h"
#include "mcc_config.h"
#include "emv_utils_config.h"

#include <string>
#include <map>
Expand Down
2 changes: 1 addition & 1 deletion tests/mcc_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*/

#include "mcc_lookup.h"
#include "mcc_config.h"
#include "emv_utils_config.h"

#include <stdio.h>
#include <string.h>
Expand Down
8 changes: 0 additions & 8 deletions tools/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,6 @@ target_link_libraries(print_helpers PRIVATE iso7816 emv emv_strings)
# EMV decode command line tool
if(BUILD_EMV_DECODE)
add_executable(emv-decode emv-decode.c)
set_source_files_properties(emv-decode.c
PROPERTIES
COMPILE_DEFINITIONS EMV_UTILS_VERSION_STRING="${EMV_UTILS_VERSION_STRING}"
)
target_link_libraries(emv-decode PRIVATE print_helpers iso7816 emv emv_strings iso8859)
if(TARGET libargp::argp)
target_link_libraries(emv-decode PRIVATE libargp::argp)
Expand All @@ -85,10 +81,6 @@ if(BUILD_EMV_TOOL)
endif()

add_executable(emv-tool emv-tool.c ../src/pcsc.c)
set_source_files_properties(emv-tool.c
PROPERTIES
COMPILE_DEFINITIONS EMV_UTILS_VERSION_STRING="${EMV_UTILS_VERSION_STRING}"
)
target_link_libraries(emv-tool PRIVATE print_helpers iso7816 emv emv_strings)
if(TARGET libargp::argp)
target_link_libraries(emv-tool PRIVATE libargp::argp)
Expand Down
10 changes: 9 additions & 1 deletion tools/emv-decode.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
* <https://www.gnu.org/licenses/>.
*/

#include "emv.h"
#include "iso7816.h"
#include "print_helpers.h"
#include "emv_strings.h"
Expand Down Expand Up @@ -286,7 +287,14 @@ static error_t argp_parser_helper(int key, char* arg, struct argp_state* state)
}

case EMV_DECODE_VERSION: {
printf("%s\n", EMV_UTILS_VERSION_STRING);
const char* version;

version = emv_lib_version_string();
if (version) {
printf("%s\n", version);
} else {
printf("Unknown\n");
}
exit(EXIT_SUCCESS);
return 0;
}
Expand Down
10 changes: 9 additions & 1 deletion tools/emv-tool.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
* <https://www.gnu.org/licenses/>.
*/

#include "emv.h"
#include "pcsc.h"
#include "iso7816.h"
#include "print_helpers.h"
Expand Down Expand Up @@ -255,7 +256,14 @@ static error_t argp_parser_helper(int key, char* arg, struct argp_state* state)
}

case EMV_TOOL_VERSION: {
printf("%s\n", EMV_UTILS_VERSION_STRING);
const char* version;

version = emv_lib_version_string();
if (version) {
printf("%s\n", version);
} else {
printf("Unknown\n");
}
exit(EXIT_SUCCESS);
return 0;
}
Expand Down

0 comments on commit 98a58e0

Please sign in to comment.