1 /*
2 * Copyright (c) 2022 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 "dm_publish_manager.h"
17 #include "dm_anonymous.h"
18 #include "dm_constants.h"
19 #include "dm_log.h"
20
21 namespace OHOS {
22 namespace DistributedHardware {
23 constexpr const char* PUBLISH_DISCOVERY_TIMEOUT_TASK = "deviceManagerTimer:publish";
24 const int32_t PUBLISH_DISCOVERY_TIMEOUT = 120;
25
DmPublishManager(std::shared_ptr<SoftbusConnector> softbusConnector,std::shared_ptr<IDeviceManagerServiceListener> listener)26 DmPublishManager::DmPublishManager(std::shared_ptr<SoftbusConnector> softbusConnector,
27 std::shared_ptr<IDeviceManagerServiceListener> listener)
28 : softbusConnector_(softbusConnector), listener_(listener)
29 {
30 LOGI("DmPublishManager constructor");
31 }
32
~DmPublishManager()33 DmPublishManager::~DmPublishManager()
34 {
35 LOGI("DmPublishManager destructor");
36 }
37
CfgPublishTimer()38 void DmPublishManager::CfgPublishTimer()
39 {
40 if (timer_ == nullptr) {
41 timer_ = std::make_shared<DmTimer>();
42 }
43 timer_->StartTimer(std::string(PUBLISH_DISCOVERY_TIMEOUT_TASK), PUBLISH_DISCOVERY_TIMEOUT,
44 [this] (std::string name) {
45 DmPublishManager::HandlePublishTimeout(name);
46 });
47 }
48
CheckPublishQueue(const std::string & pkgName)49 int32_t DmPublishManager::CheckPublishQueue(const std::string &pkgName)
50 {
51 if (publishQueue_.empty()) {
52 return DM_OK;
53 }
54
55 if (pkgName == publishQueue_.front()) {
56 LOGE("DmPublishManager::pkgName : %s PublishDeviceDiscovery repeated", pkgName.c_str());
57 return ERR_DM_PUBLISH_REPEATED;
58 } else {
59 LOGI("DmPublishManager::UnPublishDeviceDiscovery the preview pkgName : %s",
60 publishQueue_.front().c_str());
61 UnPublishDeviceDiscovery(publishQueue_.front(),
62 publishContextMap_[publishQueue_.front()].publishInfo.publishId);
63 return DM_OK;
64 }
65 }
66
PublishDeviceDiscovery(const std::string & pkgName,const DmPublishInfo & publishInfo)67 int32_t DmPublishManager::PublishDeviceDiscovery(const std::string &pkgName, const DmPublishInfo &publishInfo)
68 {
69 int32_t ret = CheckPublishQueue(pkgName);
70 if (ret != DM_OK) {
71 return ret;
72 }
73 std::lock_guard<std::mutex> autoLock(locks_);
74 publishQueue_.push(pkgName);
75 DmPublishContext context = {pkgName, publishInfo};
76 publishContextMap_.emplace(pkgName, context);
77 softbusConnector_->RegisterSoftbusPublishCallback(pkgName,
78 std::shared_ptr<ISoftbusPublishCallback>(shared_from_this()));
79 CfgPublishTimer();
80 return softbusConnector_->PublishDiscovery(publishInfo);
81 }
82
UnPublishDeviceDiscovery(const std::string & pkgName,int32_t publishId)83 int32_t DmPublishManager::UnPublishDeviceDiscovery(const std::string &pkgName, int32_t publishId)
84 {
85 if (pkgName.empty()) {
86 LOGE("Invalid parameter, pkgName is empty.");
87 return ERR_DM_INPUT_PARA_INVALID;
88 }
89 std::lock_guard<std::mutex> autoLock(locks_);
90 if (!publishQueue_.empty()) {
91 publishQueue_.pop();
92 }
93 if (!publishContextMap_.empty()) {
94 publishContextMap_.erase(pkgName);
95 softbusConnector_->UnRegisterSoftbusPublishCallback(pkgName);
96 timer_->DeleteTimer(std::string(PUBLISH_DISCOVERY_TIMEOUT_TASK));
97 }
98 return softbusConnector_->UnPublishDiscovery(publishId);
99 }
100
OnPublishResult(const std::string & pkgName,int32_t publishId,int32_t publishResult)101 void DmPublishManager::OnPublishResult(const std::string &pkgName, int32_t publishId, int32_t publishResult)
102 {
103 if (pkgName.empty()) {
104 LOGE("Invalid parameter, pkgName is empty.");
105 return;
106 }
107 LOGI("DmPublishManager::OnPublishResult, publishId = %d, publishResult = %d", publishId, publishResult);
108 if (publishResult != 0) {
109 UnPublishDeviceDiscovery(pkgName, publishId);
110 }
111 listener_->OnPublishResult(pkgName, publishId, publishResult);
112 }
113
HandlePublishTimeout(std::string name)114 void DmPublishManager::HandlePublishTimeout(std::string name)
115 {
116 LOGI("DmPublishManager::HandlePublishDiscoveryTimeout");
117 if (publishQueue_.empty()) {
118 LOGE("HandlePublishDiscoveryTimeout: Publish discovery queue is empty.");
119 return;
120 }
121
122 std::string pkgName = publishQueue_.front();
123 auto iter = publishContextMap_.find(pkgName);
124 if (iter == publishContextMap_.end()) {
125 LOGE("HandleDiscoveryTimeout:pkgName %s fail to publish", GetAnonyString(pkgName).c_str());
126 return;
127 }
128 UnPublishDeviceDiscovery(pkgName, iter->second.publishInfo.publishId);
129 }
130 } // namespace DistributedHardware
131 } // namespace OHOS
132