diff --git a/nfc_core/bundle.json b/nfc_core/bundle.json index 7667ce54bb551e6c6b9898c37332272b2c89d427..ea69cf79123f9d4f23f12513854f6b4890deacef 100644 --- a/nfc_core/bundle.json +++ b/nfc_core/bundle.json @@ -60,6 +60,7 @@ "//foundation/communication/nfc/nfc_core/sa_profile:nfc_profile", "//foundation/communication/nfc/nfc_core/interfaces/innerkits:nfc_inner_kits", "//foundation/communication/nfc/nfc_core/interfaces/js/napi:controller", + "//foundation/communication/nfc/nfc_core/interfaces/js/napi/tag:tag", "//foundation/communication/nfc/nfc_core/services:nfc_service" ], "inner_kits": [ diff --git a/nfc_core/interfaces/js/napi/tag/BUILD.gn b/nfc_core/interfaces/js/napi/tag/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..ad6a18f11e25249a826d078430fdc12bae94406e --- /dev/null +++ b/nfc_core/interfaces/js/napi/tag/BUILD.gn @@ -0,0 +1,56 @@ +# Copyright (c) 2022 Shenzhen Kaihong Digital Industry Development Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/ohos.gni") +import("//build/ohos_var.gni") + +ohos_shared_library("tag") { + defines = [ "DEBUG" ] + install_enable = true + include_dirs = [ + "//third_party/node/src", + "//native_engine", + "//utils/native/base/include", + "//utils/system/safwk/native/include", + "//foundation/communication/nfc/nfc_core/interfaces/innerkits/include", + "//foundation/communication/nfc/nfc_core/interfaces/innerkits/include/tags", + "//base/hiviewdfx/hilog/interfaces/native/innerkits/include", + "//base/notification/common_event_service/frameworks/core/include", + ] + + sources = [ + "nfc_napi_tag.cpp", + "nfc_napi_taga.cpp", + "nfc_napi_utils.cpp", + "nfc_napi_common.cpp", + "nfc_tag_context.cpp" + + ] + + deps = [ "//foundation/communication/nfc/nfc_core/interfaces/innerkits:nfc_inner_kits" ] + + external_deps = [ + "ability_base:want", + "common_event_service:cesfwk_core", + "common_event_service:cesfwk_innerkits", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "napi:ace_napi", + "samgr_standard:samgr_proxy", + "utils_base:utils", + ] + + relative_install_dir = "module/nfc" + part_name = "nfc" + subsystem_name = "communication" +} diff --git a/nfc_core/interfaces/js/napi/tag/nfc_napi_common.cpp b/nfc_core/interfaces/js/napi/tag/nfc_napi_common.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ee1f0b35925f12b2615c7918fa415124da0736a2 --- /dev/null +++ b/nfc_core/interfaces/js/napi/tag/nfc_napi_common.cpp @@ -0,0 +1,221 @@ +/* + * Copyright (C) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "nfc_napi_common.h" + +namespace OHOS { +namespace NFC { +namespace KITS { +void NfcNapiCommon::SetPropertyBool(napi_env env, napi_value object, const std::string &propertyName, bool property) +{ + napi_value propertyDest = nullptr; + NAPI_CALL_RETURN_VOID(env, napi_get_boolean(env, property, &propertyDest)); + NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, object, propertyName.c_str(), propertyDest)); +} + +void NfcNapiCommon::SetPropertyInt32( + napi_env env, napi_value object, const std::string &propertyName, int32_t property) +{ + napi_value propertyDest = nullptr; + NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, property, &propertyDest)); + NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, object, propertyName.c_str(), propertyDest)); +} + +void NfcNapiCommon::SetPropertyInt64( + napi_env env, napi_value object, const std::string &propertyName, int64_t property) +{ + napi_value propertyDest = nullptr; + NAPI_CALL_RETURN_VOID(env, napi_create_int64(env, property, &propertyDest)); + NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, object, propertyName.c_str(), propertyDest)); +} + +void NfcNapiCommon::SetPropertyUint32( + napi_env env, napi_value object, const std::string &propertyName, uint32_t property) +{ + napi_value propertyDest = nullptr; + NAPI_CALL_RETURN_VOID(env, napi_create_uint32(env, property, &propertyDest)); + NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, object, propertyName.c_str(), propertyDest)); +} + +void NfcNapiCommon::SetPropertyString( + napi_env env, napi_value object, const std::string &propertyName, const std::string &property) +{ + napi_value propertyDest = nullptr; + NAPI_CALL_RETURN_VOID(env, napi_create_string_utf8(env, property.c_str(), property.length(), &propertyDest)); + NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, object, propertyName.c_str(), propertyDest)); +} + +void NfcNapiCommon::GetPropertyInt32( + napi_env env, napi_value object, const std::string &propertyName, int32_t &property) +{ + napi_value value = nullptr; + NAPI_CALL_RETURN_VOID(env, napi_get_named_property(env, object, propertyName.c_str(), &value)); + NAPI_CALL_RETURN_VOID(env, napi_get_value_int32(env, value, &property)); +} + +void NfcNapiCommon::GetPropertyInt64( + napi_env env, napi_value object, const std::string &propertyName, int64_t &property) +{ + napi_value value = nullptr; + NAPI_CALL_RETURN_VOID(env, napi_get_named_property(env, object, propertyName.c_str(), &value)); + NAPI_CALL_RETURN_VOID(env, napi_get_value_int64(env, value, &property)); +} + +napi_value NfcNapiCommon::NapiValueByInt32(napi_env env, int32_t property) +{ + napi_value value = nullptr; + NAPI_CALL(env, napi_create_int32(env, property, &value)); + return value; +} + +std::string NfcNapiCommon::GetNapiStringValue( + napi_env env, napi_value napiValue, const std::string &name, const std::string &defValue) +{ + napi_value value = GetNamedProperty(env, napiValue, name); + if (value != nullptr) { + return GetStringFromValue(env, value); + } else { + return defValue; + } +} + +std::string NfcNapiCommon::GetStringFromValue(napi_env env, napi_value value) +{ + constexpr int32_t MAX_TEXT_LENGTH = 4096; + char msgChars[MAX_TEXT_LENGTH] = {0}; + size_t msgLength = 0; + NAPI_CALL_BASE(env, napi_get_value_string_utf8(env, value, msgChars, MAX_TEXT_LENGTH, &msgLength), ""); + if (msgLength > 0) { + return std::string(msgChars, 0, msgLength); + } else { + return ""; + } +} + +napi_value NfcNapiCommon::GetNamedProperty(napi_env env, napi_value object, const std::string &propertyName) +{ + napi_value value = nullptr; + bool hasProperty = false; + NAPI_CALL(env, napi_has_named_property(env, object, propertyName.data(), &hasProperty)); + if (hasProperty) { + NAPI_CALL(env, napi_get_named_property(env, object, propertyName.data(), &value)); + } + return value; +} + +int32_t NfcNapiCommon::GetNapiInt32Value( + napi_env env, napi_value napiValue, const std::string &name, const int32_t &defValue) +{ + napi_value value = GetNamedProperty(env, napiValue, name); + if (value != nullptr) { + int32_t intValue = 0; + napi_status getIntStatus = napi_get_value_int32(env, value, &intValue); + if (getIntStatus == napi_ok) { + return intValue; + } + } + return defValue; +} + +int64_t NfcNapiCommon::GetNapiInt64Value( + napi_env env, napi_value napiValue, const std::string &name, const int64_t &defValue) +{ + napi_value value = GetNamedProperty(env, napiValue, name); + if (value != nullptr) { + int64_t intValue = 0; + napi_status getIntStatus = napi_get_value_int64(env, value, &intValue); + if (getIntStatus == napi_ok) { + return intValue; + } + } + return defValue; +} + +bool NfcNapiCommon::MatchValueType(napi_env env, napi_value value, napi_valuetype targetType) +{ + napi_valuetype valueType = napi_undefined; + NAPI_CALL_BASE(env, napi_typeof(env, value, &valueType), false); + return valueType == targetType; +} + +bool NfcNapiCommon::MatchParameters( + napi_env env, const napi_value parameters[], std::initializer_list valueTypes) +{ + if (parameters == nullptr) { + return false; + } + int i = 0; + for (auto beg = valueTypes.begin(); beg != valueTypes.end(); ++beg) { + if (!MatchValueType(env, parameters[i], *beg)) { + return false; + } + ++i; + } + return true; +} + +napi_value NfcNapiCommon::CreateUndefined(napi_env env) +{ + napi_value result = nullptr; + NAPI_CALL(env, napi_get_undefined(env, &result)); + return result; +} + +bool NfcNapiCommon::HasNamedTypeProperty( + napi_env env, napi_value object, napi_valuetype type, std::string propertyName) +{ + bool hasProperty = false; + NAPI_CALL_BASE(env, napi_has_named_property(env, object, propertyName.data(), &hasProperty), false); + if (hasProperty) { + napi_value value = nullptr; + NAPI_CALL_BASE(env, napi_get_named_property(env, object, propertyName.data(), &value), false); + return NfcNapiCommon::MatchValueType(env, value, type); + } + return false; +} + +bool NfcNapiCommon::HasNamedProperty(napi_env env, napi_value object, std::string propertyName) +{ + bool hasProperty = false; + NAPI_CALL_BASE(env, napi_has_named_property(env, object, propertyName.data(), &hasProperty), false); + return hasProperty; +} + +bool NfcNapiCommon::MatchObjectProperty( + napi_env env, napi_value object, std::initializer_list> pairList) +{ + if (object == nullptr) { + return false; + } + for (auto beg = pairList.begin(); beg != pairList.end(); ++beg) { + if (!HasNamedTypeProperty(env, object, beg->second, beg->first)) { + return false; + } + } + return true; +} + +napi_value NfcNapiCommon::CreateEnumConstructor(napi_env env, napi_callback_info info) +{ + napi_value thisArg = nullptr; + void *data = nullptr; + napi_get_cb_info(env, info, nullptr, nullptr, &thisArg, &data); + napi_value global = nullptr; + napi_get_global(env, &global); + return thisArg; +} +} // namespace KITS +} // namespace NFC +} // namespace OHOS diff --git a/nfc_core/interfaces/js/napi/tag/nfc_napi_common.h b/nfc_core/interfaces/js/napi/tag/nfc_napi_common.h new file mode 100644 index 0000000000000000000000000000000000000000..3686bac6e063dbffeb16bcaa6d631f3548861eb4 --- /dev/null +++ b/nfc_core/interfaces/js/napi/tag/nfc_napi_common.h @@ -0,0 +1,84 @@ +/* + * Copyright (C) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef NFC_NAPI_COMMON_H +#define NFC_NAPI_COMMON_H + +#include +#include +#include "napi/native_api.h" +#include "napi/native_node_api.h" + +namespace OHOS { +namespace NFC { +namespace KITS { +enum class JS_CALLBACK_ARGV { + CALLBACK_ARGV_INDEX_0 = 0, + CALLBACK_ARGV_INDEX_1, + CALLBACK_ARGV_CNT, +}; + +enum class JS_ARGV_NUM { + ARGV_NUM_0 = 0, + ARGV_NUM_1, + ARGV_NUM_2, + ARGV_NUM_3, + ARGV_NUM_4, + ARGV_NUM_5, +}; + +enum class JS_ARGV_INDEX { + ARGV_INDEX_0 = 0, + ARGV_INDEX_1, + ARGV_INDEX_2, + ARGV_INDEX_3, + ARGV_INDEX_4, +}; + +class NfcNapiCommon { +public: + static napi_value NapiValueByInt32(napi_env env, int32_t property); + static void SetPropertyBool(napi_env env, napi_value object, const std::string &propertyName, bool property); + static void SetPropertyInt32(napi_env env, napi_value object, const std::string &propertyName, int32_t property); + static void SetPropertyInt64(napi_env env, napi_value object, const std::string &propertyName, int64_t property); + static void SetPropertyUint32(napi_env env, napi_value object, const std::string &propertyName, uint32_t property); + static void SetPropertyString( + napi_env env, napi_value object, const std::string &propertyName, const std::string &property); + static void GetPropertyInt32(napi_env env, napi_value object, const std::string &propertyName, int32_t &property); + static void GetPropertyInt64(napi_env env, napi_value object, const std::string &propertyName, int64_t &property); + static std::string GetNapiStringValue( + napi_env env, napi_value napiValue, const std::string &name, const std::string &defValue = ""); + static std::string GetStringFromValue(napi_env env, napi_value value); + static napi_value GetNamedProperty(napi_env env, napi_value object, const std::string &propertyName); + static int32_t GetNapiInt32Value( + napi_env env, napi_value napiValue, const std::string &name, const int32_t &defValue = 0); + static int64_t GetNapiInt64Value( + napi_env env, napi_value napiValue, const std::string &name, const int64_t &defValue = 0); + static bool MatchValueType(napi_env env, napi_value value, napi_valuetype targetType); + static bool MatchParameters( + napi_env env, const napi_value parameters[], std::initializer_list valueTypes); + + static napi_value CreateUndefined(napi_env env); + + static bool HasNamedProperty(napi_env env, napi_value object, std::string propertyName); + static bool MatchObjectProperty( + napi_env env, napi_value object, std::initializer_list> pairList); + static bool HasNamedTypeProperty(napi_env env, napi_value object, napi_valuetype type, std::string propertyName); + static napi_value CreateEnumConstructor(napi_env env, napi_callback_info info); +}; +} // namespace KITS +} // namespace NFC +} // namespace OHOS +#endif // NAPI_COMMON_H diff --git a/nfc_core/interfaces/js/napi/tag/nfc_napi_tag.cpp b/nfc_core/interfaces/js/napi/tag/nfc_napi_tag.cpp new file mode 100755 index 0000000000000000000000000000000000000000..cc5708f46cbcbd29f6b0cb741c88d57a7dafb973 --- /dev/null +++ b/nfc_core/interfaces/js/napi/tag/nfc_napi_tag.cpp @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2022 Shenzhen Kaihong Digital Industry Development Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "nfc_napi_tag.h" +#include "napi/native_api.h" +#include "napi/native_node_api.h" +#include "loghelper.h" + +namespace OHOS { +namespace NFC { +namespace KITS { +static napi_value InitJs(napi_env env, napi_value exports) +{ + InfoLog("Init, nfc_napi_tag"); + RegisternfcATagObject(env, exports); + napi_property_descriptor desc[] = { + DECLARE_NAPI_FUNCTION("getNfcATag", GetNfcATag), + }; + + NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(napi_property_descriptor), desc)); + return exports; +} + +static napi_module nfcTagModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = InitJs, + .nm_modname = "nfc.tag", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterNfcTagModule(void) +{ + napi_module_register(&nfcTagModule); +} +} // namespace KITS +} // namespace NFC +} // namespace OHOS diff --git a/nfc_core/interfaces/js/napi/tag/nfc_napi_tag.h b/nfc_core/interfaces/js/napi/tag/nfc_napi_tag.h new file mode 100644 index 0000000000000000000000000000000000000000..7765b2632d5a48f93300fbac7f29d62ac985bd2e --- /dev/null +++ b/nfc_core/interfaces/js/napi/tag/nfc_napi_tag.h @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef NFC_NAPI_TAG_H_ +#define NFC_NAPI_TAG_H_ + +#include "napi/native_api.h" +#include "napi/native_node_api.h" +#include "nfc_napi_tagsesstion.h" +#include "taginfo.h" + +namespace OHOS { +namespace NFC { +namespace KITS { +napi_value RegisternfcATagObject(napi_env env, napi_value exports); +napi_value GetNfcATag(napi_env env, napi_callback_info info); +} // namespace KITS +} // namespace NFC +} // namespace OHOS +#endif diff --git a/nfc_core/interfaces/js/napi/tag/nfc_napi_taga.cpp b/nfc_core/interfaces/js/napi/tag/nfc_napi_taga.cpp new file mode 100755 index 0000000000000000000000000000000000000000..d21f5bc0eaea2ea403cd1dfbb70cfec1a4c603ba --- /dev/null +++ b/nfc_core/interfaces/js/napi/tag/nfc_napi_taga.cpp @@ -0,0 +1,412 @@ +/* + * Copyright (c) 2022 Shenzhen Kaihong Digital Industry Development Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "nfc_napi_taga.h" + +#include +#include "loghelper.h" +#include "taginfo.h" +#include "nfc_sdk_common.h" +#include "nfc_napi_tagsesstion.h" +#include "nfc_napi_utils.h" +#include "nfc_tag_context.h" +#include "nfc_napi_common.h" +#include "itag_session.h" + +namespace OHOS { +namespace NFC { +namespace KITS { +napi_value nfcATagObject; + +napi_value ParseIntArray(napi_env env, napi_value obj, std::vector &typeArray) +{ + const int32_t ERROR_DEFAULT = -1; + bool result = false; + napi_status status = napi_is_array(env, obj, &result); + if (status != napi_ok || !result) { + InfoLog("Invalid input parameter type!"); + return nullptr; + } + + napi_value elementValue = nullptr; + int32_t element = ERROR_DEFAULT; + uint32_t arrayLength = 0; + NAPI_CALL(env, napi_get_array_length(env, obj, &arrayLength)); + typeArray.resize(arrayLength); + for (uint32_t i = 0; i < arrayLength; ++i) { + NAPI_CALL(env, napi_get_element(env, obj, i, &elementValue)); + napi_valuetype valueType = napi_undefined; + napi_typeof(env, elementValue, &valueType); + if (valueType == napi_number) { + NAPI_CALL(env, napi_get_value_int32(env, elementValue, &element)); + typeArray[i] = element; + InfoLog("tag tech array :%{public}d is %{public}d ", i, element); + } else { + InfoLog("Invalid parameter type of array element!"); + return nullptr; + } + } + + return NfcNapiCommon::CreateUndefined(env); +} + +void SetPacMapObject( + std::shared_ptr &pacMap, const napi_env &env, std::string keyStr, napi_value value) +{ + napi_valuetype valueType = napi_undefined; + napi_typeof(env, value, &valueType); + if (valueType == napi_string) { + std::string valueString = UnwrapStringFromJS(env, value); + InfoLog("SetPacMap keystr :%{public}s", valueString.c_str()); + pacMap->PutStringValue(keyStr, valueString); + } else if (valueType == napi_number) { + double valueNumber = 0; + napi_get_value_double(env, value, &valueNumber); + pacMap->PutDoubleValue(keyStr, valueNumber); + } else if (valueType == napi_boolean) { + bool valueBool = false; + napi_get_value_bool(env, value, &valueBool); + pacMap->PutBooleanValue(keyStr, valueBool); + } else if (valueType == napi_null) { + pacMap->PutObject(keyStr, nullptr); + } else if (valueType == napi_object) { + pacMap->PutStringValueArray(keyStr, ConvertStringVector(env, value)); + } else { + InfoLog("SetPacMapObject pacMap type error"); + } +} + +void AnalysisPacMap(std::shared_ptr &pacMap, const napi_env &env, const napi_value &arg) +{ + InfoLog("AnalysisPacMap begin"); + napi_value keys = 0; + napi_get_property_names(env, arg, &keys); + uint32_t arrLen = 0; + napi_status status = napi_get_array_length(env, keys, &arrLen); + if (status != napi_ok) { + InfoLog("AnalysisPacMap errr"); + return; + } + for (size_t i = 0; i < arrLen; ++i) { + napi_value key = 0; + status = napi_get_element(env, keys, i, &key); + std::string keyStr = UnwrapStringFromJS(env, key); + napi_value value = 0; + napi_get_property(env, arg, key, &value); + SetPacMapObject(pacMap, env, keyStr, value); + } +} + +napi_value ParseExtrasData(napi_env env, napi_value obj, std::shared_ptr &tagTechExtrasData) +{ + napi_valuetype valueType = napi_undefined; + + napi_typeof(env, obj, &valueType); + + if (valueType == napi_object) { + InfoLog("PacMap parse begin"); + AnalysisPacMap(tagTechExtrasData, env, obj); + } else { + InfoLog("ParseExtrasData wrong arg!"); + return nullptr; + } + + return NfcNapiCommon::CreateUndefined(env); +} + +napi_value ParseTagSession(napi_env env, napi_value obj, OHOS::sptr &tagSession) +{ + napi_valuetype valueType = napi_undefined; + + napi_typeof(env, obj, &valueType); + + if (valueType == napi_object) { + InfoLog("TagSession is object"); + napi_wrap( + env, obj, tagSession, + [](napi_env env, void *data, void *hint) { + OHOS::sptr *tagSession = (OHOS::sptr *)data; + delete tagSession; + }, + nullptr, nullptr); + InfoLog("wrap tagSession obj %{public}p", obj); + } else { + InfoLog("ParseTagSession arg err!"); + return nullptr; + } + + return NfcNapiCommon::CreateUndefined(env); +} + +std::shared_ptr ParseTagInfo(napi_env env, napi_value obj) +{ + std::string tagUid = NfcNapiCommon::GetNapiStringValue(env, obj, "uid"); + InfoLog("tag uid:%{public}s", tagUid.c_str()); + std::vector tagTechList; + napi_value technology = NfcNapiCommon::GetNamedProperty(env, obj, "technology"); + if (technology) { + if (ParseIntArray(env, technology, tagTechList) == nullptr) { + InfoLog("parse tagTechList failed"); + return nullptr; + } + } + + std::shared_ptr tagTechExtrasData = std::make_shared(); + napi_value extrasData = NfcNapiCommon::GetNamedProperty(env, obj, "extrasData"); + if (extrasData) { + if (ParseExtrasData(env, extrasData, tagTechExtrasData) == nullptr) { + InfoLog("parse tagTechExtrasData failed"); + return nullptr; + } + } + + int tagRfDiscId = NfcNapiCommon::GetNapiInt32Value(env, obj, "tagRfDiscId"); + InfoLog("tag RfDiscId:%{public}d", tagRfDiscId); + + OHOS::sptr tagSession = nullptr; + napi_value remoteTagSession = NfcNapiCommon::GetNamedProperty(env, obj, "remoteTagService"); + if (remoteTagSession) { + if (ParseTagSession(env, remoteTagSession, tagSession) == nullptr) { + InfoLog("parse tagTechList failed"); + return nullptr; + } + } + InfoLog("taginfo parse finished."); + return std::make_shared(tagTechList, tagTechExtrasData, tagUid, tagRfDiscId, tagSession); +} + +void RegisterTagA(NapiNfcATag *nfcATag, std::shared_ptr nfcATaginfo) +{ + std::shared_ptr NfcATagPtr = NfcATag::GetTag(nfcATaginfo); + if (NfcATagPtr == nullptr) { + InfoLog("Get NfcA Tag failed"); + return; + } else { + NfcTagContext instance = NfcTagContext::GetInstance(); + instance.Register(nfcATag, NfcATagPtr); + } +} + +napi_value JS_Constructor(napi_env env, napi_callback_info cbinfo) +{ + InfoLog("nfcTag JS_Constructor"); + std::shared_ptr nfcATaginfo; + NapiNfcATag *nfcATag = new NapiNfcATag(); + size_t argc = 1; + napi_value argv[] = {nullptr}; + napi_value thisVar = nullptr; + NAPI_CALL(env, napi_get_cb_info(env, cbinfo, &argc, argv, &thisVar, nullptr)); + + if (argc == static_cast(JS_ARGV_INDEX::ARGV_INDEX_1)) { + napi_valuetype valueType = napi_undefined; + NAPI_CALL(env, napi_typeof(env, argv[static_cast(JS_ARGV_INDEX::ARGV_INDEX_0)], &valueType)); + if (valueType == napi_object) { + nfcATaginfo = ParseTagInfo(env, argv[static_cast(JS_ARGV_INDEX::ARGV_INDEX_0)]); + if (nfcATaginfo) { + InfoLog("taginfo parse succeed."); + RegisterTagA(nfcATag, nfcATaginfo); + } else { + InfoLog("taginfo parse failed."); + return nullptr; + } + } else { + InfoLog("invalid data type!"); + return nullptr; + } + } else { + InfoLog("Invalid number of arguments"); + return nullptr; + } + napi_wrap( + env, thisVar, nfcATag, + [](napi_env env, void *data, void *hint) { + NfcATag *nfcATag = (NfcATag *)data; + delete nfcATag; + }, + nullptr, nullptr); + InfoLog("thisVar %{public}p", thisVar); + return thisVar; +} + +napi_value RegisternfcATagObject(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = { + DECLARE_NAPI_FUNCTION("getSak", NapiNfcATag::GetSak), + DECLARE_NAPI_FUNCTION("getAtqa", NapiNfcATag::GetAtqa), + DECLARE_NAPI_FUNCTION("connectTag", NapiNfcTagSession::ConnectTag), + DECLARE_NAPI_FUNCTION("reset", NapiNfcTagSession::Reset), + DECLARE_NAPI_FUNCTION("isTagConnected", NapiNfcTagSession::IsTagConnected), + DECLARE_NAPI_FUNCTION("getMaxSendLength", NapiNfcTagSession::GetMaxSendLength), + }; + NAPI_CALL(env, + napi_define_class(env, "NfcATag", NAPI_AUTO_LENGTH, JS_Constructor, nullptr, sizeof(desc) / sizeof(desc[0]), + desc, &nfcATagObject)); + return exports; +} + +napi_value GetNfcATag(napi_env env, napi_callback_info info) +{ + InfoLog("nfcTag GetNfcATag begin"); + std::size_t argc = 1; + napi_value argv[] = {nullptr}; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr)); + napi_value result = nullptr; + NAPI_CALL(env, napi_new_instance(env, nfcATagObject, argc, argv, &result)); + return result; +} + +napi_value NapiNfcATag::GetSak(napi_env env, napi_callback_info info) +{ + InfoLog("GetNfcATag GetSak called"); + napi_value thisVar = nullptr; + std::size_t argc = 0; + napi_value argv[] = {nullptr}; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr)); + NapiNfcATag *objectInfo = nullptr; + napi_unwrap(env, thisVar, (void **)&objectInfo); + InfoLog("getSak objInfo %{public}p", objectInfo); + NfcTagContext instance = NfcTagContext::GetInstance(); + std::shared_ptr NfcATagPtr = instance.Find(objectInfo); + if (NfcATagPtr == nullptr) { + InfoLog("GetSak find objectInfo failed!"); + return nullptr; + } else { + int sak = NfcATagPtr->GetSak(); + napi_value result = nullptr; + napi_create_int32(env, sak, &result); + return result; + } +} + +napi_value NapiNfcATag::GetAtqa(napi_env env, napi_callback_info info) +{ + InfoLog("GetNfcATag GetAtqa called"); + napi_value thisVar = nullptr; + std::size_t argc = 0; + napi_value argv[] = {nullptr}; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr)); + NapiNfcATag *objectInfo = nullptr; + napi_unwrap(env, thisVar, (void **)&objectInfo); + InfoLog("getAtqa %{public}p", objectInfo); + NfcTagContext instance = NfcTagContext::GetInstance(); + std::shared_ptr NfcATagPtr = instance.Find(objectInfo); + if (NfcATagPtr == nullptr) { + DebugLog("GetAtqa find objectInfo failed!"); + return nullptr; + } else { + napi_value ret = nullptr; + napi_create_array(env, &ret); + std::string atqa = NfcATagPtr->GetAtqa(); + napi_create_string_utf8(env, "atqa", NAPI_AUTO_LENGTH, &ret); + return ret; + } +} + +napi_value NapiNfcTagSession::ConnectTag(napi_env env, napi_callback_info info) +{ + InfoLog("GetTagSession ConnectTag called"); + std::size_t argc = 0; + napi_value argv[] = {nullptr}; + napi_value result = nullptr; + napi_value thisVar = nullptr; + bool isConnected = false; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr)); + NapiNfcATag *objectInfo = nullptr; + napi_unwrap(env, thisVar, (void **)&objectInfo); + NfcTagContext instance = NfcTagContext::GetInstance(); + std::shared_ptr NfcATagPtr = instance.Find(objectInfo); + if (NfcATagPtr == nullptr) { + DebugLog("ConnectTag find objectInfo failed!"); + return nullptr; + } else { + napi_value ret = nullptr; + isConnected = NfcATagPtr->Connect(); + napi_get_boolean(env, isConnected, &result); + return ret; + } +} + +napi_value NapiNfcTagSession::Reset(napi_env env, napi_callback_info info) +{ + InfoLog("TagSession Reset called"); + std::size_t argc = 0; + napi_value argv[] = {nullptr}; + napi_value result = nullptr; + napi_value thisVar = nullptr; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr)); + NapiNfcATag *objectInfo = nullptr; + napi_unwrap(env, thisVar, (void **)&objectInfo); + NfcTagContext instance = NfcTagContext::GetInstance(); + std::shared_ptr NfcATagPtr = instance.Find(objectInfo); + if (NfcATagPtr == nullptr) { + DebugLog("Reset find objectInfo failed!"); + return nullptr; + } else { + int err = NfcATagPtr->Close(); + if (err != NfcErrorCode::NFC_SUCCESS) { + InfoLog("reset failed!"); + } else + InfoLog("reset finished."); + return result; + } +} + +napi_value NapiNfcTagSession::IsTagConnected(napi_env env, napi_callback_info info) +{ + InfoLog("GetTagSession IsTagConnected called"); + std::size_t argc = 0; + napi_value argv[] = {nullptr}; + napi_value result = nullptr; + napi_value thisVar = nullptr; + bool connectTag = false; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr)); + NapiNfcATag *objectInfo = nullptr; + napi_unwrap(env, thisVar, (void **)&objectInfo); + NfcTagContext instance = NfcTagContext::GetInstance(); + std::shared_ptr NfcATagPtr = instance.Find(objectInfo); + if (NfcATagPtr == nullptr) { + DebugLog("IsTagConnected find objectInfo failed!"); + return nullptr; + } else { + connectTag = NfcATagPtr->IsConnected(); + napi_get_boolean(env, connectTag, &result); + return result; + } +} + +napi_value NapiNfcTagSession::GetMaxSendLength(napi_env env, napi_callback_info info) +{ + InfoLog("TagSession GetMaxSendLength called"); + std::size_t argc = 0; + napi_value argv[] = {nullptr}; + napi_value result = nullptr; + napi_value thisVar = nullptr; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr)); + NapiNfcATag *objectInfo = nullptr; + napi_unwrap(env, thisVar, (void **)&objectInfo); + NfcTagContext instance = NfcTagContext::GetInstance(); + std::shared_ptr NfcATagPtr = instance.Find(objectInfo); + if (NfcATagPtr == nullptr) { + DebugLog("GetMaxSendLength find objectInfo failed!"); + return nullptr; + } else { + int maxsendlen = NfcATagPtr->GetMaxSendCommandLength(); + napi_create_int32(env, maxsendlen, &result); + return result; + } +} +} // namespace KITS +} // namespace NFC +} // namespace OHOS diff --git a/nfc_core/interfaces/js/napi/tag/nfc_napi_taga.h b/nfc_core/interfaces/js/napi/tag/nfc_napi_taga.h new file mode 100755 index 0000000000000000000000000000000000000000..755a4522b4c868a77f6a2148804cd1f09d92a9cd --- /dev/null +++ b/nfc_core/interfaces/js/napi/tag/nfc_napi_taga.h @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef NFC_NAPI_TAGA_H_ +#define NFC_NAPI_TAGA_H_ + +#include "napi/native_api.h" +#include "napi/native_node_api.h" +#include "nfc_napi_tagsesstion.h" +#include "../../innerkits/include/tags/taginfo.h" + +namespace OHOS { +namespace NFC { +namespace KITS { +struct NapiNfcATag : public NapiNfcTagSession { + static napi_value GetSak(napi_env env, napi_callback_info info); + static napi_value GetAtqa(napi_env env, napi_callback_info info); +}; +} // namespace KITS +} // namespace NFC +} // namespace OHOS +#endif \ No newline at end of file diff --git a/nfc_core/interfaces/js/napi/tag/nfc_napi_tagsesstion.h b/nfc_core/interfaces/js/napi/tag/nfc_napi_tagsesstion.h new file mode 100755 index 0000000000000000000000000000000000000000..b212f68d0af07843b45412368929e7babb914c35 --- /dev/null +++ b/nfc_core/interfaces/js/napi/tag/nfc_napi_tagsesstion.h @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef NFC_NAPI_TAGSESSTION_H_ +#define NFC_NAPI_TAGSESSTION_H_ + +#include "napi/native_api.h" +#include "napi/native_node_api.h" + +namespace OHOS { +namespace NFC { +namespace KITS { +struct NapiNfcTagSession { + static napi_value GetTagInfo(napi_env env, napi_callback_info info); + static napi_value ConnectTag(napi_env env, napi_callback_info info); + static napi_value Reset(napi_env env, napi_callback_info info); + static napi_value IsTagConnected(napi_env env, napi_callback_info info); + static napi_value SetSendDataTimeout(napi_env env, napi_callback_info info); + static napi_value GetSendDataTimeout(napi_env env, napi_callback_info info); + static napi_value SendData(napi_env env, napi_callback_info info); + static napi_value GetMaxSendLength(napi_env env, napi_callback_info info); +}; +} // namespace KITS +} // namespace NFC +} // namespace OHOS +#endif \ No newline at end of file diff --git a/nfc_core/interfaces/js/napi/tag/nfc_napi_utils.cpp b/nfc_core/interfaces/js/napi/tag/nfc_napi_utils.cpp new file mode 100755 index 0000000000000000000000000000000000000000..49b4c3c643447c83ed758b243170be2e14cfb466 --- /dev/null +++ b/nfc_core/interfaces/js/napi/tag/nfc_napi_utils.cpp @@ -0,0 +1,345 @@ +/* + * Copyright (C) 2021-2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "nfc_napi_utils.h" +#include +#include +#include "loghelper.h" +#include "securec.h" + +namespace OHOS { +namespace NFC { +namespace KITS { +napi_value UndefinedNapiValue(const napi_env &env) +{ + napi_value result; + napi_get_undefined(env, &result); + return result; +} + +napi_value JsObjectToInt(const napi_env &env, const napi_value &object, const char *fieldStr, int &fieldRef) +{ + bool hasProperty = false; + NAPI_CALL(env, napi_has_named_property(env, object, fieldStr, &hasProperty)); + if (hasProperty) { + napi_value field; + napi_valuetype valueType; + + napi_get_named_property(env, object, fieldStr, &field); + NAPI_CALL(env, napi_typeof(env, field, &valueType)); + NAPI_ASSERT(env, valueType == napi_number, "Wrong argument type. Number expected."); + napi_get_value_int32(env, field, &fieldRef); + } else { + DebugLog("Js to int no property: %{public}s", fieldStr); + } + return UndefinedNapiValue(env); +} + +napi_value JsObjectToUint(const napi_env &env, const napi_value &object, const char *fieldStr, uint32_t &fieldRef) +{ + bool hasProperty = false; + NAPI_CALL(env, napi_has_named_property(env, object, fieldStr, &hasProperty)); + if (hasProperty) { + napi_value field; + napi_valuetype valueType; + + napi_get_named_property(env, object, fieldStr, &field); + NAPI_CALL(env, napi_typeof(env, field, &valueType)); + NAPI_ASSERT(env, valueType == napi_number, "Wrong argument type. Number expected."); + napi_get_value_uint32(env, field, &fieldRef); + } else { + DebugLog("Js to int no property: %{public}s", fieldStr); + } + return UndefinedNapiValue(env); +} + +napi_value JsObjectToBool(const napi_env &env, const napi_value &object, const char *fieldStr, bool &fieldRef) +{ + bool hasProperty = false; + NAPI_CALL(env, napi_has_named_property(env, object, fieldStr, &hasProperty)); + if (hasProperty) { + napi_value field; + napi_valuetype valueType; + + napi_get_named_property(env, object, fieldStr, &field); + NAPI_CALL(env, napi_typeof(env, field, &valueType)); + NAPI_ASSERT(env, valueType == napi_boolean, "Wrong argument type. Bool expected."); + napi_get_value_bool(env, field, &fieldRef); + } else { + DebugLog("Js to bool no property: %{public}s", fieldStr); + } + return UndefinedNapiValue(env); +} + +napi_status SetValueUtf8String(const napi_env &env, const char *fieldStr, const char *str, napi_value &result) +{ + napi_value value; + napi_status status = napi_create_string_utf8(env, str, NAPI_AUTO_LENGTH, &value); + if (status != napi_ok) { + DebugLog("Set value create utf8 string error! field: %{public}s", fieldStr); + return status; + } + status = napi_set_named_property(env, result, fieldStr, value); + if (status != napi_ok) { + DebugLog("Set utf8 string named property error! field: %{public}s", fieldStr); + } + return status; +} + +napi_status SetValueInt32(const napi_env &env, const char *fieldStr, const int intValue, napi_value &result) +{ + napi_value value; + napi_status status = napi_create_int32(env, intValue, &value); + if (status != napi_ok) { + DebugLog("Set value create int32 error! field: %{public}s", fieldStr); + return status; + } + status = napi_set_named_property(env, result, fieldStr, value); + if (status != napi_ok) { + DebugLog("Set int32 named property error! field: %{public}s", fieldStr); + } + return status; +} + +napi_status SetValueUnsignedInt32(const napi_env &env, const char *fieldStr, const int intValue, napi_value &result) +{ + napi_value value; + napi_status status = napi_create_uint32(env, intValue, &value); + if (status != napi_ok) { + DebugLog("Set value create unsigned int32 error! field: %{public}s", fieldStr); + return status; + } + status = napi_set_named_property(env, result, fieldStr, value); + if (status != napi_ok) { + DebugLog("Set unsigned int32 named property error! field: %{public}s", fieldStr); + } + return status; +} + +napi_status SetValueInt64(const napi_env &env, const char *fieldStr, const int64_t intValue, napi_value &result) +{ + napi_value value; + napi_status status = napi_create_int64(env, intValue, &value); + if (status != napi_ok) { + DebugLog("Set value create int64 error! field: %{public}s", fieldStr); + return status; + } + status = napi_set_named_property(env, result, fieldStr, value); + if (status != napi_ok) { + DebugLog("Set int64 named property error! field: %{public}s", fieldStr); + } + return status; +} + +napi_status SetValueBool(const napi_env &env, const char *fieldStr, const bool boolvalue, napi_value &result) +{ + napi_value value; + napi_status status = napi_get_boolean(env, boolvalue, &value); + if (status != napi_ok) { + DebugLog("Set value create boolean error! field: %{public}s", fieldStr); + return status; + } + status = napi_set_named_property(env, result, fieldStr, value); + if (status != napi_ok) { + DebugLog("Set boolean named property error! field: %{public}s", fieldStr); + } + return status; +} + +std::vector ConvertStringVector(napi_env env, napi_value jsValue) +{ + bool isTypedArray = false; + napi_status status = napi_is_typedarray(env, jsValue, &isTypedArray); + if (status != napi_ok || !isTypedArray) { + DebugLog("%{public}s called, napi_is_typedarray error", __func__); + return {}; + } + + napi_typedarray_type type; + size_t length = 0; + napi_value buffer = nullptr; + size_t offset = 0; + NAPI_CALL_BASE(env, napi_get_typedarray_info(env, jsValue, &type, &length, nullptr, &buffer, &offset), {}); + if (type != napi_uint8_array) { + DebugLog("%{public}s called, napi_uint8_array is null", __func__); + return {}; + } + std::string *data = nullptr; + size_t total = 0; + NAPI_CALL_BASE(env, napi_get_arraybuffer_info(env, buffer, reinterpret_cast(&data), &total), {}); + length = std::min(length, total - offset); + std::vector result(sizeof(std::string) + length); + int retCode = memcpy_s(result.data(), result.size(), &data[offset], length); + if (retCode != 0) { + return {}; + } + return result; +} + +std::string UnwrapStringFromJS(napi_env env, napi_value arg) +{ + constexpr size_t MAX_TEXT_LENGTH = 1024; + char msgChars[MAX_TEXT_LENGTH] = {0}; + size_t msgLength = 0; + NAPI_CALL_BASE(env, napi_get_value_string_utf8(env, arg, msgChars, MAX_TEXT_LENGTH, &msgLength), ""); + DebugLog("NapiUtil GetStringFromValue msgLength = %{public}zu", msgLength); + if (msgLength > 0) { + return std::string(msgChars, 0, msgLength); + } else { + return ""; + } +} + +bool UnwrapIntValue(napi_env env, napi_value jsValue, int &result) +{ + napi_valuetype jsValueType = napi_undefined; + NAPI_CALL_BASE(env, napi_typeof(env, jsValue, &jsValueType), false); + if (jsValueType != napi_number) { + return false; + } + int32_t natValue32 = 0; + NAPI_CALL_BASE(env, napi_get_value_int32(env, jsValue, &natValue32), false); + result = static_cast(natValue32); + return true; +} +napi_value GetCallbackErrorValue(napi_env env, int errCode) +{ + napi_value jsObject = nullptr; + napi_value jsValue = nullptr; + NAPI_CALL(env, napi_create_int32(env, errCode, &jsValue)); + NAPI_CALL(env, napi_create_object(env, &jsObject)); + NAPI_CALL(env, napi_set_named_property(env, jsObject, "code", jsValue)); + return jsObject; +} + +static napi_value InitAsyncCallBackEnv(const napi_env &env, AsyncContext *asyncContext, const size_t argc, + const napi_value *argv, const size_t nonCallbackArgNum) +{ + for (size_t i = nonCallbackArgNum; i != argc; ++i) { + napi_valuetype valuetype; + NAPI_CALL(env, napi_typeof(env, argv[i], &valuetype)); + NAPI_ASSERT(env, valuetype == napi_function, "Wrong argument type. Function expected."); + napi_create_reference(env, argv[i], 1, &asyncContext->callback[i - nonCallbackArgNum]); + } + return nullptr; +} + +static napi_value InitAsyncPromiseEnv(const napi_env &env, AsyncContext *asyncContext, napi_value &promise) +{ + napi_deferred deferred; + NAPI_CALL(env, napi_create_promise(env, &deferred, &promise)); + asyncContext->deferred = deferred; + return nullptr; +} + +static napi_value DoCallBackAsyncWork(const napi_env &env, AsyncContext *asyncContext) +{ + napi_create_async_work( + env, nullptr, asyncContext->resourceName, + [](napi_env env, void *data) { + if (data == nullptr) { + DebugLog("Async data parameter is null"); + return; + } + AsyncContext *context = (AsyncContext *)data; + context->executeFunc(context); + }, + [](napi_env env, napi_status status, void *data) { + if (data == nullptr) { + DebugLog("Async data parameter is null"); + return; + } + AsyncContext *context = (AsyncContext *)data; + napi_value undefine; + napi_get_undefined(env, &undefine); + napi_value callback; + context->completeFunc(data); + constexpr int ARGS_TWO = 2; + napi_value result[ARGS_TWO] = {nullptr}; + napi_create_uint32(env, context->errorCode, &result[0]); + result[1] = context->result; + if (context->errorCode == 0) { + napi_get_reference_value(env, context->callback[0], &callback); + napi_call_function(env, nullptr, callback, ARGS_TWO, result, &undefine); + } else { + if (context->callback[1]) { + napi_get_reference_value(env, context->callback[1], &callback); + napi_call_function(env, nullptr, callback, ARGS_TWO, result, &undefine); + } else { + DebugLog("Get callback func[1] is null"); + } + } + if (context->callback[0] != nullptr) { + napi_delete_reference(env, context->callback[0]); + } + if (context->callback[1] != nullptr) { + napi_delete_reference(env, context->callback[1]); + } + napi_delete_async_work(env, context->work); + delete context; + }, + (void *)asyncContext, &asyncContext->work); + NAPI_CALL(env, napi_queue_async_work(env, asyncContext->work)); + return UndefinedNapiValue(env); +} + +static napi_value DoPromiseAsyncWork(const napi_env &env, AsyncContext *asyncContext) +{ + napi_create_async_work( + env, nullptr, asyncContext->resourceName, + [](napi_env env, void *data) { + if (data == nullptr) { + DebugLog("Async data parameter is null"); + return; + } + AsyncContext *context = (AsyncContext *)data; + context->executeFunc(context); + }, + [](napi_env env, napi_status status, void *data) { + if (data == nullptr) { + DebugLog("Async data parameter is null"); + return; + } + AsyncContext *context = (AsyncContext *)data; + context->completeFunc(data); + if (context->errorCode == 0) { + napi_resolve_deferred(context->env, context->deferred, context->result); + } else { + napi_reject_deferred(context->env, context->deferred, context->result); + } + napi_delete_async_work(env, context->work); + delete context; + }, + (void *)asyncContext, &asyncContext->work); + napi_queue_async_work(env, asyncContext->work); + return UndefinedNapiValue(env); +} + +napi_value DoAsyncWork(const napi_env &env, AsyncContext *asyncContext, const size_t argc, const napi_value *argv, + const size_t nonCallbackArgNum) +{ + if (argc > nonCallbackArgNum) { + InitAsyncCallBackEnv(env, asyncContext, argc, argv, nonCallbackArgNum); + return DoCallBackAsyncWork(env, asyncContext); + } else { + napi_value promise; + InitAsyncPromiseEnv(env, asyncContext, promise); + DoPromiseAsyncWork(env, asyncContext); + return promise; + } +} +} // namespace KITS +} // namespace NFC +} // namespace OHOS diff --git a/nfc_core/interfaces/js/napi/tag/nfc_napi_utils.h b/nfc_core/interfaces/js/napi/tag/nfc_napi_utils.h new file mode 100755 index 0000000000000000000000000000000000000000..90af49798bca9fdbf746a8402185fdd42270619f --- /dev/null +++ b/nfc_core/interfaces/js/napi/tag/nfc_napi_utils.h @@ -0,0 +1,88 @@ +/* + * Copyright (C) 2021-2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef NFC_NAPI_UTILS_H_ +#define NFC_NAPI_UTILS_H_ + +#include +#include +#include "napi/native_api.h" +#include "napi/native_node_api.h" + +namespace OHOS { +namespace NFC { +namespace KITS { +class TraceFuncCall final { +public: + explicit TraceFuncCall(std::string funcName); + + TraceFuncCall() = delete; + + ~TraceFuncCall(); + +private: + std::string m_funcName; + std::chrono::steady_clock::time_point m_startTime; + bool m_isTrace = true; +}; + +#define TRACE_FUNC_CALL TraceFuncCall func(__func__) + +class AsyncContext { +public: + napi_env env; + napi_async_work work; + napi_deferred deferred; + napi_ref callback[2] = {0}; + std::function executeFunc; + std::function completeFunc; + napi_value resourceName; + napi_value result; + int errorCode; + + AsyncContext(napi_env e, napi_async_work w = nullptr, napi_deferred d = nullptr) + { + env = e; + work = w; + deferred = d; + executeFunc = nullptr; + completeFunc = nullptr; + result = nullptr; + errorCode = 0; + } + + AsyncContext() = delete; + + virtual ~AsyncContext() {} +}; + +napi_value UndefinedNapiValue(const napi_env &env); +napi_value JsObjectToInt(const napi_env &env, const napi_value &object, const char *fieldStr, int &fieldRef); +napi_value JsObjectToUint(const napi_env &env, const napi_value &object, const char *fieldStr, uint32_t &fieldRef); +napi_value JsObjectToBool(const napi_env &env, const napi_value &object, const char *fieldStr, bool &fieldRef); +napi_status SetValueUtf8String(const napi_env &env, const char *fieldStr, const char *str, napi_value &result); +napi_status SetValueInt32(const napi_env &env, const char *fieldStr, const int intValue, napi_value &result); +napi_status SetValueUnsignedInt32(const napi_env &env, const char *fieldStr, const int intValue, napi_value &result); +napi_status SetValueInt64(const napi_env &env, const char *fieldStr, const int64_t intValue, napi_value &result); +void ConvertStringVectorToJS(napi_env env, napi_value result, std::vector &stringVector); +std::vector ConvertStringVector(napi_env env, napi_value jsValue); +std::string UnwrapStringFromJS(napi_env env, napi_value param); +napi_status SetValueBool(const napi_env &env, const char *fieldStr, const bool boolValue, napi_value &result); +napi_value DoAsyncWork(const napi_env &env, AsyncContext *asyncContext, const size_t argc, const napi_value *argv, + const size_t nonCallbackArgNum); +} // namespace KITS +} // namespace NFC +} // namespace OHOS +#endif \ No newline at end of file diff --git a/nfc_core/interfaces/js/napi/tag/nfc_tag_context.cpp b/nfc_core/interfaces/js/napi/tag/nfc_tag_context.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7bbac90377756b7ef39979761e52603ffa138f4d --- /dev/null +++ b/nfc_core/interfaces/js/napi/tag/nfc_tag_context.cpp @@ -0,0 +1,55 @@ +/* + * Copyright (C) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "nfc_tag_context.h" + +#include +#include +#include +#include "napi/native_api.h" +#include "napi/native_node_api.h" +#include "nfc_napi_taga.h" +#include "../../innerkits/include/tags/nfca_tag.h" +#include "loghelper.h" +#include "taginfo.h" + +namespace OHOS { +namespace NFC { +namespace KITS { +std::map> tagMap; + +NfcTagContext &NfcTagContext::GetInstance() +{ + DebugLog("NfcTagContext::GetInstance"); + static NfcTagContext instance; + return instance; +} + +void NfcTagContext::Register(NapiNfcATag *jsObj, std::shared_ptr serviceObj) +{ + tagMap[jsObj] = serviceObj; +} + +std::shared_ptr NfcTagContext::Find(NapiNfcATag *jsObj) +{ + auto search = tagMap.find(jsObj); + if (search != tagMap.end()) { + return tagMap[jsObj]; + } else + return {}; +} +} // namespace KITS +} // namespace NFC +} // namespace OHOS \ No newline at end of file diff --git a/nfc_core/interfaces/js/napi/tag/nfc_tag_context.h b/nfc_core/interfaces/js/napi/tag/nfc_tag_context.h new file mode 100755 index 0000000000000000000000000000000000000000..96ab12ec8f50ac960fe65bac45121a6fea1ba787 --- /dev/null +++ b/nfc_core/interfaces/js/napi/tag/nfc_tag_context.h @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2021-2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef NFC_TAG_CONTEXT_H_ +#define NFC_TAG_CONTEXT_H_ + +#include +#include +#include +#include "napi/native_api.h" +#include "napi/native_node_api.h" +#include "nfc_napi_taga.h" +#include "nfc_napi_tagsesstion.h" +#include "../../innerkits/include/tags/nfca_tag.h" +#include "taginfo.h" + +namespace OHOS { +namespace NFC { +namespace KITS { +class NfcTagContext { +public: + static NfcTagContext &GetInstance(); + static void Register(NapiNfcATag *jsObj, std::shared_ptr serviceObj); + static std::shared_ptr Find(NapiNfcATag *jsObj); + +private: +}; +} // namespace KITS +} // namespace NFC +} // namespace OHOS +#endif \ No newline at end of file