1 /* 2 * Copyright (c) 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 "dms_info_collector.h" 17 #include "cJSON.h" 18 #include "dms_constant.h" 19 #include "content_sensor_manager_utils.h" 20 #include "distributed_device_profile_log.h" 21 #include "profile_utils.h" 22 23 namespace OHOS { 24 namespace DistributedDeviceProfile { 25 namespace { 26 const std::string TAG = "DmsInfoCollector"; 27 } 28 ConvertToProfile(std::vector<ServiceProfile> & svrProfileList)29bool DmsInfoCollector::ConvertToProfile(std::vector<ServiceProfile>& svrProfileList) 30 { 31 HILOGI("called!"); 32 ServiceProfile svrProfile; 33 svrProfile.SetDeviceId(GetDeviceUdid()); 34 svrProfile.SetServiceName(DistributedSchedule::Constants::DMS_SERVICE_ID); 35 svrProfile.SetServiceType(DistributedSchedule::Constants::DMS_SERVICE_ID); 36 svrProfileList.push_back(svrProfile); 37 return true; 38 } 39 ConvertToProfile(std::vector<CharacteristicProfile> & charProfileList)40bool DmsInfoCollector::ConvertToProfile(std::vector<CharacteristicProfile>& charProfileList) 41 { 42 HILOGI("called!"); 43 CharacteristicProfile charProfile; 44 charProfile.SetDeviceId(GetDeviceUdid()); 45 charProfile.SetServiceName(DistributedSchedule::Constants::DMS_SERVICE_ID); 46 charProfile.SetCharacteristicKey(DistributedSchedule::Constants::DMS_CHAR_ID); 47 cJSON* jsonData = cJSON_CreateObject(); 48 if (!cJSON_IsObject(jsonData)) { 49 cJSON_Delete(jsonData); 50 HILOGE("Create cJSON failed!"); 51 return false; 52 } 53 cJSON* item = cJSON_AddStringToObject(jsonData, DistributedSchedule::Constants::PACKAGE_NAMES, 54 DistributedSchedule::Constants::DMS_NAME); 55 if (!cJSON_IsString(item)) { 56 HILOGE("Add PACKAGE_NAMES to cJSON failed!"); 57 cJSON_Delete(jsonData); 58 return false; 59 } 60 item = cJSON_AddStringToObject(jsonData, DistributedSchedule::Constants::VERSIONS, 61 DistributedSchedule::Constants::DMS_VERSION); 62 if (!cJSON_IsString(item)) { 63 HILOGE("Add VERSIONS to cJSON failed!"); 64 cJSON_Delete(jsonData); 65 return false; 66 } 67 char* jsonChars = cJSON_PrintUnformatted(jsonData); 68 if (jsonChars == NULL) { 69 cJSON_Delete(jsonData); 70 HILOGE("cJSON formatted to string failed!"); 71 return false; 72 } 73 std::string jsonStr = jsonChars; 74 charProfile.SetCharacteristicValue(jsonStr); 75 cJSON_Delete(jsonData); 76 cJSON_free(jsonChars); 77 charProfileList.push_back(charProfile); 78 return true; 79 } 80 GetDeviceUdid()81std::string DmsInfoCollector::GetDeviceUdid() 82 { 83 HILOGI("called!"); 84 return DistributedDeviceProfile::ContentSensorManagerUtils::GetInstance().ObtainLocalUdid(); 85 } 86 } // namespace DeviceProfile 87 } // namespace OHOS