From: Grzegorz Rynkowski Date: Fri, 27 Mar 2015 08:51:28 +0000 (+0100) Subject: Revert "Revert "[NFC] Native part implementation of HCE"" X-Git-Tag: submit/tizen_tv/20150603.064601~1^2~202 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=3fc225a652bdfc44014166cba51f5fc5f57e5355;p=platform%2Fcore%2Fapi%2Fwebapi-plugins.git Revert "Revert "[NFC] Native part implementation of HCE"" This reverts commit 4ab5b92925afa10559da41d695ddfb13aa07709f. Build break was repaired. Verification: 1. Successful build on tizen 2.4 2. TCT without change - 91/91 Change-Id: I1bde34473ca0211a3302e0cb8a5e024c3e8441fe Signed-off-by: Grzegorz Rynkowski --- diff --git a/src/nfc/aid_data.cc b/src/nfc/aid_data.cc new file mode 100644 index 00000000..bad5b376 --- /dev/null +++ b/src/nfc/aid_data.cc @@ -0,0 +1,26 @@ +// Copyright 2015 Samsung Electronics Co, Ltd. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "nfc/aid_data.h" + +namespace extension { +namespace nfc { + +AIDData::AIDData(nfc_se_type_e se_type, const char* aid, bool read_only) + : se_type_(se_type), + aid_(aid), + read_only_(read_only) {} + +picojson::value AIDData::toJSON() const { + picojson::value retval = picojson::value(picojson::object()); + picojson::object& obj = retval.get(); + + obj["aid"] = picojson::value(); + obj["type"] = picojson::value(); + obj["readOnly"] = picojson::value(); + return retval; +} + +} // nfc +} // extension \ No newline at end of file diff --git a/src/nfc/aid_data.h b/src/nfc/aid_data.h new file mode 100644 index 00000000..3d68c7c7 --- /dev/null +++ b/src/nfc/aid_data.h @@ -0,0 +1,33 @@ +// Copyright 2015 Samsung Electronics Co, Ltd. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef NFC_AID_DATA_H_ +#define NFC_AID_DATA_H_ + +#include + +#include + +#include "common/picojson.h" + +namespace extension { +namespace nfc { + +class AIDData { + public: + AIDData(nfc_se_type_e se_type, const char* aid, bool read_only); + picojson::value toJSON() const; + + private: + nfc_se_type_e se_type_; + const char* aid_; + bool read_only_; +}; + +typedef std::vector AIDDataVector; + +} // nfc +} // extension + +#endif // NFC_AID_DATA_H_ \ No newline at end of file diff --git a/src/nfc/defs.h b/src/nfc/defs.h new file mode 100644 index 00000000..e354075d --- /dev/null +++ b/src/nfc/defs.h @@ -0,0 +1,23 @@ +// Copyright 2015 Samsung Electronics Co, Ltd. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef NFC_DEFS_H +#define NFC_DEFS_H + +#define JSON_AID "aid" +#define JSON_APDU "apdu" +#define JSON_CALLBACK_ID "callbackId" +#define JSON_CATEGORY "category" +#define JSON_DATA "data" +#define JSON_EVENT_TYPE "eventType" +#define JSON_LENGTH "length" +#define JSON_LISTENER_ID "listenerId" +#define JSON_MODE "mode" +#define JSON_TYPE "type" + +#define DATA_NFC_SE_TYPE_ESE "ESE" +#define DATA_NFC_SE_TYPE_UICC "UICC" +#define DATA_NFC_SE_TYPE_HCE "HCE" + +#endif // NFC_DEFS_H diff --git a/src/nfc/nfc.gyp b/src/nfc/nfc.gyp index 7fce11cf..886a51bf 100644 --- a/src/nfc/nfc.gyp +++ b/src/nfc/nfc.gyp @@ -20,7 +20,10 @@ 'nfc_util.cc', 'nfc_util.h', 'nfc_message_utils.cc', - 'nfc_message_utils.h' + 'nfc_message_utils.h', + 'nfc_platform_callbacks.h', + 'aid_data.cc', + 'aid_data.h' ], 'includes': [ '../common/pkg-config.gypi', diff --git a/src/nfc/nfc_adapter.cc b/src/nfc/nfc_adapter.cc index 41217336..7339788f 100644 --- a/src/nfc/nfc_adapter.cc +++ b/src/nfc/nfc_adapter.cc @@ -2,16 +2,22 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "nfc_adapter.h" -#include "nfc_util.h" -#include "nfc_message_utils.h" +#include "nfc/nfc_adapter.h" #include + #include +#include +#include "common/assert.h" +#include "common/converter.h" +#include "common/extension.h" #include "common/logger.h" #include "common/platform_exception.h" -#include "common/converter.h" +#include "nfc/aid_data.h" +#include "nfc/defs.h" +#include "nfc/nfc_message_utils.h" +#include "nfc/nfc_util.h" using namespace common; using namespace std; @@ -20,20 +26,45 @@ namespace extension { namespace nfc { namespace { -const std::string CALLBACK_ID = "callbackId"; -const std::string LISTENER_ID = "listenerId"; -const std::string TYPE = "type"; -const std::string MODE = "mode"; const std::string CARD_ELEMENT = "CardElement"; const std::string TRANSACTION = "Transaction"; +const std::string HCE_EVENT_DATA = "HCEEventData"; const std::string ACTIVE_SECURE_ELEMENT_CHANGED = "ActiveSecureElementChanged"; const std::string CARD_EMULATION_MODE_CHANGED = "CardEmulationModeChanged"; const std::string TRANSACTION_EVENT_LISTENER_ESE = "TransactionEventListener_ESE"; const std::string TRANSACTION_EVENT_LISTENER_UICC = "TransactionEventListener_UICC"; +const std::string PEER_LISTENER = "PeerListener"; +const std::string HCE_EVENT_LISTENER = "HCEEventListener"; + +void HCEEventCallback(nfc_se_h handle, + nfc_hce_event_type_e event_type, + unsigned char* apdu, + unsigned int apdu_len, + void* /*user_data*/) { + LoggerD("Entered"); + NFCAdapter::GetInstance()->SetSEHandle(handle); + + picojson::value response = picojson::value(picojson::object()); + picojson::object& response_obj = response.get(); + response_obj[JSON_LISTENER_ID] = picojson::value(HCE_EVENT_LISTENER); + response_obj[JSON_TYPE] = picojson::value(HCE_EVENT_DATA); + + // HCE Event Data + picojson::value event_data = picojson::value(picojson::object()); + picojson::object& event_data_obj = event_data.get(); + event_data_obj[JSON_EVENT_TYPE] = picojson::value(NFCUtil::ToStr(event_type)); + event_data_obj[JSON_APDU] = picojson::value( + NFCUtil::FromUCharArray(apdu, apdu_len)); + event_data_obj[JSON_LENGTH] = picojson::value(static_cast(apdu_len)); + tools::ReportSuccess(event_data, response_obj); + + NFCAdapter::GetInstance()->RespondAsync(response.serialize().c_str()); } +} // anonymous namespace + NFCAdapter::NFCAdapter(): m_is_listener_set(false), m_is_transaction_ese_listener_set(false), @@ -41,18 +72,23 @@ NFCAdapter::NFCAdapter(): m_is_peer_listener_set(false), m_is_tag_listener_set(false), m_latest_peer_id(0), - m_peer_handle(NULL), + m_peer_handle(nullptr), m_is_ndef_listener_set(false), m_latest_tag_id(0), - m_last_tag_handle(NULL) -{ + m_last_tag_handle(nullptr), + m_se_handle(nullptr), + responder_(nullptr) { + // NFC library initialization + int ret = nfc_manager_initialize(); + if(ret != NFC_ERROR_NONE) { + LoggerE("Could not initialize NFC Manager, error: %d", ret); + } } NFCAdapter::~NFCAdapter() { if (m_is_listener_set) { nfc_manager_unset_se_event_cb(); } - if (m_is_peer_listener_set) { nfc_manager_unset_p2p_target_discovered_cb(); } @@ -68,6 +104,24 @@ NFCAdapter::~NFCAdapter() { if (m_is_tag_listener_set) { nfc_manager_unset_tag_discovered_cb(); } + if (m_is_hce_listener_set) { + nfc_manager_unset_hce_event_cb(); + } + + // NFC library deinitialization + int ret = nfc_manager_deinitialize(); + if(ret != NFC_ERROR_NONE) { + LoggerE("Could not deinitialize NFC Manager, error: %d", ret); + } +} + +void NFCAdapter::SetResponder(IResponder* responder) { + responder_ = responder; +} + +void NFCAdapter::RespondAsync(const char* msg) { + AssertMsg(GetInstance()->responder_, "Handler variable should be set"); + GetInstance()->responder_->RespondAsync(msg); } static picojson::value CreateEventError(double callbackId, const PlatformResult& ret) { @@ -75,7 +129,7 @@ static picojson::value CreateEventError(double callbackId, const PlatformResult& picojson::value event = picojson::value(picojson::object()); picojson::object& obj = event.get(); tools::ReportError(ret, &obj); - obj.insert(std::make_pair(CALLBACK_ID, picojson::value(callbackId))); + obj[JSON_CALLBACK_ID] = picojson::value(callbackId); return event; } @@ -84,16 +138,16 @@ static picojson::value createEventSuccess(double callbackId) { picojson::value event = picojson::value(picojson::object()); picojson::object& obj = event.get(); tools::ReportSuccess(obj); - obj.insert(std::make_pair(CALLBACK_ID, picojson::value(callbackId))); + obj[JSON_CALLBACK_ID] = picojson::value(callbackId); return event; } -static gboolean setPoweredCompleteCB(void * user_data) { +static gboolean setPoweredCompleteCB(void* user_data) { double* callbackId = static_cast(user_data); picojson::value event = createEventSuccess(*callbackId); - NFCInstance::getInstance().PostMessage(event.serialize().c_str()); + NFCAdapter::GetInstance()->RespondAsync(event.serialize().c_str()); delete callbackId; callbackId = NULL; @@ -101,11 +155,12 @@ static gboolean setPoweredCompleteCB(void * user_data) { } static void targetDetectedCallback(nfc_discovered_type_e type, - nfc_p2p_target_h target, void *data) { - + nfc_p2p_target_h target, + void* /*user_data*/) { + LoggerD("Entered"); picojson::value event = picojson::value(picojson::object()); picojson::object& obj = event.get(); - obj.insert(make_pair("listenerId", picojson::value("PeerListener"))); + obj[JSON_LISTENER_ID] = picojson::value(PEER_LISTENER); NFCAdapter* adapter = NFCAdapter::GetInstance(); @@ -116,15 +171,14 @@ static void targetDetectedCallback(nfc_discovered_type_e type, if (NFC_DISCOVERED_TYPE_ATTACHED == type) { adapter->SetPeerHandle(target); - obj.insert(make_pair("action", picojson::value("onattach"))); + obj["action"] = picojson::value("onattach"); adapter->IncreasePeerId(); - obj.insert(make_pair("id", picojson::value(static_cast(adapter->GetPeerId())))); - NFCInstance::getInstance().PostMessage(event.serialize().c_str()); + obj["id"] = picojson::value(static_cast(adapter->GetPeerId())); } else { adapter->SetPeerHandle(NULL); - obj.insert(make_pair("action", picojson::value("ondetach"))); - NFCInstance::getInstance().PostMessage(event.serialize().c_str()); + obj["action"] = picojson::value("ondetach"); } + NFCAdapter::GetInstance()->RespondAsync(event.serialize().c_str()); } NFCAdapter* NFCAdapter::GetInstance() { @@ -138,19 +192,20 @@ bool NFCAdapter::GetPowered() { #ifndef APP_CONTROL_SETTING_SUPPORT -static void NFCSetActivationCompletedCallback(nfc_error_e error, void *user_data) -{ +static void NFCSetActivationCompletedCallback(nfc_error_e error, + void* user_data) { + LoggerD("Entered"); double* callbackId = static_cast(user_data); if (NFC_ERROR_NONE != error) { PlatformResult result = NFCUtil::CodeToResult(error, NFCUtil::getNFCErrorMessage(error).c_str()); picojson::value event = CreateEventError(*callbackId, result); - NFCInstance::getInstance().PostMessage(event.serialize().c_str()); + NFCAdapter::GetInstance()->RespondAsync(event.serialize().c_str()); } else { picojson::value event = createEventSuccess(*callbackId); - NFCInstance::getInstance().PostMessage(event.serialize().c_str()); + NFCAdapter::GetInstance()->RespondAsync(event.serialize().c_str()); } delete callbackId; callbackId = NULL; @@ -158,45 +213,48 @@ static void NFCSetActivationCompletedCallback(nfc_error_e error, void *user_data #endif -static void se_event_callback(nfc_se_event_e se_event, void *user_data) { - +static void se_event_callback(nfc_se_event_e se_event, void* /*user_data*/) { + LoggerD("Entered"); picojson::value event = picojson::value(picojson::object()); picojson::object& obj = event.get(); tools::ReportSuccess(obj); + LoggerD("se_event_occured: %d", se_event); string result = ""; switch (se_event) { case NFC_SE_EVENT_SE_TYPE_CHANGED: NFCAdapter::GetInstance()->GetActiveSecureElement(&result); - obj.insert(make_pair(LISTENER_ID, picojson::value(ACTIVE_SECURE_ELEMENT_CHANGED))); + obj.insert(make_pair(JSON_LISTENER_ID, + picojson::value(ACTIVE_SECURE_ELEMENT_CHANGED))); break; case NFC_SE_EVENT_CARD_EMULATION_CHANGED: NFCAdapter::GetInstance()->GetCardEmulationMode(&result); - obj.insert(make_pair(LISTENER_ID, picojson::value(CARD_EMULATION_MODE_CHANGED))); + obj.insert(make_pair(JSON_LISTENER_ID, + picojson::value(CARD_EMULATION_MODE_CHANGED))); break; default: - LOGD("se_event_occured: %d", se_event); + LoggerE("Unsupported se_event: %d", se_event); return; } - obj.insert(make_pair(TYPE, picojson::value(CARD_ELEMENT))); - obj.insert(make_pair(MODE, picojson::value(result))); - NFCInstance::getInstance().PostMessage(event.serialize().c_str()); + obj.insert(make_pair(JSON_TYPE, picojson::value(CARD_ELEMENT))); + obj.insert(make_pair(JSON_MODE, picojson::value(result))); + NFCAdapter::GetInstance()->RespondAsync(event.serialize().c_str()); } static void transaction_event_callback(nfc_se_type_e type, - unsigned char *_aid, + unsigned char* _aid, int aid_size, - unsigned char *param, + unsigned char* param, int param_size, - void *user_data) -{ + void* /*user_data*/) { + LoggerD("Entered"); picojson::value response = picojson::value(picojson::object()); picojson::object& response_obj = response.get(); tools::ReportSuccess(response_obj); - picojson::array& aid_array = response_obj.insert(std::make_pair("aid", + picojson::array& aid_array = response_obj.insert(std::make_pair(JSON_AID, picojson::value(picojson::array()))).first->second.get(); - picojson::array& data_array = response_obj.insert(std::make_pair("data", + picojson::array& data_array = response_obj.insert(std::make_pair(JSON_DATA, picojson::value(picojson::array()))).first->second.get(); for (unsigned int i = 0; i < aid_size; i++) { @@ -208,21 +266,23 @@ static void transaction_event_callback(nfc_se_type_e type, } if (NFC_SE_TYPE_ESE == type) { - response_obj.insert(make_pair(LISTENER_ID, picojson::value(TRANSACTION_EVENT_LISTENER_ESE))); + response_obj.insert(make_pair(JSON_LISTENER_ID, + picojson::value(TRANSACTION_EVENT_LISTENER_ESE))); } else { - response_obj.insert(make_pair(LISTENER_ID, picojson::value(TRANSACTION_EVENT_LISTENER_UICC))); + response_obj.insert(make_pair(JSON_LISTENER_ID, + picojson::value(TRANSACTION_EVENT_LISTENER_UICC))); } - response_obj.insert(make_pair(TYPE, picojson::value(TRANSACTION))); - NFCInstance::getInstance().PostMessage(response.serialize().c_str()); + response_obj.insert(make_pair(JSON_TYPE, picojson::value(TRANSACTION))); + NFCAdapter::GetInstance()->RespondAsync(response.serialize().c_str()); } #ifdef APP_CONTROL_SETTING_SUPPORT -static void PostMessage(double *callbackId) { +static void PostMessage(double* callbackId) { picojson::value event = CreateEventError(*callbackId, PlatformResult(ErrorCode::UNKNOWN_ERR, "SetPowered failed.")); - NFCInstance::getInstance().PostMessage(event.serialize().c_str()); + NFCAdapter::GetInstance()->RespondAsync(event.serialize().c_str()); delete callbackId; callbackId = NULL; } @@ -230,12 +290,12 @@ static void PostMessage(double *callbackId) { PlatformResult NFCAdapter::SetPowered(const picojson::value& args) { - double* callbackId = new double(args.get(CALLBACK_ID).get()); + double* callbackId = new double(args.get(JSON_CALLBACK_ID).get()); bool powered = args.get("powered").get(); if (nfc_manager_is_activated() == powered) { - if (!g_idle_add(setPoweredCompleteCB, static_cast(callbackId))) { + if (!g_idle_add(setPoweredCompleteCB, static_cast(callbackId))) { LOGE("g_idle addition failed"); delete callbackId; callbackId = NULL; @@ -275,10 +335,10 @@ PlatformResult NFCAdapter::SetPowered(const picojson::value& args) { } ret = app_control_send_launch_request(service, [](app_control_h request, - app_control_h reply, app_control_result_e result, void *user_data) { + app_control_h reply, app_control_result_e result, void* user_data) { double* callbackId = static_cast(user_data); if (result == APP_CONTROL_RESULT_SUCCEEDED) { - char *type = NULL; + char* type = NULL; int ret = app_control_get_extra_data(reply, "nfc_status", &type); if (ret != APP_CONTROL_ERROR_NONE) { @@ -293,12 +353,12 @@ PlatformResult NFCAdapter::SetPowered(const picojson::value& args) { return; } - if (!g_idle_add(setPoweredCompleteCB, static_cast(callbackId))) { + if (!g_idle_add(setPoweredCompleteCB, static_cast(callbackId))) { LOGE("g_idle addition failed"); PostMessage(callbackId); return; } - }, static_cast(callbackId)); + }, static_cast(callbackId)); if (ret != APP_CONTROL_ERROR_NONE) { LOGE("app_control_send_launch_request failed: %d", ret); @@ -315,7 +375,7 @@ PlatformResult NFCAdapter::SetPowered(const picojson::value& args) { } #else int ret = nfc_manager_set_activation(powered, - NFCSetActivationCompletedCallback, static_cast(callbackId)); + NFCSetActivationCompletedCallback, static_cast(callbackId)); if (NFC_ERROR_NONE != ret) { LOGE("setPowered failed %d",ret); @@ -327,9 +387,7 @@ PlatformResult NFCAdapter::SetPowered(const picojson::value& args) { return PlatformResult(ErrorCode::NO_ERROR); } - -PlatformResult NFCAdapter::GetCardEmulationMode(std::string *mode) { - +PlatformResult NFCAdapter::GetCardEmulationMode(std::string* mode) { LoggerD("Entered"); nfc_se_card_emulation_mode_type_e card_mode; @@ -344,7 +402,6 @@ PlatformResult NFCAdapter::GetCardEmulationMode(std::string *mode) { } PlatformResult NFCAdapter::SetCardEmulationMode(std::string mode) { - LoggerD("Entered"); nfc_se_card_emulation_mode_type_e new_mode = @@ -387,22 +444,21 @@ PlatformResult NFCAdapter::SetCardEmulationMode(std::string mode) { return PlatformResult(ErrorCode::NO_ERROR); } -PlatformResult NFCAdapter::GetActiveSecureElement(std::string *type) { - +PlatformResult NFCAdapter::GetActiveSecureElement(std::string* type) { LoggerD("Entered"); nfc_se_type_e se_type = NFC_SE_TYPE_DISABLE; int ret = nfc_manager_get_se_type(&se_type); if (NFC_ERROR_NONE != ret) { LoggerE("Failed to get active secure element type: %d", ret); - return NFCUtil::CodeToResult(ret, "Unable to get active secure element type"); + return NFCUtil::CodeToResult(ret, + "Unable to get active secure element type"); } return NFCUtil::ToStringSecureElementType(se_type, type); } PlatformResult NFCAdapter::SetActiveSecureElement(std::string element) { - LoggerD("Entered"); // if given value is not correct secure element type then @@ -431,13 +487,12 @@ PlatformResult NFCAdapter::SetActiveSecureElement(std::string element) { if (NFC_ERROR_NONE != ret) { LoggerE("Failed to set active secure element type: %d", ret); return NFCUtil::CodeToResult(ret, - "Unable to set active secure element type"); + "Unable to set active secure element type"); } return PlatformResult(ErrorCode::NO_ERROR); } PlatformResult NFCAdapter::SetExclusiveModeForTransaction(bool exmode) { - LoggerD("Entered"); int ret = NFC_ERROR_NONE; @@ -450,14 +505,14 @@ PlatformResult NFCAdapter::SetExclusiveModeForTransaction(bool exmode) { if (NFC_ERROR_NONE != ret) { LoggerE("Failed to set exclusive mode for transaction: %d", ret); return NFCUtil::CodeToResult(ret, - "Setting exclusive mode for transaction failed."); + "Setting exclusive mode for transaction failed."); } return PlatformResult(ErrorCode::NO_ERROR); } PlatformResult NFCAdapter::AddCardEmulationModeChangeListener() { if (!m_is_listener_set) { - int ret = nfc_manager_set_se_event_cb(se_event_callback, NULL); + int ret = nfc_manager_set_se_event_cb(se_event_callback, nullptr); if (NFC_ERROR_NONE != ret) { LOGE("AddCardEmulationModeChangeListener failed: %d", ret); return NFCUtil::CodeToResult(ret, @@ -483,60 +538,72 @@ PlatformResult NFCAdapter::RemoveCardEmulationModeChangeListener() { } -PlatformResult NFCAdapter::AddTransactionEventListener(const picojson::value& args) { +PlatformResult NFCAdapter::AddTransactionEventListener( + const picojson::value& args) { nfc_se_type_e se_type = NFC_SE_TYPE_DISABLE; PlatformResult result = NFCUtil::ToSecureElementType( - args.get("type").get(), &se_type); + args.get(JSON_TYPE).get(), &se_type); if (result.IsError()) { return result; } - int ret = NFC_ERROR_NONE; + int ret = NFC_ERROR_NONE; if (NFC_SE_TYPE_ESE == se_type) { if (!m_is_transaction_ese_listener_set) { ret = nfc_manager_set_se_transaction_event_cb(se_type, - transaction_event_callback, NULL); + transaction_event_callback, NULL); m_is_transaction_ese_listener_set = true; } - } else { + } else if (NFC_SE_TYPE_UICC == se_type) { if (!m_is_transaction_uicc_listener_set) { ret = nfc_manager_set_se_transaction_event_cb(se_type, - transaction_event_callback, NULL); + transaction_event_callback, NULL); m_is_transaction_uicc_listener_set = true; } + } else if (NFC_SE_TYPE_HCE == se_type) { + if (!m_is_transaction_hce_listener_set) { + ret = nfc_manager_set_se_transaction_event_cb(se_type, + transaction_event_callback, NULL); + m_is_transaction_hce_listener_set = true; + } } if (NFC_ERROR_NONE != ret) { LOGE("AddTransactionEventListener failed: %d", ret); - return NFCUtil::CodeToResult(ret, - NFCUtil::getNFCErrorMessage(ret).c_str()); + return NFCUtil::CodeToResult(ret, NFCUtil::getNFCErrorMessage(ret).c_str()); } return PlatformResult(ErrorCode::NO_ERROR); } -PlatformResult NFCAdapter::RemoveTransactionEventListener(const picojson::value& args) { +PlatformResult NFCAdapter::RemoveTransactionEventListener( + const picojson::value& args) { nfc_se_type_e se_type = NFC_SE_TYPE_DISABLE; - PlatformResult result = NFCUtil::ToSecureElementType( - args.get("type").get(), &se_type); + PlatformResult result = + NFCUtil::ToSecureElementType(args.get(JSON_TYPE).get(), &se_type); if (result.IsError()) { return result; } - nfc_manager_unset_se_transaction_event_cb(se_type); - if (se_type == NFC_SE_TYPE_ESE) { + nfc_manager_unset_se_transaction_event_cb(se_type); m_is_transaction_ese_listener_set = false; - } else { + } else if (se_type == NFC_SE_TYPE_UICC) { + nfc_manager_unset_se_transaction_event_cb(se_type); m_is_transaction_uicc_listener_set = false; + } else if (se_type == NFC_SE_TYPE_HCE) { + nfc_manager_unset_se_transaction_event_cb(se_type); + m_is_transaction_hce_listener_set = false; + } else { + AssertMsg(false, "Invalid NFC SecureElement type"); } return PlatformResult(ErrorCode::NO_ERROR); } PlatformResult NFCAdapter::AddActiveSecureElementChangeListener() { if (!m_is_listener_set) { - int ret = nfc_manager_set_se_event_cb(se_event_callback, NULL); + int ret = nfc_manager_set_se_event_cb(se_event_callback, nullptr); if (NFC_ERROR_NONE != ret) { LOGE("AddActiveSecureElementChangeListener failed: %d", ret); return NFCUtil::CodeToResult(ret, @@ -577,7 +644,7 @@ void NFCAdapter::IncreasePeerId() { m_latest_peer_id++; } -PlatformResult NFCAdapter::PeerIsConnectedGetter(int peer_id, bool *state) { +PlatformResult NFCAdapter::PeerIsConnectedGetter(int peer_id, bool* state) { if (m_latest_peer_id != peer_id || !m_peer_handle) { *state = false; return PlatformResult(ErrorCode::NO_ERROR); @@ -626,9 +693,10 @@ PlatformResult NFCAdapter::UnsetPeerListener() { return PlatformResult(ErrorCode::NO_ERROR); } -static void targetReceivedCallback(nfc_p2p_target_h target, nfc_ndef_message_h message, void *data) -{ - unsigned char *raw_data = NULL; +static void targetReceivedCallback(nfc_p2p_target_h /*target*/, + nfc_ndef_message_h message, + void* /*data*/) { + unsigned char* raw_data = NULL; unsigned int size; if (NFC_ERROR_NONE != nfc_ndef_message_get_rawdata(message, &raw_data, &size)) { LOGE("Unknown error while getting raw data of message."); @@ -643,12 +711,11 @@ static void targetReceivedCallback(nfc_p2p_target_h target, nfc_ndef_message_h m NFCMessageUtils::ReportNdefMessageFromData(raw_data, size, obj); - NFCInstance::getInstance().PostMessage(event.serialize().c_str()); + NFCAdapter::GetInstance()->RespondAsync(event.serialize().c_str()); free(raw_data); } PlatformResult NFCAdapter::SetReceiveNDEFListener(int peer_id) { - //unregister previous NDEF listener if (m_is_ndef_listener_set) { int ret = nfc_p2p_unset_data_received_cb(m_peer_handle); @@ -711,8 +778,7 @@ bool NFCAdapter::IsNDEFListenerSet() { // NFCTag related functions -PlatformResult NFCAdapter::TagTypeGetter(int tag_id, std::string* type) { - +PlatformResult NFCAdapter::TagTypeGetter(int /*tag_id*/, std::string* type) { LoggerD("Entered"); nfc_tag_type_e nfc_type = NFC_UNKNOWN_TARGET; @@ -728,8 +794,8 @@ PlatformResult NFCAdapter::TagTypeGetter(int tag_id, std::string* type) { return PlatformResult(ErrorCode::NO_ERROR); } -PlatformResult NFCAdapter::TagIsSupportedNDEFGetter(int tag_id, bool *is_supported) { - +PlatformResult NFCAdapter::TagIsSupportedNDEFGetter(int /*tag_id*/, + bool* is_supported) { LoggerD("Entered"); int err = nfc_tag_is_support_ndef(m_last_tag_handle, is_supported); @@ -742,8 +808,8 @@ PlatformResult NFCAdapter::TagIsSupportedNDEFGetter(int tag_id, bool *is_support return PlatformResult(ErrorCode::NO_ERROR); } -PlatformResult NFCAdapter::TagNDEFSizeGetter(int tag_id, unsigned int *size) { - +PlatformResult NFCAdapter::TagNDEFSizeGetter(int /*tag_id*/, + unsigned int* size) { LoggerD("Entered"); int err = nfc_tag_get_ndef_size(m_last_tag_handle, size); @@ -755,11 +821,10 @@ PlatformResult NFCAdapter::TagNDEFSizeGetter(int tag_id, unsigned int *size) { return PlatformResult(ErrorCode::NO_ERROR); } -static bool tagPropertiesGetterCb(const char *key, - const unsigned char *value, +static bool tagPropertiesGetterCb(const char* key, + const unsigned char* value, int value_size, - void *user_data) -{ + void* user_data) { if (user_data) { UCharVector tag_info = NFCUtil::toVector(value, value_size); (static_cast(user_data))->push_back( @@ -769,8 +834,8 @@ static bool tagPropertiesGetterCb(const char *key, return false; } -PlatformResult NFCAdapter::TagPropertiesGetter(int tag_id, NFCTagPropertiesT *properties) { - +PlatformResult NFCAdapter::TagPropertiesGetter(int /*tag_id*/, + NFCTagPropertiesT* properties) { LoggerD("Entered"); int err = nfc_tag_foreach_information(m_last_tag_handle, @@ -784,8 +849,7 @@ PlatformResult NFCAdapter::TagPropertiesGetter(int tag_id, NFCTagPropertiesT *pr return PlatformResult(ErrorCode::NO_ERROR); } -PlatformResult NFCAdapter::TagIsConnectedGetter(int tag_id, bool *state) { - +PlatformResult NFCAdapter::TagIsConnectedGetter(int tag_id, bool* state) { LoggerD("Entered"); PlatformResult result = PlatformResult(ErrorCode::NO_ERROR); @@ -825,8 +889,9 @@ int NFCAdapter::GetNextTagId() { } -static void tagEventCallback(nfc_discovered_type_e type, nfc_tag_h tag, void *data) -{ +static void tagEventCallback(nfc_discovered_type_e type, + nfc_tag_h tag, + void* /*data*/) { LoggerD("Entered"); picojson::value event = picojson::value(picojson::object()); @@ -852,7 +917,7 @@ static void tagEventCallback(nfc_discovered_type_e type, nfc_tag_h tag, void *da obj.insert(make_pair("id", picojson::value(static_cast(generated_id)))); obj.insert(make_pair("type", picojson::value(NFCUtil::ToStringNFCTag(tag_type)))); - NFCInstance::getInstance().PostMessage(event.serialize().c_str()); + NFCAdapter::GetInstance()->RespondAsync(event.serialize().c_str()); } // Tag disconnected event else if (NFC_DISCOVERED_TYPE_DETACHED == type) { @@ -860,7 +925,7 @@ static void tagEventCallback(nfc_discovered_type_e type, nfc_tag_h tag, void *da adapter->SetTagHandle(NULL); obj.insert(make_pair("action", picojson::value("ondetach"))); - NFCInstance::getInstance().PostMessage(event.serialize().c_str()); + NFCAdapter::GetInstance()->RespondAsync(event.serialize().c_str()); } // ERROR - should never happen else { @@ -869,8 +934,7 @@ static void tagEventCallback(nfc_discovered_type_e type, nfc_tag_h tag, void *da } -PlatformResult NFCAdapter::SetTagListener(){ - +PlatformResult NFCAdapter::SetTagListener() { LoggerD("Entered"); if (!m_is_tag_listener_set) { @@ -885,8 +949,7 @@ PlatformResult NFCAdapter::SetTagListener(){ return PlatformResult(ErrorCode::NO_ERROR); } -void NFCAdapter::UnsetTagListener(){ - +void NFCAdapter::UnsetTagListener() { LoggerD("Entered"); if(m_is_tag_listener_set) { @@ -896,14 +959,12 @@ void NFCAdapter::UnsetTagListener(){ } void NFCAdapter::SetTagHandle(nfc_tag_h tag) { - - LoggerD("Entered"); - m_last_tag_handle = tag; } -static void tagReadNDEFCb(nfc_error_e result , nfc_ndef_message_h message , void *data) -{ +static void tagReadNDEFCb(nfc_error_e result, + nfc_ndef_message_h message, + void* data) { LoggerD("Entered"); if(!data) { @@ -919,11 +980,11 @@ static void tagReadNDEFCb(nfc_error_e result , nfc_ndef_message_h message , void // create exception and post error message (call error callback) picojson::value event = CreateEventError(callbackId, PlatformResult(ErrorCode::UNKNOWN_ERR, "Failed to read NDEF message")); - NFCInstance::getInstance().PostMessage(event.serialize().c_str()); + NFCAdapter::GetInstance()->RespondAsync(event.serialize().c_str()); return; } - unsigned char *raw_data = NULL; + unsigned char* raw_data = NULL; unsigned int size = 0; int ret = nfc_ndef_message_get_rawdata(message, &raw_data, &size); @@ -936,7 +997,7 @@ static void tagReadNDEFCb(nfc_error_e result , nfc_ndef_message_h message , void // create exception and post error message (call error callback) picojson::value event = CreateEventError(callbackId, PlatformResult(ErrorCode::UNKNOWN_ERR, "Failed to retrieve NDEF message data")); - NFCInstance::getInstance().PostMessage(event.serialize().c_str()); + NFCAdapter::GetInstance()->RespondAsync(event.serialize().c_str()); return; } @@ -946,12 +1007,12 @@ static void tagReadNDEFCb(nfc_error_e result , nfc_ndef_message_h message , void NFCMessageUtils::ReportNdefMessageFromData(raw_data, size, obj); - NFCInstance::getInstance().PostMessage(event.serialize().c_str()); + NFCAdapter::GetInstance()->RespondAsync(event.serialize().c_str()); free(raw_data); } -PlatformResult NFCAdapter::TagReadNDEF(int tag_id, const picojson::value& args) { - +PlatformResult NFCAdapter::TagReadNDEF(int tag_id, + const picojson::value& args) { LoggerD("Entered"); bool is_connected = false; @@ -960,14 +1021,14 @@ PlatformResult NFCAdapter::TagReadNDEF(int tag_id, const picojson::value& args) return result; } - double callbackId = args.get(CALLBACK_ID).get(); + double callbackId = args.get(JSON_CALLBACK_ID).get(); LoggerD("Received callback id: %f", callbackId); if(!is_connected) { picojson::value event = CreateEventError(callbackId, PlatformResult(ErrorCode::UNKNOWN_ERR, "Tag is no more connected.")); - NFCInstance::getInstance().PostMessage(event.serialize().c_str()); + NFCAdapter::GetInstance()->RespondAsync(event.serialize().c_str()); return PlatformResult(ErrorCode::NO_ERROR);; } @@ -992,14 +1053,14 @@ PlatformResult NFCAdapter::TagReadNDEF(int tag_id, const picojson::value& args) result = NFCUtil::CodeToResult(ret, errMessage.c_str()); picojson::value event = CreateEventError(callbackId, result); - NFCInstance::getInstance().PostMessage(event.serialize().c_str()); + NFCAdapter::GetInstance()->RespondAsync(event.serialize().c_str()); } return PlatformResult(ErrorCode::NO_ERROR); } -PlatformResult NFCAdapter::TagWriteNDEF(int tag_id, const picojson::value& args) { - +PlatformResult NFCAdapter::TagWriteNDEF(int tag_id, + const picojson::value& args) { LoggerD("Entered"); bool is_connected = false; @@ -1008,14 +1069,14 @@ PlatformResult NFCAdapter::TagWriteNDEF(int tag_id, const picojson::value& args) return result; } - double callbackId = args.get(CALLBACK_ID).get(); + double callbackId = args.get(JSON_CALLBACK_ID).get(); LoggerD("Received callback id: %f", callbackId); if(!is_connected) { picojson::value event = CreateEventError(callbackId, PlatformResult(ErrorCode::UNKNOWN_ERR, "Tag is no more connected.")); - NFCInstance::getInstance().PostMessage(event.serialize().c_str()); + NFCAdapter::GetInstance()->RespondAsync(event.serialize().c_str()); return PlatformResult(ErrorCode::NO_ERROR);; } @@ -1049,25 +1110,26 @@ PlatformResult NFCAdapter::TagWriteNDEF(int tag_id, const picojson::value& args) result = NFCUtil::CodeToResult(ret, errMessage.c_str()); picojson::value event = CreateEventError(callbackId, result); // create and post error message - NFCInstance::getInstance().PostMessage(event.serialize().c_str()); + NFCAdapter::GetInstance()->RespondAsync(event.serialize().c_str()); } else { // create and post success message picojson::value event = createEventSuccess(callbackId); - NFCInstance::getInstance().PostMessage(event.serialize().c_str()); + NFCAdapter::GetInstance()->RespondAsync(event.serialize().c_str()); } } else { LoggerE("Invalid message handle"); picojson::value event = CreateEventError(callbackId, PlatformResult(ErrorCode::INVALID_VALUES_ERR, "Message is not valid")); - NFCInstance::getInstance().PostMessage(event.serialize().c_str()); + NFCAdapter::GetInstance()->RespondAsync(event.serialize().c_str()); } return PlatformResult(ErrorCode::NO_ERROR); } -static void tagTransceiveCb(nfc_error_e err, unsigned char *buffer, - int buffer_size, void* data) { - +static void tagTransceiveCb(nfc_error_e err, + unsigned char* buffer, + int buffer_size, + void* data) { LoggerD("Entered"); if (!data) { @@ -1089,7 +1151,7 @@ static void tagTransceiveCb(nfc_error_e err, unsigned char *buffer, PlatformResult result = NFCUtil::CodeToResult(err, error_message.c_str()); picojson::value event = CreateEventError(callback_id, result); - NFCInstance::getInstance().PostMessage(event.serialize().c_str()); + NFCAdapter::GetInstance()->RespondAsync(event.serialize().c_str()); return; } @@ -1097,14 +1159,15 @@ static void tagTransceiveCb(nfc_error_e err, unsigned char *buffer, picojson::value response = createEventSuccess(callback_id); picojson::object& response_obj = response.get(); tools::ReportSuccess(response_obj); - picojson::array& callback_data_array = response_obj.insert(std::make_pair("data", + picojson::array& callback_data_array = response_obj.insert(std::make_pair(JSON_DATA, picojson::value(picojson::array()))).first->second.get(); for (unsigned int i = 0; i < buffer_size; i++) { - callback_data_array.push_back(picojson::value(static_cast(buffer[i]))); + callback_data_array.push_back( + picojson::value(static_cast(buffer[i]))); } - NFCInstance::getInstance().PostMessage(response.serialize().c_str()); + NFCAdapter::GetInstance()->RespondAsync(response.serialize().c_str()); } PlatformResult NFCAdapter::TagTransceive(int tag_id, const picojson::value& args) { @@ -1116,36 +1179,29 @@ PlatformResult NFCAdapter::TagTransceive(int tag_id, const picojson::value& args return result; } - double callback_id = args.get(CALLBACK_ID).get(); + double callback_id = args.get(JSON_CALLBACK_ID).get(); LoggerD("Received callback id: %f", callback_id); if(!is_connected) { picojson::value event = CreateEventError(callback_id, - PlatformResult(ErrorCode::UNKNOWN_ERR, - "Tag is no more connected.")); - NFCInstance::getInstance().PostMessage(event.serialize().c_str()); + PlatformResult(ErrorCode::UNKNOWN_ERR, "Tag is no more connected.")); + NFCAdapter::GetInstance()->RespondAsync(event.serialize().c_str()); return PlatformResult(ErrorCode::NO_ERROR);; } const picojson::array& data_array = FromJson( - args.get(), "data"); - - unsigned char *buffer = new unsigned char[data_array.size()]; - // unique pointer used to automatically release memory when leaving scope - std::unique_ptr buffer_ptr(buffer); - int i = 0; + args.get(), JSON_DATA); - for(auto it = data_array.begin(); it != data_array.end(); - ++it, ++i) { + std::unique_ptr buffer_ptr(new unsigned char[data_array.size()]); - unsigned char val = (unsigned char)it->get(); - buffer[i] = val; + for(std::size_t i = 0; i < data_array.size(); ++i) { + buffer_ptr.get()[i] = static_cast(data_array[i].get()); } double* callback_id_pointer = new double(callback_id); - int ret = nfc_tag_transceive(m_last_tag_handle, buffer, data_array.size(), - tagTransceiveCb, (void*) callback_id_pointer); + int ret = nfc_tag_transceive(m_last_tag_handle, buffer_ptr.get(), + data_array.size(), tagTransceiveCb, (void*) callback_id_pointer); if (NFC_ERROR_NONE != ret) { delete callback_id_pointer; @@ -1154,7 +1210,8 @@ PlatformResult NFCAdapter::TagTransceive(int tag_id, const picojson::value& args // for permission related error throw exception if(NFC_ERROR_SECURITY_RESTRICTED == ret || NFC_ERROR_PERMISSION_DENIED == ret) { - return PlatformResult(ErrorCode::SECURITY_ERR, "Failed to read NDEF - permission denied"); + return PlatformResult(ErrorCode::SECURITY_ERR, + "Failed to read NDEF - permission denied"); } std::string error_message = NFCUtil::getNFCErrorMessage(ret); @@ -1163,7 +1220,7 @@ PlatformResult NFCAdapter::TagTransceive(int tag_id, const picojson::value& args result = NFCUtil::CodeToResult(ret, error_message.c_str()); picojson::value event = CreateEventError(callback_id, result); - NFCInstance::getInstance().PostMessage(event.serialize().c_str()); + NFCAdapter::GetInstance()->RespondAsync(event.serialize().c_str()); } return PlatformResult(ErrorCode::NO_ERROR); } @@ -1181,7 +1238,7 @@ PlatformResult NFCAdapter::GetCachedMessage(picojson::object& out) { NFCMessageUtils::RemoveMessageHandle(message_handle); return NFCUtil::CodeToResult(result, "Failed to get cached message"); } - unsigned char *raw_data = NULL; + unsigned char* raw_data = NULL; unsigned int size; if (NFC_ERROR_NONE != nfc_ndef_message_get_rawdata(message_handle, &raw_data, &size)) { @@ -1190,7 +1247,8 @@ PlatformResult NFCAdapter::GetCachedMessage(picojson::object& out) { NFCMessageUtils::RemoveMessageHandle(message_handle); return PlatformResult(ErrorCode::NO_ERROR); } - PlatformResult ret = NFCMessageUtils::ReportNdefMessageFromData(raw_data, size, out); + PlatformResult ret = NFCMessageUtils::ReportNdefMessageFromData(raw_data, + size, out); free(raw_data); if (ret.IsError()) { NFCMessageUtils::RemoveMessageHandle(message_handle); @@ -1200,7 +1258,7 @@ PlatformResult NFCAdapter::GetCachedMessage(picojson::object& out) { return PlatformResult(ErrorCode::NO_ERROR); } -static void peerSentCallback(nfc_error_e result, void *user_data) { +static void peerSentCallback(nfc_error_e result, void* user_data) { double* callbackId = static_cast(user_data); if (NFC_ERROR_NONE != result){ @@ -1208,20 +1266,17 @@ static void peerSentCallback(nfc_error_e result, void *user_data) { PlatformResult ret = NFCUtil::CodeToResult(result, error_message.c_str()); picojson::value event = CreateEventError(*callbackId, ret); - - NFCInstance::getInstance().PostMessage(event.serialize().c_str()); + NFCAdapter::GetInstance()->RespondAsync(event.serialize().c_str()); } else { picojson::value event = createEventSuccess(*callbackId); - - NFCInstance::getInstance().PostMessage(event.serialize().c_str()); + NFCAdapter::GetInstance()->RespondAsync(event.serialize().c_str()); } delete callbackId; callbackId = NULL; - } -static gboolean sendNDEFErrorCB(void * user_data) { +static gboolean sendNDEFErrorCB(void* user_data) { peerSentCallback(NFC_ERROR_INVALID_NDEF_MESSAGE, user_data); return false; } @@ -1234,13 +1289,12 @@ PlatformResult NFCAdapter::sendNDEF(int peer_id, const picojson::value& args) { return result; } - double* callbackId = new double(args.get(CALLBACK_ID).get()); + double* callbackId = new double(args.get(JSON_CALLBACK_ID).get()); if (!is_connected) { picojson::value event = CreateEventError(*callbackId, - PlatformResult(ErrorCode::UNKNOWN_ERR, - "Peer is no more connected")); - NFCInstance::getInstance().PostMessage(event.serialize().c_str()); + PlatformResult(ErrorCode::UNKNOWN_ERR, "Peer is no more connected")); + NFCAdapter::GetInstance()->RespondAsync(event.serialize().c_str()); delete callbackId; callbackId = NULL; return PlatformResult(ErrorCode::NO_ERROR); @@ -1254,8 +1308,8 @@ PlatformResult NFCAdapter::sendNDEF(int peer_id, const picojson::value& args) { result = NFCMessageUtils::NDEFMessageToStruct( records_array, size, &message); if (message) { - int ret = nfc_p2p_send(m_peer_handle, message, - peerSentCallback, static_cast(callbackId)); + int ret = nfc_p2p_send(m_peer_handle, message, peerSentCallback, + static_cast(callbackId)); NFCMessageUtils::RemoveMessageHandle(message); if (NFC_ERROR_NONE != ret) { LOGE("sendNDEF failed %d",ret); @@ -1272,5 +1326,148 @@ PlatformResult NFCAdapter::sendNDEF(int peer_id, const picojson::value& args) { } return PlatformResult(ErrorCode::NO_ERROR); } + +void NFCAdapter::SetSEHandle(nfc_se_h handle) { + m_se_handle = handle; +} + +nfc_se_h NFCAdapter::GetSEHandle() { + return m_se_handle; +} + +PlatformResult NFCAdapter::AddHCEEventListener() { + LoggerD("Entered"); + if (!m_is_hce_listener_set) { + int ret = nfc_manager_set_hce_event_cb(HCEEventCallback, nullptr); + if (NFC_ERROR_NONE != ret) { + LoggerE("AddHCEEventListener failed: %d", ret); + return NFCUtil::CodeToResult(ret, + NFCUtil::getNFCErrorMessage(ret).c_str()); + } + m_is_hce_listener_set = true; + } + return PlatformResult(ErrorCode::NO_ERROR); +} + +PlatformResult NFCAdapter::RemoveHCEEventListener() { + LoggerD("Entered"); + if (m_is_hce_listener_set) { + nfc_manager_unset_hce_event_cb(); + m_is_hce_listener_set = false; + } + return PlatformResult(ErrorCode::NO_ERROR); +} + +void NFCAdapter::SendHostAPDUResponse( + const UCharVector& apdu, + const std::function& success_cb, + const std::function& error_cb) { + LoggerD("Entered"); + + if (!nfc_manager_is_supported()) { + LoggerE("NFC Not Supported"); + error_cb(PlatformResult(ErrorCode::NOT_SUPPORTED_ERR, "NFC Not Supported")); + return; + } + + int ret = nfc_hce_send_apdu_response(m_se_handle, + const_cast(apdu.data()), + static_cast(apdu.size())); + if (NFC_ERROR_NONE == ret) { + success_cb(); + } else { + error_cb( + NFCUtil::CodeToResult(ret, NFCUtil::getNFCErrorMessage(ret).c_str())); + } +} + +PlatformResult NFCAdapter::IsActivatedHandlerForAID( + const char* aid, + bool* is_activated_handler) { + AssertMsg(aid, "Poiner can not be null!"); + AssertMsg(is_activated_handler, "Poiner can not be null!"); + LoggerD("Entered"); + + int ret = nfc_se_is_activated_handler_for_aid(aid, is_activated_handler); + if (NFC_ERROR_NONE != ret) { + LoggerE("IsActivatedHandlerForAID failed: %d", ret); + return NFCUtil::CodeToResult(ret, + NFCUtil::getNFCErrorMessage(ret).c_str()); + } + return PlatformResult(ErrorCode::NO_ERROR); +} + +PlatformResult NFCAdapter::IsActivatedHandlerForCategory( + nfc_card_emulation_category_type_e category, bool* is_activated_handler) { + AssertMsg(is_activated_handler, "Poiner can not be null!"); + LoggerD("Category: %d", category); + + int ret = nfc_se_is_activated_handler_for_category(category, + is_activated_handler); + if (NFC_ERROR_NONE != ret) { + LoggerE("IsActivatedHandlerForCategory failed: %d", ret); + return NFCUtil::CodeToResult(ret, + NFCUtil::getNFCErrorMessage(ret).c_str()); + } + return PlatformResult(ErrorCode::NO_ERROR); +} + +PlatformResult NFCAdapter::RegisterAID( + const char* aid, + nfc_card_emulation_category_type_e category) { + AssertMsg(aid, "Poiner can not be null!"); + LoggerD("AID: %s, Category: %d", aid, category); + + int ret = nfc_se_register_aid(category, aid); + if (NFC_ERROR_NONE != ret) { + LoggerE("RegisterAID failed: %d", ret); + return NFCUtil::CodeToResult(ret, + NFCUtil::getNFCErrorMessage(ret).c_str()); + } + return PlatformResult(ErrorCode::NO_ERROR); +} + +PlatformResult NFCAdapter::UnregisterAID( + const char* aid, + nfc_card_emulation_category_type_e category) { + AssertMsg(aid, "Poiner can not be null!"); + LoggerD("AID: %s, Category: %d", aid, category); + + int ret = nfc_se_unregister_aid(category, aid); + if (NFC_ERROR_NONE != ret) { + LoggerE("UnregisterAID failed: %d", ret); + return NFCUtil::CodeToResult(ret, + NFCUtil::getNFCErrorMessage(ret).c_str()); + } + return PlatformResult(ErrorCode::NO_ERROR); +} + +static void SaveRow(nfc_se_type_e se_type, + const char* aid, + bool read_only, + void* user_data) { + LoggerD("Entered"); + AssertMsg(aid, "Poiner can not be null!"); + AssertMsg(user_data, "Poiner can not be null!"); + AIDDataVector* aids = static_cast(user_data); + aids->push_back(AIDData(se_type, aid, read_only)); +}; + +void NFCAdapter::GetAIDsForCategory( + nfc_card_emulation_category_type_e category, + const std::function& success_cb, + const std::function& error_cb) { + LoggerD("Category: %d", category); + + AIDDataVector aids{}; + int ret = nfc_se_foreach_registered_aids(category, SaveRow, &aids); + if (NFC_ERROR_NONE != ret) { + LoggerE("GetAIDsForCategory failed: %d", ret); + error_cb(NFCUtil::CodeToResult(ret, + NFCUtil::getNFCErrorMessage(ret).c_str())); + } + success_cb(aids); +} + }// nfc }// extension diff --git a/src/nfc/nfc_adapter.h b/src/nfc/nfc_adapter.h index f78ac2fa..9f988eef 100644 --- a/src/nfc/nfc_adapter.h +++ b/src/nfc/nfc_adapter.h @@ -5,36 +5,45 @@ #ifndef NFC_NFC_ADAPTER_H_ #define NFC_NFC_ADAPTER_H_ -#include "nfc/nfc_instance.h" -#include "nfc/nfc_util.h" - -#include "common/picojson.h" -#include "common/platform_result.h" -#include -#include -#include - #ifdef APP_CONTROL_SETTING_SUPPORT #include #endif +#include + +#include +#include + +#include "common/picojson.h" +#include "common/platform_result.h" +#include "nfc/aid_data.h" +#include "nfc/nfc_util.h" namespace extension { namespace nfc { -class NFCInstance; - typedef std::list> NFCTagPropertiesT; class NFCAdapter { public: + static NFCAdapter* GetInstance(); + + // Interface provides method PostMessage() that returns asynchronous response. + class IResponder { + public: + virtual void RespondAsync(const char* msg) = 0; + }; + + void SetResponder(IResponder* responder); + void RespondAsync(const char* msg); + bool GetPowered(); common::PlatformResult SetPowered(const picojson::value& args); // cardEmulationMode getter and setter - common::PlatformResult GetCardEmulationMode(std::string *mode); + common::PlatformResult GetCardEmulationMode(std::string* mode); common::PlatformResult SetCardEmulationMode(std::string mode); // activeSecureElement getter and setter - common::PlatformResult GetActiveSecureElement(std::string *type); + common::PlatformResult GetActiveSecureElement(std::string* type); common::PlatformResult SetActiveSecureElement(std::string element); // Adapter methods @@ -52,7 +61,7 @@ class NFCAdapter { nfc_p2p_target_h GetPeerHandle(); int GetPeerId(); void IncreasePeerId(); - common::PlatformResult PeerIsConnectedGetter(int peer_id, bool *state); + common::PlatformResult PeerIsConnectedGetter(int peer_id, bool* state); common::PlatformResult SetPeerListener(); common::PlatformResult UnsetPeerListener(); common::PlatformResult SetReceiveNDEFListener(int peer_id); @@ -62,12 +71,11 @@ class NFCAdapter { // NFCTag related methods // attributes - common::PlatformResult TagTypeGetter(int tag_id, std::string *type); - common::PlatformResult TagIsSupportedNDEFGetter(int tag_id, bool *is_supported); - common::PlatformResult TagNDEFSizeGetter(int tag_id, unsigned int *size); - common::PlatformResult TagPropertiesGetter(int tag_id, NFCTagPropertiesT *properties); - common::PlatformResult TagIsConnectedGetter(int tag_id, bool *state); - + common::PlatformResult TagTypeGetter(int tag_id, std::string* type); + common::PlatformResult TagIsSupportedNDEFGetter(int tag_id, bool* is_supported); + common::PlatformResult TagNDEFSizeGetter(int tag_id, unsigned int* size); + common::PlatformResult TagPropertiesGetter(int tag_id, NFCTagPropertiesT* properties); + common::PlatformResult TagIsConnectedGetter(int tag_id, bool* state); // methods common::PlatformResult TagReadNDEF(int tag_id, const picojson::value& args); common::PlatformResult TagWriteNDEF(int tag_id, const picojson::value& args); @@ -78,7 +86,31 @@ class NFCAdapter { int GetNextTagId(); void SetTagHandle(nfc_tag_h tag); - static NFCAdapter* GetInstance(); + // HCE + void SetSEHandle(nfc_se_h handle); + nfc_se_h GetSEHandle(); + common::PlatformResult AddHCEEventListener(); + common::PlatformResult RemoveHCEEventListener(); + void SendHostAPDUResponse( + const UCharVector& apdu, + const std::function& success_cb, + const std::function& error_cb); + common::PlatformResult IsActivatedHandlerForAID(const char* aid, + bool* is_activated_handler); + common::PlatformResult IsActivatedHandlerForCategory( + nfc_card_emulation_category_type_e category, bool* is_activated_handler); + common::PlatformResult RegisterAID( + const char* aid, + nfc_card_emulation_category_type_e category); + common::PlatformResult UnregisterAID( + const char* aid, + nfc_card_emulation_category_type_e category); + void GetAIDsForCategory( + nfc_card_emulation_category_type_e category, + const std::function& success_cb, + const std::function& error_cb); + + private: NFCAdapter(); virtual ~NFCAdapter(); @@ -89,10 +121,15 @@ class NFCAdapter { bool m_is_listener_set; bool m_is_transaction_ese_listener_set; bool m_is_transaction_uicc_listener_set; + bool m_is_transaction_hce_listener_set; bool m_is_peer_listener_set; int m_latest_peer_id; nfc_p2p_target_h m_peer_handle; bool m_is_ndef_listener_set; + nfc_se_h m_se_handle; + bool m_is_hce_listener_set; + + IResponder* responder_; }; } // nfc diff --git a/src/nfc/nfc_instance.cc b/src/nfc/nfc_instance.cc index 2283da0f..1c6bd28b 100644 --- a/src/nfc/nfc_instance.cc +++ b/src/nfc/nfc_instance.cc @@ -2,17 +2,19 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "nfc_instance.h" -#include "nfc_util.h" -#include "nfc_message_utils.h" +#include "nfc/nfc_instance.h" -#include "common/picojson.h" +#include + +#include "common/converter.h" #include "common/logger.h" +#include "common/picojson.h" #include "common/platform_exception.h" - -// platform header -#include - +#include "common/platform_result.h" +#include "common/task-queue.h" +#include "nfc/defs.h" +#include "nfc/nfc_message_utils.h" +#include "nfc/nfc_util.h" namespace extension { namespace nfc { @@ -35,14 +37,20 @@ NFCInstance& NFCInstance::getInstance() { return instance; } +void NFCInstance::RespondAsync(const char* msg) { + PostMessage(msg); +} + NFCInstance::NFCInstance() { using std::placeholders::_1; using std::placeholders::_2; - +#define REGISTER_ASYNC(c,x) \ + RegisterSyncHandler(c, std::bind(&NFCInstance::x, this, _1, _2)); #define REGISTER_SYNC(c,x) \ - RegisterSyncHandler(c, std::bind(&NFCInstance::x, this, _1, _2)); + RegisterSyncHandler(c, std::bind(&NFCInstance::x, this, _1, _2)); REGISTER_SYNC("NFCManager_getDefaultAdapter", GetDefaultAdapter); REGISTER_SYNC("NFCManager_setExclusiveMode", SetExclusiveMode); + REGISTER_ASYNC("NFCAdapter_setPowered", SetPowered); REGISTER_SYNC("NFCAdapter_getPowered", GetPowered); REGISTER_SYNC("NFCAdapter_cardEmulationModeSetter", CardEmulationModeSetter); REGISTER_SYNC("NFCAdapter_cardEmulationModeGetter", CardEmulationModeGetter); @@ -68,6 +76,19 @@ NFCInstance::NFCInstance() { REGISTER_SYNC("NFCAdapter_getCachedMessage", GetCachedMessage); REGISTER_SYNC("NFCAdapter_setExclusiveModeForTransaction", SetExclusiveModeForTransaction); + + // HCE related methods + REGISTER_SYNC("NFCAdapter_addHCEEventListener", AddHCEEventListener); + REGISTER_SYNC("NFCAdapter_removeHCEEventListener", RemoveHCEEventListener); + REGISTER_ASYNC("NFCAdapter_sendHostAPDUResponse", SendHostAPDUResponse); + REGISTER_SYNC("NFCAdapter_isActivatedHandlerForAID", + IsActivatedHandlerForAID); + REGISTER_SYNC("NFCAdapter_isActivatedHandlerForCategory", + IsActivatedHandlerForCategory); + REGISTER_SYNC("NFCAdapter_registerAID", RegisterAID); + REGISTER_SYNC("NFCAdapter_unregisterAID", UnregisterAID); + REGISTER_ASYNC("NFCAdapter_getAIDsForCategory", GetAIDsForCategory); + REGISTER_SYNC("NFCPeer_setReceiveNDEFListener", SetReceiveNDEFListener); REGISTER_SYNC("NFCPeer_unsetReceiveNDEFListener", UnsetReceiveNDEFListener); REGISTER_SYNC("NDEFMessage_toByte", ToByte); @@ -84,33 +105,27 @@ NFCInstance::NFCInstance() { REGISTER_SYNC("NFCTag_NDEFSizeGetter", TagNDEFSizeGetter); REGISTER_SYNC("NFCTag_propertiesGetter", TagPropertiesGetter); REGISTER_SYNC("NFCTag_isConnectedGetter", TagIsConnectedGetter); + + REGISTER_ASYNC("NFCTag_readNDEF", ReadNDEF); + REGISTER_ASYNC("NFCTag_writeNDEF", WriteNDEF); + REGISTER_ASYNC("NFCTag_transceive", Transceive ); + REGISTER_ASYNC("NFCPeer_sendNDEF", SendNDEF); #undef REGISTER_SYNC -#define REGISTER(c,x) \ - RegisterSyncHandler(c, std::bind(&NFCInstance::x, this, _1, _2)); - REGISTER("NFCAdapter_setPowered", SetPowered); - REGISTER("NFCTag_readNDEF", ReadNDEF); - REGISTER("NFCTag_writeNDEF", WriteNDEF); - REGISTER("NFCTag_transceive", Transceive ); - REGISTER("NFCPeer_sendNDEF", SendNDEF); -#undef REGISTER - // NFC library initialization - int result = nfc_manager_initialize(); - if (NFC_ERROR_NONE != result) { - LoggerE("Could not initialize NFC Manager."); - } -} - -#define CHECK_EXIST(args, name, out) \ - if (!args.contains(name)) {\ - ReportError(TypeMismatchException(name" is required argument"), out);\ - return;\ - } +#undef REGISTER_ASYNC -NFCInstance::~NFCInstance() { - int result = nfc_manager_deinitialize(); - if (NFC_ERROR_NONE != result) { - LoggerE("NFC Manager deinitialization failed."); + // Set a PostMessageHandler at NFCAdapter to provide mechanizm of returning + // asynchronous response + NFCAdapter::GetInstance()->SetResponder(this); +} + +#define CHECK_EXIST(args, name, out) \ + if (!args.contains(name)) { \ + LoggerE("args doesn't contain attribute '%s'", name); \ + ReportError(TypeMismatchException(name" is required argument"), out); \ + return; \ } + +NFCInstance::~NFCInstance() { } void NFCInstance::GetDefaultAdapter( @@ -140,9 +155,8 @@ void NFCInstance::SetExclusiveMode( CHECK_EXIST(args, "exclusiveMode", out); bool exmode = args.get("exclusiveMode").get(); - int ret = NFC_ERROR_NONE; - ret = nfc_manager_set_system_handler_enable(!exmode); + int ret = nfc_manager_set_system_handler_enable(!exmode); if (NFC_ERROR_NONE != ret) { PlatformResult result = NFCUtil::CodeToResult(ret, "Failed to set exclusive mode."); @@ -152,6 +166,7 @@ void NFCInstance::SetExclusiveMode( } } +//TODO(g.rynkowski): Rewrite to asynchronous approach void NFCInstance::SetPowered( const picojson::value& args, picojson::object& out) { CHECK_PRIVILEGE_ACCESS(kPrivilegeNfcAdmin, &out); @@ -388,6 +403,7 @@ void NFCInstance::SetExclusiveModeForTransaction( } } +//TODO(g.rynkowski): Rewrite to asynchronous approach void NFCInstance::ReadNDEF( const picojson::value& args, picojson::object& out) { CHECK_PRIVILEGE_ACCESS(kPrivilegeNfcTag, &out); @@ -405,6 +421,7 @@ void NFCInstance::ReadNDEF( } } +//TODO(g.rynkowski): Rewrite to asynchronous approach void NFCInstance::WriteNDEF( const picojson::value& args, picojson::object& out) { CHECK_PRIVILEGE_ACCESS(kPrivilegeNfcTag, &out); @@ -422,6 +439,7 @@ void NFCInstance::WriteNDEF( } } +//TODO(g.rynkowski): Rewrite to asynchronous approach void NFCInstance::Transceive( const picojson::value& args, picojson::object& out) { LoggerD("Entered"); @@ -470,6 +488,7 @@ void NFCInstance::UnsetReceiveNDEFListener( } } +//TODO(g.rynkowski): Rewrite to asynchronous approach void NFCInstance::SendNDEF( const picojson::value& args, picojson::object& out) { CHECK_PRIVILEGE_ACCESS(kPrivilegeNfcP2P, &out); @@ -720,7 +739,7 @@ void NFCInstance::TagPropertiesGetter( std::to_string(it->second[i]))); } - obj.insert(std::make_pair(it->first, value_vector)); + obj[it->first] = value_vector; properties_array.push_back(val); } ReportSuccess(properties, out); @@ -749,5 +768,160 @@ void NFCInstance::TagIsConnectedGetter( } } +void NFCInstance::AddHCEEventListener(const picojson::value& args, + picojson::object& out) { + PlatformResult result = NFCAdapter::GetInstance()->AddHCEEventListener(); + if (result.IsSuccess()) + ReportSuccess(out); + else + ReportError(result, &out); +} + +void NFCInstance::RemoveHCEEventListener(const picojson::value& args, + picojson::object& out) { + PlatformResult result = NFCAdapter::GetInstance()->RemoveHCEEventListener(); + if (result.IsSuccess()) + ReportSuccess(out); + else + ReportError(result, &out); +} + +void NFCInstance::SendHostAPDUResponse(const picojson::value& args, + picojson::object& out) { + LoggerD("Entered"); + CHECK_EXIST(args, JSON_APDU, out); + const picojson::array& apdu_array = FromJson( + args.get(), JSON_APDU); + const UCharVector& apdu = NFCUtil::DoubleArrayToUCharVector(apdu_array); + const double& callback_id = args.get(JSON_CALLBACK_ID).get(); + + auto success_cb = [this, callback_id]() -> void { + LoggerD("Entered"); + picojson::value response = picojson::value(picojson::object()); + picojson::object& response_obj = response.get(); + response_obj[JSON_CALLBACK_ID] = picojson::value(callback_id); + ReportSuccess(response_obj); + PostMessage(response.serialize().c_str()); + }; + + auto error_cb = [this, callback_id](const PlatformResult& error) -> void { + LoggerD("Entered"); + picojson::value response = picojson::value(picojson::object()); + picojson::object& response_obj = response.get(); + response_obj[JSON_CALLBACK_ID] = picojson::value(callback_id); + ReportError(error, &response_obj); + PostMessage(response.serialize().c_str()); + }; + + common::TaskQueue::GetInstance().Async( + std::bind(&NFCAdapter::SendHostAPDUResponse, NFCAdapter::GetInstance(), + apdu, success_cb, error_cb)); +} + +void NFCInstance::IsActivatedHandlerForAID(const picojson::value& args, + picojson::object& out) { + LoggerD("Entered"); + CHECK_EXIST(args, JSON_AID, out); + + const char* aid = args.get(JSON_AID).get().c_str(); + bool is_activated_handler = false; + + PlatformResult result = NFCAdapter::GetInstance()->IsActivatedHandlerForAID( + aid, &is_activated_handler); + if (result.IsSuccess()) + ReportSuccess(picojson::value(is_activated_handler), out); + else + ReportError(result, &out); +} + +void NFCInstance::IsActivatedHandlerForCategory(const picojson::value& args, + picojson::object& out) { + LoggerD("Entered"); + CHECK_EXIST(args, JSON_CATEGORY, out); + + nfc_card_emulation_category_type_e category = + NFCUtil::StringToCategory(args.get(JSON_CATEGORY).get()); + bool is_activated_handler = false; + + PlatformResult result = + NFCAdapter::GetInstance()->IsActivatedHandlerForCategory(category, + &is_activated_handler); + if (result.IsSuccess()) + ReportSuccess(picojson::value(is_activated_handler), out); + else + ReportError(result, &out); +} + +void NFCInstance::RegisterAID(const picojson::value& args, + picojson::object& out) { + LoggerD("Entered"); + CHECK_EXIST(args, JSON_AID, out); + CHECK_EXIST(args, JSON_CATEGORY, out); + + const char* aid = args.get(JSON_AID).get().c_str(); + nfc_card_emulation_category_type_e category = + NFCUtil::StringToCategory(args.get(JSON_CATEGORY).get()); + + PlatformResult result = NFCAdapter::GetInstance()->RegisterAID(aid, category); + if (result.IsSuccess()) + ReportSuccess(out); + else + ReportError(result, &out); +} + +void NFCInstance::UnregisterAID(const picojson::value& args, + picojson::object& out) { + LoggerD("Entered"); + CHECK_EXIST(args, JSON_AID, out); + CHECK_EXIST(args, JSON_CATEGORY, out); + + const char* aid = args.get(JSON_AID).get().c_str(); + nfc_card_emulation_category_type_e category = + NFCUtil::StringToCategory(args.get(JSON_CATEGORY).get()); + + PlatformResult result = + NFCAdapter::GetInstance()->UnregisterAID(aid, category); + if (result.IsSuccess()) + ReportSuccess(out); + else + ReportError(result, &out); +} + +void NFCInstance::GetAIDsForCategory(const picojson::value& args, + picojson::object& out) { + LoggerD("Entered"); + CHECK_EXIST(args, JSON_CATEGORY, out); + nfc_card_emulation_category_type_e required_category = + NFCUtil::StringToCategory(args.get(JSON_CATEGORY).get()); + const double& callback_id = args.get(JSON_CALLBACK_ID).get(); + + auto success_cb = [this, callback_id](const AIDDataVector& data) -> void { + LoggerD("enter"); + picojson::array aids; + aids.reserve(data.size()); + for (const auto& aid : data) { + aids.push_back(aid.toJSON()); + } + picojson::value response = picojson::value(picojson::object()); + picojson::object& response_obj = response.get(); + response_obj[JSON_CALLBACK_ID] = picojson::value(callback_id); + ReportSuccess(picojson::value(aids), response_obj); + PostMessage(response.serialize().c_str()); + }; + + auto error_cb = [this, callback_id](const PlatformResult& error) -> void { + LoggerD("enter"); + picojson::value response = picojson::value(picojson::object()); + picojson::object& response_obj = response.get(); + response_obj[JSON_CALLBACK_ID] = picojson::value(callback_id); + ReportError(error, &response_obj); + PostMessage(response.serialize().c_str()); + }; + + common::TaskQueue::GetInstance().Async( + std::bind(&NFCAdapter::GetAIDsForCategory, NFCAdapter::GetInstance(), + required_category, success_cb, error_cb)); +} + } // namespace nfc } // namespace extension diff --git a/src/nfc/nfc_instance.h b/src/nfc/nfc_instance.h index 365cc42d..da04ef07 100644 --- a/src/nfc/nfc_instance.h +++ b/src/nfc/nfc_instance.h @@ -6,15 +6,17 @@ #define NFC_NFC_INSTANCE_H_ #include "common/extension.h" - -#include "nfc_adapter.h" +#include "nfc/nfc_adapter.h" namespace extension { namespace nfc { -class NFCInstance: public common::ParsedInstance { +class NFCInstance: public common::ParsedInstance, NFCAdapter::IResponder +{ public: static NFCInstance& getInstance(); + void RespondAsync(const char* msg); + private: NFCInstance(); virtual ~NFCInstance(); @@ -48,7 +50,7 @@ class NFCInstance: public common::ParsedInstance { void SendNDEF(const picojson::value& args, picojson::object& out); void ToByte(const picojson::value& args, picojson::object& out); - //Message related methods + // Message related methods void NDEFMessageContructor(const picojson::value& args, picojson::object& out); void NDEFMessageToByte(const picojson::value& args, picojson::object& out); void NDEFRecordContructor(const picojson::value& args, picojson::object& out); @@ -63,6 +65,15 @@ class NFCInstance: public common::ParsedInstance { void TagPropertiesGetter(const picojson::value& args, picojson::object& out); void TagIsConnectedGetter(const picojson::value& args, picojson::object& out); + // HCE related methods + void AddHCEEventListener(const picojson::value& args, picojson::object& out); + void RemoveHCEEventListener(const picojson::value& args, picojson::object& out); + void SendHostAPDUResponse(const picojson::value& args, picojson::object& out); + void IsActivatedHandlerForAID(const picojson::value& args, picojson::object& out); + void IsActivatedHandlerForCategory(const picojson::value& args, picojson::object& out); + 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); }; } // namespace nfc diff --git a/src/nfc/nfc_message_utils.cc b/src/nfc/nfc_message_utils.cc index 5ba0aa09..1ddd8b8c 100644 --- a/src/nfc/nfc_message_utils.cc +++ b/src/nfc/nfc_message_utils.cc @@ -2,16 +2,14 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "nfc_message_utils.h" +#include "nfc/nfc_message_utils.h" #include -#include - -#include "nfc_util.h" +#include "common/converter.h" #include "common/logger.h" #include "common/platform_exception.h" -#include "common/converter.h" +#include "nfc/nfc_util.h" using namespace common; @@ -93,7 +91,7 @@ static PlatformResult getTypeNameFromHandle(nfc_ndef_record_h handle, nfc_ndef_message_h message_handle, UCharVector *type) { - unsigned char *type_name; + unsigned char* type_name; int type_size, result; result = nfc_ndef_record_get_type(handle, &type_name, &type_size); @@ -118,7 +116,7 @@ static PlatformResult getIdFromHandle(nfc_ndef_record_h handle, nfc_ndef_message_h message_handle, UCharVector *id) { - unsigned char *tmp_id; + unsigned char* tmp_id; int id_size, result; result = nfc_ndef_record_get_id(handle, &tmp_id, &id_size); @@ -144,7 +142,7 @@ static PlatformResult getPayloadFromHandle(nfc_ndef_record_h handle, nfc_ndef_message_h message_handle, UCharVector *payload) { - unsigned char *tmp_payload; + unsigned char* tmp_payload; unsigned int payload_size; int result; @@ -415,7 +413,7 @@ PlatformResult NFCMessageUtils::NDEFMessageToByte(const picojson::value& args, p return PlatformResult(ErrorCode::NO_ERROR); } - unsigned char *raw_data = NULL; + unsigned char* raw_data = NULL; unsigned int raw_data_size = 0; int result = nfc_ndef_message_get_rawdata(message, &raw_data, &raw_data_size); diff --git a/src/nfc/nfc_message_utils.h b/src/nfc/nfc_message_utils.h index 1c8b8231..8b89c011 100644 --- a/src/nfc/nfc_message_utils.h +++ b/src/nfc/nfc_message_utils.h @@ -6,9 +6,10 @@ #ifndef __TIZEN_NFC_NFC_MESSAGE_UTILS_H_ #define __TIZEN_NFC_NFC_MESSAGE_UTILS_H_ -#include +#include + #include -#include +#include #include "common/picojson.h" #include "common/platform_result.h" @@ -46,7 +47,7 @@ class NFCMessageUtils static void RemoveMessageHandle(nfc_ndef_message_h message_handle); }; -} // nfc -} // extension +} // nfc +} // extension -#endif // __TIZEN_NFC_NFC_MESSAGE_UTILS_H_ +#endif // __TIZEN_NFC_NFC_MESSAGE_UTILS_H_ diff --git a/src/nfc/nfc_util.cc b/src/nfc/nfc_util.cc index 94ddd4bf..91a702dd 100644 --- a/src/nfc/nfc_util.cc +++ b/src/nfc/nfc_util.cc @@ -2,26 +2,26 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "nfc_util.h" - -#include +#include "nfc/nfc_util.h" +#include "common/assert.h" #include "common/logger.h" #include "common/platform_exception.h" +#include "nfc/defs.h" using namespace common; namespace extension { namespace nfc { -UCharVector NFCUtil::toVector(const unsigned char *ch, const int size) +UCharVector NFCUtil::toVector(const unsigned char* ch, const int size) { UCharVector vec(ch, ch + size / sizeof(char)); return vec; } -PlatformResult NFCUtil::CodeToResult(const int errorCode, const char* message) -{ +PlatformResult NFCUtil::CodeToResult(const int errorCode, + const std::string& message) { switch(errorCode) { case NFC_ERROR_INVALID_PARAMETER: case NFC_ERROR_INVALID_NDEF_MESSAGE: @@ -71,9 +71,8 @@ std::string NFCUtil::getNFCErrorString(const int error_code) return UNKNOWN_ERROR_NAME_STR; } -std::string NFCUtil::getNFCErrorMessage(const int error_code) -{ - LOGD("Error code : %d",error_code); +const std::string NFCUtil::getNFCErrorMessage(const int error_code) { + LOGD("Error code : %d", error_code); switch(error_code) { case NFC_ERROR_ALREADY_ACTIVATED: case NFC_ERROR_ALREADY_DEACTIVATED: @@ -108,8 +107,9 @@ std::string NFCUtil::getNFCErrorMessage(const int error_code) return "Not NDEF Format"; case NFC_ERROR_SECURITY_RESTRICTED: return "Security Restricted"; + default: + return "UnknownError"; } - return "UnknownError"; } std::string NFCUtil::ToStringNFCTag(nfc_tag_type_e tag_type) @@ -207,7 +207,7 @@ nfc_tag_type_e NFCUtil::toNfcTagString(const std::string& type_string) } PlatformResult NFCUtil::ToStringCardEmulationMode( - const nfc_se_card_emulation_mode_type_e card_mode, std::string *mode) + const nfc_se_card_emulation_mode_type_e card_mode, std::string* mode) { switch (card_mode) { @@ -237,14 +237,17 @@ nfc_se_card_emulation_mode_type_e NFCUtil::toCardEmulationMode( } } -PlatformResult NFCUtil::ToStringSecureElementType(const nfc_se_type_e se_type, std::string *type) -{ +PlatformResult NFCUtil::ToStringSecureElementType(const nfc_se_type_e se_type, + std::string* type) { switch (se_type) { case NFC_SE_TYPE_ESE: - *type = ESE; + *type = DATA_NFC_SE_TYPE_ESE; break; case NFC_SE_TYPE_UICC: - *type = UICC; + *type = DATA_NFC_SE_TYPE_UICC; + break; + case NFC_SE_TYPE_HCE: + *type = DATA_NFC_SE_TYPE_HCE; break; default: LOGE("No Match Secure Element Type: %x", se_type); @@ -253,12 +256,14 @@ PlatformResult NFCUtil::ToStringSecureElementType(const nfc_se_type_e se_type, s return PlatformResult(ErrorCode::NO_ERROR); } -PlatformResult NFCUtil::ToSecureElementType(const std::string &type_string, nfc_se_type_e *type) -{ - if (type_string == ESE) { +PlatformResult NFCUtil::ToSecureElementType(const std::string& type_string, + nfc_se_type_e* type) { + if (type_string == DATA_NFC_SE_TYPE_ESE) { *type = NFC_SE_TYPE_ESE; - } else if (type_string == UICC) { + } else if (type_string == DATA_NFC_SE_TYPE_UICC) { *type = NFC_SE_TYPE_UICC; + } else if (type_string == DATA_NFC_SE_TYPE_HCE) { + *type = NFC_SE_TYPE_HCE; } else { LoggerE("No Match Secure Element Type: %s", type_string.c_str()); return PlatformResult(ErrorCode::TYPE_MISMATCH_ERR, "No Match Secure Element Type"); @@ -286,5 +291,64 @@ void NFCUtil::setDefaultFilterValues(std::vector& filter) filter.push_back(NFC_UNKNOWN_TARGET); } -} // NFC -} // DeviceApi +// Convertion of enum to HCEEventType(characters sequence). +const char* NFCUtil::ToStr(nfc_hce_event_type_e event_type) { + switch (event_type) { + case NFC_HCE_EVENT_DEACTIVATED: + return "DEACTIVATED"; + case NFC_HCE_EVENT_ACTIVATED: + return "ACTIVATED"; + case NFC_HCE_EVENT_APDU_RECEIVED: + return "APDU_RECEIVED"; + default: + AssertMsg(false, "That event type is incorrect."); + } +} + +// Convertion of enum to SecureElementType(characters sequence). +// Warning! DISABLE and SDCARD are not mentioned at widl spec. +const char* NFCUtil::ToStr(nfc_se_type_e se_type) { + switch (se_type) { + case NFC_SE_TYPE_DISABLE: + return "DISABLE"; + case NFC_SE_TYPE_ESE: + return "ESE"; + case NFC_SE_TYPE_UICC: + return "UICC"; + case NFC_SE_TYPE_SDCARD: + return "SDCARD"; + case NFC_SE_TYPE_HCE: + return "HCE"; + default: + AssertMsg(false, "That event type is incorrect."); + } +} + +// Convertion CardEmulationCategoryType(characters sequence) to enum. +nfc_card_emulation_category_type_e NFCUtil::StringToCategory(const std::string& category_type) { + if (category_type == "PAYMENT") + return NFC_CARD_EMULATION_CATEGORY_PAYMENT; + if (category_type == "OTHER") + return NFC_CARD_EMULATION_CATEGORY_OTHER; + AssertMsg(false, "That category type is incorrect."); +} + +UCharVector NFCUtil::DoubleArrayToUCharVector(const picojson::array& apdu_array) { + unsigned char apdu[apdu_array.size()]; + for(std::size_t i = 0; i < apdu_array.size(); ++i) { + apdu[i] = static_cast(apdu_array.at(i).get()); + } + return toVector(apdu, apdu_array.size()); +} + +picojson::array NFCUtil::FromUCharArray(unsigned char* array, + unsigned int apdu_len) { + picojson::array apdu_array; + apdu_array.reserve(apdu_len); + for(int i = 0; i < apdu_len; ++i) + apdu_array.push_back(picojson::value(static_cast(array[i]))); + return apdu_array; +} + +} // nfc +} // extension diff --git a/src/nfc/nfc_util.h b/src/nfc/nfc_util.h index d228bc6f..f702e418 100644 --- a/src/nfc/nfc_util.h +++ b/src/nfc/nfc_util.h @@ -6,9 +6,10 @@ #ifndef __TIZEN_NFC_NFCUTIL_H_ #define __TIZEN_NFC_NFCUTIL_H_ -#include +#include + #include -#include +#include #include "common/platform_result.h" @@ -36,9 +37,6 @@ const std::string UNKNOWN_TARGET = "UNKNOWN_TARGET"; const std::string ALWAYS_ON = "ALWAYS_ON"; const std::string OFF = "OFF"; -const std::string ESE = "ESE"; -const std::string UICC = "UICC"; - const std::string UNKNOWN_ERROR_NAME_STR = "UnknownError"; const std::string INVALID_VALUES_ERROR_NAME_STR = "InvalidValuesError"; const std::string TIMEOUT_ERROR_NAME_STR = "TimeoutError"; @@ -52,9 +50,10 @@ class NFCUtil { public: static UCharVector toVector(const unsigned char *ch, const int size); - static common::PlatformResult CodeToResult(const int errorCode, const char * message); + static common::PlatformResult CodeToResult(const int errorCode, + const std::string& message); static std::string getNFCErrorString(const int error_code); - static std::string getNFCErrorMessage(const int error_code); + static const std::string getNFCErrorMessage(const int error_code); static std::string ToStringNFCTag(const nfc_tag_type_e tag_type); static nfc_tag_type_e toNfcTagString(const std::string& type_string); static common::PlatformResult ToStringCardEmulationMode( @@ -64,6 +63,11 @@ class NFCUtil static common::PlatformResult ToStringSecureElementType(const nfc_se_type_e se_type, std::string *type); static common::PlatformResult ToSecureElementType(const std::string& type_string, nfc_se_type_e *type); static void setDefaultFilterValues(std::vector& filter); + static const char* ToStr(nfc_hce_event_type_e event_type); + static const char* ToStr(nfc_se_type_e se_type); + static nfc_card_emulation_category_type_e StringToCategory(const std::string& category_type); + static UCharVector DoubleArrayToUCharVector(const picojson::array& apdu_array); + static picojson::array FromUCharArray(unsigned char* array, unsigned int apdu_len); }; } // nfc