1f8af9c48Sopenharmony_ci/* 2f8af9c48Sopenharmony_ci * Copyright (c) 2023-2024 Huawei Device Co., Ltd. 3f8af9c48Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4f8af9c48Sopenharmony_ci * you may not use this file except in compliance with the License. 5f8af9c48Sopenharmony_ci * You may obtain a copy of the License at 6f8af9c48Sopenharmony_ci * 7f8af9c48Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8f8af9c48Sopenharmony_ci * 9f8af9c48Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10f8af9c48Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11f8af9c48Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12f8af9c48Sopenharmony_ci * See the License for the specific language governing permissions and 13f8af9c48Sopenharmony_ci * limitations under the License. 14f8af9c48Sopenharmony_ci */ 15f8af9c48Sopenharmony_ci 16f8af9c48Sopenharmony_ci#include "characteristic_profile.h" 17f8af9c48Sopenharmony_ci#include "cJSON.h" 18f8af9c48Sopenharmony_ci#include "distributed_device_profile_constants.h" 19f8af9c48Sopenharmony_ci#include "macro_utils.h" 20f8af9c48Sopenharmony_ci#include "profile_utils.h" 21f8af9c48Sopenharmony_ci 22f8af9c48Sopenharmony_cinamespace OHOS { 23f8af9c48Sopenharmony_cinamespace DistributedDeviceProfile { 24f8af9c48Sopenharmony_cinamespace { 25f8af9c48Sopenharmony_ci const std::string TAG = "CharacteristicProfile"; 26f8af9c48Sopenharmony_ci} 27f8af9c48Sopenharmony_cistd::string CharacteristicProfile::GetDeviceId() const 28f8af9c48Sopenharmony_ci{ 29f8af9c48Sopenharmony_ci return deviceId_; 30f8af9c48Sopenharmony_ci} 31f8af9c48Sopenharmony_ci 32f8af9c48Sopenharmony_civoid CharacteristicProfile::SetDeviceId(const std::string& deviceId) 33f8af9c48Sopenharmony_ci{ 34f8af9c48Sopenharmony_ci deviceId_ = deviceId; 35f8af9c48Sopenharmony_ci} 36f8af9c48Sopenharmony_ci 37f8af9c48Sopenharmony_cistd::string CharacteristicProfile::GetServiceName() const 38f8af9c48Sopenharmony_ci{ 39f8af9c48Sopenharmony_ci return serviceName_; 40f8af9c48Sopenharmony_ci} 41f8af9c48Sopenharmony_ci 42f8af9c48Sopenharmony_civoid CharacteristicProfile::SetServiceName(const std::string& serviceName) 43f8af9c48Sopenharmony_ci{ 44f8af9c48Sopenharmony_ci serviceName_ = serviceName; 45f8af9c48Sopenharmony_ci} 46f8af9c48Sopenharmony_ci 47f8af9c48Sopenharmony_cistd::string CharacteristicProfile::GetCharacteristicKey() const 48f8af9c48Sopenharmony_ci{ 49f8af9c48Sopenharmony_ci return characteristicKey_; 50f8af9c48Sopenharmony_ci} 51f8af9c48Sopenharmony_ci 52f8af9c48Sopenharmony_civoid CharacteristicProfile::SetCharacteristicKey(const std::string& characteristicId) 53f8af9c48Sopenharmony_ci{ 54f8af9c48Sopenharmony_ci characteristicKey_ = characteristicId; 55f8af9c48Sopenharmony_ci} 56f8af9c48Sopenharmony_ci 57f8af9c48Sopenharmony_cistd::string CharacteristicProfile::GetCharacteristicValue() const 58f8af9c48Sopenharmony_ci{ 59f8af9c48Sopenharmony_ci return characteristicValue_; 60f8af9c48Sopenharmony_ci} 61f8af9c48Sopenharmony_ci 62f8af9c48Sopenharmony_civoid CharacteristicProfile::SetCharacteristicValue(const std::string& characteristicValue) 63f8af9c48Sopenharmony_ci{ 64f8af9c48Sopenharmony_ci characteristicValue_ = characteristicValue; 65f8af9c48Sopenharmony_ci} 66f8af9c48Sopenharmony_ci 67f8af9c48Sopenharmony_cibool CharacteristicProfile::Marshalling(MessageParcel& parcel) const 68f8af9c48Sopenharmony_ci{ 69f8af9c48Sopenharmony_ci WRITE_HELPER_RET(parcel, String, deviceId_, false); 70f8af9c48Sopenharmony_ci WRITE_HELPER_RET(parcel, String, serviceName_, false); 71f8af9c48Sopenharmony_ci WRITE_HELPER_RET(parcel, String, characteristicKey_, false); 72f8af9c48Sopenharmony_ci WRITE_HELPER_RET(parcel, String, characteristicValue_, false); 73f8af9c48Sopenharmony_ci return true; 74f8af9c48Sopenharmony_ci} 75f8af9c48Sopenharmony_ci 76f8af9c48Sopenharmony_cibool CharacteristicProfile::UnMarshalling(MessageParcel& parcel) 77f8af9c48Sopenharmony_ci{ 78f8af9c48Sopenharmony_ci READ_HELPER_RET(parcel, String, deviceId_, false); 79f8af9c48Sopenharmony_ci READ_HELPER_RET(parcel, String, serviceName_, false); 80f8af9c48Sopenharmony_ci READ_HELPER_RET(parcel, String, characteristicKey_, false); 81f8af9c48Sopenharmony_ci READ_HELPER_RET(parcel, String, characteristicValue_, false); 82f8af9c48Sopenharmony_ci return true; 83f8af9c48Sopenharmony_ci} 84f8af9c48Sopenharmony_ci 85f8af9c48Sopenharmony_cibool CharacteristicProfile::operator!=(const CharacteristicProfile& charProfile) const 86f8af9c48Sopenharmony_ci{ 87f8af9c48Sopenharmony_ci bool isNotEqual = (deviceId_ != charProfile.GetDeviceId() || serviceName_ != charProfile.GetServiceName() || 88f8af9c48Sopenharmony_ci characteristicKey_ != charProfile.GetCharacteristicKey() || 89f8af9c48Sopenharmony_ci characteristicValue_ != charProfile.GetCharacteristicValue()); 90f8af9c48Sopenharmony_ci if (isNotEqual) { 91f8af9c48Sopenharmony_ci return true; 92f8af9c48Sopenharmony_ci } else { 93f8af9c48Sopenharmony_ci return false; 94f8af9c48Sopenharmony_ci } 95f8af9c48Sopenharmony_ci} 96f8af9c48Sopenharmony_ci 97f8af9c48Sopenharmony_cistd::string CharacteristicProfile::dump() const 98f8af9c48Sopenharmony_ci{ 99f8af9c48Sopenharmony_ci cJSON* json = cJSON_CreateObject(); 100f8af9c48Sopenharmony_ci if (!cJSON_IsObject(json)) { 101f8af9c48Sopenharmony_ci cJSON_Delete(json); 102f8af9c48Sopenharmony_ci return EMPTY_STRING; 103f8af9c48Sopenharmony_ci } 104f8af9c48Sopenharmony_ci cJSON_AddStringToObject(json, DEVICE_ID.c_str(), ProfileUtils::GetAnonyString(deviceId_).c_str()); 105f8af9c48Sopenharmony_ci cJSON_AddStringToObject(json, SERVICE_NAME.c_str(), serviceName_.c_str()); 106f8af9c48Sopenharmony_ci cJSON_AddStringToObject(json, CHARACTERISTIC_KEY.c_str(), characteristicKey_.c_str()); 107f8af9c48Sopenharmony_ci if (characteristicKey_ == SWITCH_STATUS) { 108f8af9c48Sopenharmony_ci cJSON_AddStringToObject(json, CHARACTERISTIC_VALUE.c_str(), characteristicValue_.c_str()); 109f8af9c48Sopenharmony_ci } else { 110f8af9c48Sopenharmony_ci cJSON_AddStringToObject(json, CHARACTERISTIC_VALUE.c_str(), 111f8af9c48Sopenharmony_ci ProfileUtils::GetAnonyString(characteristicValue_).c_str()); 112f8af9c48Sopenharmony_ci } 113f8af9c48Sopenharmony_ci char* jsonChars = cJSON_PrintUnformatted(json); 114f8af9c48Sopenharmony_ci if (jsonChars == NULL) { 115f8af9c48Sopenharmony_ci cJSON_Delete(json); 116f8af9c48Sopenharmony_ci HILOGE("cJSON formatted to string failed!"); 117f8af9c48Sopenharmony_ci return EMPTY_STRING; 118f8af9c48Sopenharmony_ci } 119f8af9c48Sopenharmony_ci std::string jsonStr = jsonChars; 120f8af9c48Sopenharmony_ci cJSON_Delete(json); 121f8af9c48Sopenharmony_ci cJSON_free(jsonChars); 122f8af9c48Sopenharmony_ci return jsonStr; 123f8af9c48Sopenharmony_ci} 124f8af9c48Sopenharmony_ci} // namespace DistributedDeviceProfile 125f8af9c48Sopenharmony_ci} // namespace OHOS