From be766aef31246b7520931923fc27ebdcb0d573b6 Mon Sep 17 00:00:00 2001 From: Matt Ranostay Date: Wed, 11 Jul 2018 16:55:35 -0700 Subject: binding: nfc: move libnfc checks from thread To allow runtime detection of libnfc vs neard devices in the future libnfc check needs to be moved out of nfc_thread_loop Bug-AGL: SPEC-1554 Change-Id: I53854838a2fcab7eefbed3ed8d165a14e2bdc9db Signed-off-by: Matt Ranostay --- binding/afm-nfc-binding.c | 69 ++++++++++++++++++++++++++++++----------------- binding/afm-nfc-common.h | 8 ++++++ 2 files changed, 53 insertions(+), 24 deletions(-) (limited to 'binding') diff --git a/binding/afm-nfc-binding.c b/binding/afm-nfc-binding.c index 38fe1a9..e27a18c 100644 --- a/binding/afm-nfc-binding.c +++ b/binding/afm-nfc-binding.c @@ -71,30 +71,12 @@ static void send_detect_event(char *current_id) static void *nfc_loop_thread(void *ptr) { - nfc_context *ctx = NULL; - nfc_device *dev = NULL; - - nfc_init(&ctx); - - dev = nfc_open(ctx, NULL); - - if (dev == NULL) { - AFB_ERROR("Cannot get context for libnfc"); - nfc_exit(ctx); - exit(EXIT_FAILURE); - } - - if (nfc_initiator_init(dev) < 0) { - AFB_ERROR("Cannot get initiator mode from libnfc"); - nfc_close(dev); - nfc_exit(ctx); - exit(EXIT_FAILURE); - } + nfc_binding_data *data = ptr; while (1) { nfc_target nt; json_object *jresp; - int res = nfc_initiator_poll_target(dev, modulations, ARRAY_SIZE(modulations), 0xff, 2, &nt); + int res = nfc_initiator_poll_target(data->dev, modulations, ARRAY_SIZE(modulations), 0xff, 2, &nt); if (res < 0) break; @@ -106,7 +88,7 @@ static void *nfc_loop_thread(void *ptr) pthread_mutex_unlock(&mutex); - WAIT_FOR_REMOVE(dev); + WAIT_FOR_REMOVE(data->dev); pthread_mutex_lock(&mutex); @@ -121,19 +103,58 @@ static void *nfc_loop_thread(void *ptr) pthread_mutex_unlock(&mutex); } - nfc_close(dev); - nfc_exit(ctx); + nfc_close(data->dev); + nfc_exit(data->ctx); + free(data); return NULL; } + +static nfc_binding_data *get_libnfc_instance() +{ + nfc_context *ctx = NULL; + nfc_device *dev = NULL; + nfc_binding_data *data; + + nfc_init(&ctx); + + dev = nfc_open(ctx, NULL); + + if (dev == NULL) { + AFB_ERROR("Cannot get context for libnfc"); + nfc_exit(ctx); + return NULL; + } + + if (nfc_initiator_init(dev) < 0) { + AFB_ERROR("Cannot get initiator mode from libnfc"); + nfc_close(dev); + nfc_exit(ctx); + return NULL; + } + + data = malloc(sizeof(nfc_binding_data)); + + if (data) { + data->ctx = ctx; + data->dev = dev; + } + + return data; +} + static int init() { pthread_t thread_id; + nfc_binding_data *data = get_libnfc_instance(); presence_event = afb_daemon_make_event("presence"); - return pthread_create(&thread_id, NULL, nfc_loop_thread, NULL); + if (data) + return pthread_create(&thread_id, NULL, nfc_loop_thread, data); + else + return -ENODEV; } static void subscribe(struct afb_req request) diff --git a/binding/afm-nfc-common.h b/binding/afm-nfc-common.h index 49a0f8a..26741e6 100644 --- a/binding/afm-nfc-common.h +++ b/binding/afm-nfc-common.h @@ -19,8 +19,16 @@ #ifndef AFM_NFC_COMMON_H #define AFM_NFC_COMMON_H +#include + #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0])) char *to_hex_string(unsigned char *data, size_t size); + +typedef struct { + nfc_context *ctx; + nfc_device *dev; +} nfc_binding_data; + #endif // AFM_NFC_COMMON_H -- cgit 1.2.3-korg