diff options
author | Matt Ranostay <matt.ranostay@konsulko.com> | 2018-05-24 22:57:13 -0700 |
---|---|---|
committer | Matt Ranostay <matt.ranostay@konsulko.com> | 2018-07-04 23:18:45 -0700 |
commit | 6eb99ceb647cf35c39dc97292f00040fbb821170 (patch) | |
tree | 1db97cb18370f6b25902bb06b2e01a74aae169e2 /src | |
parent | f9e5259d74c8cf521921a96377c64691b5b3c3ae (diff) |
binding: nfc: new initial binding
Slight rewrite to use libnfc initially for getting tag uid's for
user settings.
Bug-AGL: SPEC-1554
Change-Id: I7b8c30102b82e86a92c89909bcbfba9ab7164c1f
Signed-off-by: Matt Ranostay <matt.ranostay@konsulko.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/CMakeLists.txt | 77 | ||||
-rw-r--r-- | src/api.c | 63 | ||||
-rw-r--r-- | src/libnfc_reader.c | 478 | ||||
-rw-r--r-- | src/libnfc_reader.h | 12 | ||||
-rw-r--r-- | src/nfc-binding.c | 134 | ||||
-rw-r--r-- | src/nfc-binding.h | 17 | ||||
-rw-r--r-- | src/stringutils.h | 41 |
7 files changed, 0 insertions, 822 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt deleted file mode 100644 index ba35037..0000000 --- a/src/CMakeLists.txt +++ /dev/null @@ -1,77 +0,0 @@ -########################################################################### -# Copyright 2015, 2016, 2017 IoT.bzh -# -# author: Loïc Collignon <loic.collignon@iot.bzh> -# -# 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. -########################################################################### - -CMAKE_MINIMUM_REQUIRED(VERSION 3.3) - -PROJECT_TARGET_ADD(nfc-binding) - -set(NFC_BINDING_SOURCES api.c nfc-binding.c) - -if (USE_LIBNFC) - set(NFC_BINDING_SOURCES ${NFC_BINDING_SOURCES} libnfc_reader.c) - add_definitions(-DUSE_LIBNFC=1) - - if(LIBNFC_POLL_ALL) - add_definitions(-DLIBNFC_POLL_ALL=1) - endif() - - if(LIBNFC_POLL_NMT_ISO14443A) - add_definitions(-DLIBNFC_POLL_NMT_ISO14443A=1) - endif() - - if(LIBNFC_POLL_NMT_ISOJEWEL) - add_definitions(-DLIBNFC_POLL_NMT_ISOJEWEL=1) - endif() - - if(LIBNFC_POLL_NMT_ISO14443B) - add_definitions(-DLIBNFC_POLL_NMT_ISO14443B=1) - endif() - - if(LIBNFC_POLL_NMT_ISO14443BI) - add_definitions(-DLIBNFC_POLL_NMT_ISO14443BI=1) - endif() - - if(LIBNFC_POLL_NMT_ISO14443B2SR) - add_definitions(-DLIBNFC_POLL_NMT_ISO14443B2SR=1) - endif() - - if(LIBNFC_POLL_NMT_ISO14443B2CT) - add_definitions(-DLIBNFC_POLL_NMT_ISO14443B2CT=1) - endif() - - if(LIBNFC_POLL_NMT_FELICA) - add_definitions(-DLIBNFC_POLL_NMT_FELICA=1) - endif() -endif() - -message(STATUS "libnfc enabled: ${USE_LIBNFC}") - -add_library(${TARGET_NAME} MODULE ${NFC_BINDING_SOURCES}) -target_link_libraries(${TARGET_NAME} ${link_libraries}) - -add_custom_command(TARGET ${TARGET_NAME} - PRE_BUILD - COMMAND mkdir -p ${CMAKE_CURRENT_BINARY_DIR}/../package/htdocs - COMMAND cp -rv ${CMAKE_CURRENT_SOURCE_DIR}/../htdocs ${CMAKE_CURRENT_BINARY_DIR}/../package/) - -SET_TARGET_PROPERTIES(${TARGET_NAME} PROPERTIES - PREFIX "afb-" - LABELS "BINDING" - LINK_FLAGS ${BINDINGS_LINK_FLAG} - OUTPUT_NAME ${TARGET_NAME}) - diff --git a/src/api.c b/src/api.c deleted file mode 100644 index 8e928fd..0000000 --- a/src/api.c +++ /dev/null @@ -1,63 +0,0 @@ -#include "nfc-binding.h" - -/* -static const struct afb_auth nfc_auths[] = { -}; -*/ - -static const struct afb_verb_v2 nfc_verbs[] = { - { - .verb = "subscribe", - .callback = verb_subscribe, - .auth = NULL, - .info = NULL, - .session = AFB_SESSION_NONE_V2 - }, - { - .verb = "unsubscribe", - .callback = verb_unsubscribe, - .auth = NULL, - .info = NULL, - .session = AFB_SESSION_NONE_V2 - }, - { - .verb = "list-devices", - .callback = verb_list_devices, - .auth = NULL, - .info = NULL, - .session = AFB_SESSION_NONE_V2 - }, - { - .verb = "list-devices-capabilities", - .callback = verb_list_devices_capabilities, - .auth = NULL, - .info = NULL, - .session = AFB_SESSION_NONE_V2 - }, - { - .verb = "start", - .callback = verb_start, - .auth = NULL, - .info = NULL, - .session = AFB_SESSION_NONE_V2 - }, - { - .verb = "stop", - .callback = verb_stop, - .auth = NULL, - .info = NULL, - .session = AFB_SESSION_NONE_V2 - }, - { .verb = NULL } -}; - -const struct afb_binding afbBindingV2 = { - .api = "nfc", - .specification = NULL, - .info = NULL, - .verbs = nfc_verbs, - .preinit = NULL, - .init = init, - .onevent = NULL, - .noconcurrency = 0 -}; diff --git a/src/libnfc_reader.c b/src/libnfc_reader.c deleted file mode 100644 index 78269db..0000000 --- a/src/libnfc_reader.c +++ /dev/null @@ -1,478 +0,0 @@ -#include "nfc-binding.h" - -#include <signal.h> -#include <string.h> -#include <sys/types.h> -#include <pthread.h> - -// FIXME: It compile without these lines, but KDevelop complains about pthread_t being undeclared. -#ifndef _BITS_PTHREADTYPES -typedef unsigned long int pthread_t; -#endif - -#include <nfc/nfc.h> -#include "libnfc_reader.h" -#include "stringutils.h" - -extern struct afb_event on_nfc_target_add_event; -extern struct afb_event on_nfc_target_remove_event; - -#define MAX_NFC_DEVICE_COUNT 8 -#define MAX_NFC_MODULATIONS 8 -#define MAX_NFC_BAUDRATES 8 -#define POLL_NUMBER 0x1 -#define POLL_PERIOD 0x7 - -typedef struct libnfc_device_tag -{ - pthread_t poller; - nfc_device* device; - nfc_connstring name; - - nfc_modulation* modulations; - size_t modulations_count; -} libnfc_device; - -typedef struct libnfc_context_tag -{ - nfc_context* context; - libnfc_device* devices; - size_t devices_count; - struct json_object* last_target; -} libnfc_context; - -static libnfc_context libnfc; - -void libnfc_polling_error(int code) -{ - switch(code) - { - case NFC_EIO: - AFB_ERROR("libnfc: polling failed with NFC_EIO (%d) code: Input / output error, device may not be usable anymore without re-open it!", code); - break; - case NFC_EINVARG: - AFB_ERROR("libnfc: polling failed with NFC_EINVARG (%d) code: Invalid argument(s)!", code); - break; - case NFC_EDEVNOTSUPP: - AFB_ERROR("libnfc: polling failed with NFC_EDEVNOTSUPP (%d) code: Operation not supported by device!", code); - break; - case NFC_ENOTSUCHDEV: - AFB_ERROR("libnfc: polling failed with NFC_ENOTSUCHDEV (%d) code: No such device!", code); - break; - case NFC_EOVFLOW: - AFB_ERROR("libnfc: polling failed with NFC_EOVFLOW (%d) code: Buffer overflow!", code); - break; - case NFC_ETIMEOUT: - AFB_ERROR("libnfc: polling failed with NFC_ETIMEOUT (%d) code: Operation timed out!", code); - break; - case NFC_EOPABORTED: - AFB_ERROR("libnfc: polling failed with NFC_EOPABORTED (%d) code: Operation aborted (by user)!", code); - break; - case NFC_ENOTIMPL: - AFB_ERROR("libnfc: polling failed with NFC_ENOTIMPL (%d) code: Not (yet) implemented!", code); - break; - case NFC_ETGRELEASED: - AFB_ERROR("libnfc: polling failed with NFC_ETGRELEASED (%d) code: Target released!", code); - break; - case NFC_ERFTRANS: - AFB_ERROR("libnfc: polling failed with NFC_ERFTRANS (%d) code: Error while RF transmission!", code); - break; - case NFC_EMFCAUTHFAIL: - AFB_ERROR("libnfc: polling failed with NFC_EMFCAUTHFAIL (%d) code: MIFARE Classic: authentication failed!", code); - break; - case NFC_ESOFT: - AFB_ERROR("libnfc: polling failed with NFC_ESOFT (%d) code: Software error (allocation, file/pipe creation, etc.)!", code); - break; - case NFC_ECHIP: - //AFB_ERROR("libnfc: polling failed with NFC_ECHIP (%d) code: Device's internal chip error!", code); - break; - default: - AFB_ERROR("libnfc: polling failed with unknown code: %d!", code); - break; - } -} - -void add_nfc_field(struct json_object* parent, const char* field, const void* src, size_t sz) -{ - char* data; - - if (parent && field && src && sz) - { - data = to_hex_string(src, sz); - if (data) - { - json_object_object_add(parent, field, json_object_new_string(data)); - free(data); - } - } -} - -struct json_object* read_target(const nfc_target* target) -{ - struct json_object* result; - const char* mt; - - if (!target) - { - AFB_WARNING("libnfc: No target to read!"); - return NULL; - } - - result = json_object_new_object(); - mt = str_nfc_modulation_type(target->nm.nmt); - json_object_object_add(result, "Type", json_object_new_string(mt)); - - switch(target->nm.nmt) - { - case NMT_ISO14443A: - add_nfc_field(result, "ATQA", target->nti.nai.abtAtqa, 2); - add_nfc_field(result, "SAK", &target->nti.nai.btSak, 1); - add_nfc_field(result, "UID", target->nti.nai.abtUid, target->nti.nai.szUidLen); - add_nfc_field(result, "ATS", target->nti.nai.abtAts, target->nti.nai.szAtsLen); - - break; - case NMT_ISO14443B: - add_nfc_field(result, "PUPI", target->nti.nbi.abtPupi, 4); - add_nfc_field(result, "Application Data", target->nti.nbi.abtApplicationData, 4); - add_nfc_field(result, "Protocol Info", target->nti.nbi.abtProtocolInfo, 3); - add_nfc_field(result, "Card Id", &target->nti.nbi.ui8CardIdentifier, 1); - - break; - default: - AFB_WARNING("libnfc: unsupported modulation type: %s.", mt); - json_object_object_add(result, "error", json_object_new_string("unsupported tag type")); - break; - } - return result; -} - -void* libnfc_reader_main(void* arg) -{ - libnfc_device* device; - nfc_target nt; - int polled_target_count; - nfc_modulation mods[MAX_NFC_MODULATIONS]; - struct json_object* result; - size_t i, j; - - device = (libnfc_device*)arg; - - memset(mods, 0, sizeof(nfc_modulation) * MAX_NFC_MODULATIONS); - for(i = 0, j = 0; i < device->modulations_count; ++i, ++j) - { - switch(device->modulations[i].nmt) - { -#if defined(LIBNFC_POLL_ALL) || defined(LIBNFC_POLL_NMT_ISO14443A) - case NMT_ISO14443A: -#endif -#if defined(LIBNFC_POLL_ALL) || defined(LIBNFC_POLL_NMT_ISOJEWEL) - case NMT_JEWEL: -#endif -#if defined(LIBNFC_POLL_ALL) || defined(LIBNFC_POLL_NMT_ISO14443B) - case NMT_ISO14443B: -#endif -#if defined(LIBNFC_POLL_ALL) || defined(LIBNFC_POLL_NMT_ISO14443BI) - case NMT_ISO14443BI: -#endif -#if defined(LIBNFC_POLL_ALL) || defined(LIBNFC_POLL_NMT_ISO14443B2SR) - case NMT_ISO14443B2SR: -#endif -#if defined(LIBNFC_POLL_ALL) || defined(LIBNFC_POLL_NMT_ISO14443B2CT) - case NMT_ISO14443B2CT: -#endif -#if defined(LIBNFC_POLL_ALL) || defined(LIBNFC_POLL_NMT_FELICA) - case NMT_FELICA: -#endif - mods[j] = device->modulations[i]; - AFB_NOTICE("libnfc: polling for %s at %s is ENABLED.", str_nfc_modulation_type(device->modulations[i].nmt), str_nfc_baud_rate(device->modulations[i].nbr)); - break; - default: - --j; - // NMT_DEP is always disabled because it can't be polled - AFB_NOTICE("libnfc: polling for %s at %s is DISABLED.", str_nfc_modulation_type(device->modulations[i].nmt), str_nfc_baud_rate(device->modulations[i].nbr)); - break; - } - } - - while(device->device) - { - polled_target_count = nfc_initiator_poll_target - ( - device->device, - mods, - j, - POLL_NUMBER, - POLL_PERIOD, - &nt - ); - - switch(polled_target_count) - { - case 0: - // No target detected - AFB_INFO("libnfc: polling done with no result."); - if (libnfc.last_target) - { - AFB_NOTICE("libnfc: tag removed = %s", json_object_to_json_string(libnfc.last_target)); - afb_event_push(on_nfc_target_remove_event, libnfc.last_target); - libnfc.last_target = NULL; - } - break; - - case 1: - AFB_INFO("libnfc: polling done with one result."); - // One target detected - result = read_target(&nt); - - if (libnfc.last_target) - { - if (strcmp(json_object_to_json_string(result), json_object_to_json_string(libnfc.last_target))) - { - AFB_NOTICE("libnfc: tag removed = %s", json_object_to_json_string(libnfc.last_target)); - afb_event_push(on_nfc_target_remove_event, libnfc.last_target); - libnfc.last_target = NULL; - } - } - - if (!libnfc.last_target) - { - json_object_get(result); - libnfc.last_target = result; - - AFB_NOTICE("libnfc: tag added = %s", json_object_to_json_string(result)); - afb_event_push(on_nfc_target_add_event, result); - } - break; - - default: - if (polled_target_count < 0) libnfc_polling_error(polled_target_count); - else AFB_WARNING("libnfc: polling done with unsupported result count: %d.", polled_target_count); - - // Consider target is removed - if (libnfc.last_target) - { - AFB_NOTICE("libnfc: tag removed = %s", json_object_to_json_string(libnfc.last_target)); - afb_event_push(on_nfc_target_remove_event, libnfc.last_target); - libnfc.last_target = NULL; - } - break; - } - } - return NULL; -} - -void exit_handler() -{ - size_t i; - nfc_device* dev; - for(i = 0; i < libnfc.devices_count; ++i) - { - if (libnfc.devices[i].device) - { - dev = libnfc.devices[i].device; - libnfc.devices[i].device = NULL; - nfc_close(dev); - } - } - nfc_exit(libnfc.context); - libnfc.context = NULL; -} - -void sigterm_handler(int sig) -{ - if (sig == SIGTERM && libnfc.context && libnfc.devices_count) - { - exit_handler(); - } -} - -/// @brief Start the libnfc context. -/// @return An exit code, @c EXIT_LIBNFC_SUCCESS (zero) on success. -int libnfc_init() -{ - nfc_device* dev; - const nfc_modulation_type* modulations; - const nfc_baud_rate* baudrates; - size_t modulation_idx; - nfc_connstring connstrings[MAX_NFC_DEVICE_COUNT]; - size_t ref_device_count; - size_t device_idx; - - atexit(exit_handler); - - memset(&libnfc, 0, sizeof(libnfc_context)); - - nfc_init(&libnfc.context); - if (libnfc.context == NULL) - { - AFB_ERROR("[libnfc] Initialization failed (malloc)!"); - return EXIT_LIBNFC_NOT_INITIALIZED; - } - - AFB_NOTICE("[libnfc] Using libnfc version: %s.", nfc_version()); - - // Find and register devices - ref_device_count = nfc_list_devices(libnfc.context, connstrings, MAX_NFC_DEVICE_COUNT); - if (!ref_device_count) - { - AFB_ERROR("libnfc: No NFC device found!"); - return EXIT_LIBNFC_NO_DEVICE_FOUND; - } - libnfc.devices_count = ref_device_count; - libnfc.devices = malloc(sizeof(libnfc_device) * libnfc.devices_count); - memset(libnfc.devices, 0, sizeof(libnfc_device) * libnfc.devices_count); - - signal(SIGTERM, sigterm_handler); - - for(device_idx = 0; device_idx < ref_device_count; ++device_idx) - { - AFB_NOTICE("libnfc: NFC Device found: \"%s\".", connstrings[device_idx]); - strcpy(libnfc.devices[device_idx].name, connstrings[device_idx]); - - // Find and register modulations - dev = nfc_open(libnfc.context, connstrings[device_idx]); - if (dev) - { - if (nfc_device_get_supported_modulation(dev, N_INITIATOR, &modulations)) - { - AFB_ERROR("libnfc: Failed to get supported modulations from '%s'!", connstrings[device_idx]); - } - else - { - // Find and register modulations - modulation_idx = 0; - while(modulations[modulation_idx]) ++modulation_idx; - libnfc.devices[device_idx].modulations_count = modulation_idx; - if (modulation_idx) - { - libnfc.devices[device_idx].modulations = malloc(sizeof(nfc_modulation) * modulation_idx); - memset(libnfc.devices[device_idx].modulations, 0, sizeof(nfc_modulation) * modulation_idx); - - modulation_idx = 0; - while(modulations[modulation_idx]) - { - libnfc.devices[device_idx].modulations[modulation_idx].nmt = modulations[modulation_idx]; - if (!nfc_device_get_supported_baud_rate(dev, modulations[modulation_idx], &baudrates)) - { - // Keep only the first speed which is supposed to be the fastest - libnfc.devices[device_idx].modulations[modulation_idx].nbr = baudrates[0]; - } - - AFB_NOTICE("libnfc: - Modulation '%s' supported at '%s'." - , str_nfc_modulation_type(libnfc.devices[device_idx].modulations[modulation_idx].nmt) - , str_nfc_baud_rate(libnfc.devices[device_idx].modulations[modulation_idx].nbr)); - ++modulation_idx; - } - } - } - nfc_close(dev); - } - } - - return EXIT_LIBNFC_SUCCESS; -} - -/// @brief List devices founds by libnfc. -/// @param[in] result A json object array into which found devices are added. -/// @return An exit code, @c EXIT_LIBNFC_SUCCESS (zero) on success. -int libnfc_list_devices(struct json_object* result) -{ - struct json_object* device; - size_t i; - - for(i = 0; i < libnfc.devices_count; ++i) - { - device = json_object_new_object(); - json_object_object_add(device, "source", json_object_new_string("libnfc")); - json_object_object_add(device, "name", json_object_new_string(libnfc.devices[i].name)); - json_object_array_add(result, device); - } - - return EXIT_LIBNFC_SUCCESS; -} - -int libnfc_list_devices_capabilities(struct json_object* result, struct json_object* devices) -{ - struct json_object* device; - struct json_object* mods; - struct json_object* mod; - size_t i, j; - - for(i = 0; i < libnfc.devices_count; ++i) - { - device = json_object_new_object(); - json_object_object_add(device, "source", json_object_new_string("libnfc")); - json_object_object_add(device, "name", json_object_new_string(libnfc.devices[i].name)); - mods = json_object_new_array(); - - for(j = 0; j < libnfc.devices[i].modulations_count; ++j) - { - mod = json_object_new_object(); - json_object_object_add(mod, "modulation", json_object_new_string(str_nfc_modulation_type(libnfc.devices[i].modulations[j].nmt))); - json_object_object_add(mod, "baudrate", json_object_new_string(str_nfc_baud_rate(libnfc.devices[i].modulations[j].nbr))); - json_object_array_add(mods, mod); - } - - json_object_object_add(device, "modulations", mods); - json_object_array_add(result, device); - } - - return EXIT_LIBNFC_SUCCESS; -} - -int libnfc_start_polling(struct json_object* result, struct json_object* devices) -{ - struct json_object* device; - size_t i; - int r; - - for(i = 0; i < libnfc.devices_count; ++i) - { - device = json_object_new_object(); - json_object_object_add(device, "source", json_object_new_string("libnfc")); - json_object_object_add(device, "name", json_object_new_string(libnfc.devices[i].name)); - if (libnfc.devices[i].device) - { - json_object_object_add(device, "status", json_object_new_string("already polling")); - AFB_NOTICE("libnfc: Device '%s' is already polling.", libnfc.devices[i].name); - } - else - { - libnfc.devices[i].device = nfc_open(libnfc.context, libnfc.devices[i].name); - if (libnfc.devices[i].device) - { - if (nfc_initiator_init(libnfc.devices[i].device) < 0) - { - nfc_close(libnfc.devices[i].device); - libnfc.devices[i].device = NULL; - json_object_object_add(device, "status", json_object_new_string("failed to set initiator mode")); - AFB_ERROR("libnfc: nfc_initiator_init failedfor device '%s'!", libnfc.devices[i].name); - } - else - { - r = pthread_create(&libnfc.devices[i].poller, NULL, libnfc_reader_main, (void*)&libnfc.devices[i]); - if (r) - { - nfc_close(libnfc.devices[i].device); - libnfc.devices[i].device = NULL; - json_object_object_add(device, "status", json_object_new_string("failed to create the polling thread")); - AFB_ERROR("libnfc: pthread_create failed!"); - } - else - { - json_object_object_add(device, "status", json_object_new_string("polling")); - AFB_NOTICE("libnfc: Polling the device '%s'.", libnfc.devices[i].name); - } - } - } - else - { - json_object_object_add(device, "status", json_object_new_string("failed to open device")); - AFB_ERROR("libnfc: Failed to open device '%s'!", libnfc.devices[i].name); - } - } - json_object_array_add(result, device); - } - - return EXIT_LIBNFC_SUCCESS; -} diff --git a/src/libnfc_reader.h b/src/libnfc_reader.h deleted file mode 100644 index 1209fda..0000000 --- a/src/libnfc_reader.h +++ /dev/null @@ -1,12 +0,0 @@ -#pragma once - -#include <json-c/json.h> - -#define EXIT_LIBNFC_SUCCESS 0 -#define EXIT_LIBNFC_NOT_INITIALIZED 1 -#define EXIT_LIBNFC_NO_DEVICE_FOUND 2 - -int libnfc_init(); -int libnfc_list_devices(struct json_object* result); -int libnfc_list_devices_capabilities(struct json_object* result, struct json_object* devices); -int libnfc_start_polling(struct json_object* result, struct json_object* devices); diff --git a/src/nfc-binding.c b/src/nfc-binding.c deleted file mode 100644 index c5f14c6..0000000 --- a/src/nfc-binding.c +++ /dev/null @@ -1,134 +0,0 @@ -#include "nfc-binding.h" - -#if USE_LIBNFC == 1 -#include "libnfc_reader.h" -#endif - -struct afb_event on_nfc_target_add_event; -struct afb_event on_nfc_target_remove_event; - -/// @brief Binding's initialization. -/// @return Exit code, zero on success, non-zero otherwise. -int init() -{ - on_nfc_target_add_event = afb_daemon_make_event("on-nfc-target-add"); - on_nfc_target_remove_event = afb_daemon_make_event("on-nfc-target-remove"); - if (!afb_event_is_valid(on_nfc_target_add_event) || !afb_event_is_valid(on_nfc_target_remove_event)) - { - AFB_ERROR("Failed to create a valid event!"); - return 1; - } - -#if USE_LIBNFC == 1 - if (libnfc_init()) - { - AFB_ERROR("Failed start libnfc reader!"); - return 2; - } -#endif - - return 0; -} - -/// @brief Get a list of devices. -/// @param[in] req The query. -void verb_subscribe(struct afb_req req) -{ - if (!afb_req_subscribe(req, on_nfc_target_remove_event)) - { - if (!afb_req_subscribe(req, on_nfc_target_add_event)) - { - afb_req_success(req, NULL, "Subscription success!"); - return; - } - else afb_req_unsubscribe(req, on_nfc_target_remove_event); - } - - afb_req_fail(req, NULL, "Subscription failure!"); -} - -/// @brief Get a list of devices. -/// @param[in] req The query. -void verb_unsubscribe(struct afb_req req) -{ - if (!afb_req_unsubscribe(req, on_nfc_target_add_event)) - { - if (!afb_req_unsubscribe(req, on_nfc_target_remove_event)) - { - afb_req_success(req, NULL, "Unsubscription success!"); - return; - } - } - - afb_req_fail(req, NULL, "Unsubscription failure!"); -} - -/// @brief Get a list of devices. -/// @param[in] req The query. -void verb_list_devices(struct afb_req req) -{ - struct json_object* result; - - result = json_object_new_array(); - -#if USE_LIBNFC == 1 - if (libnfc_list_devices(result)) - { - afb_req_fail(req, "Failed to get devices list from libnfc!", NULL); - return; - } -#endif - - afb_req_success(req, result, NULL); -} - -/// @brief Get a list of devices capabilities. -/// @param[in] req The query. -void verb_list_devices_capabilities(struct afb_req req) -{ - struct json_object* result; - struct json_object* arg; - - arg = afb_req_json(req); - - result = json_object_new_array(); - -#if USE_LIBNFC == 1 - if (libnfc_list_devices_capabilities(result, arg)) - { - afb_req_fail(req, "Failed to get devices list from libnfc!", NULL); - return; - } -#endif - - afb_req_success(req, result, NULL); -} - -/// @brief Start polling. -/// @param[in] req The query. -void verb_start(struct afb_req req) -{ - struct json_object* result; - struct json_object* arg; - - arg = afb_req_json(req); - - result = json_object_new_array(); - -#if USE_LIBNFC == 1 - if (libnfc_start_polling(result, arg)) - { - afb_req_fail(req, "Failed to get devices list from libnfc!", NULL); - return; - } -#endif - - afb_req_success(req, result, NULL); -} - -/// @brief Stop polling. -/// @param[in] req The query. -void verb_stop(struct afb_req req) -{ - afb_req_fail(req, "Not implemented yet!", NULL); -} diff --git a/src/nfc-binding.h b/src/nfc-binding.h deleted file mode 100644 index 767a448..0000000 --- a/src/nfc-binding.h +++ /dev/null @@ -1,17 +0,0 @@ -#pragma once - -#include <json-c/json.h> - -#define AFB_BINDING_VERSION 2 -#include <afb/afb-binding.h> - -// Initializations -int init(); - -// Verbs -void verb_subscribe(struct afb_req req); -void verb_unsubscribe(struct afb_req req); -void verb_list_devices(struct afb_req req); -void verb_list_devices_capabilities(struct afb_req req); -void verb_start(struct afb_req req); -void verb_stop(struct afb_req req); diff --git a/src/stringutils.h b/src/stringutils.h deleted file mode 100644 index 824d09d..0000000 --- a/src/stringutils.h +++ /dev/null @@ -1,41 +0,0 @@ -#pragma once - -#include <stdlib.h> - -/** - * @brief Get a hexadecimal string representation from memory buffer. - * @param[in] src Buffer's pointer. - * @param[in] sz Buffer's size. - * @return A pointer to the result string. Caller is responsible for the result lifetime. - */ -static inline char* to_hex_string(const void* src, long unsigned int sz) -{ - static const char lookup[] = - { - '0', '1', '2', '3', - '4', '5', '6', '7', - '8', '9', 'a', 'b', - 'c', 'd', 'e', 'f' - }; - - const char* source; - char* result; - long unsigned int i; - - result = NULL; - if (src && sz) - { - source = (const char*)src; - result = (char*)malloc(sz * 2 + 1); - if (result) - { - result[sz * 2] = 0; - for (i = 0; i < sz; ++i) - { - result[i * 2] = lookup[(source[i] & 0xf0) >> 4]; - result[i * 2 + 1] = lookup[source[i] & 0x0f]; - } - } - } - return result; -} |