• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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(const std::string & deviceId,const std::string & serviceName,const std::string & serviceType,const bool isMultiUser,const int32_t userId)32 ServiceProfile::ServiceProfile(const std::string& deviceId, const std::string& serviceName,
33     const std::string& serviceType, const bool isMultiUser, const int32_t userId)
34     : deviceId_(deviceId), serviceName_(serviceName), serviceType_(serviceType), isMultiUser_(isMultiUser),
35     userId_(userId)
36 {
37 }
ServiceProfile()38 ServiceProfile::ServiceProfile()
39 {
40 }
41 
~ServiceProfile()42 ServiceProfile::~ServiceProfile()
43 {
44 }
45 
GetDeviceId() const46 std::string ServiceProfile::GetDeviceId() const
47 {
48     return deviceId_;
49 }
50 
SetDeviceId(const std::string & deviceId)51 void ServiceProfile::SetDeviceId(const std::string& deviceId)
52 {
53     deviceId_ = deviceId;
54 }
55 
GetServiceName() const56 std::string ServiceProfile::GetServiceName() const
57 {
58     return serviceName_;
59 }
60 
SetServiceName(const std::string & serviceName)61 void ServiceProfile::SetServiceName(const std::string& serviceName)
62 {
63     serviceName_ = serviceName;
64 }
65 
GetServiceType() const66 std::string ServiceProfile::GetServiceType() const
67 {
68     return serviceType_;
69 }
70 
SetServiceType(const std::string & serviceType)71 void ServiceProfile::SetServiceType(const std::string& serviceType)
72 {
73     serviceType_ = serviceType;
74 }
75 
IsMultiUser() const76 bool ServiceProfile::IsMultiUser() const
77 {
78     return isMultiUser_;
79 }
80 
SetIsMultiUser(bool isMultiUser)81 void ServiceProfile::SetIsMultiUser(bool isMultiUser)
82 {
83     isMultiUser_ = isMultiUser;
84 }
85 
GetUserId() const86 int32_t ServiceProfile::GetUserId() const
87 {
88     return userId_;
89 }
90 
SetUserId(int32_t userId)91 void ServiceProfile::SetUserId(int32_t userId)
92 {
93     userId_ = userId;
94 }
95 
Marshalling(MessageParcel & parcel) const96 bool ServiceProfile::Marshalling(MessageParcel& parcel) const
97 {
98     WRITE_HELPER_RET(parcel, String, deviceId_, false);
99     WRITE_HELPER_RET(parcel, String, serviceName_, false);
100     WRITE_HELPER_RET(parcel, String, serviceType_, false);
101     WRITE_HELPER_RET(parcel, Bool, isMultiUser_, false);
102     WRITE_HELPER_RET(parcel, Int32, userId_, false);
103     return true;
104 }
105 
UnMarshalling(MessageParcel & parcel)106 bool ServiceProfile::UnMarshalling(MessageParcel& parcel)
107 {
108     READ_HELPER_RET(parcel, String, deviceId_, false);
109     READ_HELPER_RET(parcel, String, serviceName_, false);
110     READ_HELPER_RET(parcel, String, serviceType_, false);
111     READ_HELPER_RET(parcel, Bool, isMultiUser_, false);
112     READ_HELPER_RET(parcel, Int32, userId_, false);
113     return true;
114 }
115 
operator !=(const ServiceProfile & serviceProfile) const116 bool ServiceProfile::operator!=(const ServiceProfile& serviceProfile) const
117 {
118     bool isNotEqual = (deviceId_ != serviceProfile.GetDeviceId() || serviceName_ != serviceProfile.GetServiceName() ||
119         serviceType_ != serviceProfile.GetServiceType() || isMultiUser_ != serviceProfile.IsMultiUser() ||
120         userId_ != serviceProfile.GetUserId());
121     if (isNotEqual) {
122         return true;
123     } else {
124         return false;
125     }
126 }
127 
dump() const128 std::string ServiceProfile::dump() const
129 {
130     cJSON* json = cJSON_CreateObject();
131     if (!cJSON_IsObject(json)) {
132         cJSON_Delete(json);
133         return EMPTY_STRING;
134     }
135     cJSON_AddStringToObject(json, DEVICE_ID.c_str(), ProfileUtils::GetAnonyString(deviceId_).c_str());
136     cJSON_AddStringToObject(json, SERVICE_NAME.c_str(), serviceName_.c_str());
137     cJSON_AddStringToObject(json, SERVICE_TYPE.c_str(), serviceType_.c_str());
138     cJSON_AddBoolToObject(json, IS_MULTI_USER.c_str(), isMultiUser_);
139     cJSON_AddNumberToObject(json, USER_ID.c_str(), userId_);
140     char* jsonChars = cJSON_PrintUnformatted(json);
141     if (jsonChars == NULL) {
142         cJSON_Delete(json);
143         HILOGE("cJSON formatted to string failed!");
144         return EMPTY_STRING;
145     }
146     std::string jsonStr = jsonChars;
147     cJSON_Delete(json);
148     cJSON_free(jsonChars);
149     return jsonStr;
150 }
151 } // namespace DistributedDeviceProfile
152 } // namespace OHOS
153