• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     if (value == nullptr) {
31         value = "";
32     }
33 
34     NAPI_CALL(env, napi_create_string_utf8(env, value, strlen(value), &deviceType));
35     return deviceType;
36 }
37 
GetManufacture(napi_env env,napi_callback_info info)38 static napi_value GetManufacture(napi_env env, napi_callback_info info)
39 {
40     napi_value napiValue = nullptr;
41     const char *manfactureName = GetManufacture();
42     if (manfactureName == nullptr) {
43         manfactureName = "";
44     }
45 
46     NAPI_CALL(env, napi_create_string_utf8(env, manfactureName, strlen(manfactureName), &napiValue));
47     return napiValue;
48 }
49 
GetBrand(napi_env env,napi_callback_info info)50 static napi_value GetBrand(napi_env env, napi_callback_info info)
51 {
52     napi_value napiValue = nullptr;
53     const char *productBrand = GetBrand();
54     if (productBrand == nullptr) {
55         productBrand = "";
56     }
57 
58     NAPI_CALL(env, napi_create_string_utf8(env, productBrand, strlen(productBrand), &napiValue));
59     return napiValue;
60 }
61 
GetMarketName(napi_env env,napi_callback_info info)62 static napi_value GetMarketName(napi_env env, napi_callback_info info)
63 {
64     napi_value napiValue = nullptr;
65     const char *marketName = GetMarketName();
66     if (marketName == nullptr) {
67         marketName = "";
68     }
69 
70     NAPI_CALL(env, napi_create_string_utf8(env, marketName, strlen(marketName), &napiValue));
71     return napiValue;
72 }
73 
GetProductSeries(napi_env env,napi_callback_info info)74 static napi_value GetProductSeries(napi_env env, napi_callback_info info)
75 {
76     napi_value napiValue = nullptr;
77     const char *productSeries = GetProductSeries();
78     if (productSeries == nullptr) {
79         productSeries = "";
80     }
81 
82     NAPI_CALL(env, napi_create_string_utf8(env, productSeries, strlen(productSeries), &napiValue));
83     return napiValue;
84 }
85 
GetProductModel(napi_env env,napi_callback_info info)86 static napi_value GetProductModel(napi_env env, napi_callback_info info)
87 {
88     napi_value napiValue = nullptr;
89     const char *productModel = GetProductModel();
90     if (productModel == nullptr) {
91         productModel = "";
92     }
93 
94     NAPI_CALL(env, napi_create_string_utf8(env, productModel, strlen(productModel), &napiValue));
95     return napiValue;
96 }
97 
GetSoftwareModel(napi_env env,napi_callback_info info)98 static napi_value GetSoftwareModel(napi_env env, napi_callback_info info)
99 {
100     napi_value napiValue = nullptr;
101     const char *softwareModel = GetSoftwareModel();
102     if (softwareModel == nullptr) {
103         softwareModel = "";
104     }
105 
106     NAPI_CALL(env, napi_create_string_utf8(env, softwareModel, strlen(softwareModel), &napiValue));
107     return napiValue;
108 }
109 
GetHardwareModel(napi_env env,napi_callback_info info)110 static napi_value GetHardwareModel(napi_env env, napi_callback_info info)
111 {
112     napi_value napiValue = nullptr;
113     const char *hardwareModel = GetHardwareModel();
114     if (hardwareModel == nullptr) {
115         hardwareModel = "";
116     }
117 
118     NAPI_CALL(env, napi_create_string_utf8(env, hardwareModel, strlen(hardwareModel), &napiValue));
119     return napiValue;
120 }
121 
GetHardwareProfile(napi_env env,napi_callback_info info)122 static napi_value GetHardwareProfile(napi_env env, napi_callback_info info)
123 {
124     napi_value napiValue = nullptr;
125     const char *hardwareProfile = GetHardwareProfile();
126     if (hardwareProfile == nullptr) {
127         hardwareProfile = "";
128     }
129 
130     NAPI_CALL(env, napi_create_string_utf8(env, hardwareProfile, strlen(hardwareProfile), &napiValue));
131     return napiValue;
132 }
133 
GetSerial(napi_env env,napi_callback_info info)134 static napi_value GetSerial(napi_env env, napi_callback_info info)
135 {
136     napi_value napiValue = nullptr;
137     const char *serialNumber = AclGetSerial();
138     if (serialNumber == nullptr) {
139         serialNumber = "";
140     }
141 
142     NAPI_CALL(env, napi_create_string_utf8(env, serialNumber, strlen(serialNumber), &napiValue));
143     return napiValue;
144 }
145 
GetBootloaderVersion(napi_env env,napi_callback_info info)146 static napi_value GetBootloaderVersion(napi_env env, napi_callback_info info)
147 {
148     napi_value napiValue = nullptr;
149     const char *bootloaderVersion = GetBootloaderVersion();
150     if (bootloaderVersion == nullptr) {
151         bootloaderVersion = "";
152     }
153 
154     NAPI_CALL(env, napi_create_string_utf8(env, bootloaderVersion, strlen(bootloaderVersion), &napiValue));
155     return napiValue;
156 }
157 
GetAbiList(napi_env env,napi_callback_info info)158 static napi_value GetAbiList(napi_env env, napi_callback_info info)
159 {
160     napi_value napiValue = nullptr;
161     const char *abiList = GetAbiList();
162     if (abiList == nullptr) {
163         abiList = "";
164     }
165 
166     NAPI_CALL(env, napi_create_string_utf8(env, abiList, strlen(abiList), &napiValue));
167     return napiValue;
168 }
169 
GetSecurityPatchTag(napi_env env,napi_callback_info info)170 static napi_value GetSecurityPatchTag(napi_env env, napi_callback_info info)
171 {
172     napi_value napiValue = nullptr;
173     const char *securityPatchTag = GetSecurityPatchTag();
174     if (securityPatchTag == nullptr) {
175         securityPatchTag = "";
176     }
177 
178     NAPI_CALL(env, napi_create_string_utf8(env, securityPatchTag, strlen(securityPatchTag), &napiValue));
179     return napiValue;
180 }
181 
GetDisplayVersion(napi_env env,napi_callback_info info)182 static napi_value GetDisplayVersion(napi_env env, napi_callback_info info)
183 {
184     napi_value napiValue = nullptr;
185     const char *productVersion = GetDisplayVersion();
186     if (productVersion == nullptr) {
187         productVersion = "";
188     }
189 
190     NAPI_CALL(env, napi_create_string_utf8(env, productVersion, strlen(productVersion), &napiValue));
191     return napiValue;
192 }
193 
GetIncrementalVersion(napi_env env,napi_callback_info info)194 static napi_value GetIncrementalVersion(napi_env env, napi_callback_info info)
195 {
196     napi_value napiValue = nullptr;
197     const char *incrementalVersion = GetIncrementalVersion();
198     if (incrementalVersion == nullptr) {
199         incrementalVersion = "";
200     }
201 
202     NAPI_CALL(env, napi_create_string_utf8(env, incrementalVersion, strlen(incrementalVersion), &napiValue));
203     return napiValue;
204 }
205 
GetOsReleaseType(napi_env env,napi_callback_info info)206 static napi_value GetOsReleaseType(napi_env env, napi_callback_info info)
207 {
208     napi_value napiValue = nullptr;
209     const char *osReleaseType = GetOsReleaseType();
210     if (osReleaseType == nullptr) {
211         osReleaseType = "";
212     }
213 
214     NAPI_CALL(env, napi_create_string_utf8(env, osReleaseType, strlen(osReleaseType), &napiValue));
215     return napiValue;
216 }
217 
GetOSFullName(napi_env env,napi_callback_info info)218 static napi_value GetOSFullName(napi_env env, napi_callback_info info)
219 {
220     napi_value napiValue = nullptr;
221     const char *osVersion = GetOSFullName();
222     if (osVersion == nullptr) {
223         osVersion = "";
224     }
225 
226     NAPI_CALL(env, napi_create_string_utf8(env, osVersion, strlen(osVersion), &napiValue));
227     return napiValue;
228 }
229 
GetMajorVersion(napi_env env,napi_callback_info info)230 static napi_value GetMajorVersion(napi_env env, napi_callback_info info)
231 {
232     napi_value napiValue = nullptr;
233     int majorVersion = GetMajorVersion();
234 
235     NAPI_CALL(env, napi_create_int32(env, majorVersion, &napiValue));
236     return napiValue;
237 }
238 
GetSeniorVersion(napi_env env,napi_callback_info info)239 static napi_value GetSeniorVersion(napi_env env, napi_callback_info info)
240 {
241     napi_value napiValue = nullptr;
242     int seniorVersion = GetSeniorVersion();
243 
244     NAPI_CALL(env, napi_create_int32(env, seniorVersion, &napiValue));
245     return napiValue;
246 }
247 
GetFeatureVersion(napi_env env,napi_callback_info info)248 static napi_value GetFeatureVersion(napi_env env, napi_callback_info info)
249 {
250     napi_value napiValue = nullptr;
251     int featureVersion = GetFeatureVersion();
252 
253     NAPI_CALL(env, napi_create_int32(env, featureVersion, &napiValue));
254     return napiValue;
255 }
256 
GetBuildVersion(napi_env env,napi_callback_info info)257 static napi_value GetBuildVersion(napi_env env, napi_callback_info info)
258 {
259     napi_value napiValue = nullptr;
260     int buildVersion = GetBuildVersion();
261 
262     NAPI_CALL(env, napi_create_int32(env, buildVersion, &napiValue));
263     return napiValue;
264 }
265 
GetSdkApiVersion(napi_env env,napi_callback_info info)266 static napi_value GetSdkApiVersion(napi_env env, napi_callback_info info)
267 {
268     napi_value napiValue = nullptr;
269     int sdkApiVersion = GetSdkApiVersion();
270 
271     NAPI_CALL(env, napi_create_int32(env, sdkApiVersion, &napiValue));
272     return napiValue;
273 }
274 
GetFirstApiVersion(napi_env env,napi_callback_info info)275 static napi_value GetFirstApiVersion(napi_env env, napi_callback_info info)
276 {
277     napi_value napiValue = nullptr;
278     int firstApiVersion = GetFirstApiVersion();
279 
280     NAPI_CALL(env, napi_create_int32(env, firstApiVersion, &napiValue));
281     return napiValue;
282 }
283 
GetVersionId(napi_env env,napi_callback_info info)284 static napi_value GetVersionId(napi_env env, napi_callback_info info)
285 {
286     napi_value napiValue = nullptr;
287     const char *versionId = GetVersionId();
288     if (versionId == nullptr) {
289         versionId = "";
290     }
291 
292     NAPI_CALL(env, napi_create_string_utf8(env, versionId, strlen(versionId), &napiValue));
293     return napiValue;
294 }
295 
GetBuildType(napi_env env,napi_callback_info info)296 static napi_value GetBuildType(napi_env env, napi_callback_info info)
297 {
298     napi_value napiValue = nullptr;
299     const char *buildType = GetBuildType();
300     if (buildType == nullptr) {
301         buildType = "";
302     }
303 
304     NAPI_CALL(env, napi_create_string_utf8(env, buildType, strlen(buildType), &napiValue));
305     return napiValue;
306 }
307 
GetBuildUser(napi_env env,napi_callback_info info)308 static napi_value GetBuildUser(napi_env env, napi_callback_info info)
309 {
310     napi_value napiValue = nullptr;
311     const char *buildUser = GetBuildUser();
312     if (buildUser == nullptr) {
313         buildUser = "";
314     }
315 
316     NAPI_CALL(env, napi_create_string_utf8(env, buildUser, strlen(buildUser), &napiValue));
317     return napiValue;
318 }
319 
GetBuildHost(napi_env env,napi_callback_info info)320 static napi_value GetBuildHost(napi_env env, napi_callback_info info)
321 {
322     napi_value napiValue = nullptr;
323     const char *buildHost = GetBuildHost();
324     if (buildHost == nullptr) {
325         buildHost = "";
326     }
327 
328     NAPI_CALL(env, napi_create_string_utf8(env, buildHost, strlen(buildHost), &napiValue));
329     return napiValue;
330 }
331 
GetBuildTime(napi_env env,napi_callback_info info)332 static napi_value GetBuildTime(napi_env env, napi_callback_info info)
333 {
334     napi_value napiValue = nullptr;
335     const char *buildTime = GetBuildTime();
336     if (buildTime == nullptr) {
337         buildTime = "";
338     }
339 
340     NAPI_CALL(env, napi_create_string_utf8(env, buildTime, strlen(buildTime), &napiValue));
341     return napiValue;
342 }
343 
GetBuildRootHash(napi_env env,napi_callback_info info)344 static napi_value GetBuildRootHash(napi_env env, napi_callback_info info)
345 {
346     napi_value napiValue = nullptr;
347     const char *buildRootHash = GetBuildRootHash();
348     if (buildRootHash == nullptr) {
349         buildRootHash = "";
350     }
351 
352     NAPI_CALL(env, napi_create_string_utf8(env, buildRootHash, strlen(buildRootHash), &napiValue));
353     return napiValue;
354 }
355 
GetDevUdid(napi_env env,napi_callback_info info)356 static napi_value GetDevUdid(napi_env env, napi_callback_info info)
357 {
358     napi_value napiValue = nullptr;
359     char localDeviceId[UDID_LEN] = {0};
360     AclGetDevUdid(localDeviceId, UDID_LEN);
361 
362     NAPI_CALL(env, napi_create_string_utf8(env, localDeviceId, strlen(localDeviceId), &napiValue));
363     return napiValue;
364 }
365 
NAPI_GetDistributionOSName(napi_env env,napi_callback_info info)366 static napi_value NAPI_GetDistributionOSName(napi_env env, napi_callback_info info)
367 {
368     napi_value napiValue = nullptr;
369     const char *val = GetDistributionOSName();
370 
371     NAPI_CALL(env, napi_create_string_utf8(env, val, strlen(val), &napiValue));
372     return napiValue;
373 }
374 
NAPI_GetDistributionOSVersion(napi_env env,napi_callback_info info)375 static napi_value NAPI_GetDistributionOSVersion(napi_env env, napi_callback_info info)
376 {
377     napi_value napiValue = nullptr;
378     const char *val = GetDistributionOSVersion();
379 
380     NAPI_CALL(env, napi_create_string_utf8(env, val, strlen(val), &napiValue));
381     return napiValue;
382 }
383 
NAPI_GetDistributionOSApiVersion(napi_env env,napi_callback_info info)384 static napi_value NAPI_GetDistributionOSApiVersion(napi_env env, napi_callback_info info)
385 {
386     napi_value napiValue = nullptr;
387     int sdkApiVersion = GetDistributionOSApiVersion();
388 
389     NAPI_CALL(env, napi_create_int32(env, sdkApiVersion, &napiValue));
390     return napiValue;
391 }
392 
NAPI_GetDistributionOSReleaseType(napi_env env,napi_callback_info info)393 static napi_value NAPI_GetDistributionOSReleaseType(napi_env env, napi_callback_info info)
394 {
395     napi_value napiValue = nullptr;
396     const char *val = GetDistributionOSReleaseType();
397 
398     NAPI_CALL(env, napi_create_string_utf8(env, val, strlen(val), &napiValue));
399     return napiValue;
400 }
401 
402 EXTERN_C_START
403 /*
404  * Module init
405  */
Init(napi_env env,napi_value exports)406 static napi_value Init(napi_env env, napi_value exports)
407 {
408     /*
409      * Attribute definition
410      */
411     napi_property_descriptor desc[] = {
412         {"deviceType", nullptr, nullptr, GetDeviceType, nullptr, nullptr, napi_default, nullptr},
413         {"manufacture", nullptr, nullptr, GetManufacture, nullptr, nullptr, napi_default, nullptr},
414         {"brand", nullptr, nullptr, GetBrand, nullptr, nullptr, napi_default, nullptr},
415         {"marketName", nullptr, nullptr, GetMarketName, nullptr, nullptr, napi_default, nullptr},
416         {"productSeries", nullptr, nullptr, GetProductSeries, nullptr, nullptr, napi_default, nullptr},
417         {"productModel", nullptr, nullptr, GetProductModel, nullptr, nullptr, napi_default, nullptr},
418         {"softwareModel", nullptr, nullptr, GetSoftwareModel, nullptr, nullptr, napi_default, nullptr},
419         {"hardwareModel", nullptr, nullptr, GetHardwareModel, nullptr, nullptr, napi_default, nullptr},
420         {"hardwareProfile", nullptr, nullptr, GetHardwareProfile, nullptr, nullptr, napi_default, nullptr},
421         {"serial", nullptr, nullptr, GetSerial, nullptr, nullptr, napi_default, nullptr},
422         {"bootloaderVersion", nullptr, nullptr, GetBootloaderVersion, nullptr, nullptr, napi_default, nullptr},
423         {"abiList", nullptr, nullptr, GetAbiList, nullptr, nullptr, napi_default, nullptr},
424         {"securityPatchTag", nullptr, nullptr, GetSecurityPatchTag, nullptr, nullptr, napi_default, nullptr},
425         {"displayVersion", nullptr, nullptr, GetDisplayVersion, nullptr, nullptr, napi_default, nullptr},
426         {"incrementalVersion", nullptr, nullptr, GetIncrementalVersion, nullptr, nullptr, napi_default, nullptr},
427         {"osReleaseType", nullptr, nullptr, GetOsReleaseType, nullptr, nullptr, napi_default, nullptr},
428         {"osFullName", nullptr, nullptr, GetOSFullName, nullptr, nullptr, napi_default, nullptr},
429         {"majorVersion", nullptr, nullptr, GetMajorVersion, nullptr, nullptr, napi_default, nullptr},
430         {"seniorVersion", nullptr, nullptr, GetSeniorVersion, nullptr, nullptr, napi_default, nullptr},
431         {"featureVersion", nullptr, nullptr, GetFeatureVersion, nullptr, nullptr, napi_default, nullptr},
432         {"buildVersion", nullptr, nullptr, GetBuildVersion, nullptr, nullptr, napi_default, nullptr},
433         {"sdkApiVersion", nullptr, nullptr, GetSdkApiVersion, nullptr, nullptr, napi_default, nullptr},
434         {"firstApiVersion", nullptr, nullptr, GetFirstApiVersion, nullptr, nullptr, napi_default, nullptr},
435         {"versionId", nullptr, nullptr, GetVersionId, nullptr, nullptr, napi_default, nullptr},
436         {"buildType", nullptr, nullptr, GetBuildType, nullptr, nullptr, napi_default, nullptr},
437         {"buildUser", nullptr, nullptr, GetBuildUser, nullptr, nullptr, napi_default, nullptr},
438         {"buildHost", nullptr, nullptr, GetBuildHost, nullptr, nullptr, napi_default, nullptr},
439         {"buildTime", nullptr, nullptr, GetBuildTime, nullptr, nullptr, napi_default, nullptr},
440         {"buildRootHash", nullptr, nullptr, GetBuildRootHash, nullptr, nullptr, napi_default, nullptr},
441         {"udid", nullptr, nullptr, GetDevUdid, nullptr, nullptr, napi_default, nullptr},
442         {"distributionOSName", nullptr, nullptr, NAPI_GetDistributionOSName, nullptr, nullptr, napi_default, nullptr},
443         {"distributionOSVersion", nullptr, nullptr, NAPI_GetDistributionOSVersion, nullptr, nullptr, napi_default, nullptr},
444         {"distributionOSApiVersion", nullptr, nullptr, NAPI_GetDistributionOSApiVersion, nullptr, nullptr, napi_default, nullptr},
445         {"distributionOSReleaseType", nullptr, nullptr, NAPI_GetDistributionOSReleaseType, nullptr, nullptr, napi_default, nullptr},
446     };
447     NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(napi_property_descriptor), desc));
448 
449     return exports;
450 }
451 EXTERN_C_END
452 
453 /*
454  * Module definition
455  */
456 static napi_module _module = {
457     .nm_version = 1,
458     .nm_flags = 0,
459     .nm_filename = NULL,
460     .nm_register_func = Init,
461     .nm_modname = "deviceInfo",
462     .nm_priv = ((void *)0),
463     .reserved = { 0 }
464 };
465 /*
466  * Module registration function
467  */
RegisterModule(void)468 extern "C" __attribute__((constructor)) void RegisterModule(void)
469 {
470     napi_module_register(&_module);
471 }
472