From 399800e461ad28e067a15c794f18843b48bfc919 Mon Sep 17 00:00:00 2001 From: Loïc Collignon Date: Wed, 25 Oct 2017 15:27:12 +0200 Subject: detect tag removing and raise an event MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I812c4f0b86f2f65c0d7561af95e531fd596bd45b Signed-off-by: Loïc Collignon --- src/nfc-binding.c | 35 +++++++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 8 deletions(-) (limited to 'src/nfc-binding.c') diff --git a/src/nfc-binding.c b/src/nfc-binding.c index 3d96cbb..56aa211 100644 --- a/src/nfc-binding.c +++ b/src/nfc-binding.c @@ -4,14 +4,16 @@ #include "libnfc_reader.h" #endif -struct afb_event on_nfc_read_event; +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_read_event = afb_daemon_make_event("on-nfc-read"); - if (!afb_event_is_valid(on_nfc_read_event)) + 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; @@ -31,17 +33,34 @@ int init() /// @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!"); +{ + 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_read_event)) afb_req_fail(req, NULL, "Unsubscription failure!"); - else afb_req_success(req, NULL, "Unsubscription success!"); + 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. -- cgit 1.2.3-korg