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
21 #include "content_sensor_manager_utils.h"
22 #include "device_profile.h"
23 #include "distributed_device_profile_constants.h"
24 #include "distributed_device_profile_log.h"
25 #include "profile_utils.h"
26
27 namespace OHOS {
28 namespace DistributedDeviceProfile {
29 namespace {
30 const std::string TAG = "SystemInfoCollector";
31 const char* OHOS_API_VERSION = "const.ohos.apiversion";
32 const char* OHOS_BOOT_SN = "ohos.boot.sn";
33 const char* OHOS_FULL_NAME = "const.ohos.fullname";
34 const char* VERSION_SDK = "ro.build.version.sdk";
35 const char* UNDEFINED_VALUE = "undefined";
36 constexpr int32_t HO_OS_TYPE = 11;
37 constexpr uint32_t API_VERSION_LEN = 10;
38 constexpr uint32_t SN_LEN = 32;
39 constexpr uint32_t FULL_NAME_LEN = 128;
40 constexpr uint32_t VERSION_SDK_LEN = 10;
41 }
42
ConvertToProfile(DeviceProfile & profile)43 bool SystemInfoCollector::ConvertToProfile(DeviceProfile& profile)
44 {
45 profile.SetOsType(GetOsType());
46 profile.SetOsVersion(GetOsVersion());
47 return true;
48 }
49
GetOsType()50 int32_t SystemInfoCollector::GetOsType()
51 {
52 char apiVersion[API_VERSION_LEN + 1] = {0};
53 GetParameter(OHOS_API_VERSION, UNDEFINED_VALUE, apiVersion, API_VERSION_LEN);
54 char bootSN[SN_LEN + 1] = {0};
55 GetParameter(OHOS_BOOT_SN, UNDEFINED_VALUE, bootSN, SN_LEN);
56 char osFullName[FULL_NAME_LEN + 1] = {0};
57 GetParameter(OHOS_FULL_NAME, UNDEFINED_VALUE, osFullName, FULL_NAME_LEN);
58 if (strcmp(apiVersion, UNDEFINED_VALUE) != 0 || strcmp(bootSN, UNDEFINED_VALUE) != 0 ||
59 strcmp(osFullName, UNDEFINED_VALUE) != 0) {
60 HILOGI("apiVersion: %{public}s bootSN: %{public}s osFullName: %{public}s",
61 ProfileUtils::GetAnonyString(std::string(apiVersion)).c_str(),
62 ProfileUtils::GetAnonyString(std::string(bootSN)).c_str(),
63 ProfileUtils::GetAnonyString(std::string(osFullName)).c_str());
64 return OHOS_TYPE;
65 }
66 char versionSDK[VERSION_SDK_LEN + 1] = {0};
67 GetParameter(VERSION_SDK, UNDEFINED_VALUE, versionSDK, VERSION_SDK_LEN);
68 if (strcmp(versionSDK, UNDEFINED_VALUE) != 0) {
69 HILOGI("versionSDK: %{public}s", ProfileUtils::GetAnonyString(std::string(versionSDK)).c_str());
70 return HO_OS_TYPE;
71 }
72 HILOGE("GetOsType fail!");
73 return OHOS_TYPE_UNKNOWN;
74 }
75
GetOsVersion()76 std::string SystemInfoCollector::GetOsVersion()
77 {
78 return DistributedDeviceProfile::ContentSensorManagerUtils::GetInstance().ObtainDisplayVersion();
79 }
80 } // namespace DeviceProfile
81 } // namespace OHOS