diff options
author | Loïc Collignon <loic.collignon@iot.bzh> | 2017-10-23 10:51:37 +0200 |
---|---|---|
committer | Loïc Collignon <loic.collignon@iot.bzh> | 2017-10-23 10:51:37 +0200 |
commit | 44096523e0c45c6b02840f2fe2aca337510fac28 (patch) | |
tree | dc9c7b2be0750c011fd7c581df65f7b2e0a9ec16 /src/nfc-binding.c | |
parent | 5441251cae0eea3786c327b3b3386eae5bf687db (diff) |
add nfc binding
Change-Id: I1ebf8e803436430490201db533c2a5a04c04295e
Signed-off-by: Loïc Collignon <loic.collignon@iot.bzh>
Diffstat (limited to 'src/nfc-binding.c')
-rw-r--r-- | src/nfc-binding.c | 113 |
1 files changed, 113 insertions, 0 deletions
diff --git a/src/nfc-binding.c b/src/nfc-binding.c new file mode 100644 index 0000000..3d96cbb --- /dev/null +++ b/src/nfc-binding.c @@ -0,0 +1,113 @@ +#include "nfc-binding.h" + +#if USE_LIBNFC == 1 +#include "libnfc_reader.h" +#endif + +struct afb_event on_nfc_read_event; + +/// @brief Binding's initialization. +/// @return Exit code, zero on success, non-zero otherwise. +int init() +{ + on_nfc_read_event = afb_daemon_make_event("on-nfc-read"); + if (!afb_event_is_valid(on_nfc_read_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_read_event)) afb_req_fail(req, NULL, "Subscription failure!"); + else afb_req_success(req, NULL, "Subscription success!"); +} + +/// @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_read_event)) afb_req_fail(req, NULL, "Unsubscription failure!"); + else afb_req_success(req, NULL, "Unsubscription success!"); +} + +/// @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_polling(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); +} + +void verb_stop_polling(struct afb_req req) +{ + afb_req_fail(req, "Not implemented yet!", NULL); +} |