• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "product_info.h"
17 
18 #include "cJSON.h"
19 #include "distributed_device_profile_constants.h"
20 #include "macro_utils.h"
21 #include "profile_utils.h"
22 
23 namespace OHOS {
24 namespace DistributedDeviceProfile {
25 namespace {
26     const std::string TAG = "ProductInfo";
27 }
28 
GetProductId() const29 std::string ProductInfo::GetProductId() const
30 {
31     return productId_;
32 }
33 
SetProductId(const std::string & productId)34 void ProductInfo::SetProductId(const std::string& productId)
35 {
36     this->productId_ = productId;
37 }
38 
GetModel() const39 std::string ProductInfo::GetModel() const
40 {
41     return model_;
42 }
43 
SetModel(const std::string & model)44 void ProductInfo::SetModel(const std::string& model)
45 {
46     this->model_ = model;
47 }
48 
GetProductName() const49 std::string ProductInfo::GetProductName() const
50 {
51     return productName_;
52 }
53 
SetProductName(const std::string & productName)54 void ProductInfo::SetProductName(const std::string& productName)
55 {
56     this->productName_ = productName;
57 }
58 
GetProductShortName() const59 std::string ProductInfo::GetProductShortName() const
60 {
61     return productShortName_;
62 }
63 
SetProductShortName(const std::string & productShortName)64 void ProductInfo::SetProductShortName(const std::string& productShortName)
65 {
66     this->productShortName_ = productShortName;
67 }
68 
GetImageVersion() const69 std::string ProductInfo::GetImageVersion() const
70 {
71     return imageVersion_;
72 }
73 
SetImageVersion(const std::string & imageVersion)74 void ProductInfo::SetImageVersion(const std::string& imageVersion)
75 {
76     this->imageVersion_ = imageVersion;
77 }
78 
79 
Marshalling(MessageParcel & parcel) const80 bool ProductInfo::Marshalling(MessageParcel& parcel) const
81 {
82     WRITE_HELPER_RET(parcel, String, productId_, false);
83     WRITE_HELPER_RET(parcel, String, model_, false);
84     WRITE_HELPER_RET(parcel, String, productName_, false);
85     WRITE_HELPER_RET(parcel, String, productShortName_, false);
86     WRITE_HELPER_RET(parcel, String, imageVersion_, false);
87     return true;
88 }
89 
UnMarshalling(MessageParcel & parcel)90 bool ProductInfo::UnMarshalling(MessageParcel& parcel)
91 {
92     READ_HELPER_RET(parcel, String, productId_, false);
93     READ_HELPER_RET(parcel, String, model_, false);
94     READ_HELPER_RET(parcel, String, productName_, false);
95     READ_HELPER_RET(parcel, String, productShortName_, false);
96     READ_HELPER_RET(parcel, String, imageVersion_, false);
97     return true;
98 }
99 
operator !=(const ProductInfo & other) const100 bool ProductInfo::operator!=(const ProductInfo& other) const
101 {
102     bool isNotEqual = (productId_ != other.GetProductId() || model_ != other.GetModel() ||
103         productName_ != other.GetProductName() || productShortName_ != other.GetProductShortName() ||
104         imageVersion_ != other.GetImageVersion());
105     if (isNotEqual) {
106         return true;
107     } else {
108         return false;
109     }
110 }
111 
dump() const112 std::string ProductInfo::dump() const
113 {
114     cJSON* json = cJSON_CreateObject();
115     if (json == NULL) {
116         return EMPTY_STRING;
117     }
118     cJSON_AddStringToObject(json, PRODUCT_ID.c_str(), productId_.c_str());
119     cJSON_AddStringToObject(json, DEVICE_MODEL.c_str(), model_.c_str());
120     cJSON_AddStringToObject(json, PRODUCT_NAME.c_str(), GetAnonyProductName(productName_).c_str());
121     cJSON_AddStringToObject(json, PRODUCT_SHORT_NAME.c_str(), GetAnonyProductName(productShortName_).c_str());
122     cJSON_AddStringToObject(json, IMAGE_VERSION.c_str(), imageVersion_.c_str());
123     char* jsonChars = cJSON_PrintUnformatted(json);
124     if (jsonChars == NULL) {
125         cJSON_Delete(json);
126         HILOGE("cJSON formatted to string failed!");
127         return EMPTY_STRING;
128     }
129     std::string jsonStr = jsonChars;
130     cJSON_Delete(json);
131     cJSON_free(jsonChars);
132     return jsonStr;
133 }
134 
GetAnonyProductName(const std::string & productName) const135 std::string ProductInfo::GetAnonyProductName(const std::string& productName) const
136 {
137     cJSON* json = cJSON_Parse(productName.c_str());
138     if (!cJSON_IsObject(json)) {
139         HILOGW("cJSON_Parse productName fail!");
140         cJSON_Delete(json);
141         return EMPTY_STRING;
142     }
143     cJSON* item = json->child;
144     while (item != NULL) {
145         if (cJSON_IsString(item)) {
146             cJSON_SetValuestring(item, ProfileUtils::GetAnonyString(item->valuestring).c_str());
147         }
148         item = item->next;
149     }
150     char* jsonChars = cJSON_PrintUnformatted(json);
151     if (jsonChars == NULL) {
152         cJSON_Delete(json);
153         HILOGE("cJSON formatted to string failed!");
154         return EMPTY_STRING;
155     }
156     std::string jsonStr = jsonChars;
157     cJSON_free(jsonChars);
158     cJSON_Delete(json);
159     return jsonStr;
160 }
161 } // namespace DistributedDeviceProfile
162 } // namespace OHOS
163