From 638c76211c75a47ae44c8922ec6441c15d77a5ad Mon Sep 17 00:00:00 2001 From: pxd2022 Date: Tue, 27 Feb 2024 07:04:04 +0000 Subject: [PATCH] support odid Signed-off-by: pxd2022 --- bundle.json | 4 +- interfaces/kits/jskits/BUILD.gn | 13 +++- .../kits/jskits/src/native_deviceinfo_js.cpp | 72 ++++++++++++++++++- 3 files changed, 83 insertions(+), 6 deletions(-) diff --git a/bundle.json b/bundle.json index 31d047dd5..a9cc5d14a 100755 --- a/bundle.json +++ b/bundle.json @@ -17,8 +17,7 @@ "subsystem": "startup", "syscap": [ "SystemCapability.Startup.SystemInfo", - "SystemCapability.Startup.SystemInfo.Lite", - "SystemCapability.Base" + "SystemCapability.Startup.SystemInfo.Lite" ], "adapted_system_type": [ "mini", @@ -47,6 +46,7 @@ "hvb", "hisysevent", "bounds_checking_function", + "bundle_framework", "hiprofiler" ], "third_party": [ diff --git a/interfaces/kits/jskits/BUILD.gn b/interfaces/kits/jskits/BUILD.gn index ad8e54a2d..8b23cb210 100755 --- a/interfaces/kits/jskits/BUILD.gn +++ b/interfaces/kits/jskits/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2021-2022 Huawei Device Co., Ltd. +# Copyright (c) 2021-2024 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 @@ -21,9 +21,16 @@ ohos_shared_library("deviceinfo") { deps = [ "//base/startup/init/interfaces/innerkits:libbeget_proxy", "//base/startup/init/interfaces/innerkits:libbegetutil", - "//third_party/bounds_checking_function:libsec_shared", ] - external_deps = [ "napi:ace_napi" ] + external_deps = [ + "bounds_checking_function:libsec_shared", + "bundle_framework:appexecfwk_core", + "c_utils:utils", + "hilog:libhilog_base", + "ipc:ipc_single", + "napi:ace_napi", + "samgr:samgr_proxy", + ] relative_install_dir = "module" subsystem_name = "startup" part_name = "init" diff --git a/interfaces/kits/jskits/src/native_deviceinfo_js.cpp b/interfaces/kits/jskits/src/native_deviceinfo_js.cpp index 94fc85289..8fcf023c9 100644 --- a/interfaces/kits/jskits/src/native_deviceinfo_js.cpp +++ b/interfaces/kits/jskits/src/native_deviceinfo_js.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2024 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 @@ -20,8 +20,36 @@ #include "napi/native_node_api.h" #include "parameter.h" #include "sysversion.h" +#include "bundlemgr/bundle_mgr_proxy.h" +#include "iservice_registry.h" +#include "if_system_ability_manager.h" +#include "system_ability_definition.h" +#include "beget_ext.h" +#include "init_error.h" +#include "securec.h" + +#ifndef DEVICEINFO_JS_DOMAIN +#define DEVICEINFO_JS_DOMAIN (BASE_DOMAIN + 8) +#endif + +#ifndef DINFO_TAG +#define DINFO_TAG "DEVICEINFO_JS" +#endif + +#define DEVINFO_LOGV(fmt, ...) STARTUP_LOGV(DEVICEINFO_JS_DOMAIN, DINFO_TAG, fmt, ##__VA_ARGS__) +#define DEVINFO_LOGI(fmt, ...) STARTUP_LOGI(DEVICEINFO_JS_DOMAIN, DINFO_TAG, fmt, ##__VA_ARGS__) +#define DEVINFO_LOGW(fmt, ...) STARTUP_LOGW(DEVICEINFO_JS_DOMAIN, DINFO_TAG, fmt, ##__VA_ARGS__) +#define DEVINFO_LOGE(fmt, ...) STARTUP_LOGE(DEVICEINFO_JS_DOMAIN, DINFO_TAG, fmt, ##__VA_ARGS__) const int UDID_LEN = 65; +const int ODID_LEN = 37; + +typedef enum { + DEV_INFO_OK, + DEV_INFO_ENULLPTR, + DEV_INFO_EGETODID, + DEV_INFO_ESTRCOPY +} DevInfoError; static napi_value GetDeviceType(napi_env env, napi_callback_info info) { @@ -399,6 +427,47 @@ static napi_value NAPI_GetDistributionOSReleaseType(napi_env env, napi_callback_ return napiValue; } +static DevInfoError AclGetDevOdid(char *odid, int size) +{ + auto systemAbilityManager = OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (!systemAbilityManager) { + return DEV_INFO_ENULLPTR; + } + + auto remoteObject = systemAbilityManager->GetSystemAbility(OHOS::BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); + if (!remoteObject) { + return DEV_INFO_ENULLPTR; + } + + auto bundleMgrProxy = OHOS::iface_cast(remoteObject); + if (!bundleMgrProxy) { + return DEV_INFO_ENULLPTR; + } + + std::string odidStr; + if (bundleMgrProxy->GetOdid(odidStr) != 0) { + return DEV_INFO_EGETODID; + } + + if (strcpy_s(odid, size, odidStr.c_str()) != EOK) { + return DEV_INFO_ESTRCOPY; + } + return DEV_INFO_OK; +} + +static napi_value GetDevOdid(napi_env env, napi_callback_info info) +{ + napi_value napiValue = nullptr; + char odid[ODID_LEN] = {0}; + DevInfoError ret = AclGetDevOdid(odid, ODID_LEN); + if (ret != DEV_INFO_OK) { + DEVINFO_LOGE("GetDevOdid ret:%d", ret); + } + + NAPI_CALL(env, napi_create_string_utf8(env, odid, strlen(odid), &napiValue)); + return napiValue; +} + EXTERN_C_START /* * Module init @@ -443,6 +512,7 @@ static napi_value Init(napi_env env, napi_value exports) {"distributionOSVersion", nullptr, nullptr, NAPI_GetDistributionOSVersion, nullptr, nullptr, napi_default, nullptr}, {"distributionOSApiVersion", nullptr, nullptr, NAPI_GetDistributionOSApiVersion, nullptr, nullptr, napi_default, nullptr}, {"distributionOSReleaseType", nullptr, nullptr, NAPI_GetDistributionOSReleaseType, nullptr, nullptr, napi_default, nullptr}, + {"odid", nullptr, nullptr, GetDevOdid, nullptr, nullptr, napi_default, nullptr}, }; NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(napi_property_descriptor), desc));