From 749c7388c6ce0d5dfd1d657a83af6a0ce1e5a6de Mon Sep 17 00:00:00 2001 From: Kemin <541416002@qq.com> Date: Wed, 30 Nov 2022 14:49:29 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0napi=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- device_attest/build/BUILD.gn | 1 + device_attest/interfaces/kits/napi/BUILD.gn | 55 +++++++++++++ .../kits/napi/include/devattest_napi.h | 34 ++++++++ .../kits/napi/src/devattest_napi.cpp | 82 +++++++++++++++++++ hap/api/@ohos.devattestkit.d.ts | 41 ++++++++++ hap/js/index.ets | 23 ++++++ 6 files changed, 236 insertions(+) create mode 100644 device_attest/interfaces/kits/napi/BUILD.gn create mode 100644 device_attest/interfaces/kits/napi/include/devattest_napi.h create mode 100644 device_attest/interfaces/kits/napi/src/devattest_napi.cpp create mode 100644 hap/api/@ohos.devattestkit.d.ts create mode 100644 hap/js/index.ets diff --git a/device_attest/build/BUILD.gn b/device_attest/build/BUILD.gn index 0ffb7d9..1d72708 100644 --- a/device_attest/build/BUILD.gn +++ b/device_attest/build/BUILD.gn @@ -22,6 +22,7 @@ group("attest_standard_packages") { "${devattest_path}/services/sa_profile:devattest_sa_profile", "${devattest_path}/services/devattest_ability:devattest_service", "${devattest_path}/interfaces/innerkits/native_cpp:devattest_sdk", + "${devattest_path}/interfaces/kits/napi:devattestkit", ] if (enable_attest_test_sample) { deps += ["${devattest_path}/sample/client:attesttestclient"] diff --git a/device_attest/interfaces/kits/napi/BUILD.gn b/device_attest/interfaces/kits/napi/BUILD.gn new file mode 100644 index 0000000..20a93f7 --- /dev/null +++ b/device_attest/interfaces/kits/napi/BUILD.gn @@ -0,0 +1,55 @@ +# Copyright (c) 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. + +import("//build/ohos.gni") +import("//test/xts/device_attest/build/devattestconfig.gni") + +config("devattest_kit_config") { + include_dirs = [ + "include", + "${devattest_path}/common/log", + "${devattest_path}/interfaces/innerkits/native_cpp/include", + "${devattest_path}/services/devattest_ability/include", + ] + + include_dirs += [ + "//third_party/libuv/include", + "//third_party/node/src", + "//foundation/ace/napi/interfaces/kits", + ] +} + +ohos_shared_library("devattestkit") { + include_dirs = [] + configs = [ ":devattest_kit_config" ] + public_configs = [ ":devattest_kit_config" ] + sources = [ "src/devattest_napi.cpp" ] + deps = [ + "${devattest_path}/interfaces/innerkits/native_cpp:devattest_sdk", + "//foundation/arkui/napi:ace_napi", + ] + external_deps = [ + "access_token:libaccesstoken_sdk", + "c_utils:utils", + "eventhandler:libeventhandler", + "hisysevent_native:libhisysevent", + "hiviewdfx_hilog_native:libhilog", + "init:libbegetutil", + "ipc:ipc_core", + "samgr:samgr_proxy", + ] + + relative_install_dir = "module" + subsystem_name = "xts" + part_name = "device_attest" +} diff --git a/device_attest/interfaces/kits/napi/include/devattest_napi.h b/device_attest/interfaces/kits/napi/include/devattest_napi.h new file mode 100644 index 0000000..25633da --- /dev/null +++ b/device_attest/interfaces/kits/napi/include/devattest_napi.h @@ -0,0 +1,34 @@ +/* + * Copyright (C) 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 DEVATTEST_NAPI__H +#define DEVATTEST_NAPI__H + +#include "napi/native_api.h" +#include "napi/native_node_api.h" + +namespace OHOS { +namespace DevAttest { +class DevAttestNapi { +public: + static napi_value Init(napi_env env, napi_value exports); +private: + DevAttestNapi() = default; + ~DevAttestNapi() = default; + static napi_value GetAttestResultInfo(napi_env env, napi_callback_info info); +}; +} // namespace DevAttest +} // namespace OHOS + +#endif // DEVATTEST_NAPI__H diff --git a/device_attest/interfaces/kits/napi/src/devattest_napi.cpp b/device_attest/interfaces/kits/napi/src/devattest_napi.cpp new file mode 100644 index 0000000..6683c99 --- /dev/null +++ b/device_attest/interfaces/kits/napi/src/devattest_napi.cpp @@ -0,0 +1,82 @@ +/* + * Copyright (C) 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 +// #include "napi/native_api.h" +// #include "napi/native_node_api.h" +#include "devattest_log.h" +#include "devattest_client.h" +#include "attest_result_info.h" +#include "devattest_napi.h" + +using namespace OHOS; +using namespace OHOS::DevAttest; + +napi_value DevAttestNapi::GetAttestResultInfo(napi_env env, napi_callback_info info) +{ + AttestResultInfo attestResultInfo; + //(void)OHOS::DelayedSingleton::GetInstance()->GetAttestStatus(attestResultInfo); + (void)DelayedSingleton::GetInstance()->GetAttestStatus(attestResultInfo); + + napi_value destObject; + napi_create_object(env, &destObject); + napi_value authResult; + napi_value softwareResult; + napi_value ticket; + + napi_create_int32(env, attestResultInfo.authResult_, &authResult); + napi_create_int32(env, attestResultInfo.softwareResult_, &softwareResult); + napi_create_string_utf8(env, attestResultInfo.ticket_.c_str(), attestResultInfo.ticket_.length(), &ticket); + napi_set_named_property(env, destObject, "auth", authResult); + napi_set_named_property(env, destObject, "software", softwareResult); + napi_set_named_property(env, destObject, "ticket", ticket); + return destObject; +} + +napi_value DevAttestNapi::Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = { + DECLARE_NAPI_STATIC_FUNCTION("GetAttestStatus", GetAttestResultInfo), + }; + NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc)); + return exports; +} + +EXTERN_C_START +static napi_value DevattestInit(napi_env env, napi_value exports) +{ + HILOGI("Initialize the DevAttestNapi module"); + + napi_value ret = DevAttestNapi::Init(env, exports); + + HILOGI("The initialization of the DevAttestNapi module is complete"); + + return ret; +} +EXTERN_C_END + +static napi_module _module = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = DevattestInit, + .nm_modname = "devattest", + .nm_priv = ((void *)0), + .reserved = { 0 } +}; + +extern "C" __attribute__((constructor)) void RegisterModule(void) +{ + napi_module_register(&_module); +} diff --git a/hap/api/@ohos.devattestkit.d.ts b/hap/api/@ohos.devattestkit.d.ts new file mode 100644 index 0000000..4168c90 --- /dev/null +++ b/hap/api/@ohos.devattestkit.d.ts @@ -0,0 +1,41 @@ +/* + * Copyright (c) 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. + */ +import { AsyncCallback, Callback } from './basic'; + + /** + * @since 9 + * @syscap 略 示例:SystemCapability.Location.Location.Core + */ +declare namespace devattest { + /** + * 接口功能描述 + * + * @since 9 + */ + function GetAttestStatus() : AttestResultInfo; + + /** + * AttestResultInfo structure + * + * @since 9 + */ + export interface AttestResultInfo { + auth : number; + software : number; + ticket : string; + } +} + +export default devattest; diff --git a/hap/js/index.ets b/hap/js/index.ets new file mode 100644 index 0000000..0a28f13 --- /dev/null +++ b/hap/js/index.ets @@ -0,0 +1,23 @@ +import devattest from "@ohos.devattestkit" + +@Entry +@Component +struct Index { + @State message: string = 'Hello World' + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(50) + .fontWeight(FontWeight.Bold).onClick(() => { + console.info("[kemin] Welcome"); + let attestResultInfo : devattest.AttestResultInfo = devattest.GetAttestStatus(); + console.info("[kemin][DevAttest] auth:" + attestResultInfo.auth + " software:" + attestResultInfo.software + " ticket:" + attestResultInfo.ticket); + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file -- Gitee