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 
22 namespace OHOS {
23 namespace DistributedDeviceProfile {
24 namespace {
25     const std::string TAG = "CharacteristicProfile";
26 }
GetDeviceId() const27 std::string  CharacteristicProfile::GetDeviceId() const
28 {
29     return deviceId_;
30 }
31 
SetDeviceId(const std::string & deviceId)32 void CharacteristicProfile::SetDeviceId(const std::string& deviceId)
33 {
34     deviceId_ = deviceId;
35 }
36 
GetServiceName() const37 std::string  CharacteristicProfile::GetServiceName() const
38 {
39     return serviceName_;
40 }
41 
SetServiceName(const std::string & serviceName)42 void CharacteristicProfile::SetServiceName(const std::string& serviceName)
43 {
44     serviceName_ = serviceName;
45 }
46 
GetCharacteristicKey() const47 std::string  CharacteristicProfile::GetCharacteristicKey() const
48 {
49     return characteristicKey_;
50 }
51 
SetCharacteristicKey(const std::string & characteristicId)52 void CharacteristicProfile::SetCharacteristicKey(const std::string& characteristicId)
53 {
54     characteristicKey_ = characteristicId;
55 }
56 
GetCharacteristicValue() const57 std::string  CharacteristicProfile::GetCharacteristicValue() const
58 {
59     return characteristicValue_;
60 }
61 
SetCharacteristicValue(const std::string & characteristicValue)62 void CharacteristicProfile::SetCharacteristicValue(const std::string& characteristicValue)
63 {
64     characteristicValue_ = characteristicValue;
65 }
66 
Marshalling(MessageParcel & parcel) const67 bool 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 
UnMarshalling(MessageParcel & parcel)76 bool 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 
operator !=(const CharacteristicProfile & charProfile) const85 bool 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 
dump() const97 std::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
126