From 5418c9a2df6e02ebe5f761a80c991c790e63b2fa Mon Sep 17 00:00:00 2001 From: Rafal Galka Date: Wed, 29 Apr 2015 12:14:02 +0200 Subject: [PATCH] [NFC] Modify HCE APIs [Task] http://168.219.209.56/jira/browse/XWALK-211 [Remark] Patch should be merged when http://168.219.209.56/gerrit/#/c/29752/ will be available on nearest stable binary. Change-Id: I166c961d3844fe85d43d9dd2257a2c0f1fb93fbb --- src/nfc/nfc_adapter.cc | 67 ++++++++++++++++++++++-------- src/nfc/nfc_adapter.h | 14 +++++-- src/nfc/nfc_api.js | 91 ++++++++++++++++++++++++++++++----------- src/nfc/nfc_instance.cc | 30 ++++++++++---- 4 files changed, 148 insertions(+), 54 deletions(-) diff --git a/src/nfc/nfc_adapter.cc b/src/nfc/nfc_adapter.cc index 6f7fbefc..0c725331 100644 --- a/src/nfc/nfc_adapter.cc +++ b/src/nfc/nfc_adapter.cc @@ -1378,13 +1378,21 @@ void NFCAdapter::SendHostAPDUResponse( } PlatformResult NFCAdapter::IsActivatedHandlerForAID( - const char* aid, + const std::string& type, + const std::string& 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); + nfc_se_type_e se_type; + PlatformResult result = NFCUtil::ToSecureElementType(type, &se_type); + if (!result.IsError()) { + return result; + } + + int ret = nfc_se_is_activated_handler_for_aid(se_type, + aid.c_str(), + is_activated_handler); if (NFC_ERROR_NONE != ret) { LoggerE("IsActivatedHandlerForAID failed: %d", ret); return NFCUtil::CodeToResult(ret, @@ -1394,12 +1402,20 @@ PlatformResult NFCAdapter::IsActivatedHandlerForAID( } PlatformResult NFCAdapter::IsActivatedHandlerForCategory( - nfc_card_emulation_category_type_e category, bool* is_activated_handler) { + const std::string& type, + 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); + nfc_se_type_e se_type; + PlatformResult result = NFCUtil::ToSecureElementType(type, &se_type); + if (!result.IsError()) { + return result; + } + + int ret = nfc_se_is_activated_handler_for_category(se_type, + category, + is_activated_handler); if (NFC_ERROR_NONE != ret) { LoggerE("IsActivatedHandlerForCategory failed: %d", ret); return NFCUtil::CodeToResult(ret, @@ -1409,12 +1425,17 @@ PlatformResult NFCAdapter::IsActivatedHandlerForCategory( } PlatformResult NFCAdapter::RegisterAID( - const char* aid, + const std::string& type, + const std::string& 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); + nfc_se_type_e se_type; + PlatformResult result = NFCUtil::ToSecureElementType(type, &se_type); + if (!result.IsError()) { + return result; + } + + int ret = nfc_se_register_aid(se_type, category, aid.c_str()); if (NFC_ERROR_NONE != ret) { LoggerE("RegisterAID failed: %d", ret); return NFCUtil::CodeToResult(ret, @@ -1424,12 +1445,17 @@ PlatformResult NFCAdapter::RegisterAID( } PlatformResult NFCAdapter::UnregisterAID( - const char* aid, + const std::string& type, + const std::string& 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); + nfc_se_type_e se_type; + PlatformResult result = NFCUtil::ToSecureElementType(type, &se_type); + if (!result.IsError()) { + return result; + } + + int ret = nfc_se_unregister_aid(se_type, category, aid.c_str()); if (NFC_ERROR_NONE != ret) { LoggerE("UnregisterAID failed: %d", ret); return NFCUtil::CodeToResult(ret, @@ -1450,13 +1476,20 @@ static void SaveRow(nfc_se_type_e se_type, }; void NFCAdapter::GetAIDsForCategory( + const std::string& type, nfc_card_emulation_category_type_e category, const std::function& success_cb, const std::function& error_cb) { - LoggerD("Category: %d", category); + + nfc_se_type_e se_type; + PlatformResult result = NFCUtil::ToSecureElementType(type, &se_type); + if (!result.IsError()) { + error_cb(result); + return; + } AIDDataVector aids{}; - int ret = nfc_se_foreach_registered_aids(category, SaveRow, &aids); + int ret = nfc_se_foreach_registered_aids(se_type, category, SaveRow, &aids); if (NFC_ERROR_NONE != ret) { LoggerE("GetAIDsForCategory failed: %d", ret); error_cb(NFCUtil::CodeToResult(ret, diff --git a/src/nfc/nfc_adapter.h b/src/nfc/nfc_adapter.h index f09117d5..d167a5d3 100644 --- a/src/nfc/nfc_adapter.h +++ b/src/nfc/nfc_adapter.h @@ -95,17 +95,23 @@ class NFCAdapter { const UCharVector& apdu, const std::function& success_cb, const std::function& error_cb); - common::PlatformResult IsActivatedHandlerForAID(const char* aid, + common::PlatformResult IsActivatedHandlerForAID(const std::string& type, + const std::string& aid, bool* is_activated_handler); common::PlatformResult IsActivatedHandlerForCategory( - nfc_card_emulation_category_type_e category, bool* is_activated_handler); + const std::string& type, + nfc_card_emulation_category_type_e category, + bool* is_activated_handler); common::PlatformResult RegisterAID( - const char* aid, + const std::string& type, + const std::string& aid, nfc_card_emulation_category_type_e category); common::PlatformResult UnregisterAID( - const char* aid, + const std::string& type, + const std::string& aid, nfc_card_emulation_category_type_e category); void GetAIDsForCategory( + const std::string& type, nfc_card_emulation_category_type_e category, const std::function& success_cb, const std::function& error_cb); diff --git a/src/nfc/nfc_api.js b/src/nfc/nfc_api.js index bb8a9378..ce4af596 100644 --- a/src/nfc/nfc_api.js +++ b/src/nfc/nfc_api.js @@ -670,16 +670,22 @@ NFCAdapter.prototype.sendHostAPDUResponse = function(apdu, successCallback, erro native_.call('NFCAdapter_sendHostAPDUResponse', data, callback); }; -NFCAdapter.prototype.isActivatedHandlerForAID = function(aid) { +NFCAdapter.prototype.isActivatedHandlerForAID = function(type, aid) { var args = validator_.validateArgs(arguments, [ + { + name: 'type', + type: types_.ENUM, + values: type_.getValues(SecureElementType) + }, {name: 'aid', type: types_.STRING} ]); - if (!arguments.length) { + if (arguments.length < 2) { throw new WebAPIException(WebAPIException.TYPE_MISMATCH_ERR); } var data = { + type: args.type, aid: args.aid }; @@ -691,16 +697,23 @@ NFCAdapter.prototype.isActivatedHandlerForAID = function(aid) { return native_.getResultObject(result); }; -NFCAdapter.prototype.isActivatedHandlerForCategory = function(category) { - var args = validator_.validateArgs(arguments, [ - {name: 'category', type: types_.ENUM, values: Object.keys(CardEmulationCategoryType)} - ]); +NFCAdapter.prototype.isActivatedHandlerForCategory = function(type, category) { + var args = validator_.validateArgs(arguments, [{ + name: 'type', + type: types_.ENUM, + values: type_.getValues(SecureElementType) + }, { + name: 'category', + type: types_.ENUM, + values: Object.keys(CardEmulationCategoryType) + }]); - if (!arguments.length) { + if (arguments.length < 2) { throw new WebAPIException(WebAPIException.TYPE_MISMATCH_ERR); } var data = { + type: args.type, category: args.category }; @@ -712,17 +725,26 @@ NFCAdapter.prototype.isActivatedHandlerForCategory = function(category) { return native_.getResultObject(result); }; -NFCAdapter.prototype.registerAID = function(aid, category) { - var args = validator_.validateArgs(arguments, [ - {name: 'aid', type: types_.STRING}, - {name: 'category', type: types_.ENUM, values: Object.keys(CardEmulationCategoryType)} - ]); - - if (arguments.length < 2 || !type_.isString(arguments[0])) { +NFCAdapter.prototype.registerAID = function(type, aid, category) { + var args = validator_.validateArgs(arguments, [{ + name: 'type', + type: types_.ENUM, + values: type_.getValues(SecureElementType) + }, { + name: 'aid', + type: types_.STRING + }, { + name: 'category', + type: types_.ENUM, + values: Object.keys(CardEmulationCategoryType) + }]); + + if (arguments.length < 3 || !type_.isString(arguments[0])) { throw new WebAPIException(WebAPIException.TYPE_MISMATCH_ERR); } var data = { + type: args.type, aid: args.aid, category: args.category }; @@ -734,17 +756,23 @@ NFCAdapter.prototype.registerAID = function(aid, category) { } }; -NFCAdapter.prototype.unregisterAID = function(aid, category) { +NFCAdapter.prototype.unregisterAID = function(type, aid, category) { var args = validator_.validateArgs(arguments, [ + { + name: 'type', + type: types_.ENUM, + values: type_.getValues(SecureElementType) + }, {name: 'aid', type: types_.STRING}, {name: 'category', type: types_.ENUM, values: Object.keys(CardEmulationCategoryType)} ]); - if (arguments.length < 2 || !type_.isString(arguments[0])) { + if (arguments.length < 3 || !type_.isString(arguments[0])) { throw new WebAPIException(WebAPIException.TYPE_MISMATCH_ERR); } var data = { + type: args.type, aid: args.aid, category: args.category }; @@ -776,18 +804,31 @@ function AIDData(data) { }); } -NFCAdapter.prototype.getAIDsForCategory = function(category, successCallback, errorCallback) { - var args = validator_.validateArgs(arguments, [ - {name: 'category', type: types_.ENUM, values: Object.keys(CardEmulationCategoryType)}, - {name: 'successCallback', type: types_.FUNCTION}, - {name: 'errorCallback', type: types_.FUNCTION, optional: true, nullable: true} - ]); - - if (arguments.length < 2) { +NFCAdapter.prototype.getAIDsForCategory = function(type, category, successCallback, errorCallback) { + var args = validator_.validateArgs(arguments, [{ + name: 'type', + type: types_.ENUM, + values: type_.getValues(SecureElementType) + }, { + name: 'category', + type: types_.ENUM, + values: Object.keys(CardEmulationCategoryType) + }, { + name: 'successCallback', + type: types_.FUNCTION + }, { + name: 'errorCallback', + type: types_.FUNCTION, + optional: true, + nullable: true + }]); + + if (arguments.length < 3) { throw new WebAPIException(WebAPIException.TYPE_MISMATCH_ERR); } var data = { + type: args.type, category: args.category }; @@ -854,7 +895,7 @@ function NFCTag(tagid) { console.log('Current result: ' + result); - var result_array = new Object(); + var result_array = {}; for (var i in result.result) { var current = result.result[i]; var keys = Object.keys(current); diff --git a/src/nfc/nfc_instance.cc b/src/nfc/nfc_instance.cc index 3231a9f4..13af6dfa 100644 --- a/src/nfc/nfc_instance.cc +++ b/src/nfc/nfc_instance.cc @@ -756,13 +756,16 @@ void NFCInstance::SendHostAPDUResponse(const picojson::value& args, void NFCInstance::IsActivatedHandlerForAID(const picojson::value& args, picojson::object& out) { LoggerD("Entered"); + CHECK_EXIST(args, JSON_TYPE, out); CHECK_EXIST(args, JSON_AID, out); - const char* aid = args.get(JSON_AID).get().c_str(); + const std::string& aid = args.get(JSON_AID).get(); bool is_activated_handler = false; PlatformResult result = NFCAdapter::GetInstance()->IsActivatedHandlerForAID( - aid, &is_activated_handler); + args.get(JSON_TYPE).get(), + aid, + &is_activated_handler); if (result.IsSuccess()) ReportSuccess(picojson::value(is_activated_handler), out); else @@ -772,6 +775,7 @@ void NFCInstance::IsActivatedHandlerForAID(const picojson::value& args, void NFCInstance::IsActivatedHandlerForCategory(const picojson::value& args, picojson::object& out) { LoggerD("Entered"); + CHECK_EXIST(args, JSON_TYPE, out); CHECK_EXIST(args, JSON_CATEGORY, out); nfc_card_emulation_category_type_e category = @@ -779,7 +783,9 @@ void NFCInstance::IsActivatedHandlerForCategory(const picojson::value& args, bool is_activated_handler = false; PlatformResult result = - NFCAdapter::GetInstance()->IsActivatedHandlerForCategory(category, + NFCAdapter::GetInstance()->IsActivatedHandlerForCategory( + args.get(JSON_TYPE).get(), + category, &is_activated_handler); if (result.IsSuccess()) ReportSuccess(picojson::value(is_activated_handler), out); @@ -790,14 +796,17 @@ void NFCInstance::IsActivatedHandlerForCategory(const picojson::value& args, void NFCInstance::RegisterAID(const picojson::value& args, picojson::object& out) { LoggerD("Entered"); + CHECK_EXIST(args, JSON_TYPE, out); 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); + PlatformResult result = NFCAdapter::GetInstance()->RegisterAID( + args.get(JSON_TYPE).get(), + args.get(JSON_AID).get(), + category); if (result.IsSuccess()) ReportSuccess(out); else @@ -807,15 +816,18 @@ void NFCInstance::RegisterAID(const picojson::value& args, void NFCInstance::UnregisterAID(const picojson::value& args, picojson::object& out) { LoggerD("Entered"); + CHECK_EXIST(args, JSON_TYPE, out); 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); + NFCAdapter::GetInstance()->UnregisterAID( + args.get(JSON_TYPE).get(), + args.get(JSON_AID).get(), + category); if (result.IsSuccess()) ReportSuccess(out); else @@ -825,7 +837,9 @@ void NFCInstance::UnregisterAID(const picojson::value& args, void NFCInstance::GetAIDsForCategory(const picojson::value& args, picojson::object& out) { LoggerD("Entered"); + CHECK_EXIST(args, JSON_TYPE, out); CHECK_EXIST(args, JSON_CATEGORY, out); + const std::string& type = args.get(JSON_TYPE).get(); 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(); @@ -855,7 +869,7 @@ void NFCInstance::GetAIDsForCategory(const picojson::value& args, common::TaskQueue::GetInstance().Async( std::bind(&NFCAdapter::GetAIDsForCategory, NFCAdapter::GetInstance(), - required_category, success_cb, error_cb)); + type, required_category, success_cb, error_cb)); } } // namespace nfc -- 2.34.1