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 const std::vector<std::string> PRODUCT_NAME_PREFIXS {"485541574549", "687561776569", "e58d8ee4b8ba"};
45 }
46
ConvertToProfile(DeviceProfile & profile)47 bool SystemInfoCollector::ConvertToProfile(DeviceProfile& profile)
48 {
49 profile.SetOsType(GetOsType());
50 profile.SetOsVersion(GetOsVersion());
51 profile.SetDeviceName(GetDeviceName());
52 profile.SetProductId(GetProductId());
53 profile.SetSubProductId(GetSubProductId());
54 profile.SetSn(GetSn());
55 profile.SetDeviceModel(GetDeviceModel());
56 profile.SetDevType(GetDeviceTypeId());
57 profile.SetManu(GetDeviceManufacturer());
58 profile.SetHiv(HIV_VERSION);
59 profile.SetProtType(GetProtType());
60 profile.SetProductName(GetProductName());
61 return true;
62 }
63
GetOsType()64 int32_t SystemInfoCollector::GetOsType()
65 {
66 char apiVersion[API_VERSION_LEN + 1] = {0};
67 GetParameter(OHOS_API_VERSION, UNDEFINED_VALUE, apiVersion, API_VERSION_LEN);
68 char bootSN[SN_LEN + 1] = {0};
69 GetParameter(OHOS_BOOT_SN, UNDEFINED_VALUE, bootSN, SN_LEN);
70 char osFullName[FULL_NAME_LEN + 1] = {0};
71 GetParameter(OHOS_FULL_NAME, UNDEFINED_VALUE, osFullName, FULL_NAME_LEN);
72 if (strcmp(apiVersion, UNDEFINED_VALUE) != 0 || strcmp(bootSN, UNDEFINED_VALUE) != 0 ||
73 strcmp(osFullName, UNDEFINED_VALUE) != 0) {
74 HILOGI("apiVersion: %{public}s bootSN: %{public}s osFullName: %{public}s",
75 ProfileUtils::GetAnonyString(std::string(apiVersion)).c_str(),
76 ProfileUtils::GetAnonyString(std::string(bootSN)).c_str(),
77 ProfileUtils::GetAnonyString(std::string(osFullName)).c_str());
78 return OHOS_TYPE;
79 }
80 char versionSDK[VERSION_SDK_LEN + 1] = {0};
81 GetParameter(VERSION_SDK, UNDEFINED_VALUE, versionSDK, VERSION_SDK_LEN);
82 if (strcmp(versionSDK, UNDEFINED_VALUE) != 0) {
83 HILOGI("versionSDK: %{public}s", ProfileUtils::GetAnonyString(std::string(versionSDK)).c_str());
84 return HO_OS_TYPE;
85 }
86 HILOGE("GetOsType fail!");
87 return OHOS_TYPE_UNKNOWN;
88 }
89
GetOsVersion()90 std::string SystemInfoCollector::GetOsVersion()
91 {
92 return DistributedDeviceProfile::ContentSensorManagerUtils::GetInstance().ObtainDisplayVersion();
93 }
94
GetDeviceName()95 std::string SystemInfoCollector::GetDeviceName()
96 {
97 std::string deviceName = "";
98 SettingsDataManager::GetInstance().GetUserDefinedDeviceName(
99 MultiUserManager::GetInstance().GetCurrentForegroundUserID(), deviceName);
100 if (deviceName.empty()) {
101 deviceName = GetProductName();
102 }
103 HILOGI("deviceName : %{public}s", ProfileUtils::GetAnonyString(deviceName).c_str());
104 return deviceName;
105 }
106
GetProductId()107 std::string SystemInfoCollector::GetProductId()
108 {
109 return DistributedDeviceProfile::ContentSensorManagerUtils::GetInstance().ObtainProductId();
110 }
111
GetProductName()112 std::string SystemInfoCollector::GetProductName()
113 {
114 std::string productName = ContentSensorManagerUtils::GetInstance().ObtainMarketName();
115 for (const auto &item : PRODUCT_NAME_PREFIXS) {
116 productName = TrimStr(ReplaceStr(productName,
117 DistributedDeviceProfile::ContentSensorManagerUtils::GetInstance().DecodeHexStr(item), ""));
118 }
119 HILOGI("productName : %{public}s", ProfileUtils::GetAnonyString(productName).c_str());
120 return productName;
121 }
122
GetSubProductId()123 std::string SystemInfoCollector::GetSubProductId()
124 {
125 return DistributedDeviceProfile::ContentSensorManagerUtils::GetInstance().GetSubProductId();
126 }
127
GetSn()128 std::string SystemInfoCollector::GetSn()
129 {
130 return DistributedDeviceProfile::ContentSensorManagerUtils::GetInstance().ObtainSerial();
131 }
132
GetDeviceModel()133 std::string SystemInfoCollector::GetDeviceModel()
134 {
135 return DistributedDeviceProfile::ContentSensorManagerUtils::GetInstance().ObtainProductModel();
136 }
137
GetDevType()138 std::string SystemInfoCollector::GetDevType()
139 {
140 return DistributedDeviceProfile::ContentSensorManagerUtils::GetInstance().ObtainDeviceType();
141 }
142
GetDeviceManufacturer()143 std::string SystemInfoCollector::GetDeviceManufacturer()
144 {
145 return DistributedDeviceProfile::ContentSensorManagerUtils::GetInstance().ObtainManufacture();
146 }
147
GetProtType()148 int32_t SystemInfoCollector::GetProtType()
149 {
150 return DistributedDeviceProfile::ContentSensorManagerUtils::GetInstance().GetProtType();
151 }
152
GetDeviceTypeId()153 std::string SystemInfoCollector::GetDeviceTypeId()
154 {
155 return DistributedDeviceProfile::ContentSensorManagerUtils::GetInstance().ObtainDeviceTypeId();
156 }
157 } // namespace DeviceProfile
158 } // namespace OHOS
159