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 "device_icon_info.h"
17
18 #include "cJSON.h"
19 #include "distributed_device_profile_constants.h"
20 #include "ipc_utils.h"
21 #include "macro_utils.h"
22 #include "profile_utils.h"
23
24 namespace OHOS {
25 namespace DistributedDeviceProfile {
26 namespace {
27 const std::string TAG = "DeviceIconInfo";
28 const std::string DEVICE_ICON_WISE_VERSION = "wiseVersion";
29 const std::string DEVICE_ICON_SIZE = "iconSize";
30 }
31
GetId() const32 int32_t DeviceIconInfo::GetId() const
33 {
34 return id_;
35 }
36
SetId(const int32_t id)37 void DeviceIconInfo::SetId(const int32_t id)
38 {
39 this->id_ = id;
40 }
41
GetProductId() const42 std::string DeviceIconInfo::GetProductId() const
43 {
44 return productId_;
45 }
46
SetProductId(const std::string & productId)47 void DeviceIconInfo::SetProductId(const std::string& productId)
48 {
49 this->productId_ = productId;
50 }
51
GetInternalModel() const52 std::string DeviceIconInfo::GetInternalModel() const
53 {
54 return internalModel_;
55 }
56
SetInternalModel(const std::string & internalModel)57 void DeviceIconInfo::SetInternalModel(const std::string& internalModel)
58 {
59 internalModel_ = internalModel;
60 }
61
GetSubProductId() const62 std::string DeviceIconInfo::GetSubProductId() const
63 {
64 return subProductId_;
65 }
66
SetSubProductId(const std::string & subProductId)67 void DeviceIconInfo::SetSubProductId(const std::string& subProductId)
68 {
69 this->subProductId_ = subProductId;
70 }
71
GetImageType() const72 std::string DeviceIconInfo::GetImageType() const
73 {
74 return imageType_;
75 }
76
SetImageType(const std::string & imageType)77 void DeviceIconInfo::SetImageType(const std::string& imageType)
78 {
79 this->imageType_ = imageType;
80 }
81
GetSpecName() const82 std::string DeviceIconInfo::GetSpecName() const
83 {
84 return specName_;
85 }
86
SetSpecName(const std::string & specName)87 void DeviceIconInfo::SetSpecName(const std::string& specName)
88 {
89 this->specName_ = specName;
90 }
91
GetVersion() const92 std::string DeviceIconInfo::GetVersion() const
93 {
94 return version_;
95 }
96
SetVersion(const std::string & version)97 void DeviceIconInfo::SetVersion(const std::string& version)
98 {
99 version_ = version;
100 }
101
GetWiseVersion() const102 std::string DeviceIconInfo::GetWiseVersion() const
103 {
104 return wiseVersion_;
105 }
106
SetWiseVersion(const std::string & wiseVersion)107 void DeviceIconInfo::SetWiseVersion(const std::string& wiseVersion)
108 {
109 wiseVersion_ = wiseVersion;
110 }
111
GetUrl() const112 std::string DeviceIconInfo::GetUrl() const
113 {
114 return url_;
115 }
116
SetUrl(const std::string & url)117 void DeviceIconInfo::SetUrl(const std::string& url)
118 {
119 url_ = url;
120 }
121
GetIcon() const122 std::vector<uint8_t> DeviceIconInfo::GetIcon() const
123 {
124 return icon_;
125 }
126
SetIcon(const std::vector<uint8_t> & icon)127 void DeviceIconInfo::SetIcon(const std::vector<uint8_t>& icon)
128 {
129 this->icon_ = icon;
130 }
131
Marshalling(MessageParcel & parcel) const132 bool DeviceIconInfo::Marshalling(MessageParcel& parcel) const
133 {
134 WRITE_HELPER_RET(parcel, String, productId_, false);
135 WRITE_HELPER_RET(parcel, String, internalModel_, false);
136 WRITE_HELPER_RET(parcel, String, subProductId_, false);
137 WRITE_HELPER_RET(parcel, String, imageType_, false);
138 WRITE_HELPER_RET(parcel, String, specName_, false);
139 WRITE_HELPER_RET(parcel, String, version_, false);
140 WRITE_HELPER_RET(parcel, String, wiseVersion_, false);
141 WRITE_HELPER_RET(parcel, String, url_, false);
142 IpcUtils::Marshalling(parcel, icon_);
143 return true;
144 }
145
UnMarshalling(MessageParcel & parcel)146 bool DeviceIconInfo::UnMarshalling(MessageParcel& parcel)
147 {
148 READ_HELPER_RET(parcel, String, productId_, false);
149 READ_HELPER_RET(parcel, String, internalModel_, false);
150 READ_HELPER_RET(parcel, String, subProductId_, false);
151 READ_HELPER_RET(parcel, String, imageType_, false);
152 READ_HELPER_RET(parcel, String, specName_, false);
153 READ_HELPER_RET(parcel, String, version_, false);
154 READ_HELPER_RET(parcel, String, wiseVersion_, false);
155 READ_HELPER_RET(parcel, String, url_, false);
156 IpcUtils::UnMarshalling(parcel, icon_);
157 return true;
158 }
159
operator !=(const DeviceIconInfo & other) const160 bool DeviceIconInfo::operator!=(const DeviceIconInfo& other) const
161 {
162 bool isNotEqual = (id_ != other.GetId() || productId_ != other.GetProductId() ||
163 internalModel_ != other.GetInternalModel() ||
164 subProductId_ != other.GetSubProductId() || imageType_ != other.GetImageType() ||
165 specName_ != other.GetSpecName() || version_ != other.GetVersion() || wiseVersion_ != other.GetWiseVersion() ||
166 url_ != other.GetUrl() || icon_ != other.GetIcon());
167 if (isNotEqual) {
168 return true;
169 } else {
170 return false;
171 }
172 }
173
dump() const174 std::string DeviceIconInfo::dump() const
175 {
176 cJSON* json = cJSON_CreateObject();
177 if (json == NULL) {
178 return EMPTY_STRING;
179 }
180 cJSON_AddNumberToObject(json, ID.c_str(), id_);
181 cJSON_AddStringToObject(json, PRODUCT_ID.c_str(), productId_.c_str());
182 cJSON_AddStringToObject(json, INTERNAL_MODEL.c_str(), internalModel_.c_str());
183 cJSON_AddStringToObject(json, SUB_PRODUCT_ID.c_str(), subProductId_.c_str());
184 cJSON_AddStringToObject(json, IMAGE_TYPE.c_str(), imageType_.c_str());
185 cJSON_AddStringToObject(json, SPEC_NAME.c_str(), specName_.c_str());
186 cJSON_AddStringToObject(json, SPEC_NAME.c_str(), specName_.c_str());
187 cJSON_AddStringToObject(json, DEVICE_ICON_VERSION.c_str(), version_.c_str());
188 cJSON_AddStringToObject(json, DEVICE_ICON_WISE_VERSION.c_str(), wiseVersion_.c_str());
189 cJSON_AddStringToObject(json, DEVICE_ICON_URL.c_str(), ProfileUtils::GetAnonyString(url_).c_str());
190 cJSON_AddNumberToObject(json, DEVICE_ICON_SIZE.c_str(), icon_.size());
191 char* jsonChars = cJSON_PrintUnformatted(json);
192 cJSON_Delete(json);
193 if (jsonChars == NULL) {
194 HILOGE("cJSON formatted to string failed!");
195 return EMPTY_STRING;
196 }
197 std::string jsonStr = jsonChars;
198 cJSON_free(jsonChars);
199 return jsonStr;
200 }
201 } // namespace DistributedDeviceProfile
202 } // namespace OHOS
203