summaryrefslogtreecommitdiffstats
path: root/binding/afm-nfc-binding.c
diff options
context:
space:
mode:
authorMatt Ranostay <matt.ranostay@konsulko.com>2018-07-13 17:13:39 -0700
committerMatt Ranostay <matt.ranostay@konsulko.com>2018-07-17 13:23:25 -0700
commit885fedc954bb83a00591fd1381e9bd3749fc2935 (patch)
tree0bdd9f3196e5088ee07906f6c2d69104f50a0b2a /binding/afm-nfc-binding.c
parent66c594fdc40eb745bed177187b409e938736101b (diff)
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 <matt.ranostay@konsulko.com>
Diffstat (limited to 'binding/afm-nfc-binding.c')
-rw-r--r--binding/afm-nfc-binding.c39
1 files changed, 29 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;