1 /*
2 * Copyright (c) 2021-2025 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 "distributed_device_profile_client.h"
17
18 #include <algorithm>
19 #include <bitset>
20 #include <chrono>
21 #include <functional>
22 #include <new>
23 #include <string>
24 #include <thread>
25 #include <type_traits>
26 #include <unistd.h>
27 #include <utility>
28 #include <profile_utils.h>
29 #include "profile_change_listener_stub.h"
30 #include "device_profile_load_callback.h"
31 #include "distributed_device_profile_errors.h"
32 #include "distributed_device_profile_log.h"
33 #include "dp_radar_helper.h"
34 #include "event_handler.h"
35 #include "event_runner.h"
36 #include "i_distributed_device_profile.h"
37 #include "if_system_ability_manager.h"
38 #include "ipc_skeleton.h"
39 #include "iremote_broker.h"
40 #include "iservice_registry.h"
41 #include "system_ability_definition.h"
42
43 namespace OHOS {
44 namespace DistributedDeviceProfile {
45 using namespace std::chrono_literals;
46
47 namespace {
48 const std::string TAG = "DistributedDeviceProfileClient";
49 constexpr int32_t LOAD_SA_TIMEOUT_MS = 10000;
50 constexpr int32_t MAX_RETRY_TIMES = 7;
51 }
52
53 IMPLEMENT_SINGLE_INSTANCE(DistributedDeviceProfileClient);
54
LoadDeviceProfileService()55 sptr<IDistributedDeviceProfile> DistributedDeviceProfileClient::LoadDeviceProfileService()
56 {
57 sptr<DeviceProfileLoadCallback> loadCallback = new DeviceProfileLoadCallback();
58 if (loadCallback == nullptr) {
59 HILOGE("loadCallback is nullptr.");
60 return nullptr;
61 }
62
63 auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
64 if (samgrProxy == nullptr) {
65 HILOGE("get samgr failed");
66 return nullptr;
67 }
68
69 int32_t ret = samgrProxy->LoadSystemAbility(DISTRIBUTED_DEVICE_PROFILE_SA_ID, loadCallback);
70 int32_t stageRes = (ret == ERR_OK) ?
71 static_cast<int32_t>(StageRes::STAGE_IDLE) : static_cast<int32_t>(StageRes::STAGE_FAIL);
72 DpRadarHelper::GetInstance().ReportLoadDpSa(stageRes);
73 if (ret != ERR_OK) {
74 HILOGE("Failed to Load systemAbility");
75 return nullptr;
76 }
77 {
78 std::unique_lock<std::mutex> lock(serviceLock_);
79 auto waitStatus = proxyConVar_.wait_for(lock, std::chrono::milliseconds(LOAD_SA_TIMEOUT_MS),
80 [this]() { return dpProxy_ != nullptr; });
81 if (waitStatus && dpProxy_ != nullptr) {
82 HILOGE("Get profile Service success!");
83 return dpProxy_;
84 }
85 }
86 return nullptr;
87 }
88
LoadSystemAbilitySuccess(const sptr<IRemoteObject> & remoteObject)89 void DistributedDeviceProfileClient::LoadSystemAbilitySuccess(const sptr<IRemoteObject> &remoteObject)
90 {
91 HILOGI("FinishStartSA");
92 int32_t stageRes = static_cast<int32_t>(StageRes::STAGE_SUCC);
93 DpRadarHelper::GetInstance().ReportLoadDpSaCb(stageRes);
94 std::lock_guard<std::mutex> lock(serviceLock_);
95 if (dpDeathRecipient_ == nullptr) {
96 dpDeathRecipient_ = sptr<IRemoteObject::DeathRecipient>(new DeviceProfileDeathRecipient);
97 }
98 if (remoteObject != nullptr) {
99 remoteObject->AddDeathRecipient(dpDeathRecipient_);
100 dpProxy_ = iface_cast<IDistributedDeviceProfile>(remoteObject);
101 proxyConVar_.notify_one();
102 DpRadarHelper::GetInstance().SetDeviceProfileInit(true);
103 }
104 }
105
LoadSystemAbilityFail()106 void DistributedDeviceProfileClient::LoadSystemAbilityFail()
107 {
108 int32_t stageRes = static_cast<int32_t>(StageRes::STAGE_FAIL);
109 DpRadarHelper::GetInstance().ReportLoadDpSaCb(stageRes);
110 std::lock_guard<std::mutex> lock(serviceLock_);
111 dpProxy_ = nullptr;
112 }
113
SendSubscribeInfosToService()114 void DistributedDeviceProfileClient::SendSubscribeInfosToService()
115 {
116 auto dpService = GetDeviceProfileService();
117 if (dpService == nullptr) {
118 HILOGE("dpService is nullptr!");
119 return;
120 }
121 {
122 std::lock_guard<std::mutex> lock(subscribeLock_);
123 if (subscribeInfos_.empty() || subscribeInfos_.size() > MAX_SUBSCRIBE_INFO_SIZE) {
124 HILOGE("SubscribeInfos size is invalid!size: %{public}zu!", subscribeInfos_.size());
125 return;
126 }
127 dpService->SendSubscribeInfos(subscribeInfos_);
128 }
129 }
130
PutAccessControlProfile(const AccessControlProfile & accessControlProfile)131 int32_t DistributedDeviceProfileClient::PutAccessControlProfile(const AccessControlProfile& accessControlProfile)
132 {
133 auto dpService = GetDeviceProfileService();
134 if (dpService == nullptr) {
135 HILOGE("get dp service failed");
136 return DP_GET_SERVICE_FAILED;
137 }
138 int32_t ret = dpService->PutAccessControlProfile(accessControlProfile);
139 if (retryErrCodes_.count(ret) != 0) {
140 ret = RetryClientRequest(ret, &IDistributedDeviceProfile::PutAccessControlProfile, accessControlProfile);
141 }
142 return ret;
143 }
144
UpdateAccessControlProfile(const AccessControlProfile & accessControlProfile)145 int32_t DistributedDeviceProfileClient::UpdateAccessControlProfile(const AccessControlProfile& accessControlProfile)
146 {
147 auto dpService = GetDeviceProfileService();
148 if (dpService == nullptr) {
149 HILOGE("get dp service failed");
150 return DP_GET_SERVICE_FAILED;
151 }
152 int32_t ret = dpService->UpdateAccessControlProfile(accessControlProfile);
153 if (retryErrCodes_.count(ret) != 0) {
154 ret = RetryClientRequest(ret, &IDistributedDeviceProfile::UpdateAccessControlProfile, accessControlProfile);
155 }
156 return ret;
157 }
158
GetTrustDeviceProfile(const std::string & deviceId,TrustDeviceProfile & trustDeviceProfile)159 int32_t DistributedDeviceProfileClient::GetTrustDeviceProfile(const std::string& deviceId,
160 TrustDeviceProfile& trustDeviceProfile)
161 {
162 auto dpService = GetDeviceProfileService();
163 if (dpService == nullptr) {
164 HILOGE("get dp service failed");
165 return DP_GET_SERVICE_FAILED;
166 }
167 int32_t ret = dpService->GetTrustDeviceProfile(deviceId, trustDeviceProfile);
168 if (retryErrCodes_.count(ret) != 0) {
169 ret = RetryClientRequest(ret, &IDistributedDeviceProfile::GetTrustDeviceProfile, deviceId, trustDeviceProfile);
170 }
171 return ret;
172 }
173
GetAllTrustDeviceProfile(std::vector<TrustDeviceProfile> & trustDeviceProfiles)174 int32_t DistributedDeviceProfileClient::GetAllTrustDeviceProfile(std::vector<TrustDeviceProfile>& trustDeviceProfiles)
175 {
176 auto dpService = GetDeviceProfileService();
177 if (dpService == nullptr) {
178 HILOGE("get dp service failed");
179 return DP_GET_SERVICE_FAILED;
180 }
181 int32_t ret = dpService->GetAllTrustDeviceProfile(trustDeviceProfiles);
182 if (retryErrCodes_.count(ret) != 0) {
183 ret = RetryClientRequest(ret, &IDistributedDeviceProfile::GetAllTrustDeviceProfile, trustDeviceProfiles);
184 }
185 return ret;
186 }
187
GetAccessControlProfile(std::map<std::string,std::string> params,std::vector<AccessControlProfile> & accessControlProfiles)188 int32_t DistributedDeviceProfileClient::GetAccessControlProfile(std::map<std::string, std::string> params,
189 std::vector<AccessControlProfile>& accessControlProfiles)
190 {
191 auto dpService = GetDeviceProfileService();
192 if (dpService == nullptr) {
193 HILOGE("Get dp service failed");
194 return DP_GET_SERVICE_FAILED;
195 }
196 if (params.empty() || params.size() > MAX_PARAM_SIZE) {
197 HILOGE("Params size is invalid! size: %{public}zu!", params.size());
198 return DP_INVALID_PARAMS;
199 }
200 int32_t ret = dpService->GetAccessControlProfile(params, accessControlProfiles);
201 if (retryErrCodes_.count(ret) != 0) {
202 ret = RetryClientRequest(ret, &IDistributedDeviceProfile::GetAccessControlProfile, params,
203 accessControlProfiles);
204 }
205 return ret;
206 }
207
GetAllAccessControlProfile(std::vector<AccessControlProfile> & accessControlProfiles)208 int32_t DistributedDeviceProfileClient::GetAllAccessControlProfile(
209 std::vector<AccessControlProfile>& accessControlProfiles)
210 {
211 auto dpService = GetDeviceProfileService();
212 if (dpService == nullptr) {
213 HILOGE("Get dp service failed");
214 return DP_GET_SERVICE_FAILED;
215 }
216 int32_t ret = dpService->GetAllAccessControlProfile(accessControlProfiles);
217 if (retryErrCodes_.count(ret) != 0) {
218 ret = RetryClientRequest(ret, &IDistributedDeviceProfile::GetAllAccessControlProfile, accessControlProfiles);
219 }
220 return ret;
221 }
222
GetAllAclIncludeLnnAcl(std::vector<AccessControlProfile> & accessControlProfiles)223 int32_t DistributedDeviceProfileClient::GetAllAclIncludeLnnAcl(std::vector<AccessControlProfile>& accessControlProfiles)
224 {
225 auto dpService = GetDeviceProfileService();
226 if (dpService == nullptr) {
227 HILOGE("Get dp service failed");
228 return DP_GET_SERVICE_FAILED;
229 }
230 int32_t ret = dpService->GetAllAclIncludeLnnAcl(accessControlProfiles);
231 if (retryErrCodes_.count(ret) != 0) {
232 ret = RetryClientRequest(ret, &IDistributedDeviceProfile::GetAllAclIncludeLnnAcl, accessControlProfiles);
233 }
234 return ret;
235 }
236
DeleteAccessControlProfile(int32_t accessControlId)237 int32_t DistributedDeviceProfileClient::DeleteAccessControlProfile(int32_t accessControlId)
238 {
239 auto dpService = GetDeviceProfileService();
240 if (dpService == nullptr) {
241 HILOGE("Get dp service failed");
242 return DP_GET_SERVICE_FAILED;
243 }
244 int32_t ret = dpService->DeleteAccessControlProfile(accessControlId);
245 if (retryErrCodes_.count(ret) != 0) {
246 ret = RetryClientRequest(ret, &IDistributedDeviceProfile::DeleteAccessControlProfile, accessControlId);
247 }
248 return ret;
249 }
250
PutDeviceProfileBatch(std::vector<DeviceProfile> & deviceProfiles)251 int32_t DistributedDeviceProfileClient::PutDeviceProfileBatch(std::vector<DeviceProfile>& deviceProfiles)
252 {
253 auto dpService = GetDeviceProfileService();
254 if (dpService == nullptr) {
255 HILOGE("Get dp service failed");
256 return DP_GET_SERVICE_FAILED;
257 }
258 return dpService->PutDeviceProfileBatch(deviceProfiles);
259 }
260
DeleteDeviceProfileBatch(std::vector<DeviceProfile> & deviceProfiles)261 int32_t DistributedDeviceProfileClient::DeleteDeviceProfileBatch(std::vector<DeviceProfile>& deviceProfiles)
262 {
263 auto dpService = GetDeviceProfileService();
264 if (dpService == nullptr) {
265 HILOGE("Get dp service failed");
266 return DP_GET_SERVICE_FAILED;
267 }
268 return dpService->DeleteDeviceProfileBatch(deviceProfiles);
269 }
270
PutServiceProfile(const ServiceProfile & serviceProfile)271 int32_t DistributedDeviceProfileClient::PutServiceProfile(const ServiceProfile& serviceProfile)
272 {
273 auto dpService = GetDeviceProfileService();
274 if (dpService == nullptr) {
275 HILOGE("Get dp service failed");
276 return DP_GET_SERVICE_FAILED;
277 }
278 return dpService->PutServiceProfile(serviceProfile);
279 }
280
PutServiceInfoProfile(const ServiceInfoProfile & serviceInfoProfile)281 int32_t DistributedDeviceProfileClient::PutServiceInfoProfile(const ServiceInfoProfile& serviceInfoProfile)
282 {
283 auto dpService = GetDeviceProfileService();
284 if (dpService == nullptr) {
285 HILOGE("Get dp service failed");
286 return DP_GET_SERVICE_FAILED;
287 }
288 return dpService->PutServiceInfoProfile(serviceInfoProfile);
289 }
290
DeleteServiceInfoProfile(const ServiceInfoUniqueKey & key)291 int32_t DistributedDeviceProfileClient::DeleteServiceInfoProfile(const ServiceInfoUniqueKey& key)
292 {
293 auto dpService = GetDeviceProfileService();
294 if (dpService == nullptr) {
295 HILOGE("Get dp service failed");
296 return DP_GET_SERVICE_FAILED;
297 }
298 return dpService->DeleteServiceInfoProfile(key);
299 }
300
UpdateServiceInfoProfile(const ServiceInfoProfile & serviceInfoProfile)301 int32_t DistributedDeviceProfileClient::UpdateServiceInfoProfile(const ServiceInfoProfile& serviceInfoProfile)
302 {
303 auto dpService = GetDeviceProfileService();
304 if (dpService == nullptr) {
305 HILOGE("Get dp service failed");
306 return DP_GET_SERVICE_FAILED;
307 }
308 return dpService->UpdateServiceInfoProfile(serviceInfoProfile);
309 }
310
GetServiceInfoProfileByUniqueKey(const ServiceInfoUniqueKey & key,ServiceInfoProfile & serviceInfoProfile)311 int32_t DistributedDeviceProfileClient::GetServiceInfoProfileByUniqueKey(const ServiceInfoUniqueKey& key,
312 ServiceInfoProfile& serviceInfoProfile)
313 {
314 auto dpService = GetDeviceProfileService();
315 if (dpService == nullptr) {
316 HILOGE("Get dp service failed");
317 return DP_GET_SERVICE_FAILED;
318 }
319 return dpService->GetServiceInfoProfileByUniqueKey(key, serviceInfoProfile);
320 }
321
GetServiceInfoProfileListByTokenId(const ServiceInfoUniqueKey & key,std::vector<ServiceInfoProfile> & serviceInfoProfiles)322 int32_t DistributedDeviceProfileClient::GetServiceInfoProfileListByTokenId(const ServiceInfoUniqueKey& key,
323 std::vector<ServiceInfoProfile>& serviceInfoProfiles)
324 {
325 auto dpService = GetDeviceProfileService();
326 if (dpService == nullptr) {
327 HILOGE("Get dp service failed");
328 return DP_GET_SERVICE_FAILED;
329 }
330 return dpService->GetServiceInfoProfileListByTokenId(key, serviceInfoProfiles);
331 }
332
GetAllServiceInfoProfileList(std::vector<ServiceInfoProfile> & serviceInfoProfiles)333 int32_t DistributedDeviceProfileClient::GetAllServiceInfoProfileList(
334 std::vector<ServiceInfoProfile>& serviceInfoProfiles)
335 {
336 auto dpService = GetDeviceProfileService();
337 if (dpService == nullptr) {
338 HILOGE("Get dp service failed");
339 return DP_GET_SERVICE_FAILED;
340 }
341 return dpService->GetAllServiceInfoProfileList(serviceInfoProfiles);
342 }
343
GetServiceInfoProfileListByBundleName(const ServiceInfoUniqueKey & key,std::vector<ServiceInfoProfile> & serviceInfoProfiles)344 int32_t DistributedDeviceProfileClient::GetServiceInfoProfileListByBundleName(const ServiceInfoUniqueKey& key,
345 std::vector<ServiceInfoProfile>& serviceInfoProfiles)
346 {
347 auto dpService = GetDeviceProfileService();
348 if (dpService == nullptr) {
349 HILOGE("Get dp service failed");
350 return DP_GET_SERVICE_FAILED;
351 }
352 return dpService->GetServiceInfoProfileListByBundleName(key, serviceInfoProfiles);
353 }
354
PutServiceProfileBatch(const std::vector<ServiceProfile> & serviceProfiles)355 int32_t DistributedDeviceProfileClient::PutServiceProfileBatch(const std::vector<ServiceProfile>& serviceProfiles)
356 {
357 auto dpService = GetDeviceProfileService();
358 if (dpService == nullptr) {
359 HILOGE("Get dp service failed");
360 return DP_GET_SERVICE_FAILED;
361 }
362 if (serviceProfiles.empty() || serviceProfiles.size() > MAX_PROFILE_SIZE) {
363 HILOGE("ServiceProfiles size is invalid!size: %{public}zu!", serviceProfiles.size());
364 return DP_INVALID_PARAMS;
365 }
366 return dpService->PutServiceProfileBatch(serviceProfiles);
367 }
368
PutCharacteristicProfile(const CharacteristicProfile & characteristicProfile)369 int32_t DistributedDeviceProfileClient::PutCharacteristicProfile(const CharacteristicProfile& characteristicProfile)
370 {
371 auto dpService = GetDeviceProfileService();
372 if (dpService == nullptr) {
373 HILOGE("Get dp service failed");
374 return DP_GET_SERVICE_FAILED;
375 }
376 return dpService->PutCharacteristicProfile(characteristicProfile);
377 }
378
PutCharacteristicProfileBatch(const std::vector<CharacteristicProfile> & characteristicProfiles)379 int32_t DistributedDeviceProfileClient::PutCharacteristicProfileBatch(
380 const std::vector<CharacteristicProfile>& characteristicProfiles)
381 {
382 auto dpService = GetDeviceProfileService();
383 if (dpService == nullptr) {
384 HILOGE("Get dp service failed");
385 return DP_GET_SERVICE_FAILED;
386 }
387 if (characteristicProfiles.empty() || characteristicProfiles.size() > MAX_PROFILE_SIZE) {
388 HILOGE("ServiceProfiles size is invalid!size: %{public}zu!", characteristicProfiles.size());
389 return DP_INVALID_PARAMS;
390 }
391 return dpService->PutCharacteristicProfileBatch(characteristicProfiles);
392 }
393
GetDeviceProfile(const std::string & deviceId,DeviceProfile & deviceProfile)394 int32_t DistributedDeviceProfileClient::GetDeviceProfile(const std::string& deviceId, DeviceProfile& deviceProfile)
395 {
396 auto dpService = GetDeviceProfileService();
397 if (dpService == nullptr) {
398 HILOGE("Get dp service failed");
399 return DP_GET_SERVICE_FAILED;
400 }
401 return dpService->GetDeviceProfile(deviceId, deviceProfile);
402 }
403
GetDeviceProfiles(DeviceProfileFilterOptions & options,std::vector<DeviceProfile> & deviceProfiles)404 int32_t DistributedDeviceProfileClient::GetDeviceProfiles(DeviceProfileFilterOptions& options,
405 std::vector<DeviceProfile>& deviceProfiles)
406 {
407 auto dpService = GetDeviceProfileService();
408 if (dpService == nullptr) {
409 HILOGE("Get dp service failed");
410 return DP_GET_SERVICE_FAILED;
411 }
412 return dpService->GetDeviceProfiles(options, deviceProfiles);
413 }
414
GetServiceProfile(const std::string & deviceId,const std::string & serviceName,ServiceProfile & serviceProfile)415 int32_t DistributedDeviceProfileClient::GetServiceProfile(const std::string& deviceId, const std::string& serviceName,
416 ServiceProfile& serviceProfile)
417 {
418 auto dpService = GetDeviceProfileService();
419 if (dpService == nullptr) {
420 HILOGE("Get dp service failed");
421 return DP_GET_SERVICE_FAILED;
422 }
423 return dpService->GetServiceProfile(deviceId, serviceName, serviceProfile);
424 }
425
426
PutProductInfoBatch(const std::vector<ProductInfo> & productInfos)427 int32_t DistributedDeviceProfileClient::PutProductInfoBatch(const std::vector<ProductInfo>& productInfos)
428 {
429 auto dpService = GetDeviceProfileService();
430 if (dpService == nullptr) {
431 HILOGE("Get dp service failed");
432 return DP_GET_SERVICE_FAILED;
433 }
434 return dpService->PutProductInfoBatch(productInfos);
435 }
436
PutDeviceIconInfoBatch(const std::vector<DeviceIconInfo> & deviceIconInfos)437 int32_t DistributedDeviceProfileClient::PutDeviceIconInfoBatch(const std::vector<DeviceIconInfo>& deviceIconInfos)
438 {
439 auto dpService = GetDeviceProfileService();
440 if (dpService == nullptr) {
441 HILOGE("Get dp service failed");
442 return DP_GET_SERVICE_FAILED;
443 }
444 return dpService->PutDeviceIconInfoBatch(deviceIconInfos);
445 }
446
GetDeviceIconInfos(const DeviceIconInfoFilterOptions & filterOptions,std::vector<DeviceIconInfo> & deviceIconInfos)447 int32_t DistributedDeviceProfileClient::GetDeviceIconInfos(const DeviceIconInfoFilterOptions& filterOptions,
448 std::vector<DeviceIconInfo>& deviceIconInfos)
449 {
450 auto dpService = GetDeviceProfileService();
451 if (dpService == nullptr) {
452 HILOGE("Get dp service failed");
453 return DP_GET_SERVICE_FAILED;
454 }
455 return dpService->GetDeviceIconInfos(filterOptions, deviceIconInfos);
456 }
457
GetCharacteristicProfile(const std::string & deviceId,const std::string & serviceName,const std::string & characteristicId,CharacteristicProfile & characteristicProfile)458 int32_t DistributedDeviceProfileClient::GetCharacteristicProfile(const std::string& deviceId,
459 const std::string& serviceName, const std::string& characteristicId, CharacteristicProfile& characteristicProfile)
460 {
461 auto dpService = GetDeviceProfileService();
462 if (dpService == nullptr) {
463 HILOGE("Get dp service failed");
464 return DP_GET_SERVICE_FAILED;
465 }
466 return dpService->GetCharacteristicProfile(deviceId, serviceName, characteristicId, characteristicProfile);
467 }
468
DeleteServiceProfile(const std::string & deviceId,const std::string & serviceName,bool isMultiUser,int32_t userId)469 int32_t DistributedDeviceProfileClient::DeleteServiceProfile(const std::string& deviceId,
470 const std::string& serviceName, bool isMultiUser, int32_t userId)
471 {
472 auto dpService = GetDeviceProfileService();
473 if (dpService == nullptr) {
474 HILOGE("Get dp service failed");
475 return DP_GET_SERVICE_FAILED;
476 }
477 return dpService->DeleteServiceProfile(deviceId, serviceName, isMultiUser, userId);
478 }
479
DeleteCharacteristicProfile(const std::string & deviceId,const std::string & serviceName,const std::string & characteristicKey,bool isMultiUser,int32_t userId)480 int32_t DistributedDeviceProfileClient::DeleteCharacteristicProfile(const std::string& deviceId,
481 const std::string& serviceName, const std::string& characteristicKey, bool isMultiUser, int32_t userId)
482 {
483 auto dpService = GetDeviceProfileService();
484 if (dpService == nullptr) {
485 HILOGE("Get dp service failed");
486 return DP_GET_SERVICE_FAILED;
487 }
488 return dpService->DeleteCharacteristicProfile(deviceId, serviceName, characteristicKey, isMultiUser, userId);
489 }
490
SubscribeDeviceProfile(const SubscribeInfo & subscribeInfo)491 int32_t DistributedDeviceProfileClient::SubscribeDeviceProfile(const SubscribeInfo& subscribeInfo)
492 {
493 SubscribeDeviceProfileSA();
494 auto dpService = GetDeviceProfileService();
495 if (dpService == nullptr) {
496 HILOGE("Get dp service failed");
497 return DP_GET_SERVICE_FAILED;
498 }
499 {
500 std::lock_guard<std::mutex> lock(subscribeLock_);
501 if (subscribeInfos_.size() > MAX_LISTENER_SIZE) {
502 HILOGE("ProfileChangeListeners size is invalid!size: %{public}zu!", subscribeInfos_.size());
503 return DP_EXCEED_MAX_SIZE_FAIL;
504 }
505 std::string subscribeTag =
506 subscribeInfo.GetSubscribeKey() + SEPARATOR + std::to_string(subscribeInfo.GetSaId());
507 subscribeInfos_[subscribeTag] = subscribeInfo;
508 HILOGI("subscribeInfos_.size is %{public}zu", subscribeInfos_.size());
509 }
510 return dpService->SubscribeDeviceProfile(subscribeInfo);
511 }
512
UnSubscribeDeviceProfile(const SubscribeInfo & subscribeInfo)513 int32_t DistributedDeviceProfileClient::UnSubscribeDeviceProfile(const SubscribeInfo& subscribeInfo)
514 {
515 SubscribeDeviceProfileSA();
516 auto dpService = GetDeviceProfileService();
517 if (dpService == nullptr) {
518 HILOGE("Get dp service failed");
519 return DP_GET_SERVICE_FAILED;
520 }
521 {
522 std::lock_guard<std::mutex> lock(subscribeLock_);
523 subscribeInfos_.erase(subscribeInfo.GetSubscribeKey() + SEPARATOR + std::to_string(subscribeInfo.GetSaId()));
524 HILOGI("subscribeInfos_.size is %{public}zu", subscribeInfos_.size());
525 }
526 return dpService->UnSubscribeDeviceProfile(subscribeInfo);
527 }
528
SyncDeviceProfile(const DpSyncOptions & syncOptions,sptr<ISyncCompletedCallback> syncCb)529 int32_t DistributedDeviceProfileClient::SyncDeviceProfile(const DpSyncOptions& syncOptions,
530 sptr<ISyncCompletedCallback> syncCb)
531 {
532 auto dpService = GetDeviceProfileService();
533 if (dpService == nullptr) {
534 HILOGE("Get dp service failed");
535 return DP_GET_SERVICE_FAILED;
536 }
537 if (syncCb == nullptr) {
538 HILOGE("SyncCb is nullptr!");
539 return DP_SYNC_DEVICE_FAIL;
540 }
541 sptr<IRemoteObject> syncCompletedCallback = syncCb->AsObject();
542 if (syncCompletedCallback == nullptr) {
543 HILOGE("SyncCb ipc cast fail!");
544 return DP_SYNC_DEVICE_FAIL;
545 }
546 return dpService->SyncDeviceProfile(syncOptions, syncCompletedCallback);
547 }
548
GetDeviceProfileService()549 sptr<IDistributedDeviceProfile> DistributedDeviceProfileClient::GetDeviceProfileService()
550 {
551 {
552 std::lock_guard<std::mutex> lock(serviceLock_);
553 if (dpProxy_ != nullptr) {
554 return dpProxy_;
555 }
556 auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
557 if (samgrProxy == nullptr) {
558 HILOGE("get samgr failed");
559 return nullptr;
560 }
561 auto object = samgrProxy->CheckSystemAbility(DISTRIBUTED_DEVICE_PROFILE_SA_ID);
562 int32_t stageRes = (object != nullptr) ?
563 static_cast<int32_t>(StageRes::STAGE_SUCC) : static_cast<int32_t>(StageRes::STAGE_FAIL);
564 if (!DpRadarHelper::GetInstance().IsDeviceProfileInit()) {
565 DpRadarHelper::GetInstance().ReportCheckDpSa(stageRes);
566 }
567 if (object != nullptr) {
568 HILOGD("get service succeeded");
569 if (dpDeathRecipient_ == nullptr) {
570 dpDeathRecipient_ = sptr<IRemoteObject::DeathRecipient>(new DeviceProfileDeathRecipient);
571 }
572 object->AddDeathRecipient(dpDeathRecipient_);
573 dpProxy_ = iface_cast<IDistributedDeviceProfile>(object);
574 return dpProxy_;
575 }
576 }
577
578 HILOGW("remoteObject is null!");
579 if (LoadDeviceProfileService()) {
580 std::lock_guard<std::mutex> lock(serviceLock_);
581 if (dpProxy_ != nullptr) {
582 return dpProxy_;
583 } else {
584 HILOGE("load dp service failed");
585 return nullptr;
586 }
587 }
588 HILOGE("load dp service failed");
589 return nullptr;
590 }
591
OnServiceDied(const sptr<IRemoteObject> & remote)592 void DistributedDeviceProfileClient::OnServiceDied(const sptr<IRemoteObject>& remote)
593 {
594 HILOGI("called");
595 DpRadarHelper::GetInstance().SetDeviceProfileInit(false);
596 std::lock_guard<std::mutex> lock(serviceLock_);
597 dpProxy_ = nullptr;
598 }
599
OnRemoteDied(const wptr<IRemoteObject> & remote)600 void DistributedDeviceProfileClient::DeviceProfileDeathRecipient::OnRemoteDied(const wptr<IRemoteObject>& remote)
601 {
602 HILOGI("call!");
603 DistributedDeviceProfileClient::GetInstance().OnServiceDied(remote.promote());
604 }
605
SubscribeDeviceProfileSA()606 void DistributedDeviceProfileClient::SubscribeDeviceProfileSA()
607 {
608 auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
609 if (samgrProxy == nullptr) {
610 HILOGE("get samgr failed");
611 return;
612 }
613 int32_t ret = DP_SUCCESS;
614 {
615 std::lock_guard<std::mutex> lock(saListenerLock_);
616 if (saListenerCallback_ == nullptr) {
617 saListenerCallback_ = sptr<SystemAbilityListener>(new SystemAbilityListener());
618 }
619 if (saListenerCallback_ == nullptr) {
620 HILOGE("Create saListenerCallback failed!");
621 return;
622 }
623 ret = samgrProxy->SubscribeSystemAbility(DISTRIBUTED_DEVICE_PROFILE_SA_ID, saListenerCallback_);
624 }
625 if (ret != DP_SUCCESS) {
626 HILOGE("subscribe dp sa failed! ret %{public}d.", ret);
627 return;
628 }
629 HILOGD("subscribe dp sa success");
630 }
631
StartThreadSendSubscribeInfos()632 void DistributedDeviceProfileClient::StartThreadSendSubscribeInfos()
633 {
634 HILOGI("Send SubscribeInfos cache in proxy to service!");
635 std::thread(&DistributedDeviceProfileClient::SendSubscribeInfosToService, this).detach();
636 }
637
ReSubscribeDeviceProfileInited()638 void DistributedDeviceProfileClient::ReSubscribeDeviceProfileInited()
639 {
640 int32_t saId = 0;
641 sptr<IDpInitedCallback> dpInitedCallback = nullptr;
642 {
643 std::lock_guard<std::mutex> lock(dpInitedLock_);
644 if (dpInitedCallback_ == nullptr) {
645 HILOGI("not use Retry subscribe dp inited");
646 return;
647 }
648 saId = saId_;
649 dpInitedCallback = dpInitedCallback_;
650 }
651 auto autoTask = [this, saId, dpInitedCallback] () {
652 int32_t ret = SubscribeDeviceProfileInited(saId, dpInitedCallback);
653 if (ret != DP_SUCCESS) {
654 HILOGE("Retry subscribe dp inited failed");
655 } else {
656 HILOGD("Retry subscribe dp inited succeed");
657 }
658 };
659 std::thread(autoTask).detach();
660 }
661
ReSubscribePinCodeInvalid()662 void DistributedDeviceProfileClient::ReSubscribePinCodeInvalid()
663 {
664 HILOGI("call");
665 std::string bundleName = "";
666 int32_t pinExchangeType = DEFAULT_PIN_EXCHANGE_TYPE;
667 sptr<IPincodeInvalidCallback> pinCodeCallback = nullptr;
668 {
669 std::lock_guard<std::mutex> lock(pinCodeLock_);
670 if (pinCodeCallback_ == nullptr) {
671 HILOGI("not use Retry subscribe pincode invalid");
672 return;
673 }
674 bundleName = bundleName_;
675 pinExchangeType = pinExchangeType_;
676 pinCodeCallback = pinCodeCallback_;
677 }
678 int32_t ret = SubscribePinCodeInvalid(bundleName, pinExchangeType, pinCodeCallback);
679 if (ret != DP_SUCCESS) {
680 HILOGE("Retry subscribe pincode invalid failed");
681 } else {
682 HILOGI("Retry subscribe pincode invalid succeed");
683 }
684 }
685
StartThreadReSubscribePinCodeInvalid()686 void DistributedDeviceProfileClient::StartThreadReSubscribePinCodeInvalid()
687 {
688 HILOGI("Send Subscribe pincode invalid cache in proxy to service!");
689 std::thread(&DistributedDeviceProfileClient::ReSubscribePinCodeInvalid, this).detach();
690 }
691
SubscribeDeviceProfileInited(int32_t saId,sptr<IDpInitedCallback> initedCb)692 int32_t DistributedDeviceProfileClient::SubscribeDeviceProfileInited(int32_t saId,
693 sptr<IDpInitedCallback> initedCb)
694 {
695 HILOGI("enter, saId:%{public}d", saId);
696 SubscribeDeviceProfileSA();
697 auto dpService = GetDeviceProfileService();
698 if (dpService == nullptr) {
699 HILOGE("Get dp service failed");
700 return DP_SUBSCRIBE_INITED_FALI;
701 }
702 if (saId <= 0 || saId > MAX_SAID) {
703 HILOGE("saId is invalid, saId:%{public}d", saId);
704 return DP_INVALID_PARAM;
705 }
706 if (initedCb == nullptr) {
707 HILOGE("initedCb is nullptr!");
708 return DP_INVALID_PARAM;
709 }
710 sptr<IRemoteObject> dpInitedCallback = initedCb->AsObject();
711 if (dpInitedCallback == nullptr) {
712 HILOGE("SyncCb ipc cast fail!");
713 return DP_SUBSCRIBE_INITED_FALI;
714 }
715 {
716 std::lock_guard<std::mutex> lock(dpInitedLock_);
717 int32_t ret = dpService->SubscribeDeviceProfileInited(saId, dpInitedCallback);
718 if (ret != DP_SUCCESS) {
719 HILOGE("Subscribe DP Inited failed!");
720 return ret;
721 }
722 saId_ = saId;
723 dpInitedCallback_ = initedCb;
724 }
725 HILOGD("Subscribe DP Inited succeed!");
726 return DP_SUCCESS;
727 }
728
UnSubscribeDeviceProfileInited(int32_t saId)729 int32_t DistributedDeviceProfileClient::UnSubscribeDeviceProfileInited(int32_t saId)
730 {
731 HILOGI("enter");
732 {
733 std::lock_guard<std::mutex> lock(dpInitedLock_);
734 if (dpInitedCallback_ == nullptr) {
735 HILOGE("not subscribe dp inited, no need unsubscribe dp inited");
736 return DP_SUCCESS;
737 }
738 }
739 SubscribeDeviceProfileSA();
740 auto dpService = GetDeviceProfileService();
741 if (dpService == nullptr) {
742 HILOGE("Get dp service failed");
743 return DP_GET_SERVICE_FAILED;
744 }
745 if (saId <= 0 || saId > MAX_SAID) {
746 HILOGE("saId is invalid, saId:%{public}d", saId);
747 return DP_INVALID_PARAM;
748 }
749 {
750 std::lock_guard<std::mutex> lock(dpInitedLock_);
751 int32_t ret = dpService->UnSubscribeDeviceProfileInited(saId);
752 if (ret != DP_SUCCESS) {
753 HILOGE("Unsubscribe DP Inited failed!");
754 return ret;
755 }
756 dpInitedCallback_ = nullptr;
757 }
758 HILOGD("Unsubscribe DP Inited succeed!");
759 return DP_SUCCESS;
760 }
761
SubscribePinCodeInvalid(const std::string & bundleName,int32_t pinExchangeType,sptr<IPincodeInvalidCallback> pinCodeCallback)762 int32_t DistributedDeviceProfileClient::SubscribePinCodeInvalid(const std::string& bundleName, int32_t pinExchangeType,
763 sptr<IPincodeInvalidCallback> pinCodeCallback)
764 {
765 HILOGI("enter");
766 SubscribeDeviceProfileSA();
767 auto dpService = GetDeviceProfileService();
768 if (dpService == nullptr) {
769 HILOGE("Get dp service failed");
770 return DP_SUBSCRIBE_INITED_FALI;
771 }
772 if (bundleName.empty()) {
773 HILOGE("tokenId is invalid");
774 return DP_INVALID_PARAM;
775 }
776 if (pinExchangeType == DEFAULT_PIN_EXCHANGE_TYPE) {
777 HILOGE("pinExchangeType is invalid");
778 return DP_INVALID_PARAM;
779 }
780 if (pinCodeCallback == nullptr) {
781 HILOGE("pinCodeCallback is nullptr!");
782 return DP_INVALID_PARAM;
783 }
784 sptr<IRemoteObject> innerPinCodeCallback = pinCodeCallback->AsObject();
785 if (innerPinCodeCallback == nullptr) {
786 HILOGE("pinCodeCallback ipc cast fail!");
787 return DP_SUBSCRIBE_PINCODE_INVALID;
788 }
789 {
790 std::lock_guard<std::mutex> lock(pinCodeLock_);
791 int32_t ret = dpService->SubscribePinCodeInvalid(bundleName, pinExchangeType, innerPinCodeCallback);
792 if (ret != DP_SUCCESS) {
793 HILOGE("Subscribe DP Inited failed!");
794 return ret;
795 }
796 bundleName_ = bundleName;
797 pinExchangeType_ = pinExchangeType;
798 pinCodeCallback_ = pinCodeCallback;
799 }
800 HILOGI("Subscribe pincodeInvalid succeed!");
801 return DP_SUCCESS;
802 }
803
UnSubscribePinCodeInvalid(const std::string & bundleName,int32_t pinExchangeType)804 int32_t DistributedDeviceProfileClient::UnSubscribePinCodeInvalid(const std::string& bundleName,
805 int32_t pinExchangeType)
806 {
807 HILOGI("enter");
808 if (pinCodeCallback_ == nullptr) {
809 HILOGE("not subscribe pincode invalid, no need unsubscribe pincode invalid");
810 return DP_SUCCESS;
811 }
812 SubscribeDeviceProfileSA();
813 auto dpService = GetDeviceProfileService();
814 if (dpService == nullptr) {
815 HILOGE("Get dp service failed");
816 return DP_GET_SERVICE_FAILED;
817 }
818 if (bundleName.empty()) {
819 HILOGE("tokenId is invalid");
820 return DP_INVALID_PARAM;
821 }
822 if (pinExchangeType == DEFAULT_PIN_EXCHANGE_TYPE) {
823 HILOGE("pinExchangeType is invalid");
824 return DP_INVALID_PARAM;
825 }
826 {
827 std::lock_guard<std::mutex> lock(pinCodeLock_);
828 int32_t ret = dpService->UnSubscribePinCodeInvalid(bundleName, pinExchangeType);
829 if (ret != DP_SUCCESS) {
830 HILOGE("Unsubscribe DP Inited failed!");
831 return ret;
832 }
833 pinCodeCallback_ = nullptr;
834 }
835 HILOGD("Unsubscribe pincode invalid succeed!");
836 return DP_SUCCESS;
837 }
838
PutAllTrustedDevices(const std::vector<TrustedDeviceInfo> & deviceInfos)839 int32_t DistributedDeviceProfileClient::PutAllTrustedDevices(const std::vector<TrustedDeviceInfo>& deviceInfos)
840 {
841 auto dpService = GetDeviceProfileService();
842 if (dpService == nullptr) {
843 HILOGE("Get dp service failed");
844 return DP_GET_SERVICE_FAILED;
845 }
846 if (deviceInfos.empty() || deviceInfos.size() > MAX_TRUSTED_DEVICE_SIZE) {
847 HILOGE("size is invalid! size: %{public}zu!", deviceInfos.size());
848 return DP_INVALID_PARAMS;
849 }
850 int32_t ret = dpService->PutAllTrustedDevices(deviceInfos);
851 if (retryErrCodes_.count(ret) != 0) {
852 ret = RetryClientRequest(ret, &IDistributedDeviceProfile::PutAllTrustedDevices, deviceInfos);
853 }
854 return ret;
855 }
856
PutSessionKey(uint32_t userId,const std::vector<uint8_t> & sessionKey,int32_t & sessionKeyId)857 int32_t DistributedDeviceProfileClient::PutSessionKey(
858 uint32_t userId, const std::vector<uint8_t>& sessionKey, int32_t& sessionKeyId)
859 {
860 auto dpService = GetDeviceProfileService();
861 if (dpService == nullptr) {
862 HILOGE("get dp service failed");
863 return DP_GET_SERVICE_FAILED;
864 }
865 int32_t ret = dpService->PutSessionKey(userId, sessionKey, sessionKeyId);
866 if (retryErrCodes_.count(ret) != 0) {
867 ret = RetryClientRequest(ret, &IDistributedDeviceProfile::PutSessionKey, userId, sessionKey, sessionKeyId);
868 }
869 return ret;
870 }
871
GetSessionKey(uint32_t userId,int32_t sessionKeyId,std::vector<uint8_t> & sessionKey)872 int32_t DistributedDeviceProfileClient::GetSessionKey(
873 uint32_t userId, int32_t sessionKeyId, std::vector<uint8_t>& sessionKey)
874 {
875 auto dpService = GetDeviceProfileService();
876 if (dpService == nullptr) {
877 HILOGE("get dp service failed");
878 return DP_GET_SERVICE_FAILED;
879 }
880 return dpService->GetSessionKey(userId, sessionKeyId, sessionKey);
881 }
882
UpdateSessionKey(uint32_t userId,int32_t sessionKeyId,const std::vector<uint8_t> & sessionKey)883 int32_t DistributedDeviceProfileClient::UpdateSessionKey(
884 uint32_t userId, int32_t sessionKeyId, const std::vector<uint8_t>& sessionKey)
885 {
886 auto dpService = GetDeviceProfileService();
887 if (dpService == nullptr) {
888 HILOGE("get dp service failed");
889 return DP_GET_SERVICE_FAILED;
890 }
891 return dpService->UpdateSessionKey(userId, sessionKeyId, sessionKey);
892 }
893
DeleteSessionKey(uint32_t userId,int32_t sessionKeyId)894 int32_t DistributedDeviceProfileClient::DeleteSessionKey(uint32_t userId, int32_t sessionKeyId)
895 {
896 auto dpService = GetDeviceProfileService();
897 if (dpService == nullptr) {
898 HILOGE("get dp service failed");
899 return DP_GET_SERVICE_FAILED;
900 }
901 int32_t ret = dpService->DeleteSessionKey(userId, sessionKeyId);
902 if (retryErrCodes_.count(ret) != 0) {
903 ret = RetryClientRequest(ret, &IDistributedDeviceProfile::DeleteSessionKey, userId, sessionKeyId);
904 }
905 return ret;
906 }
907
PutLocalServiceInfo(const LocalServiceInfo & localServiceInfo)908 int32_t DistributedDeviceProfileClient::PutLocalServiceInfo(const LocalServiceInfo& localServiceInfo)
909 {
910 auto dpService = GetDeviceProfileService();
911 if (dpService == nullptr) {
912 HILOGE("get dp service failed");
913 return DP_GET_SERVICE_FAILED;
914 }
915 int32_t ret = dpService->PutLocalServiceInfo(localServiceInfo);
916 if (retryErrCodes_.count(ret) != 0) {
917 ret = RetryClientRequest(ret, &IDistributedDeviceProfile::PutLocalServiceInfo, localServiceInfo);
918 }
919 return ret;
920 }
921
UpdateLocalServiceInfo(const LocalServiceInfo & localServiceInfo)922 int32_t DistributedDeviceProfileClient::UpdateLocalServiceInfo(const LocalServiceInfo& localServiceInfo)
923 {
924 auto dpService = GetDeviceProfileService();
925 if (dpService == nullptr) {
926 HILOGE("get dp service failed");
927 return DP_GET_SERVICE_FAILED;
928 }
929 int32_t ret = dpService->UpdateLocalServiceInfo(localServiceInfo);
930 if (retryErrCodes_.count(ret) != 0) {
931 ret = RetryClientRequest(ret, &IDistributedDeviceProfile::UpdateLocalServiceInfo, localServiceInfo);
932 }
933 return ret;
934 }
935
GetLocalServiceInfoByBundleAndPinType(const std::string & bundleName,int32_t pinExchangeType,LocalServiceInfo & localServiceInfo)936 int32_t DistributedDeviceProfileClient::GetLocalServiceInfoByBundleAndPinType(const std::string& bundleName,
937 int32_t pinExchangeType, LocalServiceInfo& localServiceInfo)
938 {
939 auto dpService = GetDeviceProfileService();
940 if (dpService == nullptr) {
941 HILOGE("get dp service failed");
942 return DP_GET_SERVICE_FAILED;
943 }
944 int32_t ret = dpService->GetLocalServiceInfoByBundleAndPinType(bundleName, pinExchangeType, localServiceInfo);
945 if (retryErrCodes_.count(ret) != 0) {
946 ret = RetryClientRequest(ret, &IDistributedDeviceProfile::GetLocalServiceInfoByBundleAndPinType, bundleName,
947 pinExchangeType, localServiceInfo);
948 }
949 return ret;
950 }
951
DeleteLocalServiceInfo(const std::string & bundleName,int32_t pinExchangeType)952 int32_t DistributedDeviceProfileClient::DeleteLocalServiceInfo(const std::string& bundleName,
953 int32_t pinExchangeType)
954 {
955 auto dpService = GetDeviceProfileService();
956 if (dpService == nullptr) {
957 HILOGE("get dp service failed");
958 return DP_GET_SERVICE_FAILED;
959 }
960 int32_t ret = dpService->DeleteLocalServiceInfo(bundleName, pinExchangeType);
961 if (retryErrCodes_.count(ret) != 0) {
962 ret = RetryClientRequest(ret, &IDistributedDeviceProfile::DeleteLocalServiceInfo, bundleName, pinExchangeType);
963 }
964 return ret;
965 }
966
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)967 void DistributedDeviceProfileClient::SystemAbilityListener::OnRemoveSystemAbility(int32_t systemAbilityId,
968 const std::string &deviceId)
969 {
970 HILOGD("dp sa removed");
971 }
972
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)973 void DistributedDeviceProfileClient::SystemAbilityListener::OnAddSystemAbility(int32_t systemAbilityId,
974 const std::string &deviceId)
975 {
976 HILOGI("dp sa started");
977 DistributedDeviceProfileClient::GetInstance().StartThreadSendSubscribeInfos();
978 DistributedDeviceProfileClient::GetInstance().StartThreadReSubscribePinCodeInvalid();
979 DistributedDeviceProfileClient::GetInstance().ReSubscribeDeviceProfileInited();
980 DistributedDeviceProfileClient::GetInstance().StartThreadReRegisterBusinessCallback();
981 }
982
ReleaseSubscribeDeviceProfileSA()983 void DistributedDeviceProfileClient::ReleaseSubscribeDeviceProfileSA()
984 {
985 std::lock_guard<std::mutex> lock(saListenerLock_);
986 if (saListenerCallback_ == nullptr) {
987 return;
988 }
989
990 auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
991 if (samgrProxy == nullptr) {
992 HILOGE("get samgr failed");
993 saListenerCallback_ = nullptr;
994 return;
995 }
996
997 int32_t ret = samgrProxy->UnSubscribeSystemAbility(DISTRIBUTED_DEVICE_PROFILE_SA_ID, saListenerCallback_);
998 if (ret != DP_SUCCESS) {
999 HILOGE("unsubscribe dp sa failed, ret=%{public}d", ret);
1000 return;
1001 }
1002
1003 HILOGD("unsubscribe dp sa success");
1004 }
1005
ReleaseSubscribeDeviceProfileInited()1006 void DistributedDeviceProfileClient::ReleaseSubscribeDeviceProfileInited()
1007 {
1008 int32_t saId = 0;
1009 {
1010 std::lock_guard<std::mutex> lock(dpInitedLock_);
1011 if (dpInitedCallback_ == nullptr) {
1012 return;
1013 }
1014 saId = saId_;
1015 }
1016
1017 int32_t ret = UnSubscribeDeviceProfileInited(saId);
1018 if (ret != DP_SUCCESS) {
1019 HILOGE("unsubscribe device profile inited failed, ret=%{public}d", ret);
1020 return;
1021 }
1022
1023 HILOGD("unsubscribe device profile inited success");
1024 }
1025
ReleaseSubscribePinCodeInvalid()1026 void DistributedDeviceProfileClient::ReleaseSubscribePinCodeInvalid()
1027 {
1028 std::string bundleName = "";
1029 int32_t pinExchangeType = DEFAULT_PIN_EXCHANGE_TYPE;
1030 {
1031 std::lock_guard<std::mutex> lock(pinCodeLock_);
1032 if (pinCodeCallback_ == nullptr) {
1033 return;
1034 }
1035 bundleName = bundleName_;
1036 pinExchangeType = pinExchangeType_;
1037 }
1038
1039 int32_t ret = UnSubscribePinCodeInvalid(bundleName, pinExchangeType);
1040 if (ret != DP_SUCCESS) {
1041 HILOGE("unscribe pincode invalid failed, ret=%{public}d", ret);
1042 return;
1043 }
1044
1045 HILOGD("unscribe pincode invalid success");
1046 }
1047
ReleaseDeathRecipient()1048 void DistributedDeviceProfileClient::ReleaseDeathRecipient()
1049 {
1050 std::lock_guard<std::mutex> lock(serviceLock_);
1051 if (dpProxy_ != nullptr && dpProxy_->AsObject() != nullptr && dpDeathRecipient_ != nullptr) {
1052 dpProxy_->AsObject()->RemoveDeathRecipient(dpDeathRecipient_);
1053 }
1054 dpDeathRecipient_ = nullptr;
1055 dpProxy_ = nullptr;
1056 }
1057
1058 template <typename Method, typename... Args>
RetryClientRequest(int32_t firesRet,Method method,Args &&...args)1059 int32_t DistributedDeviceProfileClient::RetryClientRequest(int32_t firesRet, Method method, Args&& ... args)
1060 {
1061 int32_t ret = firesRet;
1062 int32_t delays[MAX_RETRY_TIMES] = {US_100000, US_200000, US_300000, US_400000, US_500000, US_500000, US_500000};
1063 for (int32_t i = 0; i < MAX_RETRY_TIMES; ++i) {
1064 HILOGI("retry times:%{public}d, retry reason:%{public}d", i + 1, ret);
1065 usleep(delays[i]);
1066 auto dpService = GetDeviceProfileService();
1067 if (dpService == nullptr) {
1068 HILOGE("get dp service failed");
1069 return DP_GET_SERVICE_FAILED;
1070 }
1071 ret = (dpService->*method)(std::forward<Args>(args)...);
1072 if (retryErrCodes_.count(ret) == 0) {
1073 break;
1074 }
1075 }
1076 HILOGI("retry end, reason:%{public}d", ret);
1077 return ret;
1078 }
1079
ReleaseResource()1080 void DistributedDeviceProfileClient::ReleaseResource()
1081 {
1082 ReleaseSubscribeDeviceProfileSA();
1083 ReleaseSubscribePinCodeInvalid();
1084 ReleaseSubscribeDeviceProfileInited();
1085 ReleaseDeathRecipient();
1086 ReleaseRegisterBusinessCallback();
1087 }
1088
StartThreadReRegisterBusinessCallback()1089 void DistributedDeviceProfileClient::StartThreadReRegisterBusinessCallback()
1090 {
1091 HILOGI("Send Register Business Callback cache in proxy to service!");
1092 std::thread(&DistributedDeviceProfileClient::ReRegisterBusinessCallback, this).detach();
1093 }
1094
ReRegisterBusinessCallback()1095 void DistributedDeviceProfileClient::ReRegisterBusinessCallback()
1096 {
1097 HILOGI("call");
1098 std::string saId = "";
1099 std::string businessKey = "";
1100 sptr<IBusinessCallback> businessCallback = nullptr;
1101 {
1102 std::lock_guard<std::mutex> lock(businessLock_);
1103 if (businessCallback_ == nullptr) {
1104 HILOGI("not use Retry Register Business Callback");
1105 return;
1106 }
1107 saId = strSaId_;
1108 businessKey = businessKey_;
1109 businessCallback = businessCallback_;
1110 }
1111 int32_t ret = RegisterBusinessCallback(saId, businessKey, businessCallback);
1112 if (ret != DP_SUCCESS) {
1113 HILOGE("Retry Register Business Callback failed");
1114 return;
1115 }
1116 HILOGI("Retry Register Business Callback succeed");
1117 }
1118
ReleaseRegisterBusinessCallback()1119 void DistributedDeviceProfileClient::ReleaseRegisterBusinessCallback()
1120 {
1121 std::string saId = "";
1122 std::string businessKey = "";
1123 {
1124 std::lock_guard<std::mutex> lock(businessLock_);
1125 if (businessCallback_ == nullptr) {
1126 return;
1127 }
1128 saId = strSaId_;
1129 businessKey = businessKey_;
1130 }
1131
1132 int32_t ret = UnRegisterBusinessCallback(saId, businessKey);
1133 if (ret != DP_SUCCESS) {
1134 HILOGE("unRegisterBusinessCallback failed, ret=%{public}d", ret);
1135 return;
1136 }
1137
1138 HILOGD("ReleaseRegisterBusinessCallback success");
1139 }
1140
RegisterBusinessCallback(const std::string & saId,const std::string & businessKey,sptr<IBusinessCallback> businessCallback)1141 int32_t DistributedDeviceProfileClient::RegisterBusinessCallback(const std::string& saId,
1142 const std::string& businessKey, sptr<IBusinessCallback> businessCallback)
1143 {
1144 auto dpService = GetDeviceProfileService();
1145 if (dpService == nullptr) {
1146 HILOGE("Get dp service failed");
1147 return DP_GET_SERVICE_FAILED;
1148 }
1149 if (saId.empty() || businessKey.empty() || businessCallback == nullptr) {
1150 HILOGE("Invalid parameters: saId or businessKey is empty, or businessCallback is nullptr");
1151 return DP_INVALID_PARAM;
1152 }
1153 sptr<IRemoteObject> innerBusinessCallback = businessCallback->AsObject();
1154 if (innerBusinessCallback == nullptr) {
1155 HILOGE("businessCallback ipc cast fail!");
1156 return DP_INVALID_PARAM;
1157 }
1158 {
1159 std::lock_guard<std::mutex> lock(businessLock_);
1160 int32_t ret = dpService->RegisterBusinessCallback(saId, businessKey, innerBusinessCallback);
1161 if (ret != DP_SUCCESS) {
1162 HILOGE("Subscribe DP Inited failed!");
1163 return ret;
1164 }
1165 strSaId_ = saId;
1166 businessKey_ = businessKey;
1167 businessCallback_ = businessCallback;
1168 }
1169 return DP_SUCCESS;
1170 }
1171
UnRegisterBusinessCallback(const std::string & saId,const std::string & businessKey)1172 int32_t DistributedDeviceProfileClient::UnRegisterBusinessCallback(const std::string& saId,
1173 const std::string& businessKey)
1174 {
1175 if (businessCallback_ == nullptr) {
1176 HILOGE("not subscribe pincode invalid, no need unsubscribe pincode invalid");
1177 return DP_SUCCESS;
1178 }
1179 auto dpService = GetDeviceProfileService();
1180 if (dpService == nullptr) {
1181 HILOGE("Get dp service failed");
1182 return DP_GET_SERVICE_FAILED;
1183 }
1184 if (saId.empty() || businessKey.empty()) {
1185 HILOGE("Invalid parameters: saId or businessKey is empty");
1186 return DP_INVALID_PARAM;
1187 }
1188 {
1189 std::lock_guard<std::mutex> lock(businessLock_);
1190 int32_t ret = dpService->UnRegisterBusinessCallback(saId, businessKey);
1191 if (ret != DP_SUCCESS) {
1192 HILOGE("Unsubscribe DP Inited failed!");
1193 businessCallback_ = nullptr;
1194 return ret;
1195 }
1196 businessCallback_ = nullptr;
1197 }
1198 return DP_SUCCESS;
1199 }
1200
PutBusinessEvent(const BusinessEvent & event)1201 int32_t DistributedDeviceProfileClient::PutBusinessEvent(const BusinessEvent& event)
1202 {
1203 auto dpService = GetDeviceProfileService();
1204 if (dpService == nullptr) {
1205 HILOGE("Get dp service failed");
1206 return DP_GET_SERVICE_FAILED;
1207 }
1208 return dpService->PutBusinessEvent(event);
1209 }
1210
GetBusinessEvent(BusinessEvent & event)1211 int32_t DistributedDeviceProfileClient::GetBusinessEvent(BusinessEvent& event)
1212 {
1213 auto dpService = GetDeviceProfileService();
1214 if (dpService == nullptr) {
1215 HILOGE("Get dp service failed");
1216 return DP_GET_SERVICE_FAILED;
1217 }
1218 return dpService->GetBusinessEvent(event);
1219 }
1220 } // namespace DeviceProfile
1221 } // namespace OHOS
1222