Initialize libnfc and connect to reader

This commit is contained in:
Markus Koch 2019-07-23 19:12:13 +02:00
parent 2eab4613e8
commit 43d3e7f04d
3 changed files with 25 additions and 5 deletions

View File

@ -8,10 +8,11 @@ find_package(PkgConfig REQUIRED)
pkg_check_modules(GLIB2 REQUIRED glib-2.0)
pkg_check_modules(GOBJECT2 REQUIRED gobject-2.0)
pkg_check_modules(GTK3 REQUIRED gtk+-3.0)
pkg_check_modules(NFC REQUIRED libnfc)
INCLUDE_DIRECTORIES (${GLIB2_INCLUDE_DIRS} ${GOBJECT2_INCLUDE_DIRS} ${GTK3_INCLUDE_DIRS})
LINK_DIRECTORIES (${GLIB2_LIBRARY_DIRS} ${GOBJECT2_LIBRARY_DIRS} ${GTK3_LIBRARY_DIRS})
add_definitions(${GLIB2_CFLAGS_OTHER} ${GOBJECT2_CFLAGS_OTHER} ${GTK3_CFLAGS_OTHER})
INCLUDE_DIRECTORIES (${GLIB2_INCLUDE_DIRS} ${GOBJECT2_INCLUDE_DIRS} ${GTK3_INCLUDE_DIRS} ${NFC_INCLUDE_DIRS})
LINK_DIRECTORIES (${GLIB2_LIBRARY_DIRS} ${GOBJECT2_LIBRARY_DIRS} ${GTK3_LIBRARY_DIRS} ${NFC_LIBRARY_DIRS})
add_definitions(${GLIB2_CFLAGS_OTHER} ${GOBJECT2_CFLAGS_OTHER} ${GTK3_CFLAGS_OTHER} ${NFC_CFLAGS_OTHER})
add_custom_target(glib-resources DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/resources.c)
add_custom_command(DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/glade/*.glade "${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_NAME}.gresource.xml"
@ -23,6 +24,6 @@ aux_source_directory(./src SRC_LIST)
add_executable(${PROJECT_NAME} ${SRC_LIST} "${CMAKE_CURRENT_BINARY_DIR}/resources.c" "${HEADERS}")
add_dependencies(${PROJECT_NAME} glib-resources)
SET_SOURCE_FILES_PROPERTIES(${CMAKE_CURRENT_BINARY_DIR}/resources.c PROPERTIES GENERATED 1)
target_link_libraries(${PROJECT_NAME} ${GTK3_LIBRARIES})
target_link_libraries(${PROJECT_NAME} ${GTK3_LIBRARIES} ${NFC_LIBRARIES})
install (TARGETS ${PROJECT_NAME} DESTINATION bin)

View File

@ -6,6 +6,9 @@ struct _YacosScanner {
// TODO: Private data goes here
};
static nfc_context *nfc_ctx; // TODO: Migrate to klass
static nfc_device *nfc_pnd = NULL; // TODO: Migrate to klass
G_DEFINE_TYPE(YacosScanner, yacos_scanner, G_TYPE_OBJECT)
enum {
@ -28,7 +31,10 @@ static void yacos_scanner_finalize(GObject *gobject)
{
YacosScanner *obj = yacos_scanner_get_instance_private(YACOS_SCANNER(gobject));
// TODO: clean up
if (nfc_pnd)
nfc_close(nfc_pnd);
if (nfc_ctx)
nfc_exit(nfc_ctx);
G_OBJECT_CLASS(yacos_scanner_parent_class)->finalize(gobject);
}
@ -47,6 +53,18 @@ static void yacos_scanner_class_init(YacosScannerClass *klass)
NULL);
object_class->finalize = yacos_scanner_finalize;
g_debug("Using libnfc %s", nfc_version());
nfc_init(&nfc_ctx);
if (!nfc_ctx) {
g_error("Error initializing libnfc");
return;
}
nfc_pnd = nfc_open(nfc_ctx, NULL);
if (!nfc_pnd) {
g_error("Error initializing NFC device");
return;
}
}
static void yacos_scanner_init(YacosScanner *self)

View File

@ -2,6 +2,7 @@
#define SCANNER_H
#include <glib-object.h>
#include <nfc/nfc.h>
G_BEGIN_DECLS