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
IsMultiUser() const67 bool CharacteristicProfile::IsMultiUser() const
68 {
69 return isMultiUser_;
70 }
71
SetIsMultiUser(bool isMultiUser)72 void CharacteristicProfile::SetIsMultiUser(bool isMultiUser)
73 {
74 isMultiUser_ = isMultiUser;
75 }
76
GetUserId() const77 int32_t CharacteristicProfile::GetUserId() const
78 {
79 return userId_;
80 }
81
SetUserId(int32_t userId)82 void CharacteristicProfile::SetUserId(int32_t userId)
83 {
84 userId_ = userId;
85 }
86
Marshalling(MessageParcel & parcel) const87 bool CharacteristicProfile::Marshalling(MessageParcel& parcel) const
88 {
89 WRITE_HELPER_RET(parcel, String, deviceId_, false);
90 WRITE_HELPER_RET(parcel, String, serviceName_, false);
91 WRITE_HELPER_RET(parcel, String, characteristicKey_, false);
92 WRITE_HELPER_RET(parcel, String, characteristicValue_, false);
93 WRITE_HELPER_RET(parcel, Bool, isMultiUser_, false);
94 WRITE_HELPER_RET(parcel, Int32, userId_, false);
95 return true;
96 }
97
UnMarshalling(MessageParcel & parcel)98 bool CharacteristicProfile::UnMarshalling(MessageParcel& parcel)
99 {
100 READ_HELPER_RET(parcel, String, deviceId_, false);
101 READ_HELPER_RET(parcel, String, serviceName_, false);
102 READ_HELPER_RET(parcel, String, characteristicKey_, false);
103 READ_HELPER_RET(parcel, String, characteristicValue_, false);
104 READ_HELPER_RET(parcel, Bool, isMultiUser_, false);
105 READ_HELPER_RET(parcel, Int32, userId_, false);
106 return true;
107 }
108
operator !=(const CharacteristicProfile & charProfile) const109 bool CharacteristicProfile::operator!=(const CharacteristicProfile& charProfile) const
110 {
111 bool isNotEqual = (deviceId_ != charProfile.GetDeviceId() || serviceName_ != charProfile.GetServiceName() ||
112 characteristicKey_ != charProfile.GetCharacteristicKey() ||
113 characteristicValue_ != charProfile.GetCharacteristicValue() || isMultiUser_ != charProfile.IsMultiUser() ||
114 userId_ != charProfile.GetUserId());
115 if (isNotEqual) {
116 return true;
117 } else {
118 return false;
119 }
120 }
121
dump() const122 std::string CharacteristicProfile::dump() const
123 {
124 cJSON* json = cJSON_CreateObject();
125 if (!cJSON_IsObject(json)) {
126 cJSON_Delete(json);
127 return EMPTY_STRING;
128 }
129 cJSON_AddStringToObject(json, DEVICE_ID.c_str(), ProfileUtils::GetAnonyString(deviceId_).c_str());
130 cJSON_AddStringToObject(json, SERVICE_NAME.c_str(), serviceName_.c_str());
131 cJSON_AddStringToObject(json, CHARACTERISTIC_KEY.c_str(), characteristicKey_.c_str());
132 cJSON_AddBoolToObject(json, IS_MULTI_USER.c_str(), isMultiUser_);
133 cJSON_AddNumberToObject(json, USER_ID.c_str(), userId_);
134 if (characteristicKey_ == SWITCH_STATUS) {
135 cJSON_AddStringToObject(json, CHARACTERISTIC_VALUE.c_str(), characteristicValue_.c_str());
136 } else {
137 cJSON_AddStringToObject(json, CHARACTERISTIC_VALUE.c_str(),
138 ProfileUtils::GetAnonyString(characteristicValue_).c_str());
139 }
140 char* jsonChars = cJSON_PrintUnformatted(json);
141 if (jsonChars == NULL) {
142 cJSON_Delete(json);
143 HILOGE("cJSON formatted to string failed!");
144 return EMPTY_STRING;
145 }
146 std::string jsonStr = jsonChars;
147 cJSON_Delete(json);
148 cJSON_free(jsonChars);
149 return jsonStr;
150 }
151 } // namespace DistributedDeviceProfile
152 } // namespace OHOS
153