1f8af9c48Sopenharmony_ci/*
2f8af9c48Sopenharmony_ci * Copyright (c) 2023-2024 Huawei Device Co., Ltd.
3f8af9c48Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4f8af9c48Sopenharmony_ci * you may not use this file except in compliance with the License.
5f8af9c48Sopenharmony_ci * You may obtain a copy of the License at
6f8af9c48Sopenharmony_ci *
7f8af9c48Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8f8af9c48Sopenharmony_ci *
9f8af9c48Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10f8af9c48Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11f8af9c48Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12f8af9c48Sopenharmony_ci * See the License for the specific language governing permissions and
13f8af9c48Sopenharmony_ci * limitations under the License.
14f8af9c48Sopenharmony_ci */
15f8af9c48Sopenharmony_ci
16f8af9c48Sopenharmony_ci#include "service_profile.h"
17f8af9c48Sopenharmony_ci#include "cJSON.h"
18f8af9c48Sopenharmony_ci#include "distributed_device_profile_constants.h"
19f8af9c48Sopenharmony_ci#include "macro_utils.h"
20f8af9c48Sopenharmony_ci#include "profile_utils.h"
21f8af9c48Sopenharmony_ci
22f8af9c48Sopenharmony_cinamespace OHOS {
23f8af9c48Sopenharmony_cinamespace DistributedDeviceProfile {
24f8af9c48Sopenharmony_cinamespace {
25f8af9c48Sopenharmony_ci    const std::string TAG = "ServiceProfile";
26f8af9c48Sopenharmony_ci}
27f8af9c48Sopenharmony_ci
28f8af9c48Sopenharmony_ciServiceProfile::ServiceProfile(const std::string& deviceId, const std::string& serviceName,
29f8af9c48Sopenharmony_ci    const std::string& serviceType) : deviceId_(deviceId), serviceName_(serviceName), serviceType_(serviceType)
30f8af9c48Sopenharmony_ci{
31f8af9c48Sopenharmony_ci}
32f8af9c48Sopenharmony_ciServiceProfile::ServiceProfile()
33f8af9c48Sopenharmony_ci{
34f8af9c48Sopenharmony_ci}
35f8af9c48Sopenharmony_ci
36f8af9c48Sopenharmony_ciServiceProfile::~ServiceProfile()
37f8af9c48Sopenharmony_ci{
38f8af9c48Sopenharmony_ci}
39f8af9c48Sopenharmony_ci
40f8af9c48Sopenharmony_cistd::string ServiceProfile::GetDeviceId() const
41f8af9c48Sopenharmony_ci{
42f8af9c48Sopenharmony_ci    return deviceId_;
43f8af9c48Sopenharmony_ci}
44f8af9c48Sopenharmony_ci
45f8af9c48Sopenharmony_civoid ServiceProfile::SetDeviceId(const std::string& deviceId)
46f8af9c48Sopenharmony_ci{
47f8af9c48Sopenharmony_ci    deviceId_ = deviceId;
48f8af9c48Sopenharmony_ci}
49f8af9c48Sopenharmony_ci
50f8af9c48Sopenharmony_cistd::string ServiceProfile::GetServiceName() const
51f8af9c48Sopenharmony_ci{
52f8af9c48Sopenharmony_ci    return serviceName_;
53f8af9c48Sopenharmony_ci}
54f8af9c48Sopenharmony_ci
55f8af9c48Sopenharmony_civoid ServiceProfile::SetServiceName(const std::string& serviceName)
56f8af9c48Sopenharmony_ci{
57f8af9c48Sopenharmony_ci    serviceName_ = serviceName;
58f8af9c48Sopenharmony_ci}
59f8af9c48Sopenharmony_ci
60f8af9c48Sopenharmony_cistd::string ServiceProfile::GetServiceType() const
61f8af9c48Sopenharmony_ci{
62f8af9c48Sopenharmony_ci    return serviceType_;
63f8af9c48Sopenharmony_ci}
64f8af9c48Sopenharmony_ci
65f8af9c48Sopenharmony_civoid ServiceProfile::SetServiceType(const std::string& serviceType)
66f8af9c48Sopenharmony_ci{
67f8af9c48Sopenharmony_ci    serviceType_ = serviceType;
68f8af9c48Sopenharmony_ci}
69f8af9c48Sopenharmony_ci
70f8af9c48Sopenharmony_cibool ServiceProfile::Marshalling(MessageParcel& parcel) const
71f8af9c48Sopenharmony_ci{
72f8af9c48Sopenharmony_ci    WRITE_HELPER_RET(parcel, String, deviceId_, false);
73f8af9c48Sopenharmony_ci    WRITE_HELPER_RET(parcel, String, serviceName_, false);
74f8af9c48Sopenharmony_ci    WRITE_HELPER_RET(parcel, String, serviceType_, false);
75f8af9c48Sopenharmony_ci    return true;
76f8af9c48Sopenharmony_ci}
77f8af9c48Sopenharmony_ci
78f8af9c48Sopenharmony_cibool ServiceProfile::UnMarshalling(MessageParcel& parcel)
79f8af9c48Sopenharmony_ci{
80f8af9c48Sopenharmony_ci    READ_HELPER_RET(parcel, String, deviceId_, false);
81f8af9c48Sopenharmony_ci    READ_HELPER_RET(parcel, String, serviceName_, false);
82f8af9c48Sopenharmony_ci    READ_HELPER_RET(parcel, String, serviceType_, false);
83f8af9c48Sopenharmony_ci    return true;
84f8af9c48Sopenharmony_ci}
85f8af9c48Sopenharmony_ci
86f8af9c48Sopenharmony_cibool ServiceProfile::operator!=(const ServiceProfile& serviceProfile) const
87f8af9c48Sopenharmony_ci{
88f8af9c48Sopenharmony_ci    bool isNotEqual = (deviceId_ != serviceProfile.GetDeviceId() || serviceName_ != serviceProfile.GetServiceName() ||
89f8af9c48Sopenharmony_ci        serviceType_ != serviceProfile.GetServiceType());
90f8af9c48Sopenharmony_ci    if (isNotEqual) {
91f8af9c48Sopenharmony_ci        return true;
92f8af9c48Sopenharmony_ci    } else {
93f8af9c48Sopenharmony_ci        return false;
94f8af9c48Sopenharmony_ci    }
95f8af9c48Sopenharmony_ci}
96f8af9c48Sopenharmony_ci
97f8af9c48Sopenharmony_cistd::string ServiceProfile::dump() const
98f8af9c48Sopenharmony_ci{
99f8af9c48Sopenharmony_ci    cJSON* json = cJSON_CreateObject();
100f8af9c48Sopenharmony_ci    if (!cJSON_IsObject(json)) {
101f8af9c48Sopenharmony_ci        cJSON_Delete(json);
102f8af9c48Sopenharmony_ci        return EMPTY_STRING;
103f8af9c48Sopenharmony_ci    }
104f8af9c48Sopenharmony_ci    cJSON_AddStringToObject(json, DEVICE_ID.c_str(), ProfileUtils::GetAnonyString(deviceId_).c_str());
105f8af9c48Sopenharmony_ci    cJSON_AddStringToObject(json, SERVICE_NAME.c_str(), serviceName_.c_str());
106f8af9c48Sopenharmony_ci    cJSON_AddStringToObject(json, SERVICE_TYPE.c_str(), serviceType_.c_str());
107f8af9c48Sopenharmony_ci    char* jsonChars = cJSON_PrintUnformatted(json);
108f8af9c48Sopenharmony_ci    if (jsonChars == NULL) {
109f8af9c48Sopenharmony_ci        cJSON_Delete(json);
110f8af9c48Sopenharmony_ci        HILOGE("cJSON formatted to string failed!");
111f8af9c48Sopenharmony_ci        return EMPTY_STRING;
112f8af9c48Sopenharmony_ci    }
113f8af9c48Sopenharmony_ci    std::string jsonStr = jsonChars;
114f8af9c48Sopenharmony_ci    cJSON_Delete(json);
115f8af9c48Sopenharmony_ci    cJSON_free(jsonChars);
116f8af9c48Sopenharmony_ci    return jsonStr;
117f8af9c48Sopenharmony_ci}
118f8af9c48Sopenharmony_ci} // namespace DistributedDeviceProfile
119f8af9c48Sopenharmony_ci} // namespace OHOS