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 23 namespace OHOS { 24 namespace DeviceProfile { 25 namespace { 26 const std::string TAG = "SystemInfoCollector"; 27 28 const std::string SERVICE_ID = "system"; 29 const std::string SERVICE_TYPE = "system"; 30 const std::string EMPTY_PARAM = ""; 31 const std::string SYSTEM_OS_TYPE = "type"; 32 const std::string DEVICE_OHOS_VERSION = "harmonyVersion"; 33 const std::string DEVICE_API_LEVEL = "harmonyApiLevel"; 34 const std::string DEVICE_OHOS_NAME = "OpenHarmony"; 35 constexpr int32_t OHOS_TYPE_UNKNOWN = -1; 36 constexpr int32_t OHOS_TYPE = 10; 37 } 38 ConvertToProfileData(ServiceCharacteristicProfile & profile)39bool SystemInfoCollector::ConvertToProfileData(ServiceCharacteristicProfile& profile) 40 { 41 profile.SetServiceId(SERVICE_ID); 42 profile.SetServiceType(SERVICE_TYPE); 43 nlohmann::json jsonData; 44 jsonData[SYSTEM_OS_TYPE] = GetOsType(); 45 jsonData[DEVICE_OHOS_VERSION] = GetOsVersion(); 46 jsonData[DEVICE_API_LEVEL] = GetSdkApiVersion(); 47 profile.SetCharacteristicProfileJson(jsonData.dump()); 48 return true; 49 } 50 GetOsType()51int32_t SystemInfoCollector::GetOsType() 52 { 53 const char* osFullName = GetOSFullName(); 54 if (strncmp(osFullName, DEVICE_OHOS_NAME.c_str(), strlen(DEVICE_OHOS_NAME.c_str())) == 0) { 55 free((char*)osFullName); 56 return OHOS_TYPE; 57 } 58 free((char*)osFullName); 59 HILOGE("get failed"); 60 return OHOS_TYPE_UNKNOWN; 61 } GetOsVersion()62std::string SystemInfoCollector::GetOsVersion() 63 { 64 const char* version = GetDisplayVersion(); 65 if (version == nullptr) { 66 HILOGE("get failed"); 67 return EMPTY_PARAM; 68 } 69 std::string osVersion = version; 70 free((char*)version); 71 return osVersion; 72 } 73 } // namespace DeviceProfile 74 } // namespace OHOS