• 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 "dp_subscribe_info.h"
17 #include "cJSON.h"
18 #include "distributed_device_profile_constants.h"
19 #include "ipc_utils.h"
20 #include "macro_utils.h"
21 #include "profile_change_listener_stub.h"
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,int32_t userId)29 SubscribeInfo::SubscribeInfo(int32_t saId, const std::string& subscribeKey,
30     std::unordered_set<ProfileChangeType> subscribeChangeTypes, sptr<IProfileChangeListener> profileChangeListener,
31     int32_t userId)
32 {
33     this->saId_ = saId;
34     this->subscribeKey_ = subscribeKey;
35     if (userId != DEFAULT_USER_ID) {
36         this->subscribeKey_ = this->subscribeKey_ + SEPARATOR + std::to_string(userId);
37     }
38     this->subscribeChangeTypes_ = subscribeChangeTypes;
39     if (profileChangeListener == nullptr) {
40         HILOGI("constructor!");
41         return;
42     }
43     if (profileChangeListener->AsObject() == nullptr) {
44         HILOGI("constructor!");
45         return;
46     }
47     this->listener_ = profileChangeListener->AsObject();
48 }
49 
SubscribeInfo()50 SubscribeInfo::SubscribeInfo()
51 {
52 }
53 
~SubscribeInfo()54 SubscribeInfo::~SubscribeInfo()
55 {
56 }
57 
GetSaId() const58 int32_t SubscribeInfo::GetSaId() const
59 {
60     return saId_;
61 }
62 
SetSaId(int32_t saId)63 void SubscribeInfo::SetSaId(int32_t saId)
64 {
65     saId_ = saId;
66 }
67 
SetSubscribeKey(const std::string & deviceId,const std::string & deviceAttribute,int32_t userId)68 void SubscribeInfo::SetSubscribeKey(const std::string& deviceId, const std::string& deviceAttribute,
69     int32_t userId)
70 {
71     subscribeKey_ = DEV_PREFIX + SEPARATOR + deviceId + SEPARATOR + deviceAttribute;
72     if (userId != DEFAULT_USER_ID) {
73         subscribeKey_ = subscribeKey_ + SEPARATOR + std::to_string(userId);
74     }
75 }
76 
SetSubscribeKey(const std::string & deviceId,const std::string & serviceName,const std::string & serviceAttribute,int32_t userId)77 void SubscribeInfo::SetSubscribeKey(const std::string& deviceId, const std::string& serviceName,
78     const std::string& serviceAttribute, int32_t userId)
79 {
80     subscribeKey_ = SVR_PREFIX + SEPARATOR + deviceId + SEPARATOR + serviceName + SEPARATOR + serviceAttribute;
81     if (userId != DEFAULT_USER_ID) {
82         subscribeKey_ = subscribeKey_ + SEPARATOR + std::to_string(userId);
83     }
84 }
SetSubscribeKey(const std::string & deviceId,const std::string & serviceName,const std::string & characteristicKey,const std::string & characteristicAttribute,int32_t userId)85 void SubscribeInfo::SetSubscribeKey(const std::string& deviceId, const std::string& serviceName,
86     const std::string& characteristicKey, const std::string& characteristicAttribute, int32_t userId)
87 {
88     subscribeKey_ = CHAR_PREFIX + SEPARATOR + deviceId + SEPARATOR + serviceName + SEPARATOR + characteristicKey +
89         SEPARATOR + characteristicAttribute;
90     if (userId != DEFAULT_USER_ID) {
91         subscribeKey_ = subscribeKey_ + SEPARATOR + std::to_string(userId);
92     }
93 }
94 
GetSubscribeKey() const95 std::string SubscribeInfo::GetSubscribeKey() const
96 {
97     return subscribeKey_;
98 }
99 
SetSubscribeKey(const std::string & subscribeKey,int32_t userId)100 void SubscribeInfo::SetSubscribeKey(const std::string& subscribeKey, int32_t userId)
101 {
102     subscribeKey_ = subscribeKey;
103     if (userId != DEFAULT_USER_ID) {
104         subscribeKey_ = subscribeKey_ + SEPARATOR + std::to_string(userId);
105     }
106 }
107 
GetListener() const108 sptr<IRemoteObject> SubscribeInfo::GetListener() const
109 {
110     return listener_;
111 }
112 
GetProfileChangeTypes() const113 std::unordered_set<ProfileChangeType> SubscribeInfo::GetProfileChangeTypes() const
114 {
115     return subscribeChangeTypes_;
116 }
117 
AddProfileChangeType(ProfileChangeType profileChangeType)118 void SubscribeInfo::AddProfileChangeType(ProfileChangeType profileChangeType)
119 {
120     if (profileChangeType <= PROFILE_CHANGE_TYPE_MIN || profileChangeType >= PROFILE_CHANGE_TYPE_MAX) {
121         HILOGE("profileChangeType is invaild");
122         return;
123     }
124     if (subscribeChangeTypes_.size() > MAX_SUBSCRIBE_CHANGE_SIZE) {
125         HILOGE("subscribeChangeTypes_ size greater than max");
126         return;
127     }
128     subscribeChangeTypes_.emplace(profileChangeType);
129 }
130 
SetListener(sptr<IProfileChangeListener> listener)131 void SubscribeInfo::SetListener(sptr<IProfileChangeListener> listener)
132 {
133     if (listener == nullptr) {
134         HILOGE("listener is null!");
135         return;
136     }
137     if (listener->AsObject() == nullptr) {
138         HILOGE("listener cast fail!");
139         return;
140     }
141     listener_ = listener->AsObject();
142 }
143 
Marshalling(MessageParcel & parcel) const144 bool SubscribeInfo::Marshalling(MessageParcel& parcel) const
145 {
146     WRITE_HELPER_RET(parcel, Int32, saId_, false);
147     WRITE_HELPER_RET(parcel, String, subscribeKey_, false);
148     IpcUtils::Marshalling(parcel, subscribeChangeTypes_);
149     WRITE_HELPER_RET(parcel, RemoteObject, listener_, false);
150     return true;
151 }
152 
UnMarshalling(MessageParcel & parcel)153 bool SubscribeInfo::UnMarshalling(MessageParcel& parcel)
154 {
155     READ_HELPER_RET(parcel, Int32, saId_, false);
156     READ_HELPER_RET(parcel, String, subscribeKey_, false);
157     IpcUtils::UnMarshalling(parcel, subscribeChangeTypes_);
158     listener_ = parcel.ReadRemoteObject();
159     if (listener_ == nullptr) {
160         HILOGE("read remoteObject failed!");
161         return false;
162     }
163     return true;
164 }
165 
dump() const166 std::string SubscribeInfo::dump() const
167 {
168     cJSON* json = cJSON_CreateObject();
169     if (!cJSON_IsObject(json)) {
170         cJSON_Delete(json);
171         return EMPTY_STRING;
172     }
173     cJSON_AddNumberToObject(json, SA_ID.c_str(), saId_);
174     cJSON_AddStringToObject(json, SUBSCRIBE_KEY.c_str(), ProfileUtils::GetDbKeyAnonyString(subscribeKey_).c_str());
175     cJSON* jsonArr = cJSON_CreateArray();
176     if (!cJSON_IsArray(jsonArr)) {
177         cJSON_Delete(jsonArr);
178     } else {
179         for (const auto &subChangeType : subscribeChangeTypes_) {
180             cJSON* jsonArrItem = cJSON_CreateNumber(static_cast<int32_t>(subChangeType));
181             if (jsonArrItem == NULL) {
182                 continue;
183             }
184             if (!cJSON_AddItemToArray(jsonArr, jsonArrItem)) {
185                 cJSON_Delete(jsonArrItem);
186                 continue;
187             }
188         }
189         if (!cJSON_AddItemToObject(json, SUBSCRIBE_CHANGE_TYPES.c_str(), jsonArr)) {
190             HILOGE("cJSON formatted to string failed!");
191             cJSON_Delete(jsonArr);
192         }
193     }
194     char* jsonChars = cJSON_PrintUnformatted(json);
195     if (jsonChars == NULL) {
196         cJSON_Delete(json);
197         HILOGE("cJSON formatted to string failed!");
198         return EMPTY_STRING;
199     }
200     std::string jsonStr = jsonChars;
201     cJSON_Delete(json);
202     cJSON_free(jsonChars);
203     return jsonStr;
204 }
205 } // namespace DistributedDeviceProfile
206 } // namespace OHOS
207 
208