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
22namespace OHOS {
23namespace DistributedDeviceProfile {
24namespace {
25    const std::string TAG = "ServiceProfile";
26}
27
28ServiceProfile::ServiceProfile(const std::string& deviceId, const std::string& serviceName,
29    const std::string& serviceType) : deviceId_(deviceId), serviceName_(serviceName), serviceType_(serviceType)
30{
31}
32ServiceProfile::ServiceProfile()
33{
34}
35
36ServiceProfile::~ServiceProfile()
37{
38}
39
40std::string ServiceProfile::GetDeviceId() const
41{
42    return deviceId_;
43}
44
45void ServiceProfile::SetDeviceId(const std::string& deviceId)
46{
47    deviceId_ = deviceId;
48}
49
50std::string ServiceProfile::GetServiceName() const
51{
52    return serviceName_;
53}
54
55void ServiceProfile::SetServiceName(const std::string& serviceName)
56{
57    serviceName_ = serviceName;
58}
59
60std::string ServiceProfile::GetServiceType() const
61{
62    return serviceType_;
63}
64
65void ServiceProfile::SetServiceType(const std::string& serviceType)
66{
67    serviceType_ = serviceType;
68}
69
70bool 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
78bool 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
86bool 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
97std::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