1/* 2 * Copyright (c) 2023-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 "characteristic_profile.h" 17#include "cJSON.h" 18#include "distributed_device_profile_constants.h" 19#include "macro_utils.h" 20#include "profile_utils.h" 21 22namespace OHOS { 23namespace DistributedDeviceProfile { 24namespace { 25 const std::string TAG = "CharacteristicProfile"; 26} 27std::string CharacteristicProfile::GetDeviceId() const 28{ 29 return deviceId_; 30} 31 32void CharacteristicProfile::SetDeviceId(const std::string& deviceId) 33{ 34 deviceId_ = deviceId; 35} 36 37std::string CharacteristicProfile::GetServiceName() const 38{ 39 return serviceName_; 40} 41 42void CharacteristicProfile::SetServiceName(const std::string& serviceName) 43{ 44 serviceName_ = serviceName; 45} 46 47std::string CharacteristicProfile::GetCharacteristicKey() const 48{ 49 return characteristicKey_; 50} 51 52void CharacteristicProfile::SetCharacteristicKey(const std::string& characteristicId) 53{ 54 characteristicKey_ = characteristicId; 55} 56 57std::string CharacteristicProfile::GetCharacteristicValue() const 58{ 59 return characteristicValue_; 60} 61 62void CharacteristicProfile::SetCharacteristicValue(const std::string& characteristicValue) 63{ 64 characteristicValue_ = characteristicValue; 65} 66 67bool CharacteristicProfile::Marshalling(MessageParcel& parcel) const 68{ 69 WRITE_HELPER_RET(parcel, String, deviceId_, false); 70 WRITE_HELPER_RET(parcel, String, serviceName_, false); 71 WRITE_HELPER_RET(parcel, String, characteristicKey_, false); 72 WRITE_HELPER_RET(parcel, String, characteristicValue_, false); 73 return true; 74} 75 76bool CharacteristicProfile::UnMarshalling(MessageParcel& parcel) 77{ 78 READ_HELPER_RET(parcel, String, deviceId_, false); 79 READ_HELPER_RET(parcel, String, serviceName_, false); 80 READ_HELPER_RET(parcel, String, characteristicKey_, false); 81 READ_HELPER_RET(parcel, String, characteristicValue_, false); 82 return true; 83} 84 85bool CharacteristicProfile::operator!=(const CharacteristicProfile& charProfile) const 86{ 87 bool isNotEqual = (deviceId_ != charProfile.GetDeviceId() || serviceName_ != charProfile.GetServiceName() || 88 characteristicKey_ != charProfile.GetCharacteristicKey() || 89 characteristicValue_ != charProfile.GetCharacteristicValue()); 90 if (isNotEqual) { 91 return true; 92 } else { 93 return false; 94 } 95} 96 97std::string CharacteristicProfile::dump() const 98{ 99 cJSON* json = cJSON_CreateObject(); 100 if (!cJSON_IsObject(json)) { 101 cJSON_Delete(json); 102 return EMPTY_STRING; 103 } 104 cJSON_AddStringToObject(json, DEVICE_ID.c_str(), ProfileUtils::GetAnonyString(deviceId_).c_str()); 105 cJSON_AddStringToObject(json, SERVICE_NAME.c_str(), serviceName_.c_str()); 106 cJSON_AddStringToObject(json, CHARACTERISTIC_KEY.c_str(), characteristicKey_.c_str()); 107 if (characteristicKey_ == SWITCH_STATUS) { 108 cJSON_AddStringToObject(json, CHARACTERISTIC_VALUE.c_str(), characteristicValue_.c_str()); 109 } else { 110 cJSON_AddStringToObject(json, CHARACTERISTIC_VALUE.c_str(), 111 ProfileUtils::GetAnonyString(characteristicValue_).c_str()); 112 } 113 char* jsonChars = cJSON_PrintUnformatted(json); 114 if (jsonChars == NULL) { 115 cJSON_Delete(json); 116 HILOGE("cJSON formatted to string failed!"); 117 return EMPTY_STRING; 118 } 119 std::string jsonStr = jsonChars; 120 cJSON_Delete(json); 121 cJSON_free(jsonChars); 122 return jsonStr; 123} 124} // namespace DistributedDeviceProfile 125} // namespace OHOS