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 "service_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 = "ServiceProfile";
26 }
27 
ServiceProfile(const std::string & deviceId,const std::string & serviceName,const std::string & serviceType)28 ServiceProfile::ServiceProfile(const std::string& deviceId, const std::string& serviceName,
29     const std::string& serviceType) : deviceId_(deviceId), serviceName_(serviceName), serviceType_(serviceType)
30 {
31 }
ServiceProfile()32 ServiceProfile::ServiceProfile()
33 {
34 }
35 
~ServiceProfile()36 ServiceProfile::~ServiceProfile()
37 {
38 }
39 
GetDeviceId() const40 std::string ServiceProfile::GetDeviceId() const
41 {
42     return deviceId_;
43 }
44 
SetDeviceId(const std::string & deviceId)45 void ServiceProfile::SetDeviceId(const std::string& deviceId)
46 {
47     deviceId_ = deviceId;
48 }
49 
GetServiceName() const50 std::string ServiceProfile::GetServiceName() const
51 {
52     return serviceName_;
53 }
54 
SetServiceName(const std::string & serviceName)55 void ServiceProfile::SetServiceName(const std::string& serviceName)
56 {
57     serviceName_ = serviceName;
58 }
59 
GetServiceType() const60 std::string ServiceProfile::GetServiceType() const
61 {
62     return serviceType_;
63 }
64 
SetServiceType(const std::string & serviceType)65 void ServiceProfile::SetServiceType(const std::string& serviceType)
66 {
67     serviceType_ = serviceType;
68 }
69 
Marshalling(MessageParcel & parcel) const70 bool ServiceProfile::Marshalling(MessageParcel& parcel) const
71 {
72     WRITE_HELPER_RET(parcel, String, deviceId_, false);
73     WRITE_HELPER_RET(parcel, String, serviceName_, false);
74     WRITE_HELPER_RET(parcel, String, serviceType_, false);
75     return true;
76 }
77 
UnMarshalling(MessageParcel & parcel)78 bool ServiceProfile::UnMarshalling(MessageParcel& parcel)
79 {
80     READ_HELPER_RET(parcel, String, deviceId_, false);
81     READ_HELPER_RET(parcel, String, serviceName_, false);
82     READ_HELPER_RET(parcel, String, serviceType_, false);
83     return true;
84 }
85 
operator !=(const ServiceProfile & serviceProfile) const86 bool ServiceProfile::operator!=(const ServiceProfile& serviceProfile) const
87 {
88     bool isNotEqual = (deviceId_ != serviceProfile.GetDeviceId() || serviceName_ != serviceProfile.GetServiceName() ||
89         serviceType_ != serviceProfile.GetServiceType());
90     if (isNotEqual) {
91         return true;
92     } else {
93         return false;
94     }
95 }
96 
dump() const97 std::string ServiceProfile::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, SERVICE_TYPE.c_str(), serviceType_.c_str());
107     char* jsonChars = cJSON_PrintUnformatted(json);
108     if (jsonChars == NULL) {
109         cJSON_Delete(json);
110         HILOGE("cJSON formatted to string failed!");
111         return EMPTY_STRING;
112     }
113     std::string jsonStr = jsonChars;
114     cJSON_Delete(json);
115     cJSON_free(jsonChars);
116     return jsonStr;
117 }
118 } // namespace DistributedDeviceProfile
119 } // namespace OHOS