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 : %{public}s PublishDeviceDiscovery repeated", pkgName.c_str());
57         return ERR_DM_PUBLISH_REPEATED;
58     } else {
59         LOGI("DmPublishManager::UnPublishDeviceDiscovery the preview pkgName : %{public}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         if (timer_ != nullptr) {
97             timer_->DeleteTimer(std::string(PUBLISH_DISCOVERY_TIMEOUT_TASK));
98         }
99     }
100     return softbusConnector_->UnPublishDiscovery(publishId);
101 }
102 
OnPublishResult(const std::string & pkgName,int32_t publishId,int32_t publishResult)103 void DmPublishManager::OnPublishResult(const std::string &pkgName, int32_t publishId, int32_t publishResult)
104 {
105     if (pkgName.empty()) {
106         LOGE("Invalid parameter, pkgName is empty.");
107         return;
108     }
109     LOGI("DmPublishManager::OnPublishResult, publishId = %{public}d, publishResult = %{public}d", publishId,
110         publishResult);
111     if (publishResult != 0) {
112         UnPublishDeviceDiscovery(pkgName, publishId);
113     }
114     listener_->OnPublishResult(pkgName, publishId, publishResult);
115 }
116 
HandlePublishTimeout(std::string name)117 void DmPublishManager::HandlePublishTimeout(std::string name)
118 {
119     (void)name;
120     LOGI("DmPublishManager::HandlePublishDiscoveryTimeout");
121     if (publishQueue_.empty()) {
122         LOGE("HandlePublishDiscoveryTimeout: Publish discovery queue is empty.");
123         return;
124     }
125 
126     std::string pkgName = publishQueue_.front();
127     auto iter = publishContextMap_.find(pkgName);
128     if (iter == publishContextMap_.end()) {
129         LOGE("HandleDiscoveryTimeout:pkgName %{public}s fail to publish", GetAnonyString(pkgName).c_str());
130         return;
131     }
132     UnPublishDeviceDiscovery(pkgName, iter->second.publishInfo.publishId);
133 }
134 } // namespace DistributedHardware
135 } // namespace OHOS
136