1 /*
2 * Copyright (c) 2022-2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include "device_info_addon.h"
16
17 #include "edm_ipc_interface_code.h"
18 #include "edm_log.h"
19 #include "napi_edm_adapter.h"
20
21 using namespace OHOS::EDM;
22
Init(napi_env env,napi_value exports)23 napi_value DeviceInfoAddon::Init(napi_env env, napi_value exports)
24 {
25 napi_property_descriptor property[] = {
26 DECLARE_NAPI_FUNCTION("getDeviceSerial", GetDeviceSerial),
27 DECLARE_NAPI_FUNCTION("getDisplayVersion", GetDisplayVersion),
28 DECLARE_NAPI_FUNCTION("getDeviceName", GetDeviceName),
29 DECLARE_NAPI_FUNCTION("getDeviceInfo", GetDeviceInfoSync),
30 };
31 NAPI_CALL(env, napi_define_properties(env, exports, sizeof(property) / sizeof(property[0]), property));
32 return exports;
33 }
34
GetDeviceSerial(napi_env env,napi_callback_info info)35 napi_value DeviceInfoAddon::GetDeviceSerial(napi_env env, napi_callback_info info)
36 {
37 EDMLOGI("NAPI_GetDeviceSerial called");
38 return GetDeviceInfo(env, info, EdmInterfaceCode::GET_DEVICE_SERIAL);
39 }
40
GetDisplayVersion(napi_env env,napi_callback_info info)41 napi_value DeviceInfoAddon::GetDisplayVersion(napi_env env, napi_callback_info info)
42 {
43 EDMLOGI("NAPI_GetDisplayVersion called");
44 return GetDeviceInfo(env, info, EdmInterfaceCode::GET_DISPLAY_VERSION);
45 }
46
GetDeviceName(napi_env env,napi_callback_info info)47 napi_value DeviceInfoAddon::GetDeviceName(napi_env env, napi_callback_info info)
48 {
49 EDMLOGI("NAPI_GetDeviceName called");
50 return GetDeviceInfo(env, info, EdmInterfaceCode::GET_DEVICE_NAME);
51 }
52
GetDeviceInfoSync(napi_env env,napi_callback_info info)53 napi_value DeviceInfoAddon::GetDeviceInfoSync(napi_env env, napi_callback_info info)
54 {
55 AddonMethodSign addonMethodSign;
56 addonMethodSign.name = "GetDeviceInfoSync";
57 addonMethodSign.argsType = {EdmAddonCommonType::ELEMENT, EdmAddonCommonType::STRING};
58 addonMethodSign.methodAttribute = MethodAttribute::GET;
59 AdapterAddonData adapterAddonData{};
60 napi_value result = JsObjectToData(env, info, addonMethodSign, &adapterAddonData);
61 if (result == nullptr) {
62 return nullptr;
63 }
64 std::string deviceInfo;
65 int32_t ret = DeviceInfoProxy::GetDeviceInfoProxy()->GetDeviceInfo(adapterAddonData.data,
66 "", EdmInterfaceCode::GET_DEVICE_INFO, deviceInfo);
67 if (FAILED(ret)) {
68 napi_throw(env, CreateError(env, ret));
69 return nullptr;
70 }
71 napi_value napiInfo;
72 NAPI_CALL(env, napi_create_string_utf8(env, deviceInfo.c_str(), NAPI_AUTO_LENGTH, &napiInfo));
73 return napiInfo;
74 }
75
GetDeviceInfo(napi_env env,napi_callback_info info,int code)76 napi_value DeviceInfoAddon::GetDeviceInfo(napi_env env, napi_callback_info info, int code)
77 {
78 AddonMethodSign addonMethodSign;
79 addonMethodSign.name = "GetDeviceInfo";
80 addonMethodSign.policyCode = code;
81 addonMethodSign.argsType = {EdmAddonCommonType::ELEMENT};
82 addonMethodSign.methodAttribute = MethodAttribute::GET;
83 return AddonMethodAdapter(env, info, addonMethodSign, NativeGetDeviceInfo, NativeStringCallbackComplete);
84 }
85
NativeGetDeviceInfo(napi_env env,void * data)86 void DeviceInfoAddon::NativeGetDeviceInfo(napi_env env, void *data)
87 {
88 EDMLOGI("NAPI_NativeGetDeviceInfo called");
89 if (data == nullptr) {
90 EDMLOGE("data is nullptr");
91 return;
92 }
93 AdapterAddonData *asyncCallbackInfo = static_cast<AdapterAddonData *>(data);
94 auto deviceInfoProxy = DeviceInfoProxy::GetDeviceInfoProxy();
95 if (deviceInfoProxy == nullptr) {
96 EDMLOGE("can not get DeviceInfoProxy");
97 return;
98 }
99 switch (asyncCallbackInfo->policyCode) {
100 case static_cast<int32_t>(EdmInterfaceCode::GET_DEVICE_SERIAL):
101 asyncCallbackInfo->ret =
102 deviceInfoProxy->GetDeviceSerial(asyncCallbackInfo->data, asyncCallbackInfo->stringRet);
103 break;
104 case static_cast<int32_t>(EdmInterfaceCode::GET_DISPLAY_VERSION):
105 asyncCallbackInfo->ret =
106 deviceInfoProxy->GetDisplayVersion(asyncCallbackInfo->data, asyncCallbackInfo->stringRet);
107 break;
108 case static_cast<int32_t>(EdmInterfaceCode::GET_DEVICE_NAME):
109 asyncCallbackInfo->ret =
110 deviceInfoProxy->GetDeviceName(asyncCallbackInfo->data, asyncCallbackInfo->stringRet);
111 break;
112 default:
113 asyncCallbackInfo->ret = EdmReturnErrCode::INTERFACE_UNSUPPORTED;
114 EDMLOGE("NAPI_GetDeviceInfo failed");
115 return;
116 }
117 }
118
119 static napi_module g_deviceInfoModule = {
120 .nm_version = 1,
121 .nm_flags = 0,
122 .nm_filename = nullptr,
123 .nm_register_func = DeviceInfoAddon::Init,
124 .nm_modname = "enterprise.deviceInfo",
125 .nm_priv = ((void *)0),
126 .reserved = {0},
127 };
128
DeviceInfoRegister()129 extern "C" __attribute__((constructor)) void DeviceInfoRegister()
130 {
131 napi_module_register(&g_deviceInfoModule);
132 }