aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatt Ranostay <matt.ranostay@konsulko.com>2018-07-11 16:55:35 -0700
committerMatt Ranostay <matt.ranostay@konsulko.com>2018-07-13 10:33:42 -0700
commitbe766aef31246b7520931923fc27ebdcb0d573b6 (patch)
tree1a05c71ae6d4f5e4cc9c4a9759a2c5ee34767c4d
parent0d822f802d7ce1604966bbb7c9cf536052d8ea15 (diff)
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 <matt.ranostay@konsulko.com>
-rw-r--r--binding/afm-nfc-binding.c69
-rw-r--r--binding/afm-nfc-common.h8
2 files changed, 53 insertions, 24 deletions
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 <nfc/nfc-types.h>
+
#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