diff options
Diffstat (limited to 'binding')
-rw-r--r-- | binding/CMakeLists.txt | 2 | ||||
-rw-r--r-- | binding/afm-nfc-binding.c | 127 | ||||
-rw-r--r-- | binding/afm-nfc-common.c | 37 | ||||
-rw-r--r-- | binding/afm-nfc-common.h | 9 |
4 files changed, 2 insertions, 173 deletions
diff --git a/binding/CMakeLists.txt b/binding/CMakeLists.txt index 0e6a5bc..512cede 100644 --- a/binding/CMakeLists.txt +++ b/binding/CMakeLists.txt @@ -21,7 +21,7 @@ PROJECT_TARGET_ADD(afm-nfc-binding) # Define project Targets - add_library(afm-nfc-binding MODULE afm-nfc-common.c afm-nfc-binding.c) + add_library(afm-nfc-binding MODULE afm-nfc-binding.c) # Binder exposes a unique public entry point SET_TARGET_PROPERTIES(${TARGET_NAME} PROPERTIES diff --git a/binding/afm-nfc-binding.c b/binding/afm-nfc-binding.c index 70a8625..94705de 100644 --- a/binding/afm-nfc-binding.c +++ b/binding/afm-nfc-binding.c @@ -29,133 +29,14 @@ #include <glib-object.h> #include <json-c/json.h> #include <neardal/neardal.h> -#include <nfc/nfc.h> -#include <nfc/nfc-types.h> - #define AFB_BINDING_VERSION 3 #include <afb/afb-binding.h> #include "afm-nfc-common.h" -#define WAIT_FOR_REMOVE(dev) { while (0 == nfc_initiator_target_is_present(dev, NULL)) {} } - static afb_event_t presence_event; static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; -static const nfc_modulation modulations[] = { - { .nmt = NMT_ISO14443A, .nbr = NBR_106 }, -}; - -static char *get_tag_uid(nfc_target *nt) -{ - if (nt->nm.nmt == NMT_ISO14443A) - return to_hex_string((unsigned char *) &nt->nti.nai.abtUid, nt->nti.nai.szUidLen); - - return NULL; -} - -static void send_detect_event(char *current_id, nfc_binding_data *data) -{ - json_object *jresp; - - if (current_id == NULL) - return; - - jresp = json_object_new_object(); - - json_object_object_add(jresp, "status", json_object_new_string("detected")); - json_object_object_add(jresp, "uid", json_object_new_string(current_id)); - - if (data->jresp) { - json_object_put(data->jresp); - data->jresp = NULL; - } - - json_object_get(jresp); - data->jresp = jresp; - - afb_event_push(presence_event, jresp); -} - -static void *nfc_loop_thread(void *ptr) -{ - nfc_binding_data *data = ptr; - - while (1) { - nfc_target nt; - json_object *jresp; - int res = nfc_initiator_poll_target(data->dev, modulations, ARRAY_SIZE(modulations), 0xff, 2, &nt); - char *current_uid; - - if (res < 0) - break; - - pthread_mutex_lock(&mutex); - - current_uid = get_tag_uid(&nt); - send_detect_event(current_uid, data); - - pthread_mutex_unlock(&mutex); - - WAIT_FOR_REMOVE(data->dev); - - pthread_mutex_lock(&mutex); - - 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)); - - if (data->jresp) { - json_object_put(data->jresp); - data->jresp = NULL; - } - - afb_event_push(presence_event, jresp); - - pthread_mutex_unlock(&mutex); - } - - 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_WARNING("Cannot get context for libnfc"); - nfc_exit(ctx); - return NULL; - } - - if (nfc_initiator_init(dev) < 0) { - AFB_WARNING("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 void neard_cb_record_found(const char *tag_name, void *ptr) { nfc_binding_data *data = ptr; @@ -245,18 +126,12 @@ static void *neard_loop_thread(void *ptr) static int init(afb_api_t api) { pthread_t thread_id; - nfc_binding_data *data = get_libnfc_instance(); + nfc_binding_data *data = NULL; char **adapters = NULL; int num_adapters, ret; presence_event = afb_daemon_make_event("presence"); - if (data) { - afb_api_set_userdata(api, data); - - return pthread_create(&thread_id, NULL, nfc_loop_thread, data); - } - ret = neardal_get_adapters(&adapters, &num_adapters); if (ret == NEARDAL_SUCCESS) { diff --git a/binding/afm-nfc-common.c b/binding/afm-nfc-common.c deleted file mode 100644 index 6dee428..0000000 --- a/binding/afm-nfc-common.c +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright (C) 2018 Konsulko Group - * Author: Matt Ranostay <matt.ranostay@konsulko.com> - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -#include <stdlib.h> -#include <stdio.h> -#include "afm-nfc-common.h" - -char *to_hex_string(unsigned char *data, size_t size) -{ - char *buffer = malloc((2 * size) + 1); - char *tmp = buffer; - int i; - - if (buffer == NULL) - return buffer; - - for (i = 0; i < size; i++) { - tmp += sprintf(tmp, "%.2x", data[i]); - } - - return buffer; -} diff --git a/binding/afm-nfc-common.h b/binding/afm-nfc-common.h index c4c4cff..24a879c 100644 --- a/binding/afm-nfc-common.h +++ b/binding/afm-nfc-common.h @@ -21,17 +21,8 @@ #include <glib.h> #include <json-c/json.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; - gchar *adapter; GMainLoop *loop; |