• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 
16 #include "system_info_collector.h"
17 
18 #include "parameter.h"
19 #include "securec.h"
20 #include "string_ex.h"
21 
22 #include "content_sensor_manager_utils.h"
23 #include "device_profile.h"
24 #include "distributed_device_profile_constants.h"
25 #include "distributed_device_profile_log.h"
26 #include "multi_user_manager.h"
27 #include "profile_utils.h"
28 #include "settings_data_manager.h"
29 
30 namespace OHOS {
31 namespace DistributedDeviceProfile {
32 namespace {
33 const std::string TAG = "SystemInfoCollector";
34 const char* OHOS_API_VERSION = "const.ohos.apiversion";
35 const char* OHOS_BOOT_SN = "ohos.boot.sn";
36 const char* OHOS_FULL_NAME = "const.ohos.fullname";
37 const char* VERSION_SDK = "ro.build.version.sdk";
38 const char* UNDEFINED_VALUE = "undefined";
39 constexpr int32_t HO_OS_TYPE = 11;
40 constexpr uint32_t API_VERSION_LEN = 10;
41 constexpr uint32_t SN_LEN = 32;
42 constexpr uint32_t FULL_NAME_LEN = 128;
43 constexpr uint32_t VERSION_SDK_LEN = 10;
44 }
45 
ConvertToProfile(DeviceProfile & profile)46 bool SystemInfoCollector::ConvertToProfile(DeviceProfile& profile)
47 {
48     profile.SetOsType(GetOsType());
49     profile.SetOsVersion(GetOsVersion());
50     profile.SetDeviceName(GetDeviceName());
51     profile.SetProductId(GetProductId());
52     profile.SetSubProductId(GetSubProductId());
53     profile.SetSn(GetSn());
54     profile.SetDeviceModel(GetDeviceModel());
55     profile.SetDevType(GetDeviceTypeId());
56     profile.SetManu(GetDeviceManufacturer());
57     profile.SetHiv(HIV_VERSION);
58     profile.SetProtType(GetProtType());
59     profile.SetProductName(GetProductName());
60     return true;
61 }
62 
GetOsType()63 int32_t SystemInfoCollector::GetOsType()
64 {
65     char apiVersion[API_VERSION_LEN + 1] = {0};
66     GetParameter(OHOS_API_VERSION, UNDEFINED_VALUE, apiVersion, API_VERSION_LEN);
67     char bootSN[SN_LEN + 1] = {0};
68     GetParameter(OHOS_BOOT_SN, UNDEFINED_VALUE, bootSN, SN_LEN);
69     char osFullName[FULL_NAME_LEN + 1] = {0};
70     GetParameter(OHOS_FULL_NAME, UNDEFINED_VALUE, osFullName, FULL_NAME_LEN);
71     if (strcmp(apiVersion, UNDEFINED_VALUE) != 0 || strcmp(bootSN, UNDEFINED_VALUE) != 0 ||
72         strcmp(osFullName, UNDEFINED_VALUE) != 0) {
73         HILOGI("apiVersion: %{public}s bootSN: %{public}s osFullName: %{public}s",
74             ProfileUtils::GetAnonyString(std::string(apiVersion)).c_str(),
75             ProfileUtils::GetAnonyString(std::string(bootSN)).c_str(),
76             ProfileUtils::GetAnonyString(std::string(osFullName)).c_str());
77         return OHOS_TYPE;
78     }
79     char versionSDK[VERSION_SDK_LEN + 1] = {0};
80     GetParameter(VERSION_SDK, UNDEFINED_VALUE, versionSDK, VERSION_SDK_LEN);
81     if (strcmp(versionSDK, UNDEFINED_VALUE) != 0) {
82         HILOGI("versionSDK: %{public}s", ProfileUtils::GetAnonyString(std::string(versionSDK)).c_str());
83         return HO_OS_TYPE;
84     }
85     HILOGE("GetOsType fail!");
86     return OHOS_TYPE_UNKNOWN;
87 }
88 
GetOsVersion()89 std::string SystemInfoCollector::GetOsVersion()
90 {
91     return DistributedDeviceProfile::ContentSensorManagerUtils::GetInstance().ObtainDisplayVersion();
92 }
93 
GetDeviceName()94 std::string SystemInfoCollector::GetDeviceName()
95 {
96     std::string deviceName = "";
97     SettingsDataManager::GetInstance().GetUserDefinedDeviceName(
98         MultiUserManager::GetInstance().GetCurrentForegroundUserID(), deviceName);
99     if (deviceName.empty()) {
100         SettingsDataManager::GetInstance().GetDeviceName(deviceName);
101     }
102     return deviceName;
103 }
104 
GetProductId()105 std::string SystemInfoCollector::GetProductId()
106 {
107     return DistributedDeviceProfile::ContentSensorManagerUtils::GetInstance().ObtainProductId();
108 }
109 
GetProductName()110 std::string SystemInfoCollector::GetProductName()
111 {
112     std::string productName = "";
113     SettingsDataManager::GetInstance().GetDeviceName(productName);
114     return productName;
115 }
116 
GetSubProductId()117 std::string SystemInfoCollector::GetSubProductId()
118 {
119     return DistributedDeviceProfile::ContentSensorManagerUtils::GetInstance().GetSubProductId();
120 }
121 
GetSn()122 std::string SystemInfoCollector::GetSn()
123 {
124     return DistributedDeviceProfile::ContentSensorManagerUtils::GetInstance().ObtainSerial();
125 }
126 
GetDeviceModel()127 std::string SystemInfoCollector::GetDeviceModel()
128 {
129     return DistributedDeviceProfile::ContentSensorManagerUtils::GetInstance().ObtainProductModel();
130 }
131 
GetDevType()132 std::string SystemInfoCollector::GetDevType()
133 {
134     return DistributedDeviceProfile::ContentSensorManagerUtils::GetInstance().ObtainDeviceType();
135 }
136 
GetDeviceManufacturer()137 std::string SystemInfoCollector::GetDeviceManufacturer()
138 {
139     return DistributedDeviceProfile::ContentSensorManagerUtils::GetInstance().ObtainManufacture();
140 }
141 
GetProtType()142 int32_t SystemInfoCollector::GetProtType()
143 {
144     return DistributedDeviceProfile::ContentSensorManagerUtils::GetInstance().GetProtType();
145 }
146 
GetDeviceTypeId()147 std::string SystemInfoCollector::GetDeviceTypeId()
148 {
149     return DistributedDeviceProfile::ContentSensorManagerUtils::GetInstance().ObtainDeviceTypeId();
150 }
151 } // namespace DeviceProfile
152 } // namespace OHOS
153