• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 "ohos.deviceInfo.proj.hpp"
16 #include "ohos.deviceInfo.impl.hpp"
17 #include "taihe/runtime.hpp"
18 #include "stdexcept"
19 
20 #include "beget_ext.h"
21 #include "parameter.h"
22 #include "sysversion.h"
23 #include "bundlemgr/bundle_mgr_proxy.h"
24 #include "iservice_registry.h"
25 #include "if_system_ability_manager.h"
26 #include "system_ability_definition.h"
27 #include "init_error.h"
28 #include "securec.h"
29 
30 #ifndef DEVICEINFO_JS_DOMAIN
31 #define DEVICEINFO_JS_DOMAIN (BASE_DOMAIN + 8)
32 #endif
33 
34 #ifndef DINFO_TAG
35 #define DINFO_TAG "DEVICEINFO_JS"
36 #endif
37 
38 #define DEVINFO_LOGV(fmt, ...) STARTUP_LOGV(DEVICEINFO_JS_DOMAIN, DINFO_TAG, fmt, ##__VA_ARGS__)
39 #define DEVINFO_LOGI(fmt, ...) STARTUP_LOGI(DEVICEINFO_JS_DOMAIN, DINFO_TAG, fmt, ##__VA_ARGS__)
40 #define DEVINFO_LOGW(fmt, ...) STARTUP_LOGW(DEVICEINFO_JS_DOMAIN, DINFO_TAG, fmt, ##__VA_ARGS__)
41 #define DEVINFO_LOGE(fmt, ...) STARTUP_LOGE(DEVICEINFO_JS_DOMAIN, DINFO_TAG, fmt, ##__VA_ARGS__)
42 
43 constexpr int UDID_LEN = 65;
44 constexpr int ODID_LEN = 37;
45 constexpr int DISK_SN_LEN = 20;
46 
47 typedef enum {
48     DEV_INFO_OK,
49     DEV_INFO_ENULLPTR,
50     DEV_INFO_EGETODID,
51     DEV_INFO_ESTRCOPY
52 } DevInfoError;
53 
54 using namespace taihe;
55 // using namespace ohos::deviceInfo;
56 
57 namespace {
58 // To be implemented.
59 
60 class deviceInfoImpl {
61 public:
deviceInfoImpl()62     deviceInfoImpl() {
63         // Don't forget to implement the constructor.
64     }
65 };
66 
getbrand()67 string getbrand()
68 {
69     const char *value = GetBrand();
70     if (value == nullptr) {
71         value = "";
72     }
73     return value;
74 }
75 
getdeviceType()76 string getdeviceType()
77 {
78     const char *value = GetDeviceType();
79     if (value == nullptr) {
80         value = "";
81     }
82     return value;
83 }
84 
getproductSeries()85 string getproductSeries()
86 {
87     const char *value = GetProductSeries();
88     if (value == nullptr) {
89         value = "";
90     }
91     return value;
92 }
93 
getproductModel()94 string getproductModel()
95 {
96     const char *value = GetProductModel();
97     if (value == nullptr) {
98         value = "";
99     }
100     return value;
101 }
102 
AclGetDevOdid(char * odid,int size)103 static DevInfoError AclGetDevOdid(char *odid, int size)
104 {
105     DevInfoError ret = DEV_INFO_OK;
106     if (odid[0] != '\0') {
107         return DEV_INFO_OK;
108     }
109     auto systemAbilityManager = OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
110     if (!systemAbilityManager) {
111         return DEV_INFO_ENULLPTR;
112     }
113 
114     auto remoteObject = systemAbilityManager->GetSystemAbility(OHOS::BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
115     if (!remoteObject) {
116         return DEV_INFO_ENULLPTR;
117     }
118 
119 #ifdef DEPENDENT_APPEXECFWK_BASE
120     auto bundleMgrProxy = OHOS::iface_cast<OHOS::AppExecFwk::BundleMgrProxy>(remoteObject);
121     if (!bundleMgrProxy) {
122         return DEV_INFO_ENULLPTR;
123     }
124 
125     std::string odidStr;
126     if (bundleMgrProxy->GetOdid(odidStr) != 0) {
127         return DEV_INFO_EGETODID;
128     }
129 
130     if (strcpy_s(odid, size, odidStr.c_str()) != EOK) {
131         return DEV_INFO_ESTRCOPY;
132     }
133 #else
134     DEVINFO_LOGE("DEPENDENT_APPEXECFWK_BASE does not exist, The ODID could not be obtained");
135     ret = DEV_INFO_EGETODID;
136 #endif
137 
138     return ret;
139 }
140 
getODID()141 string getODID()
142 {
143     static char value[ODID_LEN] = {0};
144     DevInfoError ret = AclGetDevOdid(value, ODID_LEN);
145     if (ret != DEV_INFO_OK) {
146         DEVINFO_LOGE("GetDevOdid ret:%d", ret);
147     }
148     return value;
149 }
150 
getudid()151 string getudid()
152 {
153     char value[UDID_LEN] = {0};
154     AclGetDevUdid(value, UDID_LEN);
155     return value;
156 }
157 
getserial()158 string getserial()
159 {
160     const char *value = AclGetSerial();
161     if (value == nullptr) {
162         value = "";
163     }
164     return value;
165 }
166 
getmanufacture()167 string getmanufacture()
168 {
169     const char *value = GetManufacture();
170     if (value == nullptr) {
171         value = "";
172     }
173     return value;
174 }
175 
getmarketName()176 string getmarketName()
177 {
178     const char *value = GetMarketName();
179     if (value == nullptr) {
180         value = "";
181     }
182     return value;
183 }
184 
getproductModelAlias()185 string getproductModelAlias()
186 {
187     const char *value = GetProductModelAlias();
188     if (value == nullptr) {
189         value = "";
190     }
191     return value;
192 }
193 
getsoftwareModel()194 string getsoftwareModel()
195 {
196     const char *value = GetSoftwareModel();
197     if (value == nullptr) {
198         value = "";
199     }
200     return value;
201 }
202 
gethardwareModel()203 string gethardwareModel()
204 {
205     const char *value = GetHardwareModel();
206     if (value == nullptr) {
207         value = "";
208     }
209     return value;
210 }
211 
getbootloaderVersion()212 string getbootloaderVersion()
213 {
214     const char *value = GetBootloaderVersion();
215     if (value == nullptr) {
216         value = "";
217     }
218     return value;
219 }
220 
getabiList()221 string getabiList()
222 {
223     const char *value = GetAbiList();
224     if (value == nullptr) {
225         value = "";
226     }
227     return value;
228 }
229 
getsecurityPatchTag()230 string getsecurityPatchTag()
231 {
232     const char *value = GetSecurityPatchTag();
233     if (value == nullptr) {
234         value = "";
235     }
236     return value;
237 }
238 
getdisplayVersion()239 string getdisplayVersion()
240 {
241     const char *value = GetDisplayVersion();
242     if (value == nullptr) {
243         value = "";
244     }
245     return value;
246 }
247 
getincrementalVersion()248 string getincrementalVersion()
249 {
250     const char *value = GetIncrementalVersion();
251     if (value == nullptr) {
252         value = "";
253     }
254     return value;
255 }
256 
getosReleaseType()257 string getosReleaseType()
258 {
259     const char *value = GetOsReleaseType();
260     if (value == nullptr) {
261         value = "";
262     }
263     return value;
264 }
265 
getosFullName()266 string getosFullName()
267 {
268     const char *value = GetOSFullName();
269     if (value == nullptr) {
270         value = "";
271     }
272     return value;
273 }
274 
getversionId()275 string getversionId()
276 {
277     const char *value = GetVersionId();
278     if (value == nullptr) {
279         value = "";
280     }
281     return value;
282 }
283 
getbuildType()284 string getbuildType()
285 {
286     const char *value = GetBuildType();
287     if (value == nullptr) {
288         value = "";
289     }
290     return value;
291 }
292 
getbuildUser()293 string getbuildUser()
294 {
295     const char *value = GetBuildUser();
296     if (value == nullptr) {
297         value = "";
298     }
299     return value;
300 }
301 
getbuildHost()302 string getbuildHost()
303 {
304     const char *value = GetBuildHost();
305     if (value == nullptr) {
306         value = "";
307     }
308     return value;
309 }
310 
getbuildTime()311 string getbuildTime()
312 {
313     const char *value = GetBuildTime();
314     if (value == nullptr) {
315         value = "";
316     }
317     return value;
318 }
319 
getbuildRootHash()320 string getbuildRootHash()
321 {
322     const char *value = GetBuildRootHash();
323     if (value == nullptr) {
324         value = "";
325     }
326     return value;
327 }
328 
getdistributionOSName()329 string getdistributionOSName()
330 {
331     const char *value = GetDistributionOSName();
332     if (value == nullptr) {
333         value = "";
334     }
335     return value;
336 }
337 
getdistributionOSVersion()338 string getdistributionOSVersion()
339 {
340     const char *value = GetDistributionOSVersion();
341     if (value == nullptr) {
342         value = "";
343     }
344     return value;
345 }
346 
getdistributionOSApiName()347 string getdistributionOSApiName()
348 {
349     const char *value = GetDistributionOSApiName();
350     if (value == nullptr) {
351         value = "";
352     }
353     return value;
354 }
355 
getdistributionOSReleaseType()356 string getdistributionOSReleaseType()
357 {
358     const char *value = GetDistributionOSReleaseType();
359     if (value == nullptr) {
360         value = "";
361     }
362     return value;
363 }
364 
getdiskSN()365 string getdiskSN()
366 {
367     static char value[DISK_SN_LEN] = {0};
368     AclGetDiskSN(value, DISK_SN_LEN);
369     return value;
370 }
371 
getsdkApiVersion()372 int32_t getsdkApiVersion()
373 {
374     int value = GetSdkApiVersion();
375     return value;
376 }
377 
getmajorVersion()378 int32_t getmajorVersion()
379 {
380     int value = GetMajorVersion();
381     return value;
382 }
383 
getseniorVersion()384 int32_t getseniorVersion()
385 {
386     int value = GetSeniorVersion();
387     return value;
388 }
389 
getfeatureVersion()390 int32_t getfeatureVersion()
391 {
392     int value = GetFeatureVersion();
393     return value;
394 }
395 
getbuildVersion()396 int32_t getbuildVersion()
397 {
398     int value = GetBuildVersion();
399     return value;
400 }
401 
getfirstApiVersion()402 int32_t getfirstApiVersion()
403 {
404     int value = GetFirstApiVersion();
405     return value;
406 }
407 
getdistributionOSApiVersion()408 int32_t getdistributionOSApiVersion()
409 {
410     int value = GetDistributionOSApiVersion();
411     return value;
412 }
413 }  // namespace
414 
415 TH_EXPORT_CPP_API_getbrand(getbrand);
416 TH_EXPORT_CPP_API_getdeviceType(getdeviceType);
417 TH_EXPORT_CPP_API_getproductSeries(getproductSeries);
418 TH_EXPORT_CPP_API_getproductModel(getproductModel);
419 TH_EXPORT_CPP_API_getODID(getODID);
420 TH_EXPORT_CPP_API_getudid(getudid);
421 TH_EXPORT_CPP_API_getserial(getserial);
422 TH_EXPORT_CPP_API_getmanufacture(getmanufacture);
423 TH_EXPORT_CPP_API_getmarketName(getmarketName);
424 TH_EXPORT_CPP_API_getproductModelAlias(getproductModelAlias);
425 TH_EXPORT_CPP_API_getsoftwareModel(getsoftwareModel);
426 TH_EXPORT_CPP_API_gethardwareModel(gethardwareModel);
427 TH_EXPORT_CPP_API_getbootloaderVersion(getbootloaderVersion);
428 TH_EXPORT_CPP_API_getabiList(getabiList);
429 TH_EXPORT_CPP_API_getsecurityPatchTag(getsecurityPatchTag);
430 TH_EXPORT_CPP_API_getdisplayVersion(getdisplayVersion);
431 TH_EXPORT_CPP_API_getincrementalVersion(getincrementalVersion);
432 TH_EXPORT_CPP_API_getosReleaseType(getosReleaseType);
433 TH_EXPORT_CPP_API_getosFullName(getosFullName);
434 TH_EXPORT_CPP_API_getversionId(getversionId);
435 TH_EXPORT_CPP_API_getbuildType(getbuildType);
436 TH_EXPORT_CPP_API_getbuildUser(getbuildUser);
437 TH_EXPORT_CPP_API_getbuildHost(getbuildHost);
438 TH_EXPORT_CPP_API_getbuildTime(getbuildTime);
439 TH_EXPORT_CPP_API_getbuildRootHash(getbuildRootHash);
440 TH_EXPORT_CPP_API_getdistributionOSName(getdistributionOSName);
441 TH_EXPORT_CPP_API_getdistributionOSVersion(getdistributionOSVersion);
442 TH_EXPORT_CPP_API_getdistributionOSApiName(getdistributionOSApiName);
443 TH_EXPORT_CPP_API_getdiskSN(getdiskSN);
444 TH_EXPORT_CPP_API_getdistributionOSReleaseType(getdistributionOSReleaseType);
445 TH_EXPORT_CPP_API_getsdkApiVersion(getsdkApiVersion);
446 TH_EXPORT_CPP_API_getmajorVersion(getmajorVersion);
447 TH_EXPORT_CPP_API_getseniorVersion(getseniorVersion);
448 TH_EXPORT_CPP_API_getfeatureVersion(getfeatureVersion);
449 TH_EXPORT_CPP_API_getbuildVersion(getbuildVersion);
450 TH_EXPORT_CPP_API_getfirstApiVersion(getfirstApiVersion);
451 TH_EXPORT_CPP_API_getdistributionOSApiVersion(getdistributionOSApiVersion);
452