From 885fedc954bb83a00591fd1381e9bd3749fc2935 Mon Sep 17 00:00:00 2001 From: Matt Ranostay Date: Fri, 13 Jul 2018 17:13:39 -0700 Subject: binding: nfc: use cached json_object for events Cache the json_object for event response for detect for future subscribers Bug-AGL: SPEC-1554 Change-Id: I4dfeb7f157a872db96c8c5fabae9c26547815baa Signed-off-by: Matt Ranostay --- binding/afm-nfc-binding.c | 39 +++++++++++++++++++++++++++++---------- binding/afm-nfc-common.h | 4 ++++ 2 files changed, 33 insertions(+), 10 deletions(-) diff --git a/binding/afm-nfc-binding.c b/binding/afm-nfc-binding.c index 5820eae..35fd00e 100644 --- a/binding/afm-nfc-binding.c +++ b/binding/afm-nfc-binding.c @@ -54,7 +54,7 @@ static char *get_tag_uid(nfc_target *nt) return NULL; } -static void send_detect_event(char *current_id) +static void send_detect_event(char *current_id, nfc_binding_data *data) { json_object *jresp; @@ -66,6 +66,14 @@ static void send_detect_event(char *current_id) json_object_object_add(jresp, "status", json_object_new_string("detected")); json_object_object_add(jresp, "uid", json_object_new_string(current_uid)); + if (data->jresp) { + json_object_put(data->jresp); + data->jresp = NULL; + } + + json_object_get(jresp); + data->jresp = jresp; + afb_event_push(presence_event, jresp); } @@ -84,7 +92,7 @@ static void *nfc_loop_thread(void *ptr) pthread_mutex_lock(&mutex); current_uid = get_tag_uid(&nt); - send_detect_event(current_uid); + send_detect_event(current_uid, data); pthread_mutex_unlock(&mutex); @@ -95,10 +103,13 @@ static void *nfc_loop_thread(void *ptr) jresp = json_object_new_object(); json_object_object_add(jresp, "status", json_object_new_string("removed")); json_object_object_add(jresp, "uid", json_object_new_string(current_uid)); - afb_event_push(presence_event, jresp); - free(current_uid); - current_uid = NULL; + if (data->jresp) { + json_object_put(data->jresp); + data->jresp = NULL; + } + + afb_event_push(presence_event, jresp); pthread_mutex_unlock(&mutex); } @@ -144,22 +155,27 @@ static nfc_binding_data *get_libnfc_instance() return data; } -static int init() +static int init(afb_api_t api) { pthread_t thread_id; nfc_binding_data *data = get_libnfc_instance(); presence_event = afb_daemon_make_event("presence"); - if (data) + if (data) { + afb_api_set_userdata(api, data); + return pthread_create(&thread_id, NULL, nfc_loop_thread, data); - else - return -ENODEV; + } + + return -ENODEV; } static void subscribe(afb_req_t request) { const char *value = afb_req_value(request, "value"); + afb_api_t api = afb_req_get_api(request); + nfc_binding_data *data = afb_api_get_userdata(api); if (value && !strcasecmp(value, "presence")) { afb_req_subscribe(request, presence_event); @@ -167,7 +183,10 @@ static void subscribe(afb_req_t request) // send initial tag if exists pthread_mutex_lock(&mutex); - send_detect_event(current_uid); + if (data && data->jresp) { + json_object_get(data->jresp); + afb_event_push(presence_event, data->jresp); + } pthread_mutex_unlock(&mutex); return; diff --git a/binding/afm-nfc-common.h b/binding/afm-nfc-common.h index 26741e6..84e70bd 100644 --- a/binding/afm-nfc-common.h +++ b/binding/afm-nfc-common.h @@ -19,6 +19,7 @@ #ifndef AFM_NFC_COMMON_H #define AFM_NFC_COMMON_H +#include #include #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0])) @@ -29,6 +30,9 @@ char *to_hex_string(unsigned char *data, size_t size); typedef struct { nfc_context *ctx; nfc_device *dev; + + /* cached JSON event response */ + json_object *jresp; } nfc_binding_data; #endif // AFM_NFC_COMMON_H -- cgit 1.2.3-korg