From 4eb636a052f0418fdfbe288a4a923e3ded5f1314 Mon Sep 17 00:00:00 2001 From: Szymon Jastrzebski Date: Wed, 5 Jul 2017 09:36:37 +0200 Subject: [PATCH] [NFC] Implementation for NFC preferred application Change-Id: I7322a9f5a40e9b8fd5453a3b39f6dbf1b947ba25 Signed-off-by: Szymon Jastrzebski --- src/nfc/nfc_adapter.cc | 43 +++++++++++++++++++++++++++++++++++++++++ src/nfc/nfc_adapter.h | 3 +++ src/nfc/nfc_api.js | 41 ++++++++++++++++++++++++++++++++++++++- src/nfc/nfc_instance.cc | 26 +++++++++++++++++++++++++ src/nfc/nfc_instance.h | 2 ++ src/nfc/nfc_util.cc | 9 +++++++-- src/nfc/nfc_util.h | 3 ++- src/utils/utils_api.js | 2 ++ 8 files changed, 125 insertions(+), 4 deletions(-) diff --git a/src/nfc/nfc_adapter.cc b/src/nfc/nfc_adapter.cc index 89b28a4a..be45d2fc 100644 --- a/src/nfc/nfc_adapter.cc +++ b/src/nfc/nfc_adapter.cc @@ -93,6 +93,7 @@ NFCAdapter::NFCAdapter(): m_is_ndef_listener_set(false), m_se_handle(nullptr), m_is_hce_listener_set(false), + m_is_preferred_app_set(false), responder_(nullptr) { LoggerD("Entered"); @@ -126,6 +127,9 @@ NFCAdapter::~NFCAdapter() { if (m_is_hce_listener_set) { nfc_manager_unset_hce_event_cb(); } + if (m_is_preferred_app_set) { + nfc_se_unset_preferred_handler(); + } // NFC library deinitialization int ret = nfc_manager_deinitialize(); @@ -1574,5 +1578,44 @@ void NFCAdapter::GetAIDsForCategory( success_cb(aids); } +PlatformResult NFCAdapter::SetPreferredApp() { + LoggerD("Entered"); + + if (m_is_preferred_app_set) { + return PlatformResult(ErrorCode::NO_ERROR); + } + + int ret = nfc_se_set_preferred_handler(); + if (ret != NFC_ERROR_NONE) { + LoggerE("SetPreferredApp failed: %d", ret); + return NFCUtil::CodeToResult(ret, + NFCUtil::getNFCErrorMessage(ret).c_str(), + true); + } + m_is_preferred_app_set = true; + + return PlatformResult(ErrorCode::NO_ERROR); +} + +PlatformResult NFCAdapter::UnsetPreferredApp() { + LoggerD("Entered"); + + if (!m_is_preferred_app_set) { + return PlatformResult(ErrorCode::NO_ERROR); + } + + int ret = nfc_se_unset_preferred_handler(); + if (ret != NFC_ERROR_NONE) { + LoggerE("SetPreferredApp failed: %d", ret); + return NFCUtil::CodeToResult(ret, + NFCUtil::getNFCErrorMessage(ret).c_str(), + true); + } + + m_is_preferred_app_set = false; + + return PlatformResult(ErrorCode::NO_ERROR); +} + }// nfc }// extension diff --git a/src/nfc/nfc_adapter.h b/src/nfc/nfc_adapter.h index cec14c91..40d2e4be 100644 --- a/src/nfc/nfc_adapter.h +++ b/src/nfc/nfc_adapter.h @@ -127,6 +127,8 @@ class NFCAdapter { nfc_card_emulation_category_type_e category, const std::function& success_cb, const std::function& error_cb); + common::PlatformResult SetPreferredApp(); + common::PlatformResult UnsetPreferredApp(); private: @@ -146,6 +148,7 @@ class NFCAdapter { bool m_is_ndef_listener_set; nfc_se_h m_se_handle; bool m_is_hce_listener_set; + bool m_is_preferred_app_set; IResponder* responder_; }; diff --git a/src/nfc/nfc_api.js b/src/nfc/nfc_api.js index 8aa8b58d..5448c6df 100644 --- a/src/nfc/nfc_api.js +++ b/src/nfc/nfc_api.js @@ -142,7 +142,7 @@ var activeSecureElementChangeListener = new ListenerManager(native_, ACTIVE_SECU var transactionEventListenerEse = new ListenerManager(native_, TRANSACTION_EVENT_ESE_LISTENER); var transactionEventListenerUicc = new ListenerManager(native_, TRANSACTION_EVENT_UICC_LISTENER); var HCEEventListener = new ListenerManager(native_, HCE_EVENT_LISTENER); - +var isWebkitVisibilityChangeListenerSet = false; //////////////////NFCManager ///////////////// @@ -907,6 +907,45 @@ NFCAdapter.prototype.getAIDsForCategory = function(type, category, successCallba } }; +function WebkitVisibilityChangeListener() { + var result; + if (true === privUtils_.global.document.hidden) { + result = native_.call('NFCAdapter_unsetPreferredApp'); + } else if (false === privUtils_.global.document.hidden) { + result = native_.call('NFCAdapter_setPreferredApp'); + } + + if (native_.isFailure(result)) { + privUtils_.log('Failed to (un)set: ' + result.error.message); + } +} + +NFCAdapter.prototype.setPreferredApp = function() { + var result = native_.call('NFCAdapter_setPreferredApp'); + + if (native_.isFailure(result)) { + throw native_.getErrorObject(result); + } else { + if (false === isWebkitVisibilityChangeListenerSet) { + privUtils_.global.document.addEventListener("webkitvisibilitychange", WebkitVisibilityChangeListener); + isWebkitVisibilityChangeListenerSet = true; + } + } +}; + +NFCAdapter.prototype.unsetPreferredApp = function() { + var result = native_.call('NFCAdapter_unsetPreferredApp'); + + if (native_.isFailure(result)) { + throw native_.getErrorObject(result); + } else { + if (true === isWebkitVisibilityChangeListenerSet) { + privUtils_.global.document.removeEventListener("webkitvisibilitychange", WebkitVisibilityChangeListener); + isWebkitVisibilityChangeListenerSet = false; + } + } +}; + function InternalRecordData(tnf, type, payload, id) { this.tnf = tnf; this.type = type; diff --git a/src/nfc/nfc_instance.cc b/src/nfc/nfc_instance.cc index cb9bedf1..3809aa3a 100644 --- a/src/nfc/nfc_instance.cc +++ b/src/nfc/nfc_instance.cc @@ -121,6 +121,8 @@ NFCInstance::NFCInstance() { REGISTER_SYNC("NFCAdapter_registerAID", RegisterAID); REGISTER_SYNC("NFCAdapter_unregisterAID", UnregisterAID); REGISTER_ASYNC("NFCAdapter_getAIDsForCategory", GetAIDsForCategory); + REGISTER_SYNC("NFCAdapter_setPreferredApp", SetPreferredApp); + REGISTER_SYNC("NFCAdapter_unsetPreferredApp", UnsetPreferredApp); REGISTER_SYNC("NFCPeer_setReceiveNDEFListener", SetReceiveNDEFListener); REGISTER_SYNC("NFCPeer_unsetReceiveNDEFListener", UnsetReceiveNDEFListener); @@ -1056,5 +1058,29 @@ void NFCInstance::GetAIDsForCategory(const picojson::value& args, type, required_category, success_cb, error_cb)); } +void NFCInstance::SetPreferredApp(const picojson::value& args, picojson::object& out) { + LoggerD("Entered"); + CHECK_PRIVILEGE_ACCESS(kPrivilegeNfcCardEmulation, &out); + + PlatformResult result = NFCAdapter::GetInstance()->SetPreferredApp(); + if (result.IsSuccess()) { + ReportSuccess(out); + } else { + LogAndReportError(result, &out); + } +} + +void NFCInstance::UnsetPreferredApp(const picojson::value& args, picojson::object& out) { + LoggerD("Entered"); + CHECK_PRIVILEGE_ACCESS(kPrivilegeNfcCardEmulation, &out); + + PlatformResult result = NFCAdapter::GetInstance()->UnsetPreferredApp(); + if (result.IsSuccess()) { + ReportSuccess(out); + } else { + LogAndReportError(result, &out); + } +} + } // namespace nfc } // namespace extension diff --git a/src/nfc/nfc_instance.h b/src/nfc/nfc_instance.h index 78795e42..197e8caa 100644 --- a/src/nfc/nfc_instance.h +++ b/src/nfc/nfc_instance.h @@ -85,6 +85,8 @@ class NFCInstance: public common::ParsedInstance, NFCAdapter::IResponder void RegisterAID(const picojson::value& args, picojson::object& out); void UnregisterAID(const picojson::value& args, picojson::object& out); void GetAIDsForCategory(const picojson::value& args, picojson::object& out); + void SetPreferredApp(const picojson::value& args, picojson::object& out); + void UnsetPreferredApp(const picojson::value& args, picojson::object& out); }; } // namespace nfc diff --git a/src/nfc/nfc_util.cc b/src/nfc/nfc_util.cc index 73e173e6..a9fc84c4 100644 --- a/src/nfc/nfc_util.cc +++ b/src/nfc/nfc_util.cc @@ -34,7 +34,8 @@ UCharVector NFCUtil::ToVector(const unsigned char* ch, const int size) } PlatformResult NFCUtil::CodeToResult(const int errorCode, - const std::string& message) { + const std::string& message, + const bool newAPIVersion) { LoggerD("Entered"); switch(errorCode) { case NFC_ERROR_INVALID_PARAMETER: @@ -56,7 +57,11 @@ PlatformResult NFCUtil::CodeToResult(const int errorCode, case NFC_ERROR_OUT_OF_MEMORY: case NFC_ERROR_NOT_INITIALIZED: default: - return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, message); + if (newAPIVersion) { + return LogAndCreateResult(ErrorCode::ABORT_ERR, message); + } else { + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, message); + } } } diff --git a/src/nfc/nfc_util.h b/src/nfc/nfc_util.h index 78b3784e..6879eb1a 100644 --- a/src/nfc/nfc_util.h +++ b/src/nfc/nfc_util.h @@ -62,7 +62,8 @@ class NFCUtil public: static UCharVector ToVector(const unsigned char* ch, const int size); static common::PlatformResult CodeToResult(const int errorCode, - const std::string& message); + const std::string& message, + const bool newAPIVersion = false); static std::string getNFCErrorString(const int error_code); static const std::string getNFCErrorMessage(const int error_code); static std::string ToStringNFCTag(const nfc_tag_type_e tag_type); diff --git a/src/utils/utils_api.js b/src/utils/utils_api.js index b9a4a83f..ea9a912a 100644 --- a/src/utils/utils_api.js +++ b/src/utils/utils_api.js @@ -171,6 +171,8 @@ Utils.prototype.error = console.error.bind(console); Utils.prototype.warn = console.warn.bind(console); Utils.prototype.log = _enableJsLogs ? console.log.bind(console) : function(){}; +Utils.prototype.global = _global; + Utils.prototype.repackFilter = function(filter) { if (filter instanceof tizen.AttributeFilter) { return { -- 2.34.1