• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 "device_profile_log.h"
19 #include "nlohmann/json.hpp"
20 #include "parameter.h"
21 #include "securec.h"
22 #include "content_sensor_manager_utils.h"
23 #include "distributed_device_profile_constants.h"
24 
25 namespace OHOS {
26 namespace DeviceProfile {
27 namespace {
28 const std::string TAG = "SystemInfoCollector";
29 
30 const std::string SERVICE_ID = "system";
31 const std::string SERVICE_TYPE = "system";
32 const std::string SYSTEM_OS_TYPE = "type";
33 const std::string DEVICE_OHOS_VERSION = "harmonyVersion";
34 const std::string DEVICE_API_LEVEL = "harmonyApiLevel";
35 const std::string DEVICE_OHOS_NAME = "OpenHarmony";
36 constexpr int32_t OHOS_TYPE_UNKNOWN = -1;
37 constexpr int32_t OHOS_TYPE = 10;
38 }
39 
ConvertToProfileData(ServiceCharacteristicProfile & profile)40 bool SystemInfoCollector::ConvertToProfileData(ServiceCharacteristicProfile& profile)
41 {
42     profile.SetServiceId(SERVICE_ID);
43     profile.SetServiceType(SERVICE_TYPE);
44     nlohmann::json jsonData;
45     jsonData[SYSTEM_OS_TYPE] = GetOsType();
46     jsonData[DEVICE_OHOS_VERSION] = GetOsVersion();
47     jsonData[DEVICE_API_LEVEL] = GetSdkApiVersion();
48     profile.SetCharacteristicProfileJson(jsonData.dump());
49     return true;
50 }
51 
GetOsType()52 int32_t SystemInfoCollector::GetOsType()
53 {
54     std::string osFullName = DistributedDeviceProfile::ContentSensorManagerUtils::GetInstance().ObtainOsFullName();
55     if (osFullName.empty() || osFullName.size() >= DistributedDeviceProfile::MAX_STRING_LEN) {
56         HILOGI("osFullName size is invalid!");
57         return OHOS_TYPE_UNKNOWN;
58     }
59     size_t found = osFullName.find(DEVICE_OHOS_NAME);
60     if (found != std::string::npos) {
61         HILOGI("osFullName is invalid!");
62         return OHOS_TYPE_UNKNOWN;
63     }
64     return OHOS_TYPE;
65 }
66 
GetOsVersion()67 std::string SystemInfoCollector::GetOsVersion()
68 {
69     return DistributedDeviceProfile::ContentSensorManagerUtils::GetInstance().ObtainDisplayVersion();
70 }
71 } // namespace DeviceProfile
72 } // namespace OHOS