1 /*
2 * Copyright (c) 2023 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 "dp_subscribe_info.h"
17 #include "profile_change_listener_stub.h"
18 #include "macro_utils.h"
19 #include "ipc_utils.h"
20 #include "distributed_device_profile_constants.h"
21 #include "nlohmann/json.hpp"
22 #include "profile_utils.h"
23
24 namespace OHOS {
25 namespace DistributedDeviceProfile {
26 namespace {
27 const std::string TAG = "SubscribeInfo";
28 }
SubscribeInfo(int32_t saId,const std::string & subscribeKey,std::unordered_set<ProfileChangeType> subscribeChangeTypes,sptr<IProfileChangeListener> profileChangeListener)29 SubscribeInfo::SubscribeInfo(int32_t saId, const std::string& subscribeKey,
30 std::unordered_set<ProfileChangeType> subscribeChangeTypes, sptr<IProfileChangeListener> profileChangeListener)
31 {
32 HILOGI("constructor!");
33 this->saId_ = saId;
34 this->subscribeKey_ = subscribeKey;
35 this->subscribeChangeTypes_ = subscribeChangeTypes;
36 if (profileChangeListener == nullptr) {
37 HILOGI("constructor!");
38 return;
39 }
40 if (profileChangeListener->AsObject() == nullptr) {
41 HILOGI("constructor!");
42 return;
43 }
44 this->listener_ = profileChangeListener->AsObject();
45 }
46
SubscribeInfo()47 SubscribeInfo::SubscribeInfo()
48 {
49 HILOGI("destructor!");
50 }
51
~SubscribeInfo()52 SubscribeInfo::~SubscribeInfo()
53 {
54 HILOGI("destructor!");
55 }
56
GetSaId() const57 int32_t SubscribeInfo::GetSaId() const
58 {
59 return saId_;
60 }
61
SetSaId(int32_t saId)62 void SubscribeInfo::SetSaId(int32_t saId)
63 {
64 saId_ = saId;
65 }
66
SetSubscribeKey(const std::string & deviceId,const std::string & deviceAttribute)67 void SubscribeInfo::SetSubscribeKey(const std::string& deviceId, const std::string& deviceAttribute)
68 {
69 subscribeKey_ = DEV_PREFIX + SEPARATOR + deviceId + SEPARATOR + deviceAttribute;
70 }
71
SetSubscribeKey(const std::string & deviceId,const std::string & serviceName,const std::string & serviceAttribute)72 void SubscribeInfo::SetSubscribeKey(const std::string& deviceId, const std::string& serviceName,
73 const std::string& serviceAttribute)
74 {
75 subscribeKey_ = SVR_PREFIX + SEPARATOR + deviceId + SEPARATOR + serviceName + SEPARATOR + serviceAttribute;
76 }
SetSubscribeKey(const std::string & deviceId,const std::string & serviceName,const std::string & characteristicKey,const std::string & characteristicAttribute)77 void SubscribeInfo::SetSubscribeKey(const std::string& deviceId, const std::string& serviceName,
78 const std::string& characteristicKey, const std::string& characteristicAttribute)
79 {
80 subscribeKey_ = CHAR_PREFIX + SEPARATOR + deviceId + SEPARATOR + serviceName + SEPARATOR + characteristicKey +
81 SEPARATOR + characteristicAttribute;
82 }
83
GetSubscribeKey() const84 std::string SubscribeInfo::GetSubscribeKey() const
85 {
86 return subscribeKey_;
87 }
88
SetSubscribeKey(const std::string & subscribeKey)89 void SubscribeInfo::SetSubscribeKey(const std::string& subscribeKey)
90 {
91 subscribeKey_ = subscribeKey;
92 }
93
GetListener() const94 sptr<IRemoteObject> SubscribeInfo::GetListener() const
95 {
96 return listener_;
97 }
98
GetProfileChangeTypes() const99 std::unordered_set<ProfileChangeType> SubscribeInfo::GetProfileChangeTypes() const
100 {
101 return subscribeChangeTypes_;
102 }
103
AddProfileChangeType(ProfileChangeType profileChangeType)104 void SubscribeInfo::AddProfileChangeType(ProfileChangeType profileChangeType)
105 {
106 if (profileChangeType <= PROFILE_CHANGE_TYPE_MIN || profileChangeType >= PROFILE_CHANGE_TYPE_MAX) {
107 return;
108 }
109 if (subscribeChangeTypes_.size() > MAX_SUBSCRIBE_CHANGE_SIZE) {
110 return;
111 }
112 subscribeChangeTypes_.emplace(profileChangeType);
113 }
114
SetListener(sptr<IProfileChangeListener> listener)115 void SubscribeInfo::SetListener(sptr<IProfileChangeListener> listener)
116 {
117 if (listener == nullptr) {
118 HILOGE("listener is null!");
119 return;
120 }
121 if (listener->AsObject() == nullptr) {
122 HILOGE("listener cast fail!");
123 return;
124 }
125 listener_ = listener->AsObject();
126 }
127
Marshalling(MessageParcel & parcel) const128 bool SubscribeInfo::Marshalling(MessageParcel& parcel) const
129 {
130 WRITE_HELPER_RET(parcel, Int32, saId_, false);
131 WRITE_HELPER_RET(parcel, String, subscribeKey_, false);
132 IpcUtils::Marshalling(parcel, subscribeChangeTypes_);
133 WRITE_HELPER_RET(parcel, RemoteObject, listener_, false);
134 return true;
135 }
136
UnMarshalling(MessageParcel & parcel)137 bool SubscribeInfo::UnMarshalling(MessageParcel& parcel)
138 {
139 READ_HELPER_RET(parcel, Int32, saId_, false);
140 READ_HELPER_RET(parcel, String, subscribeKey_, false);
141 IpcUtils::UnMarshalling(parcel, subscribeChangeTypes_);
142 listener_ = parcel.ReadRemoteObject();
143 if (listener_ == nullptr) {
144 HILOGE("read remoteObject failed!");
145 return false;
146 }
147 return true;
148 }
149
dump() const150 std::string SubscribeInfo::dump() const
151 {
152 nlohmann::json json;
153 json[SA_ID] = saId_;
154 json[SUBSCRIBE_KEY] = subscribeKey_;
155 json[SUBSCRIBE_CHANGE_TYPES] = subscribeChangeTypes_;
156 return json.dump();
157 }
158 } // namespace DistributedDeviceProfile
159 } // namespace OHOS
160
161