1 /*
2 * Copyright (c) 2021 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
16 #include <cstdio>
17 #include <string>
18
19 #include "napi/native_api.h"
20 #include "napi/native_node_api.h"
21 #include "parameter.h"
22 #include "sysversion.h"
23
24 const int UDID_LEN = 65;
25
GetDeviceType(napi_env env,napi_callback_info info)26 static napi_value GetDeviceType(napi_env env, napi_callback_info info)
27 {
28 napi_value deviceType = nullptr;
29 const char *value = GetDeviceType();
30
31 NAPI_CALL(env, napi_create_string_utf8(env, value, strlen(value), &deviceType));
32 return deviceType;
33 }
34
GetManufacture(napi_env env,napi_callback_info info)35 static napi_value GetManufacture(napi_env env, napi_callback_info info)
36 {
37 napi_value napiValue = nullptr;
38 const char *manfactureName = GetManufacture();
39
40 NAPI_CALL(env, napi_create_string_utf8(env, manfactureName, strlen(manfactureName), &napiValue));
41 return napiValue;
42 }
43
GetBrand(napi_env env,napi_callback_info info)44 static napi_value GetBrand(napi_env env, napi_callback_info info)
45 {
46 napi_value napiValue = nullptr;
47 const char *productBrand = GetBrand();
48
49 NAPI_CALL(env, napi_create_string_utf8(env, productBrand, strlen(productBrand), &napiValue));
50 return napiValue;
51 }
52
GetMarketName(napi_env env,napi_callback_info info)53 static napi_value GetMarketName(napi_env env, napi_callback_info info)
54 {
55 napi_value napiValue = nullptr;
56 const char *marketName = GetMarketName();
57
58 NAPI_CALL(env, napi_create_string_utf8(env, marketName, strlen(marketName), &napiValue));
59 return napiValue;
60 }
61
GetProductSeries(napi_env env,napi_callback_info info)62 static napi_value GetProductSeries(napi_env env, napi_callback_info info)
63 {
64 napi_value napiValue = nullptr;
65 const char *productSeries = GetProductSeries();
66
67 NAPI_CALL(env, napi_create_string_utf8(env, productSeries, strlen(productSeries), &napiValue));
68 return napiValue;
69 }
70
GetProductModel(napi_env env,napi_callback_info info)71 static napi_value GetProductModel(napi_env env, napi_callback_info info)
72 {
73 napi_value napiValue = nullptr;
74 const char *productModel = GetProductModel();
75
76 NAPI_CALL(env, napi_create_string_utf8(env, productModel, strlen(productModel), &napiValue));
77 return napiValue;
78 }
79
GetSoftwareModel(napi_env env,napi_callback_info info)80 static napi_value GetSoftwareModel(napi_env env, napi_callback_info info)
81 {
82 napi_value napiValue = nullptr;
83 const char *softwareModel = GetSoftwareModel();
84
85 NAPI_CALL(env, napi_create_string_utf8(env, softwareModel, strlen(softwareModel), &napiValue));
86 return napiValue;
87 }
88
GetHardwareModel(napi_env env,napi_callback_info info)89 static napi_value GetHardwareModel(napi_env env, napi_callback_info info)
90 {
91 napi_value napiValue = nullptr;
92 const char *hardwareModel = GetHardwareModel();
93
94 NAPI_CALL(env, napi_create_string_utf8(env, hardwareModel, strlen(hardwareModel), &napiValue));
95 return napiValue;
96 }
97
GetHardwareProfile(napi_env env,napi_callback_info info)98 static napi_value GetHardwareProfile(napi_env env, napi_callback_info info)
99 {
100 napi_value napiValue = nullptr;
101 const char *hardwareProfile = GetHardwareProfile();
102
103 NAPI_CALL(env, napi_create_string_utf8(env, hardwareProfile, strlen(hardwareProfile), &napiValue));
104 return napiValue;
105 }
106
GetSerial(napi_env env,napi_callback_info info)107 static napi_value GetSerial(napi_env env, napi_callback_info info)
108 {
109 napi_value napiValue = nullptr;
110 const char *serialNumber = AclGetSerial();
111
112 NAPI_CALL(env, napi_create_string_utf8(env, serialNumber, strlen(serialNumber), &napiValue));
113 return napiValue;
114 }
115
GetBootloaderVersion(napi_env env,napi_callback_info info)116 static napi_value GetBootloaderVersion(napi_env env, napi_callback_info info)
117 {
118 napi_value napiValue = nullptr;
119 const char *bootloaderVersion = GetBootloaderVersion();
120
121 NAPI_CALL(env, napi_create_string_utf8(env, bootloaderVersion, strlen(bootloaderVersion), &napiValue));
122 return napiValue;
123 }
124
GetAbiList(napi_env env,napi_callback_info info)125 static napi_value GetAbiList(napi_env env, napi_callback_info info)
126 {
127 napi_value napiValue = nullptr;
128 const char *abiList = GetAbiList();
129
130 NAPI_CALL(env, napi_create_string_utf8(env, abiList, strlen(abiList), &napiValue));
131 return napiValue;
132 }
133
GetSecurityPatchTag(napi_env env,napi_callback_info info)134 static napi_value GetSecurityPatchTag(napi_env env, napi_callback_info info)
135 {
136 napi_value napiValue = nullptr;
137 const char *securityPatchTag = GetSecurityPatchTag();
138
139 NAPI_CALL(env, napi_create_string_utf8(env, securityPatchTag, strlen(securityPatchTag), &napiValue));
140 return napiValue;
141 }
142
GetDisplayVersion(napi_env env,napi_callback_info info)143 static napi_value GetDisplayVersion(napi_env env, napi_callback_info info)
144 {
145 napi_value napiValue = nullptr;
146 const char *productVersion = GetDisplayVersion();
147
148 NAPI_CALL(env, napi_create_string_utf8(env, productVersion, strlen(productVersion), &napiValue));
149 return napiValue;
150 }
151
GetIncrementalVersion(napi_env env,napi_callback_info info)152 static napi_value GetIncrementalVersion(napi_env env, napi_callback_info info)
153 {
154 napi_value napiValue = nullptr;
155 const char *incrementalVersion = GetIncrementalVersion();
156
157 NAPI_CALL(env, napi_create_string_utf8(env, incrementalVersion, strlen(incrementalVersion), &napiValue));
158 return napiValue;
159 }
160
GetOsReleaseType(napi_env env,napi_callback_info info)161 static napi_value GetOsReleaseType(napi_env env, napi_callback_info info)
162 {
163 napi_value napiValue = nullptr;
164 const char *osReleaseType = GetOsReleaseType();
165
166 NAPI_CALL(env, napi_create_string_utf8(env, osReleaseType, strlen(osReleaseType), &napiValue));
167 return napiValue;
168 }
169
GetOSFullName(napi_env env,napi_callback_info info)170 static napi_value GetOSFullName(napi_env env, napi_callback_info info)
171 {
172 napi_value napiValue = nullptr;
173 const char *osVersion = GetOSFullName();
174
175 NAPI_CALL(env, napi_create_string_utf8(env, osVersion, strlen(osVersion), &napiValue));
176 return napiValue;
177 }
178
GetMajorVersion(napi_env env,napi_callback_info info)179 static napi_value GetMajorVersion(napi_env env, napi_callback_info info)
180 {
181 napi_value napiValue = nullptr;
182 int majorVersion = GetMajorVersion();
183
184 NAPI_CALL(env, napi_create_int32(env, majorVersion, &napiValue));
185 return napiValue;
186 }
187
GetSeniorVersion(napi_env env,napi_callback_info info)188 static napi_value GetSeniorVersion(napi_env env, napi_callback_info info)
189 {
190 napi_value napiValue = nullptr;
191 int seniorVersion = GetSeniorVersion();
192
193 NAPI_CALL(env, napi_create_int32(env, seniorVersion, &napiValue));
194 return napiValue;
195 }
196
GetFeatureVersion(napi_env env,napi_callback_info info)197 static napi_value GetFeatureVersion(napi_env env, napi_callback_info info)
198 {
199 napi_value napiValue = nullptr;
200 int featureVersion = GetFeatureVersion();
201
202 NAPI_CALL(env, napi_create_int32(env, featureVersion, &napiValue));
203 return napiValue;
204 }
205
GetBuildVersion(napi_env env,napi_callback_info info)206 static napi_value GetBuildVersion(napi_env env, napi_callback_info info)
207 {
208 napi_value napiValue = nullptr;
209 int buildVersion = GetBuildVersion();
210
211 NAPI_CALL(env, napi_create_int32(env, buildVersion, &napiValue));
212 return napiValue;
213 }
214
GetSdkApiVersion(napi_env env,napi_callback_info info)215 static napi_value GetSdkApiVersion(napi_env env, napi_callback_info info)
216 {
217 napi_value napiValue = nullptr;
218 int sdkApiVersion = GetSdkApiVersion();
219
220 NAPI_CALL(env, napi_create_int32(env, sdkApiVersion, &napiValue));
221 return napiValue;
222 }
223
GetFirstApiVersion(napi_env env,napi_callback_info info)224 static napi_value GetFirstApiVersion(napi_env env, napi_callback_info info)
225 {
226 napi_value napiValue = nullptr;
227 int firstApiVersion = GetFirstApiVersion();
228
229 NAPI_CALL(env, napi_create_int32(env, firstApiVersion, &napiValue));
230 return napiValue;
231 }
232
GetVersionId(napi_env env,napi_callback_info info)233 static napi_value GetVersionId(napi_env env, napi_callback_info info)
234 {
235 napi_value napiValue = nullptr;
236 const char *versionId = GetVersionId();
237
238 NAPI_CALL(env, napi_create_string_utf8(env, versionId, strlen(versionId), &napiValue));
239 return napiValue;
240 }
241
GetBuildType(napi_env env,napi_callback_info info)242 static napi_value GetBuildType(napi_env env, napi_callback_info info)
243 {
244 napi_value napiValue = nullptr;
245 const char *buildType = GetBuildType();
246
247 NAPI_CALL(env, napi_create_string_utf8(env, buildType, strlen(buildType), &napiValue));
248 return napiValue;
249 }
250
GetBuildUser(napi_env env,napi_callback_info info)251 static napi_value GetBuildUser(napi_env env, napi_callback_info info)
252 {
253 napi_value napiValue = nullptr;
254 const char *buildUser = GetBuildUser();
255
256 NAPI_CALL(env, napi_create_string_utf8(env, buildUser, strlen(buildUser), &napiValue));
257 return napiValue;
258 }
259
GetBuildHost(napi_env env,napi_callback_info info)260 static napi_value GetBuildHost(napi_env env, napi_callback_info info)
261 {
262 napi_value napiValue = nullptr;
263 const char *buildHost = GetBuildHost();
264
265 NAPI_CALL(env, napi_create_string_utf8(env, buildHost, strlen(buildHost), &napiValue));
266 return napiValue;
267 }
268
GetBuildTime(napi_env env,napi_callback_info info)269 static napi_value GetBuildTime(napi_env env, napi_callback_info info)
270 {
271 napi_value napiValue = nullptr;
272 const char *buildTime = GetBuildTime();
273
274 NAPI_CALL(env, napi_create_string_utf8(env, buildTime, strlen(buildTime), &napiValue));
275 return napiValue;
276 }
277
GetBuildRootHash(napi_env env,napi_callback_info info)278 static napi_value GetBuildRootHash(napi_env env, napi_callback_info info)
279 {
280 napi_value napiValue = nullptr;
281 const char *buildRootHash = GetBuildRootHash();
282
283 NAPI_CALL(env, napi_create_string_utf8(env, buildRootHash, strlen(buildRootHash), &napiValue));
284 return napiValue;
285 }
286
GetDevUdid(napi_env env,napi_callback_info info)287 static napi_value GetDevUdid(napi_env env, napi_callback_info info)
288 {
289 napi_value napiValue = nullptr;
290 char localDeviceId[UDID_LEN] = {0};
291 AclGetDevUdid(localDeviceId, UDID_LEN);
292
293 NAPI_CALL(env, napi_create_string_utf8(env, localDeviceId, strlen(localDeviceId), &napiValue));
294 return napiValue;
295 }
296
297 EXTERN_C_START
298 /*
299 * Module init
300 */
Init(napi_env env,napi_value exports)301 static napi_value Init(napi_env env, napi_value exports)
302 {
303 /*
304 * Attribute definition
305 */
306 napi_property_descriptor desc[] = {
307 {"deviceType", nullptr, nullptr, GetDeviceType, nullptr, nullptr, napi_default, nullptr},
308 {"manufacture", nullptr, nullptr, GetManufacture, nullptr, nullptr, napi_default, nullptr},
309 {"brand", nullptr, nullptr, GetBrand, nullptr, nullptr, napi_default, nullptr},
310 {"marketName", nullptr, nullptr, GetMarketName, nullptr, nullptr, napi_default, nullptr},
311 {"productSeries", nullptr, nullptr, GetProductSeries, nullptr, nullptr, napi_default, nullptr},
312 {"productModel", nullptr, nullptr, GetProductModel, nullptr, nullptr, napi_default, nullptr},
313 {"softwareModel", nullptr, nullptr, GetSoftwareModel, nullptr, nullptr, napi_default, nullptr},
314 {"hardwareModel", nullptr, nullptr, GetHardwareModel, nullptr, nullptr, napi_default, nullptr},
315 {"hardwareProfile", nullptr, nullptr, GetHardwareProfile, nullptr, nullptr, napi_default, nullptr},
316 {"serial", nullptr, nullptr, GetSerial, nullptr, nullptr, napi_default, nullptr},
317 {"bootloaderVersion", nullptr, nullptr, GetBootloaderVersion, nullptr, nullptr, napi_default, nullptr},
318 {"abiList", nullptr, nullptr, GetAbiList, nullptr, nullptr, napi_default, nullptr},
319 {"securityPatchTag", nullptr, nullptr, GetSecurityPatchTag, nullptr, nullptr, napi_default, nullptr},
320 {"displayVersion", nullptr, nullptr, GetDisplayVersion, nullptr, nullptr, napi_default, nullptr},
321 {"incrementalVersion", nullptr, nullptr, GetIncrementalVersion, nullptr, nullptr, napi_default, nullptr},
322 {"osReleaseType", nullptr, nullptr, GetOsReleaseType, nullptr, nullptr, napi_default, nullptr},
323 {"osFullName", nullptr, nullptr, GetOSFullName, nullptr, nullptr, napi_default, nullptr},
324 {"majorVersion", nullptr, nullptr, GetMajorVersion, nullptr, nullptr, napi_default, nullptr},
325 {"seniorVersion", nullptr, nullptr, GetSeniorVersion, nullptr, nullptr, napi_default, nullptr},
326 {"featureVersion", nullptr, nullptr, GetFeatureVersion, nullptr, nullptr, napi_default, nullptr},
327 {"buildVersion", nullptr, nullptr, GetBuildVersion, nullptr, nullptr, napi_default, nullptr},
328 {"sdkApiVersion", nullptr, nullptr, GetSdkApiVersion, nullptr, nullptr, napi_default, nullptr},
329 {"firstApiVersion", nullptr, nullptr, GetFirstApiVersion, nullptr, nullptr, napi_default, nullptr},
330 {"versionId", nullptr, nullptr, GetVersionId, nullptr, nullptr, napi_default, nullptr},
331 {"buildType", nullptr, nullptr, GetBuildType, nullptr, nullptr, napi_default, nullptr},
332 {"buildUser", nullptr, nullptr, GetBuildUser, nullptr, nullptr, napi_default, nullptr},
333 {"buildHost", nullptr, nullptr, GetBuildHost, nullptr, nullptr, napi_default, nullptr},
334 {"buildTime", nullptr, nullptr, GetBuildTime, nullptr, nullptr, napi_default, nullptr},
335 {"buildRootHash", nullptr, nullptr, GetBuildRootHash, nullptr, nullptr, napi_default, nullptr},
336 {"udid", nullptr, nullptr, GetDevUdid, nullptr, nullptr, napi_default, nullptr},
337 };
338 NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(napi_property_descriptor), desc));
339
340 return exports;
341 }
342 EXTERN_C_END
343
344 /*
345 * Module definition
346 */
347 static napi_module _module = {
348 .nm_version = 1,
349 .nm_flags = 0,
350 .nm_filename = NULL,
351 .nm_register_func = Init,
352 .nm_modname = "deviceInfo",
353 .nm_priv = ((void *)0),
354 .reserved = { 0 }
355 };
356 /*
357 * Module registration function
358 */
RegisterModule(void)359 extern "C" __attribute__((constructor)) void RegisterModule(void)
360 {
361 napi_module_register(&_module);
362 }
363